blob: 00a8f46f7c2acce1d7dbcf7ac05c2e2ac8b4e56e [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
2 * Copyright (C) 2007 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package android.app;
18
Tor Norbye80756e32015-03-02 09:39:27 -080019import android.annotation.ColorInt;
Tor Norbye7b9c9122013-05-30 16:48:33 -070020import android.annotation.DrawableRes;
Tor Norbyed9273d62013-05-30 15:59:53 -070021import android.annotation.IntDef;
Alex Hillsfd590442016-10-07 09:52:44 -040022import android.annotation.NonNull;
Daniel Sandler01df1c62014-06-09 10:54:01 -040023import android.annotation.SdkConstant;
24import android.annotation.SdkConstant.SdkConstantType;
Julia Reynoldse46bb372016-03-17 11:05:58 -040025import android.annotation.SystemApi;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080026import android.content.Context;
27import android.content.Intent;
Kenny Guy77320062014-08-27 21:37:15 +010028import android.content.pm.ApplicationInfo;
Dan Sandler732bd6c2016-04-12 14:20:32 -040029import android.content.pm.PackageManager;
Christoph Studer4600f9b2014-07-22 22:44:43 +020030import android.content.pm.PackageManager.NameNotFoundException;
Julia Reynolds13d898c2017-02-02 12:22:05 -050031import android.content.pm.ShortcutInfo;
Jorim Jaggief72a192014-08-26 21:57:46 +020032import android.content.res.ColorStateList;
Joe Onoratoef1e7762010-09-17 18:38:38 -040033import android.graphics.Bitmap;
Kenny Guy8a0101b2014-05-08 23:34:12 +010034import android.graphics.Canvas;
Adrian Roosc1a80b02016-04-05 14:54:55 -070035import android.graphics.Color;
Jorim Jaggi5c2d8462014-03-21 17:37:00 +010036import android.graphics.PorterDuff;
Kenny Guy8a0101b2014-05-08 23:34:12 +010037import android.graphics.drawable.Drawable;
Dan Sandlerd63f9322015-05-06 15:18:49 -040038import android.graphics.drawable.Icon;
John Spurlockc0650f022014-07-19 13:22:39 -040039import android.media.AudioAttributes;
Jeff Sharkey098d5802012-04-26 17:30:34 -070040import android.media.AudioManager;
Jean-Michel Trivi2f7511f2016-11-28 15:40:27 -080041import android.media.PlayerBase;
Jeff Browndba34ba2014-06-24 20:46:03 -070042import android.media.session.MediaSession;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080043import android.net.Uri;
Daniel Sandlerdcbaf662013-04-26 16:23:09 -040044import android.os.BadParcelableException;
Christoph Studer239f8352014-08-25 15:13:18 +020045import android.os.Build;
Daniel Sandler2561b0b2012-02-13 21:04:12 -050046import android.os.Bundle;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080047import android.os.Parcel;
48import android.os.Parcelable;
Daniel Sandlera2985ed2012-04-03 16:42:00 -040049import android.os.SystemClock;
Selim Cinek6743c0b2017-01-18 18:24:01 -080050import android.os.SystemProperties;
Jeff Sharkey6d515712012-09-20 16:06:08 -070051import android.os.UserHandle;
Adrian Roosc1a80b02016-04-05 14:54:55 -070052import android.text.BidiFormatter;
Selim Cinek60a54252016-02-26 17:03:25 -080053import android.text.SpannableStringBuilder;
54import android.text.Spanned;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080055import android.text.TextUtils;
Selim Cinek60a54252016-02-26 17:03:25 -080056import android.text.style.AbsoluteSizeSpan;
Selim Cinek981962e2016-07-20 20:41:58 -070057import android.text.style.BackgroundColorSpan;
Selim Cinek89991a22016-03-07 19:51:54 -080058import android.text.style.CharacterStyle;
Selim Cinek981962e2016-07-20 20:41:58 -070059import android.text.style.ForegroundColorSpan;
Selim Cinek60a54252016-02-26 17:03:25 -080060import android.text.style.RelativeSizeSpan;
61import android.text.style.TextAppearanceSpan;
Svet Ganovddb94882016-06-23 19:55:24 -070062import android.util.ArraySet;
Daniel Sandlerdcbaf662013-04-26 16:23:09 -040063import android.util.Log;
Dan Sandler50128532015-12-08 15:42:41 -050064import android.util.SparseArray;
Griff Hazen61a9e862014-05-22 16:05:19 -070065import android.view.Gravity;
Selim Cinekea4bef72015-12-02 15:51:10 -080066import android.view.NotificationHeaderView;
Joe Onorato8595a3d2010-11-19 18:12:07 -080067import android.view.View;
Selim Cinek954cc232016-05-20 13:29:23 -070068import android.view.ViewGroup;
Jeff Sharkey1c400132011-08-05 14:50:13 -070069import android.widget.ProgressBar;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080070import android.widget.RemoteViews;
71
Griff Hazen959591e2014-05-15 22:26:18 -070072import com.android.internal.R;
Svet Ganovddb94882016-06-23 19:55:24 -070073import com.android.internal.util.ArrayUtils;
Griff Hazenc091ba82014-05-16 10:13:26 -070074import com.android.internal.util.NotificationColorUtil;
Adrian Roos0bc3f6a2017-03-06 11:54:05 -080075import com.android.internal.util.Preconditions;
Griff Hazen959591e2014-05-15 22:26:18 -070076
Tor Norbyed9273d62013-05-30 15:59:53 -070077import java.lang.annotation.Retention;
78import java.lang.annotation.RetentionPolicy;
Christoph Studer4600f9b2014-07-22 22:44:43 +020079import java.lang.reflect.Constructor;
Daniel Sandler2561b0b2012-02-13 21:04:12 -050080import java.util.ArrayList;
Griff Hazen61a9e862014-05-22 16:05:19 -070081import java.util.Arrays;
Griff Hazen5cadc3b2014-05-20 09:55:39 -070082import java.util.Collections;
Griff Hazen61a9e862014-05-22 16:05:19 -070083import java.util.List;
Dan Sandler50128532015-12-08 15:42:41 -050084import java.util.Set;
Joe Onorato561d3852010-11-20 18:09:34 -080085
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080086/**
87 * A class that represents how a persistent notification is to be presented to
88 * the user using the {@link android.app.NotificationManager}.
89 *
Joe Onoratocb109a02011-01-18 17:57:41 -080090 * <p>The {@link Notification.Builder Notification.Builder} has been added to make it
91 * easier to construct Notifications.</p>
92 *
Joe Fernandez558459f2011-10-13 16:47:36 -070093 * <div class="special reference">
94 * <h3>Developer Guides</h3>
95 * <p>For a guide to creating notifications, read the
96 * <a href="{@docRoot}guide/topics/ui/notifiers/notifications.html">Status Bar Notifications</a>
97 * developer guide.</p>
98 * </div>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080099 */
100public class Notification implements Parcelable
101{
Daniel Sandlerdcbaf662013-04-26 16:23:09 -0400102 private static final String TAG = "Notification";
103
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800104 /**
Daniel Sandler01df1c62014-06-09 10:54:01 -0400105 * An activity that provides a user interface for adjusting notification preferences for its
Julia Reynolds3aedded2017-03-31 14:42:09 -0400106 * containing application.
Daniel Sandler01df1c62014-06-09 10:54:01 -0400107 */
108 @SdkConstant(SdkConstantType.INTENT_CATEGORY)
109 public static final String INTENT_CATEGORY_NOTIFICATION_PREFERENCES
110 = "android.intent.category.NOTIFICATION_PREFERENCES";
111
112 /**
Julia Reynolds2619b5e2017-02-09 09:58:15 -0500113 * Optional extra for {@link #INTENT_CATEGORY_NOTIFICATION_PREFERENCES}. If provided, will
114 * contain a {@link NotificationChannel#getId() channel id} that can be used to narrow down
Julia Reynolds3aedded2017-03-31 14:42:09 -0400115 * what settings should be shown in the target app.
Julia Reynolds2619b5e2017-02-09 09:58:15 -0500116 */
117 public static final String EXTRA_CHANNEL_ID = "android.intent.extra.CHANNEL_ID";
118
119 /**
Julia Reynolds3aedded2017-03-31 14:42:09 -0400120 * Optional extra for {@link #INTENT_CATEGORY_NOTIFICATION_PREFERENCES}. If provided, will
121 * contain the tag provided to {@link NotificationManager#notify(String, int, Notification)}
122 * that can be used to narrow down what settings should be shown in the target app.
123 */
124 public static final String EXTRA_NOTIFICATION_TAG = "android.intent.extra.NOTIFICATION_TAG";
125
126 /**
127 * Optional extra for {@link #INTENT_CATEGORY_NOTIFICATION_PREFERENCES}. If provided, will
128 * contain the id provided to {@link NotificationManager#notify(String, int, Notification)}
129 * that can be used to narrow down what settings should be shown in the target app.
130 */
131 public static final String EXTRA_NOTIFICATION_ID = "android.intent.extra.NOTIFICATION_ID";
132
133 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800134 * Use all default values (where applicable).
135 */
136 public static final int DEFAULT_ALL = ~0;
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500137
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800138 /**
139 * Use the default notification sound. This will ignore any given
140 * {@link #sound}.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500141 *
Chris Wren47c20a12014-06-18 17:27:29 -0400142 * <p>
143 * A notification that is noisy is more likely to be presented as a heads-up notification.
144 * </p>
145 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800146 * @see #defaults
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500147 */
148
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800149 public static final int DEFAULT_SOUND = 1;
150
151 /**
152 * Use the default notification vibrate. This will ignore any given
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500153 * {@link #vibrate}. Using phone vibration requires the
Scott Mainb8b36452009-04-26 15:50:49 -0700154 * {@link android.Manifest.permission#VIBRATE VIBRATE} permission.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500155 *
Chris Wren47c20a12014-06-18 17:27:29 -0400156 * <p>
157 * A notification that vibrates is more likely to be presented as a heads-up notification.
158 * </p>
159 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800160 * @see #defaults
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500161 */
162
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800163 public static final int DEFAULT_VIBRATE = 2;
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500164
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800165 /**
166 * Use the default notification lights. This will ignore the
167 * {@link #FLAG_SHOW_LIGHTS} bit, and {@link #ledARGB}, {@link #ledOffMS}, or
168 * {@link #ledOnMS}.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500169 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800170 * @see #defaults
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500171 */
172
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800173 public static final int DEFAULT_LIGHTS = 4;
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500174
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800175 /**
Christoph Studer535ec612014-09-03 15:47:47 +0200176 * Maximum length of CharSequences accepted by Builder and friends.
177 *
178 * <p>
179 * Avoids spamming the system with overly large strings such as full e-mails.
180 */
181 private static final int MAX_CHARSEQUENCE_LENGTH = 5 * 1024;
182
183 /**
Adrian Roose458aa82015-12-08 16:17:19 -0800184 * Maximum entries of reply text that are accepted by Builder and friends.
185 */
186 private static final int MAX_REPLY_HISTORY = 5;
187
188 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500189 * A timestamp related to this notification, in milliseconds since the epoch.
Joe Malin8d40d042012-11-05 11:36:40 -0800190 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500191 * Default value: {@link System#currentTimeMillis() Now}.
192 *
193 * Choose a timestamp that will be most relevant to the user. For most finite events, this
194 * corresponds to the time the event happened (or will happen, in the case of events that have
195 * yet to occur but about which the user is being informed). Indefinite events should be
Joe Malin8d40d042012-11-05 11:36:40 -0800196 * timestamped according to when the activity began.
197 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500198 * Some examples:
Joe Malin8d40d042012-11-05 11:36:40 -0800199 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500200 * <ul>
201 * <li>Notification of a new chat message should be stamped when the message was received.</li>
202 * <li>Notification of an ongoing file download (with a progress bar, for example) should be stamped when the download started.</li>
203 * <li>Notification of a completed file download should be stamped when the download finished.</li>
204 * <li>Notification of an upcoming meeting should be stamped with the time the meeting will begin (that is, in the future).</li>
205 * <li>Notification of an ongoing stopwatch (increasing timer) should be stamped with the watch's start time.
206 * <li>Notification of an ongoing countdown timer should be stamped with the timer's end time.
Joe Malin8d40d042012-11-05 11:36:40 -0800207 * </ul>
208 *
Selim Cinek0ff1ce602016-04-05 18:27:16 -0700209 * For apps targeting {@link android.os.Build.VERSION_CODES#N} and above, this time is not shown
210 * anymore by default and must be opted into by using
211 * {@link android.app.Notification.Builder#setShowWhen(boolean)}
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800212 */
213 public long when;
214
215 /**
Selim Cinekb85f36fd2016-04-20 18:46:36 -0700216 * The creation time of the notification
217 */
218 private long creationTime;
219
220 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800221 * The resource id of a drawable to use as the icon in the status bar.
Dan Sandler86647982015-05-13 23:41:13 -0400222 *
223 * @deprecated Use {@link Builder#setSmallIcon(Icon)} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800224 */
Dan Sandler86647982015-05-13 23:41:13 -0400225 @Deprecated
Tor Norbye7b9c9122013-05-30 16:48:33 -0700226 @DrawableRes
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800227 public int icon;
228
229 /**
Joe Onorato46439ce2010-11-19 13:56:21 -0800230 * If the icon in the status bar is to have more than one level, you can set this. Otherwise,
231 * leave it at its default value of 0.
232 *
233 * @see android.widget.ImageView#setImageLevel
Griff Hazen959591e2014-05-15 22:26:18 -0700234 * @see android.graphics.drawable.Drawable#setLevel
Joe Onorato46439ce2010-11-19 13:56:21 -0800235 */
236 public int iconLevel;
237
238 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500239 * The number of events that this notification represents. For example, in a new mail
240 * notification, this could be the number of unread messages.
Joe Malin8d40d042012-11-05 11:36:40 -0800241 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500242 * The system may or may not use this field to modify the appearance of the notification. For
243 * example, before {@link android.os.Build.VERSION_CODES#HONEYCOMB}, this number was
244 * superimposed over the icon in the status bar. Starting with
245 * {@link android.os.Build.VERSION_CODES#HONEYCOMB}, the template used by
246 * {@link Notification.Builder} has displayed the number in the expanded notification view.
Julia Reynolds13d898c2017-02-02 12:22:05 -0500247 * Starting with {@link android.os.Build.VERSION_CODES#O}, the number may be displayed as a
248 * badge icon in Launchers that support badging.
Joe Malin8d40d042012-11-05 11:36:40 -0800249 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800250 */
Julia Reynolds13d898c2017-02-02 12:22:05 -0500251 public int number = 1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800252
253 /**
254 * The intent to execute when the expanded status entry is clicked. If
255 * this is an activity, it must include the
256 * {@link android.content.Intent#FLAG_ACTIVITY_NEW_TASK} flag, which requires
Scott Main7aee61f2011-02-08 11:25:01 -0800257 * that you take care of task management as described in the
258 * <a href="{@docRoot}guide/topics/fundamentals/tasks-and-back-stack.html">Tasks and Back
Dianne Hackborn6ceca582012-01-10 15:24:26 -0800259 * Stack</a> document. In particular, make sure to read the notification section
260 * <a href="{@docRoot}guide/topics/ui/notifiers/notifications.html#HandlingNotifications">Handling
261 * Notifications</a> for the correct ways to launch an application from a
262 * notification.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800263 */
264 public PendingIntent contentIntent;
265
266 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500267 * The intent to execute when the notification is explicitly dismissed by the user, either with
268 * the "Clear All" button or by swiping it away individually.
269 *
270 * This probably shouldn't be launching an activity since several of those will be sent
271 * at the same time.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800272 */
273 public PendingIntent deleteIntent;
274
275 /**
Dianne Hackborn170bae72010-09-03 15:14:28 -0700276 * An intent to launch instead of posting the notification to the status bar.
Joe Onoratocb109a02011-01-18 17:57:41 -0800277 *
Chris Wren47c20a12014-06-18 17:27:29 -0400278 * <p>
279 * The system UI may choose to display a heads-up notification, instead of
280 * launching this intent, while the user is using the device.
281 * </p>
282 *
Joe Onoratocb109a02011-01-18 17:57:41 -0800283 * @see Notification.Builder#setFullScreenIntent
Daniel Sandlere46cbd32010-06-17 10:35:26 -0400284 */
285 public PendingIntent fullScreenIntent;
286
287 /**
Dan Sandler5fcdf6e2014-07-18 11:31:15 -0400288 * Text that summarizes this notification for accessibility services.
289 *
290 * As of the L release, this text is no longer shown on screen, but it is still useful to
291 * accessibility services (where it serves as an audible announcement of the notification's
292 * appearance).
Joe Onoratoef1e7762010-09-17 18:38:38 -0400293 *
Joe Onorato46439ce2010-11-19 13:56:21 -0800294 * @see #tickerView
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800295 */
296 public CharSequence tickerText;
297
298 /**
Dan Sandler5fcdf6e2014-07-18 11:31:15 -0400299 * Formerly, a view showing the {@link #tickerText}.
300 *
301 * No longer displayed in the status bar as of API 21.
Joe Onoratoef1e7762010-09-17 18:38:38 -0400302 */
Dan Sandler5fcdf6e2014-07-18 11:31:15 -0400303 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -0800304 public RemoteViews tickerView;
Joe Onoratoef1e7762010-09-17 18:38:38 -0400305
306 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400307 * The view that will represent this notification in the notification list (which is pulled
308 * down from the status bar).
309 *
Julia Reynoldsd4ea7412016-02-17 14:00:56 -0500310 * As of N, this field may be null. The notification view is determined by the inputs
311 * to {@link Notification.Builder}; a custom RemoteViews can optionally be
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400312 * supplied with {@link Notification.Builder#setCustomContentView(RemoteViews)}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800313 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400314 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800315 public RemoteViews contentView;
316
Daniel Sandlera0a938c2012-03-15 08:42:37 -0400317 /**
Daniel Sandler4dfbe832012-04-11 14:51:46 -0400318 * A large-format version of {@link #contentView}, giving the Notification an
Daniel Sandlerf3b73432012-03-27 15:01:25 -0400319 * opportunity to show more detail. The system UI may choose to show this
320 * instead of the normal content view at its discretion.
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400321 *
Julia Reynoldsd4ea7412016-02-17 14:00:56 -0500322 * As of N, this field may be null. The expanded notification view is determined by the
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400323 * inputs to {@link Notification.Builder}; a custom RemoteViews can optionally be
324 * supplied with {@link Notification.Builder#setCustomBigContentView(RemoteViews)}.
Daniel Sandlerf3b73432012-03-27 15:01:25 -0400325 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400326 @Deprecated
Daniel Sandlerf3b73432012-03-27 15:01:25 -0400327 public RemoteViews bigContentView;
328
Chris Wren8fd39ec2014-02-27 17:43:26 -0500329
330 /**
Chris Wren47c20a12014-06-18 17:27:29 -0400331 * A medium-format version of {@link #contentView}, providing the Notification an
332 * opportunity to add action buttons to contentView. At its discretion, the system UI may
333 * choose to show this as a heads-up notification, which will pop up so the user can see
334 * it without leaving their current activity.
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400335 *
Julia Reynoldsd4ea7412016-02-17 14:00:56 -0500336 * As of N, this field may be null. The heads-up notification view is determined by the
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400337 * inputs to {@link Notification.Builder}; a custom RemoteViews can optionally be
338 * supplied with {@link Notification.Builder#setCustomHeadsUpContentView(RemoteViews)}.
Chris Wren8fd39ec2014-02-27 17:43:26 -0500339 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400340 @Deprecated
Chris Wren8fd39ec2014-02-27 17:43:26 -0500341 public RemoteViews headsUpContentView;
342
Daniel Sandlerf3b73432012-03-27 15:01:25 -0400343 /**
Dan Sandler86647982015-05-13 23:41:13 -0400344 * A large bitmap to be shown in the notification content area.
345 *
346 * @deprecated Use {@link Builder#setLargeIcon(Icon)} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800347 */
Dan Sandler86647982015-05-13 23:41:13 -0400348 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -0800349 public Bitmap largeIcon;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800350
351 /**
352 * The sound to play.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500353 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800354 * <p>
Chris Wren47c20a12014-06-18 17:27:29 -0400355 * A notification that is noisy is more likely to be presented as a heads-up notification.
356 * </p>
357 *
358 * <p>
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500359 * To play the default notification sound, see {@link #defaults}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800360 * </p>
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500361 * @deprecated use {@link NotificationChannel#getSound()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800362 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500363 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800364 public Uri sound;
365
366 /**
367 * Use this constant as the value for audioStreamType to request that
368 * the default stream type for notifications be used. Currently the
Jeff Sharkey098d5802012-04-26 17:30:34 -0700369 * default stream type is {@link AudioManager#STREAM_NOTIFICATION}.
John Spurlockc0650f022014-07-19 13:22:39 -0400370 *
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500371 * @deprecated Use {@link NotificationChannel#getAudioAttributes()} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800372 */
Jean-Michel Trivi81f871e2014-08-06 16:32:38 -0700373 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800374 public static final int STREAM_DEFAULT = -1;
375
376 /**
377 * The audio stream type to use when playing the sound.
378 * Should be one of the STREAM_ constants from
379 * {@link android.media.AudioManager}.
John Spurlockc0650f022014-07-19 13:22:39 -0400380 *
381 * @deprecated Use {@link #audioAttributes} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800382 */
Jean-Michel Trivi81f871e2014-08-06 16:32:38 -0700383 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800384 public int audioStreamType = STREAM_DEFAULT;
385
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800386 /**
John Spurlockc0650f022014-07-19 13:22:39 -0400387 * The default value of {@link #audioAttributes}.
388 */
389 public static final AudioAttributes AUDIO_ATTRIBUTES_DEFAULT = new AudioAttributes.Builder()
390 .setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
391 .setUsage(AudioAttributes.USAGE_NOTIFICATION)
392 .build();
393
394 /**
395 * The {@link AudioAttributes audio attributes} to use when playing the sound.
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500396 *
397 * @deprecated use {@link NotificationChannel#getAudioAttributes()} instead.
John Spurlockc0650f022014-07-19 13:22:39 -0400398 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500399 @Deprecated
John Spurlockc0650f022014-07-19 13:22:39 -0400400 public AudioAttributes audioAttributes = AUDIO_ATTRIBUTES_DEFAULT;
401
402 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500403 * The pattern with which to vibrate.
404 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800405 * <p>
406 * To vibrate the default pattern, see {@link #defaults}.
407 * </p>
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500408 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800409 * @see android.os.Vibrator#vibrate(long[],int)
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500410 * @deprecated use {@link NotificationChannel#getVibrationPattern()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800411 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500412 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800413 public long[] vibrate;
414
415 /**
416 * The color of the led. The hardware will do its best approximation.
417 *
418 * @see #FLAG_SHOW_LIGHTS
419 * @see #flags
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500420 * @deprecated use {@link NotificationChannel#shouldShowLights()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800421 */
Tor Norbye80756e32015-03-02 09:39:27 -0800422 @ColorInt
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500423 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800424 public int ledARGB;
425
426 /**
427 * The number of milliseconds for the LED to be on while it's flashing.
428 * The hardware will do its best approximation.
429 *
430 * @see #FLAG_SHOW_LIGHTS
431 * @see #flags
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500432 * @deprecated use {@link NotificationChannel#shouldShowLights()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800433 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500434 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800435 public int ledOnMS;
436
437 /**
438 * The number of milliseconds for the LED to be off while it's flashing.
439 * The hardware will do its best approximation.
440 *
441 * @see #FLAG_SHOW_LIGHTS
442 * @see #flags
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500443 *
444 * @deprecated use {@link NotificationChannel#shouldShowLights()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800445 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500446 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800447 public int ledOffMS;
448
449 /**
450 * Specifies which values should be taken from the defaults.
451 * <p>
452 * To set, OR the desired from {@link #DEFAULT_SOUND},
453 * {@link #DEFAULT_VIBRATE}, {@link #DEFAULT_LIGHTS}. For all default
454 * values, use {@link #DEFAULT_ALL}.
455 * </p>
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500456 *
457 * @deprecated use {@link NotificationChannel#getSound()} and
458 * {@link NotificationChannel#shouldShowLights()} and
459 * {@link NotificationChannel#shouldVibrate()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800460 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500461 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800462 public int defaults;
463
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800464 /**
465 * Bit to be bitwise-ored into the {@link #flags} field that should be
466 * set if you want the LED on for this notification.
467 * <ul>
468 * <li>To turn the LED off, pass 0 in the alpha channel for colorARGB
469 * or 0 for both ledOnMS and ledOffMS.</li>
470 * <li>To turn the LED on, pass 1 for ledOnMS and 0 for ledOffMS.</li>
471 * <li>To flash the LED, pass the number of milliseconds that it should
472 * be on and off to ledOnMS and ledOffMS.</li>
473 * </ul>
474 * <p>
475 * Since hardware varies, you are not guaranteed that any of the values
476 * you pass are honored exactly. Use the system defaults (TODO) if possible
477 * because they will be set to values that work on any given hardware.
478 * <p>
479 * The alpha channel must be set for forward compatibility.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500480 *
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500481 * @deprecated use {@link NotificationChannel#shouldShowLights()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800482 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500483 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800484 public static final int FLAG_SHOW_LIGHTS = 0x00000001;
485
486 /**
487 * Bit to be bitwise-ored into the {@link #flags} field that should be
488 * set if this notification is in reference to something that is ongoing,
489 * like a phone call. It should not be set if this notification is in
490 * reference to something that happened at a particular point in time,
491 * like a missed phone call.
492 */
493 public static final int FLAG_ONGOING_EVENT = 0x00000002;
494
495 /**
496 * Bit to be bitwise-ored into the {@link #flags} field that if set,
Scott Mainb8b36452009-04-26 15:50:49 -0700497 * the audio will be repeated until the notification is
498 * cancelled or the notification window is opened.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800499 */
500 public static final int FLAG_INSISTENT = 0x00000004;
501
502 /**
503 * Bit to be bitwise-ored into the {@link #flags} field that should be
Griff Hazen293977b2014-04-28 08:37:20 -0700504 * set if you would only like the sound, vibrate and ticker to be played
505 * if the notification was not already showing.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800506 */
507 public static final int FLAG_ONLY_ALERT_ONCE = 0x00000008;
508
509 /**
510 * Bit to be bitwise-ored into the {@link #flags} field that should be
511 * set if the notification should be canceled when it is clicked by the
Daniel Sandler8aa9ae62012-12-04 23:31:47 -0500512 * user.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800513 */
514 public static final int FLAG_AUTO_CANCEL = 0x00000010;
515
516 /**
517 * Bit to be bitwise-ored into the {@link #flags} field that should be
518 * set if the notification should not be canceled when the user clicks
519 * the Clear all button.
520 */
521 public static final int FLAG_NO_CLEAR = 0x00000020;
522
Dianne Hackbornd8a43f62009-08-17 23:33:56 -0700523 /**
524 * Bit to be bitwise-ored into the {@link #flags} field that should be
525 * set if this notification represents a currently running service. This
526 * will normally be set for you by {@link Service#startForeground}.
527 */
528 public static final int FLAG_FOREGROUND_SERVICE = 0x00000040;
529
Daniel Sandlere46cbd32010-06-17 10:35:26 -0400530 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500531 * Obsolete flag indicating high-priority notifications; use the priority field instead.
Joe Malin8d40d042012-11-05 11:36:40 -0800532 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500533 * @deprecated Use {@link #priority} with a positive value.
Daniel Sandlere46cbd32010-06-17 10:35:26 -0400534 */
Aurimas Liutikas514c5ef2016-05-24 15:22:55 -0700535 @Deprecated
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500536 public static final int FLAG_HIGH_PRIORITY = 0x00000080;
Daniel Sandlere46cbd32010-06-17 10:35:26 -0400537
Griff Hazendfcb0802014-02-11 12:00:00 -0800538 /**
539 * Bit to be bitswise-ored into the {@link #flags} field that should be
540 * set if this notification is relevant to the current device only
541 * and it is not recommended that it bridge to other devices.
542 */
543 public static final int FLAG_LOCAL_ONLY = 0x00000100;
544
Griff Hazen5cadc3b2014-05-20 09:55:39 -0700545 /**
546 * Bit to be bitswise-ored into the {@link #flags} field that should be
547 * set if this notification is the group summary for a group of notifications.
548 * Grouped notifications may display in a cluster or stack on devices which
549 * support such rendering. Requires a group key also be set using {@link Builder#setGroup}.
550 */
551 public static final int FLAG_GROUP_SUMMARY = 0x00000200;
552
Julia Reynoldse46bb372016-03-17 11:05:58 -0400553 /**
554 * Bit to be bitswise-ored into the {@link #flags} field that should be
555 * set if this notification is the group summary for an auto-group of notifications.
556 *
557 * @hide
558 */
559 @SystemApi
560 public static final int FLAG_AUTOGROUP_SUMMARY = 0x00000400;
561
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800562 public int flags;
563
Tor Norbyed9273d62013-05-30 15:59:53 -0700564 /** @hide */
565 @IntDef({PRIORITY_DEFAULT,PRIORITY_LOW,PRIORITY_MIN,PRIORITY_HIGH,PRIORITY_MAX})
566 @Retention(RetentionPolicy.SOURCE)
567 public @interface Priority {}
568
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800569 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500570 * Default notification {@link #priority}. If your application does not prioritize its own
571 * notifications, use this value for all notifications.
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500572 *
573 * @deprecated use {@link NotificationManager#IMPORTANCE_DEFAULT} instead.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500574 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500575 @Deprecated
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500576 public static final int PRIORITY_DEFAULT = 0;
577
578 /**
579 * Lower {@link #priority}, for items that are less important. The UI may choose to show these
580 * items smaller, or at a different position in the list, compared with your app's
581 * {@link #PRIORITY_DEFAULT} items.
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500582 *
583 * @deprecated use {@link NotificationManager#IMPORTANCE_LOW} instead.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500584 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500585 @Deprecated
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500586 public static final int PRIORITY_LOW = -1;
587
588 /**
589 * Lowest {@link #priority}; these items might not be shown to the user except under special
590 * circumstances, such as detailed notification logs.
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500591 *
592 * @deprecated use {@link NotificationManager#IMPORTANCE_MIN} instead.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500593 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500594 @Deprecated
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500595 public static final int PRIORITY_MIN = -2;
596
597 /**
598 * Higher {@link #priority}, for more important notifications or alerts. The UI may choose to
599 * show these items larger, or at a different position in notification lists, compared with
600 * your app's {@link #PRIORITY_DEFAULT} items.
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500601 *
602 * @deprecated use {@link NotificationManager#IMPORTANCE_HIGH} instead.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500603 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500604 @Deprecated
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500605 public static final int PRIORITY_HIGH = 1;
606
607 /**
608 * Highest {@link #priority}, for your application's most important items that require the
609 * user's prompt attention or input.
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500610 *
611 * @deprecated use {@link NotificationManager#IMPORTANCE_HIGH} instead.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500612 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500613 @Deprecated
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500614 public static final int PRIORITY_MAX = 2;
615
616 /**
617 * Relative priority for this notification.
Joe Malin8d40d042012-11-05 11:36:40 -0800618 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500619 * Priority is an indication of how much of the user's valuable attention should be consumed by
620 * this notification. Low-priority notifications may be hidden from the user in certain
621 * situations, while the user might be interrupted for a higher-priority notification. The
Daniel Sandler6738eee2012-11-16 12:03:32 -0500622 * system will make a determination about how to interpret this priority when presenting
623 * the notification.
Chris Wren47c20a12014-06-18 17:27:29 -0400624 *
625 * <p>
626 * A notification that is at least {@link #PRIORITY_HIGH} is more likely to be presented
627 * as a heads-up notification.
628 * </p>
629 *
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500630 * @deprecated use {@link NotificationChannel#getImportance()} instead.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500631 */
Tor Norbyed9273d62013-05-30 15:59:53 -0700632 @Priority
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500633 @Deprecated
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500634 public int priority;
Joe Malin8d40d042012-11-05 11:36:40 -0800635
Dan Sandler26e81cf2014-05-06 10:01:27 -0400636 /**
637 * Accent color (an ARGB integer like the constants in {@link android.graphics.Color})
638 * to be applied by the standard Style templates when presenting this notification.
639 *
640 * The current template design constructs a colorful header image by overlaying the
641 * {@link #icon} image (stenciled in white) atop a field of this color. Alpha components are
642 * ignored.
643 */
Tor Norbye80756e32015-03-02 09:39:27 -0800644 @ColorInt
Dan Sandler26e81cf2014-05-06 10:01:27 -0400645 public int color = COLOR_DEFAULT;
646
647 /**
648 * Special value of {@link #color} telling the system not to decorate this notification with
649 * any special color but instead use default colors when presenting this notification.
650 */
Tor Norbye80756e32015-03-02 09:39:27 -0800651 @ColorInt
Dan Sandler26e81cf2014-05-06 10:01:27 -0400652 public static final int COLOR_DEFAULT = 0; // AKA Color.TRANSPARENT
Dan Sandler0bf2ed82013-12-21 23:33:41 -0600653
654 /**
Adrian Roos4ff3b122016-02-01 12:26:13 -0800655 * Special value of {@link #color} used as a place holder for an invalid color.
Selim Cinek99104832017-01-25 14:47:33 -0800656 * @hide
Adrian Roos4ff3b122016-02-01 12:26:13 -0800657 */
658 @ColorInt
Selim Cinek99104832017-01-25 14:47:33 -0800659 public static final int COLOR_INVALID = 1;
Adrian Roos4ff3b122016-02-01 12:26:13 -0800660
661 /**
Adrian Roosc1a80b02016-04-05 14:54:55 -0700662 * Sphere of visibility of this notification, which affects how and when the SystemUI reveals
663 * the notification's presence and contents in untrusted situations (namely, on the secure
Dan Sandler0bf2ed82013-12-21 23:33:41 -0600664 * lockscreen).
665 *
666 * The default level, {@link #VISIBILITY_PRIVATE}, behaves exactly as notifications have always
667 * done on Android: The notification's {@link #icon} and {@link #tickerText} (if available) are
668 * shown in all situations, but the contents are only available if the device is unlocked for
669 * the appropriate user.
670 *
671 * A more permissive policy can be expressed by {@link #VISIBILITY_PUBLIC}; such a notification
672 * can be read even in an "insecure" context (that is, above a secure lockscreen).
673 * To modify the public version of this notification—for example, to redact some portions—see
674 * {@link Builder#setPublicVersion(Notification)}.
675 *
676 * Finally, a notification can be made {@link #VISIBILITY_SECRET}, which will suppress its icon
677 * and ticker until the user has bypassed the lockscreen.
678 */
Jeff Sharkey30e06bb2017-04-24 11:18:03 -0600679 public @Visibility int visibility;
680
681 /** @hide */
682 @IntDef(prefix = { "VISIBILITY_" }, value = {
683 VISIBILITY_PUBLIC,
684 VISIBILITY_PRIVATE,
685 VISIBILITY_SECRET,
686 })
687 @Retention(RetentionPolicy.SOURCE)
688 public @interface Visibility {}
Dan Sandler0bf2ed82013-12-21 23:33:41 -0600689
Griff Hazenfc3922d2014-08-20 11:56:44 -0700690 /**
691 * Notification visibility: Show this notification in its entirety on all lockscreens.
692 *
693 * {@see #visibility}
694 */
Dan Sandler0bf2ed82013-12-21 23:33:41 -0600695 public static final int VISIBILITY_PUBLIC = 1;
Griff Hazenfc3922d2014-08-20 11:56:44 -0700696
697 /**
698 * Notification visibility: Show this notification on all lockscreens, but conceal sensitive or
699 * private information on secure lockscreens.
700 *
701 * {@see #visibility}
702 */
Dan Sandler0bf2ed82013-12-21 23:33:41 -0600703 public static final int VISIBILITY_PRIVATE = 0;
Griff Hazenfc3922d2014-08-20 11:56:44 -0700704
705 /**
706 * Notification visibility: Do not reveal any part of this notification on a secure lockscreen.
707 *
708 * {@see #visibility}
709 */
Dan Sandler0bf2ed82013-12-21 23:33:41 -0600710 public static final int VISIBILITY_SECRET = -1;
711
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500712 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400713 * Notification category: incoming call (voice or video) or similar synchronous communication request.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500714 */
John Spurlockfd7f1e02014-03-18 16:41:57 -0400715 public static final String CATEGORY_CALL = "call";
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500716
717 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400718 * Notification category: incoming direct message (SMS, instant message, etc.).
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500719 */
John Spurlockfd7f1e02014-03-18 16:41:57 -0400720 public static final String CATEGORY_MESSAGE = "msg";
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500721
722 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400723 * Notification category: asynchronous bulk message (email).
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500724 */
John Spurlockfd7f1e02014-03-18 16:41:57 -0400725 public static final String CATEGORY_EMAIL = "email";
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500726
727 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400728 * Notification category: calendar event.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500729 */
John Spurlockfd7f1e02014-03-18 16:41:57 -0400730 public static final String CATEGORY_EVENT = "event";
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500731
732 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400733 * Notification category: promotion or advertisement.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500734 */
John Spurlockfd7f1e02014-03-18 16:41:57 -0400735 public static final String CATEGORY_PROMO = "promo";
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500736
737 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400738 * Notification category: alarm or timer.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500739 */
John Spurlockfd7f1e02014-03-18 16:41:57 -0400740 public static final String CATEGORY_ALARM = "alarm";
741
742 /**
743 * Notification category: progress of a long-running background operation.
744 */
745 public static final String CATEGORY_PROGRESS = "progress";
746
747 /**
748 * Notification category: social network or sharing update.
749 */
750 public static final String CATEGORY_SOCIAL = "social";
751
752 /**
753 * Notification category: error in background operation or authentication status.
754 */
755 public static final String CATEGORY_ERROR = "err";
756
757 /**
758 * Notification category: media transport control for playback.
759 */
760 public static final String CATEGORY_TRANSPORT = "transport";
761
762 /**
763 * Notification category: system or device status update. Reserved for system use.
764 */
765 public static final String CATEGORY_SYSTEM = "sys";
766
767 /**
768 * Notification category: indication of running background service.
769 */
770 public static final String CATEGORY_SERVICE = "service";
771
772 /**
John Spurlock0a69c8c2014-03-21 13:30:57 -0400773 * Notification category: a specific, timely recommendation for a single thing.
774 * For example, a news app might want to recommend a news story it believes the user will
775 * want to read next.
776 */
777 public static final String CATEGORY_RECOMMENDATION = "recommendation";
778
779 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400780 * Notification category: ongoing information about device or contextual status.
781 */
782 public static final String CATEGORY_STATUS = "status";
783
784 /**
John Spurlock24d3dad2015-04-02 12:24:02 -0400785 * Notification category: user-scheduled reminder.
786 */
787 public static final String CATEGORY_REMINDER = "reminder";
788
789 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400790 * One of the predefined notification categories (see the <code>CATEGORY_*</code> constants)
791 * that best describes this Notification. May be used by the system for ranking and filtering.
792 */
793 public String category;
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500794
Griff Hazen5cadc3b2014-05-20 09:55:39 -0700795 private String mGroupKey;
796
797 /**
798 * Get the key used to group this notification into a cluster or stack
799 * with other notifications on devices which support such rendering.
800 */
801 public String getGroup() {
802 return mGroupKey;
803 }
804
805 private String mSortKey;
806
807 /**
808 * Get a sort key that orders this notification among other notifications from the
809 * same package. This can be useful if an external sort was already applied and an app
810 * would like to preserve this. Notifications will be sorted lexicographically using this
811 * value, although providing different priorities in addition to providing sort key may
812 * cause this value to be ignored.
813 *
814 * <p>This sort key can also be used to order members of a notification group. See
815 * {@link Builder#setGroup}.
816 *
817 * @see String#compareTo(String)
818 */
819 public String getSortKey() {
820 return mSortKey;
821 }
822
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500823 /**
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400824 * Additional semantic data to be carried around with this Notification.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400825 * <p>
826 * The extras keys defined here are intended to capture the original inputs to {@link Builder}
827 * APIs, and are intended to be used by
828 * {@link android.service.notification.NotificationListenerService} implementations to extract
829 * detailed information from notification objects.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500830 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400831 public Bundle extras = new Bundle();
832
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400833 /**
Felipe Lemedd85da62016-06-28 11:29:54 -0700834 * All pending intents in the notification as the system needs to be able to access them but
835 * touching the extras bundle in the system process is not safe because the bundle may contain
Svet Ganovddb94882016-06-23 19:55:24 -0700836 * custom parcelable objects.
837 *
838 * @hide
839 */
Felipe Lemedd85da62016-06-28 11:29:54 -0700840 public ArraySet<PendingIntent> allPendingIntents;
Svet Ganovddb94882016-06-23 19:55:24 -0700841
842 /**
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400843 * {@link #extras} key: this is the title of the notification,
844 * as supplied to {@link Builder#setContentTitle(CharSequence)}.
845 */
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -0500846 public static final String EXTRA_TITLE = "android.title";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400847
848 /**
849 * {@link #extras} key: this is the title of the notification when shown in expanded form,
850 * e.g. as supplied to {@link BigTextStyle#setBigContentTitle(CharSequence)}.
851 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400852 public static final String EXTRA_TITLE_BIG = EXTRA_TITLE + ".big";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400853
854 /**
855 * {@link #extras} key: this is the main text payload, as supplied to
856 * {@link Builder#setContentText(CharSequence)}.
857 */
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -0500858 public static final String EXTRA_TEXT = "android.text";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400859
860 /**
861 * {@link #extras} key: this is a third line of text, as supplied to
862 * {@link Builder#setSubText(CharSequence)}.
863 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400864 public static final String EXTRA_SUB_TEXT = "android.subText";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400865
866 /**
Adrian Roose458aa82015-12-08 16:17:19 -0800867 * {@link #extras} key: this is the remote input history, as supplied to
868 * {@link Builder#setRemoteInputHistory(CharSequence[])}.
Adrian Roos005e7742016-04-13 15:02:20 -0700869 *
870 * Apps can fill this through {@link Builder#setRemoteInputHistory(CharSequence[])}
871 * with the most recent inputs that have been sent through a {@link RemoteInput} of this
872 * Notification and are expected to clear it once the it is no longer relevant (e.g. for chat
873 * notifications once the other party has responded).
874 *
875 * The extra with this key is of type CharSequence[] and contains the most recent entry at
876 * the 0 index, the second most recent at the 1 index, etc.
877 *
878 * @see Builder#setRemoteInputHistory(CharSequence[])
Adrian Roose458aa82015-12-08 16:17:19 -0800879 */
880 public static final String EXTRA_REMOTE_INPUT_HISTORY = "android.remoteInputHistory";
881
882 /**
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400883 * {@link #extras} key: this is a small piece of additional text as supplied to
884 * {@link Builder#setContentInfo(CharSequence)}.
885 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400886 public static final String EXTRA_INFO_TEXT = "android.infoText";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400887
888 /**
889 * {@link #extras} key: this is a line of summary information intended to be shown
890 * alongside expanded notifications, as supplied to (e.g.)
891 * {@link BigTextStyle#setSummaryText(CharSequence)}.
892 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400893 public static final String EXTRA_SUMMARY_TEXT = "android.summaryText";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400894
895 /**
Christoph Studer4600f9b2014-07-22 22:44:43 +0200896 * {@link #extras} key: this is the longer text shown in the big form of a
897 * {@link BigTextStyle} notification, as supplied to
898 * {@link BigTextStyle#bigText(CharSequence)}.
899 */
900 public static final String EXTRA_BIG_TEXT = "android.bigText";
901
902 /**
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400903 * {@link #extras} key: this is the resource ID of the notification's main small icon, as
904 * supplied to {@link Builder#setSmallIcon(int)}.
Julia Reynoldse071abd2017-03-22 10:52:11 -0400905 *
906 * @deprecated Use {@link #getSmallIcon()}, which supports a wider variety of icon sources.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400907 */
Julia Reynoldse071abd2017-03-22 10:52:11 -0400908 @Deprecated
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -0500909 public static final String EXTRA_SMALL_ICON = "android.icon";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400910
911 /**
912 * {@link #extras} key: this is a bitmap to be used instead of the small icon when showing the
913 * notification payload, as
914 * supplied to {@link Builder#setLargeIcon(android.graphics.Bitmap)}.
Julia Reynoldse071abd2017-03-22 10:52:11 -0400915 *
916 * @deprecated Use {@link #getLargeIcon()}, which supports a wider variety of icon sources.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400917 */
Julia Reynoldse071abd2017-03-22 10:52:11 -0400918 @Deprecated
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400919 public static final String EXTRA_LARGE_ICON = "android.largeIcon";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400920
921 /**
922 * {@link #extras} key: this is a bitmap to be used instead of the one from
923 * {@link Builder#setLargeIcon(android.graphics.Bitmap)} when the notification is
924 * shown in its expanded form, as supplied to
925 * {@link BigPictureStyle#bigLargeIcon(android.graphics.Bitmap)}.
926 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400927 public static final String EXTRA_LARGE_ICON_BIG = EXTRA_LARGE_ICON + ".big";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400928
929 /**
930 * {@link #extras} key: this is the progress value supplied to
931 * {@link Builder#setProgress(int, int, boolean)}.
932 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400933 public static final String EXTRA_PROGRESS = "android.progress";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400934
935 /**
936 * {@link #extras} key: this is the maximum value supplied to
937 * {@link Builder#setProgress(int, int, boolean)}.
938 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400939 public static final String EXTRA_PROGRESS_MAX = "android.progressMax";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400940
941 /**
942 * {@link #extras} key: whether the progress bar is indeterminate, supplied to
943 * {@link Builder#setProgress(int, int, boolean)}.
944 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400945 public static final String EXTRA_PROGRESS_INDETERMINATE = "android.progressIndeterminate";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400946
947 /**
948 * {@link #extras} key: whether {@link #when} should be shown as a count-up timer (specifically
949 * a {@link android.widget.Chronometer}) instead of a timestamp, as supplied to
950 * {@link Builder#setUsesChronometer(boolean)}.
951 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400952 public static final String EXTRA_SHOW_CHRONOMETER = "android.showChronometer";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400953
954 /**
Selim Cinek81c23aa2016-02-25 16:23:13 -0800955 * {@link #extras} key: whether the chronometer set on the notification should count down
956 * instead of counting up. Is only relevant if key {@link #EXTRA_SHOW_CHRONOMETER} is present.
Adrian Roos96b7e202016-05-17 13:50:38 -0700957 * This extra is a boolean. The default is false.
Selim Cinek81c23aa2016-02-25 16:23:13 -0800958 */
Adrian Roos96b7e202016-05-17 13:50:38 -0700959 public static final String EXTRA_CHRONOMETER_COUNT_DOWN = "android.chronometerCountDown";
Selim Cinek81c23aa2016-02-25 16:23:13 -0800960
961 /**
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400962 * {@link #extras} key: whether {@link #when} should be shown,
963 * as supplied to {@link Builder#setShowWhen(boolean)}.
964 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400965 public static final String EXTRA_SHOW_WHEN = "android.showWhen";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400966
967 /**
968 * {@link #extras} key: this is a bitmap to be shown in {@link BigPictureStyle} expanded
969 * notifications, supplied to {@link BigPictureStyle#bigPicture(android.graphics.Bitmap)}.
970 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400971 public static final String EXTRA_PICTURE = "android.picture";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400972
973 /**
974 * {@link #extras} key: An array of CharSequences to show in {@link InboxStyle} expanded
975 * notifications, each of which was supplied to {@link InboxStyle#addLine(CharSequence)}.
976 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400977 public static final String EXTRA_TEXT_LINES = "android.textLines";
Dan Sandler842dd772014-05-15 09:36:47 -0400978
979 /**
980 * {@link #extras} key: A string representing the name of the specific
981 * {@link android.app.Notification.Style} used to create this notification.
982 */
Chris Wren91ad5632013-06-05 15:05:57 -0400983 public static final String EXTRA_TEMPLATE = "android.template";
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400984
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400985 /**
Chris Wrene6c48932014-09-29 17:19:27 -0400986 * {@link #extras} key: A String array containing the people that this notification relates to,
987 * each of which was supplied to {@link Builder#addPerson(String)}.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400988 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400989 public static final String EXTRA_PEOPLE = "android.people";
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500990
991 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400992 * Allow certain system-generated notifications to appear before the device is provisioned.
993 * Only available to notifications coming from the android package.
994 * @hide
995 */
Maurice Lam96c10032017-03-29 15:42:38 -0700996 @SystemApi
John Spurlockfd7f1e02014-03-18 16:41:57 -0400997 public static final String EXTRA_ALLOW_DURING_SETUP = "android.allowDuringSetup";
998
999 /**
Jose Limae9e3b3b2014-05-18 23:44:50 -07001000 * {@link #extras} key: A
1001 * {@link android.content.ContentUris content URI} pointing to an image that can be displayed
1002 * in the background when the notification is selected. The URI must point to an image stream
1003 * suitable for passing into
1004 * {@link android.graphics.BitmapFactory#decodeStream(java.io.InputStream)
1005 * BitmapFactory.decodeStream}; all other content types will be ignored. The content provider
1006 * URI used for this purpose must require no permissions to read the image data.
1007 */
1008 public static final String EXTRA_BACKGROUND_IMAGE_URI = "android.backgroundImageUri";
1009
1010 /**
Dan Sandler842dd772014-05-15 09:36:47 -04001011 * {@link #extras} key: A
Jeff Browndba34ba2014-06-24 20:46:03 -07001012 * {@link android.media.session.MediaSession.Token} associated with a
Dan Sandler842dd772014-05-15 09:36:47 -04001013 * {@link android.app.Notification.MediaStyle} notification.
1014 */
1015 public static final String EXTRA_MEDIA_SESSION = "android.mediaSession";
1016
1017 /**
Bryan Mawhinneye191f902014-07-22 12:50:09 +01001018 * {@link #extras} key: the indices of actions to be shown in the compact view,
1019 * as supplied to (e.g.) {@link MediaStyle#setShowActionsInCompactView(int...)}.
1020 */
1021 public static final String EXTRA_COMPACT_ACTIONS = "android.compactActions";
1022
Christoph Studer943aa672014-08-03 20:31:16 +02001023 /**
Alex Hillsfc737de2016-03-23 17:33:02 -04001024 * {@link #extras} key: the username to be displayed for all messages sent by the user including
1025 * direct replies
Adrian Roos96b7e202016-05-17 13:50:38 -07001026 * {@link android.app.Notification.MessagingStyle} notification. This extra is a
1027 * {@link CharSequence}
Alex Hillsfc737de2016-03-23 17:33:02 -04001028 */
1029 public static final String EXTRA_SELF_DISPLAY_NAME = "android.selfDisplayName";
1030
1031 /**
Adrian Roos96b7e202016-05-17 13:50:38 -07001032 * {@link #extras} key: a {@link CharSequence} to be displayed as the title to a conversation
Alex Hillsd9b04d92016-04-11 16:38:16 -04001033 * represented by a {@link android.app.Notification.MessagingStyle}
Alex Hillsfc737de2016-03-23 17:33:02 -04001034 */
Alex Hillsd9b04d92016-04-11 16:38:16 -04001035 public static final String EXTRA_CONVERSATION_TITLE = "android.conversationTitle";
Alex Hillsfc737de2016-03-23 17:33:02 -04001036
1037 /**
1038 * {@link #extras} key: an array of {@link android.app.Notification.MessagingStyle.Message}
1039 * bundles provided by a
Adrian Roos96b7e202016-05-17 13:50:38 -07001040 * {@link android.app.Notification.MessagingStyle} notification. This extra is a parcelable
1041 * array of bundles.
Alex Hillsfc737de2016-03-23 17:33:02 -04001042 */
1043 public static final String EXTRA_MESSAGES = "android.messages";
1044
1045 /**
Adrian Roos437cd562017-01-18 15:47:03 -08001046 * {@link #extras} key: an array of
1047 * {@link android.app.Notification.MessagingStyle#addHistoricMessage historic}
1048 * {@link android.app.Notification.MessagingStyle.Message} bundles provided by a
1049 * {@link android.app.Notification.MessagingStyle} notification. This extra is a parcelable
1050 * array of bundles.
1051 */
1052 public static final String EXTRA_HISTORIC_MESSAGES = "android.messages.historic";
1053
1054 /**
Selim Cinek7b9605b2017-01-19 17:36:00 -08001055 * {@link #extras} key: whether the notification should be colorized as
1056 * supplied to {@link Builder#setColorized(boolean)}}.
1057 */
1058 public static final String EXTRA_COLORIZED = "android.colorized";
1059
1060 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -04001061 * @hide
1062 */
1063 public static final String EXTRA_BUILDER_APPLICATION_INFO = "android.appInfo";
1064
Selim Cinek247fa012016-02-18 09:50:48 -08001065 /**
1066 * @hide
1067 */
1068 public static final String EXTRA_CONTAINS_CUSTOM_VIEW = "android.contains.customView";
1069
Shane Brennan472a3b32016-12-12 15:28:10 -08001070 /**
1071 * {@link #extras} key: the audio contents of this notification.
1072 *
1073 * This is for use when rendering the notification on an audio-focused interface;
1074 * the audio contents are a complete sound sample that contains the contents/body of the
1075 * notification. This may be used in substitute of a Text-to-Speech reading of the
1076 * notification. For example if the notification represents a voice message this should point
1077 * to the audio of that message.
1078 *
1079 * The data stored under this key should be a String representation of a Uri that contains the
1080 * audio contents in one of the following formats: WAV, PCM 16-bit, AMR-WB.
1081 *
1082 * This extra is unnecessary if you are using {@code MessagingStyle} since each {@code Message}
1083 * has a field for holding data URI. That field can be used for audio.
1084 * See {@code Message#setData}.
1085 *
1086 * Example usage:
1087 * <pre>
1088 * {@code
1089 * Notification.Builder myBuilder = (build your Notification as normal);
1090 * myBuilder.getExtras().putString(EXTRA_AUDIO_CONTENTS_URI, myAudioUri.toString());
1091 * }
1092 * </pre>
1093 */
1094 public static final String EXTRA_AUDIO_CONTENTS_URI = "android.audioContents";
1095
Dan Sandler80eaa592016-04-14 23:34:54 -04001096 /** @hide */
1097 @SystemApi
Dan Sandler732bd6c2016-04-12 14:20:32 -04001098 public static final String EXTRA_SUBSTITUTE_APP_NAME = "android.substName";
1099
Dan Sandlerd63f9322015-05-06 15:18:49 -04001100 private Icon mSmallIcon;
1101 private Icon mLargeIcon;
1102
Julia Reynoldsb5e44b72016-08-16 15:00:25 -04001103 private String mChannelId;
Julia Reynolds2a128742016-11-28 14:29:25 -05001104 private long mTimeout;
Julia Reynoldsb5e44b72016-08-16 15:00:25 -04001105
Julia Reynolds13d898c2017-02-02 12:22:05 -05001106 private String mShortcutId;
Julia Reynolds3aedded2017-03-31 14:42:09 -04001107 private CharSequence mSettingsText;
Julia Reynolds13d898c2017-02-02 12:22:05 -05001108
Julia Reynoldsa79c3712017-04-21 10:29:57 -04001109 /** @hide */
1110 @IntDef(prefix = { "GROUP_ALERT_" }, value = {
1111 GROUP_ALERT_ALL, GROUP_ALERT_CHILDREN, GROUP_ALERT_SUMMARY
1112 })
1113 @Retention(RetentionPolicy.SOURCE)
1114 public @interface GroupAlertBehavior {}
1115
1116 /**
1117 * Constant for {@link Builder#setGroupAlertBehavior(int)}, meaning that all notifications in a
1118 * group with sound or vibration ought to make sound or vibrate (respectively), so this
1119 * notification will not be muted when it is in a group.
1120 */
1121 public static final int GROUP_ALERT_ALL = 0;
1122
1123 /**
1124 * Constant for {@link Builder#setGroupAlertBehavior(int)}, meaning that all children
1125 * notification in a group should be silenced (no sound or vibration) even if they are posted
1126 * to a {@link NotificationChannel} that has sound and/or vibration. Use this constant to
1127 * mute this notification if this notification is a group child.
1128 *
1129 * <p> For example, you might want to use this constant if you post a number of children
1130 * notifications at once (say, after a periodic sync), and only need to notify the user
1131 * audibly once.
1132 */
1133 public static final int GROUP_ALERT_SUMMARY = 1;
1134
1135 /**
1136 * Constant for {@link Builder#setGroupAlertBehavior(int)}, meaning that the summary
1137 * notification in a group should be silenced (no sound or vibration) even if they are
1138 * posted to a {@link NotificationChannel} that has sound and/or vibration. Use this constant
1139 * to mute this notification if this notification is a group summary.
1140 *
1141 * <p>For example, you might want to use this constant if only the children notifications
1142 * in your group have content and the summary is only used to visually group notifications.
1143 */
1144 public static final int GROUP_ALERT_CHILDREN = 2;
1145
1146 private int mGroupAlertBehavior = GROUP_ALERT_ALL;
1147
Julia Reynolds13d898c2017-02-02 12:22:05 -05001148 /**
1149 * If this notification is being shown as a badge, always show as a number.
1150 */
1151 public static final int BADGE_ICON_NONE = 0;
1152
1153 /**
1154 * If this notification is being shown as a badge, use the {@link #getSmallIcon()} to
1155 * represent this notification.
1156 */
1157 public static final int BADGE_ICON_SMALL = 1;
1158
1159 /**
1160 * If this notification is being shown as a badge, use the {@link #getLargeIcon()} to
1161 * represent this notification.
1162 */
1163 public static final int BADGE_ICON_LARGE = 2;
Julia Reynolds7d5b4da2017-03-20 10:18:49 -04001164 private int mBadgeIcon = BADGE_ICON_NONE;
Julia Reynolds13d898c2017-02-02 12:22:05 -05001165
Chris Wren51c75102013-07-16 20:49:17 -04001166 /**
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001167 * Structure to encapsulate a named action that can be shown as part of this notification.
1168 * It must include an icon, a label, and a {@link PendingIntent} to be fired when the action is
1169 * selected by the user.
1170 * <p>
Griff Hazen959591e2014-05-15 22:26:18 -07001171 * Apps should use {@link Notification.Builder#addAction(int, CharSequence, PendingIntent)}
1172 * or {@link Notification.Builder#addAction(Notification.Action)}
1173 * to attach actions.
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001174 */
Daniel Sandlerea2a3172013-02-20 22:24:20 -05001175 public static class Action implements Parcelable {
Shane Brennan472a3b32016-12-12 15:28:10 -08001176 /**
1177 * {@link #extras} key: Keys to a {@link Parcelable} {@link ArrayList} of
1178 * {@link RemoteInput}s.
1179 *
1180 * This is intended for {@link RemoteInput}s that only accept data, meaning
1181 * {@link RemoteInput#getAllowFreeFormInput} is false, {@link RemoteInput#getChoices}
1182 * is null or empty, and {@link RemoteInput#getAllowedDataTypes is non-null and not
1183 * empty. These {@link RemoteInput}s will be ignored by devices that do not
1184 * support non-text-based {@link RemoteInput}s. See {@link Builder#build}.
1185 *
1186 * You can test if a RemoteInput matches these constraints using
1187 * {@link RemoteInput#isDataOnly}.
1188 */
1189 private static final String EXTRA_DATA_ONLY_INPUTS = "android.extra.DATA_ONLY_INPUTS";
1190
Griff Hazen959591e2014-05-15 22:26:18 -07001191 private final Bundle mExtras;
Dan Sandler86647982015-05-13 23:41:13 -04001192 private Icon mIcon;
Griff Hazen61a9e862014-05-22 16:05:19 -07001193 private final RemoteInput[] mRemoteInputs;
Alex Hills1f27f882017-01-12 11:24:46 -05001194 private boolean mAllowGeneratedReplies = true;
Griff Hazen959591e2014-05-15 22:26:18 -07001195
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001196 /**
1197 * Small icon representing the action.
Dan Sandler86647982015-05-13 23:41:13 -04001198 *
1199 * @deprecated Use {@link Action#getIcon()} instead.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001200 */
Dan Sandler86647982015-05-13 23:41:13 -04001201 @Deprecated
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001202 public int icon;
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001203
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001204 /**
1205 * Title of the action.
1206 */
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001207 public CharSequence title;
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001208
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001209 /**
1210 * Intent to send when the user invokes this action. May be null, in which case the action
1211 * may be rendered in a disabled presentation by the system UI.
1212 */
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001213 public PendingIntent actionIntent;
Griff Hazen959591e2014-05-15 22:26:18 -07001214
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001215 private Action(Parcel in) {
Dan Sandler86647982015-05-13 23:41:13 -04001216 if (in.readInt() != 0) {
1217 mIcon = Icon.CREATOR.createFromParcel(in);
Dan Sandler68079d52015-07-22 10:45:30 -04001218 if (mIcon.getType() == Icon.TYPE_RESOURCE) {
1219 icon = mIcon.getResId();
1220 }
Dan Sandler86647982015-05-13 23:41:13 -04001221 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001222 title = TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(in);
1223 if (in.readInt() == 1) {
1224 actionIntent = PendingIntent.CREATOR.createFromParcel(in);
1225 }
Jeff Sharkeya04c7a72016-03-18 12:20:36 -06001226 mExtras = Bundle.setDefusable(in.readBundle(), true);
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001227 mRemoteInputs = in.createTypedArray(RemoteInput.CREATOR);
Alex Hills42b0c4d2016-04-26 13:35:36 -04001228 mAllowGeneratedReplies = in.readInt() == 1;
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001229 }
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001230
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001231 /**
Dan Sandler86647982015-05-13 23:41:13 -04001232 * @deprecated Use {@link android.app.Notification.Action.Builder}.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001233 */
Dan Sandler86647982015-05-13 23:41:13 -04001234 @Deprecated
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001235 public Action(int icon, CharSequence title, PendingIntent intent) {
Alex Hills1f27f882017-01-12 11:24:46 -05001236 this(Icon.createWithResource("", icon), title, intent, new Bundle(), null, true);
Griff Hazen959591e2014-05-15 22:26:18 -07001237 }
1238
Adrian Roos7af53622016-10-12 13:44:05 -07001239 /** Keep in sync with {@link Notification.Action.Builder#Builder(Action)}! */
Dan Sandler86647982015-05-13 23:41:13 -04001240 private Action(Icon icon, CharSequence title, PendingIntent intent, Bundle extras,
Alex Hills42b0c4d2016-04-26 13:35:36 -04001241 RemoteInput[] remoteInputs, boolean allowGeneratedReplies) {
Dan Sandler86647982015-05-13 23:41:13 -04001242 this.mIcon = icon;
Gus Prevasf5bff46f2015-08-24 10:34:28 -04001243 if (icon != null && icon.getType() == Icon.TYPE_RESOURCE) {
1244 this.icon = icon.getResId();
1245 }
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001246 this.title = title;
1247 this.actionIntent = intent;
Griff Hazen959591e2014-05-15 22:26:18 -07001248 this.mExtras = extras != null ? extras : new Bundle();
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001249 this.mRemoteInputs = remoteInputs;
Alex Hills42b0c4d2016-04-26 13:35:36 -04001250 this.mAllowGeneratedReplies = allowGeneratedReplies;
Griff Hazen959591e2014-05-15 22:26:18 -07001251 }
1252
1253 /**
Dan Sandler86647982015-05-13 23:41:13 -04001254 * Return an icon representing the action.
1255 */
1256 public Icon getIcon() {
1257 if (mIcon == null && icon != 0) {
1258 // you snuck an icon in here without using the builder; let's try to keep it
1259 mIcon = Icon.createWithResource("", icon);
1260 }
1261 return mIcon;
1262 }
1263
1264 /**
Griff Hazen959591e2014-05-15 22:26:18 -07001265 * Get additional metadata carried around with this Action.
1266 */
1267 public Bundle getExtras() {
1268 return mExtras;
1269 }
1270
1271 /**
Alex Hills42b0c4d2016-04-26 13:35:36 -04001272 * Return whether the platform should automatically generate possible replies for this
1273 * {@link Action}
1274 */
1275 public boolean getAllowGeneratedReplies() {
1276 return mAllowGeneratedReplies;
1277 }
1278
1279 /**
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001280 * Get the list of inputs to be collected from the user when this action is sent.
Shane Brennan472a3b32016-12-12 15:28:10 -08001281 * May return null if no remote inputs were added. Only returns inputs which accept
1282 * a text input. For inputs which only accept data use {@link #getDataOnlyRemoteInputs}.
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001283 */
1284 public RemoteInput[] getRemoteInputs() {
1285 return mRemoteInputs;
1286 }
1287
1288 /**
Shane Brennan472a3b32016-12-12 15:28:10 -08001289 * Get the list of inputs to be collected from the user that ONLY accept data when this
1290 * action is sent. These remote inputs are guaranteed to return true on a call to
1291 * {@link RemoteInput#isDataOnly}.
1292 *
1293 * May return null if no data-only remote inputs were added.
1294 *
1295 * This method exists so that legacy RemoteInput collectors that pre-date the addition
1296 * of non-textual RemoteInputs do not access these remote inputs.
1297 */
1298 public RemoteInput[] getDataOnlyRemoteInputs() {
1299 return (RemoteInput[]) mExtras.getParcelableArray(EXTRA_DATA_ONLY_INPUTS);
1300 }
1301
1302 /**
Griff Hazen959591e2014-05-15 22:26:18 -07001303 * Builder class for {@link Action} objects.
1304 */
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001305 public static final class Builder {
Dan Sandler86647982015-05-13 23:41:13 -04001306 private final Icon mIcon;
Griff Hazen959591e2014-05-15 22:26:18 -07001307 private final CharSequence mTitle;
1308 private final PendingIntent mIntent;
Alex Hills1f27f882017-01-12 11:24:46 -05001309 private boolean mAllowGeneratedReplies = true;
Griff Hazen959591e2014-05-15 22:26:18 -07001310 private final Bundle mExtras;
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001311 private ArrayList<RemoteInput> mRemoteInputs;
Griff Hazen959591e2014-05-15 22:26:18 -07001312
1313 /**
1314 * Construct a new builder for {@link Action} object.
1315 * @param icon icon to show for this action
1316 * @param title the title of the action
1317 * @param intent the {@link PendingIntent} to fire when users trigger this action
1318 */
Dan Sandler86647982015-05-13 23:41:13 -04001319 @Deprecated
Griff Hazen959591e2014-05-15 22:26:18 -07001320 public Builder(int icon, CharSequence title, PendingIntent intent) {
Adrian Roos7af53622016-10-12 13:44:05 -07001321 this(Icon.createWithResource("", icon), title, intent);
Dan Sandler86647982015-05-13 23:41:13 -04001322 }
1323
1324 /**
1325 * Construct a new builder for {@link Action} object.
1326 * @param icon icon to show for this action
1327 * @param title the title of the action
1328 * @param intent the {@link PendingIntent} to fire when users trigger this action
1329 */
1330 public Builder(Icon icon, CharSequence title, PendingIntent intent) {
Alex Hills1f27f882017-01-12 11:24:46 -05001331 this(icon, title, intent, new Bundle(), null, true);
Griff Hazen959591e2014-05-15 22:26:18 -07001332 }
1333
1334 /**
1335 * Construct a new builder for {@link Action} object using the fields from an
1336 * {@link Action}.
1337 * @param action the action to read fields from.
1338 */
1339 public Builder(Action action) {
Adrian Roos7af53622016-10-12 13:44:05 -07001340 this(action.getIcon(), action.title, action.actionIntent,
1341 new Bundle(action.mExtras), action.getRemoteInputs(),
1342 action.getAllowGeneratedReplies());
Griff Hazen959591e2014-05-15 22:26:18 -07001343 }
1344
Dan Sandler86647982015-05-13 23:41:13 -04001345 private Builder(Icon icon, CharSequence title, PendingIntent intent, Bundle extras,
Adrian Roos7af53622016-10-12 13:44:05 -07001346 RemoteInput[] remoteInputs, boolean allowGeneratedReplies) {
Griff Hazen959591e2014-05-15 22:26:18 -07001347 mIcon = icon;
1348 mTitle = title;
1349 mIntent = intent;
1350 mExtras = extras;
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001351 if (remoteInputs != null) {
1352 mRemoteInputs = new ArrayList<RemoteInput>(remoteInputs.length);
1353 Collections.addAll(mRemoteInputs, remoteInputs);
1354 }
Adrian Roos7af53622016-10-12 13:44:05 -07001355 mAllowGeneratedReplies = allowGeneratedReplies;
Griff Hazen959591e2014-05-15 22:26:18 -07001356 }
1357
1358 /**
1359 * Merge additional metadata into this builder.
1360 *
1361 * <p>Values within the Bundle will replace existing extras values in this Builder.
1362 *
1363 * @see Notification.Action#extras
1364 */
1365 public Builder addExtras(Bundle extras) {
1366 if (extras != null) {
1367 mExtras.putAll(extras);
1368 }
1369 return this;
1370 }
1371
1372 /**
1373 * Get the metadata Bundle used by this Builder.
1374 *
1375 * <p>The returned Bundle is shared with this Builder.
1376 */
1377 public Bundle getExtras() {
1378 return mExtras;
1379 }
1380
1381 /**
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001382 * Add an input to be collected from the user when this action is sent.
1383 * Response values can be retrieved from the fired intent by using the
1384 * {@link RemoteInput#getResultsFromIntent} function.
1385 * @param remoteInput a {@link RemoteInput} to add to the action
1386 * @return this object for method chaining
1387 */
1388 public Builder addRemoteInput(RemoteInput remoteInput) {
1389 if (mRemoteInputs == null) {
1390 mRemoteInputs = new ArrayList<RemoteInput>();
1391 }
1392 mRemoteInputs.add(remoteInput);
1393 return this;
1394 }
1395
1396 /**
Alex Hills42b0c4d2016-04-26 13:35:36 -04001397 * Set whether the platform should automatically generate possible replies to add to
1398 * {@link RemoteInput#getChoices()}. If the {@link Action} doesn't have a
1399 * {@link RemoteInput}, this has no effect.
1400 * @param allowGeneratedReplies {@code true} to allow generated replies, {@code false}
1401 * otherwise
1402 * @return this object for method chaining
Alex Hills1f27f882017-01-12 11:24:46 -05001403 * The default value is {@code true}
Alex Hills42b0c4d2016-04-26 13:35:36 -04001404 */
1405 public Builder setAllowGeneratedReplies(boolean allowGeneratedReplies) {
1406 mAllowGeneratedReplies = allowGeneratedReplies;
1407 return this;
1408 }
1409
1410 /**
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001411 * Apply an extender to this action builder. Extenders may be used to add
1412 * metadata or change options on this builder.
1413 */
Griff Hazen61a9e862014-05-22 16:05:19 -07001414 public Builder extend(Extender extender) {
1415 extender.extend(this);
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001416 return this;
1417 }
1418
1419 /**
Griff Hazen959591e2014-05-15 22:26:18 -07001420 * Combine all of the options that have been set and return a new {@link Action}
1421 * object.
1422 * @return the built action
1423 */
1424 public Action build() {
Shane Brennan472a3b32016-12-12 15:28:10 -08001425 ArrayList<RemoteInput> dataOnlyInputs = new ArrayList<>();
1426 RemoteInput[] previousDataInputs =
1427 (RemoteInput[]) mExtras.getParcelableArray(EXTRA_DATA_ONLY_INPUTS);
Felipe Lemedfd11962017-01-18 18:38:46 -08001428 if (previousDataInputs != null) {
Shane Brennan472a3b32016-12-12 15:28:10 -08001429 for (RemoteInput input : previousDataInputs) {
1430 dataOnlyInputs.add(input);
1431 }
1432 }
1433 List<RemoteInput> textInputs = new ArrayList<>();
1434 if (mRemoteInputs != null) {
1435 for (RemoteInput input : mRemoteInputs) {
1436 if (input.isDataOnly()) {
1437 dataOnlyInputs.add(input);
1438 } else {
1439 textInputs.add(input);
1440 }
1441 }
1442 }
1443 if (!dataOnlyInputs.isEmpty()) {
1444 RemoteInput[] dataInputsArr =
1445 dataOnlyInputs.toArray(new RemoteInput[dataOnlyInputs.size()]);
1446 mExtras.putParcelableArray(EXTRA_DATA_ONLY_INPUTS, dataInputsArr);
1447 }
1448 RemoteInput[] textInputsArr = textInputs.isEmpty()
1449 ? null : textInputs.toArray(new RemoteInput[textInputs.size()]);
1450 return new Action(mIcon, mTitle, mIntent, mExtras, textInputsArr,
Alex Hills42b0c4d2016-04-26 13:35:36 -04001451 mAllowGeneratedReplies);
Griff Hazen959591e2014-05-15 22:26:18 -07001452 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001453 }
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001454
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001455 @Override
1456 public Action clone() {
1457 return new Action(
Dan Sandler86647982015-05-13 23:41:13 -04001458 getIcon(),
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001459 title,
1460 actionIntent, // safe to alias
Julia Reynolds53c934c2016-09-16 14:03:43 -04001461 mExtras == null ? new Bundle() : new Bundle(mExtras),
Alex Hills42b0c4d2016-04-26 13:35:36 -04001462 getRemoteInputs(),
1463 getAllowGeneratedReplies());
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001464 }
1465 @Override
1466 public int describeContents() {
1467 return 0;
1468 }
1469 @Override
1470 public void writeToParcel(Parcel out, int flags) {
Dan Sandler86647982015-05-13 23:41:13 -04001471 final Icon ic = getIcon();
1472 if (ic != null) {
1473 out.writeInt(1);
1474 ic.writeToParcel(out, 0);
1475 } else {
1476 out.writeInt(0);
1477 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001478 TextUtils.writeToParcel(title, out, flags);
1479 if (actionIntent != null) {
1480 out.writeInt(1);
1481 actionIntent.writeToParcel(out, flags);
1482 } else {
1483 out.writeInt(0);
1484 }
Griff Hazen959591e2014-05-15 22:26:18 -07001485 out.writeBundle(mExtras);
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001486 out.writeTypedArray(mRemoteInputs, flags);
Alex Hills42b0c4d2016-04-26 13:35:36 -04001487 out.writeInt(mAllowGeneratedReplies ? 1 : 0);
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001488 }
Griff Hazen959591e2014-05-15 22:26:18 -07001489 public static final Parcelable.Creator<Action> CREATOR =
1490 new Parcelable.Creator<Action>() {
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001491 public Action createFromParcel(Parcel in) {
1492 return new Action(in);
1493 }
1494 public Action[] newArray(int size) {
1495 return new Action[size];
1496 }
1497 };
Griff Hazen61a9e862014-05-22 16:05:19 -07001498
1499 /**
1500 * Extender interface for use with {@link Builder#extend}. Extenders may be used to add
1501 * metadata or change options on an action builder.
1502 */
1503 public interface Extender {
1504 /**
1505 * Apply this extender to a notification action builder.
1506 * @param builder the builder to be modified.
1507 * @return the build object for chaining.
1508 */
1509 public Builder extend(Builder builder);
1510 }
1511
1512 /**
1513 * Wearable extender for notification actions. To add extensions to an action,
1514 * create a new {@link android.app.Notification.Action.WearableExtender} object using
1515 * the {@code WearableExtender()} constructor and apply it to a
1516 * {@link android.app.Notification.Action.Builder} using
1517 * {@link android.app.Notification.Action.Builder#extend}.
1518 *
1519 * <pre class="prettyprint">
1520 * Notification.Action action = new Notification.Action.Builder(
1521 * R.drawable.archive_all, "Archive all", actionIntent)
Griff Hazen14f57992014-05-26 09:07:14 -07001522 * .extend(new Notification.Action.WearableExtender()
Griff Hazen61a9e862014-05-22 16:05:19 -07001523 * .setAvailableOffline(false))
Griff Hazen14f57992014-05-26 09:07:14 -07001524 * .build();</pre>
Griff Hazen61a9e862014-05-22 16:05:19 -07001525 */
1526 public static final class WearableExtender implements Extender {
1527 /** Notification action extra which contains wearable extensions */
1528 private static final String EXTRA_WEARABLE_EXTENSIONS = "android.wearable.EXTENSIONS";
1529
Pete Gastaf6781d2014-10-07 15:17:05 -04001530 // Keys within EXTRA_WEARABLE_EXTENSIONS for wearable options.
Griff Hazen61a9e862014-05-22 16:05:19 -07001531 private static final String KEY_FLAGS = "flags";
Pete Gastaf6781d2014-10-07 15:17:05 -04001532 private static final String KEY_IN_PROGRESS_LABEL = "inProgressLabel";
1533 private static final String KEY_CONFIRM_LABEL = "confirmLabel";
1534 private static final String KEY_CANCEL_LABEL = "cancelLabel";
Griff Hazen61a9e862014-05-22 16:05:19 -07001535
1536 // Flags bitwise-ored to mFlags
1537 private static final int FLAG_AVAILABLE_OFFLINE = 0x1;
Alex Hills9ab3a232016-04-05 14:54:56 -04001538 private static final int FLAG_HINT_LAUNCHES_ACTIVITY = 1 << 1;
Alex Hills9f087612016-06-07 09:08:59 -04001539 private static final int FLAG_HINT_DISPLAY_INLINE = 1 << 2;
Griff Hazen61a9e862014-05-22 16:05:19 -07001540
1541 // Default value for flags integer
1542 private static final int DEFAULT_FLAGS = FLAG_AVAILABLE_OFFLINE;
1543
1544 private int mFlags = DEFAULT_FLAGS;
1545
Pete Gastaf6781d2014-10-07 15:17:05 -04001546 private CharSequence mInProgressLabel;
1547 private CharSequence mConfirmLabel;
1548 private CharSequence mCancelLabel;
1549
Griff Hazen61a9e862014-05-22 16:05:19 -07001550 /**
1551 * Create a {@link android.app.Notification.Action.WearableExtender} with default
1552 * options.
1553 */
1554 public WearableExtender() {
1555 }
1556
1557 /**
1558 * Create a {@link android.app.Notification.Action.WearableExtender} by reading
1559 * wearable options present in an existing notification action.
1560 * @param action the notification action to inspect.
1561 */
1562 public WearableExtender(Action action) {
1563 Bundle wearableBundle = action.getExtras().getBundle(EXTRA_WEARABLE_EXTENSIONS);
1564 if (wearableBundle != null) {
1565 mFlags = wearableBundle.getInt(KEY_FLAGS, DEFAULT_FLAGS);
Pete Gastaf6781d2014-10-07 15:17:05 -04001566 mInProgressLabel = wearableBundle.getCharSequence(KEY_IN_PROGRESS_LABEL);
1567 mConfirmLabel = wearableBundle.getCharSequence(KEY_CONFIRM_LABEL);
1568 mCancelLabel = wearableBundle.getCharSequence(KEY_CANCEL_LABEL);
Griff Hazen61a9e862014-05-22 16:05:19 -07001569 }
1570 }
1571
1572 /**
1573 * Apply wearable extensions to a notification action that is being built. This is
1574 * typically called by the {@link android.app.Notification.Action.Builder#extend}
1575 * method of {@link android.app.Notification.Action.Builder}.
1576 */
1577 @Override
1578 public Action.Builder extend(Action.Builder builder) {
1579 Bundle wearableBundle = new Bundle();
1580
1581 if (mFlags != DEFAULT_FLAGS) {
1582 wearableBundle.putInt(KEY_FLAGS, mFlags);
1583 }
Pete Gastaf6781d2014-10-07 15:17:05 -04001584 if (mInProgressLabel != null) {
1585 wearableBundle.putCharSequence(KEY_IN_PROGRESS_LABEL, mInProgressLabel);
1586 }
1587 if (mConfirmLabel != null) {
1588 wearableBundle.putCharSequence(KEY_CONFIRM_LABEL, mConfirmLabel);
1589 }
1590 if (mCancelLabel != null) {
1591 wearableBundle.putCharSequence(KEY_CANCEL_LABEL, mCancelLabel);
1592 }
Griff Hazen61a9e862014-05-22 16:05:19 -07001593
1594 builder.getExtras().putBundle(EXTRA_WEARABLE_EXTENSIONS, wearableBundle);
1595 return builder;
1596 }
1597
1598 @Override
1599 public WearableExtender clone() {
1600 WearableExtender that = new WearableExtender();
1601 that.mFlags = this.mFlags;
Pete Gastaf6781d2014-10-07 15:17:05 -04001602 that.mInProgressLabel = this.mInProgressLabel;
1603 that.mConfirmLabel = this.mConfirmLabel;
1604 that.mCancelLabel = this.mCancelLabel;
Griff Hazen61a9e862014-05-22 16:05:19 -07001605 return that;
1606 }
1607
1608 /**
1609 * Set whether this action is available when the wearable device is not connected to
1610 * a companion device. The user can still trigger this action when the wearable device is
1611 * offline, but a visual hint will indicate that the action may not be available.
1612 * Defaults to true.
1613 */
1614 public WearableExtender setAvailableOffline(boolean availableOffline) {
1615 setFlag(FLAG_AVAILABLE_OFFLINE, availableOffline);
1616 return this;
1617 }
1618
1619 /**
1620 * Get whether this action is available when the wearable device is not connected to
1621 * a companion device. The user can still trigger this action when the wearable device is
1622 * offline, but a visual hint will indicate that the action may not be available.
1623 * Defaults to true.
1624 */
1625 public boolean isAvailableOffline() {
1626 return (mFlags & FLAG_AVAILABLE_OFFLINE) != 0;
1627 }
1628
1629 private void setFlag(int mask, boolean value) {
1630 if (value) {
1631 mFlags |= mask;
1632 } else {
1633 mFlags &= ~mask;
1634 }
1635 }
Pete Gastaf6781d2014-10-07 15:17:05 -04001636
1637 /**
1638 * Set a label to display while the wearable is preparing to automatically execute the
1639 * action. This is usually a 'ing' verb ending in ellipsis like "Sending..."
1640 *
1641 * @param label the label to display while the action is being prepared to execute
1642 * @return this object for method chaining
1643 */
1644 public WearableExtender setInProgressLabel(CharSequence label) {
1645 mInProgressLabel = label;
1646 return this;
1647 }
1648
1649 /**
1650 * Get the label to display while the wearable is preparing to automatically execute
1651 * the action. This is usually a 'ing' verb ending in ellipsis like "Sending..."
1652 *
1653 * @return the label to display while the action is being prepared to execute
1654 */
1655 public CharSequence getInProgressLabel() {
1656 return mInProgressLabel;
1657 }
1658
1659 /**
1660 * Set a label to display to confirm that the action should be executed.
1661 * This is usually an imperative verb like "Send".
1662 *
1663 * @param label the label to confirm the action should be executed
1664 * @return this object for method chaining
1665 */
1666 public WearableExtender setConfirmLabel(CharSequence label) {
1667 mConfirmLabel = label;
1668 return this;
1669 }
1670
1671 /**
1672 * Get the label to display to confirm that the action should be executed.
1673 * This is usually an imperative verb like "Send".
1674 *
1675 * @return the label to confirm the action should be executed
1676 */
1677 public CharSequence getConfirmLabel() {
1678 return mConfirmLabel;
1679 }
1680
1681 /**
1682 * Set a label to display to cancel the action.
1683 * This is usually an imperative verb, like "Cancel".
1684 *
1685 * @param label the label to display to cancel the action
1686 * @return this object for method chaining
1687 */
1688 public WearableExtender setCancelLabel(CharSequence label) {
1689 mCancelLabel = label;
1690 return this;
1691 }
1692
1693 /**
1694 * Get the label to display to cancel the action.
1695 * This is usually an imperative verb like "Cancel".
1696 *
1697 * @return the label to display to cancel the action
1698 */
1699 public CharSequence getCancelLabel() {
1700 return mCancelLabel;
1701 }
Alex Hills9ab3a232016-04-05 14:54:56 -04001702
1703 /**
1704 * Set a hint that this Action will launch an {@link Activity} directly, telling the
1705 * platform that it can generate the appropriate transitions.
1706 * @param hintLaunchesActivity {@code true} if the content intent will launch
1707 * an activity and transitions should be generated, false otherwise.
1708 * @return this object for method chaining
1709 */
Alex Hills4ec3ff42016-04-12 11:36:18 -04001710 public WearableExtender setHintLaunchesActivity(
Alex Hills9ab3a232016-04-05 14:54:56 -04001711 boolean hintLaunchesActivity) {
1712 setFlag(FLAG_HINT_LAUNCHES_ACTIVITY, hintLaunchesActivity);
1713 return this;
1714 }
1715
1716 /**
1717 * Get a hint that this Action will launch an {@link Activity} directly, telling the
1718 * platform that it can generate the appropriate transitions
1719 * @return {@code true} if the content intent will launch an activity and transitions
1720 * should be generated, false otherwise. The default value is {@code false} if this was
1721 * never set.
1722 */
Alex Hills4ec3ff42016-04-12 11:36:18 -04001723 public boolean getHintLaunchesActivity() {
Alex Hills9ab3a232016-04-05 14:54:56 -04001724 return (mFlags & FLAG_HINT_LAUNCHES_ACTIVITY) != 0;
1725 }
Alex Hills9f087612016-06-07 09:08:59 -04001726
1727 /**
1728 * Set a hint that this Action should be displayed inline.
1729 *
1730 * @param hintDisplayInline {@code true} if action should be displayed inline, false
1731 * otherwise
1732 * @return this object for method chaining
1733 */
1734 public WearableExtender setHintDisplayActionInline(
1735 boolean hintDisplayInline) {
1736 setFlag(FLAG_HINT_DISPLAY_INLINE, hintDisplayInline);
1737 return this;
1738 }
1739
1740 /**
1741 * Get a hint that this Action should be displayed inline.
1742 *
Dmitri Plotnikov9e9cfd92016-12-21 10:52:26 -08001743 * @return {@code true} if the Action should be displayed inline, {@code false}
Alex Hills9f087612016-06-07 09:08:59 -04001744 * otherwise. The default value is {@code false} if this was never set.
1745 */
1746 public boolean getHintDisplayActionInline() {
1747 return (mFlags & FLAG_HINT_DISPLAY_INLINE) != 0;
1748 }
Griff Hazen61a9e862014-05-22 16:05:19 -07001749 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001750 }
1751
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001752 /**
1753 * Array of all {@link Action} structures attached to this notification by
1754 * {@link Builder#addAction(int, CharSequence, PendingIntent)}. Mostly useful for instances of
1755 * {@link android.service.notification.NotificationListenerService} that provide an alternative
1756 * interface for invoking actions.
1757 */
Daniel Sandlerea2a3172013-02-20 22:24:20 -05001758 public Action[] actions;
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001759
1760 /**
Dan Sandler0bf2ed82013-12-21 23:33:41 -06001761 * Replacement version of this notification whose content will be shown
1762 * in an insecure context such as atop a secure keyguard. See {@link #visibility}
1763 * and {@link #VISIBILITY_PUBLIC}.
1764 */
1765 public Notification publicVersion;
1766
1767 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001768 * Constructs a Notification object with default values.
Joe Onorato46439ce2010-11-19 13:56:21 -08001769 * You might want to consider using {@link Builder} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001770 */
1771 public Notification()
1772 {
1773 this.when = System.currentTimeMillis();
Selim Cinekb85f36fd2016-04-20 18:46:36 -07001774 this.creationTime = System.currentTimeMillis();
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001775 this.priority = PRIORITY_DEFAULT;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001776 }
1777
1778 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001779 * @hide
1780 */
1781 public Notification(Context context, int icon, CharSequence tickerText, long when,
1782 CharSequence contentTitle, CharSequence contentText, Intent contentIntent)
1783 {
Chris Wren1ce4b6d2015-06-11 10:19:43 -04001784 new Builder(context)
1785 .setWhen(when)
1786 .setSmallIcon(icon)
1787 .setTicker(tickerText)
1788 .setContentTitle(contentTitle)
1789 .setContentText(contentText)
1790 .setContentIntent(PendingIntent.getActivity(context, 0, contentIntent, 0))
1791 .buildInto(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001792 }
1793
1794 /**
1795 * Constructs a Notification object with the information needed to
1796 * have a status bar icon without the standard expanded view.
1797 *
1798 * @param icon The resource id of the icon to put in the status bar.
1799 * @param tickerText The text that flows by in the status bar when the notification first
1800 * activates.
1801 * @param when The time to show in the time field. In the System.currentTimeMillis
1802 * timebase.
Joe Onorato46439ce2010-11-19 13:56:21 -08001803 *
1804 * @deprecated Use {@link Builder} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001805 */
Joe Onorato46439ce2010-11-19 13:56:21 -08001806 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001807 public Notification(int icon, CharSequence tickerText, long when)
1808 {
1809 this.icon = icon;
1810 this.tickerText = tickerText;
1811 this.when = when;
Selim Cinekb85f36fd2016-04-20 18:46:36 -07001812 this.creationTime = System.currentTimeMillis();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001813 }
1814
1815 /**
1816 * Unflatten the notification from a parcel.
1817 */
Svet Ganovddb94882016-06-23 19:55:24 -07001818 @SuppressWarnings("unchecked")
1819 public Notification(Parcel parcel) {
1820 // IMPORTANT: Add unmarshaling code in readFromParcel as the pending
1821 // intents in extras are always written as the last entry.
1822 readFromParcelImpl(parcel);
1823 // Must be read last!
Felipe Lemedd85da62016-06-28 11:29:54 -07001824 allPendingIntents = (ArraySet<PendingIntent>) parcel.readArraySet(null);
Svet Ganovddb94882016-06-23 19:55:24 -07001825 }
1826
1827 private void readFromParcelImpl(Parcel parcel)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001828 {
1829 int version = parcel.readInt();
1830
1831 when = parcel.readLong();
Selim Cinekb85f36fd2016-04-20 18:46:36 -07001832 creationTime = parcel.readLong();
Dan Sandler3936e7a2015-05-19 20:59:12 -04001833 if (parcel.readInt() != 0) {
1834 mSmallIcon = Icon.CREATOR.createFromParcel(parcel);
Dan Sandler4e787062015-06-17 15:09:48 -04001835 if (mSmallIcon.getType() == Icon.TYPE_RESOURCE) {
1836 icon = mSmallIcon.getResId();
1837 }
Dan Sandler3936e7a2015-05-19 20:59:12 -04001838 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001839 number = parcel.readInt();
1840 if (parcel.readInt() != 0) {
1841 contentIntent = PendingIntent.CREATOR.createFromParcel(parcel);
1842 }
1843 if (parcel.readInt() != 0) {
1844 deleteIntent = PendingIntent.CREATOR.createFromParcel(parcel);
1845 }
1846 if (parcel.readInt() != 0) {
1847 tickerText = TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(parcel);
1848 }
1849 if (parcel.readInt() != 0) {
Joe Onorato46439ce2010-11-19 13:56:21 -08001850 tickerView = RemoteViews.CREATOR.createFromParcel(parcel);
Joe Onoratoef1e7762010-09-17 18:38:38 -04001851 }
1852 if (parcel.readInt() != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001853 contentView = RemoteViews.CREATOR.createFromParcel(parcel);
1854 }
Joe Onorato561d3852010-11-20 18:09:34 -08001855 if (parcel.readInt() != 0) {
Dan Sandlerd63f9322015-05-06 15:18:49 -04001856 mLargeIcon = Icon.CREATOR.createFromParcel(parcel);
Joe Onorato561d3852010-11-20 18:09:34 -08001857 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001858 defaults = parcel.readInt();
1859 flags = parcel.readInt();
1860 if (parcel.readInt() != 0) {
1861 sound = Uri.CREATOR.createFromParcel(parcel);
1862 }
1863
1864 audioStreamType = parcel.readInt();
John Spurlockc0650f022014-07-19 13:22:39 -04001865 if (parcel.readInt() != 0) {
1866 audioAttributes = AudioAttributes.CREATOR.createFromParcel(parcel);
1867 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001868 vibrate = parcel.createLongArray();
1869 ledARGB = parcel.readInt();
1870 ledOnMS = parcel.readInt();
1871 ledOffMS = parcel.readInt();
1872 iconLevel = parcel.readInt();
Daniel Sandlere46cbd32010-06-17 10:35:26 -04001873
1874 if (parcel.readInt() != 0) {
1875 fullScreenIntent = PendingIntent.CREATOR.createFromParcel(parcel);
1876 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001877
1878 priority = parcel.readInt();
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001879
John Spurlockfd7f1e02014-03-18 16:41:57 -04001880 category = parcel.readString();
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001881
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001882 mGroupKey = parcel.readString();
1883
1884 mSortKey = parcel.readString();
1885
Jeff Sharkeya04c7a72016-03-18 12:20:36 -06001886 extras = Bundle.setDefusable(parcel.readBundle(), true); // may be null
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001887
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001888 actions = parcel.createTypedArray(Action.CREATOR); // may be null
1889
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001890 if (parcel.readInt() != 0) {
Daniel Sandlerf3b73432012-03-27 15:01:25 -04001891 bigContentView = RemoteViews.CREATOR.createFromParcel(parcel);
1892 }
Dan Sandler0bf2ed82013-12-21 23:33:41 -06001893
Chris Wren8fd39ec2014-02-27 17:43:26 -05001894 if (parcel.readInt() != 0) {
1895 headsUpContentView = RemoteViews.CREATOR.createFromParcel(parcel);
1896 }
1897
Dan Sandler0bf2ed82013-12-21 23:33:41 -06001898 visibility = parcel.readInt();
1899
1900 if (parcel.readInt() != 0) {
1901 publicVersion = Notification.CREATOR.createFromParcel(parcel);
1902 }
Dan Sandler26e81cf2014-05-06 10:01:27 -04001903
1904 color = parcel.readInt();
Julia Reynoldsb5e44b72016-08-16 15:00:25 -04001905
1906 if (parcel.readInt() != 0) {
1907 mChannelId = parcel.readString();
1908 }
Julia Reynolds2a128742016-11-28 14:29:25 -05001909 mTimeout = parcel.readLong();
Julia Reynolds13d898c2017-02-02 12:22:05 -05001910
1911 if (parcel.readInt() != 0) {
1912 mShortcutId = parcel.readString();
1913 }
1914
1915 mBadgeIcon = parcel.readInt();
Julia Reynolds3aedded2017-03-31 14:42:09 -04001916
1917 if (parcel.readInt() != 0) {
1918 mSettingsText = TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(parcel);
1919 }
Julia Reynoldsa79c3712017-04-21 10:29:57 -04001920
1921 mGroupAlertBehavior = parcel.readInt();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001922 }
1923
Andy Stadler110988c2010-12-03 14:29:16 -08001924 @Override
Joe Onorato18e69df2010-05-17 22:26:12 -07001925 public Notification clone() {
1926 Notification that = new Notification();
Daniel Sandler1a497d32013-04-18 14:52:45 -04001927 cloneInto(that, true);
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001928 return that;
1929 }
Joe Onorato18e69df2010-05-17 22:26:12 -07001930
Daniel Sandler1a497d32013-04-18 14:52:45 -04001931 /**
1932 * Copy all (or if heavy is false, all except Bitmaps and RemoteViews) members
1933 * of this into that.
1934 * @hide
1935 */
1936 public void cloneInto(Notification that, boolean heavy) {
Joe Onorato18e69df2010-05-17 22:26:12 -07001937 that.when = this.when;
Selim Cinekb85f36fd2016-04-20 18:46:36 -07001938 that.creationTime = this.creationTime;
Dan Sandlerd63f9322015-05-06 15:18:49 -04001939 that.mSmallIcon = this.mSmallIcon;
Joe Onorato18e69df2010-05-17 22:26:12 -07001940 that.number = this.number;
1941
1942 // PendingIntents are global, so there's no reason (or way) to clone them.
1943 that.contentIntent = this.contentIntent;
1944 that.deleteIntent = this.deleteIntent;
Daniel Sandlere46cbd32010-06-17 10:35:26 -04001945 that.fullScreenIntent = this.fullScreenIntent;
Joe Onorato18e69df2010-05-17 22:26:12 -07001946
1947 if (this.tickerText != null) {
1948 that.tickerText = this.tickerText.toString();
1949 }
Daniel Sandler1a497d32013-04-18 14:52:45 -04001950 if (heavy && this.tickerView != null) {
Joe Onorato46439ce2010-11-19 13:56:21 -08001951 that.tickerView = this.tickerView.clone();
Joe Onoratoef1e7762010-09-17 18:38:38 -04001952 }
Daniel Sandler1a497d32013-04-18 14:52:45 -04001953 if (heavy && this.contentView != null) {
Joe Onorato18e69df2010-05-17 22:26:12 -07001954 that.contentView = this.contentView.clone();
1955 }
Dan Sandlerd63f9322015-05-06 15:18:49 -04001956 if (heavy && this.mLargeIcon != null) {
1957 that.mLargeIcon = this.mLargeIcon;
Joe Onorato561d3852010-11-20 18:09:34 -08001958 }
Jozef BABJAKa8b91832011-02-22 08:05:08 +01001959 that.iconLevel = this.iconLevel;
Joe Onorato18e69df2010-05-17 22:26:12 -07001960 that.sound = this.sound; // android.net.Uri is immutable
1961 that.audioStreamType = this.audioStreamType;
John Spurlockc0650f022014-07-19 13:22:39 -04001962 if (this.audioAttributes != null) {
1963 that.audioAttributes = new AudioAttributes.Builder(this.audioAttributes).build();
1964 }
Joe Onorato18e69df2010-05-17 22:26:12 -07001965
1966 final long[] vibrate = this.vibrate;
1967 if (vibrate != null) {
1968 final int N = vibrate.length;
1969 final long[] vib = that.vibrate = new long[N];
1970 System.arraycopy(vibrate, 0, vib, 0, N);
1971 }
1972
1973 that.ledARGB = this.ledARGB;
1974 that.ledOnMS = this.ledOnMS;
1975 that.ledOffMS = this.ledOffMS;
1976 that.defaults = this.defaults;
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001977
Joe Onorato18e69df2010-05-17 22:26:12 -07001978 that.flags = this.flags;
1979
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001980 that.priority = this.priority;
Joe Malin8d40d042012-11-05 11:36:40 -08001981
John Spurlockfd7f1e02014-03-18 16:41:57 -04001982 that.category = this.category;
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001983
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001984 that.mGroupKey = this.mGroupKey;
1985
1986 that.mSortKey = this.mSortKey;
1987
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001988 if (this.extras != null) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04001989 try {
1990 that.extras = new Bundle(this.extras);
1991 // will unparcel
1992 that.extras.size();
1993 } catch (BadParcelableException e) {
1994 Log.e(TAG, "could not unparcel extras from notification: " + this, e);
1995 that.extras = null;
1996 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001997 }
1998
Felipe Lemedd85da62016-06-28 11:29:54 -07001999 if (!ArrayUtils.isEmpty(allPendingIntents)) {
2000 that.allPendingIntents = new ArraySet<>(allPendingIntents);
Svet Ganovddb94882016-06-23 19:55:24 -07002001 }
2002
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002003 if (this.actions != null) {
2004 that.actions = new Action[this.actions.length];
2005 for(int i=0; i<this.actions.length; i++) {
liangweikang63b03b52017-03-16 19:22:15 +08002006 if ( this.actions[i] != null) {
2007 that.actions[i] = this.actions[i].clone();
2008 }
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002009 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002010 }
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002011
Daniel Sandler1a497d32013-04-18 14:52:45 -04002012 if (heavy && this.bigContentView != null) {
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002013 that.bigContentView = this.bigContentView.clone();
2014 }
Daniel Sandler1a497d32013-04-18 14:52:45 -04002015
Chris Wren8fd39ec2014-02-27 17:43:26 -05002016 if (heavy && this.headsUpContentView != null) {
2017 that.headsUpContentView = this.headsUpContentView.clone();
2018 }
2019
Dan Sandler0bf2ed82013-12-21 23:33:41 -06002020 that.visibility = this.visibility;
2021
2022 if (this.publicVersion != null) {
2023 that.publicVersion = new Notification();
2024 this.publicVersion.cloneInto(that.publicVersion, heavy);
2025 }
2026
Dan Sandler26e81cf2014-05-06 10:01:27 -04002027 that.color = this.color;
2028
Julia Reynoldsb5e44b72016-08-16 15:00:25 -04002029 that.mChannelId = this.mChannelId;
Julia Reynolds2a128742016-11-28 14:29:25 -05002030 that.mTimeout = this.mTimeout;
Julia Reynolds3aedded2017-03-31 14:42:09 -04002031 that.mShortcutId = this.mShortcutId;
2032 that.mBadgeIcon = this.mBadgeIcon;
2033 that.mSettingsText = this.mSettingsText;
Julia Reynoldsa79c3712017-04-21 10:29:57 -04002034 that.mGroupAlertBehavior = this.mGroupAlertBehavior;
Julia Reynoldsb5e44b72016-08-16 15:00:25 -04002035
Daniel Sandler1a497d32013-04-18 14:52:45 -04002036 if (!heavy) {
2037 that.lightenPayload(); // will clean out extras
2038 }
2039 }
2040
2041 /**
2042 * Removes heavyweight parts of the Notification object for archival or for sending to
2043 * listeners when the full contents are not necessary.
2044 * @hide
2045 */
2046 public final void lightenPayload() {
2047 tickerView = null;
2048 contentView = null;
2049 bigContentView = null;
Chris Wren8fd39ec2014-02-27 17:43:26 -05002050 headsUpContentView = null;
Dan Sandlerd63f9322015-05-06 15:18:49 -04002051 mLargeIcon = null;
Dan Sandler50128532015-12-08 15:42:41 -05002052 if (extras != null && !extras.isEmpty()) {
2053 final Set<String> keyset = extras.keySet();
2054 final int N = keyset.size();
2055 final String[] keys = keyset.toArray(new String[N]);
2056 for (int i=0; i<N; i++) {
2057 final String key = keys[i];
Dmitri Plotnikov22281362017-01-30 11:16:21 -08002058 if (TvExtender.EXTRA_TV_EXTENDER.equals(key)) {
2059 continue;
2060 }
Dan Sandler50128532015-12-08 15:42:41 -05002061 final Object obj = extras.get(key);
2062 if (obj != null &&
2063 ( obj instanceof Parcelable
2064 || obj instanceof Parcelable[]
2065 || obj instanceof SparseArray
2066 || obj instanceof ArrayList)) {
2067 extras.remove(key);
2068 }
2069 }
Daniel Sandler1a497d32013-04-18 14:52:45 -04002070 }
Joe Onorato18e69df2010-05-17 22:26:12 -07002071 }
2072
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04002073 /**
2074 * Make sure this CharSequence is safe to put into a bundle, which basically
2075 * means it had better not be some custom Parcelable implementation.
2076 * @hide
2077 */
2078 public static CharSequence safeCharSequence(CharSequence cs) {
Christoph Studer535ec612014-09-03 15:47:47 +02002079 if (cs == null) return cs;
2080 if (cs.length() > MAX_CHARSEQUENCE_LENGTH) {
2081 cs = cs.subSequence(0, MAX_CHARSEQUENCE_LENGTH);
2082 }
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04002083 if (cs instanceof Parcelable) {
2084 Log.e(TAG, "warning: " + cs.getClass().getCanonicalName()
2085 + " instance is a custom Parcelable and not allowed in Notification");
2086 return cs.toString();
2087 }
Selim Cinek60a54252016-02-26 17:03:25 -08002088 return removeTextSizeSpans(cs);
2089 }
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04002090
Selim Cinek60a54252016-02-26 17:03:25 -08002091 private static CharSequence removeTextSizeSpans(CharSequence charSequence) {
2092 if (charSequence instanceof Spanned) {
2093 Spanned ss = (Spanned) charSequence;
2094 Object[] spans = ss.getSpans(0, ss.length(), Object.class);
2095 SpannableStringBuilder builder = new SpannableStringBuilder(ss.toString());
2096 for (Object span : spans) {
2097 Object resultSpan = span;
Selim Cinek89991a22016-03-07 19:51:54 -08002098 if (resultSpan instanceof CharacterStyle) {
2099 resultSpan = ((CharacterStyle) span).getUnderlying();
2100 }
2101 if (resultSpan instanceof TextAppearanceSpan) {
2102 TextAppearanceSpan originalSpan = (TextAppearanceSpan) resultSpan;
Selim Cinek60a54252016-02-26 17:03:25 -08002103 resultSpan = new TextAppearanceSpan(
2104 originalSpan.getFamily(),
2105 originalSpan.getTextStyle(),
2106 -1,
2107 originalSpan.getTextColor(),
2108 originalSpan.getLinkTextColor());
Selim Cinek89991a22016-03-07 19:51:54 -08002109 } else if (resultSpan instanceof RelativeSizeSpan
2110 || resultSpan instanceof AbsoluteSizeSpan) {
Selim Cinek60a54252016-02-26 17:03:25 -08002111 continue;
Selim Cinek89991a22016-03-07 19:51:54 -08002112 } else {
2113 resultSpan = span;
Selim Cinek60a54252016-02-26 17:03:25 -08002114 }
2115 builder.setSpan(resultSpan, ss.getSpanStart(span), ss.getSpanEnd(span),
2116 ss.getSpanFlags(span));
2117 }
2118 return builder;
2119 }
2120 return charSequence;
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04002121 }
2122
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002123 public int describeContents() {
2124 return 0;
2125 }
2126
2127 /**
Dan Sandler4e787062015-06-17 15:09:48 -04002128 * Flatten this notification into a parcel.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002129 */
Svet Ganovddb94882016-06-23 19:55:24 -07002130 public void writeToParcel(Parcel parcel, int flags) {
2131 // We need to mark all pending intents getting into the notification
2132 // system as being put there to later allow the notification ranker
2133 // to launch them and by doing so add the app to the battery saver white
2134 // list for a short period of time. The problem is that the system
2135 // cannot look into the extras as there may be parcelables there that
2136 // the platform does not know how to handle. To go around that we have
2137 // an explicit list of the pending intents in the extras bundle.
Felipe Lemedd85da62016-06-28 11:29:54 -07002138 final boolean collectPendingIntents = (allPendingIntents == null);
Svet Ganovddb94882016-06-23 19:55:24 -07002139 if (collectPendingIntents) {
2140 PendingIntent.setOnMarshaledListener(
2141 (PendingIntent intent, Parcel out, int outFlags) -> {
2142 if (parcel == out) {
Felipe Lemedd85da62016-06-28 11:29:54 -07002143 if (allPendingIntents == null) {
2144 allPendingIntents = new ArraySet<>();
Svet Ganovddb94882016-06-23 19:55:24 -07002145 }
Felipe Lemedd85da62016-06-28 11:29:54 -07002146 allPendingIntents.add(intent);
Svet Ganovddb94882016-06-23 19:55:24 -07002147 }
2148 });
2149 }
2150 try {
2151 // IMPORTANT: Add marshaling code in writeToParcelImpl as we
Julia Reynolds13d898c2017-02-02 12:22:05 -05002152 // want to intercept all pending events written to the parcel.
Svet Ganovddb94882016-06-23 19:55:24 -07002153 writeToParcelImpl(parcel, flags);
2154 // Must be written last!
Felipe Lemedd85da62016-06-28 11:29:54 -07002155 parcel.writeArraySet(allPendingIntents);
Svet Ganovddb94882016-06-23 19:55:24 -07002156 } finally {
2157 if (collectPendingIntents) {
2158 PendingIntent.setOnMarshaledListener(null);
2159 }
2160 }
2161 }
2162
2163 private void writeToParcelImpl(Parcel parcel, int flags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002164 parcel.writeInt(1);
2165
2166 parcel.writeLong(when);
Selim Cinekb85f36fd2016-04-20 18:46:36 -07002167 parcel.writeLong(creationTime);
Dan Sandler4e787062015-06-17 15:09:48 -04002168 if (mSmallIcon == null && icon != 0) {
2169 // you snuck an icon in here without using the builder; let's try to keep it
2170 mSmallIcon = Icon.createWithResource("", icon);
2171 }
Dan Sandler3936e7a2015-05-19 20:59:12 -04002172 if (mSmallIcon != null) {
2173 parcel.writeInt(1);
2174 mSmallIcon.writeToParcel(parcel, 0);
2175 } else {
2176 parcel.writeInt(0);
2177 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002178 parcel.writeInt(number);
2179 if (contentIntent != null) {
2180 parcel.writeInt(1);
2181 contentIntent.writeToParcel(parcel, 0);
2182 } else {
2183 parcel.writeInt(0);
2184 }
2185 if (deleteIntent != null) {
2186 parcel.writeInt(1);
2187 deleteIntent.writeToParcel(parcel, 0);
2188 } else {
2189 parcel.writeInt(0);
2190 }
2191 if (tickerText != null) {
2192 parcel.writeInt(1);
2193 TextUtils.writeToParcel(tickerText, parcel, flags);
2194 } else {
2195 parcel.writeInt(0);
2196 }
Joe Onorato46439ce2010-11-19 13:56:21 -08002197 if (tickerView != null) {
Joe Onoratoef1e7762010-09-17 18:38:38 -04002198 parcel.writeInt(1);
Joe Onorato46439ce2010-11-19 13:56:21 -08002199 tickerView.writeToParcel(parcel, 0);
Joe Onoratoef1e7762010-09-17 18:38:38 -04002200 } else {
2201 parcel.writeInt(0);
2202 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002203 if (contentView != null) {
2204 parcel.writeInt(1);
2205 contentView.writeToParcel(parcel, 0);
2206 } else {
2207 parcel.writeInt(0);
2208 }
Selim Cinek279fa862016-06-14 10:57:25 -07002209 if (mLargeIcon == null && largeIcon != null) {
2210 // you snuck an icon in here without using the builder; let's try to keep it
2211 mLargeIcon = Icon.createWithBitmap(largeIcon);
2212 }
Dan Sandlerd63f9322015-05-06 15:18:49 -04002213 if (mLargeIcon != null) {
Joe Onorato561d3852010-11-20 18:09:34 -08002214 parcel.writeInt(1);
Dan Sandlerd63f9322015-05-06 15:18:49 -04002215 mLargeIcon.writeToParcel(parcel, 0);
Joe Onorato561d3852010-11-20 18:09:34 -08002216 } else {
2217 parcel.writeInt(0);
2218 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002219
2220 parcel.writeInt(defaults);
2221 parcel.writeInt(this.flags);
2222
2223 if (sound != null) {
2224 parcel.writeInt(1);
2225 sound.writeToParcel(parcel, 0);
2226 } else {
2227 parcel.writeInt(0);
2228 }
2229 parcel.writeInt(audioStreamType);
John Spurlockc0650f022014-07-19 13:22:39 -04002230
2231 if (audioAttributes != null) {
2232 parcel.writeInt(1);
2233 audioAttributes.writeToParcel(parcel, 0);
2234 } else {
2235 parcel.writeInt(0);
2236 }
2237
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002238 parcel.writeLongArray(vibrate);
2239 parcel.writeInt(ledARGB);
2240 parcel.writeInt(ledOnMS);
2241 parcel.writeInt(ledOffMS);
2242 parcel.writeInt(iconLevel);
Daniel Sandlere46cbd32010-06-17 10:35:26 -04002243
2244 if (fullScreenIntent != null) {
2245 parcel.writeInt(1);
2246 fullScreenIntent.writeToParcel(parcel, 0);
2247 } else {
2248 parcel.writeInt(0);
2249 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002250
2251 parcel.writeInt(priority);
Joe Malin8d40d042012-11-05 11:36:40 -08002252
John Spurlockfd7f1e02014-03-18 16:41:57 -04002253 parcel.writeString(category);
Joe Malin8d40d042012-11-05 11:36:40 -08002254
Griff Hazen5cadc3b2014-05-20 09:55:39 -07002255 parcel.writeString(mGroupKey);
2256
2257 parcel.writeString(mSortKey);
2258
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002259 parcel.writeBundle(extras); // null ok
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002260
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002261 parcel.writeTypedArray(actions, 0); // null ok
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002262
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002263 if (bigContentView != null) {
2264 parcel.writeInt(1);
2265 bigContentView.writeToParcel(parcel, 0);
2266 } else {
2267 parcel.writeInt(0);
2268 }
Dan Sandler0bf2ed82013-12-21 23:33:41 -06002269
Chris Wren8fd39ec2014-02-27 17:43:26 -05002270 if (headsUpContentView != null) {
2271 parcel.writeInt(1);
2272 headsUpContentView.writeToParcel(parcel, 0);
2273 } else {
2274 parcel.writeInt(0);
2275 }
2276
Dan Sandler0bf2ed82013-12-21 23:33:41 -06002277 parcel.writeInt(visibility);
2278
2279 if (publicVersion != null) {
2280 parcel.writeInt(1);
2281 publicVersion.writeToParcel(parcel, 0);
2282 } else {
2283 parcel.writeInt(0);
2284 }
Dan Sandler26e81cf2014-05-06 10:01:27 -04002285
2286 parcel.writeInt(color);
Julia Reynoldsb5e44b72016-08-16 15:00:25 -04002287
2288 if (mChannelId != null) {
2289 parcel.writeInt(1);
2290 parcel.writeString(mChannelId);
2291 } else {
2292 parcel.writeInt(0);
2293 }
Julia Reynolds2a128742016-11-28 14:29:25 -05002294 parcel.writeLong(mTimeout);
Julia Reynolds13d898c2017-02-02 12:22:05 -05002295
2296 if (mShortcutId != null) {
2297 parcel.writeInt(1);
2298 parcel.writeString(mShortcutId);
2299 } else {
2300 parcel.writeInt(0);
2301 }
2302
2303 parcel.writeInt(mBadgeIcon);
Julia Reynolds3aedded2017-03-31 14:42:09 -04002304
2305 if (mSettingsText != null) {
2306 parcel.writeInt(1);
2307 TextUtils.writeToParcel(mSettingsText, parcel, flags);
2308 } else {
2309 parcel.writeInt(0);
2310 }
Julia Reynoldsa79c3712017-04-21 10:29:57 -04002311
2312 parcel.writeInt(mGroupAlertBehavior);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002313 }
2314
2315 /**
2316 * Parcelable.Creator that instantiates Notification objects
2317 */
2318 public static final Parcelable.Creator<Notification> CREATOR
2319 = new Parcelable.Creator<Notification>()
2320 {
2321 public Notification createFromParcel(Parcel parcel)
2322 {
2323 return new Notification(parcel);
2324 }
2325
2326 public Notification[] newArray(int size)
2327 {
2328 return new Notification[size];
2329 }
2330 };
2331
2332 /**
2333 * Sets the {@link #contentView} field to be a view with the standard "Latest Event"
2334 * layout.
2335 *
2336 * <p>Uses the {@link #icon} and {@link #when} fields to set the icon and time fields
2337 * in the view.</p>
2338 * @param context The context for your application / activity.
2339 * @param contentTitle The title that goes in the expanded entry.
2340 * @param contentText The text that goes in the expanded entry.
2341 * @param contentIntent The intent to launch when the user clicks the expanded notification.
2342 * If this is an activity, it must include the
2343 * {@link android.content.Intent#FLAG_ACTIVITY_NEW_TASK} flag, which requires
Scott Main7aee61f2011-02-08 11:25:01 -08002344 * that you take care of task management as described in the
2345 * <a href="{@docRoot}guide/topics/fundamentals/tasks-and-back-stack.html">Tasks and Back
2346 * Stack</a> document.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002347 *
Joe Onorato46439ce2010-11-19 13:56:21 -08002348 * @deprecated Use {@link Builder} instead.
Chris Wrena05db382015-06-24 15:18:34 -04002349 * @removed
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002350 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002351 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002352 public void setLatestEventInfo(Context context,
2353 CharSequence contentTitle, CharSequence contentText, PendingIntent contentIntent) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002354 if (context.getApplicationInfo().targetSdkVersion > Build.VERSION_CODES.LOLLIPOP_MR1){
2355 Log.e(TAG, "setLatestEventInfo() is deprecated and you should feel deprecated.",
2356 new Throwable());
2357 }
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002358
Selim Cinek4ac6f602016-06-13 15:47:03 -07002359 if (context.getApplicationInfo().targetSdkVersion < Build.VERSION_CODES.N) {
2360 extras.putBoolean(EXTRA_SHOW_WHEN, true);
2361 }
2362
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002363 // ensure that any information already set directly is preserved
2364 final Notification.Builder builder = new Notification.Builder(context, this);
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002365
2366 // now apply the latestEventInfo fields
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002367 if (contentTitle != null) {
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002368 builder.setContentTitle(contentTitle);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002369 }
2370 if (contentText != null) {
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002371 builder.setContentText(contentText);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002372 }
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002373 builder.setContentIntent(contentIntent);
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002374
2375 builder.build(); // callers expect this notification to be ready to use
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002376 }
2377
Julia Reynoldsda303542015-11-23 14:00:20 -05002378 /**
2379 * @hide
2380 */
2381 public static void addFieldsFromContext(Context context, Notification notification) {
Julia Reynoldse071abd2017-03-22 10:52:11 -04002382 addFieldsFromContext(context.getApplicationInfo(), notification);
Jeff Sharkey012bc7b2016-04-11 16:30:27 -06002383 }
2384
2385 /**
2386 * @hide
2387 */
Julia Reynoldse071abd2017-03-22 10:52:11 -04002388 public static void addFieldsFromContext(ApplicationInfo ai, Notification notification) {
Jeff Sharkey012bc7b2016-04-11 16:30:27 -06002389 notification.extras.putParcelable(EXTRA_BUILDER_APPLICATION_INFO, ai);
Julia Reynoldsda303542015-11-23 14:00:20 -05002390 }
2391
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002392 @Override
2393 public String toString() {
2394 StringBuilder sb = new StringBuilder();
Julia Reynoldsb9e712e2017-04-17 10:31:03 -04002395 sb.append("Notification(channel=");
Julia Reynoldsbad42972017-04-25 13:52:49 -04002396 sb.append(getChannelId());
Julia Reynoldsb9e712e2017-04-17 10:31:03 -04002397 sb.append(" pri=");
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002398 sb.append(priority);
2399 sb.append(" contentView=");
Joe Onoratoc9596d62011-01-12 17:03:11 -08002400 if (contentView != null) {
2401 sb.append(contentView.getPackage());
2402 sb.append("/0x");
2403 sb.append(Integer.toHexString(contentView.getLayoutId()));
2404 } else {
2405 sb.append("null");
2406 }
2407 sb.append(" vibrate=");
Daniel Sandler6738eee2012-11-16 12:03:32 -05002408 if ((this.defaults & DEFAULT_VIBRATE) != 0) {
2409 sb.append("default");
2410 } else if (this.vibrate != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002411 int N = this.vibrate.length-1;
2412 sb.append("[");
2413 for (int i=0; i<N; i++) {
2414 sb.append(this.vibrate[i]);
2415 sb.append(',');
2416 }
Simon Schoar8cf97d92009-06-10 22:08:37 +02002417 if (N != -1) {
2418 sb.append(this.vibrate[N]);
2419 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002420 sb.append("]");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002421 } else {
2422 sb.append("null");
2423 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002424 sb.append(" sound=");
Daniel Sandler6738eee2012-11-16 12:03:32 -05002425 if ((this.defaults & DEFAULT_SOUND) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002426 sb.append("default");
Daniel Sandler6738eee2012-11-16 12:03:32 -05002427 } else if (this.sound != null) {
2428 sb.append(this.sound.toString());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002429 } else {
2430 sb.append("null");
2431 }
Chris Wren365b6d32015-07-16 10:39:26 -04002432 if (this.tickerText != null) {
2433 sb.append(" tick");
2434 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002435 sb.append(" defaults=0x");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002436 sb.append(Integer.toHexString(this.defaults));
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002437 sb.append(" flags=0x");
Daniel Sandlere46cbd32010-06-17 10:35:26 -04002438 sb.append(Integer.toHexString(this.flags));
Dan Sandler26e81cf2014-05-06 10:01:27 -04002439 sb.append(String.format(" color=0x%08x", this.color));
Griff Hazen5cadc3b2014-05-20 09:55:39 -07002440 if (this.category != null) {
2441 sb.append(" category=");
2442 sb.append(this.category);
2443 }
2444 if (this.mGroupKey != null) {
2445 sb.append(" groupKey=");
2446 sb.append(this.mGroupKey);
2447 }
2448 if (this.mSortKey != null) {
2449 sb.append(" sortKey=");
2450 sb.append(this.mSortKey);
2451 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002452 if (actions != null) {
Dan Sandler1b718782014-07-18 12:43:45 -04002453 sb.append(" actions=");
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002454 sb.append(actions.length);
Dan Sandler1b718782014-07-18 12:43:45 -04002455 }
2456 sb.append(" vis=");
2457 sb.append(visibilityToString(this.visibility));
2458 if (this.publicVersion != null) {
2459 sb.append(" publicVersion=");
2460 sb.append(publicVersion.toString());
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002461 }
2462 sb.append(")");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002463 return sb.toString();
2464 }
Joe Onorato46439ce2010-11-19 13:56:21 -08002465
Dan Sandler1b718782014-07-18 12:43:45 -04002466 /**
2467 * {@hide}
2468 */
2469 public static String visibilityToString(int vis) {
2470 switch (vis) {
2471 case VISIBILITY_PRIVATE:
2472 return "PRIVATE";
2473 case VISIBILITY_PUBLIC:
2474 return "PUBLIC";
2475 case VISIBILITY_SECRET:
2476 return "SECRET";
2477 default:
2478 return "UNKNOWN(" + String.valueOf(vis) + ")";
2479 }
2480 }
2481
Joe Onoratocb109a02011-01-18 17:57:41 -08002482 /**
John Spurlock1d881a12015-03-18 19:21:54 -04002483 * {@hide}
2484 */
2485 public static String priorityToString(@Priority int pri) {
2486 switch (pri) {
2487 case PRIORITY_MIN:
2488 return "MIN";
2489 case PRIORITY_LOW:
2490 return "LOW";
2491 case PRIORITY_DEFAULT:
2492 return "DEFAULT";
2493 case PRIORITY_HIGH:
2494 return "HIGH";
2495 case PRIORITY_MAX:
2496 return "MAX";
2497 default:
2498 return "UNKNOWN(" + String.valueOf(pri) + ")";
2499 }
2500 }
2501
2502 /**
Julia Reynoldsb5e44b72016-08-16 15:00:25 -04002503 * Returns the id of the channel this notification posts to.
2504 */
Julia Reynolds37856052016-11-11 09:20:07 -05002505 public String getChannel() {
Julia Reynoldsb5e44b72016-08-16 15:00:25 -04002506 return mChannelId;
2507 }
2508
2509 /**
Julia Reynoldsbad42972017-04-25 13:52:49 -04002510 * Returns the id of the channel this notification posts to.
2511 */
2512 public String getChannelId() {
2513 return mChannelId;
2514 }
2515
2516 /**
Julia Reynolds13d898c2017-02-02 12:22:05 -05002517 * Returns the time at which this notification should be canceled by the system, if it's not
2518 * canceled already.
Julia Reynolds2a128742016-11-28 14:29:25 -05002519 */
2520 public long getTimeout() {
2521 return mTimeout;
2522 }
2523
2524 /**
Julia Reynoldsbad42972017-04-25 13:52:49 -04002525 * Returns the duration from posting after which this notification should be canceled by the
2526 * system, if it's not canceled already.
2527 */
2528 public long getTimeoutAfter() {
2529 return mTimeout;
2530 }
2531
2532 /**
Julia Reynoldse071abd2017-03-22 10:52:11 -04002533 * Returns what icon should be shown for this notification if it is being displayed in a
2534 * Launcher that supports badging. Will be one of {@link #BADGE_ICON_NONE},
2535 * {@link #BADGE_ICON_SMALL}, or {@link #BADGE_ICON_LARGE}.
2536 */
2537 public int getBadgeIconType() {
2538 return mBadgeIcon;
2539 }
2540
2541 /**
Julia Reynolds13d898c2017-02-02 12:22:05 -05002542 * Returns the {@link ShortcutInfo#getId() id} that this notification supersedes, if any.
Julia Reynoldsbad42972017-04-25 13:52:49 -04002543 *
2544 * <p>Used by some Launchers that display notification content to hide shortcuts that duplicate
2545 * notifications.
Julia Reynolds13d898c2017-02-02 12:22:05 -05002546 */
2547 public String getShortcutId() {
2548 return mShortcutId;
2549 }
2550
Julia Reynolds3aedded2017-03-31 14:42:09 -04002551
2552 /**
2553 * Returns the settings text provided to {@link Builder#setSettingsText(CharSequence)}.
2554 */
2555 public CharSequence getSettingsText() {
2556 return mSettingsText;
2557 }
2558
Julia Reynolds13d898c2017-02-02 12:22:05 -05002559 /**
Julia Reynoldsa79c3712017-04-21 10:29:57 -04002560 * Returns which type of notifications in a group are responsible for audibly alerting the
2561 * user. See {@link #GROUP_ALERT_ALL}, {@link #GROUP_ALERT_CHILDREN},
2562 * {@link #GROUP_ALERT_SUMMARY}.
2563 */
2564 public @GroupAlertBehavior int getGroupAlertBehavior() {
2565 return mGroupAlertBehavior;
2566 }
2567
2568 /**
Dan Sandlerd63f9322015-05-06 15:18:49 -04002569 * The small icon representing this notification in the status bar and content view.
2570 *
2571 * @return the small icon representing this notification.
2572 *
2573 * @see Builder#getSmallIcon()
2574 * @see Builder#setSmallIcon(Icon)
2575 */
2576 public Icon getSmallIcon() {
2577 return mSmallIcon;
2578 }
2579
2580 /**
2581 * Used when notifying to clean up legacy small icons.
2582 * @hide
2583 */
2584 public void setSmallIcon(Icon icon) {
2585 mSmallIcon = icon;
2586 }
2587
2588 /**
2589 * The large icon shown in this notification's content view.
2590 * @see Builder#getLargeIcon()
2591 * @see Builder#setLargeIcon(Icon)
2592 */
2593 public Icon getLargeIcon() {
2594 return mLargeIcon;
2595 }
2596
2597 /**
Christoph Studer4600f9b2014-07-22 22:44:43 +02002598 * @hide
2599 */
Christoph Studerc8db24b2014-07-25 17:50:30 +02002600 public boolean isGroupSummary() {
2601 return mGroupKey != null && (flags & FLAG_GROUP_SUMMARY) != 0;
2602 }
2603
2604 /**
2605 * @hide
2606 */
2607 public boolean isGroupChild() {
2608 return mGroupKey != null && (flags & FLAG_GROUP_SUMMARY) == 0;
2609 }
2610
2611 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002612 * Builder class for {@link Notification} objects.
Joe Malin8d40d042012-11-05 11:36:40 -08002613 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002614 * Provides a convenient way to set the various fields of a {@link Notification} and generate
Scott Main183bf112012-08-13 19:12:13 -07002615 * content views using the platform's notification layout template. If your app supports
2616 * versions of Android as old as API level 4, you can instead use
2617 * {@link android.support.v4.app.NotificationCompat.Builder NotificationCompat.Builder},
2618 * available in the <a href="{@docRoot}tools/extras/support-library.html">Android Support
2619 * library</a>.
Joe Malin8d40d042012-11-05 11:36:40 -08002620 *
Scott Main183bf112012-08-13 19:12:13 -07002621 * <p>Example:
Joe Malin8d40d042012-11-05 11:36:40 -08002622 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002623 * <pre class="prettyprint">
Scott Main183bf112012-08-13 19:12:13 -07002624 * Notification noti = new Notification.Builder(mContext)
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002625 * .setContentTitle(&quot;New mail from &quot; + sender.toString())
2626 * .setContentText(subject)
2627 * .setSmallIcon(R.drawable.new_mail)
2628 * .setLargeIcon(aBitmap)
Chris Wrenfbd96ba2012-05-01 12:03:58 -04002629 * .build();
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002630 * </pre>
Joe Onoratocb109a02011-01-18 17:57:41 -08002631 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002632 public static class Builder {
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05002633 /**
2634 * @hide
2635 */
2636 public static final String EXTRA_REBUILD_CONTENT_VIEW_ACTION_COUNT =
2637 "android.rebuild.contentViewActionCount";
2638 /**
2639 * @hide
2640 */
2641 public static final String EXTRA_REBUILD_BIG_CONTENT_VIEW_ACTION_COUNT
2642 = "android.rebuild.bigViewActionCount";
2643 /**
2644 * @hide
2645 */
2646 public static final String EXTRA_REBUILD_HEADS_UP_CONTENT_VIEW_ACTION_COUNT
2647 = "android.rebuild.hudViewActionCount";
2648
Daniel Sandler602ad1c2012-06-12 16:06:27 -04002649 private static final int MAX_ACTION_BUTTONS = 3;
Daniel Sandler8680bf82012-05-15 16:52:52 -04002650
Selim Cinek6743c0b2017-01-18 18:24:01 -08002651 private static final boolean USE_ONLY_TITLE_IN_LOW_PRIORITY_SUMMARY =
2652 SystemProperties.getBoolean("notifications.only_title", true);
2653
Joe Onorato46439ce2010-11-19 13:56:21 -08002654 private Context mContext;
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002655 private Notification mN;
2656 private Bundle mUserExtras = new Bundle();
Chris Wrenfbd96ba2012-05-01 12:03:58 -04002657 private Style mStyle;
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002658 private ArrayList<Action> mActions = new ArrayList<Action>(MAX_ACTION_BUTTONS);
2659 private ArrayList<String> mPersonList = new ArrayList<String>();
2660 private NotificationColorUtil mColorUtil;
Selim Cinek7b9605b2017-01-19 17:36:00 -08002661 private boolean mIsLegacy;
2662 private boolean mIsLegacyInitialized;
Christoph Studer7ac80e62014-08-04 16:01:57 +02002663
2664 /**
Adrian Roos4ff3b122016-02-01 12:26:13 -08002665 * Caches a contrast-enhanced version of {@link #mCachedContrastColorIsFor}.
2666 */
2667 private int mCachedContrastColor = COLOR_INVALID;
2668 private int mCachedContrastColorIsFor = COLOR_INVALID;
Adrian Roos487374f2017-01-11 15:48:14 -08002669 /**
2670 * Caches a ambient version of {@link #mCachedContrastColorIsFor}.
2671 */
2672 private int mCachedAmbientColor = COLOR_INVALID;
2673 private int mCachedAmbientColorIsFor = COLOR_INVALID;
Adrian Roos4ff3b122016-02-01 12:26:13 -08002674
2675 /**
Adrian Roos70d7aa32017-01-11 15:39:06 -08002676 * Caches an instance of StandardTemplateParams. Note that this may have been used before,
2677 * so make sure to call {@link StandardTemplateParams#reset()} before using it.
2678 */
2679 StandardTemplateParams mParams = new StandardTemplateParams();
Selim Cinek7b9605b2017-01-19 17:36:00 -08002680 private int mTextColorsAreForBackground = COLOR_INVALID;
2681 private int mPrimaryTextColor = COLOR_INVALID;
2682 private int mSecondaryTextColor = COLOR_INVALID;
2683 private int mActionBarColor = COLOR_INVALID;
Adrian Roos70d7aa32017-01-11 15:39:06 -08002684
2685 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002686 * Constructs a new Builder with the defaults:
Joe Onoratocb109a02011-01-18 17:57:41 -08002687 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002688
2689 * <table>
2690 * <tr><th align=right>priority</th>
2691 * <td>{@link #PRIORITY_DEFAULT}</td></tr>
2692 * <tr><th align=right>when</th>
2693 * <td>now ({@link System#currentTimeMillis()})</td></tr>
2694 * <tr><th align=right>audio stream</th>
2695 * <td>{@link #STREAM_DEFAULT}</td></tr>
2696 * </table>
Joe Onoratocb109a02011-01-18 17:57:41 -08002697 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002698
2699 * @param context
2700 * A {@link Context} that will be used by the Builder to construct the
2701 * RemoteViews. The Context will not be held past the lifetime of this Builder
2702 * object.
Geoffrey Pitsch5caa2762017-01-12 09:35:54 -05002703 * @param channelId
2704 * The constructed Notification will be posted on this
2705 * {@link NotificationChannel}. To use a NotificationChannel, it must first be
2706 * created using {@link NotificationManager#createNotificationChannel}.
Joe Onoratocb109a02011-01-18 17:57:41 -08002707 */
Geoffrey Pitsch5caa2762017-01-12 09:35:54 -05002708 public Builder(Context context, String channelId) {
2709 this(context, (Notification) null);
2710 mN.mChannelId = channelId;
2711 }
2712
2713 /**
2714 * @deprecated use {@link Notification.Builder#Notification.Builder(Context, String)}
2715 * instead. All posted Notifications must specify a NotificationChannel Id.
2716 */
2717 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -08002718 public Builder(Context context) {
Geoffrey Pitsch5caa2762017-01-12 09:35:54 -05002719 this(context, (Notification) null);
Joe Onorato46439ce2010-11-19 13:56:21 -08002720 }
2721
Joe Onoratocb109a02011-01-18 17:57:41 -08002722 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002723 * @hide
Christoph Studer4600f9b2014-07-22 22:44:43 +02002724 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002725 public Builder(Context context, Notification toAdopt) {
2726 mContext = context;
Christoph Studer4600f9b2014-07-22 22:44:43 +02002727
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002728 if (toAdopt == null) {
2729 mN = new Notification();
Selim Cinek0ff1ce602016-04-05 18:27:16 -07002730 if (context.getApplicationInfo().targetSdkVersion < Build.VERSION_CODES.N) {
2731 mN.extras.putBoolean(EXTRA_SHOW_WHEN, true);
2732 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002733 mN.priority = PRIORITY_DEFAULT;
2734 mN.visibility = VISIBILITY_PRIVATE;
2735 } else {
2736 mN = toAdopt;
2737 if (mN.actions != null) {
2738 Collections.addAll(mActions, mN.actions);
Christoph Studer4600f9b2014-07-22 22:44:43 +02002739 }
2740
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002741 if (mN.extras.containsKey(EXTRA_PEOPLE)) {
2742 Collections.addAll(mPersonList, mN.extras.getStringArray(EXTRA_PEOPLE));
2743 }
2744
Selim Cinek4ac6f602016-06-13 15:47:03 -07002745 if (mN.getSmallIcon() == null && mN.icon != 0) {
2746 setSmallIcon(mN.icon);
2747 }
2748
2749 if (mN.getLargeIcon() == null && mN.largeIcon != null) {
2750 setLargeIcon(mN.largeIcon);
2751 }
2752
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002753 String templateClass = mN.extras.getString(EXTRA_TEMPLATE);
2754 if (!TextUtils.isEmpty(templateClass)) {
2755 final Class<? extends Style> styleClass
2756 = getNotificationStyleClass(templateClass);
2757 if (styleClass == null) {
2758 Log.d(TAG, "Unknown style class: " + templateClass);
2759 } else {
2760 try {
Adrian Roosc1a80b02016-04-05 14:54:55 -07002761 final Constructor<? extends Style> ctor =
2762 styleClass.getDeclaredConstructor();
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002763 ctor.setAccessible(true);
2764 final Style style = ctor.newInstance();
2765 style.restoreFromExtras(mN.extras);
2766
2767 if (style != null) {
2768 setStyle(style);
2769 }
2770 } catch (Throwable t) {
2771 Log.e(TAG, "Could not create Style", t);
2772 }
2773 }
2774 }
2775
2776 }
2777 }
2778
2779 private NotificationColorUtil getColorUtil() {
Selim Cinek99104832017-01-25 14:47:33 -08002780 if (mColorUtil == null) {
2781 mColorUtil = NotificationColorUtil.getInstance(mContext);
Christoph Studer4600f9b2014-07-22 22:44:43 +02002782 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002783 return mColorUtil;
Christoph Studer4600f9b2014-07-22 22:44:43 +02002784 }
2785
2786 /**
Julia Reynolds13d898c2017-02-02 12:22:05 -05002787 * If this notification is duplicative of a Launcher shortcut, sets the
2788 * {@link ShortcutInfo#getId() id} of the shortcut, in case the Launcher wants to hide
2789 * the shortcut.
2790 *
Julia Reynoldsbad42972017-04-25 13:52:49 -04002791 * This field will be ignored by Launchers that don't support badging, don't show
2792 * notification content, or don't show {@link android.content.pm.ShortcutManager shortcuts}.
Julia Reynolds13d898c2017-02-02 12:22:05 -05002793 *
2794 * @param shortcutId the {@link ShortcutInfo#getId() id} of the shortcut this notification
2795 * supersedes
2796 */
2797 public Builder setShortcutId(String shortcutId) {
2798 mN.mShortcutId = shortcutId;
2799 return this;
2800 }
2801
2802 /**
Julia Reynoldse071abd2017-03-22 10:52:11 -04002803 * Sets which icon to display as a badge for this notification.
2804 *
2805 * Must be one of {@link #BADGE_ICON_NONE}, {@link #BADGE_ICON_SMALL},
2806 * {@link #BADGE_ICON_LARGE}.
2807 *
2808 * Note: This value might be ignored, for launchers that don't support badge icons.
2809 */
Julia Reynolds612beb22017-03-30 10:48:30 -04002810 public Builder setBadgeIconType(int icon) {
Julia Reynoldse071abd2017-03-22 10:52:11 -04002811 mN.mBadgeIcon = icon;
2812 return this;
2813 }
2814
2815 /**
Julia Reynoldsa79c3712017-04-21 10:29:57 -04002816 * Sets the group alert behavior for this notification. Use this method to mute this
2817 * notification if alerts for this notification's group should be handled by a different
2818 * notification. This is only applicable for notifications that belong to a
2819 * {@link #setGroup(String) group}.
2820 *
2821 * <p> The default value is {@link #GROUP_ALERT_ALL}.</p>
2822 */
2823 public Builder setGroupAlertBehavior(@GroupAlertBehavior int groupAlertBehavior) {
2824 mN.mGroupAlertBehavior = groupAlertBehavior;
2825 return this;
2826 }
2827
2828 /**
Julia Reynoldsb5e44b72016-08-16 15:00:25 -04002829 * Specifies the channel the notification should be delivered on.
2830 */
2831 public Builder setChannel(String channelId) {
2832 mN.mChannelId = channelId;
2833 return this;
2834 }
2835
2836 /**
Julia Reynoldsbad42972017-04-25 13:52:49 -04002837 * Specifies the channel the notification should be delivered on.
2838 */
2839 public Builder setChannelId(String channelId) {
2840 mN.mChannelId = channelId;
2841 return this;
2842 }
2843
2844 /**
Julia Reynolds50989772017-02-23 14:32:16 -05002845 * Specifies a duration in milliseconds after which this notification should be canceled,
2846 * if it is not already canceled.
Julia Reynolds2a128742016-11-28 14:29:25 -05002847 */
Julia Reynolds50989772017-02-23 14:32:16 -05002848 public Builder setTimeout(long durationMs) {
2849 mN.mTimeout = durationMs;
Julia Reynolds2a128742016-11-28 14:29:25 -05002850 return this;
2851 }
2852
2853 /**
Julia Reynoldsbad42972017-04-25 13:52:49 -04002854 * Specifies a duration in milliseconds after which this notification should be canceled,
2855 * if it is not already canceled.
2856 */
2857 public Builder setTimeoutAfter(long durationMs) {
2858 mN.mTimeout = durationMs;
2859 return this;
2860 }
2861
2862 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002863 * Add a timestamp pertaining to the notification (usually the time the event occurred).
Selim Cinek0ff1ce602016-04-05 18:27:16 -07002864 *
2865 * For apps targeting {@link android.os.Build.VERSION_CODES#N} and above, this time is not
2866 * shown anymore by default and must be opted into by using
2867 * {@link android.app.Notification.Builder#setShowWhen(boolean)}
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002868 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002869 * @see Notification#when
Joe Onoratocb109a02011-01-18 17:57:41 -08002870 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002871 public Builder setWhen(long when) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002872 mN.when = when;
Joe Onorato46439ce2010-11-19 13:56:21 -08002873 return this;
2874 }
2875
Joe Onoratocb109a02011-01-18 17:57:41 -08002876 /**
Griff Hazen50c11652014-05-16 09:46:31 -07002877 * Control whether the timestamp set with {@link #setWhen(long) setWhen} is shown
Daniel Sandler0c890492012-09-12 17:23:10 -07002878 * in the content view.
Selim Cinek0ff1ce602016-04-05 18:27:16 -07002879 * For apps targeting {@link android.os.Build.VERSION_CODES#N} and above, this defaults to
2880 * {@code false}. For earlier apps, the default is {@code true}.
Daniel Sandler0c890492012-09-12 17:23:10 -07002881 */
2882 public Builder setShowWhen(boolean show) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002883 mN.extras.putBoolean(EXTRA_SHOW_WHEN, show);
Daniel Sandler0c890492012-09-12 17:23:10 -07002884 return this;
2885 }
2886
2887 /**
Daniel Sandlerd33b8032012-05-10 11:41:48 -04002888 * Show the {@link Notification#when} field as a stopwatch.
Joe Malin8d40d042012-11-05 11:36:40 -08002889 *
2890 * Instead of presenting <code>when</code> as a timestamp, the notification will show an
Daniel Sandlerd33b8032012-05-10 11:41:48 -04002891 * automatically updating display of the minutes and seconds since <code>when</code>.
Daniel Sandlera2985ed2012-04-03 16:42:00 -04002892 *
Daniel Sandlerd33b8032012-05-10 11:41:48 -04002893 * Useful when showing an elapsed time (like an ongoing phone call).
2894 *
Selim Cinek81c23aa2016-02-25 16:23:13 -08002895 * The counter can also be set to count down to <code>when</code> when using
Adrian Roos96b7e202016-05-17 13:50:38 -07002896 * {@link #setChronometerCountDown(boolean)}.
Selim Cinek81c23aa2016-02-25 16:23:13 -08002897 *
Daniel Sandlerd33b8032012-05-10 11:41:48 -04002898 * @see android.widget.Chronometer
Daniel Sandlera2985ed2012-04-03 16:42:00 -04002899 * @see Notification#when
Adrian Roos96b7e202016-05-17 13:50:38 -07002900 * @see #setChronometerCountDown(boolean)
Daniel Sandlera2985ed2012-04-03 16:42:00 -04002901 */
2902 public Builder setUsesChronometer(boolean b) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002903 mN.extras.putBoolean(EXTRA_SHOW_CHRONOMETER, b);
Daniel Sandlera2985ed2012-04-03 16:42:00 -04002904 return this;
2905 }
2906
2907 /**
Selim Cinek81c23aa2016-02-25 16:23:13 -08002908 * Sets the Chronometer to count down instead of counting up.
2909 *
2910 * <p>This is only relevant if {@link #setUsesChronometer(boolean)} has been set to true.
2911 * If it isn't set the chronometer will count up.
2912 *
2913 * @see #setUsesChronometer(boolean)
2914 */
Adrian Roos96b7e202016-05-17 13:50:38 -07002915 public Builder setChronometerCountDown(boolean countDown) {
2916 mN.extras.putBoolean(EXTRA_CHRONOMETER_COUNT_DOWN, countDown);
Selim Cinek81c23aa2016-02-25 16:23:13 -08002917 return this;
2918 }
2919
2920 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002921 * Set the small icon resource, which will be used to represent the notification in the
2922 * status bar.
Joe Onoratocb109a02011-01-18 17:57:41 -08002923 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002924
2925 * The platform template for the expanded view will draw this icon in the left, unless a
2926 * {@link #setLargeIcon(Bitmap) large icon} has also been specified, in which case the small
2927 * icon will be moved to the right-hand side.
2928 *
2929
2930 * @param icon
2931 * A resource ID in the application's package of the drawable to use.
2932 * @see Notification#icon
Joe Onoratocb109a02011-01-18 17:57:41 -08002933 */
Tor Norbye7b9c9122013-05-30 16:48:33 -07002934 public Builder setSmallIcon(@DrawableRes int icon) {
Dan Sandlerd63f9322015-05-06 15:18:49 -04002935 return setSmallIcon(icon != 0
2936 ? Icon.createWithResource(mContext, icon)
2937 : null);
Joe Onorato46439ce2010-11-19 13:56:21 -08002938 }
2939
Joe Onoratocb109a02011-01-18 17:57:41 -08002940 /**
2941 * A variant of {@link #setSmallIcon(int) setSmallIcon(int)} that takes an additional
2942 * level parameter for when the icon is a {@link android.graphics.drawable.LevelListDrawable
2943 * LevelListDrawable}.
2944 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002945 * @param icon A resource ID in the application's package of the drawable to use.
Joe Onoratocb109a02011-01-18 17:57:41 -08002946 * @param level The level to use for the icon.
2947 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002948 * @see Notification#icon
2949 * @see Notification#iconLevel
Joe Onoratocb109a02011-01-18 17:57:41 -08002950 */
Tor Norbye7b9c9122013-05-30 16:48:33 -07002951 public Builder setSmallIcon(@DrawableRes int icon, int level) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002952 mN.iconLevel = level;
Dan Sandlerd63f9322015-05-06 15:18:49 -04002953 return setSmallIcon(icon);
2954 }
2955
2956 /**
2957 * Set the small icon, which will be used to represent the notification in the
2958 * status bar and content view (unless overriden there by a
2959 * {@link #setLargeIcon(Bitmap) large icon}).
2960 *
2961 * @param icon An Icon object to use.
2962 * @see Notification#icon
2963 */
2964 public Builder setSmallIcon(Icon icon) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002965 mN.setSmallIcon(icon);
2966 if (icon != null && icon.getType() == Icon.TYPE_RESOURCE) {
2967 mN.icon = icon.getResId();
2968 }
Joe Onorato46439ce2010-11-19 13:56:21 -08002969 return this;
2970 }
2971
Joe Onoratocb109a02011-01-18 17:57:41 -08002972 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002973 * Set the first line of text in the platform notification template.
Joe Onoratocb109a02011-01-18 17:57:41 -08002974 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002975 public Builder setContentTitle(CharSequence title) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002976 mN.extras.putCharSequence(EXTRA_TITLE, safeCharSequence(title));
Joe Onorato46439ce2010-11-19 13:56:21 -08002977 return this;
2978 }
2979
Joe Onoratocb109a02011-01-18 17:57:41 -08002980 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002981 * Set the second line of text in the platform notification template.
Joe Onoratocb109a02011-01-18 17:57:41 -08002982 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002983 public Builder setContentText(CharSequence text) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002984 mN.extras.putCharSequence(EXTRA_TEXT, safeCharSequence(text));
Joe Onorato46439ce2010-11-19 13:56:21 -08002985 return this;
2986 }
2987
Joe Onoratocb109a02011-01-18 17:57:41 -08002988 /**
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07002989 * This provides some additional information that is displayed in the notification. No
2990 * guarantees are given where exactly it is displayed.
2991 *
2992 * <p>This information should only be provided if it provides an essential
2993 * benefit to the understanding of the notification. The more text you provide the
2994 * less readable it becomes. For example, an email client should only provide the account
2995 * name here if more than one email account has been added.</p>
2996 *
2997 * <p>As of {@link android.os.Build.VERSION_CODES#N} this information is displayed in the
2998 * notification header area.
2999 *
3000 * On Android versions before {@link android.os.Build.VERSION_CODES#N}
3001 * this will be shown in the third line of text in the platform notification template.
3002 * You should not be using {@link #setProgress(int, int, boolean)} at the
3003 * same time on those versions; they occupy the same place.
3004 * </p>
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003005 */
3006 public Builder setSubText(CharSequence text) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003007 mN.extras.putCharSequence(EXTRA_SUB_TEXT, safeCharSequence(text));
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003008 return this;
3009 }
3010
3011 /**
Julia Reynolds3aedded2017-03-31 14:42:09 -04003012 * Provides text that will appear as a link to your application's settings.
3013 *
3014 * <p>This text does not appear within notification {@link Style templates} but may
3015 * appear when the user uses an affordance to learn more about the notification.
3016 * Additionally, this text will not appear unless you provide a valid link target by
3017 * handling {@link #INTENT_CATEGORY_NOTIFICATION_PREFERENCES}.
3018 *
3019 * <p>This text is meant to be concise description about what the user can customize
3020 * when they click on this link. The recommended maximum length is 40 characters.
3021 * @param text
3022 * @return
3023 */
3024 public Builder setSettingsText(CharSequence text) {
3025 mN.mSettingsText = safeCharSequence(text);
3026 return this;
3027 }
3028
3029 /**
Adrian Roose458aa82015-12-08 16:17:19 -08003030 * Set the remote input history.
3031 *
3032 * This should be set to the most recent inputs that have been sent
3033 * through a {@link RemoteInput} of this Notification and cleared once the it is no
3034 * longer relevant (e.g. for chat notifications once the other party has responded).
3035 *
3036 * The most recent input must be stored at the 0 index, the second most recent at the
3037 * 1 index, etc. Note that the system will limit both how far back the inputs will be shown
3038 * and how much of each individual input is shown.
3039 *
3040 * <p>Note: The reply text will only be shown on notifications that have least one action
3041 * with a {@code RemoteInput}.</p>
3042 */
3043 public Builder setRemoteInputHistory(CharSequence[] text) {
3044 if (text == null) {
3045 mN.extras.putCharSequenceArray(EXTRA_REMOTE_INPUT_HISTORY, null);
3046 } else {
3047 final int N = Math.min(MAX_REPLY_HISTORY, text.length);
3048 CharSequence[] safe = new CharSequence[N];
3049 for (int i = 0; i < N; i++) {
3050 safe[i] = safeCharSequence(text[i]);
3051 }
3052 mN.extras.putCharSequenceArray(EXTRA_REMOTE_INPUT_HISTORY, safe);
3053 }
3054 return this;
3055 }
3056
3057 /**
Julia Reynolds13d898c2017-02-02 12:22:05 -05003058 * Sets the number of items this notification represents. May be displayed as a badge count
3059 * for Launchers that support badging.
Joe Onoratocb109a02011-01-18 17:57:41 -08003060 */
Joe Onorato8595a3d2010-11-19 18:12:07 -08003061 public Builder setNumber(int number) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003062 mN.number = number;
Joe Onorato8595a3d2010-11-19 18:12:07 -08003063 return this;
3064 }
3065
Joe Onoratocb109a02011-01-18 17:57:41 -08003066 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003067 * A small piece of additional information pertaining to this notification.
3068 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003069 * The platform template will draw this on the last line of the notification, at the far
3070 * right (to the right of a smallIcon if it has been placed there).
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07003071 *
3072 * @deprecated use {@link #setSubText(CharSequence)} instead to set a text in the header.
3073 * For legacy apps targeting a version below {@link android.os.Build.VERSION_CODES#N} this
3074 * field will still show up, but the subtext will take precedence.
Joe Onoratocb109a02011-01-18 17:57:41 -08003075 */
Aurimas Liutikas514c5ef2016-05-24 15:22:55 -07003076 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -08003077 public Builder setContentInfo(CharSequence info) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003078 mN.extras.putCharSequence(EXTRA_INFO_TEXT, safeCharSequence(info));
Joe Onorato46439ce2010-11-19 13:56:21 -08003079 return this;
3080 }
3081
Joe Onoratocb109a02011-01-18 17:57:41 -08003082 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003083 * Set the progress this notification represents.
3084 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003085 * The platform template will represent this using a {@link ProgressBar}.
Jeff Sharkey1c400132011-08-05 14:50:13 -07003086 */
3087 public Builder setProgress(int max, int progress, boolean indeterminate) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003088 mN.extras.putInt(EXTRA_PROGRESS, progress);
3089 mN.extras.putInt(EXTRA_PROGRESS_MAX, max);
3090 mN.extras.putBoolean(EXTRA_PROGRESS_INDETERMINATE, indeterminate);
Jeff Sharkey1c400132011-08-05 14:50:13 -07003091 return this;
3092 }
3093
3094 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003095 * Supply a custom RemoteViews to use instead of the platform template.
3096 *
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003097 * Use {@link #setCustomContentView(RemoteViews)} instead.
Joe Onoratocb109a02011-01-18 17:57:41 -08003098 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003099 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -08003100 public Builder setContent(RemoteViews views) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003101 return setCustomContentView(views);
3102 }
3103
3104 /**
3105 * Supply custom RemoteViews to use instead of the platform template.
3106 *
3107 * This will override the layout that would otherwise be constructed by this Builder
3108 * object.
3109 */
3110 public Builder setCustomContentView(RemoteViews contentView) {
3111 mN.contentView = contentView;
3112 return this;
3113 }
3114
3115 /**
3116 * Supply custom RemoteViews to use instead of the platform template in the expanded form.
3117 *
3118 * This will override the expanded layout that would otherwise be constructed by this
3119 * Builder object.
3120 */
3121 public Builder setCustomBigContentView(RemoteViews contentView) {
3122 mN.bigContentView = contentView;
3123 return this;
3124 }
3125
3126 /**
3127 * Supply custom RemoteViews to use instead of the platform template in the heads up dialog.
3128 *
3129 * This will override the heads-up layout that would otherwise be constructed by this
3130 * Builder object.
3131 */
3132 public Builder setCustomHeadsUpContentView(RemoteViews contentView) {
3133 mN.headsUpContentView = contentView;
Joe Onorato46439ce2010-11-19 13:56:21 -08003134 return this;
3135 }
3136
Joe Onoratocb109a02011-01-18 17:57:41 -08003137 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003138 * Supply a {@link PendingIntent} to be sent when the notification is clicked.
3139 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003140 * As of {@link android.os.Build.VERSION_CODES#HONEYCOMB}, if this field is unset and you
3141 * have specified a custom RemoteViews with {@link #setContent(RemoteViews)}, you can use
3142 * {@link RemoteViews#setOnClickPendingIntent RemoteViews.setOnClickPendingIntent(int,PendingIntent)}
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003143 * to assign PendingIntents to individual views in that custom layout (i.e., to create
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003144 * clickable buttons inside the notification view).
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003145 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003146 * @see Notification#contentIntent Notification.contentIntent
Joe Onoratocb109a02011-01-18 17:57:41 -08003147 */
Joe Onorato46439ce2010-11-19 13:56:21 -08003148 public Builder setContentIntent(PendingIntent intent) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003149 mN.contentIntent = intent;
Joe Onorato46439ce2010-11-19 13:56:21 -08003150 return this;
3151 }
3152
Joe Onoratocb109a02011-01-18 17:57:41 -08003153 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003154 * Supply a {@link PendingIntent} to send when the notification is cleared explicitly by the user.
3155 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003156 * @see Notification#deleteIntent
Joe Onoratocb109a02011-01-18 17:57:41 -08003157 */
Joe Onorato46439ce2010-11-19 13:56:21 -08003158 public Builder setDeleteIntent(PendingIntent intent) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003159 mN.deleteIntent = intent;
Joe Onorato46439ce2010-11-19 13:56:21 -08003160 return this;
3161 }
3162
Joe Onoratocb109a02011-01-18 17:57:41 -08003163 /**
3164 * An intent to launch instead of posting the notification to the status bar.
3165 * Only for use with extremely high-priority notifications demanding the user's
3166 * <strong>immediate</strong> attention, such as an incoming phone call or
3167 * alarm clock that the user has explicitly set to a particular time.
3168 * If this facility is used for something else, please give the user an option
3169 * to turn it off and use a normal notification, as this can be extremely
3170 * disruptive.
3171 *
Chris Wren47c20a12014-06-18 17:27:29 -04003172 * <p>
3173 * The system UI may choose to display a heads-up notification, instead of
3174 * launching this intent, while the user is using the device.
3175 * </p>
3176 *
Joe Onoratocb109a02011-01-18 17:57:41 -08003177 * @param intent The pending intent to launch.
3178 * @param highPriority Passing true will cause this notification to be sent
3179 * even if other notifications are suppressed.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003180 *
3181 * @see Notification#fullScreenIntent
Joe Onoratocb109a02011-01-18 17:57:41 -08003182 */
Joe Onorato46439ce2010-11-19 13:56:21 -08003183 public Builder setFullScreenIntent(PendingIntent intent, boolean highPriority) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003184 mN.fullScreenIntent = intent;
Joe Onorato46439ce2010-11-19 13:56:21 -08003185 setFlag(FLAG_HIGH_PRIORITY, highPriority);
3186 return this;
3187 }
3188
Joe Onoratocb109a02011-01-18 17:57:41 -08003189 /**
Dan Sandler5fcdf6e2014-07-18 11:31:15 -04003190 * Set the "ticker" text which is sent to accessibility services.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003191 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003192 * @see Notification#tickerText
Joe Onoratocb109a02011-01-18 17:57:41 -08003193 */
Joe Onorato46439ce2010-11-19 13:56:21 -08003194 public Builder setTicker(CharSequence tickerText) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003195 mN.tickerText = safeCharSequence(tickerText);
Joe Onorato46439ce2010-11-19 13:56:21 -08003196 return this;
3197 }
3198
Joe Onoratocb109a02011-01-18 17:57:41 -08003199 /**
Dan Sandler5fcdf6e2014-07-18 11:31:15 -04003200 * Obsolete version of {@link #setTicker(CharSequence)}.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003201 *
Joe Onoratocb109a02011-01-18 17:57:41 -08003202 */
Dan Sandler5fcdf6e2014-07-18 11:31:15 -04003203 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -08003204 public Builder setTicker(CharSequence tickerText, RemoteViews views) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003205 setTicker(tickerText);
3206 // views is ignored
Joe Onorato46439ce2010-11-19 13:56:21 -08003207 return this;
3208 }
3209
Joe Onoratocb109a02011-01-18 17:57:41 -08003210 /**
Dan Sandlerd63f9322015-05-06 15:18:49 -04003211 * Add a large icon to the notification content view.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003212 *
3213 * In the platform template, this image will be shown on the left of the notification view
Dan Sandlerd63f9322015-05-06 15:18:49 -04003214 * in place of the {@link #setSmallIcon(Icon) small icon} (which will be placed in a small
3215 * badge atop the large icon).
Dan Sandler08a04c12015-05-06 15:18:49 -04003216 */
Dan Sandlerd63f9322015-05-06 15:18:49 -04003217 public Builder setLargeIcon(Bitmap b) {
3218 return setLargeIcon(b != null ? Icon.createWithBitmap(b) : null);
3219 }
3220
3221 /**
3222 * Add a large icon to the notification content view.
3223 *
3224 * In the platform template, this image will be shown on the left of the notification view
3225 * in place of the {@link #setSmallIcon(Icon) small icon} (which will be placed in a small
3226 * badge atop the large icon).
3227 */
3228 public Builder setLargeIcon(Icon icon) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003229 mN.mLargeIcon = icon;
3230 mN.extras.putParcelable(EXTRA_LARGE_ICON, icon);
Joe Onorato46439ce2010-11-19 13:56:21 -08003231 return this;
3232 }
3233
Joe Onoratocb109a02011-01-18 17:57:41 -08003234 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003235 * Set the sound to play.
3236 *
John Spurlockc0650f022014-07-19 13:22:39 -04003237 * It will be played using the {@link #AUDIO_ATTRIBUTES_DEFAULT default audio attributes}
3238 * for notifications.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003239 *
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05003240 * @deprecated use {@link NotificationChannel#setSound(Uri, AudioAttributes)} instead.
Joe Onoratocb109a02011-01-18 17:57:41 -08003241 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05003242 @Deprecated
Joe Onorato52f80cd2010-11-21 15:34:48 -08003243 public Builder setSound(Uri sound) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003244 mN.sound = sound;
3245 mN.audioAttributes = AUDIO_ATTRIBUTES_DEFAULT;
Joe Onorato52f80cd2010-11-21 15:34:48 -08003246 return this;
3247 }
3248
Joe Onoratocb109a02011-01-18 17:57:41 -08003249 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003250 * Set the sound to play, along with a specific stream on which to play it.
Joe Onoratocb109a02011-01-18 17:57:41 -08003251 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003252 * See {@link android.media.AudioManager} for the <code>STREAM_</code> constants.
3253 *
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05003254 * @deprecated use {@link NotificationChannel#setSound(Uri, AudioAttributes)}.
Joe Onoratocb109a02011-01-18 17:57:41 -08003255 */
Jean-Michel Trivi81f871e2014-08-06 16:32:38 -07003256 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -08003257 public Builder setSound(Uri sound, int streamType) {
Jean-Michel Trivi2f7511f2016-11-28 15:40:27 -08003258 PlayerBase.deprecateStreamTypeForPlayback(streamType, "Notification", "setSound()");
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003259 mN.sound = sound;
3260 mN.audioStreamType = streamType;
Joe Onorato46439ce2010-11-19 13:56:21 -08003261 return this;
3262 }
3263
Joe Onoratocb109a02011-01-18 17:57:41 -08003264 /**
John Spurlockc0650f022014-07-19 13:22:39 -04003265 * Set the sound to play, along with specific {@link AudioAttributes audio attributes} to
3266 * use during playback.
3267 *
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05003268 * @deprecated use {@link NotificationChannel#setSound(Uri, AudioAttributes)} instead.
John Spurlockc0650f022014-07-19 13:22:39 -04003269 * @see Notification#sound
3270 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05003271 @Deprecated
John Spurlockc0650f022014-07-19 13:22:39 -04003272 public Builder setSound(Uri sound, AudioAttributes audioAttributes) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003273 mN.sound = sound;
3274 mN.audioAttributes = audioAttributes;
John Spurlockc0650f022014-07-19 13:22:39 -04003275 return this;
3276 }
3277
3278 /**
Joe Onoratocb109a02011-01-18 17:57:41 -08003279 * Set the vibration pattern to use.
3280 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003281 * See {@link android.os.Vibrator#vibrate(long[], int)} for a discussion of the
3282 * <code>pattern</code> parameter.
3283 *
Chris Wren47c20a12014-06-18 17:27:29 -04003284 * <p>
3285 * A notification that vibrates is more likely to be presented as a heads-up notification.
3286 * </p>
3287 *
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05003288 * @deprecated use {@link NotificationChannel#setVibrationPattern(long[])} instead.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003289 * @see Notification#vibrate
Joe Onoratocb109a02011-01-18 17:57:41 -08003290 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05003291 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -08003292 public Builder setVibrate(long[] pattern) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003293 mN.vibrate = pattern;
Joe Onorato46439ce2010-11-19 13:56:21 -08003294 return this;
3295 }
3296
Joe Onoratocb109a02011-01-18 17:57:41 -08003297 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003298 * Set the desired color for the indicator LED on the device, as well as the
3299 * blink duty cycle (specified in milliseconds).
3300 *
3301
3302 * Not all devices will honor all (or even any) of these values.
3303 *
Julia Reynolds529e3322017-02-06 08:33:01 -05003304 * @deprecated use {@link NotificationChannel#enableLights(boolean)} instead.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003305 * @see Notification#ledARGB
3306 * @see Notification#ledOnMS
3307 * @see Notification#ledOffMS
Joe Onoratocb109a02011-01-18 17:57:41 -08003308 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05003309 @Deprecated
Tor Norbye80756e32015-03-02 09:39:27 -08003310 public Builder setLights(@ColorInt int argb, int onMs, int offMs) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003311 mN.ledARGB = argb;
3312 mN.ledOnMS = onMs;
3313 mN.ledOffMS = offMs;
Julia Reynolds10ee1fc2015-11-09 11:04:55 -05003314 if (onMs != 0 || offMs != 0) {
3315 mN.flags |= FLAG_SHOW_LIGHTS;
3316 }
Joe Onorato46439ce2010-11-19 13:56:21 -08003317 return this;
3318 }
3319
Joe Onoratocb109a02011-01-18 17:57:41 -08003320 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003321 * Set whether this is an "ongoing" notification.
Joe Onoratocb109a02011-01-18 17:57:41 -08003322 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003323
3324 * Ongoing notifications cannot be dismissed by the user, so your application or service
3325 * must take care of canceling them.
3326 *
3327
3328 * They are typically used to indicate a background task that the user is actively engaged
3329 * with (e.g., playing music) or is pending in some way and therefore occupying the device
3330 * (e.g., a file download, sync operation, active network connection).
3331 *
3332
3333 * @see Notification#FLAG_ONGOING_EVENT
3334 * @see Service#setForeground(boolean)
Joe Onoratocb109a02011-01-18 17:57:41 -08003335 */
Joe Onorato46439ce2010-11-19 13:56:21 -08003336 public Builder setOngoing(boolean ongoing) {
3337 setFlag(FLAG_ONGOING_EVENT, ongoing);
3338 return this;
3339 }
3340
Joe Onoratocb109a02011-01-18 17:57:41 -08003341 /**
Selim Cinek7b9605b2017-01-19 17:36:00 -08003342 * Set whether this notification should be colorized. When set, the color set with
3343 * {@link #setColor(int)} will be used as the background color of this notification.
3344 * <p>
Selim Cinek7b9605b2017-01-19 17:36:00 -08003345 * This should only be used for high priority ongoing tasks like navigation, an ongoing
3346 * call, or other similarly high-priority events for the user.
Selim Cinek99104832017-01-25 14:47:33 -08003347 * <p>
Selim Cinek22714f12017-04-13 16:23:53 -07003348 * For most styles, the coloring will only be applied if the notification is for a
3349 * foreground service notification.
Selim Cinek99104832017-01-25 14:47:33 -08003350 * However, for {@link MediaStyle} and {@link DecoratedMediaCustomViewStyle} notifications
Selim Cinek22714f12017-04-13 16:23:53 -07003351 * that have a media session attached there is no such requirement.
Selim Cinek7b9605b2017-01-19 17:36:00 -08003352 *
Selim Cinek7b9605b2017-01-19 17:36:00 -08003353 * @see Builder#setColor(int)
Selim Cinek99104832017-01-25 14:47:33 -08003354 * @see MediaStyle#setMediaSession(MediaSession.Token)
Selim Cinek7b9605b2017-01-19 17:36:00 -08003355 */
3356 public Builder setColorized(boolean colorize) {
3357 mN.extras.putBoolean(EXTRA_COLORIZED, colorize);
3358 return this;
3359 }
3360
3361 /**
Joe Onoratocb109a02011-01-18 17:57:41 -08003362 * Set this flag if you would only like the sound, vibrate
3363 * and ticker to be played if the notification is not already showing.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003364 *
3365 * @see Notification#FLAG_ONLY_ALERT_ONCE
Joe Onoratocb109a02011-01-18 17:57:41 -08003366 */
Joe Onorato46439ce2010-11-19 13:56:21 -08003367 public Builder setOnlyAlertOnce(boolean onlyAlertOnce) {
3368 setFlag(FLAG_ONLY_ALERT_ONCE, onlyAlertOnce);
3369 return this;
3370 }
3371
Joe Onoratocb109a02011-01-18 17:57:41 -08003372 /**
Julia Reynolds04499532016-09-13 14:04:53 -04003373 * Make this notification automatically dismissed when the user touches it.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003374 *
3375 * @see Notification#FLAG_AUTO_CANCEL
Joe Onoratocb109a02011-01-18 17:57:41 -08003376 */
Joe Onorato46439ce2010-11-19 13:56:21 -08003377 public Builder setAutoCancel(boolean autoCancel) {
Joe Onorato281d83f2011-01-04 17:13:10 -08003378 setFlag(FLAG_AUTO_CANCEL, autoCancel);
Joe Onorato46439ce2010-11-19 13:56:21 -08003379 return this;
3380 }
3381
Joe Onoratocb109a02011-01-18 17:57:41 -08003382 /**
Griff Hazendfcb0802014-02-11 12:00:00 -08003383 * Set whether or not this notification should not bridge to other devices.
3384 *
3385 * <p>Some notifications can be bridged to other devices for remote display.
3386 * This hint can be set to recommend this notification not be bridged.
3387 */
3388 public Builder setLocalOnly(boolean localOnly) {
3389 setFlag(FLAG_LOCAL_ONLY, localOnly);
3390 return this;
3391 }
3392
3393 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003394 * Set which notification properties will be inherited from system defaults.
Joe Onoratocb109a02011-01-18 17:57:41 -08003395 * <p>
3396 * The value should be one or more of the following fields combined with
3397 * bitwise-or:
3398 * {@link #DEFAULT_SOUND}, {@link #DEFAULT_VIBRATE}, {@link #DEFAULT_LIGHTS}.
3399 * <p>
3400 * For all default values, use {@link #DEFAULT_ALL}.
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05003401 *
3402 * @deprecated use {@link NotificationChannel#enableVibration(boolean)} and
Julia Reynolds529e3322017-02-06 08:33:01 -05003403 * {@link NotificationChannel#enableLights(boolean)} and
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05003404 * {@link NotificationChannel#setSound(Uri, AudioAttributes)} instead.
Joe Onoratocb109a02011-01-18 17:57:41 -08003405 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05003406 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -08003407 public Builder setDefaults(int defaults) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003408 mN.defaults = defaults;
Joe Onorato46439ce2010-11-19 13:56:21 -08003409 return this;
3410 }
3411
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003412 /**
3413 * Set the priority of this notification.
3414 *
3415 * @see Notification#priority
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05003416 * @deprecated use {@link NotificationChannel#setImportance(int)} instead.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003417 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05003418 @Deprecated
Tor Norbyed9273d62013-05-30 15:59:53 -07003419 public Builder setPriority(@Priority int pri) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003420 mN.priority = pri;
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003421 return this;
3422 }
Joe Malin8d40d042012-11-05 11:36:40 -08003423
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003424 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -04003425 * Set the notification category.
Joe Malin8d40d042012-11-05 11:36:40 -08003426 *
John Spurlockfd7f1e02014-03-18 16:41:57 -04003427 * @see Notification#category
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003428 */
John Spurlockfd7f1e02014-03-18 16:41:57 -04003429 public Builder setCategory(String category) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003430 mN.category = category;
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003431 return this;
3432 }
3433
3434 /**
Chris Wrendde75302014-03-26 17:24:15 -04003435 * Add a person that is relevant to this notification.
3436 *
Chris Wrene6c48932014-09-29 17:19:27 -04003437 * <P>
3438 * Depending on user preferences, this annotation may allow the notification to pass
Julia Reynoldse071abd2017-03-22 10:52:11 -04003439 * through interruption filters, if this notification is of category {@link #CATEGORY_CALL}
3440 * or {@link #CATEGORY_MESSAGE}. The addition of people may also cause this notification to
3441 * appear more prominently in the user interface.
Chris Wrene6c48932014-09-29 17:19:27 -04003442 * </P>
3443 *
3444 * <P>
3445 * The person should be specified by the {@code String} representation of a
3446 * {@link android.provider.ContactsContract.Contacts#CONTENT_LOOKUP_URI}.
3447 * </P>
3448 *
3449 * <P>The system will also attempt to resolve {@code mailto:} and {@code tel:} schema
3450 * URIs. The path part of these URIs must exist in the contacts database, in the
3451 * appropriate column, or the reference will be discarded as invalid. Telephone schema
3452 * URIs will be resolved by {@link android.provider.ContactsContract.PhoneLookup}.
3453 * </P>
3454 *
3455 * @param uri A URI for the person.
Chris Wrendde75302014-03-26 17:24:15 -04003456 * @see Notification#EXTRA_PEOPLE
3457 */
Chris Wrene6c48932014-09-29 17:19:27 -04003458 public Builder addPerson(String uri) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003459 mPersonList.add(uri);
Chris Wrendde75302014-03-26 17:24:15 -04003460 return this;
3461 }
3462
3463 /**
Griff Hazen5cadc3b2014-05-20 09:55:39 -07003464 * Set this notification to be part of a group of notifications sharing the same key.
3465 * Grouped notifications may display in a cluster or stack on devices which
3466 * support such rendering.
3467 *
3468 * <p>To make this notification the summary for its group, also call
3469 * {@link #setGroupSummary}. A sort order can be specified for group members by using
3470 * {@link #setSortKey}.
3471 * @param groupKey The group key of the group.
3472 * @return this object for method chaining
3473 */
3474 public Builder setGroup(String groupKey) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003475 mN.mGroupKey = groupKey;
Griff Hazen5cadc3b2014-05-20 09:55:39 -07003476 return this;
3477 }
3478
3479 /**
3480 * Set this notification to be the group summary for a group of notifications.
3481 * Grouped notifications may display in a cluster or stack on devices which
Julia Reynolds04499532016-09-13 14:04:53 -04003482 * support such rendering. If thereRequires a group key also be set using {@link #setGroup}.
3483 * The group summary may be suppressed if too few notifications are included in the group.
Griff Hazen5cadc3b2014-05-20 09:55:39 -07003484 * @param isGroupSummary Whether this notification should be a group summary.
3485 * @return this object for method chaining
3486 */
3487 public Builder setGroupSummary(boolean isGroupSummary) {
3488 setFlag(FLAG_GROUP_SUMMARY, isGroupSummary);
3489 return this;
3490 }
3491
3492 /**
3493 * Set a sort key that orders this notification among other notifications from the
3494 * same package. This can be useful if an external sort was already applied and an app
3495 * would like to preserve this. Notifications will be sorted lexicographically using this
3496 * value, although providing different priorities in addition to providing sort key may
3497 * cause this value to be ignored.
3498 *
3499 * <p>This sort key can also be used to order members of a notification group. See
Griff Hazen9e1379f2014-05-20 12:50:51 -07003500 * {@link #setGroup}.
Griff Hazen5cadc3b2014-05-20 09:55:39 -07003501 *
3502 * @see String#compareTo(String)
3503 */
3504 public Builder setSortKey(String sortKey) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003505 mN.mSortKey = sortKey;
Griff Hazen5cadc3b2014-05-20 09:55:39 -07003506 return this;
3507 }
3508
3509 /**
Griff Hazen720042b2014-02-24 15:46:56 -08003510 * Merge additional metadata into this notification.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003511 *
Griff Hazen720042b2014-02-24 15:46:56 -08003512 * <p>Values within the Bundle will replace existing extras values in this Builder.
3513 *
3514 * @see Notification#extras
3515 */
Griff Hazen959591e2014-05-15 22:26:18 -07003516 public Builder addExtras(Bundle extras) {
3517 if (extras != null) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003518 mUserExtras.putAll(extras);
Griff Hazen720042b2014-02-24 15:46:56 -08003519 }
3520 return this;
3521 }
3522
3523 /**
3524 * Set metadata for this notification.
3525 *
3526 * <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 -04003527 * current contents are copied into the Notification each time {@link #build()} is
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003528 * called.
3529 *
Griff Hazen720042b2014-02-24 15:46:56 -08003530 * <p>Replaces any existing extras values with those from the provided Bundle.
3531 * Use {@link #addExtras} to merge in metadata instead.
3532 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003533 * @see Notification#extras
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003534 */
Griff Hazen959591e2014-05-15 22:26:18 -07003535 public Builder setExtras(Bundle extras) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003536 if (extras != null) {
3537 mUserExtras = extras;
3538 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003539 return this;
3540 }
3541
Daniel Sandlera0a938c2012-03-15 08:42:37 -04003542 /**
Griff Hazen720042b2014-02-24 15:46:56 -08003543 * Get the current metadata Bundle used by this notification Builder.
3544 *
3545 * <p>The returned Bundle is shared with this Builder.
3546 *
3547 * <p>The current contents of this Bundle are copied into the Notification each time
3548 * {@link #build()} is called.
3549 *
3550 * @see Notification#extras
3551 */
3552 public Bundle getExtras() {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003553 return mUserExtras;
3554 }
3555
3556 private Bundle getAllExtras() {
3557 final Bundle saveExtras = (Bundle) mUserExtras.clone();
3558 saveExtras.putAll(mN.extras);
3559 return saveExtras;
Griff Hazen720042b2014-02-24 15:46:56 -08003560 }
3561
3562 /**
Daniel Sandlera0a938c2012-03-15 08:42:37 -04003563 * Add an action to this notification. Actions are typically displayed by
3564 * the system as a button adjacent to the notification content.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04003565 * <p>
3566 * Every action must have an icon (32dp square and matching the
3567 * <a href="{@docRoot}design/style/iconography.html#action-bar">Holo
3568 * Dark action bar</a> visual style), a textual label, and a {@link PendingIntent}.
3569 * <p>
3570 * A notification in its expanded form can display up to 3 actions, from left to right in
3571 * the order they were added. Actions will not be displayed when the notification is
3572 * collapsed, however, so be sure that any essential functions may be accessed by the user
3573 * in some other way (for example, in the Activity pointed to by {@link #contentIntent}).
Daniel Sandlera0a938c2012-03-15 08:42:37 -04003574 *
3575 * @param icon Resource ID of a drawable that represents the action.
3576 * @param title Text describing the action.
3577 * @param intent PendingIntent to be fired when the action is invoked.
Dan Sandler86647982015-05-13 23:41:13 -04003578 *
3579 * @deprecated Use {@link #addAction(Action)} instead.
Daniel Sandlera0a938c2012-03-15 08:42:37 -04003580 */
Dan Sandler86647982015-05-13 23:41:13 -04003581 @Deprecated
Daniel Sandlera0a938c2012-03-15 08:42:37 -04003582 public Builder addAction(int icon, CharSequence title, PendingIntent intent) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04003583 mActions.add(new Action(icon, safeCharSequence(title), intent));
Daniel Sandlera0a938c2012-03-15 08:42:37 -04003584 return this;
3585 }
3586
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003587 /**
Griff Hazen959591e2014-05-15 22:26:18 -07003588 * Add an action to this notification. Actions are typically displayed by
3589 * the system as a button adjacent to the notification content.
3590 * <p>
3591 * Every action must have an icon (32dp square and matching the
3592 * <a href="{@docRoot}design/style/iconography.html#action-bar">Holo
3593 * Dark action bar</a> visual style), a textual label, and a {@link PendingIntent}.
3594 * <p>
3595 * A notification in its expanded form can display up to 3 actions, from left to right in
3596 * the order they were added. Actions will not be displayed when the notification is
3597 * collapsed, however, so be sure that any essential functions may be accessed by the user
3598 * in some other way (for example, in the Activity pointed to by {@link #contentIntent}).
3599 *
3600 * @param action The action to add.
3601 */
3602 public Builder addAction(Action action) {
liangweikang63b03b52017-03-16 19:22:15 +08003603 if (action != null) {
3604 mActions.add(action);
3605 }
Griff Hazen959591e2014-05-15 22:26:18 -07003606 return this;
3607 }
3608
3609 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003610 * Alter the complete list of actions attached to this notification.
3611 * @see #addAction(Action).
3612 *
3613 * @param actions
3614 * @return
3615 */
3616 public Builder setActions(Action... actions) {
3617 mActions.clear();
3618 for (int i = 0; i < actions.length; i++) {
liangweikang63b03b52017-03-16 19:22:15 +08003619 if (actions[i] != null) {
3620 mActions.add(actions[i]);
3621 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003622 }
3623 return this;
3624 }
3625
3626 /**
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003627 * Add a rich notification style to be applied at build time.
3628 *
3629 * @param style Object responsible for modifying the notification style.
3630 */
3631 public Builder setStyle(Style style) {
3632 if (mStyle != style) {
3633 mStyle = style;
Daniel Sandlerc08dea22012-06-28 08:35:24 -07003634 if (mStyle != null) {
3635 mStyle.setBuilder(this);
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003636 mN.extras.putString(EXTRA_TEMPLATE, style.getClass().getName());
3637 } else {
3638 mN.extras.remove(EXTRA_TEMPLATE);
Daniel Sandlerc08dea22012-06-28 08:35:24 -07003639 }
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003640 }
3641 return this;
3642 }
3643
Dan Sandler0bf2ed82013-12-21 23:33:41 -06003644 /**
3645 * Specify the value of {@link #visibility}.
Griff Hazenb720abe2014-05-20 13:15:30 -07003646 *
Dan Sandler0bf2ed82013-12-21 23:33:41 -06003647 * @return The same Builder.
3648 */
Jeff Sharkey30e06bb2017-04-24 11:18:03 -06003649 public Builder setVisibility(@Visibility int visibility) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003650 mN.visibility = visibility;
Dan Sandler0bf2ed82013-12-21 23:33:41 -06003651 return this;
3652 }
3653
3654 /**
3655 * Supply a replacement Notification whose contents should be shown in insecure contexts
3656 * (i.e. atop the secure lockscreen). See {@link #visibility} and {@link #VISIBILITY_PUBLIC}.
3657 * @param n A replacement notification, presumably with some or all info redacted.
3658 * @return The same Builder.
3659 */
3660 public Builder setPublicVersion(Notification n) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003661 if (n != null) {
3662 mN.publicVersion = new Notification();
3663 n.cloneInto(mN.publicVersion, /*heavy=*/ true);
3664 } else {
3665 mN.publicVersion = null;
3666 }
Dan Sandler0bf2ed82013-12-21 23:33:41 -06003667 return this;
3668 }
3669
Griff Hazenb720abe2014-05-20 13:15:30 -07003670 /**
Griff Hazen5cadc3b2014-05-20 09:55:39 -07003671 * Apply an extender to this notification builder. Extenders may be used to add
3672 * metadata or change options on this builder.
3673 */
Griff Hazen61a9e862014-05-22 16:05:19 -07003674 public Builder extend(Extender extender) {
3675 extender.extend(this);
Griff Hazen5cadc3b2014-05-20 09:55:39 -07003676 return this;
3677 }
3678
Dan Sandler4e787062015-06-17 15:09:48 -04003679 /**
3680 * @hide
3681 */
Julia Reynoldse46bb372016-03-17 11:05:58 -04003682 public Builder setFlag(int mask, boolean value) {
Joe Onorato46439ce2010-11-19 13:56:21 -08003683 if (value) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003684 mN.flags |= mask;
Joe Onorato46439ce2010-11-19 13:56:21 -08003685 } else {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003686 mN.flags &= ~mask;
Joe Onorato46439ce2010-11-19 13:56:21 -08003687 }
Julia Reynoldse46bb372016-03-17 11:05:58 -04003688 return this;
Joe Onorato46439ce2010-11-19 13:56:21 -08003689 }
3690
Dan Sandler26e81cf2014-05-06 10:01:27 -04003691 /**
3692 * Sets {@link Notification#color}.
3693 *
3694 * @param argb The accent color to use
3695 *
3696 * @return The same Builder.
3697 */
Tor Norbye80756e32015-03-02 09:39:27 -08003698 public Builder setColor(@ColorInt int argb) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003699 mN.color = argb;
Julia Reynolds10ee1fc2015-11-09 11:04:55 -05003700 sanitizeColor();
Dan Sandler26e81cf2014-05-06 10:01:27 -04003701 return this;
3702 }
3703
Jorim Jaggid05aa3e2014-08-28 17:52:27 +02003704 private Drawable getProfileBadgeDrawable() {
Chris Wren66619a22016-05-12 16:42:37 -04003705 if (mContext.getUserId() == UserHandle.USER_SYSTEM) {
3706 // This user can never be a badged profile,
3707 // and also includes USER_ALL system notifications.
3708 return null;
3709 }
Christoph Studer7ac80e62014-08-04 16:01:57 +02003710 // Note: This assumes that the current user can read the profile badge of the
3711 // originating user.
Selim Cineke6ff9462016-01-15 15:07:06 -08003712 return mContext.getPackageManager().getUserBadgeForDensityNoBackground(
Julia Reynoldsda303542015-11-23 14:00:20 -05003713 new UserHandle(mContext.getUserId()), 0);
Jorim Jaggid05aa3e2014-08-28 17:52:27 +02003714 }
3715
3716 private Bitmap getProfileBadge() {
3717 Drawable badge = getProfileBadgeDrawable();
Kenny Guy8a0101b2014-05-08 23:34:12 +01003718 if (badge == null) {
3719 return null;
3720 }
Jorim Jaggid05aa3e2014-08-28 17:52:27 +02003721 final int size = mContext.getResources().getDimensionPixelSize(
3722 R.dimen.notification_badge_size);
3723 Bitmap bitmap = Bitmap.createBitmap(size, size, Bitmap.Config.ARGB_8888);
Kenny Guy8a0101b2014-05-08 23:34:12 +01003724 Canvas canvas = new Canvas(bitmap);
Jorim Jaggid05aa3e2014-08-28 17:52:27 +02003725 badge.setBounds(0, 0, size, size);
Kenny Guy8a0101b2014-05-08 23:34:12 +01003726 badge.draw(canvas);
3727 return bitmap;
3728 }
3729
Selim Cinekc848c3a2016-01-13 15:27:30 -08003730 private void bindProfileBadge(RemoteViews contentView) {
Kenny Guy98193ea2014-07-24 19:54:37 +01003731 Bitmap profileBadge = getProfileBadge();
3732
Kenny Guy98193ea2014-07-24 19:54:37 +01003733 if (profileBadge != null) {
Selim Cinekc848c3a2016-01-13 15:27:30 -08003734 contentView.setImageViewBitmap(R.id.profile_badge, profileBadge);
3735 contentView.setViewVisibility(R.id.profile_badge, View.VISIBLE);
Selim Cinek7b9605b2017-01-19 17:36:00 -08003736 if (isColorized()) {
3737 contentView.setDrawableParameters(R.id.profile_badge, false, -1,
3738 getPrimaryTextColor(), PorterDuff.Mode.SRC_ATOP, -1);
3739 }
Kenny Guy98193ea2014-07-24 19:54:37 +01003740 }
Kenny Guy98193ea2014-07-24 19:54:37 +01003741 }
3742
Christoph Studerfe718432014-09-01 18:21:18 +02003743 private void resetStandardTemplate(RemoteViews contentView) {
Selim Cinekeaa29ca2015-11-23 13:51:13 -08003744 resetNotificationHeader(contentView);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003745 resetContentMargins(contentView);
Christoph Studerfe718432014-09-01 18:21:18 +02003746 contentView.setViewVisibility(R.id.right_icon, View.GONE);
Selim Cinek860b6da2015-12-16 19:02:19 -08003747 contentView.setViewVisibility(R.id.title, View.GONE);
Christoph Studerfe718432014-09-01 18:21:18 +02003748 contentView.setTextViewText(R.id.title, null);
Selim Cinek41598732016-01-11 16:58:37 -08003749 contentView.setViewVisibility(R.id.text, View.GONE);
Christoph Studerfe718432014-09-01 18:21:18 +02003750 contentView.setTextViewText(R.id.text, null);
Selim Cinek29603462015-11-17 19:04:39 -08003751 contentView.setViewVisibility(R.id.text_line_1, View.GONE);
Selim Cinek41598732016-01-11 16:58:37 -08003752 contentView.setTextViewText(R.id.text_line_1, null);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003753 }
3754
Selim Cinekeaa29ca2015-11-23 13:51:13 -08003755 /**
3756 * Resets the notification header to its original state
3757 */
3758 private void resetNotificationHeader(RemoteViews contentView) {
Adrian Roosc4337a32016-08-02 18:30:34 -07003759 // Small icon doesn't need to be reset, as it's always set. Resetting would prevent
3760 // re-using the drawable when the notification is updated.
Selim Cinek7b836392015-12-04 20:02:59 -08003761 contentView.setBoolean(R.id.notification_header, "setExpanded", false);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003762 contentView.setTextViewText(R.id.app_name_text, null);
Christoph Studerca1db712014-09-10 17:31:33 +02003763 contentView.setViewVisibility(R.id.chronometer, View.GONE);
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07003764 contentView.setViewVisibility(R.id.header_text, View.GONE);
Adrian Roos9dfb78f2016-06-30 15:43:44 -07003765 contentView.setTextViewText(R.id.header_text, null);
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07003766 contentView.setViewVisibility(R.id.header_text_divider, View.GONE);
Selim Cinek29603462015-11-17 19:04:39 -08003767 contentView.setViewVisibility(R.id.time_divider, View.GONE);
Selim Cinekb85f36fd2016-04-20 18:46:36 -07003768 contentView.setViewVisibility(R.id.time, View.GONE);
Selim Cinekc848c3a2016-01-13 15:27:30 -08003769 contentView.setImageViewIcon(R.id.profile_badge, null);
3770 contentView.setViewVisibility(R.id.profile_badge, View.GONE);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003771 }
3772
3773 private void resetContentMargins(RemoteViews contentView) {
Adrian Roos2d5dbba2016-06-08 17:11:53 -07003774 contentView.setViewLayoutMarginEndDimen(R.id.line1, 0);
3775 contentView.setViewLayoutMarginEndDimen(R.id.text, 0);
Christoph Studerfe718432014-09-01 18:21:18 +02003776 }
3777
Jorim Jaggi445d3c02014-08-19 22:33:42 +02003778 private RemoteViews applyStandardTemplate(int resId) {
Adrian Roos70d7aa32017-01-11 15:39:06 -08003779 return applyStandardTemplate(resId, mParams.reset().fillTextsFrom(this));
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02003780 }
3781
3782 /**
3783 * @param hasProgress whether the progress bar should be shown and set
3784 */
3785 private RemoteViews applyStandardTemplate(int resId, boolean hasProgress) {
Adrian Roos70d7aa32017-01-11 15:39:06 -08003786 return applyStandardTemplate(resId, mParams.reset().hasProgress(hasProgress)
3787 .fillTextsFrom(this));
Adrian Roosc1a80b02016-04-05 14:54:55 -07003788 }
3789
Adrian Roos70d7aa32017-01-11 15:39:06 -08003790 private RemoteViews applyStandardTemplate(int resId, StandardTemplateParams p) {
Kenny Guy77320062014-08-27 21:37:15 +01003791 RemoteViews contentView = new BuilderRemoteViews(mContext.getApplicationInfo(), resId);
Dan Sandler539aad42014-08-04 00:43:39 -04003792
Christoph Studerfe718432014-09-01 18:21:18 +02003793 resetStandardTemplate(contentView);
3794
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003795 final Bundle ex = mN.extras;
Selim Cinek7b9605b2017-01-19 17:36:00 -08003796 updateBackgroundColor(contentView);
Adrian Roos487374f2017-01-11 15:48:14 -08003797 bindNotificationHeader(contentView, p.ambient);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003798 bindLargeIcon(contentView);
Adrian Roos70d7aa32017-01-11 15:39:06 -08003799 boolean showProgress = handleProgressBar(p.hasProgress, contentView, ex);
3800 if (p.title != null) {
Selim Cinek860b6da2015-12-16 19:02:19 -08003801 contentView.setViewVisibility(R.id.title, View.VISIBLE);
Adrian Roos70d7aa32017-01-11 15:39:06 -08003802 contentView.setTextViewText(R.id.title, p.title);
Adrian Roos72171622017-01-27 10:32:06 -08003803 if (!p.ambient) {
3804 setTextViewColorPrimary(contentView, R.id.title);
3805 }
Selim Cinek954cc232016-05-20 13:29:23 -07003806 contentView.setViewLayoutWidth(R.id.title, showProgress
3807 ? ViewGroup.LayoutParams.WRAP_CONTENT
3808 : ViewGroup.LayoutParams.MATCH_PARENT);
Joe Onorato561d3852010-11-20 18:09:34 -08003809 }
Adrian Roos70d7aa32017-01-11 15:39:06 -08003810 if (p.text != null) {
Selim Cinek41598732016-01-11 16:58:37 -08003811 int textId = showProgress ? com.android.internal.R.id.text_line_1
3812 : com.android.internal.R.id.text;
Adrian Roos70d7aa32017-01-11 15:39:06 -08003813 contentView.setTextViewText(textId, p.text);
Adrian Roos72171622017-01-27 10:32:06 -08003814 if (!p.ambient) {
3815 setTextViewColorSecondary(contentView, textId);
3816 }
Selim Cinek41598732016-01-11 16:58:37 -08003817 contentView.setViewVisibility(textId, View.VISIBLE);
Joe Onorato561d3852010-11-20 18:09:34 -08003818 }
Selim Cinekc848c3a2016-01-13 15:27:30 -08003819
Selim Cinek279fa862016-06-14 10:57:25 -07003820 setContentMinHeight(contentView, showProgress || mN.hasLargeIcon());
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003821
Selim Cinek29603462015-11-17 19:04:39 -08003822 return contentView;
3823 }
3824
Selim Cinek7b9605b2017-01-19 17:36:00 -08003825 private void setTextViewColorPrimary(RemoteViews contentView, int id) {
3826 ensureColors();
3827 contentView.setTextColor(id, mPrimaryTextColor);
3828 }
3829
3830 private int getPrimaryTextColor() {
3831 ensureColors();
3832 return mPrimaryTextColor;
3833 }
3834
3835 private int getActionBarColor() {
3836 ensureColors();
3837 return mActionBarColor;
3838 }
3839
Selim Cinek622c64a2017-04-17 17:10:05 -07003840 private int getActionBarColorDeEmphasized() {
3841 int backgroundColor = getBackgroundColor();
3842 return NotificationColorUtil.getShiftedColor(backgroundColor, 12);
3843 }
3844
Selim Cinek7b9605b2017-01-19 17:36:00 -08003845 private void setTextViewColorSecondary(RemoteViews contentView, int id) {
3846 ensureColors();
3847 contentView.setTextColor(id, mSecondaryTextColor);
3848 }
3849
3850 private void ensureColors() {
3851 int backgroundColor = getBackgroundColor();
3852 if (mPrimaryTextColor == COLOR_INVALID
3853 || mSecondaryTextColor == COLOR_INVALID
3854 || mActionBarColor == COLOR_INVALID
3855 || mTextColorsAreForBackground != backgroundColor) {
3856 mTextColorsAreForBackground = backgroundColor;
3857 mPrimaryTextColor = NotificationColorUtil.resolvePrimaryColor(
3858 mContext, backgroundColor);
3859 mSecondaryTextColor = NotificationColorUtil.resolveSecondaryColor(
3860 mContext, backgroundColor);
Selim Cinek875ba9b2017-02-13 16:20:17 -08003861 mActionBarColor = NotificationColorUtil.resolveActionBarColor(mContext,
3862 backgroundColor);
Selim Cinek7b9605b2017-01-19 17:36:00 -08003863 }
3864 }
3865
3866 private void updateBackgroundColor(RemoteViews contentView) {
3867 if (isColorized()) {
3868 contentView.setInt(R.id.status_bar_latest_event_content, "setBackgroundColor",
3869 getBackgroundColor());
3870 } else {
3871 // Clear it!
3872 contentView.setInt(R.id.status_bar_latest_event_content, "setBackgroundResource",
3873 0);
3874 }
3875 }
3876
Selim Cinek860b6da2015-12-16 19:02:19 -08003877 /**
3878 * @param remoteView the remote view to update the minheight in
3879 * @param hasMinHeight does it have a mimHeight
3880 * @hide
3881 */
3882 void setContentMinHeight(RemoteViews remoteView, boolean hasMinHeight) {
3883 int minHeight = 0;
3884 if (hasMinHeight) {
3885 // we need to set the minHeight of the notification
3886 minHeight = mContext.getResources().getDimensionPixelSize(
3887 com.android.internal.R.dimen.notification_min_content_height);
3888 }
3889 remoteView.setInt(R.id.notification_main_column, "setMinimumHeight", minHeight);
3890 }
3891
Selim Cinek29603462015-11-17 19:04:39 -08003892 private boolean handleProgressBar(boolean hasProgress, RemoteViews contentView, Bundle ex) {
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003893 final int max = ex.getInt(EXTRA_PROGRESS_MAX, 0);
3894 final int progress = ex.getInt(EXTRA_PROGRESS, 0);
3895 final boolean ind = ex.getBoolean(EXTRA_PROGRESS_INDETERMINATE);
3896 if (hasProgress && (max != 0 || ind)) {
Selim Cinek29603462015-11-17 19:04:39 -08003897 contentView.setViewVisibility(com.android.internal.R.id.progress, View.VISIBLE);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003898 contentView.setProgressBar(
Selim Cinek29603462015-11-17 19:04:39 -08003899 R.id.progress, max, progress, ind);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003900 contentView.setProgressBackgroundTintList(
3901 R.id.progress, ColorStateList.valueOf(mContext.getColor(
3902 R.color.notification_progress_background_color)));
Selim Cinek29603462015-11-17 19:04:39 -08003903 if (mN.color != COLOR_DEFAULT) {
Adrian Roos4ff3b122016-02-01 12:26:13 -08003904 ColorStateList colorStateList = ColorStateList.valueOf(resolveContrastColor());
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003905 contentView.setProgressTintList(R.id.progress, colorStateList);
3906 contentView.setProgressIndeterminateTintList(R.id.progress, colorStateList);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003907 }
Selim Cinek29603462015-11-17 19:04:39 -08003908 return true;
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003909 } else {
3910 contentView.setViewVisibility(R.id.progress, View.GONE);
Selim Cinek29603462015-11-17 19:04:39 -08003911 return false;
Jeff Sharkey1c400132011-08-05 14:50:13 -07003912 }
Joe Onorato561d3852010-11-20 18:09:34 -08003913 }
3914
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003915 private void bindLargeIcon(RemoteViews contentView) {
Selim Cinek279fa862016-06-14 10:57:25 -07003916 if (mN.mLargeIcon == null && mN.largeIcon != null) {
3917 mN.mLargeIcon = Icon.createWithBitmap(mN.largeIcon);
3918 }
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003919 if (mN.mLargeIcon != null) {
3920 contentView.setViewVisibility(R.id.right_icon, View.VISIBLE);
3921 contentView.setImageViewIcon(R.id.right_icon, mN.mLargeIcon);
3922 processLargeLegacyIcon(mN.mLargeIcon, contentView);
Adrian Roos2d5dbba2016-06-08 17:11:53 -07003923 int endMargin = R.dimen.notification_content_picture_margin;
3924 contentView.setViewLayoutMarginEndDimen(R.id.line1, endMargin);
3925 contentView.setViewLayoutMarginEndDimen(R.id.text, endMargin);
3926 contentView.setViewLayoutMarginEndDimen(R.id.progress, endMargin);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003927 }
3928 }
3929
Adrian Roos487374f2017-01-11 15:48:14 -08003930 private void bindNotificationHeader(RemoteViews contentView, boolean ambient) {
3931 bindSmallIcon(contentView, ambient);
3932 bindHeaderAppName(contentView, ambient);
3933 if (!ambient) {
3934 // Ambient view does not have these
3935 bindHeaderText(contentView);
3936 bindHeaderChronometerAndTime(contentView);
Adrian Roos487374f2017-01-11 15:48:14 -08003937 bindProfileBadge(contentView);
3938 }
Adrian Roosd83e9992017-03-16 15:17:57 -07003939 bindExpandButton(contentView);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003940 }
3941
3942 private void bindExpandButton(RemoteViews contentView) {
Selim Cinek99104832017-01-25 14:47:33 -08003943 int color = getPrimaryHighlightColor();
Selim Cinek7b9605b2017-01-19 17:36:00 -08003944 contentView.setDrawableParameters(R.id.expand_button, false, -1, color,
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003945 PorterDuff.Mode.SRC_ATOP, -1);
Selim Cinekea4bef72015-12-02 15:51:10 -08003946 contentView.setInt(R.id.notification_header, "setOriginalNotificationColor",
Selim Cinek7b9605b2017-01-19 17:36:00 -08003947 color);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003948 }
3949
Selim Cinek99104832017-01-25 14:47:33 -08003950 /**
3951 * @return the color that is used as the first primary highlight color. This is applied
3952 * in several places like the action buttons or the app name in the header.
3953 */
3954 private int getPrimaryHighlightColor() {
3955 return isColorized() ? getPrimaryTextColor() : resolveContrastColor();
3956 }
3957
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003958 private void bindHeaderChronometerAndTime(RemoteViews contentView) {
3959 if (showsTimeOrChronometer()) {
Selim Cinek29603462015-11-17 19:04:39 -08003960 contentView.setViewVisibility(R.id.time_divider, View.VISIBLE);
Selim Cinek7b9605b2017-01-19 17:36:00 -08003961 setTextViewColorSecondary(contentView, R.id.time_divider);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003962 if (mN.extras.getBoolean(EXTRA_SHOW_CHRONOMETER)) {
3963 contentView.setViewVisibility(R.id.chronometer, View.VISIBLE);
3964 contentView.setLong(R.id.chronometer, "setBase",
3965 mN.when + (SystemClock.elapsedRealtime() - System.currentTimeMillis()));
3966 contentView.setBoolean(R.id.chronometer, "setStarted", true);
Adrian Roos96b7e202016-05-17 13:50:38 -07003967 boolean countsDown = mN.extras.getBoolean(EXTRA_CHRONOMETER_COUNT_DOWN);
Selim Cinekc3b752e2016-04-20 16:13:59 -07003968 contentView.setChronometerCountDown(R.id.chronometer, countsDown);
Selim Cinek7b9605b2017-01-19 17:36:00 -08003969 setTextViewColorSecondary(contentView, R.id.chronometer);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003970 } else {
3971 contentView.setViewVisibility(R.id.time, View.VISIBLE);
3972 contentView.setLong(R.id.time, "setTime", mN.when);
Selim Cinek7b9605b2017-01-19 17:36:00 -08003973 setTextViewColorSecondary(contentView, R.id.time);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003974 }
Selim Cinekb85f36fd2016-04-20 18:46:36 -07003975 } else {
3976 // We still want a time to be set but gone, such that we can show and hide it
3977 // on demand in case it's a child notification without anything in the header
3978 contentView.setLong(R.id.time, "setTime", mN.when != 0 ? mN.when : mN.creationTime);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003979 }
3980 }
3981
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07003982 private void bindHeaderText(RemoteViews contentView) {
3983 CharSequence headerText = mN.extras.getCharSequence(EXTRA_SUB_TEXT);
3984 if (headerText == null && mStyle != null && mStyle.mSummaryTextSet
Selim Cinek03d0d652015-11-13 13:18:09 -05003985 && mStyle.hasSummaryInHeader()) {
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07003986 headerText = mStyle.mSummaryText;
Selim Cinek03d0d652015-11-13 13:18:09 -05003987 }
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07003988 if (headerText == null
3989 && mContext.getApplicationInfo().targetSdkVersion < Build.VERSION_CODES.N
3990 && mN.extras.getCharSequence(EXTRA_INFO_TEXT) != null) {
3991 headerText = mN.extras.getCharSequence(EXTRA_INFO_TEXT);
3992 }
3993 if (headerText != null) {
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003994 // TODO: Remove the span entirely to only have the string with propper formating.
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07003995 contentView.setTextViewText(R.id.header_text, processLegacyText(headerText));
Selim Cinek7b9605b2017-01-19 17:36:00 -08003996 setTextViewColorSecondary(contentView, R.id.header_text);
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07003997 contentView.setViewVisibility(R.id.header_text, View.VISIBLE);
3998 contentView.setViewVisibility(R.id.header_text_divider, View.VISIBLE);
Selim Cinek7b9605b2017-01-19 17:36:00 -08003999 setTextViewColorSecondary(contentView, R.id.header_text_divider);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004000 }
4001 }
4002
Adrian Rooseba05822016-04-22 17:09:27 -07004003 /**
4004 * @hide
4005 */
4006 public String loadHeaderAppName() {
Dan Sandler732bd6c2016-04-12 14:20:32 -04004007 CharSequence name = null;
4008 final PackageManager pm = mContext.getPackageManager();
4009 if (mN.extras.containsKey(EXTRA_SUBSTITUTE_APP_NAME)) {
4010 // only system packages which lump together a bunch of unrelated stuff
4011 // may substitute a different name to make the purpose of the
4012 // notification more clear. the correct package label should always
4013 // be accessible via SystemUI.
4014 final String pkg = mContext.getPackageName();
4015 final String subName = mN.extras.getString(EXTRA_SUBSTITUTE_APP_NAME);
4016 if (PackageManager.PERMISSION_GRANTED == pm.checkPermission(
4017 android.Manifest.permission.SUBSTITUTE_NOTIFICATION_APP_NAME, pkg)) {
4018 name = subName;
4019 } else {
4020 Log.w(TAG, "warning: pkg "
4021 + pkg + " attempting to substitute app name '" + subName
4022 + "' without holding perm "
4023 + android.Manifest.permission.SUBSTITUTE_NOTIFICATION_APP_NAME);
4024 }
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004025 }
Dan Sandler732bd6c2016-04-12 14:20:32 -04004026 if (TextUtils.isEmpty(name)) {
4027 name = pm.getApplicationLabel(mContext.getApplicationInfo());
4028 }
4029 if (TextUtils.isEmpty(name)) {
4030 // still nothing?
4031 return null;
4032 }
4033
4034 return String.valueOf(name);
4035 }
Adrian Roos487374f2017-01-11 15:48:14 -08004036 private void bindHeaderAppName(RemoteViews contentView, boolean ambient) {
Dan Sandler732bd6c2016-04-12 14:20:32 -04004037 contentView.setTextViewText(R.id.app_name_text, loadHeaderAppName());
Adrian Roos72171622017-01-27 10:32:06 -08004038 if (isColorized() && !ambient) {
Selim Cinek7b9605b2017-01-19 17:36:00 -08004039 setTextViewColorPrimary(contentView, R.id.app_name_text);
4040 } else {
4041 contentView.setTextColor(R.id.app_name_text,
4042 ambient ? resolveAmbientColor() : resolveContrastColor());
4043 }
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004044 }
4045
Adrian Roos487374f2017-01-11 15:48:14 -08004046 private void bindSmallIcon(RemoteViews contentView, boolean ambient) {
Selim Cinek279fa862016-06-14 10:57:25 -07004047 if (mN.mSmallIcon == null && mN.icon != 0) {
4048 mN.mSmallIcon = Icon.createWithResource(mContext, mN.icon);
4049 }
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004050 contentView.setImageViewIcon(R.id.icon, mN.mSmallIcon);
Adrian Roos9b45a152016-06-28 13:32:29 -07004051 contentView.setDrawableParameters(R.id.icon, false /* targetBackground */,
4052 -1 /* alpha */, -1 /* colorFilter */, null /* mode */, mN.iconLevel);
Adrian Roos487374f2017-01-11 15:48:14 -08004053 processSmallIconColor(mN.mSmallIcon, contentView, ambient);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004054 }
4055
Jorim Jaggi445d3c02014-08-19 22:33:42 +02004056 /**
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02004057 * @return true if the built notification will show the time or the chronometer; false
4058 * otherwise
4059 */
4060 private boolean showsTimeOrChronometer() {
Selim Cinekc2c0b042016-05-18 17:13:46 -07004061 return mN.showsTime() || mN.showsChronometer();
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02004062 }
4063
Christoph Studerfe718432014-09-01 18:21:18 +02004064 private void resetStandardTemplateWithActions(RemoteViews big) {
Adrian Roos4c1fcc82016-03-31 14:39:39 -07004065 // actions_container is only reset when there are no actions to avoid focus issues with
4066 // remote inputs.
Christoph Studerfe718432014-09-01 18:21:18 +02004067 big.setViewVisibility(R.id.actions, View.GONE);
Christoph Studerfe718432014-09-01 18:21:18 +02004068 big.removeAllViews(R.id.actions);
Adrian Roose458aa82015-12-08 16:17:19 -08004069
4070 big.setViewVisibility(R.id.notification_material_reply_container, View.GONE);
4071 big.setTextViewText(R.id.notification_material_reply_text_1, null);
4072
4073 big.setViewVisibility(R.id.notification_material_reply_text_2, View.GONE);
4074 big.setTextViewText(R.id.notification_material_reply_text_2, null);
4075 big.setViewVisibility(R.id.notification_material_reply_text_3, View.GONE);
4076 big.setTextViewText(R.id.notification_material_reply_text_3, null);
Adrian Roosf852a422016-06-03 13:33:43 -07004077
4078 big.setViewLayoutMarginBottomDimen(R.id.notification_action_list_margin_target, 0);
Christoph Studerfe718432014-09-01 18:21:18 +02004079 }
4080
Daniel Sandler96fd7c12012-03-30 16:37:36 -04004081 private RemoteViews applyStandardTemplateWithActions(int layoutId) {
Adrian Roos70d7aa32017-01-11 15:39:06 -08004082 return applyStandardTemplateWithActions(layoutId, mParams.reset().fillTextsFrom(this));
Adrian Roos48d746a2016-04-12 14:57:28 -07004083 }
4084
Adrian Roos70d7aa32017-01-11 15:39:06 -08004085 private RemoteViews applyStandardTemplateWithActions(int layoutId,
4086 StandardTemplateParams p) {
4087 RemoteViews big = applyStandardTemplate(layoutId, p);
Daniel Sandler96fd7c12012-03-30 16:37:36 -04004088
Christoph Studerfe718432014-09-01 18:21:18 +02004089 resetStandardTemplateWithActions(big);
4090
Adrian Roose458aa82015-12-08 16:17:19 -08004091 boolean validRemoteInput = false;
4092
Daniel Sandler96fd7c12012-03-30 16:37:36 -04004093 int N = mActions.size();
Adrian Roos487374f2017-01-11 15:48:14 -08004094 boolean emphazisedMode = mN.fullScreenIntent != null && !p.ambient;
Selim Cinek06e9e1f2016-07-08 17:14:16 -07004095 big.setBoolean(R.id.actions, "setEmphasizedMode", emphazisedMode);
Daniel Sandler96fd7c12012-03-30 16:37:36 -04004096 if (N > 0) {
Adrian Roos7052de52016-03-03 15:53:34 -08004097 big.setViewVisibility(R.id.actions_container, View.VISIBLE);
Daniel Sandler96fd7c12012-03-30 16:37:36 -04004098 big.setViewVisibility(R.id.actions, View.VISIBLE);
Adrian Roos487374f2017-01-11 15:48:14 -08004099 if (p.ambient) {
4100 big.setInt(R.id.actions, "setBackgroundColor", Color.TRANSPARENT);
Selim Cinek7b9605b2017-01-19 17:36:00 -08004101 } else if (isColorized()) {
4102 big.setInt(R.id.actions, "setBackgroundColor", getActionBarColor());
4103 } else {
4104 big.setInt(R.id.actions, "setBackgroundColor", mContext.getColor(
4105 R.color.notification_action_list));
Adrian Roos487374f2017-01-11 15:48:14 -08004106 }
Adrian Roosf852a422016-06-03 13:33:43 -07004107 big.setViewLayoutMarginBottomDimen(R.id.notification_action_list_margin_target,
4108 R.dimen.notification_action_list_height);
Daniel Sandler8680bf82012-05-15 16:52:52 -04004109 if (N>MAX_ACTION_BUTTONS) N=MAX_ACTION_BUTTONS;
Daniel Sandler96fd7c12012-03-30 16:37:36 -04004110 for (int i=0; i<N; i++) {
Adrian Roose458aa82015-12-08 16:17:19 -08004111 Action action = mActions.get(i);
4112 validRemoteInput |= hasValidRemoteInput(action);
4113
Selim Cinek06e9e1f2016-07-08 17:14:16 -07004114 final RemoteViews button = generateActionButton(action, emphazisedMode,
Adrian Roos487374f2017-01-11 15:48:14 -08004115 i % 2 != 0, p.ambient);
Daniel Sandler96fd7c12012-03-30 16:37:36 -04004116 big.addView(R.id.actions, button);
4117 }
Adrian Roos4c1fcc82016-03-31 14:39:39 -07004118 } else {
4119 big.setViewVisibility(R.id.actions_container, View.GONE);
Daniel Sandler96fd7c12012-03-30 16:37:36 -04004120 }
Adrian Roose458aa82015-12-08 16:17:19 -08004121
4122 CharSequence[] replyText = mN.extras.getCharSequenceArray(EXTRA_REMOTE_INPUT_HISTORY);
Adrian Roos487374f2017-01-11 15:48:14 -08004123 if (!p.ambient && validRemoteInput && replyText != null
Adrian Roose458aa82015-12-08 16:17:19 -08004124 && replyText.length > 0 && !TextUtils.isEmpty(replyText[0])) {
4125 big.setViewVisibility(R.id.notification_material_reply_container, View.VISIBLE);
4126 big.setTextViewText(R.id.notification_material_reply_text_1, replyText[0]);
Selim Cinek7b9605b2017-01-19 17:36:00 -08004127 setTextViewColorSecondary(big, R.id.notification_material_reply_text_1);
Adrian Roose458aa82015-12-08 16:17:19 -08004128
4129 if (replyText.length > 1 && !TextUtils.isEmpty(replyText[1])) {
4130 big.setViewVisibility(R.id.notification_material_reply_text_2, View.VISIBLE);
4131 big.setTextViewText(R.id.notification_material_reply_text_2, replyText[1]);
Selim Cinek7b9605b2017-01-19 17:36:00 -08004132 setTextViewColorSecondary(big, R.id.notification_material_reply_text_2);
Adrian Roose458aa82015-12-08 16:17:19 -08004133
4134 if (replyText.length > 2 && !TextUtils.isEmpty(replyText[2])) {
4135 big.setViewVisibility(
4136 R.id.notification_material_reply_text_3, View.VISIBLE);
4137 big.setTextViewText(R.id.notification_material_reply_text_3, replyText[2]);
Selim Cinek7b9605b2017-01-19 17:36:00 -08004138 setTextViewColorSecondary(big, R.id.notification_material_reply_text_3);
Adrian Roose458aa82015-12-08 16:17:19 -08004139 }
4140 }
4141 }
4142
Daniel Sandler96fd7c12012-03-30 16:37:36 -04004143 return big;
4144 }
4145
Adrian Roose458aa82015-12-08 16:17:19 -08004146 private boolean hasValidRemoteInput(Action action) {
4147 if (TextUtils.isEmpty(action.title) || action.actionIntent == null) {
4148 // Weird actions
4149 return false;
4150 }
4151
4152 RemoteInput[] remoteInputs = action.getRemoteInputs();
4153 if (remoteInputs == null) {
4154 return false;
4155 }
4156
4157 for (RemoteInput r : remoteInputs) {
4158 CharSequence[] choices = r.getChoices();
4159 if (r.getAllowFreeFormInput() || (choices != null && choices.length != 0)) {
4160 return true;
4161 }
4162 }
4163 return false;
4164 }
4165
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004166 /**
4167 * Construct a RemoteViews for the final 1U notification layout. In order:
4168 * 1. Custom contentView from the caller
4169 * 2. Style's proposed content view
4170 * 3. Standard template view
4171 */
Julia Reynolds3b848122016-02-26 10:45:32 -05004172 public RemoteViews createContentView() {
Selim Cinek7d1009b2017-01-25 15:28:28 -08004173 return createContentView(false /* increasedheight */ );
4174 }
4175
4176 /**
4177 * Construct a RemoteViews for the smaller content view.
4178 *
4179 * @param increasedHeight true if this layout be created with an increased height. Some
4180 * styles may support showing more then just that basic 1U size
4181 * and the system may decide to render important notifications
4182 * slightly bigger even when collapsed.
4183 *
4184 * @hide
4185 */
4186 public RemoteViews createContentView(boolean increasedHeight) {
Selim Cinek593610c2016-02-16 18:42:57 -08004187 if (mN.contentView != null && (mStyle == null || !mStyle.displayCustomViewInline())) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004188 return mN.contentView;
4189 } else if (mStyle != null) {
Selim Cinek7d1009b2017-01-25 15:28:28 -08004190 final RemoteViews styleView = mStyle.makeContentView(increasedHeight);
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004191 if (styleView != null) {
4192 return styleView;
4193 }
Joe Onorato46439ce2010-11-19 13:56:21 -08004194 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004195 return applyStandardTemplate(getBaseLayoutResource());
Joe Onorato46439ce2010-11-19 13:56:21 -08004196 }
4197
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004198 /**
4199 * Construct a RemoteViews for the final big notification layout.
4200 */
Julia Reynolds3b848122016-02-26 10:45:32 -05004201 public RemoteViews createBigContentView() {
Selim Cinek850a8542015-11-11 11:48:36 -05004202 RemoteViews result = null;
Selim Cinek593610c2016-02-16 18:42:57 -08004203 if (mN.bigContentView != null
4204 && (mStyle == null || !mStyle.displayCustomViewInline())) {
Julia Reynolds089e3e42015-11-18 09:59:57 -05004205 return mN.bigContentView;
4206 } else if (mStyle != null) {
Selim Cinek850a8542015-11-11 11:48:36 -05004207 result = mStyle.makeBigContentView();
Selim Cinek90dcf6d2015-11-18 20:24:13 -08004208 hideLine1Text(result);
Selim Cinekcc10bfb2016-02-10 16:24:21 -08004209 } else if (mActions.size() != 0) {
4210 result = applyStandardTemplateWithActions(getBigBaseLayoutResource());
Selim Cinek850a8542015-11-11 11:48:36 -05004211 }
Selim Cinek6743c0b2017-01-18 18:24:01 -08004212 makeHeaderExpanded(result);
Selim Cinek850a8542015-11-11 11:48:36 -05004213 return result;
4214 }
Daniel Sandler96fd7c12012-03-30 16:37:36 -04004215
Selim Cinekeaa29ca2015-11-23 13:51:13 -08004216 /**
Selim Cinek414ad332017-02-24 19:06:12 -08004217 * Construct a RemoteViews for the final notification header only. This will not be
4218 * colorized.
Selim Cinekeaa29ca2015-11-23 13:51:13 -08004219 *
4220 * @hide
4221 */
4222 public RemoteViews makeNotificationHeader() {
Selim Cinek414ad332017-02-24 19:06:12 -08004223 Boolean colorized = (Boolean) mN.extras.get(EXTRA_COLORIZED);
4224 mN.extras.putBoolean(EXTRA_COLORIZED, false);
Selim Cinekeaa29ca2015-11-23 13:51:13 -08004225 RemoteViews header = new BuilderRemoteViews(mContext.getApplicationInfo(),
4226 R.layout.notification_template_header);
4227 resetNotificationHeader(header);
Adrian Roos487374f2017-01-11 15:48:14 -08004228 bindNotificationHeader(header, false /* ambient */);
Selim Cinek414ad332017-02-24 19:06:12 -08004229 if (colorized != null) {
4230 mN.extras.putBoolean(EXTRA_COLORIZED, colorized);
4231 } else {
4232 mN.extras.remove(EXTRA_COLORIZED);
4233 }
Selim Cinekeaa29ca2015-11-23 13:51:13 -08004234 return header;
4235 }
4236
Adrian Roos487374f2017-01-11 15:48:14 -08004237 /**
4238 * Construct a RemoteViews for the ambient version of the notification.
4239 *
4240 * @hide
4241 */
4242 public RemoteViews makeAmbientNotification() {
4243 RemoteViews ambient = applyStandardTemplateWithActions(
4244 R.layout.notification_template_material_ambient,
Adrian Roos0bc3f6a2017-03-06 11:54:05 -08004245 mParams.reset().ambient(true).fillTextsFrom(this).hasProgress(false));
Adrian Roos487374f2017-01-11 15:48:14 -08004246 return ambient;
4247 }
4248
Selim Cinek29603462015-11-17 19:04:39 -08004249 private void hideLine1Text(RemoteViews result) {
Selim Cinekcc10bfb2016-02-10 16:24:21 -08004250 if (result != null) {
4251 result.setViewVisibility(R.id.text_line_1, View.GONE);
4252 }
Selim Cinek29603462015-11-17 19:04:39 -08004253 }
4254
Selim Cinek6743c0b2017-01-18 18:24:01 -08004255 /**
4256 * Adapt the Notification header if this view is used as an expanded view.
4257 *
4258 * @hide
4259 */
4260 public static void makeHeaderExpanded(RemoteViews result) {
Selim Cinekcc10bfb2016-02-10 16:24:21 -08004261 if (result != null) {
4262 result.setBoolean(R.id.notification_header, "setExpanded", true);
4263 }
Daniel Sandler96fd7c12012-03-30 16:37:36 -04004264 }
4265
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004266 /**
4267 * Construct a RemoteViews for the final heads-up notification layout.
Selim Cinek87ed69b2017-02-09 15:59:43 -08004268 *
4269 * @param increasedHeight true if this layout be created with an increased height. Some
4270 * styles may support showing more then just that basic 1U size
4271 * and the system may decide to render important notifications
4272 * slightly bigger even when collapsed.
4273 *
4274 * @hide
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004275 */
Selim Cinek87ed69b2017-02-09 15:59:43 -08004276 public RemoteViews createHeadsUpContentView(boolean increasedHeight) {
Selim Cinek593610c2016-02-16 18:42:57 -08004277 if (mN.headsUpContentView != null
4278 && (mStyle == null || !mStyle.displayCustomViewInline())) {
Julia Reynolds089e3e42015-11-18 09:59:57 -05004279 return mN.headsUpContentView;
4280 } else if (mStyle != null) {
Selim Cinek87ed69b2017-02-09 15:59:43 -08004281 final RemoteViews styleView = mStyle.makeHeadsUpContentView(increasedHeight);
4282 if (styleView != null) {
4283 return styleView;
4284 }
Julia Reynolds089e3e42015-11-18 09:59:57 -05004285 } else if (mActions.size() == 0) {
4286 return null;
4287 }
4288
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01004289 return applyStandardTemplateWithActions(getBigBaseLayoutResource());
Chris Wren8fd39ec2014-02-27 17:43:26 -05004290 }
4291
Selim Cinek624c02db2015-12-14 21:00:02 -08004292 /**
Selim Cinek87ed69b2017-02-09 15:59:43 -08004293 * Construct a RemoteViews for the final heads-up notification layout.
4294 */
4295 public RemoteViews createHeadsUpContentView() {
4296 return createHeadsUpContentView(false /* useIncreasedHeight */);
4297 }
4298
4299 /**
Selim Cinek624c02db2015-12-14 21:00:02 -08004300 * Construct a RemoteViews for the display in public contexts like on the lockscreen.
4301 *
4302 * @hide
4303 */
4304 public RemoteViews makePublicContentView() {
Adrian Roos1a1ecfc2017-04-17 11:17:59 -07004305 return makePublicView(false /* ambient */);
4306 }
4307
4308 /**
4309 * Construct a RemoteViews for the display in public contexts like on the lockscreen.
4310 *
4311 * @hide
4312 */
4313 public RemoteViews makePublicAmbientNotification() {
4314 return makePublicView(true /* ambient */);
4315 }
4316
4317 private RemoteViews makePublicView(boolean ambient) {
Selim Cinek624c02db2015-12-14 21:00:02 -08004318 if (mN.publicVersion != null) {
4319 final Builder builder = recoverBuilder(mContext, mN.publicVersion);
Adrian Roos1a1ecfc2017-04-17 11:17:59 -07004320 return ambient ? builder.makeAmbientNotification() : builder.createContentView();
Selim Cinek624c02db2015-12-14 21:00:02 -08004321 }
4322 Bundle savedBundle = mN.extras;
4323 Style style = mStyle;
4324 mStyle = null;
4325 Icon largeIcon = mN.mLargeIcon;
4326 mN.mLargeIcon = null;
Selim Cinek279fa862016-06-14 10:57:25 -07004327 Bitmap largeIconLegacy = mN.largeIcon;
4328 mN.largeIcon = null;
Selim Cinek624c02db2015-12-14 21:00:02 -08004329 Bundle publicExtras = new Bundle();
4330 publicExtras.putBoolean(EXTRA_SHOW_WHEN,
4331 savedBundle.getBoolean(EXTRA_SHOW_WHEN));
4332 publicExtras.putBoolean(EXTRA_SHOW_CHRONOMETER,
4333 savedBundle.getBoolean(EXTRA_SHOW_CHRONOMETER));
Adrian Roos96b7e202016-05-17 13:50:38 -07004334 publicExtras.putBoolean(EXTRA_CHRONOMETER_COUNT_DOWN,
4335 savedBundle.getBoolean(EXTRA_CHRONOMETER_COUNT_DOWN));
Selim Cinek624c02db2015-12-14 21:00:02 -08004336 publicExtras.putCharSequence(EXTRA_TITLE,
Adrian Roos1a1ecfc2017-04-17 11:17:59 -07004337 mContext.getString(com.android.internal.R.string.notification_hidden_text));
Selim Cinek624c02db2015-12-14 21:00:02 -08004338 mN.extras = publicExtras;
Adrian Roos1a1ecfc2017-04-17 11:17:59 -07004339 final RemoteViews view = ambient ? makeAmbientNotification()
4340 : applyStandardTemplate(getBaseLayoutResource());
Selim Cinek624c02db2015-12-14 21:00:02 -08004341 mN.extras = savedBundle;
4342 mN.mLargeIcon = largeIcon;
Selim Cinek279fa862016-06-14 10:57:25 -07004343 mN.largeIcon = largeIconLegacy;
Selim Cinek624c02db2015-12-14 21:00:02 -08004344 mStyle = style;
Adrian Roos1a1ecfc2017-04-17 11:17:59 -07004345 return view;
Selim Cinek624c02db2015-12-14 21:00:02 -08004346 }
4347
Selim Cinek6743c0b2017-01-18 18:24:01 -08004348 /**
4349 * Construct a content view for the display when low - priority
4350 *
4351 * @param useRegularSubtext uses the normal subtext set if there is one available. Otherwise
4352 * a new subtext is created consisting of the content of the
4353 * notification.
4354 * @hide
4355 */
4356 public RemoteViews makeLowPriorityContentView(boolean useRegularSubtext) {
4357 int color = mN.color;
4358 mN.color = COLOR_DEFAULT;
4359 CharSequence summary = mN.extras.getCharSequence(EXTRA_SUB_TEXT);
4360 if (!useRegularSubtext || TextUtils.isEmpty(summary)) {
4361 CharSequence newSummary = createSummaryText();
4362 if (!TextUtils.isEmpty(newSummary)) {
4363 mN.extras.putCharSequence(EXTRA_SUB_TEXT, newSummary);
4364 }
4365 }
Selim Cinek875ba9b2017-02-13 16:20:17 -08004366
Selim Cinek6743c0b2017-01-18 18:24:01 -08004367 RemoteViews header = makeNotificationHeader();
Selim Cinek1b554392017-02-28 17:22:49 -08004368 header.setBoolean(R.id.notification_header, "setAcceptAllTouches", true);
Selim Cinek6743c0b2017-01-18 18:24:01 -08004369 if (summary != null) {
4370 mN.extras.putCharSequence(EXTRA_SUB_TEXT, summary);
4371 } else {
4372 mN.extras.remove(EXTRA_SUB_TEXT);
4373 }
4374 mN.color = color;
4375 return header;
4376 }
Selim Cinek624c02db2015-12-14 21:00:02 -08004377
Selim Cinek6743c0b2017-01-18 18:24:01 -08004378 private CharSequence createSummaryText() {
4379 CharSequence titleText = mN.extras.getCharSequence(Notification.EXTRA_TITLE);
4380 if (USE_ONLY_TITLE_IN_LOW_PRIORITY_SUMMARY) {
4381 return titleText;
4382 }
4383 SpannableStringBuilder summary = new SpannableStringBuilder();
4384 if (titleText == null) {
4385 titleText = mN.extras.getCharSequence(Notification.EXTRA_TITLE_BIG);
4386 }
4387 BidiFormatter bidi = BidiFormatter.getInstance();
4388 if (titleText != null) {
4389 summary.append(bidi.unicodeWrap(titleText));
4390 }
4391 CharSequence contentText = mN.extras.getCharSequence(Notification.EXTRA_TEXT);
4392 if (titleText != null && contentText != null) {
4393 summary.append(bidi.unicodeWrap(mContext.getText(
4394 R.string.notification_header_divider_symbol_with_spaces)));
4395 }
4396 if (contentText != null) {
4397 summary.append(bidi.unicodeWrap(contentText));
4398 }
4399 return summary;
4400 }
Chris Wren8fd39ec2014-02-27 17:43:26 -05004401
Selim Cinek06e9e1f2016-07-08 17:14:16 -07004402 private RemoteViews generateActionButton(Action action, boolean emphazisedMode,
Adrian Roos487374f2017-01-11 15:48:14 -08004403 boolean oddAction, boolean ambient) {
Daniel Sandler8680bf82012-05-15 16:52:52 -04004404 final boolean tombstone = (action.actionIntent == null);
Selim Cinekf33b1112015-07-15 17:45:11 -07004405 RemoteViews button = new BuilderRemoteViews(mContext.getApplicationInfo(),
Selim Cinek06e9e1f2016-07-08 17:14:16 -07004406 emphazisedMode ? getEmphasizedActionLayoutResource()
4407 : tombstone ? getActionTombstoneLayoutResource()
4408 : getActionLayoutResource());
Daniel Sandler8680bf82012-05-15 16:52:52 -04004409 if (!tombstone) {
Daniel Sandlere5518842012-05-10 16:20:40 -04004410 button.setOnClickPendingIntent(R.id.action0, action.actionIntent);
Daniel Sandlere5518842012-05-10 16:20:40 -04004411 }
Daniel Sandler96fd7c12012-03-30 16:37:36 -04004412 button.setContentDescription(R.id.action0, action.title);
Adrian Roosfe84e1f2015-11-04 15:55:39 -08004413 if (action.mRemoteInputs != null) {
4414 button.setRemoteInputs(R.id.action0, action.mRemoteInputs);
4415 }
Selim Cinek7b9605b2017-01-19 17:36:00 -08004416 // TODO: handle emphasized mode / actions right
Selim Cinek06e9e1f2016-07-08 17:14:16 -07004417 if (emphazisedMode) {
Selim Cinek981962e2016-07-20 20:41:58 -07004418 // change the background bgColor
Selim Cinek622c64a2017-04-17 17:10:05 -07004419 int bgColor;
4420 if (isColorized()) {
4421 bgColor = oddAction ? getActionBarColor() : getActionBarColorDeEmphasized();
4422 } else {
4423 bgColor = mContext.getColor(oddAction ? R.color.notification_action_list
4424 : R.color.notification_action_list_dark);
4425 }
Selim Cinek981962e2016-07-20 20:41:58 -07004426 button.setDrawableParameters(R.id.button_holder, true, -1, bgColor,
Selim Cinek06e9e1f2016-07-08 17:14:16 -07004427 PorterDuff.Mode.SRC_ATOP, -1);
Selim Cinek981962e2016-07-20 20:41:58 -07004428 CharSequence title = action.title;
4429 ColorStateList[] outResultColor = null;
4430 if (isLegacy()) {
4431 title = clearColorSpans(title);
4432 } else {
4433 outResultColor = new ColorStateList[1];
4434 title = ensureColorSpanContrast(title, bgColor, outResultColor);
4435 }
4436 button.setTextViewText(R.id.action0, title);
Selim Cinek7b9605b2017-01-19 17:36:00 -08004437 setTextViewColorPrimary(button, R.id.action0);
Selim Cinek981962e2016-07-20 20:41:58 -07004438 if (outResultColor != null && outResultColor[0] != null) {
4439 // We need to set the text color as well since changing a text to uppercase
4440 // clears its spans.
4441 button.setTextColor(R.id.action0, outResultColor[0]);
Selim Cinek7b9605b2017-01-19 17:36:00 -08004442 } else if (mN.color != COLOR_DEFAULT && !isColorized()) {
Selim Cinek981962e2016-07-20 20:41:58 -07004443 button.setTextColor(R.id.action0,resolveContrastColor());
4444 }
Selim Cinek06e9e1f2016-07-08 17:14:16 -07004445 } else {
Selim Cinek981962e2016-07-20 20:41:58 -07004446 button.setTextViewText(R.id.action0, processLegacyText(action.title));
Adrian Roos72171622017-01-27 10:32:06 -08004447 if (isColorized() && !ambient) {
Selim Cinek7b9605b2017-01-19 17:36:00 -08004448 setTextViewColorPrimary(button, R.id.action0);
4449 } else if (mN.color != COLOR_DEFAULT) {
Adrian Roos487374f2017-01-11 15:48:14 -08004450 button.setTextColor(R.id.action0,
4451 ambient ? resolveAmbientColor() : resolveContrastColor());
Selim Cinek06e9e1f2016-07-08 17:14:16 -07004452 }
Adrian Roosfe84e1f2015-11-04 15:55:39 -08004453 }
Daniel Sandler96fd7c12012-03-30 16:37:36 -04004454 return button;
4455 }
4456
Joe Onoratocb109a02011-01-18 17:57:41 -08004457 /**
Selim Cinek981962e2016-07-20 20:41:58 -07004458 * Clears all color spans of a text
4459 * @param charSequence the input text
4460 * @return the same text but without color spans
4461 */
4462 private CharSequence clearColorSpans(CharSequence charSequence) {
4463 if (charSequence instanceof Spanned) {
4464 Spanned ss = (Spanned) charSequence;
4465 Object[] spans = ss.getSpans(0, ss.length(), Object.class);
4466 SpannableStringBuilder builder = new SpannableStringBuilder(ss.toString());
4467 for (Object span : spans) {
4468 Object resultSpan = span;
4469 if (resultSpan instanceof CharacterStyle) {
4470 resultSpan = ((CharacterStyle) span).getUnderlying();
4471 }
4472 if (resultSpan instanceof TextAppearanceSpan) {
4473 TextAppearanceSpan originalSpan = (TextAppearanceSpan) resultSpan;
4474 if (originalSpan.getTextColor() != null) {
4475 resultSpan = new TextAppearanceSpan(
4476 originalSpan.getFamily(),
4477 originalSpan.getTextStyle(),
4478 originalSpan.getTextSize(),
4479 null,
4480 originalSpan.getLinkTextColor());
4481 }
4482 } else if (resultSpan instanceof ForegroundColorSpan
4483 || (resultSpan instanceof BackgroundColorSpan)) {
4484 continue;
4485 } else {
4486 resultSpan = span;
4487 }
4488 builder.setSpan(resultSpan, ss.getSpanStart(span), ss.getSpanEnd(span),
4489 ss.getSpanFlags(span));
4490 }
4491 return builder;
4492 }
4493 return charSequence;
4494 }
4495
4496 /**
4497 * Ensures contrast on color spans against a background color. also returns the color of the
4498 * text if a span was found that spans over the whole text.
4499 *
4500 * @param charSequence the charSequence on which the spans are
4501 * @param background the background color to ensure the contrast against
4502 * @param outResultColor an array in which a color will be returned as the first element if
4503 * there exists a full length color span.
4504 * @return the contrasted charSequence
4505 */
4506 private CharSequence ensureColorSpanContrast(CharSequence charSequence, int background,
4507 ColorStateList[] outResultColor) {
4508 if (charSequence instanceof Spanned) {
4509 Spanned ss = (Spanned) charSequence;
4510 Object[] spans = ss.getSpans(0, ss.length(), Object.class);
4511 SpannableStringBuilder builder = new SpannableStringBuilder(ss.toString());
4512 for (Object span : spans) {
4513 Object resultSpan = span;
4514 int spanStart = ss.getSpanStart(span);
4515 int spanEnd = ss.getSpanEnd(span);
4516 boolean fullLength = (spanEnd - spanStart) == charSequence.length();
4517 if (resultSpan instanceof CharacterStyle) {
4518 resultSpan = ((CharacterStyle) span).getUnderlying();
4519 }
4520 if (resultSpan instanceof TextAppearanceSpan) {
4521 TextAppearanceSpan originalSpan = (TextAppearanceSpan) resultSpan;
4522 ColorStateList textColor = originalSpan.getTextColor();
4523 if (textColor != null) {
4524 int[] colors = textColor.getColors();
4525 int[] newColors = new int[colors.length];
4526 for (int i = 0; i < newColors.length; i++) {
4527 newColors[i] = NotificationColorUtil.ensureLargeTextContrast(
4528 colors[i], background);
4529 }
4530 textColor = new ColorStateList(textColor.getStates().clone(),
4531 newColors);
4532 resultSpan = new TextAppearanceSpan(
4533 originalSpan.getFamily(),
4534 originalSpan.getTextStyle(),
4535 originalSpan.getTextSize(),
4536 textColor,
4537 originalSpan.getLinkTextColor());
4538 if (fullLength) {
4539 outResultColor[0] = new ColorStateList(
4540 textColor.getStates().clone(), newColors);
4541 }
4542 }
4543 } else if (resultSpan instanceof ForegroundColorSpan) {
4544 ForegroundColorSpan originalSpan = (ForegroundColorSpan) resultSpan;
4545 int foregroundColor = originalSpan.getForegroundColor();
4546 foregroundColor = NotificationColorUtil.ensureLargeTextContrast(
4547 foregroundColor, background);
4548 resultSpan = new ForegroundColorSpan(foregroundColor);
4549 if (fullLength) {
4550 outResultColor[0] = ColorStateList.valueOf(foregroundColor);
4551 }
4552 } else {
4553 resultSpan = span;
4554 }
4555
4556 builder.setSpan(resultSpan, spanStart, spanEnd, ss.getSpanFlags(span));
4557 }
4558 return builder;
4559 }
4560 return charSequence;
4561 }
4562
4563 /**
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01004564 * @return Whether we are currently building a notification from a legacy (an app that
Alan Viverette3cb07a462014-06-06 14:19:53 -07004565 * doesn't create material notifications by itself) app.
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01004566 */
4567 private boolean isLegacy() {
Selim Cinek7b9605b2017-01-19 17:36:00 -08004568 if (!mIsLegacyInitialized) {
4569 mIsLegacy = mContext.getApplicationInfo().targetSdkVersion
4570 < Build.VERSION_CODES.LOLLIPOP;
4571 mIsLegacyInitialized = true;
4572 }
4573 return mIsLegacy;
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01004574 }
4575
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01004576 private CharSequence processLegacyText(CharSequence charSequence) {
Adrian Roos0bc3f6a2017-03-06 11:54:05 -08004577 return processLegacyText(charSequence, false /* ambient */);
4578 }
4579
4580 private CharSequence processLegacyText(CharSequence charSequence, boolean ambient) {
4581 boolean isAlreadyLightText = isLegacy() || textColorsNeedInversion();
4582 boolean wantLightText = ambient;
4583 if (isAlreadyLightText != wantLightText) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004584 return getColorUtil().invertCharSequenceColors(charSequence);
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01004585 } else {
4586 return charSequence;
4587 }
4588 }
4589
Dan Sandler26e81cf2014-05-06 10:01:27 -04004590 /**
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004591 * Apply any necessariy colors to the small icon
Dan Sandler26e81cf2014-05-06 10:01:27 -04004592 */
Adrian Roos487374f2017-01-11 15:48:14 -08004593 private void processSmallIconColor(Icon smallIcon, RemoteViews contentView,
4594 boolean ambient) {
Selim Cinekea4bef72015-12-02 15:51:10 -08004595 boolean colorable = !isLegacy() || getColorUtil().isGrayscaleIcon(mContext, smallIcon);
Selim Cinek99104832017-01-25 14:47:33 -08004596 int color = ambient ? resolveAmbientColor() : getPrimaryHighlightColor();
Selim Cinekea4bef72015-12-02 15:51:10 -08004597 if (colorable) {
Adrian Roos487374f2017-01-11 15:48:14 -08004598 contentView.setDrawableParameters(R.id.icon, false, -1, color,
Jorim Jaggi92df1f22014-12-16 19:44:41 +01004599 PorterDuff.Mode.SRC_ATOP, -1);
Selim Cinekea4bef72015-12-02 15:51:10 -08004600
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01004601 }
Selim Cinekea4bef72015-12-02 15:51:10 -08004602 contentView.setInt(R.id.notification_header, "setOriginalIconColor",
Adrian Roos487374f2017-01-11 15:48:14 -08004603 colorable ? color : NotificationHeaderView.NO_COLOR);
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01004604 }
4605
Dan Sandler26e81cf2014-05-06 10:01:27 -04004606 /**
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004607 * Make the largeIcon dark if it's a fake smallIcon (that is,
Dan Sandler26e81cf2014-05-06 10:01:27 -04004608 * if it's grayscale).
4609 */
4610 // TODO: also check bounds, transparency, that sort of thing.
Dan Sandlerd63f9322015-05-06 15:18:49 -04004611 private void processLargeLegacyIcon(Icon largeIcon, RemoteViews contentView) {
4612 if (largeIcon != null && isLegacy()
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004613 && getColorUtil().isGrayscaleIcon(mContext, largeIcon)) {
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004614 // resolve color will fall back to the default when legacy
Adrian Roos4ff3b122016-02-01 12:26:13 -08004615 contentView.setDrawableParameters(R.id.icon, false, -1, resolveContrastColor(),
Dan Sandler190d58d2014-05-15 09:33:39 -04004616 PorterDuff.Mode.SRC_ATOP, -1);
Jorim Jaggi92df1f22014-12-16 19:44:41 +01004617 }
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01004618 }
4619
Julia Reynolds10ee1fc2015-11-09 11:04:55 -05004620 private void sanitizeColor() {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004621 if (mN.color != COLOR_DEFAULT) {
4622 mN.color |= 0xFF000000; // no alpha for custom colors
Jorim Jaggi74419312014-06-10 20:57:21 +02004623 }
Jorim Jaggi74419312014-06-10 20:57:21 +02004624 }
4625
Adrian Roos4ff3b122016-02-01 12:26:13 -08004626 int resolveContrastColor() {
4627 if (mCachedContrastColorIsFor == mN.color && mCachedContrastColor != COLOR_INVALID) {
4628 return mCachedContrastColor;
Dan Sandler26e81cf2014-05-06 10:01:27 -04004629 }
Adrian Roos4ff3b122016-02-01 12:26:13 -08004630 final int contrasted = NotificationColorUtil.resolveContrastColor(mContext, mN.color);
4631
4632 mCachedContrastColorIsFor = mN.color;
4633 return mCachedContrastColor = contrasted;
Dan Sandler26e81cf2014-05-06 10:01:27 -04004634 }
4635
Adrian Roos487374f2017-01-11 15:48:14 -08004636 int resolveAmbientColor() {
4637 if (mCachedAmbientColorIsFor == mN.color && mCachedAmbientColorIsFor != COLOR_INVALID) {
4638 return mCachedAmbientColor;
4639 }
4640 final int contrasted = NotificationColorUtil.resolveAmbientColor(mContext, mN.color);
4641
4642 mCachedAmbientColorIsFor = mN.color;
4643 return mCachedAmbientColor = contrasted;
4644 }
4645
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01004646 /**
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004647 * Apply the unstyled operations and return a new {@link Notification} object.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04004648 * @hide
Joe Onoratocb109a02011-01-18 17:57:41 -08004649 */
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04004650 public Notification buildUnstyled() {
Daniel Sandlera0a938c2012-03-15 08:42:37 -04004651 if (mActions.size() > 0) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004652 mN.actions = new Action[mActions.size()];
4653 mActions.toArray(mN.actions);
Daniel Sandlera0a938c2012-03-15 08:42:37 -04004654 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004655 if (!mPersonList.isEmpty()) {
4656 mN.extras.putStringArray(EXTRA_PEOPLE,
4657 mPersonList.toArray(new String[mPersonList.size()]));
Dan Sandler0bf2ed82013-12-21 23:33:41 -06004658 }
Selim Cinek247fa012016-02-18 09:50:48 -08004659 if (mN.bigContentView != null || mN.contentView != null
4660 || mN.headsUpContentView != null) {
4661 mN.extras.putBoolean(EXTRA_CONTAINS_CUSTOM_VIEW, true);
4662 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004663 return mN;
Joe Onorato46439ce2010-11-19 13:56:21 -08004664 }
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004665
Julia Reynolds3b848122016-02-26 10:45:32 -05004666 /**
4667 * Creates a Builder from an existing notification so further changes can be made.
4668 * @param context The context for your application / activity.
4669 * @param n The notification to create a Builder from.
4670 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004671 public static Notification.Builder recoverBuilder(Context context, Notification n) {
Christoph Studer4600f9b2014-07-22 22:44:43 +02004672 // Re-create notification context so we can access app resources.
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004673 ApplicationInfo applicationInfo = n.extras.getParcelable(
4674 EXTRA_BUILDER_APPLICATION_INFO);
Christoph Studer4600f9b2014-07-22 22:44:43 +02004675 Context builderContext;
Julia Reynoldsda303542015-11-23 14:00:20 -05004676 if (applicationInfo != null) {
4677 try {
4678 builderContext = context.createApplicationContext(applicationInfo,
4679 Context.CONTEXT_RESTRICTED);
4680 } catch (NameNotFoundException e) {
4681 Log.e(TAG, "ApplicationInfo " + applicationInfo + " not found");
4682 builderContext = context; // try with our context
4683 }
4684 } else {
4685 builderContext = context; // try with given context
Christoph Studer4600f9b2014-07-22 22:44:43 +02004686 }
4687
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004688 return new Builder(builderContext, n);
Christoph Studer4600f9b2014-07-22 22:44:43 +02004689 }
4690
Daniel Sandlerf45564e2013-04-15 15:05:08 -04004691 /**
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004692 * @deprecated Use {@link #build()} instead.
4693 */
4694 @Deprecated
4695 public Notification getNotification() {
4696 return build();
4697 }
4698
4699 /**
4700 * Combine all of the options that have been set and return a new {@link Notification}
4701 * object.
4702 */
4703 public Notification build() {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004704 // first, add any extras from the calling code
4705 if (mUserExtras != null) {
4706 mN.extras = getAllExtras();
Jorim Jaggia0d58ae2015-06-03 11:48:13 -07004707 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004708
Selim Cinekb85f36fd2016-04-20 18:46:36 -07004709 mN.creationTime = System.currentTimeMillis();
4710
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004711 // lazy stuff from mContext; see comment in Builder(Context, Notification)
Julia Reynoldsda303542015-11-23 14:00:20 -05004712 Notification.addFieldsFromContext(mContext, mN);
Christoph Studer943aa672014-08-03 20:31:16 +02004713
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004714 buildUnstyled();
Daniel Sandlerf45564e2013-04-15 15:05:08 -04004715
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004716 if (mStyle != null) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004717 mStyle.buildStyled(mN);
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004718 }
Daniel Sandlerf45564e2013-04-15 15:05:08 -04004719
Adrian Roos5081c0d2016-02-26 16:04:19 -08004720 if (mContext.getApplicationInfo().targetSdkVersion < Build.VERSION_CODES.N
4721 && (mStyle == null || !mStyle.displayCustomViewInline())) {
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05004722 if (mN.contentView == null) {
Julia Reynolds3b848122016-02-26 10:45:32 -05004723 mN.contentView = createContentView();
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05004724 mN.extras.putInt(EXTRA_REBUILD_CONTENT_VIEW_ACTION_COUNT,
4725 mN.contentView.getSequenceNumber());
4726 }
4727 if (mN.bigContentView == null) {
Julia Reynolds3b848122016-02-26 10:45:32 -05004728 mN.bigContentView = createBigContentView();
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05004729 if (mN.bigContentView != null) {
4730 mN.extras.putInt(EXTRA_REBUILD_BIG_CONTENT_VIEW_ACTION_COUNT,
4731 mN.bigContentView.getSequenceNumber());
4732 }
4733 }
4734 if (mN.headsUpContentView == null) {
Julia Reynolds3b848122016-02-26 10:45:32 -05004735 mN.headsUpContentView = createHeadsUpContentView();
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05004736 if (mN.headsUpContentView != null) {
4737 mN.extras.putInt(EXTRA_REBUILD_HEADS_UP_CONTENT_VIEW_ACTION_COUNT,
4738 mN.headsUpContentView.getSequenceNumber());
4739 }
4740 }
4741 }
4742
Julia Reynolds4c0c2022016-02-02 15:11:59 -05004743 if ((mN.defaults & DEFAULT_LIGHTS) != 0) {
4744 mN.flags |= FLAG_SHOW_LIGHTS;
4745 }
4746
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004747 return mN;
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004748 }
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05004749
4750 /**
4751 * Apply this Builder to an existing {@link Notification} object.
4752 *
4753 * @hide
4754 */
4755 public Notification buildInto(Notification n) {
Daniel Sandler1a497d32013-04-18 14:52:45 -04004756 build().cloneInto(n, true);
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05004757 return n;
4758 }
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01004759
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05004760 /**
Adrian Roos184bfe022016-03-03 13:41:44 -08004761 * Removes RemoteViews that were created for compatibility from {@param n}, if they did not
4762 * change.
4763 *
4764 * @return {@param n}, if no stripping is needed, otherwise a stripped clone of {@param n}.
4765 *
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05004766 * @hide
4767 */
Adrian Roos184bfe022016-03-03 13:41:44 -08004768 public static Notification maybeCloneStrippedForDelivery(Notification n) {
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05004769 String templateClass = n.extras.getString(EXTRA_TEMPLATE);
Adrian Roos184bfe022016-03-03 13:41:44 -08004770
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05004771 // Only strip views for known Styles because we won't know how to
4772 // re-create them otherwise.
Adrian Roos184bfe022016-03-03 13:41:44 -08004773 if (!TextUtils.isEmpty(templateClass)
4774 && getNotificationStyleClass(templateClass) == null) {
4775 return n;
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05004776 }
Adrian Roos184bfe022016-03-03 13:41:44 -08004777
4778 // Only strip unmodified BuilderRemoteViews.
4779 boolean stripContentView = n.contentView instanceof BuilderRemoteViews &&
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05004780 n.extras.getInt(EXTRA_REBUILD_CONTENT_VIEW_ACTION_COUNT, -1) ==
Adrian Roos184bfe022016-03-03 13:41:44 -08004781 n.contentView.getSequenceNumber();
4782 boolean stripBigContentView = n.bigContentView instanceof BuilderRemoteViews &&
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05004783 n.extras.getInt(EXTRA_REBUILD_BIG_CONTENT_VIEW_ACTION_COUNT, -1) ==
Adrian Roos184bfe022016-03-03 13:41:44 -08004784 n.bigContentView.getSequenceNumber();
4785 boolean stripHeadsUpContentView = n.headsUpContentView instanceof BuilderRemoteViews &&
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05004786 n.extras.getInt(EXTRA_REBUILD_HEADS_UP_CONTENT_VIEW_ACTION_COUNT, -1) ==
Adrian Roos184bfe022016-03-03 13:41:44 -08004787 n.headsUpContentView.getSequenceNumber();
4788
4789 // Nothing to do here, no need to clone.
4790 if (!stripContentView && !stripBigContentView && !stripHeadsUpContentView) {
4791 return n;
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05004792 }
Adrian Roos184bfe022016-03-03 13:41:44 -08004793
4794 Notification clone = n.clone();
4795 if (stripContentView) {
4796 clone.contentView = null;
4797 clone.extras.remove(EXTRA_REBUILD_CONTENT_VIEW_ACTION_COUNT);
4798 }
4799 if (stripBigContentView) {
4800 clone.bigContentView = null;
4801 clone.extras.remove(EXTRA_REBUILD_BIG_CONTENT_VIEW_ACTION_COUNT);
4802 }
4803 if (stripHeadsUpContentView) {
4804 clone.headsUpContentView = null;
4805 clone.extras.remove(EXTRA_REBUILD_HEADS_UP_CONTENT_VIEW_ACTION_COUNT);
4806 }
4807 return clone;
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05004808 }
4809
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01004810 private int getBaseLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07004811 return R.layout.notification_template_material_base;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01004812 }
4813
4814 private int getBigBaseLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07004815 return R.layout.notification_template_material_big_base;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01004816 }
4817
4818 private int getBigPictureLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07004819 return R.layout.notification_template_material_big_picture;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01004820 }
4821
4822 private int getBigTextLayoutResource() {
Jorim Jaggi445d3c02014-08-19 22:33:42 +02004823 return R.layout.notification_template_material_big_text;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01004824 }
4825
4826 private int getInboxLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07004827 return R.layout.notification_template_material_inbox;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01004828 }
4829
Adrian Roosc1a80b02016-04-05 14:54:55 -07004830 private int getMessagingLayoutResource() {
4831 return R.layout.notification_template_material_messaging;
4832 }
4833
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01004834 private int getActionLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07004835 return R.layout.notification_material_action;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01004836 }
4837
Selim Cinek06e9e1f2016-07-08 17:14:16 -07004838 private int getEmphasizedActionLayoutResource() {
4839 return R.layout.notification_material_action_emphasized;
4840 }
4841
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01004842 private int getActionTombstoneLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07004843 return R.layout.notification_material_action_tombstone;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01004844 }
Selim Cinek7b9605b2017-01-19 17:36:00 -08004845
4846 private int getBackgroundColor() {
4847 if (isColorized()) {
4848 return mN.color;
4849 } else {
4850 return COLOR_DEFAULT;
4851 }
4852 }
4853
4854 private boolean isColorized() {
4855 return mN.isColorized();
4856 }
Selim Cinek99104832017-01-25 14:47:33 -08004857
4858 private boolean textColorsNeedInversion() {
4859 if (mStyle == null || !MediaStyle.class.equals(mStyle.getClass())) {
4860 return false;
4861 }
4862 int targetSdkVersion = mContext.getApplicationInfo().targetSdkVersion;
4863 return targetSdkVersion > Build.VERSION_CODES.M
4864 && targetSdkVersion < Build.VERSION_CODES.O;
4865 }
Selim Cinek7b9605b2017-01-19 17:36:00 -08004866 }
4867
4868 /**
Selim Cinek22714f12017-04-13 16:23:53 -07004869 * @return whether this notification is a foreground service notification
Selim Cinek7b9605b2017-01-19 17:36:00 -08004870 */
Selim Cinek22714f12017-04-13 16:23:53 -07004871 private boolean isForegroundService() {
4872 return (flags & Notification.FLAG_FOREGROUND_SERVICE) != 0;
Selim Cinek7b9605b2017-01-19 17:36:00 -08004873 }
4874
4875 /**
Selim Cinek99104832017-01-25 14:47:33 -08004876 * @return whether this notification has a media session attached
4877 * @hide
4878 */
4879 public boolean hasMediaSession() {
4880 return extras.getParcelable(Notification.EXTRA_MEDIA_SESSION) != null;
4881 }
4882
4883 /**
Selim Cinek7d1009b2017-01-25 15:28:28 -08004884 * @return the style class of this notification
4885 * @hide
4886 */
4887 public Class<? extends Notification.Style> getNotificationStyle() {
4888 String templateClass = extras.getString(Notification.EXTRA_TEMPLATE);
4889
4890 if (!TextUtils.isEmpty(templateClass)) {
4891 return Notification.getNotificationStyleClass(templateClass);
4892 }
4893 return null;
4894 }
4895
4896 /**
Selim Cinek22714f12017-04-13 16:23:53 -07004897 * @return true if this notification is colorized.
Selim Cinek7b9605b2017-01-19 17:36:00 -08004898 *
4899 * @hide
4900 */
4901 public boolean isColorized() {
Selim Cinek99104832017-01-25 14:47:33 -08004902 Class<? extends Style> style = getNotificationStyle();
4903 if (MediaStyle.class.equals(style)) {
4904 Boolean colorized = (Boolean) extras.get(EXTRA_COLORIZED);
4905 if ((colorized == null || colorized) && hasMediaSession()) {
4906 return true;
4907 }
4908 } else if (DecoratedMediaCustomViewStyle.class.equals(style)) {
4909 if (extras.getBoolean(EXTRA_COLORIZED) && hasMediaSession()) {
4910 return true;
4911 }
4912 }
Selim Cinek22714f12017-04-13 16:23:53 -07004913 return extras.getBoolean(EXTRA_COLORIZED) && isForegroundService();
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004914 }
4915
Selim Cinek279fa862016-06-14 10:57:25 -07004916 private boolean hasLargeIcon() {
4917 return mLargeIcon != null || largeIcon != null;
4918 }
4919
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004920 /**
Selim Cinekc2c0b042016-05-18 17:13:46 -07004921 * @return true if the notification will show the time; false otherwise
Selim Cinekb85f36fd2016-04-20 18:46:36 -07004922 * @hide
4923 */
Selim Cinekc2c0b042016-05-18 17:13:46 -07004924 public boolean showsTime() {
Selim Cinekb85f36fd2016-04-20 18:46:36 -07004925 return when != 0 && extras.getBoolean(EXTRA_SHOW_WHEN);
4926 }
4927
4928 /**
Selim Cinekc2c0b042016-05-18 17:13:46 -07004929 * @return true if the notification will show a chronometer; false otherwise
4930 * @hide
4931 */
4932 public boolean showsChronometer() {
4933 return when != 0 && extras.getBoolean(EXTRA_SHOW_CHRONOMETER);
4934 }
4935
4936 /**
Julia Reynolds4a02afb2016-12-13 13:39:52 -05004937 * @hide
4938 */
4939 @SystemApi
4940 public static Class<? extends Style> getNotificationStyleClass(String templateClass) {
4941 Class<? extends Style>[] classes = new Class[] {
4942 BigTextStyle.class, BigPictureStyle.class, InboxStyle.class, MediaStyle.class,
4943 DecoratedCustomViewStyle.class, DecoratedMediaCustomViewStyle.class,
4944 MessagingStyle.class };
4945 for (Class<? extends Style> innerClass : classes) {
4946 if (templateClass.equals(innerClass.getName())) {
4947 return innerClass;
4948 }
4949 }
4950 return null;
4951 }
4952
4953 /**
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004954 * An object that can apply a rich notification style to a {@link Notification.Builder}
4955 * object.
4956 */
Griff Hazendfcb0802014-02-11 12:00:00 -08004957 public static abstract class Style {
Chris Wrend6297db2012-05-03 16:20:13 -04004958 private CharSequence mBigContentTitle;
Jorim Jaggi457a10d2014-09-08 16:18:23 +02004959
4960 /**
4961 * @hide
4962 */
4963 protected CharSequence mSummaryText = null;
4964
4965 /**
4966 * @hide
4967 */
4968 protected boolean mSummaryTextSet = false;
Chris Wrend6297db2012-05-03 16:20:13 -04004969
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004970 protected Builder mBuilder;
4971
Chris Wrend6297db2012-05-03 16:20:13 -04004972 /**
4973 * Overrides ContentTitle in the big form of the template.
4974 * This defaults to the value passed to setContentTitle().
4975 */
4976 protected void internalSetBigContentTitle(CharSequence title) {
4977 mBigContentTitle = title;
4978 }
4979
4980 /**
4981 * Set the first line of text after the detail section in the big form of the template.
4982 */
4983 protected void internalSetSummaryText(CharSequence cs) {
4984 mSummaryText = cs;
Daniel Sandler619738c2012-06-07 16:33:08 -04004985 mSummaryTextSet = true;
Chris Wrend6297db2012-05-03 16:20:13 -04004986 }
4987
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004988 public void setBuilder(Builder builder) {
4989 if (mBuilder != builder) {
4990 mBuilder = builder;
Daniel Sandlerc08dea22012-06-28 08:35:24 -07004991 if (mBuilder != null) {
4992 mBuilder.setStyle(this);
4993 }
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004994 }
4995 }
4996
Chris Wrend6297db2012-05-03 16:20:13 -04004997 protected void checkBuilder() {
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004998 if (mBuilder == null) {
4999 throw new IllegalArgumentException("Style requires a valid Builder object");
5000 }
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005001 }
Chris Wrend6297db2012-05-03 16:20:13 -04005002
5003 protected RemoteViews getStandardView(int layoutId) {
5004 checkBuilder();
5005
Christoph Studer4600f9b2014-07-22 22:44:43 +02005006 // Nasty.
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005007 CharSequence oldBuilderContentTitle =
5008 mBuilder.getAllExtras().getCharSequence(EXTRA_TITLE);
Chris Wrend6297db2012-05-03 16:20:13 -04005009 if (mBigContentTitle != null) {
5010 mBuilder.setContentTitle(mBigContentTitle);
5011 }
5012
Chris Wrend6297db2012-05-03 16:20:13 -04005013 RemoteViews contentView = mBuilder.applyStandardTemplateWithActions(layoutId);
5014
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005015 mBuilder.getAllExtras().putCharSequence(EXTRA_TITLE, oldBuilderContentTitle);
Christoph Studer4600f9b2014-07-22 22:44:43 +02005016
Chris Wrend6297db2012-05-03 16:20:13 -04005017 if (mBigContentTitle != null && mBigContentTitle.equals("")) {
5018 contentView.setViewVisibility(R.id.line1, View.GONE);
Chris Wren67dc9a02012-05-16 01:03:20 -04005019 } else {
5020 contentView.setViewVisibility(R.id.line1, View.VISIBLE);
Chris Wrend6297db2012-05-03 16:20:13 -04005021 }
5022
Chris Wrend6297db2012-05-03 16:20:13 -04005023 return contentView;
5024 }
5025
Daniel Sandlerf45564e2013-04-15 15:05:08 -04005026 /**
Selim Cinek7d1009b2017-01-25 15:28:28 -08005027 * Construct a Style-specific RemoteViews for the collapsed notification layout.
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005028 * The default implementation has nothing additional to add.
Selim Cinek7d1009b2017-01-25 15:28:28 -08005029 *
5030 * @param increasedHeight true if this layout be created with an increased height.
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005031 * @hide
5032 */
Selim Cinek7d1009b2017-01-25 15:28:28 -08005033 public RemoteViews makeContentView(boolean increasedHeight) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005034 return null;
5035 }
5036
5037 /**
5038 * Construct a Style-specific RemoteViews for the final big notification layout.
5039 * @hide
5040 */
5041 public RemoteViews makeBigContentView() {
5042 return null;
5043 }
5044
5045 /**
5046 * Construct a Style-specific RemoteViews for the final HUN layout.
Selim Cinek87ed69b2017-02-09 15:59:43 -08005047 *
5048 * @param increasedHeight true if this layout be created with an increased height.
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005049 * @hide
5050 */
Selim Cinek87ed69b2017-02-09 15:59:43 -08005051 public RemoteViews makeHeadsUpContentView(boolean increasedHeight) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005052 return null;
5053 }
5054
5055 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005056 * Apply any style-specific extras to this notification before shipping it out.
Daniel Sandlerf45564e2013-04-15 15:05:08 -04005057 * @hide
5058 */
5059 public void addExtras(Bundle extras) {
5060 if (mSummaryTextSet) {
5061 extras.putCharSequence(EXTRA_SUMMARY_TEXT, mSummaryText);
5062 }
5063 if (mBigContentTitle != null) {
5064 extras.putCharSequence(EXTRA_TITLE_BIG, mBigContentTitle);
5065 }
Chris Wren91ad5632013-06-05 15:05:57 -04005066 extras.putString(EXTRA_TEMPLATE, this.getClass().getName());
Daniel Sandlerf45564e2013-04-15 15:05:08 -04005067 }
5068
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04005069 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005070 * Reconstruct the internal state of this Style object from extras.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04005071 * @hide
5072 */
Christoph Studer4600f9b2014-07-22 22:44:43 +02005073 protected void restoreFromExtras(Bundle extras) {
5074 if (extras.containsKey(EXTRA_SUMMARY_TEXT)) {
5075 mSummaryText = extras.getCharSequence(EXTRA_SUMMARY_TEXT);
5076 mSummaryTextSet = true;
5077 }
5078 if (extras.containsKey(EXTRA_TITLE_BIG)) {
5079 mBigContentTitle = extras.getCharSequence(EXTRA_TITLE_BIG);
5080 }
5081 }
5082
5083
5084 /**
5085 * @hide
5086 */
5087 public Notification buildStyled(Notification wip) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005088 addExtras(wip.extras);
Christoph Studer4600f9b2014-07-22 22:44:43 +02005089 return wip;
5090 }
5091
Daniel Sandler0ec46202015-06-24 01:27:05 -04005092 /**
5093 * @hide
5094 */
Jorim Jaggia0d58ae2015-06-03 11:48:13 -07005095 public void purgeResources() {}
5096
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04005097 /**
5098 * Calls {@link android.app.Notification.Builder#build()} on the Builder this Style is
5099 * attached to.
5100 *
5101 * @return the fully constructed Notification.
5102 */
5103 public Notification build() {
5104 checkBuilder();
5105 return mBuilder.build();
5106 }
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02005107
5108 /**
5109 * @hide
5110 * @return true if the style positions the progress bar on the second line; false if the
5111 * style hides the progress bar
5112 */
5113 protected boolean hasProgress() {
5114 return true;
5115 }
Selim Cinek03d0d652015-11-13 13:18:09 -05005116
5117 /**
5118 * @hide
5119 * @return Whether we should put the summary be put into the notification header
5120 */
5121 public boolean hasSummaryInHeader() {
5122 return true;
5123 }
Selim Cinek593610c2016-02-16 18:42:57 -08005124
5125 /**
5126 * @hide
5127 * @return Whether custom content views are displayed inline in the style
5128 */
5129 public boolean displayCustomViewInline() {
5130 return false;
5131 }
Joe Onorato46439ce2010-11-19 13:56:21 -08005132 }
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005133
5134 /**
Daniel Sandler4dfbe832012-04-11 14:51:46 -04005135 * Helper class for generating large-format notifications that include a large image attachment.
Joe Malin8d40d042012-11-05 11:36:40 -08005136 *
Robert Ly91c5ce32014-06-08 15:37:00 -07005137 * Here's how you'd set the <code>BigPictureStyle</code> on a notification:
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005138 * <pre class="prettyprint">
Robert Ly91c5ce32014-06-08 15:37:00 -07005139 * Notification notif = new Notification.Builder(mContext)
5140 * .setContentTitle(&quot;New photo from &quot; + sender.toString())
5141 * .setContentText(subject)
5142 * .setSmallIcon(R.drawable.new_post)
5143 * .setLargeIcon(aBitmap)
5144 * .setStyle(new Notification.BigPictureStyle()
5145 * .bigPicture(aBigBitmap))
5146 * .build();
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005147 * </pre>
Joe Malin8d40d042012-11-05 11:36:40 -08005148 *
Daniel Sandler4dfbe832012-04-11 14:51:46 -04005149 * @see Notification#bigContentView
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005150 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005151 public static class BigPictureStyle extends Style {
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005152 private Bitmap mPicture;
Dan Sandlerd63f9322015-05-06 15:18:49 -04005153 private Icon mBigLargeIcon;
Chris Wren3745a3d2012-05-22 15:11:52 -04005154 private boolean mBigLargeIconSet = false;
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005155
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005156 public BigPictureStyle() {
5157 }
5158
Adrian Roosf5faf9d2016-05-23 13:56:15 -07005159 /**
5160 * @deprecated use {@code BigPictureStyle()}.
5161 */
5162 @Deprecated
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005163 public BigPictureStyle(Builder builder) {
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005164 setBuilder(builder);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005165 }
5166
Chris Wrend6297db2012-05-03 16:20:13 -04005167 /**
5168 * Overrides ContentTitle in the big form of the template.
5169 * This defaults to the value passed to setContentTitle().
5170 */
5171 public BigPictureStyle setBigContentTitle(CharSequence title) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04005172 internalSetBigContentTitle(safeCharSequence(title));
Chris Wrend6297db2012-05-03 16:20:13 -04005173 return this;
5174 }
5175
5176 /**
5177 * Set the first line of text after the detail section in the big form of the template.
5178 */
5179 public BigPictureStyle setSummaryText(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04005180 internalSetSummaryText(safeCharSequence(cs));
Chris Wrend6297db2012-05-03 16:20:13 -04005181 return this;
5182 }
5183
Chris Wren0bd664d2012-08-01 13:56:56 -04005184 /**
5185 * Provide the bitmap to be used as the payload for the BigPicture notification.
5186 */
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005187 public BigPictureStyle bigPicture(Bitmap b) {
5188 mPicture = b;
5189 return this;
5190 }
5191
Chris Wren3745a3d2012-05-22 15:11:52 -04005192 /**
Chris Wren3745a3d2012-05-22 15:11:52 -04005193 * Override the large icon when the big notification is shown.
5194 */
5195 public BigPictureStyle bigLargeIcon(Bitmap b) {
Dan Sandlerd63f9322015-05-06 15:18:49 -04005196 return bigLargeIcon(b != null ? Icon.createWithBitmap(b) : null);
5197 }
5198
5199 /**
5200 * Override the large icon when the big notification is shown.
5201 */
5202 public BigPictureStyle bigLargeIcon(Icon icon) {
Chris Wren3745a3d2012-05-22 15:11:52 -04005203 mBigLargeIconSet = true;
Dan Sandlerd63f9322015-05-06 15:18:49 -04005204 mBigLargeIcon = icon;
Chris Wren3745a3d2012-05-22 15:11:52 -04005205 return this;
5206 }
5207
Riley Andrews0394a0c2015-11-03 23:36:52 -08005208 /** @hide */
5209 public static final int MIN_ASHMEM_BITMAP_SIZE = 128 * (1 << 10);
5210
Daniel Sandler0ec46202015-06-24 01:27:05 -04005211 /**
5212 * @hide
5213 */
Jorim Jaggia0d58ae2015-06-03 11:48:13 -07005214 @Override
5215 public void purgeResources() {
5216 super.purgeResources();
Riley Andrews8cee7c12015-11-01 23:36:04 -08005217 if (mPicture != null &&
5218 mPicture.isMutable() &&
Riley Andrews0394a0c2015-11-03 23:36:52 -08005219 mPicture.getAllocationByteCount() >= MIN_ASHMEM_BITMAP_SIZE) {
Jorim Jaggia0d58ae2015-06-03 11:48:13 -07005220 mPicture = mPicture.createAshmemBitmap();
5221 }
5222 if (mBigLargeIcon != null) {
5223 mBigLargeIcon.convertToAshmem();
5224 }
5225 }
Christoph Studer5c510ee2014-12-15 16:32:27 +01005226
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005227 /**
5228 * @hide
5229 */
5230 public RemoteViews makeBigContentView() {
5231 // Replace mN.mLargeIcon with mBigLargeIcon if mBigLargeIconSet
Christoph Studer5c510ee2014-12-15 16:32:27 +01005232 // This covers the following cases:
5233 // 1. mBigLargeIconSet -> mBigLargeIcon (null or non-null) applies, overrides
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005234 // mN.mLargeIcon
5235 // 2. !mBigLargeIconSet -> mN.mLargeIcon applies
Dan Sandlerd63f9322015-05-06 15:18:49 -04005236 Icon oldLargeIcon = null;
Selim Cineke99acb22016-08-04 12:55:48 -07005237 Bitmap largeIconLegacy = null;
Christoph Studer5c510ee2014-12-15 16:32:27 +01005238 if (mBigLargeIconSet) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005239 oldLargeIcon = mBuilder.mN.mLargeIcon;
5240 mBuilder.mN.mLargeIcon = mBigLargeIcon;
Selim Cineke99acb22016-08-04 12:55:48 -07005241 // The legacy largeIcon might not allow us to clear the image, as it's taken in
5242 // replacement if the other one is null. Because we're restoring these legacy icons
5243 // for old listeners, this is in general non-null.
5244 largeIconLegacy = mBuilder.mN.largeIcon;
5245 mBuilder.mN.largeIcon = null;
Christoph Studer5c510ee2014-12-15 16:32:27 +01005246 }
5247
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005248 RemoteViews contentView = getStandardView(mBuilder.getBigPictureLayoutResource());
Selim Cinek03d0d652015-11-13 13:18:09 -05005249 if (mSummaryTextSet) {
5250 contentView.setTextViewText(R.id.text, mBuilder.processLegacyText(mSummaryText));
Selim Cinek7b9605b2017-01-19 17:36:00 -08005251 mBuilder.setTextViewColorSecondary(contentView, R.id.text);
Selim Cinekc848c3a2016-01-13 15:27:30 -08005252 contentView.setViewVisibility(R.id.text, View.VISIBLE);
Selim Cinek03d0d652015-11-13 13:18:09 -05005253 }
Selim Cinek279fa862016-06-14 10:57:25 -07005254 mBuilder.setContentMinHeight(contentView, mBuilder.mN.hasLargeIcon());
Selim Cinek53e64a42015-11-16 10:40:56 -08005255
Christoph Studer5c510ee2014-12-15 16:32:27 +01005256 if (mBigLargeIconSet) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005257 mBuilder.mN.mLargeIcon = oldLargeIcon;
Selim Cineke99acb22016-08-04 12:55:48 -07005258 mBuilder.mN.largeIcon = largeIconLegacy;
Christoph Studer5c510ee2014-12-15 16:32:27 +01005259 }
5260
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005261 contentView.setImageViewBitmap(R.id.big_picture, mPicture);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005262 return contentView;
5263 }
5264
Daniel Sandlerf45564e2013-04-15 15:05:08 -04005265 /**
5266 * @hide
5267 */
5268 public void addExtras(Bundle extras) {
5269 super.addExtras(extras);
5270
5271 if (mBigLargeIconSet) {
5272 extras.putParcelable(EXTRA_LARGE_ICON_BIG, mBigLargeIcon);
5273 }
5274 extras.putParcelable(EXTRA_PICTURE, mPicture);
5275 }
5276
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04005277 /**
5278 * @hide
5279 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005280 @Override
Christoph Studer4600f9b2014-07-22 22:44:43 +02005281 protected void restoreFromExtras(Bundle extras) {
5282 super.restoreFromExtras(extras);
5283
5284 if (extras.containsKey(EXTRA_LARGE_ICON_BIG)) {
Christoph Studer5c510ee2014-12-15 16:32:27 +01005285 mBigLargeIconSet = true;
Christoph Studer4600f9b2014-07-22 22:44:43 +02005286 mBigLargeIcon = extras.getParcelable(EXTRA_LARGE_ICON_BIG);
Chris Wren3745a3d2012-05-22 15:11:52 -04005287 }
Christoph Studer4600f9b2014-07-22 22:44:43 +02005288 mPicture = extras.getParcelable(EXTRA_PICTURE);
5289 }
Selim Cinek03d0d652015-11-13 13:18:09 -05005290
5291 /**
5292 * @hide
5293 */
5294 @Override
5295 public boolean hasSummaryInHeader() {
5296 return false;
5297 }
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005298 }
5299
5300 /**
Daniel Sandler4dfbe832012-04-11 14:51:46 -04005301 * Helper class for generating large-format notifications that include a lot of text.
Joe Malin8d40d042012-11-05 11:36:40 -08005302 *
Robert Ly91c5ce32014-06-08 15:37:00 -07005303 * Here's how you'd set the <code>BigTextStyle</code> on a notification:
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005304 * <pre class="prettyprint">
Robert Ly91c5ce32014-06-08 15:37:00 -07005305 * Notification notif = new Notification.Builder(mContext)
5306 * .setContentTitle(&quot;New mail from &quot; + sender.toString())
5307 * .setContentText(subject)
5308 * .setSmallIcon(R.drawable.new_mail)
5309 * .setLargeIcon(aBitmap)
5310 * .setStyle(new Notification.BigTextStyle()
5311 * .bigText(aVeryLongString))
5312 * .build();
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005313 * </pre>
Joe Malin8d40d042012-11-05 11:36:40 -08005314 *
Daniel Sandler4dfbe832012-04-11 14:51:46 -04005315 * @see Notification#bigContentView
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005316 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005317 public static class BigTextStyle extends Style {
Jorim Jaggi457a10d2014-09-08 16:18:23 +02005318
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005319 private CharSequence mBigText;
5320
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005321 public BigTextStyle() {
5322 }
5323
Adrian Roosf5faf9d2016-05-23 13:56:15 -07005324 /**
5325 * @deprecated use {@code BigTextStyle()}.
5326 */
5327 @Deprecated
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005328 public BigTextStyle(Builder builder) {
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005329 setBuilder(builder);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005330 }
5331
Chris Wrend6297db2012-05-03 16:20:13 -04005332 /**
5333 * Overrides ContentTitle in the big form of the template.
5334 * This defaults to the value passed to setContentTitle().
5335 */
5336 public BigTextStyle setBigContentTitle(CharSequence title) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04005337 internalSetBigContentTitle(safeCharSequence(title));
Chris Wrend6297db2012-05-03 16:20:13 -04005338 return this;
5339 }
5340
5341 /**
5342 * Set the first line of text after the detail section in the big form of the template.
5343 */
5344 public BigTextStyle setSummaryText(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04005345 internalSetSummaryText(safeCharSequence(cs));
Chris Wrend6297db2012-05-03 16:20:13 -04005346 return this;
5347 }
5348
Chris Wren0bd664d2012-08-01 13:56:56 -04005349 /**
5350 * Provide the longer text to be displayed in the big form of the
5351 * template in place of the content text.
5352 */
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005353 public BigTextStyle bigText(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04005354 mBigText = safeCharSequence(cs);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005355 return this;
5356 }
5357
Daniel Sandlerf45564e2013-04-15 15:05:08 -04005358 /**
5359 * @hide
5360 */
5361 public void addExtras(Bundle extras) {
5362 super.addExtras(extras);
5363
Christoph Studer4600f9b2014-07-22 22:44:43 +02005364 extras.putCharSequence(EXTRA_BIG_TEXT, mBigText);
5365 }
5366
5367 /**
5368 * @hide
5369 */
5370 @Override
5371 protected void restoreFromExtras(Bundle extras) {
5372 super.restoreFromExtras(extras);
5373
5374 mBigText = extras.getCharSequence(EXTRA_BIG_TEXT);
Daniel Sandlerf45564e2013-04-15 15:05:08 -04005375 }
5376
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005377 /**
Selim Cinek7d1009b2017-01-25 15:28:28 -08005378 * @param increasedHeight true if this layout be created with an increased height.
5379 *
5380 * @hide
5381 */
5382 @Override
5383 public RemoteViews makeContentView(boolean increasedHeight) {
5384 if (increasedHeight) {
5385 ArrayList<Action> actions = mBuilder.mActions;
5386 mBuilder.mActions = new ArrayList<>();
5387 RemoteViews remoteViews = makeBigContentView();
5388 mBuilder.mActions = actions;
5389 return remoteViews;
5390 }
5391 return super.makeContentView(increasedHeight);
5392 }
5393
5394 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005395 * @hide
5396 */
Selim Cinek87ed69b2017-02-09 15:59:43 -08005397 @Override
5398 public RemoteViews makeHeadsUpContentView(boolean increasedHeight) {
5399 if (increasedHeight && mBuilder.mActions.size() > 0) {
5400 return makeBigContentView();
5401 }
5402 return super.makeHeadsUpContentView(increasedHeight);
5403 }
5404
5405 /**
5406 * @hide
5407 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005408 public RemoteViews makeBigContentView() {
Christoph Studer4600f9b2014-07-22 22:44:43 +02005409
5410 // Nasty
Selim Cinek75998782016-04-26 10:39:17 -07005411 CharSequence text = mBuilder.getAllExtras().getCharSequence(EXTRA_TEXT);
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005412 mBuilder.getAllExtras().putCharSequence(EXTRA_TEXT, null);
Daniel Sandler916ad912012-06-13 12:17:07 -04005413
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005414 RemoteViews contentView = getStandardView(mBuilder.getBigTextLayoutResource());
Joe Malin8d40d042012-11-05 11:36:40 -08005415
Selim Cinek75998782016-04-26 10:39:17 -07005416 mBuilder.getAllExtras().putCharSequence(EXTRA_TEXT, text);
Christoph Studer4600f9b2014-07-22 22:44:43 +02005417
Selim Cinek3a2c4b92015-12-17 17:01:17 -08005418 CharSequence bigTextText = mBuilder.processLegacyText(mBigText);
Selim Cinek75998782016-04-26 10:39:17 -07005419 if (TextUtils.isEmpty(bigTextText)) {
5420 // In case the bigtext is null / empty fall back to the normal text to avoid a weird
5421 // experience
5422 bigTextText = mBuilder.processLegacyText(text);
5423 }
Adrian Roosb1f427c2016-05-26 12:27:15 -07005424 applyBigTextContentView(mBuilder, contentView, bigTextText);
Selim Cinek4fb12d32015-11-19 18:10:48 -08005425
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005426 return contentView;
5427 }
5428
Adrian Roosb1f427c2016-05-26 12:27:15 -07005429 static void applyBigTextContentView(Builder builder,
5430 RemoteViews contentView, CharSequence bigTextText) {
5431 contentView.setTextViewText(R.id.big_text, bigTextText);
Selim Cinek7b9605b2017-01-19 17:36:00 -08005432 builder.setTextViewColorSecondary(contentView, R.id.big_text);
Adrian Roosb1f427c2016-05-26 12:27:15 -07005433 contentView.setViewVisibility(R.id.big_text,
5434 TextUtils.isEmpty(bigTextText) ? View.GONE : View.VISIBLE);
Selim Cinek279fa862016-06-14 10:57:25 -07005435 contentView.setBoolean(R.id.big_text, "setHasImage", builder.mN.hasLargeIcon());
Adrian Roosb1f427c2016-05-26 12:27:15 -07005436 }
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005437 }
Daniel Sandler879c5e02012-04-17 16:46:51 -04005438
5439 /**
Alex Hillsfc737de2016-03-23 17:33:02 -04005440 * Helper class for generating large-format notifications that include multiple back-and-forth
5441 * messages of varying types between any number of people.
5442 *
5443 * <br>
5444 * If the platform does not provide large-format notifications, this method has no effect. The
5445 * user will always see the normal notification view.
5446 * <br>
5447 * This class is a "rebuilder": It attaches to a Builder object and modifies its behavior, like
5448 * so:
5449 * <pre class="prettyprint">
5450 *
5451 * Notification noti = new Notification.Builder()
5452 * .setContentTitle(&quot;2 new messages wtih &quot; + sender.toString())
5453 * .setContentText(subject)
5454 * .setSmallIcon(R.drawable.new_message)
5455 * .setLargeIcon(aBitmap)
5456 * .setStyle(new Notification.MessagingStyle(resources.getString(R.string.reply_name))
5457 * .addMessage(messages[0].getText(), messages[0].getTime(), messages[0].getSender())
5458 * .addMessage(messages[1].getText(), messages[1].getTime(), messages[1].getSender()))
5459 * .build();
5460 * </pre>
5461 */
5462 public static class MessagingStyle extends Style {
5463
5464 /**
5465 * The maximum number of messages that will be retained in the Notification itself (the
5466 * number displayed is up to the platform).
5467 */
5468 public static final int MAXIMUM_RETAINED_MESSAGES = 25;
5469
5470 CharSequence mUserDisplayName;
5471 CharSequence mConversationTitle;
Alex Hillsd9b04d92016-04-11 16:38:16 -04005472 List<Message> mMessages = new ArrayList<>();
Adrian Roos437cd562017-01-18 15:47:03 -08005473 List<Message> mHistoricMessages = new ArrayList<>();
Alex Hillsfc737de2016-03-23 17:33:02 -04005474
5475 MessagingStyle() {
5476 }
5477
5478 /**
Alex Hillsfd590442016-10-07 09:52:44 -04005479 * @param userDisplayName Required - the name to be displayed for any replies sent by the
5480 * user before the posting app reposts the notification with those messages after they've
5481 * been actually sent and in previous messages sent by the user added in
Alex Hillsfc737de2016-03-23 17:33:02 -04005482 * {@link #addMessage(Notification.MessagingStyle.Message)}
5483 */
Alex Hillsfd590442016-10-07 09:52:44 -04005484 public MessagingStyle(@NonNull CharSequence userDisplayName) {
Alex Hillsfc737de2016-03-23 17:33:02 -04005485 mUserDisplayName = userDisplayName;
5486 }
5487
5488 /**
5489 * Returns the name to be displayed for any replies sent by the user
5490 */
5491 public CharSequence getUserDisplayName() {
5492 return mUserDisplayName;
5493 }
5494
5495 /**
Alex Hillsfc737de2016-03-23 17:33:02 -04005496 * Sets the title to be displayed on this conversation. This should only be used for
5497 * group messaging and left unset for one-on-one conversations.
5498 * @param conversationTitle
5499 * @return this object for method chaining.
5500 */
5501 public MessagingStyle setConversationTitle(CharSequence conversationTitle) {
5502 mConversationTitle = conversationTitle;
5503 return this;
5504 }
5505
5506 /**
5507 * Return the title to be displayed on this conversation. Can be <code>null</code> and
5508 * should be for one-on-one conversations
5509 */
5510 public CharSequence getConversationTitle() {
5511 return mConversationTitle;
5512 }
5513
5514 /**
5515 * Adds a message for display by this notification. Convenience call for a simple
5516 * {@link Message} in {@link #addMessage(Notification.MessagingStyle.Message)}.
5517 * @param text A {@link CharSequence} to be displayed as the message content
5518 * @param timestamp Time at which the message arrived
5519 * @param sender A {@link CharSequence} to be used for displaying the name of the
5520 * sender. Should be <code>null</code> for messages by the current user, in which case
5521 * the platform will insert {@link #getUserDisplayName()}.
5522 * Should be unique amongst all individuals in the conversation, and should be
5523 * consistent during re-posts of the notification.
5524 *
5525 * @see Message#Message(CharSequence, long, CharSequence)
5526 *
5527 * @return this object for method chaining
5528 */
5529 public MessagingStyle addMessage(CharSequence text, long timestamp, CharSequence sender) {
Adrian Roos437cd562017-01-18 15:47:03 -08005530 return addMessage(new Message(text, timestamp, sender));
Alex Hillsfc737de2016-03-23 17:33:02 -04005531 }
5532
5533 /**
5534 * Adds a {@link Message} for display in this notification.
Adrian Roos437cd562017-01-18 15:47:03 -08005535 *
5536 * <p>The messages should be added in chronologic order, i.e. the oldest first,
5537 * the newest last.
5538 *
Alex Hillsfc737de2016-03-23 17:33:02 -04005539 * @param message The {@link Message} to be displayed
5540 * @return this object for method chaining
5541 */
5542 public MessagingStyle addMessage(Message message) {
5543 mMessages.add(message);
5544 if (mMessages.size() > MAXIMUM_RETAINED_MESSAGES) {
5545 mMessages.remove(0);
5546 }
5547 return this;
5548 }
5549
5550 /**
Adrian Roos437cd562017-01-18 15:47:03 -08005551 * Adds a {@link Message} for historic context in this notification.
5552 *
5553 * <p>Messages should be added as historic if they are not the main subject of the
5554 * notification but may give context to a conversation. The system may choose to present
5555 * them only when relevant, e.g. when replying to a message through a {@link RemoteInput}.
5556 *
5557 * <p>The messages should be added in chronologic order, i.e. the oldest first,
5558 * the newest last.
5559 *
5560 * @param message The historic {@link Message} to be added
5561 * @return this object for method chaining
5562 */
5563 public MessagingStyle addHistoricMessage(Message message) {
5564 mHistoricMessages.add(message);
5565 if (mHistoricMessages.size() > MAXIMUM_RETAINED_MESSAGES) {
5566 mHistoricMessages.remove(0);
5567 }
5568 return this;
5569 }
5570
5571 /**
Alex Hillsfc737de2016-03-23 17:33:02 -04005572 * Gets the list of {@code Message} objects that represent the notification
5573 */
5574 public List<Message> getMessages() {
5575 return mMessages;
5576 }
5577
5578 /**
Adrian Roos437cd562017-01-18 15:47:03 -08005579 * Gets the list of historic {@code Message}s in the notification.
5580 */
5581 public List<Message> getHistoricMessages() {
5582 return mHistoricMessages;
5583 }
5584
5585 /**
Alex Hillsfc737de2016-03-23 17:33:02 -04005586 * @hide
5587 */
5588 @Override
5589 public void addExtras(Bundle extras) {
5590 super.addExtras(extras);
5591 if (mUserDisplayName != null) {
5592 extras.putCharSequence(EXTRA_SELF_DISPLAY_NAME, mUserDisplayName);
5593 }
5594 if (mConversationTitle != null) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04005595 extras.putCharSequence(EXTRA_CONVERSATION_TITLE, mConversationTitle);
Alex Hillsfc737de2016-03-23 17:33:02 -04005596 }
Alex Hillsd9b04d92016-04-11 16:38:16 -04005597 if (!mMessages.isEmpty()) { extras.putParcelableArray(EXTRA_MESSAGES,
5598 Message.getBundleArrayForMessages(mMessages));
Alex Hillsfc737de2016-03-23 17:33:02 -04005599 }
Adrian Roos437cd562017-01-18 15:47:03 -08005600 if (!mHistoricMessages.isEmpty()) { extras.putParcelableArray(EXTRA_HISTORIC_MESSAGES,
5601 Message.getBundleArrayForMessages(mHistoricMessages));
5602 }
Adrian Roos33fbd2c2016-05-27 15:35:28 -07005603
5604 fixTitleAndTextExtras(extras);
5605 }
5606
5607 private void fixTitleAndTextExtras(Bundle extras) {
5608 Message m = findLatestIncomingMessage();
5609 CharSequence text = (m == null) ? null : m.mText;
5610 CharSequence sender = m == null ? null
5611 : TextUtils.isEmpty(m.mSender) ? mUserDisplayName : m.mSender;
5612 CharSequence title;
5613 if (!TextUtils.isEmpty(mConversationTitle)) {
5614 if (!TextUtils.isEmpty(sender)) {
5615 BidiFormatter bidi = BidiFormatter.getInstance();
5616 title = mBuilder.mContext.getString(
5617 com.android.internal.R.string.notification_messaging_title_template,
5618 bidi.unicodeWrap(mConversationTitle), bidi.unicodeWrap(m.mSender));
5619 } else {
5620 title = mConversationTitle;
5621 }
5622 } else {
5623 title = sender;
5624 }
5625
5626 if (title != null) {
5627 extras.putCharSequence(EXTRA_TITLE, title);
5628 }
5629 if (text != null) {
5630 extras.putCharSequence(EXTRA_TEXT, text);
5631 }
Alex Hillsfc737de2016-03-23 17:33:02 -04005632 }
5633
5634 /**
5635 * @hide
5636 */
5637 @Override
5638 protected void restoreFromExtras(Bundle extras) {
5639 super.restoreFromExtras(extras);
5640
5641 mMessages.clear();
Adrian Roos437cd562017-01-18 15:47:03 -08005642 mHistoricMessages.clear();
Adrian Roos96b7e202016-05-17 13:50:38 -07005643 mUserDisplayName = extras.getCharSequence(EXTRA_SELF_DISPLAY_NAME);
5644 mConversationTitle = extras.getCharSequence(EXTRA_CONVERSATION_TITLE);
Adrian Roos437cd562017-01-18 15:47:03 -08005645 Parcelable[] messages = extras.getParcelableArray(EXTRA_MESSAGES);
5646 if (messages != null && messages instanceof Parcelable[]) {
5647 mMessages = Message.getMessagesFromBundleArray(messages);
5648 }
5649 Parcelable[] histMessages = extras.getParcelableArray(EXTRA_HISTORIC_MESSAGES);
5650 if (histMessages != null && histMessages instanceof Parcelable[]) {
5651 mHistoricMessages = Message.getMessagesFromBundleArray(histMessages);
Alex Hillsfc737de2016-03-23 17:33:02 -04005652 }
5653 }
5654
5655 /**
5656 * @hide
5657 */
Adrian Roosc1a80b02016-04-05 14:54:55 -07005658 @Override
Selim Cinek7d1009b2017-01-25 15:28:28 -08005659 public RemoteViews makeContentView(boolean increasedHeight) {
5660 if (!increasedHeight) {
5661 Message m = findLatestIncomingMessage();
5662 CharSequence title = mConversationTitle != null
5663 ? mConversationTitle
5664 : (m == null) ? null : m.mSender;
5665 CharSequence text = (m == null)
5666 ? null
5667 : mConversationTitle != null ? makeMessageLine(m, mBuilder) : m.mText;
Alex Hillsfc737de2016-03-23 17:33:02 -04005668
Selim Cinek7d1009b2017-01-25 15:28:28 -08005669 return mBuilder.applyStandardTemplate(mBuilder.getBaseLayoutResource(),
5670 mBuilder.mParams.reset().hasProgress(false).title(title).text(text));
5671 } else {
5672 ArrayList<Action> actions = mBuilder.mActions;
5673 mBuilder.mActions = new ArrayList<>();
5674 RemoteViews remoteViews = makeBigContentView();
5675 mBuilder.mActions = actions;
5676 return remoteViews;
5677 }
Adrian Roosc1a80b02016-04-05 14:54:55 -07005678 }
5679
5680 private Message findLatestIncomingMessage() {
5681 for (int i = mMessages.size() - 1; i >= 0; i--) {
5682 Message m = mMessages.get(i);
5683 // Incoming messages have a non-empty sender.
5684 if (!TextUtils.isEmpty(m.mSender)) {
5685 return m;
5686 }
5687 }
Adrian Roos33fbd2c2016-05-27 15:35:28 -07005688 if (!mMessages.isEmpty()) {
5689 // No incoming messages, fall back to outgoing message
5690 return mMessages.get(mMessages.size() - 1);
5691 }
Adrian Roosc1a80b02016-04-05 14:54:55 -07005692 return null;
5693 }
5694
5695 /**
5696 * @hide
5697 */
5698 @Override
5699 public RemoteViews makeBigContentView() {
5700 CharSequence title = !TextUtils.isEmpty(super.mBigContentTitle)
5701 ? super.mBigContentTitle
5702 : mConversationTitle;
5703 boolean hasTitle = !TextUtils.isEmpty(title);
5704
Adrian Roosfeafa052016-06-01 17:09:45 -07005705 if (mMessages.size() == 1) {
5706 // Special case for a single message: Use the big text style
5707 // so the collapsed and expanded versions match nicely.
5708 CharSequence bigTitle;
5709 CharSequence text;
5710 if (hasTitle) {
5711 bigTitle = title;
Selim Cinek7b9605b2017-01-19 17:36:00 -08005712 text = makeMessageLine(mMessages.get(0), mBuilder);
Adrian Roosfeafa052016-06-01 17:09:45 -07005713 } else {
5714 bigTitle = mMessages.get(0).mSender;
5715 text = mMessages.get(0).mText;
5716 }
Adrian Roosb1f427c2016-05-26 12:27:15 -07005717 RemoteViews contentView = mBuilder.applyStandardTemplateWithActions(
5718 mBuilder.getBigTextLayoutResource(),
Adrian Roos70d7aa32017-01-11 15:39:06 -08005719 mBuilder.mParams.reset().hasProgress(false).title(bigTitle).text(null));
Adrian Roosb1f427c2016-05-26 12:27:15 -07005720 BigTextStyle.applyBigTextContentView(mBuilder, contentView, text);
5721 return contentView;
5722 }
5723
Adrian Roos48d746a2016-04-12 14:57:28 -07005724 RemoteViews contentView = mBuilder.applyStandardTemplateWithActions(
Adrian Roosc1a80b02016-04-05 14:54:55 -07005725 mBuilder.getMessagingLayoutResource(),
Adrian Roos70d7aa32017-01-11 15:39:06 -08005726 mBuilder.mParams.reset().hasProgress(false).title(title).text(null));
Adrian Roosc1a80b02016-04-05 14:54:55 -07005727
5728 int[] rowIds = {R.id.inbox_text0, R.id.inbox_text1, R.id.inbox_text2, R.id.inbox_text3,
5729 R.id.inbox_text4, R.id.inbox_text5, R.id.inbox_text6};
5730
5731 // Make sure all rows are gone in case we reuse a view.
5732 for (int rowId : rowIds) {
5733 contentView.setViewVisibility(rowId, View.GONE);
5734 }
5735
5736 int i=0;
Adrian Roos2d5dbba2016-06-08 17:11:53 -07005737 contentView.setViewLayoutMarginBottomDimen(R.id.line1,
5738 hasTitle ? R.dimen.notification_messaging_spacing : 0);
Adrian Roosc1a80b02016-04-05 14:54:55 -07005739 contentView.setInt(R.id.notification_messaging, "setNumIndentLines",
Selim Cinek279fa862016-06-14 10:57:25 -07005740 !mBuilder.mN.hasLargeIcon() ? 0 : (hasTitle ? 1 : 2));
Adrian Roosc1a80b02016-04-05 14:54:55 -07005741
Adrian Roosfeafa052016-06-01 17:09:45 -07005742 int contractedChildId = View.NO_ID;
5743 Message contractedMessage = findLatestIncomingMessage();
Adrian Roos437cd562017-01-18 15:47:03 -08005744 int firstHistoricMessage = Math.max(0, mHistoricMessages.size()
5745 - (rowIds.length - mMessages.size()));
5746 while (firstHistoricMessage + i < mHistoricMessages.size() && i < rowIds.length) {
5747 Message m = mHistoricMessages.get(firstHistoricMessage + i);
5748 int rowId = rowIds[i];
5749
Selim Cinek7b9605b2017-01-19 17:36:00 -08005750 contentView.setTextViewText(rowId, makeMessageLine(m, mBuilder));
Adrian Roos437cd562017-01-18 15:47:03 -08005751
5752 if (contractedMessage == m) {
5753 contractedChildId = rowId;
5754 }
5755
5756 i++;
5757 }
5758
Adrian Roosc1a80b02016-04-05 14:54:55 -07005759 int firstMessage = Math.max(0, mMessages.size() - rowIds.length);
5760 while (firstMessage + i < mMessages.size() && i < rowIds.length) {
5761 Message m = mMessages.get(firstMessage + i);
5762 int rowId = rowIds[i];
5763
5764 contentView.setViewVisibility(rowId, View.VISIBLE);
Selim Cinek7b9605b2017-01-19 17:36:00 -08005765 contentView.setTextViewText(rowId, makeMessageLine(m, mBuilder));
5766 mBuilder.setTextViewColorSecondary(contentView, rowId);
Adrian Roosc1a80b02016-04-05 14:54:55 -07005767
Adrian Roosfeafa052016-06-01 17:09:45 -07005768 if (contractedMessage == m) {
5769 contractedChildId = rowId;
5770 }
5771
Adrian Roosc1a80b02016-04-05 14:54:55 -07005772 i++;
5773 }
Adrian Roos437cd562017-01-18 15:47:03 -08005774 // Clear the remaining views for reapply. Ensures that historic message views can
5775 // reliably be identified as being GONE and having non-null text.
5776 while (i < rowIds.length) {
5777 int rowId = rowIds[i];
5778 contentView.setTextViewText(rowId, null);
5779 i++;
5780 }
5781
Adrian Roosfeafa052016-06-01 17:09:45 -07005782 // Record this here to allow transformation between the contracted and expanded views.
5783 contentView.setInt(R.id.notification_messaging, "setContractedChildId",
5784 contractedChildId);
Alex Hillsfc737de2016-03-23 17:33:02 -04005785 return contentView;
5786 }
5787
Selim Cinek7b9605b2017-01-19 17:36:00 -08005788 private CharSequence makeMessageLine(Message m, Builder builder) {
Adrian Roosc1a80b02016-04-05 14:54:55 -07005789 BidiFormatter bidi = BidiFormatter.getInstance();
5790 SpannableStringBuilder sb = new SpannableStringBuilder();
Selim Cinek7b9605b2017-01-19 17:36:00 -08005791 boolean colorize = builder.isColorized();
Adrian Roosc1a80b02016-04-05 14:54:55 -07005792 if (TextUtils.isEmpty(m.mSender)) {
5793 CharSequence replyName = mUserDisplayName == null ? "" : mUserDisplayName;
5794 sb.append(bidi.unicodeWrap(replyName),
Selim Cinek7b9605b2017-01-19 17:36:00 -08005795 makeFontColorSpan(colorize
5796 ? builder.getPrimaryTextColor()
5797 : mBuilder.resolveContrastColor()),
Adrian Roosc1a80b02016-04-05 14:54:55 -07005798 0 /* flags */);
5799 } else {
5800 sb.append(bidi.unicodeWrap(m.mSender),
Selim Cinek7b9605b2017-01-19 17:36:00 -08005801 makeFontColorSpan(colorize
5802 ? builder.getPrimaryTextColor()
5803 : Color.BLACK),
Adrian Roosc1a80b02016-04-05 14:54:55 -07005804 0 /* flags */);
5805 }
5806 CharSequence text = m.mText == null ? "" : m.mText;
5807 sb.append(" ").append(bidi.unicodeWrap(text));
5808 return sb;
5809 }
5810
Adrian Roosdedd1df2016-04-26 16:38:47 -07005811 /**
5812 * @hide
5813 */
5814 @Override
Selim Cinek87ed69b2017-02-09 15:59:43 -08005815 public RemoteViews makeHeadsUpContentView(boolean increasedHeight) {
5816 if (increasedHeight) {
5817 return makeBigContentView();
5818 }
Adrian Roosdedd1df2016-04-26 16:38:47 -07005819 Message m = findLatestIncomingMessage();
5820 CharSequence title = mConversationTitle != null
5821 ? mConversationTitle
5822 : (m == null) ? null : m.mSender;
5823 CharSequence text = (m == null)
5824 ? null
Selim Cinek7b9605b2017-01-19 17:36:00 -08005825 : mConversationTitle != null ? makeMessageLine(m, mBuilder) : m.mText;
Adrian Roosdedd1df2016-04-26 16:38:47 -07005826
5827 return mBuilder.applyStandardTemplateWithActions(mBuilder.getBigBaseLayoutResource(),
Adrian Roos70d7aa32017-01-11 15:39:06 -08005828 mBuilder.mParams.reset().hasProgress(false).title(title).text(text));
Adrian Roosdedd1df2016-04-26 16:38:47 -07005829 }
5830
Adrian Roosc1a80b02016-04-05 14:54:55 -07005831 private static TextAppearanceSpan makeFontColorSpan(int color) {
5832 return new TextAppearanceSpan(null, 0, 0,
5833 ColorStateList.valueOf(color), null);
5834 }
5835
Alex Hillsd9b04d92016-04-11 16:38:16 -04005836 public static final class Message {
5837
5838 static final String KEY_TEXT = "text";
5839 static final String KEY_TIMESTAMP = "time";
5840 static final String KEY_SENDER = "sender";
5841 static final String KEY_DATA_MIME_TYPE = "type";
5842 static final String KEY_DATA_URI= "uri";
Shane Brennan5a871862017-03-11 13:14:17 -08005843 static final String KEY_EXTRAS_BUNDLE = "extras";
Alex Hillsfc737de2016-03-23 17:33:02 -04005844
5845 private final CharSequence mText;
5846 private final long mTimestamp;
5847 private final CharSequence mSender;
5848
Shane Brennan5a871862017-03-11 13:14:17 -08005849 private Bundle mExtras = new Bundle();
Alex Hillsfc737de2016-03-23 17:33:02 -04005850 private String mDataMimeType;
5851 private Uri mDataUri;
5852
5853 /**
5854 * Constructor
5855 * @param text A {@link CharSequence} to be displayed as the message content
5856 * @param timestamp Time at which the message arrived
5857 * @param sender A {@link CharSequence} to be used for displaying the name of the
5858 * sender. Should be <code>null</code> for messages by the current user, in which case
5859 * the platform will insert {@link MessagingStyle#getUserDisplayName()}.
5860 * Should be unique amongst all individuals in the conversation, and should be
5861 * consistent during re-posts of the notification.
5862 */
5863 public Message(CharSequence text, long timestamp, CharSequence sender){
5864 mText = text;
5865 mTimestamp = timestamp;
5866 mSender = sender;
5867 }
5868
5869 /**
5870 * Sets a binary blob of data and an associated MIME type for a message. In the case
5871 * where the platform doesn't support the MIME type, the original text provided in the
5872 * constructor will be used.
5873 * @param dataMimeType The MIME type of the content. See
5874 * <a href="{@docRoot}notifications/messaging.html"> for the list of supported MIME
5875 * types on Android and Android Wear.
5876 * @param dataUri The uri containing the content whose type is given by the MIME type.
5877 * <p class="note">
5878 * <ol>
5879 * <li>Notification Listeners including the System UI need permission to access the
5880 * data the Uri points to. The recommended ways to do this are:</li>
5881 * <li>Store the data in your own ContentProvider, making sure that other apps have
5882 * the correct permission to access your provider. The preferred mechanism for
5883 * providing access is to use per-URI permissions which are temporary and only
5884 * grant access to the receiving application. An easy way to create a
5885 * ContentProvider like this is to use the FileProvider helper class.</li>
5886 * <li>Use the system MediaStore. The MediaStore is primarily aimed at video, audio
5887 * and image MIME types, however beginning with Android 3.0 (API level 11) it can
5888 * also store non-media types (see MediaStore.Files for more info). Files can be
5889 * inserted into the MediaStore using scanFile() after which a content:// style
5890 * Uri suitable for sharing is passed to the provided onScanCompleted() callback.
5891 * Note that once added to the system MediaStore the content is accessible to any
5892 * app on the device.</li>
5893 * </ol>
5894 * @return this object for method chaining
5895 */
5896 public Message setData(String dataMimeType, Uri dataUri) {
5897 mDataMimeType = dataMimeType;
5898 mDataUri = dataUri;
5899 return this;
5900 }
5901
Alex Hillsfc737de2016-03-23 17:33:02 -04005902 /**
5903 * Get the text to be used for this message, or the fallback text if a type and content
5904 * Uri have been set
5905 */
5906 public CharSequence getText() {
5907 return mText;
5908 }
5909
5910 /**
5911 * Get the time at which this message arrived
5912 */
5913 public long getTimestamp() {
5914 return mTimestamp;
5915 }
5916
5917 /**
Shane Brennan5a871862017-03-11 13:14:17 -08005918 * Get the extras Bundle for this message.
5919 */
5920 public Bundle getExtras() {
5921 return mExtras;
5922 }
5923
5924 /**
Alex Hillsfc737de2016-03-23 17:33:02 -04005925 * Get the text used to display the contact's name in the messaging experience
5926 */
5927 public CharSequence getSender() {
5928 return mSender;
5929 }
5930
5931 /**
5932 * Get the MIME type of the data pointed to by the Uri
5933 */
5934 public String getDataMimeType() {
5935 return mDataMimeType;
5936 }
5937
5938 /**
5939 * Get the the Uri pointing to the content of the message. Can be null, in which case
5940 * {@see #getText()} is used.
5941 */
5942 public Uri getDataUri() {
5943 return mDataUri;
5944 }
5945
Alex Hillsd9b04d92016-04-11 16:38:16 -04005946 private Bundle toBundle() {
5947 Bundle bundle = new Bundle();
Alex Hillsfc737de2016-03-23 17:33:02 -04005948 if (mText != null) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04005949 bundle.putCharSequence(KEY_TEXT, mText);
Alex Hillsfc737de2016-03-23 17:33:02 -04005950 }
Alex Hillsd9b04d92016-04-11 16:38:16 -04005951 bundle.putLong(KEY_TIMESTAMP, mTimestamp);
Alex Hillsfc737de2016-03-23 17:33:02 -04005952 if (mSender != null) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04005953 bundle.putCharSequence(KEY_SENDER, mSender);
Alex Hillsfc737de2016-03-23 17:33:02 -04005954 }
5955 if (mDataMimeType != null) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04005956 bundle.putString(KEY_DATA_MIME_TYPE, mDataMimeType);
Alex Hillsfc737de2016-03-23 17:33:02 -04005957 }
5958 if (mDataUri != null) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04005959 bundle.putParcelable(KEY_DATA_URI, mDataUri);
Alex Hillsfc737de2016-03-23 17:33:02 -04005960 }
Shane Brennan5a871862017-03-11 13:14:17 -08005961 if (mExtras != null) {
5962 bundle.putBundle(KEY_EXTRAS_BUNDLE, mExtras);
5963 }
Alex Hillsd9b04d92016-04-11 16:38:16 -04005964 return bundle;
Alex Hillsfc737de2016-03-23 17:33:02 -04005965 }
5966
Alex Hillsd9b04d92016-04-11 16:38:16 -04005967 static Bundle[] getBundleArrayForMessages(List<Message> messages) {
5968 Bundle[] bundles = new Bundle[messages.size()];
5969 final int N = messages.size();
5970 for (int i = 0; i < N; i++) {
5971 bundles[i] = messages.get(i).toBundle();
5972 }
5973 return bundles;
5974 }
5975
Adrian Roosdedd1df2016-04-26 16:38:47 -07005976 static List<Message> getMessagesFromBundleArray(Parcelable[] bundles) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04005977 List<Message> messages = new ArrayList<>(bundles.length);
5978 for (int i = 0; i < bundles.length; i++) {
Adrian Roosdedd1df2016-04-26 16:38:47 -07005979 if (bundles[i] instanceof Bundle) {
5980 Message message = getMessageFromBundle((Bundle)bundles[i]);
5981 if (message != null) {
5982 messages.add(message);
5983 }
Alex Hillsd9b04d92016-04-11 16:38:16 -04005984 }
5985 }
5986 return messages;
5987 }
5988
5989 static Message getMessageFromBundle(Bundle bundle) {
5990 try {
Adrian Roosfbddd2c2016-05-13 12:57:20 -07005991 if (!bundle.containsKey(KEY_TEXT) || !bundle.containsKey(KEY_TIMESTAMP)) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04005992 return null;
5993 } else {
5994 Message message = new Message(bundle.getCharSequence(KEY_TEXT),
5995 bundle.getLong(KEY_TIMESTAMP), bundle.getCharSequence(KEY_SENDER));
5996 if (bundle.containsKey(KEY_DATA_MIME_TYPE) &&
5997 bundle.containsKey(KEY_DATA_URI)) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04005998 message.setData(bundle.getString(KEY_DATA_MIME_TYPE),
5999 (Uri) bundle.getParcelable(KEY_DATA_URI));
Alex Hillsfc737de2016-03-23 17:33:02 -04006000 }
Shane Brennan5a871862017-03-11 13:14:17 -08006001 if (bundle.containsKey(KEY_EXTRAS_BUNDLE)) {
6002 message.getExtras().putAll(bundle.getBundle(KEY_EXTRAS_BUNDLE));
6003 }
Alex Hillsd9b04d92016-04-11 16:38:16 -04006004 return message;
6005 }
6006 } catch (ClassCastException e) {
6007 return null;
6008 }
6009 }
Alex Hillsfc737de2016-03-23 17:33:02 -04006010 }
6011 }
6012
6013 /**
Daniel Sandler879c5e02012-04-17 16:46:51 -04006014 * Helper class for generating large-format notifications that include a list of (up to 5) strings.
Joe Malin8d40d042012-11-05 11:36:40 -08006015 *
Robert Ly91c5ce32014-06-08 15:37:00 -07006016 * Here's how you'd set the <code>InboxStyle</code> on a notification:
Daniel Sandler879c5e02012-04-17 16:46:51 -04006017 * <pre class="prettyprint">
Robert Ly91c5ce32014-06-08 15:37:00 -07006018 * Notification notif = new Notification.Builder(mContext)
6019 * .setContentTitle(&quot;5 New mails from &quot; + sender.toString())
6020 * .setContentText(subject)
6021 * .setSmallIcon(R.drawable.new_mail)
6022 * .setLargeIcon(aBitmap)
6023 * .setStyle(new Notification.InboxStyle()
6024 * .addLine(str1)
6025 * .addLine(str2)
6026 * .setContentTitle(&quot;&quot;)
6027 * .setSummaryText(&quot;+3 more&quot;))
6028 * .build();
Daniel Sandler879c5e02012-04-17 16:46:51 -04006029 * </pre>
Joe Malin8d40d042012-11-05 11:36:40 -08006030 *
Daniel Sandler879c5e02012-04-17 16:46:51 -04006031 * @see Notification#bigContentView
6032 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006033 public static class InboxStyle extends Style {
Daniel Sandler879c5e02012-04-17 16:46:51 -04006034 private ArrayList<CharSequence> mTexts = new ArrayList<CharSequence>(5);
6035
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006036 public InboxStyle() {
6037 }
6038
Adrian Roosf5faf9d2016-05-23 13:56:15 -07006039 /**
6040 * @deprecated use {@code InboxStyle()}.
6041 */
6042 @Deprecated
Daniel Sandler879c5e02012-04-17 16:46:51 -04006043 public InboxStyle(Builder builder) {
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006044 setBuilder(builder);
Daniel Sandler879c5e02012-04-17 16:46:51 -04006045 }
6046
Chris Wrend6297db2012-05-03 16:20:13 -04006047 /**
6048 * Overrides ContentTitle in the big form of the template.
6049 * This defaults to the value passed to setContentTitle().
6050 */
6051 public InboxStyle setBigContentTitle(CharSequence title) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04006052 internalSetBigContentTitle(safeCharSequence(title));
Chris Wrend6297db2012-05-03 16:20:13 -04006053 return this;
6054 }
6055
6056 /**
6057 * Set the first line of text after the detail section in the big form of the template.
6058 */
6059 public InboxStyle setSummaryText(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04006060 internalSetSummaryText(safeCharSequence(cs));
Chris Wrend6297db2012-05-03 16:20:13 -04006061 return this;
6062 }
6063
Chris Wren0bd664d2012-08-01 13:56:56 -04006064 /**
6065 * Append a line to the digest section of the Inbox notification.
6066 */
Daniel Sandler879c5e02012-04-17 16:46:51 -04006067 public InboxStyle addLine(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04006068 mTexts.add(safeCharSequence(cs));
Daniel Sandler879c5e02012-04-17 16:46:51 -04006069 return this;
6070 }
6071
Daniel Sandlerf45564e2013-04-15 15:05:08 -04006072 /**
6073 * @hide
6074 */
6075 public void addExtras(Bundle extras) {
6076 super.addExtras(extras);
Christoph Studer4600f9b2014-07-22 22:44:43 +02006077
Daniel Sandlerf45564e2013-04-15 15:05:08 -04006078 CharSequence[] a = new CharSequence[mTexts.size()];
6079 extras.putCharSequenceArray(EXTRA_TEXT_LINES, mTexts.toArray(a));
6080 }
6081
Christoph Studer4600f9b2014-07-22 22:44:43 +02006082 /**
6083 * @hide
6084 */
6085 @Override
6086 protected void restoreFromExtras(Bundle extras) {
6087 super.restoreFromExtras(extras);
6088
6089 mTexts.clear();
6090 if (extras.containsKey(EXTRA_TEXT_LINES)) {
6091 Collections.addAll(mTexts, extras.getCharSequenceArray(EXTRA_TEXT_LINES));
6092 }
6093 }
6094
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006095 /**
6096 * @hide
6097 */
6098 public RemoteViews makeBigContentView() {
Selim Cinekc848c3a2016-01-13 15:27:30 -08006099 // Remove the content text so it disappears unless you have a summary
Christoph Studer4600f9b2014-07-22 22:44:43 +02006100 // Nasty
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006101 CharSequence oldBuilderContentText = mBuilder.mN.extras.getCharSequence(EXTRA_TEXT);
6102 mBuilder.getAllExtras().putCharSequence(EXTRA_TEXT, null);
Christoph Studer4600f9b2014-07-22 22:44:43 +02006103
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01006104 RemoteViews contentView = getStandardView(mBuilder.getInboxLayoutResource());
Daniel Sandler619738c2012-06-07 16:33:08 -04006105
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006106 mBuilder.getAllExtras().putCharSequence(EXTRA_TEXT, oldBuilderContentText);
Christoph Studer4600f9b2014-07-22 22:44:43 +02006107
Chris Wrend6297db2012-05-03 16:20:13 -04006108 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 -04006109 R.id.inbox_text4, R.id.inbox_text5, R.id.inbox_text6};
Chris Wrend6297db2012-05-03 16:20:13 -04006110
Chris Wren4ed80d52012-05-17 09:30:03 -04006111 // Make sure all rows are gone in case we reuse a view.
6112 for (int rowId : rowIds) {
6113 contentView.setViewVisibility(rowId, View.GONE);
6114 }
6115
Daniel Sandler879c5e02012-04-17 16:46:51 -04006116 int i=0;
Selim Cinek07c80172016-04-21 16:40:47 -07006117 int topPadding = mBuilder.mContext.getResources().getDimensionPixelSize(
6118 R.dimen.notification_inbox_item_top_padding);
Selim Cinek247fa012016-02-18 09:50:48 -08006119 boolean first = true;
Selim Cinek07c80172016-04-21 16:40:47 -07006120 int onlyViewId = 0;
6121 int maxRows = rowIds.length;
6122 if (mBuilder.mActions.size() > 0) {
6123 maxRows--;
6124 }
6125 while (i < mTexts.size() && i < maxRows) {
Daniel Sandler879c5e02012-04-17 16:46:51 -04006126 CharSequence str = mTexts.get(i);
Selim Cinek07c80172016-04-21 16:40:47 -07006127 if (!TextUtils.isEmpty(str)) {
Daniel Sandler879c5e02012-04-17 16:46:51 -04006128 contentView.setViewVisibility(rowIds[i], View.VISIBLE);
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01006129 contentView.setTextViewText(rowIds[i], mBuilder.processLegacyText(str));
Selim Cinek7b9605b2017-01-19 17:36:00 -08006130 mBuilder.setTextViewColorSecondary(contentView, rowIds[i]);
Selim Cinek07c80172016-04-21 16:40:47 -07006131 contentView.setViewPadding(rowIds[i], 0, topPadding, 0, 0);
Selim Cinek247fa012016-02-18 09:50:48 -08006132 handleInboxImageMargin(contentView, rowIds[i], first);
Selim Cinek07c80172016-04-21 16:40:47 -07006133 if (first) {
6134 onlyViewId = rowIds[i];
6135 } else {
6136 onlyViewId = 0;
6137 }
Selim Cinek247fa012016-02-18 09:50:48 -08006138 first = false;
Daniel Sandler879c5e02012-04-17 16:46:51 -04006139 }
6140 i++;
6141 }
Selim Cinek07c80172016-04-21 16:40:47 -07006142 if (onlyViewId != 0) {
6143 // We only have 1 entry, lets make it look like the normal Text of a Bigtext
6144 topPadding = mBuilder.mContext.getResources().getDimensionPixelSize(
6145 R.dimen.notification_text_margin_top);
6146 contentView.setViewPadding(onlyViewId, 0, topPadding, 0, 0);
6147 }
Selim Cinek1e0bf612015-11-20 15:57:26 -08006148
Daniel Sandler879c5e02012-04-17 16:46:51 -04006149 return contentView;
6150 }
Selim Cinek1e0bf612015-11-20 15:57:26 -08006151
Selim Cinek247fa012016-02-18 09:50:48 -08006152 private void handleInboxImageMargin(RemoteViews contentView, int id, boolean first) {
Selim Cinek1e0bf612015-11-20 15:57:26 -08006153 int endMargin = 0;
Selim Cinek247fa012016-02-18 09:50:48 -08006154 if (first) {
6155 final int max = mBuilder.mN.extras.getInt(EXTRA_PROGRESS_MAX, 0);
6156 final boolean ind = mBuilder.mN.extras.getBoolean(EXTRA_PROGRESS_INDETERMINATE);
6157 boolean hasProgress = max != 0 || ind;
Selim Cinek279fa862016-06-14 10:57:25 -07006158 if (mBuilder.mN.hasLargeIcon() && !hasProgress) {
Adrian Roos2d5dbba2016-06-08 17:11:53 -07006159 endMargin = R.dimen.notification_content_picture_margin;
Selim Cinek247fa012016-02-18 09:50:48 -08006160 }
Selim Cinek1e0bf612015-11-20 15:57:26 -08006161 }
Adrian Roos2d5dbba2016-06-08 17:11:53 -07006162 contentView.setViewLayoutMarginEndDimen(id, endMargin);
Selim Cinek1e0bf612015-11-20 15:57:26 -08006163 }
Daniel Sandler879c5e02012-04-17 16:46:51 -04006164 }
Dan Sandler842dd772014-05-15 09:36:47 -04006165
6166 /**
6167 * Notification style for media playback notifications.
6168 *
6169 * In the expanded form, {@link Notification#bigContentView}, up to 5
6170 * {@link Notification.Action}s specified with
Dan Sandler86647982015-05-13 23:41:13 -04006171 * {@link Notification.Builder#addAction(Action) addAction} will be
Dan Sandler842dd772014-05-15 09:36:47 -04006172 * shown as icon-only pushbuttons, suitable for transport controls. The Bitmap given to
6173 * {@link Notification.Builder#setLargeIcon(android.graphics.Bitmap) setLargeIcon()} will be
6174 * treated as album artwork.
Selim Cinek99104832017-01-25 14:47:33 -08006175 * <p>
Dan Sandler842dd772014-05-15 09:36:47 -04006176 * Unlike the other styles provided here, MediaStyle can also modify the standard-size
6177 * {@link Notification#contentView}; by providing action indices to
Christoph Studerfde6f4d2014-12-12 13:23:26 +01006178 * {@link #setShowActionsInCompactView(int...)} you can promote up to 3 actions to be displayed
Dan Sandler842dd772014-05-15 09:36:47 -04006179 * in the standard view alongside the usual content.
Selim Cinek99104832017-01-25 14:47:33 -08006180 * <p>
Bryan Mawhinney6be8de32014-07-18 10:35:12 +01006181 * Notifications created with MediaStyle will have their category set to
6182 * {@link Notification#CATEGORY_TRANSPORT CATEGORY_TRANSPORT} unless you set a different
6183 * category using {@link Notification.Builder#setCategory(String) setCategory()}.
Selim Cinek99104832017-01-25 14:47:33 -08006184 * <p>
Jeff Browndba34ba2014-06-24 20:46:03 -07006185 * Finally, if you attach a {@link android.media.session.MediaSession.Token} using
6186 * {@link android.app.Notification.MediaStyle#setMediaSession(MediaSession.Token)},
Dan Sandler842dd772014-05-15 09:36:47 -04006187 * the System UI can identify this as a notification representing an active media session
6188 * and respond accordingly (by showing album artwork in the lockscreen, for example).
6189 *
Selim Cinek99104832017-01-25 14:47:33 -08006190 * <p>
6191 * Starting at {@link android.os.Build.VERSION_CODES#O Android O} any notification that has a
6192 * media session attached with {@link #setMediaSession(MediaSession.Token)} will be colorized.
6193 * You can opt-out of this behavior by using {@link Notification.Builder#setColorized(boolean)}.
6194 * <p>
6195 *
Dan Sandler842dd772014-05-15 09:36:47 -04006196 * To use this style with your Notification, feed it to
6197 * {@link Notification.Builder#setStyle(android.app.Notification.Style)} like so:
6198 * <pre class="prettyprint">
6199 * Notification noti = new Notification.Builder()
6200 * .setSmallIcon(R.drawable.ic_stat_player)
Christoph Studere935fe92014-11-24 14:18:06 +01006201 * .setContentTitle(&quot;Track title&quot;)
6202 * .setContentText(&quot;Artist - Album&quot;)
6203 * .setLargeIcon(albumArtBitmap))
Bryan Mawhinney6be8de32014-07-18 10:35:12 +01006204 * .setStyle(<b>new Notification.MediaStyle()</b>
6205 * .setMediaSession(mySession))
Dan Sandler842dd772014-05-15 09:36:47 -04006206 * .build();
6207 * </pre>
6208 *
6209 * @see Notification#bigContentView
Selim Cinek99104832017-01-25 14:47:33 -08006210 * @see Notification.Builder#setColorized(boolean)
Dan Sandler842dd772014-05-15 09:36:47 -04006211 */
6212 public static class MediaStyle extends Style {
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02006213 static final int MAX_MEDIA_BUTTONS_IN_COMPACT = 3;
Dan Sandler842dd772014-05-15 09:36:47 -04006214 static final int MAX_MEDIA_BUTTONS = 5;
6215
6216 private int[] mActionsToShowInCompact = null;
Jeff Browndba34ba2014-06-24 20:46:03 -07006217 private MediaSession.Token mToken;
Dan Sandler842dd772014-05-15 09:36:47 -04006218
6219 public MediaStyle() {
6220 }
6221
Adrian Roosf5faf9d2016-05-23 13:56:15 -07006222 /**
6223 * @deprecated use {@code MediaStyle()}.
6224 */
6225 @Deprecated
Dan Sandler842dd772014-05-15 09:36:47 -04006226 public MediaStyle(Builder builder) {
6227 setBuilder(builder);
6228 }
6229
6230 /**
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02006231 * 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 -04006232 * notification view.
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02006233 *
6234 * @param actions the indices of the actions to show in the compact notification view
Dan Sandler842dd772014-05-15 09:36:47 -04006235 */
6236 public MediaStyle setShowActionsInCompactView(int...actions) {
6237 mActionsToShowInCompact = actions;
6238 return this;
6239 }
6240
6241 /**
Jeff Browndba34ba2014-06-24 20:46:03 -07006242 * Attach a {@link android.media.session.MediaSession.Token} to this Notification
6243 * to provide additional playback information and control to the SystemUI.
Dan Sandler842dd772014-05-15 09:36:47 -04006244 */
Jeff Browndba34ba2014-06-24 20:46:03 -07006245 public MediaStyle setMediaSession(MediaSession.Token token) {
Dan Sandler842dd772014-05-15 09:36:47 -04006246 mToken = token;
6247 return this;
6248 }
6249
Christoph Studer4600f9b2014-07-22 22:44:43 +02006250 /**
6251 * @hide
6252 */
Dan Sandler842dd772014-05-15 09:36:47 -04006253 @Override
6254 public Notification buildStyled(Notification wip) {
Christoph Studer4600f9b2014-07-22 22:44:43 +02006255 super.buildStyled(wip);
Bryan Mawhinney6be8de32014-07-18 10:35:12 +01006256 if (wip.category == null) {
6257 wip.category = Notification.CATEGORY_TRANSPORT;
6258 }
Dan Sandler842dd772014-05-15 09:36:47 -04006259 return wip;
6260 }
6261
Christoph Studer4600f9b2014-07-22 22:44:43 +02006262 /**
6263 * @hide
6264 */
6265 @Override
Selim Cinek7d1009b2017-01-25 15:28:28 -08006266 public RemoteViews makeContentView(boolean increasedHeight) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006267 return makeMediaContentView();
Christoph Studer4600f9b2014-07-22 22:44:43 +02006268 }
6269
6270 /**
6271 * @hide
6272 */
6273 @Override
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006274 public RemoteViews makeBigContentView() {
6275 return makeMediaBigContentView();
Christoph Studer4600f9b2014-07-22 22:44:43 +02006276 }
6277
Selim Cinekcc10bfb2016-02-10 16:24:21 -08006278 /**
6279 * @hide
6280 */
6281 @Override
Selim Cinek87ed69b2017-02-09 15:59:43 -08006282 public RemoteViews makeHeadsUpContentView(boolean increasedHeight) {
Selim Cinekcc10bfb2016-02-10 16:24:21 -08006283 RemoteViews expanded = makeMediaBigContentView();
6284 return expanded != null ? expanded : makeMediaContentView();
6285 }
6286
Dan Sandler842dd772014-05-15 09:36:47 -04006287 /** @hide */
6288 @Override
6289 public void addExtras(Bundle extras) {
6290 super.addExtras(extras);
6291
6292 if (mToken != null) {
6293 extras.putParcelable(EXTRA_MEDIA_SESSION, mToken);
6294 }
Bryan Mawhinneye191f902014-07-22 12:50:09 +01006295 if (mActionsToShowInCompact != null) {
6296 extras.putIntArray(EXTRA_COMPACT_ACTIONS, mActionsToShowInCompact);
6297 }
Dan Sandler842dd772014-05-15 09:36:47 -04006298 }
6299
Christoph Studer4600f9b2014-07-22 22:44:43 +02006300 /**
6301 * @hide
6302 */
6303 @Override
6304 protected void restoreFromExtras(Bundle extras) {
6305 super.restoreFromExtras(extras);
6306
6307 if (extras.containsKey(EXTRA_MEDIA_SESSION)) {
6308 mToken = extras.getParcelable(EXTRA_MEDIA_SESSION);
6309 }
6310 if (extras.containsKey(EXTRA_COMPACT_ACTIONS)) {
6311 mActionsToShowInCompact = extras.getIntArray(EXTRA_COMPACT_ACTIONS);
6312 }
6313 }
6314
Selim Cinek5bf069a2015-11-10 19:14:27 -05006315 private RemoteViews generateMediaActionButton(Action action, int color) {
Dan Sandler842dd772014-05-15 09:36:47 -04006316 final boolean tombstone = (action.actionIntent == null);
Selim Cinekf33b1112015-07-15 17:45:11 -07006317 RemoteViews button = new BuilderRemoteViews(mBuilder.mContext.getApplicationInfo(),
Alan Viverette3cb07a462014-06-06 14:19:53 -07006318 R.layout.notification_material_media_action);
Dan Sandler68079d52015-07-22 10:45:30 -04006319 button.setImageViewIcon(R.id.action0, action.getIcon());
Selim Cinek5bf069a2015-11-10 19:14:27 -05006320 button.setDrawableParameters(R.id.action0, false, -1, color, PorterDuff.Mode.SRC_ATOP,
6321 -1);
Dan Sandler842dd772014-05-15 09:36:47 -04006322 if (!tombstone) {
6323 button.setOnClickPendingIntent(R.id.action0, action.actionIntent);
6324 }
6325 button.setContentDescription(R.id.action0, action.title);
6326 return button;
6327 }
6328
6329 private RemoteViews makeMediaContentView() {
6330 RemoteViews view = mBuilder.applyStandardTemplate(
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02006331 R.layout.notification_template_material_media, false /* hasProgress */);
Dan Sandler842dd772014-05-15 09:36:47 -04006332
6333 final int numActions = mBuilder.mActions.size();
6334 final int N = mActionsToShowInCompact == null
6335 ? 0
6336 : Math.min(mActionsToShowInCompact.length, MAX_MEDIA_BUTTONS_IN_COMPACT);
6337 if (N > 0) {
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02006338 view.removeAllViews(com.android.internal.R.id.media_actions);
Dan Sandler842dd772014-05-15 09:36:47 -04006339 for (int i = 0; i < N; i++) {
6340 if (i >= numActions) {
6341 throw new IllegalArgumentException(String.format(
6342 "setShowActionsInCompactView: action %d out of bounds (max %d)",
6343 i, numActions - 1));
6344 }
6345
6346 final Action action = mBuilder.mActions.get(mActionsToShowInCompact[i]);
Selim Cinek5bf069a2015-11-10 19:14:27 -05006347 final RemoteViews button = generateMediaActionButton(action,
Selim Cinek99104832017-01-25 14:47:33 -08006348 getPrimaryHighlightColor());
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02006349 view.addView(com.android.internal.R.id.media_actions, button);
Dan Sandler842dd772014-05-15 09:36:47 -04006350 }
6351 }
Selim Cinekfdc738f2016-01-27 20:04:27 -08006352 handleImage(view);
6353 // handle the content margin
Adrian Roos2d5dbba2016-06-08 17:11:53 -07006354 int endMargin = R.dimen.notification_content_margin_end;
Selim Cinek279fa862016-06-14 10:57:25 -07006355 if (mBuilder.mN.hasLargeIcon()) {
Adrian Roos2d5dbba2016-06-08 17:11:53 -07006356 endMargin = R.dimen.notification_content_plus_picture_margin_end;
Selim Cinekfdc738f2016-01-27 20:04:27 -08006357 }
Adrian Roos2d5dbba2016-06-08 17:11:53 -07006358 view.setViewLayoutMarginEndDimen(R.id.notification_main_column, endMargin);
Dan Sandler842dd772014-05-15 09:36:47 -04006359 return view;
6360 }
6361
Selim Cinek99104832017-01-25 14:47:33 -08006362 private int getPrimaryHighlightColor() {
6363 return mBuilder.getPrimaryHighlightColor();
6364 }
6365
Dan Sandler842dd772014-05-15 09:36:47 -04006366 private RemoteViews makeMediaBigContentView() {
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02006367 final int actionCount = Math.min(mBuilder.mActions.size(), MAX_MEDIA_BUTTONS);
Selim Cinekcc10bfb2016-02-10 16:24:21 -08006368 // Dont add an expanded view if there is no more content to be revealed
6369 int actionsInCompact = mActionsToShowInCompact == null
6370 ? 0
6371 : Math.min(mActionsToShowInCompact.length, MAX_MEDIA_BUTTONS_IN_COMPACT);
Selim Cinek279fa862016-06-14 10:57:25 -07006372 if (!mBuilder.mN.hasLargeIcon() && actionCount <= actionsInCompact) {
Selim Cinekcc10bfb2016-02-10 16:24:21 -08006373 return null;
6374 }
Selim Cinek5bf069a2015-11-10 19:14:27 -05006375 RemoteViews big = mBuilder.applyStandardTemplate(
6376 R.layout.notification_template_material_big_media,
6377 false);
Dan Sandler842dd772014-05-15 09:36:47 -04006378
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02006379 if (actionCount > 0) {
6380 big.removeAllViews(com.android.internal.R.id.media_actions);
6381 for (int i = 0; i < actionCount; i++) {
Selim Cinek5bf069a2015-11-10 19:14:27 -05006382 final RemoteViews button = generateMediaActionButton(mBuilder.mActions.get(i),
Selim Cinek99104832017-01-25 14:47:33 -08006383 getPrimaryHighlightColor());
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02006384 big.addView(com.android.internal.R.id.media_actions, button);
Dan Sandler842dd772014-05-15 09:36:47 -04006385 }
6386 }
Selim Cinek5bf069a2015-11-10 19:14:27 -05006387 handleImage(big);
Dan Sandler842dd772014-05-15 09:36:47 -04006388 return big;
6389 }
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02006390
Selim Cinek5bf069a2015-11-10 19:14:27 -05006391 private void handleImage(RemoteViews contentView) {
Selim Cinek279fa862016-06-14 10:57:25 -07006392 if (mBuilder.mN.hasLargeIcon()) {
Adrian Roos2d5dbba2016-06-08 17:11:53 -07006393 contentView.setViewLayoutMarginEndDimen(R.id.line1, 0);
6394 contentView.setViewLayoutMarginEndDimen(R.id.text, 0);
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02006395 }
6396 }
6397
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02006398 /**
6399 * @hide
6400 */
6401 @Override
6402 protected boolean hasProgress() {
6403 return false;
6404 }
Dan Sandler842dd772014-05-15 09:36:47 -04006405 }
Griff Hazen61a9e862014-05-22 16:05:19 -07006406
Selim Cinek593610c2016-02-16 18:42:57 -08006407 /**
6408 * Notification style for custom views that are decorated by the system
6409 *
6410 * <p>Instead of providing a notification that is completely custom, a developer can set this
6411 * style and still obtain system decorations like the notification header with the expand
6412 * affordance and actions.
6413 *
6414 * <p>Use {@link android.app.Notification.Builder#setCustomContentView(RemoteViews)},
6415 * {@link android.app.Notification.Builder#setCustomBigContentView(RemoteViews)} and
6416 * {@link android.app.Notification.Builder#setCustomHeadsUpContentView(RemoteViews)} to set the
6417 * corresponding custom views to display.
6418 *
6419 * To use this style with your Notification, feed it to
6420 * {@link Notification.Builder#setStyle(android.app.Notification.Style)} like so:
6421 * <pre class="prettyprint">
6422 * Notification noti = new Notification.Builder()
6423 * .setSmallIcon(R.drawable.ic_stat_player)
6424 * .setLargeIcon(albumArtBitmap))
6425 * .setCustomContentView(contentView);
6426 * .setStyle(<b>new Notification.DecoratedCustomViewStyle()</b>)
6427 * .build();
6428 * </pre>
6429 */
6430 public static class DecoratedCustomViewStyle extends Style {
6431
6432 public DecoratedCustomViewStyle() {
6433 }
6434
Selim Cinek593610c2016-02-16 18:42:57 -08006435 /**
6436 * @hide
6437 */
6438 public boolean displayCustomViewInline() {
6439 return true;
6440 }
6441
6442 /**
6443 * @hide
6444 */
6445 @Override
Selim Cinek7d1009b2017-01-25 15:28:28 -08006446 public RemoteViews makeContentView(boolean increasedHeight) {
Selim Cinek593610c2016-02-16 18:42:57 -08006447 return makeStandardTemplateWithCustomContent(mBuilder.mN.contentView);
6448 }
6449
6450 /**
6451 * @hide
6452 */
6453 @Override
6454 public RemoteViews makeBigContentView() {
6455 return makeDecoratedBigContentView();
6456 }
6457
6458 /**
6459 * @hide
6460 */
6461 @Override
Selim Cinek87ed69b2017-02-09 15:59:43 -08006462 public RemoteViews makeHeadsUpContentView(boolean increasedHeight) {
Selim Cinek593610c2016-02-16 18:42:57 -08006463 return makeDecoratedHeadsUpContentView();
6464 }
6465
Selim Cinek593610c2016-02-16 18:42:57 -08006466 private RemoteViews makeDecoratedHeadsUpContentView() {
6467 RemoteViews headsUpContentView = mBuilder.mN.headsUpContentView == null
6468 ? mBuilder.mN.contentView
6469 : mBuilder.mN.headsUpContentView;
6470 if (mBuilder.mActions.size() == 0) {
6471 return makeStandardTemplateWithCustomContent(headsUpContentView);
6472 }
6473 RemoteViews remoteViews = mBuilder.applyStandardTemplateWithActions(
6474 mBuilder.getBigBaseLayoutResource());
Selim Cinek247fa012016-02-18 09:50:48 -08006475 buildIntoRemoteViewContent(remoteViews, headsUpContentView);
Selim Cinek593610c2016-02-16 18:42:57 -08006476 return remoteViews;
6477 }
6478
Selim Cinek593610c2016-02-16 18:42:57 -08006479 private RemoteViews makeStandardTemplateWithCustomContent(RemoteViews customContent) {
6480 RemoteViews remoteViews = mBuilder.applyStandardTemplate(
6481 mBuilder.getBaseLayoutResource());
Selim Cinek247fa012016-02-18 09:50:48 -08006482 buildIntoRemoteViewContent(remoteViews, customContent);
Selim Cinek593610c2016-02-16 18:42:57 -08006483 return remoteViews;
6484 }
6485
Selim Cinek593610c2016-02-16 18:42:57 -08006486 private RemoteViews makeDecoratedBigContentView() {
6487 RemoteViews bigContentView = mBuilder.mN.bigContentView == null
6488 ? mBuilder.mN.contentView
6489 : mBuilder.mN.bigContentView;
6490 if (mBuilder.mActions.size() == 0) {
6491 return makeStandardTemplateWithCustomContent(bigContentView);
6492 }
6493 RemoteViews remoteViews = mBuilder.applyStandardTemplateWithActions(
6494 mBuilder.getBigBaseLayoutResource());
Selim Cinek247fa012016-02-18 09:50:48 -08006495 buildIntoRemoteViewContent(remoteViews, bigContentView);
Selim Cinek593610c2016-02-16 18:42:57 -08006496 return remoteViews;
6497 }
Selim Cinek247fa012016-02-18 09:50:48 -08006498
6499 private void buildIntoRemoteViewContent(RemoteViews remoteViews,
6500 RemoteViews customContent) {
Adrian Roos5081c0d2016-02-26 16:04:19 -08006501 if (customContent != null) {
Selim Cinekf91017e2016-03-14 12:25:09 -07006502 // Need to clone customContent before adding, because otherwise it can no longer be
6503 // parceled independently of remoteViews.
Adrian Roos5081c0d2016-02-26 16:04:19 -08006504 customContent = customContent.clone();
Selim Cinekf91017e2016-03-14 12:25:09 -07006505 remoteViews.removeAllViews(R.id.notification_main_column);
6506 remoteViews.addView(R.id.notification_main_column, customContent);
Adrian Roos5081c0d2016-02-26 16:04:19 -08006507 }
Selim Cinek247fa012016-02-18 09:50:48 -08006508 // also update the end margin if there is an image
Adrian Roos2d5dbba2016-06-08 17:11:53 -07006509 int endMargin = R.dimen.notification_content_margin_end;
Selim Cinek279fa862016-06-14 10:57:25 -07006510 if (mBuilder.mN.hasLargeIcon()) {
Adrian Roos2d5dbba2016-06-08 17:11:53 -07006511 endMargin = R.dimen.notification_content_plus_picture_margin_end;
Selim Cinek247fa012016-02-18 09:50:48 -08006512 }
Adrian Roos2d5dbba2016-06-08 17:11:53 -07006513 remoteViews.setViewLayoutMarginEndDimen(R.id.notification_main_column, endMargin);
Selim Cinek247fa012016-02-18 09:50:48 -08006514 }
Selim Cinek593610c2016-02-16 18:42:57 -08006515 }
6516
Selim Cinek03eb3b72016-02-18 10:39:45 -08006517 /**
6518 * Notification style for media custom views that are decorated by the system
6519 *
6520 * <p>Instead of providing a media notification that is completely custom, a developer can set
6521 * this style and still obtain system decorations like the notification header with the expand
6522 * affordance and actions.
6523 *
6524 * <p>Use {@link android.app.Notification.Builder#setCustomContentView(RemoteViews)},
6525 * {@link android.app.Notification.Builder#setCustomBigContentView(RemoteViews)} and
6526 * {@link android.app.Notification.Builder#setCustomHeadsUpContentView(RemoteViews)} to set the
6527 * corresponding custom views to display.
Selim Cinek99104832017-01-25 14:47:33 -08006528 * <p>
6529 * Contrary to {@link MediaStyle} a developer has to opt-in to the colorizing of the
6530 * notification by using {@link Notification.Builder#setColorized(boolean)}.
6531 * <p>
Selim Cinek03eb3b72016-02-18 10:39:45 -08006532 * To use this style with your Notification, feed it to
6533 * {@link Notification.Builder#setStyle(android.app.Notification.Style)} like so:
6534 * <pre class="prettyprint">
6535 * Notification noti = new Notification.Builder()
6536 * .setSmallIcon(R.drawable.ic_stat_player)
6537 * .setLargeIcon(albumArtBitmap))
6538 * .setCustomContentView(contentView);
6539 * .setStyle(<b>new Notification.DecoratedMediaCustomViewStyle()</b>
6540 * .setMediaSession(mySession))
6541 * .build();
6542 * </pre>
6543 *
6544 * @see android.app.Notification.DecoratedCustomViewStyle
6545 * @see android.app.Notification.MediaStyle
6546 */
6547 public static class DecoratedMediaCustomViewStyle extends MediaStyle {
6548
6549 public DecoratedMediaCustomViewStyle() {
6550 }
6551
Selim Cinek03eb3b72016-02-18 10:39:45 -08006552 /**
6553 * @hide
6554 */
6555 public boolean displayCustomViewInline() {
6556 return true;
6557 }
6558
6559 /**
6560 * @hide
6561 */
6562 @Override
Selim Cinek7d1009b2017-01-25 15:28:28 -08006563 public RemoteViews makeContentView(boolean increasedHeight) {
6564 RemoteViews remoteViews = super.makeContentView(false /* increasedHeight */);
Selim Cinek03eb3b72016-02-18 10:39:45 -08006565 return buildIntoRemoteView(remoteViews, R.id.notification_content_container,
6566 mBuilder.mN.contentView);
6567 }
6568
6569 /**
6570 * @hide
6571 */
6572 @Override
6573 public RemoteViews makeBigContentView() {
6574 RemoteViews customRemoteView = mBuilder.mN.bigContentView != null
6575 ? mBuilder.mN.bigContentView
6576 : mBuilder.mN.contentView;
6577 return makeBigContentViewWithCustomContent(customRemoteView);
6578 }
6579
6580 private RemoteViews makeBigContentViewWithCustomContent(RemoteViews customRemoteView) {
6581 RemoteViews remoteViews = super.makeBigContentView();
6582 if (remoteViews != null) {
6583 return buildIntoRemoteView(remoteViews, R.id.notification_main_column,
6584 customRemoteView);
6585 } else if (customRemoteView != mBuilder.mN.contentView){
Selim Cinek7d1009b2017-01-25 15:28:28 -08006586 remoteViews = super.makeContentView(false /* increasedHeight */);
Selim Cinek03eb3b72016-02-18 10:39:45 -08006587 return buildIntoRemoteView(remoteViews, R.id.notification_content_container,
6588 customRemoteView);
6589 } else {
6590 return null;
6591 }
6592 }
6593
6594 /**
6595 * @hide
6596 */
6597 @Override
Selim Cinek87ed69b2017-02-09 15:59:43 -08006598 public RemoteViews makeHeadsUpContentView(boolean increasedHeight) {
Selim Cinek03eb3b72016-02-18 10:39:45 -08006599 RemoteViews customRemoteView = mBuilder.mN.headsUpContentView != null
6600 ? mBuilder.mN.headsUpContentView
6601 : mBuilder.mN.contentView;
6602 return makeBigContentViewWithCustomContent(customRemoteView);
6603 }
6604
6605 private RemoteViews buildIntoRemoteView(RemoteViews remoteViews, int id,
6606 RemoteViews customContent) {
Adrian Roos5081c0d2016-02-26 16:04:19 -08006607 if (customContent != null) {
Selim Cinekf91017e2016-03-14 12:25:09 -07006608 // Need to clone customContent before adding, because otherwise it can no longer be
6609 // parceled independently of remoteViews.
Adrian Roos5081c0d2016-02-26 16:04:19 -08006610 customContent = customContent.clone();
Selim Cinekf91017e2016-03-14 12:25:09 -07006611 remoteViews.removeAllViews(id);
6612 remoteViews.addView(id, customContent);
Adrian Roos5081c0d2016-02-26 16:04:19 -08006613 }
Selim Cinek03eb3b72016-02-18 10:39:45 -08006614 return remoteViews;
6615 }
6616 }
6617
Christoph Studer4600f9b2014-07-22 22:44:43 +02006618 // When adding a new Style subclass here, don't forget to update
6619 // Builder.getNotificationStyleClass.
6620
Griff Hazen61a9e862014-05-22 16:05:19 -07006621 /**
6622 * Extender interface for use with {@link Builder#extend}. Extenders may be used to add
6623 * metadata or change options on a notification builder.
6624 */
6625 public interface Extender {
6626 /**
6627 * Apply this extender to a notification builder.
6628 * @param builder the builder to be modified.
6629 * @return the build object for chaining.
6630 */
6631 public Builder extend(Builder builder);
6632 }
6633
6634 /**
6635 * Helper class to add wearable extensions to notifications.
6636 * <p class="note"> See
6637 * <a href="{@docRoot}wear/notifications/creating.html">Creating Notifications
6638 * for Android Wear</a> for more information on how to use this class.
6639 * <p>
6640 * To create a notification with wearable extensions:
6641 * <ol>
6642 * <li>Create a {@link android.app.Notification.Builder}, setting any desired
6643 * properties.
6644 * <li>Create a {@link android.app.Notification.WearableExtender}.
6645 * <li>Set wearable-specific properties using the
6646 * {@code add} and {@code set} methods of {@link android.app.Notification.WearableExtender}.
6647 * <li>Call {@link android.app.Notification.Builder#extend} to apply the extensions to a
6648 * notification.
6649 * <li>Post the notification to the notification system with the
6650 * {@code NotificationManager.notify(...)} methods.
6651 * </ol>
6652 *
6653 * <pre class="prettyprint">
6654 * Notification notif = new Notification.Builder(mContext)
6655 * .setContentTitle(&quot;New mail from &quot; + sender.toString())
6656 * .setContentText(subject)
6657 * .setSmallIcon(R.drawable.new_mail)
6658 * .extend(new Notification.WearableExtender()
6659 * .setContentIcon(R.drawable.new_mail))
6660 * .build();
6661 * NotificationManager notificationManger =
6662 * (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
6663 * notificationManger.notify(0, notif);</pre>
6664 *
6665 * <p>Wearable extensions can be accessed on an existing notification by using the
6666 * {@code WearableExtender(Notification)} constructor,
6667 * and then using the {@code get} methods to access values.
6668 *
6669 * <pre class="prettyprint">
6670 * Notification.WearableExtender wearableExtender = new Notification.WearableExtender(
6671 * notification);
Griff Hazen14f57992014-05-26 09:07:14 -07006672 * List&lt;Notification&gt; pages = wearableExtender.getPages();</pre>
Griff Hazen61a9e862014-05-22 16:05:19 -07006673 */
6674 public static final class WearableExtender implements Extender {
6675 /**
6676 * Sentinel value for an action index that is unset.
6677 */
6678 public static final int UNSET_ACTION_INDEX = -1;
6679
6680 /**
6681 * Size value for use with {@link #setCustomSizePreset} to show this notification with
6682 * default sizing.
6683 * <p>For custom display notifications created using {@link #setDisplayIntent},
Paul Soulosaa4f4bf2015-08-04 11:59:45 -07006684 * the default is {@link #SIZE_MEDIUM}. All other notifications size automatically based
Griff Hazen61a9e862014-05-22 16:05:19 -07006685 * on their content.
6686 */
6687 public static final int SIZE_DEFAULT = 0;
6688
6689 /**
6690 * Size value for use with {@link #setCustomSizePreset} to show this notification
6691 * with an extra small size.
6692 * <p>This value is only applicable for custom display notifications created using
6693 * {@link #setDisplayIntent}.
6694 */
6695 public static final int SIZE_XSMALL = 1;
6696
6697 /**
6698 * Size value for use with {@link #setCustomSizePreset} to show this notification
6699 * with a small size.
6700 * <p>This value is only applicable for custom display notifications created using
6701 * {@link #setDisplayIntent}.
6702 */
6703 public static final int SIZE_SMALL = 2;
6704
6705 /**
6706 * Size value for use with {@link #setCustomSizePreset} to show this notification
6707 * with a medium size.
6708 * <p>This value is only applicable for custom display notifications created using
6709 * {@link #setDisplayIntent}.
6710 */
6711 public static final int SIZE_MEDIUM = 3;
6712
6713 /**
6714 * Size value for use with {@link #setCustomSizePreset} to show this notification
6715 * with a large size.
6716 * <p>This value is only applicable for custom display notifications created using
6717 * {@link #setDisplayIntent}.
6718 */
6719 public static final int SIZE_LARGE = 4;
6720
Griff Hazend5f11f92014-05-27 15:40:09 -07006721 /**
6722 * Size value for use with {@link #setCustomSizePreset} to show this notification
6723 * full screen.
6724 * <p>This value is only applicable for custom display notifications created using
6725 * {@link #setDisplayIntent}.
6726 */
6727 public static final int SIZE_FULL_SCREEN = 5;
6728
Griff Hazen5f2edfc2014-09-29 16:28:44 -07006729 /**
6730 * Sentinel value for use with {@link #setHintScreenTimeout} to keep the screen on for a
6731 * short amount of time when this notification is displayed on the screen. This
6732 * is the default value.
6733 */
6734 public static final int SCREEN_TIMEOUT_SHORT = 0;
6735
6736 /**
6737 * Sentinel value for use with {@link #setHintScreenTimeout} to keep the screen on
6738 * for a longer amount of time when this notification is displayed on the screen.
6739 */
6740 public static final int SCREEN_TIMEOUT_LONG = -1;
6741
Griff Hazen61a9e862014-05-22 16:05:19 -07006742 /** Notification extra which contains wearable extensions */
6743 private static final String EXTRA_WEARABLE_EXTENSIONS = "android.wearable.EXTENSIONS";
6744
Pete Gastaf6781d2014-10-07 15:17:05 -04006745 // Keys within EXTRA_WEARABLE_EXTENSIONS for wearable options.
Griff Hazen61a9e862014-05-22 16:05:19 -07006746 private static final String KEY_ACTIONS = "actions";
6747 private static final String KEY_FLAGS = "flags";
6748 private static final String KEY_DISPLAY_INTENT = "displayIntent";
6749 private static final String KEY_PAGES = "pages";
6750 private static final String KEY_BACKGROUND = "background";
6751 private static final String KEY_CONTENT_ICON = "contentIcon";
6752 private static final String KEY_CONTENT_ICON_GRAVITY = "contentIconGravity";
6753 private static final String KEY_CONTENT_ACTION_INDEX = "contentActionIndex";
6754 private static final String KEY_CUSTOM_SIZE_PRESET = "customSizePreset";
6755 private static final String KEY_CUSTOM_CONTENT_HEIGHT = "customContentHeight";
6756 private static final String KEY_GRAVITY = "gravity";
Griff Hazen5f2edfc2014-09-29 16:28:44 -07006757 private static final String KEY_HINT_SCREEN_TIMEOUT = "hintScreenTimeout";
Nadia Benbernou948627e2016-04-14 14:41:08 -04006758 private static final String KEY_DISMISSAL_ID = "dismissalId";
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04006759 private static final String KEY_BRIDGE_TAG = "bridgeTag";
Griff Hazen61a9e862014-05-22 16:05:19 -07006760
6761 // Flags bitwise-ored to mFlags
6762 private static final int FLAG_CONTENT_INTENT_AVAILABLE_OFFLINE = 0x1;
6763 private static final int FLAG_HINT_HIDE_ICON = 1 << 1;
6764 private static final int FLAG_HINT_SHOW_BACKGROUND_ONLY = 1 << 2;
6765 private static final int FLAG_START_SCROLL_BOTTOM = 1 << 3;
Griff Hazen5f2edfc2014-09-29 16:28:44 -07006766 private static final int FLAG_HINT_AVOID_BACKGROUND_CLIPPING = 1 << 4;
Alex Hills4bcb06b2016-04-05 14:26:25 -04006767 private static final int FLAG_BIG_PICTURE_AMBIENT = 1 << 5;
Alex Hills9ab3a232016-04-05 14:54:56 -04006768 private static final int FLAG_HINT_CONTENT_INTENT_LAUNCHES_ACTIVITY = 1 << 6;
Griff Hazen61a9e862014-05-22 16:05:19 -07006769
6770 // Default value for flags integer
6771 private static final int DEFAULT_FLAGS = FLAG_CONTENT_INTENT_AVAILABLE_OFFLINE;
6772
6773 private static final int DEFAULT_CONTENT_ICON_GRAVITY = Gravity.END;
6774 private static final int DEFAULT_GRAVITY = Gravity.BOTTOM;
6775
6776 private ArrayList<Action> mActions = new ArrayList<Action>();
6777 private int mFlags = DEFAULT_FLAGS;
6778 private PendingIntent mDisplayIntent;
6779 private ArrayList<Notification> mPages = new ArrayList<Notification>();
6780 private Bitmap mBackground;
6781 private int mContentIcon;
6782 private int mContentIconGravity = DEFAULT_CONTENT_ICON_GRAVITY;
6783 private int mContentActionIndex = UNSET_ACTION_INDEX;
6784 private int mCustomSizePreset = SIZE_DEFAULT;
6785 private int mCustomContentHeight;
6786 private int mGravity = DEFAULT_GRAVITY;
Griff Hazen5f2edfc2014-09-29 16:28:44 -07006787 private int mHintScreenTimeout;
Nadia Benbernou948627e2016-04-14 14:41:08 -04006788 private String mDismissalId;
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04006789 private String mBridgeTag;
Griff Hazen61a9e862014-05-22 16:05:19 -07006790
6791 /**
6792 * Create a {@link android.app.Notification.WearableExtender} with default
6793 * options.
6794 */
6795 public WearableExtender() {
6796 }
6797
6798 public WearableExtender(Notification notif) {
6799 Bundle wearableBundle = notif.extras.getBundle(EXTRA_WEARABLE_EXTENSIONS);
6800 if (wearableBundle != null) {
6801 List<Action> actions = wearableBundle.getParcelableArrayList(KEY_ACTIONS);
6802 if (actions != null) {
6803 mActions.addAll(actions);
6804 }
6805
6806 mFlags = wearableBundle.getInt(KEY_FLAGS, DEFAULT_FLAGS);
6807 mDisplayIntent = wearableBundle.getParcelable(KEY_DISPLAY_INTENT);
6808
6809 Notification[] pages = getNotificationArrayFromBundle(
6810 wearableBundle, KEY_PAGES);
6811 if (pages != null) {
6812 Collections.addAll(mPages, pages);
6813 }
6814
6815 mBackground = wearableBundle.getParcelable(KEY_BACKGROUND);
6816 mContentIcon = wearableBundle.getInt(KEY_CONTENT_ICON);
6817 mContentIconGravity = wearableBundle.getInt(KEY_CONTENT_ICON_GRAVITY,
6818 DEFAULT_CONTENT_ICON_GRAVITY);
6819 mContentActionIndex = wearableBundle.getInt(KEY_CONTENT_ACTION_INDEX,
6820 UNSET_ACTION_INDEX);
6821 mCustomSizePreset = wearableBundle.getInt(KEY_CUSTOM_SIZE_PRESET,
6822 SIZE_DEFAULT);
6823 mCustomContentHeight = wearableBundle.getInt(KEY_CUSTOM_CONTENT_HEIGHT);
6824 mGravity = wearableBundle.getInt(KEY_GRAVITY, DEFAULT_GRAVITY);
Griff Hazen5f2edfc2014-09-29 16:28:44 -07006825 mHintScreenTimeout = wearableBundle.getInt(KEY_HINT_SCREEN_TIMEOUT);
Nadia Benbernou948627e2016-04-14 14:41:08 -04006826 mDismissalId = wearableBundle.getString(KEY_DISMISSAL_ID);
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04006827 mBridgeTag = wearableBundle.getString(KEY_BRIDGE_TAG);
Griff Hazen61a9e862014-05-22 16:05:19 -07006828 }
6829 }
6830
6831 /**
6832 * Apply wearable extensions to a notification that is being built. This is typically
6833 * called by the {@link android.app.Notification.Builder#extend} method of
6834 * {@link android.app.Notification.Builder}.
6835 */
6836 @Override
6837 public Notification.Builder extend(Notification.Builder builder) {
6838 Bundle wearableBundle = new Bundle();
6839
6840 if (!mActions.isEmpty()) {
6841 wearableBundle.putParcelableArrayList(KEY_ACTIONS, mActions);
6842 }
6843 if (mFlags != DEFAULT_FLAGS) {
6844 wearableBundle.putInt(KEY_FLAGS, mFlags);
6845 }
6846 if (mDisplayIntent != null) {
6847 wearableBundle.putParcelable(KEY_DISPLAY_INTENT, mDisplayIntent);
6848 }
6849 if (!mPages.isEmpty()) {
6850 wearableBundle.putParcelableArray(KEY_PAGES, mPages.toArray(
6851 new Notification[mPages.size()]));
6852 }
6853 if (mBackground != null) {
6854 wearableBundle.putParcelable(KEY_BACKGROUND, mBackground);
6855 }
6856 if (mContentIcon != 0) {
6857 wearableBundle.putInt(KEY_CONTENT_ICON, mContentIcon);
6858 }
6859 if (mContentIconGravity != DEFAULT_CONTENT_ICON_GRAVITY) {
6860 wearableBundle.putInt(KEY_CONTENT_ICON_GRAVITY, mContentIconGravity);
6861 }
6862 if (mContentActionIndex != UNSET_ACTION_INDEX) {
6863 wearableBundle.putInt(KEY_CONTENT_ACTION_INDEX,
6864 mContentActionIndex);
6865 }
6866 if (mCustomSizePreset != SIZE_DEFAULT) {
6867 wearableBundle.putInt(KEY_CUSTOM_SIZE_PRESET, mCustomSizePreset);
6868 }
6869 if (mCustomContentHeight != 0) {
6870 wearableBundle.putInt(KEY_CUSTOM_CONTENT_HEIGHT, mCustomContentHeight);
6871 }
6872 if (mGravity != DEFAULT_GRAVITY) {
6873 wearableBundle.putInt(KEY_GRAVITY, mGravity);
6874 }
Griff Hazen5f2edfc2014-09-29 16:28:44 -07006875 if (mHintScreenTimeout != 0) {
6876 wearableBundle.putInt(KEY_HINT_SCREEN_TIMEOUT, mHintScreenTimeout);
6877 }
Nadia Benbernou948627e2016-04-14 14:41:08 -04006878 if (mDismissalId != null) {
6879 wearableBundle.putString(KEY_DISMISSAL_ID, mDismissalId);
6880 }
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04006881 if (mBridgeTag != null) {
6882 wearableBundle.putString(KEY_BRIDGE_TAG, mBridgeTag);
6883 }
Griff Hazen61a9e862014-05-22 16:05:19 -07006884
6885 builder.getExtras().putBundle(EXTRA_WEARABLE_EXTENSIONS, wearableBundle);
6886 return builder;
6887 }
6888
6889 @Override
6890 public WearableExtender clone() {
6891 WearableExtender that = new WearableExtender();
6892 that.mActions = new ArrayList<Action>(this.mActions);
6893 that.mFlags = this.mFlags;
6894 that.mDisplayIntent = this.mDisplayIntent;
6895 that.mPages = new ArrayList<Notification>(this.mPages);
6896 that.mBackground = this.mBackground;
6897 that.mContentIcon = this.mContentIcon;
6898 that.mContentIconGravity = this.mContentIconGravity;
6899 that.mContentActionIndex = this.mContentActionIndex;
6900 that.mCustomSizePreset = this.mCustomSizePreset;
6901 that.mCustomContentHeight = this.mCustomContentHeight;
6902 that.mGravity = this.mGravity;
Griff Hazen5f2edfc2014-09-29 16:28:44 -07006903 that.mHintScreenTimeout = this.mHintScreenTimeout;
Nadia Benbernou948627e2016-04-14 14:41:08 -04006904 that.mDismissalId = this.mDismissalId;
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04006905 that.mBridgeTag = this.mBridgeTag;
Griff Hazen61a9e862014-05-22 16:05:19 -07006906 return that;
6907 }
6908
6909 /**
6910 * Add a wearable action to this notification.
6911 *
6912 * <p>When wearable actions are added using this method, the set of actions that
6913 * show on a wearable device splits from devices that only show actions added
6914 * using {@link android.app.Notification.Builder#addAction}. This allows for customization
6915 * of which actions display on different devices.
6916 *
6917 * @param action the action to add to this notification
6918 * @return this object for method chaining
6919 * @see android.app.Notification.Action
6920 */
6921 public WearableExtender addAction(Action action) {
6922 mActions.add(action);
6923 return this;
6924 }
6925
6926 /**
6927 * Adds wearable actions to this notification.
6928 *
6929 * <p>When wearable actions are added using this method, the set of actions that
6930 * show on a wearable device splits from devices that only show actions added
6931 * using {@link android.app.Notification.Builder#addAction}. This allows for customization
6932 * of which actions display on different devices.
6933 *
6934 * @param actions the actions to add to this notification
6935 * @return this object for method chaining
6936 * @see android.app.Notification.Action
6937 */
6938 public WearableExtender addActions(List<Action> actions) {
6939 mActions.addAll(actions);
6940 return this;
6941 }
6942
6943 /**
6944 * Clear all wearable actions present on this builder.
6945 * @return this object for method chaining.
6946 * @see #addAction
6947 */
6948 public WearableExtender clearActions() {
6949 mActions.clear();
6950 return this;
6951 }
6952
6953 /**
6954 * Get the wearable actions present on this notification.
6955 */
6956 public List<Action> getActions() {
6957 return mActions;
6958 }
6959
6960 /**
6961 * Set an intent to launch inside of an activity view when displaying
Griff Hazen14f57992014-05-26 09:07:14 -07006962 * this notification. The {@link PendingIntent} provided should be for an activity.
6963 *
6964 * <pre class="prettyprint">
6965 * Intent displayIntent = new Intent(context, MyDisplayActivity.class);
6966 * PendingIntent displayPendingIntent = PendingIntent.getActivity(context,
6967 * 0, displayIntent, PendingIntent.FLAG_UPDATE_CURRENT);
6968 * Notification notif = new Notification.Builder(context)
6969 * .extend(new Notification.WearableExtender()
6970 * .setDisplayIntent(displayPendingIntent)
6971 * .setCustomSizePreset(Notification.WearableExtender.SIZE_MEDIUM))
6972 * .build();</pre>
6973 *
6974 * <p>The activity to launch needs to allow embedding, must be exported, and
Griff Hazen831ca9d2014-06-17 00:38:38 -07006975 * should have an empty task affinity. It is also recommended to use the device
6976 * default light theme.
Griff Hazen14f57992014-05-26 09:07:14 -07006977 *
6978 * <p>Example AndroidManifest.xml entry:
6979 * <pre class="prettyprint">
6980 * &lt;activity android:name=&quot;com.example.MyDisplayActivity&quot;
6981 * android:exported=&quot;true&quot;
6982 * android:allowEmbedded=&quot;true&quot;
Griff Hazen831ca9d2014-06-17 00:38:38 -07006983 * android:taskAffinity=&quot;&quot;
6984 * android:theme=&quot;@android:style/Theme.DeviceDefault.Light&quot; /&gt;</pre>
Griff Hazen61a9e862014-05-22 16:05:19 -07006985 *
6986 * @param intent the {@link PendingIntent} for an activity
6987 * @return this object for method chaining
6988 * @see android.app.Notification.WearableExtender#getDisplayIntent
6989 */
6990 public WearableExtender setDisplayIntent(PendingIntent intent) {
6991 mDisplayIntent = intent;
6992 return this;
6993 }
6994
6995 /**
6996 * Get the intent to launch inside of an activity view when displaying this
6997 * notification. This {@code PendingIntent} should be for an activity.
6998 */
6999 public PendingIntent getDisplayIntent() {
7000 return mDisplayIntent;
7001 }
7002
7003 /**
7004 * Add an additional page of content to display with this notification. The current
7005 * notification forms the first page, and pages added using this function form
7006 * subsequent pages. This field can be used to separate a notification into multiple
7007 * sections.
7008 *
7009 * @param page the notification to add as another page
7010 * @return this object for method chaining
7011 * @see android.app.Notification.WearableExtender#getPages
7012 */
7013 public WearableExtender addPage(Notification page) {
7014 mPages.add(page);
7015 return this;
7016 }
7017
7018 /**
7019 * Add additional pages of content to display with this notification. The current
7020 * notification forms the first page, and pages added using this function form
7021 * subsequent pages. This field can be used to separate a notification into multiple
7022 * sections.
7023 *
7024 * @param pages a list of notifications
7025 * @return this object for method chaining
7026 * @see android.app.Notification.WearableExtender#getPages
7027 */
7028 public WearableExtender addPages(List<Notification> pages) {
7029 mPages.addAll(pages);
7030 return this;
7031 }
7032
7033 /**
7034 * Clear all additional pages present on this builder.
7035 * @return this object for method chaining.
7036 * @see #addPage
7037 */
7038 public WearableExtender clearPages() {
7039 mPages.clear();
7040 return this;
7041 }
7042
7043 /**
7044 * Get the array of additional pages of content for displaying this notification. The
7045 * current notification forms the first page, and elements within this array form
7046 * subsequent pages. This field can be used to separate a notification into multiple
7047 * sections.
7048 * @return the pages for this notification
7049 */
7050 public List<Notification> getPages() {
7051 return mPages;
7052 }
7053
7054 /**
7055 * Set a background image to be displayed behind the notification content.
7056 * Contrary to the {@link android.app.Notification.BigPictureStyle}, this background
7057 * will work with any notification style.
7058 *
7059 * @param background the background bitmap
7060 * @return this object for method chaining
7061 * @see android.app.Notification.WearableExtender#getBackground
7062 */
7063 public WearableExtender setBackground(Bitmap background) {
7064 mBackground = background;
7065 return this;
7066 }
7067
7068 /**
7069 * Get a background image to be displayed behind the notification content.
7070 * Contrary to the {@link android.app.Notification.BigPictureStyle}, this background
7071 * will work with any notification style.
7072 *
7073 * @return the background image
7074 * @see android.app.Notification.WearableExtender#setBackground
7075 */
7076 public Bitmap getBackground() {
7077 return mBackground;
7078 }
7079
7080 /**
7081 * Set an icon that goes with the content of this notification.
7082 */
7083 public WearableExtender setContentIcon(int icon) {
7084 mContentIcon = icon;
7085 return this;
7086 }
7087
7088 /**
7089 * Get an icon that goes with the content of this notification.
7090 */
7091 public int getContentIcon() {
7092 return mContentIcon;
7093 }
7094
7095 /**
7096 * Set the gravity that the content icon should have within the notification display.
7097 * Supported values include {@link android.view.Gravity#START} and
7098 * {@link android.view.Gravity#END}. The default value is {@link android.view.Gravity#END}.
7099 * @see #setContentIcon
7100 */
7101 public WearableExtender setContentIconGravity(int contentIconGravity) {
7102 mContentIconGravity = contentIconGravity;
7103 return this;
7104 }
7105
7106 /**
7107 * Get the gravity that the content icon should have within the notification display.
7108 * Supported values include {@link android.view.Gravity#START} and
7109 * {@link android.view.Gravity#END}. The default value is {@link android.view.Gravity#END}.
7110 * @see #getContentIcon
7111 */
7112 public int getContentIconGravity() {
7113 return mContentIconGravity;
7114 }
7115
7116 /**
7117 * Set an action from this notification's actions to be clickable with the content of
Griff Hazen14f57992014-05-26 09:07:14 -07007118 * this notification. This action will no longer display separately from the
7119 * notification's content.
7120 *
Griff Hazenca48d352014-05-28 22:37:13 -07007121 * <p>For notifications with multiple pages, child pages can also have content actions
Griff Hazen14f57992014-05-26 09:07:14 -07007122 * set, although the list of available actions comes from the main notification and not
7123 * from the child page's notification.
7124 *
7125 * @param actionIndex The index of the action to hoist onto the current notification page.
7126 * If wearable actions were added to the main notification, this index
7127 * will apply to that list, otherwise it will apply to the regular
7128 * actions list.
Griff Hazen61a9e862014-05-22 16:05:19 -07007129 */
7130 public WearableExtender setContentAction(int actionIndex) {
7131 mContentActionIndex = actionIndex;
7132 return this;
7133 }
7134
7135 /**
Griff Hazenca48d352014-05-28 22:37:13 -07007136 * Get the index of the notification action, if any, that was specified as being clickable
7137 * with the content of this notification. This action will no longer display separately
Griff Hazen14f57992014-05-26 09:07:14 -07007138 * from the notification's content.
Griff Hazen61a9e862014-05-22 16:05:19 -07007139 *
Griff Hazenca48d352014-05-28 22:37:13 -07007140 * <p>For notifications with multiple pages, child pages can also have content actions
Griff Hazen14f57992014-05-26 09:07:14 -07007141 * set, although the list of available actions comes from the main notification and not
7142 * from the child page's notification.
7143 *
7144 * <p>If wearable specific actions were added to the main notification, this index will
7145 * apply to that list, otherwise it will apply to the regular actions list.
Griff Hazenca48d352014-05-28 22:37:13 -07007146 *
7147 * @return the action index or {@link #UNSET_ACTION_INDEX} if no action was selected.
Griff Hazen61a9e862014-05-22 16:05:19 -07007148 */
7149 public int getContentAction() {
7150 return mContentActionIndex;
7151 }
7152
7153 /**
7154 * Set the gravity that this notification should have within the available viewport space.
7155 * Supported values include {@link android.view.Gravity#TOP},
7156 * {@link android.view.Gravity#CENTER_VERTICAL} and {@link android.view.Gravity#BOTTOM}.
7157 * The default value is {@link android.view.Gravity#BOTTOM}.
7158 */
7159 public WearableExtender setGravity(int gravity) {
7160 mGravity = gravity;
7161 return this;
7162 }
7163
7164 /**
7165 * Get the gravity that this notification should have within the available viewport space.
7166 * Supported values include {@link android.view.Gravity#TOP},
7167 * {@link android.view.Gravity#CENTER_VERTICAL} and {@link android.view.Gravity#BOTTOM}.
7168 * The default value is {@link android.view.Gravity#BOTTOM}.
7169 */
7170 public int getGravity() {
7171 return mGravity;
7172 }
7173
7174 /**
7175 * Set the custom size preset for the display of this notification out of the available
7176 * presets found in {@link android.app.Notification.WearableExtender}, e.g.
7177 * {@link #SIZE_LARGE}.
7178 * <p>Some custom size presets are only applicable for custom display notifications created
7179 * using {@link android.app.Notification.WearableExtender#setDisplayIntent}. Check the
7180 * documentation for the preset in question. See also
7181 * {@link #setCustomContentHeight} and {@link #getCustomSizePreset}.
7182 */
7183 public WearableExtender setCustomSizePreset(int sizePreset) {
7184 mCustomSizePreset = sizePreset;
7185 return this;
7186 }
7187
7188 /**
7189 * Get the custom size preset for the display of this notification out of the available
7190 * presets found in {@link android.app.Notification.WearableExtender}, e.g.
7191 * {@link #SIZE_LARGE}.
7192 * <p>Some custom size presets are only applicable for custom display notifications created
7193 * using {@link #setDisplayIntent}. Check the documentation for the preset in question.
7194 * See also {@link #setCustomContentHeight} and {@link #setCustomSizePreset}.
7195 */
7196 public int getCustomSizePreset() {
7197 return mCustomSizePreset;
7198 }
7199
7200 /**
7201 * Set the custom height in pixels for the display of this notification's content.
7202 * <p>This option is only available for custom display notifications created
7203 * using {@link android.app.Notification.WearableExtender#setDisplayIntent}. See also
7204 * {@link android.app.Notification.WearableExtender#setCustomSizePreset} and
7205 * {@link #getCustomContentHeight}.
7206 */
7207 public WearableExtender setCustomContentHeight(int height) {
7208 mCustomContentHeight = height;
7209 return this;
7210 }
7211
7212 /**
7213 * Get the custom height in pixels for the display of this notification's content.
7214 * <p>This option is only available for custom display notifications created
7215 * using {@link #setDisplayIntent}. See also {@link #setCustomSizePreset} and
7216 * {@link #setCustomContentHeight}.
7217 */
7218 public int getCustomContentHeight() {
7219 return mCustomContentHeight;
7220 }
7221
7222 /**
7223 * Set whether the scrolling position for the contents of this notification should start
7224 * at the bottom of the contents instead of the top when the contents are too long to
7225 * display within the screen. Default is false (start scroll at the top).
7226 */
7227 public WearableExtender setStartScrollBottom(boolean startScrollBottom) {
7228 setFlag(FLAG_START_SCROLL_BOTTOM, startScrollBottom);
7229 return this;
7230 }
7231
7232 /**
7233 * Get whether the scrolling position for the contents of this notification should start
7234 * at the bottom of the contents instead of the top when the contents are too long to
7235 * display within the screen. Default is false (start scroll at the top).
7236 */
7237 public boolean getStartScrollBottom() {
7238 return (mFlags & FLAG_START_SCROLL_BOTTOM) != 0;
7239 }
7240
7241 /**
7242 * Set whether the content intent is available when the wearable device is not connected
7243 * to a companion device. The user can still trigger this intent when the wearable device
7244 * is offline, but a visual hint will indicate that the content intent may not be available.
7245 * Defaults to true.
7246 */
7247 public WearableExtender setContentIntentAvailableOffline(
7248 boolean contentIntentAvailableOffline) {
7249 setFlag(FLAG_CONTENT_INTENT_AVAILABLE_OFFLINE, contentIntentAvailableOffline);
7250 return this;
7251 }
7252
7253 /**
7254 * Get whether the content intent is available when the wearable device is not connected
7255 * to a companion device. The user can still trigger this intent when the wearable device
7256 * is offline, but a visual hint will indicate that the content intent may not be available.
7257 * Defaults to true.
7258 */
7259 public boolean getContentIntentAvailableOffline() {
7260 return (mFlags & FLAG_CONTENT_INTENT_AVAILABLE_OFFLINE) != 0;
7261 }
7262
7263 /**
7264 * Set a hint that this notification's icon should not be displayed.
7265 * @param hintHideIcon {@code true} to hide the icon, {@code false} otherwise.
7266 * @return this object for method chaining
7267 */
7268 public WearableExtender setHintHideIcon(boolean hintHideIcon) {
7269 setFlag(FLAG_HINT_HIDE_ICON, hintHideIcon);
7270 return this;
7271 }
7272
7273 /**
7274 * Get a hint that this notification's icon should not be displayed.
7275 * @return {@code true} if this icon should not be displayed, false otherwise.
7276 * The default value is {@code false} if this was never set.
7277 */
7278 public boolean getHintHideIcon() {
7279 return (mFlags & FLAG_HINT_HIDE_ICON) != 0;
7280 }
7281
7282 /**
7283 * Set a visual hint that only the background image of this notification should be
7284 * displayed, and other semantic content should be hidden. This hint is only applicable
7285 * to sub-pages added using {@link #addPage}.
7286 */
7287 public WearableExtender setHintShowBackgroundOnly(boolean hintShowBackgroundOnly) {
7288 setFlag(FLAG_HINT_SHOW_BACKGROUND_ONLY, hintShowBackgroundOnly);
7289 return this;
7290 }
7291
7292 /**
7293 * Get a visual hint that only the background image of this notification should be
7294 * displayed, and other semantic content should be hidden. This hint is only applicable
7295 * to sub-pages added using {@link android.app.Notification.WearableExtender#addPage}.
7296 */
7297 public boolean getHintShowBackgroundOnly() {
7298 return (mFlags & FLAG_HINT_SHOW_BACKGROUND_ONLY) != 0;
7299 }
7300
Griff Hazen5f2edfc2014-09-29 16:28:44 -07007301 /**
Griff Hazen9c5be4e2014-11-17 17:47:50 -08007302 * Set a hint that this notification's background should not be clipped if possible,
7303 * and should instead be resized to fully display on the screen, retaining the aspect
7304 * ratio of the image. This can be useful for images like barcodes or qr codes.
Griff Hazen5f2edfc2014-09-29 16:28:44 -07007305 * @param hintAvoidBackgroundClipping {@code true} to avoid clipping if possible.
7306 * @return this object for method chaining
7307 */
7308 public WearableExtender setHintAvoidBackgroundClipping(
7309 boolean hintAvoidBackgroundClipping) {
7310 setFlag(FLAG_HINT_AVOID_BACKGROUND_CLIPPING, hintAvoidBackgroundClipping);
7311 return this;
7312 }
7313
7314 /**
Griff Hazen9c5be4e2014-11-17 17:47:50 -08007315 * Get a hint that this notification's background should not be clipped if possible,
7316 * and should instead be resized to fully display on the screen, retaining the aspect
7317 * ratio of the image. This can be useful for images like barcodes or qr codes.
Griff Hazen5f2edfc2014-09-29 16:28:44 -07007318 * @return {@code true} if it's ok if the background is clipped on the screen, false
7319 * otherwise. The default value is {@code false} if this was never set.
7320 */
7321 public boolean getHintAvoidBackgroundClipping() {
7322 return (mFlags & FLAG_HINT_AVOID_BACKGROUND_CLIPPING) != 0;
7323 }
7324
7325 /**
7326 * Set a hint that the screen should remain on for at least this duration when
7327 * this notification is displayed on the screen.
7328 * @param timeout The requested screen timeout in milliseconds. Can also be either
7329 * {@link #SCREEN_TIMEOUT_SHORT} or {@link #SCREEN_TIMEOUT_LONG}.
7330 * @return this object for method chaining
7331 */
7332 public WearableExtender setHintScreenTimeout(int timeout) {
7333 mHintScreenTimeout = timeout;
7334 return this;
7335 }
7336
7337 /**
7338 * Get the duration, in milliseconds, that the screen should remain on for
7339 * when this notification is displayed.
7340 * @return the duration in milliseconds if > 0, or either one of the sentinel values
7341 * {@link #SCREEN_TIMEOUT_SHORT} or {@link #SCREEN_TIMEOUT_LONG}.
7342 */
7343 public int getHintScreenTimeout() {
7344 return mHintScreenTimeout;
7345 }
7346
Alex Hills9ab3a232016-04-05 14:54:56 -04007347 /**
Alex Hills4bcb06b2016-04-05 14:26:25 -04007348 * Set a hint that this notification's {@link BigPictureStyle} (if present) should be
7349 * converted to low-bit and displayed in ambient mode, especially useful for barcodes and
7350 * qr codes, as well as other simple black-and-white tickets.
7351 * @param hintAmbientBigPicture {@code true} to enable converstion and ambient.
7352 * @return this object for method chaining
7353 */
7354 public WearableExtender setHintAmbientBigPicture(boolean hintAmbientBigPicture) {
7355 setFlag(FLAG_BIG_PICTURE_AMBIENT, hintAmbientBigPicture);
7356 return this;
7357 }
7358
7359 /**
7360 * Get a hint that this notification's {@link BigPictureStyle} (if present) should be
7361 * converted to low-bit and displayed in ambient mode, especially useful for barcodes and
7362 * qr codes, as well as other simple black-and-white tickets.
7363 * @return {@code true} if it should be displayed in ambient, false otherwise
7364 * otherwise. The default value is {@code false} if this was never set.
7365 */
7366 public boolean getHintAmbientBigPicture() {
7367 return (mFlags & FLAG_BIG_PICTURE_AMBIENT) != 0;
7368 }
7369
7370 /**
Alex Hills9ab3a232016-04-05 14:54:56 -04007371 * Set a hint that this notification's content intent will launch an {@link Activity}
7372 * directly, telling the platform that it can generate the appropriate transitions.
7373 * @param hintContentIntentLaunchesActivity {@code true} if the content intent will launch
7374 * an activity and transitions should be generated, false otherwise.
7375 * @return this object for method chaining
7376 */
7377 public WearableExtender setHintContentIntentLaunchesActivity(
7378 boolean hintContentIntentLaunchesActivity) {
7379 setFlag(FLAG_HINT_CONTENT_INTENT_LAUNCHES_ACTIVITY, hintContentIntentLaunchesActivity);
7380 return this;
7381 }
7382
7383 /**
7384 * Get a hint that this notification's content intent will launch an {@link Activity}
7385 * directly, telling the platform that it can generate the appropriate transitions
7386 * @return {@code true} if the content intent will launch an activity and transitions should
7387 * be generated, false otherwise. The default value is {@code false} if this was never set.
7388 */
7389 public boolean getHintContentIntentLaunchesActivity() {
7390 return (mFlags & FLAG_HINT_CONTENT_INTENT_LAUNCHES_ACTIVITY) != 0;
7391 }
7392
Nadia Benbernou948627e2016-04-14 14:41:08 -04007393 /**
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04007394 * Sets the dismissal id for this notification. If a notification is posted with a
7395 * dismissal id, then when that notification is canceled, notifications on other wearables
7396 * and the paired Android phone having that same dismissal id will also be canceled. See
Nadia Benbernou948627e2016-04-14 14:41:08 -04007397 * <a href="{@docRoot}wear/notifications/index.html">Adding Wearable Features to
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04007398 * Notifications</a> for more information.
Nadia Benbernou948627e2016-04-14 14:41:08 -04007399 * @param dismissalId the dismissal id of the notification.
7400 * @return this object for method chaining
7401 */
7402 public WearableExtender setDismissalId(String dismissalId) {
7403 mDismissalId = dismissalId;
7404 return this;
7405 }
7406
7407 /**
7408 * Returns the dismissal id of the notification.
7409 * @return the dismissal id of the notification or null if it has not been set.
7410 */
7411 public String getDismissalId() {
7412 return mDismissalId;
7413 }
7414
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04007415 /**
7416 * Sets a bridge tag for this notification. A bridge tag can be set for notifications
7417 * posted from a phone to provide finer-grained control on what notifications are bridged
7418 * to wearables. See <a href="{@docRoot}wear/notifications/index.html">Adding Wearable
7419 * Features to Notifications</a> for more information.
7420 * @param bridgeTag the bridge tag of the notification.
7421 * @return this object for method chaining
7422 */
7423 public WearableExtender setBridgeTag(String bridgeTag) {
7424 mBridgeTag = bridgeTag;
7425 return this;
7426 }
7427
7428 /**
7429 * Returns the bridge tag of the notification.
7430 * @return the bridge tag or null if not present.
7431 */
7432 public String getBridgeTag() {
7433 return mBridgeTag;
7434 }
7435
Griff Hazen61a9e862014-05-22 16:05:19 -07007436 private void setFlag(int mask, boolean value) {
7437 if (value) {
7438 mFlags |= mask;
7439 } else {
7440 mFlags &= ~mask;
7441 }
7442 }
7443 }
7444
7445 /**
Zhen Yu Song9d5528b2014-11-14 15:34:39 -08007446 * <p>Helper class to add Android Auto extensions to notifications. To create a notification
7447 * with car extensions:
7448 *
7449 * <ol>
7450 * <li>Create an {@link Notification.Builder}, setting any desired
7451 * properties.
7452 * <li>Create a {@link CarExtender}.
7453 * <li>Set car-specific properties using the {@code add} and {@code set} methods of
7454 * {@link CarExtender}.
7455 * <li>Call {@link Notification.Builder#extend(Notification.Extender)}
7456 * to apply the extensions to a notification.
7457 * </ol>
7458 *
7459 * <pre class="prettyprint">
7460 * Notification notification = new Notification.Builder(context)
7461 * ...
7462 * .extend(new CarExtender()
7463 * .set*(...))
7464 * .build();
7465 * </pre>
7466 *
7467 * <p>Car extensions can be accessed on an existing notification by using the
7468 * {@code CarExtender(Notification)} constructor, and then using the {@code get} methods
7469 * to access values.
7470 */
7471 public static final class CarExtender implements Extender {
7472 private static final String TAG = "CarExtender";
7473
7474 private static final String EXTRA_CAR_EXTENDER = "android.car.EXTENSIONS";
7475 private static final String EXTRA_LARGE_ICON = "large_icon";
7476 private static final String EXTRA_CONVERSATION = "car_conversation";
7477 private static final String EXTRA_COLOR = "app_color";
7478
7479 private Bitmap mLargeIcon;
7480 private UnreadConversation mUnreadConversation;
7481 private int mColor = Notification.COLOR_DEFAULT;
7482
7483 /**
7484 * Create a {@link CarExtender} with default options.
7485 */
7486 public CarExtender() {
7487 }
7488
7489 /**
7490 * Create a {@link CarExtender} from the CarExtender options of an existing Notification.
7491 *
7492 * @param notif The notification from which to copy options.
7493 */
7494 public CarExtender(Notification notif) {
7495 Bundle carBundle = notif.extras == null ?
7496 null : notif.extras.getBundle(EXTRA_CAR_EXTENDER);
7497 if (carBundle != null) {
7498 mLargeIcon = carBundle.getParcelable(EXTRA_LARGE_ICON);
7499 mColor = carBundle.getInt(EXTRA_COLOR, Notification.COLOR_DEFAULT);
7500
7501 Bundle b = carBundle.getBundle(EXTRA_CONVERSATION);
7502 mUnreadConversation = UnreadConversation.getUnreadConversationFromBundle(b);
7503 }
7504 }
7505
7506 /**
7507 * Apply car extensions to a notification that is being built. This is typically called by
7508 * the {@link Notification.Builder#extend(Notification.Extender)}
7509 * method of {@link Notification.Builder}.
7510 */
7511 @Override
7512 public Notification.Builder extend(Notification.Builder builder) {
7513 Bundle carExtensions = new Bundle();
7514
7515 if (mLargeIcon != null) {
7516 carExtensions.putParcelable(EXTRA_LARGE_ICON, mLargeIcon);
7517 }
7518 if (mColor != Notification.COLOR_DEFAULT) {
7519 carExtensions.putInt(EXTRA_COLOR, mColor);
7520 }
7521
7522 if (mUnreadConversation != null) {
7523 Bundle b = mUnreadConversation.getBundleForUnreadConversation();
7524 carExtensions.putBundle(EXTRA_CONVERSATION, b);
7525 }
7526
7527 builder.getExtras().putBundle(EXTRA_CAR_EXTENDER, carExtensions);
7528 return builder;
7529 }
7530
7531 /**
7532 * Sets the accent color to use when Android Auto presents the notification.
7533 *
7534 * Android Auto uses the color set with {@link Notification.Builder#setColor(int)}
7535 * to accent the displayed notification. However, not all colors are acceptable in an
7536 * automotive setting. This method can be used to override the color provided in the
7537 * notification in such a situation.
7538 */
Tor Norbye80756e32015-03-02 09:39:27 -08007539 public CarExtender setColor(@ColorInt int color) {
Zhen Yu Song9d5528b2014-11-14 15:34:39 -08007540 mColor = color;
7541 return this;
7542 }
7543
7544 /**
7545 * Gets the accent color.
7546 *
Julia Reynoldsd9228f12015-10-20 10:37:27 -04007547 * @see #setColor
Zhen Yu Song9d5528b2014-11-14 15:34:39 -08007548 */
Tor Norbye80756e32015-03-02 09:39:27 -08007549 @ColorInt
Zhen Yu Song9d5528b2014-11-14 15:34:39 -08007550 public int getColor() {
7551 return mColor;
7552 }
7553
7554 /**
7555 * Sets the large icon of the car notification.
7556 *
7557 * If no large icon is set in the extender, Android Auto will display the icon
7558 * specified by {@link Notification.Builder#setLargeIcon(android.graphics.Bitmap)}
7559 *
7560 * @param largeIcon The large icon to use in the car notification.
7561 * @return This object for method chaining.
7562 */
7563 public CarExtender setLargeIcon(Bitmap largeIcon) {
7564 mLargeIcon = largeIcon;
7565 return this;
7566 }
7567
7568 /**
7569 * Gets the large icon used in this car notification, or null if no icon has been set.
7570 *
7571 * @return The large icon for the car notification.
7572 * @see CarExtender#setLargeIcon
7573 */
7574 public Bitmap getLargeIcon() {
7575 return mLargeIcon;
7576 }
7577
7578 /**
7579 * Sets the unread conversation in a message notification.
7580 *
7581 * @param unreadConversation The unread part of the conversation this notification conveys.
7582 * @return This object for method chaining.
7583 */
7584 public CarExtender setUnreadConversation(UnreadConversation unreadConversation) {
7585 mUnreadConversation = unreadConversation;
7586 return this;
7587 }
7588
7589 /**
7590 * Returns the unread conversation conveyed by this notification.
7591 * @see #setUnreadConversation(UnreadConversation)
7592 */
7593 public UnreadConversation getUnreadConversation() {
7594 return mUnreadConversation;
7595 }
7596
7597 /**
7598 * A class which holds the unread messages from a conversation.
7599 */
7600 public static class UnreadConversation {
7601 private static final String KEY_AUTHOR = "author";
7602 private static final String KEY_TEXT = "text";
7603 private static final String KEY_MESSAGES = "messages";
7604 private static final String KEY_REMOTE_INPUT = "remote_input";
7605 private static final String KEY_ON_REPLY = "on_reply";
7606 private static final String KEY_ON_READ = "on_read";
7607 private static final String KEY_PARTICIPANTS = "participants";
7608 private static final String KEY_TIMESTAMP = "timestamp";
7609
7610 private final String[] mMessages;
7611 private final RemoteInput mRemoteInput;
7612 private final PendingIntent mReplyPendingIntent;
7613 private final PendingIntent mReadPendingIntent;
7614 private final String[] mParticipants;
7615 private final long mLatestTimestamp;
7616
7617 UnreadConversation(String[] messages, RemoteInput remoteInput,
7618 PendingIntent replyPendingIntent, PendingIntent readPendingIntent,
7619 String[] participants, long latestTimestamp) {
7620 mMessages = messages;
7621 mRemoteInput = remoteInput;
7622 mReadPendingIntent = readPendingIntent;
7623 mReplyPendingIntent = replyPendingIntent;
7624 mParticipants = participants;
7625 mLatestTimestamp = latestTimestamp;
7626 }
7627
7628 /**
7629 * Gets the list of messages conveyed by this notification.
7630 */
7631 public String[] getMessages() {
7632 return mMessages;
7633 }
7634
7635 /**
7636 * Gets the remote input that will be used to convey the response to a message list, or
7637 * null if no such remote input exists.
7638 */
7639 public RemoteInput getRemoteInput() {
7640 return mRemoteInput;
7641 }
7642
7643 /**
7644 * Gets the pending intent that will be triggered when the user replies to this
7645 * notification.
7646 */
7647 public PendingIntent getReplyPendingIntent() {
7648 return mReplyPendingIntent;
7649 }
7650
7651 /**
7652 * Gets the pending intent that Android Auto will send after it reads aloud all messages
7653 * in this object's message list.
7654 */
7655 public PendingIntent getReadPendingIntent() {
7656 return mReadPendingIntent;
7657 }
7658
7659 /**
7660 * Gets the participants in the conversation.
7661 */
7662 public String[] getParticipants() {
7663 return mParticipants;
7664 }
7665
7666 /**
7667 * Gets the firs participant in the conversation.
7668 */
7669 public String getParticipant() {
7670 return mParticipants.length > 0 ? mParticipants[0] : null;
7671 }
7672
7673 /**
7674 * Gets the timestamp of the conversation.
7675 */
7676 public long getLatestTimestamp() {
7677 return mLatestTimestamp;
7678 }
7679
7680 Bundle getBundleForUnreadConversation() {
7681 Bundle b = new Bundle();
7682 String author = null;
7683 if (mParticipants != null && mParticipants.length > 1) {
7684 author = mParticipants[0];
7685 }
7686 Parcelable[] messages = new Parcelable[mMessages.length];
7687 for (int i = 0; i < messages.length; i++) {
7688 Bundle m = new Bundle();
7689 m.putString(KEY_TEXT, mMessages[i]);
7690 m.putString(KEY_AUTHOR, author);
7691 messages[i] = m;
7692 }
7693 b.putParcelableArray(KEY_MESSAGES, messages);
7694 if (mRemoteInput != null) {
7695 b.putParcelable(KEY_REMOTE_INPUT, mRemoteInput);
7696 }
7697 b.putParcelable(KEY_ON_REPLY, mReplyPendingIntent);
7698 b.putParcelable(KEY_ON_READ, mReadPendingIntent);
7699 b.putStringArray(KEY_PARTICIPANTS, mParticipants);
7700 b.putLong(KEY_TIMESTAMP, mLatestTimestamp);
7701 return b;
7702 }
7703
7704 static UnreadConversation getUnreadConversationFromBundle(Bundle b) {
7705 if (b == null) {
7706 return null;
7707 }
7708 Parcelable[] parcelableMessages = b.getParcelableArray(KEY_MESSAGES);
7709 String[] messages = null;
7710 if (parcelableMessages != null) {
7711 String[] tmp = new String[parcelableMessages.length];
7712 boolean success = true;
7713 for (int i = 0; i < tmp.length; i++) {
7714 if (!(parcelableMessages[i] instanceof Bundle)) {
7715 success = false;
7716 break;
7717 }
7718 tmp[i] = ((Bundle) parcelableMessages[i]).getString(KEY_TEXT);
7719 if (tmp[i] == null) {
7720 success = false;
7721 break;
7722 }
7723 }
7724 if (success) {
7725 messages = tmp;
7726 } else {
7727 return null;
7728 }
7729 }
7730
7731 PendingIntent onRead = b.getParcelable(KEY_ON_READ);
7732 PendingIntent onReply = b.getParcelable(KEY_ON_REPLY);
7733
7734 RemoteInput remoteInput = b.getParcelable(KEY_REMOTE_INPUT);
7735
7736 String[] participants = b.getStringArray(KEY_PARTICIPANTS);
7737 if (participants == null || participants.length != 1) {
7738 return null;
7739 }
7740
7741 return new UnreadConversation(messages,
7742 remoteInput,
7743 onReply,
7744 onRead,
7745 participants, b.getLong(KEY_TIMESTAMP));
7746 }
7747 };
7748
7749 /**
7750 * Builder class for {@link CarExtender.UnreadConversation} objects.
7751 */
7752 public static class Builder {
7753 private final List<String> mMessages = new ArrayList<String>();
7754 private final String mParticipant;
7755 private RemoteInput mRemoteInput;
7756 private PendingIntent mReadPendingIntent;
7757 private PendingIntent mReplyPendingIntent;
7758 private long mLatestTimestamp;
7759
7760 /**
7761 * Constructs a new builder for {@link CarExtender.UnreadConversation}.
7762 *
7763 * @param name The name of the other participant in the conversation.
7764 */
7765 public Builder(String name) {
7766 mParticipant = name;
7767 }
7768
7769 /**
7770 * Appends a new unread message to the list of messages for this conversation.
7771 *
7772 * The messages should be added from oldest to newest.
7773 *
7774 * @param message The text of the new unread message.
7775 * @return This object for method chaining.
7776 */
7777 public Builder addMessage(String message) {
7778 mMessages.add(message);
7779 return this;
7780 }
7781
7782 /**
7783 * Sets the pending intent and remote input which will convey the reply to this
7784 * notification.
7785 *
7786 * @param pendingIntent The pending intent which will be triggered on a reply.
7787 * @param remoteInput The remote input parcelable which will carry the reply.
7788 * @return This object for method chaining.
7789 *
7790 * @see CarExtender.UnreadConversation#getRemoteInput
7791 * @see CarExtender.UnreadConversation#getReplyPendingIntent
7792 */
7793 public Builder setReplyAction(
7794 PendingIntent pendingIntent, RemoteInput remoteInput) {
7795 mRemoteInput = remoteInput;
7796 mReplyPendingIntent = pendingIntent;
7797
7798 return this;
7799 }
7800
7801 /**
7802 * Sets the pending intent that will be sent once the messages in this notification
7803 * are read.
7804 *
7805 * @param pendingIntent The pending intent to use.
7806 * @return This object for method chaining.
7807 */
7808 public Builder setReadPendingIntent(PendingIntent pendingIntent) {
7809 mReadPendingIntent = pendingIntent;
7810 return this;
7811 }
7812
7813 /**
7814 * Sets the timestamp of the most recent message in an unread conversation.
7815 *
7816 * If a messaging notification has been posted by your application and has not
7817 * yet been cancelled, posting a later notification with the same id and tag
7818 * but without a newer timestamp may result in Android Auto not displaying a
7819 * heads up notification for the later notification.
7820 *
7821 * @param timestamp The timestamp of the most recent message in the conversation.
7822 * @return This object for method chaining.
7823 */
7824 public Builder setLatestTimestamp(long timestamp) {
7825 mLatestTimestamp = timestamp;
7826 return this;
7827 }
7828
7829 /**
7830 * Builds a new unread conversation object.
7831 *
7832 * @return The new unread conversation object.
7833 */
7834 public UnreadConversation build() {
7835 String[] messages = mMessages.toArray(new String[mMessages.size()]);
7836 String[] participants = { mParticipant };
7837 return new UnreadConversation(messages, mRemoteInput, mReplyPendingIntent,
7838 mReadPendingIntent, participants, mLatestTimestamp);
7839 }
7840 }
7841 }
7842
7843 /**
Dmitri Plotnikov9e9cfd92016-12-21 10:52:26 -08007844 * <p>Helper class to add Android TV extensions to notifications. To create a notification
7845 * with a TV extension:
7846 *
7847 * <ol>
7848 * <li>Create an {@link Notification.Builder}, setting any desired properties.
7849 * <li>Create a {@link TvExtender}.
7850 * <li>Set TV-specific properties using the {@code set} methods of
7851 * {@link TvExtender}.
7852 * <li>Call {@link Notification.Builder#extend(Notification.Extender)}
7853 * to apply the extension to a notification.
7854 * </ol>
7855 *
7856 * <pre class="prettyprint">
7857 * Notification notification = new Notification.Builder(context)
7858 * ...
7859 * .extend(new TvExtender()
7860 * .set*(...))
7861 * .build();
7862 * </pre>
7863 *
7864 * <p>TV extensions can be accessed on an existing notification by using the
7865 * {@code TvExtender(Notification)} constructor, and then using the {@code get} methods
7866 * to access values.
7867 *
7868 * @hide
7869 */
7870 @SystemApi
7871 public static final class TvExtender implements Extender {
7872 private static final String TAG = "TvExtender";
7873
7874 private static final String EXTRA_TV_EXTENDER = "android.tv.EXTENSIONS";
7875 private static final String EXTRA_FLAGS = "flags";
7876 private static final String EXTRA_CONTENT_INTENT = "content_intent";
7877 private static final String EXTRA_DELETE_INTENT = "delete_intent";
Dmitri Plotnikovb8a04ee2017-01-20 10:39:22 -08007878 private static final String EXTRA_CHANNEL_ID = "channel_id";
Dmitri Plotnikov9e9cfd92016-12-21 10:52:26 -08007879
7880 // Flags bitwise-ored to mFlags
7881 private static final int FLAG_AVAILABLE_ON_TV = 0x1;
7882
7883 private int mFlags;
Dmitri Plotnikovb8a04ee2017-01-20 10:39:22 -08007884 private String mChannelId;
Dmitri Plotnikov9e9cfd92016-12-21 10:52:26 -08007885 private PendingIntent mContentIntent;
7886 private PendingIntent mDeleteIntent;
7887
7888 /**
7889 * Create a {@link TvExtender} with default options.
7890 */
7891 public TvExtender() {
7892 mFlags = FLAG_AVAILABLE_ON_TV;
7893 }
7894
7895 /**
7896 * Create a {@link TvExtender} from the TvExtender options of an existing Notification.
7897 *
7898 * @param notif The notification from which to copy options.
7899 */
7900 public TvExtender(Notification notif) {
7901 Bundle bundle = notif.extras == null ?
7902 null : notif.extras.getBundle(EXTRA_TV_EXTENDER);
7903 if (bundle != null) {
7904 mFlags = bundle.getInt(EXTRA_FLAGS);
Dmitri Plotnikovb8a04ee2017-01-20 10:39:22 -08007905 mChannelId = bundle.getString(EXTRA_CHANNEL_ID);
Dmitri Plotnikov9e9cfd92016-12-21 10:52:26 -08007906 mContentIntent = bundle.getParcelable(EXTRA_CONTENT_INTENT);
7907 mDeleteIntent = bundle.getParcelable(EXTRA_DELETE_INTENT);
7908 }
7909 }
7910
7911 /**
7912 * Apply a TV extension to a notification that is being built. This is typically called by
7913 * the {@link Notification.Builder#extend(Notification.Extender)}
7914 * method of {@link Notification.Builder}.
7915 */
7916 @Override
7917 public Notification.Builder extend(Notification.Builder builder) {
7918 Bundle bundle = new Bundle();
7919
7920 bundle.putInt(EXTRA_FLAGS, mFlags);
Dmitri Plotnikovb8a04ee2017-01-20 10:39:22 -08007921 bundle.putString(EXTRA_CHANNEL_ID, mChannelId);
Dmitri Plotnikov9e9cfd92016-12-21 10:52:26 -08007922 if (mContentIntent != null) {
7923 bundle.putParcelable(EXTRA_CONTENT_INTENT, mContentIntent);
7924 }
7925
7926 if (mDeleteIntent != null) {
7927 bundle.putParcelable(EXTRA_DELETE_INTENT, mDeleteIntent);
7928 }
7929
7930 builder.getExtras().putBundle(EXTRA_TV_EXTENDER, bundle);
7931 return builder;
7932 }
7933
7934 /**
7935 * Returns true if this notification should be shown on TV. This method return true
7936 * if the notification was extended with a TvExtender.
7937 */
7938 public boolean isAvailableOnTv() {
7939 return (mFlags & FLAG_AVAILABLE_ON_TV) != 0;
7940 }
7941
7942 /**
Dmitri Plotnikovb8a04ee2017-01-20 10:39:22 -08007943 * Specifies the channel the notification should be delivered on when shown on TV.
7944 * It can be different from the channel that the notification is delivered to when
7945 * posting on a non-TV device.
7946 */
7947 public TvExtender setChannel(String channelId) {
7948 mChannelId = channelId;
7949 return this;
7950 }
7951
7952 /**
Julia Reynoldsbad42972017-04-25 13:52:49 -04007953 * Specifies the channel the notification should be delivered on when shown on TV.
7954 * It can be different from the channel that the notification is delivered to when
7955 * posting on a non-TV device.
7956 */
7957 public TvExtender setChannelId(String channelId) {
7958 mChannelId = channelId;
7959 return this;
7960 }
7961
7962 /**
Dmitri Plotnikovb8a04ee2017-01-20 10:39:22 -08007963 * Returns the id of the channel this notification posts to on TV.
7964 */
7965 public String getChannel() {
7966 return mChannelId;
7967 }
7968
7969 /**
Julia Reynoldsbad42972017-04-25 13:52:49 -04007970 * Returns the id of the channel this notification posts to on TV.
7971 */
7972 public String getChannelId() {
7973 return mChannelId;
7974 }
7975
7976 /**
Dmitri Plotnikov9e9cfd92016-12-21 10:52:26 -08007977 * Supplies a {@link PendingIntent} to be sent when the notification is selected on TV.
7978 * If provided, it is used instead of the content intent specified
7979 * at the level of Notification.
7980 */
7981 public TvExtender setContentIntent(PendingIntent intent) {
7982 mContentIntent = intent;
7983 return this;
7984 }
7985
7986 /**
7987 * Returns the TV-specific content intent. If this method returns null, the
7988 * main content intent on the notification should be used.
7989 *
7990 * @see {@link Notification#contentIntent}
7991 */
7992 public PendingIntent getContentIntent() {
7993 return mContentIntent;
7994 }
7995
7996 /**
7997 * Supplies a {@link PendingIntent} to send when the notification is cleared explicitly
7998 * by the user on TV. If provided, it is used instead of the delete intent specified
7999 * at the level of Notification.
8000 */
8001 public TvExtender setDeleteIntent(PendingIntent intent) {
8002 mDeleteIntent = intent;
8003 return this;
8004 }
8005
8006 /**
8007 * Returns the TV-specific delete intent. If this method returns null, the
8008 * main delete intent on the notification should be used.
8009 *
8010 * @see {@link Notification#deleteIntent}
8011 */
8012 public PendingIntent getDeleteIntent() {
8013 return mDeleteIntent;
8014 }
8015 }
8016
8017 /**
Griff Hazen61a9e862014-05-22 16:05:19 -07008018 * Get an array of Notification objects from a parcelable array bundle field.
8019 * Update the bundle to have a typed array so fetches in the future don't need
8020 * to do an array copy.
8021 */
8022 private static Notification[] getNotificationArrayFromBundle(Bundle bundle, String key) {
8023 Parcelable[] array = bundle.getParcelableArray(key);
8024 if (array instanceof Notification[] || array == null) {
8025 return (Notification[]) array;
8026 }
8027 Notification[] typedArray = Arrays.copyOf(array, array.length,
8028 Notification[].class);
8029 bundle.putParcelableArray(key, typedArray);
8030 return typedArray;
8031 }
Christoph Studer4600f9b2014-07-22 22:44:43 +02008032
8033 private static class BuilderRemoteViews extends RemoteViews {
8034 public BuilderRemoteViews(Parcel parcel) {
8035 super(parcel);
8036 }
8037
Kenny Guy77320062014-08-27 21:37:15 +01008038 public BuilderRemoteViews(ApplicationInfo appInfo, int layoutId) {
8039 super(appInfo, layoutId);
Christoph Studer4600f9b2014-07-22 22:44:43 +02008040 }
8041
8042 @Override
8043 public BuilderRemoteViews clone() {
8044 Parcel p = Parcel.obtain();
8045 writeToParcel(p, 0);
8046 p.setDataPosition(0);
8047 BuilderRemoteViews brv = new BuilderRemoteViews(p);
8048 p.recycle();
8049 return brv;
8050 }
8051 }
Adrian Roos70d7aa32017-01-11 15:39:06 -08008052
8053 private static class StandardTemplateParams {
8054 boolean hasProgress = true;
8055 boolean ambient = false;
8056 CharSequence title;
8057 CharSequence text;
8058
8059 final StandardTemplateParams reset() {
8060 hasProgress = true;
8061 ambient = false;
8062 title = null;
8063 text = null;
8064 return this;
8065 }
8066
8067 final StandardTemplateParams hasProgress(boolean hasProgress) {
8068 this.hasProgress = hasProgress;
8069 return this;
8070 }
8071
8072 final StandardTemplateParams title(CharSequence title) {
8073 this.title = title;
8074 return this;
8075 }
8076
8077 final StandardTemplateParams text(CharSequence text) {
8078 this.text = text;
8079 return this;
8080 }
8081
8082 final StandardTemplateParams ambient(boolean ambient) {
Adrian Roos0bc3f6a2017-03-06 11:54:05 -08008083 Preconditions.checkState(title == null && text == null, "must set ambient before text");
Adrian Roos70d7aa32017-01-11 15:39:06 -08008084 this.ambient = ambient;
8085 return this;
8086 }
8087
8088 final StandardTemplateParams fillTextsFrom(Builder b) {
8089 Bundle extras = b.mN.extras;
Adrian Roos0bc3f6a2017-03-06 11:54:05 -08008090 title = b.processLegacyText(extras.getCharSequence(EXTRA_TITLE), ambient);
8091 text = b.processLegacyText(extras.getCharSequence(EXTRA_TEXT), ambient);
Adrian Roos70d7aa32017-01-11 15:39:06 -08008092 return this;
8093 }
8094 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008095}