blob: 2ad7498709b61532a1e45f69503bcba509c41de2 [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 *
Julia Reynolds30331982017-04-27 10:12:50 -0400242 * The system may or may not use this field to modify the appearance of the notification.
Julia Reynolds13d898c2017-02-02 12:22:05 -0500243 * Starting with {@link android.os.Build.VERSION_CODES#O}, the number may be displayed as a
244 * badge icon in Launchers that support badging.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800245 */
Julia Reynolds30331982017-04-27 10:12:50 -0400246 public int number = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800247
248 /**
249 * The intent to execute when the expanded status entry is clicked. If
250 * this is an activity, it must include the
251 * {@link android.content.Intent#FLAG_ACTIVITY_NEW_TASK} flag, which requires
Scott Main7aee61f2011-02-08 11:25:01 -0800252 * that you take care of task management as described in the
253 * <a href="{@docRoot}guide/topics/fundamentals/tasks-and-back-stack.html">Tasks and Back
Dianne Hackborn6ceca582012-01-10 15:24:26 -0800254 * Stack</a> document. In particular, make sure to read the notification section
255 * <a href="{@docRoot}guide/topics/ui/notifiers/notifications.html#HandlingNotifications">Handling
256 * Notifications</a> for the correct ways to launch an application from a
257 * notification.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800258 */
259 public PendingIntent contentIntent;
260
261 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500262 * The intent to execute when the notification is explicitly dismissed by the user, either with
263 * the "Clear All" button or by swiping it away individually.
264 *
265 * This probably shouldn't be launching an activity since several of those will be sent
266 * at the same time.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800267 */
268 public PendingIntent deleteIntent;
269
270 /**
Dianne Hackborn170bae72010-09-03 15:14:28 -0700271 * An intent to launch instead of posting the notification to the status bar.
Joe Onoratocb109a02011-01-18 17:57:41 -0800272 *
Chris Wren47c20a12014-06-18 17:27:29 -0400273 * <p>
274 * The system UI may choose to display a heads-up notification, instead of
275 * launching this intent, while the user is using the device.
276 * </p>
277 *
Joe Onoratocb109a02011-01-18 17:57:41 -0800278 * @see Notification.Builder#setFullScreenIntent
Daniel Sandlere46cbd32010-06-17 10:35:26 -0400279 */
280 public PendingIntent fullScreenIntent;
281
282 /**
Dan Sandler5fcdf6e2014-07-18 11:31:15 -0400283 * Text that summarizes this notification for accessibility services.
284 *
285 * As of the L release, this text is no longer shown on screen, but it is still useful to
286 * accessibility services (where it serves as an audible announcement of the notification's
287 * appearance).
Joe Onoratoef1e7762010-09-17 18:38:38 -0400288 *
Joe Onorato46439ce2010-11-19 13:56:21 -0800289 * @see #tickerView
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800290 */
291 public CharSequence tickerText;
292
293 /**
Dan Sandler5fcdf6e2014-07-18 11:31:15 -0400294 * Formerly, a view showing the {@link #tickerText}.
295 *
296 * No longer displayed in the status bar as of API 21.
Joe Onoratoef1e7762010-09-17 18:38:38 -0400297 */
Dan Sandler5fcdf6e2014-07-18 11:31:15 -0400298 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -0800299 public RemoteViews tickerView;
Joe Onoratoef1e7762010-09-17 18:38:38 -0400300
301 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400302 * The view that will represent this notification in the notification list (which is pulled
303 * down from the status bar).
304 *
Julia Reynoldsd4ea7412016-02-17 14:00:56 -0500305 * As of N, this field may be null. The notification view is determined by the inputs
306 * to {@link Notification.Builder}; a custom RemoteViews can optionally be
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400307 * supplied with {@link Notification.Builder#setCustomContentView(RemoteViews)}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800308 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400309 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800310 public RemoteViews contentView;
311
Daniel Sandlera0a938c2012-03-15 08:42:37 -0400312 /**
Daniel Sandler4dfbe832012-04-11 14:51:46 -0400313 * A large-format version of {@link #contentView}, giving the Notification an
Daniel Sandlerf3b73432012-03-27 15:01:25 -0400314 * opportunity to show more detail. The system UI may choose to show this
315 * instead of the normal content view at its discretion.
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400316 *
Julia Reynoldsd4ea7412016-02-17 14:00:56 -0500317 * As of N, this field may be null. The expanded notification view is determined by the
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400318 * inputs to {@link Notification.Builder}; a custom RemoteViews can optionally be
319 * supplied with {@link Notification.Builder#setCustomBigContentView(RemoteViews)}.
Daniel Sandlerf3b73432012-03-27 15:01:25 -0400320 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400321 @Deprecated
Daniel Sandlerf3b73432012-03-27 15:01:25 -0400322 public RemoteViews bigContentView;
323
Chris Wren8fd39ec2014-02-27 17:43:26 -0500324
325 /**
Chris Wren47c20a12014-06-18 17:27:29 -0400326 * A medium-format version of {@link #contentView}, providing the Notification an
327 * opportunity to add action buttons to contentView. At its discretion, the system UI may
328 * choose to show this as a heads-up notification, which will pop up so the user can see
329 * it without leaving their current activity.
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400330 *
Julia Reynoldsd4ea7412016-02-17 14:00:56 -0500331 * As of N, this field may be null. The heads-up notification view is determined by the
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400332 * inputs to {@link Notification.Builder}; a custom RemoteViews can optionally be
333 * supplied with {@link Notification.Builder#setCustomHeadsUpContentView(RemoteViews)}.
Chris Wren8fd39ec2014-02-27 17:43:26 -0500334 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400335 @Deprecated
Chris Wren8fd39ec2014-02-27 17:43:26 -0500336 public RemoteViews headsUpContentView;
337
Daniel Sandlerf3b73432012-03-27 15:01:25 -0400338 /**
Dan Sandler86647982015-05-13 23:41:13 -0400339 * A large bitmap to be shown in the notification content area.
340 *
341 * @deprecated Use {@link Builder#setLargeIcon(Icon)} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800342 */
Dan Sandler86647982015-05-13 23:41:13 -0400343 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -0800344 public Bitmap largeIcon;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800345
346 /**
347 * The sound to play.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500348 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800349 * <p>
Chris Wren47c20a12014-06-18 17:27:29 -0400350 * A notification that is noisy is more likely to be presented as a heads-up notification.
351 * </p>
352 *
353 * <p>
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500354 * To play the default notification sound, see {@link #defaults}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800355 * </p>
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500356 * @deprecated use {@link NotificationChannel#getSound()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800357 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500358 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800359 public Uri sound;
360
361 /**
362 * Use this constant as the value for audioStreamType to request that
363 * the default stream type for notifications be used. Currently the
Jeff Sharkey098d5802012-04-26 17:30:34 -0700364 * default stream type is {@link AudioManager#STREAM_NOTIFICATION}.
John Spurlockc0650f022014-07-19 13:22:39 -0400365 *
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500366 * @deprecated Use {@link NotificationChannel#getAudioAttributes()} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800367 */
Jean-Michel Trivi81f871e2014-08-06 16:32:38 -0700368 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800369 public static final int STREAM_DEFAULT = -1;
370
371 /**
372 * The audio stream type to use when playing the sound.
373 * Should be one of the STREAM_ constants from
374 * {@link android.media.AudioManager}.
John Spurlockc0650f022014-07-19 13:22:39 -0400375 *
376 * @deprecated Use {@link #audioAttributes} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800377 */
Jean-Michel Trivi81f871e2014-08-06 16:32:38 -0700378 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800379 public int audioStreamType = STREAM_DEFAULT;
380
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800381 /**
John Spurlockc0650f022014-07-19 13:22:39 -0400382 * The default value of {@link #audioAttributes}.
383 */
384 public static final AudioAttributes AUDIO_ATTRIBUTES_DEFAULT = new AudioAttributes.Builder()
385 .setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
386 .setUsage(AudioAttributes.USAGE_NOTIFICATION)
387 .build();
388
389 /**
390 * The {@link AudioAttributes audio attributes} to use when playing the sound.
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500391 *
392 * @deprecated use {@link NotificationChannel#getAudioAttributes()} instead.
John Spurlockc0650f022014-07-19 13:22:39 -0400393 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500394 @Deprecated
John Spurlockc0650f022014-07-19 13:22:39 -0400395 public AudioAttributes audioAttributes = AUDIO_ATTRIBUTES_DEFAULT;
396
397 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500398 * The pattern with which to vibrate.
399 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800400 * <p>
401 * To vibrate the default pattern, see {@link #defaults}.
402 * </p>
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500403 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800404 * @see android.os.Vibrator#vibrate(long[],int)
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500405 * @deprecated use {@link NotificationChannel#getVibrationPattern()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800406 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500407 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800408 public long[] vibrate;
409
410 /**
411 * The color of the led. The hardware will do its best approximation.
412 *
413 * @see #FLAG_SHOW_LIGHTS
414 * @see #flags
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500415 * @deprecated use {@link NotificationChannel#shouldShowLights()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800416 */
Tor Norbye80756e32015-03-02 09:39:27 -0800417 @ColorInt
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500418 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800419 public int ledARGB;
420
421 /**
422 * The number of milliseconds for the LED to be on while it's flashing.
423 * The hardware will do its best approximation.
424 *
425 * @see #FLAG_SHOW_LIGHTS
426 * @see #flags
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500427 * @deprecated use {@link NotificationChannel#shouldShowLights()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800428 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500429 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800430 public int ledOnMS;
431
432 /**
433 * The number of milliseconds for the LED to be off while it's flashing.
434 * The hardware will do its best approximation.
435 *
436 * @see #FLAG_SHOW_LIGHTS
437 * @see #flags
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500438 *
439 * @deprecated use {@link NotificationChannel#shouldShowLights()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800440 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500441 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800442 public int ledOffMS;
443
444 /**
445 * Specifies which values should be taken from the defaults.
446 * <p>
447 * To set, OR the desired from {@link #DEFAULT_SOUND},
448 * {@link #DEFAULT_VIBRATE}, {@link #DEFAULT_LIGHTS}. For all default
449 * values, use {@link #DEFAULT_ALL}.
450 * </p>
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500451 *
452 * @deprecated use {@link NotificationChannel#getSound()} and
453 * {@link NotificationChannel#shouldShowLights()} and
454 * {@link NotificationChannel#shouldVibrate()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800455 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500456 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800457 public int defaults;
458
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800459 /**
460 * Bit to be bitwise-ored into the {@link #flags} field that should be
461 * set if you want the LED on for this notification.
462 * <ul>
463 * <li>To turn the LED off, pass 0 in the alpha channel for colorARGB
464 * or 0 for both ledOnMS and ledOffMS.</li>
465 * <li>To turn the LED on, pass 1 for ledOnMS and 0 for ledOffMS.</li>
466 * <li>To flash the LED, pass the number of milliseconds that it should
467 * be on and off to ledOnMS and ledOffMS.</li>
468 * </ul>
469 * <p>
470 * Since hardware varies, you are not guaranteed that any of the values
471 * you pass are honored exactly. Use the system defaults (TODO) if possible
472 * because they will be set to values that work on any given hardware.
473 * <p>
474 * The alpha channel must be set for forward compatibility.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500475 *
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500476 * @deprecated use {@link NotificationChannel#shouldShowLights()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800477 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500478 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800479 public static final int FLAG_SHOW_LIGHTS = 0x00000001;
480
481 /**
482 * Bit to be bitwise-ored into the {@link #flags} field that should be
483 * set if this notification is in reference to something that is ongoing,
484 * like a phone call. It should not be set if this notification is in
485 * reference to something that happened at a particular point in time,
486 * like a missed phone call.
487 */
488 public static final int FLAG_ONGOING_EVENT = 0x00000002;
489
490 /**
491 * Bit to be bitwise-ored into the {@link #flags} field that if set,
Scott Mainb8b36452009-04-26 15:50:49 -0700492 * the audio will be repeated until the notification is
493 * cancelled or the notification window is opened.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800494 */
495 public static final int FLAG_INSISTENT = 0x00000004;
496
497 /**
498 * Bit to be bitwise-ored into the {@link #flags} field that should be
Griff Hazen293977b2014-04-28 08:37:20 -0700499 * set if you would only like the sound, vibrate and ticker to be played
500 * if the notification was not already showing.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800501 */
502 public static final int FLAG_ONLY_ALERT_ONCE = 0x00000008;
503
504 /**
505 * Bit to be bitwise-ored into the {@link #flags} field that should be
506 * set if the notification should be canceled when it is clicked by the
Daniel Sandler8aa9ae62012-12-04 23:31:47 -0500507 * user.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800508 */
509 public static final int FLAG_AUTO_CANCEL = 0x00000010;
510
511 /**
512 * Bit to be bitwise-ored into the {@link #flags} field that should be
513 * set if the notification should not be canceled when the user clicks
514 * the Clear all button.
515 */
516 public static final int FLAG_NO_CLEAR = 0x00000020;
517
Dianne Hackbornd8a43f62009-08-17 23:33:56 -0700518 /**
519 * Bit to be bitwise-ored into the {@link #flags} field that should be
520 * set if this notification represents a currently running service. This
521 * will normally be set for you by {@link Service#startForeground}.
522 */
523 public static final int FLAG_FOREGROUND_SERVICE = 0x00000040;
524
Daniel Sandlere46cbd32010-06-17 10:35:26 -0400525 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500526 * Obsolete flag indicating high-priority notifications; use the priority field instead.
Joe Malin8d40d042012-11-05 11:36:40 -0800527 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500528 * @deprecated Use {@link #priority} with a positive value.
Daniel Sandlere46cbd32010-06-17 10:35:26 -0400529 */
Aurimas Liutikas514c5ef2016-05-24 15:22:55 -0700530 @Deprecated
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500531 public static final int FLAG_HIGH_PRIORITY = 0x00000080;
Daniel Sandlere46cbd32010-06-17 10:35:26 -0400532
Griff Hazendfcb0802014-02-11 12:00:00 -0800533 /**
534 * Bit to be bitswise-ored into the {@link #flags} field that should be
535 * set if this notification is relevant to the current device only
536 * and it is not recommended that it bridge to other devices.
537 */
538 public static final int FLAG_LOCAL_ONLY = 0x00000100;
539
Griff Hazen5cadc3b2014-05-20 09:55:39 -0700540 /**
541 * Bit to be bitswise-ored into the {@link #flags} field that should be
542 * set if this notification is the group summary for a group of notifications.
543 * Grouped notifications may display in a cluster or stack on devices which
544 * support such rendering. Requires a group key also be set using {@link Builder#setGroup}.
545 */
546 public static final int FLAG_GROUP_SUMMARY = 0x00000200;
547
Julia Reynoldse46bb372016-03-17 11:05:58 -0400548 /**
549 * Bit to be bitswise-ored into the {@link #flags} field that should be
550 * set if this notification is the group summary for an auto-group of notifications.
551 *
552 * @hide
553 */
554 @SystemApi
555 public static final int FLAG_AUTOGROUP_SUMMARY = 0x00000400;
556
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800557 public int flags;
558
Tor Norbyed9273d62013-05-30 15:59:53 -0700559 /** @hide */
560 @IntDef({PRIORITY_DEFAULT,PRIORITY_LOW,PRIORITY_MIN,PRIORITY_HIGH,PRIORITY_MAX})
561 @Retention(RetentionPolicy.SOURCE)
562 public @interface Priority {}
563
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800564 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500565 * Default notification {@link #priority}. If your application does not prioritize its own
566 * notifications, use this value for all notifications.
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500567 *
568 * @deprecated use {@link NotificationManager#IMPORTANCE_DEFAULT} instead.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500569 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500570 @Deprecated
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500571 public static final int PRIORITY_DEFAULT = 0;
572
573 /**
574 * Lower {@link #priority}, for items that are less important. The UI may choose to show these
575 * items smaller, or at a different position in the list, compared with your app's
576 * {@link #PRIORITY_DEFAULT} items.
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500577 *
578 * @deprecated use {@link NotificationManager#IMPORTANCE_LOW} instead.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500579 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500580 @Deprecated
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500581 public static final int PRIORITY_LOW = -1;
582
583 /**
584 * Lowest {@link #priority}; these items might not be shown to the user except under special
585 * circumstances, such as detailed notification logs.
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500586 *
587 * @deprecated use {@link NotificationManager#IMPORTANCE_MIN} instead.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500588 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500589 @Deprecated
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500590 public static final int PRIORITY_MIN = -2;
591
592 /**
593 * Higher {@link #priority}, for more important notifications or alerts. The UI may choose to
594 * show these items larger, or at a different position in notification lists, compared with
595 * your app's {@link #PRIORITY_DEFAULT} items.
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500596 *
597 * @deprecated use {@link NotificationManager#IMPORTANCE_HIGH} instead.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500598 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500599 @Deprecated
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500600 public static final int PRIORITY_HIGH = 1;
601
602 /**
603 * Highest {@link #priority}, for your application's most important items that require the
604 * user's prompt attention or input.
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500605 *
606 * @deprecated use {@link NotificationManager#IMPORTANCE_HIGH} instead.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500607 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500608 @Deprecated
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500609 public static final int PRIORITY_MAX = 2;
610
611 /**
612 * Relative priority for this notification.
Joe Malin8d40d042012-11-05 11:36:40 -0800613 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500614 * Priority is an indication of how much of the user's valuable attention should be consumed by
615 * this notification. Low-priority notifications may be hidden from the user in certain
616 * situations, while the user might be interrupted for a higher-priority notification. The
Daniel Sandler6738eee2012-11-16 12:03:32 -0500617 * system will make a determination about how to interpret this priority when presenting
618 * the notification.
Chris Wren47c20a12014-06-18 17:27:29 -0400619 *
620 * <p>
621 * A notification that is at least {@link #PRIORITY_HIGH} is more likely to be presented
622 * as a heads-up notification.
623 * </p>
624 *
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500625 * @deprecated use {@link NotificationChannel#getImportance()} instead.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500626 */
Tor Norbyed9273d62013-05-30 15:59:53 -0700627 @Priority
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500628 @Deprecated
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500629 public int priority;
Joe Malin8d40d042012-11-05 11:36:40 -0800630
Dan Sandler26e81cf2014-05-06 10:01:27 -0400631 /**
632 * Accent color (an ARGB integer like the constants in {@link android.graphics.Color})
633 * to be applied by the standard Style templates when presenting this notification.
634 *
635 * The current template design constructs a colorful header image by overlaying the
636 * {@link #icon} image (stenciled in white) atop a field of this color. Alpha components are
637 * ignored.
638 */
Tor Norbye80756e32015-03-02 09:39:27 -0800639 @ColorInt
Dan Sandler26e81cf2014-05-06 10:01:27 -0400640 public int color = COLOR_DEFAULT;
641
642 /**
643 * Special value of {@link #color} telling the system not to decorate this notification with
644 * any special color but instead use default colors when presenting this notification.
645 */
Tor Norbye80756e32015-03-02 09:39:27 -0800646 @ColorInt
Dan Sandler26e81cf2014-05-06 10:01:27 -0400647 public static final int COLOR_DEFAULT = 0; // AKA Color.TRANSPARENT
Dan Sandler0bf2ed82013-12-21 23:33:41 -0600648
649 /**
Adrian Roos4ff3b122016-02-01 12:26:13 -0800650 * Special value of {@link #color} used as a place holder for an invalid color.
Selim Cinek99104832017-01-25 14:47:33 -0800651 * @hide
Adrian Roos4ff3b122016-02-01 12:26:13 -0800652 */
653 @ColorInt
Selim Cinek99104832017-01-25 14:47:33 -0800654 public static final int COLOR_INVALID = 1;
Adrian Roos4ff3b122016-02-01 12:26:13 -0800655
656 /**
Adrian Roosc1a80b02016-04-05 14:54:55 -0700657 * Sphere of visibility of this notification, which affects how and when the SystemUI reveals
658 * the notification's presence and contents in untrusted situations (namely, on the secure
Dan Sandler0bf2ed82013-12-21 23:33:41 -0600659 * lockscreen).
660 *
661 * The default level, {@link #VISIBILITY_PRIVATE}, behaves exactly as notifications have always
662 * done on Android: The notification's {@link #icon} and {@link #tickerText} (if available) are
663 * shown in all situations, but the contents are only available if the device is unlocked for
664 * the appropriate user.
665 *
666 * A more permissive policy can be expressed by {@link #VISIBILITY_PUBLIC}; such a notification
667 * can be read even in an "insecure" context (that is, above a secure lockscreen).
668 * To modify the public version of this notification—for example, to redact some portions—see
669 * {@link Builder#setPublicVersion(Notification)}.
670 *
671 * Finally, a notification can be made {@link #VISIBILITY_SECRET}, which will suppress its icon
672 * and ticker until the user has bypassed the lockscreen.
673 */
Jeff Sharkey30e06bb2017-04-24 11:18:03 -0600674 public @Visibility int visibility;
675
676 /** @hide */
677 @IntDef(prefix = { "VISIBILITY_" }, value = {
678 VISIBILITY_PUBLIC,
679 VISIBILITY_PRIVATE,
680 VISIBILITY_SECRET,
681 })
682 @Retention(RetentionPolicy.SOURCE)
683 public @interface Visibility {}
Dan Sandler0bf2ed82013-12-21 23:33:41 -0600684
Griff Hazenfc3922d2014-08-20 11:56:44 -0700685 /**
686 * Notification visibility: Show this notification in its entirety on all lockscreens.
687 *
688 * {@see #visibility}
689 */
Dan Sandler0bf2ed82013-12-21 23:33:41 -0600690 public static final int VISIBILITY_PUBLIC = 1;
Griff Hazenfc3922d2014-08-20 11:56:44 -0700691
692 /**
693 * Notification visibility: Show this notification on all lockscreens, but conceal sensitive or
694 * private information on secure lockscreens.
695 *
696 * {@see #visibility}
697 */
Dan Sandler0bf2ed82013-12-21 23:33:41 -0600698 public static final int VISIBILITY_PRIVATE = 0;
Griff Hazenfc3922d2014-08-20 11:56:44 -0700699
700 /**
701 * Notification visibility: Do not reveal any part of this notification on a secure lockscreen.
702 *
703 * {@see #visibility}
704 */
Dan Sandler0bf2ed82013-12-21 23:33:41 -0600705 public static final int VISIBILITY_SECRET = -1;
706
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500707 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400708 * Notification category: incoming call (voice or video) or similar synchronous communication request.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500709 */
John Spurlockfd7f1e02014-03-18 16:41:57 -0400710 public static final String CATEGORY_CALL = "call";
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500711
712 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400713 * Notification category: incoming direct message (SMS, instant message, etc.).
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500714 */
John Spurlockfd7f1e02014-03-18 16:41:57 -0400715 public static final String CATEGORY_MESSAGE = "msg";
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500716
717 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400718 * Notification category: asynchronous bulk message (email).
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500719 */
John Spurlockfd7f1e02014-03-18 16:41:57 -0400720 public static final String CATEGORY_EMAIL = "email";
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500721
722 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400723 * Notification category: calendar event.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500724 */
John Spurlockfd7f1e02014-03-18 16:41:57 -0400725 public static final String CATEGORY_EVENT = "event";
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500726
727 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400728 * Notification category: promotion or advertisement.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500729 */
John Spurlockfd7f1e02014-03-18 16:41:57 -0400730 public static final String CATEGORY_PROMO = "promo";
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500731
732 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400733 * Notification category: alarm or timer.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500734 */
John Spurlockfd7f1e02014-03-18 16:41:57 -0400735 public static final String CATEGORY_ALARM = "alarm";
736
737 /**
738 * Notification category: progress of a long-running background operation.
739 */
740 public static final String CATEGORY_PROGRESS = "progress";
741
742 /**
743 * Notification category: social network or sharing update.
744 */
745 public static final String CATEGORY_SOCIAL = "social";
746
747 /**
748 * Notification category: error in background operation or authentication status.
749 */
750 public static final String CATEGORY_ERROR = "err";
751
752 /**
753 * Notification category: media transport control for playback.
754 */
755 public static final String CATEGORY_TRANSPORT = "transport";
756
757 /**
758 * Notification category: system or device status update. Reserved for system use.
759 */
760 public static final String CATEGORY_SYSTEM = "sys";
761
762 /**
763 * Notification category: indication of running background service.
764 */
765 public static final String CATEGORY_SERVICE = "service";
766
767 /**
John Spurlock0a69c8c2014-03-21 13:30:57 -0400768 * Notification category: a specific, timely recommendation for a single thing.
769 * For example, a news app might want to recommend a news story it believes the user will
770 * want to read next.
771 */
772 public static final String CATEGORY_RECOMMENDATION = "recommendation";
773
774 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400775 * Notification category: ongoing information about device or contextual status.
776 */
777 public static final String CATEGORY_STATUS = "status";
778
779 /**
John Spurlock24d3dad2015-04-02 12:24:02 -0400780 * Notification category: user-scheduled reminder.
781 */
782 public static final String CATEGORY_REMINDER = "reminder";
783
784 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400785 * One of the predefined notification categories (see the <code>CATEGORY_*</code> constants)
786 * that best describes this Notification. May be used by the system for ranking and filtering.
787 */
788 public String category;
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500789
Griff Hazen5cadc3b2014-05-20 09:55:39 -0700790 private String mGroupKey;
791
792 /**
793 * Get the key used to group this notification into a cluster or stack
794 * with other notifications on devices which support such rendering.
795 */
796 public String getGroup() {
797 return mGroupKey;
798 }
799
800 private String mSortKey;
801
802 /**
803 * Get a sort key that orders this notification among other notifications from the
804 * same package. This can be useful if an external sort was already applied and an app
805 * would like to preserve this. Notifications will be sorted lexicographically using this
806 * value, although providing different priorities in addition to providing sort key may
807 * cause this value to be ignored.
808 *
809 * <p>This sort key can also be used to order members of a notification group. See
810 * {@link Builder#setGroup}.
811 *
812 * @see String#compareTo(String)
813 */
814 public String getSortKey() {
815 return mSortKey;
816 }
817
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500818 /**
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400819 * Additional semantic data to be carried around with this Notification.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400820 * <p>
821 * The extras keys defined here are intended to capture the original inputs to {@link Builder}
822 * APIs, and are intended to be used by
823 * {@link android.service.notification.NotificationListenerService} implementations to extract
824 * detailed information from notification objects.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500825 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400826 public Bundle extras = new Bundle();
827
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400828 /**
Felipe Lemedd85da62016-06-28 11:29:54 -0700829 * All pending intents in the notification as the system needs to be able to access them but
830 * touching the extras bundle in the system process is not safe because the bundle may contain
Svet Ganovddb94882016-06-23 19:55:24 -0700831 * custom parcelable objects.
832 *
833 * @hide
834 */
Felipe Lemedd85da62016-06-28 11:29:54 -0700835 public ArraySet<PendingIntent> allPendingIntents;
Svet Ganovddb94882016-06-23 19:55:24 -0700836
837 /**
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400838 * {@link #extras} key: this is the title of the notification,
839 * as supplied to {@link Builder#setContentTitle(CharSequence)}.
840 */
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -0500841 public static final String EXTRA_TITLE = "android.title";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400842
843 /**
844 * {@link #extras} key: this is the title of the notification when shown in expanded form,
845 * e.g. as supplied to {@link BigTextStyle#setBigContentTitle(CharSequence)}.
846 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400847 public static final String EXTRA_TITLE_BIG = EXTRA_TITLE + ".big";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400848
849 /**
850 * {@link #extras} key: this is the main text payload, as supplied to
851 * {@link Builder#setContentText(CharSequence)}.
852 */
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -0500853 public static final String EXTRA_TEXT = "android.text";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400854
855 /**
856 * {@link #extras} key: this is a third line of text, as supplied to
857 * {@link Builder#setSubText(CharSequence)}.
858 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400859 public static final String EXTRA_SUB_TEXT = "android.subText";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400860
861 /**
Adrian Roose458aa82015-12-08 16:17:19 -0800862 * {@link #extras} key: this is the remote input history, as supplied to
863 * {@link Builder#setRemoteInputHistory(CharSequence[])}.
Adrian Roos005e7742016-04-13 15:02:20 -0700864 *
865 * Apps can fill this through {@link Builder#setRemoteInputHistory(CharSequence[])}
866 * with the most recent inputs that have been sent through a {@link RemoteInput} of this
867 * Notification and are expected to clear it once the it is no longer relevant (e.g. for chat
868 * notifications once the other party has responded).
869 *
870 * The extra with this key is of type CharSequence[] and contains the most recent entry at
871 * the 0 index, the second most recent at the 1 index, etc.
872 *
873 * @see Builder#setRemoteInputHistory(CharSequence[])
Adrian Roose458aa82015-12-08 16:17:19 -0800874 */
875 public static final String EXTRA_REMOTE_INPUT_HISTORY = "android.remoteInputHistory";
876
877 /**
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400878 * {@link #extras} key: this is a small piece of additional text as supplied to
879 * {@link Builder#setContentInfo(CharSequence)}.
880 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400881 public static final String EXTRA_INFO_TEXT = "android.infoText";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400882
883 /**
884 * {@link #extras} key: this is a line of summary information intended to be shown
885 * alongside expanded notifications, as supplied to (e.g.)
886 * {@link BigTextStyle#setSummaryText(CharSequence)}.
887 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400888 public static final String EXTRA_SUMMARY_TEXT = "android.summaryText";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400889
890 /**
Christoph Studer4600f9b2014-07-22 22:44:43 +0200891 * {@link #extras} key: this is the longer text shown in the big form of a
892 * {@link BigTextStyle} notification, as supplied to
893 * {@link BigTextStyle#bigText(CharSequence)}.
894 */
895 public static final String EXTRA_BIG_TEXT = "android.bigText";
896
897 /**
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400898 * {@link #extras} key: this is the resource ID of the notification's main small icon, as
899 * supplied to {@link Builder#setSmallIcon(int)}.
Julia Reynoldse071abd2017-03-22 10:52:11 -0400900 *
901 * @deprecated Use {@link #getSmallIcon()}, which supports a wider variety of icon sources.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400902 */
Julia Reynoldse071abd2017-03-22 10:52:11 -0400903 @Deprecated
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -0500904 public static final String EXTRA_SMALL_ICON = "android.icon";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400905
906 /**
907 * {@link #extras} key: this is a bitmap to be used instead of the small icon when showing the
908 * notification payload, as
909 * supplied to {@link Builder#setLargeIcon(android.graphics.Bitmap)}.
Julia Reynoldse071abd2017-03-22 10:52:11 -0400910 *
911 * @deprecated Use {@link #getLargeIcon()}, which supports a wider variety of icon sources.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400912 */
Julia Reynoldse071abd2017-03-22 10:52:11 -0400913 @Deprecated
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400914 public static final String EXTRA_LARGE_ICON = "android.largeIcon";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400915
916 /**
917 * {@link #extras} key: this is a bitmap to be used instead of the one from
918 * {@link Builder#setLargeIcon(android.graphics.Bitmap)} when the notification is
919 * shown in its expanded form, as supplied to
920 * {@link BigPictureStyle#bigLargeIcon(android.graphics.Bitmap)}.
921 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400922 public static final String EXTRA_LARGE_ICON_BIG = EXTRA_LARGE_ICON + ".big";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400923
924 /**
925 * {@link #extras} key: this is the progress value supplied to
926 * {@link Builder#setProgress(int, int, boolean)}.
927 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400928 public static final String EXTRA_PROGRESS = "android.progress";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400929
930 /**
931 * {@link #extras} key: this is the maximum value supplied to
932 * {@link Builder#setProgress(int, int, boolean)}.
933 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400934 public static final String EXTRA_PROGRESS_MAX = "android.progressMax";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400935
936 /**
937 * {@link #extras} key: whether the progress bar is indeterminate, supplied to
938 * {@link Builder#setProgress(int, int, boolean)}.
939 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400940 public static final String EXTRA_PROGRESS_INDETERMINATE = "android.progressIndeterminate";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400941
942 /**
943 * {@link #extras} key: whether {@link #when} should be shown as a count-up timer (specifically
944 * a {@link android.widget.Chronometer}) instead of a timestamp, as supplied to
945 * {@link Builder#setUsesChronometer(boolean)}.
946 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400947 public static final String EXTRA_SHOW_CHRONOMETER = "android.showChronometer";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400948
949 /**
Selim Cinek81c23aa2016-02-25 16:23:13 -0800950 * {@link #extras} key: whether the chronometer set on the notification should count down
951 * instead of counting up. Is only relevant if key {@link #EXTRA_SHOW_CHRONOMETER} is present.
Adrian Roos96b7e202016-05-17 13:50:38 -0700952 * This extra is a boolean. The default is false.
Selim Cinek81c23aa2016-02-25 16:23:13 -0800953 */
Adrian Roos96b7e202016-05-17 13:50:38 -0700954 public static final String EXTRA_CHRONOMETER_COUNT_DOWN = "android.chronometerCountDown";
Selim Cinek81c23aa2016-02-25 16:23:13 -0800955
956 /**
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400957 * {@link #extras} key: whether {@link #when} should be shown,
958 * as supplied to {@link Builder#setShowWhen(boolean)}.
959 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400960 public static final String EXTRA_SHOW_WHEN = "android.showWhen";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400961
962 /**
963 * {@link #extras} key: this is a bitmap to be shown in {@link BigPictureStyle} expanded
964 * notifications, supplied to {@link BigPictureStyle#bigPicture(android.graphics.Bitmap)}.
965 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400966 public static final String EXTRA_PICTURE = "android.picture";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400967
968 /**
969 * {@link #extras} key: An array of CharSequences to show in {@link InboxStyle} expanded
970 * notifications, each of which was supplied to {@link InboxStyle#addLine(CharSequence)}.
971 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400972 public static final String EXTRA_TEXT_LINES = "android.textLines";
Dan Sandler842dd772014-05-15 09:36:47 -0400973
974 /**
975 * {@link #extras} key: A string representing the name of the specific
976 * {@link android.app.Notification.Style} used to create this notification.
977 */
Chris Wren91ad5632013-06-05 15:05:57 -0400978 public static final String EXTRA_TEMPLATE = "android.template";
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400979
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400980 /**
Chris Wrene6c48932014-09-29 17:19:27 -0400981 * {@link #extras} key: A String array containing the people that this notification relates to,
982 * each of which was supplied to {@link Builder#addPerson(String)}.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400983 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400984 public static final String EXTRA_PEOPLE = "android.people";
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500985
986 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400987 * Allow certain system-generated notifications to appear before the device is provisioned.
988 * Only available to notifications coming from the android package.
989 * @hide
990 */
Maurice Lam96c10032017-03-29 15:42:38 -0700991 @SystemApi
John Spurlockfd7f1e02014-03-18 16:41:57 -0400992 public static final String EXTRA_ALLOW_DURING_SETUP = "android.allowDuringSetup";
993
994 /**
Jose Limae9e3b3b2014-05-18 23:44:50 -0700995 * {@link #extras} key: A
996 * {@link android.content.ContentUris content URI} pointing to an image that can be displayed
997 * in the background when the notification is selected. The URI must point to an image stream
998 * suitable for passing into
999 * {@link android.graphics.BitmapFactory#decodeStream(java.io.InputStream)
1000 * BitmapFactory.decodeStream}; all other content types will be ignored. The content provider
1001 * URI used for this purpose must require no permissions to read the image data.
1002 */
1003 public static final String EXTRA_BACKGROUND_IMAGE_URI = "android.backgroundImageUri";
1004
1005 /**
Dan Sandler842dd772014-05-15 09:36:47 -04001006 * {@link #extras} key: A
Jeff Browndba34ba2014-06-24 20:46:03 -07001007 * {@link android.media.session.MediaSession.Token} associated with a
Dan Sandler842dd772014-05-15 09:36:47 -04001008 * {@link android.app.Notification.MediaStyle} notification.
1009 */
1010 public static final String EXTRA_MEDIA_SESSION = "android.mediaSession";
1011
1012 /**
Bryan Mawhinneye191f902014-07-22 12:50:09 +01001013 * {@link #extras} key: the indices of actions to be shown in the compact view,
1014 * as supplied to (e.g.) {@link MediaStyle#setShowActionsInCompactView(int...)}.
1015 */
1016 public static final String EXTRA_COMPACT_ACTIONS = "android.compactActions";
1017
Christoph Studer943aa672014-08-03 20:31:16 +02001018 /**
Alex Hillsfc737de2016-03-23 17:33:02 -04001019 * {@link #extras} key: the username to be displayed for all messages sent by the user including
1020 * direct replies
Adrian Roos96b7e202016-05-17 13:50:38 -07001021 * {@link android.app.Notification.MessagingStyle} notification. This extra is a
1022 * {@link CharSequence}
Alex Hillsfc737de2016-03-23 17:33:02 -04001023 */
1024 public static final String EXTRA_SELF_DISPLAY_NAME = "android.selfDisplayName";
1025
1026 /**
Adrian Roos96b7e202016-05-17 13:50:38 -07001027 * {@link #extras} key: a {@link CharSequence} to be displayed as the title to a conversation
Alex Hillsd9b04d92016-04-11 16:38:16 -04001028 * represented by a {@link android.app.Notification.MessagingStyle}
Alex Hillsfc737de2016-03-23 17:33:02 -04001029 */
Alex Hillsd9b04d92016-04-11 16:38:16 -04001030 public static final String EXTRA_CONVERSATION_TITLE = "android.conversationTitle";
Alex Hillsfc737de2016-03-23 17:33:02 -04001031
1032 /**
1033 * {@link #extras} key: an array of {@link android.app.Notification.MessagingStyle.Message}
1034 * bundles provided by a
Adrian Roos96b7e202016-05-17 13:50:38 -07001035 * {@link android.app.Notification.MessagingStyle} notification. This extra is a parcelable
1036 * array of bundles.
Alex Hillsfc737de2016-03-23 17:33:02 -04001037 */
1038 public static final String EXTRA_MESSAGES = "android.messages";
1039
1040 /**
Adrian Roos437cd562017-01-18 15:47:03 -08001041 * {@link #extras} key: an array of
1042 * {@link android.app.Notification.MessagingStyle#addHistoricMessage historic}
1043 * {@link android.app.Notification.MessagingStyle.Message} bundles provided by a
1044 * {@link android.app.Notification.MessagingStyle} notification. This extra is a parcelable
1045 * array of bundles.
1046 */
1047 public static final String EXTRA_HISTORIC_MESSAGES = "android.messages.historic";
1048
1049 /**
Selim Cinek7b9605b2017-01-19 17:36:00 -08001050 * {@link #extras} key: whether the notification should be colorized as
1051 * supplied to {@link Builder#setColorized(boolean)}}.
1052 */
1053 public static final String EXTRA_COLORIZED = "android.colorized";
1054
1055 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -04001056 * @hide
1057 */
1058 public static final String EXTRA_BUILDER_APPLICATION_INFO = "android.appInfo";
1059
Selim Cinek247fa012016-02-18 09:50:48 -08001060 /**
1061 * @hide
1062 */
1063 public static final String EXTRA_CONTAINS_CUSTOM_VIEW = "android.contains.customView";
1064
Shane Brennan472a3b32016-12-12 15:28:10 -08001065 /**
1066 * {@link #extras} key: the audio contents of this notification.
1067 *
1068 * This is for use when rendering the notification on an audio-focused interface;
1069 * the audio contents are a complete sound sample that contains the contents/body of the
1070 * notification. This may be used in substitute of a Text-to-Speech reading of the
1071 * notification. For example if the notification represents a voice message this should point
1072 * to the audio of that message.
1073 *
1074 * The data stored under this key should be a String representation of a Uri that contains the
1075 * audio contents in one of the following formats: WAV, PCM 16-bit, AMR-WB.
1076 *
1077 * This extra is unnecessary if you are using {@code MessagingStyle} since each {@code Message}
1078 * has a field for holding data URI. That field can be used for audio.
1079 * See {@code Message#setData}.
1080 *
1081 * Example usage:
1082 * <pre>
1083 * {@code
1084 * Notification.Builder myBuilder = (build your Notification as normal);
1085 * myBuilder.getExtras().putString(EXTRA_AUDIO_CONTENTS_URI, myAudioUri.toString());
1086 * }
1087 * </pre>
1088 */
1089 public static final String EXTRA_AUDIO_CONTENTS_URI = "android.audioContents";
1090
Dan Sandler80eaa592016-04-14 23:34:54 -04001091 /** @hide */
1092 @SystemApi
Dan Sandler732bd6c2016-04-12 14:20:32 -04001093 public static final String EXTRA_SUBSTITUTE_APP_NAME = "android.substName";
1094
Dan Sandlerd63f9322015-05-06 15:18:49 -04001095 private Icon mSmallIcon;
1096 private Icon mLargeIcon;
1097
Julia Reynoldsb5e44b72016-08-16 15:00:25 -04001098 private String mChannelId;
Julia Reynolds2a128742016-11-28 14:29:25 -05001099 private long mTimeout;
Julia Reynoldsb5e44b72016-08-16 15:00:25 -04001100
Julia Reynolds13d898c2017-02-02 12:22:05 -05001101 private String mShortcutId;
Julia Reynolds3aedded2017-03-31 14:42:09 -04001102 private CharSequence mSettingsText;
Julia Reynolds13d898c2017-02-02 12:22:05 -05001103
Julia Reynoldsa79c3712017-04-21 10:29:57 -04001104 /** @hide */
1105 @IntDef(prefix = { "GROUP_ALERT_" }, value = {
1106 GROUP_ALERT_ALL, GROUP_ALERT_CHILDREN, GROUP_ALERT_SUMMARY
1107 })
1108 @Retention(RetentionPolicy.SOURCE)
1109 public @interface GroupAlertBehavior {}
1110
1111 /**
1112 * Constant for {@link Builder#setGroupAlertBehavior(int)}, meaning that all notifications in a
1113 * group with sound or vibration ought to make sound or vibrate (respectively), so this
1114 * notification will not be muted when it is in a group.
1115 */
1116 public static final int GROUP_ALERT_ALL = 0;
1117
1118 /**
1119 * Constant for {@link Builder#setGroupAlertBehavior(int)}, meaning that all children
1120 * notification in a group should be silenced (no sound or vibration) even if they are posted
1121 * to a {@link NotificationChannel} that has sound and/or vibration. Use this constant to
1122 * mute this notification if this notification is a group child.
1123 *
1124 * <p> For example, you might want to use this constant if you post a number of children
1125 * notifications at once (say, after a periodic sync), and only need to notify the user
1126 * audibly once.
1127 */
1128 public static final int GROUP_ALERT_SUMMARY = 1;
1129
1130 /**
1131 * Constant for {@link Builder#setGroupAlertBehavior(int)}, meaning that the summary
1132 * notification in a group should be silenced (no sound or vibration) even if they are
1133 * posted to a {@link NotificationChannel} that has sound and/or vibration. Use this constant
1134 * to mute this notification if this notification is a group summary.
1135 *
1136 * <p>For example, you might want to use this constant if only the children notifications
1137 * in your group have content and the summary is only used to visually group notifications.
1138 */
1139 public static final int GROUP_ALERT_CHILDREN = 2;
1140
1141 private int mGroupAlertBehavior = GROUP_ALERT_ALL;
1142
Julia Reynolds13d898c2017-02-02 12:22:05 -05001143 /**
1144 * If this notification is being shown as a badge, always show as a number.
1145 */
1146 public static final int BADGE_ICON_NONE = 0;
1147
1148 /**
1149 * If this notification is being shown as a badge, use the {@link #getSmallIcon()} to
1150 * represent this notification.
1151 */
1152 public static final int BADGE_ICON_SMALL = 1;
1153
1154 /**
1155 * If this notification is being shown as a badge, use the {@link #getLargeIcon()} to
1156 * represent this notification.
1157 */
1158 public static final int BADGE_ICON_LARGE = 2;
Julia Reynolds7d5b4da2017-03-20 10:18:49 -04001159 private int mBadgeIcon = BADGE_ICON_NONE;
Julia Reynolds13d898c2017-02-02 12:22:05 -05001160
Chris Wren51c75102013-07-16 20:49:17 -04001161 /**
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001162 * Structure to encapsulate a named action that can be shown as part of this notification.
1163 * It must include an icon, a label, and a {@link PendingIntent} to be fired when the action is
1164 * selected by the user.
1165 * <p>
Griff Hazen959591e2014-05-15 22:26:18 -07001166 * Apps should use {@link Notification.Builder#addAction(int, CharSequence, PendingIntent)}
1167 * or {@link Notification.Builder#addAction(Notification.Action)}
1168 * to attach actions.
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001169 */
Daniel Sandlerea2a3172013-02-20 22:24:20 -05001170 public static class Action implements Parcelable {
Shane Brennan472a3b32016-12-12 15:28:10 -08001171 /**
1172 * {@link #extras} key: Keys to a {@link Parcelable} {@link ArrayList} of
1173 * {@link RemoteInput}s.
1174 *
1175 * This is intended for {@link RemoteInput}s that only accept data, meaning
1176 * {@link RemoteInput#getAllowFreeFormInput} is false, {@link RemoteInput#getChoices}
1177 * is null or empty, and {@link RemoteInput#getAllowedDataTypes is non-null and not
1178 * empty. These {@link RemoteInput}s will be ignored by devices that do not
1179 * support non-text-based {@link RemoteInput}s. See {@link Builder#build}.
1180 *
1181 * You can test if a RemoteInput matches these constraints using
1182 * {@link RemoteInput#isDataOnly}.
1183 */
1184 private static final String EXTRA_DATA_ONLY_INPUTS = "android.extra.DATA_ONLY_INPUTS";
1185
Griff Hazen959591e2014-05-15 22:26:18 -07001186 private final Bundle mExtras;
Dan Sandler86647982015-05-13 23:41:13 -04001187 private Icon mIcon;
Griff Hazen61a9e862014-05-22 16:05:19 -07001188 private final RemoteInput[] mRemoteInputs;
Alex Hills1f27f882017-01-12 11:24:46 -05001189 private boolean mAllowGeneratedReplies = true;
Griff Hazen959591e2014-05-15 22:26:18 -07001190
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001191 /**
1192 * Small icon representing the action.
Dan Sandler86647982015-05-13 23:41:13 -04001193 *
1194 * @deprecated Use {@link Action#getIcon()} instead.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001195 */
Dan Sandler86647982015-05-13 23:41:13 -04001196 @Deprecated
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001197 public int icon;
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001198
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001199 /**
1200 * Title of the action.
1201 */
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001202 public CharSequence title;
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001203
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001204 /**
1205 * Intent to send when the user invokes this action. May be null, in which case the action
1206 * may be rendered in a disabled presentation by the system UI.
1207 */
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001208 public PendingIntent actionIntent;
Griff Hazen959591e2014-05-15 22:26:18 -07001209
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001210 private Action(Parcel in) {
Dan Sandler86647982015-05-13 23:41:13 -04001211 if (in.readInt() != 0) {
1212 mIcon = Icon.CREATOR.createFromParcel(in);
Dan Sandler68079d52015-07-22 10:45:30 -04001213 if (mIcon.getType() == Icon.TYPE_RESOURCE) {
1214 icon = mIcon.getResId();
1215 }
Dan Sandler86647982015-05-13 23:41:13 -04001216 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001217 title = TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(in);
1218 if (in.readInt() == 1) {
1219 actionIntent = PendingIntent.CREATOR.createFromParcel(in);
1220 }
Jeff Sharkeya04c7a72016-03-18 12:20:36 -06001221 mExtras = Bundle.setDefusable(in.readBundle(), true);
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001222 mRemoteInputs = in.createTypedArray(RemoteInput.CREATOR);
Alex Hills42b0c4d2016-04-26 13:35:36 -04001223 mAllowGeneratedReplies = in.readInt() == 1;
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001224 }
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001225
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001226 /**
Dan Sandler86647982015-05-13 23:41:13 -04001227 * @deprecated Use {@link android.app.Notification.Action.Builder}.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001228 */
Dan Sandler86647982015-05-13 23:41:13 -04001229 @Deprecated
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001230 public Action(int icon, CharSequence title, PendingIntent intent) {
Alex Hills1f27f882017-01-12 11:24:46 -05001231 this(Icon.createWithResource("", icon), title, intent, new Bundle(), null, true);
Griff Hazen959591e2014-05-15 22:26:18 -07001232 }
1233
Adrian Roos7af53622016-10-12 13:44:05 -07001234 /** Keep in sync with {@link Notification.Action.Builder#Builder(Action)}! */
Dan Sandler86647982015-05-13 23:41:13 -04001235 private Action(Icon icon, CharSequence title, PendingIntent intent, Bundle extras,
Alex Hills42b0c4d2016-04-26 13:35:36 -04001236 RemoteInput[] remoteInputs, boolean allowGeneratedReplies) {
Dan Sandler86647982015-05-13 23:41:13 -04001237 this.mIcon = icon;
Gus Prevasf5bff46f2015-08-24 10:34:28 -04001238 if (icon != null && icon.getType() == Icon.TYPE_RESOURCE) {
1239 this.icon = icon.getResId();
1240 }
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001241 this.title = title;
1242 this.actionIntent = intent;
Griff Hazen959591e2014-05-15 22:26:18 -07001243 this.mExtras = extras != null ? extras : new Bundle();
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001244 this.mRemoteInputs = remoteInputs;
Alex Hills42b0c4d2016-04-26 13:35:36 -04001245 this.mAllowGeneratedReplies = allowGeneratedReplies;
Griff Hazen959591e2014-05-15 22:26:18 -07001246 }
1247
1248 /**
Dan Sandler86647982015-05-13 23:41:13 -04001249 * Return an icon representing the action.
1250 */
1251 public Icon getIcon() {
1252 if (mIcon == null && icon != 0) {
1253 // you snuck an icon in here without using the builder; let's try to keep it
1254 mIcon = Icon.createWithResource("", icon);
1255 }
1256 return mIcon;
1257 }
1258
1259 /**
Griff Hazen959591e2014-05-15 22:26:18 -07001260 * Get additional metadata carried around with this Action.
1261 */
1262 public Bundle getExtras() {
1263 return mExtras;
1264 }
1265
1266 /**
Alex Hills42b0c4d2016-04-26 13:35:36 -04001267 * Return whether the platform should automatically generate possible replies for this
1268 * {@link Action}
1269 */
1270 public boolean getAllowGeneratedReplies() {
1271 return mAllowGeneratedReplies;
1272 }
1273
1274 /**
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001275 * Get the list of inputs to be collected from the user when this action is sent.
Shane Brennan472a3b32016-12-12 15:28:10 -08001276 * May return null if no remote inputs were added. Only returns inputs which accept
1277 * a text input. For inputs which only accept data use {@link #getDataOnlyRemoteInputs}.
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001278 */
1279 public RemoteInput[] getRemoteInputs() {
1280 return mRemoteInputs;
1281 }
1282
1283 /**
Shane Brennan472a3b32016-12-12 15:28:10 -08001284 * Get the list of inputs to be collected from the user that ONLY accept data when this
1285 * action is sent. These remote inputs are guaranteed to return true on a call to
1286 * {@link RemoteInput#isDataOnly}.
1287 *
Shane Brennanf670d6c2017-04-25 13:05:45 -07001288 * Returns null if there are no data-only remote inputs.
Shane Brennan472a3b32016-12-12 15:28:10 -08001289 *
1290 * This method exists so that legacy RemoteInput collectors that pre-date the addition
1291 * of non-textual RemoteInputs do not access these remote inputs.
1292 */
1293 public RemoteInput[] getDataOnlyRemoteInputs() {
1294 return (RemoteInput[]) mExtras.getParcelableArray(EXTRA_DATA_ONLY_INPUTS);
1295 }
1296
1297 /**
Griff Hazen959591e2014-05-15 22:26:18 -07001298 * Builder class for {@link Action} objects.
1299 */
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001300 public static final class Builder {
Dan Sandler86647982015-05-13 23:41:13 -04001301 private final Icon mIcon;
Griff Hazen959591e2014-05-15 22:26:18 -07001302 private final CharSequence mTitle;
1303 private final PendingIntent mIntent;
Alex Hills1f27f882017-01-12 11:24:46 -05001304 private boolean mAllowGeneratedReplies = true;
Griff Hazen959591e2014-05-15 22:26:18 -07001305 private final Bundle mExtras;
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001306 private ArrayList<RemoteInput> mRemoteInputs;
Griff Hazen959591e2014-05-15 22:26:18 -07001307
1308 /**
1309 * Construct a new builder for {@link Action} object.
1310 * @param icon icon to show for this action
1311 * @param title the title of the action
1312 * @param intent the {@link PendingIntent} to fire when users trigger this action
1313 */
Dan Sandler86647982015-05-13 23:41:13 -04001314 @Deprecated
Griff Hazen959591e2014-05-15 22:26:18 -07001315 public Builder(int icon, CharSequence title, PendingIntent intent) {
Adrian Roos7af53622016-10-12 13:44:05 -07001316 this(Icon.createWithResource("", icon), title, intent);
Dan Sandler86647982015-05-13 23:41:13 -04001317 }
1318
1319 /**
1320 * Construct a new builder for {@link Action} object.
1321 * @param icon icon to show for this action
1322 * @param title the title of the action
1323 * @param intent the {@link PendingIntent} to fire when users trigger this action
1324 */
1325 public Builder(Icon icon, CharSequence title, PendingIntent intent) {
Alex Hills1f27f882017-01-12 11:24:46 -05001326 this(icon, title, intent, new Bundle(), null, true);
Griff Hazen959591e2014-05-15 22:26:18 -07001327 }
1328
1329 /**
1330 * Construct a new builder for {@link Action} object using the fields from an
1331 * {@link Action}.
1332 * @param action the action to read fields from.
1333 */
1334 public Builder(Action action) {
Adrian Roos7af53622016-10-12 13:44:05 -07001335 this(action.getIcon(), action.title, action.actionIntent,
1336 new Bundle(action.mExtras), action.getRemoteInputs(),
1337 action.getAllowGeneratedReplies());
Griff Hazen959591e2014-05-15 22:26:18 -07001338 }
1339
Dan Sandler86647982015-05-13 23:41:13 -04001340 private Builder(Icon icon, CharSequence title, PendingIntent intent, Bundle extras,
Adrian Roos7af53622016-10-12 13:44:05 -07001341 RemoteInput[] remoteInputs, boolean allowGeneratedReplies) {
Griff Hazen959591e2014-05-15 22:26:18 -07001342 mIcon = icon;
1343 mTitle = title;
1344 mIntent = intent;
1345 mExtras = extras;
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001346 if (remoteInputs != null) {
1347 mRemoteInputs = new ArrayList<RemoteInput>(remoteInputs.length);
1348 Collections.addAll(mRemoteInputs, remoteInputs);
1349 }
Adrian Roos7af53622016-10-12 13:44:05 -07001350 mAllowGeneratedReplies = allowGeneratedReplies;
Griff Hazen959591e2014-05-15 22:26:18 -07001351 }
1352
1353 /**
1354 * Merge additional metadata into this builder.
1355 *
1356 * <p>Values within the Bundle will replace existing extras values in this Builder.
1357 *
1358 * @see Notification.Action#extras
1359 */
1360 public Builder addExtras(Bundle extras) {
1361 if (extras != null) {
1362 mExtras.putAll(extras);
1363 }
1364 return this;
1365 }
1366
1367 /**
1368 * Get the metadata Bundle used by this Builder.
1369 *
1370 * <p>The returned Bundle is shared with this Builder.
1371 */
1372 public Bundle getExtras() {
1373 return mExtras;
1374 }
1375
1376 /**
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001377 * Add an input to be collected from the user when this action is sent.
1378 * Response values can be retrieved from the fired intent by using the
1379 * {@link RemoteInput#getResultsFromIntent} function.
1380 * @param remoteInput a {@link RemoteInput} to add to the action
1381 * @return this object for method chaining
1382 */
1383 public Builder addRemoteInput(RemoteInput remoteInput) {
1384 if (mRemoteInputs == null) {
1385 mRemoteInputs = new ArrayList<RemoteInput>();
1386 }
1387 mRemoteInputs.add(remoteInput);
1388 return this;
1389 }
1390
1391 /**
Alex Hills42b0c4d2016-04-26 13:35:36 -04001392 * Set whether the platform should automatically generate possible replies to add to
1393 * {@link RemoteInput#getChoices()}. If the {@link Action} doesn't have a
1394 * {@link RemoteInput}, this has no effect.
1395 * @param allowGeneratedReplies {@code true} to allow generated replies, {@code false}
1396 * otherwise
1397 * @return this object for method chaining
Alex Hills1f27f882017-01-12 11:24:46 -05001398 * The default value is {@code true}
Alex Hills42b0c4d2016-04-26 13:35:36 -04001399 */
1400 public Builder setAllowGeneratedReplies(boolean allowGeneratedReplies) {
1401 mAllowGeneratedReplies = allowGeneratedReplies;
1402 return this;
1403 }
1404
1405 /**
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001406 * Apply an extender to this action builder. Extenders may be used to add
1407 * metadata or change options on this builder.
1408 */
Griff Hazen61a9e862014-05-22 16:05:19 -07001409 public Builder extend(Extender extender) {
1410 extender.extend(this);
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001411 return this;
1412 }
1413
1414 /**
Griff Hazen959591e2014-05-15 22:26:18 -07001415 * Combine all of the options that have been set and return a new {@link Action}
1416 * object.
1417 * @return the built action
1418 */
1419 public Action build() {
Shane Brennan472a3b32016-12-12 15:28:10 -08001420 ArrayList<RemoteInput> dataOnlyInputs = new ArrayList<>();
1421 RemoteInput[] previousDataInputs =
1422 (RemoteInput[]) mExtras.getParcelableArray(EXTRA_DATA_ONLY_INPUTS);
Felipe Lemedfd11962017-01-18 18:38:46 -08001423 if (previousDataInputs != null) {
Shane Brennan472a3b32016-12-12 15:28:10 -08001424 for (RemoteInput input : previousDataInputs) {
1425 dataOnlyInputs.add(input);
1426 }
1427 }
1428 List<RemoteInput> textInputs = new ArrayList<>();
1429 if (mRemoteInputs != null) {
1430 for (RemoteInput input : mRemoteInputs) {
1431 if (input.isDataOnly()) {
1432 dataOnlyInputs.add(input);
1433 } else {
1434 textInputs.add(input);
1435 }
1436 }
1437 }
1438 if (!dataOnlyInputs.isEmpty()) {
1439 RemoteInput[] dataInputsArr =
1440 dataOnlyInputs.toArray(new RemoteInput[dataOnlyInputs.size()]);
1441 mExtras.putParcelableArray(EXTRA_DATA_ONLY_INPUTS, dataInputsArr);
1442 }
1443 RemoteInput[] textInputsArr = textInputs.isEmpty()
1444 ? null : textInputs.toArray(new RemoteInput[textInputs.size()]);
1445 return new Action(mIcon, mTitle, mIntent, mExtras, textInputsArr,
Alex Hills42b0c4d2016-04-26 13:35:36 -04001446 mAllowGeneratedReplies);
Griff Hazen959591e2014-05-15 22:26:18 -07001447 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001448 }
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001449
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001450 @Override
1451 public Action clone() {
1452 return new Action(
Dan Sandler86647982015-05-13 23:41:13 -04001453 getIcon(),
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001454 title,
1455 actionIntent, // safe to alias
Julia Reynolds53c934c2016-09-16 14:03:43 -04001456 mExtras == null ? new Bundle() : new Bundle(mExtras),
Alex Hills42b0c4d2016-04-26 13:35:36 -04001457 getRemoteInputs(),
1458 getAllowGeneratedReplies());
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001459 }
1460 @Override
1461 public int describeContents() {
1462 return 0;
1463 }
1464 @Override
1465 public void writeToParcel(Parcel out, int flags) {
Dan Sandler86647982015-05-13 23:41:13 -04001466 final Icon ic = getIcon();
1467 if (ic != null) {
1468 out.writeInt(1);
1469 ic.writeToParcel(out, 0);
1470 } else {
1471 out.writeInt(0);
1472 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001473 TextUtils.writeToParcel(title, out, flags);
1474 if (actionIntent != null) {
1475 out.writeInt(1);
1476 actionIntent.writeToParcel(out, flags);
1477 } else {
1478 out.writeInt(0);
1479 }
Griff Hazen959591e2014-05-15 22:26:18 -07001480 out.writeBundle(mExtras);
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001481 out.writeTypedArray(mRemoteInputs, flags);
Alex Hills42b0c4d2016-04-26 13:35:36 -04001482 out.writeInt(mAllowGeneratedReplies ? 1 : 0);
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001483 }
Griff Hazen959591e2014-05-15 22:26:18 -07001484 public static final Parcelable.Creator<Action> CREATOR =
1485 new Parcelable.Creator<Action>() {
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001486 public Action createFromParcel(Parcel in) {
1487 return new Action(in);
1488 }
1489 public Action[] newArray(int size) {
1490 return new Action[size];
1491 }
1492 };
Griff Hazen61a9e862014-05-22 16:05:19 -07001493
1494 /**
1495 * Extender interface for use with {@link Builder#extend}. Extenders may be used to add
1496 * metadata or change options on an action builder.
1497 */
1498 public interface Extender {
1499 /**
1500 * Apply this extender to a notification action builder.
1501 * @param builder the builder to be modified.
1502 * @return the build object for chaining.
1503 */
1504 public Builder extend(Builder builder);
1505 }
1506
1507 /**
1508 * Wearable extender for notification actions. To add extensions to an action,
1509 * create a new {@link android.app.Notification.Action.WearableExtender} object using
1510 * the {@code WearableExtender()} constructor and apply it to a
1511 * {@link android.app.Notification.Action.Builder} using
1512 * {@link android.app.Notification.Action.Builder#extend}.
1513 *
1514 * <pre class="prettyprint">
1515 * Notification.Action action = new Notification.Action.Builder(
1516 * R.drawable.archive_all, "Archive all", actionIntent)
Griff Hazen14f57992014-05-26 09:07:14 -07001517 * .extend(new Notification.Action.WearableExtender()
Griff Hazen61a9e862014-05-22 16:05:19 -07001518 * .setAvailableOffline(false))
Griff Hazen14f57992014-05-26 09:07:14 -07001519 * .build();</pre>
Griff Hazen61a9e862014-05-22 16:05:19 -07001520 */
1521 public static final class WearableExtender implements Extender {
1522 /** Notification action extra which contains wearable extensions */
1523 private static final String EXTRA_WEARABLE_EXTENSIONS = "android.wearable.EXTENSIONS";
1524
Pete Gastaf6781d2014-10-07 15:17:05 -04001525 // Keys within EXTRA_WEARABLE_EXTENSIONS for wearable options.
Griff Hazen61a9e862014-05-22 16:05:19 -07001526 private static final String KEY_FLAGS = "flags";
Pete Gastaf6781d2014-10-07 15:17:05 -04001527 private static final String KEY_IN_PROGRESS_LABEL = "inProgressLabel";
1528 private static final String KEY_CONFIRM_LABEL = "confirmLabel";
1529 private static final String KEY_CANCEL_LABEL = "cancelLabel";
Griff Hazen61a9e862014-05-22 16:05:19 -07001530
1531 // Flags bitwise-ored to mFlags
1532 private static final int FLAG_AVAILABLE_OFFLINE = 0x1;
Alex Hills9ab3a232016-04-05 14:54:56 -04001533 private static final int FLAG_HINT_LAUNCHES_ACTIVITY = 1 << 1;
Alex Hills9f087612016-06-07 09:08:59 -04001534 private static final int FLAG_HINT_DISPLAY_INLINE = 1 << 2;
Griff Hazen61a9e862014-05-22 16:05:19 -07001535
1536 // Default value for flags integer
1537 private static final int DEFAULT_FLAGS = FLAG_AVAILABLE_OFFLINE;
1538
1539 private int mFlags = DEFAULT_FLAGS;
1540
Pete Gastaf6781d2014-10-07 15:17:05 -04001541 private CharSequence mInProgressLabel;
1542 private CharSequence mConfirmLabel;
1543 private CharSequence mCancelLabel;
1544
Griff Hazen61a9e862014-05-22 16:05:19 -07001545 /**
1546 * Create a {@link android.app.Notification.Action.WearableExtender} with default
1547 * options.
1548 */
1549 public WearableExtender() {
1550 }
1551
1552 /**
1553 * Create a {@link android.app.Notification.Action.WearableExtender} by reading
1554 * wearable options present in an existing notification action.
1555 * @param action the notification action to inspect.
1556 */
1557 public WearableExtender(Action action) {
1558 Bundle wearableBundle = action.getExtras().getBundle(EXTRA_WEARABLE_EXTENSIONS);
1559 if (wearableBundle != null) {
1560 mFlags = wearableBundle.getInt(KEY_FLAGS, DEFAULT_FLAGS);
Pete Gastaf6781d2014-10-07 15:17:05 -04001561 mInProgressLabel = wearableBundle.getCharSequence(KEY_IN_PROGRESS_LABEL);
1562 mConfirmLabel = wearableBundle.getCharSequence(KEY_CONFIRM_LABEL);
1563 mCancelLabel = wearableBundle.getCharSequence(KEY_CANCEL_LABEL);
Griff Hazen61a9e862014-05-22 16:05:19 -07001564 }
1565 }
1566
1567 /**
1568 * Apply wearable extensions to a notification action that is being built. This is
1569 * typically called by the {@link android.app.Notification.Action.Builder#extend}
1570 * method of {@link android.app.Notification.Action.Builder}.
1571 */
1572 @Override
1573 public Action.Builder extend(Action.Builder builder) {
1574 Bundle wearableBundle = new Bundle();
1575
1576 if (mFlags != DEFAULT_FLAGS) {
1577 wearableBundle.putInt(KEY_FLAGS, mFlags);
1578 }
Pete Gastaf6781d2014-10-07 15:17:05 -04001579 if (mInProgressLabel != null) {
1580 wearableBundle.putCharSequence(KEY_IN_PROGRESS_LABEL, mInProgressLabel);
1581 }
1582 if (mConfirmLabel != null) {
1583 wearableBundle.putCharSequence(KEY_CONFIRM_LABEL, mConfirmLabel);
1584 }
1585 if (mCancelLabel != null) {
1586 wearableBundle.putCharSequence(KEY_CANCEL_LABEL, mCancelLabel);
1587 }
Griff Hazen61a9e862014-05-22 16:05:19 -07001588
1589 builder.getExtras().putBundle(EXTRA_WEARABLE_EXTENSIONS, wearableBundle);
1590 return builder;
1591 }
1592
1593 @Override
1594 public WearableExtender clone() {
1595 WearableExtender that = new WearableExtender();
1596 that.mFlags = this.mFlags;
Pete Gastaf6781d2014-10-07 15:17:05 -04001597 that.mInProgressLabel = this.mInProgressLabel;
1598 that.mConfirmLabel = this.mConfirmLabel;
1599 that.mCancelLabel = this.mCancelLabel;
Griff Hazen61a9e862014-05-22 16:05:19 -07001600 return that;
1601 }
1602
1603 /**
1604 * Set whether this action is available when the wearable device is not connected to
1605 * a companion device. The user can still trigger this action when the wearable device is
1606 * offline, but a visual hint will indicate that the action may not be available.
1607 * Defaults to true.
1608 */
1609 public WearableExtender setAvailableOffline(boolean availableOffline) {
1610 setFlag(FLAG_AVAILABLE_OFFLINE, availableOffline);
1611 return this;
1612 }
1613
1614 /**
1615 * Get whether this action is available when the wearable device is not connected to
1616 * a companion device. The user can still trigger this action when the wearable device is
1617 * offline, but a visual hint will indicate that the action may not be available.
1618 * Defaults to true.
1619 */
1620 public boolean isAvailableOffline() {
1621 return (mFlags & FLAG_AVAILABLE_OFFLINE) != 0;
1622 }
1623
1624 private void setFlag(int mask, boolean value) {
1625 if (value) {
1626 mFlags |= mask;
1627 } else {
1628 mFlags &= ~mask;
1629 }
1630 }
Pete Gastaf6781d2014-10-07 15:17:05 -04001631
1632 /**
1633 * Set a label to display while the wearable is preparing to automatically execute the
1634 * action. This is usually a 'ing' verb ending in ellipsis like "Sending..."
1635 *
1636 * @param label the label to display while the action is being prepared to execute
1637 * @return this object for method chaining
1638 */
1639 public WearableExtender setInProgressLabel(CharSequence label) {
1640 mInProgressLabel = label;
1641 return this;
1642 }
1643
1644 /**
1645 * Get the label to display while the wearable is preparing to automatically execute
1646 * the action. This is usually a 'ing' verb ending in ellipsis like "Sending..."
1647 *
1648 * @return the label to display while the action is being prepared to execute
1649 */
1650 public CharSequence getInProgressLabel() {
1651 return mInProgressLabel;
1652 }
1653
1654 /**
1655 * Set a label to display to confirm that the action should be executed.
1656 * This is usually an imperative verb like "Send".
1657 *
1658 * @param label the label to confirm the action should be executed
1659 * @return this object for method chaining
1660 */
1661 public WearableExtender setConfirmLabel(CharSequence label) {
1662 mConfirmLabel = label;
1663 return this;
1664 }
1665
1666 /**
1667 * Get the label to display to confirm that the action should be executed.
1668 * This is usually an imperative verb like "Send".
1669 *
1670 * @return the label to confirm the action should be executed
1671 */
1672 public CharSequence getConfirmLabel() {
1673 return mConfirmLabel;
1674 }
1675
1676 /**
1677 * Set a label to display to cancel the action.
1678 * This is usually an imperative verb, like "Cancel".
1679 *
1680 * @param label the label to display to cancel the action
1681 * @return this object for method chaining
1682 */
1683 public WearableExtender setCancelLabel(CharSequence label) {
1684 mCancelLabel = label;
1685 return this;
1686 }
1687
1688 /**
1689 * Get the label to display to cancel the action.
1690 * This is usually an imperative verb like "Cancel".
1691 *
1692 * @return the label to display to cancel the action
1693 */
1694 public CharSequence getCancelLabel() {
1695 return mCancelLabel;
1696 }
Alex Hills9ab3a232016-04-05 14:54:56 -04001697
1698 /**
1699 * Set a hint that this Action will launch an {@link Activity} directly, telling the
1700 * platform that it can generate the appropriate transitions.
1701 * @param hintLaunchesActivity {@code true} if the content intent will launch
1702 * an activity and transitions should be generated, false otherwise.
1703 * @return this object for method chaining
1704 */
Alex Hills4ec3ff42016-04-12 11:36:18 -04001705 public WearableExtender setHintLaunchesActivity(
Alex Hills9ab3a232016-04-05 14:54:56 -04001706 boolean hintLaunchesActivity) {
1707 setFlag(FLAG_HINT_LAUNCHES_ACTIVITY, hintLaunchesActivity);
1708 return this;
1709 }
1710
1711 /**
1712 * Get a hint that this Action will launch an {@link Activity} directly, telling the
1713 * platform that it can generate the appropriate transitions
1714 * @return {@code true} if the content intent will launch an activity and transitions
1715 * should be generated, false otherwise. The default value is {@code false} if this was
1716 * never set.
1717 */
Alex Hills4ec3ff42016-04-12 11:36:18 -04001718 public boolean getHintLaunchesActivity() {
Alex Hills9ab3a232016-04-05 14:54:56 -04001719 return (mFlags & FLAG_HINT_LAUNCHES_ACTIVITY) != 0;
1720 }
Alex Hills9f087612016-06-07 09:08:59 -04001721
1722 /**
1723 * Set a hint that this Action should be displayed inline.
1724 *
1725 * @param hintDisplayInline {@code true} if action should be displayed inline, false
1726 * otherwise
1727 * @return this object for method chaining
1728 */
1729 public WearableExtender setHintDisplayActionInline(
1730 boolean hintDisplayInline) {
1731 setFlag(FLAG_HINT_DISPLAY_INLINE, hintDisplayInline);
1732 return this;
1733 }
1734
1735 /**
1736 * Get a hint that this Action should be displayed inline.
1737 *
Dmitri Plotnikov9e9cfd92016-12-21 10:52:26 -08001738 * @return {@code true} if the Action should be displayed inline, {@code false}
Alex Hills9f087612016-06-07 09:08:59 -04001739 * otherwise. The default value is {@code false} if this was never set.
1740 */
1741 public boolean getHintDisplayActionInline() {
1742 return (mFlags & FLAG_HINT_DISPLAY_INLINE) != 0;
1743 }
Griff Hazen61a9e862014-05-22 16:05:19 -07001744 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001745 }
1746
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001747 /**
1748 * Array of all {@link Action} structures attached to this notification by
1749 * {@link Builder#addAction(int, CharSequence, PendingIntent)}. Mostly useful for instances of
1750 * {@link android.service.notification.NotificationListenerService} that provide an alternative
1751 * interface for invoking actions.
1752 */
Daniel Sandlerea2a3172013-02-20 22:24:20 -05001753 public Action[] actions;
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001754
1755 /**
Dan Sandler0bf2ed82013-12-21 23:33:41 -06001756 * Replacement version of this notification whose content will be shown
1757 * in an insecure context such as atop a secure keyguard. See {@link #visibility}
1758 * and {@link #VISIBILITY_PUBLIC}.
1759 */
1760 public Notification publicVersion;
1761
1762 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001763 * Constructs a Notification object with default values.
Joe Onorato46439ce2010-11-19 13:56:21 -08001764 * You might want to consider using {@link Builder} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001765 */
1766 public Notification()
1767 {
1768 this.when = System.currentTimeMillis();
Selim Cinekb85f36fd2016-04-20 18:46:36 -07001769 this.creationTime = System.currentTimeMillis();
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001770 this.priority = PRIORITY_DEFAULT;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001771 }
1772
1773 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001774 * @hide
1775 */
1776 public Notification(Context context, int icon, CharSequence tickerText, long when,
1777 CharSequence contentTitle, CharSequence contentText, Intent contentIntent)
1778 {
Chris Wren1ce4b6d2015-06-11 10:19:43 -04001779 new Builder(context)
1780 .setWhen(when)
1781 .setSmallIcon(icon)
1782 .setTicker(tickerText)
1783 .setContentTitle(contentTitle)
1784 .setContentText(contentText)
1785 .setContentIntent(PendingIntent.getActivity(context, 0, contentIntent, 0))
1786 .buildInto(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001787 }
1788
1789 /**
1790 * Constructs a Notification object with the information needed to
1791 * have a status bar icon without the standard expanded view.
1792 *
1793 * @param icon The resource id of the icon to put in the status bar.
1794 * @param tickerText The text that flows by in the status bar when the notification first
1795 * activates.
1796 * @param when The time to show in the time field. In the System.currentTimeMillis
1797 * timebase.
Joe Onorato46439ce2010-11-19 13:56:21 -08001798 *
1799 * @deprecated Use {@link Builder} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001800 */
Joe Onorato46439ce2010-11-19 13:56:21 -08001801 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001802 public Notification(int icon, CharSequence tickerText, long when)
1803 {
1804 this.icon = icon;
1805 this.tickerText = tickerText;
1806 this.when = when;
Selim Cinekb85f36fd2016-04-20 18:46:36 -07001807 this.creationTime = System.currentTimeMillis();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001808 }
1809
1810 /**
1811 * Unflatten the notification from a parcel.
1812 */
Svet Ganovddb94882016-06-23 19:55:24 -07001813 @SuppressWarnings("unchecked")
1814 public Notification(Parcel parcel) {
1815 // IMPORTANT: Add unmarshaling code in readFromParcel as the pending
1816 // intents in extras are always written as the last entry.
1817 readFromParcelImpl(parcel);
1818 // Must be read last!
Felipe Lemedd85da62016-06-28 11:29:54 -07001819 allPendingIntents = (ArraySet<PendingIntent>) parcel.readArraySet(null);
Svet Ganovddb94882016-06-23 19:55:24 -07001820 }
1821
1822 private void readFromParcelImpl(Parcel parcel)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001823 {
1824 int version = parcel.readInt();
1825
1826 when = parcel.readLong();
Selim Cinekb85f36fd2016-04-20 18:46:36 -07001827 creationTime = parcel.readLong();
Dan Sandler3936e7a2015-05-19 20:59:12 -04001828 if (parcel.readInt() != 0) {
1829 mSmallIcon = Icon.CREATOR.createFromParcel(parcel);
Dan Sandler4e787062015-06-17 15:09:48 -04001830 if (mSmallIcon.getType() == Icon.TYPE_RESOURCE) {
1831 icon = mSmallIcon.getResId();
1832 }
Dan Sandler3936e7a2015-05-19 20:59:12 -04001833 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001834 number = parcel.readInt();
1835 if (parcel.readInt() != 0) {
1836 contentIntent = PendingIntent.CREATOR.createFromParcel(parcel);
1837 }
1838 if (parcel.readInt() != 0) {
1839 deleteIntent = PendingIntent.CREATOR.createFromParcel(parcel);
1840 }
1841 if (parcel.readInt() != 0) {
1842 tickerText = TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(parcel);
1843 }
1844 if (parcel.readInt() != 0) {
Joe Onorato46439ce2010-11-19 13:56:21 -08001845 tickerView = RemoteViews.CREATOR.createFromParcel(parcel);
Joe Onoratoef1e7762010-09-17 18:38:38 -04001846 }
1847 if (parcel.readInt() != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001848 contentView = RemoteViews.CREATOR.createFromParcel(parcel);
1849 }
Joe Onorato561d3852010-11-20 18:09:34 -08001850 if (parcel.readInt() != 0) {
Dan Sandlerd63f9322015-05-06 15:18:49 -04001851 mLargeIcon = Icon.CREATOR.createFromParcel(parcel);
Joe Onorato561d3852010-11-20 18:09:34 -08001852 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001853 defaults = parcel.readInt();
1854 flags = parcel.readInt();
1855 if (parcel.readInt() != 0) {
1856 sound = Uri.CREATOR.createFromParcel(parcel);
1857 }
1858
1859 audioStreamType = parcel.readInt();
John Spurlockc0650f022014-07-19 13:22:39 -04001860 if (parcel.readInt() != 0) {
1861 audioAttributes = AudioAttributes.CREATOR.createFromParcel(parcel);
1862 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001863 vibrate = parcel.createLongArray();
1864 ledARGB = parcel.readInt();
1865 ledOnMS = parcel.readInt();
1866 ledOffMS = parcel.readInt();
1867 iconLevel = parcel.readInt();
Daniel Sandlere46cbd32010-06-17 10:35:26 -04001868
1869 if (parcel.readInt() != 0) {
1870 fullScreenIntent = PendingIntent.CREATOR.createFromParcel(parcel);
1871 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001872
1873 priority = parcel.readInt();
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001874
John Spurlockfd7f1e02014-03-18 16:41:57 -04001875 category = parcel.readString();
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001876
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001877 mGroupKey = parcel.readString();
1878
1879 mSortKey = parcel.readString();
1880
Jeff Sharkeya04c7a72016-03-18 12:20:36 -06001881 extras = Bundle.setDefusable(parcel.readBundle(), true); // may be null
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001882
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001883 actions = parcel.createTypedArray(Action.CREATOR); // may be null
1884
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001885 if (parcel.readInt() != 0) {
Daniel Sandlerf3b73432012-03-27 15:01:25 -04001886 bigContentView = RemoteViews.CREATOR.createFromParcel(parcel);
1887 }
Dan Sandler0bf2ed82013-12-21 23:33:41 -06001888
Chris Wren8fd39ec2014-02-27 17:43:26 -05001889 if (parcel.readInt() != 0) {
1890 headsUpContentView = RemoteViews.CREATOR.createFromParcel(parcel);
1891 }
1892
Dan Sandler0bf2ed82013-12-21 23:33:41 -06001893 visibility = parcel.readInt();
1894
1895 if (parcel.readInt() != 0) {
1896 publicVersion = Notification.CREATOR.createFromParcel(parcel);
1897 }
Dan Sandler26e81cf2014-05-06 10:01:27 -04001898
1899 color = parcel.readInt();
Julia Reynoldsb5e44b72016-08-16 15:00:25 -04001900
1901 if (parcel.readInt() != 0) {
1902 mChannelId = parcel.readString();
1903 }
Julia Reynolds2a128742016-11-28 14:29:25 -05001904 mTimeout = parcel.readLong();
Julia Reynolds13d898c2017-02-02 12:22:05 -05001905
1906 if (parcel.readInt() != 0) {
1907 mShortcutId = parcel.readString();
1908 }
1909
1910 mBadgeIcon = parcel.readInt();
Julia Reynolds3aedded2017-03-31 14:42:09 -04001911
1912 if (parcel.readInt() != 0) {
1913 mSettingsText = TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(parcel);
1914 }
Julia Reynoldsa79c3712017-04-21 10:29:57 -04001915
1916 mGroupAlertBehavior = parcel.readInt();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001917 }
1918
Andy Stadler110988c2010-12-03 14:29:16 -08001919 @Override
Joe Onorato18e69df2010-05-17 22:26:12 -07001920 public Notification clone() {
1921 Notification that = new Notification();
Daniel Sandler1a497d32013-04-18 14:52:45 -04001922 cloneInto(that, true);
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001923 return that;
1924 }
Joe Onorato18e69df2010-05-17 22:26:12 -07001925
Daniel Sandler1a497d32013-04-18 14:52:45 -04001926 /**
1927 * Copy all (or if heavy is false, all except Bitmaps and RemoteViews) members
1928 * of this into that.
1929 * @hide
1930 */
1931 public void cloneInto(Notification that, boolean heavy) {
Joe Onorato18e69df2010-05-17 22:26:12 -07001932 that.when = this.when;
Selim Cinekb85f36fd2016-04-20 18:46:36 -07001933 that.creationTime = this.creationTime;
Dan Sandlerd63f9322015-05-06 15:18:49 -04001934 that.mSmallIcon = this.mSmallIcon;
Joe Onorato18e69df2010-05-17 22:26:12 -07001935 that.number = this.number;
1936
1937 // PendingIntents are global, so there's no reason (or way) to clone them.
1938 that.contentIntent = this.contentIntent;
1939 that.deleteIntent = this.deleteIntent;
Daniel Sandlere46cbd32010-06-17 10:35:26 -04001940 that.fullScreenIntent = this.fullScreenIntent;
Joe Onorato18e69df2010-05-17 22:26:12 -07001941
1942 if (this.tickerText != null) {
1943 that.tickerText = this.tickerText.toString();
1944 }
Daniel Sandler1a497d32013-04-18 14:52:45 -04001945 if (heavy && this.tickerView != null) {
Joe Onorato46439ce2010-11-19 13:56:21 -08001946 that.tickerView = this.tickerView.clone();
Joe Onoratoef1e7762010-09-17 18:38:38 -04001947 }
Daniel Sandler1a497d32013-04-18 14:52:45 -04001948 if (heavy && this.contentView != null) {
Joe Onorato18e69df2010-05-17 22:26:12 -07001949 that.contentView = this.contentView.clone();
1950 }
Dan Sandlerd63f9322015-05-06 15:18:49 -04001951 if (heavy && this.mLargeIcon != null) {
1952 that.mLargeIcon = this.mLargeIcon;
Joe Onorato561d3852010-11-20 18:09:34 -08001953 }
Jozef BABJAKa8b91832011-02-22 08:05:08 +01001954 that.iconLevel = this.iconLevel;
Joe Onorato18e69df2010-05-17 22:26:12 -07001955 that.sound = this.sound; // android.net.Uri is immutable
1956 that.audioStreamType = this.audioStreamType;
John Spurlockc0650f022014-07-19 13:22:39 -04001957 if (this.audioAttributes != null) {
1958 that.audioAttributes = new AudioAttributes.Builder(this.audioAttributes).build();
1959 }
Joe Onorato18e69df2010-05-17 22:26:12 -07001960
1961 final long[] vibrate = this.vibrate;
1962 if (vibrate != null) {
1963 final int N = vibrate.length;
1964 final long[] vib = that.vibrate = new long[N];
1965 System.arraycopy(vibrate, 0, vib, 0, N);
1966 }
1967
1968 that.ledARGB = this.ledARGB;
1969 that.ledOnMS = this.ledOnMS;
1970 that.ledOffMS = this.ledOffMS;
1971 that.defaults = this.defaults;
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001972
Joe Onorato18e69df2010-05-17 22:26:12 -07001973 that.flags = this.flags;
1974
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001975 that.priority = this.priority;
Joe Malin8d40d042012-11-05 11:36:40 -08001976
John Spurlockfd7f1e02014-03-18 16:41:57 -04001977 that.category = this.category;
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001978
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001979 that.mGroupKey = this.mGroupKey;
1980
1981 that.mSortKey = this.mSortKey;
1982
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001983 if (this.extras != null) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04001984 try {
1985 that.extras = new Bundle(this.extras);
1986 // will unparcel
1987 that.extras.size();
1988 } catch (BadParcelableException e) {
1989 Log.e(TAG, "could not unparcel extras from notification: " + this, e);
1990 that.extras = null;
1991 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001992 }
1993
Felipe Lemedd85da62016-06-28 11:29:54 -07001994 if (!ArrayUtils.isEmpty(allPendingIntents)) {
1995 that.allPendingIntents = new ArraySet<>(allPendingIntents);
Svet Ganovddb94882016-06-23 19:55:24 -07001996 }
1997
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001998 if (this.actions != null) {
1999 that.actions = new Action[this.actions.length];
2000 for(int i=0; i<this.actions.length; i++) {
liangweikang63b03b52017-03-16 19:22:15 +08002001 if ( this.actions[i] != null) {
2002 that.actions[i] = this.actions[i].clone();
2003 }
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002004 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002005 }
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002006
Daniel Sandler1a497d32013-04-18 14:52:45 -04002007 if (heavy && this.bigContentView != null) {
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002008 that.bigContentView = this.bigContentView.clone();
2009 }
Daniel Sandler1a497d32013-04-18 14:52:45 -04002010
Chris Wren8fd39ec2014-02-27 17:43:26 -05002011 if (heavy && this.headsUpContentView != null) {
2012 that.headsUpContentView = this.headsUpContentView.clone();
2013 }
2014
Dan Sandler0bf2ed82013-12-21 23:33:41 -06002015 that.visibility = this.visibility;
2016
2017 if (this.publicVersion != null) {
2018 that.publicVersion = new Notification();
2019 this.publicVersion.cloneInto(that.publicVersion, heavy);
2020 }
2021
Dan Sandler26e81cf2014-05-06 10:01:27 -04002022 that.color = this.color;
2023
Julia Reynoldsb5e44b72016-08-16 15:00:25 -04002024 that.mChannelId = this.mChannelId;
Julia Reynolds2a128742016-11-28 14:29:25 -05002025 that.mTimeout = this.mTimeout;
Julia Reynolds3aedded2017-03-31 14:42:09 -04002026 that.mShortcutId = this.mShortcutId;
2027 that.mBadgeIcon = this.mBadgeIcon;
2028 that.mSettingsText = this.mSettingsText;
Julia Reynoldsa79c3712017-04-21 10:29:57 -04002029 that.mGroupAlertBehavior = this.mGroupAlertBehavior;
Julia Reynoldsb5e44b72016-08-16 15:00:25 -04002030
Daniel Sandler1a497d32013-04-18 14:52:45 -04002031 if (!heavy) {
2032 that.lightenPayload(); // will clean out extras
2033 }
2034 }
2035
2036 /**
2037 * Removes heavyweight parts of the Notification object for archival or for sending to
2038 * listeners when the full contents are not necessary.
2039 * @hide
2040 */
2041 public final void lightenPayload() {
2042 tickerView = null;
2043 contentView = null;
2044 bigContentView = null;
Chris Wren8fd39ec2014-02-27 17:43:26 -05002045 headsUpContentView = null;
Dan Sandlerd63f9322015-05-06 15:18:49 -04002046 mLargeIcon = null;
Dan Sandler50128532015-12-08 15:42:41 -05002047 if (extras != null && !extras.isEmpty()) {
2048 final Set<String> keyset = extras.keySet();
2049 final int N = keyset.size();
2050 final String[] keys = keyset.toArray(new String[N]);
2051 for (int i=0; i<N; i++) {
2052 final String key = keys[i];
Dmitri Plotnikov22281362017-01-30 11:16:21 -08002053 if (TvExtender.EXTRA_TV_EXTENDER.equals(key)) {
2054 continue;
2055 }
Dan Sandler50128532015-12-08 15:42:41 -05002056 final Object obj = extras.get(key);
2057 if (obj != null &&
2058 ( obj instanceof Parcelable
2059 || obj instanceof Parcelable[]
2060 || obj instanceof SparseArray
2061 || obj instanceof ArrayList)) {
2062 extras.remove(key);
2063 }
2064 }
Daniel Sandler1a497d32013-04-18 14:52:45 -04002065 }
Joe Onorato18e69df2010-05-17 22:26:12 -07002066 }
2067
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04002068 /**
2069 * Make sure this CharSequence is safe to put into a bundle, which basically
2070 * means it had better not be some custom Parcelable implementation.
2071 * @hide
2072 */
2073 public static CharSequence safeCharSequence(CharSequence cs) {
Christoph Studer535ec612014-09-03 15:47:47 +02002074 if (cs == null) return cs;
2075 if (cs.length() > MAX_CHARSEQUENCE_LENGTH) {
2076 cs = cs.subSequence(0, MAX_CHARSEQUENCE_LENGTH);
2077 }
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04002078 if (cs instanceof Parcelable) {
2079 Log.e(TAG, "warning: " + cs.getClass().getCanonicalName()
2080 + " instance is a custom Parcelable and not allowed in Notification");
2081 return cs.toString();
2082 }
Selim Cinek60a54252016-02-26 17:03:25 -08002083 return removeTextSizeSpans(cs);
2084 }
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04002085
Selim Cinek60a54252016-02-26 17:03:25 -08002086 private static CharSequence removeTextSizeSpans(CharSequence charSequence) {
2087 if (charSequence instanceof Spanned) {
2088 Spanned ss = (Spanned) charSequence;
2089 Object[] spans = ss.getSpans(0, ss.length(), Object.class);
2090 SpannableStringBuilder builder = new SpannableStringBuilder(ss.toString());
2091 for (Object span : spans) {
2092 Object resultSpan = span;
Selim Cinek89991a22016-03-07 19:51:54 -08002093 if (resultSpan instanceof CharacterStyle) {
2094 resultSpan = ((CharacterStyle) span).getUnderlying();
2095 }
2096 if (resultSpan instanceof TextAppearanceSpan) {
2097 TextAppearanceSpan originalSpan = (TextAppearanceSpan) resultSpan;
Selim Cinek60a54252016-02-26 17:03:25 -08002098 resultSpan = new TextAppearanceSpan(
2099 originalSpan.getFamily(),
2100 originalSpan.getTextStyle(),
2101 -1,
2102 originalSpan.getTextColor(),
2103 originalSpan.getLinkTextColor());
Selim Cinek89991a22016-03-07 19:51:54 -08002104 } else if (resultSpan instanceof RelativeSizeSpan
2105 || resultSpan instanceof AbsoluteSizeSpan) {
Selim Cinek60a54252016-02-26 17:03:25 -08002106 continue;
Selim Cinek89991a22016-03-07 19:51:54 -08002107 } else {
2108 resultSpan = span;
Selim Cinek60a54252016-02-26 17:03:25 -08002109 }
2110 builder.setSpan(resultSpan, ss.getSpanStart(span), ss.getSpanEnd(span),
2111 ss.getSpanFlags(span));
2112 }
2113 return builder;
2114 }
2115 return charSequence;
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04002116 }
2117
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002118 public int describeContents() {
2119 return 0;
2120 }
2121
2122 /**
Dan Sandler4e787062015-06-17 15:09:48 -04002123 * Flatten this notification into a parcel.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002124 */
Svet Ganovddb94882016-06-23 19:55:24 -07002125 public void writeToParcel(Parcel parcel, int flags) {
2126 // We need to mark all pending intents getting into the notification
2127 // system as being put there to later allow the notification ranker
2128 // to launch them and by doing so add the app to the battery saver white
2129 // list for a short period of time. The problem is that the system
2130 // cannot look into the extras as there may be parcelables there that
2131 // the platform does not know how to handle. To go around that we have
2132 // an explicit list of the pending intents in the extras bundle.
Felipe Lemedd85da62016-06-28 11:29:54 -07002133 final boolean collectPendingIntents = (allPendingIntents == null);
Svet Ganovddb94882016-06-23 19:55:24 -07002134 if (collectPendingIntents) {
2135 PendingIntent.setOnMarshaledListener(
2136 (PendingIntent intent, Parcel out, int outFlags) -> {
2137 if (parcel == out) {
Felipe Lemedd85da62016-06-28 11:29:54 -07002138 if (allPendingIntents == null) {
2139 allPendingIntents = new ArraySet<>();
Svet Ganovddb94882016-06-23 19:55:24 -07002140 }
Felipe Lemedd85da62016-06-28 11:29:54 -07002141 allPendingIntents.add(intent);
Svet Ganovddb94882016-06-23 19:55:24 -07002142 }
2143 });
2144 }
2145 try {
2146 // IMPORTANT: Add marshaling code in writeToParcelImpl as we
Julia Reynolds13d898c2017-02-02 12:22:05 -05002147 // want to intercept all pending events written to the parcel.
Svet Ganovddb94882016-06-23 19:55:24 -07002148 writeToParcelImpl(parcel, flags);
2149 // Must be written last!
Felipe Lemedd85da62016-06-28 11:29:54 -07002150 parcel.writeArraySet(allPendingIntents);
Svet Ganovddb94882016-06-23 19:55:24 -07002151 } finally {
2152 if (collectPendingIntents) {
2153 PendingIntent.setOnMarshaledListener(null);
2154 }
2155 }
2156 }
2157
2158 private void writeToParcelImpl(Parcel parcel, int flags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002159 parcel.writeInt(1);
2160
2161 parcel.writeLong(when);
Selim Cinekb85f36fd2016-04-20 18:46:36 -07002162 parcel.writeLong(creationTime);
Dan Sandler4e787062015-06-17 15:09:48 -04002163 if (mSmallIcon == null && icon != 0) {
2164 // you snuck an icon in here without using the builder; let's try to keep it
2165 mSmallIcon = Icon.createWithResource("", icon);
2166 }
Dan Sandler3936e7a2015-05-19 20:59:12 -04002167 if (mSmallIcon != null) {
2168 parcel.writeInt(1);
2169 mSmallIcon.writeToParcel(parcel, 0);
2170 } else {
2171 parcel.writeInt(0);
2172 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002173 parcel.writeInt(number);
2174 if (contentIntent != null) {
2175 parcel.writeInt(1);
2176 contentIntent.writeToParcel(parcel, 0);
2177 } else {
2178 parcel.writeInt(0);
2179 }
2180 if (deleteIntent != null) {
2181 parcel.writeInt(1);
2182 deleteIntent.writeToParcel(parcel, 0);
2183 } else {
2184 parcel.writeInt(0);
2185 }
2186 if (tickerText != null) {
2187 parcel.writeInt(1);
2188 TextUtils.writeToParcel(tickerText, parcel, flags);
2189 } else {
2190 parcel.writeInt(0);
2191 }
Joe Onorato46439ce2010-11-19 13:56:21 -08002192 if (tickerView != null) {
Joe Onoratoef1e7762010-09-17 18:38:38 -04002193 parcel.writeInt(1);
Joe Onorato46439ce2010-11-19 13:56:21 -08002194 tickerView.writeToParcel(parcel, 0);
Joe Onoratoef1e7762010-09-17 18:38:38 -04002195 } else {
2196 parcel.writeInt(0);
2197 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002198 if (contentView != null) {
2199 parcel.writeInt(1);
2200 contentView.writeToParcel(parcel, 0);
2201 } else {
2202 parcel.writeInt(0);
2203 }
Selim Cinek279fa862016-06-14 10:57:25 -07002204 if (mLargeIcon == null && largeIcon != null) {
2205 // you snuck an icon in here without using the builder; let's try to keep it
2206 mLargeIcon = Icon.createWithBitmap(largeIcon);
2207 }
Dan Sandlerd63f9322015-05-06 15:18:49 -04002208 if (mLargeIcon != null) {
Joe Onorato561d3852010-11-20 18:09:34 -08002209 parcel.writeInt(1);
Dan Sandlerd63f9322015-05-06 15:18:49 -04002210 mLargeIcon.writeToParcel(parcel, 0);
Joe Onorato561d3852010-11-20 18:09:34 -08002211 } else {
2212 parcel.writeInt(0);
2213 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002214
2215 parcel.writeInt(defaults);
2216 parcel.writeInt(this.flags);
2217
2218 if (sound != null) {
2219 parcel.writeInt(1);
2220 sound.writeToParcel(parcel, 0);
2221 } else {
2222 parcel.writeInt(0);
2223 }
2224 parcel.writeInt(audioStreamType);
John Spurlockc0650f022014-07-19 13:22:39 -04002225
2226 if (audioAttributes != null) {
2227 parcel.writeInt(1);
2228 audioAttributes.writeToParcel(parcel, 0);
2229 } else {
2230 parcel.writeInt(0);
2231 }
2232
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002233 parcel.writeLongArray(vibrate);
2234 parcel.writeInt(ledARGB);
2235 parcel.writeInt(ledOnMS);
2236 parcel.writeInt(ledOffMS);
2237 parcel.writeInt(iconLevel);
Daniel Sandlere46cbd32010-06-17 10:35:26 -04002238
2239 if (fullScreenIntent != null) {
2240 parcel.writeInt(1);
2241 fullScreenIntent.writeToParcel(parcel, 0);
2242 } else {
2243 parcel.writeInt(0);
2244 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002245
2246 parcel.writeInt(priority);
Joe Malin8d40d042012-11-05 11:36:40 -08002247
John Spurlockfd7f1e02014-03-18 16:41:57 -04002248 parcel.writeString(category);
Joe Malin8d40d042012-11-05 11:36:40 -08002249
Griff Hazen5cadc3b2014-05-20 09:55:39 -07002250 parcel.writeString(mGroupKey);
2251
2252 parcel.writeString(mSortKey);
2253
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002254 parcel.writeBundle(extras); // null ok
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002255
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002256 parcel.writeTypedArray(actions, 0); // null ok
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002257
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002258 if (bigContentView != null) {
2259 parcel.writeInt(1);
2260 bigContentView.writeToParcel(parcel, 0);
2261 } else {
2262 parcel.writeInt(0);
2263 }
Dan Sandler0bf2ed82013-12-21 23:33:41 -06002264
Chris Wren8fd39ec2014-02-27 17:43:26 -05002265 if (headsUpContentView != null) {
2266 parcel.writeInt(1);
2267 headsUpContentView.writeToParcel(parcel, 0);
2268 } else {
2269 parcel.writeInt(0);
2270 }
2271
Dan Sandler0bf2ed82013-12-21 23:33:41 -06002272 parcel.writeInt(visibility);
2273
2274 if (publicVersion != null) {
2275 parcel.writeInt(1);
2276 publicVersion.writeToParcel(parcel, 0);
2277 } else {
2278 parcel.writeInt(0);
2279 }
Dan Sandler26e81cf2014-05-06 10:01:27 -04002280
2281 parcel.writeInt(color);
Julia Reynoldsb5e44b72016-08-16 15:00:25 -04002282
2283 if (mChannelId != null) {
2284 parcel.writeInt(1);
2285 parcel.writeString(mChannelId);
2286 } else {
2287 parcel.writeInt(0);
2288 }
Julia Reynolds2a128742016-11-28 14:29:25 -05002289 parcel.writeLong(mTimeout);
Julia Reynolds13d898c2017-02-02 12:22:05 -05002290
2291 if (mShortcutId != null) {
2292 parcel.writeInt(1);
2293 parcel.writeString(mShortcutId);
2294 } else {
2295 parcel.writeInt(0);
2296 }
2297
2298 parcel.writeInt(mBadgeIcon);
Julia Reynolds3aedded2017-03-31 14:42:09 -04002299
2300 if (mSettingsText != null) {
2301 parcel.writeInt(1);
2302 TextUtils.writeToParcel(mSettingsText, parcel, flags);
2303 } else {
2304 parcel.writeInt(0);
2305 }
Julia Reynoldsa79c3712017-04-21 10:29:57 -04002306
2307 parcel.writeInt(mGroupAlertBehavior);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002308 }
2309
2310 /**
2311 * Parcelable.Creator that instantiates Notification objects
2312 */
2313 public static final Parcelable.Creator<Notification> CREATOR
2314 = new Parcelable.Creator<Notification>()
2315 {
2316 public Notification createFromParcel(Parcel parcel)
2317 {
2318 return new Notification(parcel);
2319 }
2320
2321 public Notification[] newArray(int size)
2322 {
2323 return new Notification[size];
2324 }
2325 };
2326
2327 /**
2328 * Sets the {@link #contentView} field to be a view with the standard "Latest Event"
2329 * layout.
2330 *
2331 * <p>Uses the {@link #icon} and {@link #when} fields to set the icon and time fields
2332 * in the view.</p>
2333 * @param context The context for your application / activity.
2334 * @param contentTitle The title that goes in the expanded entry.
2335 * @param contentText The text that goes in the expanded entry.
2336 * @param contentIntent The intent to launch when the user clicks the expanded notification.
2337 * If this is an activity, it must include the
2338 * {@link android.content.Intent#FLAG_ACTIVITY_NEW_TASK} flag, which requires
Scott Main7aee61f2011-02-08 11:25:01 -08002339 * that you take care of task management as described in the
2340 * <a href="{@docRoot}guide/topics/fundamentals/tasks-and-back-stack.html">Tasks and Back
2341 * Stack</a> document.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002342 *
Joe Onorato46439ce2010-11-19 13:56:21 -08002343 * @deprecated Use {@link Builder} instead.
Chris Wrena05db382015-06-24 15:18:34 -04002344 * @removed
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002345 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002346 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002347 public void setLatestEventInfo(Context context,
2348 CharSequence contentTitle, CharSequence contentText, PendingIntent contentIntent) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002349 if (context.getApplicationInfo().targetSdkVersion > Build.VERSION_CODES.LOLLIPOP_MR1){
2350 Log.e(TAG, "setLatestEventInfo() is deprecated and you should feel deprecated.",
2351 new Throwable());
2352 }
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002353
Selim Cinek4ac6f602016-06-13 15:47:03 -07002354 if (context.getApplicationInfo().targetSdkVersion < Build.VERSION_CODES.N) {
2355 extras.putBoolean(EXTRA_SHOW_WHEN, true);
2356 }
2357
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002358 // ensure that any information already set directly is preserved
2359 final Notification.Builder builder = new Notification.Builder(context, this);
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002360
2361 // now apply the latestEventInfo fields
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002362 if (contentTitle != null) {
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002363 builder.setContentTitle(contentTitle);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002364 }
2365 if (contentText != null) {
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002366 builder.setContentText(contentText);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002367 }
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002368 builder.setContentIntent(contentIntent);
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002369
2370 builder.build(); // callers expect this notification to be ready to use
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002371 }
2372
Julia Reynoldsda303542015-11-23 14:00:20 -05002373 /**
2374 * @hide
2375 */
2376 public static void addFieldsFromContext(Context context, Notification notification) {
Julia Reynoldse071abd2017-03-22 10:52:11 -04002377 addFieldsFromContext(context.getApplicationInfo(), notification);
Jeff Sharkey012bc7b2016-04-11 16:30:27 -06002378 }
2379
2380 /**
2381 * @hide
2382 */
Julia Reynoldse071abd2017-03-22 10:52:11 -04002383 public static void addFieldsFromContext(ApplicationInfo ai, Notification notification) {
Jeff Sharkey012bc7b2016-04-11 16:30:27 -06002384 notification.extras.putParcelable(EXTRA_BUILDER_APPLICATION_INFO, ai);
Julia Reynoldsda303542015-11-23 14:00:20 -05002385 }
2386
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002387 @Override
2388 public String toString() {
2389 StringBuilder sb = new StringBuilder();
Julia Reynoldsb9e712e2017-04-17 10:31:03 -04002390 sb.append("Notification(channel=");
Julia Reynoldsbad42972017-04-25 13:52:49 -04002391 sb.append(getChannelId());
Julia Reynoldsb9e712e2017-04-17 10:31:03 -04002392 sb.append(" pri=");
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002393 sb.append(priority);
2394 sb.append(" contentView=");
Joe Onoratoc9596d62011-01-12 17:03:11 -08002395 if (contentView != null) {
2396 sb.append(contentView.getPackage());
2397 sb.append("/0x");
2398 sb.append(Integer.toHexString(contentView.getLayoutId()));
2399 } else {
2400 sb.append("null");
2401 }
2402 sb.append(" vibrate=");
Daniel Sandler6738eee2012-11-16 12:03:32 -05002403 if ((this.defaults & DEFAULT_VIBRATE) != 0) {
2404 sb.append("default");
2405 } else if (this.vibrate != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002406 int N = this.vibrate.length-1;
2407 sb.append("[");
2408 for (int i=0; i<N; i++) {
2409 sb.append(this.vibrate[i]);
2410 sb.append(',');
2411 }
Simon Schoar8cf97d92009-06-10 22:08:37 +02002412 if (N != -1) {
2413 sb.append(this.vibrate[N]);
2414 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002415 sb.append("]");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002416 } else {
2417 sb.append("null");
2418 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002419 sb.append(" sound=");
Daniel Sandler6738eee2012-11-16 12:03:32 -05002420 if ((this.defaults & DEFAULT_SOUND) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002421 sb.append("default");
Daniel Sandler6738eee2012-11-16 12:03:32 -05002422 } else if (this.sound != null) {
2423 sb.append(this.sound.toString());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002424 } else {
2425 sb.append("null");
2426 }
Chris Wren365b6d32015-07-16 10:39:26 -04002427 if (this.tickerText != null) {
2428 sb.append(" tick");
2429 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002430 sb.append(" defaults=0x");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002431 sb.append(Integer.toHexString(this.defaults));
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002432 sb.append(" flags=0x");
Daniel Sandlere46cbd32010-06-17 10:35:26 -04002433 sb.append(Integer.toHexString(this.flags));
Dan Sandler26e81cf2014-05-06 10:01:27 -04002434 sb.append(String.format(" color=0x%08x", this.color));
Griff Hazen5cadc3b2014-05-20 09:55:39 -07002435 if (this.category != null) {
2436 sb.append(" category=");
2437 sb.append(this.category);
2438 }
2439 if (this.mGroupKey != null) {
2440 sb.append(" groupKey=");
2441 sb.append(this.mGroupKey);
2442 }
2443 if (this.mSortKey != null) {
2444 sb.append(" sortKey=");
2445 sb.append(this.mSortKey);
2446 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002447 if (actions != null) {
Dan Sandler1b718782014-07-18 12:43:45 -04002448 sb.append(" actions=");
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002449 sb.append(actions.length);
Dan Sandler1b718782014-07-18 12:43:45 -04002450 }
2451 sb.append(" vis=");
2452 sb.append(visibilityToString(this.visibility));
2453 if (this.publicVersion != null) {
2454 sb.append(" publicVersion=");
2455 sb.append(publicVersion.toString());
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002456 }
2457 sb.append(")");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002458 return sb.toString();
2459 }
Joe Onorato46439ce2010-11-19 13:56:21 -08002460
Dan Sandler1b718782014-07-18 12:43:45 -04002461 /**
2462 * {@hide}
2463 */
2464 public static String visibilityToString(int vis) {
2465 switch (vis) {
2466 case VISIBILITY_PRIVATE:
2467 return "PRIVATE";
2468 case VISIBILITY_PUBLIC:
2469 return "PUBLIC";
2470 case VISIBILITY_SECRET:
2471 return "SECRET";
2472 default:
2473 return "UNKNOWN(" + String.valueOf(vis) + ")";
2474 }
2475 }
2476
Joe Onoratocb109a02011-01-18 17:57:41 -08002477 /**
John Spurlock1d881a12015-03-18 19:21:54 -04002478 * {@hide}
2479 */
2480 public static String priorityToString(@Priority int pri) {
2481 switch (pri) {
2482 case PRIORITY_MIN:
2483 return "MIN";
2484 case PRIORITY_LOW:
2485 return "LOW";
2486 case PRIORITY_DEFAULT:
2487 return "DEFAULT";
2488 case PRIORITY_HIGH:
2489 return "HIGH";
2490 case PRIORITY_MAX:
2491 return "MAX";
2492 default:
2493 return "UNKNOWN(" + String.valueOf(pri) + ")";
2494 }
2495 }
2496
Jeff Sharkey000ce802017-04-29 13:13:27 -06002497 /** @removed */
2498 @Deprecated
Julia Reynolds37856052016-11-11 09:20:07 -05002499 public String getChannel() {
Julia Reynoldsb5e44b72016-08-16 15:00:25 -04002500 return mChannelId;
2501 }
2502
2503 /**
Julia Reynoldsbad42972017-04-25 13:52:49 -04002504 * Returns the id of the channel this notification posts to.
2505 */
2506 public String getChannelId() {
2507 return mChannelId;
2508 }
2509
Jeff Sharkey000ce802017-04-29 13:13:27 -06002510 /** @removed */
2511 @Deprecated
Julia Reynolds2a128742016-11-28 14:29:25 -05002512 public long getTimeout() {
2513 return mTimeout;
2514 }
2515
2516 /**
Julia Reynoldsbad42972017-04-25 13:52:49 -04002517 * Returns the duration from posting after which this notification should be canceled by the
2518 * system, if it's not canceled already.
2519 */
2520 public long getTimeoutAfter() {
2521 return mTimeout;
2522 }
2523
2524 /**
Julia Reynoldse071abd2017-03-22 10:52:11 -04002525 * Returns what icon should be shown for this notification if it is being displayed in a
2526 * Launcher that supports badging. Will be one of {@link #BADGE_ICON_NONE},
2527 * {@link #BADGE_ICON_SMALL}, or {@link #BADGE_ICON_LARGE}.
2528 */
2529 public int getBadgeIconType() {
2530 return mBadgeIcon;
2531 }
2532
2533 /**
Julia Reynolds13d898c2017-02-02 12:22:05 -05002534 * Returns the {@link ShortcutInfo#getId() id} that this notification supersedes, if any.
Julia Reynoldsbad42972017-04-25 13:52:49 -04002535 *
2536 * <p>Used by some Launchers that display notification content to hide shortcuts that duplicate
2537 * notifications.
Julia Reynolds13d898c2017-02-02 12:22:05 -05002538 */
2539 public String getShortcutId() {
2540 return mShortcutId;
2541 }
2542
Julia Reynolds3aedded2017-03-31 14:42:09 -04002543
2544 /**
2545 * Returns the settings text provided to {@link Builder#setSettingsText(CharSequence)}.
2546 */
2547 public CharSequence getSettingsText() {
2548 return mSettingsText;
2549 }
2550
Julia Reynolds13d898c2017-02-02 12:22:05 -05002551 /**
Julia Reynoldsa79c3712017-04-21 10:29:57 -04002552 * Returns which type of notifications in a group are responsible for audibly alerting the
2553 * user. See {@link #GROUP_ALERT_ALL}, {@link #GROUP_ALERT_CHILDREN},
2554 * {@link #GROUP_ALERT_SUMMARY}.
2555 */
2556 public @GroupAlertBehavior int getGroupAlertBehavior() {
2557 return mGroupAlertBehavior;
2558 }
2559
2560 /**
Dan Sandlerd63f9322015-05-06 15:18:49 -04002561 * The small icon representing this notification in the status bar and content view.
2562 *
2563 * @return the small icon representing this notification.
2564 *
2565 * @see Builder#getSmallIcon()
2566 * @see Builder#setSmallIcon(Icon)
2567 */
2568 public Icon getSmallIcon() {
2569 return mSmallIcon;
2570 }
2571
2572 /**
2573 * Used when notifying to clean up legacy small icons.
2574 * @hide
2575 */
2576 public void setSmallIcon(Icon icon) {
2577 mSmallIcon = icon;
2578 }
2579
2580 /**
2581 * The large icon shown in this notification's content view.
2582 * @see Builder#getLargeIcon()
2583 * @see Builder#setLargeIcon(Icon)
2584 */
2585 public Icon getLargeIcon() {
2586 return mLargeIcon;
2587 }
2588
2589 /**
Christoph Studer4600f9b2014-07-22 22:44:43 +02002590 * @hide
2591 */
Christoph Studerc8db24b2014-07-25 17:50:30 +02002592 public boolean isGroupSummary() {
2593 return mGroupKey != null && (flags & FLAG_GROUP_SUMMARY) != 0;
2594 }
2595
2596 /**
2597 * @hide
2598 */
2599 public boolean isGroupChild() {
2600 return mGroupKey != null && (flags & FLAG_GROUP_SUMMARY) == 0;
2601 }
2602
2603 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002604 * Builder class for {@link Notification} objects.
Joe Malin8d40d042012-11-05 11:36:40 -08002605 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002606 * Provides a convenient way to set the various fields of a {@link Notification} and generate
Scott Main183bf112012-08-13 19:12:13 -07002607 * content views using the platform's notification layout template. If your app supports
2608 * versions of Android as old as API level 4, you can instead use
2609 * {@link android.support.v4.app.NotificationCompat.Builder NotificationCompat.Builder},
2610 * available in the <a href="{@docRoot}tools/extras/support-library.html">Android Support
2611 * library</a>.
Joe Malin8d40d042012-11-05 11:36:40 -08002612 *
Scott Main183bf112012-08-13 19:12:13 -07002613 * <p>Example:
Joe Malin8d40d042012-11-05 11:36:40 -08002614 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002615 * <pre class="prettyprint">
Scott Main183bf112012-08-13 19:12:13 -07002616 * Notification noti = new Notification.Builder(mContext)
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002617 * .setContentTitle(&quot;New mail from &quot; + sender.toString())
2618 * .setContentText(subject)
2619 * .setSmallIcon(R.drawable.new_mail)
2620 * .setLargeIcon(aBitmap)
Chris Wrenfbd96ba2012-05-01 12:03:58 -04002621 * .build();
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002622 * </pre>
Joe Onoratocb109a02011-01-18 17:57:41 -08002623 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002624 public static class Builder {
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05002625 /**
2626 * @hide
2627 */
2628 public static final String EXTRA_REBUILD_CONTENT_VIEW_ACTION_COUNT =
2629 "android.rebuild.contentViewActionCount";
2630 /**
2631 * @hide
2632 */
2633 public static final String EXTRA_REBUILD_BIG_CONTENT_VIEW_ACTION_COUNT
2634 = "android.rebuild.bigViewActionCount";
2635 /**
2636 * @hide
2637 */
2638 public static final String EXTRA_REBUILD_HEADS_UP_CONTENT_VIEW_ACTION_COUNT
2639 = "android.rebuild.hudViewActionCount";
2640
Daniel Sandler602ad1c2012-06-12 16:06:27 -04002641 private static final int MAX_ACTION_BUTTONS = 3;
Daniel Sandler8680bf82012-05-15 16:52:52 -04002642
Selim Cinek6743c0b2017-01-18 18:24:01 -08002643 private static final boolean USE_ONLY_TITLE_IN_LOW_PRIORITY_SUMMARY =
2644 SystemProperties.getBoolean("notifications.only_title", true);
2645
Joe Onorato46439ce2010-11-19 13:56:21 -08002646 private Context mContext;
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002647 private Notification mN;
2648 private Bundle mUserExtras = new Bundle();
Chris Wrenfbd96ba2012-05-01 12:03:58 -04002649 private Style mStyle;
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002650 private ArrayList<Action> mActions = new ArrayList<Action>(MAX_ACTION_BUTTONS);
2651 private ArrayList<String> mPersonList = new ArrayList<String>();
2652 private NotificationColorUtil mColorUtil;
Selim Cinek7b9605b2017-01-19 17:36:00 -08002653 private boolean mIsLegacy;
2654 private boolean mIsLegacyInitialized;
Christoph Studer7ac80e62014-08-04 16:01:57 +02002655
2656 /**
Adrian Roos4ff3b122016-02-01 12:26:13 -08002657 * Caches a contrast-enhanced version of {@link #mCachedContrastColorIsFor}.
2658 */
2659 private int mCachedContrastColor = COLOR_INVALID;
2660 private int mCachedContrastColorIsFor = COLOR_INVALID;
Adrian Roos487374f2017-01-11 15:48:14 -08002661 /**
2662 * Caches a ambient version of {@link #mCachedContrastColorIsFor}.
2663 */
2664 private int mCachedAmbientColor = COLOR_INVALID;
2665 private int mCachedAmbientColorIsFor = COLOR_INVALID;
Adrian Roos4ff3b122016-02-01 12:26:13 -08002666
2667 /**
Adrian Roos70d7aa32017-01-11 15:39:06 -08002668 * Caches an instance of StandardTemplateParams. Note that this may have been used before,
2669 * so make sure to call {@link StandardTemplateParams#reset()} before using it.
2670 */
2671 StandardTemplateParams mParams = new StandardTemplateParams();
Selim Cinek7b9605b2017-01-19 17:36:00 -08002672 private int mTextColorsAreForBackground = COLOR_INVALID;
2673 private int mPrimaryTextColor = COLOR_INVALID;
2674 private int mSecondaryTextColor = COLOR_INVALID;
2675 private int mActionBarColor = COLOR_INVALID;
Selim Cinek5fb73f82017-04-20 16:55:38 -07002676 private int mBackgroundColor = COLOR_INVALID;
2677 private int mForegroundColor = COLOR_INVALID;
Selim Cinekac5f0272017-05-02 16:05:41 -07002678 private int mBackgroundColorHint = COLOR_INVALID;
Adrian Roos70d7aa32017-01-11 15:39:06 -08002679
2680 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002681 * Constructs a new Builder with the defaults:
Joe Onoratocb109a02011-01-18 17:57:41 -08002682 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002683 * @param context
2684 * A {@link Context} that will be used by the Builder to construct the
2685 * RemoteViews. The Context will not be held past the lifetime of this Builder
2686 * object.
Geoffrey Pitsch5caa2762017-01-12 09:35:54 -05002687 * @param channelId
2688 * The constructed Notification will be posted on this
2689 * {@link NotificationChannel}. To use a NotificationChannel, it must first be
2690 * created using {@link NotificationManager#createNotificationChannel}.
Joe Onoratocb109a02011-01-18 17:57:41 -08002691 */
Geoffrey Pitsch5caa2762017-01-12 09:35:54 -05002692 public Builder(Context context, String channelId) {
2693 this(context, (Notification) null);
2694 mN.mChannelId = channelId;
2695 }
2696
2697 /**
2698 * @deprecated use {@link Notification.Builder#Notification.Builder(Context, String)}
2699 * instead. All posted Notifications must specify a NotificationChannel Id.
2700 */
2701 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -08002702 public Builder(Context context) {
Geoffrey Pitsch5caa2762017-01-12 09:35:54 -05002703 this(context, (Notification) null);
Joe Onorato46439ce2010-11-19 13:56:21 -08002704 }
2705
Joe Onoratocb109a02011-01-18 17:57:41 -08002706 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002707 * @hide
Christoph Studer4600f9b2014-07-22 22:44:43 +02002708 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002709 public Builder(Context context, Notification toAdopt) {
2710 mContext = context;
Christoph Studer4600f9b2014-07-22 22:44:43 +02002711
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002712 if (toAdopt == null) {
2713 mN = new Notification();
Selim Cinek0ff1ce602016-04-05 18:27:16 -07002714 if (context.getApplicationInfo().targetSdkVersion < Build.VERSION_CODES.N) {
2715 mN.extras.putBoolean(EXTRA_SHOW_WHEN, true);
2716 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002717 mN.priority = PRIORITY_DEFAULT;
2718 mN.visibility = VISIBILITY_PRIVATE;
2719 } else {
2720 mN = toAdopt;
2721 if (mN.actions != null) {
2722 Collections.addAll(mActions, mN.actions);
Christoph Studer4600f9b2014-07-22 22:44:43 +02002723 }
2724
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002725 if (mN.extras.containsKey(EXTRA_PEOPLE)) {
2726 Collections.addAll(mPersonList, mN.extras.getStringArray(EXTRA_PEOPLE));
2727 }
2728
Selim Cinek4ac6f602016-06-13 15:47:03 -07002729 if (mN.getSmallIcon() == null && mN.icon != 0) {
2730 setSmallIcon(mN.icon);
2731 }
2732
2733 if (mN.getLargeIcon() == null && mN.largeIcon != null) {
2734 setLargeIcon(mN.largeIcon);
2735 }
2736
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002737 String templateClass = mN.extras.getString(EXTRA_TEMPLATE);
2738 if (!TextUtils.isEmpty(templateClass)) {
2739 final Class<? extends Style> styleClass
2740 = getNotificationStyleClass(templateClass);
2741 if (styleClass == null) {
2742 Log.d(TAG, "Unknown style class: " + templateClass);
2743 } else {
2744 try {
Adrian Roosc1a80b02016-04-05 14:54:55 -07002745 final Constructor<? extends Style> ctor =
2746 styleClass.getDeclaredConstructor();
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002747 ctor.setAccessible(true);
2748 final Style style = ctor.newInstance();
2749 style.restoreFromExtras(mN.extras);
2750
2751 if (style != null) {
2752 setStyle(style);
2753 }
2754 } catch (Throwable t) {
2755 Log.e(TAG, "Could not create Style", t);
2756 }
2757 }
2758 }
2759
2760 }
2761 }
2762
2763 private NotificationColorUtil getColorUtil() {
Selim Cinek99104832017-01-25 14:47:33 -08002764 if (mColorUtil == null) {
2765 mColorUtil = NotificationColorUtil.getInstance(mContext);
Christoph Studer4600f9b2014-07-22 22:44:43 +02002766 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002767 return mColorUtil;
Christoph Studer4600f9b2014-07-22 22:44:43 +02002768 }
2769
2770 /**
Julia Reynolds13d898c2017-02-02 12:22:05 -05002771 * If this notification is duplicative of a Launcher shortcut, sets the
2772 * {@link ShortcutInfo#getId() id} of the shortcut, in case the Launcher wants to hide
2773 * the shortcut.
2774 *
Julia Reynoldsbad42972017-04-25 13:52:49 -04002775 * This field will be ignored by Launchers that don't support badging, don't show
2776 * notification content, or don't show {@link android.content.pm.ShortcutManager shortcuts}.
Julia Reynolds13d898c2017-02-02 12:22:05 -05002777 *
2778 * @param shortcutId the {@link ShortcutInfo#getId() id} of the shortcut this notification
2779 * supersedes
2780 */
2781 public Builder setShortcutId(String shortcutId) {
2782 mN.mShortcutId = shortcutId;
2783 return this;
2784 }
2785
2786 /**
Julia Reynoldse071abd2017-03-22 10:52:11 -04002787 * Sets which icon to display as a badge for this notification.
2788 *
2789 * Must be one of {@link #BADGE_ICON_NONE}, {@link #BADGE_ICON_SMALL},
2790 * {@link #BADGE_ICON_LARGE}.
2791 *
2792 * Note: This value might be ignored, for launchers that don't support badge icons.
2793 */
Julia Reynolds612beb22017-03-30 10:48:30 -04002794 public Builder setBadgeIconType(int icon) {
Julia Reynoldse071abd2017-03-22 10:52:11 -04002795 mN.mBadgeIcon = icon;
2796 return this;
2797 }
2798
2799 /**
Julia Reynoldsa79c3712017-04-21 10:29:57 -04002800 * Sets the group alert behavior for this notification. Use this method to mute this
2801 * notification if alerts for this notification's group should be handled by a different
2802 * notification. This is only applicable for notifications that belong to a
2803 * {@link #setGroup(String) group}.
2804 *
2805 * <p> The default value is {@link #GROUP_ALERT_ALL}.</p>
2806 */
2807 public Builder setGroupAlertBehavior(@GroupAlertBehavior int groupAlertBehavior) {
2808 mN.mGroupAlertBehavior = groupAlertBehavior;
2809 return this;
2810 }
2811
Jeff Sharkey000ce802017-04-29 13:13:27 -06002812 /** @removed */
2813 @Deprecated
Julia Reynoldsb5e44b72016-08-16 15:00:25 -04002814 public Builder setChannel(String channelId) {
2815 mN.mChannelId = channelId;
2816 return this;
2817 }
2818
2819 /**
Julia Reynoldsbad42972017-04-25 13:52:49 -04002820 * Specifies the channel the notification should be delivered on.
2821 */
2822 public Builder setChannelId(String channelId) {
2823 mN.mChannelId = channelId;
2824 return this;
2825 }
2826
Jeff Sharkey000ce802017-04-29 13:13:27 -06002827 /** @removed */
2828 @Deprecated
Julia Reynolds50989772017-02-23 14:32:16 -05002829 public Builder setTimeout(long durationMs) {
2830 mN.mTimeout = durationMs;
Julia Reynolds2a128742016-11-28 14:29:25 -05002831 return this;
2832 }
2833
2834 /**
Julia Reynoldsbad42972017-04-25 13:52:49 -04002835 * Specifies a duration in milliseconds after which this notification should be canceled,
2836 * if it is not already canceled.
2837 */
2838 public Builder setTimeoutAfter(long durationMs) {
2839 mN.mTimeout = durationMs;
2840 return this;
2841 }
2842
2843 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002844 * Add a timestamp pertaining to the notification (usually the time the event occurred).
Selim Cinek0ff1ce602016-04-05 18:27:16 -07002845 *
2846 * For apps targeting {@link android.os.Build.VERSION_CODES#N} and above, this time is not
2847 * shown anymore by default and must be opted into by using
2848 * {@link android.app.Notification.Builder#setShowWhen(boolean)}
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002849 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002850 * @see Notification#when
Joe Onoratocb109a02011-01-18 17:57:41 -08002851 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002852 public Builder setWhen(long when) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002853 mN.when = when;
Joe Onorato46439ce2010-11-19 13:56:21 -08002854 return this;
2855 }
2856
Joe Onoratocb109a02011-01-18 17:57:41 -08002857 /**
Griff Hazen50c11652014-05-16 09:46:31 -07002858 * Control whether the timestamp set with {@link #setWhen(long) setWhen} is shown
Daniel Sandler0c890492012-09-12 17:23:10 -07002859 * in the content view.
Selim Cinek0ff1ce602016-04-05 18:27:16 -07002860 * For apps targeting {@link android.os.Build.VERSION_CODES#N} and above, this defaults to
2861 * {@code false}. For earlier apps, the default is {@code true}.
Daniel Sandler0c890492012-09-12 17:23:10 -07002862 */
2863 public Builder setShowWhen(boolean show) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002864 mN.extras.putBoolean(EXTRA_SHOW_WHEN, show);
Daniel Sandler0c890492012-09-12 17:23:10 -07002865 return this;
2866 }
2867
2868 /**
Daniel Sandlerd33b8032012-05-10 11:41:48 -04002869 * Show the {@link Notification#when} field as a stopwatch.
Joe Malin8d40d042012-11-05 11:36:40 -08002870 *
2871 * Instead of presenting <code>when</code> as a timestamp, the notification will show an
Daniel Sandlerd33b8032012-05-10 11:41:48 -04002872 * automatically updating display of the minutes and seconds since <code>when</code>.
Daniel Sandlera2985ed2012-04-03 16:42:00 -04002873 *
Daniel Sandlerd33b8032012-05-10 11:41:48 -04002874 * Useful when showing an elapsed time (like an ongoing phone call).
2875 *
Selim Cinek81c23aa2016-02-25 16:23:13 -08002876 * The counter can also be set to count down to <code>when</code> when using
Adrian Roos96b7e202016-05-17 13:50:38 -07002877 * {@link #setChronometerCountDown(boolean)}.
Selim Cinek81c23aa2016-02-25 16:23:13 -08002878 *
Daniel Sandlerd33b8032012-05-10 11:41:48 -04002879 * @see android.widget.Chronometer
Daniel Sandlera2985ed2012-04-03 16:42:00 -04002880 * @see Notification#when
Adrian Roos96b7e202016-05-17 13:50:38 -07002881 * @see #setChronometerCountDown(boolean)
Daniel Sandlera2985ed2012-04-03 16:42:00 -04002882 */
2883 public Builder setUsesChronometer(boolean b) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002884 mN.extras.putBoolean(EXTRA_SHOW_CHRONOMETER, b);
Daniel Sandlera2985ed2012-04-03 16:42:00 -04002885 return this;
2886 }
2887
2888 /**
Selim Cinek81c23aa2016-02-25 16:23:13 -08002889 * Sets the Chronometer to count down instead of counting up.
2890 *
2891 * <p>This is only relevant if {@link #setUsesChronometer(boolean)} has been set to true.
2892 * If it isn't set the chronometer will count up.
2893 *
2894 * @see #setUsesChronometer(boolean)
2895 */
Adrian Roos96b7e202016-05-17 13:50:38 -07002896 public Builder setChronometerCountDown(boolean countDown) {
2897 mN.extras.putBoolean(EXTRA_CHRONOMETER_COUNT_DOWN, countDown);
Selim Cinek81c23aa2016-02-25 16:23:13 -08002898 return this;
2899 }
2900
2901 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002902 * Set the small icon resource, which will be used to represent the notification in the
2903 * status bar.
Joe Onoratocb109a02011-01-18 17:57:41 -08002904 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002905
2906 * The platform template for the expanded view will draw this icon in the left, unless a
2907 * {@link #setLargeIcon(Bitmap) large icon} has also been specified, in which case the small
2908 * icon will be moved to the right-hand side.
2909 *
2910
2911 * @param icon
2912 * A resource ID in the application's package of the drawable to use.
2913 * @see Notification#icon
Joe Onoratocb109a02011-01-18 17:57:41 -08002914 */
Tor Norbye7b9c9122013-05-30 16:48:33 -07002915 public Builder setSmallIcon(@DrawableRes int icon) {
Dan Sandlerd63f9322015-05-06 15:18:49 -04002916 return setSmallIcon(icon != 0
2917 ? Icon.createWithResource(mContext, icon)
2918 : null);
Joe Onorato46439ce2010-11-19 13:56:21 -08002919 }
2920
Joe Onoratocb109a02011-01-18 17:57:41 -08002921 /**
2922 * A variant of {@link #setSmallIcon(int) setSmallIcon(int)} that takes an additional
2923 * level parameter for when the icon is a {@link android.graphics.drawable.LevelListDrawable
2924 * LevelListDrawable}.
2925 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002926 * @param icon A resource ID in the application's package of the drawable to use.
Joe Onoratocb109a02011-01-18 17:57:41 -08002927 * @param level The level to use for the icon.
2928 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002929 * @see Notification#icon
2930 * @see Notification#iconLevel
Joe Onoratocb109a02011-01-18 17:57:41 -08002931 */
Tor Norbye7b9c9122013-05-30 16:48:33 -07002932 public Builder setSmallIcon(@DrawableRes int icon, int level) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002933 mN.iconLevel = level;
Dan Sandlerd63f9322015-05-06 15:18:49 -04002934 return setSmallIcon(icon);
2935 }
2936
2937 /**
2938 * Set the small icon, which will be used to represent the notification in the
2939 * status bar and content view (unless overriden there by a
2940 * {@link #setLargeIcon(Bitmap) large icon}).
2941 *
2942 * @param icon An Icon object to use.
2943 * @see Notification#icon
2944 */
2945 public Builder setSmallIcon(Icon icon) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002946 mN.setSmallIcon(icon);
2947 if (icon != null && icon.getType() == Icon.TYPE_RESOURCE) {
2948 mN.icon = icon.getResId();
2949 }
Joe Onorato46439ce2010-11-19 13:56:21 -08002950 return this;
2951 }
2952
Joe Onoratocb109a02011-01-18 17:57:41 -08002953 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002954 * Set the first line of text in the platform notification template.
Joe Onoratocb109a02011-01-18 17:57:41 -08002955 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002956 public Builder setContentTitle(CharSequence title) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002957 mN.extras.putCharSequence(EXTRA_TITLE, safeCharSequence(title));
Joe Onorato46439ce2010-11-19 13:56:21 -08002958 return this;
2959 }
2960
Joe Onoratocb109a02011-01-18 17:57:41 -08002961 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002962 * Set the second line of text in the platform notification template.
Joe Onoratocb109a02011-01-18 17:57:41 -08002963 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002964 public Builder setContentText(CharSequence text) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002965 mN.extras.putCharSequence(EXTRA_TEXT, safeCharSequence(text));
Joe Onorato46439ce2010-11-19 13:56:21 -08002966 return this;
2967 }
2968
Joe Onoratocb109a02011-01-18 17:57:41 -08002969 /**
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07002970 * This provides some additional information that is displayed in the notification. No
2971 * guarantees are given where exactly it is displayed.
2972 *
2973 * <p>This information should only be provided if it provides an essential
2974 * benefit to the understanding of the notification. The more text you provide the
2975 * less readable it becomes. For example, an email client should only provide the account
2976 * name here if more than one email account has been added.</p>
2977 *
2978 * <p>As of {@link android.os.Build.VERSION_CODES#N} this information is displayed in the
2979 * notification header area.
2980 *
2981 * On Android versions before {@link android.os.Build.VERSION_CODES#N}
2982 * this will be shown in the third line of text in the platform notification template.
2983 * You should not be using {@link #setProgress(int, int, boolean)} at the
2984 * same time on those versions; they occupy the same place.
2985 * </p>
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002986 */
2987 public Builder setSubText(CharSequence text) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002988 mN.extras.putCharSequence(EXTRA_SUB_TEXT, safeCharSequence(text));
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002989 return this;
2990 }
2991
2992 /**
Julia Reynolds3aedded2017-03-31 14:42:09 -04002993 * Provides text that will appear as a link to your application's settings.
2994 *
2995 * <p>This text does not appear within notification {@link Style templates} but may
2996 * appear when the user uses an affordance to learn more about the notification.
2997 * Additionally, this text will not appear unless you provide a valid link target by
2998 * handling {@link #INTENT_CATEGORY_NOTIFICATION_PREFERENCES}.
2999 *
3000 * <p>This text is meant to be concise description about what the user can customize
3001 * when they click on this link. The recommended maximum length is 40 characters.
3002 * @param text
3003 * @return
3004 */
3005 public Builder setSettingsText(CharSequence text) {
3006 mN.mSettingsText = safeCharSequence(text);
3007 return this;
3008 }
3009
3010 /**
Adrian Roose458aa82015-12-08 16:17:19 -08003011 * Set the remote input history.
3012 *
3013 * This should be set to the most recent inputs that have been sent
3014 * through a {@link RemoteInput} of this Notification and cleared once the it is no
3015 * longer relevant (e.g. for chat notifications once the other party has responded).
3016 *
3017 * The most recent input must be stored at the 0 index, the second most recent at the
3018 * 1 index, etc. Note that the system will limit both how far back the inputs will be shown
3019 * and how much of each individual input is shown.
3020 *
3021 * <p>Note: The reply text will only be shown on notifications that have least one action
3022 * with a {@code RemoteInput}.</p>
3023 */
3024 public Builder setRemoteInputHistory(CharSequence[] text) {
3025 if (text == null) {
3026 mN.extras.putCharSequenceArray(EXTRA_REMOTE_INPUT_HISTORY, null);
3027 } else {
3028 final int N = Math.min(MAX_REPLY_HISTORY, text.length);
3029 CharSequence[] safe = new CharSequence[N];
3030 for (int i = 0; i < N; i++) {
3031 safe[i] = safeCharSequence(text[i]);
3032 }
3033 mN.extras.putCharSequenceArray(EXTRA_REMOTE_INPUT_HISTORY, safe);
3034 }
3035 return this;
3036 }
3037
3038 /**
Julia Reynolds13d898c2017-02-02 12:22:05 -05003039 * Sets the number of items this notification represents. May be displayed as a badge count
3040 * for Launchers that support badging.
Joe Onoratocb109a02011-01-18 17:57:41 -08003041 */
Joe Onorato8595a3d2010-11-19 18:12:07 -08003042 public Builder setNumber(int number) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003043 mN.number = number;
Joe Onorato8595a3d2010-11-19 18:12:07 -08003044 return this;
3045 }
3046
Joe Onoratocb109a02011-01-18 17:57:41 -08003047 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003048 * A small piece of additional information pertaining to this notification.
3049 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003050 * The platform template will draw this on the last line of the notification, at the far
3051 * right (to the right of a smallIcon if it has been placed there).
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07003052 *
3053 * @deprecated use {@link #setSubText(CharSequence)} instead to set a text in the header.
3054 * For legacy apps targeting a version below {@link android.os.Build.VERSION_CODES#N} this
3055 * field will still show up, but the subtext will take precedence.
Joe Onoratocb109a02011-01-18 17:57:41 -08003056 */
Aurimas Liutikas514c5ef2016-05-24 15:22:55 -07003057 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -08003058 public Builder setContentInfo(CharSequence info) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003059 mN.extras.putCharSequence(EXTRA_INFO_TEXT, safeCharSequence(info));
Joe Onorato46439ce2010-11-19 13:56:21 -08003060 return this;
3061 }
3062
Joe Onoratocb109a02011-01-18 17:57:41 -08003063 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003064 * Set the progress this notification represents.
3065 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003066 * The platform template will represent this using a {@link ProgressBar}.
Jeff Sharkey1c400132011-08-05 14:50:13 -07003067 */
3068 public Builder setProgress(int max, int progress, boolean indeterminate) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003069 mN.extras.putInt(EXTRA_PROGRESS, progress);
3070 mN.extras.putInt(EXTRA_PROGRESS_MAX, max);
3071 mN.extras.putBoolean(EXTRA_PROGRESS_INDETERMINATE, indeterminate);
Jeff Sharkey1c400132011-08-05 14:50:13 -07003072 return this;
3073 }
3074
3075 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003076 * Supply a custom RemoteViews to use instead of the platform template.
3077 *
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003078 * Use {@link #setCustomContentView(RemoteViews)} instead.
Joe Onoratocb109a02011-01-18 17:57:41 -08003079 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003080 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -08003081 public Builder setContent(RemoteViews views) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003082 return setCustomContentView(views);
3083 }
3084
3085 /**
3086 * Supply custom RemoteViews to use instead of the platform template.
3087 *
3088 * This will override the layout that would otherwise be constructed by this Builder
3089 * object.
3090 */
3091 public Builder setCustomContentView(RemoteViews contentView) {
3092 mN.contentView = contentView;
3093 return this;
3094 }
3095
3096 /**
3097 * Supply custom RemoteViews to use instead of the platform template in the expanded form.
3098 *
3099 * This will override the expanded layout that would otherwise be constructed by this
3100 * Builder object.
3101 */
3102 public Builder setCustomBigContentView(RemoteViews contentView) {
3103 mN.bigContentView = contentView;
3104 return this;
3105 }
3106
3107 /**
3108 * Supply custom RemoteViews to use instead of the platform template in the heads up dialog.
3109 *
3110 * This will override the heads-up layout that would otherwise be constructed by this
3111 * Builder object.
3112 */
3113 public Builder setCustomHeadsUpContentView(RemoteViews contentView) {
3114 mN.headsUpContentView = contentView;
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 be sent when the notification is clicked.
3120 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003121 * As of {@link android.os.Build.VERSION_CODES#HONEYCOMB}, if this field is unset and you
3122 * have specified a custom RemoteViews with {@link #setContent(RemoteViews)}, you can use
3123 * {@link RemoteViews#setOnClickPendingIntent RemoteViews.setOnClickPendingIntent(int,PendingIntent)}
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003124 * to assign PendingIntents to individual views in that custom layout (i.e., to create
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003125 * clickable buttons inside the notification view).
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003126 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003127 * @see Notification#contentIntent Notification.contentIntent
Joe Onoratocb109a02011-01-18 17:57:41 -08003128 */
Joe Onorato46439ce2010-11-19 13:56:21 -08003129 public Builder setContentIntent(PendingIntent intent) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003130 mN.contentIntent = intent;
Joe Onorato46439ce2010-11-19 13:56:21 -08003131 return this;
3132 }
3133
Joe Onoratocb109a02011-01-18 17:57:41 -08003134 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003135 * Supply a {@link PendingIntent} to send when the notification is cleared explicitly by the user.
3136 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003137 * @see Notification#deleteIntent
Joe Onoratocb109a02011-01-18 17:57:41 -08003138 */
Joe Onorato46439ce2010-11-19 13:56:21 -08003139 public Builder setDeleteIntent(PendingIntent intent) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003140 mN.deleteIntent = intent;
Joe Onorato46439ce2010-11-19 13:56:21 -08003141 return this;
3142 }
3143
Joe Onoratocb109a02011-01-18 17:57:41 -08003144 /**
3145 * An intent to launch instead of posting the notification to the status bar.
3146 * Only for use with extremely high-priority notifications demanding the user's
3147 * <strong>immediate</strong> attention, such as an incoming phone call or
3148 * alarm clock that the user has explicitly set to a particular time.
3149 * If this facility is used for something else, please give the user an option
3150 * to turn it off and use a normal notification, as this can be extremely
3151 * disruptive.
3152 *
Chris Wren47c20a12014-06-18 17:27:29 -04003153 * <p>
3154 * The system UI may choose to display a heads-up notification, instead of
3155 * launching this intent, while the user is using the device.
3156 * </p>
3157 *
Joe Onoratocb109a02011-01-18 17:57:41 -08003158 * @param intent The pending intent to launch.
3159 * @param highPriority Passing true will cause this notification to be sent
3160 * even if other notifications are suppressed.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003161 *
3162 * @see Notification#fullScreenIntent
Joe Onoratocb109a02011-01-18 17:57:41 -08003163 */
Joe Onorato46439ce2010-11-19 13:56:21 -08003164 public Builder setFullScreenIntent(PendingIntent intent, boolean highPriority) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003165 mN.fullScreenIntent = intent;
Joe Onorato46439ce2010-11-19 13:56:21 -08003166 setFlag(FLAG_HIGH_PRIORITY, highPriority);
3167 return this;
3168 }
3169
Joe Onoratocb109a02011-01-18 17:57:41 -08003170 /**
Dan Sandler5fcdf6e2014-07-18 11:31:15 -04003171 * Set the "ticker" text which is sent to accessibility services.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003172 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003173 * @see Notification#tickerText
Joe Onoratocb109a02011-01-18 17:57:41 -08003174 */
Joe Onorato46439ce2010-11-19 13:56:21 -08003175 public Builder setTicker(CharSequence tickerText) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003176 mN.tickerText = safeCharSequence(tickerText);
Joe Onorato46439ce2010-11-19 13:56:21 -08003177 return this;
3178 }
3179
Joe Onoratocb109a02011-01-18 17:57:41 -08003180 /**
Dan Sandler5fcdf6e2014-07-18 11:31:15 -04003181 * Obsolete version of {@link #setTicker(CharSequence)}.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003182 *
Joe Onoratocb109a02011-01-18 17:57:41 -08003183 */
Dan Sandler5fcdf6e2014-07-18 11:31:15 -04003184 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -08003185 public Builder setTicker(CharSequence tickerText, RemoteViews views) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003186 setTicker(tickerText);
3187 // views is ignored
Joe Onorato46439ce2010-11-19 13:56:21 -08003188 return this;
3189 }
3190
Joe Onoratocb109a02011-01-18 17:57:41 -08003191 /**
Dan Sandlerd63f9322015-05-06 15:18:49 -04003192 * Add a large icon to the notification content view.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003193 *
3194 * In the platform template, this image will be shown on the left of the notification view
Dan Sandlerd63f9322015-05-06 15:18:49 -04003195 * in place of the {@link #setSmallIcon(Icon) small icon} (which will be placed in a small
3196 * badge atop the large icon).
Dan Sandler08a04c12015-05-06 15:18:49 -04003197 */
Dan Sandlerd63f9322015-05-06 15:18:49 -04003198 public Builder setLargeIcon(Bitmap b) {
3199 return setLargeIcon(b != null ? Icon.createWithBitmap(b) : null);
3200 }
3201
3202 /**
3203 * Add a large icon to the notification content view.
3204 *
3205 * In the platform template, this image will be shown on the left of the notification view
3206 * in place of the {@link #setSmallIcon(Icon) small icon} (which will be placed in a small
3207 * badge atop the large icon).
3208 */
3209 public Builder setLargeIcon(Icon icon) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003210 mN.mLargeIcon = icon;
3211 mN.extras.putParcelable(EXTRA_LARGE_ICON, icon);
Joe Onorato46439ce2010-11-19 13:56:21 -08003212 return this;
3213 }
3214
Joe Onoratocb109a02011-01-18 17:57:41 -08003215 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003216 * Set the sound to play.
3217 *
John Spurlockc0650f022014-07-19 13:22:39 -04003218 * It will be played using the {@link #AUDIO_ATTRIBUTES_DEFAULT default audio attributes}
3219 * for notifications.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003220 *
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05003221 * @deprecated use {@link NotificationChannel#setSound(Uri, AudioAttributes)} instead.
Joe Onoratocb109a02011-01-18 17:57:41 -08003222 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05003223 @Deprecated
Joe Onorato52f80cd2010-11-21 15:34:48 -08003224 public Builder setSound(Uri sound) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003225 mN.sound = sound;
3226 mN.audioAttributes = AUDIO_ATTRIBUTES_DEFAULT;
Joe Onorato52f80cd2010-11-21 15:34:48 -08003227 return this;
3228 }
3229
Joe Onoratocb109a02011-01-18 17:57:41 -08003230 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003231 * Set the sound to play, along with a specific stream on which to play it.
Joe Onoratocb109a02011-01-18 17:57:41 -08003232 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003233 * See {@link android.media.AudioManager} for the <code>STREAM_</code> constants.
3234 *
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05003235 * @deprecated use {@link NotificationChannel#setSound(Uri, AudioAttributes)}.
Joe Onoratocb109a02011-01-18 17:57:41 -08003236 */
Jean-Michel Trivi81f871e2014-08-06 16:32:38 -07003237 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -08003238 public Builder setSound(Uri sound, int streamType) {
Jean-Michel Trivi2f7511f2016-11-28 15:40:27 -08003239 PlayerBase.deprecateStreamTypeForPlayback(streamType, "Notification", "setSound()");
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003240 mN.sound = sound;
3241 mN.audioStreamType = streamType;
Joe Onorato46439ce2010-11-19 13:56:21 -08003242 return this;
3243 }
3244
Joe Onoratocb109a02011-01-18 17:57:41 -08003245 /**
John Spurlockc0650f022014-07-19 13:22:39 -04003246 * Set the sound to play, along with specific {@link AudioAttributes audio attributes} to
3247 * use during playback.
3248 *
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05003249 * @deprecated use {@link NotificationChannel#setSound(Uri, AudioAttributes)} instead.
John Spurlockc0650f022014-07-19 13:22:39 -04003250 * @see Notification#sound
3251 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05003252 @Deprecated
John Spurlockc0650f022014-07-19 13:22:39 -04003253 public Builder setSound(Uri sound, AudioAttributes audioAttributes) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003254 mN.sound = sound;
3255 mN.audioAttributes = audioAttributes;
John Spurlockc0650f022014-07-19 13:22:39 -04003256 return this;
3257 }
3258
3259 /**
Joe Onoratocb109a02011-01-18 17:57:41 -08003260 * Set the vibration pattern to use.
3261 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003262 * See {@link android.os.Vibrator#vibrate(long[], int)} for a discussion of the
3263 * <code>pattern</code> parameter.
3264 *
Chris Wren47c20a12014-06-18 17:27:29 -04003265 * <p>
3266 * A notification that vibrates is more likely to be presented as a heads-up notification.
3267 * </p>
3268 *
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05003269 * @deprecated use {@link NotificationChannel#setVibrationPattern(long[])} instead.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003270 * @see Notification#vibrate
Joe Onoratocb109a02011-01-18 17:57:41 -08003271 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05003272 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -08003273 public Builder setVibrate(long[] pattern) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003274 mN.vibrate = pattern;
Joe Onorato46439ce2010-11-19 13:56:21 -08003275 return this;
3276 }
3277
Joe Onoratocb109a02011-01-18 17:57:41 -08003278 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003279 * Set the desired color for the indicator LED on the device, as well as the
3280 * blink duty cycle (specified in milliseconds).
3281 *
3282
3283 * Not all devices will honor all (or even any) of these values.
3284 *
Julia Reynolds529e3322017-02-06 08:33:01 -05003285 * @deprecated use {@link NotificationChannel#enableLights(boolean)} instead.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003286 * @see Notification#ledARGB
3287 * @see Notification#ledOnMS
3288 * @see Notification#ledOffMS
Joe Onoratocb109a02011-01-18 17:57:41 -08003289 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05003290 @Deprecated
Tor Norbye80756e32015-03-02 09:39:27 -08003291 public Builder setLights(@ColorInt int argb, int onMs, int offMs) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003292 mN.ledARGB = argb;
3293 mN.ledOnMS = onMs;
3294 mN.ledOffMS = offMs;
Julia Reynolds10ee1fc2015-11-09 11:04:55 -05003295 if (onMs != 0 || offMs != 0) {
3296 mN.flags |= FLAG_SHOW_LIGHTS;
3297 }
Joe Onorato46439ce2010-11-19 13:56:21 -08003298 return this;
3299 }
3300
Joe Onoratocb109a02011-01-18 17:57:41 -08003301 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003302 * Set whether this is an "ongoing" notification.
Joe Onoratocb109a02011-01-18 17:57:41 -08003303 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003304
3305 * Ongoing notifications cannot be dismissed by the user, so your application or service
3306 * must take care of canceling them.
3307 *
3308
3309 * They are typically used to indicate a background task that the user is actively engaged
3310 * with (e.g., playing music) or is pending in some way and therefore occupying the device
3311 * (e.g., a file download, sync operation, active network connection).
3312 *
3313
3314 * @see Notification#FLAG_ONGOING_EVENT
3315 * @see Service#setForeground(boolean)
Joe Onoratocb109a02011-01-18 17:57:41 -08003316 */
Joe Onorato46439ce2010-11-19 13:56:21 -08003317 public Builder setOngoing(boolean ongoing) {
3318 setFlag(FLAG_ONGOING_EVENT, ongoing);
3319 return this;
3320 }
3321
Joe Onoratocb109a02011-01-18 17:57:41 -08003322 /**
Selim Cinek7b9605b2017-01-19 17:36:00 -08003323 * Set whether this notification should be colorized. When set, the color set with
3324 * {@link #setColor(int)} will be used as the background color of this notification.
3325 * <p>
Selim Cinek7b9605b2017-01-19 17:36:00 -08003326 * This should only be used for high priority ongoing tasks like navigation, an ongoing
3327 * call, or other similarly high-priority events for the user.
Selim Cinek99104832017-01-25 14:47:33 -08003328 * <p>
Selim Cinek22714f12017-04-13 16:23:53 -07003329 * For most styles, the coloring will only be applied if the notification is for a
3330 * foreground service notification.
Selim Cinek99104832017-01-25 14:47:33 -08003331 * However, for {@link MediaStyle} and {@link DecoratedMediaCustomViewStyle} notifications
Selim Cinek22714f12017-04-13 16:23:53 -07003332 * that have a media session attached there is no such requirement.
Selim Cinek7b9605b2017-01-19 17:36:00 -08003333 *
Selim Cinek7b9605b2017-01-19 17:36:00 -08003334 * @see Builder#setColor(int)
Selim Cinek99104832017-01-25 14:47:33 -08003335 * @see MediaStyle#setMediaSession(MediaSession.Token)
Selim Cinek7b9605b2017-01-19 17:36:00 -08003336 */
3337 public Builder setColorized(boolean colorize) {
3338 mN.extras.putBoolean(EXTRA_COLORIZED, colorize);
3339 return this;
3340 }
3341
3342 /**
Joe Onoratocb109a02011-01-18 17:57:41 -08003343 * Set this flag if you would only like the sound, vibrate
3344 * and ticker to be played if the notification is not already showing.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003345 *
3346 * @see Notification#FLAG_ONLY_ALERT_ONCE
Joe Onoratocb109a02011-01-18 17:57:41 -08003347 */
Joe Onorato46439ce2010-11-19 13:56:21 -08003348 public Builder setOnlyAlertOnce(boolean onlyAlertOnce) {
3349 setFlag(FLAG_ONLY_ALERT_ONCE, onlyAlertOnce);
3350 return this;
3351 }
3352
Joe Onoratocb109a02011-01-18 17:57:41 -08003353 /**
Julia Reynolds04499532016-09-13 14:04:53 -04003354 * Make this notification automatically dismissed when the user touches it.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003355 *
3356 * @see Notification#FLAG_AUTO_CANCEL
Joe Onoratocb109a02011-01-18 17:57:41 -08003357 */
Joe Onorato46439ce2010-11-19 13:56:21 -08003358 public Builder setAutoCancel(boolean autoCancel) {
Joe Onorato281d83f2011-01-04 17:13:10 -08003359 setFlag(FLAG_AUTO_CANCEL, autoCancel);
Joe Onorato46439ce2010-11-19 13:56:21 -08003360 return this;
3361 }
3362
Joe Onoratocb109a02011-01-18 17:57:41 -08003363 /**
Griff Hazendfcb0802014-02-11 12:00:00 -08003364 * Set whether or not this notification should not bridge to other devices.
3365 *
3366 * <p>Some notifications can be bridged to other devices for remote display.
3367 * This hint can be set to recommend this notification not be bridged.
3368 */
3369 public Builder setLocalOnly(boolean localOnly) {
3370 setFlag(FLAG_LOCAL_ONLY, localOnly);
3371 return this;
3372 }
3373
3374 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003375 * Set which notification properties will be inherited from system defaults.
Joe Onoratocb109a02011-01-18 17:57:41 -08003376 * <p>
3377 * The value should be one or more of the following fields combined with
3378 * bitwise-or:
3379 * {@link #DEFAULT_SOUND}, {@link #DEFAULT_VIBRATE}, {@link #DEFAULT_LIGHTS}.
3380 * <p>
3381 * For all default values, use {@link #DEFAULT_ALL}.
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05003382 *
3383 * @deprecated use {@link NotificationChannel#enableVibration(boolean)} and
Julia Reynolds529e3322017-02-06 08:33:01 -05003384 * {@link NotificationChannel#enableLights(boolean)} and
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05003385 * {@link NotificationChannel#setSound(Uri, AudioAttributes)} instead.
Joe Onoratocb109a02011-01-18 17:57:41 -08003386 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05003387 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -08003388 public Builder setDefaults(int defaults) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003389 mN.defaults = defaults;
Joe Onorato46439ce2010-11-19 13:56:21 -08003390 return this;
3391 }
3392
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003393 /**
3394 * Set the priority of this notification.
3395 *
3396 * @see Notification#priority
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05003397 * @deprecated use {@link NotificationChannel#setImportance(int)} instead.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003398 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05003399 @Deprecated
Tor Norbyed9273d62013-05-30 15:59:53 -07003400 public Builder setPriority(@Priority int pri) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003401 mN.priority = pri;
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003402 return this;
3403 }
Joe Malin8d40d042012-11-05 11:36:40 -08003404
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003405 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -04003406 * Set the notification category.
Joe Malin8d40d042012-11-05 11:36:40 -08003407 *
John Spurlockfd7f1e02014-03-18 16:41:57 -04003408 * @see Notification#category
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003409 */
John Spurlockfd7f1e02014-03-18 16:41:57 -04003410 public Builder setCategory(String category) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003411 mN.category = category;
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003412 return this;
3413 }
3414
3415 /**
Chris Wrendde75302014-03-26 17:24:15 -04003416 * Add a person that is relevant to this notification.
3417 *
Chris Wrene6c48932014-09-29 17:19:27 -04003418 * <P>
3419 * Depending on user preferences, this annotation may allow the notification to pass
Julia Reynoldse071abd2017-03-22 10:52:11 -04003420 * through interruption filters, if this notification is of category {@link #CATEGORY_CALL}
3421 * or {@link #CATEGORY_MESSAGE}. The addition of people may also cause this notification to
3422 * appear more prominently in the user interface.
Chris Wrene6c48932014-09-29 17:19:27 -04003423 * </P>
3424 *
3425 * <P>
3426 * The person should be specified by the {@code String} representation of a
3427 * {@link android.provider.ContactsContract.Contacts#CONTENT_LOOKUP_URI}.
3428 * </P>
3429 *
3430 * <P>The system will also attempt to resolve {@code mailto:} and {@code tel:} schema
3431 * URIs. The path part of these URIs must exist in the contacts database, in the
3432 * appropriate column, or the reference will be discarded as invalid. Telephone schema
3433 * URIs will be resolved by {@link android.provider.ContactsContract.PhoneLookup}.
3434 * </P>
3435 *
3436 * @param uri A URI for the person.
Chris Wrendde75302014-03-26 17:24:15 -04003437 * @see Notification#EXTRA_PEOPLE
3438 */
Chris Wrene6c48932014-09-29 17:19:27 -04003439 public Builder addPerson(String uri) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003440 mPersonList.add(uri);
Chris Wrendde75302014-03-26 17:24:15 -04003441 return this;
3442 }
3443
3444 /**
Griff Hazen5cadc3b2014-05-20 09:55:39 -07003445 * Set this notification to be part of a group of notifications sharing the same key.
3446 * Grouped notifications may display in a cluster or stack on devices which
3447 * support such rendering.
3448 *
3449 * <p>To make this notification the summary for its group, also call
3450 * {@link #setGroupSummary}. A sort order can be specified for group members by using
3451 * {@link #setSortKey}.
3452 * @param groupKey The group key of the group.
3453 * @return this object for method chaining
3454 */
3455 public Builder setGroup(String groupKey) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003456 mN.mGroupKey = groupKey;
Griff Hazen5cadc3b2014-05-20 09:55:39 -07003457 return this;
3458 }
3459
3460 /**
3461 * Set this notification to be the group summary for a group of notifications.
3462 * Grouped notifications may display in a cluster or stack on devices which
Julia Reynolds04499532016-09-13 14:04:53 -04003463 * support such rendering. If thereRequires a group key also be set using {@link #setGroup}.
3464 * The group summary may be suppressed if too few notifications are included in the group.
Griff Hazen5cadc3b2014-05-20 09:55:39 -07003465 * @param isGroupSummary Whether this notification should be a group summary.
3466 * @return this object for method chaining
3467 */
3468 public Builder setGroupSummary(boolean isGroupSummary) {
3469 setFlag(FLAG_GROUP_SUMMARY, isGroupSummary);
3470 return this;
3471 }
3472
3473 /**
3474 * Set a sort key that orders this notification among other notifications from the
3475 * same package. This can be useful if an external sort was already applied and an app
3476 * would like to preserve this. Notifications will be sorted lexicographically using this
3477 * value, although providing different priorities in addition to providing sort key may
3478 * cause this value to be ignored.
3479 *
3480 * <p>This sort key can also be used to order members of a notification group. See
Griff Hazen9e1379f2014-05-20 12:50:51 -07003481 * {@link #setGroup}.
Griff Hazen5cadc3b2014-05-20 09:55:39 -07003482 *
3483 * @see String#compareTo(String)
3484 */
3485 public Builder setSortKey(String sortKey) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003486 mN.mSortKey = sortKey;
Griff Hazen5cadc3b2014-05-20 09:55:39 -07003487 return this;
3488 }
3489
3490 /**
Griff Hazen720042b2014-02-24 15:46:56 -08003491 * Merge additional metadata into this notification.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003492 *
Griff Hazen720042b2014-02-24 15:46:56 -08003493 * <p>Values within the Bundle will replace existing extras values in this Builder.
3494 *
3495 * @see Notification#extras
3496 */
Griff Hazen959591e2014-05-15 22:26:18 -07003497 public Builder addExtras(Bundle extras) {
3498 if (extras != null) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003499 mUserExtras.putAll(extras);
Griff Hazen720042b2014-02-24 15:46:56 -08003500 }
3501 return this;
3502 }
3503
3504 /**
3505 * Set metadata for this notification.
3506 *
3507 * <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 -04003508 * current contents are copied into the Notification each time {@link #build()} is
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003509 * called.
3510 *
Griff Hazen720042b2014-02-24 15:46:56 -08003511 * <p>Replaces any existing extras values with those from the provided Bundle.
3512 * Use {@link #addExtras} to merge in metadata instead.
3513 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003514 * @see Notification#extras
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003515 */
Griff Hazen959591e2014-05-15 22:26:18 -07003516 public Builder setExtras(Bundle extras) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003517 if (extras != null) {
3518 mUserExtras = extras;
3519 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003520 return this;
3521 }
3522
Daniel Sandlera0a938c2012-03-15 08:42:37 -04003523 /**
Griff Hazen720042b2014-02-24 15:46:56 -08003524 * Get the current metadata Bundle used by this notification Builder.
3525 *
3526 * <p>The returned Bundle is shared with this Builder.
3527 *
3528 * <p>The current contents of this Bundle are copied into the Notification each time
3529 * {@link #build()} is called.
3530 *
3531 * @see Notification#extras
3532 */
3533 public Bundle getExtras() {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003534 return mUserExtras;
3535 }
3536
3537 private Bundle getAllExtras() {
3538 final Bundle saveExtras = (Bundle) mUserExtras.clone();
3539 saveExtras.putAll(mN.extras);
3540 return saveExtras;
Griff Hazen720042b2014-02-24 15:46:56 -08003541 }
3542
3543 /**
Daniel Sandlera0a938c2012-03-15 08:42:37 -04003544 * Add an action to this notification. Actions are typically displayed by
3545 * the system as a button adjacent to the notification content.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04003546 * <p>
3547 * Every action must have an icon (32dp square and matching the
3548 * <a href="{@docRoot}design/style/iconography.html#action-bar">Holo
3549 * Dark action bar</a> visual style), a textual label, and a {@link PendingIntent}.
3550 * <p>
3551 * A notification in its expanded form can display up to 3 actions, from left to right in
3552 * the order they were added. Actions will not be displayed when the notification is
3553 * collapsed, however, so be sure that any essential functions may be accessed by the user
3554 * in some other way (for example, in the Activity pointed to by {@link #contentIntent}).
Daniel Sandlera0a938c2012-03-15 08:42:37 -04003555 *
3556 * @param icon Resource ID of a drawable that represents the action.
3557 * @param title Text describing the action.
3558 * @param intent PendingIntent to be fired when the action is invoked.
Dan Sandler86647982015-05-13 23:41:13 -04003559 *
3560 * @deprecated Use {@link #addAction(Action)} instead.
Daniel Sandlera0a938c2012-03-15 08:42:37 -04003561 */
Dan Sandler86647982015-05-13 23:41:13 -04003562 @Deprecated
Daniel Sandlera0a938c2012-03-15 08:42:37 -04003563 public Builder addAction(int icon, CharSequence title, PendingIntent intent) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04003564 mActions.add(new Action(icon, safeCharSequence(title), intent));
Daniel Sandlera0a938c2012-03-15 08:42:37 -04003565 return this;
3566 }
3567
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003568 /**
Griff Hazen959591e2014-05-15 22:26:18 -07003569 * Add an action to this notification. Actions are typically displayed by
3570 * the system as a button adjacent to the notification content.
3571 * <p>
3572 * Every action must have an icon (32dp square and matching the
3573 * <a href="{@docRoot}design/style/iconography.html#action-bar">Holo
3574 * Dark action bar</a> visual style), a textual label, and a {@link PendingIntent}.
3575 * <p>
3576 * A notification in its expanded form can display up to 3 actions, from left to right in
3577 * the order they were added. Actions will not be displayed when the notification is
3578 * collapsed, however, so be sure that any essential functions may be accessed by the user
3579 * in some other way (for example, in the Activity pointed to by {@link #contentIntent}).
3580 *
3581 * @param action The action to add.
3582 */
3583 public Builder addAction(Action action) {
liangweikang63b03b52017-03-16 19:22:15 +08003584 if (action != null) {
3585 mActions.add(action);
3586 }
Griff Hazen959591e2014-05-15 22:26:18 -07003587 return this;
3588 }
3589
3590 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003591 * Alter the complete list of actions attached to this notification.
3592 * @see #addAction(Action).
3593 *
3594 * @param actions
3595 * @return
3596 */
3597 public Builder setActions(Action... actions) {
3598 mActions.clear();
3599 for (int i = 0; i < actions.length; i++) {
liangweikang63b03b52017-03-16 19:22:15 +08003600 if (actions[i] != null) {
3601 mActions.add(actions[i]);
3602 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003603 }
3604 return this;
3605 }
3606
3607 /**
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003608 * Add a rich notification style to be applied at build time.
3609 *
3610 * @param style Object responsible for modifying the notification style.
3611 */
3612 public Builder setStyle(Style style) {
3613 if (mStyle != style) {
3614 mStyle = style;
Daniel Sandlerc08dea22012-06-28 08:35:24 -07003615 if (mStyle != null) {
3616 mStyle.setBuilder(this);
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003617 mN.extras.putString(EXTRA_TEMPLATE, style.getClass().getName());
3618 } else {
3619 mN.extras.remove(EXTRA_TEMPLATE);
Daniel Sandlerc08dea22012-06-28 08:35:24 -07003620 }
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003621 }
3622 return this;
3623 }
3624
Dan Sandler0bf2ed82013-12-21 23:33:41 -06003625 /**
3626 * Specify the value of {@link #visibility}.
Griff Hazenb720abe2014-05-20 13:15:30 -07003627 *
Dan Sandler0bf2ed82013-12-21 23:33:41 -06003628 * @return The same Builder.
3629 */
Jeff Sharkey30e06bb2017-04-24 11:18:03 -06003630 public Builder setVisibility(@Visibility int visibility) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003631 mN.visibility = visibility;
Dan Sandler0bf2ed82013-12-21 23:33:41 -06003632 return this;
3633 }
3634
3635 /**
3636 * Supply a replacement Notification whose contents should be shown in insecure contexts
3637 * (i.e. atop the secure lockscreen). See {@link #visibility} and {@link #VISIBILITY_PUBLIC}.
3638 * @param n A replacement notification, presumably with some or all info redacted.
3639 * @return The same Builder.
3640 */
3641 public Builder setPublicVersion(Notification n) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003642 if (n != null) {
3643 mN.publicVersion = new Notification();
3644 n.cloneInto(mN.publicVersion, /*heavy=*/ true);
3645 } else {
3646 mN.publicVersion = null;
3647 }
Dan Sandler0bf2ed82013-12-21 23:33:41 -06003648 return this;
3649 }
3650
Griff Hazenb720abe2014-05-20 13:15:30 -07003651 /**
Griff Hazen5cadc3b2014-05-20 09:55:39 -07003652 * Apply an extender to this notification builder. Extenders may be used to add
3653 * metadata or change options on this builder.
3654 */
Griff Hazen61a9e862014-05-22 16:05:19 -07003655 public Builder extend(Extender extender) {
3656 extender.extend(this);
Griff Hazen5cadc3b2014-05-20 09:55:39 -07003657 return this;
3658 }
3659
Dan Sandler4e787062015-06-17 15:09:48 -04003660 /**
3661 * @hide
3662 */
Julia Reynoldse46bb372016-03-17 11:05:58 -04003663 public Builder setFlag(int mask, boolean value) {
Joe Onorato46439ce2010-11-19 13:56:21 -08003664 if (value) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003665 mN.flags |= mask;
Joe Onorato46439ce2010-11-19 13:56:21 -08003666 } else {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003667 mN.flags &= ~mask;
Joe Onorato46439ce2010-11-19 13:56:21 -08003668 }
Julia Reynoldse46bb372016-03-17 11:05:58 -04003669 return this;
Joe Onorato46439ce2010-11-19 13:56:21 -08003670 }
3671
Dan Sandler26e81cf2014-05-06 10:01:27 -04003672 /**
3673 * Sets {@link Notification#color}.
3674 *
3675 * @param argb The accent color to use
3676 *
3677 * @return The same Builder.
3678 */
Tor Norbye80756e32015-03-02 09:39:27 -08003679 public Builder setColor(@ColorInt int argb) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003680 mN.color = argb;
Julia Reynolds10ee1fc2015-11-09 11:04:55 -05003681 sanitizeColor();
Dan Sandler26e81cf2014-05-06 10:01:27 -04003682 return this;
3683 }
3684
Jorim Jaggid05aa3e2014-08-28 17:52:27 +02003685 private Drawable getProfileBadgeDrawable() {
Chris Wren66619a22016-05-12 16:42:37 -04003686 if (mContext.getUserId() == UserHandle.USER_SYSTEM) {
3687 // This user can never be a badged profile,
3688 // and also includes USER_ALL system notifications.
3689 return null;
3690 }
Christoph Studer7ac80e62014-08-04 16:01:57 +02003691 // Note: This assumes that the current user can read the profile badge of the
3692 // originating user.
Selim Cineke6ff9462016-01-15 15:07:06 -08003693 return mContext.getPackageManager().getUserBadgeForDensityNoBackground(
Julia Reynoldsda303542015-11-23 14:00:20 -05003694 new UserHandle(mContext.getUserId()), 0);
Jorim Jaggid05aa3e2014-08-28 17:52:27 +02003695 }
3696
3697 private Bitmap getProfileBadge() {
3698 Drawable badge = getProfileBadgeDrawable();
Kenny Guy8a0101b2014-05-08 23:34:12 +01003699 if (badge == null) {
3700 return null;
3701 }
Jorim Jaggid05aa3e2014-08-28 17:52:27 +02003702 final int size = mContext.getResources().getDimensionPixelSize(
3703 R.dimen.notification_badge_size);
3704 Bitmap bitmap = Bitmap.createBitmap(size, size, Bitmap.Config.ARGB_8888);
Kenny Guy8a0101b2014-05-08 23:34:12 +01003705 Canvas canvas = new Canvas(bitmap);
Jorim Jaggid05aa3e2014-08-28 17:52:27 +02003706 badge.setBounds(0, 0, size, size);
Kenny Guy8a0101b2014-05-08 23:34:12 +01003707 badge.draw(canvas);
3708 return bitmap;
3709 }
3710
Selim Cinekc848c3a2016-01-13 15:27:30 -08003711 private void bindProfileBadge(RemoteViews contentView) {
Kenny Guy98193ea2014-07-24 19:54:37 +01003712 Bitmap profileBadge = getProfileBadge();
3713
Kenny Guy98193ea2014-07-24 19:54:37 +01003714 if (profileBadge != null) {
Selim Cinekc848c3a2016-01-13 15:27:30 -08003715 contentView.setImageViewBitmap(R.id.profile_badge, profileBadge);
3716 contentView.setViewVisibility(R.id.profile_badge, View.VISIBLE);
Selim Cinek7b9605b2017-01-19 17:36:00 -08003717 if (isColorized()) {
3718 contentView.setDrawableParameters(R.id.profile_badge, false, -1,
3719 getPrimaryTextColor(), PorterDuff.Mode.SRC_ATOP, -1);
3720 }
Kenny Guy98193ea2014-07-24 19:54:37 +01003721 }
Kenny Guy98193ea2014-07-24 19:54:37 +01003722 }
3723
Christoph Studerfe718432014-09-01 18:21:18 +02003724 private void resetStandardTemplate(RemoteViews contentView) {
Selim Cinekeaa29ca2015-11-23 13:51:13 -08003725 resetNotificationHeader(contentView);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003726 resetContentMargins(contentView);
Christoph Studerfe718432014-09-01 18:21:18 +02003727 contentView.setViewVisibility(R.id.right_icon, View.GONE);
Selim Cinek860b6da2015-12-16 19:02:19 -08003728 contentView.setViewVisibility(R.id.title, View.GONE);
Christoph Studerfe718432014-09-01 18:21:18 +02003729 contentView.setTextViewText(R.id.title, null);
Selim Cinek41598732016-01-11 16:58:37 -08003730 contentView.setViewVisibility(R.id.text, View.GONE);
Christoph Studerfe718432014-09-01 18:21:18 +02003731 contentView.setTextViewText(R.id.text, null);
Selim Cinek29603462015-11-17 19:04:39 -08003732 contentView.setViewVisibility(R.id.text_line_1, View.GONE);
Selim Cinek41598732016-01-11 16:58:37 -08003733 contentView.setTextViewText(R.id.text_line_1, null);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003734 }
3735
Selim Cinekeaa29ca2015-11-23 13:51:13 -08003736 /**
3737 * Resets the notification header to its original state
3738 */
3739 private void resetNotificationHeader(RemoteViews contentView) {
Adrian Roosc4337a32016-08-02 18:30:34 -07003740 // Small icon doesn't need to be reset, as it's always set. Resetting would prevent
3741 // re-using the drawable when the notification is updated.
Selim Cinek7b836392015-12-04 20:02:59 -08003742 contentView.setBoolean(R.id.notification_header, "setExpanded", false);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003743 contentView.setTextViewText(R.id.app_name_text, null);
Christoph Studerca1db712014-09-10 17:31:33 +02003744 contentView.setViewVisibility(R.id.chronometer, View.GONE);
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07003745 contentView.setViewVisibility(R.id.header_text, View.GONE);
Adrian Roos9dfb78f2016-06-30 15:43:44 -07003746 contentView.setTextViewText(R.id.header_text, null);
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07003747 contentView.setViewVisibility(R.id.header_text_divider, View.GONE);
Selim Cinek29603462015-11-17 19:04:39 -08003748 contentView.setViewVisibility(R.id.time_divider, View.GONE);
Selim Cinekb85f36fd2016-04-20 18:46:36 -07003749 contentView.setViewVisibility(R.id.time, View.GONE);
Selim Cinekc848c3a2016-01-13 15:27:30 -08003750 contentView.setImageViewIcon(R.id.profile_badge, null);
3751 contentView.setViewVisibility(R.id.profile_badge, View.GONE);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003752 }
3753
3754 private void resetContentMargins(RemoteViews contentView) {
Adrian Roos2d5dbba2016-06-08 17:11:53 -07003755 contentView.setViewLayoutMarginEndDimen(R.id.line1, 0);
3756 contentView.setViewLayoutMarginEndDimen(R.id.text, 0);
Christoph Studerfe718432014-09-01 18:21:18 +02003757 }
3758
Jorim Jaggi445d3c02014-08-19 22:33:42 +02003759 private RemoteViews applyStandardTemplate(int resId) {
Adrian Roos70d7aa32017-01-11 15:39:06 -08003760 return applyStandardTemplate(resId, mParams.reset().fillTextsFrom(this));
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02003761 }
3762
3763 /**
3764 * @param hasProgress whether the progress bar should be shown and set
3765 */
3766 private RemoteViews applyStandardTemplate(int resId, boolean hasProgress) {
Adrian Roos70d7aa32017-01-11 15:39:06 -08003767 return applyStandardTemplate(resId, mParams.reset().hasProgress(hasProgress)
3768 .fillTextsFrom(this));
Adrian Roosc1a80b02016-04-05 14:54:55 -07003769 }
3770
Adrian Roos70d7aa32017-01-11 15:39:06 -08003771 private RemoteViews applyStandardTemplate(int resId, StandardTemplateParams p) {
Kenny Guy77320062014-08-27 21:37:15 +01003772 RemoteViews contentView = new BuilderRemoteViews(mContext.getApplicationInfo(), resId);
Dan Sandler539aad42014-08-04 00:43:39 -04003773
Christoph Studerfe718432014-09-01 18:21:18 +02003774 resetStandardTemplate(contentView);
3775
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003776 final Bundle ex = mN.extras;
Selim Cinek7b9605b2017-01-19 17:36:00 -08003777 updateBackgroundColor(contentView);
Adrian Roos487374f2017-01-11 15:48:14 -08003778 bindNotificationHeader(contentView, p.ambient);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003779 bindLargeIcon(contentView);
Adrian Roos70d7aa32017-01-11 15:39:06 -08003780 boolean showProgress = handleProgressBar(p.hasProgress, contentView, ex);
3781 if (p.title != null) {
Selim Cinek860b6da2015-12-16 19:02:19 -08003782 contentView.setViewVisibility(R.id.title, View.VISIBLE);
Adrian Roos70d7aa32017-01-11 15:39:06 -08003783 contentView.setTextViewText(R.id.title, p.title);
Adrian Roos72171622017-01-27 10:32:06 -08003784 if (!p.ambient) {
3785 setTextViewColorPrimary(contentView, R.id.title);
3786 }
Selim Cinek954cc232016-05-20 13:29:23 -07003787 contentView.setViewLayoutWidth(R.id.title, showProgress
3788 ? ViewGroup.LayoutParams.WRAP_CONTENT
3789 : ViewGroup.LayoutParams.MATCH_PARENT);
Joe Onorato561d3852010-11-20 18:09:34 -08003790 }
Adrian Roos70d7aa32017-01-11 15:39:06 -08003791 if (p.text != null) {
Selim Cinek41598732016-01-11 16:58:37 -08003792 int textId = showProgress ? com.android.internal.R.id.text_line_1
3793 : com.android.internal.R.id.text;
Adrian Roos70d7aa32017-01-11 15:39:06 -08003794 contentView.setTextViewText(textId, p.text);
Adrian Roos72171622017-01-27 10:32:06 -08003795 if (!p.ambient) {
3796 setTextViewColorSecondary(contentView, textId);
3797 }
Selim Cinek41598732016-01-11 16:58:37 -08003798 contentView.setViewVisibility(textId, View.VISIBLE);
Joe Onorato561d3852010-11-20 18:09:34 -08003799 }
Selim Cinekc848c3a2016-01-13 15:27:30 -08003800
Selim Cinek279fa862016-06-14 10:57:25 -07003801 setContentMinHeight(contentView, showProgress || mN.hasLargeIcon());
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003802
Selim Cinek29603462015-11-17 19:04:39 -08003803 return contentView;
3804 }
3805
Selim Cinek7b9605b2017-01-19 17:36:00 -08003806 private void setTextViewColorPrimary(RemoteViews contentView, int id) {
3807 ensureColors();
3808 contentView.setTextColor(id, mPrimaryTextColor);
3809 }
3810
3811 private int getPrimaryTextColor() {
3812 ensureColors();
3813 return mPrimaryTextColor;
3814 }
3815
3816 private int getActionBarColor() {
3817 ensureColors();
3818 return mActionBarColor;
3819 }
3820
Selim Cinek622c64a2017-04-17 17:10:05 -07003821 private int getActionBarColorDeEmphasized() {
3822 int backgroundColor = getBackgroundColor();
3823 return NotificationColorUtil.getShiftedColor(backgroundColor, 12);
3824 }
3825
Selim Cinek7b9605b2017-01-19 17:36:00 -08003826 private void setTextViewColorSecondary(RemoteViews contentView, int id) {
3827 ensureColors();
3828 contentView.setTextColor(id, mSecondaryTextColor);
3829 }
3830
3831 private void ensureColors() {
3832 int backgroundColor = getBackgroundColor();
3833 if (mPrimaryTextColor == COLOR_INVALID
3834 || mSecondaryTextColor == COLOR_INVALID
3835 || mActionBarColor == COLOR_INVALID
3836 || mTextColorsAreForBackground != backgroundColor) {
3837 mTextColorsAreForBackground = backgroundColor;
Selim Cinek5fb73f82017-04-20 16:55:38 -07003838 if (mForegroundColor == COLOR_INVALID || !isColorized()) {
3839 mPrimaryTextColor = NotificationColorUtil.resolvePrimaryColor(mContext,
3840 backgroundColor);
3841 mSecondaryTextColor = NotificationColorUtil.resolveSecondaryColor(mContext,
3842 backgroundColor);
Selim Cinekac5f0272017-05-02 16:05:41 -07003843 if (backgroundColor != COLOR_DEFAULT
3844 && (mBackgroundColorHint != COLOR_INVALID || isColorized())) {
3845 mPrimaryTextColor = NotificationColorUtil.findAlphaToMeetContrast(
3846 mPrimaryTextColor, backgroundColor, 4.5);
3847 mSecondaryTextColor = NotificationColorUtil.findAlphaToMeetContrast(
3848 mSecondaryTextColor, backgroundColor, 4.5);
3849 }
Selim Cinek5fb73f82017-04-20 16:55:38 -07003850 } else {
3851 double backLum = NotificationColorUtil.calculateLuminance(backgroundColor);
3852 double textLum = NotificationColorUtil.calculateLuminance(mForegroundColor);
3853 double contrast = NotificationColorUtil.calculateContrast(mForegroundColor,
3854 backgroundColor);
3855 boolean textDark = backLum > textLum;
3856 if (contrast < 4.5f) {
3857 if (textDark) {
3858 mSecondaryTextColor = NotificationColorUtil.findContrastColor(
3859 mForegroundColor,
3860 backgroundColor,
3861 true /* findFG */,
3862 4.5f);
3863 mPrimaryTextColor = NotificationColorUtil.changeColorLightness(
3864 mSecondaryTextColor, -20);
3865 } else {
3866 mSecondaryTextColor =
3867 NotificationColorUtil.findContrastColorAgainstDark(
3868 mForegroundColor,
3869 backgroundColor,
3870 true /* findFG */,
3871 4.5f);
3872 mPrimaryTextColor = NotificationColorUtil.changeColorLightness(
3873 mSecondaryTextColor, 10);
3874 }
3875 } else {
3876 mPrimaryTextColor = mForegroundColor;
3877 mSecondaryTextColor = NotificationColorUtil.changeColorLightness(
3878 mPrimaryTextColor, textDark ? 10 : -20);
3879 if (NotificationColorUtil.calculateContrast(mSecondaryTextColor,
3880 backgroundColor) < 4.5f) {
3881 // oh well the secondary is not good enough
3882 if (textDark) {
3883 mSecondaryTextColor = NotificationColorUtil.findContrastColor(
3884 mSecondaryTextColor,
3885 backgroundColor,
3886 true /* findFG */,
3887 4.5f);
3888 } else {
3889 mSecondaryTextColor
3890 = NotificationColorUtil.findContrastColorAgainstDark(
3891 mSecondaryTextColor,
3892 backgroundColor,
3893 true /* findFG */,
3894 4.5f);
3895 }
3896 mPrimaryTextColor = NotificationColorUtil.changeColorLightness(
3897 mSecondaryTextColor, textDark ? -20 : 10);
3898 }
3899 }
3900 }
Selim Cinek875ba9b2017-02-13 16:20:17 -08003901 mActionBarColor = NotificationColorUtil.resolveActionBarColor(mContext,
3902 backgroundColor);
Selim Cinek7b9605b2017-01-19 17:36:00 -08003903 }
3904 }
3905
3906 private void updateBackgroundColor(RemoteViews contentView) {
3907 if (isColorized()) {
3908 contentView.setInt(R.id.status_bar_latest_event_content, "setBackgroundColor",
3909 getBackgroundColor());
3910 } else {
3911 // Clear it!
3912 contentView.setInt(R.id.status_bar_latest_event_content, "setBackgroundResource",
3913 0);
3914 }
3915 }
3916
Selim Cinek860b6da2015-12-16 19:02:19 -08003917 /**
3918 * @param remoteView the remote view to update the minheight in
3919 * @param hasMinHeight does it have a mimHeight
3920 * @hide
3921 */
3922 void setContentMinHeight(RemoteViews remoteView, boolean hasMinHeight) {
3923 int minHeight = 0;
3924 if (hasMinHeight) {
3925 // we need to set the minHeight of the notification
3926 minHeight = mContext.getResources().getDimensionPixelSize(
3927 com.android.internal.R.dimen.notification_min_content_height);
3928 }
3929 remoteView.setInt(R.id.notification_main_column, "setMinimumHeight", minHeight);
3930 }
3931
Selim Cinek29603462015-11-17 19:04:39 -08003932 private boolean handleProgressBar(boolean hasProgress, RemoteViews contentView, Bundle ex) {
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003933 final int max = ex.getInt(EXTRA_PROGRESS_MAX, 0);
3934 final int progress = ex.getInt(EXTRA_PROGRESS, 0);
3935 final boolean ind = ex.getBoolean(EXTRA_PROGRESS_INDETERMINATE);
3936 if (hasProgress && (max != 0 || ind)) {
Selim Cinek29603462015-11-17 19:04:39 -08003937 contentView.setViewVisibility(com.android.internal.R.id.progress, View.VISIBLE);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003938 contentView.setProgressBar(
Selim Cinek29603462015-11-17 19:04:39 -08003939 R.id.progress, max, progress, ind);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003940 contentView.setProgressBackgroundTintList(
3941 R.id.progress, ColorStateList.valueOf(mContext.getColor(
3942 R.color.notification_progress_background_color)));
Selim Cinek29603462015-11-17 19:04:39 -08003943 if (mN.color != COLOR_DEFAULT) {
Adrian Roos4ff3b122016-02-01 12:26:13 -08003944 ColorStateList colorStateList = ColorStateList.valueOf(resolveContrastColor());
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003945 contentView.setProgressTintList(R.id.progress, colorStateList);
3946 contentView.setProgressIndeterminateTintList(R.id.progress, colorStateList);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003947 }
Selim Cinek29603462015-11-17 19:04:39 -08003948 return true;
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003949 } else {
3950 contentView.setViewVisibility(R.id.progress, View.GONE);
Selim Cinek29603462015-11-17 19:04:39 -08003951 return false;
Jeff Sharkey1c400132011-08-05 14:50:13 -07003952 }
Joe Onorato561d3852010-11-20 18:09:34 -08003953 }
3954
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003955 private void bindLargeIcon(RemoteViews contentView) {
Selim Cinek279fa862016-06-14 10:57:25 -07003956 if (mN.mLargeIcon == null && mN.largeIcon != null) {
3957 mN.mLargeIcon = Icon.createWithBitmap(mN.largeIcon);
3958 }
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003959 if (mN.mLargeIcon != null) {
3960 contentView.setViewVisibility(R.id.right_icon, View.VISIBLE);
3961 contentView.setImageViewIcon(R.id.right_icon, mN.mLargeIcon);
3962 processLargeLegacyIcon(mN.mLargeIcon, contentView);
Adrian Roos2d5dbba2016-06-08 17:11:53 -07003963 int endMargin = R.dimen.notification_content_picture_margin;
3964 contentView.setViewLayoutMarginEndDimen(R.id.line1, endMargin);
3965 contentView.setViewLayoutMarginEndDimen(R.id.text, endMargin);
3966 contentView.setViewLayoutMarginEndDimen(R.id.progress, endMargin);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003967 }
3968 }
3969
Adrian Roos487374f2017-01-11 15:48:14 -08003970 private void bindNotificationHeader(RemoteViews contentView, boolean ambient) {
3971 bindSmallIcon(contentView, ambient);
3972 bindHeaderAppName(contentView, ambient);
3973 if (!ambient) {
3974 // Ambient view does not have these
3975 bindHeaderText(contentView);
3976 bindHeaderChronometerAndTime(contentView);
Adrian Roos487374f2017-01-11 15:48:14 -08003977 bindProfileBadge(contentView);
3978 }
Adrian Roosd83e9992017-03-16 15:17:57 -07003979 bindExpandButton(contentView);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003980 }
3981
3982 private void bindExpandButton(RemoteViews contentView) {
Selim Cinek99104832017-01-25 14:47:33 -08003983 int color = getPrimaryHighlightColor();
Selim Cinek7b9605b2017-01-19 17:36:00 -08003984 contentView.setDrawableParameters(R.id.expand_button, false, -1, color,
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003985 PorterDuff.Mode.SRC_ATOP, -1);
Selim Cinekea4bef72015-12-02 15:51:10 -08003986 contentView.setInt(R.id.notification_header, "setOriginalNotificationColor",
Selim Cinek7b9605b2017-01-19 17:36:00 -08003987 color);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003988 }
3989
Selim Cinek99104832017-01-25 14:47:33 -08003990 /**
3991 * @return the color that is used as the first primary highlight color. This is applied
3992 * in several places like the action buttons or the app name in the header.
3993 */
3994 private int getPrimaryHighlightColor() {
3995 return isColorized() ? getPrimaryTextColor() : resolveContrastColor();
3996 }
3997
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003998 private void bindHeaderChronometerAndTime(RemoteViews contentView) {
3999 if (showsTimeOrChronometer()) {
Selim Cinek29603462015-11-17 19:04:39 -08004000 contentView.setViewVisibility(R.id.time_divider, View.VISIBLE);
Selim Cinek7b9605b2017-01-19 17:36:00 -08004001 setTextViewColorSecondary(contentView, R.id.time_divider);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004002 if (mN.extras.getBoolean(EXTRA_SHOW_CHRONOMETER)) {
4003 contentView.setViewVisibility(R.id.chronometer, View.VISIBLE);
4004 contentView.setLong(R.id.chronometer, "setBase",
4005 mN.when + (SystemClock.elapsedRealtime() - System.currentTimeMillis()));
4006 contentView.setBoolean(R.id.chronometer, "setStarted", true);
Adrian Roos96b7e202016-05-17 13:50:38 -07004007 boolean countsDown = mN.extras.getBoolean(EXTRA_CHRONOMETER_COUNT_DOWN);
Selim Cinekc3b752e2016-04-20 16:13:59 -07004008 contentView.setChronometerCountDown(R.id.chronometer, countsDown);
Selim Cinek7b9605b2017-01-19 17:36:00 -08004009 setTextViewColorSecondary(contentView, R.id.chronometer);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004010 } else {
4011 contentView.setViewVisibility(R.id.time, View.VISIBLE);
4012 contentView.setLong(R.id.time, "setTime", mN.when);
Selim Cinek7b9605b2017-01-19 17:36:00 -08004013 setTextViewColorSecondary(contentView, R.id.time);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004014 }
Selim Cinekb85f36fd2016-04-20 18:46:36 -07004015 } else {
4016 // We still want a time to be set but gone, such that we can show and hide it
4017 // on demand in case it's a child notification without anything in the header
4018 contentView.setLong(R.id.time, "setTime", mN.when != 0 ? mN.when : mN.creationTime);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004019 }
4020 }
4021
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07004022 private void bindHeaderText(RemoteViews contentView) {
4023 CharSequence headerText = mN.extras.getCharSequence(EXTRA_SUB_TEXT);
4024 if (headerText == null && mStyle != null && mStyle.mSummaryTextSet
Selim Cinek03d0d652015-11-13 13:18:09 -05004025 && mStyle.hasSummaryInHeader()) {
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07004026 headerText = mStyle.mSummaryText;
Selim Cinek03d0d652015-11-13 13:18:09 -05004027 }
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07004028 if (headerText == null
4029 && mContext.getApplicationInfo().targetSdkVersion < Build.VERSION_CODES.N
4030 && mN.extras.getCharSequence(EXTRA_INFO_TEXT) != null) {
4031 headerText = mN.extras.getCharSequence(EXTRA_INFO_TEXT);
4032 }
4033 if (headerText != null) {
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004034 // TODO: Remove the span entirely to only have the string with propper formating.
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07004035 contentView.setTextViewText(R.id.header_text, processLegacyText(headerText));
Selim Cinek7b9605b2017-01-19 17:36:00 -08004036 setTextViewColorSecondary(contentView, R.id.header_text);
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07004037 contentView.setViewVisibility(R.id.header_text, View.VISIBLE);
4038 contentView.setViewVisibility(R.id.header_text_divider, View.VISIBLE);
Selim Cinek7b9605b2017-01-19 17:36:00 -08004039 setTextViewColorSecondary(contentView, R.id.header_text_divider);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004040 }
4041 }
4042
Adrian Rooseba05822016-04-22 17:09:27 -07004043 /**
4044 * @hide
4045 */
4046 public String loadHeaderAppName() {
Dan Sandler732bd6c2016-04-12 14:20:32 -04004047 CharSequence name = null;
4048 final PackageManager pm = mContext.getPackageManager();
4049 if (mN.extras.containsKey(EXTRA_SUBSTITUTE_APP_NAME)) {
4050 // only system packages which lump together a bunch of unrelated stuff
4051 // may substitute a different name to make the purpose of the
4052 // notification more clear. the correct package label should always
4053 // be accessible via SystemUI.
4054 final String pkg = mContext.getPackageName();
4055 final String subName = mN.extras.getString(EXTRA_SUBSTITUTE_APP_NAME);
4056 if (PackageManager.PERMISSION_GRANTED == pm.checkPermission(
4057 android.Manifest.permission.SUBSTITUTE_NOTIFICATION_APP_NAME, pkg)) {
4058 name = subName;
4059 } else {
4060 Log.w(TAG, "warning: pkg "
4061 + pkg + " attempting to substitute app name '" + subName
4062 + "' without holding perm "
4063 + android.Manifest.permission.SUBSTITUTE_NOTIFICATION_APP_NAME);
4064 }
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004065 }
Dan Sandler732bd6c2016-04-12 14:20:32 -04004066 if (TextUtils.isEmpty(name)) {
4067 name = pm.getApplicationLabel(mContext.getApplicationInfo());
4068 }
4069 if (TextUtils.isEmpty(name)) {
4070 // still nothing?
4071 return null;
4072 }
4073
4074 return String.valueOf(name);
4075 }
Adrian Roos487374f2017-01-11 15:48:14 -08004076 private void bindHeaderAppName(RemoteViews contentView, boolean ambient) {
Dan Sandler732bd6c2016-04-12 14:20:32 -04004077 contentView.setTextViewText(R.id.app_name_text, loadHeaderAppName());
Adrian Roos72171622017-01-27 10:32:06 -08004078 if (isColorized() && !ambient) {
Selim Cinek7b9605b2017-01-19 17:36:00 -08004079 setTextViewColorPrimary(contentView, R.id.app_name_text);
4080 } else {
4081 contentView.setTextColor(R.id.app_name_text,
4082 ambient ? resolveAmbientColor() : resolveContrastColor());
4083 }
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004084 }
4085
Adrian Roos487374f2017-01-11 15:48:14 -08004086 private void bindSmallIcon(RemoteViews contentView, boolean ambient) {
Selim Cinek279fa862016-06-14 10:57:25 -07004087 if (mN.mSmallIcon == null && mN.icon != 0) {
4088 mN.mSmallIcon = Icon.createWithResource(mContext, mN.icon);
4089 }
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004090 contentView.setImageViewIcon(R.id.icon, mN.mSmallIcon);
Adrian Roos9b45a152016-06-28 13:32:29 -07004091 contentView.setDrawableParameters(R.id.icon, false /* targetBackground */,
4092 -1 /* alpha */, -1 /* colorFilter */, null /* mode */, mN.iconLevel);
Adrian Roos487374f2017-01-11 15:48:14 -08004093 processSmallIconColor(mN.mSmallIcon, contentView, ambient);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004094 }
4095
Jorim Jaggi445d3c02014-08-19 22:33:42 +02004096 /**
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02004097 * @return true if the built notification will show the time or the chronometer; false
4098 * otherwise
4099 */
4100 private boolean showsTimeOrChronometer() {
Selim Cinekc2c0b042016-05-18 17:13:46 -07004101 return mN.showsTime() || mN.showsChronometer();
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02004102 }
4103
Christoph Studerfe718432014-09-01 18:21:18 +02004104 private void resetStandardTemplateWithActions(RemoteViews big) {
Adrian Roos4c1fcc82016-03-31 14:39:39 -07004105 // actions_container is only reset when there are no actions to avoid focus issues with
4106 // remote inputs.
Christoph Studerfe718432014-09-01 18:21:18 +02004107 big.setViewVisibility(R.id.actions, View.GONE);
Christoph Studerfe718432014-09-01 18:21:18 +02004108 big.removeAllViews(R.id.actions);
Adrian Roose458aa82015-12-08 16:17:19 -08004109
4110 big.setViewVisibility(R.id.notification_material_reply_container, View.GONE);
4111 big.setTextViewText(R.id.notification_material_reply_text_1, null);
4112
4113 big.setViewVisibility(R.id.notification_material_reply_text_2, View.GONE);
4114 big.setTextViewText(R.id.notification_material_reply_text_2, null);
4115 big.setViewVisibility(R.id.notification_material_reply_text_3, View.GONE);
4116 big.setTextViewText(R.id.notification_material_reply_text_3, null);
Adrian Roosf852a422016-06-03 13:33:43 -07004117
4118 big.setViewLayoutMarginBottomDimen(R.id.notification_action_list_margin_target, 0);
Christoph Studerfe718432014-09-01 18:21:18 +02004119 }
4120
Daniel Sandler96fd7c12012-03-30 16:37:36 -04004121 private RemoteViews applyStandardTemplateWithActions(int layoutId) {
Adrian Roos70d7aa32017-01-11 15:39:06 -08004122 return applyStandardTemplateWithActions(layoutId, mParams.reset().fillTextsFrom(this));
Adrian Roos48d746a2016-04-12 14:57:28 -07004123 }
4124
Adrian Roos70d7aa32017-01-11 15:39:06 -08004125 private RemoteViews applyStandardTemplateWithActions(int layoutId,
4126 StandardTemplateParams p) {
4127 RemoteViews big = applyStandardTemplate(layoutId, p);
Daniel Sandler96fd7c12012-03-30 16:37:36 -04004128
Christoph Studerfe718432014-09-01 18:21:18 +02004129 resetStandardTemplateWithActions(big);
4130
Adrian Roose458aa82015-12-08 16:17:19 -08004131 boolean validRemoteInput = false;
4132
Daniel Sandler96fd7c12012-03-30 16:37:36 -04004133 int N = mActions.size();
Adrian Roos487374f2017-01-11 15:48:14 -08004134 boolean emphazisedMode = mN.fullScreenIntent != null && !p.ambient;
Selim Cinek06e9e1f2016-07-08 17:14:16 -07004135 big.setBoolean(R.id.actions, "setEmphasizedMode", emphazisedMode);
Daniel Sandler96fd7c12012-03-30 16:37:36 -04004136 if (N > 0) {
Adrian Roos7052de52016-03-03 15:53:34 -08004137 big.setViewVisibility(R.id.actions_container, View.VISIBLE);
Daniel Sandler96fd7c12012-03-30 16:37:36 -04004138 big.setViewVisibility(R.id.actions, View.VISIBLE);
Adrian Roos487374f2017-01-11 15:48:14 -08004139 if (p.ambient) {
4140 big.setInt(R.id.actions, "setBackgroundColor", Color.TRANSPARENT);
Selim Cinek7b9605b2017-01-19 17:36:00 -08004141 } else if (isColorized()) {
4142 big.setInt(R.id.actions, "setBackgroundColor", getActionBarColor());
4143 } else {
4144 big.setInt(R.id.actions, "setBackgroundColor", mContext.getColor(
4145 R.color.notification_action_list));
Adrian Roos487374f2017-01-11 15:48:14 -08004146 }
Adrian Roosf852a422016-06-03 13:33:43 -07004147 big.setViewLayoutMarginBottomDimen(R.id.notification_action_list_margin_target,
4148 R.dimen.notification_action_list_height);
Daniel Sandler8680bf82012-05-15 16:52:52 -04004149 if (N>MAX_ACTION_BUTTONS) N=MAX_ACTION_BUTTONS;
Daniel Sandler96fd7c12012-03-30 16:37:36 -04004150 for (int i=0; i<N; i++) {
Adrian Roose458aa82015-12-08 16:17:19 -08004151 Action action = mActions.get(i);
4152 validRemoteInput |= hasValidRemoteInput(action);
4153
Selim Cinek06e9e1f2016-07-08 17:14:16 -07004154 final RemoteViews button = generateActionButton(action, emphazisedMode,
Adrian Roos487374f2017-01-11 15:48:14 -08004155 i % 2 != 0, p.ambient);
Daniel Sandler96fd7c12012-03-30 16:37:36 -04004156 big.addView(R.id.actions, button);
4157 }
Adrian Roos4c1fcc82016-03-31 14:39:39 -07004158 } else {
4159 big.setViewVisibility(R.id.actions_container, View.GONE);
Daniel Sandler96fd7c12012-03-30 16:37:36 -04004160 }
Adrian Roose458aa82015-12-08 16:17:19 -08004161
4162 CharSequence[] replyText = mN.extras.getCharSequenceArray(EXTRA_REMOTE_INPUT_HISTORY);
Adrian Roos487374f2017-01-11 15:48:14 -08004163 if (!p.ambient && validRemoteInput && replyText != null
Adrian Roose458aa82015-12-08 16:17:19 -08004164 && replyText.length > 0 && !TextUtils.isEmpty(replyText[0])) {
4165 big.setViewVisibility(R.id.notification_material_reply_container, View.VISIBLE);
4166 big.setTextViewText(R.id.notification_material_reply_text_1, replyText[0]);
Selim Cinek7b9605b2017-01-19 17:36:00 -08004167 setTextViewColorSecondary(big, R.id.notification_material_reply_text_1);
Adrian Roose458aa82015-12-08 16:17:19 -08004168
4169 if (replyText.length > 1 && !TextUtils.isEmpty(replyText[1])) {
4170 big.setViewVisibility(R.id.notification_material_reply_text_2, View.VISIBLE);
4171 big.setTextViewText(R.id.notification_material_reply_text_2, replyText[1]);
Selim Cinek7b9605b2017-01-19 17:36:00 -08004172 setTextViewColorSecondary(big, R.id.notification_material_reply_text_2);
Adrian Roose458aa82015-12-08 16:17:19 -08004173
4174 if (replyText.length > 2 && !TextUtils.isEmpty(replyText[2])) {
4175 big.setViewVisibility(
4176 R.id.notification_material_reply_text_3, View.VISIBLE);
4177 big.setTextViewText(R.id.notification_material_reply_text_3, replyText[2]);
Selim Cinek7b9605b2017-01-19 17:36:00 -08004178 setTextViewColorSecondary(big, R.id.notification_material_reply_text_3);
Adrian Roose458aa82015-12-08 16:17:19 -08004179 }
4180 }
4181 }
4182
Daniel Sandler96fd7c12012-03-30 16:37:36 -04004183 return big;
4184 }
4185
Adrian Roose458aa82015-12-08 16:17:19 -08004186 private boolean hasValidRemoteInput(Action action) {
4187 if (TextUtils.isEmpty(action.title) || action.actionIntent == null) {
4188 // Weird actions
4189 return false;
4190 }
4191
4192 RemoteInput[] remoteInputs = action.getRemoteInputs();
4193 if (remoteInputs == null) {
4194 return false;
4195 }
4196
4197 for (RemoteInput r : remoteInputs) {
4198 CharSequence[] choices = r.getChoices();
4199 if (r.getAllowFreeFormInput() || (choices != null && choices.length != 0)) {
4200 return true;
4201 }
4202 }
4203 return false;
4204 }
4205
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004206 /**
4207 * Construct a RemoteViews for the final 1U notification layout. In order:
4208 * 1. Custom contentView from the caller
4209 * 2. Style's proposed content view
4210 * 3. Standard template view
4211 */
Julia Reynolds3b848122016-02-26 10:45:32 -05004212 public RemoteViews createContentView() {
Selim Cinek7d1009b2017-01-25 15:28:28 -08004213 return createContentView(false /* increasedheight */ );
4214 }
4215
4216 /**
4217 * Construct a RemoteViews for the smaller content view.
4218 *
4219 * @param increasedHeight true if this layout be created with an increased height. Some
4220 * styles may support showing more then just that basic 1U size
4221 * and the system may decide to render important notifications
4222 * slightly bigger even when collapsed.
4223 *
4224 * @hide
4225 */
4226 public RemoteViews createContentView(boolean increasedHeight) {
Selim Cinek593610c2016-02-16 18:42:57 -08004227 if (mN.contentView != null && (mStyle == null || !mStyle.displayCustomViewInline())) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004228 return mN.contentView;
4229 } else if (mStyle != null) {
Selim Cinek7d1009b2017-01-25 15:28:28 -08004230 final RemoteViews styleView = mStyle.makeContentView(increasedHeight);
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004231 if (styleView != null) {
4232 return styleView;
4233 }
Joe Onorato46439ce2010-11-19 13:56:21 -08004234 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004235 return applyStandardTemplate(getBaseLayoutResource());
Joe Onorato46439ce2010-11-19 13:56:21 -08004236 }
4237
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004238 /**
4239 * Construct a RemoteViews for the final big notification layout.
4240 */
Julia Reynolds3b848122016-02-26 10:45:32 -05004241 public RemoteViews createBigContentView() {
Selim Cinek850a8542015-11-11 11:48:36 -05004242 RemoteViews result = null;
Selim Cinek593610c2016-02-16 18:42:57 -08004243 if (mN.bigContentView != null
4244 && (mStyle == null || !mStyle.displayCustomViewInline())) {
Julia Reynolds089e3e42015-11-18 09:59:57 -05004245 return mN.bigContentView;
4246 } else if (mStyle != null) {
Selim Cinek850a8542015-11-11 11:48:36 -05004247 result = mStyle.makeBigContentView();
Selim Cinek90dcf6d2015-11-18 20:24:13 -08004248 hideLine1Text(result);
Selim Cinekcc10bfb2016-02-10 16:24:21 -08004249 } else if (mActions.size() != 0) {
4250 result = applyStandardTemplateWithActions(getBigBaseLayoutResource());
Selim Cinek850a8542015-11-11 11:48:36 -05004251 }
Selim Cinek6743c0b2017-01-18 18:24:01 -08004252 makeHeaderExpanded(result);
Selim Cinek850a8542015-11-11 11:48:36 -05004253 return result;
4254 }
Daniel Sandler96fd7c12012-03-30 16:37:36 -04004255
Selim Cinekeaa29ca2015-11-23 13:51:13 -08004256 /**
Selim Cinek414ad332017-02-24 19:06:12 -08004257 * Construct a RemoteViews for the final notification header only. This will not be
4258 * colorized.
Selim Cinekeaa29ca2015-11-23 13:51:13 -08004259 *
4260 * @hide
4261 */
4262 public RemoteViews makeNotificationHeader() {
Selim Cinek414ad332017-02-24 19:06:12 -08004263 Boolean colorized = (Boolean) mN.extras.get(EXTRA_COLORIZED);
4264 mN.extras.putBoolean(EXTRA_COLORIZED, false);
Selim Cinekeaa29ca2015-11-23 13:51:13 -08004265 RemoteViews header = new BuilderRemoteViews(mContext.getApplicationInfo(),
4266 R.layout.notification_template_header);
4267 resetNotificationHeader(header);
Adrian Roos487374f2017-01-11 15:48:14 -08004268 bindNotificationHeader(header, false /* ambient */);
Selim Cinek414ad332017-02-24 19:06:12 -08004269 if (colorized != null) {
4270 mN.extras.putBoolean(EXTRA_COLORIZED, colorized);
4271 } else {
4272 mN.extras.remove(EXTRA_COLORIZED);
4273 }
Selim Cinekeaa29ca2015-11-23 13:51:13 -08004274 return header;
4275 }
4276
Adrian Roos487374f2017-01-11 15:48:14 -08004277 /**
4278 * Construct a RemoteViews for the ambient version of the notification.
4279 *
4280 * @hide
4281 */
4282 public RemoteViews makeAmbientNotification() {
4283 RemoteViews ambient = applyStandardTemplateWithActions(
4284 R.layout.notification_template_material_ambient,
Adrian Roos0bc3f6a2017-03-06 11:54:05 -08004285 mParams.reset().ambient(true).fillTextsFrom(this).hasProgress(false));
Adrian Roos487374f2017-01-11 15:48:14 -08004286 return ambient;
4287 }
4288
Selim Cinek29603462015-11-17 19:04:39 -08004289 private void hideLine1Text(RemoteViews result) {
Selim Cinekcc10bfb2016-02-10 16:24:21 -08004290 if (result != null) {
4291 result.setViewVisibility(R.id.text_line_1, View.GONE);
4292 }
Selim Cinek29603462015-11-17 19:04:39 -08004293 }
4294
Selim Cinek6743c0b2017-01-18 18:24:01 -08004295 /**
4296 * Adapt the Notification header if this view is used as an expanded view.
4297 *
4298 * @hide
4299 */
4300 public static void makeHeaderExpanded(RemoteViews result) {
Selim Cinekcc10bfb2016-02-10 16:24:21 -08004301 if (result != null) {
4302 result.setBoolean(R.id.notification_header, "setExpanded", true);
4303 }
Daniel Sandler96fd7c12012-03-30 16:37:36 -04004304 }
4305
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004306 /**
4307 * Construct a RemoteViews for the final heads-up notification layout.
Selim Cinek87ed69b2017-02-09 15:59:43 -08004308 *
4309 * @param increasedHeight true if this layout be created with an increased height. Some
4310 * styles may support showing more then just that basic 1U size
4311 * and the system may decide to render important notifications
4312 * slightly bigger even when collapsed.
4313 *
4314 * @hide
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004315 */
Selim Cinek87ed69b2017-02-09 15:59:43 -08004316 public RemoteViews createHeadsUpContentView(boolean increasedHeight) {
Selim Cinek593610c2016-02-16 18:42:57 -08004317 if (mN.headsUpContentView != null
4318 && (mStyle == null || !mStyle.displayCustomViewInline())) {
Julia Reynolds089e3e42015-11-18 09:59:57 -05004319 return mN.headsUpContentView;
4320 } else if (mStyle != null) {
Selim Cinek87ed69b2017-02-09 15:59:43 -08004321 final RemoteViews styleView = mStyle.makeHeadsUpContentView(increasedHeight);
4322 if (styleView != null) {
4323 return styleView;
4324 }
Julia Reynolds089e3e42015-11-18 09:59:57 -05004325 } else if (mActions.size() == 0) {
4326 return null;
4327 }
4328
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01004329 return applyStandardTemplateWithActions(getBigBaseLayoutResource());
Chris Wren8fd39ec2014-02-27 17:43:26 -05004330 }
4331
Selim Cinek624c02db2015-12-14 21:00:02 -08004332 /**
Selim Cinek87ed69b2017-02-09 15:59:43 -08004333 * Construct a RemoteViews for the final heads-up notification layout.
4334 */
4335 public RemoteViews createHeadsUpContentView() {
4336 return createHeadsUpContentView(false /* useIncreasedHeight */);
4337 }
4338
4339 /**
Selim Cinek624c02db2015-12-14 21:00:02 -08004340 * Construct a RemoteViews for the display in public contexts like on the lockscreen.
4341 *
4342 * @hide
4343 */
4344 public RemoteViews makePublicContentView() {
Adrian Roos1a1ecfc2017-04-17 11:17:59 -07004345 return makePublicView(false /* ambient */);
4346 }
4347
4348 /**
4349 * Construct a RemoteViews for the display in public contexts like on the lockscreen.
4350 *
4351 * @hide
4352 */
4353 public RemoteViews makePublicAmbientNotification() {
4354 return makePublicView(true /* ambient */);
4355 }
4356
4357 private RemoteViews makePublicView(boolean ambient) {
Selim Cinek624c02db2015-12-14 21:00:02 -08004358 if (mN.publicVersion != null) {
4359 final Builder builder = recoverBuilder(mContext, mN.publicVersion);
Adrian Roos1a1ecfc2017-04-17 11:17:59 -07004360 return ambient ? builder.makeAmbientNotification() : builder.createContentView();
Selim Cinek624c02db2015-12-14 21:00:02 -08004361 }
4362 Bundle savedBundle = mN.extras;
4363 Style style = mStyle;
4364 mStyle = null;
4365 Icon largeIcon = mN.mLargeIcon;
4366 mN.mLargeIcon = null;
Selim Cinek279fa862016-06-14 10:57:25 -07004367 Bitmap largeIconLegacy = mN.largeIcon;
4368 mN.largeIcon = null;
Selim Cinek624c02db2015-12-14 21:00:02 -08004369 Bundle publicExtras = new Bundle();
4370 publicExtras.putBoolean(EXTRA_SHOW_WHEN,
4371 savedBundle.getBoolean(EXTRA_SHOW_WHEN));
4372 publicExtras.putBoolean(EXTRA_SHOW_CHRONOMETER,
4373 savedBundle.getBoolean(EXTRA_SHOW_CHRONOMETER));
Adrian Roos96b7e202016-05-17 13:50:38 -07004374 publicExtras.putBoolean(EXTRA_CHRONOMETER_COUNT_DOWN,
4375 savedBundle.getBoolean(EXTRA_CHRONOMETER_COUNT_DOWN));
Selim Cinek624c02db2015-12-14 21:00:02 -08004376 publicExtras.putCharSequence(EXTRA_TITLE,
Adrian Roos1a1ecfc2017-04-17 11:17:59 -07004377 mContext.getString(com.android.internal.R.string.notification_hidden_text));
Selim Cinek624c02db2015-12-14 21:00:02 -08004378 mN.extras = publicExtras;
Adrian Roos1a1ecfc2017-04-17 11:17:59 -07004379 final RemoteViews view = ambient ? makeAmbientNotification()
4380 : applyStandardTemplate(getBaseLayoutResource());
Selim Cinek624c02db2015-12-14 21:00:02 -08004381 mN.extras = savedBundle;
4382 mN.mLargeIcon = largeIcon;
Selim Cinek279fa862016-06-14 10:57:25 -07004383 mN.largeIcon = largeIconLegacy;
Selim Cinek624c02db2015-12-14 21:00:02 -08004384 mStyle = style;
Adrian Roos1a1ecfc2017-04-17 11:17:59 -07004385 return view;
Selim Cinek624c02db2015-12-14 21:00:02 -08004386 }
4387
Selim Cinek6743c0b2017-01-18 18:24:01 -08004388 /**
4389 * Construct a content view for the display when low - priority
4390 *
4391 * @param useRegularSubtext uses the normal subtext set if there is one available. Otherwise
4392 * a new subtext is created consisting of the content of the
4393 * notification.
4394 * @hide
4395 */
4396 public RemoteViews makeLowPriorityContentView(boolean useRegularSubtext) {
4397 int color = mN.color;
4398 mN.color = COLOR_DEFAULT;
4399 CharSequence summary = mN.extras.getCharSequence(EXTRA_SUB_TEXT);
4400 if (!useRegularSubtext || TextUtils.isEmpty(summary)) {
4401 CharSequence newSummary = createSummaryText();
4402 if (!TextUtils.isEmpty(newSummary)) {
4403 mN.extras.putCharSequence(EXTRA_SUB_TEXT, newSummary);
4404 }
4405 }
Selim Cinek875ba9b2017-02-13 16:20:17 -08004406
Selim Cinek6743c0b2017-01-18 18:24:01 -08004407 RemoteViews header = makeNotificationHeader();
Selim Cinek1b554392017-02-28 17:22:49 -08004408 header.setBoolean(R.id.notification_header, "setAcceptAllTouches", true);
Selim Cinek6743c0b2017-01-18 18:24:01 -08004409 if (summary != null) {
4410 mN.extras.putCharSequence(EXTRA_SUB_TEXT, summary);
4411 } else {
4412 mN.extras.remove(EXTRA_SUB_TEXT);
4413 }
4414 mN.color = color;
4415 return header;
4416 }
Selim Cinek624c02db2015-12-14 21:00:02 -08004417
Selim Cinek6743c0b2017-01-18 18:24:01 -08004418 private CharSequence createSummaryText() {
4419 CharSequence titleText = mN.extras.getCharSequence(Notification.EXTRA_TITLE);
4420 if (USE_ONLY_TITLE_IN_LOW_PRIORITY_SUMMARY) {
4421 return titleText;
4422 }
4423 SpannableStringBuilder summary = new SpannableStringBuilder();
4424 if (titleText == null) {
4425 titleText = mN.extras.getCharSequence(Notification.EXTRA_TITLE_BIG);
4426 }
4427 BidiFormatter bidi = BidiFormatter.getInstance();
4428 if (titleText != null) {
4429 summary.append(bidi.unicodeWrap(titleText));
4430 }
4431 CharSequence contentText = mN.extras.getCharSequence(Notification.EXTRA_TEXT);
4432 if (titleText != null && contentText != null) {
4433 summary.append(bidi.unicodeWrap(mContext.getText(
4434 R.string.notification_header_divider_symbol_with_spaces)));
4435 }
4436 if (contentText != null) {
4437 summary.append(bidi.unicodeWrap(contentText));
4438 }
4439 return summary;
4440 }
Chris Wren8fd39ec2014-02-27 17:43:26 -05004441
Selim Cinek06e9e1f2016-07-08 17:14:16 -07004442 private RemoteViews generateActionButton(Action action, boolean emphazisedMode,
Adrian Roos487374f2017-01-11 15:48:14 -08004443 boolean oddAction, boolean ambient) {
Daniel Sandler8680bf82012-05-15 16:52:52 -04004444 final boolean tombstone = (action.actionIntent == null);
Selim Cinekf33b1112015-07-15 17:45:11 -07004445 RemoteViews button = new BuilderRemoteViews(mContext.getApplicationInfo(),
Selim Cinek06e9e1f2016-07-08 17:14:16 -07004446 emphazisedMode ? getEmphasizedActionLayoutResource()
4447 : tombstone ? getActionTombstoneLayoutResource()
4448 : getActionLayoutResource());
Daniel Sandler8680bf82012-05-15 16:52:52 -04004449 if (!tombstone) {
Daniel Sandlere5518842012-05-10 16:20:40 -04004450 button.setOnClickPendingIntent(R.id.action0, action.actionIntent);
Daniel Sandlere5518842012-05-10 16:20:40 -04004451 }
Daniel Sandler96fd7c12012-03-30 16:37:36 -04004452 button.setContentDescription(R.id.action0, action.title);
Adrian Roosfe84e1f2015-11-04 15:55:39 -08004453 if (action.mRemoteInputs != null) {
4454 button.setRemoteInputs(R.id.action0, action.mRemoteInputs);
4455 }
Selim Cinek7b9605b2017-01-19 17:36:00 -08004456 // TODO: handle emphasized mode / actions right
Selim Cinek06e9e1f2016-07-08 17:14:16 -07004457 if (emphazisedMode) {
Selim Cinek981962e2016-07-20 20:41:58 -07004458 // change the background bgColor
Selim Cinek622c64a2017-04-17 17:10:05 -07004459 int bgColor;
4460 if (isColorized()) {
4461 bgColor = oddAction ? getActionBarColor() : getActionBarColorDeEmphasized();
4462 } else {
4463 bgColor = mContext.getColor(oddAction ? R.color.notification_action_list
4464 : R.color.notification_action_list_dark);
4465 }
Selim Cinek981962e2016-07-20 20:41:58 -07004466 button.setDrawableParameters(R.id.button_holder, true, -1, bgColor,
Selim Cinek06e9e1f2016-07-08 17:14:16 -07004467 PorterDuff.Mode.SRC_ATOP, -1);
Selim Cinek981962e2016-07-20 20:41:58 -07004468 CharSequence title = action.title;
4469 ColorStateList[] outResultColor = null;
4470 if (isLegacy()) {
4471 title = clearColorSpans(title);
4472 } else {
4473 outResultColor = new ColorStateList[1];
4474 title = ensureColorSpanContrast(title, bgColor, outResultColor);
4475 }
4476 button.setTextViewText(R.id.action0, title);
Selim Cinek7b9605b2017-01-19 17:36:00 -08004477 setTextViewColorPrimary(button, R.id.action0);
Selim Cinek981962e2016-07-20 20:41:58 -07004478 if (outResultColor != null && outResultColor[0] != null) {
4479 // We need to set the text color as well since changing a text to uppercase
4480 // clears its spans.
4481 button.setTextColor(R.id.action0, outResultColor[0]);
Selim Cinek7b9605b2017-01-19 17:36:00 -08004482 } else if (mN.color != COLOR_DEFAULT && !isColorized()) {
Selim Cinek981962e2016-07-20 20:41:58 -07004483 button.setTextColor(R.id.action0,resolveContrastColor());
4484 }
Selim Cinek06e9e1f2016-07-08 17:14:16 -07004485 } else {
Selim Cinek981962e2016-07-20 20:41:58 -07004486 button.setTextViewText(R.id.action0, processLegacyText(action.title));
Adrian Roos72171622017-01-27 10:32:06 -08004487 if (isColorized() && !ambient) {
Selim Cinek7b9605b2017-01-19 17:36:00 -08004488 setTextViewColorPrimary(button, R.id.action0);
4489 } else if (mN.color != COLOR_DEFAULT) {
Adrian Roos487374f2017-01-11 15:48:14 -08004490 button.setTextColor(R.id.action0,
4491 ambient ? resolveAmbientColor() : resolveContrastColor());
Selim Cinek06e9e1f2016-07-08 17:14:16 -07004492 }
Adrian Roosfe84e1f2015-11-04 15:55:39 -08004493 }
Daniel Sandler96fd7c12012-03-30 16:37:36 -04004494 return button;
4495 }
4496
Joe Onoratocb109a02011-01-18 17:57:41 -08004497 /**
Selim Cinek981962e2016-07-20 20:41:58 -07004498 * Clears all color spans of a text
4499 * @param charSequence the input text
4500 * @return the same text but without color spans
4501 */
4502 private CharSequence clearColorSpans(CharSequence charSequence) {
4503 if (charSequence instanceof Spanned) {
4504 Spanned ss = (Spanned) charSequence;
4505 Object[] spans = ss.getSpans(0, ss.length(), Object.class);
4506 SpannableStringBuilder builder = new SpannableStringBuilder(ss.toString());
4507 for (Object span : spans) {
4508 Object resultSpan = span;
4509 if (resultSpan instanceof CharacterStyle) {
4510 resultSpan = ((CharacterStyle) span).getUnderlying();
4511 }
4512 if (resultSpan instanceof TextAppearanceSpan) {
4513 TextAppearanceSpan originalSpan = (TextAppearanceSpan) resultSpan;
4514 if (originalSpan.getTextColor() != null) {
4515 resultSpan = new TextAppearanceSpan(
4516 originalSpan.getFamily(),
4517 originalSpan.getTextStyle(),
4518 originalSpan.getTextSize(),
4519 null,
4520 originalSpan.getLinkTextColor());
4521 }
4522 } else if (resultSpan instanceof ForegroundColorSpan
4523 || (resultSpan instanceof BackgroundColorSpan)) {
4524 continue;
4525 } else {
4526 resultSpan = span;
4527 }
4528 builder.setSpan(resultSpan, ss.getSpanStart(span), ss.getSpanEnd(span),
4529 ss.getSpanFlags(span));
4530 }
4531 return builder;
4532 }
4533 return charSequence;
4534 }
4535
4536 /**
4537 * Ensures contrast on color spans against a background color. also returns the color of the
4538 * text if a span was found that spans over the whole text.
4539 *
4540 * @param charSequence the charSequence on which the spans are
4541 * @param background the background color to ensure the contrast against
4542 * @param outResultColor an array in which a color will be returned as the first element if
4543 * there exists a full length color span.
4544 * @return the contrasted charSequence
4545 */
4546 private CharSequence ensureColorSpanContrast(CharSequence charSequence, int background,
4547 ColorStateList[] outResultColor) {
4548 if (charSequence instanceof Spanned) {
4549 Spanned ss = (Spanned) charSequence;
4550 Object[] spans = ss.getSpans(0, ss.length(), Object.class);
4551 SpannableStringBuilder builder = new SpannableStringBuilder(ss.toString());
4552 for (Object span : spans) {
4553 Object resultSpan = span;
4554 int spanStart = ss.getSpanStart(span);
4555 int spanEnd = ss.getSpanEnd(span);
4556 boolean fullLength = (spanEnd - spanStart) == charSequence.length();
4557 if (resultSpan instanceof CharacterStyle) {
4558 resultSpan = ((CharacterStyle) span).getUnderlying();
4559 }
4560 if (resultSpan instanceof TextAppearanceSpan) {
4561 TextAppearanceSpan originalSpan = (TextAppearanceSpan) resultSpan;
4562 ColorStateList textColor = originalSpan.getTextColor();
4563 if (textColor != null) {
4564 int[] colors = textColor.getColors();
4565 int[] newColors = new int[colors.length];
4566 for (int i = 0; i < newColors.length; i++) {
4567 newColors[i] = NotificationColorUtil.ensureLargeTextContrast(
4568 colors[i], background);
4569 }
4570 textColor = new ColorStateList(textColor.getStates().clone(),
4571 newColors);
4572 resultSpan = new TextAppearanceSpan(
4573 originalSpan.getFamily(),
4574 originalSpan.getTextStyle(),
4575 originalSpan.getTextSize(),
4576 textColor,
4577 originalSpan.getLinkTextColor());
4578 if (fullLength) {
4579 outResultColor[0] = new ColorStateList(
4580 textColor.getStates().clone(), newColors);
4581 }
4582 }
4583 } else if (resultSpan instanceof ForegroundColorSpan) {
4584 ForegroundColorSpan originalSpan = (ForegroundColorSpan) resultSpan;
4585 int foregroundColor = originalSpan.getForegroundColor();
4586 foregroundColor = NotificationColorUtil.ensureLargeTextContrast(
4587 foregroundColor, background);
4588 resultSpan = new ForegroundColorSpan(foregroundColor);
4589 if (fullLength) {
4590 outResultColor[0] = ColorStateList.valueOf(foregroundColor);
4591 }
4592 } else {
4593 resultSpan = span;
4594 }
4595
4596 builder.setSpan(resultSpan, spanStart, spanEnd, ss.getSpanFlags(span));
4597 }
4598 return builder;
4599 }
4600 return charSequence;
4601 }
4602
4603 /**
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01004604 * @return Whether we are currently building a notification from a legacy (an app that
Alan Viverette3cb07a462014-06-06 14:19:53 -07004605 * doesn't create material notifications by itself) app.
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01004606 */
4607 private boolean isLegacy() {
Selim Cinek7b9605b2017-01-19 17:36:00 -08004608 if (!mIsLegacyInitialized) {
4609 mIsLegacy = mContext.getApplicationInfo().targetSdkVersion
4610 < Build.VERSION_CODES.LOLLIPOP;
4611 mIsLegacyInitialized = true;
4612 }
4613 return mIsLegacy;
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01004614 }
4615
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01004616 private CharSequence processLegacyText(CharSequence charSequence) {
Adrian Roos0bc3f6a2017-03-06 11:54:05 -08004617 return processLegacyText(charSequence, false /* ambient */);
4618 }
4619
4620 private CharSequence processLegacyText(CharSequence charSequence, boolean ambient) {
4621 boolean isAlreadyLightText = isLegacy() || textColorsNeedInversion();
4622 boolean wantLightText = ambient;
4623 if (isAlreadyLightText != wantLightText) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004624 return getColorUtil().invertCharSequenceColors(charSequence);
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01004625 } else {
4626 return charSequence;
4627 }
4628 }
4629
Dan Sandler26e81cf2014-05-06 10:01:27 -04004630 /**
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004631 * Apply any necessariy colors to the small icon
Dan Sandler26e81cf2014-05-06 10:01:27 -04004632 */
Adrian Roos487374f2017-01-11 15:48:14 -08004633 private void processSmallIconColor(Icon smallIcon, RemoteViews contentView,
4634 boolean ambient) {
Selim Cinekea4bef72015-12-02 15:51:10 -08004635 boolean colorable = !isLegacy() || getColorUtil().isGrayscaleIcon(mContext, smallIcon);
Selim Cinek99104832017-01-25 14:47:33 -08004636 int color = ambient ? resolveAmbientColor() : getPrimaryHighlightColor();
Selim Cinekea4bef72015-12-02 15:51:10 -08004637 if (colorable) {
Adrian Roos487374f2017-01-11 15:48:14 -08004638 contentView.setDrawableParameters(R.id.icon, false, -1, color,
Jorim Jaggi92df1f22014-12-16 19:44:41 +01004639 PorterDuff.Mode.SRC_ATOP, -1);
Selim Cinekea4bef72015-12-02 15:51:10 -08004640
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01004641 }
Selim Cinekea4bef72015-12-02 15:51:10 -08004642 contentView.setInt(R.id.notification_header, "setOriginalIconColor",
Adrian Roos487374f2017-01-11 15:48:14 -08004643 colorable ? color : NotificationHeaderView.NO_COLOR);
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01004644 }
4645
Dan Sandler26e81cf2014-05-06 10:01:27 -04004646 /**
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004647 * Make the largeIcon dark if it's a fake smallIcon (that is,
Dan Sandler26e81cf2014-05-06 10:01:27 -04004648 * if it's grayscale).
4649 */
4650 // TODO: also check bounds, transparency, that sort of thing.
Dan Sandlerd63f9322015-05-06 15:18:49 -04004651 private void processLargeLegacyIcon(Icon largeIcon, RemoteViews contentView) {
4652 if (largeIcon != null && isLegacy()
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004653 && getColorUtil().isGrayscaleIcon(mContext, largeIcon)) {
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004654 // resolve color will fall back to the default when legacy
Adrian Roos4ff3b122016-02-01 12:26:13 -08004655 contentView.setDrawableParameters(R.id.icon, false, -1, resolveContrastColor(),
Dan Sandler190d58d2014-05-15 09:33:39 -04004656 PorterDuff.Mode.SRC_ATOP, -1);
Jorim Jaggi92df1f22014-12-16 19:44:41 +01004657 }
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01004658 }
4659
Julia Reynolds10ee1fc2015-11-09 11:04:55 -05004660 private void sanitizeColor() {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004661 if (mN.color != COLOR_DEFAULT) {
4662 mN.color |= 0xFF000000; // no alpha for custom colors
Jorim Jaggi74419312014-06-10 20:57:21 +02004663 }
Jorim Jaggi74419312014-06-10 20:57:21 +02004664 }
4665
Adrian Roos4ff3b122016-02-01 12:26:13 -08004666 int resolveContrastColor() {
4667 if (mCachedContrastColorIsFor == mN.color && mCachedContrastColor != COLOR_INVALID) {
4668 return mCachedContrastColor;
Dan Sandler26e81cf2014-05-06 10:01:27 -04004669 }
Adrian Roos4ff3b122016-02-01 12:26:13 -08004670
Selim Cinekac5f0272017-05-02 16:05:41 -07004671 int color;
4672 int background = mBackgroundColorHint;
4673 if (mBackgroundColorHint == COLOR_INVALID) {
4674 background = mContext.getColor(
4675 com.android.internal.R.color.notification_material_background_color);
4676 }
4677 if (mN.color == COLOR_DEFAULT) {
4678 ensureColors();
4679 color = mSecondaryTextColor;
4680 } else {
4681 color = NotificationColorUtil.resolveContrastColor(mContext, mN.color,
4682 background);
4683 }
4684 if (Color.alpha(color) < 255) {
4685 // alpha doesn't go well for color filters, so let's blend it manually
4686 color = NotificationColorUtil.compositeColors(color, background);
4687 }
Adrian Roos4ff3b122016-02-01 12:26:13 -08004688 mCachedContrastColorIsFor = mN.color;
Selim Cinekac5f0272017-05-02 16:05:41 -07004689 return mCachedContrastColor = color;
Dan Sandler26e81cf2014-05-06 10:01:27 -04004690 }
4691
Adrian Roos487374f2017-01-11 15:48:14 -08004692 int resolveAmbientColor() {
4693 if (mCachedAmbientColorIsFor == mN.color && mCachedAmbientColorIsFor != COLOR_INVALID) {
4694 return mCachedAmbientColor;
4695 }
4696 final int contrasted = NotificationColorUtil.resolveAmbientColor(mContext, mN.color);
4697
4698 mCachedAmbientColorIsFor = mN.color;
4699 return mCachedAmbientColor = contrasted;
4700 }
4701
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01004702 /**
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004703 * Apply the unstyled operations and return a new {@link Notification} object.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04004704 * @hide
Joe Onoratocb109a02011-01-18 17:57:41 -08004705 */
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04004706 public Notification buildUnstyled() {
Daniel Sandlera0a938c2012-03-15 08:42:37 -04004707 if (mActions.size() > 0) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004708 mN.actions = new Action[mActions.size()];
4709 mActions.toArray(mN.actions);
Daniel Sandlera0a938c2012-03-15 08:42:37 -04004710 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004711 if (!mPersonList.isEmpty()) {
4712 mN.extras.putStringArray(EXTRA_PEOPLE,
4713 mPersonList.toArray(new String[mPersonList.size()]));
Dan Sandler0bf2ed82013-12-21 23:33:41 -06004714 }
Selim Cinek247fa012016-02-18 09:50:48 -08004715 if (mN.bigContentView != null || mN.contentView != null
4716 || mN.headsUpContentView != null) {
4717 mN.extras.putBoolean(EXTRA_CONTAINS_CUSTOM_VIEW, true);
4718 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004719 return mN;
Joe Onorato46439ce2010-11-19 13:56:21 -08004720 }
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004721
Julia Reynolds3b848122016-02-26 10:45:32 -05004722 /**
4723 * Creates a Builder from an existing notification so further changes can be made.
4724 * @param context The context for your application / activity.
4725 * @param n The notification to create a Builder from.
4726 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004727 public static Notification.Builder recoverBuilder(Context context, Notification n) {
Christoph Studer4600f9b2014-07-22 22:44:43 +02004728 // Re-create notification context so we can access app resources.
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004729 ApplicationInfo applicationInfo = n.extras.getParcelable(
4730 EXTRA_BUILDER_APPLICATION_INFO);
Christoph Studer4600f9b2014-07-22 22:44:43 +02004731 Context builderContext;
Julia Reynoldsda303542015-11-23 14:00:20 -05004732 if (applicationInfo != null) {
4733 try {
4734 builderContext = context.createApplicationContext(applicationInfo,
4735 Context.CONTEXT_RESTRICTED);
4736 } catch (NameNotFoundException e) {
4737 Log.e(TAG, "ApplicationInfo " + applicationInfo + " not found");
4738 builderContext = context; // try with our context
4739 }
4740 } else {
4741 builderContext = context; // try with given context
Christoph Studer4600f9b2014-07-22 22:44:43 +02004742 }
4743
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004744 return new Builder(builderContext, n);
Christoph Studer4600f9b2014-07-22 22:44:43 +02004745 }
4746
Daniel Sandlerf45564e2013-04-15 15:05:08 -04004747 /**
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004748 * @deprecated Use {@link #build()} instead.
4749 */
4750 @Deprecated
4751 public Notification getNotification() {
4752 return build();
4753 }
4754
4755 /**
4756 * Combine all of the options that have been set and return a new {@link Notification}
4757 * object.
4758 */
4759 public Notification build() {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004760 // first, add any extras from the calling code
4761 if (mUserExtras != null) {
4762 mN.extras = getAllExtras();
Jorim Jaggia0d58ae2015-06-03 11:48:13 -07004763 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004764
Selim Cinekb85f36fd2016-04-20 18:46:36 -07004765 mN.creationTime = System.currentTimeMillis();
4766
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004767 // lazy stuff from mContext; see comment in Builder(Context, Notification)
Julia Reynoldsda303542015-11-23 14:00:20 -05004768 Notification.addFieldsFromContext(mContext, mN);
Christoph Studer943aa672014-08-03 20:31:16 +02004769
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004770 buildUnstyled();
Daniel Sandlerf45564e2013-04-15 15:05:08 -04004771
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004772 if (mStyle != null) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004773 mStyle.buildStyled(mN);
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004774 }
Daniel Sandlerf45564e2013-04-15 15:05:08 -04004775
Adrian Roos5081c0d2016-02-26 16:04:19 -08004776 if (mContext.getApplicationInfo().targetSdkVersion < Build.VERSION_CODES.N
4777 && (mStyle == null || !mStyle.displayCustomViewInline())) {
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05004778 if (mN.contentView == null) {
Julia Reynolds3b848122016-02-26 10:45:32 -05004779 mN.contentView = createContentView();
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05004780 mN.extras.putInt(EXTRA_REBUILD_CONTENT_VIEW_ACTION_COUNT,
4781 mN.contentView.getSequenceNumber());
4782 }
4783 if (mN.bigContentView == null) {
Julia Reynolds3b848122016-02-26 10:45:32 -05004784 mN.bigContentView = createBigContentView();
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05004785 if (mN.bigContentView != null) {
4786 mN.extras.putInt(EXTRA_REBUILD_BIG_CONTENT_VIEW_ACTION_COUNT,
4787 mN.bigContentView.getSequenceNumber());
4788 }
4789 }
4790 if (mN.headsUpContentView == null) {
Julia Reynolds3b848122016-02-26 10:45:32 -05004791 mN.headsUpContentView = createHeadsUpContentView();
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05004792 if (mN.headsUpContentView != null) {
4793 mN.extras.putInt(EXTRA_REBUILD_HEADS_UP_CONTENT_VIEW_ACTION_COUNT,
4794 mN.headsUpContentView.getSequenceNumber());
4795 }
4796 }
4797 }
4798
Julia Reynolds4c0c2022016-02-02 15:11:59 -05004799 if ((mN.defaults & DEFAULT_LIGHTS) != 0) {
4800 mN.flags |= FLAG_SHOW_LIGHTS;
4801 }
4802
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004803 return mN;
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004804 }
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05004805
4806 /**
4807 * Apply this Builder to an existing {@link Notification} object.
4808 *
4809 * @hide
4810 */
4811 public Notification buildInto(Notification n) {
Daniel Sandler1a497d32013-04-18 14:52:45 -04004812 build().cloneInto(n, true);
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05004813 return n;
4814 }
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01004815
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05004816 /**
Adrian Roos184bfe022016-03-03 13:41:44 -08004817 * Removes RemoteViews that were created for compatibility from {@param n}, if they did not
4818 * change.
4819 *
4820 * @return {@param n}, if no stripping is needed, otherwise a stripped clone of {@param n}.
4821 *
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05004822 * @hide
4823 */
Adrian Roos184bfe022016-03-03 13:41:44 -08004824 public static Notification maybeCloneStrippedForDelivery(Notification n) {
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05004825 String templateClass = n.extras.getString(EXTRA_TEMPLATE);
Adrian Roos184bfe022016-03-03 13:41:44 -08004826
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05004827 // Only strip views for known Styles because we won't know how to
4828 // re-create them otherwise.
Adrian Roos184bfe022016-03-03 13:41:44 -08004829 if (!TextUtils.isEmpty(templateClass)
4830 && getNotificationStyleClass(templateClass) == null) {
4831 return n;
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05004832 }
Adrian Roos184bfe022016-03-03 13:41:44 -08004833
4834 // Only strip unmodified BuilderRemoteViews.
4835 boolean stripContentView = n.contentView instanceof BuilderRemoteViews &&
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05004836 n.extras.getInt(EXTRA_REBUILD_CONTENT_VIEW_ACTION_COUNT, -1) ==
Adrian Roos184bfe022016-03-03 13:41:44 -08004837 n.contentView.getSequenceNumber();
4838 boolean stripBigContentView = n.bigContentView instanceof BuilderRemoteViews &&
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05004839 n.extras.getInt(EXTRA_REBUILD_BIG_CONTENT_VIEW_ACTION_COUNT, -1) ==
Adrian Roos184bfe022016-03-03 13:41:44 -08004840 n.bigContentView.getSequenceNumber();
4841 boolean stripHeadsUpContentView = n.headsUpContentView instanceof BuilderRemoteViews &&
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05004842 n.extras.getInt(EXTRA_REBUILD_HEADS_UP_CONTENT_VIEW_ACTION_COUNT, -1) ==
Adrian Roos184bfe022016-03-03 13:41:44 -08004843 n.headsUpContentView.getSequenceNumber();
4844
4845 // Nothing to do here, no need to clone.
4846 if (!stripContentView && !stripBigContentView && !stripHeadsUpContentView) {
4847 return n;
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05004848 }
Adrian Roos184bfe022016-03-03 13:41:44 -08004849
4850 Notification clone = n.clone();
4851 if (stripContentView) {
4852 clone.contentView = null;
4853 clone.extras.remove(EXTRA_REBUILD_CONTENT_VIEW_ACTION_COUNT);
4854 }
4855 if (stripBigContentView) {
4856 clone.bigContentView = null;
4857 clone.extras.remove(EXTRA_REBUILD_BIG_CONTENT_VIEW_ACTION_COUNT);
4858 }
4859 if (stripHeadsUpContentView) {
4860 clone.headsUpContentView = null;
4861 clone.extras.remove(EXTRA_REBUILD_HEADS_UP_CONTENT_VIEW_ACTION_COUNT);
4862 }
4863 return clone;
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05004864 }
4865
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01004866 private int getBaseLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07004867 return R.layout.notification_template_material_base;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01004868 }
4869
4870 private int getBigBaseLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07004871 return R.layout.notification_template_material_big_base;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01004872 }
4873
4874 private int getBigPictureLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07004875 return R.layout.notification_template_material_big_picture;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01004876 }
4877
4878 private int getBigTextLayoutResource() {
Jorim Jaggi445d3c02014-08-19 22:33:42 +02004879 return R.layout.notification_template_material_big_text;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01004880 }
4881
4882 private int getInboxLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07004883 return R.layout.notification_template_material_inbox;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01004884 }
4885
Adrian Roosc1a80b02016-04-05 14:54:55 -07004886 private int getMessagingLayoutResource() {
4887 return R.layout.notification_template_material_messaging;
4888 }
4889
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01004890 private int getActionLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07004891 return R.layout.notification_material_action;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01004892 }
4893
Selim Cinek06e9e1f2016-07-08 17:14:16 -07004894 private int getEmphasizedActionLayoutResource() {
4895 return R.layout.notification_material_action_emphasized;
4896 }
4897
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01004898 private int getActionTombstoneLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07004899 return R.layout.notification_material_action_tombstone;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01004900 }
Selim Cinek7b9605b2017-01-19 17:36:00 -08004901
4902 private int getBackgroundColor() {
4903 if (isColorized()) {
Selim Cinek5fb73f82017-04-20 16:55:38 -07004904 return mBackgroundColor != COLOR_INVALID ? mBackgroundColor : mN.color;
Selim Cinek7b9605b2017-01-19 17:36:00 -08004905 } else {
Selim Cinekac5f0272017-05-02 16:05:41 -07004906 return mBackgroundColorHint != COLOR_INVALID ? mBackgroundColorHint
4907 : COLOR_DEFAULT;
Selim Cinek7b9605b2017-01-19 17:36:00 -08004908 }
4909 }
4910
4911 private boolean isColorized() {
4912 return mN.isColorized();
4913 }
Selim Cinek99104832017-01-25 14:47:33 -08004914
4915 private boolean textColorsNeedInversion() {
4916 if (mStyle == null || !MediaStyle.class.equals(mStyle.getClass())) {
4917 return false;
4918 }
4919 int targetSdkVersion = mContext.getApplicationInfo().targetSdkVersion;
4920 return targetSdkVersion > Build.VERSION_CODES.M
4921 && targetSdkVersion < Build.VERSION_CODES.O;
4922 }
Selim Cinek5fb73f82017-04-20 16:55:38 -07004923
4924 /**
4925 * Set a color palette to be used as the background and textColors
4926 *
4927 * @param backgroundColor the color to be used as the background
4928 * @param foregroundColor the color to be used as the foreground
4929 *
4930 * @hide
4931 */
4932 public void setColorPalette(int backgroundColor, int foregroundColor) {
4933 mBackgroundColor = backgroundColor;
4934 mForegroundColor = foregroundColor;
4935 mTextColorsAreForBackground = COLOR_INVALID;
4936 ensureColors();
4937 }
Selim Cinekac5f0272017-05-02 16:05:41 -07004938
4939 /**
4940 * Sets the background color for this notification to be a different one then the default.
4941 * This is mainly used to calculate contrast and won't necessarily be applied to the
4942 * background.
4943 *
4944 * @hide
4945 */
4946 public void setBackgroundColorHint(int backgroundColor) {
4947 mBackgroundColorHint = backgroundColor;
4948 }
Selim Cinek7b9605b2017-01-19 17:36:00 -08004949 }
4950
4951 /**
Selim Cinek22714f12017-04-13 16:23:53 -07004952 * @return whether this notification is a foreground service notification
Selim Cinek7b9605b2017-01-19 17:36:00 -08004953 */
Selim Cinek22714f12017-04-13 16:23:53 -07004954 private boolean isForegroundService() {
4955 return (flags & Notification.FLAG_FOREGROUND_SERVICE) != 0;
Selim Cinek7b9605b2017-01-19 17:36:00 -08004956 }
4957
4958 /**
Selim Cinek99104832017-01-25 14:47:33 -08004959 * @return whether this notification has a media session attached
4960 * @hide
4961 */
4962 public boolean hasMediaSession() {
4963 return extras.getParcelable(Notification.EXTRA_MEDIA_SESSION) != null;
4964 }
4965
4966 /**
Selim Cinek7d1009b2017-01-25 15:28:28 -08004967 * @return the style class of this notification
4968 * @hide
4969 */
4970 public Class<? extends Notification.Style> getNotificationStyle() {
4971 String templateClass = extras.getString(Notification.EXTRA_TEMPLATE);
4972
4973 if (!TextUtils.isEmpty(templateClass)) {
4974 return Notification.getNotificationStyleClass(templateClass);
4975 }
4976 return null;
4977 }
4978
4979 /**
Selim Cinek22714f12017-04-13 16:23:53 -07004980 * @return true if this notification is colorized.
Selim Cinek7b9605b2017-01-19 17:36:00 -08004981 *
4982 * @hide
4983 */
4984 public boolean isColorized() {
Selim Cinek5fb73f82017-04-20 16:55:38 -07004985 if (isColorizedMedia()) {
4986 return true;
4987 }
4988 return extras.getBoolean(EXTRA_COLORIZED) && isForegroundService();
4989 }
4990
4991 /**
4992 * @return true if this notification is colorized and it is a media notification
4993 *
4994 * @hide
4995 */
4996 public boolean isColorizedMedia() {
Selim Cinek99104832017-01-25 14:47:33 -08004997 Class<? extends Style> style = getNotificationStyle();
4998 if (MediaStyle.class.equals(style)) {
4999 Boolean colorized = (Boolean) extras.get(EXTRA_COLORIZED);
5000 if ((colorized == null || colorized) && hasMediaSession()) {
5001 return true;
5002 }
5003 } else if (DecoratedMediaCustomViewStyle.class.equals(style)) {
5004 if (extras.getBoolean(EXTRA_COLORIZED) && hasMediaSession()) {
5005 return true;
5006 }
5007 }
Selim Cinek5fb73f82017-04-20 16:55:38 -07005008 return false;
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005009 }
5010
Selim Cinek0847acd2017-04-24 19:48:29 -07005011
5012 /**
5013 * @return true if this is a media notification
5014 *
5015 * @hide
5016 */
5017 public boolean isMediaNotification() {
5018 Class<? extends Style> style = getNotificationStyle();
5019 if (MediaStyle.class.equals(style)) {
5020 return true;
5021 } else if (DecoratedMediaCustomViewStyle.class.equals(style)) {
5022 return true;
5023 }
5024 return false;
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005025 }
5026
Selim Cinek279fa862016-06-14 10:57:25 -07005027 private boolean hasLargeIcon() {
5028 return mLargeIcon != null || largeIcon != null;
5029 }
5030
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005031 /**
Selim Cinekc2c0b042016-05-18 17:13:46 -07005032 * @return true if the notification will show the time; false otherwise
Selim Cinekb85f36fd2016-04-20 18:46:36 -07005033 * @hide
5034 */
Selim Cinekc2c0b042016-05-18 17:13:46 -07005035 public boolean showsTime() {
Selim Cinekb85f36fd2016-04-20 18:46:36 -07005036 return when != 0 && extras.getBoolean(EXTRA_SHOW_WHEN);
5037 }
5038
5039 /**
Selim Cinekc2c0b042016-05-18 17:13:46 -07005040 * @return true if the notification will show a chronometer; false otherwise
5041 * @hide
5042 */
5043 public boolean showsChronometer() {
5044 return when != 0 && extras.getBoolean(EXTRA_SHOW_CHRONOMETER);
5045 }
5046
5047 /**
Julia Reynolds4a02afb2016-12-13 13:39:52 -05005048 * @hide
5049 */
5050 @SystemApi
5051 public static Class<? extends Style> getNotificationStyleClass(String templateClass) {
5052 Class<? extends Style>[] classes = new Class[] {
5053 BigTextStyle.class, BigPictureStyle.class, InboxStyle.class, MediaStyle.class,
5054 DecoratedCustomViewStyle.class, DecoratedMediaCustomViewStyle.class,
5055 MessagingStyle.class };
5056 for (Class<? extends Style> innerClass : classes) {
5057 if (templateClass.equals(innerClass.getName())) {
5058 return innerClass;
5059 }
5060 }
5061 return null;
5062 }
5063
5064 /**
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005065 * An object that can apply a rich notification style to a {@link Notification.Builder}
5066 * object.
5067 */
Griff Hazendfcb0802014-02-11 12:00:00 -08005068 public static abstract class Style {
Chris Wrend6297db2012-05-03 16:20:13 -04005069 private CharSequence mBigContentTitle;
Jorim Jaggi457a10d2014-09-08 16:18:23 +02005070
5071 /**
5072 * @hide
5073 */
5074 protected CharSequence mSummaryText = null;
5075
5076 /**
5077 * @hide
5078 */
5079 protected boolean mSummaryTextSet = false;
Chris Wrend6297db2012-05-03 16:20:13 -04005080
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005081 protected Builder mBuilder;
5082
Chris Wrend6297db2012-05-03 16:20:13 -04005083 /**
5084 * Overrides ContentTitle in the big form of the template.
5085 * This defaults to the value passed to setContentTitle().
5086 */
5087 protected void internalSetBigContentTitle(CharSequence title) {
5088 mBigContentTitle = title;
5089 }
5090
5091 /**
5092 * Set the first line of text after the detail section in the big form of the template.
5093 */
5094 protected void internalSetSummaryText(CharSequence cs) {
5095 mSummaryText = cs;
Daniel Sandler619738c2012-06-07 16:33:08 -04005096 mSummaryTextSet = true;
Chris Wrend6297db2012-05-03 16:20:13 -04005097 }
5098
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005099 public void setBuilder(Builder builder) {
5100 if (mBuilder != builder) {
5101 mBuilder = builder;
Daniel Sandlerc08dea22012-06-28 08:35:24 -07005102 if (mBuilder != null) {
5103 mBuilder.setStyle(this);
5104 }
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005105 }
5106 }
5107
Chris Wrend6297db2012-05-03 16:20:13 -04005108 protected void checkBuilder() {
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005109 if (mBuilder == null) {
5110 throw new IllegalArgumentException("Style requires a valid Builder object");
5111 }
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005112 }
Chris Wrend6297db2012-05-03 16:20:13 -04005113
5114 protected RemoteViews getStandardView(int layoutId) {
5115 checkBuilder();
5116
Christoph Studer4600f9b2014-07-22 22:44:43 +02005117 // Nasty.
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005118 CharSequence oldBuilderContentTitle =
5119 mBuilder.getAllExtras().getCharSequence(EXTRA_TITLE);
Chris Wrend6297db2012-05-03 16:20:13 -04005120 if (mBigContentTitle != null) {
5121 mBuilder.setContentTitle(mBigContentTitle);
5122 }
5123
Chris Wrend6297db2012-05-03 16:20:13 -04005124 RemoteViews contentView = mBuilder.applyStandardTemplateWithActions(layoutId);
5125
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005126 mBuilder.getAllExtras().putCharSequence(EXTRA_TITLE, oldBuilderContentTitle);
Christoph Studer4600f9b2014-07-22 22:44:43 +02005127
Chris Wrend6297db2012-05-03 16:20:13 -04005128 if (mBigContentTitle != null && mBigContentTitle.equals("")) {
5129 contentView.setViewVisibility(R.id.line1, View.GONE);
Chris Wren67dc9a02012-05-16 01:03:20 -04005130 } else {
5131 contentView.setViewVisibility(R.id.line1, View.VISIBLE);
Chris Wrend6297db2012-05-03 16:20:13 -04005132 }
5133
Chris Wrend6297db2012-05-03 16:20:13 -04005134 return contentView;
5135 }
5136
Daniel Sandlerf45564e2013-04-15 15:05:08 -04005137 /**
Selim Cinek7d1009b2017-01-25 15:28:28 -08005138 * Construct a Style-specific RemoteViews for the collapsed notification layout.
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005139 * The default implementation has nothing additional to add.
Selim Cinek7d1009b2017-01-25 15:28:28 -08005140 *
5141 * @param increasedHeight true if this layout be created with an increased height.
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005142 * @hide
5143 */
Selim Cinek7d1009b2017-01-25 15:28:28 -08005144 public RemoteViews makeContentView(boolean increasedHeight) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005145 return null;
5146 }
5147
5148 /**
5149 * Construct a Style-specific RemoteViews for the final big notification layout.
5150 * @hide
5151 */
5152 public RemoteViews makeBigContentView() {
5153 return null;
5154 }
5155
5156 /**
5157 * Construct a Style-specific RemoteViews for the final HUN layout.
Selim Cinek87ed69b2017-02-09 15:59:43 -08005158 *
5159 * @param increasedHeight true if this layout be created with an increased height.
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005160 * @hide
5161 */
Selim Cinek87ed69b2017-02-09 15:59:43 -08005162 public RemoteViews makeHeadsUpContentView(boolean increasedHeight) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005163 return null;
5164 }
5165
5166 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005167 * Apply any style-specific extras to this notification before shipping it out.
Daniel Sandlerf45564e2013-04-15 15:05:08 -04005168 * @hide
5169 */
5170 public void addExtras(Bundle extras) {
5171 if (mSummaryTextSet) {
5172 extras.putCharSequence(EXTRA_SUMMARY_TEXT, mSummaryText);
5173 }
5174 if (mBigContentTitle != null) {
5175 extras.putCharSequence(EXTRA_TITLE_BIG, mBigContentTitle);
5176 }
Chris Wren91ad5632013-06-05 15:05:57 -04005177 extras.putString(EXTRA_TEMPLATE, this.getClass().getName());
Daniel Sandlerf45564e2013-04-15 15:05:08 -04005178 }
5179
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04005180 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005181 * Reconstruct the internal state of this Style object from extras.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04005182 * @hide
5183 */
Christoph Studer4600f9b2014-07-22 22:44:43 +02005184 protected void restoreFromExtras(Bundle extras) {
5185 if (extras.containsKey(EXTRA_SUMMARY_TEXT)) {
5186 mSummaryText = extras.getCharSequence(EXTRA_SUMMARY_TEXT);
5187 mSummaryTextSet = true;
5188 }
5189 if (extras.containsKey(EXTRA_TITLE_BIG)) {
5190 mBigContentTitle = extras.getCharSequence(EXTRA_TITLE_BIG);
5191 }
5192 }
5193
5194
5195 /**
5196 * @hide
5197 */
5198 public Notification buildStyled(Notification wip) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005199 addExtras(wip.extras);
Christoph Studer4600f9b2014-07-22 22:44:43 +02005200 return wip;
5201 }
5202
Daniel Sandler0ec46202015-06-24 01:27:05 -04005203 /**
5204 * @hide
5205 */
Jorim Jaggia0d58ae2015-06-03 11:48:13 -07005206 public void purgeResources() {}
5207
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04005208 /**
5209 * Calls {@link android.app.Notification.Builder#build()} on the Builder this Style is
5210 * attached to.
5211 *
5212 * @return the fully constructed Notification.
5213 */
5214 public Notification build() {
5215 checkBuilder();
5216 return mBuilder.build();
5217 }
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02005218
5219 /**
5220 * @hide
5221 * @return true if the style positions the progress bar on the second line; false if the
5222 * style hides the progress bar
5223 */
5224 protected boolean hasProgress() {
5225 return true;
5226 }
Selim Cinek03d0d652015-11-13 13:18:09 -05005227
5228 /**
5229 * @hide
5230 * @return Whether we should put the summary be put into the notification header
5231 */
5232 public boolean hasSummaryInHeader() {
5233 return true;
5234 }
Selim Cinek593610c2016-02-16 18:42:57 -08005235
5236 /**
5237 * @hide
5238 * @return Whether custom content views are displayed inline in the style
5239 */
5240 public boolean displayCustomViewInline() {
5241 return false;
5242 }
Joe Onorato46439ce2010-11-19 13:56:21 -08005243 }
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005244
5245 /**
Daniel Sandler4dfbe832012-04-11 14:51:46 -04005246 * Helper class for generating large-format notifications that include a large image attachment.
Joe Malin8d40d042012-11-05 11:36:40 -08005247 *
Robert Ly91c5ce32014-06-08 15:37:00 -07005248 * Here's how you'd set the <code>BigPictureStyle</code> on a notification:
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005249 * <pre class="prettyprint">
Robert Ly91c5ce32014-06-08 15:37:00 -07005250 * Notification notif = new Notification.Builder(mContext)
5251 * .setContentTitle(&quot;New photo from &quot; + sender.toString())
5252 * .setContentText(subject)
5253 * .setSmallIcon(R.drawable.new_post)
5254 * .setLargeIcon(aBitmap)
5255 * .setStyle(new Notification.BigPictureStyle()
5256 * .bigPicture(aBigBitmap))
5257 * .build();
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005258 * </pre>
Joe Malin8d40d042012-11-05 11:36:40 -08005259 *
Daniel Sandler4dfbe832012-04-11 14:51:46 -04005260 * @see Notification#bigContentView
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005261 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005262 public static class BigPictureStyle extends Style {
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005263 private Bitmap mPicture;
Dan Sandlerd63f9322015-05-06 15:18:49 -04005264 private Icon mBigLargeIcon;
Chris Wren3745a3d2012-05-22 15:11:52 -04005265 private boolean mBigLargeIconSet = false;
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005266
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005267 public BigPictureStyle() {
5268 }
5269
Adrian Roosf5faf9d2016-05-23 13:56:15 -07005270 /**
5271 * @deprecated use {@code BigPictureStyle()}.
5272 */
5273 @Deprecated
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005274 public BigPictureStyle(Builder builder) {
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005275 setBuilder(builder);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005276 }
5277
Chris Wrend6297db2012-05-03 16:20:13 -04005278 /**
5279 * Overrides ContentTitle in the big form of the template.
5280 * This defaults to the value passed to setContentTitle().
5281 */
5282 public BigPictureStyle setBigContentTitle(CharSequence title) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04005283 internalSetBigContentTitle(safeCharSequence(title));
Chris Wrend6297db2012-05-03 16:20:13 -04005284 return this;
5285 }
5286
5287 /**
5288 * Set the first line of text after the detail section in the big form of the template.
5289 */
5290 public BigPictureStyle setSummaryText(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04005291 internalSetSummaryText(safeCharSequence(cs));
Chris Wrend6297db2012-05-03 16:20:13 -04005292 return this;
5293 }
5294
Chris Wren0bd664d2012-08-01 13:56:56 -04005295 /**
5296 * Provide the bitmap to be used as the payload for the BigPicture notification.
5297 */
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005298 public BigPictureStyle bigPicture(Bitmap b) {
5299 mPicture = b;
5300 return this;
5301 }
5302
Chris Wren3745a3d2012-05-22 15:11:52 -04005303 /**
Chris Wren3745a3d2012-05-22 15:11:52 -04005304 * Override the large icon when the big notification is shown.
5305 */
5306 public BigPictureStyle bigLargeIcon(Bitmap b) {
Dan Sandlerd63f9322015-05-06 15:18:49 -04005307 return bigLargeIcon(b != null ? Icon.createWithBitmap(b) : null);
5308 }
5309
5310 /**
5311 * Override the large icon when the big notification is shown.
5312 */
5313 public BigPictureStyle bigLargeIcon(Icon icon) {
Chris Wren3745a3d2012-05-22 15:11:52 -04005314 mBigLargeIconSet = true;
Dan Sandlerd63f9322015-05-06 15:18:49 -04005315 mBigLargeIcon = icon;
Chris Wren3745a3d2012-05-22 15:11:52 -04005316 return this;
5317 }
5318
Riley Andrews0394a0c2015-11-03 23:36:52 -08005319 /** @hide */
5320 public static final int MIN_ASHMEM_BITMAP_SIZE = 128 * (1 << 10);
5321
Daniel Sandler0ec46202015-06-24 01:27:05 -04005322 /**
5323 * @hide
5324 */
Jorim Jaggia0d58ae2015-06-03 11:48:13 -07005325 @Override
5326 public void purgeResources() {
5327 super.purgeResources();
Riley Andrews8cee7c12015-11-01 23:36:04 -08005328 if (mPicture != null &&
5329 mPicture.isMutable() &&
Riley Andrews0394a0c2015-11-03 23:36:52 -08005330 mPicture.getAllocationByteCount() >= MIN_ASHMEM_BITMAP_SIZE) {
Jorim Jaggia0d58ae2015-06-03 11:48:13 -07005331 mPicture = mPicture.createAshmemBitmap();
5332 }
5333 if (mBigLargeIcon != null) {
5334 mBigLargeIcon.convertToAshmem();
5335 }
5336 }
Christoph Studer5c510ee2014-12-15 16:32:27 +01005337
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005338 /**
5339 * @hide
5340 */
5341 public RemoteViews makeBigContentView() {
5342 // Replace mN.mLargeIcon with mBigLargeIcon if mBigLargeIconSet
Christoph Studer5c510ee2014-12-15 16:32:27 +01005343 // This covers the following cases:
5344 // 1. mBigLargeIconSet -> mBigLargeIcon (null or non-null) applies, overrides
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005345 // mN.mLargeIcon
5346 // 2. !mBigLargeIconSet -> mN.mLargeIcon applies
Dan Sandlerd63f9322015-05-06 15:18:49 -04005347 Icon oldLargeIcon = null;
Selim Cineke99acb22016-08-04 12:55:48 -07005348 Bitmap largeIconLegacy = null;
Christoph Studer5c510ee2014-12-15 16:32:27 +01005349 if (mBigLargeIconSet) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005350 oldLargeIcon = mBuilder.mN.mLargeIcon;
5351 mBuilder.mN.mLargeIcon = mBigLargeIcon;
Selim Cineke99acb22016-08-04 12:55:48 -07005352 // The legacy largeIcon might not allow us to clear the image, as it's taken in
5353 // replacement if the other one is null. Because we're restoring these legacy icons
5354 // for old listeners, this is in general non-null.
5355 largeIconLegacy = mBuilder.mN.largeIcon;
5356 mBuilder.mN.largeIcon = null;
Christoph Studer5c510ee2014-12-15 16:32:27 +01005357 }
5358
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005359 RemoteViews contentView = getStandardView(mBuilder.getBigPictureLayoutResource());
Selim Cinek03d0d652015-11-13 13:18:09 -05005360 if (mSummaryTextSet) {
5361 contentView.setTextViewText(R.id.text, mBuilder.processLegacyText(mSummaryText));
Selim Cinek7b9605b2017-01-19 17:36:00 -08005362 mBuilder.setTextViewColorSecondary(contentView, R.id.text);
Selim Cinekc848c3a2016-01-13 15:27:30 -08005363 contentView.setViewVisibility(R.id.text, View.VISIBLE);
Selim Cinek03d0d652015-11-13 13:18:09 -05005364 }
Selim Cinek279fa862016-06-14 10:57:25 -07005365 mBuilder.setContentMinHeight(contentView, mBuilder.mN.hasLargeIcon());
Selim Cinek53e64a42015-11-16 10:40:56 -08005366
Christoph Studer5c510ee2014-12-15 16:32:27 +01005367 if (mBigLargeIconSet) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005368 mBuilder.mN.mLargeIcon = oldLargeIcon;
Selim Cineke99acb22016-08-04 12:55:48 -07005369 mBuilder.mN.largeIcon = largeIconLegacy;
Christoph Studer5c510ee2014-12-15 16:32:27 +01005370 }
5371
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005372 contentView.setImageViewBitmap(R.id.big_picture, mPicture);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005373 return contentView;
5374 }
5375
Daniel Sandlerf45564e2013-04-15 15:05:08 -04005376 /**
5377 * @hide
5378 */
5379 public void addExtras(Bundle extras) {
5380 super.addExtras(extras);
5381
5382 if (mBigLargeIconSet) {
5383 extras.putParcelable(EXTRA_LARGE_ICON_BIG, mBigLargeIcon);
5384 }
5385 extras.putParcelable(EXTRA_PICTURE, mPicture);
5386 }
5387
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04005388 /**
5389 * @hide
5390 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005391 @Override
Christoph Studer4600f9b2014-07-22 22:44:43 +02005392 protected void restoreFromExtras(Bundle extras) {
5393 super.restoreFromExtras(extras);
5394
5395 if (extras.containsKey(EXTRA_LARGE_ICON_BIG)) {
Christoph Studer5c510ee2014-12-15 16:32:27 +01005396 mBigLargeIconSet = true;
Christoph Studer4600f9b2014-07-22 22:44:43 +02005397 mBigLargeIcon = extras.getParcelable(EXTRA_LARGE_ICON_BIG);
Chris Wren3745a3d2012-05-22 15:11:52 -04005398 }
Christoph Studer4600f9b2014-07-22 22:44:43 +02005399 mPicture = extras.getParcelable(EXTRA_PICTURE);
5400 }
Selim Cinek03d0d652015-11-13 13:18:09 -05005401
5402 /**
5403 * @hide
5404 */
5405 @Override
5406 public boolean hasSummaryInHeader() {
5407 return false;
5408 }
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005409 }
5410
5411 /**
Daniel Sandler4dfbe832012-04-11 14:51:46 -04005412 * Helper class for generating large-format notifications that include a lot of text.
Joe Malin8d40d042012-11-05 11:36:40 -08005413 *
Robert Ly91c5ce32014-06-08 15:37:00 -07005414 * Here's how you'd set the <code>BigTextStyle</code> on a notification:
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005415 * <pre class="prettyprint">
Robert Ly91c5ce32014-06-08 15:37:00 -07005416 * Notification notif = new Notification.Builder(mContext)
5417 * .setContentTitle(&quot;New mail from &quot; + sender.toString())
5418 * .setContentText(subject)
5419 * .setSmallIcon(R.drawable.new_mail)
5420 * .setLargeIcon(aBitmap)
5421 * .setStyle(new Notification.BigTextStyle()
5422 * .bigText(aVeryLongString))
5423 * .build();
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005424 * </pre>
Joe Malin8d40d042012-11-05 11:36:40 -08005425 *
Daniel Sandler4dfbe832012-04-11 14:51:46 -04005426 * @see Notification#bigContentView
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005427 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005428 public static class BigTextStyle extends Style {
Jorim Jaggi457a10d2014-09-08 16:18:23 +02005429
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005430 private CharSequence mBigText;
5431
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005432 public BigTextStyle() {
5433 }
5434
Adrian Roosf5faf9d2016-05-23 13:56:15 -07005435 /**
5436 * @deprecated use {@code BigTextStyle()}.
5437 */
5438 @Deprecated
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005439 public BigTextStyle(Builder builder) {
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005440 setBuilder(builder);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005441 }
5442
Chris Wrend6297db2012-05-03 16:20:13 -04005443 /**
5444 * Overrides ContentTitle in the big form of the template.
5445 * This defaults to the value passed to setContentTitle().
5446 */
5447 public BigTextStyle setBigContentTitle(CharSequence title) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04005448 internalSetBigContentTitle(safeCharSequence(title));
Chris Wrend6297db2012-05-03 16:20:13 -04005449 return this;
5450 }
5451
5452 /**
5453 * Set the first line of text after the detail section in the big form of the template.
5454 */
5455 public BigTextStyle setSummaryText(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04005456 internalSetSummaryText(safeCharSequence(cs));
Chris Wrend6297db2012-05-03 16:20:13 -04005457 return this;
5458 }
5459
Chris Wren0bd664d2012-08-01 13:56:56 -04005460 /**
5461 * Provide the longer text to be displayed in the big form of the
5462 * template in place of the content text.
5463 */
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005464 public BigTextStyle bigText(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04005465 mBigText = safeCharSequence(cs);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005466 return this;
5467 }
5468
Daniel Sandlerf45564e2013-04-15 15:05:08 -04005469 /**
5470 * @hide
5471 */
5472 public void addExtras(Bundle extras) {
5473 super.addExtras(extras);
5474
Christoph Studer4600f9b2014-07-22 22:44:43 +02005475 extras.putCharSequence(EXTRA_BIG_TEXT, mBigText);
5476 }
5477
5478 /**
5479 * @hide
5480 */
5481 @Override
5482 protected void restoreFromExtras(Bundle extras) {
5483 super.restoreFromExtras(extras);
5484
5485 mBigText = extras.getCharSequence(EXTRA_BIG_TEXT);
Daniel Sandlerf45564e2013-04-15 15:05:08 -04005486 }
5487
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005488 /**
Selim Cinek7d1009b2017-01-25 15:28:28 -08005489 * @param increasedHeight true if this layout be created with an increased height.
5490 *
5491 * @hide
5492 */
5493 @Override
5494 public RemoteViews makeContentView(boolean increasedHeight) {
5495 if (increasedHeight) {
5496 ArrayList<Action> actions = mBuilder.mActions;
5497 mBuilder.mActions = new ArrayList<>();
5498 RemoteViews remoteViews = makeBigContentView();
5499 mBuilder.mActions = actions;
5500 return remoteViews;
5501 }
5502 return super.makeContentView(increasedHeight);
5503 }
5504
5505 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005506 * @hide
5507 */
Selim Cinek87ed69b2017-02-09 15:59:43 -08005508 @Override
5509 public RemoteViews makeHeadsUpContentView(boolean increasedHeight) {
5510 if (increasedHeight && mBuilder.mActions.size() > 0) {
5511 return makeBigContentView();
5512 }
5513 return super.makeHeadsUpContentView(increasedHeight);
5514 }
5515
5516 /**
5517 * @hide
5518 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005519 public RemoteViews makeBigContentView() {
Christoph Studer4600f9b2014-07-22 22:44:43 +02005520
5521 // Nasty
Selim Cinek75998782016-04-26 10:39:17 -07005522 CharSequence text = mBuilder.getAllExtras().getCharSequence(EXTRA_TEXT);
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005523 mBuilder.getAllExtras().putCharSequence(EXTRA_TEXT, null);
Daniel Sandler916ad912012-06-13 12:17:07 -04005524
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005525 RemoteViews contentView = getStandardView(mBuilder.getBigTextLayoutResource());
Joe Malin8d40d042012-11-05 11:36:40 -08005526
Selim Cinek75998782016-04-26 10:39:17 -07005527 mBuilder.getAllExtras().putCharSequence(EXTRA_TEXT, text);
Christoph Studer4600f9b2014-07-22 22:44:43 +02005528
Selim Cinek3a2c4b92015-12-17 17:01:17 -08005529 CharSequence bigTextText = mBuilder.processLegacyText(mBigText);
Selim Cinek75998782016-04-26 10:39:17 -07005530 if (TextUtils.isEmpty(bigTextText)) {
5531 // In case the bigtext is null / empty fall back to the normal text to avoid a weird
5532 // experience
5533 bigTextText = mBuilder.processLegacyText(text);
5534 }
Adrian Roosb1f427c2016-05-26 12:27:15 -07005535 applyBigTextContentView(mBuilder, contentView, bigTextText);
Selim Cinek4fb12d32015-11-19 18:10:48 -08005536
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005537 return contentView;
5538 }
5539
Adrian Roosb1f427c2016-05-26 12:27:15 -07005540 static void applyBigTextContentView(Builder builder,
5541 RemoteViews contentView, CharSequence bigTextText) {
5542 contentView.setTextViewText(R.id.big_text, bigTextText);
Selim Cinek7b9605b2017-01-19 17:36:00 -08005543 builder.setTextViewColorSecondary(contentView, R.id.big_text);
Adrian Roosb1f427c2016-05-26 12:27:15 -07005544 contentView.setViewVisibility(R.id.big_text,
5545 TextUtils.isEmpty(bigTextText) ? View.GONE : View.VISIBLE);
Selim Cinek279fa862016-06-14 10:57:25 -07005546 contentView.setBoolean(R.id.big_text, "setHasImage", builder.mN.hasLargeIcon());
Adrian Roosb1f427c2016-05-26 12:27:15 -07005547 }
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005548 }
Daniel Sandler879c5e02012-04-17 16:46:51 -04005549
5550 /**
Alex Hillsfc737de2016-03-23 17:33:02 -04005551 * Helper class for generating large-format notifications that include multiple back-and-forth
5552 * messages of varying types between any number of people.
5553 *
5554 * <br>
5555 * If the platform does not provide large-format notifications, this method has no effect. The
5556 * user will always see the normal notification view.
5557 * <br>
5558 * This class is a "rebuilder": It attaches to a Builder object and modifies its behavior, like
5559 * so:
5560 * <pre class="prettyprint">
5561 *
5562 * Notification noti = new Notification.Builder()
5563 * .setContentTitle(&quot;2 new messages wtih &quot; + sender.toString())
5564 * .setContentText(subject)
5565 * .setSmallIcon(R.drawable.new_message)
5566 * .setLargeIcon(aBitmap)
5567 * .setStyle(new Notification.MessagingStyle(resources.getString(R.string.reply_name))
5568 * .addMessage(messages[0].getText(), messages[0].getTime(), messages[0].getSender())
5569 * .addMessage(messages[1].getText(), messages[1].getTime(), messages[1].getSender()))
5570 * .build();
5571 * </pre>
5572 */
5573 public static class MessagingStyle extends Style {
5574
5575 /**
5576 * The maximum number of messages that will be retained in the Notification itself (the
5577 * number displayed is up to the platform).
5578 */
5579 public static final int MAXIMUM_RETAINED_MESSAGES = 25;
5580
5581 CharSequence mUserDisplayName;
5582 CharSequence mConversationTitle;
Alex Hillsd9b04d92016-04-11 16:38:16 -04005583 List<Message> mMessages = new ArrayList<>();
Adrian Roos437cd562017-01-18 15:47:03 -08005584 List<Message> mHistoricMessages = new ArrayList<>();
Alex Hillsfc737de2016-03-23 17:33:02 -04005585
5586 MessagingStyle() {
5587 }
5588
5589 /**
Alex Hillsfd590442016-10-07 09:52:44 -04005590 * @param userDisplayName Required - the name to be displayed for any replies sent by the
5591 * user before the posting app reposts the notification with those messages after they've
5592 * been actually sent and in previous messages sent by the user added in
Alex Hillsfc737de2016-03-23 17:33:02 -04005593 * {@link #addMessage(Notification.MessagingStyle.Message)}
5594 */
Alex Hillsfd590442016-10-07 09:52:44 -04005595 public MessagingStyle(@NonNull CharSequence userDisplayName) {
Alex Hillsfc737de2016-03-23 17:33:02 -04005596 mUserDisplayName = userDisplayName;
5597 }
5598
5599 /**
5600 * Returns the name to be displayed for any replies sent by the user
5601 */
5602 public CharSequence getUserDisplayName() {
5603 return mUserDisplayName;
5604 }
5605
5606 /**
Alex Hillsfc737de2016-03-23 17:33:02 -04005607 * Sets the title to be displayed on this conversation. This should only be used for
5608 * group messaging and left unset for one-on-one conversations.
5609 * @param conversationTitle
5610 * @return this object for method chaining.
5611 */
5612 public MessagingStyle setConversationTitle(CharSequence conversationTitle) {
5613 mConversationTitle = conversationTitle;
5614 return this;
5615 }
5616
5617 /**
5618 * Return the title to be displayed on this conversation. Can be <code>null</code> and
5619 * should be for one-on-one conversations
5620 */
5621 public CharSequence getConversationTitle() {
5622 return mConversationTitle;
5623 }
5624
5625 /**
5626 * Adds a message for display by this notification. Convenience call for a simple
5627 * {@link Message} in {@link #addMessage(Notification.MessagingStyle.Message)}.
5628 * @param text A {@link CharSequence} to be displayed as the message content
5629 * @param timestamp Time at which the message arrived
5630 * @param sender A {@link CharSequence} to be used for displaying the name of the
5631 * sender. Should be <code>null</code> for messages by the current user, in which case
5632 * the platform will insert {@link #getUserDisplayName()}.
5633 * Should be unique amongst all individuals in the conversation, and should be
5634 * consistent during re-posts of the notification.
5635 *
5636 * @see Message#Message(CharSequence, long, CharSequence)
5637 *
5638 * @return this object for method chaining
5639 */
5640 public MessagingStyle addMessage(CharSequence text, long timestamp, CharSequence sender) {
Adrian Roos437cd562017-01-18 15:47:03 -08005641 return addMessage(new Message(text, timestamp, sender));
Alex Hillsfc737de2016-03-23 17:33:02 -04005642 }
5643
5644 /**
5645 * Adds a {@link Message} for display in this notification.
Adrian Roos437cd562017-01-18 15:47:03 -08005646 *
5647 * <p>The messages should be added in chronologic order, i.e. the oldest first,
5648 * the newest last.
5649 *
Alex Hillsfc737de2016-03-23 17:33:02 -04005650 * @param message The {@link Message} to be displayed
5651 * @return this object for method chaining
5652 */
5653 public MessagingStyle addMessage(Message message) {
5654 mMessages.add(message);
5655 if (mMessages.size() > MAXIMUM_RETAINED_MESSAGES) {
5656 mMessages.remove(0);
5657 }
5658 return this;
5659 }
5660
5661 /**
Adrian Roos437cd562017-01-18 15:47:03 -08005662 * Adds a {@link Message} for historic context in this notification.
5663 *
5664 * <p>Messages should be added as historic if they are not the main subject of the
5665 * notification but may give context to a conversation. The system may choose to present
5666 * them only when relevant, e.g. when replying to a message through a {@link RemoteInput}.
5667 *
5668 * <p>The messages should be added in chronologic order, i.e. the oldest first,
5669 * the newest last.
5670 *
5671 * @param message The historic {@link Message} to be added
5672 * @return this object for method chaining
5673 */
5674 public MessagingStyle addHistoricMessage(Message message) {
5675 mHistoricMessages.add(message);
5676 if (mHistoricMessages.size() > MAXIMUM_RETAINED_MESSAGES) {
5677 mHistoricMessages.remove(0);
5678 }
5679 return this;
5680 }
5681
5682 /**
Alex Hillsfc737de2016-03-23 17:33:02 -04005683 * Gets the list of {@code Message} objects that represent the notification
5684 */
5685 public List<Message> getMessages() {
5686 return mMessages;
5687 }
5688
5689 /**
Adrian Roos437cd562017-01-18 15:47:03 -08005690 * Gets the list of historic {@code Message}s in the notification.
5691 */
5692 public List<Message> getHistoricMessages() {
5693 return mHistoricMessages;
5694 }
5695
5696 /**
Alex Hillsfc737de2016-03-23 17:33:02 -04005697 * @hide
5698 */
5699 @Override
5700 public void addExtras(Bundle extras) {
5701 super.addExtras(extras);
5702 if (mUserDisplayName != null) {
5703 extras.putCharSequence(EXTRA_SELF_DISPLAY_NAME, mUserDisplayName);
5704 }
5705 if (mConversationTitle != null) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04005706 extras.putCharSequence(EXTRA_CONVERSATION_TITLE, mConversationTitle);
Alex Hillsfc737de2016-03-23 17:33:02 -04005707 }
Alex Hillsd9b04d92016-04-11 16:38:16 -04005708 if (!mMessages.isEmpty()) { extras.putParcelableArray(EXTRA_MESSAGES,
5709 Message.getBundleArrayForMessages(mMessages));
Alex Hillsfc737de2016-03-23 17:33:02 -04005710 }
Adrian Roos437cd562017-01-18 15:47:03 -08005711 if (!mHistoricMessages.isEmpty()) { extras.putParcelableArray(EXTRA_HISTORIC_MESSAGES,
5712 Message.getBundleArrayForMessages(mHistoricMessages));
5713 }
Adrian Roos33fbd2c2016-05-27 15:35:28 -07005714
5715 fixTitleAndTextExtras(extras);
5716 }
5717
5718 private void fixTitleAndTextExtras(Bundle extras) {
5719 Message m = findLatestIncomingMessage();
5720 CharSequence text = (m == null) ? null : m.mText;
5721 CharSequence sender = m == null ? null
5722 : TextUtils.isEmpty(m.mSender) ? mUserDisplayName : m.mSender;
5723 CharSequence title;
5724 if (!TextUtils.isEmpty(mConversationTitle)) {
5725 if (!TextUtils.isEmpty(sender)) {
5726 BidiFormatter bidi = BidiFormatter.getInstance();
5727 title = mBuilder.mContext.getString(
5728 com.android.internal.R.string.notification_messaging_title_template,
5729 bidi.unicodeWrap(mConversationTitle), bidi.unicodeWrap(m.mSender));
5730 } else {
5731 title = mConversationTitle;
5732 }
5733 } else {
5734 title = sender;
5735 }
5736
5737 if (title != null) {
5738 extras.putCharSequence(EXTRA_TITLE, title);
5739 }
5740 if (text != null) {
5741 extras.putCharSequence(EXTRA_TEXT, text);
5742 }
Alex Hillsfc737de2016-03-23 17:33:02 -04005743 }
5744
5745 /**
5746 * @hide
5747 */
5748 @Override
5749 protected void restoreFromExtras(Bundle extras) {
5750 super.restoreFromExtras(extras);
5751
5752 mMessages.clear();
Adrian Roos437cd562017-01-18 15:47:03 -08005753 mHistoricMessages.clear();
Adrian Roos96b7e202016-05-17 13:50:38 -07005754 mUserDisplayName = extras.getCharSequence(EXTRA_SELF_DISPLAY_NAME);
5755 mConversationTitle = extras.getCharSequence(EXTRA_CONVERSATION_TITLE);
Adrian Roos437cd562017-01-18 15:47:03 -08005756 Parcelable[] messages = extras.getParcelableArray(EXTRA_MESSAGES);
5757 if (messages != null && messages instanceof Parcelable[]) {
5758 mMessages = Message.getMessagesFromBundleArray(messages);
5759 }
5760 Parcelable[] histMessages = extras.getParcelableArray(EXTRA_HISTORIC_MESSAGES);
5761 if (histMessages != null && histMessages instanceof Parcelable[]) {
5762 mHistoricMessages = Message.getMessagesFromBundleArray(histMessages);
Alex Hillsfc737de2016-03-23 17:33:02 -04005763 }
5764 }
5765
5766 /**
5767 * @hide
5768 */
Adrian Roosc1a80b02016-04-05 14:54:55 -07005769 @Override
Selim Cinek7d1009b2017-01-25 15:28:28 -08005770 public RemoteViews makeContentView(boolean increasedHeight) {
5771 if (!increasedHeight) {
5772 Message m = findLatestIncomingMessage();
5773 CharSequence title = mConversationTitle != null
5774 ? mConversationTitle
5775 : (m == null) ? null : m.mSender;
5776 CharSequence text = (m == null)
5777 ? null
5778 : mConversationTitle != null ? makeMessageLine(m, mBuilder) : m.mText;
Alex Hillsfc737de2016-03-23 17:33:02 -04005779
Selim Cinek7d1009b2017-01-25 15:28:28 -08005780 return mBuilder.applyStandardTemplate(mBuilder.getBaseLayoutResource(),
5781 mBuilder.mParams.reset().hasProgress(false).title(title).text(text));
5782 } else {
5783 ArrayList<Action> actions = mBuilder.mActions;
5784 mBuilder.mActions = new ArrayList<>();
5785 RemoteViews remoteViews = makeBigContentView();
5786 mBuilder.mActions = actions;
5787 return remoteViews;
5788 }
Adrian Roosc1a80b02016-04-05 14:54:55 -07005789 }
5790
5791 private Message findLatestIncomingMessage() {
5792 for (int i = mMessages.size() - 1; i >= 0; i--) {
5793 Message m = mMessages.get(i);
5794 // Incoming messages have a non-empty sender.
5795 if (!TextUtils.isEmpty(m.mSender)) {
5796 return m;
5797 }
5798 }
Adrian Roos33fbd2c2016-05-27 15:35:28 -07005799 if (!mMessages.isEmpty()) {
5800 // No incoming messages, fall back to outgoing message
5801 return mMessages.get(mMessages.size() - 1);
5802 }
Adrian Roosc1a80b02016-04-05 14:54:55 -07005803 return null;
5804 }
5805
5806 /**
5807 * @hide
5808 */
5809 @Override
5810 public RemoteViews makeBigContentView() {
5811 CharSequence title = !TextUtils.isEmpty(super.mBigContentTitle)
5812 ? super.mBigContentTitle
5813 : mConversationTitle;
5814 boolean hasTitle = !TextUtils.isEmpty(title);
5815
Adrian Roosfeafa052016-06-01 17:09:45 -07005816 if (mMessages.size() == 1) {
5817 // Special case for a single message: Use the big text style
5818 // so the collapsed and expanded versions match nicely.
5819 CharSequence bigTitle;
5820 CharSequence text;
5821 if (hasTitle) {
5822 bigTitle = title;
Selim Cinek7b9605b2017-01-19 17:36:00 -08005823 text = makeMessageLine(mMessages.get(0), mBuilder);
Adrian Roosfeafa052016-06-01 17:09:45 -07005824 } else {
5825 bigTitle = mMessages.get(0).mSender;
5826 text = mMessages.get(0).mText;
5827 }
Adrian Roosb1f427c2016-05-26 12:27:15 -07005828 RemoteViews contentView = mBuilder.applyStandardTemplateWithActions(
5829 mBuilder.getBigTextLayoutResource(),
Adrian Roos70d7aa32017-01-11 15:39:06 -08005830 mBuilder.mParams.reset().hasProgress(false).title(bigTitle).text(null));
Adrian Roosb1f427c2016-05-26 12:27:15 -07005831 BigTextStyle.applyBigTextContentView(mBuilder, contentView, text);
5832 return contentView;
5833 }
5834
Adrian Roos48d746a2016-04-12 14:57:28 -07005835 RemoteViews contentView = mBuilder.applyStandardTemplateWithActions(
Adrian Roosc1a80b02016-04-05 14:54:55 -07005836 mBuilder.getMessagingLayoutResource(),
Adrian Roos70d7aa32017-01-11 15:39:06 -08005837 mBuilder.mParams.reset().hasProgress(false).title(title).text(null));
Adrian Roosc1a80b02016-04-05 14:54:55 -07005838
5839 int[] rowIds = {R.id.inbox_text0, R.id.inbox_text1, R.id.inbox_text2, R.id.inbox_text3,
5840 R.id.inbox_text4, R.id.inbox_text5, R.id.inbox_text6};
5841
5842 // Make sure all rows are gone in case we reuse a view.
5843 for (int rowId : rowIds) {
5844 contentView.setViewVisibility(rowId, View.GONE);
5845 }
5846
5847 int i=0;
Adrian Roos2d5dbba2016-06-08 17:11:53 -07005848 contentView.setViewLayoutMarginBottomDimen(R.id.line1,
5849 hasTitle ? R.dimen.notification_messaging_spacing : 0);
Adrian Roosc1a80b02016-04-05 14:54:55 -07005850 contentView.setInt(R.id.notification_messaging, "setNumIndentLines",
Selim Cinek279fa862016-06-14 10:57:25 -07005851 !mBuilder.mN.hasLargeIcon() ? 0 : (hasTitle ? 1 : 2));
Adrian Roosc1a80b02016-04-05 14:54:55 -07005852
Adrian Roosfeafa052016-06-01 17:09:45 -07005853 int contractedChildId = View.NO_ID;
5854 Message contractedMessage = findLatestIncomingMessage();
Adrian Roos437cd562017-01-18 15:47:03 -08005855 int firstHistoricMessage = Math.max(0, mHistoricMessages.size()
5856 - (rowIds.length - mMessages.size()));
5857 while (firstHistoricMessage + i < mHistoricMessages.size() && i < rowIds.length) {
5858 Message m = mHistoricMessages.get(firstHistoricMessage + i);
5859 int rowId = rowIds[i];
5860
Selim Cinek7b9605b2017-01-19 17:36:00 -08005861 contentView.setTextViewText(rowId, makeMessageLine(m, mBuilder));
Adrian Roos437cd562017-01-18 15:47:03 -08005862
5863 if (contractedMessage == m) {
5864 contractedChildId = rowId;
5865 }
5866
5867 i++;
5868 }
5869
Adrian Roosc1a80b02016-04-05 14:54:55 -07005870 int firstMessage = Math.max(0, mMessages.size() - rowIds.length);
5871 while (firstMessage + i < mMessages.size() && i < rowIds.length) {
5872 Message m = mMessages.get(firstMessage + i);
5873 int rowId = rowIds[i];
5874
5875 contentView.setViewVisibility(rowId, View.VISIBLE);
Selim Cinek7b9605b2017-01-19 17:36:00 -08005876 contentView.setTextViewText(rowId, makeMessageLine(m, mBuilder));
5877 mBuilder.setTextViewColorSecondary(contentView, rowId);
Adrian Roosc1a80b02016-04-05 14:54:55 -07005878
Adrian Roosfeafa052016-06-01 17:09:45 -07005879 if (contractedMessage == m) {
5880 contractedChildId = rowId;
5881 }
5882
Adrian Roosc1a80b02016-04-05 14:54:55 -07005883 i++;
5884 }
Adrian Roos437cd562017-01-18 15:47:03 -08005885 // Clear the remaining views for reapply. Ensures that historic message views can
5886 // reliably be identified as being GONE and having non-null text.
5887 while (i < rowIds.length) {
5888 int rowId = rowIds[i];
5889 contentView.setTextViewText(rowId, null);
5890 i++;
5891 }
5892
Adrian Roosfeafa052016-06-01 17:09:45 -07005893 // Record this here to allow transformation between the contracted and expanded views.
5894 contentView.setInt(R.id.notification_messaging, "setContractedChildId",
5895 contractedChildId);
Alex Hillsfc737de2016-03-23 17:33:02 -04005896 return contentView;
5897 }
5898
Selim Cinek7b9605b2017-01-19 17:36:00 -08005899 private CharSequence makeMessageLine(Message m, Builder builder) {
Adrian Roosc1a80b02016-04-05 14:54:55 -07005900 BidiFormatter bidi = BidiFormatter.getInstance();
5901 SpannableStringBuilder sb = new SpannableStringBuilder();
Selim Cinek7b9605b2017-01-19 17:36:00 -08005902 boolean colorize = builder.isColorized();
Adrian Roosc1a80b02016-04-05 14:54:55 -07005903 if (TextUtils.isEmpty(m.mSender)) {
5904 CharSequence replyName = mUserDisplayName == null ? "" : mUserDisplayName;
5905 sb.append(bidi.unicodeWrap(replyName),
Selim Cinek7b9605b2017-01-19 17:36:00 -08005906 makeFontColorSpan(colorize
5907 ? builder.getPrimaryTextColor()
5908 : mBuilder.resolveContrastColor()),
Adrian Roosc1a80b02016-04-05 14:54:55 -07005909 0 /* flags */);
5910 } else {
5911 sb.append(bidi.unicodeWrap(m.mSender),
Selim Cinek7b9605b2017-01-19 17:36:00 -08005912 makeFontColorSpan(colorize
5913 ? builder.getPrimaryTextColor()
5914 : Color.BLACK),
Adrian Roosc1a80b02016-04-05 14:54:55 -07005915 0 /* flags */);
5916 }
5917 CharSequence text = m.mText == null ? "" : m.mText;
5918 sb.append(" ").append(bidi.unicodeWrap(text));
5919 return sb;
5920 }
5921
Adrian Roosdedd1df2016-04-26 16:38:47 -07005922 /**
5923 * @hide
5924 */
5925 @Override
Selim Cinek87ed69b2017-02-09 15:59:43 -08005926 public RemoteViews makeHeadsUpContentView(boolean increasedHeight) {
5927 if (increasedHeight) {
5928 return makeBigContentView();
5929 }
Adrian Roosdedd1df2016-04-26 16:38:47 -07005930 Message m = findLatestIncomingMessage();
5931 CharSequence title = mConversationTitle != null
5932 ? mConversationTitle
5933 : (m == null) ? null : m.mSender;
5934 CharSequence text = (m == null)
5935 ? null
Selim Cinek7b9605b2017-01-19 17:36:00 -08005936 : mConversationTitle != null ? makeMessageLine(m, mBuilder) : m.mText;
Adrian Roosdedd1df2016-04-26 16:38:47 -07005937
5938 return mBuilder.applyStandardTemplateWithActions(mBuilder.getBigBaseLayoutResource(),
Adrian Roos70d7aa32017-01-11 15:39:06 -08005939 mBuilder.mParams.reset().hasProgress(false).title(title).text(text));
Adrian Roosdedd1df2016-04-26 16:38:47 -07005940 }
5941
Adrian Roosc1a80b02016-04-05 14:54:55 -07005942 private static TextAppearanceSpan makeFontColorSpan(int color) {
5943 return new TextAppearanceSpan(null, 0, 0,
5944 ColorStateList.valueOf(color), null);
5945 }
5946
Alex Hillsd9b04d92016-04-11 16:38:16 -04005947 public static final class Message {
5948
5949 static final String KEY_TEXT = "text";
5950 static final String KEY_TIMESTAMP = "time";
5951 static final String KEY_SENDER = "sender";
5952 static final String KEY_DATA_MIME_TYPE = "type";
5953 static final String KEY_DATA_URI= "uri";
Shane Brennan5a871862017-03-11 13:14:17 -08005954 static final String KEY_EXTRAS_BUNDLE = "extras";
Alex Hillsfc737de2016-03-23 17:33:02 -04005955
5956 private final CharSequence mText;
5957 private final long mTimestamp;
5958 private final CharSequence mSender;
5959
Shane Brennan5a871862017-03-11 13:14:17 -08005960 private Bundle mExtras = new Bundle();
Alex Hillsfc737de2016-03-23 17:33:02 -04005961 private String mDataMimeType;
5962 private Uri mDataUri;
5963
5964 /**
5965 * Constructor
5966 * @param text A {@link CharSequence} to be displayed as the message content
5967 * @param timestamp Time at which the message arrived
5968 * @param sender A {@link CharSequence} to be used for displaying the name of the
5969 * sender. Should be <code>null</code> for messages by the current user, in which case
5970 * the platform will insert {@link MessagingStyle#getUserDisplayName()}.
5971 * Should be unique amongst all individuals in the conversation, and should be
5972 * consistent during re-posts of the notification.
5973 */
5974 public Message(CharSequence text, long timestamp, CharSequence sender){
5975 mText = text;
5976 mTimestamp = timestamp;
5977 mSender = sender;
5978 }
5979
5980 /**
5981 * Sets a binary blob of data and an associated MIME type for a message. In the case
5982 * where the platform doesn't support the MIME type, the original text provided in the
5983 * constructor will be used.
5984 * @param dataMimeType The MIME type of the content. See
5985 * <a href="{@docRoot}notifications/messaging.html"> for the list of supported MIME
5986 * types on Android and Android Wear.
5987 * @param dataUri The uri containing the content whose type is given by the MIME type.
5988 * <p class="note">
5989 * <ol>
5990 * <li>Notification Listeners including the System UI need permission to access the
5991 * data the Uri points to. The recommended ways to do this are:</li>
5992 * <li>Store the data in your own ContentProvider, making sure that other apps have
5993 * the correct permission to access your provider. The preferred mechanism for
5994 * providing access is to use per-URI permissions which are temporary and only
5995 * grant access to the receiving application. An easy way to create a
5996 * ContentProvider like this is to use the FileProvider helper class.</li>
5997 * <li>Use the system MediaStore. The MediaStore is primarily aimed at video, audio
5998 * and image MIME types, however beginning with Android 3.0 (API level 11) it can
5999 * also store non-media types (see MediaStore.Files for more info). Files can be
6000 * inserted into the MediaStore using scanFile() after which a content:// style
6001 * Uri suitable for sharing is passed to the provided onScanCompleted() callback.
6002 * Note that once added to the system MediaStore the content is accessible to any
6003 * app on the device.</li>
6004 * </ol>
6005 * @return this object for method chaining
6006 */
6007 public Message setData(String dataMimeType, Uri dataUri) {
6008 mDataMimeType = dataMimeType;
6009 mDataUri = dataUri;
6010 return this;
6011 }
6012
Alex Hillsfc737de2016-03-23 17:33:02 -04006013 /**
6014 * Get the text to be used for this message, or the fallback text if a type and content
6015 * Uri have been set
6016 */
6017 public CharSequence getText() {
6018 return mText;
6019 }
6020
6021 /**
6022 * Get the time at which this message arrived
6023 */
6024 public long getTimestamp() {
6025 return mTimestamp;
6026 }
6027
6028 /**
Shane Brennan5a871862017-03-11 13:14:17 -08006029 * Get the extras Bundle for this message.
6030 */
6031 public Bundle getExtras() {
6032 return mExtras;
6033 }
6034
6035 /**
Alex Hillsfc737de2016-03-23 17:33:02 -04006036 * Get the text used to display the contact's name in the messaging experience
6037 */
6038 public CharSequence getSender() {
6039 return mSender;
6040 }
6041
6042 /**
6043 * Get the MIME type of the data pointed to by the Uri
6044 */
6045 public String getDataMimeType() {
6046 return mDataMimeType;
6047 }
6048
6049 /**
6050 * Get the the Uri pointing to the content of the message. Can be null, in which case
6051 * {@see #getText()} is used.
6052 */
6053 public Uri getDataUri() {
6054 return mDataUri;
6055 }
6056
Alex Hillsd9b04d92016-04-11 16:38:16 -04006057 private Bundle toBundle() {
6058 Bundle bundle = new Bundle();
Alex Hillsfc737de2016-03-23 17:33:02 -04006059 if (mText != null) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04006060 bundle.putCharSequence(KEY_TEXT, mText);
Alex Hillsfc737de2016-03-23 17:33:02 -04006061 }
Alex Hillsd9b04d92016-04-11 16:38:16 -04006062 bundle.putLong(KEY_TIMESTAMP, mTimestamp);
Alex Hillsfc737de2016-03-23 17:33:02 -04006063 if (mSender != null) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04006064 bundle.putCharSequence(KEY_SENDER, mSender);
Alex Hillsfc737de2016-03-23 17:33:02 -04006065 }
6066 if (mDataMimeType != null) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04006067 bundle.putString(KEY_DATA_MIME_TYPE, mDataMimeType);
Alex Hillsfc737de2016-03-23 17:33:02 -04006068 }
6069 if (mDataUri != null) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04006070 bundle.putParcelable(KEY_DATA_URI, mDataUri);
Alex Hillsfc737de2016-03-23 17:33:02 -04006071 }
Shane Brennan5a871862017-03-11 13:14:17 -08006072 if (mExtras != null) {
6073 bundle.putBundle(KEY_EXTRAS_BUNDLE, mExtras);
6074 }
Alex Hillsd9b04d92016-04-11 16:38:16 -04006075 return bundle;
Alex Hillsfc737de2016-03-23 17:33:02 -04006076 }
6077
Alex Hillsd9b04d92016-04-11 16:38:16 -04006078 static Bundle[] getBundleArrayForMessages(List<Message> messages) {
6079 Bundle[] bundles = new Bundle[messages.size()];
6080 final int N = messages.size();
6081 for (int i = 0; i < N; i++) {
6082 bundles[i] = messages.get(i).toBundle();
6083 }
6084 return bundles;
6085 }
6086
Adrian Roosdedd1df2016-04-26 16:38:47 -07006087 static List<Message> getMessagesFromBundleArray(Parcelable[] bundles) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04006088 List<Message> messages = new ArrayList<>(bundles.length);
6089 for (int i = 0; i < bundles.length; i++) {
Adrian Roosdedd1df2016-04-26 16:38:47 -07006090 if (bundles[i] instanceof Bundle) {
6091 Message message = getMessageFromBundle((Bundle)bundles[i]);
6092 if (message != null) {
6093 messages.add(message);
6094 }
Alex Hillsd9b04d92016-04-11 16:38:16 -04006095 }
6096 }
6097 return messages;
6098 }
6099
6100 static Message getMessageFromBundle(Bundle bundle) {
6101 try {
Adrian Roosfbddd2c2016-05-13 12:57:20 -07006102 if (!bundle.containsKey(KEY_TEXT) || !bundle.containsKey(KEY_TIMESTAMP)) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04006103 return null;
6104 } else {
6105 Message message = new Message(bundle.getCharSequence(KEY_TEXT),
6106 bundle.getLong(KEY_TIMESTAMP), bundle.getCharSequence(KEY_SENDER));
6107 if (bundle.containsKey(KEY_DATA_MIME_TYPE) &&
6108 bundle.containsKey(KEY_DATA_URI)) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04006109 message.setData(bundle.getString(KEY_DATA_MIME_TYPE),
6110 (Uri) bundle.getParcelable(KEY_DATA_URI));
Alex Hillsfc737de2016-03-23 17:33:02 -04006111 }
Shane Brennan5a871862017-03-11 13:14:17 -08006112 if (bundle.containsKey(KEY_EXTRAS_BUNDLE)) {
6113 message.getExtras().putAll(bundle.getBundle(KEY_EXTRAS_BUNDLE));
6114 }
Alex Hillsd9b04d92016-04-11 16:38:16 -04006115 return message;
6116 }
6117 } catch (ClassCastException e) {
6118 return null;
6119 }
6120 }
Alex Hillsfc737de2016-03-23 17:33:02 -04006121 }
6122 }
6123
6124 /**
Daniel Sandler879c5e02012-04-17 16:46:51 -04006125 * Helper class for generating large-format notifications that include a list of (up to 5) strings.
Joe Malin8d40d042012-11-05 11:36:40 -08006126 *
Robert Ly91c5ce32014-06-08 15:37:00 -07006127 * Here's how you'd set the <code>InboxStyle</code> on a notification:
Daniel Sandler879c5e02012-04-17 16:46:51 -04006128 * <pre class="prettyprint">
Robert Ly91c5ce32014-06-08 15:37:00 -07006129 * Notification notif = new Notification.Builder(mContext)
6130 * .setContentTitle(&quot;5 New mails from &quot; + sender.toString())
6131 * .setContentText(subject)
6132 * .setSmallIcon(R.drawable.new_mail)
6133 * .setLargeIcon(aBitmap)
6134 * .setStyle(new Notification.InboxStyle()
6135 * .addLine(str1)
6136 * .addLine(str2)
6137 * .setContentTitle(&quot;&quot;)
6138 * .setSummaryText(&quot;+3 more&quot;))
6139 * .build();
Daniel Sandler879c5e02012-04-17 16:46:51 -04006140 * </pre>
Joe Malin8d40d042012-11-05 11:36:40 -08006141 *
Daniel Sandler879c5e02012-04-17 16:46:51 -04006142 * @see Notification#bigContentView
6143 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006144 public static class InboxStyle extends Style {
Daniel Sandler879c5e02012-04-17 16:46:51 -04006145 private ArrayList<CharSequence> mTexts = new ArrayList<CharSequence>(5);
6146
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006147 public InboxStyle() {
6148 }
6149
Adrian Roosf5faf9d2016-05-23 13:56:15 -07006150 /**
6151 * @deprecated use {@code InboxStyle()}.
6152 */
6153 @Deprecated
Daniel Sandler879c5e02012-04-17 16:46:51 -04006154 public InboxStyle(Builder builder) {
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006155 setBuilder(builder);
Daniel Sandler879c5e02012-04-17 16:46:51 -04006156 }
6157
Chris Wrend6297db2012-05-03 16:20:13 -04006158 /**
6159 * Overrides ContentTitle in the big form of the template.
6160 * This defaults to the value passed to setContentTitle().
6161 */
6162 public InboxStyle setBigContentTitle(CharSequence title) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04006163 internalSetBigContentTitle(safeCharSequence(title));
Chris Wrend6297db2012-05-03 16:20:13 -04006164 return this;
6165 }
6166
6167 /**
6168 * Set the first line of text after the detail section in the big form of the template.
6169 */
6170 public InboxStyle setSummaryText(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04006171 internalSetSummaryText(safeCharSequence(cs));
Chris Wrend6297db2012-05-03 16:20:13 -04006172 return this;
6173 }
6174
Chris Wren0bd664d2012-08-01 13:56:56 -04006175 /**
6176 * Append a line to the digest section of the Inbox notification.
6177 */
Daniel Sandler879c5e02012-04-17 16:46:51 -04006178 public InboxStyle addLine(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04006179 mTexts.add(safeCharSequence(cs));
Daniel Sandler879c5e02012-04-17 16:46:51 -04006180 return this;
6181 }
6182
Daniel Sandlerf45564e2013-04-15 15:05:08 -04006183 /**
6184 * @hide
6185 */
6186 public void addExtras(Bundle extras) {
6187 super.addExtras(extras);
Christoph Studer4600f9b2014-07-22 22:44:43 +02006188
Daniel Sandlerf45564e2013-04-15 15:05:08 -04006189 CharSequence[] a = new CharSequence[mTexts.size()];
6190 extras.putCharSequenceArray(EXTRA_TEXT_LINES, mTexts.toArray(a));
6191 }
6192
Christoph Studer4600f9b2014-07-22 22:44:43 +02006193 /**
6194 * @hide
6195 */
6196 @Override
6197 protected void restoreFromExtras(Bundle extras) {
6198 super.restoreFromExtras(extras);
6199
6200 mTexts.clear();
6201 if (extras.containsKey(EXTRA_TEXT_LINES)) {
6202 Collections.addAll(mTexts, extras.getCharSequenceArray(EXTRA_TEXT_LINES));
6203 }
6204 }
6205
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006206 /**
6207 * @hide
6208 */
6209 public RemoteViews makeBigContentView() {
Selim Cinekc848c3a2016-01-13 15:27:30 -08006210 // Remove the content text so it disappears unless you have a summary
Christoph Studer4600f9b2014-07-22 22:44:43 +02006211 // Nasty
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006212 CharSequence oldBuilderContentText = mBuilder.mN.extras.getCharSequence(EXTRA_TEXT);
6213 mBuilder.getAllExtras().putCharSequence(EXTRA_TEXT, null);
Christoph Studer4600f9b2014-07-22 22:44:43 +02006214
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01006215 RemoteViews contentView = getStandardView(mBuilder.getInboxLayoutResource());
Daniel Sandler619738c2012-06-07 16:33:08 -04006216
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006217 mBuilder.getAllExtras().putCharSequence(EXTRA_TEXT, oldBuilderContentText);
Christoph Studer4600f9b2014-07-22 22:44:43 +02006218
Chris Wrend6297db2012-05-03 16:20:13 -04006219 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 -04006220 R.id.inbox_text4, R.id.inbox_text5, R.id.inbox_text6};
Chris Wrend6297db2012-05-03 16:20:13 -04006221
Chris Wren4ed80d52012-05-17 09:30:03 -04006222 // Make sure all rows are gone in case we reuse a view.
6223 for (int rowId : rowIds) {
6224 contentView.setViewVisibility(rowId, View.GONE);
6225 }
6226
Daniel Sandler879c5e02012-04-17 16:46:51 -04006227 int i=0;
Selim Cinek07c80172016-04-21 16:40:47 -07006228 int topPadding = mBuilder.mContext.getResources().getDimensionPixelSize(
6229 R.dimen.notification_inbox_item_top_padding);
Selim Cinek247fa012016-02-18 09:50:48 -08006230 boolean first = true;
Selim Cinek07c80172016-04-21 16:40:47 -07006231 int onlyViewId = 0;
6232 int maxRows = rowIds.length;
6233 if (mBuilder.mActions.size() > 0) {
6234 maxRows--;
6235 }
6236 while (i < mTexts.size() && i < maxRows) {
Daniel Sandler879c5e02012-04-17 16:46:51 -04006237 CharSequence str = mTexts.get(i);
Selim Cinek07c80172016-04-21 16:40:47 -07006238 if (!TextUtils.isEmpty(str)) {
Daniel Sandler879c5e02012-04-17 16:46:51 -04006239 contentView.setViewVisibility(rowIds[i], View.VISIBLE);
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01006240 contentView.setTextViewText(rowIds[i], mBuilder.processLegacyText(str));
Selim Cinek7b9605b2017-01-19 17:36:00 -08006241 mBuilder.setTextViewColorSecondary(contentView, rowIds[i]);
Selim Cinek07c80172016-04-21 16:40:47 -07006242 contentView.setViewPadding(rowIds[i], 0, topPadding, 0, 0);
Selim Cinek247fa012016-02-18 09:50:48 -08006243 handleInboxImageMargin(contentView, rowIds[i], first);
Selim Cinek07c80172016-04-21 16:40:47 -07006244 if (first) {
6245 onlyViewId = rowIds[i];
6246 } else {
6247 onlyViewId = 0;
6248 }
Selim Cinek247fa012016-02-18 09:50:48 -08006249 first = false;
Daniel Sandler879c5e02012-04-17 16:46:51 -04006250 }
6251 i++;
6252 }
Selim Cinek07c80172016-04-21 16:40:47 -07006253 if (onlyViewId != 0) {
6254 // We only have 1 entry, lets make it look like the normal Text of a Bigtext
6255 topPadding = mBuilder.mContext.getResources().getDimensionPixelSize(
6256 R.dimen.notification_text_margin_top);
6257 contentView.setViewPadding(onlyViewId, 0, topPadding, 0, 0);
6258 }
Selim Cinek1e0bf612015-11-20 15:57:26 -08006259
Daniel Sandler879c5e02012-04-17 16:46:51 -04006260 return contentView;
6261 }
Selim Cinek1e0bf612015-11-20 15:57:26 -08006262
Selim Cinek247fa012016-02-18 09:50:48 -08006263 private void handleInboxImageMargin(RemoteViews contentView, int id, boolean first) {
Selim Cinek1e0bf612015-11-20 15:57:26 -08006264 int endMargin = 0;
Selim Cinek247fa012016-02-18 09:50:48 -08006265 if (first) {
6266 final int max = mBuilder.mN.extras.getInt(EXTRA_PROGRESS_MAX, 0);
6267 final boolean ind = mBuilder.mN.extras.getBoolean(EXTRA_PROGRESS_INDETERMINATE);
6268 boolean hasProgress = max != 0 || ind;
Selim Cinek279fa862016-06-14 10:57:25 -07006269 if (mBuilder.mN.hasLargeIcon() && !hasProgress) {
Adrian Roos2d5dbba2016-06-08 17:11:53 -07006270 endMargin = R.dimen.notification_content_picture_margin;
Selim Cinek247fa012016-02-18 09:50:48 -08006271 }
Selim Cinek1e0bf612015-11-20 15:57:26 -08006272 }
Adrian Roos2d5dbba2016-06-08 17:11:53 -07006273 contentView.setViewLayoutMarginEndDimen(id, endMargin);
Selim Cinek1e0bf612015-11-20 15:57:26 -08006274 }
Daniel Sandler879c5e02012-04-17 16:46:51 -04006275 }
Dan Sandler842dd772014-05-15 09:36:47 -04006276
6277 /**
6278 * Notification style for media playback notifications.
6279 *
6280 * In the expanded form, {@link Notification#bigContentView}, up to 5
6281 * {@link Notification.Action}s specified with
Dan Sandler86647982015-05-13 23:41:13 -04006282 * {@link Notification.Builder#addAction(Action) addAction} will be
Dan Sandler842dd772014-05-15 09:36:47 -04006283 * shown as icon-only pushbuttons, suitable for transport controls. The Bitmap given to
6284 * {@link Notification.Builder#setLargeIcon(android.graphics.Bitmap) setLargeIcon()} will be
6285 * treated as album artwork.
Selim Cinek99104832017-01-25 14:47:33 -08006286 * <p>
Dan Sandler842dd772014-05-15 09:36:47 -04006287 * Unlike the other styles provided here, MediaStyle can also modify the standard-size
6288 * {@link Notification#contentView}; by providing action indices to
Christoph Studerfde6f4d2014-12-12 13:23:26 +01006289 * {@link #setShowActionsInCompactView(int...)} you can promote up to 3 actions to be displayed
Dan Sandler842dd772014-05-15 09:36:47 -04006290 * in the standard view alongside the usual content.
Selim Cinek99104832017-01-25 14:47:33 -08006291 * <p>
Bryan Mawhinney6be8de32014-07-18 10:35:12 +01006292 * Notifications created with MediaStyle will have their category set to
6293 * {@link Notification#CATEGORY_TRANSPORT CATEGORY_TRANSPORT} unless you set a different
6294 * category using {@link Notification.Builder#setCategory(String) setCategory()}.
Selim Cinek99104832017-01-25 14:47:33 -08006295 * <p>
Jeff Browndba34ba2014-06-24 20:46:03 -07006296 * Finally, if you attach a {@link android.media.session.MediaSession.Token} using
6297 * {@link android.app.Notification.MediaStyle#setMediaSession(MediaSession.Token)},
Dan Sandler842dd772014-05-15 09:36:47 -04006298 * the System UI can identify this as a notification representing an active media session
6299 * and respond accordingly (by showing album artwork in the lockscreen, for example).
6300 *
Selim Cinek99104832017-01-25 14:47:33 -08006301 * <p>
6302 * Starting at {@link android.os.Build.VERSION_CODES#O Android O} any notification that has a
6303 * media session attached with {@link #setMediaSession(MediaSession.Token)} will be colorized.
6304 * You can opt-out of this behavior by using {@link Notification.Builder#setColorized(boolean)}.
6305 * <p>
6306 *
Dan Sandler842dd772014-05-15 09:36:47 -04006307 * To use this style with your Notification, feed it to
6308 * {@link Notification.Builder#setStyle(android.app.Notification.Style)} like so:
6309 * <pre class="prettyprint">
6310 * Notification noti = new Notification.Builder()
6311 * .setSmallIcon(R.drawable.ic_stat_player)
Christoph Studere935fe92014-11-24 14:18:06 +01006312 * .setContentTitle(&quot;Track title&quot;)
6313 * .setContentText(&quot;Artist - Album&quot;)
6314 * .setLargeIcon(albumArtBitmap))
Bryan Mawhinney6be8de32014-07-18 10:35:12 +01006315 * .setStyle(<b>new Notification.MediaStyle()</b>
6316 * .setMediaSession(mySession))
Dan Sandler842dd772014-05-15 09:36:47 -04006317 * .build();
6318 * </pre>
6319 *
6320 * @see Notification#bigContentView
Selim Cinek99104832017-01-25 14:47:33 -08006321 * @see Notification.Builder#setColorized(boolean)
Dan Sandler842dd772014-05-15 09:36:47 -04006322 */
6323 public static class MediaStyle extends Style {
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02006324 static final int MAX_MEDIA_BUTTONS_IN_COMPACT = 3;
Dan Sandler842dd772014-05-15 09:36:47 -04006325 static final int MAX_MEDIA_BUTTONS = 5;
6326
6327 private int[] mActionsToShowInCompact = null;
Jeff Browndba34ba2014-06-24 20:46:03 -07006328 private MediaSession.Token mToken;
Dan Sandler842dd772014-05-15 09:36:47 -04006329
6330 public MediaStyle() {
6331 }
6332
Adrian Roosf5faf9d2016-05-23 13:56:15 -07006333 /**
6334 * @deprecated use {@code MediaStyle()}.
6335 */
6336 @Deprecated
Dan Sandler842dd772014-05-15 09:36:47 -04006337 public MediaStyle(Builder builder) {
6338 setBuilder(builder);
6339 }
6340
6341 /**
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02006342 * 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 -04006343 * notification view.
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02006344 *
6345 * @param actions the indices of the actions to show in the compact notification view
Dan Sandler842dd772014-05-15 09:36:47 -04006346 */
6347 public MediaStyle setShowActionsInCompactView(int...actions) {
6348 mActionsToShowInCompact = actions;
6349 return this;
6350 }
6351
6352 /**
Jeff Browndba34ba2014-06-24 20:46:03 -07006353 * Attach a {@link android.media.session.MediaSession.Token} to this Notification
6354 * to provide additional playback information and control to the SystemUI.
Dan Sandler842dd772014-05-15 09:36:47 -04006355 */
Jeff Browndba34ba2014-06-24 20:46:03 -07006356 public MediaStyle setMediaSession(MediaSession.Token token) {
Dan Sandler842dd772014-05-15 09:36:47 -04006357 mToken = token;
6358 return this;
6359 }
6360
Christoph Studer4600f9b2014-07-22 22:44:43 +02006361 /**
6362 * @hide
6363 */
Dan Sandler842dd772014-05-15 09:36:47 -04006364 @Override
6365 public Notification buildStyled(Notification wip) {
Christoph Studer4600f9b2014-07-22 22:44:43 +02006366 super.buildStyled(wip);
Bryan Mawhinney6be8de32014-07-18 10:35:12 +01006367 if (wip.category == null) {
6368 wip.category = Notification.CATEGORY_TRANSPORT;
6369 }
Dan Sandler842dd772014-05-15 09:36:47 -04006370 return wip;
6371 }
6372
Christoph Studer4600f9b2014-07-22 22:44:43 +02006373 /**
6374 * @hide
6375 */
6376 @Override
Selim Cinek7d1009b2017-01-25 15:28:28 -08006377 public RemoteViews makeContentView(boolean increasedHeight) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006378 return makeMediaContentView();
Christoph Studer4600f9b2014-07-22 22:44:43 +02006379 }
6380
6381 /**
6382 * @hide
6383 */
6384 @Override
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006385 public RemoteViews makeBigContentView() {
6386 return makeMediaBigContentView();
Christoph Studer4600f9b2014-07-22 22:44:43 +02006387 }
6388
Selim Cinekcc10bfb2016-02-10 16:24:21 -08006389 /**
6390 * @hide
6391 */
6392 @Override
Selim Cinek87ed69b2017-02-09 15:59:43 -08006393 public RemoteViews makeHeadsUpContentView(boolean increasedHeight) {
Selim Cinekcc10bfb2016-02-10 16:24:21 -08006394 RemoteViews expanded = makeMediaBigContentView();
6395 return expanded != null ? expanded : makeMediaContentView();
6396 }
6397
Dan Sandler842dd772014-05-15 09:36:47 -04006398 /** @hide */
6399 @Override
6400 public void addExtras(Bundle extras) {
6401 super.addExtras(extras);
6402
6403 if (mToken != null) {
6404 extras.putParcelable(EXTRA_MEDIA_SESSION, mToken);
6405 }
Bryan Mawhinneye191f902014-07-22 12:50:09 +01006406 if (mActionsToShowInCompact != null) {
6407 extras.putIntArray(EXTRA_COMPACT_ACTIONS, mActionsToShowInCompact);
6408 }
Dan Sandler842dd772014-05-15 09:36:47 -04006409 }
6410
Christoph Studer4600f9b2014-07-22 22:44:43 +02006411 /**
6412 * @hide
6413 */
6414 @Override
6415 protected void restoreFromExtras(Bundle extras) {
6416 super.restoreFromExtras(extras);
6417
6418 if (extras.containsKey(EXTRA_MEDIA_SESSION)) {
6419 mToken = extras.getParcelable(EXTRA_MEDIA_SESSION);
6420 }
6421 if (extras.containsKey(EXTRA_COMPACT_ACTIONS)) {
6422 mActionsToShowInCompact = extras.getIntArray(EXTRA_COMPACT_ACTIONS);
6423 }
6424 }
6425
Selim Cinek5bf069a2015-11-10 19:14:27 -05006426 private RemoteViews generateMediaActionButton(Action action, int color) {
Dan Sandler842dd772014-05-15 09:36:47 -04006427 final boolean tombstone = (action.actionIntent == null);
Selim Cinekf33b1112015-07-15 17:45:11 -07006428 RemoteViews button = new BuilderRemoteViews(mBuilder.mContext.getApplicationInfo(),
Alan Viverette3cb07a462014-06-06 14:19:53 -07006429 R.layout.notification_material_media_action);
Dan Sandler68079d52015-07-22 10:45:30 -04006430 button.setImageViewIcon(R.id.action0, action.getIcon());
Selim Cinek5bf069a2015-11-10 19:14:27 -05006431 button.setDrawableParameters(R.id.action0, false, -1, color, PorterDuff.Mode.SRC_ATOP,
6432 -1);
Dan Sandler842dd772014-05-15 09:36:47 -04006433 if (!tombstone) {
6434 button.setOnClickPendingIntent(R.id.action0, action.actionIntent);
6435 }
6436 button.setContentDescription(R.id.action0, action.title);
6437 return button;
6438 }
6439
6440 private RemoteViews makeMediaContentView() {
6441 RemoteViews view = mBuilder.applyStandardTemplate(
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02006442 R.layout.notification_template_material_media, false /* hasProgress */);
Dan Sandler842dd772014-05-15 09:36:47 -04006443
6444 final int numActions = mBuilder.mActions.size();
6445 final int N = mActionsToShowInCompact == null
6446 ? 0
6447 : Math.min(mActionsToShowInCompact.length, MAX_MEDIA_BUTTONS_IN_COMPACT);
6448 if (N > 0) {
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02006449 view.removeAllViews(com.android.internal.R.id.media_actions);
Dan Sandler842dd772014-05-15 09:36:47 -04006450 for (int i = 0; i < N; i++) {
6451 if (i >= numActions) {
6452 throw new IllegalArgumentException(String.format(
6453 "setShowActionsInCompactView: action %d out of bounds (max %d)",
6454 i, numActions - 1));
6455 }
6456
6457 final Action action = mBuilder.mActions.get(mActionsToShowInCompact[i]);
Selim Cinek5bf069a2015-11-10 19:14:27 -05006458 final RemoteViews button = generateMediaActionButton(action,
Selim Cinek99104832017-01-25 14:47:33 -08006459 getPrimaryHighlightColor());
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02006460 view.addView(com.android.internal.R.id.media_actions, button);
Dan Sandler842dd772014-05-15 09:36:47 -04006461 }
6462 }
Selim Cinekfdc738f2016-01-27 20:04:27 -08006463 handleImage(view);
6464 // handle the content margin
Adrian Roos2d5dbba2016-06-08 17:11:53 -07006465 int endMargin = R.dimen.notification_content_margin_end;
Selim Cinek279fa862016-06-14 10:57:25 -07006466 if (mBuilder.mN.hasLargeIcon()) {
Adrian Roos2d5dbba2016-06-08 17:11:53 -07006467 endMargin = R.dimen.notification_content_plus_picture_margin_end;
Selim Cinekfdc738f2016-01-27 20:04:27 -08006468 }
Adrian Roos2d5dbba2016-06-08 17:11:53 -07006469 view.setViewLayoutMarginEndDimen(R.id.notification_main_column, endMargin);
Dan Sandler842dd772014-05-15 09:36:47 -04006470 return view;
6471 }
6472
Selim Cinek99104832017-01-25 14:47:33 -08006473 private int getPrimaryHighlightColor() {
6474 return mBuilder.getPrimaryHighlightColor();
6475 }
6476
Dan Sandler842dd772014-05-15 09:36:47 -04006477 private RemoteViews makeMediaBigContentView() {
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02006478 final int actionCount = Math.min(mBuilder.mActions.size(), MAX_MEDIA_BUTTONS);
Selim Cinekcc10bfb2016-02-10 16:24:21 -08006479 // Dont add an expanded view if there is no more content to be revealed
6480 int actionsInCompact = mActionsToShowInCompact == null
6481 ? 0
6482 : Math.min(mActionsToShowInCompact.length, MAX_MEDIA_BUTTONS_IN_COMPACT);
Selim Cinek279fa862016-06-14 10:57:25 -07006483 if (!mBuilder.mN.hasLargeIcon() && actionCount <= actionsInCompact) {
Selim Cinekcc10bfb2016-02-10 16:24:21 -08006484 return null;
6485 }
Selim Cinek5bf069a2015-11-10 19:14:27 -05006486 RemoteViews big = mBuilder.applyStandardTemplate(
6487 R.layout.notification_template_material_big_media,
6488 false);
Dan Sandler842dd772014-05-15 09:36:47 -04006489
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02006490 if (actionCount > 0) {
6491 big.removeAllViews(com.android.internal.R.id.media_actions);
6492 for (int i = 0; i < actionCount; i++) {
Selim Cinek5bf069a2015-11-10 19:14:27 -05006493 final RemoteViews button = generateMediaActionButton(mBuilder.mActions.get(i),
Selim Cinek99104832017-01-25 14:47:33 -08006494 getPrimaryHighlightColor());
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02006495 big.addView(com.android.internal.R.id.media_actions, button);
Dan Sandler842dd772014-05-15 09:36:47 -04006496 }
6497 }
Selim Cinek5bf069a2015-11-10 19:14:27 -05006498 handleImage(big);
Dan Sandler842dd772014-05-15 09:36:47 -04006499 return big;
6500 }
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02006501
Selim Cinek5bf069a2015-11-10 19:14:27 -05006502 private void handleImage(RemoteViews contentView) {
Selim Cinek279fa862016-06-14 10:57:25 -07006503 if (mBuilder.mN.hasLargeIcon()) {
Adrian Roos2d5dbba2016-06-08 17:11:53 -07006504 contentView.setViewLayoutMarginEndDimen(R.id.line1, 0);
6505 contentView.setViewLayoutMarginEndDimen(R.id.text, 0);
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02006506 }
6507 }
6508
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02006509 /**
6510 * @hide
6511 */
6512 @Override
6513 protected boolean hasProgress() {
6514 return false;
6515 }
Dan Sandler842dd772014-05-15 09:36:47 -04006516 }
Griff Hazen61a9e862014-05-22 16:05:19 -07006517
Selim Cinek593610c2016-02-16 18:42:57 -08006518 /**
6519 * Notification style for custom views that are decorated by the system
6520 *
6521 * <p>Instead of providing a notification that is completely custom, a developer can set this
6522 * style and still obtain system decorations like the notification header with the expand
6523 * affordance and actions.
6524 *
6525 * <p>Use {@link android.app.Notification.Builder#setCustomContentView(RemoteViews)},
6526 * {@link android.app.Notification.Builder#setCustomBigContentView(RemoteViews)} and
6527 * {@link android.app.Notification.Builder#setCustomHeadsUpContentView(RemoteViews)} to set the
6528 * corresponding custom views to display.
6529 *
6530 * To use this style with your Notification, feed it to
6531 * {@link Notification.Builder#setStyle(android.app.Notification.Style)} like so:
6532 * <pre class="prettyprint">
6533 * Notification noti = new Notification.Builder()
6534 * .setSmallIcon(R.drawable.ic_stat_player)
6535 * .setLargeIcon(albumArtBitmap))
6536 * .setCustomContentView(contentView);
6537 * .setStyle(<b>new Notification.DecoratedCustomViewStyle()</b>)
6538 * .build();
6539 * </pre>
6540 */
6541 public static class DecoratedCustomViewStyle extends Style {
6542
6543 public DecoratedCustomViewStyle() {
6544 }
6545
Selim Cinek593610c2016-02-16 18:42:57 -08006546 /**
6547 * @hide
6548 */
6549 public boolean displayCustomViewInline() {
6550 return true;
6551 }
6552
6553 /**
6554 * @hide
6555 */
6556 @Override
Selim Cinek7d1009b2017-01-25 15:28:28 -08006557 public RemoteViews makeContentView(boolean increasedHeight) {
Selim Cinek593610c2016-02-16 18:42:57 -08006558 return makeStandardTemplateWithCustomContent(mBuilder.mN.contentView);
6559 }
6560
6561 /**
6562 * @hide
6563 */
6564 @Override
6565 public RemoteViews makeBigContentView() {
6566 return makeDecoratedBigContentView();
6567 }
6568
6569 /**
6570 * @hide
6571 */
6572 @Override
Selim Cinek87ed69b2017-02-09 15:59:43 -08006573 public RemoteViews makeHeadsUpContentView(boolean increasedHeight) {
Selim Cinek593610c2016-02-16 18:42:57 -08006574 return makeDecoratedHeadsUpContentView();
6575 }
6576
Selim Cinek593610c2016-02-16 18:42:57 -08006577 private RemoteViews makeDecoratedHeadsUpContentView() {
6578 RemoteViews headsUpContentView = mBuilder.mN.headsUpContentView == null
6579 ? mBuilder.mN.contentView
6580 : mBuilder.mN.headsUpContentView;
6581 if (mBuilder.mActions.size() == 0) {
6582 return makeStandardTemplateWithCustomContent(headsUpContentView);
6583 }
6584 RemoteViews remoteViews = mBuilder.applyStandardTemplateWithActions(
6585 mBuilder.getBigBaseLayoutResource());
Selim Cinek247fa012016-02-18 09:50:48 -08006586 buildIntoRemoteViewContent(remoteViews, headsUpContentView);
Selim Cinek593610c2016-02-16 18:42:57 -08006587 return remoteViews;
6588 }
6589
Selim Cinek593610c2016-02-16 18:42:57 -08006590 private RemoteViews makeStandardTemplateWithCustomContent(RemoteViews customContent) {
6591 RemoteViews remoteViews = mBuilder.applyStandardTemplate(
6592 mBuilder.getBaseLayoutResource());
Selim Cinek247fa012016-02-18 09:50:48 -08006593 buildIntoRemoteViewContent(remoteViews, customContent);
Selim Cinek593610c2016-02-16 18:42:57 -08006594 return remoteViews;
6595 }
6596
Selim Cinek593610c2016-02-16 18:42:57 -08006597 private RemoteViews makeDecoratedBigContentView() {
6598 RemoteViews bigContentView = mBuilder.mN.bigContentView == null
6599 ? mBuilder.mN.contentView
6600 : mBuilder.mN.bigContentView;
6601 if (mBuilder.mActions.size() == 0) {
6602 return makeStandardTemplateWithCustomContent(bigContentView);
6603 }
6604 RemoteViews remoteViews = mBuilder.applyStandardTemplateWithActions(
6605 mBuilder.getBigBaseLayoutResource());
Selim Cinek247fa012016-02-18 09:50:48 -08006606 buildIntoRemoteViewContent(remoteViews, bigContentView);
Selim Cinek593610c2016-02-16 18:42:57 -08006607 return remoteViews;
6608 }
Selim Cinek247fa012016-02-18 09:50:48 -08006609
6610 private void buildIntoRemoteViewContent(RemoteViews remoteViews,
6611 RemoteViews customContent) {
Adrian Roos5081c0d2016-02-26 16:04:19 -08006612 if (customContent != null) {
Selim Cinekf91017e2016-03-14 12:25:09 -07006613 // Need to clone customContent before adding, because otherwise it can no longer be
6614 // parceled independently of remoteViews.
Adrian Roos5081c0d2016-02-26 16:04:19 -08006615 customContent = customContent.clone();
Selim Cinekf91017e2016-03-14 12:25:09 -07006616 remoteViews.removeAllViews(R.id.notification_main_column);
6617 remoteViews.addView(R.id.notification_main_column, customContent);
Adrian Roos5081c0d2016-02-26 16:04:19 -08006618 }
Selim Cinek247fa012016-02-18 09:50:48 -08006619 // also update the end margin if there is an image
Adrian Roos2d5dbba2016-06-08 17:11:53 -07006620 int endMargin = R.dimen.notification_content_margin_end;
Selim Cinek279fa862016-06-14 10:57:25 -07006621 if (mBuilder.mN.hasLargeIcon()) {
Adrian Roos2d5dbba2016-06-08 17:11:53 -07006622 endMargin = R.dimen.notification_content_plus_picture_margin_end;
Selim Cinek247fa012016-02-18 09:50:48 -08006623 }
Adrian Roos2d5dbba2016-06-08 17:11:53 -07006624 remoteViews.setViewLayoutMarginEndDimen(R.id.notification_main_column, endMargin);
Selim Cinek247fa012016-02-18 09:50:48 -08006625 }
Selim Cinek593610c2016-02-16 18:42:57 -08006626 }
6627
Selim Cinek03eb3b72016-02-18 10:39:45 -08006628 /**
6629 * Notification style for media custom views that are decorated by the system
6630 *
6631 * <p>Instead of providing a media notification that is completely custom, a developer can set
6632 * this style and still obtain system decorations like the notification header with the expand
6633 * affordance and actions.
6634 *
6635 * <p>Use {@link android.app.Notification.Builder#setCustomContentView(RemoteViews)},
6636 * {@link android.app.Notification.Builder#setCustomBigContentView(RemoteViews)} and
6637 * {@link android.app.Notification.Builder#setCustomHeadsUpContentView(RemoteViews)} to set the
6638 * corresponding custom views to display.
Selim Cinek99104832017-01-25 14:47:33 -08006639 * <p>
6640 * Contrary to {@link MediaStyle} a developer has to opt-in to the colorizing of the
6641 * notification by using {@link Notification.Builder#setColorized(boolean)}.
6642 * <p>
Selim Cinek03eb3b72016-02-18 10:39:45 -08006643 * To use this style with your Notification, feed it to
6644 * {@link Notification.Builder#setStyle(android.app.Notification.Style)} like so:
6645 * <pre class="prettyprint">
6646 * Notification noti = new Notification.Builder()
6647 * .setSmallIcon(R.drawable.ic_stat_player)
6648 * .setLargeIcon(albumArtBitmap))
6649 * .setCustomContentView(contentView);
6650 * .setStyle(<b>new Notification.DecoratedMediaCustomViewStyle()</b>
6651 * .setMediaSession(mySession))
6652 * .build();
6653 * </pre>
6654 *
6655 * @see android.app.Notification.DecoratedCustomViewStyle
6656 * @see android.app.Notification.MediaStyle
6657 */
6658 public static class DecoratedMediaCustomViewStyle extends MediaStyle {
6659
6660 public DecoratedMediaCustomViewStyle() {
6661 }
6662
Selim Cinek03eb3b72016-02-18 10:39:45 -08006663 /**
6664 * @hide
6665 */
6666 public boolean displayCustomViewInline() {
6667 return true;
6668 }
6669
6670 /**
6671 * @hide
6672 */
6673 @Override
Selim Cinek7d1009b2017-01-25 15:28:28 -08006674 public RemoteViews makeContentView(boolean increasedHeight) {
6675 RemoteViews remoteViews = super.makeContentView(false /* increasedHeight */);
Selim Cinek03eb3b72016-02-18 10:39:45 -08006676 return buildIntoRemoteView(remoteViews, R.id.notification_content_container,
6677 mBuilder.mN.contentView);
6678 }
6679
6680 /**
6681 * @hide
6682 */
6683 @Override
6684 public RemoteViews makeBigContentView() {
6685 RemoteViews customRemoteView = mBuilder.mN.bigContentView != null
6686 ? mBuilder.mN.bigContentView
6687 : mBuilder.mN.contentView;
6688 return makeBigContentViewWithCustomContent(customRemoteView);
6689 }
6690
6691 private RemoteViews makeBigContentViewWithCustomContent(RemoteViews customRemoteView) {
6692 RemoteViews remoteViews = super.makeBigContentView();
6693 if (remoteViews != null) {
6694 return buildIntoRemoteView(remoteViews, R.id.notification_main_column,
6695 customRemoteView);
6696 } else if (customRemoteView != mBuilder.mN.contentView){
Selim Cinek7d1009b2017-01-25 15:28:28 -08006697 remoteViews = super.makeContentView(false /* increasedHeight */);
Selim Cinek03eb3b72016-02-18 10:39:45 -08006698 return buildIntoRemoteView(remoteViews, R.id.notification_content_container,
6699 customRemoteView);
6700 } else {
6701 return null;
6702 }
6703 }
6704
6705 /**
6706 * @hide
6707 */
6708 @Override
Selim Cinek87ed69b2017-02-09 15:59:43 -08006709 public RemoteViews makeHeadsUpContentView(boolean increasedHeight) {
Selim Cinek03eb3b72016-02-18 10:39:45 -08006710 RemoteViews customRemoteView = mBuilder.mN.headsUpContentView != null
6711 ? mBuilder.mN.headsUpContentView
6712 : mBuilder.mN.contentView;
6713 return makeBigContentViewWithCustomContent(customRemoteView);
6714 }
6715
6716 private RemoteViews buildIntoRemoteView(RemoteViews remoteViews, int id,
6717 RemoteViews customContent) {
Adrian Roos5081c0d2016-02-26 16:04:19 -08006718 if (customContent != null) {
Selim Cinekf91017e2016-03-14 12:25:09 -07006719 // Need to clone customContent before adding, because otherwise it can no longer be
6720 // parceled independently of remoteViews.
Adrian Roos5081c0d2016-02-26 16:04:19 -08006721 customContent = customContent.clone();
Selim Cinekf91017e2016-03-14 12:25:09 -07006722 remoteViews.removeAllViews(id);
6723 remoteViews.addView(id, customContent);
Adrian Roos5081c0d2016-02-26 16:04:19 -08006724 }
Selim Cinek03eb3b72016-02-18 10:39:45 -08006725 return remoteViews;
6726 }
6727 }
6728
Christoph Studer4600f9b2014-07-22 22:44:43 +02006729 // When adding a new Style subclass here, don't forget to update
6730 // Builder.getNotificationStyleClass.
6731
Griff Hazen61a9e862014-05-22 16:05:19 -07006732 /**
6733 * Extender interface for use with {@link Builder#extend}. Extenders may be used to add
6734 * metadata or change options on a notification builder.
6735 */
6736 public interface Extender {
6737 /**
6738 * Apply this extender to a notification builder.
6739 * @param builder the builder to be modified.
6740 * @return the build object for chaining.
6741 */
6742 public Builder extend(Builder builder);
6743 }
6744
6745 /**
6746 * Helper class to add wearable extensions to notifications.
6747 * <p class="note"> See
6748 * <a href="{@docRoot}wear/notifications/creating.html">Creating Notifications
6749 * for Android Wear</a> for more information on how to use this class.
6750 * <p>
6751 * To create a notification with wearable extensions:
6752 * <ol>
6753 * <li>Create a {@link android.app.Notification.Builder}, setting any desired
6754 * properties.
6755 * <li>Create a {@link android.app.Notification.WearableExtender}.
6756 * <li>Set wearable-specific properties using the
6757 * {@code add} and {@code set} methods of {@link android.app.Notification.WearableExtender}.
6758 * <li>Call {@link android.app.Notification.Builder#extend} to apply the extensions to a
6759 * notification.
6760 * <li>Post the notification to the notification system with the
6761 * {@code NotificationManager.notify(...)} methods.
6762 * </ol>
6763 *
6764 * <pre class="prettyprint">
6765 * Notification notif = new Notification.Builder(mContext)
6766 * .setContentTitle(&quot;New mail from &quot; + sender.toString())
6767 * .setContentText(subject)
6768 * .setSmallIcon(R.drawable.new_mail)
6769 * .extend(new Notification.WearableExtender()
6770 * .setContentIcon(R.drawable.new_mail))
6771 * .build();
6772 * NotificationManager notificationManger =
6773 * (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
6774 * notificationManger.notify(0, notif);</pre>
6775 *
6776 * <p>Wearable extensions can be accessed on an existing notification by using the
6777 * {@code WearableExtender(Notification)} constructor,
6778 * and then using the {@code get} methods to access values.
6779 *
6780 * <pre class="prettyprint">
6781 * Notification.WearableExtender wearableExtender = new Notification.WearableExtender(
6782 * notification);
Griff Hazen14f57992014-05-26 09:07:14 -07006783 * List&lt;Notification&gt; pages = wearableExtender.getPages();</pre>
Griff Hazen61a9e862014-05-22 16:05:19 -07006784 */
6785 public static final class WearableExtender implements Extender {
6786 /**
6787 * Sentinel value for an action index that is unset.
6788 */
6789 public static final int UNSET_ACTION_INDEX = -1;
6790
6791 /**
6792 * Size value for use with {@link #setCustomSizePreset} to show this notification with
6793 * default sizing.
6794 * <p>For custom display notifications created using {@link #setDisplayIntent},
Paul Soulosaa4f4bf2015-08-04 11:59:45 -07006795 * the default is {@link #SIZE_MEDIUM}. All other notifications size automatically based
Griff Hazen61a9e862014-05-22 16:05:19 -07006796 * on their content.
6797 */
6798 public static final int SIZE_DEFAULT = 0;
6799
6800 /**
6801 * Size value for use with {@link #setCustomSizePreset} to show this notification
6802 * with an extra small size.
6803 * <p>This value is only applicable for custom display notifications created using
6804 * {@link #setDisplayIntent}.
6805 */
6806 public static final int SIZE_XSMALL = 1;
6807
6808 /**
6809 * Size value for use with {@link #setCustomSizePreset} to show this notification
6810 * with a small size.
6811 * <p>This value is only applicable for custom display notifications created using
6812 * {@link #setDisplayIntent}.
6813 */
6814 public static final int SIZE_SMALL = 2;
6815
6816 /**
6817 * Size value for use with {@link #setCustomSizePreset} to show this notification
6818 * with a medium size.
6819 * <p>This value is only applicable for custom display notifications created using
6820 * {@link #setDisplayIntent}.
6821 */
6822 public static final int SIZE_MEDIUM = 3;
6823
6824 /**
6825 * Size value for use with {@link #setCustomSizePreset} to show this notification
6826 * with a large size.
6827 * <p>This value is only applicable for custom display notifications created using
6828 * {@link #setDisplayIntent}.
6829 */
6830 public static final int SIZE_LARGE = 4;
6831
Griff Hazend5f11f92014-05-27 15:40:09 -07006832 /**
6833 * Size value for use with {@link #setCustomSizePreset} to show this notification
6834 * full screen.
6835 * <p>This value is only applicable for custom display notifications created using
6836 * {@link #setDisplayIntent}.
6837 */
6838 public static final int SIZE_FULL_SCREEN = 5;
6839
Griff Hazen5f2edfc2014-09-29 16:28:44 -07006840 /**
6841 * Sentinel value for use with {@link #setHintScreenTimeout} to keep the screen on for a
6842 * short amount of time when this notification is displayed on the screen. This
6843 * is the default value.
6844 */
6845 public static final int SCREEN_TIMEOUT_SHORT = 0;
6846
6847 /**
6848 * Sentinel value for use with {@link #setHintScreenTimeout} to keep the screen on
6849 * for a longer amount of time when this notification is displayed on the screen.
6850 */
6851 public static final int SCREEN_TIMEOUT_LONG = -1;
6852
Griff Hazen61a9e862014-05-22 16:05:19 -07006853 /** Notification extra which contains wearable extensions */
6854 private static final String EXTRA_WEARABLE_EXTENSIONS = "android.wearable.EXTENSIONS";
6855
Pete Gastaf6781d2014-10-07 15:17:05 -04006856 // Keys within EXTRA_WEARABLE_EXTENSIONS for wearable options.
Griff Hazen61a9e862014-05-22 16:05:19 -07006857 private static final String KEY_ACTIONS = "actions";
6858 private static final String KEY_FLAGS = "flags";
6859 private static final String KEY_DISPLAY_INTENT = "displayIntent";
6860 private static final String KEY_PAGES = "pages";
6861 private static final String KEY_BACKGROUND = "background";
6862 private static final String KEY_CONTENT_ICON = "contentIcon";
6863 private static final String KEY_CONTENT_ICON_GRAVITY = "contentIconGravity";
6864 private static final String KEY_CONTENT_ACTION_INDEX = "contentActionIndex";
6865 private static final String KEY_CUSTOM_SIZE_PRESET = "customSizePreset";
6866 private static final String KEY_CUSTOM_CONTENT_HEIGHT = "customContentHeight";
6867 private static final String KEY_GRAVITY = "gravity";
Griff Hazen5f2edfc2014-09-29 16:28:44 -07006868 private static final String KEY_HINT_SCREEN_TIMEOUT = "hintScreenTimeout";
Nadia Benbernou948627e2016-04-14 14:41:08 -04006869 private static final String KEY_DISMISSAL_ID = "dismissalId";
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04006870 private static final String KEY_BRIDGE_TAG = "bridgeTag";
Griff Hazen61a9e862014-05-22 16:05:19 -07006871
6872 // Flags bitwise-ored to mFlags
6873 private static final int FLAG_CONTENT_INTENT_AVAILABLE_OFFLINE = 0x1;
6874 private static final int FLAG_HINT_HIDE_ICON = 1 << 1;
6875 private static final int FLAG_HINT_SHOW_BACKGROUND_ONLY = 1 << 2;
6876 private static final int FLAG_START_SCROLL_BOTTOM = 1 << 3;
Griff Hazen5f2edfc2014-09-29 16:28:44 -07006877 private static final int FLAG_HINT_AVOID_BACKGROUND_CLIPPING = 1 << 4;
Alex Hills4bcb06b2016-04-05 14:26:25 -04006878 private static final int FLAG_BIG_PICTURE_AMBIENT = 1 << 5;
Alex Hills9ab3a232016-04-05 14:54:56 -04006879 private static final int FLAG_HINT_CONTENT_INTENT_LAUNCHES_ACTIVITY = 1 << 6;
Griff Hazen61a9e862014-05-22 16:05:19 -07006880
6881 // Default value for flags integer
6882 private static final int DEFAULT_FLAGS = FLAG_CONTENT_INTENT_AVAILABLE_OFFLINE;
6883
6884 private static final int DEFAULT_CONTENT_ICON_GRAVITY = Gravity.END;
6885 private static final int DEFAULT_GRAVITY = Gravity.BOTTOM;
6886
6887 private ArrayList<Action> mActions = new ArrayList<Action>();
6888 private int mFlags = DEFAULT_FLAGS;
6889 private PendingIntent mDisplayIntent;
6890 private ArrayList<Notification> mPages = new ArrayList<Notification>();
6891 private Bitmap mBackground;
6892 private int mContentIcon;
6893 private int mContentIconGravity = DEFAULT_CONTENT_ICON_GRAVITY;
6894 private int mContentActionIndex = UNSET_ACTION_INDEX;
6895 private int mCustomSizePreset = SIZE_DEFAULT;
6896 private int mCustomContentHeight;
6897 private int mGravity = DEFAULT_GRAVITY;
Griff Hazen5f2edfc2014-09-29 16:28:44 -07006898 private int mHintScreenTimeout;
Nadia Benbernou948627e2016-04-14 14:41:08 -04006899 private String mDismissalId;
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04006900 private String mBridgeTag;
Griff Hazen61a9e862014-05-22 16:05:19 -07006901
6902 /**
6903 * Create a {@link android.app.Notification.WearableExtender} with default
6904 * options.
6905 */
6906 public WearableExtender() {
6907 }
6908
6909 public WearableExtender(Notification notif) {
6910 Bundle wearableBundle = notif.extras.getBundle(EXTRA_WEARABLE_EXTENSIONS);
6911 if (wearableBundle != null) {
6912 List<Action> actions = wearableBundle.getParcelableArrayList(KEY_ACTIONS);
6913 if (actions != null) {
6914 mActions.addAll(actions);
6915 }
6916
6917 mFlags = wearableBundle.getInt(KEY_FLAGS, DEFAULT_FLAGS);
6918 mDisplayIntent = wearableBundle.getParcelable(KEY_DISPLAY_INTENT);
6919
6920 Notification[] pages = getNotificationArrayFromBundle(
6921 wearableBundle, KEY_PAGES);
6922 if (pages != null) {
6923 Collections.addAll(mPages, pages);
6924 }
6925
6926 mBackground = wearableBundle.getParcelable(KEY_BACKGROUND);
6927 mContentIcon = wearableBundle.getInt(KEY_CONTENT_ICON);
6928 mContentIconGravity = wearableBundle.getInt(KEY_CONTENT_ICON_GRAVITY,
6929 DEFAULT_CONTENT_ICON_GRAVITY);
6930 mContentActionIndex = wearableBundle.getInt(KEY_CONTENT_ACTION_INDEX,
6931 UNSET_ACTION_INDEX);
6932 mCustomSizePreset = wearableBundle.getInt(KEY_CUSTOM_SIZE_PRESET,
6933 SIZE_DEFAULT);
6934 mCustomContentHeight = wearableBundle.getInt(KEY_CUSTOM_CONTENT_HEIGHT);
6935 mGravity = wearableBundle.getInt(KEY_GRAVITY, DEFAULT_GRAVITY);
Griff Hazen5f2edfc2014-09-29 16:28:44 -07006936 mHintScreenTimeout = wearableBundle.getInt(KEY_HINT_SCREEN_TIMEOUT);
Nadia Benbernou948627e2016-04-14 14:41:08 -04006937 mDismissalId = wearableBundle.getString(KEY_DISMISSAL_ID);
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04006938 mBridgeTag = wearableBundle.getString(KEY_BRIDGE_TAG);
Griff Hazen61a9e862014-05-22 16:05:19 -07006939 }
6940 }
6941
6942 /**
6943 * Apply wearable extensions to a notification that is being built. This is typically
6944 * called by the {@link android.app.Notification.Builder#extend} method of
6945 * {@link android.app.Notification.Builder}.
6946 */
6947 @Override
6948 public Notification.Builder extend(Notification.Builder builder) {
6949 Bundle wearableBundle = new Bundle();
6950
6951 if (!mActions.isEmpty()) {
6952 wearableBundle.putParcelableArrayList(KEY_ACTIONS, mActions);
6953 }
6954 if (mFlags != DEFAULT_FLAGS) {
6955 wearableBundle.putInt(KEY_FLAGS, mFlags);
6956 }
6957 if (mDisplayIntent != null) {
6958 wearableBundle.putParcelable(KEY_DISPLAY_INTENT, mDisplayIntent);
6959 }
6960 if (!mPages.isEmpty()) {
6961 wearableBundle.putParcelableArray(KEY_PAGES, mPages.toArray(
6962 new Notification[mPages.size()]));
6963 }
6964 if (mBackground != null) {
6965 wearableBundle.putParcelable(KEY_BACKGROUND, mBackground);
6966 }
6967 if (mContentIcon != 0) {
6968 wearableBundle.putInt(KEY_CONTENT_ICON, mContentIcon);
6969 }
6970 if (mContentIconGravity != DEFAULT_CONTENT_ICON_GRAVITY) {
6971 wearableBundle.putInt(KEY_CONTENT_ICON_GRAVITY, mContentIconGravity);
6972 }
6973 if (mContentActionIndex != UNSET_ACTION_INDEX) {
6974 wearableBundle.putInt(KEY_CONTENT_ACTION_INDEX,
6975 mContentActionIndex);
6976 }
6977 if (mCustomSizePreset != SIZE_DEFAULT) {
6978 wearableBundle.putInt(KEY_CUSTOM_SIZE_PRESET, mCustomSizePreset);
6979 }
6980 if (mCustomContentHeight != 0) {
6981 wearableBundle.putInt(KEY_CUSTOM_CONTENT_HEIGHT, mCustomContentHeight);
6982 }
6983 if (mGravity != DEFAULT_GRAVITY) {
6984 wearableBundle.putInt(KEY_GRAVITY, mGravity);
6985 }
Griff Hazen5f2edfc2014-09-29 16:28:44 -07006986 if (mHintScreenTimeout != 0) {
6987 wearableBundle.putInt(KEY_HINT_SCREEN_TIMEOUT, mHintScreenTimeout);
6988 }
Nadia Benbernou948627e2016-04-14 14:41:08 -04006989 if (mDismissalId != null) {
6990 wearableBundle.putString(KEY_DISMISSAL_ID, mDismissalId);
6991 }
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04006992 if (mBridgeTag != null) {
6993 wearableBundle.putString(KEY_BRIDGE_TAG, mBridgeTag);
6994 }
Griff Hazen61a9e862014-05-22 16:05:19 -07006995
6996 builder.getExtras().putBundle(EXTRA_WEARABLE_EXTENSIONS, wearableBundle);
6997 return builder;
6998 }
6999
7000 @Override
7001 public WearableExtender clone() {
7002 WearableExtender that = new WearableExtender();
7003 that.mActions = new ArrayList<Action>(this.mActions);
7004 that.mFlags = this.mFlags;
7005 that.mDisplayIntent = this.mDisplayIntent;
7006 that.mPages = new ArrayList<Notification>(this.mPages);
7007 that.mBackground = this.mBackground;
7008 that.mContentIcon = this.mContentIcon;
7009 that.mContentIconGravity = this.mContentIconGravity;
7010 that.mContentActionIndex = this.mContentActionIndex;
7011 that.mCustomSizePreset = this.mCustomSizePreset;
7012 that.mCustomContentHeight = this.mCustomContentHeight;
7013 that.mGravity = this.mGravity;
Griff Hazen5f2edfc2014-09-29 16:28:44 -07007014 that.mHintScreenTimeout = this.mHintScreenTimeout;
Nadia Benbernou948627e2016-04-14 14:41:08 -04007015 that.mDismissalId = this.mDismissalId;
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04007016 that.mBridgeTag = this.mBridgeTag;
Griff Hazen61a9e862014-05-22 16:05:19 -07007017 return that;
7018 }
7019
7020 /**
7021 * Add a wearable action to this notification.
7022 *
7023 * <p>When wearable actions are added using this method, the set of actions that
7024 * show on a wearable device splits from devices that only show actions added
7025 * using {@link android.app.Notification.Builder#addAction}. This allows for customization
7026 * of which actions display on different devices.
7027 *
7028 * @param action the action to add to this notification
7029 * @return this object for method chaining
7030 * @see android.app.Notification.Action
7031 */
7032 public WearableExtender addAction(Action action) {
7033 mActions.add(action);
7034 return this;
7035 }
7036
7037 /**
7038 * Adds wearable actions to this notification.
7039 *
7040 * <p>When wearable actions are added using this method, the set of actions that
7041 * show on a wearable device splits from devices that only show actions added
7042 * using {@link android.app.Notification.Builder#addAction}. This allows for customization
7043 * of which actions display on different devices.
7044 *
7045 * @param actions the actions to add to this notification
7046 * @return this object for method chaining
7047 * @see android.app.Notification.Action
7048 */
7049 public WearableExtender addActions(List<Action> actions) {
7050 mActions.addAll(actions);
7051 return this;
7052 }
7053
7054 /**
7055 * Clear all wearable actions present on this builder.
7056 * @return this object for method chaining.
7057 * @see #addAction
7058 */
7059 public WearableExtender clearActions() {
7060 mActions.clear();
7061 return this;
7062 }
7063
7064 /**
7065 * Get the wearable actions present on this notification.
7066 */
7067 public List<Action> getActions() {
7068 return mActions;
7069 }
7070
7071 /**
7072 * Set an intent to launch inside of an activity view when displaying
Griff Hazen14f57992014-05-26 09:07:14 -07007073 * this notification. The {@link PendingIntent} provided should be for an activity.
7074 *
7075 * <pre class="prettyprint">
7076 * Intent displayIntent = new Intent(context, MyDisplayActivity.class);
7077 * PendingIntent displayPendingIntent = PendingIntent.getActivity(context,
7078 * 0, displayIntent, PendingIntent.FLAG_UPDATE_CURRENT);
7079 * Notification notif = new Notification.Builder(context)
7080 * .extend(new Notification.WearableExtender()
7081 * .setDisplayIntent(displayPendingIntent)
7082 * .setCustomSizePreset(Notification.WearableExtender.SIZE_MEDIUM))
7083 * .build();</pre>
7084 *
7085 * <p>The activity to launch needs to allow embedding, must be exported, and
Griff Hazen831ca9d2014-06-17 00:38:38 -07007086 * should have an empty task affinity. It is also recommended to use the device
7087 * default light theme.
Griff Hazen14f57992014-05-26 09:07:14 -07007088 *
7089 * <p>Example AndroidManifest.xml entry:
7090 * <pre class="prettyprint">
7091 * &lt;activity android:name=&quot;com.example.MyDisplayActivity&quot;
7092 * android:exported=&quot;true&quot;
7093 * android:allowEmbedded=&quot;true&quot;
Griff Hazen831ca9d2014-06-17 00:38:38 -07007094 * android:taskAffinity=&quot;&quot;
7095 * android:theme=&quot;@android:style/Theme.DeviceDefault.Light&quot; /&gt;</pre>
Griff Hazen61a9e862014-05-22 16:05:19 -07007096 *
7097 * @param intent the {@link PendingIntent} for an activity
7098 * @return this object for method chaining
7099 * @see android.app.Notification.WearableExtender#getDisplayIntent
7100 */
7101 public WearableExtender setDisplayIntent(PendingIntent intent) {
7102 mDisplayIntent = intent;
7103 return this;
7104 }
7105
7106 /**
7107 * Get the intent to launch inside of an activity view when displaying this
7108 * notification. This {@code PendingIntent} should be for an activity.
7109 */
7110 public PendingIntent getDisplayIntent() {
7111 return mDisplayIntent;
7112 }
7113
7114 /**
7115 * Add an additional page of content to display with this notification. The current
7116 * notification forms the first page, and pages added using this function form
7117 * subsequent pages. This field can be used to separate a notification into multiple
7118 * sections.
7119 *
7120 * @param page the notification to add as another page
7121 * @return this object for method chaining
7122 * @see android.app.Notification.WearableExtender#getPages
7123 */
7124 public WearableExtender addPage(Notification page) {
7125 mPages.add(page);
7126 return this;
7127 }
7128
7129 /**
7130 * Add additional pages of content to display with this notification. The current
7131 * notification forms the first page, and pages added using this function form
7132 * subsequent pages. This field can be used to separate a notification into multiple
7133 * sections.
7134 *
7135 * @param pages a list of notifications
7136 * @return this object for method chaining
7137 * @see android.app.Notification.WearableExtender#getPages
7138 */
7139 public WearableExtender addPages(List<Notification> pages) {
7140 mPages.addAll(pages);
7141 return this;
7142 }
7143
7144 /**
7145 * Clear all additional pages present on this builder.
7146 * @return this object for method chaining.
7147 * @see #addPage
7148 */
7149 public WearableExtender clearPages() {
7150 mPages.clear();
7151 return this;
7152 }
7153
7154 /**
7155 * Get the array of additional pages of content for displaying this notification. The
7156 * current notification forms the first page, and elements within this array form
7157 * subsequent pages. This field can be used to separate a notification into multiple
7158 * sections.
7159 * @return the pages for this notification
7160 */
7161 public List<Notification> getPages() {
7162 return mPages;
7163 }
7164
7165 /**
7166 * Set a background image to be displayed behind the notification content.
7167 * Contrary to the {@link android.app.Notification.BigPictureStyle}, this background
7168 * will work with any notification style.
7169 *
7170 * @param background the background bitmap
7171 * @return this object for method chaining
7172 * @see android.app.Notification.WearableExtender#getBackground
7173 */
7174 public WearableExtender setBackground(Bitmap background) {
7175 mBackground = background;
7176 return this;
7177 }
7178
7179 /**
7180 * Get a background image to be displayed behind the notification content.
7181 * Contrary to the {@link android.app.Notification.BigPictureStyle}, this background
7182 * will work with any notification style.
7183 *
7184 * @return the background image
7185 * @see android.app.Notification.WearableExtender#setBackground
7186 */
7187 public Bitmap getBackground() {
7188 return mBackground;
7189 }
7190
7191 /**
7192 * Set an icon that goes with the content of this notification.
7193 */
7194 public WearableExtender setContentIcon(int icon) {
7195 mContentIcon = icon;
7196 return this;
7197 }
7198
7199 /**
7200 * Get an icon that goes with the content of this notification.
7201 */
7202 public int getContentIcon() {
7203 return mContentIcon;
7204 }
7205
7206 /**
7207 * Set the gravity that the content icon should have within the notification display.
7208 * Supported values include {@link android.view.Gravity#START} and
7209 * {@link android.view.Gravity#END}. The default value is {@link android.view.Gravity#END}.
7210 * @see #setContentIcon
7211 */
7212 public WearableExtender setContentIconGravity(int contentIconGravity) {
7213 mContentIconGravity = contentIconGravity;
7214 return this;
7215 }
7216
7217 /**
7218 * Get the gravity that the content icon should have within the notification display.
7219 * Supported values include {@link android.view.Gravity#START} and
7220 * {@link android.view.Gravity#END}. The default value is {@link android.view.Gravity#END}.
7221 * @see #getContentIcon
7222 */
7223 public int getContentIconGravity() {
7224 return mContentIconGravity;
7225 }
7226
7227 /**
7228 * Set an action from this notification's actions to be clickable with the content of
Griff Hazen14f57992014-05-26 09:07:14 -07007229 * this notification. This action will no longer display separately from the
7230 * notification's content.
7231 *
Griff Hazenca48d352014-05-28 22:37:13 -07007232 * <p>For notifications with multiple pages, child pages can also have content actions
Griff Hazen14f57992014-05-26 09:07:14 -07007233 * set, although the list of available actions comes from the main notification and not
7234 * from the child page's notification.
7235 *
7236 * @param actionIndex The index of the action to hoist onto the current notification page.
7237 * If wearable actions were added to the main notification, this index
7238 * will apply to that list, otherwise it will apply to the regular
7239 * actions list.
Griff Hazen61a9e862014-05-22 16:05:19 -07007240 */
7241 public WearableExtender setContentAction(int actionIndex) {
7242 mContentActionIndex = actionIndex;
7243 return this;
7244 }
7245
7246 /**
Griff Hazenca48d352014-05-28 22:37:13 -07007247 * Get the index of the notification action, if any, that was specified as being clickable
7248 * with the content of this notification. This action will no longer display separately
Griff Hazen14f57992014-05-26 09:07:14 -07007249 * from the notification's content.
Griff Hazen61a9e862014-05-22 16:05:19 -07007250 *
Griff Hazenca48d352014-05-28 22:37:13 -07007251 * <p>For notifications with multiple pages, child pages can also have content actions
Griff Hazen14f57992014-05-26 09:07:14 -07007252 * set, although the list of available actions comes from the main notification and not
7253 * from the child page's notification.
7254 *
7255 * <p>If wearable specific actions were added to the main notification, this index will
7256 * apply to that list, otherwise it will apply to the regular actions list.
Griff Hazenca48d352014-05-28 22:37:13 -07007257 *
7258 * @return the action index or {@link #UNSET_ACTION_INDEX} if no action was selected.
Griff Hazen61a9e862014-05-22 16:05:19 -07007259 */
7260 public int getContentAction() {
7261 return mContentActionIndex;
7262 }
7263
7264 /**
7265 * Set the gravity that this notification should have within the available viewport space.
7266 * Supported values include {@link android.view.Gravity#TOP},
7267 * {@link android.view.Gravity#CENTER_VERTICAL} and {@link android.view.Gravity#BOTTOM}.
7268 * The default value is {@link android.view.Gravity#BOTTOM}.
7269 */
7270 public WearableExtender setGravity(int gravity) {
7271 mGravity = gravity;
7272 return this;
7273 }
7274
7275 /**
7276 * Get the gravity that this notification should have within the available viewport space.
7277 * Supported values include {@link android.view.Gravity#TOP},
7278 * {@link android.view.Gravity#CENTER_VERTICAL} and {@link android.view.Gravity#BOTTOM}.
7279 * The default value is {@link android.view.Gravity#BOTTOM}.
7280 */
7281 public int getGravity() {
7282 return mGravity;
7283 }
7284
7285 /**
7286 * Set the custom size preset for the display of this notification out of the available
7287 * presets found in {@link android.app.Notification.WearableExtender}, e.g.
7288 * {@link #SIZE_LARGE}.
7289 * <p>Some custom size presets are only applicable for custom display notifications created
7290 * using {@link android.app.Notification.WearableExtender#setDisplayIntent}. Check the
7291 * documentation for the preset in question. See also
7292 * {@link #setCustomContentHeight} and {@link #getCustomSizePreset}.
7293 */
7294 public WearableExtender setCustomSizePreset(int sizePreset) {
7295 mCustomSizePreset = sizePreset;
7296 return this;
7297 }
7298
7299 /**
7300 * Get the custom size preset for the display of this notification out of the available
7301 * presets found in {@link android.app.Notification.WearableExtender}, e.g.
7302 * {@link #SIZE_LARGE}.
7303 * <p>Some custom size presets are only applicable for custom display notifications created
7304 * using {@link #setDisplayIntent}. Check the documentation for the preset in question.
7305 * See also {@link #setCustomContentHeight} and {@link #setCustomSizePreset}.
7306 */
7307 public int getCustomSizePreset() {
7308 return mCustomSizePreset;
7309 }
7310
7311 /**
7312 * Set the custom height in pixels for the display of this notification's content.
7313 * <p>This option is only available for custom display notifications created
7314 * using {@link android.app.Notification.WearableExtender#setDisplayIntent}. See also
7315 * {@link android.app.Notification.WearableExtender#setCustomSizePreset} and
7316 * {@link #getCustomContentHeight}.
7317 */
7318 public WearableExtender setCustomContentHeight(int height) {
7319 mCustomContentHeight = height;
7320 return this;
7321 }
7322
7323 /**
7324 * Get the custom height in pixels for the display of this notification's content.
7325 * <p>This option is only available for custom display notifications created
7326 * using {@link #setDisplayIntent}. See also {@link #setCustomSizePreset} and
7327 * {@link #setCustomContentHeight}.
7328 */
7329 public int getCustomContentHeight() {
7330 return mCustomContentHeight;
7331 }
7332
7333 /**
7334 * Set whether the scrolling position for the contents of this notification should start
7335 * at the bottom of the contents instead of the top when the contents are too long to
7336 * display within the screen. Default is false (start scroll at the top).
7337 */
7338 public WearableExtender setStartScrollBottom(boolean startScrollBottom) {
7339 setFlag(FLAG_START_SCROLL_BOTTOM, startScrollBottom);
7340 return this;
7341 }
7342
7343 /**
7344 * Get whether the scrolling position for the contents of this notification should start
7345 * at the bottom of the contents instead of the top when the contents are too long to
7346 * display within the screen. Default is false (start scroll at the top).
7347 */
7348 public boolean getStartScrollBottom() {
7349 return (mFlags & FLAG_START_SCROLL_BOTTOM) != 0;
7350 }
7351
7352 /**
7353 * Set whether the content intent is available when the wearable device is not connected
7354 * to a companion device. The user can still trigger this intent when the wearable device
7355 * is offline, but a visual hint will indicate that the content intent may not be available.
7356 * Defaults to true.
7357 */
7358 public WearableExtender setContentIntentAvailableOffline(
7359 boolean contentIntentAvailableOffline) {
7360 setFlag(FLAG_CONTENT_INTENT_AVAILABLE_OFFLINE, contentIntentAvailableOffline);
7361 return this;
7362 }
7363
7364 /**
7365 * Get whether the content intent is available when the wearable device is not connected
7366 * to a companion device. The user can still trigger this intent when the wearable device
7367 * is offline, but a visual hint will indicate that the content intent may not be available.
7368 * Defaults to true.
7369 */
7370 public boolean getContentIntentAvailableOffline() {
7371 return (mFlags & FLAG_CONTENT_INTENT_AVAILABLE_OFFLINE) != 0;
7372 }
7373
7374 /**
7375 * Set a hint that this notification's icon should not be displayed.
7376 * @param hintHideIcon {@code true} to hide the icon, {@code false} otherwise.
7377 * @return this object for method chaining
7378 */
7379 public WearableExtender setHintHideIcon(boolean hintHideIcon) {
7380 setFlag(FLAG_HINT_HIDE_ICON, hintHideIcon);
7381 return this;
7382 }
7383
7384 /**
7385 * Get a hint that this notification's icon should not be displayed.
7386 * @return {@code true} if this icon should not be displayed, false otherwise.
7387 * The default value is {@code false} if this was never set.
7388 */
7389 public boolean getHintHideIcon() {
7390 return (mFlags & FLAG_HINT_HIDE_ICON) != 0;
7391 }
7392
7393 /**
7394 * Set a visual hint that only the background image of this notification should be
7395 * displayed, and other semantic content should be hidden. This hint is only applicable
7396 * to sub-pages added using {@link #addPage}.
7397 */
7398 public WearableExtender setHintShowBackgroundOnly(boolean hintShowBackgroundOnly) {
7399 setFlag(FLAG_HINT_SHOW_BACKGROUND_ONLY, hintShowBackgroundOnly);
7400 return this;
7401 }
7402
7403 /**
7404 * Get a visual hint that only the background image of this notification should be
7405 * displayed, and other semantic content should be hidden. This hint is only applicable
7406 * to sub-pages added using {@link android.app.Notification.WearableExtender#addPage}.
7407 */
7408 public boolean getHintShowBackgroundOnly() {
7409 return (mFlags & FLAG_HINT_SHOW_BACKGROUND_ONLY) != 0;
7410 }
7411
Griff Hazen5f2edfc2014-09-29 16:28:44 -07007412 /**
Griff Hazen9c5be4e2014-11-17 17:47:50 -08007413 * Set a hint that this notification's background should not be clipped if possible,
7414 * and should instead be resized to fully display on the screen, retaining the aspect
7415 * ratio of the image. This can be useful for images like barcodes or qr codes.
Griff Hazen5f2edfc2014-09-29 16:28:44 -07007416 * @param hintAvoidBackgroundClipping {@code true} to avoid clipping if possible.
7417 * @return this object for method chaining
7418 */
7419 public WearableExtender setHintAvoidBackgroundClipping(
7420 boolean hintAvoidBackgroundClipping) {
7421 setFlag(FLAG_HINT_AVOID_BACKGROUND_CLIPPING, hintAvoidBackgroundClipping);
7422 return this;
7423 }
7424
7425 /**
Griff Hazen9c5be4e2014-11-17 17:47:50 -08007426 * Get a hint that this notification's background should not be clipped if possible,
7427 * and should instead be resized to fully display on the screen, retaining the aspect
7428 * ratio of the image. This can be useful for images like barcodes or qr codes.
Griff Hazen5f2edfc2014-09-29 16:28:44 -07007429 * @return {@code true} if it's ok if the background is clipped on the screen, false
7430 * otherwise. The default value is {@code false} if this was never set.
7431 */
7432 public boolean getHintAvoidBackgroundClipping() {
7433 return (mFlags & FLAG_HINT_AVOID_BACKGROUND_CLIPPING) != 0;
7434 }
7435
7436 /**
7437 * Set a hint that the screen should remain on for at least this duration when
7438 * this notification is displayed on the screen.
7439 * @param timeout The requested screen timeout in milliseconds. Can also be either
7440 * {@link #SCREEN_TIMEOUT_SHORT} or {@link #SCREEN_TIMEOUT_LONG}.
7441 * @return this object for method chaining
7442 */
7443 public WearableExtender setHintScreenTimeout(int timeout) {
7444 mHintScreenTimeout = timeout;
7445 return this;
7446 }
7447
7448 /**
7449 * Get the duration, in milliseconds, that the screen should remain on for
7450 * when this notification is displayed.
7451 * @return the duration in milliseconds if > 0, or either one of the sentinel values
7452 * {@link #SCREEN_TIMEOUT_SHORT} or {@link #SCREEN_TIMEOUT_LONG}.
7453 */
7454 public int getHintScreenTimeout() {
7455 return mHintScreenTimeout;
7456 }
7457
Alex Hills9ab3a232016-04-05 14:54:56 -04007458 /**
Alex Hills4bcb06b2016-04-05 14:26:25 -04007459 * Set a hint that this notification's {@link BigPictureStyle} (if present) should be
7460 * converted to low-bit and displayed in ambient mode, especially useful for barcodes and
7461 * qr codes, as well as other simple black-and-white tickets.
7462 * @param hintAmbientBigPicture {@code true} to enable converstion and ambient.
7463 * @return this object for method chaining
7464 */
7465 public WearableExtender setHintAmbientBigPicture(boolean hintAmbientBigPicture) {
7466 setFlag(FLAG_BIG_PICTURE_AMBIENT, hintAmbientBigPicture);
7467 return this;
7468 }
7469
7470 /**
7471 * Get a hint that this notification's {@link BigPictureStyle} (if present) should be
7472 * converted to low-bit and displayed in ambient mode, especially useful for barcodes and
7473 * qr codes, as well as other simple black-and-white tickets.
7474 * @return {@code true} if it should be displayed in ambient, false otherwise
7475 * otherwise. The default value is {@code false} if this was never set.
7476 */
7477 public boolean getHintAmbientBigPicture() {
7478 return (mFlags & FLAG_BIG_PICTURE_AMBIENT) != 0;
7479 }
7480
7481 /**
Alex Hills9ab3a232016-04-05 14:54:56 -04007482 * Set a hint that this notification's content intent will launch an {@link Activity}
7483 * directly, telling the platform that it can generate the appropriate transitions.
7484 * @param hintContentIntentLaunchesActivity {@code true} if the content intent will launch
7485 * an activity and transitions should be generated, false otherwise.
7486 * @return this object for method chaining
7487 */
7488 public WearableExtender setHintContentIntentLaunchesActivity(
7489 boolean hintContentIntentLaunchesActivity) {
7490 setFlag(FLAG_HINT_CONTENT_INTENT_LAUNCHES_ACTIVITY, hintContentIntentLaunchesActivity);
7491 return this;
7492 }
7493
7494 /**
7495 * Get a hint that this notification's content intent will launch an {@link Activity}
7496 * directly, telling the platform that it can generate the appropriate transitions
7497 * @return {@code true} if the content intent will launch an activity and transitions should
7498 * be generated, false otherwise. The default value is {@code false} if this was never set.
7499 */
7500 public boolean getHintContentIntentLaunchesActivity() {
7501 return (mFlags & FLAG_HINT_CONTENT_INTENT_LAUNCHES_ACTIVITY) != 0;
7502 }
7503
Nadia Benbernou948627e2016-04-14 14:41:08 -04007504 /**
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04007505 * Sets the dismissal id for this notification. If a notification is posted with a
7506 * dismissal id, then when that notification is canceled, notifications on other wearables
7507 * and the paired Android phone having that same dismissal id will also be canceled. See
Nadia Benbernou948627e2016-04-14 14:41:08 -04007508 * <a href="{@docRoot}wear/notifications/index.html">Adding Wearable Features to
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04007509 * Notifications</a> for more information.
Nadia Benbernou948627e2016-04-14 14:41:08 -04007510 * @param dismissalId the dismissal id of the notification.
7511 * @return this object for method chaining
7512 */
7513 public WearableExtender setDismissalId(String dismissalId) {
7514 mDismissalId = dismissalId;
7515 return this;
7516 }
7517
7518 /**
7519 * Returns the dismissal id of the notification.
7520 * @return the dismissal id of the notification or null if it has not been set.
7521 */
7522 public String getDismissalId() {
7523 return mDismissalId;
7524 }
7525
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04007526 /**
7527 * Sets a bridge tag for this notification. A bridge tag can be set for notifications
7528 * posted from a phone to provide finer-grained control on what notifications are bridged
7529 * to wearables. See <a href="{@docRoot}wear/notifications/index.html">Adding Wearable
7530 * Features to Notifications</a> for more information.
7531 * @param bridgeTag the bridge tag of the notification.
7532 * @return this object for method chaining
7533 */
7534 public WearableExtender setBridgeTag(String bridgeTag) {
7535 mBridgeTag = bridgeTag;
7536 return this;
7537 }
7538
7539 /**
7540 * Returns the bridge tag of the notification.
7541 * @return the bridge tag or null if not present.
7542 */
7543 public String getBridgeTag() {
7544 return mBridgeTag;
7545 }
7546
Griff Hazen61a9e862014-05-22 16:05:19 -07007547 private void setFlag(int mask, boolean value) {
7548 if (value) {
7549 mFlags |= mask;
7550 } else {
7551 mFlags &= ~mask;
7552 }
7553 }
7554 }
7555
7556 /**
Zhen Yu Song9d5528b2014-11-14 15:34:39 -08007557 * <p>Helper class to add Android Auto extensions to notifications. To create a notification
7558 * with car extensions:
7559 *
7560 * <ol>
7561 * <li>Create an {@link Notification.Builder}, setting any desired
7562 * properties.
7563 * <li>Create a {@link CarExtender}.
7564 * <li>Set car-specific properties using the {@code add} and {@code set} methods of
7565 * {@link CarExtender}.
7566 * <li>Call {@link Notification.Builder#extend(Notification.Extender)}
7567 * to apply the extensions to a notification.
7568 * </ol>
7569 *
7570 * <pre class="prettyprint">
7571 * Notification notification = new Notification.Builder(context)
7572 * ...
7573 * .extend(new CarExtender()
7574 * .set*(...))
7575 * .build();
7576 * </pre>
7577 *
7578 * <p>Car extensions can be accessed on an existing notification by using the
7579 * {@code CarExtender(Notification)} constructor, and then using the {@code get} methods
7580 * to access values.
7581 */
7582 public static final class CarExtender implements Extender {
7583 private static final String TAG = "CarExtender";
7584
7585 private static final String EXTRA_CAR_EXTENDER = "android.car.EXTENSIONS";
7586 private static final String EXTRA_LARGE_ICON = "large_icon";
7587 private static final String EXTRA_CONVERSATION = "car_conversation";
7588 private static final String EXTRA_COLOR = "app_color";
7589
7590 private Bitmap mLargeIcon;
7591 private UnreadConversation mUnreadConversation;
7592 private int mColor = Notification.COLOR_DEFAULT;
7593
7594 /**
7595 * Create a {@link CarExtender} with default options.
7596 */
7597 public CarExtender() {
7598 }
7599
7600 /**
7601 * Create a {@link CarExtender} from the CarExtender options of an existing Notification.
7602 *
7603 * @param notif The notification from which to copy options.
7604 */
7605 public CarExtender(Notification notif) {
7606 Bundle carBundle = notif.extras == null ?
7607 null : notif.extras.getBundle(EXTRA_CAR_EXTENDER);
7608 if (carBundle != null) {
7609 mLargeIcon = carBundle.getParcelable(EXTRA_LARGE_ICON);
7610 mColor = carBundle.getInt(EXTRA_COLOR, Notification.COLOR_DEFAULT);
7611
7612 Bundle b = carBundle.getBundle(EXTRA_CONVERSATION);
7613 mUnreadConversation = UnreadConversation.getUnreadConversationFromBundle(b);
7614 }
7615 }
7616
7617 /**
7618 * Apply car extensions to a notification that is being built. This is typically called by
7619 * the {@link Notification.Builder#extend(Notification.Extender)}
7620 * method of {@link Notification.Builder}.
7621 */
7622 @Override
7623 public Notification.Builder extend(Notification.Builder builder) {
7624 Bundle carExtensions = new Bundle();
7625
7626 if (mLargeIcon != null) {
7627 carExtensions.putParcelable(EXTRA_LARGE_ICON, mLargeIcon);
7628 }
7629 if (mColor != Notification.COLOR_DEFAULT) {
7630 carExtensions.putInt(EXTRA_COLOR, mColor);
7631 }
7632
7633 if (mUnreadConversation != null) {
7634 Bundle b = mUnreadConversation.getBundleForUnreadConversation();
7635 carExtensions.putBundle(EXTRA_CONVERSATION, b);
7636 }
7637
7638 builder.getExtras().putBundle(EXTRA_CAR_EXTENDER, carExtensions);
7639 return builder;
7640 }
7641
7642 /**
7643 * Sets the accent color to use when Android Auto presents the notification.
7644 *
7645 * Android Auto uses the color set with {@link Notification.Builder#setColor(int)}
7646 * to accent the displayed notification. However, not all colors are acceptable in an
7647 * automotive setting. This method can be used to override the color provided in the
7648 * notification in such a situation.
7649 */
Tor Norbye80756e32015-03-02 09:39:27 -08007650 public CarExtender setColor(@ColorInt int color) {
Zhen Yu Song9d5528b2014-11-14 15:34:39 -08007651 mColor = color;
7652 return this;
7653 }
7654
7655 /**
7656 * Gets the accent color.
7657 *
Julia Reynoldsd9228f12015-10-20 10:37:27 -04007658 * @see #setColor
Zhen Yu Song9d5528b2014-11-14 15:34:39 -08007659 */
Tor Norbye80756e32015-03-02 09:39:27 -08007660 @ColorInt
Zhen Yu Song9d5528b2014-11-14 15:34:39 -08007661 public int getColor() {
7662 return mColor;
7663 }
7664
7665 /**
7666 * Sets the large icon of the car notification.
7667 *
7668 * If no large icon is set in the extender, Android Auto will display the icon
7669 * specified by {@link Notification.Builder#setLargeIcon(android.graphics.Bitmap)}
7670 *
7671 * @param largeIcon The large icon to use in the car notification.
7672 * @return This object for method chaining.
7673 */
7674 public CarExtender setLargeIcon(Bitmap largeIcon) {
7675 mLargeIcon = largeIcon;
7676 return this;
7677 }
7678
7679 /**
7680 * Gets the large icon used in this car notification, or null if no icon has been set.
7681 *
7682 * @return The large icon for the car notification.
7683 * @see CarExtender#setLargeIcon
7684 */
7685 public Bitmap getLargeIcon() {
7686 return mLargeIcon;
7687 }
7688
7689 /**
7690 * Sets the unread conversation in a message notification.
7691 *
7692 * @param unreadConversation The unread part of the conversation this notification conveys.
7693 * @return This object for method chaining.
7694 */
7695 public CarExtender setUnreadConversation(UnreadConversation unreadConversation) {
7696 mUnreadConversation = unreadConversation;
7697 return this;
7698 }
7699
7700 /**
7701 * Returns the unread conversation conveyed by this notification.
7702 * @see #setUnreadConversation(UnreadConversation)
7703 */
7704 public UnreadConversation getUnreadConversation() {
7705 return mUnreadConversation;
7706 }
7707
7708 /**
7709 * A class which holds the unread messages from a conversation.
7710 */
7711 public static class UnreadConversation {
7712 private static final String KEY_AUTHOR = "author";
7713 private static final String KEY_TEXT = "text";
7714 private static final String KEY_MESSAGES = "messages";
7715 private static final String KEY_REMOTE_INPUT = "remote_input";
7716 private static final String KEY_ON_REPLY = "on_reply";
7717 private static final String KEY_ON_READ = "on_read";
7718 private static final String KEY_PARTICIPANTS = "participants";
7719 private static final String KEY_TIMESTAMP = "timestamp";
7720
7721 private final String[] mMessages;
7722 private final RemoteInput mRemoteInput;
7723 private final PendingIntent mReplyPendingIntent;
7724 private final PendingIntent mReadPendingIntent;
7725 private final String[] mParticipants;
7726 private final long mLatestTimestamp;
7727
7728 UnreadConversation(String[] messages, RemoteInput remoteInput,
7729 PendingIntent replyPendingIntent, PendingIntent readPendingIntent,
7730 String[] participants, long latestTimestamp) {
7731 mMessages = messages;
7732 mRemoteInput = remoteInput;
7733 mReadPendingIntent = readPendingIntent;
7734 mReplyPendingIntent = replyPendingIntent;
7735 mParticipants = participants;
7736 mLatestTimestamp = latestTimestamp;
7737 }
7738
7739 /**
7740 * Gets the list of messages conveyed by this notification.
7741 */
7742 public String[] getMessages() {
7743 return mMessages;
7744 }
7745
7746 /**
7747 * Gets the remote input that will be used to convey the response to a message list, or
7748 * null if no such remote input exists.
7749 */
7750 public RemoteInput getRemoteInput() {
7751 return mRemoteInput;
7752 }
7753
7754 /**
7755 * Gets the pending intent that will be triggered when the user replies to this
7756 * notification.
7757 */
7758 public PendingIntent getReplyPendingIntent() {
7759 return mReplyPendingIntent;
7760 }
7761
7762 /**
7763 * Gets the pending intent that Android Auto will send after it reads aloud all messages
7764 * in this object's message list.
7765 */
7766 public PendingIntent getReadPendingIntent() {
7767 return mReadPendingIntent;
7768 }
7769
7770 /**
7771 * Gets the participants in the conversation.
7772 */
7773 public String[] getParticipants() {
7774 return mParticipants;
7775 }
7776
7777 /**
7778 * Gets the firs participant in the conversation.
7779 */
7780 public String getParticipant() {
7781 return mParticipants.length > 0 ? mParticipants[0] : null;
7782 }
7783
7784 /**
7785 * Gets the timestamp of the conversation.
7786 */
7787 public long getLatestTimestamp() {
7788 return mLatestTimestamp;
7789 }
7790
7791 Bundle getBundleForUnreadConversation() {
7792 Bundle b = new Bundle();
7793 String author = null;
7794 if (mParticipants != null && mParticipants.length > 1) {
7795 author = mParticipants[0];
7796 }
7797 Parcelable[] messages = new Parcelable[mMessages.length];
7798 for (int i = 0; i < messages.length; i++) {
7799 Bundle m = new Bundle();
7800 m.putString(KEY_TEXT, mMessages[i]);
7801 m.putString(KEY_AUTHOR, author);
7802 messages[i] = m;
7803 }
7804 b.putParcelableArray(KEY_MESSAGES, messages);
7805 if (mRemoteInput != null) {
7806 b.putParcelable(KEY_REMOTE_INPUT, mRemoteInput);
7807 }
7808 b.putParcelable(KEY_ON_REPLY, mReplyPendingIntent);
7809 b.putParcelable(KEY_ON_READ, mReadPendingIntent);
7810 b.putStringArray(KEY_PARTICIPANTS, mParticipants);
7811 b.putLong(KEY_TIMESTAMP, mLatestTimestamp);
7812 return b;
7813 }
7814
7815 static UnreadConversation getUnreadConversationFromBundle(Bundle b) {
7816 if (b == null) {
7817 return null;
7818 }
7819 Parcelable[] parcelableMessages = b.getParcelableArray(KEY_MESSAGES);
7820 String[] messages = null;
7821 if (parcelableMessages != null) {
7822 String[] tmp = new String[parcelableMessages.length];
7823 boolean success = true;
7824 for (int i = 0; i < tmp.length; i++) {
7825 if (!(parcelableMessages[i] instanceof Bundle)) {
7826 success = false;
7827 break;
7828 }
7829 tmp[i] = ((Bundle) parcelableMessages[i]).getString(KEY_TEXT);
7830 if (tmp[i] == null) {
7831 success = false;
7832 break;
7833 }
7834 }
7835 if (success) {
7836 messages = tmp;
7837 } else {
7838 return null;
7839 }
7840 }
7841
7842 PendingIntent onRead = b.getParcelable(KEY_ON_READ);
7843 PendingIntent onReply = b.getParcelable(KEY_ON_REPLY);
7844
7845 RemoteInput remoteInput = b.getParcelable(KEY_REMOTE_INPUT);
7846
7847 String[] participants = b.getStringArray(KEY_PARTICIPANTS);
7848 if (participants == null || participants.length != 1) {
7849 return null;
7850 }
7851
7852 return new UnreadConversation(messages,
7853 remoteInput,
7854 onReply,
7855 onRead,
7856 participants, b.getLong(KEY_TIMESTAMP));
7857 }
7858 };
7859
7860 /**
7861 * Builder class for {@link CarExtender.UnreadConversation} objects.
7862 */
7863 public static class Builder {
7864 private final List<String> mMessages = new ArrayList<String>();
7865 private final String mParticipant;
7866 private RemoteInput mRemoteInput;
7867 private PendingIntent mReadPendingIntent;
7868 private PendingIntent mReplyPendingIntent;
7869 private long mLatestTimestamp;
7870
7871 /**
7872 * Constructs a new builder for {@link CarExtender.UnreadConversation}.
7873 *
7874 * @param name The name of the other participant in the conversation.
7875 */
7876 public Builder(String name) {
7877 mParticipant = name;
7878 }
7879
7880 /**
7881 * Appends a new unread message to the list of messages for this conversation.
7882 *
7883 * The messages should be added from oldest to newest.
7884 *
7885 * @param message The text of the new unread message.
7886 * @return This object for method chaining.
7887 */
7888 public Builder addMessage(String message) {
7889 mMessages.add(message);
7890 return this;
7891 }
7892
7893 /**
7894 * Sets the pending intent and remote input which will convey the reply to this
7895 * notification.
7896 *
7897 * @param pendingIntent The pending intent which will be triggered on a reply.
7898 * @param remoteInput The remote input parcelable which will carry the reply.
7899 * @return This object for method chaining.
7900 *
7901 * @see CarExtender.UnreadConversation#getRemoteInput
7902 * @see CarExtender.UnreadConversation#getReplyPendingIntent
7903 */
7904 public Builder setReplyAction(
7905 PendingIntent pendingIntent, RemoteInput remoteInput) {
7906 mRemoteInput = remoteInput;
7907 mReplyPendingIntent = pendingIntent;
7908
7909 return this;
7910 }
7911
7912 /**
7913 * Sets the pending intent that will be sent once the messages in this notification
7914 * are read.
7915 *
7916 * @param pendingIntent The pending intent to use.
7917 * @return This object for method chaining.
7918 */
7919 public Builder setReadPendingIntent(PendingIntent pendingIntent) {
7920 mReadPendingIntent = pendingIntent;
7921 return this;
7922 }
7923
7924 /**
7925 * Sets the timestamp of the most recent message in an unread conversation.
7926 *
7927 * If a messaging notification has been posted by your application and has not
7928 * yet been cancelled, posting a later notification with the same id and tag
7929 * but without a newer timestamp may result in Android Auto not displaying a
7930 * heads up notification for the later notification.
7931 *
7932 * @param timestamp The timestamp of the most recent message in the conversation.
7933 * @return This object for method chaining.
7934 */
7935 public Builder setLatestTimestamp(long timestamp) {
7936 mLatestTimestamp = timestamp;
7937 return this;
7938 }
7939
7940 /**
7941 * Builds a new unread conversation object.
7942 *
7943 * @return The new unread conversation object.
7944 */
7945 public UnreadConversation build() {
7946 String[] messages = mMessages.toArray(new String[mMessages.size()]);
7947 String[] participants = { mParticipant };
7948 return new UnreadConversation(messages, mRemoteInput, mReplyPendingIntent,
7949 mReadPendingIntent, participants, mLatestTimestamp);
7950 }
7951 }
7952 }
7953
7954 /**
Dmitri Plotnikov9e9cfd92016-12-21 10:52:26 -08007955 * <p>Helper class to add Android TV extensions to notifications. To create a notification
7956 * with a TV extension:
7957 *
7958 * <ol>
7959 * <li>Create an {@link Notification.Builder}, setting any desired properties.
7960 * <li>Create a {@link TvExtender}.
7961 * <li>Set TV-specific properties using the {@code set} methods of
7962 * {@link TvExtender}.
7963 * <li>Call {@link Notification.Builder#extend(Notification.Extender)}
7964 * to apply the extension to a notification.
7965 * </ol>
7966 *
7967 * <pre class="prettyprint">
7968 * Notification notification = new Notification.Builder(context)
7969 * ...
7970 * .extend(new TvExtender()
7971 * .set*(...))
7972 * .build();
7973 * </pre>
7974 *
7975 * <p>TV extensions can be accessed on an existing notification by using the
7976 * {@code TvExtender(Notification)} constructor, and then using the {@code get} methods
7977 * to access values.
7978 *
7979 * @hide
7980 */
7981 @SystemApi
7982 public static final class TvExtender implements Extender {
7983 private static final String TAG = "TvExtender";
7984
7985 private static final String EXTRA_TV_EXTENDER = "android.tv.EXTENSIONS";
7986 private static final String EXTRA_FLAGS = "flags";
7987 private static final String EXTRA_CONTENT_INTENT = "content_intent";
7988 private static final String EXTRA_DELETE_INTENT = "delete_intent";
Dmitri Plotnikovb8a04ee2017-01-20 10:39:22 -08007989 private static final String EXTRA_CHANNEL_ID = "channel_id";
Dmitri Plotnikov9e9cfd92016-12-21 10:52:26 -08007990
7991 // Flags bitwise-ored to mFlags
7992 private static final int FLAG_AVAILABLE_ON_TV = 0x1;
7993
7994 private int mFlags;
Dmitri Plotnikovb8a04ee2017-01-20 10:39:22 -08007995 private String mChannelId;
Dmitri Plotnikov9e9cfd92016-12-21 10:52:26 -08007996 private PendingIntent mContentIntent;
7997 private PendingIntent mDeleteIntent;
7998
7999 /**
8000 * Create a {@link TvExtender} with default options.
8001 */
8002 public TvExtender() {
8003 mFlags = FLAG_AVAILABLE_ON_TV;
8004 }
8005
8006 /**
8007 * Create a {@link TvExtender} from the TvExtender options of an existing Notification.
8008 *
8009 * @param notif The notification from which to copy options.
8010 */
8011 public TvExtender(Notification notif) {
8012 Bundle bundle = notif.extras == null ?
8013 null : notif.extras.getBundle(EXTRA_TV_EXTENDER);
8014 if (bundle != null) {
8015 mFlags = bundle.getInt(EXTRA_FLAGS);
Dmitri Plotnikovb8a04ee2017-01-20 10:39:22 -08008016 mChannelId = bundle.getString(EXTRA_CHANNEL_ID);
Dmitri Plotnikov9e9cfd92016-12-21 10:52:26 -08008017 mContentIntent = bundle.getParcelable(EXTRA_CONTENT_INTENT);
8018 mDeleteIntent = bundle.getParcelable(EXTRA_DELETE_INTENT);
8019 }
8020 }
8021
8022 /**
8023 * Apply a TV extension to a notification that is being built. This is typically called by
8024 * the {@link Notification.Builder#extend(Notification.Extender)}
8025 * method of {@link Notification.Builder}.
8026 */
8027 @Override
8028 public Notification.Builder extend(Notification.Builder builder) {
8029 Bundle bundle = new Bundle();
8030
8031 bundle.putInt(EXTRA_FLAGS, mFlags);
Dmitri Plotnikovb8a04ee2017-01-20 10:39:22 -08008032 bundle.putString(EXTRA_CHANNEL_ID, mChannelId);
Dmitri Plotnikov9e9cfd92016-12-21 10:52:26 -08008033 if (mContentIntent != null) {
8034 bundle.putParcelable(EXTRA_CONTENT_INTENT, mContentIntent);
8035 }
8036
8037 if (mDeleteIntent != null) {
8038 bundle.putParcelable(EXTRA_DELETE_INTENT, mDeleteIntent);
8039 }
8040
8041 builder.getExtras().putBundle(EXTRA_TV_EXTENDER, bundle);
8042 return builder;
8043 }
8044
8045 /**
8046 * Returns true if this notification should be shown on TV. This method return true
8047 * if the notification was extended with a TvExtender.
8048 */
8049 public boolean isAvailableOnTv() {
8050 return (mFlags & FLAG_AVAILABLE_ON_TV) != 0;
8051 }
8052
8053 /**
Dmitri Plotnikovb8a04ee2017-01-20 10:39:22 -08008054 * Specifies the channel the notification should be delivered on when shown on TV.
8055 * It can be different from the channel that the notification is delivered to when
8056 * posting on a non-TV device.
8057 */
8058 public TvExtender setChannel(String channelId) {
8059 mChannelId = channelId;
8060 return this;
8061 }
8062
8063 /**
Julia Reynoldsbad42972017-04-25 13:52:49 -04008064 * Specifies the channel the notification should be delivered on when shown on TV.
8065 * It can be different from the channel that the notification is delivered to when
8066 * posting on a non-TV device.
8067 */
8068 public TvExtender setChannelId(String channelId) {
8069 mChannelId = channelId;
8070 return this;
8071 }
8072
Jeff Sharkey000ce802017-04-29 13:13:27 -06008073 /** @removed */
8074 @Deprecated
Dmitri Plotnikovb8a04ee2017-01-20 10:39:22 -08008075 public String getChannel() {
8076 return mChannelId;
8077 }
8078
8079 /**
Julia Reynoldsbad42972017-04-25 13:52:49 -04008080 * Returns the id of the channel this notification posts to on TV.
8081 */
8082 public String getChannelId() {
8083 return mChannelId;
8084 }
8085
8086 /**
Dmitri Plotnikov9e9cfd92016-12-21 10:52:26 -08008087 * Supplies a {@link PendingIntent} to be sent when the notification is selected on TV.
8088 * If provided, it is used instead of the content intent specified
8089 * at the level of Notification.
8090 */
8091 public TvExtender setContentIntent(PendingIntent intent) {
8092 mContentIntent = intent;
8093 return this;
8094 }
8095
8096 /**
8097 * Returns the TV-specific content intent. If this method returns null, the
8098 * main content intent on the notification should be used.
8099 *
8100 * @see {@link Notification#contentIntent}
8101 */
8102 public PendingIntent getContentIntent() {
8103 return mContentIntent;
8104 }
8105
8106 /**
8107 * Supplies a {@link PendingIntent} to send when the notification is cleared explicitly
8108 * by the user on TV. If provided, it is used instead of the delete intent specified
8109 * at the level of Notification.
8110 */
8111 public TvExtender setDeleteIntent(PendingIntent intent) {
8112 mDeleteIntent = intent;
8113 return this;
8114 }
8115
8116 /**
8117 * Returns the TV-specific delete intent. If this method returns null, the
8118 * main delete intent on the notification should be used.
8119 *
8120 * @see {@link Notification#deleteIntent}
8121 */
8122 public PendingIntent getDeleteIntent() {
8123 return mDeleteIntent;
8124 }
8125 }
8126
8127 /**
Griff Hazen61a9e862014-05-22 16:05:19 -07008128 * Get an array of Notification objects from a parcelable array bundle field.
8129 * Update the bundle to have a typed array so fetches in the future don't need
8130 * to do an array copy.
8131 */
8132 private static Notification[] getNotificationArrayFromBundle(Bundle bundle, String key) {
8133 Parcelable[] array = bundle.getParcelableArray(key);
8134 if (array instanceof Notification[] || array == null) {
8135 return (Notification[]) array;
8136 }
8137 Notification[] typedArray = Arrays.copyOf(array, array.length,
8138 Notification[].class);
8139 bundle.putParcelableArray(key, typedArray);
8140 return typedArray;
8141 }
Christoph Studer4600f9b2014-07-22 22:44:43 +02008142
8143 private static class BuilderRemoteViews extends RemoteViews {
8144 public BuilderRemoteViews(Parcel parcel) {
8145 super(parcel);
8146 }
8147
Kenny Guy77320062014-08-27 21:37:15 +01008148 public BuilderRemoteViews(ApplicationInfo appInfo, int layoutId) {
8149 super(appInfo, layoutId);
Christoph Studer4600f9b2014-07-22 22:44:43 +02008150 }
8151
8152 @Override
8153 public BuilderRemoteViews clone() {
8154 Parcel p = Parcel.obtain();
8155 writeToParcel(p, 0);
8156 p.setDataPosition(0);
8157 BuilderRemoteViews brv = new BuilderRemoteViews(p);
8158 p.recycle();
8159 return brv;
8160 }
8161 }
Adrian Roos70d7aa32017-01-11 15:39:06 -08008162
8163 private static class StandardTemplateParams {
8164 boolean hasProgress = true;
8165 boolean ambient = false;
8166 CharSequence title;
8167 CharSequence text;
8168
8169 final StandardTemplateParams reset() {
8170 hasProgress = true;
8171 ambient = false;
8172 title = null;
8173 text = null;
8174 return this;
8175 }
8176
8177 final StandardTemplateParams hasProgress(boolean hasProgress) {
8178 this.hasProgress = hasProgress;
8179 return this;
8180 }
8181
8182 final StandardTemplateParams title(CharSequence title) {
8183 this.title = title;
8184 return this;
8185 }
8186
8187 final StandardTemplateParams text(CharSequence text) {
8188 this.text = text;
8189 return this;
8190 }
8191
8192 final StandardTemplateParams ambient(boolean ambient) {
Adrian Roos0bc3f6a2017-03-06 11:54:05 -08008193 Preconditions.checkState(title == null && text == null, "must set ambient before text");
Adrian Roos70d7aa32017-01-11 15:39:06 -08008194 this.ambient = ambient;
8195 return this;
8196 }
8197
8198 final StandardTemplateParams fillTextsFrom(Builder b) {
8199 Bundle extras = b.mN.extras;
Adrian Roos0bc3f6a2017-03-06 11:54:05 -08008200 title = b.processLegacyText(extras.getCharSequence(EXTRA_TITLE), ambient);
8201 text = b.processLegacyText(extras.getCharSequence(EXTRA_TEXT), ambient);
Adrian Roos70d7aa32017-01-11 15:39:06 -08008202 return this;
8203 }
8204 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008205}