blob: fcdf7b0d38f53abff710d789ccb1511bb76abfa5 [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
2 * Copyright (C) 2007 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package android.app;
18
Tor Norbye80756e32015-03-02 09:39:27 -080019import android.annotation.ColorInt;
Tor Norbye7b9c9122013-05-30 16:48:33 -070020import android.annotation.DrawableRes;
Tor Norbyed9273d62013-05-30 15:59:53 -070021import android.annotation.IntDef;
Daniel Sandler01df1c62014-06-09 10:54:01 -040022import android.annotation.SdkConstant;
23import android.annotation.SdkConstant.SdkConstantType;
Julia Reynoldse46bb372016-03-17 11:05:58 -040024import android.annotation.SystemApi;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080025import android.content.Context;
26import android.content.Intent;
Kenny Guy77320062014-08-27 21:37:15 +010027import android.content.pm.ApplicationInfo;
Dan Sandler732bd6c2016-04-12 14:20:32 -040028import android.content.pm.PackageInfo;
29import android.content.pm.PackageManager;
Christoph Studer4600f9b2014-07-22 22:44:43 +020030import android.content.pm.PackageManager.NameNotFoundException;
Jorim Jaggief72a192014-08-26 21:57:46 +020031import android.content.res.ColorStateList;
Joe Onoratoef1e7762010-09-17 18:38:38 -040032import android.graphics.Bitmap;
Kenny Guy8a0101b2014-05-08 23:34:12 +010033import android.graphics.Canvas;
Adrian Roosc1a80b02016-04-05 14:54:55 -070034import android.graphics.Color;
Jorim Jaggi5c2d8462014-03-21 17:37:00 +010035import android.graphics.PorterDuff;
Kenny Guy8a0101b2014-05-08 23:34:12 +010036import android.graphics.drawable.Drawable;
Dan Sandlerd63f9322015-05-06 15:18:49 -040037import android.graphics.drawable.Icon;
John Spurlockc0650f022014-07-19 13:22:39 -040038import android.media.AudioAttributes;
Jeff Sharkey098d5802012-04-26 17:30:34 -070039import android.media.AudioManager;
Jeff Browndba34ba2014-06-24 20:46:03 -070040import android.media.session.MediaSession;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080041import android.net.Uri;
Daniel Sandlerdcbaf662013-04-26 16:23:09 -040042import android.os.BadParcelableException;
Christoph Studer239f8352014-08-25 15:13:18 +020043import android.os.Build;
Daniel Sandler2561b0b2012-02-13 21:04:12 -050044import android.os.Bundle;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080045import android.os.Parcel;
46import android.os.Parcelable;
Daniel Sandlera2985ed2012-04-03 16:42:00 -040047import android.os.SystemClock;
Jeff Sharkey6d515712012-09-20 16:06:08 -070048import android.os.UserHandle;
Adrian Roosc1a80b02016-04-05 14:54:55 -070049import android.text.BidiFormatter;
Selim Cinek60a54252016-02-26 17:03:25 -080050import android.text.SpannableStringBuilder;
51import android.text.Spanned;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080052import android.text.TextUtils;
Selim Cinek60a54252016-02-26 17:03:25 -080053import android.text.style.AbsoluteSizeSpan;
Selim Cinek89991a22016-03-07 19:51:54 -080054import android.text.style.CharacterStyle;
Selim Cinek60a54252016-02-26 17:03:25 -080055import android.text.style.RelativeSizeSpan;
56import android.text.style.TextAppearanceSpan;
Daniel Sandlerdcbaf662013-04-26 16:23:09 -040057import android.util.Log;
Dan Sandler50128532015-12-08 15:42:41 -050058import android.util.SparseArray;
Daniel Sandler9f7936a2012-05-21 16:14:28 -040059import android.util.TypedValue;
Griff Hazen61a9e862014-05-22 16:05:19 -070060import android.view.Gravity;
Selim Cinekea4bef72015-12-02 15:51:10 -080061import android.view.NotificationHeaderView;
Joe Onorato8595a3d2010-11-19 18:12:07 -080062import android.view.View;
Adrian Roos9b123cf2016-02-04 14:55:57 -080063import android.view.ViewGroup;
Jeff Sharkey1c400132011-08-05 14:50:13 -070064import android.widget.ProgressBar;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080065import android.widget.RemoteViews;
66
Griff Hazen959591e2014-05-15 22:26:18 -070067import com.android.internal.R;
Griff Hazenc091ba82014-05-16 10:13:26 -070068import com.android.internal.util.NotificationColorUtil;
Griff Hazen959591e2014-05-15 22:26:18 -070069
Tor Norbyed9273d62013-05-30 15:59:53 -070070import java.lang.annotation.Retention;
71import java.lang.annotation.RetentionPolicy;
Christoph Studer4600f9b2014-07-22 22:44:43 +020072import java.lang.reflect.Constructor;
Daniel Sandler2561b0b2012-02-13 21:04:12 -050073import java.util.ArrayList;
Griff Hazen61a9e862014-05-22 16:05:19 -070074import java.util.Arrays;
Griff Hazen5cadc3b2014-05-20 09:55:39 -070075import java.util.Collections;
Griff Hazen61a9e862014-05-22 16:05:19 -070076import java.util.List;
Dan Sandler50128532015-12-08 15:42:41 -050077import java.util.Set;
Joe Onorato561d3852010-11-20 18:09:34 -080078
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080079/**
80 * A class that represents how a persistent notification is to be presented to
81 * the user using the {@link android.app.NotificationManager}.
82 *
Joe Onoratocb109a02011-01-18 17:57:41 -080083 * <p>The {@link Notification.Builder Notification.Builder} has been added to make it
84 * easier to construct Notifications.</p>
85 *
Joe Fernandez558459f2011-10-13 16:47:36 -070086 * <div class="special reference">
87 * <h3>Developer Guides</h3>
88 * <p>For a guide to creating notifications, read the
89 * <a href="{@docRoot}guide/topics/ui/notifiers/notifications.html">Status Bar Notifications</a>
90 * developer guide.</p>
91 * </div>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080092 */
93public class Notification implements Parcelable
94{
Daniel Sandlerdcbaf662013-04-26 16:23:09 -040095 private static final String TAG = "Notification";
96
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080097 /**
Daniel Sandler01df1c62014-06-09 10:54:01 -040098 * An activity that provides a user interface for adjusting notification preferences for its
99 * containing application. Optional but recommended for apps that post
100 * {@link android.app.Notification Notifications}.
101 */
102 @SdkConstant(SdkConstantType.INTENT_CATEGORY)
103 public static final String INTENT_CATEGORY_NOTIFICATION_PREFERENCES
104 = "android.intent.category.NOTIFICATION_PREFERENCES";
105
106 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800107 * Use all default values (where applicable).
108 */
109 public static final int DEFAULT_ALL = ~0;
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500110
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800111 /**
112 * Use the default notification sound. This will ignore any given
113 * {@link #sound}.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500114 *
Chris Wren47c20a12014-06-18 17:27:29 -0400115 * <p>
116 * A notification that is noisy is more likely to be presented as a heads-up notification.
117 * </p>
118 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800119 * @see #defaults
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500120 */
121
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800122 public static final int DEFAULT_SOUND = 1;
123
124 /**
125 * Use the default notification vibrate. This will ignore any given
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500126 * {@link #vibrate}. Using phone vibration requires the
Scott Mainb8b36452009-04-26 15:50:49 -0700127 * {@link android.Manifest.permission#VIBRATE VIBRATE} permission.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500128 *
Chris Wren47c20a12014-06-18 17:27:29 -0400129 * <p>
130 * A notification that vibrates is more likely to be presented as a heads-up notification.
131 * </p>
132 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800133 * @see #defaults
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500134 */
135
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800136 public static final int DEFAULT_VIBRATE = 2;
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500137
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800138 /**
139 * Use the default notification lights. This will ignore the
140 * {@link #FLAG_SHOW_LIGHTS} bit, and {@link #ledARGB}, {@link #ledOffMS}, or
141 * {@link #ledOnMS}.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500142 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800143 * @see #defaults
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500144 */
145
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800146 public static final int DEFAULT_LIGHTS = 4;
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500147
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800148 /**
Christoph Studer535ec612014-09-03 15:47:47 +0200149 * Maximum length of CharSequences accepted by Builder and friends.
150 *
151 * <p>
152 * Avoids spamming the system with overly large strings such as full e-mails.
153 */
154 private static final int MAX_CHARSEQUENCE_LENGTH = 5 * 1024;
155
156 /**
Adrian Roose458aa82015-12-08 16:17:19 -0800157 * Maximum entries of reply text that are accepted by Builder and friends.
158 */
159 private static final int MAX_REPLY_HISTORY = 5;
160
161 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500162 * A timestamp related to this notification, in milliseconds since the epoch.
Joe Malin8d40d042012-11-05 11:36:40 -0800163 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500164 * Default value: {@link System#currentTimeMillis() Now}.
165 *
166 * Choose a timestamp that will be most relevant to the user. For most finite events, this
167 * corresponds to the time the event happened (or will happen, in the case of events that have
168 * yet to occur but about which the user is being informed). Indefinite events should be
Joe Malin8d40d042012-11-05 11:36:40 -0800169 * timestamped according to when the activity began.
170 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500171 * Some examples:
Joe Malin8d40d042012-11-05 11:36:40 -0800172 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500173 * <ul>
174 * <li>Notification of a new chat message should be stamped when the message was received.</li>
175 * <li>Notification of an ongoing file download (with a progress bar, for example) should be stamped when the download started.</li>
176 * <li>Notification of a completed file download should be stamped when the download finished.</li>
177 * <li>Notification of an upcoming meeting should be stamped with the time the meeting will begin (that is, in the future).</li>
178 * <li>Notification of an ongoing stopwatch (increasing timer) should be stamped with the watch's start time.
179 * <li>Notification of an ongoing countdown timer should be stamped with the timer's end time.
Joe Malin8d40d042012-11-05 11:36:40 -0800180 * </ul>
181 *
Selim Cinek0ff1ce602016-04-05 18:27:16 -0700182 * For apps targeting {@link android.os.Build.VERSION_CODES#N} and above, this time is not shown
183 * anymore by default and must be opted into by using
184 * {@link android.app.Notification.Builder#setShowWhen(boolean)}
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800185 */
186 public long when;
187
188 /**
189 * The resource id of a drawable to use as the icon in the status bar.
Dan Sandler86647982015-05-13 23:41:13 -0400190 *
191 * @deprecated Use {@link Builder#setSmallIcon(Icon)} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800192 */
Dan Sandler86647982015-05-13 23:41:13 -0400193 @Deprecated
Tor Norbye7b9c9122013-05-30 16:48:33 -0700194 @DrawableRes
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800195 public int icon;
196
197 /**
Joe Onorato46439ce2010-11-19 13:56:21 -0800198 * If the icon in the status bar is to have more than one level, you can set this. Otherwise,
199 * leave it at its default value of 0.
200 *
201 * @see android.widget.ImageView#setImageLevel
Griff Hazen959591e2014-05-15 22:26:18 -0700202 * @see android.graphics.drawable.Drawable#setLevel
Joe Onorato46439ce2010-11-19 13:56:21 -0800203 */
204 public int iconLevel;
205
206 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500207 * The number of events that this notification represents. For example, in a new mail
208 * notification, this could be the number of unread messages.
Joe Malin8d40d042012-11-05 11:36:40 -0800209 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500210 * The system may or may not use this field to modify the appearance of the notification. For
211 * example, before {@link android.os.Build.VERSION_CODES#HONEYCOMB}, this number was
212 * superimposed over the icon in the status bar. Starting with
213 * {@link android.os.Build.VERSION_CODES#HONEYCOMB}, the template used by
214 * {@link Notification.Builder} has displayed the number in the expanded notification view.
Joe Malin8d40d042012-11-05 11:36:40 -0800215 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500216 * If the number is 0 or negative, it is never shown.
Selim Cinek0f9dd1e2016-04-05 17:03:40 -0700217 *
218 * @deprecated this number is not shown anymore
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800219 */
220 public int number;
221
222 /**
223 * The intent to execute when the expanded status entry is clicked. If
224 * this is an activity, it must include the
225 * {@link android.content.Intent#FLAG_ACTIVITY_NEW_TASK} flag, which requires
Scott Main7aee61f2011-02-08 11:25:01 -0800226 * that you take care of task management as described in the
227 * <a href="{@docRoot}guide/topics/fundamentals/tasks-and-back-stack.html">Tasks and Back
Dianne Hackborn6ceca582012-01-10 15:24:26 -0800228 * Stack</a> document. In particular, make sure to read the notification section
229 * <a href="{@docRoot}guide/topics/ui/notifiers/notifications.html#HandlingNotifications">Handling
230 * Notifications</a> for the correct ways to launch an application from a
231 * notification.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800232 */
233 public PendingIntent contentIntent;
234
235 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500236 * The intent to execute when the notification is explicitly dismissed by the user, either with
237 * the "Clear All" button or by swiping it away individually.
238 *
239 * This probably shouldn't be launching an activity since several of those will be sent
240 * at the same time.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800241 */
242 public PendingIntent deleteIntent;
243
244 /**
Dianne Hackborn170bae72010-09-03 15:14:28 -0700245 * An intent to launch instead of posting the notification to the status bar.
Joe Onoratocb109a02011-01-18 17:57:41 -0800246 *
Chris Wren47c20a12014-06-18 17:27:29 -0400247 * <p>
248 * The system UI may choose to display a heads-up notification, instead of
249 * launching this intent, while the user is using the device.
250 * </p>
251 *
Joe Onoratocb109a02011-01-18 17:57:41 -0800252 * @see Notification.Builder#setFullScreenIntent
Daniel Sandlere46cbd32010-06-17 10:35:26 -0400253 */
254 public PendingIntent fullScreenIntent;
255
256 /**
Dan Sandler5fcdf6e2014-07-18 11:31:15 -0400257 * Text that summarizes this notification for accessibility services.
258 *
259 * As of the L release, this text is no longer shown on screen, but it is still useful to
260 * accessibility services (where it serves as an audible announcement of the notification's
261 * appearance).
Joe Onoratoef1e7762010-09-17 18:38:38 -0400262 *
Joe Onorato46439ce2010-11-19 13:56:21 -0800263 * @see #tickerView
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800264 */
265 public CharSequence tickerText;
266
267 /**
Dan Sandler5fcdf6e2014-07-18 11:31:15 -0400268 * Formerly, a view showing the {@link #tickerText}.
269 *
270 * No longer displayed in the status bar as of API 21.
Joe Onoratoef1e7762010-09-17 18:38:38 -0400271 */
Dan Sandler5fcdf6e2014-07-18 11:31:15 -0400272 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -0800273 public RemoteViews tickerView;
Joe Onoratoef1e7762010-09-17 18:38:38 -0400274
275 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400276 * The view that will represent this notification in the notification list (which is pulled
277 * down from the status bar).
278 *
Julia Reynoldsd4ea7412016-02-17 14:00:56 -0500279 * As of N, this field may be null. The notification view is determined by the inputs
280 * to {@link Notification.Builder}; a custom RemoteViews can optionally be
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400281 * supplied with {@link Notification.Builder#setCustomContentView(RemoteViews)}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800282 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400283 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800284 public RemoteViews contentView;
285
Daniel Sandlera0a938c2012-03-15 08:42:37 -0400286 /**
Daniel Sandler4dfbe832012-04-11 14:51:46 -0400287 * A large-format version of {@link #contentView}, giving the Notification an
Daniel Sandlerf3b73432012-03-27 15:01:25 -0400288 * opportunity to show more detail. The system UI may choose to show this
289 * instead of the normal content view at its discretion.
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400290 *
Julia Reynoldsd4ea7412016-02-17 14:00:56 -0500291 * As of N, this field may be null. The expanded notification view is determined by the
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400292 * inputs to {@link Notification.Builder}; a custom RemoteViews can optionally be
293 * supplied with {@link Notification.Builder#setCustomBigContentView(RemoteViews)}.
Daniel Sandlerf3b73432012-03-27 15:01:25 -0400294 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400295 @Deprecated
Daniel Sandlerf3b73432012-03-27 15:01:25 -0400296 public RemoteViews bigContentView;
297
Chris Wren8fd39ec2014-02-27 17:43:26 -0500298
299 /**
Chris Wren47c20a12014-06-18 17:27:29 -0400300 * A medium-format version of {@link #contentView}, providing the Notification an
301 * opportunity to add action buttons to contentView. At its discretion, the system UI may
302 * choose to show this as a heads-up notification, which will pop up so the user can see
303 * it without leaving their current activity.
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400304 *
Julia Reynoldsd4ea7412016-02-17 14:00:56 -0500305 * As of N, this field may be null. The heads-up notification view is determined by the
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400306 * inputs to {@link Notification.Builder}; a custom RemoteViews can optionally be
307 * supplied with {@link Notification.Builder#setCustomHeadsUpContentView(RemoteViews)}.
Chris Wren8fd39ec2014-02-27 17:43:26 -0500308 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400309 @Deprecated
Chris Wren8fd39ec2014-02-27 17:43:26 -0500310 public RemoteViews headsUpContentView;
311
Daniel Sandlerf3b73432012-03-27 15:01:25 -0400312 /**
Dan Sandler86647982015-05-13 23:41:13 -0400313 * A large bitmap to be shown in the notification content area.
314 *
315 * @deprecated Use {@link Builder#setLargeIcon(Icon)} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800316 */
Dan Sandler86647982015-05-13 23:41:13 -0400317 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -0800318 public Bitmap largeIcon;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800319
320 /**
321 * The sound to play.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500322 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800323 * <p>
Chris Wren47c20a12014-06-18 17:27:29 -0400324 * A notification that is noisy is more likely to be presented as a heads-up notification.
325 * </p>
326 *
327 * <p>
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500328 * To play the default notification sound, see {@link #defaults}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800329 * </p>
330 */
331 public Uri sound;
332
333 /**
334 * Use this constant as the value for audioStreamType to request that
335 * the default stream type for notifications be used. Currently the
Jeff Sharkey098d5802012-04-26 17:30:34 -0700336 * default stream type is {@link AudioManager#STREAM_NOTIFICATION}.
John Spurlockc0650f022014-07-19 13:22:39 -0400337 *
338 * @deprecated Use {@link #audioAttributes} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800339 */
Jean-Michel Trivi81f871e2014-08-06 16:32:38 -0700340 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800341 public static final int STREAM_DEFAULT = -1;
342
343 /**
344 * The audio stream type to use when playing the sound.
345 * Should be one of the STREAM_ constants from
346 * {@link android.media.AudioManager}.
John Spurlockc0650f022014-07-19 13:22:39 -0400347 *
348 * @deprecated Use {@link #audioAttributes} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800349 */
Jean-Michel Trivi81f871e2014-08-06 16:32:38 -0700350 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800351 public int audioStreamType = STREAM_DEFAULT;
352
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800353 /**
John Spurlockc0650f022014-07-19 13:22:39 -0400354 * The default value of {@link #audioAttributes}.
355 */
356 public static final AudioAttributes AUDIO_ATTRIBUTES_DEFAULT = new AudioAttributes.Builder()
357 .setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
358 .setUsage(AudioAttributes.USAGE_NOTIFICATION)
359 .build();
360
361 /**
362 * The {@link AudioAttributes audio attributes} to use when playing the sound.
363 */
364 public AudioAttributes audioAttributes = AUDIO_ATTRIBUTES_DEFAULT;
365
366 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500367 * The pattern with which to vibrate.
368 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800369 * <p>
370 * To vibrate the default pattern, see {@link #defaults}.
371 * </p>
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500372 *
Chris Wren47c20a12014-06-18 17:27:29 -0400373 * <p>
374 * A notification that vibrates is more likely to be presented as a heads-up notification.
375 * </p>
376 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800377 * @see android.os.Vibrator#vibrate(long[],int)
378 */
379 public long[] vibrate;
380
381 /**
382 * The color of the led. The hardware will do its best approximation.
383 *
384 * @see #FLAG_SHOW_LIGHTS
385 * @see #flags
386 */
Tor Norbye80756e32015-03-02 09:39:27 -0800387 @ColorInt
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800388 public int ledARGB;
389
390 /**
391 * The number of milliseconds for the LED to be on while it's flashing.
392 * The hardware will do its best approximation.
393 *
394 * @see #FLAG_SHOW_LIGHTS
395 * @see #flags
396 */
397 public int ledOnMS;
398
399 /**
400 * The number of milliseconds for the LED to be off while it's flashing.
401 * The hardware will do its best approximation.
402 *
403 * @see #FLAG_SHOW_LIGHTS
404 * @see #flags
405 */
406 public int ledOffMS;
407
408 /**
409 * Specifies which values should be taken from the defaults.
410 * <p>
411 * To set, OR the desired from {@link #DEFAULT_SOUND},
412 * {@link #DEFAULT_VIBRATE}, {@link #DEFAULT_LIGHTS}. For all default
413 * values, use {@link #DEFAULT_ALL}.
414 * </p>
415 */
416 public int defaults;
417
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800418 /**
419 * Bit to be bitwise-ored into the {@link #flags} field that should be
420 * set if you want the LED on for this notification.
421 * <ul>
422 * <li>To turn the LED off, pass 0 in the alpha channel for colorARGB
423 * or 0 for both ledOnMS and ledOffMS.</li>
424 * <li>To turn the LED on, pass 1 for ledOnMS and 0 for ledOffMS.</li>
425 * <li>To flash the LED, pass the number of milliseconds that it should
426 * be on and off to ledOnMS and ledOffMS.</li>
427 * </ul>
428 * <p>
429 * Since hardware varies, you are not guaranteed that any of the values
430 * you pass are honored exactly. Use the system defaults (TODO) if possible
431 * because they will be set to values that work on any given hardware.
432 * <p>
433 * The alpha channel must be set for forward compatibility.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500434 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800435 */
436 public static final int FLAG_SHOW_LIGHTS = 0x00000001;
437
438 /**
439 * Bit to be bitwise-ored into the {@link #flags} field that should be
440 * set if this notification is in reference to something that is ongoing,
441 * like a phone call. It should not be set if this notification is in
442 * reference to something that happened at a particular point in time,
443 * like a missed phone call.
444 */
445 public static final int FLAG_ONGOING_EVENT = 0x00000002;
446
447 /**
448 * Bit to be bitwise-ored into the {@link #flags} field that if set,
Scott Mainb8b36452009-04-26 15:50:49 -0700449 * the audio will be repeated until the notification is
450 * cancelled or the notification window is opened.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800451 */
452 public static final int FLAG_INSISTENT = 0x00000004;
453
454 /**
455 * Bit to be bitwise-ored into the {@link #flags} field that should be
Griff Hazen293977b2014-04-28 08:37:20 -0700456 * set if you would only like the sound, vibrate and ticker to be played
457 * if the notification was not already showing.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800458 */
459 public static final int FLAG_ONLY_ALERT_ONCE = 0x00000008;
460
461 /**
462 * Bit to be bitwise-ored into the {@link #flags} field that should be
463 * set if the notification should be canceled when it is clicked by the
Daniel Sandler8aa9ae62012-12-04 23:31:47 -0500464 * user.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800465 */
466 public static final int FLAG_AUTO_CANCEL = 0x00000010;
467
468 /**
469 * Bit to be bitwise-ored into the {@link #flags} field that should be
470 * set if the notification should not be canceled when the user clicks
471 * the Clear all button.
472 */
473 public static final int FLAG_NO_CLEAR = 0x00000020;
474
Dianne Hackbornd8a43f62009-08-17 23:33:56 -0700475 /**
476 * Bit to be bitwise-ored into the {@link #flags} field that should be
477 * set if this notification represents a currently running service. This
478 * will normally be set for you by {@link Service#startForeground}.
479 */
480 public static final int FLAG_FOREGROUND_SERVICE = 0x00000040;
481
Daniel Sandlere46cbd32010-06-17 10:35:26 -0400482 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500483 * Obsolete flag indicating high-priority notifications; use the priority field instead.
Joe Malin8d40d042012-11-05 11:36:40 -0800484 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500485 * @deprecated Use {@link #priority} with a positive value.
Daniel Sandlere46cbd32010-06-17 10:35:26 -0400486 */
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500487 public static final int FLAG_HIGH_PRIORITY = 0x00000080;
Daniel Sandlere46cbd32010-06-17 10:35:26 -0400488
Griff Hazendfcb0802014-02-11 12:00:00 -0800489 /**
490 * Bit to be bitswise-ored into the {@link #flags} field that should be
491 * set if this notification is relevant to the current device only
492 * and it is not recommended that it bridge to other devices.
493 */
494 public static final int FLAG_LOCAL_ONLY = 0x00000100;
495
Griff Hazen5cadc3b2014-05-20 09:55:39 -0700496 /**
497 * Bit to be bitswise-ored into the {@link #flags} field that should be
498 * set if this notification is the group summary for a group of notifications.
499 * Grouped notifications may display in a cluster or stack on devices which
500 * support such rendering. Requires a group key also be set using {@link Builder#setGroup}.
501 */
502 public static final int FLAG_GROUP_SUMMARY = 0x00000200;
503
Julia Reynoldse46bb372016-03-17 11:05:58 -0400504 /**
505 * Bit to be bitswise-ored into the {@link #flags} field that should be
506 * set if this notification is the group summary for an auto-group of notifications.
507 *
508 * @hide
509 */
510 @SystemApi
511 public static final int FLAG_AUTOGROUP_SUMMARY = 0x00000400;
512
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800513 public int flags;
514
Tor Norbyed9273d62013-05-30 15:59:53 -0700515 /** @hide */
516 @IntDef({PRIORITY_DEFAULT,PRIORITY_LOW,PRIORITY_MIN,PRIORITY_HIGH,PRIORITY_MAX})
517 @Retention(RetentionPolicy.SOURCE)
518 public @interface Priority {}
519
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800520 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500521 * Default notification {@link #priority}. If your application does not prioritize its own
522 * notifications, use this value for all notifications.
523 */
524 public static final int PRIORITY_DEFAULT = 0;
525
526 /**
527 * Lower {@link #priority}, for items that are less important. The UI may choose to show these
528 * items smaller, or at a different position in the list, compared with your app's
529 * {@link #PRIORITY_DEFAULT} items.
530 */
531 public static final int PRIORITY_LOW = -1;
532
533 /**
534 * Lowest {@link #priority}; these items might not be shown to the user except under special
535 * circumstances, such as detailed notification logs.
536 */
537 public static final int PRIORITY_MIN = -2;
538
539 /**
540 * Higher {@link #priority}, for more important notifications or alerts. The UI may choose to
541 * show these items larger, or at a different position in notification lists, compared with
542 * your app's {@link #PRIORITY_DEFAULT} items.
543 */
544 public static final int PRIORITY_HIGH = 1;
545
546 /**
547 * Highest {@link #priority}, for your application's most important items that require the
548 * user's prompt attention or input.
549 */
550 public static final int PRIORITY_MAX = 2;
551
552 /**
553 * Relative priority for this notification.
Joe Malin8d40d042012-11-05 11:36:40 -0800554 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500555 * Priority is an indication of how much of the user's valuable attention should be consumed by
556 * this notification. Low-priority notifications may be hidden from the user in certain
557 * situations, while the user might be interrupted for a higher-priority notification. The
Daniel Sandler6738eee2012-11-16 12:03:32 -0500558 * system will make a determination about how to interpret this priority when presenting
559 * the notification.
Chris Wren47c20a12014-06-18 17:27:29 -0400560 *
561 * <p>
562 * A notification that is at least {@link #PRIORITY_HIGH} is more likely to be presented
563 * as a heads-up notification.
564 * </p>
565 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500566 */
Tor Norbyed9273d62013-05-30 15:59:53 -0700567 @Priority
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500568 public int priority;
Joe Malin8d40d042012-11-05 11:36:40 -0800569
Dan Sandler26e81cf2014-05-06 10:01:27 -0400570 /**
571 * Accent color (an ARGB integer like the constants in {@link android.graphics.Color})
572 * to be applied by the standard Style templates when presenting this notification.
573 *
574 * The current template design constructs a colorful header image by overlaying the
575 * {@link #icon} image (stenciled in white) atop a field of this color. Alpha components are
576 * ignored.
577 */
Tor Norbye80756e32015-03-02 09:39:27 -0800578 @ColorInt
Dan Sandler26e81cf2014-05-06 10:01:27 -0400579 public int color = COLOR_DEFAULT;
580
581 /**
582 * Special value of {@link #color} telling the system not to decorate this notification with
583 * any special color but instead use default colors when presenting this notification.
584 */
Tor Norbye80756e32015-03-02 09:39:27 -0800585 @ColorInt
Dan Sandler26e81cf2014-05-06 10:01:27 -0400586 public static final int COLOR_DEFAULT = 0; // AKA Color.TRANSPARENT
Dan Sandler0bf2ed82013-12-21 23:33:41 -0600587
588 /**
Adrian Roos4ff3b122016-02-01 12:26:13 -0800589 * Special value of {@link #color} used as a place holder for an invalid color.
590 */
591 @ColorInt
592 private static final int COLOR_INVALID = 1;
593
594 /**
Adrian Roosc1a80b02016-04-05 14:54:55 -0700595 * Sphere of visibility of this notification, which affects how and when the SystemUI reveals
596 * the notification's presence and contents in untrusted situations (namely, on the secure
Dan Sandler0bf2ed82013-12-21 23:33:41 -0600597 * lockscreen).
598 *
599 * The default level, {@link #VISIBILITY_PRIVATE}, behaves exactly as notifications have always
600 * done on Android: The notification's {@link #icon} and {@link #tickerText} (if available) are
601 * shown in all situations, but the contents are only available if the device is unlocked for
602 * the appropriate user.
603 *
604 * A more permissive policy can be expressed by {@link #VISIBILITY_PUBLIC}; such a notification
605 * can be read even in an "insecure" context (that is, above a secure lockscreen).
606 * To modify the public version of this notification—for example, to redact some portions—see
607 * {@link Builder#setPublicVersion(Notification)}.
608 *
609 * Finally, a notification can be made {@link #VISIBILITY_SECRET}, which will suppress its icon
610 * and ticker until the user has bypassed the lockscreen.
611 */
612 public int visibility;
613
Griff Hazenfc3922d2014-08-20 11:56:44 -0700614 /**
615 * Notification visibility: Show this notification in its entirety on all lockscreens.
616 *
617 * {@see #visibility}
618 */
Dan Sandler0bf2ed82013-12-21 23:33:41 -0600619 public static final int VISIBILITY_PUBLIC = 1;
Griff Hazenfc3922d2014-08-20 11:56:44 -0700620
621 /**
622 * Notification visibility: Show this notification on all lockscreens, but conceal sensitive or
623 * private information on secure lockscreens.
624 *
625 * {@see #visibility}
626 */
Dan Sandler0bf2ed82013-12-21 23:33:41 -0600627 public static final int VISIBILITY_PRIVATE = 0;
Griff Hazenfc3922d2014-08-20 11:56:44 -0700628
629 /**
630 * Notification visibility: Do not reveal any part of this notification on a secure lockscreen.
631 *
632 * {@see #visibility}
633 */
Dan Sandler0bf2ed82013-12-21 23:33:41 -0600634 public static final int VISIBILITY_SECRET = -1;
635
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500636 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400637 * Notification category: incoming call (voice or video) or similar synchronous communication request.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500638 */
John Spurlockfd7f1e02014-03-18 16:41:57 -0400639 public static final String CATEGORY_CALL = "call";
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500640
641 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400642 * Notification category: incoming direct message (SMS, instant message, etc.).
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500643 */
John Spurlockfd7f1e02014-03-18 16:41:57 -0400644 public static final String CATEGORY_MESSAGE = "msg";
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500645
646 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400647 * Notification category: asynchronous bulk message (email).
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500648 */
John Spurlockfd7f1e02014-03-18 16:41:57 -0400649 public static final String CATEGORY_EMAIL = "email";
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500650
651 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400652 * Notification category: calendar event.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500653 */
John Spurlockfd7f1e02014-03-18 16:41:57 -0400654 public static final String CATEGORY_EVENT = "event";
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500655
656 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400657 * Notification category: promotion or advertisement.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500658 */
John Spurlockfd7f1e02014-03-18 16:41:57 -0400659 public static final String CATEGORY_PROMO = "promo";
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500660
661 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400662 * Notification category: alarm or timer.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500663 */
John Spurlockfd7f1e02014-03-18 16:41:57 -0400664 public static final String CATEGORY_ALARM = "alarm";
665
666 /**
667 * Notification category: progress of a long-running background operation.
668 */
669 public static final String CATEGORY_PROGRESS = "progress";
670
671 /**
672 * Notification category: social network or sharing update.
673 */
674 public static final String CATEGORY_SOCIAL = "social";
675
676 /**
677 * Notification category: error in background operation or authentication status.
678 */
679 public static final String CATEGORY_ERROR = "err";
680
681 /**
682 * Notification category: media transport control for playback.
683 */
684 public static final String CATEGORY_TRANSPORT = "transport";
685
686 /**
687 * Notification category: system or device status update. Reserved for system use.
688 */
689 public static final String CATEGORY_SYSTEM = "sys";
690
691 /**
692 * Notification category: indication of running background service.
693 */
694 public static final String CATEGORY_SERVICE = "service";
695
696 /**
John Spurlock0a69c8c2014-03-21 13:30:57 -0400697 * Notification category: a specific, timely recommendation for a single thing.
698 * For example, a news app might want to recommend a news story it believes the user will
699 * want to read next.
700 */
701 public static final String CATEGORY_RECOMMENDATION = "recommendation";
702
703 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400704 * Notification category: ongoing information about device or contextual status.
705 */
706 public static final String CATEGORY_STATUS = "status";
707
708 /**
John Spurlock24d3dad2015-04-02 12:24:02 -0400709 * Notification category: user-scheduled reminder.
710 */
711 public static final String CATEGORY_REMINDER = "reminder";
712
713 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400714 * One of the predefined notification categories (see the <code>CATEGORY_*</code> constants)
715 * that best describes this Notification. May be used by the system for ranking and filtering.
716 */
717 public String category;
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500718
Griff Hazen5cadc3b2014-05-20 09:55:39 -0700719 private String mGroupKey;
720
721 /**
722 * Get the key used to group this notification into a cluster or stack
723 * with other notifications on devices which support such rendering.
724 */
725 public String getGroup() {
726 return mGroupKey;
727 }
728
729 private String mSortKey;
730
731 /**
732 * Get a sort key that orders this notification among other notifications from the
733 * same package. This can be useful if an external sort was already applied and an app
734 * would like to preserve this. Notifications will be sorted lexicographically using this
735 * value, although providing different priorities in addition to providing sort key may
736 * cause this value to be ignored.
737 *
738 * <p>This sort key can also be used to order members of a notification group. See
739 * {@link Builder#setGroup}.
740 *
741 * @see String#compareTo(String)
742 */
743 public String getSortKey() {
744 return mSortKey;
745 }
746
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500747 /**
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400748 * Additional semantic data to be carried around with this Notification.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400749 * <p>
750 * The extras keys defined here are intended to capture the original inputs to {@link Builder}
751 * APIs, and are intended to be used by
752 * {@link android.service.notification.NotificationListenerService} implementations to extract
753 * detailed information from notification objects.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500754 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400755 public Bundle extras = new Bundle();
756
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400757 /**
758 * {@link #extras} key: this is the title of the notification,
759 * as supplied to {@link Builder#setContentTitle(CharSequence)}.
760 */
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -0500761 public static final String EXTRA_TITLE = "android.title";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400762
763 /**
764 * {@link #extras} key: this is the title of the notification when shown in expanded form,
765 * e.g. as supplied to {@link BigTextStyle#setBigContentTitle(CharSequence)}.
766 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400767 public static final String EXTRA_TITLE_BIG = EXTRA_TITLE + ".big";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400768
769 /**
770 * {@link #extras} key: this is the main text payload, as supplied to
771 * {@link Builder#setContentText(CharSequence)}.
772 */
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -0500773 public static final String EXTRA_TEXT = "android.text";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400774
775 /**
776 * {@link #extras} key: this is a third line of text, as supplied to
777 * {@link Builder#setSubText(CharSequence)}.
778 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400779 public static final String EXTRA_SUB_TEXT = "android.subText";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400780
781 /**
Adrian Roose458aa82015-12-08 16:17:19 -0800782 * {@link #extras} key: this is the remote input history, as supplied to
783 * {@link Builder#setRemoteInputHistory(CharSequence[])}.
Adrian Roos005e7742016-04-13 15:02:20 -0700784 *
785 * Apps can fill this through {@link Builder#setRemoteInputHistory(CharSequence[])}
786 * with the most recent inputs that have been sent through a {@link RemoteInput} of this
787 * Notification and are expected to clear it once the it is no longer relevant (e.g. for chat
788 * notifications once the other party has responded).
789 *
790 * The extra with this key is of type CharSequence[] and contains the most recent entry at
791 * the 0 index, the second most recent at the 1 index, etc.
792 *
793 * @see Builder#setRemoteInputHistory(CharSequence[])
Adrian Roose458aa82015-12-08 16:17:19 -0800794 */
795 public static final String EXTRA_REMOTE_INPUT_HISTORY = "android.remoteInputHistory";
796
797 /**
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400798 * {@link #extras} key: this is a small piece of additional text as supplied to
799 * {@link Builder#setContentInfo(CharSequence)}.
800 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400801 public static final String EXTRA_INFO_TEXT = "android.infoText";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400802
803 /**
804 * {@link #extras} key: this is a line of summary information intended to be shown
805 * alongside expanded notifications, as supplied to (e.g.)
806 * {@link BigTextStyle#setSummaryText(CharSequence)}.
807 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400808 public static final String EXTRA_SUMMARY_TEXT = "android.summaryText";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400809
810 /**
Christoph Studer4600f9b2014-07-22 22:44:43 +0200811 * {@link #extras} key: this is the longer text shown in the big form of a
812 * {@link BigTextStyle} notification, as supplied to
813 * {@link BigTextStyle#bigText(CharSequence)}.
814 */
815 public static final String EXTRA_BIG_TEXT = "android.bigText";
816
817 /**
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400818 * {@link #extras} key: this is the resource ID of the notification's main small icon, as
819 * supplied to {@link Builder#setSmallIcon(int)}.
820 */
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -0500821 public static final String EXTRA_SMALL_ICON = "android.icon";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400822
823 /**
824 * {@link #extras} key: this is a bitmap to be used instead of the small icon when showing the
825 * notification payload, as
826 * supplied to {@link Builder#setLargeIcon(android.graphics.Bitmap)}.
827 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400828 public static final String EXTRA_LARGE_ICON = "android.largeIcon";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400829
830 /**
831 * {@link #extras} key: this is a bitmap to be used instead of the one from
832 * {@link Builder#setLargeIcon(android.graphics.Bitmap)} when the notification is
833 * shown in its expanded form, as supplied to
834 * {@link BigPictureStyle#bigLargeIcon(android.graphics.Bitmap)}.
835 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400836 public static final String EXTRA_LARGE_ICON_BIG = EXTRA_LARGE_ICON + ".big";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400837
838 /**
839 * {@link #extras} key: this is the progress value supplied to
840 * {@link Builder#setProgress(int, int, boolean)}.
841 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400842 public static final String EXTRA_PROGRESS = "android.progress";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400843
844 /**
845 * {@link #extras} key: this is the maximum value supplied to
846 * {@link Builder#setProgress(int, int, boolean)}.
847 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400848 public static final String EXTRA_PROGRESS_MAX = "android.progressMax";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400849
850 /**
851 * {@link #extras} key: whether the progress bar is indeterminate, supplied to
852 * {@link Builder#setProgress(int, int, boolean)}.
853 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400854 public static final String EXTRA_PROGRESS_INDETERMINATE = "android.progressIndeterminate";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400855
856 /**
857 * {@link #extras} key: whether {@link #when} should be shown as a count-up timer (specifically
858 * a {@link android.widget.Chronometer}) instead of a timestamp, as supplied to
859 * {@link Builder#setUsesChronometer(boolean)}.
860 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400861 public static final String EXTRA_SHOW_CHRONOMETER = "android.showChronometer";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400862
863 /**
Selim Cinek81c23aa2016-02-25 16:23:13 -0800864 * {@link #extras} key: whether the chronometer set on the notification should count down
865 * instead of counting up. Is only relevant if key {@link #EXTRA_SHOW_CHRONOMETER} is present.
866 */
867 public static final String EXTRA_CHRONOMETER_COUNTS_DOWN = "android.chronometerCountsDown";
868
869 /**
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400870 * {@link #extras} key: whether {@link #when} should be shown,
871 * as supplied to {@link Builder#setShowWhen(boolean)}.
872 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400873 public static final String EXTRA_SHOW_WHEN = "android.showWhen";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400874
875 /**
876 * {@link #extras} key: this is a bitmap to be shown in {@link BigPictureStyle} expanded
877 * notifications, supplied to {@link BigPictureStyle#bigPicture(android.graphics.Bitmap)}.
878 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400879 public static final String EXTRA_PICTURE = "android.picture";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400880
881 /**
882 * {@link #extras} key: An array of CharSequences to show in {@link InboxStyle} expanded
883 * notifications, each of which was supplied to {@link InboxStyle#addLine(CharSequence)}.
884 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400885 public static final String EXTRA_TEXT_LINES = "android.textLines";
Dan Sandler842dd772014-05-15 09:36:47 -0400886
887 /**
888 * {@link #extras} key: A string representing the name of the specific
889 * {@link android.app.Notification.Style} used to create this notification.
890 */
Chris Wren91ad5632013-06-05 15:05:57 -0400891 public static final String EXTRA_TEMPLATE = "android.template";
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400892
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400893 /**
Chris Wrene6c48932014-09-29 17:19:27 -0400894 * {@link #extras} key: A String array containing the people that this notification relates to,
895 * each of which was supplied to {@link Builder#addPerson(String)}.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400896 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400897 public static final String EXTRA_PEOPLE = "android.people";
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500898
899 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400900 * Allow certain system-generated notifications to appear before the device is provisioned.
901 * Only available to notifications coming from the android package.
902 * @hide
903 */
904 public static final String EXTRA_ALLOW_DURING_SETUP = "android.allowDuringSetup";
905
906 /**
Jose Limae9e3b3b2014-05-18 23:44:50 -0700907 * {@link #extras} key: A
908 * {@link android.content.ContentUris content URI} pointing to an image that can be displayed
909 * in the background when the notification is selected. The URI must point to an image stream
910 * suitable for passing into
911 * {@link android.graphics.BitmapFactory#decodeStream(java.io.InputStream)
912 * BitmapFactory.decodeStream}; all other content types will be ignored. The content provider
913 * URI used for this purpose must require no permissions to read the image data.
914 */
915 public static final String EXTRA_BACKGROUND_IMAGE_URI = "android.backgroundImageUri";
916
917 /**
Dan Sandler842dd772014-05-15 09:36:47 -0400918 * {@link #extras} key: A
Jeff Browndba34ba2014-06-24 20:46:03 -0700919 * {@link android.media.session.MediaSession.Token} associated with a
Dan Sandler842dd772014-05-15 09:36:47 -0400920 * {@link android.app.Notification.MediaStyle} notification.
921 */
922 public static final String EXTRA_MEDIA_SESSION = "android.mediaSession";
923
924 /**
Bryan Mawhinneye191f902014-07-22 12:50:09 +0100925 * {@link #extras} key: the indices of actions to be shown in the compact view,
926 * as supplied to (e.g.) {@link MediaStyle#setShowActionsInCompactView(int...)}.
927 */
928 public static final String EXTRA_COMPACT_ACTIONS = "android.compactActions";
929
Christoph Studer943aa672014-08-03 20:31:16 +0200930 /**
Alex Hillsfc737de2016-03-23 17:33:02 -0400931 * {@link #extras} key: the username to be displayed for all messages sent by the user including
932 * direct replies
933 * {@link android.app.Notification.MessagingStyle} notification.
934 */
935 public static final String EXTRA_SELF_DISPLAY_NAME = "android.selfDisplayName";
936
937 /**
938 * {@link #extras} key: a boolean describing whether the platform should automatically
939 * generate possible replies to
940 * {@link android.app.Notification.MessagingStyle.Message} objects provided by a
941 * {@link android.app.Notification.MessagingStyle} notification.
942 */
943 public static final String EXTRA_ALLOW_GENERATED_REPLIES = "android.allowGeneratedReplies";
944
945 /**
Alex Hillsd9b04d92016-04-11 16:38:16 -0400946 * {@link #extras} key: a {@link String} to be displayed as the title to a conversation
947 * represented by a {@link android.app.Notification.MessagingStyle}
Alex Hillsfc737de2016-03-23 17:33:02 -0400948 */
Alex Hillsd9b04d92016-04-11 16:38:16 -0400949 public static final String EXTRA_CONVERSATION_TITLE = "android.conversationTitle";
Alex Hillsfc737de2016-03-23 17:33:02 -0400950
951 /**
952 * {@link #extras} key: an array of {@link android.app.Notification.MessagingStyle.Message}
953 * bundles provided by a
954 * {@link android.app.Notification.MessagingStyle} notification.
955 */
956 public static final String EXTRA_MESSAGES = "android.messages";
957
958 /**
Kenny Guy8942bcd2014-09-08 21:09:47 +0100959 * {@link #extras} key: the user that built the notification.
960 *
961 * @hide
962 */
963 public static final String EXTRA_ORIGINATING_USERID = "android.originatingUserId";
964
965 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400966 * @hide
967 */
968 public static final String EXTRA_BUILDER_APPLICATION_INFO = "android.appInfo";
969
Selim Cinek247fa012016-02-18 09:50:48 -0800970 /**
971 * @hide
972 */
973 public static final String EXTRA_CONTAINS_CUSTOM_VIEW = "android.contains.customView";
974
Dan Sandler732bd6c2016-04-12 14:20:32 -0400975 /**
976 * @SystemApi
977 * @hide
978 */
979 public static final String EXTRA_SUBSTITUTE_APP_NAME = "android.substName";
980
Dan Sandlerd63f9322015-05-06 15:18:49 -0400981 private Icon mSmallIcon;
982 private Icon mLargeIcon;
983
Chris Wren51c75102013-07-16 20:49:17 -0400984 /**
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400985 * Structure to encapsulate a named action that can be shown as part of this notification.
986 * It must include an icon, a label, and a {@link PendingIntent} to be fired when the action is
987 * selected by the user.
988 * <p>
Griff Hazen959591e2014-05-15 22:26:18 -0700989 * Apps should use {@link Notification.Builder#addAction(int, CharSequence, PendingIntent)}
990 * or {@link Notification.Builder#addAction(Notification.Action)}
991 * to attach actions.
Daniel Sandlera0a938c2012-03-15 08:42:37 -0400992 */
Daniel Sandlerea2a3172013-02-20 22:24:20 -0500993 public static class Action implements Parcelable {
Griff Hazen959591e2014-05-15 22:26:18 -0700994 private final Bundle mExtras;
Dan Sandler86647982015-05-13 23:41:13 -0400995 private Icon mIcon;
Griff Hazen61a9e862014-05-22 16:05:19 -0700996 private final RemoteInput[] mRemoteInputs;
Griff Hazen959591e2014-05-15 22:26:18 -0700997
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400998 /**
999 * Small icon representing the action.
Dan Sandler86647982015-05-13 23:41:13 -04001000 *
1001 * @deprecated Use {@link Action#getIcon()} instead.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001002 */
Dan Sandler86647982015-05-13 23:41:13 -04001003 @Deprecated
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001004 public int icon;
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001005
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001006 /**
1007 * Title of the action.
1008 */
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001009 public CharSequence title;
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001010
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001011 /**
1012 * Intent to send when the user invokes this action. May be null, in which case the action
1013 * may be rendered in a disabled presentation by the system UI.
1014 */
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001015 public PendingIntent actionIntent;
Griff Hazen959591e2014-05-15 22:26:18 -07001016
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001017 private Action(Parcel in) {
Dan Sandler86647982015-05-13 23:41:13 -04001018 if (in.readInt() != 0) {
1019 mIcon = Icon.CREATOR.createFromParcel(in);
Dan Sandler68079d52015-07-22 10:45:30 -04001020 if (mIcon.getType() == Icon.TYPE_RESOURCE) {
1021 icon = mIcon.getResId();
1022 }
Dan Sandler86647982015-05-13 23:41:13 -04001023 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001024 title = TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(in);
1025 if (in.readInt() == 1) {
1026 actionIntent = PendingIntent.CREATOR.createFromParcel(in);
1027 }
Jeff Sharkeya04c7a72016-03-18 12:20:36 -06001028 mExtras = Bundle.setDefusable(in.readBundle(), true);
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001029 mRemoteInputs = in.createTypedArray(RemoteInput.CREATOR);
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001030 }
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001031
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001032 /**
Dan Sandler86647982015-05-13 23:41:13 -04001033 * @deprecated Use {@link android.app.Notification.Action.Builder}.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001034 */
Dan Sandler86647982015-05-13 23:41:13 -04001035 @Deprecated
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001036 public Action(int icon, CharSequence title, PendingIntent intent) {
Dan Sandler86647982015-05-13 23:41:13 -04001037 this(Icon.createWithResource("", icon), title, intent, new Bundle(), null);
Griff Hazen959591e2014-05-15 22:26:18 -07001038 }
1039
Dan Sandler86647982015-05-13 23:41:13 -04001040 private Action(Icon icon, CharSequence title, PendingIntent intent, Bundle extras,
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001041 RemoteInput[] remoteInputs) {
Dan Sandler86647982015-05-13 23:41:13 -04001042 this.mIcon = icon;
Gus Prevasf5bff46f2015-08-24 10:34:28 -04001043 if (icon != null && icon.getType() == Icon.TYPE_RESOURCE) {
1044 this.icon = icon.getResId();
1045 }
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001046 this.title = title;
1047 this.actionIntent = intent;
Griff Hazen959591e2014-05-15 22:26:18 -07001048 this.mExtras = extras != null ? extras : new Bundle();
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001049 this.mRemoteInputs = remoteInputs;
Griff Hazen959591e2014-05-15 22:26:18 -07001050 }
1051
1052 /**
Dan Sandler86647982015-05-13 23:41:13 -04001053 * Return an icon representing the action.
1054 */
1055 public Icon getIcon() {
1056 if (mIcon == null && icon != 0) {
1057 // you snuck an icon in here without using the builder; let's try to keep it
1058 mIcon = Icon.createWithResource("", icon);
1059 }
1060 return mIcon;
1061 }
1062
1063 /**
Griff Hazen959591e2014-05-15 22:26:18 -07001064 * Get additional metadata carried around with this Action.
1065 */
1066 public Bundle getExtras() {
1067 return mExtras;
1068 }
1069
1070 /**
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001071 * Get the list of inputs to be collected from the user when this action is sent.
1072 * May return null if no remote inputs were added.
1073 */
1074 public RemoteInput[] getRemoteInputs() {
1075 return mRemoteInputs;
1076 }
1077
1078 /**
Griff Hazen959591e2014-05-15 22:26:18 -07001079 * Builder class for {@link Action} objects.
1080 */
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001081 public static final class Builder {
Dan Sandler86647982015-05-13 23:41:13 -04001082 private final Icon mIcon;
Griff Hazen959591e2014-05-15 22:26:18 -07001083 private final CharSequence mTitle;
1084 private final PendingIntent mIntent;
1085 private final Bundle mExtras;
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001086 private ArrayList<RemoteInput> mRemoteInputs;
Griff Hazen959591e2014-05-15 22:26:18 -07001087
1088 /**
1089 * Construct a new builder for {@link Action} object.
1090 * @param icon icon to show for this action
1091 * @param title the title of the action
1092 * @param intent the {@link PendingIntent} to fire when users trigger this action
1093 */
Dan Sandler86647982015-05-13 23:41:13 -04001094 @Deprecated
Griff Hazen959591e2014-05-15 22:26:18 -07001095 public Builder(int icon, CharSequence title, PendingIntent intent) {
Dan Sandler86647982015-05-13 23:41:13 -04001096 this(Icon.createWithResource("", icon), title, intent, new Bundle(), null);
1097 }
1098
1099 /**
1100 * Construct a new builder for {@link Action} object.
1101 * @param icon icon to show for this action
1102 * @param title the title of the action
1103 * @param intent the {@link PendingIntent} to fire when users trigger this action
1104 */
1105 public Builder(Icon icon, CharSequence title, PendingIntent intent) {
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001106 this(icon, title, intent, new Bundle(), null);
Griff Hazen959591e2014-05-15 22:26:18 -07001107 }
1108
1109 /**
1110 * Construct a new builder for {@link Action} object using the fields from an
1111 * {@link Action}.
1112 * @param action the action to read fields from.
1113 */
1114 public Builder(Action action) {
Dan Sandler86647982015-05-13 23:41:13 -04001115 this(action.getIcon(), action.title, action.actionIntent, new Bundle(action.mExtras),
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001116 action.getRemoteInputs());
Griff Hazen959591e2014-05-15 22:26:18 -07001117 }
1118
Dan Sandler86647982015-05-13 23:41:13 -04001119 private Builder(Icon icon, CharSequence title, PendingIntent intent, Bundle extras,
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001120 RemoteInput[] remoteInputs) {
Griff Hazen959591e2014-05-15 22:26:18 -07001121 mIcon = icon;
1122 mTitle = title;
1123 mIntent = intent;
1124 mExtras = extras;
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001125 if (remoteInputs != null) {
1126 mRemoteInputs = new ArrayList<RemoteInput>(remoteInputs.length);
1127 Collections.addAll(mRemoteInputs, remoteInputs);
1128 }
Griff Hazen959591e2014-05-15 22:26:18 -07001129 }
1130
1131 /**
1132 * Merge additional metadata into this builder.
1133 *
1134 * <p>Values within the Bundle will replace existing extras values in this Builder.
1135 *
1136 * @see Notification.Action#extras
1137 */
1138 public Builder addExtras(Bundle extras) {
1139 if (extras != null) {
1140 mExtras.putAll(extras);
1141 }
1142 return this;
1143 }
1144
1145 /**
1146 * Get the metadata Bundle used by this Builder.
1147 *
1148 * <p>The returned Bundle is shared with this Builder.
1149 */
1150 public Bundle getExtras() {
1151 return mExtras;
1152 }
1153
1154 /**
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001155 * Add an input to be collected from the user when this action is sent.
1156 * Response values can be retrieved from the fired intent by using the
1157 * {@link RemoteInput#getResultsFromIntent} function.
1158 * @param remoteInput a {@link RemoteInput} to add to the action
1159 * @return this object for method chaining
1160 */
1161 public Builder addRemoteInput(RemoteInput remoteInput) {
1162 if (mRemoteInputs == null) {
1163 mRemoteInputs = new ArrayList<RemoteInput>();
1164 }
1165 mRemoteInputs.add(remoteInput);
1166 return this;
1167 }
1168
1169 /**
1170 * Apply an extender to this action builder. Extenders may be used to add
1171 * metadata or change options on this builder.
1172 */
Griff Hazen61a9e862014-05-22 16:05:19 -07001173 public Builder extend(Extender extender) {
1174 extender.extend(this);
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001175 return this;
1176 }
1177
1178 /**
Griff Hazen959591e2014-05-15 22:26:18 -07001179 * Combine all of the options that have been set and return a new {@link Action}
1180 * object.
1181 * @return the built action
1182 */
1183 public Action build() {
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001184 RemoteInput[] remoteInputs = mRemoteInputs != null
1185 ? mRemoteInputs.toArray(new RemoteInput[mRemoteInputs.size()]) : null;
1186 return new Action(mIcon, mTitle, mIntent, mExtras, remoteInputs);
Griff Hazen959591e2014-05-15 22:26:18 -07001187 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001188 }
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001189
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001190 @Override
1191 public Action clone() {
1192 return new Action(
Dan Sandler86647982015-05-13 23:41:13 -04001193 getIcon(),
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001194 title,
1195 actionIntent, // safe to alias
1196 new Bundle(mExtras),
1197 getRemoteInputs());
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001198 }
1199 @Override
1200 public int describeContents() {
1201 return 0;
1202 }
1203 @Override
1204 public void writeToParcel(Parcel out, int flags) {
Dan Sandler86647982015-05-13 23:41:13 -04001205 final Icon ic = getIcon();
1206 if (ic != null) {
1207 out.writeInt(1);
1208 ic.writeToParcel(out, 0);
1209 } else {
1210 out.writeInt(0);
1211 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001212 TextUtils.writeToParcel(title, out, flags);
1213 if (actionIntent != null) {
1214 out.writeInt(1);
1215 actionIntent.writeToParcel(out, flags);
1216 } else {
1217 out.writeInt(0);
1218 }
Griff Hazen959591e2014-05-15 22:26:18 -07001219 out.writeBundle(mExtras);
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001220 out.writeTypedArray(mRemoteInputs, flags);
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001221 }
Griff Hazen959591e2014-05-15 22:26:18 -07001222 public static final Parcelable.Creator<Action> CREATOR =
1223 new Parcelable.Creator<Action>() {
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001224 public Action createFromParcel(Parcel in) {
1225 return new Action(in);
1226 }
1227 public Action[] newArray(int size) {
1228 return new Action[size];
1229 }
1230 };
Griff Hazen61a9e862014-05-22 16:05:19 -07001231
1232 /**
1233 * Extender interface for use with {@link Builder#extend}. Extenders may be used to add
1234 * metadata or change options on an action builder.
1235 */
1236 public interface Extender {
1237 /**
1238 * Apply this extender to a notification action builder.
1239 * @param builder the builder to be modified.
1240 * @return the build object for chaining.
1241 */
1242 public Builder extend(Builder builder);
1243 }
1244
1245 /**
1246 * Wearable extender for notification actions. To add extensions to an action,
1247 * create a new {@link android.app.Notification.Action.WearableExtender} object using
1248 * the {@code WearableExtender()} constructor and apply it to a
1249 * {@link android.app.Notification.Action.Builder} using
1250 * {@link android.app.Notification.Action.Builder#extend}.
1251 *
1252 * <pre class="prettyprint">
1253 * Notification.Action action = new Notification.Action.Builder(
1254 * R.drawable.archive_all, "Archive all", actionIntent)
Griff Hazen14f57992014-05-26 09:07:14 -07001255 * .extend(new Notification.Action.WearableExtender()
Griff Hazen61a9e862014-05-22 16:05:19 -07001256 * .setAvailableOffline(false))
Griff Hazen14f57992014-05-26 09:07:14 -07001257 * .build();</pre>
Griff Hazen61a9e862014-05-22 16:05:19 -07001258 */
1259 public static final class WearableExtender implements Extender {
1260 /** Notification action extra which contains wearable extensions */
1261 private static final String EXTRA_WEARABLE_EXTENSIONS = "android.wearable.EXTENSIONS";
1262
Pete Gastaf6781d2014-10-07 15:17:05 -04001263 // Keys within EXTRA_WEARABLE_EXTENSIONS for wearable options.
Griff Hazen61a9e862014-05-22 16:05:19 -07001264 private static final String KEY_FLAGS = "flags";
Pete Gastaf6781d2014-10-07 15:17:05 -04001265 private static final String KEY_IN_PROGRESS_LABEL = "inProgressLabel";
1266 private static final String KEY_CONFIRM_LABEL = "confirmLabel";
1267 private static final String KEY_CANCEL_LABEL = "cancelLabel";
Griff Hazen61a9e862014-05-22 16:05:19 -07001268
1269 // Flags bitwise-ored to mFlags
1270 private static final int FLAG_AVAILABLE_OFFLINE = 0x1;
Alex Hills9ab3a232016-04-05 14:54:56 -04001271 private static final int FLAG_HINT_LAUNCHES_ACTIVITY = 1 << 1;
Griff Hazen61a9e862014-05-22 16:05:19 -07001272
1273 // Default value for flags integer
1274 private static final int DEFAULT_FLAGS = FLAG_AVAILABLE_OFFLINE;
1275
1276 private int mFlags = DEFAULT_FLAGS;
1277
Pete Gastaf6781d2014-10-07 15:17:05 -04001278 private CharSequence mInProgressLabel;
1279 private CharSequence mConfirmLabel;
1280 private CharSequence mCancelLabel;
1281
Griff Hazen61a9e862014-05-22 16:05:19 -07001282 /**
1283 * Create a {@link android.app.Notification.Action.WearableExtender} with default
1284 * options.
1285 */
1286 public WearableExtender() {
1287 }
1288
1289 /**
1290 * Create a {@link android.app.Notification.Action.WearableExtender} by reading
1291 * wearable options present in an existing notification action.
1292 * @param action the notification action to inspect.
1293 */
1294 public WearableExtender(Action action) {
1295 Bundle wearableBundle = action.getExtras().getBundle(EXTRA_WEARABLE_EXTENSIONS);
1296 if (wearableBundle != null) {
1297 mFlags = wearableBundle.getInt(KEY_FLAGS, DEFAULT_FLAGS);
Pete Gastaf6781d2014-10-07 15:17:05 -04001298 mInProgressLabel = wearableBundle.getCharSequence(KEY_IN_PROGRESS_LABEL);
1299 mConfirmLabel = wearableBundle.getCharSequence(KEY_CONFIRM_LABEL);
1300 mCancelLabel = wearableBundle.getCharSequence(KEY_CANCEL_LABEL);
Griff Hazen61a9e862014-05-22 16:05:19 -07001301 }
1302 }
1303
1304 /**
1305 * Apply wearable extensions to a notification action that is being built. This is
1306 * typically called by the {@link android.app.Notification.Action.Builder#extend}
1307 * method of {@link android.app.Notification.Action.Builder}.
1308 */
1309 @Override
1310 public Action.Builder extend(Action.Builder builder) {
1311 Bundle wearableBundle = new Bundle();
1312
1313 if (mFlags != DEFAULT_FLAGS) {
1314 wearableBundle.putInt(KEY_FLAGS, mFlags);
1315 }
Pete Gastaf6781d2014-10-07 15:17:05 -04001316 if (mInProgressLabel != null) {
1317 wearableBundle.putCharSequence(KEY_IN_PROGRESS_LABEL, mInProgressLabel);
1318 }
1319 if (mConfirmLabel != null) {
1320 wearableBundle.putCharSequence(KEY_CONFIRM_LABEL, mConfirmLabel);
1321 }
1322 if (mCancelLabel != null) {
1323 wearableBundle.putCharSequence(KEY_CANCEL_LABEL, mCancelLabel);
1324 }
Griff Hazen61a9e862014-05-22 16:05:19 -07001325
1326 builder.getExtras().putBundle(EXTRA_WEARABLE_EXTENSIONS, wearableBundle);
1327 return builder;
1328 }
1329
1330 @Override
1331 public WearableExtender clone() {
1332 WearableExtender that = new WearableExtender();
1333 that.mFlags = this.mFlags;
Pete Gastaf6781d2014-10-07 15:17:05 -04001334 that.mInProgressLabel = this.mInProgressLabel;
1335 that.mConfirmLabel = this.mConfirmLabel;
1336 that.mCancelLabel = this.mCancelLabel;
Griff Hazen61a9e862014-05-22 16:05:19 -07001337 return that;
1338 }
1339
1340 /**
1341 * Set whether this action is available when the wearable device is not connected to
1342 * a companion device. The user can still trigger this action when the wearable device is
1343 * offline, but a visual hint will indicate that the action may not be available.
1344 * Defaults to true.
1345 */
1346 public WearableExtender setAvailableOffline(boolean availableOffline) {
1347 setFlag(FLAG_AVAILABLE_OFFLINE, availableOffline);
1348 return this;
1349 }
1350
1351 /**
1352 * Get whether this action is available when the wearable device is not connected to
1353 * a companion device. The user can still trigger this action when the wearable device is
1354 * offline, but a visual hint will indicate that the action may not be available.
1355 * Defaults to true.
1356 */
1357 public boolean isAvailableOffline() {
1358 return (mFlags & FLAG_AVAILABLE_OFFLINE) != 0;
1359 }
1360
1361 private void setFlag(int mask, boolean value) {
1362 if (value) {
1363 mFlags |= mask;
1364 } else {
1365 mFlags &= ~mask;
1366 }
1367 }
Pete Gastaf6781d2014-10-07 15:17:05 -04001368
1369 /**
1370 * Set a label to display while the wearable is preparing to automatically execute the
1371 * action. This is usually a 'ing' verb ending in ellipsis like "Sending..."
1372 *
1373 * @param label the label to display while the action is being prepared to execute
1374 * @return this object for method chaining
1375 */
1376 public WearableExtender setInProgressLabel(CharSequence label) {
1377 mInProgressLabel = label;
1378 return this;
1379 }
1380
1381 /**
1382 * Get the label to display while the wearable is preparing to automatically execute
1383 * the action. This is usually a 'ing' verb ending in ellipsis like "Sending..."
1384 *
1385 * @return the label to display while the action is being prepared to execute
1386 */
1387 public CharSequence getInProgressLabel() {
1388 return mInProgressLabel;
1389 }
1390
1391 /**
1392 * Set a label to display to confirm that the action should be executed.
1393 * This is usually an imperative verb like "Send".
1394 *
1395 * @param label the label to confirm the action should be executed
1396 * @return this object for method chaining
1397 */
1398 public WearableExtender setConfirmLabel(CharSequence label) {
1399 mConfirmLabel = label;
1400 return this;
1401 }
1402
1403 /**
1404 * Get the label to display to confirm that the action should be executed.
1405 * This is usually an imperative verb like "Send".
1406 *
1407 * @return the label to confirm the action should be executed
1408 */
1409 public CharSequence getConfirmLabel() {
1410 return mConfirmLabel;
1411 }
1412
1413 /**
1414 * Set a label to display to cancel the action.
1415 * This is usually an imperative verb, like "Cancel".
1416 *
1417 * @param label the label to display to cancel the action
1418 * @return this object for method chaining
1419 */
1420 public WearableExtender setCancelLabel(CharSequence label) {
1421 mCancelLabel = label;
1422 return this;
1423 }
1424
1425 /**
1426 * Get the label to display to cancel the action.
1427 * This is usually an imperative verb like "Cancel".
1428 *
1429 * @return the label to display to cancel the action
1430 */
1431 public CharSequence getCancelLabel() {
1432 return mCancelLabel;
1433 }
Alex Hills9ab3a232016-04-05 14:54:56 -04001434
1435 /**
1436 * Set a hint that this Action will launch an {@link Activity} directly, telling the
1437 * platform that it can generate the appropriate transitions.
1438 * @param hintLaunchesActivity {@code true} if the content intent will launch
1439 * an activity and transitions should be generated, false otherwise.
1440 * @return this object for method chaining
1441 */
Alex Hills4ec3ff42016-04-12 11:36:18 -04001442 public WearableExtender setHintLaunchesActivity(
Alex Hills9ab3a232016-04-05 14:54:56 -04001443 boolean hintLaunchesActivity) {
1444 setFlag(FLAG_HINT_LAUNCHES_ACTIVITY, hintLaunchesActivity);
1445 return this;
1446 }
1447
1448 /**
1449 * Get a hint that this Action will launch an {@link Activity} directly, telling the
1450 * platform that it can generate the appropriate transitions
1451 * @return {@code true} if the content intent will launch an activity and transitions
1452 * should be generated, false otherwise. The default value is {@code false} if this was
1453 * never set.
1454 */
Alex Hills4ec3ff42016-04-12 11:36:18 -04001455 public boolean getHintLaunchesActivity() {
Alex Hills9ab3a232016-04-05 14:54:56 -04001456 return (mFlags & FLAG_HINT_LAUNCHES_ACTIVITY) != 0;
1457 }
Griff Hazen61a9e862014-05-22 16:05:19 -07001458 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001459 }
1460
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001461 /**
1462 * Array of all {@link Action} structures attached to this notification by
1463 * {@link Builder#addAction(int, CharSequence, PendingIntent)}. Mostly useful for instances of
1464 * {@link android.service.notification.NotificationListenerService} that provide an alternative
1465 * interface for invoking actions.
1466 */
Daniel Sandlerea2a3172013-02-20 22:24:20 -05001467 public Action[] actions;
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001468
1469 /**
Dan Sandler0bf2ed82013-12-21 23:33:41 -06001470 * Replacement version of this notification whose content will be shown
1471 * in an insecure context such as atop a secure keyguard. See {@link #visibility}
1472 * and {@link #VISIBILITY_PUBLIC}.
1473 */
1474 public Notification publicVersion;
1475
1476 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001477 * Constructs a Notification object with default values.
Joe Onorato46439ce2010-11-19 13:56:21 -08001478 * You might want to consider using {@link Builder} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001479 */
1480 public Notification()
1481 {
1482 this.when = System.currentTimeMillis();
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001483 this.priority = PRIORITY_DEFAULT;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001484 }
1485
1486 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001487 * @hide
1488 */
1489 public Notification(Context context, int icon, CharSequence tickerText, long when,
1490 CharSequence contentTitle, CharSequence contentText, Intent contentIntent)
1491 {
Chris Wren1ce4b6d2015-06-11 10:19:43 -04001492 new Builder(context)
1493 .setWhen(when)
1494 .setSmallIcon(icon)
1495 .setTicker(tickerText)
1496 .setContentTitle(contentTitle)
1497 .setContentText(contentText)
1498 .setContentIntent(PendingIntent.getActivity(context, 0, contentIntent, 0))
1499 .buildInto(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001500 }
1501
1502 /**
1503 * Constructs a Notification object with the information needed to
1504 * have a status bar icon without the standard expanded view.
1505 *
1506 * @param icon The resource id of the icon to put in the status bar.
1507 * @param tickerText The text that flows by in the status bar when the notification first
1508 * activates.
1509 * @param when The time to show in the time field. In the System.currentTimeMillis
1510 * timebase.
Joe Onorato46439ce2010-11-19 13:56:21 -08001511 *
1512 * @deprecated Use {@link Builder} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001513 */
Joe Onorato46439ce2010-11-19 13:56:21 -08001514 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001515 public Notification(int icon, CharSequence tickerText, long when)
1516 {
1517 this.icon = icon;
1518 this.tickerText = tickerText;
1519 this.when = when;
1520 }
1521
1522 /**
1523 * Unflatten the notification from a parcel.
1524 */
1525 public Notification(Parcel parcel)
1526 {
1527 int version = parcel.readInt();
1528
1529 when = parcel.readLong();
Dan Sandler3936e7a2015-05-19 20:59:12 -04001530 if (parcel.readInt() != 0) {
1531 mSmallIcon = Icon.CREATOR.createFromParcel(parcel);
Dan Sandler4e787062015-06-17 15:09:48 -04001532 if (mSmallIcon.getType() == Icon.TYPE_RESOURCE) {
1533 icon = mSmallIcon.getResId();
1534 }
Dan Sandler3936e7a2015-05-19 20:59:12 -04001535 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001536 number = parcel.readInt();
1537 if (parcel.readInt() != 0) {
1538 contentIntent = PendingIntent.CREATOR.createFromParcel(parcel);
1539 }
1540 if (parcel.readInt() != 0) {
1541 deleteIntent = PendingIntent.CREATOR.createFromParcel(parcel);
1542 }
1543 if (parcel.readInt() != 0) {
1544 tickerText = TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(parcel);
1545 }
1546 if (parcel.readInt() != 0) {
Joe Onorato46439ce2010-11-19 13:56:21 -08001547 tickerView = RemoteViews.CREATOR.createFromParcel(parcel);
Joe Onoratoef1e7762010-09-17 18:38:38 -04001548 }
1549 if (parcel.readInt() != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001550 contentView = RemoteViews.CREATOR.createFromParcel(parcel);
1551 }
Joe Onorato561d3852010-11-20 18:09:34 -08001552 if (parcel.readInt() != 0) {
Dan Sandlerd63f9322015-05-06 15:18:49 -04001553 mLargeIcon = Icon.CREATOR.createFromParcel(parcel);
Joe Onorato561d3852010-11-20 18:09:34 -08001554 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001555 defaults = parcel.readInt();
1556 flags = parcel.readInt();
1557 if (parcel.readInt() != 0) {
1558 sound = Uri.CREATOR.createFromParcel(parcel);
1559 }
1560
1561 audioStreamType = parcel.readInt();
John Spurlockc0650f022014-07-19 13:22:39 -04001562 if (parcel.readInt() != 0) {
1563 audioAttributes = AudioAttributes.CREATOR.createFromParcel(parcel);
1564 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001565 vibrate = parcel.createLongArray();
1566 ledARGB = parcel.readInt();
1567 ledOnMS = parcel.readInt();
1568 ledOffMS = parcel.readInt();
1569 iconLevel = parcel.readInt();
Daniel Sandlere46cbd32010-06-17 10:35:26 -04001570
1571 if (parcel.readInt() != 0) {
1572 fullScreenIntent = PendingIntent.CREATOR.createFromParcel(parcel);
1573 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001574
1575 priority = parcel.readInt();
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001576
John Spurlockfd7f1e02014-03-18 16:41:57 -04001577 category = parcel.readString();
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001578
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001579 mGroupKey = parcel.readString();
1580
1581 mSortKey = parcel.readString();
1582
Jeff Sharkeya04c7a72016-03-18 12:20:36 -06001583 extras = Bundle.setDefusable(parcel.readBundle(), true); // may be null
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001584
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001585 actions = parcel.createTypedArray(Action.CREATOR); // may be null
1586
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001587 if (parcel.readInt() != 0) {
Daniel Sandlerf3b73432012-03-27 15:01:25 -04001588 bigContentView = RemoteViews.CREATOR.createFromParcel(parcel);
1589 }
Dan Sandler0bf2ed82013-12-21 23:33:41 -06001590
Chris Wren8fd39ec2014-02-27 17:43:26 -05001591 if (parcel.readInt() != 0) {
1592 headsUpContentView = RemoteViews.CREATOR.createFromParcel(parcel);
1593 }
1594
Dan Sandler0bf2ed82013-12-21 23:33:41 -06001595 visibility = parcel.readInt();
1596
1597 if (parcel.readInt() != 0) {
1598 publicVersion = Notification.CREATOR.createFromParcel(parcel);
1599 }
Dan Sandler26e81cf2014-05-06 10:01:27 -04001600
1601 color = parcel.readInt();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001602 }
1603
Andy Stadler110988c2010-12-03 14:29:16 -08001604 @Override
Joe Onorato18e69df2010-05-17 22:26:12 -07001605 public Notification clone() {
1606 Notification that = new Notification();
Daniel Sandler1a497d32013-04-18 14:52:45 -04001607 cloneInto(that, true);
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001608 return that;
1609 }
Joe Onorato18e69df2010-05-17 22:26:12 -07001610
Daniel Sandler1a497d32013-04-18 14:52:45 -04001611 /**
1612 * Copy all (or if heavy is false, all except Bitmaps and RemoteViews) members
1613 * of this into that.
1614 * @hide
1615 */
1616 public void cloneInto(Notification that, boolean heavy) {
Joe Onorato18e69df2010-05-17 22:26:12 -07001617 that.when = this.when;
Dan Sandlerd63f9322015-05-06 15:18:49 -04001618 that.mSmallIcon = this.mSmallIcon;
Joe Onorato18e69df2010-05-17 22:26:12 -07001619 that.number = this.number;
1620
1621 // PendingIntents are global, so there's no reason (or way) to clone them.
1622 that.contentIntent = this.contentIntent;
1623 that.deleteIntent = this.deleteIntent;
Daniel Sandlere46cbd32010-06-17 10:35:26 -04001624 that.fullScreenIntent = this.fullScreenIntent;
Joe Onorato18e69df2010-05-17 22:26:12 -07001625
1626 if (this.tickerText != null) {
1627 that.tickerText = this.tickerText.toString();
1628 }
Daniel Sandler1a497d32013-04-18 14:52:45 -04001629 if (heavy && this.tickerView != null) {
Joe Onorato46439ce2010-11-19 13:56:21 -08001630 that.tickerView = this.tickerView.clone();
Joe Onoratoef1e7762010-09-17 18:38:38 -04001631 }
Daniel Sandler1a497d32013-04-18 14:52:45 -04001632 if (heavy && this.contentView != null) {
Joe Onorato18e69df2010-05-17 22:26:12 -07001633 that.contentView = this.contentView.clone();
1634 }
Dan Sandlerd63f9322015-05-06 15:18:49 -04001635 if (heavy && this.mLargeIcon != null) {
1636 that.mLargeIcon = this.mLargeIcon;
Joe Onorato561d3852010-11-20 18:09:34 -08001637 }
Jozef BABJAKa8b91832011-02-22 08:05:08 +01001638 that.iconLevel = this.iconLevel;
Joe Onorato18e69df2010-05-17 22:26:12 -07001639 that.sound = this.sound; // android.net.Uri is immutable
1640 that.audioStreamType = this.audioStreamType;
John Spurlockc0650f022014-07-19 13:22:39 -04001641 if (this.audioAttributes != null) {
1642 that.audioAttributes = new AudioAttributes.Builder(this.audioAttributes).build();
1643 }
Joe Onorato18e69df2010-05-17 22:26:12 -07001644
1645 final long[] vibrate = this.vibrate;
1646 if (vibrate != null) {
1647 final int N = vibrate.length;
1648 final long[] vib = that.vibrate = new long[N];
1649 System.arraycopy(vibrate, 0, vib, 0, N);
1650 }
1651
1652 that.ledARGB = this.ledARGB;
1653 that.ledOnMS = this.ledOnMS;
1654 that.ledOffMS = this.ledOffMS;
1655 that.defaults = this.defaults;
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001656
Joe Onorato18e69df2010-05-17 22:26:12 -07001657 that.flags = this.flags;
1658
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001659 that.priority = this.priority;
Joe Malin8d40d042012-11-05 11:36:40 -08001660
John Spurlockfd7f1e02014-03-18 16:41:57 -04001661 that.category = this.category;
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001662
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001663 that.mGroupKey = this.mGroupKey;
1664
1665 that.mSortKey = this.mSortKey;
1666
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001667 if (this.extras != null) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04001668 try {
1669 that.extras = new Bundle(this.extras);
1670 // will unparcel
1671 that.extras.size();
1672 } catch (BadParcelableException e) {
1673 Log.e(TAG, "could not unparcel extras from notification: " + this, e);
1674 that.extras = null;
1675 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001676 }
1677
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001678 if (this.actions != null) {
1679 that.actions = new Action[this.actions.length];
1680 for(int i=0; i<this.actions.length; i++) {
1681 that.actions[i] = this.actions[i].clone();
1682 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001683 }
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001684
Daniel Sandler1a497d32013-04-18 14:52:45 -04001685 if (heavy && this.bigContentView != null) {
Daniel Sandlerf3b73432012-03-27 15:01:25 -04001686 that.bigContentView = this.bigContentView.clone();
1687 }
Daniel Sandler1a497d32013-04-18 14:52:45 -04001688
Chris Wren8fd39ec2014-02-27 17:43:26 -05001689 if (heavy && this.headsUpContentView != null) {
1690 that.headsUpContentView = this.headsUpContentView.clone();
1691 }
1692
Dan Sandler0bf2ed82013-12-21 23:33:41 -06001693 that.visibility = this.visibility;
1694
1695 if (this.publicVersion != null) {
1696 that.publicVersion = new Notification();
1697 this.publicVersion.cloneInto(that.publicVersion, heavy);
1698 }
1699
Dan Sandler26e81cf2014-05-06 10:01:27 -04001700 that.color = this.color;
1701
Daniel Sandler1a497d32013-04-18 14:52:45 -04001702 if (!heavy) {
1703 that.lightenPayload(); // will clean out extras
1704 }
1705 }
1706
1707 /**
1708 * Removes heavyweight parts of the Notification object for archival or for sending to
1709 * listeners when the full contents are not necessary.
1710 * @hide
1711 */
1712 public final void lightenPayload() {
1713 tickerView = null;
1714 contentView = null;
1715 bigContentView = null;
Chris Wren8fd39ec2014-02-27 17:43:26 -05001716 headsUpContentView = null;
Dan Sandlerd63f9322015-05-06 15:18:49 -04001717 mLargeIcon = null;
Dan Sandler50128532015-12-08 15:42:41 -05001718 if (extras != null && !extras.isEmpty()) {
1719 final Set<String> keyset = extras.keySet();
1720 final int N = keyset.size();
1721 final String[] keys = keyset.toArray(new String[N]);
1722 for (int i=0; i<N; i++) {
1723 final String key = keys[i];
1724 final Object obj = extras.get(key);
1725 if (obj != null &&
1726 ( obj instanceof Parcelable
1727 || obj instanceof Parcelable[]
1728 || obj instanceof SparseArray
1729 || obj instanceof ArrayList)) {
1730 extras.remove(key);
1731 }
1732 }
Daniel Sandler1a497d32013-04-18 14:52:45 -04001733 }
Joe Onorato18e69df2010-05-17 22:26:12 -07001734 }
1735
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04001736 /**
1737 * Make sure this CharSequence is safe to put into a bundle, which basically
1738 * means it had better not be some custom Parcelable implementation.
1739 * @hide
1740 */
1741 public static CharSequence safeCharSequence(CharSequence cs) {
Christoph Studer535ec612014-09-03 15:47:47 +02001742 if (cs == null) return cs;
1743 if (cs.length() > MAX_CHARSEQUENCE_LENGTH) {
1744 cs = cs.subSequence(0, MAX_CHARSEQUENCE_LENGTH);
1745 }
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04001746 if (cs instanceof Parcelable) {
1747 Log.e(TAG, "warning: " + cs.getClass().getCanonicalName()
1748 + " instance is a custom Parcelable and not allowed in Notification");
1749 return cs.toString();
1750 }
Selim Cinek60a54252016-02-26 17:03:25 -08001751 return removeTextSizeSpans(cs);
1752 }
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04001753
Selim Cinek60a54252016-02-26 17:03:25 -08001754 private static CharSequence removeTextSizeSpans(CharSequence charSequence) {
1755 if (charSequence instanceof Spanned) {
1756 Spanned ss = (Spanned) charSequence;
1757 Object[] spans = ss.getSpans(0, ss.length(), Object.class);
1758 SpannableStringBuilder builder = new SpannableStringBuilder(ss.toString());
1759 for (Object span : spans) {
1760 Object resultSpan = span;
Selim Cinek89991a22016-03-07 19:51:54 -08001761 if (resultSpan instanceof CharacterStyle) {
1762 resultSpan = ((CharacterStyle) span).getUnderlying();
1763 }
1764 if (resultSpan instanceof TextAppearanceSpan) {
1765 TextAppearanceSpan originalSpan = (TextAppearanceSpan) resultSpan;
Selim Cinek60a54252016-02-26 17:03:25 -08001766 resultSpan = new TextAppearanceSpan(
1767 originalSpan.getFamily(),
1768 originalSpan.getTextStyle(),
1769 -1,
1770 originalSpan.getTextColor(),
1771 originalSpan.getLinkTextColor());
Selim Cinek89991a22016-03-07 19:51:54 -08001772 } else if (resultSpan instanceof RelativeSizeSpan
1773 || resultSpan instanceof AbsoluteSizeSpan) {
Selim Cinek60a54252016-02-26 17:03:25 -08001774 continue;
Selim Cinek89991a22016-03-07 19:51:54 -08001775 } else {
1776 resultSpan = span;
Selim Cinek60a54252016-02-26 17:03:25 -08001777 }
1778 builder.setSpan(resultSpan, ss.getSpanStart(span), ss.getSpanEnd(span),
1779 ss.getSpanFlags(span));
1780 }
1781 return builder;
1782 }
1783 return charSequence;
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04001784 }
1785
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001786 public int describeContents() {
1787 return 0;
1788 }
1789
1790 /**
Dan Sandler4e787062015-06-17 15:09:48 -04001791 * Flatten this notification into a parcel.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001792 */
1793 public void writeToParcel(Parcel parcel, int flags)
1794 {
1795 parcel.writeInt(1);
1796
1797 parcel.writeLong(when);
Dan Sandler4e787062015-06-17 15:09:48 -04001798 if (mSmallIcon == null && icon != 0) {
1799 // you snuck an icon in here without using the builder; let's try to keep it
1800 mSmallIcon = Icon.createWithResource("", icon);
1801 }
Dan Sandler3936e7a2015-05-19 20:59:12 -04001802 if (mSmallIcon != null) {
1803 parcel.writeInt(1);
1804 mSmallIcon.writeToParcel(parcel, 0);
1805 } else {
1806 parcel.writeInt(0);
1807 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001808 parcel.writeInt(number);
1809 if (contentIntent != null) {
1810 parcel.writeInt(1);
1811 contentIntent.writeToParcel(parcel, 0);
1812 } else {
1813 parcel.writeInt(0);
1814 }
1815 if (deleteIntent != null) {
1816 parcel.writeInt(1);
1817 deleteIntent.writeToParcel(parcel, 0);
1818 } else {
1819 parcel.writeInt(0);
1820 }
1821 if (tickerText != null) {
1822 parcel.writeInt(1);
1823 TextUtils.writeToParcel(tickerText, parcel, flags);
1824 } else {
1825 parcel.writeInt(0);
1826 }
Joe Onorato46439ce2010-11-19 13:56:21 -08001827 if (tickerView != null) {
Joe Onoratoef1e7762010-09-17 18:38:38 -04001828 parcel.writeInt(1);
Joe Onorato46439ce2010-11-19 13:56:21 -08001829 tickerView.writeToParcel(parcel, 0);
Joe Onoratoef1e7762010-09-17 18:38:38 -04001830 } else {
1831 parcel.writeInt(0);
1832 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001833 if (contentView != null) {
1834 parcel.writeInt(1);
1835 contentView.writeToParcel(parcel, 0);
1836 } else {
1837 parcel.writeInt(0);
1838 }
Dan Sandlerd63f9322015-05-06 15:18:49 -04001839 if (mLargeIcon != null) {
Joe Onorato561d3852010-11-20 18:09:34 -08001840 parcel.writeInt(1);
Dan Sandlerd63f9322015-05-06 15:18:49 -04001841 mLargeIcon.writeToParcel(parcel, 0);
Joe Onorato561d3852010-11-20 18:09:34 -08001842 } else {
1843 parcel.writeInt(0);
1844 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001845
1846 parcel.writeInt(defaults);
1847 parcel.writeInt(this.flags);
1848
1849 if (sound != null) {
1850 parcel.writeInt(1);
1851 sound.writeToParcel(parcel, 0);
1852 } else {
1853 parcel.writeInt(0);
1854 }
1855 parcel.writeInt(audioStreamType);
John Spurlockc0650f022014-07-19 13:22:39 -04001856
1857 if (audioAttributes != null) {
1858 parcel.writeInt(1);
1859 audioAttributes.writeToParcel(parcel, 0);
1860 } else {
1861 parcel.writeInt(0);
1862 }
1863
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001864 parcel.writeLongArray(vibrate);
1865 parcel.writeInt(ledARGB);
1866 parcel.writeInt(ledOnMS);
1867 parcel.writeInt(ledOffMS);
1868 parcel.writeInt(iconLevel);
Daniel Sandlere46cbd32010-06-17 10:35:26 -04001869
1870 if (fullScreenIntent != null) {
1871 parcel.writeInt(1);
1872 fullScreenIntent.writeToParcel(parcel, 0);
1873 } else {
1874 parcel.writeInt(0);
1875 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001876
1877 parcel.writeInt(priority);
Joe Malin8d40d042012-11-05 11:36:40 -08001878
John Spurlockfd7f1e02014-03-18 16:41:57 -04001879 parcel.writeString(category);
Joe Malin8d40d042012-11-05 11:36:40 -08001880
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001881 parcel.writeString(mGroupKey);
1882
1883 parcel.writeString(mSortKey);
1884
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001885 parcel.writeBundle(extras); // null ok
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001886
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001887 parcel.writeTypedArray(actions, 0); // null ok
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001888
Daniel Sandlerf3b73432012-03-27 15:01:25 -04001889 if (bigContentView != null) {
1890 parcel.writeInt(1);
1891 bigContentView.writeToParcel(parcel, 0);
1892 } else {
1893 parcel.writeInt(0);
1894 }
Dan Sandler0bf2ed82013-12-21 23:33:41 -06001895
Chris Wren8fd39ec2014-02-27 17:43:26 -05001896 if (headsUpContentView != null) {
1897 parcel.writeInt(1);
1898 headsUpContentView.writeToParcel(parcel, 0);
1899 } else {
1900 parcel.writeInt(0);
1901 }
1902
Dan Sandler0bf2ed82013-12-21 23:33:41 -06001903 parcel.writeInt(visibility);
1904
1905 if (publicVersion != null) {
1906 parcel.writeInt(1);
1907 publicVersion.writeToParcel(parcel, 0);
1908 } else {
1909 parcel.writeInt(0);
1910 }
Dan Sandler26e81cf2014-05-06 10:01:27 -04001911
1912 parcel.writeInt(color);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001913 }
1914
1915 /**
1916 * Parcelable.Creator that instantiates Notification objects
1917 */
1918 public static final Parcelable.Creator<Notification> CREATOR
1919 = new Parcelable.Creator<Notification>()
1920 {
1921 public Notification createFromParcel(Parcel parcel)
1922 {
1923 return new Notification(parcel);
1924 }
1925
1926 public Notification[] newArray(int size)
1927 {
1928 return new Notification[size];
1929 }
1930 };
1931
1932 /**
1933 * Sets the {@link #contentView} field to be a view with the standard "Latest Event"
1934 * layout.
1935 *
1936 * <p>Uses the {@link #icon} and {@link #when} fields to set the icon and time fields
1937 * in the view.</p>
1938 * @param context The context for your application / activity.
1939 * @param contentTitle The title that goes in the expanded entry.
1940 * @param contentText The text that goes in the expanded entry.
1941 * @param contentIntent The intent to launch when the user clicks the expanded notification.
1942 * If this is an activity, it must include the
1943 * {@link android.content.Intent#FLAG_ACTIVITY_NEW_TASK} flag, which requires
Scott Main7aee61f2011-02-08 11:25:01 -08001944 * that you take care of task management as described in the
1945 * <a href="{@docRoot}guide/topics/fundamentals/tasks-and-back-stack.html">Tasks and Back
1946 * Stack</a> document.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001947 *
Joe Onorato46439ce2010-11-19 13:56:21 -08001948 * @deprecated Use {@link Builder} instead.
Chris Wrena05db382015-06-24 15:18:34 -04001949 * @removed
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001950 */
Joe Onorato46439ce2010-11-19 13:56:21 -08001951 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001952 public void setLatestEventInfo(Context context,
1953 CharSequence contentTitle, CharSequence contentText, PendingIntent contentIntent) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04001954 if (context.getApplicationInfo().targetSdkVersion > Build.VERSION_CODES.LOLLIPOP_MR1){
1955 Log.e(TAG, "setLatestEventInfo() is deprecated and you should feel deprecated.",
1956 new Throwable());
1957 }
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001958
Julia Reynoldsd9228f12015-10-20 10:37:27 -04001959 // ensure that any information already set directly is preserved
1960 final Notification.Builder builder = new Notification.Builder(context, this);
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001961
1962 // now apply the latestEventInfo fields
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001963 if (contentTitle != null) {
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001964 builder.setContentTitle(contentTitle);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001965 }
1966 if (contentText != null) {
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001967 builder.setContentText(contentText);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001968 }
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001969 builder.setContentIntent(contentIntent);
Julia Reynoldsd9228f12015-10-20 10:37:27 -04001970
1971 builder.build(); // callers expect this notification to be ready to use
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001972 }
1973
Julia Reynoldsda303542015-11-23 14:00:20 -05001974 /**
1975 * @hide
1976 */
1977 public static void addFieldsFromContext(Context context, Notification notification) {
Jeff Sharkey012bc7b2016-04-11 16:30:27 -06001978 addFieldsFromContext(context.getApplicationInfo(), context.getUserId(), notification);
1979 }
1980
1981 /**
1982 * @hide
1983 */
1984 public static void addFieldsFromContext(ApplicationInfo ai, int userId,
1985 Notification notification) {
1986 notification.extras.putParcelable(EXTRA_BUILDER_APPLICATION_INFO, ai);
1987 notification.extras.putInt(EXTRA_ORIGINATING_USERID, userId);
Julia Reynoldsda303542015-11-23 14:00:20 -05001988 }
1989
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001990 @Override
1991 public String toString() {
1992 StringBuilder sb = new StringBuilder();
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001993 sb.append("Notification(pri=");
1994 sb.append(priority);
1995 sb.append(" contentView=");
Joe Onoratoc9596d62011-01-12 17:03:11 -08001996 if (contentView != null) {
1997 sb.append(contentView.getPackage());
1998 sb.append("/0x");
1999 sb.append(Integer.toHexString(contentView.getLayoutId()));
2000 } else {
2001 sb.append("null");
2002 }
2003 sb.append(" vibrate=");
Daniel Sandler6738eee2012-11-16 12:03:32 -05002004 if ((this.defaults & DEFAULT_VIBRATE) != 0) {
2005 sb.append("default");
2006 } else if (this.vibrate != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002007 int N = this.vibrate.length-1;
2008 sb.append("[");
2009 for (int i=0; i<N; i++) {
2010 sb.append(this.vibrate[i]);
2011 sb.append(',');
2012 }
Simon Schoar8cf97d92009-06-10 22:08:37 +02002013 if (N != -1) {
2014 sb.append(this.vibrate[N]);
2015 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002016 sb.append("]");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002017 } else {
2018 sb.append("null");
2019 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002020 sb.append(" sound=");
Daniel Sandler6738eee2012-11-16 12:03:32 -05002021 if ((this.defaults & DEFAULT_SOUND) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002022 sb.append("default");
Daniel Sandler6738eee2012-11-16 12:03:32 -05002023 } else if (this.sound != null) {
2024 sb.append(this.sound.toString());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002025 } else {
2026 sb.append("null");
2027 }
Chris Wren365b6d32015-07-16 10:39:26 -04002028 if (this.tickerText != null) {
2029 sb.append(" tick");
2030 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002031 sb.append(" defaults=0x");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002032 sb.append(Integer.toHexString(this.defaults));
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002033 sb.append(" flags=0x");
Daniel Sandlere46cbd32010-06-17 10:35:26 -04002034 sb.append(Integer.toHexString(this.flags));
Dan Sandler26e81cf2014-05-06 10:01:27 -04002035 sb.append(String.format(" color=0x%08x", this.color));
Griff Hazen5cadc3b2014-05-20 09:55:39 -07002036 if (this.category != null) {
2037 sb.append(" category=");
2038 sb.append(this.category);
2039 }
2040 if (this.mGroupKey != null) {
2041 sb.append(" groupKey=");
2042 sb.append(this.mGroupKey);
2043 }
2044 if (this.mSortKey != null) {
2045 sb.append(" sortKey=");
2046 sb.append(this.mSortKey);
2047 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002048 if (actions != null) {
Dan Sandler1b718782014-07-18 12:43:45 -04002049 sb.append(" actions=");
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002050 sb.append(actions.length);
Dan Sandler1b718782014-07-18 12:43:45 -04002051 }
2052 sb.append(" vis=");
2053 sb.append(visibilityToString(this.visibility));
2054 if (this.publicVersion != null) {
2055 sb.append(" publicVersion=");
2056 sb.append(publicVersion.toString());
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002057 }
2058 sb.append(")");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002059 return sb.toString();
2060 }
Joe Onorato46439ce2010-11-19 13:56:21 -08002061
Dan Sandler1b718782014-07-18 12:43:45 -04002062 /**
2063 * {@hide}
2064 */
2065 public static String visibilityToString(int vis) {
2066 switch (vis) {
2067 case VISIBILITY_PRIVATE:
2068 return "PRIVATE";
2069 case VISIBILITY_PUBLIC:
2070 return "PUBLIC";
2071 case VISIBILITY_SECRET:
2072 return "SECRET";
2073 default:
2074 return "UNKNOWN(" + String.valueOf(vis) + ")";
2075 }
2076 }
2077
Joe Onoratocb109a02011-01-18 17:57:41 -08002078 /**
John Spurlock1d881a12015-03-18 19:21:54 -04002079 * {@hide}
2080 */
2081 public static String priorityToString(@Priority int pri) {
2082 switch (pri) {
2083 case PRIORITY_MIN:
2084 return "MIN";
2085 case PRIORITY_LOW:
2086 return "LOW";
2087 case PRIORITY_DEFAULT:
2088 return "DEFAULT";
2089 case PRIORITY_HIGH:
2090 return "HIGH";
2091 case PRIORITY_MAX:
2092 return "MAX";
2093 default:
2094 return "UNKNOWN(" + String.valueOf(pri) + ")";
2095 }
2096 }
2097
2098 /**
Dan Sandlerd63f9322015-05-06 15:18:49 -04002099 * The small icon representing this notification in the status bar and content view.
2100 *
2101 * @return the small icon representing this notification.
2102 *
2103 * @see Builder#getSmallIcon()
2104 * @see Builder#setSmallIcon(Icon)
2105 */
2106 public Icon getSmallIcon() {
2107 return mSmallIcon;
2108 }
2109
2110 /**
2111 * Used when notifying to clean up legacy small icons.
2112 * @hide
2113 */
2114 public void setSmallIcon(Icon icon) {
2115 mSmallIcon = icon;
2116 }
2117
2118 /**
2119 * The large icon shown in this notification's content view.
2120 * @see Builder#getLargeIcon()
2121 * @see Builder#setLargeIcon(Icon)
2122 */
2123 public Icon getLargeIcon() {
2124 return mLargeIcon;
2125 }
2126
2127 /**
Christoph Studer4600f9b2014-07-22 22:44:43 +02002128 * @hide
2129 */
Christoph Studerc8db24b2014-07-25 17:50:30 +02002130 public boolean isGroupSummary() {
2131 return mGroupKey != null && (flags & FLAG_GROUP_SUMMARY) != 0;
2132 }
2133
2134 /**
2135 * @hide
2136 */
2137 public boolean isGroupChild() {
2138 return mGroupKey != null && (flags & FLAG_GROUP_SUMMARY) == 0;
2139 }
2140
2141 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002142 * Builder class for {@link Notification} objects.
Joe Malin8d40d042012-11-05 11:36:40 -08002143 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002144 * Provides a convenient way to set the various fields of a {@link Notification} and generate
Scott Main183bf112012-08-13 19:12:13 -07002145 * content views using the platform's notification layout template. If your app supports
2146 * versions of Android as old as API level 4, you can instead use
2147 * {@link android.support.v4.app.NotificationCompat.Builder NotificationCompat.Builder},
2148 * available in the <a href="{@docRoot}tools/extras/support-library.html">Android Support
2149 * library</a>.
Joe Malin8d40d042012-11-05 11:36:40 -08002150 *
Scott Main183bf112012-08-13 19:12:13 -07002151 * <p>Example:
Joe Malin8d40d042012-11-05 11:36:40 -08002152 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002153 * <pre class="prettyprint">
Scott Main183bf112012-08-13 19:12:13 -07002154 * Notification noti = new Notification.Builder(mContext)
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002155 * .setContentTitle(&quot;New mail from &quot; + sender.toString())
2156 * .setContentText(subject)
2157 * .setSmallIcon(R.drawable.new_mail)
2158 * .setLargeIcon(aBitmap)
Chris Wrenfbd96ba2012-05-01 12:03:58 -04002159 * .build();
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002160 * </pre>
Joe Onoratocb109a02011-01-18 17:57:41 -08002161 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002162 public static class Builder {
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05002163 /**
2164 * @hide
2165 */
2166 public static final String EXTRA_REBUILD_CONTENT_VIEW_ACTION_COUNT =
2167 "android.rebuild.contentViewActionCount";
2168 /**
2169 * @hide
2170 */
2171 public static final String EXTRA_REBUILD_BIG_CONTENT_VIEW_ACTION_COUNT
2172 = "android.rebuild.bigViewActionCount";
2173 /**
2174 * @hide
2175 */
2176 public static final String EXTRA_REBUILD_HEADS_UP_CONTENT_VIEW_ACTION_COUNT
2177 = "android.rebuild.hudViewActionCount";
2178
Daniel Sandler602ad1c2012-06-12 16:06:27 -04002179 private static final int MAX_ACTION_BUTTONS = 3;
Daniel Sandler8680bf82012-05-15 16:52:52 -04002180
Joe Onorato46439ce2010-11-19 13:56:21 -08002181 private Context mContext;
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002182 private Notification mN;
2183 private Bundle mUserExtras = new Bundle();
Chris Wrenfbd96ba2012-05-01 12:03:58 -04002184 private Style mStyle;
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002185 private ArrayList<Action> mActions = new ArrayList<Action>(MAX_ACTION_BUTTONS);
2186 private ArrayList<String> mPersonList = new ArrayList<String>();
2187 private NotificationColorUtil mColorUtil;
2188 private boolean mColorUtilInited = false;
Christoph Studer7ac80e62014-08-04 16:01:57 +02002189
2190 /**
Adrian Roos4ff3b122016-02-01 12:26:13 -08002191 * Caches a contrast-enhanced version of {@link #mCachedContrastColorIsFor}.
2192 */
2193 private int mCachedContrastColor = COLOR_INVALID;
2194 private int mCachedContrastColorIsFor = COLOR_INVALID;
2195
2196 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002197 * Constructs a new Builder with the defaults:
Joe Onoratocb109a02011-01-18 17:57:41 -08002198 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002199
2200 * <table>
2201 * <tr><th align=right>priority</th>
2202 * <td>{@link #PRIORITY_DEFAULT}</td></tr>
2203 * <tr><th align=right>when</th>
2204 * <td>now ({@link System#currentTimeMillis()})</td></tr>
2205 * <tr><th align=right>audio stream</th>
2206 * <td>{@link #STREAM_DEFAULT}</td></tr>
2207 * </table>
Joe Onoratocb109a02011-01-18 17:57:41 -08002208 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002209
2210 * @param context
2211 * A {@link Context} that will be used by the Builder to construct the
2212 * RemoteViews. The Context will not be held past the lifetime of this Builder
2213 * object.
Joe Onoratocb109a02011-01-18 17:57:41 -08002214 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002215 public Builder(Context context) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002216 this(context, null);
Joe Onorato46439ce2010-11-19 13:56:21 -08002217 }
2218
Joe Onoratocb109a02011-01-18 17:57:41 -08002219 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002220 * @hide
Christoph Studer4600f9b2014-07-22 22:44:43 +02002221 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002222 public Builder(Context context, Notification toAdopt) {
2223 mContext = context;
Christoph Studer4600f9b2014-07-22 22:44:43 +02002224
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002225 if (toAdopt == null) {
2226 mN = new Notification();
Selim Cinek0ff1ce602016-04-05 18:27:16 -07002227 if (context.getApplicationInfo().targetSdkVersion < Build.VERSION_CODES.N) {
2228 mN.extras.putBoolean(EXTRA_SHOW_WHEN, true);
2229 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002230 mN.priority = PRIORITY_DEFAULT;
2231 mN.visibility = VISIBILITY_PRIVATE;
2232 } else {
2233 mN = toAdopt;
2234 if (mN.actions != null) {
2235 Collections.addAll(mActions, mN.actions);
Christoph Studer4600f9b2014-07-22 22:44:43 +02002236 }
2237
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002238 if (mN.extras.containsKey(EXTRA_PEOPLE)) {
2239 Collections.addAll(mPersonList, mN.extras.getStringArray(EXTRA_PEOPLE));
2240 }
2241
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002242 String templateClass = mN.extras.getString(EXTRA_TEMPLATE);
2243 if (!TextUtils.isEmpty(templateClass)) {
2244 final Class<? extends Style> styleClass
2245 = getNotificationStyleClass(templateClass);
2246 if (styleClass == null) {
2247 Log.d(TAG, "Unknown style class: " + templateClass);
2248 } else {
2249 try {
Adrian Roosc1a80b02016-04-05 14:54:55 -07002250 final Constructor<? extends Style> ctor =
2251 styleClass.getDeclaredConstructor();
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002252 ctor.setAccessible(true);
2253 final Style style = ctor.newInstance();
2254 style.restoreFromExtras(mN.extras);
2255
2256 if (style != null) {
2257 setStyle(style);
2258 }
2259 } catch (Throwable t) {
2260 Log.e(TAG, "Could not create Style", t);
2261 }
2262 }
2263 }
2264
2265 }
2266 }
2267
2268 private NotificationColorUtil getColorUtil() {
2269 if (!mColorUtilInited) {
2270 mColorUtilInited = true;
2271 if (mContext.getApplicationInfo().targetSdkVersion < Build.VERSION_CODES.LOLLIPOP) {
2272 mColorUtil = NotificationColorUtil.getInstance(mContext);
Christoph Studer4600f9b2014-07-22 22:44:43 +02002273 }
2274 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002275 return mColorUtil;
Christoph Studer4600f9b2014-07-22 22:44:43 +02002276 }
2277
2278 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002279 * Add a timestamp pertaining to the notification (usually the time the event occurred).
Selim Cinek0ff1ce602016-04-05 18:27:16 -07002280 *
2281 * For apps targeting {@link android.os.Build.VERSION_CODES#N} and above, this time is not
2282 * shown anymore by default and must be opted into by using
2283 * {@link android.app.Notification.Builder#setShowWhen(boolean)}
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002284 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002285 * @see Notification#when
Joe Onoratocb109a02011-01-18 17:57:41 -08002286 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002287 public Builder setWhen(long when) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002288 mN.when = when;
Joe Onorato46439ce2010-11-19 13:56:21 -08002289 return this;
2290 }
2291
Joe Onoratocb109a02011-01-18 17:57:41 -08002292 /**
Griff Hazen50c11652014-05-16 09:46:31 -07002293 * Control whether the timestamp set with {@link #setWhen(long) setWhen} is shown
Daniel Sandler0c890492012-09-12 17:23:10 -07002294 * in the content view.
Selim Cinek0ff1ce602016-04-05 18:27:16 -07002295 * For apps targeting {@link android.os.Build.VERSION_CODES#N} and above, this defaults to
2296 * {@code false}. For earlier apps, the default is {@code true}.
Daniel Sandler0c890492012-09-12 17:23:10 -07002297 */
2298 public Builder setShowWhen(boolean show) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002299 mN.extras.putBoolean(EXTRA_SHOW_WHEN, show);
Daniel Sandler0c890492012-09-12 17:23:10 -07002300 return this;
2301 }
2302
2303 /**
Daniel Sandlerd33b8032012-05-10 11:41:48 -04002304 * Show the {@link Notification#when} field as a stopwatch.
Joe Malin8d40d042012-11-05 11:36:40 -08002305 *
2306 * Instead of presenting <code>when</code> as a timestamp, the notification will show an
Daniel Sandlerd33b8032012-05-10 11:41:48 -04002307 * automatically updating display of the minutes and seconds since <code>when</code>.
Daniel Sandlera2985ed2012-04-03 16:42:00 -04002308 *
Daniel Sandlerd33b8032012-05-10 11:41:48 -04002309 * Useful when showing an elapsed time (like an ongoing phone call).
2310 *
Selim Cinek81c23aa2016-02-25 16:23:13 -08002311 * The counter can also be set to count down to <code>when</code> when using
2312 * {@link #setChronometerCountsDown(boolean)}.
2313 *
Daniel Sandlerd33b8032012-05-10 11:41:48 -04002314 * @see android.widget.Chronometer
Daniel Sandlera2985ed2012-04-03 16:42:00 -04002315 * @see Notification#when
Selim Cinek81c23aa2016-02-25 16:23:13 -08002316 * @see #setChronometerCountsDown(boolean)
Daniel Sandlera2985ed2012-04-03 16:42:00 -04002317 */
2318 public Builder setUsesChronometer(boolean b) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002319 mN.extras.putBoolean(EXTRA_SHOW_CHRONOMETER, b);
Daniel Sandlera2985ed2012-04-03 16:42:00 -04002320 return this;
2321 }
2322
2323 /**
Selim Cinek81c23aa2016-02-25 16:23:13 -08002324 * Sets the Chronometer to count down instead of counting up.
2325 *
2326 * <p>This is only relevant if {@link #setUsesChronometer(boolean)} has been set to true.
2327 * If it isn't set the chronometer will count up.
2328 *
2329 * @see #setUsesChronometer(boolean)
2330 */
2331 public Builder setChronometerCountsDown(boolean countsDown) {
2332 mN.extras.putBoolean(EXTRA_CHRONOMETER_COUNTS_DOWN, countsDown);
2333 return this;
2334 }
2335
2336 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002337 * Set the small icon resource, which will be used to represent the notification in the
2338 * status bar.
Joe Onoratocb109a02011-01-18 17:57:41 -08002339 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002340
2341 * The platform template for the expanded view will draw this icon in the left, unless a
2342 * {@link #setLargeIcon(Bitmap) large icon} has also been specified, in which case the small
2343 * icon will be moved to the right-hand side.
2344 *
2345
2346 * @param icon
2347 * A resource ID in the application's package of the drawable to use.
2348 * @see Notification#icon
Joe Onoratocb109a02011-01-18 17:57:41 -08002349 */
Tor Norbye7b9c9122013-05-30 16:48:33 -07002350 public Builder setSmallIcon(@DrawableRes int icon) {
Dan Sandlerd63f9322015-05-06 15:18:49 -04002351 return setSmallIcon(icon != 0
2352 ? Icon.createWithResource(mContext, icon)
2353 : null);
Joe Onorato46439ce2010-11-19 13:56:21 -08002354 }
2355
Joe Onoratocb109a02011-01-18 17:57:41 -08002356 /**
2357 * A variant of {@link #setSmallIcon(int) setSmallIcon(int)} that takes an additional
2358 * level parameter for when the icon is a {@link android.graphics.drawable.LevelListDrawable
2359 * LevelListDrawable}.
2360 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002361 * @param icon A resource ID in the application's package of the drawable to use.
Joe Onoratocb109a02011-01-18 17:57:41 -08002362 * @param level The level to use for the icon.
2363 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002364 * @see Notification#icon
2365 * @see Notification#iconLevel
Joe Onoratocb109a02011-01-18 17:57:41 -08002366 */
Tor Norbye7b9c9122013-05-30 16:48:33 -07002367 public Builder setSmallIcon(@DrawableRes int icon, int level) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002368 mN.iconLevel = level;
Dan Sandlerd63f9322015-05-06 15:18:49 -04002369 return setSmallIcon(icon);
2370 }
2371
2372 /**
2373 * Set the small icon, which will be used to represent the notification in the
2374 * status bar and content view (unless overriden there by a
2375 * {@link #setLargeIcon(Bitmap) large icon}).
2376 *
2377 * @param icon An Icon object to use.
2378 * @see Notification#icon
2379 */
2380 public Builder setSmallIcon(Icon icon) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002381 mN.setSmallIcon(icon);
2382 if (icon != null && icon.getType() == Icon.TYPE_RESOURCE) {
2383 mN.icon = icon.getResId();
2384 }
Joe Onorato46439ce2010-11-19 13:56:21 -08002385 return this;
2386 }
2387
Joe Onoratocb109a02011-01-18 17:57:41 -08002388 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002389 * Set the first line of text in the platform notification template.
Joe Onoratocb109a02011-01-18 17:57:41 -08002390 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002391 public Builder setContentTitle(CharSequence title) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002392 mN.extras.putCharSequence(EXTRA_TITLE, safeCharSequence(title));
Joe Onorato46439ce2010-11-19 13:56:21 -08002393 return this;
2394 }
2395
Joe Onoratocb109a02011-01-18 17:57:41 -08002396 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002397 * Set the second line of text in the platform notification template.
Joe Onoratocb109a02011-01-18 17:57:41 -08002398 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002399 public Builder setContentText(CharSequence text) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002400 mN.extras.putCharSequence(EXTRA_TEXT, safeCharSequence(text));
Joe Onorato46439ce2010-11-19 13:56:21 -08002401 return this;
2402 }
2403
Joe Onoratocb109a02011-01-18 17:57:41 -08002404 /**
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07002405 * This provides some additional information that is displayed in the notification. No
2406 * guarantees are given where exactly it is displayed.
2407 *
2408 * <p>This information should only be provided if it provides an essential
2409 * benefit to the understanding of the notification. The more text you provide the
2410 * less readable it becomes. For example, an email client should only provide the account
2411 * name here if more than one email account has been added.</p>
2412 *
2413 * <p>As of {@link android.os.Build.VERSION_CODES#N} this information is displayed in the
2414 * notification header area.
2415 *
2416 * On Android versions before {@link android.os.Build.VERSION_CODES#N}
2417 * this will be shown in the third line of text in the platform notification template.
2418 * You should not be using {@link #setProgress(int, int, boolean)} at the
2419 * same time on those versions; they occupy the same place.
2420 * </p>
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002421 */
2422 public Builder setSubText(CharSequence text) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002423 mN.extras.putCharSequence(EXTRA_SUB_TEXT, safeCharSequence(text));
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002424 return this;
2425 }
2426
2427 /**
Adrian Roose458aa82015-12-08 16:17:19 -08002428 * Set the remote input history.
2429 *
2430 * This should be set to the most recent inputs that have been sent
2431 * through a {@link RemoteInput} of this Notification and cleared once the it is no
2432 * longer relevant (e.g. for chat notifications once the other party has responded).
2433 *
2434 * The most recent input must be stored at the 0 index, the second most recent at the
2435 * 1 index, etc. Note that the system will limit both how far back the inputs will be shown
2436 * and how much of each individual input is shown.
2437 *
2438 * <p>Note: The reply text will only be shown on notifications that have least one action
2439 * with a {@code RemoteInput}.</p>
2440 */
2441 public Builder setRemoteInputHistory(CharSequence[] text) {
2442 if (text == null) {
2443 mN.extras.putCharSequenceArray(EXTRA_REMOTE_INPUT_HISTORY, null);
2444 } else {
2445 final int N = Math.min(MAX_REPLY_HISTORY, text.length);
2446 CharSequence[] safe = new CharSequence[N];
2447 for (int i = 0; i < N; i++) {
2448 safe[i] = safeCharSequence(text[i]);
2449 }
2450 mN.extras.putCharSequenceArray(EXTRA_REMOTE_INPUT_HISTORY, safe);
2451 }
2452 return this;
2453 }
2454
2455 /**
Joe Onoratocb109a02011-01-18 17:57:41 -08002456 * Set the large number at the right-hand side of the notification. This is
2457 * equivalent to setContentInfo, although it might show the number in a different
2458 * font size for readability.
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07002459 *
2460 * @deprecated this number is not shown anywhere anymore
Joe Onoratocb109a02011-01-18 17:57:41 -08002461 */
Joe Onorato8595a3d2010-11-19 18:12:07 -08002462 public Builder setNumber(int number) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002463 mN.number = number;
Joe Onorato8595a3d2010-11-19 18:12:07 -08002464 return this;
2465 }
2466
Joe Onoratocb109a02011-01-18 17:57:41 -08002467 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002468 * A small piece of additional information pertaining to this notification.
2469 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002470 * The platform template will draw this on the last line of the notification, at the far
2471 * right (to the right of a smallIcon if it has been placed there).
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07002472 *
2473 * @deprecated use {@link #setSubText(CharSequence)} instead to set a text in the header.
2474 * For legacy apps targeting a version below {@link android.os.Build.VERSION_CODES#N} this
2475 * field will still show up, but the subtext will take precedence.
Joe Onoratocb109a02011-01-18 17:57:41 -08002476 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002477 public Builder setContentInfo(CharSequence info) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002478 mN.extras.putCharSequence(EXTRA_INFO_TEXT, safeCharSequence(info));
Joe Onorato46439ce2010-11-19 13:56:21 -08002479 return this;
2480 }
2481
Joe Onoratocb109a02011-01-18 17:57:41 -08002482 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002483 * Set the progress this notification represents.
2484 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002485 * The platform template will represent this using a {@link ProgressBar}.
Jeff Sharkey1c400132011-08-05 14:50:13 -07002486 */
2487 public Builder setProgress(int max, int progress, boolean indeterminate) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002488 mN.extras.putInt(EXTRA_PROGRESS, progress);
2489 mN.extras.putInt(EXTRA_PROGRESS_MAX, max);
2490 mN.extras.putBoolean(EXTRA_PROGRESS_INDETERMINATE, indeterminate);
Jeff Sharkey1c400132011-08-05 14:50:13 -07002491 return this;
2492 }
2493
2494 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002495 * Supply a custom RemoteViews to use instead of the platform template.
2496 *
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002497 * Use {@link #setCustomContentView(RemoteViews)} instead.
Joe Onoratocb109a02011-01-18 17:57:41 -08002498 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002499 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -08002500 public Builder setContent(RemoteViews views) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002501 return setCustomContentView(views);
2502 }
2503
2504 /**
2505 * Supply custom RemoteViews to use instead of the platform template.
2506 *
2507 * This will override the layout that would otherwise be constructed by this Builder
2508 * object.
2509 */
2510 public Builder setCustomContentView(RemoteViews contentView) {
2511 mN.contentView = contentView;
2512 return this;
2513 }
2514
2515 /**
2516 * Supply custom RemoteViews to use instead of the platform template in the expanded form.
2517 *
2518 * This will override the expanded layout that would otherwise be constructed by this
2519 * Builder object.
2520 */
2521 public Builder setCustomBigContentView(RemoteViews contentView) {
2522 mN.bigContentView = contentView;
2523 return this;
2524 }
2525
2526 /**
2527 * Supply custom RemoteViews to use instead of the platform template in the heads up dialog.
2528 *
2529 * This will override the heads-up layout that would otherwise be constructed by this
2530 * Builder object.
2531 */
2532 public Builder setCustomHeadsUpContentView(RemoteViews contentView) {
2533 mN.headsUpContentView = contentView;
Joe Onorato46439ce2010-11-19 13:56:21 -08002534 return this;
2535 }
2536
Joe Onoratocb109a02011-01-18 17:57:41 -08002537 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002538 * Supply a {@link PendingIntent} to be sent when the notification is clicked.
2539 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002540 * As of {@link android.os.Build.VERSION_CODES#HONEYCOMB}, if this field is unset and you
2541 * have specified a custom RemoteViews with {@link #setContent(RemoteViews)}, you can use
2542 * {@link RemoteViews#setOnClickPendingIntent RemoteViews.setOnClickPendingIntent(int,PendingIntent)}
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002543 * to assign PendingIntents to individual views in that custom layout (i.e., to create
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002544 * clickable buttons inside the notification view).
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002545 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002546 * @see Notification#contentIntent Notification.contentIntent
Joe Onoratocb109a02011-01-18 17:57:41 -08002547 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002548 public Builder setContentIntent(PendingIntent intent) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002549 mN.contentIntent = intent;
Joe Onorato46439ce2010-11-19 13:56:21 -08002550 return this;
2551 }
2552
Joe Onoratocb109a02011-01-18 17:57:41 -08002553 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002554 * Supply a {@link PendingIntent} to send when the notification is cleared explicitly by the user.
2555 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002556 * @see Notification#deleteIntent
Joe Onoratocb109a02011-01-18 17:57:41 -08002557 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002558 public Builder setDeleteIntent(PendingIntent intent) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002559 mN.deleteIntent = intent;
Joe Onorato46439ce2010-11-19 13:56:21 -08002560 return this;
2561 }
2562
Joe Onoratocb109a02011-01-18 17:57:41 -08002563 /**
2564 * An intent to launch instead of posting the notification to the status bar.
2565 * Only for use with extremely high-priority notifications demanding the user's
2566 * <strong>immediate</strong> attention, such as an incoming phone call or
2567 * alarm clock that the user has explicitly set to a particular time.
2568 * If this facility is used for something else, please give the user an option
2569 * to turn it off and use a normal notification, as this can be extremely
2570 * disruptive.
2571 *
Chris Wren47c20a12014-06-18 17:27:29 -04002572 * <p>
2573 * The system UI may choose to display a heads-up notification, instead of
2574 * launching this intent, while the user is using the device.
2575 * </p>
2576 *
Joe Onoratocb109a02011-01-18 17:57:41 -08002577 * @param intent The pending intent to launch.
2578 * @param highPriority Passing true will cause this notification to be sent
2579 * even if other notifications are suppressed.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002580 *
2581 * @see Notification#fullScreenIntent
Joe Onoratocb109a02011-01-18 17:57:41 -08002582 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002583 public Builder setFullScreenIntent(PendingIntent intent, boolean highPriority) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002584 mN.fullScreenIntent = intent;
Joe Onorato46439ce2010-11-19 13:56:21 -08002585 setFlag(FLAG_HIGH_PRIORITY, highPriority);
2586 return this;
2587 }
2588
Joe Onoratocb109a02011-01-18 17:57:41 -08002589 /**
Dan Sandler5fcdf6e2014-07-18 11:31:15 -04002590 * Set the "ticker" text which is sent to accessibility services.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002591 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002592 * @see Notification#tickerText
Joe Onoratocb109a02011-01-18 17:57:41 -08002593 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002594 public Builder setTicker(CharSequence tickerText) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002595 mN.tickerText = safeCharSequence(tickerText);
Joe Onorato46439ce2010-11-19 13:56:21 -08002596 return this;
2597 }
2598
Joe Onoratocb109a02011-01-18 17:57:41 -08002599 /**
Dan Sandler5fcdf6e2014-07-18 11:31:15 -04002600 * Obsolete version of {@link #setTicker(CharSequence)}.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002601 *
Joe Onoratocb109a02011-01-18 17:57:41 -08002602 */
Dan Sandler5fcdf6e2014-07-18 11:31:15 -04002603 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -08002604 public Builder setTicker(CharSequence tickerText, RemoteViews views) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002605 setTicker(tickerText);
2606 // views is ignored
Joe Onorato46439ce2010-11-19 13:56:21 -08002607 return this;
2608 }
2609
Joe Onoratocb109a02011-01-18 17:57:41 -08002610 /**
Dan Sandlerd63f9322015-05-06 15:18:49 -04002611 * Add a large icon to the notification content view.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002612 *
2613 * In the platform template, this image will be shown on the left of the notification view
Dan Sandlerd63f9322015-05-06 15:18:49 -04002614 * in place of the {@link #setSmallIcon(Icon) small icon} (which will be placed in a small
2615 * badge atop the large icon).
Dan Sandler08a04c12015-05-06 15:18:49 -04002616 */
Dan Sandlerd63f9322015-05-06 15:18:49 -04002617 public Builder setLargeIcon(Bitmap b) {
2618 return setLargeIcon(b != null ? Icon.createWithBitmap(b) : null);
2619 }
2620
2621 /**
2622 * Add a large icon to the notification content view.
2623 *
2624 * In the platform template, this image will be shown on the left of the notification view
2625 * in place of the {@link #setSmallIcon(Icon) small icon} (which will be placed in a small
2626 * badge atop the large icon).
2627 */
2628 public Builder setLargeIcon(Icon icon) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002629 mN.mLargeIcon = icon;
2630 mN.extras.putParcelable(EXTRA_LARGE_ICON, icon);
Joe Onorato46439ce2010-11-19 13:56:21 -08002631 return this;
2632 }
2633
Joe Onoratocb109a02011-01-18 17:57:41 -08002634 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002635 * Set the sound to play.
2636 *
John Spurlockc0650f022014-07-19 13:22:39 -04002637 * It will be played using the {@link #AUDIO_ATTRIBUTES_DEFAULT default audio attributes}
2638 * for notifications.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002639 *
Chris Wren47c20a12014-06-18 17:27:29 -04002640 * <p>
2641 * A notification that is noisy is more likely to be presented as a heads-up notification.
2642 * </p>
2643 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002644 * @see Notification#sound
Joe Onoratocb109a02011-01-18 17:57:41 -08002645 */
Joe Onorato52f80cd2010-11-21 15:34:48 -08002646 public Builder setSound(Uri sound) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002647 mN.sound = sound;
2648 mN.audioAttributes = AUDIO_ATTRIBUTES_DEFAULT;
Joe Onorato52f80cd2010-11-21 15:34:48 -08002649 return this;
2650 }
2651
Joe Onoratocb109a02011-01-18 17:57:41 -08002652 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002653 * Set the sound to play, along with a specific stream on which to play it.
Joe Onoratocb109a02011-01-18 17:57:41 -08002654 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002655 * See {@link android.media.AudioManager} for the <code>STREAM_</code> constants.
2656 *
Chris Wren47c20a12014-06-18 17:27:29 -04002657 * <p>
2658 * A notification that is noisy is more likely to be presented as a heads-up notification.
2659 * </p>
John Spurlockc0650f022014-07-19 13:22:39 -04002660 * @deprecated use {@link #setSound(Uri, AudioAttributes)} instead.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002661 * @see Notification#sound
Joe Onoratocb109a02011-01-18 17:57:41 -08002662 */
Jean-Michel Trivi81f871e2014-08-06 16:32:38 -07002663 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -08002664 public Builder setSound(Uri sound, int streamType) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002665 mN.sound = sound;
2666 mN.audioStreamType = streamType;
Joe Onorato46439ce2010-11-19 13:56:21 -08002667 return this;
2668 }
2669
Joe Onoratocb109a02011-01-18 17:57:41 -08002670 /**
John Spurlockc0650f022014-07-19 13:22:39 -04002671 * Set the sound to play, along with specific {@link AudioAttributes audio attributes} to
2672 * use during playback.
2673 *
2674 * <p>
2675 * A notification that is noisy is more likely to be presented as a heads-up notification.
2676 * </p>
2677 *
2678 * @see Notification#sound
2679 */
2680 public Builder setSound(Uri sound, AudioAttributes audioAttributes) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002681 mN.sound = sound;
2682 mN.audioAttributes = audioAttributes;
John Spurlockc0650f022014-07-19 13:22:39 -04002683 return this;
2684 }
2685
2686 /**
Joe Onoratocb109a02011-01-18 17:57:41 -08002687 * Set the vibration pattern to use.
2688 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002689 * See {@link android.os.Vibrator#vibrate(long[], int)} for a discussion of the
2690 * <code>pattern</code> parameter.
2691 *
Chris Wren47c20a12014-06-18 17:27:29 -04002692 * <p>
2693 * A notification that vibrates is more likely to be presented as a heads-up notification.
2694 * </p>
2695 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002696 * @see Notification#vibrate
Joe Onoratocb109a02011-01-18 17:57:41 -08002697 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002698 public Builder setVibrate(long[] pattern) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002699 mN.vibrate = pattern;
Joe Onorato46439ce2010-11-19 13:56:21 -08002700 return this;
2701 }
2702
Joe Onoratocb109a02011-01-18 17:57:41 -08002703 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002704 * Set the desired color for the indicator LED on the device, as well as the
2705 * blink duty cycle (specified in milliseconds).
2706 *
2707
2708 * Not all devices will honor all (or even any) of these values.
2709 *
2710
2711 * @see Notification#ledARGB
2712 * @see Notification#ledOnMS
2713 * @see Notification#ledOffMS
Joe Onoratocb109a02011-01-18 17:57:41 -08002714 */
Tor Norbye80756e32015-03-02 09:39:27 -08002715 public Builder setLights(@ColorInt int argb, int onMs, int offMs) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002716 mN.ledARGB = argb;
2717 mN.ledOnMS = onMs;
2718 mN.ledOffMS = offMs;
Julia Reynolds10ee1fc2015-11-09 11:04:55 -05002719 if (onMs != 0 || offMs != 0) {
2720 mN.flags |= FLAG_SHOW_LIGHTS;
2721 }
Joe Onorato46439ce2010-11-19 13:56:21 -08002722 return this;
2723 }
2724
Joe Onoratocb109a02011-01-18 17:57:41 -08002725 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002726 * Set whether this is an "ongoing" notification.
Joe Onoratocb109a02011-01-18 17:57:41 -08002727 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002728
2729 * Ongoing notifications cannot be dismissed by the user, so your application or service
2730 * must take care of canceling them.
2731 *
2732
2733 * They are typically used to indicate a background task that the user is actively engaged
2734 * with (e.g., playing music) or is pending in some way and therefore occupying the device
2735 * (e.g., a file download, sync operation, active network connection).
2736 *
2737
2738 * @see Notification#FLAG_ONGOING_EVENT
2739 * @see Service#setForeground(boolean)
Joe Onoratocb109a02011-01-18 17:57:41 -08002740 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002741 public Builder setOngoing(boolean ongoing) {
2742 setFlag(FLAG_ONGOING_EVENT, ongoing);
2743 return this;
2744 }
2745
Joe Onoratocb109a02011-01-18 17:57:41 -08002746 /**
2747 * Set this flag if you would only like the sound, vibrate
2748 * and ticker to be played if the notification is not already showing.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002749 *
2750 * @see Notification#FLAG_ONLY_ALERT_ONCE
Joe Onoratocb109a02011-01-18 17:57:41 -08002751 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002752 public Builder setOnlyAlertOnce(boolean onlyAlertOnce) {
2753 setFlag(FLAG_ONLY_ALERT_ONCE, onlyAlertOnce);
2754 return this;
2755 }
2756
Joe Onoratocb109a02011-01-18 17:57:41 -08002757 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002758 * Make this notification automatically dismissed when the user touches it. The
2759 * PendingIntent set with {@link #setDeleteIntent} will be sent when this happens.
2760 *
2761 * @see Notification#FLAG_AUTO_CANCEL
Joe Onoratocb109a02011-01-18 17:57:41 -08002762 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002763 public Builder setAutoCancel(boolean autoCancel) {
Joe Onorato281d83f2011-01-04 17:13:10 -08002764 setFlag(FLAG_AUTO_CANCEL, autoCancel);
Joe Onorato46439ce2010-11-19 13:56:21 -08002765 return this;
2766 }
2767
Joe Onoratocb109a02011-01-18 17:57:41 -08002768 /**
Griff Hazendfcb0802014-02-11 12:00:00 -08002769 * Set whether or not this notification should not bridge to other devices.
2770 *
2771 * <p>Some notifications can be bridged to other devices for remote display.
2772 * This hint can be set to recommend this notification not be bridged.
2773 */
2774 public Builder setLocalOnly(boolean localOnly) {
2775 setFlag(FLAG_LOCAL_ONLY, localOnly);
2776 return this;
2777 }
2778
2779 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002780 * Set which notification properties will be inherited from system defaults.
Joe Onoratocb109a02011-01-18 17:57:41 -08002781 * <p>
2782 * The value should be one or more of the following fields combined with
2783 * bitwise-or:
2784 * {@link #DEFAULT_SOUND}, {@link #DEFAULT_VIBRATE}, {@link #DEFAULT_LIGHTS}.
2785 * <p>
2786 * For all default values, use {@link #DEFAULT_ALL}.
2787 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002788 public Builder setDefaults(int defaults) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002789 mN.defaults = defaults;
Joe Onorato46439ce2010-11-19 13:56:21 -08002790 return this;
2791 }
2792
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002793 /**
2794 * Set the priority of this notification.
2795 *
2796 * @see Notification#priority
2797 */
Tor Norbyed9273d62013-05-30 15:59:53 -07002798 public Builder setPriority(@Priority int pri) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002799 mN.priority = pri;
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002800 return this;
2801 }
Joe Malin8d40d042012-11-05 11:36:40 -08002802
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002803 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -04002804 * Set the notification category.
Joe Malin8d40d042012-11-05 11:36:40 -08002805 *
John Spurlockfd7f1e02014-03-18 16:41:57 -04002806 * @see Notification#category
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002807 */
John Spurlockfd7f1e02014-03-18 16:41:57 -04002808 public Builder setCategory(String category) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002809 mN.category = category;
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002810 return this;
2811 }
2812
2813 /**
Chris Wrendde75302014-03-26 17:24:15 -04002814 * Add a person that is relevant to this notification.
2815 *
Chris Wrene6c48932014-09-29 17:19:27 -04002816 * <P>
2817 * Depending on user preferences, this annotation may allow the notification to pass
2818 * through interruption filters, and to appear more prominently in the user interface.
2819 * </P>
2820 *
2821 * <P>
2822 * The person should be specified by the {@code String} representation of a
2823 * {@link android.provider.ContactsContract.Contacts#CONTENT_LOOKUP_URI}.
2824 * </P>
2825 *
2826 * <P>The system will also attempt to resolve {@code mailto:} and {@code tel:} schema
2827 * URIs. The path part of these URIs must exist in the contacts database, in the
2828 * appropriate column, or the reference will be discarded as invalid. Telephone schema
2829 * URIs will be resolved by {@link android.provider.ContactsContract.PhoneLookup}.
2830 * </P>
2831 *
2832 * @param uri A URI for the person.
Chris Wrendde75302014-03-26 17:24:15 -04002833 * @see Notification#EXTRA_PEOPLE
2834 */
Chris Wrene6c48932014-09-29 17:19:27 -04002835 public Builder addPerson(String uri) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002836 mPersonList.add(uri);
Chris Wrendde75302014-03-26 17:24:15 -04002837 return this;
2838 }
2839
2840 /**
Griff Hazen5cadc3b2014-05-20 09:55:39 -07002841 * Set this notification to be part of a group of notifications sharing the same key.
2842 * Grouped notifications may display in a cluster or stack on devices which
2843 * support such rendering.
2844 *
2845 * <p>To make this notification the summary for its group, also call
2846 * {@link #setGroupSummary}. A sort order can be specified for group members by using
2847 * {@link #setSortKey}.
2848 * @param groupKey The group key of the group.
2849 * @return this object for method chaining
2850 */
2851 public Builder setGroup(String groupKey) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002852 mN.mGroupKey = groupKey;
Griff Hazen5cadc3b2014-05-20 09:55:39 -07002853 return this;
2854 }
2855
2856 /**
2857 * Set this notification to be the group summary for a group of notifications.
2858 * Grouped notifications may display in a cluster or stack on devices which
2859 * support such rendering. Requires a group key also be set using {@link #setGroup}.
2860 * @param isGroupSummary Whether this notification should be a group summary.
2861 * @return this object for method chaining
2862 */
2863 public Builder setGroupSummary(boolean isGroupSummary) {
2864 setFlag(FLAG_GROUP_SUMMARY, isGroupSummary);
2865 return this;
2866 }
2867
2868 /**
2869 * Set a sort key that orders this notification among other notifications from the
2870 * same package. This can be useful if an external sort was already applied and an app
2871 * would like to preserve this. Notifications will be sorted lexicographically using this
2872 * value, although providing different priorities in addition to providing sort key may
2873 * cause this value to be ignored.
2874 *
2875 * <p>This sort key can also be used to order members of a notification group. See
Griff Hazen9e1379f2014-05-20 12:50:51 -07002876 * {@link #setGroup}.
Griff Hazen5cadc3b2014-05-20 09:55:39 -07002877 *
2878 * @see String#compareTo(String)
2879 */
2880 public Builder setSortKey(String sortKey) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002881 mN.mSortKey = sortKey;
Griff Hazen5cadc3b2014-05-20 09:55:39 -07002882 return this;
2883 }
2884
2885 /**
Griff Hazen720042b2014-02-24 15:46:56 -08002886 * Merge additional metadata into this notification.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002887 *
Griff Hazen720042b2014-02-24 15:46:56 -08002888 * <p>Values within the Bundle will replace existing extras values in this Builder.
2889 *
2890 * @see Notification#extras
2891 */
Griff Hazen959591e2014-05-15 22:26:18 -07002892 public Builder addExtras(Bundle extras) {
2893 if (extras != null) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002894 mUserExtras.putAll(extras);
Griff Hazen720042b2014-02-24 15:46:56 -08002895 }
2896 return this;
2897 }
2898
2899 /**
2900 * Set metadata for this notification.
2901 *
2902 * <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 -04002903 * current contents are copied into the Notification each time {@link #build()} is
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002904 * called.
2905 *
Griff Hazen720042b2014-02-24 15:46:56 -08002906 * <p>Replaces any existing extras values with those from the provided Bundle.
2907 * Use {@link #addExtras} to merge in metadata instead.
2908 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002909 * @see Notification#extras
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002910 */
Griff Hazen959591e2014-05-15 22:26:18 -07002911 public Builder setExtras(Bundle extras) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002912 if (extras != null) {
2913 mUserExtras = extras;
2914 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002915 return this;
2916 }
2917
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002918 /**
Griff Hazen720042b2014-02-24 15:46:56 -08002919 * Get the current metadata Bundle used by this notification Builder.
2920 *
2921 * <p>The returned Bundle is shared with this Builder.
2922 *
2923 * <p>The current contents of this Bundle are copied into the Notification each time
2924 * {@link #build()} is called.
2925 *
2926 * @see Notification#extras
2927 */
2928 public Bundle getExtras() {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002929 return mUserExtras;
2930 }
2931
2932 private Bundle getAllExtras() {
2933 final Bundle saveExtras = (Bundle) mUserExtras.clone();
2934 saveExtras.putAll(mN.extras);
2935 return saveExtras;
Griff Hazen720042b2014-02-24 15:46:56 -08002936 }
2937
2938 /**
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002939 * Add an action to this notification. Actions are typically displayed by
2940 * the system as a button adjacent to the notification content.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04002941 * <p>
2942 * Every action must have an icon (32dp square and matching the
2943 * <a href="{@docRoot}design/style/iconography.html#action-bar">Holo
2944 * Dark action bar</a> visual style), a textual label, and a {@link PendingIntent}.
2945 * <p>
2946 * A notification in its expanded form can display up to 3 actions, from left to right in
2947 * the order they were added. Actions will not be displayed when the notification is
2948 * collapsed, however, so be sure that any essential functions may be accessed by the user
2949 * in some other way (for example, in the Activity pointed to by {@link #contentIntent}).
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002950 *
2951 * @param icon Resource ID of a drawable that represents the action.
2952 * @param title Text describing the action.
2953 * @param intent PendingIntent to be fired when the action is invoked.
Dan Sandler86647982015-05-13 23:41:13 -04002954 *
2955 * @deprecated Use {@link #addAction(Action)} instead.
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002956 */
Dan Sandler86647982015-05-13 23:41:13 -04002957 @Deprecated
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002958 public Builder addAction(int icon, CharSequence title, PendingIntent intent) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04002959 mActions.add(new Action(icon, safeCharSequence(title), intent));
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002960 return this;
2961 }
2962
Chris Wrenfbd96ba2012-05-01 12:03:58 -04002963 /**
Griff Hazen959591e2014-05-15 22:26:18 -07002964 * Add an action to this notification. Actions are typically displayed by
2965 * the system as a button adjacent to the notification content.
2966 * <p>
2967 * Every action must have an icon (32dp square and matching the
2968 * <a href="{@docRoot}design/style/iconography.html#action-bar">Holo
2969 * Dark action bar</a> visual style), a textual label, and a {@link PendingIntent}.
2970 * <p>
2971 * A notification in its expanded form can display up to 3 actions, from left to right in
2972 * the order they were added. Actions will not be displayed when the notification is
2973 * collapsed, however, so be sure that any essential functions may be accessed by the user
2974 * in some other way (for example, in the Activity pointed to by {@link #contentIntent}).
2975 *
2976 * @param action The action to add.
2977 */
2978 public Builder addAction(Action action) {
2979 mActions.add(action);
2980 return this;
2981 }
2982
2983 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002984 * Alter the complete list of actions attached to this notification.
2985 * @see #addAction(Action).
2986 *
2987 * @param actions
2988 * @return
2989 */
2990 public Builder setActions(Action... actions) {
2991 mActions.clear();
2992 for (int i = 0; i < actions.length; i++) {
2993 mActions.add(actions[i]);
2994 }
2995 return this;
2996 }
2997
2998 /**
Chris Wrenfbd96ba2012-05-01 12:03:58 -04002999 * Add a rich notification style to be applied at build time.
3000 *
3001 * @param style Object responsible for modifying the notification style.
3002 */
3003 public Builder setStyle(Style style) {
3004 if (mStyle != style) {
3005 mStyle = style;
Daniel Sandlerc08dea22012-06-28 08:35:24 -07003006 if (mStyle != null) {
3007 mStyle.setBuilder(this);
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003008 mN.extras.putString(EXTRA_TEMPLATE, style.getClass().getName());
3009 } else {
3010 mN.extras.remove(EXTRA_TEMPLATE);
Daniel Sandlerc08dea22012-06-28 08:35:24 -07003011 }
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003012 }
3013 return this;
3014 }
3015
Dan Sandler0bf2ed82013-12-21 23:33:41 -06003016 /**
3017 * Specify the value of {@link #visibility}.
Griff Hazenb720abe2014-05-20 13:15:30 -07003018 *
Dan Sandler0bf2ed82013-12-21 23:33:41 -06003019 * @param visibility One of {@link #VISIBILITY_PRIVATE} (the default),
3020 * {@link #VISIBILITY_SECRET}, or {@link #VISIBILITY_PUBLIC}.
3021 *
3022 * @return The same Builder.
3023 */
3024 public Builder setVisibility(int visibility) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003025 mN.visibility = visibility;
Dan Sandler0bf2ed82013-12-21 23:33:41 -06003026 return this;
3027 }
3028
3029 /**
3030 * Supply a replacement Notification whose contents should be shown in insecure contexts
3031 * (i.e. atop the secure lockscreen). See {@link #visibility} and {@link #VISIBILITY_PUBLIC}.
3032 * @param n A replacement notification, presumably with some or all info redacted.
3033 * @return The same Builder.
3034 */
3035 public Builder setPublicVersion(Notification n) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003036 if (n != null) {
3037 mN.publicVersion = new Notification();
3038 n.cloneInto(mN.publicVersion, /*heavy=*/ true);
3039 } else {
3040 mN.publicVersion = null;
3041 }
Dan Sandler0bf2ed82013-12-21 23:33:41 -06003042 return this;
3043 }
3044
Griff Hazenb720abe2014-05-20 13:15:30 -07003045 /**
Griff Hazen5cadc3b2014-05-20 09:55:39 -07003046 * Apply an extender to this notification builder. Extenders may be used to add
3047 * metadata or change options on this builder.
3048 */
Griff Hazen61a9e862014-05-22 16:05:19 -07003049 public Builder extend(Extender extender) {
3050 extender.extend(this);
Griff Hazen5cadc3b2014-05-20 09:55:39 -07003051 return this;
3052 }
3053
Dan Sandler4e787062015-06-17 15:09:48 -04003054 /**
3055 * @hide
3056 */
Julia Reynoldse46bb372016-03-17 11:05:58 -04003057 public Builder setFlag(int mask, boolean value) {
Joe Onorato46439ce2010-11-19 13:56:21 -08003058 if (value) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003059 mN.flags |= mask;
Joe Onorato46439ce2010-11-19 13:56:21 -08003060 } else {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003061 mN.flags &= ~mask;
Joe Onorato46439ce2010-11-19 13:56:21 -08003062 }
Julia Reynoldse46bb372016-03-17 11:05:58 -04003063 return this;
Joe Onorato46439ce2010-11-19 13:56:21 -08003064 }
3065
Dan Sandler26e81cf2014-05-06 10:01:27 -04003066 /**
3067 * Sets {@link Notification#color}.
3068 *
3069 * @param argb The accent color to use
3070 *
3071 * @return The same Builder.
3072 */
Tor Norbye80756e32015-03-02 09:39:27 -08003073 public Builder setColor(@ColorInt int argb) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003074 mN.color = argb;
Julia Reynolds10ee1fc2015-11-09 11:04:55 -05003075 sanitizeColor();
Dan Sandler26e81cf2014-05-06 10:01:27 -04003076 return this;
3077 }
3078
Jorim Jaggid05aa3e2014-08-28 17:52:27 +02003079 private Drawable getProfileBadgeDrawable() {
Christoph Studer7ac80e62014-08-04 16:01:57 +02003080 // Note: This assumes that the current user can read the profile badge of the
3081 // originating user.
Selim Cineke6ff9462016-01-15 15:07:06 -08003082 return mContext.getPackageManager().getUserBadgeForDensityNoBackground(
Julia Reynoldsda303542015-11-23 14:00:20 -05003083 new UserHandle(mContext.getUserId()), 0);
Jorim Jaggid05aa3e2014-08-28 17:52:27 +02003084 }
3085
3086 private Bitmap getProfileBadge() {
3087 Drawable badge = getProfileBadgeDrawable();
Kenny Guy8a0101b2014-05-08 23:34:12 +01003088 if (badge == null) {
3089 return null;
3090 }
Jorim Jaggid05aa3e2014-08-28 17:52:27 +02003091 final int size = mContext.getResources().getDimensionPixelSize(
3092 R.dimen.notification_badge_size);
3093 Bitmap bitmap = Bitmap.createBitmap(size, size, Bitmap.Config.ARGB_8888);
Kenny Guy8a0101b2014-05-08 23:34:12 +01003094 Canvas canvas = new Canvas(bitmap);
Jorim Jaggid05aa3e2014-08-28 17:52:27 +02003095 badge.setBounds(0, 0, size, size);
Kenny Guy8a0101b2014-05-08 23:34:12 +01003096 badge.draw(canvas);
3097 return bitmap;
3098 }
3099
Selim Cinekc848c3a2016-01-13 15:27:30 -08003100 private void bindProfileBadge(RemoteViews contentView) {
Kenny Guy98193ea2014-07-24 19:54:37 +01003101 Bitmap profileBadge = getProfileBadge();
3102
Kenny Guy98193ea2014-07-24 19:54:37 +01003103 if (profileBadge != null) {
Selim Cinekc848c3a2016-01-13 15:27:30 -08003104 contentView.setImageViewBitmap(R.id.profile_badge, profileBadge);
3105 contentView.setViewVisibility(R.id.profile_badge, View.VISIBLE);
Kenny Guy98193ea2014-07-24 19:54:37 +01003106 }
Kenny Guy98193ea2014-07-24 19:54:37 +01003107 }
3108
Christoph Studerfe718432014-09-01 18:21:18 +02003109 private void resetStandardTemplate(RemoteViews contentView) {
Selim Cinekeaa29ca2015-11-23 13:51:13 -08003110 resetNotificationHeader(contentView);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003111 resetContentMargins(contentView);
Christoph Studerfe718432014-09-01 18:21:18 +02003112 contentView.setViewVisibility(R.id.right_icon, View.GONE);
Selim Cinek860b6da2015-12-16 19:02:19 -08003113 contentView.setViewVisibility(R.id.title, View.GONE);
Christoph Studerfe718432014-09-01 18:21:18 +02003114 contentView.setTextViewText(R.id.title, null);
Selim Cinek41598732016-01-11 16:58:37 -08003115 contentView.setViewVisibility(R.id.text, View.GONE);
Christoph Studerfe718432014-09-01 18:21:18 +02003116 contentView.setTextViewText(R.id.text, null);
Selim Cinek29603462015-11-17 19:04:39 -08003117 contentView.setViewVisibility(R.id.text_line_1, View.GONE);
Selim Cinek41598732016-01-11 16:58:37 -08003118 contentView.setTextViewText(R.id.text_line_1, null);
Christoph Studerfe718432014-09-01 18:21:18 +02003119 contentView.setViewVisibility(R.id.progress, View.GONE);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003120 }
3121
Selim Cinekeaa29ca2015-11-23 13:51:13 -08003122 /**
3123 * Resets the notification header to its original state
3124 */
3125 private void resetNotificationHeader(RemoteViews contentView) {
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003126 contentView.setImageViewResource(R.id.icon, 0);
Selim Cinek7b836392015-12-04 20:02:59 -08003127 contentView.setBoolean(R.id.notification_header, "setExpanded", false);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003128 contentView.setTextViewText(R.id.app_name_text, null);
Christoph Studerca1db712014-09-10 17:31:33 +02003129 contentView.setViewVisibility(R.id.chronometer, View.GONE);
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07003130 contentView.setViewVisibility(R.id.header_text, View.GONE);
3131 contentView.setViewVisibility(R.id.header_text_divider, View.GONE);
Selim Cinek29603462015-11-17 19:04:39 -08003132 contentView.setViewVisibility(R.id.time_divider, View.GONE);
Selim Cinekc848c3a2016-01-13 15:27:30 -08003133 contentView.setImageViewIcon(R.id.profile_badge, null);
3134 contentView.setViewVisibility(R.id.profile_badge, View.GONE);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003135 }
3136
3137 private void resetContentMargins(RemoteViews contentView) {
3138 contentView.setViewLayoutMarginEnd(R.id.line1, 0);
Selim Cinekc848c3a2016-01-13 15:27:30 -08003139 contentView.setViewLayoutMarginEnd(R.id.text, 0);
Christoph Studerfe718432014-09-01 18:21:18 +02003140 }
3141
Jorim Jaggi445d3c02014-08-19 22:33:42 +02003142 private RemoteViews applyStandardTemplate(int resId) {
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02003143 return applyStandardTemplate(resId, true /* hasProgress */);
3144 }
3145
3146 /**
3147 * @param hasProgress whether the progress bar should be shown and set
3148 */
3149 private RemoteViews applyStandardTemplate(int resId, boolean hasProgress) {
Adrian Roosc1a80b02016-04-05 14:54:55 -07003150 final Bundle ex = mN.extras;
3151
3152 CharSequence title = processLegacyText(ex.getCharSequence(EXTRA_TITLE));
3153 CharSequence text = processLegacyText(ex.getCharSequence(EXTRA_TEXT));
3154 return applyStandardTemplate(resId, hasProgress, title, text);
3155 }
3156
3157 /**
3158 * @param hasProgress whether the progress bar should be shown and set
3159 */
3160 private RemoteViews applyStandardTemplate(int resId, boolean hasProgress,
3161 CharSequence title, CharSequence text) {
Kenny Guy77320062014-08-27 21:37:15 +01003162 RemoteViews contentView = new BuilderRemoteViews(mContext.getApplicationInfo(), resId);
Dan Sandler539aad42014-08-04 00:43:39 -04003163
Christoph Studerfe718432014-09-01 18:21:18 +02003164 resetStandardTemplate(contentView);
3165
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003166 final Bundle ex = mN.extras;
Dan Sandler190d58d2014-05-15 09:33:39 -04003167
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003168 bindNotificationHeader(contentView);
3169 bindLargeIcon(contentView);
Adrian Roosc1a80b02016-04-05 14:54:55 -07003170 if (title != null) {
Selim Cinek860b6da2015-12-16 19:02:19 -08003171 contentView.setViewVisibility(R.id.title, View.VISIBLE);
Adrian Roosc1a80b02016-04-05 14:54:55 -07003172 contentView.setTextViewText(R.id.title, title);
Joe Onorato561d3852010-11-20 18:09:34 -08003173 }
Selim Cinek29603462015-11-17 19:04:39 -08003174 boolean showProgress = handleProgressBar(hasProgress, contentView, ex);
Adrian Roosc1a80b02016-04-05 14:54:55 -07003175 if (text != null) {
Selim Cinek41598732016-01-11 16:58:37 -08003176 int textId = showProgress ? com.android.internal.R.id.text_line_1
3177 : com.android.internal.R.id.text;
Adrian Roosc1a80b02016-04-05 14:54:55 -07003178 contentView.setTextViewText(textId, text);
Selim Cinek41598732016-01-11 16:58:37 -08003179 contentView.setViewVisibility(textId, View.VISIBLE);
Joe Onorato561d3852010-11-20 18:09:34 -08003180 }
Selim Cinekc848c3a2016-01-13 15:27:30 -08003181
Selim Cinek860b6da2015-12-16 19:02:19 -08003182 setContentMinHeight(contentView, showProgress || mN.mLargeIcon != null);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003183
Selim Cinek29603462015-11-17 19:04:39 -08003184 return contentView;
3185 }
3186
Selim Cinek860b6da2015-12-16 19:02:19 -08003187 /**
3188 * @param remoteView the remote view to update the minheight in
3189 * @param hasMinHeight does it have a mimHeight
3190 * @hide
3191 */
3192 void setContentMinHeight(RemoteViews remoteView, boolean hasMinHeight) {
3193 int minHeight = 0;
3194 if (hasMinHeight) {
3195 // we need to set the minHeight of the notification
3196 minHeight = mContext.getResources().getDimensionPixelSize(
3197 com.android.internal.R.dimen.notification_min_content_height);
3198 }
3199 remoteView.setInt(R.id.notification_main_column, "setMinimumHeight", minHeight);
3200 }
3201
Selim Cinek29603462015-11-17 19:04:39 -08003202 private boolean handleProgressBar(boolean hasProgress, RemoteViews contentView, Bundle ex) {
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003203 final int max = ex.getInt(EXTRA_PROGRESS_MAX, 0);
3204 final int progress = ex.getInt(EXTRA_PROGRESS, 0);
3205 final boolean ind = ex.getBoolean(EXTRA_PROGRESS_INDETERMINATE);
3206 if (hasProgress && (max != 0 || ind)) {
Selim Cinek29603462015-11-17 19:04:39 -08003207 contentView.setViewVisibility(com.android.internal.R.id.progress, View.VISIBLE);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003208 contentView.setProgressBar(
Selim Cinek29603462015-11-17 19:04:39 -08003209 R.id.progress, max, progress, ind);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003210 contentView.setProgressBackgroundTintList(
3211 R.id.progress, ColorStateList.valueOf(mContext.getColor(
3212 R.color.notification_progress_background_color)));
Selim Cinek29603462015-11-17 19:04:39 -08003213 if (mN.color != COLOR_DEFAULT) {
Adrian Roos4ff3b122016-02-01 12:26:13 -08003214 ColorStateList colorStateList = ColorStateList.valueOf(resolveContrastColor());
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003215 contentView.setProgressTintList(R.id.progress, colorStateList);
3216 contentView.setProgressIndeterminateTintList(R.id.progress, colorStateList);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003217 }
Selim Cinek29603462015-11-17 19:04:39 -08003218 return true;
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003219 } else {
3220 contentView.setViewVisibility(R.id.progress, View.GONE);
Selim Cinek29603462015-11-17 19:04:39 -08003221 return false;
Jeff Sharkey1c400132011-08-05 14:50:13 -07003222 }
Joe Onorato561d3852010-11-20 18:09:34 -08003223 }
3224
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003225 private void bindLargeIcon(RemoteViews contentView) {
3226 if (mN.mLargeIcon != null) {
3227 contentView.setViewVisibility(R.id.right_icon, View.VISIBLE);
3228 contentView.setImageViewIcon(R.id.right_icon, mN.mLargeIcon);
3229 processLargeLegacyIcon(mN.mLargeIcon, contentView);
3230 int endMargin = mContext.getResources().getDimensionPixelSize(
3231 R.dimen.notification_content_picture_margin);
3232 contentView.setViewLayoutMarginEnd(R.id.line1, endMargin);
Selim Cinekc848c3a2016-01-13 15:27:30 -08003233 contentView.setViewLayoutMarginEnd(R.id.text, endMargin);
Selim Cinek29603462015-11-17 19:04:39 -08003234 contentView.setViewLayoutMarginEnd(R.id.progress, endMargin);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003235 }
3236 }
3237
3238 private void bindNotificationHeader(RemoteViews contentView) {
3239 bindSmallIcon(contentView);
3240 bindHeaderAppName(contentView);
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07003241 bindHeaderText(contentView);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003242 bindHeaderChronometerAndTime(contentView);
3243 bindExpandButton(contentView);
Selim Cinekc848c3a2016-01-13 15:27:30 -08003244 bindProfileBadge(contentView);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003245 }
3246
3247 private void bindExpandButton(RemoteViews contentView) {
Adrian Roos4ff3b122016-02-01 12:26:13 -08003248 contentView.setDrawableParameters(R.id.expand_button, false, -1, resolveContrastColor(),
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003249 PorterDuff.Mode.SRC_ATOP, -1);
Selim Cinekea4bef72015-12-02 15:51:10 -08003250 contentView.setInt(R.id.notification_header, "setOriginalNotificationColor",
Adrian Roos4ff3b122016-02-01 12:26:13 -08003251 resolveContrastColor());
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003252 }
3253
3254 private void bindHeaderChronometerAndTime(RemoteViews contentView) {
3255 if (showsTimeOrChronometer()) {
Selim Cinek29603462015-11-17 19:04:39 -08003256 contentView.setViewVisibility(R.id.time_divider, View.VISIBLE);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003257 if (mN.extras.getBoolean(EXTRA_SHOW_CHRONOMETER)) {
3258 contentView.setViewVisibility(R.id.chronometer, View.VISIBLE);
3259 contentView.setLong(R.id.chronometer, "setBase",
3260 mN.when + (SystemClock.elapsedRealtime() - System.currentTimeMillis()));
3261 contentView.setBoolean(R.id.chronometer, "setStarted", true);
Selim Cinek81c23aa2016-02-25 16:23:13 -08003262 boolean countsDown = mN.extras.getBoolean(EXTRA_CHRONOMETER_COUNTS_DOWN);
3263 contentView.setChronometerCountsDown(R.id.chronometer, countsDown);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003264 } else {
3265 contentView.setViewVisibility(R.id.time, View.VISIBLE);
3266 contentView.setLong(R.id.time, "setTime", mN.when);
3267 }
3268 }
3269 }
3270
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07003271 private void bindHeaderText(RemoteViews contentView) {
3272 CharSequence headerText = mN.extras.getCharSequence(EXTRA_SUB_TEXT);
3273 if (headerText == null && mStyle != null && mStyle.mSummaryTextSet
Selim Cinek03d0d652015-11-13 13:18:09 -05003274 && mStyle.hasSummaryInHeader()) {
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07003275 headerText = mStyle.mSummaryText;
Selim Cinek03d0d652015-11-13 13:18:09 -05003276 }
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07003277 if (headerText == null
3278 && mContext.getApplicationInfo().targetSdkVersion < Build.VERSION_CODES.N
3279 && mN.extras.getCharSequence(EXTRA_INFO_TEXT) != null) {
3280 headerText = mN.extras.getCharSequence(EXTRA_INFO_TEXT);
3281 }
3282 if (headerText != null) {
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003283 // TODO: Remove the span entirely to only have the string with propper formating.
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07003284 contentView.setTextViewText(R.id.header_text, processLegacyText(headerText));
3285 contentView.setViewVisibility(R.id.header_text, View.VISIBLE);
3286 contentView.setViewVisibility(R.id.header_text_divider, View.VISIBLE);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003287 }
3288 }
3289
Dan Sandler732bd6c2016-04-12 14:20:32 -04003290 private String loadHeaderAppName() {
3291 CharSequence name = null;
3292 final PackageManager pm = mContext.getPackageManager();
3293 if (mN.extras.containsKey(EXTRA_SUBSTITUTE_APP_NAME)) {
3294 // only system packages which lump together a bunch of unrelated stuff
3295 // may substitute a different name to make the purpose of the
3296 // notification more clear. the correct package label should always
3297 // be accessible via SystemUI.
3298 final String pkg = mContext.getPackageName();
3299 final String subName = mN.extras.getString(EXTRA_SUBSTITUTE_APP_NAME);
3300 if (PackageManager.PERMISSION_GRANTED == pm.checkPermission(
3301 android.Manifest.permission.SUBSTITUTE_NOTIFICATION_APP_NAME, pkg)) {
3302 name = subName;
3303 } else {
3304 Log.w(TAG, "warning: pkg "
3305 + pkg + " attempting to substitute app name '" + subName
3306 + "' without holding perm "
3307 + android.Manifest.permission.SUBSTITUTE_NOTIFICATION_APP_NAME);
3308 }
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003309 }
Dan Sandler732bd6c2016-04-12 14:20:32 -04003310 if (TextUtils.isEmpty(name)) {
3311 name = pm.getApplicationLabel(mContext.getApplicationInfo());
3312 }
3313 if (TextUtils.isEmpty(name)) {
3314 // still nothing?
3315 return null;
3316 }
3317
3318 return String.valueOf(name);
3319 }
3320 private void bindHeaderAppName(RemoteViews contentView) {
3321 contentView.setTextViewText(R.id.app_name_text, loadHeaderAppName());
Adrian Roos4ff3b122016-02-01 12:26:13 -08003322 contentView.setTextColor(R.id.app_name_text, resolveContrastColor());
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003323 }
3324
3325 private void bindSmallIcon(RemoteViews contentView) {
3326 contentView.setImageViewIcon(R.id.icon, mN.mSmallIcon);
3327 processSmallIconColor(mN.mSmallIcon, contentView);
3328 }
3329
Jorim Jaggi445d3c02014-08-19 22:33:42 +02003330 /**
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02003331 * @return true if the built notification will show the time or the chronometer; false
3332 * otherwise
3333 */
3334 private boolean showsTimeOrChronometer() {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003335 return mN.when != 0 && mN.extras.getBoolean(EXTRA_SHOW_WHEN);
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02003336 }
3337
Christoph Studerfe718432014-09-01 18:21:18 +02003338 private void resetStandardTemplateWithActions(RemoteViews big) {
Adrian Roos7052de52016-03-03 15:53:34 -08003339 big.setViewVisibility(R.id.actions_container, View.GONE);
Christoph Studerfe718432014-09-01 18:21:18 +02003340 big.setViewVisibility(R.id.actions, View.GONE);
Christoph Studerfe718432014-09-01 18:21:18 +02003341 big.removeAllViews(R.id.actions);
Adrian Roose458aa82015-12-08 16:17:19 -08003342
3343 big.setViewVisibility(R.id.notification_material_reply_container, View.GONE);
3344 big.setTextViewText(R.id.notification_material_reply_text_1, null);
3345
3346 big.setViewVisibility(R.id.notification_material_reply_text_2, View.GONE);
3347 big.setTextViewText(R.id.notification_material_reply_text_2, null);
3348 big.setViewVisibility(R.id.notification_material_reply_text_3, View.GONE);
3349 big.setTextViewText(R.id.notification_material_reply_text_3, null);
Christoph Studerfe718432014-09-01 18:21:18 +02003350 }
3351
Daniel Sandler96fd7c12012-03-30 16:37:36 -04003352 private RemoteViews applyStandardTemplateWithActions(int layoutId) {
Adrian Roos48d746a2016-04-12 14:57:28 -07003353 final Bundle ex = mN.extras;
3354
3355 CharSequence title = processLegacyText(ex.getCharSequence(EXTRA_TITLE));
3356 CharSequence text = processLegacyText(ex.getCharSequence(EXTRA_TEXT));
3357 return applyStandardTemplateWithActions(layoutId, true /* hasProgress */, title, text);
3358 }
3359
3360 private RemoteViews applyStandardTemplateWithActions(int layoutId, boolean hasProgress,
3361 CharSequence title, CharSequence text) {
3362 RemoteViews big = applyStandardTemplate(layoutId, hasProgress, title, text);
Daniel Sandler96fd7c12012-03-30 16:37:36 -04003363
Christoph Studerfe718432014-09-01 18:21:18 +02003364 resetStandardTemplateWithActions(big);
3365
Adrian Roose458aa82015-12-08 16:17:19 -08003366 boolean validRemoteInput = false;
3367
Daniel Sandler96fd7c12012-03-30 16:37:36 -04003368 int N = mActions.size();
3369 if (N > 0) {
Adrian Roos7052de52016-03-03 15:53:34 -08003370 big.setViewVisibility(R.id.actions_container, View.VISIBLE);
Daniel Sandler96fd7c12012-03-30 16:37:36 -04003371 big.setViewVisibility(R.id.actions, View.VISIBLE);
Daniel Sandler8680bf82012-05-15 16:52:52 -04003372 if (N>MAX_ACTION_BUTTONS) N=MAX_ACTION_BUTTONS;
Daniel Sandler96fd7c12012-03-30 16:37:36 -04003373 for (int i=0; i<N; i++) {
Adrian Roose458aa82015-12-08 16:17:19 -08003374 Action action = mActions.get(i);
3375 validRemoteInput |= hasValidRemoteInput(action);
3376
3377 final RemoteViews button = generateActionButton(action);
Adrian Roos9b123cf2016-02-04 14:55:57 -08003378 if (i == N - 1) {
3379 button.setViewLayoutWidth(com.android.internal.R.id.action0,
3380 ViewGroup.LayoutParams.MATCH_PARENT);
3381 }
Daniel Sandler96fd7c12012-03-30 16:37:36 -04003382 big.addView(R.id.actions, button);
3383 }
3384 }
Adrian Roose458aa82015-12-08 16:17:19 -08003385
3386 CharSequence[] replyText = mN.extras.getCharSequenceArray(EXTRA_REMOTE_INPUT_HISTORY);
3387 if (validRemoteInput && replyText != null
3388 && replyText.length > 0 && !TextUtils.isEmpty(replyText[0])) {
3389 big.setViewVisibility(R.id.notification_material_reply_container, View.VISIBLE);
3390 big.setTextViewText(R.id.notification_material_reply_text_1, replyText[0]);
3391
3392 if (replyText.length > 1 && !TextUtils.isEmpty(replyText[1])) {
3393 big.setViewVisibility(R.id.notification_material_reply_text_2, View.VISIBLE);
3394 big.setTextViewText(R.id.notification_material_reply_text_2, replyText[1]);
3395
3396 if (replyText.length > 2 && !TextUtils.isEmpty(replyText[2])) {
3397 big.setViewVisibility(
3398 R.id.notification_material_reply_text_3, View.VISIBLE);
3399 big.setTextViewText(R.id.notification_material_reply_text_3, replyText[2]);
3400 }
3401 }
3402 }
3403
Daniel Sandler96fd7c12012-03-30 16:37:36 -04003404 return big;
3405 }
3406
Adrian Roose458aa82015-12-08 16:17:19 -08003407 private boolean hasValidRemoteInput(Action action) {
3408 if (TextUtils.isEmpty(action.title) || action.actionIntent == null) {
3409 // Weird actions
3410 return false;
3411 }
3412
3413 RemoteInput[] remoteInputs = action.getRemoteInputs();
3414 if (remoteInputs == null) {
3415 return false;
3416 }
3417
3418 for (RemoteInput r : remoteInputs) {
3419 CharSequence[] choices = r.getChoices();
3420 if (r.getAllowFreeFormInput() || (choices != null && choices.length != 0)) {
3421 return true;
3422 }
3423 }
3424 return false;
3425 }
3426
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003427 /**
3428 * Construct a RemoteViews for the final 1U notification layout. In order:
3429 * 1. Custom contentView from the caller
3430 * 2. Style's proposed content view
3431 * 3. Standard template view
3432 */
Julia Reynolds3b848122016-02-26 10:45:32 -05003433 public RemoteViews createContentView() {
Selim Cinek593610c2016-02-16 18:42:57 -08003434 if (mN.contentView != null && (mStyle == null || !mStyle.displayCustomViewInline())) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003435 return mN.contentView;
3436 } else if (mStyle != null) {
3437 final RemoteViews styleView = mStyle.makeContentView();
3438 if (styleView != null) {
3439 return styleView;
3440 }
Joe Onorato46439ce2010-11-19 13:56:21 -08003441 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003442 return applyStandardTemplate(getBaseLayoutResource());
Joe Onorato46439ce2010-11-19 13:56:21 -08003443 }
3444
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003445 /**
3446 * Construct a RemoteViews for the final big notification layout.
3447 */
Julia Reynolds3b848122016-02-26 10:45:32 -05003448 public RemoteViews createBigContentView() {
Selim Cinek850a8542015-11-11 11:48:36 -05003449 RemoteViews result = null;
Selim Cinek593610c2016-02-16 18:42:57 -08003450 if (mN.bigContentView != null
3451 && (mStyle == null || !mStyle.displayCustomViewInline())) {
Julia Reynolds089e3e42015-11-18 09:59:57 -05003452 return mN.bigContentView;
3453 } else if (mStyle != null) {
Selim Cinek850a8542015-11-11 11:48:36 -05003454 result = mStyle.makeBigContentView();
Selim Cinek90dcf6d2015-11-18 20:24:13 -08003455 hideLine1Text(result);
Selim Cinekcc10bfb2016-02-10 16:24:21 -08003456 } else if (mActions.size() != 0) {
3457 result = applyStandardTemplateWithActions(getBigBaseLayoutResource());
Selim Cinek850a8542015-11-11 11:48:36 -05003458 }
3459 adaptNotificationHeaderForBigContentView(result);
3460 return result;
3461 }
Daniel Sandler96fd7c12012-03-30 16:37:36 -04003462
Selim Cinekeaa29ca2015-11-23 13:51:13 -08003463 /**
3464 * Construct a RemoteViews for the final notification header only
3465 *
3466 * @hide
3467 */
3468 public RemoteViews makeNotificationHeader() {
3469 RemoteViews header = new BuilderRemoteViews(mContext.getApplicationInfo(),
3470 R.layout.notification_template_header);
3471 resetNotificationHeader(header);
3472 bindNotificationHeader(header);
3473 return header;
3474 }
3475
Selim Cinek29603462015-11-17 19:04:39 -08003476 private void hideLine1Text(RemoteViews result) {
Selim Cinekcc10bfb2016-02-10 16:24:21 -08003477 if (result != null) {
3478 result.setViewVisibility(R.id.text_line_1, View.GONE);
3479 }
Selim Cinek29603462015-11-17 19:04:39 -08003480 }
3481
Selim Cinek850a8542015-11-11 11:48:36 -05003482 private void adaptNotificationHeaderForBigContentView(RemoteViews result) {
Selim Cinekcc10bfb2016-02-10 16:24:21 -08003483 if (result != null) {
3484 result.setBoolean(R.id.notification_header, "setExpanded", true);
3485 }
Daniel Sandler96fd7c12012-03-30 16:37:36 -04003486 }
3487
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003488 /**
3489 * Construct a RemoteViews for the final heads-up notification layout.
3490 */
Julia Reynolds3b848122016-02-26 10:45:32 -05003491 public RemoteViews createHeadsUpContentView() {
Selim Cinek593610c2016-02-16 18:42:57 -08003492 if (mN.headsUpContentView != null
3493 && (mStyle == null || !mStyle.displayCustomViewInline())) {
Julia Reynolds089e3e42015-11-18 09:59:57 -05003494 return mN.headsUpContentView;
3495 } else if (mStyle != null) {
3496 final RemoteViews styleView = mStyle.makeHeadsUpContentView();
3497 if (styleView != null) {
3498 return styleView;
3499 }
3500 } else if (mActions.size() == 0) {
3501 return null;
3502 }
3503
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01003504 return applyStandardTemplateWithActions(getBigBaseLayoutResource());
Chris Wren8fd39ec2014-02-27 17:43:26 -05003505 }
3506
Selim Cinek624c02db2015-12-14 21:00:02 -08003507 /**
3508 * Construct a RemoteViews for the display in public contexts like on the lockscreen.
3509 *
3510 * @hide
3511 */
3512 public RemoteViews makePublicContentView() {
3513 if (mN.publicVersion != null) {
3514 final Builder builder = recoverBuilder(mContext, mN.publicVersion);
Julia Reynolds3b848122016-02-26 10:45:32 -05003515 return builder.createContentView();
Selim Cinek624c02db2015-12-14 21:00:02 -08003516 }
3517 Bundle savedBundle = mN.extras;
3518 Style style = mStyle;
3519 mStyle = null;
3520 Icon largeIcon = mN.mLargeIcon;
3521 mN.mLargeIcon = null;
3522 Bundle publicExtras = new Bundle();
3523 publicExtras.putBoolean(EXTRA_SHOW_WHEN,
3524 savedBundle.getBoolean(EXTRA_SHOW_WHEN));
3525 publicExtras.putBoolean(EXTRA_SHOW_CHRONOMETER,
3526 savedBundle.getBoolean(EXTRA_SHOW_CHRONOMETER));
Selim Cinek81c23aa2016-02-25 16:23:13 -08003527 publicExtras.putBoolean(EXTRA_CHRONOMETER_COUNTS_DOWN,
3528 savedBundle.getBoolean(EXTRA_CHRONOMETER_COUNTS_DOWN));
Selim Cinek624c02db2015-12-14 21:00:02 -08003529 publicExtras.putCharSequence(EXTRA_TITLE,
3530 mContext.getString(R.string.notification_hidden_text));
3531 mN.extras = publicExtras;
3532 final RemoteViews publicView = applyStandardTemplate(getBaseLayoutResource());
3533 mN.extras = savedBundle;
3534 mN.mLargeIcon = largeIcon;
3535 mStyle = style;
3536 return publicView;
3537 }
3538
3539
Chris Wren8fd39ec2014-02-27 17:43:26 -05003540
Daniel Sandler96fd7c12012-03-30 16:37:36 -04003541 private RemoteViews generateActionButton(Action action) {
Daniel Sandler8680bf82012-05-15 16:52:52 -04003542 final boolean tombstone = (action.actionIntent == null);
Selim Cinekf33b1112015-07-15 17:45:11 -07003543 RemoteViews button = new BuilderRemoteViews(mContext.getApplicationInfo(),
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01003544 tombstone ? getActionTombstoneLayoutResource()
3545 : getActionLayoutResource());
Dan Sandler68079d52015-07-22 10:45:30 -04003546 final Icon ai = action.getIcon();
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01003547 button.setTextViewText(R.id.action0, processLegacyText(action.title));
Daniel Sandler8680bf82012-05-15 16:52:52 -04003548 if (!tombstone) {
Daniel Sandlere5518842012-05-10 16:20:40 -04003549 button.setOnClickPendingIntent(R.id.action0, action.actionIntent);
Daniel Sandlere5518842012-05-10 16:20:40 -04003550 }
Daniel Sandler96fd7c12012-03-30 16:37:36 -04003551 button.setContentDescription(R.id.action0, action.title);
Adrian Roosfe84e1f2015-11-04 15:55:39 -08003552 if (action.mRemoteInputs != null) {
3553 button.setRemoteInputs(R.id.action0, action.mRemoteInputs);
3554 }
3555 if (mN.color != COLOR_DEFAULT) {
Adrian Roos4ff3b122016-02-01 12:26:13 -08003556 button.setTextColor(R.id.action0, resolveContrastColor());
Adrian Roosfe84e1f2015-11-04 15:55:39 -08003557 }
Daniel Sandler96fd7c12012-03-30 16:37:36 -04003558 return button;
3559 }
3560
Joe Onoratocb109a02011-01-18 17:57:41 -08003561 /**
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01003562 * @return Whether we are currently building a notification from a legacy (an app that
Alan Viverette3cb07a462014-06-06 14:19:53 -07003563 * doesn't create material notifications by itself) app.
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01003564 */
3565 private boolean isLegacy() {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003566 return getColorUtil() != null;
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01003567 }
3568
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01003569 private CharSequence processLegacyText(CharSequence charSequence) {
3570 if (isLegacy()) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003571 return getColorUtil().invertCharSequenceColors(charSequence);
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01003572 } else {
3573 return charSequence;
3574 }
3575 }
3576
Dan Sandler26e81cf2014-05-06 10:01:27 -04003577 /**
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003578 * Apply any necessariy colors to the small icon
Dan Sandler26e81cf2014-05-06 10:01:27 -04003579 */
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003580 private void processSmallIconColor(Icon smallIcon, RemoteViews contentView) {
Selim Cinekea4bef72015-12-02 15:51:10 -08003581 boolean colorable = !isLegacy() || getColorUtil().isGrayscaleIcon(mContext, smallIcon);
3582 if (colorable) {
Adrian Roos4ff3b122016-02-01 12:26:13 -08003583 contentView.setDrawableParameters(R.id.icon, false, -1, resolveContrastColor(),
Jorim Jaggi92df1f22014-12-16 19:44:41 +01003584 PorterDuff.Mode.SRC_ATOP, -1);
Selim Cinekea4bef72015-12-02 15:51:10 -08003585
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01003586 }
Selim Cinekea4bef72015-12-02 15:51:10 -08003587 contentView.setInt(R.id.notification_header, "setOriginalIconColor",
Adrian Roos4ff3b122016-02-01 12:26:13 -08003588 colorable ? resolveContrastColor() : NotificationHeaderView.NO_COLOR);
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01003589 }
3590
Dan Sandler26e81cf2014-05-06 10:01:27 -04003591 /**
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003592 * Make the largeIcon dark if it's a fake smallIcon (that is,
Dan Sandler26e81cf2014-05-06 10:01:27 -04003593 * if it's grayscale).
3594 */
3595 // TODO: also check bounds, transparency, that sort of thing.
Dan Sandlerd63f9322015-05-06 15:18:49 -04003596 private void processLargeLegacyIcon(Icon largeIcon, RemoteViews contentView) {
3597 if (largeIcon != null && isLegacy()
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003598 && getColorUtil().isGrayscaleIcon(mContext, largeIcon)) {
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003599 // resolve color will fall back to the default when legacy
Adrian Roos4ff3b122016-02-01 12:26:13 -08003600 contentView.setDrawableParameters(R.id.icon, false, -1, resolveContrastColor(),
Dan Sandler190d58d2014-05-15 09:33:39 -04003601 PorterDuff.Mode.SRC_ATOP, -1);
Jorim Jaggi92df1f22014-12-16 19:44:41 +01003602 }
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01003603 }
3604
Julia Reynolds10ee1fc2015-11-09 11:04:55 -05003605 private void sanitizeColor() {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003606 if (mN.color != COLOR_DEFAULT) {
3607 mN.color |= 0xFF000000; // no alpha for custom colors
Jorim Jaggi74419312014-06-10 20:57:21 +02003608 }
Jorim Jaggi74419312014-06-10 20:57:21 +02003609 }
3610
Adrian Roos4ff3b122016-02-01 12:26:13 -08003611 int resolveContrastColor() {
3612 if (mCachedContrastColorIsFor == mN.color && mCachedContrastColor != COLOR_INVALID) {
3613 return mCachedContrastColor;
Dan Sandler26e81cf2014-05-06 10:01:27 -04003614 }
Adrian Roos4ff3b122016-02-01 12:26:13 -08003615 final int contrasted = NotificationColorUtil.resolveContrastColor(mContext, mN.color);
3616
3617 mCachedContrastColorIsFor = mN.color;
3618 return mCachedContrastColor = contrasted;
Dan Sandler26e81cf2014-05-06 10:01:27 -04003619 }
3620
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01003621 /**
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003622 * Apply the unstyled operations and return a new {@link Notification} object.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04003623 * @hide
Joe Onoratocb109a02011-01-18 17:57:41 -08003624 */
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04003625 public Notification buildUnstyled() {
Daniel Sandlera0a938c2012-03-15 08:42:37 -04003626 if (mActions.size() > 0) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003627 mN.actions = new Action[mActions.size()];
3628 mActions.toArray(mN.actions);
Daniel Sandlera0a938c2012-03-15 08:42:37 -04003629 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003630 if (!mPersonList.isEmpty()) {
3631 mN.extras.putStringArray(EXTRA_PEOPLE,
3632 mPersonList.toArray(new String[mPersonList.size()]));
Dan Sandler0bf2ed82013-12-21 23:33:41 -06003633 }
Selim Cinek247fa012016-02-18 09:50:48 -08003634 if (mN.bigContentView != null || mN.contentView != null
3635 || mN.headsUpContentView != null) {
3636 mN.extras.putBoolean(EXTRA_CONTAINS_CUSTOM_VIEW, true);
3637 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003638 return mN;
Joe Onorato46439ce2010-11-19 13:56:21 -08003639 }
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003640
Julia Reynolds3b848122016-02-26 10:45:32 -05003641 /**
3642 * Creates a Builder from an existing notification so further changes can be made.
3643 * @param context The context for your application / activity.
3644 * @param n The notification to create a Builder from.
3645 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003646 public static Notification.Builder recoverBuilder(Context context, Notification n) {
Christoph Studer4600f9b2014-07-22 22:44:43 +02003647 // Re-create notification context so we can access app resources.
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003648 ApplicationInfo applicationInfo = n.extras.getParcelable(
3649 EXTRA_BUILDER_APPLICATION_INFO);
Christoph Studer4600f9b2014-07-22 22:44:43 +02003650 Context builderContext;
Julia Reynoldsda303542015-11-23 14:00:20 -05003651 if (applicationInfo != null) {
3652 try {
3653 builderContext = context.createApplicationContext(applicationInfo,
3654 Context.CONTEXT_RESTRICTED);
3655 } catch (NameNotFoundException e) {
3656 Log.e(TAG, "ApplicationInfo " + applicationInfo + " not found");
3657 builderContext = context; // try with our context
3658 }
3659 } else {
3660 builderContext = context; // try with given context
Christoph Studer4600f9b2014-07-22 22:44:43 +02003661 }
3662
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003663 return new Builder(builderContext, n);
Christoph Studer4600f9b2014-07-22 22:44:43 +02003664 }
3665
3666 private static Class<? extends Style> getNotificationStyleClass(String templateClass) {
Selim Cinek593610c2016-02-16 18:42:57 -08003667 Class<? extends Style>[] classes = new Class[] {
3668 BigTextStyle.class, BigPictureStyle.class, InboxStyle.class, MediaStyle.class,
Alex Hillsfc737de2016-03-23 17:33:02 -04003669 DecoratedCustomViewStyle.class, DecoratedMediaCustomViewStyle.class,
3670 MessagingStyle.class };
Christoph Studer4600f9b2014-07-22 22:44:43 +02003671 for (Class<? extends Style> innerClass : classes) {
3672 if (templateClass.equals(innerClass.getName())) {
3673 return innerClass;
3674 }
3675 }
3676 return null;
3677 }
3678
Daniel Sandlerf45564e2013-04-15 15:05:08 -04003679 /**
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003680 * @deprecated Use {@link #build()} instead.
3681 */
3682 @Deprecated
3683 public Notification getNotification() {
3684 return build();
3685 }
3686
3687 /**
3688 * Combine all of the options that have been set and return a new {@link Notification}
3689 * object.
3690 */
3691 public Notification build() {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003692 // first, add any extras from the calling code
3693 if (mUserExtras != null) {
3694 mN.extras = getAllExtras();
Jorim Jaggia0d58ae2015-06-03 11:48:13 -07003695 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003696
3697 // lazy stuff from mContext; see comment in Builder(Context, Notification)
Julia Reynoldsda303542015-11-23 14:00:20 -05003698 Notification.addFieldsFromContext(mContext, mN);
Christoph Studer943aa672014-08-03 20:31:16 +02003699
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003700 buildUnstyled();
Daniel Sandlerf45564e2013-04-15 15:05:08 -04003701
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003702 if (mStyle != null) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003703 mStyle.buildStyled(mN);
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003704 }
Daniel Sandlerf45564e2013-04-15 15:05:08 -04003705
Adrian Roos5081c0d2016-02-26 16:04:19 -08003706 if (mContext.getApplicationInfo().targetSdkVersion < Build.VERSION_CODES.N
3707 && (mStyle == null || !mStyle.displayCustomViewInline())) {
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05003708 if (mN.contentView == null) {
Julia Reynolds3b848122016-02-26 10:45:32 -05003709 mN.contentView = createContentView();
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05003710 mN.extras.putInt(EXTRA_REBUILD_CONTENT_VIEW_ACTION_COUNT,
3711 mN.contentView.getSequenceNumber());
3712 }
3713 if (mN.bigContentView == null) {
Julia Reynolds3b848122016-02-26 10:45:32 -05003714 mN.bigContentView = createBigContentView();
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05003715 if (mN.bigContentView != null) {
3716 mN.extras.putInt(EXTRA_REBUILD_BIG_CONTENT_VIEW_ACTION_COUNT,
3717 mN.bigContentView.getSequenceNumber());
3718 }
3719 }
3720 if (mN.headsUpContentView == null) {
Julia Reynolds3b848122016-02-26 10:45:32 -05003721 mN.headsUpContentView = createHeadsUpContentView();
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05003722 if (mN.headsUpContentView != null) {
3723 mN.extras.putInt(EXTRA_REBUILD_HEADS_UP_CONTENT_VIEW_ACTION_COUNT,
3724 mN.headsUpContentView.getSequenceNumber());
3725 }
3726 }
3727 }
3728
Julia Reynolds4c0c2022016-02-02 15:11:59 -05003729 if ((mN.defaults & DEFAULT_LIGHTS) != 0) {
3730 mN.flags |= FLAG_SHOW_LIGHTS;
3731 }
3732
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003733 return mN;
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003734 }
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05003735
3736 /**
3737 * Apply this Builder to an existing {@link Notification} object.
3738 *
3739 * @hide
3740 */
3741 public Notification buildInto(Notification n) {
Daniel Sandler1a497d32013-04-18 14:52:45 -04003742 build().cloneInto(n, true);
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05003743 return n;
3744 }
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01003745
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05003746 /**
Adrian Roos184bfe022016-03-03 13:41:44 -08003747 * Removes RemoteViews that were created for compatibility from {@param n}, if they did not
3748 * change.
3749 *
3750 * @return {@param n}, if no stripping is needed, otherwise a stripped clone of {@param n}.
3751 *
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05003752 * @hide
3753 */
Adrian Roos184bfe022016-03-03 13:41:44 -08003754 public static Notification maybeCloneStrippedForDelivery(Notification n) {
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05003755 String templateClass = n.extras.getString(EXTRA_TEMPLATE);
Adrian Roos184bfe022016-03-03 13:41:44 -08003756
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05003757 // Only strip views for known Styles because we won't know how to
3758 // re-create them otherwise.
Adrian Roos184bfe022016-03-03 13:41:44 -08003759 if (!TextUtils.isEmpty(templateClass)
3760 && getNotificationStyleClass(templateClass) == null) {
3761 return n;
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05003762 }
Adrian Roos184bfe022016-03-03 13:41:44 -08003763
3764 // Only strip unmodified BuilderRemoteViews.
3765 boolean stripContentView = n.contentView instanceof BuilderRemoteViews &&
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05003766 n.extras.getInt(EXTRA_REBUILD_CONTENT_VIEW_ACTION_COUNT, -1) ==
Adrian Roos184bfe022016-03-03 13:41:44 -08003767 n.contentView.getSequenceNumber();
3768 boolean stripBigContentView = n.bigContentView instanceof BuilderRemoteViews &&
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05003769 n.extras.getInt(EXTRA_REBUILD_BIG_CONTENT_VIEW_ACTION_COUNT, -1) ==
Adrian Roos184bfe022016-03-03 13:41:44 -08003770 n.bigContentView.getSequenceNumber();
3771 boolean stripHeadsUpContentView = n.headsUpContentView instanceof BuilderRemoteViews &&
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05003772 n.extras.getInt(EXTRA_REBUILD_HEADS_UP_CONTENT_VIEW_ACTION_COUNT, -1) ==
Adrian Roos184bfe022016-03-03 13:41:44 -08003773 n.headsUpContentView.getSequenceNumber();
3774
3775 // Nothing to do here, no need to clone.
3776 if (!stripContentView && !stripBigContentView && !stripHeadsUpContentView) {
3777 return n;
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05003778 }
Adrian Roos184bfe022016-03-03 13:41:44 -08003779
3780 Notification clone = n.clone();
3781 if (stripContentView) {
3782 clone.contentView = null;
3783 clone.extras.remove(EXTRA_REBUILD_CONTENT_VIEW_ACTION_COUNT);
3784 }
3785 if (stripBigContentView) {
3786 clone.bigContentView = null;
3787 clone.extras.remove(EXTRA_REBUILD_BIG_CONTENT_VIEW_ACTION_COUNT);
3788 }
3789 if (stripHeadsUpContentView) {
3790 clone.headsUpContentView = null;
3791 clone.extras.remove(EXTRA_REBUILD_HEADS_UP_CONTENT_VIEW_ACTION_COUNT);
3792 }
3793 return clone;
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05003794 }
3795
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01003796 private int getBaseLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07003797 return R.layout.notification_template_material_base;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01003798 }
3799
3800 private int getBigBaseLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07003801 return R.layout.notification_template_material_big_base;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01003802 }
3803
3804 private int getBigPictureLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07003805 return R.layout.notification_template_material_big_picture;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01003806 }
3807
3808 private int getBigTextLayoutResource() {
Jorim Jaggi445d3c02014-08-19 22:33:42 +02003809 return R.layout.notification_template_material_big_text;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01003810 }
3811
3812 private int getInboxLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07003813 return R.layout.notification_template_material_inbox;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01003814 }
3815
Adrian Roosc1a80b02016-04-05 14:54:55 -07003816 private int getMessagingLayoutResource() {
3817 return R.layout.notification_template_material_messaging;
3818 }
3819
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01003820 private int getActionLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07003821 return R.layout.notification_material_action;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01003822 }
3823
3824 private int getActionTombstoneLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07003825 return R.layout.notification_material_action_tombstone;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01003826 }
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003827 }
3828
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003829 /**
3830 * An object that can apply a rich notification style to a {@link Notification.Builder}
3831 * object.
3832 */
Griff Hazendfcb0802014-02-11 12:00:00 -08003833 public static abstract class Style {
Chris Wrend6297db2012-05-03 16:20:13 -04003834 private CharSequence mBigContentTitle;
Jorim Jaggi457a10d2014-09-08 16:18:23 +02003835
3836 /**
3837 * @hide
3838 */
3839 protected CharSequence mSummaryText = null;
3840
3841 /**
3842 * @hide
3843 */
3844 protected boolean mSummaryTextSet = false;
Chris Wrend6297db2012-05-03 16:20:13 -04003845
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003846 protected Builder mBuilder;
3847
Chris Wrend6297db2012-05-03 16:20:13 -04003848 /**
3849 * Overrides ContentTitle in the big form of the template.
3850 * This defaults to the value passed to setContentTitle().
3851 */
3852 protected void internalSetBigContentTitle(CharSequence title) {
3853 mBigContentTitle = title;
3854 }
3855
3856 /**
3857 * Set the first line of text after the detail section in the big form of the template.
3858 */
3859 protected void internalSetSummaryText(CharSequence cs) {
3860 mSummaryText = cs;
Daniel Sandler619738c2012-06-07 16:33:08 -04003861 mSummaryTextSet = true;
Chris Wrend6297db2012-05-03 16:20:13 -04003862 }
3863
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003864 public void setBuilder(Builder builder) {
3865 if (mBuilder != builder) {
3866 mBuilder = builder;
Daniel Sandlerc08dea22012-06-28 08:35:24 -07003867 if (mBuilder != null) {
3868 mBuilder.setStyle(this);
3869 }
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003870 }
3871 }
3872
Chris Wrend6297db2012-05-03 16:20:13 -04003873 protected void checkBuilder() {
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003874 if (mBuilder == null) {
3875 throw new IllegalArgumentException("Style requires a valid Builder object");
3876 }
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003877 }
Chris Wrend6297db2012-05-03 16:20:13 -04003878
3879 protected RemoteViews getStandardView(int layoutId) {
3880 checkBuilder();
3881
Christoph Studer4600f9b2014-07-22 22:44:43 +02003882 // Nasty.
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003883 CharSequence oldBuilderContentTitle =
3884 mBuilder.getAllExtras().getCharSequence(EXTRA_TITLE);
Chris Wrend6297db2012-05-03 16:20:13 -04003885 if (mBigContentTitle != null) {
3886 mBuilder.setContentTitle(mBigContentTitle);
3887 }
3888
Chris Wrend6297db2012-05-03 16:20:13 -04003889 RemoteViews contentView = mBuilder.applyStandardTemplateWithActions(layoutId);
3890
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003891 mBuilder.getAllExtras().putCharSequence(EXTRA_TITLE, oldBuilderContentTitle);
Christoph Studer4600f9b2014-07-22 22:44:43 +02003892
Chris Wrend6297db2012-05-03 16:20:13 -04003893 if (mBigContentTitle != null && mBigContentTitle.equals("")) {
3894 contentView.setViewVisibility(R.id.line1, View.GONE);
Chris Wren67dc9a02012-05-16 01:03:20 -04003895 } else {
3896 contentView.setViewVisibility(R.id.line1, View.VISIBLE);
Chris Wrend6297db2012-05-03 16:20:13 -04003897 }
3898
Chris Wrend6297db2012-05-03 16:20:13 -04003899 return contentView;
3900 }
3901
Daniel Sandlerf45564e2013-04-15 15:05:08 -04003902 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003903 * Construct a Style-specific RemoteViews for the final 1U notification layout.
3904 * The default implementation has nothing additional to add.
3905 * @hide
3906 */
3907 public RemoteViews makeContentView() {
3908 return null;
3909 }
3910
3911 /**
3912 * Construct a Style-specific RemoteViews for the final big notification layout.
3913 * @hide
3914 */
3915 public RemoteViews makeBigContentView() {
3916 return null;
3917 }
3918
3919 /**
3920 * Construct a Style-specific RemoteViews for the final HUN layout.
3921 * @hide
3922 */
3923 public RemoteViews makeHeadsUpContentView() {
3924 return null;
3925 }
3926
3927 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003928 * Apply any style-specific extras to this notification before shipping it out.
Daniel Sandlerf45564e2013-04-15 15:05:08 -04003929 * @hide
3930 */
3931 public void addExtras(Bundle extras) {
3932 if (mSummaryTextSet) {
3933 extras.putCharSequence(EXTRA_SUMMARY_TEXT, mSummaryText);
3934 }
3935 if (mBigContentTitle != null) {
3936 extras.putCharSequence(EXTRA_TITLE_BIG, mBigContentTitle);
3937 }
Chris Wren91ad5632013-06-05 15:05:57 -04003938 extras.putString(EXTRA_TEMPLATE, this.getClass().getName());
Daniel Sandlerf45564e2013-04-15 15:05:08 -04003939 }
3940
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04003941 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003942 * Reconstruct the internal state of this Style object from extras.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04003943 * @hide
3944 */
Christoph Studer4600f9b2014-07-22 22:44:43 +02003945 protected void restoreFromExtras(Bundle extras) {
3946 if (extras.containsKey(EXTRA_SUMMARY_TEXT)) {
3947 mSummaryText = extras.getCharSequence(EXTRA_SUMMARY_TEXT);
3948 mSummaryTextSet = true;
3949 }
3950 if (extras.containsKey(EXTRA_TITLE_BIG)) {
3951 mBigContentTitle = extras.getCharSequence(EXTRA_TITLE_BIG);
3952 }
3953 }
3954
3955
3956 /**
3957 * @hide
3958 */
3959 public Notification buildStyled(Notification wip) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003960 addExtras(wip.extras);
Christoph Studer4600f9b2014-07-22 22:44:43 +02003961 return wip;
3962 }
3963
Daniel Sandler0ec46202015-06-24 01:27:05 -04003964 /**
3965 * @hide
3966 */
Jorim Jaggia0d58ae2015-06-03 11:48:13 -07003967 public void purgeResources() {}
3968
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04003969 /**
3970 * Calls {@link android.app.Notification.Builder#build()} on the Builder this Style is
3971 * attached to.
3972 *
3973 * @return the fully constructed Notification.
3974 */
3975 public Notification build() {
3976 checkBuilder();
3977 return mBuilder.build();
3978 }
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02003979
3980 /**
3981 * @hide
3982 * @return true if the style positions the progress bar on the second line; false if the
3983 * style hides the progress bar
3984 */
3985 protected boolean hasProgress() {
3986 return true;
3987 }
Selim Cinek03d0d652015-11-13 13:18:09 -05003988
3989 /**
3990 * @hide
3991 * @return Whether we should put the summary be put into the notification header
3992 */
3993 public boolean hasSummaryInHeader() {
3994 return true;
3995 }
Selim Cinek593610c2016-02-16 18:42:57 -08003996
3997 /**
3998 * @hide
3999 * @return Whether custom content views are displayed inline in the style
4000 */
4001 public boolean displayCustomViewInline() {
4002 return false;
4003 }
Joe Onorato46439ce2010-11-19 13:56:21 -08004004 }
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004005
4006 /**
Daniel Sandler4dfbe832012-04-11 14:51:46 -04004007 * Helper class for generating large-format notifications that include a large image attachment.
Joe Malin8d40d042012-11-05 11:36:40 -08004008 *
Robert Ly91c5ce32014-06-08 15:37:00 -07004009 * Here's how you'd set the <code>BigPictureStyle</code> on a notification:
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004010 * <pre class="prettyprint">
Robert Ly91c5ce32014-06-08 15:37:00 -07004011 * Notification notif = new Notification.Builder(mContext)
4012 * .setContentTitle(&quot;New photo from &quot; + sender.toString())
4013 * .setContentText(subject)
4014 * .setSmallIcon(R.drawable.new_post)
4015 * .setLargeIcon(aBitmap)
4016 * .setStyle(new Notification.BigPictureStyle()
4017 * .bigPicture(aBigBitmap))
4018 * .build();
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004019 * </pre>
Joe Malin8d40d042012-11-05 11:36:40 -08004020 *
Daniel Sandler4dfbe832012-04-11 14:51:46 -04004021 * @see Notification#bigContentView
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004022 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004023 public static class BigPictureStyle extends Style {
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004024 private Bitmap mPicture;
Dan Sandlerd63f9322015-05-06 15:18:49 -04004025 private Icon mBigLargeIcon;
Chris Wren3745a3d2012-05-22 15:11:52 -04004026 private boolean mBigLargeIconSet = false;
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004027
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004028 public BigPictureStyle() {
4029 }
4030
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004031 public BigPictureStyle(Builder builder) {
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004032 setBuilder(builder);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004033 }
4034
Chris Wrend6297db2012-05-03 16:20:13 -04004035 /**
4036 * Overrides ContentTitle in the big form of the template.
4037 * This defaults to the value passed to setContentTitle().
4038 */
4039 public BigPictureStyle setBigContentTitle(CharSequence title) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04004040 internalSetBigContentTitle(safeCharSequence(title));
Chris Wrend6297db2012-05-03 16:20:13 -04004041 return this;
4042 }
4043
4044 /**
4045 * Set the first line of text after the detail section in the big form of the template.
4046 */
4047 public BigPictureStyle setSummaryText(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04004048 internalSetSummaryText(safeCharSequence(cs));
Chris Wrend6297db2012-05-03 16:20:13 -04004049 return this;
4050 }
4051
Chris Wren0bd664d2012-08-01 13:56:56 -04004052 /**
4053 * Provide the bitmap to be used as the payload for the BigPicture notification.
4054 */
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004055 public BigPictureStyle bigPicture(Bitmap b) {
4056 mPicture = b;
4057 return this;
4058 }
4059
Chris Wren3745a3d2012-05-22 15:11:52 -04004060 /**
Chris Wren3745a3d2012-05-22 15:11:52 -04004061 * Override the large icon when the big notification is shown.
4062 */
4063 public BigPictureStyle bigLargeIcon(Bitmap b) {
Dan Sandlerd63f9322015-05-06 15:18:49 -04004064 return bigLargeIcon(b != null ? Icon.createWithBitmap(b) : null);
4065 }
4066
4067 /**
4068 * Override the large icon when the big notification is shown.
4069 */
4070 public BigPictureStyle bigLargeIcon(Icon icon) {
Chris Wren3745a3d2012-05-22 15:11:52 -04004071 mBigLargeIconSet = true;
Dan Sandlerd63f9322015-05-06 15:18:49 -04004072 mBigLargeIcon = icon;
Chris Wren3745a3d2012-05-22 15:11:52 -04004073 return this;
4074 }
4075
Riley Andrews0394a0c2015-11-03 23:36:52 -08004076 /** @hide */
4077 public static final int MIN_ASHMEM_BITMAP_SIZE = 128 * (1 << 10);
4078
Daniel Sandler0ec46202015-06-24 01:27:05 -04004079 /**
4080 * @hide
4081 */
Jorim Jaggia0d58ae2015-06-03 11:48:13 -07004082 @Override
4083 public void purgeResources() {
4084 super.purgeResources();
Riley Andrews8cee7c12015-11-01 23:36:04 -08004085 if (mPicture != null &&
4086 mPicture.isMutable() &&
Riley Andrews0394a0c2015-11-03 23:36:52 -08004087 mPicture.getAllocationByteCount() >= MIN_ASHMEM_BITMAP_SIZE) {
Jorim Jaggia0d58ae2015-06-03 11:48:13 -07004088 mPicture = mPicture.createAshmemBitmap();
4089 }
4090 if (mBigLargeIcon != null) {
4091 mBigLargeIcon.convertToAshmem();
4092 }
4093 }
Christoph Studer5c510ee2014-12-15 16:32:27 +01004094
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004095 /**
4096 * @hide
4097 */
4098 public RemoteViews makeBigContentView() {
4099 // Replace mN.mLargeIcon with mBigLargeIcon if mBigLargeIconSet
Christoph Studer5c510ee2014-12-15 16:32:27 +01004100 // This covers the following cases:
4101 // 1. mBigLargeIconSet -> mBigLargeIcon (null or non-null) applies, overrides
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004102 // mN.mLargeIcon
4103 // 2. !mBigLargeIconSet -> mN.mLargeIcon applies
Dan Sandlerd63f9322015-05-06 15:18:49 -04004104 Icon oldLargeIcon = null;
Christoph Studer5c510ee2014-12-15 16:32:27 +01004105 if (mBigLargeIconSet) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004106 oldLargeIcon = mBuilder.mN.mLargeIcon;
4107 mBuilder.mN.mLargeIcon = mBigLargeIcon;
Christoph Studer5c510ee2014-12-15 16:32:27 +01004108 }
4109
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01004110 RemoteViews contentView = getStandardView(mBuilder.getBigPictureLayoutResource());
Selim Cinek03d0d652015-11-13 13:18:09 -05004111 if (mSummaryTextSet) {
4112 contentView.setTextViewText(R.id.text, mBuilder.processLegacyText(mSummaryText));
Selim Cinekc848c3a2016-01-13 15:27:30 -08004113 contentView.setViewVisibility(R.id.text, View.VISIBLE);
Selim Cinek03d0d652015-11-13 13:18:09 -05004114 }
Selim Cinek860b6da2015-12-16 19:02:19 -08004115 mBuilder.setContentMinHeight(contentView, mBuilder.mN.mLargeIcon != null);
Selim Cinek53e64a42015-11-16 10:40:56 -08004116
Christoph Studer5c510ee2014-12-15 16:32:27 +01004117 if (mBigLargeIconSet) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004118 mBuilder.mN.mLargeIcon = oldLargeIcon;
Christoph Studer5c510ee2014-12-15 16:32:27 +01004119 }
4120
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004121 contentView.setImageViewBitmap(R.id.big_picture, mPicture);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004122 return contentView;
4123 }
4124
Daniel Sandlerf45564e2013-04-15 15:05:08 -04004125 /**
4126 * @hide
4127 */
4128 public void addExtras(Bundle extras) {
4129 super.addExtras(extras);
4130
4131 if (mBigLargeIconSet) {
4132 extras.putParcelable(EXTRA_LARGE_ICON_BIG, mBigLargeIcon);
4133 }
4134 extras.putParcelable(EXTRA_PICTURE, mPicture);
4135 }
4136
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04004137 /**
4138 * @hide
4139 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004140 @Override
Christoph Studer4600f9b2014-07-22 22:44:43 +02004141 protected void restoreFromExtras(Bundle extras) {
4142 super.restoreFromExtras(extras);
4143
4144 if (extras.containsKey(EXTRA_LARGE_ICON_BIG)) {
Christoph Studer5c510ee2014-12-15 16:32:27 +01004145 mBigLargeIconSet = true;
Christoph Studer4600f9b2014-07-22 22:44:43 +02004146 mBigLargeIcon = extras.getParcelable(EXTRA_LARGE_ICON_BIG);
Chris Wren3745a3d2012-05-22 15:11:52 -04004147 }
Christoph Studer4600f9b2014-07-22 22:44:43 +02004148 mPicture = extras.getParcelable(EXTRA_PICTURE);
4149 }
Selim Cinek03d0d652015-11-13 13:18:09 -05004150
4151 /**
4152 * @hide
4153 */
4154 @Override
4155 public boolean hasSummaryInHeader() {
4156 return false;
4157 }
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004158 }
4159
4160 /**
Daniel Sandler4dfbe832012-04-11 14:51:46 -04004161 * Helper class for generating large-format notifications that include a lot of text.
Joe Malin8d40d042012-11-05 11:36:40 -08004162 *
Robert Ly91c5ce32014-06-08 15:37:00 -07004163 * Here's how you'd set the <code>BigTextStyle</code> on a notification:
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004164 * <pre class="prettyprint">
Robert Ly91c5ce32014-06-08 15:37:00 -07004165 * Notification notif = new Notification.Builder(mContext)
4166 * .setContentTitle(&quot;New mail from &quot; + sender.toString())
4167 * .setContentText(subject)
4168 * .setSmallIcon(R.drawable.new_mail)
4169 * .setLargeIcon(aBitmap)
4170 * .setStyle(new Notification.BigTextStyle()
4171 * .bigText(aVeryLongString))
4172 * .build();
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004173 * </pre>
Joe Malin8d40d042012-11-05 11:36:40 -08004174 *
Daniel Sandler4dfbe832012-04-11 14:51:46 -04004175 * @see Notification#bigContentView
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004176 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004177 public static class BigTextStyle extends Style {
Jorim Jaggi457a10d2014-09-08 16:18:23 +02004178
4179 private static final int MAX_LINES = 13;
Selim Cinek3a2c4b92015-12-17 17:01:17 -08004180 private static final int LINES_CONSUMED_BY_ACTIONS = 4;
Jorim Jaggi457a10d2014-09-08 16:18:23 +02004181
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004182 private CharSequence mBigText;
4183
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004184 public BigTextStyle() {
4185 }
4186
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004187 public BigTextStyle(Builder builder) {
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004188 setBuilder(builder);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004189 }
4190
Chris Wrend6297db2012-05-03 16:20:13 -04004191 /**
4192 * Overrides ContentTitle in the big form of the template.
4193 * This defaults to the value passed to setContentTitle().
4194 */
4195 public BigTextStyle setBigContentTitle(CharSequence title) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04004196 internalSetBigContentTitle(safeCharSequence(title));
Chris Wrend6297db2012-05-03 16:20:13 -04004197 return this;
4198 }
4199
4200 /**
4201 * Set the first line of text after the detail section in the big form of the template.
4202 */
4203 public BigTextStyle setSummaryText(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04004204 internalSetSummaryText(safeCharSequence(cs));
Chris Wrend6297db2012-05-03 16:20:13 -04004205 return this;
4206 }
4207
Chris Wren0bd664d2012-08-01 13:56:56 -04004208 /**
4209 * Provide the longer text to be displayed in the big form of the
4210 * template in place of the content text.
4211 */
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004212 public BigTextStyle bigText(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04004213 mBigText = safeCharSequence(cs);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004214 return this;
4215 }
4216
Daniel Sandlerf45564e2013-04-15 15:05:08 -04004217 /**
4218 * @hide
4219 */
4220 public void addExtras(Bundle extras) {
4221 super.addExtras(extras);
4222
Christoph Studer4600f9b2014-07-22 22:44:43 +02004223 extras.putCharSequence(EXTRA_BIG_TEXT, mBigText);
4224 }
4225
4226 /**
4227 * @hide
4228 */
4229 @Override
4230 protected void restoreFromExtras(Bundle extras) {
4231 super.restoreFromExtras(extras);
4232
4233 mBigText = extras.getCharSequence(EXTRA_BIG_TEXT);
Daniel Sandlerf45564e2013-04-15 15:05:08 -04004234 }
4235
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004236 /**
4237 * @hide
4238 */
4239 public RemoteViews makeBigContentView() {
Christoph Studer4600f9b2014-07-22 22:44:43 +02004240
4241 // Nasty
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004242 CharSequence oldBuilderContentText =
4243 mBuilder.getAllExtras().getCharSequence(EXTRA_TEXT);
4244 mBuilder.getAllExtras().putCharSequence(EXTRA_TEXT, null);
Daniel Sandler916ad912012-06-13 12:17:07 -04004245
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01004246 RemoteViews contentView = getStandardView(mBuilder.getBigTextLayoutResource());
Joe Malin8d40d042012-11-05 11:36:40 -08004247
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004248 mBuilder.getAllExtras().putCharSequence(EXTRA_TEXT, oldBuilderContentText);
Christoph Studer4600f9b2014-07-22 22:44:43 +02004249
Selim Cinek3a2c4b92015-12-17 17:01:17 -08004250 CharSequence bigTextText = mBuilder.processLegacyText(mBigText);
4251 contentView.setTextViewText(R.id.big_text, bigTextText);
4252 contentView.setViewVisibility(R.id.big_text,
4253 TextUtils.isEmpty(bigTextText) ? View.GONE : View.VISIBLE);
Jorim Jaggi457a10d2014-09-08 16:18:23 +02004254 contentView.setInt(R.id.big_text, "setMaxLines", calculateMaxLines());
Selim Cinek4fb12d32015-11-19 18:10:48 -08004255 contentView.setBoolean(R.id.big_text, "setHasImage", mBuilder.mN.mLargeIcon != null);
4256
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004257 return contentView;
4258 }
4259
Jorim Jaggi457a10d2014-09-08 16:18:23 +02004260 private int calculateMaxLines() {
4261 int lineCount = MAX_LINES;
4262 boolean hasActions = mBuilder.mActions.size() > 0;
Jorim Jaggi457a10d2014-09-08 16:18:23 +02004263 if (hasActions) {
4264 lineCount -= LINES_CONSUMED_BY_ACTIONS;
4265 }
Jorim Jaggi457a10d2014-09-08 16:18:23 +02004266 return lineCount;
4267 }
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004268 }
Daniel Sandler879c5e02012-04-17 16:46:51 -04004269
4270 /**
Alex Hillsfc737de2016-03-23 17:33:02 -04004271 * Helper class for generating large-format notifications that include multiple back-and-forth
4272 * messages of varying types between any number of people.
4273 *
4274 * <br>
4275 * If the platform does not provide large-format notifications, this method has no effect. The
4276 * user will always see the normal notification view.
4277 * <br>
4278 * This class is a "rebuilder": It attaches to a Builder object and modifies its behavior, like
4279 * so:
4280 * <pre class="prettyprint">
4281 *
4282 * Notification noti = new Notification.Builder()
4283 * .setContentTitle(&quot;2 new messages wtih &quot; + sender.toString())
4284 * .setContentText(subject)
4285 * .setSmallIcon(R.drawable.new_message)
4286 * .setLargeIcon(aBitmap)
4287 * .setStyle(new Notification.MessagingStyle(resources.getString(R.string.reply_name))
4288 * .addMessage(messages[0].getText(), messages[0].getTime(), messages[0].getSender())
4289 * .addMessage(messages[1].getText(), messages[1].getTime(), messages[1].getSender()))
4290 * .build();
4291 * </pre>
4292 */
4293 public static class MessagingStyle extends Style {
4294
4295 /**
4296 * The maximum number of messages that will be retained in the Notification itself (the
4297 * number displayed is up to the platform).
4298 */
4299 public static final int MAXIMUM_RETAINED_MESSAGES = 25;
4300
4301 CharSequence mUserDisplayName;
4302 CharSequence mConversationTitle;
4303 boolean mAllowGeneratedReplies = true;
Alex Hillsd9b04d92016-04-11 16:38:16 -04004304 List<Message> mMessages = new ArrayList<>();
Alex Hillsfc737de2016-03-23 17:33:02 -04004305
4306 MessagingStyle() {
4307 }
4308
4309 /**
4310 * @param userDisplayName the name to be displayed for any replies sent by the user before the
4311 * posting app reposts the notification with those messages after they've been actually
4312 * sent and in previous messages sent by the user added in
4313 * {@link #addMessage(Notification.MessagingStyle.Message)}
4314 */
4315 public MessagingStyle(CharSequence userDisplayName) {
4316 mUserDisplayName = userDisplayName;
4317 }
4318
4319 /**
4320 * Returns the name to be displayed for any replies sent by the user
4321 */
4322 public CharSequence getUserDisplayName() {
4323 return mUserDisplayName;
4324 }
4325
4326 /**
4327 * Set whether the platform should automatically generate possible replies from messages.
4328 * @param allowGeneratedReplies {@code true} to allow generated replies, {@code false}
4329 * otherwise
4330 * @return this object for method chaining
4331 * The default value is {@code true}
4332 */
4333 public MessagingStyle setAllowGeneratedReplies(boolean allowGeneratedReplies) {
4334 mAllowGeneratedReplies = allowGeneratedReplies;
4335 return this;
4336 }
4337
4338 /**
4339 * Return whether the platform should automatically generate possible replies from messages.
4340 */
4341 public boolean getAllowGeneratedReplies() {
4342 return mAllowGeneratedReplies;
4343 }
4344
4345 /**
4346 * Sets the title to be displayed on this conversation. This should only be used for
4347 * group messaging and left unset for one-on-one conversations.
4348 * @param conversationTitle
4349 * @return this object for method chaining.
4350 */
4351 public MessagingStyle setConversationTitle(CharSequence conversationTitle) {
4352 mConversationTitle = conversationTitle;
4353 return this;
4354 }
4355
4356 /**
4357 * Return the title to be displayed on this conversation. Can be <code>null</code> and
4358 * should be for one-on-one conversations
4359 */
4360 public CharSequence getConversationTitle() {
4361 return mConversationTitle;
4362 }
4363
4364 /**
4365 * Adds a message for display by this notification. Convenience call for a simple
4366 * {@link Message} in {@link #addMessage(Notification.MessagingStyle.Message)}.
4367 * @param text A {@link CharSequence} to be displayed as the message content
4368 * @param timestamp Time at which the message arrived
4369 * @param sender A {@link CharSequence} to be used for displaying the name of the
4370 * sender. Should be <code>null</code> for messages by the current user, in which case
4371 * the platform will insert {@link #getUserDisplayName()}.
4372 * Should be unique amongst all individuals in the conversation, and should be
4373 * consistent during re-posts of the notification.
4374 *
4375 * @see Message#Message(CharSequence, long, CharSequence)
4376 *
4377 * @return this object for method chaining
4378 */
4379 public MessagingStyle addMessage(CharSequence text, long timestamp, CharSequence sender) {
4380 mMessages.add(new Message(text, timestamp, sender));
4381 if (mMessages.size() > MAXIMUM_RETAINED_MESSAGES) {
4382 mMessages.remove(0);
4383 }
4384 return this;
4385 }
4386
4387 /**
4388 * Adds a {@link Message} for display in this notification.
4389 * @param message The {@link Message} to be displayed
4390 * @return this object for method chaining
4391 */
4392 public MessagingStyle addMessage(Message message) {
4393 mMessages.add(message);
4394 if (mMessages.size() > MAXIMUM_RETAINED_MESSAGES) {
4395 mMessages.remove(0);
4396 }
4397 return this;
4398 }
4399
4400 /**
4401 * Gets the list of {@code Message} objects that represent the notification
4402 */
4403 public List<Message> getMessages() {
4404 return mMessages;
4405 }
4406
4407 /**
4408 * @hide
4409 */
4410 @Override
4411 public void addExtras(Bundle extras) {
4412 super.addExtras(extras);
4413 if (mUserDisplayName != null) {
4414 extras.putCharSequence(EXTRA_SELF_DISPLAY_NAME, mUserDisplayName);
4415 }
4416 if (mConversationTitle != null) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04004417 extras.putCharSequence(EXTRA_CONVERSATION_TITLE, mConversationTitle);
Alex Hillsfc737de2016-03-23 17:33:02 -04004418 }
4419 extras.putBoolean(EXTRA_ALLOW_GENERATED_REPLIES, mAllowGeneratedReplies);
Alex Hillsd9b04d92016-04-11 16:38:16 -04004420 if (!mMessages.isEmpty()) { extras.putParcelableArray(EXTRA_MESSAGES,
4421 Message.getBundleArrayForMessages(mMessages));
Alex Hillsfc737de2016-03-23 17:33:02 -04004422 }
4423 }
4424
4425 /**
4426 * @hide
4427 */
4428 @Override
4429 protected void restoreFromExtras(Bundle extras) {
4430 super.restoreFromExtras(extras);
4431
4432 mMessages.clear();
4433 mUserDisplayName = extras.getString(EXTRA_SELF_DISPLAY_NAME);
Alex Hillsd9b04d92016-04-11 16:38:16 -04004434 mConversationTitle = extras.getString(EXTRA_CONVERSATION_TITLE);
Alex Hillsfc737de2016-03-23 17:33:02 -04004435 mAllowGeneratedReplies = extras.getBoolean(EXTRA_ALLOW_GENERATED_REPLIES,
4436 mAllowGeneratedReplies);
Alex Hillsd9b04d92016-04-11 16:38:16 -04004437 Parcelable[] parcelables = extras.getParcelableArray(EXTRA_MESSAGES);
4438 if (parcelables != null && parcelables instanceof Bundle[]) {
4439 mMessages = Message.getMessagesFromBundleArray((Bundle[]) parcelables);
Alex Hillsfc737de2016-03-23 17:33:02 -04004440 }
4441 }
4442
4443 /**
4444 * @hide
4445 */
Adrian Roosc1a80b02016-04-05 14:54:55 -07004446 @Override
4447 public RemoteViews makeContentView() {
4448 Message m = findLatestIncomingMessage();
4449 CharSequence title = mConversationTitle != null
4450 ? mConversationTitle
4451 : (m == null) ? null : m.mSender;
4452 CharSequence text = (m == null)
4453 ? null
4454 : mConversationTitle != null ? makeMessageLine(m) : m.mText;
Alex Hillsfc737de2016-03-23 17:33:02 -04004455
Adrian Roosc1a80b02016-04-05 14:54:55 -07004456 return mBuilder.applyStandardTemplate(mBuilder.getBaseLayoutResource(),
4457 false /* hasProgress */,
4458 title,
4459 text);
4460 }
4461
4462 private Message findLatestIncomingMessage() {
4463 for (int i = mMessages.size() - 1; i >= 0; i--) {
4464 Message m = mMessages.get(i);
4465 // Incoming messages have a non-empty sender.
4466 if (!TextUtils.isEmpty(m.mSender)) {
4467 return m;
4468 }
4469 }
4470 return null;
4471 }
4472
4473 /**
4474 * @hide
4475 */
4476 @Override
4477 public RemoteViews makeBigContentView() {
4478 CharSequence title = !TextUtils.isEmpty(super.mBigContentTitle)
4479 ? super.mBigContentTitle
4480 : mConversationTitle;
4481 boolean hasTitle = !TextUtils.isEmpty(title);
4482
Adrian Roos48d746a2016-04-12 14:57:28 -07004483 RemoteViews contentView = mBuilder.applyStandardTemplateWithActions(
Adrian Roosc1a80b02016-04-05 14:54:55 -07004484 mBuilder.getMessagingLayoutResource(),
4485 false /* hasProgress */,
4486 title,
4487 null /* text */);
4488
4489 int[] rowIds = {R.id.inbox_text0, R.id.inbox_text1, R.id.inbox_text2, R.id.inbox_text3,
4490 R.id.inbox_text4, R.id.inbox_text5, R.id.inbox_text6};
4491
4492 // Make sure all rows are gone in case we reuse a view.
4493 for (int rowId : rowIds) {
4494 contentView.setViewVisibility(rowId, View.GONE);
4495 }
4496
4497 int i=0;
4498 int titlePadding = mBuilder.mContext.getResources().getDimensionPixelSize(
4499 R.dimen.notification_messaging_spacing);
4500 contentView.setViewLayoutMarginBottom(R.id.line1, hasTitle ? titlePadding : 0);
4501 contentView.setInt(R.id.notification_messaging, "setNumIndentLines",
4502 mBuilder.mN.mLargeIcon == null ? 0 : (hasTitle ? 1 : 2));
4503
4504 int firstMessage = Math.max(0, mMessages.size() - rowIds.length);
4505 while (firstMessage + i < mMessages.size() && i < rowIds.length) {
4506 Message m = mMessages.get(firstMessage + i);
4507 int rowId = rowIds[i];
4508
4509 contentView.setViewVisibility(rowId, View.VISIBLE);
4510 contentView.setTextViewText(rowId, makeMessageLine(m));
4511
4512 i++;
4513 }
Alex Hillsfc737de2016-03-23 17:33:02 -04004514 return contentView;
4515 }
4516
Adrian Roosc1a80b02016-04-05 14:54:55 -07004517 private CharSequence makeMessageLine(Message m) {
4518 BidiFormatter bidi = BidiFormatter.getInstance();
4519 SpannableStringBuilder sb = new SpannableStringBuilder();
4520 if (TextUtils.isEmpty(m.mSender)) {
4521 CharSequence replyName = mUserDisplayName == null ? "" : mUserDisplayName;
4522 sb.append(bidi.unicodeWrap(replyName),
4523 makeFontColorSpan(mBuilder.resolveContrastColor()),
4524 0 /* flags */);
4525 } else {
4526 sb.append(bidi.unicodeWrap(m.mSender),
4527 makeFontColorSpan(Color.BLACK),
4528 0 /* flags */);
4529 }
4530 CharSequence text = m.mText == null ? "" : m.mText;
4531 sb.append(" ").append(bidi.unicodeWrap(text));
4532 return sb;
4533 }
4534
4535 private static TextAppearanceSpan makeFontColorSpan(int color) {
4536 return new TextAppearanceSpan(null, 0, 0,
4537 ColorStateList.valueOf(color), null);
4538 }
4539
Alex Hillsd9b04d92016-04-11 16:38:16 -04004540 public static final class Message {
4541
4542 static final String KEY_TEXT = "text";
4543 static final String KEY_TIMESTAMP = "time";
4544 static final String KEY_SENDER = "sender";
4545 static final String KEY_DATA_MIME_TYPE = "type";
4546 static final String KEY_DATA_URI= "uri";
Alex Hillsfc737de2016-03-23 17:33:02 -04004547
4548 private final CharSequence mText;
4549 private final long mTimestamp;
4550 private final CharSequence mSender;
4551
4552 private String mDataMimeType;
4553 private Uri mDataUri;
4554
4555 /**
4556 * Constructor
4557 * @param text A {@link CharSequence} to be displayed as the message content
4558 * @param timestamp Time at which the message arrived
4559 * @param sender A {@link CharSequence} to be used for displaying the name of the
4560 * sender. Should be <code>null</code> for messages by the current user, in which case
4561 * the platform will insert {@link MessagingStyle#getUserDisplayName()}.
4562 * Should be unique amongst all individuals in the conversation, and should be
4563 * consistent during re-posts of the notification.
4564 */
4565 public Message(CharSequence text, long timestamp, CharSequence sender){
4566 mText = text;
4567 mTimestamp = timestamp;
4568 mSender = sender;
4569 }
4570
4571 /**
4572 * Sets a binary blob of data and an associated MIME type for a message. In the case
4573 * where the platform doesn't support the MIME type, the original text provided in the
4574 * constructor will be used.
4575 * @param dataMimeType The MIME type of the content. See
4576 * <a href="{@docRoot}notifications/messaging.html"> for the list of supported MIME
4577 * types on Android and Android Wear.
4578 * @param dataUri The uri containing the content whose type is given by the MIME type.
4579 * <p class="note">
4580 * <ol>
4581 * <li>Notification Listeners including the System UI need permission to access the
4582 * data the Uri points to. The recommended ways to do this are:</li>
4583 * <li>Store the data in your own ContentProvider, making sure that other apps have
4584 * the correct permission to access your provider. The preferred mechanism for
4585 * providing access is to use per-URI permissions which are temporary and only
4586 * grant access to the receiving application. An easy way to create a
4587 * ContentProvider like this is to use the FileProvider helper class.</li>
4588 * <li>Use the system MediaStore. The MediaStore is primarily aimed at video, audio
4589 * and image MIME types, however beginning with Android 3.0 (API level 11) it can
4590 * also store non-media types (see MediaStore.Files for more info). Files can be
4591 * inserted into the MediaStore using scanFile() after which a content:// style
4592 * Uri suitable for sharing is passed to the provided onScanCompleted() callback.
4593 * Note that once added to the system MediaStore the content is accessible to any
4594 * app on the device.</li>
4595 * </ol>
4596 * @return this object for method chaining
4597 */
4598 public Message setData(String dataMimeType, Uri dataUri) {
4599 mDataMimeType = dataMimeType;
4600 mDataUri = dataUri;
4601 return this;
4602 }
4603
Alex Hillsfc737de2016-03-23 17:33:02 -04004604 /**
4605 * Get the text to be used for this message, or the fallback text if a type and content
4606 * Uri have been set
4607 */
4608 public CharSequence getText() {
4609 return mText;
4610 }
4611
4612 /**
4613 * Get the time at which this message arrived
4614 */
4615 public long getTimestamp() {
4616 return mTimestamp;
4617 }
4618
4619 /**
4620 * Get the text used to display the contact's name in the messaging experience
4621 */
4622 public CharSequence getSender() {
4623 return mSender;
4624 }
4625
4626 /**
4627 * Get the MIME type of the data pointed to by the Uri
4628 */
4629 public String getDataMimeType() {
4630 return mDataMimeType;
4631 }
4632
4633 /**
4634 * Get the the Uri pointing to the content of the message. Can be null, in which case
4635 * {@see #getText()} is used.
4636 */
4637 public Uri getDataUri() {
4638 return mDataUri;
4639 }
4640
Alex Hillsd9b04d92016-04-11 16:38:16 -04004641 private Bundle toBundle() {
4642 Bundle bundle = new Bundle();
Alex Hillsfc737de2016-03-23 17:33:02 -04004643 if (mText != null) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04004644 bundle.putCharSequence(KEY_TEXT, mText);
Alex Hillsfc737de2016-03-23 17:33:02 -04004645 }
Alex Hillsd9b04d92016-04-11 16:38:16 -04004646 bundle.putLong(KEY_TIMESTAMP, mTimestamp);
Alex Hillsfc737de2016-03-23 17:33:02 -04004647 if (mSender != null) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04004648 bundle.putCharSequence(KEY_SENDER, mSender);
Alex Hillsfc737de2016-03-23 17:33:02 -04004649 }
4650 if (mDataMimeType != null) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04004651 bundle.putString(KEY_DATA_MIME_TYPE, mDataMimeType);
Alex Hillsfc737de2016-03-23 17:33:02 -04004652 }
4653 if (mDataUri != null) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04004654 bundle.putParcelable(KEY_DATA_URI, mDataUri);
Alex Hillsfc737de2016-03-23 17:33:02 -04004655 }
Alex Hillsd9b04d92016-04-11 16:38:16 -04004656 return bundle;
Alex Hillsfc737de2016-03-23 17:33:02 -04004657 }
4658
Alex Hillsd9b04d92016-04-11 16:38:16 -04004659 static Bundle[] getBundleArrayForMessages(List<Message> messages) {
4660 Bundle[] bundles = new Bundle[messages.size()];
4661 final int N = messages.size();
4662 for (int i = 0; i < N; i++) {
4663 bundles[i] = messages.get(i).toBundle();
4664 }
4665 return bundles;
4666 }
4667
4668 static List<Message> getMessagesFromBundleArray(Bundle[] bundles) {
4669 List<Message> messages = new ArrayList<>(bundles.length);
4670 for (int i = 0; i < bundles.length; i++) {
4671 Message message = getMessageFromBundle(bundles[i]);
4672 if (message != null) {
4673 messages.add(message);
4674 }
4675 }
4676 return messages;
4677 }
4678
4679 static Message getMessageFromBundle(Bundle bundle) {
4680 try {
4681 if (!bundle.containsKey(KEY_TEXT) || !bundle.containsKey(KEY_TIMESTAMP) ||
4682 !bundle.containsKey(KEY_SENDER)) {
4683 return null;
4684 } else {
4685 Message message = new Message(bundle.getCharSequence(KEY_TEXT),
4686 bundle.getLong(KEY_TIMESTAMP), bundle.getCharSequence(KEY_SENDER));
4687 if (bundle.containsKey(KEY_DATA_MIME_TYPE) &&
4688 bundle.containsKey(KEY_DATA_URI)) {
4689
4690 message.setData(bundle.getString(KEY_DATA_MIME_TYPE),
4691 (Uri) bundle.getParcelable(KEY_DATA_URI));
Alex Hillsfc737de2016-03-23 17:33:02 -04004692 }
Alex Hillsd9b04d92016-04-11 16:38:16 -04004693 return message;
4694 }
4695 } catch (ClassCastException e) {
4696 return null;
4697 }
4698 }
Alex Hillsfc737de2016-03-23 17:33:02 -04004699 }
4700 }
4701
4702 /**
Daniel Sandler879c5e02012-04-17 16:46:51 -04004703 * Helper class for generating large-format notifications that include a list of (up to 5) strings.
Joe Malin8d40d042012-11-05 11:36:40 -08004704 *
Robert Ly91c5ce32014-06-08 15:37:00 -07004705 * Here's how you'd set the <code>InboxStyle</code> on a notification:
Daniel Sandler879c5e02012-04-17 16:46:51 -04004706 * <pre class="prettyprint">
Robert Ly91c5ce32014-06-08 15:37:00 -07004707 * Notification notif = new Notification.Builder(mContext)
4708 * .setContentTitle(&quot;5 New mails from &quot; + sender.toString())
4709 * .setContentText(subject)
4710 * .setSmallIcon(R.drawable.new_mail)
4711 * .setLargeIcon(aBitmap)
4712 * .setStyle(new Notification.InboxStyle()
4713 * .addLine(str1)
4714 * .addLine(str2)
4715 * .setContentTitle(&quot;&quot;)
4716 * .setSummaryText(&quot;+3 more&quot;))
4717 * .build();
Daniel Sandler879c5e02012-04-17 16:46:51 -04004718 * </pre>
Joe Malin8d40d042012-11-05 11:36:40 -08004719 *
Daniel Sandler879c5e02012-04-17 16:46:51 -04004720 * @see Notification#bigContentView
4721 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004722 public static class InboxStyle extends Style {
Daniel Sandler879c5e02012-04-17 16:46:51 -04004723 private ArrayList<CharSequence> mTexts = new ArrayList<CharSequence>(5);
4724
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004725 public InboxStyle() {
4726 }
4727
Daniel Sandler879c5e02012-04-17 16:46:51 -04004728 public InboxStyle(Builder builder) {
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004729 setBuilder(builder);
Daniel Sandler879c5e02012-04-17 16:46:51 -04004730 }
4731
Chris Wrend6297db2012-05-03 16:20:13 -04004732 /**
4733 * Overrides ContentTitle in the big form of the template.
4734 * This defaults to the value passed to setContentTitle().
4735 */
4736 public InboxStyle setBigContentTitle(CharSequence title) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04004737 internalSetBigContentTitle(safeCharSequence(title));
Chris Wrend6297db2012-05-03 16:20:13 -04004738 return this;
4739 }
4740
4741 /**
4742 * Set the first line of text after the detail section in the big form of the template.
4743 */
4744 public InboxStyle setSummaryText(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04004745 internalSetSummaryText(safeCharSequence(cs));
Chris Wrend6297db2012-05-03 16:20:13 -04004746 return this;
4747 }
4748
Chris Wren0bd664d2012-08-01 13:56:56 -04004749 /**
4750 * Append a line to the digest section of the Inbox notification.
4751 */
Daniel Sandler879c5e02012-04-17 16:46:51 -04004752 public InboxStyle addLine(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04004753 mTexts.add(safeCharSequence(cs));
Daniel Sandler879c5e02012-04-17 16:46:51 -04004754 return this;
4755 }
4756
Daniel Sandlerf45564e2013-04-15 15:05:08 -04004757 /**
4758 * @hide
4759 */
4760 public void addExtras(Bundle extras) {
4761 super.addExtras(extras);
Christoph Studer4600f9b2014-07-22 22:44:43 +02004762
Daniel Sandlerf45564e2013-04-15 15:05:08 -04004763 CharSequence[] a = new CharSequence[mTexts.size()];
4764 extras.putCharSequenceArray(EXTRA_TEXT_LINES, mTexts.toArray(a));
4765 }
4766
Christoph Studer4600f9b2014-07-22 22:44:43 +02004767 /**
4768 * @hide
4769 */
4770 @Override
4771 protected void restoreFromExtras(Bundle extras) {
4772 super.restoreFromExtras(extras);
4773
4774 mTexts.clear();
4775 if (extras.containsKey(EXTRA_TEXT_LINES)) {
4776 Collections.addAll(mTexts, extras.getCharSequenceArray(EXTRA_TEXT_LINES));
4777 }
4778 }
4779
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004780 /**
4781 * @hide
4782 */
4783 public RemoteViews makeBigContentView() {
Selim Cinekc848c3a2016-01-13 15:27:30 -08004784 // Remove the content text so it disappears unless you have a summary
Christoph Studer4600f9b2014-07-22 22:44:43 +02004785 // Nasty
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004786 CharSequence oldBuilderContentText = mBuilder.mN.extras.getCharSequence(EXTRA_TEXT);
4787 mBuilder.getAllExtras().putCharSequence(EXTRA_TEXT, null);
Christoph Studer4600f9b2014-07-22 22:44:43 +02004788
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01004789 RemoteViews contentView = getStandardView(mBuilder.getInboxLayoutResource());
Daniel Sandler619738c2012-06-07 16:33:08 -04004790
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004791 mBuilder.getAllExtras().putCharSequence(EXTRA_TEXT, oldBuilderContentText);
Christoph Studer4600f9b2014-07-22 22:44:43 +02004792
Chris Wrend6297db2012-05-03 16:20:13 -04004793 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 -04004794 R.id.inbox_text4, R.id.inbox_text5, R.id.inbox_text6};
Chris Wrend6297db2012-05-03 16:20:13 -04004795
Chris Wren4ed80d52012-05-17 09:30:03 -04004796 // Make sure all rows are gone in case we reuse a view.
4797 for (int rowId : rowIds) {
4798 contentView.setViewVisibility(rowId, View.GONE);
4799 }
4800
Jorim Jaggi445d3c02014-08-19 22:33:42 +02004801 final boolean largeText =
4802 mBuilder.mContext.getResources().getConfiguration().fontScale > 1f;
4803 final float subTextSize = mBuilder.mContext.getResources().getDimensionPixelSize(
4804 R.dimen.notification_subtext_size);
Daniel Sandler879c5e02012-04-17 16:46:51 -04004805 int i=0;
Selim Cinek9d9fc6e2015-11-12 15:49:14 -05004806 final float density = mBuilder.mContext.getResources().getDisplayMetrics().density;
4807 int topPadding = (int) (5 * density);
Selim Cinekc0fac722016-03-07 19:57:06 -08004808 int bottomPadding = mBuilder.mContext.getResources().getDimensionPixelSize(
4809 com.android.internal.R.dimen.notification_content_margin_bottom);
Selim Cinek247fa012016-02-18 09:50:48 -08004810 boolean first = true;
Daniel Sandler879c5e02012-04-17 16:46:51 -04004811 while (i < mTexts.size() && i < rowIds.length) {
4812 CharSequence str = mTexts.get(i);
4813 if (str != null && !str.equals("")) {
4814 contentView.setViewVisibility(rowIds[i], View.VISIBLE);
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01004815 contentView.setTextViewText(rowIds[i], mBuilder.processLegacyText(str));
Jorim Jaggi445d3c02014-08-19 22:33:42 +02004816 if (largeText) {
4817 contentView.setTextViewTextSize(rowIds[i], TypedValue.COMPLEX_UNIT_PX,
4818 subTextSize);
4819 }
Selim Cinek9d9fc6e2015-11-12 15:49:14 -05004820 contentView.setViewPadding(rowIds[i], 0, topPadding, 0,
4821 i == rowIds.length - 1 || i == mTexts.size() - 1 ? bottomPadding : 0);
Selim Cinek247fa012016-02-18 09:50:48 -08004822 handleInboxImageMargin(contentView, rowIds[i], first);
4823 first = false;
Daniel Sandler879c5e02012-04-17 16:46:51 -04004824 }
4825 i++;
4826 }
Kenny Guy98193ea2014-07-24 19:54:37 +01004827
Selim Cinek1e0bf612015-11-20 15:57:26 -08004828
Daniel Sandler879c5e02012-04-17 16:46:51 -04004829 return contentView;
4830 }
Selim Cinek1e0bf612015-11-20 15:57:26 -08004831
Selim Cinek247fa012016-02-18 09:50:48 -08004832 private void handleInboxImageMargin(RemoteViews contentView, int id, boolean first) {
Selim Cinek1e0bf612015-11-20 15:57:26 -08004833 int endMargin = 0;
Selim Cinek247fa012016-02-18 09:50:48 -08004834 if (first) {
4835 final int max = mBuilder.mN.extras.getInt(EXTRA_PROGRESS_MAX, 0);
4836 final boolean ind = mBuilder.mN.extras.getBoolean(EXTRA_PROGRESS_INDETERMINATE);
4837 boolean hasProgress = max != 0 || ind;
4838 if (mBuilder.mN.mLargeIcon != null && !hasProgress) {
4839 endMargin = mBuilder.mContext.getResources().getDimensionPixelSize(
4840 R.dimen.notification_content_picture_margin);
4841 }
Selim Cinek1e0bf612015-11-20 15:57:26 -08004842 }
4843 contentView.setViewLayoutMarginEnd(id, endMargin);
4844 }
Daniel Sandler879c5e02012-04-17 16:46:51 -04004845 }
Dan Sandler842dd772014-05-15 09:36:47 -04004846
4847 /**
4848 * Notification style for media playback notifications.
4849 *
4850 * In the expanded form, {@link Notification#bigContentView}, up to 5
4851 * {@link Notification.Action}s specified with
Dan Sandler86647982015-05-13 23:41:13 -04004852 * {@link Notification.Builder#addAction(Action) addAction} will be
Dan Sandler842dd772014-05-15 09:36:47 -04004853 * shown as icon-only pushbuttons, suitable for transport controls. The Bitmap given to
4854 * {@link Notification.Builder#setLargeIcon(android.graphics.Bitmap) setLargeIcon()} will be
4855 * treated as album artwork.
4856 *
4857 * Unlike the other styles provided here, MediaStyle can also modify the standard-size
4858 * {@link Notification#contentView}; by providing action indices to
Christoph Studerfde6f4d2014-12-12 13:23:26 +01004859 * {@link #setShowActionsInCompactView(int...)} you can promote up to 3 actions to be displayed
Dan Sandler842dd772014-05-15 09:36:47 -04004860 * in the standard view alongside the usual content.
4861 *
Bryan Mawhinney6be8de32014-07-18 10:35:12 +01004862 * Notifications created with MediaStyle will have their category set to
4863 * {@link Notification#CATEGORY_TRANSPORT CATEGORY_TRANSPORT} unless you set a different
4864 * category using {@link Notification.Builder#setCategory(String) setCategory()}.
4865 *
Jeff Browndba34ba2014-06-24 20:46:03 -07004866 * Finally, if you attach a {@link android.media.session.MediaSession.Token} using
4867 * {@link android.app.Notification.MediaStyle#setMediaSession(MediaSession.Token)},
Dan Sandler842dd772014-05-15 09:36:47 -04004868 * the System UI can identify this as a notification representing an active media session
4869 * and respond accordingly (by showing album artwork in the lockscreen, for example).
4870 *
4871 * To use this style with your Notification, feed it to
4872 * {@link Notification.Builder#setStyle(android.app.Notification.Style)} like so:
4873 * <pre class="prettyprint">
4874 * Notification noti = new Notification.Builder()
4875 * .setSmallIcon(R.drawable.ic_stat_player)
Christoph Studere935fe92014-11-24 14:18:06 +01004876 * .setContentTitle(&quot;Track title&quot;)
4877 * .setContentText(&quot;Artist - Album&quot;)
4878 * .setLargeIcon(albumArtBitmap))
Bryan Mawhinney6be8de32014-07-18 10:35:12 +01004879 * .setStyle(<b>new Notification.MediaStyle()</b>
4880 * .setMediaSession(mySession))
Dan Sandler842dd772014-05-15 09:36:47 -04004881 * .build();
4882 * </pre>
4883 *
4884 * @see Notification#bigContentView
4885 */
4886 public static class MediaStyle extends Style {
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02004887 static final int MAX_MEDIA_BUTTONS_IN_COMPACT = 3;
Dan Sandler842dd772014-05-15 09:36:47 -04004888 static final int MAX_MEDIA_BUTTONS = 5;
4889
4890 private int[] mActionsToShowInCompact = null;
Jeff Browndba34ba2014-06-24 20:46:03 -07004891 private MediaSession.Token mToken;
Dan Sandler842dd772014-05-15 09:36:47 -04004892
4893 public MediaStyle() {
4894 }
4895
4896 public MediaStyle(Builder builder) {
4897 setBuilder(builder);
4898 }
4899
4900 /**
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02004901 * 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 -04004902 * notification view.
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02004903 *
4904 * @param actions the indices of the actions to show in the compact notification view
Dan Sandler842dd772014-05-15 09:36:47 -04004905 */
4906 public MediaStyle setShowActionsInCompactView(int...actions) {
4907 mActionsToShowInCompact = actions;
4908 return this;
4909 }
4910
4911 /**
Jeff Browndba34ba2014-06-24 20:46:03 -07004912 * Attach a {@link android.media.session.MediaSession.Token} to this Notification
4913 * to provide additional playback information and control to the SystemUI.
Dan Sandler842dd772014-05-15 09:36:47 -04004914 */
Jeff Browndba34ba2014-06-24 20:46:03 -07004915 public MediaStyle setMediaSession(MediaSession.Token token) {
Dan Sandler842dd772014-05-15 09:36:47 -04004916 mToken = token;
4917 return this;
4918 }
4919
Christoph Studer4600f9b2014-07-22 22:44:43 +02004920 /**
4921 * @hide
4922 */
Dan Sandler842dd772014-05-15 09:36:47 -04004923 @Override
4924 public Notification buildStyled(Notification wip) {
Christoph Studer4600f9b2014-07-22 22:44:43 +02004925 super.buildStyled(wip);
Bryan Mawhinney6be8de32014-07-18 10:35:12 +01004926 if (wip.category == null) {
4927 wip.category = Notification.CATEGORY_TRANSPORT;
4928 }
Dan Sandler842dd772014-05-15 09:36:47 -04004929 return wip;
4930 }
4931
Christoph Studer4600f9b2014-07-22 22:44:43 +02004932 /**
4933 * @hide
4934 */
4935 @Override
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004936 public RemoteViews makeContentView() {
4937 return makeMediaContentView();
Christoph Studer4600f9b2014-07-22 22:44:43 +02004938 }
4939
4940 /**
4941 * @hide
4942 */
4943 @Override
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004944 public RemoteViews makeBigContentView() {
4945 return makeMediaBigContentView();
Christoph Studer4600f9b2014-07-22 22:44:43 +02004946 }
4947
Selim Cinekcc10bfb2016-02-10 16:24:21 -08004948 /**
4949 * @hide
4950 */
4951 @Override
4952 public RemoteViews makeHeadsUpContentView() {
4953 RemoteViews expanded = makeMediaBigContentView();
4954 return expanded != null ? expanded : makeMediaContentView();
4955 }
4956
Dan Sandler842dd772014-05-15 09:36:47 -04004957 /** @hide */
4958 @Override
4959 public void addExtras(Bundle extras) {
4960 super.addExtras(extras);
4961
4962 if (mToken != null) {
4963 extras.putParcelable(EXTRA_MEDIA_SESSION, mToken);
4964 }
Bryan Mawhinneye191f902014-07-22 12:50:09 +01004965 if (mActionsToShowInCompact != null) {
4966 extras.putIntArray(EXTRA_COMPACT_ACTIONS, mActionsToShowInCompact);
4967 }
Dan Sandler842dd772014-05-15 09:36:47 -04004968 }
4969
Christoph Studer4600f9b2014-07-22 22:44:43 +02004970 /**
4971 * @hide
4972 */
4973 @Override
4974 protected void restoreFromExtras(Bundle extras) {
4975 super.restoreFromExtras(extras);
4976
4977 if (extras.containsKey(EXTRA_MEDIA_SESSION)) {
4978 mToken = extras.getParcelable(EXTRA_MEDIA_SESSION);
4979 }
4980 if (extras.containsKey(EXTRA_COMPACT_ACTIONS)) {
4981 mActionsToShowInCompact = extras.getIntArray(EXTRA_COMPACT_ACTIONS);
4982 }
4983 }
4984
Selim Cinek5bf069a2015-11-10 19:14:27 -05004985 private RemoteViews generateMediaActionButton(Action action, int color) {
Dan Sandler842dd772014-05-15 09:36:47 -04004986 final boolean tombstone = (action.actionIntent == null);
Selim Cinekf33b1112015-07-15 17:45:11 -07004987 RemoteViews button = new BuilderRemoteViews(mBuilder.mContext.getApplicationInfo(),
Alan Viverette3cb07a462014-06-06 14:19:53 -07004988 R.layout.notification_material_media_action);
Dan Sandler68079d52015-07-22 10:45:30 -04004989 button.setImageViewIcon(R.id.action0, action.getIcon());
Selim Cinek5bf069a2015-11-10 19:14:27 -05004990 button.setDrawableParameters(R.id.action0, false, -1, color, PorterDuff.Mode.SRC_ATOP,
4991 -1);
Dan Sandler842dd772014-05-15 09:36:47 -04004992 if (!tombstone) {
4993 button.setOnClickPendingIntent(R.id.action0, action.actionIntent);
4994 }
4995 button.setContentDescription(R.id.action0, action.title);
4996 return button;
4997 }
4998
4999 private RemoteViews makeMediaContentView() {
5000 RemoteViews view = mBuilder.applyStandardTemplate(
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02005001 R.layout.notification_template_material_media, false /* hasProgress */);
Dan Sandler842dd772014-05-15 09:36:47 -04005002
5003 final int numActions = mBuilder.mActions.size();
5004 final int N = mActionsToShowInCompact == null
5005 ? 0
5006 : Math.min(mActionsToShowInCompact.length, MAX_MEDIA_BUTTONS_IN_COMPACT);
5007 if (N > 0) {
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02005008 view.removeAllViews(com.android.internal.R.id.media_actions);
Dan Sandler842dd772014-05-15 09:36:47 -04005009 for (int i = 0; i < N; i++) {
5010 if (i >= numActions) {
5011 throw new IllegalArgumentException(String.format(
5012 "setShowActionsInCompactView: action %d out of bounds (max %d)",
5013 i, numActions - 1));
5014 }
5015
5016 final Action action = mBuilder.mActions.get(mActionsToShowInCompact[i]);
Selim Cinek5bf069a2015-11-10 19:14:27 -05005017 final RemoteViews button = generateMediaActionButton(action,
Adrian Roos4ff3b122016-02-01 12:26:13 -08005018 mBuilder.resolveContrastColor());
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02005019 view.addView(com.android.internal.R.id.media_actions, button);
Dan Sandler842dd772014-05-15 09:36:47 -04005020 }
5021 }
Selim Cinekfdc738f2016-01-27 20:04:27 -08005022 handleImage(view);
5023 // handle the content margin
Selim Cinek247fa012016-02-18 09:50:48 -08005024 int endMargin = mBuilder.mContext.getResources().getDimensionPixelSize(
5025 R.dimen.notification_content_margin_end);;
Selim Cinekfdc738f2016-01-27 20:04:27 -08005026 if (mBuilder.mN.mLargeIcon != null) {
Selim Cinek247fa012016-02-18 09:50:48 -08005027 endMargin += mBuilder.mContext.getResources().getDimensionPixelSize(
5028 R.dimen.notification_content_picture_margin);
Selim Cinekfdc738f2016-01-27 20:04:27 -08005029 }
5030 view.setViewLayoutMarginEnd(R.id.notification_main_column, endMargin);
Dan Sandler842dd772014-05-15 09:36:47 -04005031 return view;
5032 }
5033
5034 private RemoteViews makeMediaBigContentView() {
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02005035 final int actionCount = Math.min(mBuilder.mActions.size(), MAX_MEDIA_BUTTONS);
Selim Cinekcc10bfb2016-02-10 16:24:21 -08005036 // Dont add an expanded view if there is no more content to be revealed
5037 int actionsInCompact = mActionsToShowInCompact == null
5038 ? 0
5039 : Math.min(mActionsToShowInCompact.length, MAX_MEDIA_BUTTONS_IN_COMPACT);
5040 if (mBuilder.mN.mLargeIcon == null && actionCount <= actionsInCompact) {
5041 return null;
5042 }
Selim Cinek5bf069a2015-11-10 19:14:27 -05005043 RemoteViews big = mBuilder.applyStandardTemplate(
5044 R.layout.notification_template_material_big_media,
5045 false);
Dan Sandler842dd772014-05-15 09:36:47 -04005046
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02005047 if (actionCount > 0) {
5048 big.removeAllViews(com.android.internal.R.id.media_actions);
5049 for (int i = 0; i < actionCount; i++) {
Selim Cinek5bf069a2015-11-10 19:14:27 -05005050 final RemoteViews button = generateMediaActionButton(mBuilder.mActions.get(i),
Adrian Roos4ff3b122016-02-01 12:26:13 -08005051 mBuilder.resolveContrastColor());
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02005052 big.addView(com.android.internal.R.id.media_actions, button);
Dan Sandler842dd772014-05-15 09:36:47 -04005053 }
5054 }
Selim Cinek5bf069a2015-11-10 19:14:27 -05005055 handleImage(big);
Dan Sandler842dd772014-05-15 09:36:47 -04005056 return big;
5057 }
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02005058
Selim Cinek5bf069a2015-11-10 19:14:27 -05005059 private void handleImage(RemoteViews contentView) {
5060 if (mBuilder.mN.mLargeIcon != null) {
5061 contentView.setViewLayoutMarginEnd(R.id.line1, 0);
Selim Cinekc848c3a2016-01-13 15:27:30 -08005062 contentView.setViewLayoutMarginEnd(R.id.text, 0);
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02005063 }
5064 }
5065
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02005066 /**
5067 * @hide
5068 */
5069 @Override
5070 protected boolean hasProgress() {
5071 return false;
5072 }
Dan Sandler842dd772014-05-15 09:36:47 -04005073 }
Griff Hazen61a9e862014-05-22 16:05:19 -07005074
Selim Cinek593610c2016-02-16 18:42:57 -08005075 /**
5076 * Notification style for custom views that are decorated by the system
5077 *
5078 * <p>Instead of providing a notification that is completely custom, a developer can set this
5079 * style and still obtain system decorations like the notification header with the expand
5080 * affordance and actions.
5081 *
5082 * <p>Use {@link android.app.Notification.Builder#setCustomContentView(RemoteViews)},
5083 * {@link android.app.Notification.Builder#setCustomBigContentView(RemoteViews)} and
5084 * {@link android.app.Notification.Builder#setCustomHeadsUpContentView(RemoteViews)} to set the
5085 * corresponding custom views to display.
5086 *
5087 * To use this style with your Notification, feed it to
5088 * {@link Notification.Builder#setStyle(android.app.Notification.Style)} like so:
5089 * <pre class="prettyprint">
5090 * Notification noti = new Notification.Builder()
5091 * .setSmallIcon(R.drawable.ic_stat_player)
5092 * .setLargeIcon(albumArtBitmap))
5093 * .setCustomContentView(contentView);
5094 * .setStyle(<b>new Notification.DecoratedCustomViewStyle()</b>)
5095 * .build();
5096 * </pre>
5097 */
5098 public static class DecoratedCustomViewStyle extends Style {
5099
5100 public DecoratedCustomViewStyle() {
5101 }
5102
5103 public DecoratedCustomViewStyle(Builder builder) {
5104 setBuilder(builder);
5105 }
5106
5107 /**
5108 * @hide
5109 */
5110 public boolean displayCustomViewInline() {
5111 return true;
5112 }
5113
5114 /**
5115 * @hide
5116 */
5117 @Override
5118 public RemoteViews makeContentView() {
5119 return makeStandardTemplateWithCustomContent(mBuilder.mN.contentView);
5120 }
5121
5122 /**
5123 * @hide
5124 */
5125 @Override
5126 public RemoteViews makeBigContentView() {
5127 return makeDecoratedBigContentView();
5128 }
5129
5130 /**
5131 * @hide
5132 */
5133 @Override
5134 public RemoteViews makeHeadsUpContentView() {
5135 return makeDecoratedHeadsUpContentView();
5136 }
5137
Selim Cinek593610c2016-02-16 18:42:57 -08005138 private RemoteViews makeDecoratedHeadsUpContentView() {
5139 RemoteViews headsUpContentView = mBuilder.mN.headsUpContentView == null
5140 ? mBuilder.mN.contentView
5141 : mBuilder.mN.headsUpContentView;
5142 if (mBuilder.mActions.size() == 0) {
5143 return makeStandardTemplateWithCustomContent(headsUpContentView);
5144 }
5145 RemoteViews remoteViews = mBuilder.applyStandardTemplateWithActions(
5146 mBuilder.getBigBaseLayoutResource());
Selim Cinek247fa012016-02-18 09:50:48 -08005147 buildIntoRemoteViewContent(remoteViews, headsUpContentView);
Selim Cinek593610c2016-02-16 18:42:57 -08005148 return remoteViews;
5149 }
5150
Selim Cinek593610c2016-02-16 18:42:57 -08005151 private RemoteViews makeStandardTemplateWithCustomContent(RemoteViews customContent) {
5152 RemoteViews remoteViews = mBuilder.applyStandardTemplate(
5153 mBuilder.getBaseLayoutResource());
Selim Cinek247fa012016-02-18 09:50:48 -08005154 buildIntoRemoteViewContent(remoteViews, customContent);
Selim Cinek593610c2016-02-16 18:42:57 -08005155 return remoteViews;
5156 }
5157
Selim Cinek593610c2016-02-16 18:42:57 -08005158 private RemoteViews makeDecoratedBigContentView() {
5159 RemoteViews bigContentView = mBuilder.mN.bigContentView == null
5160 ? mBuilder.mN.contentView
5161 : mBuilder.mN.bigContentView;
5162 if (mBuilder.mActions.size() == 0) {
5163 return makeStandardTemplateWithCustomContent(bigContentView);
5164 }
5165 RemoteViews remoteViews = mBuilder.applyStandardTemplateWithActions(
5166 mBuilder.getBigBaseLayoutResource());
Selim Cinek247fa012016-02-18 09:50:48 -08005167 buildIntoRemoteViewContent(remoteViews, bigContentView);
Selim Cinek593610c2016-02-16 18:42:57 -08005168 return remoteViews;
5169 }
Selim Cinek247fa012016-02-18 09:50:48 -08005170
5171 private void buildIntoRemoteViewContent(RemoteViews remoteViews,
5172 RemoteViews customContent) {
Adrian Roos5081c0d2016-02-26 16:04:19 -08005173 if (customContent != null) {
Selim Cinekf91017e2016-03-14 12:25:09 -07005174 // Need to clone customContent before adding, because otherwise it can no longer be
5175 // parceled independently of remoteViews.
Adrian Roos5081c0d2016-02-26 16:04:19 -08005176 customContent = customContent.clone();
Selim Cinekf91017e2016-03-14 12:25:09 -07005177 remoteViews.removeAllViews(R.id.notification_main_column);
5178 remoteViews.addView(R.id.notification_main_column, customContent);
Adrian Roos5081c0d2016-02-26 16:04:19 -08005179 }
Selim Cinek247fa012016-02-18 09:50:48 -08005180 // also update the end margin if there is an image
5181 int endMargin = mBuilder.mContext.getResources().getDimensionPixelSize(
5182 R.dimen.notification_content_margin_end);
5183 if (mBuilder.mN.mLargeIcon != null) {
5184 endMargin += mBuilder.mContext.getResources().getDimensionPixelSize(
5185 R.dimen.notification_content_picture_margin);
5186 }
5187 remoteViews.setViewLayoutMarginEnd(R.id.notification_main_column, endMargin);
5188 }
Selim Cinek593610c2016-02-16 18:42:57 -08005189 }
5190
Selim Cinek03eb3b72016-02-18 10:39:45 -08005191 /**
5192 * Notification style for media custom views that are decorated by the system
5193 *
5194 * <p>Instead of providing a media notification that is completely custom, a developer can set
5195 * this style and still obtain system decorations like the notification header with the expand
5196 * affordance and actions.
5197 *
5198 * <p>Use {@link android.app.Notification.Builder#setCustomContentView(RemoteViews)},
5199 * {@link android.app.Notification.Builder#setCustomBigContentView(RemoteViews)} and
5200 * {@link android.app.Notification.Builder#setCustomHeadsUpContentView(RemoteViews)} to set the
5201 * corresponding custom views to display.
5202 *
5203 * To use this style with your Notification, feed it to
5204 * {@link Notification.Builder#setStyle(android.app.Notification.Style)} like so:
5205 * <pre class="prettyprint">
5206 * Notification noti = new Notification.Builder()
5207 * .setSmallIcon(R.drawable.ic_stat_player)
5208 * .setLargeIcon(albumArtBitmap))
5209 * .setCustomContentView(contentView);
5210 * .setStyle(<b>new Notification.DecoratedMediaCustomViewStyle()</b>
5211 * .setMediaSession(mySession))
5212 * .build();
5213 * </pre>
5214 *
5215 * @see android.app.Notification.DecoratedCustomViewStyle
5216 * @see android.app.Notification.MediaStyle
5217 */
5218 public static class DecoratedMediaCustomViewStyle extends MediaStyle {
5219
5220 public DecoratedMediaCustomViewStyle() {
5221 }
5222
5223 public DecoratedMediaCustomViewStyle(Builder builder) {
5224 setBuilder(builder);
5225 }
5226
5227 /**
5228 * @hide
5229 */
5230 public boolean displayCustomViewInline() {
5231 return true;
5232 }
5233
5234 /**
5235 * @hide
5236 */
5237 @Override
5238 public RemoteViews makeContentView() {
5239 RemoteViews remoteViews = super.makeContentView();
5240 return buildIntoRemoteView(remoteViews, R.id.notification_content_container,
5241 mBuilder.mN.contentView);
5242 }
5243
5244 /**
5245 * @hide
5246 */
5247 @Override
5248 public RemoteViews makeBigContentView() {
5249 RemoteViews customRemoteView = mBuilder.mN.bigContentView != null
5250 ? mBuilder.mN.bigContentView
5251 : mBuilder.mN.contentView;
5252 return makeBigContentViewWithCustomContent(customRemoteView);
5253 }
5254
5255 private RemoteViews makeBigContentViewWithCustomContent(RemoteViews customRemoteView) {
5256 RemoteViews remoteViews = super.makeBigContentView();
5257 if (remoteViews != null) {
5258 return buildIntoRemoteView(remoteViews, R.id.notification_main_column,
5259 customRemoteView);
5260 } else if (customRemoteView != mBuilder.mN.contentView){
5261 remoteViews = super.makeContentView();
5262 return buildIntoRemoteView(remoteViews, R.id.notification_content_container,
5263 customRemoteView);
5264 } else {
5265 return null;
5266 }
5267 }
5268
5269 /**
5270 * @hide
5271 */
5272 @Override
5273 public RemoteViews makeHeadsUpContentView() {
5274 RemoteViews customRemoteView = mBuilder.mN.headsUpContentView != null
5275 ? mBuilder.mN.headsUpContentView
5276 : mBuilder.mN.contentView;
5277 return makeBigContentViewWithCustomContent(customRemoteView);
5278 }
5279
5280 private RemoteViews buildIntoRemoteView(RemoteViews remoteViews, int id,
5281 RemoteViews customContent) {
Adrian Roos5081c0d2016-02-26 16:04:19 -08005282 if (customContent != null) {
Selim Cinekf91017e2016-03-14 12:25:09 -07005283 // Need to clone customContent before adding, because otherwise it can no longer be
5284 // parceled independently of remoteViews.
Adrian Roos5081c0d2016-02-26 16:04:19 -08005285 customContent = customContent.clone();
Selim Cinekf91017e2016-03-14 12:25:09 -07005286 remoteViews.removeAllViews(id);
5287 remoteViews.addView(id, customContent);
Adrian Roos5081c0d2016-02-26 16:04:19 -08005288 }
Selim Cinek03eb3b72016-02-18 10:39:45 -08005289 return remoteViews;
5290 }
5291 }
5292
Christoph Studer4600f9b2014-07-22 22:44:43 +02005293 // When adding a new Style subclass here, don't forget to update
5294 // Builder.getNotificationStyleClass.
5295
Griff Hazen61a9e862014-05-22 16:05:19 -07005296 /**
5297 * Extender interface for use with {@link Builder#extend}. Extenders may be used to add
5298 * metadata or change options on a notification builder.
5299 */
5300 public interface Extender {
5301 /**
5302 * Apply this extender to a notification builder.
5303 * @param builder the builder to be modified.
5304 * @return the build object for chaining.
5305 */
5306 public Builder extend(Builder builder);
5307 }
5308
5309 /**
5310 * Helper class to add wearable extensions to notifications.
5311 * <p class="note"> See
5312 * <a href="{@docRoot}wear/notifications/creating.html">Creating Notifications
5313 * for Android Wear</a> for more information on how to use this class.
5314 * <p>
5315 * To create a notification with wearable extensions:
5316 * <ol>
5317 * <li>Create a {@link android.app.Notification.Builder}, setting any desired
5318 * properties.
5319 * <li>Create a {@link android.app.Notification.WearableExtender}.
5320 * <li>Set wearable-specific properties using the
5321 * {@code add} and {@code set} methods of {@link android.app.Notification.WearableExtender}.
5322 * <li>Call {@link android.app.Notification.Builder#extend} to apply the extensions to a
5323 * notification.
5324 * <li>Post the notification to the notification system with the
5325 * {@code NotificationManager.notify(...)} methods.
5326 * </ol>
5327 *
5328 * <pre class="prettyprint">
5329 * Notification notif = new Notification.Builder(mContext)
5330 * .setContentTitle(&quot;New mail from &quot; + sender.toString())
5331 * .setContentText(subject)
5332 * .setSmallIcon(R.drawable.new_mail)
5333 * .extend(new Notification.WearableExtender()
5334 * .setContentIcon(R.drawable.new_mail))
5335 * .build();
5336 * NotificationManager notificationManger =
5337 * (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
5338 * notificationManger.notify(0, notif);</pre>
5339 *
5340 * <p>Wearable extensions can be accessed on an existing notification by using the
5341 * {@code WearableExtender(Notification)} constructor,
5342 * and then using the {@code get} methods to access values.
5343 *
5344 * <pre class="prettyprint">
5345 * Notification.WearableExtender wearableExtender = new Notification.WearableExtender(
5346 * notification);
Griff Hazen14f57992014-05-26 09:07:14 -07005347 * List&lt;Notification&gt; pages = wearableExtender.getPages();</pre>
Griff Hazen61a9e862014-05-22 16:05:19 -07005348 */
5349 public static final class WearableExtender implements Extender {
5350 /**
5351 * Sentinel value for an action index that is unset.
5352 */
5353 public static final int UNSET_ACTION_INDEX = -1;
5354
5355 /**
5356 * Size value for use with {@link #setCustomSizePreset} to show this notification with
5357 * default sizing.
5358 * <p>For custom display notifications created using {@link #setDisplayIntent},
Paul Soulosaa4f4bf2015-08-04 11:59:45 -07005359 * the default is {@link #SIZE_MEDIUM}. All other notifications size automatically based
Griff Hazen61a9e862014-05-22 16:05:19 -07005360 * on their content.
5361 */
5362 public static final int SIZE_DEFAULT = 0;
5363
5364 /**
5365 * Size value for use with {@link #setCustomSizePreset} to show this notification
5366 * with an extra small size.
5367 * <p>This value is only applicable for custom display notifications created using
5368 * {@link #setDisplayIntent}.
5369 */
5370 public static final int SIZE_XSMALL = 1;
5371
5372 /**
5373 * Size value for use with {@link #setCustomSizePreset} to show this notification
5374 * with a small size.
5375 * <p>This value is only applicable for custom display notifications created using
5376 * {@link #setDisplayIntent}.
5377 */
5378 public static final int SIZE_SMALL = 2;
5379
5380 /**
5381 * Size value for use with {@link #setCustomSizePreset} to show this notification
5382 * with a medium size.
5383 * <p>This value is only applicable for custom display notifications created using
5384 * {@link #setDisplayIntent}.
5385 */
5386 public static final int SIZE_MEDIUM = 3;
5387
5388 /**
5389 * Size value for use with {@link #setCustomSizePreset} to show this notification
5390 * with a large size.
5391 * <p>This value is only applicable for custom display notifications created using
5392 * {@link #setDisplayIntent}.
5393 */
5394 public static final int SIZE_LARGE = 4;
5395
Griff Hazend5f11f92014-05-27 15:40:09 -07005396 /**
5397 * Size value for use with {@link #setCustomSizePreset} to show this notification
5398 * full screen.
5399 * <p>This value is only applicable for custom display notifications created using
5400 * {@link #setDisplayIntent}.
5401 */
5402 public static final int SIZE_FULL_SCREEN = 5;
5403
Griff Hazen5f2edfc2014-09-29 16:28:44 -07005404 /**
5405 * Sentinel value for use with {@link #setHintScreenTimeout} to keep the screen on for a
5406 * short amount of time when this notification is displayed on the screen. This
5407 * is the default value.
5408 */
5409 public static final int SCREEN_TIMEOUT_SHORT = 0;
5410
5411 /**
5412 * Sentinel value for use with {@link #setHintScreenTimeout} to keep the screen on
5413 * for a longer amount of time when this notification is displayed on the screen.
5414 */
5415 public static final int SCREEN_TIMEOUT_LONG = -1;
5416
Griff Hazen61a9e862014-05-22 16:05:19 -07005417 /** Notification extra which contains wearable extensions */
5418 private static final String EXTRA_WEARABLE_EXTENSIONS = "android.wearable.EXTENSIONS";
5419
Pete Gastaf6781d2014-10-07 15:17:05 -04005420 // Keys within EXTRA_WEARABLE_EXTENSIONS for wearable options.
Griff Hazen61a9e862014-05-22 16:05:19 -07005421 private static final String KEY_ACTIONS = "actions";
5422 private static final String KEY_FLAGS = "flags";
5423 private static final String KEY_DISPLAY_INTENT = "displayIntent";
5424 private static final String KEY_PAGES = "pages";
5425 private static final String KEY_BACKGROUND = "background";
5426 private static final String KEY_CONTENT_ICON = "contentIcon";
5427 private static final String KEY_CONTENT_ICON_GRAVITY = "contentIconGravity";
5428 private static final String KEY_CONTENT_ACTION_INDEX = "contentActionIndex";
5429 private static final String KEY_CUSTOM_SIZE_PRESET = "customSizePreset";
5430 private static final String KEY_CUSTOM_CONTENT_HEIGHT = "customContentHeight";
5431 private static final String KEY_GRAVITY = "gravity";
Griff Hazen5f2edfc2014-09-29 16:28:44 -07005432 private static final String KEY_HINT_SCREEN_TIMEOUT = "hintScreenTimeout";
Griff Hazen61a9e862014-05-22 16:05:19 -07005433
5434 // Flags bitwise-ored to mFlags
5435 private static final int FLAG_CONTENT_INTENT_AVAILABLE_OFFLINE = 0x1;
5436 private static final int FLAG_HINT_HIDE_ICON = 1 << 1;
5437 private static final int FLAG_HINT_SHOW_BACKGROUND_ONLY = 1 << 2;
5438 private static final int FLAG_START_SCROLL_BOTTOM = 1 << 3;
Griff Hazen5f2edfc2014-09-29 16:28:44 -07005439 private static final int FLAG_HINT_AVOID_BACKGROUND_CLIPPING = 1 << 4;
Alex Hills9ab3a232016-04-05 14:54:56 -04005440 private static final int FLAG_HINT_CONTENT_INTENT_LAUNCHES_ACTIVITY = 1 << 6;
Griff Hazen61a9e862014-05-22 16:05:19 -07005441
5442 // Default value for flags integer
5443 private static final int DEFAULT_FLAGS = FLAG_CONTENT_INTENT_AVAILABLE_OFFLINE;
5444
5445 private static final int DEFAULT_CONTENT_ICON_GRAVITY = Gravity.END;
5446 private static final int DEFAULT_GRAVITY = Gravity.BOTTOM;
5447
5448 private ArrayList<Action> mActions = new ArrayList<Action>();
5449 private int mFlags = DEFAULT_FLAGS;
5450 private PendingIntent mDisplayIntent;
5451 private ArrayList<Notification> mPages = new ArrayList<Notification>();
5452 private Bitmap mBackground;
5453 private int mContentIcon;
5454 private int mContentIconGravity = DEFAULT_CONTENT_ICON_GRAVITY;
5455 private int mContentActionIndex = UNSET_ACTION_INDEX;
5456 private int mCustomSizePreset = SIZE_DEFAULT;
5457 private int mCustomContentHeight;
5458 private int mGravity = DEFAULT_GRAVITY;
Griff Hazen5f2edfc2014-09-29 16:28:44 -07005459 private int mHintScreenTimeout;
Griff Hazen61a9e862014-05-22 16:05:19 -07005460
5461 /**
5462 * Create a {@link android.app.Notification.WearableExtender} with default
5463 * options.
5464 */
5465 public WearableExtender() {
5466 }
5467
5468 public WearableExtender(Notification notif) {
5469 Bundle wearableBundle = notif.extras.getBundle(EXTRA_WEARABLE_EXTENSIONS);
5470 if (wearableBundle != null) {
5471 List<Action> actions = wearableBundle.getParcelableArrayList(KEY_ACTIONS);
5472 if (actions != null) {
5473 mActions.addAll(actions);
5474 }
5475
5476 mFlags = wearableBundle.getInt(KEY_FLAGS, DEFAULT_FLAGS);
5477 mDisplayIntent = wearableBundle.getParcelable(KEY_DISPLAY_INTENT);
5478
5479 Notification[] pages = getNotificationArrayFromBundle(
5480 wearableBundle, KEY_PAGES);
5481 if (pages != null) {
5482 Collections.addAll(mPages, pages);
5483 }
5484
5485 mBackground = wearableBundle.getParcelable(KEY_BACKGROUND);
5486 mContentIcon = wearableBundle.getInt(KEY_CONTENT_ICON);
5487 mContentIconGravity = wearableBundle.getInt(KEY_CONTENT_ICON_GRAVITY,
5488 DEFAULT_CONTENT_ICON_GRAVITY);
5489 mContentActionIndex = wearableBundle.getInt(KEY_CONTENT_ACTION_INDEX,
5490 UNSET_ACTION_INDEX);
5491 mCustomSizePreset = wearableBundle.getInt(KEY_CUSTOM_SIZE_PRESET,
5492 SIZE_DEFAULT);
5493 mCustomContentHeight = wearableBundle.getInt(KEY_CUSTOM_CONTENT_HEIGHT);
5494 mGravity = wearableBundle.getInt(KEY_GRAVITY, DEFAULT_GRAVITY);
Griff Hazen5f2edfc2014-09-29 16:28:44 -07005495 mHintScreenTimeout = wearableBundle.getInt(KEY_HINT_SCREEN_TIMEOUT);
Griff Hazen61a9e862014-05-22 16:05:19 -07005496 }
5497 }
5498
5499 /**
5500 * Apply wearable extensions to a notification that is being built. This is typically
5501 * called by the {@link android.app.Notification.Builder#extend} method of
5502 * {@link android.app.Notification.Builder}.
5503 */
5504 @Override
5505 public Notification.Builder extend(Notification.Builder builder) {
5506 Bundle wearableBundle = new Bundle();
5507
5508 if (!mActions.isEmpty()) {
5509 wearableBundle.putParcelableArrayList(KEY_ACTIONS, mActions);
5510 }
5511 if (mFlags != DEFAULT_FLAGS) {
5512 wearableBundle.putInt(KEY_FLAGS, mFlags);
5513 }
5514 if (mDisplayIntent != null) {
5515 wearableBundle.putParcelable(KEY_DISPLAY_INTENT, mDisplayIntent);
5516 }
5517 if (!mPages.isEmpty()) {
5518 wearableBundle.putParcelableArray(KEY_PAGES, mPages.toArray(
5519 new Notification[mPages.size()]));
5520 }
5521 if (mBackground != null) {
5522 wearableBundle.putParcelable(KEY_BACKGROUND, mBackground);
5523 }
5524 if (mContentIcon != 0) {
5525 wearableBundle.putInt(KEY_CONTENT_ICON, mContentIcon);
5526 }
5527 if (mContentIconGravity != DEFAULT_CONTENT_ICON_GRAVITY) {
5528 wearableBundle.putInt(KEY_CONTENT_ICON_GRAVITY, mContentIconGravity);
5529 }
5530 if (mContentActionIndex != UNSET_ACTION_INDEX) {
5531 wearableBundle.putInt(KEY_CONTENT_ACTION_INDEX,
5532 mContentActionIndex);
5533 }
5534 if (mCustomSizePreset != SIZE_DEFAULT) {
5535 wearableBundle.putInt(KEY_CUSTOM_SIZE_PRESET, mCustomSizePreset);
5536 }
5537 if (mCustomContentHeight != 0) {
5538 wearableBundle.putInt(KEY_CUSTOM_CONTENT_HEIGHT, mCustomContentHeight);
5539 }
5540 if (mGravity != DEFAULT_GRAVITY) {
5541 wearableBundle.putInt(KEY_GRAVITY, mGravity);
5542 }
Griff Hazen5f2edfc2014-09-29 16:28:44 -07005543 if (mHintScreenTimeout != 0) {
5544 wearableBundle.putInt(KEY_HINT_SCREEN_TIMEOUT, mHintScreenTimeout);
5545 }
Griff Hazen61a9e862014-05-22 16:05:19 -07005546
5547 builder.getExtras().putBundle(EXTRA_WEARABLE_EXTENSIONS, wearableBundle);
5548 return builder;
5549 }
5550
5551 @Override
5552 public WearableExtender clone() {
5553 WearableExtender that = new WearableExtender();
5554 that.mActions = new ArrayList<Action>(this.mActions);
5555 that.mFlags = this.mFlags;
5556 that.mDisplayIntent = this.mDisplayIntent;
5557 that.mPages = new ArrayList<Notification>(this.mPages);
5558 that.mBackground = this.mBackground;
5559 that.mContentIcon = this.mContentIcon;
5560 that.mContentIconGravity = this.mContentIconGravity;
5561 that.mContentActionIndex = this.mContentActionIndex;
5562 that.mCustomSizePreset = this.mCustomSizePreset;
5563 that.mCustomContentHeight = this.mCustomContentHeight;
5564 that.mGravity = this.mGravity;
Griff Hazen5f2edfc2014-09-29 16:28:44 -07005565 that.mHintScreenTimeout = this.mHintScreenTimeout;
Griff Hazen61a9e862014-05-22 16:05:19 -07005566 return that;
5567 }
5568
5569 /**
5570 * Add a wearable action to this notification.
5571 *
5572 * <p>When wearable actions are added using this method, the set of actions that
5573 * show on a wearable device splits from devices that only show actions added
5574 * using {@link android.app.Notification.Builder#addAction}. This allows for customization
5575 * of which actions display on different devices.
5576 *
5577 * @param action the action to add to this notification
5578 * @return this object for method chaining
5579 * @see android.app.Notification.Action
5580 */
5581 public WearableExtender addAction(Action action) {
5582 mActions.add(action);
5583 return this;
5584 }
5585
5586 /**
5587 * Adds wearable actions to this notification.
5588 *
5589 * <p>When wearable actions are added using this method, the set of actions that
5590 * show on a wearable device splits from devices that only show actions added
5591 * using {@link android.app.Notification.Builder#addAction}. This allows for customization
5592 * of which actions display on different devices.
5593 *
5594 * @param actions the actions to add to this notification
5595 * @return this object for method chaining
5596 * @see android.app.Notification.Action
5597 */
5598 public WearableExtender addActions(List<Action> actions) {
5599 mActions.addAll(actions);
5600 return this;
5601 }
5602
5603 /**
5604 * Clear all wearable actions present on this builder.
5605 * @return this object for method chaining.
5606 * @see #addAction
5607 */
5608 public WearableExtender clearActions() {
5609 mActions.clear();
5610 return this;
5611 }
5612
5613 /**
5614 * Get the wearable actions present on this notification.
5615 */
5616 public List<Action> getActions() {
5617 return mActions;
5618 }
5619
5620 /**
5621 * Set an intent to launch inside of an activity view when displaying
Griff Hazen14f57992014-05-26 09:07:14 -07005622 * this notification. The {@link PendingIntent} provided should be for an activity.
5623 *
5624 * <pre class="prettyprint">
5625 * Intent displayIntent = new Intent(context, MyDisplayActivity.class);
5626 * PendingIntent displayPendingIntent = PendingIntent.getActivity(context,
5627 * 0, displayIntent, PendingIntent.FLAG_UPDATE_CURRENT);
5628 * Notification notif = new Notification.Builder(context)
5629 * .extend(new Notification.WearableExtender()
5630 * .setDisplayIntent(displayPendingIntent)
5631 * .setCustomSizePreset(Notification.WearableExtender.SIZE_MEDIUM))
5632 * .build();</pre>
5633 *
5634 * <p>The activity to launch needs to allow embedding, must be exported, and
Griff Hazen831ca9d2014-06-17 00:38:38 -07005635 * should have an empty task affinity. It is also recommended to use the device
5636 * default light theme.
Griff Hazen14f57992014-05-26 09:07:14 -07005637 *
5638 * <p>Example AndroidManifest.xml entry:
5639 * <pre class="prettyprint">
5640 * &lt;activity android:name=&quot;com.example.MyDisplayActivity&quot;
5641 * android:exported=&quot;true&quot;
5642 * android:allowEmbedded=&quot;true&quot;
Griff Hazen831ca9d2014-06-17 00:38:38 -07005643 * android:taskAffinity=&quot;&quot;
5644 * android:theme=&quot;@android:style/Theme.DeviceDefault.Light&quot; /&gt;</pre>
Griff Hazen61a9e862014-05-22 16:05:19 -07005645 *
5646 * @param intent the {@link PendingIntent} for an activity
5647 * @return this object for method chaining
5648 * @see android.app.Notification.WearableExtender#getDisplayIntent
5649 */
5650 public WearableExtender setDisplayIntent(PendingIntent intent) {
5651 mDisplayIntent = intent;
5652 return this;
5653 }
5654
5655 /**
5656 * Get the intent to launch inside of an activity view when displaying this
5657 * notification. This {@code PendingIntent} should be for an activity.
5658 */
5659 public PendingIntent getDisplayIntent() {
5660 return mDisplayIntent;
5661 }
5662
5663 /**
5664 * Add an additional page of content to display with this notification. The current
5665 * notification forms the first page, and pages added using this function form
5666 * subsequent pages. This field can be used to separate a notification into multiple
5667 * sections.
5668 *
5669 * @param page the notification to add as another page
5670 * @return this object for method chaining
5671 * @see android.app.Notification.WearableExtender#getPages
5672 */
5673 public WearableExtender addPage(Notification page) {
5674 mPages.add(page);
5675 return this;
5676 }
5677
5678 /**
5679 * Add additional pages of content to display with this notification. The current
5680 * notification forms the first page, and pages added using this function form
5681 * subsequent pages. This field can be used to separate a notification into multiple
5682 * sections.
5683 *
5684 * @param pages a list of notifications
5685 * @return this object for method chaining
5686 * @see android.app.Notification.WearableExtender#getPages
5687 */
5688 public WearableExtender addPages(List<Notification> pages) {
5689 mPages.addAll(pages);
5690 return this;
5691 }
5692
5693 /**
5694 * Clear all additional pages present on this builder.
5695 * @return this object for method chaining.
5696 * @see #addPage
5697 */
5698 public WearableExtender clearPages() {
5699 mPages.clear();
5700 return this;
5701 }
5702
5703 /**
5704 * Get the array of additional pages of content for displaying this notification. The
5705 * current notification forms the first page, and elements within this array form
5706 * subsequent pages. This field can be used to separate a notification into multiple
5707 * sections.
5708 * @return the pages for this notification
5709 */
5710 public List<Notification> getPages() {
5711 return mPages;
5712 }
5713
5714 /**
5715 * Set a background image to be displayed behind the notification content.
5716 * Contrary to the {@link android.app.Notification.BigPictureStyle}, this background
5717 * will work with any notification style.
5718 *
5719 * @param background the background bitmap
5720 * @return this object for method chaining
5721 * @see android.app.Notification.WearableExtender#getBackground
5722 */
5723 public WearableExtender setBackground(Bitmap background) {
5724 mBackground = background;
5725 return this;
5726 }
5727
5728 /**
5729 * Get a background image to be displayed behind the notification content.
5730 * Contrary to the {@link android.app.Notification.BigPictureStyle}, this background
5731 * will work with any notification style.
5732 *
5733 * @return the background image
5734 * @see android.app.Notification.WearableExtender#setBackground
5735 */
5736 public Bitmap getBackground() {
5737 return mBackground;
5738 }
5739
5740 /**
5741 * Set an icon that goes with the content of this notification.
5742 */
5743 public WearableExtender setContentIcon(int icon) {
5744 mContentIcon = icon;
5745 return this;
5746 }
5747
5748 /**
5749 * Get an icon that goes with the content of this notification.
5750 */
5751 public int getContentIcon() {
5752 return mContentIcon;
5753 }
5754
5755 /**
5756 * Set the gravity that the content icon should have within the notification display.
5757 * Supported values include {@link android.view.Gravity#START} and
5758 * {@link android.view.Gravity#END}. The default value is {@link android.view.Gravity#END}.
5759 * @see #setContentIcon
5760 */
5761 public WearableExtender setContentIconGravity(int contentIconGravity) {
5762 mContentIconGravity = contentIconGravity;
5763 return this;
5764 }
5765
5766 /**
5767 * Get the gravity that the content icon should have within the notification display.
5768 * Supported values include {@link android.view.Gravity#START} and
5769 * {@link android.view.Gravity#END}. The default value is {@link android.view.Gravity#END}.
5770 * @see #getContentIcon
5771 */
5772 public int getContentIconGravity() {
5773 return mContentIconGravity;
5774 }
5775
5776 /**
5777 * Set an action from this notification's actions to be clickable with the content of
Griff Hazen14f57992014-05-26 09:07:14 -07005778 * this notification. This action will no longer display separately from the
5779 * notification's content.
5780 *
Griff Hazenca48d352014-05-28 22:37:13 -07005781 * <p>For notifications with multiple pages, child pages can also have content actions
Griff Hazen14f57992014-05-26 09:07:14 -07005782 * set, although the list of available actions comes from the main notification and not
5783 * from the child page's notification.
5784 *
5785 * @param actionIndex The index of the action to hoist onto the current notification page.
5786 * If wearable actions were added to the main notification, this index
5787 * will apply to that list, otherwise it will apply to the regular
5788 * actions list.
Griff Hazen61a9e862014-05-22 16:05:19 -07005789 */
5790 public WearableExtender setContentAction(int actionIndex) {
5791 mContentActionIndex = actionIndex;
5792 return this;
5793 }
5794
5795 /**
Griff Hazenca48d352014-05-28 22:37:13 -07005796 * Get the index of the notification action, if any, that was specified as being clickable
5797 * with the content of this notification. This action will no longer display separately
Griff Hazen14f57992014-05-26 09:07:14 -07005798 * from the notification's content.
Griff Hazen61a9e862014-05-22 16:05:19 -07005799 *
Griff Hazenca48d352014-05-28 22:37:13 -07005800 * <p>For notifications with multiple pages, child pages can also have content actions
Griff Hazen14f57992014-05-26 09:07:14 -07005801 * set, although the list of available actions comes from the main notification and not
5802 * from the child page's notification.
5803 *
5804 * <p>If wearable specific actions were added to the main notification, this index will
5805 * apply to that list, otherwise it will apply to the regular actions list.
Griff Hazenca48d352014-05-28 22:37:13 -07005806 *
5807 * @return the action index or {@link #UNSET_ACTION_INDEX} if no action was selected.
Griff Hazen61a9e862014-05-22 16:05:19 -07005808 */
5809 public int getContentAction() {
5810 return mContentActionIndex;
5811 }
5812
5813 /**
5814 * Set the gravity that this notification should have within the available viewport space.
5815 * Supported values include {@link android.view.Gravity#TOP},
5816 * {@link android.view.Gravity#CENTER_VERTICAL} and {@link android.view.Gravity#BOTTOM}.
5817 * The default value is {@link android.view.Gravity#BOTTOM}.
5818 */
5819 public WearableExtender setGravity(int gravity) {
5820 mGravity = gravity;
5821 return this;
5822 }
5823
5824 /**
5825 * Get the gravity that this notification should have within the available viewport space.
5826 * Supported values include {@link android.view.Gravity#TOP},
5827 * {@link android.view.Gravity#CENTER_VERTICAL} and {@link android.view.Gravity#BOTTOM}.
5828 * The default value is {@link android.view.Gravity#BOTTOM}.
5829 */
5830 public int getGravity() {
5831 return mGravity;
5832 }
5833
5834 /**
5835 * Set the custom size preset for the display of this notification out of the available
5836 * presets found in {@link android.app.Notification.WearableExtender}, e.g.
5837 * {@link #SIZE_LARGE}.
5838 * <p>Some custom size presets are only applicable for custom display notifications created
5839 * using {@link android.app.Notification.WearableExtender#setDisplayIntent}. Check the
5840 * documentation for the preset in question. See also
5841 * {@link #setCustomContentHeight} and {@link #getCustomSizePreset}.
5842 */
5843 public WearableExtender setCustomSizePreset(int sizePreset) {
5844 mCustomSizePreset = sizePreset;
5845 return this;
5846 }
5847
5848 /**
5849 * Get the custom size preset for the display of this notification out of the available
5850 * presets found in {@link android.app.Notification.WearableExtender}, e.g.
5851 * {@link #SIZE_LARGE}.
5852 * <p>Some custom size presets are only applicable for custom display notifications created
5853 * using {@link #setDisplayIntent}. Check the documentation for the preset in question.
5854 * See also {@link #setCustomContentHeight} and {@link #setCustomSizePreset}.
5855 */
5856 public int getCustomSizePreset() {
5857 return mCustomSizePreset;
5858 }
5859
5860 /**
5861 * Set the custom height in pixels for the display of this notification's content.
5862 * <p>This option is only available for custom display notifications created
5863 * using {@link android.app.Notification.WearableExtender#setDisplayIntent}. See also
5864 * {@link android.app.Notification.WearableExtender#setCustomSizePreset} and
5865 * {@link #getCustomContentHeight}.
5866 */
5867 public WearableExtender setCustomContentHeight(int height) {
5868 mCustomContentHeight = height;
5869 return this;
5870 }
5871
5872 /**
5873 * Get the custom height in pixels for the display of this notification's content.
5874 * <p>This option is only available for custom display notifications created
5875 * using {@link #setDisplayIntent}. See also {@link #setCustomSizePreset} and
5876 * {@link #setCustomContentHeight}.
5877 */
5878 public int getCustomContentHeight() {
5879 return mCustomContentHeight;
5880 }
5881
5882 /**
5883 * Set whether the scrolling position for the contents of this notification should start
5884 * at the bottom of the contents instead of the top when the contents are too long to
5885 * display within the screen. Default is false (start scroll at the top).
5886 */
5887 public WearableExtender setStartScrollBottom(boolean startScrollBottom) {
5888 setFlag(FLAG_START_SCROLL_BOTTOM, startScrollBottom);
5889 return this;
5890 }
5891
5892 /**
5893 * Get whether the scrolling position for the contents of this notification should start
5894 * at the bottom of the contents instead of the top when the contents are too long to
5895 * display within the screen. Default is false (start scroll at the top).
5896 */
5897 public boolean getStartScrollBottom() {
5898 return (mFlags & FLAG_START_SCROLL_BOTTOM) != 0;
5899 }
5900
5901 /**
5902 * Set whether the content intent is available when the wearable device is not connected
5903 * to a companion device. The user can still trigger this intent when the wearable device
5904 * is offline, but a visual hint will indicate that the content intent may not be available.
5905 * Defaults to true.
5906 */
5907 public WearableExtender setContentIntentAvailableOffline(
5908 boolean contentIntentAvailableOffline) {
5909 setFlag(FLAG_CONTENT_INTENT_AVAILABLE_OFFLINE, contentIntentAvailableOffline);
5910 return this;
5911 }
5912
5913 /**
5914 * Get whether the content intent is available when the wearable device is not connected
5915 * to a companion device. The user can still trigger this intent when the wearable device
5916 * is offline, but a visual hint will indicate that the content intent may not be available.
5917 * Defaults to true.
5918 */
5919 public boolean getContentIntentAvailableOffline() {
5920 return (mFlags & FLAG_CONTENT_INTENT_AVAILABLE_OFFLINE) != 0;
5921 }
5922
5923 /**
5924 * Set a hint that this notification's icon should not be displayed.
5925 * @param hintHideIcon {@code true} to hide the icon, {@code false} otherwise.
5926 * @return this object for method chaining
5927 */
5928 public WearableExtender setHintHideIcon(boolean hintHideIcon) {
5929 setFlag(FLAG_HINT_HIDE_ICON, hintHideIcon);
5930 return this;
5931 }
5932
5933 /**
5934 * Get a hint that this notification's icon should not be displayed.
5935 * @return {@code true} if this icon should not be displayed, false otherwise.
5936 * The default value is {@code false} if this was never set.
5937 */
5938 public boolean getHintHideIcon() {
5939 return (mFlags & FLAG_HINT_HIDE_ICON) != 0;
5940 }
5941
5942 /**
5943 * Set a visual hint that only the background image of this notification should be
5944 * displayed, and other semantic content should be hidden. This hint is only applicable
5945 * to sub-pages added using {@link #addPage}.
5946 */
5947 public WearableExtender setHintShowBackgroundOnly(boolean hintShowBackgroundOnly) {
5948 setFlag(FLAG_HINT_SHOW_BACKGROUND_ONLY, hintShowBackgroundOnly);
5949 return this;
5950 }
5951
5952 /**
5953 * Get a visual hint that only the background image of this notification should be
5954 * displayed, and other semantic content should be hidden. This hint is only applicable
5955 * to sub-pages added using {@link android.app.Notification.WearableExtender#addPage}.
5956 */
5957 public boolean getHintShowBackgroundOnly() {
5958 return (mFlags & FLAG_HINT_SHOW_BACKGROUND_ONLY) != 0;
5959 }
5960
Griff Hazen5f2edfc2014-09-29 16:28:44 -07005961 /**
Griff Hazen9c5be4e2014-11-17 17:47:50 -08005962 * Set a hint that this notification's background should not be clipped if possible,
5963 * and should instead be resized to fully display on the screen, retaining the aspect
5964 * ratio of the image. This can be useful for images like barcodes or qr codes.
Griff Hazen5f2edfc2014-09-29 16:28:44 -07005965 * @param hintAvoidBackgroundClipping {@code true} to avoid clipping if possible.
5966 * @return this object for method chaining
5967 */
5968 public WearableExtender setHintAvoidBackgroundClipping(
5969 boolean hintAvoidBackgroundClipping) {
5970 setFlag(FLAG_HINT_AVOID_BACKGROUND_CLIPPING, hintAvoidBackgroundClipping);
5971 return this;
5972 }
5973
5974 /**
Griff Hazen9c5be4e2014-11-17 17:47:50 -08005975 * Get a hint that this notification's background should not be clipped if possible,
5976 * and should instead be resized to fully display on the screen, retaining the aspect
5977 * ratio of the image. This can be useful for images like barcodes or qr codes.
Griff Hazen5f2edfc2014-09-29 16:28:44 -07005978 * @return {@code true} if it's ok if the background is clipped on the screen, false
5979 * otherwise. The default value is {@code false} if this was never set.
5980 */
5981 public boolean getHintAvoidBackgroundClipping() {
5982 return (mFlags & FLAG_HINT_AVOID_BACKGROUND_CLIPPING) != 0;
5983 }
5984
5985 /**
5986 * Set a hint that the screen should remain on for at least this duration when
5987 * this notification is displayed on the screen.
5988 * @param timeout The requested screen timeout in milliseconds. Can also be either
5989 * {@link #SCREEN_TIMEOUT_SHORT} or {@link #SCREEN_TIMEOUT_LONG}.
5990 * @return this object for method chaining
5991 */
5992 public WearableExtender setHintScreenTimeout(int timeout) {
5993 mHintScreenTimeout = timeout;
5994 return this;
5995 }
5996
5997 /**
5998 * Get the duration, in milliseconds, that the screen should remain on for
5999 * when this notification is displayed.
6000 * @return the duration in milliseconds if > 0, or either one of the sentinel values
6001 * {@link #SCREEN_TIMEOUT_SHORT} or {@link #SCREEN_TIMEOUT_LONG}.
6002 */
6003 public int getHintScreenTimeout() {
6004 return mHintScreenTimeout;
6005 }
6006
Alex Hills9ab3a232016-04-05 14:54:56 -04006007 /**
6008 * Set a hint that this notification's content intent will launch an {@link Activity}
6009 * directly, telling the platform that it can generate the appropriate transitions.
6010 * @param hintContentIntentLaunchesActivity {@code true} if the content intent will launch
6011 * an activity and transitions should be generated, false otherwise.
6012 * @return this object for method chaining
6013 */
6014 public WearableExtender setHintContentIntentLaunchesActivity(
6015 boolean hintContentIntentLaunchesActivity) {
6016 setFlag(FLAG_HINT_CONTENT_INTENT_LAUNCHES_ACTIVITY, hintContentIntentLaunchesActivity);
6017 return this;
6018 }
6019
6020 /**
6021 * Get a hint that this notification's content intent will launch an {@link Activity}
6022 * directly, telling the platform that it can generate the appropriate transitions
6023 * @return {@code true} if the content intent will launch an activity and transitions should
6024 * be generated, false otherwise. The default value is {@code false} if this was never set.
6025 */
6026 public boolean getHintContentIntentLaunchesActivity() {
6027 return (mFlags & FLAG_HINT_CONTENT_INTENT_LAUNCHES_ACTIVITY) != 0;
6028 }
6029
Griff Hazen61a9e862014-05-22 16:05:19 -07006030 private void setFlag(int mask, boolean value) {
6031 if (value) {
6032 mFlags |= mask;
6033 } else {
6034 mFlags &= ~mask;
6035 }
6036 }
6037 }
6038
6039 /**
Zhen Yu Song9d5528b2014-11-14 15:34:39 -08006040 * <p>Helper class to add Android Auto extensions to notifications. To create a notification
6041 * with car extensions:
6042 *
6043 * <ol>
6044 * <li>Create an {@link Notification.Builder}, setting any desired
6045 * properties.
6046 * <li>Create a {@link CarExtender}.
6047 * <li>Set car-specific properties using the {@code add} and {@code set} methods of
6048 * {@link CarExtender}.
6049 * <li>Call {@link Notification.Builder#extend(Notification.Extender)}
6050 * to apply the extensions to a notification.
6051 * </ol>
6052 *
6053 * <pre class="prettyprint">
6054 * Notification notification = new Notification.Builder(context)
6055 * ...
6056 * .extend(new CarExtender()
6057 * .set*(...))
6058 * .build();
6059 * </pre>
6060 *
6061 * <p>Car extensions can be accessed on an existing notification by using the
6062 * {@code CarExtender(Notification)} constructor, and then using the {@code get} methods
6063 * to access values.
6064 */
6065 public static final class CarExtender implements Extender {
6066 private static final String TAG = "CarExtender";
6067
6068 private static final String EXTRA_CAR_EXTENDER = "android.car.EXTENSIONS";
6069 private static final String EXTRA_LARGE_ICON = "large_icon";
6070 private static final String EXTRA_CONVERSATION = "car_conversation";
6071 private static final String EXTRA_COLOR = "app_color";
6072
6073 private Bitmap mLargeIcon;
6074 private UnreadConversation mUnreadConversation;
6075 private int mColor = Notification.COLOR_DEFAULT;
6076
6077 /**
6078 * Create a {@link CarExtender} with default options.
6079 */
6080 public CarExtender() {
6081 }
6082
6083 /**
6084 * Create a {@link CarExtender} from the CarExtender options of an existing Notification.
6085 *
6086 * @param notif The notification from which to copy options.
6087 */
6088 public CarExtender(Notification notif) {
6089 Bundle carBundle = notif.extras == null ?
6090 null : notif.extras.getBundle(EXTRA_CAR_EXTENDER);
6091 if (carBundle != null) {
6092 mLargeIcon = carBundle.getParcelable(EXTRA_LARGE_ICON);
6093 mColor = carBundle.getInt(EXTRA_COLOR, Notification.COLOR_DEFAULT);
6094
6095 Bundle b = carBundle.getBundle(EXTRA_CONVERSATION);
6096 mUnreadConversation = UnreadConversation.getUnreadConversationFromBundle(b);
6097 }
6098 }
6099
6100 /**
6101 * Apply car extensions to a notification that is being built. This is typically called by
6102 * the {@link Notification.Builder#extend(Notification.Extender)}
6103 * method of {@link Notification.Builder}.
6104 */
6105 @Override
6106 public Notification.Builder extend(Notification.Builder builder) {
6107 Bundle carExtensions = new Bundle();
6108
6109 if (mLargeIcon != null) {
6110 carExtensions.putParcelable(EXTRA_LARGE_ICON, mLargeIcon);
6111 }
6112 if (mColor != Notification.COLOR_DEFAULT) {
6113 carExtensions.putInt(EXTRA_COLOR, mColor);
6114 }
6115
6116 if (mUnreadConversation != null) {
6117 Bundle b = mUnreadConversation.getBundleForUnreadConversation();
6118 carExtensions.putBundle(EXTRA_CONVERSATION, b);
6119 }
6120
6121 builder.getExtras().putBundle(EXTRA_CAR_EXTENDER, carExtensions);
6122 return builder;
6123 }
6124
6125 /**
6126 * Sets the accent color to use when Android Auto presents the notification.
6127 *
6128 * Android Auto uses the color set with {@link Notification.Builder#setColor(int)}
6129 * to accent the displayed notification. However, not all colors are acceptable in an
6130 * automotive setting. This method can be used to override the color provided in the
6131 * notification in such a situation.
6132 */
Tor Norbye80756e32015-03-02 09:39:27 -08006133 public CarExtender setColor(@ColorInt int color) {
Zhen Yu Song9d5528b2014-11-14 15:34:39 -08006134 mColor = color;
6135 return this;
6136 }
6137
6138 /**
6139 * Gets the accent color.
6140 *
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006141 * @see #setColor
Zhen Yu Song9d5528b2014-11-14 15:34:39 -08006142 */
Tor Norbye80756e32015-03-02 09:39:27 -08006143 @ColorInt
Zhen Yu Song9d5528b2014-11-14 15:34:39 -08006144 public int getColor() {
6145 return mColor;
6146 }
6147
6148 /**
6149 * Sets the large icon of the car notification.
6150 *
6151 * If no large icon is set in the extender, Android Auto will display the icon
6152 * specified by {@link Notification.Builder#setLargeIcon(android.graphics.Bitmap)}
6153 *
6154 * @param largeIcon The large icon to use in the car notification.
6155 * @return This object for method chaining.
6156 */
6157 public CarExtender setLargeIcon(Bitmap largeIcon) {
6158 mLargeIcon = largeIcon;
6159 return this;
6160 }
6161
6162 /**
6163 * Gets the large icon used in this car notification, or null if no icon has been set.
6164 *
6165 * @return The large icon for the car notification.
6166 * @see CarExtender#setLargeIcon
6167 */
6168 public Bitmap getLargeIcon() {
6169 return mLargeIcon;
6170 }
6171
6172 /**
6173 * Sets the unread conversation in a message notification.
6174 *
6175 * @param unreadConversation The unread part of the conversation this notification conveys.
6176 * @return This object for method chaining.
6177 */
6178 public CarExtender setUnreadConversation(UnreadConversation unreadConversation) {
6179 mUnreadConversation = unreadConversation;
6180 return this;
6181 }
6182
6183 /**
6184 * Returns the unread conversation conveyed by this notification.
6185 * @see #setUnreadConversation(UnreadConversation)
6186 */
6187 public UnreadConversation getUnreadConversation() {
6188 return mUnreadConversation;
6189 }
6190
6191 /**
6192 * A class which holds the unread messages from a conversation.
6193 */
6194 public static class UnreadConversation {
6195 private static final String KEY_AUTHOR = "author";
6196 private static final String KEY_TEXT = "text";
6197 private static final String KEY_MESSAGES = "messages";
6198 private static final String KEY_REMOTE_INPUT = "remote_input";
6199 private static final String KEY_ON_REPLY = "on_reply";
6200 private static final String KEY_ON_READ = "on_read";
6201 private static final String KEY_PARTICIPANTS = "participants";
6202 private static final String KEY_TIMESTAMP = "timestamp";
6203
6204 private final String[] mMessages;
6205 private final RemoteInput mRemoteInput;
6206 private final PendingIntent mReplyPendingIntent;
6207 private final PendingIntent mReadPendingIntent;
6208 private final String[] mParticipants;
6209 private final long mLatestTimestamp;
6210
6211 UnreadConversation(String[] messages, RemoteInput remoteInput,
6212 PendingIntent replyPendingIntent, PendingIntent readPendingIntent,
6213 String[] participants, long latestTimestamp) {
6214 mMessages = messages;
6215 mRemoteInput = remoteInput;
6216 mReadPendingIntent = readPendingIntent;
6217 mReplyPendingIntent = replyPendingIntent;
6218 mParticipants = participants;
6219 mLatestTimestamp = latestTimestamp;
6220 }
6221
6222 /**
6223 * Gets the list of messages conveyed by this notification.
6224 */
6225 public String[] getMessages() {
6226 return mMessages;
6227 }
6228
6229 /**
6230 * Gets the remote input that will be used to convey the response to a message list, or
6231 * null if no such remote input exists.
6232 */
6233 public RemoteInput getRemoteInput() {
6234 return mRemoteInput;
6235 }
6236
6237 /**
6238 * Gets the pending intent that will be triggered when the user replies to this
6239 * notification.
6240 */
6241 public PendingIntent getReplyPendingIntent() {
6242 return mReplyPendingIntent;
6243 }
6244
6245 /**
6246 * Gets the pending intent that Android Auto will send after it reads aloud all messages
6247 * in this object's message list.
6248 */
6249 public PendingIntent getReadPendingIntent() {
6250 return mReadPendingIntent;
6251 }
6252
6253 /**
6254 * Gets the participants in the conversation.
6255 */
6256 public String[] getParticipants() {
6257 return mParticipants;
6258 }
6259
6260 /**
6261 * Gets the firs participant in the conversation.
6262 */
6263 public String getParticipant() {
6264 return mParticipants.length > 0 ? mParticipants[0] : null;
6265 }
6266
6267 /**
6268 * Gets the timestamp of the conversation.
6269 */
6270 public long getLatestTimestamp() {
6271 return mLatestTimestamp;
6272 }
6273
6274 Bundle getBundleForUnreadConversation() {
6275 Bundle b = new Bundle();
6276 String author = null;
6277 if (mParticipants != null && mParticipants.length > 1) {
6278 author = mParticipants[0];
6279 }
6280 Parcelable[] messages = new Parcelable[mMessages.length];
6281 for (int i = 0; i < messages.length; i++) {
6282 Bundle m = new Bundle();
6283 m.putString(KEY_TEXT, mMessages[i]);
6284 m.putString(KEY_AUTHOR, author);
6285 messages[i] = m;
6286 }
6287 b.putParcelableArray(KEY_MESSAGES, messages);
6288 if (mRemoteInput != null) {
6289 b.putParcelable(KEY_REMOTE_INPUT, mRemoteInput);
6290 }
6291 b.putParcelable(KEY_ON_REPLY, mReplyPendingIntent);
6292 b.putParcelable(KEY_ON_READ, mReadPendingIntent);
6293 b.putStringArray(KEY_PARTICIPANTS, mParticipants);
6294 b.putLong(KEY_TIMESTAMP, mLatestTimestamp);
6295 return b;
6296 }
6297
6298 static UnreadConversation getUnreadConversationFromBundle(Bundle b) {
6299 if (b == null) {
6300 return null;
6301 }
6302 Parcelable[] parcelableMessages = b.getParcelableArray(KEY_MESSAGES);
6303 String[] messages = null;
6304 if (parcelableMessages != null) {
6305 String[] tmp = new String[parcelableMessages.length];
6306 boolean success = true;
6307 for (int i = 0; i < tmp.length; i++) {
6308 if (!(parcelableMessages[i] instanceof Bundle)) {
6309 success = false;
6310 break;
6311 }
6312 tmp[i] = ((Bundle) parcelableMessages[i]).getString(KEY_TEXT);
6313 if (tmp[i] == null) {
6314 success = false;
6315 break;
6316 }
6317 }
6318 if (success) {
6319 messages = tmp;
6320 } else {
6321 return null;
6322 }
6323 }
6324
6325 PendingIntent onRead = b.getParcelable(KEY_ON_READ);
6326 PendingIntent onReply = b.getParcelable(KEY_ON_REPLY);
6327
6328 RemoteInput remoteInput = b.getParcelable(KEY_REMOTE_INPUT);
6329
6330 String[] participants = b.getStringArray(KEY_PARTICIPANTS);
6331 if (participants == null || participants.length != 1) {
6332 return null;
6333 }
6334
6335 return new UnreadConversation(messages,
6336 remoteInput,
6337 onReply,
6338 onRead,
6339 participants, b.getLong(KEY_TIMESTAMP));
6340 }
6341 };
6342
6343 /**
6344 * Builder class for {@link CarExtender.UnreadConversation} objects.
6345 */
6346 public static class Builder {
6347 private final List<String> mMessages = new ArrayList<String>();
6348 private final String mParticipant;
6349 private RemoteInput mRemoteInput;
6350 private PendingIntent mReadPendingIntent;
6351 private PendingIntent mReplyPendingIntent;
6352 private long mLatestTimestamp;
6353
6354 /**
6355 * Constructs a new builder for {@link CarExtender.UnreadConversation}.
6356 *
6357 * @param name The name of the other participant in the conversation.
6358 */
6359 public Builder(String name) {
6360 mParticipant = name;
6361 }
6362
6363 /**
6364 * Appends a new unread message to the list of messages for this conversation.
6365 *
6366 * The messages should be added from oldest to newest.
6367 *
6368 * @param message The text of the new unread message.
6369 * @return This object for method chaining.
6370 */
6371 public Builder addMessage(String message) {
6372 mMessages.add(message);
6373 return this;
6374 }
6375
6376 /**
6377 * Sets the pending intent and remote input which will convey the reply to this
6378 * notification.
6379 *
6380 * @param pendingIntent The pending intent which will be triggered on a reply.
6381 * @param remoteInput The remote input parcelable which will carry the reply.
6382 * @return This object for method chaining.
6383 *
6384 * @see CarExtender.UnreadConversation#getRemoteInput
6385 * @see CarExtender.UnreadConversation#getReplyPendingIntent
6386 */
6387 public Builder setReplyAction(
6388 PendingIntent pendingIntent, RemoteInput remoteInput) {
6389 mRemoteInput = remoteInput;
6390 mReplyPendingIntent = pendingIntent;
6391
6392 return this;
6393 }
6394
6395 /**
6396 * Sets the pending intent that will be sent once the messages in this notification
6397 * are read.
6398 *
6399 * @param pendingIntent The pending intent to use.
6400 * @return This object for method chaining.
6401 */
6402 public Builder setReadPendingIntent(PendingIntent pendingIntent) {
6403 mReadPendingIntent = pendingIntent;
6404 return this;
6405 }
6406
6407 /**
6408 * Sets the timestamp of the most recent message in an unread conversation.
6409 *
6410 * If a messaging notification has been posted by your application and has not
6411 * yet been cancelled, posting a later notification with the same id and tag
6412 * but without a newer timestamp may result in Android Auto not displaying a
6413 * heads up notification for the later notification.
6414 *
6415 * @param timestamp The timestamp of the most recent message in the conversation.
6416 * @return This object for method chaining.
6417 */
6418 public Builder setLatestTimestamp(long timestamp) {
6419 mLatestTimestamp = timestamp;
6420 return this;
6421 }
6422
6423 /**
6424 * Builds a new unread conversation object.
6425 *
6426 * @return The new unread conversation object.
6427 */
6428 public UnreadConversation build() {
6429 String[] messages = mMessages.toArray(new String[mMessages.size()]);
6430 String[] participants = { mParticipant };
6431 return new UnreadConversation(messages, mRemoteInput, mReplyPendingIntent,
6432 mReadPendingIntent, participants, mLatestTimestamp);
6433 }
6434 }
6435 }
6436
6437 /**
Griff Hazen61a9e862014-05-22 16:05:19 -07006438 * Get an array of Notification objects from a parcelable array bundle field.
6439 * Update the bundle to have a typed array so fetches in the future don't need
6440 * to do an array copy.
6441 */
6442 private static Notification[] getNotificationArrayFromBundle(Bundle bundle, String key) {
6443 Parcelable[] array = bundle.getParcelableArray(key);
6444 if (array instanceof Notification[] || array == null) {
6445 return (Notification[]) array;
6446 }
6447 Notification[] typedArray = Arrays.copyOf(array, array.length,
6448 Notification[].class);
6449 bundle.putParcelableArray(key, typedArray);
6450 return typedArray;
6451 }
Christoph Studer4600f9b2014-07-22 22:44:43 +02006452
6453 private static class BuilderRemoteViews extends RemoteViews {
6454 public BuilderRemoteViews(Parcel parcel) {
6455 super(parcel);
6456 }
6457
Kenny Guy77320062014-08-27 21:37:15 +01006458 public BuilderRemoteViews(ApplicationInfo appInfo, int layoutId) {
6459 super(appInfo, layoutId);
Christoph Studer4600f9b2014-07-22 22:44:43 +02006460 }
6461
6462 @Override
6463 public BuilderRemoteViews clone() {
6464 Parcel p = Parcel.obtain();
6465 writeToParcel(p, 0);
6466 p.setDataPosition(0);
6467 BuilderRemoteViews brv = new BuilderRemoteViews(p);
6468 p.recycle();
6469 return brv;
6470 }
6471 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006472}