blob: 0b5b363ddecd4d1545ddddab4aea7695364374e7 [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
Daniel Sandlerf3b73432012-03-27 15:01:25 -0400363 /**
Dan Sandler86647982015-05-13 23:41:13 -0400364 * A large bitmap to be shown in the notification content area.
365 *
366 * @deprecated Use {@link Builder#setLargeIcon(Icon)} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800367 */
Dan Sandler86647982015-05-13 23:41:13 -0400368 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -0800369 public Bitmap largeIcon;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800370
371 /**
372 * The sound to play.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500373 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800374 * <p>
Chris Wren47c20a12014-06-18 17:27:29 -0400375 * A notification that is noisy is more likely to be presented as a heads-up notification.
376 * </p>
377 *
378 * <p>
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500379 * To play the default notification sound, see {@link #defaults}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800380 * </p>
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500381 * @deprecated use {@link NotificationChannel#getSound()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800382 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500383 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800384 public Uri sound;
385
386 /**
387 * Use this constant as the value for audioStreamType to request that
388 * the default stream type for notifications be used. Currently the
Jeff Sharkey098d5802012-04-26 17:30:34 -0700389 * default stream type is {@link AudioManager#STREAM_NOTIFICATION}.
John Spurlockc0650f022014-07-19 13:22:39 -0400390 *
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500391 * @deprecated Use {@link NotificationChannel#getAudioAttributes()} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800392 */
Jean-Michel Trivi81f871e2014-08-06 16:32:38 -0700393 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800394 public static final int STREAM_DEFAULT = -1;
395
396 /**
397 * The audio stream type to use when playing the sound.
398 * Should be one of the STREAM_ constants from
399 * {@link android.media.AudioManager}.
John Spurlockc0650f022014-07-19 13:22:39 -0400400 *
401 * @deprecated Use {@link #audioAttributes} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800402 */
Jean-Michel Trivi81f871e2014-08-06 16:32:38 -0700403 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800404 public int audioStreamType = STREAM_DEFAULT;
405
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800406 /**
John Spurlockc0650f022014-07-19 13:22:39 -0400407 * The default value of {@link #audioAttributes}.
408 */
409 public static final AudioAttributes AUDIO_ATTRIBUTES_DEFAULT = new AudioAttributes.Builder()
410 .setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
411 .setUsage(AudioAttributes.USAGE_NOTIFICATION)
412 .build();
413
414 /**
415 * The {@link AudioAttributes audio attributes} to use when playing the sound.
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500416 *
417 * @deprecated use {@link NotificationChannel#getAudioAttributes()} instead.
John Spurlockc0650f022014-07-19 13:22:39 -0400418 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500419 @Deprecated
John Spurlockc0650f022014-07-19 13:22:39 -0400420 public AudioAttributes audioAttributes = AUDIO_ATTRIBUTES_DEFAULT;
421
422 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500423 * The pattern with which to vibrate.
424 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800425 * <p>
426 * To vibrate the default pattern, see {@link #defaults}.
427 * </p>
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500428 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800429 * @see android.os.Vibrator#vibrate(long[],int)
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500430 * @deprecated use {@link NotificationChannel#getVibrationPattern()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800431 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500432 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800433 public long[] vibrate;
434
435 /**
436 * The color of the led. The hardware will do its best approximation.
437 *
438 * @see #FLAG_SHOW_LIGHTS
439 * @see #flags
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500440 * @deprecated use {@link NotificationChannel#shouldShowLights()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800441 */
Tor Norbye80756e32015-03-02 09:39:27 -0800442 @ColorInt
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500443 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800444 public int ledARGB;
445
446 /**
447 * The number of milliseconds for the LED to be on while it's flashing.
448 * The hardware will do its best approximation.
449 *
450 * @see #FLAG_SHOW_LIGHTS
451 * @see #flags
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500452 * @deprecated use {@link NotificationChannel#shouldShowLights()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800453 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500454 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800455 public int ledOnMS;
456
457 /**
458 * The number of milliseconds for the LED to be off while it's flashing.
459 * The hardware will do its best approximation.
460 *
461 * @see #FLAG_SHOW_LIGHTS
462 * @see #flags
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500463 *
464 * @deprecated use {@link NotificationChannel#shouldShowLights()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800465 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500466 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800467 public int ledOffMS;
468
469 /**
470 * Specifies which values should be taken from the defaults.
471 * <p>
472 * To set, OR the desired from {@link #DEFAULT_SOUND},
473 * {@link #DEFAULT_VIBRATE}, {@link #DEFAULT_LIGHTS}. For all default
474 * values, use {@link #DEFAULT_ALL}.
475 * </p>
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500476 *
477 * @deprecated use {@link NotificationChannel#getSound()} and
478 * {@link NotificationChannel#shouldShowLights()} and
479 * {@link NotificationChannel#shouldVibrate()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800480 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500481 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800482 public int defaults;
483
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800484 /**
485 * Bit to be bitwise-ored into the {@link #flags} field that should be
486 * set if you want the LED on for this notification.
487 * <ul>
488 * <li>To turn the LED off, pass 0 in the alpha channel for colorARGB
489 * or 0 for both ledOnMS and ledOffMS.</li>
490 * <li>To turn the LED on, pass 1 for ledOnMS and 0 for ledOffMS.</li>
491 * <li>To flash the LED, pass the number of milliseconds that it should
492 * be on and off to ledOnMS and ledOffMS.</li>
493 * </ul>
494 * <p>
495 * Since hardware varies, you are not guaranteed that any of the values
496 * you pass are honored exactly. Use the system defaults (TODO) if possible
497 * because they will be set to values that work on any given hardware.
498 * <p>
499 * The alpha channel must be set for forward compatibility.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500500 *
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500501 * @deprecated use {@link NotificationChannel#shouldShowLights()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800502 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500503 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800504 public static final int FLAG_SHOW_LIGHTS = 0x00000001;
505
506 /**
507 * Bit to be bitwise-ored into the {@link #flags} field that should be
508 * set if this notification is in reference to something that is ongoing,
509 * like a phone call. It should not be set if this notification is in
510 * reference to something that happened at a particular point in time,
511 * like a missed phone call.
512 */
513 public static final int FLAG_ONGOING_EVENT = 0x00000002;
514
515 /**
516 * Bit to be bitwise-ored into the {@link #flags} field that if set,
Scott Mainb8b36452009-04-26 15:50:49 -0700517 * the audio will be repeated until the notification is
518 * cancelled or the notification window is opened.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800519 */
520 public static final int FLAG_INSISTENT = 0x00000004;
521
522 /**
523 * Bit to be bitwise-ored into the {@link #flags} field that should be
Griff Hazen293977b2014-04-28 08:37:20 -0700524 * set if you would only like the sound, vibrate and ticker to be played
525 * if the notification was not already showing.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800526 */
527 public static final int FLAG_ONLY_ALERT_ONCE = 0x00000008;
528
529 /**
530 * Bit to be bitwise-ored into the {@link #flags} field that should be
531 * set if the notification should be canceled when it is clicked by the
Daniel Sandler8aa9ae62012-12-04 23:31:47 -0500532 * user.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800533 */
534 public static final int FLAG_AUTO_CANCEL = 0x00000010;
535
536 /**
537 * Bit to be bitwise-ored into the {@link #flags} field that should be
538 * set if the notification should not be canceled when the user clicks
539 * the Clear all button.
540 */
541 public static final int FLAG_NO_CLEAR = 0x00000020;
542
Dianne Hackbornd8a43f62009-08-17 23:33:56 -0700543 /**
544 * Bit to be bitwise-ored into the {@link #flags} field that should be
545 * set if this notification represents a currently running service. This
546 * will normally be set for you by {@link Service#startForeground}.
547 */
548 public static final int FLAG_FOREGROUND_SERVICE = 0x00000040;
549
Daniel Sandlere46cbd32010-06-17 10:35:26 -0400550 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500551 * Obsolete flag indicating high-priority notifications; use the priority field instead.
Joe Malin8d40d042012-11-05 11:36:40 -0800552 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500553 * @deprecated Use {@link #priority} with a positive value.
Daniel Sandlere46cbd32010-06-17 10:35:26 -0400554 */
Aurimas Liutikas514c5ef2016-05-24 15:22:55 -0700555 @Deprecated
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500556 public static final int FLAG_HIGH_PRIORITY = 0x00000080;
Daniel Sandlere46cbd32010-06-17 10:35:26 -0400557
Griff Hazendfcb0802014-02-11 12:00:00 -0800558 /**
559 * Bit to be bitswise-ored into the {@link #flags} field that should be
560 * set if this notification is relevant to the current device only
561 * and it is not recommended that it bridge to other devices.
562 */
563 public static final int FLAG_LOCAL_ONLY = 0x00000100;
564
Griff Hazen5cadc3b2014-05-20 09:55:39 -0700565 /**
566 * Bit to be bitswise-ored into the {@link #flags} field that should be
567 * set if this notification is the group summary for a group of notifications.
568 * Grouped notifications may display in a cluster or stack on devices which
569 * support such rendering. Requires a group key also be set using {@link Builder#setGroup}.
570 */
571 public static final int FLAG_GROUP_SUMMARY = 0x00000200;
572
Julia Reynoldse46bb372016-03-17 11:05:58 -0400573 /**
574 * Bit to be bitswise-ored into the {@link #flags} field that should be
575 * set if this notification is the group summary for an auto-group of notifications.
576 *
577 * @hide
578 */
579 @SystemApi
580 public static final int FLAG_AUTOGROUP_SUMMARY = 0x00000400;
581
Julia Reynolds4db59552017-06-30 13:34:01 -0400582 /**
583 * @hide
584 */
585 public static final int FLAG_CAN_COLORIZE = 0x00000800;
586
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800587 public int flags;
588
Tor Norbyed9273d62013-05-30 15:59:53 -0700589 /** @hide */
Jeff Sharkeyce8db992017-12-13 20:05:05 -0700590 @IntDef(prefix = { "PRIORITY_" }, value = {
591 PRIORITY_DEFAULT,
592 PRIORITY_LOW,
593 PRIORITY_MIN,
594 PRIORITY_HIGH,
595 PRIORITY_MAX
596 })
Tor Norbyed9273d62013-05-30 15:59:53 -0700597 @Retention(RetentionPolicy.SOURCE)
598 public @interface Priority {}
599
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800600 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500601 * Default notification {@link #priority}. If your application does not prioritize its own
602 * notifications, use this value for all notifications.
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500603 *
604 * @deprecated use {@link NotificationManager#IMPORTANCE_DEFAULT} instead.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500605 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500606 @Deprecated
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500607 public static final int PRIORITY_DEFAULT = 0;
608
609 /**
610 * Lower {@link #priority}, for items that are less important. The UI may choose to show these
611 * items smaller, or at a different position in the list, compared with your app's
612 * {@link #PRIORITY_DEFAULT} items.
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500613 *
614 * @deprecated use {@link NotificationManager#IMPORTANCE_LOW} instead.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500615 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500616 @Deprecated
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500617 public static final int PRIORITY_LOW = -1;
618
619 /**
620 * Lowest {@link #priority}; these items might not be shown to the user except under special
621 * circumstances, such as detailed notification logs.
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500622 *
623 * @deprecated use {@link NotificationManager#IMPORTANCE_MIN} instead.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500624 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500625 @Deprecated
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500626 public static final int PRIORITY_MIN = -2;
627
628 /**
629 * Higher {@link #priority}, for more important notifications or alerts. The UI may choose to
630 * show these items larger, or at a different position in notification lists, compared with
631 * your app's {@link #PRIORITY_DEFAULT} items.
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500632 *
633 * @deprecated use {@link NotificationManager#IMPORTANCE_HIGH} instead.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500634 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500635 @Deprecated
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500636 public static final int PRIORITY_HIGH = 1;
637
638 /**
639 * Highest {@link #priority}, for your application's most important items that require the
640 * user's prompt attention or input.
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500641 *
642 * @deprecated use {@link NotificationManager#IMPORTANCE_HIGH} instead.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500643 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500644 @Deprecated
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500645 public static final int PRIORITY_MAX = 2;
646
647 /**
648 * Relative priority for this notification.
Joe Malin8d40d042012-11-05 11:36:40 -0800649 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500650 * Priority is an indication of how much of the user's valuable attention should be consumed by
651 * this notification. Low-priority notifications may be hidden from the user in certain
652 * situations, while the user might be interrupted for a higher-priority notification. The
Daniel Sandler6738eee2012-11-16 12:03:32 -0500653 * system will make a determination about how to interpret this priority when presenting
654 * the notification.
Chris Wren47c20a12014-06-18 17:27:29 -0400655 *
656 * <p>
657 * A notification that is at least {@link #PRIORITY_HIGH} is more likely to be presented
658 * as a heads-up notification.
659 * </p>
660 *
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500661 * @deprecated use {@link NotificationChannel#getImportance()} instead.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500662 */
Tor Norbyed9273d62013-05-30 15:59:53 -0700663 @Priority
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500664 @Deprecated
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500665 public int priority;
Joe Malin8d40d042012-11-05 11:36:40 -0800666
Dan Sandler26e81cf2014-05-06 10:01:27 -0400667 /**
668 * Accent color (an ARGB integer like the constants in {@link android.graphics.Color})
669 * to be applied by the standard Style templates when presenting this notification.
670 *
671 * The current template design constructs a colorful header image by overlaying the
672 * {@link #icon} image (stenciled in white) atop a field of this color. Alpha components are
673 * ignored.
674 */
Tor Norbye80756e32015-03-02 09:39:27 -0800675 @ColorInt
Dan Sandler26e81cf2014-05-06 10:01:27 -0400676 public int color = COLOR_DEFAULT;
677
678 /**
679 * Special value of {@link #color} telling the system not to decorate this notification with
680 * any special color but instead use default colors when presenting this notification.
681 */
Tor Norbye80756e32015-03-02 09:39:27 -0800682 @ColorInt
Dan Sandler26e81cf2014-05-06 10:01:27 -0400683 public static final int COLOR_DEFAULT = 0; // AKA Color.TRANSPARENT
Dan Sandler0bf2ed82013-12-21 23:33:41 -0600684
685 /**
Adrian Roos4ff3b122016-02-01 12:26:13 -0800686 * Special value of {@link #color} used as a place holder for an invalid color.
Selim Cinek99104832017-01-25 14:47:33 -0800687 * @hide
Adrian Roos4ff3b122016-02-01 12:26:13 -0800688 */
689 @ColorInt
Selim Cinek99104832017-01-25 14:47:33 -0800690 public static final int COLOR_INVALID = 1;
Adrian Roos4ff3b122016-02-01 12:26:13 -0800691
692 /**
Adrian Roosc1a80b02016-04-05 14:54:55 -0700693 * Sphere of visibility of this notification, which affects how and when the SystemUI reveals
694 * the notification's presence and contents in untrusted situations (namely, on the secure
Dan Sandler0bf2ed82013-12-21 23:33:41 -0600695 * lockscreen).
696 *
697 * The default level, {@link #VISIBILITY_PRIVATE}, behaves exactly as notifications have always
698 * done on Android: The notification's {@link #icon} and {@link #tickerText} (if available) are
699 * shown in all situations, but the contents are only available if the device is unlocked for
700 * the appropriate user.
701 *
702 * A more permissive policy can be expressed by {@link #VISIBILITY_PUBLIC}; such a notification
703 * can be read even in an "insecure" context (that is, above a secure lockscreen).
704 * To modify the public version of this notification—for example, to redact some portions—see
705 * {@link Builder#setPublicVersion(Notification)}.
706 *
707 * Finally, a notification can be made {@link #VISIBILITY_SECRET}, which will suppress its icon
708 * and ticker until the user has bypassed the lockscreen.
709 */
Jeff Sharkey30e06bb2017-04-24 11:18:03 -0600710 public @Visibility int visibility;
711
712 /** @hide */
713 @IntDef(prefix = { "VISIBILITY_" }, value = {
714 VISIBILITY_PUBLIC,
715 VISIBILITY_PRIVATE,
716 VISIBILITY_SECRET,
717 })
718 @Retention(RetentionPolicy.SOURCE)
719 public @interface Visibility {}
Dan Sandler0bf2ed82013-12-21 23:33:41 -0600720
Griff Hazenfc3922d2014-08-20 11:56:44 -0700721 /**
722 * Notification visibility: Show this notification in its entirety on all lockscreens.
723 *
724 * {@see #visibility}
725 */
Dan Sandler0bf2ed82013-12-21 23:33:41 -0600726 public static final int VISIBILITY_PUBLIC = 1;
Griff Hazenfc3922d2014-08-20 11:56:44 -0700727
728 /**
729 * Notification visibility: Show this notification on all lockscreens, but conceal sensitive or
730 * private information on secure lockscreens.
731 *
732 * {@see #visibility}
733 */
Dan Sandler0bf2ed82013-12-21 23:33:41 -0600734 public static final int VISIBILITY_PRIVATE = 0;
Griff Hazenfc3922d2014-08-20 11:56:44 -0700735
736 /**
737 * Notification visibility: Do not reveal any part of this notification on a secure lockscreen.
738 *
739 * {@see #visibility}
740 */
Dan Sandler0bf2ed82013-12-21 23:33:41 -0600741 public static final int VISIBILITY_SECRET = -1;
742
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500743 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400744 * Notification category: incoming call (voice or video) or similar synchronous communication request.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500745 */
John Spurlockfd7f1e02014-03-18 16:41:57 -0400746 public static final String CATEGORY_CALL = "call";
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500747
748 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400749 * Notification category: incoming direct message (SMS, instant message, etc.).
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500750 */
John Spurlockfd7f1e02014-03-18 16:41:57 -0400751 public static final String CATEGORY_MESSAGE = "msg";
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500752
753 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400754 * Notification category: asynchronous bulk message (email).
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500755 */
John Spurlockfd7f1e02014-03-18 16:41:57 -0400756 public static final String CATEGORY_EMAIL = "email";
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500757
758 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400759 * Notification category: calendar event.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500760 */
John Spurlockfd7f1e02014-03-18 16:41:57 -0400761 public static final String CATEGORY_EVENT = "event";
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500762
763 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400764 * Notification category: promotion or advertisement.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500765 */
John Spurlockfd7f1e02014-03-18 16:41:57 -0400766 public static final String CATEGORY_PROMO = "promo";
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500767
768 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400769 * Notification category: alarm or timer.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500770 */
John Spurlockfd7f1e02014-03-18 16:41:57 -0400771 public static final String CATEGORY_ALARM = "alarm";
772
773 /**
774 * Notification category: progress of a long-running background operation.
775 */
776 public static final String CATEGORY_PROGRESS = "progress";
777
778 /**
779 * Notification category: social network or sharing update.
780 */
781 public static final String CATEGORY_SOCIAL = "social";
782
783 /**
784 * Notification category: error in background operation or authentication status.
785 */
786 public static final String CATEGORY_ERROR = "err";
787
788 /**
789 * Notification category: media transport control for playback.
790 */
791 public static final String CATEGORY_TRANSPORT = "transport";
792
793 /**
794 * Notification category: system or device status update. Reserved for system use.
795 */
796 public static final String CATEGORY_SYSTEM = "sys";
797
798 /**
799 * Notification category: indication of running background service.
800 */
801 public static final String CATEGORY_SERVICE = "service";
802
803 /**
John Spurlock0a69c8c2014-03-21 13:30:57 -0400804 * Notification category: a specific, timely recommendation for a single thing.
805 * For example, a news app might want to recommend a news story it believes the user will
806 * want to read next.
807 */
808 public static final String CATEGORY_RECOMMENDATION = "recommendation";
809
810 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400811 * Notification category: ongoing information about device or contextual status.
812 */
813 public static final String CATEGORY_STATUS = "status";
814
815 /**
John Spurlock24d3dad2015-04-02 12:24:02 -0400816 * Notification category: user-scheduled reminder.
817 */
818 public static final String CATEGORY_REMINDER = "reminder";
819
820 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400821 * One of the predefined notification categories (see the <code>CATEGORY_*</code> constants)
822 * that best describes this Notification. May be used by the system for ranking and filtering.
823 */
824 public String category;
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500825
Griff Hazen5cadc3b2014-05-20 09:55:39 -0700826 private String mGroupKey;
827
828 /**
829 * Get the key used to group this notification into a cluster or stack
830 * with other notifications on devices which support such rendering.
831 */
832 public String getGroup() {
833 return mGroupKey;
834 }
835
836 private String mSortKey;
837
838 /**
839 * Get a sort key that orders this notification among other notifications from the
840 * same package. This can be useful if an external sort was already applied and an app
841 * would like to preserve this. Notifications will be sorted lexicographically using this
842 * value, although providing different priorities in addition to providing sort key may
843 * cause this value to be ignored.
844 *
845 * <p>This sort key can also be used to order members of a notification group. See
846 * {@link Builder#setGroup}.
847 *
848 * @see String#compareTo(String)
849 */
850 public String getSortKey() {
851 return mSortKey;
852 }
853
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500854 /**
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400855 * Additional semantic data to be carried around with this Notification.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400856 * <p>
857 * The extras keys defined here are intended to capture the original inputs to {@link Builder}
858 * APIs, and are intended to be used by
859 * {@link android.service.notification.NotificationListenerService} implementations to extract
860 * detailed information from notification objects.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500861 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400862 public Bundle extras = new Bundle();
863
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400864 /**
Felipe Lemedd85da62016-06-28 11:29:54 -0700865 * All pending intents in the notification as the system needs to be able to access them but
866 * touching the extras bundle in the system process is not safe because the bundle may contain
Svet Ganovddb94882016-06-23 19:55:24 -0700867 * custom parcelable objects.
868 *
869 * @hide
870 */
Felipe Lemedd85da62016-06-28 11:29:54 -0700871 public ArraySet<PendingIntent> allPendingIntents;
Svet Ganovddb94882016-06-23 19:55:24 -0700872
873 /**
Dianne Hackborn98305522017-05-05 17:53:53 -0700874 * Token identifying the notification that is applying doze/bgcheck whitelisting to the
875 * pending intents inside of it, so only those will get the behavior.
876 *
877 * @hide
878 */
Adrian Roosfb921842017-10-26 14:49:56 +0200879 private IBinder mWhitelistToken;
Dianne Hackborn98305522017-05-05 17:53:53 -0700880
881 /**
882 * Must be set by a process to start associating tokens with Notification objects
883 * coming in to it. This is set by NotificationManagerService.
884 *
885 * @hide
886 */
887 static public IBinder processWhitelistToken;
888
889 /**
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400890 * {@link #extras} key: this is the title of the notification,
891 * as supplied to {@link Builder#setContentTitle(CharSequence)}.
892 */
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -0500893 public static final String EXTRA_TITLE = "android.title";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400894
895 /**
896 * {@link #extras} key: this is the title of the notification when shown in expanded form,
897 * e.g. as supplied to {@link BigTextStyle#setBigContentTitle(CharSequence)}.
898 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400899 public static final String EXTRA_TITLE_BIG = EXTRA_TITLE + ".big";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400900
901 /**
902 * {@link #extras} key: this is the main text payload, as supplied to
903 * {@link Builder#setContentText(CharSequence)}.
904 */
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -0500905 public static final String EXTRA_TEXT = "android.text";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400906
907 /**
908 * {@link #extras} key: this is a third line of text, as supplied to
909 * {@link Builder#setSubText(CharSequence)}.
910 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400911 public static final String EXTRA_SUB_TEXT = "android.subText";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400912
913 /**
Adrian Roose458aa82015-12-08 16:17:19 -0800914 * {@link #extras} key: this is the remote input history, as supplied to
915 * {@link Builder#setRemoteInputHistory(CharSequence[])}.
Adrian Roos005e7742016-04-13 15:02:20 -0700916 *
917 * Apps can fill this through {@link Builder#setRemoteInputHistory(CharSequence[])}
918 * with the most recent inputs that have been sent through a {@link RemoteInput} of this
919 * Notification and are expected to clear it once the it is no longer relevant (e.g. for chat
920 * notifications once the other party has responded).
921 *
922 * The extra with this key is of type CharSequence[] and contains the most recent entry at
923 * the 0 index, the second most recent at the 1 index, etc.
924 *
925 * @see Builder#setRemoteInputHistory(CharSequence[])
Adrian Roose458aa82015-12-08 16:17:19 -0800926 */
927 public static final String EXTRA_REMOTE_INPUT_HISTORY = "android.remoteInputHistory";
928
929 /**
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400930 * {@link #extras} key: this is a small piece of additional text as supplied to
931 * {@link Builder#setContentInfo(CharSequence)}.
932 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400933 public static final String EXTRA_INFO_TEXT = "android.infoText";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400934
935 /**
936 * {@link #extras} key: this is a line of summary information intended to be shown
937 * alongside expanded notifications, as supplied to (e.g.)
938 * {@link BigTextStyle#setSummaryText(CharSequence)}.
939 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400940 public static final String EXTRA_SUMMARY_TEXT = "android.summaryText";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400941
942 /**
Christoph Studer4600f9b2014-07-22 22:44:43 +0200943 * {@link #extras} key: this is the longer text shown in the big form of a
944 * {@link BigTextStyle} notification, as supplied to
945 * {@link BigTextStyle#bigText(CharSequence)}.
946 */
947 public static final String EXTRA_BIG_TEXT = "android.bigText";
948
949 /**
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400950 * {@link #extras} key: this is the resource ID of the notification's main small icon, as
951 * supplied to {@link Builder#setSmallIcon(int)}.
Julia Reynoldse071abd2017-03-22 10:52:11 -0400952 *
953 * @deprecated Use {@link #getSmallIcon()}, which supports a wider variety of icon sources.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400954 */
Julia Reynoldse071abd2017-03-22 10:52:11 -0400955 @Deprecated
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -0500956 public static final String EXTRA_SMALL_ICON = "android.icon";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400957
958 /**
959 * {@link #extras} key: this is a bitmap to be used instead of the small icon when showing the
960 * notification payload, as
961 * supplied to {@link Builder#setLargeIcon(android.graphics.Bitmap)}.
Julia Reynoldse071abd2017-03-22 10:52:11 -0400962 *
963 * @deprecated Use {@link #getLargeIcon()}, which supports a wider variety of icon sources.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400964 */
Julia Reynoldse071abd2017-03-22 10:52:11 -0400965 @Deprecated
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400966 public static final String EXTRA_LARGE_ICON = "android.largeIcon";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400967
968 /**
969 * {@link #extras} key: this is a bitmap to be used instead of the one from
970 * {@link Builder#setLargeIcon(android.graphics.Bitmap)} when the notification is
971 * shown in its expanded form, as supplied to
972 * {@link BigPictureStyle#bigLargeIcon(android.graphics.Bitmap)}.
973 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400974 public static final String EXTRA_LARGE_ICON_BIG = EXTRA_LARGE_ICON + ".big";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400975
976 /**
977 * {@link #extras} key: this is the progress value supplied to
978 * {@link Builder#setProgress(int, int, boolean)}.
979 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400980 public static final String EXTRA_PROGRESS = "android.progress";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400981
982 /**
983 * {@link #extras} key: this is the maximum value supplied to
984 * {@link Builder#setProgress(int, int, boolean)}.
985 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400986 public static final String EXTRA_PROGRESS_MAX = "android.progressMax";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400987
988 /**
989 * {@link #extras} key: whether the progress bar is indeterminate, supplied to
990 * {@link Builder#setProgress(int, int, boolean)}.
991 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400992 public static final String EXTRA_PROGRESS_INDETERMINATE = "android.progressIndeterminate";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400993
994 /**
995 * {@link #extras} key: whether {@link #when} should be shown as a count-up timer (specifically
996 * a {@link android.widget.Chronometer}) instead of a timestamp, as supplied to
997 * {@link Builder#setUsesChronometer(boolean)}.
998 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400999 public static final String EXTRA_SHOW_CHRONOMETER = "android.showChronometer";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001000
1001 /**
Selim Cinek81c23aa2016-02-25 16:23:13 -08001002 * {@link #extras} key: whether the chronometer set on the notification should count down
1003 * instead of counting up. Is only relevant if key {@link #EXTRA_SHOW_CHRONOMETER} is present.
Adrian Roos96b7e202016-05-17 13:50:38 -07001004 * This extra is a boolean. The default is false.
Selim Cinek81c23aa2016-02-25 16:23:13 -08001005 */
Adrian Roos96b7e202016-05-17 13:50:38 -07001006 public static final String EXTRA_CHRONOMETER_COUNT_DOWN = "android.chronometerCountDown";
Selim Cinek81c23aa2016-02-25 16:23:13 -08001007
1008 /**
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001009 * {@link #extras} key: whether {@link #when} should be shown,
1010 * as supplied to {@link Builder#setShowWhen(boolean)}.
1011 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -04001012 public static final String EXTRA_SHOW_WHEN = "android.showWhen";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001013
1014 /**
1015 * {@link #extras} key: this is a bitmap to be shown in {@link BigPictureStyle} expanded
1016 * notifications, supplied to {@link BigPictureStyle#bigPicture(android.graphics.Bitmap)}.
1017 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -04001018 public static final String EXTRA_PICTURE = "android.picture";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001019
1020 /**
1021 * {@link #extras} key: An array of CharSequences to show in {@link InboxStyle} expanded
1022 * notifications, each of which was supplied to {@link InboxStyle#addLine(CharSequence)}.
1023 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -04001024 public static final String EXTRA_TEXT_LINES = "android.textLines";
Dan Sandler842dd772014-05-15 09:36:47 -04001025
1026 /**
1027 * {@link #extras} key: A string representing the name of the specific
1028 * {@link android.app.Notification.Style} used to create this notification.
1029 */
Chris Wren91ad5632013-06-05 15:05:57 -04001030 public static final String EXTRA_TEMPLATE = "android.template";
Daniel Sandlerf45564e2013-04-15 15:05:08 -04001031
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001032 /**
Chris Wrene6c48932014-09-29 17:19:27 -04001033 * {@link #extras} key: A String array containing the people that this notification relates to,
1034 * each of which was supplied to {@link Builder#addPerson(String)}.
Selim Cineke7238dd2017-12-14 17:48:32 -08001035 *
1036 * @deprecated the actual objects are now in {@link #EXTRA_PEOPLE_LIST}
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001037 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -04001038 public static final String EXTRA_PEOPLE = "android.people";
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001039
1040 /**
Selim Cineke7238dd2017-12-14 17:48:32 -08001041 * {@link #extras} key: An arrayList of {@link Person} objects containing the people that
1042 * this notification relates to.
1043 */
1044 public static final String EXTRA_PEOPLE_LIST = "android.people.list";
1045
1046 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -04001047 * Allow certain system-generated notifications to appear before the device is provisioned.
1048 * Only available to notifications coming from the android package.
1049 * @hide
1050 */
Maurice Lam96c10032017-03-29 15:42:38 -07001051 @SystemApi
Jeff Sharkeyd86b8fe2017-06-02 17:36:26 -06001052 @RequiresPermission(android.Manifest.permission.NOTIFICATION_DURING_SETUP)
John Spurlockfd7f1e02014-03-18 16:41:57 -04001053 public static final String EXTRA_ALLOW_DURING_SETUP = "android.allowDuringSetup";
1054
1055 /**
Jose Limae9e3b3b2014-05-18 23:44:50 -07001056 * {@link #extras} key: A
1057 * {@link android.content.ContentUris content URI} pointing to an image that can be displayed
1058 * in the background when the notification is selected. The URI must point to an image stream
1059 * suitable for passing into
1060 * {@link android.graphics.BitmapFactory#decodeStream(java.io.InputStream)
1061 * BitmapFactory.decodeStream}; all other content types will be ignored. The content provider
1062 * URI used for this purpose must require no permissions to read the image data.
1063 */
1064 public static final String EXTRA_BACKGROUND_IMAGE_URI = "android.backgroundImageUri";
1065
1066 /**
Dan Sandler842dd772014-05-15 09:36:47 -04001067 * {@link #extras} key: A
Jeff Browndba34ba2014-06-24 20:46:03 -07001068 * {@link android.media.session.MediaSession.Token} associated with a
Dan Sandler842dd772014-05-15 09:36:47 -04001069 * {@link android.app.Notification.MediaStyle} notification.
1070 */
1071 public static final String EXTRA_MEDIA_SESSION = "android.mediaSession";
1072
1073 /**
Bryan Mawhinneye191f902014-07-22 12:50:09 +01001074 * {@link #extras} key: the indices of actions to be shown in the compact view,
1075 * as supplied to (e.g.) {@link MediaStyle#setShowActionsInCompactView(int...)}.
1076 */
1077 public static final String EXTRA_COMPACT_ACTIONS = "android.compactActions";
1078
Christoph Studer943aa672014-08-03 20:31:16 +02001079 /**
Alex Hillsfc737de2016-03-23 17:33:02 -04001080 * {@link #extras} key: the username to be displayed for all messages sent by the user including
1081 * direct replies
Adrian Roos96b7e202016-05-17 13:50:38 -07001082 * {@link android.app.Notification.MessagingStyle} notification. This extra is a
1083 * {@link CharSequence}
Selim Cinekcb8b9852017-12-15 18:01:52 -08001084 *
1085 * @deprecated use {@link #EXTRA_MESSAGING_PERSON}
Alex Hillsfc737de2016-03-23 17:33:02 -04001086 */
1087 public static final String EXTRA_SELF_DISPLAY_NAME = "android.selfDisplayName";
1088
1089 /**
Selim Cinekcb8b9852017-12-15 18:01:52 -08001090 * {@link #extras} key: the person to be displayed for all messages sent by the user including
1091 * direct replies
1092 * {@link android.app.Notification.MessagingStyle} notification. This extra is a
1093 * {@link Person}
1094 */
1095 public static final String EXTRA_MESSAGING_PERSON = "android.messagingUser";
1096
1097 /**
Adrian Roos96b7e202016-05-17 13:50:38 -07001098 * {@link #extras} key: a {@link CharSequence} to be displayed as the title to a conversation
Alex Hillsd9b04d92016-04-11 16:38:16 -04001099 * represented by a {@link android.app.Notification.MessagingStyle}
Alex Hillsfc737de2016-03-23 17:33:02 -04001100 */
Alex Hillsd9b04d92016-04-11 16:38:16 -04001101 public static final String EXTRA_CONVERSATION_TITLE = "android.conversationTitle";
Alex Hillsfc737de2016-03-23 17:33:02 -04001102
1103 /**
1104 * {@link #extras} key: an array of {@link android.app.Notification.MessagingStyle.Message}
1105 * bundles provided by a
Adrian Roos96b7e202016-05-17 13:50:38 -07001106 * {@link android.app.Notification.MessagingStyle} notification. This extra is a parcelable
1107 * array of bundles.
Alex Hillsfc737de2016-03-23 17:33:02 -04001108 */
1109 public static final String EXTRA_MESSAGES = "android.messages";
1110
1111 /**
Adrian Roos437cd562017-01-18 15:47:03 -08001112 * {@link #extras} key: an array of
1113 * {@link android.app.Notification.MessagingStyle#addHistoricMessage historic}
1114 * {@link android.app.Notification.MessagingStyle.Message} bundles provided by a
1115 * {@link android.app.Notification.MessagingStyle} notification. This extra is a parcelable
1116 * array of bundles.
1117 */
1118 public static final String EXTRA_HISTORIC_MESSAGES = "android.messages.historic";
1119
1120 /**
Kodlee Yin9ac617c2017-12-19 11:20:50 -08001121 * {@link #extras} key: whether the {@link android.app.Notification.MessagingStyle} notification
1122 * represents a group conversation.
1123 */
1124 public static final String EXTRA_IS_GROUP_CONVERSATION = "android.isGroupConversation";
1125
1126 /**
Selim Cinek7b9605b2017-01-19 17:36:00 -08001127 * {@link #extras} key: whether the notification should be colorized as
1128 * supplied to {@link Builder#setColorized(boolean)}}.
1129 */
1130 public static final String EXTRA_COLORIZED = "android.colorized";
1131
1132 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -04001133 * @hide
1134 */
1135 public static final String EXTRA_BUILDER_APPLICATION_INFO = "android.appInfo";
1136
Selim Cinek247fa012016-02-18 09:50:48 -08001137 /**
1138 * @hide
1139 */
1140 public static final String EXTRA_CONTAINS_CUSTOM_VIEW = "android.contains.customView";
1141
Shane Brennan472a3b32016-12-12 15:28:10 -08001142 /**
Selim Cinekd0426622017-07-11 13:19:59 +02001143 * @hide
1144 */
1145 public static final String EXTRA_REDUCED_IMAGES = "android.reduced.images";
1146
1147 /**
Shane Brennan472a3b32016-12-12 15:28:10 -08001148 * {@link #extras} key: the audio contents of this notification.
1149 *
1150 * This is for use when rendering the notification on an audio-focused interface;
1151 * the audio contents are a complete sound sample that contains the contents/body of the
1152 * notification. This may be used in substitute of a Text-to-Speech reading of the
1153 * notification. For example if the notification represents a voice message this should point
1154 * to the audio of that message.
1155 *
1156 * The data stored under this key should be a String representation of a Uri that contains the
1157 * audio contents in one of the following formats: WAV, PCM 16-bit, AMR-WB.
1158 *
1159 * This extra is unnecessary if you are using {@code MessagingStyle} since each {@code Message}
1160 * has a field for holding data URI. That field can be used for audio.
1161 * See {@code Message#setData}.
1162 *
1163 * Example usage:
1164 * <pre>
1165 * {@code
1166 * Notification.Builder myBuilder = (build your Notification as normal);
1167 * myBuilder.getExtras().putString(EXTRA_AUDIO_CONTENTS_URI, myAudioUri.toString());
1168 * }
1169 * </pre>
1170 */
1171 public static final String EXTRA_AUDIO_CONTENTS_URI = "android.audioContents";
1172
Dan Sandler80eaa592016-04-14 23:34:54 -04001173 /** @hide */
1174 @SystemApi
Jeff Sharkeyd86b8fe2017-06-02 17:36:26 -06001175 @RequiresPermission(android.Manifest.permission.SUBSTITUTE_NOTIFICATION_APP_NAME)
Dan Sandler732bd6c2016-04-12 14:20:32 -04001176 public static final String EXTRA_SUBSTITUTE_APP_NAME = "android.substName";
1177
Dianne Hackbornfb5d4b52017-05-16 17:03:44 -07001178 /**
1179 * This is set on the notification shown by the activity manager about all apps
1180 * running in the background. It indicates that the notification should be shown
1181 * only if any of the given apps do not already have a {@link #FLAG_FOREGROUND_SERVICE}
1182 * notification currently visible to the user. This is a string array of all
1183 * package names of the apps.
1184 * @hide
1185 */
1186 public static final String EXTRA_FOREGROUND_APPS = "android.foregroundApps";
1187
Dan Sandlerd63f9322015-05-06 15:18:49 -04001188 private Icon mSmallIcon;
1189 private Icon mLargeIcon;
1190
Julia Reynoldsb5e44b72016-08-16 15:00:25 -04001191 private String mChannelId;
Julia Reynolds2a128742016-11-28 14:29:25 -05001192 private long mTimeout;
Julia Reynoldsb5e44b72016-08-16 15:00:25 -04001193
Julia Reynolds13d898c2017-02-02 12:22:05 -05001194 private String mShortcutId;
Julia Reynolds3aedded2017-03-31 14:42:09 -04001195 private CharSequence mSettingsText;
Julia Reynolds13d898c2017-02-02 12:22:05 -05001196
Julia Reynoldsa79c3712017-04-21 10:29:57 -04001197 /** @hide */
1198 @IntDef(prefix = { "GROUP_ALERT_" }, value = {
1199 GROUP_ALERT_ALL, GROUP_ALERT_CHILDREN, GROUP_ALERT_SUMMARY
1200 })
1201 @Retention(RetentionPolicy.SOURCE)
1202 public @interface GroupAlertBehavior {}
1203
1204 /**
1205 * Constant for {@link Builder#setGroupAlertBehavior(int)}, meaning that all notifications in a
1206 * group with sound or vibration ought to make sound or vibrate (respectively), so this
1207 * notification will not be muted when it is in a group.
1208 */
1209 public static final int GROUP_ALERT_ALL = 0;
1210
1211 /**
1212 * Constant for {@link Builder#setGroupAlertBehavior(int)}, meaning that all children
1213 * notification in a group should be silenced (no sound or vibration) even if they are posted
1214 * to a {@link NotificationChannel} that has sound and/or vibration. Use this constant to
Julia Reynolds399d9bf2017-08-11 12:52:14 -04001215 * mute this notification if this notification is a group child. This must be applied to all
1216 * children notifications you want to mute.
Julia Reynoldsa79c3712017-04-21 10:29:57 -04001217 *
1218 * <p> For example, you might want to use this constant if you post a number of children
1219 * notifications at once (say, after a periodic sync), and only need to notify the user
1220 * audibly once.
1221 */
1222 public static final int GROUP_ALERT_SUMMARY = 1;
1223
1224 /**
1225 * Constant for {@link Builder#setGroupAlertBehavior(int)}, meaning that the summary
1226 * notification in a group should be silenced (no sound or vibration) even if they are
1227 * posted to a {@link NotificationChannel} that has sound and/or vibration. Use this constant
1228 * to mute this notification if this notification is a group summary.
1229 *
1230 * <p>For example, you might want to use this constant if only the children notifications
Julia Reynolds399d9bf2017-08-11 12:52:14 -04001231 * in your group have content and the summary is only used to visually group notifications
1232 * rather than to alert the user that new information is available.
Julia Reynoldsa79c3712017-04-21 10:29:57 -04001233 */
1234 public static final int GROUP_ALERT_CHILDREN = 2;
1235
Julia Reynolds2f431e22017-06-07 14:12:09 +00001236 private int mGroupAlertBehavior = GROUP_ALERT_ALL;
Julia Reynoldsa79c3712017-04-21 10:29:57 -04001237
Julia Reynolds13d898c2017-02-02 12:22:05 -05001238 /**
1239 * If this notification is being shown as a badge, always show as a number.
1240 */
1241 public static final int BADGE_ICON_NONE = 0;
1242
1243 /**
1244 * If this notification is being shown as a badge, use the {@link #getSmallIcon()} to
1245 * represent this notification.
1246 */
1247 public static final int BADGE_ICON_SMALL = 1;
1248
1249 /**
1250 * If this notification is being shown as a badge, use the {@link #getLargeIcon()} to
1251 * represent this notification.
1252 */
1253 public static final int BADGE_ICON_LARGE = 2;
Julia Reynolds7d5b4da2017-03-20 10:18:49 -04001254 private int mBadgeIcon = BADGE_ICON_NONE;
Julia Reynolds13d898c2017-02-02 12:22:05 -05001255
Chris Wren51c75102013-07-16 20:49:17 -04001256 /**
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001257 * Structure to encapsulate a named action that can be shown as part of this notification.
1258 * It must include an icon, a label, and a {@link PendingIntent} to be fired when the action is
1259 * selected by the user.
1260 * <p>
Griff Hazen959591e2014-05-15 22:26:18 -07001261 * Apps should use {@link Notification.Builder#addAction(int, CharSequence, PendingIntent)}
1262 * or {@link Notification.Builder#addAction(Notification.Action)}
1263 * to attach actions.
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001264 */
Daniel Sandlerea2a3172013-02-20 22:24:20 -05001265 public static class Action implements Parcelable {
Shane Brennan472a3b32016-12-12 15:28:10 -08001266 /**
1267 * {@link #extras} key: Keys to a {@link Parcelable} {@link ArrayList} of
1268 * {@link RemoteInput}s.
1269 *
1270 * This is intended for {@link RemoteInput}s that only accept data, meaning
1271 * {@link RemoteInput#getAllowFreeFormInput} is false, {@link RemoteInput#getChoices}
1272 * is null or empty, and {@link RemoteInput#getAllowedDataTypes is non-null and not
1273 * empty. These {@link RemoteInput}s will be ignored by devices that do not
1274 * support non-text-based {@link RemoteInput}s. See {@link Builder#build}.
1275 *
1276 * You can test if a RemoteInput matches these constraints using
1277 * {@link RemoteInput#isDataOnly}.
1278 */
1279 private static final String EXTRA_DATA_ONLY_INPUTS = "android.extra.DATA_ONLY_INPUTS";
1280
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001281 /**
1282 * {@link }: No semantic action defined.
1283 */
1284 public static final int SEMANTIC_ACTION_NONE = 0;
1285
1286 /**
1287 * {@code SemanticAction}: Reply to a conversation, chat, group, or wherever replies
1288 * may be appropriate.
1289 */
1290 public static final int SEMANTIC_ACTION_REPLY = 1;
1291
1292 /**
1293 * {@code SemanticAction}: Mark content as read.
1294 */
1295 public static final int SEMANTIC_ACTION_MARK_AS_READ = 2;
1296
1297 /**
1298 * {@code SemanticAction}: Mark content as unread.
1299 */
1300 public static final int SEMANTIC_ACTION_MARK_AS_UNREAD = 3;
1301
1302 /**
1303 * {@code SemanticAction}: Delete the content associated with the notification. This
1304 * could mean deleting an email, message, etc.
1305 */
1306 public static final int SEMANTIC_ACTION_DELETE = 4;
1307
1308 /**
1309 * {@code SemanticAction}: Archive the content associated with the notification. This
1310 * could mean archiving an email, message, etc.
1311 */
1312 public static final int SEMANTIC_ACTION_ARCHIVE = 5;
1313
1314 /**
1315 * {@code SemanticAction}: Mute the content associated with the notification. This could
1316 * mean silencing a conversation or currently playing media.
1317 */
1318 public static final int SEMANTIC_ACTION_MUTE = 6;
1319
1320 /**
1321 * {@code SemanticAction}: Unmute the content associated with the notification. This could
1322 * mean un-silencing a conversation or currently playing media.
1323 */
1324 public static final int SEMANTIC_ACTION_UNMUTE = 7;
1325
1326 /**
1327 * {@code SemanticAction}: Mark content with a thumbs up.
1328 */
1329 public static final int SEMANTIC_ACTION_THUMBS_UP = 8;
1330
1331 /**
1332 * {@code SemanticAction}: Mark content with a thumbs down.
1333 */
1334 public static final int SEMANTIC_ACTION_THUMBS_DOWN = 9;
1335
1336
Griff Hazen959591e2014-05-15 22:26:18 -07001337 private final Bundle mExtras;
Dan Sandler86647982015-05-13 23:41:13 -04001338 private Icon mIcon;
Griff Hazen61a9e862014-05-22 16:05:19 -07001339 private final RemoteInput[] mRemoteInputs;
Alex Hills1f27f882017-01-12 11:24:46 -05001340 private boolean mAllowGeneratedReplies = true;
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001341 private final @SemanticAction int mSemanticAction;
Griff Hazen959591e2014-05-15 22:26:18 -07001342
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001343 /**
1344 * Small icon representing the action.
Dan Sandler86647982015-05-13 23:41:13 -04001345 *
1346 * @deprecated Use {@link Action#getIcon()} instead.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001347 */
Dan Sandler86647982015-05-13 23:41:13 -04001348 @Deprecated
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001349 public int icon;
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001350
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001351 /**
1352 * Title of the action.
1353 */
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001354 public CharSequence title;
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001355
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001356 /**
1357 * Intent to send when the user invokes this action. May be null, in which case the action
1358 * may be rendered in a disabled presentation by the system UI.
1359 */
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001360 public PendingIntent actionIntent;
Griff Hazen959591e2014-05-15 22:26:18 -07001361
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001362 private Action(Parcel in) {
Dan Sandler86647982015-05-13 23:41:13 -04001363 if (in.readInt() != 0) {
1364 mIcon = Icon.CREATOR.createFromParcel(in);
Dan Sandler68079d52015-07-22 10:45:30 -04001365 if (mIcon.getType() == Icon.TYPE_RESOURCE) {
1366 icon = mIcon.getResId();
1367 }
Dan Sandler86647982015-05-13 23:41:13 -04001368 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001369 title = TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(in);
1370 if (in.readInt() == 1) {
1371 actionIntent = PendingIntent.CREATOR.createFromParcel(in);
1372 }
Jeff Sharkeya04c7a72016-03-18 12:20:36 -06001373 mExtras = Bundle.setDefusable(in.readBundle(), true);
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001374 mRemoteInputs = in.createTypedArray(RemoteInput.CREATOR);
Alex Hills42b0c4d2016-04-26 13:35:36 -04001375 mAllowGeneratedReplies = in.readInt() == 1;
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001376 mSemanticAction = in.readInt();
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001377 }
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001378
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001379 /**
Dan Sandler86647982015-05-13 23:41:13 -04001380 * @deprecated Use {@link android.app.Notification.Action.Builder}.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001381 */
Dan Sandler86647982015-05-13 23:41:13 -04001382 @Deprecated
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001383 public Action(int icon, CharSequence title, PendingIntent intent) {
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001384 this(Icon.createWithResource("", icon), title, intent, new Bundle(), null, true,
1385 SEMANTIC_ACTION_NONE);
Griff Hazen959591e2014-05-15 22:26:18 -07001386 }
1387
Adrian Roos7af53622016-10-12 13:44:05 -07001388 /** Keep in sync with {@link Notification.Action.Builder#Builder(Action)}! */
Dan Sandler86647982015-05-13 23:41:13 -04001389 private Action(Icon icon, CharSequence title, PendingIntent intent, Bundle extras,
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001390 RemoteInput[] remoteInputs, boolean allowGeneratedReplies,
1391 @SemanticAction int semanticAction) {
Dan Sandler86647982015-05-13 23:41:13 -04001392 this.mIcon = icon;
Gus Prevasf5bff46f2015-08-24 10:34:28 -04001393 if (icon != null && icon.getType() == Icon.TYPE_RESOURCE) {
1394 this.icon = icon.getResId();
1395 }
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001396 this.title = title;
1397 this.actionIntent = intent;
Griff Hazen959591e2014-05-15 22:26:18 -07001398 this.mExtras = extras != null ? extras : new Bundle();
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001399 this.mRemoteInputs = remoteInputs;
Alex Hills42b0c4d2016-04-26 13:35:36 -04001400 this.mAllowGeneratedReplies = allowGeneratedReplies;
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001401 this.mSemanticAction = semanticAction;
Griff Hazen959591e2014-05-15 22:26:18 -07001402 }
1403
1404 /**
Dan Sandler86647982015-05-13 23:41:13 -04001405 * Return an icon representing the action.
1406 */
1407 public Icon getIcon() {
1408 if (mIcon == null && icon != 0) {
1409 // you snuck an icon in here without using the builder; let's try to keep it
1410 mIcon = Icon.createWithResource("", icon);
1411 }
1412 return mIcon;
1413 }
1414
1415 /**
Griff Hazen959591e2014-05-15 22:26:18 -07001416 * Get additional metadata carried around with this Action.
1417 */
1418 public Bundle getExtras() {
1419 return mExtras;
1420 }
1421
1422 /**
Alex Hills42b0c4d2016-04-26 13:35:36 -04001423 * Return whether the platform should automatically generate possible replies for this
1424 * {@link Action}
1425 */
1426 public boolean getAllowGeneratedReplies() {
1427 return mAllowGeneratedReplies;
1428 }
1429
1430 /**
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001431 * Get the list of inputs to be collected from the user when this action is sent.
Shane Brennan472a3b32016-12-12 15:28:10 -08001432 * May return null if no remote inputs were added. Only returns inputs which accept
1433 * a text input. For inputs which only accept data use {@link #getDataOnlyRemoteInputs}.
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001434 */
1435 public RemoteInput[] getRemoteInputs() {
1436 return mRemoteInputs;
1437 }
1438
1439 /**
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001440 * Returns the {@code SemanticAction} associated with this {@link Action}. A
1441 * {@code SemanticAction} denotes what an {@link Action}'s {@link PendingIntent} will do
1442 * (eg. reply, mark as read, delete, etc).
1443 */
1444 public @SemanticAction int getSemanticAction() {
1445 return mSemanticAction;
1446 }
1447
1448 /**
Shane Brennan472a3b32016-12-12 15:28:10 -08001449 * Get the list of inputs to be collected from the user that ONLY accept data when this
1450 * action is sent. These remote inputs are guaranteed to return true on a call to
1451 * {@link RemoteInput#isDataOnly}.
1452 *
Shane Brennanf670d6c2017-04-25 13:05:45 -07001453 * Returns null if there are no data-only remote inputs.
Shane Brennan472a3b32016-12-12 15:28:10 -08001454 *
1455 * This method exists so that legacy RemoteInput collectors that pre-date the addition
1456 * of non-textual RemoteInputs do not access these remote inputs.
1457 */
1458 public RemoteInput[] getDataOnlyRemoteInputs() {
1459 return (RemoteInput[]) mExtras.getParcelableArray(EXTRA_DATA_ONLY_INPUTS);
1460 }
1461
1462 /**
Griff Hazen959591e2014-05-15 22:26:18 -07001463 * Builder class for {@link Action} objects.
1464 */
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001465 public static final class Builder {
Dan Sandler86647982015-05-13 23:41:13 -04001466 private final Icon mIcon;
Griff Hazen959591e2014-05-15 22:26:18 -07001467 private final CharSequence mTitle;
1468 private final PendingIntent mIntent;
Alex Hills1f27f882017-01-12 11:24:46 -05001469 private boolean mAllowGeneratedReplies = true;
Griff Hazen959591e2014-05-15 22:26:18 -07001470 private final Bundle mExtras;
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001471 private ArrayList<RemoteInput> mRemoteInputs;
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001472 private @SemanticAction int mSemanticAction;
Griff Hazen959591e2014-05-15 22:26:18 -07001473
1474 /**
1475 * Construct a new builder for {@link Action} object.
1476 * @param icon icon to show for this action
1477 * @param title the title of the action
1478 * @param intent the {@link PendingIntent} to fire when users trigger this action
1479 */
Dan Sandler86647982015-05-13 23:41:13 -04001480 @Deprecated
Griff Hazen959591e2014-05-15 22:26:18 -07001481 public Builder(int icon, CharSequence title, PendingIntent intent) {
Adrian Roos7af53622016-10-12 13:44:05 -07001482 this(Icon.createWithResource("", icon), title, intent);
Dan Sandler86647982015-05-13 23:41:13 -04001483 }
1484
1485 /**
1486 * Construct a new builder for {@link Action} object.
1487 * @param icon icon to show for this action
1488 * @param title the title of the action
1489 * @param intent the {@link PendingIntent} to fire when users trigger this action
1490 */
1491 public Builder(Icon icon, CharSequence title, PendingIntent intent) {
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001492 this(icon, title, intent, new Bundle(), null, true, SEMANTIC_ACTION_NONE);
Griff Hazen959591e2014-05-15 22:26:18 -07001493 }
1494
1495 /**
1496 * Construct a new builder for {@link Action} object using the fields from an
1497 * {@link Action}.
1498 * @param action the action to read fields from.
1499 */
1500 public Builder(Action action) {
Adrian Roos7af53622016-10-12 13:44:05 -07001501 this(action.getIcon(), action.title, action.actionIntent,
1502 new Bundle(action.mExtras), action.getRemoteInputs(),
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001503 action.getAllowGeneratedReplies(), action.getSemanticAction());
Griff Hazen959591e2014-05-15 22:26:18 -07001504 }
1505
Dan Sandler86647982015-05-13 23:41:13 -04001506 private Builder(Icon icon, CharSequence title, PendingIntent intent, Bundle extras,
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001507 RemoteInput[] remoteInputs, boolean allowGeneratedReplies,
1508 @SemanticAction int semanticAction) {
Griff Hazen959591e2014-05-15 22:26:18 -07001509 mIcon = icon;
1510 mTitle = title;
1511 mIntent = intent;
1512 mExtras = extras;
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001513 if (remoteInputs != null) {
1514 mRemoteInputs = new ArrayList<RemoteInput>(remoteInputs.length);
1515 Collections.addAll(mRemoteInputs, remoteInputs);
1516 }
Adrian Roos7af53622016-10-12 13:44:05 -07001517 mAllowGeneratedReplies = allowGeneratedReplies;
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001518 mSemanticAction = semanticAction;
Griff Hazen959591e2014-05-15 22:26:18 -07001519 }
1520
1521 /**
1522 * Merge additional metadata into this builder.
1523 *
1524 * <p>Values within the Bundle will replace existing extras values in this Builder.
1525 *
1526 * @see Notification.Action#extras
1527 */
1528 public Builder addExtras(Bundle extras) {
1529 if (extras != null) {
1530 mExtras.putAll(extras);
1531 }
1532 return this;
1533 }
1534
1535 /**
1536 * Get the metadata Bundle used by this Builder.
1537 *
1538 * <p>The returned Bundle is shared with this Builder.
1539 */
1540 public Bundle getExtras() {
1541 return mExtras;
1542 }
1543
1544 /**
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001545 * Add an input to be collected from the user when this action is sent.
1546 * Response values can be retrieved from the fired intent by using the
1547 * {@link RemoteInput#getResultsFromIntent} function.
1548 * @param remoteInput a {@link RemoteInput} to add to the action
1549 * @return this object for method chaining
1550 */
1551 public Builder addRemoteInput(RemoteInput remoteInput) {
1552 if (mRemoteInputs == null) {
1553 mRemoteInputs = new ArrayList<RemoteInput>();
1554 }
1555 mRemoteInputs.add(remoteInput);
1556 return this;
1557 }
1558
1559 /**
Alex Hills42b0c4d2016-04-26 13:35:36 -04001560 * Set whether the platform should automatically generate possible replies to add to
1561 * {@link RemoteInput#getChoices()}. If the {@link Action} doesn't have a
1562 * {@link RemoteInput}, this has no effect.
1563 * @param allowGeneratedReplies {@code true} to allow generated replies, {@code false}
1564 * otherwise
1565 * @return this object for method chaining
Alex Hills1f27f882017-01-12 11:24:46 -05001566 * The default value is {@code true}
Alex Hills42b0c4d2016-04-26 13:35:36 -04001567 */
1568 public Builder setAllowGeneratedReplies(boolean allowGeneratedReplies) {
1569 mAllowGeneratedReplies = allowGeneratedReplies;
1570 return this;
1571 }
1572
1573 /**
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001574 * Sets the {@code SemanticAction} for this {@link Action}. A
1575 * {@code SemanticAction} denotes what an {@link Action}'s
1576 * {@link PendingIntent} will do (eg. reply, mark as read, delete, etc).
1577 * @param semanticAction a SemanticAction defined within {@link Action} with
1578 * {@code SEMANTIC_ACTION_} prefixes
1579 * @return this object for method chaining
1580 */
1581 public Builder setSemanticAction(@SemanticAction int semanticAction) {
1582 mSemanticAction = semanticAction;
1583 return this;
1584 }
1585
1586 /**
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001587 * Apply an extender to this action builder. Extenders may be used to add
1588 * metadata or change options on this builder.
1589 */
Griff Hazen61a9e862014-05-22 16:05:19 -07001590 public Builder extend(Extender extender) {
1591 extender.extend(this);
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001592 return this;
1593 }
1594
1595 /**
Griff Hazen959591e2014-05-15 22:26:18 -07001596 * Combine all of the options that have been set and return a new {@link Action}
1597 * object.
1598 * @return the built action
1599 */
1600 public Action build() {
Shane Brennan472a3b32016-12-12 15:28:10 -08001601 ArrayList<RemoteInput> dataOnlyInputs = new ArrayList<>();
1602 RemoteInput[] previousDataInputs =
1603 (RemoteInput[]) mExtras.getParcelableArray(EXTRA_DATA_ONLY_INPUTS);
Felipe Lemedfd11962017-01-18 18:38:46 -08001604 if (previousDataInputs != null) {
Shane Brennan472a3b32016-12-12 15:28:10 -08001605 for (RemoteInput input : previousDataInputs) {
1606 dataOnlyInputs.add(input);
1607 }
1608 }
1609 List<RemoteInput> textInputs = new ArrayList<>();
1610 if (mRemoteInputs != null) {
1611 for (RemoteInput input : mRemoteInputs) {
1612 if (input.isDataOnly()) {
1613 dataOnlyInputs.add(input);
1614 } else {
1615 textInputs.add(input);
1616 }
1617 }
1618 }
1619 if (!dataOnlyInputs.isEmpty()) {
1620 RemoteInput[] dataInputsArr =
1621 dataOnlyInputs.toArray(new RemoteInput[dataOnlyInputs.size()]);
1622 mExtras.putParcelableArray(EXTRA_DATA_ONLY_INPUTS, dataInputsArr);
1623 }
1624 RemoteInput[] textInputsArr = textInputs.isEmpty()
1625 ? null : textInputs.toArray(new RemoteInput[textInputs.size()]);
1626 return new Action(mIcon, mTitle, mIntent, mExtras, textInputsArr,
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001627 mAllowGeneratedReplies, mSemanticAction);
Griff Hazen959591e2014-05-15 22:26:18 -07001628 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001629 }
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001630
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001631 @Override
1632 public Action clone() {
1633 return new Action(
Dan Sandler86647982015-05-13 23:41:13 -04001634 getIcon(),
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001635 title,
1636 actionIntent, // safe to alias
Julia Reynolds53c934c2016-09-16 14:03:43 -04001637 mExtras == null ? new Bundle() : new Bundle(mExtras),
Alex Hills42b0c4d2016-04-26 13:35:36 -04001638 getRemoteInputs(),
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001639 getAllowGeneratedReplies(),
1640 getSemanticAction());
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001641 }
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001642
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001643 @Override
1644 public int describeContents() {
1645 return 0;
1646 }
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001647
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001648 @Override
1649 public void writeToParcel(Parcel out, int flags) {
Dan Sandler86647982015-05-13 23:41:13 -04001650 final Icon ic = getIcon();
1651 if (ic != null) {
1652 out.writeInt(1);
1653 ic.writeToParcel(out, 0);
1654 } else {
1655 out.writeInt(0);
1656 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001657 TextUtils.writeToParcel(title, out, flags);
1658 if (actionIntent != null) {
1659 out.writeInt(1);
1660 actionIntent.writeToParcel(out, flags);
1661 } else {
1662 out.writeInt(0);
1663 }
Griff Hazen959591e2014-05-15 22:26:18 -07001664 out.writeBundle(mExtras);
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001665 out.writeTypedArray(mRemoteInputs, flags);
Alex Hills42b0c4d2016-04-26 13:35:36 -04001666 out.writeInt(mAllowGeneratedReplies ? 1 : 0);
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001667 out.writeInt(mSemanticAction);
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001668 }
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001669
Griff Hazen959591e2014-05-15 22:26:18 -07001670 public static final Parcelable.Creator<Action> CREATOR =
1671 new Parcelable.Creator<Action>() {
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001672 public Action createFromParcel(Parcel in) {
1673 return new Action(in);
1674 }
1675 public Action[] newArray(int size) {
1676 return new Action[size];
1677 }
1678 };
Griff Hazen61a9e862014-05-22 16:05:19 -07001679
1680 /**
1681 * Extender interface for use with {@link Builder#extend}. Extenders may be used to add
1682 * metadata or change options on an action builder.
1683 */
1684 public interface Extender {
1685 /**
1686 * Apply this extender to a notification action builder.
1687 * @param builder the builder to be modified.
1688 * @return the build object for chaining.
1689 */
1690 public Builder extend(Builder builder);
1691 }
1692
1693 /**
1694 * Wearable extender for notification actions. To add extensions to an action,
1695 * create a new {@link android.app.Notification.Action.WearableExtender} object using
1696 * the {@code WearableExtender()} constructor and apply it to a
1697 * {@link android.app.Notification.Action.Builder} using
1698 * {@link android.app.Notification.Action.Builder#extend}.
1699 *
1700 * <pre class="prettyprint">
1701 * Notification.Action action = new Notification.Action.Builder(
1702 * R.drawable.archive_all, "Archive all", actionIntent)
Griff Hazen14f57992014-05-26 09:07:14 -07001703 * .extend(new Notification.Action.WearableExtender()
Griff Hazen61a9e862014-05-22 16:05:19 -07001704 * .setAvailableOffline(false))
Griff Hazen14f57992014-05-26 09:07:14 -07001705 * .build();</pre>
Griff Hazen61a9e862014-05-22 16:05:19 -07001706 */
1707 public static final class WearableExtender implements Extender {
1708 /** Notification action extra which contains wearable extensions */
1709 private static final String EXTRA_WEARABLE_EXTENSIONS = "android.wearable.EXTENSIONS";
1710
Pete Gastaf6781d2014-10-07 15:17:05 -04001711 // Keys within EXTRA_WEARABLE_EXTENSIONS for wearable options.
Griff Hazen61a9e862014-05-22 16:05:19 -07001712 private static final String KEY_FLAGS = "flags";
Pete Gastaf6781d2014-10-07 15:17:05 -04001713 private static final String KEY_IN_PROGRESS_LABEL = "inProgressLabel";
1714 private static final String KEY_CONFIRM_LABEL = "confirmLabel";
1715 private static final String KEY_CANCEL_LABEL = "cancelLabel";
Griff Hazen61a9e862014-05-22 16:05:19 -07001716
1717 // Flags bitwise-ored to mFlags
1718 private static final int FLAG_AVAILABLE_OFFLINE = 0x1;
Alex Hills9ab3a232016-04-05 14:54:56 -04001719 private static final int FLAG_HINT_LAUNCHES_ACTIVITY = 1 << 1;
Alex Hills9f087612016-06-07 09:08:59 -04001720 private static final int FLAG_HINT_DISPLAY_INLINE = 1 << 2;
Griff Hazen61a9e862014-05-22 16:05:19 -07001721
1722 // Default value for flags integer
1723 private static final int DEFAULT_FLAGS = FLAG_AVAILABLE_OFFLINE;
1724
1725 private int mFlags = DEFAULT_FLAGS;
1726
Pete Gastaf6781d2014-10-07 15:17:05 -04001727 private CharSequence mInProgressLabel;
1728 private CharSequence mConfirmLabel;
1729 private CharSequence mCancelLabel;
1730
Griff Hazen61a9e862014-05-22 16:05:19 -07001731 /**
1732 * Create a {@link android.app.Notification.Action.WearableExtender} with default
1733 * options.
1734 */
1735 public WearableExtender() {
1736 }
1737
1738 /**
1739 * Create a {@link android.app.Notification.Action.WearableExtender} by reading
1740 * wearable options present in an existing notification action.
1741 * @param action the notification action to inspect.
1742 */
1743 public WearableExtender(Action action) {
1744 Bundle wearableBundle = action.getExtras().getBundle(EXTRA_WEARABLE_EXTENSIONS);
1745 if (wearableBundle != null) {
1746 mFlags = wearableBundle.getInt(KEY_FLAGS, DEFAULT_FLAGS);
Pete Gastaf6781d2014-10-07 15:17:05 -04001747 mInProgressLabel = wearableBundle.getCharSequence(KEY_IN_PROGRESS_LABEL);
1748 mConfirmLabel = wearableBundle.getCharSequence(KEY_CONFIRM_LABEL);
1749 mCancelLabel = wearableBundle.getCharSequence(KEY_CANCEL_LABEL);
Griff Hazen61a9e862014-05-22 16:05:19 -07001750 }
1751 }
1752
1753 /**
1754 * Apply wearable extensions to a notification action that is being built. This is
1755 * typically called by the {@link android.app.Notification.Action.Builder#extend}
1756 * method of {@link android.app.Notification.Action.Builder}.
1757 */
1758 @Override
1759 public Action.Builder extend(Action.Builder builder) {
1760 Bundle wearableBundle = new Bundle();
1761
1762 if (mFlags != DEFAULT_FLAGS) {
1763 wearableBundle.putInt(KEY_FLAGS, mFlags);
1764 }
Pete Gastaf6781d2014-10-07 15:17:05 -04001765 if (mInProgressLabel != null) {
1766 wearableBundle.putCharSequence(KEY_IN_PROGRESS_LABEL, mInProgressLabel);
1767 }
1768 if (mConfirmLabel != null) {
1769 wearableBundle.putCharSequence(KEY_CONFIRM_LABEL, mConfirmLabel);
1770 }
1771 if (mCancelLabel != null) {
1772 wearableBundle.putCharSequence(KEY_CANCEL_LABEL, mCancelLabel);
1773 }
Griff Hazen61a9e862014-05-22 16:05:19 -07001774
1775 builder.getExtras().putBundle(EXTRA_WEARABLE_EXTENSIONS, wearableBundle);
1776 return builder;
1777 }
1778
1779 @Override
1780 public WearableExtender clone() {
1781 WearableExtender that = new WearableExtender();
1782 that.mFlags = this.mFlags;
Pete Gastaf6781d2014-10-07 15:17:05 -04001783 that.mInProgressLabel = this.mInProgressLabel;
1784 that.mConfirmLabel = this.mConfirmLabel;
1785 that.mCancelLabel = this.mCancelLabel;
Griff Hazen61a9e862014-05-22 16:05:19 -07001786 return that;
1787 }
1788
1789 /**
1790 * Set whether this action is available when the wearable device is not connected to
1791 * a companion device. The user can still trigger this action when the wearable device is
1792 * offline, but a visual hint will indicate that the action may not be available.
1793 * Defaults to true.
1794 */
1795 public WearableExtender setAvailableOffline(boolean availableOffline) {
1796 setFlag(FLAG_AVAILABLE_OFFLINE, availableOffline);
1797 return this;
1798 }
1799
1800 /**
1801 * Get whether this action is available when the wearable device is not connected to
1802 * a companion device. The user can still trigger this action when the wearable device is
1803 * offline, but a visual hint will indicate that the action may not be available.
1804 * Defaults to true.
1805 */
1806 public boolean isAvailableOffline() {
1807 return (mFlags & FLAG_AVAILABLE_OFFLINE) != 0;
1808 }
1809
1810 private void setFlag(int mask, boolean value) {
1811 if (value) {
1812 mFlags |= mask;
1813 } else {
1814 mFlags &= ~mask;
1815 }
1816 }
Pete Gastaf6781d2014-10-07 15:17:05 -04001817
1818 /**
1819 * Set a label to display while the wearable is preparing to automatically execute the
1820 * action. This is usually a 'ing' verb ending in ellipsis like "Sending..."
1821 *
1822 * @param label the label to display while the action is being prepared to execute
1823 * @return this object for method chaining
1824 */
1825 public WearableExtender setInProgressLabel(CharSequence label) {
1826 mInProgressLabel = label;
1827 return this;
1828 }
1829
1830 /**
1831 * Get the label to display while the wearable is preparing to automatically execute
1832 * the action. This is usually a 'ing' verb ending in ellipsis like "Sending..."
1833 *
1834 * @return the label to display while the action is being prepared to execute
1835 */
1836 public CharSequence getInProgressLabel() {
1837 return mInProgressLabel;
1838 }
1839
1840 /**
1841 * Set a label to display to confirm that the action should be executed.
1842 * This is usually an imperative verb like "Send".
1843 *
1844 * @param label the label to confirm the action should be executed
1845 * @return this object for method chaining
1846 */
1847 public WearableExtender setConfirmLabel(CharSequence label) {
1848 mConfirmLabel = label;
1849 return this;
1850 }
1851
1852 /**
1853 * Get the label to display to confirm that the action should be executed.
1854 * This is usually an imperative verb like "Send".
1855 *
1856 * @return the label to confirm the action should be executed
1857 */
1858 public CharSequence getConfirmLabel() {
1859 return mConfirmLabel;
1860 }
1861
1862 /**
1863 * Set a label to display to cancel the action.
1864 * This is usually an imperative verb, like "Cancel".
1865 *
1866 * @param label the label to display to cancel the action
1867 * @return this object for method chaining
1868 */
1869 public WearableExtender setCancelLabel(CharSequence label) {
1870 mCancelLabel = label;
1871 return this;
1872 }
1873
1874 /**
1875 * Get the label to display to cancel the action.
1876 * This is usually an imperative verb like "Cancel".
1877 *
1878 * @return the label to display to cancel the action
1879 */
1880 public CharSequence getCancelLabel() {
1881 return mCancelLabel;
1882 }
Alex Hills9ab3a232016-04-05 14:54:56 -04001883
1884 /**
1885 * Set a hint that this Action will launch an {@link Activity} directly, telling the
1886 * platform that it can generate the appropriate transitions.
1887 * @param hintLaunchesActivity {@code true} if the content intent will launch
1888 * an activity and transitions should be generated, false otherwise.
1889 * @return this object for method chaining
1890 */
Alex Hills4ec3ff42016-04-12 11:36:18 -04001891 public WearableExtender setHintLaunchesActivity(
Alex Hills9ab3a232016-04-05 14:54:56 -04001892 boolean hintLaunchesActivity) {
1893 setFlag(FLAG_HINT_LAUNCHES_ACTIVITY, hintLaunchesActivity);
1894 return this;
1895 }
1896
1897 /**
1898 * Get a hint that this Action will launch an {@link Activity} directly, telling the
1899 * platform that it can generate the appropriate transitions
1900 * @return {@code true} if the content intent will launch an activity and transitions
1901 * should be generated, false otherwise. The default value is {@code false} if this was
1902 * never set.
1903 */
Alex Hills4ec3ff42016-04-12 11:36:18 -04001904 public boolean getHintLaunchesActivity() {
Alex Hills9ab3a232016-04-05 14:54:56 -04001905 return (mFlags & FLAG_HINT_LAUNCHES_ACTIVITY) != 0;
1906 }
Alex Hills9f087612016-06-07 09:08:59 -04001907
1908 /**
1909 * Set a hint that this Action should be displayed inline.
1910 *
1911 * @param hintDisplayInline {@code true} if action should be displayed inline, false
1912 * otherwise
1913 * @return this object for method chaining
1914 */
1915 public WearableExtender setHintDisplayActionInline(
1916 boolean hintDisplayInline) {
1917 setFlag(FLAG_HINT_DISPLAY_INLINE, hintDisplayInline);
1918 return this;
1919 }
1920
1921 /**
1922 * Get a hint that this Action should be displayed inline.
1923 *
Dmitri Plotnikov9e9cfd92016-12-21 10:52:26 -08001924 * @return {@code true} if the Action should be displayed inline, {@code false}
Alex Hills9f087612016-06-07 09:08:59 -04001925 * otherwise. The default value is {@code false} if this was never set.
1926 */
1927 public boolean getHintDisplayActionInline() {
1928 return (mFlags & FLAG_HINT_DISPLAY_INLINE) != 0;
1929 }
Griff Hazen61a9e862014-05-22 16:05:19 -07001930 }
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001931
1932 /**
1933 * Provides meaning to an {@link Action} that hints at what the associated
1934 * {@link PendingIntent} will do. For example, an {@link Action} with a
1935 * {@link PendingIntent} that replies to a text message notification may have the
1936 * {@link #SEMANTIC_ACTION_REPLY} {@code SemanticAction} set within it.
1937 *
1938 * @hide
1939 */
1940 @IntDef(prefix = { "SEMANTIC_ACTION_" }, value = {
1941 SEMANTIC_ACTION_NONE,
1942 SEMANTIC_ACTION_REPLY,
1943 SEMANTIC_ACTION_MARK_AS_READ,
1944 SEMANTIC_ACTION_MARK_AS_UNREAD,
1945 SEMANTIC_ACTION_DELETE,
1946 SEMANTIC_ACTION_ARCHIVE,
1947 SEMANTIC_ACTION_MUTE,
1948 SEMANTIC_ACTION_UNMUTE,
1949 SEMANTIC_ACTION_THUMBS_UP,
1950 SEMANTIC_ACTION_THUMBS_DOWN
1951 })
1952 @Retention(RetentionPolicy.SOURCE)
1953 public @interface SemanticAction {}
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001954 }
1955
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001956 /**
1957 * Array of all {@link Action} structures attached to this notification by
1958 * {@link Builder#addAction(int, CharSequence, PendingIntent)}. Mostly useful for instances of
1959 * {@link android.service.notification.NotificationListenerService} that provide an alternative
1960 * interface for invoking actions.
1961 */
Daniel Sandlerea2a3172013-02-20 22:24:20 -05001962 public Action[] actions;
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001963
1964 /**
Dan Sandler0bf2ed82013-12-21 23:33:41 -06001965 * Replacement version of this notification whose content will be shown
1966 * in an insecure context such as atop a secure keyguard. See {@link #visibility}
1967 * and {@link #VISIBILITY_PUBLIC}.
1968 */
1969 public Notification publicVersion;
1970
1971 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001972 * Constructs a Notification object with default values.
Joe Onorato46439ce2010-11-19 13:56:21 -08001973 * You might want to consider using {@link Builder} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001974 */
1975 public Notification()
1976 {
1977 this.when = System.currentTimeMillis();
Selim Cinekb85f36fd2016-04-20 18:46:36 -07001978 this.creationTime = System.currentTimeMillis();
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001979 this.priority = PRIORITY_DEFAULT;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001980 }
1981
1982 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001983 * @hide
1984 */
1985 public Notification(Context context, int icon, CharSequence tickerText, long when,
1986 CharSequence contentTitle, CharSequence contentText, Intent contentIntent)
1987 {
Chris Wren1ce4b6d2015-06-11 10:19:43 -04001988 new Builder(context)
1989 .setWhen(when)
1990 .setSmallIcon(icon)
1991 .setTicker(tickerText)
1992 .setContentTitle(contentTitle)
1993 .setContentText(contentText)
1994 .setContentIntent(PendingIntent.getActivity(context, 0, contentIntent, 0))
1995 .buildInto(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001996 }
1997
1998 /**
1999 * Constructs a Notification object with the information needed to
2000 * have a status bar icon without the standard expanded view.
2001 *
2002 * @param icon The resource id of the icon to put in the status bar.
2003 * @param tickerText The text that flows by in the status bar when the notification first
2004 * activates.
2005 * @param when The time to show in the time field. In the System.currentTimeMillis
2006 * timebase.
Joe Onorato46439ce2010-11-19 13:56:21 -08002007 *
2008 * @deprecated Use {@link Builder} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002009 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002010 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002011 public Notification(int icon, CharSequence tickerText, long when)
2012 {
2013 this.icon = icon;
2014 this.tickerText = tickerText;
2015 this.when = when;
Selim Cinekb85f36fd2016-04-20 18:46:36 -07002016 this.creationTime = System.currentTimeMillis();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002017 }
2018
2019 /**
2020 * Unflatten the notification from a parcel.
2021 */
Svet Ganovddb94882016-06-23 19:55:24 -07002022 @SuppressWarnings("unchecked")
2023 public Notification(Parcel parcel) {
2024 // IMPORTANT: Add unmarshaling code in readFromParcel as the pending
2025 // intents in extras are always written as the last entry.
2026 readFromParcelImpl(parcel);
2027 // Must be read last!
Felipe Lemedd85da62016-06-28 11:29:54 -07002028 allPendingIntents = (ArraySet<PendingIntent>) parcel.readArraySet(null);
Svet Ganovddb94882016-06-23 19:55:24 -07002029 }
2030
2031 private void readFromParcelImpl(Parcel parcel)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002032 {
2033 int version = parcel.readInt();
2034
Adrian Roosfb921842017-10-26 14:49:56 +02002035 mWhitelistToken = parcel.readStrongBinder();
2036 if (mWhitelistToken == null) {
2037 mWhitelistToken = processWhitelistToken;
Dianne Hackborn98305522017-05-05 17:53:53 -07002038 }
2039 // Propagate this token to all pending intents that are unmarshalled from the parcel.
Adrian Roosfb921842017-10-26 14:49:56 +02002040 parcel.setClassCookie(PendingIntent.class, mWhitelistToken);
Dianne Hackborn98305522017-05-05 17:53:53 -07002041
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002042 when = parcel.readLong();
Selim Cinekb85f36fd2016-04-20 18:46:36 -07002043 creationTime = parcel.readLong();
Dan Sandler3936e7a2015-05-19 20:59:12 -04002044 if (parcel.readInt() != 0) {
2045 mSmallIcon = Icon.CREATOR.createFromParcel(parcel);
Dan Sandler4e787062015-06-17 15:09:48 -04002046 if (mSmallIcon.getType() == Icon.TYPE_RESOURCE) {
2047 icon = mSmallIcon.getResId();
2048 }
Dan Sandler3936e7a2015-05-19 20:59:12 -04002049 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002050 number = parcel.readInt();
2051 if (parcel.readInt() != 0) {
2052 contentIntent = PendingIntent.CREATOR.createFromParcel(parcel);
2053 }
2054 if (parcel.readInt() != 0) {
2055 deleteIntent = PendingIntent.CREATOR.createFromParcel(parcel);
2056 }
2057 if (parcel.readInt() != 0) {
2058 tickerText = TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(parcel);
2059 }
2060 if (parcel.readInt() != 0) {
Joe Onorato46439ce2010-11-19 13:56:21 -08002061 tickerView = RemoteViews.CREATOR.createFromParcel(parcel);
Joe Onoratoef1e7762010-09-17 18:38:38 -04002062 }
2063 if (parcel.readInt() != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002064 contentView = RemoteViews.CREATOR.createFromParcel(parcel);
2065 }
Joe Onorato561d3852010-11-20 18:09:34 -08002066 if (parcel.readInt() != 0) {
Dan Sandlerd63f9322015-05-06 15:18:49 -04002067 mLargeIcon = Icon.CREATOR.createFromParcel(parcel);
Joe Onorato561d3852010-11-20 18:09:34 -08002068 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002069 defaults = parcel.readInt();
2070 flags = parcel.readInt();
2071 if (parcel.readInt() != 0) {
2072 sound = Uri.CREATOR.createFromParcel(parcel);
2073 }
2074
2075 audioStreamType = parcel.readInt();
John Spurlockc0650f022014-07-19 13:22:39 -04002076 if (parcel.readInt() != 0) {
2077 audioAttributes = AudioAttributes.CREATOR.createFromParcel(parcel);
2078 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002079 vibrate = parcel.createLongArray();
2080 ledARGB = parcel.readInt();
2081 ledOnMS = parcel.readInt();
2082 ledOffMS = parcel.readInt();
2083 iconLevel = parcel.readInt();
Daniel Sandlere46cbd32010-06-17 10:35:26 -04002084
2085 if (parcel.readInt() != 0) {
2086 fullScreenIntent = PendingIntent.CREATOR.createFromParcel(parcel);
2087 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002088
2089 priority = parcel.readInt();
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002090
John Spurlockfd7f1e02014-03-18 16:41:57 -04002091 category = parcel.readString();
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002092
Griff Hazen5cadc3b2014-05-20 09:55:39 -07002093 mGroupKey = parcel.readString();
2094
2095 mSortKey = parcel.readString();
2096
Jeff Sharkeya04c7a72016-03-18 12:20:36 -06002097 extras = Bundle.setDefusable(parcel.readBundle(), true); // may be null
Robin Leead7e72a2017-12-04 15:45:46 +01002098 fixDuplicateExtras();
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002099
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002100 actions = parcel.createTypedArray(Action.CREATOR); // may be null
2101
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002102 if (parcel.readInt() != 0) {
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002103 bigContentView = RemoteViews.CREATOR.createFromParcel(parcel);
2104 }
Dan Sandler0bf2ed82013-12-21 23:33:41 -06002105
Chris Wren8fd39ec2014-02-27 17:43:26 -05002106 if (parcel.readInt() != 0) {
2107 headsUpContentView = RemoteViews.CREATOR.createFromParcel(parcel);
2108 }
2109
Dan Sandler0bf2ed82013-12-21 23:33:41 -06002110 visibility = parcel.readInt();
2111
2112 if (parcel.readInt() != 0) {
2113 publicVersion = Notification.CREATOR.createFromParcel(parcel);
2114 }
Dan Sandler26e81cf2014-05-06 10:01:27 -04002115
2116 color = parcel.readInt();
Julia Reynoldsb5e44b72016-08-16 15:00:25 -04002117
2118 if (parcel.readInt() != 0) {
2119 mChannelId = parcel.readString();
2120 }
Julia Reynolds2a128742016-11-28 14:29:25 -05002121 mTimeout = parcel.readLong();
Julia Reynolds13d898c2017-02-02 12:22:05 -05002122
2123 if (parcel.readInt() != 0) {
2124 mShortcutId = parcel.readString();
2125 }
2126
2127 mBadgeIcon = parcel.readInt();
Julia Reynolds3aedded2017-03-31 14:42:09 -04002128
2129 if (parcel.readInt() != 0) {
2130 mSettingsText = TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(parcel);
2131 }
Julia Reynoldsa79c3712017-04-21 10:29:57 -04002132
2133 mGroupAlertBehavior = parcel.readInt();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002134 }
2135
Andy Stadler110988c2010-12-03 14:29:16 -08002136 @Override
Joe Onorato18e69df2010-05-17 22:26:12 -07002137 public Notification clone() {
2138 Notification that = new Notification();
Daniel Sandler1a497d32013-04-18 14:52:45 -04002139 cloneInto(that, true);
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002140 return that;
2141 }
Joe Onorato18e69df2010-05-17 22:26:12 -07002142
Daniel Sandler1a497d32013-04-18 14:52:45 -04002143 /**
2144 * Copy all (or if heavy is false, all except Bitmaps and RemoteViews) members
2145 * of this into that.
2146 * @hide
2147 */
2148 public void cloneInto(Notification that, boolean heavy) {
Adrian Roosfb921842017-10-26 14:49:56 +02002149 that.mWhitelistToken = this.mWhitelistToken;
Joe Onorato18e69df2010-05-17 22:26:12 -07002150 that.when = this.when;
Selim Cinekb85f36fd2016-04-20 18:46:36 -07002151 that.creationTime = this.creationTime;
Dan Sandlerd63f9322015-05-06 15:18:49 -04002152 that.mSmallIcon = this.mSmallIcon;
Joe Onorato18e69df2010-05-17 22:26:12 -07002153 that.number = this.number;
2154
2155 // PendingIntents are global, so there's no reason (or way) to clone them.
2156 that.contentIntent = this.contentIntent;
2157 that.deleteIntent = this.deleteIntent;
Daniel Sandlere46cbd32010-06-17 10:35:26 -04002158 that.fullScreenIntent = this.fullScreenIntent;
Joe Onorato18e69df2010-05-17 22:26:12 -07002159
2160 if (this.tickerText != null) {
2161 that.tickerText = this.tickerText.toString();
2162 }
Daniel Sandler1a497d32013-04-18 14:52:45 -04002163 if (heavy && this.tickerView != null) {
Joe Onorato46439ce2010-11-19 13:56:21 -08002164 that.tickerView = this.tickerView.clone();
Joe Onoratoef1e7762010-09-17 18:38:38 -04002165 }
Daniel Sandler1a497d32013-04-18 14:52:45 -04002166 if (heavy && this.contentView != null) {
Joe Onorato18e69df2010-05-17 22:26:12 -07002167 that.contentView = this.contentView.clone();
2168 }
Dan Sandlerd63f9322015-05-06 15:18:49 -04002169 if (heavy && this.mLargeIcon != null) {
2170 that.mLargeIcon = this.mLargeIcon;
Joe Onorato561d3852010-11-20 18:09:34 -08002171 }
Jozef BABJAKa8b91832011-02-22 08:05:08 +01002172 that.iconLevel = this.iconLevel;
Joe Onorato18e69df2010-05-17 22:26:12 -07002173 that.sound = this.sound; // android.net.Uri is immutable
2174 that.audioStreamType = this.audioStreamType;
John Spurlockc0650f022014-07-19 13:22:39 -04002175 if (this.audioAttributes != null) {
2176 that.audioAttributes = new AudioAttributes.Builder(this.audioAttributes).build();
2177 }
Joe Onorato18e69df2010-05-17 22:26:12 -07002178
2179 final long[] vibrate = this.vibrate;
2180 if (vibrate != null) {
2181 final int N = vibrate.length;
2182 final long[] vib = that.vibrate = new long[N];
2183 System.arraycopy(vibrate, 0, vib, 0, N);
2184 }
2185
2186 that.ledARGB = this.ledARGB;
2187 that.ledOnMS = this.ledOnMS;
2188 that.ledOffMS = this.ledOffMS;
2189 that.defaults = this.defaults;
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002190
Joe Onorato18e69df2010-05-17 22:26:12 -07002191 that.flags = this.flags;
2192
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002193 that.priority = this.priority;
Joe Malin8d40d042012-11-05 11:36:40 -08002194
John Spurlockfd7f1e02014-03-18 16:41:57 -04002195 that.category = this.category;
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002196
Griff Hazen5cadc3b2014-05-20 09:55:39 -07002197 that.mGroupKey = this.mGroupKey;
2198
2199 that.mSortKey = this.mSortKey;
2200
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002201 if (this.extras != null) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04002202 try {
2203 that.extras = new Bundle(this.extras);
2204 // will unparcel
2205 that.extras.size();
2206 } catch (BadParcelableException e) {
2207 Log.e(TAG, "could not unparcel extras from notification: " + this, e);
2208 that.extras = null;
2209 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002210 }
2211
Felipe Lemedd85da62016-06-28 11:29:54 -07002212 if (!ArrayUtils.isEmpty(allPendingIntents)) {
2213 that.allPendingIntents = new ArraySet<>(allPendingIntents);
Svet Ganovddb94882016-06-23 19:55:24 -07002214 }
2215
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002216 if (this.actions != null) {
2217 that.actions = new Action[this.actions.length];
2218 for(int i=0; i<this.actions.length; i++) {
liangweikang63b03b52017-03-16 19:22:15 +08002219 if ( this.actions[i] != null) {
2220 that.actions[i] = this.actions[i].clone();
2221 }
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002222 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002223 }
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002224
Daniel Sandler1a497d32013-04-18 14:52:45 -04002225 if (heavy && this.bigContentView != null) {
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002226 that.bigContentView = this.bigContentView.clone();
2227 }
Daniel Sandler1a497d32013-04-18 14:52:45 -04002228
Chris Wren8fd39ec2014-02-27 17:43:26 -05002229 if (heavy && this.headsUpContentView != null) {
2230 that.headsUpContentView = this.headsUpContentView.clone();
2231 }
2232
Dan Sandler0bf2ed82013-12-21 23:33:41 -06002233 that.visibility = this.visibility;
2234
2235 if (this.publicVersion != null) {
2236 that.publicVersion = new Notification();
2237 this.publicVersion.cloneInto(that.publicVersion, heavy);
2238 }
2239
Dan Sandler26e81cf2014-05-06 10:01:27 -04002240 that.color = this.color;
2241
Julia Reynoldsb5e44b72016-08-16 15:00:25 -04002242 that.mChannelId = this.mChannelId;
Julia Reynolds2a128742016-11-28 14:29:25 -05002243 that.mTimeout = this.mTimeout;
Julia Reynolds3aedded2017-03-31 14:42:09 -04002244 that.mShortcutId = this.mShortcutId;
2245 that.mBadgeIcon = this.mBadgeIcon;
2246 that.mSettingsText = this.mSettingsText;
Julia Reynoldsa79c3712017-04-21 10:29:57 -04002247 that.mGroupAlertBehavior = this.mGroupAlertBehavior;
Julia Reynoldsb5e44b72016-08-16 15:00:25 -04002248
Daniel Sandler1a497d32013-04-18 14:52:45 -04002249 if (!heavy) {
2250 that.lightenPayload(); // will clean out extras
2251 }
2252 }
2253
2254 /**
2255 * Removes heavyweight parts of the Notification object for archival or for sending to
2256 * listeners when the full contents are not necessary.
2257 * @hide
2258 */
2259 public final void lightenPayload() {
2260 tickerView = null;
2261 contentView = null;
2262 bigContentView = null;
Chris Wren8fd39ec2014-02-27 17:43:26 -05002263 headsUpContentView = null;
Dan Sandlerd63f9322015-05-06 15:18:49 -04002264 mLargeIcon = null;
Dan Sandler50128532015-12-08 15:42:41 -05002265 if (extras != null && !extras.isEmpty()) {
2266 final Set<String> keyset = extras.keySet();
2267 final int N = keyset.size();
2268 final String[] keys = keyset.toArray(new String[N]);
2269 for (int i=0; i<N; i++) {
2270 final String key = keys[i];
Dmitri Plotnikov22281362017-01-30 11:16:21 -08002271 if (TvExtender.EXTRA_TV_EXTENDER.equals(key)) {
2272 continue;
2273 }
Dan Sandler50128532015-12-08 15:42:41 -05002274 final Object obj = extras.get(key);
2275 if (obj != null &&
2276 ( obj instanceof Parcelable
2277 || obj instanceof Parcelable[]
2278 || obj instanceof SparseArray
2279 || obj instanceof ArrayList)) {
2280 extras.remove(key);
2281 }
2282 }
Daniel Sandler1a497d32013-04-18 14:52:45 -04002283 }
Joe Onorato18e69df2010-05-17 22:26:12 -07002284 }
2285
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04002286 /**
2287 * Make sure this CharSequence is safe to put into a bundle, which basically
2288 * means it had better not be some custom Parcelable implementation.
2289 * @hide
2290 */
2291 public static CharSequence safeCharSequence(CharSequence cs) {
Christoph Studer535ec612014-09-03 15:47:47 +02002292 if (cs == null) return cs;
2293 if (cs.length() > MAX_CHARSEQUENCE_LENGTH) {
2294 cs = cs.subSequence(0, MAX_CHARSEQUENCE_LENGTH);
2295 }
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04002296 if (cs instanceof Parcelable) {
2297 Log.e(TAG, "warning: " + cs.getClass().getCanonicalName()
2298 + " instance is a custom Parcelable and not allowed in Notification");
2299 return cs.toString();
2300 }
Selim Cinek60a54252016-02-26 17:03:25 -08002301 return removeTextSizeSpans(cs);
2302 }
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04002303
Selim Cinek60a54252016-02-26 17:03:25 -08002304 private static CharSequence removeTextSizeSpans(CharSequence charSequence) {
2305 if (charSequence instanceof Spanned) {
2306 Spanned ss = (Spanned) charSequence;
2307 Object[] spans = ss.getSpans(0, ss.length(), Object.class);
2308 SpannableStringBuilder builder = new SpannableStringBuilder(ss.toString());
2309 for (Object span : spans) {
2310 Object resultSpan = span;
Selim Cinek89991a22016-03-07 19:51:54 -08002311 if (resultSpan instanceof CharacterStyle) {
2312 resultSpan = ((CharacterStyle) span).getUnderlying();
2313 }
2314 if (resultSpan instanceof TextAppearanceSpan) {
2315 TextAppearanceSpan originalSpan = (TextAppearanceSpan) resultSpan;
Selim Cinek60a54252016-02-26 17:03:25 -08002316 resultSpan = new TextAppearanceSpan(
2317 originalSpan.getFamily(),
2318 originalSpan.getTextStyle(),
2319 -1,
2320 originalSpan.getTextColor(),
2321 originalSpan.getLinkTextColor());
Selim Cinek89991a22016-03-07 19:51:54 -08002322 } else if (resultSpan instanceof RelativeSizeSpan
2323 || resultSpan instanceof AbsoluteSizeSpan) {
Selim Cinek60a54252016-02-26 17:03:25 -08002324 continue;
Selim Cinek89991a22016-03-07 19:51:54 -08002325 } else {
2326 resultSpan = span;
Selim Cinek60a54252016-02-26 17:03:25 -08002327 }
2328 builder.setSpan(resultSpan, ss.getSpanStart(span), ss.getSpanEnd(span),
2329 ss.getSpanFlags(span));
2330 }
2331 return builder;
2332 }
2333 return charSequence;
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04002334 }
2335
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002336 public int describeContents() {
2337 return 0;
2338 }
2339
2340 /**
Dan Sandler4e787062015-06-17 15:09:48 -04002341 * Flatten this notification into a parcel.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002342 */
Svet Ganovddb94882016-06-23 19:55:24 -07002343 public void writeToParcel(Parcel parcel, int flags) {
2344 // We need to mark all pending intents getting into the notification
2345 // system as being put there to later allow the notification ranker
2346 // to launch them and by doing so add the app to the battery saver white
2347 // list for a short period of time. The problem is that the system
2348 // cannot look into the extras as there may be parcelables there that
2349 // the platform does not know how to handle. To go around that we have
2350 // an explicit list of the pending intents in the extras bundle.
Felipe Lemedd85da62016-06-28 11:29:54 -07002351 final boolean collectPendingIntents = (allPendingIntents == null);
Svet Ganovddb94882016-06-23 19:55:24 -07002352 if (collectPendingIntents) {
2353 PendingIntent.setOnMarshaledListener(
2354 (PendingIntent intent, Parcel out, int outFlags) -> {
2355 if (parcel == out) {
Felipe Lemedd85da62016-06-28 11:29:54 -07002356 if (allPendingIntents == null) {
2357 allPendingIntents = new ArraySet<>();
Svet Ganovddb94882016-06-23 19:55:24 -07002358 }
Felipe Lemedd85da62016-06-28 11:29:54 -07002359 allPendingIntents.add(intent);
Svet Ganovddb94882016-06-23 19:55:24 -07002360 }
2361 });
2362 }
2363 try {
2364 // IMPORTANT: Add marshaling code in writeToParcelImpl as we
Julia Reynolds13d898c2017-02-02 12:22:05 -05002365 // want to intercept all pending events written to the parcel.
Svet Ganovddb94882016-06-23 19:55:24 -07002366 writeToParcelImpl(parcel, flags);
2367 // Must be written last!
Felipe Lemedd85da62016-06-28 11:29:54 -07002368 parcel.writeArraySet(allPendingIntents);
Svet Ganovddb94882016-06-23 19:55:24 -07002369 } finally {
2370 if (collectPendingIntents) {
2371 PendingIntent.setOnMarshaledListener(null);
2372 }
2373 }
2374 }
2375
2376 private void writeToParcelImpl(Parcel parcel, int flags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002377 parcel.writeInt(1);
2378
Adrian Roosfb921842017-10-26 14:49:56 +02002379 parcel.writeStrongBinder(mWhitelistToken);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002380 parcel.writeLong(when);
Selim Cinekb85f36fd2016-04-20 18:46:36 -07002381 parcel.writeLong(creationTime);
Dan Sandler4e787062015-06-17 15:09:48 -04002382 if (mSmallIcon == null && icon != 0) {
2383 // you snuck an icon in here without using the builder; let's try to keep it
2384 mSmallIcon = Icon.createWithResource("", icon);
2385 }
Dan Sandler3936e7a2015-05-19 20:59:12 -04002386 if (mSmallIcon != null) {
2387 parcel.writeInt(1);
2388 mSmallIcon.writeToParcel(parcel, 0);
2389 } else {
2390 parcel.writeInt(0);
2391 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002392 parcel.writeInt(number);
2393 if (contentIntent != null) {
2394 parcel.writeInt(1);
2395 contentIntent.writeToParcel(parcel, 0);
2396 } else {
2397 parcel.writeInt(0);
2398 }
2399 if (deleteIntent != null) {
2400 parcel.writeInt(1);
2401 deleteIntent.writeToParcel(parcel, 0);
2402 } else {
2403 parcel.writeInt(0);
2404 }
2405 if (tickerText != null) {
2406 parcel.writeInt(1);
2407 TextUtils.writeToParcel(tickerText, parcel, flags);
2408 } else {
2409 parcel.writeInt(0);
2410 }
Joe Onorato46439ce2010-11-19 13:56:21 -08002411 if (tickerView != null) {
Joe Onoratoef1e7762010-09-17 18:38:38 -04002412 parcel.writeInt(1);
Joe Onorato46439ce2010-11-19 13:56:21 -08002413 tickerView.writeToParcel(parcel, 0);
Joe Onoratoef1e7762010-09-17 18:38:38 -04002414 } else {
2415 parcel.writeInt(0);
2416 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002417 if (contentView != null) {
2418 parcel.writeInt(1);
2419 contentView.writeToParcel(parcel, 0);
2420 } else {
2421 parcel.writeInt(0);
2422 }
Selim Cinek279fa862016-06-14 10:57:25 -07002423 if (mLargeIcon == null && largeIcon != null) {
2424 // you snuck an icon in here without using the builder; let's try to keep it
2425 mLargeIcon = Icon.createWithBitmap(largeIcon);
2426 }
Dan Sandlerd63f9322015-05-06 15:18:49 -04002427 if (mLargeIcon != null) {
Joe Onorato561d3852010-11-20 18:09:34 -08002428 parcel.writeInt(1);
Dan Sandlerd63f9322015-05-06 15:18:49 -04002429 mLargeIcon.writeToParcel(parcel, 0);
Joe Onorato561d3852010-11-20 18:09:34 -08002430 } else {
2431 parcel.writeInt(0);
2432 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002433
2434 parcel.writeInt(defaults);
2435 parcel.writeInt(this.flags);
2436
2437 if (sound != null) {
2438 parcel.writeInt(1);
2439 sound.writeToParcel(parcel, 0);
2440 } else {
2441 parcel.writeInt(0);
2442 }
2443 parcel.writeInt(audioStreamType);
John Spurlockc0650f022014-07-19 13:22:39 -04002444
2445 if (audioAttributes != null) {
2446 parcel.writeInt(1);
2447 audioAttributes.writeToParcel(parcel, 0);
2448 } else {
2449 parcel.writeInt(0);
2450 }
2451
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002452 parcel.writeLongArray(vibrate);
2453 parcel.writeInt(ledARGB);
2454 parcel.writeInt(ledOnMS);
2455 parcel.writeInt(ledOffMS);
2456 parcel.writeInt(iconLevel);
Daniel Sandlere46cbd32010-06-17 10:35:26 -04002457
2458 if (fullScreenIntent != null) {
2459 parcel.writeInt(1);
2460 fullScreenIntent.writeToParcel(parcel, 0);
2461 } else {
2462 parcel.writeInt(0);
2463 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002464
2465 parcel.writeInt(priority);
Joe Malin8d40d042012-11-05 11:36:40 -08002466
John Spurlockfd7f1e02014-03-18 16:41:57 -04002467 parcel.writeString(category);
Joe Malin8d40d042012-11-05 11:36:40 -08002468
Griff Hazen5cadc3b2014-05-20 09:55:39 -07002469 parcel.writeString(mGroupKey);
2470
2471 parcel.writeString(mSortKey);
2472
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002473 parcel.writeBundle(extras); // null ok
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002474
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002475 parcel.writeTypedArray(actions, 0); // null ok
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002476
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002477 if (bigContentView != null) {
2478 parcel.writeInt(1);
2479 bigContentView.writeToParcel(parcel, 0);
2480 } else {
2481 parcel.writeInt(0);
2482 }
Dan Sandler0bf2ed82013-12-21 23:33:41 -06002483
Chris Wren8fd39ec2014-02-27 17:43:26 -05002484 if (headsUpContentView != null) {
2485 parcel.writeInt(1);
2486 headsUpContentView.writeToParcel(parcel, 0);
2487 } else {
2488 parcel.writeInt(0);
2489 }
2490
Dan Sandler0bf2ed82013-12-21 23:33:41 -06002491 parcel.writeInt(visibility);
2492
2493 if (publicVersion != null) {
2494 parcel.writeInt(1);
2495 publicVersion.writeToParcel(parcel, 0);
2496 } else {
2497 parcel.writeInt(0);
2498 }
Dan Sandler26e81cf2014-05-06 10:01:27 -04002499
2500 parcel.writeInt(color);
Julia Reynoldsb5e44b72016-08-16 15:00:25 -04002501
2502 if (mChannelId != null) {
2503 parcel.writeInt(1);
2504 parcel.writeString(mChannelId);
2505 } else {
2506 parcel.writeInt(0);
2507 }
Julia Reynolds2a128742016-11-28 14:29:25 -05002508 parcel.writeLong(mTimeout);
Julia Reynolds13d898c2017-02-02 12:22:05 -05002509
2510 if (mShortcutId != null) {
2511 parcel.writeInt(1);
2512 parcel.writeString(mShortcutId);
2513 } else {
2514 parcel.writeInt(0);
2515 }
2516
2517 parcel.writeInt(mBadgeIcon);
Julia Reynolds3aedded2017-03-31 14:42:09 -04002518
2519 if (mSettingsText != null) {
2520 parcel.writeInt(1);
2521 TextUtils.writeToParcel(mSettingsText, parcel, flags);
2522 } else {
2523 parcel.writeInt(0);
2524 }
Julia Reynoldsa79c3712017-04-21 10:29:57 -04002525
2526 parcel.writeInt(mGroupAlertBehavior);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002527 }
2528
2529 /**
2530 * Parcelable.Creator that instantiates Notification objects
2531 */
2532 public static final Parcelable.Creator<Notification> CREATOR
2533 = new Parcelable.Creator<Notification>()
2534 {
2535 public Notification createFromParcel(Parcel parcel)
2536 {
2537 return new Notification(parcel);
2538 }
2539
2540 public Notification[] newArray(int size)
2541 {
2542 return new Notification[size];
2543 }
2544 };
2545
2546 /**
Robin Leead7e72a2017-12-04 15:45:46 +01002547 * Parcelling creates multiple copies of objects in {@code extras}. Fix them.
2548 * <p>
2549 * For backwards compatibility {@code extras} holds some references to "real" member data such
2550 * as {@link getLargeIcon()} which is mirrored by {@link #EXTRA_LARGE_ICON}. This is mostly
2551 * fine as long as the object stays in one process.
2552 * <p>
2553 * However, once the notification goes into a parcel each reference gets marshalled separately,
2554 * wasting memory. Especially with large images on Auto and TV, this is worth fixing.
2555 */
2556 private void fixDuplicateExtras() {
2557 if (extras != null) {
2558 fixDuplicateExtra(mSmallIcon, EXTRA_SMALL_ICON);
2559 fixDuplicateExtra(mLargeIcon, EXTRA_LARGE_ICON);
2560 }
2561 }
2562
2563 /**
2564 * If we find an extra that's exactly the same as one of the "real" fields but refers to a
2565 * separate object, replace it with the field's version to avoid holding duplicate copies.
2566 */
2567 private void fixDuplicateExtra(@Nullable Parcelable original, @NonNull String extraName) {
2568 if (original != null && extras.getParcelable(extraName) != null) {
2569 extras.putParcelable(extraName, original);
2570 }
2571 }
2572
2573 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002574 * Sets the {@link #contentView} field to be a view with the standard "Latest Event"
2575 * layout.
2576 *
2577 * <p>Uses the {@link #icon} and {@link #when} fields to set the icon and time fields
2578 * in the view.</p>
2579 * @param context The context for your application / activity.
2580 * @param contentTitle The title that goes in the expanded entry.
2581 * @param contentText The text that goes in the expanded entry.
2582 * @param contentIntent The intent to launch when the user clicks the expanded notification.
2583 * If this is an activity, it must include the
2584 * {@link android.content.Intent#FLAG_ACTIVITY_NEW_TASK} flag, which requires
Scott Main7aee61f2011-02-08 11:25:01 -08002585 * that you take care of task management as described in the
2586 * <a href="{@docRoot}guide/topics/fundamentals/tasks-and-back-stack.html">Tasks and Back
2587 * Stack</a> document.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002588 *
Joe Onorato46439ce2010-11-19 13:56:21 -08002589 * @deprecated Use {@link Builder} instead.
Chris Wrena05db382015-06-24 15:18:34 -04002590 * @removed
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002591 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002592 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002593 public void setLatestEventInfo(Context context,
2594 CharSequence contentTitle, CharSequence contentText, PendingIntent contentIntent) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002595 if (context.getApplicationInfo().targetSdkVersion > Build.VERSION_CODES.LOLLIPOP_MR1){
2596 Log.e(TAG, "setLatestEventInfo() is deprecated and you should feel deprecated.",
2597 new Throwable());
2598 }
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002599
Selim Cinek4ac6f602016-06-13 15:47:03 -07002600 if (context.getApplicationInfo().targetSdkVersion < Build.VERSION_CODES.N) {
2601 extras.putBoolean(EXTRA_SHOW_WHEN, true);
2602 }
2603
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002604 // ensure that any information already set directly is preserved
2605 final Notification.Builder builder = new Notification.Builder(context, this);
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002606
2607 // now apply the latestEventInfo fields
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002608 if (contentTitle != null) {
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002609 builder.setContentTitle(contentTitle);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002610 }
2611 if (contentText != null) {
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002612 builder.setContentText(contentText);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002613 }
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002614 builder.setContentIntent(contentIntent);
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002615
2616 builder.build(); // callers expect this notification to be ready to use
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002617 }
2618
Julia Reynoldsda303542015-11-23 14:00:20 -05002619 /**
2620 * @hide
2621 */
2622 public static void addFieldsFromContext(Context context, Notification notification) {
Julia Reynoldse071abd2017-03-22 10:52:11 -04002623 addFieldsFromContext(context.getApplicationInfo(), notification);
Jeff Sharkey012bc7b2016-04-11 16:30:27 -06002624 }
2625
2626 /**
2627 * @hide
2628 */
Julia Reynoldse071abd2017-03-22 10:52:11 -04002629 public static void addFieldsFromContext(ApplicationInfo ai, Notification notification) {
Jeff Sharkey012bc7b2016-04-11 16:30:27 -06002630 notification.extras.putParcelable(EXTRA_BUILDER_APPLICATION_INFO, ai);
Julia Reynoldsda303542015-11-23 14:00:20 -05002631 }
2632
Yi Jin6b514142017-10-30 14:54:12 -07002633 /**
2634 * @hide
2635 */
2636 public void writeToProto(ProtoOutputStream proto, long fieldId) {
2637 long token = proto.start(fieldId);
2638 proto.write(NotificationProto.CHANNEL_ID, getChannelId());
2639 proto.write(NotificationProto.HAS_TICKER_TEXT, this.tickerText != null);
2640 proto.write(NotificationProto.FLAGS, this.flags);
2641 proto.write(NotificationProto.COLOR, this.color);
2642 proto.write(NotificationProto.CATEGORY, this.category);
2643 proto.write(NotificationProto.GROUP_KEY, this.mGroupKey);
2644 proto.write(NotificationProto.SORT_KEY, this.mSortKey);
2645 if (this.actions != null) {
2646 proto.write(NotificationProto.ACTION_LENGTH, this.actions.length);
2647 }
2648 if (this.visibility >= VISIBILITY_SECRET && this.visibility <= VISIBILITY_PUBLIC) {
2649 proto.write(NotificationProto.VISIBILITY, this.visibility);
2650 }
2651 if (publicVersion != null) {
2652 publicVersion.writeToProto(proto, NotificationProto.PUBLIC_VERSION);
2653 }
2654 proto.end(token);
2655 }
2656
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002657 @Override
2658 public String toString() {
2659 StringBuilder sb = new StringBuilder();
Julia Reynoldsb9e712e2017-04-17 10:31:03 -04002660 sb.append("Notification(channel=");
Julia Reynoldsbad42972017-04-25 13:52:49 -04002661 sb.append(getChannelId());
Julia Reynoldsb9e712e2017-04-17 10:31:03 -04002662 sb.append(" pri=");
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002663 sb.append(priority);
2664 sb.append(" contentView=");
Joe Onoratoc9596d62011-01-12 17:03:11 -08002665 if (contentView != null) {
2666 sb.append(contentView.getPackage());
2667 sb.append("/0x");
2668 sb.append(Integer.toHexString(contentView.getLayoutId()));
2669 } else {
2670 sb.append("null");
2671 }
2672 sb.append(" vibrate=");
Daniel Sandler6738eee2012-11-16 12:03:32 -05002673 if ((this.defaults & DEFAULT_VIBRATE) != 0) {
2674 sb.append("default");
2675 } else if (this.vibrate != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002676 int N = this.vibrate.length-1;
2677 sb.append("[");
2678 for (int i=0; i<N; i++) {
2679 sb.append(this.vibrate[i]);
2680 sb.append(',');
2681 }
Simon Schoar8cf97d92009-06-10 22:08:37 +02002682 if (N != -1) {
2683 sb.append(this.vibrate[N]);
2684 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002685 sb.append("]");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002686 } else {
2687 sb.append("null");
2688 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002689 sb.append(" sound=");
Daniel Sandler6738eee2012-11-16 12:03:32 -05002690 if ((this.defaults & DEFAULT_SOUND) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002691 sb.append("default");
Daniel Sandler6738eee2012-11-16 12:03:32 -05002692 } else if (this.sound != null) {
2693 sb.append(this.sound.toString());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002694 } else {
2695 sb.append("null");
2696 }
Chris Wren365b6d32015-07-16 10:39:26 -04002697 if (this.tickerText != null) {
2698 sb.append(" tick");
2699 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002700 sb.append(" defaults=0x");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002701 sb.append(Integer.toHexString(this.defaults));
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002702 sb.append(" flags=0x");
Daniel Sandlere46cbd32010-06-17 10:35:26 -04002703 sb.append(Integer.toHexString(this.flags));
Dan Sandler26e81cf2014-05-06 10:01:27 -04002704 sb.append(String.format(" color=0x%08x", this.color));
Griff Hazen5cadc3b2014-05-20 09:55:39 -07002705 if (this.category != null) {
2706 sb.append(" category=");
2707 sb.append(this.category);
2708 }
2709 if (this.mGroupKey != null) {
2710 sb.append(" groupKey=");
2711 sb.append(this.mGroupKey);
2712 }
2713 if (this.mSortKey != null) {
2714 sb.append(" sortKey=");
2715 sb.append(this.mSortKey);
2716 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002717 if (actions != null) {
Dan Sandler1b718782014-07-18 12:43:45 -04002718 sb.append(" actions=");
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002719 sb.append(actions.length);
Dan Sandler1b718782014-07-18 12:43:45 -04002720 }
2721 sb.append(" vis=");
2722 sb.append(visibilityToString(this.visibility));
2723 if (this.publicVersion != null) {
2724 sb.append(" publicVersion=");
2725 sb.append(publicVersion.toString());
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002726 }
2727 sb.append(")");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002728 return sb.toString();
2729 }
Joe Onorato46439ce2010-11-19 13:56:21 -08002730
Dan Sandler1b718782014-07-18 12:43:45 -04002731 /**
2732 * {@hide}
2733 */
2734 public static String visibilityToString(int vis) {
2735 switch (vis) {
2736 case VISIBILITY_PRIVATE:
2737 return "PRIVATE";
2738 case VISIBILITY_PUBLIC:
2739 return "PUBLIC";
2740 case VISIBILITY_SECRET:
2741 return "SECRET";
2742 default:
2743 return "UNKNOWN(" + String.valueOf(vis) + ")";
2744 }
2745 }
2746
Joe Onoratocb109a02011-01-18 17:57:41 -08002747 /**
John Spurlock1d881a12015-03-18 19:21:54 -04002748 * {@hide}
2749 */
2750 public static String priorityToString(@Priority int pri) {
2751 switch (pri) {
2752 case PRIORITY_MIN:
2753 return "MIN";
2754 case PRIORITY_LOW:
2755 return "LOW";
2756 case PRIORITY_DEFAULT:
2757 return "DEFAULT";
2758 case PRIORITY_HIGH:
2759 return "HIGH";
2760 case PRIORITY_MAX:
2761 return "MAX";
2762 default:
2763 return "UNKNOWN(" + String.valueOf(pri) + ")";
2764 }
2765 }
2766
Julia Reynolds6ad0aec2017-07-05 08:47:03 -04002767 /**
2768 * @hide
2769 */
2770 public boolean hasCompletedProgress() {
2771 // not a progress notification; can't be complete
2772 if (!extras.containsKey(EXTRA_PROGRESS)
2773 || !extras.containsKey(EXTRA_PROGRESS_MAX)) {
2774 return false;
2775 }
2776 // many apps use max 0 for 'indeterminate'; not complete
2777 if (extras.getInt(EXTRA_PROGRESS_MAX) == 0) {
2778 return false;
2779 }
2780 return extras.getInt(EXTRA_PROGRESS) == extras.getInt(EXTRA_PROGRESS_MAX);
2781 }
2782
Jeff Sharkey000ce802017-04-29 13:13:27 -06002783 /** @removed */
2784 @Deprecated
Julia Reynolds37856052016-11-11 09:20:07 -05002785 public String getChannel() {
Julia Reynoldsb5e44b72016-08-16 15:00:25 -04002786 return mChannelId;
2787 }
2788
2789 /**
Julia Reynoldsbad42972017-04-25 13:52:49 -04002790 * Returns the id of the channel this notification posts to.
2791 */
2792 public String getChannelId() {
2793 return mChannelId;
2794 }
2795
Jeff Sharkey000ce802017-04-29 13:13:27 -06002796 /** @removed */
2797 @Deprecated
Julia Reynolds2a128742016-11-28 14:29:25 -05002798 public long getTimeout() {
2799 return mTimeout;
2800 }
2801
2802 /**
Julia Reynoldsbad42972017-04-25 13:52:49 -04002803 * Returns the duration from posting after which this notification should be canceled by the
2804 * system, if it's not canceled already.
2805 */
2806 public long getTimeoutAfter() {
2807 return mTimeout;
2808 }
2809
2810 /**
Julia Reynoldse071abd2017-03-22 10:52:11 -04002811 * Returns what icon should be shown for this notification if it is being displayed in a
2812 * Launcher that supports badging. Will be one of {@link #BADGE_ICON_NONE},
2813 * {@link #BADGE_ICON_SMALL}, or {@link #BADGE_ICON_LARGE}.
2814 */
2815 public int getBadgeIconType() {
2816 return mBadgeIcon;
2817 }
2818
2819 /**
Julia Reynolds13d898c2017-02-02 12:22:05 -05002820 * Returns the {@link ShortcutInfo#getId() id} that this notification supersedes, if any.
Julia Reynoldsbad42972017-04-25 13:52:49 -04002821 *
2822 * <p>Used by some Launchers that display notification content to hide shortcuts that duplicate
2823 * notifications.
Julia Reynolds13d898c2017-02-02 12:22:05 -05002824 */
2825 public String getShortcutId() {
2826 return mShortcutId;
2827 }
2828
Julia Reynolds3aedded2017-03-31 14:42:09 -04002829
2830 /**
2831 * Returns the settings text provided to {@link Builder#setSettingsText(CharSequence)}.
2832 */
2833 public CharSequence getSettingsText() {
2834 return mSettingsText;
2835 }
2836
Julia Reynolds13d898c2017-02-02 12:22:05 -05002837 /**
Julia Reynoldsa79c3712017-04-21 10:29:57 -04002838 * Returns which type of notifications in a group are responsible for audibly alerting the
2839 * user. See {@link #GROUP_ALERT_ALL}, {@link #GROUP_ALERT_CHILDREN},
2840 * {@link #GROUP_ALERT_SUMMARY}.
2841 */
2842 public @GroupAlertBehavior int getGroupAlertBehavior() {
2843 return mGroupAlertBehavior;
2844 }
2845
2846 /**
Dan Sandlerd63f9322015-05-06 15:18:49 -04002847 * The small icon representing this notification in the status bar and content view.
2848 *
2849 * @return the small icon representing this notification.
2850 *
2851 * @see Builder#getSmallIcon()
2852 * @see Builder#setSmallIcon(Icon)
2853 */
2854 public Icon getSmallIcon() {
2855 return mSmallIcon;
2856 }
2857
2858 /**
2859 * Used when notifying to clean up legacy small icons.
2860 * @hide
2861 */
2862 public void setSmallIcon(Icon icon) {
2863 mSmallIcon = icon;
2864 }
2865
2866 /**
2867 * The large icon shown in this notification's content view.
2868 * @see Builder#getLargeIcon()
2869 * @see Builder#setLargeIcon(Icon)
2870 */
2871 public Icon getLargeIcon() {
2872 return mLargeIcon;
2873 }
2874
2875 /**
Christoph Studer4600f9b2014-07-22 22:44:43 +02002876 * @hide
2877 */
Christoph Studerc8db24b2014-07-25 17:50:30 +02002878 public boolean isGroupSummary() {
2879 return mGroupKey != null && (flags & FLAG_GROUP_SUMMARY) != 0;
2880 }
2881
2882 /**
2883 * @hide
2884 */
2885 public boolean isGroupChild() {
2886 return mGroupKey != null && (flags & FLAG_GROUP_SUMMARY) == 0;
2887 }
2888
2889 /**
Julia Reynolds30203152017-05-26 13:36:31 -04002890 * @hide
2891 */
2892 public boolean suppressAlertingDueToGrouping() {
2893 if (isGroupSummary()
2894 && getGroupAlertBehavior() == Notification.GROUP_ALERT_CHILDREN) {
2895 return true;
2896 } else if (isGroupChild()
2897 && getGroupAlertBehavior() == Notification.GROUP_ALERT_SUMMARY) {
2898 return true;
2899 }
2900 return false;
2901 }
2902
2903 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002904 * Builder class for {@link Notification} objects.
Joe Malin8d40d042012-11-05 11:36:40 -08002905 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002906 * Provides a convenient way to set the various fields of a {@link Notification} and generate
Scott Main183bf112012-08-13 19:12:13 -07002907 * content views using the platform's notification layout template. If your app supports
2908 * versions of Android as old as API level 4, you can instead use
2909 * {@link android.support.v4.app.NotificationCompat.Builder NotificationCompat.Builder},
2910 * available in the <a href="{@docRoot}tools/extras/support-library.html">Android Support
2911 * library</a>.
Joe Malin8d40d042012-11-05 11:36:40 -08002912 *
Scott Main183bf112012-08-13 19:12:13 -07002913 * <p>Example:
Joe Malin8d40d042012-11-05 11:36:40 -08002914 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002915 * <pre class="prettyprint">
Scott Main183bf112012-08-13 19:12:13 -07002916 * Notification noti = new Notification.Builder(mContext)
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002917 * .setContentTitle(&quot;New mail from &quot; + sender.toString())
2918 * .setContentText(subject)
2919 * .setSmallIcon(R.drawable.new_mail)
2920 * .setLargeIcon(aBitmap)
Chris Wrenfbd96ba2012-05-01 12:03:58 -04002921 * .build();
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002922 * </pre>
Joe Onoratocb109a02011-01-18 17:57:41 -08002923 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002924 public static class Builder {
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05002925 /**
2926 * @hide
2927 */
2928 public static final String EXTRA_REBUILD_CONTENT_VIEW_ACTION_COUNT =
2929 "android.rebuild.contentViewActionCount";
2930 /**
2931 * @hide
2932 */
2933 public static final String EXTRA_REBUILD_BIG_CONTENT_VIEW_ACTION_COUNT
2934 = "android.rebuild.bigViewActionCount";
2935 /**
2936 * @hide
2937 */
2938 public static final String EXTRA_REBUILD_HEADS_UP_CONTENT_VIEW_ACTION_COUNT
2939 = "android.rebuild.hudViewActionCount";
2940
Daniel Sandler602ad1c2012-06-12 16:06:27 -04002941 private static final int MAX_ACTION_BUTTONS = 3;
Daniel Sandler8680bf82012-05-15 16:52:52 -04002942
Selim Cinek6743c0b2017-01-18 18:24:01 -08002943 private static final boolean USE_ONLY_TITLE_IN_LOW_PRIORITY_SUMMARY =
2944 SystemProperties.getBoolean("notifications.only_title", true);
2945
Selim Cinek389edcd2017-05-11 19:16:44 -07002946 /**
2947 * The lightness difference that has to be added to the primary text color to obtain the
2948 * secondary text color when the background is light.
2949 */
2950 private static final int LIGHTNESS_TEXT_DIFFERENCE_LIGHT = 20;
2951
2952 /**
2953 * The lightness difference that has to be added to the primary text color to obtain the
2954 * secondary text color when the background is dark.
2955 * A bit less then the above value, since it looks better on dark backgrounds.
2956 */
2957 private static final int LIGHTNESS_TEXT_DIFFERENCE_DARK = -10;
2958
Joe Onorato46439ce2010-11-19 13:56:21 -08002959 private Context mContext;
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002960 private Notification mN;
2961 private Bundle mUserExtras = new Bundle();
Chris Wrenfbd96ba2012-05-01 12:03:58 -04002962 private Style mStyle;
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002963 private ArrayList<Action> mActions = new ArrayList<Action>(MAX_ACTION_BUTTONS);
Selim Cineke7238dd2017-12-14 17:48:32 -08002964 private ArrayList<Person> mPersonList = new ArrayList<>();
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002965 private NotificationColorUtil mColorUtil;
Selim Cinek7b9605b2017-01-19 17:36:00 -08002966 private boolean mIsLegacy;
2967 private boolean mIsLegacyInitialized;
Christoph Studer7ac80e62014-08-04 16:01:57 +02002968
2969 /**
Adrian Roos4ff3b122016-02-01 12:26:13 -08002970 * Caches a contrast-enhanced version of {@link #mCachedContrastColorIsFor}.
2971 */
2972 private int mCachedContrastColor = COLOR_INVALID;
2973 private int mCachedContrastColorIsFor = COLOR_INVALID;
Adrian Roos487374f2017-01-11 15:48:14 -08002974 /**
2975 * Caches a ambient version of {@link #mCachedContrastColorIsFor}.
2976 */
2977 private int mCachedAmbientColor = COLOR_INVALID;
2978 private int mCachedAmbientColorIsFor = COLOR_INVALID;
Adrian Roos4ff3b122016-02-01 12:26:13 -08002979
2980 /**
Adrian Roos70d7aa32017-01-11 15:39:06 -08002981 * Caches an instance of StandardTemplateParams. Note that this may have been used before,
2982 * so make sure to call {@link StandardTemplateParams#reset()} before using it.
2983 */
2984 StandardTemplateParams mParams = new StandardTemplateParams();
Selim Cinek7b9605b2017-01-19 17:36:00 -08002985 private int mTextColorsAreForBackground = COLOR_INVALID;
2986 private int mPrimaryTextColor = COLOR_INVALID;
2987 private int mSecondaryTextColor = COLOR_INVALID;
2988 private int mActionBarColor = COLOR_INVALID;
Selim Cinek5fb73f82017-04-20 16:55:38 -07002989 private int mBackgroundColor = COLOR_INVALID;
2990 private int mForegroundColor = COLOR_INVALID;
Selim Cinekac5f0272017-05-02 16:05:41 -07002991 private int mBackgroundColorHint = COLOR_INVALID;
Selim Cinek5d6ef8d2017-05-18 22:16:00 -07002992 /**
2993 * A temporary location where actions are stored. If != null the view originally has action
2994 * but doesn't have any for this inflation.
2995 */
2996 private ArrayList<Action> mOriginalActions;
Selim Cineka7679b62017-05-10 16:33:25 -07002997 private boolean mRebuildStyledRemoteViews;
Adrian Roos70d7aa32017-01-11 15:39:06 -08002998
Anthony Chenad4d1582017-04-10 16:07:58 -07002999 private boolean mTintActionButtons;
3000 private boolean mInNightMode;
3001
Adrian Roos70d7aa32017-01-11 15:39:06 -08003002 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003003 * Constructs a new Builder with the defaults:
Joe Onoratocb109a02011-01-18 17:57:41 -08003004 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003005 * @param context
3006 * A {@link Context} that will be used by the Builder to construct the
3007 * RemoteViews. The Context will not be held past the lifetime of this Builder
3008 * object.
Geoffrey Pitsch5caa2762017-01-12 09:35:54 -05003009 * @param channelId
3010 * The constructed Notification will be posted on this
3011 * {@link NotificationChannel}. To use a NotificationChannel, it must first be
3012 * created using {@link NotificationManager#createNotificationChannel}.
Joe Onoratocb109a02011-01-18 17:57:41 -08003013 */
Geoffrey Pitsch5caa2762017-01-12 09:35:54 -05003014 public Builder(Context context, String channelId) {
3015 this(context, (Notification) null);
3016 mN.mChannelId = channelId;
3017 }
3018
3019 /**
3020 * @deprecated use {@link Notification.Builder#Notification.Builder(Context, String)}
3021 * instead. All posted Notifications must specify a NotificationChannel Id.
3022 */
3023 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -08003024 public Builder(Context context) {
Geoffrey Pitsch5caa2762017-01-12 09:35:54 -05003025 this(context, (Notification) null);
Joe Onorato46439ce2010-11-19 13:56:21 -08003026 }
3027
Joe Onoratocb109a02011-01-18 17:57:41 -08003028 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003029 * @hide
Christoph Studer4600f9b2014-07-22 22:44:43 +02003030 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003031 public Builder(Context context, Notification toAdopt) {
3032 mContext = context;
Anthony Chenad4d1582017-04-10 16:07:58 -07003033 Resources res = mContext.getResources();
3034 mTintActionButtons = res.getBoolean(R.bool.config_tintNotificationActionButtons);
3035
3036 if (res.getBoolean(R.bool.config_enableNightMode)) {
3037 Configuration currentConfig = res.getConfiguration();
3038 mInNightMode = (currentConfig.uiMode & Configuration.UI_MODE_NIGHT_MASK)
3039 == Configuration.UI_MODE_NIGHT_YES;
3040 }
Christoph Studer4600f9b2014-07-22 22:44:43 +02003041
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003042 if (toAdopt == null) {
3043 mN = new Notification();
Selim Cinek0ff1ce602016-04-05 18:27:16 -07003044 if (context.getApplicationInfo().targetSdkVersion < Build.VERSION_CODES.N) {
3045 mN.extras.putBoolean(EXTRA_SHOW_WHEN, true);
3046 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003047 mN.priority = PRIORITY_DEFAULT;
3048 mN.visibility = VISIBILITY_PRIVATE;
3049 } else {
3050 mN = toAdopt;
3051 if (mN.actions != null) {
3052 Collections.addAll(mActions, mN.actions);
Christoph Studer4600f9b2014-07-22 22:44:43 +02003053 }
3054
Selim Cineke7238dd2017-12-14 17:48:32 -08003055 if (mN.extras.containsKey(EXTRA_PEOPLE_LIST)) {
3056 ArrayList<Person> people = mN.extras.getParcelableArrayList(EXTRA_PEOPLE_LIST);
3057 mPersonList.addAll(people);
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003058 }
3059
Selim Cinek4ac6f602016-06-13 15:47:03 -07003060 if (mN.getSmallIcon() == null && mN.icon != 0) {
3061 setSmallIcon(mN.icon);
3062 }
3063
3064 if (mN.getLargeIcon() == null && mN.largeIcon != null) {
3065 setLargeIcon(mN.largeIcon);
3066 }
3067
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003068 String templateClass = mN.extras.getString(EXTRA_TEMPLATE);
3069 if (!TextUtils.isEmpty(templateClass)) {
3070 final Class<? extends Style> styleClass
3071 = getNotificationStyleClass(templateClass);
3072 if (styleClass == null) {
3073 Log.d(TAG, "Unknown style class: " + templateClass);
3074 } else {
3075 try {
Adrian Roosc1a80b02016-04-05 14:54:55 -07003076 final Constructor<? extends Style> ctor =
3077 styleClass.getDeclaredConstructor();
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003078 ctor.setAccessible(true);
3079 final Style style = ctor.newInstance();
3080 style.restoreFromExtras(mN.extras);
3081
3082 if (style != null) {
3083 setStyle(style);
3084 }
3085 } catch (Throwable t) {
3086 Log.e(TAG, "Could not create Style", t);
3087 }
3088 }
3089 }
3090
3091 }
3092 }
3093
3094 private NotificationColorUtil getColorUtil() {
Selim Cinek99104832017-01-25 14:47:33 -08003095 if (mColorUtil == null) {
3096 mColorUtil = NotificationColorUtil.getInstance(mContext);
Christoph Studer4600f9b2014-07-22 22:44:43 +02003097 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003098 return mColorUtil;
Christoph Studer4600f9b2014-07-22 22:44:43 +02003099 }
3100
3101 /**
Julia Reynolds13d898c2017-02-02 12:22:05 -05003102 * If this notification is duplicative of a Launcher shortcut, sets the
3103 * {@link ShortcutInfo#getId() id} of the shortcut, in case the Launcher wants to hide
3104 * the shortcut.
3105 *
Julia Reynoldsbad42972017-04-25 13:52:49 -04003106 * This field will be ignored by Launchers that don't support badging, don't show
3107 * notification content, or don't show {@link android.content.pm.ShortcutManager shortcuts}.
Julia Reynolds13d898c2017-02-02 12:22:05 -05003108 *
3109 * @param shortcutId the {@link ShortcutInfo#getId() id} of the shortcut this notification
3110 * supersedes
3111 */
3112 public Builder setShortcutId(String shortcutId) {
3113 mN.mShortcutId = shortcutId;
3114 return this;
3115 }
3116
3117 /**
Julia Reynoldse071abd2017-03-22 10:52:11 -04003118 * Sets which icon to display as a badge for this notification.
3119 *
3120 * Must be one of {@link #BADGE_ICON_NONE}, {@link #BADGE_ICON_SMALL},
3121 * {@link #BADGE_ICON_LARGE}.
3122 *
3123 * Note: This value might be ignored, for launchers that don't support badge icons.
3124 */
Julia Reynolds612beb22017-03-30 10:48:30 -04003125 public Builder setBadgeIconType(int icon) {
Julia Reynoldse071abd2017-03-22 10:52:11 -04003126 mN.mBadgeIcon = icon;
3127 return this;
3128 }
3129
3130 /**
Julia Reynoldsa79c3712017-04-21 10:29:57 -04003131 * Sets the group alert behavior for this notification. Use this method to mute this
3132 * notification if alerts for this notification's group should be handled by a different
3133 * notification. This is only applicable for notifications that belong to a
Julia Reynolds399d9bf2017-08-11 12:52:14 -04003134 * {@link #setGroup(String) group}. This must be called on all notifications you want to
3135 * mute. For example, if you want only the summary of your group to make noise, all
3136 * children in the group should have the group alert behavior {@link #GROUP_ALERT_SUMMARY}.
Julia Reynoldsa79c3712017-04-21 10:29:57 -04003137 *
3138 * <p> The default value is {@link #GROUP_ALERT_ALL}.</p>
3139 */
3140 public Builder setGroupAlertBehavior(@GroupAlertBehavior int groupAlertBehavior) {
3141 mN.mGroupAlertBehavior = groupAlertBehavior;
3142 return this;
3143 }
3144
Jeff Sharkey000ce802017-04-29 13:13:27 -06003145 /** @removed */
3146 @Deprecated
Julia Reynoldsb5e44b72016-08-16 15:00:25 -04003147 public Builder setChannel(String channelId) {
3148 mN.mChannelId = channelId;
3149 return this;
3150 }
3151
3152 /**
Julia Reynoldsbad42972017-04-25 13:52:49 -04003153 * Specifies the channel the notification should be delivered on.
3154 */
3155 public Builder setChannelId(String channelId) {
3156 mN.mChannelId = channelId;
3157 return this;
3158 }
3159
Jeff Sharkey000ce802017-04-29 13:13:27 -06003160 /** @removed */
3161 @Deprecated
Julia Reynolds50989772017-02-23 14:32:16 -05003162 public Builder setTimeout(long durationMs) {
3163 mN.mTimeout = durationMs;
Julia Reynolds2a128742016-11-28 14:29:25 -05003164 return this;
3165 }
3166
3167 /**
Julia Reynoldsbad42972017-04-25 13:52:49 -04003168 * Specifies a duration in milliseconds after which this notification should be canceled,
3169 * if it is not already canceled.
3170 */
3171 public Builder setTimeoutAfter(long durationMs) {
3172 mN.mTimeout = durationMs;
3173 return this;
3174 }
3175
3176 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003177 * Add a timestamp pertaining to the notification (usually the time the event occurred).
Selim Cinek0ff1ce602016-04-05 18:27:16 -07003178 *
3179 * For apps targeting {@link android.os.Build.VERSION_CODES#N} and above, this time is not
3180 * shown anymore by default and must be opted into by using
3181 * {@link android.app.Notification.Builder#setShowWhen(boolean)}
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003182 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003183 * @see Notification#when
Joe Onoratocb109a02011-01-18 17:57:41 -08003184 */
Joe Onorato46439ce2010-11-19 13:56:21 -08003185 public Builder setWhen(long when) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003186 mN.when = when;
Joe Onorato46439ce2010-11-19 13:56:21 -08003187 return this;
3188 }
3189
Joe Onoratocb109a02011-01-18 17:57:41 -08003190 /**
Griff Hazen50c11652014-05-16 09:46:31 -07003191 * Control whether the timestamp set with {@link #setWhen(long) setWhen} is shown
Daniel Sandler0c890492012-09-12 17:23:10 -07003192 * in the content view.
Selim Cinek0ff1ce602016-04-05 18:27:16 -07003193 * For apps targeting {@link android.os.Build.VERSION_CODES#N} and above, this defaults to
3194 * {@code false}. For earlier apps, the default is {@code true}.
Daniel Sandler0c890492012-09-12 17:23:10 -07003195 */
3196 public Builder setShowWhen(boolean show) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003197 mN.extras.putBoolean(EXTRA_SHOW_WHEN, show);
Daniel Sandler0c890492012-09-12 17:23:10 -07003198 return this;
3199 }
3200
3201 /**
Daniel Sandlerd33b8032012-05-10 11:41:48 -04003202 * Show the {@link Notification#when} field as a stopwatch.
Joe Malin8d40d042012-11-05 11:36:40 -08003203 *
3204 * Instead of presenting <code>when</code> as a timestamp, the notification will show an
Daniel Sandlerd33b8032012-05-10 11:41:48 -04003205 * automatically updating display of the minutes and seconds since <code>when</code>.
Daniel Sandlera2985ed2012-04-03 16:42:00 -04003206 *
Daniel Sandlerd33b8032012-05-10 11:41:48 -04003207 * Useful when showing an elapsed time (like an ongoing phone call).
3208 *
Selim Cinek81c23aa2016-02-25 16:23:13 -08003209 * The counter can also be set to count down to <code>when</code> when using
Adrian Roos96b7e202016-05-17 13:50:38 -07003210 * {@link #setChronometerCountDown(boolean)}.
Selim Cinek81c23aa2016-02-25 16:23:13 -08003211 *
Daniel Sandlerd33b8032012-05-10 11:41:48 -04003212 * @see android.widget.Chronometer
Daniel Sandlera2985ed2012-04-03 16:42:00 -04003213 * @see Notification#when
Adrian Roos96b7e202016-05-17 13:50:38 -07003214 * @see #setChronometerCountDown(boolean)
Daniel Sandlera2985ed2012-04-03 16:42:00 -04003215 */
3216 public Builder setUsesChronometer(boolean b) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003217 mN.extras.putBoolean(EXTRA_SHOW_CHRONOMETER, b);
Daniel Sandlera2985ed2012-04-03 16:42:00 -04003218 return this;
3219 }
3220
3221 /**
Selim Cinek81c23aa2016-02-25 16:23:13 -08003222 * Sets the Chronometer to count down instead of counting up.
3223 *
3224 * <p>This is only relevant if {@link #setUsesChronometer(boolean)} has been set to true.
3225 * If it isn't set the chronometer will count up.
3226 *
3227 * @see #setUsesChronometer(boolean)
3228 */
Adrian Roos96b7e202016-05-17 13:50:38 -07003229 public Builder setChronometerCountDown(boolean countDown) {
3230 mN.extras.putBoolean(EXTRA_CHRONOMETER_COUNT_DOWN, countDown);
Selim Cinek81c23aa2016-02-25 16:23:13 -08003231 return this;
3232 }
3233
3234 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003235 * Set the small icon resource, which will be used to represent the notification in the
3236 * status bar.
Joe Onoratocb109a02011-01-18 17:57:41 -08003237 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003238
3239 * The platform template for the expanded view will draw this icon in the left, unless a
3240 * {@link #setLargeIcon(Bitmap) large icon} has also been specified, in which case the small
3241 * icon will be moved to the right-hand side.
3242 *
3243
3244 * @param icon
3245 * A resource ID in the application's package of the drawable to use.
3246 * @see Notification#icon
Joe Onoratocb109a02011-01-18 17:57:41 -08003247 */
Tor Norbye7b9c9122013-05-30 16:48:33 -07003248 public Builder setSmallIcon(@DrawableRes int icon) {
Dan Sandlerd63f9322015-05-06 15:18:49 -04003249 return setSmallIcon(icon != 0
3250 ? Icon.createWithResource(mContext, icon)
3251 : null);
Joe Onorato46439ce2010-11-19 13:56:21 -08003252 }
3253
Joe Onoratocb109a02011-01-18 17:57:41 -08003254 /**
3255 * A variant of {@link #setSmallIcon(int) setSmallIcon(int)} that takes an additional
3256 * level parameter for when the icon is a {@link android.graphics.drawable.LevelListDrawable
3257 * LevelListDrawable}.
3258 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003259 * @param icon A resource ID in the application's package of the drawable to use.
Joe Onoratocb109a02011-01-18 17:57:41 -08003260 * @param level The level to use for the icon.
3261 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003262 * @see Notification#icon
3263 * @see Notification#iconLevel
Joe Onoratocb109a02011-01-18 17:57:41 -08003264 */
Tor Norbye7b9c9122013-05-30 16:48:33 -07003265 public Builder setSmallIcon(@DrawableRes int icon, int level) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003266 mN.iconLevel = level;
Dan Sandlerd63f9322015-05-06 15:18:49 -04003267 return setSmallIcon(icon);
3268 }
3269
3270 /**
3271 * Set the small icon, which will be used to represent the notification in the
3272 * status bar and content view (unless overriden there by a
3273 * {@link #setLargeIcon(Bitmap) large icon}).
3274 *
3275 * @param icon An Icon object to use.
3276 * @see Notification#icon
3277 */
3278 public Builder setSmallIcon(Icon icon) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003279 mN.setSmallIcon(icon);
3280 if (icon != null && icon.getType() == Icon.TYPE_RESOURCE) {
3281 mN.icon = icon.getResId();
3282 }
Joe Onorato46439ce2010-11-19 13:56:21 -08003283 return this;
3284 }
3285
Joe Onoratocb109a02011-01-18 17:57:41 -08003286 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003287 * Set the first line of text in the platform notification template.
Joe Onoratocb109a02011-01-18 17:57:41 -08003288 */
Joe Onorato46439ce2010-11-19 13:56:21 -08003289 public Builder setContentTitle(CharSequence title) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003290 mN.extras.putCharSequence(EXTRA_TITLE, safeCharSequence(title));
Joe Onorato46439ce2010-11-19 13:56:21 -08003291 return this;
3292 }
3293
Joe Onoratocb109a02011-01-18 17:57:41 -08003294 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003295 * Set the second line of text in the platform notification template.
Joe Onoratocb109a02011-01-18 17:57:41 -08003296 */
Joe Onorato46439ce2010-11-19 13:56:21 -08003297 public Builder setContentText(CharSequence text) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003298 mN.extras.putCharSequence(EXTRA_TEXT, safeCharSequence(text));
Joe Onorato46439ce2010-11-19 13:56:21 -08003299 return this;
3300 }
3301
Joe Onoratocb109a02011-01-18 17:57:41 -08003302 /**
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07003303 * This provides some additional information that is displayed in the notification. No
3304 * guarantees are given where exactly it is displayed.
3305 *
3306 * <p>This information should only be provided if it provides an essential
3307 * benefit to the understanding of the notification. The more text you provide the
3308 * less readable it becomes. For example, an email client should only provide the account
3309 * name here if more than one email account has been added.</p>
3310 *
3311 * <p>As of {@link android.os.Build.VERSION_CODES#N} this information is displayed in the
3312 * notification header area.
3313 *
3314 * On Android versions before {@link android.os.Build.VERSION_CODES#N}
3315 * this will be shown in the third line of text in the platform notification template.
3316 * You should not be using {@link #setProgress(int, int, boolean)} at the
3317 * same time on those versions; they occupy the same place.
3318 * </p>
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003319 */
3320 public Builder setSubText(CharSequence text) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003321 mN.extras.putCharSequence(EXTRA_SUB_TEXT, safeCharSequence(text));
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003322 return this;
3323 }
3324
3325 /**
Julia Reynolds3aedded2017-03-31 14:42:09 -04003326 * Provides text that will appear as a link to your application's settings.
3327 *
3328 * <p>This text does not appear within notification {@link Style templates} but may
3329 * appear when the user uses an affordance to learn more about the notification.
3330 * Additionally, this text will not appear unless you provide a valid link target by
3331 * handling {@link #INTENT_CATEGORY_NOTIFICATION_PREFERENCES}.
3332 *
3333 * <p>This text is meant to be concise description about what the user can customize
3334 * when they click on this link. The recommended maximum length is 40 characters.
3335 * @param text
3336 * @return
3337 */
3338 public Builder setSettingsText(CharSequence text) {
3339 mN.mSettingsText = safeCharSequence(text);
3340 return this;
3341 }
3342
3343 /**
Adrian Roose458aa82015-12-08 16:17:19 -08003344 * Set the remote input history.
3345 *
3346 * This should be set to the most recent inputs that have been sent
3347 * through a {@link RemoteInput} of this Notification and cleared once the it is no
3348 * longer relevant (e.g. for chat notifications once the other party has responded).
3349 *
3350 * The most recent input must be stored at the 0 index, the second most recent at the
3351 * 1 index, etc. Note that the system will limit both how far back the inputs will be shown
3352 * and how much of each individual input is shown.
3353 *
3354 * <p>Note: The reply text will only be shown on notifications that have least one action
3355 * with a {@code RemoteInput}.</p>
3356 */
3357 public Builder setRemoteInputHistory(CharSequence[] text) {
3358 if (text == null) {
3359 mN.extras.putCharSequenceArray(EXTRA_REMOTE_INPUT_HISTORY, null);
3360 } else {
3361 final int N = Math.min(MAX_REPLY_HISTORY, text.length);
3362 CharSequence[] safe = new CharSequence[N];
3363 for (int i = 0; i < N; i++) {
3364 safe[i] = safeCharSequence(text[i]);
3365 }
3366 mN.extras.putCharSequenceArray(EXTRA_REMOTE_INPUT_HISTORY, safe);
3367 }
3368 return this;
3369 }
3370
3371 /**
Julia Reynolds13d898c2017-02-02 12:22:05 -05003372 * Sets the number of items this notification represents. May be displayed as a badge count
3373 * for Launchers that support badging.
Joe Onoratocb109a02011-01-18 17:57:41 -08003374 */
Joe Onorato8595a3d2010-11-19 18:12:07 -08003375 public Builder setNumber(int number) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003376 mN.number = number;
Joe Onorato8595a3d2010-11-19 18:12:07 -08003377 return this;
3378 }
3379
Joe Onoratocb109a02011-01-18 17:57:41 -08003380 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003381 * A small piece of additional information pertaining to this notification.
3382 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003383 * The platform template will draw this on the last line of the notification, at the far
3384 * right (to the right of a smallIcon if it has been placed there).
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07003385 *
3386 * @deprecated use {@link #setSubText(CharSequence)} instead to set a text in the header.
3387 * For legacy apps targeting a version below {@link android.os.Build.VERSION_CODES#N} this
3388 * field will still show up, but the subtext will take precedence.
Joe Onoratocb109a02011-01-18 17:57:41 -08003389 */
Aurimas Liutikas514c5ef2016-05-24 15:22:55 -07003390 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -08003391 public Builder setContentInfo(CharSequence info) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003392 mN.extras.putCharSequence(EXTRA_INFO_TEXT, safeCharSequence(info));
Joe Onorato46439ce2010-11-19 13:56:21 -08003393 return this;
3394 }
3395
Joe Onoratocb109a02011-01-18 17:57:41 -08003396 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003397 * Set the progress this notification represents.
3398 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003399 * The platform template will represent this using a {@link ProgressBar}.
Jeff Sharkey1c400132011-08-05 14:50:13 -07003400 */
3401 public Builder setProgress(int max, int progress, boolean indeterminate) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003402 mN.extras.putInt(EXTRA_PROGRESS, progress);
3403 mN.extras.putInt(EXTRA_PROGRESS_MAX, max);
3404 mN.extras.putBoolean(EXTRA_PROGRESS_INDETERMINATE, indeterminate);
Jeff Sharkey1c400132011-08-05 14:50:13 -07003405 return this;
3406 }
3407
3408 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003409 * Supply a custom RemoteViews to use instead of the platform template.
3410 *
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003411 * Use {@link #setCustomContentView(RemoteViews)} instead.
Joe Onoratocb109a02011-01-18 17:57:41 -08003412 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003413 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -08003414 public Builder setContent(RemoteViews views) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003415 return setCustomContentView(views);
3416 }
3417
3418 /**
3419 * Supply custom RemoteViews to use instead of the platform template.
3420 *
3421 * This will override the layout that would otherwise be constructed by this Builder
3422 * object.
3423 */
3424 public Builder setCustomContentView(RemoteViews contentView) {
3425 mN.contentView = contentView;
3426 return this;
3427 }
3428
3429 /**
3430 * Supply custom RemoteViews to use instead of the platform template in the expanded form.
3431 *
3432 * This will override the expanded layout that would otherwise be constructed by this
3433 * Builder object.
3434 */
3435 public Builder setCustomBigContentView(RemoteViews contentView) {
3436 mN.bigContentView = contentView;
3437 return this;
3438 }
3439
3440 /**
3441 * Supply custom RemoteViews to use instead of the platform template in the heads up dialog.
3442 *
3443 * This will override the heads-up layout that would otherwise be constructed by this
3444 * Builder object.
3445 */
3446 public Builder setCustomHeadsUpContentView(RemoteViews contentView) {
3447 mN.headsUpContentView = contentView;
Joe Onorato46439ce2010-11-19 13:56:21 -08003448 return this;
3449 }
3450
Joe Onoratocb109a02011-01-18 17:57:41 -08003451 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003452 * Supply a {@link PendingIntent} to be sent when the notification is clicked.
3453 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003454 * As of {@link android.os.Build.VERSION_CODES#HONEYCOMB}, if this field is unset and you
3455 * have specified a custom RemoteViews with {@link #setContent(RemoteViews)}, you can use
3456 * {@link RemoteViews#setOnClickPendingIntent RemoteViews.setOnClickPendingIntent(int,PendingIntent)}
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003457 * to assign PendingIntents to individual views in that custom layout (i.e., to create
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003458 * clickable buttons inside the notification view).
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003459 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003460 * @see Notification#contentIntent Notification.contentIntent
Joe Onoratocb109a02011-01-18 17:57:41 -08003461 */
Joe Onorato46439ce2010-11-19 13:56:21 -08003462 public Builder setContentIntent(PendingIntent intent) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003463 mN.contentIntent = intent;
Joe Onorato46439ce2010-11-19 13:56:21 -08003464 return this;
3465 }
3466
Joe Onoratocb109a02011-01-18 17:57:41 -08003467 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003468 * Supply a {@link PendingIntent} to send when the notification is cleared explicitly by the user.
3469 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003470 * @see Notification#deleteIntent
Joe Onoratocb109a02011-01-18 17:57:41 -08003471 */
Joe Onorato46439ce2010-11-19 13:56:21 -08003472 public Builder setDeleteIntent(PendingIntent intent) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003473 mN.deleteIntent = intent;
Joe Onorato46439ce2010-11-19 13:56:21 -08003474 return this;
3475 }
3476
Joe Onoratocb109a02011-01-18 17:57:41 -08003477 /**
3478 * An intent to launch instead of posting the notification to the status bar.
3479 * Only for use with extremely high-priority notifications demanding the user's
3480 * <strong>immediate</strong> attention, such as an incoming phone call or
3481 * alarm clock that the user has explicitly set to a particular time.
3482 * If this facility is used for something else, please give the user an option
3483 * to turn it off and use a normal notification, as this can be extremely
3484 * disruptive.
3485 *
Chris Wren47c20a12014-06-18 17:27:29 -04003486 * <p>
3487 * The system UI may choose to display a heads-up notification, instead of
3488 * launching this intent, while the user is using the device.
3489 * </p>
3490 *
Joe Onoratocb109a02011-01-18 17:57:41 -08003491 * @param intent The pending intent to launch.
3492 * @param highPriority Passing true will cause this notification to be sent
3493 * even if other notifications are suppressed.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003494 *
3495 * @see Notification#fullScreenIntent
Joe Onoratocb109a02011-01-18 17:57:41 -08003496 */
Joe Onorato46439ce2010-11-19 13:56:21 -08003497 public Builder setFullScreenIntent(PendingIntent intent, boolean highPriority) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003498 mN.fullScreenIntent = intent;
Joe Onorato46439ce2010-11-19 13:56:21 -08003499 setFlag(FLAG_HIGH_PRIORITY, highPriority);
3500 return this;
3501 }
3502
Joe Onoratocb109a02011-01-18 17:57:41 -08003503 /**
Dan Sandler5fcdf6e2014-07-18 11:31:15 -04003504 * Set the "ticker" text which is sent to accessibility services.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003505 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003506 * @see Notification#tickerText
Joe Onoratocb109a02011-01-18 17:57:41 -08003507 */
Joe Onorato46439ce2010-11-19 13:56:21 -08003508 public Builder setTicker(CharSequence tickerText) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003509 mN.tickerText = safeCharSequence(tickerText);
Joe Onorato46439ce2010-11-19 13:56:21 -08003510 return this;
3511 }
3512
Joe Onoratocb109a02011-01-18 17:57:41 -08003513 /**
Dan Sandler5fcdf6e2014-07-18 11:31:15 -04003514 * Obsolete version of {@link #setTicker(CharSequence)}.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003515 *
Joe Onoratocb109a02011-01-18 17:57:41 -08003516 */
Dan Sandler5fcdf6e2014-07-18 11:31:15 -04003517 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -08003518 public Builder setTicker(CharSequence tickerText, RemoteViews views) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003519 setTicker(tickerText);
3520 // views is ignored
Joe Onorato46439ce2010-11-19 13:56:21 -08003521 return this;
3522 }
3523
Joe Onoratocb109a02011-01-18 17:57:41 -08003524 /**
Dan Sandlerd63f9322015-05-06 15:18:49 -04003525 * Add a large icon to the notification content view.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003526 *
3527 * In the platform template, this image will be shown on the left of the notification view
Dan Sandlerd63f9322015-05-06 15:18:49 -04003528 * in place of the {@link #setSmallIcon(Icon) small icon} (which will be placed in a small
3529 * badge atop the large icon).
Dan Sandler08a04c12015-05-06 15:18:49 -04003530 */
Dan Sandlerd63f9322015-05-06 15:18:49 -04003531 public Builder setLargeIcon(Bitmap b) {
3532 return setLargeIcon(b != null ? Icon.createWithBitmap(b) : null);
3533 }
3534
3535 /**
3536 * Add a large icon to the notification content view.
3537 *
3538 * In the platform template, this image will be shown on the left of the notification view
3539 * in place of the {@link #setSmallIcon(Icon) small icon} (which will be placed in a small
3540 * badge atop the large icon).
3541 */
3542 public Builder setLargeIcon(Icon icon) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003543 mN.mLargeIcon = icon;
3544 mN.extras.putParcelable(EXTRA_LARGE_ICON, icon);
Joe Onorato46439ce2010-11-19 13:56:21 -08003545 return this;
3546 }
3547
Joe Onoratocb109a02011-01-18 17:57:41 -08003548 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003549 * Set the sound to play.
3550 *
John Spurlockc0650f022014-07-19 13:22:39 -04003551 * It will be played using the {@link #AUDIO_ATTRIBUTES_DEFAULT default audio attributes}
3552 * for notifications.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003553 *
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05003554 * @deprecated use {@link NotificationChannel#setSound(Uri, AudioAttributes)} instead.
Joe Onoratocb109a02011-01-18 17:57:41 -08003555 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05003556 @Deprecated
Joe Onorato52f80cd2010-11-21 15:34:48 -08003557 public Builder setSound(Uri sound) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003558 mN.sound = sound;
3559 mN.audioAttributes = AUDIO_ATTRIBUTES_DEFAULT;
Joe Onorato52f80cd2010-11-21 15:34:48 -08003560 return this;
3561 }
3562
Joe Onoratocb109a02011-01-18 17:57:41 -08003563 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003564 * Set the sound to play, along with a specific stream on which to play it.
Joe Onoratocb109a02011-01-18 17:57:41 -08003565 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003566 * See {@link android.media.AudioManager} for the <code>STREAM_</code> constants.
3567 *
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05003568 * @deprecated use {@link NotificationChannel#setSound(Uri, AudioAttributes)}.
Joe Onoratocb109a02011-01-18 17:57:41 -08003569 */
Jean-Michel Trivi81f871e2014-08-06 16:32:38 -07003570 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -08003571 public Builder setSound(Uri sound, int streamType) {
Jean-Michel Trivi2f7511f2016-11-28 15:40:27 -08003572 PlayerBase.deprecateStreamTypeForPlayback(streamType, "Notification", "setSound()");
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003573 mN.sound = sound;
3574 mN.audioStreamType = streamType;
Joe Onorato46439ce2010-11-19 13:56:21 -08003575 return this;
3576 }
3577
Joe Onoratocb109a02011-01-18 17:57:41 -08003578 /**
John Spurlockc0650f022014-07-19 13:22:39 -04003579 * Set the sound to play, along with specific {@link AudioAttributes audio attributes} to
3580 * use during playback.
3581 *
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05003582 * @deprecated use {@link NotificationChannel#setSound(Uri, AudioAttributes)} instead.
John Spurlockc0650f022014-07-19 13:22:39 -04003583 * @see Notification#sound
3584 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05003585 @Deprecated
John Spurlockc0650f022014-07-19 13:22:39 -04003586 public Builder setSound(Uri sound, AudioAttributes audioAttributes) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003587 mN.sound = sound;
3588 mN.audioAttributes = audioAttributes;
John Spurlockc0650f022014-07-19 13:22:39 -04003589 return this;
3590 }
3591
3592 /**
Joe Onoratocb109a02011-01-18 17:57:41 -08003593 * Set the vibration pattern to use.
3594 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003595 * See {@link android.os.Vibrator#vibrate(long[], int)} for a discussion of the
3596 * <code>pattern</code> parameter.
3597 *
Chris Wren47c20a12014-06-18 17:27:29 -04003598 * <p>
3599 * A notification that vibrates is more likely to be presented as a heads-up notification.
3600 * </p>
3601 *
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05003602 * @deprecated use {@link NotificationChannel#setVibrationPattern(long[])} instead.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003603 * @see Notification#vibrate
Joe Onoratocb109a02011-01-18 17:57:41 -08003604 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05003605 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -08003606 public Builder setVibrate(long[] pattern) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003607 mN.vibrate = pattern;
Joe Onorato46439ce2010-11-19 13:56:21 -08003608 return this;
3609 }
3610
Joe Onoratocb109a02011-01-18 17:57:41 -08003611 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003612 * Set the desired color for the indicator LED on the device, as well as the
3613 * blink duty cycle (specified in milliseconds).
3614 *
3615
3616 * Not all devices will honor all (or even any) of these values.
3617 *
Julia Reynolds529e3322017-02-06 08:33:01 -05003618 * @deprecated use {@link NotificationChannel#enableLights(boolean)} instead.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003619 * @see Notification#ledARGB
3620 * @see Notification#ledOnMS
3621 * @see Notification#ledOffMS
Joe Onoratocb109a02011-01-18 17:57:41 -08003622 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05003623 @Deprecated
Tor Norbye80756e32015-03-02 09:39:27 -08003624 public Builder setLights(@ColorInt int argb, int onMs, int offMs) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003625 mN.ledARGB = argb;
3626 mN.ledOnMS = onMs;
3627 mN.ledOffMS = offMs;
Julia Reynolds10ee1fc2015-11-09 11:04:55 -05003628 if (onMs != 0 || offMs != 0) {
3629 mN.flags |= FLAG_SHOW_LIGHTS;
3630 }
Joe Onorato46439ce2010-11-19 13:56:21 -08003631 return this;
3632 }
3633
Joe Onoratocb109a02011-01-18 17:57:41 -08003634 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003635 * Set whether this is an "ongoing" notification.
Joe Onoratocb109a02011-01-18 17:57:41 -08003636 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003637
3638 * Ongoing notifications cannot be dismissed by the user, so your application or service
3639 * must take care of canceling them.
3640 *
3641
3642 * They are typically used to indicate a background task that the user is actively engaged
3643 * with (e.g., playing music) or is pending in some way and therefore occupying the device
3644 * (e.g., a file download, sync operation, active network connection).
3645 *
3646
3647 * @see Notification#FLAG_ONGOING_EVENT
3648 * @see Service#setForeground(boolean)
Joe Onoratocb109a02011-01-18 17:57:41 -08003649 */
Joe Onorato46439ce2010-11-19 13:56:21 -08003650 public Builder setOngoing(boolean ongoing) {
3651 setFlag(FLAG_ONGOING_EVENT, ongoing);
3652 return this;
3653 }
3654
Joe Onoratocb109a02011-01-18 17:57:41 -08003655 /**
Selim Cinek7b9605b2017-01-19 17:36:00 -08003656 * Set whether this notification should be colorized. When set, the color set with
3657 * {@link #setColor(int)} will be used as the background color of this notification.
3658 * <p>
Selim Cinek7b9605b2017-01-19 17:36:00 -08003659 * This should only be used for high priority ongoing tasks like navigation, an ongoing
3660 * call, or other similarly high-priority events for the user.
Selim Cinek99104832017-01-25 14:47:33 -08003661 * <p>
Selim Cinek22714f12017-04-13 16:23:53 -07003662 * For most styles, the coloring will only be applied if the notification is for a
3663 * foreground service notification.
Selim Cinek99104832017-01-25 14:47:33 -08003664 * However, for {@link MediaStyle} and {@link DecoratedMediaCustomViewStyle} notifications
Selim Cinek22714f12017-04-13 16:23:53 -07003665 * that have a media session attached there is no such requirement.
Selim Cinek7b9605b2017-01-19 17:36:00 -08003666 *
Selim Cinek7b9605b2017-01-19 17:36:00 -08003667 * @see Builder#setColor(int)
Selim Cinek99104832017-01-25 14:47:33 -08003668 * @see MediaStyle#setMediaSession(MediaSession.Token)
Selim Cinek7b9605b2017-01-19 17:36:00 -08003669 */
3670 public Builder setColorized(boolean colorize) {
3671 mN.extras.putBoolean(EXTRA_COLORIZED, colorize);
3672 return this;
3673 }
3674
3675 /**
Joe Onoratocb109a02011-01-18 17:57:41 -08003676 * Set this flag if you would only like the sound, vibrate
3677 * and ticker to be played if the notification is not already showing.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003678 *
3679 * @see Notification#FLAG_ONLY_ALERT_ONCE
Joe Onoratocb109a02011-01-18 17:57:41 -08003680 */
Joe Onorato46439ce2010-11-19 13:56:21 -08003681 public Builder setOnlyAlertOnce(boolean onlyAlertOnce) {
3682 setFlag(FLAG_ONLY_ALERT_ONCE, onlyAlertOnce);
3683 return this;
3684 }
3685
Joe Onoratocb109a02011-01-18 17:57:41 -08003686 /**
Julia Reynolds04499532016-09-13 14:04:53 -04003687 * Make this notification automatically dismissed when the user touches it.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003688 *
3689 * @see Notification#FLAG_AUTO_CANCEL
Joe Onoratocb109a02011-01-18 17:57:41 -08003690 */
Joe Onorato46439ce2010-11-19 13:56:21 -08003691 public Builder setAutoCancel(boolean autoCancel) {
Joe Onorato281d83f2011-01-04 17:13:10 -08003692 setFlag(FLAG_AUTO_CANCEL, autoCancel);
Joe Onorato46439ce2010-11-19 13:56:21 -08003693 return this;
3694 }
3695
Joe Onoratocb109a02011-01-18 17:57:41 -08003696 /**
Griff Hazendfcb0802014-02-11 12:00:00 -08003697 * Set whether or not this notification should not bridge to other devices.
3698 *
3699 * <p>Some notifications can be bridged to other devices for remote display.
3700 * This hint can be set to recommend this notification not be bridged.
3701 */
3702 public Builder setLocalOnly(boolean localOnly) {
3703 setFlag(FLAG_LOCAL_ONLY, localOnly);
3704 return this;
3705 }
3706
3707 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003708 * Set which notification properties will be inherited from system defaults.
Joe Onoratocb109a02011-01-18 17:57:41 -08003709 * <p>
3710 * The value should be one or more of the following fields combined with
3711 * bitwise-or:
3712 * {@link #DEFAULT_SOUND}, {@link #DEFAULT_VIBRATE}, {@link #DEFAULT_LIGHTS}.
3713 * <p>
3714 * For all default values, use {@link #DEFAULT_ALL}.
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05003715 *
3716 * @deprecated use {@link NotificationChannel#enableVibration(boolean)} and
Julia Reynolds529e3322017-02-06 08:33:01 -05003717 * {@link NotificationChannel#enableLights(boolean)} and
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05003718 * {@link NotificationChannel#setSound(Uri, AudioAttributes)} instead.
Joe Onoratocb109a02011-01-18 17:57:41 -08003719 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05003720 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -08003721 public Builder setDefaults(int defaults) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003722 mN.defaults = defaults;
Joe Onorato46439ce2010-11-19 13:56:21 -08003723 return this;
3724 }
3725
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003726 /**
3727 * Set the priority of this notification.
3728 *
3729 * @see Notification#priority
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05003730 * @deprecated use {@link NotificationChannel#setImportance(int)} instead.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003731 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05003732 @Deprecated
Tor Norbyed9273d62013-05-30 15:59:53 -07003733 public Builder setPriority(@Priority int pri) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003734 mN.priority = pri;
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003735 return this;
3736 }
Joe Malin8d40d042012-11-05 11:36:40 -08003737
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003738 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -04003739 * Set the notification category.
Joe Malin8d40d042012-11-05 11:36:40 -08003740 *
John Spurlockfd7f1e02014-03-18 16:41:57 -04003741 * @see Notification#category
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003742 */
John Spurlockfd7f1e02014-03-18 16:41:57 -04003743 public Builder setCategory(String category) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003744 mN.category = category;
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003745 return this;
3746 }
3747
3748 /**
Chris Wrendde75302014-03-26 17:24:15 -04003749 * Add a person that is relevant to this notification.
3750 *
Chris Wrene6c48932014-09-29 17:19:27 -04003751 * <P>
3752 * Depending on user preferences, this annotation may allow the notification to pass
Julia Reynoldse071abd2017-03-22 10:52:11 -04003753 * through interruption filters, if this notification is of category {@link #CATEGORY_CALL}
3754 * or {@link #CATEGORY_MESSAGE}. The addition of people may also cause this notification to
3755 * appear more prominently in the user interface.
Chris Wrene6c48932014-09-29 17:19:27 -04003756 * </P>
3757 *
3758 * <P>
3759 * The person should be specified by the {@code String} representation of a
3760 * {@link android.provider.ContactsContract.Contacts#CONTENT_LOOKUP_URI}.
3761 * </P>
3762 *
3763 * <P>The system will also attempt to resolve {@code mailto:} and {@code tel:} schema
3764 * URIs. The path part of these URIs must exist in the contacts database, in the
3765 * appropriate column, or the reference will be discarded as invalid. Telephone schema
3766 * URIs will be resolved by {@link android.provider.ContactsContract.PhoneLookup}.
Selim Cineke7238dd2017-12-14 17:48:32 -08003767 * It is also possible to provide a URI with the schema {@code name:} in order to uniquely
3768 * identify a person without an entry in the contacts database.
Chris Wrene6c48932014-09-29 17:19:27 -04003769 * </P>
3770 *
3771 * @param uri A URI for the person.
Chris Wrendde75302014-03-26 17:24:15 -04003772 * @see Notification#EXTRA_PEOPLE
Selim Cineke7238dd2017-12-14 17:48:32 -08003773 * @deprecated use {@link #addPerson(Person)}
Chris Wrendde75302014-03-26 17:24:15 -04003774 */
Chris Wrene6c48932014-09-29 17:19:27 -04003775 public Builder addPerson(String uri) {
Selim Cineke7238dd2017-12-14 17:48:32 -08003776 addPerson(new Person().setUri(uri));
3777 return this;
3778 }
3779
3780 /**
3781 * Add a person that is relevant to this notification.
3782 *
3783 * <P>
3784 * Depending on user preferences, this annotation may allow the notification to pass
3785 * through interruption filters, if this notification is of category {@link #CATEGORY_CALL}
3786 * or {@link #CATEGORY_MESSAGE}. The addition of people may also cause this notification to
3787 * appear more prominently in the user interface.
3788 * </P>
3789 *
3790 * <P>
3791 * A person should usually contain a uri in order to benefit from the ranking boost.
3792 * However, even if no uri is provided, it's beneficial to provide other people in the
3793 * notification, such that listeners and voice only devices can announce and handle them
3794 * properly.
3795 * </P>
3796 *
3797 * @param person the person to add.
3798 * @see Notification#EXTRA_PEOPLE_LIST
3799 */
3800 public Builder addPerson(Person person) {
3801 mPersonList.add(person);
Chris Wrendde75302014-03-26 17:24:15 -04003802 return this;
3803 }
3804
3805 /**
Griff Hazen5cadc3b2014-05-20 09:55:39 -07003806 * Set this notification to be part of a group of notifications sharing the same key.
3807 * Grouped notifications may display in a cluster or stack on devices which
3808 * support such rendering.
3809 *
3810 * <p>To make this notification the summary for its group, also call
3811 * {@link #setGroupSummary}. A sort order can be specified for group members by using
3812 * {@link #setSortKey}.
3813 * @param groupKey The group key of the group.
3814 * @return this object for method chaining
3815 */
3816 public Builder setGroup(String groupKey) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003817 mN.mGroupKey = groupKey;
Griff Hazen5cadc3b2014-05-20 09:55:39 -07003818 return this;
3819 }
3820
3821 /**
3822 * Set this notification to be the group summary for a group of notifications.
3823 * Grouped notifications may display in a cluster or stack on devices which
Julia Reynolds04499532016-09-13 14:04:53 -04003824 * support such rendering. If thereRequires a group key also be set using {@link #setGroup}.
3825 * The group summary may be suppressed if too few notifications are included in the group.
Griff Hazen5cadc3b2014-05-20 09:55:39 -07003826 * @param isGroupSummary Whether this notification should be a group summary.
3827 * @return this object for method chaining
3828 */
3829 public Builder setGroupSummary(boolean isGroupSummary) {
3830 setFlag(FLAG_GROUP_SUMMARY, isGroupSummary);
3831 return this;
3832 }
3833
3834 /**
3835 * Set a sort key that orders this notification among other notifications from the
3836 * same package. This can be useful if an external sort was already applied and an app
3837 * would like to preserve this. Notifications will be sorted lexicographically using this
3838 * value, although providing different priorities in addition to providing sort key may
3839 * cause this value to be ignored.
3840 *
3841 * <p>This sort key can also be used to order members of a notification group. See
Griff Hazen9e1379f2014-05-20 12:50:51 -07003842 * {@link #setGroup}.
Griff Hazen5cadc3b2014-05-20 09:55:39 -07003843 *
3844 * @see String#compareTo(String)
3845 */
3846 public Builder setSortKey(String sortKey) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003847 mN.mSortKey = sortKey;
Griff Hazen5cadc3b2014-05-20 09:55:39 -07003848 return this;
3849 }
3850
3851 /**
Griff Hazen720042b2014-02-24 15:46:56 -08003852 * Merge additional metadata into this notification.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003853 *
Griff Hazen720042b2014-02-24 15:46:56 -08003854 * <p>Values within the Bundle will replace existing extras values in this Builder.
3855 *
3856 * @see Notification#extras
3857 */
Griff Hazen959591e2014-05-15 22:26:18 -07003858 public Builder addExtras(Bundle extras) {
3859 if (extras != null) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003860 mUserExtras.putAll(extras);
Griff Hazen720042b2014-02-24 15:46:56 -08003861 }
3862 return this;
3863 }
3864
3865 /**
3866 * Set metadata for this notification.
3867 *
3868 * <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 -04003869 * current contents are copied into the Notification each time {@link #build()} is
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003870 * called.
3871 *
Griff Hazen720042b2014-02-24 15:46:56 -08003872 * <p>Replaces any existing extras values with those from the provided Bundle.
3873 * Use {@link #addExtras} to merge in metadata instead.
3874 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003875 * @see Notification#extras
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003876 */
Griff Hazen959591e2014-05-15 22:26:18 -07003877 public Builder setExtras(Bundle extras) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003878 if (extras != null) {
3879 mUserExtras = extras;
3880 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003881 return this;
3882 }
3883
Daniel Sandlera0a938c2012-03-15 08:42:37 -04003884 /**
Griff Hazen720042b2014-02-24 15:46:56 -08003885 * Get the current metadata Bundle used by this notification Builder.
3886 *
3887 * <p>The returned Bundle is shared with this Builder.
3888 *
3889 * <p>The current contents of this Bundle are copied into the Notification each time
3890 * {@link #build()} is called.
3891 *
3892 * @see Notification#extras
3893 */
3894 public Bundle getExtras() {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003895 return mUserExtras;
3896 }
3897
3898 private Bundle getAllExtras() {
3899 final Bundle saveExtras = (Bundle) mUserExtras.clone();
3900 saveExtras.putAll(mN.extras);
3901 return saveExtras;
Griff Hazen720042b2014-02-24 15:46:56 -08003902 }
3903
3904 /**
Daniel Sandlera0a938c2012-03-15 08:42:37 -04003905 * Add an action to this notification. Actions are typically displayed by
3906 * the system as a button adjacent to the notification content.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04003907 * <p>
3908 * Every action must have an icon (32dp square and matching the
3909 * <a href="{@docRoot}design/style/iconography.html#action-bar">Holo
3910 * Dark action bar</a> visual style), a textual label, and a {@link PendingIntent}.
3911 * <p>
3912 * A notification in its expanded form can display up to 3 actions, from left to right in
3913 * the order they were added. Actions will not be displayed when the notification is
3914 * collapsed, however, so be sure that any essential functions may be accessed by the user
3915 * in some other way (for example, in the Activity pointed to by {@link #contentIntent}).
Daniel Sandlera0a938c2012-03-15 08:42:37 -04003916 *
3917 * @param icon Resource ID of a drawable that represents the action.
3918 * @param title Text describing the action.
3919 * @param intent PendingIntent to be fired when the action is invoked.
Dan Sandler86647982015-05-13 23:41:13 -04003920 *
3921 * @deprecated Use {@link #addAction(Action)} instead.
Daniel Sandlera0a938c2012-03-15 08:42:37 -04003922 */
Dan Sandler86647982015-05-13 23:41:13 -04003923 @Deprecated
Daniel Sandlera0a938c2012-03-15 08:42:37 -04003924 public Builder addAction(int icon, CharSequence title, PendingIntent intent) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04003925 mActions.add(new Action(icon, safeCharSequence(title), intent));
Daniel Sandlera0a938c2012-03-15 08:42:37 -04003926 return this;
3927 }
3928
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003929 /**
Griff Hazen959591e2014-05-15 22:26:18 -07003930 * Add an action to this notification. Actions are typically displayed by
3931 * the system as a button adjacent to the notification content.
3932 * <p>
3933 * Every action must have an icon (32dp square and matching the
3934 * <a href="{@docRoot}design/style/iconography.html#action-bar">Holo
3935 * Dark action bar</a> visual style), a textual label, and a {@link PendingIntent}.
3936 * <p>
3937 * A notification in its expanded form can display up to 3 actions, from left to right in
3938 * the order they were added. Actions will not be displayed when the notification is
3939 * collapsed, however, so be sure that any essential functions may be accessed by the user
3940 * in some other way (for example, in the Activity pointed to by {@link #contentIntent}).
3941 *
3942 * @param action The action to add.
3943 */
3944 public Builder addAction(Action action) {
liangweikang63b03b52017-03-16 19:22:15 +08003945 if (action != null) {
3946 mActions.add(action);
3947 }
Griff Hazen959591e2014-05-15 22:26:18 -07003948 return this;
3949 }
3950
3951 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003952 * Alter the complete list of actions attached to this notification.
3953 * @see #addAction(Action).
3954 *
3955 * @param actions
3956 * @return
3957 */
3958 public Builder setActions(Action... actions) {
3959 mActions.clear();
3960 for (int i = 0; i < actions.length; i++) {
liangweikang63b03b52017-03-16 19:22:15 +08003961 if (actions[i] != null) {
3962 mActions.add(actions[i]);
3963 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003964 }
3965 return this;
3966 }
3967
3968 /**
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003969 * Add a rich notification style to be applied at build time.
3970 *
3971 * @param style Object responsible for modifying the notification style.
3972 */
3973 public Builder setStyle(Style style) {
3974 if (mStyle != style) {
3975 mStyle = style;
Daniel Sandlerc08dea22012-06-28 08:35:24 -07003976 if (mStyle != null) {
3977 mStyle.setBuilder(this);
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003978 mN.extras.putString(EXTRA_TEMPLATE, style.getClass().getName());
3979 } else {
3980 mN.extras.remove(EXTRA_TEMPLATE);
Daniel Sandlerc08dea22012-06-28 08:35:24 -07003981 }
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003982 }
3983 return this;
3984 }
3985
Dan Sandler0bf2ed82013-12-21 23:33:41 -06003986 /**
3987 * Specify the value of {@link #visibility}.
Griff Hazenb720abe2014-05-20 13:15:30 -07003988 *
Dan Sandler0bf2ed82013-12-21 23:33:41 -06003989 * @return The same Builder.
3990 */
Jeff Sharkey30e06bb2017-04-24 11:18:03 -06003991 public Builder setVisibility(@Visibility int visibility) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003992 mN.visibility = visibility;
Dan Sandler0bf2ed82013-12-21 23:33:41 -06003993 return this;
3994 }
3995
3996 /**
3997 * Supply a replacement Notification whose contents should be shown in insecure contexts
3998 * (i.e. atop the secure lockscreen). See {@link #visibility} and {@link #VISIBILITY_PUBLIC}.
3999 * @param n A replacement notification, presumably with some or all info redacted.
4000 * @return The same Builder.
4001 */
4002 public Builder setPublicVersion(Notification n) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004003 if (n != null) {
4004 mN.publicVersion = new Notification();
4005 n.cloneInto(mN.publicVersion, /*heavy=*/ true);
4006 } else {
4007 mN.publicVersion = null;
4008 }
Dan Sandler0bf2ed82013-12-21 23:33:41 -06004009 return this;
4010 }
4011
Griff Hazenb720abe2014-05-20 13:15:30 -07004012 /**
Griff Hazen5cadc3b2014-05-20 09:55:39 -07004013 * Apply an extender to this notification builder. Extenders may be used to add
4014 * metadata or change options on this builder.
4015 */
Griff Hazen61a9e862014-05-22 16:05:19 -07004016 public Builder extend(Extender extender) {
4017 extender.extend(this);
Griff Hazen5cadc3b2014-05-20 09:55:39 -07004018 return this;
4019 }
4020
Dan Sandler4e787062015-06-17 15:09:48 -04004021 /**
4022 * @hide
4023 */
Julia Reynoldse46bb372016-03-17 11:05:58 -04004024 public Builder setFlag(int mask, boolean value) {
Joe Onorato46439ce2010-11-19 13:56:21 -08004025 if (value) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004026 mN.flags |= mask;
Joe Onorato46439ce2010-11-19 13:56:21 -08004027 } else {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004028 mN.flags &= ~mask;
Joe Onorato46439ce2010-11-19 13:56:21 -08004029 }
Julia Reynoldse46bb372016-03-17 11:05:58 -04004030 return this;
Joe Onorato46439ce2010-11-19 13:56:21 -08004031 }
4032
Dan Sandler26e81cf2014-05-06 10:01:27 -04004033 /**
4034 * Sets {@link Notification#color}.
4035 *
4036 * @param argb The accent color to use
4037 *
4038 * @return The same Builder.
4039 */
Tor Norbye80756e32015-03-02 09:39:27 -08004040 public Builder setColor(@ColorInt int argb) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004041 mN.color = argb;
Julia Reynolds10ee1fc2015-11-09 11:04:55 -05004042 sanitizeColor();
Dan Sandler26e81cf2014-05-06 10:01:27 -04004043 return this;
4044 }
4045
Jorim Jaggid05aa3e2014-08-28 17:52:27 +02004046 private Drawable getProfileBadgeDrawable() {
Chris Wren66619a22016-05-12 16:42:37 -04004047 if (mContext.getUserId() == UserHandle.USER_SYSTEM) {
4048 // This user can never be a badged profile,
4049 // and also includes USER_ALL system notifications.
4050 return null;
4051 }
Christoph Studer7ac80e62014-08-04 16:01:57 +02004052 // Note: This assumes that the current user can read the profile badge of the
4053 // originating user.
Selim Cineke6ff9462016-01-15 15:07:06 -08004054 return mContext.getPackageManager().getUserBadgeForDensityNoBackground(
Julia Reynoldsda303542015-11-23 14:00:20 -05004055 new UserHandle(mContext.getUserId()), 0);
Jorim Jaggid05aa3e2014-08-28 17:52:27 +02004056 }
4057
4058 private Bitmap getProfileBadge() {
4059 Drawable badge = getProfileBadgeDrawable();
Kenny Guy8a0101b2014-05-08 23:34:12 +01004060 if (badge == null) {
4061 return null;
4062 }
Jorim Jaggid05aa3e2014-08-28 17:52:27 +02004063 final int size = mContext.getResources().getDimensionPixelSize(
4064 R.dimen.notification_badge_size);
4065 Bitmap bitmap = Bitmap.createBitmap(size, size, Bitmap.Config.ARGB_8888);
Kenny Guy8a0101b2014-05-08 23:34:12 +01004066 Canvas canvas = new Canvas(bitmap);
Jorim Jaggid05aa3e2014-08-28 17:52:27 +02004067 badge.setBounds(0, 0, size, size);
Kenny Guy8a0101b2014-05-08 23:34:12 +01004068 badge.draw(canvas);
4069 return bitmap;
4070 }
4071
Selim Cinekc848c3a2016-01-13 15:27:30 -08004072 private void bindProfileBadge(RemoteViews contentView) {
Kenny Guy98193ea2014-07-24 19:54:37 +01004073 Bitmap profileBadge = getProfileBadge();
4074
Kenny Guy98193ea2014-07-24 19:54:37 +01004075 if (profileBadge != null) {
Selim Cinekc848c3a2016-01-13 15:27:30 -08004076 contentView.setImageViewBitmap(R.id.profile_badge, profileBadge);
4077 contentView.setViewVisibility(R.id.profile_badge, View.VISIBLE);
Selim Cinek7b9605b2017-01-19 17:36:00 -08004078 if (isColorized()) {
Sunny Goyal5b153922017-09-21 21:00:36 -07004079 contentView.setDrawableTint(R.id.profile_badge, false,
4080 getPrimaryTextColor(), PorterDuff.Mode.SRC_ATOP);
Selim Cinek7b9605b2017-01-19 17:36:00 -08004081 }
Kenny Guy98193ea2014-07-24 19:54:37 +01004082 }
Kenny Guy98193ea2014-07-24 19:54:37 +01004083 }
4084
Christoph Studerfe718432014-09-01 18:21:18 +02004085 private void resetStandardTemplate(RemoteViews contentView) {
Selim Cinekeaa29ca2015-11-23 13:51:13 -08004086 resetNotificationHeader(contentView);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004087 resetContentMargins(contentView);
Christoph Studerfe718432014-09-01 18:21:18 +02004088 contentView.setViewVisibility(R.id.right_icon, View.GONE);
Selim Cinek860b6da2015-12-16 19:02:19 -08004089 contentView.setViewVisibility(R.id.title, View.GONE);
Christoph Studerfe718432014-09-01 18:21:18 +02004090 contentView.setTextViewText(R.id.title, null);
Selim Cinek41598732016-01-11 16:58:37 -08004091 contentView.setViewVisibility(R.id.text, View.GONE);
Christoph Studerfe718432014-09-01 18:21:18 +02004092 contentView.setTextViewText(R.id.text, null);
Selim Cinek29603462015-11-17 19:04:39 -08004093 contentView.setViewVisibility(R.id.text_line_1, View.GONE);
Selim Cinek41598732016-01-11 16:58:37 -08004094 contentView.setTextViewText(R.id.text_line_1, null);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004095 }
4096
Selim Cinekeaa29ca2015-11-23 13:51:13 -08004097 /**
4098 * Resets the notification header to its original state
4099 */
4100 private void resetNotificationHeader(RemoteViews contentView) {
Adrian Roosc4337a32016-08-02 18:30:34 -07004101 // Small icon doesn't need to be reset, as it's always set. Resetting would prevent
4102 // re-using the drawable when the notification is updated.
Selim Cinek7b836392015-12-04 20:02:59 -08004103 contentView.setBoolean(R.id.notification_header, "setExpanded", false);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004104 contentView.setTextViewText(R.id.app_name_text, null);
Christoph Studerca1db712014-09-10 17:31:33 +02004105 contentView.setViewVisibility(R.id.chronometer, View.GONE);
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07004106 contentView.setViewVisibility(R.id.header_text, View.GONE);
Adrian Roos9dfb78f2016-06-30 15:43:44 -07004107 contentView.setTextViewText(R.id.header_text, null);
Selim Cinekafeed292017-12-12 17:32:44 -08004108 contentView.setViewVisibility(R.id.header_text_secondary, View.GONE);
4109 contentView.setTextViewText(R.id.header_text_secondary, null);
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07004110 contentView.setViewVisibility(R.id.header_text_divider, View.GONE);
Selim Cinekafeed292017-12-12 17:32:44 -08004111 contentView.setViewVisibility(R.id.header_text_secondary_divider, View.GONE);
Selim Cinek29603462015-11-17 19:04:39 -08004112 contentView.setViewVisibility(R.id.time_divider, View.GONE);
Selim Cinekb85f36fd2016-04-20 18:46:36 -07004113 contentView.setViewVisibility(R.id.time, View.GONE);
Selim Cinekc848c3a2016-01-13 15:27:30 -08004114 contentView.setImageViewIcon(R.id.profile_badge, null);
4115 contentView.setViewVisibility(R.id.profile_badge, View.GONE);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004116 }
4117
4118 private void resetContentMargins(RemoteViews contentView) {
Adrian Roos2d5dbba2016-06-08 17:11:53 -07004119 contentView.setViewLayoutMarginEndDimen(R.id.line1, 0);
4120 contentView.setViewLayoutMarginEndDimen(R.id.text, 0);
Christoph Studerfe718432014-09-01 18:21:18 +02004121 }
4122
Jorim Jaggi445d3c02014-08-19 22:33:42 +02004123 private RemoteViews applyStandardTemplate(int resId) {
Adrian Roos70d7aa32017-01-11 15:39:06 -08004124 return applyStandardTemplate(resId, mParams.reset().fillTextsFrom(this));
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02004125 }
4126
4127 /**
4128 * @param hasProgress whether the progress bar should be shown and set
4129 */
4130 private RemoteViews applyStandardTemplate(int resId, boolean hasProgress) {
Adrian Roos70d7aa32017-01-11 15:39:06 -08004131 return applyStandardTemplate(resId, mParams.reset().hasProgress(hasProgress)
4132 .fillTextsFrom(this));
Adrian Roosc1a80b02016-04-05 14:54:55 -07004133 }
4134
Adrian Roos70d7aa32017-01-11 15:39:06 -08004135 private RemoteViews applyStandardTemplate(int resId, StandardTemplateParams p) {
Kenny Guy77320062014-08-27 21:37:15 +01004136 RemoteViews contentView = new BuilderRemoteViews(mContext.getApplicationInfo(), resId);
Dan Sandler539aad42014-08-04 00:43:39 -04004137
Christoph Studerfe718432014-09-01 18:21:18 +02004138 resetStandardTemplate(contentView);
4139
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004140 final Bundle ex = mN.extras;
Selim Cinek7b9605b2017-01-19 17:36:00 -08004141 updateBackgroundColor(contentView);
Selim Cinekafeed292017-12-12 17:32:44 -08004142 bindNotificationHeader(contentView, p.ambient, p.headerTextSecondary);
Lucas Dupin90158d02018-01-23 16:36:12 -08004143 bindLargeIcon(contentView, p.hideLargeIcon || p.ambient, p.alwaysShowReply);
Adrian Roos70d7aa32017-01-11 15:39:06 -08004144 boolean showProgress = handleProgressBar(p.hasProgress, contentView, ex);
4145 if (p.title != null) {
Selim Cinek860b6da2015-12-16 19:02:19 -08004146 contentView.setViewVisibility(R.id.title, View.VISIBLE);
Selim Cinek48f66b72017-08-18 16:17:51 -07004147 contentView.setTextViewText(R.id.title, processTextSpans(p.title));
Adrian Roos72171622017-01-27 10:32:06 -08004148 if (!p.ambient) {
4149 setTextViewColorPrimary(contentView, R.id.title);
4150 }
Selim Cinek954cc232016-05-20 13:29:23 -07004151 contentView.setViewLayoutWidth(R.id.title, showProgress
4152 ? ViewGroup.LayoutParams.WRAP_CONTENT
4153 : ViewGroup.LayoutParams.MATCH_PARENT);
Joe Onorato561d3852010-11-20 18:09:34 -08004154 }
Adrian Roos70d7aa32017-01-11 15:39:06 -08004155 if (p.text != null) {
Selim Cinek41598732016-01-11 16:58:37 -08004156 int textId = showProgress ? com.android.internal.R.id.text_line_1
4157 : com.android.internal.R.id.text;
Selim Cinek48f66b72017-08-18 16:17:51 -07004158 contentView.setTextViewText(textId, processTextSpans(p.text));
Adrian Roos72171622017-01-27 10:32:06 -08004159 if (!p.ambient) {
4160 setTextViewColorSecondary(contentView, textId);
4161 }
Selim Cinek41598732016-01-11 16:58:37 -08004162 contentView.setViewVisibility(textId, View.VISIBLE);
Joe Onorato561d3852010-11-20 18:09:34 -08004163 }
Selim Cinekc848c3a2016-01-13 15:27:30 -08004164
Selim Cinek279fa862016-06-14 10:57:25 -07004165 setContentMinHeight(contentView, showProgress || mN.hasLargeIcon());
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004166
Selim Cinek29603462015-11-17 19:04:39 -08004167 return contentView;
4168 }
4169
Selim Cinek48f66b72017-08-18 16:17:51 -07004170 private CharSequence processTextSpans(CharSequence text) {
4171 if (hasForegroundColor()) {
Selim Cinek87c31532017-08-18 18:53:44 -07004172 return NotificationColorUtil.clearColorSpans(text);
Selim Cinek48f66b72017-08-18 16:17:51 -07004173 }
4174 return text;
4175 }
4176
Selim Cinek7b9605b2017-01-19 17:36:00 -08004177 private void setTextViewColorPrimary(RemoteViews contentView, int id) {
4178 ensureColors();
4179 contentView.setTextColor(id, mPrimaryTextColor);
4180 }
4181
Selim Cinek48f66b72017-08-18 16:17:51 -07004182 private boolean hasForegroundColor() {
4183 return mForegroundColor != COLOR_INVALID;
4184 }
4185
Selim Cinek389edcd2017-05-11 19:16:44 -07004186 /**
4187 * @return the primary text color
4188 * @hide
4189 */
4190 @VisibleForTesting
4191 public int getPrimaryTextColor() {
Selim Cinek7b9605b2017-01-19 17:36:00 -08004192 ensureColors();
4193 return mPrimaryTextColor;
4194 }
4195
Selim Cinek389edcd2017-05-11 19:16:44 -07004196 /**
4197 * @return the secondary text color
4198 * @hide
4199 */
4200 @VisibleForTesting
4201 public int getSecondaryTextColor() {
4202 ensureColors();
4203 return mSecondaryTextColor;
4204 }
4205
Selim Cinek7b9605b2017-01-19 17:36:00 -08004206 private int getActionBarColor() {
4207 ensureColors();
4208 return mActionBarColor;
4209 }
4210
Selim Cinek622c64a2017-04-17 17:10:05 -07004211 private int getActionBarColorDeEmphasized() {
4212 int backgroundColor = getBackgroundColor();
4213 return NotificationColorUtil.getShiftedColor(backgroundColor, 12);
4214 }
4215
Selim Cinek7b9605b2017-01-19 17:36:00 -08004216 private void setTextViewColorSecondary(RemoteViews contentView, int id) {
4217 ensureColors();
4218 contentView.setTextColor(id, mSecondaryTextColor);
4219 }
4220
4221 private void ensureColors() {
4222 int backgroundColor = getBackgroundColor();
4223 if (mPrimaryTextColor == COLOR_INVALID
4224 || mSecondaryTextColor == COLOR_INVALID
4225 || mActionBarColor == COLOR_INVALID
4226 || mTextColorsAreForBackground != backgroundColor) {
4227 mTextColorsAreForBackground = backgroundColor;
Selim Cinek48f66b72017-08-18 16:17:51 -07004228 if (!hasForegroundColor() || !isColorized()) {
Selim Cinek5fb73f82017-04-20 16:55:38 -07004229 mPrimaryTextColor = NotificationColorUtil.resolvePrimaryColor(mContext,
4230 backgroundColor);
4231 mSecondaryTextColor = NotificationColorUtil.resolveSecondaryColor(mContext,
4232 backgroundColor);
Selim Cinekac5f0272017-05-02 16:05:41 -07004233 if (backgroundColor != COLOR_DEFAULT
4234 && (mBackgroundColorHint != COLOR_INVALID || isColorized())) {
4235 mPrimaryTextColor = NotificationColorUtil.findAlphaToMeetContrast(
4236 mPrimaryTextColor, backgroundColor, 4.5);
4237 mSecondaryTextColor = NotificationColorUtil.findAlphaToMeetContrast(
4238 mSecondaryTextColor, backgroundColor, 4.5);
4239 }
Selim Cinek5fb73f82017-04-20 16:55:38 -07004240 } else {
4241 double backLum = NotificationColorUtil.calculateLuminance(backgroundColor);
4242 double textLum = NotificationColorUtil.calculateLuminance(mForegroundColor);
4243 double contrast = NotificationColorUtil.calculateContrast(mForegroundColor,
4244 backgroundColor);
Selim Cinek389edcd2017-05-11 19:16:44 -07004245 // We only respect the given colors if worst case Black or White still has
4246 // contrast
4247 boolean backgroundLight = backLum > textLum
4248 && satisfiesTextContrast(backgroundColor, Color.BLACK)
4249 || backLum <= textLum
4250 && !satisfiesTextContrast(backgroundColor, Color.WHITE);
Selim Cinek5fb73f82017-04-20 16:55:38 -07004251 if (contrast < 4.5f) {
Selim Cinek389edcd2017-05-11 19:16:44 -07004252 if (backgroundLight) {
Selim Cinek5fb73f82017-04-20 16:55:38 -07004253 mSecondaryTextColor = NotificationColorUtil.findContrastColor(
4254 mForegroundColor,
4255 backgroundColor,
4256 true /* findFG */,
4257 4.5f);
4258 mPrimaryTextColor = NotificationColorUtil.changeColorLightness(
Selim Cinek389edcd2017-05-11 19:16:44 -07004259 mSecondaryTextColor, -LIGHTNESS_TEXT_DIFFERENCE_LIGHT);
Selim Cinek5fb73f82017-04-20 16:55:38 -07004260 } else {
4261 mSecondaryTextColor =
4262 NotificationColorUtil.findContrastColorAgainstDark(
4263 mForegroundColor,
4264 backgroundColor,
4265 true /* findFG */,
4266 4.5f);
4267 mPrimaryTextColor = NotificationColorUtil.changeColorLightness(
Selim Cinek389edcd2017-05-11 19:16:44 -07004268 mSecondaryTextColor, -LIGHTNESS_TEXT_DIFFERENCE_DARK);
Selim Cinek5fb73f82017-04-20 16:55:38 -07004269 }
4270 } else {
4271 mPrimaryTextColor = mForegroundColor;
4272 mSecondaryTextColor = NotificationColorUtil.changeColorLightness(
Selim Cinek389edcd2017-05-11 19:16:44 -07004273 mPrimaryTextColor, backgroundLight ? LIGHTNESS_TEXT_DIFFERENCE_LIGHT
4274 : LIGHTNESS_TEXT_DIFFERENCE_DARK);
Selim Cinek5fb73f82017-04-20 16:55:38 -07004275 if (NotificationColorUtil.calculateContrast(mSecondaryTextColor,
4276 backgroundColor) < 4.5f) {
4277 // oh well the secondary is not good enough
Selim Cinek389edcd2017-05-11 19:16:44 -07004278 if (backgroundLight) {
Selim Cinek5fb73f82017-04-20 16:55:38 -07004279 mSecondaryTextColor = NotificationColorUtil.findContrastColor(
4280 mSecondaryTextColor,
4281 backgroundColor,
4282 true /* findFG */,
4283 4.5f);
4284 } else {
4285 mSecondaryTextColor
4286 = NotificationColorUtil.findContrastColorAgainstDark(
4287 mSecondaryTextColor,
4288 backgroundColor,
4289 true /* findFG */,
4290 4.5f);
4291 }
4292 mPrimaryTextColor = NotificationColorUtil.changeColorLightness(
Selim Cinek389edcd2017-05-11 19:16:44 -07004293 mSecondaryTextColor, backgroundLight
4294 ? -LIGHTNESS_TEXT_DIFFERENCE_LIGHT
4295 : -LIGHTNESS_TEXT_DIFFERENCE_DARK);
Selim Cinek5fb73f82017-04-20 16:55:38 -07004296 }
4297 }
4298 }
Selim Cinek875ba9b2017-02-13 16:20:17 -08004299 mActionBarColor = NotificationColorUtil.resolveActionBarColor(mContext,
4300 backgroundColor);
Selim Cinek7b9605b2017-01-19 17:36:00 -08004301 }
4302 }
4303
4304 private void updateBackgroundColor(RemoteViews contentView) {
4305 if (isColorized()) {
4306 contentView.setInt(R.id.status_bar_latest_event_content, "setBackgroundColor",
4307 getBackgroundColor());
4308 } else {
4309 // Clear it!
4310 contentView.setInt(R.id.status_bar_latest_event_content, "setBackgroundResource",
4311 0);
4312 }
4313 }
4314
Selim Cinek860b6da2015-12-16 19:02:19 -08004315 /**
4316 * @param remoteView the remote view to update the minheight in
4317 * @param hasMinHeight does it have a mimHeight
4318 * @hide
4319 */
4320 void setContentMinHeight(RemoteViews remoteView, boolean hasMinHeight) {
4321 int minHeight = 0;
4322 if (hasMinHeight) {
4323 // we need to set the minHeight of the notification
4324 minHeight = mContext.getResources().getDimensionPixelSize(
4325 com.android.internal.R.dimen.notification_min_content_height);
4326 }
4327 remoteView.setInt(R.id.notification_main_column, "setMinimumHeight", minHeight);
4328 }
4329
Selim Cinek29603462015-11-17 19:04:39 -08004330 private boolean handleProgressBar(boolean hasProgress, RemoteViews contentView, Bundle ex) {
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004331 final int max = ex.getInt(EXTRA_PROGRESS_MAX, 0);
4332 final int progress = ex.getInt(EXTRA_PROGRESS, 0);
4333 final boolean ind = ex.getBoolean(EXTRA_PROGRESS_INDETERMINATE);
4334 if (hasProgress && (max != 0 || ind)) {
Selim Cinek29603462015-11-17 19:04:39 -08004335 contentView.setViewVisibility(com.android.internal.R.id.progress, View.VISIBLE);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004336 contentView.setProgressBar(
Selim Cinek29603462015-11-17 19:04:39 -08004337 R.id.progress, max, progress, ind);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004338 contentView.setProgressBackgroundTintList(
4339 R.id.progress, ColorStateList.valueOf(mContext.getColor(
4340 R.color.notification_progress_background_color)));
Selim Cinek29603462015-11-17 19:04:39 -08004341 if (mN.color != COLOR_DEFAULT) {
Adrian Roos4ff3b122016-02-01 12:26:13 -08004342 ColorStateList colorStateList = ColorStateList.valueOf(resolveContrastColor());
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004343 contentView.setProgressTintList(R.id.progress, colorStateList);
4344 contentView.setProgressIndeterminateTintList(R.id.progress, colorStateList);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004345 }
Selim Cinek29603462015-11-17 19:04:39 -08004346 return true;
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004347 } else {
4348 contentView.setViewVisibility(R.id.progress, View.GONE);
Selim Cinek29603462015-11-17 19:04:39 -08004349 return false;
Jeff Sharkey1c400132011-08-05 14:50:13 -07004350 }
Joe Onorato561d3852010-11-20 18:09:34 -08004351 }
4352
Selim Cinek88188f22017-09-19 16:46:56 -07004353 private void bindLargeIcon(RemoteViews contentView, boolean hideLargeIcon,
4354 boolean alwaysShowReply) {
Selim Cinek279fa862016-06-14 10:57:25 -07004355 if (mN.mLargeIcon == null && mN.largeIcon != null) {
4356 mN.mLargeIcon = Icon.createWithBitmap(mN.largeIcon);
4357 }
Selim Cinek88188f22017-09-19 16:46:56 -07004358 boolean showLargeIcon = mN.mLargeIcon != null && !hideLargeIcon;
4359 if (showLargeIcon) {
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004360 contentView.setViewVisibility(R.id.right_icon, View.VISIBLE);
4361 contentView.setImageViewIcon(R.id.right_icon, mN.mLargeIcon);
4362 processLargeLegacyIcon(mN.mLargeIcon, contentView);
Adrian Roos2d5dbba2016-06-08 17:11:53 -07004363 int endMargin = R.dimen.notification_content_picture_margin;
4364 contentView.setViewLayoutMarginEndDimen(R.id.line1, endMargin);
4365 contentView.setViewLayoutMarginEndDimen(R.id.text, endMargin);
4366 contentView.setViewLayoutMarginEndDimen(R.id.progress, endMargin);
Selim Cinek88188f22017-09-19 16:46:56 -07004367 }
4368 // Bind the reply action
4369 Action action = findReplyAction();
Selim Cinek5d6ef8d2017-05-18 22:16:00 -07004370
Selim Cinek88188f22017-09-19 16:46:56 -07004371 boolean actionVisible = action != null && (showLargeIcon || alwaysShowReply);
4372 int replyId = showLargeIcon ? R.id.reply_icon_action : R.id.right_icon;
4373 if (actionVisible) {
4374 // We're only showing the icon as big if we're hiding the large icon
4375 int contrastColor = resolveContrastColor();
4376 int iconColor;
4377 if (showLargeIcon) {
Sunny Goyal5b153922017-09-21 21:00:36 -07004378 contentView.setDrawableTint(R.id.reply_icon_action,
Selim Cinek5d6ef8d2017-05-18 22:16:00 -07004379 true /* targetBackground */,
Sunny Goyal5b153922017-09-21 21:00:36 -07004380 contrastColor, PorterDuff.Mode.SRC_ATOP);
Selim Cinek5d6ef8d2017-05-18 22:16:00 -07004381 contentView.setOnClickPendingIntent(R.id.right_icon,
4382 action.actionIntent);
Selim Cinek5d6ef8d2017-05-18 22:16:00 -07004383 contentView.setRemoteInputs(R.id.right_icon, action.mRemoteInputs);
Selim Cinek88188f22017-09-19 16:46:56 -07004384 iconColor = NotificationColorUtil.isColorLight(contrastColor)
4385 ? Color.BLACK : Color.WHITE;
4386 } else {
4387 contentView.setImageViewResource(R.id.right_icon,
4388 R.drawable.ic_reply_notification_large);
4389 contentView.setViewVisibility(R.id.right_icon, View.VISIBLE);
4390 iconColor = contrastColor;
Selim Cinek5d6ef8d2017-05-18 22:16:00 -07004391 }
Selim Cinek88188f22017-09-19 16:46:56 -07004392 contentView.setDrawableTint(replyId,
4393 false /* targetBackground */,
4394 iconColor,
4395 PorterDuff.Mode.SRC_ATOP);
4396 contentView.setOnClickPendingIntent(replyId,
4397 action.actionIntent);
4398 contentView.setRemoteInputs(replyId, action.mRemoteInputs);
4399 } else {
4400 contentView.setRemoteInputs(R.id.right_icon, null);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004401 }
Selim Cinek88188f22017-09-19 16:46:56 -07004402 contentView.setViewVisibility(R.id.reply_icon_action, actionVisible && showLargeIcon
4403 ? View.VISIBLE
4404 : View.GONE);
4405 contentView.setViewVisibility(R.id.right_icon_container, actionVisible || showLargeIcon
Selim Cinek5d6ef8d2017-05-18 22:16:00 -07004406 ? View.VISIBLE
4407 : View.GONE);
4408 }
4409
4410 private Action findReplyAction() {
4411 ArrayList<Action> actions = mActions;
4412 if (mOriginalActions != null) {
4413 actions = mOriginalActions;
4414 }
4415 int numActions = actions.size();
4416 for (int i = 0; i < numActions; i++) {
4417 Action action = actions.get(i);
4418 if (hasValidRemoteInput(action)) {
4419 return action;
4420 }
4421 }
4422 return null;
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004423 }
4424
Selim Cinekafeed292017-12-12 17:32:44 -08004425 private void bindNotificationHeader(RemoteViews contentView, boolean ambient,
4426 CharSequence secondaryHeaderText) {
Adrian Roos487374f2017-01-11 15:48:14 -08004427 bindSmallIcon(contentView, ambient);
4428 bindHeaderAppName(contentView, ambient);
4429 if (!ambient) {
4430 // Ambient view does not have these
4431 bindHeaderText(contentView);
Selim Cinekafeed292017-12-12 17:32:44 -08004432 bindHeaderTextSecondary(contentView, secondaryHeaderText);
Adrian Roos487374f2017-01-11 15:48:14 -08004433 bindHeaderChronometerAndTime(contentView);
Adrian Roos487374f2017-01-11 15:48:14 -08004434 bindProfileBadge(contentView);
4435 }
Adrian Roosd83e9992017-03-16 15:17:57 -07004436 bindExpandButton(contentView);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004437 }
4438
4439 private void bindExpandButton(RemoteViews contentView) {
Selim Cinek99104832017-01-25 14:47:33 -08004440 int color = getPrimaryHighlightColor();
Sunny Goyal5b153922017-09-21 21:00:36 -07004441 contentView.setDrawableTint(R.id.expand_button, false, color,
4442 PorterDuff.Mode.SRC_ATOP);
Selim Cinekea4bef72015-12-02 15:51:10 -08004443 contentView.setInt(R.id.notification_header, "setOriginalNotificationColor",
Selim Cinek7b9605b2017-01-19 17:36:00 -08004444 color);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004445 }
4446
Selim Cinek99104832017-01-25 14:47:33 -08004447 /**
4448 * @return the color that is used as the first primary highlight color. This is applied
4449 * in several places like the action buttons or the app name in the header.
4450 */
4451 private int getPrimaryHighlightColor() {
4452 return isColorized() ? getPrimaryTextColor() : resolveContrastColor();
4453 }
4454
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004455 private void bindHeaderChronometerAndTime(RemoteViews contentView) {
4456 if (showsTimeOrChronometer()) {
Selim Cinek29603462015-11-17 19:04:39 -08004457 contentView.setViewVisibility(R.id.time_divider, View.VISIBLE);
Selim Cinek7b9605b2017-01-19 17:36:00 -08004458 setTextViewColorSecondary(contentView, R.id.time_divider);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004459 if (mN.extras.getBoolean(EXTRA_SHOW_CHRONOMETER)) {
4460 contentView.setViewVisibility(R.id.chronometer, View.VISIBLE);
4461 contentView.setLong(R.id.chronometer, "setBase",
4462 mN.when + (SystemClock.elapsedRealtime() - System.currentTimeMillis()));
4463 contentView.setBoolean(R.id.chronometer, "setStarted", true);
Adrian Roos96b7e202016-05-17 13:50:38 -07004464 boolean countsDown = mN.extras.getBoolean(EXTRA_CHRONOMETER_COUNT_DOWN);
Selim Cinekc3b752e2016-04-20 16:13:59 -07004465 contentView.setChronometerCountDown(R.id.chronometer, countsDown);
Selim Cinek7b9605b2017-01-19 17:36:00 -08004466 setTextViewColorSecondary(contentView, R.id.chronometer);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004467 } else {
4468 contentView.setViewVisibility(R.id.time, View.VISIBLE);
4469 contentView.setLong(R.id.time, "setTime", mN.when);
Selim Cinek7b9605b2017-01-19 17:36:00 -08004470 setTextViewColorSecondary(contentView, R.id.time);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004471 }
Selim Cinekb85f36fd2016-04-20 18:46:36 -07004472 } else {
4473 // We still want a time to be set but gone, such that we can show and hide it
4474 // on demand in case it's a child notification without anything in the header
4475 contentView.setLong(R.id.time, "setTime", mN.when != 0 ? mN.when : mN.creationTime);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004476 }
4477 }
4478
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07004479 private void bindHeaderText(RemoteViews contentView) {
4480 CharSequence headerText = mN.extras.getCharSequence(EXTRA_SUB_TEXT);
4481 if (headerText == null && mStyle != null && mStyle.mSummaryTextSet
Selim Cinek03d0d652015-11-13 13:18:09 -05004482 && mStyle.hasSummaryInHeader()) {
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07004483 headerText = mStyle.mSummaryText;
Selim Cinek03d0d652015-11-13 13:18:09 -05004484 }
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07004485 if (headerText == null
4486 && mContext.getApplicationInfo().targetSdkVersion < Build.VERSION_CODES.N
4487 && mN.extras.getCharSequence(EXTRA_INFO_TEXT) != null) {
4488 headerText = mN.extras.getCharSequence(EXTRA_INFO_TEXT);
4489 }
4490 if (headerText != null) {
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004491 // TODO: Remove the span entirely to only have the string with propper formating.
Selim Cinek48f66b72017-08-18 16:17:51 -07004492 contentView.setTextViewText(R.id.header_text, processTextSpans(
4493 processLegacyText(headerText)));
Selim Cinek7b9605b2017-01-19 17:36:00 -08004494 setTextViewColorSecondary(contentView, R.id.header_text);
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07004495 contentView.setViewVisibility(R.id.header_text, View.VISIBLE);
4496 contentView.setViewVisibility(R.id.header_text_divider, View.VISIBLE);
Selim Cinek7b9605b2017-01-19 17:36:00 -08004497 setTextViewColorSecondary(contentView, R.id.header_text_divider);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004498 }
4499 }
4500
Selim Cinekafeed292017-12-12 17:32:44 -08004501 private void bindHeaderTextSecondary(RemoteViews contentView, CharSequence secondaryText) {
4502 if (!TextUtils.isEmpty(secondaryText)) {
4503 contentView.setTextViewText(R.id.header_text_secondary, processTextSpans(
4504 processLegacyText(secondaryText)));
4505 setTextViewColorSecondary(contentView, R.id.header_text_secondary);
4506 contentView.setViewVisibility(R.id.header_text_secondary, View.VISIBLE);
4507 contentView.setViewVisibility(R.id.header_text_secondary_divider, View.VISIBLE);
4508 setTextViewColorSecondary(contentView, R.id.header_text_secondary_divider);
4509 }
4510 }
4511
Adrian Rooseba05822016-04-22 17:09:27 -07004512 /**
4513 * @hide
4514 */
4515 public String loadHeaderAppName() {
Dan Sandler732bd6c2016-04-12 14:20:32 -04004516 CharSequence name = null;
4517 final PackageManager pm = mContext.getPackageManager();
4518 if (mN.extras.containsKey(EXTRA_SUBSTITUTE_APP_NAME)) {
4519 // only system packages which lump together a bunch of unrelated stuff
4520 // may substitute a different name to make the purpose of the
4521 // notification more clear. the correct package label should always
4522 // be accessible via SystemUI.
4523 final String pkg = mContext.getPackageName();
4524 final String subName = mN.extras.getString(EXTRA_SUBSTITUTE_APP_NAME);
4525 if (PackageManager.PERMISSION_GRANTED == pm.checkPermission(
4526 android.Manifest.permission.SUBSTITUTE_NOTIFICATION_APP_NAME, pkg)) {
4527 name = subName;
4528 } else {
4529 Log.w(TAG, "warning: pkg "
4530 + pkg + " attempting to substitute app name '" + subName
4531 + "' without holding perm "
4532 + android.Manifest.permission.SUBSTITUTE_NOTIFICATION_APP_NAME);
4533 }
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004534 }
Dan Sandler732bd6c2016-04-12 14:20:32 -04004535 if (TextUtils.isEmpty(name)) {
4536 name = pm.getApplicationLabel(mContext.getApplicationInfo());
4537 }
4538 if (TextUtils.isEmpty(name)) {
4539 // still nothing?
4540 return null;
4541 }
4542
4543 return String.valueOf(name);
4544 }
Adrian Roos487374f2017-01-11 15:48:14 -08004545 private void bindHeaderAppName(RemoteViews contentView, boolean ambient) {
Dan Sandler732bd6c2016-04-12 14:20:32 -04004546 contentView.setTextViewText(R.id.app_name_text, loadHeaderAppName());
Adrian Roos72171622017-01-27 10:32:06 -08004547 if (isColorized() && !ambient) {
Selim Cinek7b9605b2017-01-19 17:36:00 -08004548 setTextViewColorPrimary(contentView, R.id.app_name_text);
4549 } else {
4550 contentView.setTextColor(R.id.app_name_text,
4551 ambient ? resolveAmbientColor() : resolveContrastColor());
4552 }
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004553 }
4554
Adrian Roos487374f2017-01-11 15:48:14 -08004555 private void bindSmallIcon(RemoteViews contentView, boolean ambient) {
Selim Cinek279fa862016-06-14 10:57:25 -07004556 if (mN.mSmallIcon == null && mN.icon != 0) {
4557 mN.mSmallIcon = Icon.createWithResource(mContext, mN.icon);
4558 }
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004559 contentView.setImageViewIcon(R.id.icon, mN.mSmallIcon);
Sunny Goyal5b153922017-09-21 21:00:36 -07004560 contentView.setInt(R.id.icon, "setImageLevel", mN.iconLevel);
Adrian Roos487374f2017-01-11 15:48:14 -08004561 processSmallIconColor(mN.mSmallIcon, contentView, ambient);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004562 }
4563
Jorim Jaggi445d3c02014-08-19 22:33:42 +02004564 /**
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02004565 * @return true if the built notification will show the time or the chronometer; false
4566 * otherwise
4567 */
4568 private boolean showsTimeOrChronometer() {
Selim Cinekc2c0b042016-05-18 17:13:46 -07004569 return mN.showsTime() || mN.showsChronometer();
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02004570 }
4571
Christoph Studerfe718432014-09-01 18:21:18 +02004572 private void resetStandardTemplateWithActions(RemoteViews big) {
Adrian Roos4c1fcc82016-03-31 14:39:39 -07004573 // actions_container is only reset when there are no actions to avoid focus issues with
4574 // remote inputs.
Christoph Studerfe718432014-09-01 18:21:18 +02004575 big.setViewVisibility(R.id.actions, View.GONE);
Christoph Studerfe718432014-09-01 18:21:18 +02004576 big.removeAllViews(R.id.actions);
Adrian Roose458aa82015-12-08 16:17:19 -08004577
4578 big.setViewVisibility(R.id.notification_material_reply_container, View.GONE);
4579 big.setTextViewText(R.id.notification_material_reply_text_1, null);
4580
4581 big.setViewVisibility(R.id.notification_material_reply_text_2, View.GONE);
4582 big.setTextViewText(R.id.notification_material_reply_text_2, null);
4583 big.setViewVisibility(R.id.notification_material_reply_text_3, View.GONE);
4584 big.setTextViewText(R.id.notification_material_reply_text_3, null);
Adrian Roosf852a422016-06-03 13:33:43 -07004585
4586 big.setViewLayoutMarginBottomDimen(R.id.notification_action_list_margin_target, 0);
Christoph Studerfe718432014-09-01 18:21:18 +02004587 }
4588
Daniel Sandler96fd7c12012-03-30 16:37:36 -04004589 private RemoteViews applyStandardTemplateWithActions(int layoutId) {
Adrian Roos70d7aa32017-01-11 15:39:06 -08004590 return applyStandardTemplateWithActions(layoutId, mParams.reset().fillTextsFrom(this));
Adrian Roos48d746a2016-04-12 14:57:28 -07004591 }
4592
Adrian Roos70d7aa32017-01-11 15:39:06 -08004593 private RemoteViews applyStandardTemplateWithActions(int layoutId,
4594 StandardTemplateParams p) {
4595 RemoteViews big = applyStandardTemplate(layoutId, p);
Daniel Sandler96fd7c12012-03-30 16:37:36 -04004596
Christoph Studerfe718432014-09-01 18:21:18 +02004597 resetStandardTemplateWithActions(big);
4598
Adrian Roose458aa82015-12-08 16:17:19 -08004599 boolean validRemoteInput = false;
4600
Daniel Sandler96fd7c12012-03-30 16:37:36 -04004601 int N = mActions.size();
Adrian Roos487374f2017-01-11 15:48:14 -08004602 boolean emphazisedMode = mN.fullScreenIntent != null && !p.ambient;
Selim Cinek06e9e1f2016-07-08 17:14:16 -07004603 big.setBoolean(R.id.actions, "setEmphasizedMode", emphazisedMode);
Daniel Sandler96fd7c12012-03-30 16:37:36 -04004604 if (N > 0) {
Adrian Roos7052de52016-03-03 15:53:34 -08004605 big.setViewVisibility(R.id.actions_container, View.VISIBLE);
Daniel Sandler96fd7c12012-03-30 16:37:36 -04004606 big.setViewVisibility(R.id.actions, View.VISIBLE);
Adrian Roos487374f2017-01-11 15:48:14 -08004607 if (p.ambient) {
4608 big.setInt(R.id.actions, "setBackgroundColor", Color.TRANSPARENT);
Selim Cinek7b9605b2017-01-19 17:36:00 -08004609 } else if (isColorized()) {
4610 big.setInt(R.id.actions, "setBackgroundColor", getActionBarColor());
4611 } else {
4612 big.setInt(R.id.actions, "setBackgroundColor", mContext.getColor(
4613 R.color.notification_action_list));
Adrian Roos487374f2017-01-11 15:48:14 -08004614 }
Adrian Roosf852a422016-06-03 13:33:43 -07004615 big.setViewLayoutMarginBottomDimen(R.id.notification_action_list_margin_target,
4616 R.dimen.notification_action_list_height);
Daniel Sandler8680bf82012-05-15 16:52:52 -04004617 if (N>MAX_ACTION_BUTTONS) N=MAX_ACTION_BUTTONS;
Daniel Sandler96fd7c12012-03-30 16:37:36 -04004618 for (int i=0; i<N; i++) {
Adrian Roose458aa82015-12-08 16:17:19 -08004619 Action action = mActions.get(i);
4620 validRemoteInput |= hasValidRemoteInput(action);
4621
Selim Cinek06e9e1f2016-07-08 17:14:16 -07004622 final RemoteViews button = generateActionButton(action, emphazisedMode,
Adrian Roos487374f2017-01-11 15:48:14 -08004623 i % 2 != 0, p.ambient);
Daniel Sandler96fd7c12012-03-30 16:37:36 -04004624 big.addView(R.id.actions, button);
4625 }
Adrian Roos4c1fcc82016-03-31 14:39:39 -07004626 } else {
4627 big.setViewVisibility(R.id.actions_container, View.GONE);
Daniel Sandler96fd7c12012-03-30 16:37:36 -04004628 }
Adrian Roose458aa82015-12-08 16:17:19 -08004629
4630 CharSequence[] replyText = mN.extras.getCharSequenceArray(EXTRA_REMOTE_INPUT_HISTORY);
Adrian Roos487374f2017-01-11 15:48:14 -08004631 if (!p.ambient && validRemoteInput && replyText != null
Adrian Roose458aa82015-12-08 16:17:19 -08004632 && replyText.length > 0 && !TextUtils.isEmpty(replyText[0])) {
4633 big.setViewVisibility(R.id.notification_material_reply_container, View.VISIBLE);
Selim Cinek48f66b72017-08-18 16:17:51 -07004634 big.setTextViewText(R.id.notification_material_reply_text_1,
4635 processTextSpans(replyText[0]));
Selim Cinek7b9605b2017-01-19 17:36:00 -08004636 setTextViewColorSecondary(big, R.id.notification_material_reply_text_1);
Adrian Roose458aa82015-12-08 16:17:19 -08004637
4638 if (replyText.length > 1 && !TextUtils.isEmpty(replyText[1])) {
4639 big.setViewVisibility(R.id.notification_material_reply_text_2, View.VISIBLE);
Selim Cinek48f66b72017-08-18 16:17:51 -07004640 big.setTextViewText(R.id.notification_material_reply_text_2,
4641 processTextSpans(replyText[1]));
Selim Cinek7b9605b2017-01-19 17:36:00 -08004642 setTextViewColorSecondary(big, R.id.notification_material_reply_text_2);
Adrian Roose458aa82015-12-08 16:17:19 -08004643
4644 if (replyText.length > 2 && !TextUtils.isEmpty(replyText[2])) {
4645 big.setViewVisibility(
4646 R.id.notification_material_reply_text_3, View.VISIBLE);
Selim Cinek48f66b72017-08-18 16:17:51 -07004647 big.setTextViewText(R.id.notification_material_reply_text_3,
4648 processTextSpans(replyText[2]));
Selim Cinek7b9605b2017-01-19 17:36:00 -08004649 setTextViewColorSecondary(big, R.id.notification_material_reply_text_3);
Adrian Roose458aa82015-12-08 16:17:19 -08004650 }
4651 }
4652 }
4653
Daniel Sandler96fd7c12012-03-30 16:37:36 -04004654 return big;
4655 }
4656
Adrian Roose458aa82015-12-08 16:17:19 -08004657 private boolean hasValidRemoteInput(Action action) {
4658 if (TextUtils.isEmpty(action.title) || action.actionIntent == null) {
4659 // Weird actions
4660 return false;
4661 }
4662
4663 RemoteInput[] remoteInputs = action.getRemoteInputs();
4664 if (remoteInputs == null) {
4665 return false;
4666 }
4667
4668 for (RemoteInput r : remoteInputs) {
4669 CharSequence[] choices = r.getChoices();
4670 if (r.getAllowFreeFormInput() || (choices != null && choices.length != 0)) {
4671 return true;
4672 }
4673 }
4674 return false;
4675 }
4676
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004677 /**
4678 * Construct a RemoteViews for the final 1U notification layout. In order:
4679 * 1. Custom contentView from the caller
4680 * 2. Style's proposed content view
4681 * 3. Standard template view
4682 */
Julia Reynolds3b848122016-02-26 10:45:32 -05004683 public RemoteViews createContentView() {
Selim Cinek7d1009b2017-01-25 15:28:28 -08004684 return createContentView(false /* increasedheight */ );
4685 }
4686
4687 /**
4688 * Construct a RemoteViews for the smaller content view.
4689 *
4690 * @param increasedHeight true if this layout be created with an increased height. Some
4691 * styles may support showing more then just that basic 1U size
4692 * and the system may decide to render important notifications
4693 * slightly bigger even when collapsed.
4694 *
4695 * @hide
4696 */
4697 public RemoteViews createContentView(boolean increasedHeight) {
Selim Cineka7679b62017-05-10 16:33:25 -07004698 if (mN.contentView != null && useExistingRemoteView()) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004699 return mN.contentView;
4700 } else if (mStyle != null) {
Selim Cinek7d1009b2017-01-25 15:28:28 -08004701 final RemoteViews styleView = mStyle.makeContentView(increasedHeight);
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004702 if (styleView != null) {
4703 return styleView;
4704 }
Joe Onorato46439ce2010-11-19 13:56:21 -08004705 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004706 return applyStandardTemplate(getBaseLayoutResource());
Joe Onorato46439ce2010-11-19 13:56:21 -08004707 }
4708
Selim Cineka7679b62017-05-10 16:33:25 -07004709 private boolean useExistingRemoteView() {
4710 return mStyle == null || (!mStyle.displayCustomViewInline()
4711 && !mRebuildStyledRemoteViews);
4712 }
4713
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004714 /**
4715 * Construct a RemoteViews for the final big notification layout.
4716 */
Julia Reynolds3b848122016-02-26 10:45:32 -05004717 public RemoteViews createBigContentView() {
Selim Cinek850a8542015-11-11 11:48:36 -05004718 RemoteViews result = null;
Selim Cineka7679b62017-05-10 16:33:25 -07004719 if (mN.bigContentView != null && useExistingRemoteView()) {
Julia Reynolds089e3e42015-11-18 09:59:57 -05004720 return mN.bigContentView;
4721 } else if (mStyle != null) {
Selim Cinek850a8542015-11-11 11:48:36 -05004722 result = mStyle.makeBigContentView();
Selim Cinek90dcf6d2015-11-18 20:24:13 -08004723 hideLine1Text(result);
Selim Cinekcc10bfb2016-02-10 16:24:21 -08004724 } else if (mActions.size() != 0) {
4725 result = applyStandardTemplateWithActions(getBigBaseLayoutResource());
Selim Cinek850a8542015-11-11 11:48:36 -05004726 }
Selim Cinek6743c0b2017-01-18 18:24:01 -08004727 makeHeaderExpanded(result);
Selim Cinek850a8542015-11-11 11:48:36 -05004728 return result;
4729 }
Daniel Sandler96fd7c12012-03-30 16:37:36 -04004730
Selim Cinekeaa29ca2015-11-23 13:51:13 -08004731 /**
Selim Cinek414ad332017-02-24 19:06:12 -08004732 * Construct a RemoteViews for the final notification header only. This will not be
4733 * colorized.
Selim Cinekeaa29ca2015-11-23 13:51:13 -08004734 *
Adrian Roos6f6e1592017-05-02 16:22:53 -07004735 * @param ambient if true, generate the header for the ambient display layout.
Selim Cinekeaa29ca2015-11-23 13:51:13 -08004736 * @hide
4737 */
Adrian Roos6f6e1592017-05-02 16:22:53 -07004738 public RemoteViews makeNotificationHeader(boolean ambient) {
Selim Cinek414ad332017-02-24 19:06:12 -08004739 Boolean colorized = (Boolean) mN.extras.get(EXTRA_COLORIZED);
4740 mN.extras.putBoolean(EXTRA_COLORIZED, false);
Selim Cinekeaa29ca2015-11-23 13:51:13 -08004741 RemoteViews header = new BuilderRemoteViews(mContext.getApplicationInfo(),
Adrian Roos6f6e1592017-05-02 16:22:53 -07004742 ambient ? R.layout.notification_template_ambient_header
4743 : R.layout.notification_template_header);
Selim Cinekeaa29ca2015-11-23 13:51:13 -08004744 resetNotificationHeader(header);
Selim Cinekafeed292017-12-12 17:32:44 -08004745 bindNotificationHeader(header, ambient, null);
Selim Cinek414ad332017-02-24 19:06:12 -08004746 if (colorized != null) {
4747 mN.extras.putBoolean(EXTRA_COLORIZED, colorized);
4748 } else {
4749 mN.extras.remove(EXTRA_COLORIZED);
4750 }
Selim Cinekeaa29ca2015-11-23 13:51:13 -08004751 return header;
4752 }
4753
Adrian Roos487374f2017-01-11 15:48:14 -08004754 /**
4755 * Construct a RemoteViews for the ambient version of the notification.
4756 *
4757 * @hide
4758 */
4759 public RemoteViews makeAmbientNotification() {
4760 RemoteViews ambient = applyStandardTemplateWithActions(
4761 R.layout.notification_template_material_ambient,
Adrian Roos0bc3f6a2017-03-06 11:54:05 -08004762 mParams.reset().ambient(true).fillTextsFrom(this).hasProgress(false));
Adrian Roos487374f2017-01-11 15:48:14 -08004763 return ambient;
4764 }
4765
Selim Cinek29603462015-11-17 19:04:39 -08004766 private void hideLine1Text(RemoteViews result) {
Selim Cinekcc10bfb2016-02-10 16:24:21 -08004767 if (result != null) {
4768 result.setViewVisibility(R.id.text_line_1, View.GONE);
4769 }
Selim Cinek29603462015-11-17 19:04:39 -08004770 }
4771
Selim Cinek6743c0b2017-01-18 18:24:01 -08004772 /**
4773 * Adapt the Notification header if this view is used as an expanded view.
4774 *
4775 * @hide
4776 */
4777 public static void makeHeaderExpanded(RemoteViews result) {
Selim Cinekcc10bfb2016-02-10 16:24:21 -08004778 if (result != null) {
4779 result.setBoolean(R.id.notification_header, "setExpanded", true);
4780 }
Daniel Sandler96fd7c12012-03-30 16:37:36 -04004781 }
4782
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004783 /**
4784 * Construct a RemoteViews for the final heads-up notification layout.
Selim Cinek87ed69b2017-02-09 15:59:43 -08004785 *
4786 * @param increasedHeight true if this layout be created with an increased height. Some
4787 * styles may support showing more then just that basic 1U size
4788 * and the system may decide to render important notifications
4789 * slightly bigger even when collapsed.
4790 *
4791 * @hide
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004792 */
Selim Cinek87ed69b2017-02-09 15:59:43 -08004793 public RemoteViews createHeadsUpContentView(boolean increasedHeight) {
Selim Cineka7679b62017-05-10 16:33:25 -07004794 if (mN.headsUpContentView != null && useExistingRemoteView()) {
Julia Reynolds089e3e42015-11-18 09:59:57 -05004795 return mN.headsUpContentView;
4796 } else if (mStyle != null) {
Selim Cinek87ed69b2017-02-09 15:59:43 -08004797 final RemoteViews styleView = mStyle.makeHeadsUpContentView(increasedHeight);
4798 if (styleView != null) {
4799 return styleView;
4800 }
Julia Reynolds089e3e42015-11-18 09:59:57 -05004801 } else if (mActions.size() == 0) {
4802 return null;
4803 }
4804
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01004805 return applyStandardTemplateWithActions(getBigBaseLayoutResource());
Chris Wren8fd39ec2014-02-27 17:43:26 -05004806 }
4807
Selim Cinek624c02db2015-12-14 21:00:02 -08004808 /**
Selim Cinek87ed69b2017-02-09 15:59:43 -08004809 * Construct a RemoteViews for the final heads-up notification layout.
4810 */
4811 public RemoteViews createHeadsUpContentView() {
4812 return createHeadsUpContentView(false /* useIncreasedHeight */);
4813 }
4814
4815 /**
Selim Cinek624c02db2015-12-14 21:00:02 -08004816 * Construct a RemoteViews for the display in public contexts like on the lockscreen.
4817 *
4818 * @hide
4819 */
4820 public RemoteViews makePublicContentView() {
Adrian Roos1a1ecfc2017-04-17 11:17:59 -07004821 return makePublicView(false /* ambient */);
4822 }
4823
4824 /**
4825 * Construct a RemoteViews for the display in public contexts like on the lockscreen.
4826 *
4827 * @hide
4828 */
4829 public RemoteViews makePublicAmbientNotification() {
4830 return makePublicView(true /* ambient */);
4831 }
4832
4833 private RemoteViews makePublicView(boolean ambient) {
Selim Cinek624c02db2015-12-14 21:00:02 -08004834 if (mN.publicVersion != null) {
4835 final Builder builder = recoverBuilder(mContext, mN.publicVersion);
Adrian Roos1a1ecfc2017-04-17 11:17:59 -07004836 return ambient ? builder.makeAmbientNotification() : builder.createContentView();
Selim Cinek624c02db2015-12-14 21:00:02 -08004837 }
4838 Bundle savedBundle = mN.extras;
4839 Style style = mStyle;
4840 mStyle = null;
4841 Icon largeIcon = mN.mLargeIcon;
4842 mN.mLargeIcon = null;
Selim Cinek279fa862016-06-14 10:57:25 -07004843 Bitmap largeIconLegacy = mN.largeIcon;
4844 mN.largeIcon = null;
Adrian Roosb19b06b2017-05-02 18:54:40 -07004845 ArrayList<Action> actions = mActions;
4846 mActions = new ArrayList<>();
Selim Cinek624c02db2015-12-14 21:00:02 -08004847 Bundle publicExtras = new Bundle();
4848 publicExtras.putBoolean(EXTRA_SHOW_WHEN,
4849 savedBundle.getBoolean(EXTRA_SHOW_WHEN));
4850 publicExtras.putBoolean(EXTRA_SHOW_CHRONOMETER,
4851 savedBundle.getBoolean(EXTRA_SHOW_CHRONOMETER));
Adrian Roos96b7e202016-05-17 13:50:38 -07004852 publicExtras.putBoolean(EXTRA_CHRONOMETER_COUNT_DOWN,
4853 savedBundle.getBoolean(EXTRA_CHRONOMETER_COUNT_DOWN));
Selim Cinek624c02db2015-12-14 21:00:02 -08004854 mN.extras = publicExtras;
Selim Cinek499c20f2017-07-20 14:06:09 -07004855 RemoteViews view;
4856 if (ambient) {
4857 publicExtras.putCharSequence(EXTRA_TITLE,
4858 mContext.getString(com.android.internal.R.string.notification_hidden_text));
4859 view = makeAmbientNotification();
4860 } else{
4861 view = makeNotificationHeader(false /* ambient */);
4862 view.setBoolean(R.id.notification_header, "setExpandOnlyOnButton", true);
4863 }
Selim Cinek624c02db2015-12-14 21:00:02 -08004864 mN.extras = savedBundle;
4865 mN.mLargeIcon = largeIcon;
Selim Cinek279fa862016-06-14 10:57:25 -07004866 mN.largeIcon = largeIconLegacy;
Adrian Roosb19b06b2017-05-02 18:54:40 -07004867 mActions = actions;
Selim Cinek624c02db2015-12-14 21:00:02 -08004868 mStyle = style;
Adrian Roos1a1ecfc2017-04-17 11:17:59 -07004869 return view;
Selim Cinek624c02db2015-12-14 21:00:02 -08004870 }
4871
Selim Cinek6743c0b2017-01-18 18:24:01 -08004872 /**
4873 * Construct a content view for the display when low - priority
4874 *
4875 * @param useRegularSubtext uses the normal subtext set if there is one available. Otherwise
4876 * a new subtext is created consisting of the content of the
4877 * notification.
4878 * @hide
4879 */
4880 public RemoteViews makeLowPriorityContentView(boolean useRegularSubtext) {
4881 int color = mN.color;
4882 mN.color = COLOR_DEFAULT;
4883 CharSequence summary = mN.extras.getCharSequence(EXTRA_SUB_TEXT);
4884 if (!useRegularSubtext || TextUtils.isEmpty(summary)) {
4885 CharSequence newSummary = createSummaryText();
4886 if (!TextUtils.isEmpty(newSummary)) {
4887 mN.extras.putCharSequence(EXTRA_SUB_TEXT, newSummary);
4888 }
4889 }
Selim Cinek875ba9b2017-02-13 16:20:17 -08004890
Adrian Roos6f6e1592017-05-02 16:22:53 -07004891 RemoteViews header = makeNotificationHeader(false /* ambient */);
Selim Cinek1b554392017-02-28 17:22:49 -08004892 header.setBoolean(R.id.notification_header, "setAcceptAllTouches", true);
Selim Cinek6743c0b2017-01-18 18:24:01 -08004893 if (summary != null) {
4894 mN.extras.putCharSequence(EXTRA_SUB_TEXT, summary);
4895 } else {
4896 mN.extras.remove(EXTRA_SUB_TEXT);
4897 }
4898 mN.color = color;
4899 return header;
4900 }
Selim Cinek624c02db2015-12-14 21:00:02 -08004901
Selim Cinek6743c0b2017-01-18 18:24:01 -08004902 private CharSequence createSummaryText() {
4903 CharSequence titleText = mN.extras.getCharSequence(Notification.EXTRA_TITLE);
4904 if (USE_ONLY_TITLE_IN_LOW_PRIORITY_SUMMARY) {
4905 return titleText;
4906 }
4907 SpannableStringBuilder summary = new SpannableStringBuilder();
4908 if (titleText == null) {
4909 titleText = mN.extras.getCharSequence(Notification.EXTRA_TITLE_BIG);
4910 }
4911 BidiFormatter bidi = BidiFormatter.getInstance();
4912 if (titleText != null) {
4913 summary.append(bidi.unicodeWrap(titleText));
4914 }
4915 CharSequence contentText = mN.extras.getCharSequence(Notification.EXTRA_TEXT);
4916 if (titleText != null && contentText != null) {
4917 summary.append(bidi.unicodeWrap(mContext.getText(
4918 R.string.notification_header_divider_symbol_with_spaces)));
4919 }
4920 if (contentText != null) {
4921 summary.append(bidi.unicodeWrap(contentText));
4922 }
4923 return summary;
4924 }
Chris Wren8fd39ec2014-02-27 17:43:26 -05004925
Selim Cinek06e9e1f2016-07-08 17:14:16 -07004926 private RemoteViews generateActionButton(Action action, boolean emphazisedMode,
Adrian Roos487374f2017-01-11 15:48:14 -08004927 boolean oddAction, boolean ambient) {
Daniel Sandler8680bf82012-05-15 16:52:52 -04004928 final boolean tombstone = (action.actionIntent == null);
Selim Cinekf33b1112015-07-15 17:45:11 -07004929 RemoteViews button = new BuilderRemoteViews(mContext.getApplicationInfo(),
Selim Cinek06e9e1f2016-07-08 17:14:16 -07004930 emphazisedMode ? getEmphasizedActionLayoutResource()
4931 : tombstone ? getActionTombstoneLayoutResource()
4932 : getActionLayoutResource());
Daniel Sandler8680bf82012-05-15 16:52:52 -04004933 if (!tombstone) {
Daniel Sandlere5518842012-05-10 16:20:40 -04004934 button.setOnClickPendingIntent(R.id.action0, action.actionIntent);
Daniel Sandlere5518842012-05-10 16:20:40 -04004935 }
Daniel Sandler96fd7c12012-03-30 16:37:36 -04004936 button.setContentDescription(R.id.action0, action.title);
Adrian Roosfe84e1f2015-11-04 15:55:39 -08004937 if (action.mRemoteInputs != null) {
4938 button.setRemoteInputs(R.id.action0, action.mRemoteInputs);
4939 }
Selim Cinek7b9605b2017-01-19 17:36:00 -08004940 // TODO: handle emphasized mode / actions right
Selim Cinek06e9e1f2016-07-08 17:14:16 -07004941 if (emphazisedMode) {
Selim Cinek981962e2016-07-20 20:41:58 -07004942 // change the background bgColor
Selim Cinek622c64a2017-04-17 17:10:05 -07004943 int bgColor;
4944 if (isColorized()) {
4945 bgColor = oddAction ? getActionBarColor() : getActionBarColorDeEmphasized();
4946 } else {
4947 bgColor = mContext.getColor(oddAction ? R.color.notification_action_list
4948 : R.color.notification_action_list_dark);
4949 }
Sunny Goyal5b153922017-09-21 21:00:36 -07004950 button.setDrawableTint(R.id.button_holder, true,
4951 bgColor, PorterDuff.Mode.SRC_ATOP);
Selim Cinek981962e2016-07-20 20:41:58 -07004952 CharSequence title = action.title;
4953 ColorStateList[] outResultColor = null;
4954 if (isLegacy()) {
Selim Cinek87c31532017-08-18 18:53:44 -07004955 title = NotificationColorUtil.clearColorSpans(title);
Selim Cinek981962e2016-07-20 20:41:58 -07004956 } else {
4957 outResultColor = new ColorStateList[1];
4958 title = ensureColorSpanContrast(title, bgColor, outResultColor);
4959 }
Selim Cinek48f66b72017-08-18 16:17:51 -07004960 button.setTextViewText(R.id.action0, processTextSpans(title));
Selim Cinek7b9605b2017-01-19 17:36:00 -08004961 setTextViewColorPrimary(button, R.id.action0);
Selim Cinek981962e2016-07-20 20:41:58 -07004962 if (outResultColor != null && outResultColor[0] != null) {
4963 // We need to set the text color as well since changing a text to uppercase
4964 // clears its spans.
4965 button.setTextColor(R.id.action0, outResultColor[0]);
Anthony Chenad4d1582017-04-10 16:07:58 -07004966 } else if (mN.color != COLOR_DEFAULT && !isColorized() && mTintActionButtons) {
Selim Cinek981962e2016-07-20 20:41:58 -07004967 button.setTextColor(R.id.action0,resolveContrastColor());
4968 }
Selim Cinek06e9e1f2016-07-08 17:14:16 -07004969 } else {
Selim Cinek48f66b72017-08-18 16:17:51 -07004970 button.setTextViewText(R.id.action0, processTextSpans(
4971 processLegacyText(action.title)));
Adrian Roos72171622017-01-27 10:32:06 -08004972 if (isColorized() && !ambient) {
Selim Cinek7b9605b2017-01-19 17:36:00 -08004973 setTextViewColorPrimary(button, R.id.action0);
Anthony Chenad4d1582017-04-10 16:07:58 -07004974 } else if (mN.color != COLOR_DEFAULT && mTintActionButtons) {
Adrian Roos487374f2017-01-11 15:48:14 -08004975 button.setTextColor(R.id.action0,
4976 ambient ? resolveAmbientColor() : resolveContrastColor());
Selim Cinek06e9e1f2016-07-08 17:14:16 -07004977 }
Adrian Roosfe84e1f2015-11-04 15:55:39 -08004978 }
Daniel Sandler96fd7c12012-03-30 16:37:36 -04004979 return button;
4980 }
4981
Joe Onoratocb109a02011-01-18 17:57:41 -08004982 /**
Selim Cinek981962e2016-07-20 20:41:58 -07004983 * Ensures contrast on color spans against a background color. also returns the color of the
4984 * text if a span was found that spans over the whole text.
4985 *
4986 * @param charSequence the charSequence on which the spans are
4987 * @param background the background color to ensure the contrast against
4988 * @param outResultColor an array in which a color will be returned as the first element if
4989 * there exists a full length color span.
4990 * @return the contrasted charSequence
4991 */
4992 private CharSequence ensureColorSpanContrast(CharSequence charSequence, int background,
4993 ColorStateList[] outResultColor) {
4994 if (charSequence instanceof Spanned) {
4995 Spanned ss = (Spanned) charSequence;
4996 Object[] spans = ss.getSpans(0, ss.length(), Object.class);
4997 SpannableStringBuilder builder = new SpannableStringBuilder(ss.toString());
4998 for (Object span : spans) {
4999 Object resultSpan = span;
5000 int spanStart = ss.getSpanStart(span);
5001 int spanEnd = ss.getSpanEnd(span);
5002 boolean fullLength = (spanEnd - spanStart) == charSequence.length();
5003 if (resultSpan instanceof CharacterStyle) {
5004 resultSpan = ((CharacterStyle) span).getUnderlying();
5005 }
5006 if (resultSpan instanceof TextAppearanceSpan) {
5007 TextAppearanceSpan originalSpan = (TextAppearanceSpan) resultSpan;
5008 ColorStateList textColor = originalSpan.getTextColor();
5009 if (textColor != null) {
5010 int[] colors = textColor.getColors();
5011 int[] newColors = new int[colors.length];
5012 for (int i = 0; i < newColors.length; i++) {
5013 newColors[i] = NotificationColorUtil.ensureLargeTextContrast(
Anthony Chenad4d1582017-04-10 16:07:58 -07005014 colors[i], background, mInNightMode);
Selim Cinek981962e2016-07-20 20:41:58 -07005015 }
5016 textColor = new ColorStateList(textColor.getStates().clone(),
5017 newColors);
5018 resultSpan = new TextAppearanceSpan(
5019 originalSpan.getFamily(),
5020 originalSpan.getTextStyle(),
5021 originalSpan.getTextSize(),
5022 textColor,
5023 originalSpan.getLinkTextColor());
5024 if (fullLength) {
5025 outResultColor[0] = new ColorStateList(
5026 textColor.getStates().clone(), newColors);
5027 }
5028 }
5029 } else if (resultSpan instanceof ForegroundColorSpan) {
5030 ForegroundColorSpan originalSpan = (ForegroundColorSpan) resultSpan;
5031 int foregroundColor = originalSpan.getForegroundColor();
5032 foregroundColor = NotificationColorUtil.ensureLargeTextContrast(
Anthony Chenad4d1582017-04-10 16:07:58 -07005033 foregroundColor, background, mInNightMode);
Selim Cinek981962e2016-07-20 20:41:58 -07005034 resultSpan = new ForegroundColorSpan(foregroundColor);
5035 if (fullLength) {
5036 outResultColor[0] = ColorStateList.valueOf(foregroundColor);
5037 }
5038 } else {
5039 resultSpan = span;
5040 }
5041
5042 builder.setSpan(resultSpan, spanStart, spanEnd, ss.getSpanFlags(span));
5043 }
5044 return builder;
5045 }
5046 return charSequence;
5047 }
5048
5049 /**
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01005050 * @return Whether we are currently building a notification from a legacy (an app that
Alan Viverette3cb07a462014-06-06 14:19:53 -07005051 * doesn't create material notifications by itself) app.
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01005052 */
5053 private boolean isLegacy() {
Selim Cinek7b9605b2017-01-19 17:36:00 -08005054 if (!mIsLegacyInitialized) {
5055 mIsLegacy = mContext.getApplicationInfo().targetSdkVersion
5056 < Build.VERSION_CODES.LOLLIPOP;
5057 mIsLegacyInitialized = true;
5058 }
5059 return mIsLegacy;
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01005060 }
5061
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01005062 private CharSequence processLegacyText(CharSequence charSequence) {
Adrian Roos0bc3f6a2017-03-06 11:54:05 -08005063 return processLegacyText(charSequence, false /* ambient */);
5064 }
5065
5066 private CharSequence processLegacyText(CharSequence charSequence, boolean ambient) {
5067 boolean isAlreadyLightText = isLegacy() || textColorsNeedInversion();
5068 boolean wantLightText = ambient;
5069 if (isAlreadyLightText != wantLightText) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005070 return getColorUtil().invertCharSequenceColors(charSequence);
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01005071 } else {
5072 return charSequence;
5073 }
5074 }
5075
Dan Sandler26e81cf2014-05-06 10:01:27 -04005076 /**
Selim Cinek65b2e7c2015-10-26 14:11:31 -07005077 * Apply any necessariy colors to the small icon
Dan Sandler26e81cf2014-05-06 10:01:27 -04005078 */
Adrian Roos487374f2017-01-11 15:48:14 -08005079 private void processSmallIconColor(Icon smallIcon, RemoteViews contentView,
5080 boolean ambient) {
Selim Cinekea4bef72015-12-02 15:51:10 -08005081 boolean colorable = !isLegacy() || getColorUtil().isGrayscaleIcon(mContext, smallIcon);
Selim Cinek99104832017-01-25 14:47:33 -08005082 int color = ambient ? resolveAmbientColor() : getPrimaryHighlightColor();
Selim Cinekea4bef72015-12-02 15:51:10 -08005083 if (colorable) {
Sunny Goyal5b153922017-09-21 21:00:36 -07005084 contentView.setDrawableTint(R.id.icon, false, color,
5085 PorterDuff.Mode.SRC_ATOP);
Selim Cinekea4bef72015-12-02 15:51:10 -08005086
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01005087 }
Selim Cinekea4bef72015-12-02 15:51:10 -08005088 contentView.setInt(R.id.notification_header, "setOriginalIconColor",
Adrian Roos487374f2017-01-11 15:48:14 -08005089 colorable ? color : NotificationHeaderView.NO_COLOR);
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01005090 }
5091
Dan Sandler26e81cf2014-05-06 10:01:27 -04005092 /**
Selim Cinek65b2e7c2015-10-26 14:11:31 -07005093 * Make the largeIcon dark if it's a fake smallIcon (that is,
Dan Sandler26e81cf2014-05-06 10:01:27 -04005094 * if it's grayscale).
5095 */
5096 // TODO: also check bounds, transparency, that sort of thing.
Dan Sandlerd63f9322015-05-06 15:18:49 -04005097 private void processLargeLegacyIcon(Icon largeIcon, RemoteViews contentView) {
5098 if (largeIcon != null && isLegacy()
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005099 && getColorUtil().isGrayscaleIcon(mContext, largeIcon)) {
Selim Cinek65b2e7c2015-10-26 14:11:31 -07005100 // resolve color will fall back to the default when legacy
Sunny Goyal5b153922017-09-21 21:00:36 -07005101 contentView.setDrawableTint(R.id.icon, false, resolveContrastColor(),
5102 PorterDuff.Mode.SRC_ATOP);
Jorim Jaggi92df1f22014-12-16 19:44:41 +01005103 }
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01005104 }
5105
Julia Reynolds10ee1fc2015-11-09 11:04:55 -05005106 private void sanitizeColor() {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005107 if (mN.color != COLOR_DEFAULT) {
5108 mN.color |= 0xFF000000; // no alpha for custom colors
Jorim Jaggi74419312014-06-10 20:57:21 +02005109 }
Jorim Jaggi74419312014-06-10 20:57:21 +02005110 }
5111
Adrian Roos4ff3b122016-02-01 12:26:13 -08005112 int resolveContrastColor() {
5113 if (mCachedContrastColorIsFor == mN.color && mCachedContrastColor != COLOR_INVALID) {
5114 return mCachedContrastColor;
Dan Sandler26e81cf2014-05-06 10:01:27 -04005115 }
Adrian Roos4ff3b122016-02-01 12:26:13 -08005116
Selim Cinekac5f0272017-05-02 16:05:41 -07005117 int color;
5118 int background = mBackgroundColorHint;
5119 if (mBackgroundColorHint == COLOR_INVALID) {
5120 background = mContext.getColor(
5121 com.android.internal.R.color.notification_material_background_color);
5122 }
5123 if (mN.color == COLOR_DEFAULT) {
5124 ensureColors();
5125 color = mSecondaryTextColor;
5126 } else {
5127 color = NotificationColorUtil.resolveContrastColor(mContext, mN.color,
Anthony Chenad4d1582017-04-10 16:07:58 -07005128 background, mInNightMode);
Selim Cinekac5f0272017-05-02 16:05:41 -07005129 }
5130 if (Color.alpha(color) < 255) {
5131 // alpha doesn't go well for color filters, so let's blend it manually
5132 color = NotificationColorUtil.compositeColors(color, background);
5133 }
Adrian Roos4ff3b122016-02-01 12:26:13 -08005134 mCachedContrastColorIsFor = mN.color;
Selim Cinekac5f0272017-05-02 16:05:41 -07005135 return mCachedContrastColor = color;
Dan Sandler26e81cf2014-05-06 10:01:27 -04005136 }
5137
Adrian Roos487374f2017-01-11 15:48:14 -08005138 int resolveAmbientColor() {
5139 if (mCachedAmbientColorIsFor == mN.color && mCachedAmbientColorIsFor != COLOR_INVALID) {
5140 return mCachedAmbientColor;
5141 }
5142 final int contrasted = NotificationColorUtil.resolveAmbientColor(mContext, mN.color);
5143
5144 mCachedAmbientColorIsFor = mN.color;
5145 return mCachedAmbientColor = contrasted;
5146 }
5147
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01005148 /**
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005149 * Apply the unstyled operations and return a new {@link Notification} object.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04005150 * @hide
Joe Onoratocb109a02011-01-18 17:57:41 -08005151 */
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04005152 public Notification buildUnstyled() {
Daniel Sandlera0a938c2012-03-15 08:42:37 -04005153 if (mActions.size() > 0) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005154 mN.actions = new Action[mActions.size()];
5155 mActions.toArray(mN.actions);
Daniel Sandlera0a938c2012-03-15 08:42:37 -04005156 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005157 if (!mPersonList.isEmpty()) {
Selim Cineke7238dd2017-12-14 17:48:32 -08005158 mN.extras.putParcelableArrayList(EXTRA_PEOPLE_LIST, mPersonList);
Dan Sandler0bf2ed82013-12-21 23:33:41 -06005159 }
Selim Cinek247fa012016-02-18 09:50:48 -08005160 if (mN.bigContentView != null || mN.contentView != null
5161 || mN.headsUpContentView != null) {
5162 mN.extras.putBoolean(EXTRA_CONTAINS_CUSTOM_VIEW, true);
5163 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005164 return mN;
Joe Onorato46439ce2010-11-19 13:56:21 -08005165 }
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005166
Julia Reynolds3b848122016-02-26 10:45:32 -05005167 /**
5168 * Creates a Builder from an existing notification so further changes can be made.
5169 * @param context The context for your application / activity.
5170 * @param n The notification to create a Builder from.
5171 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005172 public static Notification.Builder recoverBuilder(Context context, Notification n) {
Christoph Studer4600f9b2014-07-22 22:44:43 +02005173 // Re-create notification context so we can access app resources.
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005174 ApplicationInfo applicationInfo = n.extras.getParcelable(
5175 EXTRA_BUILDER_APPLICATION_INFO);
Christoph Studer4600f9b2014-07-22 22:44:43 +02005176 Context builderContext;
Julia Reynoldsda303542015-11-23 14:00:20 -05005177 if (applicationInfo != null) {
5178 try {
5179 builderContext = context.createApplicationContext(applicationInfo,
5180 Context.CONTEXT_RESTRICTED);
5181 } catch (NameNotFoundException e) {
5182 Log.e(TAG, "ApplicationInfo " + applicationInfo + " not found");
5183 builderContext = context; // try with our context
5184 }
5185 } else {
5186 builderContext = context; // try with given context
Christoph Studer4600f9b2014-07-22 22:44:43 +02005187 }
5188
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005189 return new Builder(builderContext, n);
Christoph Studer4600f9b2014-07-22 22:44:43 +02005190 }
5191
Daniel Sandlerf45564e2013-04-15 15:05:08 -04005192 /**
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005193 * @deprecated Use {@link #build()} instead.
5194 */
5195 @Deprecated
5196 public Notification getNotification() {
5197 return build();
5198 }
5199
5200 /**
5201 * Combine all of the options that have been set and return a new {@link Notification}
5202 * object.
5203 */
5204 public Notification build() {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005205 // first, add any extras from the calling code
5206 if (mUserExtras != null) {
5207 mN.extras = getAllExtras();
Jorim Jaggia0d58ae2015-06-03 11:48:13 -07005208 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005209
Selim Cinekb85f36fd2016-04-20 18:46:36 -07005210 mN.creationTime = System.currentTimeMillis();
5211
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005212 // lazy stuff from mContext; see comment in Builder(Context, Notification)
Julia Reynoldsda303542015-11-23 14:00:20 -05005213 Notification.addFieldsFromContext(mContext, mN);
Christoph Studer943aa672014-08-03 20:31:16 +02005214
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005215 buildUnstyled();
Daniel Sandlerf45564e2013-04-15 15:05:08 -04005216
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005217 if (mStyle != null) {
Selim Cinekd0426622017-07-11 13:19:59 +02005218 mStyle.reduceImageSizes(mContext);
5219 mStyle.purgeResources();
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005220 mStyle.buildStyled(mN);
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005221 }
Selim Cinekd0426622017-07-11 13:19:59 +02005222 mN.reduceImageSizes(mContext);
5223
Adrian Roos5081c0d2016-02-26 16:04:19 -08005224 if (mContext.getApplicationInfo().targetSdkVersion < Build.VERSION_CODES.N
Selim Cineka7679b62017-05-10 16:33:25 -07005225 && (useExistingRemoteView())) {
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005226 if (mN.contentView == null) {
Julia Reynolds3b848122016-02-26 10:45:32 -05005227 mN.contentView = createContentView();
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005228 mN.extras.putInt(EXTRA_REBUILD_CONTENT_VIEW_ACTION_COUNT,
5229 mN.contentView.getSequenceNumber());
5230 }
5231 if (mN.bigContentView == null) {
Julia Reynolds3b848122016-02-26 10:45:32 -05005232 mN.bigContentView = createBigContentView();
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005233 if (mN.bigContentView != null) {
5234 mN.extras.putInt(EXTRA_REBUILD_BIG_CONTENT_VIEW_ACTION_COUNT,
5235 mN.bigContentView.getSequenceNumber());
5236 }
5237 }
5238 if (mN.headsUpContentView == null) {
Julia Reynolds3b848122016-02-26 10:45:32 -05005239 mN.headsUpContentView = createHeadsUpContentView();
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005240 if (mN.headsUpContentView != null) {
5241 mN.extras.putInt(EXTRA_REBUILD_HEADS_UP_CONTENT_VIEW_ACTION_COUNT,
5242 mN.headsUpContentView.getSequenceNumber());
5243 }
5244 }
5245 }
5246
Julia Reynolds4c0c2022016-02-02 15:11:59 -05005247 if ((mN.defaults & DEFAULT_LIGHTS) != 0) {
5248 mN.flags |= FLAG_SHOW_LIGHTS;
5249 }
5250
Adrian Roosfb921842017-10-26 14:49:56 +02005251 mN.allPendingIntents = null;
5252
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005253 return mN;
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005254 }
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05005255
5256 /**
5257 * Apply this Builder to an existing {@link Notification} object.
5258 *
5259 * @hide
5260 */
5261 public Notification buildInto(Notification n) {
Daniel Sandler1a497d32013-04-18 14:52:45 -04005262 build().cloneInto(n, true);
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05005263 return n;
5264 }
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005265
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005266 /**
Adrian Roos184bfe022016-03-03 13:41:44 -08005267 * Removes RemoteViews that were created for compatibility from {@param n}, if they did not
Julia Reynolds8a3b4592017-06-26 17:15:14 -04005268 * change. Also removes extenders on low ram devices, as
5269 * {@link android.service.notification.NotificationListenerService} services are disabled.
Adrian Roos184bfe022016-03-03 13:41:44 -08005270 *
5271 * @return {@param n}, if no stripping is needed, otherwise a stripped clone of {@param n}.
5272 *
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005273 * @hide
5274 */
Julia Reynolds8a3b4592017-06-26 17:15:14 -04005275 public static Notification maybeCloneStrippedForDelivery(Notification n, boolean isLowRam) {
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005276 String templateClass = n.extras.getString(EXTRA_TEMPLATE);
Adrian Roos184bfe022016-03-03 13:41:44 -08005277
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005278 // Only strip views for known Styles because we won't know how to
5279 // re-create them otherwise.
Julia Reynolds8a3b4592017-06-26 17:15:14 -04005280 if (!isLowRam
5281 && !TextUtils.isEmpty(templateClass)
Adrian Roos184bfe022016-03-03 13:41:44 -08005282 && getNotificationStyleClass(templateClass) == null) {
5283 return n;
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005284 }
Adrian Roos184bfe022016-03-03 13:41:44 -08005285
5286 // Only strip unmodified BuilderRemoteViews.
5287 boolean stripContentView = n.contentView instanceof BuilderRemoteViews &&
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005288 n.extras.getInt(EXTRA_REBUILD_CONTENT_VIEW_ACTION_COUNT, -1) ==
Adrian Roos184bfe022016-03-03 13:41:44 -08005289 n.contentView.getSequenceNumber();
5290 boolean stripBigContentView = n.bigContentView instanceof BuilderRemoteViews &&
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005291 n.extras.getInt(EXTRA_REBUILD_BIG_CONTENT_VIEW_ACTION_COUNT, -1) ==
Adrian Roos184bfe022016-03-03 13:41:44 -08005292 n.bigContentView.getSequenceNumber();
5293 boolean stripHeadsUpContentView = n.headsUpContentView instanceof BuilderRemoteViews &&
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005294 n.extras.getInt(EXTRA_REBUILD_HEADS_UP_CONTENT_VIEW_ACTION_COUNT, -1) ==
Adrian Roos184bfe022016-03-03 13:41:44 -08005295 n.headsUpContentView.getSequenceNumber();
5296
5297 // Nothing to do here, no need to clone.
Julia Reynolds8a3b4592017-06-26 17:15:14 -04005298 if (!isLowRam
5299 && !stripContentView && !stripBigContentView && !stripHeadsUpContentView) {
Adrian Roos184bfe022016-03-03 13:41:44 -08005300 return n;
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005301 }
Adrian Roos184bfe022016-03-03 13:41:44 -08005302
5303 Notification clone = n.clone();
5304 if (stripContentView) {
5305 clone.contentView = null;
5306 clone.extras.remove(EXTRA_REBUILD_CONTENT_VIEW_ACTION_COUNT);
5307 }
5308 if (stripBigContentView) {
5309 clone.bigContentView = null;
5310 clone.extras.remove(EXTRA_REBUILD_BIG_CONTENT_VIEW_ACTION_COUNT);
5311 }
5312 if (stripHeadsUpContentView) {
5313 clone.headsUpContentView = null;
5314 clone.extras.remove(EXTRA_REBUILD_HEADS_UP_CONTENT_VIEW_ACTION_COUNT);
5315 }
Julia Reynolds8a3b4592017-06-26 17:15:14 -04005316 if (isLowRam) {
5317 clone.extras.remove(Notification.TvExtender.EXTRA_TV_EXTENDER);
5318 clone.extras.remove(WearableExtender.EXTRA_WEARABLE_EXTENSIONS);
5319 clone.extras.remove(CarExtender.EXTRA_CAR_EXTENDER);
5320 }
Adrian Roos184bfe022016-03-03 13:41:44 -08005321 return clone;
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005322 }
5323
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005324 private int getBaseLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07005325 return R.layout.notification_template_material_base;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005326 }
5327
5328 private int getBigBaseLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07005329 return R.layout.notification_template_material_big_base;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005330 }
5331
5332 private int getBigPictureLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07005333 return R.layout.notification_template_material_big_picture;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005334 }
5335
5336 private int getBigTextLayoutResource() {
Jorim Jaggi445d3c02014-08-19 22:33:42 +02005337 return R.layout.notification_template_material_big_text;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005338 }
5339
5340 private int getInboxLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07005341 return R.layout.notification_template_material_inbox;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005342 }
5343
Adrian Roosc1a80b02016-04-05 14:54:55 -07005344 private int getMessagingLayoutResource() {
5345 return R.layout.notification_template_material_messaging;
5346 }
5347
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005348 private int getActionLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07005349 return R.layout.notification_material_action;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005350 }
5351
Selim Cinek06e9e1f2016-07-08 17:14:16 -07005352 private int getEmphasizedActionLayoutResource() {
5353 return R.layout.notification_material_action_emphasized;
5354 }
5355
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005356 private int getActionTombstoneLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07005357 return R.layout.notification_material_action_tombstone;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005358 }
Selim Cinek7b9605b2017-01-19 17:36:00 -08005359
5360 private int getBackgroundColor() {
5361 if (isColorized()) {
Selim Cinek5fb73f82017-04-20 16:55:38 -07005362 return mBackgroundColor != COLOR_INVALID ? mBackgroundColor : mN.color;
Selim Cinek7b9605b2017-01-19 17:36:00 -08005363 } else {
Selim Cinekac5f0272017-05-02 16:05:41 -07005364 return mBackgroundColorHint != COLOR_INVALID ? mBackgroundColorHint
5365 : COLOR_DEFAULT;
Selim Cinek7b9605b2017-01-19 17:36:00 -08005366 }
5367 }
5368
5369 private boolean isColorized() {
5370 return mN.isColorized();
5371 }
Selim Cinek99104832017-01-25 14:47:33 -08005372
Anthony Chenad4d1582017-04-10 16:07:58 -07005373 private boolean shouldTintActionButtons() {
5374 return mTintActionButtons;
5375 }
5376
Selim Cinek99104832017-01-25 14:47:33 -08005377 private boolean textColorsNeedInversion() {
5378 if (mStyle == null || !MediaStyle.class.equals(mStyle.getClass())) {
5379 return false;
5380 }
5381 int targetSdkVersion = mContext.getApplicationInfo().targetSdkVersion;
5382 return targetSdkVersion > Build.VERSION_CODES.M
5383 && targetSdkVersion < Build.VERSION_CODES.O;
5384 }
Selim Cinek5fb73f82017-04-20 16:55:38 -07005385
5386 /**
5387 * Set a color palette to be used as the background and textColors
5388 *
5389 * @param backgroundColor the color to be used as the background
5390 * @param foregroundColor the color to be used as the foreground
5391 *
5392 * @hide
5393 */
5394 public void setColorPalette(int backgroundColor, int foregroundColor) {
5395 mBackgroundColor = backgroundColor;
5396 mForegroundColor = foregroundColor;
5397 mTextColorsAreForBackground = COLOR_INVALID;
5398 ensureColors();
5399 }
Selim Cinekac5f0272017-05-02 16:05:41 -07005400
5401 /**
5402 * Sets the background color for this notification to be a different one then the default.
5403 * This is mainly used to calculate contrast and won't necessarily be applied to the
5404 * background.
5405 *
5406 * @hide
5407 */
5408 public void setBackgroundColorHint(int backgroundColor) {
5409 mBackgroundColorHint = backgroundColor;
5410 }
Selim Cineka7679b62017-05-10 16:33:25 -07005411
5412
5413 /**
5414 * Forces all styled remoteViews to be built from scratch and not use any cached
5415 * RemoteViews.
5416 * This is needed for legacy apps that are baking in their remoteviews into the
5417 * notification.
5418 *
5419 * @hide
5420 */
5421 public void setRebuildStyledRemoteViews(boolean rebuild) {
5422 mRebuildStyledRemoteViews = rebuild;
5423 }
Selim Cinek7b9605b2017-01-19 17:36:00 -08005424 }
5425
5426 /**
Selim Cinekd0426622017-07-11 13:19:59 +02005427 * Reduces the image sizes to conform to a maximum allowed size. This also processes all custom
5428 * remote views.
5429 *
5430 * @hide
5431 */
5432 void reduceImageSizes(Context context) {
5433 if (extras.getBoolean(EXTRA_REDUCED_IMAGES)) {
5434 return;
5435 }
Selim Cineka8cb1262017-08-15 16:53:44 -07005436 boolean isLowRam = ActivityManager.isLowRamDeviceStatic();
Selim Cinekd0426622017-07-11 13:19:59 +02005437 if (mLargeIcon != null || largeIcon != null) {
5438 Resources resources = context.getResources();
5439 Class<? extends Style> style = getNotificationStyle();
Selim Cineka8cb1262017-08-15 16:53:44 -07005440 int maxWidth = resources.getDimensionPixelSize(isLowRam
5441 ? R.dimen.notification_right_icon_size_low_ram
5442 : R.dimen.notification_right_icon_size);
Selim Cinekd0426622017-07-11 13:19:59 +02005443 int maxHeight = maxWidth;
5444 if (MediaStyle.class.equals(style)
5445 || DecoratedMediaCustomViewStyle.class.equals(style)) {
Selim Cineka8cb1262017-08-15 16:53:44 -07005446 maxHeight = resources.getDimensionPixelSize(isLowRam
5447 ? R.dimen.notification_media_image_max_height_low_ram
5448 : R.dimen.notification_media_image_max_height);
5449 maxWidth = resources.getDimensionPixelSize(isLowRam
5450 ? R.dimen.notification_media_image_max_width_low_ram
5451 : R.dimen.notification_media_image_max_width);
Selim Cinekd0426622017-07-11 13:19:59 +02005452 }
5453 if (mLargeIcon != null) {
5454 mLargeIcon.scaleDownIfNecessary(maxWidth, maxHeight);
5455 }
5456 if (largeIcon != null) {
5457 largeIcon = Icon.scaleDownIfNecessary(largeIcon, maxWidth, maxHeight);
5458 }
5459 }
Selim Cineka8cb1262017-08-15 16:53:44 -07005460 reduceImageSizesForRemoteView(contentView, context, isLowRam);
5461 reduceImageSizesForRemoteView(headsUpContentView, context, isLowRam);
5462 reduceImageSizesForRemoteView(bigContentView, context, isLowRam);
Selim Cinekd0426622017-07-11 13:19:59 +02005463 extras.putBoolean(EXTRA_REDUCED_IMAGES, true);
5464 }
5465
Selim Cineka8cb1262017-08-15 16:53:44 -07005466 private void reduceImageSizesForRemoteView(RemoteViews remoteView, Context context,
5467 boolean isLowRam) {
Selim Cinekd0426622017-07-11 13:19:59 +02005468 if (remoteView != null) {
5469 Resources resources = context.getResources();
Selim Cineka8cb1262017-08-15 16:53:44 -07005470 int maxWidth = resources.getDimensionPixelSize(isLowRam
5471 ? R.dimen.notification_custom_view_max_image_width_low_ram
5472 : R.dimen.notification_custom_view_max_image_width);
5473 int maxHeight = resources.getDimensionPixelSize(isLowRam
5474 ? R.dimen.notification_custom_view_max_image_height_low_ram
5475 : R.dimen.notification_custom_view_max_image_height);
Selim Cinekd0426622017-07-11 13:19:59 +02005476 remoteView.reduceImageSizes(maxWidth, maxHeight);
5477 }
5478 }
5479
5480 /**
Selim Cinek22714f12017-04-13 16:23:53 -07005481 * @return whether this notification is a foreground service notification
Selim Cinek7b9605b2017-01-19 17:36:00 -08005482 */
Selim Cinek22714f12017-04-13 16:23:53 -07005483 private boolean isForegroundService() {
5484 return (flags & Notification.FLAG_FOREGROUND_SERVICE) != 0;
Selim Cinek7b9605b2017-01-19 17:36:00 -08005485 }
5486
5487 /**
Selim Cinek99104832017-01-25 14:47:33 -08005488 * @return whether this notification has a media session attached
5489 * @hide
5490 */
5491 public boolean hasMediaSession() {
5492 return extras.getParcelable(Notification.EXTRA_MEDIA_SESSION) != null;
5493 }
5494
5495 /**
Selim Cinek7d1009b2017-01-25 15:28:28 -08005496 * @return the style class of this notification
5497 * @hide
5498 */
5499 public Class<? extends Notification.Style> getNotificationStyle() {
5500 String templateClass = extras.getString(Notification.EXTRA_TEMPLATE);
5501
5502 if (!TextUtils.isEmpty(templateClass)) {
5503 return Notification.getNotificationStyleClass(templateClass);
5504 }
5505 return null;
5506 }
5507
5508 /**
Selim Cinek22714f12017-04-13 16:23:53 -07005509 * @return true if this notification is colorized.
Selim Cinek7b9605b2017-01-19 17:36:00 -08005510 *
5511 * @hide
5512 */
5513 public boolean isColorized() {
Selim Cinek5fb73f82017-04-20 16:55:38 -07005514 if (isColorizedMedia()) {
5515 return true;
5516 }
Julia Reynolds4db59552017-06-30 13:34:01 -04005517 return extras.getBoolean(EXTRA_COLORIZED)
5518 && (hasColorizedPermission() || isForegroundService());
5519 }
5520
5521 /**
5522 * Returns whether an app can colorize due to the android.permission.USE_COLORIZED_NOTIFICATIONS
5523 * permission. The permission is checked when a notification is enqueued.
5524 */
5525 private boolean hasColorizedPermission() {
5526 return (flags & Notification.FLAG_CAN_COLORIZE) != 0;
Selim Cinek5fb73f82017-04-20 16:55:38 -07005527 }
5528
5529 /**
5530 * @return true if this notification is colorized and it is a media notification
5531 *
5532 * @hide
5533 */
5534 public boolean isColorizedMedia() {
Selim Cinek99104832017-01-25 14:47:33 -08005535 Class<? extends Style> style = getNotificationStyle();
5536 if (MediaStyle.class.equals(style)) {
5537 Boolean colorized = (Boolean) extras.get(EXTRA_COLORIZED);
5538 if ((colorized == null || colorized) && hasMediaSession()) {
5539 return true;
5540 }
5541 } else if (DecoratedMediaCustomViewStyle.class.equals(style)) {
5542 if (extras.getBoolean(EXTRA_COLORIZED) && hasMediaSession()) {
5543 return true;
5544 }
5545 }
Selim Cinek5fb73f82017-04-20 16:55:38 -07005546 return false;
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005547 }
5548
Selim Cinek0847acd2017-04-24 19:48:29 -07005549
5550 /**
5551 * @return true if this is a media notification
5552 *
5553 * @hide
5554 */
5555 public boolean isMediaNotification() {
5556 Class<? extends Style> style = getNotificationStyle();
5557 if (MediaStyle.class.equals(style)) {
5558 return true;
5559 } else if (DecoratedMediaCustomViewStyle.class.equals(style)) {
5560 return true;
5561 }
5562 return false;
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005563 }
5564
Selim Cinek279fa862016-06-14 10:57:25 -07005565 private boolean hasLargeIcon() {
5566 return mLargeIcon != null || largeIcon != null;
5567 }
5568
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005569 /**
Selim Cinekc2c0b042016-05-18 17:13:46 -07005570 * @return true if the notification will show the time; false otherwise
Selim Cinekb85f36fd2016-04-20 18:46:36 -07005571 * @hide
5572 */
Selim Cinekc2c0b042016-05-18 17:13:46 -07005573 public boolean showsTime() {
Selim Cinekb85f36fd2016-04-20 18:46:36 -07005574 return when != 0 && extras.getBoolean(EXTRA_SHOW_WHEN);
5575 }
5576
5577 /**
Selim Cinekc2c0b042016-05-18 17:13:46 -07005578 * @return true if the notification will show a chronometer; false otherwise
5579 * @hide
5580 */
5581 public boolean showsChronometer() {
5582 return when != 0 && extras.getBoolean(EXTRA_SHOW_CHRONOMETER);
5583 }
5584
5585 /**
Julia Reynolds7ca33072017-06-29 13:58:24 -04005586 * @removed
Julia Reynolds4a02afb2016-12-13 13:39:52 -05005587 */
5588 @SystemApi
5589 public static Class<? extends Style> getNotificationStyleClass(String templateClass) {
5590 Class<? extends Style>[] classes = new Class[] {
5591 BigTextStyle.class, BigPictureStyle.class, InboxStyle.class, MediaStyle.class,
5592 DecoratedCustomViewStyle.class, DecoratedMediaCustomViewStyle.class,
5593 MessagingStyle.class };
5594 for (Class<? extends Style> innerClass : classes) {
5595 if (templateClass.equals(innerClass.getName())) {
5596 return innerClass;
5597 }
5598 }
5599 return null;
5600 }
5601
5602 /**
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005603 * An object that can apply a rich notification style to a {@link Notification.Builder}
5604 * object.
5605 */
Griff Hazendfcb0802014-02-11 12:00:00 -08005606 public static abstract class Style {
Chris Wrend6297db2012-05-03 16:20:13 -04005607 private CharSequence mBigContentTitle;
Jorim Jaggi457a10d2014-09-08 16:18:23 +02005608
5609 /**
5610 * @hide
5611 */
5612 protected CharSequence mSummaryText = null;
5613
5614 /**
5615 * @hide
5616 */
5617 protected boolean mSummaryTextSet = false;
Chris Wrend6297db2012-05-03 16:20:13 -04005618
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005619 protected Builder mBuilder;
5620
Chris Wrend6297db2012-05-03 16:20:13 -04005621 /**
5622 * Overrides ContentTitle in the big form of the template.
5623 * This defaults to the value passed to setContentTitle().
5624 */
5625 protected void internalSetBigContentTitle(CharSequence title) {
5626 mBigContentTitle = title;
5627 }
5628
5629 /**
5630 * Set the first line of text after the detail section in the big form of the template.
5631 */
5632 protected void internalSetSummaryText(CharSequence cs) {
5633 mSummaryText = cs;
Daniel Sandler619738c2012-06-07 16:33:08 -04005634 mSummaryTextSet = true;
Chris Wrend6297db2012-05-03 16:20:13 -04005635 }
5636
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005637 public void setBuilder(Builder builder) {
5638 if (mBuilder != builder) {
5639 mBuilder = builder;
Daniel Sandlerc08dea22012-06-28 08:35:24 -07005640 if (mBuilder != null) {
5641 mBuilder.setStyle(this);
5642 }
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005643 }
5644 }
5645
Chris Wrend6297db2012-05-03 16:20:13 -04005646 protected void checkBuilder() {
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005647 if (mBuilder == null) {
5648 throw new IllegalArgumentException("Style requires a valid Builder object");
5649 }
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005650 }
Chris Wrend6297db2012-05-03 16:20:13 -04005651
5652 protected RemoteViews getStandardView(int layoutId) {
5653 checkBuilder();
5654
Christoph Studer4600f9b2014-07-22 22:44:43 +02005655 // Nasty.
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005656 CharSequence oldBuilderContentTitle =
5657 mBuilder.getAllExtras().getCharSequence(EXTRA_TITLE);
Chris Wrend6297db2012-05-03 16:20:13 -04005658 if (mBigContentTitle != null) {
5659 mBuilder.setContentTitle(mBigContentTitle);
5660 }
5661
Chris Wrend6297db2012-05-03 16:20:13 -04005662 RemoteViews contentView = mBuilder.applyStandardTemplateWithActions(layoutId);
5663
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005664 mBuilder.getAllExtras().putCharSequence(EXTRA_TITLE, oldBuilderContentTitle);
Christoph Studer4600f9b2014-07-22 22:44:43 +02005665
Chris Wrend6297db2012-05-03 16:20:13 -04005666 if (mBigContentTitle != null && mBigContentTitle.equals("")) {
5667 contentView.setViewVisibility(R.id.line1, View.GONE);
Chris Wren67dc9a02012-05-16 01:03:20 -04005668 } else {
5669 contentView.setViewVisibility(R.id.line1, View.VISIBLE);
Chris Wrend6297db2012-05-03 16:20:13 -04005670 }
5671
Chris Wrend6297db2012-05-03 16:20:13 -04005672 return contentView;
5673 }
5674
Daniel Sandlerf45564e2013-04-15 15:05:08 -04005675 /**
Selim Cinek7d1009b2017-01-25 15:28:28 -08005676 * Construct a Style-specific RemoteViews for the collapsed notification layout.
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005677 * The default implementation has nothing additional to add.
Selim Cinek7d1009b2017-01-25 15:28:28 -08005678 *
5679 * @param increasedHeight true if this layout be created with an increased height.
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005680 * @hide
5681 */
Selim Cinek7d1009b2017-01-25 15:28:28 -08005682 public RemoteViews makeContentView(boolean increasedHeight) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005683 return null;
5684 }
5685
5686 /**
5687 * Construct a Style-specific RemoteViews for the final big notification layout.
5688 * @hide
5689 */
5690 public RemoteViews makeBigContentView() {
5691 return null;
5692 }
5693
5694 /**
5695 * Construct a Style-specific RemoteViews for the final HUN layout.
Selim Cinek87ed69b2017-02-09 15:59:43 -08005696 *
5697 * @param increasedHeight true if this layout be created with an increased height.
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005698 * @hide
5699 */
Selim Cinek87ed69b2017-02-09 15:59:43 -08005700 public RemoteViews makeHeadsUpContentView(boolean increasedHeight) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005701 return null;
5702 }
5703
5704 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005705 * Apply any style-specific extras to this notification before shipping it out.
Daniel Sandlerf45564e2013-04-15 15:05:08 -04005706 * @hide
5707 */
5708 public void addExtras(Bundle extras) {
5709 if (mSummaryTextSet) {
5710 extras.putCharSequence(EXTRA_SUMMARY_TEXT, mSummaryText);
5711 }
5712 if (mBigContentTitle != null) {
5713 extras.putCharSequence(EXTRA_TITLE_BIG, mBigContentTitle);
5714 }
Chris Wren91ad5632013-06-05 15:05:57 -04005715 extras.putString(EXTRA_TEMPLATE, this.getClass().getName());
Daniel Sandlerf45564e2013-04-15 15:05:08 -04005716 }
5717
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04005718 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005719 * Reconstruct the internal state of this Style object from extras.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04005720 * @hide
5721 */
Christoph Studer4600f9b2014-07-22 22:44:43 +02005722 protected void restoreFromExtras(Bundle extras) {
5723 if (extras.containsKey(EXTRA_SUMMARY_TEXT)) {
5724 mSummaryText = extras.getCharSequence(EXTRA_SUMMARY_TEXT);
5725 mSummaryTextSet = true;
5726 }
5727 if (extras.containsKey(EXTRA_TITLE_BIG)) {
5728 mBigContentTitle = extras.getCharSequence(EXTRA_TITLE_BIG);
5729 }
5730 }
5731
5732
5733 /**
5734 * @hide
5735 */
5736 public Notification buildStyled(Notification wip) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005737 addExtras(wip.extras);
Christoph Studer4600f9b2014-07-22 22:44:43 +02005738 return wip;
5739 }
5740
Daniel Sandler0ec46202015-06-24 01:27:05 -04005741 /**
5742 * @hide
5743 */
Jorim Jaggia0d58ae2015-06-03 11:48:13 -07005744 public void purgeResources() {}
5745
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04005746 /**
5747 * Calls {@link android.app.Notification.Builder#build()} on the Builder this Style is
5748 * attached to.
5749 *
5750 * @return the fully constructed Notification.
5751 */
5752 public Notification build() {
5753 checkBuilder();
5754 return mBuilder.build();
5755 }
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02005756
5757 /**
5758 * @hide
5759 * @return true if the style positions the progress bar on the second line; false if the
5760 * style hides the progress bar
5761 */
5762 protected boolean hasProgress() {
5763 return true;
5764 }
Selim Cinek03d0d652015-11-13 13:18:09 -05005765
5766 /**
5767 * @hide
5768 * @return Whether we should put the summary be put into the notification header
5769 */
5770 public boolean hasSummaryInHeader() {
5771 return true;
5772 }
Selim Cinek593610c2016-02-16 18:42:57 -08005773
5774 /**
5775 * @hide
5776 * @return Whether custom content views are displayed inline in the style
5777 */
5778 public boolean displayCustomViewInline() {
5779 return false;
5780 }
Selim Cinekd0426622017-07-11 13:19:59 +02005781
5782 /**
5783 * Reduces the image sizes contained in this style.
5784 *
5785 * @hide
5786 */
5787 public void reduceImageSizes(Context context) {
5788 }
Joe Onorato46439ce2010-11-19 13:56:21 -08005789 }
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005790
5791 /**
Daniel Sandler4dfbe832012-04-11 14:51:46 -04005792 * Helper class for generating large-format notifications that include a large image attachment.
Joe Malin8d40d042012-11-05 11:36:40 -08005793 *
Robert Ly91c5ce32014-06-08 15:37:00 -07005794 * Here's how you'd set the <code>BigPictureStyle</code> on a notification:
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005795 * <pre class="prettyprint">
Robert Ly91c5ce32014-06-08 15:37:00 -07005796 * Notification notif = new Notification.Builder(mContext)
5797 * .setContentTitle(&quot;New photo from &quot; + sender.toString())
5798 * .setContentText(subject)
5799 * .setSmallIcon(R.drawable.new_post)
5800 * .setLargeIcon(aBitmap)
5801 * .setStyle(new Notification.BigPictureStyle()
5802 * .bigPicture(aBigBitmap))
5803 * .build();
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005804 * </pre>
Joe Malin8d40d042012-11-05 11:36:40 -08005805 *
Daniel Sandler4dfbe832012-04-11 14:51:46 -04005806 * @see Notification#bigContentView
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005807 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005808 public static class BigPictureStyle extends Style {
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005809 private Bitmap mPicture;
Dan Sandlerd63f9322015-05-06 15:18:49 -04005810 private Icon mBigLargeIcon;
Chris Wren3745a3d2012-05-22 15:11:52 -04005811 private boolean mBigLargeIconSet = false;
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005812
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005813 public BigPictureStyle() {
5814 }
5815
Adrian Roosf5faf9d2016-05-23 13:56:15 -07005816 /**
5817 * @deprecated use {@code BigPictureStyle()}.
5818 */
5819 @Deprecated
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005820 public BigPictureStyle(Builder builder) {
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005821 setBuilder(builder);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005822 }
5823
Chris Wrend6297db2012-05-03 16:20:13 -04005824 /**
5825 * Overrides ContentTitle in the big form of the template.
5826 * This defaults to the value passed to setContentTitle().
5827 */
5828 public BigPictureStyle setBigContentTitle(CharSequence title) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04005829 internalSetBigContentTitle(safeCharSequence(title));
Chris Wrend6297db2012-05-03 16:20:13 -04005830 return this;
5831 }
5832
5833 /**
5834 * Set the first line of text after the detail section in the big form of the template.
5835 */
5836 public BigPictureStyle setSummaryText(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04005837 internalSetSummaryText(safeCharSequence(cs));
Chris Wrend6297db2012-05-03 16:20:13 -04005838 return this;
5839 }
5840
Chris Wren0bd664d2012-08-01 13:56:56 -04005841 /**
5842 * Provide the bitmap to be used as the payload for the BigPicture notification.
5843 */
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005844 public BigPictureStyle bigPicture(Bitmap b) {
5845 mPicture = b;
5846 return this;
5847 }
5848
Chris Wren3745a3d2012-05-22 15:11:52 -04005849 /**
Chris Wren3745a3d2012-05-22 15:11:52 -04005850 * Override the large icon when the big notification is shown.
5851 */
5852 public BigPictureStyle bigLargeIcon(Bitmap b) {
Dan Sandlerd63f9322015-05-06 15:18:49 -04005853 return bigLargeIcon(b != null ? Icon.createWithBitmap(b) : null);
5854 }
5855
5856 /**
5857 * Override the large icon when the big notification is shown.
5858 */
5859 public BigPictureStyle bigLargeIcon(Icon icon) {
Chris Wren3745a3d2012-05-22 15:11:52 -04005860 mBigLargeIconSet = true;
Dan Sandlerd63f9322015-05-06 15:18:49 -04005861 mBigLargeIcon = icon;
Chris Wren3745a3d2012-05-22 15:11:52 -04005862 return this;
5863 }
5864
Riley Andrews0394a0c2015-11-03 23:36:52 -08005865 /** @hide */
5866 public static final int MIN_ASHMEM_BITMAP_SIZE = 128 * (1 << 10);
5867
Daniel Sandler0ec46202015-06-24 01:27:05 -04005868 /**
5869 * @hide
5870 */
Jorim Jaggia0d58ae2015-06-03 11:48:13 -07005871 @Override
5872 public void purgeResources() {
5873 super.purgeResources();
Riley Andrews8cee7c12015-11-01 23:36:04 -08005874 if (mPicture != null &&
5875 mPicture.isMutable() &&
Riley Andrews0394a0c2015-11-03 23:36:52 -08005876 mPicture.getAllocationByteCount() >= MIN_ASHMEM_BITMAP_SIZE) {
Jorim Jaggia0d58ae2015-06-03 11:48:13 -07005877 mPicture = mPicture.createAshmemBitmap();
5878 }
5879 if (mBigLargeIcon != null) {
5880 mBigLargeIcon.convertToAshmem();
5881 }
5882 }
Christoph Studer5c510ee2014-12-15 16:32:27 +01005883
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005884 /**
5885 * @hide
5886 */
Selim Cinekd0426622017-07-11 13:19:59 +02005887 @Override
5888 public void reduceImageSizes(Context context) {
5889 super.reduceImageSizes(context);
5890 Resources resources = context.getResources();
Selim Cineka8cb1262017-08-15 16:53:44 -07005891 boolean isLowRam = ActivityManager.isLowRamDeviceStatic();
Selim Cinekd0426622017-07-11 13:19:59 +02005892 if (mPicture != null) {
Selim Cineka8cb1262017-08-15 16:53:44 -07005893 int maxPictureWidth = resources.getDimensionPixelSize(isLowRam
5894 ? R.dimen.notification_big_picture_max_height_low_ram
5895 : R.dimen.notification_big_picture_max_height);
5896 int maxPictureHeight = resources.getDimensionPixelSize(isLowRam
5897 ? R.dimen.notification_big_picture_max_width_low_ram
5898 : R.dimen.notification_big_picture_max_width);
Selim Cinekd0426622017-07-11 13:19:59 +02005899 mPicture = Icon.scaleDownIfNecessary(mPicture, maxPictureWidth, maxPictureHeight);
5900 }
5901 if (mBigLargeIcon != null) {
Selim Cineka8cb1262017-08-15 16:53:44 -07005902 int rightIconSize = resources.getDimensionPixelSize(isLowRam
5903 ? R.dimen.notification_right_icon_size_low_ram
5904 : R.dimen.notification_right_icon_size);
Selim Cinekd0426622017-07-11 13:19:59 +02005905 mBigLargeIcon.scaleDownIfNecessary(rightIconSize, rightIconSize);
5906 }
5907 }
5908
5909 /**
5910 * @hide
5911 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005912 public RemoteViews makeBigContentView() {
5913 // Replace mN.mLargeIcon with mBigLargeIcon if mBigLargeIconSet
Christoph Studer5c510ee2014-12-15 16:32:27 +01005914 // This covers the following cases:
5915 // 1. mBigLargeIconSet -> mBigLargeIcon (null or non-null) applies, overrides
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005916 // mN.mLargeIcon
5917 // 2. !mBigLargeIconSet -> mN.mLargeIcon applies
Dan Sandlerd63f9322015-05-06 15:18:49 -04005918 Icon oldLargeIcon = null;
Selim Cineke99acb22016-08-04 12:55:48 -07005919 Bitmap largeIconLegacy = null;
Christoph Studer5c510ee2014-12-15 16:32:27 +01005920 if (mBigLargeIconSet) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005921 oldLargeIcon = mBuilder.mN.mLargeIcon;
5922 mBuilder.mN.mLargeIcon = mBigLargeIcon;
Selim Cineke99acb22016-08-04 12:55:48 -07005923 // The legacy largeIcon might not allow us to clear the image, as it's taken in
5924 // replacement if the other one is null. Because we're restoring these legacy icons
5925 // for old listeners, this is in general non-null.
5926 largeIconLegacy = mBuilder.mN.largeIcon;
5927 mBuilder.mN.largeIcon = null;
Christoph Studer5c510ee2014-12-15 16:32:27 +01005928 }
5929
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005930 RemoteViews contentView = getStandardView(mBuilder.getBigPictureLayoutResource());
Selim Cinek03d0d652015-11-13 13:18:09 -05005931 if (mSummaryTextSet) {
Selim Cinek48f66b72017-08-18 16:17:51 -07005932 contentView.setTextViewText(R.id.text, mBuilder.processTextSpans(
5933 mBuilder.processLegacyText(mSummaryText)));
Selim Cinek7b9605b2017-01-19 17:36:00 -08005934 mBuilder.setTextViewColorSecondary(contentView, R.id.text);
Selim Cinekc848c3a2016-01-13 15:27:30 -08005935 contentView.setViewVisibility(R.id.text, View.VISIBLE);
Selim Cinek03d0d652015-11-13 13:18:09 -05005936 }
Selim Cinek279fa862016-06-14 10:57:25 -07005937 mBuilder.setContentMinHeight(contentView, mBuilder.mN.hasLargeIcon());
Selim Cinek53e64a42015-11-16 10:40:56 -08005938
Christoph Studer5c510ee2014-12-15 16:32:27 +01005939 if (mBigLargeIconSet) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005940 mBuilder.mN.mLargeIcon = oldLargeIcon;
Selim Cineke99acb22016-08-04 12:55:48 -07005941 mBuilder.mN.largeIcon = largeIconLegacy;
Christoph Studer5c510ee2014-12-15 16:32:27 +01005942 }
5943
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005944 contentView.setImageViewBitmap(R.id.big_picture, mPicture);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005945 return contentView;
5946 }
5947
Daniel Sandlerf45564e2013-04-15 15:05:08 -04005948 /**
5949 * @hide
5950 */
5951 public void addExtras(Bundle extras) {
5952 super.addExtras(extras);
5953
5954 if (mBigLargeIconSet) {
5955 extras.putParcelable(EXTRA_LARGE_ICON_BIG, mBigLargeIcon);
5956 }
5957 extras.putParcelable(EXTRA_PICTURE, mPicture);
5958 }
5959
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04005960 /**
5961 * @hide
5962 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005963 @Override
Christoph Studer4600f9b2014-07-22 22:44:43 +02005964 protected void restoreFromExtras(Bundle extras) {
5965 super.restoreFromExtras(extras);
5966
5967 if (extras.containsKey(EXTRA_LARGE_ICON_BIG)) {
Christoph Studer5c510ee2014-12-15 16:32:27 +01005968 mBigLargeIconSet = true;
Christoph Studer4600f9b2014-07-22 22:44:43 +02005969 mBigLargeIcon = extras.getParcelable(EXTRA_LARGE_ICON_BIG);
Chris Wren3745a3d2012-05-22 15:11:52 -04005970 }
Christoph Studer4600f9b2014-07-22 22:44:43 +02005971 mPicture = extras.getParcelable(EXTRA_PICTURE);
5972 }
Selim Cinek03d0d652015-11-13 13:18:09 -05005973
5974 /**
5975 * @hide
5976 */
5977 @Override
5978 public boolean hasSummaryInHeader() {
5979 return false;
5980 }
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005981 }
5982
5983 /**
Daniel Sandler4dfbe832012-04-11 14:51:46 -04005984 * Helper class for generating large-format notifications that include a lot of text.
Joe Malin8d40d042012-11-05 11:36:40 -08005985 *
Robert Ly91c5ce32014-06-08 15:37:00 -07005986 * Here's how you'd set the <code>BigTextStyle</code> on a notification:
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005987 * <pre class="prettyprint">
Robert Ly91c5ce32014-06-08 15:37:00 -07005988 * Notification notif = new Notification.Builder(mContext)
5989 * .setContentTitle(&quot;New mail from &quot; + sender.toString())
5990 * .setContentText(subject)
5991 * .setSmallIcon(R.drawable.new_mail)
5992 * .setLargeIcon(aBitmap)
5993 * .setStyle(new Notification.BigTextStyle()
5994 * .bigText(aVeryLongString))
5995 * .build();
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005996 * </pre>
Joe Malin8d40d042012-11-05 11:36:40 -08005997 *
Daniel Sandler4dfbe832012-04-11 14:51:46 -04005998 * @see Notification#bigContentView
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005999 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006000 public static class BigTextStyle extends Style {
Jorim Jaggi457a10d2014-09-08 16:18:23 +02006001
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006002 private CharSequence mBigText;
6003
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006004 public BigTextStyle() {
6005 }
6006
Adrian Roosf5faf9d2016-05-23 13:56:15 -07006007 /**
6008 * @deprecated use {@code BigTextStyle()}.
6009 */
6010 @Deprecated
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006011 public BigTextStyle(Builder builder) {
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006012 setBuilder(builder);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006013 }
6014
Chris Wrend6297db2012-05-03 16:20:13 -04006015 /**
6016 * Overrides ContentTitle in the big form of the template.
6017 * This defaults to the value passed to setContentTitle().
6018 */
6019 public BigTextStyle setBigContentTitle(CharSequence title) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04006020 internalSetBigContentTitle(safeCharSequence(title));
Chris Wrend6297db2012-05-03 16:20:13 -04006021 return this;
6022 }
6023
6024 /**
6025 * Set the first line of text after the detail section in the big form of the template.
6026 */
6027 public BigTextStyle setSummaryText(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04006028 internalSetSummaryText(safeCharSequence(cs));
Chris Wrend6297db2012-05-03 16:20:13 -04006029 return this;
6030 }
6031
Chris Wren0bd664d2012-08-01 13:56:56 -04006032 /**
6033 * Provide the longer text to be displayed in the big form of the
6034 * template in place of the content text.
6035 */
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006036 public BigTextStyle bigText(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04006037 mBigText = safeCharSequence(cs);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006038 return this;
6039 }
6040
Daniel Sandlerf45564e2013-04-15 15:05:08 -04006041 /**
6042 * @hide
6043 */
6044 public void addExtras(Bundle extras) {
6045 super.addExtras(extras);
6046
Christoph Studer4600f9b2014-07-22 22:44:43 +02006047 extras.putCharSequence(EXTRA_BIG_TEXT, mBigText);
6048 }
6049
6050 /**
6051 * @hide
6052 */
6053 @Override
6054 protected void restoreFromExtras(Bundle extras) {
6055 super.restoreFromExtras(extras);
6056
6057 mBigText = extras.getCharSequence(EXTRA_BIG_TEXT);
Daniel Sandlerf45564e2013-04-15 15:05:08 -04006058 }
6059
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006060 /**
Selim Cinek7d1009b2017-01-25 15:28:28 -08006061 * @param increasedHeight true if this layout be created with an increased height.
6062 *
6063 * @hide
6064 */
6065 @Override
6066 public RemoteViews makeContentView(boolean increasedHeight) {
6067 if (increasedHeight) {
Selim Cinek5d6ef8d2017-05-18 22:16:00 -07006068 mBuilder.mOriginalActions = mBuilder.mActions;
Selim Cinek7d1009b2017-01-25 15:28:28 -08006069 mBuilder.mActions = new ArrayList<>();
6070 RemoteViews remoteViews = makeBigContentView();
Selim Cinek5d6ef8d2017-05-18 22:16:00 -07006071 mBuilder.mActions = mBuilder.mOriginalActions;
6072 mBuilder.mOriginalActions = null;
Selim Cinek7d1009b2017-01-25 15:28:28 -08006073 return remoteViews;
6074 }
6075 return super.makeContentView(increasedHeight);
6076 }
6077
6078 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006079 * @hide
6080 */
Selim Cinek87ed69b2017-02-09 15:59:43 -08006081 @Override
6082 public RemoteViews makeHeadsUpContentView(boolean increasedHeight) {
6083 if (increasedHeight && mBuilder.mActions.size() > 0) {
6084 return makeBigContentView();
6085 }
6086 return super.makeHeadsUpContentView(increasedHeight);
6087 }
6088
6089 /**
6090 * @hide
6091 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006092 public RemoteViews makeBigContentView() {
Christoph Studer4600f9b2014-07-22 22:44:43 +02006093
6094 // Nasty
Selim Cinek75998782016-04-26 10:39:17 -07006095 CharSequence text = mBuilder.getAllExtras().getCharSequence(EXTRA_TEXT);
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006096 mBuilder.getAllExtras().putCharSequence(EXTRA_TEXT, null);
Daniel Sandler916ad912012-06-13 12:17:07 -04006097
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01006098 RemoteViews contentView = getStandardView(mBuilder.getBigTextLayoutResource());
Joe Malin8d40d042012-11-05 11:36:40 -08006099
Selim Cinek75998782016-04-26 10:39:17 -07006100 mBuilder.getAllExtras().putCharSequence(EXTRA_TEXT, text);
Christoph Studer4600f9b2014-07-22 22:44:43 +02006101
Selim Cinek3a2c4b92015-12-17 17:01:17 -08006102 CharSequence bigTextText = mBuilder.processLegacyText(mBigText);
Selim Cinek75998782016-04-26 10:39:17 -07006103 if (TextUtils.isEmpty(bigTextText)) {
6104 // In case the bigtext is null / empty fall back to the normal text to avoid a weird
6105 // experience
6106 bigTextText = mBuilder.processLegacyText(text);
6107 }
Adrian Roosb1f427c2016-05-26 12:27:15 -07006108 applyBigTextContentView(mBuilder, contentView, bigTextText);
Selim Cinek4fb12d32015-11-19 18:10:48 -08006109
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006110 return contentView;
6111 }
6112
Adrian Roosb1f427c2016-05-26 12:27:15 -07006113 static void applyBigTextContentView(Builder builder,
6114 RemoteViews contentView, CharSequence bigTextText) {
Selim Cinek48f66b72017-08-18 16:17:51 -07006115 contentView.setTextViewText(R.id.big_text, builder.processTextSpans(bigTextText));
Selim Cinek7b9605b2017-01-19 17:36:00 -08006116 builder.setTextViewColorSecondary(contentView, R.id.big_text);
Adrian Roosb1f427c2016-05-26 12:27:15 -07006117 contentView.setViewVisibility(R.id.big_text,
6118 TextUtils.isEmpty(bigTextText) ? View.GONE : View.VISIBLE);
Selim Cinek279fa862016-06-14 10:57:25 -07006119 contentView.setBoolean(R.id.big_text, "setHasImage", builder.mN.hasLargeIcon());
Adrian Roosb1f427c2016-05-26 12:27:15 -07006120 }
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006121 }
Daniel Sandler879c5e02012-04-17 16:46:51 -04006122
6123 /**
Alex Hillsfc737de2016-03-23 17:33:02 -04006124 * Helper class for generating large-format notifications that include multiple back-and-forth
6125 * messages of varying types between any number of people.
6126 *
6127 * <br>
6128 * If the platform does not provide large-format notifications, this method has no effect. The
6129 * user will always see the normal notification view.
6130 * <br>
6131 * This class is a "rebuilder": It attaches to a Builder object and modifies its behavior, like
6132 * so:
6133 * <pre class="prettyprint">
6134 *
6135 * Notification noti = new Notification.Builder()
6136 * .setContentTitle(&quot;2 new messages wtih &quot; + sender.toString())
6137 * .setContentText(subject)
6138 * .setSmallIcon(R.drawable.new_message)
6139 * .setLargeIcon(aBitmap)
6140 * .setStyle(new Notification.MessagingStyle(resources.getString(R.string.reply_name))
6141 * .addMessage(messages[0].getText(), messages[0].getTime(), messages[0].getSender())
6142 * .addMessage(messages[1].getText(), messages[1].getTime(), messages[1].getSender()))
6143 * .build();
6144 * </pre>
6145 */
6146 public static class MessagingStyle extends Style {
6147
6148 /**
6149 * The maximum number of messages that will be retained in the Notification itself (the
6150 * number displayed is up to the platform).
6151 */
6152 public static final int MAXIMUM_RETAINED_MESSAGES = 25;
6153
Selim Cinekcb8b9852017-12-15 18:01:52 -08006154 @NonNull Person mUser;
Kodlee Yin9ac617c2017-12-19 11:20:50 -08006155 @Nullable CharSequence mConversationTitle;
Alex Hillsd9b04d92016-04-11 16:38:16 -04006156 List<Message> mMessages = new ArrayList<>();
Adrian Roos437cd562017-01-18 15:47:03 -08006157 List<Message> mHistoricMessages = new ArrayList<>();
Kodlee Yin9ac617c2017-12-19 11:20:50 -08006158 boolean mIsGroupConversation;
Alex Hillsfc737de2016-03-23 17:33:02 -04006159
6160 MessagingStyle() {
6161 }
6162
6163 /**
Alex Hillsfd590442016-10-07 09:52:44 -04006164 * @param userDisplayName Required - the name to be displayed for any replies sent by the
6165 * user before the posting app reposts the notification with those messages after they've
6166 * been actually sent and in previous messages sent by the user added in
Alex Hillsfc737de2016-03-23 17:33:02 -04006167 * {@link #addMessage(Notification.MessagingStyle.Message)}
Selim Cinekcb8b9852017-12-15 18:01:52 -08006168 *
6169 * @deprecated use {@code MessagingStyle(Person)}
Alex Hillsfc737de2016-03-23 17:33:02 -04006170 */
Alex Hillsfd590442016-10-07 09:52:44 -04006171 public MessagingStyle(@NonNull CharSequence userDisplayName) {
Selim Cinekcb8b9852017-12-15 18:01:52 -08006172 this(new Person().setName(userDisplayName));
6173 }
6174
6175 /**
6176 * @param user Required - The person displayed for any messages that are sent by the
6177 * user. Any messages added with {@link #addMessage(Notification.MessagingStyle.Message)}
6178 * who don't have a Person associated with it will be displayed as if they were sent
6179 * by this user. The user also needs to have a valid name associated with it.
6180 */
6181 public MessagingStyle(@NonNull Person user) {
6182 mUser = user;
6183 if (user == null || user.getName() == null) {
6184 throw new RuntimeException("user must be valid and have a name");
6185 }
6186 }
6187
6188 /**
6189 * @return the user to be displayed for any replies sent by the user
6190 */
6191 public Person getUser() {
6192 return mUser;
Alex Hillsfc737de2016-03-23 17:33:02 -04006193 }
6194
6195 /**
6196 * Returns the name to be displayed for any replies sent by the user
Selim Cinekcb8b9852017-12-15 18:01:52 -08006197 *
6198 * @deprecated use {@link #getUser()} instead
Alex Hillsfc737de2016-03-23 17:33:02 -04006199 */
6200 public CharSequence getUserDisplayName() {
Selim Cinekcb8b9852017-12-15 18:01:52 -08006201 return mUser.getName();
Alex Hillsfc737de2016-03-23 17:33:02 -04006202 }
6203
6204 /**
Kodlee Yin9ac617c2017-12-19 11:20:50 -08006205 * Sets the title to be displayed on this conversation. May be set to {@code null}.
6206 *
Kodlee Yin14656422017-12-22 17:00:46 -08006207 * <p>This API's behavior was changed in SDK version {@link Build.VERSION_CODES#P}. If your
6208 * application's target version is less than {@link Build.VERSION_CODES#P}, setting a
6209 * conversation title to a non-null value will make {@link #isGroupConversation()} return
6210 * {@code true} and passing {@code null} will make it return {@code false}. In
6211 * {@link Build.VERSION_CODES#P} and beyond, use {@link #setGroupConversation(boolean)}
6212 * to set group conversation status.
6213 *
6214 * @param conversationTitle Title displayed for this conversation
6215 * @return this object for method chaining
Alex Hillsfc737de2016-03-23 17:33:02 -04006216 */
Kodlee Yin9ac617c2017-12-19 11:20:50 -08006217 public MessagingStyle setConversationTitle(@Nullable CharSequence conversationTitle) {
Alex Hillsfc737de2016-03-23 17:33:02 -04006218 mConversationTitle = conversationTitle;
6219 return this;
6220 }
6221
6222 /**
Kodlee Yin9ac617c2017-12-19 11:20:50 -08006223 * Return the title to be displayed on this conversation. May return {@code null}.
Alex Hillsfc737de2016-03-23 17:33:02 -04006224 */
Kodlee Yin9ac617c2017-12-19 11:20:50 -08006225 @Nullable
Alex Hillsfc737de2016-03-23 17:33:02 -04006226 public CharSequence getConversationTitle() {
6227 return mConversationTitle;
6228 }
6229
6230 /**
6231 * Adds a message for display by this notification. Convenience call for a simple
6232 * {@link Message} in {@link #addMessage(Notification.MessagingStyle.Message)}.
6233 * @param text A {@link CharSequence} to be displayed as the message content
6234 * @param timestamp Time at which the message arrived
6235 * @param sender A {@link CharSequence} to be used for displaying the name of the
6236 * sender. Should be <code>null</code> for messages by the current user, in which case
6237 * the platform will insert {@link #getUserDisplayName()}.
6238 * Should be unique amongst all individuals in the conversation, and should be
6239 * consistent during re-posts of the notification.
6240 *
6241 * @see Message#Message(CharSequence, long, CharSequence)
6242 *
6243 * @return this object for method chaining
Selim Cinekcb8b9852017-12-15 18:01:52 -08006244 *
6245 * @deprecated use {@link #addMessage(CharSequence, long, Person)}
Alex Hillsfc737de2016-03-23 17:33:02 -04006246 */
6247 public MessagingStyle addMessage(CharSequence text, long timestamp, CharSequence sender) {
Selim Cinekcb8b9852017-12-15 18:01:52 -08006248 return addMessage(text, timestamp,
6249 sender == null ? null : new Person().setName(sender));
6250 }
6251
6252 /**
6253 * Adds a message for display by this notification. Convenience call for a simple
6254 * {@link Message} in {@link #addMessage(Notification.MessagingStyle.Message)}.
6255 * @param text A {@link CharSequence} to be displayed as the message content
6256 * @param timestamp Time at which the message arrived
6257 * @param sender The {@link Person} who sent the message.
6258 * Should be <code>null</code> for messages by the current user, in which case
6259 * the platform will insert the user set in {@code MessagingStyle(Person)}.
6260 *
6261 * @see Message#Message(CharSequence, long, CharSequence)
6262 *
6263 * @return this object for method chaining
6264 */
6265 public MessagingStyle addMessage(CharSequence text, long timestamp, Person sender) {
Adrian Roos437cd562017-01-18 15:47:03 -08006266 return addMessage(new Message(text, timestamp, sender));
Alex Hillsfc737de2016-03-23 17:33:02 -04006267 }
6268
6269 /**
6270 * Adds a {@link Message} for display in this notification.
Adrian Roos437cd562017-01-18 15:47:03 -08006271 *
6272 * <p>The messages should be added in chronologic order, i.e. the oldest first,
6273 * the newest last.
6274 *
Alex Hillsfc737de2016-03-23 17:33:02 -04006275 * @param message The {@link Message} to be displayed
6276 * @return this object for method chaining
6277 */
6278 public MessagingStyle addMessage(Message message) {
6279 mMessages.add(message);
6280 if (mMessages.size() > MAXIMUM_RETAINED_MESSAGES) {
6281 mMessages.remove(0);
6282 }
6283 return this;
6284 }
6285
6286 /**
Adrian Roos437cd562017-01-18 15:47:03 -08006287 * Adds a {@link Message} for historic context in this notification.
6288 *
6289 * <p>Messages should be added as historic if they are not the main subject of the
6290 * notification but may give context to a conversation. The system may choose to present
6291 * them only when relevant, e.g. when replying to a message through a {@link RemoteInput}.
6292 *
6293 * <p>The messages should be added in chronologic order, i.e. the oldest first,
6294 * the newest last.
6295 *
6296 * @param message The historic {@link Message} to be added
6297 * @return this object for method chaining
6298 */
6299 public MessagingStyle addHistoricMessage(Message message) {
6300 mHistoricMessages.add(message);
6301 if (mHistoricMessages.size() > MAXIMUM_RETAINED_MESSAGES) {
6302 mHistoricMessages.remove(0);
6303 }
6304 return this;
6305 }
6306
6307 /**
Alex Hillsfc737de2016-03-23 17:33:02 -04006308 * Gets the list of {@code Message} objects that represent the notification
6309 */
6310 public List<Message> getMessages() {
6311 return mMessages;
6312 }
6313
6314 /**
Adrian Roos437cd562017-01-18 15:47:03 -08006315 * Gets the list of historic {@code Message}s in the notification.
6316 */
6317 public List<Message> getHistoricMessages() {
6318 return mHistoricMessages;
6319 }
6320
6321 /**
Kodlee Yin9ac617c2017-12-19 11:20:50 -08006322 * Sets whether this conversation notification represents a group.
Kodlee Yin14656422017-12-22 17:00:46 -08006323 *
Kodlee Yin9ac617c2017-12-19 11:20:50 -08006324 * @param isGroupConversation {@code true} if the conversation represents a group,
6325 * {@code false} otherwise.
6326 * @return this object for method chaining
6327 */
6328 public MessagingStyle setGroupConversation(boolean isGroupConversation) {
6329 mIsGroupConversation = isGroupConversation;
6330 return this;
6331 }
6332
6333 /**
Kodlee Yin14656422017-12-22 17:00:46 -08006334 * Returns {@code true} if this notification represents a group conversation, otherwise
6335 * {@code false}.
6336 *
6337 * <p> If the application that generated this {@link MessagingStyle} targets an SDK version
6338 * less than {@link Build.VERSION_CODES#P}, this method becomes dependent on whether or
6339 * not the conversation title is set; returning {@code true} if the conversation title is
6340 * a non-null value, or {@code false} otherwise. From {@link Build.VERSION_CODES#P} forward,
6341 * this method returns what's set by {@link #setGroupConversation(boolean)} allowing for
6342 * named, non-group conversations.
6343 *
6344 * @see #setConversationTitle(CharSequence)
Kodlee Yin9ac617c2017-12-19 11:20:50 -08006345 */
6346 public boolean isGroupConversation() {
Kodlee Yin14656422017-12-22 17:00:46 -08006347 // When target SDK version is < P, a non-null conversation title dictates if this is
6348 // as group conversation.
6349 if (mBuilder != null
6350 && mBuilder.mContext.getApplicationInfo().targetSdkVersion
6351 < Build.VERSION_CODES.P) {
6352 return mConversationTitle != null;
6353 }
6354
Kodlee Yin9ac617c2017-12-19 11:20:50 -08006355 return mIsGroupConversation;
6356 }
6357
6358 /**
Alex Hillsfc737de2016-03-23 17:33:02 -04006359 * @hide
6360 */
6361 @Override
6362 public void addExtras(Bundle extras) {
6363 super.addExtras(extras);
Selim Cinekcb8b9852017-12-15 18:01:52 -08006364 if (mUser != null) {
6365 // For legacy usages
6366 extras.putCharSequence(EXTRA_SELF_DISPLAY_NAME, mUser.getName());
6367 extras.putParcelable(EXTRA_MESSAGING_PERSON, mUser);
Alex Hillsfc737de2016-03-23 17:33:02 -04006368 }
6369 if (mConversationTitle != null) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04006370 extras.putCharSequence(EXTRA_CONVERSATION_TITLE, mConversationTitle);
Alex Hillsfc737de2016-03-23 17:33:02 -04006371 }
Alex Hillsd9b04d92016-04-11 16:38:16 -04006372 if (!mMessages.isEmpty()) { extras.putParcelableArray(EXTRA_MESSAGES,
6373 Message.getBundleArrayForMessages(mMessages));
Alex Hillsfc737de2016-03-23 17:33:02 -04006374 }
Adrian Roos437cd562017-01-18 15:47:03 -08006375 if (!mHistoricMessages.isEmpty()) { extras.putParcelableArray(EXTRA_HISTORIC_MESSAGES,
6376 Message.getBundleArrayForMessages(mHistoricMessages));
6377 }
Adrian Roos33fbd2c2016-05-27 15:35:28 -07006378
6379 fixTitleAndTextExtras(extras);
Kodlee Yin9ac617c2017-12-19 11:20:50 -08006380 extras.putBoolean(EXTRA_IS_GROUP_CONVERSATION, mIsGroupConversation);
Adrian Roos33fbd2c2016-05-27 15:35:28 -07006381 }
6382
6383 private void fixTitleAndTextExtras(Bundle extras) {
6384 Message m = findLatestIncomingMessage();
6385 CharSequence text = (m == null) ? null : m.mText;
6386 CharSequence sender = m == null ? null
Selim Cinekcb8b9852017-12-15 18:01:52 -08006387 : m.mSender == null || TextUtils.isEmpty(m.mSender.getName())
6388 ? mUser.getName() : m.mSender.getName();
Adrian Roos33fbd2c2016-05-27 15:35:28 -07006389 CharSequence title;
6390 if (!TextUtils.isEmpty(mConversationTitle)) {
6391 if (!TextUtils.isEmpty(sender)) {
6392 BidiFormatter bidi = BidiFormatter.getInstance();
6393 title = mBuilder.mContext.getString(
6394 com.android.internal.R.string.notification_messaging_title_template,
Selim Cinekcb8b9852017-12-15 18:01:52 -08006395 bidi.unicodeWrap(mConversationTitle), bidi.unicodeWrap(sender));
Adrian Roos33fbd2c2016-05-27 15:35:28 -07006396 } else {
6397 title = mConversationTitle;
6398 }
6399 } else {
6400 title = sender;
6401 }
6402
6403 if (title != null) {
6404 extras.putCharSequence(EXTRA_TITLE, title);
6405 }
6406 if (text != null) {
6407 extras.putCharSequence(EXTRA_TEXT, text);
6408 }
Alex Hillsfc737de2016-03-23 17:33:02 -04006409 }
6410
6411 /**
6412 * @hide
6413 */
6414 @Override
6415 protected void restoreFromExtras(Bundle extras) {
6416 super.restoreFromExtras(extras);
6417
Selim Cinekcb8b9852017-12-15 18:01:52 -08006418 mUser = extras.getParcelable(EXTRA_MESSAGING_PERSON);
6419 if (mUser == null) {
6420 CharSequence displayName = extras.getCharSequence(EXTRA_SELF_DISPLAY_NAME);
6421 mUser = new Person().setName(displayName);
6422 }
Adrian Roos96b7e202016-05-17 13:50:38 -07006423 mConversationTitle = extras.getCharSequence(EXTRA_CONVERSATION_TITLE);
Adrian Roos437cd562017-01-18 15:47:03 -08006424 Parcelable[] messages = extras.getParcelableArray(EXTRA_MESSAGES);
Selim Cinek88188f22017-09-19 16:46:56 -07006425 mMessages = Message.getMessagesFromBundleArray(messages);
Adrian Roos437cd562017-01-18 15:47:03 -08006426 Parcelable[] histMessages = extras.getParcelableArray(EXTRA_HISTORIC_MESSAGES);
Selim Cinek88188f22017-09-19 16:46:56 -07006427 mHistoricMessages = Message.getMessagesFromBundleArray(histMessages);
Kodlee Yin9ac617c2017-12-19 11:20:50 -08006428 mIsGroupConversation = extras.getBoolean(EXTRA_IS_GROUP_CONVERSATION);
Alex Hillsfc737de2016-03-23 17:33:02 -04006429 }
6430
6431 /**
6432 * @hide
6433 */
Adrian Roosc1a80b02016-04-05 14:54:55 -07006434 @Override
Selim Cinek7d1009b2017-01-25 15:28:28 -08006435 public RemoteViews makeContentView(boolean increasedHeight) {
Selim Cineke62255c2017-09-28 18:23:23 -07006436 mBuilder.mOriginalActions = mBuilder.mActions;
6437 mBuilder.mActions = new ArrayList<>();
Selim Cinekafeed292017-12-12 17:32:44 -08006438 RemoteViews remoteViews = makeBigContentView(true /* showRightIcon */);
Selim Cineke62255c2017-09-28 18:23:23 -07006439 mBuilder.mActions = mBuilder.mOriginalActions;
6440 mBuilder.mOriginalActions = null;
6441 return remoteViews;
Adrian Roosc1a80b02016-04-05 14:54:55 -07006442 }
6443
6444 private Message findLatestIncomingMessage() {
Selim Cinek88188f22017-09-19 16:46:56 -07006445 return findLatestIncomingMessage(mMessages);
6446 }
6447
6448 /**
6449 * @hide
6450 */
6451 @Nullable
6452 public static Message findLatestIncomingMessage(
6453 List<Message> messages) {
6454 for (int i = messages.size() - 1; i >= 0; i--) {
6455 Message m = messages.get(i);
Adrian Roosc1a80b02016-04-05 14:54:55 -07006456 // Incoming messages have a non-empty sender.
Selim Cinekcb8b9852017-12-15 18:01:52 -08006457 if (m.mSender != null && !TextUtils.isEmpty(m.mSender.getName())) {
Adrian Roosc1a80b02016-04-05 14:54:55 -07006458 return m;
6459 }
6460 }
Selim Cinek88188f22017-09-19 16:46:56 -07006461 if (!messages.isEmpty()) {
Adrian Roos33fbd2c2016-05-27 15:35:28 -07006462 // No incoming messages, fall back to outgoing message
Selim Cinek88188f22017-09-19 16:46:56 -07006463 return messages.get(messages.size() - 1);
Adrian Roos33fbd2c2016-05-27 15:35:28 -07006464 }
Adrian Roosc1a80b02016-04-05 14:54:55 -07006465 return null;
6466 }
6467
6468 /**
6469 * @hide
6470 */
6471 @Override
6472 public RemoteViews makeBigContentView() {
Selim Cinekafeed292017-12-12 17:32:44 -08006473 return makeBigContentView(false /* showRightIcon */);
6474 }
6475
6476 @NonNull
6477 private RemoteViews makeBigContentView(boolean showRightIcon) {
Selim Cinek88188f22017-09-19 16:46:56 -07006478 CharSequence conversationTitle = !TextUtils.isEmpty(super.mBigContentTitle)
Adrian Roosc1a80b02016-04-05 14:54:55 -07006479 ? super.mBigContentTitle
6480 : mConversationTitle;
Selim Cinek88188f22017-09-19 16:46:56 -07006481 boolean isOneToOne = TextUtils.isEmpty(conversationTitle);
Selim Cinek2dd3e722018-01-19 11:06:06 -08006482 CharSequence nameReplacement = null;
Selim Cinekafeed292017-12-12 17:32:44 -08006483 if (hasOnlyWhiteSpaceSenders()) {
Selim Cinekf7409db2017-10-24 16:17:14 -07006484 isOneToOne = true;
Selim Cinek2dd3e722018-01-19 11:06:06 -08006485 nameReplacement = conversationTitle;
6486 conversationTitle = null;
Adrian Roosb1f427c2016-05-26 12:27:15 -07006487 }
Adrian Roos48d746a2016-04-12 14:57:28 -07006488 RemoteViews contentView = mBuilder.applyStandardTemplateWithActions(
Adrian Roosc1a80b02016-04-05 14:54:55 -07006489 mBuilder.getMessagingLayoutResource(),
Selim Cinek88188f22017-09-19 16:46:56 -07006490 mBuilder.mParams.reset().hasProgress(false).title(conversationTitle).text(null)
Selim Cinekafeed292017-12-12 17:32:44 -08006491 .hideLargeIcon(!showRightIcon || isOneToOne)
6492 .headerTextSecondary(conversationTitle)
6493 .alwaysShowReply(showRightIcon));
Selim Cinek88188f22017-09-19 16:46:56 -07006494 addExtras(mBuilder.mN.extras);
Selim Cinekafeed292017-12-12 17:32:44 -08006495 // also update the end margin if there is an image
6496 int endMargin = R.dimen.notification_content_margin_end;
6497 if (mBuilder.mN.hasLargeIcon() && showRightIcon) {
6498 endMargin = R.dimen.notification_content_plus_picture_margin_end;
6499 }
6500 contentView.setViewLayoutMarginEndDimen(R.id.notification_main_column, endMargin);
Selim Cinek88188f22017-09-19 16:46:56 -07006501 contentView.setInt(R.id.status_bar_latest_event_content, "setLayoutColor",
6502 mBuilder.resolveContrastColor());
6503 contentView.setIcon(R.id.status_bar_latest_event_content, "setLargeIcon",
6504 mBuilder.mN.mLargeIcon);
Selim Cinek2dd3e722018-01-19 11:06:06 -08006505 contentView.setCharSequence(R.id.status_bar_latest_event_content, "setNameReplacement",
6506 nameReplacement);
Selim Cinek88188f22017-09-19 16:46:56 -07006507 contentView.setBoolean(R.id.status_bar_latest_event_content, "setIsOneToOne",
6508 isOneToOne);
6509 contentView.setBundle(R.id.status_bar_latest_event_content, "setData",
6510 mBuilder.mN.extras);
Alex Hillsfc737de2016-03-23 17:33:02 -04006511 return contentView;
6512 }
6513
Selim Cinekf7409db2017-10-24 16:17:14 -07006514 private boolean hasOnlyWhiteSpaceSenders() {
6515 for (int i = 0; i < mMessages.size(); i++) {
6516 Message m = mMessages.get(i);
Selim Cinekcb8b9852017-12-15 18:01:52 -08006517 Person sender = m.getSenderPerson();
6518 if (sender != null && !isWhiteSpace(sender.getName())) {
Selim Cinekf7409db2017-10-24 16:17:14 -07006519 return false;
6520 }
6521 }
6522 return true;
6523 }
6524
6525 private boolean isWhiteSpace(CharSequence sender) {
6526 if (TextUtils.isEmpty(sender)) {
6527 return true;
6528 }
6529 if (sender.toString().matches("^\\s*$")) {
6530 return true;
6531 }
6532 // Let's check if we only have 0 whitespace chars. Some apps did this as a workaround
6533 // For the presentation that we had.
6534 for (int i = 0; i < sender.length(); i++) {
6535 char c = sender.charAt(i);
6536 if (c != '\u200B') {
6537 return false;
6538 }
6539 }
6540 return true;
6541 }
6542
Selim Cinek88188f22017-09-19 16:46:56 -07006543 private CharSequence createConversationTitleFromMessages() {
6544 ArraySet<CharSequence> names = new ArraySet<>();
6545 for (int i = 0; i < mMessages.size(); i++) {
6546 Message m = mMessages.get(i);
Selim Cinekcb8b9852017-12-15 18:01:52 -08006547 Person sender = m.getSenderPerson();
Selim Cinek88188f22017-09-19 16:46:56 -07006548 if (sender != null) {
Selim Cinekcb8b9852017-12-15 18:01:52 -08006549 names.add(sender.getName());
Selim Cinek88188f22017-09-19 16:46:56 -07006550 }
6551 }
6552 SpannableStringBuilder title = new SpannableStringBuilder();
6553 int size = names.size();
6554 for (int i = 0; i < size; i++) {
6555 CharSequence name = names.valueAt(i);
6556 if (!TextUtils.isEmpty(title)) {
6557 title.append(", ");
6558 }
6559 title.append(BidiFormatter.getInstance().unicodeWrap(name));
6560 }
6561 return title;
6562 }
6563
Adrian Roosdedd1df2016-04-26 16:38:47 -07006564 /**
6565 * @hide
6566 */
6567 @Override
Selim Cinek87ed69b2017-02-09 15:59:43 -08006568 public RemoteViews makeHeadsUpContentView(boolean increasedHeight) {
Selim Cinekafeed292017-12-12 17:32:44 -08006569 RemoteViews remoteViews = makeBigContentView(true /* showRightIcon */);
Selim Cineke62255c2017-09-28 18:23:23 -07006570 remoteViews.setInt(R.id.notification_messaging, "setMaxDisplayedLines", 1);
6571 return remoteViews;
Adrian Roosdedd1df2016-04-26 16:38:47 -07006572 }
6573
Adrian Roosc1a80b02016-04-05 14:54:55 -07006574 private static TextAppearanceSpan makeFontColorSpan(int color) {
6575 return new TextAppearanceSpan(null, 0, 0,
6576 ColorStateList.valueOf(color), null);
6577 }
6578
Alex Hillsd9b04d92016-04-11 16:38:16 -04006579 public static final class Message {
6580
6581 static final String KEY_TEXT = "text";
6582 static final String KEY_TIMESTAMP = "time";
6583 static final String KEY_SENDER = "sender";
Selim Cinekcb8b9852017-12-15 18:01:52 -08006584 static final String KEY_SENDER_PERSON = "sender_person";
Alex Hillsd9b04d92016-04-11 16:38:16 -04006585 static final String KEY_DATA_MIME_TYPE = "type";
6586 static final String KEY_DATA_URI= "uri";
Shane Brennan5a871862017-03-11 13:14:17 -08006587 static final String KEY_EXTRAS_BUNDLE = "extras";
Alex Hillsfc737de2016-03-23 17:33:02 -04006588
6589 private final CharSequence mText;
6590 private final long mTimestamp;
Selim Cinekcb8b9852017-12-15 18:01:52 -08006591 @Nullable
6592 private final Person mSender;
Alex Hillsfc737de2016-03-23 17:33:02 -04006593
Shane Brennan5a871862017-03-11 13:14:17 -08006594 private Bundle mExtras = new Bundle();
Alex Hillsfc737de2016-03-23 17:33:02 -04006595 private String mDataMimeType;
6596 private Uri mDataUri;
6597
6598 /**
6599 * Constructor
6600 * @param text A {@link CharSequence} to be displayed as the message content
6601 * @param timestamp Time at which the message arrived
6602 * @param sender A {@link CharSequence} to be used for displaying the name of the
6603 * sender. Should be <code>null</code> for messages by the current user, in which case
6604 * the platform will insert {@link MessagingStyle#getUserDisplayName()}.
6605 * Should be unique amongst all individuals in the conversation, and should be
6606 * consistent during re-posts of the notification.
Selim Cinekcb8b9852017-12-15 18:01:52 -08006607 *
6608 * @deprecated use {@code Message(CharSequence, long, Person)}
Alex Hillsfc737de2016-03-23 17:33:02 -04006609 */
6610 public Message(CharSequence text, long timestamp, CharSequence sender){
Selim Cinekcb8b9852017-12-15 18:01:52 -08006611 this(text, timestamp, sender == null ? null : new Person().setName(sender));
6612 }
6613
6614 /**
6615 * Constructor
6616 * @param text A {@link CharSequence} to be displayed as the message content
6617 * @param timestamp Time at which the message arrived
6618 * @param sender The {@link Person} who sent the message.
6619 * Should be <code>null</code> for messages by the current user, in which case
6620 * the platform will insert the user set in {@code MessagingStyle(Person)}.
6621 * <p>
6622 * The person provided should contain an Icon, set with {@link Person#setIcon(Icon)}
6623 * and also have a name provided with {@link Person#setName(CharSequence)}. If multiple
6624 * users have the same name, consider providing a key with {@link Person#setKey(String)}
6625 * in order to differentiate between the different users.
6626 * </p>
6627 */
6628 public Message(CharSequence text, long timestamp, @Nullable Person sender){
Alex Hillsfc737de2016-03-23 17:33:02 -04006629 mText = text;
6630 mTimestamp = timestamp;
6631 mSender = sender;
6632 }
6633
6634 /**
6635 * Sets a binary blob of data and an associated MIME type for a message. In the case
6636 * where the platform doesn't support the MIME type, the original text provided in the
6637 * constructor will be used.
6638 * @param dataMimeType The MIME type of the content. See
6639 * <a href="{@docRoot}notifications/messaging.html"> for the list of supported MIME
6640 * types on Android and Android Wear.
6641 * @param dataUri The uri containing the content whose type is given by the MIME type.
6642 * <p class="note">
6643 * <ol>
6644 * <li>Notification Listeners including the System UI need permission to access the
6645 * data the Uri points to. The recommended ways to do this are:</li>
6646 * <li>Store the data in your own ContentProvider, making sure that other apps have
6647 * the correct permission to access your provider. The preferred mechanism for
6648 * providing access is to use per-URI permissions which are temporary and only
6649 * grant access to the receiving application. An easy way to create a
6650 * ContentProvider like this is to use the FileProvider helper class.</li>
6651 * <li>Use the system MediaStore. The MediaStore is primarily aimed at video, audio
6652 * and image MIME types, however beginning with Android 3.0 (API level 11) it can
6653 * also store non-media types (see MediaStore.Files for more info). Files can be
6654 * inserted into the MediaStore using scanFile() after which a content:// style
6655 * Uri suitable for sharing is passed to the provided onScanCompleted() callback.
6656 * Note that once added to the system MediaStore the content is accessible to any
6657 * app on the device.</li>
6658 * </ol>
6659 * @return this object for method chaining
6660 */
6661 public Message setData(String dataMimeType, Uri dataUri) {
6662 mDataMimeType = dataMimeType;
6663 mDataUri = dataUri;
6664 return this;
6665 }
6666
Alex Hillsfc737de2016-03-23 17:33:02 -04006667 /**
6668 * Get the text to be used for this message, or the fallback text if a type and content
6669 * Uri have been set
6670 */
6671 public CharSequence getText() {
6672 return mText;
6673 }
6674
6675 /**
6676 * Get the time at which this message arrived
6677 */
6678 public long getTimestamp() {
6679 return mTimestamp;
6680 }
6681
6682 /**
Shane Brennan5a871862017-03-11 13:14:17 -08006683 * Get the extras Bundle for this message.
6684 */
6685 public Bundle getExtras() {
6686 return mExtras;
6687 }
6688
6689 /**
Alex Hillsfc737de2016-03-23 17:33:02 -04006690 * Get the text used to display the contact's name in the messaging experience
Selim Cinekcb8b9852017-12-15 18:01:52 -08006691 *
6692 * @deprecated use {@link #getSenderPerson()}
Alex Hillsfc737de2016-03-23 17:33:02 -04006693 */
6694 public CharSequence getSender() {
Selim Cinekcb8b9852017-12-15 18:01:52 -08006695 return mSender == null ? null : mSender.getName();
6696 }
6697
6698 /**
6699 * Get the sender associated with this message.
6700 */
6701 @Nullable
6702 public Person getSenderPerson() {
Alex Hillsfc737de2016-03-23 17:33:02 -04006703 return mSender;
6704 }
6705
6706 /**
6707 * Get the MIME type of the data pointed to by the Uri
6708 */
6709 public String getDataMimeType() {
6710 return mDataMimeType;
6711 }
6712
6713 /**
6714 * Get the the Uri pointing to the content of the message. Can be null, in which case
6715 * {@see #getText()} is used.
6716 */
6717 public Uri getDataUri() {
6718 return mDataUri;
6719 }
6720
Alex Hillsd9b04d92016-04-11 16:38:16 -04006721 private Bundle toBundle() {
6722 Bundle bundle = new Bundle();
Alex Hillsfc737de2016-03-23 17:33:02 -04006723 if (mText != null) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04006724 bundle.putCharSequence(KEY_TEXT, mText);
Alex Hillsfc737de2016-03-23 17:33:02 -04006725 }
Alex Hillsd9b04d92016-04-11 16:38:16 -04006726 bundle.putLong(KEY_TIMESTAMP, mTimestamp);
Alex Hillsfc737de2016-03-23 17:33:02 -04006727 if (mSender != null) {
Selim Cinekcb8b9852017-12-15 18:01:52 -08006728 // Legacy listeners need this
6729 bundle.putCharSequence(KEY_SENDER, mSender.getName());
6730 bundle.putParcelable(KEY_SENDER_PERSON, mSender);
Alex Hillsfc737de2016-03-23 17:33:02 -04006731 }
6732 if (mDataMimeType != null) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04006733 bundle.putString(KEY_DATA_MIME_TYPE, mDataMimeType);
Alex Hillsfc737de2016-03-23 17:33:02 -04006734 }
6735 if (mDataUri != null) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04006736 bundle.putParcelable(KEY_DATA_URI, mDataUri);
Alex Hillsfc737de2016-03-23 17:33:02 -04006737 }
Shane Brennan5a871862017-03-11 13:14:17 -08006738 if (mExtras != null) {
6739 bundle.putBundle(KEY_EXTRAS_BUNDLE, mExtras);
6740 }
Alex Hillsd9b04d92016-04-11 16:38:16 -04006741 return bundle;
Alex Hillsfc737de2016-03-23 17:33:02 -04006742 }
6743
Alex Hillsd9b04d92016-04-11 16:38:16 -04006744 static Bundle[] getBundleArrayForMessages(List<Message> messages) {
6745 Bundle[] bundles = new Bundle[messages.size()];
6746 final int N = messages.size();
6747 for (int i = 0; i < N; i++) {
6748 bundles[i] = messages.get(i).toBundle();
6749 }
6750 return bundles;
6751 }
6752
Selim Cinek88188f22017-09-19 16:46:56 -07006753 /**
6754 * @return A list of messages read from the bundles.
6755 *
6756 * @hide
6757 */
6758 public static List<Message> getMessagesFromBundleArray(Parcelable[] bundles) {
6759 if (bundles == null) {
6760 return new ArrayList<>();
6761 }
Alex Hillsd9b04d92016-04-11 16:38:16 -04006762 List<Message> messages = new ArrayList<>(bundles.length);
6763 for (int i = 0; i < bundles.length; i++) {
Adrian Roosdedd1df2016-04-26 16:38:47 -07006764 if (bundles[i] instanceof Bundle) {
6765 Message message = getMessageFromBundle((Bundle)bundles[i]);
6766 if (message != null) {
6767 messages.add(message);
6768 }
Alex Hillsd9b04d92016-04-11 16:38:16 -04006769 }
6770 }
6771 return messages;
6772 }
6773
6774 static Message getMessageFromBundle(Bundle bundle) {
6775 try {
Adrian Roosfbddd2c2016-05-13 12:57:20 -07006776 if (!bundle.containsKey(KEY_TEXT) || !bundle.containsKey(KEY_TIMESTAMP)) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04006777 return null;
6778 } else {
Selim Cinekcb8b9852017-12-15 18:01:52 -08006779
6780 Person senderPerson = bundle.getParcelable(KEY_SENDER_PERSON);
6781 if (senderPerson == null) {
6782 // Legacy apps that use compat don't actually provide the sender objects
6783 // We need to fix the compat version to provide people / use
6784 // the native api instead
6785 CharSequence senderName = bundle.getCharSequence(KEY_SENDER);
6786 if (senderName != null) {
6787 senderPerson = new Person().setName(senderName);
6788 }
6789 }
Alex Hillsd9b04d92016-04-11 16:38:16 -04006790 Message message = new Message(bundle.getCharSequence(KEY_TEXT),
Selim Cinekcb8b9852017-12-15 18:01:52 -08006791 bundle.getLong(KEY_TIMESTAMP),
6792 senderPerson);
Alex Hillsd9b04d92016-04-11 16:38:16 -04006793 if (bundle.containsKey(KEY_DATA_MIME_TYPE) &&
6794 bundle.containsKey(KEY_DATA_URI)) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04006795 message.setData(bundle.getString(KEY_DATA_MIME_TYPE),
6796 (Uri) bundle.getParcelable(KEY_DATA_URI));
Alex Hillsfc737de2016-03-23 17:33:02 -04006797 }
Shane Brennan5a871862017-03-11 13:14:17 -08006798 if (bundle.containsKey(KEY_EXTRAS_BUNDLE)) {
6799 message.getExtras().putAll(bundle.getBundle(KEY_EXTRAS_BUNDLE));
6800 }
Alex Hillsd9b04d92016-04-11 16:38:16 -04006801 return message;
6802 }
6803 } catch (ClassCastException e) {
6804 return null;
6805 }
6806 }
Alex Hillsfc737de2016-03-23 17:33:02 -04006807 }
6808 }
6809
6810 /**
Daniel Sandler879c5e02012-04-17 16:46:51 -04006811 * Helper class for generating large-format notifications that include a list of (up to 5) strings.
Joe Malin8d40d042012-11-05 11:36:40 -08006812 *
Robert Ly91c5ce32014-06-08 15:37:00 -07006813 * Here's how you'd set the <code>InboxStyle</code> on a notification:
Daniel Sandler879c5e02012-04-17 16:46:51 -04006814 * <pre class="prettyprint">
Robert Ly91c5ce32014-06-08 15:37:00 -07006815 * Notification notif = new Notification.Builder(mContext)
6816 * .setContentTitle(&quot;5 New mails from &quot; + sender.toString())
6817 * .setContentText(subject)
6818 * .setSmallIcon(R.drawable.new_mail)
6819 * .setLargeIcon(aBitmap)
6820 * .setStyle(new Notification.InboxStyle()
6821 * .addLine(str1)
6822 * .addLine(str2)
6823 * .setContentTitle(&quot;&quot;)
6824 * .setSummaryText(&quot;+3 more&quot;))
6825 * .build();
Daniel Sandler879c5e02012-04-17 16:46:51 -04006826 * </pre>
Joe Malin8d40d042012-11-05 11:36:40 -08006827 *
Daniel Sandler879c5e02012-04-17 16:46:51 -04006828 * @see Notification#bigContentView
6829 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006830 public static class InboxStyle extends Style {
Daniel Sandler879c5e02012-04-17 16:46:51 -04006831 private ArrayList<CharSequence> mTexts = new ArrayList<CharSequence>(5);
6832
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006833 public InboxStyle() {
6834 }
6835
Adrian Roosf5faf9d2016-05-23 13:56:15 -07006836 /**
6837 * @deprecated use {@code InboxStyle()}.
6838 */
6839 @Deprecated
Daniel Sandler879c5e02012-04-17 16:46:51 -04006840 public InboxStyle(Builder builder) {
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006841 setBuilder(builder);
Daniel Sandler879c5e02012-04-17 16:46:51 -04006842 }
6843
Chris Wrend6297db2012-05-03 16:20:13 -04006844 /**
6845 * Overrides ContentTitle in the big form of the template.
6846 * This defaults to the value passed to setContentTitle().
6847 */
6848 public InboxStyle setBigContentTitle(CharSequence title) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04006849 internalSetBigContentTitle(safeCharSequence(title));
Chris Wrend6297db2012-05-03 16:20:13 -04006850 return this;
6851 }
6852
6853 /**
6854 * Set the first line of text after the detail section in the big form of the template.
6855 */
6856 public InboxStyle setSummaryText(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04006857 internalSetSummaryText(safeCharSequence(cs));
Chris Wrend6297db2012-05-03 16:20:13 -04006858 return this;
6859 }
6860
Chris Wren0bd664d2012-08-01 13:56:56 -04006861 /**
6862 * Append a line to the digest section of the Inbox notification.
6863 */
Daniel Sandler879c5e02012-04-17 16:46:51 -04006864 public InboxStyle addLine(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04006865 mTexts.add(safeCharSequence(cs));
Daniel Sandler879c5e02012-04-17 16:46:51 -04006866 return this;
6867 }
6868
Daniel Sandlerf45564e2013-04-15 15:05:08 -04006869 /**
6870 * @hide
6871 */
6872 public void addExtras(Bundle extras) {
6873 super.addExtras(extras);
Christoph Studer4600f9b2014-07-22 22:44:43 +02006874
Daniel Sandlerf45564e2013-04-15 15:05:08 -04006875 CharSequence[] a = new CharSequence[mTexts.size()];
6876 extras.putCharSequenceArray(EXTRA_TEXT_LINES, mTexts.toArray(a));
6877 }
6878
Christoph Studer4600f9b2014-07-22 22:44:43 +02006879 /**
6880 * @hide
6881 */
6882 @Override
6883 protected void restoreFromExtras(Bundle extras) {
6884 super.restoreFromExtras(extras);
6885
6886 mTexts.clear();
6887 if (extras.containsKey(EXTRA_TEXT_LINES)) {
6888 Collections.addAll(mTexts, extras.getCharSequenceArray(EXTRA_TEXT_LINES));
6889 }
6890 }
6891
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006892 /**
6893 * @hide
6894 */
6895 public RemoteViews makeBigContentView() {
Selim Cinekc848c3a2016-01-13 15:27:30 -08006896 // Remove the content text so it disappears unless you have a summary
Christoph Studer4600f9b2014-07-22 22:44:43 +02006897 // Nasty
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006898 CharSequence oldBuilderContentText = mBuilder.mN.extras.getCharSequence(EXTRA_TEXT);
6899 mBuilder.getAllExtras().putCharSequence(EXTRA_TEXT, null);
Christoph Studer4600f9b2014-07-22 22:44:43 +02006900
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01006901 RemoteViews contentView = getStandardView(mBuilder.getInboxLayoutResource());
Daniel Sandler619738c2012-06-07 16:33:08 -04006902
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006903 mBuilder.getAllExtras().putCharSequence(EXTRA_TEXT, oldBuilderContentText);
Christoph Studer4600f9b2014-07-22 22:44:43 +02006904
Chris Wrend6297db2012-05-03 16:20:13 -04006905 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 -04006906 R.id.inbox_text4, R.id.inbox_text5, R.id.inbox_text6};
Chris Wrend6297db2012-05-03 16:20:13 -04006907
Chris Wren4ed80d52012-05-17 09:30:03 -04006908 // Make sure all rows are gone in case we reuse a view.
6909 for (int rowId : rowIds) {
6910 contentView.setViewVisibility(rowId, View.GONE);
6911 }
6912
Daniel Sandler879c5e02012-04-17 16:46:51 -04006913 int i=0;
Selim Cinek07c80172016-04-21 16:40:47 -07006914 int topPadding = mBuilder.mContext.getResources().getDimensionPixelSize(
6915 R.dimen.notification_inbox_item_top_padding);
Selim Cinek247fa012016-02-18 09:50:48 -08006916 boolean first = true;
Selim Cinek07c80172016-04-21 16:40:47 -07006917 int onlyViewId = 0;
6918 int maxRows = rowIds.length;
6919 if (mBuilder.mActions.size() > 0) {
6920 maxRows--;
6921 }
6922 while (i < mTexts.size() && i < maxRows) {
Daniel Sandler879c5e02012-04-17 16:46:51 -04006923 CharSequence str = mTexts.get(i);
Selim Cinek07c80172016-04-21 16:40:47 -07006924 if (!TextUtils.isEmpty(str)) {
Daniel Sandler879c5e02012-04-17 16:46:51 -04006925 contentView.setViewVisibility(rowIds[i], View.VISIBLE);
Selim Cinek48f66b72017-08-18 16:17:51 -07006926 contentView.setTextViewText(rowIds[i],
6927 mBuilder.processTextSpans(mBuilder.processLegacyText(str)));
Selim Cinek7b9605b2017-01-19 17:36:00 -08006928 mBuilder.setTextViewColorSecondary(contentView, rowIds[i]);
Selim Cinek07c80172016-04-21 16:40:47 -07006929 contentView.setViewPadding(rowIds[i], 0, topPadding, 0, 0);
Selim Cinek247fa012016-02-18 09:50:48 -08006930 handleInboxImageMargin(contentView, rowIds[i], first);
Selim Cinek07c80172016-04-21 16:40:47 -07006931 if (first) {
6932 onlyViewId = rowIds[i];
6933 } else {
6934 onlyViewId = 0;
6935 }
Selim Cinek247fa012016-02-18 09:50:48 -08006936 first = false;
Daniel Sandler879c5e02012-04-17 16:46:51 -04006937 }
6938 i++;
6939 }
Selim Cinek07c80172016-04-21 16:40:47 -07006940 if (onlyViewId != 0) {
6941 // We only have 1 entry, lets make it look like the normal Text of a Bigtext
6942 topPadding = mBuilder.mContext.getResources().getDimensionPixelSize(
6943 R.dimen.notification_text_margin_top);
6944 contentView.setViewPadding(onlyViewId, 0, topPadding, 0, 0);
6945 }
Selim Cinek1e0bf612015-11-20 15:57:26 -08006946
Daniel Sandler879c5e02012-04-17 16:46:51 -04006947 return contentView;
6948 }
Selim Cinek1e0bf612015-11-20 15:57:26 -08006949
Selim Cinek247fa012016-02-18 09:50:48 -08006950 private void handleInboxImageMargin(RemoteViews contentView, int id, boolean first) {
Selim Cinek1e0bf612015-11-20 15:57:26 -08006951 int endMargin = 0;
Selim Cinek247fa012016-02-18 09:50:48 -08006952 if (first) {
6953 final int max = mBuilder.mN.extras.getInt(EXTRA_PROGRESS_MAX, 0);
6954 final boolean ind = mBuilder.mN.extras.getBoolean(EXTRA_PROGRESS_INDETERMINATE);
6955 boolean hasProgress = max != 0 || ind;
Selim Cinek279fa862016-06-14 10:57:25 -07006956 if (mBuilder.mN.hasLargeIcon() && !hasProgress) {
Adrian Roos2d5dbba2016-06-08 17:11:53 -07006957 endMargin = R.dimen.notification_content_picture_margin;
Selim Cinek247fa012016-02-18 09:50:48 -08006958 }
Selim Cinek1e0bf612015-11-20 15:57:26 -08006959 }
Adrian Roos2d5dbba2016-06-08 17:11:53 -07006960 contentView.setViewLayoutMarginEndDimen(id, endMargin);
Selim Cinek1e0bf612015-11-20 15:57:26 -08006961 }
Daniel Sandler879c5e02012-04-17 16:46:51 -04006962 }
Dan Sandler842dd772014-05-15 09:36:47 -04006963
6964 /**
6965 * Notification style for media playback notifications.
6966 *
6967 * In the expanded form, {@link Notification#bigContentView}, up to 5
6968 * {@link Notification.Action}s specified with
Dan Sandler86647982015-05-13 23:41:13 -04006969 * {@link Notification.Builder#addAction(Action) addAction} will be
Dan Sandler842dd772014-05-15 09:36:47 -04006970 * shown as icon-only pushbuttons, suitable for transport controls. The Bitmap given to
6971 * {@link Notification.Builder#setLargeIcon(android.graphics.Bitmap) setLargeIcon()} will be
6972 * treated as album artwork.
Selim Cinek99104832017-01-25 14:47:33 -08006973 * <p>
Dan Sandler842dd772014-05-15 09:36:47 -04006974 * Unlike the other styles provided here, MediaStyle can also modify the standard-size
6975 * {@link Notification#contentView}; by providing action indices to
Christoph Studerfde6f4d2014-12-12 13:23:26 +01006976 * {@link #setShowActionsInCompactView(int...)} you can promote up to 3 actions to be displayed
Dan Sandler842dd772014-05-15 09:36:47 -04006977 * in the standard view alongside the usual content.
Selim Cinek99104832017-01-25 14:47:33 -08006978 * <p>
Bryan Mawhinney6be8de32014-07-18 10:35:12 +01006979 * Notifications created with MediaStyle will have their category set to
6980 * {@link Notification#CATEGORY_TRANSPORT CATEGORY_TRANSPORT} unless you set a different
6981 * category using {@link Notification.Builder#setCategory(String) setCategory()}.
Selim Cinek99104832017-01-25 14:47:33 -08006982 * <p>
Jeff Browndba34ba2014-06-24 20:46:03 -07006983 * Finally, if you attach a {@link android.media.session.MediaSession.Token} using
6984 * {@link android.app.Notification.MediaStyle#setMediaSession(MediaSession.Token)},
Dan Sandler842dd772014-05-15 09:36:47 -04006985 * the System UI can identify this as a notification representing an active media session
6986 * and respond accordingly (by showing album artwork in the lockscreen, for example).
6987 *
Selim Cinek99104832017-01-25 14:47:33 -08006988 * <p>
6989 * Starting at {@link android.os.Build.VERSION_CODES#O Android O} any notification that has a
6990 * media session attached with {@link #setMediaSession(MediaSession.Token)} will be colorized.
6991 * You can opt-out of this behavior by using {@link Notification.Builder#setColorized(boolean)}.
6992 * <p>
6993 *
Dan Sandler842dd772014-05-15 09:36:47 -04006994 * To use this style with your Notification, feed it to
6995 * {@link Notification.Builder#setStyle(android.app.Notification.Style)} like so:
6996 * <pre class="prettyprint">
6997 * Notification noti = new Notification.Builder()
6998 * .setSmallIcon(R.drawable.ic_stat_player)
Christoph Studere935fe92014-11-24 14:18:06 +01006999 * .setContentTitle(&quot;Track title&quot;)
7000 * .setContentText(&quot;Artist - Album&quot;)
7001 * .setLargeIcon(albumArtBitmap))
Bryan Mawhinney6be8de32014-07-18 10:35:12 +01007002 * .setStyle(<b>new Notification.MediaStyle()</b>
7003 * .setMediaSession(mySession))
Dan Sandler842dd772014-05-15 09:36:47 -04007004 * .build();
7005 * </pre>
7006 *
7007 * @see Notification#bigContentView
Selim Cinek99104832017-01-25 14:47:33 -08007008 * @see Notification.Builder#setColorized(boolean)
Dan Sandler842dd772014-05-15 09:36:47 -04007009 */
7010 public static class MediaStyle extends Style {
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02007011 static final int MAX_MEDIA_BUTTONS_IN_COMPACT = 3;
Dan Sandler842dd772014-05-15 09:36:47 -04007012 static final int MAX_MEDIA_BUTTONS = 5;
7013
7014 private int[] mActionsToShowInCompact = null;
Jeff Browndba34ba2014-06-24 20:46:03 -07007015 private MediaSession.Token mToken;
Dan Sandler842dd772014-05-15 09:36:47 -04007016
7017 public MediaStyle() {
7018 }
7019
Adrian Roosf5faf9d2016-05-23 13:56:15 -07007020 /**
7021 * @deprecated use {@code MediaStyle()}.
7022 */
7023 @Deprecated
Dan Sandler842dd772014-05-15 09:36:47 -04007024 public MediaStyle(Builder builder) {
7025 setBuilder(builder);
7026 }
7027
7028 /**
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02007029 * 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 -04007030 * notification view.
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02007031 *
7032 * @param actions the indices of the actions to show in the compact notification view
Dan Sandler842dd772014-05-15 09:36:47 -04007033 */
7034 public MediaStyle setShowActionsInCompactView(int...actions) {
7035 mActionsToShowInCompact = actions;
7036 return this;
7037 }
7038
7039 /**
Jeff Browndba34ba2014-06-24 20:46:03 -07007040 * Attach a {@link android.media.session.MediaSession.Token} to this Notification
7041 * to provide additional playback information and control to the SystemUI.
Dan Sandler842dd772014-05-15 09:36:47 -04007042 */
Jeff Browndba34ba2014-06-24 20:46:03 -07007043 public MediaStyle setMediaSession(MediaSession.Token token) {
Dan Sandler842dd772014-05-15 09:36:47 -04007044 mToken = token;
7045 return this;
7046 }
7047
Christoph Studer4600f9b2014-07-22 22:44:43 +02007048 /**
7049 * @hide
7050 */
Dan Sandler842dd772014-05-15 09:36:47 -04007051 @Override
7052 public Notification buildStyled(Notification wip) {
Christoph Studer4600f9b2014-07-22 22:44:43 +02007053 super.buildStyled(wip);
Bryan Mawhinney6be8de32014-07-18 10:35:12 +01007054 if (wip.category == null) {
7055 wip.category = Notification.CATEGORY_TRANSPORT;
7056 }
Dan Sandler842dd772014-05-15 09:36:47 -04007057 return wip;
7058 }
7059
Christoph Studer4600f9b2014-07-22 22:44:43 +02007060 /**
7061 * @hide
7062 */
7063 @Override
Selim Cinek7d1009b2017-01-25 15:28:28 -08007064 public RemoteViews makeContentView(boolean increasedHeight) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04007065 return makeMediaContentView();
Christoph Studer4600f9b2014-07-22 22:44:43 +02007066 }
7067
7068 /**
7069 * @hide
7070 */
7071 @Override
Julia Reynoldsd9228f12015-10-20 10:37:27 -04007072 public RemoteViews makeBigContentView() {
7073 return makeMediaBigContentView();
Christoph Studer4600f9b2014-07-22 22:44:43 +02007074 }
7075
Selim Cinekcc10bfb2016-02-10 16:24:21 -08007076 /**
7077 * @hide
7078 */
7079 @Override
Selim Cinek87ed69b2017-02-09 15:59:43 -08007080 public RemoteViews makeHeadsUpContentView(boolean increasedHeight) {
Selim Cinekcc10bfb2016-02-10 16:24:21 -08007081 RemoteViews expanded = makeMediaBigContentView();
7082 return expanded != null ? expanded : makeMediaContentView();
7083 }
7084
Dan Sandler842dd772014-05-15 09:36:47 -04007085 /** @hide */
7086 @Override
7087 public void addExtras(Bundle extras) {
7088 super.addExtras(extras);
7089
7090 if (mToken != null) {
7091 extras.putParcelable(EXTRA_MEDIA_SESSION, mToken);
7092 }
Bryan Mawhinneye191f902014-07-22 12:50:09 +01007093 if (mActionsToShowInCompact != null) {
7094 extras.putIntArray(EXTRA_COMPACT_ACTIONS, mActionsToShowInCompact);
7095 }
Dan Sandler842dd772014-05-15 09:36:47 -04007096 }
7097
Christoph Studer4600f9b2014-07-22 22:44:43 +02007098 /**
7099 * @hide
7100 */
7101 @Override
7102 protected void restoreFromExtras(Bundle extras) {
7103 super.restoreFromExtras(extras);
7104
7105 if (extras.containsKey(EXTRA_MEDIA_SESSION)) {
7106 mToken = extras.getParcelable(EXTRA_MEDIA_SESSION);
7107 }
7108 if (extras.containsKey(EXTRA_COMPACT_ACTIONS)) {
7109 mActionsToShowInCompact = extras.getIntArray(EXTRA_COMPACT_ACTIONS);
7110 }
7111 }
7112
Selim Cinek5bf069a2015-11-10 19:14:27 -05007113 private RemoteViews generateMediaActionButton(Action action, int color) {
Dan Sandler842dd772014-05-15 09:36:47 -04007114 final boolean tombstone = (action.actionIntent == null);
Selim Cinekf33b1112015-07-15 17:45:11 -07007115 RemoteViews button = new BuilderRemoteViews(mBuilder.mContext.getApplicationInfo(),
Alan Viverette3cb07a462014-06-06 14:19:53 -07007116 R.layout.notification_material_media_action);
Dan Sandler68079d52015-07-22 10:45:30 -04007117 button.setImageViewIcon(R.id.action0, action.getIcon());
Anthony Chenad4d1582017-04-10 16:07:58 -07007118
7119 // If the action buttons should not be tinted, then just use the default
7120 // notification color. Otherwise, just use the passed-in color.
7121 int tintColor = mBuilder.shouldTintActionButtons() || mBuilder.isColorized()
7122 ? color
7123 : NotificationColorUtil.resolveColor(mBuilder.mContext,
7124 Notification.COLOR_DEFAULT);
7125
Sunny Goyal5b153922017-09-21 21:00:36 -07007126 button.setDrawableTint(R.id.action0, false, tintColor,
7127 PorterDuff.Mode.SRC_ATOP);
Dan Sandler842dd772014-05-15 09:36:47 -04007128 if (!tombstone) {
7129 button.setOnClickPendingIntent(R.id.action0, action.actionIntent);
7130 }
7131 button.setContentDescription(R.id.action0, action.title);
7132 return button;
7133 }
7134
7135 private RemoteViews makeMediaContentView() {
7136 RemoteViews view = mBuilder.applyStandardTemplate(
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02007137 R.layout.notification_template_material_media, false /* hasProgress */);
Dan Sandler842dd772014-05-15 09:36:47 -04007138
7139 final int numActions = mBuilder.mActions.size();
7140 final int N = mActionsToShowInCompact == null
7141 ? 0
7142 : Math.min(mActionsToShowInCompact.length, MAX_MEDIA_BUTTONS_IN_COMPACT);
7143 if (N > 0) {
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02007144 view.removeAllViews(com.android.internal.R.id.media_actions);
Dan Sandler842dd772014-05-15 09:36:47 -04007145 for (int i = 0; i < N; i++) {
7146 if (i >= numActions) {
7147 throw new IllegalArgumentException(String.format(
7148 "setShowActionsInCompactView: action %d out of bounds (max %d)",
7149 i, numActions - 1));
7150 }
7151
7152 final Action action = mBuilder.mActions.get(mActionsToShowInCompact[i]);
Selim Cinek5bf069a2015-11-10 19:14:27 -05007153 final RemoteViews button = generateMediaActionButton(action,
Selim Cinek99104832017-01-25 14:47:33 -08007154 getPrimaryHighlightColor());
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02007155 view.addView(com.android.internal.R.id.media_actions, button);
Dan Sandler842dd772014-05-15 09:36:47 -04007156 }
7157 }
Selim Cinekfdc738f2016-01-27 20:04:27 -08007158 handleImage(view);
7159 // handle the content margin
Adrian Roos2d5dbba2016-06-08 17:11:53 -07007160 int endMargin = R.dimen.notification_content_margin_end;
Selim Cinek279fa862016-06-14 10:57:25 -07007161 if (mBuilder.mN.hasLargeIcon()) {
Adrian Roos2d5dbba2016-06-08 17:11:53 -07007162 endMargin = R.dimen.notification_content_plus_picture_margin_end;
Selim Cinekfdc738f2016-01-27 20:04:27 -08007163 }
Adrian Roos2d5dbba2016-06-08 17:11:53 -07007164 view.setViewLayoutMarginEndDimen(R.id.notification_main_column, endMargin);
Dan Sandler842dd772014-05-15 09:36:47 -04007165 return view;
7166 }
7167
Selim Cinek99104832017-01-25 14:47:33 -08007168 private int getPrimaryHighlightColor() {
7169 return mBuilder.getPrimaryHighlightColor();
7170 }
7171
Dan Sandler842dd772014-05-15 09:36:47 -04007172 private RemoteViews makeMediaBigContentView() {
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02007173 final int actionCount = Math.min(mBuilder.mActions.size(), MAX_MEDIA_BUTTONS);
Selim Cinekcc10bfb2016-02-10 16:24:21 -08007174 // Dont add an expanded view if there is no more content to be revealed
7175 int actionsInCompact = mActionsToShowInCompact == null
7176 ? 0
7177 : Math.min(mActionsToShowInCompact.length, MAX_MEDIA_BUTTONS_IN_COMPACT);
Selim Cinek279fa862016-06-14 10:57:25 -07007178 if (!mBuilder.mN.hasLargeIcon() && actionCount <= actionsInCompact) {
Selim Cinekcc10bfb2016-02-10 16:24:21 -08007179 return null;
7180 }
Selim Cinek5bf069a2015-11-10 19:14:27 -05007181 RemoteViews big = mBuilder.applyStandardTemplate(
7182 R.layout.notification_template_material_big_media,
7183 false);
Dan Sandler842dd772014-05-15 09:36:47 -04007184
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02007185 if (actionCount > 0) {
7186 big.removeAllViews(com.android.internal.R.id.media_actions);
7187 for (int i = 0; i < actionCount; i++) {
Selim Cinek5bf069a2015-11-10 19:14:27 -05007188 final RemoteViews button = generateMediaActionButton(mBuilder.mActions.get(i),
Selim Cinek99104832017-01-25 14:47:33 -08007189 getPrimaryHighlightColor());
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02007190 big.addView(com.android.internal.R.id.media_actions, button);
Dan Sandler842dd772014-05-15 09:36:47 -04007191 }
7192 }
Selim Cinek5bf069a2015-11-10 19:14:27 -05007193 handleImage(big);
Dan Sandler842dd772014-05-15 09:36:47 -04007194 return big;
7195 }
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02007196
Selim Cinek5bf069a2015-11-10 19:14:27 -05007197 private void handleImage(RemoteViews contentView) {
Selim Cinek279fa862016-06-14 10:57:25 -07007198 if (mBuilder.mN.hasLargeIcon()) {
Adrian Roos2d5dbba2016-06-08 17:11:53 -07007199 contentView.setViewLayoutMarginEndDimen(R.id.line1, 0);
7200 contentView.setViewLayoutMarginEndDimen(R.id.text, 0);
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02007201 }
7202 }
7203
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02007204 /**
7205 * @hide
7206 */
7207 @Override
7208 protected boolean hasProgress() {
7209 return false;
7210 }
Dan Sandler842dd772014-05-15 09:36:47 -04007211 }
Griff Hazen61a9e862014-05-22 16:05:19 -07007212
Selim Cinek593610c2016-02-16 18:42:57 -08007213 /**
7214 * Notification style for custom views that are decorated by the system
7215 *
7216 * <p>Instead of providing a notification that is completely custom, a developer can set this
7217 * style and still obtain system decorations like the notification header with the expand
7218 * affordance and actions.
7219 *
7220 * <p>Use {@link android.app.Notification.Builder#setCustomContentView(RemoteViews)},
7221 * {@link android.app.Notification.Builder#setCustomBigContentView(RemoteViews)} and
7222 * {@link android.app.Notification.Builder#setCustomHeadsUpContentView(RemoteViews)} to set the
7223 * corresponding custom views to display.
7224 *
7225 * To use this style with your Notification, feed it to
7226 * {@link Notification.Builder#setStyle(android.app.Notification.Style)} like so:
7227 * <pre class="prettyprint">
7228 * Notification noti = new Notification.Builder()
7229 * .setSmallIcon(R.drawable.ic_stat_player)
7230 * .setLargeIcon(albumArtBitmap))
7231 * .setCustomContentView(contentView);
7232 * .setStyle(<b>new Notification.DecoratedCustomViewStyle()</b>)
7233 * .build();
7234 * </pre>
7235 */
7236 public static class DecoratedCustomViewStyle extends Style {
7237
7238 public DecoratedCustomViewStyle() {
7239 }
7240
Selim Cinek593610c2016-02-16 18:42:57 -08007241 /**
7242 * @hide
7243 */
7244 public boolean displayCustomViewInline() {
7245 return true;
7246 }
7247
7248 /**
7249 * @hide
7250 */
7251 @Override
Selim Cinek7d1009b2017-01-25 15:28:28 -08007252 public RemoteViews makeContentView(boolean increasedHeight) {
Selim Cinek593610c2016-02-16 18:42:57 -08007253 return makeStandardTemplateWithCustomContent(mBuilder.mN.contentView);
7254 }
7255
7256 /**
7257 * @hide
7258 */
7259 @Override
7260 public RemoteViews makeBigContentView() {
7261 return makeDecoratedBigContentView();
7262 }
7263
7264 /**
7265 * @hide
7266 */
7267 @Override
Selim Cinek87ed69b2017-02-09 15:59:43 -08007268 public RemoteViews makeHeadsUpContentView(boolean increasedHeight) {
Selim Cinek593610c2016-02-16 18:42:57 -08007269 return makeDecoratedHeadsUpContentView();
7270 }
7271
Selim Cinek593610c2016-02-16 18:42:57 -08007272 private RemoteViews makeDecoratedHeadsUpContentView() {
7273 RemoteViews headsUpContentView = mBuilder.mN.headsUpContentView == null
7274 ? mBuilder.mN.contentView
7275 : mBuilder.mN.headsUpContentView;
7276 if (mBuilder.mActions.size() == 0) {
7277 return makeStandardTemplateWithCustomContent(headsUpContentView);
7278 }
7279 RemoteViews remoteViews = mBuilder.applyStandardTemplateWithActions(
7280 mBuilder.getBigBaseLayoutResource());
Selim Cinek247fa012016-02-18 09:50:48 -08007281 buildIntoRemoteViewContent(remoteViews, headsUpContentView);
Selim Cinek593610c2016-02-16 18:42:57 -08007282 return remoteViews;
7283 }
7284
Selim Cinek593610c2016-02-16 18:42:57 -08007285 private RemoteViews makeStandardTemplateWithCustomContent(RemoteViews customContent) {
7286 RemoteViews remoteViews = mBuilder.applyStandardTemplate(
7287 mBuilder.getBaseLayoutResource());
Selim Cinek247fa012016-02-18 09:50:48 -08007288 buildIntoRemoteViewContent(remoteViews, customContent);
Selim Cinek593610c2016-02-16 18:42:57 -08007289 return remoteViews;
7290 }
7291
Selim Cinek593610c2016-02-16 18:42:57 -08007292 private RemoteViews makeDecoratedBigContentView() {
7293 RemoteViews bigContentView = mBuilder.mN.bigContentView == null
7294 ? mBuilder.mN.contentView
7295 : mBuilder.mN.bigContentView;
7296 if (mBuilder.mActions.size() == 0) {
7297 return makeStandardTemplateWithCustomContent(bigContentView);
7298 }
7299 RemoteViews remoteViews = mBuilder.applyStandardTemplateWithActions(
7300 mBuilder.getBigBaseLayoutResource());
Selim Cinek247fa012016-02-18 09:50:48 -08007301 buildIntoRemoteViewContent(remoteViews, bigContentView);
Selim Cinek593610c2016-02-16 18:42:57 -08007302 return remoteViews;
7303 }
Selim Cinek247fa012016-02-18 09:50:48 -08007304
7305 private void buildIntoRemoteViewContent(RemoteViews remoteViews,
7306 RemoteViews customContent) {
Adrian Roos5081c0d2016-02-26 16:04:19 -08007307 if (customContent != null) {
Selim Cinekf91017e2016-03-14 12:25:09 -07007308 // Need to clone customContent before adding, because otherwise it can no longer be
7309 // parceled independently of remoteViews.
Adrian Roos5081c0d2016-02-26 16:04:19 -08007310 customContent = customContent.clone();
Anthony Chen8f5f3582017-04-11 11:18:37 -07007311 remoteViews.removeAllViewsExceptId(R.id.notification_main_column, R.id.progress);
7312 remoteViews.addView(R.id.notification_main_column, customContent, 0 /* index */);
Selim Cinekfc8073c2017-08-16 17:50:20 -07007313 remoteViews.setReapplyDisallowed();
Adrian Roos5081c0d2016-02-26 16:04:19 -08007314 }
Selim Cinek247fa012016-02-18 09:50:48 -08007315 // also update the end margin if there is an image
Adrian Roos2d5dbba2016-06-08 17:11:53 -07007316 int endMargin = R.dimen.notification_content_margin_end;
Selim Cinek279fa862016-06-14 10:57:25 -07007317 if (mBuilder.mN.hasLargeIcon()) {
Adrian Roos2d5dbba2016-06-08 17:11:53 -07007318 endMargin = R.dimen.notification_content_plus_picture_margin_end;
Selim Cinek247fa012016-02-18 09:50:48 -08007319 }
Adrian Roos2d5dbba2016-06-08 17:11:53 -07007320 remoteViews.setViewLayoutMarginEndDimen(R.id.notification_main_column, endMargin);
Selim Cinek247fa012016-02-18 09:50:48 -08007321 }
Selim Cinek593610c2016-02-16 18:42:57 -08007322 }
7323
Selim Cinek03eb3b72016-02-18 10:39:45 -08007324 /**
7325 * Notification style for media custom views that are decorated by the system
7326 *
7327 * <p>Instead of providing a media notification that is completely custom, a developer can set
7328 * this style and still obtain system decorations like the notification header with the expand
7329 * affordance and actions.
7330 *
7331 * <p>Use {@link android.app.Notification.Builder#setCustomContentView(RemoteViews)},
7332 * {@link android.app.Notification.Builder#setCustomBigContentView(RemoteViews)} and
7333 * {@link android.app.Notification.Builder#setCustomHeadsUpContentView(RemoteViews)} to set the
7334 * corresponding custom views to display.
Selim Cinek99104832017-01-25 14:47:33 -08007335 * <p>
7336 * Contrary to {@link MediaStyle} a developer has to opt-in to the colorizing of the
7337 * notification by using {@link Notification.Builder#setColorized(boolean)}.
7338 * <p>
Selim Cinek03eb3b72016-02-18 10:39:45 -08007339 * To use this style with your Notification, feed it to
7340 * {@link Notification.Builder#setStyle(android.app.Notification.Style)} like so:
7341 * <pre class="prettyprint">
7342 * Notification noti = new Notification.Builder()
7343 * .setSmallIcon(R.drawable.ic_stat_player)
7344 * .setLargeIcon(albumArtBitmap))
7345 * .setCustomContentView(contentView);
7346 * .setStyle(<b>new Notification.DecoratedMediaCustomViewStyle()</b>
7347 * .setMediaSession(mySession))
7348 * .build();
7349 * </pre>
7350 *
7351 * @see android.app.Notification.DecoratedCustomViewStyle
7352 * @see android.app.Notification.MediaStyle
7353 */
7354 public static class DecoratedMediaCustomViewStyle extends MediaStyle {
7355
7356 public DecoratedMediaCustomViewStyle() {
7357 }
7358
Selim Cinek03eb3b72016-02-18 10:39:45 -08007359 /**
7360 * @hide
7361 */
7362 public boolean displayCustomViewInline() {
7363 return true;
7364 }
7365
7366 /**
7367 * @hide
7368 */
7369 @Override
Selim Cinek7d1009b2017-01-25 15:28:28 -08007370 public RemoteViews makeContentView(boolean increasedHeight) {
7371 RemoteViews remoteViews = super.makeContentView(false /* increasedHeight */);
Selim Cinek03eb3b72016-02-18 10:39:45 -08007372 return buildIntoRemoteView(remoteViews, R.id.notification_content_container,
7373 mBuilder.mN.contentView);
7374 }
7375
7376 /**
7377 * @hide
7378 */
7379 @Override
7380 public RemoteViews makeBigContentView() {
7381 RemoteViews customRemoteView = mBuilder.mN.bigContentView != null
7382 ? mBuilder.mN.bigContentView
7383 : mBuilder.mN.contentView;
7384 return makeBigContentViewWithCustomContent(customRemoteView);
7385 }
7386
7387 private RemoteViews makeBigContentViewWithCustomContent(RemoteViews customRemoteView) {
7388 RemoteViews remoteViews = super.makeBigContentView();
7389 if (remoteViews != null) {
7390 return buildIntoRemoteView(remoteViews, R.id.notification_main_column,
7391 customRemoteView);
7392 } else if (customRemoteView != mBuilder.mN.contentView){
Selim Cinek7d1009b2017-01-25 15:28:28 -08007393 remoteViews = super.makeContentView(false /* increasedHeight */);
Selim Cinek03eb3b72016-02-18 10:39:45 -08007394 return buildIntoRemoteView(remoteViews, R.id.notification_content_container,
7395 customRemoteView);
7396 } else {
7397 return null;
7398 }
7399 }
7400
7401 /**
7402 * @hide
7403 */
7404 @Override
Selim Cinek87ed69b2017-02-09 15:59:43 -08007405 public RemoteViews makeHeadsUpContentView(boolean increasedHeight) {
Selim Cinek03eb3b72016-02-18 10:39:45 -08007406 RemoteViews customRemoteView = mBuilder.mN.headsUpContentView != null
7407 ? mBuilder.mN.headsUpContentView
7408 : mBuilder.mN.contentView;
7409 return makeBigContentViewWithCustomContent(customRemoteView);
7410 }
7411
7412 private RemoteViews buildIntoRemoteView(RemoteViews remoteViews, int id,
7413 RemoteViews customContent) {
Adrian Roos5081c0d2016-02-26 16:04:19 -08007414 if (customContent != null) {
Selim Cinekf91017e2016-03-14 12:25:09 -07007415 // Need to clone customContent before adding, because otherwise it can no longer be
7416 // parceled independently of remoteViews.
Adrian Roos5081c0d2016-02-26 16:04:19 -08007417 customContent = customContent.clone();
Selim Cinek87c31532017-08-18 18:53:44 -07007418 customContent.overrideTextColors(mBuilder.getPrimaryTextColor());
Selim Cinekf91017e2016-03-14 12:25:09 -07007419 remoteViews.removeAllViews(id);
7420 remoteViews.addView(id, customContent);
Selim Cinekfc8073c2017-08-16 17:50:20 -07007421 remoteViews.setReapplyDisallowed();
Adrian Roos5081c0d2016-02-26 16:04:19 -08007422 }
Selim Cinek03eb3b72016-02-18 10:39:45 -08007423 return remoteViews;
7424 }
7425 }
7426
Selim Cineke7238dd2017-12-14 17:48:32 -08007427 /**
7428 * A Person associated with this Notification.
7429 */
7430 public static final class Person implements Parcelable {
7431 @Nullable private CharSequence mName;
7432 @Nullable private Icon mIcon;
7433 @Nullable private String mUri;
7434 @Nullable private String mKey;
7435
7436 protected Person(Parcel in) {
7437 mName = in.readCharSequence();
7438 if (in.readInt() != 0) {
7439 mIcon = Icon.CREATOR.createFromParcel(in);
7440 }
7441 mUri = in.readString();
7442 mKey = in.readString();
7443 }
7444
7445 /**
7446 * Create a new person.
7447 */
7448 public Person() {
7449 }
7450
7451 /**
7452 * Give this person a name.
7453 *
7454 * @param name the name of this person
7455 */
7456 public Person setName(@Nullable CharSequence name) {
7457 this.mName = name;
7458 return this;
7459 }
7460
7461 /**
7462 * Add an icon for this person.
7463 * <br />
7464 * This is currently only used for {@link MessagingStyle} notifications and should not be
7465 * provided otherwise, in order to save memory. The system will prefer this icon over any
7466 * images that are resolved from the URI.
7467 *
7468 * @param icon the icon of the person
7469 */
7470 public Person setIcon(@Nullable Icon icon) {
7471 this.mIcon = icon;
7472 return this;
7473 }
7474
7475 /**
7476 * Set a URI associated with this person.
7477 *
7478 * <P>
7479 * Depending on user preferences, adding a URI to a Person may allow the notification to
7480 * pass through interruption filters, if this notification is of
7481 * category {@link #CATEGORY_CALL} or {@link #CATEGORY_MESSAGE}.
7482 * The addition of people may also cause this notification to appear more prominently in
7483 * the user interface.
7484 * </P>
7485 *
7486 * <P>
7487 * The person should be specified by the {@code String} representation of a
7488 * {@link android.provider.ContactsContract.Contacts#CONTENT_LOOKUP_URI}.
7489 * </P>
7490 *
7491 * <P>The system will also attempt to resolve {@code mailto:} and {@code tel:} schema
7492 * URIs. The path part of these URIs must exist in the contacts database, in the
7493 * appropriate column, or the reference will be discarded as invalid. Telephone schema
7494 * URIs will be resolved by {@link android.provider.ContactsContract.PhoneLookup}.
7495 * </P>
7496 *
7497 * @param uri a URI for the person
7498 */
7499 public Person setUri(@Nullable String uri) {
7500 mUri = uri;
7501 return this;
7502 }
7503
7504 /**
7505 * Add a key to this person in order to uniquely identify it.
7506 * This is especially useful if the name doesn't uniquely identify this person or if the
7507 * display name is a short handle of the actual name.
7508 *
7509 * <P>If no key is provided, the name serves as as the key for the purpose of
7510 * identification.</P>
7511 *
7512 * @param key the key that uniquely identifies this person
7513 */
7514 public Person setKey(@Nullable String key) {
7515 mKey = key;
7516 return this;
7517 }
7518
7519
7520 /**
7521 * @return the uri provided for this person or {@code null} if no Uri was provided
7522 */
7523 @Nullable
7524 public String getUri() {
7525 return mUri;
7526 }
7527
7528 /**
7529 * @return the name provided for this person or {@code null} if no name was provided
7530 */
7531 @Nullable
7532 public CharSequence getName() {
7533 return mName;
7534 }
7535
7536 /**
7537 * @return the icon provided for this person or {@code null} if no icon was provided
7538 */
7539 @Nullable
7540 public Icon getIcon() {
7541 return mIcon;
7542 }
7543
7544 /**
7545 * @return the key provided for this person or {@code null} if no key was provided
7546 */
7547 @Nullable
7548 public String getKey() {
7549 return mKey;
7550 }
7551
7552 /**
7553 * @return the URI associated with this person, or "name:mName" otherwise
7554 * @hide
7555 */
7556 public String resolveToLegacyUri() {
7557 if (mUri != null) {
7558 return mUri;
7559 }
7560 if (mName != null) {
7561 return "name:" + mName;
7562 }
7563 return "";
7564 }
7565
7566 @Override
7567 public int describeContents() {
7568 return 0;
7569 }
7570
7571 @Override
7572 public void writeToParcel(Parcel dest, @WriteFlags int flags) {
7573 dest.writeCharSequence(mName);
7574 if (mIcon != null) {
7575 dest.writeInt(1);
7576 mIcon.writeToParcel(dest, 0);
7577 } else {
7578 dest.writeInt(0);
7579 }
7580 dest.writeString(mUri);
7581 dest.writeString(mKey);
7582 }
7583
7584 public static final Creator<Person> CREATOR = new Creator<Person>() {
7585 @Override
7586 public Person createFromParcel(Parcel in) {
7587 return new Person(in);
7588 }
7589
7590 @Override
7591 public Person[] newArray(int size) {
7592 return new Person[size];
7593 }
7594 };
7595 }
7596
Christoph Studer4600f9b2014-07-22 22:44:43 +02007597 // When adding a new Style subclass here, don't forget to update
7598 // Builder.getNotificationStyleClass.
7599
Griff Hazen61a9e862014-05-22 16:05:19 -07007600 /**
7601 * Extender interface for use with {@link Builder#extend}. Extenders may be used to add
7602 * metadata or change options on a notification builder.
7603 */
7604 public interface Extender {
7605 /**
7606 * Apply this extender to a notification builder.
7607 * @param builder the builder to be modified.
7608 * @return the build object for chaining.
7609 */
7610 public Builder extend(Builder builder);
7611 }
7612
7613 /**
7614 * Helper class to add wearable extensions to notifications.
7615 * <p class="note"> See
7616 * <a href="{@docRoot}wear/notifications/creating.html">Creating Notifications
7617 * for Android Wear</a> for more information on how to use this class.
7618 * <p>
7619 * To create a notification with wearable extensions:
7620 * <ol>
7621 * <li>Create a {@link android.app.Notification.Builder}, setting any desired
7622 * properties.
7623 * <li>Create a {@link android.app.Notification.WearableExtender}.
7624 * <li>Set wearable-specific properties using the
7625 * {@code add} and {@code set} methods of {@link android.app.Notification.WearableExtender}.
7626 * <li>Call {@link android.app.Notification.Builder#extend} to apply the extensions to a
7627 * notification.
7628 * <li>Post the notification to the notification system with the
7629 * {@code NotificationManager.notify(...)} methods.
7630 * </ol>
7631 *
7632 * <pre class="prettyprint">
7633 * Notification notif = new Notification.Builder(mContext)
7634 * .setContentTitle(&quot;New mail from &quot; + sender.toString())
7635 * .setContentText(subject)
7636 * .setSmallIcon(R.drawable.new_mail)
7637 * .extend(new Notification.WearableExtender()
7638 * .setContentIcon(R.drawable.new_mail))
7639 * .build();
7640 * NotificationManager notificationManger =
7641 * (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
7642 * notificationManger.notify(0, notif);</pre>
7643 *
7644 * <p>Wearable extensions can be accessed on an existing notification by using the
7645 * {@code WearableExtender(Notification)} constructor,
7646 * and then using the {@code get} methods to access values.
7647 *
7648 * <pre class="prettyprint">
7649 * Notification.WearableExtender wearableExtender = new Notification.WearableExtender(
7650 * notification);
Griff Hazen14f57992014-05-26 09:07:14 -07007651 * List&lt;Notification&gt; pages = wearableExtender.getPages();</pre>
Griff Hazen61a9e862014-05-22 16:05:19 -07007652 */
7653 public static final class WearableExtender implements Extender {
7654 /**
7655 * Sentinel value for an action index that is unset.
7656 */
7657 public static final int UNSET_ACTION_INDEX = -1;
7658
7659 /**
7660 * Size value for use with {@link #setCustomSizePreset} to show this notification with
7661 * default sizing.
7662 * <p>For custom display notifications created using {@link #setDisplayIntent},
Paul Soulosaa4f4bf2015-08-04 11:59:45 -07007663 * the default is {@link #SIZE_MEDIUM}. All other notifications size automatically based
Griff Hazen61a9e862014-05-22 16:05:19 -07007664 * on their content.
7665 */
7666 public static final int SIZE_DEFAULT = 0;
7667
7668 /**
7669 * Size value for use with {@link #setCustomSizePreset} to show this notification
7670 * with an extra small size.
7671 * <p>This value is only applicable for custom display notifications created using
7672 * {@link #setDisplayIntent}.
7673 */
7674 public static final int SIZE_XSMALL = 1;
7675
7676 /**
7677 * Size value for use with {@link #setCustomSizePreset} to show this notification
7678 * with a small size.
7679 * <p>This value is only applicable for custom display notifications created using
7680 * {@link #setDisplayIntent}.
7681 */
7682 public static final int SIZE_SMALL = 2;
7683
7684 /**
7685 * Size value for use with {@link #setCustomSizePreset} to show this notification
7686 * with a medium size.
7687 * <p>This value is only applicable for custom display notifications created using
7688 * {@link #setDisplayIntent}.
7689 */
7690 public static final int SIZE_MEDIUM = 3;
7691
7692 /**
7693 * Size value for use with {@link #setCustomSizePreset} to show this notification
7694 * with a large size.
7695 * <p>This value is only applicable for custom display notifications created using
7696 * {@link #setDisplayIntent}.
7697 */
7698 public static final int SIZE_LARGE = 4;
7699
Griff Hazend5f11f92014-05-27 15:40:09 -07007700 /**
7701 * Size value for use with {@link #setCustomSizePreset} to show this notification
7702 * full screen.
7703 * <p>This value is only applicable for custom display notifications created using
7704 * {@link #setDisplayIntent}.
7705 */
7706 public static final int SIZE_FULL_SCREEN = 5;
7707
Griff Hazen5f2edfc2014-09-29 16:28:44 -07007708 /**
7709 * Sentinel value for use with {@link #setHintScreenTimeout} to keep the screen on for a
7710 * short amount of time when this notification is displayed on the screen. This
7711 * is the default value.
7712 */
7713 public static final int SCREEN_TIMEOUT_SHORT = 0;
7714
7715 /**
7716 * Sentinel value for use with {@link #setHintScreenTimeout} to keep the screen on
7717 * for a longer amount of time when this notification is displayed on the screen.
7718 */
7719 public static final int SCREEN_TIMEOUT_LONG = -1;
7720
Griff Hazen61a9e862014-05-22 16:05:19 -07007721 /** Notification extra which contains wearable extensions */
7722 private static final String EXTRA_WEARABLE_EXTENSIONS = "android.wearable.EXTENSIONS";
7723
Pete Gastaf6781d2014-10-07 15:17:05 -04007724 // Keys within EXTRA_WEARABLE_EXTENSIONS for wearable options.
Griff Hazen61a9e862014-05-22 16:05:19 -07007725 private static final String KEY_ACTIONS = "actions";
7726 private static final String KEY_FLAGS = "flags";
7727 private static final String KEY_DISPLAY_INTENT = "displayIntent";
7728 private static final String KEY_PAGES = "pages";
7729 private static final String KEY_BACKGROUND = "background";
7730 private static final String KEY_CONTENT_ICON = "contentIcon";
7731 private static final String KEY_CONTENT_ICON_GRAVITY = "contentIconGravity";
7732 private static final String KEY_CONTENT_ACTION_INDEX = "contentActionIndex";
7733 private static final String KEY_CUSTOM_SIZE_PRESET = "customSizePreset";
7734 private static final String KEY_CUSTOM_CONTENT_HEIGHT = "customContentHeight";
7735 private static final String KEY_GRAVITY = "gravity";
Griff Hazen5f2edfc2014-09-29 16:28:44 -07007736 private static final String KEY_HINT_SCREEN_TIMEOUT = "hintScreenTimeout";
Nadia Benbernou948627e2016-04-14 14:41:08 -04007737 private static final String KEY_DISMISSAL_ID = "dismissalId";
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04007738 private static final String KEY_BRIDGE_TAG = "bridgeTag";
Griff Hazen61a9e862014-05-22 16:05:19 -07007739
7740 // Flags bitwise-ored to mFlags
7741 private static final int FLAG_CONTENT_INTENT_AVAILABLE_OFFLINE = 0x1;
7742 private static final int FLAG_HINT_HIDE_ICON = 1 << 1;
7743 private static final int FLAG_HINT_SHOW_BACKGROUND_ONLY = 1 << 2;
7744 private static final int FLAG_START_SCROLL_BOTTOM = 1 << 3;
Griff Hazen5f2edfc2014-09-29 16:28:44 -07007745 private static final int FLAG_HINT_AVOID_BACKGROUND_CLIPPING = 1 << 4;
Alex Hills4bcb06b2016-04-05 14:26:25 -04007746 private static final int FLAG_BIG_PICTURE_AMBIENT = 1 << 5;
Alex Hills9ab3a232016-04-05 14:54:56 -04007747 private static final int FLAG_HINT_CONTENT_INTENT_LAUNCHES_ACTIVITY = 1 << 6;
Griff Hazen61a9e862014-05-22 16:05:19 -07007748
7749 // Default value for flags integer
7750 private static final int DEFAULT_FLAGS = FLAG_CONTENT_INTENT_AVAILABLE_OFFLINE;
7751
7752 private static final int DEFAULT_CONTENT_ICON_GRAVITY = Gravity.END;
7753 private static final int DEFAULT_GRAVITY = Gravity.BOTTOM;
7754
7755 private ArrayList<Action> mActions = new ArrayList<Action>();
7756 private int mFlags = DEFAULT_FLAGS;
7757 private PendingIntent mDisplayIntent;
7758 private ArrayList<Notification> mPages = new ArrayList<Notification>();
7759 private Bitmap mBackground;
7760 private int mContentIcon;
7761 private int mContentIconGravity = DEFAULT_CONTENT_ICON_GRAVITY;
7762 private int mContentActionIndex = UNSET_ACTION_INDEX;
7763 private int mCustomSizePreset = SIZE_DEFAULT;
7764 private int mCustomContentHeight;
7765 private int mGravity = DEFAULT_GRAVITY;
Griff Hazen5f2edfc2014-09-29 16:28:44 -07007766 private int mHintScreenTimeout;
Nadia Benbernou948627e2016-04-14 14:41:08 -04007767 private String mDismissalId;
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04007768 private String mBridgeTag;
Griff Hazen61a9e862014-05-22 16:05:19 -07007769
7770 /**
7771 * Create a {@link android.app.Notification.WearableExtender} with default
7772 * options.
7773 */
7774 public WearableExtender() {
7775 }
7776
7777 public WearableExtender(Notification notif) {
7778 Bundle wearableBundle = notif.extras.getBundle(EXTRA_WEARABLE_EXTENSIONS);
7779 if (wearableBundle != null) {
7780 List<Action> actions = wearableBundle.getParcelableArrayList(KEY_ACTIONS);
7781 if (actions != null) {
7782 mActions.addAll(actions);
7783 }
7784
7785 mFlags = wearableBundle.getInt(KEY_FLAGS, DEFAULT_FLAGS);
7786 mDisplayIntent = wearableBundle.getParcelable(KEY_DISPLAY_INTENT);
7787
7788 Notification[] pages = getNotificationArrayFromBundle(
7789 wearableBundle, KEY_PAGES);
7790 if (pages != null) {
7791 Collections.addAll(mPages, pages);
7792 }
7793
7794 mBackground = wearableBundle.getParcelable(KEY_BACKGROUND);
7795 mContentIcon = wearableBundle.getInt(KEY_CONTENT_ICON);
7796 mContentIconGravity = wearableBundle.getInt(KEY_CONTENT_ICON_GRAVITY,
7797 DEFAULT_CONTENT_ICON_GRAVITY);
7798 mContentActionIndex = wearableBundle.getInt(KEY_CONTENT_ACTION_INDEX,
7799 UNSET_ACTION_INDEX);
7800 mCustomSizePreset = wearableBundle.getInt(KEY_CUSTOM_SIZE_PRESET,
7801 SIZE_DEFAULT);
7802 mCustomContentHeight = wearableBundle.getInt(KEY_CUSTOM_CONTENT_HEIGHT);
7803 mGravity = wearableBundle.getInt(KEY_GRAVITY, DEFAULT_GRAVITY);
Griff Hazen5f2edfc2014-09-29 16:28:44 -07007804 mHintScreenTimeout = wearableBundle.getInt(KEY_HINT_SCREEN_TIMEOUT);
Nadia Benbernou948627e2016-04-14 14:41:08 -04007805 mDismissalId = wearableBundle.getString(KEY_DISMISSAL_ID);
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04007806 mBridgeTag = wearableBundle.getString(KEY_BRIDGE_TAG);
Griff Hazen61a9e862014-05-22 16:05:19 -07007807 }
7808 }
7809
7810 /**
7811 * Apply wearable extensions to a notification that is being built. This is typically
7812 * called by the {@link android.app.Notification.Builder#extend} method of
7813 * {@link android.app.Notification.Builder}.
7814 */
7815 @Override
7816 public Notification.Builder extend(Notification.Builder builder) {
7817 Bundle wearableBundle = new Bundle();
7818
7819 if (!mActions.isEmpty()) {
7820 wearableBundle.putParcelableArrayList(KEY_ACTIONS, mActions);
7821 }
7822 if (mFlags != DEFAULT_FLAGS) {
7823 wearableBundle.putInt(KEY_FLAGS, mFlags);
7824 }
7825 if (mDisplayIntent != null) {
7826 wearableBundle.putParcelable(KEY_DISPLAY_INTENT, mDisplayIntent);
7827 }
7828 if (!mPages.isEmpty()) {
7829 wearableBundle.putParcelableArray(KEY_PAGES, mPages.toArray(
7830 new Notification[mPages.size()]));
7831 }
7832 if (mBackground != null) {
7833 wearableBundle.putParcelable(KEY_BACKGROUND, mBackground);
7834 }
7835 if (mContentIcon != 0) {
7836 wearableBundle.putInt(KEY_CONTENT_ICON, mContentIcon);
7837 }
7838 if (mContentIconGravity != DEFAULT_CONTENT_ICON_GRAVITY) {
7839 wearableBundle.putInt(KEY_CONTENT_ICON_GRAVITY, mContentIconGravity);
7840 }
7841 if (mContentActionIndex != UNSET_ACTION_INDEX) {
7842 wearableBundle.putInt(KEY_CONTENT_ACTION_INDEX,
7843 mContentActionIndex);
7844 }
7845 if (mCustomSizePreset != SIZE_DEFAULT) {
7846 wearableBundle.putInt(KEY_CUSTOM_SIZE_PRESET, mCustomSizePreset);
7847 }
7848 if (mCustomContentHeight != 0) {
7849 wearableBundle.putInt(KEY_CUSTOM_CONTENT_HEIGHT, mCustomContentHeight);
7850 }
7851 if (mGravity != DEFAULT_GRAVITY) {
7852 wearableBundle.putInt(KEY_GRAVITY, mGravity);
7853 }
Griff Hazen5f2edfc2014-09-29 16:28:44 -07007854 if (mHintScreenTimeout != 0) {
7855 wearableBundle.putInt(KEY_HINT_SCREEN_TIMEOUT, mHintScreenTimeout);
7856 }
Nadia Benbernou948627e2016-04-14 14:41:08 -04007857 if (mDismissalId != null) {
7858 wearableBundle.putString(KEY_DISMISSAL_ID, mDismissalId);
7859 }
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04007860 if (mBridgeTag != null) {
7861 wearableBundle.putString(KEY_BRIDGE_TAG, mBridgeTag);
7862 }
Griff Hazen61a9e862014-05-22 16:05:19 -07007863
7864 builder.getExtras().putBundle(EXTRA_WEARABLE_EXTENSIONS, wearableBundle);
7865 return builder;
7866 }
7867
7868 @Override
7869 public WearableExtender clone() {
7870 WearableExtender that = new WearableExtender();
7871 that.mActions = new ArrayList<Action>(this.mActions);
7872 that.mFlags = this.mFlags;
7873 that.mDisplayIntent = this.mDisplayIntent;
7874 that.mPages = new ArrayList<Notification>(this.mPages);
7875 that.mBackground = this.mBackground;
7876 that.mContentIcon = this.mContentIcon;
7877 that.mContentIconGravity = this.mContentIconGravity;
7878 that.mContentActionIndex = this.mContentActionIndex;
7879 that.mCustomSizePreset = this.mCustomSizePreset;
7880 that.mCustomContentHeight = this.mCustomContentHeight;
7881 that.mGravity = this.mGravity;
Griff Hazen5f2edfc2014-09-29 16:28:44 -07007882 that.mHintScreenTimeout = this.mHintScreenTimeout;
Nadia Benbernou948627e2016-04-14 14:41:08 -04007883 that.mDismissalId = this.mDismissalId;
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04007884 that.mBridgeTag = this.mBridgeTag;
Griff Hazen61a9e862014-05-22 16:05:19 -07007885 return that;
7886 }
7887
7888 /**
7889 * Add a wearable action to this notification.
7890 *
7891 * <p>When wearable actions are added using this method, the set of actions that
7892 * show on a wearable device splits from devices that only show actions added
7893 * using {@link android.app.Notification.Builder#addAction}. This allows for customization
7894 * of which actions display on different devices.
7895 *
7896 * @param action the action to add to this notification
7897 * @return this object for method chaining
7898 * @see android.app.Notification.Action
7899 */
7900 public WearableExtender addAction(Action action) {
7901 mActions.add(action);
7902 return this;
7903 }
7904
7905 /**
7906 * Adds wearable actions to this notification.
7907 *
7908 * <p>When wearable actions are added using this method, the set of actions that
7909 * show on a wearable device splits from devices that only show actions added
7910 * using {@link android.app.Notification.Builder#addAction}. This allows for customization
7911 * of which actions display on different devices.
7912 *
7913 * @param actions the actions to add to this notification
7914 * @return this object for method chaining
7915 * @see android.app.Notification.Action
7916 */
7917 public WearableExtender addActions(List<Action> actions) {
7918 mActions.addAll(actions);
7919 return this;
7920 }
7921
7922 /**
7923 * Clear all wearable actions present on this builder.
7924 * @return this object for method chaining.
7925 * @see #addAction
7926 */
7927 public WearableExtender clearActions() {
7928 mActions.clear();
7929 return this;
7930 }
7931
7932 /**
7933 * Get the wearable actions present on this notification.
7934 */
7935 public List<Action> getActions() {
7936 return mActions;
7937 }
7938
7939 /**
7940 * Set an intent to launch inside of an activity view when displaying
Griff Hazen14f57992014-05-26 09:07:14 -07007941 * this notification. The {@link PendingIntent} provided should be for an activity.
7942 *
7943 * <pre class="prettyprint">
7944 * Intent displayIntent = new Intent(context, MyDisplayActivity.class);
7945 * PendingIntent displayPendingIntent = PendingIntent.getActivity(context,
7946 * 0, displayIntent, PendingIntent.FLAG_UPDATE_CURRENT);
7947 * Notification notif = new Notification.Builder(context)
7948 * .extend(new Notification.WearableExtender()
7949 * .setDisplayIntent(displayPendingIntent)
7950 * .setCustomSizePreset(Notification.WearableExtender.SIZE_MEDIUM))
7951 * .build();</pre>
7952 *
7953 * <p>The activity to launch needs to allow embedding, must be exported, and
Griff Hazen831ca9d2014-06-17 00:38:38 -07007954 * should have an empty task affinity. It is also recommended to use the device
7955 * default light theme.
Griff Hazen14f57992014-05-26 09:07:14 -07007956 *
7957 * <p>Example AndroidManifest.xml entry:
7958 * <pre class="prettyprint">
7959 * &lt;activity android:name=&quot;com.example.MyDisplayActivity&quot;
7960 * android:exported=&quot;true&quot;
7961 * android:allowEmbedded=&quot;true&quot;
Griff Hazen831ca9d2014-06-17 00:38:38 -07007962 * android:taskAffinity=&quot;&quot;
7963 * android:theme=&quot;@android:style/Theme.DeviceDefault.Light&quot; /&gt;</pre>
Griff Hazen61a9e862014-05-22 16:05:19 -07007964 *
7965 * @param intent the {@link PendingIntent} for an activity
7966 * @return this object for method chaining
7967 * @see android.app.Notification.WearableExtender#getDisplayIntent
7968 */
7969 public WearableExtender setDisplayIntent(PendingIntent intent) {
7970 mDisplayIntent = intent;
7971 return this;
7972 }
7973
7974 /**
7975 * Get the intent to launch inside of an activity view when displaying this
7976 * notification. This {@code PendingIntent} should be for an activity.
7977 */
7978 public PendingIntent getDisplayIntent() {
7979 return mDisplayIntent;
7980 }
7981
7982 /**
7983 * Add an additional page of content to display with this notification. The current
7984 * notification forms the first page, and pages added using this function form
7985 * subsequent pages. This field can be used to separate a notification into multiple
7986 * sections.
7987 *
7988 * @param page the notification to add as another page
7989 * @return this object for method chaining
7990 * @see android.app.Notification.WearableExtender#getPages
7991 */
7992 public WearableExtender addPage(Notification page) {
7993 mPages.add(page);
7994 return this;
7995 }
7996
7997 /**
7998 * Add additional pages of content to display with this notification. The current
7999 * notification forms the first page, and pages added using this function form
8000 * subsequent pages. This field can be used to separate a notification into multiple
8001 * sections.
8002 *
8003 * @param pages a list of notifications
8004 * @return this object for method chaining
8005 * @see android.app.Notification.WearableExtender#getPages
8006 */
8007 public WearableExtender addPages(List<Notification> pages) {
8008 mPages.addAll(pages);
8009 return this;
8010 }
8011
8012 /**
8013 * Clear all additional pages present on this builder.
8014 * @return this object for method chaining.
8015 * @see #addPage
8016 */
8017 public WearableExtender clearPages() {
8018 mPages.clear();
8019 return this;
8020 }
8021
8022 /**
8023 * Get the array of additional pages of content for displaying this notification. The
8024 * current notification forms the first page, and elements within this array form
8025 * subsequent pages. This field can be used to separate a notification into multiple
8026 * sections.
8027 * @return the pages for this notification
8028 */
8029 public List<Notification> getPages() {
8030 return mPages;
8031 }
8032
8033 /**
8034 * Set a background image to be displayed behind the notification content.
8035 * Contrary to the {@link android.app.Notification.BigPictureStyle}, this background
8036 * will work with any notification style.
8037 *
8038 * @param background the background bitmap
8039 * @return this object for method chaining
8040 * @see android.app.Notification.WearableExtender#getBackground
8041 */
8042 public WearableExtender setBackground(Bitmap background) {
8043 mBackground = background;
8044 return this;
8045 }
8046
8047 /**
8048 * Get a background image to be displayed behind the notification content.
8049 * Contrary to the {@link android.app.Notification.BigPictureStyle}, this background
8050 * will work with any notification style.
8051 *
8052 * @return the background image
8053 * @see android.app.Notification.WearableExtender#setBackground
8054 */
8055 public Bitmap getBackground() {
8056 return mBackground;
8057 }
8058
8059 /**
8060 * Set an icon that goes with the content of this notification.
8061 */
8062 public WearableExtender setContentIcon(int icon) {
8063 mContentIcon = icon;
8064 return this;
8065 }
8066
8067 /**
8068 * Get an icon that goes with the content of this notification.
8069 */
8070 public int getContentIcon() {
8071 return mContentIcon;
8072 }
8073
8074 /**
8075 * Set the gravity that the content icon should have within the notification display.
8076 * Supported values include {@link android.view.Gravity#START} and
8077 * {@link android.view.Gravity#END}. The default value is {@link android.view.Gravity#END}.
8078 * @see #setContentIcon
8079 */
8080 public WearableExtender setContentIconGravity(int contentIconGravity) {
8081 mContentIconGravity = contentIconGravity;
8082 return this;
8083 }
8084
8085 /**
8086 * Get the gravity that the content icon should have within the notification display.
8087 * Supported values include {@link android.view.Gravity#START} and
8088 * {@link android.view.Gravity#END}. The default value is {@link android.view.Gravity#END}.
8089 * @see #getContentIcon
8090 */
8091 public int getContentIconGravity() {
8092 return mContentIconGravity;
8093 }
8094
8095 /**
8096 * Set an action from this notification's actions to be clickable with the content of
Griff Hazen14f57992014-05-26 09:07:14 -07008097 * this notification. This action will no longer display separately from the
8098 * notification's content.
8099 *
Griff Hazenca48d352014-05-28 22:37:13 -07008100 * <p>For notifications with multiple pages, child pages can also have content actions
Griff Hazen14f57992014-05-26 09:07:14 -07008101 * set, although the list of available actions comes from the main notification and not
8102 * from the child page's notification.
8103 *
8104 * @param actionIndex The index of the action to hoist onto the current notification page.
8105 * If wearable actions were added to the main notification, this index
8106 * will apply to that list, otherwise it will apply to the regular
8107 * actions list.
Griff Hazen61a9e862014-05-22 16:05:19 -07008108 */
8109 public WearableExtender setContentAction(int actionIndex) {
8110 mContentActionIndex = actionIndex;
8111 return this;
8112 }
8113
8114 /**
Griff Hazenca48d352014-05-28 22:37:13 -07008115 * Get the index of the notification action, if any, that was specified as being clickable
8116 * with the content of this notification. This action will no longer display separately
Griff Hazen14f57992014-05-26 09:07:14 -07008117 * from the notification's content.
Griff Hazen61a9e862014-05-22 16:05:19 -07008118 *
Griff Hazenca48d352014-05-28 22:37:13 -07008119 * <p>For notifications with multiple pages, child pages can also have content actions
Griff Hazen14f57992014-05-26 09:07:14 -07008120 * set, although the list of available actions comes from the main notification and not
8121 * from the child page's notification.
8122 *
8123 * <p>If wearable specific actions were added to the main notification, this index will
8124 * apply to that list, otherwise it will apply to the regular actions list.
Griff Hazenca48d352014-05-28 22:37:13 -07008125 *
8126 * @return the action index or {@link #UNSET_ACTION_INDEX} if no action was selected.
Griff Hazen61a9e862014-05-22 16:05:19 -07008127 */
8128 public int getContentAction() {
8129 return mContentActionIndex;
8130 }
8131
8132 /**
8133 * Set the gravity that this notification should have within the available viewport space.
8134 * Supported values include {@link android.view.Gravity#TOP},
8135 * {@link android.view.Gravity#CENTER_VERTICAL} and {@link android.view.Gravity#BOTTOM}.
8136 * The default value is {@link android.view.Gravity#BOTTOM}.
8137 */
8138 public WearableExtender setGravity(int gravity) {
8139 mGravity = gravity;
8140 return this;
8141 }
8142
8143 /**
8144 * Get the gravity that this notification should have within the available viewport space.
8145 * Supported values include {@link android.view.Gravity#TOP},
8146 * {@link android.view.Gravity#CENTER_VERTICAL} and {@link android.view.Gravity#BOTTOM}.
8147 * The default value is {@link android.view.Gravity#BOTTOM}.
8148 */
8149 public int getGravity() {
8150 return mGravity;
8151 }
8152
8153 /**
8154 * Set the custom size preset for the display of this notification out of the available
8155 * presets found in {@link android.app.Notification.WearableExtender}, e.g.
8156 * {@link #SIZE_LARGE}.
8157 * <p>Some custom size presets are only applicable for custom display notifications created
8158 * using {@link android.app.Notification.WearableExtender#setDisplayIntent}. Check the
8159 * documentation for the preset in question. See also
8160 * {@link #setCustomContentHeight} and {@link #getCustomSizePreset}.
8161 */
8162 public WearableExtender setCustomSizePreset(int sizePreset) {
8163 mCustomSizePreset = sizePreset;
8164 return this;
8165 }
8166
8167 /**
8168 * Get the custom size preset for the display of this notification out of the available
8169 * presets found in {@link android.app.Notification.WearableExtender}, e.g.
8170 * {@link #SIZE_LARGE}.
8171 * <p>Some custom size presets are only applicable for custom display notifications created
8172 * using {@link #setDisplayIntent}. Check the documentation for the preset in question.
8173 * See also {@link #setCustomContentHeight} and {@link #setCustomSizePreset}.
8174 */
8175 public int getCustomSizePreset() {
8176 return mCustomSizePreset;
8177 }
8178
8179 /**
8180 * Set the custom height in pixels for the display of this notification's content.
8181 * <p>This option is only available for custom display notifications created
8182 * using {@link android.app.Notification.WearableExtender#setDisplayIntent}. See also
8183 * {@link android.app.Notification.WearableExtender#setCustomSizePreset} and
8184 * {@link #getCustomContentHeight}.
8185 */
8186 public WearableExtender setCustomContentHeight(int height) {
8187 mCustomContentHeight = height;
8188 return this;
8189 }
8190
8191 /**
8192 * Get the custom height in pixels for the display of this notification's content.
8193 * <p>This option is only available for custom display notifications created
8194 * using {@link #setDisplayIntent}. See also {@link #setCustomSizePreset} and
8195 * {@link #setCustomContentHeight}.
8196 */
8197 public int getCustomContentHeight() {
8198 return mCustomContentHeight;
8199 }
8200
8201 /**
8202 * Set whether the scrolling position for the contents of this notification should start
8203 * at the bottom of the contents instead of the top when the contents are too long to
8204 * display within the screen. Default is false (start scroll at the top).
8205 */
8206 public WearableExtender setStartScrollBottom(boolean startScrollBottom) {
8207 setFlag(FLAG_START_SCROLL_BOTTOM, startScrollBottom);
8208 return this;
8209 }
8210
8211 /**
8212 * Get whether the scrolling position for the contents of this notification should start
8213 * at the bottom of the contents instead of the top when the contents are too long to
8214 * display within the screen. Default is false (start scroll at the top).
8215 */
8216 public boolean getStartScrollBottom() {
8217 return (mFlags & FLAG_START_SCROLL_BOTTOM) != 0;
8218 }
8219
8220 /**
8221 * Set whether the content intent is available when the wearable device is not connected
8222 * to a companion device. The user can still trigger this intent when the wearable device
8223 * is offline, but a visual hint will indicate that the content intent may not be available.
8224 * Defaults to true.
8225 */
8226 public WearableExtender setContentIntentAvailableOffline(
8227 boolean contentIntentAvailableOffline) {
8228 setFlag(FLAG_CONTENT_INTENT_AVAILABLE_OFFLINE, contentIntentAvailableOffline);
8229 return this;
8230 }
8231
8232 /**
8233 * Get whether the content intent is available when the wearable device is not connected
8234 * to a companion device. The user can still trigger this intent when the wearable device
8235 * is offline, but a visual hint will indicate that the content intent may not be available.
8236 * Defaults to true.
8237 */
8238 public boolean getContentIntentAvailableOffline() {
8239 return (mFlags & FLAG_CONTENT_INTENT_AVAILABLE_OFFLINE) != 0;
8240 }
8241
8242 /**
8243 * Set a hint that this notification's icon should not be displayed.
8244 * @param hintHideIcon {@code true} to hide the icon, {@code false} otherwise.
8245 * @return this object for method chaining
8246 */
8247 public WearableExtender setHintHideIcon(boolean hintHideIcon) {
8248 setFlag(FLAG_HINT_HIDE_ICON, hintHideIcon);
8249 return this;
8250 }
8251
8252 /**
8253 * Get a hint that this notification's icon should not be displayed.
8254 * @return {@code true} if this icon should not be displayed, false otherwise.
8255 * The default value is {@code false} if this was never set.
8256 */
8257 public boolean getHintHideIcon() {
8258 return (mFlags & FLAG_HINT_HIDE_ICON) != 0;
8259 }
8260
8261 /**
8262 * Set a visual hint that only the background image of this notification should be
8263 * displayed, and other semantic content should be hidden. This hint is only applicable
8264 * to sub-pages added using {@link #addPage}.
8265 */
8266 public WearableExtender setHintShowBackgroundOnly(boolean hintShowBackgroundOnly) {
8267 setFlag(FLAG_HINT_SHOW_BACKGROUND_ONLY, hintShowBackgroundOnly);
8268 return this;
8269 }
8270
8271 /**
8272 * Get a visual hint that only the background image of this notification should be
8273 * displayed, and other semantic content should be hidden. This hint is only applicable
8274 * to sub-pages added using {@link android.app.Notification.WearableExtender#addPage}.
8275 */
8276 public boolean getHintShowBackgroundOnly() {
8277 return (mFlags & FLAG_HINT_SHOW_BACKGROUND_ONLY) != 0;
8278 }
8279
Griff Hazen5f2edfc2014-09-29 16:28:44 -07008280 /**
Griff Hazen9c5be4e2014-11-17 17:47:50 -08008281 * Set a hint that this notification's background should not be clipped if possible,
8282 * and should instead be resized to fully display on the screen, retaining the aspect
8283 * ratio of the image. This can be useful for images like barcodes or qr codes.
Griff Hazen5f2edfc2014-09-29 16:28:44 -07008284 * @param hintAvoidBackgroundClipping {@code true} to avoid clipping if possible.
8285 * @return this object for method chaining
8286 */
8287 public WearableExtender setHintAvoidBackgroundClipping(
8288 boolean hintAvoidBackgroundClipping) {
8289 setFlag(FLAG_HINT_AVOID_BACKGROUND_CLIPPING, hintAvoidBackgroundClipping);
8290 return this;
8291 }
8292
8293 /**
Griff Hazen9c5be4e2014-11-17 17:47:50 -08008294 * Get a hint that this notification's background should not be clipped if possible,
8295 * and should instead be resized to fully display on the screen, retaining the aspect
8296 * ratio of the image. This can be useful for images like barcodes or qr codes.
Griff Hazen5f2edfc2014-09-29 16:28:44 -07008297 * @return {@code true} if it's ok if the background is clipped on the screen, false
8298 * otherwise. The default value is {@code false} if this was never set.
8299 */
8300 public boolean getHintAvoidBackgroundClipping() {
8301 return (mFlags & FLAG_HINT_AVOID_BACKGROUND_CLIPPING) != 0;
8302 }
8303
8304 /**
8305 * Set a hint that the screen should remain on for at least this duration when
8306 * this notification is displayed on the screen.
8307 * @param timeout The requested screen timeout in milliseconds. Can also be either
8308 * {@link #SCREEN_TIMEOUT_SHORT} or {@link #SCREEN_TIMEOUT_LONG}.
8309 * @return this object for method chaining
8310 */
8311 public WearableExtender setHintScreenTimeout(int timeout) {
8312 mHintScreenTimeout = timeout;
8313 return this;
8314 }
8315
8316 /**
8317 * Get the duration, in milliseconds, that the screen should remain on for
8318 * when this notification is displayed.
8319 * @return the duration in milliseconds if > 0, or either one of the sentinel values
8320 * {@link #SCREEN_TIMEOUT_SHORT} or {@link #SCREEN_TIMEOUT_LONG}.
8321 */
8322 public int getHintScreenTimeout() {
8323 return mHintScreenTimeout;
8324 }
8325
Alex Hills9ab3a232016-04-05 14:54:56 -04008326 /**
Alex Hills4bcb06b2016-04-05 14:26:25 -04008327 * Set a hint that this notification's {@link BigPictureStyle} (if present) should be
8328 * converted to low-bit and displayed in ambient mode, especially useful for barcodes and
8329 * qr codes, as well as other simple black-and-white tickets.
8330 * @param hintAmbientBigPicture {@code true} to enable converstion and ambient.
8331 * @return this object for method chaining
8332 */
8333 public WearableExtender setHintAmbientBigPicture(boolean hintAmbientBigPicture) {
8334 setFlag(FLAG_BIG_PICTURE_AMBIENT, hintAmbientBigPicture);
8335 return this;
8336 }
8337
8338 /**
8339 * Get a hint that this notification's {@link BigPictureStyle} (if present) should be
8340 * converted to low-bit and displayed in ambient mode, especially useful for barcodes and
8341 * qr codes, as well as other simple black-and-white tickets.
8342 * @return {@code true} if it should be displayed in ambient, false otherwise
8343 * otherwise. The default value is {@code false} if this was never set.
8344 */
8345 public boolean getHintAmbientBigPicture() {
8346 return (mFlags & FLAG_BIG_PICTURE_AMBIENT) != 0;
8347 }
8348
8349 /**
Alex Hills9ab3a232016-04-05 14:54:56 -04008350 * Set a hint that this notification's content intent will launch an {@link Activity}
8351 * directly, telling the platform that it can generate the appropriate transitions.
8352 * @param hintContentIntentLaunchesActivity {@code true} if the content intent will launch
8353 * an activity and transitions should be generated, false otherwise.
8354 * @return this object for method chaining
8355 */
8356 public WearableExtender setHintContentIntentLaunchesActivity(
8357 boolean hintContentIntentLaunchesActivity) {
8358 setFlag(FLAG_HINT_CONTENT_INTENT_LAUNCHES_ACTIVITY, hintContentIntentLaunchesActivity);
8359 return this;
8360 }
8361
8362 /**
8363 * Get a hint that this notification's content intent will launch an {@link Activity}
8364 * directly, telling the platform that it can generate the appropriate transitions
8365 * @return {@code true} if the content intent will launch an activity and transitions should
8366 * be generated, false otherwise. The default value is {@code false} if this was never set.
8367 */
8368 public boolean getHintContentIntentLaunchesActivity() {
8369 return (mFlags & FLAG_HINT_CONTENT_INTENT_LAUNCHES_ACTIVITY) != 0;
8370 }
8371
Nadia Benbernou948627e2016-04-14 14:41:08 -04008372 /**
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04008373 * Sets the dismissal id for this notification. If a notification is posted with a
8374 * dismissal id, then when that notification is canceled, notifications on other wearables
8375 * and the paired Android phone having that same dismissal id will also be canceled. See
Nadia Benbernou948627e2016-04-14 14:41:08 -04008376 * <a href="{@docRoot}wear/notifications/index.html">Adding Wearable Features to
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04008377 * Notifications</a> for more information.
Nadia Benbernou948627e2016-04-14 14:41:08 -04008378 * @param dismissalId the dismissal id of the notification.
8379 * @return this object for method chaining
8380 */
8381 public WearableExtender setDismissalId(String dismissalId) {
8382 mDismissalId = dismissalId;
8383 return this;
8384 }
8385
8386 /**
8387 * Returns the dismissal id of the notification.
8388 * @return the dismissal id of the notification or null if it has not been set.
8389 */
8390 public String getDismissalId() {
8391 return mDismissalId;
8392 }
8393
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04008394 /**
8395 * Sets a bridge tag for this notification. A bridge tag can be set for notifications
8396 * posted from a phone to provide finer-grained control on what notifications are bridged
8397 * to wearables. See <a href="{@docRoot}wear/notifications/index.html">Adding Wearable
8398 * Features to Notifications</a> for more information.
8399 * @param bridgeTag the bridge tag of the notification.
8400 * @return this object for method chaining
8401 */
8402 public WearableExtender setBridgeTag(String bridgeTag) {
8403 mBridgeTag = bridgeTag;
8404 return this;
8405 }
8406
8407 /**
8408 * Returns the bridge tag of the notification.
8409 * @return the bridge tag or null if not present.
8410 */
8411 public String getBridgeTag() {
8412 return mBridgeTag;
8413 }
8414
Griff Hazen61a9e862014-05-22 16:05:19 -07008415 private void setFlag(int mask, boolean value) {
8416 if (value) {
8417 mFlags |= mask;
8418 } else {
8419 mFlags &= ~mask;
8420 }
8421 }
8422 }
8423
8424 /**
Zhen Yu Song9d5528b2014-11-14 15:34:39 -08008425 * <p>Helper class to add Android Auto extensions to notifications. To create a notification
8426 * with car extensions:
8427 *
8428 * <ol>
8429 * <li>Create an {@link Notification.Builder}, setting any desired
8430 * properties.
8431 * <li>Create a {@link CarExtender}.
8432 * <li>Set car-specific properties using the {@code add} and {@code set} methods of
8433 * {@link CarExtender}.
8434 * <li>Call {@link Notification.Builder#extend(Notification.Extender)}
8435 * to apply the extensions to a notification.
8436 * </ol>
8437 *
8438 * <pre class="prettyprint">
8439 * Notification notification = new Notification.Builder(context)
8440 * ...
8441 * .extend(new CarExtender()
8442 * .set*(...))
8443 * .build();
8444 * </pre>
8445 *
8446 * <p>Car extensions can be accessed on an existing notification by using the
8447 * {@code CarExtender(Notification)} constructor, and then using the {@code get} methods
8448 * to access values.
8449 */
8450 public static final class CarExtender implements Extender {
8451 private static final String TAG = "CarExtender";
8452
8453 private static final String EXTRA_CAR_EXTENDER = "android.car.EXTENSIONS";
8454 private static final String EXTRA_LARGE_ICON = "large_icon";
8455 private static final String EXTRA_CONVERSATION = "car_conversation";
8456 private static final String EXTRA_COLOR = "app_color";
8457
8458 private Bitmap mLargeIcon;
8459 private UnreadConversation mUnreadConversation;
8460 private int mColor = Notification.COLOR_DEFAULT;
8461
8462 /**
8463 * Create a {@link CarExtender} with default options.
8464 */
8465 public CarExtender() {
8466 }
8467
8468 /**
8469 * Create a {@link CarExtender} from the CarExtender options of an existing Notification.
8470 *
8471 * @param notif The notification from which to copy options.
8472 */
8473 public CarExtender(Notification notif) {
8474 Bundle carBundle = notif.extras == null ?
8475 null : notif.extras.getBundle(EXTRA_CAR_EXTENDER);
8476 if (carBundle != null) {
8477 mLargeIcon = carBundle.getParcelable(EXTRA_LARGE_ICON);
8478 mColor = carBundle.getInt(EXTRA_COLOR, Notification.COLOR_DEFAULT);
8479
8480 Bundle b = carBundle.getBundle(EXTRA_CONVERSATION);
8481 mUnreadConversation = UnreadConversation.getUnreadConversationFromBundle(b);
8482 }
8483 }
8484
8485 /**
8486 * Apply car extensions to a notification that is being built. This is typically called by
8487 * the {@link Notification.Builder#extend(Notification.Extender)}
8488 * method of {@link Notification.Builder}.
8489 */
8490 @Override
8491 public Notification.Builder extend(Notification.Builder builder) {
8492 Bundle carExtensions = new Bundle();
8493
8494 if (mLargeIcon != null) {
8495 carExtensions.putParcelable(EXTRA_LARGE_ICON, mLargeIcon);
8496 }
8497 if (mColor != Notification.COLOR_DEFAULT) {
8498 carExtensions.putInt(EXTRA_COLOR, mColor);
8499 }
8500
8501 if (mUnreadConversation != null) {
8502 Bundle b = mUnreadConversation.getBundleForUnreadConversation();
8503 carExtensions.putBundle(EXTRA_CONVERSATION, b);
8504 }
8505
8506 builder.getExtras().putBundle(EXTRA_CAR_EXTENDER, carExtensions);
8507 return builder;
8508 }
8509
8510 /**
8511 * Sets the accent color to use when Android Auto presents the notification.
8512 *
8513 * Android Auto uses the color set with {@link Notification.Builder#setColor(int)}
8514 * to accent the displayed notification. However, not all colors are acceptable in an
8515 * automotive setting. This method can be used to override the color provided in the
8516 * notification in such a situation.
8517 */
Tor Norbye80756e32015-03-02 09:39:27 -08008518 public CarExtender setColor(@ColorInt int color) {
Zhen Yu Song9d5528b2014-11-14 15:34:39 -08008519 mColor = color;
8520 return this;
8521 }
8522
8523 /**
8524 * Gets the accent color.
8525 *
Julia Reynoldsd9228f12015-10-20 10:37:27 -04008526 * @see #setColor
Zhen Yu Song9d5528b2014-11-14 15:34:39 -08008527 */
Tor Norbye80756e32015-03-02 09:39:27 -08008528 @ColorInt
Zhen Yu Song9d5528b2014-11-14 15:34:39 -08008529 public int getColor() {
8530 return mColor;
8531 }
8532
8533 /**
8534 * Sets the large icon of the car notification.
8535 *
8536 * If no large icon is set in the extender, Android Auto will display the icon
8537 * specified by {@link Notification.Builder#setLargeIcon(android.graphics.Bitmap)}
8538 *
8539 * @param largeIcon The large icon to use in the car notification.
8540 * @return This object for method chaining.
8541 */
8542 public CarExtender setLargeIcon(Bitmap largeIcon) {
8543 mLargeIcon = largeIcon;
8544 return this;
8545 }
8546
8547 /**
8548 * Gets the large icon used in this car notification, or null if no icon has been set.
8549 *
8550 * @return The large icon for the car notification.
8551 * @see CarExtender#setLargeIcon
8552 */
8553 public Bitmap getLargeIcon() {
8554 return mLargeIcon;
8555 }
8556
8557 /**
8558 * Sets the unread conversation in a message notification.
8559 *
8560 * @param unreadConversation The unread part of the conversation this notification conveys.
8561 * @return This object for method chaining.
8562 */
8563 public CarExtender setUnreadConversation(UnreadConversation unreadConversation) {
8564 mUnreadConversation = unreadConversation;
8565 return this;
8566 }
8567
8568 /**
8569 * Returns the unread conversation conveyed by this notification.
8570 * @see #setUnreadConversation(UnreadConversation)
8571 */
8572 public UnreadConversation getUnreadConversation() {
8573 return mUnreadConversation;
8574 }
8575
8576 /**
8577 * A class which holds the unread messages from a conversation.
8578 */
8579 public static class UnreadConversation {
8580 private static final String KEY_AUTHOR = "author";
8581 private static final String KEY_TEXT = "text";
8582 private static final String KEY_MESSAGES = "messages";
8583 private static final String KEY_REMOTE_INPUT = "remote_input";
8584 private static final String KEY_ON_REPLY = "on_reply";
8585 private static final String KEY_ON_READ = "on_read";
8586 private static final String KEY_PARTICIPANTS = "participants";
8587 private static final String KEY_TIMESTAMP = "timestamp";
8588
8589 private final String[] mMessages;
8590 private final RemoteInput mRemoteInput;
8591 private final PendingIntent mReplyPendingIntent;
8592 private final PendingIntent mReadPendingIntent;
8593 private final String[] mParticipants;
8594 private final long mLatestTimestamp;
8595
8596 UnreadConversation(String[] messages, RemoteInput remoteInput,
8597 PendingIntent replyPendingIntent, PendingIntent readPendingIntent,
8598 String[] participants, long latestTimestamp) {
8599 mMessages = messages;
8600 mRemoteInput = remoteInput;
8601 mReadPendingIntent = readPendingIntent;
8602 mReplyPendingIntent = replyPendingIntent;
8603 mParticipants = participants;
8604 mLatestTimestamp = latestTimestamp;
8605 }
8606
8607 /**
8608 * Gets the list of messages conveyed by this notification.
8609 */
8610 public String[] getMessages() {
8611 return mMessages;
8612 }
8613
8614 /**
8615 * Gets the remote input that will be used to convey the response to a message list, or
8616 * null if no such remote input exists.
8617 */
8618 public RemoteInput getRemoteInput() {
8619 return mRemoteInput;
8620 }
8621
8622 /**
8623 * Gets the pending intent that will be triggered when the user replies to this
8624 * notification.
8625 */
8626 public PendingIntent getReplyPendingIntent() {
8627 return mReplyPendingIntent;
8628 }
8629
8630 /**
8631 * Gets the pending intent that Android Auto will send after it reads aloud all messages
8632 * in this object's message list.
8633 */
8634 public PendingIntent getReadPendingIntent() {
8635 return mReadPendingIntent;
8636 }
8637
8638 /**
8639 * Gets the participants in the conversation.
8640 */
8641 public String[] getParticipants() {
8642 return mParticipants;
8643 }
8644
8645 /**
8646 * Gets the firs participant in the conversation.
8647 */
8648 public String getParticipant() {
8649 return mParticipants.length > 0 ? mParticipants[0] : null;
8650 }
8651
8652 /**
8653 * Gets the timestamp of the conversation.
8654 */
8655 public long getLatestTimestamp() {
8656 return mLatestTimestamp;
8657 }
8658
8659 Bundle getBundleForUnreadConversation() {
8660 Bundle b = new Bundle();
8661 String author = null;
8662 if (mParticipants != null && mParticipants.length > 1) {
8663 author = mParticipants[0];
8664 }
8665 Parcelable[] messages = new Parcelable[mMessages.length];
8666 for (int i = 0; i < messages.length; i++) {
8667 Bundle m = new Bundle();
8668 m.putString(KEY_TEXT, mMessages[i]);
8669 m.putString(KEY_AUTHOR, author);
8670 messages[i] = m;
8671 }
8672 b.putParcelableArray(KEY_MESSAGES, messages);
8673 if (mRemoteInput != null) {
8674 b.putParcelable(KEY_REMOTE_INPUT, mRemoteInput);
8675 }
8676 b.putParcelable(KEY_ON_REPLY, mReplyPendingIntent);
8677 b.putParcelable(KEY_ON_READ, mReadPendingIntent);
8678 b.putStringArray(KEY_PARTICIPANTS, mParticipants);
8679 b.putLong(KEY_TIMESTAMP, mLatestTimestamp);
8680 return b;
8681 }
8682
8683 static UnreadConversation getUnreadConversationFromBundle(Bundle b) {
8684 if (b == null) {
8685 return null;
8686 }
8687 Parcelable[] parcelableMessages = b.getParcelableArray(KEY_MESSAGES);
8688 String[] messages = null;
8689 if (parcelableMessages != null) {
8690 String[] tmp = new String[parcelableMessages.length];
8691 boolean success = true;
8692 for (int i = 0; i < tmp.length; i++) {
8693 if (!(parcelableMessages[i] instanceof Bundle)) {
8694 success = false;
8695 break;
8696 }
8697 tmp[i] = ((Bundle) parcelableMessages[i]).getString(KEY_TEXT);
8698 if (tmp[i] == null) {
8699 success = false;
8700 break;
8701 }
8702 }
8703 if (success) {
8704 messages = tmp;
8705 } else {
8706 return null;
8707 }
8708 }
8709
8710 PendingIntent onRead = b.getParcelable(KEY_ON_READ);
8711 PendingIntent onReply = b.getParcelable(KEY_ON_REPLY);
8712
8713 RemoteInput remoteInput = b.getParcelable(KEY_REMOTE_INPUT);
8714
8715 String[] participants = b.getStringArray(KEY_PARTICIPANTS);
8716 if (participants == null || participants.length != 1) {
8717 return null;
8718 }
8719
8720 return new UnreadConversation(messages,
8721 remoteInput,
8722 onReply,
8723 onRead,
8724 participants, b.getLong(KEY_TIMESTAMP));
8725 }
8726 };
8727
8728 /**
8729 * Builder class for {@link CarExtender.UnreadConversation} objects.
8730 */
8731 public static class Builder {
8732 private final List<String> mMessages = new ArrayList<String>();
8733 private final String mParticipant;
8734 private RemoteInput mRemoteInput;
8735 private PendingIntent mReadPendingIntent;
8736 private PendingIntent mReplyPendingIntent;
8737 private long mLatestTimestamp;
8738
8739 /**
8740 * Constructs a new builder for {@link CarExtender.UnreadConversation}.
8741 *
8742 * @param name The name of the other participant in the conversation.
8743 */
8744 public Builder(String name) {
8745 mParticipant = name;
8746 }
8747
8748 /**
8749 * Appends a new unread message to the list of messages for this conversation.
8750 *
8751 * The messages should be added from oldest to newest.
8752 *
8753 * @param message The text of the new unread message.
8754 * @return This object for method chaining.
8755 */
8756 public Builder addMessage(String message) {
8757 mMessages.add(message);
8758 return this;
8759 }
8760
8761 /**
8762 * Sets the pending intent and remote input which will convey the reply to this
8763 * notification.
8764 *
8765 * @param pendingIntent The pending intent which will be triggered on a reply.
8766 * @param remoteInput The remote input parcelable which will carry the reply.
8767 * @return This object for method chaining.
8768 *
8769 * @see CarExtender.UnreadConversation#getRemoteInput
8770 * @see CarExtender.UnreadConversation#getReplyPendingIntent
8771 */
8772 public Builder setReplyAction(
8773 PendingIntent pendingIntent, RemoteInput remoteInput) {
8774 mRemoteInput = remoteInput;
8775 mReplyPendingIntent = pendingIntent;
8776
8777 return this;
8778 }
8779
8780 /**
8781 * Sets the pending intent that will be sent once the messages in this notification
8782 * are read.
8783 *
8784 * @param pendingIntent The pending intent to use.
8785 * @return This object for method chaining.
8786 */
8787 public Builder setReadPendingIntent(PendingIntent pendingIntent) {
8788 mReadPendingIntent = pendingIntent;
8789 return this;
8790 }
8791
8792 /**
8793 * Sets the timestamp of the most recent message in an unread conversation.
8794 *
8795 * If a messaging notification has been posted by your application and has not
8796 * yet been cancelled, posting a later notification with the same id and tag
8797 * but without a newer timestamp may result in Android Auto not displaying a
8798 * heads up notification for the later notification.
8799 *
8800 * @param timestamp The timestamp of the most recent message in the conversation.
8801 * @return This object for method chaining.
8802 */
8803 public Builder setLatestTimestamp(long timestamp) {
8804 mLatestTimestamp = timestamp;
8805 return this;
8806 }
8807
8808 /**
8809 * Builds a new unread conversation object.
8810 *
8811 * @return The new unread conversation object.
8812 */
8813 public UnreadConversation build() {
8814 String[] messages = mMessages.toArray(new String[mMessages.size()]);
8815 String[] participants = { mParticipant };
8816 return new UnreadConversation(messages, mRemoteInput, mReplyPendingIntent,
8817 mReadPendingIntent, participants, mLatestTimestamp);
8818 }
8819 }
8820 }
8821
8822 /**
Dmitri Plotnikov9e9cfd92016-12-21 10:52:26 -08008823 * <p>Helper class to add Android TV extensions to notifications. To create a notification
8824 * with a TV extension:
8825 *
8826 * <ol>
8827 * <li>Create an {@link Notification.Builder}, setting any desired properties.
8828 * <li>Create a {@link TvExtender}.
8829 * <li>Set TV-specific properties using the {@code set} methods of
8830 * {@link TvExtender}.
8831 * <li>Call {@link Notification.Builder#extend(Notification.Extender)}
8832 * to apply the extension to a notification.
8833 * </ol>
8834 *
8835 * <pre class="prettyprint">
8836 * Notification notification = new Notification.Builder(context)
8837 * ...
8838 * .extend(new TvExtender()
8839 * .set*(...))
8840 * .build();
8841 * </pre>
8842 *
8843 * <p>TV extensions can be accessed on an existing notification by using the
8844 * {@code TvExtender(Notification)} constructor, and then using the {@code get} methods
8845 * to access values.
8846 *
8847 * @hide
8848 */
8849 @SystemApi
8850 public static final class TvExtender implements Extender {
8851 private static final String TAG = "TvExtender";
8852
8853 private static final String EXTRA_TV_EXTENDER = "android.tv.EXTENSIONS";
8854 private static final String EXTRA_FLAGS = "flags";
8855 private static final String EXTRA_CONTENT_INTENT = "content_intent";
8856 private static final String EXTRA_DELETE_INTENT = "delete_intent";
Dmitri Plotnikovb8a04ee2017-01-20 10:39:22 -08008857 private static final String EXTRA_CHANNEL_ID = "channel_id";
Dmitri Plotnikov9e9cfd92016-12-21 10:52:26 -08008858
8859 // Flags bitwise-ored to mFlags
8860 private static final int FLAG_AVAILABLE_ON_TV = 0x1;
8861
8862 private int mFlags;
Dmitri Plotnikovb8a04ee2017-01-20 10:39:22 -08008863 private String mChannelId;
Dmitri Plotnikov9e9cfd92016-12-21 10:52:26 -08008864 private PendingIntent mContentIntent;
8865 private PendingIntent mDeleteIntent;
8866
8867 /**
8868 * Create a {@link TvExtender} with default options.
8869 */
8870 public TvExtender() {
8871 mFlags = FLAG_AVAILABLE_ON_TV;
8872 }
8873
8874 /**
8875 * Create a {@link TvExtender} from the TvExtender options of an existing Notification.
8876 *
8877 * @param notif The notification from which to copy options.
8878 */
8879 public TvExtender(Notification notif) {
8880 Bundle bundle = notif.extras == null ?
8881 null : notif.extras.getBundle(EXTRA_TV_EXTENDER);
8882 if (bundle != null) {
8883 mFlags = bundle.getInt(EXTRA_FLAGS);
Dmitri Plotnikovb8a04ee2017-01-20 10:39:22 -08008884 mChannelId = bundle.getString(EXTRA_CHANNEL_ID);
Dmitri Plotnikov9e9cfd92016-12-21 10:52:26 -08008885 mContentIntent = bundle.getParcelable(EXTRA_CONTENT_INTENT);
8886 mDeleteIntent = bundle.getParcelable(EXTRA_DELETE_INTENT);
8887 }
8888 }
8889
8890 /**
8891 * Apply a TV extension to a notification that is being built. This is typically called by
8892 * the {@link Notification.Builder#extend(Notification.Extender)}
8893 * method of {@link Notification.Builder}.
8894 */
8895 @Override
8896 public Notification.Builder extend(Notification.Builder builder) {
8897 Bundle bundle = new Bundle();
8898
8899 bundle.putInt(EXTRA_FLAGS, mFlags);
Dmitri Plotnikovb8a04ee2017-01-20 10:39:22 -08008900 bundle.putString(EXTRA_CHANNEL_ID, mChannelId);
Dmitri Plotnikov9e9cfd92016-12-21 10:52:26 -08008901 if (mContentIntent != null) {
8902 bundle.putParcelable(EXTRA_CONTENT_INTENT, mContentIntent);
8903 }
8904
8905 if (mDeleteIntent != null) {
8906 bundle.putParcelable(EXTRA_DELETE_INTENT, mDeleteIntent);
8907 }
8908
8909 builder.getExtras().putBundle(EXTRA_TV_EXTENDER, bundle);
8910 return builder;
8911 }
8912
8913 /**
8914 * Returns true if this notification should be shown on TV. This method return true
8915 * if the notification was extended with a TvExtender.
8916 */
8917 public boolean isAvailableOnTv() {
8918 return (mFlags & FLAG_AVAILABLE_ON_TV) != 0;
8919 }
8920
8921 /**
Dmitri Plotnikovb8a04ee2017-01-20 10:39:22 -08008922 * Specifies the channel the notification should be delivered on when shown on TV.
8923 * It can be different from the channel that the notification is delivered to when
8924 * posting on a non-TV device.
8925 */
8926 public TvExtender setChannel(String channelId) {
8927 mChannelId = channelId;
8928 return this;
8929 }
8930
8931 /**
Julia Reynoldsbad42972017-04-25 13:52:49 -04008932 * Specifies the channel the notification should be delivered on when shown on TV.
8933 * It can be different from the channel that the notification is delivered to when
8934 * posting on a non-TV device.
8935 */
8936 public TvExtender setChannelId(String channelId) {
8937 mChannelId = channelId;
8938 return this;
8939 }
8940
Jeff Sharkey000ce802017-04-29 13:13:27 -06008941 /** @removed */
8942 @Deprecated
Dmitri Plotnikovb8a04ee2017-01-20 10:39:22 -08008943 public String getChannel() {
8944 return mChannelId;
8945 }
8946
8947 /**
Julia Reynoldsbad42972017-04-25 13:52:49 -04008948 * Returns the id of the channel this notification posts to on TV.
8949 */
8950 public String getChannelId() {
8951 return mChannelId;
8952 }
8953
8954 /**
Dmitri Plotnikov9e9cfd92016-12-21 10:52:26 -08008955 * Supplies a {@link PendingIntent} to be sent when the notification is selected on TV.
8956 * If provided, it is used instead of the content intent specified
8957 * at the level of Notification.
8958 */
8959 public TvExtender setContentIntent(PendingIntent intent) {
8960 mContentIntent = intent;
8961 return this;
8962 }
8963
8964 /**
8965 * Returns the TV-specific content intent. If this method returns null, the
8966 * main content intent on the notification should be used.
8967 *
8968 * @see {@link Notification#contentIntent}
8969 */
8970 public PendingIntent getContentIntent() {
8971 return mContentIntent;
8972 }
8973
8974 /**
8975 * Supplies a {@link PendingIntent} to send when the notification is cleared explicitly
8976 * by the user on TV. If provided, it is used instead of the delete intent specified
8977 * at the level of Notification.
8978 */
8979 public TvExtender setDeleteIntent(PendingIntent intent) {
8980 mDeleteIntent = intent;
8981 return this;
8982 }
8983
8984 /**
8985 * Returns the TV-specific delete intent. If this method returns null, the
8986 * main delete intent on the notification should be used.
8987 *
8988 * @see {@link Notification#deleteIntent}
8989 */
8990 public PendingIntent getDeleteIntent() {
8991 return mDeleteIntent;
8992 }
8993 }
8994
8995 /**
Griff Hazen61a9e862014-05-22 16:05:19 -07008996 * Get an array of Notification objects from a parcelable array bundle field.
8997 * Update the bundle to have a typed array so fetches in the future don't need
8998 * to do an array copy.
8999 */
9000 private static Notification[] getNotificationArrayFromBundle(Bundle bundle, String key) {
9001 Parcelable[] array = bundle.getParcelableArray(key);
9002 if (array instanceof Notification[] || array == null) {
9003 return (Notification[]) array;
9004 }
9005 Notification[] typedArray = Arrays.copyOf(array, array.length,
9006 Notification[].class);
9007 bundle.putParcelableArray(key, typedArray);
9008 return typedArray;
9009 }
Christoph Studer4600f9b2014-07-22 22:44:43 +02009010
9011 private static class BuilderRemoteViews extends RemoteViews {
9012 public BuilderRemoteViews(Parcel parcel) {
9013 super(parcel);
9014 }
9015
Kenny Guy77320062014-08-27 21:37:15 +01009016 public BuilderRemoteViews(ApplicationInfo appInfo, int layoutId) {
9017 super(appInfo, layoutId);
Christoph Studer4600f9b2014-07-22 22:44:43 +02009018 }
9019
9020 @Override
9021 public BuilderRemoteViews clone() {
9022 Parcel p = Parcel.obtain();
9023 writeToParcel(p, 0);
9024 p.setDataPosition(0);
9025 BuilderRemoteViews brv = new BuilderRemoteViews(p);
9026 p.recycle();
9027 return brv;
9028 }
9029 }
Adrian Roos70d7aa32017-01-11 15:39:06 -08009030
9031 private static class StandardTemplateParams {
9032 boolean hasProgress = true;
9033 boolean ambient = false;
9034 CharSequence title;
9035 CharSequence text;
Selim Cinekafeed292017-12-12 17:32:44 -08009036 CharSequence headerTextSecondary;
Selim Cinek88188f22017-09-19 16:46:56 -07009037 boolean hideLargeIcon;
9038 public boolean alwaysShowReply;
Adrian Roos70d7aa32017-01-11 15:39:06 -08009039
9040 final StandardTemplateParams reset() {
9041 hasProgress = true;
9042 ambient = false;
9043 title = null;
9044 text = null;
Selim Cinekafeed292017-12-12 17:32:44 -08009045 headerTextSecondary = null;
Adrian Roos70d7aa32017-01-11 15:39:06 -08009046 return this;
9047 }
9048
9049 final StandardTemplateParams hasProgress(boolean hasProgress) {
9050 this.hasProgress = hasProgress;
9051 return this;
9052 }
9053
9054 final StandardTemplateParams title(CharSequence title) {
9055 this.title = title;
9056 return this;
9057 }
9058
9059 final StandardTemplateParams text(CharSequence text) {
9060 this.text = text;
9061 return this;
9062 }
9063
Selim Cinekafeed292017-12-12 17:32:44 -08009064 final StandardTemplateParams headerTextSecondary(CharSequence text) {
9065 this.headerTextSecondary = text;
9066 return this;
9067 }
9068
Selim Cinek88188f22017-09-19 16:46:56 -07009069 final StandardTemplateParams alwaysShowReply(boolean alwaysShowReply) {
9070 this.alwaysShowReply = alwaysShowReply;
9071 return this;
9072 }
9073
9074 final StandardTemplateParams hideLargeIcon(boolean hideLargeIcon) {
9075 this.hideLargeIcon = hideLargeIcon;
9076 return this;
9077 }
9078
Adrian Roos70d7aa32017-01-11 15:39:06 -08009079 final StandardTemplateParams ambient(boolean ambient) {
Adrian Roos0bc3f6a2017-03-06 11:54:05 -08009080 Preconditions.checkState(title == null && text == null, "must set ambient before text");
Adrian Roos70d7aa32017-01-11 15:39:06 -08009081 this.ambient = ambient;
9082 return this;
9083 }
9084
9085 final StandardTemplateParams fillTextsFrom(Builder b) {
9086 Bundle extras = b.mN.extras;
Lucas Dupin06c5e642017-09-13 16:34:58 -07009087 this.title = b.processLegacyText(extras.getCharSequence(EXTRA_TITLE), ambient);
9088
9089 // Big text notifications should contain their content when viewed in ambient mode.
9090 CharSequence text = extras.getCharSequence(EXTRA_BIG_TEXT);
9091 if (!ambient || TextUtils.isEmpty(text)) {
9092 text = extras.getCharSequence(EXTRA_TEXT);
9093 }
9094 this.text = b.processLegacyText(text, ambient);
Adrian Roos70d7aa32017-01-11 15:39:06 -08009095 return this;
9096 }
9097 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009098}