blob: 13a6be557dae6f180e4ed248d81ea5950d92e53a [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
Selim Cinek389edcd2017-05-11 19:16:44 -070019import static com.android.internal.util.NotificationColorUtil.satisfiesTextContrast;
20
Tor Norbye80756e32015-03-02 09:39:27 -080021import android.annotation.ColorInt;
Tor Norbye7b9c9122013-05-30 16:48:33 -070022import android.annotation.DrawableRes;
Tor Norbyed9273d62013-05-30 15:59:53 -070023import android.annotation.IntDef;
Alex Hillsfd590442016-10-07 09:52:44 -040024import android.annotation.NonNull;
Selim Cinek88188f22017-09-19 16:46:56 -070025import android.annotation.Nullable;
Jeff Sharkeyd86b8fe2017-06-02 17:36:26 -060026import android.annotation.RequiresPermission;
Daniel Sandler01df1c62014-06-09 10:54:01 -040027import android.annotation.SdkConstant;
28import android.annotation.SdkConstant.SdkConstantType;
Julia Reynoldse46bb372016-03-17 11:05:58 -040029import android.annotation.SystemApi;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080030import android.content.Context;
31import android.content.Intent;
Kenny Guy77320062014-08-27 21:37:15 +010032import android.content.pm.ApplicationInfo;
Dan Sandler732bd6c2016-04-12 14:20:32 -040033import android.content.pm.PackageManager;
Christoph Studer4600f9b2014-07-22 22:44:43 +020034import android.content.pm.PackageManager.NameNotFoundException;
Julia Reynolds13d898c2017-02-02 12:22:05 -050035import android.content.pm.ShortcutInfo;
Jorim Jaggief72a192014-08-26 21:57:46 +020036import android.content.res.ColorStateList;
Anthony Chenad4d1582017-04-10 16:07:58 -070037import android.content.res.Configuration;
38import android.content.res.Resources;
Joe Onoratoef1e7762010-09-17 18:38:38 -040039import android.graphics.Bitmap;
Kenny Guy8a0101b2014-05-08 23:34:12 +010040import android.graphics.Canvas;
Adrian Roosc1a80b02016-04-05 14:54:55 -070041import android.graphics.Color;
Jorim Jaggi5c2d8462014-03-21 17:37:00 +010042import android.graphics.PorterDuff;
Kenny Guy8a0101b2014-05-08 23:34:12 +010043import android.graphics.drawable.Drawable;
Dan Sandlerd63f9322015-05-06 15:18:49 -040044import android.graphics.drawable.Icon;
John Spurlockc0650f022014-07-19 13:22:39 -040045import android.media.AudioAttributes;
Jeff Sharkey098d5802012-04-26 17:30:34 -070046import android.media.AudioManager;
Jean-Michel Trivi2f7511f2016-11-28 15:40:27 -080047import android.media.PlayerBase;
Jeff Browndba34ba2014-06-24 20:46:03 -070048import android.media.session.MediaSession;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080049import android.net.Uri;
Daniel Sandlerdcbaf662013-04-26 16:23:09 -040050import android.os.BadParcelableException;
Christoph Studer239f8352014-08-25 15:13:18 +020051import android.os.Build;
Daniel Sandler2561b0b2012-02-13 21:04:12 -050052import android.os.Bundle;
Dianne Hackborn98305522017-05-05 17:53:53 -070053import android.os.IBinder;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080054import android.os.Parcel;
55import android.os.Parcelable;
Daniel Sandlera2985ed2012-04-03 16:42:00 -040056import android.os.SystemClock;
Selim Cinek6743c0b2017-01-18 18:24:01 -080057import android.os.SystemProperties;
Jeff Sharkey6d515712012-09-20 16:06:08 -070058import android.os.UserHandle;
Adrian Roosc1a80b02016-04-05 14:54:55 -070059import android.text.BidiFormatter;
Selim Cinek60a54252016-02-26 17:03:25 -080060import android.text.SpannableStringBuilder;
61import android.text.Spanned;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080062import android.text.TextUtils;
Selim Cinek60a54252016-02-26 17:03:25 -080063import android.text.style.AbsoluteSizeSpan;
Selim Cinek89991a22016-03-07 19:51:54 -080064import android.text.style.CharacterStyle;
Selim Cinek981962e2016-07-20 20:41:58 -070065import android.text.style.ForegroundColorSpan;
Selim Cinek60a54252016-02-26 17:03:25 -080066import android.text.style.RelativeSizeSpan;
67import android.text.style.TextAppearanceSpan;
Svet Ganovddb94882016-06-23 19:55:24 -070068import android.util.ArraySet;
Daniel Sandlerdcbaf662013-04-26 16:23:09 -040069import android.util.Log;
Dan Sandler50128532015-12-08 15:42:41 -050070import android.util.SparseArray;
Yi Jin6b514142017-10-30 14:54:12 -070071import android.util.proto.ProtoOutputStream;
Griff Hazen61a9e862014-05-22 16:05:19 -070072import android.view.Gravity;
Selim Cinekea4bef72015-12-02 15:51:10 -080073import android.view.NotificationHeaderView;
Joe Onorato8595a3d2010-11-19 18:12:07 -080074import android.view.View;
Selim Cinek954cc232016-05-20 13:29:23 -070075import android.view.ViewGroup;
Jeff Sharkey1c400132011-08-05 14:50:13 -070076import android.widget.ProgressBar;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080077import android.widget.RemoteViews;
78
Griff Hazen959591e2014-05-15 22:26:18 -070079import com.android.internal.R;
Selim Cinek389edcd2017-05-11 19:16:44 -070080import com.android.internal.annotations.VisibleForTesting;
Svet Ganovddb94882016-06-23 19:55:24 -070081import com.android.internal.util.ArrayUtils;
Griff Hazenc091ba82014-05-16 10:13:26 -070082import com.android.internal.util.NotificationColorUtil;
Adrian Roos0bc3f6a2017-03-06 11:54:05 -080083import com.android.internal.util.Preconditions;
Griff Hazen959591e2014-05-15 22:26:18 -070084
Tor Norbyed9273d62013-05-30 15:59:53 -070085import java.lang.annotation.Retention;
86import java.lang.annotation.RetentionPolicy;
Christoph Studer4600f9b2014-07-22 22:44:43 +020087import java.lang.reflect.Constructor;
Daniel Sandler2561b0b2012-02-13 21:04:12 -050088import java.util.ArrayList;
Griff Hazen61a9e862014-05-22 16:05:19 -070089import java.util.Arrays;
Griff Hazen5cadc3b2014-05-20 09:55:39 -070090import java.util.Collections;
Griff Hazen61a9e862014-05-22 16:05:19 -070091import java.util.List;
Dan Sandler50128532015-12-08 15:42:41 -050092import java.util.Set;
Joe Onorato561d3852010-11-20 18:09:34 -080093
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080094/**
95 * A class that represents how a persistent notification is to be presented to
96 * the user using the {@link android.app.NotificationManager}.
97 *
Joe Onoratocb109a02011-01-18 17:57:41 -080098 * <p>The {@link Notification.Builder Notification.Builder} has been added to make it
99 * easier to construct Notifications.</p>
100 *
Joe Fernandez558459f2011-10-13 16:47:36 -0700101 * <div class="special reference">
102 * <h3>Developer Guides</h3>
103 * <p>For a guide to creating notifications, read the
104 * <a href="{@docRoot}guide/topics/ui/notifiers/notifications.html">Status Bar Notifications</a>
105 * developer guide.</p>
106 * </div>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800107 */
108public class Notification implements Parcelable
109{
Daniel Sandlerdcbaf662013-04-26 16:23:09 -0400110 private static final String TAG = "Notification";
111
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800112 /**
Daniel Sandler01df1c62014-06-09 10:54:01 -0400113 * An activity that provides a user interface for adjusting notification preferences for its
Julia Reynolds3aedded2017-03-31 14:42:09 -0400114 * containing application.
Daniel Sandler01df1c62014-06-09 10:54:01 -0400115 */
116 @SdkConstant(SdkConstantType.INTENT_CATEGORY)
117 public static final String INTENT_CATEGORY_NOTIFICATION_PREFERENCES
118 = "android.intent.category.NOTIFICATION_PREFERENCES";
119
120 /**
Julia Reynolds2619b5e2017-02-09 09:58:15 -0500121 * Optional extra for {@link #INTENT_CATEGORY_NOTIFICATION_PREFERENCES}. If provided, will
122 * contain a {@link NotificationChannel#getId() channel id} that can be used to narrow down
Julia Reynolds3aedded2017-03-31 14:42:09 -0400123 * what settings should be shown in the target app.
Julia Reynolds2619b5e2017-02-09 09:58:15 -0500124 */
125 public static final String EXTRA_CHANNEL_ID = "android.intent.extra.CHANNEL_ID";
126
127 /**
Julia Reynolds3aedded2017-03-31 14:42:09 -0400128 * Optional extra for {@link #INTENT_CATEGORY_NOTIFICATION_PREFERENCES}. If provided, will
Julia Reynolds005c8b92017-08-24 10:35:53 -0400129 * contain a {@link NotificationChannelGroup#getId() group id} that can be used to narrow down
130 * what settings should be shown in the target app.
131 */
132 public static final String EXTRA_CHANNEL_GROUP_ID = "android.intent.extra.CHANNEL_GROUP_ID";
133
134 /**
135 * Optional extra for {@link #INTENT_CATEGORY_NOTIFICATION_PREFERENCES}. If provided, will
Julia Reynolds3aedded2017-03-31 14:42:09 -0400136 * contain the tag provided to {@link NotificationManager#notify(String, int, Notification)}
137 * that can be used to narrow down what settings should be shown in the target app.
138 */
139 public static final String EXTRA_NOTIFICATION_TAG = "android.intent.extra.NOTIFICATION_TAG";
140
141 /**
142 * Optional extra for {@link #INTENT_CATEGORY_NOTIFICATION_PREFERENCES}. If provided, will
143 * contain the id provided to {@link NotificationManager#notify(String, int, Notification)}
144 * that can be used to narrow down what settings should be shown in the target app.
145 */
146 public static final String EXTRA_NOTIFICATION_ID = "android.intent.extra.NOTIFICATION_ID";
147
148 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800149 * Use all default values (where applicable).
150 */
151 public static final int DEFAULT_ALL = ~0;
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500152
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800153 /**
154 * Use the default notification sound. This will ignore any given
155 * {@link #sound}.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500156 *
Chris Wren47c20a12014-06-18 17:27:29 -0400157 * <p>
158 * A notification that is noisy is more likely to be presented as a heads-up notification.
159 * </p>
160 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800161 * @see #defaults
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500162 */
163
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800164 public static final int DEFAULT_SOUND = 1;
165
166 /**
167 * Use the default notification vibrate. This will ignore any given
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500168 * {@link #vibrate}. Using phone vibration requires the
Scott Mainb8b36452009-04-26 15:50:49 -0700169 * {@link android.Manifest.permission#VIBRATE VIBRATE} permission.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500170 *
Chris Wren47c20a12014-06-18 17:27:29 -0400171 * <p>
172 * A notification that vibrates is more likely to be presented as a heads-up notification.
173 * </p>
174 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800175 * @see #defaults
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500176 */
177
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800178 public static final int DEFAULT_VIBRATE = 2;
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500179
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800180 /**
181 * Use the default notification lights. This will ignore the
182 * {@link #FLAG_SHOW_LIGHTS} bit, and {@link #ledARGB}, {@link #ledOffMS}, or
183 * {@link #ledOnMS}.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500184 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800185 * @see #defaults
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500186 */
187
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800188 public static final int DEFAULT_LIGHTS = 4;
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500189
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800190 /**
Christoph Studer535ec612014-09-03 15:47:47 +0200191 * Maximum length of CharSequences accepted by Builder and friends.
192 *
193 * <p>
194 * Avoids spamming the system with overly large strings such as full e-mails.
195 */
196 private static final int MAX_CHARSEQUENCE_LENGTH = 5 * 1024;
197
198 /**
Adrian Roose458aa82015-12-08 16:17:19 -0800199 * Maximum entries of reply text that are accepted by Builder and friends.
200 */
201 private static final int MAX_REPLY_HISTORY = 5;
202
Selim Cinekde4de0e2018-01-24 16:21:07 -0800203
204 /**
205 * If the notification contained an unsent draft for a RemoteInput when the user clicked on it,
206 * we're adding the draft as a String extra to the {@link #contentIntent} using this key.
207 *
208 * <p>Apps may use this extra to prepopulate text fields in the app, where the user usually
209 * sends messages.</p>
210 */
211 public static final String EXTRA_REMOTE_INPUT_DRAFT = "android.remoteInputDraft";
212
Adrian Roose458aa82015-12-08 16:17:19 -0800213 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500214 * A timestamp related to this notification, in milliseconds since the epoch.
Joe Malin8d40d042012-11-05 11:36:40 -0800215 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500216 * Default value: {@link System#currentTimeMillis() Now}.
217 *
218 * Choose a timestamp that will be most relevant to the user. For most finite events, this
219 * corresponds to the time the event happened (or will happen, in the case of events that have
220 * yet to occur but about which the user is being informed). Indefinite events should be
Joe Malin8d40d042012-11-05 11:36:40 -0800221 * timestamped according to when the activity began.
222 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500223 * Some examples:
Joe Malin8d40d042012-11-05 11:36:40 -0800224 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500225 * <ul>
226 * <li>Notification of a new chat message should be stamped when the message was received.</li>
227 * <li>Notification of an ongoing file download (with a progress bar, for example) should be stamped when the download started.</li>
228 * <li>Notification of a completed file download should be stamped when the download finished.</li>
229 * <li>Notification of an upcoming meeting should be stamped with the time the meeting will begin (that is, in the future).</li>
230 * <li>Notification of an ongoing stopwatch (increasing timer) should be stamped with the watch's start time.
231 * <li>Notification of an ongoing countdown timer should be stamped with the timer's end time.
Joe Malin8d40d042012-11-05 11:36:40 -0800232 * </ul>
233 *
Selim Cinek0ff1ce602016-04-05 18:27:16 -0700234 * For apps targeting {@link android.os.Build.VERSION_CODES#N} and above, this time is not shown
235 * anymore by default and must be opted into by using
236 * {@link android.app.Notification.Builder#setShowWhen(boolean)}
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800237 */
238 public long when;
239
240 /**
Selim Cinekb85f36fd2016-04-20 18:46:36 -0700241 * The creation time of the notification
242 */
243 private long creationTime;
244
245 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800246 * The resource id of a drawable to use as the icon in the status bar.
Dan Sandler86647982015-05-13 23:41:13 -0400247 *
248 * @deprecated Use {@link Builder#setSmallIcon(Icon)} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800249 */
Dan Sandler86647982015-05-13 23:41:13 -0400250 @Deprecated
Tor Norbye7b9c9122013-05-30 16:48:33 -0700251 @DrawableRes
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800252 public int icon;
253
254 /**
Joe Onorato46439ce2010-11-19 13:56:21 -0800255 * If the icon in the status bar is to have more than one level, you can set this. Otherwise,
256 * leave it at its default value of 0.
257 *
258 * @see android.widget.ImageView#setImageLevel
Griff Hazen959591e2014-05-15 22:26:18 -0700259 * @see android.graphics.drawable.Drawable#setLevel
Joe Onorato46439ce2010-11-19 13:56:21 -0800260 */
261 public int iconLevel;
262
263 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500264 * The number of events that this notification represents. For example, in a new mail
265 * notification, this could be the number of unread messages.
Joe Malin8d40d042012-11-05 11:36:40 -0800266 *
Julia Reynolds30331982017-04-27 10:12:50 -0400267 * The system may or may not use this field to modify the appearance of the notification.
Julia Reynolds13d898c2017-02-02 12:22:05 -0500268 * Starting with {@link android.os.Build.VERSION_CODES#O}, the number may be displayed as a
269 * badge icon in Launchers that support badging.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800270 */
Julia Reynolds30331982017-04-27 10:12:50 -0400271 public int number = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800272
273 /**
274 * The intent to execute when the expanded status entry is clicked. If
275 * this is an activity, it must include the
276 * {@link android.content.Intent#FLAG_ACTIVITY_NEW_TASK} flag, which requires
Scott Main7aee61f2011-02-08 11:25:01 -0800277 * that you take care of task management as described in the
278 * <a href="{@docRoot}guide/topics/fundamentals/tasks-and-back-stack.html">Tasks and Back
Dianne Hackborn6ceca582012-01-10 15:24:26 -0800279 * Stack</a> document. In particular, make sure to read the notification section
280 * <a href="{@docRoot}guide/topics/ui/notifiers/notifications.html#HandlingNotifications">Handling
281 * Notifications</a> for the correct ways to launch an application from a
282 * notification.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800283 */
284 public PendingIntent contentIntent;
285
286 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500287 * The intent to execute when the notification is explicitly dismissed by the user, either with
288 * the "Clear All" button or by swiping it away individually.
289 *
290 * This probably shouldn't be launching an activity since several of those will be sent
291 * at the same time.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800292 */
293 public PendingIntent deleteIntent;
294
295 /**
Dianne Hackborn170bae72010-09-03 15:14:28 -0700296 * An intent to launch instead of posting the notification to the status bar.
Joe Onoratocb109a02011-01-18 17:57:41 -0800297 *
Chris Wren47c20a12014-06-18 17:27:29 -0400298 * <p>
299 * The system UI may choose to display a heads-up notification, instead of
300 * launching this intent, while the user is using the device.
301 * </p>
302 *
Joe Onoratocb109a02011-01-18 17:57:41 -0800303 * @see Notification.Builder#setFullScreenIntent
Daniel Sandlere46cbd32010-06-17 10:35:26 -0400304 */
305 public PendingIntent fullScreenIntent;
306
307 /**
Dan Sandler5fcdf6e2014-07-18 11:31:15 -0400308 * Text that summarizes this notification for accessibility services.
309 *
310 * As of the L release, this text is no longer shown on screen, but it is still useful to
311 * accessibility services (where it serves as an audible announcement of the notification's
312 * appearance).
Joe Onoratoef1e7762010-09-17 18:38:38 -0400313 *
Joe Onorato46439ce2010-11-19 13:56:21 -0800314 * @see #tickerView
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800315 */
316 public CharSequence tickerText;
317
318 /**
Dan Sandler5fcdf6e2014-07-18 11:31:15 -0400319 * Formerly, a view showing the {@link #tickerText}.
320 *
321 * No longer displayed in the status bar as of API 21.
Joe Onoratoef1e7762010-09-17 18:38:38 -0400322 */
Dan Sandler5fcdf6e2014-07-18 11:31:15 -0400323 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -0800324 public RemoteViews tickerView;
Joe Onoratoef1e7762010-09-17 18:38:38 -0400325
326 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400327 * The view that will represent this notification in the notification list (which is pulled
328 * down from the status bar).
329 *
Julia Reynoldsd4ea7412016-02-17 14:00:56 -0500330 * As of N, this field may be null. The notification view is determined by the inputs
331 * to {@link Notification.Builder}; a custom RemoteViews can optionally be
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400332 * supplied with {@link Notification.Builder#setCustomContentView(RemoteViews)}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800333 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400334 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800335 public RemoteViews contentView;
336
Daniel Sandlera0a938c2012-03-15 08:42:37 -0400337 /**
Daniel Sandler4dfbe832012-04-11 14:51:46 -0400338 * A large-format version of {@link #contentView}, giving the Notification an
Daniel Sandlerf3b73432012-03-27 15:01:25 -0400339 * opportunity to show more detail. The system UI may choose to show this
340 * instead of the normal content view at its discretion.
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400341 *
Julia Reynoldsd4ea7412016-02-17 14:00:56 -0500342 * As of N, this field may be null. The expanded notification view is determined by the
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400343 * inputs to {@link Notification.Builder}; a custom RemoteViews can optionally be
344 * supplied with {@link Notification.Builder#setCustomBigContentView(RemoteViews)}.
Daniel Sandlerf3b73432012-03-27 15:01:25 -0400345 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400346 @Deprecated
Daniel Sandlerf3b73432012-03-27 15:01:25 -0400347 public RemoteViews bigContentView;
348
Chris Wren8fd39ec2014-02-27 17:43:26 -0500349
350 /**
Chris Wren47c20a12014-06-18 17:27:29 -0400351 * A medium-format version of {@link #contentView}, providing the Notification an
352 * opportunity to add action buttons to contentView. At its discretion, the system UI may
353 * choose to show this as a heads-up notification, which will pop up so the user can see
354 * it without leaving their current activity.
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400355 *
Julia Reynoldsd4ea7412016-02-17 14:00:56 -0500356 * As of N, this field may be null. The heads-up notification view is determined by the
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400357 * inputs to {@link Notification.Builder}; a custom RemoteViews can optionally be
358 * supplied with {@link Notification.Builder#setCustomHeadsUpContentView(RemoteViews)}.
Chris Wren8fd39ec2014-02-27 17:43:26 -0500359 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400360 @Deprecated
Chris Wren8fd39ec2014-02-27 17:43:26 -0500361 public RemoteViews headsUpContentView;
362
Julia Reynoldsfc640012018-02-21 12:25:27 -0500363 private boolean mUsesStandardHeader;
364
365 private static final ArraySet<Integer> STANDARD_LAYOUTS = new ArraySet<>();
366 static {
367 STANDARD_LAYOUTS.add(R.layout.notification_template_material_base);
368 STANDARD_LAYOUTS.add(R.layout.notification_template_material_big_base);
369 STANDARD_LAYOUTS.add(R.layout.notification_template_material_big_picture);
370 STANDARD_LAYOUTS.add(R.layout.notification_template_material_big_text);
371 STANDARD_LAYOUTS.add(R.layout.notification_template_material_inbox);
372 STANDARD_LAYOUTS.add(R.layout.notification_template_material_messaging);
373 STANDARD_LAYOUTS.add(R.layout.notification_template_material_media);
374 STANDARD_LAYOUTS.add(R.layout.notification_template_material_big_media);
375 STANDARD_LAYOUTS.add(R.layout.notification_template_ambient_header);
376 STANDARD_LAYOUTS.add(R.layout.notification_template_header);
377 STANDARD_LAYOUTS.add(R.layout.notification_template_material_ambient);
378 }
379
Daniel Sandlerf3b73432012-03-27 15:01:25 -0400380 /**
Dan Sandler86647982015-05-13 23:41:13 -0400381 * A large bitmap to be shown in the notification content area.
382 *
383 * @deprecated Use {@link Builder#setLargeIcon(Icon)} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800384 */
Dan Sandler86647982015-05-13 23:41:13 -0400385 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -0800386 public Bitmap largeIcon;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800387
388 /**
389 * The sound to play.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500390 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800391 * <p>
Chris Wren47c20a12014-06-18 17:27:29 -0400392 * A notification that is noisy is more likely to be presented as a heads-up notification.
393 * </p>
394 *
395 * <p>
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500396 * To play the default notification sound, see {@link #defaults}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800397 * </p>
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500398 * @deprecated use {@link NotificationChannel#getSound()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800399 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500400 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800401 public Uri sound;
402
403 /**
404 * Use this constant as the value for audioStreamType to request that
405 * the default stream type for notifications be used. Currently the
Jeff Sharkey098d5802012-04-26 17:30:34 -0700406 * default stream type is {@link AudioManager#STREAM_NOTIFICATION}.
John Spurlockc0650f022014-07-19 13:22:39 -0400407 *
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500408 * @deprecated Use {@link NotificationChannel#getAudioAttributes()} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800409 */
Jean-Michel Trivi81f871e2014-08-06 16:32:38 -0700410 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800411 public static final int STREAM_DEFAULT = -1;
412
413 /**
414 * The audio stream type to use when playing the sound.
415 * Should be one of the STREAM_ constants from
416 * {@link android.media.AudioManager}.
John Spurlockc0650f022014-07-19 13:22:39 -0400417 *
418 * @deprecated Use {@link #audioAttributes} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800419 */
Jean-Michel Trivi81f871e2014-08-06 16:32:38 -0700420 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800421 public int audioStreamType = STREAM_DEFAULT;
422
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800423 /**
John Spurlockc0650f022014-07-19 13:22:39 -0400424 * The default value of {@link #audioAttributes}.
425 */
426 public static final AudioAttributes AUDIO_ATTRIBUTES_DEFAULT = new AudioAttributes.Builder()
427 .setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
428 .setUsage(AudioAttributes.USAGE_NOTIFICATION)
429 .build();
430
431 /**
432 * The {@link AudioAttributes audio attributes} to use when playing the sound.
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500433 *
434 * @deprecated use {@link NotificationChannel#getAudioAttributes()} instead.
John Spurlockc0650f022014-07-19 13:22:39 -0400435 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500436 @Deprecated
John Spurlockc0650f022014-07-19 13:22:39 -0400437 public AudioAttributes audioAttributes = AUDIO_ATTRIBUTES_DEFAULT;
438
439 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500440 * The pattern with which to vibrate.
441 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800442 * <p>
443 * To vibrate the default pattern, see {@link #defaults}.
444 * </p>
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500445 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800446 * @see android.os.Vibrator#vibrate(long[],int)
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500447 * @deprecated use {@link NotificationChannel#getVibrationPattern()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800448 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500449 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800450 public long[] vibrate;
451
452 /**
453 * The color of the led. The hardware will do its best approximation.
454 *
455 * @see #FLAG_SHOW_LIGHTS
456 * @see #flags
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500457 * @deprecated use {@link NotificationChannel#shouldShowLights()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800458 */
Tor Norbye80756e32015-03-02 09:39:27 -0800459 @ColorInt
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500460 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800461 public int ledARGB;
462
463 /**
464 * The number of milliseconds for the LED to be on while it's flashing.
465 * The hardware will do its best approximation.
466 *
467 * @see #FLAG_SHOW_LIGHTS
468 * @see #flags
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500469 * @deprecated use {@link NotificationChannel#shouldShowLights()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800470 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500471 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800472 public int ledOnMS;
473
474 /**
475 * The number of milliseconds for the LED to be off while it's flashing.
476 * The hardware will do its best approximation.
477 *
478 * @see #FLAG_SHOW_LIGHTS
479 * @see #flags
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500480 *
481 * @deprecated use {@link NotificationChannel#shouldShowLights()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800482 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500483 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800484 public int ledOffMS;
485
486 /**
487 * Specifies which values should be taken from the defaults.
488 * <p>
489 * To set, OR the desired from {@link #DEFAULT_SOUND},
490 * {@link #DEFAULT_VIBRATE}, {@link #DEFAULT_LIGHTS}. For all default
491 * values, use {@link #DEFAULT_ALL}.
492 * </p>
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500493 *
494 * @deprecated use {@link NotificationChannel#getSound()} and
495 * {@link NotificationChannel#shouldShowLights()} and
496 * {@link NotificationChannel#shouldVibrate()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800497 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500498 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800499 public int defaults;
500
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800501 /**
502 * Bit to be bitwise-ored into the {@link #flags} field that should be
503 * set if you want the LED on for this notification.
504 * <ul>
505 * <li>To turn the LED off, pass 0 in the alpha channel for colorARGB
506 * or 0 for both ledOnMS and ledOffMS.</li>
507 * <li>To turn the LED on, pass 1 for ledOnMS and 0 for ledOffMS.</li>
508 * <li>To flash the LED, pass the number of milliseconds that it should
509 * be on and off to ledOnMS and ledOffMS.</li>
510 * </ul>
511 * <p>
512 * Since hardware varies, you are not guaranteed that any of the values
Ricardo Loo Forondaf8f6d0a2018-01-25 08:42:43 -0800513 * you pass are honored exactly. Use the system defaults if possible
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800514 * because they will be set to values that work on any given hardware.
515 * <p>
516 * The alpha channel must be set for forward compatibility.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500517 *
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500518 * @deprecated use {@link NotificationChannel#shouldShowLights()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800519 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500520 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800521 public static final int FLAG_SHOW_LIGHTS = 0x00000001;
522
523 /**
524 * Bit to be bitwise-ored into the {@link #flags} field that should be
525 * set if this notification is in reference to something that is ongoing,
526 * like a phone call. It should not be set if this notification is in
527 * reference to something that happened at a particular point in time,
528 * like a missed phone call.
529 */
530 public static final int FLAG_ONGOING_EVENT = 0x00000002;
531
532 /**
533 * Bit to be bitwise-ored into the {@link #flags} field that if set,
Scott Mainb8b36452009-04-26 15:50:49 -0700534 * the audio will be repeated until the notification is
535 * cancelled or the notification window is opened.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800536 */
537 public static final int FLAG_INSISTENT = 0x00000004;
538
539 /**
540 * Bit to be bitwise-ored into the {@link #flags} field that should be
Griff Hazen293977b2014-04-28 08:37:20 -0700541 * set if you would only like the sound, vibrate and ticker to be played
542 * if the notification was not already showing.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800543 */
544 public static final int FLAG_ONLY_ALERT_ONCE = 0x00000008;
545
546 /**
547 * Bit to be bitwise-ored into the {@link #flags} field that should be
548 * set if the notification should be canceled when it is clicked by the
Daniel Sandler8aa9ae62012-12-04 23:31:47 -0500549 * user.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800550 */
551 public static final int FLAG_AUTO_CANCEL = 0x00000010;
552
553 /**
554 * Bit to be bitwise-ored into the {@link #flags} field that should be
555 * set if the notification should not be canceled when the user clicks
556 * the Clear all button.
557 */
558 public static final int FLAG_NO_CLEAR = 0x00000020;
559
Dianne Hackbornd8a43f62009-08-17 23:33:56 -0700560 /**
561 * Bit to be bitwise-ored into the {@link #flags} field that should be
562 * set if this notification represents a currently running service. This
563 * will normally be set for you by {@link Service#startForeground}.
564 */
565 public static final int FLAG_FOREGROUND_SERVICE = 0x00000040;
566
Daniel Sandlere46cbd32010-06-17 10:35:26 -0400567 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500568 * Obsolete flag indicating high-priority notifications; use the priority field instead.
Joe Malin8d40d042012-11-05 11:36:40 -0800569 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500570 * @deprecated Use {@link #priority} with a positive value.
Daniel Sandlere46cbd32010-06-17 10:35:26 -0400571 */
Aurimas Liutikas514c5ef2016-05-24 15:22:55 -0700572 @Deprecated
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500573 public static final int FLAG_HIGH_PRIORITY = 0x00000080;
Daniel Sandlere46cbd32010-06-17 10:35:26 -0400574
Griff Hazendfcb0802014-02-11 12:00:00 -0800575 /**
576 * Bit to be bitswise-ored into the {@link #flags} field that should be
577 * set if this notification is relevant to the current device only
578 * and it is not recommended that it bridge to other devices.
579 */
580 public static final int FLAG_LOCAL_ONLY = 0x00000100;
581
Griff Hazen5cadc3b2014-05-20 09:55:39 -0700582 /**
583 * Bit to be bitswise-ored into the {@link #flags} field that should be
584 * set if this notification is the group summary for a group of notifications.
585 * Grouped notifications may display in a cluster or stack on devices which
586 * support such rendering. Requires a group key also be set using {@link Builder#setGroup}.
587 */
588 public static final int FLAG_GROUP_SUMMARY = 0x00000200;
589
Julia Reynoldse46bb372016-03-17 11:05:58 -0400590 /**
591 * Bit to be bitswise-ored into the {@link #flags} field that should be
592 * set if this notification is the group summary for an auto-group of notifications.
593 *
594 * @hide
595 */
596 @SystemApi
597 public static final int FLAG_AUTOGROUP_SUMMARY = 0x00000400;
598
Julia Reynolds4db59552017-06-30 13:34:01 -0400599 /**
600 * @hide
601 */
602 public static final int FLAG_CAN_COLORIZE = 0x00000800;
603
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800604 public int flags;
605
Tor Norbyed9273d62013-05-30 15:59:53 -0700606 /** @hide */
Jeff Sharkeyce8db992017-12-13 20:05:05 -0700607 @IntDef(prefix = { "PRIORITY_" }, value = {
608 PRIORITY_DEFAULT,
609 PRIORITY_LOW,
610 PRIORITY_MIN,
611 PRIORITY_HIGH,
612 PRIORITY_MAX
613 })
Tor Norbyed9273d62013-05-30 15:59:53 -0700614 @Retention(RetentionPolicy.SOURCE)
615 public @interface Priority {}
616
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800617 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500618 * Default notification {@link #priority}. If your application does not prioritize its own
619 * notifications, use this value for all notifications.
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500620 *
621 * @deprecated use {@link NotificationManager#IMPORTANCE_DEFAULT} instead.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500622 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500623 @Deprecated
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500624 public static final int PRIORITY_DEFAULT = 0;
625
626 /**
627 * Lower {@link #priority}, for items that are less important. The UI may choose to show these
628 * items smaller, or at a different position in the list, compared with your app's
629 * {@link #PRIORITY_DEFAULT} items.
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500630 *
631 * @deprecated use {@link NotificationManager#IMPORTANCE_LOW} instead.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500632 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500633 @Deprecated
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500634 public static final int PRIORITY_LOW = -1;
635
636 /**
637 * Lowest {@link #priority}; these items might not be shown to the user except under special
638 * circumstances, such as detailed notification logs.
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500639 *
640 * @deprecated use {@link NotificationManager#IMPORTANCE_MIN} instead.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500641 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500642 @Deprecated
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500643 public static final int PRIORITY_MIN = -2;
644
645 /**
646 * Higher {@link #priority}, for more important notifications or alerts. The UI may choose to
647 * show these items larger, or at a different position in notification lists, compared with
648 * your app's {@link #PRIORITY_DEFAULT} items.
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500649 *
650 * @deprecated use {@link NotificationManager#IMPORTANCE_HIGH} instead.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500651 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500652 @Deprecated
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500653 public static final int PRIORITY_HIGH = 1;
654
655 /**
656 * Highest {@link #priority}, for your application's most important items that require the
657 * user's prompt attention or input.
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500658 *
659 * @deprecated use {@link NotificationManager#IMPORTANCE_HIGH} instead.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500660 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500661 @Deprecated
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500662 public static final int PRIORITY_MAX = 2;
663
664 /**
665 * Relative priority for this notification.
Joe Malin8d40d042012-11-05 11:36:40 -0800666 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500667 * Priority is an indication of how much of the user's valuable attention should be consumed by
668 * this notification. Low-priority notifications may be hidden from the user in certain
669 * situations, while the user might be interrupted for a higher-priority notification. The
Daniel Sandler6738eee2012-11-16 12:03:32 -0500670 * system will make a determination about how to interpret this priority when presenting
671 * the notification.
Chris Wren47c20a12014-06-18 17:27:29 -0400672 *
673 * <p>
674 * A notification that is at least {@link #PRIORITY_HIGH} is more likely to be presented
675 * as a heads-up notification.
676 * </p>
677 *
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500678 * @deprecated use {@link NotificationChannel#getImportance()} instead.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500679 */
Tor Norbyed9273d62013-05-30 15:59:53 -0700680 @Priority
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500681 @Deprecated
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500682 public int priority;
Joe Malin8d40d042012-11-05 11:36:40 -0800683
Dan Sandler26e81cf2014-05-06 10:01:27 -0400684 /**
685 * Accent color (an ARGB integer like the constants in {@link android.graphics.Color})
686 * to be applied by the standard Style templates when presenting this notification.
687 *
688 * The current template design constructs a colorful header image by overlaying the
689 * {@link #icon} image (stenciled in white) atop a field of this color. Alpha components are
690 * ignored.
691 */
Tor Norbye80756e32015-03-02 09:39:27 -0800692 @ColorInt
Dan Sandler26e81cf2014-05-06 10:01:27 -0400693 public int color = COLOR_DEFAULT;
694
695 /**
696 * Special value of {@link #color} telling the system not to decorate this notification with
697 * any special color but instead use default colors when presenting this notification.
698 */
Tor Norbye80756e32015-03-02 09:39:27 -0800699 @ColorInt
Dan Sandler26e81cf2014-05-06 10:01:27 -0400700 public static final int COLOR_DEFAULT = 0; // AKA Color.TRANSPARENT
Dan Sandler0bf2ed82013-12-21 23:33:41 -0600701
702 /**
Adrian Roos4ff3b122016-02-01 12:26:13 -0800703 * Special value of {@link #color} used as a place holder for an invalid color.
Selim Cinek99104832017-01-25 14:47:33 -0800704 * @hide
Adrian Roos4ff3b122016-02-01 12:26:13 -0800705 */
706 @ColorInt
Selim Cinek99104832017-01-25 14:47:33 -0800707 public static final int COLOR_INVALID = 1;
Adrian Roos4ff3b122016-02-01 12:26:13 -0800708
709 /**
Adrian Roosc1a80b02016-04-05 14:54:55 -0700710 * Sphere of visibility of this notification, which affects how and when the SystemUI reveals
711 * the notification's presence and contents in untrusted situations (namely, on the secure
Dan Sandler0bf2ed82013-12-21 23:33:41 -0600712 * lockscreen).
713 *
714 * The default level, {@link #VISIBILITY_PRIVATE}, behaves exactly as notifications have always
715 * done on Android: The notification's {@link #icon} and {@link #tickerText} (if available) are
716 * shown in all situations, but the contents are only available if the device is unlocked for
717 * the appropriate user.
718 *
719 * A more permissive policy can be expressed by {@link #VISIBILITY_PUBLIC}; such a notification
720 * can be read even in an "insecure" context (that is, above a secure lockscreen).
721 * To modify the public version of this notification—for example, to redact some portions—see
722 * {@link Builder#setPublicVersion(Notification)}.
723 *
724 * Finally, a notification can be made {@link #VISIBILITY_SECRET}, which will suppress its icon
725 * and ticker until the user has bypassed the lockscreen.
726 */
Jeff Sharkey30e06bb2017-04-24 11:18:03 -0600727 public @Visibility int visibility;
728
729 /** @hide */
730 @IntDef(prefix = { "VISIBILITY_" }, value = {
731 VISIBILITY_PUBLIC,
732 VISIBILITY_PRIVATE,
733 VISIBILITY_SECRET,
734 })
735 @Retention(RetentionPolicy.SOURCE)
736 public @interface Visibility {}
Dan Sandler0bf2ed82013-12-21 23:33:41 -0600737
Griff Hazenfc3922d2014-08-20 11:56:44 -0700738 /**
739 * Notification visibility: Show this notification in its entirety on all lockscreens.
740 *
741 * {@see #visibility}
742 */
Dan Sandler0bf2ed82013-12-21 23:33:41 -0600743 public static final int VISIBILITY_PUBLIC = 1;
Griff Hazenfc3922d2014-08-20 11:56:44 -0700744
745 /**
746 * Notification visibility: Show this notification on all lockscreens, but conceal sensitive or
747 * private information on secure lockscreens.
748 *
749 * {@see #visibility}
750 */
Dan Sandler0bf2ed82013-12-21 23:33:41 -0600751 public static final int VISIBILITY_PRIVATE = 0;
Griff Hazenfc3922d2014-08-20 11:56:44 -0700752
753 /**
754 * Notification visibility: Do not reveal any part of this notification on a secure lockscreen.
755 *
756 * {@see #visibility}
757 */
Dan Sandler0bf2ed82013-12-21 23:33:41 -0600758 public static final int VISIBILITY_SECRET = -1;
759
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500760 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400761 * Notification category: incoming call (voice or video) or similar synchronous communication request.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500762 */
John Spurlockfd7f1e02014-03-18 16:41:57 -0400763 public static final String CATEGORY_CALL = "call";
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500764
765 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400766 * Notification category: incoming direct message (SMS, instant message, etc.).
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500767 */
John Spurlockfd7f1e02014-03-18 16:41:57 -0400768 public static final String CATEGORY_MESSAGE = "msg";
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500769
770 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400771 * Notification category: asynchronous bulk message (email).
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500772 */
John Spurlockfd7f1e02014-03-18 16:41:57 -0400773 public static final String CATEGORY_EMAIL = "email";
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500774
775 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400776 * Notification category: calendar event.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500777 */
John Spurlockfd7f1e02014-03-18 16:41:57 -0400778 public static final String CATEGORY_EVENT = "event";
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500779
780 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400781 * Notification category: promotion or advertisement.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500782 */
John Spurlockfd7f1e02014-03-18 16:41:57 -0400783 public static final String CATEGORY_PROMO = "promo";
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500784
785 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400786 * Notification category: alarm or timer.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500787 */
John Spurlockfd7f1e02014-03-18 16:41:57 -0400788 public static final String CATEGORY_ALARM = "alarm";
789
790 /**
791 * Notification category: progress of a long-running background operation.
792 */
793 public static final String CATEGORY_PROGRESS = "progress";
794
795 /**
796 * Notification category: social network or sharing update.
797 */
798 public static final String CATEGORY_SOCIAL = "social";
799
800 /**
801 * Notification category: error in background operation or authentication status.
802 */
803 public static final String CATEGORY_ERROR = "err";
804
805 /**
806 * Notification category: media transport control for playback.
807 */
808 public static final String CATEGORY_TRANSPORT = "transport";
809
810 /**
811 * Notification category: system or device status update. Reserved for system use.
812 */
813 public static final String CATEGORY_SYSTEM = "sys";
814
815 /**
816 * Notification category: indication of running background service.
817 */
818 public static final String CATEGORY_SERVICE = "service";
819
820 /**
John Spurlock0a69c8c2014-03-21 13:30:57 -0400821 * Notification category: a specific, timely recommendation for a single thing.
822 * For example, a news app might want to recommend a news story it believes the user will
823 * want to read next.
824 */
825 public static final String CATEGORY_RECOMMENDATION = "recommendation";
826
827 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400828 * Notification category: ongoing information about device or contextual status.
829 */
830 public static final String CATEGORY_STATUS = "status";
831
832 /**
John Spurlock24d3dad2015-04-02 12:24:02 -0400833 * Notification category: user-scheduled reminder.
834 */
835 public static final String CATEGORY_REMINDER = "reminder";
836
837 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400838 * One of the predefined notification categories (see the <code>CATEGORY_*</code> constants)
839 * that best describes this Notification. May be used by the system for ranking and filtering.
840 */
841 public String category;
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500842
Griff Hazen5cadc3b2014-05-20 09:55:39 -0700843 private String mGroupKey;
844
845 /**
846 * Get the key used to group this notification into a cluster or stack
847 * with other notifications on devices which support such rendering.
848 */
849 public String getGroup() {
850 return mGroupKey;
851 }
852
853 private String mSortKey;
854
855 /**
856 * Get a sort key that orders this notification among other notifications from the
857 * same package. This can be useful if an external sort was already applied and an app
858 * would like to preserve this. Notifications will be sorted lexicographically using this
859 * value, although providing different priorities in addition to providing sort key may
860 * cause this value to be ignored.
861 *
862 * <p>This sort key can also be used to order members of a notification group. See
863 * {@link Builder#setGroup}.
864 *
865 * @see String#compareTo(String)
866 */
867 public String getSortKey() {
868 return mSortKey;
869 }
870
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500871 /**
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400872 * Additional semantic data to be carried around with this Notification.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400873 * <p>
874 * The extras keys defined here are intended to capture the original inputs to {@link Builder}
875 * APIs, and are intended to be used by
876 * {@link android.service.notification.NotificationListenerService} implementations to extract
877 * detailed information from notification objects.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500878 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400879 public Bundle extras = new Bundle();
880
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400881 /**
Felipe Lemedd85da62016-06-28 11:29:54 -0700882 * All pending intents in the notification as the system needs to be able to access them but
883 * touching the extras bundle in the system process is not safe because the bundle may contain
Svet Ganovddb94882016-06-23 19:55:24 -0700884 * custom parcelable objects.
885 *
886 * @hide
887 */
Felipe Lemedd85da62016-06-28 11:29:54 -0700888 public ArraySet<PendingIntent> allPendingIntents;
Svet Ganovddb94882016-06-23 19:55:24 -0700889
890 /**
Dianne Hackborn98305522017-05-05 17:53:53 -0700891 * Token identifying the notification that is applying doze/bgcheck whitelisting to the
892 * pending intents inside of it, so only those will get the behavior.
893 *
894 * @hide
895 */
Adrian Roosfb921842017-10-26 14:49:56 +0200896 private IBinder mWhitelistToken;
Dianne Hackborn98305522017-05-05 17:53:53 -0700897
898 /**
899 * Must be set by a process to start associating tokens with Notification objects
900 * coming in to it. This is set by NotificationManagerService.
901 *
902 * @hide
903 */
904 static public IBinder processWhitelistToken;
905
906 /**
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400907 * {@link #extras} key: this is the title of the notification,
908 * as supplied to {@link Builder#setContentTitle(CharSequence)}.
909 */
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -0500910 public static final String EXTRA_TITLE = "android.title";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400911
912 /**
913 * {@link #extras} key: this is the title of the notification when shown in expanded form,
914 * e.g. as supplied to {@link BigTextStyle#setBigContentTitle(CharSequence)}.
915 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400916 public static final String EXTRA_TITLE_BIG = EXTRA_TITLE + ".big";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400917
918 /**
919 * {@link #extras} key: this is the main text payload, as supplied to
920 * {@link Builder#setContentText(CharSequence)}.
921 */
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -0500922 public static final String EXTRA_TEXT = "android.text";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400923
924 /**
925 * {@link #extras} key: this is a third line of text, as supplied to
926 * {@link Builder#setSubText(CharSequence)}.
927 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400928 public static final String EXTRA_SUB_TEXT = "android.subText";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400929
930 /**
Adrian Roose458aa82015-12-08 16:17:19 -0800931 * {@link #extras} key: this is the remote input history, as supplied to
932 * {@link Builder#setRemoteInputHistory(CharSequence[])}.
Adrian Roos005e7742016-04-13 15:02:20 -0700933 *
934 * Apps can fill this through {@link Builder#setRemoteInputHistory(CharSequence[])}
935 * with the most recent inputs that have been sent through a {@link RemoteInput} of this
936 * Notification and are expected to clear it once the it is no longer relevant (e.g. for chat
937 * notifications once the other party has responded).
938 *
939 * The extra with this key is of type CharSequence[] and contains the most recent entry at
940 * the 0 index, the second most recent at the 1 index, etc.
941 *
942 * @see Builder#setRemoteInputHistory(CharSequence[])
Adrian Roose458aa82015-12-08 16:17:19 -0800943 */
944 public static final String EXTRA_REMOTE_INPUT_HISTORY = "android.remoteInputHistory";
945
946 /**
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400947 * {@link #extras} key: this is a small piece of additional text as supplied to
948 * {@link Builder#setContentInfo(CharSequence)}.
949 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400950 public static final String EXTRA_INFO_TEXT = "android.infoText";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400951
952 /**
953 * {@link #extras} key: this is a line of summary information intended to be shown
954 * alongside expanded notifications, as supplied to (e.g.)
955 * {@link BigTextStyle#setSummaryText(CharSequence)}.
956 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400957 public static final String EXTRA_SUMMARY_TEXT = "android.summaryText";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400958
959 /**
Christoph Studer4600f9b2014-07-22 22:44:43 +0200960 * {@link #extras} key: this is the longer text shown in the big form of a
961 * {@link BigTextStyle} notification, as supplied to
962 * {@link BigTextStyle#bigText(CharSequence)}.
963 */
964 public static final String EXTRA_BIG_TEXT = "android.bigText";
965
966 /**
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400967 * {@link #extras} key: this is the resource ID of the notification's main small icon, as
968 * supplied to {@link Builder#setSmallIcon(int)}.
Julia Reynoldse071abd2017-03-22 10:52:11 -0400969 *
970 * @deprecated Use {@link #getSmallIcon()}, which supports a wider variety of icon sources.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400971 */
Julia Reynoldse071abd2017-03-22 10:52:11 -0400972 @Deprecated
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -0500973 public static final String EXTRA_SMALL_ICON = "android.icon";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400974
975 /**
976 * {@link #extras} key: this is a bitmap to be used instead of the small icon when showing the
977 * notification payload, as
978 * supplied to {@link Builder#setLargeIcon(android.graphics.Bitmap)}.
Julia Reynoldse071abd2017-03-22 10:52:11 -0400979 *
980 * @deprecated Use {@link #getLargeIcon()}, which supports a wider variety of icon sources.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400981 */
Julia Reynoldse071abd2017-03-22 10:52:11 -0400982 @Deprecated
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400983 public static final String EXTRA_LARGE_ICON = "android.largeIcon";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400984
985 /**
986 * {@link #extras} key: this is a bitmap to be used instead of the one from
987 * {@link Builder#setLargeIcon(android.graphics.Bitmap)} when the notification is
988 * shown in its expanded form, as supplied to
989 * {@link BigPictureStyle#bigLargeIcon(android.graphics.Bitmap)}.
990 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400991 public static final String EXTRA_LARGE_ICON_BIG = EXTRA_LARGE_ICON + ".big";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400992
993 /**
994 * {@link #extras} key: this is the progress value supplied to
995 * {@link Builder#setProgress(int, int, boolean)}.
996 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400997 public static final String EXTRA_PROGRESS = "android.progress";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400998
999 /**
1000 * {@link #extras} key: this is the maximum value supplied to
1001 * {@link Builder#setProgress(int, int, boolean)}.
1002 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -04001003 public static final String EXTRA_PROGRESS_MAX = "android.progressMax";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001004
1005 /**
1006 * {@link #extras} key: whether the progress bar is indeterminate, supplied to
1007 * {@link Builder#setProgress(int, int, boolean)}.
1008 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -04001009 public static final String EXTRA_PROGRESS_INDETERMINATE = "android.progressIndeterminate";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001010
1011 /**
1012 * {@link #extras} key: whether {@link #when} should be shown as a count-up timer (specifically
1013 * a {@link android.widget.Chronometer}) instead of a timestamp, as supplied to
1014 * {@link Builder#setUsesChronometer(boolean)}.
1015 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -04001016 public static final String EXTRA_SHOW_CHRONOMETER = "android.showChronometer";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001017
1018 /**
Selim Cinek81c23aa2016-02-25 16:23:13 -08001019 * {@link #extras} key: whether the chronometer set on the notification should count down
1020 * instead of counting up. Is only relevant if key {@link #EXTRA_SHOW_CHRONOMETER} is present.
Adrian Roos96b7e202016-05-17 13:50:38 -07001021 * This extra is a boolean. The default is false.
Selim Cinek81c23aa2016-02-25 16:23:13 -08001022 */
Adrian Roos96b7e202016-05-17 13:50:38 -07001023 public static final String EXTRA_CHRONOMETER_COUNT_DOWN = "android.chronometerCountDown";
Selim Cinek81c23aa2016-02-25 16:23:13 -08001024
1025 /**
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001026 * {@link #extras} key: whether {@link #when} should be shown,
1027 * as supplied to {@link Builder#setShowWhen(boolean)}.
1028 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -04001029 public static final String EXTRA_SHOW_WHEN = "android.showWhen";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001030
1031 /**
1032 * {@link #extras} key: this is a bitmap to be shown in {@link BigPictureStyle} expanded
1033 * notifications, supplied to {@link BigPictureStyle#bigPicture(android.graphics.Bitmap)}.
1034 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -04001035 public static final String EXTRA_PICTURE = "android.picture";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001036
1037 /**
1038 * {@link #extras} key: An array of CharSequences to show in {@link InboxStyle} expanded
1039 * notifications, each of which was supplied to {@link InboxStyle#addLine(CharSequence)}.
1040 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -04001041 public static final String EXTRA_TEXT_LINES = "android.textLines";
Dan Sandler842dd772014-05-15 09:36:47 -04001042
1043 /**
1044 * {@link #extras} key: A string representing the name of the specific
1045 * {@link android.app.Notification.Style} used to create this notification.
1046 */
Chris Wren91ad5632013-06-05 15:05:57 -04001047 public static final String EXTRA_TEMPLATE = "android.template";
Daniel Sandlerf45564e2013-04-15 15:05:08 -04001048
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001049 /**
Chris Wrene6c48932014-09-29 17:19:27 -04001050 * {@link #extras} key: A String array containing the people that this notification relates to,
1051 * each of which was supplied to {@link Builder#addPerson(String)}.
Selim Cineke7238dd2017-12-14 17:48:32 -08001052 *
1053 * @deprecated the actual objects are now in {@link #EXTRA_PEOPLE_LIST}
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001054 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -04001055 public static final String EXTRA_PEOPLE = "android.people";
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001056
1057 /**
Selim Cineke7238dd2017-12-14 17:48:32 -08001058 * {@link #extras} key: An arrayList of {@link Person} objects containing the people that
1059 * this notification relates to.
1060 */
1061 public static final String EXTRA_PEOPLE_LIST = "android.people.list";
1062
1063 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -04001064 * Allow certain system-generated notifications to appear before the device is provisioned.
1065 * Only available to notifications coming from the android package.
1066 * @hide
1067 */
Maurice Lam96c10032017-03-29 15:42:38 -07001068 @SystemApi
Jeff Sharkeyd86b8fe2017-06-02 17:36:26 -06001069 @RequiresPermission(android.Manifest.permission.NOTIFICATION_DURING_SETUP)
John Spurlockfd7f1e02014-03-18 16:41:57 -04001070 public static final String EXTRA_ALLOW_DURING_SETUP = "android.allowDuringSetup";
1071
1072 /**
Jose Limae9e3b3b2014-05-18 23:44:50 -07001073 * {@link #extras} key: A
1074 * {@link android.content.ContentUris content URI} pointing to an image that can be displayed
Julia Reynoldse0d711f2017-09-01 08:50:47 -04001075 * in the background when the notification is selected. Used on television platforms.
1076 * The URI must point to an image stream suitable for passing into
Jose Limae9e3b3b2014-05-18 23:44:50 -07001077 * {@link android.graphics.BitmapFactory#decodeStream(java.io.InputStream)
Julia Reynoldse0d711f2017-09-01 08:50:47 -04001078 * BitmapFactory.decodeStream}; all other content types will be ignored.
Jose Limae9e3b3b2014-05-18 23:44:50 -07001079 */
1080 public static final String EXTRA_BACKGROUND_IMAGE_URI = "android.backgroundImageUri";
1081
1082 /**
Dan Sandler842dd772014-05-15 09:36:47 -04001083 * {@link #extras} key: A
Jeff Browndba34ba2014-06-24 20:46:03 -07001084 * {@link android.media.session.MediaSession.Token} associated with a
Dan Sandler842dd772014-05-15 09:36:47 -04001085 * {@link android.app.Notification.MediaStyle} notification.
1086 */
1087 public static final String EXTRA_MEDIA_SESSION = "android.mediaSession";
1088
1089 /**
Bryan Mawhinneye191f902014-07-22 12:50:09 +01001090 * {@link #extras} key: the indices of actions to be shown in the compact view,
1091 * as supplied to (e.g.) {@link MediaStyle#setShowActionsInCompactView(int...)}.
1092 */
1093 public static final String EXTRA_COMPACT_ACTIONS = "android.compactActions";
1094
Christoph Studer943aa672014-08-03 20:31:16 +02001095 /**
Alex Hillsfc737de2016-03-23 17:33:02 -04001096 * {@link #extras} key: the username to be displayed for all messages sent by the user including
1097 * direct replies
Adrian Roos96b7e202016-05-17 13:50:38 -07001098 * {@link android.app.Notification.MessagingStyle} notification. This extra is a
1099 * {@link CharSequence}
Selim Cinekcb8b9852017-12-15 18:01:52 -08001100 *
1101 * @deprecated use {@link #EXTRA_MESSAGING_PERSON}
Alex Hillsfc737de2016-03-23 17:33:02 -04001102 */
1103 public static final String EXTRA_SELF_DISPLAY_NAME = "android.selfDisplayName";
1104
1105 /**
Selim Cinekcb8b9852017-12-15 18:01:52 -08001106 * {@link #extras} key: the person to be displayed for all messages sent by the user including
1107 * direct replies
1108 * {@link android.app.Notification.MessagingStyle} notification. This extra is a
1109 * {@link Person}
1110 */
1111 public static final String EXTRA_MESSAGING_PERSON = "android.messagingUser";
1112
1113 /**
Adrian Roos96b7e202016-05-17 13:50:38 -07001114 * {@link #extras} key: a {@link CharSequence} to be displayed as the title to a conversation
Alex Hillsd9b04d92016-04-11 16:38:16 -04001115 * represented by a {@link android.app.Notification.MessagingStyle}
Alex Hillsfc737de2016-03-23 17:33:02 -04001116 */
Alex Hillsd9b04d92016-04-11 16:38:16 -04001117 public static final String EXTRA_CONVERSATION_TITLE = "android.conversationTitle";
Alex Hillsfc737de2016-03-23 17:33:02 -04001118
1119 /**
1120 * {@link #extras} key: an array of {@link android.app.Notification.MessagingStyle.Message}
1121 * bundles provided by a
Adrian Roos96b7e202016-05-17 13:50:38 -07001122 * {@link android.app.Notification.MessagingStyle} notification. This extra is a parcelable
1123 * array of bundles.
Alex Hillsfc737de2016-03-23 17:33:02 -04001124 */
1125 public static final String EXTRA_MESSAGES = "android.messages";
1126
1127 /**
Adrian Roos437cd562017-01-18 15:47:03 -08001128 * {@link #extras} key: an array of
1129 * {@link android.app.Notification.MessagingStyle#addHistoricMessage historic}
1130 * {@link android.app.Notification.MessagingStyle.Message} bundles provided by a
1131 * {@link android.app.Notification.MessagingStyle} notification. This extra is a parcelable
1132 * array of bundles.
1133 */
1134 public static final String EXTRA_HISTORIC_MESSAGES = "android.messages.historic";
1135
1136 /**
Kodlee Yin9ac617c2017-12-19 11:20:50 -08001137 * {@link #extras} key: whether the {@link android.app.Notification.MessagingStyle} notification
1138 * represents a group conversation.
1139 */
1140 public static final String EXTRA_IS_GROUP_CONVERSATION = "android.isGroupConversation";
1141
1142 /**
Selim Cinek7b9605b2017-01-19 17:36:00 -08001143 * {@link #extras} key: whether the notification should be colorized as
1144 * supplied to {@link Builder#setColorized(boolean)}}.
1145 */
1146 public static final String EXTRA_COLORIZED = "android.colorized";
1147
1148 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -04001149 * @hide
1150 */
1151 public static final String EXTRA_BUILDER_APPLICATION_INFO = "android.appInfo";
1152
Selim Cinek247fa012016-02-18 09:50:48 -08001153 /**
1154 * @hide
1155 */
1156 public static final String EXTRA_CONTAINS_CUSTOM_VIEW = "android.contains.customView";
1157
Shane Brennan472a3b32016-12-12 15:28:10 -08001158 /**
Selim Cinekd0426622017-07-11 13:19:59 +02001159 * @hide
1160 */
1161 public static final String EXTRA_REDUCED_IMAGES = "android.reduced.images";
1162
1163 /**
Shane Brennan472a3b32016-12-12 15:28:10 -08001164 * {@link #extras} key: the audio contents of this notification.
1165 *
1166 * This is for use when rendering the notification on an audio-focused interface;
1167 * the audio contents are a complete sound sample that contains the contents/body of the
1168 * notification. This may be used in substitute of a Text-to-Speech reading of the
1169 * notification. For example if the notification represents a voice message this should point
1170 * to the audio of that message.
1171 *
1172 * The data stored under this key should be a String representation of a Uri that contains the
1173 * audio contents in one of the following formats: WAV, PCM 16-bit, AMR-WB.
1174 *
1175 * This extra is unnecessary if you are using {@code MessagingStyle} since each {@code Message}
1176 * has a field for holding data URI. That field can be used for audio.
1177 * See {@code Message#setData}.
1178 *
1179 * Example usage:
1180 * <pre>
1181 * {@code
1182 * Notification.Builder myBuilder = (build your Notification as normal);
1183 * myBuilder.getExtras().putString(EXTRA_AUDIO_CONTENTS_URI, myAudioUri.toString());
1184 * }
1185 * </pre>
1186 */
1187 public static final String EXTRA_AUDIO_CONTENTS_URI = "android.audioContents";
1188
Dan Sandler80eaa592016-04-14 23:34:54 -04001189 /** @hide */
1190 @SystemApi
Jeff Sharkeyd86b8fe2017-06-02 17:36:26 -06001191 @RequiresPermission(android.Manifest.permission.SUBSTITUTE_NOTIFICATION_APP_NAME)
Dan Sandler732bd6c2016-04-12 14:20:32 -04001192 public static final String EXTRA_SUBSTITUTE_APP_NAME = "android.substName";
1193
Dianne Hackbornfb5d4b52017-05-16 17:03:44 -07001194 /**
1195 * This is set on the notification shown by the activity manager about all apps
1196 * running in the background. It indicates that the notification should be shown
1197 * only if any of the given apps do not already have a {@link #FLAG_FOREGROUND_SERVICE}
1198 * notification currently visible to the user. This is a string array of all
1199 * package names of the apps.
1200 * @hide
1201 */
1202 public static final String EXTRA_FOREGROUND_APPS = "android.foregroundApps";
1203
Dan Sandlerd63f9322015-05-06 15:18:49 -04001204 private Icon mSmallIcon;
1205 private Icon mLargeIcon;
1206
Julia Reynoldsb5e44b72016-08-16 15:00:25 -04001207 private String mChannelId;
Julia Reynolds2a128742016-11-28 14:29:25 -05001208 private long mTimeout;
Julia Reynoldsb5e44b72016-08-16 15:00:25 -04001209
Julia Reynolds13d898c2017-02-02 12:22:05 -05001210 private String mShortcutId;
Julia Reynolds3aedded2017-03-31 14:42:09 -04001211 private CharSequence mSettingsText;
Julia Reynolds13d898c2017-02-02 12:22:05 -05001212
Julia Reynoldsa79c3712017-04-21 10:29:57 -04001213 /** @hide */
1214 @IntDef(prefix = { "GROUP_ALERT_" }, value = {
1215 GROUP_ALERT_ALL, GROUP_ALERT_CHILDREN, GROUP_ALERT_SUMMARY
1216 })
1217 @Retention(RetentionPolicy.SOURCE)
1218 public @interface GroupAlertBehavior {}
1219
1220 /**
1221 * Constant for {@link Builder#setGroupAlertBehavior(int)}, meaning that all notifications in a
1222 * group with sound or vibration ought to make sound or vibrate (respectively), so this
1223 * notification will not be muted when it is in a group.
1224 */
1225 public static final int GROUP_ALERT_ALL = 0;
1226
1227 /**
1228 * Constant for {@link Builder#setGroupAlertBehavior(int)}, meaning that all children
1229 * notification in a group should be silenced (no sound or vibration) even if they are posted
1230 * to a {@link NotificationChannel} that has sound and/or vibration. Use this constant to
Julia Reynolds399d9bf2017-08-11 12:52:14 -04001231 * mute this notification if this notification is a group child. This must be applied to all
1232 * children notifications you want to mute.
Julia Reynoldsa79c3712017-04-21 10:29:57 -04001233 *
1234 * <p> For example, you might want to use this constant if you post a number of children
1235 * notifications at once (say, after a periodic sync), and only need to notify the user
1236 * audibly once.
1237 */
1238 public static final int GROUP_ALERT_SUMMARY = 1;
1239
1240 /**
1241 * Constant for {@link Builder#setGroupAlertBehavior(int)}, meaning that the summary
1242 * notification in a group should be silenced (no sound or vibration) even if they are
1243 * posted to a {@link NotificationChannel} that has sound and/or vibration. Use this constant
1244 * to mute this notification if this notification is a group summary.
1245 *
1246 * <p>For example, you might want to use this constant if only the children notifications
Julia Reynolds399d9bf2017-08-11 12:52:14 -04001247 * in your group have content and the summary is only used to visually group notifications
1248 * rather than to alert the user that new information is available.
Julia Reynoldsa79c3712017-04-21 10:29:57 -04001249 */
1250 public static final int GROUP_ALERT_CHILDREN = 2;
1251
Julia Reynolds2f431e22017-06-07 14:12:09 +00001252 private int mGroupAlertBehavior = GROUP_ALERT_ALL;
Julia Reynoldsa79c3712017-04-21 10:29:57 -04001253
Julia Reynolds13d898c2017-02-02 12:22:05 -05001254 /**
1255 * If this notification is being shown as a badge, always show as a number.
1256 */
1257 public static final int BADGE_ICON_NONE = 0;
1258
1259 /**
1260 * If this notification is being shown as a badge, use the {@link #getSmallIcon()} to
1261 * represent this notification.
1262 */
1263 public static final int BADGE_ICON_SMALL = 1;
1264
1265 /**
1266 * If this notification is being shown as a badge, use the {@link #getLargeIcon()} to
1267 * represent this notification.
1268 */
1269 public static final int BADGE_ICON_LARGE = 2;
Julia Reynolds7d5b4da2017-03-20 10:18:49 -04001270 private int mBadgeIcon = BADGE_ICON_NONE;
Julia Reynolds13d898c2017-02-02 12:22:05 -05001271
Chris Wren51c75102013-07-16 20:49:17 -04001272 /**
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001273 * Structure to encapsulate a named action that can be shown as part of this notification.
1274 * It must include an icon, a label, and a {@link PendingIntent} to be fired when the action is
1275 * selected by the user.
1276 * <p>
Griff Hazen959591e2014-05-15 22:26:18 -07001277 * Apps should use {@link Notification.Builder#addAction(int, CharSequence, PendingIntent)}
1278 * or {@link Notification.Builder#addAction(Notification.Action)}
1279 * to attach actions.
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001280 */
Daniel Sandlerea2a3172013-02-20 22:24:20 -05001281 public static class Action implements Parcelable {
Shane Brennan472a3b32016-12-12 15:28:10 -08001282 /**
1283 * {@link #extras} key: Keys to a {@link Parcelable} {@link ArrayList} of
1284 * {@link RemoteInput}s.
1285 *
1286 * This is intended for {@link RemoteInput}s that only accept data, meaning
1287 * {@link RemoteInput#getAllowFreeFormInput} is false, {@link RemoteInput#getChoices}
1288 * is null or empty, and {@link RemoteInput#getAllowedDataTypes is non-null and not
1289 * empty. These {@link RemoteInput}s will be ignored by devices that do not
1290 * support non-text-based {@link RemoteInput}s. See {@link Builder#build}.
1291 *
1292 * You can test if a RemoteInput matches these constraints using
1293 * {@link RemoteInput#isDataOnly}.
1294 */
1295 private static final String EXTRA_DATA_ONLY_INPUTS = "android.extra.DATA_ONLY_INPUTS";
1296
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001297 /**
1298 * {@link }: No semantic action defined.
1299 */
1300 public static final int SEMANTIC_ACTION_NONE = 0;
1301
1302 /**
1303 * {@code SemanticAction}: Reply to a conversation, chat, group, or wherever replies
1304 * may be appropriate.
1305 */
1306 public static final int SEMANTIC_ACTION_REPLY = 1;
1307
1308 /**
1309 * {@code SemanticAction}: Mark content as read.
1310 */
1311 public static final int SEMANTIC_ACTION_MARK_AS_READ = 2;
1312
1313 /**
1314 * {@code SemanticAction}: Mark content as unread.
1315 */
1316 public static final int SEMANTIC_ACTION_MARK_AS_UNREAD = 3;
1317
1318 /**
1319 * {@code SemanticAction}: Delete the content associated with the notification. This
1320 * could mean deleting an email, message, etc.
1321 */
1322 public static final int SEMANTIC_ACTION_DELETE = 4;
1323
1324 /**
1325 * {@code SemanticAction}: Archive the content associated with the notification. This
1326 * could mean archiving an email, message, etc.
1327 */
1328 public static final int SEMANTIC_ACTION_ARCHIVE = 5;
1329
1330 /**
1331 * {@code SemanticAction}: Mute the content associated with the notification. This could
1332 * mean silencing a conversation or currently playing media.
1333 */
1334 public static final int SEMANTIC_ACTION_MUTE = 6;
1335
1336 /**
1337 * {@code SemanticAction}: Unmute the content associated with the notification. This could
1338 * mean un-silencing a conversation or currently playing media.
1339 */
1340 public static final int SEMANTIC_ACTION_UNMUTE = 7;
1341
1342 /**
1343 * {@code SemanticAction}: Mark content with a thumbs up.
1344 */
1345 public static final int SEMANTIC_ACTION_THUMBS_UP = 8;
1346
1347 /**
1348 * {@code SemanticAction}: Mark content with a thumbs down.
1349 */
1350 public static final int SEMANTIC_ACTION_THUMBS_DOWN = 9;
1351
Kodlee Yincda5b092018-02-15 15:34:53 -08001352 /**
1353 * {@code SemanticAction}: Call a contact, group, etc.
1354 */
1355 public static final int SEMANTIC_ACTION_CALL = 10;
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001356
Griff Hazen959591e2014-05-15 22:26:18 -07001357 private final Bundle mExtras;
Dan Sandler86647982015-05-13 23:41:13 -04001358 private Icon mIcon;
Griff Hazen61a9e862014-05-22 16:05:19 -07001359 private final RemoteInput[] mRemoteInputs;
Alex Hills1f27f882017-01-12 11:24:46 -05001360 private boolean mAllowGeneratedReplies = true;
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001361 private final @SemanticAction int mSemanticAction;
Griff Hazen959591e2014-05-15 22:26:18 -07001362
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001363 /**
1364 * Small icon representing the action.
Dan Sandler86647982015-05-13 23:41:13 -04001365 *
1366 * @deprecated Use {@link Action#getIcon()} instead.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001367 */
Dan Sandler86647982015-05-13 23:41:13 -04001368 @Deprecated
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001369 public int icon;
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001370
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001371 /**
1372 * Title of the action.
1373 */
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001374 public CharSequence title;
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001375
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001376 /**
1377 * Intent to send when the user invokes this action. May be null, in which case the action
1378 * may be rendered in a disabled presentation by the system UI.
1379 */
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001380 public PendingIntent actionIntent;
Griff Hazen959591e2014-05-15 22:26:18 -07001381
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001382 private Action(Parcel in) {
Dan Sandler86647982015-05-13 23:41:13 -04001383 if (in.readInt() != 0) {
1384 mIcon = Icon.CREATOR.createFromParcel(in);
Dan Sandler68079d52015-07-22 10:45:30 -04001385 if (mIcon.getType() == Icon.TYPE_RESOURCE) {
1386 icon = mIcon.getResId();
1387 }
Dan Sandler86647982015-05-13 23:41:13 -04001388 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001389 title = TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(in);
1390 if (in.readInt() == 1) {
1391 actionIntent = PendingIntent.CREATOR.createFromParcel(in);
1392 }
Jeff Sharkeya04c7a72016-03-18 12:20:36 -06001393 mExtras = Bundle.setDefusable(in.readBundle(), true);
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001394 mRemoteInputs = in.createTypedArray(RemoteInput.CREATOR);
Alex Hills42b0c4d2016-04-26 13:35:36 -04001395 mAllowGeneratedReplies = in.readInt() == 1;
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001396 mSemanticAction = in.readInt();
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001397 }
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001398
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001399 /**
Dan Sandler86647982015-05-13 23:41:13 -04001400 * @deprecated Use {@link android.app.Notification.Action.Builder}.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001401 */
Dan Sandler86647982015-05-13 23:41:13 -04001402 @Deprecated
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001403 public Action(int icon, CharSequence title, PendingIntent intent) {
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001404 this(Icon.createWithResource("", icon), title, intent, new Bundle(), null, true,
1405 SEMANTIC_ACTION_NONE);
Griff Hazen959591e2014-05-15 22:26:18 -07001406 }
1407
Adrian Roos7af53622016-10-12 13:44:05 -07001408 /** Keep in sync with {@link Notification.Action.Builder#Builder(Action)}! */
Dan Sandler86647982015-05-13 23:41:13 -04001409 private Action(Icon icon, CharSequence title, PendingIntent intent, Bundle extras,
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001410 RemoteInput[] remoteInputs, boolean allowGeneratedReplies,
1411 @SemanticAction int semanticAction) {
Dan Sandler86647982015-05-13 23:41:13 -04001412 this.mIcon = icon;
Gus Prevasf5bff46f2015-08-24 10:34:28 -04001413 if (icon != null && icon.getType() == Icon.TYPE_RESOURCE) {
1414 this.icon = icon.getResId();
1415 }
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001416 this.title = title;
1417 this.actionIntent = intent;
Griff Hazen959591e2014-05-15 22:26:18 -07001418 this.mExtras = extras != null ? extras : new Bundle();
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001419 this.mRemoteInputs = remoteInputs;
Alex Hills42b0c4d2016-04-26 13:35:36 -04001420 this.mAllowGeneratedReplies = allowGeneratedReplies;
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001421 this.mSemanticAction = semanticAction;
Griff Hazen959591e2014-05-15 22:26:18 -07001422 }
1423
1424 /**
Dan Sandler86647982015-05-13 23:41:13 -04001425 * Return an icon representing the action.
1426 */
1427 public Icon getIcon() {
1428 if (mIcon == null && icon != 0) {
1429 // you snuck an icon in here without using the builder; let's try to keep it
1430 mIcon = Icon.createWithResource("", icon);
1431 }
1432 return mIcon;
1433 }
1434
1435 /**
Griff Hazen959591e2014-05-15 22:26:18 -07001436 * Get additional metadata carried around with this Action.
1437 */
1438 public Bundle getExtras() {
1439 return mExtras;
1440 }
1441
1442 /**
Alex Hills42b0c4d2016-04-26 13:35:36 -04001443 * Return whether the platform should automatically generate possible replies for this
1444 * {@link Action}
1445 */
1446 public boolean getAllowGeneratedReplies() {
1447 return mAllowGeneratedReplies;
1448 }
1449
1450 /**
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001451 * Get the list of inputs to be collected from the user when this action is sent.
Shane Brennan472a3b32016-12-12 15:28:10 -08001452 * May return null if no remote inputs were added. Only returns inputs which accept
1453 * a text input. For inputs which only accept data use {@link #getDataOnlyRemoteInputs}.
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001454 */
1455 public RemoteInput[] getRemoteInputs() {
1456 return mRemoteInputs;
1457 }
1458
1459 /**
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001460 * Returns the {@code SemanticAction} associated with this {@link Action}. A
1461 * {@code SemanticAction} denotes what an {@link Action}'s {@link PendingIntent} will do
1462 * (eg. reply, mark as read, delete, etc).
1463 */
1464 public @SemanticAction int getSemanticAction() {
1465 return mSemanticAction;
1466 }
1467
1468 /**
Shane Brennan472a3b32016-12-12 15:28:10 -08001469 * Get the list of inputs to be collected from the user that ONLY accept data when this
1470 * action is sent. These remote inputs are guaranteed to return true on a call to
1471 * {@link RemoteInput#isDataOnly}.
1472 *
Shane Brennanf670d6c2017-04-25 13:05:45 -07001473 * Returns null if there are no data-only remote inputs.
Shane Brennan472a3b32016-12-12 15:28:10 -08001474 *
1475 * This method exists so that legacy RemoteInput collectors that pre-date the addition
1476 * of non-textual RemoteInputs do not access these remote inputs.
1477 */
1478 public RemoteInput[] getDataOnlyRemoteInputs() {
1479 return (RemoteInput[]) mExtras.getParcelableArray(EXTRA_DATA_ONLY_INPUTS);
1480 }
1481
1482 /**
Griff Hazen959591e2014-05-15 22:26:18 -07001483 * Builder class for {@link Action} objects.
1484 */
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001485 public static final class Builder {
Dan Sandler86647982015-05-13 23:41:13 -04001486 private final Icon mIcon;
Griff Hazen959591e2014-05-15 22:26:18 -07001487 private final CharSequence mTitle;
1488 private final PendingIntent mIntent;
Alex Hills1f27f882017-01-12 11:24:46 -05001489 private boolean mAllowGeneratedReplies = true;
Griff Hazen959591e2014-05-15 22:26:18 -07001490 private final Bundle mExtras;
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001491 private ArrayList<RemoteInput> mRemoteInputs;
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001492 private @SemanticAction int mSemanticAction;
Griff Hazen959591e2014-05-15 22:26:18 -07001493
1494 /**
1495 * Construct a new builder for {@link Action} object.
1496 * @param icon icon to show for this action
1497 * @param title the title of the action
1498 * @param intent the {@link PendingIntent} to fire when users trigger this action
1499 */
Dan Sandler86647982015-05-13 23:41:13 -04001500 @Deprecated
Griff Hazen959591e2014-05-15 22:26:18 -07001501 public Builder(int icon, CharSequence title, PendingIntent intent) {
Adrian Roos7af53622016-10-12 13:44:05 -07001502 this(Icon.createWithResource("", icon), title, intent);
Dan Sandler86647982015-05-13 23:41:13 -04001503 }
1504
1505 /**
1506 * Construct a new builder for {@link Action} object.
1507 * @param icon icon to show for this action
1508 * @param title the title of the action
1509 * @param intent the {@link PendingIntent} to fire when users trigger this action
1510 */
1511 public Builder(Icon icon, CharSequence title, PendingIntent intent) {
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001512 this(icon, title, intent, new Bundle(), null, true, SEMANTIC_ACTION_NONE);
Griff Hazen959591e2014-05-15 22:26:18 -07001513 }
1514
1515 /**
1516 * Construct a new builder for {@link Action} object using the fields from an
1517 * {@link Action}.
1518 * @param action the action to read fields from.
1519 */
1520 public Builder(Action action) {
Adrian Roos7af53622016-10-12 13:44:05 -07001521 this(action.getIcon(), action.title, action.actionIntent,
1522 new Bundle(action.mExtras), action.getRemoteInputs(),
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001523 action.getAllowGeneratedReplies(), action.getSemanticAction());
Griff Hazen959591e2014-05-15 22:26:18 -07001524 }
1525
Dan Sandler86647982015-05-13 23:41:13 -04001526 private Builder(Icon icon, CharSequence title, PendingIntent intent, Bundle extras,
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001527 RemoteInput[] remoteInputs, boolean allowGeneratedReplies,
1528 @SemanticAction int semanticAction) {
Griff Hazen959591e2014-05-15 22:26:18 -07001529 mIcon = icon;
1530 mTitle = title;
1531 mIntent = intent;
1532 mExtras = extras;
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001533 if (remoteInputs != null) {
1534 mRemoteInputs = new ArrayList<RemoteInput>(remoteInputs.length);
1535 Collections.addAll(mRemoteInputs, remoteInputs);
1536 }
Adrian Roos7af53622016-10-12 13:44:05 -07001537 mAllowGeneratedReplies = allowGeneratedReplies;
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001538 mSemanticAction = semanticAction;
Griff Hazen959591e2014-05-15 22:26:18 -07001539 }
1540
1541 /**
1542 * Merge additional metadata into this builder.
1543 *
1544 * <p>Values within the Bundle will replace existing extras values in this Builder.
1545 *
1546 * @see Notification.Action#extras
1547 */
1548 public Builder addExtras(Bundle extras) {
1549 if (extras != null) {
1550 mExtras.putAll(extras);
1551 }
1552 return this;
1553 }
1554
1555 /**
1556 * Get the metadata Bundle used by this Builder.
1557 *
1558 * <p>The returned Bundle is shared with this Builder.
1559 */
1560 public Bundle getExtras() {
1561 return mExtras;
1562 }
1563
1564 /**
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001565 * Add an input to be collected from the user when this action is sent.
1566 * Response values can be retrieved from the fired intent by using the
1567 * {@link RemoteInput#getResultsFromIntent} function.
1568 * @param remoteInput a {@link RemoteInput} to add to the action
1569 * @return this object for method chaining
1570 */
1571 public Builder addRemoteInput(RemoteInput remoteInput) {
1572 if (mRemoteInputs == null) {
1573 mRemoteInputs = new ArrayList<RemoteInput>();
1574 }
1575 mRemoteInputs.add(remoteInput);
1576 return this;
1577 }
1578
1579 /**
Alex Hills42b0c4d2016-04-26 13:35:36 -04001580 * Set whether the platform should automatically generate possible replies to add to
1581 * {@link RemoteInput#getChoices()}. If the {@link Action} doesn't have a
1582 * {@link RemoteInput}, this has no effect.
1583 * @param allowGeneratedReplies {@code true} to allow generated replies, {@code false}
1584 * otherwise
1585 * @return this object for method chaining
Alex Hills1f27f882017-01-12 11:24:46 -05001586 * The default value is {@code true}
Alex Hills42b0c4d2016-04-26 13:35:36 -04001587 */
1588 public Builder setAllowGeneratedReplies(boolean allowGeneratedReplies) {
1589 mAllowGeneratedReplies = allowGeneratedReplies;
1590 return this;
1591 }
1592
1593 /**
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001594 * Sets the {@code SemanticAction} for this {@link Action}. A
1595 * {@code SemanticAction} denotes what an {@link Action}'s
1596 * {@link PendingIntent} will do (eg. reply, mark as read, delete, etc).
1597 * @param semanticAction a SemanticAction defined within {@link Action} with
1598 * {@code SEMANTIC_ACTION_} prefixes
1599 * @return this object for method chaining
1600 */
1601 public Builder setSemanticAction(@SemanticAction int semanticAction) {
1602 mSemanticAction = semanticAction;
1603 return this;
1604 }
1605
1606 /**
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001607 * Apply an extender to this action builder. Extenders may be used to add
1608 * metadata or change options on this builder.
1609 */
Griff Hazen61a9e862014-05-22 16:05:19 -07001610 public Builder extend(Extender extender) {
1611 extender.extend(this);
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001612 return this;
1613 }
1614
1615 /**
Griff Hazen959591e2014-05-15 22:26:18 -07001616 * Combine all of the options that have been set and return a new {@link Action}
1617 * object.
1618 * @return the built action
1619 */
1620 public Action build() {
Shane Brennan472a3b32016-12-12 15:28:10 -08001621 ArrayList<RemoteInput> dataOnlyInputs = new ArrayList<>();
1622 RemoteInput[] previousDataInputs =
1623 (RemoteInput[]) mExtras.getParcelableArray(EXTRA_DATA_ONLY_INPUTS);
Felipe Lemedfd11962017-01-18 18:38:46 -08001624 if (previousDataInputs != null) {
Shane Brennan472a3b32016-12-12 15:28:10 -08001625 for (RemoteInput input : previousDataInputs) {
1626 dataOnlyInputs.add(input);
1627 }
1628 }
1629 List<RemoteInput> textInputs = new ArrayList<>();
1630 if (mRemoteInputs != null) {
1631 for (RemoteInput input : mRemoteInputs) {
1632 if (input.isDataOnly()) {
1633 dataOnlyInputs.add(input);
1634 } else {
1635 textInputs.add(input);
1636 }
1637 }
1638 }
1639 if (!dataOnlyInputs.isEmpty()) {
1640 RemoteInput[] dataInputsArr =
1641 dataOnlyInputs.toArray(new RemoteInput[dataOnlyInputs.size()]);
1642 mExtras.putParcelableArray(EXTRA_DATA_ONLY_INPUTS, dataInputsArr);
1643 }
1644 RemoteInput[] textInputsArr = textInputs.isEmpty()
1645 ? null : textInputs.toArray(new RemoteInput[textInputs.size()]);
1646 return new Action(mIcon, mTitle, mIntent, mExtras, textInputsArr,
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001647 mAllowGeneratedReplies, mSemanticAction);
Griff Hazen959591e2014-05-15 22:26:18 -07001648 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001649 }
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001650
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001651 @Override
1652 public Action clone() {
1653 return new Action(
Dan Sandler86647982015-05-13 23:41:13 -04001654 getIcon(),
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001655 title,
1656 actionIntent, // safe to alias
Julia Reynolds53c934c2016-09-16 14:03:43 -04001657 mExtras == null ? new Bundle() : new Bundle(mExtras),
Alex Hills42b0c4d2016-04-26 13:35:36 -04001658 getRemoteInputs(),
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001659 getAllowGeneratedReplies(),
1660 getSemanticAction());
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001661 }
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001662
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001663 @Override
1664 public int describeContents() {
1665 return 0;
1666 }
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001667
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001668 @Override
1669 public void writeToParcel(Parcel out, int flags) {
Dan Sandler86647982015-05-13 23:41:13 -04001670 final Icon ic = getIcon();
1671 if (ic != null) {
1672 out.writeInt(1);
1673 ic.writeToParcel(out, 0);
1674 } else {
1675 out.writeInt(0);
1676 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001677 TextUtils.writeToParcel(title, out, flags);
1678 if (actionIntent != null) {
1679 out.writeInt(1);
1680 actionIntent.writeToParcel(out, flags);
1681 } else {
1682 out.writeInt(0);
1683 }
Griff Hazen959591e2014-05-15 22:26:18 -07001684 out.writeBundle(mExtras);
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001685 out.writeTypedArray(mRemoteInputs, flags);
Alex Hills42b0c4d2016-04-26 13:35:36 -04001686 out.writeInt(mAllowGeneratedReplies ? 1 : 0);
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001687 out.writeInt(mSemanticAction);
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001688 }
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001689
Griff Hazen959591e2014-05-15 22:26:18 -07001690 public static final Parcelable.Creator<Action> CREATOR =
1691 new Parcelable.Creator<Action>() {
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001692 public Action createFromParcel(Parcel in) {
1693 return new Action(in);
1694 }
1695 public Action[] newArray(int size) {
1696 return new Action[size];
1697 }
1698 };
Griff Hazen61a9e862014-05-22 16:05:19 -07001699
1700 /**
1701 * Extender interface for use with {@link Builder#extend}. Extenders may be used to add
1702 * metadata or change options on an action builder.
1703 */
1704 public interface Extender {
1705 /**
1706 * Apply this extender to a notification action builder.
1707 * @param builder the builder to be modified.
1708 * @return the build object for chaining.
1709 */
1710 public Builder extend(Builder builder);
1711 }
1712
1713 /**
1714 * Wearable extender for notification actions. To add extensions to an action,
1715 * create a new {@link android.app.Notification.Action.WearableExtender} object using
1716 * the {@code WearableExtender()} constructor and apply it to a
1717 * {@link android.app.Notification.Action.Builder} using
1718 * {@link android.app.Notification.Action.Builder#extend}.
1719 *
1720 * <pre class="prettyprint">
1721 * Notification.Action action = new Notification.Action.Builder(
1722 * R.drawable.archive_all, "Archive all", actionIntent)
Griff Hazen14f57992014-05-26 09:07:14 -07001723 * .extend(new Notification.Action.WearableExtender()
Griff Hazen61a9e862014-05-22 16:05:19 -07001724 * .setAvailableOffline(false))
Griff Hazen14f57992014-05-26 09:07:14 -07001725 * .build();</pre>
Griff Hazen61a9e862014-05-22 16:05:19 -07001726 */
1727 public static final class WearableExtender implements Extender {
1728 /** Notification action extra which contains wearable extensions */
1729 private static final String EXTRA_WEARABLE_EXTENSIONS = "android.wearable.EXTENSIONS";
1730
Pete Gastaf6781d2014-10-07 15:17:05 -04001731 // Keys within EXTRA_WEARABLE_EXTENSIONS for wearable options.
Griff Hazen61a9e862014-05-22 16:05:19 -07001732 private static final String KEY_FLAGS = "flags";
Pete Gastaf6781d2014-10-07 15:17:05 -04001733 private static final String KEY_IN_PROGRESS_LABEL = "inProgressLabel";
1734 private static final String KEY_CONFIRM_LABEL = "confirmLabel";
1735 private static final String KEY_CANCEL_LABEL = "cancelLabel";
Griff Hazen61a9e862014-05-22 16:05:19 -07001736
1737 // Flags bitwise-ored to mFlags
1738 private static final int FLAG_AVAILABLE_OFFLINE = 0x1;
Alex Hills9ab3a232016-04-05 14:54:56 -04001739 private static final int FLAG_HINT_LAUNCHES_ACTIVITY = 1 << 1;
Alex Hills9f087612016-06-07 09:08:59 -04001740 private static final int FLAG_HINT_DISPLAY_INLINE = 1 << 2;
Griff Hazen61a9e862014-05-22 16:05:19 -07001741
1742 // Default value for flags integer
1743 private static final int DEFAULT_FLAGS = FLAG_AVAILABLE_OFFLINE;
1744
1745 private int mFlags = DEFAULT_FLAGS;
1746
Pete Gastaf6781d2014-10-07 15:17:05 -04001747 private CharSequence mInProgressLabel;
1748 private CharSequence mConfirmLabel;
1749 private CharSequence mCancelLabel;
1750
Griff Hazen61a9e862014-05-22 16:05:19 -07001751 /**
1752 * Create a {@link android.app.Notification.Action.WearableExtender} with default
1753 * options.
1754 */
1755 public WearableExtender() {
1756 }
1757
1758 /**
1759 * Create a {@link android.app.Notification.Action.WearableExtender} by reading
1760 * wearable options present in an existing notification action.
1761 * @param action the notification action to inspect.
1762 */
1763 public WearableExtender(Action action) {
1764 Bundle wearableBundle = action.getExtras().getBundle(EXTRA_WEARABLE_EXTENSIONS);
1765 if (wearableBundle != null) {
1766 mFlags = wearableBundle.getInt(KEY_FLAGS, DEFAULT_FLAGS);
Pete Gastaf6781d2014-10-07 15:17:05 -04001767 mInProgressLabel = wearableBundle.getCharSequence(KEY_IN_PROGRESS_LABEL);
1768 mConfirmLabel = wearableBundle.getCharSequence(KEY_CONFIRM_LABEL);
1769 mCancelLabel = wearableBundle.getCharSequence(KEY_CANCEL_LABEL);
Griff Hazen61a9e862014-05-22 16:05:19 -07001770 }
1771 }
1772
1773 /**
1774 * Apply wearable extensions to a notification action that is being built. This is
1775 * typically called by the {@link android.app.Notification.Action.Builder#extend}
1776 * method of {@link android.app.Notification.Action.Builder}.
1777 */
1778 @Override
1779 public Action.Builder extend(Action.Builder builder) {
1780 Bundle wearableBundle = new Bundle();
1781
1782 if (mFlags != DEFAULT_FLAGS) {
1783 wearableBundle.putInt(KEY_FLAGS, mFlags);
1784 }
Pete Gastaf6781d2014-10-07 15:17:05 -04001785 if (mInProgressLabel != null) {
1786 wearableBundle.putCharSequence(KEY_IN_PROGRESS_LABEL, mInProgressLabel);
1787 }
1788 if (mConfirmLabel != null) {
1789 wearableBundle.putCharSequence(KEY_CONFIRM_LABEL, mConfirmLabel);
1790 }
1791 if (mCancelLabel != null) {
1792 wearableBundle.putCharSequence(KEY_CANCEL_LABEL, mCancelLabel);
1793 }
Griff Hazen61a9e862014-05-22 16:05:19 -07001794
1795 builder.getExtras().putBundle(EXTRA_WEARABLE_EXTENSIONS, wearableBundle);
1796 return builder;
1797 }
1798
1799 @Override
1800 public WearableExtender clone() {
1801 WearableExtender that = new WearableExtender();
1802 that.mFlags = this.mFlags;
Pete Gastaf6781d2014-10-07 15:17:05 -04001803 that.mInProgressLabel = this.mInProgressLabel;
1804 that.mConfirmLabel = this.mConfirmLabel;
1805 that.mCancelLabel = this.mCancelLabel;
Griff Hazen61a9e862014-05-22 16:05:19 -07001806 return that;
1807 }
1808
1809 /**
1810 * Set whether this action is available when the wearable device is not connected to
1811 * a companion device. The user can still trigger this action when the wearable device is
1812 * offline, but a visual hint will indicate that the action may not be available.
1813 * Defaults to true.
1814 */
1815 public WearableExtender setAvailableOffline(boolean availableOffline) {
1816 setFlag(FLAG_AVAILABLE_OFFLINE, availableOffline);
1817 return this;
1818 }
1819
1820 /**
1821 * Get whether this action is available when the wearable device is not connected to
1822 * a companion device. The user can still trigger this action when the wearable device is
1823 * offline, but a visual hint will indicate that the action may not be available.
1824 * Defaults to true.
1825 */
1826 public boolean isAvailableOffline() {
1827 return (mFlags & FLAG_AVAILABLE_OFFLINE) != 0;
1828 }
1829
1830 private void setFlag(int mask, boolean value) {
1831 if (value) {
1832 mFlags |= mask;
1833 } else {
1834 mFlags &= ~mask;
1835 }
1836 }
Pete Gastaf6781d2014-10-07 15:17:05 -04001837
1838 /**
1839 * Set a label to display while the wearable is preparing to automatically execute the
1840 * action. This is usually a 'ing' verb ending in ellipsis like "Sending..."
1841 *
1842 * @param label the label to display while the action is being prepared to execute
1843 * @return this object for method chaining
1844 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05001845 @Deprecated
Pete Gastaf6781d2014-10-07 15:17:05 -04001846 public WearableExtender setInProgressLabel(CharSequence label) {
1847 mInProgressLabel = label;
1848 return this;
1849 }
1850
1851 /**
1852 * Get the label to display while the wearable is preparing to automatically execute
1853 * the action. This is usually a 'ing' verb ending in ellipsis like "Sending..."
1854 *
1855 * @return the label to display while the action is being prepared to execute
1856 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05001857 @Deprecated
Pete Gastaf6781d2014-10-07 15:17:05 -04001858 public CharSequence getInProgressLabel() {
1859 return mInProgressLabel;
1860 }
1861
1862 /**
1863 * Set a label to display to confirm that the action should be executed.
1864 * This is usually an imperative verb like "Send".
1865 *
1866 * @param label the label to confirm the action should be executed
1867 * @return this object for method chaining
1868 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05001869 @Deprecated
Pete Gastaf6781d2014-10-07 15:17:05 -04001870 public WearableExtender setConfirmLabel(CharSequence label) {
1871 mConfirmLabel = label;
1872 return this;
1873 }
1874
1875 /**
1876 * Get the label to display to confirm that the action should be executed.
1877 * This is usually an imperative verb like "Send".
1878 *
1879 * @return the label to confirm the action should be executed
1880 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05001881 @Deprecated
Pete Gastaf6781d2014-10-07 15:17:05 -04001882 public CharSequence getConfirmLabel() {
1883 return mConfirmLabel;
1884 }
1885
1886 /**
1887 * Set a label to display to cancel the action.
1888 * This is usually an imperative verb, like "Cancel".
1889 *
1890 * @param label the label to display to cancel the action
1891 * @return this object for method chaining
1892 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05001893 @Deprecated
Pete Gastaf6781d2014-10-07 15:17:05 -04001894 public WearableExtender setCancelLabel(CharSequence label) {
1895 mCancelLabel = label;
1896 return this;
1897 }
1898
1899 /**
1900 * Get the label to display to cancel the action.
1901 * This is usually an imperative verb like "Cancel".
1902 *
1903 * @return the label to display to cancel the action
1904 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05001905 @Deprecated
Pete Gastaf6781d2014-10-07 15:17:05 -04001906 public CharSequence getCancelLabel() {
1907 return mCancelLabel;
1908 }
Alex Hills9ab3a232016-04-05 14:54:56 -04001909
1910 /**
1911 * Set a hint that this Action will launch an {@link Activity} directly, telling the
1912 * platform that it can generate the appropriate transitions.
1913 * @param hintLaunchesActivity {@code true} if the content intent will launch
1914 * an activity and transitions should be generated, false otherwise.
1915 * @return this object for method chaining
1916 */
Alex Hills4ec3ff42016-04-12 11:36:18 -04001917 public WearableExtender setHintLaunchesActivity(
Alex Hills9ab3a232016-04-05 14:54:56 -04001918 boolean hintLaunchesActivity) {
1919 setFlag(FLAG_HINT_LAUNCHES_ACTIVITY, hintLaunchesActivity);
1920 return this;
1921 }
1922
1923 /**
1924 * Get a hint that this Action will launch an {@link Activity} directly, telling the
1925 * platform that it can generate the appropriate transitions
1926 * @return {@code true} if the content intent will launch an activity and transitions
1927 * should be generated, false otherwise. The default value is {@code false} if this was
1928 * never set.
1929 */
Alex Hills4ec3ff42016-04-12 11:36:18 -04001930 public boolean getHintLaunchesActivity() {
Alex Hills9ab3a232016-04-05 14:54:56 -04001931 return (mFlags & FLAG_HINT_LAUNCHES_ACTIVITY) != 0;
1932 }
Alex Hills9f087612016-06-07 09:08:59 -04001933
1934 /**
1935 * Set a hint that this Action should be displayed inline.
1936 *
1937 * @param hintDisplayInline {@code true} if action should be displayed inline, false
1938 * otherwise
1939 * @return this object for method chaining
1940 */
1941 public WearableExtender setHintDisplayActionInline(
1942 boolean hintDisplayInline) {
1943 setFlag(FLAG_HINT_DISPLAY_INLINE, hintDisplayInline);
1944 return this;
1945 }
1946
1947 /**
1948 * Get a hint that this Action should be displayed inline.
1949 *
Dmitri Plotnikov9e9cfd92016-12-21 10:52:26 -08001950 * @return {@code true} if the Action should be displayed inline, {@code false}
Alex Hills9f087612016-06-07 09:08:59 -04001951 * otherwise. The default value is {@code false} if this was never set.
1952 */
1953 public boolean getHintDisplayActionInline() {
1954 return (mFlags & FLAG_HINT_DISPLAY_INLINE) != 0;
1955 }
Griff Hazen61a9e862014-05-22 16:05:19 -07001956 }
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001957
1958 /**
1959 * Provides meaning to an {@link Action} that hints at what the associated
1960 * {@link PendingIntent} will do. For example, an {@link Action} with a
1961 * {@link PendingIntent} that replies to a text message notification may have the
1962 * {@link #SEMANTIC_ACTION_REPLY} {@code SemanticAction} set within it.
1963 *
1964 * @hide
1965 */
1966 @IntDef(prefix = { "SEMANTIC_ACTION_" }, value = {
1967 SEMANTIC_ACTION_NONE,
1968 SEMANTIC_ACTION_REPLY,
1969 SEMANTIC_ACTION_MARK_AS_READ,
1970 SEMANTIC_ACTION_MARK_AS_UNREAD,
1971 SEMANTIC_ACTION_DELETE,
1972 SEMANTIC_ACTION_ARCHIVE,
1973 SEMANTIC_ACTION_MUTE,
1974 SEMANTIC_ACTION_UNMUTE,
1975 SEMANTIC_ACTION_THUMBS_UP,
Kodlee Yincda5b092018-02-15 15:34:53 -08001976 SEMANTIC_ACTION_THUMBS_DOWN,
1977 SEMANTIC_ACTION_CALL
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001978 })
1979 @Retention(RetentionPolicy.SOURCE)
1980 public @interface SemanticAction {}
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001981 }
1982
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001983 /**
1984 * Array of all {@link Action} structures attached to this notification by
1985 * {@link Builder#addAction(int, CharSequence, PendingIntent)}. Mostly useful for instances of
1986 * {@link android.service.notification.NotificationListenerService} that provide an alternative
1987 * interface for invoking actions.
1988 */
Daniel Sandlerea2a3172013-02-20 22:24:20 -05001989 public Action[] actions;
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001990
1991 /**
Dan Sandler0bf2ed82013-12-21 23:33:41 -06001992 * Replacement version of this notification whose content will be shown
1993 * in an insecure context such as atop a secure keyguard. See {@link #visibility}
1994 * and {@link #VISIBILITY_PUBLIC}.
1995 */
1996 public Notification publicVersion;
1997
1998 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001999 * Constructs a Notification object with default values.
Joe Onorato46439ce2010-11-19 13:56:21 -08002000 * You might want to consider using {@link Builder} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002001 */
2002 public Notification()
2003 {
2004 this.when = System.currentTimeMillis();
Selim Cinekb85f36fd2016-04-20 18:46:36 -07002005 this.creationTime = System.currentTimeMillis();
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002006 this.priority = PRIORITY_DEFAULT;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002007 }
2008
2009 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002010 * @hide
2011 */
2012 public Notification(Context context, int icon, CharSequence tickerText, long when,
2013 CharSequence contentTitle, CharSequence contentText, Intent contentIntent)
2014 {
Chris Wren1ce4b6d2015-06-11 10:19:43 -04002015 new Builder(context)
2016 .setWhen(when)
2017 .setSmallIcon(icon)
2018 .setTicker(tickerText)
2019 .setContentTitle(contentTitle)
2020 .setContentText(contentText)
2021 .setContentIntent(PendingIntent.getActivity(context, 0, contentIntent, 0))
2022 .buildInto(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002023 }
2024
2025 /**
2026 * Constructs a Notification object with the information needed to
2027 * have a status bar icon without the standard expanded view.
2028 *
2029 * @param icon The resource id of the icon to put in the status bar.
2030 * @param tickerText The text that flows by in the status bar when the notification first
2031 * activates.
2032 * @param when The time to show in the time field. In the System.currentTimeMillis
2033 * timebase.
Joe Onorato46439ce2010-11-19 13:56:21 -08002034 *
2035 * @deprecated Use {@link Builder} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002036 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002037 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002038 public Notification(int icon, CharSequence tickerText, long when)
2039 {
2040 this.icon = icon;
2041 this.tickerText = tickerText;
2042 this.when = when;
Selim Cinekb85f36fd2016-04-20 18:46:36 -07002043 this.creationTime = System.currentTimeMillis();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002044 }
2045
2046 /**
2047 * Unflatten the notification from a parcel.
2048 */
Svet Ganovddb94882016-06-23 19:55:24 -07002049 @SuppressWarnings("unchecked")
2050 public Notification(Parcel parcel) {
2051 // IMPORTANT: Add unmarshaling code in readFromParcel as the pending
2052 // intents in extras are always written as the last entry.
2053 readFromParcelImpl(parcel);
2054 // Must be read last!
Felipe Lemedd85da62016-06-28 11:29:54 -07002055 allPendingIntents = (ArraySet<PendingIntent>) parcel.readArraySet(null);
Svet Ganovddb94882016-06-23 19:55:24 -07002056 }
2057
2058 private void readFromParcelImpl(Parcel parcel)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002059 {
2060 int version = parcel.readInt();
2061
Adrian Roosfb921842017-10-26 14:49:56 +02002062 mWhitelistToken = parcel.readStrongBinder();
2063 if (mWhitelistToken == null) {
2064 mWhitelistToken = processWhitelistToken;
Dianne Hackborn98305522017-05-05 17:53:53 -07002065 }
2066 // Propagate this token to all pending intents that are unmarshalled from the parcel.
Adrian Roosfb921842017-10-26 14:49:56 +02002067 parcel.setClassCookie(PendingIntent.class, mWhitelistToken);
Dianne Hackborn98305522017-05-05 17:53:53 -07002068
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002069 when = parcel.readLong();
Selim Cinekb85f36fd2016-04-20 18:46:36 -07002070 creationTime = parcel.readLong();
Dan Sandler3936e7a2015-05-19 20:59:12 -04002071 if (parcel.readInt() != 0) {
2072 mSmallIcon = Icon.CREATOR.createFromParcel(parcel);
Dan Sandler4e787062015-06-17 15:09:48 -04002073 if (mSmallIcon.getType() == Icon.TYPE_RESOURCE) {
2074 icon = mSmallIcon.getResId();
2075 }
Dan Sandler3936e7a2015-05-19 20:59:12 -04002076 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002077 number = parcel.readInt();
2078 if (parcel.readInt() != 0) {
2079 contentIntent = PendingIntent.CREATOR.createFromParcel(parcel);
2080 }
2081 if (parcel.readInt() != 0) {
2082 deleteIntent = PendingIntent.CREATOR.createFromParcel(parcel);
2083 }
2084 if (parcel.readInt() != 0) {
2085 tickerText = TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(parcel);
2086 }
2087 if (parcel.readInt() != 0) {
Joe Onorato46439ce2010-11-19 13:56:21 -08002088 tickerView = RemoteViews.CREATOR.createFromParcel(parcel);
Joe Onoratoef1e7762010-09-17 18:38:38 -04002089 }
2090 if (parcel.readInt() != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002091 contentView = RemoteViews.CREATOR.createFromParcel(parcel);
2092 }
Joe Onorato561d3852010-11-20 18:09:34 -08002093 if (parcel.readInt() != 0) {
Dan Sandlerd63f9322015-05-06 15:18:49 -04002094 mLargeIcon = Icon.CREATOR.createFromParcel(parcel);
Joe Onorato561d3852010-11-20 18:09:34 -08002095 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002096 defaults = parcel.readInt();
2097 flags = parcel.readInt();
2098 if (parcel.readInt() != 0) {
2099 sound = Uri.CREATOR.createFromParcel(parcel);
2100 }
2101
2102 audioStreamType = parcel.readInt();
John Spurlockc0650f022014-07-19 13:22:39 -04002103 if (parcel.readInt() != 0) {
2104 audioAttributes = AudioAttributes.CREATOR.createFromParcel(parcel);
2105 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002106 vibrate = parcel.createLongArray();
2107 ledARGB = parcel.readInt();
2108 ledOnMS = parcel.readInt();
2109 ledOffMS = parcel.readInt();
2110 iconLevel = parcel.readInt();
Daniel Sandlere46cbd32010-06-17 10:35:26 -04002111
2112 if (parcel.readInt() != 0) {
2113 fullScreenIntent = PendingIntent.CREATOR.createFromParcel(parcel);
2114 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002115
2116 priority = parcel.readInt();
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002117
John Spurlockfd7f1e02014-03-18 16:41:57 -04002118 category = parcel.readString();
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002119
Griff Hazen5cadc3b2014-05-20 09:55:39 -07002120 mGroupKey = parcel.readString();
2121
2122 mSortKey = parcel.readString();
2123
Jeff Sharkeya04c7a72016-03-18 12:20:36 -06002124 extras = Bundle.setDefusable(parcel.readBundle(), true); // may be null
Robin Leead7e72a2017-12-04 15:45:46 +01002125 fixDuplicateExtras();
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002126
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002127 actions = parcel.createTypedArray(Action.CREATOR); // may be null
2128
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002129 if (parcel.readInt() != 0) {
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002130 bigContentView = RemoteViews.CREATOR.createFromParcel(parcel);
2131 }
Dan Sandler0bf2ed82013-12-21 23:33:41 -06002132
Chris Wren8fd39ec2014-02-27 17:43:26 -05002133 if (parcel.readInt() != 0) {
2134 headsUpContentView = RemoteViews.CREATOR.createFromParcel(parcel);
2135 }
2136
Dan Sandler0bf2ed82013-12-21 23:33:41 -06002137 visibility = parcel.readInt();
2138
2139 if (parcel.readInt() != 0) {
2140 publicVersion = Notification.CREATOR.createFromParcel(parcel);
2141 }
Dan Sandler26e81cf2014-05-06 10:01:27 -04002142
2143 color = parcel.readInt();
Julia Reynoldsb5e44b72016-08-16 15:00:25 -04002144
2145 if (parcel.readInt() != 0) {
2146 mChannelId = parcel.readString();
2147 }
Julia Reynolds2a128742016-11-28 14:29:25 -05002148 mTimeout = parcel.readLong();
Julia Reynolds13d898c2017-02-02 12:22:05 -05002149
2150 if (parcel.readInt() != 0) {
2151 mShortcutId = parcel.readString();
2152 }
2153
2154 mBadgeIcon = parcel.readInt();
Julia Reynolds3aedded2017-03-31 14:42:09 -04002155
2156 if (parcel.readInt() != 0) {
2157 mSettingsText = TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(parcel);
2158 }
Julia Reynoldsa79c3712017-04-21 10:29:57 -04002159
2160 mGroupAlertBehavior = parcel.readInt();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002161 }
2162
Andy Stadler110988c2010-12-03 14:29:16 -08002163 @Override
Joe Onorato18e69df2010-05-17 22:26:12 -07002164 public Notification clone() {
2165 Notification that = new Notification();
Daniel Sandler1a497d32013-04-18 14:52:45 -04002166 cloneInto(that, true);
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002167 return that;
2168 }
Joe Onorato18e69df2010-05-17 22:26:12 -07002169
Daniel Sandler1a497d32013-04-18 14:52:45 -04002170 /**
2171 * Copy all (or if heavy is false, all except Bitmaps and RemoteViews) members
2172 * of this into that.
2173 * @hide
2174 */
2175 public void cloneInto(Notification that, boolean heavy) {
Adrian Roosfb921842017-10-26 14:49:56 +02002176 that.mWhitelistToken = this.mWhitelistToken;
Joe Onorato18e69df2010-05-17 22:26:12 -07002177 that.when = this.when;
Selim Cinekb85f36fd2016-04-20 18:46:36 -07002178 that.creationTime = this.creationTime;
Dan Sandlerd63f9322015-05-06 15:18:49 -04002179 that.mSmallIcon = this.mSmallIcon;
Joe Onorato18e69df2010-05-17 22:26:12 -07002180 that.number = this.number;
2181
2182 // PendingIntents are global, so there's no reason (or way) to clone them.
2183 that.contentIntent = this.contentIntent;
2184 that.deleteIntent = this.deleteIntent;
Daniel Sandlere46cbd32010-06-17 10:35:26 -04002185 that.fullScreenIntent = this.fullScreenIntent;
Joe Onorato18e69df2010-05-17 22:26:12 -07002186
2187 if (this.tickerText != null) {
2188 that.tickerText = this.tickerText.toString();
2189 }
Daniel Sandler1a497d32013-04-18 14:52:45 -04002190 if (heavy && this.tickerView != null) {
Joe Onorato46439ce2010-11-19 13:56:21 -08002191 that.tickerView = this.tickerView.clone();
Joe Onoratoef1e7762010-09-17 18:38:38 -04002192 }
Daniel Sandler1a497d32013-04-18 14:52:45 -04002193 if (heavy && this.contentView != null) {
Joe Onorato18e69df2010-05-17 22:26:12 -07002194 that.contentView = this.contentView.clone();
2195 }
Dan Sandlerd63f9322015-05-06 15:18:49 -04002196 if (heavy && this.mLargeIcon != null) {
2197 that.mLargeIcon = this.mLargeIcon;
Joe Onorato561d3852010-11-20 18:09:34 -08002198 }
Jozef BABJAKa8b91832011-02-22 08:05:08 +01002199 that.iconLevel = this.iconLevel;
Joe Onorato18e69df2010-05-17 22:26:12 -07002200 that.sound = this.sound; // android.net.Uri is immutable
2201 that.audioStreamType = this.audioStreamType;
John Spurlockc0650f022014-07-19 13:22:39 -04002202 if (this.audioAttributes != null) {
2203 that.audioAttributes = new AudioAttributes.Builder(this.audioAttributes).build();
2204 }
Joe Onorato18e69df2010-05-17 22:26:12 -07002205
2206 final long[] vibrate = this.vibrate;
2207 if (vibrate != null) {
2208 final int N = vibrate.length;
2209 final long[] vib = that.vibrate = new long[N];
2210 System.arraycopy(vibrate, 0, vib, 0, N);
2211 }
2212
2213 that.ledARGB = this.ledARGB;
2214 that.ledOnMS = this.ledOnMS;
2215 that.ledOffMS = this.ledOffMS;
2216 that.defaults = this.defaults;
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002217
Joe Onorato18e69df2010-05-17 22:26:12 -07002218 that.flags = this.flags;
2219
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002220 that.priority = this.priority;
Joe Malin8d40d042012-11-05 11:36:40 -08002221
John Spurlockfd7f1e02014-03-18 16:41:57 -04002222 that.category = this.category;
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002223
Griff Hazen5cadc3b2014-05-20 09:55:39 -07002224 that.mGroupKey = this.mGroupKey;
2225
2226 that.mSortKey = this.mSortKey;
2227
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002228 if (this.extras != null) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04002229 try {
2230 that.extras = new Bundle(this.extras);
2231 // will unparcel
2232 that.extras.size();
2233 } catch (BadParcelableException e) {
2234 Log.e(TAG, "could not unparcel extras from notification: " + this, e);
2235 that.extras = null;
2236 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002237 }
2238
Felipe Lemedd85da62016-06-28 11:29:54 -07002239 if (!ArrayUtils.isEmpty(allPendingIntents)) {
2240 that.allPendingIntents = new ArraySet<>(allPendingIntents);
Svet Ganovddb94882016-06-23 19:55:24 -07002241 }
2242
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002243 if (this.actions != null) {
2244 that.actions = new Action[this.actions.length];
2245 for(int i=0; i<this.actions.length; i++) {
liangweikang63b03b52017-03-16 19:22:15 +08002246 if ( this.actions[i] != null) {
2247 that.actions[i] = this.actions[i].clone();
2248 }
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002249 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002250 }
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002251
Daniel Sandler1a497d32013-04-18 14:52:45 -04002252 if (heavy && this.bigContentView != null) {
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002253 that.bigContentView = this.bigContentView.clone();
2254 }
Daniel Sandler1a497d32013-04-18 14:52:45 -04002255
Chris Wren8fd39ec2014-02-27 17:43:26 -05002256 if (heavy && this.headsUpContentView != null) {
2257 that.headsUpContentView = this.headsUpContentView.clone();
2258 }
2259
Dan Sandler0bf2ed82013-12-21 23:33:41 -06002260 that.visibility = this.visibility;
2261
2262 if (this.publicVersion != null) {
2263 that.publicVersion = new Notification();
2264 this.publicVersion.cloneInto(that.publicVersion, heavy);
2265 }
2266
Dan Sandler26e81cf2014-05-06 10:01:27 -04002267 that.color = this.color;
2268
Julia Reynoldsb5e44b72016-08-16 15:00:25 -04002269 that.mChannelId = this.mChannelId;
Julia Reynolds2a128742016-11-28 14:29:25 -05002270 that.mTimeout = this.mTimeout;
Julia Reynolds3aedded2017-03-31 14:42:09 -04002271 that.mShortcutId = this.mShortcutId;
2272 that.mBadgeIcon = this.mBadgeIcon;
2273 that.mSettingsText = this.mSettingsText;
Julia Reynoldsa79c3712017-04-21 10:29:57 -04002274 that.mGroupAlertBehavior = this.mGroupAlertBehavior;
Julia Reynoldsb5e44b72016-08-16 15:00:25 -04002275
Daniel Sandler1a497d32013-04-18 14:52:45 -04002276 if (!heavy) {
2277 that.lightenPayload(); // will clean out extras
2278 }
2279 }
2280
2281 /**
2282 * Removes heavyweight parts of the Notification object for archival or for sending to
2283 * listeners when the full contents are not necessary.
2284 * @hide
2285 */
2286 public final void lightenPayload() {
2287 tickerView = null;
2288 contentView = null;
2289 bigContentView = null;
Chris Wren8fd39ec2014-02-27 17:43:26 -05002290 headsUpContentView = null;
Dan Sandlerd63f9322015-05-06 15:18:49 -04002291 mLargeIcon = null;
Dan Sandler50128532015-12-08 15:42:41 -05002292 if (extras != null && !extras.isEmpty()) {
2293 final Set<String> keyset = extras.keySet();
2294 final int N = keyset.size();
2295 final String[] keys = keyset.toArray(new String[N]);
2296 for (int i=0; i<N; i++) {
2297 final String key = keys[i];
Dmitri Plotnikov22281362017-01-30 11:16:21 -08002298 if (TvExtender.EXTRA_TV_EXTENDER.equals(key)) {
2299 continue;
2300 }
Dan Sandler50128532015-12-08 15:42:41 -05002301 final Object obj = extras.get(key);
2302 if (obj != null &&
2303 ( obj instanceof Parcelable
2304 || obj instanceof Parcelable[]
2305 || obj instanceof SparseArray
2306 || obj instanceof ArrayList)) {
2307 extras.remove(key);
2308 }
2309 }
Daniel Sandler1a497d32013-04-18 14:52:45 -04002310 }
Joe Onorato18e69df2010-05-17 22:26:12 -07002311 }
2312
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04002313 /**
2314 * Make sure this CharSequence is safe to put into a bundle, which basically
2315 * means it had better not be some custom Parcelable implementation.
2316 * @hide
2317 */
2318 public static CharSequence safeCharSequence(CharSequence cs) {
Christoph Studer535ec612014-09-03 15:47:47 +02002319 if (cs == null) return cs;
2320 if (cs.length() > MAX_CHARSEQUENCE_LENGTH) {
2321 cs = cs.subSequence(0, MAX_CHARSEQUENCE_LENGTH);
2322 }
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04002323 if (cs instanceof Parcelable) {
2324 Log.e(TAG, "warning: " + cs.getClass().getCanonicalName()
2325 + " instance is a custom Parcelable and not allowed in Notification");
2326 return cs.toString();
2327 }
Selim Cinek60a54252016-02-26 17:03:25 -08002328 return removeTextSizeSpans(cs);
2329 }
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04002330
Selim Cinek60a54252016-02-26 17:03:25 -08002331 private static CharSequence removeTextSizeSpans(CharSequence charSequence) {
2332 if (charSequence instanceof Spanned) {
2333 Spanned ss = (Spanned) charSequence;
2334 Object[] spans = ss.getSpans(0, ss.length(), Object.class);
2335 SpannableStringBuilder builder = new SpannableStringBuilder(ss.toString());
2336 for (Object span : spans) {
2337 Object resultSpan = span;
Selim Cinek89991a22016-03-07 19:51:54 -08002338 if (resultSpan instanceof CharacterStyle) {
2339 resultSpan = ((CharacterStyle) span).getUnderlying();
2340 }
2341 if (resultSpan instanceof TextAppearanceSpan) {
2342 TextAppearanceSpan originalSpan = (TextAppearanceSpan) resultSpan;
Selim Cinek60a54252016-02-26 17:03:25 -08002343 resultSpan = new TextAppearanceSpan(
2344 originalSpan.getFamily(),
2345 originalSpan.getTextStyle(),
2346 -1,
2347 originalSpan.getTextColor(),
2348 originalSpan.getLinkTextColor());
Selim Cinek89991a22016-03-07 19:51:54 -08002349 } else if (resultSpan instanceof RelativeSizeSpan
2350 || resultSpan instanceof AbsoluteSizeSpan) {
Selim Cinek60a54252016-02-26 17:03:25 -08002351 continue;
Selim Cinek89991a22016-03-07 19:51:54 -08002352 } else {
2353 resultSpan = span;
Selim Cinek60a54252016-02-26 17:03:25 -08002354 }
2355 builder.setSpan(resultSpan, ss.getSpanStart(span), ss.getSpanEnd(span),
2356 ss.getSpanFlags(span));
2357 }
2358 return builder;
2359 }
2360 return charSequence;
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04002361 }
2362
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002363 public int describeContents() {
2364 return 0;
2365 }
2366
2367 /**
Dan Sandler4e787062015-06-17 15:09:48 -04002368 * Flatten this notification into a parcel.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002369 */
Svet Ganovddb94882016-06-23 19:55:24 -07002370 public void writeToParcel(Parcel parcel, int flags) {
2371 // We need to mark all pending intents getting into the notification
2372 // system as being put there to later allow the notification ranker
2373 // to launch them and by doing so add the app to the battery saver white
2374 // list for a short period of time. The problem is that the system
2375 // cannot look into the extras as there may be parcelables there that
2376 // the platform does not know how to handle. To go around that we have
2377 // an explicit list of the pending intents in the extras bundle.
Felipe Lemedd85da62016-06-28 11:29:54 -07002378 final boolean collectPendingIntents = (allPendingIntents == null);
Svet Ganovddb94882016-06-23 19:55:24 -07002379 if (collectPendingIntents) {
2380 PendingIntent.setOnMarshaledListener(
2381 (PendingIntent intent, Parcel out, int outFlags) -> {
2382 if (parcel == out) {
Felipe Lemedd85da62016-06-28 11:29:54 -07002383 if (allPendingIntents == null) {
2384 allPendingIntents = new ArraySet<>();
Svet Ganovddb94882016-06-23 19:55:24 -07002385 }
Felipe Lemedd85da62016-06-28 11:29:54 -07002386 allPendingIntents.add(intent);
Svet Ganovddb94882016-06-23 19:55:24 -07002387 }
2388 });
2389 }
2390 try {
2391 // IMPORTANT: Add marshaling code in writeToParcelImpl as we
Julia Reynolds13d898c2017-02-02 12:22:05 -05002392 // want to intercept all pending events written to the parcel.
Svet Ganovddb94882016-06-23 19:55:24 -07002393 writeToParcelImpl(parcel, flags);
2394 // Must be written last!
Felipe Lemedd85da62016-06-28 11:29:54 -07002395 parcel.writeArraySet(allPendingIntents);
Svet Ganovddb94882016-06-23 19:55:24 -07002396 } finally {
2397 if (collectPendingIntents) {
2398 PendingIntent.setOnMarshaledListener(null);
2399 }
2400 }
2401 }
2402
2403 private void writeToParcelImpl(Parcel parcel, int flags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002404 parcel.writeInt(1);
2405
Adrian Roosfb921842017-10-26 14:49:56 +02002406 parcel.writeStrongBinder(mWhitelistToken);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002407 parcel.writeLong(when);
Selim Cinekb85f36fd2016-04-20 18:46:36 -07002408 parcel.writeLong(creationTime);
Dan Sandler4e787062015-06-17 15:09:48 -04002409 if (mSmallIcon == null && icon != 0) {
2410 // you snuck an icon in here without using the builder; let's try to keep it
2411 mSmallIcon = Icon.createWithResource("", icon);
2412 }
Dan Sandler3936e7a2015-05-19 20:59:12 -04002413 if (mSmallIcon != null) {
2414 parcel.writeInt(1);
2415 mSmallIcon.writeToParcel(parcel, 0);
2416 } else {
2417 parcel.writeInt(0);
2418 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002419 parcel.writeInt(number);
2420 if (contentIntent != null) {
2421 parcel.writeInt(1);
2422 contentIntent.writeToParcel(parcel, 0);
2423 } else {
2424 parcel.writeInt(0);
2425 }
2426 if (deleteIntent != null) {
2427 parcel.writeInt(1);
2428 deleteIntent.writeToParcel(parcel, 0);
2429 } else {
2430 parcel.writeInt(0);
2431 }
2432 if (tickerText != null) {
2433 parcel.writeInt(1);
2434 TextUtils.writeToParcel(tickerText, parcel, flags);
2435 } else {
2436 parcel.writeInt(0);
2437 }
Joe Onorato46439ce2010-11-19 13:56:21 -08002438 if (tickerView != null) {
Joe Onoratoef1e7762010-09-17 18:38:38 -04002439 parcel.writeInt(1);
Joe Onorato46439ce2010-11-19 13:56:21 -08002440 tickerView.writeToParcel(parcel, 0);
Joe Onoratoef1e7762010-09-17 18:38:38 -04002441 } else {
2442 parcel.writeInt(0);
2443 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002444 if (contentView != null) {
2445 parcel.writeInt(1);
2446 contentView.writeToParcel(parcel, 0);
2447 } else {
2448 parcel.writeInt(0);
2449 }
Selim Cinek279fa862016-06-14 10:57:25 -07002450 if (mLargeIcon == null && largeIcon != null) {
2451 // you snuck an icon in here without using the builder; let's try to keep it
2452 mLargeIcon = Icon.createWithBitmap(largeIcon);
2453 }
Dan Sandlerd63f9322015-05-06 15:18:49 -04002454 if (mLargeIcon != null) {
Joe Onorato561d3852010-11-20 18:09:34 -08002455 parcel.writeInt(1);
Dan Sandlerd63f9322015-05-06 15:18:49 -04002456 mLargeIcon.writeToParcel(parcel, 0);
Joe Onorato561d3852010-11-20 18:09:34 -08002457 } else {
2458 parcel.writeInt(0);
2459 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002460
2461 parcel.writeInt(defaults);
2462 parcel.writeInt(this.flags);
2463
2464 if (sound != null) {
2465 parcel.writeInt(1);
2466 sound.writeToParcel(parcel, 0);
2467 } else {
2468 parcel.writeInt(0);
2469 }
2470 parcel.writeInt(audioStreamType);
John Spurlockc0650f022014-07-19 13:22:39 -04002471
2472 if (audioAttributes != null) {
2473 parcel.writeInt(1);
2474 audioAttributes.writeToParcel(parcel, 0);
2475 } else {
2476 parcel.writeInt(0);
2477 }
2478
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002479 parcel.writeLongArray(vibrate);
2480 parcel.writeInt(ledARGB);
2481 parcel.writeInt(ledOnMS);
2482 parcel.writeInt(ledOffMS);
2483 parcel.writeInt(iconLevel);
Daniel Sandlere46cbd32010-06-17 10:35:26 -04002484
2485 if (fullScreenIntent != null) {
2486 parcel.writeInt(1);
2487 fullScreenIntent.writeToParcel(parcel, 0);
2488 } else {
2489 parcel.writeInt(0);
2490 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002491
2492 parcel.writeInt(priority);
Joe Malin8d40d042012-11-05 11:36:40 -08002493
John Spurlockfd7f1e02014-03-18 16:41:57 -04002494 parcel.writeString(category);
Joe Malin8d40d042012-11-05 11:36:40 -08002495
Griff Hazen5cadc3b2014-05-20 09:55:39 -07002496 parcel.writeString(mGroupKey);
2497
2498 parcel.writeString(mSortKey);
2499
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002500 parcel.writeBundle(extras); // null ok
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002501
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002502 parcel.writeTypedArray(actions, 0); // null ok
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002503
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002504 if (bigContentView != null) {
2505 parcel.writeInt(1);
2506 bigContentView.writeToParcel(parcel, 0);
2507 } else {
2508 parcel.writeInt(0);
2509 }
Dan Sandler0bf2ed82013-12-21 23:33:41 -06002510
Chris Wren8fd39ec2014-02-27 17:43:26 -05002511 if (headsUpContentView != null) {
2512 parcel.writeInt(1);
2513 headsUpContentView.writeToParcel(parcel, 0);
2514 } else {
2515 parcel.writeInt(0);
2516 }
2517
Dan Sandler0bf2ed82013-12-21 23:33:41 -06002518 parcel.writeInt(visibility);
2519
2520 if (publicVersion != null) {
2521 parcel.writeInt(1);
2522 publicVersion.writeToParcel(parcel, 0);
2523 } else {
2524 parcel.writeInt(0);
2525 }
Dan Sandler26e81cf2014-05-06 10:01:27 -04002526
2527 parcel.writeInt(color);
Julia Reynoldsb5e44b72016-08-16 15:00:25 -04002528
2529 if (mChannelId != null) {
2530 parcel.writeInt(1);
2531 parcel.writeString(mChannelId);
2532 } else {
2533 parcel.writeInt(0);
2534 }
Julia Reynolds2a128742016-11-28 14:29:25 -05002535 parcel.writeLong(mTimeout);
Julia Reynolds13d898c2017-02-02 12:22:05 -05002536
2537 if (mShortcutId != null) {
2538 parcel.writeInt(1);
2539 parcel.writeString(mShortcutId);
2540 } else {
2541 parcel.writeInt(0);
2542 }
2543
2544 parcel.writeInt(mBadgeIcon);
Julia Reynolds3aedded2017-03-31 14:42:09 -04002545
2546 if (mSettingsText != null) {
2547 parcel.writeInt(1);
2548 TextUtils.writeToParcel(mSettingsText, parcel, flags);
2549 } else {
2550 parcel.writeInt(0);
2551 }
Julia Reynoldsa79c3712017-04-21 10:29:57 -04002552
2553 parcel.writeInt(mGroupAlertBehavior);
Julia Reynoldsfc640012018-02-21 12:25:27 -05002554
2555 // mUsesStandardHeader is not written because it should be recomputed in listeners
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002556 }
2557
2558 /**
2559 * Parcelable.Creator that instantiates Notification objects
2560 */
2561 public static final Parcelable.Creator<Notification> CREATOR
2562 = new Parcelable.Creator<Notification>()
2563 {
2564 public Notification createFromParcel(Parcel parcel)
2565 {
2566 return new Notification(parcel);
2567 }
2568
2569 public Notification[] newArray(int size)
2570 {
2571 return new Notification[size];
2572 }
2573 };
2574
2575 /**
Robin Leead7e72a2017-12-04 15:45:46 +01002576 * Parcelling creates multiple copies of objects in {@code extras}. Fix them.
2577 * <p>
2578 * For backwards compatibility {@code extras} holds some references to "real" member data such
2579 * as {@link getLargeIcon()} which is mirrored by {@link #EXTRA_LARGE_ICON}. This is mostly
2580 * fine as long as the object stays in one process.
2581 * <p>
2582 * However, once the notification goes into a parcel each reference gets marshalled separately,
2583 * wasting memory. Especially with large images on Auto and TV, this is worth fixing.
2584 */
2585 private void fixDuplicateExtras() {
2586 if (extras != null) {
2587 fixDuplicateExtra(mSmallIcon, EXTRA_SMALL_ICON);
2588 fixDuplicateExtra(mLargeIcon, EXTRA_LARGE_ICON);
2589 }
2590 }
2591
2592 /**
2593 * If we find an extra that's exactly the same as one of the "real" fields but refers to a
2594 * separate object, replace it with the field's version to avoid holding duplicate copies.
2595 */
2596 private void fixDuplicateExtra(@Nullable Parcelable original, @NonNull String extraName) {
2597 if (original != null && extras.getParcelable(extraName) != null) {
2598 extras.putParcelable(extraName, original);
2599 }
2600 }
2601
2602 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002603 * Sets the {@link #contentView} field to be a view with the standard "Latest Event"
2604 * layout.
2605 *
2606 * <p>Uses the {@link #icon} and {@link #when} fields to set the icon and time fields
2607 * in the view.</p>
2608 * @param context The context for your application / activity.
2609 * @param contentTitle The title that goes in the expanded entry.
2610 * @param contentText The text that goes in the expanded entry.
2611 * @param contentIntent The intent to launch when the user clicks the expanded notification.
2612 * If this is an activity, it must include the
2613 * {@link android.content.Intent#FLAG_ACTIVITY_NEW_TASK} flag, which requires
Scott Main7aee61f2011-02-08 11:25:01 -08002614 * that you take care of task management as described in the
2615 * <a href="{@docRoot}guide/topics/fundamentals/tasks-and-back-stack.html">Tasks and Back
2616 * Stack</a> document.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002617 *
Joe Onorato46439ce2010-11-19 13:56:21 -08002618 * @deprecated Use {@link Builder} instead.
Chris Wrena05db382015-06-24 15:18:34 -04002619 * @removed
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002620 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002621 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002622 public void setLatestEventInfo(Context context,
2623 CharSequence contentTitle, CharSequence contentText, PendingIntent contentIntent) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002624 if (context.getApplicationInfo().targetSdkVersion > Build.VERSION_CODES.LOLLIPOP_MR1){
2625 Log.e(TAG, "setLatestEventInfo() is deprecated and you should feel deprecated.",
2626 new Throwable());
2627 }
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002628
Selim Cinek4ac6f602016-06-13 15:47:03 -07002629 if (context.getApplicationInfo().targetSdkVersion < Build.VERSION_CODES.N) {
2630 extras.putBoolean(EXTRA_SHOW_WHEN, true);
2631 }
2632
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002633 // ensure that any information already set directly is preserved
2634 final Notification.Builder builder = new Notification.Builder(context, this);
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002635
2636 // now apply the latestEventInfo fields
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002637 if (contentTitle != null) {
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002638 builder.setContentTitle(contentTitle);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002639 }
2640 if (contentText != null) {
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002641 builder.setContentText(contentText);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002642 }
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002643 builder.setContentIntent(contentIntent);
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002644
2645 builder.build(); // callers expect this notification to be ready to use
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002646 }
2647
Julia Reynoldsda303542015-11-23 14:00:20 -05002648 /**
2649 * @hide
2650 */
2651 public static void addFieldsFromContext(Context context, Notification notification) {
Julia Reynoldse071abd2017-03-22 10:52:11 -04002652 addFieldsFromContext(context.getApplicationInfo(), notification);
Jeff Sharkey012bc7b2016-04-11 16:30:27 -06002653 }
2654
2655 /**
2656 * @hide
2657 */
Julia Reynoldse071abd2017-03-22 10:52:11 -04002658 public static void addFieldsFromContext(ApplicationInfo ai, Notification notification) {
Jeff Sharkey012bc7b2016-04-11 16:30:27 -06002659 notification.extras.putParcelable(EXTRA_BUILDER_APPLICATION_INFO, ai);
Julia Reynoldsda303542015-11-23 14:00:20 -05002660 }
2661
Yi Jin6b514142017-10-30 14:54:12 -07002662 /**
2663 * @hide
2664 */
2665 public void writeToProto(ProtoOutputStream proto, long fieldId) {
2666 long token = proto.start(fieldId);
2667 proto.write(NotificationProto.CHANNEL_ID, getChannelId());
2668 proto.write(NotificationProto.HAS_TICKER_TEXT, this.tickerText != null);
2669 proto.write(NotificationProto.FLAGS, this.flags);
2670 proto.write(NotificationProto.COLOR, this.color);
2671 proto.write(NotificationProto.CATEGORY, this.category);
2672 proto.write(NotificationProto.GROUP_KEY, this.mGroupKey);
2673 proto.write(NotificationProto.SORT_KEY, this.mSortKey);
2674 if (this.actions != null) {
2675 proto.write(NotificationProto.ACTION_LENGTH, this.actions.length);
2676 }
2677 if (this.visibility >= VISIBILITY_SECRET && this.visibility <= VISIBILITY_PUBLIC) {
2678 proto.write(NotificationProto.VISIBILITY, this.visibility);
2679 }
2680 if (publicVersion != null) {
2681 publicVersion.writeToProto(proto, NotificationProto.PUBLIC_VERSION);
2682 }
2683 proto.end(token);
2684 }
2685
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002686 @Override
2687 public String toString() {
2688 StringBuilder sb = new StringBuilder();
Julia Reynoldsb9e712e2017-04-17 10:31:03 -04002689 sb.append("Notification(channel=");
Julia Reynoldsbad42972017-04-25 13:52:49 -04002690 sb.append(getChannelId());
Julia Reynoldsb9e712e2017-04-17 10:31:03 -04002691 sb.append(" pri=");
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002692 sb.append(priority);
2693 sb.append(" contentView=");
Joe Onoratoc9596d62011-01-12 17:03:11 -08002694 if (contentView != null) {
2695 sb.append(contentView.getPackage());
2696 sb.append("/0x");
2697 sb.append(Integer.toHexString(contentView.getLayoutId()));
2698 } else {
2699 sb.append("null");
2700 }
2701 sb.append(" vibrate=");
Daniel Sandler6738eee2012-11-16 12:03:32 -05002702 if ((this.defaults & DEFAULT_VIBRATE) != 0) {
2703 sb.append("default");
2704 } else if (this.vibrate != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002705 int N = this.vibrate.length-1;
2706 sb.append("[");
2707 for (int i=0; i<N; i++) {
2708 sb.append(this.vibrate[i]);
2709 sb.append(',');
2710 }
Simon Schoar8cf97d92009-06-10 22:08:37 +02002711 if (N != -1) {
2712 sb.append(this.vibrate[N]);
2713 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002714 sb.append("]");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002715 } else {
2716 sb.append("null");
2717 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002718 sb.append(" sound=");
Daniel Sandler6738eee2012-11-16 12:03:32 -05002719 if ((this.defaults & DEFAULT_SOUND) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002720 sb.append("default");
Daniel Sandler6738eee2012-11-16 12:03:32 -05002721 } else if (this.sound != null) {
2722 sb.append(this.sound.toString());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002723 } else {
2724 sb.append("null");
2725 }
Chris Wren365b6d32015-07-16 10:39:26 -04002726 if (this.tickerText != null) {
2727 sb.append(" tick");
2728 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002729 sb.append(" defaults=0x");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002730 sb.append(Integer.toHexString(this.defaults));
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002731 sb.append(" flags=0x");
Daniel Sandlere46cbd32010-06-17 10:35:26 -04002732 sb.append(Integer.toHexString(this.flags));
Dan Sandler26e81cf2014-05-06 10:01:27 -04002733 sb.append(String.format(" color=0x%08x", this.color));
Griff Hazen5cadc3b2014-05-20 09:55:39 -07002734 if (this.category != null) {
2735 sb.append(" category=");
2736 sb.append(this.category);
2737 }
2738 if (this.mGroupKey != null) {
2739 sb.append(" groupKey=");
2740 sb.append(this.mGroupKey);
2741 }
2742 if (this.mSortKey != null) {
2743 sb.append(" sortKey=");
2744 sb.append(this.mSortKey);
2745 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002746 if (actions != null) {
Dan Sandler1b718782014-07-18 12:43:45 -04002747 sb.append(" actions=");
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002748 sb.append(actions.length);
Dan Sandler1b718782014-07-18 12:43:45 -04002749 }
2750 sb.append(" vis=");
2751 sb.append(visibilityToString(this.visibility));
2752 if (this.publicVersion != null) {
2753 sb.append(" publicVersion=");
2754 sb.append(publicVersion.toString());
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002755 }
2756 sb.append(")");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002757 return sb.toString();
2758 }
Joe Onorato46439ce2010-11-19 13:56:21 -08002759
Dan Sandler1b718782014-07-18 12:43:45 -04002760 /**
2761 * {@hide}
2762 */
2763 public static String visibilityToString(int vis) {
2764 switch (vis) {
2765 case VISIBILITY_PRIVATE:
2766 return "PRIVATE";
2767 case VISIBILITY_PUBLIC:
2768 return "PUBLIC";
2769 case VISIBILITY_SECRET:
2770 return "SECRET";
2771 default:
2772 return "UNKNOWN(" + String.valueOf(vis) + ")";
2773 }
2774 }
2775
Joe Onoratocb109a02011-01-18 17:57:41 -08002776 /**
John Spurlock1d881a12015-03-18 19:21:54 -04002777 * {@hide}
2778 */
2779 public static String priorityToString(@Priority int pri) {
2780 switch (pri) {
2781 case PRIORITY_MIN:
2782 return "MIN";
2783 case PRIORITY_LOW:
2784 return "LOW";
2785 case PRIORITY_DEFAULT:
2786 return "DEFAULT";
2787 case PRIORITY_HIGH:
2788 return "HIGH";
2789 case PRIORITY_MAX:
2790 return "MAX";
2791 default:
2792 return "UNKNOWN(" + String.valueOf(pri) + ")";
2793 }
2794 }
2795
Julia Reynolds6ad0aec2017-07-05 08:47:03 -04002796 /**
2797 * @hide
2798 */
2799 public boolean hasCompletedProgress() {
2800 // not a progress notification; can't be complete
2801 if (!extras.containsKey(EXTRA_PROGRESS)
2802 || !extras.containsKey(EXTRA_PROGRESS_MAX)) {
2803 return false;
2804 }
2805 // many apps use max 0 for 'indeterminate'; not complete
2806 if (extras.getInt(EXTRA_PROGRESS_MAX) == 0) {
2807 return false;
2808 }
2809 return extras.getInt(EXTRA_PROGRESS) == extras.getInt(EXTRA_PROGRESS_MAX);
2810 }
2811
Jeff Sharkey000ce802017-04-29 13:13:27 -06002812 /** @removed */
2813 @Deprecated
Julia Reynolds37856052016-11-11 09:20:07 -05002814 public String getChannel() {
Julia Reynoldsb5e44b72016-08-16 15:00:25 -04002815 return mChannelId;
2816 }
2817
2818 /**
Julia Reynoldsbad42972017-04-25 13:52:49 -04002819 * Returns the id of the channel this notification posts to.
2820 */
2821 public String getChannelId() {
2822 return mChannelId;
2823 }
2824
Jeff Sharkey000ce802017-04-29 13:13:27 -06002825 /** @removed */
2826 @Deprecated
Julia Reynolds2a128742016-11-28 14:29:25 -05002827 public long getTimeout() {
2828 return mTimeout;
2829 }
2830
2831 /**
Julia Reynoldsbad42972017-04-25 13:52:49 -04002832 * Returns the duration from posting after which this notification should be canceled by the
2833 * system, if it's not canceled already.
2834 */
2835 public long getTimeoutAfter() {
2836 return mTimeout;
2837 }
2838
2839 /**
Julia Reynoldse071abd2017-03-22 10:52:11 -04002840 * Returns what icon should be shown for this notification if it is being displayed in a
2841 * Launcher that supports badging. Will be one of {@link #BADGE_ICON_NONE},
2842 * {@link #BADGE_ICON_SMALL}, or {@link #BADGE_ICON_LARGE}.
2843 */
2844 public int getBadgeIconType() {
2845 return mBadgeIcon;
2846 }
2847
2848 /**
Julia Reynolds13d898c2017-02-02 12:22:05 -05002849 * Returns the {@link ShortcutInfo#getId() id} that this notification supersedes, if any.
Julia Reynoldsbad42972017-04-25 13:52:49 -04002850 *
2851 * <p>Used by some Launchers that display notification content to hide shortcuts that duplicate
2852 * notifications.
Julia Reynolds13d898c2017-02-02 12:22:05 -05002853 */
2854 public String getShortcutId() {
2855 return mShortcutId;
2856 }
2857
Julia Reynolds3aedded2017-03-31 14:42:09 -04002858
2859 /**
2860 * Returns the settings text provided to {@link Builder#setSettingsText(CharSequence)}.
2861 */
2862 public CharSequence getSettingsText() {
2863 return mSettingsText;
2864 }
2865
Julia Reynolds13d898c2017-02-02 12:22:05 -05002866 /**
Julia Reynoldsa79c3712017-04-21 10:29:57 -04002867 * Returns which type of notifications in a group are responsible for audibly alerting the
2868 * user. See {@link #GROUP_ALERT_ALL}, {@link #GROUP_ALERT_CHILDREN},
2869 * {@link #GROUP_ALERT_SUMMARY}.
2870 */
2871 public @GroupAlertBehavior int getGroupAlertBehavior() {
2872 return mGroupAlertBehavior;
2873 }
2874
2875 /**
Dan Sandlerd63f9322015-05-06 15:18:49 -04002876 * The small icon representing this notification in the status bar and content view.
2877 *
2878 * @return the small icon representing this notification.
2879 *
2880 * @see Builder#getSmallIcon()
2881 * @see Builder#setSmallIcon(Icon)
2882 */
2883 public Icon getSmallIcon() {
2884 return mSmallIcon;
2885 }
2886
2887 /**
2888 * Used when notifying to clean up legacy small icons.
2889 * @hide
2890 */
2891 public void setSmallIcon(Icon icon) {
2892 mSmallIcon = icon;
2893 }
2894
2895 /**
2896 * The large icon shown in this notification's content view.
2897 * @see Builder#getLargeIcon()
2898 * @see Builder#setLargeIcon(Icon)
2899 */
2900 public Icon getLargeIcon() {
2901 return mLargeIcon;
2902 }
2903
2904 /**
Christoph Studer4600f9b2014-07-22 22:44:43 +02002905 * @hide
2906 */
Christoph Studerc8db24b2014-07-25 17:50:30 +02002907 public boolean isGroupSummary() {
2908 return mGroupKey != null && (flags & FLAG_GROUP_SUMMARY) != 0;
2909 }
2910
2911 /**
2912 * @hide
2913 */
2914 public boolean isGroupChild() {
2915 return mGroupKey != null && (flags & FLAG_GROUP_SUMMARY) == 0;
2916 }
2917
2918 /**
Julia Reynolds30203152017-05-26 13:36:31 -04002919 * @hide
2920 */
2921 public boolean suppressAlertingDueToGrouping() {
2922 if (isGroupSummary()
2923 && getGroupAlertBehavior() == Notification.GROUP_ALERT_CHILDREN) {
2924 return true;
2925 } else if (isGroupChild()
2926 && getGroupAlertBehavior() == Notification.GROUP_ALERT_SUMMARY) {
2927 return true;
2928 }
2929 return false;
2930 }
2931
2932 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002933 * Builder class for {@link Notification} objects.
Joe Malin8d40d042012-11-05 11:36:40 -08002934 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002935 * Provides a convenient way to set the various fields of a {@link Notification} and generate
Scott Main183bf112012-08-13 19:12:13 -07002936 * content views using the platform's notification layout template. If your app supports
2937 * versions of Android as old as API level 4, you can instead use
2938 * {@link android.support.v4.app.NotificationCompat.Builder NotificationCompat.Builder},
2939 * available in the <a href="{@docRoot}tools/extras/support-library.html">Android Support
2940 * library</a>.
Joe Malin8d40d042012-11-05 11:36:40 -08002941 *
Scott Main183bf112012-08-13 19:12:13 -07002942 * <p>Example:
Joe Malin8d40d042012-11-05 11:36:40 -08002943 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002944 * <pre class="prettyprint">
Scott Main183bf112012-08-13 19:12:13 -07002945 * Notification noti = new Notification.Builder(mContext)
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002946 * .setContentTitle(&quot;New mail from &quot; + sender.toString())
2947 * .setContentText(subject)
2948 * .setSmallIcon(R.drawable.new_mail)
2949 * .setLargeIcon(aBitmap)
Chris Wrenfbd96ba2012-05-01 12:03:58 -04002950 * .build();
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002951 * </pre>
Joe Onoratocb109a02011-01-18 17:57:41 -08002952 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002953 public static class Builder {
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05002954 /**
2955 * @hide
2956 */
2957 public static final String EXTRA_REBUILD_CONTENT_VIEW_ACTION_COUNT =
2958 "android.rebuild.contentViewActionCount";
2959 /**
2960 * @hide
2961 */
2962 public static final String EXTRA_REBUILD_BIG_CONTENT_VIEW_ACTION_COUNT
2963 = "android.rebuild.bigViewActionCount";
2964 /**
2965 * @hide
2966 */
2967 public static final String EXTRA_REBUILD_HEADS_UP_CONTENT_VIEW_ACTION_COUNT
2968 = "android.rebuild.hudViewActionCount";
2969
Daniel Sandler602ad1c2012-06-12 16:06:27 -04002970 private static final int MAX_ACTION_BUTTONS = 3;
Daniel Sandler8680bf82012-05-15 16:52:52 -04002971
Selim Cinek6743c0b2017-01-18 18:24:01 -08002972 private static final boolean USE_ONLY_TITLE_IN_LOW_PRIORITY_SUMMARY =
2973 SystemProperties.getBoolean("notifications.only_title", true);
2974
Selim Cinek389edcd2017-05-11 19:16:44 -07002975 /**
2976 * The lightness difference that has to be added to the primary text color to obtain the
2977 * secondary text color when the background is light.
2978 */
2979 private static final int LIGHTNESS_TEXT_DIFFERENCE_LIGHT = 20;
2980
2981 /**
2982 * The lightness difference that has to be added to the primary text color to obtain the
2983 * secondary text color when the background is dark.
2984 * A bit less then the above value, since it looks better on dark backgrounds.
2985 */
2986 private static final int LIGHTNESS_TEXT_DIFFERENCE_DARK = -10;
2987
Joe Onorato46439ce2010-11-19 13:56:21 -08002988 private Context mContext;
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002989 private Notification mN;
2990 private Bundle mUserExtras = new Bundle();
Chris Wrenfbd96ba2012-05-01 12:03:58 -04002991 private Style mStyle;
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002992 private ArrayList<Action> mActions = new ArrayList<Action>(MAX_ACTION_BUTTONS);
Selim Cineke7238dd2017-12-14 17:48:32 -08002993 private ArrayList<Person> mPersonList = new ArrayList<>();
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002994 private NotificationColorUtil mColorUtil;
Selim Cinek7b9605b2017-01-19 17:36:00 -08002995 private boolean mIsLegacy;
2996 private boolean mIsLegacyInitialized;
Christoph Studer7ac80e62014-08-04 16:01:57 +02002997
2998 /**
Adrian Roos4ff3b122016-02-01 12:26:13 -08002999 * Caches a contrast-enhanced version of {@link #mCachedContrastColorIsFor}.
3000 */
3001 private int mCachedContrastColor = COLOR_INVALID;
3002 private int mCachedContrastColorIsFor = COLOR_INVALID;
Adrian Roos487374f2017-01-11 15:48:14 -08003003 /**
3004 * Caches a ambient version of {@link #mCachedContrastColorIsFor}.
3005 */
3006 private int mCachedAmbientColor = COLOR_INVALID;
3007 private int mCachedAmbientColorIsFor = COLOR_INVALID;
Adrian Roos4ff3b122016-02-01 12:26:13 -08003008
3009 /**
Adrian Roos70d7aa32017-01-11 15:39:06 -08003010 * Caches an instance of StandardTemplateParams. Note that this may have been used before,
3011 * so make sure to call {@link StandardTemplateParams#reset()} before using it.
3012 */
3013 StandardTemplateParams mParams = new StandardTemplateParams();
Selim Cinek7b9605b2017-01-19 17:36:00 -08003014 private int mTextColorsAreForBackground = COLOR_INVALID;
3015 private int mPrimaryTextColor = COLOR_INVALID;
3016 private int mSecondaryTextColor = COLOR_INVALID;
3017 private int mActionBarColor = COLOR_INVALID;
Selim Cinek5fb73f82017-04-20 16:55:38 -07003018 private int mBackgroundColor = COLOR_INVALID;
3019 private int mForegroundColor = COLOR_INVALID;
Selim Cinekac5f0272017-05-02 16:05:41 -07003020 private int mBackgroundColorHint = COLOR_INVALID;
Selim Cinek5d6ef8d2017-05-18 22:16:00 -07003021 /**
3022 * A temporary location where actions are stored. If != null the view originally has action
3023 * but doesn't have any for this inflation.
3024 */
3025 private ArrayList<Action> mOriginalActions;
Selim Cineka7679b62017-05-10 16:33:25 -07003026 private boolean mRebuildStyledRemoteViews;
Adrian Roos70d7aa32017-01-11 15:39:06 -08003027
Anthony Chenad4d1582017-04-10 16:07:58 -07003028 private boolean mTintActionButtons;
3029 private boolean mInNightMode;
3030
Adrian Roos70d7aa32017-01-11 15:39:06 -08003031 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003032 * Constructs a new Builder with the defaults:
Joe Onoratocb109a02011-01-18 17:57:41 -08003033 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003034 * @param context
3035 * A {@link Context} that will be used by the Builder to construct the
3036 * RemoteViews. The Context will not be held past the lifetime of this Builder
3037 * object.
Geoffrey Pitsch5caa2762017-01-12 09:35:54 -05003038 * @param channelId
3039 * The constructed Notification will be posted on this
3040 * {@link NotificationChannel}. To use a NotificationChannel, it must first be
3041 * created using {@link NotificationManager#createNotificationChannel}.
Joe Onoratocb109a02011-01-18 17:57:41 -08003042 */
Geoffrey Pitsch5caa2762017-01-12 09:35:54 -05003043 public Builder(Context context, String channelId) {
3044 this(context, (Notification) null);
3045 mN.mChannelId = channelId;
3046 }
3047
3048 /**
3049 * @deprecated use {@link Notification.Builder#Notification.Builder(Context, String)}
3050 * instead. All posted Notifications must specify a NotificationChannel Id.
3051 */
3052 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -08003053 public Builder(Context context) {
Geoffrey Pitsch5caa2762017-01-12 09:35:54 -05003054 this(context, (Notification) null);
Joe Onorato46439ce2010-11-19 13:56:21 -08003055 }
3056
Joe Onoratocb109a02011-01-18 17:57:41 -08003057 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003058 * @hide
Christoph Studer4600f9b2014-07-22 22:44:43 +02003059 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003060 public Builder(Context context, Notification toAdopt) {
3061 mContext = context;
Anthony Chenad4d1582017-04-10 16:07:58 -07003062 Resources res = mContext.getResources();
3063 mTintActionButtons = res.getBoolean(R.bool.config_tintNotificationActionButtons);
3064
3065 if (res.getBoolean(R.bool.config_enableNightMode)) {
3066 Configuration currentConfig = res.getConfiguration();
3067 mInNightMode = (currentConfig.uiMode & Configuration.UI_MODE_NIGHT_MASK)
3068 == Configuration.UI_MODE_NIGHT_YES;
3069 }
Christoph Studer4600f9b2014-07-22 22:44:43 +02003070
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003071 if (toAdopt == null) {
3072 mN = new Notification();
Selim Cinek0ff1ce602016-04-05 18:27:16 -07003073 if (context.getApplicationInfo().targetSdkVersion < Build.VERSION_CODES.N) {
3074 mN.extras.putBoolean(EXTRA_SHOW_WHEN, true);
3075 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003076 mN.priority = PRIORITY_DEFAULT;
3077 mN.visibility = VISIBILITY_PRIVATE;
3078 } else {
3079 mN = toAdopt;
3080 if (mN.actions != null) {
3081 Collections.addAll(mActions, mN.actions);
Christoph Studer4600f9b2014-07-22 22:44:43 +02003082 }
3083
Selim Cineke7238dd2017-12-14 17:48:32 -08003084 if (mN.extras.containsKey(EXTRA_PEOPLE_LIST)) {
3085 ArrayList<Person> people = mN.extras.getParcelableArrayList(EXTRA_PEOPLE_LIST);
3086 mPersonList.addAll(people);
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003087 }
3088
Selim Cinek4ac6f602016-06-13 15:47:03 -07003089 if (mN.getSmallIcon() == null && mN.icon != 0) {
3090 setSmallIcon(mN.icon);
3091 }
3092
3093 if (mN.getLargeIcon() == null && mN.largeIcon != null) {
3094 setLargeIcon(mN.largeIcon);
3095 }
3096
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003097 String templateClass = mN.extras.getString(EXTRA_TEMPLATE);
3098 if (!TextUtils.isEmpty(templateClass)) {
3099 final Class<? extends Style> styleClass
3100 = getNotificationStyleClass(templateClass);
3101 if (styleClass == null) {
3102 Log.d(TAG, "Unknown style class: " + templateClass);
3103 } else {
3104 try {
Adrian Roosc1a80b02016-04-05 14:54:55 -07003105 final Constructor<? extends Style> ctor =
3106 styleClass.getDeclaredConstructor();
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003107 ctor.setAccessible(true);
3108 final Style style = ctor.newInstance();
3109 style.restoreFromExtras(mN.extras);
3110
3111 if (style != null) {
3112 setStyle(style);
3113 }
3114 } catch (Throwable t) {
3115 Log.e(TAG, "Could not create Style", t);
3116 }
3117 }
3118 }
3119
3120 }
3121 }
3122
3123 private NotificationColorUtil getColorUtil() {
Selim Cinek99104832017-01-25 14:47:33 -08003124 if (mColorUtil == null) {
3125 mColorUtil = NotificationColorUtil.getInstance(mContext);
Christoph Studer4600f9b2014-07-22 22:44:43 +02003126 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003127 return mColorUtil;
Christoph Studer4600f9b2014-07-22 22:44:43 +02003128 }
3129
3130 /**
Julia Reynolds13d898c2017-02-02 12:22:05 -05003131 * If this notification is duplicative of a Launcher shortcut, sets the
3132 * {@link ShortcutInfo#getId() id} of the shortcut, in case the Launcher wants to hide
3133 * the shortcut.
3134 *
Julia Reynoldsbad42972017-04-25 13:52:49 -04003135 * This field will be ignored by Launchers that don't support badging, don't show
3136 * notification content, or don't show {@link android.content.pm.ShortcutManager shortcuts}.
Julia Reynolds13d898c2017-02-02 12:22:05 -05003137 *
3138 * @param shortcutId the {@link ShortcutInfo#getId() id} of the shortcut this notification
3139 * supersedes
3140 */
3141 public Builder setShortcutId(String shortcutId) {
3142 mN.mShortcutId = shortcutId;
3143 return this;
3144 }
3145
3146 /**
Julia Reynoldse071abd2017-03-22 10:52:11 -04003147 * Sets which icon to display as a badge for this notification.
3148 *
3149 * Must be one of {@link #BADGE_ICON_NONE}, {@link #BADGE_ICON_SMALL},
3150 * {@link #BADGE_ICON_LARGE}.
3151 *
3152 * Note: This value might be ignored, for launchers that don't support badge icons.
3153 */
Julia Reynolds612beb22017-03-30 10:48:30 -04003154 public Builder setBadgeIconType(int icon) {
Julia Reynoldse071abd2017-03-22 10:52:11 -04003155 mN.mBadgeIcon = icon;
3156 return this;
3157 }
3158
3159 /**
Julia Reynoldsa79c3712017-04-21 10:29:57 -04003160 * Sets the group alert behavior for this notification. Use this method to mute this
3161 * notification if alerts for this notification's group should be handled by a different
3162 * notification. This is only applicable for notifications that belong to a
Julia Reynolds399d9bf2017-08-11 12:52:14 -04003163 * {@link #setGroup(String) group}. This must be called on all notifications you want to
3164 * mute. For example, if you want only the summary of your group to make noise, all
3165 * children in the group should have the group alert behavior {@link #GROUP_ALERT_SUMMARY}.
Julia Reynoldsa79c3712017-04-21 10:29:57 -04003166 *
3167 * <p> The default value is {@link #GROUP_ALERT_ALL}.</p>
3168 */
3169 public Builder setGroupAlertBehavior(@GroupAlertBehavior int groupAlertBehavior) {
3170 mN.mGroupAlertBehavior = groupAlertBehavior;
3171 return this;
3172 }
3173
Jeff Sharkey000ce802017-04-29 13:13:27 -06003174 /** @removed */
3175 @Deprecated
Julia Reynoldsb5e44b72016-08-16 15:00:25 -04003176 public Builder setChannel(String channelId) {
3177 mN.mChannelId = channelId;
3178 return this;
3179 }
3180
3181 /**
Julia Reynoldsbad42972017-04-25 13:52:49 -04003182 * Specifies the channel the notification should be delivered on.
3183 */
3184 public Builder setChannelId(String channelId) {
3185 mN.mChannelId = channelId;
3186 return this;
3187 }
3188
Jeff Sharkey000ce802017-04-29 13:13:27 -06003189 /** @removed */
3190 @Deprecated
Julia Reynolds50989772017-02-23 14:32:16 -05003191 public Builder setTimeout(long durationMs) {
3192 mN.mTimeout = durationMs;
Julia Reynolds2a128742016-11-28 14:29:25 -05003193 return this;
3194 }
3195
3196 /**
Julia Reynoldsbad42972017-04-25 13:52:49 -04003197 * Specifies a duration in milliseconds after which this notification should be canceled,
3198 * if it is not already canceled.
3199 */
3200 public Builder setTimeoutAfter(long durationMs) {
3201 mN.mTimeout = durationMs;
3202 return this;
3203 }
3204
3205 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003206 * Add a timestamp pertaining to the notification (usually the time the event occurred).
Selim Cinek0ff1ce602016-04-05 18:27:16 -07003207 *
3208 * For apps targeting {@link android.os.Build.VERSION_CODES#N} and above, this time is not
3209 * shown anymore by default and must be opted into by using
3210 * {@link android.app.Notification.Builder#setShowWhen(boolean)}
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003211 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003212 * @see Notification#when
Joe Onoratocb109a02011-01-18 17:57:41 -08003213 */
Joe Onorato46439ce2010-11-19 13:56:21 -08003214 public Builder setWhen(long when) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003215 mN.when = when;
Joe Onorato46439ce2010-11-19 13:56:21 -08003216 return this;
3217 }
3218
Joe Onoratocb109a02011-01-18 17:57:41 -08003219 /**
Griff Hazen50c11652014-05-16 09:46:31 -07003220 * Control whether the timestamp set with {@link #setWhen(long) setWhen} is shown
Daniel Sandler0c890492012-09-12 17:23:10 -07003221 * in the content view.
Selim Cinek0ff1ce602016-04-05 18:27:16 -07003222 * For apps targeting {@link android.os.Build.VERSION_CODES#N} and above, this defaults to
3223 * {@code false}. For earlier apps, the default is {@code true}.
Daniel Sandler0c890492012-09-12 17:23:10 -07003224 */
3225 public Builder setShowWhen(boolean show) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003226 mN.extras.putBoolean(EXTRA_SHOW_WHEN, show);
Daniel Sandler0c890492012-09-12 17:23:10 -07003227 return this;
3228 }
3229
3230 /**
Daniel Sandlerd33b8032012-05-10 11:41:48 -04003231 * Show the {@link Notification#when} field as a stopwatch.
Joe Malin8d40d042012-11-05 11:36:40 -08003232 *
3233 * Instead of presenting <code>when</code> as a timestamp, the notification will show an
Daniel Sandlerd33b8032012-05-10 11:41:48 -04003234 * automatically updating display of the minutes and seconds since <code>when</code>.
Daniel Sandlera2985ed2012-04-03 16:42:00 -04003235 *
Daniel Sandlerd33b8032012-05-10 11:41:48 -04003236 * Useful when showing an elapsed time (like an ongoing phone call).
3237 *
Selim Cinek81c23aa2016-02-25 16:23:13 -08003238 * The counter can also be set to count down to <code>when</code> when using
Adrian Roos96b7e202016-05-17 13:50:38 -07003239 * {@link #setChronometerCountDown(boolean)}.
Selim Cinek81c23aa2016-02-25 16:23:13 -08003240 *
Daniel Sandlerd33b8032012-05-10 11:41:48 -04003241 * @see android.widget.Chronometer
Daniel Sandlera2985ed2012-04-03 16:42:00 -04003242 * @see Notification#when
Adrian Roos96b7e202016-05-17 13:50:38 -07003243 * @see #setChronometerCountDown(boolean)
Daniel Sandlera2985ed2012-04-03 16:42:00 -04003244 */
3245 public Builder setUsesChronometer(boolean b) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003246 mN.extras.putBoolean(EXTRA_SHOW_CHRONOMETER, b);
Daniel Sandlera2985ed2012-04-03 16:42:00 -04003247 return this;
3248 }
3249
3250 /**
Selim Cinek81c23aa2016-02-25 16:23:13 -08003251 * Sets the Chronometer to count down instead of counting up.
3252 *
3253 * <p>This is only relevant if {@link #setUsesChronometer(boolean)} has been set to true.
3254 * If it isn't set the chronometer will count up.
3255 *
3256 * @see #setUsesChronometer(boolean)
3257 */
Adrian Roos96b7e202016-05-17 13:50:38 -07003258 public Builder setChronometerCountDown(boolean countDown) {
3259 mN.extras.putBoolean(EXTRA_CHRONOMETER_COUNT_DOWN, countDown);
Selim Cinek81c23aa2016-02-25 16:23:13 -08003260 return this;
3261 }
3262
3263 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003264 * Set the small icon resource, which will be used to represent the notification in the
3265 * status bar.
Joe Onoratocb109a02011-01-18 17:57:41 -08003266 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003267
3268 * The platform template for the expanded view will draw this icon in the left, unless a
3269 * {@link #setLargeIcon(Bitmap) large icon} has also been specified, in which case the small
3270 * icon will be moved to the right-hand side.
3271 *
3272
3273 * @param icon
3274 * A resource ID in the application's package of the drawable to use.
3275 * @see Notification#icon
Joe Onoratocb109a02011-01-18 17:57:41 -08003276 */
Tor Norbye7b9c9122013-05-30 16:48:33 -07003277 public Builder setSmallIcon(@DrawableRes int icon) {
Dan Sandlerd63f9322015-05-06 15:18:49 -04003278 return setSmallIcon(icon != 0
3279 ? Icon.createWithResource(mContext, icon)
3280 : null);
Joe Onorato46439ce2010-11-19 13:56:21 -08003281 }
3282
Joe Onoratocb109a02011-01-18 17:57:41 -08003283 /**
3284 * A variant of {@link #setSmallIcon(int) setSmallIcon(int)} that takes an additional
3285 * level parameter for when the icon is a {@link android.graphics.drawable.LevelListDrawable
3286 * LevelListDrawable}.
3287 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003288 * @param icon A resource ID in the application's package of the drawable to use.
Joe Onoratocb109a02011-01-18 17:57:41 -08003289 * @param level The level to use for the icon.
3290 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003291 * @see Notification#icon
3292 * @see Notification#iconLevel
Joe Onoratocb109a02011-01-18 17:57:41 -08003293 */
Tor Norbye7b9c9122013-05-30 16:48:33 -07003294 public Builder setSmallIcon(@DrawableRes int icon, int level) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003295 mN.iconLevel = level;
Dan Sandlerd63f9322015-05-06 15:18:49 -04003296 return setSmallIcon(icon);
3297 }
3298
3299 /**
3300 * Set the small icon, which will be used to represent the notification in the
3301 * status bar and content view (unless overriden there by a
3302 * {@link #setLargeIcon(Bitmap) large icon}).
3303 *
3304 * @param icon An Icon object to use.
3305 * @see Notification#icon
3306 */
3307 public Builder setSmallIcon(Icon icon) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003308 mN.setSmallIcon(icon);
3309 if (icon != null && icon.getType() == Icon.TYPE_RESOURCE) {
3310 mN.icon = icon.getResId();
3311 }
Joe Onorato46439ce2010-11-19 13:56:21 -08003312 return this;
3313 }
3314
Joe Onoratocb109a02011-01-18 17:57:41 -08003315 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003316 * Set the first line of text in the platform notification template.
Joe Onoratocb109a02011-01-18 17:57:41 -08003317 */
Joe Onorato46439ce2010-11-19 13:56:21 -08003318 public Builder setContentTitle(CharSequence title) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003319 mN.extras.putCharSequence(EXTRA_TITLE, safeCharSequence(title));
Joe Onorato46439ce2010-11-19 13:56:21 -08003320 return this;
3321 }
3322
Joe Onoratocb109a02011-01-18 17:57:41 -08003323 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003324 * Set the second line of text in the platform notification template.
Joe Onoratocb109a02011-01-18 17:57:41 -08003325 */
Joe Onorato46439ce2010-11-19 13:56:21 -08003326 public Builder setContentText(CharSequence text) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003327 mN.extras.putCharSequence(EXTRA_TEXT, safeCharSequence(text));
Joe Onorato46439ce2010-11-19 13:56:21 -08003328 return this;
3329 }
3330
Joe Onoratocb109a02011-01-18 17:57:41 -08003331 /**
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07003332 * This provides some additional information that is displayed in the notification. No
3333 * guarantees are given where exactly it is displayed.
3334 *
3335 * <p>This information should only be provided if it provides an essential
3336 * benefit to the understanding of the notification. The more text you provide the
3337 * less readable it becomes. For example, an email client should only provide the account
3338 * name here if more than one email account has been added.</p>
3339 *
3340 * <p>As of {@link android.os.Build.VERSION_CODES#N} this information is displayed in the
3341 * notification header area.
3342 *
3343 * On Android versions before {@link android.os.Build.VERSION_CODES#N}
3344 * this will be shown in the third line of text in the platform notification template.
3345 * You should not be using {@link #setProgress(int, int, boolean)} at the
3346 * same time on those versions; they occupy the same place.
3347 * </p>
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003348 */
3349 public Builder setSubText(CharSequence text) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003350 mN.extras.putCharSequence(EXTRA_SUB_TEXT, safeCharSequence(text));
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003351 return this;
3352 }
3353
3354 /**
Julia Reynolds3aedded2017-03-31 14:42:09 -04003355 * Provides text that will appear as a link to your application's settings.
3356 *
3357 * <p>This text does not appear within notification {@link Style templates} but may
3358 * appear when the user uses an affordance to learn more about the notification.
3359 * Additionally, this text will not appear unless you provide a valid link target by
3360 * handling {@link #INTENT_CATEGORY_NOTIFICATION_PREFERENCES}.
3361 *
3362 * <p>This text is meant to be concise description about what the user can customize
3363 * when they click on this link. The recommended maximum length is 40 characters.
3364 * @param text
3365 * @return
3366 */
3367 public Builder setSettingsText(CharSequence text) {
3368 mN.mSettingsText = safeCharSequence(text);
3369 return this;
3370 }
3371
3372 /**
Adrian Roose458aa82015-12-08 16:17:19 -08003373 * Set the remote input history.
3374 *
3375 * This should be set to the most recent inputs that have been sent
3376 * through a {@link RemoteInput} of this Notification and cleared once the it is no
3377 * longer relevant (e.g. for chat notifications once the other party has responded).
3378 *
3379 * The most recent input must be stored at the 0 index, the second most recent at the
3380 * 1 index, etc. Note that the system will limit both how far back the inputs will be shown
3381 * and how much of each individual input is shown.
3382 *
3383 * <p>Note: The reply text will only be shown on notifications that have least one action
3384 * with a {@code RemoteInput}.</p>
3385 */
3386 public Builder setRemoteInputHistory(CharSequence[] text) {
3387 if (text == null) {
3388 mN.extras.putCharSequenceArray(EXTRA_REMOTE_INPUT_HISTORY, null);
3389 } else {
3390 final int N = Math.min(MAX_REPLY_HISTORY, text.length);
3391 CharSequence[] safe = new CharSequence[N];
3392 for (int i = 0; i < N; i++) {
3393 safe[i] = safeCharSequence(text[i]);
3394 }
3395 mN.extras.putCharSequenceArray(EXTRA_REMOTE_INPUT_HISTORY, safe);
3396 }
3397 return this;
3398 }
3399
3400 /**
Julia Reynolds13d898c2017-02-02 12:22:05 -05003401 * Sets the number of items this notification represents. May be displayed as a badge count
3402 * for Launchers that support badging.
Joe Onoratocb109a02011-01-18 17:57:41 -08003403 */
Joe Onorato8595a3d2010-11-19 18:12:07 -08003404 public Builder setNumber(int number) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003405 mN.number = number;
Joe Onorato8595a3d2010-11-19 18:12:07 -08003406 return this;
3407 }
3408
Joe Onoratocb109a02011-01-18 17:57:41 -08003409 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003410 * A small piece of additional information pertaining to this notification.
3411 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003412 * The platform template will draw this on the last line of the notification, at the far
3413 * right (to the right of a smallIcon if it has been placed there).
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07003414 *
3415 * @deprecated use {@link #setSubText(CharSequence)} instead to set a text in the header.
3416 * For legacy apps targeting a version below {@link android.os.Build.VERSION_CODES#N} this
3417 * field will still show up, but the subtext will take precedence.
Joe Onoratocb109a02011-01-18 17:57:41 -08003418 */
Aurimas Liutikas514c5ef2016-05-24 15:22:55 -07003419 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -08003420 public Builder setContentInfo(CharSequence info) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003421 mN.extras.putCharSequence(EXTRA_INFO_TEXT, safeCharSequence(info));
Joe Onorato46439ce2010-11-19 13:56:21 -08003422 return this;
3423 }
3424
Joe Onoratocb109a02011-01-18 17:57:41 -08003425 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003426 * Set the progress this notification represents.
3427 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003428 * The platform template will represent this using a {@link ProgressBar}.
Jeff Sharkey1c400132011-08-05 14:50:13 -07003429 */
3430 public Builder setProgress(int max, int progress, boolean indeterminate) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003431 mN.extras.putInt(EXTRA_PROGRESS, progress);
3432 mN.extras.putInt(EXTRA_PROGRESS_MAX, max);
3433 mN.extras.putBoolean(EXTRA_PROGRESS_INDETERMINATE, indeterminate);
Jeff Sharkey1c400132011-08-05 14:50:13 -07003434 return this;
3435 }
3436
3437 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003438 * Supply a custom RemoteViews to use instead of the platform template.
3439 *
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003440 * Use {@link #setCustomContentView(RemoteViews)} instead.
Joe Onoratocb109a02011-01-18 17:57:41 -08003441 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003442 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -08003443 public Builder setContent(RemoteViews views) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003444 return setCustomContentView(views);
3445 }
3446
3447 /**
3448 * Supply custom RemoteViews to use instead of the platform template.
3449 *
3450 * This will override the layout that would otherwise be constructed by this Builder
3451 * object.
3452 */
3453 public Builder setCustomContentView(RemoteViews contentView) {
3454 mN.contentView = contentView;
3455 return this;
3456 }
3457
3458 /**
3459 * Supply custom RemoteViews to use instead of the platform template in the expanded form.
3460 *
3461 * This will override the expanded layout that would otherwise be constructed by this
3462 * Builder object.
3463 */
3464 public Builder setCustomBigContentView(RemoteViews contentView) {
3465 mN.bigContentView = contentView;
3466 return this;
3467 }
3468
3469 /**
3470 * Supply custom RemoteViews to use instead of the platform template in the heads up dialog.
3471 *
3472 * This will override the heads-up layout that would otherwise be constructed by this
3473 * Builder object.
3474 */
3475 public Builder setCustomHeadsUpContentView(RemoteViews contentView) {
3476 mN.headsUpContentView = contentView;
Joe Onorato46439ce2010-11-19 13:56:21 -08003477 return this;
3478 }
3479
Joe Onoratocb109a02011-01-18 17:57:41 -08003480 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003481 * Supply a {@link PendingIntent} to be sent when the notification is clicked.
3482 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003483 * As of {@link android.os.Build.VERSION_CODES#HONEYCOMB}, if this field is unset and you
3484 * have specified a custom RemoteViews with {@link #setContent(RemoteViews)}, you can use
3485 * {@link RemoteViews#setOnClickPendingIntent RemoteViews.setOnClickPendingIntent(int,PendingIntent)}
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003486 * to assign PendingIntents to individual views in that custom layout (i.e., to create
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003487 * clickable buttons inside the notification view).
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003488 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003489 * @see Notification#contentIntent Notification.contentIntent
Joe Onoratocb109a02011-01-18 17:57:41 -08003490 */
Joe Onorato46439ce2010-11-19 13:56:21 -08003491 public Builder setContentIntent(PendingIntent intent) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003492 mN.contentIntent = intent;
Joe Onorato46439ce2010-11-19 13:56:21 -08003493 return this;
3494 }
3495
Joe Onoratocb109a02011-01-18 17:57:41 -08003496 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003497 * Supply a {@link PendingIntent} to send when the notification is cleared explicitly by the user.
3498 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003499 * @see Notification#deleteIntent
Joe Onoratocb109a02011-01-18 17:57:41 -08003500 */
Joe Onorato46439ce2010-11-19 13:56:21 -08003501 public Builder setDeleteIntent(PendingIntent intent) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003502 mN.deleteIntent = intent;
Joe Onorato46439ce2010-11-19 13:56:21 -08003503 return this;
3504 }
3505
Joe Onoratocb109a02011-01-18 17:57:41 -08003506 /**
3507 * An intent to launch instead of posting the notification to the status bar.
3508 * Only for use with extremely high-priority notifications demanding the user's
3509 * <strong>immediate</strong> attention, such as an incoming phone call or
3510 * alarm clock that the user has explicitly set to a particular time.
3511 * If this facility is used for something else, please give the user an option
3512 * to turn it off and use a normal notification, as this can be extremely
3513 * disruptive.
3514 *
Chris Wren47c20a12014-06-18 17:27:29 -04003515 * <p>
3516 * The system UI may choose to display a heads-up notification, instead of
3517 * launching this intent, while the user is using the device.
3518 * </p>
3519 *
Joe Onoratocb109a02011-01-18 17:57:41 -08003520 * @param intent The pending intent to launch.
3521 * @param highPriority Passing true will cause this notification to be sent
3522 * even if other notifications are suppressed.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003523 *
3524 * @see Notification#fullScreenIntent
Joe Onoratocb109a02011-01-18 17:57:41 -08003525 */
Joe Onorato46439ce2010-11-19 13:56:21 -08003526 public Builder setFullScreenIntent(PendingIntent intent, boolean highPriority) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003527 mN.fullScreenIntent = intent;
Joe Onorato46439ce2010-11-19 13:56:21 -08003528 setFlag(FLAG_HIGH_PRIORITY, highPriority);
3529 return this;
3530 }
3531
Joe Onoratocb109a02011-01-18 17:57:41 -08003532 /**
Dan Sandler5fcdf6e2014-07-18 11:31:15 -04003533 * Set the "ticker" text which is sent to accessibility services.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003534 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003535 * @see Notification#tickerText
Joe Onoratocb109a02011-01-18 17:57:41 -08003536 */
Joe Onorato46439ce2010-11-19 13:56:21 -08003537 public Builder setTicker(CharSequence tickerText) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003538 mN.tickerText = safeCharSequence(tickerText);
Joe Onorato46439ce2010-11-19 13:56:21 -08003539 return this;
3540 }
3541
Joe Onoratocb109a02011-01-18 17:57:41 -08003542 /**
Dan Sandler5fcdf6e2014-07-18 11:31:15 -04003543 * Obsolete version of {@link #setTicker(CharSequence)}.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003544 *
Joe Onoratocb109a02011-01-18 17:57:41 -08003545 */
Dan Sandler5fcdf6e2014-07-18 11:31:15 -04003546 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -08003547 public Builder setTicker(CharSequence tickerText, RemoteViews views) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003548 setTicker(tickerText);
3549 // views is ignored
Joe Onorato46439ce2010-11-19 13:56:21 -08003550 return this;
3551 }
3552
Joe Onoratocb109a02011-01-18 17:57:41 -08003553 /**
Dan Sandlerd63f9322015-05-06 15:18:49 -04003554 * Add a large icon to the notification content view.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003555 *
3556 * In the platform template, this image will be shown on the left of the notification view
Dan Sandlerd63f9322015-05-06 15:18:49 -04003557 * in place of the {@link #setSmallIcon(Icon) small icon} (which will be placed in a small
3558 * badge atop the large icon).
Dan Sandler08a04c12015-05-06 15:18:49 -04003559 */
Dan Sandlerd63f9322015-05-06 15:18:49 -04003560 public Builder setLargeIcon(Bitmap b) {
3561 return setLargeIcon(b != null ? Icon.createWithBitmap(b) : null);
3562 }
3563
3564 /**
3565 * Add a large icon to the notification content view.
3566 *
3567 * In the platform template, this image will be shown on the left of the notification view
3568 * in place of the {@link #setSmallIcon(Icon) small icon} (which will be placed in a small
3569 * badge atop the large icon).
3570 */
3571 public Builder setLargeIcon(Icon icon) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003572 mN.mLargeIcon = icon;
3573 mN.extras.putParcelable(EXTRA_LARGE_ICON, icon);
Joe Onorato46439ce2010-11-19 13:56:21 -08003574 return this;
3575 }
3576
Joe Onoratocb109a02011-01-18 17:57:41 -08003577 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003578 * Set the sound to play.
3579 *
John Spurlockc0650f022014-07-19 13:22:39 -04003580 * It will be played using the {@link #AUDIO_ATTRIBUTES_DEFAULT default audio attributes}
3581 * for notifications.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003582 *
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05003583 * @deprecated use {@link NotificationChannel#setSound(Uri, AudioAttributes)} instead.
Joe Onoratocb109a02011-01-18 17:57:41 -08003584 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05003585 @Deprecated
Joe Onorato52f80cd2010-11-21 15:34:48 -08003586 public Builder setSound(Uri sound) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003587 mN.sound = sound;
3588 mN.audioAttributes = AUDIO_ATTRIBUTES_DEFAULT;
Joe Onorato52f80cd2010-11-21 15:34:48 -08003589 return this;
3590 }
3591
Joe Onoratocb109a02011-01-18 17:57:41 -08003592 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003593 * Set the sound to play, along with a specific stream on which to play it.
Joe Onoratocb109a02011-01-18 17:57:41 -08003594 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003595 * See {@link android.media.AudioManager} for the <code>STREAM_</code> constants.
3596 *
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05003597 * @deprecated use {@link NotificationChannel#setSound(Uri, AudioAttributes)}.
Joe Onoratocb109a02011-01-18 17:57:41 -08003598 */
Jean-Michel Trivi81f871e2014-08-06 16:32:38 -07003599 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -08003600 public Builder setSound(Uri sound, int streamType) {
Jean-Michel Trivi2f7511f2016-11-28 15:40:27 -08003601 PlayerBase.deprecateStreamTypeForPlayback(streamType, "Notification", "setSound()");
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003602 mN.sound = sound;
3603 mN.audioStreamType = streamType;
Joe Onorato46439ce2010-11-19 13:56:21 -08003604 return this;
3605 }
3606
Joe Onoratocb109a02011-01-18 17:57:41 -08003607 /**
John Spurlockc0650f022014-07-19 13:22:39 -04003608 * Set the sound to play, along with specific {@link AudioAttributes audio attributes} to
3609 * use during playback.
3610 *
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05003611 * @deprecated use {@link NotificationChannel#setSound(Uri, AudioAttributes)} instead.
John Spurlockc0650f022014-07-19 13:22:39 -04003612 * @see Notification#sound
3613 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05003614 @Deprecated
John Spurlockc0650f022014-07-19 13:22:39 -04003615 public Builder setSound(Uri sound, AudioAttributes audioAttributes) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003616 mN.sound = sound;
3617 mN.audioAttributes = audioAttributes;
John Spurlockc0650f022014-07-19 13:22:39 -04003618 return this;
3619 }
3620
3621 /**
Joe Onoratocb109a02011-01-18 17:57:41 -08003622 * Set the vibration pattern to use.
3623 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003624 * See {@link android.os.Vibrator#vibrate(long[], int)} for a discussion of the
3625 * <code>pattern</code> parameter.
3626 *
Chris Wren47c20a12014-06-18 17:27:29 -04003627 * <p>
3628 * A notification that vibrates is more likely to be presented as a heads-up notification.
3629 * </p>
3630 *
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05003631 * @deprecated use {@link NotificationChannel#setVibrationPattern(long[])} instead.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003632 * @see Notification#vibrate
Joe Onoratocb109a02011-01-18 17:57:41 -08003633 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05003634 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -08003635 public Builder setVibrate(long[] pattern) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003636 mN.vibrate = pattern;
Joe Onorato46439ce2010-11-19 13:56:21 -08003637 return this;
3638 }
3639
Joe Onoratocb109a02011-01-18 17:57:41 -08003640 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003641 * Set the desired color for the indicator LED on the device, as well as the
3642 * blink duty cycle (specified in milliseconds).
3643 *
3644
3645 * Not all devices will honor all (or even any) of these values.
3646 *
Julia Reynolds529e3322017-02-06 08:33:01 -05003647 * @deprecated use {@link NotificationChannel#enableLights(boolean)} instead.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003648 * @see Notification#ledARGB
3649 * @see Notification#ledOnMS
3650 * @see Notification#ledOffMS
Joe Onoratocb109a02011-01-18 17:57:41 -08003651 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05003652 @Deprecated
Tor Norbye80756e32015-03-02 09:39:27 -08003653 public Builder setLights(@ColorInt int argb, int onMs, int offMs) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003654 mN.ledARGB = argb;
3655 mN.ledOnMS = onMs;
3656 mN.ledOffMS = offMs;
Julia Reynolds10ee1fc2015-11-09 11:04:55 -05003657 if (onMs != 0 || offMs != 0) {
3658 mN.flags |= FLAG_SHOW_LIGHTS;
3659 }
Joe Onorato46439ce2010-11-19 13:56:21 -08003660 return this;
3661 }
3662
Joe Onoratocb109a02011-01-18 17:57:41 -08003663 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003664 * Set whether this is an "ongoing" notification.
Joe Onoratocb109a02011-01-18 17:57:41 -08003665 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003666
3667 * Ongoing notifications cannot be dismissed by the user, so your application or service
3668 * must take care of canceling them.
3669 *
3670
3671 * They are typically used to indicate a background task that the user is actively engaged
3672 * with (e.g., playing music) or is pending in some way and therefore occupying the device
3673 * (e.g., a file download, sync operation, active network connection).
3674 *
3675
3676 * @see Notification#FLAG_ONGOING_EVENT
3677 * @see Service#setForeground(boolean)
Joe Onoratocb109a02011-01-18 17:57:41 -08003678 */
Joe Onorato46439ce2010-11-19 13:56:21 -08003679 public Builder setOngoing(boolean ongoing) {
3680 setFlag(FLAG_ONGOING_EVENT, ongoing);
3681 return this;
3682 }
3683
Joe Onoratocb109a02011-01-18 17:57:41 -08003684 /**
Selim Cinek7b9605b2017-01-19 17:36:00 -08003685 * Set whether this notification should be colorized. When set, the color set with
3686 * {@link #setColor(int)} will be used as the background color of this notification.
3687 * <p>
Selim Cinek7b9605b2017-01-19 17:36:00 -08003688 * This should only be used for high priority ongoing tasks like navigation, an ongoing
3689 * call, or other similarly high-priority events for the user.
Selim Cinek99104832017-01-25 14:47:33 -08003690 * <p>
Selim Cinek22714f12017-04-13 16:23:53 -07003691 * For most styles, the coloring will only be applied if the notification is for a
3692 * foreground service notification.
Selim Cinek99104832017-01-25 14:47:33 -08003693 * However, for {@link MediaStyle} and {@link DecoratedMediaCustomViewStyle} notifications
Selim Cinek22714f12017-04-13 16:23:53 -07003694 * that have a media session attached there is no such requirement.
Selim Cinek7b9605b2017-01-19 17:36:00 -08003695 *
Selim Cinek7b9605b2017-01-19 17:36:00 -08003696 * @see Builder#setColor(int)
Selim Cinek99104832017-01-25 14:47:33 -08003697 * @see MediaStyle#setMediaSession(MediaSession.Token)
Selim Cinek7b9605b2017-01-19 17:36:00 -08003698 */
3699 public Builder setColorized(boolean colorize) {
3700 mN.extras.putBoolean(EXTRA_COLORIZED, colorize);
3701 return this;
3702 }
3703
3704 /**
Joe Onoratocb109a02011-01-18 17:57:41 -08003705 * Set this flag if you would only like the sound, vibrate
3706 * and ticker to be played if the notification is not already showing.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003707 *
3708 * @see Notification#FLAG_ONLY_ALERT_ONCE
Joe Onoratocb109a02011-01-18 17:57:41 -08003709 */
Joe Onorato46439ce2010-11-19 13:56:21 -08003710 public Builder setOnlyAlertOnce(boolean onlyAlertOnce) {
3711 setFlag(FLAG_ONLY_ALERT_ONCE, onlyAlertOnce);
3712 return this;
3713 }
3714
Joe Onoratocb109a02011-01-18 17:57:41 -08003715 /**
Julia Reynolds04499532016-09-13 14:04:53 -04003716 * Make this notification automatically dismissed when the user touches it.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003717 *
3718 * @see Notification#FLAG_AUTO_CANCEL
Joe Onoratocb109a02011-01-18 17:57:41 -08003719 */
Joe Onorato46439ce2010-11-19 13:56:21 -08003720 public Builder setAutoCancel(boolean autoCancel) {
Joe Onorato281d83f2011-01-04 17:13:10 -08003721 setFlag(FLAG_AUTO_CANCEL, autoCancel);
Joe Onorato46439ce2010-11-19 13:56:21 -08003722 return this;
3723 }
3724
Joe Onoratocb109a02011-01-18 17:57:41 -08003725 /**
Griff Hazendfcb0802014-02-11 12:00:00 -08003726 * Set whether or not this notification should not bridge to other devices.
3727 *
3728 * <p>Some notifications can be bridged to other devices for remote display.
3729 * This hint can be set to recommend this notification not be bridged.
3730 */
3731 public Builder setLocalOnly(boolean localOnly) {
3732 setFlag(FLAG_LOCAL_ONLY, localOnly);
3733 return this;
3734 }
3735
3736 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003737 * Set which notification properties will be inherited from system defaults.
Joe Onoratocb109a02011-01-18 17:57:41 -08003738 * <p>
3739 * The value should be one or more of the following fields combined with
3740 * bitwise-or:
3741 * {@link #DEFAULT_SOUND}, {@link #DEFAULT_VIBRATE}, {@link #DEFAULT_LIGHTS}.
3742 * <p>
3743 * For all default values, use {@link #DEFAULT_ALL}.
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05003744 *
3745 * @deprecated use {@link NotificationChannel#enableVibration(boolean)} and
Julia Reynolds529e3322017-02-06 08:33:01 -05003746 * {@link NotificationChannel#enableLights(boolean)} and
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05003747 * {@link NotificationChannel#setSound(Uri, AudioAttributes)} instead.
Joe Onoratocb109a02011-01-18 17:57:41 -08003748 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05003749 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -08003750 public Builder setDefaults(int defaults) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003751 mN.defaults = defaults;
Joe Onorato46439ce2010-11-19 13:56:21 -08003752 return this;
3753 }
3754
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003755 /**
3756 * Set the priority of this notification.
3757 *
3758 * @see Notification#priority
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05003759 * @deprecated use {@link NotificationChannel#setImportance(int)} instead.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003760 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05003761 @Deprecated
Tor Norbyed9273d62013-05-30 15:59:53 -07003762 public Builder setPriority(@Priority int pri) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003763 mN.priority = pri;
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003764 return this;
3765 }
Joe Malin8d40d042012-11-05 11:36:40 -08003766
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003767 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -04003768 * Set the notification category.
Joe Malin8d40d042012-11-05 11:36:40 -08003769 *
John Spurlockfd7f1e02014-03-18 16:41:57 -04003770 * @see Notification#category
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003771 */
John Spurlockfd7f1e02014-03-18 16:41:57 -04003772 public Builder setCategory(String category) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003773 mN.category = category;
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003774 return this;
3775 }
3776
3777 /**
Chris Wrendde75302014-03-26 17:24:15 -04003778 * Add a person that is relevant to this notification.
3779 *
Chris Wrene6c48932014-09-29 17:19:27 -04003780 * <P>
3781 * Depending on user preferences, this annotation may allow the notification to pass
Julia Reynoldse071abd2017-03-22 10:52:11 -04003782 * through interruption filters, if this notification is of category {@link #CATEGORY_CALL}
3783 * or {@link #CATEGORY_MESSAGE}. The addition of people may also cause this notification to
3784 * appear more prominently in the user interface.
Chris Wrene6c48932014-09-29 17:19:27 -04003785 * </P>
3786 *
3787 * <P>
3788 * The person should be specified by the {@code String} representation of a
3789 * {@link android.provider.ContactsContract.Contacts#CONTENT_LOOKUP_URI}.
3790 * </P>
3791 *
3792 * <P>The system will also attempt to resolve {@code mailto:} and {@code tel:} schema
3793 * URIs. The path part of these URIs must exist in the contacts database, in the
3794 * appropriate column, or the reference will be discarded as invalid. Telephone schema
3795 * URIs will be resolved by {@link android.provider.ContactsContract.PhoneLookup}.
Selim Cineke7238dd2017-12-14 17:48:32 -08003796 * It is also possible to provide a URI with the schema {@code name:} in order to uniquely
3797 * identify a person without an entry in the contacts database.
Chris Wrene6c48932014-09-29 17:19:27 -04003798 * </P>
3799 *
3800 * @param uri A URI for the person.
Chris Wrendde75302014-03-26 17:24:15 -04003801 * @see Notification#EXTRA_PEOPLE
Selim Cineke7238dd2017-12-14 17:48:32 -08003802 * @deprecated use {@link #addPerson(Person)}
Chris Wrendde75302014-03-26 17:24:15 -04003803 */
Chris Wrene6c48932014-09-29 17:19:27 -04003804 public Builder addPerson(String uri) {
Selim Cineke7238dd2017-12-14 17:48:32 -08003805 addPerson(new Person().setUri(uri));
3806 return this;
3807 }
3808
3809 /**
3810 * Add a person that is relevant to this notification.
3811 *
3812 * <P>
3813 * Depending on user preferences, this annotation may allow the notification to pass
3814 * through interruption filters, if this notification is of category {@link #CATEGORY_CALL}
3815 * or {@link #CATEGORY_MESSAGE}. The addition of people may also cause this notification to
3816 * appear more prominently in the user interface.
3817 * </P>
3818 *
3819 * <P>
3820 * A person should usually contain a uri in order to benefit from the ranking boost.
3821 * However, even if no uri is provided, it's beneficial to provide other people in the
3822 * notification, such that listeners and voice only devices can announce and handle them
3823 * properly.
3824 * </P>
3825 *
3826 * @param person the person to add.
3827 * @see Notification#EXTRA_PEOPLE_LIST
3828 */
3829 public Builder addPerson(Person person) {
3830 mPersonList.add(person);
Chris Wrendde75302014-03-26 17:24:15 -04003831 return this;
3832 }
3833
3834 /**
Griff Hazen5cadc3b2014-05-20 09:55:39 -07003835 * Set this notification to be part of a group of notifications sharing the same key.
3836 * Grouped notifications may display in a cluster or stack on devices which
3837 * support such rendering.
3838 *
3839 * <p>To make this notification the summary for its group, also call
3840 * {@link #setGroupSummary}. A sort order can be specified for group members by using
3841 * {@link #setSortKey}.
3842 * @param groupKey The group key of the group.
3843 * @return this object for method chaining
3844 */
3845 public Builder setGroup(String groupKey) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003846 mN.mGroupKey = groupKey;
Griff Hazen5cadc3b2014-05-20 09:55:39 -07003847 return this;
3848 }
3849
3850 /**
3851 * Set this notification to be the group summary for a group of notifications.
3852 * Grouped notifications may display in a cluster or stack on devices which
Julia Reynolds04499532016-09-13 14:04:53 -04003853 * support such rendering. If thereRequires a group key also be set using {@link #setGroup}.
3854 * The group summary may be suppressed if too few notifications are included in the group.
Griff Hazen5cadc3b2014-05-20 09:55:39 -07003855 * @param isGroupSummary Whether this notification should be a group summary.
3856 * @return this object for method chaining
3857 */
3858 public Builder setGroupSummary(boolean isGroupSummary) {
3859 setFlag(FLAG_GROUP_SUMMARY, isGroupSummary);
3860 return this;
3861 }
3862
3863 /**
3864 * Set a sort key that orders this notification among other notifications from the
3865 * same package. This can be useful if an external sort was already applied and an app
3866 * would like to preserve this. Notifications will be sorted lexicographically using this
3867 * value, although providing different priorities in addition to providing sort key may
3868 * cause this value to be ignored.
3869 *
3870 * <p>This sort key can also be used to order members of a notification group. See
Griff Hazen9e1379f2014-05-20 12:50:51 -07003871 * {@link #setGroup}.
Griff Hazen5cadc3b2014-05-20 09:55:39 -07003872 *
3873 * @see String#compareTo(String)
3874 */
3875 public Builder setSortKey(String sortKey) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003876 mN.mSortKey = sortKey;
Griff Hazen5cadc3b2014-05-20 09:55:39 -07003877 return this;
3878 }
3879
3880 /**
Griff Hazen720042b2014-02-24 15:46:56 -08003881 * Merge additional metadata into this notification.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003882 *
Griff Hazen720042b2014-02-24 15:46:56 -08003883 * <p>Values within the Bundle will replace existing extras values in this Builder.
3884 *
3885 * @see Notification#extras
3886 */
Griff Hazen959591e2014-05-15 22:26:18 -07003887 public Builder addExtras(Bundle extras) {
3888 if (extras != null) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003889 mUserExtras.putAll(extras);
Griff Hazen720042b2014-02-24 15:46:56 -08003890 }
3891 return this;
3892 }
3893
3894 /**
3895 * Set metadata for this notification.
3896 *
3897 * <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 -04003898 * current contents are copied into the Notification each time {@link #build()} is
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003899 * called.
3900 *
Griff Hazen720042b2014-02-24 15:46:56 -08003901 * <p>Replaces any existing extras values with those from the provided Bundle.
3902 * Use {@link #addExtras} to merge in metadata instead.
3903 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003904 * @see Notification#extras
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003905 */
Griff Hazen959591e2014-05-15 22:26:18 -07003906 public Builder setExtras(Bundle extras) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003907 if (extras != null) {
3908 mUserExtras = extras;
3909 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003910 return this;
3911 }
3912
Daniel Sandlera0a938c2012-03-15 08:42:37 -04003913 /**
Griff Hazen720042b2014-02-24 15:46:56 -08003914 * Get the current metadata Bundle used by this notification Builder.
3915 *
3916 * <p>The returned Bundle is shared with this Builder.
3917 *
3918 * <p>The current contents of this Bundle are copied into the Notification each time
3919 * {@link #build()} is called.
3920 *
3921 * @see Notification#extras
3922 */
3923 public Bundle getExtras() {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003924 return mUserExtras;
3925 }
3926
3927 private Bundle getAllExtras() {
3928 final Bundle saveExtras = (Bundle) mUserExtras.clone();
3929 saveExtras.putAll(mN.extras);
3930 return saveExtras;
Griff Hazen720042b2014-02-24 15:46:56 -08003931 }
3932
3933 /**
Daniel Sandlera0a938c2012-03-15 08:42:37 -04003934 * Add an action to this notification. Actions are typically displayed by
3935 * the system as a button adjacent to the notification content.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04003936 * <p>
3937 * Every action must have an icon (32dp square and matching the
3938 * <a href="{@docRoot}design/style/iconography.html#action-bar">Holo
3939 * Dark action bar</a> visual style), a textual label, and a {@link PendingIntent}.
3940 * <p>
3941 * A notification in its expanded form can display up to 3 actions, from left to right in
3942 * the order they were added. Actions will not be displayed when the notification is
3943 * collapsed, however, so be sure that any essential functions may be accessed by the user
3944 * in some other way (for example, in the Activity pointed to by {@link #contentIntent}).
Daniel Sandlera0a938c2012-03-15 08:42:37 -04003945 *
3946 * @param icon Resource ID of a drawable that represents the action.
3947 * @param title Text describing the action.
3948 * @param intent PendingIntent to be fired when the action is invoked.
Dan Sandler86647982015-05-13 23:41:13 -04003949 *
3950 * @deprecated Use {@link #addAction(Action)} instead.
Daniel Sandlera0a938c2012-03-15 08:42:37 -04003951 */
Dan Sandler86647982015-05-13 23:41:13 -04003952 @Deprecated
Daniel Sandlera0a938c2012-03-15 08:42:37 -04003953 public Builder addAction(int icon, CharSequence title, PendingIntent intent) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04003954 mActions.add(new Action(icon, safeCharSequence(title), intent));
Daniel Sandlera0a938c2012-03-15 08:42:37 -04003955 return this;
3956 }
3957
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003958 /**
Griff Hazen959591e2014-05-15 22:26:18 -07003959 * Add an action to this notification. Actions are typically displayed by
3960 * the system as a button adjacent to the notification content.
3961 * <p>
3962 * Every action must have an icon (32dp square and matching the
3963 * <a href="{@docRoot}design/style/iconography.html#action-bar">Holo
3964 * Dark action bar</a> visual style), a textual label, and a {@link PendingIntent}.
3965 * <p>
3966 * A notification in its expanded form can display up to 3 actions, from left to right in
3967 * the order they were added. Actions will not be displayed when the notification is
3968 * collapsed, however, so be sure that any essential functions may be accessed by the user
3969 * in some other way (for example, in the Activity pointed to by {@link #contentIntent}).
3970 *
3971 * @param action The action to add.
3972 */
3973 public Builder addAction(Action action) {
liangweikang63b03b52017-03-16 19:22:15 +08003974 if (action != null) {
3975 mActions.add(action);
3976 }
Griff Hazen959591e2014-05-15 22:26:18 -07003977 return this;
3978 }
3979
3980 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003981 * Alter the complete list of actions attached to this notification.
3982 * @see #addAction(Action).
3983 *
3984 * @param actions
3985 * @return
3986 */
3987 public Builder setActions(Action... actions) {
3988 mActions.clear();
3989 for (int i = 0; i < actions.length; i++) {
liangweikang63b03b52017-03-16 19:22:15 +08003990 if (actions[i] != null) {
3991 mActions.add(actions[i]);
3992 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003993 }
3994 return this;
3995 }
3996
3997 /**
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003998 * Add a rich notification style to be applied at build time.
3999 *
4000 * @param style Object responsible for modifying the notification style.
4001 */
4002 public Builder setStyle(Style style) {
4003 if (mStyle != style) {
4004 mStyle = style;
Daniel Sandlerc08dea22012-06-28 08:35:24 -07004005 if (mStyle != null) {
4006 mStyle.setBuilder(this);
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004007 mN.extras.putString(EXTRA_TEMPLATE, style.getClass().getName());
4008 } else {
4009 mN.extras.remove(EXTRA_TEMPLATE);
Daniel Sandlerc08dea22012-06-28 08:35:24 -07004010 }
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004011 }
4012 return this;
4013 }
4014
Dan Sandler0bf2ed82013-12-21 23:33:41 -06004015 /**
4016 * Specify the value of {@link #visibility}.
Griff Hazenb720abe2014-05-20 13:15:30 -07004017 *
Dan Sandler0bf2ed82013-12-21 23:33:41 -06004018 * @return The same Builder.
4019 */
Jeff Sharkey30e06bb2017-04-24 11:18:03 -06004020 public Builder setVisibility(@Visibility int visibility) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004021 mN.visibility = visibility;
Dan Sandler0bf2ed82013-12-21 23:33:41 -06004022 return this;
4023 }
4024
4025 /**
4026 * Supply a replacement Notification whose contents should be shown in insecure contexts
4027 * (i.e. atop the secure lockscreen). See {@link #visibility} and {@link #VISIBILITY_PUBLIC}.
4028 * @param n A replacement notification, presumably with some or all info redacted.
4029 * @return The same Builder.
4030 */
4031 public Builder setPublicVersion(Notification n) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004032 if (n != null) {
4033 mN.publicVersion = new Notification();
4034 n.cloneInto(mN.publicVersion, /*heavy=*/ true);
4035 } else {
4036 mN.publicVersion = null;
4037 }
Dan Sandler0bf2ed82013-12-21 23:33:41 -06004038 return this;
4039 }
4040
Griff Hazenb720abe2014-05-20 13:15:30 -07004041 /**
Griff Hazen5cadc3b2014-05-20 09:55:39 -07004042 * Apply an extender to this notification builder. Extenders may be used to add
4043 * metadata or change options on this builder.
4044 */
Griff Hazen61a9e862014-05-22 16:05:19 -07004045 public Builder extend(Extender extender) {
4046 extender.extend(this);
Griff Hazen5cadc3b2014-05-20 09:55:39 -07004047 return this;
4048 }
4049
Dan Sandler4e787062015-06-17 15:09:48 -04004050 /**
4051 * @hide
4052 */
Julia Reynoldse46bb372016-03-17 11:05:58 -04004053 public Builder setFlag(int mask, boolean value) {
Joe Onorato46439ce2010-11-19 13:56:21 -08004054 if (value) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004055 mN.flags |= mask;
Joe Onorato46439ce2010-11-19 13:56:21 -08004056 } else {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004057 mN.flags &= ~mask;
Joe Onorato46439ce2010-11-19 13:56:21 -08004058 }
Julia Reynoldse46bb372016-03-17 11:05:58 -04004059 return this;
Joe Onorato46439ce2010-11-19 13:56:21 -08004060 }
4061
Dan Sandler26e81cf2014-05-06 10:01:27 -04004062 /**
4063 * Sets {@link Notification#color}.
4064 *
4065 * @param argb The accent color to use
4066 *
4067 * @return The same Builder.
4068 */
Tor Norbye80756e32015-03-02 09:39:27 -08004069 public Builder setColor(@ColorInt int argb) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004070 mN.color = argb;
Julia Reynolds10ee1fc2015-11-09 11:04:55 -05004071 sanitizeColor();
Dan Sandler26e81cf2014-05-06 10:01:27 -04004072 return this;
4073 }
4074
Jorim Jaggid05aa3e2014-08-28 17:52:27 +02004075 private Drawable getProfileBadgeDrawable() {
Chris Wren66619a22016-05-12 16:42:37 -04004076 if (mContext.getUserId() == UserHandle.USER_SYSTEM) {
4077 // This user can never be a badged profile,
4078 // and also includes USER_ALL system notifications.
4079 return null;
4080 }
Christoph Studer7ac80e62014-08-04 16:01:57 +02004081 // Note: This assumes that the current user can read the profile badge of the
4082 // originating user.
Selim Cineke6ff9462016-01-15 15:07:06 -08004083 return mContext.getPackageManager().getUserBadgeForDensityNoBackground(
Julia Reynoldsda303542015-11-23 14:00:20 -05004084 new UserHandle(mContext.getUserId()), 0);
Jorim Jaggid05aa3e2014-08-28 17:52:27 +02004085 }
4086
4087 private Bitmap getProfileBadge() {
4088 Drawable badge = getProfileBadgeDrawable();
Kenny Guy8a0101b2014-05-08 23:34:12 +01004089 if (badge == null) {
4090 return null;
4091 }
Jorim Jaggid05aa3e2014-08-28 17:52:27 +02004092 final int size = mContext.getResources().getDimensionPixelSize(
4093 R.dimen.notification_badge_size);
4094 Bitmap bitmap = Bitmap.createBitmap(size, size, Bitmap.Config.ARGB_8888);
Kenny Guy8a0101b2014-05-08 23:34:12 +01004095 Canvas canvas = new Canvas(bitmap);
Jorim Jaggid05aa3e2014-08-28 17:52:27 +02004096 badge.setBounds(0, 0, size, size);
Kenny Guy8a0101b2014-05-08 23:34:12 +01004097 badge.draw(canvas);
4098 return bitmap;
4099 }
4100
Selim Cinekc848c3a2016-01-13 15:27:30 -08004101 private void bindProfileBadge(RemoteViews contentView) {
Kenny Guy98193ea2014-07-24 19:54:37 +01004102 Bitmap profileBadge = getProfileBadge();
4103
Kenny Guy98193ea2014-07-24 19:54:37 +01004104 if (profileBadge != null) {
Selim Cinekc848c3a2016-01-13 15:27:30 -08004105 contentView.setImageViewBitmap(R.id.profile_badge, profileBadge);
4106 contentView.setViewVisibility(R.id.profile_badge, View.VISIBLE);
Selim Cinek7b9605b2017-01-19 17:36:00 -08004107 if (isColorized()) {
Sunny Goyal5b153922017-09-21 21:00:36 -07004108 contentView.setDrawableTint(R.id.profile_badge, false,
4109 getPrimaryTextColor(), PorterDuff.Mode.SRC_ATOP);
Selim Cinek7b9605b2017-01-19 17:36:00 -08004110 }
Kenny Guy98193ea2014-07-24 19:54:37 +01004111 }
Kenny Guy98193ea2014-07-24 19:54:37 +01004112 }
4113
Julia Reynoldsfc640012018-02-21 12:25:27 -05004114 /**
4115 * @hide
4116 */
4117 public boolean usesStandardHeader() {
4118 if (mN.mUsesStandardHeader) {
4119 return true;
4120 }
4121 if (mContext.getApplicationInfo().targetSdkVersion >= Build.VERSION_CODES.N) {
4122 if (mN.contentView == null && mN.bigContentView == null) {
4123 return true;
4124 }
4125 }
4126 boolean contentViewUsesHeader = mN.contentView == null
4127 || STANDARD_LAYOUTS.contains(mN.contentView.getLayoutId());
4128 boolean bigContentViewUsesHeader = mN.bigContentView == null
4129 || STANDARD_LAYOUTS.contains(mN.bigContentView.getLayoutId());
4130 return contentViewUsesHeader && bigContentViewUsesHeader;
4131 }
4132
Christoph Studerfe718432014-09-01 18:21:18 +02004133 private void resetStandardTemplate(RemoteViews contentView) {
Selim Cinekeaa29ca2015-11-23 13:51:13 -08004134 resetNotificationHeader(contentView);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004135 resetContentMargins(contentView);
Christoph Studerfe718432014-09-01 18:21:18 +02004136 contentView.setViewVisibility(R.id.right_icon, View.GONE);
Selim Cinek860b6da2015-12-16 19:02:19 -08004137 contentView.setViewVisibility(R.id.title, View.GONE);
Christoph Studerfe718432014-09-01 18:21:18 +02004138 contentView.setTextViewText(R.id.title, null);
Selim Cinek41598732016-01-11 16:58:37 -08004139 contentView.setViewVisibility(R.id.text, View.GONE);
Christoph Studerfe718432014-09-01 18:21:18 +02004140 contentView.setTextViewText(R.id.text, null);
Selim Cinek29603462015-11-17 19:04:39 -08004141 contentView.setViewVisibility(R.id.text_line_1, View.GONE);
Selim Cinek41598732016-01-11 16:58:37 -08004142 contentView.setTextViewText(R.id.text_line_1, null);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004143 }
4144
Selim Cinekeaa29ca2015-11-23 13:51:13 -08004145 /**
4146 * Resets the notification header to its original state
4147 */
4148 private void resetNotificationHeader(RemoteViews contentView) {
Adrian Roosc4337a32016-08-02 18:30:34 -07004149 // Small icon doesn't need to be reset, as it's always set. Resetting would prevent
4150 // re-using the drawable when the notification is updated.
Selim Cinek7b836392015-12-04 20:02:59 -08004151 contentView.setBoolean(R.id.notification_header, "setExpanded", false);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004152 contentView.setTextViewText(R.id.app_name_text, null);
Christoph Studerca1db712014-09-10 17:31:33 +02004153 contentView.setViewVisibility(R.id.chronometer, View.GONE);
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07004154 contentView.setViewVisibility(R.id.header_text, View.GONE);
Adrian Roos9dfb78f2016-06-30 15:43:44 -07004155 contentView.setTextViewText(R.id.header_text, null);
Selim Cinekafeed292017-12-12 17:32:44 -08004156 contentView.setViewVisibility(R.id.header_text_secondary, View.GONE);
4157 contentView.setTextViewText(R.id.header_text_secondary, null);
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07004158 contentView.setViewVisibility(R.id.header_text_divider, View.GONE);
Selim Cinekafeed292017-12-12 17:32:44 -08004159 contentView.setViewVisibility(R.id.header_text_secondary_divider, View.GONE);
Selim Cinek29603462015-11-17 19:04:39 -08004160 contentView.setViewVisibility(R.id.time_divider, View.GONE);
Selim Cinekb85f36fd2016-04-20 18:46:36 -07004161 contentView.setViewVisibility(R.id.time, View.GONE);
Selim Cinekc848c3a2016-01-13 15:27:30 -08004162 contentView.setImageViewIcon(R.id.profile_badge, null);
4163 contentView.setViewVisibility(R.id.profile_badge, View.GONE);
Julia Reynoldsfc640012018-02-21 12:25:27 -05004164 mN.mUsesStandardHeader = false;
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004165 }
4166
4167 private void resetContentMargins(RemoteViews contentView) {
Adrian Roos2d5dbba2016-06-08 17:11:53 -07004168 contentView.setViewLayoutMarginEndDimen(R.id.line1, 0);
4169 contentView.setViewLayoutMarginEndDimen(R.id.text, 0);
Christoph Studerfe718432014-09-01 18:21:18 +02004170 }
4171
Jorim Jaggi445d3c02014-08-19 22:33:42 +02004172 private RemoteViews applyStandardTemplate(int resId) {
Adrian Roos70d7aa32017-01-11 15:39:06 -08004173 return applyStandardTemplate(resId, mParams.reset().fillTextsFrom(this));
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02004174 }
4175
4176 /**
4177 * @param hasProgress whether the progress bar should be shown and set
4178 */
4179 private RemoteViews applyStandardTemplate(int resId, boolean hasProgress) {
Adrian Roos70d7aa32017-01-11 15:39:06 -08004180 return applyStandardTemplate(resId, mParams.reset().hasProgress(hasProgress)
4181 .fillTextsFrom(this));
Adrian Roosc1a80b02016-04-05 14:54:55 -07004182 }
4183
Adrian Roos70d7aa32017-01-11 15:39:06 -08004184 private RemoteViews applyStandardTemplate(int resId, StandardTemplateParams p) {
Kenny Guy77320062014-08-27 21:37:15 +01004185 RemoteViews contentView = new BuilderRemoteViews(mContext.getApplicationInfo(), resId);
Dan Sandler539aad42014-08-04 00:43:39 -04004186
Christoph Studerfe718432014-09-01 18:21:18 +02004187 resetStandardTemplate(contentView);
4188
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004189 final Bundle ex = mN.extras;
Selim Cinek7b9605b2017-01-19 17:36:00 -08004190 updateBackgroundColor(contentView);
Selim Cinekafeed292017-12-12 17:32:44 -08004191 bindNotificationHeader(contentView, p.ambient, p.headerTextSecondary);
Lucas Dupin90158d02018-01-23 16:36:12 -08004192 bindLargeIcon(contentView, p.hideLargeIcon || p.ambient, p.alwaysShowReply);
Adrian Roos70d7aa32017-01-11 15:39:06 -08004193 boolean showProgress = handleProgressBar(p.hasProgress, contentView, ex);
4194 if (p.title != null) {
Selim Cinek860b6da2015-12-16 19:02:19 -08004195 contentView.setViewVisibility(R.id.title, View.VISIBLE);
Selim Cinek48f66b72017-08-18 16:17:51 -07004196 contentView.setTextViewText(R.id.title, processTextSpans(p.title));
Adrian Roos72171622017-01-27 10:32:06 -08004197 if (!p.ambient) {
4198 setTextViewColorPrimary(contentView, R.id.title);
4199 }
Selim Cinek954cc232016-05-20 13:29:23 -07004200 contentView.setViewLayoutWidth(R.id.title, showProgress
4201 ? ViewGroup.LayoutParams.WRAP_CONTENT
4202 : ViewGroup.LayoutParams.MATCH_PARENT);
Joe Onorato561d3852010-11-20 18:09:34 -08004203 }
Adrian Roos70d7aa32017-01-11 15:39:06 -08004204 if (p.text != null) {
Selim Cinek41598732016-01-11 16:58:37 -08004205 int textId = showProgress ? com.android.internal.R.id.text_line_1
4206 : com.android.internal.R.id.text;
Selim Cinek48f66b72017-08-18 16:17:51 -07004207 contentView.setTextViewText(textId, processTextSpans(p.text));
Adrian Roos72171622017-01-27 10:32:06 -08004208 if (!p.ambient) {
4209 setTextViewColorSecondary(contentView, textId);
4210 }
Selim Cinek41598732016-01-11 16:58:37 -08004211 contentView.setViewVisibility(textId, View.VISIBLE);
Joe Onorato561d3852010-11-20 18:09:34 -08004212 }
Selim Cinekc848c3a2016-01-13 15:27:30 -08004213
Selim Cinek279fa862016-06-14 10:57:25 -07004214 setContentMinHeight(contentView, showProgress || mN.hasLargeIcon());
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004215
Selim Cinek29603462015-11-17 19:04:39 -08004216 return contentView;
4217 }
4218
Selim Cinek48f66b72017-08-18 16:17:51 -07004219 private CharSequence processTextSpans(CharSequence text) {
4220 if (hasForegroundColor()) {
Selim Cinek87c31532017-08-18 18:53:44 -07004221 return NotificationColorUtil.clearColorSpans(text);
Selim Cinek48f66b72017-08-18 16:17:51 -07004222 }
4223 return text;
4224 }
4225
Selim Cinek7b9605b2017-01-19 17:36:00 -08004226 private void setTextViewColorPrimary(RemoteViews contentView, int id) {
4227 ensureColors();
4228 contentView.setTextColor(id, mPrimaryTextColor);
4229 }
4230
Selim Cinek48f66b72017-08-18 16:17:51 -07004231 private boolean hasForegroundColor() {
4232 return mForegroundColor != COLOR_INVALID;
4233 }
4234
Selim Cinek389edcd2017-05-11 19:16:44 -07004235 /**
4236 * @return the primary text color
4237 * @hide
4238 */
4239 @VisibleForTesting
4240 public int getPrimaryTextColor() {
Selim Cinek7b9605b2017-01-19 17:36:00 -08004241 ensureColors();
4242 return mPrimaryTextColor;
4243 }
4244
Selim Cinek389edcd2017-05-11 19:16:44 -07004245 /**
4246 * @return the secondary text color
4247 * @hide
4248 */
4249 @VisibleForTesting
4250 public int getSecondaryTextColor() {
4251 ensureColors();
4252 return mSecondaryTextColor;
4253 }
4254
Selim Cinek7b9605b2017-01-19 17:36:00 -08004255 private int getActionBarColor() {
4256 ensureColors();
4257 return mActionBarColor;
4258 }
4259
Selim Cinek622c64a2017-04-17 17:10:05 -07004260 private int getActionBarColorDeEmphasized() {
4261 int backgroundColor = getBackgroundColor();
4262 return NotificationColorUtil.getShiftedColor(backgroundColor, 12);
4263 }
4264
Selim Cinek7b9605b2017-01-19 17:36:00 -08004265 private void setTextViewColorSecondary(RemoteViews contentView, int id) {
4266 ensureColors();
4267 contentView.setTextColor(id, mSecondaryTextColor);
4268 }
4269
4270 private void ensureColors() {
4271 int backgroundColor = getBackgroundColor();
4272 if (mPrimaryTextColor == COLOR_INVALID
4273 || mSecondaryTextColor == COLOR_INVALID
4274 || mActionBarColor == COLOR_INVALID
4275 || mTextColorsAreForBackground != backgroundColor) {
4276 mTextColorsAreForBackground = backgroundColor;
Selim Cinek48f66b72017-08-18 16:17:51 -07004277 if (!hasForegroundColor() || !isColorized()) {
Selim Cinek5fb73f82017-04-20 16:55:38 -07004278 mPrimaryTextColor = NotificationColorUtil.resolvePrimaryColor(mContext,
4279 backgroundColor);
4280 mSecondaryTextColor = NotificationColorUtil.resolveSecondaryColor(mContext,
4281 backgroundColor);
Selim Cinekac5f0272017-05-02 16:05:41 -07004282 if (backgroundColor != COLOR_DEFAULT
4283 && (mBackgroundColorHint != COLOR_INVALID || isColorized())) {
4284 mPrimaryTextColor = NotificationColorUtil.findAlphaToMeetContrast(
4285 mPrimaryTextColor, backgroundColor, 4.5);
4286 mSecondaryTextColor = NotificationColorUtil.findAlphaToMeetContrast(
4287 mSecondaryTextColor, backgroundColor, 4.5);
4288 }
Selim Cinek5fb73f82017-04-20 16:55:38 -07004289 } else {
4290 double backLum = NotificationColorUtil.calculateLuminance(backgroundColor);
4291 double textLum = NotificationColorUtil.calculateLuminance(mForegroundColor);
4292 double contrast = NotificationColorUtil.calculateContrast(mForegroundColor,
4293 backgroundColor);
Selim Cinek389edcd2017-05-11 19:16:44 -07004294 // We only respect the given colors if worst case Black or White still has
4295 // contrast
4296 boolean backgroundLight = backLum > textLum
4297 && satisfiesTextContrast(backgroundColor, Color.BLACK)
4298 || backLum <= textLum
4299 && !satisfiesTextContrast(backgroundColor, Color.WHITE);
Selim Cinek5fb73f82017-04-20 16:55:38 -07004300 if (contrast < 4.5f) {
Selim Cinek389edcd2017-05-11 19:16:44 -07004301 if (backgroundLight) {
Selim Cinek5fb73f82017-04-20 16:55:38 -07004302 mSecondaryTextColor = NotificationColorUtil.findContrastColor(
4303 mForegroundColor,
4304 backgroundColor,
4305 true /* findFG */,
4306 4.5f);
4307 mPrimaryTextColor = NotificationColorUtil.changeColorLightness(
Selim Cinek389edcd2017-05-11 19:16:44 -07004308 mSecondaryTextColor, -LIGHTNESS_TEXT_DIFFERENCE_LIGHT);
Selim Cinek5fb73f82017-04-20 16:55:38 -07004309 } else {
4310 mSecondaryTextColor =
4311 NotificationColorUtil.findContrastColorAgainstDark(
4312 mForegroundColor,
4313 backgroundColor,
4314 true /* findFG */,
4315 4.5f);
4316 mPrimaryTextColor = NotificationColorUtil.changeColorLightness(
Selim Cinek389edcd2017-05-11 19:16:44 -07004317 mSecondaryTextColor, -LIGHTNESS_TEXT_DIFFERENCE_DARK);
Selim Cinek5fb73f82017-04-20 16:55:38 -07004318 }
4319 } else {
4320 mPrimaryTextColor = mForegroundColor;
4321 mSecondaryTextColor = NotificationColorUtil.changeColorLightness(
Selim Cinek389edcd2017-05-11 19:16:44 -07004322 mPrimaryTextColor, backgroundLight ? LIGHTNESS_TEXT_DIFFERENCE_LIGHT
4323 : LIGHTNESS_TEXT_DIFFERENCE_DARK);
Selim Cinek5fb73f82017-04-20 16:55:38 -07004324 if (NotificationColorUtil.calculateContrast(mSecondaryTextColor,
4325 backgroundColor) < 4.5f) {
4326 // oh well the secondary is not good enough
Selim Cinek389edcd2017-05-11 19:16:44 -07004327 if (backgroundLight) {
Selim Cinek5fb73f82017-04-20 16:55:38 -07004328 mSecondaryTextColor = NotificationColorUtil.findContrastColor(
4329 mSecondaryTextColor,
4330 backgroundColor,
4331 true /* findFG */,
4332 4.5f);
4333 } else {
4334 mSecondaryTextColor
4335 = NotificationColorUtil.findContrastColorAgainstDark(
4336 mSecondaryTextColor,
4337 backgroundColor,
4338 true /* findFG */,
4339 4.5f);
4340 }
4341 mPrimaryTextColor = NotificationColorUtil.changeColorLightness(
Selim Cinek389edcd2017-05-11 19:16:44 -07004342 mSecondaryTextColor, backgroundLight
4343 ? -LIGHTNESS_TEXT_DIFFERENCE_LIGHT
4344 : -LIGHTNESS_TEXT_DIFFERENCE_DARK);
Selim Cinek5fb73f82017-04-20 16:55:38 -07004345 }
4346 }
4347 }
Selim Cinek875ba9b2017-02-13 16:20:17 -08004348 mActionBarColor = NotificationColorUtil.resolveActionBarColor(mContext,
4349 backgroundColor);
Selim Cinek7b9605b2017-01-19 17:36:00 -08004350 }
4351 }
4352
4353 private void updateBackgroundColor(RemoteViews contentView) {
4354 if (isColorized()) {
4355 contentView.setInt(R.id.status_bar_latest_event_content, "setBackgroundColor",
4356 getBackgroundColor());
4357 } else {
4358 // Clear it!
4359 contentView.setInt(R.id.status_bar_latest_event_content, "setBackgroundResource",
4360 0);
4361 }
4362 }
4363
Selim Cinek860b6da2015-12-16 19:02:19 -08004364 /**
4365 * @param remoteView the remote view to update the minheight in
4366 * @param hasMinHeight does it have a mimHeight
4367 * @hide
4368 */
4369 void setContentMinHeight(RemoteViews remoteView, boolean hasMinHeight) {
4370 int minHeight = 0;
4371 if (hasMinHeight) {
4372 // we need to set the minHeight of the notification
4373 minHeight = mContext.getResources().getDimensionPixelSize(
4374 com.android.internal.R.dimen.notification_min_content_height);
4375 }
4376 remoteView.setInt(R.id.notification_main_column, "setMinimumHeight", minHeight);
4377 }
4378
Selim Cinek29603462015-11-17 19:04:39 -08004379 private boolean handleProgressBar(boolean hasProgress, RemoteViews contentView, Bundle ex) {
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004380 final int max = ex.getInt(EXTRA_PROGRESS_MAX, 0);
4381 final int progress = ex.getInt(EXTRA_PROGRESS, 0);
4382 final boolean ind = ex.getBoolean(EXTRA_PROGRESS_INDETERMINATE);
4383 if (hasProgress && (max != 0 || ind)) {
Selim Cinek29603462015-11-17 19:04:39 -08004384 contentView.setViewVisibility(com.android.internal.R.id.progress, View.VISIBLE);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004385 contentView.setProgressBar(
Selim Cinek29603462015-11-17 19:04:39 -08004386 R.id.progress, max, progress, ind);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004387 contentView.setProgressBackgroundTintList(
4388 R.id.progress, ColorStateList.valueOf(mContext.getColor(
4389 R.color.notification_progress_background_color)));
Selim Cinek29603462015-11-17 19:04:39 -08004390 if (mN.color != COLOR_DEFAULT) {
Adrian Roos4ff3b122016-02-01 12:26:13 -08004391 ColorStateList colorStateList = ColorStateList.valueOf(resolveContrastColor());
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004392 contentView.setProgressTintList(R.id.progress, colorStateList);
4393 contentView.setProgressIndeterminateTintList(R.id.progress, colorStateList);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004394 }
Selim Cinek29603462015-11-17 19:04:39 -08004395 return true;
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004396 } else {
4397 contentView.setViewVisibility(R.id.progress, View.GONE);
Selim Cinek29603462015-11-17 19:04:39 -08004398 return false;
Jeff Sharkey1c400132011-08-05 14:50:13 -07004399 }
Joe Onorato561d3852010-11-20 18:09:34 -08004400 }
4401
Selim Cinek88188f22017-09-19 16:46:56 -07004402 private void bindLargeIcon(RemoteViews contentView, boolean hideLargeIcon,
4403 boolean alwaysShowReply) {
Selim Cinek279fa862016-06-14 10:57:25 -07004404 if (mN.mLargeIcon == null && mN.largeIcon != null) {
4405 mN.mLargeIcon = Icon.createWithBitmap(mN.largeIcon);
4406 }
Selim Cinek88188f22017-09-19 16:46:56 -07004407 boolean showLargeIcon = mN.mLargeIcon != null && !hideLargeIcon;
4408 if (showLargeIcon) {
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004409 contentView.setViewVisibility(R.id.right_icon, View.VISIBLE);
4410 contentView.setImageViewIcon(R.id.right_icon, mN.mLargeIcon);
4411 processLargeLegacyIcon(mN.mLargeIcon, contentView);
Adrian Roos2d5dbba2016-06-08 17:11:53 -07004412 int endMargin = R.dimen.notification_content_picture_margin;
4413 contentView.setViewLayoutMarginEndDimen(R.id.line1, endMargin);
4414 contentView.setViewLayoutMarginEndDimen(R.id.text, endMargin);
4415 contentView.setViewLayoutMarginEndDimen(R.id.progress, endMargin);
Selim Cinek88188f22017-09-19 16:46:56 -07004416 }
4417 // Bind the reply action
4418 Action action = findReplyAction();
Selim Cinek5d6ef8d2017-05-18 22:16:00 -07004419
Selim Cinek88188f22017-09-19 16:46:56 -07004420 boolean actionVisible = action != null && (showLargeIcon || alwaysShowReply);
4421 int replyId = showLargeIcon ? R.id.reply_icon_action : R.id.right_icon;
4422 if (actionVisible) {
4423 // We're only showing the icon as big if we're hiding the large icon
4424 int contrastColor = resolveContrastColor();
4425 int iconColor;
4426 if (showLargeIcon) {
Sunny Goyal5b153922017-09-21 21:00:36 -07004427 contentView.setDrawableTint(R.id.reply_icon_action,
Selim Cinek5d6ef8d2017-05-18 22:16:00 -07004428 true /* targetBackground */,
Sunny Goyal5b153922017-09-21 21:00:36 -07004429 contrastColor, PorterDuff.Mode.SRC_ATOP);
Selim Cinek5d6ef8d2017-05-18 22:16:00 -07004430 contentView.setOnClickPendingIntent(R.id.right_icon,
4431 action.actionIntent);
Selim Cinek5d6ef8d2017-05-18 22:16:00 -07004432 contentView.setRemoteInputs(R.id.right_icon, action.mRemoteInputs);
Selim Cinek88188f22017-09-19 16:46:56 -07004433 iconColor = NotificationColorUtil.isColorLight(contrastColor)
4434 ? Color.BLACK : Color.WHITE;
4435 } else {
4436 contentView.setImageViewResource(R.id.right_icon,
4437 R.drawable.ic_reply_notification_large);
4438 contentView.setViewVisibility(R.id.right_icon, View.VISIBLE);
4439 iconColor = contrastColor;
Selim Cinek5d6ef8d2017-05-18 22:16:00 -07004440 }
Selim Cinek88188f22017-09-19 16:46:56 -07004441 contentView.setDrawableTint(replyId,
4442 false /* targetBackground */,
4443 iconColor,
4444 PorterDuff.Mode.SRC_ATOP);
4445 contentView.setOnClickPendingIntent(replyId,
4446 action.actionIntent);
4447 contentView.setRemoteInputs(replyId, action.mRemoteInputs);
4448 } else {
4449 contentView.setRemoteInputs(R.id.right_icon, null);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004450 }
Selim Cinek88188f22017-09-19 16:46:56 -07004451 contentView.setViewVisibility(R.id.reply_icon_action, actionVisible && showLargeIcon
4452 ? View.VISIBLE
4453 : View.GONE);
4454 contentView.setViewVisibility(R.id.right_icon_container, actionVisible || showLargeIcon
Selim Cinek5d6ef8d2017-05-18 22:16:00 -07004455 ? View.VISIBLE
4456 : View.GONE);
4457 }
4458
4459 private Action findReplyAction() {
4460 ArrayList<Action> actions = mActions;
4461 if (mOriginalActions != null) {
4462 actions = mOriginalActions;
4463 }
4464 int numActions = actions.size();
4465 for (int i = 0; i < numActions; i++) {
4466 Action action = actions.get(i);
4467 if (hasValidRemoteInput(action)) {
4468 return action;
4469 }
4470 }
4471 return null;
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004472 }
4473
Selim Cinekafeed292017-12-12 17:32:44 -08004474 private void bindNotificationHeader(RemoteViews contentView, boolean ambient,
4475 CharSequence secondaryHeaderText) {
Adrian Roos487374f2017-01-11 15:48:14 -08004476 bindSmallIcon(contentView, ambient);
4477 bindHeaderAppName(contentView, ambient);
4478 if (!ambient) {
4479 // Ambient view does not have these
4480 bindHeaderText(contentView);
Selim Cinekafeed292017-12-12 17:32:44 -08004481 bindHeaderTextSecondary(contentView, secondaryHeaderText);
Adrian Roos487374f2017-01-11 15:48:14 -08004482 bindHeaderChronometerAndTime(contentView);
Adrian Roos487374f2017-01-11 15:48:14 -08004483 bindProfileBadge(contentView);
4484 }
Adrian Roosd83e9992017-03-16 15:17:57 -07004485 bindExpandButton(contentView);
Julia Reynoldsfc640012018-02-21 12:25:27 -05004486 mN.mUsesStandardHeader = true;
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004487 }
4488
4489 private void bindExpandButton(RemoteViews contentView) {
Selim Cinek99104832017-01-25 14:47:33 -08004490 int color = getPrimaryHighlightColor();
Sunny Goyal5b153922017-09-21 21:00:36 -07004491 contentView.setDrawableTint(R.id.expand_button, false, color,
4492 PorterDuff.Mode.SRC_ATOP);
Selim Cinekea4bef72015-12-02 15:51:10 -08004493 contentView.setInt(R.id.notification_header, "setOriginalNotificationColor",
Selim Cinek7b9605b2017-01-19 17:36:00 -08004494 color);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004495 }
4496
Selim Cinek99104832017-01-25 14:47:33 -08004497 /**
4498 * @return the color that is used as the first primary highlight color. This is applied
4499 * in several places like the action buttons or the app name in the header.
4500 */
4501 private int getPrimaryHighlightColor() {
4502 return isColorized() ? getPrimaryTextColor() : resolveContrastColor();
4503 }
4504
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004505 private void bindHeaderChronometerAndTime(RemoteViews contentView) {
4506 if (showsTimeOrChronometer()) {
Selim Cinek29603462015-11-17 19:04:39 -08004507 contentView.setViewVisibility(R.id.time_divider, View.VISIBLE);
Selim Cinek7b9605b2017-01-19 17:36:00 -08004508 setTextViewColorSecondary(contentView, R.id.time_divider);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004509 if (mN.extras.getBoolean(EXTRA_SHOW_CHRONOMETER)) {
4510 contentView.setViewVisibility(R.id.chronometer, View.VISIBLE);
4511 contentView.setLong(R.id.chronometer, "setBase",
4512 mN.when + (SystemClock.elapsedRealtime() - System.currentTimeMillis()));
4513 contentView.setBoolean(R.id.chronometer, "setStarted", true);
Adrian Roos96b7e202016-05-17 13:50:38 -07004514 boolean countsDown = mN.extras.getBoolean(EXTRA_CHRONOMETER_COUNT_DOWN);
Selim Cinekc3b752e2016-04-20 16:13:59 -07004515 contentView.setChronometerCountDown(R.id.chronometer, countsDown);
Selim Cinek7b9605b2017-01-19 17:36:00 -08004516 setTextViewColorSecondary(contentView, R.id.chronometer);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004517 } else {
4518 contentView.setViewVisibility(R.id.time, View.VISIBLE);
4519 contentView.setLong(R.id.time, "setTime", mN.when);
Selim Cinek7b9605b2017-01-19 17:36:00 -08004520 setTextViewColorSecondary(contentView, R.id.time);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004521 }
Selim Cinekb85f36fd2016-04-20 18:46:36 -07004522 } else {
4523 // We still want a time to be set but gone, such that we can show and hide it
4524 // on demand in case it's a child notification without anything in the header
4525 contentView.setLong(R.id.time, "setTime", mN.when != 0 ? mN.when : mN.creationTime);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004526 }
4527 }
4528
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07004529 private void bindHeaderText(RemoteViews contentView) {
4530 CharSequence headerText = mN.extras.getCharSequence(EXTRA_SUB_TEXT);
4531 if (headerText == null && mStyle != null && mStyle.mSummaryTextSet
Selim Cinek03d0d652015-11-13 13:18:09 -05004532 && mStyle.hasSummaryInHeader()) {
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07004533 headerText = mStyle.mSummaryText;
Selim Cinek03d0d652015-11-13 13:18:09 -05004534 }
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07004535 if (headerText == null
4536 && mContext.getApplicationInfo().targetSdkVersion < Build.VERSION_CODES.N
4537 && mN.extras.getCharSequence(EXTRA_INFO_TEXT) != null) {
4538 headerText = mN.extras.getCharSequence(EXTRA_INFO_TEXT);
4539 }
4540 if (headerText != null) {
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004541 // TODO: Remove the span entirely to only have the string with propper formating.
Selim Cinek48f66b72017-08-18 16:17:51 -07004542 contentView.setTextViewText(R.id.header_text, processTextSpans(
4543 processLegacyText(headerText)));
Selim Cinek7b9605b2017-01-19 17:36:00 -08004544 setTextViewColorSecondary(contentView, R.id.header_text);
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07004545 contentView.setViewVisibility(R.id.header_text, View.VISIBLE);
4546 contentView.setViewVisibility(R.id.header_text_divider, View.VISIBLE);
Selim Cinek7b9605b2017-01-19 17:36:00 -08004547 setTextViewColorSecondary(contentView, R.id.header_text_divider);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004548 }
4549 }
4550
Selim Cinekafeed292017-12-12 17:32:44 -08004551 private void bindHeaderTextSecondary(RemoteViews contentView, CharSequence secondaryText) {
4552 if (!TextUtils.isEmpty(secondaryText)) {
4553 contentView.setTextViewText(R.id.header_text_secondary, processTextSpans(
4554 processLegacyText(secondaryText)));
4555 setTextViewColorSecondary(contentView, R.id.header_text_secondary);
4556 contentView.setViewVisibility(R.id.header_text_secondary, View.VISIBLE);
4557 contentView.setViewVisibility(R.id.header_text_secondary_divider, View.VISIBLE);
4558 setTextViewColorSecondary(contentView, R.id.header_text_secondary_divider);
4559 }
4560 }
4561
Adrian Rooseba05822016-04-22 17:09:27 -07004562 /**
4563 * @hide
4564 */
4565 public String loadHeaderAppName() {
Dan Sandler732bd6c2016-04-12 14:20:32 -04004566 CharSequence name = null;
4567 final PackageManager pm = mContext.getPackageManager();
4568 if (mN.extras.containsKey(EXTRA_SUBSTITUTE_APP_NAME)) {
4569 // only system packages which lump together a bunch of unrelated stuff
4570 // may substitute a different name to make the purpose of the
4571 // notification more clear. the correct package label should always
4572 // be accessible via SystemUI.
4573 final String pkg = mContext.getPackageName();
4574 final String subName = mN.extras.getString(EXTRA_SUBSTITUTE_APP_NAME);
4575 if (PackageManager.PERMISSION_GRANTED == pm.checkPermission(
4576 android.Manifest.permission.SUBSTITUTE_NOTIFICATION_APP_NAME, pkg)) {
4577 name = subName;
4578 } else {
4579 Log.w(TAG, "warning: pkg "
4580 + pkg + " attempting to substitute app name '" + subName
4581 + "' without holding perm "
4582 + android.Manifest.permission.SUBSTITUTE_NOTIFICATION_APP_NAME);
4583 }
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004584 }
Dan Sandler732bd6c2016-04-12 14:20:32 -04004585 if (TextUtils.isEmpty(name)) {
4586 name = pm.getApplicationLabel(mContext.getApplicationInfo());
4587 }
4588 if (TextUtils.isEmpty(name)) {
4589 // still nothing?
4590 return null;
4591 }
4592
4593 return String.valueOf(name);
4594 }
Adrian Roos487374f2017-01-11 15:48:14 -08004595 private void bindHeaderAppName(RemoteViews contentView, boolean ambient) {
Dan Sandler732bd6c2016-04-12 14:20:32 -04004596 contentView.setTextViewText(R.id.app_name_text, loadHeaderAppName());
Adrian Roos72171622017-01-27 10:32:06 -08004597 if (isColorized() && !ambient) {
Selim Cinek7b9605b2017-01-19 17:36:00 -08004598 setTextViewColorPrimary(contentView, R.id.app_name_text);
4599 } else {
4600 contentView.setTextColor(R.id.app_name_text,
4601 ambient ? resolveAmbientColor() : resolveContrastColor());
4602 }
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004603 }
4604
Adrian Roos487374f2017-01-11 15:48:14 -08004605 private void bindSmallIcon(RemoteViews contentView, boolean ambient) {
Selim Cinek279fa862016-06-14 10:57:25 -07004606 if (mN.mSmallIcon == null && mN.icon != 0) {
4607 mN.mSmallIcon = Icon.createWithResource(mContext, mN.icon);
4608 }
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004609 contentView.setImageViewIcon(R.id.icon, mN.mSmallIcon);
Sunny Goyal5b153922017-09-21 21:00:36 -07004610 contentView.setInt(R.id.icon, "setImageLevel", mN.iconLevel);
Adrian Roos487374f2017-01-11 15:48:14 -08004611 processSmallIconColor(mN.mSmallIcon, contentView, ambient);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004612 }
4613
Jorim Jaggi445d3c02014-08-19 22:33:42 +02004614 /**
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02004615 * @return true if the built notification will show the time or the chronometer; false
4616 * otherwise
4617 */
4618 private boolean showsTimeOrChronometer() {
Selim Cinekc2c0b042016-05-18 17:13:46 -07004619 return mN.showsTime() || mN.showsChronometer();
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02004620 }
4621
Christoph Studerfe718432014-09-01 18:21:18 +02004622 private void resetStandardTemplateWithActions(RemoteViews big) {
Adrian Roos4c1fcc82016-03-31 14:39:39 -07004623 // actions_container is only reset when there are no actions to avoid focus issues with
4624 // remote inputs.
Christoph Studerfe718432014-09-01 18:21:18 +02004625 big.setViewVisibility(R.id.actions, View.GONE);
Christoph Studerfe718432014-09-01 18:21:18 +02004626 big.removeAllViews(R.id.actions);
Adrian Roose458aa82015-12-08 16:17:19 -08004627
4628 big.setViewVisibility(R.id.notification_material_reply_container, View.GONE);
4629 big.setTextViewText(R.id.notification_material_reply_text_1, null);
4630
4631 big.setViewVisibility(R.id.notification_material_reply_text_2, View.GONE);
4632 big.setTextViewText(R.id.notification_material_reply_text_2, null);
4633 big.setViewVisibility(R.id.notification_material_reply_text_3, View.GONE);
4634 big.setTextViewText(R.id.notification_material_reply_text_3, null);
Adrian Roosf852a422016-06-03 13:33:43 -07004635
Selim Cineked64a142018-02-06 18:06:01 -08004636 big.setViewLayoutMarginBottomDimen(R.id.notification_action_list_margin_target,
4637 R.dimen.notification_content_margin);
Christoph Studerfe718432014-09-01 18:21:18 +02004638 }
4639
Daniel Sandler96fd7c12012-03-30 16:37:36 -04004640 private RemoteViews applyStandardTemplateWithActions(int layoutId) {
Adrian Roos70d7aa32017-01-11 15:39:06 -08004641 return applyStandardTemplateWithActions(layoutId, mParams.reset().fillTextsFrom(this));
Adrian Roos48d746a2016-04-12 14:57:28 -07004642 }
4643
Adrian Roos70d7aa32017-01-11 15:39:06 -08004644 private RemoteViews applyStandardTemplateWithActions(int layoutId,
4645 StandardTemplateParams p) {
4646 RemoteViews big = applyStandardTemplate(layoutId, p);
Daniel Sandler96fd7c12012-03-30 16:37:36 -04004647
Christoph Studerfe718432014-09-01 18:21:18 +02004648 resetStandardTemplateWithActions(big);
4649
Adrian Roose458aa82015-12-08 16:17:19 -08004650 boolean validRemoteInput = false;
4651
Daniel Sandler96fd7c12012-03-30 16:37:36 -04004652 int N = mActions.size();
Adrian Roos487374f2017-01-11 15:48:14 -08004653 boolean emphazisedMode = mN.fullScreenIntent != null && !p.ambient;
Selim Cinek06e9e1f2016-07-08 17:14:16 -07004654 big.setBoolean(R.id.actions, "setEmphasizedMode", emphazisedMode);
Daniel Sandler96fd7c12012-03-30 16:37:36 -04004655 if (N > 0) {
Adrian Roos7052de52016-03-03 15:53:34 -08004656 big.setViewVisibility(R.id.actions_container, View.VISIBLE);
Daniel Sandler96fd7c12012-03-30 16:37:36 -04004657 big.setViewVisibility(R.id.actions, View.VISIBLE);
Selim Cineked64a142018-02-06 18:06:01 -08004658 big.setViewLayoutMarginBottomDimen(R.id.notification_action_list_margin_target, 0);
Daniel Sandler8680bf82012-05-15 16:52:52 -04004659 if (N>MAX_ACTION_BUTTONS) N=MAX_ACTION_BUTTONS;
Daniel Sandler96fd7c12012-03-30 16:37:36 -04004660 for (int i=0; i<N; i++) {
Adrian Roose458aa82015-12-08 16:17:19 -08004661 Action action = mActions.get(i);
Selim Cinekffcc7cf2018-02-06 17:43:51 -08004662 boolean actionHasValidInput = hasValidRemoteInput(action);
4663 validRemoteInput |= actionHasValidInput;
Adrian Roose458aa82015-12-08 16:17:19 -08004664
Selim Cinek06e9e1f2016-07-08 17:14:16 -07004665 final RemoteViews button = generateActionButton(action, emphazisedMode,
Adrian Roos487374f2017-01-11 15:48:14 -08004666 i % 2 != 0, p.ambient);
Selim Cinekffcc7cf2018-02-06 17:43:51 -08004667 if (actionHasValidInput) {
4668 // Clear the drawable
4669 button.setInt(R.id.action0, "setBackgroundResource", 0);
4670 }
Daniel Sandler96fd7c12012-03-30 16:37:36 -04004671 big.addView(R.id.actions, button);
4672 }
Adrian Roos4c1fcc82016-03-31 14:39:39 -07004673 } else {
4674 big.setViewVisibility(R.id.actions_container, View.GONE);
Daniel Sandler96fd7c12012-03-30 16:37:36 -04004675 }
Adrian Roose458aa82015-12-08 16:17:19 -08004676
4677 CharSequence[] replyText = mN.extras.getCharSequenceArray(EXTRA_REMOTE_INPUT_HISTORY);
Adrian Roos487374f2017-01-11 15:48:14 -08004678 if (!p.ambient && validRemoteInput && replyText != null
Adrian Roose458aa82015-12-08 16:17:19 -08004679 && replyText.length > 0 && !TextUtils.isEmpty(replyText[0])) {
4680 big.setViewVisibility(R.id.notification_material_reply_container, View.VISIBLE);
Selim Cinek48f66b72017-08-18 16:17:51 -07004681 big.setTextViewText(R.id.notification_material_reply_text_1,
4682 processTextSpans(replyText[0]));
Selim Cinek7b9605b2017-01-19 17:36:00 -08004683 setTextViewColorSecondary(big, R.id.notification_material_reply_text_1);
Adrian Roose458aa82015-12-08 16:17:19 -08004684
4685 if (replyText.length > 1 && !TextUtils.isEmpty(replyText[1])) {
4686 big.setViewVisibility(R.id.notification_material_reply_text_2, View.VISIBLE);
Selim Cinek48f66b72017-08-18 16:17:51 -07004687 big.setTextViewText(R.id.notification_material_reply_text_2,
4688 processTextSpans(replyText[1]));
Selim Cinek7b9605b2017-01-19 17:36:00 -08004689 setTextViewColorSecondary(big, R.id.notification_material_reply_text_2);
Adrian Roose458aa82015-12-08 16:17:19 -08004690
4691 if (replyText.length > 2 && !TextUtils.isEmpty(replyText[2])) {
4692 big.setViewVisibility(
4693 R.id.notification_material_reply_text_3, View.VISIBLE);
Selim Cinek48f66b72017-08-18 16:17:51 -07004694 big.setTextViewText(R.id.notification_material_reply_text_3,
4695 processTextSpans(replyText[2]));
Selim Cinek7b9605b2017-01-19 17:36:00 -08004696 setTextViewColorSecondary(big, R.id.notification_material_reply_text_3);
Adrian Roose458aa82015-12-08 16:17:19 -08004697 }
4698 }
4699 }
4700
Daniel Sandler96fd7c12012-03-30 16:37:36 -04004701 return big;
4702 }
4703
Adrian Roose458aa82015-12-08 16:17:19 -08004704 private boolean hasValidRemoteInput(Action action) {
4705 if (TextUtils.isEmpty(action.title) || action.actionIntent == null) {
4706 // Weird actions
4707 return false;
4708 }
4709
4710 RemoteInput[] remoteInputs = action.getRemoteInputs();
4711 if (remoteInputs == null) {
4712 return false;
4713 }
4714
4715 for (RemoteInput r : remoteInputs) {
4716 CharSequence[] choices = r.getChoices();
4717 if (r.getAllowFreeFormInput() || (choices != null && choices.length != 0)) {
4718 return true;
4719 }
4720 }
4721 return false;
4722 }
4723
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004724 /**
4725 * Construct a RemoteViews for the final 1U notification layout. In order:
4726 * 1. Custom contentView from the caller
4727 * 2. Style's proposed content view
4728 * 3. Standard template view
4729 */
Julia Reynolds3b848122016-02-26 10:45:32 -05004730 public RemoteViews createContentView() {
Selim Cinek7d1009b2017-01-25 15:28:28 -08004731 return createContentView(false /* increasedheight */ );
4732 }
4733
4734 /**
4735 * Construct a RemoteViews for the smaller content view.
4736 *
4737 * @param increasedHeight true if this layout be created with an increased height. Some
4738 * styles may support showing more then just that basic 1U size
4739 * and the system may decide to render important notifications
4740 * slightly bigger even when collapsed.
4741 *
4742 * @hide
4743 */
4744 public RemoteViews createContentView(boolean increasedHeight) {
Selim Cineka7679b62017-05-10 16:33:25 -07004745 if (mN.contentView != null && useExistingRemoteView()) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004746 return mN.contentView;
4747 } else if (mStyle != null) {
Selim Cinek7d1009b2017-01-25 15:28:28 -08004748 final RemoteViews styleView = mStyle.makeContentView(increasedHeight);
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004749 if (styleView != null) {
4750 return styleView;
4751 }
Joe Onorato46439ce2010-11-19 13:56:21 -08004752 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004753 return applyStandardTemplate(getBaseLayoutResource());
Joe Onorato46439ce2010-11-19 13:56:21 -08004754 }
4755
Selim Cineka7679b62017-05-10 16:33:25 -07004756 private boolean useExistingRemoteView() {
4757 return mStyle == null || (!mStyle.displayCustomViewInline()
4758 && !mRebuildStyledRemoteViews);
4759 }
4760
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004761 /**
4762 * Construct a RemoteViews for the final big notification layout.
4763 */
Julia Reynolds3b848122016-02-26 10:45:32 -05004764 public RemoteViews createBigContentView() {
Selim Cinek850a8542015-11-11 11:48:36 -05004765 RemoteViews result = null;
Selim Cineka7679b62017-05-10 16:33:25 -07004766 if (mN.bigContentView != null && useExistingRemoteView()) {
Julia Reynolds089e3e42015-11-18 09:59:57 -05004767 return mN.bigContentView;
4768 } else if (mStyle != null) {
Selim Cinek850a8542015-11-11 11:48:36 -05004769 result = mStyle.makeBigContentView();
Selim Cinek90dcf6d2015-11-18 20:24:13 -08004770 hideLine1Text(result);
Selim Cinekcc10bfb2016-02-10 16:24:21 -08004771 } else if (mActions.size() != 0) {
4772 result = applyStandardTemplateWithActions(getBigBaseLayoutResource());
Selim Cinek850a8542015-11-11 11:48:36 -05004773 }
Selim Cinek6743c0b2017-01-18 18:24:01 -08004774 makeHeaderExpanded(result);
Selim Cinek850a8542015-11-11 11:48:36 -05004775 return result;
4776 }
Daniel Sandler96fd7c12012-03-30 16:37:36 -04004777
Selim Cinekeaa29ca2015-11-23 13:51:13 -08004778 /**
Selim Cinek414ad332017-02-24 19:06:12 -08004779 * Construct a RemoteViews for the final notification header only. This will not be
4780 * colorized.
Selim Cinekeaa29ca2015-11-23 13:51:13 -08004781 *
Adrian Roos6f6e1592017-05-02 16:22:53 -07004782 * @param ambient if true, generate the header for the ambient display layout.
Selim Cinekeaa29ca2015-11-23 13:51:13 -08004783 * @hide
4784 */
Adrian Roos6f6e1592017-05-02 16:22:53 -07004785 public RemoteViews makeNotificationHeader(boolean ambient) {
Selim Cinek414ad332017-02-24 19:06:12 -08004786 Boolean colorized = (Boolean) mN.extras.get(EXTRA_COLORIZED);
4787 mN.extras.putBoolean(EXTRA_COLORIZED, false);
Selim Cinekeaa29ca2015-11-23 13:51:13 -08004788 RemoteViews header = new BuilderRemoteViews(mContext.getApplicationInfo(),
Adrian Roos6f6e1592017-05-02 16:22:53 -07004789 ambient ? R.layout.notification_template_ambient_header
4790 : R.layout.notification_template_header);
Selim Cinekeaa29ca2015-11-23 13:51:13 -08004791 resetNotificationHeader(header);
Selim Cinekafeed292017-12-12 17:32:44 -08004792 bindNotificationHeader(header, ambient, null);
Selim Cinek414ad332017-02-24 19:06:12 -08004793 if (colorized != null) {
4794 mN.extras.putBoolean(EXTRA_COLORIZED, colorized);
4795 } else {
4796 mN.extras.remove(EXTRA_COLORIZED);
4797 }
Selim Cinekeaa29ca2015-11-23 13:51:13 -08004798 return header;
4799 }
4800
Adrian Roos487374f2017-01-11 15:48:14 -08004801 /**
4802 * Construct a RemoteViews for the ambient version of the notification.
4803 *
4804 * @hide
4805 */
4806 public RemoteViews makeAmbientNotification() {
4807 RemoteViews ambient = applyStandardTemplateWithActions(
4808 R.layout.notification_template_material_ambient,
Adrian Roos0bc3f6a2017-03-06 11:54:05 -08004809 mParams.reset().ambient(true).fillTextsFrom(this).hasProgress(false));
Adrian Roos487374f2017-01-11 15:48:14 -08004810 return ambient;
4811 }
4812
Selim Cinek29603462015-11-17 19:04:39 -08004813 private void hideLine1Text(RemoteViews result) {
Selim Cinekcc10bfb2016-02-10 16:24:21 -08004814 if (result != null) {
4815 result.setViewVisibility(R.id.text_line_1, View.GONE);
4816 }
Selim Cinek29603462015-11-17 19:04:39 -08004817 }
4818
Selim Cinek6743c0b2017-01-18 18:24:01 -08004819 /**
4820 * Adapt the Notification header if this view is used as an expanded view.
4821 *
4822 * @hide
4823 */
4824 public static void makeHeaderExpanded(RemoteViews result) {
Selim Cinekcc10bfb2016-02-10 16:24:21 -08004825 if (result != null) {
4826 result.setBoolean(R.id.notification_header, "setExpanded", true);
4827 }
Daniel Sandler96fd7c12012-03-30 16:37:36 -04004828 }
4829
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004830 /**
4831 * Construct a RemoteViews for the final heads-up notification layout.
Selim Cinek87ed69b2017-02-09 15:59:43 -08004832 *
4833 * @param increasedHeight true if this layout be created with an increased height. Some
4834 * styles may support showing more then just that basic 1U size
4835 * and the system may decide to render important notifications
4836 * slightly bigger even when collapsed.
4837 *
4838 * @hide
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004839 */
Selim Cinek87ed69b2017-02-09 15:59:43 -08004840 public RemoteViews createHeadsUpContentView(boolean increasedHeight) {
Selim Cineka7679b62017-05-10 16:33:25 -07004841 if (mN.headsUpContentView != null && useExistingRemoteView()) {
Julia Reynolds089e3e42015-11-18 09:59:57 -05004842 return mN.headsUpContentView;
4843 } else if (mStyle != null) {
Selim Cinek87ed69b2017-02-09 15:59:43 -08004844 final RemoteViews styleView = mStyle.makeHeadsUpContentView(increasedHeight);
4845 if (styleView != null) {
4846 return styleView;
4847 }
Julia Reynolds089e3e42015-11-18 09:59:57 -05004848 } else if (mActions.size() == 0) {
4849 return null;
4850 }
4851
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01004852 return applyStandardTemplateWithActions(getBigBaseLayoutResource());
Chris Wren8fd39ec2014-02-27 17:43:26 -05004853 }
4854
Selim Cinek624c02db2015-12-14 21:00:02 -08004855 /**
Selim Cinek87ed69b2017-02-09 15:59:43 -08004856 * Construct a RemoteViews for the final heads-up notification layout.
4857 */
4858 public RemoteViews createHeadsUpContentView() {
4859 return createHeadsUpContentView(false /* useIncreasedHeight */);
4860 }
4861
4862 /**
Selim Cinek624c02db2015-12-14 21:00:02 -08004863 * Construct a RemoteViews for the display in public contexts like on the lockscreen.
4864 *
4865 * @hide
4866 */
4867 public RemoteViews makePublicContentView() {
Adrian Roos1a1ecfc2017-04-17 11:17:59 -07004868 return makePublicView(false /* ambient */);
4869 }
4870
4871 /**
4872 * Construct a RemoteViews for the display in public contexts like on the lockscreen.
4873 *
4874 * @hide
4875 */
4876 public RemoteViews makePublicAmbientNotification() {
4877 return makePublicView(true /* ambient */);
4878 }
4879
4880 private RemoteViews makePublicView(boolean ambient) {
Selim Cinek624c02db2015-12-14 21:00:02 -08004881 if (mN.publicVersion != null) {
4882 final Builder builder = recoverBuilder(mContext, mN.publicVersion);
Adrian Roos1a1ecfc2017-04-17 11:17:59 -07004883 return ambient ? builder.makeAmbientNotification() : builder.createContentView();
Selim Cinek624c02db2015-12-14 21:00:02 -08004884 }
4885 Bundle savedBundle = mN.extras;
4886 Style style = mStyle;
4887 mStyle = null;
4888 Icon largeIcon = mN.mLargeIcon;
4889 mN.mLargeIcon = null;
Selim Cinek279fa862016-06-14 10:57:25 -07004890 Bitmap largeIconLegacy = mN.largeIcon;
4891 mN.largeIcon = null;
Adrian Roosb19b06b2017-05-02 18:54:40 -07004892 ArrayList<Action> actions = mActions;
4893 mActions = new ArrayList<>();
Selim Cinek624c02db2015-12-14 21:00:02 -08004894 Bundle publicExtras = new Bundle();
4895 publicExtras.putBoolean(EXTRA_SHOW_WHEN,
4896 savedBundle.getBoolean(EXTRA_SHOW_WHEN));
4897 publicExtras.putBoolean(EXTRA_SHOW_CHRONOMETER,
4898 savedBundle.getBoolean(EXTRA_SHOW_CHRONOMETER));
Adrian Roos96b7e202016-05-17 13:50:38 -07004899 publicExtras.putBoolean(EXTRA_CHRONOMETER_COUNT_DOWN,
4900 savedBundle.getBoolean(EXTRA_CHRONOMETER_COUNT_DOWN));
Selim Cinek624c02db2015-12-14 21:00:02 -08004901 mN.extras = publicExtras;
Selim Cinek499c20f2017-07-20 14:06:09 -07004902 RemoteViews view;
4903 if (ambient) {
4904 publicExtras.putCharSequence(EXTRA_TITLE,
4905 mContext.getString(com.android.internal.R.string.notification_hidden_text));
4906 view = makeAmbientNotification();
4907 } else{
4908 view = makeNotificationHeader(false /* ambient */);
4909 view.setBoolean(R.id.notification_header, "setExpandOnlyOnButton", true);
4910 }
Selim Cinek624c02db2015-12-14 21:00:02 -08004911 mN.extras = savedBundle;
4912 mN.mLargeIcon = largeIcon;
Selim Cinek279fa862016-06-14 10:57:25 -07004913 mN.largeIcon = largeIconLegacy;
Adrian Roosb19b06b2017-05-02 18:54:40 -07004914 mActions = actions;
Selim Cinek624c02db2015-12-14 21:00:02 -08004915 mStyle = style;
Adrian Roos1a1ecfc2017-04-17 11:17:59 -07004916 return view;
Selim Cinek624c02db2015-12-14 21:00:02 -08004917 }
4918
Selim Cinek6743c0b2017-01-18 18:24:01 -08004919 /**
4920 * Construct a content view for the display when low - priority
4921 *
4922 * @param useRegularSubtext uses the normal subtext set if there is one available. Otherwise
4923 * a new subtext is created consisting of the content of the
4924 * notification.
4925 * @hide
4926 */
4927 public RemoteViews makeLowPriorityContentView(boolean useRegularSubtext) {
4928 int color = mN.color;
4929 mN.color = COLOR_DEFAULT;
4930 CharSequence summary = mN.extras.getCharSequence(EXTRA_SUB_TEXT);
4931 if (!useRegularSubtext || TextUtils.isEmpty(summary)) {
4932 CharSequence newSummary = createSummaryText();
4933 if (!TextUtils.isEmpty(newSummary)) {
4934 mN.extras.putCharSequence(EXTRA_SUB_TEXT, newSummary);
4935 }
4936 }
Selim Cinek875ba9b2017-02-13 16:20:17 -08004937
Adrian Roos6f6e1592017-05-02 16:22:53 -07004938 RemoteViews header = makeNotificationHeader(false /* ambient */);
Selim Cinek1b554392017-02-28 17:22:49 -08004939 header.setBoolean(R.id.notification_header, "setAcceptAllTouches", true);
Selim Cinek6743c0b2017-01-18 18:24:01 -08004940 if (summary != null) {
4941 mN.extras.putCharSequence(EXTRA_SUB_TEXT, summary);
4942 } else {
4943 mN.extras.remove(EXTRA_SUB_TEXT);
4944 }
4945 mN.color = color;
4946 return header;
4947 }
Selim Cinek624c02db2015-12-14 21:00:02 -08004948
Selim Cinek6743c0b2017-01-18 18:24:01 -08004949 private CharSequence createSummaryText() {
4950 CharSequence titleText = mN.extras.getCharSequence(Notification.EXTRA_TITLE);
4951 if (USE_ONLY_TITLE_IN_LOW_PRIORITY_SUMMARY) {
4952 return titleText;
4953 }
4954 SpannableStringBuilder summary = new SpannableStringBuilder();
4955 if (titleText == null) {
4956 titleText = mN.extras.getCharSequence(Notification.EXTRA_TITLE_BIG);
4957 }
4958 BidiFormatter bidi = BidiFormatter.getInstance();
4959 if (titleText != null) {
4960 summary.append(bidi.unicodeWrap(titleText));
4961 }
4962 CharSequence contentText = mN.extras.getCharSequence(Notification.EXTRA_TEXT);
4963 if (titleText != null && contentText != null) {
4964 summary.append(bidi.unicodeWrap(mContext.getText(
4965 R.string.notification_header_divider_symbol_with_spaces)));
4966 }
4967 if (contentText != null) {
4968 summary.append(bidi.unicodeWrap(contentText));
4969 }
4970 return summary;
4971 }
Chris Wren8fd39ec2014-02-27 17:43:26 -05004972
Selim Cinek06e9e1f2016-07-08 17:14:16 -07004973 private RemoteViews generateActionButton(Action action, boolean emphazisedMode,
Adrian Roos487374f2017-01-11 15:48:14 -08004974 boolean oddAction, boolean ambient) {
Daniel Sandler8680bf82012-05-15 16:52:52 -04004975 final boolean tombstone = (action.actionIntent == null);
Selim Cinekf33b1112015-07-15 17:45:11 -07004976 RemoteViews button = new BuilderRemoteViews(mContext.getApplicationInfo(),
Selim Cinek06e9e1f2016-07-08 17:14:16 -07004977 emphazisedMode ? getEmphasizedActionLayoutResource()
4978 : tombstone ? getActionTombstoneLayoutResource()
4979 : getActionLayoutResource());
Daniel Sandler8680bf82012-05-15 16:52:52 -04004980 if (!tombstone) {
Daniel Sandlere5518842012-05-10 16:20:40 -04004981 button.setOnClickPendingIntent(R.id.action0, action.actionIntent);
Daniel Sandlere5518842012-05-10 16:20:40 -04004982 }
Daniel Sandler96fd7c12012-03-30 16:37:36 -04004983 button.setContentDescription(R.id.action0, action.title);
Adrian Roosfe84e1f2015-11-04 15:55:39 -08004984 if (action.mRemoteInputs != null) {
4985 button.setRemoteInputs(R.id.action0, action.mRemoteInputs);
4986 }
Selim Cinek7b9605b2017-01-19 17:36:00 -08004987 // TODO: handle emphasized mode / actions right
Selim Cinek06e9e1f2016-07-08 17:14:16 -07004988 if (emphazisedMode) {
Selim Cinek981962e2016-07-20 20:41:58 -07004989 // change the background bgColor
Selim Cinek622c64a2017-04-17 17:10:05 -07004990 int bgColor;
4991 if (isColorized()) {
4992 bgColor = oddAction ? getActionBarColor() : getActionBarColorDeEmphasized();
4993 } else {
4994 bgColor = mContext.getColor(oddAction ? R.color.notification_action_list
4995 : R.color.notification_action_list_dark);
4996 }
Sunny Goyal5b153922017-09-21 21:00:36 -07004997 button.setDrawableTint(R.id.button_holder, true,
4998 bgColor, PorterDuff.Mode.SRC_ATOP);
Selim Cinek981962e2016-07-20 20:41:58 -07004999 CharSequence title = action.title;
5000 ColorStateList[] outResultColor = null;
5001 if (isLegacy()) {
Selim Cinek87c31532017-08-18 18:53:44 -07005002 title = NotificationColorUtil.clearColorSpans(title);
Selim Cinek981962e2016-07-20 20:41:58 -07005003 } else {
5004 outResultColor = new ColorStateList[1];
5005 title = ensureColorSpanContrast(title, bgColor, outResultColor);
5006 }
Selim Cinek48f66b72017-08-18 16:17:51 -07005007 button.setTextViewText(R.id.action0, processTextSpans(title));
Selim Cinek7b9605b2017-01-19 17:36:00 -08005008 setTextViewColorPrimary(button, R.id.action0);
Selim Cinek981962e2016-07-20 20:41:58 -07005009 if (outResultColor != null && outResultColor[0] != null) {
5010 // We need to set the text color as well since changing a text to uppercase
5011 // clears its spans.
5012 button.setTextColor(R.id.action0, outResultColor[0]);
Anthony Chenad4d1582017-04-10 16:07:58 -07005013 } else if (mN.color != COLOR_DEFAULT && !isColorized() && mTintActionButtons) {
Selim Cinek981962e2016-07-20 20:41:58 -07005014 button.setTextColor(R.id.action0,resolveContrastColor());
5015 }
Selim Cinek06e9e1f2016-07-08 17:14:16 -07005016 } else {
Selim Cinek48f66b72017-08-18 16:17:51 -07005017 button.setTextViewText(R.id.action0, processTextSpans(
5018 processLegacyText(action.title)));
Adrian Roos72171622017-01-27 10:32:06 -08005019 if (isColorized() && !ambient) {
Selim Cinek7b9605b2017-01-19 17:36:00 -08005020 setTextViewColorPrimary(button, R.id.action0);
Anthony Chenad4d1582017-04-10 16:07:58 -07005021 } else if (mN.color != COLOR_DEFAULT && mTintActionButtons) {
Adrian Roos487374f2017-01-11 15:48:14 -08005022 button.setTextColor(R.id.action0,
5023 ambient ? resolveAmbientColor() : resolveContrastColor());
Selim Cinek06e9e1f2016-07-08 17:14:16 -07005024 }
Adrian Roosfe84e1f2015-11-04 15:55:39 -08005025 }
Daniel Sandler96fd7c12012-03-30 16:37:36 -04005026 return button;
5027 }
5028
Joe Onoratocb109a02011-01-18 17:57:41 -08005029 /**
Selim Cinek981962e2016-07-20 20:41:58 -07005030 * Ensures contrast on color spans against a background color. also returns the color of the
5031 * text if a span was found that spans over the whole text.
5032 *
5033 * @param charSequence the charSequence on which the spans are
5034 * @param background the background color to ensure the contrast against
5035 * @param outResultColor an array in which a color will be returned as the first element if
5036 * there exists a full length color span.
5037 * @return the contrasted charSequence
5038 */
5039 private CharSequence ensureColorSpanContrast(CharSequence charSequence, int background,
5040 ColorStateList[] outResultColor) {
5041 if (charSequence instanceof Spanned) {
5042 Spanned ss = (Spanned) charSequence;
5043 Object[] spans = ss.getSpans(0, ss.length(), Object.class);
5044 SpannableStringBuilder builder = new SpannableStringBuilder(ss.toString());
5045 for (Object span : spans) {
5046 Object resultSpan = span;
5047 int spanStart = ss.getSpanStart(span);
5048 int spanEnd = ss.getSpanEnd(span);
5049 boolean fullLength = (spanEnd - spanStart) == charSequence.length();
5050 if (resultSpan instanceof CharacterStyle) {
5051 resultSpan = ((CharacterStyle) span).getUnderlying();
5052 }
5053 if (resultSpan instanceof TextAppearanceSpan) {
5054 TextAppearanceSpan originalSpan = (TextAppearanceSpan) resultSpan;
5055 ColorStateList textColor = originalSpan.getTextColor();
5056 if (textColor != null) {
5057 int[] colors = textColor.getColors();
5058 int[] newColors = new int[colors.length];
5059 for (int i = 0; i < newColors.length; i++) {
5060 newColors[i] = NotificationColorUtil.ensureLargeTextContrast(
Anthony Chenad4d1582017-04-10 16:07:58 -07005061 colors[i], background, mInNightMode);
Selim Cinek981962e2016-07-20 20:41:58 -07005062 }
5063 textColor = new ColorStateList(textColor.getStates().clone(),
5064 newColors);
5065 resultSpan = new TextAppearanceSpan(
5066 originalSpan.getFamily(),
5067 originalSpan.getTextStyle(),
5068 originalSpan.getTextSize(),
5069 textColor,
5070 originalSpan.getLinkTextColor());
5071 if (fullLength) {
5072 outResultColor[0] = new ColorStateList(
5073 textColor.getStates().clone(), newColors);
5074 }
5075 }
5076 } else if (resultSpan instanceof ForegroundColorSpan) {
5077 ForegroundColorSpan originalSpan = (ForegroundColorSpan) resultSpan;
5078 int foregroundColor = originalSpan.getForegroundColor();
5079 foregroundColor = NotificationColorUtil.ensureLargeTextContrast(
Anthony Chenad4d1582017-04-10 16:07:58 -07005080 foregroundColor, background, mInNightMode);
Selim Cinek981962e2016-07-20 20:41:58 -07005081 resultSpan = new ForegroundColorSpan(foregroundColor);
5082 if (fullLength) {
5083 outResultColor[0] = ColorStateList.valueOf(foregroundColor);
5084 }
5085 } else {
5086 resultSpan = span;
5087 }
5088
5089 builder.setSpan(resultSpan, spanStart, spanEnd, ss.getSpanFlags(span));
5090 }
5091 return builder;
5092 }
5093 return charSequence;
5094 }
5095
5096 /**
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01005097 * @return Whether we are currently building a notification from a legacy (an app that
Alan Viverette3cb07a462014-06-06 14:19:53 -07005098 * doesn't create material notifications by itself) app.
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01005099 */
5100 private boolean isLegacy() {
Selim Cinek7b9605b2017-01-19 17:36:00 -08005101 if (!mIsLegacyInitialized) {
5102 mIsLegacy = mContext.getApplicationInfo().targetSdkVersion
5103 < Build.VERSION_CODES.LOLLIPOP;
5104 mIsLegacyInitialized = true;
5105 }
5106 return mIsLegacy;
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01005107 }
5108
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01005109 private CharSequence processLegacyText(CharSequence charSequence) {
Adrian Roos0bc3f6a2017-03-06 11:54:05 -08005110 return processLegacyText(charSequence, false /* ambient */);
5111 }
5112
5113 private CharSequence processLegacyText(CharSequence charSequence, boolean ambient) {
5114 boolean isAlreadyLightText = isLegacy() || textColorsNeedInversion();
5115 boolean wantLightText = ambient;
5116 if (isAlreadyLightText != wantLightText) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005117 return getColorUtil().invertCharSequenceColors(charSequence);
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01005118 } else {
5119 return charSequence;
5120 }
5121 }
5122
Dan Sandler26e81cf2014-05-06 10:01:27 -04005123 /**
Selim Cinek65b2e7c2015-10-26 14:11:31 -07005124 * Apply any necessariy colors to the small icon
Dan Sandler26e81cf2014-05-06 10:01:27 -04005125 */
Adrian Roos487374f2017-01-11 15:48:14 -08005126 private void processSmallIconColor(Icon smallIcon, RemoteViews contentView,
5127 boolean ambient) {
Selim Cinekea4bef72015-12-02 15:51:10 -08005128 boolean colorable = !isLegacy() || getColorUtil().isGrayscaleIcon(mContext, smallIcon);
Selim Cinek99104832017-01-25 14:47:33 -08005129 int color = ambient ? resolveAmbientColor() : getPrimaryHighlightColor();
Selim Cinekea4bef72015-12-02 15:51:10 -08005130 if (colorable) {
Sunny Goyal5b153922017-09-21 21:00:36 -07005131 contentView.setDrawableTint(R.id.icon, false, color,
5132 PorterDuff.Mode.SRC_ATOP);
Selim Cinekea4bef72015-12-02 15:51:10 -08005133
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01005134 }
Selim Cinekea4bef72015-12-02 15:51:10 -08005135 contentView.setInt(R.id.notification_header, "setOriginalIconColor",
Adrian Roos487374f2017-01-11 15:48:14 -08005136 colorable ? color : NotificationHeaderView.NO_COLOR);
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01005137 }
5138
Dan Sandler26e81cf2014-05-06 10:01:27 -04005139 /**
Selim Cinek65b2e7c2015-10-26 14:11:31 -07005140 * Make the largeIcon dark if it's a fake smallIcon (that is,
Dan Sandler26e81cf2014-05-06 10:01:27 -04005141 * if it's grayscale).
5142 */
5143 // TODO: also check bounds, transparency, that sort of thing.
Dan Sandlerd63f9322015-05-06 15:18:49 -04005144 private void processLargeLegacyIcon(Icon largeIcon, RemoteViews contentView) {
5145 if (largeIcon != null && isLegacy()
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005146 && getColorUtil().isGrayscaleIcon(mContext, largeIcon)) {
Selim Cinek65b2e7c2015-10-26 14:11:31 -07005147 // resolve color will fall back to the default when legacy
Sunny Goyal5b153922017-09-21 21:00:36 -07005148 contentView.setDrawableTint(R.id.icon, false, resolveContrastColor(),
5149 PorterDuff.Mode.SRC_ATOP);
Jorim Jaggi92df1f22014-12-16 19:44:41 +01005150 }
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01005151 }
5152
Julia Reynolds10ee1fc2015-11-09 11:04:55 -05005153 private void sanitizeColor() {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005154 if (mN.color != COLOR_DEFAULT) {
5155 mN.color |= 0xFF000000; // no alpha for custom colors
Jorim Jaggi74419312014-06-10 20:57:21 +02005156 }
Jorim Jaggi74419312014-06-10 20:57:21 +02005157 }
5158
Adrian Roos4ff3b122016-02-01 12:26:13 -08005159 int resolveContrastColor() {
5160 if (mCachedContrastColorIsFor == mN.color && mCachedContrastColor != COLOR_INVALID) {
5161 return mCachedContrastColor;
Dan Sandler26e81cf2014-05-06 10:01:27 -04005162 }
Adrian Roos4ff3b122016-02-01 12:26:13 -08005163
Selim Cinekac5f0272017-05-02 16:05:41 -07005164 int color;
5165 int background = mBackgroundColorHint;
5166 if (mBackgroundColorHint == COLOR_INVALID) {
5167 background = mContext.getColor(
5168 com.android.internal.R.color.notification_material_background_color);
5169 }
5170 if (mN.color == COLOR_DEFAULT) {
5171 ensureColors();
5172 color = mSecondaryTextColor;
5173 } else {
5174 color = NotificationColorUtil.resolveContrastColor(mContext, mN.color,
Anthony Chenad4d1582017-04-10 16:07:58 -07005175 background, mInNightMode);
Selim Cinekac5f0272017-05-02 16:05:41 -07005176 }
5177 if (Color.alpha(color) < 255) {
5178 // alpha doesn't go well for color filters, so let's blend it manually
5179 color = NotificationColorUtil.compositeColors(color, background);
5180 }
Adrian Roos4ff3b122016-02-01 12:26:13 -08005181 mCachedContrastColorIsFor = mN.color;
Selim Cinekac5f0272017-05-02 16:05:41 -07005182 return mCachedContrastColor = color;
Dan Sandler26e81cf2014-05-06 10:01:27 -04005183 }
5184
Adrian Roos487374f2017-01-11 15:48:14 -08005185 int resolveAmbientColor() {
5186 if (mCachedAmbientColorIsFor == mN.color && mCachedAmbientColorIsFor != COLOR_INVALID) {
5187 return mCachedAmbientColor;
5188 }
5189 final int contrasted = NotificationColorUtil.resolveAmbientColor(mContext, mN.color);
5190
5191 mCachedAmbientColorIsFor = mN.color;
5192 return mCachedAmbientColor = contrasted;
5193 }
5194
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01005195 /**
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005196 * Apply the unstyled operations and return a new {@link Notification} object.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04005197 * @hide
Joe Onoratocb109a02011-01-18 17:57:41 -08005198 */
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04005199 public Notification buildUnstyled() {
Daniel Sandlera0a938c2012-03-15 08:42:37 -04005200 if (mActions.size() > 0) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005201 mN.actions = new Action[mActions.size()];
5202 mActions.toArray(mN.actions);
Daniel Sandlera0a938c2012-03-15 08:42:37 -04005203 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005204 if (!mPersonList.isEmpty()) {
Selim Cineke7238dd2017-12-14 17:48:32 -08005205 mN.extras.putParcelableArrayList(EXTRA_PEOPLE_LIST, mPersonList);
Dan Sandler0bf2ed82013-12-21 23:33:41 -06005206 }
Selim Cinek247fa012016-02-18 09:50:48 -08005207 if (mN.bigContentView != null || mN.contentView != null
5208 || mN.headsUpContentView != null) {
5209 mN.extras.putBoolean(EXTRA_CONTAINS_CUSTOM_VIEW, true);
5210 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005211 return mN;
Joe Onorato46439ce2010-11-19 13:56:21 -08005212 }
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005213
Julia Reynolds3b848122016-02-26 10:45:32 -05005214 /**
5215 * Creates a Builder from an existing notification so further changes can be made.
5216 * @param context The context for your application / activity.
5217 * @param n The notification to create a Builder from.
5218 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005219 public static Notification.Builder recoverBuilder(Context context, Notification n) {
Christoph Studer4600f9b2014-07-22 22:44:43 +02005220 // Re-create notification context so we can access app resources.
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005221 ApplicationInfo applicationInfo = n.extras.getParcelable(
5222 EXTRA_BUILDER_APPLICATION_INFO);
Christoph Studer4600f9b2014-07-22 22:44:43 +02005223 Context builderContext;
Julia Reynoldsda303542015-11-23 14:00:20 -05005224 if (applicationInfo != null) {
5225 try {
5226 builderContext = context.createApplicationContext(applicationInfo,
5227 Context.CONTEXT_RESTRICTED);
5228 } catch (NameNotFoundException e) {
5229 Log.e(TAG, "ApplicationInfo " + applicationInfo + " not found");
5230 builderContext = context; // try with our context
5231 }
5232 } else {
5233 builderContext = context; // try with given context
Christoph Studer4600f9b2014-07-22 22:44:43 +02005234 }
5235
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005236 return new Builder(builderContext, n);
Christoph Studer4600f9b2014-07-22 22:44:43 +02005237 }
5238
Daniel Sandlerf45564e2013-04-15 15:05:08 -04005239 /**
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005240 * @deprecated Use {@link #build()} instead.
5241 */
5242 @Deprecated
5243 public Notification getNotification() {
5244 return build();
5245 }
5246
5247 /**
5248 * Combine all of the options that have been set and return a new {@link Notification}
5249 * object.
5250 */
5251 public Notification build() {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005252 // first, add any extras from the calling code
5253 if (mUserExtras != null) {
5254 mN.extras = getAllExtras();
Jorim Jaggia0d58ae2015-06-03 11:48:13 -07005255 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005256
Selim Cinekb85f36fd2016-04-20 18:46:36 -07005257 mN.creationTime = System.currentTimeMillis();
5258
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005259 // lazy stuff from mContext; see comment in Builder(Context, Notification)
Julia Reynoldsda303542015-11-23 14:00:20 -05005260 Notification.addFieldsFromContext(mContext, mN);
Christoph Studer943aa672014-08-03 20:31:16 +02005261
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005262 buildUnstyled();
Daniel Sandlerf45564e2013-04-15 15:05:08 -04005263
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005264 if (mStyle != null) {
Selim Cinekd0426622017-07-11 13:19:59 +02005265 mStyle.reduceImageSizes(mContext);
5266 mStyle.purgeResources();
Selim Cinek90343862018-02-01 11:07:11 -08005267 mStyle.validate(mContext);
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005268 mStyle.buildStyled(mN);
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005269 }
Selim Cinekd0426622017-07-11 13:19:59 +02005270 mN.reduceImageSizes(mContext);
5271
Adrian Roos5081c0d2016-02-26 16:04:19 -08005272 if (mContext.getApplicationInfo().targetSdkVersion < Build.VERSION_CODES.N
Selim Cineka7679b62017-05-10 16:33:25 -07005273 && (useExistingRemoteView())) {
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005274 if (mN.contentView == null) {
Julia Reynolds3b848122016-02-26 10:45:32 -05005275 mN.contentView = createContentView();
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005276 mN.extras.putInt(EXTRA_REBUILD_CONTENT_VIEW_ACTION_COUNT,
5277 mN.contentView.getSequenceNumber());
5278 }
5279 if (mN.bigContentView == null) {
Julia Reynolds3b848122016-02-26 10:45:32 -05005280 mN.bigContentView = createBigContentView();
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005281 if (mN.bigContentView != null) {
5282 mN.extras.putInt(EXTRA_REBUILD_BIG_CONTENT_VIEW_ACTION_COUNT,
5283 mN.bigContentView.getSequenceNumber());
5284 }
5285 }
5286 if (mN.headsUpContentView == null) {
Julia Reynolds3b848122016-02-26 10:45:32 -05005287 mN.headsUpContentView = createHeadsUpContentView();
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005288 if (mN.headsUpContentView != null) {
5289 mN.extras.putInt(EXTRA_REBUILD_HEADS_UP_CONTENT_VIEW_ACTION_COUNT,
5290 mN.headsUpContentView.getSequenceNumber());
5291 }
5292 }
5293 }
5294
Julia Reynolds4c0c2022016-02-02 15:11:59 -05005295 if ((mN.defaults & DEFAULT_LIGHTS) != 0) {
5296 mN.flags |= FLAG_SHOW_LIGHTS;
5297 }
5298
Adrian Roosfb921842017-10-26 14:49:56 +02005299 mN.allPendingIntents = null;
5300
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005301 return mN;
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005302 }
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05005303
5304 /**
5305 * Apply this Builder to an existing {@link Notification} object.
5306 *
5307 * @hide
5308 */
5309 public Notification buildInto(Notification n) {
Daniel Sandler1a497d32013-04-18 14:52:45 -04005310 build().cloneInto(n, true);
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05005311 return n;
5312 }
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005313
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005314 /**
Adrian Roos184bfe022016-03-03 13:41:44 -08005315 * Removes RemoteViews that were created for compatibility from {@param n}, if they did not
Julia Reynolds8a3b4592017-06-26 17:15:14 -04005316 * change. Also removes extenders on low ram devices, as
5317 * {@link android.service.notification.NotificationListenerService} services are disabled.
Adrian Roos184bfe022016-03-03 13:41:44 -08005318 *
5319 * @return {@param n}, if no stripping is needed, otherwise a stripped clone of {@param n}.
5320 *
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005321 * @hide
5322 */
Julia Reynolds8a3b4592017-06-26 17:15:14 -04005323 public static Notification maybeCloneStrippedForDelivery(Notification n, boolean isLowRam) {
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005324 String templateClass = n.extras.getString(EXTRA_TEMPLATE);
Adrian Roos184bfe022016-03-03 13:41:44 -08005325
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005326 // Only strip views for known Styles because we won't know how to
5327 // re-create them otherwise.
Julia Reynolds8a3b4592017-06-26 17:15:14 -04005328 if (!isLowRam
5329 && !TextUtils.isEmpty(templateClass)
Adrian Roos184bfe022016-03-03 13:41:44 -08005330 && getNotificationStyleClass(templateClass) == null) {
5331 return n;
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005332 }
Adrian Roos184bfe022016-03-03 13:41:44 -08005333
5334 // Only strip unmodified BuilderRemoteViews.
5335 boolean stripContentView = n.contentView instanceof BuilderRemoteViews &&
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005336 n.extras.getInt(EXTRA_REBUILD_CONTENT_VIEW_ACTION_COUNT, -1) ==
Adrian Roos184bfe022016-03-03 13:41:44 -08005337 n.contentView.getSequenceNumber();
5338 boolean stripBigContentView = n.bigContentView instanceof BuilderRemoteViews &&
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005339 n.extras.getInt(EXTRA_REBUILD_BIG_CONTENT_VIEW_ACTION_COUNT, -1) ==
Adrian Roos184bfe022016-03-03 13:41:44 -08005340 n.bigContentView.getSequenceNumber();
5341 boolean stripHeadsUpContentView = n.headsUpContentView instanceof BuilderRemoteViews &&
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005342 n.extras.getInt(EXTRA_REBUILD_HEADS_UP_CONTENT_VIEW_ACTION_COUNT, -1) ==
Adrian Roos184bfe022016-03-03 13:41:44 -08005343 n.headsUpContentView.getSequenceNumber();
5344
5345 // Nothing to do here, no need to clone.
Julia Reynolds8a3b4592017-06-26 17:15:14 -04005346 if (!isLowRam
5347 && !stripContentView && !stripBigContentView && !stripHeadsUpContentView) {
Adrian Roos184bfe022016-03-03 13:41:44 -08005348 return n;
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005349 }
Adrian Roos184bfe022016-03-03 13:41:44 -08005350
5351 Notification clone = n.clone();
5352 if (stripContentView) {
5353 clone.contentView = null;
5354 clone.extras.remove(EXTRA_REBUILD_CONTENT_VIEW_ACTION_COUNT);
5355 }
5356 if (stripBigContentView) {
5357 clone.bigContentView = null;
5358 clone.extras.remove(EXTRA_REBUILD_BIG_CONTENT_VIEW_ACTION_COUNT);
5359 }
5360 if (stripHeadsUpContentView) {
5361 clone.headsUpContentView = null;
5362 clone.extras.remove(EXTRA_REBUILD_HEADS_UP_CONTENT_VIEW_ACTION_COUNT);
5363 }
Julia Reynolds8a3b4592017-06-26 17:15:14 -04005364 if (isLowRam) {
5365 clone.extras.remove(Notification.TvExtender.EXTRA_TV_EXTENDER);
5366 clone.extras.remove(WearableExtender.EXTRA_WEARABLE_EXTENSIONS);
5367 clone.extras.remove(CarExtender.EXTRA_CAR_EXTENDER);
5368 }
Adrian Roos184bfe022016-03-03 13:41:44 -08005369 return clone;
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005370 }
5371
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005372 private int getBaseLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07005373 return R.layout.notification_template_material_base;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005374 }
5375
5376 private int getBigBaseLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07005377 return R.layout.notification_template_material_big_base;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005378 }
5379
5380 private int getBigPictureLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07005381 return R.layout.notification_template_material_big_picture;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005382 }
5383
5384 private int getBigTextLayoutResource() {
Jorim Jaggi445d3c02014-08-19 22:33:42 +02005385 return R.layout.notification_template_material_big_text;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005386 }
5387
5388 private int getInboxLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07005389 return R.layout.notification_template_material_inbox;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005390 }
5391
Adrian Roosc1a80b02016-04-05 14:54:55 -07005392 private int getMessagingLayoutResource() {
5393 return R.layout.notification_template_material_messaging;
5394 }
5395
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005396 private int getActionLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07005397 return R.layout.notification_material_action;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005398 }
5399
Selim Cinek06e9e1f2016-07-08 17:14:16 -07005400 private int getEmphasizedActionLayoutResource() {
5401 return R.layout.notification_material_action_emphasized;
5402 }
5403
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005404 private int getActionTombstoneLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07005405 return R.layout.notification_material_action_tombstone;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005406 }
Selim Cinek7b9605b2017-01-19 17:36:00 -08005407
5408 private int getBackgroundColor() {
5409 if (isColorized()) {
Selim Cinek5fb73f82017-04-20 16:55:38 -07005410 return mBackgroundColor != COLOR_INVALID ? mBackgroundColor : mN.color;
Selim Cinek7b9605b2017-01-19 17:36:00 -08005411 } else {
Selim Cinekac5f0272017-05-02 16:05:41 -07005412 return mBackgroundColorHint != COLOR_INVALID ? mBackgroundColorHint
5413 : COLOR_DEFAULT;
Selim Cinek7b9605b2017-01-19 17:36:00 -08005414 }
5415 }
5416
5417 private boolean isColorized() {
5418 return mN.isColorized();
5419 }
Selim Cinek99104832017-01-25 14:47:33 -08005420
Anthony Chenad4d1582017-04-10 16:07:58 -07005421 private boolean shouldTintActionButtons() {
5422 return mTintActionButtons;
5423 }
5424
Selim Cinek99104832017-01-25 14:47:33 -08005425 private boolean textColorsNeedInversion() {
5426 if (mStyle == null || !MediaStyle.class.equals(mStyle.getClass())) {
5427 return false;
5428 }
5429 int targetSdkVersion = mContext.getApplicationInfo().targetSdkVersion;
5430 return targetSdkVersion > Build.VERSION_CODES.M
5431 && targetSdkVersion < Build.VERSION_CODES.O;
5432 }
Selim Cinek5fb73f82017-04-20 16:55:38 -07005433
5434 /**
5435 * Set a color palette to be used as the background and textColors
5436 *
5437 * @param backgroundColor the color to be used as the background
5438 * @param foregroundColor the color to be used as the foreground
5439 *
5440 * @hide
5441 */
5442 public void setColorPalette(int backgroundColor, int foregroundColor) {
5443 mBackgroundColor = backgroundColor;
5444 mForegroundColor = foregroundColor;
5445 mTextColorsAreForBackground = COLOR_INVALID;
5446 ensureColors();
5447 }
Selim Cinekac5f0272017-05-02 16:05:41 -07005448
5449 /**
5450 * Sets the background color for this notification to be a different one then the default.
5451 * This is mainly used to calculate contrast and won't necessarily be applied to the
5452 * background.
5453 *
5454 * @hide
5455 */
5456 public void setBackgroundColorHint(int backgroundColor) {
5457 mBackgroundColorHint = backgroundColor;
5458 }
Selim Cineka7679b62017-05-10 16:33:25 -07005459
5460
5461 /**
5462 * Forces all styled remoteViews to be built from scratch and not use any cached
5463 * RemoteViews.
5464 * This is needed for legacy apps that are baking in their remoteviews into the
5465 * notification.
5466 *
5467 * @hide
5468 */
5469 public void setRebuildStyledRemoteViews(boolean rebuild) {
5470 mRebuildStyledRemoteViews = rebuild;
5471 }
Selim Cinek7b9605b2017-01-19 17:36:00 -08005472 }
5473
5474 /**
Selim Cinekd0426622017-07-11 13:19:59 +02005475 * Reduces the image sizes to conform to a maximum allowed size. This also processes all custom
5476 * remote views.
5477 *
5478 * @hide
5479 */
5480 void reduceImageSizes(Context context) {
5481 if (extras.getBoolean(EXTRA_REDUCED_IMAGES)) {
5482 return;
5483 }
Selim Cineka8cb1262017-08-15 16:53:44 -07005484 boolean isLowRam = ActivityManager.isLowRamDeviceStatic();
Selim Cinekd0426622017-07-11 13:19:59 +02005485 if (mLargeIcon != null || largeIcon != null) {
5486 Resources resources = context.getResources();
5487 Class<? extends Style> style = getNotificationStyle();
Selim Cineka8cb1262017-08-15 16:53:44 -07005488 int maxWidth = resources.getDimensionPixelSize(isLowRam
5489 ? R.dimen.notification_right_icon_size_low_ram
5490 : R.dimen.notification_right_icon_size);
Selim Cinekd0426622017-07-11 13:19:59 +02005491 int maxHeight = maxWidth;
5492 if (MediaStyle.class.equals(style)
5493 || DecoratedMediaCustomViewStyle.class.equals(style)) {
Selim Cineka8cb1262017-08-15 16:53:44 -07005494 maxHeight = resources.getDimensionPixelSize(isLowRam
5495 ? R.dimen.notification_media_image_max_height_low_ram
5496 : R.dimen.notification_media_image_max_height);
5497 maxWidth = resources.getDimensionPixelSize(isLowRam
5498 ? R.dimen.notification_media_image_max_width_low_ram
5499 : R.dimen.notification_media_image_max_width);
Selim Cinekd0426622017-07-11 13:19:59 +02005500 }
5501 if (mLargeIcon != null) {
5502 mLargeIcon.scaleDownIfNecessary(maxWidth, maxHeight);
5503 }
5504 if (largeIcon != null) {
5505 largeIcon = Icon.scaleDownIfNecessary(largeIcon, maxWidth, maxHeight);
5506 }
5507 }
Selim Cineka8cb1262017-08-15 16:53:44 -07005508 reduceImageSizesForRemoteView(contentView, context, isLowRam);
5509 reduceImageSizesForRemoteView(headsUpContentView, context, isLowRam);
5510 reduceImageSizesForRemoteView(bigContentView, context, isLowRam);
Selim Cinekd0426622017-07-11 13:19:59 +02005511 extras.putBoolean(EXTRA_REDUCED_IMAGES, true);
5512 }
5513
Selim Cineka8cb1262017-08-15 16:53:44 -07005514 private void reduceImageSizesForRemoteView(RemoteViews remoteView, Context context,
5515 boolean isLowRam) {
Selim Cinekd0426622017-07-11 13:19:59 +02005516 if (remoteView != null) {
5517 Resources resources = context.getResources();
Selim Cineka8cb1262017-08-15 16:53:44 -07005518 int maxWidth = resources.getDimensionPixelSize(isLowRam
5519 ? R.dimen.notification_custom_view_max_image_width_low_ram
5520 : R.dimen.notification_custom_view_max_image_width);
5521 int maxHeight = resources.getDimensionPixelSize(isLowRam
5522 ? R.dimen.notification_custom_view_max_image_height_low_ram
5523 : R.dimen.notification_custom_view_max_image_height);
Selim Cinekd0426622017-07-11 13:19:59 +02005524 remoteView.reduceImageSizes(maxWidth, maxHeight);
5525 }
5526 }
5527
5528 /**
Selim Cinek22714f12017-04-13 16:23:53 -07005529 * @return whether this notification is a foreground service notification
Selim Cinek7b9605b2017-01-19 17:36:00 -08005530 */
Selim Cinek22714f12017-04-13 16:23:53 -07005531 private boolean isForegroundService() {
5532 return (flags & Notification.FLAG_FOREGROUND_SERVICE) != 0;
Selim Cinek7b9605b2017-01-19 17:36:00 -08005533 }
5534
5535 /**
Selim Cinek99104832017-01-25 14:47:33 -08005536 * @return whether this notification has a media session attached
5537 * @hide
5538 */
5539 public boolean hasMediaSession() {
5540 return extras.getParcelable(Notification.EXTRA_MEDIA_SESSION) != null;
5541 }
5542
5543 /**
Selim Cinek7d1009b2017-01-25 15:28:28 -08005544 * @return the style class of this notification
5545 * @hide
5546 */
5547 public Class<? extends Notification.Style> getNotificationStyle() {
5548 String templateClass = extras.getString(Notification.EXTRA_TEMPLATE);
5549
5550 if (!TextUtils.isEmpty(templateClass)) {
5551 return Notification.getNotificationStyleClass(templateClass);
5552 }
5553 return null;
5554 }
5555
5556 /**
Selim Cinek22714f12017-04-13 16:23:53 -07005557 * @return true if this notification is colorized.
Selim Cinek7b9605b2017-01-19 17:36:00 -08005558 *
5559 * @hide
5560 */
5561 public boolean isColorized() {
Selim Cinek5fb73f82017-04-20 16:55:38 -07005562 if (isColorizedMedia()) {
5563 return true;
5564 }
Julia Reynolds4db59552017-06-30 13:34:01 -04005565 return extras.getBoolean(EXTRA_COLORIZED)
5566 && (hasColorizedPermission() || isForegroundService());
5567 }
5568
5569 /**
5570 * Returns whether an app can colorize due to the android.permission.USE_COLORIZED_NOTIFICATIONS
5571 * permission. The permission is checked when a notification is enqueued.
5572 */
5573 private boolean hasColorizedPermission() {
5574 return (flags & Notification.FLAG_CAN_COLORIZE) != 0;
Selim Cinek5fb73f82017-04-20 16:55:38 -07005575 }
5576
5577 /**
5578 * @return true if this notification is colorized and it is a media notification
5579 *
5580 * @hide
5581 */
5582 public boolean isColorizedMedia() {
Selim Cinek99104832017-01-25 14:47:33 -08005583 Class<? extends Style> style = getNotificationStyle();
5584 if (MediaStyle.class.equals(style)) {
5585 Boolean colorized = (Boolean) extras.get(EXTRA_COLORIZED);
5586 if ((colorized == null || colorized) && hasMediaSession()) {
5587 return true;
5588 }
5589 } else if (DecoratedMediaCustomViewStyle.class.equals(style)) {
5590 if (extras.getBoolean(EXTRA_COLORIZED) && hasMediaSession()) {
5591 return true;
5592 }
5593 }
Selim Cinek5fb73f82017-04-20 16:55:38 -07005594 return false;
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005595 }
5596
Selim Cinek0847acd2017-04-24 19:48:29 -07005597
5598 /**
5599 * @return true if this is a media notification
5600 *
5601 * @hide
5602 */
5603 public boolean isMediaNotification() {
5604 Class<? extends Style> style = getNotificationStyle();
5605 if (MediaStyle.class.equals(style)) {
5606 return true;
5607 } else if (DecoratedMediaCustomViewStyle.class.equals(style)) {
5608 return true;
5609 }
5610 return false;
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005611 }
5612
Selim Cinek279fa862016-06-14 10:57:25 -07005613 private boolean hasLargeIcon() {
5614 return mLargeIcon != null || largeIcon != null;
5615 }
5616
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005617 /**
Selim Cinekc2c0b042016-05-18 17:13:46 -07005618 * @return true if the notification will show the time; false otherwise
Selim Cinekb85f36fd2016-04-20 18:46:36 -07005619 * @hide
5620 */
Selim Cinekc2c0b042016-05-18 17:13:46 -07005621 public boolean showsTime() {
Selim Cinekb85f36fd2016-04-20 18:46:36 -07005622 return when != 0 && extras.getBoolean(EXTRA_SHOW_WHEN);
5623 }
5624
5625 /**
Selim Cinekc2c0b042016-05-18 17:13:46 -07005626 * @return true if the notification will show a chronometer; false otherwise
5627 * @hide
5628 */
5629 public boolean showsChronometer() {
5630 return when != 0 && extras.getBoolean(EXTRA_SHOW_CHRONOMETER);
5631 }
5632
5633 /**
Julia Reynolds7ca33072017-06-29 13:58:24 -04005634 * @removed
Julia Reynolds4a02afb2016-12-13 13:39:52 -05005635 */
5636 @SystemApi
5637 public static Class<? extends Style> getNotificationStyleClass(String templateClass) {
5638 Class<? extends Style>[] classes = new Class[] {
5639 BigTextStyle.class, BigPictureStyle.class, InboxStyle.class, MediaStyle.class,
5640 DecoratedCustomViewStyle.class, DecoratedMediaCustomViewStyle.class,
5641 MessagingStyle.class };
5642 for (Class<? extends Style> innerClass : classes) {
5643 if (templateClass.equals(innerClass.getName())) {
5644 return innerClass;
5645 }
5646 }
5647 return null;
5648 }
5649
5650 /**
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005651 * An object that can apply a rich notification style to a {@link Notification.Builder}
5652 * object.
5653 */
Griff Hazendfcb0802014-02-11 12:00:00 -08005654 public static abstract class Style {
Chris Wrend6297db2012-05-03 16:20:13 -04005655 private CharSequence mBigContentTitle;
Jorim Jaggi457a10d2014-09-08 16:18:23 +02005656
5657 /**
5658 * @hide
5659 */
5660 protected CharSequence mSummaryText = null;
5661
5662 /**
5663 * @hide
5664 */
5665 protected boolean mSummaryTextSet = false;
Chris Wrend6297db2012-05-03 16:20:13 -04005666
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005667 protected Builder mBuilder;
5668
Chris Wrend6297db2012-05-03 16:20:13 -04005669 /**
5670 * Overrides ContentTitle in the big form of the template.
5671 * This defaults to the value passed to setContentTitle().
5672 */
5673 protected void internalSetBigContentTitle(CharSequence title) {
5674 mBigContentTitle = title;
5675 }
5676
5677 /**
5678 * Set the first line of text after the detail section in the big form of the template.
5679 */
5680 protected void internalSetSummaryText(CharSequence cs) {
5681 mSummaryText = cs;
Daniel Sandler619738c2012-06-07 16:33:08 -04005682 mSummaryTextSet = true;
Chris Wrend6297db2012-05-03 16:20:13 -04005683 }
5684
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005685 public void setBuilder(Builder builder) {
5686 if (mBuilder != builder) {
5687 mBuilder = builder;
Daniel Sandlerc08dea22012-06-28 08:35:24 -07005688 if (mBuilder != null) {
5689 mBuilder.setStyle(this);
5690 }
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005691 }
5692 }
5693
Chris Wrend6297db2012-05-03 16:20:13 -04005694 protected void checkBuilder() {
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005695 if (mBuilder == null) {
5696 throw new IllegalArgumentException("Style requires a valid Builder object");
5697 }
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005698 }
Chris Wrend6297db2012-05-03 16:20:13 -04005699
5700 protected RemoteViews getStandardView(int layoutId) {
5701 checkBuilder();
5702
Christoph Studer4600f9b2014-07-22 22:44:43 +02005703 // Nasty.
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005704 CharSequence oldBuilderContentTitle =
5705 mBuilder.getAllExtras().getCharSequence(EXTRA_TITLE);
Chris Wrend6297db2012-05-03 16:20:13 -04005706 if (mBigContentTitle != null) {
5707 mBuilder.setContentTitle(mBigContentTitle);
5708 }
5709
Chris Wrend6297db2012-05-03 16:20:13 -04005710 RemoteViews contentView = mBuilder.applyStandardTemplateWithActions(layoutId);
5711
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005712 mBuilder.getAllExtras().putCharSequence(EXTRA_TITLE, oldBuilderContentTitle);
Christoph Studer4600f9b2014-07-22 22:44:43 +02005713
Chris Wrend6297db2012-05-03 16:20:13 -04005714 if (mBigContentTitle != null && mBigContentTitle.equals("")) {
5715 contentView.setViewVisibility(R.id.line1, View.GONE);
Chris Wren67dc9a02012-05-16 01:03:20 -04005716 } else {
5717 contentView.setViewVisibility(R.id.line1, View.VISIBLE);
Chris Wrend6297db2012-05-03 16:20:13 -04005718 }
5719
Chris Wrend6297db2012-05-03 16:20:13 -04005720 return contentView;
5721 }
5722
Daniel Sandlerf45564e2013-04-15 15:05:08 -04005723 /**
Selim Cinek7d1009b2017-01-25 15:28:28 -08005724 * Construct a Style-specific RemoteViews for the collapsed notification layout.
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005725 * The default implementation has nothing additional to add.
Selim Cinek7d1009b2017-01-25 15:28:28 -08005726 *
5727 * @param increasedHeight true if this layout be created with an increased height.
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005728 * @hide
5729 */
Selim Cinek7d1009b2017-01-25 15:28:28 -08005730 public RemoteViews makeContentView(boolean increasedHeight) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005731 return null;
5732 }
5733
5734 /**
5735 * Construct a Style-specific RemoteViews for the final big notification layout.
5736 * @hide
5737 */
5738 public RemoteViews makeBigContentView() {
5739 return null;
5740 }
5741
5742 /**
5743 * Construct a Style-specific RemoteViews for the final HUN layout.
Selim Cinek87ed69b2017-02-09 15:59:43 -08005744 *
5745 * @param increasedHeight true if this layout be created with an increased height.
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005746 * @hide
5747 */
Selim Cinek87ed69b2017-02-09 15:59:43 -08005748 public RemoteViews makeHeadsUpContentView(boolean increasedHeight) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005749 return null;
5750 }
5751
5752 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005753 * Apply any style-specific extras to this notification before shipping it out.
Daniel Sandlerf45564e2013-04-15 15:05:08 -04005754 * @hide
5755 */
5756 public void addExtras(Bundle extras) {
5757 if (mSummaryTextSet) {
5758 extras.putCharSequence(EXTRA_SUMMARY_TEXT, mSummaryText);
5759 }
5760 if (mBigContentTitle != null) {
5761 extras.putCharSequence(EXTRA_TITLE_BIG, mBigContentTitle);
5762 }
Chris Wren91ad5632013-06-05 15:05:57 -04005763 extras.putString(EXTRA_TEMPLATE, this.getClass().getName());
Daniel Sandlerf45564e2013-04-15 15:05:08 -04005764 }
5765
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04005766 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005767 * Reconstruct the internal state of this Style object from extras.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04005768 * @hide
5769 */
Christoph Studer4600f9b2014-07-22 22:44:43 +02005770 protected void restoreFromExtras(Bundle extras) {
5771 if (extras.containsKey(EXTRA_SUMMARY_TEXT)) {
5772 mSummaryText = extras.getCharSequence(EXTRA_SUMMARY_TEXT);
5773 mSummaryTextSet = true;
5774 }
5775 if (extras.containsKey(EXTRA_TITLE_BIG)) {
5776 mBigContentTitle = extras.getCharSequence(EXTRA_TITLE_BIG);
5777 }
5778 }
5779
5780
5781 /**
5782 * @hide
5783 */
5784 public Notification buildStyled(Notification wip) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005785 addExtras(wip.extras);
Christoph Studer4600f9b2014-07-22 22:44:43 +02005786 return wip;
5787 }
5788
Daniel Sandler0ec46202015-06-24 01:27:05 -04005789 /**
5790 * @hide
5791 */
Jorim Jaggia0d58ae2015-06-03 11:48:13 -07005792 public void purgeResources() {}
5793
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04005794 /**
5795 * Calls {@link android.app.Notification.Builder#build()} on the Builder this Style is
5796 * attached to.
5797 *
5798 * @return the fully constructed Notification.
5799 */
5800 public Notification build() {
5801 checkBuilder();
5802 return mBuilder.build();
5803 }
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02005804
5805 /**
5806 * @hide
5807 * @return true if the style positions the progress bar on the second line; false if the
5808 * style hides the progress bar
5809 */
5810 protected boolean hasProgress() {
5811 return true;
5812 }
Selim Cinek03d0d652015-11-13 13:18:09 -05005813
5814 /**
5815 * @hide
5816 * @return Whether we should put the summary be put into the notification header
5817 */
5818 public boolean hasSummaryInHeader() {
5819 return true;
5820 }
Selim Cinek593610c2016-02-16 18:42:57 -08005821
5822 /**
5823 * @hide
5824 * @return Whether custom content views are displayed inline in the style
5825 */
5826 public boolean displayCustomViewInline() {
5827 return false;
5828 }
Selim Cinekd0426622017-07-11 13:19:59 +02005829
5830 /**
5831 * Reduces the image sizes contained in this style.
5832 *
5833 * @hide
5834 */
5835 public void reduceImageSizes(Context context) {
5836 }
Selim Cinek90343862018-02-01 11:07:11 -08005837
5838 /**
5839 * Validate that this style was properly composed. This is called at build time.
5840 * @hide
5841 */
5842 public void validate(Context context) {
5843 }
Joe Onorato46439ce2010-11-19 13:56:21 -08005844 }
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005845
5846 /**
Daniel Sandler4dfbe832012-04-11 14:51:46 -04005847 * Helper class for generating large-format notifications that include a large image attachment.
Joe Malin8d40d042012-11-05 11:36:40 -08005848 *
Robert Ly91c5ce32014-06-08 15:37:00 -07005849 * Here's how you'd set the <code>BigPictureStyle</code> on a notification:
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005850 * <pre class="prettyprint">
Robert Ly91c5ce32014-06-08 15:37:00 -07005851 * Notification notif = new Notification.Builder(mContext)
5852 * .setContentTitle(&quot;New photo from &quot; + sender.toString())
5853 * .setContentText(subject)
5854 * .setSmallIcon(R.drawable.new_post)
5855 * .setLargeIcon(aBitmap)
5856 * .setStyle(new Notification.BigPictureStyle()
5857 * .bigPicture(aBigBitmap))
5858 * .build();
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005859 * </pre>
Joe Malin8d40d042012-11-05 11:36:40 -08005860 *
Daniel Sandler4dfbe832012-04-11 14:51:46 -04005861 * @see Notification#bigContentView
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005862 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005863 public static class BigPictureStyle extends Style {
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005864 private Bitmap mPicture;
Dan Sandlerd63f9322015-05-06 15:18:49 -04005865 private Icon mBigLargeIcon;
Chris Wren3745a3d2012-05-22 15:11:52 -04005866 private boolean mBigLargeIconSet = false;
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005867
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005868 public BigPictureStyle() {
5869 }
5870
Adrian Roosf5faf9d2016-05-23 13:56:15 -07005871 /**
5872 * @deprecated use {@code BigPictureStyle()}.
5873 */
5874 @Deprecated
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005875 public BigPictureStyle(Builder builder) {
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005876 setBuilder(builder);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005877 }
5878
Chris Wrend6297db2012-05-03 16:20:13 -04005879 /**
5880 * Overrides ContentTitle in the big form of the template.
5881 * This defaults to the value passed to setContentTitle().
5882 */
5883 public BigPictureStyle setBigContentTitle(CharSequence title) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04005884 internalSetBigContentTitle(safeCharSequence(title));
Chris Wrend6297db2012-05-03 16:20:13 -04005885 return this;
5886 }
5887
5888 /**
5889 * Set the first line of text after the detail section in the big form of the template.
5890 */
5891 public BigPictureStyle setSummaryText(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04005892 internalSetSummaryText(safeCharSequence(cs));
Chris Wrend6297db2012-05-03 16:20:13 -04005893 return this;
5894 }
5895
Chris Wren0bd664d2012-08-01 13:56:56 -04005896 /**
5897 * Provide the bitmap to be used as the payload for the BigPicture notification.
5898 */
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005899 public BigPictureStyle bigPicture(Bitmap b) {
5900 mPicture = b;
5901 return this;
5902 }
5903
Chris Wren3745a3d2012-05-22 15:11:52 -04005904 /**
Chris Wren3745a3d2012-05-22 15:11:52 -04005905 * Override the large icon when the big notification is shown.
5906 */
5907 public BigPictureStyle bigLargeIcon(Bitmap b) {
Dan Sandlerd63f9322015-05-06 15:18:49 -04005908 return bigLargeIcon(b != null ? Icon.createWithBitmap(b) : null);
5909 }
5910
5911 /**
5912 * Override the large icon when the big notification is shown.
5913 */
5914 public BigPictureStyle bigLargeIcon(Icon icon) {
Chris Wren3745a3d2012-05-22 15:11:52 -04005915 mBigLargeIconSet = true;
Dan Sandlerd63f9322015-05-06 15:18:49 -04005916 mBigLargeIcon = icon;
Chris Wren3745a3d2012-05-22 15:11:52 -04005917 return this;
5918 }
5919
Riley Andrews0394a0c2015-11-03 23:36:52 -08005920 /** @hide */
5921 public static final int MIN_ASHMEM_BITMAP_SIZE = 128 * (1 << 10);
5922
Daniel Sandler0ec46202015-06-24 01:27:05 -04005923 /**
5924 * @hide
5925 */
Jorim Jaggia0d58ae2015-06-03 11:48:13 -07005926 @Override
5927 public void purgeResources() {
5928 super.purgeResources();
Riley Andrews8cee7c12015-11-01 23:36:04 -08005929 if (mPicture != null &&
5930 mPicture.isMutable() &&
Riley Andrews0394a0c2015-11-03 23:36:52 -08005931 mPicture.getAllocationByteCount() >= MIN_ASHMEM_BITMAP_SIZE) {
Jorim Jaggia0d58ae2015-06-03 11:48:13 -07005932 mPicture = mPicture.createAshmemBitmap();
5933 }
5934 if (mBigLargeIcon != null) {
5935 mBigLargeIcon.convertToAshmem();
5936 }
5937 }
Christoph Studer5c510ee2014-12-15 16:32:27 +01005938
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005939 /**
5940 * @hide
5941 */
Selim Cinekd0426622017-07-11 13:19:59 +02005942 @Override
5943 public void reduceImageSizes(Context context) {
5944 super.reduceImageSizes(context);
5945 Resources resources = context.getResources();
Selim Cineka8cb1262017-08-15 16:53:44 -07005946 boolean isLowRam = ActivityManager.isLowRamDeviceStatic();
Selim Cinekd0426622017-07-11 13:19:59 +02005947 if (mPicture != null) {
Selim Cineka8cb1262017-08-15 16:53:44 -07005948 int maxPictureWidth = resources.getDimensionPixelSize(isLowRam
5949 ? R.dimen.notification_big_picture_max_height_low_ram
5950 : R.dimen.notification_big_picture_max_height);
5951 int maxPictureHeight = resources.getDimensionPixelSize(isLowRam
5952 ? R.dimen.notification_big_picture_max_width_low_ram
5953 : R.dimen.notification_big_picture_max_width);
Selim Cinekd0426622017-07-11 13:19:59 +02005954 mPicture = Icon.scaleDownIfNecessary(mPicture, maxPictureWidth, maxPictureHeight);
5955 }
5956 if (mBigLargeIcon != null) {
Selim Cineka8cb1262017-08-15 16:53:44 -07005957 int rightIconSize = resources.getDimensionPixelSize(isLowRam
5958 ? R.dimen.notification_right_icon_size_low_ram
5959 : R.dimen.notification_right_icon_size);
Selim Cinekd0426622017-07-11 13:19:59 +02005960 mBigLargeIcon.scaleDownIfNecessary(rightIconSize, rightIconSize);
5961 }
5962 }
5963
5964 /**
5965 * @hide
5966 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005967 public RemoteViews makeBigContentView() {
5968 // Replace mN.mLargeIcon with mBigLargeIcon if mBigLargeIconSet
Christoph Studer5c510ee2014-12-15 16:32:27 +01005969 // This covers the following cases:
5970 // 1. mBigLargeIconSet -> mBigLargeIcon (null or non-null) applies, overrides
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005971 // mN.mLargeIcon
5972 // 2. !mBigLargeIconSet -> mN.mLargeIcon applies
Dan Sandlerd63f9322015-05-06 15:18:49 -04005973 Icon oldLargeIcon = null;
Selim Cineke99acb22016-08-04 12:55:48 -07005974 Bitmap largeIconLegacy = null;
Christoph Studer5c510ee2014-12-15 16:32:27 +01005975 if (mBigLargeIconSet) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005976 oldLargeIcon = mBuilder.mN.mLargeIcon;
5977 mBuilder.mN.mLargeIcon = mBigLargeIcon;
Selim Cineke99acb22016-08-04 12:55:48 -07005978 // The legacy largeIcon might not allow us to clear the image, as it's taken in
5979 // replacement if the other one is null. Because we're restoring these legacy icons
5980 // for old listeners, this is in general non-null.
5981 largeIconLegacy = mBuilder.mN.largeIcon;
5982 mBuilder.mN.largeIcon = null;
Christoph Studer5c510ee2014-12-15 16:32:27 +01005983 }
5984
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005985 RemoteViews contentView = getStandardView(mBuilder.getBigPictureLayoutResource());
Selim Cinek03d0d652015-11-13 13:18:09 -05005986 if (mSummaryTextSet) {
Selim Cinek48f66b72017-08-18 16:17:51 -07005987 contentView.setTextViewText(R.id.text, mBuilder.processTextSpans(
5988 mBuilder.processLegacyText(mSummaryText)));
Selim Cinek7b9605b2017-01-19 17:36:00 -08005989 mBuilder.setTextViewColorSecondary(contentView, R.id.text);
Selim Cinekc848c3a2016-01-13 15:27:30 -08005990 contentView.setViewVisibility(R.id.text, View.VISIBLE);
Selim Cinek03d0d652015-11-13 13:18:09 -05005991 }
Selim Cinek279fa862016-06-14 10:57:25 -07005992 mBuilder.setContentMinHeight(contentView, mBuilder.mN.hasLargeIcon());
Selim Cinek53e64a42015-11-16 10:40:56 -08005993
Christoph Studer5c510ee2014-12-15 16:32:27 +01005994 if (mBigLargeIconSet) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005995 mBuilder.mN.mLargeIcon = oldLargeIcon;
Selim Cineke99acb22016-08-04 12:55:48 -07005996 mBuilder.mN.largeIcon = largeIconLegacy;
Christoph Studer5c510ee2014-12-15 16:32:27 +01005997 }
5998
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005999 contentView.setImageViewBitmap(R.id.big_picture, mPicture);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006000 return contentView;
6001 }
6002
Daniel Sandlerf45564e2013-04-15 15:05:08 -04006003 /**
6004 * @hide
6005 */
6006 public void addExtras(Bundle extras) {
6007 super.addExtras(extras);
6008
6009 if (mBigLargeIconSet) {
6010 extras.putParcelable(EXTRA_LARGE_ICON_BIG, mBigLargeIcon);
6011 }
6012 extras.putParcelable(EXTRA_PICTURE, mPicture);
6013 }
6014
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04006015 /**
6016 * @hide
6017 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006018 @Override
Christoph Studer4600f9b2014-07-22 22:44:43 +02006019 protected void restoreFromExtras(Bundle extras) {
6020 super.restoreFromExtras(extras);
6021
6022 if (extras.containsKey(EXTRA_LARGE_ICON_BIG)) {
Christoph Studer5c510ee2014-12-15 16:32:27 +01006023 mBigLargeIconSet = true;
Christoph Studer4600f9b2014-07-22 22:44:43 +02006024 mBigLargeIcon = extras.getParcelable(EXTRA_LARGE_ICON_BIG);
Chris Wren3745a3d2012-05-22 15:11:52 -04006025 }
Christoph Studer4600f9b2014-07-22 22:44:43 +02006026 mPicture = extras.getParcelable(EXTRA_PICTURE);
6027 }
Selim Cinek03d0d652015-11-13 13:18:09 -05006028
6029 /**
6030 * @hide
6031 */
6032 @Override
6033 public boolean hasSummaryInHeader() {
6034 return false;
6035 }
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006036 }
6037
6038 /**
Daniel Sandler4dfbe832012-04-11 14:51:46 -04006039 * Helper class for generating large-format notifications that include a lot of text.
Joe Malin8d40d042012-11-05 11:36:40 -08006040 *
Robert Ly91c5ce32014-06-08 15:37:00 -07006041 * Here's how you'd set the <code>BigTextStyle</code> on a notification:
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006042 * <pre class="prettyprint">
Robert Ly91c5ce32014-06-08 15:37:00 -07006043 * Notification notif = new Notification.Builder(mContext)
6044 * .setContentTitle(&quot;New mail from &quot; + sender.toString())
6045 * .setContentText(subject)
6046 * .setSmallIcon(R.drawable.new_mail)
6047 * .setLargeIcon(aBitmap)
6048 * .setStyle(new Notification.BigTextStyle()
6049 * .bigText(aVeryLongString))
6050 * .build();
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006051 * </pre>
Joe Malin8d40d042012-11-05 11:36:40 -08006052 *
Daniel Sandler4dfbe832012-04-11 14:51:46 -04006053 * @see Notification#bigContentView
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006054 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006055 public static class BigTextStyle extends Style {
Jorim Jaggi457a10d2014-09-08 16:18:23 +02006056
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006057 private CharSequence mBigText;
6058
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006059 public BigTextStyle() {
6060 }
6061
Adrian Roosf5faf9d2016-05-23 13:56:15 -07006062 /**
6063 * @deprecated use {@code BigTextStyle()}.
6064 */
6065 @Deprecated
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006066 public BigTextStyle(Builder builder) {
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006067 setBuilder(builder);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006068 }
6069
Chris Wrend6297db2012-05-03 16:20:13 -04006070 /**
6071 * Overrides ContentTitle in the big form of the template.
6072 * This defaults to the value passed to setContentTitle().
6073 */
6074 public BigTextStyle setBigContentTitle(CharSequence title) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04006075 internalSetBigContentTitle(safeCharSequence(title));
Chris Wrend6297db2012-05-03 16:20:13 -04006076 return this;
6077 }
6078
6079 /**
6080 * Set the first line of text after the detail section in the big form of the template.
6081 */
6082 public BigTextStyle setSummaryText(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04006083 internalSetSummaryText(safeCharSequence(cs));
Chris Wrend6297db2012-05-03 16:20:13 -04006084 return this;
6085 }
6086
Chris Wren0bd664d2012-08-01 13:56:56 -04006087 /**
6088 * Provide the longer text to be displayed in the big form of the
6089 * template in place of the content text.
6090 */
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006091 public BigTextStyle bigText(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04006092 mBigText = safeCharSequence(cs);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006093 return this;
6094 }
6095
Daniel Sandlerf45564e2013-04-15 15:05:08 -04006096 /**
6097 * @hide
6098 */
6099 public void addExtras(Bundle extras) {
6100 super.addExtras(extras);
6101
Christoph Studer4600f9b2014-07-22 22:44:43 +02006102 extras.putCharSequence(EXTRA_BIG_TEXT, mBigText);
6103 }
6104
6105 /**
6106 * @hide
6107 */
6108 @Override
6109 protected void restoreFromExtras(Bundle extras) {
6110 super.restoreFromExtras(extras);
6111
6112 mBigText = extras.getCharSequence(EXTRA_BIG_TEXT);
Daniel Sandlerf45564e2013-04-15 15:05:08 -04006113 }
6114
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006115 /**
Selim Cinek7d1009b2017-01-25 15:28:28 -08006116 * @param increasedHeight true if this layout be created with an increased height.
6117 *
6118 * @hide
6119 */
6120 @Override
6121 public RemoteViews makeContentView(boolean increasedHeight) {
6122 if (increasedHeight) {
Selim Cinek5d6ef8d2017-05-18 22:16:00 -07006123 mBuilder.mOriginalActions = mBuilder.mActions;
Selim Cinek7d1009b2017-01-25 15:28:28 -08006124 mBuilder.mActions = new ArrayList<>();
6125 RemoteViews remoteViews = makeBigContentView();
Selim Cinek5d6ef8d2017-05-18 22:16:00 -07006126 mBuilder.mActions = mBuilder.mOriginalActions;
6127 mBuilder.mOriginalActions = null;
Selim Cinek7d1009b2017-01-25 15:28:28 -08006128 return remoteViews;
6129 }
6130 return super.makeContentView(increasedHeight);
6131 }
6132
6133 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006134 * @hide
6135 */
Selim Cinek87ed69b2017-02-09 15:59:43 -08006136 @Override
6137 public RemoteViews makeHeadsUpContentView(boolean increasedHeight) {
6138 if (increasedHeight && mBuilder.mActions.size() > 0) {
6139 return makeBigContentView();
6140 }
6141 return super.makeHeadsUpContentView(increasedHeight);
6142 }
6143
6144 /**
6145 * @hide
6146 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006147 public RemoteViews makeBigContentView() {
Christoph Studer4600f9b2014-07-22 22:44:43 +02006148
6149 // Nasty
Selim Cinek75998782016-04-26 10:39:17 -07006150 CharSequence text = mBuilder.getAllExtras().getCharSequence(EXTRA_TEXT);
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006151 mBuilder.getAllExtras().putCharSequence(EXTRA_TEXT, null);
Daniel Sandler916ad912012-06-13 12:17:07 -04006152
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01006153 RemoteViews contentView = getStandardView(mBuilder.getBigTextLayoutResource());
Joe Malin8d40d042012-11-05 11:36:40 -08006154
Selim Cinek75998782016-04-26 10:39:17 -07006155 mBuilder.getAllExtras().putCharSequence(EXTRA_TEXT, text);
Christoph Studer4600f9b2014-07-22 22:44:43 +02006156
Selim Cinek3a2c4b92015-12-17 17:01:17 -08006157 CharSequence bigTextText = mBuilder.processLegacyText(mBigText);
Selim Cinek75998782016-04-26 10:39:17 -07006158 if (TextUtils.isEmpty(bigTextText)) {
6159 // In case the bigtext is null / empty fall back to the normal text to avoid a weird
6160 // experience
6161 bigTextText = mBuilder.processLegacyText(text);
6162 }
Adrian Roosb1f427c2016-05-26 12:27:15 -07006163 applyBigTextContentView(mBuilder, contentView, bigTextText);
Selim Cinek4fb12d32015-11-19 18:10:48 -08006164
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006165 return contentView;
6166 }
6167
Adrian Roosb1f427c2016-05-26 12:27:15 -07006168 static void applyBigTextContentView(Builder builder,
6169 RemoteViews contentView, CharSequence bigTextText) {
Selim Cinek48f66b72017-08-18 16:17:51 -07006170 contentView.setTextViewText(R.id.big_text, builder.processTextSpans(bigTextText));
Selim Cinek7b9605b2017-01-19 17:36:00 -08006171 builder.setTextViewColorSecondary(contentView, R.id.big_text);
Adrian Roosb1f427c2016-05-26 12:27:15 -07006172 contentView.setViewVisibility(R.id.big_text,
6173 TextUtils.isEmpty(bigTextText) ? View.GONE : View.VISIBLE);
Selim Cinek279fa862016-06-14 10:57:25 -07006174 contentView.setBoolean(R.id.big_text, "setHasImage", builder.mN.hasLargeIcon());
Adrian Roosb1f427c2016-05-26 12:27:15 -07006175 }
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006176 }
Daniel Sandler879c5e02012-04-17 16:46:51 -04006177
6178 /**
Alex Hillsfc737de2016-03-23 17:33:02 -04006179 * Helper class for generating large-format notifications that include multiple back-and-forth
6180 * messages of varying types between any number of people.
6181 *
6182 * <br>
6183 * If the platform does not provide large-format notifications, this method has no effect. The
6184 * user will always see the normal notification view.
6185 * <br>
6186 * This class is a "rebuilder": It attaches to a Builder object and modifies its behavior, like
6187 * so:
6188 * <pre class="prettyprint">
6189 *
6190 * Notification noti = new Notification.Builder()
6191 * .setContentTitle(&quot;2 new messages wtih &quot; + sender.toString())
6192 * .setContentText(subject)
6193 * .setSmallIcon(R.drawable.new_message)
6194 * .setLargeIcon(aBitmap)
6195 * .setStyle(new Notification.MessagingStyle(resources.getString(R.string.reply_name))
6196 * .addMessage(messages[0].getText(), messages[0].getTime(), messages[0].getSender())
6197 * .addMessage(messages[1].getText(), messages[1].getTime(), messages[1].getSender()))
6198 * .build();
6199 * </pre>
6200 */
6201 public static class MessagingStyle extends Style {
6202
6203 /**
6204 * The maximum number of messages that will be retained in the Notification itself (the
6205 * number displayed is up to the platform).
6206 */
6207 public static final int MAXIMUM_RETAINED_MESSAGES = 25;
6208
Selim Cinekcb8b9852017-12-15 18:01:52 -08006209 @NonNull Person mUser;
Kodlee Yin9ac617c2017-12-19 11:20:50 -08006210 @Nullable CharSequence mConversationTitle;
Alex Hillsd9b04d92016-04-11 16:38:16 -04006211 List<Message> mMessages = new ArrayList<>();
Adrian Roos437cd562017-01-18 15:47:03 -08006212 List<Message> mHistoricMessages = new ArrayList<>();
Kodlee Yin9ac617c2017-12-19 11:20:50 -08006213 boolean mIsGroupConversation;
Alex Hillsfc737de2016-03-23 17:33:02 -04006214
6215 MessagingStyle() {
6216 }
6217
6218 /**
Alex Hillsfd590442016-10-07 09:52:44 -04006219 * @param userDisplayName Required - the name to be displayed for any replies sent by the
6220 * user before the posting app reposts the notification with those messages after they've
6221 * been actually sent and in previous messages sent by the user added in
Alex Hillsfc737de2016-03-23 17:33:02 -04006222 * {@link #addMessage(Notification.MessagingStyle.Message)}
Selim Cinekcb8b9852017-12-15 18:01:52 -08006223 *
6224 * @deprecated use {@code MessagingStyle(Person)}
Alex Hillsfc737de2016-03-23 17:33:02 -04006225 */
Alex Hillsfd590442016-10-07 09:52:44 -04006226 public MessagingStyle(@NonNull CharSequence userDisplayName) {
Selim Cinekcb8b9852017-12-15 18:01:52 -08006227 this(new Person().setName(userDisplayName));
6228 }
6229
6230 /**
6231 * @param user Required - The person displayed for any messages that are sent by the
6232 * user. Any messages added with {@link #addMessage(Notification.MessagingStyle.Message)}
6233 * who don't have a Person associated with it will be displayed as if they were sent
Selim Cinek90343862018-02-01 11:07:11 -08006234 * by this user. The user also needs to have a valid name associated with it, which will
6235 * be enforced starting in Android P.
Selim Cinekcb8b9852017-12-15 18:01:52 -08006236 */
6237 public MessagingStyle(@NonNull Person user) {
6238 mUser = user;
Selim Cinek90343862018-02-01 11:07:11 -08006239 }
6240
6241 /**
6242 * Validate that this style was properly composed. This is called at build time.
6243 * @hide
6244 */
6245 @Override
6246 public void validate(Context context) {
6247 super.validate(context);
6248 if (context.getApplicationInfo().targetSdkVersion
6249 >= Build.VERSION_CODES.P && (mUser == null || mUser.getName() == null)) {
6250 throw new RuntimeException("User must be valid and have a name.");
Selim Cinekcb8b9852017-12-15 18:01:52 -08006251 }
6252 }
6253
6254 /**
6255 * @return the user to be displayed for any replies sent by the user
6256 */
6257 public Person getUser() {
6258 return mUser;
Alex Hillsfc737de2016-03-23 17:33:02 -04006259 }
6260
6261 /**
6262 * Returns the name to be displayed for any replies sent by the user
Selim Cinekcb8b9852017-12-15 18:01:52 -08006263 *
6264 * @deprecated use {@link #getUser()} instead
Alex Hillsfc737de2016-03-23 17:33:02 -04006265 */
6266 public CharSequence getUserDisplayName() {
Selim Cinekcb8b9852017-12-15 18:01:52 -08006267 return mUser.getName();
Alex Hillsfc737de2016-03-23 17:33:02 -04006268 }
6269
6270 /**
Kodlee Yin9ac617c2017-12-19 11:20:50 -08006271 * Sets the title to be displayed on this conversation. May be set to {@code null}.
6272 *
Kodlee Yin14656422017-12-22 17:00:46 -08006273 * <p>This API's behavior was changed in SDK version {@link Build.VERSION_CODES#P}. If your
6274 * application's target version is less than {@link Build.VERSION_CODES#P}, setting a
6275 * conversation title to a non-null value will make {@link #isGroupConversation()} return
6276 * {@code true} and passing {@code null} will make it return {@code false}. In
6277 * {@link Build.VERSION_CODES#P} and beyond, use {@link #setGroupConversation(boolean)}
6278 * to set group conversation status.
6279 *
6280 * @param conversationTitle Title displayed for this conversation
6281 * @return this object for method chaining
Alex Hillsfc737de2016-03-23 17:33:02 -04006282 */
Kodlee Yin9ac617c2017-12-19 11:20:50 -08006283 public MessagingStyle setConversationTitle(@Nullable CharSequence conversationTitle) {
Alex Hillsfc737de2016-03-23 17:33:02 -04006284 mConversationTitle = conversationTitle;
6285 return this;
6286 }
6287
6288 /**
Kodlee Yin9ac617c2017-12-19 11:20:50 -08006289 * Return the title to be displayed on this conversation. May return {@code null}.
Alex Hillsfc737de2016-03-23 17:33:02 -04006290 */
Kodlee Yin9ac617c2017-12-19 11:20:50 -08006291 @Nullable
Alex Hillsfc737de2016-03-23 17:33:02 -04006292 public CharSequence getConversationTitle() {
6293 return mConversationTitle;
6294 }
6295
6296 /**
6297 * Adds a message for display by this notification. Convenience call for a simple
6298 * {@link Message} in {@link #addMessage(Notification.MessagingStyle.Message)}.
6299 * @param text A {@link CharSequence} to be displayed as the message content
6300 * @param timestamp Time at which the message arrived
6301 * @param sender A {@link CharSequence} to be used for displaying the name of the
6302 * sender. Should be <code>null</code> for messages by the current user, in which case
6303 * the platform will insert {@link #getUserDisplayName()}.
6304 * Should be unique amongst all individuals in the conversation, and should be
6305 * consistent during re-posts of the notification.
6306 *
6307 * @see Message#Message(CharSequence, long, CharSequence)
6308 *
6309 * @return this object for method chaining
Selim Cinekcb8b9852017-12-15 18:01:52 -08006310 *
6311 * @deprecated use {@link #addMessage(CharSequence, long, Person)}
Alex Hillsfc737de2016-03-23 17:33:02 -04006312 */
6313 public MessagingStyle addMessage(CharSequence text, long timestamp, CharSequence sender) {
Selim Cinekcb8b9852017-12-15 18:01:52 -08006314 return addMessage(text, timestamp,
6315 sender == null ? null : new Person().setName(sender));
6316 }
6317
6318 /**
6319 * Adds a message for display by this notification. Convenience call for a simple
6320 * {@link Message} in {@link #addMessage(Notification.MessagingStyle.Message)}.
6321 * @param text A {@link CharSequence} to be displayed as the message content
6322 * @param timestamp Time at which the message arrived
6323 * @param sender The {@link Person} who sent the message.
6324 * Should be <code>null</code> for messages by the current user, in which case
6325 * the platform will insert the user set in {@code MessagingStyle(Person)}.
6326 *
6327 * @see Message#Message(CharSequence, long, CharSequence)
6328 *
6329 * @return this object for method chaining
6330 */
6331 public MessagingStyle addMessage(CharSequence text, long timestamp, Person sender) {
Adrian Roos437cd562017-01-18 15:47:03 -08006332 return addMessage(new Message(text, timestamp, sender));
Alex Hillsfc737de2016-03-23 17:33:02 -04006333 }
6334
6335 /**
6336 * Adds a {@link Message} for display in this notification.
Adrian Roos437cd562017-01-18 15:47:03 -08006337 *
6338 * <p>The messages should be added in chronologic order, i.e. the oldest first,
6339 * the newest last.
6340 *
Alex Hillsfc737de2016-03-23 17:33:02 -04006341 * @param message The {@link Message} to be displayed
6342 * @return this object for method chaining
6343 */
6344 public MessagingStyle addMessage(Message message) {
6345 mMessages.add(message);
6346 if (mMessages.size() > MAXIMUM_RETAINED_MESSAGES) {
6347 mMessages.remove(0);
6348 }
6349 return this;
6350 }
6351
6352 /**
Adrian Roos437cd562017-01-18 15:47:03 -08006353 * Adds a {@link Message} for historic context in this notification.
6354 *
6355 * <p>Messages should be added as historic if they are not the main subject of the
6356 * notification but may give context to a conversation. The system may choose to present
6357 * them only when relevant, e.g. when replying to a message through a {@link RemoteInput}.
6358 *
6359 * <p>The messages should be added in chronologic order, i.e. the oldest first,
6360 * the newest last.
6361 *
6362 * @param message The historic {@link Message} to be added
6363 * @return this object for method chaining
6364 */
6365 public MessagingStyle addHistoricMessage(Message message) {
6366 mHistoricMessages.add(message);
6367 if (mHistoricMessages.size() > MAXIMUM_RETAINED_MESSAGES) {
6368 mHistoricMessages.remove(0);
6369 }
6370 return this;
6371 }
6372
6373 /**
Alex Hillsfc737de2016-03-23 17:33:02 -04006374 * Gets the list of {@code Message} objects that represent the notification
6375 */
6376 public List<Message> getMessages() {
6377 return mMessages;
6378 }
6379
6380 /**
Adrian Roos437cd562017-01-18 15:47:03 -08006381 * Gets the list of historic {@code Message}s in the notification.
6382 */
6383 public List<Message> getHistoricMessages() {
6384 return mHistoricMessages;
6385 }
6386
6387 /**
Kodlee Yin9ac617c2017-12-19 11:20:50 -08006388 * Sets whether this conversation notification represents a group.
Kodlee Yin14656422017-12-22 17:00:46 -08006389 *
Kodlee Yin9ac617c2017-12-19 11:20:50 -08006390 * @param isGroupConversation {@code true} if the conversation represents a group,
6391 * {@code false} otherwise.
6392 * @return this object for method chaining
6393 */
6394 public MessagingStyle setGroupConversation(boolean isGroupConversation) {
6395 mIsGroupConversation = isGroupConversation;
6396 return this;
6397 }
6398
6399 /**
Kodlee Yin14656422017-12-22 17:00:46 -08006400 * Returns {@code true} if this notification represents a group conversation, otherwise
6401 * {@code false}.
6402 *
6403 * <p> If the application that generated this {@link MessagingStyle} targets an SDK version
6404 * less than {@link Build.VERSION_CODES#P}, this method becomes dependent on whether or
6405 * not the conversation title is set; returning {@code true} if the conversation title is
6406 * a non-null value, or {@code false} otherwise. From {@link Build.VERSION_CODES#P} forward,
6407 * this method returns what's set by {@link #setGroupConversation(boolean)} allowing for
6408 * named, non-group conversations.
6409 *
6410 * @see #setConversationTitle(CharSequence)
Kodlee Yin9ac617c2017-12-19 11:20:50 -08006411 */
6412 public boolean isGroupConversation() {
Kodlee Yin14656422017-12-22 17:00:46 -08006413 // When target SDK version is < P, a non-null conversation title dictates if this is
6414 // as group conversation.
6415 if (mBuilder != null
6416 && mBuilder.mContext.getApplicationInfo().targetSdkVersion
6417 < Build.VERSION_CODES.P) {
6418 return mConversationTitle != null;
6419 }
6420
Kodlee Yin9ac617c2017-12-19 11:20:50 -08006421 return mIsGroupConversation;
6422 }
6423
6424 /**
Alex Hillsfc737de2016-03-23 17:33:02 -04006425 * @hide
6426 */
6427 @Override
6428 public void addExtras(Bundle extras) {
6429 super.addExtras(extras);
Selim Cinekcb8b9852017-12-15 18:01:52 -08006430 if (mUser != null) {
6431 // For legacy usages
6432 extras.putCharSequence(EXTRA_SELF_DISPLAY_NAME, mUser.getName());
6433 extras.putParcelable(EXTRA_MESSAGING_PERSON, mUser);
Alex Hillsfc737de2016-03-23 17:33:02 -04006434 }
6435 if (mConversationTitle != null) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04006436 extras.putCharSequence(EXTRA_CONVERSATION_TITLE, mConversationTitle);
Alex Hillsfc737de2016-03-23 17:33:02 -04006437 }
Alex Hillsd9b04d92016-04-11 16:38:16 -04006438 if (!mMessages.isEmpty()) { extras.putParcelableArray(EXTRA_MESSAGES,
6439 Message.getBundleArrayForMessages(mMessages));
Alex Hillsfc737de2016-03-23 17:33:02 -04006440 }
Adrian Roos437cd562017-01-18 15:47:03 -08006441 if (!mHistoricMessages.isEmpty()) { extras.putParcelableArray(EXTRA_HISTORIC_MESSAGES,
6442 Message.getBundleArrayForMessages(mHistoricMessages));
6443 }
Adrian Roos33fbd2c2016-05-27 15:35:28 -07006444
6445 fixTitleAndTextExtras(extras);
Kodlee Yin9ac617c2017-12-19 11:20:50 -08006446 extras.putBoolean(EXTRA_IS_GROUP_CONVERSATION, mIsGroupConversation);
Adrian Roos33fbd2c2016-05-27 15:35:28 -07006447 }
6448
6449 private void fixTitleAndTextExtras(Bundle extras) {
6450 Message m = findLatestIncomingMessage();
6451 CharSequence text = (m == null) ? null : m.mText;
6452 CharSequence sender = m == null ? null
Selim Cinekcb8b9852017-12-15 18:01:52 -08006453 : m.mSender == null || TextUtils.isEmpty(m.mSender.getName())
6454 ? mUser.getName() : m.mSender.getName();
Adrian Roos33fbd2c2016-05-27 15:35:28 -07006455 CharSequence title;
6456 if (!TextUtils.isEmpty(mConversationTitle)) {
6457 if (!TextUtils.isEmpty(sender)) {
6458 BidiFormatter bidi = BidiFormatter.getInstance();
6459 title = mBuilder.mContext.getString(
6460 com.android.internal.R.string.notification_messaging_title_template,
Selim Cinekcb8b9852017-12-15 18:01:52 -08006461 bidi.unicodeWrap(mConversationTitle), bidi.unicodeWrap(sender));
Adrian Roos33fbd2c2016-05-27 15:35:28 -07006462 } else {
6463 title = mConversationTitle;
6464 }
6465 } else {
6466 title = sender;
6467 }
6468
6469 if (title != null) {
6470 extras.putCharSequence(EXTRA_TITLE, title);
6471 }
6472 if (text != null) {
6473 extras.putCharSequence(EXTRA_TEXT, text);
6474 }
Alex Hillsfc737de2016-03-23 17:33:02 -04006475 }
6476
6477 /**
6478 * @hide
6479 */
6480 @Override
6481 protected void restoreFromExtras(Bundle extras) {
6482 super.restoreFromExtras(extras);
6483
Selim Cinekcb8b9852017-12-15 18:01:52 -08006484 mUser = extras.getParcelable(EXTRA_MESSAGING_PERSON);
6485 if (mUser == null) {
6486 CharSequence displayName = extras.getCharSequence(EXTRA_SELF_DISPLAY_NAME);
6487 mUser = new Person().setName(displayName);
6488 }
Adrian Roos96b7e202016-05-17 13:50:38 -07006489 mConversationTitle = extras.getCharSequence(EXTRA_CONVERSATION_TITLE);
Adrian Roos437cd562017-01-18 15:47:03 -08006490 Parcelable[] messages = extras.getParcelableArray(EXTRA_MESSAGES);
Selim Cinek88188f22017-09-19 16:46:56 -07006491 mMessages = Message.getMessagesFromBundleArray(messages);
Adrian Roos437cd562017-01-18 15:47:03 -08006492 Parcelable[] histMessages = extras.getParcelableArray(EXTRA_HISTORIC_MESSAGES);
Selim Cinek88188f22017-09-19 16:46:56 -07006493 mHistoricMessages = Message.getMessagesFromBundleArray(histMessages);
Kodlee Yin9ac617c2017-12-19 11:20:50 -08006494 mIsGroupConversation = extras.getBoolean(EXTRA_IS_GROUP_CONVERSATION);
Alex Hillsfc737de2016-03-23 17:33:02 -04006495 }
6496
6497 /**
6498 * @hide
6499 */
Adrian Roosc1a80b02016-04-05 14:54:55 -07006500 @Override
Selim Cinek7d1009b2017-01-25 15:28:28 -08006501 public RemoteViews makeContentView(boolean increasedHeight) {
Selim Cineke62255c2017-09-28 18:23:23 -07006502 mBuilder.mOriginalActions = mBuilder.mActions;
6503 mBuilder.mActions = new ArrayList<>();
Selim Cinek7199ed92018-01-26 13:36:43 -08006504 RemoteViews remoteViews = makeMessagingView(true /* isCollapsed */);
Selim Cineke62255c2017-09-28 18:23:23 -07006505 mBuilder.mActions = mBuilder.mOriginalActions;
6506 mBuilder.mOriginalActions = null;
6507 return remoteViews;
Adrian Roosc1a80b02016-04-05 14:54:55 -07006508 }
6509
6510 private Message findLatestIncomingMessage() {
Selim Cinek88188f22017-09-19 16:46:56 -07006511 return findLatestIncomingMessage(mMessages);
6512 }
6513
6514 /**
6515 * @hide
6516 */
6517 @Nullable
6518 public static Message findLatestIncomingMessage(
6519 List<Message> messages) {
6520 for (int i = messages.size() - 1; i >= 0; i--) {
6521 Message m = messages.get(i);
Adrian Roosc1a80b02016-04-05 14:54:55 -07006522 // Incoming messages have a non-empty sender.
Selim Cinekcb8b9852017-12-15 18:01:52 -08006523 if (m.mSender != null && !TextUtils.isEmpty(m.mSender.getName())) {
Adrian Roosc1a80b02016-04-05 14:54:55 -07006524 return m;
6525 }
6526 }
Selim Cinek88188f22017-09-19 16:46:56 -07006527 if (!messages.isEmpty()) {
Adrian Roos33fbd2c2016-05-27 15:35:28 -07006528 // No incoming messages, fall back to outgoing message
Selim Cinek88188f22017-09-19 16:46:56 -07006529 return messages.get(messages.size() - 1);
Adrian Roos33fbd2c2016-05-27 15:35:28 -07006530 }
Adrian Roosc1a80b02016-04-05 14:54:55 -07006531 return null;
6532 }
6533
6534 /**
6535 * @hide
6536 */
6537 @Override
6538 public RemoteViews makeBigContentView() {
Selim Cinek7199ed92018-01-26 13:36:43 -08006539 return makeMessagingView(false /* isCollapsed */);
Selim Cinekafeed292017-12-12 17:32:44 -08006540 }
6541
6542 @NonNull
Selim Cinek7199ed92018-01-26 13:36:43 -08006543 private RemoteViews makeMessagingView(boolean isCollapsed) {
Selim Cinek88188f22017-09-19 16:46:56 -07006544 CharSequence conversationTitle = !TextUtils.isEmpty(super.mBigContentTitle)
Adrian Roosc1a80b02016-04-05 14:54:55 -07006545 ? super.mBigContentTitle
6546 : mConversationTitle;
Selim Cinek88188f22017-09-19 16:46:56 -07006547 boolean isOneToOne = TextUtils.isEmpty(conversationTitle);
Selim Cinek2dd3e722018-01-19 11:06:06 -08006548 CharSequence nameReplacement = null;
Selim Cinekafeed292017-12-12 17:32:44 -08006549 if (hasOnlyWhiteSpaceSenders()) {
Selim Cinekf7409db2017-10-24 16:17:14 -07006550 isOneToOne = true;
Selim Cinek2dd3e722018-01-19 11:06:06 -08006551 nameReplacement = conversationTitle;
6552 conversationTitle = null;
Adrian Roosb1f427c2016-05-26 12:27:15 -07006553 }
Selim Cinek7199ed92018-01-26 13:36:43 -08006554 boolean hideLargeIcon = !isCollapsed || isOneToOne;
Adrian Roos48d746a2016-04-12 14:57:28 -07006555 RemoteViews contentView = mBuilder.applyStandardTemplateWithActions(
Adrian Roosc1a80b02016-04-05 14:54:55 -07006556 mBuilder.getMessagingLayoutResource(),
Selim Cinek88188f22017-09-19 16:46:56 -07006557 mBuilder.mParams.reset().hasProgress(false).title(conversationTitle).text(null)
Selim Cinek7199ed92018-01-26 13:36:43 -08006558 .hideLargeIcon(hideLargeIcon)
Selim Cinekafeed292017-12-12 17:32:44 -08006559 .headerTextSecondary(conversationTitle)
Selim Cinek7199ed92018-01-26 13:36:43 -08006560 .alwaysShowReply(isCollapsed));
Selim Cinek88188f22017-09-19 16:46:56 -07006561 addExtras(mBuilder.mN.extras);
Selim Cinekafeed292017-12-12 17:32:44 -08006562 // also update the end margin if there is an image
6563 int endMargin = R.dimen.notification_content_margin_end;
Selim Cinek7199ed92018-01-26 13:36:43 -08006564 if (isCollapsed) {
Selim Cinekafeed292017-12-12 17:32:44 -08006565 endMargin = R.dimen.notification_content_plus_picture_margin_end;
6566 }
6567 contentView.setViewLayoutMarginEndDimen(R.id.notification_main_column, endMargin);
Selim Cinek88188f22017-09-19 16:46:56 -07006568 contentView.setInt(R.id.status_bar_latest_event_content, "setLayoutColor",
6569 mBuilder.resolveContrastColor());
Selim Cinek7199ed92018-01-26 13:36:43 -08006570 contentView.setBoolean(R.id.status_bar_latest_event_content, "setIsCollapsed",
6571 isCollapsed);
Selim Cinek88188f22017-09-19 16:46:56 -07006572 contentView.setIcon(R.id.status_bar_latest_event_content, "setLargeIcon",
6573 mBuilder.mN.mLargeIcon);
Selim Cinek2dd3e722018-01-19 11:06:06 -08006574 contentView.setCharSequence(R.id.status_bar_latest_event_content, "setNameReplacement",
6575 nameReplacement);
Selim Cinek88188f22017-09-19 16:46:56 -07006576 contentView.setBoolean(R.id.status_bar_latest_event_content, "setIsOneToOne",
6577 isOneToOne);
6578 contentView.setBundle(R.id.status_bar_latest_event_content, "setData",
6579 mBuilder.mN.extras);
Alex Hillsfc737de2016-03-23 17:33:02 -04006580 return contentView;
6581 }
6582
Selim Cinekf7409db2017-10-24 16:17:14 -07006583 private boolean hasOnlyWhiteSpaceSenders() {
6584 for (int i = 0; i < mMessages.size(); i++) {
6585 Message m = mMessages.get(i);
Selim Cinekcb8b9852017-12-15 18:01:52 -08006586 Person sender = m.getSenderPerson();
6587 if (sender != null && !isWhiteSpace(sender.getName())) {
Selim Cinekf7409db2017-10-24 16:17:14 -07006588 return false;
6589 }
6590 }
6591 return true;
6592 }
6593
6594 private boolean isWhiteSpace(CharSequence sender) {
6595 if (TextUtils.isEmpty(sender)) {
6596 return true;
6597 }
6598 if (sender.toString().matches("^\\s*$")) {
6599 return true;
6600 }
6601 // Let's check if we only have 0 whitespace chars. Some apps did this as a workaround
6602 // For the presentation that we had.
6603 for (int i = 0; i < sender.length(); i++) {
6604 char c = sender.charAt(i);
6605 if (c != '\u200B') {
6606 return false;
6607 }
6608 }
6609 return true;
6610 }
6611
Selim Cinek88188f22017-09-19 16:46:56 -07006612 private CharSequence createConversationTitleFromMessages() {
6613 ArraySet<CharSequence> names = new ArraySet<>();
6614 for (int i = 0; i < mMessages.size(); i++) {
6615 Message m = mMessages.get(i);
Selim Cinekcb8b9852017-12-15 18:01:52 -08006616 Person sender = m.getSenderPerson();
Selim Cinek88188f22017-09-19 16:46:56 -07006617 if (sender != null) {
Selim Cinekcb8b9852017-12-15 18:01:52 -08006618 names.add(sender.getName());
Selim Cinek88188f22017-09-19 16:46:56 -07006619 }
6620 }
6621 SpannableStringBuilder title = new SpannableStringBuilder();
6622 int size = names.size();
6623 for (int i = 0; i < size; i++) {
6624 CharSequence name = names.valueAt(i);
6625 if (!TextUtils.isEmpty(title)) {
6626 title.append(", ");
6627 }
6628 title.append(BidiFormatter.getInstance().unicodeWrap(name));
6629 }
6630 return title;
6631 }
6632
Adrian Roosdedd1df2016-04-26 16:38:47 -07006633 /**
6634 * @hide
6635 */
6636 @Override
Selim Cinek87ed69b2017-02-09 15:59:43 -08006637 public RemoteViews makeHeadsUpContentView(boolean increasedHeight) {
Selim Cinek7199ed92018-01-26 13:36:43 -08006638 RemoteViews remoteViews = makeMessagingView(true /* isCollapsed */);
Selim Cineke62255c2017-09-28 18:23:23 -07006639 remoteViews.setInt(R.id.notification_messaging, "setMaxDisplayedLines", 1);
6640 return remoteViews;
Adrian Roosdedd1df2016-04-26 16:38:47 -07006641 }
6642
Adrian Roosc1a80b02016-04-05 14:54:55 -07006643 private static TextAppearanceSpan makeFontColorSpan(int color) {
6644 return new TextAppearanceSpan(null, 0, 0,
6645 ColorStateList.valueOf(color), null);
6646 }
6647
Alex Hillsd9b04d92016-04-11 16:38:16 -04006648 public static final class Message {
6649
6650 static final String KEY_TEXT = "text";
6651 static final String KEY_TIMESTAMP = "time";
6652 static final String KEY_SENDER = "sender";
Selim Cinekcb8b9852017-12-15 18:01:52 -08006653 static final String KEY_SENDER_PERSON = "sender_person";
Alex Hillsd9b04d92016-04-11 16:38:16 -04006654 static final String KEY_DATA_MIME_TYPE = "type";
6655 static final String KEY_DATA_URI= "uri";
Shane Brennan5a871862017-03-11 13:14:17 -08006656 static final String KEY_EXTRAS_BUNDLE = "extras";
Alex Hillsfc737de2016-03-23 17:33:02 -04006657
6658 private final CharSequence mText;
6659 private final long mTimestamp;
Selim Cinekcb8b9852017-12-15 18:01:52 -08006660 @Nullable
6661 private final Person mSender;
Alex Hillsfc737de2016-03-23 17:33:02 -04006662
Shane Brennan5a871862017-03-11 13:14:17 -08006663 private Bundle mExtras = new Bundle();
Alex Hillsfc737de2016-03-23 17:33:02 -04006664 private String mDataMimeType;
6665 private Uri mDataUri;
6666
6667 /**
6668 * Constructor
6669 * @param text A {@link CharSequence} to be displayed as the message content
6670 * @param timestamp Time at which the message arrived
6671 * @param sender A {@link CharSequence} to be used for displaying the name of the
6672 * sender. Should be <code>null</code> for messages by the current user, in which case
6673 * the platform will insert {@link MessagingStyle#getUserDisplayName()}.
6674 * Should be unique amongst all individuals in the conversation, and should be
6675 * consistent during re-posts of the notification.
Selim Cinekcb8b9852017-12-15 18:01:52 -08006676 *
6677 * @deprecated use {@code Message(CharSequence, long, Person)}
Alex Hillsfc737de2016-03-23 17:33:02 -04006678 */
6679 public Message(CharSequence text, long timestamp, CharSequence sender){
Selim Cinekcb8b9852017-12-15 18:01:52 -08006680 this(text, timestamp, sender == null ? null : new Person().setName(sender));
6681 }
6682
6683 /**
6684 * Constructor
6685 * @param text A {@link CharSequence} to be displayed as the message content
6686 * @param timestamp Time at which the message arrived
6687 * @param sender The {@link Person} who sent the message.
6688 * Should be <code>null</code> for messages by the current user, in which case
6689 * the platform will insert the user set in {@code MessagingStyle(Person)}.
6690 * <p>
6691 * The person provided should contain an Icon, set with {@link Person#setIcon(Icon)}
6692 * and also have a name provided with {@link Person#setName(CharSequence)}. If multiple
6693 * users have the same name, consider providing a key with {@link Person#setKey(String)}
6694 * in order to differentiate between the different users.
6695 * </p>
6696 */
6697 public Message(CharSequence text, long timestamp, @Nullable Person sender){
Alex Hillsfc737de2016-03-23 17:33:02 -04006698 mText = text;
6699 mTimestamp = timestamp;
6700 mSender = sender;
6701 }
6702
6703 /**
6704 * Sets a binary blob of data and an associated MIME type for a message. In the case
6705 * where the platform doesn't support the MIME type, the original text provided in the
6706 * constructor will be used.
6707 * @param dataMimeType The MIME type of the content. See
6708 * <a href="{@docRoot}notifications/messaging.html"> for the list of supported MIME
6709 * types on Android and Android Wear.
6710 * @param dataUri The uri containing the content whose type is given by the MIME type.
6711 * <p class="note">
6712 * <ol>
6713 * <li>Notification Listeners including the System UI need permission to access the
6714 * data the Uri points to. The recommended ways to do this are:</li>
6715 * <li>Store the data in your own ContentProvider, making sure that other apps have
6716 * the correct permission to access your provider. The preferred mechanism for
6717 * providing access is to use per-URI permissions which are temporary and only
6718 * grant access to the receiving application. An easy way to create a
6719 * ContentProvider like this is to use the FileProvider helper class.</li>
6720 * <li>Use the system MediaStore. The MediaStore is primarily aimed at video, audio
6721 * and image MIME types, however beginning with Android 3.0 (API level 11) it can
6722 * also store non-media types (see MediaStore.Files for more info). Files can be
6723 * inserted into the MediaStore using scanFile() after which a content:// style
6724 * Uri suitable for sharing is passed to the provided onScanCompleted() callback.
6725 * Note that once added to the system MediaStore the content is accessible to any
6726 * app on the device.</li>
6727 * </ol>
6728 * @return this object for method chaining
6729 */
6730 public Message setData(String dataMimeType, Uri dataUri) {
6731 mDataMimeType = dataMimeType;
6732 mDataUri = dataUri;
6733 return this;
6734 }
6735
Alex Hillsfc737de2016-03-23 17:33:02 -04006736 /**
6737 * Get the text to be used for this message, or the fallback text if a type and content
6738 * Uri have been set
6739 */
6740 public CharSequence getText() {
6741 return mText;
6742 }
6743
6744 /**
6745 * Get the time at which this message arrived
6746 */
6747 public long getTimestamp() {
6748 return mTimestamp;
6749 }
6750
6751 /**
Shane Brennan5a871862017-03-11 13:14:17 -08006752 * Get the extras Bundle for this message.
6753 */
6754 public Bundle getExtras() {
6755 return mExtras;
6756 }
6757
6758 /**
Alex Hillsfc737de2016-03-23 17:33:02 -04006759 * Get the text used to display the contact's name in the messaging experience
Selim Cinekcb8b9852017-12-15 18:01:52 -08006760 *
6761 * @deprecated use {@link #getSenderPerson()}
Alex Hillsfc737de2016-03-23 17:33:02 -04006762 */
6763 public CharSequence getSender() {
Selim Cinekcb8b9852017-12-15 18:01:52 -08006764 return mSender == null ? null : mSender.getName();
6765 }
6766
6767 /**
6768 * Get the sender associated with this message.
6769 */
6770 @Nullable
6771 public Person getSenderPerson() {
Alex Hillsfc737de2016-03-23 17:33:02 -04006772 return mSender;
6773 }
6774
6775 /**
6776 * Get the MIME type of the data pointed to by the Uri
6777 */
6778 public String getDataMimeType() {
6779 return mDataMimeType;
6780 }
6781
6782 /**
6783 * Get the the Uri pointing to the content of the message. Can be null, in which case
6784 * {@see #getText()} is used.
6785 */
6786 public Uri getDataUri() {
6787 return mDataUri;
6788 }
6789
Alex Hillsd9b04d92016-04-11 16:38:16 -04006790 private Bundle toBundle() {
6791 Bundle bundle = new Bundle();
Alex Hillsfc737de2016-03-23 17:33:02 -04006792 if (mText != null) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04006793 bundle.putCharSequence(KEY_TEXT, mText);
Alex Hillsfc737de2016-03-23 17:33:02 -04006794 }
Alex Hillsd9b04d92016-04-11 16:38:16 -04006795 bundle.putLong(KEY_TIMESTAMP, mTimestamp);
Alex Hillsfc737de2016-03-23 17:33:02 -04006796 if (mSender != null) {
Selim Cinekcb8b9852017-12-15 18:01:52 -08006797 // Legacy listeners need this
6798 bundle.putCharSequence(KEY_SENDER, mSender.getName());
6799 bundle.putParcelable(KEY_SENDER_PERSON, mSender);
Alex Hillsfc737de2016-03-23 17:33:02 -04006800 }
6801 if (mDataMimeType != null) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04006802 bundle.putString(KEY_DATA_MIME_TYPE, mDataMimeType);
Alex Hillsfc737de2016-03-23 17:33:02 -04006803 }
6804 if (mDataUri != null) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04006805 bundle.putParcelable(KEY_DATA_URI, mDataUri);
Alex Hillsfc737de2016-03-23 17:33:02 -04006806 }
Shane Brennan5a871862017-03-11 13:14:17 -08006807 if (mExtras != null) {
6808 bundle.putBundle(KEY_EXTRAS_BUNDLE, mExtras);
6809 }
Alex Hillsd9b04d92016-04-11 16:38:16 -04006810 return bundle;
Alex Hillsfc737de2016-03-23 17:33:02 -04006811 }
6812
Alex Hillsd9b04d92016-04-11 16:38:16 -04006813 static Bundle[] getBundleArrayForMessages(List<Message> messages) {
6814 Bundle[] bundles = new Bundle[messages.size()];
6815 final int N = messages.size();
6816 for (int i = 0; i < N; i++) {
6817 bundles[i] = messages.get(i).toBundle();
6818 }
6819 return bundles;
6820 }
6821
Selim Cinek88188f22017-09-19 16:46:56 -07006822 /**
6823 * @return A list of messages read from the bundles.
6824 *
6825 * @hide
6826 */
6827 public static List<Message> getMessagesFromBundleArray(Parcelable[] bundles) {
6828 if (bundles == null) {
6829 return new ArrayList<>();
6830 }
Alex Hillsd9b04d92016-04-11 16:38:16 -04006831 List<Message> messages = new ArrayList<>(bundles.length);
6832 for (int i = 0; i < bundles.length; i++) {
Adrian Roosdedd1df2016-04-26 16:38:47 -07006833 if (bundles[i] instanceof Bundle) {
6834 Message message = getMessageFromBundle((Bundle)bundles[i]);
6835 if (message != null) {
6836 messages.add(message);
6837 }
Alex Hillsd9b04d92016-04-11 16:38:16 -04006838 }
6839 }
6840 return messages;
6841 }
6842
6843 static Message getMessageFromBundle(Bundle bundle) {
6844 try {
Adrian Roosfbddd2c2016-05-13 12:57:20 -07006845 if (!bundle.containsKey(KEY_TEXT) || !bundle.containsKey(KEY_TIMESTAMP)) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04006846 return null;
6847 } else {
Selim Cinekcb8b9852017-12-15 18:01:52 -08006848
6849 Person senderPerson = bundle.getParcelable(KEY_SENDER_PERSON);
6850 if (senderPerson == null) {
6851 // Legacy apps that use compat don't actually provide the sender objects
6852 // We need to fix the compat version to provide people / use
6853 // the native api instead
6854 CharSequence senderName = bundle.getCharSequence(KEY_SENDER);
6855 if (senderName != null) {
6856 senderPerson = new Person().setName(senderName);
6857 }
6858 }
Alex Hillsd9b04d92016-04-11 16:38:16 -04006859 Message message = new Message(bundle.getCharSequence(KEY_TEXT),
Selim Cinekcb8b9852017-12-15 18:01:52 -08006860 bundle.getLong(KEY_TIMESTAMP),
6861 senderPerson);
Alex Hillsd9b04d92016-04-11 16:38:16 -04006862 if (bundle.containsKey(KEY_DATA_MIME_TYPE) &&
6863 bundle.containsKey(KEY_DATA_URI)) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04006864 message.setData(bundle.getString(KEY_DATA_MIME_TYPE),
6865 (Uri) bundle.getParcelable(KEY_DATA_URI));
Alex Hillsfc737de2016-03-23 17:33:02 -04006866 }
Shane Brennan5a871862017-03-11 13:14:17 -08006867 if (bundle.containsKey(KEY_EXTRAS_BUNDLE)) {
6868 message.getExtras().putAll(bundle.getBundle(KEY_EXTRAS_BUNDLE));
6869 }
Alex Hillsd9b04d92016-04-11 16:38:16 -04006870 return message;
6871 }
6872 } catch (ClassCastException e) {
6873 return null;
6874 }
6875 }
Alex Hillsfc737de2016-03-23 17:33:02 -04006876 }
6877 }
6878
6879 /**
Daniel Sandler879c5e02012-04-17 16:46:51 -04006880 * Helper class for generating large-format notifications that include a list of (up to 5) strings.
Joe Malin8d40d042012-11-05 11:36:40 -08006881 *
Robert Ly91c5ce32014-06-08 15:37:00 -07006882 * Here's how you'd set the <code>InboxStyle</code> on a notification:
Daniel Sandler879c5e02012-04-17 16:46:51 -04006883 * <pre class="prettyprint">
Robert Ly91c5ce32014-06-08 15:37:00 -07006884 * Notification notif = new Notification.Builder(mContext)
6885 * .setContentTitle(&quot;5 New mails from &quot; + sender.toString())
6886 * .setContentText(subject)
6887 * .setSmallIcon(R.drawable.new_mail)
6888 * .setLargeIcon(aBitmap)
6889 * .setStyle(new Notification.InboxStyle()
6890 * .addLine(str1)
6891 * .addLine(str2)
6892 * .setContentTitle(&quot;&quot;)
6893 * .setSummaryText(&quot;+3 more&quot;))
6894 * .build();
Daniel Sandler879c5e02012-04-17 16:46:51 -04006895 * </pre>
Joe Malin8d40d042012-11-05 11:36:40 -08006896 *
Daniel Sandler879c5e02012-04-17 16:46:51 -04006897 * @see Notification#bigContentView
6898 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006899 public static class InboxStyle extends Style {
Daniel Sandler879c5e02012-04-17 16:46:51 -04006900 private ArrayList<CharSequence> mTexts = new ArrayList<CharSequence>(5);
6901
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006902 public InboxStyle() {
6903 }
6904
Adrian Roosf5faf9d2016-05-23 13:56:15 -07006905 /**
6906 * @deprecated use {@code InboxStyle()}.
6907 */
6908 @Deprecated
Daniel Sandler879c5e02012-04-17 16:46:51 -04006909 public InboxStyle(Builder builder) {
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006910 setBuilder(builder);
Daniel Sandler879c5e02012-04-17 16:46:51 -04006911 }
6912
Chris Wrend6297db2012-05-03 16:20:13 -04006913 /**
6914 * Overrides ContentTitle in the big form of the template.
6915 * This defaults to the value passed to setContentTitle().
6916 */
6917 public InboxStyle setBigContentTitle(CharSequence title) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04006918 internalSetBigContentTitle(safeCharSequence(title));
Chris Wrend6297db2012-05-03 16:20:13 -04006919 return this;
6920 }
6921
6922 /**
6923 * Set the first line of text after the detail section in the big form of the template.
6924 */
6925 public InboxStyle setSummaryText(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04006926 internalSetSummaryText(safeCharSequence(cs));
Chris Wrend6297db2012-05-03 16:20:13 -04006927 return this;
6928 }
6929
Chris Wren0bd664d2012-08-01 13:56:56 -04006930 /**
6931 * Append a line to the digest section of the Inbox notification.
6932 */
Daniel Sandler879c5e02012-04-17 16:46:51 -04006933 public InboxStyle addLine(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04006934 mTexts.add(safeCharSequence(cs));
Daniel Sandler879c5e02012-04-17 16:46:51 -04006935 return this;
6936 }
6937
Daniel Sandlerf45564e2013-04-15 15:05:08 -04006938 /**
6939 * @hide
6940 */
6941 public void addExtras(Bundle extras) {
6942 super.addExtras(extras);
Christoph Studer4600f9b2014-07-22 22:44:43 +02006943
Daniel Sandlerf45564e2013-04-15 15:05:08 -04006944 CharSequence[] a = new CharSequence[mTexts.size()];
6945 extras.putCharSequenceArray(EXTRA_TEXT_LINES, mTexts.toArray(a));
6946 }
6947
Christoph Studer4600f9b2014-07-22 22:44:43 +02006948 /**
6949 * @hide
6950 */
6951 @Override
6952 protected void restoreFromExtras(Bundle extras) {
6953 super.restoreFromExtras(extras);
6954
6955 mTexts.clear();
6956 if (extras.containsKey(EXTRA_TEXT_LINES)) {
6957 Collections.addAll(mTexts, extras.getCharSequenceArray(EXTRA_TEXT_LINES));
6958 }
6959 }
6960
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006961 /**
6962 * @hide
6963 */
6964 public RemoteViews makeBigContentView() {
Selim Cinekc848c3a2016-01-13 15:27:30 -08006965 // Remove the content text so it disappears unless you have a summary
Christoph Studer4600f9b2014-07-22 22:44:43 +02006966 // Nasty
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006967 CharSequence oldBuilderContentText = mBuilder.mN.extras.getCharSequence(EXTRA_TEXT);
6968 mBuilder.getAllExtras().putCharSequence(EXTRA_TEXT, null);
Christoph Studer4600f9b2014-07-22 22:44:43 +02006969
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01006970 RemoteViews contentView = getStandardView(mBuilder.getInboxLayoutResource());
Daniel Sandler619738c2012-06-07 16:33:08 -04006971
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006972 mBuilder.getAllExtras().putCharSequence(EXTRA_TEXT, oldBuilderContentText);
Christoph Studer4600f9b2014-07-22 22:44:43 +02006973
Chris Wrend6297db2012-05-03 16:20:13 -04006974 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 -04006975 R.id.inbox_text4, R.id.inbox_text5, R.id.inbox_text6};
Chris Wrend6297db2012-05-03 16:20:13 -04006976
Chris Wren4ed80d52012-05-17 09:30:03 -04006977 // Make sure all rows are gone in case we reuse a view.
6978 for (int rowId : rowIds) {
6979 contentView.setViewVisibility(rowId, View.GONE);
6980 }
6981
Daniel Sandler879c5e02012-04-17 16:46:51 -04006982 int i=0;
Selim Cinek07c80172016-04-21 16:40:47 -07006983 int topPadding = mBuilder.mContext.getResources().getDimensionPixelSize(
6984 R.dimen.notification_inbox_item_top_padding);
Selim Cinek247fa012016-02-18 09:50:48 -08006985 boolean first = true;
Selim Cinek07c80172016-04-21 16:40:47 -07006986 int onlyViewId = 0;
6987 int maxRows = rowIds.length;
6988 if (mBuilder.mActions.size() > 0) {
6989 maxRows--;
6990 }
6991 while (i < mTexts.size() && i < maxRows) {
Daniel Sandler879c5e02012-04-17 16:46:51 -04006992 CharSequence str = mTexts.get(i);
Selim Cinek07c80172016-04-21 16:40:47 -07006993 if (!TextUtils.isEmpty(str)) {
Daniel Sandler879c5e02012-04-17 16:46:51 -04006994 contentView.setViewVisibility(rowIds[i], View.VISIBLE);
Selim Cinek48f66b72017-08-18 16:17:51 -07006995 contentView.setTextViewText(rowIds[i],
6996 mBuilder.processTextSpans(mBuilder.processLegacyText(str)));
Selim Cinek7b9605b2017-01-19 17:36:00 -08006997 mBuilder.setTextViewColorSecondary(contentView, rowIds[i]);
Selim Cinek07c80172016-04-21 16:40:47 -07006998 contentView.setViewPadding(rowIds[i], 0, topPadding, 0, 0);
Selim Cinek247fa012016-02-18 09:50:48 -08006999 handleInboxImageMargin(contentView, rowIds[i], first);
Selim Cinek07c80172016-04-21 16:40:47 -07007000 if (first) {
7001 onlyViewId = rowIds[i];
7002 } else {
7003 onlyViewId = 0;
7004 }
Selim Cinek247fa012016-02-18 09:50:48 -08007005 first = false;
Daniel Sandler879c5e02012-04-17 16:46:51 -04007006 }
7007 i++;
7008 }
Selim Cinek07c80172016-04-21 16:40:47 -07007009 if (onlyViewId != 0) {
7010 // We only have 1 entry, lets make it look like the normal Text of a Bigtext
7011 topPadding = mBuilder.mContext.getResources().getDimensionPixelSize(
7012 R.dimen.notification_text_margin_top);
7013 contentView.setViewPadding(onlyViewId, 0, topPadding, 0, 0);
7014 }
Selim Cinek1e0bf612015-11-20 15:57:26 -08007015
Daniel Sandler879c5e02012-04-17 16:46:51 -04007016 return contentView;
7017 }
Selim Cinek1e0bf612015-11-20 15:57:26 -08007018
Selim Cinek247fa012016-02-18 09:50:48 -08007019 private void handleInboxImageMargin(RemoteViews contentView, int id, boolean first) {
Selim Cinek1e0bf612015-11-20 15:57:26 -08007020 int endMargin = 0;
Selim Cinek247fa012016-02-18 09:50:48 -08007021 if (first) {
7022 final int max = mBuilder.mN.extras.getInt(EXTRA_PROGRESS_MAX, 0);
7023 final boolean ind = mBuilder.mN.extras.getBoolean(EXTRA_PROGRESS_INDETERMINATE);
7024 boolean hasProgress = max != 0 || ind;
Selim Cinek279fa862016-06-14 10:57:25 -07007025 if (mBuilder.mN.hasLargeIcon() && !hasProgress) {
Adrian Roos2d5dbba2016-06-08 17:11:53 -07007026 endMargin = R.dimen.notification_content_picture_margin;
Selim Cinek247fa012016-02-18 09:50:48 -08007027 }
Selim Cinek1e0bf612015-11-20 15:57:26 -08007028 }
Adrian Roos2d5dbba2016-06-08 17:11:53 -07007029 contentView.setViewLayoutMarginEndDimen(id, endMargin);
Selim Cinek1e0bf612015-11-20 15:57:26 -08007030 }
Daniel Sandler879c5e02012-04-17 16:46:51 -04007031 }
Dan Sandler842dd772014-05-15 09:36:47 -04007032
7033 /**
7034 * Notification style for media playback notifications.
7035 *
7036 * In the expanded form, {@link Notification#bigContentView}, up to 5
7037 * {@link Notification.Action}s specified with
Dan Sandler86647982015-05-13 23:41:13 -04007038 * {@link Notification.Builder#addAction(Action) addAction} will be
Dan Sandler842dd772014-05-15 09:36:47 -04007039 * shown as icon-only pushbuttons, suitable for transport controls. The Bitmap given to
7040 * {@link Notification.Builder#setLargeIcon(android.graphics.Bitmap) setLargeIcon()} will be
7041 * treated as album artwork.
Selim Cinek99104832017-01-25 14:47:33 -08007042 * <p>
Dan Sandler842dd772014-05-15 09:36:47 -04007043 * Unlike the other styles provided here, MediaStyle can also modify the standard-size
7044 * {@link Notification#contentView}; by providing action indices to
Christoph Studerfde6f4d2014-12-12 13:23:26 +01007045 * {@link #setShowActionsInCompactView(int...)} you can promote up to 3 actions to be displayed
Dan Sandler842dd772014-05-15 09:36:47 -04007046 * in the standard view alongside the usual content.
Selim Cinek99104832017-01-25 14:47:33 -08007047 * <p>
Bryan Mawhinney6be8de32014-07-18 10:35:12 +01007048 * Notifications created with MediaStyle will have their category set to
7049 * {@link Notification#CATEGORY_TRANSPORT CATEGORY_TRANSPORT} unless you set a different
7050 * category using {@link Notification.Builder#setCategory(String) setCategory()}.
Selim Cinek99104832017-01-25 14:47:33 -08007051 * <p>
Jeff Browndba34ba2014-06-24 20:46:03 -07007052 * Finally, if you attach a {@link android.media.session.MediaSession.Token} using
7053 * {@link android.app.Notification.MediaStyle#setMediaSession(MediaSession.Token)},
Dan Sandler842dd772014-05-15 09:36:47 -04007054 * the System UI can identify this as a notification representing an active media session
7055 * and respond accordingly (by showing album artwork in the lockscreen, for example).
7056 *
Selim Cinek99104832017-01-25 14:47:33 -08007057 * <p>
7058 * Starting at {@link android.os.Build.VERSION_CODES#O Android O} any notification that has a
7059 * media session attached with {@link #setMediaSession(MediaSession.Token)} will be colorized.
7060 * You can opt-out of this behavior by using {@link Notification.Builder#setColorized(boolean)}.
7061 * <p>
7062 *
Dan Sandler842dd772014-05-15 09:36:47 -04007063 * To use this style with your Notification, feed it to
7064 * {@link Notification.Builder#setStyle(android.app.Notification.Style)} like so:
7065 * <pre class="prettyprint">
7066 * Notification noti = new Notification.Builder()
7067 * .setSmallIcon(R.drawable.ic_stat_player)
Christoph Studere935fe92014-11-24 14:18:06 +01007068 * .setContentTitle(&quot;Track title&quot;)
7069 * .setContentText(&quot;Artist - Album&quot;)
7070 * .setLargeIcon(albumArtBitmap))
Bryan Mawhinney6be8de32014-07-18 10:35:12 +01007071 * .setStyle(<b>new Notification.MediaStyle()</b>
7072 * .setMediaSession(mySession))
Dan Sandler842dd772014-05-15 09:36:47 -04007073 * .build();
7074 * </pre>
7075 *
7076 * @see Notification#bigContentView
Selim Cinek99104832017-01-25 14:47:33 -08007077 * @see Notification.Builder#setColorized(boolean)
Dan Sandler842dd772014-05-15 09:36:47 -04007078 */
7079 public static class MediaStyle extends Style {
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02007080 static final int MAX_MEDIA_BUTTONS_IN_COMPACT = 3;
Dan Sandler842dd772014-05-15 09:36:47 -04007081 static final int MAX_MEDIA_BUTTONS = 5;
7082
7083 private int[] mActionsToShowInCompact = null;
Jeff Browndba34ba2014-06-24 20:46:03 -07007084 private MediaSession.Token mToken;
Dan Sandler842dd772014-05-15 09:36:47 -04007085
7086 public MediaStyle() {
7087 }
7088
Adrian Roosf5faf9d2016-05-23 13:56:15 -07007089 /**
7090 * @deprecated use {@code MediaStyle()}.
7091 */
7092 @Deprecated
Dan Sandler842dd772014-05-15 09:36:47 -04007093 public MediaStyle(Builder builder) {
7094 setBuilder(builder);
7095 }
7096
7097 /**
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02007098 * 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 -04007099 * notification view.
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02007100 *
7101 * @param actions the indices of the actions to show in the compact notification view
Dan Sandler842dd772014-05-15 09:36:47 -04007102 */
7103 public MediaStyle setShowActionsInCompactView(int...actions) {
7104 mActionsToShowInCompact = actions;
7105 return this;
7106 }
7107
7108 /**
Jeff Browndba34ba2014-06-24 20:46:03 -07007109 * Attach a {@link android.media.session.MediaSession.Token} to this Notification
7110 * to provide additional playback information and control to the SystemUI.
Dan Sandler842dd772014-05-15 09:36:47 -04007111 */
Jeff Browndba34ba2014-06-24 20:46:03 -07007112 public MediaStyle setMediaSession(MediaSession.Token token) {
Dan Sandler842dd772014-05-15 09:36:47 -04007113 mToken = token;
7114 return this;
7115 }
7116
Christoph Studer4600f9b2014-07-22 22:44:43 +02007117 /**
7118 * @hide
7119 */
Dan Sandler842dd772014-05-15 09:36:47 -04007120 @Override
7121 public Notification buildStyled(Notification wip) {
Christoph Studer4600f9b2014-07-22 22:44:43 +02007122 super.buildStyled(wip);
Bryan Mawhinney6be8de32014-07-18 10:35:12 +01007123 if (wip.category == null) {
7124 wip.category = Notification.CATEGORY_TRANSPORT;
7125 }
Dan Sandler842dd772014-05-15 09:36:47 -04007126 return wip;
7127 }
7128
Christoph Studer4600f9b2014-07-22 22:44:43 +02007129 /**
7130 * @hide
7131 */
7132 @Override
Selim Cinek7d1009b2017-01-25 15:28:28 -08007133 public RemoteViews makeContentView(boolean increasedHeight) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04007134 return makeMediaContentView();
Christoph Studer4600f9b2014-07-22 22:44:43 +02007135 }
7136
7137 /**
7138 * @hide
7139 */
7140 @Override
Julia Reynoldsd9228f12015-10-20 10:37:27 -04007141 public RemoteViews makeBigContentView() {
7142 return makeMediaBigContentView();
Christoph Studer4600f9b2014-07-22 22:44:43 +02007143 }
7144
Selim Cinekcc10bfb2016-02-10 16:24:21 -08007145 /**
7146 * @hide
7147 */
7148 @Override
Selim Cinek87ed69b2017-02-09 15:59:43 -08007149 public RemoteViews makeHeadsUpContentView(boolean increasedHeight) {
Selim Cinekcc10bfb2016-02-10 16:24:21 -08007150 RemoteViews expanded = makeMediaBigContentView();
7151 return expanded != null ? expanded : makeMediaContentView();
7152 }
7153
Dan Sandler842dd772014-05-15 09:36:47 -04007154 /** @hide */
7155 @Override
7156 public void addExtras(Bundle extras) {
7157 super.addExtras(extras);
7158
7159 if (mToken != null) {
7160 extras.putParcelable(EXTRA_MEDIA_SESSION, mToken);
7161 }
Bryan Mawhinneye191f902014-07-22 12:50:09 +01007162 if (mActionsToShowInCompact != null) {
7163 extras.putIntArray(EXTRA_COMPACT_ACTIONS, mActionsToShowInCompact);
7164 }
Dan Sandler842dd772014-05-15 09:36:47 -04007165 }
7166
Christoph Studer4600f9b2014-07-22 22:44:43 +02007167 /**
7168 * @hide
7169 */
7170 @Override
7171 protected void restoreFromExtras(Bundle extras) {
7172 super.restoreFromExtras(extras);
7173
7174 if (extras.containsKey(EXTRA_MEDIA_SESSION)) {
7175 mToken = extras.getParcelable(EXTRA_MEDIA_SESSION);
7176 }
7177 if (extras.containsKey(EXTRA_COMPACT_ACTIONS)) {
7178 mActionsToShowInCompact = extras.getIntArray(EXTRA_COMPACT_ACTIONS);
7179 }
7180 }
7181
Selim Cinek5bf069a2015-11-10 19:14:27 -05007182 private RemoteViews generateMediaActionButton(Action action, int color) {
Dan Sandler842dd772014-05-15 09:36:47 -04007183 final boolean tombstone = (action.actionIntent == null);
Selim Cinekf33b1112015-07-15 17:45:11 -07007184 RemoteViews button = new BuilderRemoteViews(mBuilder.mContext.getApplicationInfo(),
Alan Viverette3cb07a462014-06-06 14:19:53 -07007185 R.layout.notification_material_media_action);
Dan Sandler68079d52015-07-22 10:45:30 -04007186 button.setImageViewIcon(R.id.action0, action.getIcon());
Anthony Chenad4d1582017-04-10 16:07:58 -07007187
7188 // If the action buttons should not be tinted, then just use the default
7189 // notification color. Otherwise, just use the passed-in color.
7190 int tintColor = mBuilder.shouldTintActionButtons() || mBuilder.isColorized()
7191 ? color
7192 : NotificationColorUtil.resolveColor(mBuilder.mContext,
7193 Notification.COLOR_DEFAULT);
7194
Sunny Goyal5b153922017-09-21 21:00:36 -07007195 button.setDrawableTint(R.id.action0, false, tintColor,
7196 PorterDuff.Mode.SRC_ATOP);
Dan Sandler842dd772014-05-15 09:36:47 -04007197 if (!tombstone) {
7198 button.setOnClickPendingIntent(R.id.action0, action.actionIntent);
7199 }
7200 button.setContentDescription(R.id.action0, action.title);
7201 return button;
7202 }
7203
7204 private RemoteViews makeMediaContentView() {
7205 RemoteViews view = mBuilder.applyStandardTemplate(
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02007206 R.layout.notification_template_material_media, false /* hasProgress */);
Dan Sandler842dd772014-05-15 09:36:47 -04007207
7208 final int numActions = mBuilder.mActions.size();
7209 final int N = mActionsToShowInCompact == null
7210 ? 0
7211 : Math.min(mActionsToShowInCompact.length, MAX_MEDIA_BUTTONS_IN_COMPACT);
7212 if (N > 0) {
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02007213 view.removeAllViews(com.android.internal.R.id.media_actions);
Dan Sandler842dd772014-05-15 09:36:47 -04007214 for (int i = 0; i < N; i++) {
7215 if (i >= numActions) {
7216 throw new IllegalArgumentException(String.format(
7217 "setShowActionsInCompactView: action %d out of bounds (max %d)",
7218 i, numActions - 1));
7219 }
7220
7221 final Action action = mBuilder.mActions.get(mActionsToShowInCompact[i]);
Selim Cinek5bf069a2015-11-10 19:14:27 -05007222 final RemoteViews button = generateMediaActionButton(action,
Selim Cinek99104832017-01-25 14:47:33 -08007223 getPrimaryHighlightColor());
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02007224 view.addView(com.android.internal.R.id.media_actions, button);
Dan Sandler842dd772014-05-15 09:36:47 -04007225 }
7226 }
Selim Cinekfdc738f2016-01-27 20:04:27 -08007227 handleImage(view);
7228 // handle the content margin
Adrian Roos2d5dbba2016-06-08 17:11:53 -07007229 int endMargin = R.dimen.notification_content_margin_end;
Selim Cinek279fa862016-06-14 10:57:25 -07007230 if (mBuilder.mN.hasLargeIcon()) {
Adrian Roos2d5dbba2016-06-08 17:11:53 -07007231 endMargin = R.dimen.notification_content_plus_picture_margin_end;
Selim Cinekfdc738f2016-01-27 20:04:27 -08007232 }
Adrian Roos2d5dbba2016-06-08 17:11:53 -07007233 view.setViewLayoutMarginEndDimen(R.id.notification_main_column, endMargin);
Dan Sandler842dd772014-05-15 09:36:47 -04007234 return view;
7235 }
7236
Selim Cinek99104832017-01-25 14:47:33 -08007237 private int getPrimaryHighlightColor() {
7238 return mBuilder.getPrimaryHighlightColor();
7239 }
7240
Dan Sandler842dd772014-05-15 09:36:47 -04007241 private RemoteViews makeMediaBigContentView() {
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02007242 final int actionCount = Math.min(mBuilder.mActions.size(), MAX_MEDIA_BUTTONS);
Selim Cinekcc10bfb2016-02-10 16:24:21 -08007243 // Dont add an expanded view if there is no more content to be revealed
7244 int actionsInCompact = mActionsToShowInCompact == null
7245 ? 0
7246 : Math.min(mActionsToShowInCompact.length, MAX_MEDIA_BUTTONS_IN_COMPACT);
Selim Cinek279fa862016-06-14 10:57:25 -07007247 if (!mBuilder.mN.hasLargeIcon() && actionCount <= actionsInCompact) {
Selim Cinekcc10bfb2016-02-10 16:24:21 -08007248 return null;
7249 }
Selim Cinek5bf069a2015-11-10 19:14:27 -05007250 RemoteViews big = mBuilder.applyStandardTemplate(
7251 R.layout.notification_template_material_big_media,
7252 false);
Dan Sandler842dd772014-05-15 09:36:47 -04007253
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02007254 if (actionCount > 0) {
7255 big.removeAllViews(com.android.internal.R.id.media_actions);
7256 for (int i = 0; i < actionCount; i++) {
Selim Cinek5bf069a2015-11-10 19:14:27 -05007257 final RemoteViews button = generateMediaActionButton(mBuilder.mActions.get(i),
Selim Cinek99104832017-01-25 14:47:33 -08007258 getPrimaryHighlightColor());
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02007259 big.addView(com.android.internal.R.id.media_actions, button);
Dan Sandler842dd772014-05-15 09:36:47 -04007260 }
7261 }
Selim Cinek5bf069a2015-11-10 19:14:27 -05007262 handleImage(big);
Dan Sandler842dd772014-05-15 09:36:47 -04007263 return big;
7264 }
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02007265
Selim Cinek5bf069a2015-11-10 19:14:27 -05007266 private void handleImage(RemoteViews contentView) {
Selim Cinek279fa862016-06-14 10:57:25 -07007267 if (mBuilder.mN.hasLargeIcon()) {
Adrian Roos2d5dbba2016-06-08 17:11:53 -07007268 contentView.setViewLayoutMarginEndDimen(R.id.line1, 0);
7269 contentView.setViewLayoutMarginEndDimen(R.id.text, 0);
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02007270 }
7271 }
7272
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02007273 /**
7274 * @hide
7275 */
7276 @Override
7277 protected boolean hasProgress() {
7278 return false;
7279 }
Dan Sandler842dd772014-05-15 09:36:47 -04007280 }
Griff Hazen61a9e862014-05-22 16:05:19 -07007281
Selim Cinek593610c2016-02-16 18:42:57 -08007282 /**
7283 * Notification style for custom views that are decorated by the system
7284 *
7285 * <p>Instead of providing a notification that is completely custom, a developer can set this
7286 * style and still obtain system decorations like the notification header with the expand
7287 * affordance and actions.
7288 *
7289 * <p>Use {@link android.app.Notification.Builder#setCustomContentView(RemoteViews)},
7290 * {@link android.app.Notification.Builder#setCustomBigContentView(RemoteViews)} and
7291 * {@link android.app.Notification.Builder#setCustomHeadsUpContentView(RemoteViews)} to set the
7292 * corresponding custom views to display.
7293 *
7294 * To use this style with your Notification, feed it to
7295 * {@link Notification.Builder#setStyle(android.app.Notification.Style)} like so:
7296 * <pre class="prettyprint">
7297 * Notification noti = new Notification.Builder()
7298 * .setSmallIcon(R.drawable.ic_stat_player)
7299 * .setLargeIcon(albumArtBitmap))
7300 * .setCustomContentView(contentView);
7301 * .setStyle(<b>new Notification.DecoratedCustomViewStyle()</b>)
7302 * .build();
7303 * </pre>
7304 */
7305 public static class DecoratedCustomViewStyle extends Style {
7306
7307 public DecoratedCustomViewStyle() {
7308 }
7309
Selim Cinek593610c2016-02-16 18:42:57 -08007310 /**
7311 * @hide
7312 */
7313 public boolean displayCustomViewInline() {
7314 return true;
7315 }
7316
7317 /**
7318 * @hide
7319 */
7320 @Override
Selim Cinek7d1009b2017-01-25 15:28:28 -08007321 public RemoteViews makeContentView(boolean increasedHeight) {
Selim Cinek593610c2016-02-16 18:42:57 -08007322 return makeStandardTemplateWithCustomContent(mBuilder.mN.contentView);
7323 }
7324
7325 /**
7326 * @hide
7327 */
7328 @Override
7329 public RemoteViews makeBigContentView() {
7330 return makeDecoratedBigContentView();
7331 }
7332
7333 /**
7334 * @hide
7335 */
7336 @Override
Selim Cinek87ed69b2017-02-09 15:59:43 -08007337 public RemoteViews makeHeadsUpContentView(boolean increasedHeight) {
Selim Cinek593610c2016-02-16 18:42:57 -08007338 return makeDecoratedHeadsUpContentView();
7339 }
7340
Selim Cinek593610c2016-02-16 18:42:57 -08007341 private RemoteViews makeDecoratedHeadsUpContentView() {
7342 RemoteViews headsUpContentView = mBuilder.mN.headsUpContentView == null
7343 ? mBuilder.mN.contentView
7344 : mBuilder.mN.headsUpContentView;
7345 if (mBuilder.mActions.size() == 0) {
7346 return makeStandardTemplateWithCustomContent(headsUpContentView);
7347 }
7348 RemoteViews remoteViews = mBuilder.applyStandardTemplateWithActions(
7349 mBuilder.getBigBaseLayoutResource());
Selim Cinek247fa012016-02-18 09:50:48 -08007350 buildIntoRemoteViewContent(remoteViews, headsUpContentView);
Selim Cinek593610c2016-02-16 18:42:57 -08007351 return remoteViews;
7352 }
7353
Selim Cinek593610c2016-02-16 18:42:57 -08007354 private RemoteViews makeStandardTemplateWithCustomContent(RemoteViews customContent) {
7355 RemoteViews remoteViews = mBuilder.applyStandardTemplate(
7356 mBuilder.getBaseLayoutResource());
Selim Cinek247fa012016-02-18 09:50:48 -08007357 buildIntoRemoteViewContent(remoteViews, customContent);
Selim Cinek593610c2016-02-16 18:42:57 -08007358 return remoteViews;
7359 }
7360
Selim Cinek593610c2016-02-16 18:42:57 -08007361 private RemoteViews makeDecoratedBigContentView() {
7362 RemoteViews bigContentView = mBuilder.mN.bigContentView == null
7363 ? mBuilder.mN.contentView
7364 : mBuilder.mN.bigContentView;
7365 if (mBuilder.mActions.size() == 0) {
7366 return makeStandardTemplateWithCustomContent(bigContentView);
7367 }
7368 RemoteViews remoteViews = mBuilder.applyStandardTemplateWithActions(
7369 mBuilder.getBigBaseLayoutResource());
Selim Cinek247fa012016-02-18 09:50:48 -08007370 buildIntoRemoteViewContent(remoteViews, bigContentView);
Selim Cinek593610c2016-02-16 18:42:57 -08007371 return remoteViews;
7372 }
Selim Cinek247fa012016-02-18 09:50:48 -08007373
7374 private void buildIntoRemoteViewContent(RemoteViews remoteViews,
7375 RemoteViews customContent) {
Adrian Roos5081c0d2016-02-26 16:04:19 -08007376 if (customContent != null) {
Selim Cinekf91017e2016-03-14 12:25:09 -07007377 // Need to clone customContent before adding, because otherwise it can no longer be
7378 // parceled independently of remoteViews.
Adrian Roos5081c0d2016-02-26 16:04:19 -08007379 customContent = customContent.clone();
Anthony Chen8f5f3582017-04-11 11:18:37 -07007380 remoteViews.removeAllViewsExceptId(R.id.notification_main_column, R.id.progress);
7381 remoteViews.addView(R.id.notification_main_column, customContent, 0 /* index */);
Selim Cinekfc8073c2017-08-16 17:50:20 -07007382 remoteViews.setReapplyDisallowed();
Adrian Roos5081c0d2016-02-26 16:04:19 -08007383 }
Selim Cinek247fa012016-02-18 09:50:48 -08007384 // also update the end margin if there is an image
Adrian Roos2d5dbba2016-06-08 17:11:53 -07007385 int endMargin = R.dimen.notification_content_margin_end;
Selim Cinek279fa862016-06-14 10:57:25 -07007386 if (mBuilder.mN.hasLargeIcon()) {
Adrian Roos2d5dbba2016-06-08 17:11:53 -07007387 endMargin = R.dimen.notification_content_plus_picture_margin_end;
Selim Cinek247fa012016-02-18 09:50:48 -08007388 }
Adrian Roos2d5dbba2016-06-08 17:11:53 -07007389 remoteViews.setViewLayoutMarginEndDimen(R.id.notification_main_column, endMargin);
Selim Cinek247fa012016-02-18 09:50:48 -08007390 }
Selim Cinek593610c2016-02-16 18:42:57 -08007391 }
7392
Selim Cinek03eb3b72016-02-18 10:39:45 -08007393 /**
7394 * Notification style for media custom views that are decorated by the system
7395 *
7396 * <p>Instead of providing a media notification that is completely custom, a developer can set
7397 * this style and still obtain system decorations like the notification header with the expand
7398 * affordance and actions.
7399 *
7400 * <p>Use {@link android.app.Notification.Builder#setCustomContentView(RemoteViews)},
7401 * {@link android.app.Notification.Builder#setCustomBigContentView(RemoteViews)} and
7402 * {@link android.app.Notification.Builder#setCustomHeadsUpContentView(RemoteViews)} to set the
7403 * corresponding custom views to display.
Selim Cinek99104832017-01-25 14:47:33 -08007404 * <p>
7405 * Contrary to {@link MediaStyle} a developer has to opt-in to the colorizing of the
7406 * notification by using {@link Notification.Builder#setColorized(boolean)}.
7407 * <p>
Selim Cinek03eb3b72016-02-18 10:39:45 -08007408 * To use this style with your Notification, feed it to
7409 * {@link Notification.Builder#setStyle(android.app.Notification.Style)} like so:
7410 * <pre class="prettyprint">
7411 * Notification noti = new Notification.Builder()
7412 * .setSmallIcon(R.drawable.ic_stat_player)
7413 * .setLargeIcon(albumArtBitmap))
7414 * .setCustomContentView(contentView);
7415 * .setStyle(<b>new Notification.DecoratedMediaCustomViewStyle()</b>
7416 * .setMediaSession(mySession))
7417 * .build();
7418 * </pre>
7419 *
7420 * @see android.app.Notification.DecoratedCustomViewStyle
7421 * @see android.app.Notification.MediaStyle
7422 */
7423 public static class DecoratedMediaCustomViewStyle extends MediaStyle {
7424
7425 public DecoratedMediaCustomViewStyle() {
7426 }
7427
Selim Cinek03eb3b72016-02-18 10:39:45 -08007428 /**
7429 * @hide
7430 */
7431 public boolean displayCustomViewInline() {
7432 return true;
7433 }
7434
7435 /**
7436 * @hide
7437 */
7438 @Override
Selim Cinek7d1009b2017-01-25 15:28:28 -08007439 public RemoteViews makeContentView(boolean increasedHeight) {
7440 RemoteViews remoteViews = super.makeContentView(false /* increasedHeight */);
Selim Cinek03eb3b72016-02-18 10:39:45 -08007441 return buildIntoRemoteView(remoteViews, R.id.notification_content_container,
7442 mBuilder.mN.contentView);
7443 }
7444
7445 /**
7446 * @hide
7447 */
7448 @Override
7449 public RemoteViews makeBigContentView() {
7450 RemoteViews customRemoteView = mBuilder.mN.bigContentView != null
7451 ? mBuilder.mN.bigContentView
7452 : mBuilder.mN.contentView;
7453 return makeBigContentViewWithCustomContent(customRemoteView);
7454 }
7455
7456 private RemoteViews makeBigContentViewWithCustomContent(RemoteViews customRemoteView) {
7457 RemoteViews remoteViews = super.makeBigContentView();
7458 if (remoteViews != null) {
7459 return buildIntoRemoteView(remoteViews, R.id.notification_main_column,
7460 customRemoteView);
7461 } else if (customRemoteView != mBuilder.mN.contentView){
Selim Cinek7d1009b2017-01-25 15:28:28 -08007462 remoteViews = super.makeContentView(false /* increasedHeight */);
Selim Cinek03eb3b72016-02-18 10:39:45 -08007463 return buildIntoRemoteView(remoteViews, R.id.notification_content_container,
7464 customRemoteView);
7465 } else {
7466 return null;
7467 }
7468 }
7469
7470 /**
7471 * @hide
7472 */
7473 @Override
Selim Cinek87ed69b2017-02-09 15:59:43 -08007474 public RemoteViews makeHeadsUpContentView(boolean increasedHeight) {
Selim Cinek03eb3b72016-02-18 10:39:45 -08007475 RemoteViews customRemoteView = mBuilder.mN.headsUpContentView != null
7476 ? mBuilder.mN.headsUpContentView
7477 : mBuilder.mN.contentView;
7478 return makeBigContentViewWithCustomContent(customRemoteView);
7479 }
7480
7481 private RemoteViews buildIntoRemoteView(RemoteViews remoteViews, int id,
7482 RemoteViews customContent) {
Adrian Roos5081c0d2016-02-26 16:04:19 -08007483 if (customContent != null) {
Selim Cinekf91017e2016-03-14 12:25:09 -07007484 // Need to clone customContent before adding, because otherwise it can no longer be
7485 // parceled independently of remoteViews.
Adrian Roos5081c0d2016-02-26 16:04:19 -08007486 customContent = customContent.clone();
Selim Cinek87c31532017-08-18 18:53:44 -07007487 customContent.overrideTextColors(mBuilder.getPrimaryTextColor());
Selim Cinekf91017e2016-03-14 12:25:09 -07007488 remoteViews.removeAllViews(id);
7489 remoteViews.addView(id, customContent);
Selim Cinekfc8073c2017-08-16 17:50:20 -07007490 remoteViews.setReapplyDisallowed();
Adrian Roos5081c0d2016-02-26 16:04:19 -08007491 }
Selim Cinek03eb3b72016-02-18 10:39:45 -08007492 return remoteViews;
7493 }
7494 }
7495
Selim Cineke7238dd2017-12-14 17:48:32 -08007496 /**
7497 * A Person associated with this Notification.
7498 */
7499 public static final class Person implements Parcelable {
7500 @Nullable private CharSequence mName;
7501 @Nullable private Icon mIcon;
7502 @Nullable private String mUri;
7503 @Nullable private String mKey;
7504
7505 protected Person(Parcel in) {
7506 mName = in.readCharSequence();
7507 if (in.readInt() != 0) {
7508 mIcon = Icon.CREATOR.createFromParcel(in);
7509 }
7510 mUri = in.readString();
7511 mKey = in.readString();
7512 }
7513
7514 /**
7515 * Create a new person.
7516 */
7517 public Person() {
7518 }
7519
7520 /**
7521 * Give this person a name.
7522 *
7523 * @param name the name of this person
7524 */
7525 public Person setName(@Nullable CharSequence name) {
7526 this.mName = name;
7527 return this;
7528 }
7529
7530 /**
7531 * Add an icon for this person.
7532 * <br />
7533 * This is currently only used for {@link MessagingStyle} notifications and should not be
7534 * provided otherwise, in order to save memory. The system will prefer this icon over any
7535 * images that are resolved from the URI.
7536 *
7537 * @param icon the icon of the person
7538 */
7539 public Person setIcon(@Nullable Icon icon) {
7540 this.mIcon = icon;
7541 return this;
7542 }
7543
7544 /**
7545 * Set a URI associated with this person.
7546 *
7547 * <P>
7548 * Depending on user preferences, adding a URI to a Person may allow the notification to
7549 * pass through interruption filters, if this notification is of
7550 * category {@link #CATEGORY_CALL} or {@link #CATEGORY_MESSAGE}.
7551 * The addition of people may also cause this notification to appear more prominently in
7552 * the user interface.
7553 * </P>
7554 *
7555 * <P>
7556 * The person should be specified by the {@code String} representation of a
7557 * {@link android.provider.ContactsContract.Contacts#CONTENT_LOOKUP_URI}.
7558 * </P>
7559 *
7560 * <P>The system will also attempt to resolve {@code mailto:} and {@code tel:} schema
7561 * URIs. The path part of these URIs must exist in the contacts database, in the
7562 * appropriate column, or the reference will be discarded as invalid. Telephone schema
7563 * URIs will be resolved by {@link android.provider.ContactsContract.PhoneLookup}.
7564 * </P>
7565 *
7566 * @param uri a URI for the person
7567 */
7568 public Person setUri(@Nullable String uri) {
7569 mUri = uri;
7570 return this;
7571 }
7572
7573 /**
7574 * Add a key to this person in order to uniquely identify it.
7575 * This is especially useful if the name doesn't uniquely identify this person or if the
7576 * display name is a short handle of the actual name.
7577 *
7578 * <P>If no key is provided, the name serves as as the key for the purpose of
7579 * identification.</P>
7580 *
7581 * @param key the key that uniquely identifies this person
7582 */
7583 public Person setKey(@Nullable String key) {
7584 mKey = key;
7585 return this;
7586 }
7587
7588
7589 /**
7590 * @return the uri provided for this person or {@code null} if no Uri was provided
7591 */
7592 @Nullable
7593 public String getUri() {
7594 return mUri;
7595 }
7596
7597 /**
7598 * @return the name provided for this person or {@code null} if no name was provided
7599 */
7600 @Nullable
7601 public CharSequence getName() {
7602 return mName;
7603 }
7604
7605 /**
7606 * @return the icon provided for this person or {@code null} if no icon was provided
7607 */
7608 @Nullable
7609 public Icon getIcon() {
7610 return mIcon;
7611 }
7612
7613 /**
7614 * @return the key provided for this person or {@code null} if no key was provided
7615 */
7616 @Nullable
7617 public String getKey() {
7618 return mKey;
7619 }
7620
7621 /**
7622 * @return the URI associated with this person, or "name:mName" otherwise
7623 * @hide
7624 */
7625 public String resolveToLegacyUri() {
7626 if (mUri != null) {
7627 return mUri;
7628 }
7629 if (mName != null) {
7630 return "name:" + mName;
7631 }
7632 return "";
7633 }
7634
7635 @Override
7636 public int describeContents() {
7637 return 0;
7638 }
7639
7640 @Override
7641 public void writeToParcel(Parcel dest, @WriteFlags int flags) {
7642 dest.writeCharSequence(mName);
7643 if (mIcon != null) {
7644 dest.writeInt(1);
7645 mIcon.writeToParcel(dest, 0);
7646 } else {
7647 dest.writeInt(0);
7648 }
7649 dest.writeString(mUri);
7650 dest.writeString(mKey);
7651 }
7652
7653 public static final Creator<Person> CREATOR = new Creator<Person>() {
7654 @Override
7655 public Person createFromParcel(Parcel in) {
7656 return new Person(in);
7657 }
7658
7659 @Override
7660 public Person[] newArray(int size) {
7661 return new Person[size];
7662 }
7663 };
7664 }
7665
Christoph Studer4600f9b2014-07-22 22:44:43 +02007666 // When adding a new Style subclass here, don't forget to update
7667 // Builder.getNotificationStyleClass.
7668
Griff Hazen61a9e862014-05-22 16:05:19 -07007669 /**
7670 * Extender interface for use with {@link Builder#extend}. Extenders may be used to add
7671 * metadata or change options on a notification builder.
7672 */
7673 public interface Extender {
7674 /**
7675 * Apply this extender to a notification builder.
7676 * @param builder the builder to be modified.
7677 * @return the build object for chaining.
7678 */
7679 public Builder extend(Builder builder);
7680 }
7681
7682 /**
7683 * Helper class to add wearable extensions to notifications.
7684 * <p class="note"> See
7685 * <a href="{@docRoot}wear/notifications/creating.html">Creating Notifications
7686 * for Android Wear</a> for more information on how to use this class.
7687 * <p>
7688 * To create a notification with wearable extensions:
7689 * <ol>
7690 * <li>Create a {@link android.app.Notification.Builder}, setting any desired
7691 * properties.
7692 * <li>Create a {@link android.app.Notification.WearableExtender}.
7693 * <li>Set wearable-specific properties using the
7694 * {@code add} and {@code set} methods of {@link android.app.Notification.WearableExtender}.
7695 * <li>Call {@link android.app.Notification.Builder#extend} to apply the extensions to a
7696 * notification.
7697 * <li>Post the notification to the notification system with the
7698 * {@code NotificationManager.notify(...)} methods.
7699 * </ol>
7700 *
7701 * <pre class="prettyprint">
7702 * Notification notif = new Notification.Builder(mContext)
7703 * .setContentTitle(&quot;New mail from &quot; + sender.toString())
7704 * .setContentText(subject)
7705 * .setSmallIcon(R.drawable.new_mail)
7706 * .extend(new Notification.WearableExtender()
7707 * .setContentIcon(R.drawable.new_mail))
7708 * .build();
7709 * NotificationManager notificationManger =
7710 * (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
7711 * notificationManger.notify(0, notif);</pre>
7712 *
7713 * <p>Wearable extensions can be accessed on an existing notification by using the
7714 * {@code WearableExtender(Notification)} constructor,
7715 * and then using the {@code get} methods to access values.
7716 *
7717 * <pre class="prettyprint">
7718 * Notification.WearableExtender wearableExtender = new Notification.WearableExtender(
7719 * notification);
Griff Hazen14f57992014-05-26 09:07:14 -07007720 * List&lt;Notification&gt; pages = wearableExtender.getPages();</pre>
Griff Hazen61a9e862014-05-22 16:05:19 -07007721 */
7722 public static final class WearableExtender implements Extender {
7723 /**
7724 * Sentinel value for an action index that is unset.
7725 */
7726 public static final int UNSET_ACTION_INDEX = -1;
7727
7728 /**
7729 * Size value for use with {@link #setCustomSizePreset} to show this notification with
7730 * default sizing.
7731 * <p>For custom display notifications created using {@link #setDisplayIntent},
Paul Soulosaa4f4bf2015-08-04 11:59:45 -07007732 * the default is {@link #SIZE_MEDIUM}. All other notifications size automatically based
Griff Hazen61a9e862014-05-22 16:05:19 -07007733 * on their content.
7734 */
7735 public static final int SIZE_DEFAULT = 0;
7736
7737 /**
7738 * Size value for use with {@link #setCustomSizePreset} to show this notification
7739 * with an extra small size.
7740 * <p>This value is only applicable for custom display notifications created using
7741 * {@link #setDisplayIntent}.
7742 */
7743 public static final int SIZE_XSMALL = 1;
7744
7745 /**
7746 * Size value for use with {@link #setCustomSizePreset} to show this notification
7747 * with a small size.
7748 * <p>This value is only applicable for custom display notifications created using
7749 * {@link #setDisplayIntent}.
7750 */
7751 public static final int SIZE_SMALL = 2;
7752
7753 /**
7754 * Size value for use with {@link #setCustomSizePreset} to show this notification
7755 * with a medium size.
7756 * <p>This value is only applicable for custom display notifications created using
7757 * {@link #setDisplayIntent}.
7758 */
7759 public static final int SIZE_MEDIUM = 3;
7760
7761 /**
7762 * Size value for use with {@link #setCustomSizePreset} to show this notification
7763 * with a large size.
7764 * <p>This value is only applicable for custom display notifications created using
7765 * {@link #setDisplayIntent}.
7766 */
7767 public static final int SIZE_LARGE = 4;
7768
Griff Hazend5f11f92014-05-27 15:40:09 -07007769 /**
7770 * Size value for use with {@link #setCustomSizePreset} to show this notification
7771 * full screen.
7772 * <p>This value is only applicable for custom display notifications created using
7773 * {@link #setDisplayIntent}.
7774 */
7775 public static final int SIZE_FULL_SCREEN = 5;
7776
Griff Hazen5f2edfc2014-09-29 16:28:44 -07007777 /**
7778 * Sentinel value for use with {@link #setHintScreenTimeout} to keep the screen on for a
7779 * short amount of time when this notification is displayed on the screen. This
7780 * is the default value.
7781 */
7782 public static final int SCREEN_TIMEOUT_SHORT = 0;
7783
7784 /**
7785 * Sentinel value for use with {@link #setHintScreenTimeout} to keep the screen on
7786 * for a longer amount of time when this notification is displayed on the screen.
7787 */
7788 public static final int SCREEN_TIMEOUT_LONG = -1;
7789
Griff Hazen61a9e862014-05-22 16:05:19 -07007790 /** Notification extra which contains wearable extensions */
7791 private static final String EXTRA_WEARABLE_EXTENSIONS = "android.wearable.EXTENSIONS";
7792
Pete Gastaf6781d2014-10-07 15:17:05 -04007793 // Keys within EXTRA_WEARABLE_EXTENSIONS for wearable options.
Griff Hazen61a9e862014-05-22 16:05:19 -07007794 private static final String KEY_ACTIONS = "actions";
7795 private static final String KEY_FLAGS = "flags";
7796 private static final String KEY_DISPLAY_INTENT = "displayIntent";
7797 private static final String KEY_PAGES = "pages";
7798 private static final String KEY_BACKGROUND = "background";
7799 private static final String KEY_CONTENT_ICON = "contentIcon";
7800 private static final String KEY_CONTENT_ICON_GRAVITY = "contentIconGravity";
7801 private static final String KEY_CONTENT_ACTION_INDEX = "contentActionIndex";
7802 private static final String KEY_CUSTOM_SIZE_PRESET = "customSizePreset";
7803 private static final String KEY_CUSTOM_CONTENT_HEIGHT = "customContentHeight";
7804 private static final String KEY_GRAVITY = "gravity";
Griff Hazen5f2edfc2014-09-29 16:28:44 -07007805 private static final String KEY_HINT_SCREEN_TIMEOUT = "hintScreenTimeout";
Nadia Benbernou948627e2016-04-14 14:41:08 -04007806 private static final String KEY_DISMISSAL_ID = "dismissalId";
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04007807 private static final String KEY_BRIDGE_TAG = "bridgeTag";
Griff Hazen61a9e862014-05-22 16:05:19 -07007808
7809 // Flags bitwise-ored to mFlags
7810 private static final int FLAG_CONTENT_INTENT_AVAILABLE_OFFLINE = 0x1;
7811 private static final int FLAG_HINT_HIDE_ICON = 1 << 1;
7812 private static final int FLAG_HINT_SHOW_BACKGROUND_ONLY = 1 << 2;
7813 private static final int FLAG_START_SCROLL_BOTTOM = 1 << 3;
Griff Hazen5f2edfc2014-09-29 16:28:44 -07007814 private static final int FLAG_HINT_AVOID_BACKGROUND_CLIPPING = 1 << 4;
Alex Hills4bcb06b2016-04-05 14:26:25 -04007815 private static final int FLAG_BIG_PICTURE_AMBIENT = 1 << 5;
Alex Hills9ab3a232016-04-05 14:54:56 -04007816 private static final int FLAG_HINT_CONTENT_INTENT_LAUNCHES_ACTIVITY = 1 << 6;
Griff Hazen61a9e862014-05-22 16:05:19 -07007817
7818 // Default value for flags integer
7819 private static final int DEFAULT_FLAGS = FLAG_CONTENT_INTENT_AVAILABLE_OFFLINE;
7820
7821 private static final int DEFAULT_CONTENT_ICON_GRAVITY = Gravity.END;
7822 private static final int DEFAULT_GRAVITY = Gravity.BOTTOM;
7823
7824 private ArrayList<Action> mActions = new ArrayList<Action>();
7825 private int mFlags = DEFAULT_FLAGS;
7826 private PendingIntent mDisplayIntent;
7827 private ArrayList<Notification> mPages = new ArrayList<Notification>();
7828 private Bitmap mBackground;
7829 private int mContentIcon;
7830 private int mContentIconGravity = DEFAULT_CONTENT_ICON_GRAVITY;
7831 private int mContentActionIndex = UNSET_ACTION_INDEX;
7832 private int mCustomSizePreset = SIZE_DEFAULT;
7833 private int mCustomContentHeight;
7834 private int mGravity = DEFAULT_GRAVITY;
Griff Hazen5f2edfc2014-09-29 16:28:44 -07007835 private int mHintScreenTimeout;
Nadia Benbernou948627e2016-04-14 14:41:08 -04007836 private String mDismissalId;
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04007837 private String mBridgeTag;
Griff Hazen61a9e862014-05-22 16:05:19 -07007838
7839 /**
7840 * Create a {@link android.app.Notification.WearableExtender} with default
7841 * options.
7842 */
7843 public WearableExtender() {
7844 }
7845
7846 public WearableExtender(Notification notif) {
7847 Bundle wearableBundle = notif.extras.getBundle(EXTRA_WEARABLE_EXTENSIONS);
7848 if (wearableBundle != null) {
7849 List<Action> actions = wearableBundle.getParcelableArrayList(KEY_ACTIONS);
7850 if (actions != null) {
7851 mActions.addAll(actions);
7852 }
7853
7854 mFlags = wearableBundle.getInt(KEY_FLAGS, DEFAULT_FLAGS);
7855 mDisplayIntent = wearableBundle.getParcelable(KEY_DISPLAY_INTENT);
7856
7857 Notification[] pages = getNotificationArrayFromBundle(
7858 wearableBundle, KEY_PAGES);
7859 if (pages != null) {
7860 Collections.addAll(mPages, pages);
7861 }
7862
7863 mBackground = wearableBundle.getParcelable(KEY_BACKGROUND);
7864 mContentIcon = wearableBundle.getInt(KEY_CONTENT_ICON);
7865 mContentIconGravity = wearableBundle.getInt(KEY_CONTENT_ICON_GRAVITY,
7866 DEFAULT_CONTENT_ICON_GRAVITY);
7867 mContentActionIndex = wearableBundle.getInt(KEY_CONTENT_ACTION_INDEX,
7868 UNSET_ACTION_INDEX);
7869 mCustomSizePreset = wearableBundle.getInt(KEY_CUSTOM_SIZE_PRESET,
7870 SIZE_DEFAULT);
7871 mCustomContentHeight = wearableBundle.getInt(KEY_CUSTOM_CONTENT_HEIGHT);
7872 mGravity = wearableBundle.getInt(KEY_GRAVITY, DEFAULT_GRAVITY);
Griff Hazen5f2edfc2014-09-29 16:28:44 -07007873 mHintScreenTimeout = wearableBundle.getInt(KEY_HINT_SCREEN_TIMEOUT);
Nadia Benbernou948627e2016-04-14 14:41:08 -04007874 mDismissalId = wearableBundle.getString(KEY_DISMISSAL_ID);
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04007875 mBridgeTag = wearableBundle.getString(KEY_BRIDGE_TAG);
Griff Hazen61a9e862014-05-22 16:05:19 -07007876 }
7877 }
7878
7879 /**
7880 * Apply wearable extensions to a notification that is being built. This is typically
7881 * called by the {@link android.app.Notification.Builder#extend} method of
7882 * {@link android.app.Notification.Builder}.
7883 */
7884 @Override
7885 public Notification.Builder extend(Notification.Builder builder) {
7886 Bundle wearableBundle = new Bundle();
7887
7888 if (!mActions.isEmpty()) {
7889 wearableBundle.putParcelableArrayList(KEY_ACTIONS, mActions);
7890 }
7891 if (mFlags != DEFAULT_FLAGS) {
7892 wearableBundle.putInt(KEY_FLAGS, mFlags);
7893 }
7894 if (mDisplayIntent != null) {
7895 wearableBundle.putParcelable(KEY_DISPLAY_INTENT, mDisplayIntent);
7896 }
7897 if (!mPages.isEmpty()) {
7898 wearableBundle.putParcelableArray(KEY_PAGES, mPages.toArray(
7899 new Notification[mPages.size()]));
7900 }
7901 if (mBackground != null) {
7902 wearableBundle.putParcelable(KEY_BACKGROUND, mBackground);
7903 }
7904 if (mContentIcon != 0) {
7905 wearableBundle.putInt(KEY_CONTENT_ICON, mContentIcon);
7906 }
7907 if (mContentIconGravity != DEFAULT_CONTENT_ICON_GRAVITY) {
7908 wearableBundle.putInt(KEY_CONTENT_ICON_GRAVITY, mContentIconGravity);
7909 }
7910 if (mContentActionIndex != UNSET_ACTION_INDEX) {
7911 wearableBundle.putInt(KEY_CONTENT_ACTION_INDEX,
7912 mContentActionIndex);
7913 }
7914 if (mCustomSizePreset != SIZE_DEFAULT) {
7915 wearableBundle.putInt(KEY_CUSTOM_SIZE_PRESET, mCustomSizePreset);
7916 }
7917 if (mCustomContentHeight != 0) {
7918 wearableBundle.putInt(KEY_CUSTOM_CONTENT_HEIGHT, mCustomContentHeight);
7919 }
7920 if (mGravity != DEFAULT_GRAVITY) {
7921 wearableBundle.putInt(KEY_GRAVITY, mGravity);
7922 }
Griff Hazen5f2edfc2014-09-29 16:28:44 -07007923 if (mHintScreenTimeout != 0) {
7924 wearableBundle.putInt(KEY_HINT_SCREEN_TIMEOUT, mHintScreenTimeout);
7925 }
Nadia Benbernou948627e2016-04-14 14:41:08 -04007926 if (mDismissalId != null) {
7927 wearableBundle.putString(KEY_DISMISSAL_ID, mDismissalId);
7928 }
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04007929 if (mBridgeTag != null) {
7930 wearableBundle.putString(KEY_BRIDGE_TAG, mBridgeTag);
7931 }
Griff Hazen61a9e862014-05-22 16:05:19 -07007932
7933 builder.getExtras().putBundle(EXTRA_WEARABLE_EXTENSIONS, wearableBundle);
7934 return builder;
7935 }
7936
7937 @Override
7938 public WearableExtender clone() {
7939 WearableExtender that = new WearableExtender();
7940 that.mActions = new ArrayList<Action>(this.mActions);
7941 that.mFlags = this.mFlags;
7942 that.mDisplayIntent = this.mDisplayIntent;
7943 that.mPages = new ArrayList<Notification>(this.mPages);
7944 that.mBackground = this.mBackground;
7945 that.mContentIcon = this.mContentIcon;
7946 that.mContentIconGravity = this.mContentIconGravity;
7947 that.mContentActionIndex = this.mContentActionIndex;
7948 that.mCustomSizePreset = this.mCustomSizePreset;
7949 that.mCustomContentHeight = this.mCustomContentHeight;
7950 that.mGravity = this.mGravity;
Griff Hazen5f2edfc2014-09-29 16:28:44 -07007951 that.mHintScreenTimeout = this.mHintScreenTimeout;
Nadia Benbernou948627e2016-04-14 14:41:08 -04007952 that.mDismissalId = this.mDismissalId;
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04007953 that.mBridgeTag = this.mBridgeTag;
Griff Hazen61a9e862014-05-22 16:05:19 -07007954 return that;
7955 }
7956
7957 /**
7958 * Add a wearable action to this notification.
7959 *
7960 * <p>When wearable actions are added using this method, the set of actions that
7961 * show on a wearable device splits from devices that only show actions added
7962 * using {@link android.app.Notification.Builder#addAction}. This allows for customization
7963 * of which actions display on different devices.
7964 *
7965 * @param action the action to add to this notification
7966 * @return this object for method chaining
7967 * @see android.app.Notification.Action
7968 */
7969 public WearableExtender addAction(Action action) {
7970 mActions.add(action);
7971 return this;
7972 }
7973
7974 /**
7975 * Adds wearable actions to this notification.
7976 *
7977 * <p>When wearable actions are added using this method, the set of actions that
7978 * show on a wearable device splits from devices that only show actions added
7979 * using {@link android.app.Notification.Builder#addAction}. This allows for customization
7980 * of which actions display on different devices.
7981 *
7982 * @param actions the actions to add to this notification
7983 * @return this object for method chaining
7984 * @see android.app.Notification.Action
7985 */
7986 public WearableExtender addActions(List<Action> actions) {
7987 mActions.addAll(actions);
7988 return this;
7989 }
7990
7991 /**
7992 * Clear all wearable actions present on this builder.
7993 * @return this object for method chaining.
7994 * @see #addAction
7995 */
7996 public WearableExtender clearActions() {
7997 mActions.clear();
7998 return this;
7999 }
8000
8001 /**
8002 * Get the wearable actions present on this notification.
8003 */
8004 public List<Action> getActions() {
8005 return mActions;
8006 }
8007
8008 /**
8009 * Set an intent to launch inside of an activity view when displaying
Griff Hazen14f57992014-05-26 09:07:14 -07008010 * this notification. The {@link PendingIntent} provided should be for an activity.
8011 *
8012 * <pre class="prettyprint">
8013 * Intent displayIntent = new Intent(context, MyDisplayActivity.class);
8014 * PendingIntent displayPendingIntent = PendingIntent.getActivity(context,
8015 * 0, displayIntent, PendingIntent.FLAG_UPDATE_CURRENT);
8016 * Notification notif = new Notification.Builder(context)
8017 * .extend(new Notification.WearableExtender()
8018 * .setDisplayIntent(displayPendingIntent)
8019 * .setCustomSizePreset(Notification.WearableExtender.SIZE_MEDIUM))
8020 * .build();</pre>
8021 *
8022 * <p>The activity to launch needs to allow embedding, must be exported, and
Griff Hazen831ca9d2014-06-17 00:38:38 -07008023 * should have an empty task affinity. It is also recommended to use the device
8024 * default light theme.
Griff Hazen14f57992014-05-26 09:07:14 -07008025 *
8026 * <p>Example AndroidManifest.xml entry:
8027 * <pre class="prettyprint">
8028 * &lt;activity android:name=&quot;com.example.MyDisplayActivity&quot;
8029 * android:exported=&quot;true&quot;
8030 * android:allowEmbedded=&quot;true&quot;
Griff Hazen831ca9d2014-06-17 00:38:38 -07008031 * android:taskAffinity=&quot;&quot;
8032 * android:theme=&quot;@android:style/Theme.DeviceDefault.Light&quot; /&gt;</pre>
Griff Hazen61a9e862014-05-22 16:05:19 -07008033 *
8034 * @param intent the {@link PendingIntent} for an activity
8035 * @return this object for method chaining
8036 * @see android.app.Notification.WearableExtender#getDisplayIntent
8037 */
8038 public WearableExtender setDisplayIntent(PendingIntent intent) {
8039 mDisplayIntent = intent;
8040 return this;
8041 }
8042
8043 /**
8044 * Get the intent to launch inside of an activity view when displaying this
8045 * notification. This {@code PendingIntent} should be for an activity.
8046 */
8047 public PendingIntent getDisplayIntent() {
8048 return mDisplayIntent;
8049 }
8050
8051 /**
8052 * Add an additional page of content to display with this notification. The current
8053 * notification forms the first page, and pages added using this function form
8054 * subsequent pages. This field can be used to separate a notification into multiple
8055 * sections.
8056 *
8057 * @param page the notification to add as another page
8058 * @return this object for method chaining
8059 * @see android.app.Notification.WearableExtender#getPages
8060 */
8061 public WearableExtender addPage(Notification page) {
8062 mPages.add(page);
8063 return this;
8064 }
8065
8066 /**
8067 * Add additional pages of content to display with this notification. The current
8068 * notification forms the first page, and pages added using this function form
8069 * subsequent pages. This field can be used to separate a notification into multiple
8070 * sections.
8071 *
8072 * @param pages a list of notifications
8073 * @return this object for method chaining
8074 * @see android.app.Notification.WearableExtender#getPages
8075 */
8076 public WearableExtender addPages(List<Notification> pages) {
8077 mPages.addAll(pages);
8078 return this;
8079 }
8080
8081 /**
8082 * Clear all additional pages present on this builder.
8083 * @return this object for method chaining.
8084 * @see #addPage
8085 */
8086 public WearableExtender clearPages() {
8087 mPages.clear();
8088 return this;
8089 }
8090
8091 /**
8092 * Get the array of additional pages of content for displaying this notification. The
8093 * current notification forms the first page, and elements within this array form
8094 * subsequent pages. This field can be used to separate a notification into multiple
8095 * sections.
8096 * @return the pages for this notification
8097 */
8098 public List<Notification> getPages() {
8099 return mPages;
8100 }
8101
8102 /**
8103 * Set a background image to be displayed behind the notification content.
8104 * Contrary to the {@link android.app.Notification.BigPictureStyle}, this background
8105 * will work with any notification style.
8106 *
8107 * @param background the background bitmap
8108 * @return this object for method chaining
8109 * @see android.app.Notification.WearableExtender#getBackground
8110 */
8111 public WearableExtender setBackground(Bitmap background) {
8112 mBackground = background;
8113 return this;
8114 }
8115
8116 /**
8117 * Get a background image to be displayed behind the notification content.
8118 * Contrary to the {@link android.app.Notification.BigPictureStyle}, this background
8119 * will work with any notification style.
8120 *
8121 * @return the background image
8122 * @see android.app.Notification.WearableExtender#setBackground
8123 */
8124 public Bitmap getBackground() {
8125 return mBackground;
8126 }
8127
8128 /**
8129 * Set an icon that goes with the content of this notification.
8130 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05008131 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07008132 public WearableExtender setContentIcon(int icon) {
8133 mContentIcon = icon;
8134 return this;
8135 }
8136
8137 /**
8138 * Get an icon that goes with the content of this notification.
8139 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05008140 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07008141 public int getContentIcon() {
8142 return mContentIcon;
8143 }
8144
8145 /**
8146 * Set the gravity that the content icon should have within the notification display.
8147 * Supported values include {@link android.view.Gravity#START} and
8148 * {@link android.view.Gravity#END}. The default value is {@link android.view.Gravity#END}.
8149 * @see #setContentIcon
8150 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05008151 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07008152 public WearableExtender setContentIconGravity(int contentIconGravity) {
8153 mContentIconGravity = contentIconGravity;
8154 return this;
8155 }
8156
8157 /**
8158 * Get the gravity that the content icon should have within the notification display.
8159 * Supported values include {@link android.view.Gravity#START} and
8160 * {@link android.view.Gravity#END}. The default value is {@link android.view.Gravity#END}.
8161 * @see #getContentIcon
8162 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05008163 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07008164 public int getContentIconGravity() {
8165 return mContentIconGravity;
8166 }
8167
8168 /**
8169 * Set an action from this notification's actions to be clickable with the content of
Griff Hazen14f57992014-05-26 09:07:14 -07008170 * this notification. This action will no longer display separately from the
8171 * notification's content.
8172 *
Griff Hazenca48d352014-05-28 22:37:13 -07008173 * <p>For notifications with multiple pages, child pages can also have content actions
Griff Hazen14f57992014-05-26 09:07:14 -07008174 * set, although the list of available actions comes from the main notification and not
8175 * from the child page's notification.
8176 *
8177 * @param actionIndex The index of the action to hoist onto the current notification page.
8178 * If wearable actions were added to the main notification, this index
8179 * will apply to that list, otherwise it will apply to the regular
8180 * actions list.
Griff Hazen61a9e862014-05-22 16:05:19 -07008181 */
8182 public WearableExtender setContentAction(int actionIndex) {
8183 mContentActionIndex = actionIndex;
8184 return this;
8185 }
8186
8187 /**
Griff Hazenca48d352014-05-28 22:37:13 -07008188 * Get the index of the notification action, if any, that was specified as being clickable
8189 * with the content of this notification. This action will no longer display separately
Griff Hazen14f57992014-05-26 09:07:14 -07008190 * from the notification's content.
Griff Hazen61a9e862014-05-22 16:05:19 -07008191 *
Griff Hazenca48d352014-05-28 22:37:13 -07008192 * <p>For notifications with multiple pages, child pages can also have content actions
Griff Hazen14f57992014-05-26 09:07:14 -07008193 * set, although the list of available actions comes from the main notification and not
8194 * from the child page's notification.
8195 *
8196 * <p>If wearable specific actions were added to the main notification, this index will
8197 * apply to that list, otherwise it will apply to the regular actions list.
Griff Hazenca48d352014-05-28 22:37:13 -07008198 *
8199 * @return the action index or {@link #UNSET_ACTION_INDEX} if no action was selected.
Griff Hazen61a9e862014-05-22 16:05:19 -07008200 */
8201 public int getContentAction() {
8202 return mContentActionIndex;
8203 }
8204
8205 /**
8206 * Set the gravity that this notification should have within the available viewport space.
8207 * Supported values include {@link android.view.Gravity#TOP},
8208 * {@link android.view.Gravity#CENTER_VERTICAL} and {@link android.view.Gravity#BOTTOM}.
8209 * The default value is {@link android.view.Gravity#BOTTOM}.
8210 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05008211 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07008212 public WearableExtender setGravity(int gravity) {
8213 mGravity = gravity;
8214 return this;
8215 }
8216
8217 /**
8218 * Get the gravity that this notification should have within the available viewport space.
8219 * Supported values include {@link android.view.Gravity#TOP},
8220 * {@link android.view.Gravity#CENTER_VERTICAL} and {@link android.view.Gravity#BOTTOM}.
8221 * The default value is {@link android.view.Gravity#BOTTOM}.
8222 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05008223 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07008224 public int getGravity() {
8225 return mGravity;
8226 }
8227
8228 /**
8229 * Set the custom size preset for the display of this notification out of the available
8230 * presets found in {@link android.app.Notification.WearableExtender}, e.g.
8231 * {@link #SIZE_LARGE}.
8232 * <p>Some custom size presets are only applicable for custom display notifications created
8233 * using {@link android.app.Notification.WearableExtender#setDisplayIntent}. Check the
8234 * documentation for the preset in question. See also
8235 * {@link #setCustomContentHeight} and {@link #getCustomSizePreset}.
8236 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05008237 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07008238 public WearableExtender setCustomSizePreset(int sizePreset) {
8239 mCustomSizePreset = sizePreset;
8240 return this;
8241 }
8242
8243 /**
8244 * Get the custom size preset for the display of this notification out of the available
8245 * presets found in {@link android.app.Notification.WearableExtender}, e.g.
8246 * {@link #SIZE_LARGE}.
8247 * <p>Some custom size presets are only applicable for custom display notifications created
8248 * using {@link #setDisplayIntent}. Check the documentation for the preset in question.
8249 * See also {@link #setCustomContentHeight} and {@link #setCustomSizePreset}.
8250 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05008251 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07008252 public int getCustomSizePreset() {
8253 return mCustomSizePreset;
8254 }
8255
8256 /**
8257 * Set the custom height in pixels for the display of this notification's content.
8258 * <p>This option is only available for custom display notifications created
8259 * using {@link android.app.Notification.WearableExtender#setDisplayIntent}. See also
8260 * {@link android.app.Notification.WearableExtender#setCustomSizePreset} and
8261 * {@link #getCustomContentHeight}.
8262 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05008263 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07008264 public WearableExtender setCustomContentHeight(int height) {
8265 mCustomContentHeight = height;
8266 return this;
8267 }
8268
8269 /**
8270 * Get the custom height in pixels for the display of this notification's content.
8271 * <p>This option is only available for custom display notifications created
8272 * using {@link #setDisplayIntent}. See also {@link #setCustomSizePreset} and
8273 * {@link #setCustomContentHeight}.
8274 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05008275 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07008276 public int getCustomContentHeight() {
8277 return mCustomContentHeight;
8278 }
8279
8280 /**
8281 * Set whether the scrolling position for the contents of this notification should start
8282 * at the bottom of the contents instead of the top when the contents are too long to
8283 * display within the screen. Default is false (start scroll at the top).
8284 */
8285 public WearableExtender setStartScrollBottom(boolean startScrollBottom) {
8286 setFlag(FLAG_START_SCROLL_BOTTOM, startScrollBottom);
8287 return this;
8288 }
8289
8290 /**
8291 * Get whether the scrolling position for the contents of this notification should start
8292 * at the bottom of the contents instead of the top when the contents are too long to
8293 * display within the screen. Default is false (start scroll at the top).
8294 */
8295 public boolean getStartScrollBottom() {
8296 return (mFlags & FLAG_START_SCROLL_BOTTOM) != 0;
8297 }
8298
8299 /**
8300 * Set whether the content intent is available when the wearable device is not connected
8301 * to a companion device. The user can still trigger this intent when the wearable device
8302 * is offline, but a visual hint will indicate that the content intent may not be available.
8303 * Defaults to true.
8304 */
8305 public WearableExtender setContentIntentAvailableOffline(
8306 boolean contentIntentAvailableOffline) {
8307 setFlag(FLAG_CONTENT_INTENT_AVAILABLE_OFFLINE, contentIntentAvailableOffline);
8308 return this;
8309 }
8310
8311 /**
8312 * Get whether the content intent is available when the wearable device is not connected
8313 * to a companion device. The user can still trigger this intent when the wearable device
8314 * is offline, but a visual hint will indicate that the content intent may not be available.
8315 * Defaults to true.
8316 */
8317 public boolean getContentIntentAvailableOffline() {
8318 return (mFlags & FLAG_CONTENT_INTENT_AVAILABLE_OFFLINE) != 0;
8319 }
8320
8321 /**
8322 * Set a hint that this notification's icon should not be displayed.
8323 * @param hintHideIcon {@code true} to hide the icon, {@code false} otherwise.
8324 * @return this object for method chaining
8325 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05008326 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07008327 public WearableExtender setHintHideIcon(boolean hintHideIcon) {
8328 setFlag(FLAG_HINT_HIDE_ICON, hintHideIcon);
8329 return this;
8330 }
8331
8332 /**
8333 * Get a hint that this notification's icon should not be displayed.
8334 * @return {@code true} if this icon should not be displayed, false otherwise.
8335 * The default value is {@code false} if this was never set.
8336 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05008337 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07008338 public boolean getHintHideIcon() {
8339 return (mFlags & FLAG_HINT_HIDE_ICON) != 0;
8340 }
8341
8342 /**
8343 * Set a visual hint that only the background image of this notification should be
8344 * displayed, and other semantic content should be hidden. This hint is only applicable
8345 * to sub-pages added using {@link #addPage}.
8346 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05008347 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07008348 public WearableExtender setHintShowBackgroundOnly(boolean hintShowBackgroundOnly) {
8349 setFlag(FLAG_HINT_SHOW_BACKGROUND_ONLY, hintShowBackgroundOnly);
8350 return this;
8351 }
8352
8353 /**
8354 * Get a visual hint that only the background image of this notification should be
8355 * displayed, and other semantic content should be hidden. This hint is only applicable
8356 * to sub-pages added using {@link android.app.Notification.WearableExtender#addPage}.
8357 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05008358 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07008359 public boolean getHintShowBackgroundOnly() {
8360 return (mFlags & FLAG_HINT_SHOW_BACKGROUND_ONLY) != 0;
8361 }
8362
Griff Hazen5f2edfc2014-09-29 16:28:44 -07008363 /**
Griff Hazen9c5be4e2014-11-17 17:47:50 -08008364 * Set a hint that this notification's background should not be clipped if possible,
8365 * and should instead be resized to fully display on the screen, retaining the aspect
8366 * ratio of the image. This can be useful for images like barcodes or qr codes.
Griff Hazen5f2edfc2014-09-29 16:28:44 -07008367 * @param hintAvoidBackgroundClipping {@code true} to avoid clipping if possible.
8368 * @return this object for method chaining
8369 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05008370 @Deprecated
Griff Hazen5f2edfc2014-09-29 16:28:44 -07008371 public WearableExtender setHintAvoidBackgroundClipping(
8372 boolean hintAvoidBackgroundClipping) {
8373 setFlag(FLAG_HINT_AVOID_BACKGROUND_CLIPPING, hintAvoidBackgroundClipping);
8374 return this;
8375 }
8376
8377 /**
Griff Hazen9c5be4e2014-11-17 17:47:50 -08008378 * Get a hint that this notification's background should not be clipped if possible,
8379 * and should instead be resized to fully display on the screen, retaining the aspect
8380 * ratio of the image. This can be useful for images like barcodes or qr codes.
Griff Hazen5f2edfc2014-09-29 16:28:44 -07008381 * @return {@code true} if it's ok if the background is clipped on the screen, false
8382 * otherwise. The default value is {@code false} if this was never set.
8383 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05008384 @Deprecated
Griff Hazen5f2edfc2014-09-29 16:28:44 -07008385 public boolean getHintAvoidBackgroundClipping() {
8386 return (mFlags & FLAG_HINT_AVOID_BACKGROUND_CLIPPING) != 0;
8387 }
8388
8389 /**
8390 * Set a hint that the screen should remain on for at least this duration when
8391 * this notification is displayed on the screen.
8392 * @param timeout The requested screen timeout in milliseconds. Can also be either
8393 * {@link #SCREEN_TIMEOUT_SHORT} or {@link #SCREEN_TIMEOUT_LONG}.
8394 * @return this object for method chaining
8395 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05008396 @Deprecated
Griff Hazen5f2edfc2014-09-29 16:28:44 -07008397 public WearableExtender setHintScreenTimeout(int timeout) {
8398 mHintScreenTimeout = timeout;
8399 return this;
8400 }
8401
8402 /**
8403 * Get the duration, in milliseconds, that the screen should remain on for
8404 * when this notification is displayed.
8405 * @return the duration in milliseconds if > 0, or either one of the sentinel values
8406 * {@link #SCREEN_TIMEOUT_SHORT} or {@link #SCREEN_TIMEOUT_LONG}.
8407 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05008408 @Deprecated
Griff Hazen5f2edfc2014-09-29 16:28:44 -07008409 public int getHintScreenTimeout() {
8410 return mHintScreenTimeout;
8411 }
8412
Alex Hills9ab3a232016-04-05 14:54:56 -04008413 /**
Alex Hills4bcb06b2016-04-05 14:26:25 -04008414 * Set a hint that this notification's {@link BigPictureStyle} (if present) should be
8415 * converted to low-bit and displayed in ambient mode, especially useful for barcodes and
8416 * qr codes, as well as other simple black-and-white tickets.
8417 * @param hintAmbientBigPicture {@code true} to enable converstion and ambient.
8418 * @return this object for method chaining
8419 */
8420 public WearableExtender setHintAmbientBigPicture(boolean hintAmbientBigPicture) {
8421 setFlag(FLAG_BIG_PICTURE_AMBIENT, hintAmbientBigPicture);
8422 return this;
8423 }
8424
8425 /**
8426 * Get a hint that this notification's {@link BigPictureStyle} (if present) should be
8427 * converted to low-bit and displayed in ambient mode, especially useful for barcodes and
8428 * qr codes, as well as other simple black-and-white tickets.
8429 * @return {@code true} if it should be displayed in ambient, false otherwise
8430 * otherwise. The default value is {@code false} if this was never set.
8431 */
8432 public boolean getHintAmbientBigPicture() {
8433 return (mFlags & FLAG_BIG_PICTURE_AMBIENT) != 0;
8434 }
8435
8436 /**
Alex Hills9ab3a232016-04-05 14:54:56 -04008437 * Set a hint that this notification's content intent will launch an {@link Activity}
8438 * directly, telling the platform that it can generate the appropriate transitions.
8439 * @param hintContentIntentLaunchesActivity {@code true} if the content intent will launch
8440 * an activity and transitions should be generated, false otherwise.
8441 * @return this object for method chaining
8442 */
8443 public WearableExtender setHintContentIntentLaunchesActivity(
8444 boolean hintContentIntentLaunchesActivity) {
8445 setFlag(FLAG_HINT_CONTENT_INTENT_LAUNCHES_ACTIVITY, hintContentIntentLaunchesActivity);
8446 return this;
8447 }
8448
8449 /**
8450 * Get a hint that this notification's content intent will launch an {@link Activity}
8451 * directly, telling the platform that it can generate the appropriate transitions
8452 * @return {@code true} if the content intent will launch an activity and transitions should
8453 * be generated, false otherwise. The default value is {@code false} if this was never set.
8454 */
8455 public boolean getHintContentIntentLaunchesActivity() {
8456 return (mFlags & FLAG_HINT_CONTENT_INTENT_LAUNCHES_ACTIVITY) != 0;
8457 }
8458
Nadia Benbernou948627e2016-04-14 14:41:08 -04008459 /**
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04008460 * Sets the dismissal id for this notification. If a notification is posted with a
8461 * dismissal id, then when that notification is canceled, notifications on other wearables
8462 * and the paired Android phone having that same dismissal id will also be canceled. See
Nadia Benbernou948627e2016-04-14 14:41:08 -04008463 * <a href="{@docRoot}wear/notifications/index.html">Adding Wearable Features to
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04008464 * Notifications</a> for more information.
Nadia Benbernou948627e2016-04-14 14:41:08 -04008465 * @param dismissalId the dismissal id of the notification.
8466 * @return this object for method chaining
8467 */
8468 public WearableExtender setDismissalId(String dismissalId) {
8469 mDismissalId = dismissalId;
8470 return this;
8471 }
8472
8473 /**
8474 * Returns the dismissal id of the notification.
8475 * @return the dismissal id of the notification or null if it has not been set.
8476 */
8477 public String getDismissalId() {
8478 return mDismissalId;
8479 }
8480
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04008481 /**
8482 * Sets a bridge tag for this notification. A bridge tag can be set for notifications
8483 * posted from a phone to provide finer-grained control on what notifications are bridged
8484 * to wearables. See <a href="{@docRoot}wear/notifications/index.html">Adding Wearable
8485 * Features to Notifications</a> for more information.
8486 * @param bridgeTag the bridge tag of the notification.
8487 * @return this object for method chaining
8488 */
8489 public WearableExtender setBridgeTag(String bridgeTag) {
8490 mBridgeTag = bridgeTag;
8491 return this;
8492 }
8493
8494 /**
8495 * Returns the bridge tag of the notification.
8496 * @return the bridge tag or null if not present.
8497 */
8498 public String getBridgeTag() {
8499 return mBridgeTag;
8500 }
8501
Griff Hazen61a9e862014-05-22 16:05:19 -07008502 private void setFlag(int mask, boolean value) {
8503 if (value) {
8504 mFlags |= mask;
8505 } else {
8506 mFlags &= ~mask;
8507 }
8508 }
8509 }
8510
8511 /**
Zhen Yu Song9d5528b2014-11-14 15:34:39 -08008512 * <p>Helper class to add Android Auto extensions to notifications. To create a notification
8513 * with car extensions:
8514 *
8515 * <ol>
8516 * <li>Create an {@link Notification.Builder}, setting any desired
8517 * properties.
8518 * <li>Create a {@link CarExtender}.
8519 * <li>Set car-specific properties using the {@code add} and {@code set} methods of
8520 * {@link CarExtender}.
8521 * <li>Call {@link Notification.Builder#extend(Notification.Extender)}
8522 * to apply the extensions to a notification.
8523 * </ol>
8524 *
8525 * <pre class="prettyprint">
8526 * Notification notification = new Notification.Builder(context)
8527 * ...
8528 * .extend(new CarExtender()
8529 * .set*(...))
8530 * .build();
8531 * </pre>
8532 *
8533 * <p>Car extensions can be accessed on an existing notification by using the
8534 * {@code CarExtender(Notification)} constructor, and then using the {@code get} methods
8535 * to access values.
8536 */
8537 public static final class CarExtender implements Extender {
8538 private static final String TAG = "CarExtender";
8539
8540 private static final String EXTRA_CAR_EXTENDER = "android.car.EXTENSIONS";
8541 private static final String EXTRA_LARGE_ICON = "large_icon";
8542 private static final String EXTRA_CONVERSATION = "car_conversation";
8543 private static final String EXTRA_COLOR = "app_color";
8544
8545 private Bitmap mLargeIcon;
8546 private UnreadConversation mUnreadConversation;
8547 private int mColor = Notification.COLOR_DEFAULT;
8548
8549 /**
8550 * Create a {@link CarExtender} with default options.
8551 */
8552 public CarExtender() {
8553 }
8554
8555 /**
8556 * Create a {@link CarExtender} from the CarExtender options of an existing Notification.
8557 *
8558 * @param notif The notification from which to copy options.
8559 */
8560 public CarExtender(Notification notif) {
8561 Bundle carBundle = notif.extras == null ?
8562 null : notif.extras.getBundle(EXTRA_CAR_EXTENDER);
8563 if (carBundle != null) {
8564 mLargeIcon = carBundle.getParcelable(EXTRA_LARGE_ICON);
8565 mColor = carBundle.getInt(EXTRA_COLOR, Notification.COLOR_DEFAULT);
8566
8567 Bundle b = carBundle.getBundle(EXTRA_CONVERSATION);
8568 mUnreadConversation = UnreadConversation.getUnreadConversationFromBundle(b);
8569 }
8570 }
8571
8572 /**
8573 * Apply car extensions to a notification that is being built. This is typically called by
8574 * the {@link Notification.Builder#extend(Notification.Extender)}
8575 * method of {@link Notification.Builder}.
8576 */
8577 @Override
8578 public Notification.Builder extend(Notification.Builder builder) {
8579 Bundle carExtensions = new Bundle();
8580
8581 if (mLargeIcon != null) {
8582 carExtensions.putParcelable(EXTRA_LARGE_ICON, mLargeIcon);
8583 }
8584 if (mColor != Notification.COLOR_DEFAULT) {
8585 carExtensions.putInt(EXTRA_COLOR, mColor);
8586 }
8587
8588 if (mUnreadConversation != null) {
8589 Bundle b = mUnreadConversation.getBundleForUnreadConversation();
8590 carExtensions.putBundle(EXTRA_CONVERSATION, b);
8591 }
8592
8593 builder.getExtras().putBundle(EXTRA_CAR_EXTENDER, carExtensions);
8594 return builder;
8595 }
8596
8597 /**
8598 * Sets the accent color to use when Android Auto presents the notification.
8599 *
8600 * Android Auto uses the color set with {@link Notification.Builder#setColor(int)}
8601 * to accent the displayed notification. However, not all colors are acceptable in an
8602 * automotive setting. This method can be used to override the color provided in the
8603 * notification in such a situation.
8604 */
Tor Norbye80756e32015-03-02 09:39:27 -08008605 public CarExtender setColor(@ColorInt int color) {
Zhen Yu Song9d5528b2014-11-14 15:34:39 -08008606 mColor = color;
8607 return this;
8608 }
8609
8610 /**
8611 * Gets the accent color.
8612 *
Julia Reynoldsd9228f12015-10-20 10:37:27 -04008613 * @see #setColor
Zhen Yu Song9d5528b2014-11-14 15:34:39 -08008614 */
Tor Norbye80756e32015-03-02 09:39:27 -08008615 @ColorInt
Zhen Yu Song9d5528b2014-11-14 15:34:39 -08008616 public int getColor() {
8617 return mColor;
8618 }
8619
8620 /**
8621 * Sets the large icon of the car notification.
8622 *
8623 * If no large icon is set in the extender, Android Auto will display the icon
8624 * specified by {@link Notification.Builder#setLargeIcon(android.graphics.Bitmap)}
8625 *
8626 * @param largeIcon The large icon to use in the car notification.
8627 * @return This object for method chaining.
8628 */
8629 public CarExtender setLargeIcon(Bitmap largeIcon) {
8630 mLargeIcon = largeIcon;
8631 return this;
8632 }
8633
8634 /**
8635 * Gets the large icon used in this car notification, or null if no icon has been set.
8636 *
8637 * @return The large icon for the car notification.
8638 * @see CarExtender#setLargeIcon
8639 */
8640 public Bitmap getLargeIcon() {
8641 return mLargeIcon;
8642 }
8643
8644 /**
8645 * Sets the unread conversation in a message notification.
8646 *
8647 * @param unreadConversation The unread part of the conversation this notification conveys.
8648 * @return This object for method chaining.
8649 */
8650 public CarExtender setUnreadConversation(UnreadConversation unreadConversation) {
8651 mUnreadConversation = unreadConversation;
8652 return this;
8653 }
8654
8655 /**
8656 * Returns the unread conversation conveyed by this notification.
8657 * @see #setUnreadConversation(UnreadConversation)
8658 */
8659 public UnreadConversation getUnreadConversation() {
8660 return mUnreadConversation;
8661 }
8662
8663 /**
8664 * A class which holds the unread messages from a conversation.
8665 */
8666 public static class UnreadConversation {
8667 private static final String KEY_AUTHOR = "author";
8668 private static final String KEY_TEXT = "text";
8669 private static final String KEY_MESSAGES = "messages";
8670 private static final String KEY_REMOTE_INPUT = "remote_input";
8671 private static final String KEY_ON_REPLY = "on_reply";
8672 private static final String KEY_ON_READ = "on_read";
8673 private static final String KEY_PARTICIPANTS = "participants";
8674 private static final String KEY_TIMESTAMP = "timestamp";
8675
8676 private final String[] mMessages;
8677 private final RemoteInput mRemoteInput;
8678 private final PendingIntent mReplyPendingIntent;
8679 private final PendingIntent mReadPendingIntent;
8680 private final String[] mParticipants;
8681 private final long mLatestTimestamp;
8682
8683 UnreadConversation(String[] messages, RemoteInput remoteInput,
8684 PendingIntent replyPendingIntent, PendingIntent readPendingIntent,
8685 String[] participants, long latestTimestamp) {
8686 mMessages = messages;
8687 mRemoteInput = remoteInput;
8688 mReadPendingIntent = readPendingIntent;
8689 mReplyPendingIntent = replyPendingIntent;
8690 mParticipants = participants;
8691 mLatestTimestamp = latestTimestamp;
8692 }
8693
8694 /**
8695 * Gets the list of messages conveyed by this notification.
8696 */
8697 public String[] getMessages() {
8698 return mMessages;
8699 }
8700
8701 /**
8702 * Gets the remote input that will be used to convey the response to a message list, or
8703 * null if no such remote input exists.
8704 */
8705 public RemoteInput getRemoteInput() {
8706 return mRemoteInput;
8707 }
8708
8709 /**
8710 * Gets the pending intent that will be triggered when the user replies to this
8711 * notification.
8712 */
8713 public PendingIntent getReplyPendingIntent() {
8714 return mReplyPendingIntent;
8715 }
8716
8717 /**
8718 * Gets the pending intent that Android Auto will send after it reads aloud all messages
8719 * in this object's message list.
8720 */
8721 public PendingIntent getReadPendingIntent() {
8722 return mReadPendingIntent;
8723 }
8724
8725 /**
8726 * Gets the participants in the conversation.
8727 */
8728 public String[] getParticipants() {
8729 return mParticipants;
8730 }
8731
8732 /**
8733 * Gets the firs participant in the conversation.
8734 */
8735 public String getParticipant() {
8736 return mParticipants.length > 0 ? mParticipants[0] : null;
8737 }
8738
8739 /**
8740 * Gets the timestamp of the conversation.
8741 */
8742 public long getLatestTimestamp() {
8743 return mLatestTimestamp;
8744 }
8745
8746 Bundle getBundleForUnreadConversation() {
8747 Bundle b = new Bundle();
8748 String author = null;
8749 if (mParticipants != null && mParticipants.length > 1) {
8750 author = mParticipants[0];
8751 }
8752 Parcelable[] messages = new Parcelable[mMessages.length];
8753 for (int i = 0; i < messages.length; i++) {
8754 Bundle m = new Bundle();
8755 m.putString(KEY_TEXT, mMessages[i]);
8756 m.putString(KEY_AUTHOR, author);
8757 messages[i] = m;
8758 }
8759 b.putParcelableArray(KEY_MESSAGES, messages);
8760 if (mRemoteInput != null) {
8761 b.putParcelable(KEY_REMOTE_INPUT, mRemoteInput);
8762 }
8763 b.putParcelable(KEY_ON_REPLY, mReplyPendingIntent);
8764 b.putParcelable(KEY_ON_READ, mReadPendingIntent);
8765 b.putStringArray(KEY_PARTICIPANTS, mParticipants);
8766 b.putLong(KEY_TIMESTAMP, mLatestTimestamp);
8767 return b;
8768 }
8769
8770 static UnreadConversation getUnreadConversationFromBundle(Bundle b) {
8771 if (b == null) {
8772 return null;
8773 }
8774 Parcelable[] parcelableMessages = b.getParcelableArray(KEY_MESSAGES);
8775 String[] messages = null;
8776 if (parcelableMessages != null) {
8777 String[] tmp = new String[parcelableMessages.length];
8778 boolean success = true;
8779 for (int i = 0; i < tmp.length; i++) {
8780 if (!(parcelableMessages[i] instanceof Bundle)) {
8781 success = false;
8782 break;
8783 }
8784 tmp[i] = ((Bundle) parcelableMessages[i]).getString(KEY_TEXT);
8785 if (tmp[i] == null) {
8786 success = false;
8787 break;
8788 }
8789 }
8790 if (success) {
8791 messages = tmp;
8792 } else {
8793 return null;
8794 }
8795 }
8796
8797 PendingIntent onRead = b.getParcelable(KEY_ON_READ);
8798 PendingIntent onReply = b.getParcelable(KEY_ON_REPLY);
8799
8800 RemoteInput remoteInput = b.getParcelable(KEY_REMOTE_INPUT);
8801
8802 String[] participants = b.getStringArray(KEY_PARTICIPANTS);
8803 if (participants == null || participants.length != 1) {
8804 return null;
8805 }
8806
8807 return new UnreadConversation(messages,
8808 remoteInput,
8809 onReply,
8810 onRead,
8811 participants, b.getLong(KEY_TIMESTAMP));
8812 }
8813 };
8814
8815 /**
8816 * Builder class for {@link CarExtender.UnreadConversation} objects.
8817 */
8818 public static class Builder {
8819 private final List<String> mMessages = new ArrayList<String>();
8820 private final String mParticipant;
8821 private RemoteInput mRemoteInput;
8822 private PendingIntent mReadPendingIntent;
8823 private PendingIntent mReplyPendingIntent;
8824 private long mLatestTimestamp;
8825
8826 /**
8827 * Constructs a new builder for {@link CarExtender.UnreadConversation}.
8828 *
8829 * @param name The name of the other participant in the conversation.
8830 */
8831 public Builder(String name) {
8832 mParticipant = name;
8833 }
8834
8835 /**
8836 * Appends a new unread message to the list of messages for this conversation.
8837 *
8838 * The messages should be added from oldest to newest.
8839 *
8840 * @param message The text of the new unread message.
8841 * @return This object for method chaining.
8842 */
8843 public Builder addMessage(String message) {
8844 mMessages.add(message);
8845 return this;
8846 }
8847
8848 /**
8849 * Sets the pending intent and remote input which will convey the reply to this
8850 * notification.
8851 *
8852 * @param pendingIntent The pending intent which will be triggered on a reply.
8853 * @param remoteInput The remote input parcelable which will carry the reply.
8854 * @return This object for method chaining.
8855 *
8856 * @see CarExtender.UnreadConversation#getRemoteInput
8857 * @see CarExtender.UnreadConversation#getReplyPendingIntent
8858 */
8859 public Builder setReplyAction(
8860 PendingIntent pendingIntent, RemoteInput remoteInput) {
8861 mRemoteInput = remoteInput;
8862 mReplyPendingIntent = pendingIntent;
8863
8864 return this;
8865 }
8866
8867 /**
8868 * Sets the pending intent that will be sent once the messages in this notification
8869 * are read.
8870 *
8871 * @param pendingIntent The pending intent to use.
8872 * @return This object for method chaining.
8873 */
8874 public Builder setReadPendingIntent(PendingIntent pendingIntent) {
8875 mReadPendingIntent = pendingIntent;
8876 return this;
8877 }
8878
8879 /**
8880 * Sets the timestamp of the most recent message in an unread conversation.
8881 *
8882 * If a messaging notification has been posted by your application and has not
8883 * yet been cancelled, posting a later notification with the same id and tag
8884 * but without a newer timestamp may result in Android Auto not displaying a
8885 * heads up notification for the later notification.
8886 *
8887 * @param timestamp The timestamp of the most recent message in the conversation.
8888 * @return This object for method chaining.
8889 */
8890 public Builder setLatestTimestamp(long timestamp) {
8891 mLatestTimestamp = timestamp;
8892 return this;
8893 }
8894
8895 /**
8896 * Builds a new unread conversation object.
8897 *
8898 * @return The new unread conversation object.
8899 */
8900 public UnreadConversation build() {
8901 String[] messages = mMessages.toArray(new String[mMessages.size()]);
8902 String[] participants = { mParticipant };
8903 return new UnreadConversation(messages, mRemoteInput, mReplyPendingIntent,
8904 mReadPendingIntent, participants, mLatestTimestamp);
8905 }
8906 }
8907 }
8908
8909 /**
Dmitri Plotnikov9e9cfd92016-12-21 10:52:26 -08008910 * <p>Helper class to add Android TV extensions to notifications. To create a notification
8911 * with a TV extension:
8912 *
8913 * <ol>
8914 * <li>Create an {@link Notification.Builder}, setting any desired properties.
8915 * <li>Create a {@link TvExtender}.
8916 * <li>Set TV-specific properties using the {@code set} methods of
8917 * {@link TvExtender}.
8918 * <li>Call {@link Notification.Builder#extend(Notification.Extender)}
8919 * to apply the extension to a notification.
8920 * </ol>
8921 *
8922 * <pre class="prettyprint">
8923 * Notification notification = new Notification.Builder(context)
8924 * ...
8925 * .extend(new TvExtender()
8926 * .set*(...))
8927 * .build();
8928 * </pre>
8929 *
8930 * <p>TV extensions can be accessed on an existing notification by using the
8931 * {@code TvExtender(Notification)} constructor, and then using the {@code get} methods
8932 * to access values.
8933 *
8934 * @hide
8935 */
8936 @SystemApi
8937 public static final class TvExtender implements Extender {
8938 private static final String TAG = "TvExtender";
8939
8940 private static final String EXTRA_TV_EXTENDER = "android.tv.EXTENSIONS";
8941 private static final String EXTRA_FLAGS = "flags";
8942 private static final String EXTRA_CONTENT_INTENT = "content_intent";
8943 private static final String EXTRA_DELETE_INTENT = "delete_intent";
Dmitri Plotnikovb8a04ee2017-01-20 10:39:22 -08008944 private static final String EXTRA_CHANNEL_ID = "channel_id";
Rhiannon Malia1a083932018-01-24 15:02:30 -08008945 private static final String EXTRA_SUPPRESS_SHOW_OVER_APPS = "suppressShowOverApps";
Dmitri Plotnikov9e9cfd92016-12-21 10:52:26 -08008946
8947 // Flags bitwise-ored to mFlags
8948 private static final int FLAG_AVAILABLE_ON_TV = 0x1;
8949
8950 private int mFlags;
Dmitri Plotnikovb8a04ee2017-01-20 10:39:22 -08008951 private String mChannelId;
Dmitri Plotnikov9e9cfd92016-12-21 10:52:26 -08008952 private PendingIntent mContentIntent;
8953 private PendingIntent mDeleteIntent;
Rhiannon Malia1a083932018-01-24 15:02:30 -08008954 private boolean mSuppressShowOverApps;
Dmitri Plotnikov9e9cfd92016-12-21 10:52:26 -08008955
8956 /**
8957 * Create a {@link TvExtender} with default options.
8958 */
8959 public TvExtender() {
8960 mFlags = FLAG_AVAILABLE_ON_TV;
8961 }
8962
8963 /**
8964 * Create a {@link TvExtender} from the TvExtender options of an existing Notification.
8965 *
8966 * @param notif The notification from which to copy options.
8967 */
8968 public TvExtender(Notification notif) {
8969 Bundle bundle = notif.extras == null ?
8970 null : notif.extras.getBundle(EXTRA_TV_EXTENDER);
8971 if (bundle != null) {
8972 mFlags = bundle.getInt(EXTRA_FLAGS);
Dmitri Plotnikovb8a04ee2017-01-20 10:39:22 -08008973 mChannelId = bundle.getString(EXTRA_CHANNEL_ID);
Rhiannon Malia1a083932018-01-24 15:02:30 -08008974 mSuppressShowOverApps = bundle.getBoolean(EXTRA_SUPPRESS_SHOW_OVER_APPS);
Dmitri Plotnikov9e9cfd92016-12-21 10:52:26 -08008975 mContentIntent = bundle.getParcelable(EXTRA_CONTENT_INTENT);
8976 mDeleteIntent = bundle.getParcelable(EXTRA_DELETE_INTENT);
8977 }
8978 }
8979
8980 /**
8981 * Apply a TV extension to a notification that is being built. This is typically called by
8982 * the {@link Notification.Builder#extend(Notification.Extender)}
8983 * method of {@link Notification.Builder}.
8984 */
8985 @Override
8986 public Notification.Builder extend(Notification.Builder builder) {
8987 Bundle bundle = new Bundle();
8988
8989 bundle.putInt(EXTRA_FLAGS, mFlags);
Dmitri Plotnikovb8a04ee2017-01-20 10:39:22 -08008990 bundle.putString(EXTRA_CHANNEL_ID, mChannelId);
Rhiannon Malia1a083932018-01-24 15:02:30 -08008991 bundle.putBoolean(EXTRA_SUPPRESS_SHOW_OVER_APPS, mSuppressShowOverApps);
Dmitri Plotnikov9e9cfd92016-12-21 10:52:26 -08008992 if (mContentIntent != null) {
8993 bundle.putParcelable(EXTRA_CONTENT_INTENT, mContentIntent);
8994 }
8995
8996 if (mDeleteIntent != null) {
8997 bundle.putParcelable(EXTRA_DELETE_INTENT, mDeleteIntent);
8998 }
8999
9000 builder.getExtras().putBundle(EXTRA_TV_EXTENDER, bundle);
9001 return builder;
9002 }
9003
9004 /**
9005 * Returns true if this notification should be shown on TV. This method return true
9006 * if the notification was extended with a TvExtender.
9007 */
9008 public boolean isAvailableOnTv() {
9009 return (mFlags & FLAG_AVAILABLE_ON_TV) != 0;
9010 }
9011
9012 /**
Dmitri Plotnikovb8a04ee2017-01-20 10:39:22 -08009013 * Specifies the channel the notification should be delivered on when shown on TV.
9014 * It can be different from the channel that the notification is delivered to when
9015 * posting on a non-TV device.
9016 */
9017 public TvExtender setChannel(String channelId) {
9018 mChannelId = channelId;
9019 return this;
9020 }
9021
9022 /**
Julia Reynoldsbad42972017-04-25 13:52:49 -04009023 * Specifies the channel the notification should be delivered on when shown on TV.
9024 * It can be different from the channel that the notification is delivered to when
9025 * posting on a non-TV device.
9026 */
9027 public TvExtender setChannelId(String channelId) {
9028 mChannelId = channelId;
9029 return this;
9030 }
9031
Jeff Sharkey000ce802017-04-29 13:13:27 -06009032 /** @removed */
9033 @Deprecated
Dmitri Plotnikovb8a04ee2017-01-20 10:39:22 -08009034 public String getChannel() {
9035 return mChannelId;
9036 }
9037
9038 /**
Julia Reynoldsbad42972017-04-25 13:52:49 -04009039 * Returns the id of the channel this notification posts to on TV.
9040 */
9041 public String getChannelId() {
9042 return mChannelId;
9043 }
9044
9045 /**
Dmitri Plotnikov9e9cfd92016-12-21 10:52:26 -08009046 * Supplies a {@link PendingIntent} to be sent when the notification is selected on TV.
9047 * If provided, it is used instead of the content intent specified
9048 * at the level of Notification.
9049 */
9050 public TvExtender setContentIntent(PendingIntent intent) {
9051 mContentIntent = intent;
9052 return this;
9053 }
9054
9055 /**
9056 * Returns the TV-specific content intent. If this method returns null, the
9057 * main content intent on the notification should be used.
9058 *
9059 * @see {@link Notification#contentIntent}
9060 */
9061 public PendingIntent getContentIntent() {
9062 return mContentIntent;
9063 }
9064
9065 /**
9066 * Supplies a {@link PendingIntent} to send when the notification is cleared explicitly
9067 * by the user on TV. If provided, it is used instead of the delete intent specified
9068 * at the level of Notification.
9069 */
9070 public TvExtender setDeleteIntent(PendingIntent intent) {
9071 mDeleteIntent = intent;
9072 return this;
9073 }
9074
9075 /**
9076 * Returns the TV-specific delete intent. If this method returns null, the
9077 * main delete intent on the notification should be used.
9078 *
9079 * @see {@link Notification#deleteIntent}
9080 */
9081 public PendingIntent getDeleteIntent() {
9082 return mDeleteIntent;
9083 }
Rhiannon Malia1a083932018-01-24 15:02:30 -08009084
9085 /**
9086 * Specifies whether this notification should suppress showing a message over top of apps
9087 * outside of the launcher.
9088 */
9089 public TvExtender setSuppressShowOverApps(boolean suppress) {
9090 mSuppressShowOverApps = suppress;
9091 return this;
9092 }
9093
9094 /**
9095 * Returns true if this notification should not show messages over top of apps
9096 * outside of the launcher.
9097 */
9098 public boolean getSuppressShowOverApps() {
9099 return mSuppressShowOverApps;
9100 }
Dmitri Plotnikov9e9cfd92016-12-21 10:52:26 -08009101 }
9102
9103 /**
Griff Hazen61a9e862014-05-22 16:05:19 -07009104 * Get an array of Notification objects from a parcelable array bundle field.
9105 * Update the bundle to have a typed array so fetches in the future don't need
9106 * to do an array copy.
9107 */
9108 private static Notification[] getNotificationArrayFromBundle(Bundle bundle, String key) {
9109 Parcelable[] array = bundle.getParcelableArray(key);
9110 if (array instanceof Notification[] || array == null) {
9111 return (Notification[]) array;
9112 }
9113 Notification[] typedArray = Arrays.copyOf(array, array.length,
9114 Notification[].class);
9115 bundle.putParcelableArray(key, typedArray);
9116 return typedArray;
9117 }
Christoph Studer4600f9b2014-07-22 22:44:43 +02009118
9119 private static class BuilderRemoteViews extends RemoteViews {
9120 public BuilderRemoteViews(Parcel parcel) {
9121 super(parcel);
9122 }
9123
Kenny Guy77320062014-08-27 21:37:15 +01009124 public BuilderRemoteViews(ApplicationInfo appInfo, int layoutId) {
9125 super(appInfo, layoutId);
Christoph Studer4600f9b2014-07-22 22:44:43 +02009126 }
9127
9128 @Override
9129 public BuilderRemoteViews clone() {
9130 Parcel p = Parcel.obtain();
9131 writeToParcel(p, 0);
9132 p.setDataPosition(0);
9133 BuilderRemoteViews brv = new BuilderRemoteViews(p);
9134 p.recycle();
9135 return brv;
9136 }
9137 }
Adrian Roos70d7aa32017-01-11 15:39:06 -08009138
9139 private static class StandardTemplateParams {
9140 boolean hasProgress = true;
9141 boolean ambient = false;
9142 CharSequence title;
9143 CharSequence text;
Selim Cinekafeed292017-12-12 17:32:44 -08009144 CharSequence headerTextSecondary;
Selim Cinek88188f22017-09-19 16:46:56 -07009145 boolean hideLargeIcon;
9146 public boolean alwaysShowReply;
Adrian Roos70d7aa32017-01-11 15:39:06 -08009147
9148 final StandardTemplateParams reset() {
9149 hasProgress = true;
9150 ambient = false;
9151 title = null;
9152 text = null;
Selim Cinekafeed292017-12-12 17:32:44 -08009153 headerTextSecondary = null;
Adrian Roos70d7aa32017-01-11 15:39:06 -08009154 return this;
9155 }
9156
9157 final StandardTemplateParams hasProgress(boolean hasProgress) {
9158 this.hasProgress = hasProgress;
9159 return this;
9160 }
9161
9162 final StandardTemplateParams title(CharSequence title) {
9163 this.title = title;
9164 return this;
9165 }
9166
9167 final StandardTemplateParams text(CharSequence text) {
9168 this.text = text;
9169 return this;
9170 }
9171
Selim Cinekafeed292017-12-12 17:32:44 -08009172 final StandardTemplateParams headerTextSecondary(CharSequence text) {
9173 this.headerTextSecondary = text;
9174 return this;
9175 }
9176
Selim Cinek88188f22017-09-19 16:46:56 -07009177 final StandardTemplateParams alwaysShowReply(boolean alwaysShowReply) {
9178 this.alwaysShowReply = alwaysShowReply;
9179 return this;
9180 }
9181
9182 final StandardTemplateParams hideLargeIcon(boolean hideLargeIcon) {
9183 this.hideLargeIcon = hideLargeIcon;
9184 return this;
9185 }
9186
Adrian Roos70d7aa32017-01-11 15:39:06 -08009187 final StandardTemplateParams ambient(boolean ambient) {
Adrian Roos0bc3f6a2017-03-06 11:54:05 -08009188 Preconditions.checkState(title == null && text == null, "must set ambient before text");
Adrian Roos70d7aa32017-01-11 15:39:06 -08009189 this.ambient = ambient;
9190 return this;
9191 }
9192
9193 final StandardTemplateParams fillTextsFrom(Builder b) {
9194 Bundle extras = b.mN.extras;
Lucas Dupin06c5e642017-09-13 16:34:58 -07009195 this.title = b.processLegacyText(extras.getCharSequence(EXTRA_TITLE), ambient);
9196
9197 // Big text notifications should contain their content when viewed in ambient mode.
9198 CharSequence text = extras.getCharSequence(EXTRA_BIG_TEXT);
9199 if (!ambient || TextUtils.isEmpty(text)) {
9200 text = extras.getCharSequence(EXTRA_TEXT);
9201 }
9202 this.text = b.processLegacyText(text, ambient);
Adrian Roos70d7aa32017-01-11 15:39:06 -08009203 return this;
9204 }
9205 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009206}