blob: 13c619c1413138ed26223fc30db89f5f619d97ed [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
106 * containing application. Optional but recommended for apps that post
107 * {@link android.app.Notification Notifications}.
108 */
109 @SdkConstant(SdkConstantType.INTENT_CATEGORY)
110 public static final String INTENT_CATEGORY_NOTIFICATION_PREFERENCES
111 = "android.intent.category.NOTIFICATION_PREFERENCES";
112
113 /**
Julia Reynolds2619b5e2017-02-09 09:58:15 -0500114 * Optional extra for {@link #INTENT_CATEGORY_NOTIFICATION_PREFERENCES}. If provided, will
115 * contain a {@link NotificationChannel#getId() channel id} that can be used to narrow down
116 * what in app notifications settings should be shown.
117 */
118 public static final String EXTRA_CHANNEL_ID = "android.intent.extra.CHANNEL_ID";
119
120 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800121 * Use all default values (where applicable).
122 */
123 public static final int DEFAULT_ALL = ~0;
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500124
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800125 /**
126 * Use the default notification sound. This will ignore any given
127 * {@link #sound}.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500128 *
Chris Wren47c20a12014-06-18 17:27:29 -0400129 * <p>
130 * A notification that is noisy is more likely to be presented as a heads-up notification.
131 * </p>
132 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800133 * @see #defaults
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500134 */
135
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800136 public static final int DEFAULT_SOUND = 1;
137
138 /**
139 * Use the default notification vibrate. This will ignore any given
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500140 * {@link #vibrate}. Using phone vibration requires the
Scott Mainb8b36452009-04-26 15:50:49 -0700141 * {@link android.Manifest.permission#VIBRATE VIBRATE} permission.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500142 *
Chris Wren47c20a12014-06-18 17:27:29 -0400143 * <p>
144 * A notification that vibrates is more likely to be presented as a heads-up notification.
145 * </p>
146 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800147 * @see #defaults
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500148 */
149
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800150 public static final int DEFAULT_VIBRATE = 2;
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500151
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800152 /**
153 * Use the default notification lights. This will ignore the
154 * {@link #FLAG_SHOW_LIGHTS} bit, and {@link #ledARGB}, {@link #ledOffMS}, or
155 * {@link #ledOnMS}.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500156 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800157 * @see #defaults
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500158 */
159
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800160 public static final int DEFAULT_LIGHTS = 4;
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500161
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800162 /**
Christoph Studer535ec612014-09-03 15:47:47 +0200163 * Maximum length of CharSequences accepted by Builder and friends.
164 *
165 * <p>
166 * Avoids spamming the system with overly large strings such as full e-mails.
167 */
168 private static final int MAX_CHARSEQUENCE_LENGTH = 5 * 1024;
169
170 /**
Adrian Roose458aa82015-12-08 16:17:19 -0800171 * Maximum entries of reply text that are accepted by Builder and friends.
172 */
173 private static final int MAX_REPLY_HISTORY = 5;
174
175 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500176 * A timestamp related to this notification, in milliseconds since the epoch.
Joe Malin8d40d042012-11-05 11:36:40 -0800177 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500178 * Default value: {@link System#currentTimeMillis() Now}.
179 *
180 * Choose a timestamp that will be most relevant to the user. For most finite events, this
181 * corresponds to the time the event happened (or will happen, in the case of events that have
182 * yet to occur but about which the user is being informed). Indefinite events should be
Joe Malin8d40d042012-11-05 11:36:40 -0800183 * timestamped according to when the activity began.
184 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500185 * Some examples:
Joe Malin8d40d042012-11-05 11:36:40 -0800186 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500187 * <ul>
188 * <li>Notification of a new chat message should be stamped when the message was received.</li>
189 * <li>Notification of an ongoing file download (with a progress bar, for example) should be stamped when the download started.</li>
190 * <li>Notification of a completed file download should be stamped when the download finished.</li>
191 * <li>Notification of an upcoming meeting should be stamped with the time the meeting will begin (that is, in the future).</li>
192 * <li>Notification of an ongoing stopwatch (increasing timer) should be stamped with the watch's start time.
193 * <li>Notification of an ongoing countdown timer should be stamped with the timer's end time.
Joe Malin8d40d042012-11-05 11:36:40 -0800194 * </ul>
195 *
Selim Cinek0ff1ce602016-04-05 18:27:16 -0700196 * For apps targeting {@link android.os.Build.VERSION_CODES#N} and above, this time is not shown
197 * anymore by default and must be opted into by using
198 * {@link android.app.Notification.Builder#setShowWhen(boolean)}
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800199 */
200 public long when;
201
202 /**
Selim Cinekb85f36fd2016-04-20 18:46:36 -0700203 * The creation time of the notification
204 */
205 private long creationTime;
206
207 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800208 * The resource id of a drawable to use as the icon in the status bar.
Dan Sandler86647982015-05-13 23:41:13 -0400209 *
210 * @deprecated Use {@link Builder#setSmallIcon(Icon)} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800211 */
Dan Sandler86647982015-05-13 23:41:13 -0400212 @Deprecated
Tor Norbye7b9c9122013-05-30 16:48:33 -0700213 @DrawableRes
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800214 public int icon;
215
216 /**
Joe Onorato46439ce2010-11-19 13:56:21 -0800217 * If the icon in the status bar is to have more than one level, you can set this. Otherwise,
218 * leave it at its default value of 0.
219 *
220 * @see android.widget.ImageView#setImageLevel
Griff Hazen959591e2014-05-15 22:26:18 -0700221 * @see android.graphics.drawable.Drawable#setLevel
Joe Onorato46439ce2010-11-19 13:56:21 -0800222 */
223 public int iconLevel;
224
225 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500226 * The number of events that this notification represents. For example, in a new mail
227 * notification, this could be the number of unread messages.
Joe Malin8d40d042012-11-05 11:36:40 -0800228 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500229 * The system may or may not use this field to modify the appearance of the notification. For
230 * example, before {@link android.os.Build.VERSION_CODES#HONEYCOMB}, this number was
231 * superimposed over the icon in the status bar. Starting with
232 * {@link android.os.Build.VERSION_CODES#HONEYCOMB}, the template used by
233 * {@link Notification.Builder} has displayed the number in the expanded notification view.
Julia Reynolds13d898c2017-02-02 12:22:05 -0500234 * Starting with {@link android.os.Build.VERSION_CODES#O}, the number may be displayed as a
235 * badge icon in Launchers that support badging.
Joe Malin8d40d042012-11-05 11:36:40 -0800236 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800237 */
Julia Reynolds13d898c2017-02-02 12:22:05 -0500238 public int number = 1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800239
240 /**
241 * The intent to execute when the expanded status entry is clicked. If
242 * this is an activity, it must include the
243 * {@link android.content.Intent#FLAG_ACTIVITY_NEW_TASK} flag, which requires
Scott Main7aee61f2011-02-08 11:25:01 -0800244 * that you take care of task management as described in the
245 * <a href="{@docRoot}guide/topics/fundamentals/tasks-and-back-stack.html">Tasks and Back
Dianne Hackborn6ceca582012-01-10 15:24:26 -0800246 * Stack</a> document. In particular, make sure to read the notification section
247 * <a href="{@docRoot}guide/topics/ui/notifiers/notifications.html#HandlingNotifications">Handling
248 * Notifications</a> for the correct ways to launch an application from a
249 * notification.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800250 */
251 public PendingIntent contentIntent;
252
253 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500254 * The intent to execute when the notification is explicitly dismissed by the user, either with
255 * the "Clear All" button or by swiping it away individually.
256 *
257 * This probably shouldn't be launching an activity since several of those will be sent
258 * at the same time.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800259 */
260 public PendingIntent deleteIntent;
261
262 /**
Dianne Hackborn170bae72010-09-03 15:14:28 -0700263 * An intent to launch instead of posting the notification to the status bar.
Joe Onoratocb109a02011-01-18 17:57:41 -0800264 *
Chris Wren47c20a12014-06-18 17:27:29 -0400265 * <p>
266 * The system UI may choose to display a heads-up notification, instead of
267 * launching this intent, while the user is using the device.
268 * </p>
269 *
Joe Onoratocb109a02011-01-18 17:57:41 -0800270 * @see Notification.Builder#setFullScreenIntent
Daniel Sandlere46cbd32010-06-17 10:35:26 -0400271 */
272 public PendingIntent fullScreenIntent;
273
274 /**
Dan Sandler5fcdf6e2014-07-18 11:31:15 -0400275 * Text that summarizes this notification for accessibility services.
276 *
277 * As of the L release, this text is no longer shown on screen, but it is still useful to
278 * accessibility services (where it serves as an audible announcement of the notification's
279 * appearance).
Joe Onoratoef1e7762010-09-17 18:38:38 -0400280 *
Joe Onorato46439ce2010-11-19 13:56:21 -0800281 * @see #tickerView
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800282 */
283 public CharSequence tickerText;
284
285 /**
Dan Sandler5fcdf6e2014-07-18 11:31:15 -0400286 * Formerly, a view showing the {@link #tickerText}.
287 *
288 * No longer displayed in the status bar as of API 21.
Joe Onoratoef1e7762010-09-17 18:38:38 -0400289 */
Dan Sandler5fcdf6e2014-07-18 11:31:15 -0400290 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -0800291 public RemoteViews tickerView;
Joe Onoratoef1e7762010-09-17 18:38:38 -0400292
293 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400294 * The view that will represent this notification in the notification list (which is pulled
295 * down from the status bar).
296 *
Julia Reynoldsd4ea7412016-02-17 14:00:56 -0500297 * As of N, this field may be null. The notification view is determined by the inputs
298 * to {@link Notification.Builder}; a custom RemoteViews can optionally be
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400299 * supplied with {@link Notification.Builder#setCustomContentView(RemoteViews)}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800300 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400301 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800302 public RemoteViews contentView;
303
Daniel Sandlera0a938c2012-03-15 08:42:37 -0400304 /**
Daniel Sandler4dfbe832012-04-11 14:51:46 -0400305 * A large-format version of {@link #contentView}, giving the Notification an
Daniel Sandlerf3b73432012-03-27 15:01:25 -0400306 * opportunity to show more detail. The system UI may choose to show this
307 * instead of the normal content view at its discretion.
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400308 *
Julia Reynoldsd4ea7412016-02-17 14:00:56 -0500309 * As of N, this field may be null. The expanded notification view is determined by the
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400310 * inputs to {@link Notification.Builder}; a custom RemoteViews can optionally be
311 * supplied with {@link Notification.Builder#setCustomBigContentView(RemoteViews)}.
Daniel Sandlerf3b73432012-03-27 15:01:25 -0400312 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400313 @Deprecated
Daniel Sandlerf3b73432012-03-27 15:01:25 -0400314 public RemoteViews bigContentView;
315
Chris Wren8fd39ec2014-02-27 17:43:26 -0500316
317 /**
Chris Wren47c20a12014-06-18 17:27:29 -0400318 * A medium-format version of {@link #contentView}, providing the Notification an
319 * opportunity to add action buttons to contentView. At its discretion, the system UI may
320 * choose to show this as a heads-up notification, which will pop up so the user can see
321 * it without leaving their current activity.
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400322 *
Julia Reynoldsd4ea7412016-02-17 14:00:56 -0500323 * As of N, this field may be null. The heads-up notification view is determined by the
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400324 * inputs to {@link Notification.Builder}; a custom RemoteViews can optionally be
325 * supplied with {@link Notification.Builder#setCustomHeadsUpContentView(RemoteViews)}.
Chris Wren8fd39ec2014-02-27 17:43:26 -0500326 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400327 @Deprecated
Chris Wren8fd39ec2014-02-27 17:43:26 -0500328 public RemoteViews headsUpContentView;
329
Daniel Sandlerf3b73432012-03-27 15:01:25 -0400330 /**
Dan Sandler86647982015-05-13 23:41:13 -0400331 * A large bitmap to be shown in the notification content area.
332 *
333 * @deprecated Use {@link Builder#setLargeIcon(Icon)} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800334 */
Dan Sandler86647982015-05-13 23:41:13 -0400335 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -0800336 public Bitmap largeIcon;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800337
338 /**
339 * The sound to play.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500340 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800341 * <p>
Chris Wren47c20a12014-06-18 17:27:29 -0400342 * A notification that is noisy is more likely to be presented as a heads-up notification.
343 * </p>
344 *
345 * <p>
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500346 * To play the default notification sound, see {@link #defaults}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800347 * </p>
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500348 * @deprecated use {@link NotificationChannel#getSound()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800349 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500350 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800351 public Uri sound;
352
353 /**
354 * Use this constant as the value for audioStreamType to request that
355 * the default stream type for notifications be used. Currently the
Jeff Sharkey098d5802012-04-26 17:30:34 -0700356 * default stream type is {@link AudioManager#STREAM_NOTIFICATION}.
John Spurlockc0650f022014-07-19 13:22:39 -0400357 *
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500358 * @deprecated Use {@link NotificationChannel#getAudioAttributes()} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800359 */
Jean-Michel Trivi81f871e2014-08-06 16:32:38 -0700360 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800361 public static final int STREAM_DEFAULT = -1;
362
363 /**
364 * The audio stream type to use when playing the sound.
365 * Should be one of the STREAM_ constants from
366 * {@link android.media.AudioManager}.
John Spurlockc0650f022014-07-19 13:22:39 -0400367 *
368 * @deprecated Use {@link #audioAttributes} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800369 */
Jean-Michel Trivi81f871e2014-08-06 16:32:38 -0700370 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800371 public int audioStreamType = STREAM_DEFAULT;
372
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800373 /**
John Spurlockc0650f022014-07-19 13:22:39 -0400374 * The default value of {@link #audioAttributes}.
375 */
376 public static final AudioAttributes AUDIO_ATTRIBUTES_DEFAULT = new AudioAttributes.Builder()
377 .setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
378 .setUsage(AudioAttributes.USAGE_NOTIFICATION)
379 .build();
380
381 /**
382 * The {@link AudioAttributes audio attributes} to use when playing the sound.
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500383 *
384 * @deprecated use {@link NotificationChannel#getAudioAttributes()} instead.
John Spurlockc0650f022014-07-19 13:22:39 -0400385 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500386 @Deprecated
John Spurlockc0650f022014-07-19 13:22:39 -0400387 public AudioAttributes audioAttributes = AUDIO_ATTRIBUTES_DEFAULT;
388
389 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500390 * The pattern with which to vibrate.
391 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800392 * <p>
393 * To vibrate the default pattern, see {@link #defaults}.
394 * </p>
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500395 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800396 * @see android.os.Vibrator#vibrate(long[],int)
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500397 * @deprecated use {@link NotificationChannel#getVibrationPattern()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800398 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500399 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800400 public long[] vibrate;
401
402 /**
403 * The color of the led. The hardware will do its best approximation.
404 *
405 * @see #FLAG_SHOW_LIGHTS
406 * @see #flags
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500407 * @deprecated use {@link NotificationChannel#shouldShowLights()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800408 */
Tor Norbye80756e32015-03-02 09:39:27 -0800409 @ColorInt
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500410 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800411 public int ledARGB;
412
413 /**
414 * The number of milliseconds for the LED to be on while it's flashing.
415 * The hardware will do its best approximation.
416 *
417 * @see #FLAG_SHOW_LIGHTS
418 * @see #flags
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500419 * @deprecated use {@link NotificationChannel#shouldShowLights()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800420 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500421 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800422 public int ledOnMS;
423
424 /**
425 * The number of milliseconds for the LED to be off while it's flashing.
426 * The hardware will do its best approximation.
427 *
428 * @see #FLAG_SHOW_LIGHTS
429 * @see #flags
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500430 *
431 * @deprecated use {@link NotificationChannel#shouldShowLights()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800432 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500433 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800434 public int ledOffMS;
435
436 /**
437 * Specifies which values should be taken from the defaults.
438 * <p>
439 * To set, OR the desired from {@link #DEFAULT_SOUND},
440 * {@link #DEFAULT_VIBRATE}, {@link #DEFAULT_LIGHTS}. For all default
441 * values, use {@link #DEFAULT_ALL}.
442 * </p>
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500443 *
444 * @deprecated use {@link NotificationChannel#getSound()} and
445 * {@link NotificationChannel#shouldShowLights()} and
446 * {@link NotificationChannel#shouldVibrate()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800447 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500448 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800449 public int defaults;
450
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800451 /**
452 * Bit to be bitwise-ored into the {@link #flags} field that should be
453 * set if you want the LED on for this notification.
454 * <ul>
455 * <li>To turn the LED off, pass 0 in the alpha channel for colorARGB
456 * or 0 for both ledOnMS and ledOffMS.</li>
457 * <li>To turn the LED on, pass 1 for ledOnMS and 0 for ledOffMS.</li>
458 * <li>To flash the LED, pass the number of milliseconds that it should
459 * be on and off to ledOnMS and ledOffMS.</li>
460 * </ul>
461 * <p>
462 * Since hardware varies, you are not guaranteed that any of the values
463 * you pass are honored exactly. Use the system defaults (TODO) if possible
464 * because they will be set to values that work on any given hardware.
465 * <p>
466 * The alpha channel must be set for forward compatibility.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500467 *
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500468 * @deprecated use {@link NotificationChannel#shouldShowLights()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800469 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500470 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800471 public static final int FLAG_SHOW_LIGHTS = 0x00000001;
472
473 /**
474 * Bit to be bitwise-ored into the {@link #flags} field that should be
475 * set if this notification is in reference to something that is ongoing,
476 * like a phone call. It should not be set if this notification is in
477 * reference to something that happened at a particular point in time,
478 * like a missed phone call.
479 */
480 public static final int FLAG_ONGOING_EVENT = 0x00000002;
481
482 /**
483 * Bit to be bitwise-ored into the {@link #flags} field that if set,
Scott Mainb8b36452009-04-26 15:50:49 -0700484 * the audio will be repeated until the notification is
485 * cancelled or the notification window is opened.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800486 */
487 public static final int FLAG_INSISTENT = 0x00000004;
488
489 /**
490 * Bit to be bitwise-ored into the {@link #flags} field that should be
Griff Hazen293977b2014-04-28 08:37:20 -0700491 * set if you would only like the sound, vibrate and ticker to be played
492 * if the notification was not already showing.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800493 */
494 public static final int FLAG_ONLY_ALERT_ONCE = 0x00000008;
495
496 /**
497 * Bit to be bitwise-ored into the {@link #flags} field that should be
498 * set if the notification should be canceled when it is clicked by the
Daniel Sandler8aa9ae62012-12-04 23:31:47 -0500499 * user.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800500 */
501 public static final int FLAG_AUTO_CANCEL = 0x00000010;
502
503 /**
504 * Bit to be bitwise-ored into the {@link #flags} field that should be
505 * set if the notification should not be canceled when the user clicks
506 * the Clear all button.
507 */
508 public static final int FLAG_NO_CLEAR = 0x00000020;
509
Dianne Hackbornd8a43f62009-08-17 23:33:56 -0700510 /**
511 * Bit to be bitwise-ored into the {@link #flags} field that should be
512 * set if this notification represents a currently running service. This
513 * will normally be set for you by {@link Service#startForeground}.
514 */
515 public static final int FLAG_FOREGROUND_SERVICE = 0x00000040;
516
Daniel Sandlere46cbd32010-06-17 10:35:26 -0400517 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500518 * Obsolete flag indicating high-priority notifications; use the priority field instead.
Joe Malin8d40d042012-11-05 11:36:40 -0800519 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500520 * @deprecated Use {@link #priority} with a positive value.
Daniel Sandlere46cbd32010-06-17 10:35:26 -0400521 */
Aurimas Liutikas514c5ef2016-05-24 15:22:55 -0700522 @Deprecated
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500523 public static final int FLAG_HIGH_PRIORITY = 0x00000080;
Daniel Sandlere46cbd32010-06-17 10:35:26 -0400524
Griff Hazendfcb0802014-02-11 12:00:00 -0800525 /**
526 * Bit to be bitswise-ored into the {@link #flags} field that should be
527 * set if this notification is relevant to the current device only
528 * and it is not recommended that it bridge to other devices.
529 */
530 public static final int FLAG_LOCAL_ONLY = 0x00000100;
531
Griff Hazen5cadc3b2014-05-20 09:55:39 -0700532 /**
533 * Bit to be bitswise-ored into the {@link #flags} field that should be
534 * set if this notification is the group summary for a group of notifications.
535 * Grouped notifications may display in a cluster or stack on devices which
536 * support such rendering. Requires a group key also be set using {@link Builder#setGroup}.
537 */
538 public static final int FLAG_GROUP_SUMMARY = 0x00000200;
539
Julia Reynoldse46bb372016-03-17 11:05:58 -0400540 /**
541 * Bit to be bitswise-ored into the {@link #flags} field that should be
542 * set if this notification is the group summary for an auto-group of notifications.
543 *
544 * @hide
545 */
546 @SystemApi
547 public static final int FLAG_AUTOGROUP_SUMMARY = 0x00000400;
548
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800549 public int flags;
550
Tor Norbyed9273d62013-05-30 15:59:53 -0700551 /** @hide */
552 @IntDef({PRIORITY_DEFAULT,PRIORITY_LOW,PRIORITY_MIN,PRIORITY_HIGH,PRIORITY_MAX})
553 @Retention(RetentionPolicy.SOURCE)
554 public @interface Priority {}
555
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800556 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500557 * Default notification {@link #priority}. If your application does not prioritize its own
558 * notifications, use this value for all notifications.
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500559 *
560 * @deprecated use {@link NotificationManager#IMPORTANCE_DEFAULT} instead.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500561 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500562 @Deprecated
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500563 public static final int PRIORITY_DEFAULT = 0;
564
565 /**
566 * Lower {@link #priority}, for items that are less important. The UI may choose to show these
567 * items smaller, or at a different position in the list, compared with your app's
568 * {@link #PRIORITY_DEFAULT} items.
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500569 *
570 * @deprecated use {@link NotificationManager#IMPORTANCE_LOW} instead.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500571 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500572 @Deprecated
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500573 public static final int PRIORITY_LOW = -1;
574
575 /**
576 * Lowest {@link #priority}; these items might not be shown to the user except under special
577 * circumstances, such as detailed notification logs.
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500578 *
579 * @deprecated use {@link NotificationManager#IMPORTANCE_MIN} instead.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500580 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500581 @Deprecated
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500582 public static final int PRIORITY_MIN = -2;
583
584 /**
585 * Higher {@link #priority}, for more important notifications or alerts. The UI may choose to
586 * show these items larger, or at a different position in notification lists, compared with
587 * your app's {@link #PRIORITY_DEFAULT} items.
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500588 *
589 * @deprecated use {@link NotificationManager#IMPORTANCE_HIGH} instead.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500590 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500591 @Deprecated
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500592 public static final int PRIORITY_HIGH = 1;
593
594 /**
595 * Highest {@link #priority}, for your application's most important items that require the
596 * user's prompt attention or input.
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500597 *
598 * @deprecated use {@link NotificationManager#IMPORTANCE_HIGH} instead.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500599 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500600 @Deprecated
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500601 public static final int PRIORITY_MAX = 2;
602
603 /**
604 * Relative priority for this notification.
Joe Malin8d40d042012-11-05 11:36:40 -0800605 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500606 * Priority is an indication of how much of the user's valuable attention should be consumed by
607 * this notification. Low-priority notifications may be hidden from the user in certain
608 * situations, while the user might be interrupted for a higher-priority notification. The
Daniel Sandler6738eee2012-11-16 12:03:32 -0500609 * system will make a determination about how to interpret this priority when presenting
610 * the notification.
Chris Wren47c20a12014-06-18 17:27:29 -0400611 *
612 * <p>
613 * A notification that is at least {@link #PRIORITY_HIGH} is more likely to be presented
614 * as a heads-up notification.
615 * </p>
616 *
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500617 * @deprecated use {@link NotificationChannel#getImportance()} instead.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500618 */
Tor Norbyed9273d62013-05-30 15:59:53 -0700619 @Priority
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500620 @Deprecated
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500621 public int priority;
Joe Malin8d40d042012-11-05 11:36:40 -0800622
Dan Sandler26e81cf2014-05-06 10:01:27 -0400623 /**
624 * Accent color (an ARGB integer like the constants in {@link android.graphics.Color})
625 * to be applied by the standard Style templates when presenting this notification.
626 *
627 * The current template design constructs a colorful header image by overlaying the
628 * {@link #icon} image (stenciled in white) atop a field of this color. Alpha components are
629 * ignored.
630 */
Tor Norbye80756e32015-03-02 09:39:27 -0800631 @ColorInt
Dan Sandler26e81cf2014-05-06 10:01:27 -0400632 public int color = COLOR_DEFAULT;
633
634 /**
635 * Special value of {@link #color} telling the system not to decorate this notification with
636 * any special color but instead use default colors when presenting this notification.
637 */
Tor Norbye80756e32015-03-02 09:39:27 -0800638 @ColorInt
Dan Sandler26e81cf2014-05-06 10:01:27 -0400639 public static final int COLOR_DEFAULT = 0; // AKA Color.TRANSPARENT
Dan Sandler0bf2ed82013-12-21 23:33:41 -0600640
641 /**
Adrian Roos4ff3b122016-02-01 12:26:13 -0800642 * Special value of {@link #color} used as a place holder for an invalid color.
Selim Cinek99104832017-01-25 14:47:33 -0800643 * @hide
Adrian Roos4ff3b122016-02-01 12:26:13 -0800644 */
645 @ColorInt
Selim Cinek99104832017-01-25 14:47:33 -0800646 public static final int COLOR_INVALID = 1;
Adrian Roos4ff3b122016-02-01 12:26:13 -0800647
648 /**
Adrian Roosc1a80b02016-04-05 14:54:55 -0700649 * Sphere of visibility of this notification, which affects how and when the SystemUI reveals
650 * the notification's presence and contents in untrusted situations (namely, on the secure
Dan Sandler0bf2ed82013-12-21 23:33:41 -0600651 * lockscreen).
652 *
653 * The default level, {@link #VISIBILITY_PRIVATE}, behaves exactly as notifications have always
654 * done on Android: The notification's {@link #icon} and {@link #tickerText} (if available) are
655 * shown in all situations, but the contents are only available if the device is unlocked for
656 * the appropriate user.
657 *
658 * A more permissive policy can be expressed by {@link #VISIBILITY_PUBLIC}; such a notification
659 * can be read even in an "insecure" context (that is, above a secure lockscreen).
660 * To modify the public version of this notification—for example, to redact some portions—see
661 * {@link Builder#setPublicVersion(Notification)}.
662 *
663 * Finally, a notification can be made {@link #VISIBILITY_SECRET}, which will suppress its icon
664 * and ticker until the user has bypassed the lockscreen.
665 */
666 public int visibility;
667
Griff Hazenfc3922d2014-08-20 11:56:44 -0700668 /**
669 * Notification visibility: Show this notification in its entirety on all lockscreens.
670 *
671 * {@see #visibility}
672 */
Dan Sandler0bf2ed82013-12-21 23:33:41 -0600673 public static final int VISIBILITY_PUBLIC = 1;
Griff Hazenfc3922d2014-08-20 11:56:44 -0700674
675 /**
676 * Notification visibility: Show this notification on all lockscreens, but conceal sensitive or
677 * private information on secure lockscreens.
678 *
679 * {@see #visibility}
680 */
Dan Sandler0bf2ed82013-12-21 23:33:41 -0600681 public static final int VISIBILITY_PRIVATE = 0;
Griff Hazenfc3922d2014-08-20 11:56:44 -0700682
683 /**
684 * Notification visibility: Do not reveal any part of this notification on a secure lockscreen.
685 *
686 * {@see #visibility}
687 */
Dan Sandler0bf2ed82013-12-21 23:33:41 -0600688 public static final int VISIBILITY_SECRET = -1;
689
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500690 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400691 * Notification category: incoming call (voice or video) or similar synchronous communication request.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500692 */
John Spurlockfd7f1e02014-03-18 16:41:57 -0400693 public static final String CATEGORY_CALL = "call";
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500694
695 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400696 * Notification category: incoming direct message (SMS, instant message, etc.).
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500697 */
John Spurlockfd7f1e02014-03-18 16:41:57 -0400698 public static final String CATEGORY_MESSAGE = "msg";
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500699
700 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400701 * Notification category: asynchronous bulk message (email).
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500702 */
John Spurlockfd7f1e02014-03-18 16:41:57 -0400703 public static final String CATEGORY_EMAIL = "email";
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500704
705 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400706 * Notification category: calendar event.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500707 */
John Spurlockfd7f1e02014-03-18 16:41:57 -0400708 public static final String CATEGORY_EVENT = "event";
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500709
710 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400711 * Notification category: promotion or advertisement.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500712 */
John Spurlockfd7f1e02014-03-18 16:41:57 -0400713 public static final String CATEGORY_PROMO = "promo";
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500714
715 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400716 * Notification category: alarm or timer.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500717 */
John Spurlockfd7f1e02014-03-18 16:41:57 -0400718 public static final String CATEGORY_ALARM = "alarm";
719
720 /**
721 * Notification category: progress of a long-running background operation.
722 */
723 public static final String CATEGORY_PROGRESS = "progress";
724
725 /**
726 * Notification category: social network or sharing update.
727 */
728 public static final String CATEGORY_SOCIAL = "social";
729
730 /**
731 * Notification category: error in background operation or authentication status.
732 */
733 public static final String CATEGORY_ERROR = "err";
734
735 /**
736 * Notification category: media transport control for playback.
737 */
738 public static final String CATEGORY_TRANSPORT = "transport";
739
740 /**
741 * Notification category: system or device status update. Reserved for system use.
742 */
743 public static final String CATEGORY_SYSTEM = "sys";
744
745 /**
746 * Notification category: indication of running background service.
747 */
748 public static final String CATEGORY_SERVICE = "service";
749
750 /**
John Spurlock0a69c8c2014-03-21 13:30:57 -0400751 * Notification category: a specific, timely recommendation for a single thing.
752 * For example, a news app might want to recommend a news story it believes the user will
753 * want to read next.
754 */
755 public static final String CATEGORY_RECOMMENDATION = "recommendation";
756
757 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400758 * Notification category: ongoing information about device or contextual status.
759 */
760 public static final String CATEGORY_STATUS = "status";
761
762 /**
John Spurlock24d3dad2015-04-02 12:24:02 -0400763 * Notification category: user-scheduled reminder.
764 */
765 public static final String CATEGORY_REMINDER = "reminder";
766
767 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400768 * One of the predefined notification categories (see the <code>CATEGORY_*</code> constants)
769 * that best describes this Notification. May be used by the system for ranking and filtering.
770 */
771 public String category;
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500772
Griff Hazen5cadc3b2014-05-20 09:55:39 -0700773 private String mGroupKey;
774
775 /**
776 * Get the key used to group this notification into a cluster or stack
777 * with other notifications on devices which support such rendering.
778 */
779 public String getGroup() {
780 return mGroupKey;
781 }
782
783 private String mSortKey;
784
785 /**
786 * Get a sort key that orders this notification among other notifications from the
787 * same package. This can be useful if an external sort was already applied and an app
788 * would like to preserve this. Notifications will be sorted lexicographically using this
789 * value, although providing different priorities in addition to providing sort key may
790 * cause this value to be ignored.
791 *
792 * <p>This sort key can also be used to order members of a notification group. See
793 * {@link Builder#setGroup}.
794 *
795 * @see String#compareTo(String)
796 */
797 public String getSortKey() {
798 return mSortKey;
799 }
800
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500801 /**
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400802 * Additional semantic data to be carried around with this Notification.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400803 * <p>
804 * The extras keys defined here are intended to capture the original inputs to {@link Builder}
805 * APIs, and are intended to be used by
806 * {@link android.service.notification.NotificationListenerService} implementations to extract
807 * detailed information from notification objects.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500808 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400809 public Bundle extras = new Bundle();
810
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400811 /**
Felipe Lemedd85da62016-06-28 11:29:54 -0700812 * All pending intents in the notification as the system needs to be able to access them but
813 * touching the extras bundle in the system process is not safe because the bundle may contain
Svet Ganovddb94882016-06-23 19:55:24 -0700814 * custom parcelable objects.
815 *
816 * @hide
817 */
Felipe Lemedd85da62016-06-28 11:29:54 -0700818 public ArraySet<PendingIntent> allPendingIntents;
Svet Ganovddb94882016-06-23 19:55:24 -0700819
820 /**
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400821 * {@link #extras} key: this is the title of the notification,
822 * as supplied to {@link Builder#setContentTitle(CharSequence)}.
823 */
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -0500824 public static final String EXTRA_TITLE = "android.title";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400825
826 /**
827 * {@link #extras} key: this is the title of the notification when shown in expanded form,
828 * e.g. as supplied to {@link BigTextStyle#setBigContentTitle(CharSequence)}.
829 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400830 public static final String EXTRA_TITLE_BIG = EXTRA_TITLE + ".big";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400831
832 /**
833 * {@link #extras} key: this is the main text payload, as supplied to
834 * {@link Builder#setContentText(CharSequence)}.
835 */
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -0500836 public static final String EXTRA_TEXT = "android.text";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400837
838 /**
839 * {@link #extras} key: this is a third line of text, as supplied to
840 * {@link Builder#setSubText(CharSequence)}.
841 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400842 public static final String EXTRA_SUB_TEXT = "android.subText";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400843
844 /**
Adrian Roose458aa82015-12-08 16:17:19 -0800845 * {@link #extras} key: this is the remote input history, as supplied to
846 * {@link Builder#setRemoteInputHistory(CharSequence[])}.
Adrian Roos005e7742016-04-13 15:02:20 -0700847 *
848 * Apps can fill this through {@link Builder#setRemoteInputHistory(CharSequence[])}
849 * with the most recent inputs that have been sent through a {@link RemoteInput} of this
850 * Notification and are expected to clear it once the it is no longer relevant (e.g. for chat
851 * notifications once the other party has responded).
852 *
853 * The extra with this key is of type CharSequence[] and contains the most recent entry at
854 * the 0 index, the second most recent at the 1 index, etc.
855 *
856 * @see Builder#setRemoteInputHistory(CharSequence[])
Adrian Roose458aa82015-12-08 16:17:19 -0800857 */
858 public static final String EXTRA_REMOTE_INPUT_HISTORY = "android.remoteInputHistory";
859
860 /**
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400861 * {@link #extras} key: this is a small piece of additional text as supplied to
862 * {@link Builder#setContentInfo(CharSequence)}.
863 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400864 public static final String EXTRA_INFO_TEXT = "android.infoText";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400865
866 /**
867 * {@link #extras} key: this is a line of summary information intended to be shown
868 * alongside expanded notifications, as supplied to (e.g.)
869 * {@link BigTextStyle#setSummaryText(CharSequence)}.
870 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400871 public static final String EXTRA_SUMMARY_TEXT = "android.summaryText";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400872
873 /**
Christoph Studer4600f9b2014-07-22 22:44:43 +0200874 * {@link #extras} key: this is the longer text shown in the big form of a
875 * {@link BigTextStyle} notification, as supplied to
876 * {@link BigTextStyle#bigText(CharSequence)}.
877 */
878 public static final String EXTRA_BIG_TEXT = "android.bigText";
879
880 /**
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400881 * {@link #extras} key: this is the resource ID of the notification's main small icon, as
882 * supplied to {@link Builder#setSmallIcon(int)}.
Julia Reynoldse071abd2017-03-22 10:52:11 -0400883 *
884 * @deprecated Use {@link #getSmallIcon()}, which supports a wider variety of icon sources.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400885 */
Julia Reynoldse071abd2017-03-22 10:52:11 -0400886 @Deprecated
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -0500887 public static final String EXTRA_SMALL_ICON = "android.icon";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400888
889 /**
890 * {@link #extras} key: this is a bitmap to be used instead of the small icon when showing the
891 * notification payload, as
892 * supplied to {@link Builder#setLargeIcon(android.graphics.Bitmap)}.
Julia Reynoldse071abd2017-03-22 10:52:11 -0400893 *
894 * @deprecated Use {@link #getLargeIcon()}, which supports a wider variety of icon sources.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400895 */
Julia Reynoldse071abd2017-03-22 10:52:11 -0400896 @Deprecated
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400897 public static final String EXTRA_LARGE_ICON = "android.largeIcon";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400898
899 /**
900 * {@link #extras} key: this is a bitmap to be used instead of the one from
901 * {@link Builder#setLargeIcon(android.graphics.Bitmap)} when the notification is
902 * shown in its expanded form, as supplied to
903 * {@link BigPictureStyle#bigLargeIcon(android.graphics.Bitmap)}.
904 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400905 public static final String EXTRA_LARGE_ICON_BIG = EXTRA_LARGE_ICON + ".big";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400906
907 /**
908 * {@link #extras} key: this is the progress value supplied to
909 * {@link Builder#setProgress(int, int, boolean)}.
910 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400911 public static final String EXTRA_PROGRESS = "android.progress";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400912
913 /**
914 * {@link #extras} key: this is the maximum value supplied to
915 * {@link Builder#setProgress(int, int, boolean)}.
916 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400917 public static final String EXTRA_PROGRESS_MAX = "android.progressMax";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400918
919 /**
920 * {@link #extras} key: whether the progress bar is indeterminate, supplied to
921 * {@link Builder#setProgress(int, int, boolean)}.
922 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400923 public static final String EXTRA_PROGRESS_INDETERMINATE = "android.progressIndeterminate";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400924
925 /**
926 * {@link #extras} key: whether {@link #when} should be shown as a count-up timer (specifically
927 * a {@link android.widget.Chronometer}) instead of a timestamp, as supplied to
928 * {@link Builder#setUsesChronometer(boolean)}.
929 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400930 public static final String EXTRA_SHOW_CHRONOMETER = "android.showChronometer";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400931
932 /**
Selim Cinek81c23aa2016-02-25 16:23:13 -0800933 * {@link #extras} key: whether the chronometer set on the notification should count down
934 * instead of counting up. Is only relevant if key {@link #EXTRA_SHOW_CHRONOMETER} is present.
Adrian Roos96b7e202016-05-17 13:50:38 -0700935 * This extra is a boolean. The default is false.
Selim Cinek81c23aa2016-02-25 16:23:13 -0800936 */
Adrian Roos96b7e202016-05-17 13:50:38 -0700937 public static final String EXTRA_CHRONOMETER_COUNT_DOWN = "android.chronometerCountDown";
Selim Cinek81c23aa2016-02-25 16:23:13 -0800938
939 /**
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400940 * {@link #extras} key: whether {@link #when} should be shown,
941 * as supplied to {@link Builder#setShowWhen(boolean)}.
942 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400943 public static final String EXTRA_SHOW_WHEN = "android.showWhen";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400944
945 /**
946 * {@link #extras} key: this is a bitmap to be shown in {@link BigPictureStyle} expanded
947 * notifications, supplied to {@link BigPictureStyle#bigPicture(android.graphics.Bitmap)}.
948 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400949 public static final String EXTRA_PICTURE = "android.picture";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400950
951 /**
952 * {@link #extras} key: An array of CharSequences to show in {@link InboxStyle} expanded
953 * notifications, each of which was supplied to {@link InboxStyle#addLine(CharSequence)}.
954 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400955 public static final String EXTRA_TEXT_LINES = "android.textLines";
Dan Sandler842dd772014-05-15 09:36:47 -0400956
957 /**
958 * {@link #extras} key: A string representing the name of the specific
959 * {@link android.app.Notification.Style} used to create this notification.
960 */
Chris Wren91ad5632013-06-05 15:05:57 -0400961 public static final String EXTRA_TEMPLATE = "android.template";
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400962
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400963 /**
Chris Wrene6c48932014-09-29 17:19:27 -0400964 * {@link #extras} key: A String array containing the people that this notification relates to,
965 * each of which was supplied to {@link Builder#addPerson(String)}.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400966 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400967 public static final String EXTRA_PEOPLE = "android.people";
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500968
969 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400970 * Allow certain system-generated notifications to appear before the device is provisioned.
971 * Only available to notifications coming from the android package.
972 * @hide
973 */
Maurice Lam96c10032017-03-29 15:42:38 -0700974 @SystemApi
John Spurlockfd7f1e02014-03-18 16:41:57 -0400975 public static final String EXTRA_ALLOW_DURING_SETUP = "android.allowDuringSetup";
976
977 /**
Jose Limae9e3b3b2014-05-18 23:44:50 -0700978 * {@link #extras} key: A
979 * {@link android.content.ContentUris content URI} pointing to an image that can be displayed
980 * in the background when the notification is selected. The URI must point to an image stream
981 * suitable for passing into
982 * {@link android.graphics.BitmapFactory#decodeStream(java.io.InputStream)
983 * BitmapFactory.decodeStream}; all other content types will be ignored. The content provider
984 * URI used for this purpose must require no permissions to read the image data.
985 */
986 public static final String EXTRA_BACKGROUND_IMAGE_URI = "android.backgroundImageUri";
987
988 /**
Dan Sandler842dd772014-05-15 09:36:47 -0400989 * {@link #extras} key: A
Jeff Browndba34ba2014-06-24 20:46:03 -0700990 * {@link android.media.session.MediaSession.Token} associated with a
Dan Sandler842dd772014-05-15 09:36:47 -0400991 * {@link android.app.Notification.MediaStyle} notification.
992 */
993 public static final String EXTRA_MEDIA_SESSION = "android.mediaSession";
994
995 /**
Bryan Mawhinneye191f902014-07-22 12:50:09 +0100996 * {@link #extras} key: the indices of actions to be shown in the compact view,
997 * as supplied to (e.g.) {@link MediaStyle#setShowActionsInCompactView(int...)}.
998 */
999 public static final String EXTRA_COMPACT_ACTIONS = "android.compactActions";
1000
Christoph Studer943aa672014-08-03 20:31:16 +02001001 /**
Alex Hillsfc737de2016-03-23 17:33:02 -04001002 * {@link #extras} key: the username to be displayed for all messages sent by the user including
1003 * direct replies
Adrian Roos96b7e202016-05-17 13:50:38 -07001004 * {@link android.app.Notification.MessagingStyle} notification. This extra is a
1005 * {@link CharSequence}
Alex Hillsfc737de2016-03-23 17:33:02 -04001006 */
1007 public static final String EXTRA_SELF_DISPLAY_NAME = "android.selfDisplayName";
1008
1009 /**
Adrian Roos96b7e202016-05-17 13:50:38 -07001010 * {@link #extras} key: a {@link CharSequence} to be displayed as the title to a conversation
Alex Hillsd9b04d92016-04-11 16:38:16 -04001011 * represented by a {@link android.app.Notification.MessagingStyle}
Alex Hillsfc737de2016-03-23 17:33:02 -04001012 */
Alex Hillsd9b04d92016-04-11 16:38:16 -04001013 public static final String EXTRA_CONVERSATION_TITLE = "android.conversationTitle";
Alex Hillsfc737de2016-03-23 17:33:02 -04001014
1015 /**
1016 * {@link #extras} key: an array of {@link android.app.Notification.MessagingStyle.Message}
1017 * bundles provided by a
Adrian Roos96b7e202016-05-17 13:50:38 -07001018 * {@link android.app.Notification.MessagingStyle} notification. This extra is a parcelable
1019 * array of bundles.
Alex Hillsfc737de2016-03-23 17:33:02 -04001020 */
1021 public static final String EXTRA_MESSAGES = "android.messages";
1022
1023 /**
Adrian Roos437cd562017-01-18 15:47:03 -08001024 * {@link #extras} key: an array of
1025 * {@link android.app.Notification.MessagingStyle#addHistoricMessage historic}
1026 * {@link android.app.Notification.MessagingStyle.Message} bundles provided by a
1027 * {@link android.app.Notification.MessagingStyle} notification. This extra is a parcelable
1028 * array of bundles.
1029 */
1030 public static final String EXTRA_HISTORIC_MESSAGES = "android.messages.historic";
1031
1032 /**
Selim Cinek7b9605b2017-01-19 17:36:00 -08001033 * {@link #extras} key: whether the notification should be colorized as
1034 * supplied to {@link Builder#setColorized(boolean)}}.
1035 */
1036 public static final String EXTRA_COLORIZED = "android.colorized";
1037
1038 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -04001039 * @hide
1040 */
1041 public static final String EXTRA_BUILDER_APPLICATION_INFO = "android.appInfo";
1042
Selim Cinek247fa012016-02-18 09:50:48 -08001043 /**
1044 * @hide
1045 */
1046 public static final String EXTRA_CONTAINS_CUSTOM_VIEW = "android.contains.customView";
1047
Shane Brennan472a3b32016-12-12 15:28:10 -08001048 /**
1049 * {@link #extras} key: the audio contents of this notification.
1050 *
1051 * This is for use when rendering the notification on an audio-focused interface;
1052 * the audio contents are a complete sound sample that contains the contents/body of the
1053 * notification. This may be used in substitute of a Text-to-Speech reading of the
1054 * notification. For example if the notification represents a voice message this should point
1055 * to the audio of that message.
1056 *
1057 * The data stored under this key should be a String representation of a Uri that contains the
1058 * audio contents in one of the following formats: WAV, PCM 16-bit, AMR-WB.
1059 *
1060 * This extra is unnecessary if you are using {@code MessagingStyle} since each {@code Message}
1061 * has a field for holding data URI. That field can be used for audio.
1062 * See {@code Message#setData}.
1063 *
1064 * Example usage:
1065 * <pre>
1066 * {@code
1067 * Notification.Builder myBuilder = (build your Notification as normal);
1068 * myBuilder.getExtras().putString(EXTRA_AUDIO_CONTENTS_URI, myAudioUri.toString());
1069 * }
1070 * </pre>
1071 */
1072 public static final String EXTRA_AUDIO_CONTENTS_URI = "android.audioContents";
1073
Dan Sandler80eaa592016-04-14 23:34:54 -04001074 /** @hide */
1075 @SystemApi
Dan Sandler732bd6c2016-04-12 14:20:32 -04001076 public static final String EXTRA_SUBSTITUTE_APP_NAME = "android.substName";
1077
Dan Sandlerd63f9322015-05-06 15:18:49 -04001078 private Icon mSmallIcon;
1079 private Icon mLargeIcon;
1080
Julia Reynoldsb5e44b72016-08-16 15:00:25 -04001081 private String mChannelId;
Julia Reynolds2a128742016-11-28 14:29:25 -05001082 private long mTimeout;
Julia Reynoldsb5e44b72016-08-16 15:00:25 -04001083
Julia Reynolds13d898c2017-02-02 12:22:05 -05001084 private String mShortcutId;
1085
1086 /**
1087 * If this notification is being shown as a badge, always show as a number.
1088 */
1089 public static final int BADGE_ICON_NONE = 0;
1090
1091 /**
1092 * If this notification is being shown as a badge, use the {@link #getSmallIcon()} to
1093 * represent this notification.
1094 */
1095 public static final int BADGE_ICON_SMALL = 1;
1096
1097 /**
1098 * If this notification is being shown as a badge, use the {@link #getLargeIcon()} to
1099 * represent this notification.
1100 */
1101 public static final int BADGE_ICON_LARGE = 2;
Julia Reynolds7d5b4da2017-03-20 10:18:49 -04001102 private int mBadgeIcon = BADGE_ICON_NONE;
Julia Reynolds13d898c2017-02-02 12:22:05 -05001103
Chris Wren51c75102013-07-16 20:49:17 -04001104 /**
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001105 * Structure to encapsulate a named action that can be shown as part of this notification.
1106 * It must include an icon, a label, and a {@link PendingIntent} to be fired when the action is
1107 * selected by the user.
1108 * <p>
Griff Hazen959591e2014-05-15 22:26:18 -07001109 * Apps should use {@link Notification.Builder#addAction(int, CharSequence, PendingIntent)}
1110 * or {@link Notification.Builder#addAction(Notification.Action)}
1111 * to attach actions.
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001112 */
Daniel Sandlerea2a3172013-02-20 22:24:20 -05001113 public static class Action implements Parcelable {
Shane Brennan472a3b32016-12-12 15:28:10 -08001114 /**
1115 * {@link #extras} key: Keys to a {@link Parcelable} {@link ArrayList} of
1116 * {@link RemoteInput}s.
1117 *
1118 * This is intended for {@link RemoteInput}s that only accept data, meaning
1119 * {@link RemoteInput#getAllowFreeFormInput} is false, {@link RemoteInput#getChoices}
1120 * is null or empty, and {@link RemoteInput#getAllowedDataTypes is non-null and not
1121 * empty. These {@link RemoteInput}s will be ignored by devices that do not
1122 * support non-text-based {@link RemoteInput}s. See {@link Builder#build}.
1123 *
1124 * You can test if a RemoteInput matches these constraints using
1125 * {@link RemoteInput#isDataOnly}.
1126 */
1127 private static final String EXTRA_DATA_ONLY_INPUTS = "android.extra.DATA_ONLY_INPUTS";
1128
Griff Hazen959591e2014-05-15 22:26:18 -07001129 private final Bundle mExtras;
Dan Sandler86647982015-05-13 23:41:13 -04001130 private Icon mIcon;
Griff Hazen61a9e862014-05-22 16:05:19 -07001131 private final RemoteInput[] mRemoteInputs;
Alex Hills1f27f882017-01-12 11:24:46 -05001132 private boolean mAllowGeneratedReplies = true;
Griff Hazen959591e2014-05-15 22:26:18 -07001133
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001134 /**
1135 * Small icon representing the action.
Dan Sandler86647982015-05-13 23:41:13 -04001136 *
1137 * @deprecated Use {@link Action#getIcon()} instead.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001138 */
Dan Sandler86647982015-05-13 23:41:13 -04001139 @Deprecated
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001140 public int icon;
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001141
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001142 /**
1143 * Title of the action.
1144 */
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001145 public CharSequence title;
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001146
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001147 /**
1148 * Intent to send when the user invokes this action. May be null, in which case the action
1149 * may be rendered in a disabled presentation by the system UI.
1150 */
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001151 public PendingIntent actionIntent;
Griff Hazen959591e2014-05-15 22:26:18 -07001152
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001153 private Action(Parcel in) {
Dan Sandler86647982015-05-13 23:41:13 -04001154 if (in.readInt() != 0) {
1155 mIcon = Icon.CREATOR.createFromParcel(in);
Dan Sandler68079d52015-07-22 10:45:30 -04001156 if (mIcon.getType() == Icon.TYPE_RESOURCE) {
1157 icon = mIcon.getResId();
1158 }
Dan Sandler86647982015-05-13 23:41:13 -04001159 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001160 title = TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(in);
1161 if (in.readInt() == 1) {
1162 actionIntent = PendingIntent.CREATOR.createFromParcel(in);
1163 }
Jeff Sharkeya04c7a72016-03-18 12:20:36 -06001164 mExtras = Bundle.setDefusable(in.readBundle(), true);
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001165 mRemoteInputs = in.createTypedArray(RemoteInput.CREATOR);
Alex Hills42b0c4d2016-04-26 13:35:36 -04001166 mAllowGeneratedReplies = in.readInt() == 1;
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001167 }
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001168
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001169 /**
Dan Sandler86647982015-05-13 23:41:13 -04001170 * @deprecated Use {@link android.app.Notification.Action.Builder}.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001171 */
Dan Sandler86647982015-05-13 23:41:13 -04001172 @Deprecated
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001173 public Action(int icon, CharSequence title, PendingIntent intent) {
Alex Hills1f27f882017-01-12 11:24:46 -05001174 this(Icon.createWithResource("", icon), title, intent, new Bundle(), null, true);
Griff Hazen959591e2014-05-15 22:26:18 -07001175 }
1176
Adrian Roos7af53622016-10-12 13:44:05 -07001177 /** Keep in sync with {@link Notification.Action.Builder#Builder(Action)}! */
Dan Sandler86647982015-05-13 23:41:13 -04001178 private Action(Icon icon, CharSequence title, PendingIntent intent, Bundle extras,
Alex Hills42b0c4d2016-04-26 13:35:36 -04001179 RemoteInput[] remoteInputs, boolean allowGeneratedReplies) {
Dan Sandler86647982015-05-13 23:41:13 -04001180 this.mIcon = icon;
Gus Prevasf5bff46f2015-08-24 10:34:28 -04001181 if (icon != null && icon.getType() == Icon.TYPE_RESOURCE) {
1182 this.icon = icon.getResId();
1183 }
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001184 this.title = title;
1185 this.actionIntent = intent;
Griff Hazen959591e2014-05-15 22:26:18 -07001186 this.mExtras = extras != null ? extras : new Bundle();
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001187 this.mRemoteInputs = remoteInputs;
Alex Hills42b0c4d2016-04-26 13:35:36 -04001188 this.mAllowGeneratedReplies = allowGeneratedReplies;
Griff Hazen959591e2014-05-15 22:26:18 -07001189 }
1190
1191 /**
Dan Sandler86647982015-05-13 23:41:13 -04001192 * Return an icon representing the action.
1193 */
1194 public Icon getIcon() {
1195 if (mIcon == null && icon != 0) {
1196 // you snuck an icon in here without using the builder; let's try to keep it
1197 mIcon = Icon.createWithResource("", icon);
1198 }
1199 return mIcon;
1200 }
1201
1202 /**
Griff Hazen959591e2014-05-15 22:26:18 -07001203 * Get additional metadata carried around with this Action.
1204 */
1205 public Bundle getExtras() {
1206 return mExtras;
1207 }
1208
1209 /**
Alex Hills42b0c4d2016-04-26 13:35:36 -04001210 * Return whether the platform should automatically generate possible replies for this
1211 * {@link Action}
1212 */
1213 public boolean getAllowGeneratedReplies() {
1214 return mAllowGeneratedReplies;
1215 }
1216
1217 /**
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001218 * Get the list of inputs to be collected from the user when this action is sent.
Shane Brennan472a3b32016-12-12 15:28:10 -08001219 * May return null if no remote inputs were added. Only returns inputs which accept
1220 * a text input. For inputs which only accept data use {@link #getDataOnlyRemoteInputs}.
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001221 */
1222 public RemoteInput[] getRemoteInputs() {
1223 return mRemoteInputs;
1224 }
1225
1226 /**
Shane Brennan472a3b32016-12-12 15:28:10 -08001227 * Get the list of inputs to be collected from the user that ONLY accept data when this
1228 * action is sent. These remote inputs are guaranteed to return true on a call to
1229 * {@link RemoteInput#isDataOnly}.
1230 *
1231 * May return null if no data-only remote inputs were added.
1232 *
1233 * This method exists so that legacy RemoteInput collectors that pre-date the addition
1234 * of non-textual RemoteInputs do not access these remote inputs.
1235 */
1236 public RemoteInput[] getDataOnlyRemoteInputs() {
1237 return (RemoteInput[]) mExtras.getParcelableArray(EXTRA_DATA_ONLY_INPUTS);
1238 }
1239
1240 /**
Griff Hazen959591e2014-05-15 22:26:18 -07001241 * Builder class for {@link Action} objects.
1242 */
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001243 public static final class Builder {
Dan Sandler86647982015-05-13 23:41:13 -04001244 private final Icon mIcon;
Griff Hazen959591e2014-05-15 22:26:18 -07001245 private final CharSequence mTitle;
1246 private final PendingIntent mIntent;
Alex Hills1f27f882017-01-12 11:24:46 -05001247 private boolean mAllowGeneratedReplies = true;
Griff Hazen959591e2014-05-15 22:26:18 -07001248 private final Bundle mExtras;
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001249 private ArrayList<RemoteInput> mRemoteInputs;
Griff Hazen959591e2014-05-15 22:26:18 -07001250
1251 /**
1252 * Construct a new builder for {@link Action} object.
1253 * @param icon icon to show for this action
1254 * @param title the title of the action
1255 * @param intent the {@link PendingIntent} to fire when users trigger this action
1256 */
Dan Sandler86647982015-05-13 23:41:13 -04001257 @Deprecated
Griff Hazen959591e2014-05-15 22:26:18 -07001258 public Builder(int icon, CharSequence title, PendingIntent intent) {
Adrian Roos7af53622016-10-12 13:44:05 -07001259 this(Icon.createWithResource("", icon), title, intent);
Dan Sandler86647982015-05-13 23:41:13 -04001260 }
1261
1262 /**
1263 * Construct a new builder for {@link Action} object.
1264 * @param icon icon to show for this action
1265 * @param title the title of the action
1266 * @param intent the {@link PendingIntent} to fire when users trigger this action
1267 */
1268 public Builder(Icon icon, CharSequence title, PendingIntent intent) {
Alex Hills1f27f882017-01-12 11:24:46 -05001269 this(icon, title, intent, new Bundle(), null, true);
Griff Hazen959591e2014-05-15 22:26:18 -07001270 }
1271
1272 /**
1273 * Construct a new builder for {@link Action} object using the fields from an
1274 * {@link Action}.
1275 * @param action the action to read fields from.
1276 */
1277 public Builder(Action action) {
Adrian Roos7af53622016-10-12 13:44:05 -07001278 this(action.getIcon(), action.title, action.actionIntent,
1279 new Bundle(action.mExtras), action.getRemoteInputs(),
1280 action.getAllowGeneratedReplies());
Griff Hazen959591e2014-05-15 22:26:18 -07001281 }
1282
Dan Sandler86647982015-05-13 23:41:13 -04001283 private Builder(Icon icon, CharSequence title, PendingIntent intent, Bundle extras,
Adrian Roos7af53622016-10-12 13:44:05 -07001284 RemoteInput[] remoteInputs, boolean allowGeneratedReplies) {
Griff Hazen959591e2014-05-15 22:26:18 -07001285 mIcon = icon;
1286 mTitle = title;
1287 mIntent = intent;
1288 mExtras = extras;
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001289 if (remoteInputs != null) {
1290 mRemoteInputs = new ArrayList<RemoteInput>(remoteInputs.length);
1291 Collections.addAll(mRemoteInputs, remoteInputs);
1292 }
Adrian Roos7af53622016-10-12 13:44:05 -07001293 mAllowGeneratedReplies = allowGeneratedReplies;
Griff Hazen959591e2014-05-15 22:26:18 -07001294 }
1295
1296 /**
1297 * Merge additional metadata into this builder.
1298 *
1299 * <p>Values within the Bundle will replace existing extras values in this Builder.
1300 *
1301 * @see Notification.Action#extras
1302 */
1303 public Builder addExtras(Bundle extras) {
1304 if (extras != null) {
1305 mExtras.putAll(extras);
1306 }
1307 return this;
1308 }
1309
1310 /**
1311 * Get the metadata Bundle used by this Builder.
1312 *
1313 * <p>The returned Bundle is shared with this Builder.
1314 */
1315 public Bundle getExtras() {
1316 return mExtras;
1317 }
1318
1319 /**
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001320 * Add an input to be collected from the user when this action is sent.
1321 * Response values can be retrieved from the fired intent by using the
1322 * {@link RemoteInput#getResultsFromIntent} function.
1323 * @param remoteInput a {@link RemoteInput} to add to the action
1324 * @return this object for method chaining
1325 */
1326 public Builder addRemoteInput(RemoteInput remoteInput) {
1327 if (mRemoteInputs == null) {
1328 mRemoteInputs = new ArrayList<RemoteInput>();
1329 }
1330 mRemoteInputs.add(remoteInput);
1331 return this;
1332 }
1333
1334 /**
Alex Hills42b0c4d2016-04-26 13:35:36 -04001335 * Set whether the platform should automatically generate possible replies to add to
1336 * {@link RemoteInput#getChoices()}. If the {@link Action} doesn't have a
1337 * {@link RemoteInput}, this has no effect.
1338 * @param allowGeneratedReplies {@code true} to allow generated replies, {@code false}
1339 * otherwise
1340 * @return this object for method chaining
Alex Hills1f27f882017-01-12 11:24:46 -05001341 * The default value is {@code true}
Alex Hills42b0c4d2016-04-26 13:35:36 -04001342 */
1343 public Builder setAllowGeneratedReplies(boolean allowGeneratedReplies) {
1344 mAllowGeneratedReplies = allowGeneratedReplies;
1345 return this;
1346 }
1347
1348 /**
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001349 * Apply an extender to this action builder. Extenders may be used to add
1350 * metadata or change options on this builder.
1351 */
Griff Hazen61a9e862014-05-22 16:05:19 -07001352 public Builder extend(Extender extender) {
1353 extender.extend(this);
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001354 return this;
1355 }
1356
1357 /**
Griff Hazen959591e2014-05-15 22:26:18 -07001358 * Combine all of the options that have been set and return a new {@link Action}
1359 * object.
1360 * @return the built action
1361 */
1362 public Action build() {
Shane Brennan472a3b32016-12-12 15:28:10 -08001363 ArrayList<RemoteInput> dataOnlyInputs = new ArrayList<>();
1364 RemoteInput[] previousDataInputs =
1365 (RemoteInput[]) mExtras.getParcelableArray(EXTRA_DATA_ONLY_INPUTS);
Felipe Lemedfd11962017-01-18 18:38:46 -08001366 if (previousDataInputs != null) {
Shane Brennan472a3b32016-12-12 15:28:10 -08001367 for (RemoteInput input : previousDataInputs) {
1368 dataOnlyInputs.add(input);
1369 }
1370 }
1371 List<RemoteInput> textInputs = new ArrayList<>();
1372 if (mRemoteInputs != null) {
1373 for (RemoteInput input : mRemoteInputs) {
1374 if (input.isDataOnly()) {
1375 dataOnlyInputs.add(input);
1376 } else {
1377 textInputs.add(input);
1378 }
1379 }
1380 }
1381 if (!dataOnlyInputs.isEmpty()) {
1382 RemoteInput[] dataInputsArr =
1383 dataOnlyInputs.toArray(new RemoteInput[dataOnlyInputs.size()]);
1384 mExtras.putParcelableArray(EXTRA_DATA_ONLY_INPUTS, dataInputsArr);
1385 }
1386 RemoteInput[] textInputsArr = textInputs.isEmpty()
1387 ? null : textInputs.toArray(new RemoteInput[textInputs.size()]);
1388 return new Action(mIcon, mTitle, mIntent, mExtras, textInputsArr,
Alex Hills42b0c4d2016-04-26 13:35:36 -04001389 mAllowGeneratedReplies);
Griff Hazen959591e2014-05-15 22:26:18 -07001390 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001391 }
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001392
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001393 @Override
1394 public Action clone() {
1395 return new Action(
Dan Sandler86647982015-05-13 23:41:13 -04001396 getIcon(),
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001397 title,
1398 actionIntent, // safe to alias
Julia Reynolds53c934c2016-09-16 14:03:43 -04001399 mExtras == null ? new Bundle() : new Bundle(mExtras),
Alex Hills42b0c4d2016-04-26 13:35:36 -04001400 getRemoteInputs(),
1401 getAllowGeneratedReplies());
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001402 }
1403 @Override
1404 public int describeContents() {
1405 return 0;
1406 }
1407 @Override
1408 public void writeToParcel(Parcel out, int flags) {
Dan Sandler86647982015-05-13 23:41:13 -04001409 final Icon ic = getIcon();
1410 if (ic != null) {
1411 out.writeInt(1);
1412 ic.writeToParcel(out, 0);
1413 } else {
1414 out.writeInt(0);
1415 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001416 TextUtils.writeToParcel(title, out, flags);
1417 if (actionIntent != null) {
1418 out.writeInt(1);
1419 actionIntent.writeToParcel(out, flags);
1420 } else {
1421 out.writeInt(0);
1422 }
Griff Hazen959591e2014-05-15 22:26:18 -07001423 out.writeBundle(mExtras);
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001424 out.writeTypedArray(mRemoteInputs, flags);
Alex Hills42b0c4d2016-04-26 13:35:36 -04001425 out.writeInt(mAllowGeneratedReplies ? 1 : 0);
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001426 }
Griff Hazen959591e2014-05-15 22:26:18 -07001427 public static final Parcelable.Creator<Action> CREATOR =
1428 new Parcelable.Creator<Action>() {
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001429 public Action createFromParcel(Parcel in) {
1430 return new Action(in);
1431 }
1432 public Action[] newArray(int size) {
1433 return new Action[size];
1434 }
1435 };
Griff Hazen61a9e862014-05-22 16:05:19 -07001436
1437 /**
1438 * Extender interface for use with {@link Builder#extend}. Extenders may be used to add
1439 * metadata or change options on an action builder.
1440 */
1441 public interface Extender {
1442 /**
1443 * Apply this extender to a notification action builder.
1444 * @param builder the builder to be modified.
1445 * @return the build object for chaining.
1446 */
1447 public Builder extend(Builder builder);
1448 }
1449
1450 /**
1451 * Wearable extender for notification actions. To add extensions to an action,
1452 * create a new {@link android.app.Notification.Action.WearableExtender} object using
1453 * the {@code WearableExtender()} constructor and apply it to a
1454 * {@link android.app.Notification.Action.Builder} using
1455 * {@link android.app.Notification.Action.Builder#extend}.
1456 *
1457 * <pre class="prettyprint">
1458 * Notification.Action action = new Notification.Action.Builder(
1459 * R.drawable.archive_all, "Archive all", actionIntent)
Griff Hazen14f57992014-05-26 09:07:14 -07001460 * .extend(new Notification.Action.WearableExtender()
Griff Hazen61a9e862014-05-22 16:05:19 -07001461 * .setAvailableOffline(false))
Griff Hazen14f57992014-05-26 09:07:14 -07001462 * .build();</pre>
Griff Hazen61a9e862014-05-22 16:05:19 -07001463 */
1464 public static final class WearableExtender implements Extender {
1465 /** Notification action extra which contains wearable extensions */
1466 private static final String EXTRA_WEARABLE_EXTENSIONS = "android.wearable.EXTENSIONS";
1467
Pete Gastaf6781d2014-10-07 15:17:05 -04001468 // Keys within EXTRA_WEARABLE_EXTENSIONS for wearable options.
Griff Hazen61a9e862014-05-22 16:05:19 -07001469 private static final String KEY_FLAGS = "flags";
Pete Gastaf6781d2014-10-07 15:17:05 -04001470 private static final String KEY_IN_PROGRESS_LABEL = "inProgressLabel";
1471 private static final String KEY_CONFIRM_LABEL = "confirmLabel";
1472 private static final String KEY_CANCEL_LABEL = "cancelLabel";
Griff Hazen61a9e862014-05-22 16:05:19 -07001473
1474 // Flags bitwise-ored to mFlags
1475 private static final int FLAG_AVAILABLE_OFFLINE = 0x1;
Alex Hills9ab3a232016-04-05 14:54:56 -04001476 private static final int FLAG_HINT_LAUNCHES_ACTIVITY = 1 << 1;
Alex Hills9f087612016-06-07 09:08:59 -04001477 private static final int FLAG_HINT_DISPLAY_INLINE = 1 << 2;
Griff Hazen61a9e862014-05-22 16:05:19 -07001478
1479 // Default value for flags integer
1480 private static final int DEFAULT_FLAGS = FLAG_AVAILABLE_OFFLINE;
1481
1482 private int mFlags = DEFAULT_FLAGS;
1483
Pete Gastaf6781d2014-10-07 15:17:05 -04001484 private CharSequence mInProgressLabel;
1485 private CharSequence mConfirmLabel;
1486 private CharSequence mCancelLabel;
1487
Griff Hazen61a9e862014-05-22 16:05:19 -07001488 /**
1489 * Create a {@link android.app.Notification.Action.WearableExtender} with default
1490 * options.
1491 */
1492 public WearableExtender() {
1493 }
1494
1495 /**
1496 * Create a {@link android.app.Notification.Action.WearableExtender} by reading
1497 * wearable options present in an existing notification action.
1498 * @param action the notification action to inspect.
1499 */
1500 public WearableExtender(Action action) {
1501 Bundle wearableBundle = action.getExtras().getBundle(EXTRA_WEARABLE_EXTENSIONS);
1502 if (wearableBundle != null) {
1503 mFlags = wearableBundle.getInt(KEY_FLAGS, DEFAULT_FLAGS);
Pete Gastaf6781d2014-10-07 15:17:05 -04001504 mInProgressLabel = wearableBundle.getCharSequence(KEY_IN_PROGRESS_LABEL);
1505 mConfirmLabel = wearableBundle.getCharSequence(KEY_CONFIRM_LABEL);
1506 mCancelLabel = wearableBundle.getCharSequence(KEY_CANCEL_LABEL);
Griff Hazen61a9e862014-05-22 16:05:19 -07001507 }
1508 }
1509
1510 /**
1511 * Apply wearable extensions to a notification action that is being built. This is
1512 * typically called by the {@link android.app.Notification.Action.Builder#extend}
1513 * method of {@link android.app.Notification.Action.Builder}.
1514 */
1515 @Override
1516 public Action.Builder extend(Action.Builder builder) {
1517 Bundle wearableBundle = new Bundle();
1518
1519 if (mFlags != DEFAULT_FLAGS) {
1520 wearableBundle.putInt(KEY_FLAGS, mFlags);
1521 }
Pete Gastaf6781d2014-10-07 15:17:05 -04001522 if (mInProgressLabel != null) {
1523 wearableBundle.putCharSequence(KEY_IN_PROGRESS_LABEL, mInProgressLabel);
1524 }
1525 if (mConfirmLabel != null) {
1526 wearableBundle.putCharSequence(KEY_CONFIRM_LABEL, mConfirmLabel);
1527 }
1528 if (mCancelLabel != null) {
1529 wearableBundle.putCharSequence(KEY_CANCEL_LABEL, mCancelLabel);
1530 }
Griff Hazen61a9e862014-05-22 16:05:19 -07001531
1532 builder.getExtras().putBundle(EXTRA_WEARABLE_EXTENSIONS, wearableBundle);
1533 return builder;
1534 }
1535
1536 @Override
1537 public WearableExtender clone() {
1538 WearableExtender that = new WearableExtender();
1539 that.mFlags = this.mFlags;
Pete Gastaf6781d2014-10-07 15:17:05 -04001540 that.mInProgressLabel = this.mInProgressLabel;
1541 that.mConfirmLabel = this.mConfirmLabel;
1542 that.mCancelLabel = this.mCancelLabel;
Griff Hazen61a9e862014-05-22 16:05:19 -07001543 return that;
1544 }
1545
1546 /**
1547 * Set whether this action is available when the wearable device is not connected to
1548 * a companion device. The user can still trigger this action when the wearable device is
1549 * offline, but a visual hint will indicate that the action may not be available.
1550 * Defaults to true.
1551 */
1552 public WearableExtender setAvailableOffline(boolean availableOffline) {
1553 setFlag(FLAG_AVAILABLE_OFFLINE, availableOffline);
1554 return this;
1555 }
1556
1557 /**
1558 * Get whether this action is available when the wearable device is not connected to
1559 * a companion device. The user can still trigger this action when the wearable device is
1560 * offline, but a visual hint will indicate that the action may not be available.
1561 * Defaults to true.
1562 */
1563 public boolean isAvailableOffline() {
1564 return (mFlags & FLAG_AVAILABLE_OFFLINE) != 0;
1565 }
1566
1567 private void setFlag(int mask, boolean value) {
1568 if (value) {
1569 mFlags |= mask;
1570 } else {
1571 mFlags &= ~mask;
1572 }
1573 }
Pete Gastaf6781d2014-10-07 15:17:05 -04001574
1575 /**
1576 * Set a label to display while the wearable is preparing to automatically execute the
1577 * action. This is usually a 'ing' verb ending in ellipsis like "Sending..."
1578 *
1579 * @param label the label to display while the action is being prepared to execute
1580 * @return this object for method chaining
1581 */
1582 public WearableExtender setInProgressLabel(CharSequence label) {
1583 mInProgressLabel = label;
1584 return this;
1585 }
1586
1587 /**
1588 * Get the label to display while the wearable is preparing to automatically execute
1589 * the action. This is usually a 'ing' verb ending in ellipsis like "Sending..."
1590 *
1591 * @return the label to display while the action is being prepared to execute
1592 */
1593 public CharSequence getInProgressLabel() {
1594 return mInProgressLabel;
1595 }
1596
1597 /**
1598 * Set a label to display to confirm that the action should be executed.
1599 * This is usually an imperative verb like "Send".
1600 *
1601 * @param label the label to confirm the action should be executed
1602 * @return this object for method chaining
1603 */
1604 public WearableExtender setConfirmLabel(CharSequence label) {
1605 mConfirmLabel = label;
1606 return this;
1607 }
1608
1609 /**
1610 * Get the label to display to confirm that the action should be executed.
1611 * This is usually an imperative verb like "Send".
1612 *
1613 * @return the label to confirm the action should be executed
1614 */
1615 public CharSequence getConfirmLabel() {
1616 return mConfirmLabel;
1617 }
1618
1619 /**
1620 * Set a label to display to cancel the action.
1621 * This is usually an imperative verb, like "Cancel".
1622 *
1623 * @param label the label to display to cancel the action
1624 * @return this object for method chaining
1625 */
1626 public WearableExtender setCancelLabel(CharSequence label) {
1627 mCancelLabel = label;
1628 return this;
1629 }
1630
1631 /**
1632 * Get the label to display to cancel the action.
1633 * This is usually an imperative verb like "Cancel".
1634 *
1635 * @return the label to display to cancel the action
1636 */
1637 public CharSequence getCancelLabel() {
1638 return mCancelLabel;
1639 }
Alex Hills9ab3a232016-04-05 14:54:56 -04001640
1641 /**
1642 * Set a hint that this Action will launch an {@link Activity} directly, telling the
1643 * platform that it can generate the appropriate transitions.
1644 * @param hintLaunchesActivity {@code true} if the content intent will launch
1645 * an activity and transitions should be generated, false otherwise.
1646 * @return this object for method chaining
1647 */
Alex Hills4ec3ff42016-04-12 11:36:18 -04001648 public WearableExtender setHintLaunchesActivity(
Alex Hills9ab3a232016-04-05 14:54:56 -04001649 boolean hintLaunchesActivity) {
1650 setFlag(FLAG_HINT_LAUNCHES_ACTIVITY, hintLaunchesActivity);
1651 return this;
1652 }
1653
1654 /**
1655 * Get a hint that this Action will launch an {@link Activity} directly, telling the
1656 * platform that it can generate the appropriate transitions
1657 * @return {@code true} if the content intent will launch an activity and transitions
1658 * should be generated, false otherwise. The default value is {@code false} if this was
1659 * never set.
1660 */
Alex Hills4ec3ff42016-04-12 11:36:18 -04001661 public boolean getHintLaunchesActivity() {
Alex Hills9ab3a232016-04-05 14:54:56 -04001662 return (mFlags & FLAG_HINT_LAUNCHES_ACTIVITY) != 0;
1663 }
Alex Hills9f087612016-06-07 09:08:59 -04001664
1665 /**
1666 * Set a hint that this Action should be displayed inline.
1667 *
1668 * @param hintDisplayInline {@code true} if action should be displayed inline, false
1669 * otherwise
1670 * @return this object for method chaining
1671 */
1672 public WearableExtender setHintDisplayActionInline(
1673 boolean hintDisplayInline) {
1674 setFlag(FLAG_HINT_DISPLAY_INLINE, hintDisplayInline);
1675 return this;
1676 }
1677
1678 /**
1679 * Get a hint that this Action should be displayed inline.
1680 *
Dmitri Plotnikov9e9cfd92016-12-21 10:52:26 -08001681 * @return {@code true} if the Action should be displayed inline, {@code false}
Alex Hills9f087612016-06-07 09:08:59 -04001682 * otherwise. The default value is {@code false} if this was never set.
1683 */
1684 public boolean getHintDisplayActionInline() {
1685 return (mFlags & FLAG_HINT_DISPLAY_INLINE) != 0;
1686 }
Griff Hazen61a9e862014-05-22 16:05:19 -07001687 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001688 }
1689
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001690 /**
1691 * Array of all {@link Action} structures attached to this notification by
1692 * {@link Builder#addAction(int, CharSequence, PendingIntent)}. Mostly useful for instances of
1693 * {@link android.service.notification.NotificationListenerService} that provide an alternative
1694 * interface for invoking actions.
1695 */
Daniel Sandlerea2a3172013-02-20 22:24:20 -05001696 public Action[] actions;
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001697
1698 /**
Dan Sandler0bf2ed82013-12-21 23:33:41 -06001699 * Replacement version of this notification whose content will be shown
1700 * in an insecure context such as atop a secure keyguard. See {@link #visibility}
1701 * and {@link #VISIBILITY_PUBLIC}.
1702 */
1703 public Notification publicVersion;
1704
1705 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001706 * Constructs a Notification object with default values.
Joe Onorato46439ce2010-11-19 13:56:21 -08001707 * You might want to consider using {@link Builder} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001708 */
1709 public Notification()
1710 {
1711 this.when = System.currentTimeMillis();
Selim Cinekb85f36fd2016-04-20 18:46:36 -07001712 this.creationTime = System.currentTimeMillis();
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001713 this.priority = PRIORITY_DEFAULT;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001714 }
1715
1716 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001717 * @hide
1718 */
1719 public Notification(Context context, int icon, CharSequence tickerText, long when,
1720 CharSequence contentTitle, CharSequence contentText, Intent contentIntent)
1721 {
Chris Wren1ce4b6d2015-06-11 10:19:43 -04001722 new Builder(context)
1723 .setWhen(when)
1724 .setSmallIcon(icon)
1725 .setTicker(tickerText)
1726 .setContentTitle(contentTitle)
1727 .setContentText(contentText)
1728 .setContentIntent(PendingIntent.getActivity(context, 0, contentIntent, 0))
1729 .buildInto(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001730 }
1731
1732 /**
1733 * Constructs a Notification object with the information needed to
1734 * have a status bar icon without the standard expanded view.
1735 *
1736 * @param icon The resource id of the icon to put in the status bar.
1737 * @param tickerText The text that flows by in the status bar when the notification first
1738 * activates.
1739 * @param when The time to show in the time field. In the System.currentTimeMillis
1740 * timebase.
Joe Onorato46439ce2010-11-19 13:56:21 -08001741 *
1742 * @deprecated Use {@link Builder} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001743 */
Joe Onorato46439ce2010-11-19 13:56:21 -08001744 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001745 public Notification(int icon, CharSequence tickerText, long when)
1746 {
1747 this.icon = icon;
1748 this.tickerText = tickerText;
1749 this.when = when;
Selim Cinekb85f36fd2016-04-20 18:46:36 -07001750 this.creationTime = System.currentTimeMillis();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001751 }
1752
1753 /**
1754 * Unflatten the notification from a parcel.
1755 */
Svet Ganovddb94882016-06-23 19:55:24 -07001756 @SuppressWarnings("unchecked")
1757 public Notification(Parcel parcel) {
1758 // IMPORTANT: Add unmarshaling code in readFromParcel as the pending
1759 // intents in extras are always written as the last entry.
1760 readFromParcelImpl(parcel);
1761 // Must be read last!
Felipe Lemedd85da62016-06-28 11:29:54 -07001762 allPendingIntents = (ArraySet<PendingIntent>) parcel.readArraySet(null);
Svet Ganovddb94882016-06-23 19:55:24 -07001763 }
1764
1765 private void readFromParcelImpl(Parcel parcel)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001766 {
1767 int version = parcel.readInt();
1768
1769 when = parcel.readLong();
Selim Cinekb85f36fd2016-04-20 18:46:36 -07001770 creationTime = parcel.readLong();
Dan Sandler3936e7a2015-05-19 20:59:12 -04001771 if (parcel.readInt() != 0) {
1772 mSmallIcon = Icon.CREATOR.createFromParcel(parcel);
Dan Sandler4e787062015-06-17 15:09:48 -04001773 if (mSmallIcon.getType() == Icon.TYPE_RESOURCE) {
1774 icon = mSmallIcon.getResId();
1775 }
Dan Sandler3936e7a2015-05-19 20:59:12 -04001776 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001777 number = parcel.readInt();
1778 if (parcel.readInt() != 0) {
1779 contentIntent = PendingIntent.CREATOR.createFromParcel(parcel);
1780 }
1781 if (parcel.readInt() != 0) {
1782 deleteIntent = PendingIntent.CREATOR.createFromParcel(parcel);
1783 }
1784 if (parcel.readInt() != 0) {
1785 tickerText = TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(parcel);
1786 }
1787 if (parcel.readInt() != 0) {
Joe Onorato46439ce2010-11-19 13:56:21 -08001788 tickerView = RemoteViews.CREATOR.createFromParcel(parcel);
Joe Onoratoef1e7762010-09-17 18:38:38 -04001789 }
1790 if (parcel.readInt() != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001791 contentView = RemoteViews.CREATOR.createFromParcel(parcel);
1792 }
Joe Onorato561d3852010-11-20 18:09:34 -08001793 if (parcel.readInt() != 0) {
Dan Sandlerd63f9322015-05-06 15:18:49 -04001794 mLargeIcon = Icon.CREATOR.createFromParcel(parcel);
Joe Onorato561d3852010-11-20 18:09:34 -08001795 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001796 defaults = parcel.readInt();
1797 flags = parcel.readInt();
1798 if (parcel.readInt() != 0) {
1799 sound = Uri.CREATOR.createFromParcel(parcel);
1800 }
1801
1802 audioStreamType = parcel.readInt();
John Spurlockc0650f022014-07-19 13:22:39 -04001803 if (parcel.readInt() != 0) {
1804 audioAttributes = AudioAttributes.CREATOR.createFromParcel(parcel);
1805 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001806 vibrate = parcel.createLongArray();
1807 ledARGB = parcel.readInt();
1808 ledOnMS = parcel.readInt();
1809 ledOffMS = parcel.readInt();
1810 iconLevel = parcel.readInt();
Daniel Sandlere46cbd32010-06-17 10:35:26 -04001811
1812 if (parcel.readInt() != 0) {
1813 fullScreenIntent = PendingIntent.CREATOR.createFromParcel(parcel);
1814 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001815
1816 priority = parcel.readInt();
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001817
John Spurlockfd7f1e02014-03-18 16:41:57 -04001818 category = parcel.readString();
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001819
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001820 mGroupKey = parcel.readString();
1821
1822 mSortKey = parcel.readString();
1823
Jeff Sharkeya04c7a72016-03-18 12:20:36 -06001824 extras = Bundle.setDefusable(parcel.readBundle(), true); // may be null
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001825
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001826 actions = parcel.createTypedArray(Action.CREATOR); // may be null
1827
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001828 if (parcel.readInt() != 0) {
Daniel Sandlerf3b73432012-03-27 15:01:25 -04001829 bigContentView = RemoteViews.CREATOR.createFromParcel(parcel);
1830 }
Dan Sandler0bf2ed82013-12-21 23:33:41 -06001831
Chris Wren8fd39ec2014-02-27 17:43:26 -05001832 if (parcel.readInt() != 0) {
1833 headsUpContentView = RemoteViews.CREATOR.createFromParcel(parcel);
1834 }
1835
Dan Sandler0bf2ed82013-12-21 23:33:41 -06001836 visibility = parcel.readInt();
1837
1838 if (parcel.readInt() != 0) {
1839 publicVersion = Notification.CREATOR.createFromParcel(parcel);
1840 }
Dan Sandler26e81cf2014-05-06 10:01:27 -04001841
1842 color = parcel.readInt();
Julia Reynoldsb5e44b72016-08-16 15:00:25 -04001843
1844 if (parcel.readInt() != 0) {
1845 mChannelId = parcel.readString();
1846 }
Julia Reynolds2a128742016-11-28 14:29:25 -05001847 mTimeout = parcel.readLong();
Julia Reynolds13d898c2017-02-02 12:22:05 -05001848
1849 if (parcel.readInt() != 0) {
1850 mShortcutId = parcel.readString();
1851 }
1852
1853 mBadgeIcon = parcel.readInt();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001854 }
1855
Andy Stadler110988c2010-12-03 14:29:16 -08001856 @Override
Joe Onorato18e69df2010-05-17 22:26:12 -07001857 public Notification clone() {
1858 Notification that = new Notification();
Daniel Sandler1a497d32013-04-18 14:52:45 -04001859 cloneInto(that, true);
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001860 return that;
1861 }
Joe Onorato18e69df2010-05-17 22:26:12 -07001862
Daniel Sandler1a497d32013-04-18 14:52:45 -04001863 /**
1864 * Copy all (or if heavy is false, all except Bitmaps and RemoteViews) members
1865 * of this into that.
1866 * @hide
1867 */
1868 public void cloneInto(Notification that, boolean heavy) {
Joe Onorato18e69df2010-05-17 22:26:12 -07001869 that.when = this.when;
Selim Cinekb85f36fd2016-04-20 18:46:36 -07001870 that.creationTime = this.creationTime;
Dan Sandlerd63f9322015-05-06 15:18:49 -04001871 that.mSmallIcon = this.mSmallIcon;
Joe Onorato18e69df2010-05-17 22:26:12 -07001872 that.number = this.number;
1873
1874 // PendingIntents are global, so there's no reason (or way) to clone them.
1875 that.contentIntent = this.contentIntent;
1876 that.deleteIntent = this.deleteIntent;
Daniel Sandlere46cbd32010-06-17 10:35:26 -04001877 that.fullScreenIntent = this.fullScreenIntent;
Joe Onorato18e69df2010-05-17 22:26:12 -07001878
1879 if (this.tickerText != null) {
1880 that.tickerText = this.tickerText.toString();
1881 }
Daniel Sandler1a497d32013-04-18 14:52:45 -04001882 if (heavy && this.tickerView != null) {
Joe Onorato46439ce2010-11-19 13:56:21 -08001883 that.tickerView = this.tickerView.clone();
Joe Onoratoef1e7762010-09-17 18:38:38 -04001884 }
Daniel Sandler1a497d32013-04-18 14:52:45 -04001885 if (heavy && this.contentView != null) {
Joe Onorato18e69df2010-05-17 22:26:12 -07001886 that.contentView = this.contentView.clone();
1887 }
Dan Sandlerd63f9322015-05-06 15:18:49 -04001888 if (heavy && this.mLargeIcon != null) {
1889 that.mLargeIcon = this.mLargeIcon;
Joe Onorato561d3852010-11-20 18:09:34 -08001890 }
Jozef BABJAKa8b91832011-02-22 08:05:08 +01001891 that.iconLevel = this.iconLevel;
Joe Onorato18e69df2010-05-17 22:26:12 -07001892 that.sound = this.sound; // android.net.Uri is immutable
1893 that.audioStreamType = this.audioStreamType;
John Spurlockc0650f022014-07-19 13:22:39 -04001894 if (this.audioAttributes != null) {
1895 that.audioAttributes = new AudioAttributes.Builder(this.audioAttributes).build();
1896 }
Joe Onorato18e69df2010-05-17 22:26:12 -07001897
1898 final long[] vibrate = this.vibrate;
1899 if (vibrate != null) {
1900 final int N = vibrate.length;
1901 final long[] vib = that.vibrate = new long[N];
1902 System.arraycopy(vibrate, 0, vib, 0, N);
1903 }
1904
1905 that.ledARGB = this.ledARGB;
1906 that.ledOnMS = this.ledOnMS;
1907 that.ledOffMS = this.ledOffMS;
1908 that.defaults = this.defaults;
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001909
Joe Onorato18e69df2010-05-17 22:26:12 -07001910 that.flags = this.flags;
1911
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001912 that.priority = this.priority;
Joe Malin8d40d042012-11-05 11:36:40 -08001913
John Spurlockfd7f1e02014-03-18 16:41:57 -04001914 that.category = this.category;
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001915
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001916 that.mGroupKey = this.mGroupKey;
1917
1918 that.mSortKey = this.mSortKey;
1919
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001920 if (this.extras != null) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04001921 try {
1922 that.extras = new Bundle(this.extras);
1923 // will unparcel
1924 that.extras.size();
1925 } catch (BadParcelableException e) {
1926 Log.e(TAG, "could not unparcel extras from notification: " + this, e);
1927 that.extras = null;
1928 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001929 }
1930
Felipe Lemedd85da62016-06-28 11:29:54 -07001931 if (!ArrayUtils.isEmpty(allPendingIntents)) {
1932 that.allPendingIntents = new ArraySet<>(allPendingIntents);
Svet Ganovddb94882016-06-23 19:55:24 -07001933 }
1934
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001935 if (this.actions != null) {
1936 that.actions = new Action[this.actions.length];
1937 for(int i=0; i<this.actions.length; i++) {
liangweikang63b03b52017-03-16 19:22:15 +08001938 if ( this.actions[i] != null) {
1939 that.actions[i] = this.actions[i].clone();
1940 }
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001941 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001942 }
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001943
Daniel Sandler1a497d32013-04-18 14:52:45 -04001944 if (heavy && this.bigContentView != null) {
Daniel Sandlerf3b73432012-03-27 15:01:25 -04001945 that.bigContentView = this.bigContentView.clone();
1946 }
Daniel Sandler1a497d32013-04-18 14:52:45 -04001947
Chris Wren8fd39ec2014-02-27 17:43:26 -05001948 if (heavy && this.headsUpContentView != null) {
1949 that.headsUpContentView = this.headsUpContentView.clone();
1950 }
1951
Dan Sandler0bf2ed82013-12-21 23:33:41 -06001952 that.visibility = this.visibility;
1953
1954 if (this.publicVersion != null) {
1955 that.publicVersion = new Notification();
1956 this.publicVersion.cloneInto(that.publicVersion, heavy);
1957 }
1958
Dan Sandler26e81cf2014-05-06 10:01:27 -04001959 that.color = this.color;
1960
Julia Reynoldsb5e44b72016-08-16 15:00:25 -04001961 that.mChannelId = this.mChannelId;
Julia Reynolds2a128742016-11-28 14:29:25 -05001962 that.mTimeout = this.mTimeout;
Julia Reynoldsb5e44b72016-08-16 15:00:25 -04001963
Daniel Sandler1a497d32013-04-18 14:52:45 -04001964 if (!heavy) {
1965 that.lightenPayload(); // will clean out extras
1966 }
1967 }
1968
1969 /**
1970 * Removes heavyweight parts of the Notification object for archival or for sending to
1971 * listeners when the full contents are not necessary.
1972 * @hide
1973 */
1974 public final void lightenPayload() {
1975 tickerView = null;
1976 contentView = null;
1977 bigContentView = null;
Chris Wren8fd39ec2014-02-27 17:43:26 -05001978 headsUpContentView = null;
Dan Sandlerd63f9322015-05-06 15:18:49 -04001979 mLargeIcon = null;
Dan Sandler50128532015-12-08 15:42:41 -05001980 if (extras != null && !extras.isEmpty()) {
1981 final Set<String> keyset = extras.keySet();
1982 final int N = keyset.size();
1983 final String[] keys = keyset.toArray(new String[N]);
1984 for (int i=0; i<N; i++) {
1985 final String key = keys[i];
Dmitri Plotnikov22281362017-01-30 11:16:21 -08001986 if (TvExtender.EXTRA_TV_EXTENDER.equals(key)) {
1987 continue;
1988 }
Dan Sandler50128532015-12-08 15:42:41 -05001989 final Object obj = extras.get(key);
1990 if (obj != null &&
1991 ( obj instanceof Parcelable
1992 || obj instanceof Parcelable[]
1993 || obj instanceof SparseArray
1994 || obj instanceof ArrayList)) {
1995 extras.remove(key);
1996 }
1997 }
Daniel Sandler1a497d32013-04-18 14:52:45 -04001998 }
Joe Onorato18e69df2010-05-17 22:26:12 -07001999 }
2000
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04002001 /**
2002 * Make sure this CharSequence is safe to put into a bundle, which basically
2003 * means it had better not be some custom Parcelable implementation.
2004 * @hide
2005 */
2006 public static CharSequence safeCharSequence(CharSequence cs) {
Christoph Studer535ec612014-09-03 15:47:47 +02002007 if (cs == null) return cs;
2008 if (cs.length() > MAX_CHARSEQUENCE_LENGTH) {
2009 cs = cs.subSequence(0, MAX_CHARSEQUENCE_LENGTH);
2010 }
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04002011 if (cs instanceof Parcelable) {
2012 Log.e(TAG, "warning: " + cs.getClass().getCanonicalName()
2013 + " instance is a custom Parcelable and not allowed in Notification");
2014 return cs.toString();
2015 }
Selim Cinek60a54252016-02-26 17:03:25 -08002016 return removeTextSizeSpans(cs);
2017 }
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04002018
Selim Cinek60a54252016-02-26 17:03:25 -08002019 private static CharSequence removeTextSizeSpans(CharSequence charSequence) {
2020 if (charSequence instanceof Spanned) {
2021 Spanned ss = (Spanned) charSequence;
2022 Object[] spans = ss.getSpans(0, ss.length(), Object.class);
2023 SpannableStringBuilder builder = new SpannableStringBuilder(ss.toString());
2024 for (Object span : spans) {
2025 Object resultSpan = span;
Selim Cinek89991a22016-03-07 19:51:54 -08002026 if (resultSpan instanceof CharacterStyle) {
2027 resultSpan = ((CharacterStyle) span).getUnderlying();
2028 }
2029 if (resultSpan instanceof TextAppearanceSpan) {
2030 TextAppearanceSpan originalSpan = (TextAppearanceSpan) resultSpan;
Selim Cinek60a54252016-02-26 17:03:25 -08002031 resultSpan = new TextAppearanceSpan(
2032 originalSpan.getFamily(),
2033 originalSpan.getTextStyle(),
2034 -1,
2035 originalSpan.getTextColor(),
2036 originalSpan.getLinkTextColor());
Selim Cinek89991a22016-03-07 19:51:54 -08002037 } else if (resultSpan instanceof RelativeSizeSpan
2038 || resultSpan instanceof AbsoluteSizeSpan) {
Selim Cinek60a54252016-02-26 17:03:25 -08002039 continue;
Selim Cinek89991a22016-03-07 19:51:54 -08002040 } else {
2041 resultSpan = span;
Selim Cinek60a54252016-02-26 17:03:25 -08002042 }
2043 builder.setSpan(resultSpan, ss.getSpanStart(span), ss.getSpanEnd(span),
2044 ss.getSpanFlags(span));
2045 }
2046 return builder;
2047 }
2048 return charSequence;
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04002049 }
2050
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002051 public int describeContents() {
2052 return 0;
2053 }
2054
2055 /**
Dan Sandler4e787062015-06-17 15:09:48 -04002056 * Flatten this notification into a parcel.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002057 */
Svet Ganovddb94882016-06-23 19:55:24 -07002058 public void writeToParcel(Parcel parcel, int flags) {
2059 // We need to mark all pending intents getting into the notification
2060 // system as being put there to later allow the notification ranker
2061 // to launch them and by doing so add the app to the battery saver white
2062 // list for a short period of time. The problem is that the system
2063 // cannot look into the extras as there may be parcelables there that
2064 // the platform does not know how to handle. To go around that we have
2065 // an explicit list of the pending intents in the extras bundle.
Felipe Lemedd85da62016-06-28 11:29:54 -07002066 final boolean collectPendingIntents = (allPendingIntents == null);
Svet Ganovddb94882016-06-23 19:55:24 -07002067 if (collectPendingIntents) {
2068 PendingIntent.setOnMarshaledListener(
2069 (PendingIntent intent, Parcel out, int outFlags) -> {
2070 if (parcel == out) {
Felipe Lemedd85da62016-06-28 11:29:54 -07002071 if (allPendingIntents == null) {
2072 allPendingIntents = new ArraySet<>();
Svet Ganovddb94882016-06-23 19:55:24 -07002073 }
Felipe Lemedd85da62016-06-28 11:29:54 -07002074 allPendingIntents.add(intent);
Svet Ganovddb94882016-06-23 19:55:24 -07002075 }
2076 });
2077 }
2078 try {
2079 // IMPORTANT: Add marshaling code in writeToParcelImpl as we
Julia Reynolds13d898c2017-02-02 12:22:05 -05002080 // want to intercept all pending events written to the parcel.
Svet Ganovddb94882016-06-23 19:55:24 -07002081 writeToParcelImpl(parcel, flags);
2082 // Must be written last!
Felipe Lemedd85da62016-06-28 11:29:54 -07002083 parcel.writeArraySet(allPendingIntents);
Svet Ganovddb94882016-06-23 19:55:24 -07002084 } finally {
2085 if (collectPendingIntents) {
2086 PendingIntent.setOnMarshaledListener(null);
2087 }
2088 }
2089 }
2090
2091 private void writeToParcelImpl(Parcel parcel, int flags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002092 parcel.writeInt(1);
2093
2094 parcel.writeLong(when);
Selim Cinekb85f36fd2016-04-20 18:46:36 -07002095 parcel.writeLong(creationTime);
Dan Sandler4e787062015-06-17 15:09:48 -04002096 if (mSmallIcon == null && icon != 0) {
2097 // you snuck an icon in here without using the builder; let's try to keep it
2098 mSmallIcon = Icon.createWithResource("", icon);
2099 }
Dan Sandler3936e7a2015-05-19 20:59:12 -04002100 if (mSmallIcon != null) {
2101 parcel.writeInt(1);
2102 mSmallIcon.writeToParcel(parcel, 0);
2103 } else {
2104 parcel.writeInt(0);
2105 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002106 parcel.writeInt(number);
2107 if (contentIntent != null) {
2108 parcel.writeInt(1);
2109 contentIntent.writeToParcel(parcel, 0);
2110 } else {
2111 parcel.writeInt(0);
2112 }
2113 if (deleteIntent != null) {
2114 parcel.writeInt(1);
2115 deleteIntent.writeToParcel(parcel, 0);
2116 } else {
2117 parcel.writeInt(0);
2118 }
2119 if (tickerText != null) {
2120 parcel.writeInt(1);
2121 TextUtils.writeToParcel(tickerText, parcel, flags);
2122 } else {
2123 parcel.writeInt(0);
2124 }
Joe Onorato46439ce2010-11-19 13:56:21 -08002125 if (tickerView != null) {
Joe Onoratoef1e7762010-09-17 18:38:38 -04002126 parcel.writeInt(1);
Joe Onorato46439ce2010-11-19 13:56:21 -08002127 tickerView.writeToParcel(parcel, 0);
Joe Onoratoef1e7762010-09-17 18:38:38 -04002128 } else {
2129 parcel.writeInt(0);
2130 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002131 if (contentView != null) {
2132 parcel.writeInt(1);
2133 contentView.writeToParcel(parcel, 0);
2134 } else {
2135 parcel.writeInt(0);
2136 }
Selim Cinek279fa862016-06-14 10:57:25 -07002137 if (mLargeIcon == null && largeIcon != null) {
2138 // you snuck an icon in here without using the builder; let's try to keep it
2139 mLargeIcon = Icon.createWithBitmap(largeIcon);
2140 }
Dan Sandlerd63f9322015-05-06 15:18:49 -04002141 if (mLargeIcon != null) {
Joe Onorato561d3852010-11-20 18:09:34 -08002142 parcel.writeInt(1);
Dan Sandlerd63f9322015-05-06 15:18:49 -04002143 mLargeIcon.writeToParcel(parcel, 0);
Joe Onorato561d3852010-11-20 18:09:34 -08002144 } else {
2145 parcel.writeInt(0);
2146 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002147
2148 parcel.writeInt(defaults);
2149 parcel.writeInt(this.flags);
2150
2151 if (sound != null) {
2152 parcel.writeInt(1);
2153 sound.writeToParcel(parcel, 0);
2154 } else {
2155 parcel.writeInt(0);
2156 }
2157 parcel.writeInt(audioStreamType);
John Spurlockc0650f022014-07-19 13:22:39 -04002158
2159 if (audioAttributes != null) {
2160 parcel.writeInt(1);
2161 audioAttributes.writeToParcel(parcel, 0);
2162 } else {
2163 parcel.writeInt(0);
2164 }
2165
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002166 parcel.writeLongArray(vibrate);
2167 parcel.writeInt(ledARGB);
2168 parcel.writeInt(ledOnMS);
2169 parcel.writeInt(ledOffMS);
2170 parcel.writeInt(iconLevel);
Daniel Sandlere46cbd32010-06-17 10:35:26 -04002171
2172 if (fullScreenIntent != null) {
2173 parcel.writeInt(1);
2174 fullScreenIntent.writeToParcel(parcel, 0);
2175 } else {
2176 parcel.writeInt(0);
2177 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002178
2179 parcel.writeInt(priority);
Joe Malin8d40d042012-11-05 11:36:40 -08002180
John Spurlockfd7f1e02014-03-18 16:41:57 -04002181 parcel.writeString(category);
Joe Malin8d40d042012-11-05 11:36:40 -08002182
Griff Hazen5cadc3b2014-05-20 09:55:39 -07002183 parcel.writeString(mGroupKey);
2184
2185 parcel.writeString(mSortKey);
2186
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002187 parcel.writeBundle(extras); // null ok
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002188
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002189 parcel.writeTypedArray(actions, 0); // null ok
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002190
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002191 if (bigContentView != null) {
2192 parcel.writeInt(1);
2193 bigContentView.writeToParcel(parcel, 0);
2194 } else {
2195 parcel.writeInt(0);
2196 }
Dan Sandler0bf2ed82013-12-21 23:33:41 -06002197
Chris Wren8fd39ec2014-02-27 17:43:26 -05002198 if (headsUpContentView != null) {
2199 parcel.writeInt(1);
2200 headsUpContentView.writeToParcel(parcel, 0);
2201 } else {
2202 parcel.writeInt(0);
2203 }
2204
Dan Sandler0bf2ed82013-12-21 23:33:41 -06002205 parcel.writeInt(visibility);
2206
2207 if (publicVersion != null) {
2208 parcel.writeInt(1);
2209 publicVersion.writeToParcel(parcel, 0);
2210 } else {
2211 parcel.writeInt(0);
2212 }
Dan Sandler26e81cf2014-05-06 10:01:27 -04002213
2214 parcel.writeInt(color);
Julia Reynoldsb5e44b72016-08-16 15:00:25 -04002215
2216 if (mChannelId != null) {
2217 parcel.writeInt(1);
2218 parcel.writeString(mChannelId);
2219 } else {
2220 parcel.writeInt(0);
2221 }
Julia Reynolds2a128742016-11-28 14:29:25 -05002222 parcel.writeLong(mTimeout);
Julia Reynolds13d898c2017-02-02 12:22:05 -05002223
2224 if (mShortcutId != null) {
2225 parcel.writeInt(1);
2226 parcel.writeString(mShortcutId);
2227 } else {
2228 parcel.writeInt(0);
2229 }
2230
2231 parcel.writeInt(mBadgeIcon);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002232 }
2233
2234 /**
2235 * Parcelable.Creator that instantiates Notification objects
2236 */
2237 public static final Parcelable.Creator<Notification> CREATOR
2238 = new Parcelable.Creator<Notification>()
2239 {
2240 public Notification createFromParcel(Parcel parcel)
2241 {
2242 return new Notification(parcel);
2243 }
2244
2245 public Notification[] newArray(int size)
2246 {
2247 return new Notification[size];
2248 }
2249 };
2250
2251 /**
2252 * Sets the {@link #contentView} field to be a view with the standard "Latest Event"
2253 * layout.
2254 *
2255 * <p>Uses the {@link #icon} and {@link #when} fields to set the icon and time fields
2256 * in the view.</p>
2257 * @param context The context for your application / activity.
2258 * @param contentTitle The title that goes in the expanded entry.
2259 * @param contentText The text that goes in the expanded entry.
2260 * @param contentIntent The intent to launch when the user clicks the expanded notification.
2261 * If this is an activity, it must include the
2262 * {@link android.content.Intent#FLAG_ACTIVITY_NEW_TASK} flag, which requires
Scott Main7aee61f2011-02-08 11:25:01 -08002263 * that you take care of task management as described in the
2264 * <a href="{@docRoot}guide/topics/fundamentals/tasks-and-back-stack.html">Tasks and Back
2265 * Stack</a> document.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002266 *
Joe Onorato46439ce2010-11-19 13:56:21 -08002267 * @deprecated Use {@link Builder} instead.
Chris Wrena05db382015-06-24 15:18:34 -04002268 * @removed
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002269 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002270 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002271 public void setLatestEventInfo(Context context,
2272 CharSequence contentTitle, CharSequence contentText, PendingIntent contentIntent) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002273 if (context.getApplicationInfo().targetSdkVersion > Build.VERSION_CODES.LOLLIPOP_MR1){
2274 Log.e(TAG, "setLatestEventInfo() is deprecated and you should feel deprecated.",
2275 new Throwable());
2276 }
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002277
Selim Cinek4ac6f602016-06-13 15:47:03 -07002278 if (context.getApplicationInfo().targetSdkVersion < Build.VERSION_CODES.N) {
2279 extras.putBoolean(EXTRA_SHOW_WHEN, true);
2280 }
2281
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002282 // ensure that any information already set directly is preserved
2283 final Notification.Builder builder = new Notification.Builder(context, this);
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002284
2285 // now apply the latestEventInfo fields
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002286 if (contentTitle != null) {
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002287 builder.setContentTitle(contentTitle);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002288 }
2289 if (contentText != null) {
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002290 builder.setContentText(contentText);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002291 }
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002292 builder.setContentIntent(contentIntent);
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002293
2294 builder.build(); // callers expect this notification to be ready to use
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002295 }
2296
Julia Reynoldsda303542015-11-23 14:00:20 -05002297 /**
2298 * @hide
2299 */
2300 public static void addFieldsFromContext(Context context, Notification notification) {
Julia Reynoldse071abd2017-03-22 10:52:11 -04002301 addFieldsFromContext(context.getApplicationInfo(), notification);
Jeff Sharkey012bc7b2016-04-11 16:30:27 -06002302 }
2303
2304 /**
2305 * @hide
2306 */
Julia Reynoldse071abd2017-03-22 10:52:11 -04002307 public static void addFieldsFromContext(ApplicationInfo ai, Notification notification) {
Jeff Sharkey012bc7b2016-04-11 16:30:27 -06002308 notification.extras.putParcelable(EXTRA_BUILDER_APPLICATION_INFO, ai);
Julia Reynoldsda303542015-11-23 14:00:20 -05002309 }
2310
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002311 @Override
2312 public String toString() {
2313 StringBuilder sb = new StringBuilder();
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002314 sb.append("Notification(pri=");
2315 sb.append(priority);
2316 sb.append(" contentView=");
Joe Onoratoc9596d62011-01-12 17:03:11 -08002317 if (contentView != null) {
2318 sb.append(contentView.getPackage());
2319 sb.append("/0x");
2320 sb.append(Integer.toHexString(contentView.getLayoutId()));
2321 } else {
2322 sb.append("null");
2323 }
2324 sb.append(" vibrate=");
Daniel Sandler6738eee2012-11-16 12:03:32 -05002325 if ((this.defaults & DEFAULT_VIBRATE) != 0) {
2326 sb.append("default");
2327 } else if (this.vibrate != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002328 int N = this.vibrate.length-1;
2329 sb.append("[");
2330 for (int i=0; i<N; i++) {
2331 sb.append(this.vibrate[i]);
2332 sb.append(',');
2333 }
Simon Schoar8cf97d92009-06-10 22:08:37 +02002334 if (N != -1) {
2335 sb.append(this.vibrate[N]);
2336 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002337 sb.append("]");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002338 } else {
2339 sb.append("null");
2340 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002341 sb.append(" sound=");
Daniel Sandler6738eee2012-11-16 12:03:32 -05002342 if ((this.defaults & DEFAULT_SOUND) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002343 sb.append("default");
Daniel Sandler6738eee2012-11-16 12:03:32 -05002344 } else if (this.sound != null) {
2345 sb.append(this.sound.toString());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002346 } else {
2347 sb.append("null");
2348 }
Chris Wren365b6d32015-07-16 10:39:26 -04002349 if (this.tickerText != null) {
2350 sb.append(" tick");
2351 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002352 sb.append(" defaults=0x");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002353 sb.append(Integer.toHexString(this.defaults));
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002354 sb.append(" flags=0x");
Daniel Sandlere46cbd32010-06-17 10:35:26 -04002355 sb.append(Integer.toHexString(this.flags));
Dan Sandler26e81cf2014-05-06 10:01:27 -04002356 sb.append(String.format(" color=0x%08x", this.color));
Griff Hazen5cadc3b2014-05-20 09:55:39 -07002357 if (this.category != null) {
2358 sb.append(" category=");
2359 sb.append(this.category);
2360 }
2361 if (this.mGroupKey != null) {
2362 sb.append(" groupKey=");
2363 sb.append(this.mGroupKey);
2364 }
2365 if (this.mSortKey != null) {
2366 sb.append(" sortKey=");
2367 sb.append(this.mSortKey);
2368 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002369 if (actions != null) {
Dan Sandler1b718782014-07-18 12:43:45 -04002370 sb.append(" actions=");
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002371 sb.append(actions.length);
Dan Sandler1b718782014-07-18 12:43:45 -04002372 }
2373 sb.append(" vis=");
2374 sb.append(visibilityToString(this.visibility));
2375 if (this.publicVersion != null) {
2376 sb.append(" publicVersion=");
2377 sb.append(publicVersion.toString());
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002378 }
2379 sb.append(")");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002380 return sb.toString();
2381 }
Joe Onorato46439ce2010-11-19 13:56:21 -08002382
Dan Sandler1b718782014-07-18 12:43:45 -04002383 /**
2384 * {@hide}
2385 */
2386 public static String visibilityToString(int vis) {
2387 switch (vis) {
2388 case VISIBILITY_PRIVATE:
2389 return "PRIVATE";
2390 case VISIBILITY_PUBLIC:
2391 return "PUBLIC";
2392 case VISIBILITY_SECRET:
2393 return "SECRET";
2394 default:
2395 return "UNKNOWN(" + String.valueOf(vis) + ")";
2396 }
2397 }
2398
Joe Onoratocb109a02011-01-18 17:57:41 -08002399 /**
John Spurlock1d881a12015-03-18 19:21:54 -04002400 * {@hide}
2401 */
2402 public static String priorityToString(@Priority int pri) {
2403 switch (pri) {
2404 case PRIORITY_MIN:
2405 return "MIN";
2406 case PRIORITY_LOW:
2407 return "LOW";
2408 case PRIORITY_DEFAULT:
2409 return "DEFAULT";
2410 case PRIORITY_HIGH:
2411 return "HIGH";
2412 case PRIORITY_MAX:
2413 return "MAX";
2414 default:
2415 return "UNKNOWN(" + String.valueOf(pri) + ")";
2416 }
2417 }
2418
2419 /**
Julia Reynoldsb5e44b72016-08-16 15:00:25 -04002420 * Returns the id of the channel this notification posts to.
2421 */
Julia Reynolds37856052016-11-11 09:20:07 -05002422 public String getChannel() {
Julia Reynoldsb5e44b72016-08-16 15:00:25 -04002423 return mChannelId;
2424 }
2425
2426 /**
Julia Reynolds13d898c2017-02-02 12:22:05 -05002427 * Returns the time at which this notification should be canceled by the system, if it's not
2428 * canceled already.
Julia Reynolds2a128742016-11-28 14:29:25 -05002429 */
2430 public long getTimeout() {
2431 return mTimeout;
2432 }
2433
2434 /**
Julia Reynoldse071abd2017-03-22 10:52:11 -04002435 * @removed
Julia Reynolds13d898c2017-02-02 12:22:05 -05002436 * Returns what icon should be shown for this notification if it is being displayed in a
2437 * Launcher that supports badging. Will be one of {@link #BADGE_ICON_NONE},
2438 * {@link #BADGE_ICON_SMALL}, or {@link #BADGE_ICON_LARGE}.
2439 */
Julia Reynoldse071abd2017-03-22 10:52:11 -04002440 @Deprecated
Julia Reynolds13d898c2017-02-02 12:22:05 -05002441 public int getBadgeIcon() {
2442 return mBadgeIcon;
2443 }
2444
2445 /**
Julia Reynoldse071abd2017-03-22 10:52:11 -04002446 * Returns what icon should be shown for this notification if it is being displayed in a
2447 * Launcher that supports badging. Will be one of {@link #BADGE_ICON_NONE},
2448 * {@link #BADGE_ICON_SMALL}, or {@link #BADGE_ICON_LARGE}.
2449 */
2450 public int getBadgeIconType() {
2451 return mBadgeIcon;
2452 }
2453
2454 /**
Julia Reynolds13d898c2017-02-02 12:22:05 -05002455 * Returns the {@link ShortcutInfo#getId() id} that this notification supersedes, if any.
2456 */
2457 public String getShortcutId() {
2458 return mShortcutId;
2459 }
2460
2461 /**
Dan Sandlerd63f9322015-05-06 15:18:49 -04002462 * The small icon representing this notification in the status bar and content view.
2463 *
2464 * @return the small icon representing this notification.
2465 *
2466 * @see Builder#getSmallIcon()
2467 * @see Builder#setSmallIcon(Icon)
2468 */
2469 public Icon getSmallIcon() {
2470 return mSmallIcon;
2471 }
2472
2473 /**
2474 * Used when notifying to clean up legacy small icons.
2475 * @hide
2476 */
2477 public void setSmallIcon(Icon icon) {
2478 mSmallIcon = icon;
2479 }
2480
2481 /**
2482 * The large icon shown in this notification's content view.
2483 * @see Builder#getLargeIcon()
2484 * @see Builder#setLargeIcon(Icon)
2485 */
2486 public Icon getLargeIcon() {
2487 return mLargeIcon;
2488 }
2489
2490 /**
Christoph Studer4600f9b2014-07-22 22:44:43 +02002491 * @hide
2492 */
Christoph Studerc8db24b2014-07-25 17:50:30 +02002493 public boolean isGroupSummary() {
2494 return mGroupKey != null && (flags & FLAG_GROUP_SUMMARY) != 0;
2495 }
2496
2497 /**
2498 * @hide
2499 */
2500 public boolean isGroupChild() {
2501 return mGroupKey != null && (flags & FLAG_GROUP_SUMMARY) == 0;
2502 }
2503
2504 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002505 * Builder class for {@link Notification} objects.
Joe Malin8d40d042012-11-05 11:36:40 -08002506 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002507 * Provides a convenient way to set the various fields of a {@link Notification} and generate
Scott Main183bf112012-08-13 19:12:13 -07002508 * content views using the platform's notification layout template. If your app supports
2509 * versions of Android as old as API level 4, you can instead use
2510 * {@link android.support.v4.app.NotificationCompat.Builder NotificationCompat.Builder},
2511 * available in the <a href="{@docRoot}tools/extras/support-library.html">Android Support
2512 * library</a>.
Joe Malin8d40d042012-11-05 11:36:40 -08002513 *
Scott Main183bf112012-08-13 19:12:13 -07002514 * <p>Example:
Joe Malin8d40d042012-11-05 11:36:40 -08002515 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002516 * <pre class="prettyprint">
Scott Main183bf112012-08-13 19:12:13 -07002517 * Notification noti = new Notification.Builder(mContext)
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002518 * .setContentTitle(&quot;New mail from &quot; + sender.toString())
2519 * .setContentText(subject)
2520 * .setSmallIcon(R.drawable.new_mail)
2521 * .setLargeIcon(aBitmap)
Chris Wrenfbd96ba2012-05-01 12:03:58 -04002522 * .build();
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002523 * </pre>
Joe Onoratocb109a02011-01-18 17:57:41 -08002524 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002525 public static class Builder {
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05002526 /**
2527 * @hide
2528 */
2529 public static final String EXTRA_REBUILD_CONTENT_VIEW_ACTION_COUNT =
2530 "android.rebuild.contentViewActionCount";
2531 /**
2532 * @hide
2533 */
2534 public static final String EXTRA_REBUILD_BIG_CONTENT_VIEW_ACTION_COUNT
2535 = "android.rebuild.bigViewActionCount";
2536 /**
2537 * @hide
2538 */
2539 public static final String EXTRA_REBUILD_HEADS_UP_CONTENT_VIEW_ACTION_COUNT
2540 = "android.rebuild.hudViewActionCount";
2541
Daniel Sandler602ad1c2012-06-12 16:06:27 -04002542 private static final int MAX_ACTION_BUTTONS = 3;
Daniel Sandler8680bf82012-05-15 16:52:52 -04002543
Selim Cinek6743c0b2017-01-18 18:24:01 -08002544 private static final boolean USE_ONLY_TITLE_IN_LOW_PRIORITY_SUMMARY =
2545 SystemProperties.getBoolean("notifications.only_title", true);
2546
Joe Onorato46439ce2010-11-19 13:56:21 -08002547 private Context mContext;
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002548 private Notification mN;
2549 private Bundle mUserExtras = new Bundle();
Chris Wrenfbd96ba2012-05-01 12:03:58 -04002550 private Style mStyle;
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002551 private ArrayList<Action> mActions = new ArrayList<Action>(MAX_ACTION_BUTTONS);
2552 private ArrayList<String> mPersonList = new ArrayList<String>();
2553 private NotificationColorUtil mColorUtil;
Selim Cinek7b9605b2017-01-19 17:36:00 -08002554 private boolean mIsLegacy;
2555 private boolean mIsLegacyInitialized;
Christoph Studer7ac80e62014-08-04 16:01:57 +02002556
2557 /**
Adrian Roos4ff3b122016-02-01 12:26:13 -08002558 * Caches a contrast-enhanced version of {@link #mCachedContrastColorIsFor}.
2559 */
2560 private int mCachedContrastColor = COLOR_INVALID;
2561 private int mCachedContrastColorIsFor = COLOR_INVALID;
Adrian Roos487374f2017-01-11 15:48:14 -08002562 /**
2563 * Caches a ambient version of {@link #mCachedContrastColorIsFor}.
2564 */
2565 private int mCachedAmbientColor = COLOR_INVALID;
2566 private int mCachedAmbientColorIsFor = COLOR_INVALID;
Adrian Roos4ff3b122016-02-01 12:26:13 -08002567
2568 /**
Adrian Roos70d7aa32017-01-11 15:39:06 -08002569 * Caches an instance of StandardTemplateParams. Note that this may have been used before,
2570 * so make sure to call {@link StandardTemplateParams#reset()} before using it.
2571 */
2572 StandardTemplateParams mParams = new StandardTemplateParams();
Selim Cinek7b9605b2017-01-19 17:36:00 -08002573 private int mTextColorsAreForBackground = COLOR_INVALID;
2574 private int mPrimaryTextColor = COLOR_INVALID;
2575 private int mSecondaryTextColor = COLOR_INVALID;
2576 private int mActionBarColor = COLOR_INVALID;
Adrian Roos70d7aa32017-01-11 15:39:06 -08002577
2578 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002579 * Constructs a new Builder with the defaults:
Joe Onoratocb109a02011-01-18 17:57:41 -08002580 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002581
2582 * <table>
2583 * <tr><th align=right>priority</th>
2584 * <td>{@link #PRIORITY_DEFAULT}</td></tr>
2585 * <tr><th align=right>when</th>
2586 * <td>now ({@link System#currentTimeMillis()})</td></tr>
2587 * <tr><th align=right>audio stream</th>
2588 * <td>{@link #STREAM_DEFAULT}</td></tr>
2589 * </table>
Joe Onoratocb109a02011-01-18 17:57:41 -08002590 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002591
2592 * @param context
2593 * A {@link Context} that will be used by the Builder to construct the
2594 * RemoteViews. The Context will not be held past the lifetime of this Builder
2595 * object.
Geoffrey Pitsch5caa2762017-01-12 09:35:54 -05002596 * @param channelId
2597 * The constructed Notification will be posted on this
2598 * {@link NotificationChannel}. To use a NotificationChannel, it must first be
2599 * created using {@link NotificationManager#createNotificationChannel}.
Joe Onoratocb109a02011-01-18 17:57:41 -08002600 */
Geoffrey Pitsch5caa2762017-01-12 09:35:54 -05002601 public Builder(Context context, String channelId) {
2602 this(context, (Notification) null);
2603 mN.mChannelId = channelId;
2604 }
2605
2606 /**
2607 * @deprecated use {@link Notification.Builder#Notification.Builder(Context, String)}
2608 * instead. All posted Notifications must specify a NotificationChannel Id.
2609 */
2610 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -08002611 public Builder(Context context) {
Geoffrey Pitsch5caa2762017-01-12 09:35:54 -05002612 this(context, (Notification) null);
Joe Onorato46439ce2010-11-19 13:56:21 -08002613 }
2614
Joe Onoratocb109a02011-01-18 17:57:41 -08002615 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002616 * @hide
Christoph Studer4600f9b2014-07-22 22:44:43 +02002617 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002618 public Builder(Context context, Notification toAdopt) {
2619 mContext = context;
Christoph Studer4600f9b2014-07-22 22:44:43 +02002620
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002621 if (toAdopt == null) {
2622 mN = new Notification();
Selim Cinek0ff1ce602016-04-05 18:27:16 -07002623 if (context.getApplicationInfo().targetSdkVersion < Build.VERSION_CODES.N) {
2624 mN.extras.putBoolean(EXTRA_SHOW_WHEN, true);
2625 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002626 mN.priority = PRIORITY_DEFAULT;
2627 mN.visibility = VISIBILITY_PRIVATE;
2628 } else {
2629 mN = toAdopt;
2630 if (mN.actions != null) {
2631 Collections.addAll(mActions, mN.actions);
Christoph Studer4600f9b2014-07-22 22:44:43 +02002632 }
2633
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002634 if (mN.extras.containsKey(EXTRA_PEOPLE)) {
2635 Collections.addAll(mPersonList, mN.extras.getStringArray(EXTRA_PEOPLE));
2636 }
2637
Selim Cinek4ac6f602016-06-13 15:47:03 -07002638 if (mN.getSmallIcon() == null && mN.icon != 0) {
2639 setSmallIcon(mN.icon);
2640 }
2641
2642 if (mN.getLargeIcon() == null && mN.largeIcon != null) {
2643 setLargeIcon(mN.largeIcon);
2644 }
2645
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002646 String templateClass = mN.extras.getString(EXTRA_TEMPLATE);
2647 if (!TextUtils.isEmpty(templateClass)) {
2648 final Class<? extends Style> styleClass
2649 = getNotificationStyleClass(templateClass);
2650 if (styleClass == null) {
2651 Log.d(TAG, "Unknown style class: " + templateClass);
2652 } else {
2653 try {
Adrian Roosc1a80b02016-04-05 14:54:55 -07002654 final Constructor<? extends Style> ctor =
2655 styleClass.getDeclaredConstructor();
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002656 ctor.setAccessible(true);
2657 final Style style = ctor.newInstance();
2658 style.restoreFromExtras(mN.extras);
2659
2660 if (style != null) {
2661 setStyle(style);
2662 }
2663 } catch (Throwable t) {
2664 Log.e(TAG, "Could not create Style", t);
2665 }
2666 }
2667 }
2668
2669 }
2670 }
2671
2672 private NotificationColorUtil getColorUtil() {
Selim Cinek99104832017-01-25 14:47:33 -08002673 if (mColorUtil == null) {
2674 mColorUtil = NotificationColorUtil.getInstance(mContext);
Christoph Studer4600f9b2014-07-22 22:44:43 +02002675 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002676 return mColorUtil;
Christoph Studer4600f9b2014-07-22 22:44:43 +02002677 }
2678
2679 /**
Julia Reynolds13d898c2017-02-02 12:22:05 -05002680 * If this notification is duplicative of a Launcher shortcut, sets the
2681 * {@link ShortcutInfo#getId() id} of the shortcut, in case the Launcher wants to hide
2682 * the shortcut.
2683 *
2684 * This field will be ignored by Launchers that don't support badging or
2685 * {@link android.content.pm.ShortcutManager shortcuts}.
2686 *
2687 * @param shortcutId the {@link ShortcutInfo#getId() id} of the shortcut this notification
2688 * supersedes
2689 */
2690 public Builder setShortcutId(String shortcutId) {
2691 mN.mShortcutId = shortcutId;
2692 return this;
2693 }
2694
2695 /**
Julia Reynoldse071abd2017-03-22 10:52:11 -04002696 * @removed
Julia Reynolds13d898c2017-02-02 12:22:05 -05002697 * Sets which icon to display as a badge for this notification.
2698 *
2699 * Must be one of {@link #BADGE_ICON_NONE}, {@link #BADGE_ICON_SMALL},
2700 * {@link #BADGE_ICON_LARGE}.
2701 *
2702 * Note: This value might be ignored, for launchers that don't support badge icons.
2703 */
Julia Reynoldse071abd2017-03-22 10:52:11 -04002704 @Deprecated
Julia Reynolds13d898c2017-02-02 12:22:05 -05002705 public Builder chooseBadgeIcon(int icon) {
2706 mN.mBadgeIcon = icon;
2707 return this;
2708 }
2709
2710 /**
Julia Reynoldse071abd2017-03-22 10:52:11 -04002711 * Sets which icon to display as a badge for this notification.
2712 *
2713 * Must be one of {@link #BADGE_ICON_NONE}, {@link #BADGE_ICON_SMALL},
2714 * {@link #BADGE_ICON_LARGE}.
2715 *
2716 * Note: This value might be ignored, for launchers that don't support badge icons.
2717 */
Julia Reynolds612beb22017-03-30 10:48:30 -04002718 public Builder setBadgeIconType(int icon) {
Julia Reynoldse071abd2017-03-22 10:52:11 -04002719 mN.mBadgeIcon = icon;
2720 return this;
2721 }
2722
2723 /**
Julia Reynoldsb5e44b72016-08-16 15:00:25 -04002724 * Specifies the channel the notification should be delivered on.
2725 */
2726 public Builder setChannel(String channelId) {
2727 mN.mChannelId = channelId;
2728 return this;
2729 }
2730
2731 /**
Julia Reynolds50989772017-02-23 14:32:16 -05002732 * Specifies a duration in milliseconds after which this notification should be canceled,
2733 * if it is not already canceled.
Julia Reynolds2a128742016-11-28 14:29:25 -05002734 */
Julia Reynolds50989772017-02-23 14:32:16 -05002735 public Builder setTimeout(long durationMs) {
2736 mN.mTimeout = durationMs;
Julia Reynolds2a128742016-11-28 14:29:25 -05002737 return this;
2738 }
2739
2740 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002741 * Add a timestamp pertaining to the notification (usually the time the event occurred).
Selim Cinek0ff1ce602016-04-05 18:27:16 -07002742 *
2743 * For apps targeting {@link android.os.Build.VERSION_CODES#N} and above, this time is not
2744 * shown anymore by default and must be opted into by using
2745 * {@link android.app.Notification.Builder#setShowWhen(boolean)}
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002746 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002747 * @see Notification#when
Joe Onoratocb109a02011-01-18 17:57:41 -08002748 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002749 public Builder setWhen(long when) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002750 mN.when = when;
Joe Onorato46439ce2010-11-19 13:56:21 -08002751 return this;
2752 }
2753
Joe Onoratocb109a02011-01-18 17:57:41 -08002754 /**
Griff Hazen50c11652014-05-16 09:46:31 -07002755 * Control whether the timestamp set with {@link #setWhen(long) setWhen} is shown
Daniel Sandler0c890492012-09-12 17:23:10 -07002756 * in the content view.
Selim Cinek0ff1ce602016-04-05 18:27:16 -07002757 * For apps targeting {@link android.os.Build.VERSION_CODES#N} and above, this defaults to
2758 * {@code false}. For earlier apps, the default is {@code true}.
Daniel Sandler0c890492012-09-12 17:23:10 -07002759 */
2760 public Builder setShowWhen(boolean show) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002761 mN.extras.putBoolean(EXTRA_SHOW_WHEN, show);
Daniel Sandler0c890492012-09-12 17:23:10 -07002762 return this;
2763 }
2764
2765 /**
Daniel Sandlerd33b8032012-05-10 11:41:48 -04002766 * Show the {@link Notification#when} field as a stopwatch.
Joe Malin8d40d042012-11-05 11:36:40 -08002767 *
2768 * Instead of presenting <code>when</code> as a timestamp, the notification will show an
Daniel Sandlerd33b8032012-05-10 11:41:48 -04002769 * automatically updating display of the minutes and seconds since <code>when</code>.
Daniel Sandlera2985ed2012-04-03 16:42:00 -04002770 *
Daniel Sandlerd33b8032012-05-10 11:41:48 -04002771 * Useful when showing an elapsed time (like an ongoing phone call).
2772 *
Selim Cinek81c23aa2016-02-25 16:23:13 -08002773 * The counter can also be set to count down to <code>when</code> when using
Adrian Roos96b7e202016-05-17 13:50:38 -07002774 * {@link #setChronometerCountDown(boolean)}.
Selim Cinek81c23aa2016-02-25 16:23:13 -08002775 *
Daniel Sandlerd33b8032012-05-10 11:41:48 -04002776 * @see android.widget.Chronometer
Daniel Sandlera2985ed2012-04-03 16:42:00 -04002777 * @see Notification#when
Adrian Roos96b7e202016-05-17 13:50:38 -07002778 * @see #setChronometerCountDown(boolean)
Daniel Sandlera2985ed2012-04-03 16:42:00 -04002779 */
2780 public Builder setUsesChronometer(boolean b) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002781 mN.extras.putBoolean(EXTRA_SHOW_CHRONOMETER, b);
Daniel Sandlera2985ed2012-04-03 16:42:00 -04002782 return this;
2783 }
2784
2785 /**
Selim Cinek81c23aa2016-02-25 16:23:13 -08002786 * Sets the Chronometer to count down instead of counting up.
2787 *
2788 * <p>This is only relevant if {@link #setUsesChronometer(boolean)} has been set to true.
2789 * If it isn't set the chronometer will count up.
2790 *
2791 * @see #setUsesChronometer(boolean)
2792 */
Adrian Roos96b7e202016-05-17 13:50:38 -07002793 public Builder setChronometerCountDown(boolean countDown) {
2794 mN.extras.putBoolean(EXTRA_CHRONOMETER_COUNT_DOWN, countDown);
Selim Cinek81c23aa2016-02-25 16:23:13 -08002795 return this;
2796 }
2797
2798 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002799 * Set the small icon resource, which will be used to represent the notification in the
2800 * status bar.
Joe Onoratocb109a02011-01-18 17:57:41 -08002801 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002802
2803 * The platform template for the expanded view will draw this icon in the left, unless a
2804 * {@link #setLargeIcon(Bitmap) large icon} has also been specified, in which case the small
2805 * icon will be moved to the right-hand side.
2806 *
2807
2808 * @param icon
2809 * A resource ID in the application's package of the drawable to use.
2810 * @see Notification#icon
Joe Onoratocb109a02011-01-18 17:57:41 -08002811 */
Tor Norbye7b9c9122013-05-30 16:48:33 -07002812 public Builder setSmallIcon(@DrawableRes int icon) {
Dan Sandlerd63f9322015-05-06 15:18:49 -04002813 return setSmallIcon(icon != 0
2814 ? Icon.createWithResource(mContext, icon)
2815 : null);
Joe Onorato46439ce2010-11-19 13:56:21 -08002816 }
2817
Joe Onoratocb109a02011-01-18 17:57:41 -08002818 /**
2819 * A variant of {@link #setSmallIcon(int) setSmallIcon(int)} that takes an additional
2820 * level parameter for when the icon is a {@link android.graphics.drawable.LevelListDrawable
2821 * LevelListDrawable}.
2822 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002823 * @param icon A resource ID in the application's package of the drawable to use.
Joe Onoratocb109a02011-01-18 17:57:41 -08002824 * @param level The level to use for the icon.
2825 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002826 * @see Notification#icon
2827 * @see Notification#iconLevel
Joe Onoratocb109a02011-01-18 17:57:41 -08002828 */
Tor Norbye7b9c9122013-05-30 16:48:33 -07002829 public Builder setSmallIcon(@DrawableRes int icon, int level) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002830 mN.iconLevel = level;
Dan Sandlerd63f9322015-05-06 15:18:49 -04002831 return setSmallIcon(icon);
2832 }
2833
2834 /**
2835 * Set the small icon, which will be used to represent the notification in the
2836 * status bar and content view (unless overriden there by a
2837 * {@link #setLargeIcon(Bitmap) large icon}).
2838 *
2839 * @param icon An Icon object to use.
2840 * @see Notification#icon
2841 */
2842 public Builder setSmallIcon(Icon icon) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002843 mN.setSmallIcon(icon);
2844 if (icon != null && icon.getType() == Icon.TYPE_RESOURCE) {
2845 mN.icon = icon.getResId();
2846 }
Joe Onorato46439ce2010-11-19 13:56:21 -08002847 return this;
2848 }
2849
Joe Onoratocb109a02011-01-18 17:57:41 -08002850 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002851 * Set the first line of text in the platform notification template.
Joe Onoratocb109a02011-01-18 17:57:41 -08002852 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002853 public Builder setContentTitle(CharSequence title) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002854 mN.extras.putCharSequence(EXTRA_TITLE, safeCharSequence(title));
Joe Onorato46439ce2010-11-19 13:56:21 -08002855 return this;
2856 }
2857
Joe Onoratocb109a02011-01-18 17:57:41 -08002858 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002859 * Set the second line of text in the platform notification template.
Joe Onoratocb109a02011-01-18 17:57:41 -08002860 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002861 public Builder setContentText(CharSequence text) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002862 mN.extras.putCharSequence(EXTRA_TEXT, safeCharSequence(text));
Joe Onorato46439ce2010-11-19 13:56:21 -08002863 return this;
2864 }
2865
Joe Onoratocb109a02011-01-18 17:57:41 -08002866 /**
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07002867 * This provides some additional information that is displayed in the notification. No
2868 * guarantees are given where exactly it is displayed.
2869 *
2870 * <p>This information should only be provided if it provides an essential
2871 * benefit to the understanding of the notification. The more text you provide the
2872 * less readable it becomes. For example, an email client should only provide the account
2873 * name here if more than one email account has been added.</p>
2874 *
2875 * <p>As of {@link android.os.Build.VERSION_CODES#N} this information is displayed in the
2876 * notification header area.
2877 *
2878 * On Android versions before {@link android.os.Build.VERSION_CODES#N}
2879 * this will be shown in the third line of text in the platform notification template.
2880 * You should not be using {@link #setProgress(int, int, boolean)} at the
2881 * same time on those versions; they occupy the same place.
2882 * </p>
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002883 */
2884 public Builder setSubText(CharSequence text) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002885 mN.extras.putCharSequence(EXTRA_SUB_TEXT, safeCharSequence(text));
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002886 return this;
2887 }
2888
2889 /**
Adrian Roose458aa82015-12-08 16:17:19 -08002890 * Set the remote input history.
2891 *
2892 * This should be set to the most recent inputs that have been sent
2893 * through a {@link RemoteInput} of this Notification and cleared once the it is no
2894 * longer relevant (e.g. for chat notifications once the other party has responded).
2895 *
2896 * The most recent input must be stored at the 0 index, the second most recent at the
2897 * 1 index, etc. Note that the system will limit both how far back the inputs will be shown
2898 * and how much of each individual input is shown.
2899 *
2900 * <p>Note: The reply text will only be shown on notifications that have least one action
2901 * with a {@code RemoteInput}.</p>
2902 */
2903 public Builder setRemoteInputHistory(CharSequence[] text) {
2904 if (text == null) {
2905 mN.extras.putCharSequenceArray(EXTRA_REMOTE_INPUT_HISTORY, null);
2906 } else {
2907 final int N = Math.min(MAX_REPLY_HISTORY, text.length);
2908 CharSequence[] safe = new CharSequence[N];
2909 for (int i = 0; i < N; i++) {
2910 safe[i] = safeCharSequence(text[i]);
2911 }
2912 mN.extras.putCharSequenceArray(EXTRA_REMOTE_INPUT_HISTORY, safe);
2913 }
2914 return this;
2915 }
2916
2917 /**
Julia Reynolds13d898c2017-02-02 12:22:05 -05002918 * Sets the number of items this notification represents. May be displayed as a badge count
2919 * for Launchers that support badging.
Joe Onoratocb109a02011-01-18 17:57:41 -08002920 */
Joe Onorato8595a3d2010-11-19 18:12:07 -08002921 public Builder setNumber(int number) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002922 mN.number = number;
Joe Onorato8595a3d2010-11-19 18:12:07 -08002923 return this;
2924 }
2925
Joe Onoratocb109a02011-01-18 17:57:41 -08002926 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002927 * A small piece of additional information pertaining to this notification.
2928 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002929 * The platform template will draw this on the last line of the notification, at the far
2930 * right (to the right of a smallIcon if it has been placed there).
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07002931 *
2932 * @deprecated use {@link #setSubText(CharSequence)} instead to set a text in the header.
2933 * For legacy apps targeting a version below {@link android.os.Build.VERSION_CODES#N} this
2934 * field will still show up, but the subtext will take precedence.
Joe Onoratocb109a02011-01-18 17:57:41 -08002935 */
Aurimas Liutikas514c5ef2016-05-24 15:22:55 -07002936 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -08002937 public Builder setContentInfo(CharSequence info) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002938 mN.extras.putCharSequence(EXTRA_INFO_TEXT, safeCharSequence(info));
Joe Onorato46439ce2010-11-19 13:56:21 -08002939 return this;
2940 }
2941
Joe Onoratocb109a02011-01-18 17:57:41 -08002942 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002943 * Set the progress this notification represents.
2944 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002945 * The platform template will represent this using a {@link ProgressBar}.
Jeff Sharkey1c400132011-08-05 14:50:13 -07002946 */
2947 public Builder setProgress(int max, int progress, boolean indeterminate) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002948 mN.extras.putInt(EXTRA_PROGRESS, progress);
2949 mN.extras.putInt(EXTRA_PROGRESS_MAX, max);
2950 mN.extras.putBoolean(EXTRA_PROGRESS_INDETERMINATE, indeterminate);
Jeff Sharkey1c400132011-08-05 14:50:13 -07002951 return this;
2952 }
2953
2954 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002955 * Supply a custom RemoteViews to use instead of the platform template.
2956 *
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002957 * Use {@link #setCustomContentView(RemoteViews)} instead.
Joe Onoratocb109a02011-01-18 17:57:41 -08002958 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002959 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -08002960 public Builder setContent(RemoteViews views) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002961 return setCustomContentView(views);
2962 }
2963
2964 /**
2965 * Supply custom RemoteViews to use instead of the platform template.
2966 *
2967 * This will override the layout that would otherwise be constructed by this Builder
2968 * object.
2969 */
2970 public Builder setCustomContentView(RemoteViews contentView) {
2971 mN.contentView = contentView;
2972 return this;
2973 }
2974
2975 /**
2976 * Supply custom RemoteViews to use instead of the platform template in the expanded form.
2977 *
2978 * This will override the expanded layout that would otherwise be constructed by this
2979 * Builder object.
2980 */
2981 public Builder setCustomBigContentView(RemoteViews contentView) {
2982 mN.bigContentView = contentView;
2983 return this;
2984 }
2985
2986 /**
2987 * Supply custom RemoteViews to use instead of the platform template in the heads up dialog.
2988 *
2989 * This will override the heads-up layout that would otherwise be constructed by this
2990 * Builder object.
2991 */
2992 public Builder setCustomHeadsUpContentView(RemoteViews contentView) {
2993 mN.headsUpContentView = contentView;
Joe Onorato46439ce2010-11-19 13:56:21 -08002994 return this;
2995 }
2996
Joe Onoratocb109a02011-01-18 17:57:41 -08002997 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002998 * Supply a {@link PendingIntent} to be sent when the notification is clicked.
2999 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003000 * As of {@link android.os.Build.VERSION_CODES#HONEYCOMB}, if this field is unset and you
3001 * have specified a custom RemoteViews with {@link #setContent(RemoteViews)}, you can use
3002 * {@link RemoteViews#setOnClickPendingIntent RemoteViews.setOnClickPendingIntent(int,PendingIntent)}
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003003 * to assign PendingIntents to individual views in that custom layout (i.e., to create
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003004 * clickable buttons inside the notification view).
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003005 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003006 * @see Notification#contentIntent Notification.contentIntent
Joe Onoratocb109a02011-01-18 17:57:41 -08003007 */
Joe Onorato46439ce2010-11-19 13:56:21 -08003008 public Builder setContentIntent(PendingIntent intent) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003009 mN.contentIntent = intent;
Joe Onorato46439ce2010-11-19 13:56:21 -08003010 return this;
3011 }
3012
Joe Onoratocb109a02011-01-18 17:57:41 -08003013 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003014 * Supply a {@link PendingIntent} to send when the notification is cleared explicitly by the user.
3015 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003016 * @see Notification#deleteIntent
Joe Onoratocb109a02011-01-18 17:57:41 -08003017 */
Joe Onorato46439ce2010-11-19 13:56:21 -08003018 public Builder setDeleteIntent(PendingIntent intent) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003019 mN.deleteIntent = intent;
Joe Onorato46439ce2010-11-19 13:56:21 -08003020 return this;
3021 }
3022
Joe Onoratocb109a02011-01-18 17:57:41 -08003023 /**
3024 * An intent to launch instead of posting the notification to the status bar.
3025 * Only for use with extremely high-priority notifications demanding the user's
3026 * <strong>immediate</strong> attention, such as an incoming phone call or
3027 * alarm clock that the user has explicitly set to a particular time.
3028 * If this facility is used for something else, please give the user an option
3029 * to turn it off and use a normal notification, as this can be extremely
3030 * disruptive.
3031 *
Chris Wren47c20a12014-06-18 17:27:29 -04003032 * <p>
3033 * The system UI may choose to display a heads-up notification, instead of
3034 * launching this intent, while the user is using the device.
3035 * </p>
3036 *
Joe Onoratocb109a02011-01-18 17:57:41 -08003037 * @param intent The pending intent to launch.
3038 * @param highPriority Passing true will cause this notification to be sent
3039 * even if other notifications are suppressed.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003040 *
3041 * @see Notification#fullScreenIntent
Joe Onoratocb109a02011-01-18 17:57:41 -08003042 */
Joe Onorato46439ce2010-11-19 13:56:21 -08003043 public Builder setFullScreenIntent(PendingIntent intent, boolean highPriority) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003044 mN.fullScreenIntent = intent;
Joe Onorato46439ce2010-11-19 13:56:21 -08003045 setFlag(FLAG_HIGH_PRIORITY, highPriority);
3046 return this;
3047 }
3048
Joe Onoratocb109a02011-01-18 17:57:41 -08003049 /**
Dan Sandler5fcdf6e2014-07-18 11:31:15 -04003050 * Set the "ticker" text which is sent to accessibility services.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003051 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003052 * @see Notification#tickerText
Joe Onoratocb109a02011-01-18 17:57:41 -08003053 */
Joe Onorato46439ce2010-11-19 13:56:21 -08003054 public Builder setTicker(CharSequence tickerText) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003055 mN.tickerText = safeCharSequence(tickerText);
Joe Onorato46439ce2010-11-19 13:56:21 -08003056 return this;
3057 }
3058
Joe Onoratocb109a02011-01-18 17:57:41 -08003059 /**
Dan Sandler5fcdf6e2014-07-18 11:31:15 -04003060 * Obsolete version of {@link #setTicker(CharSequence)}.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003061 *
Joe Onoratocb109a02011-01-18 17:57:41 -08003062 */
Dan Sandler5fcdf6e2014-07-18 11:31:15 -04003063 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -08003064 public Builder setTicker(CharSequence tickerText, RemoteViews views) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003065 setTicker(tickerText);
3066 // views is ignored
Joe Onorato46439ce2010-11-19 13:56:21 -08003067 return this;
3068 }
3069
Joe Onoratocb109a02011-01-18 17:57:41 -08003070 /**
Dan Sandlerd63f9322015-05-06 15:18:49 -04003071 * Add a large icon to the notification content view.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003072 *
3073 * In the platform template, this image will be shown on the left of the notification view
Dan Sandlerd63f9322015-05-06 15:18:49 -04003074 * in place of the {@link #setSmallIcon(Icon) small icon} (which will be placed in a small
3075 * badge atop the large icon).
Dan Sandler08a04c12015-05-06 15:18:49 -04003076 */
Dan Sandlerd63f9322015-05-06 15:18:49 -04003077 public Builder setLargeIcon(Bitmap b) {
3078 return setLargeIcon(b != null ? Icon.createWithBitmap(b) : null);
3079 }
3080
3081 /**
3082 * Add a large icon to the notification content view.
3083 *
3084 * In the platform template, this image will be shown on the left of the notification view
3085 * in place of the {@link #setSmallIcon(Icon) small icon} (which will be placed in a small
3086 * badge atop the large icon).
3087 */
3088 public Builder setLargeIcon(Icon icon) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003089 mN.mLargeIcon = icon;
3090 mN.extras.putParcelable(EXTRA_LARGE_ICON, icon);
Joe Onorato46439ce2010-11-19 13:56:21 -08003091 return this;
3092 }
3093
Joe Onoratocb109a02011-01-18 17:57:41 -08003094 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003095 * Set the sound to play.
3096 *
John Spurlockc0650f022014-07-19 13:22:39 -04003097 * It will be played using the {@link #AUDIO_ATTRIBUTES_DEFAULT default audio attributes}
3098 * for notifications.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003099 *
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05003100 * @deprecated use {@link NotificationChannel#setSound(Uri, AudioAttributes)} instead.
Joe Onoratocb109a02011-01-18 17:57:41 -08003101 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05003102 @Deprecated
Joe Onorato52f80cd2010-11-21 15:34:48 -08003103 public Builder setSound(Uri sound) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003104 mN.sound = sound;
3105 mN.audioAttributes = AUDIO_ATTRIBUTES_DEFAULT;
Joe Onorato52f80cd2010-11-21 15:34:48 -08003106 return this;
3107 }
3108
Joe Onoratocb109a02011-01-18 17:57:41 -08003109 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003110 * Set the sound to play, along with a specific stream on which to play it.
Joe Onoratocb109a02011-01-18 17:57:41 -08003111 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003112 * See {@link android.media.AudioManager} for the <code>STREAM_</code> constants.
3113 *
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05003114 * @deprecated use {@link NotificationChannel#setSound(Uri, AudioAttributes)}.
Joe Onoratocb109a02011-01-18 17:57:41 -08003115 */
Jean-Michel Trivi81f871e2014-08-06 16:32:38 -07003116 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -08003117 public Builder setSound(Uri sound, int streamType) {
Jean-Michel Trivi2f7511f2016-11-28 15:40:27 -08003118 PlayerBase.deprecateStreamTypeForPlayback(streamType, "Notification", "setSound()");
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003119 mN.sound = sound;
3120 mN.audioStreamType = streamType;
Joe Onorato46439ce2010-11-19 13:56:21 -08003121 return this;
3122 }
3123
Joe Onoratocb109a02011-01-18 17:57:41 -08003124 /**
John Spurlockc0650f022014-07-19 13:22:39 -04003125 * Set the sound to play, along with specific {@link AudioAttributes audio attributes} to
3126 * use during playback.
3127 *
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05003128 * @deprecated use {@link NotificationChannel#setSound(Uri, AudioAttributes)} instead.
John Spurlockc0650f022014-07-19 13:22:39 -04003129 * @see Notification#sound
3130 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05003131 @Deprecated
John Spurlockc0650f022014-07-19 13:22:39 -04003132 public Builder setSound(Uri sound, AudioAttributes audioAttributes) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003133 mN.sound = sound;
3134 mN.audioAttributes = audioAttributes;
John Spurlockc0650f022014-07-19 13:22:39 -04003135 return this;
3136 }
3137
3138 /**
Joe Onoratocb109a02011-01-18 17:57:41 -08003139 * Set the vibration pattern to use.
3140 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003141 * See {@link android.os.Vibrator#vibrate(long[], int)} for a discussion of the
3142 * <code>pattern</code> parameter.
3143 *
Chris Wren47c20a12014-06-18 17:27:29 -04003144 * <p>
3145 * A notification that vibrates is more likely to be presented as a heads-up notification.
3146 * </p>
3147 *
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05003148 * @deprecated use {@link NotificationChannel#setVibrationPattern(long[])} instead.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003149 * @see Notification#vibrate
Joe Onoratocb109a02011-01-18 17:57:41 -08003150 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05003151 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -08003152 public Builder setVibrate(long[] pattern) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003153 mN.vibrate = pattern;
Joe Onorato46439ce2010-11-19 13:56:21 -08003154 return this;
3155 }
3156
Joe Onoratocb109a02011-01-18 17:57:41 -08003157 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003158 * Set the desired color for the indicator LED on the device, as well as the
3159 * blink duty cycle (specified in milliseconds).
3160 *
3161
3162 * Not all devices will honor all (or even any) of these values.
3163 *
Julia Reynolds529e3322017-02-06 08:33:01 -05003164 * @deprecated use {@link NotificationChannel#enableLights(boolean)} instead.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003165 * @see Notification#ledARGB
3166 * @see Notification#ledOnMS
3167 * @see Notification#ledOffMS
Joe Onoratocb109a02011-01-18 17:57:41 -08003168 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05003169 @Deprecated
Tor Norbye80756e32015-03-02 09:39:27 -08003170 public Builder setLights(@ColorInt int argb, int onMs, int offMs) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003171 mN.ledARGB = argb;
3172 mN.ledOnMS = onMs;
3173 mN.ledOffMS = offMs;
Julia Reynolds10ee1fc2015-11-09 11:04:55 -05003174 if (onMs != 0 || offMs != 0) {
3175 mN.flags |= FLAG_SHOW_LIGHTS;
3176 }
Joe Onorato46439ce2010-11-19 13:56:21 -08003177 return this;
3178 }
3179
Joe Onoratocb109a02011-01-18 17:57:41 -08003180 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003181 * Set whether this is an "ongoing" notification.
Joe Onoratocb109a02011-01-18 17:57:41 -08003182 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003183
3184 * Ongoing notifications cannot be dismissed by the user, so your application or service
3185 * must take care of canceling them.
3186 *
3187
3188 * They are typically used to indicate a background task that the user is actively engaged
3189 * with (e.g., playing music) or is pending in some way and therefore occupying the device
3190 * (e.g., a file download, sync operation, active network connection).
3191 *
3192
3193 * @see Notification#FLAG_ONGOING_EVENT
3194 * @see Service#setForeground(boolean)
Joe Onoratocb109a02011-01-18 17:57:41 -08003195 */
Joe Onorato46439ce2010-11-19 13:56:21 -08003196 public Builder setOngoing(boolean ongoing) {
3197 setFlag(FLAG_ONGOING_EVENT, ongoing);
3198 return this;
3199 }
3200
Joe Onoratocb109a02011-01-18 17:57:41 -08003201 /**
Selim Cinek7b9605b2017-01-19 17:36:00 -08003202 * Set whether this notification should be colorized. When set, the color set with
3203 * {@link #setColor(int)} will be used as the background color of this notification.
3204 * <p>
Selim Cinek7b9605b2017-01-19 17:36:00 -08003205 * This should only be used for high priority ongoing tasks like navigation, an ongoing
3206 * call, or other similarly high-priority events for the user.
Selim Cinek99104832017-01-25 14:47:33 -08003207 * <p>
Selim Cinek22714f12017-04-13 16:23:53 -07003208 * For most styles, the coloring will only be applied if the notification is for a
3209 * foreground service notification.
Selim Cinek99104832017-01-25 14:47:33 -08003210 * However, for {@link MediaStyle} and {@link DecoratedMediaCustomViewStyle} notifications
Selim Cinek22714f12017-04-13 16:23:53 -07003211 * that have a media session attached there is no such requirement.
Selim Cinek7b9605b2017-01-19 17:36:00 -08003212 *
3213 * @see Builder#setOngoing(boolean)
3214 * @see Builder#setColor(int)
Selim Cinek99104832017-01-25 14:47:33 -08003215 * @see MediaStyle#setMediaSession(MediaSession.Token)
Selim Cinek7b9605b2017-01-19 17:36:00 -08003216 */
3217 public Builder setColorized(boolean colorize) {
3218 mN.extras.putBoolean(EXTRA_COLORIZED, colorize);
3219 return this;
3220 }
3221
3222 /**
Joe Onoratocb109a02011-01-18 17:57:41 -08003223 * Set this flag if you would only like the sound, vibrate
3224 * and ticker to be played if the notification is not already showing.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003225 *
3226 * @see Notification#FLAG_ONLY_ALERT_ONCE
Joe Onoratocb109a02011-01-18 17:57:41 -08003227 */
Joe Onorato46439ce2010-11-19 13:56:21 -08003228 public Builder setOnlyAlertOnce(boolean onlyAlertOnce) {
3229 setFlag(FLAG_ONLY_ALERT_ONCE, onlyAlertOnce);
3230 return this;
3231 }
3232
Joe Onoratocb109a02011-01-18 17:57:41 -08003233 /**
Julia Reynolds04499532016-09-13 14:04:53 -04003234 * Make this notification automatically dismissed when the user touches it.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003235 *
3236 * @see Notification#FLAG_AUTO_CANCEL
Joe Onoratocb109a02011-01-18 17:57:41 -08003237 */
Joe Onorato46439ce2010-11-19 13:56:21 -08003238 public Builder setAutoCancel(boolean autoCancel) {
Joe Onorato281d83f2011-01-04 17:13:10 -08003239 setFlag(FLAG_AUTO_CANCEL, autoCancel);
Joe Onorato46439ce2010-11-19 13:56:21 -08003240 return this;
3241 }
3242
Joe Onoratocb109a02011-01-18 17:57:41 -08003243 /**
Griff Hazendfcb0802014-02-11 12:00:00 -08003244 * Set whether or not this notification should not bridge to other devices.
3245 *
3246 * <p>Some notifications can be bridged to other devices for remote display.
3247 * This hint can be set to recommend this notification not be bridged.
3248 */
3249 public Builder setLocalOnly(boolean localOnly) {
3250 setFlag(FLAG_LOCAL_ONLY, localOnly);
3251 return this;
3252 }
3253
3254 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003255 * Set which notification properties will be inherited from system defaults.
Joe Onoratocb109a02011-01-18 17:57:41 -08003256 * <p>
3257 * The value should be one or more of the following fields combined with
3258 * bitwise-or:
3259 * {@link #DEFAULT_SOUND}, {@link #DEFAULT_VIBRATE}, {@link #DEFAULT_LIGHTS}.
3260 * <p>
3261 * For all default values, use {@link #DEFAULT_ALL}.
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05003262 *
3263 * @deprecated use {@link NotificationChannel#enableVibration(boolean)} and
Julia Reynolds529e3322017-02-06 08:33:01 -05003264 * {@link NotificationChannel#enableLights(boolean)} and
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05003265 * {@link NotificationChannel#setSound(Uri, AudioAttributes)} instead.
Joe Onoratocb109a02011-01-18 17:57:41 -08003266 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05003267 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -08003268 public Builder setDefaults(int defaults) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003269 mN.defaults = defaults;
Joe Onorato46439ce2010-11-19 13:56:21 -08003270 return this;
3271 }
3272
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003273 /**
3274 * Set the priority of this notification.
3275 *
3276 * @see Notification#priority
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05003277 * @deprecated use {@link NotificationChannel#setImportance(int)} instead.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003278 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05003279 @Deprecated
Tor Norbyed9273d62013-05-30 15:59:53 -07003280 public Builder setPriority(@Priority int pri) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003281 mN.priority = pri;
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003282 return this;
3283 }
Joe Malin8d40d042012-11-05 11:36:40 -08003284
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003285 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -04003286 * Set the notification category.
Joe Malin8d40d042012-11-05 11:36:40 -08003287 *
John Spurlockfd7f1e02014-03-18 16:41:57 -04003288 * @see Notification#category
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003289 */
John Spurlockfd7f1e02014-03-18 16:41:57 -04003290 public Builder setCategory(String category) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003291 mN.category = category;
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003292 return this;
3293 }
3294
3295 /**
Chris Wrendde75302014-03-26 17:24:15 -04003296 * Add a person that is relevant to this notification.
3297 *
Chris Wrene6c48932014-09-29 17:19:27 -04003298 * <P>
3299 * Depending on user preferences, this annotation may allow the notification to pass
Julia Reynoldse071abd2017-03-22 10:52:11 -04003300 * through interruption filters, if this notification is of category {@link #CATEGORY_CALL}
3301 * or {@link #CATEGORY_MESSAGE}. The addition of people may also cause this notification to
3302 * appear more prominently in the user interface.
Chris Wrene6c48932014-09-29 17:19:27 -04003303 * </P>
3304 *
3305 * <P>
3306 * The person should be specified by the {@code String} representation of a
3307 * {@link android.provider.ContactsContract.Contacts#CONTENT_LOOKUP_URI}.
3308 * </P>
3309 *
3310 * <P>The system will also attempt to resolve {@code mailto:} and {@code tel:} schema
3311 * URIs. The path part of these URIs must exist in the contacts database, in the
3312 * appropriate column, or the reference will be discarded as invalid. Telephone schema
3313 * URIs will be resolved by {@link android.provider.ContactsContract.PhoneLookup}.
3314 * </P>
3315 *
3316 * @param uri A URI for the person.
Chris Wrendde75302014-03-26 17:24:15 -04003317 * @see Notification#EXTRA_PEOPLE
3318 */
Chris Wrene6c48932014-09-29 17:19:27 -04003319 public Builder addPerson(String uri) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003320 mPersonList.add(uri);
Chris Wrendde75302014-03-26 17:24:15 -04003321 return this;
3322 }
3323
3324 /**
Griff Hazen5cadc3b2014-05-20 09:55:39 -07003325 * Set this notification to be part of a group of notifications sharing the same key.
3326 * Grouped notifications may display in a cluster or stack on devices which
3327 * support such rendering.
3328 *
3329 * <p>To make this notification the summary for its group, also call
3330 * {@link #setGroupSummary}. A sort order can be specified for group members by using
3331 * {@link #setSortKey}.
3332 * @param groupKey The group key of the group.
3333 * @return this object for method chaining
3334 */
3335 public Builder setGroup(String groupKey) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003336 mN.mGroupKey = groupKey;
Griff Hazen5cadc3b2014-05-20 09:55:39 -07003337 return this;
3338 }
3339
3340 /**
3341 * Set this notification to be the group summary for a group of notifications.
3342 * Grouped notifications may display in a cluster or stack on devices which
Julia Reynolds04499532016-09-13 14:04:53 -04003343 * support such rendering. If thereRequires a group key also be set using {@link #setGroup}.
3344 * The group summary may be suppressed if too few notifications are included in the group.
Griff Hazen5cadc3b2014-05-20 09:55:39 -07003345 * @param isGroupSummary Whether this notification should be a group summary.
3346 * @return this object for method chaining
3347 */
3348 public Builder setGroupSummary(boolean isGroupSummary) {
3349 setFlag(FLAG_GROUP_SUMMARY, isGroupSummary);
3350 return this;
3351 }
3352
3353 /**
3354 * Set a sort key that orders this notification among other notifications from the
3355 * same package. This can be useful if an external sort was already applied and an app
3356 * would like to preserve this. Notifications will be sorted lexicographically using this
3357 * value, although providing different priorities in addition to providing sort key may
3358 * cause this value to be ignored.
3359 *
3360 * <p>This sort key can also be used to order members of a notification group. See
Griff Hazen9e1379f2014-05-20 12:50:51 -07003361 * {@link #setGroup}.
Griff Hazen5cadc3b2014-05-20 09:55:39 -07003362 *
3363 * @see String#compareTo(String)
3364 */
3365 public Builder setSortKey(String sortKey) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003366 mN.mSortKey = sortKey;
Griff Hazen5cadc3b2014-05-20 09:55:39 -07003367 return this;
3368 }
3369
3370 /**
Griff Hazen720042b2014-02-24 15:46:56 -08003371 * Merge additional metadata into this notification.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003372 *
Griff Hazen720042b2014-02-24 15:46:56 -08003373 * <p>Values within the Bundle will replace existing extras values in this Builder.
3374 *
3375 * @see Notification#extras
3376 */
Griff Hazen959591e2014-05-15 22:26:18 -07003377 public Builder addExtras(Bundle extras) {
3378 if (extras != null) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003379 mUserExtras.putAll(extras);
Griff Hazen720042b2014-02-24 15:46:56 -08003380 }
3381 return this;
3382 }
3383
3384 /**
3385 * Set metadata for this notification.
3386 *
3387 * <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 -04003388 * current contents are copied into the Notification each time {@link #build()} is
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003389 * called.
3390 *
Griff Hazen720042b2014-02-24 15:46:56 -08003391 * <p>Replaces any existing extras values with those from the provided Bundle.
3392 * Use {@link #addExtras} to merge in metadata instead.
3393 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003394 * @see Notification#extras
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003395 */
Griff Hazen959591e2014-05-15 22:26:18 -07003396 public Builder setExtras(Bundle extras) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003397 if (extras != null) {
3398 mUserExtras = extras;
3399 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003400 return this;
3401 }
3402
Daniel Sandlera0a938c2012-03-15 08:42:37 -04003403 /**
Griff Hazen720042b2014-02-24 15:46:56 -08003404 * Get the current metadata Bundle used by this notification Builder.
3405 *
3406 * <p>The returned Bundle is shared with this Builder.
3407 *
3408 * <p>The current contents of this Bundle are copied into the Notification each time
3409 * {@link #build()} is called.
3410 *
3411 * @see Notification#extras
3412 */
3413 public Bundle getExtras() {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003414 return mUserExtras;
3415 }
3416
3417 private Bundle getAllExtras() {
3418 final Bundle saveExtras = (Bundle) mUserExtras.clone();
3419 saveExtras.putAll(mN.extras);
3420 return saveExtras;
Griff Hazen720042b2014-02-24 15:46:56 -08003421 }
3422
3423 /**
Daniel Sandlera0a938c2012-03-15 08:42:37 -04003424 * Add an action to this notification. Actions are typically displayed by
3425 * the system as a button adjacent to the notification content.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04003426 * <p>
3427 * Every action must have an icon (32dp square and matching the
3428 * <a href="{@docRoot}design/style/iconography.html#action-bar">Holo
3429 * Dark action bar</a> visual style), a textual label, and a {@link PendingIntent}.
3430 * <p>
3431 * A notification in its expanded form can display up to 3 actions, from left to right in
3432 * the order they were added. Actions will not be displayed when the notification is
3433 * collapsed, however, so be sure that any essential functions may be accessed by the user
3434 * in some other way (for example, in the Activity pointed to by {@link #contentIntent}).
Daniel Sandlera0a938c2012-03-15 08:42:37 -04003435 *
3436 * @param icon Resource ID of a drawable that represents the action.
3437 * @param title Text describing the action.
3438 * @param intent PendingIntent to be fired when the action is invoked.
Dan Sandler86647982015-05-13 23:41:13 -04003439 *
3440 * @deprecated Use {@link #addAction(Action)} instead.
Daniel Sandlera0a938c2012-03-15 08:42:37 -04003441 */
Dan Sandler86647982015-05-13 23:41:13 -04003442 @Deprecated
Daniel Sandlera0a938c2012-03-15 08:42:37 -04003443 public Builder addAction(int icon, CharSequence title, PendingIntent intent) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04003444 mActions.add(new Action(icon, safeCharSequence(title), intent));
Daniel Sandlera0a938c2012-03-15 08:42:37 -04003445 return this;
3446 }
3447
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003448 /**
Griff Hazen959591e2014-05-15 22:26:18 -07003449 * Add an action to this notification. Actions are typically displayed by
3450 * the system as a button adjacent to the notification content.
3451 * <p>
3452 * Every action must have an icon (32dp square and matching the
3453 * <a href="{@docRoot}design/style/iconography.html#action-bar">Holo
3454 * Dark action bar</a> visual style), a textual label, and a {@link PendingIntent}.
3455 * <p>
3456 * A notification in its expanded form can display up to 3 actions, from left to right in
3457 * the order they were added. Actions will not be displayed when the notification is
3458 * collapsed, however, so be sure that any essential functions may be accessed by the user
3459 * in some other way (for example, in the Activity pointed to by {@link #contentIntent}).
3460 *
3461 * @param action The action to add.
3462 */
3463 public Builder addAction(Action action) {
liangweikang63b03b52017-03-16 19:22:15 +08003464 if (action != null) {
3465 mActions.add(action);
3466 }
Griff Hazen959591e2014-05-15 22:26:18 -07003467 return this;
3468 }
3469
3470 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003471 * Alter the complete list of actions attached to this notification.
3472 * @see #addAction(Action).
3473 *
3474 * @param actions
3475 * @return
3476 */
3477 public Builder setActions(Action... actions) {
3478 mActions.clear();
3479 for (int i = 0; i < actions.length; i++) {
liangweikang63b03b52017-03-16 19:22:15 +08003480 if (actions[i] != null) {
3481 mActions.add(actions[i]);
3482 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003483 }
3484 return this;
3485 }
3486
3487 /**
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003488 * Add a rich notification style to be applied at build time.
3489 *
3490 * @param style Object responsible for modifying the notification style.
3491 */
3492 public Builder setStyle(Style style) {
3493 if (mStyle != style) {
3494 mStyle = style;
Daniel Sandlerc08dea22012-06-28 08:35:24 -07003495 if (mStyle != null) {
3496 mStyle.setBuilder(this);
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003497 mN.extras.putString(EXTRA_TEMPLATE, style.getClass().getName());
3498 } else {
3499 mN.extras.remove(EXTRA_TEMPLATE);
Daniel Sandlerc08dea22012-06-28 08:35:24 -07003500 }
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003501 }
3502 return this;
3503 }
3504
Dan Sandler0bf2ed82013-12-21 23:33:41 -06003505 /**
3506 * Specify the value of {@link #visibility}.
Griff Hazenb720abe2014-05-20 13:15:30 -07003507 *
Dan Sandler0bf2ed82013-12-21 23:33:41 -06003508 * @param visibility One of {@link #VISIBILITY_PRIVATE} (the default),
3509 * {@link #VISIBILITY_SECRET}, or {@link #VISIBILITY_PUBLIC}.
3510 *
3511 * @return The same Builder.
3512 */
3513 public Builder setVisibility(int visibility) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003514 mN.visibility = visibility;
Dan Sandler0bf2ed82013-12-21 23:33:41 -06003515 return this;
3516 }
3517
3518 /**
3519 * Supply a replacement Notification whose contents should be shown in insecure contexts
3520 * (i.e. atop the secure lockscreen). See {@link #visibility} and {@link #VISIBILITY_PUBLIC}.
3521 * @param n A replacement notification, presumably with some or all info redacted.
3522 * @return The same Builder.
3523 */
3524 public Builder setPublicVersion(Notification n) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003525 if (n != null) {
3526 mN.publicVersion = new Notification();
3527 n.cloneInto(mN.publicVersion, /*heavy=*/ true);
3528 } else {
3529 mN.publicVersion = null;
3530 }
Dan Sandler0bf2ed82013-12-21 23:33:41 -06003531 return this;
3532 }
3533
Griff Hazenb720abe2014-05-20 13:15:30 -07003534 /**
Griff Hazen5cadc3b2014-05-20 09:55:39 -07003535 * Apply an extender to this notification builder. Extenders may be used to add
3536 * metadata or change options on this builder.
3537 */
Griff Hazen61a9e862014-05-22 16:05:19 -07003538 public Builder extend(Extender extender) {
3539 extender.extend(this);
Griff Hazen5cadc3b2014-05-20 09:55:39 -07003540 return this;
3541 }
3542
Dan Sandler4e787062015-06-17 15:09:48 -04003543 /**
3544 * @hide
3545 */
Julia Reynoldse46bb372016-03-17 11:05:58 -04003546 public Builder setFlag(int mask, boolean value) {
Joe Onorato46439ce2010-11-19 13:56:21 -08003547 if (value) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003548 mN.flags |= mask;
Joe Onorato46439ce2010-11-19 13:56:21 -08003549 } else {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003550 mN.flags &= ~mask;
Joe Onorato46439ce2010-11-19 13:56:21 -08003551 }
Julia Reynoldse46bb372016-03-17 11:05:58 -04003552 return this;
Joe Onorato46439ce2010-11-19 13:56:21 -08003553 }
3554
Dan Sandler26e81cf2014-05-06 10:01:27 -04003555 /**
3556 * Sets {@link Notification#color}.
3557 *
3558 * @param argb The accent color to use
3559 *
3560 * @return The same Builder.
3561 */
Tor Norbye80756e32015-03-02 09:39:27 -08003562 public Builder setColor(@ColorInt int argb) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003563 mN.color = argb;
Julia Reynolds10ee1fc2015-11-09 11:04:55 -05003564 sanitizeColor();
Dan Sandler26e81cf2014-05-06 10:01:27 -04003565 return this;
3566 }
3567
Jorim Jaggid05aa3e2014-08-28 17:52:27 +02003568 private Drawable getProfileBadgeDrawable() {
Chris Wren66619a22016-05-12 16:42:37 -04003569 if (mContext.getUserId() == UserHandle.USER_SYSTEM) {
3570 // This user can never be a badged profile,
3571 // and also includes USER_ALL system notifications.
3572 return null;
3573 }
Christoph Studer7ac80e62014-08-04 16:01:57 +02003574 // Note: This assumes that the current user can read the profile badge of the
3575 // originating user.
Selim Cineke6ff9462016-01-15 15:07:06 -08003576 return mContext.getPackageManager().getUserBadgeForDensityNoBackground(
Julia Reynoldsda303542015-11-23 14:00:20 -05003577 new UserHandle(mContext.getUserId()), 0);
Jorim Jaggid05aa3e2014-08-28 17:52:27 +02003578 }
3579
3580 private Bitmap getProfileBadge() {
3581 Drawable badge = getProfileBadgeDrawable();
Kenny Guy8a0101b2014-05-08 23:34:12 +01003582 if (badge == null) {
3583 return null;
3584 }
Jorim Jaggid05aa3e2014-08-28 17:52:27 +02003585 final int size = mContext.getResources().getDimensionPixelSize(
3586 R.dimen.notification_badge_size);
3587 Bitmap bitmap = Bitmap.createBitmap(size, size, Bitmap.Config.ARGB_8888);
Kenny Guy8a0101b2014-05-08 23:34:12 +01003588 Canvas canvas = new Canvas(bitmap);
Jorim Jaggid05aa3e2014-08-28 17:52:27 +02003589 badge.setBounds(0, 0, size, size);
Kenny Guy8a0101b2014-05-08 23:34:12 +01003590 badge.draw(canvas);
3591 return bitmap;
3592 }
3593
Selim Cinekc848c3a2016-01-13 15:27:30 -08003594 private void bindProfileBadge(RemoteViews contentView) {
Kenny Guy98193ea2014-07-24 19:54:37 +01003595 Bitmap profileBadge = getProfileBadge();
3596
Kenny Guy98193ea2014-07-24 19:54:37 +01003597 if (profileBadge != null) {
Selim Cinekc848c3a2016-01-13 15:27:30 -08003598 contentView.setImageViewBitmap(R.id.profile_badge, profileBadge);
3599 contentView.setViewVisibility(R.id.profile_badge, View.VISIBLE);
Selim Cinek7b9605b2017-01-19 17:36:00 -08003600 if (isColorized()) {
3601 contentView.setDrawableParameters(R.id.profile_badge, false, -1,
3602 getPrimaryTextColor(), PorterDuff.Mode.SRC_ATOP, -1);
3603 }
Kenny Guy98193ea2014-07-24 19:54:37 +01003604 }
Kenny Guy98193ea2014-07-24 19:54:37 +01003605 }
3606
Christoph Studerfe718432014-09-01 18:21:18 +02003607 private void resetStandardTemplate(RemoteViews contentView) {
Selim Cinekeaa29ca2015-11-23 13:51:13 -08003608 resetNotificationHeader(contentView);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003609 resetContentMargins(contentView);
Christoph Studerfe718432014-09-01 18:21:18 +02003610 contentView.setViewVisibility(R.id.right_icon, View.GONE);
Selim Cinek860b6da2015-12-16 19:02:19 -08003611 contentView.setViewVisibility(R.id.title, View.GONE);
Christoph Studerfe718432014-09-01 18:21:18 +02003612 contentView.setTextViewText(R.id.title, null);
Selim Cinek41598732016-01-11 16:58:37 -08003613 contentView.setViewVisibility(R.id.text, View.GONE);
Christoph Studerfe718432014-09-01 18:21:18 +02003614 contentView.setTextViewText(R.id.text, null);
Selim Cinek29603462015-11-17 19:04:39 -08003615 contentView.setViewVisibility(R.id.text_line_1, View.GONE);
Selim Cinek41598732016-01-11 16:58:37 -08003616 contentView.setTextViewText(R.id.text_line_1, null);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003617 }
3618
Selim Cinekeaa29ca2015-11-23 13:51:13 -08003619 /**
3620 * Resets the notification header to its original state
3621 */
3622 private void resetNotificationHeader(RemoteViews contentView) {
Adrian Roosc4337a32016-08-02 18:30:34 -07003623 // Small icon doesn't need to be reset, as it's always set. Resetting would prevent
3624 // re-using the drawable when the notification is updated.
Selim Cinek7b836392015-12-04 20:02:59 -08003625 contentView.setBoolean(R.id.notification_header, "setExpanded", false);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003626 contentView.setTextViewText(R.id.app_name_text, null);
Christoph Studerca1db712014-09-10 17:31:33 +02003627 contentView.setViewVisibility(R.id.chronometer, View.GONE);
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07003628 contentView.setViewVisibility(R.id.header_text, View.GONE);
Adrian Roos9dfb78f2016-06-30 15:43:44 -07003629 contentView.setTextViewText(R.id.header_text, null);
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07003630 contentView.setViewVisibility(R.id.header_text_divider, View.GONE);
Selim Cinek29603462015-11-17 19:04:39 -08003631 contentView.setViewVisibility(R.id.time_divider, View.GONE);
Selim Cinekb85f36fd2016-04-20 18:46:36 -07003632 contentView.setViewVisibility(R.id.time, View.GONE);
Selim Cinekc848c3a2016-01-13 15:27:30 -08003633 contentView.setImageViewIcon(R.id.profile_badge, null);
3634 contentView.setViewVisibility(R.id.profile_badge, View.GONE);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003635 }
3636
3637 private void resetContentMargins(RemoteViews contentView) {
Adrian Roos2d5dbba2016-06-08 17:11:53 -07003638 contentView.setViewLayoutMarginEndDimen(R.id.line1, 0);
3639 contentView.setViewLayoutMarginEndDimen(R.id.text, 0);
Christoph Studerfe718432014-09-01 18:21:18 +02003640 }
3641
Jorim Jaggi445d3c02014-08-19 22:33:42 +02003642 private RemoteViews applyStandardTemplate(int resId) {
Adrian Roos70d7aa32017-01-11 15:39:06 -08003643 return applyStandardTemplate(resId, mParams.reset().fillTextsFrom(this));
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02003644 }
3645
3646 /**
3647 * @param hasProgress whether the progress bar should be shown and set
3648 */
3649 private RemoteViews applyStandardTemplate(int resId, boolean hasProgress) {
Adrian Roos70d7aa32017-01-11 15:39:06 -08003650 return applyStandardTemplate(resId, mParams.reset().hasProgress(hasProgress)
3651 .fillTextsFrom(this));
Adrian Roosc1a80b02016-04-05 14:54:55 -07003652 }
3653
Adrian Roos70d7aa32017-01-11 15:39:06 -08003654 private RemoteViews applyStandardTemplate(int resId, StandardTemplateParams p) {
Kenny Guy77320062014-08-27 21:37:15 +01003655 RemoteViews contentView = new BuilderRemoteViews(mContext.getApplicationInfo(), resId);
Dan Sandler539aad42014-08-04 00:43:39 -04003656
Christoph Studerfe718432014-09-01 18:21:18 +02003657 resetStandardTemplate(contentView);
3658
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003659 final Bundle ex = mN.extras;
Selim Cinek7b9605b2017-01-19 17:36:00 -08003660 updateBackgroundColor(contentView);
Adrian Roos487374f2017-01-11 15:48:14 -08003661 bindNotificationHeader(contentView, p.ambient);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003662 bindLargeIcon(contentView);
Adrian Roos70d7aa32017-01-11 15:39:06 -08003663 boolean showProgress = handleProgressBar(p.hasProgress, contentView, ex);
3664 if (p.title != null) {
Selim Cinek860b6da2015-12-16 19:02:19 -08003665 contentView.setViewVisibility(R.id.title, View.VISIBLE);
Adrian Roos70d7aa32017-01-11 15:39:06 -08003666 contentView.setTextViewText(R.id.title, p.title);
Adrian Roos72171622017-01-27 10:32:06 -08003667 if (!p.ambient) {
3668 setTextViewColorPrimary(contentView, R.id.title);
3669 }
Selim Cinek954cc232016-05-20 13:29:23 -07003670 contentView.setViewLayoutWidth(R.id.title, showProgress
3671 ? ViewGroup.LayoutParams.WRAP_CONTENT
3672 : ViewGroup.LayoutParams.MATCH_PARENT);
Joe Onorato561d3852010-11-20 18:09:34 -08003673 }
Adrian Roos70d7aa32017-01-11 15:39:06 -08003674 if (p.text != null) {
Selim Cinek41598732016-01-11 16:58:37 -08003675 int textId = showProgress ? com.android.internal.R.id.text_line_1
3676 : com.android.internal.R.id.text;
Adrian Roos70d7aa32017-01-11 15:39:06 -08003677 contentView.setTextViewText(textId, p.text);
Adrian Roos72171622017-01-27 10:32:06 -08003678 if (!p.ambient) {
3679 setTextViewColorSecondary(contentView, textId);
3680 }
Selim Cinek41598732016-01-11 16:58:37 -08003681 contentView.setViewVisibility(textId, View.VISIBLE);
Joe Onorato561d3852010-11-20 18:09:34 -08003682 }
Selim Cinekc848c3a2016-01-13 15:27:30 -08003683
Selim Cinek279fa862016-06-14 10:57:25 -07003684 setContentMinHeight(contentView, showProgress || mN.hasLargeIcon());
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003685
Selim Cinek29603462015-11-17 19:04:39 -08003686 return contentView;
3687 }
3688
Selim Cinek7b9605b2017-01-19 17:36:00 -08003689 private void setTextViewColorPrimary(RemoteViews contentView, int id) {
3690 ensureColors();
3691 contentView.setTextColor(id, mPrimaryTextColor);
3692 }
3693
3694 private int getPrimaryTextColor() {
3695 ensureColors();
3696 return mPrimaryTextColor;
3697 }
3698
3699 private int getActionBarColor() {
3700 ensureColors();
3701 return mActionBarColor;
3702 }
3703
3704 private void setTextViewColorSecondary(RemoteViews contentView, int id) {
3705 ensureColors();
3706 contentView.setTextColor(id, mSecondaryTextColor);
3707 }
3708
3709 private void ensureColors() {
3710 int backgroundColor = getBackgroundColor();
3711 if (mPrimaryTextColor == COLOR_INVALID
3712 || mSecondaryTextColor == COLOR_INVALID
3713 || mActionBarColor == COLOR_INVALID
3714 || mTextColorsAreForBackground != backgroundColor) {
3715 mTextColorsAreForBackground = backgroundColor;
3716 mPrimaryTextColor = NotificationColorUtil.resolvePrimaryColor(
3717 mContext, backgroundColor);
3718 mSecondaryTextColor = NotificationColorUtil.resolveSecondaryColor(
3719 mContext, backgroundColor);
Selim Cinek875ba9b2017-02-13 16:20:17 -08003720 mActionBarColor = NotificationColorUtil.resolveActionBarColor(mContext,
3721 backgroundColor);
Selim Cinek7b9605b2017-01-19 17:36:00 -08003722 }
3723 }
3724
3725 private void updateBackgroundColor(RemoteViews contentView) {
3726 if (isColorized()) {
3727 contentView.setInt(R.id.status_bar_latest_event_content, "setBackgroundColor",
3728 getBackgroundColor());
3729 } else {
3730 // Clear it!
3731 contentView.setInt(R.id.status_bar_latest_event_content, "setBackgroundResource",
3732 0);
3733 }
3734 }
3735
Selim Cinek860b6da2015-12-16 19:02:19 -08003736 /**
3737 * @param remoteView the remote view to update the minheight in
3738 * @param hasMinHeight does it have a mimHeight
3739 * @hide
3740 */
3741 void setContentMinHeight(RemoteViews remoteView, boolean hasMinHeight) {
3742 int minHeight = 0;
3743 if (hasMinHeight) {
3744 // we need to set the minHeight of the notification
3745 minHeight = mContext.getResources().getDimensionPixelSize(
3746 com.android.internal.R.dimen.notification_min_content_height);
3747 }
3748 remoteView.setInt(R.id.notification_main_column, "setMinimumHeight", minHeight);
3749 }
3750
Selim Cinek29603462015-11-17 19:04:39 -08003751 private boolean handleProgressBar(boolean hasProgress, RemoteViews contentView, Bundle ex) {
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003752 final int max = ex.getInt(EXTRA_PROGRESS_MAX, 0);
3753 final int progress = ex.getInt(EXTRA_PROGRESS, 0);
3754 final boolean ind = ex.getBoolean(EXTRA_PROGRESS_INDETERMINATE);
3755 if (hasProgress && (max != 0 || ind)) {
Selim Cinek29603462015-11-17 19:04:39 -08003756 contentView.setViewVisibility(com.android.internal.R.id.progress, View.VISIBLE);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003757 contentView.setProgressBar(
Selim Cinek29603462015-11-17 19:04:39 -08003758 R.id.progress, max, progress, ind);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003759 contentView.setProgressBackgroundTintList(
3760 R.id.progress, ColorStateList.valueOf(mContext.getColor(
3761 R.color.notification_progress_background_color)));
Selim Cinek29603462015-11-17 19:04:39 -08003762 if (mN.color != COLOR_DEFAULT) {
Adrian Roos4ff3b122016-02-01 12:26:13 -08003763 ColorStateList colorStateList = ColorStateList.valueOf(resolveContrastColor());
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003764 contentView.setProgressTintList(R.id.progress, colorStateList);
3765 contentView.setProgressIndeterminateTintList(R.id.progress, colorStateList);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003766 }
Selim Cinek29603462015-11-17 19:04:39 -08003767 return true;
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003768 } else {
3769 contentView.setViewVisibility(R.id.progress, View.GONE);
Selim Cinek29603462015-11-17 19:04:39 -08003770 return false;
Jeff Sharkey1c400132011-08-05 14:50:13 -07003771 }
Joe Onorato561d3852010-11-20 18:09:34 -08003772 }
3773
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003774 private void bindLargeIcon(RemoteViews contentView) {
Selim Cinek279fa862016-06-14 10:57:25 -07003775 if (mN.mLargeIcon == null && mN.largeIcon != null) {
3776 mN.mLargeIcon = Icon.createWithBitmap(mN.largeIcon);
3777 }
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003778 if (mN.mLargeIcon != null) {
3779 contentView.setViewVisibility(R.id.right_icon, View.VISIBLE);
3780 contentView.setImageViewIcon(R.id.right_icon, mN.mLargeIcon);
3781 processLargeLegacyIcon(mN.mLargeIcon, contentView);
Adrian Roos2d5dbba2016-06-08 17:11:53 -07003782 int endMargin = R.dimen.notification_content_picture_margin;
3783 contentView.setViewLayoutMarginEndDimen(R.id.line1, endMargin);
3784 contentView.setViewLayoutMarginEndDimen(R.id.text, endMargin);
3785 contentView.setViewLayoutMarginEndDimen(R.id.progress, endMargin);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003786 }
3787 }
3788
Adrian Roos487374f2017-01-11 15:48:14 -08003789 private void bindNotificationHeader(RemoteViews contentView, boolean ambient) {
3790 bindSmallIcon(contentView, ambient);
3791 bindHeaderAppName(contentView, ambient);
3792 if (!ambient) {
3793 // Ambient view does not have these
3794 bindHeaderText(contentView);
3795 bindHeaderChronometerAndTime(contentView);
Adrian Roos487374f2017-01-11 15:48:14 -08003796 bindProfileBadge(contentView);
3797 }
Adrian Roosd83e9992017-03-16 15:17:57 -07003798 bindExpandButton(contentView);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003799 }
3800
3801 private void bindExpandButton(RemoteViews contentView) {
Selim Cinek99104832017-01-25 14:47:33 -08003802 int color = getPrimaryHighlightColor();
Selim Cinek7b9605b2017-01-19 17:36:00 -08003803 contentView.setDrawableParameters(R.id.expand_button, false, -1, color,
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003804 PorterDuff.Mode.SRC_ATOP, -1);
Selim Cinekea4bef72015-12-02 15:51:10 -08003805 contentView.setInt(R.id.notification_header, "setOriginalNotificationColor",
Selim Cinek7b9605b2017-01-19 17:36:00 -08003806 color);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003807 }
3808
Selim Cinek99104832017-01-25 14:47:33 -08003809 /**
3810 * @return the color that is used as the first primary highlight color. This is applied
3811 * in several places like the action buttons or the app name in the header.
3812 */
3813 private int getPrimaryHighlightColor() {
3814 return isColorized() ? getPrimaryTextColor() : resolveContrastColor();
3815 }
3816
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003817 private void bindHeaderChronometerAndTime(RemoteViews contentView) {
3818 if (showsTimeOrChronometer()) {
Selim Cinek29603462015-11-17 19:04:39 -08003819 contentView.setViewVisibility(R.id.time_divider, View.VISIBLE);
Selim Cinek7b9605b2017-01-19 17:36:00 -08003820 setTextViewColorSecondary(contentView, R.id.time_divider);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003821 if (mN.extras.getBoolean(EXTRA_SHOW_CHRONOMETER)) {
3822 contentView.setViewVisibility(R.id.chronometer, View.VISIBLE);
3823 contentView.setLong(R.id.chronometer, "setBase",
3824 mN.when + (SystemClock.elapsedRealtime() - System.currentTimeMillis()));
3825 contentView.setBoolean(R.id.chronometer, "setStarted", true);
Adrian Roos96b7e202016-05-17 13:50:38 -07003826 boolean countsDown = mN.extras.getBoolean(EXTRA_CHRONOMETER_COUNT_DOWN);
Selim Cinekc3b752e2016-04-20 16:13:59 -07003827 contentView.setChronometerCountDown(R.id.chronometer, countsDown);
Selim Cinek7b9605b2017-01-19 17:36:00 -08003828 setTextViewColorSecondary(contentView, R.id.chronometer);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003829 } else {
3830 contentView.setViewVisibility(R.id.time, View.VISIBLE);
3831 contentView.setLong(R.id.time, "setTime", mN.when);
Selim Cinek7b9605b2017-01-19 17:36:00 -08003832 setTextViewColorSecondary(contentView, R.id.time);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003833 }
Selim Cinekb85f36fd2016-04-20 18:46:36 -07003834 } else {
3835 // We still want a time to be set but gone, such that we can show and hide it
3836 // on demand in case it's a child notification without anything in the header
3837 contentView.setLong(R.id.time, "setTime", mN.when != 0 ? mN.when : mN.creationTime);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003838 }
3839 }
3840
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07003841 private void bindHeaderText(RemoteViews contentView) {
3842 CharSequence headerText = mN.extras.getCharSequence(EXTRA_SUB_TEXT);
3843 if (headerText == null && mStyle != null && mStyle.mSummaryTextSet
Selim Cinek03d0d652015-11-13 13:18:09 -05003844 && mStyle.hasSummaryInHeader()) {
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07003845 headerText = mStyle.mSummaryText;
Selim Cinek03d0d652015-11-13 13:18:09 -05003846 }
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07003847 if (headerText == null
3848 && mContext.getApplicationInfo().targetSdkVersion < Build.VERSION_CODES.N
3849 && mN.extras.getCharSequence(EXTRA_INFO_TEXT) != null) {
3850 headerText = mN.extras.getCharSequence(EXTRA_INFO_TEXT);
3851 }
3852 if (headerText != null) {
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003853 // TODO: Remove the span entirely to only have the string with propper formating.
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07003854 contentView.setTextViewText(R.id.header_text, processLegacyText(headerText));
Selim Cinek7b9605b2017-01-19 17:36:00 -08003855 setTextViewColorSecondary(contentView, R.id.header_text);
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07003856 contentView.setViewVisibility(R.id.header_text, View.VISIBLE);
3857 contentView.setViewVisibility(R.id.header_text_divider, View.VISIBLE);
Selim Cinek7b9605b2017-01-19 17:36:00 -08003858 setTextViewColorSecondary(contentView, R.id.header_text_divider);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003859 }
3860 }
3861
Adrian Rooseba05822016-04-22 17:09:27 -07003862 /**
3863 * @hide
3864 */
3865 public String loadHeaderAppName() {
Dan Sandler732bd6c2016-04-12 14:20:32 -04003866 CharSequence name = null;
3867 final PackageManager pm = mContext.getPackageManager();
3868 if (mN.extras.containsKey(EXTRA_SUBSTITUTE_APP_NAME)) {
3869 // only system packages which lump together a bunch of unrelated stuff
3870 // may substitute a different name to make the purpose of the
3871 // notification more clear. the correct package label should always
3872 // be accessible via SystemUI.
3873 final String pkg = mContext.getPackageName();
3874 final String subName = mN.extras.getString(EXTRA_SUBSTITUTE_APP_NAME);
3875 if (PackageManager.PERMISSION_GRANTED == pm.checkPermission(
3876 android.Manifest.permission.SUBSTITUTE_NOTIFICATION_APP_NAME, pkg)) {
3877 name = subName;
3878 } else {
3879 Log.w(TAG, "warning: pkg "
3880 + pkg + " attempting to substitute app name '" + subName
3881 + "' without holding perm "
3882 + android.Manifest.permission.SUBSTITUTE_NOTIFICATION_APP_NAME);
3883 }
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003884 }
Dan Sandler732bd6c2016-04-12 14:20:32 -04003885 if (TextUtils.isEmpty(name)) {
3886 name = pm.getApplicationLabel(mContext.getApplicationInfo());
3887 }
3888 if (TextUtils.isEmpty(name)) {
3889 // still nothing?
3890 return null;
3891 }
3892
3893 return String.valueOf(name);
3894 }
Adrian Roos487374f2017-01-11 15:48:14 -08003895 private void bindHeaderAppName(RemoteViews contentView, boolean ambient) {
Dan Sandler732bd6c2016-04-12 14:20:32 -04003896 contentView.setTextViewText(R.id.app_name_text, loadHeaderAppName());
Adrian Roos72171622017-01-27 10:32:06 -08003897 if (isColorized() && !ambient) {
Selim Cinek7b9605b2017-01-19 17:36:00 -08003898 setTextViewColorPrimary(contentView, R.id.app_name_text);
3899 } else {
3900 contentView.setTextColor(R.id.app_name_text,
3901 ambient ? resolveAmbientColor() : resolveContrastColor());
3902 }
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003903 }
3904
Adrian Roos487374f2017-01-11 15:48:14 -08003905 private void bindSmallIcon(RemoteViews contentView, boolean ambient) {
Selim Cinek279fa862016-06-14 10:57:25 -07003906 if (mN.mSmallIcon == null && mN.icon != 0) {
3907 mN.mSmallIcon = Icon.createWithResource(mContext, mN.icon);
3908 }
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003909 contentView.setImageViewIcon(R.id.icon, mN.mSmallIcon);
Adrian Roos9b45a152016-06-28 13:32:29 -07003910 contentView.setDrawableParameters(R.id.icon, false /* targetBackground */,
3911 -1 /* alpha */, -1 /* colorFilter */, null /* mode */, mN.iconLevel);
Adrian Roos487374f2017-01-11 15:48:14 -08003912 processSmallIconColor(mN.mSmallIcon, contentView, ambient);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003913 }
3914
Jorim Jaggi445d3c02014-08-19 22:33:42 +02003915 /**
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02003916 * @return true if the built notification will show the time or the chronometer; false
3917 * otherwise
3918 */
3919 private boolean showsTimeOrChronometer() {
Selim Cinekc2c0b042016-05-18 17:13:46 -07003920 return mN.showsTime() || mN.showsChronometer();
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02003921 }
3922
Christoph Studerfe718432014-09-01 18:21:18 +02003923 private void resetStandardTemplateWithActions(RemoteViews big) {
Adrian Roos4c1fcc82016-03-31 14:39:39 -07003924 // actions_container is only reset when there are no actions to avoid focus issues with
3925 // remote inputs.
Christoph Studerfe718432014-09-01 18:21:18 +02003926 big.setViewVisibility(R.id.actions, View.GONE);
Christoph Studerfe718432014-09-01 18:21:18 +02003927 big.removeAllViews(R.id.actions);
Adrian Roose458aa82015-12-08 16:17:19 -08003928
3929 big.setViewVisibility(R.id.notification_material_reply_container, View.GONE);
3930 big.setTextViewText(R.id.notification_material_reply_text_1, null);
3931
3932 big.setViewVisibility(R.id.notification_material_reply_text_2, View.GONE);
3933 big.setTextViewText(R.id.notification_material_reply_text_2, null);
3934 big.setViewVisibility(R.id.notification_material_reply_text_3, View.GONE);
3935 big.setTextViewText(R.id.notification_material_reply_text_3, null);
Adrian Roosf852a422016-06-03 13:33:43 -07003936
3937 big.setViewLayoutMarginBottomDimen(R.id.notification_action_list_margin_target, 0);
Christoph Studerfe718432014-09-01 18:21:18 +02003938 }
3939
Daniel Sandler96fd7c12012-03-30 16:37:36 -04003940 private RemoteViews applyStandardTemplateWithActions(int layoutId) {
Adrian Roos70d7aa32017-01-11 15:39:06 -08003941 return applyStandardTemplateWithActions(layoutId, mParams.reset().fillTextsFrom(this));
Adrian Roos48d746a2016-04-12 14:57:28 -07003942 }
3943
Adrian Roos70d7aa32017-01-11 15:39:06 -08003944 private RemoteViews applyStandardTemplateWithActions(int layoutId,
3945 StandardTemplateParams p) {
3946 RemoteViews big = applyStandardTemplate(layoutId, p);
Daniel Sandler96fd7c12012-03-30 16:37:36 -04003947
Christoph Studerfe718432014-09-01 18:21:18 +02003948 resetStandardTemplateWithActions(big);
3949
Adrian Roose458aa82015-12-08 16:17:19 -08003950 boolean validRemoteInput = false;
3951
Daniel Sandler96fd7c12012-03-30 16:37:36 -04003952 int N = mActions.size();
Adrian Roos487374f2017-01-11 15:48:14 -08003953 boolean emphazisedMode = mN.fullScreenIntent != null && !p.ambient;
Selim Cinek06e9e1f2016-07-08 17:14:16 -07003954 big.setBoolean(R.id.actions, "setEmphasizedMode", emphazisedMode);
Daniel Sandler96fd7c12012-03-30 16:37:36 -04003955 if (N > 0) {
Adrian Roos7052de52016-03-03 15:53:34 -08003956 big.setViewVisibility(R.id.actions_container, View.VISIBLE);
Daniel Sandler96fd7c12012-03-30 16:37:36 -04003957 big.setViewVisibility(R.id.actions, View.VISIBLE);
Adrian Roos487374f2017-01-11 15:48:14 -08003958 if (p.ambient) {
3959 big.setInt(R.id.actions, "setBackgroundColor", Color.TRANSPARENT);
Selim Cinek7b9605b2017-01-19 17:36:00 -08003960 } else if (isColorized()) {
3961 big.setInt(R.id.actions, "setBackgroundColor", getActionBarColor());
3962 } else {
3963 big.setInt(R.id.actions, "setBackgroundColor", mContext.getColor(
3964 R.color.notification_action_list));
Adrian Roos487374f2017-01-11 15:48:14 -08003965 }
Adrian Roosf852a422016-06-03 13:33:43 -07003966 big.setViewLayoutMarginBottomDimen(R.id.notification_action_list_margin_target,
3967 R.dimen.notification_action_list_height);
Daniel Sandler8680bf82012-05-15 16:52:52 -04003968 if (N>MAX_ACTION_BUTTONS) N=MAX_ACTION_BUTTONS;
Daniel Sandler96fd7c12012-03-30 16:37:36 -04003969 for (int i=0; i<N; i++) {
Adrian Roose458aa82015-12-08 16:17:19 -08003970 Action action = mActions.get(i);
3971 validRemoteInput |= hasValidRemoteInput(action);
3972
Selim Cinek06e9e1f2016-07-08 17:14:16 -07003973 final RemoteViews button = generateActionButton(action, emphazisedMode,
Adrian Roos487374f2017-01-11 15:48:14 -08003974 i % 2 != 0, p.ambient);
Daniel Sandler96fd7c12012-03-30 16:37:36 -04003975 big.addView(R.id.actions, button);
3976 }
Adrian Roos4c1fcc82016-03-31 14:39:39 -07003977 } else {
3978 big.setViewVisibility(R.id.actions_container, View.GONE);
Daniel Sandler96fd7c12012-03-30 16:37:36 -04003979 }
Adrian Roose458aa82015-12-08 16:17:19 -08003980
3981 CharSequence[] replyText = mN.extras.getCharSequenceArray(EXTRA_REMOTE_INPUT_HISTORY);
Adrian Roos487374f2017-01-11 15:48:14 -08003982 if (!p.ambient && validRemoteInput && replyText != null
Adrian Roose458aa82015-12-08 16:17:19 -08003983 && replyText.length > 0 && !TextUtils.isEmpty(replyText[0])) {
3984 big.setViewVisibility(R.id.notification_material_reply_container, View.VISIBLE);
3985 big.setTextViewText(R.id.notification_material_reply_text_1, replyText[0]);
Selim Cinek7b9605b2017-01-19 17:36:00 -08003986 setTextViewColorSecondary(big, R.id.notification_material_reply_text_1);
Adrian Roose458aa82015-12-08 16:17:19 -08003987
3988 if (replyText.length > 1 && !TextUtils.isEmpty(replyText[1])) {
3989 big.setViewVisibility(R.id.notification_material_reply_text_2, View.VISIBLE);
3990 big.setTextViewText(R.id.notification_material_reply_text_2, replyText[1]);
Selim Cinek7b9605b2017-01-19 17:36:00 -08003991 setTextViewColorSecondary(big, R.id.notification_material_reply_text_2);
Adrian Roose458aa82015-12-08 16:17:19 -08003992
3993 if (replyText.length > 2 && !TextUtils.isEmpty(replyText[2])) {
3994 big.setViewVisibility(
3995 R.id.notification_material_reply_text_3, View.VISIBLE);
3996 big.setTextViewText(R.id.notification_material_reply_text_3, replyText[2]);
Selim Cinek7b9605b2017-01-19 17:36:00 -08003997 setTextViewColorSecondary(big, R.id.notification_material_reply_text_3);
Adrian Roose458aa82015-12-08 16:17:19 -08003998 }
3999 }
4000 }
4001
Daniel Sandler96fd7c12012-03-30 16:37:36 -04004002 return big;
4003 }
4004
Adrian Roose458aa82015-12-08 16:17:19 -08004005 private boolean hasValidRemoteInput(Action action) {
4006 if (TextUtils.isEmpty(action.title) || action.actionIntent == null) {
4007 // Weird actions
4008 return false;
4009 }
4010
4011 RemoteInput[] remoteInputs = action.getRemoteInputs();
4012 if (remoteInputs == null) {
4013 return false;
4014 }
4015
4016 for (RemoteInput r : remoteInputs) {
4017 CharSequence[] choices = r.getChoices();
4018 if (r.getAllowFreeFormInput() || (choices != null && choices.length != 0)) {
4019 return true;
4020 }
4021 }
4022 return false;
4023 }
4024
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004025 /**
4026 * Construct a RemoteViews for the final 1U notification layout. In order:
4027 * 1. Custom contentView from the caller
4028 * 2. Style's proposed content view
4029 * 3. Standard template view
4030 */
Julia Reynolds3b848122016-02-26 10:45:32 -05004031 public RemoteViews createContentView() {
Selim Cinek7d1009b2017-01-25 15:28:28 -08004032 return createContentView(false /* increasedheight */ );
4033 }
4034
4035 /**
4036 * Construct a RemoteViews for the smaller content view.
4037 *
4038 * @param increasedHeight true if this layout be created with an increased height. Some
4039 * styles may support showing more then just that basic 1U size
4040 * and the system may decide to render important notifications
4041 * slightly bigger even when collapsed.
4042 *
4043 * @hide
4044 */
4045 public RemoteViews createContentView(boolean increasedHeight) {
Selim Cinek593610c2016-02-16 18:42:57 -08004046 if (mN.contentView != null && (mStyle == null || !mStyle.displayCustomViewInline())) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004047 return mN.contentView;
4048 } else if (mStyle != null) {
Selim Cinek7d1009b2017-01-25 15:28:28 -08004049 final RemoteViews styleView = mStyle.makeContentView(increasedHeight);
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004050 if (styleView != null) {
4051 return styleView;
4052 }
Joe Onorato46439ce2010-11-19 13:56:21 -08004053 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004054 return applyStandardTemplate(getBaseLayoutResource());
Joe Onorato46439ce2010-11-19 13:56:21 -08004055 }
4056
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004057 /**
4058 * Construct a RemoteViews for the final big notification layout.
4059 */
Julia Reynolds3b848122016-02-26 10:45:32 -05004060 public RemoteViews createBigContentView() {
Selim Cinek850a8542015-11-11 11:48:36 -05004061 RemoteViews result = null;
Selim Cinek593610c2016-02-16 18:42:57 -08004062 if (mN.bigContentView != null
4063 && (mStyle == null || !mStyle.displayCustomViewInline())) {
Julia Reynolds089e3e42015-11-18 09:59:57 -05004064 return mN.bigContentView;
4065 } else if (mStyle != null) {
Selim Cinek850a8542015-11-11 11:48:36 -05004066 result = mStyle.makeBigContentView();
Selim Cinek90dcf6d2015-11-18 20:24:13 -08004067 hideLine1Text(result);
Selim Cinekcc10bfb2016-02-10 16:24:21 -08004068 } else if (mActions.size() != 0) {
4069 result = applyStandardTemplateWithActions(getBigBaseLayoutResource());
Selim Cinek850a8542015-11-11 11:48:36 -05004070 }
Selim Cinek6743c0b2017-01-18 18:24:01 -08004071 makeHeaderExpanded(result);
Selim Cinek850a8542015-11-11 11:48:36 -05004072 return result;
4073 }
Daniel Sandler96fd7c12012-03-30 16:37:36 -04004074
Selim Cinekeaa29ca2015-11-23 13:51:13 -08004075 /**
Selim Cinek414ad332017-02-24 19:06:12 -08004076 * Construct a RemoteViews for the final notification header only. This will not be
4077 * colorized.
Selim Cinekeaa29ca2015-11-23 13:51:13 -08004078 *
4079 * @hide
4080 */
4081 public RemoteViews makeNotificationHeader() {
Selim Cinek414ad332017-02-24 19:06:12 -08004082 Boolean colorized = (Boolean) mN.extras.get(EXTRA_COLORIZED);
4083 mN.extras.putBoolean(EXTRA_COLORIZED, false);
Selim Cinekeaa29ca2015-11-23 13:51:13 -08004084 RemoteViews header = new BuilderRemoteViews(mContext.getApplicationInfo(),
4085 R.layout.notification_template_header);
4086 resetNotificationHeader(header);
Adrian Roos487374f2017-01-11 15:48:14 -08004087 bindNotificationHeader(header, false /* ambient */);
Selim Cinek414ad332017-02-24 19:06:12 -08004088 if (colorized != null) {
4089 mN.extras.putBoolean(EXTRA_COLORIZED, colorized);
4090 } else {
4091 mN.extras.remove(EXTRA_COLORIZED);
4092 }
Selim Cinekeaa29ca2015-11-23 13:51:13 -08004093 return header;
4094 }
4095
Adrian Roos487374f2017-01-11 15:48:14 -08004096 /**
4097 * Construct a RemoteViews for the ambient version of the notification.
4098 *
4099 * @hide
4100 */
4101 public RemoteViews makeAmbientNotification() {
4102 RemoteViews ambient = applyStandardTemplateWithActions(
4103 R.layout.notification_template_material_ambient,
Adrian Roos0bc3f6a2017-03-06 11:54:05 -08004104 mParams.reset().ambient(true).fillTextsFrom(this).hasProgress(false));
Adrian Roos487374f2017-01-11 15:48:14 -08004105 return ambient;
4106 }
4107
Selim Cinek29603462015-11-17 19:04:39 -08004108 private void hideLine1Text(RemoteViews result) {
Selim Cinekcc10bfb2016-02-10 16:24:21 -08004109 if (result != null) {
4110 result.setViewVisibility(R.id.text_line_1, View.GONE);
4111 }
Selim Cinek29603462015-11-17 19:04:39 -08004112 }
4113
Selim Cinek6743c0b2017-01-18 18:24:01 -08004114 /**
4115 * Adapt the Notification header if this view is used as an expanded view.
4116 *
4117 * @hide
4118 */
4119 public static void makeHeaderExpanded(RemoteViews result) {
Selim Cinekcc10bfb2016-02-10 16:24:21 -08004120 if (result != null) {
4121 result.setBoolean(R.id.notification_header, "setExpanded", true);
4122 }
Daniel Sandler96fd7c12012-03-30 16:37:36 -04004123 }
4124
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004125 /**
4126 * Construct a RemoteViews for the final heads-up notification layout.
Selim Cinek87ed69b2017-02-09 15:59:43 -08004127 *
4128 * @param increasedHeight true if this layout be created with an increased height. Some
4129 * styles may support showing more then just that basic 1U size
4130 * and the system may decide to render important notifications
4131 * slightly bigger even when collapsed.
4132 *
4133 * @hide
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004134 */
Selim Cinek87ed69b2017-02-09 15:59:43 -08004135 public RemoteViews createHeadsUpContentView(boolean increasedHeight) {
Selim Cinek593610c2016-02-16 18:42:57 -08004136 if (mN.headsUpContentView != null
4137 && (mStyle == null || !mStyle.displayCustomViewInline())) {
Julia Reynolds089e3e42015-11-18 09:59:57 -05004138 return mN.headsUpContentView;
4139 } else if (mStyle != null) {
Selim Cinek87ed69b2017-02-09 15:59:43 -08004140 final RemoteViews styleView = mStyle.makeHeadsUpContentView(increasedHeight);
4141 if (styleView != null) {
4142 return styleView;
4143 }
Julia Reynolds089e3e42015-11-18 09:59:57 -05004144 } else if (mActions.size() == 0) {
4145 return null;
4146 }
4147
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01004148 return applyStandardTemplateWithActions(getBigBaseLayoutResource());
Chris Wren8fd39ec2014-02-27 17:43:26 -05004149 }
4150
Selim Cinek624c02db2015-12-14 21:00:02 -08004151 /**
Selim Cinek87ed69b2017-02-09 15:59:43 -08004152 * Construct a RemoteViews for the final heads-up notification layout.
4153 */
4154 public RemoteViews createHeadsUpContentView() {
4155 return createHeadsUpContentView(false /* useIncreasedHeight */);
4156 }
4157
4158 /**
Selim Cinek624c02db2015-12-14 21:00:02 -08004159 * Construct a RemoteViews for the display in public contexts like on the lockscreen.
4160 *
4161 * @hide
4162 */
4163 public RemoteViews makePublicContentView() {
4164 if (mN.publicVersion != null) {
4165 final Builder builder = recoverBuilder(mContext, mN.publicVersion);
Julia Reynolds3b848122016-02-26 10:45:32 -05004166 return builder.createContentView();
Selim Cinek624c02db2015-12-14 21:00:02 -08004167 }
4168 Bundle savedBundle = mN.extras;
4169 Style style = mStyle;
4170 mStyle = null;
4171 Icon largeIcon = mN.mLargeIcon;
4172 mN.mLargeIcon = null;
Selim Cinek279fa862016-06-14 10:57:25 -07004173 Bitmap largeIconLegacy = mN.largeIcon;
4174 mN.largeIcon = null;
Selim Cinek624c02db2015-12-14 21:00:02 -08004175 Bundle publicExtras = new Bundle();
4176 publicExtras.putBoolean(EXTRA_SHOW_WHEN,
4177 savedBundle.getBoolean(EXTRA_SHOW_WHEN));
4178 publicExtras.putBoolean(EXTRA_SHOW_CHRONOMETER,
4179 savedBundle.getBoolean(EXTRA_SHOW_CHRONOMETER));
Adrian Roos96b7e202016-05-17 13:50:38 -07004180 publicExtras.putBoolean(EXTRA_CHRONOMETER_COUNT_DOWN,
4181 savedBundle.getBoolean(EXTRA_CHRONOMETER_COUNT_DOWN));
Selim Cinek624c02db2015-12-14 21:00:02 -08004182 publicExtras.putCharSequence(EXTRA_TITLE,
4183 mContext.getString(R.string.notification_hidden_text));
4184 mN.extras = publicExtras;
4185 final RemoteViews publicView = applyStandardTemplate(getBaseLayoutResource());
4186 mN.extras = savedBundle;
4187 mN.mLargeIcon = largeIcon;
Selim Cinek279fa862016-06-14 10:57:25 -07004188 mN.largeIcon = largeIconLegacy;
Selim Cinek624c02db2015-12-14 21:00:02 -08004189 mStyle = style;
4190 return publicView;
4191 }
4192
Selim Cinek6743c0b2017-01-18 18:24:01 -08004193 /**
4194 * Construct a content view for the display when low - priority
4195 *
4196 * @param useRegularSubtext uses the normal subtext set if there is one available. Otherwise
4197 * a new subtext is created consisting of the content of the
4198 * notification.
4199 * @hide
4200 */
4201 public RemoteViews makeLowPriorityContentView(boolean useRegularSubtext) {
4202 int color = mN.color;
4203 mN.color = COLOR_DEFAULT;
4204 CharSequence summary = mN.extras.getCharSequence(EXTRA_SUB_TEXT);
4205 if (!useRegularSubtext || TextUtils.isEmpty(summary)) {
4206 CharSequence newSummary = createSummaryText();
4207 if (!TextUtils.isEmpty(newSummary)) {
4208 mN.extras.putCharSequence(EXTRA_SUB_TEXT, newSummary);
4209 }
4210 }
Selim Cinek875ba9b2017-02-13 16:20:17 -08004211
Selim Cinek6743c0b2017-01-18 18:24:01 -08004212 RemoteViews header = makeNotificationHeader();
Selim Cinek1b554392017-02-28 17:22:49 -08004213 header.setBoolean(R.id.notification_header, "setAcceptAllTouches", true);
Selim Cinek6743c0b2017-01-18 18:24:01 -08004214 if (summary != null) {
4215 mN.extras.putCharSequence(EXTRA_SUB_TEXT, summary);
4216 } else {
4217 mN.extras.remove(EXTRA_SUB_TEXT);
4218 }
4219 mN.color = color;
4220 return header;
4221 }
Selim Cinek624c02db2015-12-14 21:00:02 -08004222
Selim Cinek6743c0b2017-01-18 18:24:01 -08004223 private CharSequence createSummaryText() {
4224 CharSequence titleText = mN.extras.getCharSequence(Notification.EXTRA_TITLE);
4225 if (USE_ONLY_TITLE_IN_LOW_PRIORITY_SUMMARY) {
4226 return titleText;
4227 }
4228 SpannableStringBuilder summary = new SpannableStringBuilder();
4229 if (titleText == null) {
4230 titleText = mN.extras.getCharSequence(Notification.EXTRA_TITLE_BIG);
4231 }
4232 BidiFormatter bidi = BidiFormatter.getInstance();
4233 if (titleText != null) {
4234 summary.append(bidi.unicodeWrap(titleText));
4235 }
4236 CharSequence contentText = mN.extras.getCharSequence(Notification.EXTRA_TEXT);
4237 if (titleText != null && contentText != null) {
4238 summary.append(bidi.unicodeWrap(mContext.getText(
4239 R.string.notification_header_divider_symbol_with_spaces)));
4240 }
4241 if (contentText != null) {
4242 summary.append(bidi.unicodeWrap(contentText));
4243 }
4244 return summary;
4245 }
Chris Wren8fd39ec2014-02-27 17:43:26 -05004246
Selim Cinek06e9e1f2016-07-08 17:14:16 -07004247 private RemoteViews generateActionButton(Action action, boolean emphazisedMode,
Adrian Roos487374f2017-01-11 15:48:14 -08004248 boolean oddAction, boolean ambient) {
Daniel Sandler8680bf82012-05-15 16:52:52 -04004249 final boolean tombstone = (action.actionIntent == null);
Selim Cinekf33b1112015-07-15 17:45:11 -07004250 RemoteViews button = new BuilderRemoteViews(mContext.getApplicationInfo(),
Selim Cinek06e9e1f2016-07-08 17:14:16 -07004251 emphazisedMode ? getEmphasizedActionLayoutResource()
4252 : tombstone ? getActionTombstoneLayoutResource()
4253 : getActionLayoutResource());
Daniel Sandler8680bf82012-05-15 16:52:52 -04004254 if (!tombstone) {
Daniel Sandlere5518842012-05-10 16:20:40 -04004255 button.setOnClickPendingIntent(R.id.action0, action.actionIntent);
Daniel Sandlere5518842012-05-10 16:20:40 -04004256 }
Daniel Sandler96fd7c12012-03-30 16:37:36 -04004257 button.setContentDescription(R.id.action0, action.title);
Adrian Roosfe84e1f2015-11-04 15:55:39 -08004258 if (action.mRemoteInputs != null) {
4259 button.setRemoteInputs(R.id.action0, action.mRemoteInputs);
4260 }
Selim Cinek7b9605b2017-01-19 17:36:00 -08004261 // TODO: handle emphasized mode / actions right
Selim Cinek06e9e1f2016-07-08 17:14:16 -07004262 if (emphazisedMode) {
Selim Cinek981962e2016-07-20 20:41:58 -07004263 // change the background bgColor
4264 int bgColor = mContext.getColor(oddAction ? R.color.notification_action_list
4265 : R.color.notification_action_list_dark);
4266 button.setDrawableParameters(R.id.button_holder, true, -1, bgColor,
Selim Cinek06e9e1f2016-07-08 17:14:16 -07004267 PorterDuff.Mode.SRC_ATOP, -1);
Selim Cinek981962e2016-07-20 20:41:58 -07004268 CharSequence title = action.title;
4269 ColorStateList[] outResultColor = null;
4270 if (isLegacy()) {
4271 title = clearColorSpans(title);
4272 } else {
4273 outResultColor = new ColorStateList[1];
4274 title = ensureColorSpanContrast(title, bgColor, outResultColor);
4275 }
4276 button.setTextViewText(R.id.action0, title);
Selim Cinek7b9605b2017-01-19 17:36:00 -08004277 setTextViewColorPrimary(button, R.id.action0);
Selim Cinek981962e2016-07-20 20:41:58 -07004278 if (outResultColor != null && outResultColor[0] != null) {
4279 // We need to set the text color as well since changing a text to uppercase
4280 // clears its spans.
4281 button.setTextColor(R.id.action0, outResultColor[0]);
Selim Cinek7b9605b2017-01-19 17:36:00 -08004282 } else if (mN.color != COLOR_DEFAULT && !isColorized()) {
Selim Cinek981962e2016-07-20 20:41:58 -07004283 button.setTextColor(R.id.action0,resolveContrastColor());
4284 }
Selim Cinek06e9e1f2016-07-08 17:14:16 -07004285 } else {
Selim Cinek981962e2016-07-20 20:41:58 -07004286 button.setTextViewText(R.id.action0, processLegacyText(action.title));
Adrian Roos72171622017-01-27 10:32:06 -08004287 if (isColorized() && !ambient) {
Selim Cinek7b9605b2017-01-19 17:36:00 -08004288 setTextViewColorPrimary(button, R.id.action0);
4289 } else if (mN.color != COLOR_DEFAULT) {
Adrian Roos487374f2017-01-11 15:48:14 -08004290 button.setTextColor(R.id.action0,
4291 ambient ? resolveAmbientColor() : resolveContrastColor());
Selim Cinek06e9e1f2016-07-08 17:14:16 -07004292 }
Adrian Roosfe84e1f2015-11-04 15:55:39 -08004293 }
Daniel Sandler96fd7c12012-03-30 16:37:36 -04004294 return button;
4295 }
4296
Joe Onoratocb109a02011-01-18 17:57:41 -08004297 /**
Selim Cinek981962e2016-07-20 20:41:58 -07004298 * Clears all color spans of a text
4299 * @param charSequence the input text
4300 * @return the same text but without color spans
4301 */
4302 private CharSequence clearColorSpans(CharSequence charSequence) {
4303 if (charSequence instanceof Spanned) {
4304 Spanned ss = (Spanned) charSequence;
4305 Object[] spans = ss.getSpans(0, ss.length(), Object.class);
4306 SpannableStringBuilder builder = new SpannableStringBuilder(ss.toString());
4307 for (Object span : spans) {
4308 Object resultSpan = span;
4309 if (resultSpan instanceof CharacterStyle) {
4310 resultSpan = ((CharacterStyle) span).getUnderlying();
4311 }
4312 if (resultSpan instanceof TextAppearanceSpan) {
4313 TextAppearanceSpan originalSpan = (TextAppearanceSpan) resultSpan;
4314 if (originalSpan.getTextColor() != null) {
4315 resultSpan = new TextAppearanceSpan(
4316 originalSpan.getFamily(),
4317 originalSpan.getTextStyle(),
4318 originalSpan.getTextSize(),
4319 null,
4320 originalSpan.getLinkTextColor());
4321 }
4322 } else if (resultSpan instanceof ForegroundColorSpan
4323 || (resultSpan instanceof BackgroundColorSpan)) {
4324 continue;
4325 } else {
4326 resultSpan = span;
4327 }
4328 builder.setSpan(resultSpan, ss.getSpanStart(span), ss.getSpanEnd(span),
4329 ss.getSpanFlags(span));
4330 }
4331 return builder;
4332 }
4333 return charSequence;
4334 }
4335
4336 /**
4337 * Ensures contrast on color spans against a background color. also returns the color of the
4338 * text if a span was found that spans over the whole text.
4339 *
4340 * @param charSequence the charSequence on which the spans are
4341 * @param background the background color to ensure the contrast against
4342 * @param outResultColor an array in which a color will be returned as the first element if
4343 * there exists a full length color span.
4344 * @return the contrasted charSequence
4345 */
4346 private CharSequence ensureColorSpanContrast(CharSequence charSequence, int background,
4347 ColorStateList[] outResultColor) {
4348 if (charSequence instanceof Spanned) {
4349 Spanned ss = (Spanned) charSequence;
4350 Object[] spans = ss.getSpans(0, ss.length(), Object.class);
4351 SpannableStringBuilder builder = new SpannableStringBuilder(ss.toString());
4352 for (Object span : spans) {
4353 Object resultSpan = span;
4354 int spanStart = ss.getSpanStart(span);
4355 int spanEnd = ss.getSpanEnd(span);
4356 boolean fullLength = (spanEnd - spanStart) == charSequence.length();
4357 if (resultSpan instanceof CharacterStyle) {
4358 resultSpan = ((CharacterStyle) span).getUnderlying();
4359 }
4360 if (resultSpan instanceof TextAppearanceSpan) {
4361 TextAppearanceSpan originalSpan = (TextAppearanceSpan) resultSpan;
4362 ColorStateList textColor = originalSpan.getTextColor();
4363 if (textColor != null) {
4364 int[] colors = textColor.getColors();
4365 int[] newColors = new int[colors.length];
4366 for (int i = 0; i < newColors.length; i++) {
4367 newColors[i] = NotificationColorUtil.ensureLargeTextContrast(
4368 colors[i], background);
4369 }
4370 textColor = new ColorStateList(textColor.getStates().clone(),
4371 newColors);
4372 resultSpan = new TextAppearanceSpan(
4373 originalSpan.getFamily(),
4374 originalSpan.getTextStyle(),
4375 originalSpan.getTextSize(),
4376 textColor,
4377 originalSpan.getLinkTextColor());
4378 if (fullLength) {
4379 outResultColor[0] = new ColorStateList(
4380 textColor.getStates().clone(), newColors);
4381 }
4382 }
4383 } else if (resultSpan instanceof ForegroundColorSpan) {
4384 ForegroundColorSpan originalSpan = (ForegroundColorSpan) resultSpan;
4385 int foregroundColor = originalSpan.getForegroundColor();
4386 foregroundColor = NotificationColorUtil.ensureLargeTextContrast(
4387 foregroundColor, background);
4388 resultSpan = new ForegroundColorSpan(foregroundColor);
4389 if (fullLength) {
4390 outResultColor[0] = ColorStateList.valueOf(foregroundColor);
4391 }
4392 } else {
4393 resultSpan = span;
4394 }
4395
4396 builder.setSpan(resultSpan, spanStart, spanEnd, ss.getSpanFlags(span));
4397 }
4398 return builder;
4399 }
4400 return charSequence;
4401 }
4402
4403 /**
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01004404 * @return Whether we are currently building a notification from a legacy (an app that
Alan Viverette3cb07a462014-06-06 14:19:53 -07004405 * doesn't create material notifications by itself) app.
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01004406 */
4407 private boolean isLegacy() {
Selim Cinek7b9605b2017-01-19 17:36:00 -08004408 if (!mIsLegacyInitialized) {
4409 mIsLegacy = mContext.getApplicationInfo().targetSdkVersion
4410 < Build.VERSION_CODES.LOLLIPOP;
4411 mIsLegacyInitialized = true;
4412 }
4413 return mIsLegacy;
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01004414 }
4415
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01004416 private CharSequence processLegacyText(CharSequence charSequence) {
Adrian Roos0bc3f6a2017-03-06 11:54:05 -08004417 return processLegacyText(charSequence, false /* ambient */);
4418 }
4419
4420 private CharSequence processLegacyText(CharSequence charSequence, boolean ambient) {
4421 boolean isAlreadyLightText = isLegacy() || textColorsNeedInversion();
4422 boolean wantLightText = ambient;
4423 if (isAlreadyLightText != wantLightText) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004424 return getColorUtil().invertCharSequenceColors(charSequence);
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01004425 } else {
4426 return charSequence;
4427 }
4428 }
4429
Dan Sandler26e81cf2014-05-06 10:01:27 -04004430 /**
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004431 * Apply any necessariy colors to the small icon
Dan Sandler26e81cf2014-05-06 10:01:27 -04004432 */
Adrian Roos487374f2017-01-11 15:48:14 -08004433 private void processSmallIconColor(Icon smallIcon, RemoteViews contentView,
4434 boolean ambient) {
Selim Cinekea4bef72015-12-02 15:51:10 -08004435 boolean colorable = !isLegacy() || getColorUtil().isGrayscaleIcon(mContext, smallIcon);
Selim Cinek99104832017-01-25 14:47:33 -08004436 int color = ambient ? resolveAmbientColor() : getPrimaryHighlightColor();
Selim Cinekea4bef72015-12-02 15:51:10 -08004437 if (colorable) {
Adrian Roos487374f2017-01-11 15:48:14 -08004438 contentView.setDrawableParameters(R.id.icon, false, -1, color,
Jorim Jaggi92df1f22014-12-16 19:44:41 +01004439 PorterDuff.Mode.SRC_ATOP, -1);
Selim Cinekea4bef72015-12-02 15:51:10 -08004440
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01004441 }
Selim Cinekea4bef72015-12-02 15:51:10 -08004442 contentView.setInt(R.id.notification_header, "setOriginalIconColor",
Adrian Roos487374f2017-01-11 15:48:14 -08004443 colorable ? color : NotificationHeaderView.NO_COLOR);
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01004444 }
4445
Dan Sandler26e81cf2014-05-06 10:01:27 -04004446 /**
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004447 * Make the largeIcon dark if it's a fake smallIcon (that is,
Dan Sandler26e81cf2014-05-06 10:01:27 -04004448 * if it's grayscale).
4449 */
4450 // TODO: also check bounds, transparency, that sort of thing.
Dan Sandlerd63f9322015-05-06 15:18:49 -04004451 private void processLargeLegacyIcon(Icon largeIcon, RemoteViews contentView) {
4452 if (largeIcon != null && isLegacy()
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004453 && getColorUtil().isGrayscaleIcon(mContext, largeIcon)) {
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004454 // resolve color will fall back to the default when legacy
Adrian Roos4ff3b122016-02-01 12:26:13 -08004455 contentView.setDrawableParameters(R.id.icon, false, -1, resolveContrastColor(),
Dan Sandler190d58d2014-05-15 09:33:39 -04004456 PorterDuff.Mode.SRC_ATOP, -1);
Jorim Jaggi92df1f22014-12-16 19:44:41 +01004457 }
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01004458 }
4459
Julia Reynolds10ee1fc2015-11-09 11:04:55 -05004460 private void sanitizeColor() {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004461 if (mN.color != COLOR_DEFAULT) {
4462 mN.color |= 0xFF000000; // no alpha for custom colors
Jorim Jaggi74419312014-06-10 20:57:21 +02004463 }
Jorim Jaggi74419312014-06-10 20:57:21 +02004464 }
4465
Adrian Roos4ff3b122016-02-01 12:26:13 -08004466 int resolveContrastColor() {
4467 if (mCachedContrastColorIsFor == mN.color && mCachedContrastColor != COLOR_INVALID) {
4468 return mCachedContrastColor;
Dan Sandler26e81cf2014-05-06 10:01:27 -04004469 }
Adrian Roos4ff3b122016-02-01 12:26:13 -08004470 final int contrasted = NotificationColorUtil.resolveContrastColor(mContext, mN.color);
4471
4472 mCachedContrastColorIsFor = mN.color;
4473 return mCachedContrastColor = contrasted;
Dan Sandler26e81cf2014-05-06 10:01:27 -04004474 }
4475
Adrian Roos487374f2017-01-11 15:48:14 -08004476 int resolveAmbientColor() {
4477 if (mCachedAmbientColorIsFor == mN.color && mCachedAmbientColorIsFor != COLOR_INVALID) {
4478 return mCachedAmbientColor;
4479 }
4480 final int contrasted = NotificationColorUtil.resolveAmbientColor(mContext, mN.color);
4481
4482 mCachedAmbientColorIsFor = mN.color;
4483 return mCachedAmbientColor = contrasted;
4484 }
4485
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01004486 /**
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004487 * Apply the unstyled operations and return a new {@link Notification} object.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04004488 * @hide
Joe Onoratocb109a02011-01-18 17:57:41 -08004489 */
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04004490 public Notification buildUnstyled() {
Daniel Sandlera0a938c2012-03-15 08:42:37 -04004491 if (mActions.size() > 0) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004492 mN.actions = new Action[mActions.size()];
4493 mActions.toArray(mN.actions);
Daniel Sandlera0a938c2012-03-15 08:42:37 -04004494 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004495 if (!mPersonList.isEmpty()) {
4496 mN.extras.putStringArray(EXTRA_PEOPLE,
4497 mPersonList.toArray(new String[mPersonList.size()]));
Dan Sandler0bf2ed82013-12-21 23:33:41 -06004498 }
Selim Cinek247fa012016-02-18 09:50:48 -08004499 if (mN.bigContentView != null || mN.contentView != null
4500 || mN.headsUpContentView != null) {
4501 mN.extras.putBoolean(EXTRA_CONTAINS_CUSTOM_VIEW, true);
4502 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004503 return mN;
Joe Onorato46439ce2010-11-19 13:56:21 -08004504 }
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004505
Julia Reynolds3b848122016-02-26 10:45:32 -05004506 /**
4507 * Creates a Builder from an existing notification so further changes can be made.
4508 * @param context The context for your application / activity.
4509 * @param n The notification to create a Builder from.
4510 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004511 public static Notification.Builder recoverBuilder(Context context, Notification n) {
Christoph Studer4600f9b2014-07-22 22:44:43 +02004512 // Re-create notification context so we can access app resources.
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004513 ApplicationInfo applicationInfo = n.extras.getParcelable(
4514 EXTRA_BUILDER_APPLICATION_INFO);
Christoph Studer4600f9b2014-07-22 22:44:43 +02004515 Context builderContext;
Julia Reynoldsda303542015-11-23 14:00:20 -05004516 if (applicationInfo != null) {
4517 try {
4518 builderContext = context.createApplicationContext(applicationInfo,
4519 Context.CONTEXT_RESTRICTED);
4520 } catch (NameNotFoundException e) {
4521 Log.e(TAG, "ApplicationInfo " + applicationInfo + " not found");
4522 builderContext = context; // try with our context
4523 }
4524 } else {
4525 builderContext = context; // try with given context
Christoph Studer4600f9b2014-07-22 22:44:43 +02004526 }
4527
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004528 return new Builder(builderContext, n);
Christoph Studer4600f9b2014-07-22 22:44:43 +02004529 }
4530
Daniel Sandlerf45564e2013-04-15 15:05:08 -04004531 /**
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004532 * @deprecated Use {@link #build()} instead.
4533 */
4534 @Deprecated
4535 public Notification getNotification() {
4536 return build();
4537 }
4538
4539 /**
4540 * Combine all of the options that have been set and return a new {@link Notification}
4541 * object.
4542 */
4543 public Notification build() {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004544 // first, add any extras from the calling code
4545 if (mUserExtras != null) {
4546 mN.extras = getAllExtras();
Jorim Jaggia0d58ae2015-06-03 11:48:13 -07004547 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004548
Selim Cinekb85f36fd2016-04-20 18:46:36 -07004549 mN.creationTime = System.currentTimeMillis();
4550
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004551 // lazy stuff from mContext; see comment in Builder(Context, Notification)
Julia Reynoldsda303542015-11-23 14:00:20 -05004552 Notification.addFieldsFromContext(mContext, mN);
Christoph Studer943aa672014-08-03 20:31:16 +02004553
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004554 buildUnstyled();
Daniel Sandlerf45564e2013-04-15 15:05:08 -04004555
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004556 if (mStyle != null) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004557 mStyle.buildStyled(mN);
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004558 }
Daniel Sandlerf45564e2013-04-15 15:05:08 -04004559
Adrian Roos5081c0d2016-02-26 16:04:19 -08004560 if (mContext.getApplicationInfo().targetSdkVersion < Build.VERSION_CODES.N
4561 && (mStyle == null || !mStyle.displayCustomViewInline())) {
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05004562 if (mN.contentView == null) {
Julia Reynolds3b848122016-02-26 10:45:32 -05004563 mN.contentView = createContentView();
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05004564 mN.extras.putInt(EXTRA_REBUILD_CONTENT_VIEW_ACTION_COUNT,
4565 mN.contentView.getSequenceNumber());
4566 }
4567 if (mN.bigContentView == null) {
Julia Reynolds3b848122016-02-26 10:45:32 -05004568 mN.bigContentView = createBigContentView();
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05004569 if (mN.bigContentView != null) {
4570 mN.extras.putInt(EXTRA_REBUILD_BIG_CONTENT_VIEW_ACTION_COUNT,
4571 mN.bigContentView.getSequenceNumber());
4572 }
4573 }
4574 if (mN.headsUpContentView == null) {
Julia Reynolds3b848122016-02-26 10:45:32 -05004575 mN.headsUpContentView = createHeadsUpContentView();
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05004576 if (mN.headsUpContentView != null) {
4577 mN.extras.putInt(EXTRA_REBUILD_HEADS_UP_CONTENT_VIEW_ACTION_COUNT,
4578 mN.headsUpContentView.getSequenceNumber());
4579 }
4580 }
4581 }
4582
Julia Reynolds4c0c2022016-02-02 15:11:59 -05004583 if ((mN.defaults & DEFAULT_LIGHTS) != 0) {
4584 mN.flags |= FLAG_SHOW_LIGHTS;
4585 }
4586
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004587 return mN;
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004588 }
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05004589
4590 /**
4591 * Apply this Builder to an existing {@link Notification} object.
4592 *
4593 * @hide
4594 */
4595 public Notification buildInto(Notification n) {
Daniel Sandler1a497d32013-04-18 14:52:45 -04004596 build().cloneInto(n, true);
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05004597 return n;
4598 }
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01004599
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05004600 /**
Adrian Roos184bfe022016-03-03 13:41:44 -08004601 * Removes RemoteViews that were created for compatibility from {@param n}, if they did not
4602 * change.
4603 *
4604 * @return {@param n}, if no stripping is needed, otherwise a stripped clone of {@param n}.
4605 *
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05004606 * @hide
4607 */
Adrian Roos184bfe022016-03-03 13:41:44 -08004608 public static Notification maybeCloneStrippedForDelivery(Notification n) {
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05004609 String templateClass = n.extras.getString(EXTRA_TEMPLATE);
Adrian Roos184bfe022016-03-03 13:41:44 -08004610
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05004611 // Only strip views for known Styles because we won't know how to
4612 // re-create them otherwise.
Adrian Roos184bfe022016-03-03 13:41:44 -08004613 if (!TextUtils.isEmpty(templateClass)
4614 && getNotificationStyleClass(templateClass) == null) {
4615 return n;
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05004616 }
Adrian Roos184bfe022016-03-03 13:41:44 -08004617
4618 // Only strip unmodified BuilderRemoteViews.
4619 boolean stripContentView = n.contentView instanceof BuilderRemoteViews &&
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05004620 n.extras.getInt(EXTRA_REBUILD_CONTENT_VIEW_ACTION_COUNT, -1) ==
Adrian Roos184bfe022016-03-03 13:41:44 -08004621 n.contentView.getSequenceNumber();
4622 boolean stripBigContentView = n.bigContentView instanceof BuilderRemoteViews &&
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05004623 n.extras.getInt(EXTRA_REBUILD_BIG_CONTENT_VIEW_ACTION_COUNT, -1) ==
Adrian Roos184bfe022016-03-03 13:41:44 -08004624 n.bigContentView.getSequenceNumber();
4625 boolean stripHeadsUpContentView = n.headsUpContentView instanceof BuilderRemoteViews &&
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05004626 n.extras.getInt(EXTRA_REBUILD_HEADS_UP_CONTENT_VIEW_ACTION_COUNT, -1) ==
Adrian Roos184bfe022016-03-03 13:41:44 -08004627 n.headsUpContentView.getSequenceNumber();
4628
4629 // Nothing to do here, no need to clone.
4630 if (!stripContentView && !stripBigContentView && !stripHeadsUpContentView) {
4631 return n;
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05004632 }
Adrian Roos184bfe022016-03-03 13:41:44 -08004633
4634 Notification clone = n.clone();
4635 if (stripContentView) {
4636 clone.contentView = null;
4637 clone.extras.remove(EXTRA_REBUILD_CONTENT_VIEW_ACTION_COUNT);
4638 }
4639 if (stripBigContentView) {
4640 clone.bigContentView = null;
4641 clone.extras.remove(EXTRA_REBUILD_BIG_CONTENT_VIEW_ACTION_COUNT);
4642 }
4643 if (stripHeadsUpContentView) {
4644 clone.headsUpContentView = null;
4645 clone.extras.remove(EXTRA_REBUILD_HEADS_UP_CONTENT_VIEW_ACTION_COUNT);
4646 }
4647 return clone;
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05004648 }
4649
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01004650 private int getBaseLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07004651 return R.layout.notification_template_material_base;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01004652 }
4653
4654 private int getBigBaseLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07004655 return R.layout.notification_template_material_big_base;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01004656 }
4657
4658 private int getBigPictureLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07004659 return R.layout.notification_template_material_big_picture;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01004660 }
4661
4662 private int getBigTextLayoutResource() {
Jorim Jaggi445d3c02014-08-19 22:33:42 +02004663 return R.layout.notification_template_material_big_text;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01004664 }
4665
4666 private int getInboxLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07004667 return R.layout.notification_template_material_inbox;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01004668 }
4669
Adrian Roosc1a80b02016-04-05 14:54:55 -07004670 private int getMessagingLayoutResource() {
4671 return R.layout.notification_template_material_messaging;
4672 }
4673
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01004674 private int getActionLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07004675 return R.layout.notification_material_action;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01004676 }
4677
Selim Cinek06e9e1f2016-07-08 17:14:16 -07004678 private int getEmphasizedActionLayoutResource() {
4679 return R.layout.notification_material_action_emphasized;
4680 }
4681
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01004682 private int getActionTombstoneLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07004683 return R.layout.notification_material_action_tombstone;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01004684 }
Selim Cinek7b9605b2017-01-19 17:36:00 -08004685
4686 private int getBackgroundColor() {
4687 if (isColorized()) {
4688 return mN.color;
4689 } else {
4690 return COLOR_DEFAULT;
4691 }
4692 }
4693
4694 private boolean isColorized() {
4695 return mN.isColorized();
4696 }
Selim Cinek99104832017-01-25 14:47:33 -08004697
4698 private boolean textColorsNeedInversion() {
4699 if (mStyle == null || !MediaStyle.class.equals(mStyle.getClass())) {
4700 return false;
4701 }
4702 int targetSdkVersion = mContext.getApplicationInfo().targetSdkVersion;
4703 return targetSdkVersion > Build.VERSION_CODES.M
4704 && targetSdkVersion < Build.VERSION_CODES.O;
4705 }
Selim Cinek7b9605b2017-01-19 17:36:00 -08004706 }
4707
4708 /**
Selim Cinek22714f12017-04-13 16:23:53 -07004709 * @return whether this notification is a foreground service notification
Selim Cinek7b9605b2017-01-19 17:36:00 -08004710 */
Selim Cinek22714f12017-04-13 16:23:53 -07004711 private boolean isForegroundService() {
4712 return (flags & Notification.FLAG_FOREGROUND_SERVICE) != 0;
Selim Cinek7b9605b2017-01-19 17:36:00 -08004713 }
4714
4715 /**
Selim Cinek99104832017-01-25 14:47:33 -08004716 * @return whether this notification has a media session attached
4717 * @hide
4718 */
4719 public boolean hasMediaSession() {
4720 return extras.getParcelable(Notification.EXTRA_MEDIA_SESSION) != null;
4721 }
4722
4723 /**
Selim Cinek7d1009b2017-01-25 15:28:28 -08004724 * @return the style class of this notification
4725 * @hide
4726 */
4727 public Class<? extends Notification.Style> getNotificationStyle() {
4728 String templateClass = extras.getString(Notification.EXTRA_TEMPLATE);
4729
4730 if (!TextUtils.isEmpty(templateClass)) {
4731 return Notification.getNotificationStyleClass(templateClass);
4732 }
4733 return null;
4734 }
4735
4736 /**
Selim Cinek22714f12017-04-13 16:23:53 -07004737 * @return true if this notification is colorized.
Selim Cinek7b9605b2017-01-19 17:36:00 -08004738 *
4739 * @hide
4740 */
4741 public boolean isColorized() {
Selim Cinek99104832017-01-25 14:47:33 -08004742 Class<? extends Style> style = getNotificationStyle();
4743 if (MediaStyle.class.equals(style)) {
4744 Boolean colorized = (Boolean) extras.get(EXTRA_COLORIZED);
4745 if ((colorized == null || colorized) && hasMediaSession()) {
4746 return true;
4747 }
4748 } else if (DecoratedMediaCustomViewStyle.class.equals(style)) {
4749 if (extras.getBoolean(EXTRA_COLORIZED) && hasMediaSession()) {
4750 return true;
4751 }
4752 }
Selim Cinek22714f12017-04-13 16:23:53 -07004753 return extras.getBoolean(EXTRA_COLORIZED) && isForegroundService();
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004754 }
4755
Selim Cinek279fa862016-06-14 10:57:25 -07004756 private boolean hasLargeIcon() {
4757 return mLargeIcon != null || largeIcon != null;
4758 }
4759
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004760 /**
Selim Cinekc2c0b042016-05-18 17:13:46 -07004761 * @return true if the notification will show the time; false otherwise
Selim Cinekb85f36fd2016-04-20 18:46:36 -07004762 * @hide
4763 */
Selim Cinekc2c0b042016-05-18 17:13:46 -07004764 public boolean showsTime() {
Selim Cinekb85f36fd2016-04-20 18:46:36 -07004765 return when != 0 && extras.getBoolean(EXTRA_SHOW_WHEN);
4766 }
4767
4768 /**
Selim Cinekc2c0b042016-05-18 17:13:46 -07004769 * @return true if the notification will show a chronometer; false otherwise
4770 * @hide
4771 */
4772 public boolean showsChronometer() {
4773 return when != 0 && extras.getBoolean(EXTRA_SHOW_CHRONOMETER);
4774 }
4775
4776 /**
Julia Reynolds4a02afb2016-12-13 13:39:52 -05004777 * @hide
4778 */
4779 @SystemApi
4780 public static Class<? extends Style> getNotificationStyleClass(String templateClass) {
4781 Class<? extends Style>[] classes = new Class[] {
4782 BigTextStyle.class, BigPictureStyle.class, InboxStyle.class, MediaStyle.class,
4783 DecoratedCustomViewStyle.class, DecoratedMediaCustomViewStyle.class,
4784 MessagingStyle.class };
4785 for (Class<? extends Style> innerClass : classes) {
4786 if (templateClass.equals(innerClass.getName())) {
4787 return innerClass;
4788 }
4789 }
4790 return null;
4791 }
4792
4793 /**
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004794 * An object that can apply a rich notification style to a {@link Notification.Builder}
4795 * object.
4796 */
Griff Hazendfcb0802014-02-11 12:00:00 -08004797 public static abstract class Style {
Chris Wrend6297db2012-05-03 16:20:13 -04004798 private CharSequence mBigContentTitle;
Jorim Jaggi457a10d2014-09-08 16:18:23 +02004799
4800 /**
4801 * @hide
4802 */
4803 protected CharSequence mSummaryText = null;
4804
4805 /**
4806 * @hide
4807 */
4808 protected boolean mSummaryTextSet = false;
Chris Wrend6297db2012-05-03 16:20:13 -04004809
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004810 protected Builder mBuilder;
4811
Chris Wrend6297db2012-05-03 16:20:13 -04004812 /**
4813 * Overrides ContentTitle in the big form of the template.
4814 * This defaults to the value passed to setContentTitle().
4815 */
4816 protected void internalSetBigContentTitle(CharSequence title) {
4817 mBigContentTitle = title;
4818 }
4819
4820 /**
4821 * Set the first line of text after the detail section in the big form of the template.
4822 */
4823 protected void internalSetSummaryText(CharSequence cs) {
4824 mSummaryText = cs;
Daniel Sandler619738c2012-06-07 16:33:08 -04004825 mSummaryTextSet = true;
Chris Wrend6297db2012-05-03 16:20:13 -04004826 }
4827
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004828 public void setBuilder(Builder builder) {
4829 if (mBuilder != builder) {
4830 mBuilder = builder;
Daniel Sandlerc08dea22012-06-28 08:35:24 -07004831 if (mBuilder != null) {
4832 mBuilder.setStyle(this);
4833 }
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004834 }
4835 }
4836
Chris Wrend6297db2012-05-03 16:20:13 -04004837 protected void checkBuilder() {
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004838 if (mBuilder == null) {
4839 throw new IllegalArgumentException("Style requires a valid Builder object");
4840 }
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004841 }
Chris Wrend6297db2012-05-03 16:20:13 -04004842
4843 protected RemoteViews getStandardView(int layoutId) {
4844 checkBuilder();
4845
Christoph Studer4600f9b2014-07-22 22:44:43 +02004846 // Nasty.
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004847 CharSequence oldBuilderContentTitle =
4848 mBuilder.getAllExtras().getCharSequence(EXTRA_TITLE);
Chris Wrend6297db2012-05-03 16:20:13 -04004849 if (mBigContentTitle != null) {
4850 mBuilder.setContentTitle(mBigContentTitle);
4851 }
4852
Chris Wrend6297db2012-05-03 16:20:13 -04004853 RemoteViews contentView = mBuilder.applyStandardTemplateWithActions(layoutId);
4854
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004855 mBuilder.getAllExtras().putCharSequence(EXTRA_TITLE, oldBuilderContentTitle);
Christoph Studer4600f9b2014-07-22 22:44:43 +02004856
Chris Wrend6297db2012-05-03 16:20:13 -04004857 if (mBigContentTitle != null && mBigContentTitle.equals("")) {
4858 contentView.setViewVisibility(R.id.line1, View.GONE);
Chris Wren67dc9a02012-05-16 01:03:20 -04004859 } else {
4860 contentView.setViewVisibility(R.id.line1, View.VISIBLE);
Chris Wrend6297db2012-05-03 16:20:13 -04004861 }
4862
Chris Wrend6297db2012-05-03 16:20:13 -04004863 return contentView;
4864 }
4865
Daniel Sandlerf45564e2013-04-15 15:05:08 -04004866 /**
Selim Cinek7d1009b2017-01-25 15:28:28 -08004867 * Construct a Style-specific RemoteViews for the collapsed notification layout.
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004868 * The default implementation has nothing additional to add.
Selim Cinek7d1009b2017-01-25 15:28:28 -08004869 *
4870 * @param increasedHeight true if this layout be created with an increased height.
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004871 * @hide
4872 */
Selim Cinek7d1009b2017-01-25 15:28:28 -08004873 public RemoteViews makeContentView(boolean increasedHeight) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004874 return null;
4875 }
4876
4877 /**
4878 * Construct a Style-specific RemoteViews for the final big notification layout.
4879 * @hide
4880 */
4881 public RemoteViews makeBigContentView() {
4882 return null;
4883 }
4884
4885 /**
4886 * Construct a Style-specific RemoteViews for the final HUN layout.
Selim Cinek87ed69b2017-02-09 15:59:43 -08004887 *
4888 * @param increasedHeight true if this layout be created with an increased height.
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004889 * @hide
4890 */
Selim Cinek87ed69b2017-02-09 15:59:43 -08004891 public RemoteViews makeHeadsUpContentView(boolean increasedHeight) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004892 return null;
4893 }
4894
4895 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004896 * Apply any style-specific extras to this notification before shipping it out.
Daniel Sandlerf45564e2013-04-15 15:05:08 -04004897 * @hide
4898 */
4899 public void addExtras(Bundle extras) {
4900 if (mSummaryTextSet) {
4901 extras.putCharSequence(EXTRA_SUMMARY_TEXT, mSummaryText);
4902 }
4903 if (mBigContentTitle != null) {
4904 extras.putCharSequence(EXTRA_TITLE_BIG, mBigContentTitle);
4905 }
Chris Wren91ad5632013-06-05 15:05:57 -04004906 extras.putString(EXTRA_TEMPLATE, this.getClass().getName());
Daniel Sandlerf45564e2013-04-15 15:05:08 -04004907 }
4908
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04004909 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004910 * Reconstruct the internal state of this Style object from extras.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04004911 * @hide
4912 */
Christoph Studer4600f9b2014-07-22 22:44:43 +02004913 protected void restoreFromExtras(Bundle extras) {
4914 if (extras.containsKey(EXTRA_SUMMARY_TEXT)) {
4915 mSummaryText = extras.getCharSequence(EXTRA_SUMMARY_TEXT);
4916 mSummaryTextSet = true;
4917 }
4918 if (extras.containsKey(EXTRA_TITLE_BIG)) {
4919 mBigContentTitle = extras.getCharSequence(EXTRA_TITLE_BIG);
4920 }
4921 }
4922
4923
4924 /**
4925 * @hide
4926 */
4927 public Notification buildStyled(Notification wip) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004928 addExtras(wip.extras);
Christoph Studer4600f9b2014-07-22 22:44:43 +02004929 return wip;
4930 }
4931
Daniel Sandler0ec46202015-06-24 01:27:05 -04004932 /**
4933 * @hide
4934 */
Jorim Jaggia0d58ae2015-06-03 11:48:13 -07004935 public void purgeResources() {}
4936
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04004937 /**
4938 * Calls {@link android.app.Notification.Builder#build()} on the Builder this Style is
4939 * attached to.
4940 *
4941 * @return the fully constructed Notification.
4942 */
4943 public Notification build() {
4944 checkBuilder();
4945 return mBuilder.build();
4946 }
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02004947
4948 /**
4949 * @hide
4950 * @return true if the style positions the progress bar on the second line; false if the
4951 * style hides the progress bar
4952 */
4953 protected boolean hasProgress() {
4954 return true;
4955 }
Selim Cinek03d0d652015-11-13 13:18:09 -05004956
4957 /**
4958 * @hide
4959 * @return Whether we should put the summary be put into the notification header
4960 */
4961 public boolean hasSummaryInHeader() {
4962 return true;
4963 }
Selim Cinek593610c2016-02-16 18:42:57 -08004964
4965 /**
4966 * @hide
4967 * @return Whether custom content views are displayed inline in the style
4968 */
4969 public boolean displayCustomViewInline() {
4970 return false;
4971 }
Joe Onorato46439ce2010-11-19 13:56:21 -08004972 }
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004973
4974 /**
Daniel Sandler4dfbe832012-04-11 14:51:46 -04004975 * Helper class for generating large-format notifications that include a large image attachment.
Joe Malin8d40d042012-11-05 11:36:40 -08004976 *
Robert Ly91c5ce32014-06-08 15:37:00 -07004977 * Here's how you'd set the <code>BigPictureStyle</code> on a notification:
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004978 * <pre class="prettyprint">
Robert Ly91c5ce32014-06-08 15:37:00 -07004979 * Notification notif = new Notification.Builder(mContext)
4980 * .setContentTitle(&quot;New photo from &quot; + sender.toString())
4981 * .setContentText(subject)
4982 * .setSmallIcon(R.drawable.new_post)
4983 * .setLargeIcon(aBitmap)
4984 * .setStyle(new Notification.BigPictureStyle()
4985 * .bigPicture(aBigBitmap))
4986 * .build();
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004987 * </pre>
Joe Malin8d40d042012-11-05 11:36:40 -08004988 *
Daniel Sandler4dfbe832012-04-11 14:51:46 -04004989 * @see Notification#bigContentView
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004990 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004991 public static class BigPictureStyle extends Style {
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004992 private Bitmap mPicture;
Dan Sandlerd63f9322015-05-06 15:18:49 -04004993 private Icon mBigLargeIcon;
Chris Wren3745a3d2012-05-22 15:11:52 -04004994 private boolean mBigLargeIconSet = false;
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004995
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004996 public BigPictureStyle() {
4997 }
4998
Adrian Roosf5faf9d2016-05-23 13:56:15 -07004999 /**
5000 * @deprecated use {@code BigPictureStyle()}.
5001 */
5002 @Deprecated
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005003 public BigPictureStyle(Builder builder) {
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005004 setBuilder(builder);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005005 }
5006
Chris Wrend6297db2012-05-03 16:20:13 -04005007 /**
5008 * Overrides ContentTitle in the big form of the template.
5009 * This defaults to the value passed to setContentTitle().
5010 */
5011 public BigPictureStyle setBigContentTitle(CharSequence title) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04005012 internalSetBigContentTitle(safeCharSequence(title));
Chris Wrend6297db2012-05-03 16:20:13 -04005013 return this;
5014 }
5015
5016 /**
5017 * Set the first line of text after the detail section in the big form of the template.
5018 */
5019 public BigPictureStyle setSummaryText(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04005020 internalSetSummaryText(safeCharSequence(cs));
Chris Wrend6297db2012-05-03 16:20:13 -04005021 return this;
5022 }
5023
Chris Wren0bd664d2012-08-01 13:56:56 -04005024 /**
5025 * Provide the bitmap to be used as the payload for the BigPicture notification.
5026 */
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005027 public BigPictureStyle bigPicture(Bitmap b) {
5028 mPicture = b;
5029 return this;
5030 }
5031
Chris Wren3745a3d2012-05-22 15:11:52 -04005032 /**
Chris Wren3745a3d2012-05-22 15:11:52 -04005033 * Override the large icon when the big notification is shown.
5034 */
5035 public BigPictureStyle bigLargeIcon(Bitmap b) {
Dan Sandlerd63f9322015-05-06 15:18:49 -04005036 return bigLargeIcon(b != null ? Icon.createWithBitmap(b) : null);
5037 }
5038
5039 /**
5040 * Override the large icon when the big notification is shown.
5041 */
5042 public BigPictureStyle bigLargeIcon(Icon icon) {
Chris Wren3745a3d2012-05-22 15:11:52 -04005043 mBigLargeIconSet = true;
Dan Sandlerd63f9322015-05-06 15:18:49 -04005044 mBigLargeIcon = icon;
Chris Wren3745a3d2012-05-22 15:11:52 -04005045 return this;
5046 }
5047
Riley Andrews0394a0c2015-11-03 23:36:52 -08005048 /** @hide */
5049 public static final int MIN_ASHMEM_BITMAP_SIZE = 128 * (1 << 10);
5050
Daniel Sandler0ec46202015-06-24 01:27:05 -04005051 /**
5052 * @hide
5053 */
Jorim Jaggia0d58ae2015-06-03 11:48:13 -07005054 @Override
5055 public void purgeResources() {
5056 super.purgeResources();
Riley Andrews8cee7c12015-11-01 23:36:04 -08005057 if (mPicture != null &&
5058 mPicture.isMutable() &&
Riley Andrews0394a0c2015-11-03 23:36:52 -08005059 mPicture.getAllocationByteCount() >= MIN_ASHMEM_BITMAP_SIZE) {
Jorim Jaggia0d58ae2015-06-03 11:48:13 -07005060 mPicture = mPicture.createAshmemBitmap();
5061 }
5062 if (mBigLargeIcon != null) {
5063 mBigLargeIcon.convertToAshmem();
5064 }
5065 }
Christoph Studer5c510ee2014-12-15 16:32:27 +01005066
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005067 /**
5068 * @hide
5069 */
5070 public RemoteViews makeBigContentView() {
5071 // Replace mN.mLargeIcon with mBigLargeIcon if mBigLargeIconSet
Christoph Studer5c510ee2014-12-15 16:32:27 +01005072 // This covers the following cases:
5073 // 1. mBigLargeIconSet -> mBigLargeIcon (null or non-null) applies, overrides
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005074 // mN.mLargeIcon
5075 // 2. !mBigLargeIconSet -> mN.mLargeIcon applies
Dan Sandlerd63f9322015-05-06 15:18:49 -04005076 Icon oldLargeIcon = null;
Selim Cineke99acb22016-08-04 12:55:48 -07005077 Bitmap largeIconLegacy = null;
Christoph Studer5c510ee2014-12-15 16:32:27 +01005078 if (mBigLargeIconSet) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005079 oldLargeIcon = mBuilder.mN.mLargeIcon;
5080 mBuilder.mN.mLargeIcon = mBigLargeIcon;
Selim Cineke99acb22016-08-04 12:55:48 -07005081 // The legacy largeIcon might not allow us to clear the image, as it's taken in
5082 // replacement if the other one is null. Because we're restoring these legacy icons
5083 // for old listeners, this is in general non-null.
5084 largeIconLegacy = mBuilder.mN.largeIcon;
5085 mBuilder.mN.largeIcon = null;
Christoph Studer5c510ee2014-12-15 16:32:27 +01005086 }
5087
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005088 RemoteViews contentView = getStandardView(mBuilder.getBigPictureLayoutResource());
Selim Cinek03d0d652015-11-13 13:18:09 -05005089 if (mSummaryTextSet) {
5090 contentView.setTextViewText(R.id.text, mBuilder.processLegacyText(mSummaryText));
Selim Cinek7b9605b2017-01-19 17:36:00 -08005091 mBuilder.setTextViewColorSecondary(contentView, R.id.text);
Selim Cinekc848c3a2016-01-13 15:27:30 -08005092 contentView.setViewVisibility(R.id.text, View.VISIBLE);
Selim Cinek03d0d652015-11-13 13:18:09 -05005093 }
Selim Cinek279fa862016-06-14 10:57:25 -07005094 mBuilder.setContentMinHeight(contentView, mBuilder.mN.hasLargeIcon());
Selim Cinek53e64a42015-11-16 10:40:56 -08005095
Christoph Studer5c510ee2014-12-15 16:32:27 +01005096 if (mBigLargeIconSet) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005097 mBuilder.mN.mLargeIcon = oldLargeIcon;
Selim Cineke99acb22016-08-04 12:55:48 -07005098 mBuilder.mN.largeIcon = largeIconLegacy;
Christoph Studer5c510ee2014-12-15 16:32:27 +01005099 }
5100
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005101 contentView.setImageViewBitmap(R.id.big_picture, mPicture);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005102 return contentView;
5103 }
5104
Daniel Sandlerf45564e2013-04-15 15:05:08 -04005105 /**
5106 * @hide
5107 */
5108 public void addExtras(Bundle extras) {
5109 super.addExtras(extras);
5110
5111 if (mBigLargeIconSet) {
5112 extras.putParcelable(EXTRA_LARGE_ICON_BIG, mBigLargeIcon);
5113 }
5114 extras.putParcelable(EXTRA_PICTURE, mPicture);
5115 }
5116
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04005117 /**
5118 * @hide
5119 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005120 @Override
Christoph Studer4600f9b2014-07-22 22:44:43 +02005121 protected void restoreFromExtras(Bundle extras) {
5122 super.restoreFromExtras(extras);
5123
5124 if (extras.containsKey(EXTRA_LARGE_ICON_BIG)) {
Christoph Studer5c510ee2014-12-15 16:32:27 +01005125 mBigLargeIconSet = true;
Christoph Studer4600f9b2014-07-22 22:44:43 +02005126 mBigLargeIcon = extras.getParcelable(EXTRA_LARGE_ICON_BIG);
Chris Wren3745a3d2012-05-22 15:11:52 -04005127 }
Christoph Studer4600f9b2014-07-22 22:44:43 +02005128 mPicture = extras.getParcelable(EXTRA_PICTURE);
5129 }
Selim Cinek03d0d652015-11-13 13:18:09 -05005130
5131 /**
5132 * @hide
5133 */
5134 @Override
5135 public boolean hasSummaryInHeader() {
5136 return false;
5137 }
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005138 }
5139
5140 /**
Daniel Sandler4dfbe832012-04-11 14:51:46 -04005141 * Helper class for generating large-format notifications that include a lot of text.
Joe Malin8d40d042012-11-05 11:36:40 -08005142 *
Robert Ly91c5ce32014-06-08 15:37:00 -07005143 * Here's how you'd set the <code>BigTextStyle</code> on a notification:
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005144 * <pre class="prettyprint">
Robert Ly91c5ce32014-06-08 15:37:00 -07005145 * Notification notif = new Notification.Builder(mContext)
5146 * .setContentTitle(&quot;New mail from &quot; + sender.toString())
5147 * .setContentText(subject)
5148 * .setSmallIcon(R.drawable.new_mail)
5149 * .setLargeIcon(aBitmap)
5150 * .setStyle(new Notification.BigTextStyle()
5151 * .bigText(aVeryLongString))
5152 * .build();
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005153 * </pre>
Joe Malin8d40d042012-11-05 11:36:40 -08005154 *
Daniel Sandler4dfbe832012-04-11 14:51:46 -04005155 * @see Notification#bigContentView
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005156 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005157 public static class BigTextStyle extends Style {
Jorim Jaggi457a10d2014-09-08 16:18:23 +02005158
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005159 private CharSequence mBigText;
5160
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005161 public BigTextStyle() {
5162 }
5163
Adrian Roosf5faf9d2016-05-23 13:56:15 -07005164 /**
5165 * @deprecated use {@code BigTextStyle()}.
5166 */
5167 @Deprecated
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005168 public BigTextStyle(Builder builder) {
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005169 setBuilder(builder);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005170 }
5171
Chris Wrend6297db2012-05-03 16:20:13 -04005172 /**
5173 * Overrides ContentTitle in the big form of the template.
5174 * This defaults to the value passed to setContentTitle().
5175 */
5176 public BigTextStyle setBigContentTitle(CharSequence title) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04005177 internalSetBigContentTitle(safeCharSequence(title));
Chris Wrend6297db2012-05-03 16:20:13 -04005178 return this;
5179 }
5180
5181 /**
5182 * Set the first line of text after the detail section in the big form of the template.
5183 */
5184 public BigTextStyle setSummaryText(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04005185 internalSetSummaryText(safeCharSequence(cs));
Chris Wrend6297db2012-05-03 16:20:13 -04005186 return this;
5187 }
5188
Chris Wren0bd664d2012-08-01 13:56:56 -04005189 /**
5190 * Provide the longer text to be displayed in the big form of the
5191 * template in place of the content text.
5192 */
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005193 public BigTextStyle bigText(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04005194 mBigText = safeCharSequence(cs);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005195 return this;
5196 }
5197
Daniel Sandlerf45564e2013-04-15 15:05:08 -04005198 /**
5199 * @hide
5200 */
5201 public void addExtras(Bundle extras) {
5202 super.addExtras(extras);
5203
Christoph Studer4600f9b2014-07-22 22:44:43 +02005204 extras.putCharSequence(EXTRA_BIG_TEXT, mBigText);
5205 }
5206
5207 /**
5208 * @hide
5209 */
5210 @Override
5211 protected void restoreFromExtras(Bundle extras) {
5212 super.restoreFromExtras(extras);
5213
5214 mBigText = extras.getCharSequence(EXTRA_BIG_TEXT);
Daniel Sandlerf45564e2013-04-15 15:05:08 -04005215 }
5216
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005217 /**
Selim Cinek7d1009b2017-01-25 15:28:28 -08005218 * @param increasedHeight true if this layout be created with an increased height.
5219 *
5220 * @hide
5221 */
5222 @Override
5223 public RemoteViews makeContentView(boolean increasedHeight) {
5224 if (increasedHeight) {
5225 ArrayList<Action> actions = mBuilder.mActions;
5226 mBuilder.mActions = new ArrayList<>();
5227 RemoteViews remoteViews = makeBigContentView();
5228 mBuilder.mActions = actions;
5229 return remoteViews;
5230 }
5231 return super.makeContentView(increasedHeight);
5232 }
5233
5234 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005235 * @hide
5236 */
Selim Cinek87ed69b2017-02-09 15:59:43 -08005237 @Override
5238 public RemoteViews makeHeadsUpContentView(boolean increasedHeight) {
5239 if (increasedHeight && mBuilder.mActions.size() > 0) {
5240 return makeBigContentView();
5241 }
5242 return super.makeHeadsUpContentView(increasedHeight);
5243 }
5244
5245 /**
5246 * @hide
5247 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005248 public RemoteViews makeBigContentView() {
Christoph Studer4600f9b2014-07-22 22:44:43 +02005249
5250 // Nasty
Selim Cinek75998782016-04-26 10:39:17 -07005251 CharSequence text = mBuilder.getAllExtras().getCharSequence(EXTRA_TEXT);
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005252 mBuilder.getAllExtras().putCharSequence(EXTRA_TEXT, null);
Daniel Sandler916ad912012-06-13 12:17:07 -04005253
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005254 RemoteViews contentView = getStandardView(mBuilder.getBigTextLayoutResource());
Joe Malin8d40d042012-11-05 11:36:40 -08005255
Selim Cinek75998782016-04-26 10:39:17 -07005256 mBuilder.getAllExtras().putCharSequence(EXTRA_TEXT, text);
Christoph Studer4600f9b2014-07-22 22:44:43 +02005257
Selim Cinek3a2c4b92015-12-17 17:01:17 -08005258 CharSequence bigTextText = mBuilder.processLegacyText(mBigText);
Selim Cinek75998782016-04-26 10:39:17 -07005259 if (TextUtils.isEmpty(bigTextText)) {
5260 // In case the bigtext is null / empty fall back to the normal text to avoid a weird
5261 // experience
5262 bigTextText = mBuilder.processLegacyText(text);
5263 }
Adrian Roosb1f427c2016-05-26 12:27:15 -07005264 applyBigTextContentView(mBuilder, contentView, bigTextText);
Selim Cinek4fb12d32015-11-19 18:10:48 -08005265
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005266 return contentView;
5267 }
5268
Adrian Roosb1f427c2016-05-26 12:27:15 -07005269 static void applyBigTextContentView(Builder builder,
5270 RemoteViews contentView, CharSequence bigTextText) {
5271 contentView.setTextViewText(R.id.big_text, bigTextText);
Selim Cinek7b9605b2017-01-19 17:36:00 -08005272 builder.setTextViewColorSecondary(contentView, R.id.big_text);
Adrian Roosb1f427c2016-05-26 12:27:15 -07005273 contentView.setViewVisibility(R.id.big_text,
5274 TextUtils.isEmpty(bigTextText) ? View.GONE : View.VISIBLE);
Selim Cinek279fa862016-06-14 10:57:25 -07005275 contentView.setBoolean(R.id.big_text, "setHasImage", builder.mN.hasLargeIcon());
Adrian Roosb1f427c2016-05-26 12:27:15 -07005276 }
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005277 }
Daniel Sandler879c5e02012-04-17 16:46:51 -04005278
5279 /**
Alex Hillsfc737de2016-03-23 17:33:02 -04005280 * Helper class for generating large-format notifications that include multiple back-and-forth
5281 * messages of varying types between any number of people.
5282 *
5283 * <br>
5284 * If the platform does not provide large-format notifications, this method has no effect. The
5285 * user will always see the normal notification view.
5286 * <br>
5287 * This class is a "rebuilder": It attaches to a Builder object and modifies its behavior, like
5288 * so:
5289 * <pre class="prettyprint">
5290 *
5291 * Notification noti = new Notification.Builder()
5292 * .setContentTitle(&quot;2 new messages wtih &quot; + sender.toString())
5293 * .setContentText(subject)
5294 * .setSmallIcon(R.drawable.new_message)
5295 * .setLargeIcon(aBitmap)
5296 * .setStyle(new Notification.MessagingStyle(resources.getString(R.string.reply_name))
5297 * .addMessage(messages[0].getText(), messages[0].getTime(), messages[0].getSender())
5298 * .addMessage(messages[1].getText(), messages[1].getTime(), messages[1].getSender()))
5299 * .build();
5300 * </pre>
5301 */
5302 public static class MessagingStyle extends Style {
5303
5304 /**
5305 * The maximum number of messages that will be retained in the Notification itself (the
5306 * number displayed is up to the platform).
5307 */
5308 public static final int MAXIMUM_RETAINED_MESSAGES = 25;
5309
5310 CharSequence mUserDisplayName;
5311 CharSequence mConversationTitle;
Alex Hillsd9b04d92016-04-11 16:38:16 -04005312 List<Message> mMessages = new ArrayList<>();
Adrian Roos437cd562017-01-18 15:47:03 -08005313 List<Message> mHistoricMessages = new ArrayList<>();
Alex Hillsfc737de2016-03-23 17:33:02 -04005314
5315 MessagingStyle() {
5316 }
5317
5318 /**
Alex Hillsfd590442016-10-07 09:52:44 -04005319 * @param userDisplayName Required - the name to be displayed for any replies sent by the
5320 * user before the posting app reposts the notification with those messages after they've
5321 * been actually sent and in previous messages sent by the user added in
Alex Hillsfc737de2016-03-23 17:33:02 -04005322 * {@link #addMessage(Notification.MessagingStyle.Message)}
5323 */
Alex Hillsfd590442016-10-07 09:52:44 -04005324 public MessagingStyle(@NonNull CharSequence userDisplayName) {
Alex Hillsfc737de2016-03-23 17:33:02 -04005325 mUserDisplayName = userDisplayName;
5326 }
5327
5328 /**
5329 * Returns the name to be displayed for any replies sent by the user
5330 */
5331 public CharSequence getUserDisplayName() {
5332 return mUserDisplayName;
5333 }
5334
5335 /**
Alex Hillsfc737de2016-03-23 17:33:02 -04005336 * Sets the title to be displayed on this conversation. This should only be used for
5337 * group messaging and left unset for one-on-one conversations.
5338 * @param conversationTitle
5339 * @return this object for method chaining.
5340 */
5341 public MessagingStyle setConversationTitle(CharSequence conversationTitle) {
5342 mConversationTitle = conversationTitle;
5343 return this;
5344 }
5345
5346 /**
5347 * Return the title to be displayed on this conversation. Can be <code>null</code> and
5348 * should be for one-on-one conversations
5349 */
5350 public CharSequence getConversationTitle() {
5351 return mConversationTitle;
5352 }
5353
5354 /**
5355 * Adds a message for display by this notification. Convenience call for a simple
5356 * {@link Message} in {@link #addMessage(Notification.MessagingStyle.Message)}.
5357 * @param text A {@link CharSequence} to be displayed as the message content
5358 * @param timestamp Time at which the message arrived
5359 * @param sender A {@link CharSequence} to be used for displaying the name of the
5360 * sender. Should be <code>null</code> for messages by the current user, in which case
5361 * the platform will insert {@link #getUserDisplayName()}.
5362 * Should be unique amongst all individuals in the conversation, and should be
5363 * consistent during re-posts of the notification.
5364 *
5365 * @see Message#Message(CharSequence, long, CharSequence)
5366 *
5367 * @return this object for method chaining
5368 */
5369 public MessagingStyle addMessage(CharSequence text, long timestamp, CharSequence sender) {
Adrian Roos437cd562017-01-18 15:47:03 -08005370 return addMessage(new Message(text, timestamp, sender));
Alex Hillsfc737de2016-03-23 17:33:02 -04005371 }
5372
5373 /**
5374 * Adds a {@link Message} for display in this notification.
Adrian Roos437cd562017-01-18 15:47:03 -08005375 *
5376 * <p>The messages should be added in chronologic order, i.e. the oldest first,
5377 * the newest last.
5378 *
Alex Hillsfc737de2016-03-23 17:33:02 -04005379 * @param message The {@link Message} to be displayed
5380 * @return this object for method chaining
5381 */
5382 public MessagingStyle addMessage(Message message) {
5383 mMessages.add(message);
5384 if (mMessages.size() > MAXIMUM_RETAINED_MESSAGES) {
5385 mMessages.remove(0);
5386 }
5387 return this;
5388 }
5389
5390 /**
Adrian Roos437cd562017-01-18 15:47:03 -08005391 * Adds a {@link Message} for historic context in this notification.
5392 *
5393 * <p>Messages should be added as historic if they are not the main subject of the
5394 * notification but may give context to a conversation. The system may choose to present
5395 * them only when relevant, e.g. when replying to a message through a {@link RemoteInput}.
5396 *
5397 * <p>The messages should be added in chronologic order, i.e. the oldest first,
5398 * the newest last.
5399 *
5400 * @param message The historic {@link Message} to be added
5401 * @return this object for method chaining
5402 */
5403 public MessagingStyle addHistoricMessage(Message message) {
5404 mHistoricMessages.add(message);
5405 if (mHistoricMessages.size() > MAXIMUM_RETAINED_MESSAGES) {
5406 mHistoricMessages.remove(0);
5407 }
5408 return this;
5409 }
5410
5411 /**
Alex Hillsfc737de2016-03-23 17:33:02 -04005412 * Gets the list of {@code Message} objects that represent the notification
5413 */
5414 public List<Message> getMessages() {
5415 return mMessages;
5416 }
5417
5418 /**
Adrian Roos437cd562017-01-18 15:47:03 -08005419 * Gets the list of historic {@code Message}s in the notification.
5420 */
5421 public List<Message> getHistoricMessages() {
5422 return mHistoricMessages;
5423 }
5424
5425 /**
Alex Hillsfc737de2016-03-23 17:33:02 -04005426 * @hide
5427 */
5428 @Override
5429 public void addExtras(Bundle extras) {
5430 super.addExtras(extras);
5431 if (mUserDisplayName != null) {
5432 extras.putCharSequence(EXTRA_SELF_DISPLAY_NAME, mUserDisplayName);
5433 }
5434 if (mConversationTitle != null) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04005435 extras.putCharSequence(EXTRA_CONVERSATION_TITLE, mConversationTitle);
Alex Hillsfc737de2016-03-23 17:33:02 -04005436 }
Alex Hillsd9b04d92016-04-11 16:38:16 -04005437 if (!mMessages.isEmpty()) { extras.putParcelableArray(EXTRA_MESSAGES,
5438 Message.getBundleArrayForMessages(mMessages));
Alex Hillsfc737de2016-03-23 17:33:02 -04005439 }
Adrian Roos437cd562017-01-18 15:47:03 -08005440 if (!mHistoricMessages.isEmpty()) { extras.putParcelableArray(EXTRA_HISTORIC_MESSAGES,
5441 Message.getBundleArrayForMessages(mHistoricMessages));
5442 }
Adrian Roos33fbd2c2016-05-27 15:35:28 -07005443
5444 fixTitleAndTextExtras(extras);
5445 }
5446
5447 private void fixTitleAndTextExtras(Bundle extras) {
5448 Message m = findLatestIncomingMessage();
5449 CharSequence text = (m == null) ? null : m.mText;
5450 CharSequence sender = m == null ? null
5451 : TextUtils.isEmpty(m.mSender) ? mUserDisplayName : m.mSender;
5452 CharSequence title;
5453 if (!TextUtils.isEmpty(mConversationTitle)) {
5454 if (!TextUtils.isEmpty(sender)) {
5455 BidiFormatter bidi = BidiFormatter.getInstance();
5456 title = mBuilder.mContext.getString(
5457 com.android.internal.R.string.notification_messaging_title_template,
5458 bidi.unicodeWrap(mConversationTitle), bidi.unicodeWrap(m.mSender));
5459 } else {
5460 title = mConversationTitle;
5461 }
5462 } else {
5463 title = sender;
5464 }
5465
5466 if (title != null) {
5467 extras.putCharSequence(EXTRA_TITLE, title);
5468 }
5469 if (text != null) {
5470 extras.putCharSequence(EXTRA_TEXT, text);
5471 }
Alex Hillsfc737de2016-03-23 17:33:02 -04005472 }
5473
5474 /**
5475 * @hide
5476 */
5477 @Override
5478 protected void restoreFromExtras(Bundle extras) {
5479 super.restoreFromExtras(extras);
5480
5481 mMessages.clear();
Adrian Roos437cd562017-01-18 15:47:03 -08005482 mHistoricMessages.clear();
Adrian Roos96b7e202016-05-17 13:50:38 -07005483 mUserDisplayName = extras.getCharSequence(EXTRA_SELF_DISPLAY_NAME);
5484 mConversationTitle = extras.getCharSequence(EXTRA_CONVERSATION_TITLE);
Adrian Roos437cd562017-01-18 15:47:03 -08005485 Parcelable[] messages = extras.getParcelableArray(EXTRA_MESSAGES);
5486 if (messages != null && messages instanceof Parcelable[]) {
5487 mMessages = Message.getMessagesFromBundleArray(messages);
5488 }
5489 Parcelable[] histMessages = extras.getParcelableArray(EXTRA_HISTORIC_MESSAGES);
5490 if (histMessages != null && histMessages instanceof Parcelable[]) {
5491 mHistoricMessages = Message.getMessagesFromBundleArray(histMessages);
Alex Hillsfc737de2016-03-23 17:33:02 -04005492 }
5493 }
5494
5495 /**
5496 * @hide
5497 */
Adrian Roosc1a80b02016-04-05 14:54:55 -07005498 @Override
Selim Cinek7d1009b2017-01-25 15:28:28 -08005499 public RemoteViews makeContentView(boolean increasedHeight) {
5500 if (!increasedHeight) {
5501 Message m = findLatestIncomingMessage();
5502 CharSequence title = mConversationTitle != null
5503 ? mConversationTitle
5504 : (m == null) ? null : m.mSender;
5505 CharSequence text = (m == null)
5506 ? null
5507 : mConversationTitle != null ? makeMessageLine(m, mBuilder) : m.mText;
Alex Hillsfc737de2016-03-23 17:33:02 -04005508
Selim Cinek7d1009b2017-01-25 15:28:28 -08005509 return mBuilder.applyStandardTemplate(mBuilder.getBaseLayoutResource(),
5510 mBuilder.mParams.reset().hasProgress(false).title(title).text(text));
5511 } else {
5512 ArrayList<Action> actions = mBuilder.mActions;
5513 mBuilder.mActions = new ArrayList<>();
5514 RemoteViews remoteViews = makeBigContentView();
5515 mBuilder.mActions = actions;
5516 return remoteViews;
5517 }
Adrian Roosc1a80b02016-04-05 14:54:55 -07005518 }
5519
5520 private Message findLatestIncomingMessage() {
5521 for (int i = mMessages.size() - 1; i >= 0; i--) {
5522 Message m = mMessages.get(i);
5523 // Incoming messages have a non-empty sender.
5524 if (!TextUtils.isEmpty(m.mSender)) {
5525 return m;
5526 }
5527 }
Adrian Roos33fbd2c2016-05-27 15:35:28 -07005528 if (!mMessages.isEmpty()) {
5529 // No incoming messages, fall back to outgoing message
5530 return mMessages.get(mMessages.size() - 1);
5531 }
Adrian Roosc1a80b02016-04-05 14:54:55 -07005532 return null;
5533 }
5534
5535 /**
5536 * @hide
5537 */
5538 @Override
5539 public RemoteViews makeBigContentView() {
5540 CharSequence title = !TextUtils.isEmpty(super.mBigContentTitle)
5541 ? super.mBigContentTitle
5542 : mConversationTitle;
5543 boolean hasTitle = !TextUtils.isEmpty(title);
5544
Adrian Roosfeafa052016-06-01 17:09:45 -07005545 if (mMessages.size() == 1) {
5546 // Special case for a single message: Use the big text style
5547 // so the collapsed and expanded versions match nicely.
5548 CharSequence bigTitle;
5549 CharSequence text;
5550 if (hasTitle) {
5551 bigTitle = title;
Selim Cinek7b9605b2017-01-19 17:36:00 -08005552 text = makeMessageLine(mMessages.get(0), mBuilder);
Adrian Roosfeafa052016-06-01 17:09:45 -07005553 } else {
5554 bigTitle = mMessages.get(0).mSender;
5555 text = mMessages.get(0).mText;
5556 }
Adrian Roosb1f427c2016-05-26 12:27:15 -07005557 RemoteViews contentView = mBuilder.applyStandardTemplateWithActions(
5558 mBuilder.getBigTextLayoutResource(),
Adrian Roos70d7aa32017-01-11 15:39:06 -08005559 mBuilder.mParams.reset().hasProgress(false).title(bigTitle).text(null));
Adrian Roosb1f427c2016-05-26 12:27:15 -07005560 BigTextStyle.applyBigTextContentView(mBuilder, contentView, text);
5561 return contentView;
5562 }
5563
Adrian Roos48d746a2016-04-12 14:57:28 -07005564 RemoteViews contentView = mBuilder.applyStandardTemplateWithActions(
Adrian Roosc1a80b02016-04-05 14:54:55 -07005565 mBuilder.getMessagingLayoutResource(),
Adrian Roos70d7aa32017-01-11 15:39:06 -08005566 mBuilder.mParams.reset().hasProgress(false).title(title).text(null));
Adrian Roosc1a80b02016-04-05 14:54:55 -07005567
5568 int[] rowIds = {R.id.inbox_text0, R.id.inbox_text1, R.id.inbox_text2, R.id.inbox_text3,
5569 R.id.inbox_text4, R.id.inbox_text5, R.id.inbox_text6};
5570
5571 // Make sure all rows are gone in case we reuse a view.
5572 for (int rowId : rowIds) {
5573 contentView.setViewVisibility(rowId, View.GONE);
5574 }
5575
5576 int i=0;
Adrian Roos2d5dbba2016-06-08 17:11:53 -07005577 contentView.setViewLayoutMarginBottomDimen(R.id.line1,
5578 hasTitle ? R.dimen.notification_messaging_spacing : 0);
Adrian Roosc1a80b02016-04-05 14:54:55 -07005579 contentView.setInt(R.id.notification_messaging, "setNumIndentLines",
Selim Cinek279fa862016-06-14 10:57:25 -07005580 !mBuilder.mN.hasLargeIcon() ? 0 : (hasTitle ? 1 : 2));
Adrian Roosc1a80b02016-04-05 14:54:55 -07005581
Adrian Roosfeafa052016-06-01 17:09:45 -07005582 int contractedChildId = View.NO_ID;
5583 Message contractedMessage = findLatestIncomingMessage();
Adrian Roos437cd562017-01-18 15:47:03 -08005584 int firstHistoricMessage = Math.max(0, mHistoricMessages.size()
5585 - (rowIds.length - mMessages.size()));
5586 while (firstHistoricMessage + i < mHistoricMessages.size() && i < rowIds.length) {
5587 Message m = mHistoricMessages.get(firstHistoricMessage + i);
5588 int rowId = rowIds[i];
5589
Selim Cinek7b9605b2017-01-19 17:36:00 -08005590 contentView.setTextViewText(rowId, makeMessageLine(m, mBuilder));
Adrian Roos437cd562017-01-18 15:47:03 -08005591
5592 if (contractedMessage == m) {
5593 contractedChildId = rowId;
5594 }
5595
5596 i++;
5597 }
5598
Adrian Roosc1a80b02016-04-05 14:54:55 -07005599 int firstMessage = Math.max(0, mMessages.size() - rowIds.length);
5600 while (firstMessage + i < mMessages.size() && i < rowIds.length) {
5601 Message m = mMessages.get(firstMessage + i);
5602 int rowId = rowIds[i];
5603
5604 contentView.setViewVisibility(rowId, View.VISIBLE);
Selim Cinek7b9605b2017-01-19 17:36:00 -08005605 contentView.setTextViewText(rowId, makeMessageLine(m, mBuilder));
5606 mBuilder.setTextViewColorSecondary(contentView, rowId);
Adrian Roosc1a80b02016-04-05 14:54:55 -07005607
Adrian Roosfeafa052016-06-01 17:09:45 -07005608 if (contractedMessage == m) {
5609 contractedChildId = rowId;
5610 }
5611
Adrian Roosc1a80b02016-04-05 14:54:55 -07005612 i++;
5613 }
Adrian Roos437cd562017-01-18 15:47:03 -08005614 // Clear the remaining views for reapply. Ensures that historic message views can
5615 // reliably be identified as being GONE and having non-null text.
5616 while (i < rowIds.length) {
5617 int rowId = rowIds[i];
5618 contentView.setTextViewText(rowId, null);
5619 i++;
5620 }
5621
Adrian Roosfeafa052016-06-01 17:09:45 -07005622 // Record this here to allow transformation between the contracted and expanded views.
5623 contentView.setInt(R.id.notification_messaging, "setContractedChildId",
5624 contractedChildId);
Alex Hillsfc737de2016-03-23 17:33:02 -04005625 return contentView;
5626 }
5627
Selim Cinek7b9605b2017-01-19 17:36:00 -08005628 private CharSequence makeMessageLine(Message m, Builder builder) {
Adrian Roosc1a80b02016-04-05 14:54:55 -07005629 BidiFormatter bidi = BidiFormatter.getInstance();
5630 SpannableStringBuilder sb = new SpannableStringBuilder();
Selim Cinek7b9605b2017-01-19 17:36:00 -08005631 boolean colorize = builder.isColorized();
Adrian Roosc1a80b02016-04-05 14:54:55 -07005632 if (TextUtils.isEmpty(m.mSender)) {
5633 CharSequence replyName = mUserDisplayName == null ? "" : mUserDisplayName;
5634 sb.append(bidi.unicodeWrap(replyName),
Selim Cinek7b9605b2017-01-19 17:36:00 -08005635 makeFontColorSpan(colorize
5636 ? builder.getPrimaryTextColor()
5637 : mBuilder.resolveContrastColor()),
Adrian Roosc1a80b02016-04-05 14:54:55 -07005638 0 /* flags */);
5639 } else {
5640 sb.append(bidi.unicodeWrap(m.mSender),
Selim Cinek7b9605b2017-01-19 17:36:00 -08005641 makeFontColorSpan(colorize
5642 ? builder.getPrimaryTextColor()
5643 : Color.BLACK),
Adrian Roosc1a80b02016-04-05 14:54:55 -07005644 0 /* flags */);
5645 }
5646 CharSequence text = m.mText == null ? "" : m.mText;
5647 sb.append(" ").append(bidi.unicodeWrap(text));
5648 return sb;
5649 }
5650
Adrian Roosdedd1df2016-04-26 16:38:47 -07005651 /**
5652 * @hide
5653 */
5654 @Override
Selim Cinek87ed69b2017-02-09 15:59:43 -08005655 public RemoteViews makeHeadsUpContentView(boolean increasedHeight) {
5656 if (increasedHeight) {
5657 return makeBigContentView();
5658 }
Adrian Roosdedd1df2016-04-26 16:38:47 -07005659 Message m = findLatestIncomingMessage();
5660 CharSequence title = mConversationTitle != null
5661 ? mConversationTitle
5662 : (m == null) ? null : m.mSender;
5663 CharSequence text = (m == null)
5664 ? null
Selim Cinek7b9605b2017-01-19 17:36:00 -08005665 : mConversationTitle != null ? makeMessageLine(m, mBuilder) : m.mText;
Adrian Roosdedd1df2016-04-26 16:38:47 -07005666
5667 return mBuilder.applyStandardTemplateWithActions(mBuilder.getBigBaseLayoutResource(),
Adrian Roos70d7aa32017-01-11 15:39:06 -08005668 mBuilder.mParams.reset().hasProgress(false).title(title).text(text));
Adrian Roosdedd1df2016-04-26 16:38:47 -07005669 }
5670
Adrian Roosc1a80b02016-04-05 14:54:55 -07005671 private static TextAppearanceSpan makeFontColorSpan(int color) {
5672 return new TextAppearanceSpan(null, 0, 0,
5673 ColorStateList.valueOf(color), null);
5674 }
5675
Alex Hillsd9b04d92016-04-11 16:38:16 -04005676 public static final class Message {
5677
5678 static final String KEY_TEXT = "text";
5679 static final String KEY_TIMESTAMP = "time";
5680 static final String KEY_SENDER = "sender";
5681 static final String KEY_DATA_MIME_TYPE = "type";
5682 static final String KEY_DATA_URI= "uri";
Shane Brennan5a871862017-03-11 13:14:17 -08005683 static final String KEY_EXTRAS_BUNDLE = "extras";
Alex Hillsfc737de2016-03-23 17:33:02 -04005684
5685 private final CharSequence mText;
5686 private final long mTimestamp;
5687 private final CharSequence mSender;
5688
Shane Brennan5a871862017-03-11 13:14:17 -08005689 private Bundle mExtras = new Bundle();
Alex Hillsfc737de2016-03-23 17:33:02 -04005690 private String mDataMimeType;
5691 private Uri mDataUri;
5692
5693 /**
5694 * Constructor
5695 * @param text A {@link CharSequence} to be displayed as the message content
5696 * @param timestamp Time at which the message arrived
5697 * @param sender A {@link CharSequence} to be used for displaying the name of the
5698 * sender. Should be <code>null</code> for messages by the current user, in which case
5699 * the platform will insert {@link MessagingStyle#getUserDisplayName()}.
5700 * Should be unique amongst all individuals in the conversation, and should be
5701 * consistent during re-posts of the notification.
5702 */
5703 public Message(CharSequence text, long timestamp, CharSequence sender){
5704 mText = text;
5705 mTimestamp = timestamp;
5706 mSender = sender;
5707 }
5708
5709 /**
5710 * Sets a binary blob of data and an associated MIME type for a message. In the case
5711 * where the platform doesn't support the MIME type, the original text provided in the
5712 * constructor will be used.
5713 * @param dataMimeType The MIME type of the content. See
5714 * <a href="{@docRoot}notifications/messaging.html"> for the list of supported MIME
5715 * types on Android and Android Wear.
5716 * @param dataUri The uri containing the content whose type is given by the MIME type.
5717 * <p class="note">
5718 * <ol>
5719 * <li>Notification Listeners including the System UI need permission to access the
5720 * data the Uri points to. The recommended ways to do this are:</li>
5721 * <li>Store the data in your own ContentProvider, making sure that other apps have
5722 * the correct permission to access your provider. The preferred mechanism for
5723 * providing access is to use per-URI permissions which are temporary and only
5724 * grant access to the receiving application. An easy way to create a
5725 * ContentProvider like this is to use the FileProvider helper class.</li>
5726 * <li>Use the system MediaStore. The MediaStore is primarily aimed at video, audio
5727 * and image MIME types, however beginning with Android 3.0 (API level 11) it can
5728 * also store non-media types (see MediaStore.Files for more info). Files can be
5729 * inserted into the MediaStore using scanFile() after which a content:// style
5730 * Uri suitable for sharing is passed to the provided onScanCompleted() callback.
5731 * Note that once added to the system MediaStore the content is accessible to any
5732 * app on the device.</li>
5733 * </ol>
5734 * @return this object for method chaining
5735 */
5736 public Message setData(String dataMimeType, Uri dataUri) {
5737 mDataMimeType = dataMimeType;
5738 mDataUri = dataUri;
5739 return this;
5740 }
5741
Alex Hillsfc737de2016-03-23 17:33:02 -04005742 /**
5743 * Get the text to be used for this message, or the fallback text if a type and content
5744 * Uri have been set
5745 */
5746 public CharSequence getText() {
5747 return mText;
5748 }
5749
5750 /**
5751 * Get the time at which this message arrived
5752 */
5753 public long getTimestamp() {
5754 return mTimestamp;
5755 }
5756
5757 /**
Shane Brennan5a871862017-03-11 13:14:17 -08005758 * Get the extras Bundle for this message.
5759 */
5760 public Bundle getExtras() {
5761 return mExtras;
5762 }
5763
5764 /**
Alex Hillsfc737de2016-03-23 17:33:02 -04005765 * Get the text used to display the contact's name in the messaging experience
5766 */
5767 public CharSequence getSender() {
5768 return mSender;
5769 }
5770
5771 /**
5772 * Get the MIME type of the data pointed to by the Uri
5773 */
5774 public String getDataMimeType() {
5775 return mDataMimeType;
5776 }
5777
5778 /**
5779 * Get the the Uri pointing to the content of the message. Can be null, in which case
5780 * {@see #getText()} is used.
5781 */
5782 public Uri getDataUri() {
5783 return mDataUri;
5784 }
5785
Alex Hillsd9b04d92016-04-11 16:38:16 -04005786 private Bundle toBundle() {
5787 Bundle bundle = new Bundle();
Alex Hillsfc737de2016-03-23 17:33:02 -04005788 if (mText != null) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04005789 bundle.putCharSequence(KEY_TEXT, mText);
Alex Hillsfc737de2016-03-23 17:33:02 -04005790 }
Alex Hillsd9b04d92016-04-11 16:38:16 -04005791 bundle.putLong(KEY_TIMESTAMP, mTimestamp);
Alex Hillsfc737de2016-03-23 17:33:02 -04005792 if (mSender != null) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04005793 bundle.putCharSequence(KEY_SENDER, mSender);
Alex Hillsfc737de2016-03-23 17:33:02 -04005794 }
5795 if (mDataMimeType != null) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04005796 bundle.putString(KEY_DATA_MIME_TYPE, mDataMimeType);
Alex Hillsfc737de2016-03-23 17:33:02 -04005797 }
5798 if (mDataUri != null) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04005799 bundle.putParcelable(KEY_DATA_URI, mDataUri);
Alex Hillsfc737de2016-03-23 17:33:02 -04005800 }
Shane Brennan5a871862017-03-11 13:14:17 -08005801 if (mExtras != null) {
5802 bundle.putBundle(KEY_EXTRAS_BUNDLE, mExtras);
5803 }
Alex Hillsd9b04d92016-04-11 16:38:16 -04005804 return bundle;
Alex Hillsfc737de2016-03-23 17:33:02 -04005805 }
5806
Alex Hillsd9b04d92016-04-11 16:38:16 -04005807 static Bundle[] getBundleArrayForMessages(List<Message> messages) {
5808 Bundle[] bundles = new Bundle[messages.size()];
5809 final int N = messages.size();
5810 for (int i = 0; i < N; i++) {
5811 bundles[i] = messages.get(i).toBundle();
5812 }
5813 return bundles;
5814 }
5815
Adrian Roosdedd1df2016-04-26 16:38:47 -07005816 static List<Message> getMessagesFromBundleArray(Parcelable[] bundles) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04005817 List<Message> messages = new ArrayList<>(bundles.length);
5818 for (int i = 0; i < bundles.length; i++) {
Adrian Roosdedd1df2016-04-26 16:38:47 -07005819 if (bundles[i] instanceof Bundle) {
5820 Message message = getMessageFromBundle((Bundle)bundles[i]);
5821 if (message != null) {
5822 messages.add(message);
5823 }
Alex Hillsd9b04d92016-04-11 16:38:16 -04005824 }
5825 }
5826 return messages;
5827 }
5828
5829 static Message getMessageFromBundle(Bundle bundle) {
5830 try {
Adrian Roosfbddd2c2016-05-13 12:57:20 -07005831 if (!bundle.containsKey(KEY_TEXT) || !bundle.containsKey(KEY_TIMESTAMP)) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04005832 return null;
5833 } else {
5834 Message message = new Message(bundle.getCharSequence(KEY_TEXT),
5835 bundle.getLong(KEY_TIMESTAMP), bundle.getCharSequence(KEY_SENDER));
5836 if (bundle.containsKey(KEY_DATA_MIME_TYPE) &&
5837 bundle.containsKey(KEY_DATA_URI)) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04005838 message.setData(bundle.getString(KEY_DATA_MIME_TYPE),
5839 (Uri) bundle.getParcelable(KEY_DATA_URI));
Alex Hillsfc737de2016-03-23 17:33:02 -04005840 }
Shane Brennan5a871862017-03-11 13:14:17 -08005841 if (bundle.containsKey(KEY_EXTRAS_BUNDLE)) {
5842 message.getExtras().putAll(bundle.getBundle(KEY_EXTRAS_BUNDLE));
5843 }
Alex Hillsd9b04d92016-04-11 16:38:16 -04005844 return message;
5845 }
5846 } catch (ClassCastException e) {
5847 return null;
5848 }
5849 }
Alex Hillsfc737de2016-03-23 17:33:02 -04005850 }
5851 }
5852
5853 /**
Daniel Sandler879c5e02012-04-17 16:46:51 -04005854 * Helper class for generating large-format notifications that include a list of (up to 5) strings.
Joe Malin8d40d042012-11-05 11:36:40 -08005855 *
Robert Ly91c5ce32014-06-08 15:37:00 -07005856 * Here's how you'd set the <code>InboxStyle</code> on a notification:
Daniel Sandler879c5e02012-04-17 16:46:51 -04005857 * <pre class="prettyprint">
Robert Ly91c5ce32014-06-08 15:37:00 -07005858 * Notification notif = new Notification.Builder(mContext)
5859 * .setContentTitle(&quot;5 New mails from &quot; + sender.toString())
5860 * .setContentText(subject)
5861 * .setSmallIcon(R.drawable.new_mail)
5862 * .setLargeIcon(aBitmap)
5863 * .setStyle(new Notification.InboxStyle()
5864 * .addLine(str1)
5865 * .addLine(str2)
5866 * .setContentTitle(&quot;&quot;)
5867 * .setSummaryText(&quot;+3 more&quot;))
5868 * .build();
Daniel Sandler879c5e02012-04-17 16:46:51 -04005869 * </pre>
Joe Malin8d40d042012-11-05 11:36:40 -08005870 *
Daniel Sandler879c5e02012-04-17 16:46:51 -04005871 * @see Notification#bigContentView
5872 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005873 public static class InboxStyle extends Style {
Daniel Sandler879c5e02012-04-17 16:46:51 -04005874 private ArrayList<CharSequence> mTexts = new ArrayList<CharSequence>(5);
5875
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005876 public InboxStyle() {
5877 }
5878
Adrian Roosf5faf9d2016-05-23 13:56:15 -07005879 /**
5880 * @deprecated use {@code InboxStyle()}.
5881 */
5882 @Deprecated
Daniel Sandler879c5e02012-04-17 16:46:51 -04005883 public InboxStyle(Builder builder) {
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005884 setBuilder(builder);
Daniel Sandler879c5e02012-04-17 16:46:51 -04005885 }
5886
Chris Wrend6297db2012-05-03 16:20:13 -04005887 /**
5888 * Overrides ContentTitle in the big form of the template.
5889 * This defaults to the value passed to setContentTitle().
5890 */
5891 public InboxStyle setBigContentTitle(CharSequence title) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04005892 internalSetBigContentTitle(safeCharSequence(title));
Chris Wrend6297db2012-05-03 16:20:13 -04005893 return this;
5894 }
5895
5896 /**
5897 * Set the first line of text after the detail section in the big form of the template.
5898 */
5899 public InboxStyle setSummaryText(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04005900 internalSetSummaryText(safeCharSequence(cs));
Chris Wrend6297db2012-05-03 16:20:13 -04005901 return this;
5902 }
5903
Chris Wren0bd664d2012-08-01 13:56:56 -04005904 /**
5905 * Append a line to the digest section of the Inbox notification.
5906 */
Daniel Sandler879c5e02012-04-17 16:46:51 -04005907 public InboxStyle addLine(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04005908 mTexts.add(safeCharSequence(cs));
Daniel Sandler879c5e02012-04-17 16:46:51 -04005909 return this;
5910 }
5911
Daniel Sandlerf45564e2013-04-15 15:05:08 -04005912 /**
5913 * @hide
5914 */
5915 public void addExtras(Bundle extras) {
5916 super.addExtras(extras);
Christoph Studer4600f9b2014-07-22 22:44:43 +02005917
Daniel Sandlerf45564e2013-04-15 15:05:08 -04005918 CharSequence[] a = new CharSequence[mTexts.size()];
5919 extras.putCharSequenceArray(EXTRA_TEXT_LINES, mTexts.toArray(a));
5920 }
5921
Christoph Studer4600f9b2014-07-22 22:44:43 +02005922 /**
5923 * @hide
5924 */
5925 @Override
5926 protected void restoreFromExtras(Bundle extras) {
5927 super.restoreFromExtras(extras);
5928
5929 mTexts.clear();
5930 if (extras.containsKey(EXTRA_TEXT_LINES)) {
5931 Collections.addAll(mTexts, extras.getCharSequenceArray(EXTRA_TEXT_LINES));
5932 }
5933 }
5934
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005935 /**
5936 * @hide
5937 */
5938 public RemoteViews makeBigContentView() {
Selim Cinekc848c3a2016-01-13 15:27:30 -08005939 // Remove the content text so it disappears unless you have a summary
Christoph Studer4600f9b2014-07-22 22:44:43 +02005940 // Nasty
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005941 CharSequence oldBuilderContentText = mBuilder.mN.extras.getCharSequence(EXTRA_TEXT);
5942 mBuilder.getAllExtras().putCharSequence(EXTRA_TEXT, null);
Christoph Studer4600f9b2014-07-22 22:44:43 +02005943
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005944 RemoteViews contentView = getStandardView(mBuilder.getInboxLayoutResource());
Daniel Sandler619738c2012-06-07 16:33:08 -04005945
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005946 mBuilder.getAllExtras().putCharSequence(EXTRA_TEXT, oldBuilderContentText);
Christoph Studer4600f9b2014-07-22 22:44:43 +02005947
Chris Wrend6297db2012-05-03 16:20:13 -04005948 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 -04005949 R.id.inbox_text4, R.id.inbox_text5, R.id.inbox_text6};
Chris Wrend6297db2012-05-03 16:20:13 -04005950
Chris Wren4ed80d52012-05-17 09:30:03 -04005951 // Make sure all rows are gone in case we reuse a view.
5952 for (int rowId : rowIds) {
5953 contentView.setViewVisibility(rowId, View.GONE);
5954 }
5955
Daniel Sandler879c5e02012-04-17 16:46:51 -04005956 int i=0;
Selim Cinek07c80172016-04-21 16:40:47 -07005957 int topPadding = mBuilder.mContext.getResources().getDimensionPixelSize(
5958 R.dimen.notification_inbox_item_top_padding);
Selim Cinek247fa012016-02-18 09:50:48 -08005959 boolean first = true;
Selim Cinek07c80172016-04-21 16:40:47 -07005960 int onlyViewId = 0;
5961 int maxRows = rowIds.length;
5962 if (mBuilder.mActions.size() > 0) {
5963 maxRows--;
5964 }
5965 while (i < mTexts.size() && i < maxRows) {
Daniel Sandler879c5e02012-04-17 16:46:51 -04005966 CharSequence str = mTexts.get(i);
Selim Cinek07c80172016-04-21 16:40:47 -07005967 if (!TextUtils.isEmpty(str)) {
Daniel Sandler879c5e02012-04-17 16:46:51 -04005968 contentView.setViewVisibility(rowIds[i], View.VISIBLE);
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01005969 contentView.setTextViewText(rowIds[i], mBuilder.processLegacyText(str));
Selim Cinek7b9605b2017-01-19 17:36:00 -08005970 mBuilder.setTextViewColorSecondary(contentView, rowIds[i]);
Selim Cinek07c80172016-04-21 16:40:47 -07005971 contentView.setViewPadding(rowIds[i], 0, topPadding, 0, 0);
Selim Cinek247fa012016-02-18 09:50:48 -08005972 handleInboxImageMargin(contentView, rowIds[i], first);
Selim Cinek07c80172016-04-21 16:40:47 -07005973 if (first) {
5974 onlyViewId = rowIds[i];
5975 } else {
5976 onlyViewId = 0;
5977 }
Selim Cinek247fa012016-02-18 09:50:48 -08005978 first = false;
Daniel Sandler879c5e02012-04-17 16:46:51 -04005979 }
5980 i++;
5981 }
Selim Cinek07c80172016-04-21 16:40:47 -07005982 if (onlyViewId != 0) {
5983 // We only have 1 entry, lets make it look like the normal Text of a Bigtext
5984 topPadding = mBuilder.mContext.getResources().getDimensionPixelSize(
5985 R.dimen.notification_text_margin_top);
5986 contentView.setViewPadding(onlyViewId, 0, topPadding, 0, 0);
5987 }
Selim Cinek1e0bf612015-11-20 15:57:26 -08005988
Daniel Sandler879c5e02012-04-17 16:46:51 -04005989 return contentView;
5990 }
Selim Cinek1e0bf612015-11-20 15:57:26 -08005991
Selim Cinek247fa012016-02-18 09:50:48 -08005992 private void handleInboxImageMargin(RemoteViews contentView, int id, boolean first) {
Selim Cinek1e0bf612015-11-20 15:57:26 -08005993 int endMargin = 0;
Selim Cinek247fa012016-02-18 09:50:48 -08005994 if (first) {
5995 final int max = mBuilder.mN.extras.getInt(EXTRA_PROGRESS_MAX, 0);
5996 final boolean ind = mBuilder.mN.extras.getBoolean(EXTRA_PROGRESS_INDETERMINATE);
5997 boolean hasProgress = max != 0 || ind;
Selim Cinek279fa862016-06-14 10:57:25 -07005998 if (mBuilder.mN.hasLargeIcon() && !hasProgress) {
Adrian Roos2d5dbba2016-06-08 17:11:53 -07005999 endMargin = R.dimen.notification_content_picture_margin;
Selim Cinek247fa012016-02-18 09:50:48 -08006000 }
Selim Cinek1e0bf612015-11-20 15:57:26 -08006001 }
Adrian Roos2d5dbba2016-06-08 17:11:53 -07006002 contentView.setViewLayoutMarginEndDimen(id, endMargin);
Selim Cinek1e0bf612015-11-20 15:57:26 -08006003 }
Daniel Sandler879c5e02012-04-17 16:46:51 -04006004 }
Dan Sandler842dd772014-05-15 09:36:47 -04006005
6006 /**
6007 * Notification style for media playback notifications.
6008 *
6009 * In the expanded form, {@link Notification#bigContentView}, up to 5
6010 * {@link Notification.Action}s specified with
Dan Sandler86647982015-05-13 23:41:13 -04006011 * {@link Notification.Builder#addAction(Action) addAction} will be
Dan Sandler842dd772014-05-15 09:36:47 -04006012 * shown as icon-only pushbuttons, suitable for transport controls. The Bitmap given to
6013 * {@link Notification.Builder#setLargeIcon(android.graphics.Bitmap) setLargeIcon()} will be
6014 * treated as album artwork.
Selim Cinek99104832017-01-25 14:47:33 -08006015 * <p>
Dan Sandler842dd772014-05-15 09:36:47 -04006016 * Unlike the other styles provided here, MediaStyle can also modify the standard-size
6017 * {@link Notification#contentView}; by providing action indices to
Christoph Studerfde6f4d2014-12-12 13:23:26 +01006018 * {@link #setShowActionsInCompactView(int...)} you can promote up to 3 actions to be displayed
Dan Sandler842dd772014-05-15 09:36:47 -04006019 * in the standard view alongside the usual content.
Selim Cinek99104832017-01-25 14:47:33 -08006020 * <p>
Bryan Mawhinney6be8de32014-07-18 10:35:12 +01006021 * Notifications created with MediaStyle will have their category set to
6022 * {@link Notification#CATEGORY_TRANSPORT CATEGORY_TRANSPORT} unless you set a different
6023 * category using {@link Notification.Builder#setCategory(String) setCategory()}.
Selim Cinek99104832017-01-25 14:47:33 -08006024 * <p>
Jeff Browndba34ba2014-06-24 20:46:03 -07006025 * Finally, if you attach a {@link android.media.session.MediaSession.Token} using
6026 * {@link android.app.Notification.MediaStyle#setMediaSession(MediaSession.Token)},
Dan Sandler842dd772014-05-15 09:36:47 -04006027 * the System UI can identify this as a notification representing an active media session
6028 * and respond accordingly (by showing album artwork in the lockscreen, for example).
6029 *
Selim Cinek99104832017-01-25 14:47:33 -08006030 * <p>
6031 * Starting at {@link android.os.Build.VERSION_CODES#O Android O} any notification that has a
6032 * media session attached with {@link #setMediaSession(MediaSession.Token)} will be colorized.
6033 * You can opt-out of this behavior by using {@link Notification.Builder#setColorized(boolean)}.
6034 * <p>
6035 *
Dan Sandler842dd772014-05-15 09:36:47 -04006036 * To use this style with your Notification, feed it to
6037 * {@link Notification.Builder#setStyle(android.app.Notification.Style)} like so:
6038 * <pre class="prettyprint">
6039 * Notification noti = new Notification.Builder()
6040 * .setSmallIcon(R.drawable.ic_stat_player)
Christoph Studere935fe92014-11-24 14:18:06 +01006041 * .setContentTitle(&quot;Track title&quot;)
6042 * .setContentText(&quot;Artist - Album&quot;)
6043 * .setLargeIcon(albumArtBitmap))
Bryan Mawhinney6be8de32014-07-18 10:35:12 +01006044 * .setStyle(<b>new Notification.MediaStyle()</b>
6045 * .setMediaSession(mySession))
Dan Sandler842dd772014-05-15 09:36:47 -04006046 * .build();
6047 * </pre>
6048 *
6049 * @see Notification#bigContentView
Selim Cinek99104832017-01-25 14:47:33 -08006050 * @see Notification.Builder#setColorized(boolean)
Dan Sandler842dd772014-05-15 09:36:47 -04006051 */
6052 public static class MediaStyle extends Style {
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02006053 static final int MAX_MEDIA_BUTTONS_IN_COMPACT = 3;
Dan Sandler842dd772014-05-15 09:36:47 -04006054 static final int MAX_MEDIA_BUTTONS = 5;
6055
6056 private int[] mActionsToShowInCompact = null;
Jeff Browndba34ba2014-06-24 20:46:03 -07006057 private MediaSession.Token mToken;
Dan Sandler842dd772014-05-15 09:36:47 -04006058
6059 public MediaStyle() {
6060 }
6061
Adrian Roosf5faf9d2016-05-23 13:56:15 -07006062 /**
6063 * @deprecated use {@code MediaStyle()}.
6064 */
6065 @Deprecated
Dan Sandler842dd772014-05-15 09:36:47 -04006066 public MediaStyle(Builder builder) {
6067 setBuilder(builder);
6068 }
6069
6070 /**
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02006071 * 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 -04006072 * notification view.
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02006073 *
6074 * @param actions the indices of the actions to show in the compact notification view
Dan Sandler842dd772014-05-15 09:36:47 -04006075 */
6076 public MediaStyle setShowActionsInCompactView(int...actions) {
6077 mActionsToShowInCompact = actions;
6078 return this;
6079 }
6080
6081 /**
Jeff Browndba34ba2014-06-24 20:46:03 -07006082 * Attach a {@link android.media.session.MediaSession.Token} to this Notification
6083 * to provide additional playback information and control to the SystemUI.
Dan Sandler842dd772014-05-15 09:36:47 -04006084 */
Jeff Browndba34ba2014-06-24 20:46:03 -07006085 public MediaStyle setMediaSession(MediaSession.Token token) {
Dan Sandler842dd772014-05-15 09:36:47 -04006086 mToken = token;
6087 return this;
6088 }
6089
Christoph Studer4600f9b2014-07-22 22:44:43 +02006090 /**
6091 * @hide
6092 */
Dan Sandler842dd772014-05-15 09:36:47 -04006093 @Override
6094 public Notification buildStyled(Notification wip) {
Christoph Studer4600f9b2014-07-22 22:44:43 +02006095 super.buildStyled(wip);
Bryan Mawhinney6be8de32014-07-18 10:35:12 +01006096 if (wip.category == null) {
6097 wip.category = Notification.CATEGORY_TRANSPORT;
6098 }
Dan Sandler842dd772014-05-15 09:36:47 -04006099 return wip;
6100 }
6101
Christoph Studer4600f9b2014-07-22 22:44:43 +02006102 /**
6103 * @hide
6104 */
6105 @Override
Selim Cinek7d1009b2017-01-25 15:28:28 -08006106 public RemoteViews makeContentView(boolean increasedHeight) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006107 return makeMediaContentView();
Christoph Studer4600f9b2014-07-22 22:44:43 +02006108 }
6109
6110 /**
6111 * @hide
6112 */
6113 @Override
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006114 public RemoteViews makeBigContentView() {
6115 return makeMediaBigContentView();
Christoph Studer4600f9b2014-07-22 22:44:43 +02006116 }
6117
Selim Cinekcc10bfb2016-02-10 16:24:21 -08006118 /**
6119 * @hide
6120 */
6121 @Override
Selim Cinek87ed69b2017-02-09 15:59:43 -08006122 public RemoteViews makeHeadsUpContentView(boolean increasedHeight) {
Selim Cinekcc10bfb2016-02-10 16:24:21 -08006123 RemoteViews expanded = makeMediaBigContentView();
6124 return expanded != null ? expanded : makeMediaContentView();
6125 }
6126
Dan Sandler842dd772014-05-15 09:36:47 -04006127 /** @hide */
6128 @Override
6129 public void addExtras(Bundle extras) {
6130 super.addExtras(extras);
6131
6132 if (mToken != null) {
6133 extras.putParcelable(EXTRA_MEDIA_SESSION, mToken);
6134 }
Bryan Mawhinneye191f902014-07-22 12:50:09 +01006135 if (mActionsToShowInCompact != null) {
6136 extras.putIntArray(EXTRA_COMPACT_ACTIONS, mActionsToShowInCompact);
6137 }
Dan Sandler842dd772014-05-15 09:36:47 -04006138 }
6139
Christoph Studer4600f9b2014-07-22 22:44:43 +02006140 /**
6141 * @hide
6142 */
6143 @Override
6144 protected void restoreFromExtras(Bundle extras) {
6145 super.restoreFromExtras(extras);
6146
6147 if (extras.containsKey(EXTRA_MEDIA_SESSION)) {
6148 mToken = extras.getParcelable(EXTRA_MEDIA_SESSION);
6149 }
6150 if (extras.containsKey(EXTRA_COMPACT_ACTIONS)) {
6151 mActionsToShowInCompact = extras.getIntArray(EXTRA_COMPACT_ACTIONS);
6152 }
6153 }
6154
Selim Cinek5bf069a2015-11-10 19:14:27 -05006155 private RemoteViews generateMediaActionButton(Action action, int color) {
Dan Sandler842dd772014-05-15 09:36:47 -04006156 final boolean tombstone = (action.actionIntent == null);
Selim Cinekf33b1112015-07-15 17:45:11 -07006157 RemoteViews button = new BuilderRemoteViews(mBuilder.mContext.getApplicationInfo(),
Alan Viverette3cb07a462014-06-06 14:19:53 -07006158 R.layout.notification_material_media_action);
Dan Sandler68079d52015-07-22 10:45:30 -04006159 button.setImageViewIcon(R.id.action0, action.getIcon());
Selim Cinek5bf069a2015-11-10 19:14:27 -05006160 button.setDrawableParameters(R.id.action0, false, -1, color, PorterDuff.Mode.SRC_ATOP,
6161 -1);
Dan Sandler842dd772014-05-15 09:36:47 -04006162 if (!tombstone) {
6163 button.setOnClickPendingIntent(R.id.action0, action.actionIntent);
6164 }
6165 button.setContentDescription(R.id.action0, action.title);
6166 return button;
6167 }
6168
6169 private RemoteViews makeMediaContentView() {
6170 RemoteViews view = mBuilder.applyStandardTemplate(
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02006171 R.layout.notification_template_material_media, false /* hasProgress */);
Dan Sandler842dd772014-05-15 09:36:47 -04006172
6173 final int numActions = mBuilder.mActions.size();
6174 final int N = mActionsToShowInCompact == null
6175 ? 0
6176 : Math.min(mActionsToShowInCompact.length, MAX_MEDIA_BUTTONS_IN_COMPACT);
6177 if (N > 0) {
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02006178 view.removeAllViews(com.android.internal.R.id.media_actions);
Dan Sandler842dd772014-05-15 09:36:47 -04006179 for (int i = 0; i < N; i++) {
6180 if (i >= numActions) {
6181 throw new IllegalArgumentException(String.format(
6182 "setShowActionsInCompactView: action %d out of bounds (max %d)",
6183 i, numActions - 1));
6184 }
6185
6186 final Action action = mBuilder.mActions.get(mActionsToShowInCompact[i]);
Selim Cinek5bf069a2015-11-10 19:14:27 -05006187 final RemoteViews button = generateMediaActionButton(action,
Selim Cinek99104832017-01-25 14:47:33 -08006188 getPrimaryHighlightColor());
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02006189 view.addView(com.android.internal.R.id.media_actions, button);
Dan Sandler842dd772014-05-15 09:36:47 -04006190 }
6191 }
Selim Cinekfdc738f2016-01-27 20:04:27 -08006192 handleImage(view);
6193 // handle the content margin
Adrian Roos2d5dbba2016-06-08 17:11:53 -07006194 int endMargin = R.dimen.notification_content_margin_end;
Selim Cinek279fa862016-06-14 10:57:25 -07006195 if (mBuilder.mN.hasLargeIcon()) {
Adrian Roos2d5dbba2016-06-08 17:11:53 -07006196 endMargin = R.dimen.notification_content_plus_picture_margin_end;
Selim Cinekfdc738f2016-01-27 20:04:27 -08006197 }
Adrian Roos2d5dbba2016-06-08 17:11:53 -07006198 view.setViewLayoutMarginEndDimen(R.id.notification_main_column, endMargin);
Dan Sandler842dd772014-05-15 09:36:47 -04006199 return view;
6200 }
6201
Selim Cinek99104832017-01-25 14:47:33 -08006202 private int getPrimaryHighlightColor() {
6203 return mBuilder.getPrimaryHighlightColor();
6204 }
6205
Dan Sandler842dd772014-05-15 09:36:47 -04006206 private RemoteViews makeMediaBigContentView() {
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02006207 final int actionCount = Math.min(mBuilder.mActions.size(), MAX_MEDIA_BUTTONS);
Selim Cinekcc10bfb2016-02-10 16:24:21 -08006208 // Dont add an expanded view if there is no more content to be revealed
6209 int actionsInCompact = mActionsToShowInCompact == null
6210 ? 0
6211 : Math.min(mActionsToShowInCompact.length, MAX_MEDIA_BUTTONS_IN_COMPACT);
Selim Cinek279fa862016-06-14 10:57:25 -07006212 if (!mBuilder.mN.hasLargeIcon() && actionCount <= actionsInCompact) {
Selim Cinekcc10bfb2016-02-10 16:24:21 -08006213 return null;
6214 }
Selim Cinek5bf069a2015-11-10 19:14:27 -05006215 RemoteViews big = mBuilder.applyStandardTemplate(
6216 R.layout.notification_template_material_big_media,
6217 false);
Dan Sandler842dd772014-05-15 09:36:47 -04006218
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02006219 if (actionCount > 0) {
6220 big.removeAllViews(com.android.internal.R.id.media_actions);
6221 for (int i = 0; i < actionCount; i++) {
Selim Cinek5bf069a2015-11-10 19:14:27 -05006222 final RemoteViews button = generateMediaActionButton(mBuilder.mActions.get(i),
Selim Cinek99104832017-01-25 14:47:33 -08006223 getPrimaryHighlightColor());
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02006224 big.addView(com.android.internal.R.id.media_actions, button);
Dan Sandler842dd772014-05-15 09:36:47 -04006225 }
6226 }
Selim Cinek5bf069a2015-11-10 19:14:27 -05006227 handleImage(big);
Dan Sandler842dd772014-05-15 09:36:47 -04006228 return big;
6229 }
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02006230
Selim Cinek5bf069a2015-11-10 19:14:27 -05006231 private void handleImage(RemoteViews contentView) {
Selim Cinek279fa862016-06-14 10:57:25 -07006232 if (mBuilder.mN.hasLargeIcon()) {
Adrian Roos2d5dbba2016-06-08 17:11:53 -07006233 contentView.setViewLayoutMarginEndDimen(R.id.line1, 0);
6234 contentView.setViewLayoutMarginEndDimen(R.id.text, 0);
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02006235 }
6236 }
6237
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02006238 /**
6239 * @hide
6240 */
6241 @Override
6242 protected boolean hasProgress() {
6243 return false;
6244 }
Dan Sandler842dd772014-05-15 09:36:47 -04006245 }
Griff Hazen61a9e862014-05-22 16:05:19 -07006246
Selim Cinek593610c2016-02-16 18:42:57 -08006247 /**
6248 * Notification style for custom views that are decorated by the system
6249 *
6250 * <p>Instead of providing a notification that is completely custom, a developer can set this
6251 * style and still obtain system decorations like the notification header with the expand
6252 * affordance and actions.
6253 *
6254 * <p>Use {@link android.app.Notification.Builder#setCustomContentView(RemoteViews)},
6255 * {@link android.app.Notification.Builder#setCustomBigContentView(RemoteViews)} and
6256 * {@link android.app.Notification.Builder#setCustomHeadsUpContentView(RemoteViews)} to set the
6257 * corresponding custom views to display.
6258 *
6259 * To use this style with your Notification, feed it to
6260 * {@link Notification.Builder#setStyle(android.app.Notification.Style)} like so:
6261 * <pre class="prettyprint">
6262 * Notification noti = new Notification.Builder()
6263 * .setSmallIcon(R.drawable.ic_stat_player)
6264 * .setLargeIcon(albumArtBitmap))
6265 * .setCustomContentView(contentView);
6266 * .setStyle(<b>new Notification.DecoratedCustomViewStyle()</b>)
6267 * .build();
6268 * </pre>
6269 */
6270 public static class DecoratedCustomViewStyle extends Style {
6271
6272 public DecoratedCustomViewStyle() {
6273 }
6274
Selim Cinek593610c2016-02-16 18:42:57 -08006275 /**
6276 * @hide
6277 */
6278 public boolean displayCustomViewInline() {
6279 return true;
6280 }
6281
6282 /**
6283 * @hide
6284 */
6285 @Override
Selim Cinek7d1009b2017-01-25 15:28:28 -08006286 public RemoteViews makeContentView(boolean increasedHeight) {
Selim Cinek593610c2016-02-16 18:42:57 -08006287 return makeStandardTemplateWithCustomContent(mBuilder.mN.contentView);
6288 }
6289
6290 /**
6291 * @hide
6292 */
6293 @Override
6294 public RemoteViews makeBigContentView() {
6295 return makeDecoratedBigContentView();
6296 }
6297
6298 /**
6299 * @hide
6300 */
6301 @Override
Selim Cinek87ed69b2017-02-09 15:59:43 -08006302 public RemoteViews makeHeadsUpContentView(boolean increasedHeight) {
Selim Cinek593610c2016-02-16 18:42:57 -08006303 return makeDecoratedHeadsUpContentView();
6304 }
6305
Selim Cinek593610c2016-02-16 18:42:57 -08006306 private RemoteViews makeDecoratedHeadsUpContentView() {
6307 RemoteViews headsUpContentView = mBuilder.mN.headsUpContentView == null
6308 ? mBuilder.mN.contentView
6309 : mBuilder.mN.headsUpContentView;
6310 if (mBuilder.mActions.size() == 0) {
6311 return makeStandardTemplateWithCustomContent(headsUpContentView);
6312 }
6313 RemoteViews remoteViews = mBuilder.applyStandardTemplateWithActions(
6314 mBuilder.getBigBaseLayoutResource());
Selim Cinek247fa012016-02-18 09:50:48 -08006315 buildIntoRemoteViewContent(remoteViews, headsUpContentView);
Selim Cinek593610c2016-02-16 18:42:57 -08006316 return remoteViews;
6317 }
6318
Selim Cinek593610c2016-02-16 18:42:57 -08006319 private RemoteViews makeStandardTemplateWithCustomContent(RemoteViews customContent) {
6320 RemoteViews remoteViews = mBuilder.applyStandardTemplate(
6321 mBuilder.getBaseLayoutResource());
Selim Cinek247fa012016-02-18 09:50:48 -08006322 buildIntoRemoteViewContent(remoteViews, customContent);
Selim Cinek593610c2016-02-16 18:42:57 -08006323 return remoteViews;
6324 }
6325
Selim Cinek593610c2016-02-16 18:42:57 -08006326 private RemoteViews makeDecoratedBigContentView() {
6327 RemoteViews bigContentView = mBuilder.mN.bigContentView == null
6328 ? mBuilder.mN.contentView
6329 : mBuilder.mN.bigContentView;
6330 if (mBuilder.mActions.size() == 0) {
6331 return makeStandardTemplateWithCustomContent(bigContentView);
6332 }
6333 RemoteViews remoteViews = mBuilder.applyStandardTemplateWithActions(
6334 mBuilder.getBigBaseLayoutResource());
Selim Cinek247fa012016-02-18 09:50:48 -08006335 buildIntoRemoteViewContent(remoteViews, bigContentView);
Selim Cinek593610c2016-02-16 18:42:57 -08006336 return remoteViews;
6337 }
Selim Cinek247fa012016-02-18 09:50:48 -08006338
6339 private void buildIntoRemoteViewContent(RemoteViews remoteViews,
6340 RemoteViews customContent) {
Adrian Roos5081c0d2016-02-26 16:04:19 -08006341 if (customContent != null) {
Selim Cinekf91017e2016-03-14 12:25:09 -07006342 // Need to clone customContent before adding, because otherwise it can no longer be
6343 // parceled independently of remoteViews.
Adrian Roos5081c0d2016-02-26 16:04:19 -08006344 customContent = customContent.clone();
Selim Cinekf91017e2016-03-14 12:25:09 -07006345 remoteViews.removeAllViews(R.id.notification_main_column);
6346 remoteViews.addView(R.id.notification_main_column, customContent);
Adrian Roos5081c0d2016-02-26 16:04:19 -08006347 }
Selim Cinek247fa012016-02-18 09:50:48 -08006348 // also update the end margin if there is an image
Adrian Roos2d5dbba2016-06-08 17:11:53 -07006349 int endMargin = R.dimen.notification_content_margin_end;
Selim Cinek279fa862016-06-14 10:57:25 -07006350 if (mBuilder.mN.hasLargeIcon()) {
Adrian Roos2d5dbba2016-06-08 17:11:53 -07006351 endMargin = R.dimen.notification_content_plus_picture_margin_end;
Selim Cinek247fa012016-02-18 09:50:48 -08006352 }
Adrian Roos2d5dbba2016-06-08 17:11:53 -07006353 remoteViews.setViewLayoutMarginEndDimen(R.id.notification_main_column, endMargin);
Selim Cinek247fa012016-02-18 09:50:48 -08006354 }
Selim Cinek593610c2016-02-16 18:42:57 -08006355 }
6356
Selim Cinek03eb3b72016-02-18 10:39:45 -08006357 /**
6358 * Notification style for media custom views that are decorated by the system
6359 *
6360 * <p>Instead of providing a media notification that is completely custom, a developer can set
6361 * this style and still obtain system decorations like the notification header with the expand
6362 * affordance and actions.
6363 *
6364 * <p>Use {@link android.app.Notification.Builder#setCustomContentView(RemoteViews)},
6365 * {@link android.app.Notification.Builder#setCustomBigContentView(RemoteViews)} and
6366 * {@link android.app.Notification.Builder#setCustomHeadsUpContentView(RemoteViews)} to set the
6367 * corresponding custom views to display.
Selim Cinek99104832017-01-25 14:47:33 -08006368 * <p>
6369 * Contrary to {@link MediaStyle} a developer has to opt-in to the colorizing of the
6370 * notification by using {@link Notification.Builder#setColorized(boolean)}.
6371 * <p>
Selim Cinek03eb3b72016-02-18 10:39:45 -08006372 * To use this style with your Notification, feed it to
6373 * {@link Notification.Builder#setStyle(android.app.Notification.Style)} like so:
6374 * <pre class="prettyprint">
6375 * Notification noti = new Notification.Builder()
6376 * .setSmallIcon(R.drawable.ic_stat_player)
6377 * .setLargeIcon(albumArtBitmap))
6378 * .setCustomContentView(contentView);
6379 * .setStyle(<b>new Notification.DecoratedMediaCustomViewStyle()</b>
6380 * .setMediaSession(mySession))
6381 * .build();
6382 * </pre>
6383 *
6384 * @see android.app.Notification.DecoratedCustomViewStyle
6385 * @see android.app.Notification.MediaStyle
6386 */
6387 public static class DecoratedMediaCustomViewStyle extends MediaStyle {
6388
6389 public DecoratedMediaCustomViewStyle() {
6390 }
6391
Selim Cinek03eb3b72016-02-18 10:39:45 -08006392 /**
6393 * @hide
6394 */
6395 public boolean displayCustomViewInline() {
6396 return true;
6397 }
6398
6399 /**
6400 * @hide
6401 */
6402 @Override
Selim Cinek7d1009b2017-01-25 15:28:28 -08006403 public RemoteViews makeContentView(boolean increasedHeight) {
6404 RemoteViews remoteViews = super.makeContentView(false /* increasedHeight */);
Selim Cinek03eb3b72016-02-18 10:39:45 -08006405 return buildIntoRemoteView(remoteViews, R.id.notification_content_container,
6406 mBuilder.mN.contentView);
6407 }
6408
6409 /**
6410 * @hide
6411 */
6412 @Override
6413 public RemoteViews makeBigContentView() {
6414 RemoteViews customRemoteView = mBuilder.mN.bigContentView != null
6415 ? mBuilder.mN.bigContentView
6416 : mBuilder.mN.contentView;
6417 return makeBigContentViewWithCustomContent(customRemoteView);
6418 }
6419
6420 private RemoteViews makeBigContentViewWithCustomContent(RemoteViews customRemoteView) {
6421 RemoteViews remoteViews = super.makeBigContentView();
6422 if (remoteViews != null) {
6423 return buildIntoRemoteView(remoteViews, R.id.notification_main_column,
6424 customRemoteView);
6425 } else if (customRemoteView != mBuilder.mN.contentView){
Selim Cinek7d1009b2017-01-25 15:28:28 -08006426 remoteViews = super.makeContentView(false /* increasedHeight */);
Selim Cinek03eb3b72016-02-18 10:39:45 -08006427 return buildIntoRemoteView(remoteViews, R.id.notification_content_container,
6428 customRemoteView);
6429 } else {
6430 return null;
6431 }
6432 }
6433
6434 /**
6435 * @hide
6436 */
6437 @Override
Selim Cinek87ed69b2017-02-09 15:59:43 -08006438 public RemoteViews makeHeadsUpContentView(boolean increasedHeight) {
Selim Cinek03eb3b72016-02-18 10:39:45 -08006439 RemoteViews customRemoteView = mBuilder.mN.headsUpContentView != null
6440 ? mBuilder.mN.headsUpContentView
6441 : mBuilder.mN.contentView;
6442 return makeBigContentViewWithCustomContent(customRemoteView);
6443 }
6444
6445 private RemoteViews buildIntoRemoteView(RemoteViews remoteViews, int id,
6446 RemoteViews customContent) {
Adrian Roos5081c0d2016-02-26 16:04:19 -08006447 if (customContent != null) {
Selim Cinekf91017e2016-03-14 12:25:09 -07006448 // Need to clone customContent before adding, because otherwise it can no longer be
6449 // parceled independently of remoteViews.
Adrian Roos5081c0d2016-02-26 16:04:19 -08006450 customContent = customContent.clone();
Selim Cinekf91017e2016-03-14 12:25:09 -07006451 remoteViews.removeAllViews(id);
6452 remoteViews.addView(id, customContent);
Adrian Roos5081c0d2016-02-26 16:04:19 -08006453 }
Selim Cinek03eb3b72016-02-18 10:39:45 -08006454 return remoteViews;
6455 }
6456 }
6457
Christoph Studer4600f9b2014-07-22 22:44:43 +02006458 // When adding a new Style subclass here, don't forget to update
6459 // Builder.getNotificationStyleClass.
6460
Griff Hazen61a9e862014-05-22 16:05:19 -07006461 /**
6462 * Extender interface for use with {@link Builder#extend}. Extenders may be used to add
6463 * metadata or change options on a notification builder.
6464 */
6465 public interface Extender {
6466 /**
6467 * Apply this extender to a notification builder.
6468 * @param builder the builder to be modified.
6469 * @return the build object for chaining.
6470 */
6471 public Builder extend(Builder builder);
6472 }
6473
6474 /**
6475 * Helper class to add wearable extensions to notifications.
6476 * <p class="note"> See
6477 * <a href="{@docRoot}wear/notifications/creating.html">Creating Notifications
6478 * for Android Wear</a> for more information on how to use this class.
6479 * <p>
6480 * To create a notification with wearable extensions:
6481 * <ol>
6482 * <li>Create a {@link android.app.Notification.Builder}, setting any desired
6483 * properties.
6484 * <li>Create a {@link android.app.Notification.WearableExtender}.
6485 * <li>Set wearable-specific properties using the
6486 * {@code add} and {@code set} methods of {@link android.app.Notification.WearableExtender}.
6487 * <li>Call {@link android.app.Notification.Builder#extend} to apply the extensions to a
6488 * notification.
6489 * <li>Post the notification to the notification system with the
6490 * {@code NotificationManager.notify(...)} methods.
6491 * </ol>
6492 *
6493 * <pre class="prettyprint">
6494 * Notification notif = new Notification.Builder(mContext)
6495 * .setContentTitle(&quot;New mail from &quot; + sender.toString())
6496 * .setContentText(subject)
6497 * .setSmallIcon(R.drawable.new_mail)
6498 * .extend(new Notification.WearableExtender()
6499 * .setContentIcon(R.drawable.new_mail))
6500 * .build();
6501 * NotificationManager notificationManger =
6502 * (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
6503 * notificationManger.notify(0, notif);</pre>
6504 *
6505 * <p>Wearable extensions can be accessed on an existing notification by using the
6506 * {@code WearableExtender(Notification)} constructor,
6507 * and then using the {@code get} methods to access values.
6508 *
6509 * <pre class="prettyprint">
6510 * Notification.WearableExtender wearableExtender = new Notification.WearableExtender(
6511 * notification);
Griff Hazen14f57992014-05-26 09:07:14 -07006512 * List&lt;Notification&gt; pages = wearableExtender.getPages();</pre>
Griff Hazen61a9e862014-05-22 16:05:19 -07006513 */
6514 public static final class WearableExtender implements Extender {
6515 /**
6516 * Sentinel value for an action index that is unset.
6517 */
6518 public static final int UNSET_ACTION_INDEX = -1;
6519
6520 /**
6521 * Size value for use with {@link #setCustomSizePreset} to show this notification with
6522 * default sizing.
6523 * <p>For custom display notifications created using {@link #setDisplayIntent},
Paul Soulosaa4f4bf2015-08-04 11:59:45 -07006524 * the default is {@link #SIZE_MEDIUM}. All other notifications size automatically based
Griff Hazen61a9e862014-05-22 16:05:19 -07006525 * on their content.
6526 */
6527 public static final int SIZE_DEFAULT = 0;
6528
6529 /**
6530 * Size value for use with {@link #setCustomSizePreset} to show this notification
6531 * with an extra small size.
6532 * <p>This value is only applicable for custom display notifications created using
6533 * {@link #setDisplayIntent}.
6534 */
6535 public static final int SIZE_XSMALL = 1;
6536
6537 /**
6538 * Size value for use with {@link #setCustomSizePreset} to show this notification
6539 * with a small size.
6540 * <p>This value is only applicable for custom display notifications created using
6541 * {@link #setDisplayIntent}.
6542 */
6543 public static final int SIZE_SMALL = 2;
6544
6545 /**
6546 * Size value for use with {@link #setCustomSizePreset} to show this notification
6547 * with a medium size.
6548 * <p>This value is only applicable for custom display notifications created using
6549 * {@link #setDisplayIntent}.
6550 */
6551 public static final int SIZE_MEDIUM = 3;
6552
6553 /**
6554 * Size value for use with {@link #setCustomSizePreset} to show this notification
6555 * with a large size.
6556 * <p>This value is only applicable for custom display notifications created using
6557 * {@link #setDisplayIntent}.
6558 */
6559 public static final int SIZE_LARGE = 4;
6560
Griff Hazend5f11f92014-05-27 15:40:09 -07006561 /**
6562 * Size value for use with {@link #setCustomSizePreset} to show this notification
6563 * full screen.
6564 * <p>This value is only applicable for custom display notifications created using
6565 * {@link #setDisplayIntent}.
6566 */
6567 public static final int SIZE_FULL_SCREEN = 5;
6568
Griff Hazen5f2edfc2014-09-29 16:28:44 -07006569 /**
6570 * Sentinel value for use with {@link #setHintScreenTimeout} to keep the screen on for a
6571 * short amount of time when this notification is displayed on the screen. This
6572 * is the default value.
6573 */
6574 public static final int SCREEN_TIMEOUT_SHORT = 0;
6575
6576 /**
6577 * Sentinel value for use with {@link #setHintScreenTimeout} to keep the screen on
6578 * for a longer amount of time when this notification is displayed on the screen.
6579 */
6580 public static final int SCREEN_TIMEOUT_LONG = -1;
6581
Griff Hazen61a9e862014-05-22 16:05:19 -07006582 /** Notification extra which contains wearable extensions */
6583 private static final String EXTRA_WEARABLE_EXTENSIONS = "android.wearable.EXTENSIONS";
6584
Pete Gastaf6781d2014-10-07 15:17:05 -04006585 // Keys within EXTRA_WEARABLE_EXTENSIONS for wearable options.
Griff Hazen61a9e862014-05-22 16:05:19 -07006586 private static final String KEY_ACTIONS = "actions";
6587 private static final String KEY_FLAGS = "flags";
6588 private static final String KEY_DISPLAY_INTENT = "displayIntent";
6589 private static final String KEY_PAGES = "pages";
6590 private static final String KEY_BACKGROUND = "background";
6591 private static final String KEY_CONTENT_ICON = "contentIcon";
6592 private static final String KEY_CONTENT_ICON_GRAVITY = "contentIconGravity";
6593 private static final String KEY_CONTENT_ACTION_INDEX = "contentActionIndex";
6594 private static final String KEY_CUSTOM_SIZE_PRESET = "customSizePreset";
6595 private static final String KEY_CUSTOM_CONTENT_HEIGHT = "customContentHeight";
6596 private static final String KEY_GRAVITY = "gravity";
Griff Hazen5f2edfc2014-09-29 16:28:44 -07006597 private static final String KEY_HINT_SCREEN_TIMEOUT = "hintScreenTimeout";
Nadia Benbernou948627e2016-04-14 14:41:08 -04006598 private static final String KEY_DISMISSAL_ID = "dismissalId";
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04006599 private static final String KEY_BRIDGE_TAG = "bridgeTag";
Griff Hazen61a9e862014-05-22 16:05:19 -07006600
6601 // Flags bitwise-ored to mFlags
6602 private static final int FLAG_CONTENT_INTENT_AVAILABLE_OFFLINE = 0x1;
6603 private static final int FLAG_HINT_HIDE_ICON = 1 << 1;
6604 private static final int FLAG_HINT_SHOW_BACKGROUND_ONLY = 1 << 2;
6605 private static final int FLAG_START_SCROLL_BOTTOM = 1 << 3;
Griff Hazen5f2edfc2014-09-29 16:28:44 -07006606 private static final int FLAG_HINT_AVOID_BACKGROUND_CLIPPING = 1 << 4;
Alex Hills4bcb06b2016-04-05 14:26:25 -04006607 private static final int FLAG_BIG_PICTURE_AMBIENT = 1 << 5;
Alex Hills9ab3a232016-04-05 14:54:56 -04006608 private static final int FLAG_HINT_CONTENT_INTENT_LAUNCHES_ACTIVITY = 1 << 6;
Griff Hazen61a9e862014-05-22 16:05:19 -07006609
6610 // Default value for flags integer
6611 private static final int DEFAULT_FLAGS = FLAG_CONTENT_INTENT_AVAILABLE_OFFLINE;
6612
6613 private static final int DEFAULT_CONTENT_ICON_GRAVITY = Gravity.END;
6614 private static final int DEFAULT_GRAVITY = Gravity.BOTTOM;
6615
6616 private ArrayList<Action> mActions = new ArrayList<Action>();
6617 private int mFlags = DEFAULT_FLAGS;
6618 private PendingIntent mDisplayIntent;
6619 private ArrayList<Notification> mPages = new ArrayList<Notification>();
6620 private Bitmap mBackground;
6621 private int mContentIcon;
6622 private int mContentIconGravity = DEFAULT_CONTENT_ICON_GRAVITY;
6623 private int mContentActionIndex = UNSET_ACTION_INDEX;
6624 private int mCustomSizePreset = SIZE_DEFAULT;
6625 private int mCustomContentHeight;
6626 private int mGravity = DEFAULT_GRAVITY;
Griff Hazen5f2edfc2014-09-29 16:28:44 -07006627 private int mHintScreenTimeout;
Nadia Benbernou948627e2016-04-14 14:41:08 -04006628 private String mDismissalId;
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04006629 private String mBridgeTag;
Griff Hazen61a9e862014-05-22 16:05:19 -07006630
6631 /**
6632 * Create a {@link android.app.Notification.WearableExtender} with default
6633 * options.
6634 */
6635 public WearableExtender() {
6636 }
6637
6638 public WearableExtender(Notification notif) {
6639 Bundle wearableBundle = notif.extras.getBundle(EXTRA_WEARABLE_EXTENSIONS);
6640 if (wearableBundle != null) {
6641 List<Action> actions = wearableBundle.getParcelableArrayList(KEY_ACTIONS);
6642 if (actions != null) {
6643 mActions.addAll(actions);
6644 }
6645
6646 mFlags = wearableBundle.getInt(KEY_FLAGS, DEFAULT_FLAGS);
6647 mDisplayIntent = wearableBundle.getParcelable(KEY_DISPLAY_INTENT);
6648
6649 Notification[] pages = getNotificationArrayFromBundle(
6650 wearableBundle, KEY_PAGES);
6651 if (pages != null) {
6652 Collections.addAll(mPages, pages);
6653 }
6654
6655 mBackground = wearableBundle.getParcelable(KEY_BACKGROUND);
6656 mContentIcon = wearableBundle.getInt(KEY_CONTENT_ICON);
6657 mContentIconGravity = wearableBundle.getInt(KEY_CONTENT_ICON_GRAVITY,
6658 DEFAULT_CONTENT_ICON_GRAVITY);
6659 mContentActionIndex = wearableBundle.getInt(KEY_CONTENT_ACTION_INDEX,
6660 UNSET_ACTION_INDEX);
6661 mCustomSizePreset = wearableBundle.getInt(KEY_CUSTOM_SIZE_PRESET,
6662 SIZE_DEFAULT);
6663 mCustomContentHeight = wearableBundle.getInt(KEY_CUSTOM_CONTENT_HEIGHT);
6664 mGravity = wearableBundle.getInt(KEY_GRAVITY, DEFAULT_GRAVITY);
Griff Hazen5f2edfc2014-09-29 16:28:44 -07006665 mHintScreenTimeout = wearableBundle.getInt(KEY_HINT_SCREEN_TIMEOUT);
Nadia Benbernou948627e2016-04-14 14:41:08 -04006666 mDismissalId = wearableBundle.getString(KEY_DISMISSAL_ID);
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04006667 mBridgeTag = wearableBundle.getString(KEY_BRIDGE_TAG);
Griff Hazen61a9e862014-05-22 16:05:19 -07006668 }
6669 }
6670
6671 /**
6672 * Apply wearable extensions to a notification that is being built. This is typically
6673 * called by the {@link android.app.Notification.Builder#extend} method of
6674 * {@link android.app.Notification.Builder}.
6675 */
6676 @Override
6677 public Notification.Builder extend(Notification.Builder builder) {
6678 Bundle wearableBundle = new Bundle();
6679
6680 if (!mActions.isEmpty()) {
6681 wearableBundle.putParcelableArrayList(KEY_ACTIONS, mActions);
6682 }
6683 if (mFlags != DEFAULT_FLAGS) {
6684 wearableBundle.putInt(KEY_FLAGS, mFlags);
6685 }
6686 if (mDisplayIntent != null) {
6687 wearableBundle.putParcelable(KEY_DISPLAY_INTENT, mDisplayIntent);
6688 }
6689 if (!mPages.isEmpty()) {
6690 wearableBundle.putParcelableArray(KEY_PAGES, mPages.toArray(
6691 new Notification[mPages.size()]));
6692 }
6693 if (mBackground != null) {
6694 wearableBundle.putParcelable(KEY_BACKGROUND, mBackground);
6695 }
6696 if (mContentIcon != 0) {
6697 wearableBundle.putInt(KEY_CONTENT_ICON, mContentIcon);
6698 }
6699 if (mContentIconGravity != DEFAULT_CONTENT_ICON_GRAVITY) {
6700 wearableBundle.putInt(KEY_CONTENT_ICON_GRAVITY, mContentIconGravity);
6701 }
6702 if (mContentActionIndex != UNSET_ACTION_INDEX) {
6703 wearableBundle.putInt(KEY_CONTENT_ACTION_INDEX,
6704 mContentActionIndex);
6705 }
6706 if (mCustomSizePreset != SIZE_DEFAULT) {
6707 wearableBundle.putInt(KEY_CUSTOM_SIZE_PRESET, mCustomSizePreset);
6708 }
6709 if (mCustomContentHeight != 0) {
6710 wearableBundle.putInt(KEY_CUSTOM_CONTENT_HEIGHT, mCustomContentHeight);
6711 }
6712 if (mGravity != DEFAULT_GRAVITY) {
6713 wearableBundle.putInt(KEY_GRAVITY, mGravity);
6714 }
Griff Hazen5f2edfc2014-09-29 16:28:44 -07006715 if (mHintScreenTimeout != 0) {
6716 wearableBundle.putInt(KEY_HINT_SCREEN_TIMEOUT, mHintScreenTimeout);
6717 }
Nadia Benbernou948627e2016-04-14 14:41:08 -04006718 if (mDismissalId != null) {
6719 wearableBundle.putString(KEY_DISMISSAL_ID, mDismissalId);
6720 }
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04006721 if (mBridgeTag != null) {
6722 wearableBundle.putString(KEY_BRIDGE_TAG, mBridgeTag);
6723 }
Griff Hazen61a9e862014-05-22 16:05:19 -07006724
6725 builder.getExtras().putBundle(EXTRA_WEARABLE_EXTENSIONS, wearableBundle);
6726 return builder;
6727 }
6728
6729 @Override
6730 public WearableExtender clone() {
6731 WearableExtender that = new WearableExtender();
6732 that.mActions = new ArrayList<Action>(this.mActions);
6733 that.mFlags = this.mFlags;
6734 that.mDisplayIntent = this.mDisplayIntent;
6735 that.mPages = new ArrayList<Notification>(this.mPages);
6736 that.mBackground = this.mBackground;
6737 that.mContentIcon = this.mContentIcon;
6738 that.mContentIconGravity = this.mContentIconGravity;
6739 that.mContentActionIndex = this.mContentActionIndex;
6740 that.mCustomSizePreset = this.mCustomSizePreset;
6741 that.mCustomContentHeight = this.mCustomContentHeight;
6742 that.mGravity = this.mGravity;
Griff Hazen5f2edfc2014-09-29 16:28:44 -07006743 that.mHintScreenTimeout = this.mHintScreenTimeout;
Nadia Benbernou948627e2016-04-14 14:41:08 -04006744 that.mDismissalId = this.mDismissalId;
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04006745 that.mBridgeTag = this.mBridgeTag;
Griff Hazen61a9e862014-05-22 16:05:19 -07006746 return that;
6747 }
6748
6749 /**
6750 * Add a wearable action to this notification.
6751 *
6752 * <p>When wearable actions are added using this method, the set of actions that
6753 * show on a wearable device splits from devices that only show actions added
6754 * using {@link android.app.Notification.Builder#addAction}. This allows for customization
6755 * of which actions display on different devices.
6756 *
6757 * @param action the action to add to this notification
6758 * @return this object for method chaining
6759 * @see android.app.Notification.Action
6760 */
6761 public WearableExtender addAction(Action action) {
6762 mActions.add(action);
6763 return this;
6764 }
6765
6766 /**
6767 * Adds wearable actions to this notification.
6768 *
6769 * <p>When wearable actions are added using this method, the set of actions that
6770 * show on a wearable device splits from devices that only show actions added
6771 * using {@link android.app.Notification.Builder#addAction}. This allows for customization
6772 * of which actions display on different devices.
6773 *
6774 * @param actions the actions to add to this notification
6775 * @return this object for method chaining
6776 * @see android.app.Notification.Action
6777 */
6778 public WearableExtender addActions(List<Action> actions) {
6779 mActions.addAll(actions);
6780 return this;
6781 }
6782
6783 /**
6784 * Clear all wearable actions present on this builder.
6785 * @return this object for method chaining.
6786 * @see #addAction
6787 */
6788 public WearableExtender clearActions() {
6789 mActions.clear();
6790 return this;
6791 }
6792
6793 /**
6794 * Get the wearable actions present on this notification.
6795 */
6796 public List<Action> getActions() {
6797 return mActions;
6798 }
6799
6800 /**
6801 * Set an intent to launch inside of an activity view when displaying
Griff Hazen14f57992014-05-26 09:07:14 -07006802 * this notification. The {@link PendingIntent} provided should be for an activity.
6803 *
6804 * <pre class="prettyprint">
6805 * Intent displayIntent = new Intent(context, MyDisplayActivity.class);
6806 * PendingIntent displayPendingIntent = PendingIntent.getActivity(context,
6807 * 0, displayIntent, PendingIntent.FLAG_UPDATE_CURRENT);
6808 * Notification notif = new Notification.Builder(context)
6809 * .extend(new Notification.WearableExtender()
6810 * .setDisplayIntent(displayPendingIntent)
6811 * .setCustomSizePreset(Notification.WearableExtender.SIZE_MEDIUM))
6812 * .build();</pre>
6813 *
6814 * <p>The activity to launch needs to allow embedding, must be exported, and
Griff Hazen831ca9d2014-06-17 00:38:38 -07006815 * should have an empty task affinity. It is also recommended to use the device
6816 * default light theme.
Griff Hazen14f57992014-05-26 09:07:14 -07006817 *
6818 * <p>Example AndroidManifest.xml entry:
6819 * <pre class="prettyprint">
6820 * &lt;activity android:name=&quot;com.example.MyDisplayActivity&quot;
6821 * android:exported=&quot;true&quot;
6822 * android:allowEmbedded=&quot;true&quot;
Griff Hazen831ca9d2014-06-17 00:38:38 -07006823 * android:taskAffinity=&quot;&quot;
6824 * android:theme=&quot;@android:style/Theme.DeviceDefault.Light&quot; /&gt;</pre>
Griff Hazen61a9e862014-05-22 16:05:19 -07006825 *
6826 * @param intent the {@link PendingIntent} for an activity
6827 * @return this object for method chaining
6828 * @see android.app.Notification.WearableExtender#getDisplayIntent
6829 */
6830 public WearableExtender setDisplayIntent(PendingIntent intent) {
6831 mDisplayIntent = intent;
6832 return this;
6833 }
6834
6835 /**
6836 * Get the intent to launch inside of an activity view when displaying this
6837 * notification. This {@code PendingIntent} should be for an activity.
6838 */
6839 public PendingIntent getDisplayIntent() {
6840 return mDisplayIntent;
6841 }
6842
6843 /**
6844 * Add an additional page of content to display with this notification. The current
6845 * notification forms the first page, and pages added using this function form
6846 * subsequent pages. This field can be used to separate a notification into multiple
6847 * sections.
6848 *
6849 * @param page the notification to add as another page
6850 * @return this object for method chaining
6851 * @see android.app.Notification.WearableExtender#getPages
6852 */
6853 public WearableExtender addPage(Notification page) {
6854 mPages.add(page);
6855 return this;
6856 }
6857
6858 /**
6859 * Add additional pages of content to display with this notification. The current
6860 * notification forms the first page, and pages added using this function form
6861 * subsequent pages. This field can be used to separate a notification into multiple
6862 * sections.
6863 *
6864 * @param pages a list of notifications
6865 * @return this object for method chaining
6866 * @see android.app.Notification.WearableExtender#getPages
6867 */
6868 public WearableExtender addPages(List<Notification> pages) {
6869 mPages.addAll(pages);
6870 return this;
6871 }
6872
6873 /**
6874 * Clear all additional pages present on this builder.
6875 * @return this object for method chaining.
6876 * @see #addPage
6877 */
6878 public WearableExtender clearPages() {
6879 mPages.clear();
6880 return this;
6881 }
6882
6883 /**
6884 * Get the array of additional pages of content for displaying this notification. The
6885 * current notification forms the first page, and elements within this array form
6886 * subsequent pages. This field can be used to separate a notification into multiple
6887 * sections.
6888 * @return the pages for this notification
6889 */
6890 public List<Notification> getPages() {
6891 return mPages;
6892 }
6893
6894 /**
6895 * Set a background image to be displayed behind the notification content.
6896 * Contrary to the {@link android.app.Notification.BigPictureStyle}, this background
6897 * will work with any notification style.
6898 *
6899 * @param background the background bitmap
6900 * @return this object for method chaining
6901 * @see android.app.Notification.WearableExtender#getBackground
6902 */
6903 public WearableExtender setBackground(Bitmap background) {
6904 mBackground = background;
6905 return this;
6906 }
6907
6908 /**
6909 * Get a background image to be displayed behind the notification content.
6910 * Contrary to the {@link android.app.Notification.BigPictureStyle}, this background
6911 * will work with any notification style.
6912 *
6913 * @return the background image
6914 * @see android.app.Notification.WearableExtender#setBackground
6915 */
6916 public Bitmap getBackground() {
6917 return mBackground;
6918 }
6919
6920 /**
6921 * Set an icon that goes with the content of this notification.
6922 */
6923 public WearableExtender setContentIcon(int icon) {
6924 mContentIcon = icon;
6925 return this;
6926 }
6927
6928 /**
6929 * Get an icon that goes with the content of this notification.
6930 */
6931 public int getContentIcon() {
6932 return mContentIcon;
6933 }
6934
6935 /**
6936 * Set the gravity that the content icon should have within the notification display.
6937 * Supported values include {@link android.view.Gravity#START} and
6938 * {@link android.view.Gravity#END}. The default value is {@link android.view.Gravity#END}.
6939 * @see #setContentIcon
6940 */
6941 public WearableExtender setContentIconGravity(int contentIconGravity) {
6942 mContentIconGravity = contentIconGravity;
6943 return this;
6944 }
6945
6946 /**
6947 * Get the gravity that the content icon should have within the notification display.
6948 * Supported values include {@link android.view.Gravity#START} and
6949 * {@link android.view.Gravity#END}. The default value is {@link android.view.Gravity#END}.
6950 * @see #getContentIcon
6951 */
6952 public int getContentIconGravity() {
6953 return mContentIconGravity;
6954 }
6955
6956 /**
6957 * Set an action from this notification's actions to be clickable with the content of
Griff Hazen14f57992014-05-26 09:07:14 -07006958 * this notification. This action will no longer display separately from the
6959 * notification's content.
6960 *
Griff Hazenca48d352014-05-28 22:37:13 -07006961 * <p>For notifications with multiple pages, child pages can also have content actions
Griff Hazen14f57992014-05-26 09:07:14 -07006962 * set, although the list of available actions comes from the main notification and not
6963 * from the child page's notification.
6964 *
6965 * @param actionIndex The index of the action to hoist onto the current notification page.
6966 * If wearable actions were added to the main notification, this index
6967 * will apply to that list, otherwise it will apply to the regular
6968 * actions list.
Griff Hazen61a9e862014-05-22 16:05:19 -07006969 */
6970 public WearableExtender setContentAction(int actionIndex) {
6971 mContentActionIndex = actionIndex;
6972 return this;
6973 }
6974
6975 /**
Griff Hazenca48d352014-05-28 22:37:13 -07006976 * Get the index of the notification action, if any, that was specified as being clickable
6977 * with the content of this notification. This action will no longer display separately
Griff Hazen14f57992014-05-26 09:07:14 -07006978 * from the notification's content.
Griff Hazen61a9e862014-05-22 16:05:19 -07006979 *
Griff Hazenca48d352014-05-28 22:37:13 -07006980 * <p>For notifications with multiple pages, child pages can also have content actions
Griff Hazen14f57992014-05-26 09:07:14 -07006981 * set, although the list of available actions comes from the main notification and not
6982 * from the child page's notification.
6983 *
6984 * <p>If wearable specific actions were added to the main notification, this index will
6985 * apply to that list, otherwise it will apply to the regular actions list.
Griff Hazenca48d352014-05-28 22:37:13 -07006986 *
6987 * @return the action index or {@link #UNSET_ACTION_INDEX} if no action was selected.
Griff Hazen61a9e862014-05-22 16:05:19 -07006988 */
6989 public int getContentAction() {
6990 return mContentActionIndex;
6991 }
6992
6993 /**
6994 * Set the gravity that this notification should have within the available viewport space.
6995 * Supported values include {@link android.view.Gravity#TOP},
6996 * {@link android.view.Gravity#CENTER_VERTICAL} and {@link android.view.Gravity#BOTTOM}.
6997 * The default value is {@link android.view.Gravity#BOTTOM}.
6998 */
6999 public WearableExtender setGravity(int gravity) {
7000 mGravity = gravity;
7001 return this;
7002 }
7003
7004 /**
7005 * Get the gravity that this notification should have within the available viewport space.
7006 * Supported values include {@link android.view.Gravity#TOP},
7007 * {@link android.view.Gravity#CENTER_VERTICAL} and {@link android.view.Gravity#BOTTOM}.
7008 * The default value is {@link android.view.Gravity#BOTTOM}.
7009 */
7010 public int getGravity() {
7011 return mGravity;
7012 }
7013
7014 /**
7015 * Set the custom size preset for the display of this notification out of the available
7016 * presets found in {@link android.app.Notification.WearableExtender}, e.g.
7017 * {@link #SIZE_LARGE}.
7018 * <p>Some custom size presets are only applicable for custom display notifications created
7019 * using {@link android.app.Notification.WearableExtender#setDisplayIntent}. Check the
7020 * documentation for the preset in question. See also
7021 * {@link #setCustomContentHeight} and {@link #getCustomSizePreset}.
7022 */
7023 public WearableExtender setCustomSizePreset(int sizePreset) {
7024 mCustomSizePreset = sizePreset;
7025 return this;
7026 }
7027
7028 /**
7029 * Get the custom size preset for the display of this notification out of the available
7030 * presets found in {@link android.app.Notification.WearableExtender}, e.g.
7031 * {@link #SIZE_LARGE}.
7032 * <p>Some custom size presets are only applicable for custom display notifications created
7033 * using {@link #setDisplayIntent}. Check the documentation for the preset in question.
7034 * See also {@link #setCustomContentHeight} and {@link #setCustomSizePreset}.
7035 */
7036 public int getCustomSizePreset() {
7037 return mCustomSizePreset;
7038 }
7039
7040 /**
7041 * Set the custom height in pixels for the display of this notification's content.
7042 * <p>This option is only available for custom display notifications created
7043 * using {@link android.app.Notification.WearableExtender#setDisplayIntent}. See also
7044 * {@link android.app.Notification.WearableExtender#setCustomSizePreset} and
7045 * {@link #getCustomContentHeight}.
7046 */
7047 public WearableExtender setCustomContentHeight(int height) {
7048 mCustomContentHeight = height;
7049 return this;
7050 }
7051
7052 /**
7053 * Get the custom height in pixels for the display of this notification's content.
7054 * <p>This option is only available for custom display notifications created
7055 * using {@link #setDisplayIntent}. See also {@link #setCustomSizePreset} and
7056 * {@link #setCustomContentHeight}.
7057 */
7058 public int getCustomContentHeight() {
7059 return mCustomContentHeight;
7060 }
7061
7062 /**
7063 * Set whether the scrolling position for the contents of this notification should start
7064 * at the bottom of the contents instead of the top when the contents are too long to
7065 * display within the screen. Default is false (start scroll at the top).
7066 */
7067 public WearableExtender setStartScrollBottom(boolean startScrollBottom) {
7068 setFlag(FLAG_START_SCROLL_BOTTOM, startScrollBottom);
7069 return this;
7070 }
7071
7072 /**
7073 * Get whether the scrolling position for the contents of this notification should start
7074 * at the bottom of the contents instead of the top when the contents are too long to
7075 * display within the screen. Default is false (start scroll at the top).
7076 */
7077 public boolean getStartScrollBottom() {
7078 return (mFlags & FLAG_START_SCROLL_BOTTOM) != 0;
7079 }
7080
7081 /**
7082 * Set whether the content intent is available when the wearable device is not connected
7083 * to a companion device. The user can still trigger this intent when the wearable device
7084 * is offline, but a visual hint will indicate that the content intent may not be available.
7085 * Defaults to true.
7086 */
7087 public WearableExtender setContentIntentAvailableOffline(
7088 boolean contentIntentAvailableOffline) {
7089 setFlag(FLAG_CONTENT_INTENT_AVAILABLE_OFFLINE, contentIntentAvailableOffline);
7090 return this;
7091 }
7092
7093 /**
7094 * Get whether the content intent is available when the wearable device is not connected
7095 * to a companion device. The user can still trigger this intent when the wearable device
7096 * is offline, but a visual hint will indicate that the content intent may not be available.
7097 * Defaults to true.
7098 */
7099 public boolean getContentIntentAvailableOffline() {
7100 return (mFlags & FLAG_CONTENT_INTENT_AVAILABLE_OFFLINE) != 0;
7101 }
7102
7103 /**
7104 * Set a hint that this notification's icon should not be displayed.
7105 * @param hintHideIcon {@code true} to hide the icon, {@code false} otherwise.
7106 * @return this object for method chaining
7107 */
7108 public WearableExtender setHintHideIcon(boolean hintHideIcon) {
7109 setFlag(FLAG_HINT_HIDE_ICON, hintHideIcon);
7110 return this;
7111 }
7112
7113 /**
7114 * Get a hint that this notification's icon should not be displayed.
7115 * @return {@code true} if this icon should not be displayed, false otherwise.
7116 * The default value is {@code false} if this was never set.
7117 */
7118 public boolean getHintHideIcon() {
7119 return (mFlags & FLAG_HINT_HIDE_ICON) != 0;
7120 }
7121
7122 /**
7123 * Set a visual hint that only the background image of this notification should be
7124 * displayed, and other semantic content should be hidden. This hint is only applicable
7125 * to sub-pages added using {@link #addPage}.
7126 */
7127 public WearableExtender setHintShowBackgroundOnly(boolean hintShowBackgroundOnly) {
7128 setFlag(FLAG_HINT_SHOW_BACKGROUND_ONLY, hintShowBackgroundOnly);
7129 return this;
7130 }
7131
7132 /**
7133 * Get a visual hint that only the background image of this notification should be
7134 * displayed, and other semantic content should be hidden. This hint is only applicable
7135 * to sub-pages added using {@link android.app.Notification.WearableExtender#addPage}.
7136 */
7137 public boolean getHintShowBackgroundOnly() {
7138 return (mFlags & FLAG_HINT_SHOW_BACKGROUND_ONLY) != 0;
7139 }
7140
Griff Hazen5f2edfc2014-09-29 16:28:44 -07007141 /**
Griff Hazen9c5be4e2014-11-17 17:47:50 -08007142 * Set a hint that this notification's background should not be clipped if possible,
7143 * and should instead be resized to fully display on the screen, retaining the aspect
7144 * ratio of the image. This can be useful for images like barcodes or qr codes.
Griff Hazen5f2edfc2014-09-29 16:28:44 -07007145 * @param hintAvoidBackgroundClipping {@code true} to avoid clipping if possible.
7146 * @return this object for method chaining
7147 */
7148 public WearableExtender setHintAvoidBackgroundClipping(
7149 boolean hintAvoidBackgroundClipping) {
7150 setFlag(FLAG_HINT_AVOID_BACKGROUND_CLIPPING, hintAvoidBackgroundClipping);
7151 return this;
7152 }
7153
7154 /**
Griff Hazen9c5be4e2014-11-17 17:47:50 -08007155 * Get a hint that this notification's background should not be clipped if possible,
7156 * and should instead be resized to fully display on the screen, retaining the aspect
7157 * ratio of the image. This can be useful for images like barcodes or qr codes.
Griff Hazen5f2edfc2014-09-29 16:28:44 -07007158 * @return {@code true} if it's ok if the background is clipped on the screen, false
7159 * otherwise. The default value is {@code false} if this was never set.
7160 */
7161 public boolean getHintAvoidBackgroundClipping() {
7162 return (mFlags & FLAG_HINT_AVOID_BACKGROUND_CLIPPING) != 0;
7163 }
7164
7165 /**
7166 * Set a hint that the screen should remain on for at least this duration when
7167 * this notification is displayed on the screen.
7168 * @param timeout The requested screen timeout in milliseconds. Can also be either
7169 * {@link #SCREEN_TIMEOUT_SHORT} or {@link #SCREEN_TIMEOUT_LONG}.
7170 * @return this object for method chaining
7171 */
7172 public WearableExtender setHintScreenTimeout(int timeout) {
7173 mHintScreenTimeout = timeout;
7174 return this;
7175 }
7176
7177 /**
7178 * Get the duration, in milliseconds, that the screen should remain on for
7179 * when this notification is displayed.
7180 * @return the duration in milliseconds if > 0, or either one of the sentinel values
7181 * {@link #SCREEN_TIMEOUT_SHORT} or {@link #SCREEN_TIMEOUT_LONG}.
7182 */
7183 public int getHintScreenTimeout() {
7184 return mHintScreenTimeout;
7185 }
7186
Alex Hills9ab3a232016-04-05 14:54:56 -04007187 /**
Alex Hills4bcb06b2016-04-05 14:26:25 -04007188 * Set a hint that this notification's {@link BigPictureStyle} (if present) should be
7189 * converted to low-bit and displayed in ambient mode, especially useful for barcodes and
7190 * qr codes, as well as other simple black-and-white tickets.
7191 * @param hintAmbientBigPicture {@code true} to enable converstion and ambient.
7192 * @return this object for method chaining
7193 */
7194 public WearableExtender setHintAmbientBigPicture(boolean hintAmbientBigPicture) {
7195 setFlag(FLAG_BIG_PICTURE_AMBIENT, hintAmbientBigPicture);
7196 return this;
7197 }
7198
7199 /**
7200 * Get a hint that this notification's {@link BigPictureStyle} (if present) should be
7201 * converted to low-bit and displayed in ambient mode, especially useful for barcodes and
7202 * qr codes, as well as other simple black-and-white tickets.
7203 * @return {@code true} if it should be displayed in ambient, false otherwise
7204 * otherwise. The default value is {@code false} if this was never set.
7205 */
7206 public boolean getHintAmbientBigPicture() {
7207 return (mFlags & FLAG_BIG_PICTURE_AMBIENT) != 0;
7208 }
7209
7210 /**
Alex Hills9ab3a232016-04-05 14:54:56 -04007211 * Set a hint that this notification's content intent will launch an {@link Activity}
7212 * directly, telling the platform that it can generate the appropriate transitions.
7213 * @param hintContentIntentLaunchesActivity {@code true} if the content intent will launch
7214 * an activity and transitions should be generated, false otherwise.
7215 * @return this object for method chaining
7216 */
7217 public WearableExtender setHintContentIntentLaunchesActivity(
7218 boolean hintContentIntentLaunchesActivity) {
7219 setFlag(FLAG_HINT_CONTENT_INTENT_LAUNCHES_ACTIVITY, hintContentIntentLaunchesActivity);
7220 return this;
7221 }
7222
7223 /**
7224 * Get a hint that this notification's content intent will launch an {@link Activity}
7225 * directly, telling the platform that it can generate the appropriate transitions
7226 * @return {@code true} if the content intent will launch an activity and transitions should
7227 * be generated, false otherwise. The default value is {@code false} if this was never set.
7228 */
7229 public boolean getHintContentIntentLaunchesActivity() {
7230 return (mFlags & FLAG_HINT_CONTENT_INTENT_LAUNCHES_ACTIVITY) != 0;
7231 }
7232
Nadia Benbernou948627e2016-04-14 14:41:08 -04007233 /**
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04007234 * Sets the dismissal id for this notification. If a notification is posted with a
7235 * dismissal id, then when that notification is canceled, notifications on other wearables
7236 * and the paired Android phone having that same dismissal id will also be canceled. See
Nadia Benbernou948627e2016-04-14 14:41:08 -04007237 * <a href="{@docRoot}wear/notifications/index.html">Adding Wearable Features to
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04007238 * Notifications</a> for more information.
Nadia Benbernou948627e2016-04-14 14:41:08 -04007239 * @param dismissalId the dismissal id of the notification.
7240 * @return this object for method chaining
7241 */
7242 public WearableExtender setDismissalId(String dismissalId) {
7243 mDismissalId = dismissalId;
7244 return this;
7245 }
7246
7247 /**
7248 * Returns the dismissal id of the notification.
7249 * @return the dismissal id of the notification or null if it has not been set.
7250 */
7251 public String getDismissalId() {
7252 return mDismissalId;
7253 }
7254
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04007255 /**
7256 * Sets a bridge tag for this notification. A bridge tag can be set for notifications
7257 * posted from a phone to provide finer-grained control on what notifications are bridged
7258 * to wearables. See <a href="{@docRoot}wear/notifications/index.html">Adding Wearable
7259 * Features to Notifications</a> for more information.
7260 * @param bridgeTag the bridge tag of the notification.
7261 * @return this object for method chaining
7262 */
7263 public WearableExtender setBridgeTag(String bridgeTag) {
7264 mBridgeTag = bridgeTag;
7265 return this;
7266 }
7267
7268 /**
7269 * Returns the bridge tag of the notification.
7270 * @return the bridge tag or null if not present.
7271 */
7272 public String getBridgeTag() {
7273 return mBridgeTag;
7274 }
7275
Griff Hazen61a9e862014-05-22 16:05:19 -07007276 private void setFlag(int mask, boolean value) {
7277 if (value) {
7278 mFlags |= mask;
7279 } else {
7280 mFlags &= ~mask;
7281 }
7282 }
7283 }
7284
7285 /**
Zhen Yu Song9d5528b2014-11-14 15:34:39 -08007286 * <p>Helper class to add Android Auto extensions to notifications. To create a notification
7287 * with car extensions:
7288 *
7289 * <ol>
7290 * <li>Create an {@link Notification.Builder}, setting any desired
7291 * properties.
7292 * <li>Create a {@link CarExtender}.
7293 * <li>Set car-specific properties using the {@code add} and {@code set} methods of
7294 * {@link CarExtender}.
7295 * <li>Call {@link Notification.Builder#extend(Notification.Extender)}
7296 * to apply the extensions to a notification.
7297 * </ol>
7298 *
7299 * <pre class="prettyprint">
7300 * Notification notification = new Notification.Builder(context)
7301 * ...
7302 * .extend(new CarExtender()
7303 * .set*(...))
7304 * .build();
7305 * </pre>
7306 *
7307 * <p>Car extensions can be accessed on an existing notification by using the
7308 * {@code CarExtender(Notification)} constructor, and then using the {@code get} methods
7309 * to access values.
7310 */
7311 public static final class CarExtender implements Extender {
7312 private static final String TAG = "CarExtender";
7313
7314 private static final String EXTRA_CAR_EXTENDER = "android.car.EXTENSIONS";
7315 private static final String EXTRA_LARGE_ICON = "large_icon";
7316 private static final String EXTRA_CONVERSATION = "car_conversation";
7317 private static final String EXTRA_COLOR = "app_color";
7318
7319 private Bitmap mLargeIcon;
7320 private UnreadConversation mUnreadConversation;
7321 private int mColor = Notification.COLOR_DEFAULT;
7322
7323 /**
7324 * Create a {@link CarExtender} with default options.
7325 */
7326 public CarExtender() {
7327 }
7328
7329 /**
7330 * Create a {@link CarExtender} from the CarExtender options of an existing Notification.
7331 *
7332 * @param notif The notification from which to copy options.
7333 */
7334 public CarExtender(Notification notif) {
7335 Bundle carBundle = notif.extras == null ?
7336 null : notif.extras.getBundle(EXTRA_CAR_EXTENDER);
7337 if (carBundle != null) {
7338 mLargeIcon = carBundle.getParcelable(EXTRA_LARGE_ICON);
7339 mColor = carBundle.getInt(EXTRA_COLOR, Notification.COLOR_DEFAULT);
7340
7341 Bundle b = carBundle.getBundle(EXTRA_CONVERSATION);
7342 mUnreadConversation = UnreadConversation.getUnreadConversationFromBundle(b);
7343 }
7344 }
7345
7346 /**
7347 * Apply car extensions to a notification that is being built. This is typically called by
7348 * the {@link Notification.Builder#extend(Notification.Extender)}
7349 * method of {@link Notification.Builder}.
7350 */
7351 @Override
7352 public Notification.Builder extend(Notification.Builder builder) {
7353 Bundle carExtensions = new Bundle();
7354
7355 if (mLargeIcon != null) {
7356 carExtensions.putParcelable(EXTRA_LARGE_ICON, mLargeIcon);
7357 }
7358 if (mColor != Notification.COLOR_DEFAULT) {
7359 carExtensions.putInt(EXTRA_COLOR, mColor);
7360 }
7361
7362 if (mUnreadConversation != null) {
7363 Bundle b = mUnreadConversation.getBundleForUnreadConversation();
7364 carExtensions.putBundle(EXTRA_CONVERSATION, b);
7365 }
7366
7367 builder.getExtras().putBundle(EXTRA_CAR_EXTENDER, carExtensions);
7368 return builder;
7369 }
7370
7371 /**
7372 * Sets the accent color to use when Android Auto presents the notification.
7373 *
7374 * Android Auto uses the color set with {@link Notification.Builder#setColor(int)}
7375 * to accent the displayed notification. However, not all colors are acceptable in an
7376 * automotive setting. This method can be used to override the color provided in the
7377 * notification in such a situation.
7378 */
Tor Norbye80756e32015-03-02 09:39:27 -08007379 public CarExtender setColor(@ColorInt int color) {
Zhen Yu Song9d5528b2014-11-14 15:34:39 -08007380 mColor = color;
7381 return this;
7382 }
7383
7384 /**
7385 * Gets the accent color.
7386 *
Julia Reynoldsd9228f12015-10-20 10:37:27 -04007387 * @see #setColor
Zhen Yu Song9d5528b2014-11-14 15:34:39 -08007388 */
Tor Norbye80756e32015-03-02 09:39:27 -08007389 @ColorInt
Zhen Yu Song9d5528b2014-11-14 15:34:39 -08007390 public int getColor() {
7391 return mColor;
7392 }
7393
7394 /**
7395 * Sets the large icon of the car notification.
7396 *
7397 * If no large icon is set in the extender, Android Auto will display the icon
7398 * specified by {@link Notification.Builder#setLargeIcon(android.graphics.Bitmap)}
7399 *
7400 * @param largeIcon The large icon to use in the car notification.
7401 * @return This object for method chaining.
7402 */
7403 public CarExtender setLargeIcon(Bitmap largeIcon) {
7404 mLargeIcon = largeIcon;
7405 return this;
7406 }
7407
7408 /**
7409 * Gets the large icon used in this car notification, or null if no icon has been set.
7410 *
7411 * @return The large icon for the car notification.
7412 * @see CarExtender#setLargeIcon
7413 */
7414 public Bitmap getLargeIcon() {
7415 return mLargeIcon;
7416 }
7417
7418 /**
7419 * Sets the unread conversation in a message notification.
7420 *
7421 * @param unreadConversation The unread part of the conversation this notification conveys.
7422 * @return This object for method chaining.
7423 */
7424 public CarExtender setUnreadConversation(UnreadConversation unreadConversation) {
7425 mUnreadConversation = unreadConversation;
7426 return this;
7427 }
7428
7429 /**
7430 * Returns the unread conversation conveyed by this notification.
7431 * @see #setUnreadConversation(UnreadConversation)
7432 */
7433 public UnreadConversation getUnreadConversation() {
7434 return mUnreadConversation;
7435 }
7436
7437 /**
7438 * A class which holds the unread messages from a conversation.
7439 */
7440 public static class UnreadConversation {
7441 private static final String KEY_AUTHOR = "author";
7442 private static final String KEY_TEXT = "text";
7443 private static final String KEY_MESSAGES = "messages";
7444 private static final String KEY_REMOTE_INPUT = "remote_input";
7445 private static final String KEY_ON_REPLY = "on_reply";
7446 private static final String KEY_ON_READ = "on_read";
7447 private static final String KEY_PARTICIPANTS = "participants";
7448 private static final String KEY_TIMESTAMP = "timestamp";
7449
7450 private final String[] mMessages;
7451 private final RemoteInput mRemoteInput;
7452 private final PendingIntent mReplyPendingIntent;
7453 private final PendingIntent mReadPendingIntent;
7454 private final String[] mParticipants;
7455 private final long mLatestTimestamp;
7456
7457 UnreadConversation(String[] messages, RemoteInput remoteInput,
7458 PendingIntent replyPendingIntent, PendingIntent readPendingIntent,
7459 String[] participants, long latestTimestamp) {
7460 mMessages = messages;
7461 mRemoteInput = remoteInput;
7462 mReadPendingIntent = readPendingIntent;
7463 mReplyPendingIntent = replyPendingIntent;
7464 mParticipants = participants;
7465 mLatestTimestamp = latestTimestamp;
7466 }
7467
7468 /**
7469 * Gets the list of messages conveyed by this notification.
7470 */
7471 public String[] getMessages() {
7472 return mMessages;
7473 }
7474
7475 /**
7476 * Gets the remote input that will be used to convey the response to a message list, or
7477 * null if no such remote input exists.
7478 */
7479 public RemoteInput getRemoteInput() {
7480 return mRemoteInput;
7481 }
7482
7483 /**
7484 * Gets the pending intent that will be triggered when the user replies to this
7485 * notification.
7486 */
7487 public PendingIntent getReplyPendingIntent() {
7488 return mReplyPendingIntent;
7489 }
7490
7491 /**
7492 * Gets the pending intent that Android Auto will send after it reads aloud all messages
7493 * in this object's message list.
7494 */
7495 public PendingIntent getReadPendingIntent() {
7496 return mReadPendingIntent;
7497 }
7498
7499 /**
7500 * Gets the participants in the conversation.
7501 */
7502 public String[] getParticipants() {
7503 return mParticipants;
7504 }
7505
7506 /**
7507 * Gets the firs participant in the conversation.
7508 */
7509 public String getParticipant() {
7510 return mParticipants.length > 0 ? mParticipants[0] : null;
7511 }
7512
7513 /**
7514 * Gets the timestamp of the conversation.
7515 */
7516 public long getLatestTimestamp() {
7517 return mLatestTimestamp;
7518 }
7519
7520 Bundle getBundleForUnreadConversation() {
7521 Bundle b = new Bundle();
7522 String author = null;
7523 if (mParticipants != null && mParticipants.length > 1) {
7524 author = mParticipants[0];
7525 }
7526 Parcelable[] messages = new Parcelable[mMessages.length];
7527 for (int i = 0; i < messages.length; i++) {
7528 Bundle m = new Bundle();
7529 m.putString(KEY_TEXT, mMessages[i]);
7530 m.putString(KEY_AUTHOR, author);
7531 messages[i] = m;
7532 }
7533 b.putParcelableArray(KEY_MESSAGES, messages);
7534 if (mRemoteInput != null) {
7535 b.putParcelable(KEY_REMOTE_INPUT, mRemoteInput);
7536 }
7537 b.putParcelable(KEY_ON_REPLY, mReplyPendingIntent);
7538 b.putParcelable(KEY_ON_READ, mReadPendingIntent);
7539 b.putStringArray(KEY_PARTICIPANTS, mParticipants);
7540 b.putLong(KEY_TIMESTAMP, mLatestTimestamp);
7541 return b;
7542 }
7543
7544 static UnreadConversation getUnreadConversationFromBundle(Bundle b) {
7545 if (b == null) {
7546 return null;
7547 }
7548 Parcelable[] parcelableMessages = b.getParcelableArray(KEY_MESSAGES);
7549 String[] messages = null;
7550 if (parcelableMessages != null) {
7551 String[] tmp = new String[parcelableMessages.length];
7552 boolean success = true;
7553 for (int i = 0; i < tmp.length; i++) {
7554 if (!(parcelableMessages[i] instanceof Bundle)) {
7555 success = false;
7556 break;
7557 }
7558 tmp[i] = ((Bundle) parcelableMessages[i]).getString(KEY_TEXT);
7559 if (tmp[i] == null) {
7560 success = false;
7561 break;
7562 }
7563 }
7564 if (success) {
7565 messages = tmp;
7566 } else {
7567 return null;
7568 }
7569 }
7570
7571 PendingIntent onRead = b.getParcelable(KEY_ON_READ);
7572 PendingIntent onReply = b.getParcelable(KEY_ON_REPLY);
7573
7574 RemoteInput remoteInput = b.getParcelable(KEY_REMOTE_INPUT);
7575
7576 String[] participants = b.getStringArray(KEY_PARTICIPANTS);
7577 if (participants == null || participants.length != 1) {
7578 return null;
7579 }
7580
7581 return new UnreadConversation(messages,
7582 remoteInput,
7583 onReply,
7584 onRead,
7585 participants, b.getLong(KEY_TIMESTAMP));
7586 }
7587 };
7588
7589 /**
7590 * Builder class for {@link CarExtender.UnreadConversation} objects.
7591 */
7592 public static class Builder {
7593 private final List<String> mMessages = new ArrayList<String>();
7594 private final String mParticipant;
7595 private RemoteInput mRemoteInput;
7596 private PendingIntent mReadPendingIntent;
7597 private PendingIntent mReplyPendingIntent;
7598 private long mLatestTimestamp;
7599
7600 /**
7601 * Constructs a new builder for {@link CarExtender.UnreadConversation}.
7602 *
7603 * @param name The name of the other participant in the conversation.
7604 */
7605 public Builder(String name) {
7606 mParticipant = name;
7607 }
7608
7609 /**
7610 * Appends a new unread message to the list of messages for this conversation.
7611 *
7612 * The messages should be added from oldest to newest.
7613 *
7614 * @param message The text of the new unread message.
7615 * @return This object for method chaining.
7616 */
7617 public Builder addMessage(String message) {
7618 mMessages.add(message);
7619 return this;
7620 }
7621
7622 /**
7623 * Sets the pending intent and remote input which will convey the reply to this
7624 * notification.
7625 *
7626 * @param pendingIntent The pending intent which will be triggered on a reply.
7627 * @param remoteInput The remote input parcelable which will carry the reply.
7628 * @return This object for method chaining.
7629 *
7630 * @see CarExtender.UnreadConversation#getRemoteInput
7631 * @see CarExtender.UnreadConversation#getReplyPendingIntent
7632 */
7633 public Builder setReplyAction(
7634 PendingIntent pendingIntent, RemoteInput remoteInput) {
7635 mRemoteInput = remoteInput;
7636 mReplyPendingIntent = pendingIntent;
7637
7638 return this;
7639 }
7640
7641 /**
7642 * Sets the pending intent that will be sent once the messages in this notification
7643 * are read.
7644 *
7645 * @param pendingIntent The pending intent to use.
7646 * @return This object for method chaining.
7647 */
7648 public Builder setReadPendingIntent(PendingIntent pendingIntent) {
7649 mReadPendingIntent = pendingIntent;
7650 return this;
7651 }
7652
7653 /**
7654 * Sets the timestamp of the most recent message in an unread conversation.
7655 *
7656 * If a messaging notification has been posted by your application and has not
7657 * yet been cancelled, posting a later notification with the same id and tag
7658 * but without a newer timestamp may result in Android Auto not displaying a
7659 * heads up notification for the later notification.
7660 *
7661 * @param timestamp The timestamp of the most recent message in the conversation.
7662 * @return This object for method chaining.
7663 */
7664 public Builder setLatestTimestamp(long timestamp) {
7665 mLatestTimestamp = timestamp;
7666 return this;
7667 }
7668
7669 /**
7670 * Builds a new unread conversation object.
7671 *
7672 * @return The new unread conversation object.
7673 */
7674 public UnreadConversation build() {
7675 String[] messages = mMessages.toArray(new String[mMessages.size()]);
7676 String[] participants = { mParticipant };
7677 return new UnreadConversation(messages, mRemoteInput, mReplyPendingIntent,
7678 mReadPendingIntent, participants, mLatestTimestamp);
7679 }
7680 }
7681 }
7682
7683 /**
Dmitri Plotnikov9e9cfd92016-12-21 10:52:26 -08007684 * <p>Helper class to add Android TV extensions to notifications. To create a notification
7685 * with a TV extension:
7686 *
7687 * <ol>
7688 * <li>Create an {@link Notification.Builder}, setting any desired properties.
7689 * <li>Create a {@link TvExtender}.
7690 * <li>Set TV-specific properties using the {@code set} methods of
7691 * {@link TvExtender}.
7692 * <li>Call {@link Notification.Builder#extend(Notification.Extender)}
7693 * to apply the extension to a notification.
7694 * </ol>
7695 *
7696 * <pre class="prettyprint">
7697 * Notification notification = new Notification.Builder(context)
7698 * ...
7699 * .extend(new TvExtender()
7700 * .set*(...))
7701 * .build();
7702 * </pre>
7703 *
7704 * <p>TV extensions can be accessed on an existing notification by using the
7705 * {@code TvExtender(Notification)} constructor, and then using the {@code get} methods
7706 * to access values.
7707 *
7708 * @hide
7709 */
7710 @SystemApi
7711 public static final class TvExtender implements Extender {
7712 private static final String TAG = "TvExtender";
7713
7714 private static final String EXTRA_TV_EXTENDER = "android.tv.EXTENSIONS";
7715 private static final String EXTRA_FLAGS = "flags";
7716 private static final String EXTRA_CONTENT_INTENT = "content_intent";
7717 private static final String EXTRA_DELETE_INTENT = "delete_intent";
Dmitri Plotnikovb8a04ee2017-01-20 10:39:22 -08007718 private static final String EXTRA_CHANNEL_ID = "channel_id";
Dmitri Plotnikov9e9cfd92016-12-21 10:52:26 -08007719
7720 // Flags bitwise-ored to mFlags
7721 private static final int FLAG_AVAILABLE_ON_TV = 0x1;
7722
7723 private int mFlags;
Dmitri Plotnikovb8a04ee2017-01-20 10:39:22 -08007724 private String mChannelId;
Dmitri Plotnikov9e9cfd92016-12-21 10:52:26 -08007725 private PendingIntent mContentIntent;
7726 private PendingIntent mDeleteIntent;
7727
7728 /**
7729 * Create a {@link TvExtender} with default options.
7730 */
7731 public TvExtender() {
7732 mFlags = FLAG_AVAILABLE_ON_TV;
7733 }
7734
7735 /**
7736 * Create a {@link TvExtender} from the TvExtender options of an existing Notification.
7737 *
7738 * @param notif The notification from which to copy options.
7739 */
7740 public TvExtender(Notification notif) {
7741 Bundle bundle = notif.extras == null ?
7742 null : notif.extras.getBundle(EXTRA_TV_EXTENDER);
7743 if (bundle != null) {
7744 mFlags = bundle.getInt(EXTRA_FLAGS);
Dmitri Plotnikovb8a04ee2017-01-20 10:39:22 -08007745 mChannelId = bundle.getString(EXTRA_CHANNEL_ID);
Dmitri Plotnikov9e9cfd92016-12-21 10:52:26 -08007746 mContentIntent = bundle.getParcelable(EXTRA_CONTENT_INTENT);
7747 mDeleteIntent = bundle.getParcelable(EXTRA_DELETE_INTENT);
7748 }
7749 }
7750
7751 /**
7752 * Apply a TV extension to a notification that is being built. This is typically called by
7753 * the {@link Notification.Builder#extend(Notification.Extender)}
7754 * method of {@link Notification.Builder}.
7755 */
7756 @Override
7757 public Notification.Builder extend(Notification.Builder builder) {
7758 Bundle bundle = new Bundle();
7759
7760 bundle.putInt(EXTRA_FLAGS, mFlags);
Dmitri Plotnikovb8a04ee2017-01-20 10:39:22 -08007761 bundle.putString(EXTRA_CHANNEL_ID, mChannelId);
Dmitri Plotnikov9e9cfd92016-12-21 10:52:26 -08007762 if (mContentIntent != null) {
7763 bundle.putParcelable(EXTRA_CONTENT_INTENT, mContentIntent);
7764 }
7765
7766 if (mDeleteIntent != null) {
7767 bundle.putParcelable(EXTRA_DELETE_INTENT, mDeleteIntent);
7768 }
7769
7770 builder.getExtras().putBundle(EXTRA_TV_EXTENDER, bundle);
7771 return builder;
7772 }
7773
7774 /**
7775 * Returns true if this notification should be shown on TV. This method return true
7776 * if the notification was extended with a TvExtender.
7777 */
7778 public boolean isAvailableOnTv() {
7779 return (mFlags & FLAG_AVAILABLE_ON_TV) != 0;
7780 }
7781
7782 /**
Dmitri Plotnikovb8a04ee2017-01-20 10:39:22 -08007783 * Specifies the channel the notification should be delivered on when shown on TV.
7784 * It can be different from the channel that the notification is delivered to when
7785 * posting on a non-TV device.
7786 */
7787 public TvExtender setChannel(String channelId) {
7788 mChannelId = channelId;
7789 return this;
7790 }
7791
7792 /**
7793 * Returns the id of the channel this notification posts to on TV.
7794 */
7795 public String getChannel() {
7796 return mChannelId;
7797 }
7798
7799 /**
Dmitri Plotnikov9e9cfd92016-12-21 10:52:26 -08007800 * Supplies a {@link PendingIntent} to be sent when the notification is selected on TV.
7801 * If provided, it is used instead of the content intent specified
7802 * at the level of Notification.
7803 */
7804 public TvExtender setContentIntent(PendingIntent intent) {
7805 mContentIntent = intent;
7806 return this;
7807 }
7808
7809 /**
7810 * Returns the TV-specific content intent. If this method returns null, the
7811 * main content intent on the notification should be used.
7812 *
7813 * @see {@link Notification#contentIntent}
7814 */
7815 public PendingIntent getContentIntent() {
7816 return mContentIntent;
7817 }
7818
7819 /**
7820 * Supplies a {@link PendingIntent} to send when the notification is cleared explicitly
7821 * by the user on TV. If provided, it is used instead of the delete intent specified
7822 * at the level of Notification.
7823 */
7824 public TvExtender setDeleteIntent(PendingIntent intent) {
7825 mDeleteIntent = intent;
7826 return this;
7827 }
7828
7829 /**
7830 * Returns the TV-specific delete intent. If this method returns null, the
7831 * main delete intent on the notification should be used.
7832 *
7833 * @see {@link Notification#deleteIntent}
7834 */
7835 public PendingIntent getDeleteIntent() {
7836 return mDeleteIntent;
7837 }
7838 }
7839
7840 /**
Griff Hazen61a9e862014-05-22 16:05:19 -07007841 * Get an array of Notification objects from a parcelable array bundle field.
7842 * Update the bundle to have a typed array so fetches in the future don't need
7843 * to do an array copy.
7844 */
7845 private static Notification[] getNotificationArrayFromBundle(Bundle bundle, String key) {
7846 Parcelable[] array = bundle.getParcelableArray(key);
7847 if (array instanceof Notification[] || array == null) {
7848 return (Notification[]) array;
7849 }
7850 Notification[] typedArray = Arrays.copyOf(array, array.length,
7851 Notification[].class);
7852 bundle.putParcelableArray(key, typedArray);
7853 return typedArray;
7854 }
Christoph Studer4600f9b2014-07-22 22:44:43 +02007855
7856 private static class BuilderRemoteViews extends RemoteViews {
7857 public BuilderRemoteViews(Parcel parcel) {
7858 super(parcel);
7859 }
7860
Kenny Guy77320062014-08-27 21:37:15 +01007861 public BuilderRemoteViews(ApplicationInfo appInfo, int layoutId) {
7862 super(appInfo, layoutId);
Christoph Studer4600f9b2014-07-22 22:44:43 +02007863 }
7864
7865 @Override
7866 public BuilderRemoteViews clone() {
7867 Parcel p = Parcel.obtain();
7868 writeToParcel(p, 0);
7869 p.setDataPosition(0);
7870 BuilderRemoteViews brv = new BuilderRemoteViews(p);
7871 p.recycle();
7872 return brv;
7873 }
7874 }
Adrian Roos70d7aa32017-01-11 15:39:06 -08007875
7876 private static class StandardTemplateParams {
7877 boolean hasProgress = true;
7878 boolean ambient = false;
7879 CharSequence title;
7880 CharSequence text;
7881
7882 final StandardTemplateParams reset() {
7883 hasProgress = true;
7884 ambient = false;
7885 title = null;
7886 text = null;
7887 return this;
7888 }
7889
7890 final StandardTemplateParams hasProgress(boolean hasProgress) {
7891 this.hasProgress = hasProgress;
7892 return this;
7893 }
7894
7895 final StandardTemplateParams title(CharSequence title) {
7896 this.title = title;
7897 return this;
7898 }
7899
7900 final StandardTemplateParams text(CharSequence text) {
7901 this.text = text;
7902 return this;
7903 }
7904
7905 final StandardTemplateParams ambient(boolean ambient) {
Adrian Roos0bc3f6a2017-03-06 11:54:05 -08007906 Preconditions.checkState(title == null && text == null, "must set ambient before text");
Adrian Roos70d7aa32017-01-11 15:39:06 -08007907 this.ambient = ambient;
7908 return this;
7909 }
7910
7911 final StandardTemplateParams fillTextsFrom(Builder b) {
7912 Bundle extras = b.mN.extras;
Adrian Roos0bc3f6a2017-03-06 11:54:05 -08007913 title = b.processLegacyText(extras.getCharSequence(EXTRA_TITLE), ambient);
7914 text = b.processLegacyText(extras.getCharSequence(EXTRA_TEXT), ambient);
Adrian Roos70d7aa32017-01-11 15:39:06 -08007915 return this;
7916 }
7917 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007918}