blob: 784872cedaa243796687c05e4021af0185d62ea1 [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;
Christoph Studer4600f9b2014-07-22 22:44:43 +020028import android.content.pm.PackageManager.NameNotFoundException;
Jorim Jaggief72a192014-08-26 21:57:46 +020029import android.content.res.ColorStateList;
Joe Onoratoef1e7762010-09-17 18:38:38 -040030import android.graphics.Bitmap;
Kenny Guy8a0101b2014-05-08 23:34:12 +010031import android.graphics.Canvas;
Jorim Jaggi5c2d8462014-03-21 17:37:00 +010032import android.graphics.PorterDuff;
Kenny Guy8a0101b2014-05-08 23:34:12 +010033import android.graphics.drawable.Drawable;
Dan Sandlerd63f9322015-05-06 15:18:49 -040034import android.graphics.drawable.Icon;
John Spurlockc0650f022014-07-19 13:22:39 -040035import android.media.AudioAttributes;
Jeff Sharkey098d5802012-04-26 17:30:34 -070036import android.media.AudioManager;
Jeff Browndba34ba2014-06-24 20:46:03 -070037import android.media.session.MediaSession;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080038import android.net.Uri;
Daniel Sandlerdcbaf662013-04-26 16:23:09 -040039import android.os.BadParcelableException;
Christoph Studer239f8352014-08-25 15:13:18 +020040import android.os.Build;
Daniel Sandler2561b0b2012-02-13 21:04:12 -050041import android.os.Bundle;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080042import android.os.Parcel;
43import android.os.Parcelable;
Daniel Sandlera2985ed2012-04-03 16:42:00 -040044import android.os.SystemClock;
Jeff Sharkey6d515712012-09-20 16:06:08 -070045import android.os.UserHandle;
Selim Cinek60a54252016-02-26 17:03:25 -080046import android.text.SpannableStringBuilder;
47import android.text.Spanned;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080048import android.text.TextUtils;
Selim Cinek60a54252016-02-26 17:03:25 -080049import android.text.style.AbsoluteSizeSpan;
Selim Cinek89991a22016-03-07 19:51:54 -080050import android.text.style.CharacterStyle;
Selim Cinek60a54252016-02-26 17:03:25 -080051import android.text.style.RelativeSizeSpan;
52import android.text.style.TextAppearanceSpan;
Daniel Sandlerdcbaf662013-04-26 16:23:09 -040053import android.util.Log;
Dan Sandler50128532015-12-08 15:42:41 -050054import android.util.SparseArray;
Daniel Sandler9f7936a2012-05-21 16:14:28 -040055import android.util.TypedValue;
Griff Hazen61a9e862014-05-22 16:05:19 -070056import android.view.Gravity;
Selim Cinekea4bef72015-12-02 15:51:10 -080057import android.view.NotificationHeaderView;
Joe Onorato8595a3d2010-11-19 18:12:07 -080058import android.view.View;
Adrian Roos9b123cf2016-02-04 14:55:57 -080059import android.view.ViewGroup;
Jeff Sharkey1c400132011-08-05 14:50:13 -070060import android.widget.ProgressBar;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080061import android.widget.RemoteViews;
62
Griff Hazen959591e2014-05-15 22:26:18 -070063import com.android.internal.R;
Griff Hazenc091ba82014-05-16 10:13:26 -070064import com.android.internal.util.NotificationColorUtil;
Griff Hazen959591e2014-05-15 22:26:18 -070065
Tor Norbyed9273d62013-05-30 15:59:53 -070066import java.lang.annotation.Retention;
67import java.lang.annotation.RetentionPolicy;
Christoph Studer4600f9b2014-07-22 22:44:43 +020068import java.lang.reflect.Constructor;
Daniel Sandler2561b0b2012-02-13 21:04:12 -050069import java.util.ArrayList;
Griff Hazen61a9e862014-05-22 16:05:19 -070070import java.util.Arrays;
Griff Hazen5cadc3b2014-05-20 09:55:39 -070071import java.util.Collections;
Griff Hazen61a9e862014-05-22 16:05:19 -070072import java.util.List;
Dan Sandler50128532015-12-08 15:42:41 -050073import java.util.Set;
Joe Onorato561d3852010-11-20 18:09:34 -080074
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080075/**
76 * A class that represents how a persistent notification is to be presented to
77 * the user using the {@link android.app.NotificationManager}.
78 *
Joe Onoratocb109a02011-01-18 17:57:41 -080079 * <p>The {@link Notification.Builder Notification.Builder} has been added to make it
80 * easier to construct Notifications.</p>
81 *
Joe Fernandez558459f2011-10-13 16:47:36 -070082 * <div class="special reference">
83 * <h3>Developer Guides</h3>
84 * <p>For a guide to creating notifications, read the
85 * <a href="{@docRoot}guide/topics/ui/notifiers/notifications.html">Status Bar Notifications</a>
86 * developer guide.</p>
87 * </div>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080088 */
89public class Notification implements Parcelable
90{
Daniel Sandlerdcbaf662013-04-26 16:23:09 -040091 private static final String TAG = "Notification";
92
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080093 /**
Daniel Sandler01df1c62014-06-09 10:54:01 -040094 * An activity that provides a user interface for adjusting notification preferences for its
95 * containing application. Optional but recommended for apps that post
96 * {@link android.app.Notification Notifications}.
97 */
98 @SdkConstant(SdkConstantType.INTENT_CATEGORY)
99 public static final String INTENT_CATEGORY_NOTIFICATION_PREFERENCES
100 = "android.intent.category.NOTIFICATION_PREFERENCES";
101
102 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800103 * Use all default values (where applicable).
104 */
105 public static final int DEFAULT_ALL = ~0;
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500106
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800107 /**
108 * Use the default notification sound. This will ignore any given
109 * {@link #sound}.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500110 *
Chris Wren47c20a12014-06-18 17:27:29 -0400111 * <p>
112 * A notification that is noisy is more likely to be presented as a heads-up notification.
113 * </p>
114 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800115 * @see #defaults
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500116 */
117
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800118 public static final int DEFAULT_SOUND = 1;
119
120 /**
121 * Use the default notification vibrate. This will ignore any given
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500122 * {@link #vibrate}. Using phone vibration requires the
Scott Mainb8b36452009-04-26 15:50:49 -0700123 * {@link android.Manifest.permission#VIBRATE VIBRATE} permission.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500124 *
Chris Wren47c20a12014-06-18 17:27:29 -0400125 * <p>
126 * A notification that vibrates is more likely to be presented as a heads-up notification.
127 * </p>
128 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800129 * @see #defaults
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500130 */
131
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800132 public static final int DEFAULT_VIBRATE = 2;
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500133
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800134 /**
135 * Use the default notification lights. This will ignore the
136 * {@link #FLAG_SHOW_LIGHTS} bit, and {@link #ledARGB}, {@link #ledOffMS}, or
137 * {@link #ledOnMS}.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500138 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800139 * @see #defaults
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500140 */
141
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800142 public static final int DEFAULT_LIGHTS = 4;
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500143
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800144 /**
Christoph Studer535ec612014-09-03 15:47:47 +0200145 * Maximum length of CharSequences accepted by Builder and friends.
146 *
147 * <p>
148 * Avoids spamming the system with overly large strings such as full e-mails.
149 */
150 private static final int MAX_CHARSEQUENCE_LENGTH = 5 * 1024;
151
152 /**
Adrian Roose458aa82015-12-08 16:17:19 -0800153 * Maximum entries of reply text that are accepted by Builder and friends.
154 */
155 private static final int MAX_REPLY_HISTORY = 5;
156
157 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500158 * A timestamp related to this notification, in milliseconds since the epoch.
Joe Malin8d40d042012-11-05 11:36:40 -0800159 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500160 * Default value: {@link System#currentTimeMillis() Now}.
161 *
162 * Choose a timestamp that will be most relevant to the user. For most finite events, this
163 * corresponds to the time the event happened (or will happen, in the case of events that have
164 * yet to occur but about which the user is being informed). Indefinite events should be
Joe Malin8d40d042012-11-05 11:36:40 -0800165 * timestamped according to when the activity began.
166 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500167 * Some examples:
Joe Malin8d40d042012-11-05 11:36:40 -0800168 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500169 * <ul>
170 * <li>Notification of a new chat message should be stamped when the message was received.</li>
171 * <li>Notification of an ongoing file download (with a progress bar, for example) should be stamped when the download started.</li>
172 * <li>Notification of a completed file download should be stamped when the download finished.</li>
173 * <li>Notification of an upcoming meeting should be stamped with the time the meeting will begin (that is, in the future).</li>
174 * <li>Notification of an ongoing stopwatch (increasing timer) should be stamped with the watch's start time.
175 * <li>Notification of an ongoing countdown timer should be stamped with the timer's end time.
Joe Malin8d40d042012-11-05 11:36:40 -0800176 * </ul>
177 *
Selim Cinek0ff1ce602016-04-05 18:27:16 -0700178 * For apps targeting {@link android.os.Build.VERSION_CODES#N} and above, this time is not shown
179 * anymore by default and must be opted into by using
180 * {@link android.app.Notification.Builder#setShowWhen(boolean)}
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800181 */
182 public long when;
183
184 /**
185 * The resource id of a drawable to use as the icon in the status bar.
Dan Sandler86647982015-05-13 23:41:13 -0400186 *
187 * @deprecated Use {@link Builder#setSmallIcon(Icon)} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800188 */
Dan Sandler86647982015-05-13 23:41:13 -0400189 @Deprecated
Tor Norbye7b9c9122013-05-30 16:48:33 -0700190 @DrawableRes
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800191 public int icon;
192
193 /**
Joe Onorato46439ce2010-11-19 13:56:21 -0800194 * If the icon in the status bar is to have more than one level, you can set this. Otherwise,
195 * leave it at its default value of 0.
196 *
197 * @see android.widget.ImageView#setImageLevel
Griff Hazen959591e2014-05-15 22:26:18 -0700198 * @see android.graphics.drawable.Drawable#setLevel
Joe Onorato46439ce2010-11-19 13:56:21 -0800199 */
200 public int iconLevel;
201
202 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500203 * The number of events that this notification represents. For example, in a new mail
204 * notification, this could be the number of unread messages.
Joe Malin8d40d042012-11-05 11:36:40 -0800205 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500206 * The system may or may not use this field to modify the appearance of the notification. For
207 * example, before {@link android.os.Build.VERSION_CODES#HONEYCOMB}, this number was
208 * superimposed over the icon in the status bar. Starting with
209 * {@link android.os.Build.VERSION_CODES#HONEYCOMB}, the template used by
210 * {@link Notification.Builder} has displayed the number in the expanded notification view.
Joe Malin8d40d042012-11-05 11:36:40 -0800211 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500212 * If the number is 0 or negative, it is never shown.
Selim Cinek0f9dd1e2016-04-05 17:03:40 -0700213 *
214 * @deprecated this number is not shown anymore
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800215 */
216 public int number;
217
218 /**
219 * The intent to execute when the expanded status entry is clicked. If
220 * this is an activity, it must include the
221 * {@link android.content.Intent#FLAG_ACTIVITY_NEW_TASK} flag, which requires
Scott Main7aee61f2011-02-08 11:25:01 -0800222 * that you take care of task management as described in the
223 * <a href="{@docRoot}guide/topics/fundamentals/tasks-and-back-stack.html">Tasks and Back
Dianne Hackborn6ceca582012-01-10 15:24:26 -0800224 * Stack</a> document. In particular, make sure to read the notification section
225 * <a href="{@docRoot}guide/topics/ui/notifiers/notifications.html#HandlingNotifications">Handling
226 * Notifications</a> for the correct ways to launch an application from a
227 * notification.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800228 */
229 public PendingIntent contentIntent;
230
231 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500232 * The intent to execute when the notification is explicitly dismissed by the user, either with
233 * the "Clear All" button or by swiping it away individually.
234 *
235 * This probably shouldn't be launching an activity since several of those will be sent
236 * at the same time.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800237 */
238 public PendingIntent deleteIntent;
239
240 /**
Dianne Hackborn170bae72010-09-03 15:14:28 -0700241 * An intent to launch instead of posting the notification to the status bar.
Joe Onoratocb109a02011-01-18 17:57:41 -0800242 *
Chris Wren47c20a12014-06-18 17:27:29 -0400243 * <p>
244 * The system UI may choose to display a heads-up notification, instead of
245 * launching this intent, while the user is using the device.
246 * </p>
247 *
Joe Onoratocb109a02011-01-18 17:57:41 -0800248 * @see Notification.Builder#setFullScreenIntent
Daniel Sandlere46cbd32010-06-17 10:35:26 -0400249 */
250 public PendingIntent fullScreenIntent;
251
252 /**
Dan Sandler5fcdf6e2014-07-18 11:31:15 -0400253 * Text that summarizes this notification for accessibility services.
254 *
255 * As of the L release, this text is no longer shown on screen, but it is still useful to
256 * accessibility services (where it serves as an audible announcement of the notification's
257 * appearance).
Joe Onoratoef1e7762010-09-17 18:38:38 -0400258 *
Joe Onorato46439ce2010-11-19 13:56:21 -0800259 * @see #tickerView
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800260 */
261 public CharSequence tickerText;
262
263 /**
Dan Sandler5fcdf6e2014-07-18 11:31:15 -0400264 * Formerly, a view showing the {@link #tickerText}.
265 *
266 * No longer displayed in the status bar as of API 21.
Joe Onoratoef1e7762010-09-17 18:38:38 -0400267 */
Dan Sandler5fcdf6e2014-07-18 11:31:15 -0400268 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -0800269 public RemoteViews tickerView;
Joe Onoratoef1e7762010-09-17 18:38:38 -0400270
271 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400272 * The view that will represent this notification in the notification list (which is pulled
273 * down from the status bar).
274 *
Julia Reynoldsd4ea7412016-02-17 14:00:56 -0500275 * As of N, this field may be null. The notification view is determined by the inputs
276 * to {@link Notification.Builder}; a custom RemoteViews can optionally be
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400277 * supplied with {@link Notification.Builder#setCustomContentView(RemoteViews)}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800278 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400279 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800280 public RemoteViews contentView;
281
Daniel Sandlera0a938c2012-03-15 08:42:37 -0400282 /**
Daniel Sandler4dfbe832012-04-11 14:51:46 -0400283 * A large-format version of {@link #contentView}, giving the Notification an
Daniel Sandlerf3b73432012-03-27 15:01:25 -0400284 * opportunity to show more detail. The system UI may choose to show this
285 * instead of the normal content view at its discretion.
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400286 *
Julia Reynoldsd4ea7412016-02-17 14:00:56 -0500287 * As of N, this field may be null. The expanded notification view is determined by the
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400288 * inputs to {@link Notification.Builder}; a custom RemoteViews can optionally be
289 * supplied with {@link Notification.Builder#setCustomBigContentView(RemoteViews)}.
Daniel Sandlerf3b73432012-03-27 15:01:25 -0400290 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400291 @Deprecated
Daniel Sandlerf3b73432012-03-27 15:01:25 -0400292 public RemoteViews bigContentView;
293
Chris Wren8fd39ec2014-02-27 17:43:26 -0500294
295 /**
Chris Wren47c20a12014-06-18 17:27:29 -0400296 * A medium-format version of {@link #contentView}, providing the Notification an
297 * opportunity to add action buttons to contentView. At its discretion, the system UI may
298 * choose to show this as a heads-up notification, which will pop up so the user can see
299 * it without leaving their current activity.
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400300 *
Julia Reynoldsd4ea7412016-02-17 14:00:56 -0500301 * As of N, this field may be null. The heads-up notification view is determined by the
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400302 * inputs to {@link Notification.Builder}; a custom RemoteViews can optionally be
303 * supplied with {@link Notification.Builder#setCustomHeadsUpContentView(RemoteViews)}.
Chris Wren8fd39ec2014-02-27 17:43:26 -0500304 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400305 @Deprecated
Chris Wren8fd39ec2014-02-27 17:43:26 -0500306 public RemoteViews headsUpContentView;
307
Daniel Sandlerf3b73432012-03-27 15:01:25 -0400308 /**
Dan Sandler86647982015-05-13 23:41:13 -0400309 * A large bitmap to be shown in the notification content area.
310 *
311 * @deprecated Use {@link Builder#setLargeIcon(Icon)} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800312 */
Dan Sandler86647982015-05-13 23:41:13 -0400313 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -0800314 public Bitmap largeIcon;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800315
316 /**
317 * The sound to play.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500318 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800319 * <p>
Chris Wren47c20a12014-06-18 17:27:29 -0400320 * A notification that is noisy is more likely to be presented as a heads-up notification.
321 * </p>
322 *
323 * <p>
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500324 * To play the default notification sound, see {@link #defaults}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800325 * </p>
326 */
327 public Uri sound;
328
329 /**
330 * Use this constant as the value for audioStreamType to request that
331 * the default stream type for notifications be used. Currently the
Jeff Sharkey098d5802012-04-26 17:30:34 -0700332 * default stream type is {@link AudioManager#STREAM_NOTIFICATION}.
John Spurlockc0650f022014-07-19 13:22:39 -0400333 *
334 * @deprecated Use {@link #audioAttributes} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800335 */
Jean-Michel Trivi81f871e2014-08-06 16:32:38 -0700336 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800337 public static final int STREAM_DEFAULT = -1;
338
339 /**
340 * The audio stream type to use when playing the sound.
341 * Should be one of the STREAM_ constants from
342 * {@link android.media.AudioManager}.
John Spurlockc0650f022014-07-19 13:22:39 -0400343 *
344 * @deprecated Use {@link #audioAttributes} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800345 */
Jean-Michel Trivi81f871e2014-08-06 16:32:38 -0700346 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800347 public int audioStreamType = STREAM_DEFAULT;
348
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800349 /**
John Spurlockc0650f022014-07-19 13:22:39 -0400350 * The default value of {@link #audioAttributes}.
351 */
352 public static final AudioAttributes AUDIO_ATTRIBUTES_DEFAULT = new AudioAttributes.Builder()
353 .setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
354 .setUsage(AudioAttributes.USAGE_NOTIFICATION)
355 .build();
356
357 /**
358 * The {@link AudioAttributes audio attributes} to use when playing the sound.
359 */
360 public AudioAttributes audioAttributes = AUDIO_ATTRIBUTES_DEFAULT;
361
362 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500363 * The pattern with which to vibrate.
364 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800365 * <p>
366 * To vibrate the default pattern, see {@link #defaults}.
367 * </p>
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500368 *
Chris Wren47c20a12014-06-18 17:27:29 -0400369 * <p>
370 * A notification that vibrates is more likely to be presented as a heads-up notification.
371 * </p>
372 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800373 * @see android.os.Vibrator#vibrate(long[],int)
374 */
375 public long[] vibrate;
376
377 /**
378 * The color of the led. The hardware will do its best approximation.
379 *
380 * @see #FLAG_SHOW_LIGHTS
381 * @see #flags
382 */
Tor Norbye80756e32015-03-02 09:39:27 -0800383 @ColorInt
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800384 public int ledARGB;
385
386 /**
387 * The number of milliseconds for the LED to be on while it's flashing.
388 * The hardware will do its best approximation.
389 *
390 * @see #FLAG_SHOW_LIGHTS
391 * @see #flags
392 */
393 public int ledOnMS;
394
395 /**
396 * The number of milliseconds for the LED to be off while it's flashing.
397 * The hardware will do its best approximation.
398 *
399 * @see #FLAG_SHOW_LIGHTS
400 * @see #flags
401 */
402 public int ledOffMS;
403
404 /**
405 * Specifies which values should be taken from the defaults.
406 * <p>
407 * To set, OR the desired from {@link #DEFAULT_SOUND},
408 * {@link #DEFAULT_VIBRATE}, {@link #DEFAULT_LIGHTS}. For all default
409 * values, use {@link #DEFAULT_ALL}.
410 * </p>
411 */
412 public int defaults;
413
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800414 /**
415 * Bit to be bitwise-ored into the {@link #flags} field that should be
416 * set if you want the LED on for this notification.
417 * <ul>
418 * <li>To turn the LED off, pass 0 in the alpha channel for colorARGB
419 * or 0 for both ledOnMS and ledOffMS.</li>
420 * <li>To turn the LED on, pass 1 for ledOnMS and 0 for ledOffMS.</li>
421 * <li>To flash the LED, pass the number of milliseconds that it should
422 * be on and off to ledOnMS and ledOffMS.</li>
423 * </ul>
424 * <p>
425 * Since hardware varies, you are not guaranteed that any of the values
426 * you pass are honored exactly. Use the system defaults (TODO) if possible
427 * because they will be set to values that work on any given hardware.
428 * <p>
429 * The alpha channel must be set for forward compatibility.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500430 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800431 */
432 public static final int FLAG_SHOW_LIGHTS = 0x00000001;
433
434 /**
435 * Bit to be bitwise-ored into the {@link #flags} field that should be
436 * set if this notification is in reference to something that is ongoing,
437 * like a phone call. It should not be set if this notification is in
438 * reference to something that happened at a particular point in time,
439 * like a missed phone call.
440 */
441 public static final int FLAG_ONGOING_EVENT = 0x00000002;
442
443 /**
444 * Bit to be bitwise-ored into the {@link #flags} field that if set,
Scott Mainb8b36452009-04-26 15:50:49 -0700445 * the audio will be repeated until the notification is
446 * cancelled or the notification window is opened.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800447 */
448 public static final int FLAG_INSISTENT = 0x00000004;
449
450 /**
451 * Bit to be bitwise-ored into the {@link #flags} field that should be
Griff Hazen293977b2014-04-28 08:37:20 -0700452 * set if you would only like the sound, vibrate and ticker to be played
453 * if the notification was not already showing.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800454 */
455 public static final int FLAG_ONLY_ALERT_ONCE = 0x00000008;
456
457 /**
458 * Bit to be bitwise-ored into the {@link #flags} field that should be
459 * set if the notification should be canceled when it is clicked by the
Daniel Sandler8aa9ae62012-12-04 23:31:47 -0500460 * user.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800461 */
462 public static final int FLAG_AUTO_CANCEL = 0x00000010;
463
464 /**
465 * Bit to be bitwise-ored into the {@link #flags} field that should be
466 * set if the notification should not be canceled when the user clicks
467 * the Clear all button.
468 */
469 public static final int FLAG_NO_CLEAR = 0x00000020;
470
Dianne Hackbornd8a43f62009-08-17 23:33:56 -0700471 /**
472 * Bit to be bitwise-ored into the {@link #flags} field that should be
473 * set if this notification represents a currently running service. This
474 * will normally be set for you by {@link Service#startForeground}.
475 */
476 public static final int FLAG_FOREGROUND_SERVICE = 0x00000040;
477
Daniel Sandlere46cbd32010-06-17 10:35:26 -0400478 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500479 * Obsolete flag indicating high-priority notifications; use the priority field instead.
Joe Malin8d40d042012-11-05 11:36:40 -0800480 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500481 * @deprecated Use {@link #priority} with a positive value.
Daniel Sandlere46cbd32010-06-17 10:35:26 -0400482 */
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500483 public static final int FLAG_HIGH_PRIORITY = 0x00000080;
Daniel Sandlere46cbd32010-06-17 10:35:26 -0400484
Griff Hazendfcb0802014-02-11 12:00:00 -0800485 /**
486 * Bit to be bitswise-ored into the {@link #flags} field that should be
487 * set if this notification is relevant to the current device only
488 * and it is not recommended that it bridge to other devices.
489 */
490 public static final int FLAG_LOCAL_ONLY = 0x00000100;
491
Griff Hazen5cadc3b2014-05-20 09:55:39 -0700492 /**
493 * Bit to be bitswise-ored into the {@link #flags} field that should be
494 * set if this notification is the group summary for a group of notifications.
495 * Grouped notifications may display in a cluster or stack on devices which
496 * support such rendering. Requires a group key also be set using {@link Builder#setGroup}.
497 */
498 public static final int FLAG_GROUP_SUMMARY = 0x00000200;
499
Julia Reynoldse46bb372016-03-17 11:05:58 -0400500 /**
501 * Bit to be bitswise-ored into the {@link #flags} field that should be
502 * set if this notification is the group summary for an auto-group of notifications.
503 *
504 * @hide
505 */
506 @SystemApi
507 public static final int FLAG_AUTOGROUP_SUMMARY = 0x00000400;
508
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800509 public int flags;
510
Tor Norbyed9273d62013-05-30 15:59:53 -0700511 /** @hide */
512 @IntDef({PRIORITY_DEFAULT,PRIORITY_LOW,PRIORITY_MIN,PRIORITY_HIGH,PRIORITY_MAX})
513 @Retention(RetentionPolicy.SOURCE)
514 public @interface Priority {}
515
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800516 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500517 * Default notification {@link #priority}. If your application does not prioritize its own
518 * notifications, use this value for all notifications.
519 */
520 public static final int PRIORITY_DEFAULT = 0;
521
522 /**
523 * Lower {@link #priority}, for items that are less important. The UI may choose to show these
524 * items smaller, or at a different position in the list, compared with your app's
525 * {@link #PRIORITY_DEFAULT} items.
526 */
527 public static final int PRIORITY_LOW = -1;
528
529 /**
530 * Lowest {@link #priority}; these items might not be shown to the user except under special
531 * circumstances, such as detailed notification logs.
532 */
533 public static final int PRIORITY_MIN = -2;
534
535 /**
536 * Higher {@link #priority}, for more important notifications or alerts. The UI may choose to
537 * show these items larger, or at a different position in notification lists, compared with
538 * your app's {@link #PRIORITY_DEFAULT} items.
539 */
540 public static final int PRIORITY_HIGH = 1;
541
542 /**
543 * Highest {@link #priority}, for your application's most important items that require the
544 * user's prompt attention or input.
545 */
546 public static final int PRIORITY_MAX = 2;
547
548 /**
549 * Relative priority for this notification.
Joe Malin8d40d042012-11-05 11:36:40 -0800550 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500551 * Priority is an indication of how much of the user's valuable attention should be consumed by
552 * this notification. Low-priority notifications may be hidden from the user in certain
553 * situations, while the user might be interrupted for a higher-priority notification. The
Daniel Sandler6738eee2012-11-16 12:03:32 -0500554 * system will make a determination about how to interpret this priority when presenting
555 * the notification.
Chris Wren47c20a12014-06-18 17:27:29 -0400556 *
557 * <p>
558 * A notification that is at least {@link #PRIORITY_HIGH} is more likely to be presented
559 * as a heads-up notification.
560 * </p>
561 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500562 */
Tor Norbyed9273d62013-05-30 15:59:53 -0700563 @Priority
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500564 public int priority;
Joe Malin8d40d042012-11-05 11:36:40 -0800565
Dan Sandler26e81cf2014-05-06 10:01:27 -0400566 /**
567 * Accent color (an ARGB integer like the constants in {@link android.graphics.Color})
568 * to be applied by the standard Style templates when presenting this notification.
569 *
570 * The current template design constructs a colorful header image by overlaying the
571 * {@link #icon} image (stenciled in white) atop a field of this color. Alpha components are
572 * ignored.
573 */
Tor Norbye80756e32015-03-02 09:39:27 -0800574 @ColorInt
Dan Sandler26e81cf2014-05-06 10:01:27 -0400575 public int color = COLOR_DEFAULT;
576
577 /**
578 * Special value of {@link #color} telling the system not to decorate this notification with
579 * any special color but instead use default colors when presenting this notification.
580 */
Tor Norbye80756e32015-03-02 09:39:27 -0800581 @ColorInt
Dan Sandler26e81cf2014-05-06 10:01:27 -0400582 public static final int COLOR_DEFAULT = 0; // AKA Color.TRANSPARENT
Dan Sandler0bf2ed82013-12-21 23:33:41 -0600583
584 /**
Adrian Roos4ff3b122016-02-01 12:26:13 -0800585 * Special value of {@link #color} used as a place holder for an invalid color.
586 */
587 @ColorInt
588 private static final int COLOR_INVALID = 1;
589
590 /**
Dan Sandler0bf2ed82013-12-21 23:33:41 -0600591 * Sphere of visibility of this notification, which affects how and when the SystemUI reveals
592 * the notification's presence and contents in untrusted situations (namely, on the secure
593 * lockscreen).
594 *
595 * The default level, {@link #VISIBILITY_PRIVATE}, behaves exactly as notifications have always
596 * done on Android: The notification's {@link #icon} and {@link #tickerText} (if available) are
597 * shown in all situations, but the contents are only available if the device is unlocked for
598 * the appropriate user.
599 *
600 * A more permissive policy can be expressed by {@link #VISIBILITY_PUBLIC}; such a notification
601 * can be read even in an "insecure" context (that is, above a secure lockscreen).
602 * To modify the public version of this notification—for example, to redact some portions—see
603 * {@link Builder#setPublicVersion(Notification)}.
604 *
605 * Finally, a notification can be made {@link #VISIBILITY_SECRET}, which will suppress its icon
606 * and ticker until the user has bypassed the lockscreen.
607 */
608 public int visibility;
609
Griff Hazenfc3922d2014-08-20 11:56:44 -0700610 /**
611 * Notification visibility: Show this notification in its entirety on all lockscreens.
612 *
613 * {@see #visibility}
614 */
Dan Sandler0bf2ed82013-12-21 23:33:41 -0600615 public static final int VISIBILITY_PUBLIC = 1;
Griff Hazenfc3922d2014-08-20 11:56:44 -0700616
617 /**
618 * Notification visibility: Show this notification on all lockscreens, but conceal sensitive or
619 * private information on secure lockscreens.
620 *
621 * {@see #visibility}
622 */
Dan Sandler0bf2ed82013-12-21 23:33:41 -0600623 public static final int VISIBILITY_PRIVATE = 0;
Griff Hazenfc3922d2014-08-20 11:56:44 -0700624
625 /**
626 * Notification visibility: Do not reveal any part of this notification on a secure lockscreen.
627 *
628 * {@see #visibility}
629 */
Dan Sandler0bf2ed82013-12-21 23:33:41 -0600630 public static final int VISIBILITY_SECRET = -1;
631
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500632 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400633 * Notification category: incoming call (voice or video) or similar synchronous communication request.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500634 */
John Spurlockfd7f1e02014-03-18 16:41:57 -0400635 public static final String CATEGORY_CALL = "call";
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500636
637 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400638 * Notification category: incoming direct message (SMS, instant message, etc.).
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500639 */
John Spurlockfd7f1e02014-03-18 16:41:57 -0400640 public static final String CATEGORY_MESSAGE = "msg";
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500641
642 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400643 * Notification category: asynchronous bulk message (email).
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500644 */
John Spurlockfd7f1e02014-03-18 16:41:57 -0400645 public static final String CATEGORY_EMAIL = "email";
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500646
647 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400648 * Notification category: calendar event.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500649 */
John Spurlockfd7f1e02014-03-18 16:41:57 -0400650 public static final String CATEGORY_EVENT = "event";
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500651
652 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400653 * Notification category: promotion or advertisement.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500654 */
John Spurlockfd7f1e02014-03-18 16:41:57 -0400655 public static final String CATEGORY_PROMO = "promo";
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500656
657 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400658 * Notification category: alarm or timer.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500659 */
John Spurlockfd7f1e02014-03-18 16:41:57 -0400660 public static final String CATEGORY_ALARM = "alarm";
661
662 /**
663 * Notification category: progress of a long-running background operation.
664 */
665 public static final String CATEGORY_PROGRESS = "progress";
666
667 /**
668 * Notification category: social network or sharing update.
669 */
670 public static final String CATEGORY_SOCIAL = "social";
671
672 /**
673 * Notification category: error in background operation or authentication status.
674 */
675 public static final String CATEGORY_ERROR = "err";
676
677 /**
678 * Notification category: media transport control for playback.
679 */
680 public static final String CATEGORY_TRANSPORT = "transport";
681
682 /**
683 * Notification category: system or device status update. Reserved for system use.
684 */
685 public static final String CATEGORY_SYSTEM = "sys";
686
687 /**
688 * Notification category: indication of running background service.
689 */
690 public static final String CATEGORY_SERVICE = "service";
691
692 /**
John Spurlock0a69c8c2014-03-21 13:30:57 -0400693 * Notification category: a specific, timely recommendation for a single thing.
694 * For example, a news app might want to recommend a news story it believes the user will
695 * want to read next.
696 */
697 public static final String CATEGORY_RECOMMENDATION = "recommendation";
698
699 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400700 * Notification category: ongoing information about device or contextual status.
701 */
702 public static final String CATEGORY_STATUS = "status";
703
704 /**
John Spurlock24d3dad2015-04-02 12:24:02 -0400705 * Notification category: user-scheduled reminder.
706 */
707 public static final String CATEGORY_REMINDER = "reminder";
708
709 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400710 * One of the predefined notification categories (see the <code>CATEGORY_*</code> constants)
711 * that best describes this Notification. May be used by the system for ranking and filtering.
712 */
713 public String category;
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500714
Griff Hazen5cadc3b2014-05-20 09:55:39 -0700715 private String mGroupKey;
716
717 /**
718 * Get the key used to group this notification into a cluster or stack
719 * with other notifications on devices which support such rendering.
720 */
721 public String getGroup() {
722 return mGroupKey;
723 }
724
725 private String mSortKey;
726
727 /**
728 * Get a sort key that orders this notification among other notifications from the
729 * same package. This can be useful if an external sort was already applied and an app
730 * would like to preserve this. Notifications will be sorted lexicographically using this
731 * value, although providing different priorities in addition to providing sort key may
732 * cause this value to be ignored.
733 *
734 * <p>This sort key can also be used to order members of a notification group. See
735 * {@link Builder#setGroup}.
736 *
737 * @see String#compareTo(String)
738 */
739 public String getSortKey() {
740 return mSortKey;
741 }
742
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500743 /**
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400744 * Additional semantic data to be carried around with this Notification.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400745 * <p>
746 * The extras keys defined here are intended to capture the original inputs to {@link Builder}
747 * APIs, and are intended to be used by
748 * {@link android.service.notification.NotificationListenerService} implementations to extract
749 * detailed information from notification objects.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500750 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400751 public Bundle extras = new Bundle();
752
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400753 /**
754 * {@link #extras} key: this is the title of the notification,
755 * as supplied to {@link Builder#setContentTitle(CharSequence)}.
756 */
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -0500757 public static final String EXTRA_TITLE = "android.title";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400758
759 /**
760 * {@link #extras} key: this is the title of the notification when shown in expanded form,
761 * e.g. as supplied to {@link BigTextStyle#setBigContentTitle(CharSequence)}.
762 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400763 public static final String EXTRA_TITLE_BIG = EXTRA_TITLE + ".big";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400764
765 /**
766 * {@link #extras} key: this is the main text payload, as supplied to
767 * {@link Builder#setContentText(CharSequence)}.
768 */
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -0500769 public static final String EXTRA_TEXT = "android.text";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400770
771 /**
772 * {@link #extras} key: this is a third line of text, as supplied to
773 * {@link Builder#setSubText(CharSequence)}.
774 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400775 public static final String EXTRA_SUB_TEXT = "android.subText";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400776
777 /**
Adrian Roose458aa82015-12-08 16:17:19 -0800778 * {@link #extras} key: this is the remote input history, as supplied to
779 * {@link Builder#setRemoteInputHistory(CharSequence[])}.
780 */
781 public static final String EXTRA_REMOTE_INPUT_HISTORY = "android.remoteInputHistory";
782
783 /**
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400784 * {@link #extras} key: this is a small piece of additional text as supplied to
785 * {@link Builder#setContentInfo(CharSequence)}.
786 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400787 public static final String EXTRA_INFO_TEXT = "android.infoText";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400788
789 /**
790 * {@link #extras} key: this is a line of summary information intended to be shown
791 * alongside expanded notifications, as supplied to (e.g.)
792 * {@link BigTextStyle#setSummaryText(CharSequence)}.
793 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400794 public static final String EXTRA_SUMMARY_TEXT = "android.summaryText";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400795
796 /**
Christoph Studer4600f9b2014-07-22 22:44:43 +0200797 * {@link #extras} key: this is the longer text shown in the big form of a
798 * {@link BigTextStyle} notification, as supplied to
799 * {@link BigTextStyle#bigText(CharSequence)}.
800 */
801 public static final String EXTRA_BIG_TEXT = "android.bigText";
802
803 /**
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400804 * {@link #extras} key: this is the resource ID of the notification's main small icon, as
805 * supplied to {@link Builder#setSmallIcon(int)}.
806 */
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -0500807 public static final String EXTRA_SMALL_ICON = "android.icon";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400808
809 /**
810 * {@link #extras} key: this is a bitmap to be used instead of the small icon when showing the
811 * notification payload, as
812 * supplied to {@link Builder#setLargeIcon(android.graphics.Bitmap)}.
813 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400814 public static final String EXTRA_LARGE_ICON = "android.largeIcon";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400815
816 /**
817 * {@link #extras} key: this is a bitmap to be used instead of the one from
818 * {@link Builder#setLargeIcon(android.graphics.Bitmap)} when the notification is
819 * shown in its expanded form, as supplied to
820 * {@link BigPictureStyle#bigLargeIcon(android.graphics.Bitmap)}.
821 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400822 public static final String EXTRA_LARGE_ICON_BIG = EXTRA_LARGE_ICON + ".big";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400823
824 /**
825 * {@link #extras} key: this is the progress value supplied to
826 * {@link Builder#setProgress(int, int, boolean)}.
827 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400828 public static final String EXTRA_PROGRESS = "android.progress";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400829
830 /**
831 * {@link #extras} key: this is the maximum value supplied to
832 * {@link Builder#setProgress(int, int, boolean)}.
833 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400834 public static final String EXTRA_PROGRESS_MAX = "android.progressMax";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400835
836 /**
837 * {@link #extras} key: whether the progress bar is indeterminate, supplied to
838 * {@link Builder#setProgress(int, int, boolean)}.
839 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400840 public static final String EXTRA_PROGRESS_INDETERMINATE = "android.progressIndeterminate";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400841
842 /**
843 * {@link #extras} key: whether {@link #when} should be shown as a count-up timer (specifically
844 * a {@link android.widget.Chronometer}) instead of a timestamp, as supplied to
845 * {@link Builder#setUsesChronometer(boolean)}.
846 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400847 public static final String EXTRA_SHOW_CHRONOMETER = "android.showChronometer";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400848
849 /**
Selim Cinek81c23aa2016-02-25 16:23:13 -0800850 * {@link #extras} key: whether the chronometer set on the notification should count down
851 * instead of counting up. Is only relevant if key {@link #EXTRA_SHOW_CHRONOMETER} is present.
852 */
853 public static final String EXTRA_CHRONOMETER_COUNTS_DOWN = "android.chronometerCountsDown";
854
855 /**
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400856 * {@link #extras} key: whether {@link #when} should be shown,
857 * as supplied to {@link Builder#setShowWhen(boolean)}.
858 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400859 public static final String EXTRA_SHOW_WHEN = "android.showWhen";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400860
861 /**
862 * {@link #extras} key: this is a bitmap to be shown in {@link BigPictureStyle} expanded
863 * notifications, supplied to {@link BigPictureStyle#bigPicture(android.graphics.Bitmap)}.
864 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400865 public static final String EXTRA_PICTURE = "android.picture";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400866
867 /**
868 * {@link #extras} key: An array of CharSequences to show in {@link InboxStyle} expanded
869 * notifications, each of which was supplied to {@link InboxStyle#addLine(CharSequence)}.
870 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400871 public static final String EXTRA_TEXT_LINES = "android.textLines";
Dan Sandler842dd772014-05-15 09:36:47 -0400872
873 /**
874 * {@link #extras} key: A string representing the name of the specific
875 * {@link android.app.Notification.Style} used to create this notification.
876 */
Chris Wren91ad5632013-06-05 15:05:57 -0400877 public static final String EXTRA_TEMPLATE = "android.template";
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400878
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400879 /**
Chris Wrene6c48932014-09-29 17:19:27 -0400880 * {@link #extras} key: A String array containing the people that this notification relates to,
881 * each of which was supplied to {@link Builder#addPerson(String)}.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400882 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400883 public static final String EXTRA_PEOPLE = "android.people";
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500884
885 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400886 * Allow certain system-generated notifications to appear before the device is provisioned.
887 * Only available to notifications coming from the android package.
888 * @hide
889 */
890 public static final String EXTRA_ALLOW_DURING_SETUP = "android.allowDuringSetup";
891
892 /**
Jose Limae9e3b3b2014-05-18 23:44:50 -0700893 * {@link #extras} key: A
894 * {@link android.content.ContentUris content URI} pointing to an image that can be displayed
895 * in the background when the notification is selected. The URI must point to an image stream
896 * suitable for passing into
897 * {@link android.graphics.BitmapFactory#decodeStream(java.io.InputStream)
898 * BitmapFactory.decodeStream}; all other content types will be ignored. The content provider
899 * URI used for this purpose must require no permissions to read the image data.
900 */
901 public static final String EXTRA_BACKGROUND_IMAGE_URI = "android.backgroundImageUri";
902
903 /**
Dan Sandler842dd772014-05-15 09:36:47 -0400904 * {@link #extras} key: A
Jeff Browndba34ba2014-06-24 20:46:03 -0700905 * {@link android.media.session.MediaSession.Token} associated with a
Dan Sandler842dd772014-05-15 09:36:47 -0400906 * {@link android.app.Notification.MediaStyle} notification.
907 */
908 public static final String EXTRA_MEDIA_SESSION = "android.mediaSession";
909
910 /**
Bryan Mawhinneye191f902014-07-22 12:50:09 +0100911 * {@link #extras} key: the indices of actions to be shown in the compact view,
912 * as supplied to (e.g.) {@link MediaStyle#setShowActionsInCompactView(int...)}.
913 */
914 public static final String EXTRA_COMPACT_ACTIONS = "android.compactActions";
915
Christoph Studer943aa672014-08-03 20:31:16 +0200916 /**
Alex Hillsfc737de2016-03-23 17:33:02 -0400917 * {@link #extras} key: the username to be displayed for all messages sent by the user including
918 * direct replies
919 * {@link android.app.Notification.MessagingStyle} notification.
920 */
921 public static final String EXTRA_SELF_DISPLAY_NAME = "android.selfDisplayName";
922
923 /**
924 * {@link #extras} key: a boolean describing whether the platform should automatically
925 * generate possible replies to
926 * {@link android.app.Notification.MessagingStyle.Message} objects provided by a
927 * {@link android.app.Notification.MessagingStyle} notification.
928 */
929 public static final String EXTRA_ALLOW_GENERATED_REPLIES = "android.allowGeneratedReplies";
930
931 /**
932 * {@link #extras} key: a {@link String} to be displayed as the title to a thread represented by
933 * a {@link android.app.Notification.MessagingStyle}
934 */
935 public static final String EXTRA_THREAD_TITLE = "android.threadTitle";
936
937 /**
938 * {@link #extras} key: an array of {@link android.app.Notification.MessagingStyle.Message}
939 * bundles provided by a
940 * {@link android.app.Notification.MessagingStyle} notification.
941 */
942 public static final String EXTRA_MESSAGES = "android.messages";
943
944 /**
Kenny Guy8942bcd2014-09-08 21:09:47 +0100945 * {@link #extras} key: the user that built the notification.
946 *
947 * @hide
948 */
949 public static final String EXTRA_ORIGINATING_USERID = "android.originatingUserId";
950
951 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400952 * @hide
953 */
954 public static final String EXTRA_BUILDER_APPLICATION_INFO = "android.appInfo";
955
Selim Cinek247fa012016-02-18 09:50:48 -0800956 /**
957 * @hide
958 */
959 public static final String EXTRA_CONTAINS_CUSTOM_VIEW = "android.contains.customView";
960
Dan Sandlerd63f9322015-05-06 15:18:49 -0400961 private Icon mSmallIcon;
962 private Icon mLargeIcon;
963
Chris Wren51c75102013-07-16 20:49:17 -0400964 /**
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400965 * Structure to encapsulate a named action that can be shown as part of this notification.
966 * It must include an icon, a label, and a {@link PendingIntent} to be fired when the action is
967 * selected by the user.
968 * <p>
Griff Hazen959591e2014-05-15 22:26:18 -0700969 * Apps should use {@link Notification.Builder#addAction(int, CharSequence, PendingIntent)}
970 * or {@link Notification.Builder#addAction(Notification.Action)}
971 * to attach actions.
Daniel Sandlera0a938c2012-03-15 08:42:37 -0400972 */
Daniel Sandlerea2a3172013-02-20 22:24:20 -0500973 public static class Action implements Parcelable {
Griff Hazen959591e2014-05-15 22:26:18 -0700974 private final Bundle mExtras;
Dan Sandler86647982015-05-13 23:41:13 -0400975 private Icon mIcon;
Griff Hazen61a9e862014-05-22 16:05:19 -0700976 private final RemoteInput[] mRemoteInputs;
Griff Hazen959591e2014-05-15 22:26:18 -0700977
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400978 /**
979 * Small icon representing the action.
Dan Sandler86647982015-05-13 23:41:13 -0400980 *
981 * @deprecated Use {@link Action#getIcon()} instead.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400982 */
Dan Sandler86647982015-05-13 23:41:13 -0400983 @Deprecated
Daniel Sandlera0a938c2012-03-15 08:42:37 -0400984 public int icon;
Griff Hazen5cadc3b2014-05-20 09:55:39 -0700985
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400986 /**
987 * Title of the action.
988 */
Daniel Sandlera0a938c2012-03-15 08:42:37 -0400989 public CharSequence title;
Griff Hazen5cadc3b2014-05-20 09:55:39 -0700990
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400991 /**
992 * Intent to send when the user invokes this action. May be null, in which case the action
993 * may be rendered in a disabled presentation by the system UI.
994 */
Daniel Sandlera0a938c2012-03-15 08:42:37 -0400995 public PendingIntent actionIntent;
Griff Hazen959591e2014-05-15 22:26:18 -0700996
Daniel Sandlera0a938c2012-03-15 08:42:37 -0400997 private Action(Parcel in) {
Dan Sandler86647982015-05-13 23:41:13 -0400998 if (in.readInt() != 0) {
999 mIcon = Icon.CREATOR.createFromParcel(in);
Dan Sandler68079d52015-07-22 10:45:30 -04001000 if (mIcon.getType() == Icon.TYPE_RESOURCE) {
1001 icon = mIcon.getResId();
1002 }
Dan Sandler86647982015-05-13 23:41:13 -04001003 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001004 title = TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(in);
1005 if (in.readInt() == 1) {
1006 actionIntent = PendingIntent.CREATOR.createFromParcel(in);
1007 }
Jeff Sharkeya04c7a72016-03-18 12:20:36 -06001008 mExtras = Bundle.setDefusable(in.readBundle(), true);
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001009 mRemoteInputs = in.createTypedArray(RemoteInput.CREATOR);
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001010 }
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001011
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001012 /**
Dan Sandler86647982015-05-13 23:41:13 -04001013 * @deprecated Use {@link android.app.Notification.Action.Builder}.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001014 */
Dan Sandler86647982015-05-13 23:41:13 -04001015 @Deprecated
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001016 public Action(int icon, CharSequence title, PendingIntent intent) {
Dan Sandler86647982015-05-13 23:41:13 -04001017 this(Icon.createWithResource("", icon), title, intent, new Bundle(), null);
Griff Hazen959591e2014-05-15 22:26:18 -07001018 }
1019
Dan Sandler86647982015-05-13 23:41:13 -04001020 private Action(Icon icon, CharSequence title, PendingIntent intent, Bundle extras,
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001021 RemoteInput[] remoteInputs) {
Dan Sandler86647982015-05-13 23:41:13 -04001022 this.mIcon = icon;
Gus Prevasf5bff46f2015-08-24 10:34:28 -04001023 if (icon != null && icon.getType() == Icon.TYPE_RESOURCE) {
1024 this.icon = icon.getResId();
1025 }
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001026 this.title = title;
1027 this.actionIntent = intent;
Griff Hazen959591e2014-05-15 22:26:18 -07001028 this.mExtras = extras != null ? extras : new Bundle();
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001029 this.mRemoteInputs = remoteInputs;
Griff Hazen959591e2014-05-15 22:26:18 -07001030 }
1031
1032 /**
Dan Sandler86647982015-05-13 23:41:13 -04001033 * Return an icon representing the action.
1034 */
1035 public Icon getIcon() {
1036 if (mIcon == null && icon != 0) {
1037 // you snuck an icon in here without using the builder; let's try to keep it
1038 mIcon = Icon.createWithResource("", icon);
1039 }
1040 return mIcon;
1041 }
1042
1043 /**
Griff Hazen959591e2014-05-15 22:26:18 -07001044 * Get additional metadata carried around with this Action.
1045 */
1046 public Bundle getExtras() {
1047 return mExtras;
1048 }
1049
1050 /**
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001051 * Get the list of inputs to be collected from the user when this action is sent.
1052 * May return null if no remote inputs were added.
1053 */
1054 public RemoteInput[] getRemoteInputs() {
1055 return mRemoteInputs;
1056 }
1057
1058 /**
Griff Hazen959591e2014-05-15 22:26:18 -07001059 * Builder class for {@link Action} objects.
1060 */
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001061 public static final class Builder {
Dan Sandler86647982015-05-13 23:41:13 -04001062 private final Icon mIcon;
Griff Hazen959591e2014-05-15 22:26:18 -07001063 private final CharSequence mTitle;
1064 private final PendingIntent mIntent;
1065 private final Bundle mExtras;
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001066 private ArrayList<RemoteInput> mRemoteInputs;
Griff Hazen959591e2014-05-15 22:26:18 -07001067
1068 /**
1069 * Construct a new builder for {@link Action} object.
1070 * @param icon icon to show for this action
1071 * @param title the title of the action
1072 * @param intent the {@link PendingIntent} to fire when users trigger this action
1073 */
Dan Sandler86647982015-05-13 23:41:13 -04001074 @Deprecated
Griff Hazen959591e2014-05-15 22:26:18 -07001075 public Builder(int icon, CharSequence title, PendingIntent intent) {
Dan Sandler86647982015-05-13 23:41:13 -04001076 this(Icon.createWithResource("", icon), title, intent, new Bundle(), null);
1077 }
1078
1079 /**
1080 * Construct a new builder for {@link Action} object.
1081 * @param icon icon to show for this action
1082 * @param title the title of the action
1083 * @param intent the {@link PendingIntent} to fire when users trigger this action
1084 */
1085 public Builder(Icon icon, CharSequence title, PendingIntent intent) {
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001086 this(icon, title, intent, new Bundle(), null);
Griff Hazen959591e2014-05-15 22:26:18 -07001087 }
1088
1089 /**
1090 * Construct a new builder for {@link Action} object using the fields from an
1091 * {@link Action}.
1092 * @param action the action to read fields from.
1093 */
1094 public Builder(Action action) {
Dan Sandler86647982015-05-13 23:41:13 -04001095 this(action.getIcon(), action.title, action.actionIntent, new Bundle(action.mExtras),
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001096 action.getRemoteInputs());
Griff Hazen959591e2014-05-15 22:26:18 -07001097 }
1098
Dan Sandler86647982015-05-13 23:41:13 -04001099 private Builder(Icon icon, CharSequence title, PendingIntent intent, Bundle extras,
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001100 RemoteInput[] remoteInputs) {
Griff Hazen959591e2014-05-15 22:26:18 -07001101 mIcon = icon;
1102 mTitle = title;
1103 mIntent = intent;
1104 mExtras = extras;
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001105 if (remoteInputs != null) {
1106 mRemoteInputs = new ArrayList<RemoteInput>(remoteInputs.length);
1107 Collections.addAll(mRemoteInputs, remoteInputs);
1108 }
Griff Hazen959591e2014-05-15 22:26:18 -07001109 }
1110
1111 /**
1112 * Merge additional metadata into this builder.
1113 *
1114 * <p>Values within the Bundle will replace existing extras values in this Builder.
1115 *
1116 * @see Notification.Action#extras
1117 */
1118 public Builder addExtras(Bundle extras) {
1119 if (extras != null) {
1120 mExtras.putAll(extras);
1121 }
1122 return this;
1123 }
1124
1125 /**
1126 * Get the metadata Bundle used by this Builder.
1127 *
1128 * <p>The returned Bundle is shared with this Builder.
1129 */
1130 public Bundle getExtras() {
1131 return mExtras;
1132 }
1133
1134 /**
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001135 * Add an input to be collected from the user when this action is sent.
1136 * Response values can be retrieved from the fired intent by using the
1137 * {@link RemoteInput#getResultsFromIntent} function.
1138 * @param remoteInput a {@link RemoteInput} to add to the action
1139 * @return this object for method chaining
1140 */
1141 public Builder addRemoteInput(RemoteInput remoteInput) {
1142 if (mRemoteInputs == null) {
1143 mRemoteInputs = new ArrayList<RemoteInput>();
1144 }
1145 mRemoteInputs.add(remoteInput);
1146 return this;
1147 }
1148
1149 /**
1150 * Apply an extender to this action builder. Extenders may be used to add
1151 * metadata or change options on this builder.
1152 */
Griff Hazen61a9e862014-05-22 16:05:19 -07001153 public Builder extend(Extender extender) {
1154 extender.extend(this);
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001155 return this;
1156 }
1157
1158 /**
Griff Hazen959591e2014-05-15 22:26:18 -07001159 * Combine all of the options that have been set and return a new {@link Action}
1160 * object.
1161 * @return the built action
1162 */
1163 public Action build() {
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001164 RemoteInput[] remoteInputs = mRemoteInputs != null
1165 ? mRemoteInputs.toArray(new RemoteInput[mRemoteInputs.size()]) : null;
1166 return new Action(mIcon, mTitle, mIntent, mExtras, remoteInputs);
Griff Hazen959591e2014-05-15 22:26:18 -07001167 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001168 }
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001169
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001170 @Override
1171 public Action clone() {
1172 return new Action(
Dan Sandler86647982015-05-13 23:41:13 -04001173 getIcon(),
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001174 title,
1175 actionIntent, // safe to alias
1176 new Bundle(mExtras),
1177 getRemoteInputs());
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001178 }
1179 @Override
1180 public int describeContents() {
1181 return 0;
1182 }
1183 @Override
1184 public void writeToParcel(Parcel out, int flags) {
Dan Sandler86647982015-05-13 23:41:13 -04001185 final Icon ic = getIcon();
1186 if (ic != null) {
1187 out.writeInt(1);
1188 ic.writeToParcel(out, 0);
1189 } else {
1190 out.writeInt(0);
1191 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001192 TextUtils.writeToParcel(title, out, flags);
1193 if (actionIntent != null) {
1194 out.writeInt(1);
1195 actionIntent.writeToParcel(out, flags);
1196 } else {
1197 out.writeInt(0);
1198 }
Griff Hazen959591e2014-05-15 22:26:18 -07001199 out.writeBundle(mExtras);
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001200 out.writeTypedArray(mRemoteInputs, flags);
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001201 }
Griff Hazen959591e2014-05-15 22:26:18 -07001202 public static final Parcelable.Creator<Action> CREATOR =
1203 new Parcelable.Creator<Action>() {
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001204 public Action createFromParcel(Parcel in) {
1205 return new Action(in);
1206 }
1207 public Action[] newArray(int size) {
1208 return new Action[size];
1209 }
1210 };
Griff Hazen61a9e862014-05-22 16:05:19 -07001211
1212 /**
1213 * Extender interface for use with {@link Builder#extend}. Extenders may be used to add
1214 * metadata or change options on an action builder.
1215 */
1216 public interface Extender {
1217 /**
1218 * Apply this extender to a notification action builder.
1219 * @param builder the builder to be modified.
1220 * @return the build object for chaining.
1221 */
1222 public Builder extend(Builder builder);
1223 }
1224
1225 /**
1226 * Wearable extender for notification actions. To add extensions to an action,
1227 * create a new {@link android.app.Notification.Action.WearableExtender} object using
1228 * the {@code WearableExtender()} constructor and apply it to a
1229 * {@link android.app.Notification.Action.Builder} using
1230 * {@link android.app.Notification.Action.Builder#extend}.
1231 *
1232 * <pre class="prettyprint">
1233 * Notification.Action action = new Notification.Action.Builder(
1234 * R.drawable.archive_all, "Archive all", actionIntent)
Griff Hazen14f57992014-05-26 09:07:14 -07001235 * .extend(new Notification.Action.WearableExtender()
Griff Hazen61a9e862014-05-22 16:05:19 -07001236 * .setAvailableOffline(false))
Griff Hazen14f57992014-05-26 09:07:14 -07001237 * .build();</pre>
Griff Hazen61a9e862014-05-22 16:05:19 -07001238 */
1239 public static final class WearableExtender implements Extender {
1240 /** Notification action extra which contains wearable extensions */
1241 private static final String EXTRA_WEARABLE_EXTENSIONS = "android.wearable.EXTENSIONS";
1242
Pete Gastaf6781d2014-10-07 15:17:05 -04001243 // Keys within EXTRA_WEARABLE_EXTENSIONS for wearable options.
Griff Hazen61a9e862014-05-22 16:05:19 -07001244 private static final String KEY_FLAGS = "flags";
Pete Gastaf6781d2014-10-07 15:17:05 -04001245 private static final String KEY_IN_PROGRESS_LABEL = "inProgressLabel";
1246 private static final String KEY_CONFIRM_LABEL = "confirmLabel";
1247 private static final String KEY_CANCEL_LABEL = "cancelLabel";
Griff Hazen61a9e862014-05-22 16:05:19 -07001248
1249 // Flags bitwise-ored to mFlags
1250 private static final int FLAG_AVAILABLE_OFFLINE = 0x1;
Alex Hills9ab3a232016-04-05 14:54:56 -04001251 private static final int FLAG_HINT_LAUNCHES_ACTIVITY = 1 << 1;
Griff Hazen61a9e862014-05-22 16:05:19 -07001252
1253 // Default value for flags integer
1254 private static final int DEFAULT_FLAGS = FLAG_AVAILABLE_OFFLINE;
1255
1256 private int mFlags = DEFAULT_FLAGS;
1257
Pete Gastaf6781d2014-10-07 15:17:05 -04001258 private CharSequence mInProgressLabel;
1259 private CharSequence mConfirmLabel;
1260 private CharSequence mCancelLabel;
1261
Griff Hazen61a9e862014-05-22 16:05:19 -07001262 /**
1263 * Create a {@link android.app.Notification.Action.WearableExtender} with default
1264 * options.
1265 */
1266 public WearableExtender() {
1267 }
1268
1269 /**
1270 * Create a {@link android.app.Notification.Action.WearableExtender} by reading
1271 * wearable options present in an existing notification action.
1272 * @param action the notification action to inspect.
1273 */
1274 public WearableExtender(Action action) {
1275 Bundle wearableBundle = action.getExtras().getBundle(EXTRA_WEARABLE_EXTENSIONS);
1276 if (wearableBundle != null) {
1277 mFlags = wearableBundle.getInt(KEY_FLAGS, DEFAULT_FLAGS);
Pete Gastaf6781d2014-10-07 15:17:05 -04001278 mInProgressLabel = wearableBundle.getCharSequence(KEY_IN_PROGRESS_LABEL);
1279 mConfirmLabel = wearableBundle.getCharSequence(KEY_CONFIRM_LABEL);
1280 mCancelLabel = wearableBundle.getCharSequence(KEY_CANCEL_LABEL);
Griff Hazen61a9e862014-05-22 16:05:19 -07001281 }
1282 }
1283
1284 /**
1285 * Apply wearable extensions to a notification action that is being built. This is
1286 * typically called by the {@link android.app.Notification.Action.Builder#extend}
1287 * method of {@link android.app.Notification.Action.Builder}.
1288 */
1289 @Override
1290 public Action.Builder extend(Action.Builder builder) {
1291 Bundle wearableBundle = new Bundle();
1292
1293 if (mFlags != DEFAULT_FLAGS) {
1294 wearableBundle.putInt(KEY_FLAGS, mFlags);
1295 }
Pete Gastaf6781d2014-10-07 15:17:05 -04001296 if (mInProgressLabel != null) {
1297 wearableBundle.putCharSequence(KEY_IN_PROGRESS_LABEL, mInProgressLabel);
1298 }
1299 if (mConfirmLabel != null) {
1300 wearableBundle.putCharSequence(KEY_CONFIRM_LABEL, mConfirmLabel);
1301 }
1302 if (mCancelLabel != null) {
1303 wearableBundle.putCharSequence(KEY_CANCEL_LABEL, mCancelLabel);
1304 }
Griff Hazen61a9e862014-05-22 16:05:19 -07001305
1306 builder.getExtras().putBundle(EXTRA_WEARABLE_EXTENSIONS, wearableBundle);
1307 return builder;
1308 }
1309
1310 @Override
1311 public WearableExtender clone() {
1312 WearableExtender that = new WearableExtender();
1313 that.mFlags = this.mFlags;
Pete Gastaf6781d2014-10-07 15:17:05 -04001314 that.mInProgressLabel = this.mInProgressLabel;
1315 that.mConfirmLabel = this.mConfirmLabel;
1316 that.mCancelLabel = this.mCancelLabel;
Griff Hazen61a9e862014-05-22 16:05:19 -07001317 return that;
1318 }
1319
1320 /**
1321 * Set whether this action is available when the wearable device is not connected to
1322 * a companion device. The user can still trigger this action when the wearable device is
1323 * offline, but a visual hint will indicate that the action may not be available.
1324 * Defaults to true.
1325 */
1326 public WearableExtender setAvailableOffline(boolean availableOffline) {
1327 setFlag(FLAG_AVAILABLE_OFFLINE, availableOffline);
1328 return this;
1329 }
1330
1331 /**
1332 * Get whether this action is available when the wearable device is not connected to
1333 * a companion device. The user can still trigger this action when the wearable device is
1334 * offline, but a visual hint will indicate that the action may not be available.
1335 * Defaults to true.
1336 */
1337 public boolean isAvailableOffline() {
1338 return (mFlags & FLAG_AVAILABLE_OFFLINE) != 0;
1339 }
1340
1341 private void setFlag(int mask, boolean value) {
1342 if (value) {
1343 mFlags |= mask;
1344 } else {
1345 mFlags &= ~mask;
1346 }
1347 }
Pete Gastaf6781d2014-10-07 15:17:05 -04001348
1349 /**
1350 * Set a label to display while the wearable is preparing to automatically execute the
1351 * action. This is usually a 'ing' verb ending in ellipsis like "Sending..."
1352 *
1353 * @param label the label to display while the action is being prepared to execute
1354 * @return this object for method chaining
1355 */
1356 public WearableExtender setInProgressLabel(CharSequence label) {
1357 mInProgressLabel = label;
1358 return this;
1359 }
1360
1361 /**
1362 * Get the label to display while the wearable is preparing to automatically execute
1363 * the action. This is usually a 'ing' verb ending in ellipsis like "Sending..."
1364 *
1365 * @return the label to display while the action is being prepared to execute
1366 */
1367 public CharSequence getInProgressLabel() {
1368 return mInProgressLabel;
1369 }
1370
1371 /**
1372 * Set a label to display to confirm that the action should be executed.
1373 * This is usually an imperative verb like "Send".
1374 *
1375 * @param label the label to confirm the action should be executed
1376 * @return this object for method chaining
1377 */
1378 public WearableExtender setConfirmLabel(CharSequence label) {
1379 mConfirmLabel = label;
1380 return this;
1381 }
1382
1383 /**
1384 * Get the label to display to confirm that the action should be executed.
1385 * This is usually an imperative verb like "Send".
1386 *
1387 * @return the label to confirm the action should be executed
1388 */
1389 public CharSequence getConfirmLabel() {
1390 return mConfirmLabel;
1391 }
1392
1393 /**
1394 * Set a label to display to cancel the action.
1395 * This is usually an imperative verb, like "Cancel".
1396 *
1397 * @param label the label to display to cancel the action
1398 * @return this object for method chaining
1399 */
1400 public WearableExtender setCancelLabel(CharSequence label) {
1401 mCancelLabel = label;
1402 return this;
1403 }
1404
1405 /**
1406 * Get the label to display to cancel the action.
1407 * This is usually an imperative verb like "Cancel".
1408 *
1409 * @return the label to display to cancel the action
1410 */
1411 public CharSequence getCancelLabel() {
1412 return mCancelLabel;
1413 }
Alex Hills9ab3a232016-04-05 14:54:56 -04001414
1415 /**
1416 * Set a hint that this Action will launch an {@link Activity} directly, telling the
1417 * platform that it can generate the appropriate transitions.
1418 * @param hintLaunchesActivity {@code true} if the content intent will launch
1419 * an activity and transitions should be generated, false otherwise.
1420 * @return this object for method chaining
1421 */
1422 public WearableExtender setHintContentIntentLaunchesActivity(
1423 boolean hintLaunchesActivity) {
1424 setFlag(FLAG_HINT_LAUNCHES_ACTIVITY, hintLaunchesActivity);
1425 return this;
1426 }
1427
1428 /**
1429 * Get a hint that this Action will launch an {@link Activity} directly, telling the
1430 * platform that it can generate the appropriate transitions
1431 * @return {@code true} if the content intent will launch an activity and transitions
1432 * should be generated, false otherwise. The default value is {@code false} if this was
1433 * never set.
1434 */
1435 public boolean getHintContentIntentLaunchesActivity() {
1436 return (mFlags & FLAG_HINT_LAUNCHES_ACTIVITY) != 0;
1437 }
Griff Hazen61a9e862014-05-22 16:05:19 -07001438 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001439 }
1440
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001441 /**
1442 * Array of all {@link Action} structures attached to this notification by
1443 * {@link Builder#addAction(int, CharSequence, PendingIntent)}. Mostly useful for instances of
1444 * {@link android.service.notification.NotificationListenerService} that provide an alternative
1445 * interface for invoking actions.
1446 */
Daniel Sandlerea2a3172013-02-20 22:24:20 -05001447 public Action[] actions;
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001448
1449 /**
Dan Sandler0bf2ed82013-12-21 23:33:41 -06001450 * Replacement version of this notification whose content will be shown
1451 * in an insecure context such as atop a secure keyguard. See {@link #visibility}
1452 * and {@link #VISIBILITY_PUBLIC}.
1453 */
1454 public Notification publicVersion;
1455
1456 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001457 * Constructs a Notification object with default values.
Joe Onorato46439ce2010-11-19 13:56:21 -08001458 * You might want to consider using {@link Builder} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001459 */
1460 public Notification()
1461 {
1462 this.when = System.currentTimeMillis();
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001463 this.priority = PRIORITY_DEFAULT;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001464 }
1465
1466 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001467 * @hide
1468 */
1469 public Notification(Context context, int icon, CharSequence tickerText, long when,
1470 CharSequence contentTitle, CharSequence contentText, Intent contentIntent)
1471 {
Chris Wren1ce4b6d2015-06-11 10:19:43 -04001472 new Builder(context)
1473 .setWhen(when)
1474 .setSmallIcon(icon)
1475 .setTicker(tickerText)
1476 .setContentTitle(contentTitle)
1477 .setContentText(contentText)
1478 .setContentIntent(PendingIntent.getActivity(context, 0, contentIntent, 0))
1479 .buildInto(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001480 }
1481
1482 /**
1483 * Constructs a Notification object with the information needed to
1484 * have a status bar icon without the standard expanded view.
1485 *
1486 * @param icon The resource id of the icon to put in the status bar.
1487 * @param tickerText The text that flows by in the status bar when the notification first
1488 * activates.
1489 * @param when The time to show in the time field. In the System.currentTimeMillis
1490 * timebase.
Joe Onorato46439ce2010-11-19 13:56:21 -08001491 *
1492 * @deprecated Use {@link Builder} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001493 */
Joe Onorato46439ce2010-11-19 13:56:21 -08001494 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001495 public Notification(int icon, CharSequence tickerText, long when)
1496 {
1497 this.icon = icon;
1498 this.tickerText = tickerText;
1499 this.when = when;
1500 }
1501
1502 /**
1503 * Unflatten the notification from a parcel.
1504 */
1505 public Notification(Parcel parcel)
1506 {
1507 int version = parcel.readInt();
1508
1509 when = parcel.readLong();
Dan Sandler3936e7a2015-05-19 20:59:12 -04001510 if (parcel.readInt() != 0) {
1511 mSmallIcon = Icon.CREATOR.createFromParcel(parcel);
Dan Sandler4e787062015-06-17 15:09:48 -04001512 if (mSmallIcon.getType() == Icon.TYPE_RESOURCE) {
1513 icon = mSmallIcon.getResId();
1514 }
Dan Sandler3936e7a2015-05-19 20:59:12 -04001515 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001516 number = parcel.readInt();
1517 if (parcel.readInt() != 0) {
1518 contentIntent = PendingIntent.CREATOR.createFromParcel(parcel);
1519 }
1520 if (parcel.readInt() != 0) {
1521 deleteIntent = PendingIntent.CREATOR.createFromParcel(parcel);
1522 }
1523 if (parcel.readInt() != 0) {
1524 tickerText = TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(parcel);
1525 }
1526 if (parcel.readInt() != 0) {
Joe Onorato46439ce2010-11-19 13:56:21 -08001527 tickerView = RemoteViews.CREATOR.createFromParcel(parcel);
Joe Onoratoef1e7762010-09-17 18:38:38 -04001528 }
1529 if (parcel.readInt() != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001530 contentView = RemoteViews.CREATOR.createFromParcel(parcel);
1531 }
Joe Onorato561d3852010-11-20 18:09:34 -08001532 if (parcel.readInt() != 0) {
Dan Sandlerd63f9322015-05-06 15:18:49 -04001533 mLargeIcon = Icon.CREATOR.createFromParcel(parcel);
Joe Onorato561d3852010-11-20 18:09:34 -08001534 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001535 defaults = parcel.readInt();
1536 flags = parcel.readInt();
1537 if (parcel.readInt() != 0) {
1538 sound = Uri.CREATOR.createFromParcel(parcel);
1539 }
1540
1541 audioStreamType = parcel.readInt();
John Spurlockc0650f022014-07-19 13:22:39 -04001542 if (parcel.readInt() != 0) {
1543 audioAttributes = AudioAttributes.CREATOR.createFromParcel(parcel);
1544 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001545 vibrate = parcel.createLongArray();
1546 ledARGB = parcel.readInt();
1547 ledOnMS = parcel.readInt();
1548 ledOffMS = parcel.readInt();
1549 iconLevel = parcel.readInt();
Daniel Sandlere46cbd32010-06-17 10:35:26 -04001550
1551 if (parcel.readInt() != 0) {
1552 fullScreenIntent = PendingIntent.CREATOR.createFromParcel(parcel);
1553 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001554
1555 priority = parcel.readInt();
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001556
John Spurlockfd7f1e02014-03-18 16:41:57 -04001557 category = parcel.readString();
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001558
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001559 mGroupKey = parcel.readString();
1560
1561 mSortKey = parcel.readString();
1562
Jeff Sharkeya04c7a72016-03-18 12:20:36 -06001563 extras = Bundle.setDefusable(parcel.readBundle(), true); // may be null
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001564
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001565 actions = parcel.createTypedArray(Action.CREATOR); // may be null
1566
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001567 if (parcel.readInt() != 0) {
Daniel Sandlerf3b73432012-03-27 15:01:25 -04001568 bigContentView = RemoteViews.CREATOR.createFromParcel(parcel);
1569 }
Dan Sandler0bf2ed82013-12-21 23:33:41 -06001570
Chris Wren8fd39ec2014-02-27 17:43:26 -05001571 if (parcel.readInt() != 0) {
1572 headsUpContentView = RemoteViews.CREATOR.createFromParcel(parcel);
1573 }
1574
Dan Sandler0bf2ed82013-12-21 23:33:41 -06001575 visibility = parcel.readInt();
1576
1577 if (parcel.readInt() != 0) {
1578 publicVersion = Notification.CREATOR.createFromParcel(parcel);
1579 }
Dan Sandler26e81cf2014-05-06 10:01:27 -04001580
1581 color = parcel.readInt();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001582 }
1583
Andy Stadler110988c2010-12-03 14:29:16 -08001584 @Override
Joe Onorato18e69df2010-05-17 22:26:12 -07001585 public Notification clone() {
1586 Notification that = new Notification();
Daniel Sandler1a497d32013-04-18 14:52:45 -04001587 cloneInto(that, true);
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001588 return that;
1589 }
Joe Onorato18e69df2010-05-17 22:26:12 -07001590
Daniel Sandler1a497d32013-04-18 14:52:45 -04001591 /**
1592 * Copy all (or if heavy is false, all except Bitmaps and RemoteViews) members
1593 * of this into that.
1594 * @hide
1595 */
1596 public void cloneInto(Notification that, boolean heavy) {
Joe Onorato18e69df2010-05-17 22:26:12 -07001597 that.when = this.when;
Dan Sandlerd63f9322015-05-06 15:18:49 -04001598 that.mSmallIcon = this.mSmallIcon;
Joe Onorato18e69df2010-05-17 22:26:12 -07001599 that.number = this.number;
1600
1601 // PendingIntents are global, so there's no reason (or way) to clone them.
1602 that.contentIntent = this.contentIntent;
1603 that.deleteIntent = this.deleteIntent;
Daniel Sandlere46cbd32010-06-17 10:35:26 -04001604 that.fullScreenIntent = this.fullScreenIntent;
Joe Onorato18e69df2010-05-17 22:26:12 -07001605
1606 if (this.tickerText != null) {
1607 that.tickerText = this.tickerText.toString();
1608 }
Daniel Sandler1a497d32013-04-18 14:52:45 -04001609 if (heavy && this.tickerView != null) {
Joe Onorato46439ce2010-11-19 13:56:21 -08001610 that.tickerView = this.tickerView.clone();
Joe Onoratoef1e7762010-09-17 18:38:38 -04001611 }
Daniel Sandler1a497d32013-04-18 14:52:45 -04001612 if (heavy && this.contentView != null) {
Joe Onorato18e69df2010-05-17 22:26:12 -07001613 that.contentView = this.contentView.clone();
1614 }
Dan Sandlerd63f9322015-05-06 15:18:49 -04001615 if (heavy && this.mLargeIcon != null) {
1616 that.mLargeIcon = this.mLargeIcon;
Joe Onorato561d3852010-11-20 18:09:34 -08001617 }
Jozef BABJAKa8b91832011-02-22 08:05:08 +01001618 that.iconLevel = this.iconLevel;
Joe Onorato18e69df2010-05-17 22:26:12 -07001619 that.sound = this.sound; // android.net.Uri is immutable
1620 that.audioStreamType = this.audioStreamType;
John Spurlockc0650f022014-07-19 13:22:39 -04001621 if (this.audioAttributes != null) {
1622 that.audioAttributes = new AudioAttributes.Builder(this.audioAttributes).build();
1623 }
Joe Onorato18e69df2010-05-17 22:26:12 -07001624
1625 final long[] vibrate = this.vibrate;
1626 if (vibrate != null) {
1627 final int N = vibrate.length;
1628 final long[] vib = that.vibrate = new long[N];
1629 System.arraycopy(vibrate, 0, vib, 0, N);
1630 }
1631
1632 that.ledARGB = this.ledARGB;
1633 that.ledOnMS = this.ledOnMS;
1634 that.ledOffMS = this.ledOffMS;
1635 that.defaults = this.defaults;
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001636
Joe Onorato18e69df2010-05-17 22:26:12 -07001637 that.flags = this.flags;
1638
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001639 that.priority = this.priority;
Joe Malin8d40d042012-11-05 11:36:40 -08001640
John Spurlockfd7f1e02014-03-18 16:41:57 -04001641 that.category = this.category;
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001642
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001643 that.mGroupKey = this.mGroupKey;
1644
1645 that.mSortKey = this.mSortKey;
1646
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001647 if (this.extras != null) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04001648 try {
1649 that.extras = new Bundle(this.extras);
1650 // will unparcel
1651 that.extras.size();
1652 } catch (BadParcelableException e) {
1653 Log.e(TAG, "could not unparcel extras from notification: " + this, e);
1654 that.extras = null;
1655 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001656 }
1657
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001658 if (this.actions != null) {
1659 that.actions = new Action[this.actions.length];
1660 for(int i=0; i<this.actions.length; i++) {
1661 that.actions[i] = this.actions[i].clone();
1662 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001663 }
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001664
Daniel Sandler1a497d32013-04-18 14:52:45 -04001665 if (heavy && this.bigContentView != null) {
Daniel Sandlerf3b73432012-03-27 15:01:25 -04001666 that.bigContentView = this.bigContentView.clone();
1667 }
Daniel Sandler1a497d32013-04-18 14:52:45 -04001668
Chris Wren8fd39ec2014-02-27 17:43:26 -05001669 if (heavy && this.headsUpContentView != null) {
1670 that.headsUpContentView = this.headsUpContentView.clone();
1671 }
1672
Dan Sandler0bf2ed82013-12-21 23:33:41 -06001673 that.visibility = this.visibility;
1674
1675 if (this.publicVersion != null) {
1676 that.publicVersion = new Notification();
1677 this.publicVersion.cloneInto(that.publicVersion, heavy);
1678 }
1679
Dan Sandler26e81cf2014-05-06 10:01:27 -04001680 that.color = this.color;
1681
Daniel Sandler1a497d32013-04-18 14:52:45 -04001682 if (!heavy) {
1683 that.lightenPayload(); // will clean out extras
1684 }
1685 }
1686
1687 /**
1688 * Removes heavyweight parts of the Notification object for archival or for sending to
1689 * listeners when the full contents are not necessary.
1690 * @hide
1691 */
1692 public final void lightenPayload() {
1693 tickerView = null;
1694 contentView = null;
1695 bigContentView = null;
Chris Wren8fd39ec2014-02-27 17:43:26 -05001696 headsUpContentView = null;
Dan Sandlerd63f9322015-05-06 15:18:49 -04001697 mLargeIcon = null;
Dan Sandler50128532015-12-08 15:42:41 -05001698 if (extras != null && !extras.isEmpty()) {
1699 final Set<String> keyset = extras.keySet();
1700 final int N = keyset.size();
1701 final String[] keys = keyset.toArray(new String[N]);
1702 for (int i=0; i<N; i++) {
1703 final String key = keys[i];
1704 final Object obj = extras.get(key);
1705 if (obj != null &&
1706 ( obj instanceof Parcelable
1707 || obj instanceof Parcelable[]
1708 || obj instanceof SparseArray
1709 || obj instanceof ArrayList)) {
1710 extras.remove(key);
1711 }
1712 }
Daniel Sandler1a497d32013-04-18 14:52:45 -04001713 }
Joe Onorato18e69df2010-05-17 22:26:12 -07001714 }
1715
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04001716 /**
1717 * Make sure this CharSequence is safe to put into a bundle, which basically
1718 * means it had better not be some custom Parcelable implementation.
1719 * @hide
1720 */
1721 public static CharSequence safeCharSequence(CharSequence cs) {
Christoph Studer535ec612014-09-03 15:47:47 +02001722 if (cs == null) return cs;
1723 if (cs.length() > MAX_CHARSEQUENCE_LENGTH) {
1724 cs = cs.subSequence(0, MAX_CHARSEQUENCE_LENGTH);
1725 }
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04001726 if (cs instanceof Parcelable) {
1727 Log.e(TAG, "warning: " + cs.getClass().getCanonicalName()
1728 + " instance is a custom Parcelable and not allowed in Notification");
1729 return cs.toString();
1730 }
Selim Cinek60a54252016-02-26 17:03:25 -08001731 return removeTextSizeSpans(cs);
1732 }
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04001733
Selim Cinek60a54252016-02-26 17:03:25 -08001734 private static CharSequence removeTextSizeSpans(CharSequence charSequence) {
1735 if (charSequence instanceof Spanned) {
1736 Spanned ss = (Spanned) charSequence;
1737 Object[] spans = ss.getSpans(0, ss.length(), Object.class);
1738 SpannableStringBuilder builder = new SpannableStringBuilder(ss.toString());
1739 for (Object span : spans) {
1740 Object resultSpan = span;
Selim Cinek89991a22016-03-07 19:51:54 -08001741 if (resultSpan instanceof CharacterStyle) {
1742 resultSpan = ((CharacterStyle) span).getUnderlying();
1743 }
1744 if (resultSpan instanceof TextAppearanceSpan) {
1745 TextAppearanceSpan originalSpan = (TextAppearanceSpan) resultSpan;
Selim Cinek60a54252016-02-26 17:03:25 -08001746 resultSpan = new TextAppearanceSpan(
1747 originalSpan.getFamily(),
1748 originalSpan.getTextStyle(),
1749 -1,
1750 originalSpan.getTextColor(),
1751 originalSpan.getLinkTextColor());
Selim Cinek89991a22016-03-07 19:51:54 -08001752 } else if (resultSpan instanceof RelativeSizeSpan
1753 || resultSpan instanceof AbsoluteSizeSpan) {
Selim Cinek60a54252016-02-26 17:03:25 -08001754 continue;
Selim Cinek89991a22016-03-07 19:51:54 -08001755 } else {
1756 resultSpan = span;
Selim Cinek60a54252016-02-26 17:03:25 -08001757 }
1758 builder.setSpan(resultSpan, ss.getSpanStart(span), ss.getSpanEnd(span),
1759 ss.getSpanFlags(span));
1760 }
1761 return builder;
1762 }
1763 return charSequence;
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04001764 }
1765
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001766 public int describeContents() {
1767 return 0;
1768 }
1769
1770 /**
Dan Sandler4e787062015-06-17 15:09:48 -04001771 * Flatten this notification into a parcel.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001772 */
1773 public void writeToParcel(Parcel parcel, int flags)
1774 {
1775 parcel.writeInt(1);
1776
1777 parcel.writeLong(when);
Dan Sandler4e787062015-06-17 15:09:48 -04001778 if (mSmallIcon == null && icon != 0) {
1779 // you snuck an icon in here without using the builder; let's try to keep it
1780 mSmallIcon = Icon.createWithResource("", icon);
1781 }
Dan Sandler3936e7a2015-05-19 20:59:12 -04001782 if (mSmallIcon != null) {
1783 parcel.writeInt(1);
1784 mSmallIcon.writeToParcel(parcel, 0);
1785 } else {
1786 parcel.writeInt(0);
1787 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001788 parcel.writeInt(number);
1789 if (contentIntent != null) {
1790 parcel.writeInt(1);
1791 contentIntent.writeToParcel(parcel, 0);
1792 } else {
1793 parcel.writeInt(0);
1794 }
1795 if (deleteIntent != null) {
1796 parcel.writeInt(1);
1797 deleteIntent.writeToParcel(parcel, 0);
1798 } else {
1799 parcel.writeInt(0);
1800 }
1801 if (tickerText != null) {
1802 parcel.writeInt(1);
1803 TextUtils.writeToParcel(tickerText, parcel, flags);
1804 } else {
1805 parcel.writeInt(0);
1806 }
Joe Onorato46439ce2010-11-19 13:56:21 -08001807 if (tickerView != null) {
Joe Onoratoef1e7762010-09-17 18:38:38 -04001808 parcel.writeInt(1);
Joe Onorato46439ce2010-11-19 13:56:21 -08001809 tickerView.writeToParcel(parcel, 0);
Joe Onoratoef1e7762010-09-17 18:38:38 -04001810 } else {
1811 parcel.writeInt(0);
1812 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001813 if (contentView != null) {
1814 parcel.writeInt(1);
1815 contentView.writeToParcel(parcel, 0);
1816 } else {
1817 parcel.writeInt(0);
1818 }
Dan Sandlerd63f9322015-05-06 15:18:49 -04001819 if (mLargeIcon != null) {
Joe Onorato561d3852010-11-20 18:09:34 -08001820 parcel.writeInt(1);
Dan Sandlerd63f9322015-05-06 15:18:49 -04001821 mLargeIcon.writeToParcel(parcel, 0);
Joe Onorato561d3852010-11-20 18:09:34 -08001822 } else {
1823 parcel.writeInt(0);
1824 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001825
1826 parcel.writeInt(defaults);
1827 parcel.writeInt(this.flags);
1828
1829 if (sound != null) {
1830 parcel.writeInt(1);
1831 sound.writeToParcel(parcel, 0);
1832 } else {
1833 parcel.writeInt(0);
1834 }
1835 parcel.writeInt(audioStreamType);
John Spurlockc0650f022014-07-19 13:22:39 -04001836
1837 if (audioAttributes != null) {
1838 parcel.writeInt(1);
1839 audioAttributes.writeToParcel(parcel, 0);
1840 } else {
1841 parcel.writeInt(0);
1842 }
1843
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001844 parcel.writeLongArray(vibrate);
1845 parcel.writeInt(ledARGB);
1846 parcel.writeInt(ledOnMS);
1847 parcel.writeInt(ledOffMS);
1848 parcel.writeInt(iconLevel);
Daniel Sandlere46cbd32010-06-17 10:35:26 -04001849
1850 if (fullScreenIntent != null) {
1851 parcel.writeInt(1);
1852 fullScreenIntent.writeToParcel(parcel, 0);
1853 } else {
1854 parcel.writeInt(0);
1855 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001856
1857 parcel.writeInt(priority);
Joe Malin8d40d042012-11-05 11:36:40 -08001858
John Spurlockfd7f1e02014-03-18 16:41:57 -04001859 parcel.writeString(category);
Joe Malin8d40d042012-11-05 11:36:40 -08001860
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001861 parcel.writeString(mGroupKey);
1862
1863 parcel.writeString(mSortKey);
1864
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001865 parcel.writeBundle(extras); // null ok
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001866
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001867 parcel.writeTypedArray(actions, 0); // null ok
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001868
Daniel Sandlerf3b73432012-03-27 15:01:25 -04001869 if (bigContentView != null) {
1870 parcel.writeInt(1);
1871 bigContentView.writeToParcel(parcel, 0);
1872 } else {
1873 parcel.writeInt(0);
1874 }
Dan Sandler0bf2ed82013-12-21 23:33:41 -06001875
Chris Wren8fd39ec2014-02-27 17:43:26 -05001876 if (headsUpContentView != null) {
1877 parcel.writeInt(1);
1878 headsUpContentView.writeToParcel(parcel, 0);
1879 } else {
1880 parcel.writeInt(0);
1881 }
1882
Dan Sandler0bf2ed82013-12-21 23:33:41 -06001883 parcel.writeInt(visibility);
1884
1885 if (publicVersion != null) {
1886 parcel.writeInt(1);
1887 publicVersion.writeToParcel(parcel, 0);
1888 } else {
1889 parcel.writeInt(0);
1890 }
Dan Sandler26e81cf2014-05-06 10:01:27 -04001891
1892 parcel.writeInt(color);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001893 }
1894
1895 /**
1896 * Parcelable.Creator that instantiates Notification objects
1897 */
1898 public static final Parcelable.Creator<Notification> CREATOR
1899 = new Parcelable.Creator<Notification>()
1900 {
1901 public Notification createFromParcel(Parcel parcel)
1902 {
1903 return new Notification(parcel);
1904 }
1905
1906 public Notification[] newArray(int size)
1907 {
1908 return new Notification[size];
1909 }
1910 };
1911
1912 /**
1913 * Sets the {@link #contentView} field to be a view with the standard "Latest Event"
1914 * layout.
1915 *
1916 * <p>Uses the {@link #icon} and {@link #when} fields to set the icon and time fields
1917 * in the view.</p>
1918 * @param context The context for your application / activity.
1919 * @param contentTitle The title that goes in the expanded entry.
1920 * @param contentText The text that goes in the expanded entry.
1921 * @param contentIntent The intent to launch when the user clicks the expanded notification.
1922 * If this is an activity, it must include the
1923 * {@link android.content.Intent#FLAG_ACTIVITY_NEW_TASK} flag, which requires
Scott Main7aee61f2011-02-08 11:25:01 -08001924 * that you take care of task management as described in the
1925 * <a href="{@docRoot}guide/topics/fundamentals/tasks-and-back-stack.html">Tasks and Back
1926 * Stack</a> document.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001927 *
Joe Onorato46439ce2010-11-19 13:56:21 -08001928 * @deprecated Use {@link Builder} instead.
Chris Wrena05db382015-06-24 15:18:34 -04001929 * @removed
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001930 */
Joe Onorato46439ce2010-11-19 13:56:21 -08001931 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001932 public void setLatestEventInfo(Context context,
1933 CharSequence contentTitle, CharSequence contentText, PendingIntent contentIntent) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04001934 if (context.getApplicationInfo().targetSdkVersion > Build.VERSION_CODES.LOLLIPOP_MR1){
1935 Log.e(TAG, "setLatestEventInfo() is deprecated and you should feel deprecated.",
1936 new Throwable());
1937 }
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001938
Julia Reynoldsd9228f12015-10-20 10:37:27 -04001939 // ensure that any information already set directly is preserved
1940 final Notification.Builder builder = new Notification.Builder(context, this);
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001941
1942 // now apply the latestEventInfo fields
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001943 if (contentTitle != null) {
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001944 builder.setContentTitle(contentTitle);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001945 }
1946 if (contentText != null) {
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001947 builder.setContentText(contentText);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001948 }
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001949 builder.setContentIntent(contentIntent);
Julia Reynoldsd9228f12015-10-20 10:37:27 -04001950
1951 builder.build(); // callers expect this notification to be ready to use
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001952 }
1953
Julia Reynoldsda303542015-11-23 14:00:20 -05001954 /**
1955 * @hide
1956 */
1957 public static void addFieldsFromContext(Context context, Notification notification) {
Jeff Sharkey012bc7b2016-04-11 16:30:27 -06001958 addFieldsFromContext(context.getApplicationInfo(), context.getUserId(), notification);
1959 }
1960
1961 /**
1962 * @hide
1963 */
1964 public static void addFieldsFromContext(ApplicationInfo ai, int userId,
1965 Notification notification) {
1966 notification.extras.putParcelable(EXTRA_BUILDER_APPLICATION_INFO, ai);
1967 notification.extras.putInt(EXTRA_ORIGINATING_USERID, userId);
Julia Reynoldsda303542015-11-23 14:00:20 -05001968 }
1969
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001970 @Override
1971 public String toString() {
1972 StringBuilder sb = new StringBuilder();
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001973 sb.append("Notification(pri=");
1974 sb.append(priority);
1975 sb.append(" contentView=");
Joe Onoratoc9596d62011-01-12 17:03:11 -08001976 if (contentView != null) {
1977 sb.append(contentView.getPackage());
1978 sb.append("/0x");
1979 sb.append(Integer.toHexString(contentView.getLayoutId()));
1980 } else {
1981 sb.append("null");
1982 }
1983 sb.append(" vibrate=");
Daniel Sandler6738eee2012-11-16 12:03:32 -05001984 if ((this.defaults & DEFAULT_VIBRATE) != 0) {
1985 sb.append("default");
1986 } else if (this.vibrate != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001987 int N = this.vibrate.length-1;
1988 sb.append("[");
1989 for (int i=0; i<N; i++) {
1990 sb.append(this.vibrate[i]);
1991 sb.append(',');
1992 }
Simon Schoar8cf97d92009-06-10 22:08:37 +02001993 if (N != -1) {
1994 sb.append(this.vibrate[N]);
1995 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001996 sb.append("]");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001997 } else {
1998 sb.append("null");
1999 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002000 sb.append(" sound=");
Daniel Sandler6738eee2012-11-16 12:03:32 -05002001 if ((this.defaults & DEFAULT_SOUND) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002002 sb.append("default");
Daniel Sandler6738eee2012-11-16 12:03:32 -05002003 } else if (this.sound != null) {
2004 sb.append(this.sound.toString());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002005 } else {
2006 sb.append("null");
2007 }
Chris Wren365b6d32015-07-16 10:39:26 -04002008 if (this.tickerText != null) {
2009 sb.append(" tick");
2010 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002011 sb.append(" defaults=0x");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002012 sb.append(Integer.toHexString(this.defaults));
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002013 sb.append(" flags=0x");
Daniel Sandlere46cbd32010-06-17 10:35:26 -04002014 sb.append(Integer.toHexString(this.flags));
Dan Sandler26e81cf2014-05-06 10:01:27 -04002015 sb.append(String.format(" color=0x%08x", this.color));
Griff Hazen5cadc3b2014-05-20 09:55:39 -07002016 if (this.category != null) {
2017 sb.append(" category=");
2018 sb.append(this.category);
2019 }
2020 if (this.mGroupKey != null) {
2021 sb.append(" groupKey=");
2022 sb.append(this.mGroupKey);
2023 }
2024 if (this.mSortKey != null) {
2025 sb.append(" sortKey=");
2026 sb.append(this.mSortKey);
2027 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002028 if (actions != null) {
Dan Sandler1b718782014-07-18 12:43:45 -04002029 sb.append(" actions=");
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002030 sb.append(actions.length);
Dan Sandler1b718782014-07-18 12:43:45 -04002031 }
2032 sb.append(" vis=");
2033 sb.append(visibilityToString(this.visibility));
2034 if (this.publicVersion != null) {
2035 sb.append(" publicVersion=");
2036 sb.append(publicVersion.toString());
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002037 }
2038 sb.append(")");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002039 return sb.toString();
2040 }
Joe Onorato46439ce2010-11-19 13:56:21 -08002041
Dan Sandler1b718782014-07-18 12:43:45 -04002042 /**
2043 * {@hide}
2044 */
2045 public static String visibilityToString(int vis) {
2046 switch (vis) {
2047 case VISIBILITY_PRIVATE:
2048 return "PRIVATE";
2049 case VISIBILITY_PUBLIC:
2050 return "PUBLIC";
2051 case VISIBILITY_SECRET:
2052 return "SECRET";
2053 default:
2054 return "UNKNOWN(" + String.valueOf(vis) + ")";
2055 }
2056 }
2057
Joe Onoratocb109a02011-01-18 17:57:41 -08002058 /**
John Spurlock1d881a12015-03-18 19:21:54 -04002059 * {@hide}
2060 */
2061 public static String priorityToString(@Priority int pri) {
2062 switch (pri) {
2063 case PRIORITY_MIN:
2064 return "MIN";
2065 case PRIORITY_LOW:
2066 return "LOW";
2067 case PRIORITY_DEFAULT:
2068 return "DEFAULT";
2069 case PRIORITY_HIGH:
2070 return "HIGH";
2071 case PRIORITY_MAX:
2072 return "MAX";
2073 default:
2074 return "UNKNOWN(" + String.valueOf(pri) + ")";
2075 }
2076 }
2077
2078 /**
Dan Sandlerd63f9322015-05-06 15:18:49 -04002079 * The small icon representing this notification in the status bar and content view.
2080 *
2081 * @return the small icon representing this notification.
2082 *
2083 * @see Builder#getSmallIcon()
2084 * @see Builder#setSmallIcon(Icon)
2085 */
2086 public Icon getSmallIcon() {
2087 return mSmallIcon;
2088 }
2089
2090 /**
2091 * Used when notifying to clean up legacy small icons.
2092 * @hide
2093 */
2094 public void setSmallIcon(Icon icon) {
2095 mSmallIcon = icon;
2096 }
2097
2098 /**
2099 * The large icon shown in this notification's content view.
2100 * @see Builder#getLargeIcon()
2101 * @see Builder#setLargeIcon(Icon)
2102 */
2103 public Icon getLargeIcon() {
2104 return mLargeIcon;
2105 }
2106
2107 /**
Christoph Studer4600f9b2014-07-22 22:44:43 +02002108 * @hide
2109 */
Christoph Studerc8db24b2014-07-25 17:50:30 +02002110 public boolean isGroupSummary() {
2111 return mGroupKey != null && (flags & FLAG_GROUP_SUMMARY) != 0;
2112 }
2113
2114 /**
2115 * @hide
2116 */
2117 public boolean isGroupChild() {
2118 return mGroupKey != null && (flags & FLAG_GROUP_SUMMARY) == 0;
2119 }
2120
2121 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002122 * Builder class for {@link Notification} objects.
Joe Malin8d40d042012-11-05 11:36:40 -08002123 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002124 * Provides a convenient way to set the various fields of a {@link Notification} and generate
Scott Main183bf112012-08-13 19:12:13 -07002125 * content views using the platform's notification layout template. If your app supports
2126 * versions of Android as old as API level 4, you can instead use
2127 * {@link android.support.v4.app.NotificationCompat.Builder NotificationCompat.Builder},
2128 * available in the <a href="{@docRoot}tools/extras/support-library.html">Android Support
2129 * library</a>.
Joe Malin8d40d042012-11-05 11:36:40 -08002130 *
Scott Main183bf112012-08-13 19:12:13 -07002131 * <p>Example:
Joe Malin8d40d042012-11-05 11:36:40 -08002132 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002133 * <pre class="prettyprint">
Scott Main183bf112012-08-13 19:12:13 -07002134 * Notification noti = new Notification.Builder(mContext)
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002135 * .setContentTitle(&quot;New mail from &quot; + sender.toString())
2136 * .setContentText(subject)
2137 * .setSmallIcon(R.drawable.new_mail)
2138 * .setLargeIcon(aBitmap)
Chris Wrenfbd96ba2012-05-01 12:03:58 -04002139 * .build();
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002140 * </pre>
Joe Onoratocb109a02011-01-18 17:57:41 -08002141 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002142 public static class Builder {
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05002143 /**
2144 * @hide
2145 */
2146 public static final String EXTRA_REBUILD_CONTENT_VIEW_ACTION_COUNT =
2147 "android.rebuild.contentViewActionCount";
2148 /**
2149 * @hide
2150 */
2151 public static final String EXTRA_REBUILD_BIG_CONTENT_VIEW_ACTION_COUNT
2152 = "android.rebuild.bigViewActionCount";
2153 /**
2154 * @hide
2155 */
2156 public static final String EXTRA_REBUILD_HEADS_UP_CONTENT_VIEW_ACTION_COUNT
2157 = "android.rebuild.hudViewActionCount";
2158
Daniel Sandler602ad1c2012-06-12 16:06:27 -04002159 private static final int MAX_ACTION_BUTTONS = 3;
Daniel Sandler8680bf82012-05-15 16:52:52 -04002160
Joe Onorato46439ce2010-11-19 13:56:21 -08002161 private Context mContext;
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002162 private Notification mN;
2163 private Bundle mUserExtras = new Bundle();
Chris Wrenfbd96ba2012-05-01 12:03:58 -04002164 private Style mStyle;
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002165 private ArrayList<Action> mActions = new ArrayList<Action>(MAX_ACTION_BUTTONS);
2166 private ArrayList<String> mPersonList = new ArrayList<String>();
2167 private NotificationColorUtil mColorUtil;
2168 private boolean mColorUtilInited = false;
Christoph Studer7ac80e62014-08-04 16:01:57 +02002169
2170 /**
Adrian Roos4ff3b122016-02-01 12:26:13 -08002171 * Caches a contrast-enhanced version of {@link #mCachedContrastColorIsFor}.
2172 */
2173 private int mCachedContrastColor = COLOR_INVALID;
2174 private int mCachedContrastColorIsFor = COLOR_INVALID;
2175
2176 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002177 * Constructs a new Builder with the defaults:
Joe Onoratocb109a02011-01-18 17:57:41 -08002178 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002179
2180 * <table>
2181 * <tr><th align=right>priority</th>
2182 * <td>{@link #PRIORITY_DEFAULT}</td></tr>
2183 * <tr><th align=right>when</th>
2184 * <td>now ({@link System#currentTimeMillis()})</td></tr>
2185 * <tr><th align=right>audio stream</th>
2186 * <td>{@link #STREAM_DEFAULT}</td></tr>
2187 * </table>
Joe Onoratocb109a02011-01-18 17:57:41 -08002188 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002189
2190 * @param context
2191 * A {@link Context} that will be used by the Builder to construct the
2192 * RemoteViews. The Context will not be held past the lifetime of this Builder
2193 * object.
Joe Onoratocb109a02011-01-18 17:57:41 -08002194 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002195 public Builder(Context context) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002196 this(context, null);
Joe Onorato46439ce2010-11-19 13:56:21 -08002197 }
2198
Joe Onoratocb109a02011-01-18 17:57:41 -08002199 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002200 * @hide
Christoph Studer4600f9b2014-07-22 22:44:43 +02002201 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002202 public Builder(Context context, Notification toAdopt) {
2203 mContext = context;
Christoph Studer4600f9b2014-07-22 22:44:43 +02002204
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002205 if (toAdopt == null) {
2206 mN = new Notification();
Selim Cinek0ff1ce602016-04-05 18:27:16 -07002207 if (context.getApplicationInfo().targetSdkVersion < Build.VERSION_CODES.N) {
2208 mN.extras.putBoolean(EXTRA_SHOW_WHEN, true);
2209 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002210 mN.priority = PRIORITY_DEFAULT;
2211 mN.visibility = VISIBILITY_PRIVATE;
2212 } else {
2213 mN = toAdopt;
2214 if (mN.actions != null) {
2215 Collections.addAll(mActions, mN.actions);
Christoph Studer4600f9b2014-07-22 22:44:43 +02002216 }
2217
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002218 if (mN.extras.containsKey(EXTRA_PEOPLE)) {
2219 Collections.addAll(mPersonList, mN.extras.getStringArray(EXTRA_PEOPLE));
2220 }
2221
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002222 String templateClass = mN.extras.getString(EXTRA_TEMPLATE);
2223 if (!TextUtils.isEmpty(templateClass)) {
2224 final Class<? extends Style> styleClass
2225 = getNotificationStyleClass(templateClass);
2226 if (styleClass == null) {
2227 Log.d(TAG, "Unknown style class: " + templateClass);
2228 } else {
2229 try {
2230 final Constructor<? extends Style> ctor = styleClass.getConstructor();
2231 ctor.setAccessible(true);
2232 final Style style = ctor.newInstance();
2233 style.restoreFromExtras(mN.extras);
2234
2235 if (style != null) {
2236 setStyle(style);
2237 }
2238 } catch (Throwable t) {
2239 Log.e(TAG, "Could not create Style", t);
2240 }
2241 }
2242 }
2243
2244 }
2245 }
2246
2247 private NotificationColorUtil getColorUtil() {
2248 if (!mColorUtilInited) {
2249 mColorUtilInited = true;
2250 if (mContext.getApplicationInfo().targetSdkVersion < Build.VERSION_CODES.LOLLIPOP) {
2251 mColorUtil = NotificationColorUtil.getInstance(mContext);
Christoph Studer4600f9b2014-07-22 22:44:43 +02002252 }
2253 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002254 return mColorUtil;
Christoph Studer4600f9b2014-07-22 22:44:43 +02002255 }
2256
2257 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002258 * Add a timestamp pertaining to the notification (usually the time the event occurred).
Selim Cinek0ff1ce602016-04-05 18:27:16 -07002259 *
2260 * For apps targeting {@link android.os.Build.VERSION_CODES#N} and above, this time is not
2261 * shown anymore by default and must be opted into by using
2262 * {@link android.app.Notification.Builder#setShowWhen(boolean)}
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002263 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002264 * @see Notification#when
Joe Onoratocb109a02011-01-18 17:57:41 -08002265 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002266 public Builder setWhen(long when) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002267 mN.when = when;
Joe Onorato46439ce2010-11-19 13:56:21 -08002268 return this;
2269 }
2270
Joe Onoratocb109a02011-01-18 17:57:41 -08002271 /**
Griff Hazen50c11652014-05-16 09:46:31 -07002272 * Control whether the timestamp set with {@link #setWhen(long) setWhen} is shown
Daniel Sandler0c890492012-09-12 17:23:10 -07002273 * in the content view.
Selim Cinek0ff1ce602016-04-05 18:27:16 -07002274 * For apps targeting {@link android.os.Build.VERSION_CODES#N} and above, this defaults to
2275 * {@code false}. For earlier apps, the default is {@code true}.
Daniel Sandler0c890492012-09-12 17:23:10 -07002276 */
2277 public Builder setShowWhen(boolean show) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002278 mN.extras.putBoolean(EXTRA_SHOW_WHEN, show);
Daniel Sandler0c890492012-09-12 17:23:10 -07002279 return this;
2280 }
2281
2282 /**
Daniel Sandlerd33b8032012-05-10 11:41:48 -04002283 * Show the {@link Notification#when} field as a stopwatch.
Joe Malin8d40d042012-11-05 11:36:40 -08002284 *
2285 * Instead of presenting <code>when</code> as a timestamp, the notification will show an
Daniel Sandlerd33b8032012-05-10 11:41:48 -04002286 * automatically updating display of the minutes and seconds since <code>when</code>.
Daniel Sandlera2985ed2012-04-03 16:42:00 -04002287 *
Daniel Sandlerd33b8032012-05-10 11:41:48 -04002288 * Useful when showing an elapsed time (like an ongoing phone call).
2289 *
Selim Cinek81c23aa2016-02-25 16:23:13 -08002290 * The counter can also be set to count down to <code>when</code> when using
2291 * {@link #setChronometerCountsDown(boolean)}.
2292 *
Daniel Sandlerd33b8032012-05-10 11:41:48 -04002293 * @see android.widget.Chronometer
Daniel Sandlera2985ed2012-04-03 16:42:00 -04002294 * @see Notification#when
Selim Cinek81c23aa2016-02-25 16:23:13 -08002295 * @see #setChronometerCountsDown(boolean)
Daniel Sandlera2985ed2012-04-03 16:42:00 -04002296 */
2297 public Builder setUsesChronometer(boolean b) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002298 mN.extras.putBoolean(EXTRA_SHOW_CHRONOMETER, b);
Daniel Sandlera2985ed2012-04-03 16:42:00 -04002299 return this;
2300 }
2301
2302 /**
Selim Cinek81c23aa2016-02-25 16:23:13 -08002303 * Sets the Chronometer to count down instead of counting up.
2304 *
2305 * <p>This is only relevant if {@link #setUsesChronometer(boolean)} has been set to true.
2306 * If it isn't set the chronometer will count up.
2307 *
2308 * @see #setUsesChronometer(boolean)
2309 */
2310 public Builder setChronometerCountsDown(boolean countsDown) {
2311 mN.extras.putBoolean(EXTRA_CHRONOMETER_COUNTS_DOWN, countsDown);
2312 return this;
2313 }
2314
2315 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002316 * Set the small icon resource, which will be used to represent the notification in the
2317 * status bar.
Joe Onoratocb109a02011-01-18 17:57:41 -08002318 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002319
2320 * The platform template for the expanded view will draw this icon in the left, unless a
2321 * {@link #setLargeIcon(Bitmap) large icon} has also been specified, in which case the small
2322 * icon will be moved to the right-hand side.
2323 *
2324
2325 * @param icon
2326 * A resource ID in the application's package of the drawable to use.
2327 * @see Notification#icon
Joe Onoratocb109a02011-01-18 17:57:41 -08002328 */
Tor Norbye7b9c9122013-05-30 16:48:33 -07002329 public Builder setSmallIcon(@DrawableRes int icon) {
Dan Sandlerd63f9322015-05-06 15:18:49 -04002330 return setSmallIcon(icon != 0
2331 ? Icon.createWithResource(mContext, icon)
2332 : null);
Joe Onorato46439ce2010-11-19 13:56:21 -08002333 }
2334
Joe Onoratocb109a02011-01-18 17:57:41 -08002335 /**
2336 * A variant of {@link #setSmallIcon(int) setSmallIcon(int)} that takes an additional
2337 * level parameter for when the icon is a {@link android.graphics.drawable.LevelListDrawable
2338 * LevelListDrawable}.
2339 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002340 * @param icon A resource ID in the application's package of the drawable to use.
Joe Onoratocb109a02011-01-18 17:57:41 -08002341 * @param level The level to use for the icon.
2342 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002343 * @see Notification#icon
2344 * @see Notification#iconLevel
Joe Onoratocb109a02011-01-18 17:57:41 -08002345 */
Tor Norbye7b9c9122013-05-30 16:48:33 -07002346 public Builder setSmallIcon(@DrawableRes int icon, int level) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002347 mN.iconLevel = level;
Dan Sandlerd63f9322015-05-06 15:18:49 -04002348 return setSmallIcon(icon);
2349 }
2350
2351 /**
2352 * Set the small icon, which will be used to represent the notification in the
2353 * status bar and content view (unless overriden there by a
2354 * {@link #setLargeIcon(Bitmap) large icon}).
2355 *
2356 * @param icon An Icon object to use.
2357 * @see Notification#icon
2358 */
2359 public Builder setSmallIcon(Icon icon) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002360 mN.setSmallIcon(icon);
2361 if (icon != null && icon.getType() == Icon.TYPE_RESOURCE) {
2362 mN.icon = icon.getResId();
2363 }
Joe Onorato46439ce2010-11-19 13:56:21 -08002364 return this;
2365 }
2366
Joe Onoratocb109a02011-01-18 17:57:41 -08002367 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002368 * Set the first line of text in the platform notification template.
Joe Onoratocb109a02011-01-18 17:57:41 -08002369 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002370 public Builder setContentTitle(CharSequence title) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002371 mN.extras.putCharSequence(EXTRA_TITLE, safeCharSequence(title));
Joe Onorato46439ce2010-11-19 13:56:21 -08002372 return this;
2373 }
2374
Joe Onoratocb109a02011-01-18 17:57:41 -08002375 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002376 * Set the second line of text in the platform notification template.
Joe Onoratocb109a02011-01-18 17:57:41 -08002377 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002378 public Builder setContentText(CharSequence text) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002379 mN.extras.putCharSequence(EXTRA_TEXT, safeCharSequence(text));
Joe Onorato46439ce2010-11-19 13:56:21 -08002380 return this;
2381 }
2382
Joe Onoratocb109a02011-01-18 17:57:41 -08002383 /**
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07002384 * This provides some additional information that is displayed in the notification. No
2385 * guarantees are given where exactly it is displayed.
2386 *
2387 * <p>This information should only be provided if it provides an essential
2388 * benefit to the understanding of the notification. The more text you provide the
2389 * less readable it becomes. For example, an email client should only provide the account
2390 * name here if more than one email account has been added.</p>
2391 *
2392 * <p>As of {@link android.os.Build.VERSION_CODES#N} this information is displayed in the
2393 * notification header area.
2394 *
2395 * On Android versions before {@link android.os.Build.VERSION_CODES#N}
2396 * this will be shown in the third line of text in the platform notification template.
2397 * You should not be using {@link #setProgress(int, int, boolean)} at the
2398 * same time on those versions; they occupy the same place.
2399 * </p>
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002400 */
2401 public Builder setSubText(CharSequence text) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002402 mN.extras.putCharSequence(EXTRA_SUB_TEXT, safeCharSequence(text));
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002403 return this;
2404 }
2405
2406 /**
Adrian Roose458aa82015-12-08 16:17:19 -08002407 * Set the remote input history.
2408 *
2409 * This should be set to the most recent inputs that have been sent
2410 * through a {@link RemoteInput} of this Notification and cleared once the it is no
2411 * longer relevant (e.g. for chat notifications once the other party has responded).
2412 *
2413 * The most recent input must be stored at the 0 index, the second most recent at the
2414 * 1 index, etc. Note that the system will limit both how far back the inputs will be shown
2415 * and how much of each individual input is shown.
2416 *
2417 * <p>Note: The reply text will only be shown on notifications that have least one action
2418 * with a {@code RemoteInput}.</p>
2419 */
2420 public Builder setRemoteInputHistory(CharSequence[] text) {
2421 if (text == null) {
2422 mN.extras.putCharSequenceArray(EXTRA_REMOTE_INPUT_HISTORY, null);
2423 } else {
2424 final int N = Math.min(MAX_REPLY_HISTORY, text.length);
2425 CharSequence[] safe = new CharSequence[N];
2426 for (int i = 0; i < N; i++) {
2427 safe[i] = safeCharSequence(text[i]);
2428 }
2429 mN.extras.putCharSequenceArray(EXTRA_REMOTE_INPUT_HISTORY, safe);
2430 }
2431 return this;
2432 }
2433
2434 /**
Joe Onoratocb109a02011-01-18 17:57:41 -08002435 * Set the large number at the right-hand side of the notification. This is
2436 * equivalent to setContentInfo, although it might show the number in a different
2437 * font size for readability.
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07002438 *
2439 * @deprecated this number is not shown anywhere anymore
Joe Onoratocb109a02011-01-18 17:57:41 -08002440 */
Joe Onorato8595a3d2010-11-19 18:12:07 -08002441 public Builder setNumber(int number) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002442 mN.number = number;
Joe Onorato8595a3d2010-11-19 18:12:07 -08002443 return this;
2444 }
2445
Joe Onoratocb109a02011-01-18 17:57:41 -08002446 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002447 * A small piece of additional information pertaining to this notification.
2448 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002449 * The platform template will draw this on the last line of the notification, at the far
2450 * right (to the right of a smallIcon if it has been placed there).
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07002451 *
2452 * @deprecated use {@link #setSubText(CharSequence)} instead to set a text in the header.
2453 * For legacy apps targeting a version below {@link android.os.Build.VERSION_CODES#N} this
2454 * field will still show up, but the subtext will take precedence.
Joe Onoratocb109a02011-01-18 17:57:41 -08002455 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002456 public Builder setContentInfo(CharSequence info) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002457 mN.extras.putCharSequence(EXTRA_INFO_TEXT, safeCharSequence(info));
Joe Onorato46439ce2010-11-19 13:56:21 -08002458 return this;
2459 }
2460
Joe Onoratocb109a02011-01-18 17:57:41 -08002461 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002462 * Set the progress this notification represents.
2463 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002464 * The platform template will represent this using a {@link ProgressBar}.
Jeff Sharkey1c400132011-08-05 14:50:13 -07002465 */
2466 public Builder setProgress(int max, int progress, boolean indeterminate) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002467 mN.extras.putInt(EXTRA_PROGRESS, progress);
2468 mN.extras.putInt(EXTRA_PROGRESS_MAX, max);
2469 mN.extras.putBoolean(EXTRA_PROGRESS_INDETERMINATE, indeterminate);
Jeff Sharkey1c400132011-08-05 14:50:13 -07002470 return this;
2471 }
2472
2473 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002474 * Supply a custom RemoteViews to use instead of the platform template.
2475 *
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002476 * Use {@link #setCustomContentView(RemoteViews)} instead.
Joe Onoratocb109a02011-01-18 17:57:41 -08002477 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002478 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -08002479 public Builder setContent(RemoteViews views) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002480 return setCustomContentView(views);
2481 }
2482
2483 /**
2484 * Supply custom RemoteViews to use instead of the platform template.
2485 *
2486 * This will override the layout that would otherwise be constructed by this Builder
2487 * object.
2488 */
2489 public Builder setCustomContentView(RemoteViews contentView) {
2490 mN.contentView = contentView;
2491 return this;
2492 }
2493
2494 /**
2495 * Supply custom RemoteViews to use instead of the platform template in the expanded form.
2496 *
2497 * This will override the expanded layout that would otherwise be constructed by this
2498 * Builder object.
2499 */
2500 public Builder setCustomBigContentView(RemoteViews contentView) {
2501 mN.bigContentView = contentView;
2502 return this;
2503 }
2504
2505 /**
2506 * Supply custom RemoteViews to use instead of the platform template in the heads up dialog.
2507 *
2508 * This will override the heads-up layout that would otherwise be constructed by this
2509 * Builder object.
2510 */
2511 public Builder setCustomHeadsUpContentView(RemoteViews contentView) {
2512 mN.headsUpContentView = contentView;
Joe Onorato46439ce2010-11-19 13:56:21 -08002513 return this;
2514 }
2515
Joe Onoratocb109a02011-01-18 17:57:41 -08002516 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002517 * Supply a {@link PendingIntent} to be sent when the notification is clicked.
2518 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002519 * As of {@link android.os.Build.VERSION_CODES#HONEYCOMB}, if this field is unset and you
2520 * have specified a custom RemoteViews with {@link #setContent(RemoteViews)}, you can use
2521 * {@link RemoteViews#setOnClickPendingIntent RemoteViews.setOnClickPendingIntent(int,PendingIntent)}
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002522 * to assign PendingIntents to individual views in that custom layout (i.e., to create
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002523 * clickable buttons inside the notification view).
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002524 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002525 * @see Notification#contentIntent Notification.contentIntent
Joe Onoratocb109a02011-01-18 17:57:41 -08002526 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002527 public Builder setContentIntent(PendingIntent intent) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002528 mN.contentIntent = intent;
Joe Onorato46439ce2010-11-19 13:56:21 -08002529 return this;
2530 }
2531
Joe Onoratocb109a02011-01-18 17:57:41 -08002532 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002533 * Supply a {@link PendingIntent} to send when the notification is cleared explicitly by the user.
2534 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002535 * @see Notification#deleteIntent
Joe Onoratocb109a02011-01-18 17:57:41 -08002536 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002537 public Builder setDeleteIntent(PendingIntent intent) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002538 mN.deleteIntent = intent;
Joe Onorato46439ce2010-11-19 13:56:21 -08002539 return this;
2540 }
2541
Joe Onoratocb109a02011-01-18 17:57:41 -08002542 /**
2543 * An intent to launch instead of posting the notification to the status bar.
2544 * Only for use with extremely high-priority notifications demanding the user's
2545 * <strong>immediate</strong> attention, such as an incoming phone call or
2546 * alarm clock that the user has explicitly set to a particular time.
2547 * If this facility is used for something else, please give the user an option
2548 * to turn it off and use a normal notification, as this can be extremely
2549 * disruptive.
2550 *
Chris Wren47c20a12014-06-18 17:27:29 -04002551 * <p>
2552 * The system UI may choose to display a heads-up notification, instead of
2553 * launching this intent, while the user is using the device.
2554 * </p>
2555 *
Joe Onoratocb109a02011-01-18 17:57:41 -08002556 * @param intent The pending intent to launch.
2557 * @param highPriority Passing true will cause this notification to be sent
2558 * even if other notifications are suppressed.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002559 *
2560 * @see Notification#fullScreenIntent
Joe Onoratocb109a02011-01-18 17:57:41 -08002561 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002562 public Builder setFullScreenIntent(PendingIntent intent, boolean highPriority) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002563 mN.fullScreenIntent = intent;
Joe Onorato46439ce2010-11-19 13:56:21 -08002564 setFlag(FLAG_HIGH_PRIORITY, highPriority);
2565 return this;
2566 }
2567
Joe Onoratocb109a02011-01-18 17:57:41 -08002568 /**
Dan Sandler5fcdf6e2014-07-18 11:31:15 -04002569 * Set the "ticker" text which is sent to accessibility services.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002570 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002571 * @see Notification#tickerText
Joe Onoratocb109a02011-01-18 17:57:41 -08002572 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002573 public Builder setTicker(CharSequence tickerText) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002574 mN.tickerText = safeCharSequence(tickerText);
Joe Onorato46439ce2010-11-19 13:56:21 -08002575 return this;
2576 }
2577
Joe Onoratocb109a02011-01-18 17:57:41 -08002578 /**
Dan Sandler5fcdf6e2014-07-18 11:31:15 -04002579 * Obsolete version of {@link #setTicker(CharSequence)}.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002580 *
Joe Onoratocb109a02011-01-18 17:57:41 -08002581 */
Dan Sandler5fcdf6e2014-07-18 11:31:15 -04002582 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -08002583 public Builder setTicker(CharSequence tickerText, RemoteViews views) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002584 setTicker(tickerText);
2585 // views is ignored
Joe Onorato46439ce2010-11-19 13:56:21 -08002586 return this;
2587 }
2588
Joe Onoratocb109a02011-01-18 17:57:41 -08002589 /**
Dan Sandlerd63f9322015-05-06 15:18:49 -04002590 * Add a large icon to the notification content view.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002591 *
2592 * In the platform template, this image will be shown on the left of the notification view
Dan Sandlerd63f9322015-05-06 15:18:49 -04002593 * in place of the {@link #setSmallIcon(Icon) small icon} (which will be placed in a small
2594 * badge atop the large icon).
Dan Sandler08a04c12015-05-06 15:18:49 -04002595 */
Dan Sandlerd63f9322015-05-06 15:18:49 -04002596 public Builder setLargeIcon(Bitmap b) {
2597 return setLargeIcon(b != null ? Icon.createWithBitmap(b) : null);
2598 }
2599
2600 /**
2601 * Add a large icon to the notification content view.
2602 *
2603 * In the platform template, this image will be shown on the left of the notification view
2604 * in place of the {@link #setSmallIcon(Icon) small icon} (which will be placed in a small
2605 * badge atop the large icon).
2606 */
2607 public Builder setLargeIcon(Icon icon) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002608 mN.mLargeIcon = icon;
2609 mN.extras.putParcelable(EXTRA_LARGE_ICON, icon);
Joe Onorato46439ce2010-11-19 13:56:21 -08002610 return this;
2611 }
2612
Joe Onoratocb109a02011-01-18 17:57:41 -08002613 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002614 * Set the sound to play.
2615 *
John Spurlockc0650f022014-07-19 13:22:39 -04002616 * It will be played using the {@link #AUDIO_ATTRIBUTES_DEFAULT default audio attributes}
2617 * for notifications.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002618 *
Chris Wren47c20a12014-06-18 17:27:29 -04002619 * <p>
2620 * A notification that is noisy is more likely to be presented as a heads-up notification.
2621 * </p>
2622 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002623 * @see Notification#sound
Joe Onoratocb109a02011-01-18 17:57:41 -08002624 */
Joe Onorato52f80cd2010-11-21 15:34:48 -08002625 public Builder setSound(Uri sound) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002626 mN.sound = sound;
2627 mN.audioAttributes = AUDIO_ATTRIBUTES_DEFAULT;
Joe Onorato52f80cd2010-11-21 15:34:48 -08002628 return this;
2629 }
2630
Joe Onoratocb109a02011-01-18 17:57:41 -08002631 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002632 * Set the sound to play, along with a specific stream on which to play it.
Joe Onoratocb109a02011-01-18 17:57:41 -08002633 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002634 * See {@link android.media.AudioManager} for the <code>STREAM_</code> constants.
2635 *
Chris Wren47c20a12014-06-18 17:27:29 -04002636 * <p>
2637 * A notification that is noisy is more likely to be presented as a heads-up notification.
2638 * </p>
John Spurlockc0650f022014-07-19 13:22:39 -04002639 * @deprecated use {@link #setSound(Uri, AudioAttributes)} instead.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002640 * @see Notification#sound
Joe Onoratocb109a02011-01-18 17:57:41 -08002641 */
Jean-Michel Trivi81f871e2014-08-06 16:32:38 -07002642 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -08002643 public Builder setSound(Uri sound, int streamType) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002644 mN.sound = sound;
2645 mN.audioStreamType = streamType;
Joe Onorato46439ce2010-11-19 13:56:21 -08002646 return this;
2647 }
2648
Joe Onoratocb109a02011-01-18 17:57:41 -08002649 /**
John Spurlockc0650f022014-07-19 13:22:39 -04002650 * Set the sound to play, along with specific {@link AudioAttributes audio attributes} to
2651 * use during playback.
2652 *
2653 * <p>
2654 * A notification that is noisy is more likely to be presented as a heads-up notification.
2655 * </p>
2656 *
2657 * @see Notification#sound
2658 */
2659 public Builder setSound(Uri sound, AudioAttributes audioAttributes) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002660 mN.sound = sound;
2661 mN.audioAttributes = audioAttributes;
John Spurlockc0650f022014-07-19 13:22:39 -04002662 return this;
2663 }
2664
2665 /**
Joe Onoratocb109a02011-01-18 17:57:41 -08002666 * Set the vibration pattern to use.
2667 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002668 * See {@link android.os.Vibrator#vibrate(long[], int)} for a discussion of the
2669 * <code>pattern</code> parameter.
2670 *
Chris Wren47c20a12014-06-18 17:27:29 -04002671 * <p>
2672 * A notification that vibrates is more likely to be presented as a heads-up notification.
2673 * </p>
2674 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002675 * @see Notification#vibrate
Joe Onoratocb109a02011-01-18 17:57:41 -08002676 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002677 public Builder setVibrate(long[] pattern) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002678 mN.vibrate = pattern;
Joe Onorato46439ce2010-11-19 13:56:21 -08002679 return this;
2680 }
2681
Joe Onoratocb109a02011-01-18 17:57:41 -08002682 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002683 * Set the desired color for the indicator LED on the device, as well as the
2684 * blink duty cycle (specified in milliseconds).
2685 *
2686
2687 * Not all devices will honor all (or even any) of these values.
2688 *
2689
2690 * @see Notification#ledARGB
2691 * @see Notification#ledOnMS
2692 * @see Notification#ledOffMS
Joe Onoratocb109a02011-01-18 17:57:41 -08002693 */
Tor Norbye80756e32015-03-02 09:39:27 -08002694 public Builder setLights(@ColorInt int argb, int onMs, int offMs) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002695 mN.ledARGB = argb;
2696 mN.ledOnMS = onMs;
2697 mN.ledOffMS = offMs;
Julia Reynolds10ee1fc2015-11-09 11:04:55 -05002698 if (onMs != 0 || offMs != 0) {
2699 mN.flags |= FLAG_SHOW_LIGHTS;
2700 }
Joe Onorato46439ce2010-11-19 13:56:21 -08002701 return this;
2702 }
2703
Joe Onoratocb109a02011-01-18 17:57:41 -08002704 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002705 * Set whether this is an "ongoing" notification.
Joe Onoratocb109a02011-01-18 17:57:41 -08002706 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002707
2708 * Ongoing notifications cannot be dismissed by the user, so your application or service
2709 * must take care of canceling them.
2710 *
2711
2712 * They are typically used to indicate a background task that the user is actively engaged
2713 * with (e.g., playing music) or is pending in some way and therefore occupying the device
2714 * (e.g., a file download, sync operation, active network connection).
2715 *
2716
2717 * @see Notification#FLAG_ONGOING_EVENT
2718 * @see Service#setForeground(boolean)
Joe Onoratocb109a02011-01-18 17:57:41 -08002719 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002720 public Builder setOngoing(boolean ongoing) {
2721 setFlag(FLAG_ONGOING_EVENT, ongoing);
2722 return this;
2723 }
2724
Joe Onoratocb109a02011-01-18 17:57:41 -08002725 /**
2726 * Set this flag if you would only like the sound, vibrate
2727 * and ticker to be played if the notification is not already showing.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002728 *
2729 * @see Notification#FLAG_ONLY_ALERT_ONCE
Joe Onoratocb109a02011-01-18 17:57:41 -08002730 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002731 public Builder setOnlyAlertOnce(boolean onlyAlertOnce) {
2732 setFlag(FLAG_ONLY_ALERT_ONCE, onlyAlertOnce);
2733 return this;
2734 }
2735
Joe Onoratocb109a02011-01-18 17:57:41 -08002736 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002737 * Make this notification automatically dismissed when the user touches it. The
2738 * PendingIntent set with {@link #setDeleteIntent} will be sent when this happens.
2739 *
2740 * @see Notification#FLAG_AUTO_CANCEL
Joe Onoratocb109a02011-01-18 17:57:41 -08002741 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002742 public Builder setAutoCancel(boolean autoCancel) {
Joe Onorato281d83f2011-01-04 17:13:10 -08002743 setFlag(FLAG_AUTO_CANCEL, autoCancel);
Joe Onorato46439ce2010-11-19 13:56:21 -08002744 return this;
2745 }
2746
Joe Onoratocb109a02011-01-18 17:57:41 -08002747 /**
Griff Hazendfcb0802014-02-11 12:00:00 -08002748 * Set whether or not this notification should not bridge to other devices.
2749 *
2750 * <p>Some notifications can be bridged to other devices for remote display.
2751 * This hint can be set to recommend this notification not be bridged.
2752 */
2753 public Builder setLocalOnly(boolean localOnly) {
2754 setFlag(FLAG_LOCAL_ONLY, localOnly);
2755 return this;
2756 }
2757
2758 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002759 * Set which notification properties will be inherited from system defaults.
Joe Onoratocb109a02011-01-18 17:57:41 -08002760 * <p>
2761 * The value should be one or more of the following fields combined with
2762 * bitwise-or:
2763 * {@link #DEFAULT_SOUND}, {@link #DEFAULT_VIBRATE}, {@link #DEFAULT_LIGHTS}.
2764 * <p>
2765 * For all default values, use {@link #DEFAULT_ALL}.
2766 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002767 public Builder setDefaults(int defaults) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002768 mN.defaults = defaults;
Joe Onorato46439ce2010-11-19 13:56:21 -08002769 return this;
2770 }
2771
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002772 /**
2773 * Set the priority of this notification.
2774 *
2775 * @see Notification#priority
2776 */
Tor Norbyed9273d62013-05-30 15:59:53 -07002777 public Builder setPriority(@Priority int pri) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002778 mN.priority = pri;
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002779 return this;
2780 }
Joe Malin8d40d042012-11-05 11:36:40 -08002781
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002782 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -04002783 * Set the notification category.
Joe Malin8d40d042012-11-05 11:36:40 -08002784 *
John Spurlockfd7f1e02014-03-18 16:41:57 -04002785 * @see Notification#category
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002786 */
John Spurlockfd7f1e02014-03-18 16:41:57 -04002787 public Builder setCategory(String category) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002788 mN.category = category;
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002789 return this;
2790 }
2791
2792 /**
Chris Wrendde75302014-03-26 17:24:15 -04002793 * Add a person that is relevant to this notification.
2794 *
Chris Wrene6c48932014-09-29 17:19:27 -04002795 * <P>
2796 * Depending on user preferences, this annotation may allow the notification to pass
2797 * through interruption filters, and to appear more prominently in the user interface.
2798 * </P>
2799 *
2800 * <P>
2801 * The person should be specified by the {@code String} representation of a
2802 * {@link android.provider.ContactsContract.Contacts#CONTENT_LOOKUP_URI}.
2803 * </P>
2804 *
2805 * <P>The system will also attempt to resolve {@code mailto:} and {@code tel:} schema
2806 * URIs. The path part of these URIs must exist in the contacts database, in the
2807 * appropriate column, or the reference will be discarded as invalid. Telephone schema
2808 * URIs will be resolved by {@link android.provider.ContactsContract.PhoneLookup}.
2809 * </P>
2810 *
2811 * @param uri A URI for the person.
Chris Wrendde75302014-03-26 17:24:15 -04002812 * @see Notification#EXTRA_PEOPLE
2813 */
Chris Wrene6c48932014-09-29 17:19:27 -04002814 public Builder addPerson(String uri) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002815 mPersonList.add(uri);
Chris Wrendde75302014-03-26 17:24:15 -04002816 return this;
2817 }
2818
2819 /**
Griff Hazen5cadc3b2014-05-20 09:55:39 -07002820 * Set this notification to be part of a group of notifications sharing the same key.
2821 * Grouped notifications may display in a cluster or stack on devices which
2822 * support such rendering.
2823 *
2824 * <p>To make this notification the summary for its group, also call
2825 * {@link #setGroupSummary}. A sort order can be specified for group members by using
2826 * {@link #setSortKey}.
2827 * @param groupKey The group key of the group.
2828 * @return this object for method chaining
2829 */
2830 public Builder setGroup(String groupKey) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002831 mN.mGroupKey = groupKey;
Griff Hazen5cadc3b2014-05-20 09:55:39 -07002832 return this;
2833 }
2834
2835 /**
2836 * Set this notification to be the group summary for a group of notifications.
2837 * Grouped notifications may display in a cluster or stack on devices which
2838 * support such rendering. Requires a group key also be set using {@link #setGroup}.
2839 * @param isGroupSummary Whether this notification should be a group summary.
2840 * @return this object for method chaining
2841 */
2842 public Builder setGroupSummary(boolean isGroupSummary) {
2843 setFlag(FLAG_GROUP_SUMMARY, isGroupSummary);
2844 return this;
2845 }
2846
2847 /**
2848 * Set a sort key that orders this notification among other notifications from the
2849 * same package. This can be useful if an external sort was already applied and an app
2850 * would like to preserve this. Notifications will be sorted lexicographically using this
2851 * value, although providing different priorities in addition to providing sort key may
2852 * cause this value to be ignored.
2853 *
2854 * <p>This sort key can also be used to order members of a notification group. See
Griff Hazen9e1379f2014-05-20 12:50:51 -07002855 * {@link #setGroup}.
Griff Hazen5cadc3b2014-05-20 09:55:39 -07002856 *
2857 * @see String#compareTo(String)
2858 */
2859 public Builder setSortKey(String sortKey) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002860 mN.mSortKey = sortKey;
Griff Hazen5cadc3b2014-05-20 09:55:39 -07002861 return this;
2862 }
2863
2864 /**
Griff Hazen720042b2014-02-24 15:46:56 -08002865 * Merge additional metadata into this notification.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002866 *
Griff Hazen720042b2014-02-24 15:46:56 -08002867 * <p>Values within the Bundle will replace existing extras values in this Builder.
2868 *
2869 * @see Notification#extras
2870 */
Griff Hazen959591e2014-05-15 22:26:18 -07002871 public Builder addExtras(Bundle extras) {
2872 if (extras != null) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002873 mUserExtras.putAll(extras);
Griff Hazen720042b2014-02-24 15:46:56 -08002874 }
2875 return this;
2876 }
2877
2878 /**
2879 * Set metadata for this notification.
2880 *
2881 * <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 -04002882 * current contents are copied into the Notification each time {@link #build()} is
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002883 * called.
2884 *
Griff Hazen720042b2014-02-24 15:46:56 -08002885 * <p>Replaces any existing extras values with those from the provided Bundle.
2886 * Use {@link #addExtras} to merge in metadata instead.
2887 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002888 * @see Notification#extras
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002889 */
Griff Hazen959591e2014-05-15 22:26:18 -07002890 public Builder setExtras(Bundle extras) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002891 if (extras != null) {
2892 mUserExtras = extras;
2893 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002894 return this;
2895 }
2896
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002897 /**
Griff Hazen720042b2014-02-24 15:46:56 -08002898 * Get the current metadata Bundle used by this notification Builder.
2899 *
2900 * <p>The returned Bundle is shared with this Builder.
2901 *
2902 * <p>The current contents of this Bundle are copied into the Notification each time
2903 * {@link #build()} is called.
2904 *
2905 * @see Notification#extras
2906 */
2907 public Bundle getExtras() {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002908 return mUserExtras;
2909 }
2910
2911 private Bundle getAllExtras() {
2912 final Bundle saveExtras = (Bundle) mUserExtras.clone();
2913 saveExtras.putAll(mN.extras);
2914 return saveExtras;
Griff Hazen720042b2014-02-24 15:46:56 -08002915 }
2916
2917 /**
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002918 * Add an action to this notification. Actions are typically displayed by
2919 * the system as a button adjacent to the notification content.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04002920 * <p>
2921 * Every action must have an icon (32dp square and matching the
2922 * <a href="{@docRoot}design/style/iconography.html#action-bar">Holo
2923 * Dark action bar</a> visual style), a textual label, and a {@link PendingIntent}.
2924 * <p>
2925 * A notification in its expanded form can display up to 3 actions, from left to right in
2926 * the order they were added. Actions will not be displayed when the notification is
2927 * collapsed, however, so be sure that any essential functions may be accessed by the user
2928 * in some other way (for example, in the Activity pointed to by {@link #contentIntent}).
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002929 *
2930 * @param icon Resource ID of a drawable that represents the action.
2931 * @param title Text describing the action.
2932 * @param intent PendingIntent to be fired when the action is invoked.
Dan Sandler86647982015-05-13 23:41:13 -04002933 *
2934 * @deprecated Use {@link #addAction(Action)} instead.
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002935 */
Dan Sandler86647982015-05-13 23:41:13 -04002936 @Deprecated
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002937 public Builder addAction(int icon, CharSequence title, PendingIntent intent) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04002938 mActions.add(new Action(icon, safeCharSequence(title), intent));
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002939 return this;
2940 }
2941
Chris Wrenfbd96ba2012-05-01 12:03:58 -04002942 /**
Griff Hazen959591e2014-05-15 22:26:18 -07002943 * Add an action to this notification. Actions are typically displayed by
2944 * the system as a button adjacent to the notification content.
2945 * <p>
2946 * Every action must have an icon (32dp square and matching the
2947 * <a href="{@docRoot}design/style/iconography.html#action-bar">Holo
2948 * Dark action bar</a> visual style), a textual label, and a {@link PendingIntent}.
2949 * <p>
2950 * A notification in its expanded form can display up to 3 actions, from left to right in
2951 * the order they were added. Actions will not be displayed when the notification is
2952 * collapsed, however, so be sure that any essential functions may be accessed by the user
2953 * in some other way (for example, in the Activity pointed to by {@link #contentIntent}).
2954 *
2955 * @param action The action to add.
2956 */
2957 public Builder addAction(Action action) {
2958 mActions.add(action);
2959 return this;
2960 }
2961
2962 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002963 * Alter the complete list of actions attached to this notification.
2964 * @see #addAction(Action).
2965 *
2966 * @param actions
2967 * @return
2968 */
2969 public Builder setActions(Action... actions) {
2970 mActions.clear();
2971 for (int i = 0; i < actions.length; i++) {
2972 mActions.add(actions[i]);
2973 }
2974 return this;
2975 }
2976
2977 /**
Chris Wrenfbd96ba2012-05-01 12:03:58 -04002978 * Add a rich notification style to be applied at build time.
2979 *
2980 * @param style Object responsible for modifying the notification style.
2981 */
2982 public Builder setStyle(Style style) {
2983 if (mStyle != style) {
2984 mStyle = style;
Daniel Sandlerc08dea22012-06-28 08:35:24 -07002985 if (mStyle != null) {
2986 mStyle.setBuilder(this);
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002987 mN.extras.putString(EXTRA_TEMPLATE, style.getClass().getName());
2988 } else {
2989 mN.extras.remove(EXTRA_TEMPLATE);
Daniel Sandlerc08dea22012-06-28 08:35:24 -07002990 }
Chris Wrenfbd96ba2012-05-01 12:03:58 -04002991 }
2992 return this;
2993 }
2994
Dan Sandler0bf2ed82013-12-21 23:33:41 -06002995 /**
2996 * Specify the value of {@link #visibility}.
Griff Hazenb720abe2014-05-20 13:15:30 -07002997 *
Dan Sandler0bf2ed82013-12-21 23:33:41 -06002998 * @param visibility One of {@link #VISIBILITY_PRIVATE} (the default),
2999 * {@link #VISIBILITY_SECRET}, or {@link #VISIBILITY_PUBLIC}.
3000 *
3001 * @return The same Builder.
3002 */
3003 public Builder setVisibility(int visibility) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003004 mN.visibility = visibility;
Dan Sandler0bf2ed82013-12-21 23:33:41 -06003005 return this;
3006 }
3007
3008 /**
3009 * Supply a replacement Notification whose contents should be shown in insecure contexts
3010 * (i.e. atop the secure lockscreen). See {@link #visibility} and {@link #VISIBILITY_PUBLIC}.
3011 * @param n A replacement notification, presumably with some or all info redacted.
3012 * @return The same Builder.
3013 */
3014 public Builder setPublicVersion(Notification n) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003015 if (n != null) {
3016 mN.publicVersion = new Notification();
3017 n.cloneInto(mN.publicVersion, /*heavy=*/ true);
3018 } else {
3019 mN.publicVersion = null;
3020 }
Dan Sandler0bf2ed82013-12-21 23:33:41 -06003021 return this;
3022 }
3023
Griff Hazenb720abe2014-05-20 13:15:30 -07003024 /**
Griff Hazen5cadc3b2014-05-20 09:55:39 -07003025 * Apply an extender to this notification builder. Extenders may be used to add
3026 * metadata or change options on this builder.
3027 */
Griff Hazen61a9e862014-05-22 16:05:19 -07003028 public Builder extend(Extender extender) {
3029 extender.extend(this);
Griff Hazen5cadc3b2014-05-20 09:55:39 -07003030 return this;
3031 }
3032
Dan Sandler4e787062015-06-17 15:09:48 -04003033 /**
3034 * @hide
3035 */
Julia Reynoldse46bb372016-03-17 11:05:58 -04003036 public Builder setFlag(int mask, boolean value) {
Joe Onorato46439ce2010-11-19 13:56:21 -08003037 if (value) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003038 mN.flags |= mask;
Joe Onorato46439ce2010-11-19 13:56:21 -08003039 } else {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003040 mN.flags &= ~mask;
Joe Onorato46439ce2010-11-19 13:56:21 -08003041 }
Julia Reynoldse46bb372016-03-17 11:05:58 -04003042 return this;
Joe Onorato46439ce2010-11-19 13:56:21 -08003043 }
3044
Dan Sandler26e81cf2014-05-06 10:01:27 -04003045 /**
3046 * Sets {@link Notification#color}.
3047 *
3048 * @param argb The accent color to use
3049 *
3050 * @return The same Builder.
3051 */
Tor Norbye80756e32015-03-02 09:39:27 -08003052 public Builder setColor(@ColorInt int argb) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003053 mN.color = argb;
Julia Reynolds10ee1fc2015-11-09 11:04:55 -05003054 sanitizeColor();
Dan Sandler26e81cf2014-05-06 10:01:27 -04003055 return this;
3056 }
3057
Jorim Jaggid05aa3e2014-08-28 17:52:27 +02003058 private Drawable getProfileBadgeDrawable() {
Christoph Studer7ac80e62014-08-04 16:01:57 +02003059 // Note: This assumes that the current user can read the profile badge of the
3060 // originating user.
Selim Cineke6ff9462016-01-15 15:07:06 -08003061 return mContext.getPackageManager().getUserBadgeForDensityNoBackground(
Julia Reynoldsda303542015-11-23 14:00:20 -05003062 new UserHandle(mContext.getUserId()), 0);
Jorim Jaggid05aa3e2014-08-28 17:52:27 +02003063 }
3064
3065 private Bitmap getProfileBadge() {
3066 Drawable badge = getProfileBadgeDrawable();
Kenny Guy8a0101b2014-05-08 23:34:12 +01003067 if (badge == null) {
3068 return null;
3069 }
Jorim Jaggid05aa3e2014-08-28 17:52:27 +02003070 final int size = mContext.getResources().getDimensionPixelSize(
3071 R.dimen.notification_badge_size);
3072 Bitmap bitmap = Bitmap.createBitmap(size, size, Bitmap.Config.ARGB_8888);
Kenny Guy8a0101b2014-05-08 23:34:12 +01003073 Canvas canvas = new Canvas(bitmap);
Jorim Jaggid05aa3e2014-08-28 17:52:27 +02003074 badge.setBounds(0, 0, size, size);
Kenny Guy8a0101b2014-05-08 23:34:12 +01003075 badge.draw(canvas);
3076 return bitmap;
3077 }
3078
Selim Cinekc848c3a2016-01-13 15:27:30 -08003079 private void bindProfileBadge(RemoteViews contentView) {
Kenny Guy98193ea2014-07-24 19:54:37 +01003080 Bitmap profileBadge = getProfileBadge();
3081
Kenny Guy98193ea2014-07-24 19:54:37 +01003082 if (profileBadge != null) {
Selim Cinekc848c3a2016-01-13 15:27:30 -08003083 contentView.setImageViewBitmap(R.id.profile_badge, profileBadge);
3084 contentView.setViewVisibility(R.id.profile_badge, View.VISIBLE);
Kenny Guy98193ea2014-07-24 19:54:37 +01003085 }
Kenny Guy98193ea2014-07-24 19:54:37 +01003086 }
3087
Christoph Studerfe718432014-09-01 18:21:18 +02003088 private void resetStandardTemplate(RemoteViews contentView) {
Selim Cinekeaa29ca2015-11-23 13:51:13 -08003089 resetNotificationHeader(contentView);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003090 resetContentMargins(contentView);
Christoph Studerfe718432014-09-01 18:21:18 +02003091 contentView.setViewVisibility(R.id.right_icon, View.GONE);
Selim Cinek860b6da2015-12-16 19:02:19 -08003092 contentView.setViewVisibility(R.id.title, View.GONE);
Christoph Studerfe718432014-09-01 18:21:18 +02003093 contentView.setTextViewText(R.id.title, null);
Selim Cinek41598732016-01-11 16:58:37 -08003094 contentView.setViewVisibility(R.id.text, View.GONE);
Christoph Studerfe718432014-09-01 18:21:18 +02003095 contentView.setTextViewText(R.id.text, null);
Selim Cinek29603462015-11-17 19:04:39 -08003096 contentView.setViewVisibility(R.id.text_line_1, View.GONE);
Selim Cinek41598732016-01-11 16:58:37 -08003097 contentView.setTextViewText(R.id.text_line_1, null);
Christoph Studerfe718432014-09-01 18:21:18 +02003098 contentView.setViewVisibility(R.id.progress, View.GONE);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003099 }
3100
Selim Cinekeaa29ca2015-11-23 13:51:13 -08003101 /**
3102 * Resets the notification header to its original state
3103 */
3104 private void resetNotificationHeader(RemoteViews contentView) {
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003105 contentView.setImageViewResource(R.id.icon, 0);
Selim Cinek7b836392015-12-04 20:02:59 -08003106 contentView.setBoolean(R.id.notification_header, "setExpanded", false);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003107 contentView.setTextViewText(R.id.app_name_text, null);
Christoph Studerca1db712014-09-10 17:31:33 +02003108 contentView.setViewVisibility(R.id.chronometer, View.GONE);
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07003109 contentView.setViewVisibility(R.id.header_text, View.GONE);
3110 contentView.setViewVisibility(R.id.header_text_divider, View.GONE);
Selim Cinek29603462015-11-17 19:04:39 -08003111 contentView.setViewVisibility(R.id.time_divider, View.GONE);
Selim Cinekc848c3a2016-01-13 15:27:30 -08003112 contentView.setImageViewIcon(R.id.profile_badge, null);
3113 contentView.setViewVisibility(R.id.profile_badge, View.GONE);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003114 }
3115
3116 private void resetContentMargins(RemoteViews contentView) {
3117 contentView.setViewLayoutMarginEnd(R.id.line1, 0);
Selim Cinekc848c3a2016-01-13 15:27:30 -08003118 contentView.setViewLayoutMarginEnd(R.id.text, 0);
Christoph Studerfe718432014-09-01 18:21:18 +02003119 }
3120
Jorim Jaggi445d3c02014-08-19 22:33:42 +02003121 private RemoteViews applyStandardTemplate(int resId) {
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02003122 return applyStandardTemplate(resId, true /* hasProgress */);
3123 }
3124
3125 /**
3126 * @param hasProgress whether the progress bar should be shown and set
3127 */
3128 private RemoteViews applyStandardTemplate(int resId, boolean hasProgress) {
Kenny Guy77320062014-08-27 21:37:15 +01003129 RemoteViews contentView = new BuilderRemoteViews(mContext.getApplicationInfo(), resId);
Dan Sandler539aad42014-08-04 00:43:39 -04003130
Christoph Studerfe718432014-09-01 18:21:18 +02003131 resetStandardTemplate(contentView);
3132
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003133 final Bundle ex = mN.extras;
Dan Sandler190d58d2014-05-15 09:33:39 -04003134
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003135 bindNotificationHeader(contentView);
3136 bindLargeIcon(contentView);
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003137 if (ex.getCharSequence(EXTRA_TITLE) != null) {
Selim Cinek860b6da2015-12-16 19:02:19 -08003138 contentView.setViewVisibility(R.id.title, View.VISIBLE);
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003139 contentView.setTextViewText(R.id.title,
3140 processLegacyText(ex.getCharSequence(EXTRA_TITLE)));
Joe Onorato561d3852010-11-20 18:09:34 -08003141 }
Selim Cinek29603462015-11-17 19:04:39 -08003142 boolean showProgress = handleProgressBar(hasProgress, contentView, ex);
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003143 if (ex.getCharSequence(EXTRA_TEXT) != null) {
Selim Cinek41598732016-01-11 16:58:37 -08003144 int textId = showProgress ? com.android.internal.R.id.text_line_1
3145 : com.android.internal.R.id.text;
3146 contentView.setTextViewText(textId, processLegacyText(
3147 ex.getCharSequence(EXTRA_TEXT)));
3148 contentView.setViewVisibility(textId, View.VISIBLE);
Joe Onorato561d3852010-11-20 18:09:34 -08003149 }
Selim Cinekc848c3a2016-01-13 15:27:30 -08003150
Selim Cinek860b6da2015-12-16 19:02:19 -08003151 setContentMinHeight(contentView, showProgress || mN.mLargeIcon != null);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003152
Selim Cinek29603462015-11-17 19:04:39 -08003153 return contentView;
3154 }
3155
Selim Cinek860b6da2015-12-16 19:02:19 -08003156 /**
3157 * @param remoteView the remote view to update the minheight in
3158 * @param hasMinHeight does it have a mimHeight
3159 * @hide
3160 */
3161 void setContentMinHeight(RemoteViews remoteView, boolean hasMinHeight) {
3162 int minHeight = 0;
3163 if (hasMinHeight) {
3164 // we need to set the minHeight of the notification
3165 minHeight = mContext.getResources().getDimensionPixelSize(
3166 com.android.internal.R.dimen.notification_min_content_height);
3167 }
3168 remoteView.setInt(R.id.notification_main_column, "setMinimumHeight", minHeight);
3169 }
3170
Selim Cinek29603462015-11-17 19:04:39 -08003171 private boolean handleProgressBar(boolean hasProgress, RemoteViews contentView, Bundle ex) {
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003172 final int max = ex.getInt(EXTRA_PROGRESS_MAX, 0);
3173 final int progress = ex.getInt(EXTRA_PROGRESS, 0);
3174 final boolean ind = ex.getBoolean(EXTRA_PROGRESS_INDETERMINATE);
3175 if (hasProgress && (max != 0 || ind)) {
Selim Cinek29603462015-11-17 19:04:39 -08003176 contentView.setViewVisibility(com.android.internal.R.id.progress, View.VISIBLE);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003177 contentView.setProgressBar(
Selim Cinek29603462015-11-17 19:04:39 -08003178 R.id.progress, max, progress, ind);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003179 contentView.setProgressBackgroundTintList(
3180 R.id.progress, ColorStateList.valueOf(mContext.getColor(
3181 R.color.notification_progress_background_color)));
Selim Cinek29603462015-11-17 19:04:39 -08003182 if (mN.color != COLOR_DEFAULT) {
Adrian Roos4ff3b122016-02-01 12:26:13 -08003183 ColorStateList colorStateList = ColorStateList.valueOf(resolveContrastColor());
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003184 contentView.setProgressTintList(R.id.progress, colorStateList);
3185 contentView.setProgressIndeterminateTintList(R.id.progress, colorStateList);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003186 }
Selim Cinek29603462015-11-17 19:04:39 -08003187 return true;
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003188 } else {
3189 contentView.setViewVisibility(R.id.progress, View.GONE);
Selim Cinek29603462015-11-17 19:04:39 -08003190 return false;
Jeff Sharkey1c400132011-08-05 14:50:13 -07003191 }
Joe Onorato561d3852010-11-20 18:09:34 -08003192 }
3193
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003194 private void bindLargeIcon(RemoteViews contentView) {
3195 if (mN.mLargeIcon != null) {
3196 contentView.setViewVisibility(R.id.right_icon, View.VISIBLE);
3197 contentView.setImageViewIcon(R.id.right_icon, mN.mLargeIcon);
3198 processLargeLegacyIcon(mN.mLargeIcon, contentView);
3199 int endMargin = mContext.getResources().getDimensionPixelSize(
3200 R.dimen.notification_content_picture_margin);
3201 contentView.setViewLayoutMarginEnd(R.id.line1, endMargin);
Selim Cinekc848c3a2016-01-13 15:27:30 -08003202 contentView.setViewLayoutMarginEnd(R.id.text, endMargin);
Selim Cinek29603462015-11-17 19:04:39 -08003203 contentView.setViewLayoutMarginEnd(R.id.progress, endMargin);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003204 }
3205 }
3206
3207 private void bindNotificationHeader(RemoteViews contentView) {
3208 bindSmallIcon(contentView);
3209 bindHeaderAppName(contentView);
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07003210 bindHeaderText(contentView);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003211 bindHeaderChronometerAndTime(contentView);
3212 bindExpandButton(contentView);
Selim Cinekc848c3a2016-01-13 15:27:30 -08003213 bindProfileBadge(contentView);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003214 }
3215
3216 private void bindExpandButton(RemoteViews contentView) {
Adrian Roos4ff3b122016-02-01 12:26:13 -08003217 contentView.setDrawableParameters(R.id.expand_button, false, -1, resolveContrastColor(),
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003218 PorterDuff.Mode.SRC_ATOP, -1);
Selim Cinekea4bef72015-12-02 15:51:10 -08003219 contentView.setInt(R.id.notification_header, "setOriginalNotificationColor",
Adrian Roos4ff3b122016-02-01 12:26:13 -08003220 resolveContrastColor());
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003221 }
3222
3223 private void bindHeaderChronometerAndTime(RemoteViews contentView) {
3224 if (showsTimeOrChronometer()) {
Selim Cinek29603462015-11-17 19:04:39 -08003225 contentView.setViewVisibility(R.id.time_divider, View.VISIBLE);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003226 if (mN.extras.getBoolean(EXTRA_SHOW_CHRONOMETER)) {
3227 contentView.setViewVisibility(R.id.chronometer, View.VISIBLE);
3228 contentView.setLong(R.id.chronometer, "setBase",
3229 mN.when + (SystemClock.elapsedRealtime() - System.currentTimeMillis()));
3230 contentView.setBoolean(R.id.chronometer, "setStarted", true);
Selim Cinek81c23aa2016-02-25 16:23:13 -08003231 boolean countsDown = mN.extras.getBoolean(EXTRA_CHRONOMETER_COUNTS_DOWN);
3232 contentView.setChronometerCountsDown(R.id.chronometer, countsDown);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003233 } else {
3234 contentView.setViewVisibility(R.id.time, View.VISIBLE);
3235 contentView.setLong(R.id.time, "setTime", mN.when);
3236 }
3237 }
3238 }
3239
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07003240 private void bindHeaderText(RemoteViews contentView) {
3241 CharSequence headerText = mN.extras.getCharSequence(EXTRA_SUB_TEXT);
3242 if (headerText == null && mStyle != null && mStyle.mSummaryTextSet
Selim Cinek03d0d652015-11-13 13:18:09 -05003243 && mStyle.hasSummaryInHeader()) {
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07003244 headerText = mStyle.mSummaryText;
Selim Cinek03d0d652015-11-13 13:18:09 -05003245 }
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07003246 if (headerText == null
3247 && mContext.getApplicationInfo().targetSdkVersion < Build.VERSION_CODES.N
3248 && mN.extras.getCharSequence(EXTRA_INFO_TEXT) != null) {
3249 headerText = mN.extras.getCharSequence(EXTRA_INFO_TEXT);
3250 }
3251 if (headerText != null) {
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003252 // TODO: Remove the span entirely to only have the string with propper formating.
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07003253 contentView.setTextViewText(R.id.header_text, processLegacyText(headerText));
3254 contentView.setViewVisibility(R.id.header_text, View.VISIBLE);
3255 contentView.setViewVisibility(R.id.header_text_divider, View.VISIBLE);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003256 }
3257 }
3258
3259 private void bindHeaderAppName(RemoteViews contentView) {
Tony Mak4dc008c2016-03-16 10:46:49 +00003260 CharSequence appName = mContext.getPackageManager()
3261 .getApplicationLabel(mContext.getApplicationInfo());
3262
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003263 if (TextUtils.isEmpty(appName)) {
3264 return;
3265 }
3266 contentView.setTextViewText(R.id.app_name_text, appName);
Adrian Roos4ff3b122016-02-01 12:26:13 -08003267 contentView.setTextColor(R.id.app_name_text, resolveContrastColor());
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003268 }
3269
3270 private void bindSmallIcon(RemoteViews contentView) {
3271 contentView.setImageViewIcon(R.id.icon, mN.mSmallIcon);
3272 processSmallIconColor(mN.mSmallIcon, contentView);
3273 }
3274
Jorim Jaggi445d3c02014-08-19 22:33:42 +02003275 /**
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02003276 * @return true if the built notification will show the time or the chronometer; false
3277 * otherwise
3278 */
3279 private boolean showsTimeOrChronometer() {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003280 return mN.when != 0 && mN.extras.getBoolean(EXTRA_SHOW_WHEN);
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02003281 }
3282
Christoph Studerfe718432014-09-01 18:21:18 +02003283 private void resetStandardTemplateWithActions(RemoteViews big) {
Adrian Roos7052de52016-03-03 15:53:34 -08003284 big.setViewVisibility(R.id.actions_container, View.GONE);
Christoph Studerfe718432014-09-01 18:21:18 +02003285 big.setViewVisibility(R.id.actions, View.GONE);
Christoph Studerfe718432014-09-01 18:21:18 +02003286 big.removeAllViews(R.id.actions);
Adrian Roose458aa82015-12-08 16:17:19 -08003287
3288 big.setViewVisibility(R.id.notification_material_reply_container, View.GONE);
3289 big.setTextViewText(R.id.notification_material_reply_text_1, null);
3290
3291 big.setViewVisibility(R.id.notification_material_reply_text_2, View.GONE);
3292 big.setTextViewText(R.id.notification_material_reply_text_2, null);
3293 big.setViewVisibility(R.id.notification_material_reply_text_3, View.GONE);
3294 big.setTextViewText(R.id.notification_material_reply_text_3, null);
Christoph Studerfe718432014-09-01 18:21:18 +02003295 }
3296
Daniel Sandler96fd7c12012-03-30 16:37:36 -04003297 private RemoteViews applyStandardTemplateWithActions(int layoutId) {
Jorim Jaggi445d3c02014-08-19 22:33:42 +02003298 RemoteViews big = applyStandardTemplate(layoutId);
Daniel Sandler96fd7c12012-03-30 16:37:36 -04003299
Christoph Studerfe718432014-09-01 18:21:18 +02003300 resetStandardTemplateWithActions(big);
3301
Adrian Roose458aa82015-12-08 16:17:19 -08003302 boolean validRemoteInput = false;
3303
Daniel Sandler96fd7c12012-03-30 16:37:36 -04003304 int N = mActions.size();
3305 if (N > 0) {
Adrian Roos7052de52016-03-03 15:53:34 -08003306 big.setViewVisibility(R.id.actions_container, View.VISIBLE);
Daniel Sandler96fd7c12012-03-30 16:37:36 -04003307 big.setViewVisibility(R.id.actions, View.VISIBLE);
Daniel Sandler8680bf82012-05-15 16:52:52 -04003308 if (N>MAX_ACTION_BUTTONS) N=MAX_ACTION_BUTTONS;
Daniel Sandler96fd7c12012-03-30 16:37:36 -04003309 for (int i=0; i<N; i++) {
Adrian Roose458aa82015-12-08 16:17:19 -08003310 Action action = mActions.get(i);
3311 validRemoteInput |= hasValidRemoteInput(action);
3312
3313 final RemoteViews button = generateActionButton(action);
Adrian Roos9b123cf2016-02-04 14:55:57 -08003314 if (i == N - 1) {
3315 button.setViewLayoutWidth(com.android.internal.R.id.action0,
3316 ViewGroup.LayoutParams.MATCH_PARENT);
3317 }
Daniel Sandler96fd7c12012-03-30 16:37:36 -04003318 big.addView(R.id.actions, button);
3319 }
3320 }
Adrian Roose458aa82015-12-08 16:17:19 -08003321
3322 CharSequence[] replyText = mN.extras.getCharSequenceArray(EXTRA_REMOTE_INPUT_HISTORY);
3323 if (validRemoteInput && replyText != null
3324 && replyText.length > 0 && !TextUtils.isEmpty(replyText[0])) {
3325 big.setViewVisibility(R.id.notification_material_reply_container, View.VISIBLE);
3326 big.setTextViewText(R.id.notification_material_reply_text_1, replyText[0]);
3327
3328 if (replyText.length > 1 && !TextUtils.isEmpty(replyText[1])) {
3329 big.setViewVisibility(R.id.notification_material_reply_text_2, View.VISIBLE);
3330 big.setTextViewText(R.id.notification_material_reply_text_2, replyText[1]);
3331
3332 if (replyText.length > 2 && !TextUtils.isEmpty(replyText[2])) {
3333 big.setViewVisibility(
3334 R.id.notification_material_reply_text_3, View.VISIBLE);
3335 big.setTextViewText(R.id.notification_material_reply_text_3, replyText[2]);
3336 }
3337 }
3338 }
3339
Daniel Sandler96fd7c12012-03-30 16:37:36 -04003340 return big;
3341 }
3342
Adrian Roose458aa82015-12-08 16:17:19 -08003343 private boolean hasValidRemoteInput(Action action) {
3344 if (TextUtils.isEmpty(action.title) || action.actionIntent == null) {
3345 // Weird actions
3346 return false;
3347 }
3348
3349 RemoteInput[] remoteInputs = action.getRemoteInputs();
3350 if (remoteInputs == null) {
3351 return false;
3352 }
3353
3354 for (RemoteInput r : remoteInputs) {
3355 CharSequence[] choices = r.getChoices();
3356 if (r.getAllowFreeFormInput() || (choices != null && choices.length != 0)) {
3357 return true;
3358 }
3359 }
3360 return false;
3361 }
3362
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003363 /**
3364 * Construct a RemoteViews for the final 1U notification layout. In order:
3365 * 1. Custom contentView from the caller
3366 * 2. Style's proposed content view
3367 * 3. Standard template view
3368 */
Julia Reynolds3b848122016-02-26 10:45:32 -05003369 public RemoteViews createContentView() {
Selim Cinek593610c2016-02-16 18:42:57 -08003370 if (mN.contentView != null && (mStyle == null || !mStyle.displayCustomViewInline())) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003371 return mN.contentView;
3372 } else if (mStyle != null) {
3373 final RemoteViews styleView = mStyle.makeContentView();
3374 if (styleView != null) {
3375 return styleView;
3376 }
Joe Onorato46439ce2010-11-19 13:56:21 -08003377 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003378 return applyStandardTemplate(getBaseLayoutResource());
Joe Onorato46439ce2010-11-19 13:56:21 -08003379 }
3380
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003381 /**
3382 * Construct a RemoteViews for the final big notification layout.
3383 */
Julia Reynolds3b848122016-02-26 10:45:32 -05003384 public RemoteViews createBigContentView() {
Selim Cinek850a8542015-11-11 11:48:36 -05003385 RemoteViews result = null;
Selim Cinek593610c2016-02-16 18:42:57 -08003386 if (mN.bigContentView != null
3387 && (mStyle == null || !mStyle.displayCustomViewInline())) {
Julia Reynolds089e3e42015-11-18 09:59:57 -05003388 return mN.bigContentView;
3389 } else if (mStyle != null) {
Selim Cinek850a8542015-11-11 11:48:36 -05003390 result = mStyle.makeBigContentView();
Selim Cinek90dcf6d2015-11-18 20:24:13 -08003391 hideLine1Text(result);
Selim Cinekcc10bfb2016-02-10 16:24:21 -08003392 } else if (mActions.size() != 0) {
3393 result = applyStandardTemplateWithActions(getBigBaseLayoutResource());
Selim Cinek850a8542015-11-11 11:48:36 -05003394 }
3395 adaptNotificationHeaderForBigContentView(result);
3396 return result;
3397 }
Daniel Sandler96fd7c12012-03-30 16:37:36 -04003398
Selim Cinekeaa29ca2015-11-23 13:51:13 -08003399 /**
3400 * Construct a RemoteViews for the final notification header only
3401 *
3402 * @hide
3403 */
3404 public RemoteViews makeNotificationHeader() {
3405 RemoteViews header = new BuilderRemoteViews(mContext.getApplicationInfo(),
3406 R.layout.notification_template_header);
3407 resetNotificationHeader(header);
3408 bindNotificationHeader(header);
3409 return header;
3410 }
3411
Selim Cinek29603462015-11-17 19:04:39 -08003412 private void hideLine1Text(RemoteViews result) {
Selim Cinekcc10bfb2016-02-10 16:24:21 -08003413 if (result != null) {
3414 result.setViewVisibility(R.id.text_line_1, View.GONE);
3415 }
Selim Cinek29603462015-11-17 19:04:39 -08003416 }
3417
Selim Cinek850a8542015-11-11 11:48:36 -05003418 private void adaptNotificationHeaderForBigContentView(RemoteViews result) {
Selim Cinekcc10bfb2016-02-10 16:24:21 -08003419 if (result != null) {
3420 result.setBoolean(R.id.notification_header, "setExpanded", true);
3421 }
Daniel Sandler96fd7c12012-03-30 16:37:36 -04003422 }
3423
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003424 /**
3425 * Construct a RemoteViews for the final heads-up notification layout.
3426 */
Julia Reynolds3b848122016-02-26 10:45:32 -05003427 public RemoteViews createHeadsUpContentView() {
Selim Cinek593610c2016-02-16 18:42:57 -08003428 if (mN.headsUpContentView != null
3429 && (mStyle == null || !mStyle.displayCustomViewInline())) {
Julia Reynolds089e3e42015-11-18 09:59:57 -05003430 return mN.headsUpContentView;
3431 } else if (mStyle != null) {
3432 final RemoteViews styleView = mStyle.makeHeadsUpContentView();
3433 if (styleView != null) {
3434 return styleView;
3435 }
3436 } else if (mActions.size() == 0) {
3437 return null;
3438 }
3439
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01003440 return applyStandardTemplateWithActions(getBigBaseLayoutResource());
Chris Wren8fd39ec2014-02-27 17:43:26 -05003441 }
3442
Selim Cinek624c02db2015-12-14 21:00:02 -08003443 /**
3444 * Construct a RemoteViews for the display in public contexts like on the lockscreen.
3445 *
3446 * @hide
3447 */
3448 public RemoteViews makePublicContentView() {
3449 if (mN.publicVersion != null) {
3450 final Builder builder = recoverBuilder(mContext, mN.publicVersion);
Julia Reynolds3b848122016-02-26 10:45:32 -05003451 return builder.createContentView();
Selim Cinek624c02db2015-12-14 21:00:02 -08003452 }
3453 Bundle savedBundle = mN.extras;
3454 Style style = mStyle;
3455 mStyle = null;
3456 Icon largeIcon = mN.mLargeIcon;
3457 mN.mLargeIcon = null;
3458 Bundle publicExtras = new Bundle();
3459 publicExtras.putBoolean(EXTRA_SHOW_WHEN,
3460 savedBundle.getBoolean(EXTRA_SHOW_WHEN));
3461 publicExtras.putBoolean(EXTRA_SHOW_CHRONOMETER,
3462 savedBundle.getBoolean(EXTRA_SHOW_CHRONOMETER));
Selim Cinek81c23aa2016-02-25 16:23:13 -08003463 publicExtras.putBoolean(EXTRA_CHRONOMETER_COUNTS_DOWN,
3464 savedBundle.getBoolean(EXTRA_CHRONOMETER_COUNTS_DOWN));
Selim Cinek624c02db2015-12-14 21:00:02 -08003465 publicExtras.putCharSequence(EXTRA_TITLE,
3466 mContext.getString(R.string.notification_hidden_text));
3467 mN.extras = publicExtras;
3468 final RemoteViews publicView = applyStandardTemplate(getBaseLayoutResource());
3469 mN.extras = savedBundle;
3470 mN.mLargeIcon = largeIcon;
3471 mStyle = style;
3472 return publicView;
3473 }
3474
3475
Chris Wren8fd39ec2014-02-27 17:43:26 -05003476
Daniel Sandler96fd7c12012-03-30 16:37:36 -04003477 private RemoteViews generateActionButton(Action action) {
Daniel Sandler8680bf82012-05-15 16:52:52 -04003478 final boolean tombstone = (action.actionIntent == null);
Selim Cinekf33b1112015-07-15 17:45:11 -07003479 RemoteViews button = new BuilderRemoteViews(mContext.getApplicationInfo(),
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01003480 tombstone ? getActionTombstoneLayoutResource()
3481 : getActionLayoutResource());
Dan Sandler68079d52015-07-22 10:45:30 -04003482 final Icon ai = action.getIcon();
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01003483 button.setTextViewText(R.id.action0, processLegacyText(action.title));
Daniel Sandler8680bf82012-05-15 16:52:52 -04003484 if (!tombstone) {
Daniel Sandlere5518842012-05-10 16:20:40 -04003485 button.setOnClickPendingIntent(R.id.action0, action.actionIntent);
Daniel Sandlere5518842012-05-10 16:20:40 -04003486 }
Daniel Sandler96fd7c12012-03-30 16:37:36 -04003487 button.setContentDescription(R.id.action0, action.title);
Adrian Roosfe84e1f2015-11-04 15:55:39 -08003488 if (action.mRemoteInputs != null) {
3489 button.setRemoteInputs(R.id.action0, action.mRemoteInputs);
3490 }
3491 if (mN.color != COLOR_DEFAULT) {
Adrian Roos4ff3b122016-02-01 12:26:13 -08003492 button.setTextColor(R.id.action0, resolveContrastColor());
Adrian Roosfe84e1f2015-11-04 15:55:39 -08003493 }
Daniel Sandler96fd7c12012-03-30 16:37:36 -04003494 return button;
3495 }
3496
Joe Onoratocb109a02011-01-18 17:57:41 -08003497 /**
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01003498 * @return Whether we are currently building a notification from a legacy (an app that
Alan Viverette3cb07a462014-06-06 14:19:53 -07003499 * doesn't create material notifications by itself) app.
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01003500 */
3501 private boolean isLegacy() {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003502 return getColorUtil() != null;
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01003503 }
3504
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01003505 private CharSequence processLegacyText(CharSequence charSequence) {
3506 if (isLegacy()) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003507 return getColorUtil().invertCharSequenceColors(charSequence);
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01003508 } else {
3509 return charSequence;
3510 }
3511 }
3512
Dan Sandler26e81cf2014-05-06 10:01:27 -04003513 /**
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003514 * Apply any necessariy colors to the small icon
Dan Sandler26e81cf2014-05-06 10:01:27 -04003515 */
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003516 private void processSmallIconColor(Icon smallIcon, RemoteViews contentView) {
Selim Cinekea4bef72015-12-02 15:51:10 -08003517 boolean colorable = !isLegacy() || getColorUtil().isGrayscaleIcon(mContext, smallIcon);
3518 if (colorable) {
Adrian Roos4ff3b122016-02-01 12:26:13 -08003519 contentView.setDrawableParameters(R.id.icon, false, -1, resolveContrastColor(),
Jorim Jaggi92df1f22014-12-16 19:44:41 +01003520 PorterDuff.Mode.SRC_ATOP, -1);
Selim Cinekea4bef72015-12-02 15:51:10 -08003521
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01003522 }
Selim Cinekea4bef72015-12-02 15:51:10 -08003523 contentView.setInt(R.id.notification_header, "setOriginalIconColor",
Adrian Roos4ff3b122016-02-01 12:26:13 -08003524 colorable ? resolveContrastColor() : NotificationHeaderView.NO_COLOR);
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01003525 }
3526
Dan Sandler26e81cf2014-05-06 10:01:27 -04003527 /**
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003528 * Make the largeIcon dark if it's a fake smallIcon (that is,
Dan Sandler26e81cf2014-05-06 10:01:27 -04003529 * if it's grayscale).
3530 */
3531 // TODO: also check bounds, transparency, that sort of thing.
Dan Sandlerd63f9322015-05-06 15:18:49 -04003532 private void processLargeLegacyIcon(Icon largeIcon, RemoteViews contentView) {
3533 if (largeIcon != null && isLegacy()
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003534 && getColorUtil().isGrayscaleIcon(mContext, largeIcon)) {
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003535 // resolve color will fall back to the default when legacy
Adrian Roos4ff3b122016-02-01 12:26:13 -08003536 contentView.setDrawableParameters(R.id.icon, false, -1, resolveContrastColor(),
Dan Sandler190d58d2014-05-15 09:33:39 -04003537 PorterDuff.Mode.SRC_ATOP, -1);
Jorim Jaggi92df1f22014-12-16 19:44:41 +01003538 }
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01003539 }
3540
Julia Reynolds10ee1fc2015-11-09 11:04:55 -05003541 private void sanitizeColor() {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003542 if (mN.color != COLOR_DEFAULT) {
3543 mN.color |= 0xFF000000; // no alpha for custom colors
Jorim Jaggi74419312014-06-10 20:57:21 +02003544 }
Jorim Jaggi74419312014-06-10 20:57:21 +02003545 }
3546
Adrian Roos4ff3b122016-02-01 12:26:13 -08003547 int resolveContrastColor() {
3548 if (mCachedContrastColorIsFor == mN.color && mCachedContrastColor != COLOR_INVALID) {
3549 return mCachedContrastColor;
Dan Sandler26e81cf2014-05-06 10:01:27 -04003550 }
Adrian Roos4ff3b122016-02-01 12:26:13 -08003551 final int contrasted = NotificationColorUtil.resolveContrastColor(mContext, mN.color);
3552
3553 mCachedContrastColorIsFor = mN.color;
3554 return mCachedContrastColor = contrasted;
Dan Sandler26e81cf2014-05-06 10:01:27 -04003555 }
3556
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01003557 /**
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003558 * Apply the unstyled operations and return a new {@link Notification} object.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04003559 * @hide
Joe Onoratocb109a02011-01-18 17:57:41 -08003560 */
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04003561 public Notification buildUnstyled() {
Daniel Sandlera0a938c2012-03-15 08:42:37 -04003562 if (mActions.size() > 0) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003563 mN.actions = new Action[mActions.size()];
3564 mActions.toArray(mN.actions);
Daniel Sandlera0a938c2012-03-15 08:42:37 -04003565 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003566 if (!mPersonList.isEmpty()) {
3567 mN.extras.putStringArray(EXTRA_PEOPLE,
3568 mPersonList.toArray(new String[mPersonList.size()]));
Dan Sandler0bf2ed82013-12-21 23:33:41 -06003569 }
Selim Cinek247fa012016-02-18 09:50:48 -08003570 if (mN.bigContentView != null || mN.contentView != null
3571 || mN.headsUpContentView != null) {
3572 mN.extras.putBoolean(EXTRA_CONTAINS_CUSTOM_VIEW, true);
3573 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003574 return mN;
Joe Onorato46439ce2010-11-19 13:56:21 -08003575 }
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003576
Julia Reynolds3b848122016-02-26 10:45:32 -05003577 /**
3578 * Creates a Builder from an existing notification so further changes can be made.
3579 * @param context The context for your application / activity.
3580 * @param n The notification to create a Builder from.
3581 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003582 public static Notification.Builder recoverBuilder(Context context, Notification n) {
Christoph Studer4600f9b2014-07-22 22:44:43 +02003583 // Re-create notification context so we can access app resources.
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003584 ApplicationInfo applicationInfo = n.extras.getParcelable(
3585 EXTRA_BUILDER_APPLICATION_INFO);
Christoph Studer4600f9b2014-07-22 22:44:43 +02003586 Context builderContext;
Julia Reynoldsda303542015-11-23 14:00:20 -05003587 if (applicationInfo != null) {
3588 try {
3589 builderContext = context.createApplicationContext(applicationInfo,
3590 Context.CONTEXT_RESTRICTED);
3591 } catch (NameNotFoundException e) {
3592 Log.e(TAG, "ApplicationInfo " + applicationInfo + " not found");
3593 builderContext = context; // try with our context
3594 }
3595 } else {
3596 builderContext = context; // try with given context
Christoph Studer4600f9b2014-07-22 22:44:43 +02003597 }
3598
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003599 return new Builder(builderContext, n);
Christoph Studer4600f9b2014-07-22 22:44:43 +02003600 }
3601
3602 private static Class<? extends Style> getNotificationStyleClass(String templateClass) {
Selim Cinek593610c2016-02-16 18:42:57 -08003603 Class<? extends Style>[] classes = new Class[] {
3604 BigTextStyle.class, BigPictureStyle.class, InboxStyle.class, MediaStyle.class,
Alex Hillsfc737de2016-03-23 17:33:02 -04003605 DecoratedCustomViewStyle.class, DecoratedMediaCustomViewStyle.class,
3606 MessagingStyle.class };
Christoph Studer4600f9b2014-07-22 22:44:43 +02003607 for (Class<? extends Style> innerClass : classes) {
3608 if (templateClass.equals(innerClass.getName())) {
3609 return innerClass;
3610 }
3611 }
3612 return null;
3613 }
3614
Daniel Sandlerf45564e2013-04-15 15:05:08 -04003615 /**
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003616 * @deprecated Use {@link #build()} instead.
3617 */
3618 @Deprecated
3619 public Notification getNotification() {
3620 return build();
3621 }
3622
3623 /**
3624 * Combine all of the options that have been set and return a new {@link Notification}
3625 * object.
3626 */
3627 public Notification build() {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003628 // first, add any extras from the calling code
3629 if (mUserExtras != null) {
3630 mN.extras = getAllExtras();
Jorim Jaggia0d58ae2015-06-03 11:48:13 -07003631 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003632
3633 // lazy stuff from mContext; see comment in Builder(Context, Notification)
Julia Reynoldsda303542015-11-23 14:00:20 -05003634 Notification.addFieldsFromContext(mContext, mN);
Christoph Studer943aa672014-08-03 20:31:16 +02003635
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003636 buildUnstyled();
Daniel Sandlerf45564e2013-04-15 15:05:08 -04003637
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003638 if (mStyle != null) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003639 mStyle.buildStyled(mN);
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003640 }
Daniel Sandlerf45564e2013-04-15 15:05:08 -04003641
Adrian Roos5081c0d2016-02-26 16:04:19 -08003642 if (mContext.getApplicationInfo().targetSdkVersion < Build.VERSION_CODES.N
3643 && (mStyle == null || !mStyle.displayCustomViewInline())) {
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05003644 if (mN.contentView == null) {
Julia Reynolds3b848122016-02-26 10:45:32 -05003645 mN.contentView = createContentView();
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05003646 mN.extras.putInt(EXTRA_REBUILD_CONTENT_VIEW_ACTION_COUNT,
3647 mN.contentView.getSequenceNumber());
3648 }
3649 if (mN.bigContentView == null) {
Julia Reynolds3b848122016-02-26 10:45:32 -05003650 mN.bigContentView = createBigContentView();
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05003651 if (mN.bigContentView != null) {
3652 mN.extras.putInt(EXTRA_REBUILD_BIG_CONTENT_VIEW_ACTION_COUNT,
3653 mN.bigContentView.getSequenceNumber());
3654 }
3655 }
3656 if (mN.headsUpContentView == null) {
Julia Reynolds3b848122016-02-26 10:45:32 -05003657 mN.headsUpContentView = createHeadsUpContentView();
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05003658 if (mN.headsUpContentView != null) {
3659 mN.extras.putInt(EXTRA_REBUILD_HEADS_UP_CONTENT_VIEW_ACTION_COUNT,
3660 mN.headsUpContentView.getSequenceNumber());
3661 }
3662 }
3663 }
3664
Julia Reynolds4c0c2022016-02-02 15:11:59 -05003665 if ((mN.defaults & DEFAULT_LIGHTS) != 0) {
3666 mN.flags |= FLAG_SHOW_LIGHTS;
3667 }
3668
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003669 return mN;
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003670 }
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05003671
3672 /**
3673 * Apply this Builder to an existing {@link Notification} object.
3674 *
3675 * @hide
3676 */
3677 public Notification buildInto(Notification n) {
Daniel Sandler1a497d32013-04-18 14:52:45 -04003678 build().cloneInto(n, true);
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05003679 return n;
3680 }
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01003681
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05003682 /**
Adrian Roos184bfe022016-03-03 13:41:44 -08003683 * Removes RemoteViews that were created for compatibility from {@param n}, if they did not
3684 * change.
3685 *
3686 * @return {@param n}, if no stripping is needed, otherwise a stripped clone of {@param n}.
3687 *
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05003688 * @hide
3689 */
Adrian Roos184bfe022016-03-03 13:41:44 -08003690 public static Notification maybeCloneStrippedForDelivery(Notification n) {
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05003691 String templateClass = n.extras.getString(EXTRA_TEMPLATE);
Adrian Roos184bfe022016-03-03 13:41:44 -08003692
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05003693 // Only strip views for known Styles because we won't know how to
3694 // re-create them otherwise.
Adrian Roos184bfe022016-03-03 13:41:44 -08003695 if (!TextUtils.isEmpty(templateClass)
3696 && getNotificationStyleClass(templateClass) == null) {
3697 return n;
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05003698 }
Adrian Roos184bfe022016-03-03 13:41:44 -08003699
3700 // Only strip unmodified BuilderRemoteViews.
3701 boolean stripContentView = n.contentView instanceof BuilderRemoteViews &&
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05003702 n.extras.getInt(EXTRA_REBUILD_CONTENT_VIEW_ACTION_COUNT, -1) ==
Adrian Roos184bfe022016-03-03 13:41:44 -08003703 n.contentView.getSequenceNumber();
3704 boolean stripBigContentView = n.bigContentView instanceof BuilderRemoteViews &&
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05003705 n.extras.getInt(EXTRA_REBUILD_BIG_CONTENT_VIEW_ACTION_COUNT, -1) ==
Adrian Roos184bfe022016-03-03 13:41:44 -08003706 n.bigContentView.getSequenceNumber();
3707 boolean stripHeadsUpContentView = n.headsUpContentView instanceof BuilderRemoteViews &&
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05003708 n.extras.getInt(EXTRA_REBUILD_HEADS_UP_CONTENT_VIEW_ACTION_COUNT, -1) ==
Adrian Roos184bfe022016-03-03 13:41:44 -08003709 n.headsUpContentView.getSequenceNumber();
3710
3711 // Nothing to do here, no need to clone.
3712 if (!stripContentView && !stripBigContentView && !stripHeadsUpContentView) {
3713 return n;
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05003714 }
Adrian Roos184bfe022016-03-03 13:41:44 -08003715
3716 Notification clone = n.clone();
3717 if (stripContentView) {
3718 clone.contentView = null;
3719 clone.extras.remove(EXTRA_REBUILD_CONTENT_VIEW_ACTION_COUNT);
3720 }
3721 if (stripBigContentView) {
3722 clone.bigContentView = null;
3723 clone.extras.remove(EXTRA_REBUILD_BIG_CONTENT_VIEW_ACTION_COUNT);
3724 }
3725 if (stripHeadsUpContentView) {
3726 clone.headsUpContentView = null;
3727 clone.extras.remove(EXTRA_REBUILD_HEADS_UP_CONTENT_VIEW_ACTION_COUNT);
3728 }
3729 return clone;
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05003730 }
3731
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01003732 private int getBaseLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07003733 return R.layout.notification_template_material_base;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01003734 }
3735
3736 private int getBigBaseLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07003737 return R.layout.notification_template_material_big_base;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01003738 }
3739
3740 private int getBigPictureLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07003741 return R.layout.notification_template_material_big_picture;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01003742 }
3743
3744 private int getBigTextLayoutResource() {
Jorim Jaggi445d3c02014-08-19 22:33:42 +02003745 return R.layout.notification_template_material_big_text;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01003746 }
3747
3748 private int getInboxLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07003749 return R.layout.notification_template_material_inbox;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01003750 }
3751
3752 private int getActionLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07003753 return R.layout.notification_material_action;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01003754 }
3755
3756 private int getActionTombstoneLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07003757 return R.layout.notification_material_action_tombstone;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01003758 }
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003759 }
3760
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003761 /**
3762 * An object that can apply a rich notification style to a {@link Notification.Builder}
3763 * object.
3764 */
Griff Hazendfcb0802014-02-11 12:00:00 -08003765 public static abstract class Style {
Chris Wrend6297db2012-05-03 16:20:13 -04003766 private CharSequence mBigContentTitle;
Jorim Jaggi457a10d2014-09-08 16:18:23 +02003767
3768 /**
3769 * @hide
3770 */
3771 protected CharSequence mSummaryText = null;
3772
3773 /**
3774 * @hide
3775 */
3776 protected boolean mSummaryTextSet = false;
Chris Wrend6297db2012-05-03 16:20:13 -04003777
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003778 protected Builder mBuilder;
3779
Chris Wrend6297db2012-05-03 16:20:13 -04003780 /**
3781 * Overrides ContentTitle in the big form of the template.
3782 * This defaults to the value passed to setContentTitle().
3783 */
3784 protected void internalSetBigContentTitle(CharSequence title) {
3785 mBigContentTitle = title;
3786 }
3787
3788 /**
3789 * Set the first line of text after the detail section in the big form of the template.
3790 */
3791 protected void internalSetSummaryText(CharSequence cs) {
3792 mSummaryText = cs;
Daniel Sandler619738c2012-06-07 16:33:08 -04003793 mSummaryTextSet = true;
Chris Wrend6297db2012-05-03 16:20:13 -04003794 }
3795
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003796 public void setBuilder(Builder builder) {
3797 if (mBuilder != builder) {
3798 mBuilder = builder;
Daniel Sandlerc08dea22012-06-28 08:35:24 -07003799 if (mBuilder != null) {
3800 mBuilder.setStyle(this);
3801 }
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003802 }
3803 }
3804
Chris Wrend6297db2012-05-03 16:20:13 -04003805 protected void checkBuilder() {
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003806 if (mBuilder == null) {
3807 throw new IllegalArgumentException("Style requires a valid Builder object");
3808 }
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003809 }
Chris Wrend6297db2012-05-03 16:20:13 -04003810
3811 protected RemoteViews getStandardView(int layoutId) {
3812 checkBuilder();
3813
Christoph Studer4600f9b2014-07-22 22:44:43 +02003814 // Nasty.
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003815 CharSequence oldBuilderContentTitle =
3816 mBuilder.getAllExtras().getCharSequence(EXTRA_TITLE);
Chris Wrend6297db2012-05-03 16:20:13 -04003817 if (mBigContentTitle != null) {
3818 mBuilder.setContentTitle(mBigContentTitle);
3819 }
3820
Chris Wrend6297db2012-05-03 16:20:13 -04003821 RemoteViews contentView = mBuilder.applyStandardTemplateWithActions(layoutId);
3822
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003823 mBuilder.getAllExtras().putCharSequence(EXTRA_TITLE, oldBuilderContentTitle);
Christoph Studer4600f9b2014-07-22 22:44:43 +02003824
Chris Wrend6297db2012-05-03 16:20:13 -04003825 if (mBigContentTitle != null && mBigContentTitle.equals("")) {
3826 contentView.setViewVisibility(R.id.line1, View.GONE);
Chris Wren67dc9a02012-05-16 01:03:20 -04003827 } else {
3828 contentView.setViewVisibility(R.id.line1, View.VISIBLE);
Chris Wrend6297db2012-05-03 16:20:13 -04003829 }
3830
Chris Wrend6297db2012-05-03 16:20:13 -04003831 return contentView;
3832 }
3833
Daniel Sandlerf45564e2013-04-15 15:05:08 -04003834 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003835 * Construct a Style-specific RemoteViews for the final 1U notification layout.
3836 * The default implementation has nothing additional to add.
3837 * @hide
3838 */
3839 public RemoteViews makeContentView() {
3840 return null;
3841 }
3842
3843 /**
3844 * Construct a Style-specific RemoteViews for the final big notification layout.
3845 * @hide
3846 */
3847 public RemoteViews makeBigContentView() {
3848 return null;
3849 }
3850
3851 /**
3852 * Construct a Style-specific RemoteViews for the final HUN layout.
3853 * @hide
3854 */
3855 public RemoteViews makeHeadsUpContentView() {
3856 return null;
3857 }
3858
3859 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003860 * Apply any style-specific extras to this notification before shipping it out.
Daniel Sandlerf45564e2013-04-15 15:05:08 -04003861 * @hide
3862 */
3863 public void addExtras(Bundle extras) {
3864 if (mSummaryTextSet) {
3865 extras.putCharSequence(EXTRA_SUMMARY_TEXT, mSummaryText);
3866 }
3867 if (mBigContentTitle != null) {
3868 extras.putCharSequence(EXTRA_TITLE_BIG, mBigContentTitle);
3869 }
Chris Wren91ad5632013-06-05 15:05:57 -04003870 extras.putString(EXTRA_TEMPLATE, this.getClass().getName());
Daniel Sandlerf45564e2013-04-15 15:05:08 -04003871 }
3872
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04003873 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003874 * Reconstruct the internal state of this Style object from extras.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04003875 * @hide
3876 */
Christoph Studer4600f9b2014-07-22 22:44:43 +02003877 protected void restoreFromExtras(Bundle extras) {
3878 if (extras.containsKey(EXTRA_SUMMARY_TEXT)) {
3879 mSummaryText = extras.getCharSequence(EXTRA_SUMMARY_TEXT);
3880 mSummaryTextSet = true;
3881 }
3882 if (extras.containsKey(EXTRA_TITLE_BIG)) {
3883 mBigContentTitle = extras.getCharSequence(EXTRA_TITLE_BIG);
3884 }
3885 }
3886
3887
3888 /**
3889 * @hide
3890 */
3891 public Notification buildStyled(Notification wip) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003892 addExtras(wip.extras);
Christoph Studer4600f9b2014-07-22 22:44:43 +02003893 return wip;
3894 }
3895
Daniel Sandler0ec46202015-06-24 01:27:05 -04003896 /**
3897 * @hide
3898 */
Jorim Jaggia0d58ae2015-06-03 11:48:13 -07003899 public void purgeResources() {}
3900
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04003901 /**
3902 * Calls {@link android.app.Notification.Builder#build()} on the Builder this Style is
3903 * attached to.
3904 *
3905 * @return the fully constructed Notification.
3906 */
3907 public Notification build() {
3908 checkBuilder();
3909 return mBuilder.build();
3910 }
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02003911
3912 /**
3913 * @hide
3914 * @return true if the style positions the progress bar on the second line; false if the
3915 * style hides the progress bar
3916 */
3917 protected boolean hasProgress() {
3918 return true;
3919 }
Selim Cinek03d0d652015-11-13 13:18:09 -05003920
3921 /**
3922 * @hide
3923 * @return Whether we should put the summary be put into the notification header
3924 */
3925 public boolean hasSummaryInHeader() {
3926 return true;
3927 }
Selim Cinek593610c2016-02-16 18:42:57 -08003928
3929 /**
3930 * @hide
3931 * @return Whether custom content views are displayed inline in the style
3932 */
3933 public boolean displayCustomViewInline() {
3934 return false;
3935 }
Joe Onorato46439ce2010-11-19 13:56:21 -08003936 }
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003937
3938 /**
Daniel Sandler4dfbe832012-04-11 14:51:46 -04003939 * Helper class for generating large-format notifications that include a large image attachment.
Joe Malin8d40d042012-11-05 11:36:40 -08003940 *
Robert Ly91c5ce32014-06-08 15:37:00 -07003941 * Here's how you'd set the <code>BigPictureStyle</code> on a notification:
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003942 * <pre class="prettyprint">
Robert Ly91c5ce32014-06-08 15:37:00 -07003943 * Notification notif = new Notification.Builder(mContext)
3944 * .setContentTitle(&quot;New photo from &quot; + sender.toString())
3945 * .setContentText(subject)
3946 * .setSmallIcon(R.drawable.new_post)
3947 * .setLargeIcon(aBitmap)
3948 * .setStyle(new Notification.BigPictureStyle()
3949 * .bigPicture(aBigBitmap))
3950 * .build();
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003951 * </pre>
Joe Malin8d40d042012-11-05 11:36:40 -08003952 *
Daniel Sandler4dfbe832012-04-11 14:51:46 -04003953 * @see Notification#bigContentView
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003954 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003955 public static class BigPictureStyle extends Style {
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003956 private Bitmap mPicture;
Dan Sandlerd63f9322015-05-06 15:18:49 -04003957 private Icon mBigLargeIcon;
Chris Wren3745a3d2012-05-22 15:11:52 -04003958 private boolean mBigLargeIconSet = false;
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003959
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003960 public BigPictureStyle() {
3961 }
3962
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003963 public BigPictureStyle(Builder builder) {
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003964 setBuilder(builder);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003965 }
3966
Chris Wrend6297db2012-05-03 16:20:13 -04003967 /**
3968 * Overrides ContentTitle in the big form of the template.
3969 * This defaults to the value passed to setContentTitle().
3970 */
3971 public BigPictureStyle setBigContentTitle(CharSequence title) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04003972 internalSetBigContentTitle(safeCharSequence(title));
Chris Wrend6297db2012-05-03 16:20:13 -04003973 return this;
3974 }
3975
3976 /**
3977 * Set the first line of text after the detail section in the big form of the template.
3978 */
3979 public BigPictureStyle setSummaryText(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04003980 internalSetSummaryText(safeCharSequence(cs));
Chris Wrend6297db2012-05-03 16:20:13 -04003981 return this;
3982 }
3983
Chris Wren0bd664d2012-08-01 13:56:56 -04003984 /**
3985 * Provide the bitmap to be used as the payload for the BigPicture notification.
3986 */
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003987 public BigPictureStyle bigPicture(Bitmap b) {
3988 mPicture = b;
3989 return this;
3990 }
3991
Chris Wren3745a3d2012-05-22 15:11:52 -04003992 /**
Chris Wren3745a3d2012-05-22 15:11:52 -04003993 * Override the large icon when the big notification is shown.
3994 */
3995 public BigPictureStyle bigLargeIcon(Bitmap b) {
Dan Sandlerd63f9322015-05-06 15:18:49 -04003996 return bigLargeIcon(b != null ? Icon.createWithBitmap(b) : null);
3997 }
3998
3999 /**
4000 * Override the large icon when the big notification is shown.
4001 */
4002 public BigPictureStyle bigLargeIcon(Icon icon) {
Chris Wren3745a3d2012-05-22 15:11:52 -04004003 mBigLargeIconSet = true;
Dan Sandlerd63f9322015-05-06 15:18:49 -04004004 mBigLargeIcon = icon;
Chris Wren3745a3d2012-05-22 15:11:52 -04004005 return this;
4006 }
4007
Riley Andrews0394a0c2015-11-03 23:36:52 -08004008 /** @hide */
4009 public static final int MIN_ASHMEM_BITMAP_SIZE = 128 * (1 << 10);
4010
Daniel Sandler0ec46202015-06-24 01:27:05 -04004011 /**
4012 * @hide
4013 */
Jorim Jaggia0d58ae2015-06-03 11:48:13 -07004014 @Override
4015 public void purgeResources() {
4016 super.purgeResources();
Riley Andrews8cee7c12015-11-01 23:36:04 -08004017 if (mPicture != null &&
4018 mPicture.isMutable() &&
Riley Andrews0394a0c2015-11-03 23:36:52 -08004019 mPicture.getAllocationByteCount() >= MIN_ASHMEM_BITMAP_SIZE) {
Jorim Jaggia0d58ae2015-06-03 11:48:13 -07004020 mPicture = mPicture.createAshmemBitmap();
4021 }
4022 if (mBigLargeIcon != null) {
4023 mBigLargeIcon.convertToAshmem();
4024 }
4025 }
Christoph Studer5c510ee2014-12-15 16:32:27 +01004026
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004027 /**
4028 * @hide
4029 */
4030 public RemoteViews makeBigContentView() {
4031 // Replace mN.mLargeIcon with mBigLargeIcon if mBigLargeIconSet
Christoph Studer5c510ee2014-12-15 16:32:27 +01004032 // This covers the following cases:
4033 // 1. mBigLargeIconSet -> mBigLargeIcon (null or non-null) applies, overrides
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004034 // mN.mLargeIcon
4035 // 2. !mBigLargeIconSet -> mN.mLargeIcon applies
Dan Sandlerd63f9322015-05-06 15:18:49 -04004036 Icon oldLargeIcon = null;
Christoph Studer5c510ee2014-12-15 16:32:27 +01004037 if (mBigLargeIconSet) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004038 oldLargeIcon = mBuilder.mN.mLargeIcon;
4039 mBuilder.mN.mLargeIcon = mBigLargeIcon;
Christoph Studer5c510ee2014-12-15 16:32:27 +01004040 }
4041
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01004042 RemoteViews contentView = getStandardView(mBuilder.getBigPictureLayoutResource());
Selim Cinek03d0d652015-11-13 13:18:09 -05004043 if (mSummaryTextSet) {
4044 contentView.setTextViewText(R.id.text, mBuilder.processLegacyText(mSummaryText));
Selim Cinekc848c3a2016-01-13 15:27:30 -08004045 contentView.setViewVisibility(R.id.text, View.VISIBLE);
Selim Cinek03d0d652015-11-13 13:18:09 -05004046 }
Selim Cinek860b6da2015-12-16 19:02:19 -08004047 mBuilder.setContentMinHeight(contentView, mBuilder.mN.mLargeIcon != null);
Selim Cinek53e64a42015-11-16 10:40:56 -08004048
Christoph Studer5c510ee2014-12-15 16:32:27 +01004049 if (mBigLargeIconSet) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004050 mBuilder.mN.mLargeIcon = oldLargeIcon;
Christoph Studer5c510ee2014-12-15 16:32:27 +01004051 }
4052
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004053 contentView.setImageViewBitmap(R.id.big_picture, mPicture);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004054 return contentView;
4055 }
4056
Daniel Sandlerf45564e2013-04-15 15:05:08 -04004057 /**
4058 * @hide
4059 */
4060 public void addExtras(Bundle extras) {
4061 super.addExtras(extras);
4062
4063 if (mBigLargeIconSet) {
4064 extras.putParcelable(EXTRA_LARGE_ICON_BIG, mBigLargeIcon);
4065 }
4066 extras.putParcelable(EXTRA_PICTURE, mPicture);
4067 }
4068
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04004069 /**
4070 * @hide
4071 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004072 @Override
Christoph Studer4600f9b2014-07-22 22:44:43 +02004073 protected void restoreFromExtras(Bundle extras) {
4074 super.restoreFromExtras(extras);
4075
4076 if (extras.containsKey(EXTRA_LARGE_ICON_BIG)) {
Christoph Studer5c510ee2014-12-15 16:32:27 +01004077 mBigLargeIconSet = true;
Christoph Studer4600f9b2014-07-22 22:44:43 +02004078 mBigLargeIcon = extras.getParcelable(EXTRA_LARGE_ICON_BIG);
Chris Wren3745a3d2012-05-22 15:11:52 -04004079 }
Christoph Studer4600f9b2014-07-22 22:44:43 +02004080 mPicture = extras.getParcelable(EXTRA_PICTURE);
4081 }
Selim Cinek03d0d652015-11-13 13:18:09 -05004082
4083 /**
4084 * @hide
4085 */
4086 @Override
4087 public boolean hasSummaryInHeader() {
4088 return false;
4089 }
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004090 }
4091
4092 /**
Daniel Sandler4dfbe832012-04-11 14:51:46 -04004093 * Helper class for generating large-format notifications that include a lot of text.
Joe Malin8d40d042012-11-05 11:36:40 -08004094 *
Robert Ly91c5ce32014-06-08 15:37:00 -07004095 * Here's how you'd set the <code>BigTextStyle</code> on a notification:
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004096 * <pre class="prettyprint">
Robert Ly91c5ce32014-06-08 15:37:00 -07004097 * Notification notif = new Notification.Builder(mContext)
4098 * .setContentTitle(&quot;New mail from &quot; + sender.toString())
4099 * .setContentText(subject)
4100 * .setSmallIcon(R.drawable.new_mail)
4101 * .setLargeIcon(aBitmap)
4102 * .setStyle(new Notification.BigTextStyle()
4103 * .bigText(aVeryLongString))
4104 * .build();
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004105 * </pre>
Joe Malin8d40d042012-11-05 11:36:40 -08004106 *
Daniel Sandler4dfbe832012-04-11 14:51:46 -04004107 * @see Notification#bigContentView
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004108 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004109 public static class BigTextStyle extends Style {
Jorim Jaggi457a10d2014-09-08 16:18:23 +02004110
4111 private static final int MAX_LINES = 13;
Selim Cinek3a2c4b92015-12-17 17:01:17 -08004112 private static final int LINES_CONSUMED_BY_ACTIONS = 4;
Jorim Jaggi457a10d2014-09-08 16:18:23 +02004113
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004114 private CharSequence mBigText;
4115
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004116 public BigTextStyle() {
4117 }
4118
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004119 public BigTextStyle(Builder builder) {
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004120 setBuilder(builder);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004121 }
4122
Chris Wrend6297db2012-05-03 16:20:13 -04004123 /**
4124 * Overrides ContentTitle in the big form of the template.
4125 * This defaults to the value passed to setContentTitle().
4126 */
4127 public BigTextStyle setBigContentTitle(CharSequence title) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04004128 internalSetBigContentTitle(safeCharSequence(title));
Chris Wrend6297db2012-05-03 16:20:13 -04004129 return this;
4130 }
4131
4132 /**
4133 * Set the first line of text after the detail section in the big form of the template.
4134 */
4135 public BigTextStyle setSummaryText(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04004136 internalSetSummaryText(safeCharSequence(cs));
Chris Wrend6297db2012-05-03 16:20:13 -04004137 return this;
4138 }
4139
Chris Wren0bd664d2012-08-01 13:56:56 -04004140 /**
4141 * Provide the longer text to be displayed in the big form of the
4142 * template in place of the content text.
4143 */
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004144 public BigTextStyle bigText(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04004145 mBigText = safeCharSequence(cs);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004146 return this;
4147 }
4148
Daniel Sandlerf45564e2013-04-15 15:05:08 -04004149 /**
4150 * @hide
4151 */
4152 public void addExtras(Bundle extras) {
4153 super.addExtras(extras);
4154
Christoph Studer4600f9b2014-07-22 22:44:43 +02004155 extras.putCharSequence(EXTRA_BIG_TEXT, mBigText);
4156 }
4157
4158 /**
4159 * @hide
4160 */
4161 @Override
4162 protected void restoreFromExtras(Bundle extras) {
4163 super.restoreFromExtras(extras);
4164
4165 mBigText = extras.getCharSequence(EXTRA_BIG_TEXT);
Daniel Sandlerf45564e2013-04-15 15:05:08 -04004166 }
4167
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004168 /**
4169 * @hide
4170 */
4171 public RemoteViews makeBigContentView() {
Christoph Studer4600f9b2014-07-22 22:44:43 +02004172
4173 // Nasty
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004174 CharSequence oldBuilderContentText =
4175 mBuilder.getAllExtras().getCharSequence(EXTRA_TEXT);
4176 mBuilder.getAllExtras().putCharSequence(EXTRA_TEXT, null);
Daniel Sandler916ad912012-06-13 12:17:07 -04004177
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01004178 RemoteViews contentView = getStandardView(mBuilder.getBigTextLayoutResource());
Joe Malin8d40d042012-11-05 11:36:40 -08004179
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004180 mBuilder.getAllExtras().putCharSequence(EXTRA_TEXT, oldBuilderContentText);
Christoph Studer4600f9b2014-07-22 22:44:43 +02004181
Selim Cinek3a2c4b92015-12-17 17:01:17 -08004182 CharSequence bigTextText = mBuilder.processLegacyText(mBigText);
4183 contentView.setTextViewText(R.id.big_text, bigTextText);
4184 contentView.setViewVisibility(R.id.big_text,
4185 TextUtils.isEmpty(bigTextText) ? View.GONE : View.VISIBLE);
Jorim Jaggi457a10d2014-09-08 16:18:23 +02004186 contentView.setInt(R.id.big_text, "setMaxLines", calculateMaxLines());
Selim Cinek4fb12d32015-11-19 18:10:48 -08004187 contentView.setBoolean(R.id.big_text, "setHasImage", mBuilder.mN.mLargeIcon != null);
4188
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004189 return contentView;
4190 }
4191
Jorim Jaggi457a10d2014-09-08 16:18:23 +02004192 private int calculateMaxLines() {
4193 int lineCount = MAX_LINES;
4194 boolean hasActions = mBuilder.mActions.size() > 0;
Jorim Jaggi457a10d2014-09-08 16:18:23 +02004195 if (hasActions) {
4196 lineCount -= LINES_CONSUMED_BY_ACTIONS;
4197 }
Jorim Jaggi457a10d2014-09-08 16:18:23 +02004198 return lineCount;
4199 }
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004200 }
Daniel Sandler879c5e02012-04-17 16:46:51 -04004201
4202 /**
Alex Hillsfc737de2016-03-23 17:33:02 -04004203 * Helper class for generating large-format notifications that include multiple back-and-forth
4204 * messages of varying types between any number of people.
4205 *
4206 * <br>
4207 * If the platform does not provide large-format notifications, this method has no effect. The
4208 * user will always see the normal notification view.
4209 * <br>
4210 * This class is a "rebuilder": It attaches to a Builder object and modifies its behavior, like
4211 * so:
4212 * <pre class="prettyprint">
4213 *
4214 * Notification noti = new Notification.Builder()
4215 * .setContentTitle(&quot;2 new messages wtih &quot; + sender.toString())
4216 * .setContentText(subject)
4217 * .setSmallIcon(R.drawable.new_message)
4218 * .setLargeIcon(aBitmap)
4219 * .setStyle(new Notification.MessagingStyle(resources.getString(R.string.reply_name))
4220 * .addMessage(messages[0].getText(), messages[0].getTime(), messages[0].getSender())
4221 * .addMessage(messages[1].getText(), messages[1].getTime(), messages[1].getSender()))
4222 * .build();
4223 * </pre>
4224 */
4225 public static class MessagingStyle extends Style {
4226
4227 /**
4228 * The maximum number of messages that will be retained in the Notification itself (the
4229 * number displayed is up to the platform).
4230 */
4231 public static final int MAXIMUM_RETAINED_MESSAGES = 25;
4232
4233 CharSequence mUserDisplayName;
4234 CharSequence mConversationTitle;
4235 boolean mAllowGeneratedReplies = true;
4236 ArrayList<Message> mMessages = new ArrayList<>();
4237
4238 MessagingStyle() {
4239 }
4240
4241 /**
4242 * @param userDisplayName the name to be displayed for any replies sent by the user before the
4243 * posting app reposts the notification with those messages after they've been actually
4244 * sent and in previous messages sent by the user added in
4245 * {@link #addMessage(Notification.MessagingStyle.Message)}
4246 */
4247 public MessagingStyle(CharSequence userDisplayName) {
4248 mUserDisplayName = userDisplayName;
4249 }
4250
4251 /**
4252 * Returns the name to be displayed for any replies sent by the user
4253 */
4254 public CharSequence getUserDisplayName() {
4255 return mUserDisplayName;
4256 }
4257
4258 /**
4259 * Set whether the platform should automatically generate possible replies from messages.
4260 * @param allowGeneratedReplies {@code true} to allow generated replies, {@code false}
4261 * otherwise
4262 * @return this object for method chaining
4263 * The default value is {@code true}
4264 */
4265 public MessagingStyle setAllowGeneratedReplies(boolean allowGeneratedReplies) {
4266 mAllowGeneratedReplies = allowGeneratedReplies;
4267 return this;
4268 }
4269
4270 /**
4271 * Return whether the platform should automatically generate possible replies from messages.
4272 */
4273 public boolean getAllowGeneratedReplies() {
4274 return mAllowGeneratedReplies;
4275 }
4276
4277 /**
4278 * Sets the title to be displayed on this conversation. This should only be used for
4279 * group messaging and left unset for one-on-one conversations.
4280 * @param conversationTitle
4281 * @return this object for method chaining.
4282 */
4283 public MessagingStyle setConversationTitle(CharSequence conversationTitle) {
4284 mConversationTitle = conversationTitle;
4285 return this;
4286 }
4287
4288 /**
4289 * Return the title to be displayed on this conversation. Can be <code>null</code> and
4290 * should be for one-on-one conversations
4291 */
4292 public CharSequence getConversationTitle() {
4293 return mConversationTitle;
4294 }
4295
4296 /**
4297 * Adds a message for display by this notification. Convenience call for a simple
4298 * {@link Message} in {@link #addMessage(Notification.MessagingStyle.Message)}.
4299 * @param text A {@link CharSequence} to be displayed as the message content
4300 * @param timestamp Time at which the message arrived
4301 * @param sender A {@link CharSequence} to be used for displaying the name of the
4302 * sender. Should be <code>null</code> for messages by the current user, in which case
4303 * the platform will insert {@link #getUserDisplayName()}.
4304 * Should be unique amongst all individuals in the conversation, and should be
4305 * consistent during re-posts of the notification.
4306 *
4307 * @see Message#Message(CharSequence, long, CharSequence)
4308 *
4309 * @return this object for method chaining
4310 */
4311 public MessagingStyle addMessage(CharSequence text, long timestamp, CharSequence sender) {
4312 mMessages.add(new Message(text, timestamp, sender));
4313 if (mMessages.size() > MAXIMUM_RETAINED_MESSAGES) {
4314 mMessages.remove(0);
4315 }
4316 return this;
4317 }
4318
4319 /**
4320 * Adds a {@link Message} for display in this notification.
4321 * @param message The {@link Message} to be displayed
4322 * @return this object for method chaining
4323 */
4324 public MessagingStyle addMessage(Message message) {
4325 mMessages.add(message);
4326 if (mMessages.size() > MAXIMUM_RETAINED_MESSAGES) {
4327 mMessages.remove(0);
4328 }
4329 return this;
4330 }
4331
4332 /**
4333 * Gets the list of {@code Message} objects that represent the notification
4334 */
4335 public List<Message> getMessages() {
4336 return mMessages;
4337 }
4338
4339 /**
4340 * @hide
4341 */
4342 @Override
4343 public void addExtras(Bundle extras) {
4344 super.addExtras(extras);
4345 if (mUserDisplayName != null) {
4346 extras.putCharSequence(EXTRA_SELF_DISPLAY_NAME, mUserDisplayName);
4347 }
4348 if (mConversationTitle != null) {
4349 extras.putCharSequence(EXTRA_THREAD_TITLE, mConversationTitle);
4350 }
4351 extras.putBoolean(EXTRA_ALLOW_GENERATED_REPLIES, mAllowGeneratedReplies);
4352 if (!mMessages.isEmpty()) {
4353 extras.putParcelableArrayList(EXTRA_MESSAGES, mMessages);
4354 }
4355 }
4356
4357 /**
4358 * @hide
4359 */
4360 @Override
4361 protected void restoreFromExtras(Bundle extras) {
4362 super.restoreFromExtras(extras);
4363
4364 mMessages.clear();
4365 mUserDisplayName = extras.getString(EXTRA_SELF_DISPLAY_NAME);
4366 mConversationTitle = extras.getString(EXTRA_THREAD_TITLE);
4367 mAllowGeneratedReplies = extras.getBoolean(EXTRA_ALLOW_GENERATED_REPLIES,
4368 mAllowGeneratedReplies);
4369 List<Message> messages = extras.getParcelableArrayList(EXTRA_MESSAGES);
4370 if (messages != null) {
4371 mMessages.addAll(messages);
4372 }
4373 }
4374
4375 /**
4376 * @hide
4377 */
4378 public RemoteViews makeBigContentView() {
4379 // TODO handset to write implementation
4380 RemoteViews contentView = getStandardView(mBuilder.getBigTextLayoutResource());
4381
4382 return contentView;
4383 }
4384
4385 public static final class Message implements Parcelable {
4386
4387 private final CharSequence mText;
4388 private final long mTimestamp;
4389 private final CharSequence mSender;
4390
4391 private String mDataMimeType;
4392 private Uri mDataUri;
4393
4394 /**
4395 * Constructor
4396 * @param text A {@link CharSequence} to be displayed as the message content
4397 * @param timestamp Time at which the message arrived
4398 * @param sender A {@link CharSequence} to be used for displaying the name of the
4399 * sender. Should be <code>null</code> for messages by the current user, in which case
4400 * the platform will insert {@link MessagingStyle#getUserDisplayName()}.
4401 * Should be unique amongst all individuals in the conversation, and should be
4402 * consistent during re-posts of the notification.
4403 */
4404 public Message(CharSequence text, long timestamp, CharSequence sender){
4405 mText = text;
4406 mTimestamp = timestamp;
4407 mSender = sender;
4408 }
4409
4410 /**
4411 * Sets a binary blob of data and an associated MIME type for a message. In the case
4412 * where the platform doesn't support the MIME type, the original text provided in the
4413 * constructor will be used.
4414 * @param dataMimeType The MIME type of the content. See
4415 * <a href="{@docRoot}notifications/messaging.html"> for the list of supported MIME
4416 * types on Android and Android Wear.
4417 * @param dataUri The uri containing the content whose type is given by the MIME type.
4418 * <p class="note">
4419 * <ol>
4420 * <li>Notification Listeners including the System UI need permission to access the
4421 * data the Uri points to. The recommended ways to do this are:</li>
4422 * <li>Store the data in your own ContentProvider, making sure that other apps have
4423 * the correct permission to access your provider. The preferred mechanism for
4424 * providing access is to use per-URI permissions which are temporary and only
4425 * grant access to the receiving application. An easy way to create a
4426 * ContentProvider like this is to use the FileProvider helper class.</li>
4427 * <li>Use the system MediaStore. The MediaStore is primarily aimed at video, audio
4428 * and image MIME types, however beginning with Android 3.0 (API level 11) it can
4429 * also store non-media types (see MediaStore.Files for more info). Files can be
4430 * inserted into the MediaStore using scanFile() after which a content:// style
4431 * Uri suitable for sharing is passed to the provided onScanCompleted() callback.
4432 * Note that once added to the system MediaStore the content is accessible to any
4433 * app on the device.</li>
4434 * </ol>
4435 * @return this object for method chaining
4436 */
4437 public Message setData(String dataMimeType, Uri dataUri) {
4438 mDataMimeType = dataMimeType;
4439 mDataUri = dataUri;
4440 return this;
4441 }
4442
4443 private Message(Parcel in) {
4444 if (in.readInt() != 0) {
4445 mText = TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(in);
4446 } else {
4447 mText = null;
4448 }
4449 mTimestamp = in.readLong();
4450 if (in.readInt() != 0) {
4451 mSender = TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(in);
4452 } else {
4453 mSender = null;
4454 }
4455 if (in.readInt() != 0) {
4456 mDataMimeType = in.readString();
4457 }
4458 if (in.readInt() != 0) {
4459 mDataUri = in.readParcelable(Uri.class.getClassLoader());
4460 }
4461 }
4462
4463 /**
4464 * Get the text to be used for this message, or the fallback text if a type and content
4465 * Uri have been set
4466 */
4467 public CharSequence getText() {
4468 return mText;
4469 }
4470
4471 /**
4472 * Get the time at which this message arrived
4473 */
4474 public long getTimestamp() {
4475 return mTimestamp;
4476 }
4477
4478 /**
4479 * Get the text used to display the contact's name in the messaging experience
4480 */
4481 public CharSequence getSender() {
4482 return mSender;
4483 }
4484
4485 /**
4486 * Get the MIME type of the data pointed to by the Uri
4487 */
4488 public String getDataMimeType() {
4489 return mDataMimeType;
4490 }
4491
4492 /**
4493 * Get the the Uri pointing to the content of the message. Can be null, in which case
4494 * {@see #getText()} is used.
4495 */
4496 public Uri getDataUri() {
4497 return mDataUri;
4498 }
4499
4500 @Override
4501 public int describeContents() {
4502 return 0;
4503 }
4504
4505 @Override
4506 public void writeToParcel(Parcel out, int flags) {
4507 if (mText != null) {
4508 out.writeInt(1);
4509 TextUtils.writeToParcel(mText, out, flags);
4510 } else {
4511 out.writeInt(0);
4512 }
4513 out.writeLong(mTimestamp);
4514 if (mSender != null) {
4515 out.writeInt(1);
4516 TextUtils.writeToParcel(mSender, out, flags);
4517 } else {
4518 out.writeInt(0);
4519 }
4520 if (mDataMimeType != null) {
4521 out.writeInt(1);
4522 out.writeString(mDataMimeType);
4523 } else {
4524 out.writeInt(0);
4525 }
4526 if (mDataUri != null) {
4527 out.writeInt(1);
4528 out.writeParcelable(mDataUri, flags);
4529 } else {
4530 out.writeInt(0);
4531 }
4532 }
4533
4534 public static final Parcelable.Creator<Message> CREATOR =
4535 new Parcelable.Creator<Message>() {
4536 public Message createFromParcel(Parcel in) {
4537 return new Message(in);
4538 }
4539 public Message[] newArray(int size) {
4540 return new Message[size];
4541 }
4542 };
4543 }
4544 }
4545
4546 /**
Daniel Sandler879c5e02012-04-17 16:46:51 -04004547 * Helper class for generating large-format notifications that include a list of (up to 5) strings.
Joe Malin8d40d042012-11-05 11:36:40 -08004548 *
Robert Ly91c5ce32014-06-08 15:37:00 -07004549 * Here's how you'd set the <code>InboxStyle</code> on a notification:
Daniel Sandler879c5e02012-04-17 16:46:51 -04004550 * <pre class="prettyprint">
Robert Ly91c5ce32014-06-08 15:37:00 -07004551 * Notification notif = new Notification.Builder(mContext)
4552 * .setContentTitle(&quot;5 New mails from &quot; + sender.toString())
4553 * .setContentText(subject)
4554 * .setSmallIcon(R.drawable.new_mail)
4555 * .setLargeIcon(aBitmap)
4556 * .setStyle(new Notification.InboxStyle()
4557 * .addLine(str1)
4558 * .addLine(str2)
4559 * .setContentTitle(&quot;&quot;)
4560 * .setSummaryText(&quot;+3 more&quot;))
4561 * .build();
Daniel Sandler879c5e02012-04-17 16:46:51 -04004562 * </pre>
Joe Malin8d40d042012-11-05 11:36:40 -08004563 *
Daniel Sandler879c5e02012-04-17 16:46:51 -04004564 * @see Notification#bigContentView
4565 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004566 public static class InboxStyle extends Style {
Daniel Sandler879c5e02012-04-17 16:46:51 -04004567 private ArrayList<CharSequence> mTexts = new ArrayList<CharSequence>(5);
4568
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004569 public InboxStyle() {
4570 }
4571
Daniel Sandler879c5e02012-04-17 16:46:51 -04004572 public InboxStyle(Builder builder) {
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004573 setBuilder(builder);
Daniel Sandler879c5e02012-04-17 16:46:51 -04004574 }
4575
Chris Wrend6297db2012-05-03 16:20:13 -04004576 /**
4577 * Overrides ContentTitle in the big form of the template.
4578 * This defaults to the value passed to setContentTitle().
4579 */
4580 public InboxStyle setBigContentTitle(CharSequence title) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04004581 internalSetBigContentTitle(safeCharSequence(title));
Chris Wrend6297db2012-05-03 16:20:13 -04004582 return this;
4583 }
4584
4585 /**
4586 * Set the first line of text after the detail section in the big form of the template.
4587 */
4588 public InboxStyle setSummaryText(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04004589 internalSetSummaryText(safeCharSequence(cs));
Chris Wrend6297db2012-05-03 16:20:13 -04004590 return this;
4591 }
4592
Chris Wren0bd664d2012-08-01 13:56:56 -04004593 /**
4594 * Append a line to the digest section of the Inbox notification.
4595 */
Daniel Sandler879c5e02012-04-17 16:46:51 -04004596 public InboxStyle addLine(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04004597 mTexts.add(safeCharSequence(cs));
Daniel Sandler879c5e02012-04-17 16:46:51 -04004598 return this;
4599 }
4600
Daniel Sandlerf45564e2013-04-15 15:05:08 -04004601 /**
4602 * @hide
4603 */
4604 public void addExtras(Bundle extras) {
4605 super.addExtras(extras);
Christoph Studer4600f9b2014-07-22 22:44:43 +02004606
Daniel Sandlerf45564e2013-04-15 15:05:08 -04004607 CharSequence[] a = new CharSequence[mTexts.size()];
4608 extras.putCharSequenceArray(EXTRA_TEXT_LINES, mTexts.toArray(a));
4609 }
4610
Christoph Studer4600f9b2014-07-22 22:44:43 +02004611 /**
4612 * @hide
4613 */
4614 @Override
4615 protected void restoreFromExtras(Bundle extras) {
4616 super.restoreFromExtras(extras);
4617
4618 mTexts.clear();
4619 if (extras.containsKey(EXTRA_TEXT_LINES)) {
4620 Collections.addAll(mTexts, extras.getCharSequenceArray(EXTRA_TEXT_LINES));
4621 }
4622 }
4623
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004624 /**
4625 * @hide
4626 */
4627 public RemoteViews makeBigContentView() {
Selim Cinekc848c3a2016-01-13 15:27:30 -08004628 // Remove the content text so it disappears unless you have a summary
Christoph Studer4600f9b2014-07-22 22:44:43 +02004629 // Nasty
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004630 CharSequence oldBuilderContentText = mBuilder.mN.extras.getCharSequence(EXTRA_TEXT);
4631 mBuilder.getAllExtras().putCharSequence(EXTRA_TEXT, null);
Christoph Studer4600f9b2014-07-22 22:44:43 +02004632
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01004633 RemoteViews contentView = getStandardView(mBuilder.getInboxLayoutResource());
Daniel Sandler619738c2012-06-07 16:33:08 -04004634
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004635 mBuilder.getAllExtras().putCharSequence(EXTRA_TEXT, oldBuilderContentText);
Christoph Studer4600f9b2014-07-22 22:44:43 +02004636
Chris Wrend6297db2012-05-03 16:20:13 -04004637 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 -04004638 R.id.inbox_text4, R.id.inbox_text5, R.id.inbox_text6};
Chris Wrend6297db2012-05-03 16:20:13 -04004639
Chris Wren4ed80d52012-05-17 09:30:03 -04004640 // Make sure all rows are gone in case we reuse a view.
4641 for (int rowId : rowIds) {
4642 contentView.setViewVisibility(rowId, View.GONE);
4643 }
4644
Jorim Jaggi445d3c02014-08-19 22:33:42 +02004645 final boolean largeText =
4646 mBuilder.mContext.getResources().getConfiguration().fontScale > 1f;
4647 final float subTextSize = mBuilder.mContext.getResources().getDimensionPixelSize(
4648 R.dimen.notification_subtext_size);
Daniel Sandler879c5e02012-04-17 16:46:51 -04004649 int i=0;
Selim Cinek9d9fc6e2015-11-12 15:49:14 -05004650 final float density = mBuilder.mContext.getResources().getDisplayMetrics().density;
4651 int topPadding = (int) (5 * density);
Selim Cinekc0fac722016-03-07 19:57:06 -08004652 int bottomPadding = mBuilder.mContext.getResources().getDimensionPixelSize(
4653 com.android.internal.R.dimen.notification_content_margin_bottom);
Selim Cinek247fa012016-02-18 09:50:48 -08004654 boolean first = true;
Daniel Sandler879c5e02012-04-17 16:46:51 -04004655 while (i < mTexts.size() && i < rowIds.length) {
4656 CharSequence str = mTexts.get(i);
4657 if (str != null && !str.equals("")) {
4658 contentView.setViewVisibility(rowIds[i], View.VISIBLE);
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01004659 contentView.setTextViewText(rowIds[i], mBuilder.processLegacyText(str));
Jorim Jaggi445d3c02014-08-19 22:33:42 +02004660 if (largeText) {
4661 contentView.setTextViewTextSize(rowIds[i], TypedValue.COMPLEX_UNIT_PX,
4662 subTextSize);
4663 }
Selim Cinek9d9fc6e2015-11-12 15:49:14 -05004664 contentView.setViewPadding(rowIds[i], 0, topPadding, 0,
4665 i == rowIds.length - 1 || i == mTexts.size() - 1 ? bottomPadding : 0);
Selim Cinek247fa012016-02-18 09:50:48 -08004666 handleInboxImageMargin(contentView, rowIds[i], first);
4667 first = false;
Daniel Sandler879c5e02012-04-17 16:46:51 -04004668 }
4669 i++;
4670 }
Kenny Guy98193ea2014-07-24 19:54:37 +01004671
Selim Cinek1e0bf612015-11-20 15:57:26 -08004672
Daniel Sandler879c5e02012-04-17 16:46:51 -04004673 return contentView;
4674 }
Selim Cinek1e0bf612015-11-20 15:57:26 -08004675
Selim Cinek247fa012016-02-18 09:50:48 -08004676 private void handleInboxImageMargin(RemoteViews contentView, int id, boolean first) {
Selim Cinek1e0bf612015-11-20 15:57:26 -08004677 int endMargin = 0;
Selim Cinek247fa012016-02-18 09:50:48 -08004678 if (first) {
4679 final int max = mBuilder.mN.extras.getInt(EXTRA_PROGRESS_MAX, 0);
4680 final boolean ind = mBuilder.mN.extras.getBoolean(EXTRA_PROGRESS_INDETERMINATE);
4681 boolean hasProgress = max != 0 || ind;
4682 if (mBuilder.mN.mLargeIcon != null && !hasProgress) {
4683 endMargin = mBuilder.mContext.getResources().getDimensionPixelSize(
4684 R.dimen.notification_content_picture_margin);
4685 }
Selim Cinek1e0bf612015-11-20 15:57:26 -08004686 }
4687 contentView.setViewLayoutMarginEnd(id, endMargin);
4688 }
Daniel Sandler879c5e02012-04-17 16:46:51 -04004689 }
Dan Sandler842dd772014-05-15 09:36:47 -04004690
4691 /**
4692 * Notification style for media playback notifications.
4693 *
4694 * In the expanded form, {@link Notification#bigContentView}, up to 5
4695 * {@link Notification.Action}s specified with
Dan Sandler86647982015-05-13 23:41:13 -04004696 * {@link Notification.Builder#addAction(Action) addAction} will be
Dan Sandler842dd772014-05-15 09:36:47 -04004697 * shown as icon-only pushbuttons, suitable for transport controls. The Bitmap given to
4698 * {@link Notification.Builder#setLargeIcon(android.graphics.Bitmap) setLargeIcon()} will be
4699 * treated as album artwork.
4700 *
4701 * Unlike the other styles provided here, MediaStyle can also modify the standard-size
4702 * {@link Notification#contentView}; by providing action indices to
Christoph Studerfde6f4d2014-12-12 13:23:26 +01004703 * {@link #setShowActionsInCompactView(int...)} you can promote up to 3 actions to be displayed
Dan Sandler842dd772014-05-15 09:36:47 -04004704 * in the standard view alongside the usual content.
4705 *
Bryan Mawhinney6be8de32014-07-18 10:35:12 +01004706 * Notifications created with MediaStyle will have their category set to
4707 * {@link Notification#CATEGORY_TRANSPORT CATEGORY_TRANSPORT} unless you set a different
4708 * category using {@link Notification.Builder#setCategory(String) setCategory()}.
4709 *
Jeff Browndba34ba2014-06-24 20:46:03 -07004710 * Finally, if you attach a {@link android.media.session.MediaSession.Token} using
4711 * {@link android.app.Notification.MediaStyle#setMediaSession(MediaSession.Token)},
Dan Sandler842dd772014-05-15 09:36:47 -04004712 * the System UI can identify this as a notification representing an active media session
4713 * and respond accordingly (by showing album artwork in the lockscreen, for example).
4714 *
4715 * To use this style with your Notification, feed it to
4716 * {@link Notification.Builder#setStyle(android.app.Notification.Style)} like so:
4717 * <pre class="prettyprint">
4718 * Notification noti = new Notification.Builder()
4719 * .setSmallIcon(R.drawable.ic_stat_player)
Christoph Studere935fe92014-11-24 14:18:06 +01004720 * .setContentTitle(&quot;Track title&quot;)
4721 * .setContentText(&quot;Artist - Album&quot;)
4722 * .setLargeIcon(albumArtBitmap))
Bryan Mawhinney6be8de32014-07-18 10:35:12 +01004723 * .setStyle(<b>new Notification.MediaStyle()</b>
4724 * .setMediaSession(mySession))
Dan Sandler842dd772014-05-15 09:36:47 -04004725 * .build();
4726 * </pre>
4727 *
4728 * @see Notification#bigContentView
4729 */
4730 public static class MediaStyle extends Style {
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02004731 static final int MAX_MEDIA_BUTTONS_IN_COMPACT = 3;
Dan Sandler842dd772014-05-15 09:36:47 -04004732 static final int MAX_MEDIA_BUTTONS = 5;
4733
4734 private int[] mActionsToShowInCompact = null;
Jeff Browndba34ba2014-06-24 20:46:03 -07004735 private MediaSession.Token mToken;
Dan Sandler842dd772014-05-15 09:36:47 -04004736
4737 public MediaStyle() {
4738 }
4739
4740 public MediaStyle(Builder builder) {
4741 setBuilder(builder);
4742 }
4743
4744 /**
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02004745 * 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 -04004746 * notification view.
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02004747 *
4748 * @param actions the indices of the actions to show in the compact notification view
Dan Sandler842dd772014-05-15 09:36:47 -04004749 */
4750 public MediaStyle setShowActionsInCompactView(int...actions) {
4751 mActionsToShowInCompact = actions;
4752 return this;
4753 }
4754
4755 /**
Jeff Browndba34ba2014-06-24 20:46:03 -07004756 * Attach a {@link android.media.session.MediaSession.Token} to this Notification
4757 * to provide additional playback information and control to the SystemUI.
Dan Sandler842dd772014-05-15 09:36:47 -04004758 */
Jeff Browndba34ba2014-06-24 20:46:03 -07004759 public MediaStyle setMediaSession(MediaSession.Token token) {
Dan Sandler842dd772014-05-15 09:36:47 -04004760 mToken = token;
4761 return this;
4762 }
4763
Christoph Studer4600f9b2014-07-22 22:44:43 +02004764 /**
4765 * @hide
4766 */
Dan Sandler842dd772014-05-15 09:36:47 -04004767 @Override
4768 public Notification buildStyled(Notification wip) {
Christoph Studer4600f9b2014-07-22 22:44:43 +02004769 super.buildStyled(wip);
Bryan Mawhinney6be8de32014-07-18 10:35:12 +01004770 if (wip.category == null) {
4771 wip.category = Notification.CATEGORY_TRANSPORT;
4772 }
Dan Sandler842dd772014-05-15 09:36:47 -04004773 return wip;
4774 }
4775
Christoph Studer4600f9b2014-07-22 22:44:43 +02004776 /**
4777 * @hide
4778 */
4779 @Override
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004780 public RemoteViews makeContentView() {
4781 return makeMediaContentView();
Christoph Studer4600f9b2014-07-22 22:44:43 +02004782 }
4783
4784 /**
4785 * @hide
4786 */
4787 @Override
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004788 public RemoteViews makeBigContentView() {
4789 return makeMediaBigContentView();
Christoph Studer4600f9b2014-07-22 22:44:43 +02004790 }
4791
Selim Cinekcc10bfb2016-02-10 16:24:21 -08004792 /**
4793 * @hide
4794 */
4795 @Override
4796 public RemoteViews makeHeadsUpContentView() {
4797 RemoteViews expanded = makeMediaBigContentView();
4798 return expanded != null ? expanded : makeMediaContentView();
4799 }
4800
Dan Sandler842dd772014-05-15 09:36:47 -04004801 /** @hide */
4802 @Override
4803 public void addExtras(Bundle extras) {
4804 super.addExtras(extras);
4805
4806 if (mToken != null) {
4807 extras.putParcelable(EXTRA_MEDIA_SESSION, mToken);
4808 }
Bryan Mawhinneye191f902014-07-22 12:50:09 +01004809 if (mActionsToShowInCompact != null) {
4810 extras.putIntArray(EXTRA_COMPACT_ACTIONS, mActionsToShowInCompact);
4811 }
Dan Sandler842dd772014-05-15 09:36:47 -04004812 }
4813
Christoph Studer4600f9b2014-07-22 22:44:43 +02004814 /**
4815 * @hide
4816 */
4817 @Override
4818 protected void restoreFromExtras(Bundle extras) {
4819 super.restoreFromExtras(extras);
4820
4821 if (extras.containsKey(EXTRA_MEDIA_SESSION)) {
4822 mToken = extras.getParcelable(EXTRA_MEDIA_SESSION);
4823 }
4824 if (extras.containsKey(EXTRA_COMPACT_ACTIONS)) {
4825 mActionsToShowInCompact = extras.getIntArray(EXTRA_COMPACT_ACTIONS);
4826 }
4827 }
4828
Selim Cinek5bf069a2015-11-10 19:14:27 -05004829 private RemoteViews generateMediaActionButton(Action action, int color) {
Dan Sandler842dd772014-05-15 09:36:47 -04004830 final boolean tombstone = (action.actionIntent == null);
Selim Cinekf33b1112015-07-15 17:45:11 -07004831 RemoteViews button = new BuilderRemoteViews(mBuilder.mContext.getApplicationInfo(),
Alan Viverette3cb07a462014-06-06 14:19:53 -07004832 R.layout.notification_material_media_action);
Dan Sandler68079d52015-07-22 10:45:30 -04004833 button.setImageViewIcon(R.id.action0, action.getIcon());
Selim Cinek5bf069a2015-11-10 19:14:27 -05004834 button.setDrawableParameters(R.id.action0, false, -1, color, PorterDuff.Mode.SRC_ATOP,
4835 -1);
Dan Sandler842dd772014-05-15 09:36:47 -04004836 if (!tombstone) {
4837 button.setOnClickPendingIntent(R.id.action0, action.actionIntent);
4838 }
4839 button.setContentDescription(R.id.action0, action.title);
4840 return button;
4841 }
4842
4843 private RemoteViews makeMediaContentView() {
4844 RemoteViews view = mBuilder.applyStandardTemplate(
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02004845 R.layout.notification_template_material_media, false /* hasProgress */);
Dan Sandler842dd772014-05-15 09:36:47 -04004846
4847 final int numActions = mBuilder.mActions.size();
4848 final int N = mActionsToShowInCompact == null
4849 ? 0
4850 : Math.min(mActionsToShowInCompact.length, MAX_MEDIA_BUTTONS_IN_COMPACT);
4851 if (N > 0) {
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02004852 view.removeAllViews(com.android.internal.R.id.media_actions);
Dan Sandler842dd772014-05-15 09:36:47 -04004853 for (int i = 0; i < N; i++) {
4854 if (i >= numActions) {
4855 throw new IllegalArgumentException(String.format(
4856 "setShowActionsInCompactView: action %d out of bounds (max %d)",
4857 i, numActions - 1));
4858 }
4859
4860 final Action action = mBuilder.mActions.get(mActionsToShowInCompact[i]);
Selim Cinek5bf069a2015-11-10 19:14:27 -05004861 final RemoteViews button = generateMediaActionButton(action,
Adrian Roos4ff3b122016-02-01 12:26:13 -08004862 mBuilder.resolveContrastColor());
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02004863 view.addView(com.android.internal.R.id.media_actions, button);
Dan Sandler842dd772014-05-15 09:36:47 -04004864 }
4865 }
Selim Cinekfdc738f2016-01-27 20:04:27 -08004866 handleImage(view);
4867 // handle the content margin
Selim Cinek247fa012016-02-18 09:50:48 -08004868 int endMargin = mBuilder.mContext.getResources().getDimensionPixelSize(
4869 R.dimen.notification_content_margin_end);;
Selim Cinekfdc738f2016-01-27 20:04:27 -08004870 if (mBuilder.mN.mLargeIcon != null) {
Selim Cinek247fa012016-02-18 09:50:48 -08004871 endMargin += mBuilder.mContext.getResources().getDimensionPixelSize(
4872 R.dimen.notification_content_picture_margin);
Selim Cinekfdc738f2016-01-27 20:04:27 -08004873 }
4874 view.setViewLayoutMarginEnd(R.id.notification_main_column, endMargin);
Dan Sandler842dd772014-05-15 09:36:47 -04004875 return view;
4876 }
4877
4878 private RemoteViews makeMediaBigContentView() {
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02004879 final int actionCount = Math.min(mBuilder.mActions.size(), MAX_MEDIA_BUTTONS);
Selim Cinekcc10bfb2016-02-10 16:24:21 -08004880 // Dont add an expanded view if there is no more content to be revealed
4881 int actionsInCompact = mActionsToShowInCompact == null
4882 ? 0
4883 : Math.min(mActionsToShowInCompact.length, MAX_MEDIA_BUTTONS_IN_COMPACT);
4884 if (mBuilder.mN.mLargeIcon == null && actionCount <= actionsInCompact) {
4885 return null;
4886 }
Selim Cinek5bf069a2015-11-10 19:14:27 -05004887 RemoteViews big = mBuilder.applyStandardTemplate(
4888 R.layout.notification_template_material_big_media,
4889 false);
Dan Sandler842dd772014-05-15 09:36:47 -04004890
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02004891 if (actionCount > 0) {
4892 big.removeAllViews(com.android.internal.R.id.media_actions);
4893 for (int i = 0; i < actionCount; i++) {
Selim Cinek5bf069a2015-11-10 19:14:27 -05004894 final RemoteViews button = generateMediaActionButton(mBuilder.mActions.get(i),
Adrian Roos4ff3b122016-02-01 12:26:13 -08004895 mBuilder.resolveContrastColor());
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02004896 big.addView(com.android.internal.R.id.media_actions, button);
Dan Sandler842dd772014-05-15 09:36:47 -04004897 }
4898 }
Selim Cinek5bf069a2015-11-10 19:14:27 -05004899 handleImage(big);
Dan Sandler842dd772014-05-15 09:36:47 -04004900 return big;
4901 }
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02004902
Selim Cinek5bf069a2015-11-10 19:14:27 -05004903 private void handleImage(RemoteViews contentView) {
4904 if (mBuilder.mN.mLargeIcon != null) {
4905 contentView.setViewLayoutMarginEnd(R.id.line1, 0);
Selim Cinekc848c3a2016-01-13 15:27:30 -08004906 contentView.setViewLayoutMarginEnd(R.id.text, 0);
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02004907 }
4908 }
4909
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02004910 /**
4911 * @hide
4912 */
4913 @Override
4914 protected boolean hasProgress() {
4915 return false;
4916 }
Dan Sandler842dd772014-05-15 09:36:47 -04004917 }
Griff Hazen61a9e862014-05-22 16:05:19 -07004918
Selim Cinek593610c2016-02-16 18:42:57 -08004919 /**
4920 * Notification style for custom views that are decorated by the system
4921 *
4922 * <p>Instead of providing a notification that is completely custom, a developer can set this
4923 * style and still obtain system decorations like the notification header with the expand
4924 * affordance and actions.
4925 *
4926 * <p>Use {@link android.app.Notification.Builder#setCustomContentView(RemoteViews)},
4927 * {@link android.app.Notification.Builder#setCustomBigContentView(RemoteViews)} and
4928 * {@link android.app.Notification.Builder#setCustomHeadsUpContentView(RemoteViews)} to set the
4929 * corresponding custom views to display.
4930 *
4931 * To use this style with your Notification, feed it to
4932 * {@link Notification.Builder#setStyle(android.app.Notification.Style)} like so:
4933 * <pre class="prettyprint">
4934 * Notification noti = new Notification.Builder()
4935 * .setSmallIcon(R.drawable.ic_stat_player)
4936 * .setLargeIcon(albumArtBitmap))
4937 * .setCustomContentView(contentView);
4938 * .setStyle(<b>new Notification.DecoratedCustomViewStyle()</b>)
4939 * .build();
4940 * </pre>
4941 */
4942 public static class DecoratedCustomViewStyle extends Style {
4943
4944 public DecoratedCustomViewStyle() {
4945 }
4946
4947 public DecoratedCustomViewStyle(Builder builder) {
4948 setBuilder(builder);
4949 }
4950
4951 /**
4952 * @hide
4953 */
4954 public boolean displayCustomViewInline() {
4955 return true;
4956 }
4957
4958 /**
4959 * @hide
4960 */
4961 @Override
4962 public RemoteViews makeContentView() {
4963 return makeStandardTemplateWithCustomContent(mBuilder.mN.contentView);
4964 }
4965
4966 /**
4967 * @hide
4968 */
4969 @Override
4970 public RemoteViews makeBigContentView() {
4971 return makeDecoratedBigContentView();
4972 }
4973
4974 /**
4975 * @hide
4976 */
4977 @Override
4978 public RemoteViews makeHeadsUpContentView() {
4979 return makeDecoratedHeadsUpContentView();
4980 }
4981
Selim Cinek593610c2016-02-16 18:42:57 -08004982 private RemoteViews makeDecoratedHeadsUpContentView() {
4983 RemoteViews headsUpContentView = mBuilder.mN.headsUpContentView == null
4984 ? mBuilder.mN.contentView
4985 : mBuilder.mN.headsUpContentView;
4986 if (mBuilder.mActions.size() == 0) {
4987 return makeStandardTemplateWithCustomContent(headsUpContentView);
4988 }
4989 RemoteViews remoteViews = mBuilder.applyStandardTemplateWithActions(
4990 mBuilder.getBigBaseLayoutResource());
Selim Cinek247fa012016-02-18 09:50:48 -08004991 buildIntoRemoteViewContent(remoteViews, headsUpContentView);
Selim Cinek593610c2016-02-16 18:42:57 -08004992 return remoteViews;
4993 }
4994
Selim Cinek593610c2016-02-16 18:42:57 -08004995 private RemoteViews makeStandardTemplateWithCustomContent(RemoteViews customContent) {
4996 RemoteViews remoteViews = mBuilder.applyStandardTemplate(
4997 mBuilder.getBaseLayoutResource());
Selim Cinek247fa012016-02-18 09:50:48 -08004998 buildIntoRemoteViewContent(remoteViews, customContent);
Selim Cinek593610c2016-02-16 18:42:57 -08004999 return remoteViews;
5000 }
5001
Selim Cinek593610c2016-02-16 18:42:57 -08005002 private RemoteViews makeDecoratedBigContentView() {
5003 RemoteViews bigContentView = mBuilder.mN.bigContentView == null
5004 ? mBuilder.mN.contentView
5005 : mBuilder.mN.bigContentView;
5006 if (mBuilder.mActions.size() == 0) {
5007 return makeStandardTemplateWithCustomContent(bigContentView);
5008 }
5009 RemoteViews remoteViews = mBuilder.applyStandardTemplateWithActions(
5010 mBuilder.getBigBaseLayoutResource());
Selim Cinek247fa012016-02-18 09:50:48 -08005011 buildIntoRemoteViewContent(remoteViews, bigContentView);
Selim Cinek593610c2016-02-16 18:42:57 -08005012 return remoteViews;
5013 }
Selim Cinek247fa012016-02-18 09:50:48 -08005014
5015 private void buildIntoRemoteViewContent(RemoteViews remoteViews,
5016 RemoteViews customContent) {
Adrian Roos5081c0d2016-02-26 16:04:19 -08005017 if (customContent != null) {
Selim Cinekf91017e2016-03-14 12:25:09 -07005018 // Need to clone customContent before adding, because otherwise it can no longer be
5019 // parceled independently of remoteViews.
Adrian Roos5081c0d2016-02-26 16:04:19 -08005020 customContent = customContent.clone();
Selim Cinekf91017e2016-03-14 12:25:09 -07005021 remoteViews.removeAllViews(R.id.notification_main_column);
5022 remoteViews.addView(R.id.notification_main_column, customContent);
Adrian Roos5081c0d2016-02-26 16:04:19 -08005023 }
Selim Cinek247fa012016-02-18 09:50:48 -08005024 // also update the end margin if there is an image
5025 int endMargin = mBuilder.mContext.getResources().getDimensionPixelSize(
5026 R.dimen.notification_content_margin_end);
5027 if (mBuilder.mN.mLargeIcon != null) {
5028 endMargin += mBuilder.mContext.getResources().getDimensionPixelSize(
5029 R.dimen.notification_content_picture_margin);
5030 }
5031 remoteViews.setViewLayoutMarginEnd(R.id.notification_main_column, endMargin);
5032 }
Selim Cinek593610c2016-02-16 18:42:57 -08005033 }
5034
Selim Cinek03eb3b72016-02-18 10:39:45 -08005035 /**
5036 * Notification style for media custom views that are decorated by the system
5037 *
5038 * <p>Instead of providing a media notification that is completely custom, a developer can set
5039 * this style and still obtain system decorations like the notification header with the expand
5040 * affordance and actions.
5041 *
5042 * <p>Use {@link android.app.Notification.Builder#setCustomContentView(RemoteViews)},
5043 * {@link android.app.Notification.Builder#setCustomBigContentView(RemoteViews)} and
5044 * {@link android.app.Notification.Builder#setCustomHeadsUpContentView(RemoteViews)} to set the
5045 * corresponding custom views to display.
5046 *
5047 * To use this style with your Notification, feed it to
5048 * {@link Notification.Builder#setStyle(android.app.Notification.Style)} like so:
5049 * <pre class="prettyprint">
5050 * Notification noti = new Notification.Builder()
5051 * .setSmallIcon(R.drawable.ic_stat_player)
5052 * .setLargeIcon(albumArtBitmap))
5053 * .setCustomContentView(contentView);
5054 * .setStyle(<b>new Notification.DecoratedMediaCustomViewStyle()</b>
5055 * .setMediaSession(mySession))
5056 * .build();
5057 * </pre>
5058 *
5059 * @see android.app.Notification.DecoratedCustomViewStyle
5060 * @see android.app.Notification.MediaStyle
5061 */
5062 public static class DecoratedMediaCustomViewStyle extends MediaStyle {
5063
5064 public DecoratedMediaCustomViewStyle() {
5065 }
5066
5067 public DecoratedMediaCustomViewStyle(Builder builder) {
5068 setBuilder(builder);
5069 }
5070
5071 /**
5072 * @hide
5073 */
5074 public boolean displayCustomViewInline() {
5075 return true;
5076 }
5077
5078 /**
5079 * @hide
5080 */
5081 @Override
5082 public RemoteViews makeContentView() {
5083 RemoteViews remoteViews = super.makeContentView();
5084 return buildIntoRemoteView(remoteViews, R.id.notification_content_container,
5085 mBuilder.mN.contentView);
5086 }
5087
5088 /**
5089 * @hide
5090 */
5091 @Override
5092 public RemoteViews makeBigContentView() {
5093 RemoteViews customRemoteView = mBuilder.mN.bigContentView != null
5094 ? mBuilder.mN.bigContentView
5095 : mBuilder.mN.contentView;
5096 return makeBigContentViewWithCustomContent(customRemoteView);
5097 }
5098
5099 private RemoteViews makeBigContentViewWithCustomContent(RemoteViews customRemoteView) {
5100 RemoteViews remoteViews = super.makeBigContentView();
5101 if (remoteViews != null) {
5102 return buildIntoRemoteView(remoteViews, R.id.notification_main_column,
5103 customRemoteView);
5104 } else if (customRemoteView != mBuilder.mN.contentView){
5105 remoteViews = super.makeContentView();
5106 return buildIntoRemoteView(remoteViews, R.id.notification_content_container,
5107 customRemoteView);
5108 } else {
5109 return null;
5110 }
5111 }
5112
5113 /**
5114 * @hide
5115 */
5116 @Override
5117 public RemoteViews makeHeadsUpContentView() {
5118 RemoteViews customRemoteView = mBuilder.mN.headsUpContentView != null
5119 ? mBuilder.mN.headsUpContentView
5120 : mBuilder.mN.contentView;
5121 return makeBigContentViewWithCustomContent(customRemoteView);
5122 }
5123
5124 private RemoteViews buildIntoRemoteView(RemoteViews remoteViews, int id,
5125 RemoteViews customContent) {
Adrian Roos5081c0d2016-02-26 16:04:19 -08005126 if (customContent != null) {
Selim Cinekf91017e2016-03-14 12:25:09 -07005127 // Need to clone customContent before adding, because otherwise it can no longer be
5128 // parceled independently of remoteViews.
Adrian Roos5081c0d2016-02-26 16:04:19 -08005129 customContent = customContent.clone();
Selim Cinekf91017e2016-03-14 12:25:09 -07005130 remoteViews.removeAllViews(id);
5131 remoteViews.addView(id, customContent);
Adrian Roos5081c0d2016-02-26 16:04:19 -08005132 }
Selim Cinek03eb3b72016-02-18 10:39:45 -08005133 return remoteViews;
5134 }
5135 }
5136
Christoph Studer4600f9b2014-07-22 22:44:43 +02005137 // When adding a new Style subclass here, don't forget to update
5138 // Builder.getNotificationStyleClass.
5139
Griff Hazen61a9e862014-05-22 16:05:19 -07005140 /**
5141 * Extender interface for use with {@link Builder#extend}. Extenders may be used to add
5142 * metadata or change options on a notification builder.
5143 */
5144 public interface Extender {
5145 /**
5146 * Apply this extender to a notification builder.
5147 * @param builder the builder to be modified.
5148 * @return the build object for chaining.
5149 */
5150 public Builder extend(Builder builder);
5151 }
5152
5153 /**
5154 * Helper class to add wearable extensions to notifications.
5155 * <p class="note"> See
5156 * <a href="{@docRoot}wear/notifications/creating.html">Creating Notifications
5157 * for Android Wear</a> for more information on how to use this class.
5158 * <p>
5159 * To create a notification with wearable extensions:
5160 * <ol>
5161 * <li>Create a {@link android.app.Notification.Builder}, setting any desired
5162 * properties.
5163 * <li>Create a {@link android.app.Notification.WearableExtender}.
5164 * <li>Set wearable-specific properties using the
5165 * {@code add} and {@code set} methods of {@link android.app.Notification.WearableExtender}.
5166 * <li>Call {@link android.app.Notification.Builder#extend} to apply the extensions to a
5167 * notification.
5168 * <li>Post the notification to the notification system with the
5169 * {@code NotificationManager.notify(...)} methods.
5170 * </ol>
5171 *
5172 * <pre class="prettyprint">
5173 * Notification notif = new Notification.Builder(mContext)
5174 * .setContentTitle(&quot;New mail from &quot; + sender.toString())
5175 * .setContentText(subject)
5176 * .setSmallIcon(R.drawable.new_mail)
5177 * .extend(new Notification.WearableExtender()
5178 * .setContentIcon(R.drawable.new_mail))
5179 * .build();
5180 * NotificationManager notificationManger =
5181 * (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
5182 * notificationManger.notify(0, notif);</pre>
5183 *
5184 * <p>Wearable extensions can be accessed on an existing notification by using the
5185 * {@code WearableExtender(Notification)} constructor,
5186 * and then using the {@code get} methods to access values.
5187 *
5188 * <pre class="prettyprint">
5189 * Notification.WearableExtender wearableExtender = new Notification.WearableExtender(
5190 * notification);
Griff Hazen14f57992014-05-26 09:07:14 -07005191 * List&lt;Notification&gt; pages = wearableExtender.getPages();</pre>
Griff Hazen61a9e862014-05-22 16:05:19 -07005192 */
5193 public static final class WearableExtender implements Extender {
5194 /**
5195 * Sentinel value for an action index that is unset.
5196 */
5197 public static final int UNSET_ACTION_INDEX = -1;
5198
5199 /**
5200 * Size value for use with {@link #setCustomSizePreset} to show this notification with
5201 * default sizing.
5202 * <p>For custom display notifications created using {@link #setDisplayIntent},
Paul Soulosaa4f4bf2015-08-04 11:59:45 -07005203 * the default is {@link #SIZE_MEDIUM}. All other notifications size automatically based
Griff Hazen61a9e862014-05-22 16:05:19 -07005204 * on their content.
5205 */
5206 public static final int SIZE_DEFAULT = 0;
5207
5208 /**
5209 * Size value for use with {@link #setCustomSizePreset} to show this notification
5210 * with an extra small size.
5211 * <p>This value is only applicable for custom display notifications created using
5212 * {@link #setDisplayIntent}.
5213 */
5214 public static final int SIZE_XSMALL = 1;
5215
5216 /**
5217 * Size value for use with {@link #setCustomSizePreset} to show this notification
5218 * with a small size.
5219 * <p>This value is only applicable for custom display notifications created using
5220 * {@link #setDisplayIntent}.
5221 */
5222 public static final int SIZE_SMALL = 2;
5223
5224 /**
5225 * Size value for use with {@link #setCustomSizePreset} to show this notification
5226 * with a medium size.
5227 * <p>This value is only applicable for custom display notifications created using
5228 * {@link #setDisplayIntent}.
5229 */
5230 public static final int SIZE_MEDIUM = 3;
5231
5232 /**
5233 * Size value for use with {@link #setCustomSizePreset} to show this notification
5234 * with a large size.
5235 * <p>This value is only applicable for custom display notifications created using
5236 * {@link #setDisplayIntent}.
5237 */
5238 public static final int SIZE_LARGE = 4;
5239
Griff Hazend5f11f92014-05-27 15:40:09 -07005240 /**
5241 * Size value for use with {@link #setCustomSizePreset} to show this notification
5242 * full screen.
5243 * <p>This value is only applicable for custom display notifications created using
5244 * {@link #setDisplayIntent}.
5245 */
5246 public static final int SIZE_FULL_SCREEN = 5;
5247
Griff Hazen5f2edfc2014-09-29 16:28:44 -07005248 /**
5249 * Sentinel value for use with {@link #setHintScreenTimeout} to keep the screen on for a
5250 * short amount of time when this notification is displayed on the screen. This
5251 * is the default value.
5252 */
5253 public static final int SCREEN_TIMEOUT_SHORT = 0;
5254
5255 /**
5256 * Sentinel value for use with {@link #setHintScreenTimeout} to keep the screen on
5257 * for a longer amount of time when this notification is displayed on the screen.
5258 */
5259 public static final int SCREEN_TIMEOUT_LONG = -1;
5260
Griff Hazen61a9e862014-05-22 16:05:19 -07005261 /** Notification extra which contains wearable extensions */
5262 private static final String EXTRA_WEARABLE_EXTENSIONS = "android.wearable.EXTENSIONS";
5263
Pete Gastaf6781d2014-10-07 15:17:05 -04005264 // Keys within EXTRA_WEARABLE_EXTENSIONS for wearable options.
Griff Hazen61a9e862014-05-22 16:05:19 -07005265 private static final String KEY_ACTIONS = "actions";
5266 private static final String KEY_FLAGS = "flags";
5267 private static final String KEY_DISPLAY_INTENT = "displayIntent";
5268 private static final String KEY_PAGES = "pages";
5269 private static final String KEY_BACKGROUND = "background";
5270 private static final String KEY_CONTENT_ICON = "contentIcon";
5271 private static final String KEY_CONTENT_ICON_GRAVITY = "contentIconGravity";
5272 private static final String KEY_CONTENT_ACTION_INDEX = "contentActionIndex";
5273 private static final String KEY_CUSTOM_SIZE_PRESET = "customSizePreset";
5274 private static final String KEY_CUSTOM_CONTENT_HEIGHT = "customContentHeight";
5275 private static final String KEY_GRAVITY = "gravity";
Griff Hazen5f2edfc2014-09-29 16:28:44 -07005276 private static final String KEY_HINT_SCREEN_TIMEOUT = "hintScreenTimeout";
Griff Hazen61a9e862014-05-22 16:05:19 -07005277
5278 // Flags bitwise-ored to mFlags
5279 private static final int FLAG_CONTENT_INTENT_AVAILABLE_OFFLINE = 0x1;
5280 private static final int FLAG_HINT_HIDE_ICON = 1 << 1;
5281 private static final int FLAG_HINT_SHOW_BACKGROUND_ONLY = 1 << 2;
5282 private static final int FLAG_START_SCROLL_BOTTOM = 1 << 3;
Griff Hazen5f2edfc2014-09-29 16:28:44 -07005283 private static final int FLAG_HINT_AVOID_BACKGROUND_CLIPPING = 1 << 4;
Alex Hills9ab3a232016-04-05 14:54:56 -04005284 private static final int FLAG_HINT_CONTENT_INTENT_LAUNCHES_ACTIVITY = 1 << 6;
Griff Hazen61a9e862014-05-22 16:05:19 -07005285
5286 // Default value for flags integer
5287 private static final int DEFAULT_FLAGS = FLAG_CONTENT_INTENT_AVAILABLE_OFFLINE;
5288
5289 private static final int DEFAULT_CONTENT_ICON_GRAVITY = Gravity.END;
5290 private static final int DEFAULT_GRAVITY = Gravity.BOTTOM;
5291
5292 private ArrayList<Action> mActions = new ArrayList<Action>();
5293 private int mFlags = DEFAULT_FLAGS;
5294 private PendingIntent mDisplayIntent;
5295 private ArrayList<Notification> mPages = new ArrayList<Notification>();
5296 private Bitmap mBackground;
5297 private int mContentIcon;
5298 private int mContentIconGravity = DEFAULT_CONTENT_ICON_GRAVITY;
5299 private int mContentActionIndex = UNSET_ACTION_INDEX;
5300 private int mCustomSizePreset = SIZE_DEFAULT;
5301 private int mCustomContentHeight;
5302 private int mGravity = DEFAULT_GRAVITY;
Griff Hazen5f2edfc2014-09-29 16:28:44 -07005303 private int mHintScreenTimeout;
Griff Hazen61a9e862014-05-22 16:05:19 -07005304
5305 /**
5306 * Create a {@link android.app.Notification.WearableExtender} with default
5307 * options.
5308 */
5309 public WearableExtender() {
5310 }
5311
5312 public WearableExtender(Notification notif) {
5313 Bundle wearableBundle = notif.extras.getBundle(EXTRA_WEARABLE_EXTENSIONS);
5314 if (wearableBundle != null) {
5315 List<Action> actions = wearableBundle.getParcelableArrayList(KEY_ACTIONS);
5316 if (actions != null) {
5317 mActions.addAll(actions);
5318 }
5319
5320 mFlags = wearableBundle.getInt(KEY_FLAGS, DEFAULT_FLAGS);
5321 mDisplayIntent = wearableBundle.getParcelable(KEY_DISPLAY_INTENT);
5322
5323 Notification[] pages = getNotificationArrayFromBundle(
5324 wearableBundle, KEY_PAGES);
5325 if (pages != null) {
5326 Collections.addAll(mPages, pages);
5327 }
5328
5329 mBackground = wearableBundle.getParcelable(KEY_BACKGROUND);
5330 mContentIcon = wearableBundle.getInt(KEY_CONTENT_ICON);
5331 mContentIconGravity = wearableBundle.getInt(KEY_CONTENT_ICON_GRAVITY,
5332 DEFAULT_CONTENT_ICON_GRAVITY);
5333 mContentActionIndex = wearableBundle.getInt(KEY_CONTENT_ACTION_INDEX,
5334 UNSET_ACTION_INDEX);
5335 mCustomSizePreset = wearableBundle.getInt(KEY_CUSTOM_SIZE_PRESET,
5336 SIZE_DEFAULT);
5337 mCustomContentHeight = wearableBundle.getInt(KEY_CUSTOM_CONTENT_HEIGHT);
5338 mGravity = wearableBundle.getInt(KEY_GRAVITY, DEFAULT_GRAVITY);
Griff Hazen5f2edfc2014-09-29 16:28:44 -07005339 mHintScreenTimeout = wearableBundle.getInt(KEY_HINT_SCREEN_TIMEOUT);
Griff Hazen61a9e862014-05-22 16:05:19 -07005340 }
5341 }
5342
5343 /**
5344 * Apply wearable extensions to a notification that is being built. This is typically
5345 * called by the {@link android.app.Notification.Builder#extend} method of
5346 * {@link android.app.Notification.Builder}.
5347 */
5348 @Override
5349 public Notification.Builder extend(Notification.Builder builder) {
5350 Bundle wearableBundle = new Bundle();
5351
5352 if (!mActions.isEmpty()) {
5353 wearableBundle.putParcelableArrayList(KEY_ACTIONS, mActions);
5354 }
5355 if (mFlags != DEFAULT_FLAGS) {
5356 wearableBundle.putInt(KEY_FLAGS, mFlags);
5357 }
5358 if (mDisplayIntent != null) {
5359 wearableBundle.putParcelable(KEY_DISPLAY_INTENT, mDisplayIntent);
5360 }
5361 if (!mPages.isEmpty()) {
5362 wearableBundle.putParcelableArray(KEY_PAGES, mPages.toArray(
5363 new Notification[mPages.size()]));
5364 }
5365 if (mBackground != null) {
5366 wearableBundle.putParcelable(KEY_BACKGROUND, mBackground);
5367 }
5368 if (mContentIcon != 0) {
5369 wearableBundle.putInt(KEY_CONTENT_ICON, mContentIcon);
5370 }
5371 if (mContentIconGravity != DEFAULT_CONTENT_ICON_GRAVITY) {
5372 wearableBundle.putInt(KEY_CONTENT_ICON_GRAVITY, mContentIconGravity);
5373 }
5374 if (mContentActionIndex != UNSET_ACTION_INDEX) {
5375 wearableBundle.putInt(KEY_CONTENT_ACTION_INDEX,
5376 mContentActionIndex);
5377 }
5378 if (mCustomSizePreset != SIZE_DEFAULT) {
5379 wearableBundle.putInt(KEY_CUSTOM_SIZE_PRESET, mCustomSizePreset);
5380 }
5381 if (mCustomContentHeight != 0) {
5382 wearableBundle.putInt(KEY_CUSTOM_CONTENT_HEIGHT, mCustomContentHeight);
5383 }
5384 if (mGravity != DEFAULT_GRAVITY) {
5385 wearableBundle.putInt(KEY_GRAVITY, mGravity);
5386 }
Griff Hazen5f2edfc2014-09-29 16:28:44 -07005387 if (mHintScreenTimeout != 0) {
5388 wearableBundle.putInt(KEY_HINT_SCREEN_TIMEOUT, mHintScreenTimeout);
5389 }
Griff Hazen61a9e862014-05-22 16:05:19 -07005390
5391 builder.getExtras().putBundle(EXTRA_WEARABLE_EXTENSIONS, wearableBundle);
5392 return builder;
5393 }
5394
5395 @Override
5396 public WearableExtender clone() {
5397 WearableExtender that = new WearableExtender();
5398 that.mActions = new ArrayList<Action>(this.mActions);
5399 that.mFlags = this.mFlags;
5400 that.mDisplayIntent = this.mDisplayIntent;
5401 that.mPages = new ArrayList<Notification>(this.mPages);
5402 that.mBackground = this.mBackground;
5403 that.mContentIcon = this.mContentIcon;
5404 that.mContentIconGravity = this.mContentIconGravity;
5405 that.mContentActionIndex = this.mContentActionIndex;
5406 that.mCustomSizePreset = this.mCustomSizePreset;
5407 that.mCustomContentHeight = this.mCustomContentHeight;
5408 that.mGravity = this.mGravity;
Griff Hazen5f2edfc2014-09-29 16:28:44 -07005409 that.mHintScreenTimeout = this.mHintScreenTimeout;
Griff Hazen61a9e862014-05-22 16:05:19 -07005410 return that;
5411 }
5412
5413 /**
5414 * Add a wearable action to this notification.
5415 *
5416 * <p>When wearable actions are added using this method, the set of actions that
5417 * show on a wearable device splits from devices that only show actions added
5418 * using {@link android.app.Notification.Builder#addAction}. This allows for customization
5419 * of which actions display on different devices.
5420 *
5421 * @param action the action to add to this notification
5422 * @return this object for method chaining
5423 * @see android.app.Notification.Action
5424 */
5425 public WearableExtender addAction(Action action) {
5426 mActions.add(action);
5427 return this;
5428 }
5429
5430 /**
5431 * Adds wearable actions to this notification.
5432 *
5433 * <p>When wearable actions are added using this method, the set of actions that
5434 * show on a wearable device splits from devices that only show actions added
5435 * using {@link android.app.Notification.Builder#addAction}. This allows for customization
5436 * of which actions display on different devices.
5437 *
5438 * @param actions the actions to add to this notification
5439 * @return this object for method chaining
5440 * @see android.app.Notification.Action
5441 */
5442 public WearableExtender addActions(List<Action> actions) {
5443 mActions.addAll(actions);
5444 return this;
5445 }
5446
5447 /**
5448 * Clear all wearable actions present on this builder.
5449 * @return this object for method chaining.
5450 * @see #addAction
5451 */
5452 public WearableExtender clearActions() {
5453 mActions.clear();
5454 return this;
5455 }
5456
5457 /**
5458 * Get the wearable actions present on this notification.
5459 */
5460 public List<Action> getActions() {
5461 return mActions;
5462 }
5463
5464 /**
5465 * Set an intent to launch inside of an activity view when displaying
Griff Hazen14f57992014-05-26 09:07:14 -07005466 * this notification. The {@link PendingIntent} provided should be for an activity.
5467 *
5468 * <pre class="prettyprint">
5469 * Intent displayIntent = new Intent(context, MyDisplayActivity.class);
5470 * PendingIntent displayPendingIntent = PendingIntent.getActivity(context,
5471 * 0, displayIntent, PendingIntent.FLAG_UPDATE_CURRENT);
5472 * Notification notif = new Notification.Builder(context)
5473 * .extend(new Notification.WearableExtender()
5474 * .setDisplayIntent(displayPendingIntent)
5475 * .setCustomSizePreset(Notification.WearableExtender.SIZE_MEDIUM))
5476 * .build();</pre>
5477 *
5478 * <p>The activity to launch needs to allow embedding, must be exported, and
Griff Hazen831ca9d2014-06-17 00:38:38 -07005479 * should have an empty task affinity. It is also recommended to use the device
5480 * default light theme.
Griff Hazen14f57992014-05-26 09:07:14 -07005481 *
5482 * <p>Example AndroidManifest.xml entry:
5483 * <pre class="prettyprint">
5484 * &lt;activity android:name=&quot;com.example.MyDisplayActivity&quot;
5485 * android:exported=&quot;true&quot;
5486 * android:allowEmbedded=&quot;true&quot;
Griff Hazen831ca9d2014-06-17 00:38:38 -07005487 * android:taskAffinity=&quot;&quot;
5488 * android:theme=&quot;@android:style/Theme.DeviceDefault.Light&quot; /&gt;</pre>
Griff Hazen61a9e862014-05-22 16:05:19 -07005489 *
5490 * @param intent the {@link PendingIntent} for an activity
5491 * @return this object for method chaining
5492 * @see android.app.Notification.WearableExtender#getDisplayIntent
5493 */
5494 public WearableExtender setDisplayIntent(PendingIntent intent) {
5495 mDisplayIntent = intent;
5496 return this;
5497 }
5498
5499 /**
5500 * Get the intent to launch inside of an activity view when displaying this
5501 * notification. This {@code PendingIntent} should be for an activity.
5502 */
5503 public PendingIntent getDisplayIntent() {
5504 return mDisplayIntent;
5505 }
5506
5507 /**
5508 * Add an additional page of content to display with this notification. The current
5509 * notification forms the first page, and pages added using this function form
5510 * subsequent pages. This field can be used to separate a notification into multiple
5511 * sections.
5512 *
5513 * @param page the notification to add as another page
5514 * @return this object for method chaining
5515 * @see android.app.Notification.WearableExtender#getPages
5516 */
5517 public WearableExtender addPage(Notification page) {
5518 mPages.add(page);
5519 return this;
5520 }
5521
5522 /**
5523 * Add additional pages of content to display with this notification. The current
5524 * notification forms the first page, and pages added using this function form
5525 * subsequent pages. This field can be used to separate a notification into multiple
5526 * sections.
5527 *
5528 * @param pages a list of notifications
5529 * @return this object for method chaining
5530 * @see android.app.Notification.WearableExtender#getPages
5531 */
5532 public WearableExtender addPages(List<Notification> pages) {
5533 mPages.addAll(pages);
5534 return this;
5535 }
5536
5537 /**
5538 * Clear all additional pages present on this builder.
5539 * @return this object for method chaining.
5540 * @see #addPage
5541 */
5542 public WearableExtender clearPages() {
5543 mPages.clear();
5544 return this;
5545 }
5546
5547 /**
5548 * Get the array of additional pages of content for displaying this notification. The
5549 * current notification forms the first page, and elements within this array form
5550 * subsequent pages. This field can be used to separate a notification into multiple
5551 * sections.
5552 * @return the pages for this notification
5553 */
5554 public List<Notification> getPages() {
5555 return mPages;
5556 }
5557
5558 /**
5559 * Set a background image to be displayed behind the notification content.
5560 * Contrary to the {@link android.app.Notification.BigPictureStyle}, this background
5561 * will work with any notification style.
5562 *
5563 * @param background the background bitmap
5564 * @return this object for method chaining
5565 * @see android.app.Notification.WearableExtender#getBackground
5566 */
5567 public WearableExtender setBackground(Bitmap background) {
5568 mBackground = background;
5569 return this;
5570 }
5571
5572 /**
5573 * Get a background image to be displayed behind the notification content.
5574 * Contrary to the {@link android.app.Notification.BigPictureStyle}, this background
5575 * will work with any notification style.
5576 *
5577 * @return the background image
5578 * @see android.app.Notification.WearableExtender#setBackground
5579 */
5580 public Bitmap getBackground() {
5581 return mBackground;
5582 }
5583
5584 /**
5585 * Set an icon that goes with the content of this notification.
5586 */
5587 public WearableExtender setContentIcon(int icon) {
5588 mContentIcon = icon;
5589 return this;
5590 }
5591
5592 /**
5593 * Get an icon that goes with the content of this notification.
5594 */
5595 public int getContentIcon() {
5596 return mContentIcon;
5597 }
5598
5599 /**
5600 * Set the gravity that the content icon should have within the notification display.
5601 * Supported values include {@link android.view.Gravity#START} and
5602 * {@link android.view.Gravity#END}. The default value is {@link android.view.Gravity#END}.
5603 * @see #setContentIcon
5604 */
5605 public WearableExtender setContentIconGravity(int contentIconGravity) {
5606 mContentIconGravity = contentIconGravity;
5607 return this;
5608 }
5609
5610 /**
5611 * Get the gravity that the content icon should have within the notification display.
5612 * Supported values include {@link android.view.Gravity#START} and
5613 * {@link android.view.Gravity#END}. The default value is {@link android.view.Gravity#END}.
5614 * @see #getContentIcon
5615 */
5616 public int getContentIconGravity() {
5617 return mContentIconGravity;
5618 }
5619
5620 /**
5621 * Set an action from this notification's actions to be clickable with the content of
Griff Hazen14f57992014-05-26 09:07:14 -07005622 * this notification. This action will no longer display separately from the
5623 * notification's content.
5624 *
Griff Hazenca48d352014-05-28 22:37:13 -07005625 * <p>For notifications with multiple pages, child pages can also have content actions
Griff Hazen14f57992014-05-26 09:07:14 -07005626 * set, although the list of available actions comes from the main notification and not
5627 * from the child page's notification.
5628 *
5629 * @param actionIndex The index of the action to hoist onto the current notification page.
5630 * If wearable actions were added to the main notification, this index
5631 * will apply to that list, otherwise it will apply to the regular
5632 * actions list.
Griff Hazen61a9e862014-05-22 16:05:19 -07005633 */
5634 public WearableExtender setContentAction(int actionIndex) {
5635 mContentActionIndex = actionIndex;
5636 return this;
5637 }
5638
5639 /**
Griff Hazenca48d352014-05-28 22:37:13 -07005640 * Get the index of the notification action, if any, that was specified as being clickable
5641 * with the content of this notification. This action will no longer display separately
Griff Hazen14f57992014-05-26 09:07:14 -07005642 * from the notification's content.
Griff Hazen61a9e862014-05-22 16:05:19 -07005643 *
Griff Hazenca48d352014-05-28 22:37:13 -07005644 * <p>For notifications with multiple pages, child pages can also have content actions
Griff Hazen14f57992014-05-26 09:07:14 -07005645 * set, although the list of available actions comes from the main notification and not
5646 * from the child page's notification.
5647 *
5648 * <p>If wearable specific actions were added to the main notification, this index will
5649 * apply to that list, otherwise it will apply to the regular actions list.
Griff Hazenca48d352014-05-28 22:37:13 -07005650 *
5651 * @return the action index or {@link #UNSET_ACTION_INDEX} if no action was selected.
Griff Hazen61a9e862014-05-22 16:05:19 -07005652 */
5653 public int getContentAction() {
5654 return mContentActionIndex;
5655 }
5656
5657 /**
5658 * Set the gravity that this notification should have within the available viewport space.
5659 * Supported values include {@link android.view.Gravity#TOP},
5660 * {@link android.view.Gravity#CENTER_VERTICAL} and {@link android.view.Gravity#BOTTOM}.
5661 * The default value is {@link android.view.Gravity#BOTTOM}.
5662 */
5663 public WearableExtender setGravity(int gravity) {
5664 mGravity = gravity;
5665 return this;
5666 }
5667
5668 /**
5669 * Get the gravity that this notification should have within the available viewport space.
5670 * Supported values include {@link android.view.Gravity#TOP},
5671 * {@link android.view.Gravity#CENTER_VERTICAL} and {@link android.view.Gravity#BOTTOM}.
5672 * The default value is {@link android.view.Gravity#BOTTOM}.
5673 */
5674 public int getGravity() {
5675 return mGravity;
5676 }
5677
5678 /**
5679 * Set the custom size preset for the display of this notification out of the available
5680 * presets found in {@link android.app.Notification.WearableExtender}, e.g.
5681 * {@link #SIZE_LARGE}.
5682 * <p>Some custom size presets are only applicable for custom display notifications created
5683 * using {@link android.app.Notification.WearableExtender#setDisplayIntent}. Check the
5684 * documentation for the preset in question. See also
5685 * {@link #setCustomContentHeight} and {@link #getCustomSizePreset}.
5686 */
5687 public WearableExtender setCustomSizePreset(int sizePreset) {
5688 mCustomSizePreset = sizePreset;
5689 return this;
5690 }
5691
5692 /**
5693 * Get the custom size preset for the display of this notification out of the available
5694 * presets found in {@link android.app.Notification.WearableExtender}, e.g.
5695 * {@link #SIZE_LARGE}.
5696 * <p>Some custom size presets are only applicable for custom display notifications created
5697 * using {@link #setDisplayIntent}. Check the documentation for the preset in question.
5698 * See also {@link #setCustomContentHeight} and {@link #setCustomSizePreset}.
5699 */
5700 public int getCustomSizePreset() {
5701 return mCustomSizePreset;
5702 }
5703
5704 /**
5705 * Set the custom height in pixels for the display of this notification's content.
5706 * <p>This option is only available for custom display notifications created
5707 * using {@link android.app.Notification.WearableExtender#setDisplayIntent}. See also
5708 * {@link android.app.Notification.WearableExtender#setCustomSizePreset} and
5709 * {@link #getCustomContentHeight}.
5710 */
5711 public WearableExtender setCustomContentHeight(int height) {
5712 mCustomContentHeight = height;
5713 return this;
5714 }
5715
5716 /**
5717 * Get the custom height in pixels for the display of this notification's content.
5718 * <p>This option is only available for custom display notifications created
5719 * using {@link #setDisplayIntent}. See also {@link #setCustomSizePreset} and
5720 * {@link #setCustomContentHeight}.
5721 */
5722 public int getCustomContentHeight() {
5723 return mCustomContentHeight;
5724 }
5725
5726 /**
5727 * Set whether the scrolling position for the contents of this notification should start
5728 * at the bottom of the contents instead of the top when the contents are too long to
5729 * display within the screen. Default is false (start scroll at the top).
5730 */
5731 public WearableExtender setStartScrollBottom(boolean startScrollBottom) {
5732 setFlag(FLAG_START_SCROLL_BOTTOM, startScrollBottom);
5733 return this;
5734 }
5735
5736 /**
5737 * Get whether the scrolling position for the contents of this notification should start
5738 * at the bottom of the contents instead of the top when the contents are too long to
5739 * display within the screen. Default is false (start scroll at the top).
5740 */
5741 public boolean getStartScrollBottom() {
5742 return (mFlags & FLAG_START_SCROLL_BOTTOM) != 0;
5743 }
5744
5745 /**
5746 * Set whether the content intent is available when the wearable device is not connected
5747 * to a companion device. The user can still trigger this intent when the wearable device
5748 * is offline, but a visual hint will indicate that the content intent may not be available.
5749 * Defaults to true.
5750 */
5751 public WearableExtender setContentIntentAvailableOffline(
5752 boolean contentIntentAvailableOffline) {
5753 setFlag(FLAG_CONTENT_INTENT_AVAILABLE_OFFLINE, contentIntentAvailableOffline);
5754 return this;
5755 }
5756
5757 /**
5758 * Get whether the content intent is available when the wearable device is not connected
5759 * to a companion device. The user can still trigger this intent when the wearable device
5760 * is offline, but a visual hint will indicate that the content intent may not be available.
5761 * Defaults to true.
5762 */
5763 public boolean getContentIntentAvailableOffline() {
5764 return (mFlags & FLAG_CONTENT_INTENT_AVAILABLE_OFFLINE) != 0;
5765 }
5766
5767 /**
5768 * Set a hint that this notification's icon should not be displayed.
5769 * @param hintHideIcon {@code true} to hide the icon, {@code false} otherwise.
5770 * @return this object for method chaining
5771 */
5772 public WearableExtender setHintHideIcon(boolean hintHideIcon) {
5773 setFlag(FLAG_HINT_HIDE_ICON, hintHideIcon);
5774 return this;
5775 }
5776
5777 /**
5778 * Get a hint that this notification's icon should not be displayed.
5779 * @return {@code true} if this icon should not be displayed, false otherwise.
5780 * The default value is {@code false} if this was never set.
5781 */
5782 public boolean getHintHideIcon() {
5783 return (mFlags & FLAG_HINT_HIDE_ICON) != 0;
5784 }
5785
5786 /**
5787 * Set a visual hint that only the background image of this notification should be
5788 * displayed, and other semantic content should be hidden. This hint is only applicable
5789 * to sub-pages added using {@link #addPage}.
5790 */
5791 public WearableExtender setHintShowBackgroundOnly(boolean hintShowBackgroundOnly) {
5792 setFlag(FLAG_HINT_SHOW_BACKGROUND_ONLY, hintShowBackgroundOnly);
5793 return this;
5794 }
5795
5796 /**
5797 * Get a visual hint that only the background image of this notification should be
5798 * displayed, and other semantic content should be hidden. This hint is only applicable
5799 * to sub-pages added using {@link android.app.Notification.WearableExtender#addPage}.
5800 */
5801 public boolean getHintShowBackgroundOnly() {
5802 return (mFlags & FLAG_HINT_SHOW_BACKGROUND_ONLY) != 0;
5803 }
5804
Griff Hazen5f2edfc2014-09-29 16:28:44 -07005805 /**
Griff Hazen9c5be4e2014-11-17 17:47:50 -08005806 * Set a hint that this notification's background should not be clipped if possible,
5807 * and should instead be resized to fully display on the screen, retaining the aspect
5808 * ratio of the image. This can be useful for images like barcodes or qr codes.
Griff Hazen5f2edfc2014-09-29 16:28:44 -07005809 * @param hintAvoidBackgroundClipping {@code true} to avoid clipping if possible.
5810 * @return this object for method chaining
5811 */
5812 public WearableExtender setHintAvoidBackgroundClipping(
5813 boolean hintAvoidBackgroundClipping) {
5814 setFlag(FLAG_HINT_AVOID_BACKGROUND_CLIPPING, hintAvoidBackgroundClipping);
5815 return this;
5816 }
5817
5818 /**
Griff Hazen9c5be4e2014-11-17 17:47:50 -08005819 * Get a hint that this notification's background should not be clipped if possible,
5820 * and should instead be resized to fully display on the screen, retaining the aspect
5821 * ratio of the image. This can be useful for images like barcodes or qr codes.
Griff Hazen5f2edfc2014-09-29 16:28:44 -07005822 * @return {@code true} if it's ok if the background is clipped on the screen, false
5823 * otherwise. The default value is {@code false} if this was never set.
5824 */
5825 public boolean getHintAvoidBackgroundClipping() {
5826 return (mFlags & FLAG_HINT_AVOID_BACKGROUND_CLIPPING) != 0;
5827 }
5828
5829 /**
5830 * Set a hint that the screen should remain on for at least this duration when
5831 * this notification is displayed on the screen.
5832 * @param timeout The requested screen timeout in milliseconds. Can also be either
5833 * {@link #SCREEN_TIMEOUT_SHORT} or {@link #SCREEN_TIMEOUT_LONG}.
5834 * @return this object for method chaining
5835 */
5836 public WearableExtender setHintScreenTimeout(int timeout) {
5837 mHintScreenTimeout = timeout;
5838 return this;
5839 }
5840
5841 /**
5842 * Get the duration, in milliseconds, that the screen should remain on for
5843 * when this notification is displayed.
5844 * @return the duration in milliseconds if > 0, or either one of the sentinel values
5845 * {@link #SCREEN_TIMEOUT_SHORT} or {@link #SCREEN_TIMEOUT_LONG}.
5846 */
5847 public int getHintScreenTimeout() {
5848 return mHintScreenTimeout;
5849 }
5850
Alex Hills9ab3a232016-04-05 14:54:56 -04005851 /**
5852 * Set a hint that this notification's content intent will launch an {@link Activity}
5853 * directly, telling the platform that it can generate the appropriate transitions.
5854 * @param hintContentIntentLaunchesActivity {@code true} if the content intent will launch
5855 * an activity and transitions should be generated, false otherwise.
5856 * @return this object for method chaining
5857 */
5858 public WearableExtender setHintContentIntentLaunchesActivity(
5859 boolean hintContentIntentLaunchesActivity) {
5860 setFlag(FLAG_HINT_CONTENT_INTENT_LAUNCHES_ACTIVITY, hintContentIntentLaunchesActivity);
5861 return this;
5862 }
5863
5864 /**
5865 * Get a hint that this notification's content intent will launch an {@link Activity}
5866 * directly, telling the platform that it can generate the appropriate transitions
5867 * @return {@code true} if the content intent will launch an activity and transitions should
5868 * be generated, false otherwise. The default value is {@code false} if this was never set.
5869 */
5870 public boolean getHintContentIntentLaunchesActivity() {
5871 return (mFlags & FLAG_HINT_CONTENT_INTENT_LAUNCHES_ACTIVITY) != 0;
5872 }
5873
Griff Hazen61a9e862014-05-22 16:05:19 -07005874 private void setFlag(int mask, boolean value) {
5875 if (value) {
5876 mFlags |= mask;
5877 } else {
5878 mFlags &= ~mask;
5879 }
5880 }
5881 }
5882
5883 /**
Zhen Yu Song9d5528b2014-11-14 15:34:39 -08005884 * <p>Helper class to add Android Auto extensions to notifications. To create a notification
5885 * with car extensions:
5886 *
5887 * <ol>
5888 * <li>Create an {@link Notification.Builder}, setting any desired
5889 * properties.
5890 * <li>Create a {@link CarExtender}.
5891 * <li>Set car-specific properties using the {@code add} and {@code set} methods of
5892 * {@link CarExtender}.
5893 * <li>Call {@link Notification.Builder#extend(Notification.Extender)}
5894 * to apply the extensions to a notification.
5895 * </ol>
5896 *
5897 * <pre class="prettyprint">
5898 * Notification notification = new Notification.Builder(context)
5899 * ...
5900 * .extend(new CarExtender()
5901 * .set*(...))
5902 * .build();
5903 * </pre>
5904 *
5905 * <p>Car extensions can be accessed on an existing notification by using the
5906 * {@code CarExtender(Notification)} constructor, and then using the {@code get} methods
5907 * to access values.
5908 */
5909 public static final class CarExtender implements Extender {
5910 private static final String TAG = "CarExtender";
5911
5912 private static final String EXTRA_CAR_EXTENDER = "android.car.EXTENSIONS";
5913 private static final String EXTRA_LARGE_ICON = "large_icon";
5914 private static final String EXTRA_CONVERSATION = "car_conversation";
5915 private static final String EXTRA_COLOR = "app_color";
5916
5917 private Bitmap mLargeIcon;
5918 private UnreadConversation mUnreadConversation;
5919 private int mColor = Notification.COLOR_DEFAULT;
5920
5921 /**
5922 * Create a {@link CarExtender} with default options.
5923 */
5924 public CarExtender() {
5925 }
5926
5927 /**
5928 * Create a {@link CarExtender} from the CarExtender options of an existing Notification.
5929 *
5930 * @param notif The notification from which to copy options.
5931 */
5932 public CarExtender(Notification notif) {
5933 Bundle carBundle = notif.extras == null ?
5934 null : notif.extras.getBundle(EXTRA_CAR_EXTENDER);
5935 if (carBundle != null) {
5936 mLargeIcon = carBundle.getParcelable(EXTRA_LARGE_ICON);
5937 mColor = carBundle.getInt(EXTRA_COLOR, Notification.COLOR_DEFAULT);
5938
5939 Bundle b = carBundle.getBundle(EXTRA_CONVERSATION);
5940 mUnreadConversation = UnreadConversation.getUnreadConversationFromBundle(b);
5941 }
5942 }
5943
5944 /**
5945 * Apply car extensions to a notification that is being built. This is typically called by
5946 * the {@link Notification.Builder#extend(Notification.Extender)}
5947 * method of {@link Notification.Builder}.
5948 */
5949 @Override
5950 public Notification.Builder extend(Notification.Builder builder) {
5951 Bundle carExtensions = new Bundle();
5952
5953 if (mLargeIcon != null) {
5954 carExtensions.putParcelable(EXTRA_LARGE_ICON, mLargeIcon);
5955 }
5956 if (mColor != Notification.COLOR_DEFAULT) {
5957 carExtensions.putInt(EXTRA_COLOR, mColor);
5958 }
5959
5960 if (mUnreadConversation != null) {
5961 Bundle b = mUnreadConversation.getBundleForUnreadConversation();
5962 carExtensions.putBundle(EXTRA_CONVERSATION, b);
5963 }
5964
5965 builder.getExtras().putBundle(EXTRA_CAR_EXTENDER, carExtensions);
5966 return builder;
5967 }
5968
5969 /**
5970 * Sets the accent color to use when Android Auto presents the notification.
5971 *
5972 * Android Auto uses the color set with {@link Notification.Builder#setColor(int)}
5973 * to accent the displayed notification. However, not all colors are acceptable in an
5974 * automotive setting. This method can be used to override the color provided in the
5975 * notification in such a situation.
5976 */
Tor Norbye80756e32015-03-02 09:39:27 -08005977 public CarExtender setColor(@ColorInt int color) {
Zhen Yu Song9d5528b2014-11-14 15:34:39 -08005978 mColor = color;
5979 return this;
5980 }
5981
5982 /**
5983 * Gets the accent color.
5984 *
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005985 * @see #setColor
Zhen Yu Song9d5528b2014-11-14 15:34:39 -08005986 */
Tor Norbye80756e32015-03-02 09:39:27 -08005987 @ColorInt
Zhen Yu Song9d5528b2014-11-14 15:34:39 -08005988 public int getColor() {
5989 return mColor;
5990 }
5991
5992 /**
5993 * Sets the large icon of the car notification.
5994 *
5995 * If no large icon is set in the extender, Android Auto will display the icon
5996 * specified by {@link Notification.Builder#setLargeIcon(android.graphics.Bitmap)}
5997 *
5998 * @param largeIcon The large icon to use in the car notification.
5999 * @return This object for method chaining.
6000 */
6001 public CarExtender setLargeIcon(Bitmap largeIcon) {
6002 mLargeIcon = largeIcon;
6003 return this;
6004 }
6005
6006 /**
6007 * Gets the large icon used in this car notification, or null if no icon has been set.
6008 *
6009 * @return The large icon for the car notification.
6010 * @see CarExtender#setLargeIcon
6011 */
6012 public Bitmap getLargeIcon() {
6013 return mLargeIcon;
6014 }
6015
6016 /**
6017 * Sets the unread conversation in a message notification.
6018 *
6019 * @param unreadConversation The unread part of the conversation this notification conveys.
6020 * @return This object for method chaining.
6021 */
6022 public CarExtender setUnreadConversation(UnreadConversation unreadConversation) {
6023 mUnreadConversation = unreadConversation;
6024 return this;
6025 }
6026
6027 /**
6028 * Returns the unread conversation conveyed by this notification.
6029 * @see #setUnreadConversation(UnreadConversation)
6030 */
6031 public UnreadConversation getUnreadConversation() {
6032 return mUnreadConversation;
6033 }
6034
6035 /**
6036 * A class which holds the unread messages from a conversation.
6037 */
6038 public static class UnreadConversation {
6039 private static final String KEY_AUTHOR = "author";
6040 private static final String KEY_TEXT = "text";
6041 private static final String KEY_MESSAGES = "messages";
6042 private static final String KEY_REMOTE_INPUT = "remote_input";
6043 private static final String KEY_ON_REPLY = "on_reply";
6044 private static final String KEY_ON_READ = "on_read";
6045 private static final String KEY_PARTICIPANTS = "participants";
6046 private static final String KEY_TIMESTAMP = "timestamp";
6047
6048 private final String[] mMessages;
6049 private final RemoteInput mRemoteInput;
6050 private final PendingIntent mReplyPendingIntent;
6051 private final PendingIntent mReadPendingIntent;
6052 private final String[] mParticipants;
6053 private final long mLatestTimestamp;
6054
6055 UnreadConversation(String[] messages, RemoteInput remoteInput,
6056 PendingIntent replyPendingIntent, PendingIntent readPendingIntent,
6057 String[] participants, long latestTimestamp) {
6058 mMessages = messages;
6059 mRemoteInput = remoteInput;
6060 mReadPendingIntent = readPendingIntent;
6061 mReplyPendingIntent = replyPendingIntent;
6062 mParticipants = participants;
6063 mLatestTimestamp = latestTimestamp;
6064 }
6065
6066 /**
6067 * Gets the list of messages conveyed by this notification.
6068 */
6069 public String[] getMessages() {
6070 return mMessages;
6071 }
6072
6073 /**
6074 * Gets the remote input that will be used to convey the response to a message list, or
6075 * null if no such remote input exists.
6076 */
6077 public RemoteInput getRemoteInput() {
6078 return mRemoteInput;
6079 }
6080
6081 /**
6082 * Gets the pending intent that will be triggered when the user replies to this
6083 * notification.
6084 */
6085 public PendingIntent getReplyPendingIntent() {
6086 return mReplyPendingIntent;
6087 }
6088
6089 /**
6090 * Gets the pending intent that Android Auto will send after it reads aloud all messages
6091 * in this object's message list.
6092 */
6093 public PendingIntent getReadPendingIntent() {
6094 return mReadPendingIntent;
6095 }
6096
6097 /**
6098 * Gets the participants in the conversation.
6099 */
6100 public String[] getParticipants() {
6101 return mParticipants;
6102 }
6103
6104 /**
6105 * Gets the firs participant in the conversation.
6106 */
6107 public String getParticipant() {
6108 return mParticipants.length > 0 ? mParticipants[0] : null;
6109 }
6110
6111 /**
6112 * Gets the timestamp of the conversation.
6113 */
6114 public long getLatestTimestamp() {
6115 return mLatestTimestamp;
6116 }
6117
6118 Bundle getBundleForUnreadConversation() {
6119 Bundle b = new Bundle();
6120 String author = null;
6121 if (mParticipants != null && mParticipants.length > 1) {
6122 author = mParticipants[0];
6123 }
6124 Parcelable[] messages = new Parcelable[mMessages.length];
6125 for (int i = 0; i < messages.length; i++) {
6126 Bundle m = new Bundle();
6127 m.putString(KEY_TEXT, mMessages[i]);
6128 m.putString(KEY_AUTHOR, author);
6129 messages[i] = m;
6130 }
6131 b.putParcelableArray(KEY_MESSAGES, messages);
6132 if (mRemoteInput != null) {
6133 b.putParcelable(KEY_REMOTE_INPUT, mRemoteInput);
6134 }
6135 b.putParcelable(KEY_ON_REPLY, mReplyPendingIntent);
6136 b.putParcelable(KEY_ON_READ, mReadPendingIntent);
6137 b.putStringArray(KEY_PARTICIPANTS, mParticipants);
6138 b.putLong(KEY_TIMESTAMP, mLatestTimestamp);
6139 return b;
6140 }
6141
6142 static UnreadConversation getUnreadConversationFromBundle(Bundle b) {
6143 if (b == null) {
6144 return null;
6145 }
6146 Parcelable[] parcelableMessages = b.getParcelableArray(KEY_MESSAGES);
6147 String[] messages = null;
6148 if (parcelableMessages != null) {
6149 String[] tmp = new String[parcelableMessages.length];
6150 boolean success = true;
6151 for (int i = 0; i < tmp.length; i++) {
6152 if (!(parcelableMessages[i] instanceof Bundle)) {
6153 success = false;
6154 break;
6155 }
6156 tmp[i] = ((Bundle) parcelableMessages[i]).getString(KEY_TEXT);
6157 if (tmp[i] == null) {
6158 success = false;
6159 break;
6160 }
6161 }
6162 if (success) {
6163 messages = tmp;
6164 } else {
6165 return null;
6166 }
6167 }
6168
6169 PendingIntent onRead = b.getParcelable(KEY_ON_READ);
6170 PendingIntent onReply = b.getParcelable(KEY_ON_REPLY);
6171
6172 RemoteInput remoteInput = b.getParcelable(KEY_REMOTE_INPUT);
6173
6174 String[] participants = b.getStringArray(KEY_PARTICIPANTS);
6175 if (participants == null || participants.length != 1) {
6176 return null;
6177 }
6178
6179 return new UnreadConversation(messages,
6180 remoteInput,
6181 onReply,
6182 onRead,
6183 participants, b.getLong(KEY_TIMESTAMP));
6184 }
6185 };
6186
6187 /**
6188 * Builder class for {@link CarExtender.UnreadConversation} objects.
6189 */
6190 public static class Builder {
6191 private final List<String> mMessages = new ArrayList<String>();
6192 private final String mParticipant;
6193 private RemoteInput mRemoteInput;
6194 private PendingIntent mReadPendingIntent;
6195 private PendingIntent mReplyPendingIntent;
6196 private long mLatestTimestamp;
6197
6198 /**
6199 * Constructs a new builder for {@link CarExtender.UnreadConversation}.
6200 *
6201 * @param name The name of the other participant in the conversation.
6202 */
6203 public Builder(String name) {
6204 mParticipant = name;
6205 }
6206
6207 /**
6208 * Appends a new unread message to the list of messages for this conversation.
6209 *
6210 * The messages should be added from oldest to newest.
6211 *
6212 * @param message The text of the new unread message.
6213 * @return This object for method chaining.
6214 */
6215 public Builder addMessage(String message) {
6216 mMessages.add(message);
6217 return this;
6218 }
6219
6220 /**
6221 * Sets the pending intent and remote input which will convey the reply to this
6222 * notification.
6223 *
6224 * @param pendingIntent The pending intent which will be triggered on a reply.
6225 * @param remoteInput The remote input parcelable which will carry the reply.
6226 * @return This object for method chaining.
6227 *
6228 * @see CarExtender.UnreadConversation#getRemoteInput
6229 * @see CarExtender.UnreadConversation#getReplyPendingIntent
6230 */
6231 public Builder setReplyAction(
6232 PendingIntent pendingIntent, RemoteInput remoteInput) {
6233 mRemoteInput = remoteInput;
6234 mReplyPendingIntent = pendingIntent;
6235
6236 return this;
6237 }
6238
6239 /**
6240 * Sets the pending intent that will be sent once the messages in this notification
6241 * are read.
6242 *
6243 * @param pendingIntent The pending intent to use.
6244 * @return This object for method chaining.
6245 */
6246 public Builder setReadPendingIntent(PendingIntent pendingIntent) {
6247 mReadPendingIntent = pendingIntent;
6248 return this;
6249 }
6250
6251 /**
6252 * Sets the timestamp of the most recent message in an unread conversation.
6253 *
6254 * If a messaging notification has been posted by your application and has not
6255 * yet been cancelled, posting a later notification with the same id and tag
6256 * but without a newer timestamp may result in Android Auto not displaying a
6257 * heads up notification for the later notification.
6258 *
6259 * @param timestamp The timestamp of the most recent message in the conversation.
6260 * @return This object for method chaining.
6261 */
6262 public Builder setLatestTimestamp(long timestamp) {
6263 mLatestTimestamp = timestamp;
6264 return this;
6265 }
6266
6267 /**
6268 * Builds a new unread conversation object.
6269 *
6270 * @return The new unread conversation object.
6271 */
6272 public UnreadConversation build() {
6273 String[] messages = mMessages.toArray(new String[mMessages.size()]);
6274 String[] participants = { mParticipant };
6275 return new UnreadConversation(messages, mRemoteInput, mReplyPendingIntent,
6276 mReadPendingIntent, participants, mLatestTimestamp);
6277 }
6278 }
6279 }
6280
6281 /**
Griff Hazen61a9e862014-05-22 16:05:19 -07006282 * Get an array of Notification objects from a parcelable array bundle field.
6283 * Update the bundle to have a typed array so fetches in the future don't need
6284 * to do an array copy.
6285 */
6286 private static Notification[] getNotificationArrayFromBundle(Bundle bundle, String key) {
6287 Parcelable[] array = bundle.getParcelableArray(key);
6288 if (array instanceof Notification[] || array == null) {
6289 return (Notification[]) array;
6290 }
6291 Notification[] typedArray = Arrays.copyOf(array, array.length,
6292 Notification[].class);
6293 bundle.putParcelableArray(key, typedArray);
6294 return typedArray;
6295 }
Christoph Studer4600f9b2014-07-22 22:44:43 +02006296
6297 private static class BuilderRemoteViews extends RemoteViews {
6298 public BuilderRemoteViews(Parcel parcel) {
6299 super(parcel);
6300 }
6301
Kenny Guy77320062014-08-27 21:37:15 +01006302 public BuilderRemoteViews(ApplicationInfo appInfo, int layoutId) {
6303 super(appInfo, layoutId);
Christoph Studer4600f9b2014-07-22 22:44:43 +02006304 }
6305
6306 @Override
6307 public BuilderRemoteViews clone() {
6308 Parcel p = Parcel.obtain();
6309 writeToParcel(p, 0);
6310 p.setDataPosition(0);
6311 BuilderRemoteViews brv = new BuilderRemoteViews(p);
6312 p.recycle();
6313 return brv;
6314 }
6315 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006316}