blob: 0ec1b57341bf4b433827a0222d727ce658b3b1e0 [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 */
Jeff Sharkey30e06bb2017-04-24 11:18:03 -0600679 public @Visibility int visibility;
680
681 /** @hide */
682 @IntDef(prefix = { "VISIBILITY_" }, value = {
683 VISIBILITY_PUBLIC,
684 VISIBILITY_PRIVATE,
685 VISIBILITY_SECRET,
686 })
687 @Retention(RetentionPolicy.SOURCE)
688 public @interface Visibility {}
Dan Sandler0bf2ed82013-12-21 23:33:41 -0600689
Griff Hazenfc3922d2014-08-20 11:56:44 -0700690 /**
691 * Notification visibility: Show this notification in its entirety on all lockscreens.
692 *
693 * {@see #visibility}
694 */
Dan Sandler0bf2ed82013-12-21 23:33:41 -0600695 public static final int VISIBILITY_PUBLIC = 1;
Griff Hazenfc3922d2014-08-20 11:56:44 -0700696
697 /**
698 * Notification visibility: Show this notification on all lockscreens, but conceal sensitive or
699 * private information on secure lockscreens.
700 *
701 * {@see #visibility}
702 */
Dan Sandler0bf2ed82013-12-21 23:33:41 -0600703 public static final int VISIBILITY_PRIVATE = 0;
Griff Hazenfc3922d2014-08-20 11:56:44 -0700704
705 /**
706 * Notification visibility: Do not reveal any part of this notification on a secure lockscreen.
707 *
708 * {@see #visibility}
709 */
Dan Sandler0bf2ed82013-12-21 23:33:41 -0600710 public static final int VISIBILITY_SECRET = -1;
711
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500712 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400713 * Notification category: incoming call (voice or video) or similar synchronous communication request.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500714 */
John Spurlockfd7f1e02014-03-18 16:41:57 -0400715 public static final String CATEGORY_CALL = "call";
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500716
717 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400718 * Notification category: incoming direct message (SMS, instant message, etc.).
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500719 */
John Spurlockfd7f1e02014-03-18 16:41:57 -0400720 public static final String CATEGORY_MESSAGE = "msg";
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500721
722 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400723 * Notification category: asynchronous bulk message (email).
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500724 */
John Spurlockfd7f1e02014-03-18 16:41:57 -0400725 public static final String CATEGORY_EMAIL = "email";
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500726
727 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400728 * Notification category: calendar event.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500729 */
John Spurlockfd7f1e02014-03-18 16:41:57 -0400730 public static final String CATEGORY_EVENT = "event";
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500731
732 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400733 * Notification category: promotion or advertisement.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500734 */
John Spurlockfd7f1e02014-03-18 16:41:57 -0400735 public static final String CATEGORY_PROMO = "promo";
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500736
737 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400738 * Notification category: alarm or timer.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500739 */
John Spurlockfd7f1e02014-03-18 16:41:57 -0400740 public static final String CATEGORY_ALARM = "alarm";
741
742 /**
743 * Notification category: progress of a long-running background operation.
744 */
745 public static final String CATEGORY_PROGRESS = "progress";
746
747 /**
748 * Notification category: social network or sharing update.
749 */
750 public static final String CATEGORY_SOCIAL = "social";
751
752 /**
753 * Notification category: error in background operation or authentication status.
754 */
755 public static final String CATEGORY_ERROR = "err";
756
757 /**
758 * Notification category: media transport control for playback.
759 */
760 public static final String CATEGORY_TRANSPORT = "transport";
761
762 /**
763 * Notification category: system or device status update. Reserved for system use.
764 */
765 public static final String CATEGORY_SYSTEM = "sys";
766
767 /**
768 * Notification category: indication of running background service.
769 */
770 public static final String CATEGORY_SERVICE = "service";
771
772 /**
John Spurlock0a69c8c2014-03-21 13:30:57 -0400773 * Notification category: a specific, timely recommendation for a single thing.
774 * For example, a news app might want to recommend a news story it believes the user will
775 * want to read next.
776 */
777 public static final String CATEGORY_RECOMMENDATION = "recommendation";
778
779 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400780 * Notification category: ongoing information about device or contextual status.
781 */
782 public static final String CATEGORY_STATUS = "status";
783
784 /**
John Spurlock24d3dad2015-04-02 12:24:02 -0400785 * Notification category: user-scheduled reminder.
786 */
787 public static final String CATEGORY_REMINDER = "reminder";
788
789 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400790 * One of the predefined notification categories (see the <code>CATEGORY_*</code> constants)
791 * that best describes this Notification. May be used by the system for ranking and filtering.
792 */
793 public String category;
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500794
Griff Hazen5cadc3b2014-05-20 09:55:39 -0700795 private String mGroupKey;
796
797 /**
798 * Get the key used to group this notification into a cluster or stack
799 * with other notifications on devices which support such rendering.
800 */
801 public String getGroup() {
802 return mGroupKey;
803 }
804
805 private String mSortKey;
806
807 /**
808 * Get a sort key that orders this notification among other notifications from the
809 * same package. This can be useful if an external sort was already applied and an app
810 * would like to preserve this. Notifications will be sorted lexicographically using this
811 * value, although providing different priorities in addition to providing sort key may
812 * cause this value to be ignored.
813 *
814 * <p>This sort key can also be used to order members of a notification group. See
815 * {@link Builder#setGroup}.
816 *
817 * @see String#compareTo(String)
818 */
819 public String getSortKey() {
820 return mSortKey;
821 }
822
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500823 /**
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400824 * Additional semantic data to be carried around with this Notification.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400825 * <p>
826 * The extras keys defined here are intended to capture the original inputs to {@link Builder}
827 * APIs, and are intended to be used by
828 * {@link android.service.notification.NotificationListenerService} implementations to extract
829 * detailed information from notification objects.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500830 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400831 public Bundle extras = new Bundle();
832
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400833 /**
Felipe Lemedd85da62016-06-28 11:29:54 -0700834 * All pending intents in the notification as the system needs to be able to access them but
835 * touching the extras bundle in the system process is not safe because the bundle may contain
Svet Ganovddb94882016-06-23 19:55:24 -0700836 * custom parcelable objects.
837 *
838 * @hide
839 */
Felipe Lemedd85da62016-06-28 11:29:54 -0700840 public ArraySet<PendingIntent> allPendingIntents;
Svet Ganovddb94882016-06-23 19:55:24 -0700841
842 /**
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400843 * {@link #extras} key: this is the title of the notification,
844 * as supplied to {@link Builder#setContentTitle(CharSequence)}.
845 */
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -0500846 public static final String EXTRA_TITLE = "android.title";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400847
848 /**
849 * {@link #extras} key: this is the title of the notification when shown in expanded form,
850 * e.g. as supplied to {@link BigTextStyle#setBigContentTitle(CharSequence)}.
851 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400852 public static final String EXTRA_TITLE_BIG = EXTRA_TITLE + ".big";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400853
854 /**
855 * {@link #extras} key: this is the main text payload, as supplied to
856 * {@link Builder#setContentText(CharSequence)}.
857 */
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -0500858 public static final String EXTRA_TEXT = "android.text";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400859
860 /**
861 * {@link #extras} key: this is a third line of text, as supplied to
862 * {@link Builder#setSubText(CharSequence)}.
863 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400864 public static final String EXTRA_SUB_TEXT = "android.subText";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400865
866 /**
Adrian Roose458aa82015-12-08 16:17:19 -0800867 * {@link #extras} key: this is the remote input history, as supplied to
868 * {@link Builder#setRemoteInputHistory(CharSequence[])}.
Adrian Roos005e7742016-04-13 15:02:20 -0700869 *
870 * Apps can fill this through {@link Builder#setRemoteInputHistory(CharSequence[])}
871 * with the most recent inputs that have been sent through a {@link RemoteInput} of this
872 * Notification and are expected to clear it once the it is no longer relevant (e.g. for chat
873 * notifications once the other party has responded).
874 *
875 * The extra with this key is of type CharSequence[] and contains the most recent entry at
876 * the 0 index, the second most recent at the 1 index, etc.
877 *
878 * @see Builder#setRemoteInputHistory(CharSequence[])
Adrian Roose458aa82015-12-08 16:17:19 -0800879 */
880 public static final String EXTRA_REMOTE_INPUT_HISTORY = "android.remoteInputHistory";
881
882 /**
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400883 * {@link #extras} key: this is a small piece of additional text as supplied to
884 * {@link Builder#setContentInfo(CharSequence)}.
885 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400886 public static final String EXTRA_INFO_TEXT = "android.infoText";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400887
888 /**
889 * {@link #extras} key: this is a line of summary information intended to be shown
890 * alongside expanded notifications, as supplied to (e.g.)
891 * {@link BigTextStyle#setSummaryText(CharSequence)}.
892 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400893 public static final String EXTRA_SUMMARY_TEXT = "android.summaryText";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400894
895 /**
Christoph Studer4600f9b2014-07-22 22:44:43 +0200896 * {@link #extras} key: this is the longer text shown in the big form of a
897 * {@link BigTextStyle} notification, as supplied to
898 * {@link BigTextStyle#bigText(CharSequence)}.
899 */
900 public static final String EXTRA_BIG_TEXT = "android.bigText";
901
902 /**
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400903 * {@link #extras} key: this is the resource ID of the notification's main small icon, as
904 * supplied to {@link Builder#setSmallIcon(int)}.
Julia Reynoldse071abd2017-03-22 10:52:11 -0400905 *
906 * @deprecated Use {@link #getSmallIcon()}, which supports a wider variety of icon sources.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400907 */
Julia Reynoldse071abd2017-03-22 10:52:11 -0400908 @Deprecated
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -0500909 public static final String EXTRA_SMALL_ICON = "android.icon";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400910
911 /**
912 * {@link #extras} key: this is a bitmap to be used instead of the small icon when showing the
913 * notification payload, as
914 * supplied to {@link Builder#setLargeIcon(android.graphics.Bitmap)}.
Julia Reynoldse071abd2017-03-22 10:52:11 -0400915 *
916 * @deprecated Use {@link #getLargeIcon()}, which supports a wider variety of icon sources.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400917 */
Julia Reynoldse071abd2017-03-22 10:52:11 -0400918 @Deprecated
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400919 public static final String EXTRA_LARGE_ICON = "android.largeIcon";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400920
921 /**
922 * {@link #extras} key: this is a bitmap to be used instead of the one from
923 * {@link Builder#setLargeIcon(android.graphics.Bitmap)} when the notification is
924 * shown in its expanded form, as supplied to
925 * {@link BigPictureStyle#bigLargeIcon(android.graphics.Bitmap)}.
926 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400927 public static final String EXTRA_LARGE_ICON_BIG = EXTRA_LARGE_ICON + ".big";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400928
929 /**
930 * {@link #extras} key: this is the progress value supplied to
931 * {@link Builder#setProgress(int, int, boolean)}.
932 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400933 public static final String EXTRA_PROGRESS = "android.progress";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400934
935 /**
936 * {@link #extras} key: this is the maximum value supplied to
937 * {@link Builder#setProgress(int, int, boolean)}.
938 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400939 public static final String EXTRA_PROGRESS_MAX = "android.progressMax";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400940
941 /**
942 * {@link #extras} key: whether the progress bar is indeterminate, supplied to
943 * {@link Builder#setProgress(int, int, boolean)}.
944 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400945 public static final String EXTRA_PROGRESS_INDETERMINATE = "android.progressIndeterminate";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400946
947 /**
948 * {@link #extras} key: whether {@link #when} should be shown as a count-up timer (specifically
949 * a {@link android.widget.Chronometer}) instead of a timestamp, as supplied to
950 * {@link Builder#setUsesChronometer(boolean)}.
951 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400952 public static final String EXTRA_SHOW_CHRONOMETER = "android.showChronometer";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400953
954 /**
Selim Cinek81c23aa2016-02-25 16:23:13 -0800955 * {@link #extras} key: whether the chronometer set on the notification should count down
956 * instead of counting up. Is only relevant if key {@link #EXTRA_SHOW_CHRONOMETER} is present.
Adrian Roos96b7e202016-05-17 13:50:38 -0700957 * This extra is a boolean. The default is false.
Selim Cinek81c23aa2016-02-25 16:23:13 -0800958 */
Adrian Roos96b7e202016-05-17 13:50:38 -0700959 public static final String EXTRA_CHRONOMETER_COUNT_DOWN = "android.chronometerCountDown";
Selim Cinek81c23aa2016-02-25 16:23:13 -0800960
961 /**
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400962 * {@link #extras} key: whether {@link #when} should be shown,
963 * as supplied to {@link Builder#setShowWhen(boolean)}.
964 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400965 public static final String EXTRA_SHOW_WHEN = "android.showWhen";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400966
967 /**
968 * {@link #extras} key: this is a bitmap to be shown in {@link BigPictureStyle} expanded
969 * notifications, supplied to {@link BigPictureStyle#bigPicture(android.graphics.Bitmap)}.
970 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400971 public static final String EXTRA_PICTURE = "android.picture";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400972
973 /**
974 * {@link #extras} key: An array of CharSequences to show in {@link InboxStyle} expanded
975 * notifications, each of which was supplied to {@link InboxStyle#addLine(CharSequence)}.
976 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400977 public static final String EXTRA_TEXT_LINES = "android.textLines";
Dan Sandler842dd772014-05-15 09:36:47 -0400978
979 /**
980 * {@link #extras} key: A string representing the name of the specific
981 * {@link android.app.Notification.Style} used to create this notification.
982 */
Chris Wren91ad5632013-06-05 15:05:57 -0400983 public static final String EXTRA_TEMPLATE = "android.template";
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400984
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400985 /**
Chris Wrene6c48932014-09-29 17:19:27 -0400986 * {@link #extras} key: A String array containing the people that this notification relates to,
987 * each of which was supplied to {@link Builder#addPerson(String)}.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400988 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400989 public static final String EXTRA_PEOPLE = "android.people";
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500990
991 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400992 * Allow certain system-generated notifications to appear before the device is provisioned.
993 * Only available to notifications coming from the android package.
994 * @hide
995 */
Maurice Lam96c10032017-03-29 15:42:38 -0700996 @SystemApi
John Spurlockfd7f1e02014-03-18 16:41:57 -0400997 public static final String EXTRA_ALLOW_DURING_SETUP = "android.allowDuringSetup";
998
999 /**
Jose Limae9e3b3b2014-05-18 23:44:50 -07001000 * {@link #extras} key: A
1001 * {@link android.content.ContentUris content URI} pointing to an image that can be displayed
1002 * in the background when the notification is selected. The URI must point to an image stream
1003 * suitable for passing into
1004 * {@link android.graphics.BitmapFactory#decodeStream(java.io.InputStream)
1005 * BitmapFactory.decodeStream}; all other content types will be ignored. The content provider
1006 * URI used for this purpose must require no permissions to read the image data.
1007 */
1008 public static final String EXTRA_BACKGROUND_IMAGE_URI = "android.backgroundImageUri";
1009
1010 /**
Dan Sandler842dd772014-05-15 09:36:47 -04001011 * {@link #extras} key: A
Jeff Browndba34ba2014-06-24 20:46:03 -07001012 * {@link android.media.session.MediaSession.Token} associated with a
Dan Sandler842dd772014-05-15 09:36:47 -04001013 * {@link android.app.Notification.MediaStyle} notification.
1014 */
1015 public static final String EXTRA_MEDIA_SESSION = "android.mediaSession";
1016
1017 /**
Bryan Mawhinneye191f902014-07-22 12:50:09 +01001018 * {@link #extras} key: the indices of actions to be shown in the compact view,
1019 * as supplied to (e.g.) {@link MediaStyle#setShowActionsInCompactView(int...)}.
1020 */
1021 public static final String EXTRA_COMPACT_ACTIONS = "android.compactActions";
1022
Christoph Studer943aa672014-08-03 20:31:16 +02001023 /**
Alex Hillsfc737de2016-03-23 17:33:02 -04001024 * {@link #extras} key: the username to be displayed for all messages sent by the user including
1025 * direct replies
Adrian Roos96b7e202016-05-17 13:50:38 -07001026 * {@link android.app.Notification.MessagingStyle} notification. This extra is a
1027 * {@link CharSequence}
Alex Hillsfc737de2016-03-23 17:33:02 -04001028 */
1029 public static final String EXTRA_SELF_DISPLAY_NAME = "android.selfDisplayName";
1030
1031 /**
Adrian Roos96b7e202016-05-17 13:50:38 -07001032 * {@link #extras} key: a {@link CharSequence} to be displayed as the title to a conversation
Alex Hillsd9b04d92016-04-11 16:38:16 -04001033 * represented by a {@link android.app.Notification.MessagingStyle}
Alex Hillsfc737de2016-03-23 17:33:02 -04001034 */
Alex Hillsd9b04d92016-04-11 16:38:16 -04001035 public static final String EXTRA_CONVERSATION_TITLE = "android.conversationTitle";
Alex Hillsfc737de2016-03-23 17:33:02 -04001036
1037 /**
1038 * {@link #extras} key: an array of {@link android.app.Notification.MessagingStyle.Message}
1039 * bundles provided by a
Adrian Roos96b7e202016-05-17 13:50:38 -07001040 * {@link android.app.Notification.MessagingStyle} notification. This extra is a parcelable
1041 * array of bundles.
Alex Hillsfc737de2016-03-23 17:33:02 -04001042 */
1043 public static final String EXTRA_MESSAGES = "android.messages";
1044
1045 /**
Adrian Roos437cd562017-01-18 15:47:03 -08001046 * {@link #extras} key: an array of
1047 * {@link android.app.Notification.MessagingStyle#addHistoricMessage historic}
1048 * {@link android.app.Notification.MessagingStyle.Message} bundles provided by a
1049 * {@link android.app.Notification.MessagingStyle} notification. This extra is a parcelable
1050 * array of bundles.
1051 */
1052 public static final String EXTRA_HISTORIC_MESSAGES = "android.messages.historic";
1053
1054 /**
Selim Cinek7b9605b2017-01-19 17:36:00 -08001055 * {@link #extras} key: whether the notification should be colorized as
1056 * supplied to {@link Builder#setColorized(boolean)}}.
1057 */
1058 public static final String EXTRA_COLORIZED = "android.colorized";
1059
1060 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -04001061 * @hide
1062 */
1063 public static final String EXTRA_BUILDER_APPLICATION_INFO = "android.appInfo";
1064
Selim Cinek247fa012016-02-18 09:50:48 -08001065 /**
1066 * @hide
1067 */
1068 public static final String EXTRA_CONTAINS_CUSTOM_VIEW = "android.contains.customView";
1069
Shane Brennan472a3b32016-12-12 15:28:10 -08001070 /**
1071 * {@link #extras} key: the audio contents of this notification.
1072 *
1073 * This is for use when rendering the notification on an audio-focused interface;
1074 * the audio contents are a complete sound sample that contains the contents/body of the
1075 * notification. This may be used in substitute of a Text-to-Speech reading of the
1076 * notification. For example if the notification represents a voice message this should point
1077 * to the audio of that message.
1078 *
1079 * The data stored under this key should be a String representation of a Uri that contains the
1080 * audio contents in one of the following formats: WAV, PCM 16-bit, AMR-WB.
1081 *
1082 * This extra is unnecessary if you are using {@code MessagingStyle} since each {@code Message}
1083 * has a field for holding data URI. That field can be used for audio.
1084 * See {@code Message#setData}.
1085 *
1086 * Example usage:
1087 * <pre>
1088 * {@code
1089 * Notification.Builder myBuilder = (build your Notification as normal);
1090 * myBuilder.getExtras().putString(EXTRA_AUDIO_CONTENTS_URI, myAudioUri.toString());
1091 * }
1092 * </pre>
1093 */
1094 public static final String EXTRA_AUDIO_CONTENTS_URI = "android.audioContents";
1095
Dan Sandler80eaa592016-04-14 23:34:54 -04001096 /** @hide */
1097 @SystemApi
Dan Sandler732bd6c2016-04-12 14:20:32 -04001098 public static final String EXTRA_SUBSTITUTE_APP_NAME = "android.substName";
1099
Dan Sandlerd63f9322015-05-06 15:18:49 -04001100 private Icon mSmallIcon;
1101 private Icon mLargeIcon;
1102
Julia Reynoldsb5e44b72016-08-16 15:00:25 -04001103 private String mChannelId;
Julia Reynolds2a128742016-11-28 14:29:25 -05001104 private long mTimeout;
Julia Reynoldsb5e44b72016-08-16 15:00:25 -04001105
Julia Reynolds13d898c2017-02-02 12:22:05 -05001106 private String mShortcutId;
Julia Reynolds3aedded2017-03-31 14:42:09 -04001107 private CharSequence mSettingsText;
Julia Reynolds13d898c2017-02-02 12:22:05 -05001108
Julia Reynoldsa79c3712017-04-21 10:29:57 -04001109 /** @hide */
1110 @IntDef(prefix = { "GROUP_ALERT_" }, value = {
1111 GROUP_ALERT_ALL, GROUP_ALERT_CHILDREN, GROUP_ALERT_SUMMARY
1112 })
1113 @Retention(RetentionPolicy.SOURCE)
1114 public @interface GroupAlertBehavior {}
1115
1116 /**
1117 * Constant for {@link Builder#setGroupAlertBehavior(int)}, meaning that all notifications in a
1118 * group with sound or vibration ought to make sound or vibrate (respectively), so this
1119 * notification will not be muted when it is in a group.
1120 */
1121 public static final int GROUP_ALERT_ALL = 0;
1122
1123 /**
1124 * Constant for {@link Builder#setGroupAlertBehavior(int)}, meaning that all children
1125 * notification in a group should be silenced (no sound or vibration) even if they are posted
1126 * to a {@link NotificationChannel} that has sound and/or vibration. Use this constant to
1127 * mute this notification if this notification is a group child.
1128 *
1129 * <p> For example, you might want to use this constant if you post a number of children
1130 * notifications at once (say, after a periodic sync), and only need to notify the user
1131 * audibly once.
1132 */
1133 public static final int GROUP_ALERT_SUMMARY = 1;
1134
1135 /**
1136 * Constant for {@link Builder#setGroupAlertBehavior(int)}, meaning that the summary
1137 * notification in a group should be silenced (no sound or vibration) even if they are
1138 * posted to a {@link NotificationChannel} that has sound and/or vibration. Use this constant
1139 * to mute this notification if this notification is a group summary.
1140 *
1141 * <p>For example, you might want to use this constant if only the children notifications
1142 * in your group have content and the summary is only used to visually group notifications.
1143 */
1144 public static final int GROUP_ALERT_CHILDREN = 2;
1145
1146 private int mGroupAlertBehavior = GROUP_ALERT_ALL;
1147
Julia Reynolds13d898c2017-02-02 12:22:05 -05001148 /**
1149 * If this notification is being shown as a badge, always show as a number.
1150 */
1151 public static final int BADGE_ICON_NONE = 0;
1152
1153 /**
1154 * If this notification is being shown as a badge, use the {@link #getSmallIcon()} to
1155 * represent this notification.
1156 */
1157 public static final int BADGE_ICON_SMALL = 1;
1158
1159 /**
1160 * If this notification is being shown as a badge, use the {@link #getLargeIcon()} to
1161 * represent this notification.
1162 */
1163 public static final int BADGE_ICON_LARGE = 2;
Julia Reynolds7d5b4da2017-03-20 10:18:49 -04001164 private int mBadgeIcon = BADGE_ICON_NONE;
Julia Reynolds13d898c2017-02-02 12:22:05 -05001165
Chris Wren51c75102013-07-16 20:49:17 -04001166 /**
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001167 * Structure to encapsulate a named action that can be shown as part of this notification.
1168 * It must include an icon, a label, and a {@link PendingIntent} to be fired when the action is
1169 * selected by the user.
1170 * <p>
Griff Hazen959591e2014-05-15 22:26:18 -07001171 * Apps should use {@link Notification.Builder#addAction(int, CharSequence, PendingIntent)}
1172 * or {@link Notification.Builder#addAction(Notification.Action)}
1173 * to attach actions.
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001174 */
Daniel Sandlerea2a3172013-02-20 22:24:20 -05001175 public static class Action implements Parcelable {
Shane Brennan472a3b32016-12-12 15:28:10 -08001176 /**
1177 * {@link #extras} key: Keys to a {@link Parcelable} {@link ArrayList} of
1178 * {@link RemoteInput}s.
1179 *
1180 * This is intended for {@link RemoteInput}s that only accept data, meaning
1181 * {@link RemoteInput#getAllowFreeFormInput} is false, {@link RemoteInput#getChoices}
1182 * is null or empty, and {@link RemoteInput#getAllowedDataTypes is non-null and not
1183 * empty. These {@link RemoteInput}s will be ignored by devices that do not
1184 * support non-text-based {@link RemoteInput}s. See {@link Builder#build}.
1185 *
1186 * You can test if a RemoteInput matches these constraints using
1187 * {@link RemoteInput#isDataOnly}.
1188 */
1189 private static final String EXTRA_DATA_ONLY_INPUTS = "android.extra.DATA_ONLY_INPUTS";
1190
Griff Hazen959591e2014-05-15 22:26:18 -07001191 private final Bundle mExtras;
Dan Sandler86647982015-05-13 23:41:13 -04001192 private Icon mIcon;
Griff Hazen61a9e862014-05-22 16:05:19 -07001193 private final RemoteInput[] mRemoteInputs;
Alex Hills1f27f882017-01-12 11:24:46 -05001194 private boolean mAllowGeneratedReplies = true;
Griff Hazen959591e2014-05-15 22:26:18 -07001195
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001196 /**
1197 * Small icon representing the action.
Dan Sandler86647982015-05-13 23:41:13 -04001198 *
1199 * @deprecated Use {@link Action#getIcon()} instead.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001200 */
Dan Sandler86647982015-05-13 23:41:13 -04001201 @Deprecated
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001202 public int icon;
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001203
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001204 /**
1205 * Title of the action.
1206 */
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001207 public CharSequence title;
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001208
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001209 /**
1210 * Intent to send when the user invokes this action. May be null, in which case the action
1211 * may be rendered in a disabled presentation by the system UI.
1212 */
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001213 public PendingIntent actionIntent;
Griff Hazen959591e2014-05-15 22:26:18 -07001214
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001215 private Action(Parcel in) {
Dan Sandler86647982015-05-13 23:41:13 -04001216 if (in.readInt() != 0) {
1217 mIcon = Icon.CREATOR.createFromParcel(in);
Dan Sandler68079d52015-07-22 10:45:30 -04001218 if (mIcon.getType() == Icon.TYPE_RESOURCE) {
1219 icon = mIcon.getResId();
1220 }
Dan Sandler86647982015-05-13 23:41:13 -04001221 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001222 title = TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(in);
1223 if (in.readInt() == 1) {
1224 actionIntent = PendingIntent.CREATOR.createFromParcel(in);
1225 }
Jeff Sharkeya04c7a72016-03-18 12:20:36 -06001226 mExtras = Bundle.setDefusable(in.readBundle(), true);
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001227 mRemoteInputs = in.createTypedArray(RemoteInput.CREATOR);
Alex Hills42b0c4d2016-04-26 13:35:36 -04001228 mAllowGeneratedReplies = in.readInt() == 1;
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001229 }
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001230
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001231 /**
Dan Sandler86647982015-05-13 23:41:13 -04001232 * @deprecated Use {@link android.app.Notification.Action.Builder}.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001233 */
Dan Sandler86647982015-05-13 23:41:13 -04001234 @Deprecated
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001235 public Action(int icon, CharSequence title, PendingIntent intent) {
Alex Hills1f27f882017-01-12 11:24:46 -05001236 this(Icon.createWithResource("", icon), title, intent, new Bundle(), null, true);
Griff Hazen959591e2014-05-15 22:26:18 -07001237 }
1238
Adrian Roos7af53622016-10-12 13:44:05 -07001239 /** Keep in sync with {@link Notification.Action.Builder#Builder(Action)}! */
Dan Sandler86647982015-05-13 23:41:13 -04001240 private Action(Icon icon, CharSequence title, PendingIntent intent, Bundle extras,
Alex Hills42b0c4d2016-04-26 13:35:36 -04001241 RemoteInput[] remoteInputs, boolean allowGeneratedReplies) {
Dan Sandler86647982015-05-13 23:41:13 -04001242 this.mIcon = icon;
Gus Prevasf5bff46f2015-08-24 10:34:28 -04001243 if (icon != null && icon.getType() == Icon.TYPE_RESOURCE) {
1244 this.icon = icon.getResId();
1245 }
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001246 this.title = title;
1247 this.actionIntent = intent;
Griff Hazen959591e2014-05-15 22:26:18 -07001248 this.mExtras = extras != null ? extras : new Bundle();
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001249 this.mRemoteInputs = remoteInputs;
Alex Hills42b0c4d2016-04-26 13:35:36 -04001250 this.mAllowGeneratedReplies = allowGeneratedReplies;
Griff Hazen959591e2014-05-15 22:26:18 -07001251 }
1252
1253 /**
Dan Sandler86647982015-05-13 23:41:13 -04001254 * Return an icon representing the action.
1255 */
1256 public Icon getIcon() {
1257 if (mIcon == null && icon != 0) {
1258 // you snuck an icon in here without using the builder; let's try to keep it
1259 mIcon = Icon.createWithResource("", icon);
1260 }
1261 return mIcon;
1262 }
1263
1264 /**
Griff Hazen959591e2014-05-15 22:26:18 -07001265 * Get additional metadata carried around with this Action.
1266 */
1267 public Bundle getExtras() {
1268 return mExtras;
1269 }
1270
1271 /**
Alex Hills42b0c4d2016-04-26 13:35:36 -04001272 * Return whether the platform should automatically generate possible replies for this
1273 * {@link Action}
1274 */
1275 public boolean getAllowGeneratedReplies() {
1276 return mAllowGeneratedReplies;
1277 }
1278
1279 /**
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001280 * Get the list of inputs to be collected from the user when this action is sent.
Shane Brennan472a3b32016-12-12 15:28:10 -08001281 * May return null if no remote inputs were added. Only returns inputs which accept
1282 * a text input. For inputs which only accept data use {@link #getDataOnlyRemoteInputs}.
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001283 */
1284 public RemoteInput[] getRemoteInputs() {
1285 return mRemoteInputs;
1286 }
1287
1288 /**
Shane Brennan472a3b32016-12-12 15:28:10 -08001289 * Get the list of inputs to be collected from the user that ONLY accept data when this
1290 * action is sent. These remote inputs are guaranteed to return true on a call to
1291 * {@link RemoteInput#isDataOnly}.
1292 *
Shane Brennanf670d6c2017-04-25 13:05:45 -07001293 * Returns null if there are no data-only remote inputs.
Shane Brennan472a3b32016-12-12 15:28:10 -08001294 *
1295 * This method exists so that legacy RemoteInput collectors that pre-date the addition
1296 * of non-textual RemoteInputs do not access these remote inputs.
1297 */
1298 public RemoteInput[] getDataOnlyRemoteInputs() {
1299 return (RemoteInput[]) mExtras.getParcelableArray(EXTRA_DATA_ONLY_INPUTS);
1300 }
1301
1302 /**
Griff Hazen959591e2014-05-15 22:26:18 -07001303 * Builder class for {@link Action} objects.
1304 */
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001305 public static final class Builder {
Dan Sandler86647982015-05-13 23:41:13 -04001306 private final Icon mIcon;
Griff Hazen959591e2014-05-15 22:26:18 -07001307 private final CharSequence mTitle;
1308 private final PendingIntent mIntent;
Alex Hills1f27f882017-01-12 11:24:46 -05001309 private boolean mAllowGeneratedReplies = true;
Griff Hazen959591e2014-05-15 22:26:18 -07001310 private final Bundle mExtras;
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001311 private ArrayList<RemoteInput> mRemoteInputs;
Griff Hazen959591e2014-05-15 22:26:18 -07001312
1313 /**
1314 * Construct a new builder for {@link Action} object.
1315 * @param icon icon to show for this action
1316 * @param title the title of the action
1317 * @param intent the {@link PendingIntent} to fire when users trigger this action
1318 */
Dan Sandler86647982015-05-13 23:41:13 -04001319 @Deprecated
Griff Hazen959591e2014-05-15 22:26:18 -07001320 public Builder(int icon, CharSequence title, PendingIntent intent) {
Adrian Roos7af53622016-10-12 13:44:05 -07001321 this(Icon.createWithResource("", icon), title, intent);
Dan Sandler86647982015-05-13 23:41:13 -04001322 }
1323
1324 /**
1325 * Construct a new builder for {@link Action} object.
1326 * @param icon icon to show for this action
1327 * @param title the title of the action
1328 * @param intent the {@link PendingIntent} to fire when users trigger this action
1329 */
1330 public Builder(Icon icon, CharSequence title, PendingIntent intent) {
Alex Hills1f27f882017-01-12 11:24:46 -05001331 this(icon, title, intent, new Bundle(), null, true);
Griff Hazen959591e2014-05-15 22:26:18 -07001332 }
1333
1334 /**
1335 * Construct a new builder for {@link Action} object using the fields from an
1336 * {@link Action}.
1337 * @param action the action to read fields from.
1338 */
1339 public Builder(Action action) {
Adrian Roos7af53622016-10-12 13:44:05 -07001340 this(action.getIcon(), action.title, action.actionIntent,
1341 new Bundle(action.mExtras), action.getRemoteInputs(),
1342 action.getAllowGeneratedReplies());
Griff Hazen959591e2014-05-15 22:26:18 -07001343 }
1344
Dan Sandler86647982015-05-13 23:41:13 -04001345 private Builder(Icon icon, CharSequence title, PendingIntent intent, Bundle extras,
Adrian Roos7af53622016-10-12 13:44:05 -07001346 RemoteInput[] remoteInputs, boolean allowGeneratedReplies) {
Griff Hazen959591e2014-05-15 22:26:18 -07001347 mIcon = icon;
1348 mTitle = title;
1349 mIntent = intent;
1350 mExtras = extras;
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001351 if (remoteInputs != null) {
1352 mRemoteInputs = new ArrayList<RemoteInput>(remoteInputs.length);
1353 Collections.addAll(mRemoteInputs, remoteInputs);
1354 }
Adrian Roos7af53622016-10-12 13:44:05 -07001355 mAllowGeneratedReplies = allowGeneratedReplies;
Griff Hazen959591e2014-05-15 22:26:18 -07001356 }
1357
1358 /**
1359 * Merge additional metadata into this builder.
1360 *
1361 * <p>Values within the Bundle will replace existing extras values in this Builder.
1362 *
1363 * @see Notification.Action#extras
1364 */
1365 public Builder addExtras(Bundle extras) {
1366 if (extras != null) {
1367 mExtras.putAll(extras);
1368 }
1369 return this;
1370 }
1371
1372 /**
1373 * Get the metadata Bundle used by this Builder.
1374 *
1375 * <p>The returned Bundle is shared with this Builder.
1376 */
1377 public Bundle getExtras() {
1378 return mExtras;
1379 }
1380
1381 /**
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001382 * Add an input to be collected from the user when this action is sent.
1383 * Response values can be retrieved from the fired intent by using the
1384 * {@link RemoteInput#getResultsFromIntent} function.
1385 * @param remoteInput a {@link RemoteInput} to add to the action
1386 * @return this object for method chaining
1387 */
1388 public Builder addRemoteInput(RemoteInput remoteInput) {
1389 if (mRemoteInputs == null) {
1390 mRemoteInputs = new ArrayList<RemoteInput>();
1391 }
1392 mRemoteInputs.add(remoteInput);
1393 return this;
1394 }
1395
1396 /**
Alex Hills42b0c4d2016-04-26 13:35:36 -04001397 * Set whether the platform should automatically generate possible replies to add to
1398 * {@link RemoteInput#getChoices()}. If the {@link Action} doesn't have a
1399 * {@link RemoteInput}, this has no effect.
1400 * @param allowGeneratedReplies {@code true} to allow generated replies, {@code false}
1401 * otherwise
1402 * @return this object for method chaining
Alex Hills1f27f882017-01-12 11:24:46 -05001403 * The default value is {@code true}
Alex Hills42b0c4d2016-04-26 13:35:36 -04001404 */
1405 public Builder setAllowGeneratedReplies(boolean allowGeneratedReplies) {
1406 mAllowGeneratedReplies = allowGeneratedReplies;
1407 return this;
1408 }
1409
1410 /**
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001411 * Apply an extender to this action builder. Extenders may be used to add
1412 * metadata or change options on this builder.
1413 */
Griff Hazen61a9e862014-05-22 16:05:19 -07001414 public Builder extend(Extender extender) {
1415 extender.extend(this);
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001416 return this;
1417 }
1418
1419 /**
Griff Hazen959591e2014-05-15 22:26:18 -07001420 * Combine all of the options that have been set and return a new {@link Action}
1421 * object.
1422 * @return the built action
1423 */
1424 public Action build() {
Shane Brennan472a3b32016-12-12 15:28:10 -08001425 ArrayList<RemoteInput> dataOnlyInputs = new ArrayList<>();
1426 RemoteInput[] previousDataInputs =
1427 (RemoteInput[]) mExtras.getParcelableArray(EXTRA_DATA_ONLY_INPUTS);
Felipe Lemedfd11962017-01-18 18:38:46 -08001428 if (previousDataInputs != null) {
Shane Brennan472a3b32016-12-12 15:28:10 -08001429 for (RemoteInput input : previousDataInputs) {
1430 dataOnlyInputs.add(input);
1431 }
1432 }
1433 List<RemoteInput> textInputs = new ArrayList<>();
1434 if (mRemoteInputs != null) {
1435 for (RemoteInput input : mRemoteInputs) {
1436 if (input.isDataOnly()) {
1437 dataOnlyInputs.add(input);
1438 } else {
1439 textInputs.add(input);
1440 }
1441 }
1442 }
1443 if (!dataOnlyInputs.isEmpty()) {
1444 RemoteInput[] dataInputsArr =
1445 dataOnlyInputs.toArray(new RemoteInput[dataOnlyInputs.size()]);
1446 mExtras.putParcelableArray(EXTRA_DATA_ONLY_INPUTS, dataInputsArr);
1447 }
1448 RemoteInput[] textInputsArr = textInputs.isEmpty()
1449 ? null : textInputs.toArray(new RemoteInput[textInputs.size()]);
1450 return new Action(mIcon, mTitle, mIntent, mExtras, textInputsArr,
Alex Hills42b0c4d2016-04-26 13:35:36 -04001451 mAllowGeneratedReplies);
Griff Hazen959591e2014-05-15 22:26:18 -07001452 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001453 }
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001454
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001455 @Override
1456 public Action clone() {
1457 return new Action(
Dan Sandler86647982015-05-13 23:41:13 -04001458 getIcon(),
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001459 title,
1460 actionIntent, // safe to alias
Julia Reynolds53c934c2016-09-16 14:03:43 -04001461 mExtras == null ? new Bundle() : new Bundle(mExtras),
Alex Hills42b0c4d2016-04-26 13:35:36 -04001462 getRemoteInputs(),
1463 getAllowGeneratedReplies());
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001464 }
1465 @Override
1466 public int describeContents() {
1467 return 0;
1468 }
1469 @Override
1470 public void writeToParcel(Parcel out, int flags) {
Dan Sandler86647982015-05-13 23:41:13 -04001471 final Icon ic = getIcon();
1472 if (ic != null) {
1473 out.writeInt(1);
1474 ic.writeToParcel(out, 0);
1475 } else {
1476 out.writeInt(0);
1477 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001478 TextUtils.writeToParcel(title, out, flags);
1479 if (actionIntent != null) {
1480 out.writeInt(1);
1481 actionIntent.writeToParcel(out, flags);
1482 } else {
1483 out.writeInt(0);
1484 }
Griff Hazen959591e2014-05-15 22:26:18 -07001485 out.writeBundle(mExtras);
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001486 out.writeTypedArray(mRemoteInputs, flags);
Alex Hills42b0c4d2016-04-26 13:35:36 -04001487 out.writeInt(mAllowGeneratedReplies ? 1 : 0);
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001488 }
Griff Hazen959591e2014-05-15 22:26:18 -07001489 public static final Parcelable.Creator<Action> CREATOR =
1490 new Parcelable.Creator<Action>() {
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001491 public Action createFromParcel(Parcel in) {
1492 return new Action(in);
1493 }
1494 public Action[] newArray(int size) {
1495 return new Action[size];
1496 }
1497 };
Griff Hazen61a9e862014-05-22 16:05:19 -07001498
1499 /**
1500 * Extender interface for use with {@link Builder#extend}. Extenders may be used to add
1501 * metadata or change options on an action builder.
1502 */
1503 public interface Extender {
1504 /**
1505 * Apply this extender to a notification action builder.
1506 * @param builder the builder to be modified.
1507 * @return the build object for chaining.
1508 */
1509 public Builder extend(Builder builder);
1510 }
1511
1512 /**
1513 * Wearable extender for notification actions. To add extensions to an action,
1514 * create a new {@link android.app.Notification.Action.WearableExtender} object using
1515 * the {@code WearableExtender()} constructor and apply it to a
1516 * {@link android.app.Notification.Action.Builder} using
1517 * {@link android.app.Notification.Action.Builder#extend}.
1518 *
1519 * <pre class="prettyprint">
1520 * Notification.Action action = new Notification.Action.Builder(
1521 * R.drawable.archive_all, "Archive all", actionIntent)
Griff Hazen14f57992014-05-26 09:07:14 -07001522 * .extend(new Notification.Action.WearableExtender()
Griff Hazen61a9e862014-05-22 16:05:19 -07001523 * .setAvailableOffline(false))
Griff Hazen14f57992014-05-26 09:07:14 -07001524 * .build();</pre>
Griff Hazen61a9e862014-05-22 16:05:19 -07001525 */
1526 public static final class WearableExtender implements Extender {
1527 /** Notification action extra which contains wearable extensions */
1528 private static final String EXTRA_WEARABLE_EXTENSIONS = "android.wearable.EXTENSIONS";
1529
Pete Gastaf6781d2014-10-07 15:17:05 -04001530 // Keys within EXTRA_WEARABLE_EXTENSIONS for wearable options.
Griff Hazen61a9e862014-05-22 16:05:19 -07001531 private static final String KEY_FLAGS = "flags";
Pete Gastaf6781d2014-10-07 15:17:05 -04001532 private static final String KEY_IN_PROGRESS_LABEL = "inProgressLabel";
1533 private static final String KEY_CONFIRM_LABEL = "confirmLabel";
1534 private static final String KEY_CANCEL_LABEL = "cancelLabel";
Griff Hazen61a9e862014-05-22 16:05:19 -07001535
1536 // Flags bitwise-ored to mFlags
1537 private static final int FLAG_AVAILABLE_OFFLINE = 0x1;
Alex Hills9ab3a232016-04-05 14:54:56 -04001538 private static final int FLAG_HINT_LAUNCHES_ACTIVITY = 1 << 1;
Alex Hills9f087612016-06-07 09:08:59 -04001539 private static final int FLAG_HINT_DISPLAY_INLINE = 1 << 2;
Griff Hazen61a9e862014-05-22 16:05:19 -07001540
1541 // Default value for flags integer
1542 private static final int DEFAULT_FLAGS = FLAG_AVAILABLE_OFFLINE;
1543
1544 private int mFlags = DEFAULT_FLAGS;
1545
Pete Gastaf6781d2014-10-07 15:17:05 -04001546 private CharSequence mInProgressLabel;
1547 private CharSequence mConfirmLabel;
1548 private CharSequence mCancelLabel;
1549
Griff Hazen61a9e862014-05-22 16:05:19 -07001550 /**
1551 * Create a {@link android.app.Notification.Action.WearableExtender} with default
1552 * options.
1553 */
1554 public WearableExtender() {
1555 }
1556
1557 /**
1558 * Create a {@link android.app.Notification.Action.WearableExtender} by reading
1559 * wearable options present in an existing notification action.
1560 * @param action the notification action to inspect.
1561 */
1562 public WearableExtender(Action action) {
1563 Bundle wearableBundle = action.getExtras().getBundle(EXTRA_WEARABLE_EXTENSIONS);
1564 if (wearableBundle != null) {
1565 mFlags = wearableBundle.getInt(KEY_FLAGS, DEFAULT_FLAGS);
Pete Gastaf6781d2014-10-07 15:17:05 -04001566 mInProgressLabel = wearableBundle.getCharSequence(KEY_IN_PROGRESS_LABEL);
1567 mConfirmLabel = wearableBundle.getCharSequence(KEY_CONFIRM_LABEL);
1568 mCancelLabel = wearableBundle.getCharSequence(KEY_CANCEL_LABEL);
Griff Hazen61a9e862014-05-22 16:05:19 -07001569 }
1570 }
1571
1572 /**
1573 * Apply wearable extensions to a notification action that is being built. This is
1574 * typically called by the {@link android.app.Notification.Action.Builder#extend}
1575 * method of {@link android.app.Notification.Action.Builder}.
1576 */
1577 @Override
1578 public Action.Builder extend(Action.Builder builder) {
1579 Bundle wearableBundle = new Bundle();
1580
1581 if (mFlags != DEFAULT_FLAGS) {
1582 wearableBundle.putInt(KEY_FLAGS, mFlags);
1583 }
Pete Gastaf6781d2014-10-07 15:17:05 -04001584 if (mInProgressLabel != null) {
1585 wearableBundle.putCharSequence(KEY_IN_PROGRESS_LABEL, mInProgressLabel);
1586 }
1587 if (mConfirmLabel != null) {
1588 wearableBundle.putCharSequence(KEY_CONFIRM_LABEL, mConfirmLabel);
1589 }
1590 if (mCancelLabel != null) {
1591 wearableBundle.putCharSequence(KEY_CANCEL_LABEL, mCancelLabel);
1592 }
Griff Hazen61a9e862014-05-22 16:05:19 -07001593
1594 builder.getExtras().putBundle(EXTRA_WEARABLE_EXTENSIONS, wearableBundle);
1595 return builder;
1596 }
1597
1598 @Override
1599 public WearableExtender clone() {
1600 WearableExtender that = new WearableExtender();
1601 that.mFlags = this.mFlags;
Pete Gastaf6781d2014-10-07 15:17:05 -04001602 that.mInProgressLabel = this.mInProgressLabel;
1603 that.mConfirmLabel = this.mConfirmLabel;
1604 that.mCancelLabel = this.mCancelLabel;
Griff Hazen61a9e862014-05-22 16:05:19 -07001605 return that;
1606 }
1607
1608 /**
1609 * Set whether this action is available when the wearable device is not connected to
1610 * a companion device. The user can still trigger this action when the wearable device is
1611 * offline, but a visual hint will indicate that the action may not be available.
1612 * Defaults to true.
1613 */
1614 public WearableExtender setAvailableOffline(boolean availableOffline) {
1615 setFlag(FLAG_AVAILABLE_OFFLINE, availableOffline);
1616 return this;
1617 }
1618
1619 /**
1620 * Get whether this action is available when the wearable device is not connected to
1621 * a companion device. The user can still trigger this action when the wearable device is
1622 * offline, but a visual hint will indicate that the action may not be available.
1623 * Defaults to true.
1624 */
1625 public boolean isAvailableOffline() {
1626 return (mFlags & FLAG_AVAILABLE_OFFLINE) != 0;
1627 }
1628
1629 private void setFlag(int mask, boolean value) {
1630 if (value) {
1631 mFlags |= mask;
1632 } else {
1633 mFlags &= ~mask;
1634 }
1635 }
Pete Gastaf6781d2014-10-07 15:17:05 -04001636
1637 /**
1638 * Set a label to display while the wearable is preparing to automatically execute the
1639 * action. This is usually a 'ing' verb ending in ellipsis like "Sending..."
1640 *
1641 * @param label the label to display while the action is being prepared to execute
1642 * @return this object for method chaining
1643 */
1644 public WearableExtender setInProgressLabel(CharSequence label) {
1645 mInProgressLabel = label;
1646 return this;
1647 }
1648
1649 /**
1650 * Get the label to display while the wearable is preparing to automatically execute
1651 * the action. This is usually a 'ing' verb ending in ellipsis like "Sending..."
1652 *
1653 * @return the label to display while the action is being prepared to execute
1654 */
1655 public CharSequence getInProgressLabel() {
1656 return mInProgressLabel;
1657 }
1658
1659 /**
1660 * Set a label to display to confirm that the action should be executed.
1661 * This is usually an imperative verb like "Send".
1662 *
1663 * @param label the label to confirm the action should be executed
1664 * @return this object for method chaining
1665 */
1666 public WearableExtender setConfirmLabel(CharSequence label) {
1667 mConfirmLabel = label;
1668 return this;
1669 }
1670
1671 /**
1672 * Get the label to display to confirm that the action should be executed.
1673 * This is usually an imperative verb like "Send".
1674 *
1675 * @return the label to confirm the action should be executed
1676 */
1677 public CharSequence getConfirmLabel() {
1678 return mConfirmLabel;
1679 }
1680
1681 /**
1682 * Set a label to display to cancel the action.
1683 * This is usually an imperative verb, like "Cancel".
1684 *
1685 * @param label the label to display to cancel the action
1686 * @return this object for method chaining
1687 */
1688 public WearableExtender setCancelLabel(CharSequence label) {
1689 mCancelLabel = label;
1690 return this;
1691 }
1692
1693 /**
1694 * Get the label to display to cancel the action.
1695 * This is usually an imperative verb like "Cancel".
1696 *
1697 * @return the label to display to cancel the action
1698 */
1699 public CharSequence getCancelLabel() {
1700 return mCancelLabel;
1701 }
Alex Hills9ab3a232016-04-05 14:54:56 -04001702
1703 /**
1704 * Set a hint that this Action will launch an {@link Activity} directly, telling the
1705 * platform that it can generate the appropriate transitions.
1706 * @param hintLaunchesActivity {@code true} if the content intent will launch
1707 * an activity and transitions should be generated, false otherwise.
1708 * @return this object for method chaining
1709 */
Alex Hills4ec3ff42016-04-12 11:36:18 -04001710 public WearableExtender setHintLaunchesActivity(
Alex Hills9ab3a232016-04-05 14:54:56 -04001711 boolean hintLaunchesActivity) {
1712 setFlag(FLAG_HINT_LAUNCHES_ACTIVITY, hintLaunchesActivity);
1713 return this;
1714 }
1715
1716 /**
1717 * Get a hint that this Action will launch an {@link Activity} directly, telling the
1718 * platform that it can generate the appropriate transitions
1719 * @return {@code true} if the content intent will launch an activity and transitions
1720 * should be generated, false otherwise. The default value is {@code false} if this was
1721 * never set.
1722 */
Alex Hills4ec3ff42016-04-12 11:36:18 -04001723 public boolean getHintLaunchesActivity() {
Alex Hills9ab3a232016-04-05 14:54:56 -04001724 return (mFlags & FLAG_HINT_LAUNCHES_ACTIVITY) != 0;
1725 }
Alex Hills9f087612016-06-07 09:08:59 -04001726
1727 /**
1728 * Set a hint that this Action should be displayed inline.
1729 *
1730 * @param hintDisplayInline {@code true} if action should be displayed inline, false
1731 * otherwise
1732 * @return this object for method chaining
1733 */
1734 public WearableExtender setHintDisplayActionInline(
1735 boolean hintDisplayInline) {
1736 setFlag(FLAG_HINT_DISPLAY_INLINE, hintDisplayInline);
1737 return this;
1738 }
1739
1740 /**
1741 * Get a hint that this Action should be displayed inline.
1742 *
Dmitri Plotnikov9e9cfd92016-12-21 10:52:26 -08001743 * @return {@code true} if the Action should be displayed inline, {@code false}
Alex Hills9f087612016-06-07 09:08:59 -04001744 * otherwise. The default value is {@code false} if this was never set.
1745 */
1746 public boolean getHintDisplayActionInline() {
1747 return (mFlags & FLAG_HINT_DISPLAY_INLINE) != 0;
1748 }
Griff Hazen61a9e862014-05-22 16:05:19 -07001749 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001750 }
1751
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001752 /**
1753 * Array of all {@link Action} structures attached to this notification by
1754 * {@link Builder#addAction(int, CharSequence, PendingIntent)}. Mostly useful for instances of
1755 * {@link android.service.notification.NotificationListenerService} that provide an alternative
1756 * interface for invoking actions.
1757 */
Daniel Sandlerea2a3172013-02-20 22:24:20 -05001758 public Action[] actions;
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001759
1760 /**
Dan Sandler0bf2ed82013-12-21 23:33:41 -06001761 * Replacement version of this notification whose content will be shown
1762 * in an insecure context such as atop a secure keyguard. See {@link #visibility}
1763 * and {@link #VISIBILITY_PUBLIC}.
1764 */
1765 public Notification publicVersion;
1766
1767 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001768 * Constructs a Notification object with default values.
Joe Onorato46439ce2010-11-19 13:56:21 -08001769 * You might want to consider using {@link Builder} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001770 */
1771 public Notification()
1772 {
1773 this.when = System.currentTimeMillis();
Selim Cinekb85f36fd2016-04-20 18:46:36 -07001774 this.creationTime = System.currentTimeMillis();
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001775 this.priority = PRIORITY_DEFAULT;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001776 }
1777
1778 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001779 * @hide
1780 */
1781 public Notification(Context context, int icon, CharSequence tickerText, long when,
1782 CharSequence contentTitle, CharSequence contentText, Intent contentIntent)
1783 {
Chris Wren1ce4b6d2015-06-11 10:19:43 -04001784 new Builder(context)
1785 .setWhen(when)
1786 .setSmallIcon(icon)
1787 .setTicker(tickerText)
1788 .setContentTitle(contentTitle)
1789 .setContentText(contentText)
1790 .setContentIntent(PendingIntent.getActivity(context, 0, contentIntent, 0))
1791 .buildInto(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001792 }
1793
1794 /**
1795 * Constructs a Notification object with the information needed to
1796 * have a status bar icon without the standard expanded view.
1797 *
1798 * @param icon The resource id of the icon to put in the status bar.
1799 * @param tickerText The text that flows by in the status bar when the notification first
1800 * activates.
1801 * @param when The time to show in the time field. In the System.currentTimeMillis
1802 * timebase.
Joe Onorato46439ce2010-11-19 13:56:21 -08001803 *
1804 * @deprecated Use {@link Builder} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001805 */
Joe Onorato46439ce2010-11-19 13:56:21 -08001806 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001807 public Notification(int icon, CharSequence tickerText, long when)
1808 {
1809 this.icon = icon;
1810 this.tickerText = tickerText;
1811 this.when = when;
Selim Cinekb85f36fd2016-04-20 18:46:36 -07001812 this.creationTime = System.currentTimeMillis();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001813 }
1814
1815 /**
1816 * Unflatten the notification from a parcel.
1817 */
Svet Ganovddb94882016-06-23 19:55:24 -07001818 @SuppressWarnings("unchecked")
1819 public Notification(Parcel parcel) {
1820 // IMPORTANT: Add unmarshaling code in readFromParcel as the pending
1821 // intents in extras are always written as the last entry.
1822 readFromParcelImpl(parcel);
1823 // Must be read last!
Felipe Lemedd85da62016-06-28 11:29:54 -07001824 allPendingIntents = (ArraySet<PendingIntent>) parcel.readArraySet(null);
Svet Ganovddb94882016-06-23 19:55:24 -07001825 }
1826
1827 private void readFromParcelImpl(Parcel parcel)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001828 {
1829 int version = parcel.readInt();
1830
1831 when = parcel.readLong();
Selim Cinekb85f36fd2016-04-20 18:46:36 -07001832 creationTime = parcel.readLong();
Dan Sandler3936e7a2015-05-19 20:59:12 -04001833 if (parcel.readInt() != 0) {
1834 mSmallIcon = Icon.CREATOR.createFromParcel(parcel);
Dan Sandler4e787062015-06-17 15:09:48 -04001835 if (mSmallIcon.getType() == Icon.TYPE_RESOURCE) {
1836 icon = mSmallIcon.getResId();
1837 }
Dan Sandler3936e7a2015-05-19 20:59:12 -04001838 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001839 number = parcel.readInt();
1840 if (parcel.readInt() != 0) {
1841 contentIntent = PendingIntent.CREATOR.createFromParcel(parcel);
1842 }
1843 if (parcel.readInt() != 0) {
1844 deleteIntent = PendingIntent.CREATOR.createFromParcel(parcel);
1845 }
1846 if (parcel.readInt() != 0) {
1847 tickerText = TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(parcel);
1848 }
1849 if (parcel.readInt() != 0) {
Joe Onorato46439ce2010-11-19 13:56:21 -08001850 tickerView = RemoteViews.CREATOR.createFromParcel(parcel);
Joe Onoratoef1e7762010-09-17 18:38:38 -04001851 }
1852 if (parcel.readInt() != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001853 contentView = RemoteViews.CREATOR.createFromParcel(parcel);
1854 }
Joe Onorato561d3852010-11-20 18:09:34 -08001855 if (parcel.readInt() != 0) {
Dan Sandlerd63f9322015-05-06 15:18:49 -04001856 mLargeIcon = Icon.CREATOR.createFromParcel(parcel);
Joe Onorato561d3852010-11-20 18:09:34 -08001857 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001858 defaults = parcel.readInt();
1859 flags = parcel.readInt();
1860 if (parcel.readInt() != 0) {
1861 sound = Uri.CREATOR.createFromParcel(parcel);
1862 }
1863
1864 audioStreamType = parcel.readInt();
John Spurlockc0650f022014-07-19 13:22:39 -04001865 if (parcel.readInt() != 0) {
1866 audioAttributes = AudioAttributes.CREATOR.createFromParcel(parcel);
1867 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001868 vibrate = parcel.createLongArray();
1869 ledARGB = parcel.readInt();
1870 ledOnMS = parcel.readInt();
1871 ledOffMS = parcel.readInt();
1872 iconLevel = parcel.readInt();
Daniel Sandlere46cbd32010-06-17 10:35:26 -04001873
1874 if (parcel.readInt() != 0) {
1875 fullScreenIntent = PendingIntent.CREATOR.createFromParcel(parcel);
1876 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001877
1878 priority = parcel.readInt();
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001879
John Spurlockfd7f1e02014-03-18 16:41:57 -04001880 category = parcel.readString();
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001881
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001882 mGroupKey = parcel.readString();
1883
1884 mSortKey = parcel.readString();
1885
Jeff Sharkeya04c7a72016-03-18 12:20:36 -06001886 extras = Bundle.setDefusable(parcel.readBundle(), true); // may be null
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001887
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001888 actions = parcel.createTypedArray(Action.CREATOR); // may be null
1889
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001890 if (parcel.readInt() != 0) {
Daniel Sandlerf3b73432012-03-27 15:01:25 -04001891 bigContentView = RemoteViews.CREATOR.createFromParcel(parcel);
1892 }
Dan Sandler0bf2ed82013-12-21 23:33:41 -06001893
Chris Wren8fd39ec2014-02-27 17:43:26 -05001894 if (parcel.readInt() != 0) {
1895 headsUpContentView = RemoteViews.CREATOR.createFromParcel(parcel);
1896 }
1897
Dan Sandler0bf2ed82013-12-21 23:33:41 -06001898 visibility = parcel.readInt();
1899
1900 if (parcel.readInt() != 0) {
1901 publicVersion = Notification.CREATOR.createFromParcel(parcel);
1902 }
Dan Sandler26e81cf2014-05-06 10:01:27 -04001903
1904 color = parcel.readInt();
Julia Reynoldsb5e44b72016-08-16 15:00:25 -04001905
1906 if (parcel.readInt() != 0) {
1907 mChannelId = parcel.readString();
1908 }
Julia Reynolds2a128742016-11-28 14:29:25 -05001909 mTimeout = parcel.readLong();
Julia Reynolds13d898c2017-02-02 12:22:05 -05001910
1911 if (parcel.readInt() != 0) {
1912 mShortcutId = parcel.readString();
1913 }
1914
1915 mBadgeIcon = parcel.readInt();
Julia Reynolds3aedded2017-03-31 14:42:09 -04001916
1917 if (parcel.readInt() != 0) {
1918 mSettingsText = TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(parcel);
1919 }
Julia Reynoldsa79c3712017-04-21 10:29:57 -04001920
1921 mGroupAlertBehavior = parcel.readInt();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001922 }
1923
Andy Stadler110988c2010-12-03 14:29:16 -08001924 @Override
Joe Onorato18e69df2010-05-17 22:26:12 -07001925 public Notification clone() {
1926 Notification that = new Notification();
Daniel Sandler1a497d32013-04-18 14:52:45 -04001927 cloneInto(that, true);
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001928 return that;
1929 }
Joe Onorato18e69df2010-05-17 22:26:12 -07001930
Daniel Sandler1a497d32013-04-18 14:52:45 -04001931 /**
1932 * Copy all (or if heavy is false, all except Bitmaps and RemoteViews) members
1933 * of this into that.
1934 * @hide
1935 */
1936 public void cloneInto(Notification that, boolean heavy) {
Joe Onorato18e69df2010-05-17 22:26:12 -07001937 that.when = this.when;
Selim Cinekb85f36fd2016-04-20 18:46:36 -07001938 that.creationTime = this.creationTime;
Dan Sandlerd63f9322015-05-06 15:18:49 -04001939 that.mSmallIcon = this.mSmallIcon;
Joe Onorato18e69df2010-05-17 22:26:12 -07001940 that.number = this.number;
1941
1942 // PendingIntents are global, so there's no reason (or way) to clone them.
1943 that.contentIntent = this.contentIntent;
1944 that.deleteIntent = this.deleteIntent;
Daniel Sandlere46cbd32010-06-17 10:35:26 -04001945 that.fullScreenIntent = this.fullScreenIntent;
Joe Onorato18e69df2010-05-17 22:26:12 -07001946
1947 if (this.tickerText != null) {
1948 that.tickerText = this.tickerText.toString();
1949 }
Daniel Sandler1a497d32013-04-18 14:52:45 -04001950 if (heavy && this.tickerView != null) {
Joe Onorato46439ce2010-11-19 13:56:21 -08001951 that.tickerView = this.tickerView.clone();
Joe Onoratoef1e7762010-09-17 18:38:38 -04001952 }
Daniel Sandler1a497d32013-04-18 14:52:45 -04001953 if (heavy && this.contentView != null) {
Joe Onorato18e69df2010-05-17 22:26:12 -07001954 that.contentView = this.contentView.clone();
1955 }
Dan Sandlerd63f9322015-05-06 15:18:49 -04001956 if (heavy && this.mLargeIcon != null) {
1957 that.mLargeIcon = this.mLargeIcon;
Joe Onorato561d3852010-11-20 18:09:34 -08001958 }
Jozef BABJAKa8b91832011-02-22 08:05:08 +01001959 that.iconLevel = this.iconLevel;
Joe Onorato18e69df2010-05-17 22:26:12 -07001960 that.sound = this.sound; // android.net.Uri is immutable
1961 that.audioStreamType = this.audioStreamType;
John Spurlockc0650f022014-07-19 13:22:39 -04001962 if (this.audioAttributes != null) {
1963 that.audioAttributes = new AudioAttributes.Builder(this.audioAttributes).build();
1964 }
Joe Onorato18e69df2010-05-17 22:26:12 -07001965
1966 final long[] vibrate = this.vibrate;
1967 if (vibrate != null) {
1968 final int N = vibrate.length;
1969 final long[] vib = that.vibrate = new long[N];
1970 System.arraycopy(vibrate, 0, vib, 0, N);
1971 }
1972
1973 that.ledARGB = this.ledARGB;
1974 that.ledOnMS = this.ledOnMS;
1975 that.ledOffMS = this.ledOffMS;
1976 that.defaults = this.defaults;
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001977
Joe Onorato18e69df2010-05-17 22:26:12 -07001978 that.flags = this.flags;
1979
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001980 that.priority = this.priority;
Joe Malin8d40d042012-11-05 11:36:40 -08001981
John Spurlockfd7f1e02014-03-18 16:41:57 -04001982 that.category = this.category;
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001983
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001984 that.mGroupKey = this.mGroupKey;
1985
1986 that.mSortKey = this.mSortKey;
1987
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001988 if (this.extras != null) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04001989 try {
1990 that.extras = new Bundle(this.extras);
1991 // will unparcel
1992 that.extras.size();
1993 } catch (BadParcelableException e) {
1994 Log.e(TAG, "could not unparcel extras from notification: " + this, e);
1995 that.extras = null;
1996 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001997 }
1998
Felipe Lemedd85da62016-06-28 11:29:54 -07001999 if (!ArrayUtils.isEmpty(allPendingIntents)) {
2000 that.allPendingIntents = new ArraySet<>(allPendingIntents);
Svet Ganovddb94882016-06-23 19:55:24 -07002001 }
2002
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002003 if (this.actions != null) {
2004 that.actions = new Action[this.actions.length];
2005 for(int i=0; i<this.actions.length; i++) {
liangweikang63b03b52017-03-16 19:22:15 +08002006 if ( this.actions[i] != null) {
2007 that.actions[i] = this.actions[i].clone();
2008 }
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002009 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002010 }
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002011
Daniel Sandler1a497d32013-04-18 14:52:45 -04002012 if (heavy && this.bigContentView != null) {
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002013 that.bigContentView = this.bigContentView.clone();
2014 }
Daniel Sandler1a497d32013-04-18 14:52:45 -04002015
Chris Wren8fd39ec2014-02-27 17:43:26 -05002016 if (heavy && this.headsUpContentView != null) {
2017 that.headsUpContentView = this.headsUpContentView.clone();
2018 }
2019
Dan Sandler0bf2ed82013-12-21 23:33:41 -06002020 that.visibility = this.visibility;
2021
2022 if (this.publicVersion != null) {
2023 that.publicVersion = new Notification();
2024 this.publicVersion.cloneInto(that.publicVersion, heavy);
2025 }
2026
Dan Sandler26e81cf2014-05-06 10:01:27 -04002027 that.color = this.color;
2028
Julia Reynoldsb5e44b72016-08-16 15:00:25 -04002029 that.mChannelId = this.mChannelId;
Julia Reynolds2a128742016-11-28 14:29:25 -05002030 that.mTimeout = this.mTimeout;
Julia Reynolds3aedded2017-03-31 14:42:09 -04002031 that.mShortcutId = this.mShortcutId;
2032 that.mBadgeIcon = this.mBadgeIcon;
2033 that.mSettingsText = this.mSettingsText;
Julia Reynoldsa79c3712017-04-21 10:29:57 -04002034 that.mGroupAlertBehavior = this.mGroupAlertBehavior;
Julia Reynoldsb5e44b72016-08-16 15:00:25 -04002035
Daniel Sandler1a497d32013-04-18 14:52:45 -04002036 if (!heavy) {
2037 that.lightenPayload(); // will clean out extras
2038 }
2039 }
2040
2041 /**
2042 * Removes heavyweight parts of the Notification object for archival or for sending to
2043 * listeners when the full contents are not necessary.
2044 * @hide
2045 */
2046 public final void lightenPayload() {
2047 tickerView = null;
2048 contentView = null;
2049 bigContentView = null;
Chris Wren8fd39ec2014-02-27 17:43:26 -05002050 headsUpContentView = null;
Dan Sandlerd63f9322015-05-06 15:18:49 -04002051 mLargeIcon = null;
Dan Sandler50128532015-12-08 15:42:41 -05002052 if (extras != null && !extras.isEmpty()) {
2053 final Set<String> keyset = extras.keySet();
2054 final int N = keyset.size();
2055 final String[] keys = keyset.toArray(new String[N]);
2056 for (int i=0; i<N; i++) {
2057 final String key = keys[i];
Dmitri Plotnikov22281362017-01-30 11:16:21 -08002058 if (TvExtender.EXTRA_TV_EXTENDER.equals(key)) {
2059 continue;
2060 }
Dan Sandler50128532015-12-08 15:42:41 -05002061 final Object obj = extras.get(key);
2062 if (obj != null &&
2063 ( obj instanceof Parcelable
2064 || obj instanceof Parcelable[]
2065 || obj instanceof SparseArray
2066 || obj instanceof ArrayList)) {
2067 extras.remove(key);
2068 }
2069 }
Daniel Sandler1a497d32013-04-18 14:52:45 -04002070 }
Joe Onorato18e69df2010-05-17 22:26:12 -07002071 }
2072
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04002073 /**
2074 * Make sure this CharSequence is safe to put into a bundle, which basically
2075 * means it had better not be some custom Parcelable implementation.
2076 * @hide
2077 */
2078 public static CharSequence safeCharSequence(CharSequence cs) {
Christoph Studer535ec612014-09-03 15:47:47 +02002079 if (cs == null) return cs;
2080 if (cs.length() > MAX_CHARSEQUENCE_LENGTH) {
2081 cs = cs.subSequence(0, MAX_CHARSEQUENCE_LENGTH);
2082 }
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04002083 if (cs instanceof Parcelable) {
2084 Log.e(TAG, "warning: " + cs.getClass().getCanonicalName()
2085 + " instance is a custom Parcelable and not allowed in Notification");
2086 return cs.toString();
2087 }
Selim Cinek60a54252016-02-26 17:03:25 -08002088 return removeTextSizeSpans(cs);
2089 }
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04002090
Selim Cinek60a54252016-02-26 17:03:25 -08002091 private static CharSequence removeTextSizeSpans(CharSequence charSequence) {
2092 if (charSequence instanceof Spanned) {
2093 Spanned ss = (Spanned) charSequence;
2094 Object[] spans = ss.getSpans(0, ss.length(), Object.class);
2095 SpannableStringBuilder builder = new SpannableStringBuilder(ss.toString());
2096 for (Object span : spans) {
2097 Object resultSpan = span;
Selim Cinek89991a22016-03-07 19:51:54 -08002098 if (resultSpan instanceof CharacterStyle) {
2099 resultSpan = ((CharacterStyle) span).getUnderlying();
2100 }
2101 if (resultSpan instanceof TextAppearanceSpan) {
2102 TextAppearanceSpan originalSpan = (TextAppearanceSpan) resultSpan;
Selim Cinek60a54252016-02-26 17:03:25 -08002103 resultSpan = new TextAppearanceSpan(
2104 originalSpan.getFamily(),
2105 originalSpan.getTextStyle(),
2106 -1,
2107 originalSpan.getTextColor(),
2108 originalSpan.getLinkTextColor());
Selim Cinek89991a22016-03-07 19:51:54 -08002109 } else if (resultSpan instanceof RelativeSizeSpan
2110 || resultSpan instanceof AbsoluteSizeSpan) {
Selim Cinek60a54252016-02-26 17:03:25 -08002111 continue;
Selim Cinek89991a22016-03-07 19:51:54 -08002112 } else {
2113 resultSpan = span;
Selim Cinek60a54252016-02-26 17:03:25 -08002114 }
2115 builder.setSpan(resultSpan, ss.getSpanStart(span), ss.getSpanEnd(span),
2116 ss.getSpanFlags(span));
2117 }
2118 return builder;
2119 }
2120 return charSequence;
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04002121 }
2122
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002123 public int describeContents() {
2124 return 0;
2125 }
2126
2127 /**
Dan Sandler4e787062015-06-17 15:09:48 -04002128 * Flatten this notification into a parcel.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002129 */
Svet Ganovddb94882016-06-23 19:55:24 -07002130 public void writeToParcel(Parcel parcel, int flags) {
2131 // We need to mark all pending intents getting into the notification
2132 // system as being put there to later allow the notification ranker
2133 // to launch them and by doing so add the app to the battery saver white
2134 // list for a short period of time. The problem is that the system
2135 // cannot look into the extras as there may be parcelables there that
2136 // the platform does not know how to handle. To go around that we have
2137 // an explicit list of the pending intents in the extras bundle.
Felipe Lemedd85da62016-06-28 11:29:54 -07002138 final boolean collectPendingIntents = (allPendingIntents == null);
Svet Ganovddb94882016-06-23 19:55:24 -07002139 if (collectPendingIntents) {
2140 PendingIntent.setOnMarshaledListener(
2141 (PendingIntent intent, Parcel out, int outFlags) -> {
2142 if (parcel == out) {
Felipe Lemedd85da62016-06-28 11:29:54 -07002143 if (allPendingIntents == null) {
2144 allPendingIntents = new ArraySet<>();
Svet Ganovddb94882016-06-23 19:55:24 -07002145 }
Felipe Lemedd85da62016-06-28 11:29:54 -07002146 allPendingIntents.add(intent);
Svet Ganovddb94882016-06-23 19:55:24 -07002147 }
2148 });
2149 }
2150 try {
2151 // IMPORTANT: Add marshaling code in writeToParcelImpl as we
Julia Reynolds13d898c2017-02-02 12:22:05 -05002152 // want to intercept all pending events written to the parcel.
Svet Ganovddb94882016-06-23 19:55:24 -07002153 writeToParcelImpl(parcel, flags);
2154 // Must be written last!
Felipe Lemedd85da62016-06-28 11:29:54 -07002155 parcel.writeArraySet(allPendingIntents);
Svet Ganovddb94882016-06-23 19:55:24 -07002156 } finally {
2157 if (collectPendingIntents) {
2158 PendingIntent.setOnMarshaledListener(null);
2159 }
2160 }
2161 }
2162
2163 private void writeToParcelImpl(Parcel parcel, int flags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002164 parcel.writeInt(1);
2165
2166 parcel.writeLong(when);
Selim Cinekb85f36fd2016-04-20 18:46:36 -07002167 parcel.writeLong(creationTime);
Dan Sandler4e787062015-06-17 15:09:48 -04002168 if (mSmallIcon == null && icon != 0) {
2169 // you snuck an icon in here without using the builder; let's try to keep it
2170 mSmallIcon = Icon.createWithResource("", icon);
2171 }
Dan Sandler3936e7a2015-05-19 20:59:12 -04002172 if (mSmallIcon != null) {
2173 parcel.writeInt(1);
2174 mSmallIcon.writeToParcel(parcel, 0);
2175 } else {
2176 parcel.writeInt(0);
2177 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002178 parcel.writeInt(number);
2179 if (contentIntent != null) {
2180 parcel.writeInt(1);
2181 contentIntent.writeToParcel(parcel, 0);
2182 } else {
2183 parcel.writeInt(0);
2184 }
2185 if (deleteIntent != null) {
2186 parcel.writeInt(1);
2187 deleteIntent.writeToParcel(parcel, 0);
2188 } else {
2189 parcel.writeInt(0);
2190 }
2191 if (tickerText != null) {
2192 parcel.writeInt(1);
2193 TextUtils.writeToParcel(tickerText, parcel, flags);
2194 } else {
2195 parcel.writeInt(0);
2196 }
Joe Onorato46439ce2010-11-19 13:56:21 -08002197 if (tickerView != null) {
Joe Onoratoef1e7762010-09-17 18:38:38 -04002198 parcel.writeInt(1);
Joe Onorato46439ce2010-11-19 13:56:21 -08002199 tickerView.writeToParcel(parcel, 0);
Joe Onoratoef1e7762010-09-17 18:38:38 -04002200 } else {
2201 parcel.writeInt(0);
2202 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002203 if (contentView != null) {
2204 parcel.writeInt(1);
2205 contentView.writeToParcel(parcel, 0);
2206 } else {
2207 parcel.writeInt(0);
2208 }
Selim Cinek279fa862016-06-14 10:57:25 -07002209 if (mLargeIcon == null && largeIcon != null) {
2210 // you snuck an icon in here without using the builder; let's try to keep it
2211 mLargeIcon = Icon.createWithBitmap(largeIcon);
2212 }
Dan Sandlerd63f9322015-05-06 15:18:49 -04002213 if (mLargeIcon != null) {
Joe Onorato561d3852010-11-20 18:09:34 -08002214 parcel.writeInt(1);
Dan Sandlerd63f9322015-05-06 15:18:49 -04002215 mLargeIcon.writeToParcel(parcel, 0);
Joe Onorato561d3852010-11-20 18:09:34 -08002216 } else {
2217 parcel.writeInt(0);
2218 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002219
2220 parcel.writeInt(defaults);
2221 parcel.writeInt(this.flags);
2222
2223 if (sound != null) {
2224 parcel.writeInt(1);
2225 sound.writeToParcel(parcel, 0);
2226 } else {
2227 parcel.writeInt(0);
2228 }
2229 parcel.writeInt(audioStreamType);
John Spurlockc0650f022014-07-19 13:22:39 -04002230
2231 if (audioAttributes != null) {
2232 parcel.writeInt(1);
2233 audioAttributes.writeToParcel(parcel, 0);
2234 } else {
2235 parcel.writeInt(0);
2236 }
2237
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002238 parcel.writeLongArray(vibrate);
2239 parcel.writeInt(ledARGB);
2240 parcel.writeInt(ledOnMS);
2241 parcel.writeInt(ledOffMS);
2242 parcel.writeInt(iconLevel);
Daniel Sandlere46cbd32010-06-17 10:35:26 -04002243
2244 if (fullScreenIntent != null) {
2245 parcel.writeInt(1);
2246 fullScreenIntent.writeToParcel(parcel, 0);
2247 } else {
2248 parcel.writeInt(0);
2249 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002250
2251 parcel.writeInt(priority);
Joe Malin8d40d042012-11-05 11:36:40 -08002252
John Spurlockfd7f1e02014-03-18 16:41:57 -04002253 parcel.writeString(category);
Joe Malin8d40d042012-11-05 11:36:40 -08002254
Griff Hazen5cadc3b2014-05-20 09:55:39 -07002255 parcel.writeString(mGroupKey);
2256
2257 parcel.writeString(mSortKey);
2258
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002259 parcel.writeBundle(extras); // null ok
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002260
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002261 parcel.writeTypedArray(actions, 0); // null ok
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002262
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002263 if (bigContentView != null) {
2264 parcel.writeInt(1);
2265 bigContentView.writeToParcel(parcel, 0);
2266 } else {
2267 parcel.writeInt(0);
2268 }
Dan Sandler0bf2ed82013-12-21 23:33:41 -06002269
Chris Wren8fd39ec2014-02-27 17:43:26 -05002270 if (headsUpContentView != null) {
2271 parcel.writeInt(1);
2272 headsUpContentView.writeToParcel(parcel, 0);
2273 } else {
2274 parcel.writeInt(0);
2275 }
2276
Dan Sandler0bf2ed82013-12-21 23:33:41 -06002277 parcel.writeInt(visibility);
2278
2279 if (publicVersion != null) {
2280 parcel.writeInt(1);
2281 publicVersion.writeToParcel(parcel, 0);
2282 } else {
2283 parcel.writeInt(0);
2284 }
Dan Sandler26e81cf2014-05-06 10:01:27 -04002285
2286 parcel.writeInt(color);
Julia Reynoldsb5e44b72016-08-16 15:00:25 -04002287
2288 if (mChannelId != null) {
2289 parcel.writeInt(1);
2290 parcel.writeString(mChannelId);
2291 } else {
2292 parcel.writeInt(0);
2293 }
Julia Reynolds2a128742016-11-28 14:29:25 -05002294 parcel.writeLong(mTimeout);
Julia Reynolds13d898c2017-02-02 12:22:05 -05002295
2296 if (mShortcutId != null) {
2297 parcel.writeInt(1);
2298 parcel.writeString(mShortcutId);
2299 } else {
2300 parcel.writeInt(0);
2301 }
2302
2303 parcel.writeInt(mBadgeIcon);
Julia Reynolds3aedded2017-03-31 14:42:09 -04002304
2305 if (mSettingsText != null) {
2306 parcel.writeInt(1);
2307 TextUtils.writeToParcel(mSettingsText, parcel, flags);
2308 } else {
2309 parcel.writeInt(0);
2310 }
Julia Reynoldsa79c3712017-04-21 10:29:57 -04002311
2312 parcel.writeInt(mGroupAlertBehavior);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002313 }
2314
2315 /**
2316 * Parcelable.Creator that instantiates Notification objects
2317 */
2318 public static final Parcelable.Creator<Notification> CREATOR
2319 = new Parcelable.Creator<Notification>()
2320 {
2321 public Notification createFromParcel(Parcel parcel)
2322 {
2323 return new Notification(parcel);
2324 }
2325
2326 public Notification[] newArray(int size)
2327 {
2328 return new Notification[size];
2329 }
2330 };
2331
2332 /**
2333 * Sets the {@link #contentView} field to be a view with the standard "Latest Event"
2334 * layout.
2335 *
2336 * <p>Uses the {@link #icon} and {@link #when} fields to set the icon and time fields
2337 * in the view.</p>
2338 * @param context The context for your application / activity.
2339 * @param contentTitle The title that goes in the expanded entry.
2340 * @param contentText The text that goes in the expanded entry.
2341 * @param contentIntent The intent to launch when the user clicks the expanded notification.
2342 * If this is an activity, it must include the
2343 * {@link android.content.Intent#FLAG_ACTIVITY_NEW_TASK} flag, which requires
Scott Main7aee61f2011-02-08 11:25:01 -08002344 * that you take care of task management as described in the
2345 * <a href="{@docRoot}guide/topics/fundamentals/tasks-and-back-stack.html">Tasks and Back
2346 * Stack</a> document.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002347 *
Joe Onorato46439ce2010-11-19 13:56:21 -08002348 * @deprecated Use {@link Builder} instead.
Chris Wrena05db382015-06-24 15:18:34 -04002349 * @removed
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002350 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002351 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002352 public void setLatestEventInfo(Context context,
2353 CharSequence contentTitle, CharSequence contentText, PendingIntent contentIntent) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002354 if (context.getApplicationInfo().targetSdkVersion > Build.VERSION_CODES.LOLLIPOP_MR1){
2355 Log.e(TAG, "setLatestEventInfo() is deprecated and you should feel deprecated.",
2356 new Throwable());
2357 }
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002358
Selim Cinek4ac6f602016-06-13 15:47:03 -07002359 if (context.getApplicationInfo().targetSdkVersion < Build.VERSION_CODES.N) {
2360 extras.putBoolean(EXTRA_SHOW_WHEN, true);
2361 }
2362
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002363 // ensure that any information already set directly is preserved
2364 final Notification.Builder builder = new Notification.Builder(context, this);
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002365
2366 // now apply the latestEventInfo fields
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002367 if (contentTitle != null) {
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002368 builder.setContentTitle(contentTitle);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002369 }
2370 if (contentText != null) {
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002371 builder.setContentText(contentText);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002372 }
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002373 builder.setContentIntent(contentIntent);
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002374
2375 builder.build(); // callers expect this notification to be ready to use
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002376 }
2377
Julia Reynoldsda303542015-11-23 14:00:20 -05002378 /**
2379 * @hide
2380 */
2381 public static void addFieldsFromContext(Context context, Notification notification) {
Julia Reynoldse071abd2017-03-22 10:52:11 -04002382 addFieldsFromContext(context.getApplicationInfo(), notification);
Jeff Sharkey012bc7b2016-04-11 16:30:27 -06002383 }
2384
2385 /**
2386 * @hide
2387 */
Julia Reynoldse071abd2017-03-22 10:52:11 -04002388 public static void addFieldsFromContext(ApplicationInfo ai, Notification notification) {
Jeff Sharkey012bc7b2016-04-11 16:30:27 -06002389 notification.extras.putParcelable(EXTRA_BUILDER_APPLICATION_INFO, ai);
Julia Reynoldsda303542015-11-23 14:00:20 -05002390 }
2391
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002392 @Override
2393 public String toString() {
2394 StringBuilder sb = new StringBuilder();
Julia Reynoldsb9e712e2017-04-17 10:31:03 -04002395 sb.append("Notification(channel=");
2396 sb.append(getChannel());
2397 sb.append(" pri=");
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002398 sb.append(priority);
2399 sb.append(" contentView=");
Joe Onoratoc9596d62011-01-12 17:03:11 -08002400 if (contentView != null) {
2401 sb.append(contentView.getPackage());
2402 sb.append("/0x");
2403 sb.append(Integer.toHexString(contentView.getLayoutId()));
2404 } else {
2405 sb.append("null");
2406 }
2407 sb.append(" vibrate=");
Daniel Sandler6738eee2012-11-16 12:03:32 -05002408 if ((this.defaults & DEFAULT_VIBRATE) != 0) {
2409 sb.append("default");
2410 } else if (this.vibrate != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002411 int N = this.vibrate.length-1;
2412 sb.append("[");
2413 for (int i=0; i<N; i++) {
2414 sb.append(this.vibrate[i]);
2415 sb.append(',');
2416 }
Simon Schoar8cf97d92009-06-10 22:08:37 +02002417 if (N != -1) {
2418 sb.append(this.vibrate[N]);
2419 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002420 sb.append("]");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002421 } else {
2422 sb.append("null");
2423 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002424 sb.append(" sound=");
Daniel Sandler6738eee2012-11-16 12:03:32 -05002425 if ((this.defaults & DEFAULT_SOUND) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002426 sb.append("default");
Daniel Sandler6738eee2012-11-16 12:03:32 -05002427 } else if (this.sound != null) {
2428 sb.append(this.sound.toString());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002429 } else {
2430 sb.append("null");
2431 }
Chris Wren365b6d32015-07-16 10:39:26 -04002432 if (this.tickerText != null) {
2433 sb.append(" tick");
2434 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002435 sb.append(" defaults=0x");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002436 sb.append(Integer.toHexString(this.defaults));
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002437 sb.append(" flags=0x");
Daniel Sandlere46cbd32010-06-17 10:35:26 -04002438 sb.append(Integer.toHexString(this.flags));
Dan Sandler26e81cf2014-05-06 10:01:27 -04002439 sb.append(String.format(" color=0x%08x", this.color));
Griff Hazen5cadc3b2014-05-20 09:55:39 -07002440 if (this.category != null) {
2441 sb.append(" category=");
2442 sb.append(this.category);
2443 }
2444 if (this.mGroupKey != null) {
2445 sb.append(" groupKey=");
2446 sb.append(this.mGroupKey);
2447 }
2448 if (this.mSortKey != null) {
2449 sb.append(" sortKey=");
2450 sb.append(this.mSortKey);
2451 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002452 if (actions != null) {
Dan Sandler1b718782014-07-18 12:43:45 -04002453 sb.append(" actions=");
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002454 sb.append(actions.length);
Dan Sandler1b718782014-07-18 12:43:45 -04002455 }
2456 sb.append(" vis=");
2457 sb.append(visibilityToString(this.visibility));
2458 if (this.publicVersion != null) {
2459 sb.append(" publicVersion=");
2460 sb.append(publicVersion.toString());
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002461 }
2462 sb.append(")");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002463 return sb.toString();
2464 }
Joe Onorato46439ce2010-11-19 13:56:21 -08002465
Dan Sandler1b718782014-07-18 12:43:45 -04002466 /**
2467 * {@hide}
2468 */
2469 public static String visibilityToString(int vis) {
2470 switch (vis) {
2471 case VISIBILITY_PRIVATE:
2472 return "PRIVATE";
2473 case VISIBILITY_PUBLIC:
2474 return "PUBLIC";
2475 case VISIBILITY_SECRET:
2476 return "SECRET";
2477 default:
2478 return "UNKNOWN(" + String.valueOf(vis) + ")";
2479 }
2480 }
2481
Joe Onoratocb109a02011-01-18 17:57:41 -08002482 /**
John Spurlock1d881a12015-03-18 19:21:54 -04002483 * {@hide}
2484 */
2485 public static String priorityToString(@Priority int pri) {
2486 switch (pri) {
2487 case PRIORITY_MIN:
2488 return "MIN";
2489 case PRIORITY_LOW:
2490 return "LOW";
2491 case PRIORITY_DEFAULT:
2492 return "DEFAULT";
2493 case PRIORITY_HIGH:
2494 return "HIGH";
2495 case PRIORITY_MAX:
2496 return "MAX";
2497 default:
2498 return "UNKNOWN(" + String.valueOf(pri) + ")";
2499 }
2500 }
2501
2502 /**
Julia Reynoldsb5e44b72016-08-16 15:00:25 -04002503 * Returns the id of the channel this notification posts to.
2504 */
Julia Reynolds37856052016-11-11 09:20:07 -05002505 public String getChannel() {
Julia Reynoldsb5e44b72016-08-16 15:00:25 -04002506 return mChannelId;
2507 }
2508
2509 /**
Julia Reynolds13d898c2017-02-02 12:22:05 -05002510 * Returns the time at which this notification should be canceled by the system, if it's not
2511 * canceled already.
Julia Reynolds2a128742016-11-28 14:29:25 -05002512 */
2513 public long getTimeout() {
2514 return mTimeout;
2515 }
2516
2517 /**
Julia Reynoldse071abd2017-03-22 10:52:11 -04002518 * Returns what icon should be shown for this notification if it is being displayed in a
2519 * Launcher that supports badging. Will be one of {@link #BADGE_ICON_NONE},
2520 * {@link #BADGE_ICON_SMALL}, or {@link #BADGE_ICON_LARGE}.
2521 */
2522 public int getBadgeIconType() {
2523 return mBadgeIcon;
2524 }
2525
2526 /**
Julia Reynolds13d898c2017-02-02 12:22:05 -05002527 * Returns the {@link ShortcutInfo#getId() id} that this notification supersedes, if any.
2528 */
2529 public String getShortcutId() {
2530 return mShortcutId;
2531 }
2532
Julia Reynolds3aedded2017-03-31 14:42:09 -04002533
2534 /**
2535 * Returns the settings text provided to {@link Builder#setSettingsText(CharSequence)}.
2536 */
2537 public CharSequence getSettingsText() {
2538 return mSettingsText;
2539 }
2540
Julia Reynolds13d898c2017-02-02 12:22:05 -05002541 /**
Julia Reynoldsa79c3712017-04-21 10:29:57 -04002542 * Returns which type of notifications in a group are responsible for audibly alerting the
2543 * user. See {@link #GROUP_ALERT_ALL}, {@link #GROUP_ALERT_CHILDREN},
2544 * {@link #GROUP_ALERT_SUMMARY}.
2545 */
2546 public @GroupAlertBehavior int getGroupAlertBehavior() {
2547 return mGroupAlertBehavior;
2548 }
2549
2550 /**
Dan Sandlerd63f9322015-05-06 15:18:49 -04002551 * The small icon representing this notification in the status bar and content view.
2552 *
2553 * @return the small icon representing this notification.
2554 *
2555 * @see Builder#getSmallIcon()
2556 * @see Builder#setSmallIcon(Icon)
2557 */
2558 public Icon getSmallIcon() {
2559 return mSmallIcon;
2560 }
2561
2562 /**
2563 * Used when notifying to clean up legacy small icons.
2564 * @hide
2565 */
2566 public void setSmallIcon(Icon icon) {
2567 mSmallIcon = icon;
2568 }
2569
2570 /**
2571 * The large icon shown in this notification's content view.
2572 * @see Builder#getLargeIcon()
2573 * @see Builder#setLargeIcon(Icon)
2574 */
2575 public Icon getLargeIcon() {
2576 return mLargeIcon;
2577 }
2578
2579 /**
Christoph Studer4600f9b2014-07-22 22:44:43 +02002580 * @hide
2581 */
Christoph Studerc8db24b2014-07-25 17:50:30 +02002582 public boolean isGroupSummary() {
2583 return mGroupKey != null && (flags & FLAG_GROUP_SUMMARY) != 0;
2584 }
2585
2586 /**
2587 * @hide
2588 */
2589 public boolean isGroupChild() {
2590 return mGroupKey != null && (flags & FLAG_GROUP_SUMMARY) == 0;
2591 }
2592
2593 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002594 * Builder class for {@link Notification} objects.
Joe Malin8d40d042012-11-05 11:36:40 -08002595 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002596 * Provides a convenient way to set the various fields of a {@link Notification} and generate
Scott Main183bf112012-08-13 19:12:13 -07002597 * content views using the platform's notification layout template. If your app supports
2598 * versions of Android as old as API level 4, you can instead use
2599 * {@link android.support.v4.app.NotificationCompat.Builder NotificationCompat.Builder},
2600 * available in the <a href="{@docRoot}tools/extras/support-library.html">Android Support
2601 * library</a>.
Joe Malin8d40d042012-11-05 11:36:40 -08002602 *
Scott Main183bf112012-08-13 19:12:13 -07002603 * <p>Example:
Joe Malin8d40d042012-11-05 11:36:40 -08002604 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002605 * <pre class="prettyprint">
Scott Main183bf112012-08-13 19:12:13 -07002606 * Notification noti = new Notification.Builder(mContext)
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002607 * .setContentTitle(&quot;New mail from &quot; + sender.toString())
2608 * .setContentText(subject)
2609 * .setSmallIcon(R.drawable.new_mail)
2610 * .setLargeIcon(aBitmap)
Chris Wrenfbd96ba2012-05-01 12:03:58 -04002611 * .build();
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002612 * </pre>
Joe Onoratocb109a02011-01-18 17:57:41 -08002613 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002614 public static class Builder {
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05002615 /**
2616 * @hide
2617 */
2618 public static final String EXTRA_REBUILD_CONTENT_VIEW_ACTION_COUNT =
2619 "android.rebuild.contentViewActionCount";
2620 /**
2621 * @hide
2622 */
2623 public static final String EXTRA_REBUILD_BIG_CONTENT_VIEW_ACTION_COUNT
2624 = "android.rebuild.bigViewActionCount";
2625 /**
2626 * @hide
2627 */
2628 public static final String EXTRA_REBUILD_HEADS_UP_CONTENT_VIEW_ACTION_COUNT
2629 = "android.rebuild.hudViewActionCount";
2630
Daniel Sandler602ad1c2012-06-12 16:06:27 -04002631 private static final int MAX_ACTION_BUTTONS = 3;
Daniel Sandler8680bf82012-05-15 16:52:52 -04002632
Selim Cinek6743c0b2017-01-18 18:24:01 -08002633 private static final boolean USE_ONLY_TITLE_IN_LOW_PRIORITY_SUMMARY =
2634 SystemProperties.getBoolean("notifications.only_title", true);
2635
Joe Onorato46439ce2010-11-19 13:56:21 -08002636 private Context mContext;
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002637 private Notification mN;
2638 private Bundle mUserExtras = new Bundle();
Chris Wrenfbd96ba2012-05-01 12:03:58 -04002639 private Style mStyle;
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002640 private ArrayList<Action> mActions = new ArrayList<Action>(MAX_ACTION_BUTTONS);
2641 private ArrayList<String> mPersonList = new ArrayList<String>();
2642 private NotificationColorUtil mColorUtil;
Selim Cinek7b9605b2017-01-19 17:36:00 -08002643 private boolean mIsLegacy;
2644 private boolean mIsLegacyInitialized;
Christoph Studer7ac80e62014-08-04 16:01:57 +02002645
2646 /**
Adrian Roos4ff3b122016-02-01 12:26:13 -08002647 * Caches a contrast-enhanced version of {@link #mCachedContrastColorIsFor}.
2648 */
2649 private int mCachedContrastColor = COLOR_INVALID;
2650 private int mCachedContrastColorIsFor = COLOR_INVALID;
Adrian Roos487374f2017-01-11 15:48:14 -08002651 /**
2652 * Caches a ambient version of {@link #mCachedContrastColorIsFor}.
2653 */
2654 private int mCachedAmbientColor = COLOR_INVALID;
2655 private int mCachedAmbientColorIsFor = COLOR_INVALID;
Adrian Roos4ff3b122016-02-01 12:26:13 -08002656
2657 /**
Adrian Roos70d7aa32017-01-11 15:39:06 -08002658 * Caches an instance of StandardTemplateParams. Note that this may have been used before,
2659 * so make sure to call {@link StandardTemplateParams#reset()} before using it.
2660 */
2661 StandardTemplateParams mParams = new StandardTemplateParams();
Selim Cinek7b9605b2017-01-19 17:36:00 -08002662 private int mTextColorsAreForBackground = COLOR_INVALID;
2663 private int mPrimaryTextColor = COLOR_INVALID;
2664 private int mSecondaryTextColor = COLOR_INVALID;
2665 private int mActionBarColor = COLOR_INVALID;
Adrian Roos70d7aa32017-01-11 15:39:06 -08002666
2667 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002668 * Constructs a new Builder with the defaults:
Joe Onoratocb109a02011-01-18 17:57:41 -08002669 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002670
2671 * <table>
2672 * <tr><th align=right>priority</th>
2673 * <td>{@link #PRIORITY_DEFAULT}</td></tr>
2674 * <tr><th align=right>when</th>
2675 * <td>now ({@link System#currentTimeMillis()})</td></tr>
2676 * <tr><th align=right>audio stream</th>
2677 * <td>{@link #STREAM_DEFAULT}</td></tr>
2678 * </table>
Joe Onoratocb109a02011-01-18 17:57:41 -08002679 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002680
2681 * @param context
2682 * A {@link Context} that will be used by the Builder to construct the
2683 * RemoteViews. The Context will not be held past the lifetime of this Builder
2684 * object.
Geoffrey Pitsch5caa2762017-01-12 09:35:54 -05002685 * @param channelId
2686 * The constructed Notification will be posted on this
2687 * {@link NotificationChannel}. To use a NotificationChannel, it must first be
2688 * created using {@link NotificationManager#createNotificationChannel}.
Joe Onoratocb109a02011-01-18 17:57:41 -08002689 */
Geoffrey Pitsch5caa2762017-01-12 09:35:54 -05002690 public Builder(Context context, String channelId) {
2691 this(context, (Notification) null);
2692 mN.mChannelId = channelId;
2693 }
2694
2695 /**
2696 * @deprecated use {@link Notification.Builder#Notification.Builder(Context, String)}
2697 * instead. All posted Notifications must specify a NotificationChannel Id.
2698 */
2699 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -08002700 public Builder(Context context) {
Geoffrey Pitsch5caa2762017-01-12 09:35:54 -05002701 this(context, (Notification) null);
Joe Onorato46439ce2010-11-19 13:56:21 -08002702 }
2703
Joe Onoratocb109a02011-01-18 17:57:41 -08002704 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002705 * @hide
Christoph Studer4600f9b2014-07-22 22:44:43 +02002706 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002707 public Builder(Context context, Notification toAdopt) {
2708 mContext = context;
Christoph Studer4600f9b2014-07-22 22:44:43 +02002709
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002710 if (toAdopt == null) {
2711 mN = new Notification();
Selim Cinek0ff1ce602016-04-05 18:27:16 -07002712 if (context.getApplicationInfo().targetSdkVersion < Build.VERSION_CODES.N) {
2713 mN.extras.putBoolean(EXTRA_SHOW_WHEN, true);
2714 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002715 mN.priority = PRIORITY_DEFAULT;
2716 mN.visibility = VISIBILITY_PRIVATE;
2717 } else {
2718 mN = toAdopt;
2719 if (mN.actions != null) {
2720 Collections.addAll(mActions, mN.actions);
Christoph Studer4600f9b2014-07-22 22:44:43 +02002721 }
2722
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002723 if (mN.extras.containsKey(EXTRA_PEOPLE)) {
2724 Collections.addAll(mPersonList, mN.extras.getStringArray(EXTRA_PEOPLE));
2725 }
2726
Selim Cinek4ac6f602016-06-13 15:47:03 -07002727 if (mN.getSmallIcon() == null && mN.icon != 0) {
2728 setSmallIcon(mN.icon);
2729 }
2730
2731 if (mN.getLargeIcon() == null && mN.largeIcon != null) {
2732 setLargeIcon(mN.largeIcon);
2733 }
2734
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002735 String templateClass = mN.extras.getString(EXTRA_TEMPLATE);
2736 if (!TextUtils.isEmpty(templateClass)) {
2737 final Class<? extends Style> styleClass
2738 = getNotificationStyleClass(templateClass);
2739 if (styleClass == null) {
2740 Log.d(TAG, "Unknown style class: " + templateClass);
2741 } else {
2742 try {
Adrian Roosc1a80b02016-04-05 14:54:55 -07002743 final Constructor<? extends Style> ctor =
2744 styleClass.getDeclaredConstructor();
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002745 ctor.setAccessible(true);
2746 final Style style = ctor.newInstance();
2747 style.restoreFromExtras(mN.extras);
2748
2749 if (style != null) {
2750 setStyle(style);
2751 }
2752 } catch (Throwable t) {
2753 Log.e(TAG, "Could not create Style", t);
2754 }
2755 }
2756 }
2757
2758 }
2759 }
2760
2761 private NotificationColorUtil getColorUtil() {
Selim Cinek99104832017-01-25 14:47:33 -08002762 if (mColorUtil == null) {
2763 mColorUtil = NotificationColorUtil.getInstance(mContext);
Christoph Studer4600f9b2014-07-22 22:44:43 +02002764 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002765 return mColorUtil;
Christoph Studer4600f9b2014-07-22 22:44:43 +02002766 }
2767
2768 /**
Julia Reynolds13d898c2017-02-02 12:22:05 -05002769 * If this notification is duplicative of a Launcher shortcut, sets the
2770 * {@link ShortcutInfo#getId() id} of the shortcut, in case the Launcher wants to hide
2771 * the shortcut.
2772 *
2773 * This field will be ignored by Launchers that don't support badging or
2774 * {@link android.content.pm.ShortcutManager shortcuts}.
2775 *
2776 * @param shortcutId the {@link ShortcutInfo#getId() id} of the shortcut this notification
2777 * supersedes
2778 */
2779 public Builder setShortcutId(String shortcutId) {
2780 mN.mShortcutId = shortcutId;
2781 return this;
2782 }
2783
2784 /**
Julia Reynoldse071abd2017-03-22 10:52:11 -04002785 * Sets which icon to display as a badge for this notification.
2786 *
2787 * Must be one of {@link #BADGE_ICON_NONE}, {@link #BADGE_ICON_SMALL},
2788 * {@link #BADGE_ICON_LARGE}.
2789 *
2790 * Note: This value might be ignored, for launchers that don't support badge icons.
2791 */
Julia Reynolds612beb22017-03-30 10:48:30 -04002792 public Builder setBadgeIconType(int icon) {
Julia Reynoldse071abd2017-03-22 10:52:11 -04002793 mN.mBadgeIcon = icon;
2794 return this;
2795 }
2796
2797 /**
Julia Reynoldsa79c3712017-04-21 10:29:57 -04002798 * Sets the group alert behavior for this notification. Use this method to mute this
2799 * notification if alerts for this notification's group should be handled by a different
2800 * notification. This is only applicable for notifications that belong to a
2801 * {@link #setGroup(String) group}.
2802 *
2803 * <p> The default value is {@link #GROUP_ALERT_ALL}.</p>
2804 */
2805 public Builder setGroupAlertBehavior(@GroupAlertBehavior int groupAlertBehavior) {
2806 mN.mGroupAlertBehavior = groupAlertBehavior;
2807 return this;
2808 }
2809
2810 /**
Julia Reynoldsb5e44b72016-08-16 15:00:25 -04002811 * Specifies the channel the notification should be delivered on.
2812 */
2813 public Builder setChannel(String channelId) {
2814 mN.mChannelId = channelId;
2815 return this;
2816 }
2817
2818 /**
Julia Reynolds50989772017-02-23 14:32:16 -05002819 * Specifies a duration in milliseconds after which this notification should be canceled,
2820 * if it is not already canceled.
Julia Reynolds2a128742016-11-28 14:29:25 -05002821 */
Julia Reynolds50989772017-02-23 14:32:16 -05002822 public Builder setTimeout(long durationMs) {
2823 mN.mTimeout = durationMs;
Julia Reynolds2a128742016-11-28 14:29:25 -05002824 return this;
2825 }
2826
2827 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002828 * Add a timestamp pertaining to the notification (usually the time the event occurred).
Selim Cinek0ff1ce602016-04-05 18:27:16 -07002829 *
2830 * For apps targeting {@link android.os.Build.VERSION_CODES#N} and above, this time is not
2831 * shown anymore by default and must be opted into by using
2832 * {@link android.app.Notification.Builder#setShowWhen(boolean)}
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002833 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002834 * @see Notification#when
Joe Onoratocb109a02011-01-18 17:57:41 -08002835 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002836 public Builder setWhen(long when) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002837 mN.when = when;
Joe Onorato46439ce2010-11-19 13:56:21 -08002838 return this;
2839 }
2840
Joe Onoratocb109a02011-01-18 17:57:41 -08002841 /**
Griff Hazen50c11652014-05-16 09:46:31 -07002842 * Control whether the timestamp set with {@link #setWhen(long) setWhen} is shown
Daniel Sandler0c890492012-09-12 17:23:10 -07002843 * in the content view.
Selim Cinek0ff1ce602016-04-05 18:27:16 -07002844 * For apps targeting {@link android.os.Build.VERSION_CODES#N} and above, this defaults to
2845 * {@code false}. For earlier apps, the default is {@code true}.
Daniel Sandler0c890492012-09-12 17:23:10 -07002846 */
2847 public Builder setShowWhen(boolean show) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002848 mN.extras.putBoolean(EXTRA_SHOW_WHEN, show);
Daniel Sandler0c890492012-09-12 17:23:10 -07002849 return this;
2850 }
2851
2852 /**
Daniel Sandlerd33b8032012-05-10 11:41:48 -04002853 * Show the {@link Notification#when} field as a stopwatch.
Joe Malin8d40d042012-11-05 11:36:40 -08002854 *
2855 * Instead of presenting <code>when</code> as a timestamp, the notification will show an
Daniel Sandlerd33b8032012-05-10 11:41:48 -04002856 * automatically updating display of the minutes and seconds since <code>when</code>.
Daniel Sandlera2985ed2012-04-03 16:42:00 -04002857 *
Daniel Sandlerd33b8032012-05-10 11:41:48 -04002858 * Useful when showing an elapsed time (like an ongoing phone call).
2859 *
Selim Cinek81c23aa2016-02-25 16:23:13 -08002860 * The counter can also be set to count down to <code>when</code> when using
Adrian Roos96b7e202016-05-17 13:50:38 -07002861 * {@link #setChronometerCountDown(boolean)}.
Selim Cinek81c23aa2016-02-25 16:23:13 -08002862 *
Daniel Sandlerd33b8032012-05-10 11:41:48 -04002863 * @see android.widget.Chronometer
Daniel Sandlera2985ed2012-04-03 16:42:00 -04002864 * @see Notification#when
Adrian Roos96b7e202016-05-17 13:50:38 -07002865 * @see #setChronometerCountDown(boolean)
Daniel Sandlera2985ed2012-04-03 16:42:00 -04002866 */
2867 public Builder setUsesChronometer(boolean b) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002868 mN.extras.putBoolean(EXTRA_SHOW_CHRONOMETER, b);
Daniel Sandlera2985ed2012-04-03 16:42:00 -04002869 return this;
2870 }
2871
2872 /**
Selim Cinek81c23aa2016-02-25 16:23:13 -08002873 * Sets the Chronometer to count down instead of counting up.
2874 *
2875 * <p>This is only relevant if {@link #setUsesChronometer(boolean)} has been set to true.
2876 * If it isn't set the chronometer will count up.
2877 *
2878 * @see #setUsesChronometer(boolean)
2879 */
Adrian Roos96b7e202016-05-17 13:50:38 -07002880 public Builder setChronometerCountDown(boolean countDown) {
2881 mN.extras.putBoolean(EXTRA_CHRONOMETER_COUNT_DOWN, countDown);
Selim Cinek81c23aa2016-02-25 16:23:13 -08002882 return this;
2883 }
2884
2885 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002886 * Set the small icon resource, which will be used to represent the notification in the
2887 * status bar.
Joe Onoratocb109a02011-01-18 17:57:41 -08002888 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002889
2890 * The platform template for the expanded view will draw this icon in the left, unless a
2891 * {@link #setLargeIcon(Bitmap) large icon} has also been specified, in which case the small
2892 * icon will be moved to the right-hand side.
2893 *
2894
2895 * @param icon
2896 * A resource ID in the application's package of the drawable to use.
2897 * @see Notification#icon
Joe Onoratocb109a02011-01-18 17:57:41 -08002898 */
Tor Norbye7b9c9122013-05-30 16:48:33 -07002899 public Builder setSmallIcon(@DrawableRes int icon) {
Dan Sandlerd63f9322015-05-06 15:18:49 -04002900 return setSmallIcon(icon != 0
2901 ? Icon.createWithResource(mContext, icon)
2902 : null);
Joe Onorato46439ce2010-11-19 13:56:21 -08002903 }
2904
Joe Onoratocb109a02011-01-18 17:57:41 -08002905 /**
2906 * A variant of {@link #setSmallIcon(int) setSmallIcon(int)} that takes an additional
2907 * level parameter for when the icon is a {@link android.graphics.drawable.LevelListDrawable
2908 * LevelListDrawable}.
2909 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002910 * @param icon A resource ID in the application's package of the drawable to use.
Joe Onoratocb109a02011-01-18 17:57:41 -08002911 * @param level The level to use for the icon.
2912 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002913 * @see Notification#icon
2914 * @see Notification#iconLevel
Joe Onoratocb109a02011-01-18 17:57:41 -08002915 */
Tor Norbye7b9c9122013-05-30 16:48:33 -07002916 public Builder setSmallIcon(@DrawableRes int icon, int level) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002917 mN.iconLevel = level;
Dan Sandlerd63f9322015-05-06 15:18:49 -04002918 return setSmallIcon(icon);
2919 }
2920
2921 /**
2922 * Set the small icon, which will be used to represent the notification in the
2923 * status bar and content view (unless overriden there by a
2924 * {@link #setLargeIcon(Bitmap) large icon}).
2925 *
2926 * @param icon An Icon object to use.
2927 * @see Notification#icon
2928 */
2929 public Builder setSmallIcon(Icon icon) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002930 mN.setSmallIcon(icon);
2931 if (icon != null && icon.getType() == Icon.TYPE_RESOURCE) {
2932 mN.icon = icon.getResId();
2933 }
Joe Onorato46439ce2010-11-19 13:56:21 -08002934 return this;
2935 }
2936
Joe Onoratocb109a02011-01-18 17:57:41 -08002937 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002938 * Set the first line of text in the platform notification template.
Joe Onoratocb109a02011-01-18 17:57:41 -08002939 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002940 public Builder setContentTitle(CharSequence title) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002941 mN.extras.putCharSequence(EXTRA_TITLE, safeCharSequence(title));
Joe Onorato46439ce2010-11-19 13:56:21 -08002942 return this;
2943 }
2944
Joe Onoratocb109a02011-01-18 17:57:41 -08002945 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002946 * Set the second line of text in the platform notification template.
Joe Onoratocb109a02011-01-18 17:57:41 -08002947 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002948 public Builder setContentText(CharSequence text) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002949 mN.extras.putCharSequence(EXTRA_TEXT, safeCharSequence(text));
Joe Onorato46439ce2010-11-19 13:56:21 -08002950 return this;
2951 }
2952
Joe Onoratocb109a02011-01-18 17:57:41 -08002953 /**
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07002954 * This provides some additional information that is displayed in the notification. No
2955 * guarantees are given where exactly it is displayed.
2956 *
2957 * <p>This information should only be provided if it provides an essential
2958 * benefit to the understanding of the notification. The more text you provide the
2959 * less readable it becomes. For example, an email client should only provide the account
2960 * name here if more than one email account has been added.</p>
2961 *
2962 * <p>As of {@link android.os.Build.VERSION_CODES#N} this information is displayed in the
2963 * notification header area.
2964 *
2965 * On Android versions before {@link android.os.Build.VERSION_CODES#N}
2966 * this will be shown in the third line of text in the platform notification template.
2967 * You should not be using {@link #setProgress(int, int, boolean)} at the
2968 * same time on those versions; they occupy the same place.
2969 * </p>
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002970 */
2971 public Builder setSubText(CharSequence text) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002972 mN.extras.putCharSequence(EXTRA_SUB_TEXT, safeCharSequence(text));
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002973 return this;
2974 }
2975
2976 /**
Julia Reynolds3aedded2017-03-31 14:42:09 -04002977 * Provides text that will appear as a link to your application's settings.
2978 *
2979 * <p>This text does not appear within notification {@link Style templates} but may
2980 * appear when the user uses an affordance to learn more about the notification.
2981 * Additionally, this text will not appear unless you provide a valid link target by
2982 * handling {@link #INTENT_CATEGORY_NOTIFICATION_PREFERENCES}.
2983 *
2984 * <p>This text is meant to be concise description about what the user can customize
2985 * when they click on this link. The recommended maximum length is 40 characters.
2986 * @param text
2987 * @return
2988 */
2989 public Builder setSettingsText(CharSequence text) {
2990 mN.mSettingsText = safeCharSequence(text);
2991 return this;
2992 }
2993
2994 /**
Adrian Roose458aa82015-12-08 16:17:19 -08002995 * Set the remote input history.
2996 *
2997 * This should be set to the most recent inputs that have been sent
2998 * through a {@link RemoteInput} of this Notification and cleared once the it is no
2999 * longer relevant (e.g. for chat notifications once the other party has responded).
3000 *
3001 * The most recent input must be stored at the 0 index, the second most recent at the
3002 * 1 index, etc. Note that the system will limit both how far back the inputs will be shown
3003 * and how much of each individual input is shown.
3004 *
3005 * <p>Note: The reply text will only be shown on notifications that have least one action
3006 * with a {@code RemoteInput}.</p>
3007 */
3008 public Builder setRemoteInputHistory(CharSequence[] text) {
3009 if (text == null) {
3010 mN.extras.putCharSequenceArray(EXTRA_REMOTE_INPUT_HISTORY, null);
3011 } else {
3012 final int N = Math.min(MAX_REPLY_HISTORY, text.length);
3013 CharSequence[] safe = new CharSequence[N];
3014 for (int i = 0; i < N; i++) {
3015 safe[i] = safeCharSequence(text[i]);
3016 }
3017 mN.extras.putCharSequenceArray(EXTRA_REMOTE_INPUT_HISTORY, safe);
3018 }
3019 return this;
3020 }
3021
3022 /**
Julia Reynolds13d898c2017-02-02 12:22:05 -05003023 * Sets the number of items this notification represents. May be displayed as a badge count
3024 * for Launchers that support badging.
Joe Onoratocb109a02011-01-18 17:57:41 -08003025 */
Joe Onorato8595a3d2010-11-19 18:12:07 -08003026 public Builder setNumber(int number) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003027 mN.number = number;
Joe Onorato8595a3d2010-11-19 18:12:07 -08003028 return this;
3029 }
3030
Joe Onoratocb109a02011-01-18 17:57:41 -08003031 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003032 * A small piece of additional information pertaining to this notification.
3033 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003034 * The platform template will draw this on the last line of the notification, at the far
3035 * right (to the right of a smallIcon if it has been placed there).
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07003036 *
3037 * @deprecated use {@link #setSubText(CharSequence)} instead to set a text in the header.
3038 * For legacy apps targeting a version below {@link android.os.Build.VERSION_CODES#N} this
3039 * field will still show up, but the subtext will take precedence.
Joe Onoratocb109a02011-01-18 17:57:41 -08003040 */
Aurimas Liutikas514c5ef2016-05-24 15:22:55 -07003041 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -08003042 public Builder setContentInfo(CharSequence info) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003043 mN.extras.putCharSequence(EXTRA_INFO_TEXT, safeCharSequence(info));
Joe Onorato46439ce2010-11-19 13:56:21 -08003044 return this;
3045 }
3046
Joe Onoratocb109a02011-01-18 17:57:41 -08003047 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003048 * Set the progress this notification represents.
3049 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003050 * The platform template will represent this using a {@link ProgressBar}.
Jeff Sharkey1c400132011-08-05 14:50:13 -07003051 */
3052 public Builder setProgress(int max, int progress, boolean indeterminate) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003053 mN.extras.putInt(EXTRA_PROGRESS, progress);
3054 mN.extras.putInt(EXTRA_PROGRESS_MAX, max);
3055 mN.extras.putBoolean(EXTRA_PROGRESS_INDETERMINATE, indeterminate);
Jeff Sharkey1c400132011-08-05 14:50:13 -07003056 return this;
3057 }
3058
3059 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003060 * Supply a custom RemoteViews to use instead of the platform template.
3061 *
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003062 * Use {@link #setCustomContentView(RemoteViews)} instead.
Joe Onoratocb109a02011-01-18 17:57:41 -08003063 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003064 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -08003065 public Builder setContent(RemoteViews views) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003066 return setCustomContentView(views);
3067 }
3068
3069 /**
3070 * Supply custom RemoteViews to use instead of the platform template.
3071 *
3072 * This will override the layout that would otherwise be constructed by this Builder
3073 * object.
3074 */
3075 public Builder setCustomContentView(RemoteViews contentView) {
3076 mN.contentView = contentView;
3077 return this;
3078 }
3079
3080 /**
3081 * Supply custom RemoteViews to use instead of the platform template in the expanded form.
3082 *
3083 * This will override the expanded layout that would otherwise be constructed by this
3084 * Builder object.
3085 */
3086 public Builder setCustomBigContentView(RemoteViews contentView) {
3087 mN.bigContentView = contentView;
3088 return this;
3089 }
3090
3091 /**
3092 * Supply custom RemoteViews to use instead of the platform template in the heads up dialog.
3093 *
3094 * This will override the heads-up layout that would otherwise be constructed by this
3095 * Builder object.
3096 */
3097 public Builder setCustomHeadsUpContentView(RemoteViews contentView) {
3098 mN.headsUpContentView = contentView;
Joe Onorato46439ce2010-11-19 13:56:21 -08003099 return this;
3100 }
3101
Joe Onoratocb109a02011-01-18 17:57:41 -08003102 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003103 * Supply a {@link PendingIntent} to be sent when the notification is clicked.
3104 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003105 * As of {@link android.os.Build.VERSION_CODES#HONEYCOMB}, if this field is unset and you
3106 * have specified a custom RemoteViews with {@link #setContent(RemoteViews)}, you can use
3107 * {@link RemoteViews#setOnClickPendingIntent RemoteViews.setOnClickPendingIntent(int,PendingIntent)}
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003108 * to assign PendingIntents to individual views in that custom layout (i.e., to create
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003109 * clickable buttons inside the notification view).
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003110 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003111 * @see Notification#contentIntent Notification.contentIntent
Joe Onoratocb109a02011-01-18 17:57:41 -08003112 */
Joe Onorato46439ce2010-11-19 13:56:21 -08003113 public Builder setContentIntent(PendingIntent intent) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003114 mN.contentIntent = intent;
Joe Onorato46439ce2010-11-19 13:56:21 -08003115 return this;
3116 }
3117
Joe Onoratocb109a02011-01-18 17:57:41 -08003118 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003119 * Supply a {@link PendingIntent} to send when the notification is cleared explicitly by the user.
3120 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003121 * @see Notification#deleteIntent
Joe Onoratocb109a02011-01-18 17:57:41 -08003122 */
Joe Onorato46439ce2010-11-19 13:56:21 -08003123 public Builder setDeleteIntent(PendingIntent intent) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003124 mN.deleteIntent = intent;
Joe Onorato46439ce2010-11-19 13:56:21 -08003125 return this;
3126 }
3127
Joe Onoratocb109a02011-01-18 17:57:41 -08003128 /**
3129 * An intent to launch instead of posting the notification to the status bar.
3130 * Only for use with extremely high-priority notifications demanding the user's
3131 * <strong>immediate</strong> attention, such as an incoming phone call or
3132 * alarm clock that the user has explicitly set to a particular time.
3133 * If this facility is used for something else, please give the user an option
3134 * to turn it off and use a normal notification, as this can be extremely
3135 * disruptive.
3136 *
Chris Wren47c20a12014-06-18 17:27:29 -04003137 * <p>
3138 * The system UI may choose to display a heads-up notification, instead of
3139 * launching this intent, while the user is using the device.
3140 * </p>
3141 *
Joe Onoratocb109a02011-01-18 17:57:41 -08003142 * @param intent The pending intent to launch.
3143 * @param highPriority Passing true will cause this notification to be sent
3144 * even if other notifications are suppressed.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003145 *
3146 * @see Notification#fullScreenIntent
Joe Onoratocb109a02011-01-18 17:57:41 -08003147 */
Joe Onorato46439ce2010-11-19 13:56:21 -08003148 public Builder setFullScreenIntent(PendingIntent intent, boolean highPriority) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003149 mN.fullScreenIntent = intent;
Joe Onorato46439ce2010-11-19 13:56:21 -08003150 setFlag(FLAG_HIGH_PRIORITY, highPriority);
3151 return this;
3152 }
3153
Joe Onoratocb109a02011-01-18 17:57:41 -08003154 /**
Dan Sandler5fcdf6e2014-07-18 11:31:15 -04003155 * Set the "ticker" text which is sent to accessibility services.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003156 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003157 * @see Notification#tickerText
Joe Onoratocb109a02011-01-18 17:57:41 -08003158 */
Joe Onorato46439ce2010-11-19 13:56:21 -08003159 public Builder setTicker(CharSequence tickerText) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003160 mN.tickerText = safeCharSequence(tickerText);
Joe Onorato46439ce2010-11-19 13:56:21 -08003161 return this;
3162 }
3163
Joe Onoratocb109a02011-01-18 17:57:41 -08003164 /**
Dan Sandler5fcdf6e2014-07-18 11:31:15 -04003165 * Obsolete version of {@link #setTicker(CharSequence)}.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003166 *
Joe Onoratocb109a02011-01-18 17:57:41 -08003167 */
Dan Sandler5fcdf6e2014-07-18 11:31:15 -04003168 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -08003169 public Builder setTicker(CharSequence tickerText, RemoteViews views) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003170 setTicker(tickerText);
3171 // views is ignored
Joe Onorato46439ce2010-11-19 13:56:21 -08003172 return this;
3173 }
3174
Joe Onoratocb109a02011-01-18 17:57:41 -08003175 /**
Dan Sandlerd63f9322015-05-06 15:18:49 -04003176 * Add a large icon to the notification content view.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003177 *
3178 * In the platform template, this image will be shown on the left of the notification view
Dan Sandlerd63f9322015-05-06 15:18:49 -04003179 * in place of the {@link #setSmallIcon(Icon) small icon} (which will be placed in a small
3180 * badge atop the large icon).
Dan Sandler08a04c12015-05-06 15:18:49 -04003181 */
Dan Sandlerd63f9322015-05-06 15:18:49 -04003182 public Builder setLargeIcon(Bitmap b) {
3183 return setLargeIcon(b != null ? Icon.createWithBitmap(b) : null);
3184 }
3185
3186 /**
3187 * Add a large icon to the notification content view.
3188 *
3189 * In the platform template, this image will be shown on the left of the notification view
3190 * in place of the {@link #setSmallIcon(Icon) small icon} (which will be placed in a small
3191 * badge atop the large icon).
3192 */
3193 public Builder setLargeIcon(Icon icon) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003194 mN.mLargeIcon = icon;
3195 mN.extras.putParcelable(EXTRA_LARGE_ICON, icon);
Joe Onorato46439ce2010-11-19 13:56:21 -08003196 return this;
3197 }
3198
Joe Onoratocb109a02011-01-18 17:57:41 -08003199 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003200 * Set the sound to play.
3201 *
John Spurlockc0650f022014-07-19 13:22:39 -04003202 * It will be played using the {@link #AUDIO_ATTRIBUTES_DEFAULT default audio attributes}
3203 * for notifications.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003204 *
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05003205 * @deprecated use {@link NotificationChannel#setSound(Uri, AudioAttributes)} instead.
Joe Onoratocb109a02011-01-18 17:57:41 -08003206 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05003207 @Deprecated
Joe Onorato52f80cd2010-11-21 15:34:48 -08003208 public Builder setSound(Uri sound) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003209 mN.sound = sound;
3210 mN.audioAttributes = AUDIO_ATTRIBUTES_DEFAULT;
Joe Onorato52f80cd2010-11-21 15:34:48 -08003211 return this;
3212 }
3213
Joe Onoratocb109a02011-01-18 17:57:41 -08003214 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003215 * Set the sound to play, along with a specific stream on which to play it.
Joe Onoratocb109a02011-01-18 17:57:41 -08003216 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003217 * See {@link android.media.AudioManager} for the <code>STREAM_</code> constants.
3218 *
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05003219 * @deprecated use {@link NotificationChannel#setSound(Uri, AudioAttributes)}.
Joe Onoratocb109a02011-01-18 17:57:41 -08003220 */
Jean-Michel Trivi81f871e2014-08-06 16:32:38 -07003221 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -08003222 public Builder setSound(Uri sound, int streamType) {
Jean-Michel Trivi2f7511f2016-11-28 15:40:27 -08003223 PlayerBase.deprecateStreamTypeForPlayback(streamType, "Notification", "setSound()");
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003224 mN.sound = sound;
3225 mN.audioStreamType = streamType;
Joe Onorato46439ce2010-11-19 13:56:21 -08003226 return this;
3227 }
3228
Joe Onoratocb109a02011-01-18 17:57:41 -08003229 /**
John Spurlockc0650f022014-07-19 13:22:39 -04003230 * Set the sound to play, along with specific {@link AudioAttributes audio attributes} to
3231 * use during playback.
3232 *
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05003233 * @deprecated use {@link NotificationChannel#setSound(Uri, AudioAttributes)} instead.
John Spurlockc0650f022014-07-19 13:22:39 -04003234 * @see Notification#sound
3235 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05003236 @Deprecated
John Spurlockc0650f022014-07-19 13:22:39 -04003237 public Builder setSound(Uri sound, AudioAttributes audioAttributes) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003238 mN.sound = sound;
3239 mN.audioAttributes = audioAttributes;
John Spurlockc0650f022014-07-19 13:22:39 -04003240 return this;
3241 }
3242
3243 /**
Joe Onoratocb109a02011-01-18 17:57:41 -08003244 * Set the vibration pattern to use.
3245 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003246 * See {@link android.os.Vibrator#vibrate(long[], int)} for a discussion of the
3247 * <code>pattern</code> parameter.
3248 *
Chris Wren47c20a12014-06-18 17:27:29 -04003249 * <p>
3250 * A notification that vibrates is more likely to be presented as a heads-up notification.
3251 * </p>
3252 *
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05003253 * @deprecated use {@link NotificationChannel#setVibrationPattern(long[])} instead.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003254 * @see Notification#vibrate
Joe Onoratocb109a02011-01-18 17:57:41 -08003255 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05003256 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -08003257 public Builder setVibrate(long[] pattern) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003258 mN.vibrate = pattern;
Joe Onorato46439ce2010-11-19 13:56:21 -08003259 return this;
3260 }
3261
Joe Onoratocb109a02011-01-18 17:57:41 -08003262 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003263 * Set the desired color for the indicator LED on the device, as well as the
3264 * blink duty cycle (specified in milliseconds).
3265 *
3266
3267 * Not all devices will honor all (or even any) of these values.
3268 *
Julia Reynolds529e3322017-02-06 08:33:01 -05003269 * @deprecated use {@link NotificationChannel#enableLights(boolean)} instead.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003270 * @see Notification#ledARGB
3271 * @see Notification#ledOnMS
3272 * @see Notification#ledOffMS
Joe Onoratocb109a02011-01-18 17:57:41 -08003273 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05003274 @Deprecated
Tor Norbye80756e32015-03-02 09:39:27 -08003275 public Builder setLights(@ColorInt int argb, int onMs, int offMs) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003276 mN.ledARGB = argb;
3277 mN.ledOnMS = onMs;
3278 mN.ledOffMS = offMs;
Julia Reynolds10ee1fc2015-11-09 11:04:55 -05003279 if (onMs != 0 || offMs != 0) {
3280 mN.flags |= FLAG_SHOW_LIGHTS;
3281 }
Joe Onorato46439ce2010-11-19 13:56:21 -08003282 return this;
3283 }
3284
Joe Onoratocb109a02011-01-18 17:57:41 -08003285 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003286 * Set whether this is an "ongoing" notification.
Joe Onoratocb109a02011-01-18 17:57:41 -08003287 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003288
3289 * Ongoing notifications cannot be dismissed by the user, so your application or service
3290 * must take care of canceling them.
3291 *
3292
3293 * They are typically used to indicate a background task that the user is actively engaged
3294 * with (e.g., playing music) or is pending in some way and therefore occupying the device
3295 * (e.g., a file download, sync operation, active network connection).
3296 *
3297
3298 * @see Notification#FLAG_ONGOING_EVENT
3299 * @see Service#setForeground(boolean)
Joe Onoratocb109a02011-01-18 17:57:41 -08003300 */
Joe Onorato46439ce2010-11-19 13:56:21 -08003301 public Builder setOngoing(boolean ongoing) {
3302 setFlag(FLAG_ONGOING_EVENT, ongoing);
3303 return this;
3304 }
3305
Joe Onoratocb109a02011-01-18 17:57:41 -08003306 /**
Selim Cinek7b9605b2017-01-19 17:36:00 -08003307 * Set whether this notification should be colorized. When set, the color set with
3308 * {@link #setColor(int)} will be used as the background color of this notification.
3309 * <p>
Selim Cinek7b9605b2017-01-19 17:36:00 -08003310 * This should only be used for high priority ongoing tasks like navigation, an ongoing
3311 * call, or other similarly high-priority events for the user.
Selim Cinek99104832017-01-25 14:47:33 -08003312 * <p>
Selim Cinek22714f12017-04-13 16:23:53 -07003313 * For most styles, the coloring will only be applied if the notification is for a
3314 * foreground service notification.
Selim Cinek99104832017-01-25 14:47:33 -08003315 * However, for {@link MediaStyle} and {@link DecoratedMediaCustomViewStyle} notifications
Selim Cinek22714f12017-04-13 16:23:53 -07003316 * that have a media session attached there is no such requirement.
Selim Cinek7b9605b2017-01-19 17:36:00 -08003317 *
Selim Cinek7b9605b2017-01-19 17:36:00 -08003318 * @see Builder#setColor(int)
Selim Cinek99104832017-01-25 14:47:33 -08003319 * @see MediaStyle#setMediaSession(MediaSession.Token)
Selim Cinek7b9605b2017-01-19 17:36:00 -08003320 */
3321 public Builder setColorized(boolean colorize) {
3322 mN.extras.putBoolean(EXTRA_COLORIZED, colorize);
3323 return this;
3324 }
3325
3326 /**
Joe Onoratocb109a02011-01-18 17:57:41 -08003327 * Set this flag if you would only like the sound, vibrate
3328 * and ticker to be played if the notification is not already showing.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003329 *
3330 * @see Notification#FLAG_ONLY_ALERT_ONCE
Joe Onoratocb109a02011-01-18 17:57:41 -08003331 */
Joe Onorato46439ce2010-11-19 13:56:21 -08003332 public Builder setOnlyAlertOnce(boolean onlyAlertOnce) {
3333 setFlag(FLAG_ONLY_ALERT_ONCE, onlyAlertOnce);
3334 return this;
3335 }
3336
Joe Onoratocb109a02011-01-18 17:57:41 -08003337 /**
Julia Reynolds04499532016-09-13 14:04:53 -04003338 * Make this notification automatically dismissed when the user touches it.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003339 *
3340 * @see Notification#FLAG_AUTO_CANCEL
Joe Onoratocb109a02011-01-18 17:57:41 -08003341 */
Joe Onorato46439ce2010-11-19 13:56:21 -08003342 public Builder setAutoCancel(boolean autoCancel) {
Joe Onorato281d83f2011-01-04 17:13:10 -08003343 setFlag(FLAG_AUTO_CANCEL, autoCancel);
Joe Onorato46439ce2010-11-19 13:56:21 -08003344 return this;
3345 }
3346
Joe Onoratocb109a02011-01-18 17:57:41 -08003347 /**
Griff Hazendfcb0802014-02-11 12:00:00 -08003348 * Set whether or not this notification should not bridge to other devices.
3349 *
3350 * <p>Some notifications can be bridged to other devices for remote display.
3351 * This hint can be set to recommend this notification not be bridged.
3352 */
3353 public Builder setLocalOnly(boolean localOnly) {
3354 setFlag(FLAG_LOCAL_ONLY, localOnly);
3355 return this;
3356 }
3357
3358 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003359 * Set which notification properties will be inherited from system defaults.
Joe Onoratocb109a02011-01-18 17:57:41 -08003360 * <p>
3361 * The value should be one or more of the following fields combined with
3362 * bitwise-or:
3363 * {@link #DEFAULT_SOUND}, {@link #DEFAULT_VIBRATE}, {@link #DEFAULT_LIGHTS}.
3364 * <p>
3365 * For all default values, use {@link #DEFAULT_ALL}.
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05003366 *
3367 * @deprecated use {@link NotificationChannel#enableVibration(boolean)} and
Julia Reynolds529e3322017-02-06 08:33:01 -05003368 * {@link NotificationChannel#enableLights(boolean)} and
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05003369 * {@link NotificationChannel#setSound(Uri, AudioAttributes)} instead.
Joe Onoratocb109a02011-01-18 17:57:41 -08003370 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05003371 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -08003372 public Builder setDefaults(int defaults) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003373 mN.defaults = defaults;
Joe Onorato46439ce2010-11-19 13:56:21 -08003374 return this;
3375 }
3376
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003377 /**
3378 * Set the priority of this notification.
3379 *
3380 * @see Notification#priority
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05003381 * @deprecated use {@link NotificationChannel#setImportance(int)} instead.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003382 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05003383 @Deprecated
Tor Norbyed9273d62013-05-30 15:59:53 -07003384 public Builder setPriority(@Priority int pri) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003385 mN.priority = pri;
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003386 return this;
3387 }
Joe Malin8d40d042012-11-05 11:36:40 -08003388
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003389 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -04003390 * Set the notification category.
Joe Malin8d40d042012-11-05 11:36:40 -08003391 *
John Spurlockfd7f1e02014-03-18 16:41:57 -04003392 * @see Notification#category
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003393 */
John Spurlockfd7f1e02014-03-18 16:41:57 -04003394 public Builder setCategory(String category) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003395 mN.category = category;
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003396 return this;
3397 }
3398
3399 /**
Chris Wrendde75302014-03-26 17:24:15 -04003400 * Add a person that is relevant to this notification.
3401 *
Chris Wrene6c48932014-09-29 17:19:27 -04003402 * <P>
3403 * Depending on user preferences, this annotation may allow the notification to pass
Julia Reynoldse071abd2017-03-22 10:52:11 -04003404 * through interruption filters, if this notification is of category {@link #CATEGORY_CALL}
3405 * or {@link #CATEGORY_MESSAGE}. The addition of people may also cause this notification to
3406 * appear more prominently in the user interface.
Chris Wrene6c48932014-09-29 17:19:27 -04003407 * </P>
3408 *
3409 * <P>
3410 * The person should be specified by the {@code String} representation of a
3411 * {@link android.provider.ContactsContract.Contacts#CONTENT_LOOKUP_URI}.
3412 * </P>
3413 *
3414 * <P>The system will also attempt to resolve {@code mailto:} and {@code tel:} schema
3415 * URIs. The path part of these URIs must exist in the contacts database, in the
3416 * appropriate column, or the reference will be discarded as invalid. Telephone schema
3417 * URIs will be resolved by {@link android.provider.ContactsContract.PhoneLookup}.
3418 * </P>
3419 *
3420 * @param uri A URI for the person.
Chris Wrendde75302014-03-26 17:24:15 -04003421 * @see Notification#EXTRA_PEOPLE
3422 */
Chris Wrene6c48932014-09-29 17:19:27 -04003423 public Builder addPerson(String uri) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003424 mPersonList.add(uri);
Chris Wrendde75302014-03-26 17:24:15 -04003425 return this;
3426 }
3427
3428 /**
Griff Hazen5cadc3b2014-05-20 09:55:39 -07003429 * Set this notification to be part of a group of notifications sharing the same key.
3430 * Grouped notifications may display in a cluster or stack on devices which
3431 * support such rendering.
3432 *
3433 * <p>To make this notification the summary for its group, also call
3434 * {@link #setGroupSummary}. A sort order can be specified for group members by using
3435 * {@link #setSortKey}.
3436 * @param groupKey The group key of the group.
3437 * @return this object for method chaining
3438 */
3439 public Builder setGroup(String groupKey) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003440 mN.mGroupKey = groupKey;
Griff Hazen5cadc3b2014-05-20 09:55:39 -07003441 return this;
3442 }
3443
3444 /**
3445 * Set this notification to be the group summary for a group of notifications.
3446 * Grouped notifications may display in a cluster or stack on devices which
Julia Reynolds04499532016-09-13 14:04:53 -04003447 * support such rendering. If thereRequires a group key also be set using {@link #setGroup}.
3448 * The group summary may be suppressed if too few notifications are included in the group.
Griff Hazen5cadc3b2014-05-20 09:55:39 -07003449 * @param isGroupSummary Whether this notification should be a group summary.
3450 * @return this object for method chaining
3451 */
3452 public Builder setGroupSummary(boolean isGroupSummary) {
3453 setFlag(FLAG_GROUP_SUMMARY, isGroupSummary);
3454 return this;
3455 }
3456
3457 /**
3458 * Set a sort key that orders this notification among other notifications from the
3459 * same package. This can be useful if an external sort was already applied and an app
3460 * would like to preserve this. Notifications will be sorted lexicographically using this
3461 * value, although providing different priorities in addition to providing sort key may
3462 * cause this value to be ignored.
3463 *
3464 * <p>This sort key can also be used to order members of a notification group. See
Griff Hazen9e1379f2014-05-20 12:50:51 -07003465 * {@link #setGroup}.
Griff Hazen5cadc3b2014-05-20 09:55:39 -07003466 *
3467 * @see String#compareTo(String)
3468 */
3469 public Builder setSortKey(String sortKey) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003470 mN.mSortKey = sortKey;
Griff Hazen5cadc3b2014-05-20 09:55:39 -07003471 return this;
3472 }
3473
3474 /**
Griff Hazen720042b2014-02-24 15:46:56 -08003475 * Merge additional metadata into this notification.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003476 *
Griff Hazen720042b2014-02-24 15:46:56 -08003477 * <p>Values within the Bundle will replace existing extras values in this Builder.
3478 *
3479 * @see Notification#extras
3480 */
Griff Hazen959591e2014-05-15 22:26:18 -07003481 public Builder addExtras(Bundle extras) {
3482 if (extras != null) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003483 mUserExtras.putAll(extras);
Griff Hazen720042b2014-02-24 15:46:56 -08003484 }
3485 return this;
3486 }
3487
3488 /**
3489 * Set metadata for this notification.
3490 *
3491 * <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 -04003492 * current contents are copied into the Notification each time {@link #build()} is
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003493 * called.
3494 *
Griff Hazen720042b2014-02-24 15:46:56 -08003495 * <p>Replaces any existing extras values with those from the provided Bundle.
3496 * Use {@link #addExtras} to merge in metadata instead.
3497 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003498 * @see Notification#extras
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003499 */
Griff Hazen959591e2014-05-15 22:26:18 -07003500 public Builder setExtras(Bundle extras) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003501 if (extras != null) {
3502 mUserExtras = extras;
3503 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003504 return this;
3505 }
3506
Daniel Sandlera0a938c2012-03-15 08:42:37 -04003507 /**
Griff Hazen720042b2014-02-24 15:46:56 -08003508 * Get the current metadata Bundle used by this notification Builder.
3509 *
3510 * <p>The returned Bundle is shared with this Builder.
3511 *
3512 * <p>The current contents of this Bundle are copied into the Notification each time
3513 * {@link #build()} is called.
3514 *
3515 * @see Notification#extras
3516 */
3517 public Bundle getExtras() {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003518 return mUserExtras;
3519 }
3520
3521 private Bundle getAllExtras() {
3522 final Bundle saveExtras = (Bundle) mUserExtras.clone();
3523 saveExtras.putAll(mN.extras);
3524 return saveExtras;
Griff Hazen720042b2014-02-24 15:46:56 -08003525 }
3526
3527 /**
Daniel Sandlera0a938c2012-03-15 08:42:37 -04003528 * Add an action to this notification. Actions are typically displayed by
3529 * the system as a button adjacent to the notification content.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04003530 * <p>
3531 * Every action must have an icon (32dp square and matching the
3532 * <a href="{@docRoot}design/style/iconography.html#action-bar">Holo
3533 * Dark action bar</a> visual style), a textual label, and a {@link PendingIntent}.
3534 * <p>
3535 * A notification in its expanded form can display up to 3 actions, from left to right in
3536 * the order they were added. Actions will not be displayed when the notification is
3537 * collapsed, however, so be sure that any essential functions may be accessed by the user
3538 * in some other way (for example, in the Activity pointed to by {@link #contentIntent}).
Daniel Sandlera0a938c2012-03-15 08:42:37 -04003539 *
3540 * @param icon Resource ID of a drawable that represents the action.
3541 * @param title Text describing the action.
3542 * @param intent PendingIntent to be fired when the action is invoked.
Dan Sandler86647982015-05-13 23:41:13 -04003543 *
3544 * @deprecated Use {@link #addAction(Action)} instead.
Daniel Sandlera0a938c2012-03-15 08:42:37 -04003545 */
Dan Sandler86647982015-05-13 23:41:13 -04003546 @Deprecated
Daniel Sandlera0a938c2012-03-15 08:42:37 -04003547 public Builder addAction(int icon, CharSequence title, PendingIntent intent) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04003548 mActions.add(new Action(icon, safeCharSequence(title), intent));
Daniel Sandlera0a938c2012-03-15 08:42:37 -04003549 return this;
3550 }
3551
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003552 /**
Griff Hazen959591e2014-05-15 22:26:18 -07003553 * Add an action to this notification. Actions are typically displayed by
3554 * the system as a button adjacent to the notification content.
3555 * <p>
3556 * Every action must have an icon (32dp square and matching the
3557 * <a href="{@docRoot}design/style/iconography.html#action-bar">Holo
3558 * Dark action bar</a> visual style), a textual label, and a {@link PendingIntent}.
3559 * <p>
3560 * A notification in its expanded form can display up to 3 actions, from left to right in
3561 * the order they were added. Actions will not be displayed when the notification is
3562 * collapsed, however, so be sure that any essential functions may be accessed by the user
3563 * in some other way (for example, in the Activity pointed to by {@link #contentIntent}).
3564 *
3565 * @param action The action to add.
3566 */
3567 public Builder addAction(Action action) {
liangweikang63b03b52017-03-16 19:22:15 +08003568 if (action != null) {
3569 mActions.add(action);
3570 }
Griff Hazen959591e2014-05-15 22:26:18 -07003571 return this;
3572 }
3573
3574 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003575 * Alter the complete list of actions attached to this notification.
3576 * @see #addAction(Action).
3577 *
3578 * @param actions
3579 * @return
3580 */
3581 public Builder setActions(Action... actions) {
3582 mActions.clear();
3583 for (int i = 0; i < actions.length; i++) {
liangweikang63b03b52017-03-16 19:22:15 +08003584 if (actions[i] != null) {
3585 mActions.add(actions[i]);
3586 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003587 }
3588 return this;
3589 }
3590
3591 /**
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003592 * Add a rich notification style to be applied at build time.
3593 *
3594 * @param style Object responsible for modifying the notification style.
3595 */
3596 public Builder setStyle(Style style) {
3597 if (mStyle != style) {
3598 mStyle = style;
Daniel Sandlerc08dea22012-06-28 08:35:24 -07003599 if (mStyle != null) {
3600 mStyle.setBuilder(this);
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003601 mN.extras.putString(EXTRA_TEMPLATE, style.getClass().getName());
3602 } else {
3603 mN.extras.remove(EXTRA_TEMPLATE);
Daniel Sandlerc08dea22012-06-28 08:35:24 -07003604 }
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003605 }
3606 return this;
3607 }
3608
Dan Sandler0bf2ed82013-12-21 23:33:41 -06003609 /**
3610 * Specify the value of {@link #visibility}.
Griff Hazenb720abe2014-05-20 13:15:30 -07003611 *
Dan Sandler0bf2ed82013-12-21 23:33:41 -06003612 * @return The same Builder.
3613 */
Jeff Sharkey30e06bb2017-04-24 11:18:03 -06003614 public Builder setVisibility(@Visibility int visibility) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003615 mN.visibility = visibility;
Dan Sandler0bf2ed82013-12-21 23:33:41 -06003616 return this;
3617 }
3618
3619 /**
3620 * Supply a replacement Notification whose contents should be shown in insecure contexts
3621 * (i.e. atop the secure lockscreen). See {@link #visibility} and {@link #VISIBILITY_PUBLIC}.
3622 * @param n A replacement notification, presumably with some or all info redacted.
3623 * @return The same Builder.
3624 */
3625 public Builder setPublicVersion(Notification n) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003626 if (n != null) {
3627 mN.publicVersion = new Notification();
3628 n.cloneInto(mN.publicVersion, /*heavy=*/ true);
3629 } else {
3630 mN.publicVersion = null;
3631 }
Dan Sandler0bf2ed82013-12-21 23:33:41 -06003632 return this;
3633 }
3634
Griff Hazenb720abe2014-05-20 13:15:30 -07003635 /**
Griff Hazen5cadc3b2014-05-20 09:55:39 -07003636 * Apply an extender to this notification builder. Extenders may be used to add
3637 * metadata or change options on this builder.
3638 */
Griff Hazen61a9e862014-05-22 16:05:19 -07003639 public Builder extend(Extender extender) {
3640 extender.extend(this);
Griff Hazen5cadc3b2014-05-20 09:55:39 -07003641 return this;
3642 }
3643
Dan Sandler4e787062015-06-17 15:09:48 -04003644 /**
3645 * @hide
3646 */
Julia Reynoldse46bb372016-03-17 11:05:58 -04003647 public Builder setFlag(int mask, boolean value) {
Joe Onorato46439ce2010-11-19 13:56:21 -08003648 if (value) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003649 mN.flags |= mask;
Joe Onorato46439ce2010-11-19 13:56:21 -08003650 } else {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003651 mN.flags &= ~mask;
Joe Onorato46439ce2010-11-19 13:56:21 -08003652 }
Julia Reynoldse46bb372016-03-17 11:05:58 -04003653 return this;
Joe Onorato46439ce2010-11-19 13:56:21 -08003654 }
3655
Dan Sandler26e81cf2014-05-06 10:01:27 -04003656 /**
3657 * Sets {@link Notification#color}.
3658 *
3659 * @param argb The accent color to use
3660 *
3661 * @return The same Builder.
3662 */
Tor Norbye80756e32015-03-02 09:39:27 -08003663 public Builder setColor(@ColorInt int argb) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003664 mN.color = argb;
Julia Reynolds10ee1fc2015-11-09 11:04:55 -05003665 sanitizeColor();
Dan Sandler26e81cf2014-05-06 10:01:27 -04003666 return this;
3667 }
3668
Jorim Jaggid05aa3e2014-08-28 17:52:27 +02003669 private Drawable getProfileBadgeDrawable() {
Chris Wren66619a22016-05-12 16:42:37 -04003670 if (mContext.getUserId() == UserHandle.USER_SYSTEM) {
3671 // This user can never be a badged profile,
3672 // and also includes USER_ALL system notifications.
3673 return null;
3674 }
Christoph Studer7ac80e62014-08-04 16:01:57 +02003675 // Note: This assumes that the current user can read the profile badge of the
3676 // originating user.
Selim Cineke6ff9462016-01-15 15:07:06 -08003677 return mContext.getPackageManager().getUserBadgeForDensityNoBackground(
Julia Reynoldsda303542015-11-23 14:00:20 -05003678 new UserHandle(mContext.getUserId()), 0);
Jorim Jaggid05aa3e2014-08-28 17:52:27 +02003679 }
3680
3681 private Bitmap getProfileBadge() {
3682 Drawable badge = getProfileBadgeDrawable();
Kenny Guy8a0101b2014-05-08 23:34:12 +01003683 if (badge == null) {
3684 return null;
3685 }
Jorim Jaggid05aa3e2014-08-28 17:52:27 +02003686 final int size = mContext.getResources().getDimensionPixelSize(
3687 R.dimen.notification_badge_size);
3688 Bitmap bitmap = Bitmap.createBitmap(size, size, Bitmap.Config.ARGB_8888);
Kenny Guy8a0101b2014-05-08 23:34:12 +01003689 Canvas canvas = new Canvas(bitmap);
Jorim Jaggid05aa3e2014-08-28 17:52:27 +02003690 badge.setBounds(0, 0, size, size);
Kenny Guy8a0101b2014-05-08 23:34:12 +01003691 badge.draw(canvas);
3692 return bitmap;
3693 }
3694
Selim Cinekc848c3a2016-01-13 15:27:30 -08003695 private void bindProfileBadge(RemoteViews contentView) {
Kenny Guy98193ea2014-07-24 19:54:37 +01003696 Bitmap profileBadge = getProfileBadge();
3697
Kenny Guy98193ea2014-07-24 19:54:37 +01003698 if (profileBadge != null) {
Selim Cinekc848c3a2016-01-13 15:27:30 -08003699 contentView.setImageViewBitmap(R.id.profile_badge, profileBadge);
3700 contentView.setViewVisibility(R.id.profile_badge, View.VISIBLE);
Selim Cinek7b9605b2017-01-19 17:36:00 -08003701 if (isColorized()) {
3702 contentView.setDrawableParameters(R.id.profile_badge, false, -1,
3703 getPrimaryTextColor(), PorterDuff.Mode.SRC_ATOP, -1);
3704 }
Kenny Guy98193ea2014-07-24 19:54:37 +01003705 }
Kenny Guy98193ea2014-07-24 19:54:37 +01003706 }
3707
Christoph Studerfe718432014-09-01 18:21:18 +02003708 private void resetStandardTemplate(RemoteViews contentView) {
Selim Cinekeaa29ca2015-11-23 13:51:13 -08003709 resetNotificationHeader(contentView);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003710 resetContentMargins(contentView);
Christoph Studerfe718432014-09-01 18:21:18 +02003711 contentView.setViewVisibility(R.id.right_icon, View.GONE);
Selim Cinek860b6da2015-12-16 19:02:19 -08003712 contentView.setViewVisibility(R.id.title, View.GONE);
Christoph Studerfe718432014-09-01 18:21:18 +02003713 contentView.setTextViewText(R.id.title, null);
Selim Cinek41598732016-01-11 16:58:37 -08003714 contentView.setViewVisibility(R.id.text, View.GONE);
Christoph Studerfe718432014-09-01 18:21:18 +02003715 contentView.setTextViewText(R.id.text, null);
Selim Cinek29603462015-11-17 19:04:39 -08003716 contentView.setViewVisibility(R.id.text_line_1, View.GONE);
Selim Cinek41598732016-01-11 16:58:37 -08003717 contentView.setTextViewText(R.id.text_line_1, null);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003718 }
3719
Selim Cinekeaa29ca2015-11-23 13:51:13 -08003720 /**
3721 * Resets the notification header to its original state
3722 */
3723 private void resetNotificationHeader(RemoteViews contentView) {
Adrian Roosc4337a32016-08-02 18:30:34 -07003724 // Small icon doesn't need to be reset, as it's always set. Resetting would prevent
3725 // re-using the drawable when the notification is updated.
Selim Cinek7b836392015-12-04 20:02:59 -08003726 contentView.setBoolean(R.id.notification_header, "setExpanded", false);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003727 contentView.setTextViewText(R.id.app_name_text, null);
Christoph Studerca1db712014-09-10 17:31:33 +02003728 contentView.setViewVisibility(R.id.chronometer, View.GONE);
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07003729 contentView.setViewVisibility(R.id.header_text, View.GONE);
Adrian Roos9dfb78f2016-06-30 15:43:44 -07003730 contentView.setTextViewText(R.id.header_text, null);
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07003731 contentView.setViewVisibility(R.id.header_text_divider, View.GONE);
Selim Cinek29603462015-11-17 19:04:39 -08003732 contentView.setViewVisibility(R.id.time_divider, View.GONE);
Selim Cinekb85f36fd2016-04-20 18:46:36 -07003733 contentView.setViewVisibility(R.id.time, View.GONE);
Selim Cinekc848c3a2016-01-13 15:27:30 -08003734 contentView.setImageViewIcon(R.id.profile_badge, null);
3735 contentView.setViewVisibility(R.id.profile_badge, View.GONE);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003736 }
3737
3738 private void resetContentMargins(RemoteViews contentView) {
Adrian Roos2d5dbba2016-06-08 17:11:53 -07003739 contentView.setViewLayoutMarginEndDimen(R.id.line1, 0);
3740 contentView.setViewLayoutMarginEndDimen(R.id.text, 0);
Christoph Studerfe718432014-09-01 18:21:18 +02003741 }
3742
Jorim Jaggi445d3c02014-08-19 22:33:42 +02003743 private RemoteViews applyStandardTemplate(int resId) {
Adrian Roos70d7aa32017-01-11 15:39:06 -08003744 return applyStandardTemplate(resId, mParams.reset().fillTextsFrom(this));
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02003745 }
3746
3747 /**
3748 * @param hasProgress whether the progress bar should be shown and set
3749 */
3750 private RemoteViews applyStandardTemplate(int resId, boolean hasProgress) {
Adrian Roos70d7aa32017-01-11 15:39:06 -08003751 return applyStandardTemplate(resId, mParams.reset().hasProgress(hasProgress)
3752 .fillTextsFrom(this));
Adrian Roosc1a80b02016-04-05 14:54:55 -07003753 }
3754
Adrian Roos70d7aa32017-01-11 15:39:06 -08003755 private RemoteViews applyStandardTemplate(int resId, StandardTemplateParams p) {
Kenny Guy77320062014-08-27 21:37:15 +01003756 RemoteViews contentView = new BuilderRemoteViews(mContext.getApplicationInfo(), resId);
Dan Sandler539aad42014-08-04 00:43:39 -04003757
Christoph Studerfe718432014-09-01 18:21:18 +02003758 resetStandardTemplate(contentView);
3759
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003760 final Bundle ex = mN.extras;
Selim Cinek7b9605b2017-01-19 17:36:00 -08003761 updateBackgroundColor(contentView);
Adrian Roos487374f2017-01-11 15:48:14 -08003762 bindNotificationHeader(contentView, p.ambient);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003763 bindLargeIcon(contentView);
Adrian Roos70d7aa32017-01-11 15:39:06 -08003764 boolean showProgress = handleProgressBar(p.hasProgress, contentView, ex);
3765 if (p.title != null) {
Selim Cinek860b6da2015-12-16 19:02:19 -08003766 contentView.setViewVisibility(R.id.title, View.VISIBLE);
Adrian Roos70d7aa32017-01-11 15:39:06 -08003767 contentView.setTextViewText(R.id.title, p.title);
Adrian Roos72171622017-01-27 10:32:06 -08003768 if (!p.ambient) {
3769 setTextViewColorPrimary(contentView, R.id.title);
3770 }
Selim Cinek954cc232016-05-20 13:29:23 -07003771 contentView.setViewLayoutWidth(R.id.title, showProgress
3772 ? ViewGroup.LayoutParams.WRAP_CONTENT
3773 : ViewGroup.LayoutParams.MATCH_PARENT);
Joe Onorato561d3852010-11-20 18:09:34 -08003774 }
Adrian Roos70d7aa32017-01-11 15:39:06 -08003775 if (p.text != null) {
Selim Cinek41598732016-01-11 16:58:37 -08003776 int textId = showProgress ? com.android.internal.R.id.text_line_1
3777 : com.android.internal.R.id.text;
Adrian Roos70d7aa32017-01-11 15:39:06 -08003778 contentView.setTextViewText(textId, p.text);
Adrian Roos72171622017-01-27 10:32:06 -08003779 if (!p.ambient) {
3780 setTextViewColorSecondary(contentView, textId);
3781 }
Selim Cinek41598732016-01-11 16:58:37 -08003782 contentView.setViewVisibility(textId, View.VISIBLE);
Joe Onorato561d3852010-11-20 18:09:34 -08003783 }
Selim Cinekc848c3a2016-01-13 15:27:30 -08003784
Selim Cinek279fa862016-06-14 10:57:25 -07003785 setContentMinHeight(contentView, showProgress || mN.hasLargeIcon());
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003786
Selim Cinek29603462015-11-17 19:04:39 -08003787 return contentView;
3788 }
3789
Selim Cinek7b9605b2017-01-19 17:36:00 -08003790 private void setTextViewColorPrimary(RemoteViews contentView, int id) {
3791 ensureColors();
3792 contentView.setTextColor(id, mPrimaryTextColor);
3793 }
3794
3795 private int getPrimaryTextColor() {
3796 ensureColors();
3797 return mPrimaryTextColor;
3798 }
3799
3800 private int getActionBarColor() {
3801 ensureColors();
3802 return mActionBarColor;
3803 }
3804
Selim Cinek622c64a2017-04-17 17:10:05 -07003805 private int getActionBarColorDeEmphasized() {
3806 int backgroundColor = getBackgroundColor();
3807 return NotificationColorUtil.getShiftedColor(backgroundColor, 12);
3808 }
3809
Selim Cinek7b9605b2017-01-19 17:36:00 -08003810 private void setTextViewColorSecondary(RemoteViews contentView, int id) {
3811 ensureColors();
3812 contentView.setTextColor(id, mSecondaryTextColor);
3813 }
3814
3815 private void ensureColors() {
3816 int backgroundColor = getBackgroundColor();
3817 if (mPrimaryTextColor == COLOR_INVALID
3818 || mSecondaryTextColor == COLOR_INVALID
3819 || mActionBarColor == COLOR_INVALID
3820 || mTextColorsAreForBackground != backgroundColor) {
3821 mTextColorsAreForBackground = backgroundColor;
3822 mPrimaryTextColor = NotificationColorUtil.resolvePrimaryColor(
3823 mContext, backgroundColor);
3824 mSecondaryTextColor = NotificationColorUtil.resolveSecondaryColor(
3825 mContext, backgroundColor);
Selim Cinek875ba9b2017-02-13 16:20:17 -08003826 mActionBarColor = NotificationColorUtil.resolveActionBarColor(mContext,
3827 backgroundColor);
Selim Cinek7b9605b2017-01-19 17:36:00 -08003828 }
3829 }
3830
3831 private void updateBackgroundColor(RemoteViews contentView) {
3832 if (isColorized()) {
3833 contentView.setInt(R.id.status_bar_latest_event_content, "setBackgroundColor",
3834 getBackgroundColor());
3835 } else {
3836 // Clear it!
3837 contentView.setInt(R.id.status_bar_latest_event_content, "setBackgroundResource",
3838 0);
3839 }
3840 }
3841
Selim Cinek860b6da2015-12-16 19:02:19 -08003842 /**
3843 * @param remoteView the remote view to update the minheight in
3844 * @param hasMinHeight does it have a mimHeight
3845 * @hide
3846 */
3847 void setContentMinHeight(RemoteViews remoteView, boolean hasMinHeight) {
3848 int minHeight = 0;
3849 if (hasMinHeight) {
3850 // we need to set the minHeight of the notification
3851 minHeight = mContext.getResources().getDimensionPixelSize(
3852 com.android.internal.R.dimen.notification_min_content_height);
3853 }
3854 remoteView.setInt(R.id.notification_main_column, "setMinimumHeight", minHeight);
3855 }
3856
Selim Cinek29603462015-11-17 19:04:39 -08003857 private boolean handleProgressBar(boolean hasProgress, RemoteViews contentView, Bundle ex) {
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003858 final int max = ex.getInt(EXTRA_PROGRESS_MAX, 0);
3859 final int progress = ex.getInt(EXTRA_PROGRESS, 0);
3860 final boolean ind = ex.getBoolean(EXTRA_PROGRESS_INDETERMINATE);
3861 if (hasProgress && (max != 0 || ind)) {
Selim Cinek29603462015-11-17 19:04:39 -08003862 contentView.setViewVisibility(com.android.internal.R.id.progress, View.VISIBLE);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003863 contentView.setProgressBar(
Selim Cinek29603462015-11-17 19:04:39 -08003864 R.id.progress, max, progress, ind);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003865 contentView.setProgressBackgroundTintList(
3866 R.id.progress, ColorStateList.valueOf(mContext.getColor(
3867 R.color.notification_progress_background_color)));
Selim Cinek29603462015-11-17 19:04:39 -08003868 if (mN.color != COLOR_DEFAULT) {
Adrian Roos4ff3b122016-02-01 12:26:13 -08003869 ColorStateList colorStateList = ColorStateList.valueOf(resolveContrastColor());
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003870 contentView.setProgressTintList(R.id.progress, colorStateList);
3871 contentView.setProgressIndeterminateTintList(R.id.progress, colorStateList);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003872 }
Selim Cinek29603462015-11-17 19:04:39 -08003873 return true;
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003874 } else {
3875 contentView.setViewVisibility(R.id.progress, View.GONE);
Selim Cinek29603462015-11-17 19:04:39 -08003876 return false;
Jeff Sharkey1c400132011-08-05 14:50:13 -07003877 }
Joe Onorato561d3852010-11-20 18:09:34 -08003878 }
3879
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003880 private void bindLargeIcon(RemoteViews contentView) {
Selim Cinek279fa862016-06-14 10:57:25 -07003881 if (mN.mLargeIcon == null && mN.largeIcon != null) {
3882 mN.mLargeIcon = Icon.createWithBitmap(mN.largeIcon);
3883 }
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003884 if (mN.mLargeIcon != null) {
3885 contentView.setViewVisibility(R.id.right_icon, View.VISIBLE);
3886 contentView.setImageViewIcon(R.id.right_icon, mN.mLargeIcon);
3887 processLargeLegacyIcon(mN.mLargeIcon, contentView);
Adrian Roos2d5dbba2016-06-08 17:11:53 -07003888 int endMargin = R.dimen.notification_content_picture_margin;
3889 contentView.setViewLayoutMarginEndDimen(R.id.line1, endMargin);
3890 contentView.setViewLayoutMarginEndDimen(R.id.text, endMargin);
3891 contentView.setViewLayoutMarginEndDimen(R.id.progress, endMargin);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003892 }
3893 }
3894
Adrian Roos487374f2017-01-11 15:48:14 -08003895 private void bindNotificationHeader(RemoteViews contentView, boolean ambient) {
3896 bindSmallIcon(contentView, ambient);
3897 bindHeaderAppName(contentView, ambient);
3898 if (!ambient) {
3899 // Ambient view does not have these
3900 bindHeaderText(contentView);
3901 bindHeaderChronometerAndTime(contentView);
Adrian Roos487374f2017-01-11 15:48:14 -08003902 bindProfileBadge(contentView);
3903 }
Adrian Roosd83e9992017-03-16 15:17:57 -07003904 bindExpandButton(contentView);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003905 }
3906
3907 private void bindExpandButton(RemoteViews contentView) {
Selim Cinek99104832017-01-25 14:47:33 -08003908 int color = getPrimaryHighlightColor();
Selim Cinek7b9605b2017-01-19 17:36:00 -08003909 contentView.setDrawableParameters(R.id.expand_button, false, -1, color,
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003910 PorterDuff.Mode.SRC_ATOP, -1);
Selim Cinekea4bef72015-12-02 15:51:10 -08003911 contentView.setInt(R.id.notification_header, "setOriginalNotificationColor",
Selim Cinek7b9605b2017-01-19 17:36:00 -08003912 color);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003913 }
3914
Selim Cinek99104832017-01-25 14:47:33 -08003915 /**
3916 * @return the color that is used as the first primary highlight color. This is applied
3917 * in several places like the action buttons or the app name in the header.
3918 */
3919 private int getPrimaryHighlightColor() {
3920 return isColorized() ? getPrimaryTextColor() : resolveContrastColor();
3921 }
3922
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003923 private void bindHeaderChronometerAndTime(RemoteViews contentView) {
3924 if (showsTimeOrChronometer()) {
Selim Cinek29603462015-11-17 19:04:39 -08003925 contentView.setViewVisibility(R.id.time_divider, View.VISIBLE);
Selim Cinek7b9605b2017-01-19 17:36:00 -08003926 setTextViewColorSecondary(contentView, R.id.time_divider);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003927 if (mN.extras.getBoolean(EXTRA_SHOW_CHRONOMETER)) {
3928 contentView.setViewVisibility(R.id.chronometer, View.VISIBLE);
3929 contentView.setLong(R.id.chronometer, "setBase",
3930 mN.when + (SystemClock.elapsedRealtime() - System.currentTimeMillis()));
3931 contentView.setBoolean(R.id.chronometer, "setStarted", true);
Adrian Roos96b7e202016-05-17 13:50:38 -07003932 boolean countsDown = mN.extras.getBoolean(EXTRA_CHRONOMETER_COUNT_DOWN);
Selim Cinekc3b752e2016-04-20 16:13:59 -07003933 contentView.setChronometerCountDown(R.id.chronometer, countsDown);
Selim Cinek7b9605b2017-01-19 17:36:00 -08003934 setTextViewColorSecondary(contentView, R.id.chronometer);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003935 } else {
3936 contentView.setViewVisibility(R.id.time, View.VISIBLE);
3937 contentView.setLong(R.id.time, "setTime", mN.when);
Selim Cinek7b9605b2017-01-19 17:36:00 -08003938 setTextViewColorSecondary(contentView, R.id.time);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003939 }
Selim Cinekb85f36fd2016-04-20 18:46:36 -07003940 } else {
3941 // We still want a time to be set but gone, such that we can show and hide it
3942 // on demand in case it's a child notification without anything in the header
3943 contentView.setLong(R.id.time, "setTime", mN.when != 0 ? mN.when : mN.creationTime);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003944 }
3945 }
3946
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07003947 private void bindHeaderText(RemoteViews contentView) {
3948 CharSequence headerText = mN.extras.getCharSequence(EXTRA_SUB_TEXT);
3949 if (headerText == null && mStyle != null && mStyle.mSummaryTextSet
Selim Cinek03d0d652015-11-13 13:18:09 -05003950 && mStyle.hasSummaryInHeader()) {
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07003951 headerText = mStyle.mSummaryText;
Selim Cinek03d0d652015-11-13 13:18:09 -05003952 }
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07003953 if (headerText == null
3954 && mContext.getApplicationInfo().targetSdkVersion < Build.VERSION_CODES.N
3955 && mN.extras.getCharSequence(EXTRA_INFO_TEXT) != null) {
3956 headerText = mN.extras.getCharSequence(EXTRA_INFO_TEXT);
3957 }
3958 if (headerText != null) {
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003959 // TODO: Remove the span entirely to only have the string with propper formating.
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07003960 contentView.setTextViewText(R.id.header_text, processLegacyText(headerText));
Selim Cinek7b9605b2017-01-19 17:36:00 -08003961 setTextViewColorSecondary(contentView, R.id.header_text);
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07003962 contentView.setViewVisibility(R.id.header_text, View.VISIBLE);
3963 contentView.setViewVisibility(R.id.header_text_divider, View.VISIBLE);
Selim Cinek7b9605b2017-01-19 17:36:00 -08003964 setTextViewColorSecondary(contentView, R.id.header_text_divider);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003965 }
3966 }
3967
Adrian Rooseba05822016-04-22 17:09:27 -07003968 /**
3969 * @hide
3970 */
3971 public String loadHeaderAppName() {
Dan Sandler732bd6c2016-04-12 14:20:32 -04003972 CharSequence name = null;
3973 final PackageManager pm = mContext.getPackageManager();
3974 if (mN.extras.containsKey(EXTRA_SUBSTITUTE_APP_NAME)) {
3975 // only system packages which lump together a bunch of unrelated stuff
3976 // may substitute a different name to make the purpose of the
3977 // notification more clear. the correct package label should always
3978 // be accessible via SystemUI.
3979 final String pkg = mContext.getPackageName();
3980 final String subName = mN.extras.getString(EXTRA_SUBSTITUTE_APP_NAME);
3981 if (PackageManager.PERMISSION_GRANTED == pm.checkPermission(
3982 android.Manifest.permission.SUBSTITUTE_NOTIFICATION_APP_NAME, pkg)) {
3983 name = subName;
3984 } else {
3985 Log.w(TAG, "warning: pkg "
3986 + pkg + " attempting to substitute app name '" + subName
3987 + "' without holding perm "
3988 + android.Manifest.permission.SUBSTITUTE_NOTIFICATION_APP_NAME);
3989 }
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003990 }
Dan Sandler732bd6c2016-04-12 14:20:32 -04003991 if (TextUtils.isEmpty(name)) {
3992 name = pm.getApplicationLabel(mContext.getApplicationInfo());
3993 }
3994 if (TextUtils.isEmpty(name)) {
3995 // still nothing?
3996 return null;
3997 }
3998
3999 return String.valueOf(name);
4000 }
Adrian Roos487374f2017-01-11 15:48:14 -08004001 private void bindHeaderAppName(RemoteViews contentView, boolean ambient) {
Dan Sandler732bd6c2016-04-12 14:20:32 -04004002 contentView.setTextViewText(R.id.app_name_text, loadHeaderAppName());
Adrian Roos72171622017-01-27 10:32:06 -08004003 if (isColorized() && !ambient) {
Selim Cinek7b9605b2017-01-19 17:36:00 -08004004 setTextViewColorPrimary(contentView, R.id.app_name_text);
4005 } else {
4006 contentView.setTextColor(R.id.app_name_text,
4007 ambient ? resolveAmbientColor() : resolveContrastColor());
4008 }
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004009 }
4010
Adrian Roos487374f2017-01-11 15:48:14 -08004011 private void bindSmallIcon(RemoteViews contentView, boolean ambient) {
Selim Cinek279fa862016-06-14 10:57:25 -07004012 if (mN.mSmallIcon == null && mN.icon != 0) {
4013 mN.mSmallIcon = Icon.createWithResource(mContext, mN.icon);
4014 }
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004015 contentView.setImageViewIcon(R.id.icon, mN.mSmallIcon);
Adrian Roos9b45a152016-06-28 13:32:29 -07004016 contentView.setDrawableParameters(R.id.icon, false /* targetBackground */,
4017 -1 /* alpha */, -1 /* colorFilter */, null /* mode */, mN.iconLevel);
Adrian Roos487374f2017-01-11 15:48:14 -08004018 processSmallIconColor(mN.mSmallIcon, contentView, ambient);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004019 }
4020
Jorim Jaggi445d3c02014-08-19 22:33:42 +02004021 /**
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02004022 * @return true if the built notification will show the time or the chronometer; false
4023 * otherwise
4024 */
4025 private boolean showsTimeOrChronometer() {
Selim Cinekc2c0b042016-05-18 17:13:46 -07004026 return mN.showsTime() || mN.showsChronometer();
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02004027 }
4028
Christoph Studerfe718432014-09-01 18:21:18 +02004029 private void resetStandardTemplateWithActions(RemoteViews big) {
Adrian Roos4c1fcc82016-03-31 14:39:39 -07004030 // actions_container is only reset when there are no actions to avoid focus issues with
4031 // remote inputs.
Christoph Studerfe718432014-09-01 18:21:18 +02004032 big.setViewVisibility(R.id.actions, View.GONE);
Christoph Studerfe718432014-09-01 18:21:18 +02004033 big.removeAllViews(R.id.actions);
Adrian Roose458aa82015-12-08 16:17:19 -08004034
4035 big.setViewVisibility(R.id.notification_material_reply_container, View.GONE);
4036 big.setTextViewText(R.id.notification_material_reply_text_1, null);
4037
4038 big.setViewVisibility(R.id.notification_material_reply_text_2, View.GONE);
4039 big.setTextViewText(R.id.notification_material_reply_text_2, null);
4040 big.setViewVisibility(R.id.notification_material_reply_text_3, View.GONE);
4041 big.setTextViewText(R.id.notification_material_reply_text_3, null);
Adrian Roosf852a422016-06-03 13:33:43 -07004042
4043 big.setViewLayoutMarginBottomDimen(R.id.notification_action_list_margin_target, 0);
Christoph Studerfe718432014-09-01 18:21:18 +02004044 }
4045
Daniel Sandler96fd7c12012-03-30 16:37:36 -04004046 private RemoteViews applyStandardTemplateWithActions(int layoutId) {
Adrian Roos70d7aa32017-01-11 15:39:06 -08004047 return applyStandardTemplateWithActions(layoutId, mParams.reset().fillTextsFrom(this));
Adrian Roos48d746a2016-04-12 14:57:28 -07004048 }
4049
Adrian Roos70d7aa32017-01-11 15:39:06 -08004050 private RemoteViews applyStandardTemplateWithActions(int layoutId,
4051 StandardTemplateParams p) {
4052 RemoteViews big = applyStandardTemplate(layoutId, p);
Daniel Sandler96fd7c12012-03-30 16:37:36 -04004053
Christoph Studerfe718432014-09-01 18:21:18 +02004054 resetStandardTemplateWithActions(big);
4055
Adrian Roose458aa82015-12-08 16:17:19 -08004056 boolean validRemoteInput = false;
4057
Daniel Sandler96fd7c12012-03-30 16:37:36 -04004058 int N = mActions.size();
Adrian Roos487374f2017-01-11 15:48:14 -08004059 boolean emphazisedMode = mN.fullScreenIntent != null && !p.ambient;
Selim Cinek06e9e1f2016-07-08 17:14:16 -07004060 big.setBoolean(R.id.actions, "setEmphasizedMode", emphazisedMode);
Daniel Sandler96fd7c12012-03-30 16:37:36 -04004061 if (N > 0) {
Adrian Roos7052de52016-03-03 15:53:34 -08004062 big.setViewVisibility(R.id.actions_container, View.VISIBLE);
Daniel Sandler96fd7c12012-03-30 16:37:36 -04004063 big.setViewVisibility(R.id.actions, View.VISIBLE);
Adrian Roos487374f2017-01-11 15:48:14 -08004064 if (p.ambient) {
4065 big.setInt(R.id.actions, "setBackgroundColor", Color.TRANSPARENT);
Selim Cinek7b9605b2017-01-19 17:36:00 -08004066 } else if (isColorized()) {
4067 big.setInt(R.id.actions, "setBackgroundColor", getActionBarColor());
4068 } else {
4069 big.setInt(R.id.actions, "setBackgroundColor", mContext.getColor(
4070 R.color.notification_action_list));
Adrian Roos487374f2017-01-11 15:48:14 -08004071 }
Adrian Roosf852a422016-06-03 13:33:43 -07004072 big.setViewLayoutMarginBottomDimen(R.id.notification_action_list_margin_target,
4073 R.dimen.notification_action_list_height);
Daniel Sandler8680bf82012-05-15 16:52:52 -04004074 if (N>MAX_ACTION_BUTTONS) N=MAX_ACTION_BUTTONS;
Daniel Sandler96fd7c12012-03-30 16:37:36 -04004075 for (int i=0; i<N; i++) {
Adrian Roose458aa82015-12-08 16:17:19 -08004076 Action action = mActions.get(i);
4077 validRemoteInput |= hasValidRemoteInput(action);
4078
Selim Cinek06e9e1f2016-07-08 17:14:16 -07004079 final RemoteViews button = generateActionButton(action, emphazisedMode,
Adrian Roos487374f2017-01-11 15:48:14 -08004080 i % 2 != 0, p.ambient);
Daniel Sandler96fd7c12012-03-30 16:37:36 -04004081 big.addView(R.id.actions, button);
4082 }
Adrian Roos4c1fcc82016-03-31 14:39:39 -07004083 } else {
4084 big.setViewVisibility(R.id.actions_container, View.GONE);
Daniel Sandler96fd7c12012-03-30 16:37:36 -04004085 }
Adrian Roose458aa82015-12-08 16:17:19 -08004086
4087 CharSequence[] replyText = mN.extras.getCharSequenceArray(EXTRA_REMOTE_INPUT_HISTORY);
Adrian Roos487374f2017-01-11 15:48:14 -08004088 if (!p.ambient && validRemoteInput && replyText != null
Adrian Roose458aa82015-12-08 16:17:19 -08004089 && replyText.length > 0 && !TextUtils.isEmpty(replyText[0])) {
4090 big.setViewVisibility(R.id.notification_material_reply_container, View.VISIBLE);
4091 big.setTextViewText(R.id.notification_material_reply_text_1, replyText[0]);
Selim Cinek7b9605b2017-01-19 17:36:00 -08004092 setTextViewColorSecondary(big, R.id.notification_material_reply_text_1);
Adrian Roose458aa82015-12-08 16:17:19 -08004093
4094 if (replyText.length > 1 && !TextUtils.isEmpty(replyText[1])) {
4095 big.setViewVisibility(R.id.notification_material_reply_text_2, View.VISIBLE);
4096 big.setTextViewText(R.id.notification_material_reply_text_2, replyText[1]);
Selim Cinek7b9605b2017-01-19 17:36:00 -08004097 setTextViewColorSecondary(big, R.id.notification_material_reply_text_2);
Adrian Roose458aa82015-12-08 16:17:19 -08004098
4099 if (replyText.length > 2 && !TextUtils.isEmpty(replyText[2])) {
4100 big.setViewVisibility(
4101 R.id.notification_material_reply_text_3, View.VISIBLE);
4102 big.setTextViewText(R.id.notification_material_reply_text_3, replyText[2]);
Selim Cinek7b9605b2017-01-19 17:36:00 -08004103 setTextViewColorSecondary(big, R.id.notification_material_reply_text_3);
Adrian Roose458aa82015-12-08 16:17:19 -08004104 }
4105 }
4106 }
4107
Daniel Sandler96fd7c12012-03-30 16:37:36 -04004108 return big;
4109 }
4110
Adrian Roose458aa82015-12-08 16:17:19 -08004111 private boolean hasValidRemoteInput(Action action) {
4112 if (TextUtils.isEmpty(action.title) || action.actionIntent == null) {
4113 // Weird actions
4114 return false;
4115 }
4116
4117 RemoteInput[] remoteInputs = action.getRemoteInputs();
4118 if (remoteInputs == null) {
4119 return false;
4120 }
4121
4122 for (RemoteInput r : remoteInputs) {
4123 CharSequence[] choices = r.getChoices();
4124 if (r.getAllowFreeFormInput() || (choices != null && choices.length != 0)) {
4125 return true;
4126 }
4127 }
4128 return false;
4129 }
4130
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004131 /**
4132 * Construct a RemoteViews for the final 1U notification layout. In order:
4133 * 1. Custom contentView from the caller
4134 * 2. Style's proposed content view
4135 * 3. Standard template view
4136 */
Julia Reynolds3b848122016-02-26 10:45:32 -05004137 public RemoteViews createContentView() {
Selim Cinek7d1009b2017-01-25 15:28:28 -08004138 return createContentView(false /* increasedheight */ );
4139 }
4140
4141 /**
4142 * Construct a RemoteViews for the smaller content view.
4143 *
4144 * @param increasedHeight true if this layout be created with an increased height. Some
4145 * styles may support showing more then just that basic 1U size
4146 * and the system may decide to render important notifications
4147 * slightly bigger even when collapsed.
4148 *
4149 * @hide
4150 */
4151 public RemoteViews createContentView(boolean increasedHeight) {
Selim Cinek593610c2016-02-16 18:42:57 -08004152 if (mN.contentView != null && (mStyle == null || !mStyle.displayCustomViewInline())) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004153 return mN.contentView;
4154 } else if (mStyle != null) {
Selim Cinek7d1009b2017-01-25 15:28:28 -08004155 final RemoteViews styleView = mStyle.makeContentView(increasedHeight);
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004156 if (styleView != null) {
4157 return styleView;
4158 }
Joe Onorato46439ce2010-11-19 13:56:21 -08004159 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004160 return applyStandardTemplate(getBaseLayoutResource());
Joe Onorato46439ce2010-11-19 13:56:21 -08004161 }
4162
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004163 /**
4164 * Construct a RemoteViews for the final big notification layout.
4165 */
Julia Reynolds3b848122016-02-26 10:45:32 -05004166 public RemoteViews createBigContentView() {
Selim Cinek850a8542015-11-11 11:48:36 -05004167 RemoteViews result = null;
Selim Cinek593610c2016-02-16 18:42:57 -08004168 if (mN.bigContentView != null
4169 && (mStyle == null || !mStyle.displayCustomViewInline())) {
Julia Reynolds089e3e42015-11-18 09:59:57 -05004170 return mN.bigContentView;
4171 } else if (mStyle != null) {
Selim Cinek850a8542015-11-11 11:48:36 -05004172 result = mStyle.makeBigContentView();
Selim Cinek90dcf6d2015-11-18 20:24:13 -08004173 hideLine1Text(result);
Selim Cinekcc10bfb2016-02-10 16:24:21 -08004174 } else if (mActions.size() != 0) {
4175 result = applyStandardTemplateWithActions(getBigBaseLayoutResource());
Selim Cinek850a8542015-11-11 11:48:36 -05004176 }
Selim Cinek6743c0b2017-01-18 18:24:01 -08004177 makeHeaderExpanded(result);
Selim Cinek850a8542015-11-11 11:48:36 -05004178 return result;
4179 }
Daniel Sandler96fd7c12012-03-30 16:37:36 -04004180
Selim Cinekeaa29ca2015-11-23 13:51:13 -08004181 /**
Selim Cinek414ad332017-02-24 19:06:12 -08004182 * Construct a RemoteViews for the final notification header only. This will not be
4183 * colorized.
Selim Cinekeaa29ca2015-11-23 13:51:13 -08004184 *
4185 * @hide
4186 */
4187 public RemoteViews makeNotificationHeader() {
Selim Cinek414ad332017-02-24 19:06:12 -08004188 Boolean colorized = (Boolean) mN.extras.get(EXTRA_COLORIZED);
4189 mN.extras.putBoolean(EXTRA_COLORIZED, false);
Selim Cinekeaa29ca2015-11-23 13:51:13 -08004190 RemoteViews header = new BuilderRemoteViews(mContext.getApplicationInfo(),
4191 R.layout.notification_template_header);
4192 resetNotificationHeader(header);
Adrian Roos487374f2017-01-11 15:48:14 -08004193 bindNotificationHeader(header, false /* ambient */);
Selim Cinek414ad332017-02-24 19:06:12 -08004194 if (colorized != null) {
4195 mN.extras.putBoolean(EXTRA_COLORIZED, colorized);
4196 } else {
4197 mN.extras.remove(EXTRA_COLORIZED);
4198 }
Selim Cinekeaa29ca2015-11-23 13:51:13 -08004199 return header;
4200 }
4201
Adrian Roos487374f2017-01-11 15:48:14 -08004202 /**
4203 * Construct a RemoteViews for the ambient version of the notification.
4204 *
4205 * @hide
4206 */
4207 public RemoteViews makeAmbientNotification() {
4208 RemoteViews ambient = applyStandardTemplateWithActions(
4209 R.layout.notification_template_material_ambient,
Adrian Roos0bc3f6a2017-03-06 11:54:05 -08004210 mParams.reset().ambient(true).fillTextsFrom(this).hasProgress(false));
Adrian Roos487374f2017-01-11 15:48:14 -08004211 return ambient;
4212 }
4213
Selim Cinek29603462015-11-17 19:04:39 -08004214 private void hideLine1Text(RemoteViews result) {
Selim Cinekcc10bfb2016-02-10 16:24:21 -08004215 if (result != null) {
4216 result.setViewVisibility(R.id.text_line_1, View.GONE);
4217 }
Selim Cinek29603462015-11-17 19:04:39 -08004218 }
4219
Selim Cinek6743c0b2017-01-18 18:24:01 -08004220 /**
4221 * Adapt the Notification header if this view is used as an expanded view.
4222 *
4223 * @hide
4224 */
4225 public static void makeHeaderExpanded(RemoteViews result) {
Selim Cinekcc10bfb2016-02-10 16:24:21 -08004226 if (result != null) {
4227 result.setBoolean(R.id.notification_header, "setExpanded", true);
4228 }
Daniel Sandler96fd7c12012-03-30 16:37:36 -04004229 }
4230
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004231 /**
4232 * Construct a RemoteViews for the final heads-up notification layout.
Selim Cinek87ed69b2017-02-09 15:59:43 -08004233 *
4234 * @param increasedHeight true if this layout be created with an increased height. Some
4235 * styles may support showing more then just that basic 1U size
4236 * and the system may decide to render important notifications
4237 * slightly bigger even when collapsed.
4238 *
4239 * @hide
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004240 */
Selim Cinek87ed69b2017-02-09 15:59:43 -08004241 public RemoteViews createHeadsUpContentView(boolean increasedHeight) {
Selim Cinek593610c2016-02-16 18:42:57 -08004242 if (mN.headsUpContentView != null
4243 && (mStyle == null || !mStyle.displayCustomViewInline())) {
Julia Reynolds089e3e42015-11-18 09:59:57 -05004244 return mN.headsUpContentView;
4245 } else if (mStyle != null) {
Selim Cinek87ed69b2017-02-09 15:59:43 -08004246 final RemoteViews styleView = mStyle.makeHeadsUpContentView(increasedHeight);
4247 if (styleView != null) {
4248 return styleView;
4249 }
Julia Reynolds089e3e42015-11-18 09:59:57 -05004250 } else if (mActions.size() == 0) {
4251 return null;
4252 }
4253
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01004254 return applyStandardTemplateWithActions(getBigBaseLayoutResource());
Chris Wren8fd39ec2014-02-27 17:43:26 -05004255 }
4256
Selim Cinek624c02db2015-12-14 21:00:02 -08004257 /**
Selim Cinek87ed69b2017-02-09 15:59:43 -08004258 * Construct a RemoteViews for the final heads-up notification layout.
4259 */
4260 public RemoteViews createHeadsUpContentView() {
4261 return createHeadsUpContentView(false /* useIncreasedHeight */);
4262 }
4263
4264 /**
Selim Cinek624c02db2015-12-14 21:00:02 -08004265 * Construct a RemoteViews for the display in public contexts like on the lockscreen.
4266 *
4267 * @hide
4268 */
4269 public RemoteViews makePublicContentView() {
Adrian Roos1a1ecfc2017-04-17 11:17:59 -07004270 return makePublicView(false /* ambient */);
4271 }
4272
4273 /**
4274 * Construct a RemoteViews for the display in public contexts like on the lockscreen.
4275 *
4276 * @hide
4277 */
4278 public RemoteViews makePublicAmbientNotification() {
4279 return makePublicView(true /* ambient */);
4280 }
4281
4282 private RemoteViews makePublicView(boolean ambient) {
Selim Cinek624c02db2015-12-14 21:00:02 -08004283 if (mN.publicVersion != null) {
4284 final Builder builder = recoverBuilder(mContext, mN.publicVersion);
Adrian Roos1a1ecfc2017-04-17 11:17:59 -07004285 return ambient ? builder.makeAmbientNotification() : builder.createContentView();
Selim Cinek624c02db2015-12-14 21:00:02 -08004286 }
4287 Bundle savedBundle = mN.extras;
4288 Style style = mStyle;
4289 mStyle = null;
4290 Icon largeIcon = mN.mLargeIcon;
4291 mN.mLargeIcon = null;
Selim Cinek279fa862016-06-14 10:57:25 -07004292 Bitmap largeIconLegacy = mN.largeIcon;
4293 mN.largeIcon = null;
Selim Cinek624c02db2015-12-14 21:00:02 -08004294 Bundle publicExtras = new Bundle();
4295 publicExtras.putBoolean(EXTRA_SHOW_WHEN,
4296 savedBundle.getBoolean(EXTRA_SHOW_WHEN));
4297 publicExtras.putBoolean(EXTRA_SHOW_CHRONOMETER,
4298 savedBundle.getBoolean(EXTRA_SHOW_CHRONOMETER));
Adrian Roos96b7e202016-05-17 13:50:38 -07004299 publicExtras.putBoolean(EXTRA_CHRONOMETER_COUNT_DOWN,
4300 savedBundle.getBoolean(EXTRA_CHRONOMETER_COUNT_DOWN));
Selim Cinek624c02db2015-12-14 21:00:02 -08004301 publicExtras.putCharSequence(EXTRA_TITLE,
Adrian Roos1a1ecfc2017-04-17 11:17:59 -07004302 mContext.getString(com.android.internal.R.string.notification_hidden_text));
Selim Cinek624c02db2015-12-14 21:00:02 -08004303 mN.extras = publicExtras;
Adrian Roos1a1ecfc2017-04-17 11:17:59 -07004304 final RemoteViews view = ambient ? makeAmbientNotification()
4305 : applyStandardTemplate(getBaseLayoutResource());
Selim Cinek624c02db2015-12-14 21:00:02 -08004306 mN.extras = savedBundle;
4307 mN.mLargeIcon = largeIcon;
Selim Cinek279fa862016-06-14 10:57:25 -07004308 mN.largeIcon = largeIconLegacy;
Selim Cinek624c02db2015-12-14 21:00:02 -08004309 mStyle = style;
Adrian Roos1a1ecfc2017-04-17 11:17:59 -07004310 return view;
Selim Cinek624c02db2015-12-14 21:00:02 -08004311 }
4312
Selim Cinek6743c0b2017-01-18 18:24:01 -08004313 /**
4314 * Construct a content view for the display when low - priority
4315 *
4316 * @param useRegularSubtext uses the normal subtext set if there is one available. Otherwise
4317 * a new subtext is created consisting of the content of the
4318 * notification.
4319 * @hide
4320 */
4321 public RemoteViews makeLowPriorityContentView(boolean useRegularSubtext) {
4322 int color = mN.color;
4323 mN.color = COLOR_DEFAULT;
4324 CharSequence summary = mN.extras.getCharSequence(EXTRA_SUB_TEXT);
4325 if (!useRegularSubtext || TextUtils.isEmpty(summary)) {
4326 CharSequence newSummary = createSummaryText();
4327 if (!TextUtils.isEmpty(newSummary)) {
4328 mN.extras.putCharSequence(EXTRA_SUB_TEXT, newSummary);
4329 }
4330 }
Selim Cinek875ba9b2017-02-13 16:20:17 -08004331
Selim Cinek6743c0b2017-01-18 18:24:01 -08004332 RemoteViews header = makeNotificationHeader();
Selim Cinek1b554392017-02-28 17:22:49 -08004333 header.setBoolean(R.id.notification_header, "setAcceptAllTouches", true);
Selim Cinek6743c0b2017-01-18 18:24:01 -08004334 if (summary != null) {
4335 mN.extras.putCharSequence(EXTRA_SUB_TEXT, summary);
4336 } else {
4337 mN.extras.remove(EXTRA_SUB_TEXT);
4338 }
4339 mN.color = color;
4340 return header;
4341 }
Selim Cinek624c02db2015-12-14 21:00:02 -08004342
Selim Cinek6743c0b2017-01-18 18:24:01 -08004343 private CharSequence createSummaryText() {
4344 CharSequence titleText = mN.extras.getCharSequence(Notification.EXTRA_TITLE);
4345 if (USE_ONLY_TITLE_IN_LOW_PRIORITY_SUMMARY) {
4346 return titleText;
4347 }
4348 SpannableStringBuilder summary = new SpannableStringBuilder();
4349 if (titleText == null) {
4350 titleText = mN.extras.getCharSequence(Notification.EXTRA_TITLE_BIG);
4351 }
4352 BidiFormatter bidi = BidiFormatter.getInstance();
4353 if (titleText != null) {
4354 summary.append(bidi.unicodeWrap(titleText));
4355 }
4356 CharSequence contentText = mN.extras.getCharSequence(Notification.EXTRA_TEXT);
4357 if (titleText != null && contentText != null) {
4358 summary.append(bidi.unicodeWrap(mContext.getText(
4359 R.string.notification_header_divider_symbol_with_spaces)));
4360 }
4361 if (contentText != null) {
4362 summary.append(bidi.unicodeWrap(contentText));
4363 }
4364 return summary;
4365 }
Chris Wren8fd39ec2014-02-27 17:43:26 -05004366
Selim Cinek06e9e1f2016-07-08 17:14:16 -07004367 private RemoteViews generateActionButton(Action action, boolean emphazisedMode,
Adrian Roos487374f2017-01-11 15:48:14 -08004368 boolean oddAction, boolean ambient) {
Daniel Sandler8680bf82012-05-15 16:52:52 -04004369 final boolean tombstone = (action.actionIntent == null);
Selim Cinekf33b1112015-07-15 17:45:11 -07004370 RemoteViews button = new BuilderRemoteViews(mContext.getApplicationInfo(),
Selim Cinek06e9e1f2016-07-08 17:14:16 -07004371 emphazisedMode ? getEmphasizedActionLayoutResource()
4372 : tombstone ? getActionTombstoneLayoutResource()
4373 : getActionLayoutResource());
Daniel Sandler8680bf82012-05-15 16:52:52 -04004374 if (!tombstone) {
Daniel Sandlere5518842012-05-10 16:20:40 -04004375 button.setOnClickPendingIntent(R.id.action0, action.actionIntent);
Daniel Sandlere5518842012-05-10 16:20:40 -04004376 }
Daniel Sandler96fd7c12012-03-30 16:37:36 -04004377 button.setContentDescription(R.id.action0, action.title);
Adrian Roosfe84e1f2015-11-04 15:55:39 -08004378 if (action.mRemoteInputs != null) {
4379 button.setRemoteInputs(R.id.action0, action.mRemoteInputs);
4380 }
Selim Cinek7b9605b2017-01-19 17:36:00 -08004381 // TODO: handle emphasized mode / actions right
Selim Cinek06e9e1f2016-07-08 17:14:16 -07004382 if (emphazisedMode) {
Selim Cinek981962e2016-07-20 20:41:58 -07004383 // change the background bgColor
Selim Cinek622c64a2017-04-17 17:10:05 -07004384 int bgColor;
4385 if (isColorized()) {
4386 bgColor = oddAction ? getActionBarColor() : getActionBarColorDeEmphasized();
4387 } else {
4388 bgColor = mContext.getColor(oddAction ? R.color.notification_action_list
4389 : R.color.notification_action_list_dark);
4390 }
Selim Cinek981962e2016-07-20 20:41:58 -07004391 button.setDrawableParameters(R.id.button_holder, true, -1, bgColor,
Selim Cinek06e9e1f2016-07-08 17:14:16 -07004392 PorterDuff.Mode.SRC_ATOP, -1);
Selim Cinek981962e2016-07-20 20:41:58 -07004393 CharSequence title = action.title;
4394 ColorStateList[] outResultColor = null;
4395 if (isLegacy()) {
4396 title = clearColorSpans(title);
4397 } else {
4398 outResultColor = new ColorStateList[1];
4399 title = ensureColorSpanContrast(title, bgColor, outResultColor);
4400 }
4401 button.setTextViewText(R.id.action0, title);
Selim Cinek7b9605b2017-01-19 17:36:00 -08004402 setTextViewColorPrimary(button, R.id.action0);
Selim Cinek981962e2016-07-20 20:41:58 -07004403 if (outResultColor != null && outResultColor[0] != null) {
4404 // We need to set the text color as well since changing a text to uppercase
4405 // clears its spans.
4406 button.setTextColor(R.id.action0, outResultColor[0]);
Selim Cinek7b9605b2017-01-19 17:36:00 -08004407 } else if (mN.color != COLOR_DEFAULT && !isColorized()) {
Selim Cinek981962e2016-07-20 20:41:58 -07004408 button.setTextColor(R.id.action0,resolveContrastColor());
4409 }
Selim Cinek06e9e1f2016-07-08 17:14:16 -07004410 } else {
Selim Cinek981962e2016-07-20 20:41:58 -07004411 button.setTextViewText(R.id.action0, processLegacyText(action.title));
Adrian Roos72171622017-01-27 10:32:06 -08004412 if (isColorized() && !ambient) {
Selim Cinek7b9605b2017-01-19 17:36:00 -08004413 setTextViewColorPrimary(button, R.id.action0);
4414 } else if (mN.color != COLOR_DEFAULT) {
Adrian Roos487374f2017-01-11 15:48:14 -08004415 button.setTextColor(R.id.action0,
4416 ambient ? resolveAmbientColor() : resolveContrastColor());
Selim Cinek06e9e1f2016-07-08 17:14:16 -07004417 }
Adrian Roosfe84e1f2015-11-04 15:55:39 -08004418 }
Daniel Sandler96fd7c12012-03-30 16:37:36 -04004419 return button;
4420 }
4421
Joe Onoratocb109a02011-01-18 17:57:41 -08004422 /**
Selim Cinek981962e2016-07-20 20:41:58 -07004423 * Clears all color spans of a text
4424 * @param charSequence the input text
4425 * @return the same text but without color spans
4426 */
4427 private CharSequence clearColorSpans(CharSequence charSequence) {
4428 if (charSequence instanceof Spanned) {
4429 Spanned ss = (Spanned) charSequence;
4430 Object[] spans = ss.getSpans(0, ss.length(), Object.class);
4431 SpannableStringBuilder builder = new SpannableStringBuilder(ss.toString());
4432 for (Object span : spans) {
4433 Object resultSpan = span;
4434 if (resultSpan instanceof CharacterStyle) {
4435 resultSpan = ((CharacterStyle) span).getUnderlying();
4436 }
4437 if (resultSpan instanceof TextAppearanceSpan) {
4438 TextAppearanceSpan originalSpan = (TextAppearanceSpan) resultSpan;
4439 if (originalSpan.getTextColor() != null) {
4440 resultSpan = new TextAppearanceSpan(
4441 originalSpan.getFamily(),
4442 originalSpan.getTextStyle(),
4443 originalSpan.getTextSize(),
4444 null,
4445 originalSpan.getLinkTextColor());
4446 }
4447 } else if (resultSpan instanceof ForegroundColorSpan
4448 || (resultSpan instanceof BackgroundColorSpan)) {
4449 continue;
4450 } else {
4451 resultSpan = span;
4452 }
4453 builder.setSpan(resultSpan, ss.getSpanStart(span), ss.getSpanEnd(span),
4454 ss.getSpanFlags(span));
4455 }
4456 return builder;
4457 }
4458 return charSequence;
4459 }
4460
4461 /**
4462 * Ensures contrast on color spans against a background color. also returns the color of the
4463 * text if a span was found that spans over the whole text.
4464 *
4465 * @param charSequence the charSequence on which the spans are
4466 * @param background the background color to ensure the contrast against
4467 * @param outResultColor an array in which a color will be returned as the first element if
4468 * there exists a full length color span.
4469 * @return the contrasted charSequence
4470 */
4471 private CharSequence ensureColorSpanContrast(CharSequence charSequence, int background,
4472 ColorStateList[] outResultColor) {
4473 if (charSequence instanceof Spanned) {
4474 Spanned ss = (Spanned) charSequence;
4475 Object[] spans = ss.getSpans(0, ss.length(), Object.class);
4476 SpannableStringBuilder builder = new SpannableStringBuilder(ss.toString());
4477 for (Object span : spans) {
4478 Object resultSpan = span;
4479 int spanStart = ss.getSpanStart(span);
4480 int spanEnd = ss.getSpanEnd(span);
4481 boolean fullLength = (spanEnd - spanStart) == charSequence.length();
4482 if (resultSpan instanceof CharacterStyle) {
4483 resultSpan = ((CharacterStyle) span).getUnderlying();
4484 }
4485 if (resultSpan instanceof TextAppearanceSpan) {
4486 TextAppearanceSpan originalSpan = (TextAppearanceSpan) resultSpan;
4487 ColorStateList textColor = originalSpan.getTextColor();
4488 if (textColor != null) {
4489 int[] colors = textColor.getColors();
4490 int[] newColors = new int[colors.length];
4491 for (int i = 0; i < newColors.length; i++) {
4492 newColors[i] = NotificationColorUtil.ensureLargeTextContrast(
4493 colors[i], background);
4494 }
4495 textColor = new ColorStateList(textColor.getStates().clone(),
4496 newColors);
4497 resultSpan = new TextAppearanceSpan(
4498 originalSpan.getFamily(),
4499 originalSpan.getTextStyle(),
4500 originalSpan.getTextSize(),
4501 textColor,
4502 originalSpan.getLinkTextColor());
4503 if (fullLength) {
4504 outResultColor[0] = new ColorStateList(
4505 textColor.getStates().clone(), newColors);
4506 }
4507 }
4508 } else if (resultSpan instanceof ForegroundColorSpan) {
4509 ForegroundColorSpan originalSpan = (ForegroundColorSpan) resultSpan;
4510 int foregroundColor = originalSpan.getForegroundColor();
4511 foregroundColor = NotificationColorUtil.ensureLargeTextContrast(
4512 foregroundColor, background);
4513 resultSpan = new ForegroundColorSpan(foregroundColor);
4514 if (fullLength) {
4515 outResultColor[0] = ColorStateList.valueOf(foregroundColor);
4516 }
4517 } else {
4518 resultSpan = span;
4519 }
4520
4521 builder.setSpan(resultSpan, spanStart, spanEnd, ss.getSpanFlags(span));
4522 }
4523 return builder;
4524 }
4525 return charSequence;
4526 }
4527
4528 /**
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01004529 * @return Whether we are currently building a notification from a legacy (an app that
Alan Viverette3cb07a462014-06-06 14:19:53 -07004530 * doesn't create material notifications by itself) app.
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01004531 */
4532 private boolean isLegacy() {
Selim Cinek7b9605b2017-01-19 17:36:00 -08004533 if (!mIsLegacyInitialized) {
4534 mIsLegacy = mContext.getApplicationInfo().targetSdkVersion
4535 < Build.VERSION_CODES.LOLLIPOP;
4536 mIsLegacyInitialized = true;
4537 }
4538 return mIsLegacy;
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01004539 }
4540
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01004541 private CharSequence processLegacyText(CharSequence charSequence) {
Adrian Roos0bc3f6a2017-03-06 11:54:05 -08004542 return processLegacyText(charSequence, false /* ambient */);
4543 }
4544
4545 private CharSequence processLegacyText(CharSequence charSequence, boolean ambient) {
4546 boolean isAlreadyLightText = isLegacy() || textColorsNeedInversion();
4547 boolean wantLightText = ambient;
4548 if (isAlreadyLightText != wantLightText) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004549 return getColorUtil().invertCharSequenceColors(charSequence);
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01004550 } else {
4551 return charSequence;
4552 }
4553 }
4554
Dan Sandler26e81cf2014-05-06 10:01:27 -04004555 /**
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004556 * Apply any necessariy colors to the small icon
Dan Sandler26e81cf2014-05-06 10:01:27 -04004557 */
Adrian Roos487374f2017-01-11 15:48:14 -08004558 private void processSmallIconColor(Icon smallIcon, RemoteViews contentView,
4559 boolean ambient) {
Selim Cinekea4bef72015-12-02 15:51:10 -08004560 boolean colorable = !isLegacy() || getColorUtil().isGrayscaleIcon(mContext, smallIcon);
Selim Cinek99104832017-01-25 14:47:33 -08004561 int color = ambient ? resolveAmbientColor() : getPrimaryHighlightColor();
Selim Cinekea4bef72015-12-02 15:51:10 -08004562 if (colorable) {
Adrian Roos487374f2017-01-11 15:48:14 -08004563 contentView.setDrawableParameters(R.id.icon, false, -1, color,
Jorim Jaggi92df1f22014-12-16 19:44:41 +01004564 PorterDuff.Mode.SRC_ATOP, -1);
Selim Cinekea4bef72015-12-02 15:51:10 -08004565
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01004566 }
Selim Cinekea4bef72015-12-02 15:51:10 -08004567 contentView.setInt(R.id.notification_header, "setOriginalIconColor",
Adrian Roos487374f2017-01-11 15:48:14 -08004568 colorable ? color : NotificationHeaderView.NO_COLOR);
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01004569 }
4570
Dan Sandler26e81cf2014-05-06 10:01:27 -04004571 /**
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004572 * Make the largeIcon dark if it's a fake smallIcon (that is,
Dan Sandler26e81cf2014-05-06 10:01:27 -04004573 * if it's grayscale).
4574 */
4575 // TODO: also check bounds, transparency, that sort of thing.
Dan Sandlerd63f9322015-05-06 15:18:49 -04004576 private void processLargeLegacyIcon(Icon largeIcon, RemoteViews contentView) {
4577 if (largeIcon != null && isLegacy()
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004578 && getColorUtil().isGrayscaleIcon(mContext, largeIcon)) {
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004579 // resolve color will fall back to the default when legacy
Adrian Roos4ff3b122016-02-01 12:26:13 -08004580 contentView.setDrawableParameters(R.id.icon, false, -1, resolveContrastColor(),
Dan Sandler190d58d2014-05-15 09:33:39 -04004581 PorterDuff.Mode.SRC_ATOP, -1);
Jorim Jaggi92df1f22014-12-16 19:44:41 +01004582 }
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01004583 }
4584
Julia Reynolds10ee1fc2015-11-09 11:04:55 -05004585 private void sanitizeColor() {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004586 if (mN.color != COLOR_DEFAULT) {
4587 mN.color |= 0xFF000000; // no alpha for custom colors
Jorim Jaggi74419312014-06-10 20:57:21 +02004588 }
Jorim Jaggi74419312014-06-10 20:57:21 +02004589 }
4590
Adrian Roos4ff3b122016-02-01 12:26:13 -08004591 int resolveContrastColor() {
4592 if (mCachedContrastColorIsFor == mN.color && mCachedContrastColor != COLOR_INVALID) {
4593 return mCachedContrastColor;
Dan Sandler26e81cf2014-05-06 10:01:27 -04004594 }
Adrian Roos4ff3b122016-02-01 12:26:13 -08004595 final int contrasted = NotificationColorUtil.resolveContrastColor(mContext, mN.color);
4596
4597 mCachedContrastColorIsFor = mN.color;
4598 return mCachedContrastColor = contrasted;
Dan Sandler26e81cf2014-05-06 10:01:27 -04004599 }
4600
Adrian Roos487374f2017-01-11 15:48:14 -08004601 int resolveAmbientColor() {
4602 if (mCachedAmbientColorIsFor == mN.color && mCachedAmbientColorIsFor != COLOR_INVALID) {
4603 return mCachedAmbientColor;
4604 }
4605 final int contrasted = NotificationColorUtil.resolveAmbientColor(mContext, mN.color);
4606
4607 mCachedAmbientColorIsFor = mN.color;
4608 return mCachedAmbientColor = contrasted;
4609 }
4610
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01004611 /**
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004612 * Apply the unstyled operations and return a new {@link Notification} object.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04004613 * @hide
Joe Onoratocb109a02011-01-18 17:57:41 -08004614 */
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04004615 public Notification buildUnstyled() {
Daniel Sandlera0a938c2012-03-15 08:42:37 -04004616 if (mActions.size() > 0) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004617 mN.actions = new Action[mActions.size()];
4618 mActions.toArray(mN.actions);
Daniel Sandlera0a938c2012-03-15 08:42:37 -04004619 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004620 if (!mPersonList.isEmpty()) {
4621 mN.extras.putStringArray(EXTRA_PEOPLE,
4622 mPersonList.toArray(new String[mPersonList.size()]));
Dan Sandler0bf2ed82013-12-21 23:33:41 -06004623 }
Selim Cinek247fa012016-02-18 09:50:48 -08004624 if (mN.bigContentView != null || mN.contentView != null
4625 || mN.headsUpContentView != null) {
4626 mN.extras.putBoolean(EXTRA_CONTAINS_CUSTOM_VIEW, true);
4627 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004628 return mN;
Joe Onorato46439ce2010-11-19 13:56:21 -08004629 }
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004630
Julia Reynolds3b848122016-02-26 10:45:32 -05004631 /**
4632 * Creates a Builder from an existing notification so further changes can be made.
4633 * @param context The context for your application / activity.
4634 * @param n The notification to create a Builder from.
4635 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004636 public static Notification.Builder recoverBuilder(Context context, Notification n) {
Christoph Studer4600f9b2014-07-22 22:44:43 +02004637 // Re-create notification context so we can access app resources.
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004638 ApplicationInfo applicationInfo = n.extras.getParcelable(
4639 EXTRA_BUILDER_APPLICATION_INFO);
Christoph Studer4600f9b2014-07-22 22:44:43 +02004640 Context builderContext;
Julia Reynoldsda303542015-11-23 14:00:20 -05004641 if (applicationInfo != null) {
4642 try {
4643 builderContext = context.createApplicationContext(applicationInfo,
4644 Context.CONTEXT_RESTRICTED);
4645 } catch (NameNotFoundException e) {
4646 Log.e(TAG, "ApplicationInfo " + applicationInfo + " not found");
4647 builderContext = context; // try with our context
4648 }
4649 } else {
4650 builderContext = context; // try with given context
Christoph Studer4600f9b2014-07-22 22:44:43 +02004651 }
4652
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004653 return new Builder(builderContext, n);
Christoph Studer4600f9b2014-07-22 22:44:43 +02004654 }
4655
Daniel Sandlerf45564e2013-04-15 15:05:08 -04004656 /**
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004657 * @deprecated Use {@link #build()} instead.
4658 */
4659 @Deprecated
4660 public Notification getNotification() {
4661 return build();
4662 }
4663
4664 /**
4665 * Combine all of the options that have been set and return a new {@link Notification}
4666 * object.
4667 */
4668 public Notification build() {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004669 // first, add any extras from the calling code
4670 if (mUserExtras != null) {
4671 mN.extras = getAllExtras();
Jorim Jaggia0d58ae2015-06-03 11:48:13 -07004672 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004673
Selim Cinekb85f36fd2016-04-20 18:46:36 -07004674 mN.creationTime = System.currentTimeMillis();
4675
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004676 // lazy stuff from mContext; see comment in Builder(Context, Notification)
Julia Reynoldsda303542015-11-23 14:00:20 -05004677 Notification.addFieldsFromContext(mContext, mN);
Christoph Studer943aa672014-08-03 20:31:16 +02004678
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004679 buildUnstyled();
Daniel Sandlerf45564e2013-04-15 15:05:08 -04004680
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004681 if (mStyle != null) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004682 mStyle.buildStyled(mN);
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004683 }
Daniel Sandlerf45564e2013-04-15 15:05:08 -04004684
Adrian Roos5081c0d2016-02-26 16:04:19 -08004685 if (mContext.getApplicationInfo().targetSdkVersion < Build.VERSION_CODES.N
4686 && (mStyle == null || !mStyle.displayCustomViewInline())) {
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05004687 if (mN.contentView == null) {
Julia Reynolds3b848122016-02-26 10:45:32 -05004688 mN.contentView = createContentView();
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05004689 mN.extras.putInt(EXTRA_REBUILD_CONTENT_VIEW_ACTION_COUNT,
4690 mN.contentView.getSequenceNumber());
4691 }
4692 if (mN.bigContentView == null) {
Julia Reynolds3b848122016-02-26 10:45:32 -05004693 mN.bigContentView = createBigContentView();
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05004694 if (mN.bigContentView != null) {
4695 mN.extras.putInt(EXTRA_REBUILD_BIG_CONTENT_VIEW_ACTION_COUNT,
4696 mN.bigContentView.getSequenceNumber());
4697 }
4698 }
4699 if (mN.headsUpContentView == null) {
Julia Reynolds3b848122016-02-26 10:45:32 -05004700 mN.headsUpContentView = createHeadsUpContentView();
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05004701 if (mN.headsUpContentView != null) {
4702 mN.extras.putInt(EXTRA_REBUILD_HEADS_UP_CONTENT_VIEW_ACTION_COUNT,
4703 mN.headsUpContentView.getSequenceNumber());
4704 }
4705 }
4706 }
4707
Julia Reynolds4c0c2022016-02-02 15:11:59 -05004708 if ((mN.defaults & DEFAULT_LIGHTS) != 0) {
4709 mN.flags |= FLAG_SHOW_LIGHTS;
4710 }
4711
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004712 return mN;
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004713 }
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05004714
4715 /**
4716 * Apply this Builder to an existing {@link Notification} object.
4717 *
4718 * @hide
4719 */
4720 public Notification buildInto(Notification n) {
Daniel Sandler1a497d32013-04-18 14:52:45 -04004721 build().cloneInto(n, true);
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05004722 return n;
4723 }
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01004724
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05004725 /**
Adrian Roos184bfe022016-03-03 13:41:44 -08004726 * Removes RemoteViews that were created for compatibility from {@param n}, if they did not
4727 * change.
4728 *
4729 * @return {@param n}, if no stripping is needed, otherwise a stripped clone of {@param n}.
4730 *
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05004731 * @hide
4732 */
Adrian Roos184bfe022016-03-03 13:41:44 -08004733 public static Notification maybeCloneStrippedForDelivery(Notification n) {
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05004734 String templateClass = n.extras.getString(EXTRA_TEMPLATE);
Adrian Roos184bfe022016-03-03 13:41:44 -08004735
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05004736 // Only strip views for known Styles because we won't know how to
4737 // re-create them otherwise.
Adrian Roos184bfe022016-03-03 13:41:44 -08004738 if (!TextUtils.isEmpty(templateClass)
4739 && getNotificationStyleClass(templateClass) == null) {
4740 return n;
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05004741 }
Adrian Roos184bfe022016-03-03 13:41:44 -08004742
4743 // Only strip unmodified BuilderRemoteViews.
4744 boolean stripContentView = n.contentView instanceof BuilderRemoteViews &&
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05004745 n.extras.getInt(EXTRA_REBUILD_CONTENT_VIEW_ACTION_COUNT, -1) ==
Adrian Roos184bfe022016-03-03 13:41:44 -08004746 n.contentView.getSequenceNumber();
4747 boolean stripBigContentView = n.bigContentView instanceof BuilderRemoteViews &&
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05004748 n.extras.getInt(EXTRA_REBUILD_BIG_CONTENT_VIEW_ACTION_COUNT, -1) ==
Adrian Roos184bfe022016-03-03 13:41:44 -08004749 n.bigContentView.getSequenceNumber();
4750 boolean stripHeadsUpContentView = n.headsUpContentView instanceof BuilderRemoteViews &&
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05004751 n.extras.getInt(EXTRA_REBUILD_HEADS_UP_CONTENT_VIEW_ACTION_COUNT, -1) ==
Adrian Roos184bfe022016-03-03 13:41:44 -08004752 n.headsUpContentView.getSequenceNumber();
4753
4754 // Nothing to do here, no need to clone.
4755 if (!stripContentView && !stripBigContentView && !stripHeadsUpContentView) {
4756 return n;
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05004757 }
Adrian Roos184bfe022016-03-03 13:41:44 -08004758
4759 Notification clone = n.clone();
4760 if (stripContentView) {
4761 clone.contentView = null;
4762 clone.extras.remove(EXTRA_REBUILD_CONTENT_VIEW_ACTION_COUNT);
4763 }
4764 if (stripBigContentView) {
4765 clone.bigContentView = null;
4766 clone.extras.remove(EXTRA_REBUILD_BIG_CONTENT_VIEW_ACTION_COUNT);
4767 }
4768 if (stripHeadsUpContentView) {
4769 clone.headsUpContentView = null;
4770 clone.extras.remove(EXTRA_REBUILD_HEADS_UP_CONTENT_VIEW_ACTION_COUNT);
4771 }
4772 return clone;
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05004773 }
4774
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01004775 private int getBaseLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07004776 return R.layout.notification_template_material_base;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01004777 }
4778
4779 private int getBigBaseLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07004780 return R.layout.notification_template_material_big_base;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01004781 }
4782
4783 private int getBigPictureLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07004784 return R.layout.notification_template_material_big_picture;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01004785 }
4786
4787 private int getBigTextLayoutResource() {
Jorim Jaggi445d3c02014-08-19 22:33:42 +02004788 return R.layout.notification_template_material_big_text;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01004789 }
4790
4791 private int getInboxLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07004792 return R.layout.notification_template_material_inbox;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01004793 }
4794
Adrian Roosc1a80b02016-04-05 14:54:55 -07004795 private int getMessagingLayoutResource() {
4796 return R.layout.notification_template_material_messaging;
4797 }
4798
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01004799 private int getActionLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07004800 return R.layout.notification_material_action;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01004801 }
4802
Selim Cinek06e9e1f2016-07-08 17:14:16 -07004803 private int getEmphasizedActionLayoutResource() {
4804 return R.layout.notification_material_action_emphasized;
4805 }
4806
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01004807 private int getActionTombstoneLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07004808 return R.layout.notification_material_action_tombstone;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01004809 }
Selim Cinek7b9605b2017-01-19 17:36:00 -08004810
4811 private int getBackgroundColor() {
4812 if (isColorized()) {
4813 return mN.color;
4814 } else {
4815 return COLOR_DEFAULT;
4816 }
4817 }
4818
4819 private boolean isColorized() {
4820 return mN.isColorized();
4821 }
Selim Cinek99104832017-01-25 14:47:33 -08004822
4823 private boolean textColorsNeedInversion() {
4824 if (mStyle == null || !MediaStyle.class.equals(mStyle.getClass())) {
4825 return false;
4826 }
4827 int targetSdkVersion = mContext.getApplicationInfo().targetSdkVersion;
4828 return targetSdkVersion > Build.VERSION_CODES.M
4829 && targetSdkVersion < Build.VERSION_CODES.O;
4830 }
Selim Cinek7b9605b2017-01-19 17:36:00 -08004831 }
4832
4833 /**
Selim Cinek22714f12017-04-13 16:23:53 -07004834 * @return whether this notification is a foreground service notification
Selim Cinek7b9605b2017-01-19 17:36:00 -08004835 */
Selim Cinek22714f12017-04-13 16:23:53 -07004836 private boolean isForegroundService() {
4837 return (flags & Notification.FLAG_FOREGROUND_SERVICE) != 0;
Selim Cinek7b9605b2017-01-19 17:36:00 -08004838 }
4839
4840 /**
Selim Cinek99104832017-01-25 14:47:33 -08004841 * @return whether this notification has a media session attached
4842 * @hide
4843 */
4844 public boolean hasMediaSession() {
4845 return extras.getParcelable(Notification.EXTRA_MEDIA_SESSION) != null;
4846 }
4847
4848 /**
Selim Cinek7d1009b2017-01-25 15:28:28 -08004849 * @return the style class of this notification
4850 * @hide
4851 */
4852 public Class<? extends Notification.Style> getNotificationStyle() {
4853 String templateClass = extras.getString(Notification.EXTRA_TEMPLATE);
4854
4855 if (!TextUtils.isEmpty(templateClass)) {
4856 return Notification.getNotificationStyleClass(templateClass);
4857 }
4858 return null;
4859 }
4860
4861 /**
Selim Cinek22714f12017-04-13 16:23:53 -07004862 * @return true if this notification is colorized.
Selim Cinek7b9605b2017-01-19 17:36:00 -08004863 *
4864 * @hide
4865 */
4866 public boolean isColorized() {
Selim Cinek99104832017-01-25 14:47:33 -08004867 Class<? extends Style> style = getNotificationStyle();
4868 if (MediaStyle.class.equals(style)) {
4869 Boolean colorized = (Boolean) extras.get(EXTRA_COLORIZED);
4870 if ((colorized == null || colorized) && hasMediaSession()) {
4871 return true;
4872 }
4873 } else if (DecoratedMediaCustomViewStyle.class.equals(style)) {
4874 if (extras.getBoolean(EXTRA_COLORIZED) && hasMediaSession()) {
4875 return true;
4876 }
4877 }
Selim Cinek22714f12017-04-13 16:23:53 -07004878 return extras.getBoolean(EXTRA_COLORIZED) && isForegroundService();
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004879 }
4880
Selim Cinek279fa862016-06-14 10:57:25 -07004881 private boolean hasLargeIcon() {
4882 return mLargeIcon != null || largeIcon != null;
4883 }
4884
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004885 /**
Selim Cinekc2c0b042016-05-18 17:13:46 -07004886 * @return true if the notification will show the time; false otherwise
Selim Cinekb85f36fd2016-04-20 18:46:36 -07004887 * @hide
4888 */
Selim Cinekc2c0b042016-05-18 17:13:46 -07004889 public boolean showsTime() {
Selim Cinekb85f36fd2016-04-20 18:46:36 -07004890 return when != 0 && extras.getBoolean(EXTRA_SHOW_WHEN);
4891 }
4892
4893 /**
Selim Cinekc2c0b042016-05-18 17:13:46 -07004894 * @return true if the notification will show a chronometer; false otherwise
4895 * @hide
4896 */
4897 public boolean showsChronometer() {
4898 return when != 0 && extras.getBoolean(EXTRA_SHOW_CHRONOMETER);
4899 }
4900
4901 /**
Julia Reynolds4a02afb2016-12-13 13:39:52 -05004902 * @hide
4903 */
4904 @SystemApi
4905 public static Class<? extends Style> getNotificationStyleClass(String templateClass) {
4906 Class<? extends Style>[] classes = new Class[] {
4907 BigTextStyle.class, BigPictureStyle.class, InboxStyle.class, MediaStyle.class,
4908 DecoratedCustomViewStyle.class, DecoratedMediaCustomViewStyle.class,
4909 MessagingStyle.class };
4910 for (Class<? extends Style> innerClass : classes) {
4911 if (templateClass.equals(innerClass.getName())) {
4912 return innerClass;
4913 }
4914 }
4915 return null;
4916 }
4917
4918 /**
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004919 * An object that can apply a rich notification style to a {@link Notification.Builder}
4920 * object.
4921 */
Griff Hazendfcb0802014-02-11 12:00:00 -08004922 public static abstract class Style {
Chris Wrend6297db2012-05-03 16:20:13 -04004923 private CharSequence mBigContentTitle;
Jorim Jaggi457a10d2014-09-08 16:18:23 +02004924
4925 /**
4926 * @hide
4927 */
4928 protected CharSequence mSummaryText = null;
4929
4930 /**
4931 * @hide
4932 */
4933 protected boolean mSummaryTextSet = false;
Chris Wrend6297db2012-05-03 16:20:13 -04004934
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004935 protected Builder mBuilder;
4936
Chris Wrend6297db2012-05-03 16:20:13 -04004937 /**
4938 * Overrides ContentTitle in the big form of the template.
4939 * This defaults to the value passed to setContentTitle().
4940 */
4941 protected void internalSetBigContentTitle(CharSequence title) {
4942 mBigContentTitle = title;
4943 }
4944
4945 /**
4946 * Set the first line of text after the detail section in the big form of the template.
4947 */
4948 protected void internalSetSummaryText(CharSequence cs) {
4949 mSummaryText = cs;
Daniel Sandler619738c2012-06-07 16:33:08 -04004950 mSummaryTextSet = true;
Chris Wrend6297db2012-05-03 16:20:13 -04004951 }
4952
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004953 public void setBuilder(Builder builder) {
4954 if (mBuilder != builder) {
4955 mBuilder = builder;
Daniel Sandlerc08dea22012-06-28 08:35:24 -07004956 if (mBuilder != null) {
4957 mBuilder.setStyle(this);
4958 }
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004959 }
4960 }
4961
Chris Wrend6297db2012-05-03 16:20:13 -04004962 protected void checkBuilder() {
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004963 if (mBuilder == null) {
4964 throw new IllegalArgumentException("Style requires a valid Builder object");
4965 }
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004966 }
Chris Wrend6297db2012-05-03 16:20:13 -04004967
4968 protected RemoteViews getStandardView(int layoutId) {
4969 checkBuilder();
4970
Christoph Studer4600f9b2014-07-22 22:44:43 +02004971 // Nasty.
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004972 CharSequence oldBuilderContentTitle =
4973 mBuilder.getAllExtras().getCharSequence(EXTRA_TITLE);
Chris Wrend6297db2012-05-03 16:20:13 -04004974 if (mBigContentTitle != null) {
4975 mBuilder.setContentTitle(mBigContentTitle);
4976 }
4977
Chris Wrend6297db2012-05-03 16:20:13 -04004978 RemoteViews contentView = mBuilder.applyStandardTemplateWithActions(layoutId);
4979
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004980 mBuilder.getAllExtras().putCharSequence(EXTRA_TITLE, oldBuilderContentTitle);
Christoph Studer4600f9b2014-07-22 22:44:43 +02004981
Chris Wrend6297db2012-05-03 16:20:13 -04004982 if (mBigContentTitle != null && mBigContentTitle.equals("")) {
4983 contentView.setViewVisibility(R.id.line1, View.GONE);
Chris Wren67dc9a02012-05-16 01:03:20 -04004984 } else {
4985 contentView.setViewVisibility(R.id.line1, View.VISIBLE);
Chris Wrend6297db2012-05-03 16:20:13 -04004986 }
4987
Chris Wrend6297db2012-05-03 16:20:13 -04004988 return contentView;
4989 }
4990
Daniel Sandlerf45564e2013-04-15 15:05:08 -04004991 /**
Selim Cinek7d1009b2017-01-25 15:28:28 -08004992 * Construct a Style-specific RemoteViews for the collapsed notification layout.
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004993 * The default implementation has nothing additional to add.
Selim Cinek7d1009b2017-01-25 15:28:28 -08004994 *
4995 * @param increasedHeight true if this layout be created with an increased height.
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004996 * @hide
4997 */
Selim Cinek7d1009b2017-01-25 15:28:28 -08004998 public RemoteViews makeContentView(boolean increasedHeight) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004999 return null;
5000 }
5001
5002 /**
5003 * Construct a Style-specific RemoteViews for the final big notification layout.
5004 * @hide
5005 */
5006 public RemoteViews makeBigContentView() {
5007 return null;
5008 }
5009
5010 /**
5011 * Construct a Style-specific RemoteViews for the final HUN layout.
Selim Cinek87ed69b2017-02-09 15:59:43 -08005012 *
5013 * @param increasedHeight true if this layout be created with an increased height.
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005014 * @hide
5015 */
Selim Cinek87ed69b2017-02-09 15:59:43 -08005016 public RemoteViews makeHeadsUpContentView(boolean increasedHeight) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005017 return null;
5018 }
5019
5020 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005021 * Apply any style-specific extras to this notification before shipping it out.
Daniel Sandlerf45564e2013-04-15 15:05:08 -04005022 * @hide
5023 */
5024 public void addExtras(Bundle extras) {
5025 if (mSummaryTextSet) {
5026 extras.putCharSequence(EXTRA_SUMMARY_TEXT, mSummaryText);
5027 }
5028 if (mBigContentTitle != null) {
5029 extras.putCharSequence(EXTRA_TITLE_BIG, mBigContentTitle);
5030 }
Chris Wren91ad5632013-06-05 15:05:57 -04005031 extras.putString(EXTRA_TEMPLATE, this.getClass().getName());
Daniel Sandlerf45564e2013-04-15 15:05:08 -04005032 }
5033
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04005034 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005035 * Reconstruct the internal state of this Style object from extras.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04005036 * @hide
5037 */
Christoph Studer4600f9b2014-07-22 22:44:43 +02005038 protected void restoreFromExtras(Bundle extras) {
5039 if (extras.containsKey(EXTRA_SUMMARY_TEXT)) {
5040 mSummaryText = extras.getCharSequence(EXTRA_SUMMARY_TEXT);
5041 mSummaryTextSet = true;
5042 }
5043 if (extras.containsKey(EXTRA_TITLE_BIG)) {
5044 mBigContentTitle = extras.getCharSequence(EXTRA_TITLE_BIG);
5045 }
5046 }
5047
5048
5049 /**
5050 * @hide
5051 */
5052 public Notification buildStyled(Notification wip) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005053 addExtras(wip.extras);
Christoph Studer4600f9b2014-07-22 22:44:43 +02005054 return wip;
5055 }
5056
Daniel Sandler0ec46202015-06-24 01:27:05 -04005057 /**
5058 * @hide
5059 */
Jorim Jaggia0d58ae2015-06-03 11:48:13 -07005060 public void purgeResources() {}
5061
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04005062 /**
5063 * Calls {@link android.app.Notification.Builder#build()} on the Builder this Style is
5064 * attached to.
5065 *
5066 * @return the fully constructed Notification.
5067 */
5068 public Notification build() {
5069 checkBuilder();
5070 return mBuilder.build();
5071 }
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02005072
5073 /**
5074 * @hide
5075 * @return true if the style positions the progress bar on the second line; false if the
5076 * style hides the progress bar
5077 */
5078 protected boolean hasProgress() {
5079 return true;
5080 }
Selim Cinek03d0d652015-11-13 13:18:09 -05005081
5082 /**
5083 * @hide
5084 * @return Whether we should put the summary be put into the notification header
5085 */
5086 public boolean hasSummaryInHeader() {
5087 return true;
5088 }
Selim Cinek593610c2016-02-16 18:42:57 -08005089
5090 /**
5091 * @hide
5092 * @return Whether custom content views are displayed inline in the style
5093 */
5094 public boolean displayCustomViewInline() {
5095 return false;
5096 }
Joe Onorato46439ce2010-11-19 13:56:21 -08005097 }
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005098
5099 /**
Daniel Sandler4dfbe832012-04-11 14:51:46 -04005100 * Helper class for generating large-format notifications that include a large image attachment.
Joe Malin8d40d042012-11-05 11:36:40 -08005101 *
Robert Ly91c5ce32014-06-08 15:37:00 -07005102 * Here's how you'd set the <code>BigPictureStyle</code> on a notification:
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005103 * <pre class="prettyprint">
Robert Ly91c5ce32014-06-08 15:37:00 -07005104 * Notification notif = new Notification.Builder(mContext)
5105 * .setContentTitle(&quot;New photo from &quot; + sender.toString())
5106 * .setContentText(subject)
5107 * .setSmallIcon(R.drawable.new_post)
5108 * .setLargeIcon(aBitmap)
5109 * .setStyle(new Notification.BigPictureStyle()
5110 * .bigPicture(aBigBitmap))
5111 * .build();
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005112 * </pre>
Joe Malin8d40d042012-11-05 11:36:40 -08005113 *
Daniel Sandler4dfbe832012-04-11 14:51:46 -04005114 * @see Notification#bigContentView
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005115 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005116 public static class BigPictureStyle extends Style {
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005117 private Bitmap mPicture;
Dan Sandlerd63f9322015-05-06 15:18:49 -04005118 private Icon mBigLargeIcon;
Chris Wren3745a3d2012-05-22 15:11:52 -04005119 private boolean mBigLargeIconSet = false;
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005120
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005121 public BigPictureStyle() {
5122 }
5123
Adrian Roosf5faf9d2016-05-23 13:56:15 -07005124 /**
5125 * @deprecated use {@code BigPictureStyle()}.
5126 */
5127 @Deprecated
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005128 public BigPictureStyle(Builder builder) {
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005129 setBuilder(builder);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005130 }
5131
Chris Wrend6297db2012-05-03 16:20:13 -04005132 /**
5133 * Overrides ContentTitle in the big form of the template.
5134 * This defaults to the value passed to setContentTitle().
5135 */
5136 public BigPictureStyle setBigContentTitle(CharSequence title) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04005137 internalSetBigContentTitle(safeCharSequence(title));
Chris Wrend6297db2012-05-03 16:20:13 -04005138 return this;
5139 }
5140
5141 /**
5142 * Set the first line of text after the detail section in the big form of the template.
5143 */
5144 public BigPictureStyle setSummaryText(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04005145 internalSetSummaryText(safeCharSequence(cs));
Chris Wrend6297db2012-05-03 16:20:13 -04005146 return this;
5147 }
5148
Chris Wren0bd664d2012-08-01 13:56:56 -04005149 /**
5150 * Provide the bitmap to be used as the payload for the BigPicture notification.
5151 */
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005152 public BigPictureStyle bigPicture(Bitmap b) {
5153 mPicture = b;
5154 return this;
5155 }
5156
Chris Wren3745a3d2012-05-22 15:11:52 -04005157 /**
Chris Wren3745a3d2012-05-22 15:11:52 -04005158 * Override the large icon when the big notification is shown.
5159 */
5160 public BigPictureStyle bigLargeIcon(Bitmap b) {
Dan Sandlerd63f9322015-05-06 15:18:49 -04005161 return bigLargeIcon(b != null ? Icon.createWithBitmap(b) : null);
5162 }
5163
5164 /**
5165 * Override the large icon when the big notification is shown.
5166 */
5167 public BigPictureStyle bigLargeIcon(Icon icon) {
Chris Wren3745a3d2012-05-22 15:11:52 -04005168 mBigLargeIconSet = true;
Dan Sandlerd63f9322015-05-06 15:18:49 -04005169 mBigLargeIcon = icon;
Chris Wren3745a3d2012-05-22 15:11:52 -04005170 return this;
5171 }
5172
Riley Andrews0394a0c2015-11-03 23:36:52 -08005173 /** @hide */
5174 public static final int MIN_ASHMEM_BITMAP_SIZE = 128 * (1 << 10);
5175
Daniel Sandler0ec46202015-06-24 01:27:05 -04005176 /**
5177 * @hide
5178 */
Jorim Jaggia0d58ae2015-06-03 11:48:13 -07005179 @Override
5180 public void purgeResources() {
5181 super.purgeResources();
Riley Andrews8cee7c12015-11-01 23:36:04 -08005182 if (mPicture != null &&
5183 mPicture.isMutable() &&
Riley Andrews0394a0c2015-11-03 23:36:52 -08005184 mPicture.getAllocationByteCount() >= MIN_ASHMEM_BITMAP_SIZE) {
Jorim Jaggia0d58ae2015-06-03 11:48:13 -07005185 mPicture = mPicture.createAshmemBitmap();
5186 }
5187 if (mBigLargeIcon != null) {
5188 mBigLargeIcon.convertToAshmem();
5189 }
5190 }
Christoph Studer5c510ee2014-12-15 16:32:27 +01005191
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005192 /**
5193 * @hide
5194 */
5195 public RemoteViews makeBigContentView() {
5196 // Replace mN.mLargeIcon with mBigLargeIcon if mBigLargeIconSet
Christoph Studer5c510ee2014-12-15 16:32:27 +01005197 // This covers the following cases:
5198 // 1. mBigLargeIconSet -> mBigLargeIcon (null or non-null) applies, overrides
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005199 // mN.mLargeIcon
5200 // 2. !mBigLargeIconSet -> mN.mLargeIcon applies
Dan Sandlerd63f9322015-05-06 15:18:49 -04005201 Icon oldLargeIcon = null;
Selim Cineke99acb22016-08-04 12:55:48 -07005202 Bitmap largeIconLegacy = null;
Christoph Studer5c510ee2014-12-15 16:32:27 +01005203 if (mBigLargeIconSet) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005204 oldLargeIcon = mBuilder.mN.mLargeIcon;
5205 mBuilder.mN.mLargeIcon = mBigLargeIcon;
Selim Cineke99acb22016-08-04 12:55:48 -07005206 // The legacy largeIcon might not allow us to clear the image, as it's taken in
5207 // replacement if the other one is null. Because we're restoring these legacy icons
5208 // for old listeners, this is in general non-null.
5209 largeIconLegacy = mBuilder.mN.largeIcon;
5210 mBuilder.mN.largeIcon = null;
Christoph Studer5c510ee2014-12-15 16:32:27 +01005211 }
5212
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005213 RemoteViews contentView = getStandardView(mBuilder.getBigPictureLayoutResource());
Selim Cinek03d0d652015-11-13 13:18:09 -05005214 if (mSummaryTextSet) {
5215 contentView.setTextViewText(R.id.text, mBuilder.processLegacyText(mSummaryText));
Selim Cinek7b9605b2017-01-19 17:36:00 -08005216 mBuilder.setTextViewColorSecondary(contentView, R.id.text);
Selim Cinekc848c3a2016-01-13 15:27:30 -08005217 contentView.setViewVisibility(R.id.text, View.VISIBLE);
Selim Cinek03d0d652015-11-13 13:18:09 -05005218 }
Selim Cinek279fa862016-06-14 10:57:25 -07005219 mBuilder.setContentMinHeight(contentView, mBuilder.mN.hasLargeIcon());
Selim Cinek53e64a42015-11-16 10:40:56 -08005220
Christoph Studer5c510ee2014-12-15 16:32:27 +01005221 if (mBigLargeIconSet) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005222 mBuilder.mN.mLargeIcon = oldLargeIcon;
Selim Cineke99acb22016-08-04 12:55:48 -07005223 mBuilder.mN.largeIcon = largeIconLegacy;
Christoph Studer5c510ee2014-12-15 16:32:27 +01005224 }
5225
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005226 contentView.setImageViewBitmap(R.id.big_picture, mPicture);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005227 return contentView;
5228 }
5229
Daniel Sandlerf45564e2013-04-15 15:05:08 -04005230 /**
5231 * @hide
5232 */
5233 public void addExtras(Bundle extras) {
5234 super.addExtras(extras);
5235
5236 if (mBigLargeIconSet) {
5237 extras.putParcelable(EXTRA_LARGE_ICON_BIG, mBigLargeIcon);
5238 }
5239 extras.putParcelable(EXTRA_PICTURE, mPicture);
5240 }
5241
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04005242 /**
5243 * @hide
5244 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005245 @Override
Christoph Studer4600f9b2014-07-22 22:44:43 +02005246 protected void restoreFromExtras(Bundle extras) {
5247 super.restoreFromExtras(extras);
5248
5249 if (extras.containsKey(EXTRA_LARGE_ICON_BIG)) {
Christoph Studer5c510ee2014-12-15 16:32:27 +01005250 mBigLargeIconSet = true;
Christoph Studer4600f9b2014-07-22 22:44:43 +02005251 mBigLargeIcon = extras.getParcelable(EXTRA_LARGE_ICON_BIG);
Chris Wren3745a3d2012-05-22 15:11:52 -04005252 }
Christoph Studer4600f9b2014-07-22 22:44:43 +02005253 mPicture = extras.getParcelable(EXTRA_PICTURE);
5254 }
Selim Cinek03d0d652015-11-13 13:18:09 -05005255
5256 /**
5257 * @hide
5258 */
5259 @Override
5260 public boolean hasSummaryInHeader() {
5261 return false;
5262 }
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005263 }
5264
5265 /**
Daniel Sandler4dfbe832012-04-11 14:51:46 -04005266 * Helper class for generating large-format notifications that include a lot of text.
Joe Malin8d40d042012-11-05 11:36:40 -08005267 *
Robert Ly91c5ce32014-06-08 15:37:00 -07005268 * Here's how you'd set the <code>BigTextStyle</code> on a notification:
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005269 * <pre class="prettyprint">
Robert Ly91c5ce32014-06-08 15:37:00 -07005270 * Notification notif = new Notification.Builder(mContext)
5271 * .setContentTitle(&quot;New mail from &quot; + sender.toString())
5272 * .setContentText(subject)
5273 * .setSmallIcon(R.drawable.new_mail)
5274 * .setLargeIcon(aBitmap)
5275 * .setStyle(new Notification.BigTextStyle()
5276 * .bigText(aVeryLongString))
5277 * .build();
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005278 * </pre>
Joe Malin8d40d042012-11-05 11:36:40 -08005279 *
Daniel Sandler4dfbe832012-04-11 14:51:46 -04005280 * @see Notification#bigContentView
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005281 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005282 public static class BigTextStyle extends Style {
Jorim Jaggi457a10d2014-09-08 16:18:23 +02005283
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005284 private CharSequence mBigText;
5285
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005286 public BigTextStyle() {
5287 }
5288
Adrian Roosf5faf9d2016-05-23 13:56:15 -07005289 /**
5290 * @deprecated use {@code BigTextStyle()}.
5291 */
5292 @Deprecated
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005293 public BigTextStyle(Builder builder) {
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005294 setBuilder(builder);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005295 }
5296
Chris Wrend6297db2012-05-03 16:20:13 -04005297 /**
5298 * Overrides ContentTitle in the big form of the template.
5299 * This defaults to the value passed to setContentTitle().
5300 */
5301 public BigTextStyle setBigContentTitle(CharSequence title) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04005302 internalSetBigContentTitle(safeCharSequence(title));
Chris Wrend6297db2012-05-03 16:20:13 -04005303 return this;
5304 }
5305
5306 /**
5307 * Set the first line of text after the detail section in the big form of the template.
5308 */
5309 public BigTextStyle setSummaryText(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04005310 internalSetSummaryText(safeCharSequence(cs));
Chris Wrend6297db2012-05-03 16:20:13 -04005311 return this;
5312 }
5313
Chris Wren0bd664d2012-08-01 13:56:56 -04005314 /**
5315 * Provide the longer text to be displayed in the big form of the
5316 * template in place of the content text.
5317 */
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005318 public BigTextStyle bigText(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04005319 mBigText = safeCharSequence(cs);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005320 return this;
5321 }
5322
Daniel Sandlerf45564e2013-04-15 15:05:08 -04005323 /**
5324 * @hide
5325 */
5326 public void addExtras(Bundle extras) {
5327 super.addExtras(extras);
5328
Christoph Studer4600f9b2014-07-22 22:44:43 +02005329 extras.putCharSequence(EXTRA_BIG_TEXT, mBigText);
5330 }
5331
5332 /**
5333 * @hide
5334 */
5335 @Override
5336 protected void restoreFromExtras(Bundle extras) {
5337 super.restoreFromExtras(extras);
5338
5339 mBigText = extras.getCharSequence(EXTRA_BIG_TEXT);
Daniel Sandlerf45564e2013-04-15 15:05:08 -04005340 }
5341
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005342 /**
Selim Cinek7d1009b2017-01-25 15:28:28 -08005343 * @param increasedHeight true if this layout be created with an increased height.
5344 *
5345 * @hide
5346 */
5347 @Override
5348 public RemoteViews makeContentView(boolean increasedHeight) {
5349 if (increasedHeight) {
5350 ArrayList<Action> actions = mBuilder.mActions;
5351 mBuilder.mActions = new ArrayList<>();
5352 RemoteViews remoteViews = makeBigContentView();
5353 mBuilder.mActions = actions;
5354 return remoteViews;
5355 }
5356 return super.makeContentView(increasedHeight);
5357 }
5358
5359 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005360 * @hide
5361 */
Selim Cinek87ed69b2017-02-09 15:59:43 -08005362 @Override
5363 public RemoteViews makeHeadsUpContentView(boolean increasedHeight) {
5364 if (increasedHeight && mBuilder.mActions.size() > 0) {
5365 return makeBigContentView();
5366 }
5367 return super.makeHeadsUpContentView(increasedHeight);
5368 }
5369
5370 /**
5371 * @hide
5372 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005373 public RemoteViews makeBigContentView() {
Christoph Studer4600f9b2014-07-22 22:44:43 +02005374
5375 // Nasty
Selim Cinek75998782016-04-26 10:39:17 -07005376 CharSequence text = mBuilder.getAllExtras().getCharSequence(EXTRA_TEXT);
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005377 mBuilder.getAllExtras().putCharSequence(EXTRA_TEXT, null);
Daniel Sandler916ad912012-06-13 12:17:07 -04005378
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005379 RemoteViews contentView = getStandardView(mBuilder.getBigTextLayoutResource());
Joe Malin8d40d042012-11-05 11:36:40 -08005380
Selim Cinek75998782016-04-26 10:39:17 -07005381 mBuilder.getAllExtras().putCharSequence(EXTRA_TEXT, text);
Christoph Studer4600f9b2014-07-22 22:44:43 +02005382
Selim Cinek3a2c4b92015-12-17 17:01:17 -08005383 CharSequence bigTextText = mBuilder.processLegacyText(mBigText);
Selim Cinek75998782016-04-26 10:39:17 -07005384 if (TextUtils.isEmpty(bigTextText)) {
5385 // In case the bigtext is null / empty fall back to the normal text to avoid a weird
5386 // experience
5387 bigTextText = mBuilder.processLegacyText(text);
5388 }
Adrian Roosb1f427c2016-05-26 12:27:15 -07005389 applyBigTextContentView(mBuilder, contentView, bigTextText);
Selim Cinek4fb12d32015-11-19 18:10:48 -08005390
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005391 return contentView;
5392 }
5393
Adrian Roosb1f427c2016-05-26 12:27:15 -07005394 static void applyBigTextContentView(Builder builder,
5395 RemoteViews contentView, CharSequence bigTextText) {
5396 contentView.setTextViewText(R.id.big_text, bigTextText);
Selim Cinek7b9605b2017-01-19 17:36:00 -08005397 builder.setTextViewColorSecondary(contentView, R.id.big_text);
Adrian Roosb1f427c2016-05-26 12:27:15 -07005398 contentView.setViewVisibility(R.id.big_text,
5399 TextUtils.isEmpty(bigTextText) ? View.GONE : View.VISIBLE);
Selim Cinek279fa862016-06-14 10:57:25 -07005400 contentView.setBoolean(R.id.big_text, "setHasImage", builder.mN.hasLargeIcon());
Adrian Roosb1f427c2016-05-26 12:27:15 -07005401 }
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005402 }
Daniel Sandler879c5e02012-04-17 16:46:51 -04005403
5404 /**
Alex Hillsfc737de2016-03-23 17:33:02 -04005405 * Helper class for generating large-format notifications that include multiple back-and-forth
5406 * messages of varying types between any number of people.
5407 *
5408 * <br>
5409 * If the platform does not provide large-format notifications, this method has no effect. The
5410 * user will always see the normal notification view.
5411 * <br>
5412 * This class is a "rebuilder": It attaches to a Builder object and modifies its behavior, like
5413 * so:
5414 * <pre class="prettyprint">
5415 *
5416 * Notification noti = new Notification.Builder()
5417 * .setContentTitle(&quot;2 new messages wtih &quot; + sender.toString())
5418 * .setContentText(subject)
5419 * .setSmallIcon(R.drawable.new_message)
5420 * .setLargeIcon(aBitmap)
5421 * .setStyle(new Notification.MessagingStyle(resources.getString(R.string.reply_name))
5422 * .addMessage(messages[0].getText(), messages[0].getTime(), messages[0].getSender())
5423 * .addMessage(messages[1].getText(), messages[1].getTime(), messages[1].getSender()))
5424 * .build();
5425 * </pre>
5426 */
5427 public static class MessagingStyle extends Style {
5428
5429 /**
5430 * The maximum number of messages that will be retained in the Notification itself (the
5431 * number displayed is up to the platform).
5432 */
5433 public static final int MAXIMUM_RETAINED_MESSAGES = 25;
5434
5435 CharSequence mUserDisplayName;
5436 CharSequence mConversationTitle;
Alex Hillsd9b04d92016-04-11 16:38:16 -04005437 List<Message> mMessages = new ArrayList<>();
Adrian Roos437cd562017-01-18 15:47:03 -08005438 List<Message> mHistoricMessages = new ArrayList<>();
Alex Hillsfc737de2016-03-23 17:33:02 -04005439
5440 MessagingStyle() {
5441 }
5442
5443 /**
Alex Hillsfd590442016-10-07 09:52:44 -04005444 * @param userDisplayName Required - the name to be displayed for any replies sent by the
5445 * user before the posting app reposts the notification with those messages after they've
5446 * been actually sent and in previous messages sent by the user added in
Alex Hillsfc737de2016-03-23 17:33:02 -04005447 * {@link #addMessage(Notification.MessagingStyle.Message)}
5448 */
Alex Hillsfd590442016-10-07 09:52:44 -04005449 public MessagingStyle(@NonNull CharSequence userDisplayName) {
Alex Hillsfc737de2016-03-23 17:33:02 -04005450 mUserDisplayName = userDisplayName;
5451 }
5452
5453 /**
5454 * Returns the name to be displayed for any replies sent by the user
5455 */
5456 public CharSequence getUserDisplayName() {
5457 return mUserDisplayName;
5458 }
5459
5460 /**
Alex Hillsfc737de2016-03-23 17:33:02 -04005461 * Sets the title to be displayed on this conversation. This should only be used for
5462 * group messaging and left unset for one-on-one conversations.
5463 * @param conversationTitle
5464 * @return this object for method chaining.
5465 */
5466 public MessagingStyle setConversationTitle(CharSequence conversationTitle) {
5467 mConversationTitle = conversationTitle;
5468 return this;
5469 }
5470
5471 /**
5472 * Return the title to be displayed on this conversation. Can be <code>null</code> and
5473 * should be for one-on-one conversations
5474 */
5475 public CharSequence getConversationTitle() {
5476 return mConversationTitle;
5477 }
5478
5479 /**
5480 * Adds a message for display by this notification. Convenience call for a simple
5481 * {@link Message} in {@link #addMessage(Notification.MessagingStyle.Message)}.
5482 * @param text A {@link CharSequence} to be displayed as the message content
5483 * @param timestamp Time at which the message arrived
5484 * @param sender A {@link CharSequence} to be used for displaying the name of the
5485 * sender. Should be <code>null</code> for messages by the current user, in which case
5486 * the platform will insert {@link #getUserDisplayName()}.
5487 * Should be unique amongst all individuals in the conversation, and should be
5488 * consistent during re-posts of the notification.
5489 *
5490 * @see Message#Message(CharSequence, long, CharSequence)
5491 *
5492 * @return this object for method chaining
5493 */
5494 public MessagingStyle addMessage(CharSequence text, long timestamp, CharSequence sender) {
Adrian Roos437cd562017-01-18 15:47:03 -08005495 return addMessage(new Message(text, timestamp, sender));
Alex Hillsfc737de2016-03-23 17:33:02 -04005496 }
5497
5498 /**
5499 * Adds a {@link Message} for display in this notification.
Adrian Roos437cd562017-01-18 15:47:03 -08005500 *
5501 * <p>The messages should be added in chronologic order, i.e. the oldest first,
5502 * the newest last.
5503 *
Alex Hillsfc737de2016-03-23 17:33:02 -04005504 * @param message The {@link Message} to be displayed
5505 * @return this object for method chaining
5506 */
5507 public MessagingStyle addMessage(Message message) {
5508 mMessages.add(message);
5509 if (mMessages.size() > MAXIMUM_RETAINED_MESSAGES) {
5510 mMessages.remove(0);
5511 }
5512 return this;
5513 }
5514
5515 /**
Adrian Roos437cd562017-01-18 15:47:03 -08005516 * Adds a {@link Message} for historic context in this notification.
5517 *
5518 * <p>Messages should be added as historic if they are not the main subject of the
5519 * notification but may give context to a conversation. The system may choose to present
5520 * them only when relevant, e.g. when replying to a message through a {@link RemoteInput}.
5521 *
5522 * <p>The messages should be added in chronologic order, i.e. the oldest first,
5523 * the newest last.
5524 *
5525 * @param message The historic {@link Message} to be added
5526 * @return this object for method chaining
5527 */
5528 public MessagingStyle addHistoricMessage(Message message) {
5529 mHistoricMessages.add(message);
5530 if (mHistoricMessages.size() > MAXIMUM_RETAINED_MESSAGES) {
5531 mHistoricMessages.remove(0);
5532 }
5533 return this;
5534 }
5535
5536 /**
Alex Hillsfc737de2016-03-23 17:33:02 -04005537 * Gets the list of {@code Message} objects that represent the notification
5538 */
5539 public List<Message> getMessages() {
5540 return mMessages;
5541 }
5542
5543 /**
Adrian Roos437cd562017-01-18 15:47:03 -08005544 * Gets the list of historic {@code Message}s in the notification.
5545 */
5546 public List<Message> getHistoricMessages() {
5547 return mHistoricMessages;
5548 }
5549
5550 /**
Alex Hillsfc737de2016-03-23 17:33:02 -04005551 * @hide
5552 */
5553 @Override
5554 public void addExtras(Bundle extras) {
5555 super.addExtras(extras);
5556 if (mUserDisplayName != null) {
5557 extras.putCharSequence(EXTRA_SELF_DISPLAY_NAME, mUserDisplayName);
5558 }
5559 if (mConversationTitle != null) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04005560 extras.putCharSequence(EXTRA_CONVERSATION_TITLE, mConversationTitle);
Alex Hillsfc737de2016-03-23 17:33:02 -04005561 }
Alex Hillsd9b04d92016-04-11 16:38:16 -04005562 if (!mMessages.isEmpty()) { extras.putParcelableArray(EXTRA_MESSAGES,
5563 Message.getBundleArrayForMessages(mMessages));
Alex Hillsfc737de2016-03-23 17:33:02 -04005564 }
Adrian Roos437cd562017-01-18 15:47:03 -08005565 if (!mHistoricMessages.isEmpty()) { extras.putParcelableArray(EXTRA_HISTORIC_MESSAGES,
5566 Message.getBundleArrayForMessages(mHistoricMessages));
5567 }
Adrian Roos33fbd2c2016-05-27 15:35:28 -07005568
5569 fixTitleAndTextExtras(extras);
5570 }
5571
5572 private void fixTitleAndTextExtras(Bundle extras) {
5573 Message m = findLatestIncomingMessage();
5574 CharSequence text = (m == null) ? null : m.mText;
5575 CharSequence sender = m == null ? null
5576 : TextUtils.isEmpty(m.mSender) ? mUserDisplayName : m.mSender;
5577 CharSequence title;
5578 if (!TextUtils.isEmpty(mConversationTitle)) {
5579 if (!TextUtils.isEmpty(sender)) {
5580 BidiFormatter bidi = BidiFormatter.getInstance();
5581 title = mBuilder.mContext.getString(
5582 com.android.internal.R.string.notification_messaging_title_template,
5583 bidi.unicodeWrap(mConversationTitle), bidi.unicodeWrap(m.mSender));
5584 } else {
5585 title = mConversationTitle;
5586 }
5587 } else {
5588 title = sender;
5589 }
5590
5591 if (title != null) {
5592 extras.putCharSequence(EXTRA_TITLE, title);
5593 }
5594 if (text != null) {
5595 extras.putCharSequence(EXTRA_TEXT, text);
5596 }
Alex Hillsfc737de2016-03-23 17:33:02 -04005597 }
5598
5599 /**
5600 * @hide
5601 */
5602 @Override
5603 protected void restoreFromExtras(Bundle extras) {
5604 super.restoreFromExtras(extras);
5605
5606 mMessages.clear();
Adrian Roos437cd562017-01-18 15:47:03 -08005607 mHistoricMessages.clear();
Adrian Roos96b7e202016-05-17 13:50:38 -07005608 mUserDisplayName = extras.getCharSequence(EXTRA_SELF_DISPLAY_NAME);
5609 mConversationTitle = extras.getCharSequence(EXTRA_CONVERSATION_TITLE);
Adrian Roos437cd562017-01-18 15:47:03 -08005610 Parcelable[] messages = extras.getParcelableArray(EXTRA_MESSAGES);
5611 if (messages != null && messages instanceof Parcelable[]) {
5612 mMessages = Message.getMessagesFromBundleArray(messages);
5613 }
5614 Parcelable[] histMessages = extras.getParcelableArray(EXTRA_HISTORIC_MESSAGES);
5615 if (histMessages != null && histMessages instanceof Parcelable[]) {
5616 mHistoricMessages = Message.getMessagesFromBundleArray(histMessages);
Alex Hillsfc737de2016-03-23 17:33:02 -04005617 }
5618 }
5619
5620 /**
5621 * @hide
5622 */
Adrian Roosc1a80b02016-04-05 14:54:55 -07005623 @Override
Selim Cinek7d1009b2017-01-25 15:28:28 -08005624 public RemoteViews makeContentView(boolean increasedHeight) {
5625 if (!increasedHeight) {
5626 Message m = findLatestIncomingMessage();
5627 CharSequence title = mConversationTitle != null
5628 ? mConversationTitle
5629 : (m == null) ? null : m.mSender;
5630 CharSequence text = (m == null)
5631 ? null
5632 : mConversationTitle != null ? makeMessageLine(m, mBuilder) : m.mText;
Alex Hillsfc737de2016-03-23 17:33:02 -04005633
Selim Cinek7d1009b2017-01-25 15:28:28 -08005634 return mBuilder.applyStandardTemplate(mBuilder.getBaseLayoutResource(),
5635 mBuilder.mParams.reset().hasProgress(false).title(title).text(text));
5636 } else {
5637 ArrayList<Action> actions = mBuilder.mActions;
5638 mBuilder.mActions = new ArrayList<>();
5639 RemoteViews remoteViews = makeBigContentView();
5640 mBuilder.mActions = actions;
5641 return remoteViews;
5642 }
Adrian Roosc1a80b02016-04-05 14:54:55 -07005643 }
5644
5645 private Message findLatestIncomingMessage() {
5646 for (int i = mMessages.size() - 1; i >= 0; i--) {
5647 Message m = mMessages.get(i);
5648 // Incoming messages have a non-empty sender.
5649 if (!TextUtils.isEmpty(m.mSender)) {
5650 return m;
5651 }
5652 }
Adrian Roos33fbd2c2016-05-27 15:35:28 -07005653 if (!mMessages.isEmpty()) {
5654 // No incoming messages, fall back to outgoing message
5655 return mMessages.get(mMessages.size() - 1);
5656 }
Adrian Roosc1a80b02016-04-05 14:54:55 -07005657 return null;
5658 }
5659
5660 /**
5661 * @hide
5662 */
5663 @Override
5664 public RemoteViews makeBigContentView() {
5665 CharSequence title = !TextUtils.isEmpty(super.mBigContentTitle)
5666 ? super.mBigContentTitle
5667 : mConversationTitle;
5668 boolean hasTitle = !TextUtils.isEmpty(title);
5669
Adrian Roosfeafa052016-06-01 17:09:45 -07005670 if (mMessages.size() == 1) {
5671 // Special case for a single message: Use the big text style
5672 // so the collapsed and expanded versions match nicely.
5673 CharSequence bigTitle;
5674 CharSequence text;
5675 if (hasTitle) {
5676 bigTitle = title;
Selim Cinek7b9605b2017-01-19 17:36:00 -08005677 text = makeMessageLine(mMessages.get(0), mBuilder);
Adrian Roosfeafa052016-06-01 17:09:45 -07005678 } else {
5679 bigTitle = mMessages.get(0).mSender;
5680 text = mMessages.get(0).mText;
5681 }
Adrian Roosb1f427c2016-05-26 12:27:15 -07005682 RemoteViews contentView = mBuilder.applyStandardTemplateWithActions(
5683 mBuilder.getBigTextLayoutResource(),
Adrian Roos70d7aa32017-01-11 15:39:06 -08005684 mBuilder.mParams.reset().hasProgress(false).title(bigTitle).text(null));
Adrian Roosb1f427c2016-05-26 12:27:15 -07005685 BigTextStyle.applyBigTextContentView(mBuilder, contentView, text);
5686 return contentView;
5687 }
5688
Adrian Roos48d746a2016-04-12 14:57:28 -07005689 RemoteViews contentView = mBuilder.applyStandardTemplateWithActions(
Adrian Roosc1a80b02016-04-05 14:54:55 -07005690 mBuilder.getMessagingLayoutResource(),
Adrian Roos70d7aa32017-01-11 15:39:06 -08005691 mBuilder.mParams.reset().hasProgress(false).title(title).text(null));
Adrian Roosc1a80b02016-04-05 14:54:55 -07005692
5693 int[] rowIds = {R.id.inbox_text0, R.id.inbox_text1, R.id.inbox_text2, R.id.inbox_text3,
5694 R.id.inbox_text4, R.id.inbox_text5, R.id.inbox_text6};
5695
5696 // Make sure all rows are gone in case we reuse a view.
5697 for (int rowId : rowIds) {
5698 contentView.setViewVisibility(rowId, View.GONE);
5699 }
5700
5701 int i=0;
Adrian Roos2d5dbba2016-06-08 17:11:53 -07005702 contentView.setViewLayoutMarginBottomDimen(R.id.line1,
5703 hasTitle ? R.dimen.notification_messaging_spacing : 0);
Adrian Roosc1a80b02016-04-05 14:54:55 -07005704 contentView.setInt(R.id.notification_messaging, "setNumIndentLines",
Selim Cinek279fa862016-06-14 10:57:25 -07005705 !mBuilder.mN.hasLargeIcon() ? 0 : (hasTitle ? 1 : 2));
Adrian Roosc1a80b02016-04-05 14:54:55 -07005706
Adrian Roosfeafa052016-06-01 17:09:45 -07005707 int contractedChildId = View.NO_ID;
5708 Message contractedMessage = findLatestIncomingMessage();
Adrian Roos437cd562017-01-18 15:47:03 -08005709 int firstHistoricMessage = Math.max(0, mHistoricMessages.size()
5710 - (rowIds.length - mMessages.size()));
5711 while (firstHistoricMessage + i < mHistoricMessages.size() && i < rowIds.length) {
5712 Message m = mHistoricMessages.get(firstHistoricMessage + i);
5713 int rowId = rowIds[i];
5714
Selim Cinek7b9605b2017-01-19 17:36:00 -08005715 contentView.setTextViewText(rowId, makeMessageLine(m, mBuilder));
Adrian Roos437cd562017-01-18 15:47:03 -08005716
5717 if (contractedMessage == m) {
5718 contractedChildId = rowId;
5719 }
5720
5721 i++;
5722 }
5723
Adrian Roosc1a80b02016-04-05 14:54:55 -07005724 int firstMessage = Math.max(0, mMessages.size() - rowIds.length);
5725 while (firstMessage + i < mMessages.size() && i < rowIds.length) {
5726 Message m = mMessages.get(firstMessage + i);
5727 int rowId = rowIds[i];
5728
5729 contentView.setViewVisibility(rowId, View.VISIBLE);
Selim Cinek7b9605b2017-01-19 17:36:00 -08005730 contentView.setTextViewText(rowId, makeMessageLine(m, mBuilder));
5731 mBuilder.setTextViewColorSecondary(contentView, rowId);
Adrian Roosc1a80b02016-04-05 14:54:55 -07005732
Adrian Roosfeafa052016-06-01 17:09:45 -07005733 if (contractedMessage == m) {
5734 contractedChildId = rowId;
5735 }
5736
Adrian Roosc1a80b02016-04-05 14:54:55 -07005737 i++;
5738 }
Adrian Roos437cd562017-01-18 15:47:03 -08005739 // Clear the remaining views for reapply. Ensures that historic message views can
5740 // reliably be identified as being GONE and having non-null text.
5741 while (i < rowIds.length) {
5742 int rowId = rowIds[i];
5743 contentView.setTextViewText(rowId, null);
5744 i++;
5745 }
5746
Adrian Roosfeafa052016-06-01 17:09:45 -07005747 // Record this here to allow transformation between the contracted and expanded views.
5748 contentView.setInt(R.id.notification_messaging, "setContractedChildId",
5749 contractedChildId);
Alex Hillsfc737de2016-03-23 17:33:02 -04005750 return contentView;
5751 }
5752
Selim Cinek7b9605b2017-01-19 17:36:00 -08005753 private CharSequence makeMessageLine(Message m, Builder builder) {
Adrian Roosc1a80b02016-04-05 14:54:55 -07005754 BidiFormatter bidi = BidiFormatter.getInstance();
5755 SpannableStringBuilder sb = new SpannableStringBuilder();
Selim Cinek7b9605b2017-01-19 17:36:00 -08005756 boolean colorize = builder.isColorized();
Adrian Roosc1a80b02016-04-05 14:54:55 -07005757 if (TextUtils.isEmpty(m.mSender)) {
5758 CharSequence replyName = mUserDisplayName == null ? "" : mUserDisplayName;
5759 sb.append(bidi.unicodeWrap(replyName),
Selim Cinek7b9605b2017-01-19 17:36:00 -08005760 makeFontColorSpan(colorize
5761 ? builder.getPrimaryTextColor()
5762 : mBuilder.resolveContrastColor()),
Adrian Roosc1a80b02016-04-05 14:54:55 -07005763 0 /* flags */);
5764 } else {
5765 sb.append(bidi.unicodeWrap(m.mSender),
Selim Cinek7b9605b2017-01-19 17:36:00 -08005766 makeFontColorSpan(colorize
5767 ? builder.getPrimaryTextColor()
5768 : Color.BLACK),
Adrian Roosc1a80b02016-04-05 14:54:55 -07005769 0 /* flags */);
5770 }
5771 CharSequence text = m.mText == null ? "" : m.mText;
5772 sb.append(" ").append(bidi.unicodeWrap(text));
5773 return sb;
5774 }
5775
Adrian Roosdedd1df2016-04-26 16:38:47 -07005776 /**
5777 * @hide
5778 */
5779 @Override
Selim Cinek87ed69b2017-02-09 15:59:43 -08005780 public RemoteViews makeHeadsUpContentView(boolean increasedHeight) {
5781 if (increasedHeight) {
5782 return makeBigContentView();
5783 }
Adrian Roosdedd1df2016-04-26 16:38:47 -07005784 Message m = findLatestIncomingMessage();
5785 CharSequence title = mConversationTitle != null
5786 ? mConversationTitle
5787 : (m == null) ? null : m.mSender;
5788 CharSequence text = (m == null)
5789 ? null
Selim Cinek7b9605b2017-01-19 17:36:00 -08005790 : mConversationTitle != null ? makeMessageLine(m, mBuilder) : m.mText;
Adrian Roosdedd1df2016-04-26 16:38:47 -07005791
5792 return mBuilder.applyStandardTemplateWithActions(mBuilder.getBigBaseLayoutResource(),
Adrian Roos70d7aa32017-01-11 15:39:06 -08005793 mBuilder.mParams.reset().hasProgress(false).title(title).text(text));
Adrian Roosdedd1df2016-04-26 16:38:47 -07005794 }
5795
Adrian Roosc1a80b02016-04-05 14:54:55 -07005796 private static TextAppearanceSpan makeFontColorSpan(int color) {
5797 return new TextAppearanceSpan(null, 0, 0,
5798 ColorStateList.valueOf(color), null);
5799 }
5800
Alex Hillsd9b04d92016-04-11 16:38:16 -04005801 public static final class Message {
5802
5803 static final String KEY_TEXT = "text";
5804 static final String KEY_TIMESTAMP = "time";
5805 static final String KEY_SENDER = "sender";
5806 static final String KEY_DATA_MIME_TYPE = "type";
5807 static final String KEY_DATA_URI= "uri";
Shane Brennan5a871862017-03-11 13:14:17 -08005808 static final String KEY_EXTRAS_BUNDLE = "extras";
Alex Hillsfc737de2016-03-23 17:33:02 -04005809
5810 private final CharSequence mText;
5811 private final long mTimestamp;
5812 private final CharSequence mSender;
5813
Shane Brennan5a871862017-03-11 13:14:17 -08005814 private Bundle mExtras = new Bundle();
Alex Hillsfc737de2016-03-23 17:33:02 -04005815 private String mDataMimeType;
5816 private Uri mDataUri;
5817
5818 /**
5819 * Constructor
5820 * @param text A {@link CharSequence} to be displayed as the message content
5821 * @param timestamp Time at which the message arrived
5822 * @param sender A {@link CharSequence} to be used for displaying the name of the
5823 * sender. Should be <code>null</code> for messages by the current user, in which case
5824 * the platform will insert {@link MessagingStyle#getUserDisplayName()}.
5825 * Should be unique amongst all individuals in the conversation, and should be
5826 * consistent during re-posts of the notification.
5827 */
5828 public Message(CharSequence text, long timestamp, CharSequence sender){
5829 mText = text;
5830 mTimestamp = timestamp;
5831 mSender = sender;
5832 }
5833
5834 /**
5835 * Sets a binary blob of data and an associated MIME type for a message. In the case
5836 * where the platform doesn't support the MIME type, the original text provided in the
5837 * constructor will be used.
5838 * @param dataMimeType The MIME type of the content. See
5839 * <a href="{@docRoot}notifications/messaging.html"> for the list of supported MIME
5840 * types on Android and Android Wear.
5841 * @param dataUri The uri containing the content whose type is given by the MIME type.
5842 * <p class="note">
5843 * <ol>
5844 * <li>Notification Listeners including the System UI need permission to access the
5845 * data the Uri points to. The recommended ways to do this are:</li>
5846 * <li>Store the data in your own ContentProvider, making sure that other apps have
5847 * the correct permission to access your provider. The preferred mechanism for
5848 * providing access is to use per-URI permissions which are temporary and only
5849 * grant access to the receiving application. An easy way to create a
5850 * ContentProvider like this is to use the FileProvider helper class.</li>
5851 * <li>Use the system MediaStore. The MediaStore is primarily aimed at video, audio
5852 * and image MIME types, however beginning with Android 3.0 (API level 11) it can
5853 * also store non-media types (see MediaStore.Files for more info). Files can be
5854 * inserted into the MediaStore using scanFile() after which a content:// style
5855 * Uri suitable for sharing is passed to the provided onScanCompleted() callback.
5856 * Note that once added to the system MediaStore the content is accessible to any
5857 * app on the device.</li>
5858 * </ol>
5859 * @return this object for method chaining
5860 */
5861 public Message setData(String dataMimeType, Uri dataUri) {
5862 mDataMimeType = dataMimeType;
5863 mDataUri = dataUri;
5864 return this;
5865 }
5866
Alex Hillsfc737de2016-03-23 17:33:02 -04005867 /**
5868 * Get the text to be used for this message, or the fallback text if a type and content
5869 * Uri have been set
5870 */
5871 public CharSequence getText() {
5872 return mText;
5873 }
5874
5875 /**
5876 * Get the time at which this message arrived
5877 */
5878 public long getTimestamp() {
5879 return mTimestamp;
5880 }
5881
5882 /**
Shane Brennan5a871862017-03-11 13:14:17 -08005883 * Get the extras Bundle for this message.
5884 */
5885 public Bundle getExtras() {
5886 return mExtras;
5887 }
5888
5889 /**
Alex Hillsfc737de2016-03-23 17:33:02 -04005890 * Get the text used to display the contact's name in the messaging experience
5891 */
5892 public CharSequence getSender() {
5893 return mSender;
5894 }
5895
5896 /**
5897 * Get the MIME type of the data pointed to by the Uri
5898 */
5899 public String getDataMimeType() {
5900 return mDataMimeType;
5901 }
5902
5903 /**
5904 * Get the the Uri pointing to the content of the message. Can be null, in which case
5905 * {@see #getText()} is used.
5906 */
5907 public Uri getDataUri() {
5908 return mDataUri;
5909 }
5910
Alex Hillsd9b04d92016-04-11 16:38:16 -04005911 private Bundle toBundle() {
5912 Bundle bundle = new Bundle();
Alex Hillsfc737de2016-03-23 17:33:02 -04005913 if (mText != null) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04005914 bundle.putCharSequence(KEY_TEXT, mText);
Alex Hillsfc737de2016-03-23 17:33:02 -04005915 }
Alex Hillsd9b04d92016-04-11 16:38:16 -04005916 bundle.putLong(KEY_TIMESTAMP, mTimestamp);
Alex Hillsfc737de2016-03-23 17:33:02 -04005917 if (mSender != null) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04005918 bundle.putCharSequence(KEY_SENDER, mSender);
Alex Hillsfc737de2016-03-23 17:33:02 -04005919 }
5920 if (mDataMimeType != null) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04005921 bundle.putString(KEY_DATA_MIME_TYPE, mDataMimeType);
Alex Hillsfc737de2016-03-23 17:33:02 -04005922 }
5923 if (mDataUri != null) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04005924 bundle.putParcelable(KEY_DATA_URI, mDataUri);
Alex Hillsfc737de2016-03-23 17:33:02 -04005925 }
Shane Brennan5a871862017-03-11 13:14:17 -08005926 if (mExtras != null) {
5927 bundle.putBundle(KEY_EXTRAS_BUNDLE, mExtras);
5928 }
Alex Hillsd9b04d92016-04-11 16:38:16 -04005929 return bundle;
Alex Hillsfc737de2016-03-23 17:33:02 -04005930 }
5931
Alex Hillsd9b04d92016-04-11 16:38:16 -04005932 static Bundle[] getBundleArrayForMessages(List<Message> messages) {
5933 Bundle[] bundles = new Bundle[messages.size()];
5934 final int N = messages.size();
5935 for (int i = 0; i < N; i++) {
5936 bundles[i] = messages.get(i).toBundle();
5937 }
5938 return bundles;
5939 }
5940
Adrian Roosdedd1df2016-04-26 16:38:47 -07005941 static List<Message> getMessagesFromBundleArray(Parcelable[] bundles) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04005942 List<Message> messages = new ArrayList<>(bundles.length);
5943 for (int i = 0; i < bundles.length; i++) {
Adrian Roosdedd1df2016-04-26 16:38:47 -07005944 if (bundles[i] instanceof Bundle) {
5945 Message message = getMessageFromBundle((Bundle)bundles[i]);
5946 if (message != null) {
5947 messages.add(message);
5948 }
Alex Hillsd9b04d92016-04-11 16:38:16 -04005949 }
5950 }
5951 return messages;
5952 }
5953
5954 static Message getMessageFromBundle(Bundle bundle) {
5955 try {
Adrian Roosfbddd2c2016-05-13 12:57:20 -07005956 if (!bundle.containsKey(KEY_TEXT) || !bundle.containsKey(KEY_TIMESTAMP)) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04005957 return null;
5958 } else {
5959 Message message = new Message(bundle.getCharSequence(KEY_TEXT),
5960 bundle.getLong(KEY_TIMESTAMP), bundle.getCharSequence(KEY_SENDER));
5961 if (bundle.containsKey(KEY_DATA_MIME_TYPE) &&
5962 bundle.containsKey(KEY_DATA_URI)) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04005963 message.setData(bundle.getString(KEY_DATA_MIME_TYPE),
5964 (Uri) bundle.getParcelable(KEY_DATA_URI));
Alex Hillsfc737de2016-03-23 17:33:02 -04005965 }
Shane Brennan5a871862017-03-11 13:14:17 -08005966 if (bundle.containsKey(KEY_EXTRAS_BUNDLE)) {
5967 message.getExtras().putAll(bundle.getBundle(KEY_EXTRAS_BUNDLE));
5968 }
Alex Hillsd9b04d92016-04-11 16:38:16 -04005969 return message;
5970 }
5971 } catch (ClassCastException e) {
5972 return null;
5973 }
5974 }
Alex Hillsfc737de2016-03-23 17:33:02 -04005975 }
5976 }
5977
5978 /**
Daniel Sandler879c5e02012-04-17 16:46:51 -04005979 * Helper class for generating large-format notifications that include a list of (up to 5) strings.
Joe Malin8d40d042012-11-05 11:36:40 -08005980 *
Robert Ly91c5ce32014-06-08 15:37:00 -07005981 * Here's how you'd set the <code>InboxStyle</code> on a notification:
Daniel Sandler879c5e02012-04-17 16:46:51 -04005982 * <pre class="prettyprint">
Robert Ly91c5ce32014-06-08 15:37:00 -07005983 * Notification notif = new Notification.Builder(mContext)
5984 * .setContentTitle(&quot;5 New mails from &quot; + sender.toString())
5985 * .setContentText(subject)
5986 * .setSmallIcon(R.drawable.new_mail)
5987 * .setLargeIcon(aBitmap)
5988 * .setStyle(new Notification.InboxStyle()
5989 * .addLine(str1)
5990 * .addLine(str2)
5991 * .setContentTitle(&quot;&quot;)
5992 * .setSummaryText(&quot;+3 more&quot;))
5993 * .build();
Daniel Sandler879c5e02012-04-17 16:46:51 -04005994 * </pre>
Joe Malin8d40d042012-11-05 11:36:40 -08005995 *
Daniel Sandler879c5e02012-04-17 16:46:51 -04005996 * @see Notification#bigContentView
5997 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005998 public static class InboxStyle extends Style {
Daniel Sandler879c5e02012-04-17 16:46:51 -04005999 private ArrayList<CharSequence> mTexts = new ArrayList<CharSequence>(5);
6000
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006001 public InboxStyle() {
6002 }
6003
Adrian Roosf5faf9d2016-05-23 13:56:15 -07006004 /**
6005 * @deprecated use {@code InboxStyle()}.
6006 */
6007 @Deprecated
Daniel Sandler879c5e02012-04-17 16:46:51 -04006008 public InboxStyle(Builder builder) {
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006009 setBuilder(builder);
Daniel Sandler879c5e02012-04-17 16:46:51 -04006010 }
6011
Chris Wrend6297db2012-05-03 16:20:13 -04006012 /**
6013 * Overrides ContentTitle in the big form of the template.
6014 * This defaults to the value passed to setContentTitle().
6015 */
6016 public InboxStyle setBigContentTitle(CharSequence title) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04006017 internalSetBigContentTitle(safeCharSequence(title));
Chris Wrend6297db2012-05-03 16:20:13 -04006018 return this;
6019 }
6020
6021 /**
6022 * Set the first line of text after the detail section in the big form of the template.
6023 */
6024 public InboxStyle setSummaryText(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04006025 internalSetSummaryText(safeCharSequence(cs));
Chris Wrend6297db2012-05-03 16:20:13 -04006026 return this;
6027 }
6028
Chris Wren0bd664d2012-08-01 13:56:56 -04006029 /**
6030 * Append a line to the digest section of the Inbox notification.
6031 */
Daniel Sandler879c5e02012-04-17 16:46:51 -04006032 public InboxStyle addLine(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04006033 mTexts.add(safeCharSequence(cs));
Daniel Sandler879c5e02012-04-17 16:46:51 -04006034 return this;
6035 }
6036
Daniel Sandlerf45564e2013-04-15 15:05:08 -04006037 /**
6038 * @hide
6039 */
6040 public void addExtras(Bundle extras) {
6041 super.addExtras(extras);
Christoph Studer4600f9b2014-07-22 22:44:43 +02006042
Daniel Sandlerf45564e2013-04-15 15:05:08 -04006043 CharSequence[] a = new CharSequence[mTexts.size()];
6044 extras.putCharSequenceArray(EXTRA_TEXT_LINES, mTexts.toArray(a));
6045 }
6046
Christoph Studer4600f9b2014-07-22 22:44:43 +02006047 /**
6048 * @hide
6049 */
6050 @Override
6051 protected void restoreFromExtras(Bundle extras) {
6052 super.restoreFromExtras(extras);
6053
6054 mTexts.clear();
6055 if (extras.containsKey(EXTRA_TEXT_LINES)) {
6056 Collections.addAll(mTexts, extras.getCharSequenceArray(EXTRA_TEXT_LINES));
6057 }
6058 }
6059
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006060 /**
6061 * @hide
6062 */
6063 public RemoteViews makeBigContentView() {
Selim Cinekc848c3a2016-01-13 15:27:30 -08006064 // Remove the content text so it disappears unless you have a summary
Christoph Studer4600f9b2014-07-22 22:44:43 +02006065 // Nasty
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006066 CharSequence oldBuilderContentText = mBuilder.mN.extras.getCharSequence(EXTRA_TEXT);
6067 mBuilder.getAllExtras().putCharSequence(EXTRA_TEXT, null);
Christoph Studer4600f9b2014-07-22 22:44:43 +02006068
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01006069 RemoteViews contentView = getStandardView(mBuilder.getInboxLayoutResource());
Daniel Sandler619738c2012-06-07 16:33:08 -04006070
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006071 mBuilder.getAllExtras().putCharSequence(EXTRA_TEXT, oldBuilderContentText);
Christoph Studer4600f9b2014-07-22 22:44:43 +02006072
Chris Wrend6297db2012-05-03 16:20:13 -04006073 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 -04006074 R.id.inbox_text4, R.id.inbox_text5, R.id.inbox_text6};
Chris Wrend6297db2012-05-03 16:20:13 -04006075
Chris Wren4ed80d52012-05-17 09:30:03 -04006076 // Make sure all rows are gone in case we reuse a view.
6077 for (int rowId : rowIds) {
6078 contentView.setViewVisibility(rowId, View.GONE);
6079 }
6080
Daniel Sandler879c5e02012-04-17 16:46:51 -04006081 int i=0;
Selim Cinek07c80172016-04-21 16:40:47 -07006082 int topPadding = mBuilder.mContext.getResources().getDimensionPixelSize(
6083 R.dimen.notification_inbox_item_top_padding);
Selim Cinek247fa012016-02-18 09:50:48 -08006084 boolean first = true;
Selim Cinek07c80172016-04-21 16:40:47 -07006085 int onlyViewId = 0;
6086 int maxRows = rowIds.length;
6087 if (mBuilder.mActions.size() > 0) {
6088 maxRows--;
6089 }
6090 while (i < mTexts.size() && i < maxRows) {
Daniel Sandler879c5e02012-04-17 16:46:51 -04006091 CharSequence str = mTexts.get(i);
Selim Cinek07c80172016-04-21 16:40:47 -07006092 if (!TextUtils.isEmpty(str)) {
Daniel Sandler879c5e02012-04-17 16:46:51 -04006093 contentView.setViewVisibility(rowIds[i], View.VISIBLE);
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01006094 contentView.setTextViewText(rowIds[i], mBuilder.processLegacyText(str));
Selim Cinek7b9605b2017-01-19 17:36:00 -08006095 mBuilder.setTextViewColorSecondary(contentView, rowIds[i]);
Selim Cinek07c80172016-04-21 16:40:47 -07006096 contentView.setViewPadding(rowIds[i], 0, topPadding, 0, 0);
Selim Cinek247fa012016-02-18 09:50:48 -08006097 handleInboxImageMargin(contentView, rowIds[i], first);
Selim Cinek07c80172016-04-21 16:40:47 -07006098 if (first) {
6099 onlyViewId = rowIds[i];
6100 } else {
6101 onlyViewId = 0;
6102 }
Selim Cinek247fa012016-02-18 09:50:48 -08006103 first = false;
Daniel Sandler879c5e02012-04-17 16:46:51 -04006104 }
6105 i++;
6106 }
Selim Cinek07c80172016-04-21 16:40:47 -07006107 if (onlyViewId != 0) {
6108 // We only have 1 entry, lets make it look like the normal Text of a Bigtext
6109 topPadding = mBuilder.mContext.getResources().getDimensionPixelSize(
6110 R.dimen.notification_text_margin_top);
6111 contentView.setViewPadding(onlyViewId, 0, topPadding, 0, 0);
6112 }
Selim Cinek1e0bf612015-11-20 15:57:26 -08006113
Daniel Sandler879c5e02012-04-17 16:46:51 -04006114 return contentView;
6115 }
Selim Cinek1e0bf612015-11-20 15:57:26 -08006116
Selim Cinek247fa012016-02-18 09:50:48 -08006117 private void handleInboxImageMargin(RemoteViews contentView, int id, boolean first) {
Selim Cinek1e0bf612015-11-20 15:57:26 -08006118 int endMargin = 0;
Selim Cinek247fa012016-02-18 09:50:48 -08006119 if (first) {
6120 final int max = mBuilder.mN.extras.getInt(EXTRA_PROGRESS_MAX, 0);
6121 final boolean ind = mBuilder.mN.extras.getBoolean(EXTRA_PROGRESS_INDETERMINATE);
6122 boolean hasProgress = max != 0 || ind;
Selim Cinek279fa862016-06-14 10:57:25 -07006123 if (mBuilder.mN.hasLargeIcon() && !hasProgress) {
Adrian Roos2d5dbba2016-06-08 17:11:53 -07006124 endMargin = R.dimen.notification_content_picture_margin;
Selim Cinek247fa012016-02-18 09:50:48 -08006125 }
Selim Cinek1e0bf612015-11-20 15:57:26 -08006126 }
Adrian Roos2d5dbba2016-06-08 17:11:53 -07006127 contentView.setViewLayoutMarginEndDimen(id, endMargin);
Selim Cinek1e0bf612015-11-20 15:57:26 -08006128 }
Daniel Sandler879c5e02012-04-17 16:46:51 -04006129 }
Dan Sandler842dd772014-05-15 09:36:47 -04006130
6131 /**
6132 * Notification style for media playback notifications.
6133 *
6134 * In the expanded form, {@link Notification#bigContentView}, up to 5
6135 * {@link Notification.Action}s specified with
Dan Sandler86647982015-05-13 23:41:13 -04006136 * {@link Notification.Builder#addAction(Action) addAction} will be
Dan Sandler842dd772014-05-15 09:36:47 -04006137 * shown as icon-only pushbuttons, suitable for transport controls. The Bitmap given to
6138 * {@link Notification.Builder#setLargeIcon(android.graphics.Bitmap) setLargeIcon()} will be
6139 * treated as album artwork.
Selim Cinek99104832017-01-25 14:47:33 -08006140 * <p>
Dan Sandler842dd772014-05-15 09:36:47 -04006141 * Unlike the other styles provided here, MediaStyle can also modify the standard-size
6142 * {@link Notification#contentView}; by providing action indices to
Christoph Studerfde6f4d2014-12-12 13:23:26 +01006143 * {@link #setShowActionsInCompactView(int...)} you can promote up to 3 actions to be displayed
Dan Sandler842dd772014-05-15 09:36:47 -04006144 * in the standard view alongside the usual content.
Selim Cinek99104832017-01-25 14:47:33 -08006145 * <p>
Bryan Mawhinney6be8de32014-07-18 10:35:12 +01006146 * Notifications created with MediaStyle will have their category set to
6147 * {@link Notification#CATEGORY_TRANSPORT CATEGORY_TRANSPORT} unless you set a different
6148 * category using {@link Notification.Builder#setCategory(String) setCategory()}.
Selim Cinek99104832017-01-25 14:47:33 -08006149 * <p>
Jeff Browndba34ba2014-06-24 20:46:03 -07006150 * Finally, if you attach a {@link android.media.session.MediaSession.Token} using
6151 * {@link android.app.Notification.MediaStyle#setMediaSession(MediaSession.Token)},
Dan Sandler842dd772014-05-15 09:36:47 -04006152 * the System UI can identify this as a notification representing an active media session
6153 * and respond accordingly (by showing album artwork in the lockscreen, for example).
6154 *
Selim Cinek99104832017-01-25 14:47:33 -08006155 * <p>
6156 * Starting at {@link android.os.Build.VERSION_CODES#O Android O} any notification that has a
6157 * media session attached with {@link #setMediaSession(MediaSession.Token)} will be colorized.
6158 * You can opt-out of this behavior by using {@link Notification.Builder#setColorized(boolean)}.
6159 * <p>
6160 *
Dan Sandler842dd772014-05-15 09:36:47 -04006161 * To use this style with your Notification, feed it to
6162 * {@link Notification.Builder#setStyle(android.app.Notification.Style)} like so:
6163 * <pre class="prettyprint">
6164 * Notification noti = new Notification.Builder()
6165 * .setSmallIcon(R.drawable.ic_stat_player)
Christoph Studere935fe92014-11-24 14:18:06 +01006166 * .setContentTitle(&quot;Track title&quot;)
6167 * .setContentText(&quot;Artist - Album&quot;)
6168 * .setLargeIcon(albumArtBitmap))
Bryan Mawhinney6be8de32014-07-18 10:35:12 +01006169 * .setStyle(<b>new Notification.MediaStyle()</b>
6170 * .setMediaSession(mySession))
Dan Sandler842dd772014-05-15 09:36:47 -04006171 * .build();
6172 * </pre>
6173 *
6174 * @see Notification#bigContentView
Selim Cinek99104832017-01-25 14:47:33 -08006175 * @see Notification.Builder#setColorized(boolean)
Dan Sandler842dd772014-05-15 09:36:47 -04006176 */
6177 public static class MediaStyle extends Style {
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02006178 static final int MAX_MEDIA_BUTTONS_IN_COMPACT = 3;
Dan Sandler842dd772014-05-15 09:36:47 -04006179 static final int MAX_MEDIA_BUTTONS = 5;
6180
6181 private int[] mActionsToShowInCompact = null;
Jeff Browndba34ba2014-06-24 20:46:03 -07006182 private MediaSession.Token mToken;
Dan Sandler842dd772014-05-15 09:36:47 -04006183
6184 public MediaStyle() {
6185 }
6186
Adrian Roosf5faf9d2016-05-23 13:56:15 -07006187 /**
6188 * @deprecated use {@code MediaStyle()}.
6189 */
6190 @Deprecated
Dan Sandler842dd772014-05-15 09:36:47 -04006191 public MediaStyle(Builder builder) {
6192 setBuilder(builder);
6193 }
6194
6195 /**
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02006196 * 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 -04006197 * notification view.
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02006198 *
6199 * @param actions the indices of the actions to show in the compact notification view
Dan Sandler842dd772014-05-15 09:36:47 -04006200 */
6201 public MediaStyle setShowActionsInCompactView(int...actions) {
6202 mActionsToShowInCompact = actions;
6203 return this;
6204 }
6205
6206 /**
Jeff Browndba34ba2014-06-24 20:46:03 -07006207 * Attach a {@link android.media.session.MediaSession.Token} to this Notification
6208 * to provide additional playback information and control to the SystemUI.
Dan Sandler842dd772014-05-15 09:36:47 -04006209 */
Jeff Browndba34ba2014-06-24 20:46:03 -07006210 public MediaStyle setMediaSession(MediaSession.Token token) {
Dan Sandler842dd772014-05-15 09:36:47 -04006211 mToken = token;
6212 return this;
6213 }
6214
Christoph Studer4600f9b2014-07-22 22:44:43 +02006215 /**
6216 * @hide
6217 */
Dan Sandler842dd772014-05-15 09:36:47 -04006218 @Override
6219 public Notification buildStyled(Notification wip) {
Christoph Studer4600f9b2014-07-22 22:44:43 +02006220 super.buildStyled(wip);
Bryan Mawhinney6be8de32014-07-18 10:35:12 +01006221 if (wip.category == null) {
6222 wip.category = Notification.CATEGORY_TRANSPORT;
6223 }
Dan Sandler842dd772014-05-15 09:36:47 -04006224 return wip;
6225 }
6226
Christoph Studer4600f9b2014-07-22 22:44:43 +02006227 /**
6228 * @hide
6229 */
6230 @Override
Selim Cinek7d1009b2017-01-25 15:28:28 -08006231 public RemoteViews makeContentView(boolean increasedHeight) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006232 return makeMediaContentView();
Christoph Studer4600f9b2014-07-22 22:44:43 +02006233 }
6234
6235 /**
6236 * @hide
6237 */
6238 @Override
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006239 public RemoteViews makeBigContentView() {
6240 return makeMediaBigContentView();
Christoph Studer4600f9b2014-07-22 22:44:43 +02006241 }
6242
Selim Cinekcc10bfb2016-02-10 16:24:21 -08006243 /**
6244 * @hide
6245 */
6246 @Override
Selim Cinek87ed69b2017-02-09 15:59:43 -08006247 public RemoteViews makeHeadsUpContentView(boolean increasedHeight) {
Selim Cinekcc10bfb2016-02-10 16:24:21 -08006248 RemoteViews expanded = makeMediaBigContentView();
6249 return expanded != null ? expanded : makeMediaContentView();
6250 }
6251
Dan Sandler842dd772014-05-15 09:36:47 -04006252 /** @hide */
6253 @Override
6254 public void addExtras(Bundle extras) {
6255 super.addExtras(extras);
6256
6257 if (mToken != null) {
6258 extras.putParcelable(EXTRA_MEDIA_SESSION, mToken);
6259 }
Bryan Mawhinneye191f902014-07-22 12:50:09 +01006260 if (mActionsToShowInCompact != null) {
6261 extras.putIntArray(EXTRA_COMPACT_ACTIONS, mActionsToShowInCompact);
6262 }
Dan Sandler842dd772014-05-15 09:36:47 -04006263 }
6264
Christoph Studer4600f9b2014-07-22 22:44:43 +02006265 /**
6266 * @hide
6267 */
6268 @Override
6269 protected void restoreFromExtras(Bundle extras) {
6270 super.restoreFromExtras(extras);
6271
6272 if (extras.containsKey(EXTRA_MEDIA_SESSION)) {
6273 mToken = extras.getParcelable(EXTRA_MEDIA_SESSION);
6274 }
6275 if (extras.containsKey(EXTRA_COMPACT_ACTIONS)) {
6276 mActionsToShowInCompact = extras.getIntArray(EXTRA_COMPACT_ACTIONS);
6277 }
6278 }
6279
Selim Cinek5bf069a2015-11-10 19:14:27 -05006280 private RemoteViews generateMediaActionButton(Action action, int color) {
Dan Sandler842dd772014-05-15 09:36:47 -04006281 final boolean tombstone = (action.actionIntent == null);
Selim Cinekf33b1112015-07-15 17:45:11 -07006282 RemoteViews button = new BuilderRemoteViews(mBuilder.mContext.getApplicationInfo(),
Alan Viverette3cb07a462014-06-06 14:19:53 -07006283 R.layout.notification_material_media_action);
Dan Sandler68079d52015-07-22 10:45:30 -04006284 button.setImageViewIcon(R.id.action0, action.getIcon());
Selim Cinek5bf069a2015-11-10 19:14:27 -05006285 button.setDrawableParameters(R.id.action0, false, -1, color, PorterDuff.Mode.SRC_ATOP,
6286 -1);
Dan Sandler842dd772014-05-15 09:36:47 -04006287 if (!tombstone) {
6288 button.setOnClickPendingIntent(R.id.action0, action.actionIntent);
6289 }
6290 button.setContentDescription(R.id.action0, action.title);
6291 return button;
6292 }
6293
6294 private RemoteViews makeMediaContentView() {
6295 RemoteViews view = mBuilder.applyStandardTemplate(
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02006296 R.layout.notification_template_material_media, false /* hasProgress */);
Dan Sandler842dd772014-05-15 09:36:47 -04006297
6298 final int numActions = mBuilder.mActions.size();
6299 final int N = mActionsToShowInCompact == null
6300 ? 0
6301 : Math.min(mActionsToShowInCompact.length, MAX_MEDIA_BUTTONS_IN_COMPACT);
6302 if (N > 0) {
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02006303 view.removeAllViews(com.android.internal.R.id.media_actions);
Dan Sandler842dd772014-05-15 09:36:47 -04006304 for (int i = 0; i < N; i++) {
6305 if (i >= numActions) {
6306 throw new IllegalArgumentException(String.format(
6307 "setShowActionsInCompactView: action %d out of bounds (max %d)",
6308 i, numActions - 1));
6309 }
6310
6311 final Action action = mBuilder.mActions.get(mActionsToShowInCompact[i]);
Selim Cinek5bf069a2015-11-10 19:14:27 -05006312 final RemoteViews button = generateMediaActionButton(action,
Selim Cinek99104832017-01-25 14:47:33 -08006313 getPrimaryHighlightColor());
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02006314 view.addView(com.android.internal.R.id.media_actions, button);
Dan Sandler842dd772014-05-15 09:36:47 -04006315 }
6316 }
Selim Cinekfdc738f2016-01-27 20:04:27 -08006317 handleImage(view);
6318 // handle the content margin
Adrian Roos2d5dbba2016-06-08 17:11:53 -07006319 int endMargin = R.dimen.notification_content_margin_end;
Selim Cinek279fa862016-06-14 10:57:25 -07006320 if (mBuilder.mN.hasLargeIcon()) {
Adrian Roos2d5dbba2016-06-08 17:11:53 -07006321 endMargin = R.dimen.notification_content_plus_picture_margin_end;
Selim Cinekfdc738f2016-01-27 20:04:27 -08006322 }
Adrian Roos2d5dbba2016-06-08 17:11:53 -07006323 view.setViewLayoutMarginEndDimen(R.id.notification_main_column, endMargin);
Dan Sandler842dd772014-05-15 09:36:47 -04006324 return view;
6325 }
6326
Selim Cinek99104832017-01-25 14:47:33 -08006327 private int getPrimaryHighlightColor() {
6328 return mBuilder.getPrimaryHighlightColor();
6329 }
6330
Dan Sandler842dd772014-05-15 09:36:47 -04006331 private RemoteViews makeMediaBigContentView() {
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02006332 final int actionCount = Math.min(mBuilder.mActions.size(), MAX_MEDIA_BUTTONS);
Selim Cinekcc10bfb2016-02-10 16:24:21 -08006333 // Dont add an expanded view if there is no more content to be revealed
6334 int actionsInCompact = mActionsToShowInCompact == null
6335 ? 0
6336 : Math.min(mActionsToShowInCompact.length, MAX_MEDIA_BUTTONS_IN_COMPACT);
Selim Cinek279fa862016-06-14 10:57:25 -07006337 if (!mBuilder.mN.hasLargeIcon() && actionCount <= actionsInCompact) {
Selim Cinekcc10bfb2016-02-10 16:24:21 -08006338 return null;
6339 }
Selim Cinek5bf069a2015-11-10 19:14:27 -05006340 RemoteViews big = mBuilder.applyStandardTemplate(
6341 R.layout.notification_template_material_big_media,
6342 false);
Dan Sandler842dd772014-05-15 09:36:47 -04006343
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02006344 if (actionCount > 0) {
6345 big.removeAllViews(com.android.internal.R.id.media_actions);
6346 for (int i = 0; i < actionCount; i++) {
Selim Cinek5bf069a2015-11-10 19:14:27 -05006347 final RemoteViews button = generateMediaActionButton(mBuilder.mActions.get(i),
Selim Cinek99104832017-01-25 14:47:33 -08006348 getPrimaryHighlightColor());
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02006349 big.addView(com.android.internal.R.id.media_actions, button);
Dan Sandler842dd772014-05-15 09:36:47 -04006350 }
6351 }
Selim Cinek5bf069a2015-11-10 19:14:27 -05006352 handleImage(big);
Dan Sandler842dd772014-05-15 09:36:47 -04006353 return big;
6354 }
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02006355
Selim Cinek5bf069a2015-11-10 19:14:27 -05006356 private void handleImage(RemoteViews contentView) {
Selim Cinek279fa862016-06-14 10:57:25 -07006357 if (mBuilder.mN.hasLargeIcon()) {
Adrian Roos2d5dbba2016-06-08 17:11:53 -07006358 contentView.setViewLayoutMarginEndDimen(R.id.line1, 0);
6359 contentView.setViewLayoutMarginEndDimen(R.id.text, 0);
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02006360 }
6361 }
6362
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02006363 /**
6364 * @hide
6365 */
6366 @Override
6367 protected boolean hasProgress() {
6368 return false;
6369 }
Dan Sandler842dd772014-05-15 09:36:47 -04006370 }
Griff Hazen61a9e862014-05-22 16:05:19 -07006371
Selim Cinek593610c2016-02-16 18:42:57 -08006372 /**
6373 * Notification style for custom views that are decorated by the system
6374 *
6375 * <p>Instead of providing a notification that is completely custom, a developer can set this
6376 * style and still obtain system decorations like the notification header with the expand
6377 * affordance and actions.
6378 *
6379 * <p>Use {@link android.app.Notification.Builder#setCustomContentView(RemoteViews)},
6380 * {@link android.app.Notification.Builder#setCustomBigContentView(RemoteViews)} and
6381 * {@link android.app.Notification.Builder#setCustomHeadsUpContentView(RemoteViews)} to set the
6382 * corresponding custom views to display.
6383 *
6384 * To use this style with your Notification, feed it to
6385 * {@link Notification.Builder#setStyle(android.app.Notification.Style)} like so:
6386 * <pre class="prettyprint">
6387 * Notification noti = new Notification.Builder()
6388 * .setSmallIcon(R.drawable.ic_stat_player)
6389 * .setLargeIcon(albumArtBitmap))
6390 * .setCustomContentView(contentView);
6391 * .setStyle(<b>new Notification.DecoratedCustomViewStyle()</b>)
6392 * .build();
6393 * </pre>
6394 */
6395 public static class DecoratedCustomViewStyle extends Style {
6396
6397 public DecoratedCustomViewStyle() {
6398 }
6399
Selim Cinek593610c2016-02-16 18:42:57 -08006400 /**
6401 * @hide
6402 */
6403 public boolean displayCustomViewInline() {
6404 return true;
6405 }
6406
6407 /**
6408 * @hide
6409 */
6410 @Override
Selim Cinek7d1009b2017-01-25 15:28:28 -08006411 public RemoteViews makeContentView(boolean increasedHeight) {
Selim Cinek593610c2016-02-16 18:42:57 -08006412 return makeStandardTemplateWithCustomContent(mBuilder.mN.contentView);
6413 }
6414
6415 /**
6416 * @hide
6417 */
6418 @Override
6419 public RemoteViews makeBigContentView() {
6420 return makeDecoratedBigContentView();
6421 }
6422
6423 /**
6424 * @hide
6425 */
6426 @Override
Selim Cinek87ed69b2017-02-09 15:59:43 -08006427 public RemoteViews makeHeadsUpContentView(boolean increasedHeight) {
Selim Cinek593610c2016-02-16 18:42:57 -08006428 return makeDecoratedHeadsUpContentView();
6429 }
6430
Selim Cinek593610c2016-02-16 18:42:57 -08006431 private RemoteViews makeDecoratedHeadsUpContentView() {
6432 RemoteViews headsUpContentView = mBuilder.mN.headsUpContentView == null
6433 ? mBuilder.mN.contentView
6434 : mBuilder.mN.headsUpContentView;
6435 if (mBuilder.mActions.size() == 0) {
6436 return makeStandardTemplateWithCustomContent(headsUpContentView);
6437 }
6438 RemoteViews remoteViews = mBuilder.applyStandardTemplateWithActions(
6439 mBuilder.getBigBaseLayoutResource());
Selim Cinek247fa012016-02-18 09:50:48 -08006440 buildIntoRemoteViewContent(remoteViews, headsUpContentView);
Selim Cinek593610c2016-02-16 18:42:57 -08006441 return remoteViews;
6442 }
6443
Selim Cinek593610c2016-02-16 18:42:57 -08006444 private RemoteViews makeStandardTemplateWithCustomContent(RemoteViews customContent) {
6445 RemoteViews remoteViews = mBuilder.applyStandardTemplate(
6446 mBuilder.getBaseLayoutResource());
Selim Cinek247fa012016-02-18 09:50:48 -08006447 buildIntoRemoteViewContent(remoteViews, customContent);
Selim Cinek593610c2016-02-16 18:42:57 -08006448 return remoteViews;
6449 }
6450
Selim Cinek593610c2016-02-16 18:42:57 -08006451 private RemoteViews makeDecoratedBigContentView() {
6452 RemoteViews bigContentView = mBuilder.mN.bigContentView == null
6453 ? mBuilder.mN.contentView
6454 : mBuilder.mN.bigContentView;
6455 if (mBuilder.mActions.size() == 0) {
6456 return makeStandardTemplateWithCustomContent(bigContentView);
6457 }
6458 RemoteViews remoteViews = mBuilder.applyStandardTemplateWithActions(
6459 mBuilder.getBigBaseLayoutResource());
Selim Cinek247fa012016-02-18 09:50:48 -08006460 buildIntoRemoteViewContent(remoteViews, bigContentView);
Selim Cinek593610c2016-02-16 18:42:57 -08006461 return remoteViews;
6462 }
Selim Cinek247fa012016-02-18 09:50:48 -08006463
6464 private void buildIntoRemoteViewContent(RemoteViews remoteViews,
6465 RemoteViews customContent) {
Adrian Roos5081c0d2016-02-26 16:04:19 -08006466 if (customContent != null) {
Selim Cinekf91017e2016-03-14 12:25:09 -07006467 // Need to clone customContent before adding, because otherwise it can no longer be
6468 // parceled independently of remoteViews.
Adrian Roos5081c0d2016-02-26 16:04:19 -08006469 customContent = customContent.clone();
Selim Cinekf91017e2016-03-14 12:25:09 -07006470 remoteViews.removeAllViews(R.id.notification_main_column);
6471 remoteViews.addView(R.id.notification_main_column, customContent);
Adrian Roos5081c0d2016-02-26 16:04:19 -08006472 }
Selim Cinek247fa012016-02-18 09:50:48 -08006473 // also update the end margin if there is an image
Adrian Roos2d5dbba2016-06-08 17:11:53 -07006474 int endMargin = R.dimen.notification_content_margin_end;
Selim Cinek279fa862016-06-14 10:57:25 -07006475 if (mBuilder.mN.hasLargeIcon()) {
Adrian Roos2d5dbba2016-06-08 17:11:53 -07006476 endMargin = R.dimen.notification_content_plus_picture_margin_end;
Selim Cinek247fa012016-02-18 09:50:48 -08006477 }
Adrian Roos2d5dbba2016-06-08 17:11:53 -07006478 remoteViews.setViewLayoutMarginEndDimen(R.id.notification_main_column, endMargin);
Selim Cinek247fa012016-02-18 09:50:48 -08006479 }
Selim Cinek593610c2016-02-16 18:42:57 -08006480 }
6481
Selim Cinek03eb3b72016-02-18 10:39:45 -08006482 /**
6483 * Notification style for media custom views that are decorated by the system
6484 *
6485 * <p>Instead of providing a media notification that is completely custom, a developer can set
6486 * this style and still obtain system decorations like the notification header with the expand
6487 * affordance and actions.
6488 *
6489 * <p>Use {@link android.app.Notification.Builder#setCustomContentView(RemoteViews)},
6490 * {@link android.app.Notification.Builder#setCustomBigContentView(RemoteViews)} and
6491 * {@link android.app.Notification.Builder#setCustomHeadsUpContentView(RemoteViews)} to set the
6492 * corresponding custom views to display.
Selim Cinek99104832017-01-25 14:47:33 -08006493 * <p>
6494 * Contrary to {@link MediaStyle} a developer has to opt-in to the colorizing of the
6495 * notification by using {@link Notification.Builder#setColorized(boolean)}.
6496 * <p>
Selim Cinek03eb3b72016-02-18 10:39:45 -08006497 * To use this style with your Notification, feed it to
6498 * {@link Notification.Builder#setStyle(android.app.Notification.Style)} like so:
6499 * <pre class="prettyprint">
6500 * Notification noti = new Notification.Builder()
6501 * .setSmallIcon(R.drawable.ic_stat_player)
6502 * .setLargeIcon(albumArtBitmap))
6503 * .setCustomContentView(contentView);
6504 * .setStyle(<b>new Notification.DecoratedMediaCustomViewStyle()</b>
6505 * .setMediaSession(mySession))
6506 * .build();
6507 * </pre>
6508 *
6509 * @see android.app.Notification.DecoratedCustomViewStyle
6510 * @see android.app.Notification.MediaStyle
6511 */
6512 public static class DecoratedMediaCustomViewStyle extends MediaStyle {
6513
6514 public DecoratedMediaCustomViewStyle() {
6515 }
6516
Selim Cinek03eb3b72016-02-18 10:39:45 -08006517 /**
6518 * @hide
6519 */
6520 public boolean displayCustomViewInline() {
6521 return true;
6522 }
6523
6524 /**
6525 * @hide
6526 */
6527 @Override
Selim Cinek7d1009b2017-01-25 15:28:28 -08006528 public RemoteViews makeContentView(boolean increasedHeight) {
6529 RemoteViews remoteViews = super.makeContentView(false /* increasedHeight */);
Selim Cinek03eb3b72016-02-18 10:39:45 -08006530 return buildIntoRemoteView(remoteViews, R.id.notification_content_container,
6531 mBuilder.mN.contentView);
6532 }
6533
6534 /**
6535 * @hide
6536 */
6537 @Override
6538 public RemoteViews makeBigContentView() {
6539 RemoteViews customRemoteView = mBuilder.mN.bigContentView != null
6540 ? mBuilder.mN.bigContentView
6541 : mBuilder.mN.contentView;
6542 return makeBigContentViewWithCustomContent(customRemoteView);
6543 }
6544
6545 private RemoteViews makeBigContentViewWithCustomContent(RemoteViews customRemoteView) {
6546 RemoteViews remoteViews = super.makeBigContentView();
6547 if (remoteViews != null) {
6548 return buildIntoRemoteView(remoteViews, R.id.notification_main_column,
6549 customRemoteView);
6550 } else if (customRemoteView != mBuilder.mN.contentView){
Selim Cinek7d1009b2017-01-25 15:28:28 -08006551 remoteViews = super.makeContentView(false /* increasedHeight */);
Selim Cinek03eb3b72016-02-18 10:39:45 -08006552 return buildIntoRemoteView(remoteViews, R.id.notification_content_container,
6553 customRemoteView);
6554 } else {
6555 return null;
6556 }
6557 }
6558
6559 /**
6560 * @hide
6561 */
6562 @Override
Selim Cinek87ed69b2017-02-09 15:59:43 -08006563 public RemoteViews makeHeadsUpContentView(boolean increasedHeight) {
Selim Cinek03eb3b72016-02-18 10:39:45 -08006564 RemoteViews customRemoteView = mBuilder.mN.headsUpContentView != null
6565 ? mBuilder.mN.headsUpContentView
6566 : mBuilder.mN.contentView;
6567 return makeBigContentViewWithCustomContent(customRemoteView);
6568 }
6569
6570 private RemoteViews buildIntoRemoteView(RemoteViews remoteViews, int id,
6571 RemoteViews customContent) {
Adrian Roos5081c0d2016-02-26 16:04:19 -08006572 if (customContent != null) {
Selim Cinekf91017e2016-03-14 12:25:09 -07006573 // Need to clone customContent before adding, because otherwise it can no longer be
6574 // parceled independently of remoteViews.
Adrian Roos5081c0d2016-02-26 16:04:19 -08006575 customContent = customContent.clone();
Selim Cinekf91017e2016-03-14 12:25:09 -07006576 remoteViews.removeAllViews(id);
6577 remoteViews.addView(id, customContent);
Adrian Roos5081c0d2016-02-26 16:04:19 -08006578 }
Selim Cinek03eb3b72016-02-18 10:39:45 -08006579 return remoteViews;
6580 }
6581 }
6582
Christoph Studer4600f9b2014-07-22 22:44:43 +02006583 // When adding a new Style subclass here, don't forget to update
6584 // Builder.getNotificationStyleClass.
6585
Griff Hazen61a9e862014-05-22 16:05:19 -07006586 /**
6587 * Extender interface for use with {@link Builder#extend}. Extenders may be used to add
6588 * metadata or change options on a notification builder.
6589 */
6590 public interface Extender {
6591 /**
6592 * Apply this extender to a notification builder.
6593 * @param builder the builder to be modified.
6594 * @return the build object for chaining.
6595 */
6596 public Builder extend(Builder builder);
6597 }
6598
6599 /**
6600 * Helper class to add wearable extensions to notifications.
6601 * <p class="note"> See
6602 * <a href="{@docRoot}wear/notifications/creating.html">Creating Notifications
6603 * for Android Wear</a> for more information on how to use this class.
6604 * <p>
6605 * To create a notification with wearable extensions:
6606 * <ol>
6607 * <li>Create a {@link android.app.Notification.Builder}, setting any desired
6608 * properties.
6609 * <li>Create a {@link android.app.Notification.WearableExtender}.
6610 * <li>Set wearable-specific properties using the
6611 * {@code add} and {@code set} methods of {@link android.app.Notification.WearableExtender}.
6612 * <li>Call {@link android.app.Notification.Builder#extend} to apply the extensions to a
6613 * notification.
6614 * <li>Post the notification to the notification system with the
6615 * {@code NotificationManager.notify(...)} methods.
6616 * </ol>
6617 *
6618 * <pre class="prettyprint">
6619 * Notification notif = new Notification.Builder(mContext)
6620 * .setContentTitle(&quot;New mail from &quot; + sender.toString())
6621 * .setContentText(subject)
6622 * .setSmallIcon(R.drawable.new_mail)
6623 * .extend(new Notification.WearableExtender()
6624 * .setContentIcon(R.drawable.new_mail))
6625 * .build();
6626 * NotificationManager notificationManger =
6627 * (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
6628 * notificationManger.notify(0, notif);</pre>
6629 *
6630 * <p>Wearable extensions can be accessed on an existing notification by using the
6631 * {@code WearableExtender(Notification)} constructor,
6632 * and then using the {@code get} methods to access values.
6633 *
6634 * <pre class="prettyprint">
6635 * Notification.WearableExtender wearableExtender = new Notification.WearableExtender(
6636 * notification);
Griff Hazen14f57992014-05-26 09:07:14 -07006637 * List&lt;Notification&gt; pages = wearableExtender.getPages();</pre>
Griff Hazen61a9e862014-05-22 16:05:19 -07006638 */
6639 public static final class WearableExtender implements Extender {
6640 /**
6641 * Sentinel value for an action index that is unset.
6642 */
6643 public static final int UNSET_ACTION_INDEX = -1;
6644
6645 /**
6646 * Size value for use with {@link #setCustomSizePreset} to show this notification with
6647 * default sizing.
6648 * <p>For custom display notifications created using {@link #setDisplayIntent},
Paul Soulosaa4f4bf2015-08-04 11:59:45 -07006649 * the default is {@link #SIZE_MEDIUM}. All other notifications size automatically based
Griff Hazen61a9e862014-05-22 16:05:19 -07006650 * on their content.
6651 */
6652 public static final int SIZE_DEFAULT = 0;
6653
6654 /**
6655 * Size value for use with {@link #setCustomSizePreset} to show this notification
6656 * with an extra small size.
6657 * <p>This value is only applicable for custom display notifications created using
6658 * {@link #setDisplayIntent}.
6659 */
6660 public static final int SIZE_XSMALL = 1;
6661
6662 /**
6663 * Size value for use with {@link #setCustomSizePreset} to show this notification
6664 * with a small size.
6665 * <p>This value is only applicable for custom display notifications created using
6666 * {@link #setDisplayIntent}.
6667 */
6668 public static final int SIZE_SMALL = 2;
6669
6670 /**
6671 * Size value for use with {@link #setCustomSizePreset} to show this notification
6672 * with a medium size.
6673 * <p>This value is only applicable for custom display notifications created using
6674 * {@link #setDisplayIntent}.
6675 */
6676 public static final int SIZE_MEDIUM = 3;
6677
6678 /**
6679 * Size value for use with {@link #setCustomSizePreset} to show this notification
6680 * with a large size.
6681 * <p>This value is only applicable for custom display notifications created using
6682 * {@link #setDisplayIntent}.
6683 */
6684 public static final int SIZE_LARGE = 4;
6685
Griff Hazend5f11f92014-05-27 15:40:09 -07006686 /**
6687 * Size value for use with {@link #setCustomSizePreset} to show this notification
6688 * full screen.
6689 * <p>This value is only applicable for custom display notifications created using
6690 * {@link #setDisplayIntent}.
6691 */
6692 public static final int SIZE_FULL_SCREEN = 5;
6693
Griff Hazen5f2edfc2014-09-29 16:28:44 -07006694 /**
6695 * Sentinel value for use with {@link #setHintScreenTimeout} to keep the screen on for a
6696 * short amount of time when this notification is displayed on the screen. This
6697 * is the default value.
6698 */
6699 public static final int SCREEN_TIMEOUT_SHORT = 0;
6700
6701 /**
6702 * Sentinel value for use with {@link #setHintScreenTimeout} to keep the screen on
6703 * for a longer amount of time when this notification is displayed on the screen.
6704 */
6705 public static final int SCREEN_TIMEOUT_LONG = -1;
6706
Griff Hazen61a9e862014-05-22 16:05:19 -07006707 /** Notification extra which contains wearable extensions */
6708 private static final String EXTRA_WEARABLE_EXTENSIONS = "android.wearable.EXTENSIONS";
6709
Pete Gastaf6781d2014-10-07 15:17:05 -04006710 // Keys within EXTRA_WEARABLE_EXTENSIONS for wearable options.
Griff Hazen61a9e862014-05-22 16:05:19 -07006711 private static final String KEY_ACTIONS = "actions";
6712 private static final String KEY_FLAGS = "flags";
6713 private static final String KEY_DISPLAY_INTENT = "displayIntent";
6714 private static final String KEY_PAGES = "pages";
6715 private static final String KEY_BACKGROUND = "background";
6716 private static final String KEY_CONTENT_ICON = "contentIcon";
6717 private static final String KEY_CONTENT_ICON_GRAVITY = "contentIconGravity";
6718 private static final String KEY_CONTENT_ACTION_INDEX = "contentActionIndex";
6719 private static final String KEY_CUSTOM_SIZE_PRESET = "customSizePreset";
6720 private static final String KEY_CUSTOM_CONTENT_HEIGHT = "customContentHeight";
6721 private static final String KEY_GRAVITY = "gravity";
Griff Hazen5f2edfc2014-09-29 16:28:44 -07006722 private static final String KEY_HINT_SCREEN_TIMEOUT = "hintScreenTimeout";
Nadia Benbernou948627e2016-04-14 14:41:08 -04006723 private static final String KEY_DISMISSAL_ID = "dismissalId";
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04006724 private static final String KEY_BRIDGE_TAG = "bridgeTag";
Griff Hazen61a9e862014-05-22 16:05:19 -07006725
6726 // Flags bitwise-ored to mFlags
6727 private static final int FLAG_CONTENT_INTENT_AVAILABLE_OFFLINE = 0x1;
6728 private static final int FLAG_HINT_HIDE_ICON = 1 << 1;
6729 private static final int FLAG_HINT_SHOW_BACKGROUND_ONLY = 1 << 2;
6730 private static final int FLAG_START_SCROLL_BOTTOM = 1 << 3;
Griff Hazen5f2edfc2014-09-29 16:28:44 -07006731 private static final int FLAG_HINT_AVOID_BACKGROUND_CLIPPING = 1 << 4;
Alex Hills4bcb06b2016-04-05 14:26:25 -04006732 private static final int FLAG_BIG_PICTURE_AMBIENT = 1 << 5;
Alex Hills9ab3a232016-04-05 14:54:56 -04006733 private static final int FLAG_HINT_CONTENT_INTENT_LAUNCHES_ACTIVITY = 1 << 6;
Griff Hazen61a9e862014-05-22 16:05:19 -07006734
6735 // Default value for flags integer
6736 private static final int DEFAULT_FLAGS = FLAG_CONTENT_INTENT_AVAILABLE_OFFLINE;
6737
6738 private static final int DEFAULT_CONTENT_ICON_GRAVITY = Gravity.END;
6739 private static final int DEFAULT_GRAVITY = Gravity.BOTTOM;
6740
6741 private ArrayList<Action> mActions = new ArrayList<Action>();
6742 private int mFlags = DEFAULT_FLAGS;
6743 private PendingIntent mDisplayIntent;
6744 private ArrayList<Notification> mPages = new ArrayList<Notification>();
6745 private Bitmap mBackground;
6746 private int mContentIcon;
6747 private int mContentIconGravity = DEFAULT_CONTENT_ICON_GRAVITY;
6748 private int mContentActionIndex = UNSET_ACTION_INDEX;
6749 private int mCustomSizePreset = SIZE_DEFAULT;
6750 private int mCustomContentHeight;
6751 private int mGravity = DEFAULT_GRAVITY;
Griff Hazen5f2edfc2014-09-29 16:28:44 -07006752 private int mHintScreenTimeout;
Nadia Benbernou948627e2016-04-14 14:41:08 -04006753 private String mDismissalId;
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04006754 private String mBridgeTag;
Griff Hazen61a9e862014-05-22 16:05:19 -07006755
6756 /**
6757 * Create a {@link android.app.Notification.WearableExtender} with default
6758 * options.
6759 */
6760 public WearableExtender() {
6761 }
6762
6763 public WearableExtender(Notification notif) {
6764 Bundle wearableBundle = notif.extras.getBundle(EXTRA_WEARABLE_EXTENSIONS);
6765 if (wearableBundle != null) {
6766 List<Action> actions = wearableBundle.getParcelableArrayList(KEY_ACTIONS);
6767 if (actions != null) {
6768 mActions.addAll(actions);
6769 }
6770
6771 mFlags = wearableBundle.getInt(KEY_FLAGS, DEFAULT_FLAGS);
6772 mDisplayIntent = wearableBundle.getParcelable(KEY_DISPLAY_INTENT);
6773
6774 Notification[] pages = getNotificationArrayFromBundle(
6775 wearableBundle, KEY_PAGES);
6776 if (pages != null) {
6777 Collections.addAll(mPages, pages);
6778 }
6779
6780 mBackground = wearableBundle.getParcelable(KEY_BACKGROUND);
6781 mContentIcon = wearableBundle.getInt(KEY_CONTENT_ICON);
6782 mContentIconGravity = wearableBundle.getInt(KEY_CONTENT_ICON_GRAVITY,
6783 DEFAULT_CONTENT_ICON_GRAVITY);
6784 mContentActionIndex = wearableBundle.getInt(KEY_CONTENT_ACTION_INDEX,
6785 UNSET_ACTION_INDEX);
6786 mCustomSizePreset = wearableBundle.getInt(KEY_CUSTOM_SIZE_PRESET,
6787 SIZE_DEFAULT);
6788 mCustomContentHeight = wearableBundle.getInt(KEY_CUSTOM_CONTENT_HEIGHT);
6789 mGravity = wearableBundle.getInt(KEY_GRAVITY, DEFAULT_GRAVITY);
Griff Hazen5f2edfc2014-09-29 16:28:44 -07006790 mHintScreenTimeout = wearableBundle.getInt(KEY_HINT_SCREEN_TIMEOUT);
Nadia Benbernou948627e2016-04-14 14:41:08 -04006791 mDismissalId = wearableBundle.getString(KEY_DISMISSAL_ID);
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04006792 mBridgeTag = wearableBundle.getString(KEY_BRIDGE_TAG);
Griff Hazen61a9e862014-05-22 16:05:19 -07006793 }
6794 }
6795
6796 /**
6797 * Apply wearable extensions to a notification that is being built. This is typically
6798 * called by the {@link android.app.Notification.Builder#extend} method of
6799 * {@link android.app.Notification.Builder}.
6800 */
6801 @Override
6802 public Notification.Builder extend(Notification.Builder builder) {
6803 Bundle wearableBundle = new Bundle();
6804
6805 if (!mActions.isEmpty()) {
6806 wearableBundle.putParcelableArrayList(KEY_ACTIONS, mActions);
6807 }
6808 if (mFlags != DEFAULT_FLAGS) {
6809 wearableBundle.putInt(KEY_FLAGS, mFlags);
6810 }
6811 if (mDisplayIntent != null) {
6812 wearableBundle.putParcelable(KEY_DISPLAY_INTENT, mDisplayIntent);
6813 }
6814 if (!mPages.isEmpty()) {
6815 wearableBundle.putParcelableArray(KEY_PAGES, mPages.toArray(
6816 new Notification[mPages.size()]));
6817 }
6818 if (mBackground != null) {
6819 wearableBundle.putParcelable(KEY_BACKGROUND, mBackground);
6820 }
6821 if (mContentIcon != 0) {
6822 wearableBundle.putInt(KEY_CONTENT_ICON, mContentIcon);
6823 }
6824 if (mContentIconGravity != DEFAULT_CONTENT_ICON_GRAVITY) {
6825 wearableBundle.putInt(KEY_CONTENT_ICON_GRAVITY, mContentIconGravity);
6826 }
6827 if (mContentActionIndex != UNSET_ACTION_INDEX) {
6828 wearableBundle.putInt(KEY_CONTENT_ACTION_INDEX,
6829 mContentActionIndex);
6830 }
6831 if (mCustomSizePreset != SIZE_DEFAULT) {
6832 wearableBundle.putInt(KEY_CUSTOM_SIZE_PRESET, mCustomSizePreset);
6833 }
6834 if (mCustomContentHeight != 0) {
6835 wearableBundle.putInt(KEY_CUSTOM_CONTENT_HEIGHT, mCustomContentHeight);
6836 }
6837 if (mGravity != DEFAULT_GRAVITY) {
6838 wearableBundle.putInt(KEY_GRAVITY, mGravity);
6839 }
Griff Hazen5f2edfc2014-09-29 16:28:44 -07006840 if (mHintScreenTimeout != 0) {
6841 wearableBundle.putInt(KEY_HINT_SCREEN_TIMEOUT, mHintScreenTimeout);
6842 }
Nadia Benbernou948627e2016-04-14 14:41:08 -04006843 if (mDismissalId != null) {
6844 wearableBundle.putString(KEY_DISMISSAL_ID, mDismissalId);
6845 }
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04006846 if (mBridgeTag != null) {
6847 wearableBundle.putString(KEY_BRIDGE_TAG, mBridgeTag);
6848 }
Griff Hazen61a9e862014-05-22 16:05:19 -07006849
6850 builder.getExtras().putBundle(EXTRA_WEARABLE_EXTENSIONS, wearableBundle);
6851 return builder;
6852 }
6853
6854 @Override
6855 public WearableExtender clone() {
6856 WearableExtender that = new WearableExtender();
6857 that.mActions = new ArrayList<Action>(this.mActions);
6858 that.mFlags = this.mFlags;
6859 that.mDisplayIntent = this.mDisplayIntent;
6860 that.mPages = new ArrayList<Notification>(this.mPages);
6861 that.mBackground = this.mBackground;
6862 that.mContentIcon = this.mContentIcon;
6863 that.mContentIconGravity = this.mContentIconGravity;
6864 that.mContentActionIndex = this.mContentActionIndex;
6865 that.mCustomSizePreset = this.mCustomSizePreset;
6866 that.mCustomContentHeight = this.mCustomContentHeight;
6867 that.mGravity = this.mGravity;
Griff Hazen5f2edfc2014-09-29 16:28:44 -07006868 that.mHintScreenTimeout = this.mHintScreenTimeout;
Nadia Benbernou948627e2016-04-14 14:41:08 -04006869 that.mDismissalId = this.mDismissalId;
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04006870 that.mBridgeTag = this.mBridgeTag;
Griff Hazen61a9e862014-05-22 16:05:19 -07006871 return that;
6872 }
6873
6874 /**
6875 * Add a wearable action to this notification.
6876 *
6877 * <p>When wearable actions are added using this method, the set of actions that
6878 * show on a wearable device splits from devices that only show actions added
6879 * using {@link android.app.Notification.Builder#addAction}. This allows for customization
6880 * of which actions display on different devices.
6881 *
6882 * @param action the action to add to this notification
6883 * @return this object for method chaining
6884 * @see android.app.Notification.Action
6885 */
6886 public WearableExtender addAction(Action action) {
6887 mActions.add(action);
6888 return this;
6889 }
6890
6891 /**
6892 * Adds wearable actions to this notification.
6893 *
6894 * <p>When wearable actions are added using this method, the set of actions that
6895 * show on a wearable device splits from devices that only show actions added
6896 * using {@link android.app.Notification.Builder#addAction}. This allows for customization
6897 * of which actions display on different devices.
6898 *
6899 * @param actions the actions to add to this notification
6900 * @return this object for method chaining
6901 * @see android.app.Notification.Action
6902 */
6903 public WearableExtender addActions(List<Action> actions) {
6904 mActions.addAll(actions);
6905 return this;
6906 }
6907
6908 /**
6909 * Clear all wearable actions present on this builder.
6910 * @return this object for method chaining.
6911 * @see #addAction
6912 */
6913 public WearableExtender clearActions() {
6914 mActions.clear();
6915 return this;
6916 }
6917
6918 /**
6919 * Get the wearable actions present on this notification.
6920 */
6921 public List<Action> getActions() {
6922 return mActions;
6923 }
6924
6925 /**
6926 * Set an intent to launch inside of an activity view when displaying
Griff Hazen14f57992014-05-26 09:07:14 -07006927 * this notification. The {@link PendingIntent} provided should be for an activity.
6928 *
6929 * <pre class="prettyprint">
6930 * Intent displayIntent = new Intent(context, MyDisplayActivity.class);
6931 * PendingIntent displayPendingIntent = PendingIntent.getActivity(context,
6932 * 0, displayIntent, PendingIntent.FLAG_UPDATE_CURRENT);
6933 * Notification notif = new Notification.Builder(context)
6934 * .extend(new Notification.WearableExtender()
6935 * .setDisplayIntent(displayPendingIntent)
6936 * .setCustomSizePreset(Notification.WearableExtender.SIZE_MEDIUM))
6937 * .build();</pre>
6938 *
6939 * <p>The activity to launch needs to allow embedding, must be exported, and
Griff Hazen831ca9d2014-06-17 00:38:38 -07006940 * should have an empty task affinity. It is also recommended to use the device
6941 * default light theme.
Griff Hazen14f57992014-05-26 09:07:14 -07006942 *
6943 * <p>Example AndroidManifest.xml entry:
6944 * <pre class="prettyprint">
6945 * &lt;activity android:name=&quot;com.example.MyDisplayActivity&quot;
6946 * android:exported=&quot;true&quot;
6947 * android:allowEmbedded=&quot;true&quot;
Griff Hazen831ca9d2014-06-17 00:38:38 -07006948 * android:taskAffinity=&quot;&quot;
6949 * android:theme=&quot;@android:style/Theme.DeviceDefault.Light&quot; /&gt;</pre>
Griff Hazen61a9e862014-05-22 16:05:19 -07006950 *
6951 * @param intent the {@link PendingIntent} for an activity
6952 * @return this object for method chaining
6953 * @see android.app.Notification.WearableExtender#getDisplayIntent
6954 */
6955 public WearableExtender setDisplayIntent(PendingIntent intent) {
6956 mDisplayIntent = intent;
6957 return this;
6958 }
6959
6960 /**
6961 * Get the intent to launch inside of an activity view when displaying this
6962 * notification. This {@code PendingIntent} should be for an activity.
6963 */
6964 public PendingIntent getDisplayIntent() {
6965 return mDisplayIntent;
6966 }
6967
6968 /**
6969 * Add an additional page of content to display with this notification. The current
6970 * notification forms the first page, and pages added using this function form
6971 * subsequent pages. This field can be used to separate a notification into multiple
6972 * sections.
6973 *
6974 * @param page the notification to add as another page
6975 * @return this object for method chaining
6976 * @see android.app.Notification.WearableExtender#getPages
6977 */
6978 public WearableExtender addPage(Notification page) {
6979 mPages.add(page);
6980 return this;
6981 }
6982
6983 /**
6984 * Add additional pages of content to display with this notification. The current
6985 * notification forms the first page, and pages added using this function form
6986 * subsequent pages. This field can be used to separate a notification into multiple
6987 * sections.
6988 *
6989 * @param pages a list of notifications
6990 * @return this object for method chaining
6991 * @see android.app.Notification.WearableExtender#getPages
6992 */
6993 public WearableExtender addPages(List<Notification> pages) {
6994 mPages.addAll(pages);
6995 return this;
6996 }
6997
6998 /**
6999 * Clear all additional pages present on this builder.
7000 * @return this object for method chaining.
7001 * @see #addPage
7002 */
7003 public WearableExtender clearPages() {
7004 mPages.clear();
7005 return this;
7006 }
7007
7008 /**
7009 * Get the array of additional pages of content for displaying this notification. The
7010 * current notification forms the first page, and elements within this array form
7011 * subsequent pages. This field can be used to separate a notification into multiple
7012 * sections.
7013 * @return the pages for this notification
7014 */
7015 public List<Notification> getPages() {
7016 return mPages;
7017 }
7018
7019 /**
7020 * Set a background image to be displayed behind the notification content.
7021 * Contrary to the {@link android.app.Notification.BigPictureStyle}, this background
7022 * will work with any notification style.
7023 *
7024 * @param background the background bitmap
7025 * @return this object for method chaining
7026 * @see android.app.Notification.WearableExtender#getBackground
7027 */
7028 public WearableExtender setBackground(Bitmap background) {
7029 mBackground = background;
7030 return this;
7031 }
7032
7033 /**
7034 * Get a background image to be displayed behind the notification content.
7035 * Contrary to the {@link android.app.Notification.BigPictureStyle}, this background
7036 * will work with any notification style.
7037 *
7038 * @return the background image
7039 * @see android.app.Notification.WearableExtender#setBackground
7040 */
7041 public Bitmap getBackground() {
7042 return mBackground;
7043 }
7044
7045 /**
7046 * Set an icon that goes with the content of this notification.
7047 */
7048 public WearableExtender setContentIcon(int icon) {
7049 mContentIcon = icon;
7050 return this;
7051 }
7052
7053 /**
7054 * Get an icon that goes with the content of this notification.
7055 */
7056 public int getContentIcon() {
7057 return mContentIcon;
7058 }
7059
7060 /**
7061 * Set the gravity that the content icon should have within the notification display.
7062 * Supported values include {@link android.view.Gravity#START} and
7063 * {@link android.view.Gravity#END}. The default value is {@link android.view.Gravity#END}.
7064 * @see #setContentIcon
7065 */
7066 public WearableExtender setContentIconGravity(int contentIconGravity) {
7067 mContentIconGravity = contentIconGravity;
7068 return this;
7069 }
7070
7071 /**
7072 * Get the gravity that the content icon should have within the notification display.
7073 * Supported values include {@link android.view.Gravity#START} and
7074 * {@link android.view.Gravity#END}. The default value is {@link android.view.Gravity#END}.
7075 * @see #getContentIcon
7076 */
7077 public int getContentIconGravity() {
7078 return mContentIconGravity;
7079 }
7080
7081 /**
7082 * Set an action from this notification's actions to be clickable with the content of
Griff Hazen14f57992014-05-26 09:07:14 -07007083 * this notification. This action will no longer display separately from the
7084 * notification's content.
7085 *
Griff Hazenca48d352014-05-28 22:37:13 -07007086 * <p>For notifications with multiple pages, child pages can also have content actions
Griff Hazen14f57992014-05-26 09:07:14 -07007087 * set, although the list of available actions comes from the main notification and not
7088 * from the child page's notification.
7089 *
7090 * @param actionIndex The index of the action to hoist onto the current notification page.
7091 * If wearable actions were added to the main notification, this index
7092 * will apply to that list, otherwise it will apply to the regular
7093 * actions list.
Griff Hazen61a9e862014-05-22 16:05:19 -07007094 */
7095 public WearableExtender setContentAction(int actionIndex) {
7096 mContentActionIndex = actionIndex;
7097 return this;
7098 }
7099
7100 /**
Griff Hazenca48d352014-05-28 22:37:13 -07007101 * Get the index of the notification action, if any, that was specified as being clickable
7102 * with the content of this notification. This action will no longer display separately
Griff Hazen14f57992014-05-26 09:07:14 -07007103 * from the notification's content.
Griff Hazen61a9e862014-05-22 16:05:19 -07007104 *
Griff Hazenca48d352014-05-28 22:37:13 -07007105 * <p>For notifications with multiple pages, child pages can also have content actions
Griff Hazen14f57992014-05-26 09:07:14 -07007106 * set, although the list of available actions comes from the main notification and not
7107 * from the child page's notification.
7108 *
7109 * <p>If wearable specific actions were added to the main notification, this index will
7110 * apply to that list, otherwise it will apply to the regular actions list.
Griff Hazenca48d352014-05-28 22:37:13 -07007111 *
7112 * @return the action index or {@link #UNSET_ACTION_INDEX} if no action was selected.
Griff Hazen61a9e862014-05-22 16:05:19 -07007113 */
7114 public int getContentAction() {
7115 return mContentActionIndex;
7116 }
7117
7118 /**
7119 * Set the gravity that this notification should have within the available viewport space.
7120 * Supported values include {@link android.view.Gravity#TOP},
7121 * {@link android.view.Gravity#CENTER_VERTICAL} and {@link android.view.Gravity#BOTTOM}.
7122 * The default value is {@link android.view.Gravity#BOTTOM}.
7123 */
7124 public WearableExtender setGravity(int gravity) {
7125 mGravity = gravity;
7126 return this;
7127 }
7128
7129 /**
7130 * Get the gravity that this notification should have within the available viewport space.
7131 * Supported values include {@link android.view.Gravity#TOP},
7132 * {@link android.view.Gravity#CENTER_VERTICAL} and {@link android.view.Gravity#BOTTOM}.
7133 * The default value is {@link android.view.Gravity#BOTTOM}.
7134 */
7135 public int getGravity() {
7136 return mGravity;
7137 }
7138
7139 /**
7140 * Set the custom size preset for the display of this notification out of the available
7141 * presets found in {@link android.app.Notification.WearableExtender}, e.g.
7142 * {@link #SIZE_LARGE}.
7143 * <p>Some custom size presets are only applicable for custom display notifications created
7144 * using {@link android.app.Notification.WearableExtender#setDisplayIntent}. Check the
7145 * documentation for the preset in question. See also
7146 * {@link #setCustomContentHeight} and {@link #getCustomSizePreset}.
7147 */
7148 public WearableExtender setCustomSizePreset(int sizePreset) {
7149 mCustomSizePreset = sizePreset;
7150 return this;
7151 }
7152
7153 /**
7154 * Get the custom size preset for the display of this notification out of the available
7155 * presets found in {@link android.app.Notification.WearableExtender}, e.g.
7156 * {@link #SIZE_LARGE}.
7157 * <p>Some custom size presets are only applicable for custom display notifications created
7158 * using {@link #setDisplayIntent}. Check the documentation for the preset in question.
7159 * See also {@link #setCustomContentHeight} and {@link #setCustomSizePreset}.
7160 */
7161 public int getCustomSizePreset() {
7162 return mCustomSizePreset;
7163 }
7164
7165 /**
7166 * Set the custom height in pixels for the display of this notification's content.
7167 * <p>This option is only available for custom display notifications created
7168 * using {@link android.app.Notification.WearableExtender#setDisplayIntent}. See also
7169 * {@link android.app.Notification.WearableExtender#setCustomSizePreset} and
7170 * {@link #getCustomContentHeight}.
7171 */
7172 public WearableExtender setCustomContentHeight(int height) {
7173 mCustomContentHeight = height;
7174 return this;
7175 }
7176
7177 /**
7178 * Get the custom height in pixels for the display of this notification's content.
7179 * <p>This option is only available for custom display notifications created
7180 * using {@link #setDisplayIntent}. See also {@link #setCustomSizePreset} and
7181 * {@link #setCustomContentHeight}.
7182 */
7183 public int getCustomContentHeight() {
7184 return mCustomContentHeight;
7185 }
7186
7187 /**
7188 * Set whether the scrolling position for the contents of this notification should start
7189 * at the bottom of the contents instead of the top when the contents are too long to
7190 * display within the screen. Default is false (start scroll at the top).
7191 */
7192 public WearableExtender setStartScrollBottom(boolean startScrollBottom) {
7193 setFlag(FLAG_START_SCROLL_BOTTOM, startScrollBottom);
7194 return this;
7195 }
7196
7197 /**
7198 * Get whether the scrolling position for the contents of this notification should start
7199 * at the bottom of the contents instead of the top when the contents are too long to
7200 * display within the screen. Default is false (start scroll at the top).
7201 */
7202 public boolean getStartScrollBottom() {
7203 return (mFlags & FLAG_START_SCROLL_BOTTOM) != 0;
7204 }
7205
7206 /**
7207 * Set whether the content intent is available when the wearable device is not connected
7208 * to a companion device. The user can still trigger this intent when the wearable device
7209 * is offline, but a visual hint will indicate that the content intent may not be available.
7210 * Defaults to true.
7211 */
7212 public WearableExtender setContentIntentAvailableOffline(
7213 boolean contentIntentAvailableOffline) {
7214 setFlag(FLAG_CONTENT_INTENT_AVAILABLE_OFFLINE, contentIntentAvailableOffline);
7215 return this;
7216 }
7217
7218 /**
7219 * Get whether the content intent is available when the wearable device is not connected
7220 * to a companion device. The user can still trigger this intent when the wearable device
7221 * is offline, but a visual hint will indicate that the content intent may not be available.
7222 * Defaults to true.
7223 */
7224 public boolean getContentIntentAvailableOffline() {
7225 return (mFlags & FLAG_CONTENT_INTENT_AVAILABLE_OFFLINE) != 0;
7226 }
7227
7228 /**
7229 * Set a hint that this notification's icon should not be displayed.
7230 * @param hintHideIcon {@code true} to hide the icon, {@code false} otherwise.
7231 * @return this object for method chaining
7232 */
7233 public WearableExtender setHintHideIcon(boolean hintHideIcon) {
7234 setFlag(FLAG_HINT_HIDE_ICON, hintHideIcon);
7235 return this;
7236 }
7237
7238 /**
7239 * Get a hint that this notification's icon should not be displayed.
7240 * @return {@code true} if this icon should not be displayed, false otherwise.
7241 * The default value is {@code false} if this was never set.
7242 */
7243 public boolean getHintHideIcon() {
7244 return (mFlags & FLAG_HINT_HIDE_ICON) != 0;
7245 }
7246
7247 /**
7248 * Set a visual hint that only the background image of this notification should be
7249 * displayed, and other semantic content should be hidden. This hint is only applicable
7250 * to sub-pages added using {@link #addPage}.
7251 */
7252 public WearableExtender setHintShowBackgroundOnly(boolean hintShowBackgroundOnly) {
7253 setFlag(FLAG_HINT_SHOW_BACKGROUND_ONLY, hintShowBackgroundOnly);
7254 return this;
7255 }
7256
7257 /**
7258 * Get a visual hint that only the background image of this notification should be
7259 * displayed, and other semantic content should be hidden. This hint is only applicable
7260 * to sub-pages added using {@link android.app.Notification.WearableExtender#addPage}.
7261 */
7262 public boolean getHintShowBackgroundOnly() {
7263 return (mFlags & FLAG_HINT_SHOW_BACKGROUND_ONLY) != 0;
7264 }
7265
Griff Hazen5f2edfc2014-09-29 16:28:44 -07007266 /**
Griff Hazen9c5be4e2014-11-17 17:47:50 -08007267 * Set a hint that this notification's background should not be clipped if possible,
7268 * and should instead be resized to fully display on the screen, retaining the aspect
7269 * ratio of the image. This can be useful for images like barcodes or qr codes.
Griff Hazen5f2edfc2014-09-29 16:28:44 -07007270 * @param hintAvoidBackgroundClipping {@code true} to avoid clipping if possible.
7271 * @return this object for method chaining
7272 */
7273 public WearableExtender setHintAvoidBackgroundClipping(
7274 boolean hintAvoidBackgroundClipping) {
7275 setFlag(FLAG_HINT_AVOID_BACKGROUND_CLIPPING, hintAvoidBackgroundClipping);
7276 return this;
7277 }
7278
7279 /**
Griff Hazen9c5be4e2014-11-17 17:47:50 -08007280 * Get a hint that this notification's background should not be clipped if possible,
7281 * and should instead be resized to fully display on the screen, retaining the aspect
7282 * ratio of the image. This can be useful for images like barcodes or qr codes.
Griff Hazen5f2edfc2014-09-29 16:28:44 -07007283 * @return {@code true} if it's ok if the background is clipped on the screen, false
7284 * otherwise. The default value is {@code false} if this was never set.
7285 */
7286 public boolean getHintAvoidBackgroundClipping() {
7287 return (mFlags & FLAG_HINT_AVOID_BACKGROUND_CLIPPING) != 0;
7288 }
7289
7290 /**
7291 * Set a hint that the screen should remain on for at least this duration when
7292 * this notification is displayed on the screen.
7293 * @param timeout The requested screen timeout in milliseconds. Can also be either
7294 * {@link #SCREEN_TIMEOUT_SHORT} or {@link #SCREEN_TIMEOUT_LONG}.
7295 * @return this object for method chaining
7296 */
7297 public WearableExtender setHintScreenTimeout(int timeout) {
7298 mHintScreenTimeout = timeout;
7299 return this;
7300 }
7301
7302 /**
7303 * Get the duration, in milliseconds, that the screen should remain on for
7304 * when this notification is displayed.
7305 * @return the duration in milliseconds if > 0, or either one of the sentinel values
7306 * {@link #SCREEN_TIMEOUT_SHORT} or {@link #SCREEN_TIMEOUT_LONG}.
7307 */
7308 public int getHintScreenTimeout() {
7309 return mHintScreenTimeout;
7310 }
7311
Alex Hills9ab3a232016-04-05 14:54:56 -04007312 /**
Alex Hills4bcb06b2016-04-05 14:26:25 -04007313 * Set a hint that this notification's {@link BigPictureStyle} (if present) should be
7314 * converted to low-bit and displayed in ambient mode, especially useful for barcodes and
7315 * qr codes, as well as other simple black-and-white tickets.
7316 * @param hintAmbientBigPicture {@code true} to enable converstion and ambient.
7317 * @return this object for method chaining
7318 */
7319 public WearableExtender setHintAmbientBigPicture(boolean hintAmbientBigPicture) {
7320 setFlag(FLAG_BIG_PICTURE_AMBIENT, hintAmbientBigPicture);
7321 return this;
7322 }
7323
7324 /**
7325 * Get a hint that this notification's {@link BigPictureStyle} (if present) should be
7326 * converted to low-bit and displayed in ambient mode, especially useful for barcodes and
7327 * qr codes, as well as other simple black-and-white tickets.
7328 * @return {@code true} if it should be displayed in ambient, false otherwise
7329 * otherwise. The default value is {@code false} if this was never set.
7330 */
7331 public boolean getHintAmbientBigPicture() {
7332 return (mFlags & FLAG_BIG_PICTURE_AMBIENT) != 0;
7333 }
7334
7335 /**
Alex Hills9ab3a232016-04-05 14:54:56 -04007336 * Set a hint that this notification's content intent will launch an {@link Activity}
7337 * directly, telling the platform that it can generate the appropriate transitions.
7338 * @param hintContentIntentLaunchesActivity {@code true} if the content intent will launch
7339 * an activity and transitions should be generated, false otherwise.
7340 * @return this object for method chaining
7341 */
7342 public WearableExtender setHintContentIntentLaunchesActivity(
7343 boolean hintContentIntentLaunchesActivity) {
7344 setFlag(FLAG_HINT_CONTENT_INTENT_LAUNCHES_ACTIVITY, hintContentIntentLaunchesActivity);
7345 return this;
7346 }
7347
7348 /**
7349 * Get a hint that this notification's content intent will launch an {@link Activity}
7350 * directly, telling the platform that it can generate the appropriate transitions
7351 * @return {@code true} if the content intent will launch an activity and transitions should
7352 * be generated, false otherwise. The default value is {@code false} if this was never set.
7353 */
7354 public boolean getHintContentIntentLaunchesActivity() {
7355 return (mFlags & FLAG_HINT_CONTENT_INTENT_LAUNCHES_ACTIVITY) != 0;
7356 }
7357
Nadia Benbernou948627e2016-04-14 14:41:08 -04007358 /**
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04007359 * Sets the dismissal id for this notification. If a notification is posted with a
7360 * dismissal id, then when that notification is canceled, notifications on other wearables
7361 * and the paired Android phone having that same dismissal id will also be canceled. See
Nadia Benbernou948627e2016-04-14 14:41:08 -04007362 * <a href="{@docRoot}wear/notifications/index.html">Adding Wearable Features to
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04007363 * Notifications</a> for more information.
Nadia Benbernou948627e2016-04-14 14:41:08 -04007364 * @param dismissalId the dismissal id of the notification.
7365 * @return this object for method chaining
7366 */
7367 public WearableExtender setDismissalId(String dismissalId) {
7368 mDismissalId = dismissalId;
7369 return this;
7370 }
7371
7372 /**
7373 * Returns the dismissal id of the notification.
7374 * @return the dismissal id of the notification or null if it has not been set.
7375 */
7376 public String getDismissalId() {
7377 return mDismissalId;
7378 }
7379
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04007380 /**
7381 * Sets a bridge tag for this notification. A bridge tag can be set for notifications
7382 * posted from a phone to provide finer-grained control on what notifications are bridged
7383 * to wearables. See <a href="{@docRoot}wear/notifications/index.html">Adding Wearable
7384 * Features to Notifications</a> for more information.
7385 * @param bridgeTag the bridge tag of the notification.
7386 * @return this object for method chaining
7387 */
7388 public WearableExtender setBridgeTag(String bridgeTag) {
7389 mBridgeTag = bridgeTag;
7390 return this;
7391 }
7392
7393 /**
7394 * Returns the bridge tag of the notification.
7395 * @return the bridge tag or null if not present.
7396 */
7397 public String getBridgeTag() {
7398 return mBridgeTag;
7399 }
7400
Griff Hazen61a9e862014-05-22 16:05:19 -07007401 private void setFlag(int mask, boolean value) {
7402 if (value) {
7403 mFlags |= mask;
7404 } else {
7405 mFlags &= ~mask;
7406 }
7407 }
7408 }
7409
7410 /**
Zhen Yu Song9d5528b2014-11-14 15:34:39 -08007411 * <p>Helper class to add Android Auto extensions to notifications. To create a notification
7412 * with car extensions:
7413 *
7414 * <ol>
7415 * <li>Create an {@link Notification.Builder}, setting any desired
7416 * properties.
7417 * <li>Create a {@link CarExtender}.
7418 * <li>Set car-specific properties using the {@code add} and {@code set} methods of
7419 * {@link CarExtender}.
7420 * <li>Call {@link Notification.Builder#extend(Notification.Extender)}
7421 * to apply the extensions to a notification.
7422 * </ol>
7423 *
7424 * <pre class="prettyprint">
7425 * Notification notification = new Notification.Builder(context)
7426 * ...
7427 * .extend(new CarExtender()
7428 * .set*(...))
7429 * .build();
7430 * </pre>
7431 *
7432 * <p>Car extensions can be accessed on an existing notification by using the
7433 * {@code CarExtender(Notification)} constructor, and then using the {@code get} methods
7434 * to access values.
7435 */
7436 public static final class CarExtender implements Extender {
7437 private static final String TAG = "CarExtender";
7438
7439 private static final String EXTRA_CAR_EXTENDER = "android.car.EXTENSIONS";
7440 private static final String EXTRA_LARGE_ICON = "large_icon";
7441 private static final String EXTRA_CONVERSATION = "car_conversation";
7442 private static final String EXTRA_COLOR = "app_color";
7443
7444 private Bitmap mLargeIcon;
7445 private UnreadConversation mUnreadConversation;
7446 private int mColor = Notification.COLOR_DEFAULT;
7447
7448 /**
7449 * Create a {@link CarExtender} with default options.
7450 */
7451 public CarExtender() {
7452 }
7453
7454 /**
7455 * Create a {@link CarExtender} from the CarExtender options of an existing Notification.
7456 *
7457 * @param notif The notification from which to copy options.
7458 */
7459 public CarExtender(Notification notif) {
7460 Bundle carBundle = notif.extras == null ?
7461 null : notif.extras.getBundle(EXTRA_CAR_EXTENDER);
7462 if (carBundle != null) {
7463 mLargeIcon = carBundle.getParcelable(EXTRA_LARGE_ICON);
7464 mColor = carBundle.getInt(EXTRA_COLOR, Notification.COLOR_DEFAULT);
7465
7466 Bundle b = carBundle.getBundle(EXTRA_CONVERSATION);
7467 mUnreadConversation = UnreadConversation.getUnreadConversationFromBundle(b);
7468 }
7469 }
7470
7471 /**
7472 * Apply car extensions to a notification that is being built. This is typically called by
7473 * the {@link Notification.Builder#extend(Notification.Extender)}
7474 * method of {@link Notification.Builder}.
7475 */
7476 @Override
7477 public Notification.Builder extend(Notification.Builder builder) {
7478 Bundle carExtensions = new Bundle();
7479
7480 if (mLargeIcon != null) {
7481 carExtensions.putParcelable(EXTRA_LARGE_ICON, mLargeIcon);
7482 }
7483 if (mColor != Notification.COLOR_DEFAULT) {
7484 carExtensions.putInt(EXTRA_COLOR, mColor);
7485 }
7486
7487 if (mUnreadConversation != null) {
7488 Bundle b = mUnreadConversation.getBundleForUnreadConversation();
7489 carExtensions.putBundle(EXTRA_CONVERSATION, b);
7490 }
7491
7492 builder.getExtras().putBundle(EXTRA_CAR_EXTENDER, carExtensions);
7493 return builder;
7494 }
7495
7496 /**
7497 * Sets the accent color to use when Android Auto presents the notification.
7498 *
7499 * Android Auto uses the color set with {@link Notification.Builder#setColor(int)}
7500 * to accent the displayed notification. However, not all colors are acceptable in an
7501 * automotive setting. This method can be used to override the color provided in the
7502 * notification in such a situation.
7503 */
Tor Norbye80756e32015-03-02 09:39:27 -08007504 public CarExtender setColor(@ColorInt int color) {
Zhen Yu Song9d5528b2014-11-14 15:34:39 -08007505 mColor = color;
7506 return this;
7507 }
7508
7509 /**
7510 * Gets the accent color.
7511 *
Julia Reynoldsd9228f12015-10-20 10:37:27 -04007512 * @see #setColor
Zhen Yu Song9d5528b2014-11-14 15:34:39 -08007513 */
Tor Norbye80756e32015-03-02 09:39:27 -08007514 @ColorInt
Zhen Yu Song9d5528b2014-11-14 15:34:39 -08007515 public int getColor() {
7516 return mColor;
7517 }
7518
7519 /**
7520 * Sets the large icon of the car notification.
7521 *
7522 * If no large icon is set in the extender, Android Auto will display the icon
7523 * specified by {@link Notification.Builder#setLargeIcon(android.graphics.Bitmap)}
7524 *
7525 * @param largeIcon The large icon to use in the car notification.
7526 * @return This object for method chaining.
7527 */
7528 public CarExtender setLargeIcon(Bitmap largeIcon) {
7529 mLargeIcon = largeIcon;
7530 return this;
7531 }
7532
7533 /**
7534 * Gets the large icon used in this car notification, or null if no icon has been set.
7535 *
7536 * @return The large icon for the car notification.
7537 * @see CarExtender#setLargeIcon
7538 */
7539 public Bitmap getLargeIcon() {
7540 return mLargeIcon;
7541 }
7542
7543 /**
7544 * Sets the unread conversation in a message notification.
7545 *
7546 * @param unreadConversation The unread part of the conversation this notification conveys.
7547 * @return This object for method chaining.
7548 */
7549 public CarExtender setUnreadConversation(UnreadConversation unreadConversation) {
7550 mUnreadConversation = unreadConversation;
7551 return this;
7552 }
7553
7554 /**
7555 * Returns the unread conversation conveyed by this notification.
7556 * @see #setUnreadConversation(UnreadConversation)
7557 */
7558 public UnreadConversation getUnreadConversation() {
7559 return mUnreadConversation;
7560 }
7561
7562 /**
7563 * A class which holds the unread messages from a conversation.
7564 */
7565 public static class UnreadConversation {
7566 private static final String KEY_AUTHOR = "author";
7567 private static final String KEY_TEXT = "text";
7568 private static final String KEY_MESSAGES = "messages";
7569 private static final String KEY_REMOTE_INPUT = "remote_input";
7570 private static final String KEY_ON_REPLY = "on_reply";
7571 private static final String KEY_ON_READ = "on_read";
7572 private static final String KEY_PARTICIPANTS = "participants";
7573 private static final String KEY_TIMESTAMP = "timestamp";
7574
7575 private final String[] mMessages;
7576 private final RemoteInput mRemoteInput;
7577 private final PendingIntent mReplyPendingIntent;
7578 private final PendingIntent mReadPendingIntent;
7579 private final String[] mParticipants;
7580 private final long mLatestTimestamp;
7581
7582 UnreadConversation(String[] messages, RemoteInput remoteInput,
7583 PendingIntent replyPendingIntent, PendingIntent readPendingIntent,
7584 String[] participants, long latestTimestamp) {
7585 mMessages = messages;
7586 mRemoteInput = remoteInput;
7587 mReadPendingIntent = readPendingIntent;
7588 mReplyPendingIntent = replyPendingIntent;
7589 mParticipants = participants;
7590 mLatestTimestamp = latestTimestamp;
7591 }
7592
7593 /**
7594 * Gets the list of messages conveyed by this notification.
7595 */
7596 public String[] getMessages() {
7597 return mMessages;
7598 }
7599
7600 /**
7601 * Gets the remote input that will be used to convey the response to a message list, or
7602 * null if no such remote input exists.
7603 */
7604 public RemoteInput getRemoteInput() {
7605 return mRemoteInput;
7606 }
7607
7608 /**
7609 * Gets the pending intent that will be triggered when the user replies to this
7610 * notification.
7611 */
7612 public PendingIntent getReplyPendingIntent() {
7613 return mReplyPendingIntent;
7614 }
7615
7616 /**
7617 * Gets the pending intent that Android Auto will send after it reads aloud all messages
7618 * in this object's message list.
7619 */
7620 public PendingIntent getReadPendingIntent() {
7621 return mReadPendingIntent;
7622 }
7623
7624 /**
7625 * Gets the participants in the conversation.
7626 */
7627 public String[] getParticipants() {
7628 return mParticipants;
7629 }
7630
7631 /**
7632 * Gets the firs participant in the conversation.
7633 */
7634 public String getParticipant() {
7635 return mParticipants.length > 0 ? mParticipants[0] : null;
7636 }
7637
7638 /**
7639 * Gets the timestamp of the conversation.
7640 */
7641 public long getLatestTimestamp() {
7642 return mLatestTimestamp;
7643 }
7644
7645 Bundle getBundleForUnreadConversation() {
7646 Bundle b = new Bundle();
7647 String author = null;
7648 if (mParticipants != null && mParticipants.length > 1) {
7649 author = mParticipants[0];
7650 }
7651 Parcelable[] messages = new Parcelable[mMessages.length];
7652 for (int i = 0; i < messages.length; i++) {
7653 Bundle m = new Bundle();
7654 m.putString(KEY_TEXT, mMessages[i]);
7655 m.putString(KEY_AUTHOR, author);
7656 messages[i] = m;
7657 }
7658 b.putParcelableArray(KEY_MESSAGES, messages);
7659 if (mRemoteInput != null) {
7660 b.putParcelable(KEY_REMOTE_INPUT, mRemoteInput);
7661 }
7662 b.putParcelable(KEY_ON_REPLY, mReplyPendingIntent);
7663 b.putParcelable(KEY_ON_READ, mReadPendingIntent);
7664 b.putStringArray(KEY_PARTICIPANTS, mParticipants);
7665 b.putLong(KEY_TIMESTAMP, mLatestTimestamp);
7666 return b;
7667 }
7668
7669 static UnreadConversation getUnreadConversationFromBundle(Bundle b) {
7670 if (b == null) {
7671 return null;
7672 }
7673 Parcelable[] parcelableMessages = b.getParcelableArray(KEY_MESSAGES);
7674 String[] messages = null;
7675 if (parcelableMessages != null) {
7676 String[] tmp = new String[parcelableMessages.length];
7677 boolean success = true;
7678 for (int i = 0; i < tmp.length; i++) {
7679 if (!(parcelableMessages[i] instanceof Bundle)) {
7680 success = false;
7681 break;
7682 }
7683 tmp[i] = ((Bundle) parcelableMessages[i]).getString(KEY_TEXT);
7684 if (tmp[i] == null) {
7685 success = false;
7686 break;
7687 }
7688 }
7689 if (success) {
7690 messages = tmp;
7691 } else {
7692 return null;
7693 }
7694 }
7695
7696 PendingIntent onRead = b.getParcelable(KEY_ON_READ);
7697 PendingIntent onReply = b.getParcelable(KEY_ON_REPLY);
7698
7699 RemoteInput remoteInput = b.getParcelable(KEY_REMOTE_INPUT);
7700
7701 String[] participants = b.getStringArray(KEY_PARTICIPANTS);
7702 if (participants == null || participants.length != 1) {
7703 return null;
7704 }
7705
7706 return new UnreadConversation(messages,
7707 remoteInput,
7708 onReply,
7709 onRead,
7710 participants, b.getLong(KEY_TIMESTAMP));
7711 }
7712 };
7713
7714 /**
7715 * Builder class for {@link CarExtender.UnreadConversation} objects.
7716 */
7717 public static class Builder {
7718 private final List<String> mMessages = new ArrayList<String>();
7719 private final String mParticipant;
7720 private RemoteInput mRemoteInput;
7721 private PendingIntent mReadPendingIntent;
7722 private PendingIntent mReplyPendingIntent;
7723 private long mLatestTimestamp;
7724
7725 /**
7726 * Constructs a new builder for {@link CarExtender.UnreadConversation}.
7727 *
7728 * @param name The name of the other participant in the conversation.
7729 */
7730 public Builder(String name) {
7731 mParticipant = name;
7732 }
7733
7734 /**
7735 * Appends a new unread message to the list of messages for this conversation.
7736 *
7737 * The messages should be added from oldest to newest.
7738 *
7739 * @param message The text of the new unread message.
7740 * @return This object for method chaining.
7741 */
7742 public Builder addMessage(String message) {
7743 mMessages.add(message);
7744 return this;
7745 }
7746
7747 /**
7748 * Sets the pending intent and remote input which will convey the reply to this
7749 * notification.
7750 *
7751 * @param pendingIntent The pending intent which will be triggered on a reply.
7752 * @param remoteInput The remote input parcelable which will carry the reply.
7753 * @return This object for method chaining.
7754 *
7755 * @see CarExtender.UnreadConversation#getRemoteInput
7756 * @see CarExtender.UnreadConversation#getReplyPendingIntent
7757 */
7758 public Builder setReplyAction(
7759 PendingIntent pendingIntent, RemoteInput remoteInput) {
7760 mRemoteInput = remoteInput;
7761 mReplyPendingIntent = pendingIntent;
7762
7763 return this;
7764 }
7765
7766 /**
7767 * Sets the pending intent that will be sent once the messages in this notification
7768 * are read.
7769 *
7770 * @param pendingIntent The pending intent to use.
7771 * @return This object for method chaining.
7772 */
7773 public Builder setReadPendingIntent(PendingIntent pendingIntent) {
7774 mReadPendingIntent = pendingIntent;
7775 return this;
7776 }
7777
7778 /**
7779 * Sets the timestamp of the most recent message in an unread conversation.
7780 *
7781 * If a messaging notification has been posted by your application and has not
7782 * yet been cancelled, posting a later notification with the same id and tag
7783 * but without a newer timestamp may result in Android Auto not displaying a
7784 * heads up notification for the later notification.
7785 *
7786 * @param timestamp The timestamp of the most recent message in the conversation.
7787 * @return This object for method chaining.
7788 */
7789 public Builder setLatestTimestamp(long timestamp) {
7790 mLatestTimestamp = timestamp;
7791 return this;
7792 }
7793
7794 /**
7795 * Builds a new unread conversation object.
7796 *
7797 * @return The new unread conversation object.
7798 */
7799 public UnreadConversation build() {
7800 String[] messages = mMessages.toArray(new String[mMessages.size()]);
7801 String[] participants = { mParticipant };
7802 return new UnreadConversation(messages, mRemoteInput, mReplyPendingIntent,
7803 mReadPendingIntent, participants, mLatestTimestamp);
7804 }
7805 }
7806 }
7807
7808 /**
Dmitri Plotnikov9e9cfd92016-12-21 10:52:26 -08007809 * <p>Helper class to add Android TV extensions to notifications. To create a notification
7810 * with a TV extension:
7811 *
7812 * <ol>
7813 * <li>Create an {@link Notification.Builder}, setting any desired properties.
7814 * <li>Create a {@link TvExtender}.
7815 * <li>Set TV-specific properties using the {@code set} methods of
7816 * {@link TvExtender}.
7817 * <li>Call {@link Notification.Builder#extend(Notification.Extender)}
7818 * to apply the extension to a notification.
7819 * </ol>
7820 *
7821 * <pre class="prettyprint">
7822 * Notification notification = new Notification.Builder(context)
7823 * ...
7824 * .extend(new TvExtender()
7825 * .set*(...))
7826 * .build();
7827 * </pre>
7828 *
7829 * <p>TV extensions can be accessed on an existing notification by using the
7830 * {@code TvExtender(Notification)} constructor, and then using the {@code get} methods
7831 * to access values.
7832 *
7833 * @hide
7834 */
7835 @SystemApi
7836 public static final class TvExtender implements Extender {
7837 private static final String TAG = "TvExtender";
7838
7839 private static final String EXTRA_TV_EXTENDER = "android.tv.EXTENSIONS";
7840 private static final String EXTRA_FLAGS = "flags";
7841 private static final String EXTRA_CONTENT_INTENT = "content_intent";
7842 private static final String EXTRA_DELETE_INTENT = "delete_intent";
Dmitri Plotnikovb8a04ee2017-01-20 10:39:22 -08007843 private static final String EXTRA_CHANNEL_ID = "channel_id";
Dmitri Plotnikov9e9cfd92016-12-21 10:52:26 -08007844
7845 // Flags bitwise-ored to mFlags
7846 private static final int FLAG_AVAILABLE_ON_TV = 0x1;
7847
7848 private int mFlags;
Dmitri Plotnikovb8a04ee2017-01-20 10:39:22 -08007849 private String mChannelId;
Dmitri Plotnikov9e9cfd92016-12-21 10:52:26 -08007850 private PendingIntent mContentIntent;
7851 private PendingIntent mDeleteIntent;
7852
7853 /**
7854 * Create a {@link TvExtender} with default options.
7855 */
7856 public TvExtender() {
7857 mFlags = FLAG_AVAILABLE_ON_TV;
7858 }
7859
7860 /**
7861 * Create a {@link TvExtender} from the TvExtender options of an existing Notification.
7862 *
7863 * @param notif The notification from which to copy options.
7864 */
7865 public TvExtender(Notification notif) {
7866 Bundle bundle = notif.extras == null ?
7867 null : notif.extras.getBundle(EXTRA_TV_EXTENDER);
7868 if (bundle != null) {
7869 mFlags = bundle.getInt(EXTRA_FLAGS);
Dmitri Plotnikovb8a04ee2017-01-20 10:39:22 -08007870 mChannelId = bundle.getString(EXTRA_CHANNEL_ID);
Dmitri Plotnikov9e9cfd92016-12-21 10:52:26 -08007871 mContentIntent = bundle.getParcelable(EXTRA_CONTENT_INTENT);
7872 mDeleteIntent = bundle.getParcelable(EXTRA_DELETE_INTENT);
7873 }
7874 }
7875
7876 /**
7877 * Apply a TV extension to a notification that is being built. This is typically called by
7878 * the {@link Notification.Builder#extend(Notification.Extender)}
7879 * method of {@link Notification.Builder}.
7880 */
7881 @Override
7882 public Notification.Builder extend(Notification.Builder builder) {
7883 Bundle bundle = new Bundle();
7884
7885 bundle.putInt(EXTRA_FLAGS, mFlags);
Dmitri Plotnikovb8a04ee2017-01-20 10:39:22 -08007886 bundle.putString(EXTRA_CHANNEL_ID, mChannelId);
Dmitri Plotnikov9e9cfd92016-12-21 10:52:26 -08007887 if (mContentIntent != null) {
7888 bundle.putParcelable(EXTRA_CONTENT_INTENT, mContentIntent);
7889 }
7890
7891 if (mDeleteIntent != null) {
7892 bundle.putParcelable(EXTRA_DELETE_INTENT, mDeleteIntent);
7893 }
7894
7895 builder.getExtras().putBundle(EXTRA_TV_EXTENDER, bundle);
7896 return builder;
7897 }
7898
7899 /**
7900 * Returns true if this notification should be shown on TV. This method return true
7901 * if the notification was extended with a TvExtender.
7902 */
7903 public boolean isAvailableOnTv() {
7904 return (mFlags & FLAG_AVAILABLE_ON_TV) != 0;
7905 }
7906
7907 /**
Dmitri Plotnikovb8a04ee2017-01-20 10:39:22 -08007908 * Specifies the channel the notification should be delivered on when shown on TV.
7909 * It can be different from the channel that the notification is delivered to when
7910 * posting on a non-TV device.
7911 */
7912 public TvExtender setChannel(String channelId) {
7913 mChannelId = channelId;
7914 return this;
7915 }
7916
7917 /**
7918 * Returns the id of the channel this notification posts to on TV.
7919 */
7920 public String getChannel() {
7921 return mChannelId;
7922 }
7923
7924 /**
Dmitri Plotnikov9e9cfd92016-12-21 10:52:26 -08007925 * Supplies a {@link PendingIntent} to be sent when the notification is selected on TV.
7926 * If provided, it is used instead of the content intent specified
7927 * at the level of Notification.
7928 */
7929 public TvExtender setContentIntent(PendingIntent intent) {
7930 mContentIntent = intent;
7931 return this;
7932 }
7933
7934 /**
7935 * Returns the TV-specific content intent. If this method returns null, the
7936 * main content intent on the notification should be used.
7937 *
7938 * @see {@link Notification#contentIntent}
7939 */
7940 public PendingIntent getContentIntent() {
7941 return mContentIntent;
7942 }
7943
7944 /**
7945 * Supplies a {@link PendingIntent} to send when the notification is cleared explicitly
7946 * by the user on TV. If provided, it is used instead of the delete intent specified
7947 * at the level of Notification.
7948 */
7949 public TvExtender setDeleteIntent(PendingIntent intent) {
7950 mDeleteIntent = intent;
7951 return this;
7952 }
7953
7954 /**
7955 * Returns the TV-specific delete intent. If this method returns null, the
7956 * main delete intent on the notification should be used.
7957 *
7958 * @see {@link Notification#deleteIntent}
7959 */
7960 public PendingIntent getDeleteIntent() {
7961 return mDeleteIntent;
7962 }
7963 }
7964
7965 /**
Griff Hazen61a9e862014-05-22 16:05:19 -07007966 * Get an array of Notification objects from a parcelable array bundle field.
7967 * Update the bundle to have a typed array so fetches in the future don't need
7968 * to do an array copy.
7969 */
7970 private static Notification[] getNotificationArrayFromBundle(Bundle bundle, String key) {
7971 Parcelable[] array = bundle.getParcelableArray(key);
7972 if (array instanceof Notification[] || array == null) {
7973 return (Notification[]) array;
7974 }
7975 Notification[] typedArray = Arrays.copyOf(array, array.length,
7976 Notification[].class);
7977 bundle.putParcelableArray(key, typedArray);
7978 return typedArray;
7979 }
Christoph Studer4600f9b2014-07-22 22:44:43 +02007980
7981 private static class BuilderRemoteViews extends RemoteViews {
7982 public BuilderRemoteViews(Parcel parcel) {
7983 super(parcel);
7984 }
7985
Kenny Guy77320062014-08-27 21:37:15 +01007986 public BuilderRemoteViews(ApplicationInfo appInfo, int layoutId) {
7987 super(appInfo, layoutId);
Christoph Studer4600f9b2014-07-22 22:44:43 +02007988 }
7989
7990 @Override
7991 public BuilderRemoteViews clone() {
7992 Parcel p = Parcel.obtain();
7993 writeToParcel(p, 0);
7994 p.setDataPosition(0);
7995 BuilderRemoteViews brv = new BuilderRemoteViews(p);
7996 p.recycle();
7997 return brv;
7998 }
7999 }
Adrian Roos70d7aa32017-01-11 15:39:06 -08008000
8001 private static class StandardTemplateParams {
8002 boolean hasProgress = true;
8003 boolean ambient = false;
8004 CharSequence title;
8005 CharSequence text;
8006
8007 final StandardTemplateParams reset() {
8008 hasProgress = true;
8009 ambient = false;
8010 title = null;
8011 text = null;
8012 return this;
8013 }
8014
8015 final StandardTemplateParams hasProgress(boolean hasProgress) {
8016 this.hasProgress = hasProgress;
8017 return this;
8018 }
8019
8020 final StandardTemplateParams title(CharSequence title) {
8021 this.title = title;
8022 return this;
8023 }
8024
8025 final StandardTemplateParams text(CharSequence text) {
8026 this.text = text;
8027 return this;
8028 }
8029
8030 final StandardTemplateParams ambient(boolean ambient) {
Adrian Roos0bc3f6a2017-03-06 11:54:05 -08008031 Preconditions.checkState(title == null && text == null, "must set ambient before text");
Adrian Roos70d7aa32017-01-11 15:39:06 -08008032 this.ambient = ambient;
8033 return this;
8034 }
8035
8036 final StandardTemplateParams fillTextsFrom(Builder b) {
8037 Bundle extras = b.mN.extras;
Adrian Roos0bc3f6a2017-03-06 11:54:05 -08008038 title = b.processLegacyText(extras.getCharSequence(EXTRA_TITLE), ambient);
8039 text = b.processLegacyText(extras.getCharSequence(EXTRA_TEXT), ambient);
Adrian Roos70d7aa32017-01-11 15:39:06 -08008040 return this;
8041 }
8042 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008043}