blob: aee4ca7d899af52afbec62af7d4a6c2124dbf475 [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;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080024import android.content.Context;
25import android.content.Intent;
Kenny Guy77320062014-08-27 21:37:15 +010026import android.content.pm.ApplicationInfo;
Christoph Studer4600f9b2014-07-22 22:44:43 +020027import android.content.pm.PackageManager.NameNotFoundException;
Jorim Jaggief72a192014-08-26 21:57:46 +020028import android.content.res.ColorStateList;
Joe Onoratoef1e7762010-09-17 18:38:38 -040029import android.graphics.Bitmap;
Kenny Guy8a0101b2014-05-08 23:34:12 +010030import android.graphics.Canvas;
Jorim Jaggi5c2d8462014-03-21 17:37:00 +010031import android.graphics.PorterDuff;
Kenny Guy8a0101b2014-05-08 23:34:12 +010032import android.graphics.drawable.Drawable;
Dan Sandlerd63f9322015-05-06 15:18:49 -040033import android.graphics.drawable.Icon;
John Spurlockc0650f022014-07-19 13:22:39 -040034import android.media.AudioAttributes;
Jeff Sharkey098d5802012-04-26 17:30:34 -070035import android.media.AudioManager;
Jeff Browndba34ba2014-06-24 20:46:03 -070036import android.media.session.MediaSession;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080037import android.net.Uri;
Daniel Sandlerdcbaf662013-04-26 16:23:09 -040038import android.os.BadParcelableException;
Christoph Studer239f8352014-08-25 15:13:18 +020039import android.os.Build;
Daniel Sandler2561b0b2012-02-13 21:04:12 -050040import android.os.Bundle;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080041import android.os.Parcel;
42import android.os.Parcelable;
Daniel Sandlera2985ed2012-04-03 16:42:00 -040043import android.os.SystemClock;
Jeff Sharkey6d515712012-09-20 16:06:08 -070044import android.os.UserHandle;
Selim Cinek60a54252016-02-26 17:03:25 -080045import android.text.SpannableStringBuilder;
46import android.text.Spanned;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080047import android.text.TextUtils;
Selim Cinek60a54252016-02-26 17:03:25 -080048import android.text.style.AbsoluteSizeSpan;
Selim Cinek89991a22016-03-07 19:51:54 -080049import android.text.style.CharacterStyle;
Selim Cinek60a54252016-02-26 17:03:25 -080050import android.text.style.RelativeSizeSpan;
51import android.text.style.TextAppearanceSpan;
Daniel Sandlerdcbaf662013-04-26 16:23:09 -040052import android.util.Log;
Dan Sandler50128532015-12-08 15:42:41 -050053import android.util.SparseArray;
Daniel Sandler9f7936a2012-05-21 16:14:28 -040054import android.util.TypedValue;
Griff Hazen61a9e862014-05-22 16:05:19 -070055import android.view.Gravity;
Selim Cinekea4bef72015-12-02 15:51:10 -080056import android.view.NotificationHeaderView;
Joe Onorato8595a3d2010-11-19 18:12:07 -080057import android.view.View;
Adrian Roos9b123cf2016-02-04 14:55:57 -080058import android.view.ViewGroup;
Jeff Sharkey1c400132011-08-05 14:50:13 -070059import android.widget.ProgressBar;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080060import android.widget.RemoteViews;
61
Griff Hazen959591e2014-05-15 22:26:18 -070062import com.android.internal.R;
Griff Hazenc091ba82014-05-16 10:13:26 -070063import com.android.internal.util.NotificationColorUtil;
Griff Hazen959591e2014-05-15 22:26:18 -070064
Tor Norbyed9273d62013-05-30 15:59:53 -070065import java.lang.annotation.Retention;
66import java.lang.annotation.RetentionPolicy;
Christoph Studer4600f9b2014-07-22 22:44:43 +020067import java.lang.reflect.Constructor;
Daniel Sandler2561b0b2012-02-13 21:04:12 -050068import java.util.ArrayList;
Griff Hazen61a9e862014-05-22 16:05:19 -070069import java.util.Arrays;
Griff Hazen5cadc3b2014-05-20 09:55:39 -070070import java.util.Collections;
Griff Hazen61a9e862014-05-22 16:05:19 -070071import java.util.List;
Dan Sandler50128532015-12-08 15:42:41 -050072import java.util.Set;
Joe Onorato561d3852010-11-20 18:09:34 -080073
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080074/**
75 * A class that represents how a persistent notification is to be presented to
76 * the user using the {@link android.app.NotificationManager}.
77 *
Joe Onoratocb109a02011-01-18 17:57:41 -080078 * <p>The {@link Notification.Builder Notification.Builder} has been added to make it
79 * easier to construct Notifications.</p>
80 *
Joe Fernandez558459f2011-10-13 16:47:36 -070081 * <div class="special reference">
82 * <h3>Developer Guides</h3>
83 * <p>For a guide to creating notifications, read the
84 * <a href="{@docRoot}guide/topics/ui/notifiers/notifications.html">Status Bar Notifications</a>
85 * developer guide.</p>
86 * </div>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080087 */
88public class Notification implements Parcelable
89{
Daniel Sandlerdcbaf662013-04-26 16:23:09 -040090 private static final String TAG = "Notification";
91
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080092 /**
Daniel Sandler01df1c62014-06-09 10:54:01 -040093 * An activity that provides a user interface for adjusting notification preferences for its
94 * containing application. Optional but recommended for apps that post
95 * {@link android.app.Notification Notifications}.
96 */
97 @SdkConstant(SdkConstantType.INTENT_CATEGORY)
98 public static final String INTENT_CATEGORY_NOTIFICATION_PREFERENCES
99 = "android.intent.category.NOTIFICATION_PREFERENCES";
100
101 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800102 * Use all default values (where applicable).
103 */
104 public static final int DEFAULT_ALL = ~0;
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500105
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800106 /**
107 * Use the default notification sound. This will ignore any given
108 * {@link #sound}.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500109 *
Chris Wren47c20a12014-06-18 17:27:29 -0400110 * <p>
111 * A notification that is noisy is more likely to be presented as a heads-up notification.
112 * </p>
113 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800114 * @see #defaults
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500115 */
116
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800117 public static final int DEFAULT_SOUND = 1;
118
119 /**
120 * Use the default notification vibrate. This will ignore any given
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500121 * {@link #vibrate}. Using phone vibration requires the
Scott Mainb8b36452009-04-26 15:50:49 -0700122 * {@link android.Manifest.permission#VIBRATE VIBRATE} permission.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500123 *
Chris Wren47c20a12014-06-18 17:27:29 -0400124 * <p>
125 * A notification that vibrates is more likely to be presented as a heads-up notification.
126 * </p>
127 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800128 * @see #defaults
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500129 */
130
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800131 public static final int DEFAULT_VIBRATE = 2;
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500132
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800133 /**
134 * Use the default notification lights. This will ignore the
135 * {@link #FLAG_SHOW_LIGHTS} bit, and {@link #ledARGB}, {@link #ledOffMS}, or
136 * {@link #ledOnMS}.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500137 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800138 * @see #defaults
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500139 */
140
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800141 public static final int DEFAULT_LIGHTS = 4;
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500142
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800143 /**
Christoph Studer535ec612014-09-03 15:47:47 +0200144 * Maximum length of CharSequences accepted by Builder and friends.
145 *
146 * <p>
147 * Avoids spamming the system with overly large strings such as full e-mails.
148 */
149 private static final int MAX_CHARSEQUENCE_LENGTH = 5 * 1024;
150
151 /**
Adrian Roose458aa82015-12-08 16:17:19 -0800152 * Maximum entries of reply text that are accepted by Builder and friends.
153 */
154 private static final int MAX_REPLY_HISTORY = 5;
155
156 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500157 * A timestamp related to this notification, in milliseconds since the epoch.
Joe Malin8d40d042012-11-05 11:36:40 -0800158 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500159 * Default value: {@link System#currentTimeMillis() Now}.
160 *
161 * Choose a timestamp that will be most relevant to the user. For most finite events, this
162 * corresponds to the time the event happened (or will happen, in the case of events that have
163 * yet to occur but about which the user is being informed). Indefinite events should be
Joe Malin8d40d042012-11-05 11:36:40 -0800164 * timestamped according to when the activity began.
165 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500166 * Some examples:
Joe Malin8d40d042012-11-05 11:36:40 -0800167 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500168 * <ul>
169 * <li>Notification of a new chat message should be stamped when the message was received.</li>
170 * <li>Notification of an ongoing file download (with a progress bar, for example) should be stamped when the download started.</li>
171 * <li>Notification of a completed file download should be stamped when the download finished.</li>
172 * <li>Notification of an upcoming meeting should be stamped with the time the meeting will begin (that is, in the future).</li>
173 * <li>Notification of an ongoing stopwatch (increasing timer) should be stamped with the watch's start time.
174 * <li>Notification of an ongoing countdown timer should be stamped with the timer's end time.
Joe Malin8d40d042012-11-05 11:36:40 -0800175 * </ul>
176 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800177 */
178 public long when;
179
180 /**
181 * The resource id of a drawable to use as the icon in the status bar.
Dan Sandler86647982015-05-13 23:41:13 -0400182 *
183 * @deprecated Use {@link Builder#setSmallIcon(Icon)} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800184 */
Dan Sandler86647982015-05-13 23:41:13 -0400185 @Deprecated
Tor Norbye7b9c9122013-05-30 16:48:33 -0700186 @DrawableRes
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800187 public int icon;
188
189 /**
Joe Onorato46439ce2010-11-19 13:56:21 -0800190 * If the icon in the status bar is to have more than one level, you can set this. Otherwise,
191 * leave it at its default value of 0.
192 *
193 * @see android.widget.ImageView#setImageLevel
Griff Hazen959591e2014-05-15 22:26:18 -0700194 * @see android.graphics.drawable.Drawable#setLevel
Joe Onorato46439ce2010-11-19 13:56:21 -0800195 */
196 public int iconLevel;
197
198 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500199 * The number of events that this notification represents. For example, in a new mail
200 * notification, this could be the number of unread messages.
Joe Malin8d40d042012-11-05 11:36:40 -0800201 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500202 * The system may or may not use this field to modify the appearance of the notification. For
203 * example, before {@link android.os.Build.VERSION_CODES#HONEYCOMB}, this number was
204 * superimposed over the icon in the status bar. Starting with
205 * {@link android.os.Build.VERSION_CODES#HONEYCOMB}, the template used by
206 * {@link Notification.Builder} has displayed the number in the expanded notification view.
Joe Malin8d40d042012-11-05 11:36:40 -0800207 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500208 * If the number is 0 or negative, it is never shown.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800209 */
210 public int number;
211
212 /**
213 * The intent to execute when the expanded status entry is clicked. If
214 * this is an activity, it must include the
215 * {@link android.content.Intent#FLAG_ACTIVITY_NEW_TASK} flag, which requires
Scott Main7aee61f2011-02-08 11:25:01 -0800216 * that you take care of task management as described in the
217 * <a href="{@docRoot}guide/topics/fundamentals/tasks-and-back-stack.html">Tasks and Back
Dianne Hackborn6ceca582012-01-10 15:24:26 -0800218 * Stack</a> document. In particular, make sure to read the notification section
219 * <a href="{@docRoot}guide/topics/ui/notifiers/notifications.html#HandlingNotifications">Handling
220 * Notifications</a> for the correct ways to launch an application from a
221 * notification.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800222 */
223 public PendingIntent contentIntent;
224
225 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500226 * The intent to execute when the notification is explicitly dismissed by the user, either with
227 * the "Clear All" button or by swiping it away individually.
228 *
229 * This probably shouldn't be launching an activity since several of those will be sent
230 * at the same time.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800231 */
232 public PendingIntent deleteIntent;
233
234 /**
Dianne Hackborn170bae72010-09-03 15:14:28 -0700235 * An intent to launch instead of posting the notification to the status bar.
Joe Onoratocb109a02011-01-18 17:57:41 -0800236 *
Chris Wren47c20a12014-06-18 17:27:29 -0400237 * <p>
238 * The system UI may choose to display a heads-up notification, instead of
239 * launching this intent, while the user is using the device.
240 * </p>
241 *
Joe Onoratocb109a02011-01-18 17:57:41 -0800242 * @see Notification.Builder#setFullScreenIntent
Daniel Sandlere46cbd32010-06-17 10:35:26 -0400243 */
244 public PendingIntent fullScreenIntent;
245
246 /**
Dan Sandler5fcdf6e2014-07-18 11:31:15 -0400247 * Text that summarizes this notification for accessibility services.
248 *
249 * As of the L release, this text is no longer shown on screen, but it is still useful to
250 * accessibility services (where it serves as an audible announcement of the notification's
251 * appearance).
Joe Onoratoef1e7762010-09-17 18:38:38 -0400252 *
Joe Onorato46439ce2010-11-19 13:56:21 -0800253 * @see #tickerView
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800254 */
255 public CharSequence tickerText;
256
257 /**
Dan Sandler5fcdf6e2014-07-18 11:31:15 -0400258 * Formerly, a view showing the {@link #tickerText}.
259 *
260 * No longer displayed in the status bar as of API 21.
Joe Onoratoef1e7762010-09-17 18:38:38 -0400261 */
Dan Sandler5fcdf6e2014-07-18 11:31:15 -0400262 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -0800263 public RemoteViews tickerView;
Joe Onoratoef1e7762010-09-17 18:38:38 -0400264
265 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400266 * The view that will represent this notification in the notification list (which is pulled
267 * down from the status bar).
268 *
Julia Reynoldsd4ea7412016-02-17 14:00:56 -0500269 * As of N, this field may be null. The notification view is determined by the inputs
270 * to {@link Notification.Builder}; a custom RemoteViews can optionally be
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400271 * supplied with {@link Notification.Builder#setCustomContentView(RemoteViews)}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800272 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400273 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800274 public RemoteViews contentView;
275
Daniel Sandlera0a938c2012-03-15 08:42:37 -0400276 /**
Daniel Sandler4dfbe832012-04-11 14:51:46 -0400277 * A large-format version of {@link #contentView}, giving the Notification an
Daniel Sandlerf3b73432012-03-27 15:01:25 -0400278 * opportunity to show more detail. The system UI may choose to show this
279 * instead of the normal content view at its discretion.
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400280 *
Julia Reynoldsd4ea7412016-02-17 14:00:56 -0500281 * As of N, this field may be null. The expanded notification view is determined by the
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400282 * inputs to {@link Notification.Builder}; a custom RemoteViews can optionally be
283 * supplied with {@link Notification.Builder#setCustomBigContentView(RemoteViews)}.
Daniel Sandlerf3b73432012-03-27 15:01:25 -0400284 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400285 @Deprecated
Daniel Sandlerf3b73432012-03-27 15:01:25 -0400286 public RemoteViews bigContentView;
287
Chris Wren8fd39ec2014-02-27 17:43:26 -0500288
289 /**
Chris Wren47c20a12014-06-18 17:27:29 -0400290 * A medium-format version of {@link #contentView}, providing the Notification an
291 * opportunity to add action buttons to contentView. At its discretion, the system UI may
292 * choose to show this as a heads-up notification, which will pop up so the user can see
293 * it without leaving their current activity.
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400294 *
Julia Reynoldsd4ea7412016-02-17 14:00:56 -0500295 * As of N, this field may be null. The heads-up notification view is determined by the
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400296 * inputs to {@link Notification.Builder}; a custom RemoteViews can optionally be
297 * supplied with {@link Notification.Builder#setCustomHeadsUpContentView(RemoteViews)}.
Chris Wren8fd39ec2014-02-27 17:43:26 -0500298 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400299 @Deprecated
Chris Wren8fd39ec2014-02-27 17:43:26 -0500300 public RemoteViews headsUpContentView;
301
Daniel Sandlerf3b73432012-03-27 15:01:25 -0400302 /**
Dan Sandler86647982015-05-13 23:41:13 -0400303 * A large bitmap to be shown in the notification content area.
304 *
305 * @deprecated Use {@link Builder#setLargeIcon(Icon)} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800306 */
Dan Sandler86647982015-05-13 23:41:13 -0400307 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -0800308 public Bitmap largeIcon;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800309
310 /**
311 * The sound to play.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500312 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800313 * <p>
Chris Wren47c20a12014-06-18 17:27:29 -0400314 * A notification that is noisy is more likely to be presented as a heads-up notification.
315 * </p>
316 *
317 * <p>
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500318 * To play the default notification sound, see {@link #defaults}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800319 * </p>
320 */
321 public Uri sound;
322
323 /**
324 * Use this constant as the value for audioStreamType to request that
325 * the default stream type for notifications be used. Currently the
Jeff Sharkey098d5802012-04-26 17:30:34 -0700326 * default stream type is {@link AudioManager#STREAM_NOTIFICATION}.
John Spurlockc0650f022014-07-19 13:22:39 -0400327 *
328 * @deprecated Use {@link #audioAttributes} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800329 */
Jean-Michel Trivi81f871e2014-08-06 16:32:38 -0700330 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800331 public static final int STREAM_DEFAULT = -1;
332
333 /**
334 * The audio stream type to use when playing the sound.
335 * Should be one of the STREAM_ constants from
336 * {@link android.media.AudioManager}.
John Spurlockc0650f022014-07-19 13:22:39 -0400337 *
338 * @deprecated Use {@link #audioAttributes} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800339 */
Jean-Michel Trivi81f871e2014-08-06 16:32:38 -0700340 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800341 public int audioStreamType = STREAM_DEFAULT;
342
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800343 /**
John Spurlockc0650f022014-07-19 13:22:39 -0400344 * The default value of {@link #audioAttributes}.
345 */
346 public static final AudioAttributes AUDIO_ATTRIBUTES_DEFAULT = new AudioAttributes.Builder()
347 .setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
348 .setUsage(AudioAttributes.USAGE_NOTIFICATION)
349 .build();
350
351 /**
352 * The {@link AudioAttributes audio attributes} to use when playing the sound.
353 */
354 public AudioAttributes audioAttributes = AUDIO_ATTRIBUTES_DEFAULT;
355
356 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500357 * The pattern with which to vibrate.
358 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800359 * <p>
360 * To vibrate the default pattern, see {@link #defaults}.
361 * </p>
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500362 *
Chris Wren47c20a12014-06-18 17:27:29 -0400363 * <p>
364 * A notification that vibrates is more likely to be presented as a heads-up notification.
365 * </p>
366 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800367 * @see android.os.Vibrator#vibrate(long[],int)
368 */
369 public long[] vibrate;
370
371 /**
372 * The color of the led. The hardware will do its best approximation.
373 *
374 * @see #FLAG_SHOW_LIGHTS
375 * @see #flags
376 */
Tor Norbye80756e32015-03-02 09:39:27 -0800377 @ColorInt
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800378 public int ledARGB;
379
380 /**
381 * The number of milliseconds for the LED to be on while it's flashing.
382 * The hardware will do its best approximation.
383 *
384 * @see #FLAG_SHOW_LIGHTS
385 * @see #flags
386 */
387 public int ledOnMS;
388
389 /**
390 * The number of milliseconds for the LED to be off while it's flashing.
391 * The hardware will do its best approximation.
392 *
393 * @see #FLAG_SHOW_LIGHTS
394 * @see #flags
395 */
396 public int ledOffMS;
397
398 /**
399 * Specifies which values should be taken from the defaults.
400 * <p>
401 * To set, OR the desired from {@link #DEFAULT_SOUND},
402 * {@link #DEFAULT_VIBRATE}, {@link #DEFAULT_LIGHTS}. For all default
403 * values, use {@link #DEFAULT_ALL}.
404 * </p>
405 */
406 public int defaults;
407
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800408 /**
409 * Bit to be bitwise-ored into the {@link #flags} field that should be
410 * set if you want the LED on for this notification.
411 * <ul>
412 * <li>To turn the LED off, pass 0 in the alpha channel for colorARGB
413 * or 0 for both ledOnMS and ledOffMS.</li>
414 * <li>To turn the LED on, pass 1 for ledOnMS and 0 for ledOffMS.</li>
415 * <li>To flash the LED, pass the number of milliseconds that it should
416 * be on and off to ledOnMS and ledOffMS.</li>
417 * </ul>
418 * <p>
419 * Since hardware varies, you are not guaranteed that any of the values
420 * you pass are honored exactly. Use the system defaults (TODO) if possible
421 * because they will be set to values that work on any given hardware.
422 * <p>
423 * The alpha channel must be set for forward compatibility.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500424 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800425 */
426 public static final int FLAG_SHOW_LIGHTS = 0x00000001;
427
428 /**
429 * Bit to be bitwise-ored into the {@link #flags} field that should be
430 * set if this notification is in reference to something that is ongoing,
431 * like a phone call. It should not be set if this notification is in
432 * reference to something that happened at a particular point in time,
433 * like a missed phone call.
434 */
435 public static final int FLAG_ONGOING_EVENT = 0x00000002;
436
437 /**
438 * Bit to be bitwise-ored into the {@link #flags} field that if set,
Scott Mainb8b36452009-04-26 15:50:49 -0700439 * the audio will be repeated until the notification is
440 * cancelled or the notification window is opened.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800441 */
442 public static final int FLAG_INSISTENT = 0x00000004;
443
444 /**
445 * Bit to be bitwise-ored into the {@link #flags} field that should be
Griff Hazen293977b2014-04-28 08:37:20 -0700446 * set if you would only like the sound, vibrate and ticker to be played
447 * if the notification was not already showing.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800448 */
449 public static final int FLAG_ONLY_ALERT_ONCE = 0x00000008;
450
451 /**
452 * Bit to be bitwise-ored into the {@link #flags} field that should be
453 * set if the notification should be canceled when it is clicked by the
Daniel Sandler8aa9ae62012-12-04 23:31:47 -0500454 * user.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800455 */
456 public static final int FLAG_AUTO_CANCEL = 0x00000010;
457
458 /**
459 * Bit to be bitwise-ored into the {@link #flags} field that should be
460 * set if the notification should not be canceled when the user clicks
461 * the Clear all button.
462 */
463 public static final int FLAG_NO_CLEAR = 0x00000020;
464
Dianne Hackbornd8a43f62009-08-17 23:33:56 -0700465 /**
466 * Bit to be bitwise-ored into the {@link #flags} field that should be
467 * set if this notification represents a currently running service. This
468 * will normally be set for you by {@link Service#startForeground}.
469 */
470 public static final int FLAG_FOREGROUND_SERVICE = 0x00000040;
471
Daniel Sandlere46cbd32010-06-17 10:35:26 -0400472 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500473 * Obsolete flag indicating high-priority notifications; use the priority field instead.
Joe Malin8d40d042012-11-05 11:36:40 -0800474 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500475 * @deprecated Use {@link #priority} with a positive value.
Daniel Sandlere46cbd32010-06-17 10:35:26 -0400476 */
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500477 public static final int FLAG_HIGH_PRIORITY = 0x00000080;
Daniel Sandlere46cbd32010-06-17 10:35:26 -0400478
Griff Hazendfcb0802014-02-11 12:00:00 -0800479 /**
480 * Bit to be bitswise-ored into the {@link #flags} field that should be
481 * set if this notification is relevant to the current device only
482 * and it is not recommended that it bridge to other devices.
483 */
484 public static final int FLAG_LOCAL_ONLY = 0x00000100;
485
Griff Hazen5cadc3b2014-05-20 09:55:39 -0700486 /**
487 * Bit to be bitswise-ored into the {@link #flags} field that should be
488 * set if this notification is the group summary for a group of notifications.
489 * Grouped notifications may display in a cluster or stack on devices which
490 * support such rendering. Requires a group key also be set using {@link Builder#setGroup}.
491 */
492 public static final int FLAG_GROUP_SUMMARY = 0x00000200;
493
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800494 public int flags;
495
Tor Norbyed9273d62013-05-30 15:59:53 -0700496 /** @hide */
497 @IntDef({PRIORITY_DEFAULT,PRIORITY_LOW,PRIORITY_MIN,PRIORITY_HIGH,PRIORITY_MAX})
498 @Retention(RetentionPolicy.SOURCE)
499 public @interface Priority {}
500
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800501 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500502 * Default notification {@link #priority}. If your application does not prioritize its own
503 * notifications, use this value for all notifications.
504 */
505 public static final int PRIORITY_DEFAULT = 0;
506
507 /**
508 * Lower {@link #priority}, for items that are less important. The UI may choose to show these
509 * items smaller, or at a different position in the list, compared with your app's
510 * {@link #PRIORITY_DEFAULT} items.
511 */
512 public static final int PRIORITY_LOW = -1;
513
514 /**
515 * Lowest {@link #priority}; these items might not be shown to the user except under special
516 * circumstances, such as detailed notification logs.
517 */
518 public static final int PRIORITY_MIN = -2;
519
520 /**
521 * Higher {@link #priority}, for more important notifications or alerts. The UI may choose to
522 * show these items larger, or at a different position in notification lists, compared with
523 * your app's {@link #PRIORITY_DEFAULT} items.
524 */
525 public static final int PRIORITY_HIGH = 1;
526
527 /**
528 * Highest {@link #priority}, for your application's most important items that require the
529 * user's prompt attention or input.
530 */
531 public static final int PRIORITY_MAX = 2;
532
533 /**
534 * Relative priority for this notification.
Joe Malin8d40d042012-11-05 11:36:40 -0800535 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500536 * Priority is an indication of how much of the user's valuable attention should be consumed by
537 * this notification. Low-priority notifications may be hidden from the user in certain
538 * situations, while the user might be interrupted for a higher-priority notification. The
Daniel Sandler6738eee2012-11-16 12:03:32 -0500539 * system will make a determination about how to interpret this priority when presenting
540 * the notification.
Chris Wren47c20a12014-06-18 17:27:29 -0400541 *
542 * <p>
543 * A notification that is at least {@link #PRIORITY_HIGH} is more likely to be presented
544 * as a heads-up notification.
545 * </p>
546 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500547 */
Tor Norbyed9273d62013-05-30 15:59:53 -0700548 @Priority
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500549 public int priority;
Joe Malin8d40d042012-11-05 11:36:40 -0800550
Dan Sandler26e81cf2014-05-06 10:01:27 -0400551 /**
552 * Accent color (an ARGB integer like the constants in {@link android.graphics.Color})
553 * to be applied by the standard Style templates when presenting this notification.
554 *
555 * The current template design constructs a colorful header image by overlaying the
556 * {@link #icon} image (stenciled in white) atop a field of this color. Alpha components are
557 * ignored.
558 */
Tor Norbye80756e32015-03-02 09:39:27 -0800559 @ColorInt
Dan Sandler26e81cf2014-05-06 10:01:27 -0400560 public int color = COLOR_DEFAULT;
561
562 /**
563 * Special value of {@link #color} telling the system not to decorate this notification with
564 * any special color but instead use default colors when presenting this notification.
565 */
Tor Norbye80756e32015-03-02 09:39:27 -0800566 @ColorInt
Dan Sandler26e81cf2014-05-06 10:01:27 -0400567 public static final int COLOR_DEFAULT = 0; // AKA Color.TRANSPARENT
Dan Sandler0bf2ed82013-12-21 23:33:41 -0600568
569 /**
Adrian Roos4ff3b122016-02-01 12:26:13 -0800570 * Special value of {@link #color} used as a place holder for an invalid color.
571 */
572 @ColorInt
573 private static final int COLOR_INVALID = 1;
574
575 /**
Dan Sandler0bf2ed82013-12-21 23:33:41 -0600576 * Sphere of visibility of this notification, which affects how and when the SystemUI reveals
577 * the notification's presence and contents in untrusted situations (namely, on the secure
578 * lockscreen).
579 *
580 * The default level, {@link #VISIBILITY_PRIVATE}, behaves exactly as notifications have always
581 * done on Android: The notification's {@link #icon} and {@link #tickerText} (if available) are
582 * shown in all situations, but the contents are only available if the device is unlocked for
583 * the appropriate user.
584 *
585 * A more permissive policy can be expressed by {@link #VISIBILITY_PUBLIC}; such a notification
586 * can be read even in an "insecure" context (that is, above a secure lockscreen).
587 * To modify the public version of this notification—for example, to redact some portions—see
588 * {@link Builder#setPublicVersion(Notification)}.
589 *
590 * Finally, a notification can be made {@link #VISIBILITY_SECRET}, which will suppress its icon
591 * and ticker until the user has bypassed the lockscreen.
592 */
593 public int visibility;
594
Griff Hazenfc3922d2014-08-20 11:56:44 -0700595 /**
596 * Notification visibility: Show this notification in its entirety on all lockscreens.
597 *
598 * {@see #visibility}
599 */
Dan Sandler0bf2ed82013-12-21 23:33:41 -0600600 public static final int VISIBILITY_PUBLIC = 1;
Griff Hazenfc3922d2014-08-20 11:56:44 -0700601
602 /**
603 * Notification visibility: Show this notification on all lockscreens, but conceal sensitive or
604 * private information on secure lockscreens.
605 *
606 * {@see #visibility}
607 */
Dan Sandler0bf2ed82013-12-21 23:33:41 -0600608 public static final int VISIBILITY_PRIVATE = 0;
Griff Hazenfc3922d2014-08-20 11:56:44 -0700609
610 /**
611 * Notification visibility: Do not reveal any part of this notification on a secure lockscreen.
612 *
613 * {@see #visibility}
614 */
Dan Sandler0bf2ed82013-12-21 23:33:41 -0600615 public static final int VISIBILITY_SECRET = -1;
616
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500617 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400618 * Notification category: incoming call (voice or video) or similar synchronous communication request.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500619 */
John Spurlockfd7f1e02014-03-18 16:41:57 -0400620 public static final String CATEGORY_CALL = "call";
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500621
622 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400623 * Notification category: incoming direct message (SMS, instant message, etc.).
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500624 */
John Spurlockfd7f1e02014-03-18 16:41:57 -0400625 public static final String CATEGORY_MESSAGE = "msg";
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500626
627 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400628 * Notification category: asynchronous bulk message (email).
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500629 */
John Spurlockfd7f1e02014-03-18 16:41:57 -0400630 public static final String CATEGORY_EMAIL = "email";
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500631
632 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400633 * Notification category: calendar event.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500634 */
John Spurlockfd7f1e02014-03-18 16:41:57 -0400635 public static final String CATEGORY_EVENT = "event";
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500636
637 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400638 * Notification category: promotion or advertisement.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500639 */
John Spurlockfd7f1e02014-03-18 16:41:57 -0400640 public static final String CATEGORY_PROMO = "promo";
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500641
642 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400643 * Notification category: alarm or timer.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500644 */
John Spurlockfd7f1e02014-03-18 16:41:57 -0400645 public static final String CATEGORY_ALARM = "alarm";
646
647 /**
648 * Notification category: progress of a long-running background operation.
649 */
650 public static final String CATEGORY_PROGRESS = "progress";
651
652 /**
653 * Notification category: social network or sharing update.
654 */
655 public static final String CATEGORY_SOCIAL = "social";
656
657 /**
658 * Notification category: error in background operation or authentication status.
659 */
660 public static final String CATEGORY_ERROR = "err";
661
662 /**
663 * Notification category: media transport control for playback.
664 */
665 public static final String CATEGORY_TRANSPORT = "transport";
666
667 /**
668 * Notification category: system or device status update. Reserved for system use.
669 */
670 public static final String CATEGORY_SYSTEM = "sys";
671
672 /**
673 * Notification category: indication of running background service.
674 */
675 public static final String CATEGORY_SERVICE = "service";
676
677 /**
John Spurlock0a69c8c2014-03-21 13:30:57 -0400678 * Notification category: a specific, timely recommendation for a single thing.
679 * For example, a news app might want to recommend a news story it believes the user will
680 * want to read next.
681 */
682 public static final String CATEGORY_RECOMMENDATION = "recommendation";
683
684 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400685 * Notification category: ongoing information about device or contextual status.
686 */
687 public static final String CATEGORY_STATUS = "status";
688
689 /**
John Spurlock24d3dad2015-04-02 12:24:02 -0400690 * Notification category: user-scheduled reminder.
691 */
692 public static final String CATEGORY_REMINDER = "reminder";
693
694 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400695 * One of the predefined notification categories (see the <code>CATEGORY_*</code> constants)
696 * that best describes this Notification. May be used by the system for ranking and filtering.
697 */
698 public String category;
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500699
Griff Hazen5cadc3b2014-05-20 09:55:39 -0700700 private String mGroupKey;
701
702 /**
703 * Get the key used to group this notification into a cluster or stack
704 * with other notifications on devices which support such rendering.
705 */
706 public String getGroup() {
707 return mGroupKey;
708 }
709
710 private String mSortKey;
711
712 /**
713 * Get a sort key that orders this notification among other notifications from the
714 * same package. This can be useful if an external sort was already applied and an app
715 * would like to preserve this. Notifications will be sorted lexicographically using this
716 * value, although providing different priorities in addition to providing sort key may
717 * cause this value to be ignored.
718 *
719 * <p>This sort key can also be used to order members of a notification group. See
720 * {@link Builder#setGroup}.
721 *
722 * @see String#compareTo(String)
723 */
724 public String getSortKey() {
725 return mSortKey;
726 }
727
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500728 /**
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400729 * Additional semantic data to be carried around with this Notification.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400730 * <p>
731 * The extras keys defined here are intended to capture the original inputs to {@link Builder}
732 * APIs, and are intended to be used by
733 * {@link android.service.notification.NotificationListenerService} implementations to extract
734 * detailed information from notification objects.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500735 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400736 public Bundle extras = new Bundle();
737
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400738 /**
739 * {@link #extras} key: this is the title of the notification,
740 * as supplied to {@link Builder#setContentTitle(CharSequence)}.
741 */
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -0500742 public static final String EXTRA_TITLE = "android.title";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400743
744 /**
745 * {@link #extras} key: this is the title of the notification when shown in expanded form,
746 * e.g. as supplied to {@link BigTextStyle#setBigContentTitle(CharSequence)}.
747 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400748 public static final String EXTRA_TITLE_BIG = EXTRA_TITLE + ".big";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400749
750 /**
751 * {@link #extras} key: this is the main text payload, as supplied to
752 * {@link Builder#setContentText(CharSequence)}.
753 */
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -0500754 public static final String EXTRA_TEXT = "android.text";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400755
756 /**
757 * {@link #extras} key: this is a third line of text, as supplied to
758 * {@link Builder#setSubText(CharSequence)}.
759 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400760 public static final String EXTRA_SUB_TEXT = "android.subText";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400761
762 /**
Adrian Roose458aa82015-12-08 16:17:19 -0800763 * {@link #extras} key: this is the remote input history, as supplied to
764 * {@link Builder#setRemoteInputHistory(CharSequence[])}.
765 */
766 public static final String EXTRA_REMOTE_INPUT_HISTORY = "android.remoteInputHistory";
767
768 /**
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400769 * {@link #extras} key: this is a small piece of additional text as supplied to
770 * {@link Builder#setContentInfo(CharSequence)}.
771 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400772 public static final String EXTRA_INFO_TEXT = "android.infoText";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400773
774 /**
775 * {@link #extras} key: this is a line of summary information intended to be shown
776 * alongside expanded notifications, as supplied to (e.g.)
777 * {@link BigTextStyle#setSummaryText(CharSequence)}.
778 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400779 public static final String EXTRA_SUMMARY_TEXT = "android.summaryText";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400780
781 /**
Christoph Studer4600f9b2014-07-22 22:44:43 +0200782 * {@link #extras} key: this is the longer text shown in the big form of a
783 * {@link BigTextStyle} notification, as supplied to
784 * {@link BigTextStyle#bigText(CharSequence)}.
785 */
786 public static final String EXTRA_BIG_TEXT = "android.bigText";
787
788 /**
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400789 * {@link #extras} key: this is the resource ID of the notification's main small icon, as
790 * supplied to {@link Builder#setSmallIcon(int)}.
791 */
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -0500792 public static final String EXTRA_SMALL_ICON = "android.icon";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400793
794 /**
795 * {@link #extras} key: this is a bitmap to be used instead of the small icon when showing the
796 * notification payload, as
797 * supplied to {@link Builder#setLargeIcon(android.graphics.Bitmap)}.
798 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400799 public static final String EXTRA_LARGE_ICON = "android.largeIcon";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400800
801 /**
802 * {@link #extras} key: this is a bitmap to be used instead of the one from
803 * {@link Builder#setLargeIcon(android.graphics.Bitmap)} when the notification is
804 * shown in its expanded form, as supplied to
805 * {@link BigPictureStyle#bigLargeIcon(android.graphics.Bitmap)}.
806 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400807 public static final String EXTRA_LARGE_ICON_BIG = EXTRA_LARGE_ICON + ".big";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400808
809 /**
810 * {@link #extras} key: this is the progress value supplied to
811 * {@link Builder#setProgress(int, int, boolean)}.
812 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400813 public static final String EXTRA_PROGRESS = "android.progress";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400814
815 /**
816 * {@link #extras} key: this is the maximum value supplied to
817 * {@link Builder#setProgress(int, int, boolean)}.
818 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400819 public static final String EXTRA_PROGRESS_MAX = "android.progressMax";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400820
821 /**
822 * {@link #extras} key: whether the progress bar is indeterminate, supplied to
823 * {@link Builder#setProgress(int, int, boolean)}.
824 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400825 public static final String EXTRA_PROGRESS_INDETERMINATE = "android.progressIndeterminate";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400826
827 /**
828 * {@link #extras} key: whether {@link #when} should be shown as a count-up timer (specifically
829 * a {@link android.widget.Chronometer}) instead of a timestamp, as supplied to
830 * {@link Builder#setUsesChronometer(boolean)}.
831 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400832 public static final String EXTRA_SHOW_CHRONOMETER = "android.showChronometer";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400833
834 /**
Selim Cinek81c23aa2016-02-25 16:23:13 -0800835 * {@link #extras} key: whether the chronometer set on the notification should count down
836 * instead of counting up. Is only relevant if key {@link #EXTRA_SHOW_CHRONOMETER} is present.
837 */
838 public static final String EXTRA_CHRONOMETER_COUNTS_DOWN = "android.chronometerCountsDown";
839
840 /**
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400841 * {@link #extras} key: whether {@link #when} should be shown,
842 * as supplied to {@link Builder#setShowWhen(boolean)}.
843 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400844 public static final String EXTRA_SHOW_WHEN = "android.showWhen";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400845
846 /**
847 * {@link #extras} key: this is a bitmap to be shown in {@link BigPictureStyle} expanded
848 * notifications, supplied to {@link BigPictureStyle#bigPicture(android.graphics.Bitmap)}.
849 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400850 public static final String EXTRA_PICTURE = "android.picture";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400851
852 /**
853 * {@link #extras} key: An array of CharSequences to show in {@link InboxStyle} expanded
854 * notifications, each of which was supplied to {@link InboxStyle#addLine(CharSequence)}.
855 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400856 public static final String EXTRA_TEXT_LINES = "android.textLines";
Dan Sandler842dd772014-05-15 09:36:47 -0400857
858 /**
859 * {@link #extras} key: A string representing the name of the specific
860 * {@link android.app.Notification.Style} used to create this notification.
861 */
Chris Wren91ad5632013-06-05 15:05:57 -0400862 public static final String EXTRA_TEMPLATE = "android.template";
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400863
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400864 /**
Chris Wrene6c48932014-09-29 17:19:27 -0400865 * {@link #extras} key: A String array containing the people that this notification relates to,
866 * each of which was supplied to {@link Builder#addPerson(String)}.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400867 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400868 public static final String EXTRA_PEOPLE = "android.people";
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500869
870 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400871 * Allow certain system-generated notifications to appear before the device is provisioned.
872 * Only available to notifications coming from the android package.
873 * @hide
874 */
875 public static final String EXTRA_ALLOW_DURING_SETUP = "android.allowDuringSetup";
876
877 /**
Jose Limae9e3b3b2014-05-18 23:44:50 -0700878 * {@link #extras} key: A
879 * {@link android.content.ContentUris content URI} pointing to an image that can be displayed
880 * in the background when the notification is selected. The URI must point to an image stream
881 * suitable for passing into
882 * {@link android.graphics.BitmapFactory#decodeStream(java.io.InputStream)
883 * BitmapFactory.decodeStream}; all other content types will be ignored. The content provider
884 * URI used for this purpose must require no permissions to read the image data.
885 */
886 public static final String EXTRA_BACKGROUND_IMAGE_URI = "android.backgroundImageUri";
887
888 /**
Dan Sandler842dd772014-05-15 09:36:47 -0400889 * {@link #extras} key: A
Jeff Browndba34ba2014-06-24 20:46:03 -0700890 * {@link android.media.session.MediaSession.Token} associated with a
Dan Sandler842dd772014-05-15 09:36:47 -0400891 * {@link android.app.Notification.MediaStyle} notification.
892 */
893 public static final String EXTRA_MEDIA_SESSION = "android.mediaSession";
894
895 /**
Bryan Mawhinneye191f902014-07-22 12:50:09 +0100896 * {@link #extras} key: the indices of actions to be shown in the compact view,
897 * as supplied to (e.g.) {@link MediaStyle#setShowActionsInCompactView(int...)}.
898 */
899 public static final String EXTRA_COMPACT_ACTIONS = "android.compactActions";
900
Christoph Studer943aa672014-08-03 20:31:16 +0200901 /**
Alex Hillsfc737de2016-03-23 17:33:02 -0400902 * {@link #extras} key: the username to be displayed for all messages sent by the user including
903 * direct replies
904 * {@link android.app.Notification.MessagingStyle} notification.
905 */
906 public static final String EXTRA_SELF_DISPLAY_NAME = "android.selfDisplayName";
907
908 /**
909 * {@link #extras} key: a boolean describing whether the platform should automatically
910 * generate possible replies to
911 * {@link android.app.Notification.MessagingStyle.Message} objects provided by a
912 * {@link android.app.Notification.MessagingStyle} notification.
913 */
914 public static final String EXTRA_ALLOW_GENERATED_REPLIES = "android.allowGeneratedReplies";
915
916 /**
917 * {@link #extras} key: a {@link String} to be displayed as the title to a thread represented by
918 * a {@link android.app.Notification.MessagingStyle}
919 */
920 public static final String EXTRA_THREAD_TITLE = "android.threadTitle";
921
922 /**
923 * {@link #extras} key: an array of {@link android.app.Notification.MessagingStyle.Message}
924 * bundles provided by a
925 * {@link android.app.Notification.MessagingStyle} notification.
926 */
927 public static final String EXTRA_MESSAGES = "android.messages";
928
929 /**
Kenny Guy8942bcd2014-09-08 21:09:47 +0100930 * {@link #extras} key: the user that built the notification.
931 *
932 * @hide
933 */
934 public static final String EXTRA_ORIGINATING_USERID = "android.originatingUserId";
935
936 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400937 * @hide
938 */
939 public static final String EXTRA_BUILDER_APPLICATION_INFO = "android.appInfo";
940
Selim Cinek247fa012016-02-18 09:50:48 -0800941 /**
942 * @hide
943 */
944 public static final String EXTRA_CONTAINS_CUSTOM_VIEW = "android.contains.customView";
945
Dan Sandlerd63f9322015-05-06 15:18:49 -0400946 private Icon mSmallIcon;
947 private Icon mLargeIcon;
948
Chris Wren51c75102013-07-16 20:49:17 -0400949 /**
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400950 * Structure to encapsulate a named action that can be shown as part of this notification.
951 * It must include an icon, a label, and a {@link PendingIntent} to be fired when the action is
952 * selected by the user.
953 * <p>
Griff Hazen959591e2014-05-15 22:26:18 -0700954 * Apps should use {@link Notification.Builder#addAction(int, CharSequence, PendingIntent)}
955 * or {@link Notification.Builder#addAction(Notification.Action)}
956 * to attach actions.
Daniel Sandlera0a938c2012-03-15 08:42:37 -0400957 */
Daniel Sandlerea2a3172013-02-20 22:24:20 -0500958 public static class Action implements Parcelable {
Griff Hazen959591e2014-05-15 22:26:18 -0700959 private final Bundle mExtras;
Dan Sandler86647982015-05-13 23:41:13 -0400960 private Icon mIcon;
Griff Hazen61a9e862014-05-22 16:05:19 -0700961 private final RemoteInput[] mRemoteInputs;
Griff Hazen959591e2014-05-15 22:26:18 -0700962
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400963 /**
964 * Small icon representing the action.
Dan Sandler86647982015-05-13 23:41:13 -0400965 *
966 * @deprecated Use {@link Action#getIcon()} instead.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400967 */
Dan Sandler86647982015-05-13 23:41:13 -0400968 @Deprecated
Daniel Sandlera0a938c2012-03-15 08:42:37 -0400969 public int icon;
Griff Hazen5cadc3b2014-05-20 09:55:39 -0700970
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400971 /**
972 * Title of the action.
973 */
Daniel Sandlera0a938c2012-03-15 08:42:37 -0400974 public CharSequence title;
Griff Hazen5cadc3b2014-05-20 09:55:39 -0700975
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400976 /**
977 * Intent to send when the user invokes this action. May be null, in which case the action
978 * may be rendered in a disabled presentation by the system UI.
979 */
Daniel Sandlera0a938c2012-03-15 08:42:37 -0400980 public PendingIntent actionIntent;
Griff Hazen959591e2014-05-15 22:26:18 -0700981
Daniel Sandlera0a938c2012-03-15 08:42:37 -0400982 private Action(Parcel in) {
Dan Sandler86647982015-05-13 23:41:13 -0400983 if (in.readInt() != 0) {
984 mIcon = Icon.CREATOR.createFromParcel(in);
Dan Sandler68079d52015-07-22 10:45:30 -0400985 if (mIcon.getType() == Icon.TYPE_RESOURCE) {
986 icon = mIcon.getResId();
987 }
Dan Sandler86647982015-05-13 23:41:13 -0400988 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -0400989 title = TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(in);
990 if (in.readInt() == 1) {
991 actionIntent = PendingIntent.CREATOR.createFromParcel(in);
992 }
Jeff Sharkeya04c7a72016-03-18 12:20:36 -0600993 mExtras = Bundle.setDefusable(in.readBundle(), true);
Griff Hazen5cadc3b2014-05-20 09:55:39 -0700994 mRemoteInputs = in.createTypedArray(RemoteInput.CREATOR);
Daniel Sandlera0a938c2012-03-15 08:42:37 -0400995 }
Griff Hazen5cadc3b2014-05-20 09:55:39 -0700996
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400997 /**
Dan Sandler86647982015-05-13 23:41:13 -0400998 * @deprecated Use {@link android.app.Notification.Action.Builder}.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400999 */
Dan Sandler86647982015-05-13 23:41:13 -04001000 @Deprecated
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001001 public Action(int icon, CharSequence title, PendingIntent intent) {
Dan Sandler86647982015-05-13 23:41:13 -04001002 this(Icon.createWithResource("", icon), title, intent, new Bundle(), null);
Griff Hazen959591e2014-05-15 22:26:18 -07001003 }
1004
Dan Sandler86647982015-05-13 23:41:13 -04001005 private Action(Icon icon, CharSequence title, PendingIntent intent, Bundle extras,
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001006 RemoteInput[] remoteInputs) {
Dan Sandler86647982015-05-13 23:41:13 -04001007 this.mIcon = icon;
Gus Prevasf5bff46f2015-08-24 10:34:28 -04001008 if (icon != null && icon.getType() == Icon.TYPE_RESOURCE) {
1009 this.icon = icon.getResId();
1010 }
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001011 this.title = title;
1012 this.actionIntent = intent;
Griff Hazen959591e2014-05-15 22:26:18 -07001013 this.mExtras = extras != null ? extras : new Bundle();
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001014 this.mRemoteInputs = remoteInputs;
Griff Hazen959591e2014-05-15 22:26:18 -07001015 }
1016
1017 /**
Dan Sandler86647982015-05-13 23:41:13 -04001018 * Return an icon representing the action.
1019 */
1020 public Icon getIcon() {
1021 if (mIcon == null && icon != 0) {
1022 // you snuck an icon in here without using the builder; let's try to keep it
1023 mIcon = Icon.createWithResource("", icon);
1024 }
1025 return mIcon;
1026 }
1027
1028 /**
Griff Hazen959591e2014-05-15 22:26:18 -07001029 * Get additional metadata carried around with this Action.
1030 */
1031 public Bundle getExtras() {
1032 return mExtras;
1033 }
1034
1035 /**
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001036 * Get the list of inputs to be collected from the user when this action is sent.
1037 * May return null if no remote inputs were added.
1038 */
1039 public RemoteInput[] getRemoteInputs() {
1040 return mRemoteInputs;
1041 }
1042
1043 /**
Griff Hazen959591e2014-05-15 22:26:18 -07001044 * Builder class for {@link Action} objects.
1045 */
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001046 public static final class Builder {
Dan Sandler86647982015-05-13 23:41:13 -04001047 private final Icon mIcon;
Griff Hazen959591e2014-05-15 22:26:18 -07001048 private final CharSequence mTitle;
1049 private final PendingIntent mIntent;
1050 private final Bundle mExtras;
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001051 private ArrayList<RemoteInput> mRemoteInputs;
Griff Hazen959591e2014-05-15 22:26:18 -07001052
1053 /**
1054 * Construct a new builder for {@link Action} object.
1055 * @param icon icon to show for this action
1056 * @param title the title of the action
1057 * @param intent the {@link PendingIntent} to fire when users trigger this action
1058 */
Dan Sandler86647982015-05-13 23:41:13 -04001059 @Deprecated
Griff Hazen959591e2014-05-15 22:26:18 -07001060 public Builder(int icon, CharSequence title, PendingIntent intent) {
Dan Sandler86647982015-05-13 23:41:13 -04001061 this(Icon.createWithResource("", icon), title, intent, new Bundle(), null);
1062 }
1063
1064 /**
1065 * Construct a new builder for {@link Action} object.
1066 * @param icon icon to show for this action
1067 * @param title the title of the action
1068 * @param intent the {@link PendingIntent} to fire when users trigger this action
1069 */
1070 public Builder(Icon icon, CharSequence title, PendingIntent intent) {
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001071 this(icon, title, intent, new Bundle(), null);
Griff Hazen959591e2014-05-15 22:26:18 -07001072 }
1073
1074 /**
1075 * Construct a new builder for {@link Action} object using the fields from an
1076 * {@link Action}.
1077 * @param action the action to read fields from.
1078 */
1079 public Builder(Action action) {
Dan Sandler86647982015-05-13 23:41:13 -04001080 this(action.getIcon(), action.title, action.actionIntent, new Bundle(action.mExtras),
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001081 action.getRemoteInputs());
Griff Hazen959591e2014-05-15 22:26:18 -07001082 }
1083
Dan Sandler86647982015-05-13 23:41:13 -04001084 private Builder(Icon icon, CharSequence title, PendingIntent intent, Bundle extras,
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001085 RemoteInput[] remoteInputs) {
Griff Hazen959591e2014-05-15 22:26:18 -07001086 mIcon = icon;
1087 mTitle = title;
1088 mIntent = intent;
1089 mExtras = extras;
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001090 if (remoteInputs != null) {
1091 mRemoteInputs = new ArrayList<RemoteInput>(remoteInputs.length);
1092 Collections.addAll(mRemoteInputs, remoteInputs);
1093 }
Griff Hazen959591e2014-05-15 22:26:18 -07001094 }
1095
1096 /**
1097 * Merge additional metadata into this builder.
1098 *
1099 * <p>Values within the Bundle will replace existing extras values in this Builder.
1100 *
1101 * @see Notification.Action#extras
1102 */
1103 public Builder addExtras(Bundle extras) {
1104 if (extras != null) {
1105 mExtras.putAll(extras);
1106 }
1107 return this;
1108 }
1109
1110 /**
1111 * Get the metadata Bundle used by this Builder.
1112 *
1113 * <p>The returned Bundle is shared with this Builder.
1114 */
1115 public Bundle getExtras() {
1116 return mExtras;
1117 }
1118
1119 /**
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001120 * Add an input to be collected from the user when this action is sent.
1121 * Response values can be retrieved from the fired intent by using the
1122 * {@link RemoteInput#getResultsFromIntent} function.
1123 * @param remoteInput a {@link RemoteInput} to add to the action
1124 * @return this object for method chaining
1125 */
1126 public Builder addRemoteInput(RemoteInput remoteInput) {
1127 if (mRemoteInputs == null) {
1128 mRemoteInputs = new ArrayList<RemoteInput>();
1129 }
1130 mRemoteInputs.add(remoteInput);
1131 return this;
1132 }
1133
1134 /**
1135 * Apply an extender to this action builder. Extenders may be used to add
1136 * metadata or change options on this builder.
1137 */
Griff Hazen61a9e862014-05-22 16:05:19 -07001138 public Builder extend(Extender extender) {
1139 extender.extend(this);
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001140 return this;
1141 }
1142
1143 /**
Griff Hazen959591e2014-05-15 22:26:18 -07001144 * Combine all of the options that have been set and return a new {@link Action}
1145 * object.
1146 * @return the built action
1147 */
1148 public Action build() {
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001149 RemoteInput[] remoteInputs = mRemoteInputs != null
1150 ? mRemoteInputs.toArray(new RemoteInput[mRemoteInputs.size()]) : null;
1151 return new Action(mIcon, mTitle, mIntent, mExtras, remoteInputs);
Griff Hazen959591e2014-05-15 22:26:18 -07001152 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001153 }
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001154
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001155 @Override
1156 public Action clone() {
1157 return new Action(
Dan Sandler86647982015-05-13 23:41:13 -04001158 getIcon(),
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001159 title,
1160 actionIntent, // safe to alias
1161 new Bundle(mExtras),
1162 getRemoteInputs());
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001163 }
1164 @Override
1165 public int describeContents() {
1166 return 0;
1167 }
1168 @Override
1169 public void writeToParcel(Parcel out, int flags) {
Dan Sandler86647982015-05-13 23:41:13 -04001170 final Icon ic = getIcon();
1171 if (ic != null) {
1172 out.writeInt(1);
1173 ic.writeToParcel(out, 0);
1174 } else {
1175 out.writeInt(0);
1176 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001177 TextUtils.writeToParcel(title, out, flags);
1178 if (actionIntent != null) {
1179 out.writeInt(1);
1180 actionIntent.writeToParcel(out, flags);
1181 } else {
1182 out.writeInt(0);
1183 }
Griff Hazen959591e2014-05-15 22:26:18 -07001184 out.writeBundle(mExtras);
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001185 out.writeTypedArray(mRemoteInputs, flags);
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001186 }
Griff Hazen959591e2014-05-15 22:26:18 -07001187 public static final Parcelable.Creator<Action> CREATOR =
1188 new Parcelable.Creator<Action>() {
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001189 public Action createFromParcel(Parcel in) {
1190 return new Action(in);
1191 }
1192 public Action[] newArray(int size) {
1193 return new Action[size];
1194 }
1195 };
Griff Hazen61a9e862014-05-22 16:05:19 -07001196
1197 /**
1198 * Extender interface for use with {@link Builder#extend}. Extenders may be used to add
1199 * metadata or change options on an action builder.
1200 */
1201 public interface Extender {
1202 /**
1203 * Apply this extender to a notification action builder.
1204 * @param builder the builder to be modified.
1205 * @return the build object for chaining.
1206 */
1207 public Builder extend(Builder builder);
1208 }
1209
1210 /**
1211 * Wearable extender for notification actions. To add extensions to an action,
1212 * create a new {@link android.app.Notification.Action.WearableExtender} object using
1213 * the {@code WearableExtender()} constructor and apply it to a
1214 * {@link android.app.Notification.Action.Builder} using
1215 * {@link android.app.Notification.Action.Builder#extend}.
1216 *
1217 * <pre class="prettyprint">
1218 * Notification.Action action = new Notification.Action.Builder(
1219 * R.drawable.archive_all, "Archive all", actionIntent)
Griff Hazen14f57992014-05-26 09:07:14 -07001220 * .extend(new Notification.Action.WearableExtender()
Griff Hazen61a9e862014-05-22 16:05:19 -07001221 * .setAvailableOffline(false))
Griff Hazen14f57992014-05-26 09:07:14 -07001222 * .build();</pre>
Griff Hazen61a9e862014-05-22 16:05:19 -07001223 */
1224 public static final class WearableExtender implements Extender {
1225 /** Notification action extra which contains wearable extensions */
1226 private static final String EXTRA_WEARABLE_EXTENSIONS = "android.wearable.EXTENSIONS";
1227
Pete Gastaf6781d2014-10-07 15:17:05 -04001228 // Keys within EXTRA_WEARABLE_EXTENSIONS for wearable options.
Griff Hazen61a9e862014-05-22 16:05:19 -07001229 private static final String KEY_FLAGS = "flags";
Pete Gastaf6781d2014-10-07 15:17:05 -04001230 private static final String KEY_IN_PROGRESS_LABEL = "inProgressLabel";
1231 private static final String KEY_CONFIRM_LABEL = "confirmLabel";
1232 private static final String KEY_CANCEL_LABEL = "cancelLabel";
Griff Hazen61a9e862014-05-22 16:05:19 -07001233
1234 // Flags bitwise-ored to mFlags
1235 private static final int FLAG_AVAILABLE_OFFLINE = 0x1;
1236
1237 // Default value for flags integer
1238 private static final int DEFAULT_FLAGS = FLAG_AVAILABLE_OFFLINE;
1239
1240 private int mFlags = DEFAULT_FLAGS;
1241
Pete Gastaf6781d2014-10-07 15:17:05 -04001242 private CharSequence mInProgressLabel;
1243 private CharSequence mConfirmLabel;
1244 private CharSequence mCancelLabel;
1245
Griff Hazen61a9e862014-05-22 16:05:19 -07001246 /**
1247 * Create a {@link android.app.Notification.Action.WearableExtender} with default
1248 * options.
1249 */
1250 public WearableExtender() {
1251 }
1252
1253 /**
1254 * Create a {@link android.app.Notification.Action.WearableExtender} by reading
1255 * wearable options present in an existing notification action.
1256 * @param action the notification action to inspect.
1257 */
1258 public WearableExtender(Action action) {
1259 Bundle wearableBundle = action.getExtras().getBundle(EXTRA_WEARABLE_EXTENSIONS);
1260 if (wearableBundle != null) {
1261 mFlags = wearableBundle.getInt(KEY_FLAGS, DEFAULT_FLAGS);
Pete Gastaf6781d2014-10-07 15:17:05 -04001262 mInProgressLabel = wearableBundle.getCharSequence(KEY_IN_PROGRESS_LABEL);
1263 mConfirmLabel = wearableBundle.getCharSequence(KEY_CONFIRM_LABEL);
1264 mCancelLabel = wearableBundle.getCharSequence(KEY_CANCEL_LABEL);
Griff Hazen61a9e862014-05-22 16:05:19 -07001265 }
1266 }
1267
1268 /**
1269 * Apply wearable extensions to a notification action that is being built. This is
1270 * typically called by the {@link android.app.Notification.Action.Builder#extend}
1271 * method of {@link android.app.Notification.Action.Builder}.
1272 */
1273 @Override
1274 public Action.Builder extend(Action.Builder builder) {
1275 Bundle wearableBundle = new Bundle();
1276
1277 if (mFlags != DEFAULT_FLAGS) {
1278 wearableBundle.putInt(KEY_FLAGS, mFlags);
1279 }
Pete Gastaf6781d2014-10-07 15:17:05 -04001280 if (mInProgressLabel != null) {
1281 wearableBundle.putCharSequence(KEY_IN_PROGRESS_LABEL, mInProgressLabel);
1282 }
1283 if (mConfirmLabel != null) {
1284 wearableBundle.putCharSequence(KEY_CONFIRM_LABEL, mConfirmLabel);
1285 }
1286 if (mCancelLabel != null) {
1287 wearableBundle.putCharSequence(KEY_CANCEL_LABEL, mCancelLabel);
1288 }
Griff Hazen61a9e862014-05-22 16:05:19 -07001289
1290 builder.getExtras().putBundle(EXTRA_WEARABLE_EXTENSIONS, wearableBundle);
1291 return builder;
1292 }
1293
1294 @Override
1295 public WearableExtender clone() {
1296 WearableExtender that = new WearableExtender();
1297 that.mFlags = this.mFlags;
Pete Gastaf6781d2014-10-07 15:17:05 -04001298 that.mInProgressLabel = this.mInProgressLabel;
1299 that.mConfirmLabel = this.mConfirmLabel;
1300 that.mCancelLabel = this.mCancelLabel;
Griff Hazen61a9e862014-05-22 16:05:19 -07001301 return that;
1302 }
1303
1304 /**
1305 * Set whether this action is available when the wearable device is not connected to
1306 * a companion device. The user can still trigger this action when the wearable device is
1307 * offline, but a visual hint will indicate that the action may not be available.
1308 * Defaults to true.
1309 */
1310 public WearableExtender setAvailableOffline(boolean availableOffline) {
1311 setFlag(FLAG_AVAILABLE_OFFLINE, availableOffline);
1312 return this;
1313 }
1314
1315 /**
1316 * Get whether this action is available when the wearable device is not connected to
1317 * a companion device. The user can still trigger this action when the wearable device is
1318 * offline, but a visual hint will indicate that the action may not be available.
1319 * Defaults to true.
1320 */
1321 public boolean isAvailableOffline() {
1322 return (mFlags & FLAG_AVAILABLE_OFFLINE) != 0;
1323 }
1324
1325 private void setFlag(int mask, boolean value) {
1326 if (value) {
1327 mFlags |= mask;
1328 } else {
1329 mFlags &= ~mask;
1330 }
1331 }
Pete Gastaf6781d2014-10-07 15:17:05 -04001332
1333 /**
1334 * Set a label to display while the wearable is preparing to automatically execute the
1335 * action. This is usually a 'ing' verb ending in ellipsis like "Sending..."
1336 *
1337 * @param label the label to display while the action is being prepared to execute
1338 * @return this object for method chaining
1339 */
1340 public WearableExtender setInProgressLabel(CharSequence label) {
1341 mInProgressLabel = label;
1342 return this;
1343 }
1344
1345 /**
1346 * Get the label to display while the wearable is preparing to automatically execute
1347 * the action. This is usually a 'ing' verb ending in ellipsis like "Sending..."
1348 *
1349 * @return the label to display while the action is being prepared to execute
1350 */
1351 public CharSequence getInProgressLabel() {
1352 return mInProgressLabel;
1353 }
1354
1355 /**
1356 * Set a label to display to confirm that the action should be executed.
1357 * This is usually an imperative verb like "Send".
1358 *
1359 * @param label the label to confirm the action should be executed
1360 * @return this object for method chaining
1361 */
1362 public WearableExtender setConfirmLabel(CharSequence label) {
1363 mConfirmLabel = label;
1364 return this;
1365 }
1366
1367 /**
1368 * Get the label to display to confirm that the action should be executed.
1369 * This is usually an imperative verb like "Send".
1370 *
1371 * @return the label to confirm the action should be executed
1372 */
1373 public CharSequence getConfirmLabel() {
1374 return mConfirmLabel;
1375 }
1376
1377 /**
1378 * Set a label to display to cancel the action.
1379 * This is usually an imperative verb, like "Cancel".
1380 *
1381 * @param label the label to display to cancel the action
1382 * @return this object for method chaining
1383 */
1384 public WearableExtender setCancelLabel(CharSequence label) {
1385 mCancelLabel = label;
1386 return this;
1387 }
1388
1389 /**
1390 * Get the label to display to cancel the action.
1391 * This is usually an imperative verb like "Cancel".
1392 *
1393 * @return the label to display to cancel the action
1394 */
1395 public CharSequence getCancelLabel() {
1396 return mCancelLabel;
1397 }
Griff Hazen61a9e862014-05-22 16:05:19 -07001398 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001399 }
1400
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001401 /**
1402 * Array of all {@link Action} structures attached to this notification by
1403 * {@link Builder#addAction(int, CharSequence, PendingIntent)}. Mostly useful for instances of
1404 * {@link android.service.notification.NotificationListenerService} that provide an alternative
1405 * interface for invoking actions.
1406 */
Daniel Sandlerea2a3172013-02-20 22:24:20 -05001407 public Action[] actions;
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001408
1409 /**
Dan Sandler0bf2ed82013-12-21 23:33:41 -06001410 * Replacement version of this notification whose content will be shown
1411 * in an insecure context such as atop a secure keyguard. See {@link #visibility}
1412 * and {@link #VISIBILITY_PUBLIC}.
1413 */
1414 public Notification publicVersion;
1415
1416 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001417 * Constructs a Notification object with default values.
Joe Onorato46439ce2010-11-19 13:56:21 -08001418 * You might want to consider using {@link Builder} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001419 */
1420 public Notification()
1421 {
1422 this.when = System.currentTimeMillis();
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001423 this.priority = PRIORITY_DEFAULT;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001424 }
1425
1426 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001427 * @hide
1428 */
1429 public Notification(Context context, int icon, CharSequence tickerText, long when,
1430 CharSequence contentTitle, CharSequence contentText, Intent contentIntent)
1431 {
Chris Wren1ce4b6d2015-06-11 10:19:43 -04001432 new Builder(context)
1433 .setWhen(when)
1434 .setSmallIcon(icon)
1435 .setTicker(tickerText)
1436 .setContentTitle(contentTitle)
1437 .setContentText(contentText)
1438 .setContentIntent(PendingIntent.getActivity(context, 0, contentIntent, 0))
1439 .buildInto(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001440 }
1441
1442 /**
1443 * Constructs a Notification object with the information needed to
1444 * have a status bar icon without the standard expanded view.
1445 *
1446 * @param icon The resource id of the icon to put in the status bar.
1447 * @param tickerText The text that flows by in the status bar when the notification first
1448 * activates.
1449 * @param when The time to show in the time field. In the System.currentTimeMillis
1450 * timebase.
Joe Onorato46439ce2010-11-19 13:56:21 -08001451 *
1452 * @deprecated Use {@link Builder} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001453 */
Joe Onorato46439ce2010-11-19 13:56:21 -08001454 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001455 public Notification(int icon, CharSequence tickerText, long when)
1456 {
1457 this.icon = icon;
1458 this.tickerText = tickerText;
1459 this.when = when;
1460 }
1461
1462 /**
1463 * Unflatten the notification from a parcel.
1464 */
1465 public Notification(Parcel parcel)
1466 {
1467 int version = parcel.readInt();
1468
1469 when = parcel.readLong();
Dan Sandler3936e7a2015-05-19 20:59:12 -04001470 if (parcel.readInt() != 0) {
1471 mSmallIcon = Icon.CREATOR.createFromParcel(parcel);
Dan Sandler4e787062015-06-17 15:09:48 -04001472 if (mSmallIcon.getType() == Icon.TYPE_RESOURCE) {
1473 icon = mSmallIcon.getResId();
1474 }
Dan Sandler3936e7a2015-05-19 20:59:12 -04001475 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001476 number = parcel.readInt();
1477 if (parcel.readInt() != 0) {
1478 contentIntent = PendingIntent.CREATOR.createFromParcel(parcel);
1479 }
1480 if (parcel.readInt() != 0) {
1481 deleteIntent = PendingIntent.CREATOR.createFromParcel(parcel);
1482 }
1483 if (parcel.readInt() != 0) {
1484 tickerText = TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(parcel);
1485 }
1486 if (parcel.readInt() != 0) {
Joe Onorato46439ce2010-11-19 13:56:21 -08001487 tickerView = RemoteViews.CREATOR.createFromParcel(parcel);
Joe Onoratoef1e7762010-09-17 18:38:38 -04001488 }
1489 if (parcel.readInt() != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001490 contentView = RemoteViews.CREATOR.createFromParcel(parcel);
1491 }
Joe Onorato561d3852010-11-20 18:09:34 -08001492 if (parcel.readInt() != 0) {
Dan Sandlerd63f9322015-05-06 15:18:49 -04001493 mLargeIcon = Icon.CREATOR.createFromParcel(parcel);
Joe Onorato561d3852010-11-20 18:09:34 -08001494 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001495 defaults = parcel.readInt();
1496 flags = parcel.readInt();
1497 if (parcel.readInt() != 0) {
1498 sound = Uri.CREATOR.createFromParcel(parcel);
1499 }
1500
1501 audioStreamType = parcel.readInt();
John Spurlockc0650f022014-07-19 13:22:39 -04001502 if (parcel.readInt() != 0) {
1503 audioAttributes = AudioAttributes.CREATOR.createFromParcel(parcel);
1504 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001505 vibrate = parcel.createLongArray();
1506 ledARGB = parcel.readInt();
1507 ledOnMS = parcel.readInt();
1508 ledOffMS = parcel.readInt();
1509 iconLevel = parcel.readInt();
Daniel Sandlere46cbd32010-06-17 10:35:26 -04001510
1511 if (parcel.readInt() != 0) {
1512 fullScreenIntent = PendingIntent.CREATOR.createFromParcel(parcel);
1513 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001514
1515 priority = parcel.readInt();
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001516
John Spurlockfd7f1e02014-03-18 16:41:57 -04001517 category = parcel.readString();
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001518
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001519 mGroupKey = parcel.readString();
1520
1521 mSortKey = parcel.readString();
1522
Jeff Sharkeya04c7a72016-03-18 12:20:36 -06001523 extras = Bundle.setDefusable(parcel.readBundle(), true); // may be null
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001524
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001525 actions = parcel.createTypedArray(Action.CREATOR); // may be null
1526
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001527 if (parcel.readInt() != 0) {
Daniel Sandlerf3b73432012-03-27 15:01:25 -04001528 bigContentView = RemoteViews.CREATOR.createFromParcel(parcel);
1529 }
Dan Sandler0bf2ed82013-12-21 23:33:41 -06001530
Chris Wren8fd39ec2014-02-27 17:43:26 -05001531 if (parcel.readInt() != 0) {
1532 headsUpContentView = RemoteViews.CREATOR.createFromParcel(parcel);
1533 }
1534
Dan Sandler0bf2ed82013-12-21 23:33:41 -06001535 visibility = parcel.readInt();
1536
1537 if (parcel.readInt() != 0) {
1538 publicVersion = Notification.CREATOR.createFromParcel(parcel);
1539 }
Dan Sandler26e81cf2014-05-06 10:01:27 -04001540
1541 color = parcel.readInt();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001542 }
1543
Andy Stadler110988c2010-12-03 14:29:16 -08001544 @Override
Joe Onorato18e69df2010-05-17 22:26:12 -07001545 public Notification clone() {
1546 Notification that = new Notification();
Daniel Sandler1a497d32013-04-18 14:52:45 -04001547 cloneInto(that, true);
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001548 return that;
1549 }
Joe Onorato18e69df2010-05-17 22:26:12 -07001550
Daniel Sandler1a497d32013-04-18 14:52:45 -04001551 /**
1552 * Copy all (or if heavy is false, all except Bitmaps and RemoteViews) members
1553 * of this into that.
1554 * @hide
1555 */
1556 public void cloneInto(Notification that, boolean heavy) {
Joe Onorato18e69df2010-05-17 22:26:12 -07001557 that.when = this.when;
Dan Sandlerd63f9322015-05-06 15:18:49 -04001558 that.mSmallIcon = this.mSmallIcon;
Joe Onorato18e69df2010-05-17 22:26:12 -07001559 that.number = this.number;
1560
1561 // PendingIntents are global, so there's no reason (or way) to clone them.
1562 that.contentIntent = this.contentIntent;
1563 that.deleteIntent = this.deleteIntent;
Daniel Sandlere46cbd32010-06-17 10:35:26 -04001564 that.fullScreenIntent = this.fullScreenIntent;
Joe Onorato18e69df2010-05-17 22:26:12 -07001565
1566 if (this.tickerText != null) {
1567 that.tickerText = this.tickerText.toString();
1568 }
Daniel Sandler1a497d32013-04-18 14:52:45 -04001569 if (heavy && this.tickerView != null) {
Joe Onorato46439ce2010-11-19 13:56:21 -08001570 that.tickerView = this.tickerView.clone();
Joe Onoratoef1e7762010-09-17 18:38:38 -04001571 }
Daniel Sandler1a497d32013-04-18 14:52:45 -04001572 if (heavy && this.contentView != null) {
Joe Onorato18e69df2010-05-17 22:26:12 -07001573 that.contentView = this.contentView.clone();
1574 }
Dan Sandlerd63f9322015-05-06 15:18:49 -04001575 if (heavy && this.mLargeIcon != null) {
1576 that.mLargeIcon = this.mLargeIcon;
Joe Onorato561d3852010-11-20 18:09:34 -08001577 }
Jozef BABJAKa8b91832011-02-22 08:05:08 +01001578 that.iconLevel = this.iconLevel;
Joe Onorato18e69df2010-05-17 22:26:12 -07001579 that.sound = this.sound; // android.net.Uri is immutable
1580 that.audioStreamType = this.audioStreamType;
John Spurlockc0650f022014-07-19 13:22:39 -04001581 if (this.audioAttributes != null) {
1582 that.audioAttributes = new AudioAttributes.Builder(this.audioAttributes).build();
1583 }
Joe Onorato18e69df2010-05-17 22:26:12 -07001584
1585 final long[] vibrate = this.vibrate;
1586 if (vibrate != null) {
1587 final int N = vibrate.length;
1588 final long[] vib = that.vibrate = new long[N];
1589 System.arraycopy(vibrate, 0, vib, 0, N);
1590 }
1591
1592 that.ledARGB = this.ledARGB;
1593 that.ledOnMS = this.ledOnMS;
1594 that.ledOffMS = this.ledOffMS;
1595 that.defaults = this.defaults;
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001596
Joe Onorato18e69df2010-05-17 22:26:12 -07001597 that.flags = this.flags;
1598
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001599 that.priority = this.priority;
Joe Malin8d40d042012-11-05 11:36:40 -08001600
John Spurlockfd7f1e02014-03-18 16:41:57 -04001601 that.category = this.category;
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001602
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001603 that.mGroupKey = this.mGroupKey;
1604
1605 that.mSortKey = this.mSortKey;
1606
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001607 if (this.extras != null) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04001608 try {
1609 that.extras = new Bundle(this.extras);
1610 // will unparcel
1611 that.extras.size();
1612 } catch (BadParcelableException e) {
1613 Log.e(TAG, "could not unparcel extras from notification: " + this, e);
1614 that.extras = null;
1615 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001616 }
1617
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001618 if (this.actions != null) {
1619 that.actions = new Action[this.actions.length];
1620 for(int i=0; i<this.actions.length; i++) {
1621 that.actions[i] = this.actions[i].clone();
1622 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001623 }
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001624
Daniel Sandler1a497d32013-04-18 14:52:45 -04001625 if (heavy && this.bigContentView != null) {
Daniel Sandlerf3b73432012-03-27 15:01:25 -04001626 that.bigContentView = this.bigContentView.clone();
1627 }
Daniel Sandler1a497d32013-04-18 14:52:45 -04001628
Chris Wren8fd39ec2014-02-27 17:43:26 -05001629 if (heavy && this.headsUpContentView != null) {
1630 that.headsUpContentView = this.headsUpContentView.clone();
1631 }
1632
Dan Sandler0bf2ed82013-12-21 23:33:41 -06001633 that.visibility = this.visibility;
1634
1635 if (this.publicVersion != null) {
1636 that.publicVersion = new Notification();
1637 this.publicVersion.cloneInto(that.publicVersion, heavy);
1638 }
1639
Dan Sandler26e81cf2014-05-06 10:01:27 -04001640 that.color = this.color;
1641
Daniel Sandler1a497d32013-04-18 14:52:45 -04001642 if (!heavy) {
1643 that.lightenPayload(); // will clean out extras
1644 }
1645 }
1646
1647 /**
1648 * Removes heavyweight parts of the Notification object for archival or for sending to
1649 * listeners when the full contents are not necessary.
1650 * @hide
1651 */
1652 public final void lightenPayload() {
1653 tickerView = null;
1654 contentView = null;
1655 bigContentView = null;
Chris Wren8fd39ec2014-02-27 17:43:26 -05001656 headsUpContentView = null;
Dan Sandlerd63f9322015-05-06 15:18:49 -04001657 mLargeIcon = null;
Dan Sandler50128532015-12-08 15:42:41 -05001658 if (extras != null && !extras.isEmpty()) {
1659 final Set<String> keyset = extras.keySet();
1660 final int N = keyset.size();
1661 final String[] keys = keyset.toArray(new String[N]);
1662 for (int i=0; i<N; i++) {
1663 final String key = keys[i];
1664 final Object obj = extras.get(key);
1665 if (obj != null &&
1666 ( obj instanceof Parcelable
1667 || obj instanceof Parcelable[]
1668 || obj instanceof SparseArray
1669 || obj instanceof ArrayList)) {
1670 extras.remove(key);
1671 }
1672 }
Daniel Sandler1a497d32013-04-18 14:52:45 -04001673 }
Joe Onorato18e69df2010-05-17 22:26:12 -07001674 }
1675
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04001676 /**
1677 * Make sure this CharSequence is safe to put into a bundle, which basically
1678 * means it had better not be some custom Parcelable implementation.
1679 * @hide
1680 */
1681 public static CharSequence safeCharSequence(CharSequence cs) {
Christoph Studer535ec612014-09-03 15:47:47 +02001682 if (cs == null) return cs;
1683 if (cs.length() > MAX_CHARSEQUENCE_LENGTH) {
1684 cs = cs.subSequence(0, MAX_CHARSEQUENCE_LENGTH);
1685 }
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04001686 if (cs instanceof Parcelable) {
1687 Log.e(TAG, "warning: " + cs.getClass().getCanonicalName()
1688 + " instance is a custom Parcelable and not allowed in Notification");
1689 return cs.toString();
1690 }
Selim Cinek60a54252016-02-26 17:03:25 -08001691 return removeTextSizeSpans(cs);
1692 }
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04001693
Selim Cinek60a54252016-02-26 17:03:25 -08001694 private static CharSequence removeTextSizeSpans(CharSequence charSequence) {
1695 if (charSequence instanceof Spanned) {
1696 Spanned ss = (Spanned) charSequence;
1697 Object[] spans = ss.getSpans(0, ss.length(), Object.class);
1698 SpannableStringBuilder builder = new SpannableStringBuilder(ss.toString());
1699 for (Object span : spans) {
1700 Object resultSpan = span;
Selim Cinek89991a22016-03-07 19:51:54 -08001701 if (resultSpan instanceof CharacterStyle) {
1702 resultSpan = ((CharacterStyle) span).getUnderlying();
1703 }
1704 if (resultSpan instanceof TextAppearanceSpan) {
1705 TextAppearanceSpan originalSpan = (TextAppearanceSpan) resultSpan;
Selim Cinek60a54252016-02-26 17:03:25 -08001706 resultSpan = new TextAppearanceSpan(
1707 originalSpan.getFamily(),
1708 originalSpan.getTextStyle(),
1709 -1,
1710 originalSpan.getTextColor(),
1711 originalSpan.getLinkTextColor());
Selim Cinek89991a22016-03-07 19:51:54 -08001712 } else if (resultSpan instanceof RelativeSizeSpan
1713 || resultSpan instanceof AbsoluteSizeSpan) {
Selim Cinek60a54252016-02-26 17:03:25 -08001714 continue;
Selim Cinek89991a22016-03-07 19:51:54 -08001715 } else {
1716 resultSpan = span;
Selim Cinek60a54252016-02-26 17:03:25 -08001717 }
1718 builder.setSpan(resultSpan, ss.getSpanStart(span), ss.getSpanEnd(span),
1719 ss.getSpanFlags(span));
1720 }
1721 return builder;
1722 }
1723 return charSequence;
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04001724 }
1725
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001726 public int describeContents() {
1727 return 0;
1728 }
1729
1730 /**
Dan Sandler4e787062015-06-17 15:09:48 -04001731 * Flatten this notification into a parcel.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001732 */
1733 public void writeToParcel(Parcel parcel, int flags)
1734 {
1735 parcel.writeInt(1);
1736
1737 parcel.writeLong(when);
Dan Sandler4e787062015-06-17 15:09:48 -04001738 if (mSmallIcon == null && icon != 0) {
1739 // you snuck an icon in here without using the builder; let's try to keep it
1740 mSmallIcon = Icon.createWithResource("", icon);
1741 }
Dan Sandler3936e7a2015-05-19 20:59:12 -04001742 if (mSmallIcon != null) {
1743 parcel.writeInt(1);
1744 mSmallIcon.writeToParcel(parcel, 0);
1745 } else {
1746 parcel.writeInt(0);
1747 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001748 parcel.writeInt(number);
1749 if (contentIntent != null) {
1750 parcel.writeInt(1);
1751 contentIntent.writeToParcel(parcel, 0);
1752 } else {
1753 parcel.writeInt(0);
1754 }
1755 if (deleteIntent != null) {
1756 parcel.writeInt(1);
1757 deleteIntent.writeToParcel(parcel, 0);
1758 } else {
1759 parcel.writeInt(0);
1760 }
1761 if (tickerText != null) {
1762 parcel.writeInt(1);
1763 TextUtils.writeToParcel(tickerText, parcel, flags);
1764 } else {
1765 parcel.writeInt(0);
1766 }
Joe Onorato46439ce2010-11-19 13:56:21 -08001767 if (tickerView != null) {
Joe Onoratoef1e7762010-09-17 18:38:38 -04001768 parcel.writeInt(1);
Joe Onorato46439ce2010-11-19 13:56:21 -08001769 tickerView.writeToParcel(parcel, 0);
Joe Onoratoef1e7762010-09-17 18:38:38 -04001770 } else {
1771 parcel.writeInt(0);
1772 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001773 if (contentView != null) {
1774 parcel.writeInt(1);
1775 contentView.writeToParcel(parcel, 0);
1776 } else {
1777 parcel.writeInt(0);
1778 }
Dan Sandlerd63f9322015-05-06 15:18:49 -04001779 if (mLargeIcon != null) {
Joe Onorato561d3852010-11-20 18:09:34 -08001780 parcel.writeInt(1);
Dan Sandlerd63f9322015-05-06 15:18:49 -04001781 mLargeIcon.writeToParcel(parcel, 0);
Joe Onorato561d3852010-11-20 18:09:34 -08001782 } else {
1783 parcel.writeInt(0);
1784 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001785
1786 parcel.writeInt(defaults);
1787 parcel.writeInt(this.flags);
1788
1789 if (sound != null) {
1790 parcel.writeInt(1);
1791 sound.writeToParcel(parcel, 0);
1792 } else {
1793 parcel.writeInt(0);
1794 }
1795 parcel.writeInt(audioStreamType);
John Spurlockc0650f022014-07-19 13:22:39 -04001796
1797 if (audioAttributes != null) {
1798 parcel.writeInt(1);
1799 audioAttributes.writeToParcel(parcel, 0);
1800 } else {
1801 parcel.writeInt(0);
1802 }
1803
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001804 parcel.writeLongArray(vibrate);
1805 parcel.writeInt(ledARGB);
1806 parcel.writeInt(ledOnMS);
1807 parcel.writeInt(ledOffMS);
1808 parcel.writeInt(iconLevel);
Daniel Sandlere46cbd32010-06-17 10:35:26 -04001809
1810 if (fullScreenIntent != null) {
1811 parcel.writeInt(1);
1812 fullScreenIntent.writeToParcel(parcel, 0);
1813 } else {
1814 parcel.writeInt(0);
1815 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001816
1817 parcel.writeInt(priority);
Joe Malin8d40d042012-11-05 11:36:40 -08001818
John Spurlockfd7f1e02014-03-18 16:41:57 -04001819 parcel.writeString(category);
Joe Malin8d40d042012-11-05 11:36:40 -08001820
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001821 parcel.writeString(mGroupKey);
1822
1823 parcel.writeString(mSortKey);
1824
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001825 parcel.writeBundle(extras); // null ok
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001826
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001827 parcel.writeTypedArray(actions, 0); // null ok
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001828
Daniel Sandlerf3b73432012-03-27 15:01:25 -04001829 if (bigContentView != null) {
1830 parcel.writeInt(1);
1831 bigContentView.writeToParcel(parcel, 0);
1832 } else {
1833 parcel.writeInt(0);
1834 }
Dan Sandler0bf2ed82013-12-21 23:33:41 -06001835
Chris Wren8fd39ec2014-02-27 17:43:26 -05001836 if (headsUpContentView != null) {
1837 parcel.writeInt(1);
1838 headsUpContentView.writeToParcel(parcel, 0);
1839 } else {
1840 parcel.writeInt(0);
1841 }
1842
Dan Sandler0bf2ed82013-12-21 23:33:41 -06001843 parcel.writeInt(visibility);
1844
1845 if (publicVersion != null) {
1846 parcel.writeInt(1);
1847 publicVersion.writeToParcel(parcel, 0);
1848 } else {
1849 parcel.writeInt(0);
1850 }
Dan Sandler26e81cf2014-05-06 10:01:27 -04001851
1852 parcel.writeInt(color);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001853 }
1854
1855 /**
1856 * Parcelable.Creator that instantiates Notification objects
1857 */
1858 public static final Parcelable.Creator<Notification> CREATOR
1859 = new Parcelable.Creator<Notification>()
1860 {
1861 public Notification createFromParcel(Parcel parcel)
1862 {
1863 return new Notification(parcel);
1864 }
1865
1866 public Notification[] newArray(int size)
1867 {
1868 return new Notification[size];
1869 }
1870 };
1871
1872 /**
1873 * Sets the {@link #contentView} field to be a view with the standard "Latest Event"
1874 * layout.
1875 *
1876 * <p>Uses the {@link #icon} and {@link #when} fields to set the icon and time fields
1877 * in the view.</p>
1878 * @param context The context for your application / activity.
1879 * @param contentTitle The title that goes in the expanded entry.
1880 * @param contentText The text that goes in the expanded entry.
1881 * @param contentIntent The intent to launch when the user clicks the expanded notification.
1882 * If this is an activity, it must include the
1883 * {@link android.content.Intent#FLAG_ACTIVITY_NEW_TASK} flag, which requires
Scott Main7aee61f2011-02-08 11:25:01 -08001884 * that you take care of task management as described in the
1885 * <a href="{@docRoot}guide/topics/fundamentals/tasks-and-back-stack.html">Tasks and Back
1886 * Stack</a> document.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001887 *
Joe Onorato46439ce2010-11-19 13:56:21 -08001888 * @deprecated Use {@link Builder} instead.
Chris Wrena05db382015-06-24 15:18:34 -04001889 * @removed
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001890 */
Joe Onorato46439ce2010-11-19 13:56:21 -08001891 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001892 public void setLatestEventInfo(Context context,
1893 CharSequence contentTitle, CharSequence contentText, PendingIntent contentIntent) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04001894 if (context.getApplicationInfo().targetSdkVersion > Build.VERSION_CODES.LOLLIPOP_MR1){
1895 Log.e(TAG, "setLatestEventInfo() is deprecated and you should feel deprecated.",
1896 new Throwable());
1897 }
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001898
Julia Reynoldsd9228f12015-10-20 10:37:27 -04001899 // ensure that any information already set directly is preserved
1900 final Notification.Builder builder = new Notification.Builder(context, this);
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001901
1902 // now apply the latestEventInfo fields
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001903 if (contentTitle != null) {
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001904 builder.setContentTitle(contentTitle);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001905 }
1906 if (contentText != null) {
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001907 builder.setContentText(contentText);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001908 }
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001909 builder.setContentIntent(contentIntent);
Julia Reynoldsd9228f12015-10-20 10:37:27 -04001910
1911 builder.build(); // callers expect this notification to be ready to use
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001912 }
1913
Julia Reynoldsda303542015-11-23 14:00:20 -05001914 /**
1915 * @hide
1916 */
1917 public static void addFieldsFromContext(Context context, Notification notification) {
1918 if (notification.extras.getParcelable(EXTRA_BUILDER_APPLICATION_INFO) == null) {
1919 notification.extras.putParcelable(EXTRA_BUILDER_APPLICATION_INFO,
1920 context.getApplicationInfo());
1921 }
1922 if (!notification.extras.containsKey(EXTRA_ORIGINATING_USERID)) {
1923 notification.extras.putInt(EXTRA_ORIGINATING_USERID, context.getUserId());
1924 }
1925 }
1926
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001927 @Override
1928 public String toString() {
1929 StringBuilder sb = new StringBuilder();
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001930 sb.append("Notification(pri=");
1931 sb.append(priority);
1932 sb.append(" contentView=");
Joe Onoratoc9596d62011-01-12 17:03:11 -08001933 if (contentView != null) {
1934 sb.append(contentView.getPackage());
1935 sb.append("/0x");
1936 sb.append(Integer.toHexString(contentView.getLayoutId()));
1937 } else {
1938 sb.append("null");
1939 }
1940 sb.append(" vibrate=");
Daniel Sandler6738eee2012-11-16 12:03:32 -05001941 if ((this.defaults & DEFAULT_VIBRATE) != 0) {
1942 sb.append("default");
1943 } else if (this.vibrate != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001944 int N = this.vibrate.length-1;
1945 sb.append("[");
1946 for (int i=0; i<N; i++) {
1947 sb.append(this.vibrate[i]);
1948 sb.append(',');
1949 }
Simon Schoar8cf97d92009-06-10 22:08:37 +02001950 if (N != -1) {
1951 sb.append(this.vibrate[N]);
1952 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001953 sb.append("]");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001954 } else {
1955 sb.append("null");
1956 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001957 sb.append(" sound=");
Daniel Sandler6738eee2012-11-16 12:03:32 -05001958 if ((this.defaults & DEFAULT_SOUND) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001959 sb.append("default");
Daniel Sandler6738eee2012-11-16 12:03:32 -05001960 } else if (this.sound != null) {
1961 sb.append(this.sound.toString());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001962 } else {
1963 sb.append("null");
1964 }
Chris Wren365b6d32015-07-16 10:39:26 -04001965 if (this.tickerText != null) {
1966 sb.append(" tick");
1967 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001968 sb.append(" defaults=0x");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001969 sb.append(Integer.toHexString(this.defaults));
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001970 sb.append(" flags=0x");
Daniel Sandlere46cbd32010-06-17 10:35:26 -04001971 sb.append(Integer.toHexString(this.flags));
Dan Sandler26e81cf2014-05-06 10:01:27 -04001972 sb.append(String.format(" color=0x%08x", this.color));
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001973 if (this.category != null) {
1974 sb.append(" category=");
1975 sb.append(this.category);
1976 }
1977 if (this.mGroupKey != null) {
1978 sb.append(" groupKey=");
1979 sb.append(this.mGroupKey);
1980 }
1981 if (this.mSortKey != null) {
1982 sb.append(" sortKey=");
1983 sb.append(this.mSortKey);
1984 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001985 if (actions != null) {
Dan Sandler1b718782014-07-18 12:43:45 -04001986 sb.append(" actions=");
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001987 sb.append(actions.length);
Dan Sandler1b718782014-07-18 12:43:45 -04001988 }
1989 sb.append(" vis=");
1990 sb.append(visibilityToString(this.visibility));
1991 if (this.publicVersion != null) {
1992 sb.append(" publicVersion=");
1993 sb.append(publicVersion.toString());
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001994 }
1995 sb.append(")");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001996 return sb.toString();
1997 }
Joe Onorato46439ce2010-11-19 13:56:21 -08001998
Dan Sandler1b718782014-07-18 12:43:45 -04001999 /**
2000 * {@hide}
2001 */
2002 public static String visibilityToString(int vis) {
2003 switch (vis) {
2004 case VISIBILITY_PRIVATE:
2005 return "PRIVATE";
2006 case VISIBILITY_PUBLIC:
2007 return "PUBLIC";
2008 case VISIBILITY_SECRET:
2009 return "SECRET";
2010 default:
2011 return "UNKNOWN(" + String.valueOf(vis) + ")";
2012 }
2013 }
2014
Joe Onoratocb109a02011-01-18 17:57:41 -08002015 /**
John Spurlock1d881a12015-03-18 19:21:54 -04002016 * {@hide}
2017 */
2018 public static String priorityToString(@Priority int pri) {
2019 switch (pri) {
2020 case PRIORITY_MIN:
2021 return "MIN";
2022 case PRIORITY_LOW:
2023 return "LOW";
2024 case PRIORITY_DEFAULT:
2025 return "DEFAULT";
2026 case PRIORITY_HIGH:
2027 return "HIGH";
2028 case PRIORITY_MAX:
2029 return "MAX";
2030 default:
2031 return "UNKNOWN(" + String.valueOf(pri) + ")";
2032 }
2033 }
2034
2035 /**
Dan Sandlerd63f9322015-05-06 15:18:49 -04002036 * The small icon representing this notification in the status bar and content view.
2037 *
2038 * @return the small icon representing this notification.
2039 *
2040 * @see Builder#getSmallIcon()
2041 * @see Builder#setSmallIcon(Icon)
2042 */
2043 public Icon getSmallIcon() {
2044 return mSmallIcon;
2045 }
2046
2047 /**
2048 * Used when notifying to clean up legacy small icons.
2049 * @hide
2050 */
2051 public void setSmallIcon(Icon icon) {
2052 mSmallIcon = icon;
2053 }
2054
2055 /**
2056 * The large icon shown in this notification's content view.
2057 * @see Builder#getLargeIcon()
2058 * @see Builder#setLargeIcon(Icon)
2059 */
2060 public Icon getLargeIcon() {
2061 return mLargeIcon;
2062 }
2063
2064 /**
Christoph Studer4600f9b2014-07-22 22:44:43 +02002065 * @hide
2066 */
Christoph Studerc8db24b2014-07-25 17:50:30 +02002067 public boolean isGroupSummary() {
2068 return mGroupKey != null && (flags & FLAG_GROUP_SUMMARY) != 0;
2069 }
2070
2071 /**
2072 * @hide
2073 */
2074 public boolean isGroupChild() {
2075 return mGroupKey != null && (flags & FLAG_GROUP_SUMMARY) == 0;
2076 }
2077
2078 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002079 * Builder class for {@link Notification} objects.
Joe Malin8d40d042012-11-05 11:36:40 -08002080 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002081 * Provides a convenient way to set the various fields of a {@link Notification} and generate
Scott Main183bf112012-08-13 19:12:13 -07002082 * content views using the platform's notification layout template. If your app supports
2083 * versions of Android as old as API level 4, you can instead use
2084 * {@link android.support.v4.app.NotificationCompat.Builder NotificationCompat.Builder},
2085 * available in the <a href="{@docRoot}tools/extras/support-library.html">Android Support
2086 * library</a>.
Joe Malin8d40d042012-11-05 11:36:40 -08002087 *
Scott Main183bf112012-08-13 19:12:13 -07002088 * <p>Example:
Joe Malin8d40d042012-11-05 11:36:40 -08002089 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002090 * <pre class="prettyprint">
Scott Main183bf112012-08-13 19:12:13 -07002091 * Notification noti = new Notification.Builder(mContext)
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002092 * .setContentTitle(&quot;New mail from &quot; + sender.toString())
2093 * .setContentText(subject)
2094 * .setSmallIcon(R.drawable.new_mail)
2095 * .setLargeIcon(aBitmap)
Chris Wrenfbd96ba2012-05-01 12:03:58 -04002096 * .build();
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002097 * </pre>
Joe Onoratocb109a02011-01-18 17:57:41 -08002098 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002099 public static class Builder {
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05002100 /**
2101 * @hide
2102 */
2103 public static final String EXTRA_REBUILD_CONTENT_VIEW_ACTION_COUNT =
2104 "android.rebuild.contentViewActionCount";
2105 /**
2106 * @hide
2107 */
2108 public static final String EXTRA_REBUILD_BIG_CONTENT_VIEW_ACTION_COUNT
2109 = "android.rebuild.bigViewActionCount";
2110 /**
2111 * @hide
2112 */
2113 public static final String EXTRA_REBUILD_HEADS_UP_CONTENT_VIEW_ACTION_COUNT
2114 = "android.rebuild.hudViewActionCount";
2115
Daniel Sandler602ad1c2012-06-12 16:06:27 -04002116 private static final int MAX_ACTION_BUTTONS = 3;
Daniel Sandler8680bf82012-05-15 16:52:52 -04002117
Joe Onorato46439ce2010-11-19 13:56:21 -08002118 private Context mContext;
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002119 private Notification mN;
2120 private Bundle mUserExtras = new Bundle();
Chris Wrenfbd96ba2012-05-01 12:03:58 -04002121 private Style mStyle;
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002122 private ArrayList<Action> mActions = new ArrayList<Action>(MAX_ACTION_BUTTONS);
2123 private ArrayList<String> mPersonList = new ArrayList<String>();
2124 private NotificationColorUtil mColorUtil;
2125 private boolean mColorUtilInited = false;
Christoph Studer7ac80e62014-08-04 16:01:57 +02002126
2127 /**
Adrian Roos4ff3b122016-02-01 12:26:13 -08002128 * Caches a contrast-enhanced version of {@link #mCachedContrastColorIsFor}.
2129 */
2130 private int mCachedContrastColor = COLOR_INVALID;
2131 private int mCachedContrastColorIsFor = COLOR_INVALID;
2132
2133 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002134 * Constructs a new Builder with the defaults:
Joe Onoratocb109a02011-01-18 17:57:41 -08002135 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002136
2137 * <table>
2138 * <tr><th align=right>priority</th>
2139 * <td>{@link #PRIORITY_DEFAULT}</td></tr>
2140 * <tr><th align=right>when</th>
2141 * <td>now ({@link System#currentTimeMillis()})</td></tr>
2142 * <tr><th align=right>audio stream</th>
2143 * <td>{@link #STREAM_DEFAULT}</td></tr>
2144 * </table>
Joe Onoratocb109a02011-01-18 17:57:41 -08002145 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002146
2147 * @param context
2148 * A {@link Context} that will be used by the Builder to construct the
2149 * RemoteViews. The Context will not be held past the lifetime of this Builder
2150 * object.
Joe Onoratocb109a02011-01-18 17:57:41 -08002151 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002152 public Builder(Context context) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002153 this(context, null);
Joe Onorato46439ce2010-11-19 13:56:21 -08002154 }
2155
Joe Onoratocb109a02011-01-18 17:57:41 -08002156 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002157 * @hide
Christoph Studer4600f9b2014-07-22 22:44:43 +02002158 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002159 public Builder(Context context, Notification toAdopt) {
2160 mContext = context;
Christoph Studer4600f9b2014-07-22 22:44:43 +02002161
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002162 if (toAdopt == null) {
2163 mN = new Notification();
2164 mN.extras.putBoolean(EXTRA_SHOW_WHEN, true);
2165 mN.priority = PRIORITY_DEFAULT;
2166 mN.visibility = VISIBILITY_PRIVATE;
2167 } else {
2168 mN = toAdopt;
2169 if (mN.actions != null) {
2170 Collections.addAll(mActions, mN.actions);
Christoph Studer4600f9b2014-07-22 22:44:43 +02002171 }
2172
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002173 if (mN.extras.containsKey(EXTRA_PEOPLE)) {
2174 Collections.addAll(mPersonList, mN.extras.getStringArray(EXTRA_PEOPLE));
2175 }
2176
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002177 String templateClass = mN.extras.getString(EXTRA_TEMPLATE);
2178 if (!TextUtils.isEmpty(templateClass)) {
2179 final Class<? extends Style> styleClass
2180 = getNotificationStyleClass(templateClass);
2181 if (styleClass == null) {
2182 Log.d(TAG, "Unknown style class: " + templateClass);
2183 } else {
2184 try {
2185 final Constructor<? extends Style> ctor = styleClass.getConstructor();
2186 ctor.setAccessible(true);
2187 final Style style = ctor.newInstance();
2188 style.restoreFromExtras(mN.extras);
2189
2190 if (style != null) {
2191 setStyle(style);
2192 }
2193 } catch (Throwable t) {
2194 Log.e(TAG, "Could not create Style", t);
2195 }
2196 }
2197 }
2198
2199 }
2200 }
2201
2202 private NotificationColorUtil getColorUtil() {
2203 if (!mColorUtilInited) {
2204 mColorUtilInited = true;
2205 if (mContext.getApplicationInfo().targetSdkVersion < Build.VERSION_CODES.LOLLIPOP) {
2206 mColorUtil = NotificationColorUtil.getInstance(mContext);
Christoph Studer4600f9b2014-07-22 22:44:43 +02002207 }
2208 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002209 return mColorUtil;
Christoph Studer4600f9b2014-07-22 22:44:43 +02002210 }
2211
2212 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002213 * Add a timestamp pertaining to the notification (usually the time the event occurred).
Daniel Sandler0c890492012-09-12 17:23:10 -07002214 * It will be shown in the notification content view by default; use
Griff Hazen50c11652014-05-16 09:46:31 -07002215 * {@link #setShowWhen(boolean) setShowWhen} to control this.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002216 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002217 * @see Notification#when
Joe Onoratocb109a02011-01-18 17:57:41 -08002218 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002219 public Builder setWhen(long when) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002220 mN.when = when;
Joe Onorato46439ce2010-11-19 13:56:21 -08002221 return this;
2222 }
2223
Joe Onoratocb109a02011-01-18 17:57:41 -08002224 /**
Griff Hazen50c11652014-05-16 09:46:31 -07002225 * Control whether the timestamp set with {@link #setWhen(long) setWhen} is shown
Daniel Sandler0c890492012-09-12 17:23:10 -07002226 * in the content view.
2227 */
2228 public Builder setShowWhen(boolean show) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002229 mN.extras.putBoolean(EXTRA_SHOW_WHEN, show);
Daniel Sandler0c890492012-09-12 17:23:10 -07002230 return this;
2231 }
2232
2233 /**
Daniel Sandlerd33b8032012-05-10 11:41:48 -04002234 * Show the {@link Notification#when} field as a stopwatch.
Joe Malin8d40d042012-11-05 11:36:40 -08002235 *
2236 * Instead of presenting <code>when</code> as a timestamp, the notification will show an
Daniel Sandlerd33b8032012-05-10 11:41:48 -04002237 * automatically updating display of the minutes and seconds since <code>when</code>.
Daniel Sandlera2985ed2012-04-03 16:42:00 -04002238 *
Daniel Sandlerd33b8032012-05-10 11:41:48 -04002239 * Useful when showing an elapsed time (like an ongoing phone call).
2240 *
Selim Cinek81c23aa2016-02-25 16:23:13 -08002241 * The counter can also be set to count down to <code>when</code> when using
2242 * {@link #setChronometerCountsDown(boolean)}.
2243 *
Daniel Sandlerd33b8032012-05-10 11:41:48 -04002244 * @see android.widget.Chronometer
Daniel Sandlera2985ed2012-04-03 16:42:00 -04002245 * @see Notification#when
Selim Cinek81c23aa2016-02-25 16:23:13 -08002246 * @see #setChronometerCountsDown(boolean)
Daniel Sandlera2985ed2012-04-03 16:42:00 -04002247 */
2248 public Builder setUsesChronometer(boolean b) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002249 mN.extras.putBoolean(EXTRA_SHOW_CHRONOMETER, b);
Daniel Sandlera2985ed2012-04-03 16:42:00 -04002250 return this;
2251 }
2252
2253 /**
Selim Cinek81c23aa2016-02-25 16:23:13 -08002254 * Sets the Chronometer to count down instead of counting up.
2255 *
2256 * <p>This is only relevant if {@link #setUsesChronometer(boolean)} has been set to true.
2257 * If it isn't set the chronometer will count up.
2258 *
2259 * @see #setUsesChronometer(boolean)
2260 */
2261 public Builder setChronometerCountsDown(boolean countsDown) {
2262 mN.extras.putBoolean(EXTRA_CHRONOMETER_COUNTS_DOWN, countsDown);
2263 return this;
2264 }
2265
2266 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002267 * Set the small icon resource, which will be used to represent the notification in the
2268 * status bar.
Joe Onoratocb109a02011-01-18 17:57:41 -08002269 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002270
2271 * The platform template for the expanded view will draw this icon in the left, unless a
2272 * {@link #setLargeIcon(Bitmap) large icon} has also been specified, in which case the small
2273 * icon will be moved to the right-hand side.
2274 *
2275
2276 * @param icon
2277 * A resource ID in the application's package of the drawable to use.
2278 * @see Notification#icon
Joe Onoratocb109a02011-01-18 17:57:41 -08002279 */
Tor Norbye7b9c9122013-05-30 16:48:33 -07002280 public Builder setSmallIcon(@DrawableRes int icon) {
Dan Sandlerd63f9322015-05-06 15:18:49 -04002281 return setSmallIcon(icon != 0
2282 ? Icon.createWithResource(mContext, icon)
2283 : null);
Joe Onorato46439ce2010-11-19 13:56:21 -08002284 }
2285
Joe Onoratocb109a02011-01-18 17:57:41 -08002286 /**
2287 * A variant of {@link #setSmallIcon(int) setSmallIcon(int)} that takes an additional
2288 * level parameter for when the icon is a {@link android.graphics.drawable.LevelListDrawable
2289 * LevelListDrawable}.
2290 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002291 * @param icon A resource ID in the application's package of the drawable to use.
Joe Onoratocb109a02011-01-18 17:57:41 -08002292 * @param level The level to use for the icon.
2293 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002294 * @see Notification#icon
2295 * @see Notification#iconLevel
Joe Onoratocb109a02011-01-18 17:57:41 -08002296 */
Tor Norbye7b9c9122013-05-30 16:48:33 -07002297 public Builder setSmallIcon(@DrawableRes int icon, int level) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002298 mN.iconLevel = level;
Dan Sandlerd63f9322015-05-06 15:18:49 -04002299 return setSmallIcon(icon);
2300 }
2301
2302 /**
2303 * Set the small icon, which will be used to represent the notification in the
2304 * status bar and content view (unless overriden there by a
2305 * {@link #setLargeIcon(Bitmap) large icon}).
2306 *
2307 * @param icon An Icon object to use.
2308 * @see Notification#icon
2309 */
2310 public Builder setSmallIcon(Icon icon) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002311 mN.setSmallIcon(icon);
2312 if (icon != null && icon.getType() == Icon.TYPE_RESOURCE) {
2313 mN.icon = icon.getResId();
2314 }
Joe Onorato46439ce2010-11-19 13:56:21 -08002315 return this;
2316 }
2317
Joe Onoratocb109a02011-01-18 17:57:41 -08002318 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002319 * Set the first line of text in the platform notification template.
Joe Onoratocb109a02011-01-18 17:57:41 -08002320 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002321 public Builder setContentTitle(CharSequence title) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002322 mN.extras.putCharSequence(EXTRA_TITLE, safeCharSequence(title));
Joe Onorato46439ce2010-11-19 13:56:21 -08002323 return this;
2324 }
2325
Joe Onoratocb109a02011-01-18 17:57:41 -08002326 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002327 * Set the second line of text in the platform notification template.
Joe Onoratocb109a02011-01-18 17:57:41 -08002328 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002329 public Builder setContentText(CharSequence text) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002330 mN.extras.putCharSequence(EXTRA_TEXT, safeCharSequence(text));
Joe Onorato46439ce2010-11-19 13:56:21 -08002331 return this;
2332 }
2333
Joe Onoratocb109a02011-01-18 17:57:41 -08002334 /**
Joe Malin8d40d042012-11-05 11:36:40 -08002335 * Set the third line of text in the platform notification template.
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04002336 * Don't use if you're also using {@link #setProgress(int, int, boolean)}; they occupy the
2337 * same location in the standard template.
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002338 */
2339 public Builder setSubText(CharSequence text) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002340 mN.extras.putCharSequence(EXTRA_SUB_TEXT, safeCharSequence(text));
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002341 return this;
2342 }
2343
2344 /**
Adrian Roose458aa82015-12-08 16:17:19 -08002345 * Set the remote input history.
2346 *
2347 * This should be set to the most recent inputs that have been sent
2348 * through a {@link RemoteInput} of this Notification and cleared once the it is no
2349 * longer relevant (e.g. for chat notifications once the other party has responded).
2350 *
2351 * The most recent input must be stored at the 0 index, the second most recent at the
2352 * 1 index, etc. Note that the system will limit both how far back the inputs will be shown
2353 * and how much of each individual input is shown.
2354 *
2355 * <p>Note: The reply text will only be shown on notifications that have least one action
2356 * with a {@code RemoteInput}.</p>
2357 */
2358 public Builder setRemoteInputHistory(CharSequence[] text) {
2359 if (text == null) {
2360 mN.extras.putCharSequenceArray(EXTRA_REMOTE_INPUT_HISTORY, null);
2361 } else {
2362 final int N = Math.min(MAX_REPLY_HISTORY, text.length);
2363 CharSequence[] safe = new CharSequence[N];
2364 for (int i = 0; i < N; i++) {
2365 safe[i] = safeCharSequence(text[i]);
2366 }
2367 mN.extras.putCharSequenceArray(EXTRA_REMOTE_INPUT_HISTORY, safe);
2368 }
2369 return this;
2370 }
2371
2372 /**
Joe Onoratocb109a02011-01-18 17:57:41 -08002373 * Set the large number at the right-hand side of the notification. This is
2374 * equivalent to setContentInfo, although it might show the number in a different
2375 * font size for readability.
2376 */
Joe Onorato8595a3d2010-11-19 18:12:07 -08002377 public Builder setNumber(int number) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002378 mN.number = number;
Joe Onorato8595a3d2010-11-19 18:12:07 -08002379 return this;
2380 }
2381
Joe Onoratocb109a02011-01-18 17:57:41 -08002382 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002383 * A small piece of additional information pertaining to this notification.
2384 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002385 * The platform template will draw this on the last line of the notification, at the far
2386 * right (to the right of a smallIcon if it has been placed there).
Joe Onoratocb109a02011-01-18 17:57:41 -08002387 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002388 public Builder setContentInfo(CharSequence info) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002389 mN.extras.putCharSequence(EXTRA_INFO_TEXT, safeCharSequence(info));
Joe Onorato46439ce2010-11-19 13:56:21 -08002390 return this;
2391 }
2392
Joe Onoratocb109a02011-01-18 17:57:41 -08002393 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002394 * Set the progress this notification represents.
2395 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002396 * The platform template will represent this using a {@link ProgressBar}.
Jeff Sharkey1c400132011-08-05 14:50:13 -07002397 */
2398 public Builder setProgress(int max, int progress, boolean indeterminate) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002399 mN.extras.putInt(EXTRA_PROGRESS, progress);
2400 mN.extras.putInt(EXTRA_PROGRESS_MAX, max);
2401 mN.extras.putBoolean(EXTRA_PROGRESS_INDETERMINATE, indeterminate);
Jeff Sharkey1c400132011-08-05 14:50:13 -07002402 return this;
2403 }
2404
2405 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002406 * Supply a custom RemoteViews to use instead of the platform template.
2407 *
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002408 * Use {@link #setCustomContentView(RemoteViews)} instead.
Joe Onoratocb109a02011-01-18 17:57:41 -08002409 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002410 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -08002411 public Builder setContent(RemoteViews views) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002412 return setCustomContentView(views);
2413 }
2414
2415 /**
2416 * Supply custom RemoteViews to use instead of the platform template.
2417 *
2418 * This will override the layout that would otherwise be constructed by this Builder
2419 * object.
2420 */
2421 public Builder setCustomContentView(RemoteViews contentView) {
2422 mN.contentView = contentView;
2423 return this;
2424 }
2425
2426 /**
2427 * Supply custom RemoteViews to use instead of the platform template in the expanded form.
2428 *
2429 * This will override the expanded layout that would otherwise be constructed by this
2430 * Builder object.
2431 */
2432 public Builder setCustomBigContentView(RemoteViews contentView) {
2433 mN.bigContentView = contentView;
2434 return this;
2435 }
2436
2437 /**
2438 * Supply custom RemoteViews to use instead of the platform template in the heads up dialog.
2439 *
2440 * This will override the heads-up layout that would otherwise be constructed by this
2441 * Builder object.
2442 */
2443 public Builder setCustomHeadsUpContentView(RemoteViews contentView) {
2444 mN.headsUpContentView = contentView;
Joe Onorato46439ce2010-11-19 13:56:21 -08002445 return this;
2446 }
2447
Joe Onoratocb109a02011-01-18 17:57:41 -08002448 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002449 * Supply a {@link PendingIntent} to be sent when the notification is clicked.
2450 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002451 * As of {@link android.os.Build.VERSION_CODES#HONEYCOMB}, if this field is unset and you
2452 * have specified a custom RemoteViews with {@link #setContent(RemoteViews)}, you can use
2453 * {@link RemoteViews#setOnClickPendingIntent RemoteViews.setOnClickPendingIntent(int,PendingIntent)}
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002454 * to assign PendingIntents to individual views in that custom layout (i.e., to create
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002455 * clickable buttons inside the notification view).
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002456 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002457 * @see Notification#contentIntent Notification.contentIntent
Joe Onoratocb109a02011-01-18 17:57:41 -08002458 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002459 public Builder setContentIntent(PendingIntent intent) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002460 mN.contentIntent = intent;
Joe Onorato46439ce2010-11-19 13:56:21 -08002461 return this;
2462 }
2463
Joe Onoratocb109a02011-01-18 17:57:41 -08002464 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002465 * Supply a {@link PendingIntent} to send when the notification is cleared explicitly by the user.
2466 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002467 * @see Notification#deleteIntent
Joe Onoratocb109a02011-01-18 17:57:41 -08002468 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002469 public Builder setDeleteIntent(PendingIntent intent) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002470 mN.deleteIntent = intent;
Joe Onorato46439ce2010-11-19 13:56:21 -08002471 return this;
2472 }
2473
Joe Onoratocb109a02011-01-18 17:57:41 -08002474 /**
2475 * An intent to launch instead of posting the notification to the status bar.
2476 * Only for use with extremely high-priority notifications demanding the user's
2477 * <strong>immediate</strong> attention, such as an incoming phone call or
2478 * alarm clock that the user has explicitly set to a particular time.
2479 * If this facility is used for something else, please give the user an option
2480 * to turn it off and use a normal notification, as this can be extremely
2481 * disruptive.
2482 *
Chris Wren47c20a12014-06-18 17:27:29 -04002483 * <p>
2484 * The system UI may choose to display a heads-up notification, instead of
2485 * launching this intent, while the user is using the device.
2486 * </p>
2487 *
Joe Onoratocb109a02011-01-18 17:57:41 -08002488 * @param intent The pending intent to launch.
2489 * @param highPriority Passing true will cause this notification to be sent
2490 * even if other notifications are suppressed.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002491 *
2492 * @see Notification#fullScreenIntent
Joe Onoratocb109a02011-01-18 17:57:41 -08002493 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002494 public Builder setFullScreenIntent(PendingIntent intent, boolean highPriority) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002495 mN.fullScreenIntent = intent;
Joe Onorato46439ce2010-11-19 13:56:21 -08002496 setFlag(FLAG_HIGH_PRIORITY, highPriority);
2497 return this;
2498 }
2499
Joe Onoratocb109a02011-01-18 17:57:41 -08002500 /**
Dan Sandler5fcdf6e2014-07-18 11:31:15 -04002501 * Set the "ticker" text which is sent to accessibility services.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002502 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002503 * @see Notification#tickerText
Joe Onoratocb109a02011-01-18 17:57:41 -08002504 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002505 public Builder setTicker(CharSequence tickerText) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002506 mN.tickerText = safeCharSequence(tickerText);
Joe Onorato46439ce2010-11-19 13:56:21 -08002507 return this;
2508 }
2509
Joe Onoratocb109a02011-01-18 17:57:41 -08002510 /**
Dan Sandler5fcdf6e2014-07-18 11:31:15 -04002511 * Obsolete version of {@link #setTicker(CharSequence)}.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002512 *
Joe Onoratocb109a02011-01-18 17:57:41 -08002513 */
Dan Sandler5fcdf6e2014-07-18 11:31:15 -04002514 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -08002515 public Builder setTicker(CharSequence tickerText, RemoteViews views) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002516 setTicker(tickerText);
2517 // views is ignored
Joe Onorato46439ce2010-11-19 13:56:21 -08002518 return this;
2519 }
2520
Joe Onoratocb109a02011-01-18 17:57:41 -08002521 /**
Dan Sandlerd63f9322015-05-06 15:18:49 -04002522 * Add a large icon to the notification content view.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002523 *
2524 * In the platform template, this image will be shown on the left of the notification view
Dan Sandlerd63f9322015-05-06 15:18:49 -04002525 * in place of the {@link #setSmallIcon(Icon) small icon} (which will be placed in a small
2526 * badge atop the large icon).
Dan Sandler08a04c12015-05-06 15:18:49 -04002527 */
Dan Sandlerd63f9322015-05-06 15:18:49 -04002528 public Builder setLargeIcon(Bitmap b) {
2529 return setLargeIcon(b != null ? Icon.createWithBitmap(b) : null);
2530 }
2531
2532 /**
2533 * Add a large icon to the notification content view.
2534 *
2535 * In the platform template, this image will be shown on the left of the notification view
2536 * in place of the {@link #setSmallIcon(Icon) small icon} (which will be placed in a small
2537 * badge atop the large icon).
2538 */
2539 public Builder setLargeIcon(Icon icon) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002540 mN.mLargeIcon = icon;
2541 mN.extras.putParcelable(EXTRA_LARGE_ICON, icon);
Joe Onorato46439ce2010-11-19 13:56:21 -08002542 return this;
2543 }
2544
Joe Onoratocb109a02011-01-18 17:57:41 -08002545 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002546 * Set the sound to play.
2547 *
John Spurlockc0650f022014-07-19 13:22:39 -04002548 * It will be played using the {@link #AUDIO_ATTRIBUTES_DEFAULT default audio attributes}
2549 * for notifications.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002550 *
Chris Wren47c20a12014-06-18 17:27:29 -04002551 * <p>
2552 * A notification that is noisy is more likely to be presented as a heads-up notification.
2553 * </p>
2554 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002555 * @see Notification#sound
Joe Onoratocb109a02011-01-18 17:57:41 -08002556 */
Joe Onorato52f80cd2010-11-21 15:34:48 -08002557 public Builder setSound(Uri sound) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002558 mN.sound = sound;
2559 mN.audioAttributes = AUDIO_ATTRIBUTES_DEFAULT;
Joe Onorato52f80cd2010-11-21 15:34:48 -08002560 return this;
2561 }
2562
Joe Onoratocb109a02011-01-18 17:57:41 -08002563 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002564 * Set the sound to play, along with a specific stream on which to play it.
Joe Onoratocb109a02011-01-18 17:57:41 -08002565 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002566 * See {@link android.media.AudioManager} for the <code>STREAM_</code> constants.
2567 *
Chris Wren47c20a12014-06-18 17:27:29 -04002568 * <p>
2569 * A notification that is noisy is more likely to be presented as a heads-up notification.
2570 * </p>
John Spurlockc0650f022014-07-19 13:22:39 -04002571 * @deprecated use {@link #setSound(Uri, AudioAttributes)} instead.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002572 * @see Notification#sound
Joe Onoratocb109a02011-01-18 17:57:41 -08002573 */
Jean-Michel Trivi81f871e2014-08-06 16:32:38 -07002574 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -08002575 public Builder setSound(Uri sound, int streamType) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002576 mN.sound = sound;
2577 mN.audioStreamType = streamType;
Joe Onorato46439ce2010-11-19 13:56:21 -08002578 return this;
2579 }
2580
Joe Onoratocb109a02011-01-18 17:57:41 -08002581 /**
John Spurlockc0650f022014-07-19 13:22:39 -04002582 * Set the sound to play, along with specific {@link AudioAttributes audio attributes} to
2583 * use during playback.
2584 *
2585 * <p>
2586 * A notification that is noisy is more likely to be presented as a heads-up notification.
2587 * </p>
2588 *
2589 * @see Notification#sound
2590 */
2591 public Builder setSound(Uri sound, AudioAttributes audioAttributes) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002592 mN.sound = sound;
2593 mN.audioAttributes = audioAttributes;
John Spurlockc0650f022014-07-19 13:22:39 -04002594 return this;
2595 }
2596
2597 /**
Joe Onoratocb109a02011-01-18 17:57:41 -08002598 * Set the vibration pattern to use.
2599 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002600 * See {@link android.os.Vibrator#vibrate(long[], int)} for a discussion of the
2601 * <code>pattern</code> parameter.
2602 *
Chris Wren47c20a12014-06-18 17:27:29 -04002603 * <p>
2604 * A notification that vibrates is more likely to be presented as a heads-up notification.
2605 * </p>
2606 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002607 * @see Notification#vibrate
Joe Onoratocb109a02011-01-18 17:57:41 -08002608 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002609 public Builder setVibrate(long[] pattern) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002610 mN.vibrate = pattern;
Joe Onorato46439ce2010-11-19 13:56:21 -08002611 return this;
2612 }
2613
Joe Onoratocb109a02011-01-18 17:57:41 -08002614 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002615 * Set the desired color for the indicator LED on the device, as well as the
2616 * blink duty cycle (specified in milliseconds).
2617 *
2618
2619 * Not all devices will honor all (or even any) of these values.
2620 *
2621
2622 * @see Notification#ledARGB
2623 * @see Notification#ledOnMS
2624 * @see Notification#ledOffMS
Joe Onoratocb109a02011-01-18 17:57:41 -08002625 */
Tor Norbye80756e32015-03-02 09:39:27 -08002626 public Builder setLights(@ColorInt int argb, int onMs, int offMs) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002627 mN.ledARGB = argb;
2628 mN.ledOnMS = onMs;
2629 mN.ledOffMS = offMs;
Julia Reynolds10ee1fc2015-11-09 11:04:55 -05002630 if (onMs != 0 || offMs != 0) {
2631 mN.flags |= FLAG_SHOW_LIGHTS;
2632 }
Joe Onorato46439ce2010-11-19 13:56:21 -08002633 return this;
2634 }
2635
Joe Onoratocb109a02011-01-18 17:57:41 -08002636 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002637 * Set whether this is an "ongoing" notification.
Joe Onoratocb109a02011-01-18 17:57:41 -08002638 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002639
2640 * Ongoing notifications cannot be dismissed by the user, so your application or service
2641 * must take care of canceling them.
2642 *
2643
2644 * They are typically used to indicate a background task that the user is actively engaged
2645 * with (e.g., playing music) or is pending in some way and therefore occupying the device
2646 * (e.g., a file download, sync operation, active network connection).
2647 *
2648
2649 * @see Notification#FLAG_ONGOING_EVENT
2650 * @see Service#setForeground(boolean)
Joe Onoratocb109a02011-01-18 17:57:41 -08002651 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002652 public Builder setOngoing(boolean ongoing) {
2653 setFlag(FLAG_ONGOING_EVENT, ongoing);
2654 return this;
2655 }
2656
Joe Onoratocb109a02011-01-18 17:57:41 -08002657 /**
2658 * Set this flag if you would only like the sound, vibrate
2659 * and ticker to be played if the notification is not already showing.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002660 *
2661 * @see Notification#FLAG_ONLY_ALERT_ONCE
Joe Onoratocb109a02011-01-18 17:57:41 -08002662 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002663 public Builder setOnlyAlertOnce(boolean onlyAlertOnce) {
2664 setFlag(FLAG_ONLY_ALERT_ONCE, onlyAlertOnce);
2665 return this;
2666 }
2667
Joe Onoratocb109a02011-01-18 17:57:41 -08002668 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002669 * Make this notification automatically dismissed when the user touches it. The
2670 * PendingIntent set with {@link #setDeleteIntent} will be sent when this happens.
2671 *
2672 * @see Notification#FLAG_AUTO_CANCEL
Joe Onoratocb109a02011-01-18 17:57:41 -08002673 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002674 public Builder setAutoCancel(boolean autoCancel) {
Joe Onorato281d83f2011-01-04 17:13:10 -08002675 setFlag(FLAG_AUTO_CANCEL, autoCancel);
Joe Onorato46439ce2010-11-19 13:56:21 -08002676 return this;
2677 }
2678
Joe Onoratocb109a02011-01-18 17:57:41 -08002679 /**
Griff Hazendfcb0802014-02-11 12:00:00 -08002680 * Set whether or not this notification should not bridge to other devices.
2681 *
2682 * <p>Some notifications can be bridged to other devices for remote display.
2683 * This hint can be set to recommend this notification not be bridged.
2684 */
2685 public Builder setLocalOnly(boolean localOnly) {
2686 setFlag(FLAG_LOCAL_ONLY, localOnly);
2687 return this;
2688 }
2689
2690 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002691 * Set which notification properties will be inherited from system defaults.
Joe Onoratocb109a02011-01-18 17:57:41 -08002692 * <p>
2693 * The value should be one or more of the following fields combined with
2694 * bitwise-or:
2695 * {@link #DEFAULT_SOUND}, {@link #DEFAULT_VIBRATE}, {@link #DEFAULT_LIGHTS}.
2696 * <p>
2697 * For all default values, use {@link #DEFAULT_ALL}.
2698 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002699 public Builder setDefaults(int defaults) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002700 mN.defaults = defaults;
Joe Onorato46439ce2010-11-19 13:56:21 -08002701 return this;
2702 }
2703
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002704 /**
2705 * Set the priority of this notification.
2706 *
2707 * @see Notification#priority
2708 */
Tor Norbyed9273d62013-05-30 15:59:53 -07002709 public Builder setPriority(@Priority int pri) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002710 mN.priority = pri;
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002711 return this;
2712 }
Joe Malin8d40d042012-11-05 11:36:40 -08002713
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002714 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -04002715 * Set the notification category.
Joe Malin8d40d042012-11-05 11:36:40 -08002716 *
John Spurlockfd7f1e02014-03-18 16:41:57 -04002717 * @see Notification#category
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002718 */
John Spurlockfd7f1e02014-03-18 16:41:57 -04002719 public Builder setCategory(String category) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002720 mN.category = category;
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002721 return this;
2722 }
2723
2724 /**
Chris Wrendde75302014-03-26 17:24:15 -04002725 * Add a person that is relevant to this notification.
2726 *
Chris Wrene6c48932014-09-29 17:19:27 -04002727 * <P>
2728 * Depending on user preferences, this annotation may allow the notification to pass
2729 * through interruption filters, and to appear more prominently in the user interface.
2730 * </P>
2731 *
2732 * <P>
2733 * The person should be specified by the {@code String} representation of a
2734 * {@link android.provider.ContactsContract.Contacts#CONTENT_LOOKUP_URI}.
2735 * </P>
2736 *
2737 * <P>The system will also attempt to resolve {@code mailto:} and {@code tel:} schema
2738 * URIs. The path part of these URIs must exist in the contacts database, in the
2739 * appropriate column, or the reference will be discarded as invalid. Telephone schema
2740 * URIs will be resolved by {@link android.provider.ContactsContract.PhoneLookup}.
2741 * </P>
2742 *
2743 * @param uri A URI for the person.
Chris Wrendde75302014-03-26 17:24:15 -04002744 * @see Notification#EXTRA_PEOPLE
2745 */
Chris Wrene6c48932014-09-29 17:19:27 -04002746 public Builder addPerson(String uri) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002747 mPersonList.add(uri);
Chris Wrendde75302014-03-26 17:24:15 -04002748 return this;
2749 }
2750
2751 /**
Griff Hazen5cadc3b2014-05-20 09:55:39 -07002752 * Set this notification to be part of a group of notifications sharing the same key.
2753 * Grouped notifications may display in a cluster or stack on devices which
2754 * support such rendering.
2755 *
2756 * <p>To make this notification the summary for its group, also call
2757 * {@link #setGroupSummary}. A sort order can be specified for group members by using
2758 * {@link #setSortKey}.
2759 * @param groupKey The group key of the group.
2760 * @return this object for method chaining
2761 */
2762 public Builder setGroup(String groupKey) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002763 mN.mGroupKey = groupKey;
Griff Hazen5cadc3b2014-05-20 09:55:39 -07002764 return this;
2765 }
2766
2767 /**
2768 * Set this notification to be the group summary for a group of notifications.
2769 * Grouped notifications may display in a cluster or stack on devices which
2770 * support such rendering. Requires a group key also be set using {@link #setGroup}.
2771 * @param isGroupSummary Whether this notification should be a group summary.
2772 * @return this object for method chaining
2773 */
2774 public Builder setGroupSummary(boolean isGroupSummary) {
2775 setFlag(FLAG_GROUP_SUMMARY, isGroupSummary);
2776 return this;
2777 }
2778
2779 /**
2780 * Set a sort key that orders this notification among other notifications from the
2781 * same package. This can be useful if an external sort was already applied and an app
2782 * would like to preserve this. Notifications will be sorted lexicographically using this
2783 * value, although providing different priorities in addition to providing sort key may
2784 * cause this value to be ignored.
2785 *
2786 * <p>This sort key can also be used to order members of a notification group. See
Griff Hazen9e1379f2014-05-20 12:50:51 -07002787 * {@link #setGroup}.
Griff Hazen5cadc3b2014-05-20 09:55:39 -07002788 *
2789 * @see String#compareTo(String)
2790 */
2791 public Builder setSortKey(String sortKey) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002792 mN.mSortKey = sortKey;
Griff Hazen5cadc3b2014-05-20 09:55:39 -07002793 return this;
2794 }
2795
2796 /**
Griff Hazen720042b2014-02-24 15:46:56 -08002797 * Merge additional metadata into this notification.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002798 *
Griff Hazen720042b2014-02-24 15:46:56 -08002799 * <p>Values within the Bundle will replace existing extras values in this Builder.
2800 *
2801 * @see Notification#extras
2802 */
Griff Hazen959591e2014-05-15 22:26:18 -07002803 public Builder addExtras(Bundle extras) {
2804 if (extras != null) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002805 mUserExtras.putAll(extras);
Griff Hazen720042b2014-02-24 15:46:56 -08002806 }
2807 return this;
2808 }
2809
2810 /**
2811 * Set metadata for this notification.
2812 *
2813 * <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 -04002814 * current contents are copied into the Notification each time {@link #build()} is
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002815 * called.
2816 *
Griff Hazen720042b2014-02-24 15:46:56 -08002817 * <p>Replaces any existing extras values with those from the provided Bundle.
2818 * Use {@link #addExtras} to merge in metadata instead.
2819 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002820 * @see Notification#extras
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002821 */
Griff Hazen959591e2014-05-15 22:26:18 -07002822 public Builder setExtras(Bundle extras) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002823 if (extras != null) {
2824 mUserExtras = extras;
2825 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002826 return this;
2827 }
2828
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002829 /**
Griff Hazen720042b2014-02-24 15:46:56 -08002830 * Get the current metadata Bundle used by this notification Builder.
2831 *
2832 * <p>The returned Bundle is shared with this Builder.
2833 *
2834 * <p>The current contents of this Bundle are copied into the Notification each time
2835 * {@link #build()} is called.
2836 *
2837 * @see Notification#extras
2838 */
2839 public Bundle getExtras() {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002840 return mUserExtras;
2841 }
2842
2843 private Bundle getAllExtras() {
2844 final Bundle saveExtras = (Bundle) mUserExtras.clone();
2845 saveExtras.putAll(mN.extras);
2846 return saveExtras;
Griff Hazen720042b2014-02-24 15:46:56 -08002847 }
2848
2849 /**
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002850 * Add an action to this notification. Actions are typically displayed by
2851 * the system as a button adjacent to the notification content.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04002852 * <p>
2853 * Every action must have an icon (32dp square and matching the
2854 * <a href="{@docRoot}design/style/iconography.html#action-bar">Holo
2855 * Dark action bar</a> visual style), a textual label, and a {@link PendingIntent}.
2856 * <p>
2857 * A notification in its expanded form can display up to 3 actions, from left to right in
2858 * the order they were added. Actions will not be displayed when the notification is
2859 * collapsed, however, so be sure that any essential functions may be accessed by the user
2860 * in some other way (for example, in the Activity pointed to by {@link #contentIntent}).
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002861 *
2862 * @param icon Resource ID of a drawable that represents the action.
2863 * @param title Text describing the action.
2864 * @param intent PendingIntent to be fired when the action is invoked.
Dan Sandler86647982015-05-13 23:41:13 -04002865 *
2866 * @deprecated Use {@link #addAction(Action)} instead.
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002867 */
Dan Sandler86647982015-05-13 23:41:13 -04002868 @Deprecated
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002869 public Builder addAction(int icon, CharSequence title, PendingIntent intent) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04002870 mActions.add(new Action(icon, safeCharSequence(title), intent));
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002871 return this;
2872 }
2873
Chris Wrenfbd96ba2012-05-01 12:03:58 -04002874 /**
Griff Hazen959591e2014-05-15 22:26:18 -07002875 * Add an action to this notification. Actions are typically displayed by
2876 * the system as a button adjacent to the notification content.
2877 * <p>
2878 * Every action must have an icon (32dp square and matching the
2879 * <a href="{@docRoot}design/style/iconography.html#action-bar">Holo
2880 * Dark action bar</a> visual style), a textual label, and a {@link PendingIntent}.
2881 * <p>
2882 * A notification in its expanded form can display up to 3 actions, from left to right in
2883 * the order they were added. Actions will not be displayed when the notification is
2884 * collapsed, however, so be sure that any essential functions may be accessed by the user
2885 * in some other way (for example, in the Activity pointed to by {@link #contentIntent}).
2886 *
2887 * @param action The action to add.
2888 */
2889 public Builder addAction(Action action) {
2890 mActions.add(action);
2891 return this;
2892 }
2893
2894 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002895 * Alter the complete list of actions attached to this notification.
2896 * @see #addAction(Action).
2897 *
2898 * @param actions
2899 * @return
2900 */
2901 public Builder setActions(Action... actions) {
2902 mActions.clear();
2903 for (int i = 0; i < actions.length; i++) {
2904 mActions.add(actions[i]);
2905 }
2906 return this;
2907 }
2908
2909 /**
Chris Wrenfbd96ba2012-05-01 12:03:58 -04002910 * Add a rich notification style to be applied at build time.
2911 *
2912 * @param style Object responsible for modifying the notification style.
2913 */
2914 public Builder setStyle(Style style) {
2915 if (mStyle != style) {
2916 mStyle = style;
Daniel Sandlerc08dea22012-06-28 08:35:24 -07002917 if (mStyle != null) {
2918 mStyle.setBuilder(this);
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002919 mN.extras.putString(EXTRA_TEMPLATE, style.getClass().getName());
2920 } else {
2921 mN.extras.remove(EXTRA_TEMPLATE);
Daniel Sandlerc08dea22012-06-28 08:35:24 -07002922 }
Chris Wrenfbd96ba2012-05-01 12:03:58 -04002923 }
2924 return this;
2925 }
2926
Dan Sandler0bf2ed82013-12-21 23:33:41 -06002927 /**
2928 * Specify the value of {@link #visibility}.
Griff Hazenb720abe2014-05-20 13:15:30 -07002929 *
Dan Sandler0bf2ed82013-12-21 23:33:41 -06002930 * @param visibility One of {@link #VISIBILITY_PRIVATE} (the default),
2931 * {@link #VISIBILITY_SECRET}, or {@link #VISIBILITY_PUBLIC}.
2932 *
2933 * @return The same Builder.
2934 */
2935 public Builder setVisibility(int visibility) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002936 mN.visibility = visibility;
Dan Sandler0bf2ed82013-12-21 23:33:41 -06002937 return this;
2938 }
2939
2940 /**
2941 * Supply a replacement Notification whose contents should be shown in insecure contexts
2942 * (i.e. atop the secure lockscreen). See {@link #visibility} and {@link #VISIBILITY_PUBLIC}.
2943 * @param n A replacement notification, presumably with some or all info redacted.
2944 * @return The same Builder.
2945 */
2946 public Builder setPublicVersion(Notification n) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002947 if (n != null) {
2948 mN.publicVersion = new Notification();
2949 n.cloneInto(mN.publicVersion, /*heavy=*/ true);
2950 } else {
2951 mN.publicVersion = null;
2952 }
Dan Sandler0bf2ed82013-12-21 23:33:41 -06002953 return this;
2954 }
2955
Griff Hazenb720abe2014-05-20 13:15:30 -07002956 /**
Griff Hazen5cadc3b2014-05-20 09:55:39 -07002957 * Apply an extender to this notification builder. Extenders may be used to add
2958 * metadata or change options on this builder.
2959 */
Griff Hazen61a9e862014-05-22 16:05:19 -07002960 public Builder extend(Extender extender) {
2961 extender.extend(this);
Griff Hazen5cadc3b2014-05-20 09:55:39 -07002962 return this;
2963 }
2964
Dan Sandler4e787062015-06-17 15:09:48 -04002965 /**
2966 * @hide
2967 */
2968 public void setFlag(int mask, boolean value) {
Joe Onorato46439ce2010-11-19 13:56:21 -08002969 if (value) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002970 mN.flags |= mask;
Joe Onorato46439ce2010-11-19 13:56:21 -08002971 } else {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002972 mN.flags &= ~mask;
Joe Onorato46439ce2010-11-19 13:56:21 -08002973 }
2974 }
2975
Dan Sandler26e81cf2014-05-06 10:01:27 -04002976 /**
2977 * Sets {@link Notification#color}.
2978 *
2979 * @param argb The accent color to use
2980 *
2981 * @return The same Builder.
2982 */
Tor Norbye80756e32015-03-02 09:39:27 -08002983 public Builder setColor(@ColorInt int argb) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002984 mN.color = argb;
Julia Reynolds10ee1fc2015-11-09 11:04:55 -05002985 sanitizeColor();
Dan Sandler26e81cf2014-05-06 10:01:27 -04002986 return this;
2987 }
2988
Jorim Jaggid05aa3e2014-08-28 17:52:27 +02002989 private Drawable getProfileBadgeDrawable() {
Christoph Studer7ac80e62014-08-04 16:01:57 +02002990 // Note: This assumes that the current user can read the profile badge of the
2991 // originating user.
Selim Cineke6ff9462016-01-15 15:07:06 -08002992 return mContext.getPackageManager().getUserBadgeForDensityNoBackground(
Julia Reynoldsda303542015-11-23 14:00:20 -05002993 new UserHandle(mContext.getUserId()), 0);
Jorim Jaggid05aa3e2014-08-28 17:52:27 +02002994 }
2995
2996 private Bitmap getProfileBadge() {
2997 Drawable badge = getProfileBadgeDrawable();
Kenny Guy8a0101b2014-05-08 23:34:12 +01002998 if (badge == null) {
2999 return null;
3000 }
Jorim Jaggid05aa3e2014-08-28 17:52:27 +02003001 final int size = mContext.getResources().getDimensionPixelSize(
3002 R.dimen.notification_badge_size);
3003 Bitmap bitmap = Bitmap.createBitmap(size, size, Bitmap.Config.ARGB_8888);
Kenny Guy8a0101b2014-05-08 23:34:12 +01003004 Canvas canvas = new Canvas(bitmap);
Jorim Jaggid05aa3e2014-08-28 17:52:27 +02003005 badge.setBounds(0, 0, size, size);
Kenny Guy8a0101b2014-05-08 23:34:12 +01003006 badge.draw(canvas);
3007 return bitmap;
3008 }
3009
Selim Cinekc848c3a2016-01-13 15:27:30 -08003010 private void bindProfileBadge(RemoteViews contentView) {
Kenny Guy98193ea2014-07-24 19:54:37 +01003011 Bitmap profileBadge = getProfileBadge();
3012
Kenny Guy98193ea2014-07-24 19:54:37 +01003013 if (profileBadge != null) {
Selim Cinekc848c3a2016-01-13 15:27:30 -08003014 contentView.setImageViewBitmap(R.id.profile_badge, profileBadge);
3015 contentView.setViewVisibility(R.id.profile_badge, View.VISIBLE);
Kenny Guy98193ea2014-07-24 19:54:37 +01003016 }
Kenny Guy98193ea2014-07-24 19:54:37 +01003017 }
3018
Christoph Studerfe718432014-09-01 18:21:18 +02003019 private void resetStandardTemplate(RemoteViews contentView) {
Selim Cinekeaa29ca2015-11-23 13:51:13 -08003020 resetNotificationHeader(contentView);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003021 resetContentMargins(contentView);
Christoph Studerfe718432014-09-01 18:21:18 +02003022 contentView.setViewVisibility(R.id.right_icon, View.GONE);
Selim Cinek860b6da2015-12-16 19:02:19 -08003023 contentView.setViewVisibility(R.id.title, View.GONE);
Christoph Studerfe718432014-09-01 18:21:18 +02003024 contentView.setTextViewText(R.id.title, null);
Selim Cinek41598732016-01-11 16:58:37 -08003025 contentView.setViewVisibility(R.id.text, View.GONE);
Christoph Studerfe718432014-09-01 18:21:18 +02003026 contentView.setTextViewText(R.id.text, null);
Selim Cinek29603462015-11-17 19:04:39 -08003027 contentView.setViewVisibility(R.id.text_line_1, View.GONE);
Selim Cinek41598732016-01-11 16:58:37 -08003028 contentView.setTextViewText(R.id.text_line_1, null);
Christoph Studerfe718432014-09-01 18:21:18 +02003029 contentView.setViewVisibility(R.id.progress, View.GONE);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003030 }
3031
Selim Cinekeaa29ca2015-11-23 13:51:13 -08003032 /**
3033 * Resets the notification header to its original state
3034 */
3035 private void resetNotificationHeader(RemoteViews contentView) {
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003036 contentView.setImageViewResource(R.id.icon, 0);
Selim Cinek7b836392015-12-04 20:02:59 -08003037 contentView.setBoolean(R.id.notification_header, "setExpanded", false);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003038 contentView.setTextViewText(R.id.app_name_text, null);
Christoph Studerca1db712014-09-10 17:31:33 +02003039 contentView.setViewVisibility(R.id.chronometer, View.GONE);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003040 contentView.setViewVisibility(R.id.header_sub_text, View.GONE);
Selim Cinek29603462015-11-17 19:04:39 -08003041 contentView.setViewVisibility(R.id.header_content_info, View.GONE);
Selim Cinek29603462015-11-17 19:04:39 -08003042 contentView.setViewVisibility(R.id.sub_text_divider, View.GONE);
3043 contentView.setViewVisibility(R.id.content_info_divider, View.GONE);
3044 contentView.setViewVisibility(R.id.time_divider, View.GONE);
Selim Cinekc848c3a2016-01-13 15:27:30 -08003045 contentView.setImageViewIcon(R.id.profile_badge, null);
3046 contentView.setViewVisibility(R.id.profile_badge, View.GONE);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003047 }
3048
3049 private void resetContentMargins(RemoteViews contentView) {
3050 contentView.setViewLayoutMarginEnd(R.id.line1, 0);
Selim Cinekc848c3a2016-01-13 15:27:30 -08003051 contentView.setViewLayoutMarginEnd(R.id.text, 0);
Christoph Studerfe718432014-09-01 18:21:18 +02003052 }
3053
Jorim Jaggi445d3c02014-08-19 22:33:42 +02003054 private RemoteViews applyStandardTemplate(int resId) {
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02003055 return applyStandardTemplate(resId, true /* hasProgress */);
3056 }
3057
3058 /**
3059 * @param hasProgress whether the progress bar should be shown and set
3060 */
3061 private RemoteViews applyStandardTemplate(int resId, boolean hasProgress) {
Kenny Guy77320062014-08-27 21:37:15 +01003062 RemoteViews contentView = new BuilderRemoteViews(mContext.getApplicationInfo(), resId);
Dan Sandler539aad42014-08-04 00:43:39 -04003063
Christoph Studerfe718432014-09-01 18:21:18 +02003064 resetStandardTemplate(contentView);
3065
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003066 final Bundle ex = mN.extras;
Dan Sandler190d58d2014-05-15 09:33:39 -04003067
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003068 bindNotificationHeader(contentView);
3069 bindLargeIcon(contentView);
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003070 if (ex.getCharSequence(EXTRA_TITLE) != null) {
Selim Cinek860b6da2015-12-16 19:02:19 -08003071 contentView.setViewVisibility(R.id.title, View.VISIBLE);
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003072 contentView.setTextViewText(R.id.title,
3073 processLegacyText(ex.getCharSequence(EXTRA_TITLE)));
Joe Onorato561d3852010-11-20 18:09:34 -08003074 }
Selim Cinek29603462015-11-17 19:04:39 -08003075 boolean showProgress = handleProgressBar(hasProgress, contentView, ex);
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003076 if (ex.getCharSequence(EXTRA_TEXT) != null) {
Selim Cinek41598732016-01-11 16:58:37 -08003077 int textId = showProgress ? com.android.internal.R.id.text_line_1
3078 : com.android.internal.R.id.text;
3079 contentView.setTextViewText(textId, processLegacyText(
3080 ex.getCharSequence(EXTRA_TEXT)));
3081 contentView.setViewVisibility(textId, View.VISIBLE);
Joe Onorato561d3852010-11-20 18:09:34 -08003082 }
Selim Cinekc848c3a2016-01-13 15:27:30 -08003083
Selim Cinek860b6da2015-12-16 19:02:19 -08003084 setContentMinHeight(contentView, showProgress || mN.mLargeIcon != null);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003085
Selim Cinek29603462015-11-17 19:04:39 -08003086 return contentView;
3087 }
3088
Selim Cinek860b6da2015-12-16 19:02:19 -08003089 /**
3090 * @param remoteView the remote view to update the minheight in
3091 * @param hasMinHeight does it have a mimHeight
3092 * @hide
3093 */
3094 void setContentMinHeight(RemoteViews remoteView, boolean hasMinHeight) {
3095 int minHeight = 0;
3096 if (hasMinHeight) {
3097 // we need to set the minHeight of the notification
3098 minHeight = mContext.getResources().getDimensionPixelSize(
3099 com.android.internal.R.dimen.notification_min_content_height);
3100 }
3101 remoteView.setInt(R.id.notification_main_column, "setMinimumHeight", minHeight);
3102 }
3103
Selim Cinek29603462015-11-17 19:04:39 -08003104 private boolean handleProgressBar(boolean hasProgress, RemoteViews contentView, Bundle ex) {
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003105 final int max = ex.getInt(EXTRA_PROGRESS_MAX, 0);
3106 final int progress = ex.getInt(EXTRA_PROGRESS, 0);
3107 final boolean ind = ex.getBoolean(EXTRA_PROGRESS_INDETERMINATE);
3108 if (hasProgress && (max != 0 || ind)) {
Selim Cinek29603462015-11-17 19:04:39 -08003109 contentView.setViewVisibility(com.android.internal.R.id.progress, View.VISIBLE);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003110 contentView.setProgressBar(
Selim Cinek29603462015-11-17 19:04:39 -08003111 R.id.progress, max, progress, ind);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003112 contentView.setProgressBackgroundTintList(
3113 R.id.progress, ColorStateList.valueOf(mContext.getColor(
3114 R.color.notification_progress_background_color)));
Selim Cinek29603462015-11-17 19:04:39 -08003115 if (mN.color != COLOR_DEFAULT) {
Adrian Roos4ff3b122016-02-01 12:26:13 -08003116 ColorStateList colorStateList = ColorStateList.valueOf(resolveContrastColor());
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003117 contentView.setProgressTintList(R.id.progress, colorStateList);
3118 contentView.setProgressIndeterminateTintList(R.id.progress, colorStateList);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003119 }
Selim Cinek29603462015-11-17 19:04:39 -08003120 return true;
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003121 } else {
3122 contentView.setViewVisibility(R.id.progress, View.GONE);
Selim Cinek29603462015-11-17 19:04:39 -08003123 return false;
Jeff Sharkey1c400132011-08-05 14:50:13 -07003124 }
Joe Onorato561d3852010-11-20 18:09:34 -08003125 }
3126
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003127 private void bindLargeIcon(RemoteViews contentView) {
3128 if (mN.mLargeIcon != null) {
3129 contentView.setViewVisibility(R.id.right_icon, View.VISIBLE);
3130 contentView.setImageViewIcon(R.id.right_icon, mN.mLargeIcon);
3131 processLargeLegacyIcon(mN.mLargeIcon, contentView);
3132 int endMargin = mContext.getResources().getDimensionPixelSize(
3133 R.dimen.notification_content_picture_margin);
3134 contentView.setViewLayoutMarginEnd(R.id.line1, endMargin);
Selim Cinekc848c3a2016-01-13 15:27:30 -08003135 contentView.setViewLayoutMarginEnd(R.id.text, endMargin);
Selim Cinek29603462015-11-17 19:04:39 -08003136 contentView.setViewLayoutMarginEnd(R.id.progress, endMargin);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003137 }
3138 }
3139
3140 private void bindNotificationHeader(RemoteViews contentView) {
3141 bindSmallIcon(contentView);
3142 bindHeaderAppName(contentView);
Selim Cinek03d0d652015-11-13 13:18:09 -05003143 bindHeaderSubText(contentView);
Selim Cinek29603462015-11-17 19:04:39 -08003144 bindContentInfo(contentView);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003145 bindHeaderChronometerAndTime(contentView);
3146 bindExpandButton(contentView);
Selim Cinekc848c3a2016-01-13 15:27:30 -08003147 bindProfileBadge(contentView);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003148 }
3149
Selim Cinek29603462015-11-17 19:04:39 -08003150 private void bindContentInfo(RemoteViews contentView) {
3151 boolean visible = false;
3152 if (mN.extras.getCharSequence(EXTRA_INFO_TEXT) != null) {
3153 contentView.setTextViewText(R.id.header_content_info,
3154 processLegacyText(mN.extras.getCharSequence(EXTRA_INFO_TEXT)));
3155 contentView.setViewVisibility(R.id.header_content_info, View.VISIBLE);
3156 visible = true;
3157 } else if (mN.number > 0) {
3158 final int tooBig = mContext.getResources().getInteger(
3159 R.integer.status_bar_notification_info_maxnum);
3160 if (mN.number > tooBig) {
3161 contentView.setTextViewText(R.id.header_content_info, processLegacyText(
3162 mContext.getResources().getString(
3163 R.string.status_bar_notification_info_overflow)));
3164 } else {
3165 contentView.setTextViewText(R.id.header_content_info,
3166 processLegacyText(String.valueOf(mN.number)));
3167 }
3168 contentView.setViewVisibility(R.id.header_content_info, View.VISIBLE);
3169 visible = true;
3170 }
3171 if (visible) {
3172 contentView.setViewVisibility(R.id.content_info_divider, View.VISIBLE);
3173 }
3174 }
3175
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003176 private void bindExpandButton(RemoteViews contentView) {
Adrian Roos4ff3b122016-02-01 12:26:13 -08003177 contentView.setDrawableParameters(R.id.expand_button, false, -1, resolveContrastColor(),
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003178 PorterDuff.Mode.SRC_ATOP, -1);
Selim Cinekea4bef72015-12-02 15:51:10 -08003179 contentView.setInt(R.id.notification_header, "setOriginalNotificationColor",
Adrian Roos4ff3b122016-02-01 12:26:13 -08003180 resolveContrastColor());
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003181 }
3182
3183 private void bindHeaderChronometerAndTime(RemoteViews contentView) {
3184 if (showsTimeOrChronometer()) {
Selim Cinek29603462015-11-17 19:04:39 -08003185 contentView.setViewVisibility(R.id.time_divider, View.VISIBLE);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003186 if (mN.extras.getBoolean(EXTRA_SHOW_CHRONOMETER)) {
3187 contentView.setViewVisibility(R.id.chronometer, View.VISIBLE);
3188 contentView.setLong(R.id.chronometer, "setBase",
3189 mN.when + (SystemClock.elapsedRealtime() - System.currentTimeMillis()));
3190 contentView.setBoolean(R.id.chronometer, "setStarted", true);
Selim Cinek81c23aa2016-02-25 16:23:13 -08003191 boolean countsDown = mN.extras.getBoolean(EXTRA_CHRONOMETER_COUNTS_DOWN);
3192 contentView.setChronometerCountsDown(R.id.chronometer, countsDown);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003193 } else {
3194 contentView.setViewVisibility(R.id.time, View.VISIBLE);
3195 contentView.setLong(R.id.time, "setTime", mN.when);
3196 }
3197 }
3198 }
3199
Selim Cinek03d0d652015-11-13 13:18:09 -05003200 private void bindHeaderSubText(RemoteViews contentView) {
3201 CharSequence subText = mN.extras.getCharSequence(EXTRA_SUB_TEXT);
3202 if (subText == null && mStyle != null && mStyle.mSummaryTextSet
3203 && mStyle.hasSummaryInHeader()) {
3204 subText = mStyle.mSummaryText;
3205 }
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003206 if (subText != null) {
3207 // TODO: Remove the span entirely to only have the string with propper formating.
3208 contentView.setTextViewText(R.id.header_sub_text, processLegacyText(subText));
3209 contentView.setViewVisibility(R.id.header_sub_text, View.VISIBLE);
Selim Cinek29603462015-11-17 19:04:39 -08003210 contentView.setViewVisibility(R.id.sub_text_divider, View.VISIBLE);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003211 }
3212 }
3213
3214 private void bindHeaderAppName(RemoteViews contentView) {
Tony Mak4dc008c2016-03-16 10:46:49 +00003215 CharSequence appName = mContext.getPackageManager()
3216 .getApplicationLabel(mContext.getApplicationInfo());
3217
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003218 if (TextUtils.isEmpty(appName)) {
3219 return;
3220 }
3221 contentView.setTextViewText(R.id.app_name_text, appName);
Adrian Roos4ff3b122016-02-01 12:26:13 -08003222 contentView.setTextColor(R.id.app_name_text, resolveContrastColor());
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003223 }
3224
3225 private void bindSmallIcon(RemoteViews contentView) {
3226 contentView.setImageViewIcon(R.id.icon, mN.mSmallIcon);
3227 processSmallIconColor(mN.mSmallIcon, contentView);
3228 }
3229
Jorim Jaggi445d3c02014-08-19 22:33:42 +02003230 /**
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02003231 * @return true if the built notification will show the time or the chronometer; false
3232 * otherwise
3233 */
3234 private boolean showsTimeOrChronometer() {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003235 return mN.when != 0 && mN.extras.getBoolean(EXTRA_SHOW_WHEN);
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02003236 }
3237
Christoph Studerfe718432014-09-01 18:21:18 +02003238 private void resetStandardTemplateWithActions(RemoteViews big) {
Adrian Roos7052de52016-03-03 15:53:34 -08003239 big.setViewVisibility(R.id.actions_container, View.GONE);
Christoph Studerfe718432014-09-01 18:21:18 +02003240 big.setViewVisibility(R.id.actions, View.GONE);
Christoph Studerfe718432014-09-01 18:21:18 +02003241 big.removeAllViews(R.id.actions);
Adrian Roose458aa82015-12-08 16:17:19 -08003242
3243 big.setViewVisibility(R.id.notification_material_reply_container, View.GONE);
3244 big.setTextViewText(R.id.notification_material_reply_text_1, null);
3245
3246 big.setViewVisibility(R.id.notification_material_reply_text_2, View.GONE);
3247 big.setTextViewText(R.id.notification_material_reply_text_2, null);
3248 big.setViewVisibility(R.id.notification_material_reply_text_3, View.GONE);
3249 big.setTextViewText(R.id.notification_material_reply_text_3, null);
Christoph Studerfe718432014-09-01 18:21:18 +02003250 }
3251
Daniel Sandler96fd7c12012-03-30 16:37:36 -04003252 private RemoteViews applyStandardTemplateWithActions(int layoutId) {
Jorim Jaggi445d3c02014-08-19 22:33:42 +02003253 RemoteViews big = applyStandardTemplate(layoutId);
Daniel Sandler96fd7c12012-03-30 16:37:36 -04003254
Christoph Studerfe718432014-09-01 18:21:18 +02003255 resetStandardTemplateWithActions(big);
3256
Adrian Roose458aa82015-12-08 16:17:19 -08003257 boolean validRemoteInput = false;
3258
Daniel Sandler96fd7c12012-03-30 16:37:36 -04003259 int N = mActions.size();
3260 if (N > 0) {
Adrian Roos7052de52016-03-03 15:53:34 -08003261 big.setViewVisibility(R.id.actions_container, View.VISIBLE);
Daniel Sandler96fd7c12012-03-30 16:37:36 -04003262 big.setViewVisibility(R.id.actions, View.VISIBLE);
Daniel Sandler8680bf82012-05-15 16:52:52 -04003263 if (N>MAX_ACTION_BUTTONS) N=MAX_ACTION_BUTTONS;
Daniel Sandler96fd7c12012-03-30 16:37:36 -04003264 for (int i=0; i<N; i++) {
Adrian Roose458aa82015-12-08 16:17:19 -08003265 Action action = mActions.get(i);
3266 validRemoteInput |= hasValidRemoteInput(action);
3267
3268 final RemoteViews button = generateActionButton(action);
Adrian Roos9b123cf2016-02-04 14:55:57 -08003269 if (i == N - 1) {
3270 button.setViewLayoutWidth(com.android.internal.R.id.action0,
3271 ViewGroup.LayoutParams.MATCH_PARENT);
3272 }
Daniel Sandler96fd7c12012-03-30 16:37:36 -04003273 big.addView(R.id.actions, button);
3274 }
3275 }
Adrian Roose458aa82015-12-08 16:17:19 -08003276
3277 CharSequence[] replyText = mN.extras.getCharSequenceArray(EXTRA_REMOTE_INPUT_HISTORY);
3278 if (validRemoteInput && replyText != null
3279 && replyText.length > 0 && !TextUtils.isEmpty(replyText[0])) {
3280 big.setViewVisibility(R.id.notification_material_reply_container, View.VISIBLE);
3281 big.setTextViewText(R.id.notification_material_reply_text_1, replyText[0]);
3282
3283 if (replyText.length > 1 && !TextUtils.isEmpty(replyText[1])) {
3284 big.setViewVisibility(R.id.notification_material_reply_text_2, View.VISIBLE);
3285 big.setTextViewText(R.id.notification_material_reply_text_2, replyText[1]);
3286
3287 if (replyText.length > 2 && !TextUtils.isEmpty(replyText[2])) {
3288 big.setViewVisibility(
3289 R.id.notification_material_reply_text_3, View.VISIBLE);
3290 big.setTextViewText(R.id.notification_material_reply_text_3, replyText[2]);
3291 }
3292 }
3293 }
3294
Daniel Sandler96fd7c12012-03-30 16:37:36 -04003295 return big;
3296 }
3297
Adrian Roose458aa82015-12-08 16:17:19 -08003298 private boolean hasValidRemoteInput(Action action) {
3299 if (TextUtils.isEmpty(action.title) || action.actionIntent == null) {
3300 // Weird actions
3301 return false;
3302 }
3303
3304 RemoteInput[] remoteInputs = action.getRemoteInputs();
3305 if (remoteInputs == null) {
3306 return false;
3307 }
3308
3309 for (RemoteInput r : remoteInputs) {
3310 CharSequence[] choices = r.getChoices();
3311 if (r.getAllowFreeFormInput() || (choices != null && choices.length != 0)) {
3312 return true;
3313 }
3314 }
3315 return false;
3316 }
3317
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003318 /**
3319 * Construct a RemoteViews for the final 1U notification layout. In order:
3320 * 1. Custom contentView from the caller
3321 * 2. Style's proposed content view
3322 * 3. Standard template view
3323 */
Julia Reynolds3b848122016-02-26 10:45:32 -05003324 public RemoteViews createContentView() {
Selim Cinek593610c2016-02-16 18:42:57 -08003325 if (mN.contentView != null && (mStyle == null || !mStyle.displayCustomViewInline())) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003326 return mN.contentView;
3327 } else if (mStyle != null) {
3328 final RemoteViews styleView = mStyle.makeContentView();
3329 if (styleView != null) {
3330 return styleView;
3331 }
Joe Onorato46439ce2010-11-19 13:56:21 -08003332 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003333 return applyStandardTemplate(getBaseLayoutResource());
Joe Onorato46439ce2010-11-19 13:56:21 -08003334 }
3335
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003336 /**
3337 * Construct a RemoteViews for the final big notification layout.
3338 */
Julia Reynolds3b848122016-02-26 10:45:32 -05003339 public RemoteViews createBigContentView() {
Selim Cinek850a8542015-11-11 11:48:36 -05003340 RemoteViews result = null;
Selim Cinek593610c2016-02-16 18:42:57 -08003341 if (mN.bigContentView != null
3342 && (mStyle == null || !mStyle.displayCustomViewInline())) {
Julia Reynolds089e3e42015-11-18 09:59:57 -05003343 return mN.bigContentView;
3344 } else if (mStyle != null) {
Selim Cinek850a8542015-11-11 11:48:36 -05003345 result = mStyle.makeBigContentView();
Selim Cinek90dcf6d2015-11-18 20:24:13 -08003346 hideLine1Text(result);
Selim Cinekcc10bfb2016-02-10 16:24:21 -08003347 } else if (mActions.size() != 0) {
3348 result = applyStandardTemplateWithActions(getBigBaseLayoutResource());
Selim Cinek850a8542015-11-11 11:48:36 -05003349 }
3350 adaptNotificationHeaderForBigContentView(result);
3351 return result;
3352 }
Daniel Sandler96fd7c12012-03-30 16:37:36 -04003353
Selim Cinekeaa29ca2015-11-23 13:51:13 -08003354 /**
3355 * Construct a RemoteViews for the final notification header only
3356 *
3357 * @hide
3358 */
3359 public RemoteViews makeNotificationHeader() {
3360 RemoteViews header = new BuilderRemoteViews(mContext.getApplicationInfo(),
3361 R.layout.notification_template_header);
3362 resetNotificationHeader(header);
3363 bindNotificationHeader(header);
3364 return header;
3365 }
3366
Selim Cinek29603462015-11-17 19:04:39 -08003367 private void hideLine1Text(RemoteViews result) {
Selim Cinekcc10bfb2016-02-10 16:24:21 -08003368 if (result != null) {
3369 result.setViewVisibility(R.id.text_line_1, View.GONE);
3370 }
Selim Cinek29603462015-11-17 19:04:39 -08003371 }
3372
Selim Cinek850a8542015-11-11 11:48:36 -05003373 private void adaptNotificationHeaderForBigContentView(RemoteViews result) {
Selim Cinekcc10bfb2016-02-10 16:24:21 -08003374 if (result != null) {
3375 result.setBoolean(R.id.notification_header, "setExpanded", true);
3376 }
Daniel Sandler96fd7c12012-03-30 16:37:36 -04003377 }
3378
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003379 /**
3380 * Construct a RemoteViews for the final heads-up notification layout.
3381 */
Julia Reynolds3b848122016-02-26 10:45:32 -05003382 public RemoteViews createHeadsUpContentView() {
Selim Cinek593610c2016-02-16 18:42:57 -08003383 if (mN.headsUpContentView != null
3384 && (mStyle == null || !mStyle.displayCustomViewInline())) {
Julia Reynolds089e3e42015-11-18 09:59:57 -05003385 return mN.headsUpContentView;
3386 } else if (mStyle != null) {
3387 final RemoteViews styleView = mStyle.makeHeadsUpContentView();
3388 if (styleView != null) {
3389 return styleView;
3390 }
3391 } else if (mActions.size() == 0) {
3392 return null;
3393 }
3394
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01003395 return applyStandardTemplateWithActions(getBigBaseLayoutResource());
Chris Wren8fd39ec2014-02-27 17:43:26 -05003396 }
3397
Selim Cinek624c02db2015-12-14 21:00:02 -08003398 /**
3399 * Construct a RemoteViews for the display in public contexts like on the lockscreen.
3400 *
3401 * @hide
3402 */
3403 public RemoteViews makePublicContentView() {
3404 if (mN.publicVersion != null) {
3405 final Builder builder = recoverBuilder(mContext, mN.publicVersion);
Julia Reynolds3b848122016-02-26 10:45:32 -05003406 return builder.createContentView();
Selim Cinek624c02db2015-12-14 21:00:02 -08003407 }
3408 Bundle savedBundle = mN.extras;
3409 Style style = mStyle;
3410 mStyle = null;
3411 Icon largeIcon = mN.mLargeIcon;
3412 mN.mLargeIcon = null;
3413 Bundle publicExtras = new Bundle();
3414 publicExtras.putBoolean(EXTRA_SHOW_WHEN,
3415 savedBundle.getBoolean(EXTRA_SHOW_WHEN));
3416 publicExtras.putBoolean(EXTRA_SHOW_CHRONOMETER,
3417 savedBundle.getBoolean(EXTRA_SHOW_CHRONOMETER));
Selim Cinek81c23aa2016-02-25 16:23:13 -08003418 publicExtras.putBoolean(EXTRA_CHRONOMETER_COUNTS_DOWN,
3419 savedBundle.getBoolean(EXTRA_CHRONOMETER_COUNTS_DOWN));
Selim Cinek624c02db2015-12-14 21:00:02 -08003420 publicExtras.putCharSequence(EXTRA_TITLE,
3421 mContext.getString(R.string.notification_hidden_text));
3422 mN.extras = publicExtras;
3423 final RemoteViews publicView = applyStandardTemplate(getBaseLayoutResource());
3424 mN.extras = savedBundle;
3425 mN.mLargeIcon = largeIcon;
3426 mStyle = style;
3427 return publicView;
3428 }
3429
3430
Chris Wren8fd39ec2014-02-27 17:43:26 -05003431
Daniel Sandler96fd7c12012-03-30 16:37:36 -04003432 private RemoteViews generateActionButton(Action action) {
Daniel Sandler8680bf82012-05-15 16:52:52 -04003433 final boolean tombstone = (action.actionIntent == null);
Selim Cinekf33b1112015-07-15 17:45:11 -07003434 RemoteViews button = new BuilderRemoteViews(mContext.getApplicationInfo(),
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01003435 tombstone ? getActionTombstoneLayoutResource()
3436 : getActionLayoutResource());
Dan Sandler68079d52015-07-22 10:45:30 -04003437 final Icon ai = action.getIcon();
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01003438 button.setTextViewText(R.id.action0, processLegacyText(action.title));
Daniel Sandler8680bf82012-05-15 16:52:52 -04003439 if (!tombstone) {
Daniel Sandlere5518842012-05-10 16:20:40 -04003440 button.setOnClickPendingIntent(R.id.action0, action.actionIntent);
Daniel Sandlere5518842012-05-10 16:20:40 -04003441 }
Daniel Sandler96fd7c12012-03-30 16:37:36 -04003442 button.setContentDescription(R.id.action0, action.title);
Adrian Roosfe84e1f2015-11-04 15:55:39 -08003443 if (action.mRemoteInputs != null) {
3444 button.setRemoteInputs(R.id.action0, action.mRemoteInputs);
3445 }
3446 if (mN.color != COLOR_DEFAULT) {
Adrian Roos4ff3b122016-02-01 12:26:13 -08003447 button.setTextColor(R.id.action0, resolveContrastColor());
Adrian Roosfe84e1f2015-11-04 15:55:39 -08003448 }
Daniel Sandler96fd7c12012-03-30 16:37:36 -04003449 return button;
3450 }
3451
Joe Onoratocb109a02011-01-18 17:57:41 -08003452 /**
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01003453 * @return Whether we are currently building a notification from a legacy (an app that
Alan Viverette3cb07a462014-06-06 14:19:53 -07003454 * doesn't create material notifications by itself) app.
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01003455 */
3456 private boolean isLegacy() {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003457 return getColorUtil() != null;
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01003458 }
3459
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01003460 private CharSequence processLegacyText(CharSequence charSequence) {
3461 if (isLegacy()) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003462 return getColorUtil().invertCharSequenceColors(charSequence);
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01003463 } else {
3464 return charSequence;
3465 }
3466 }
3467
Dan Sandler26e81cf2014-05-06 10:01:27 -04003468 /**
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003469 * Apply any necessariy colors to the small icon
Dan Sandler26e81cf2014-05-06 10:01:27 -04003470 */
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003471 private void processSmallIconColor(Icon smallIcon, RemoteViews contentView) {
Selim Cinekea4bef72015-12-02 15:51:10 -08003472 boolean colorable = !isLegacy() || getColorUtil().isGrayscaleIcon(mContext, smallIcon);
3473 if (colorable) {
Adrian Roos4ff3b122016-02-01 12:26:13 -08003474 contentView.setDrawableParameters(R.id.icon, false, -1, resolveContrastColor(),
Jorim Jaggi92df1f22014-12-16 19:44:41 +01003475 PorterDuff.Mode.SRC_ATOP, -1);
Selim Cinekea4bef72015-12-02 15:51:10 -08003476
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01003477 }
Selim Cinekea4bef72015-12-02 15:51:10 -08003478 contentView.setInt(R.id.notification_header, "setOriginalIconColor",
Adrian Roos4ff3b122016-02-01 12:26:13 -08003479 colorable ? resolveContrastColor() : NotificationHeaderView.NO_COLOR);
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01003480 }
3481
Dan Sandler26e81cf2014-05-06 10:01:27 -04003482 /**
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003483 * Make the largeIcon dark if it's a fake smallIcon (that is,
Dan Sandler26e81cf2014-05-06 10:01:27 -04003484 * if it's grayscale).
3485 */
3486 // TODO: also check bounds, transparency, that sort of thing.
Dan Sandlerd63f9322015-05-06 15:18:49 -04003487 private void processLargeLegacyIcon(Icon largeIcon, RemoteViews contentView) {
3488 if (largeIcon != null && isLegacy()
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003489 && getColorUtil().isGrayscaleIcon(mContext, largeIcon)) {
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003490 // resolve color will fall back to the default when legacy
Adrian Roos4ff3b122016-02-01 12:26:13 -08003491 contentView.setDrawableParameters(R.id.icon, false, -1, resolveContrastColor(),
Dan Sandler190d58d2014-05-15 09:33:39 -04003492 PorterDuff.Mode.SRC_ATOP, -1);
Jorim Jaggi92df1f22014-12-16 19:44:41 +01003493 }
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01003494 }
3495
Julia Reynolds10ee1fc2015-11-09 11:04:55 -05003496 private void sanitizeColor() {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003497 if (mN.color != COLOR_DEFAULT) {
3498 mN.color |= 0xFF000000; // no alpha for custom colors
Jorim Jaggi74419312014-06-10 20:57:21 +02003499 }
Jorim Jaggi74419312014-06-10 20:57:21 +02003500 }
3501
Adrian Roos4ff3b122016-02-01 12:26:13 -08003502 int resolveContrastColor() {
3503 if (mCachedContrastColorIsFor == mN.color && mCachedContrastColor != COLOR_INVALID) {
3504 return mCachedContrastColor;
Dan Sandler26e81cf2014-05-06 10:01:27 -04003505 }
Adrian Roos4ff3b122016-02-01 12:26:13 -08003506 final int contrasted = NotificationColorUtil.resolveContrastColor(mContext, mN.color);
3507
3508 mCachedContrastColorIsFor = mN.color;
3509 return mCachedContrastColor = contrasted;
Dan Sandler26e81cf2014-05-06 10:01:27 -04003510 }
3511
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01003512 /**
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003513 * Apply the unstyled operations and return a new {@link Notification} object.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04003514 * @hide
Joe Onoratocb109a02011-01-18 17:57:41 -08003515 */
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04003516 public Notification buildUnstyled() {
Daniel Sandlera0a938c2012-03-15 08:42:37 -04003517 if (mActions.size() > 0) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003518 mN.actions = new Action[mActions.size()];
3519 mActions.toArray(mN.actions);
Daniel Sandlera0a938c2012-03-15 08:42:37 -04003520 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003521 if (!mPersonList.isEmpty()) {
3522 mN.extras.putStringArray(EXTRA_PEOPLE,
3523 mPersonList.toArray(new String[mPersonList.size()]));
Dan Sandler0bf2ed82013-12-21 23:33:41 -06003524 }
Selim Cinek247fa012016-02-18 09:50:48 -08003525 if (mN.bigContentView != null || mN.contentView != null
3526 || mN.headsUpContentView != null) {
3527 mN.extras.putBoolean(EXTRA_CONTAINS_CUSTOM_VIEW, true);
3528 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003529 return mN;
Joe Onorato46439ce2010-11-19 13:56:21 -08003530 }
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003531
Julia Reynolds3b848122016-02-26 10:45:32 -05003532 /**
3533 * Creates a Builder from an existing notification so further changes can be made.
3534 * @param context The context for your application / activity.
3535 * @param n The notification to create a Builder from.
3536 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003537 public static Notification.Builder recoverBuilder(Context context, Notification n) {
Christoph Studer4600f9b2014-07-22 22:44:43 +02003538 // Re-create notification context so we can access app resources.
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003539 ApplicationInfo applicationInfo = n.extras.getParcelable(
3540 EXTRA_BUILDER_APPLICATION_INFO);
Christoph Studer4600f9b2014-07-22 22:44:43 +02003541 Context builderContext;
Julia Reynoldsda303542015-11-23 14:00:20 -05003542 if (applicationInfo != null) {
3543 try {
3544 builderContext = context.createApplicationContext(applicationInfo,
3545 Context.CONTEXT_RESTRICTED);
3546 } catch (NameNotFoundException e) {
3547 Log.e(TAG, "ApplicationInfo " + applicationInfo + " not found");
3548 builderContext = context; // try with our context
3549 }
3550 } else {
3551 builderContext = context; // try with given context
Christoph Studer4600f9b2014-07-22 22:44:43 +02003552 }
3553
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003554 return new Builder(builderContext, n);
Christoph Studer4600f9b2014-07-22 22:44:43 +02003555 }
3556
3557 private static Class<? extends Style> getNotificationStyleClass(String templateClass) {
Selim Cinek593610c2016-02-16 18:42:57 -08003558 Class<? extends Style>[] classes = new Class[] {
3559 BigTextStyle.class, BigPictureStyle.class, InboxStyle.class, MediaStyle.class,
Alex Hillsfc737de2016-03-23 17:33:02 -04003560 DecoratedCustomViewStyle.class, DecoratedMediaCustomViewStyle.class,
3561 MessagingStyle.class };
Christoph Studer4600f9b2014-07-22 22:44:43 +02003562 for (Class<? extends Style> innerClass : classes) {
3563 if (templateClass.equals(innerClass.getName())) {
3564 return innerClass;
3565 }
3566 }
3567 return null;
3568 }
3569
Daniel Sandlerf45564e2013-04-15 15:05:08 -04003570 /**
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003571 * @deprecated Use {@link #build()} instead.
3572 */
3573 @Deprecated
3574 public Notification getNotification() {
3575 return build();
3576 }
3577
3578 /**
3579 * Combine all of the options that have been set and return a new {@link Notification}
3580 * object.
3581 */
3582 public Notification build() {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003583 // first, add any extras from the calling code
3584 if (mUserExtras != null) {
3585 mN.extras = getAllExtras();
Jorim Jaggia0d58ae2015-06-03 11:48:13 -07003586 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003587
3588 // lazy stuff from mContext; see comment in Builder(Context, Notification)
Julia Reynoldsda303542015-11-23 14:00:20 -05003589 Notification.addFieldsFromContext(mContext, mN);
Christoph Studer943aa672014-08-03 20:31:16 +02003590
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003591 buildUnstyled();
Daniel Sandlerf45564e2013-04-15 15:05:08 -04003592
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003593 if (mStyle != null) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003594 mStyle.buildStyled(mN);
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003595 }
Daniel Sandlerf45564e2013-04-15 15:05:08 -04003596
Adrian Roos5081c0d2016-02-26 16:04:19 -08003597 if (mContext.getApplicationInfo().targetSdkVersion < Build.VERSION_CODES.N
3598 && (mStyle == null || !mStyle.displayCustomViewInline())) {
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05003599 if (mN.contentView == null) {
Julia Reynolds3b848122016-02-26 10:45:32 -05003600 mN.contentView = createContentView();
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05003601 mN.extras.putInt(EXTRA_REBUILD_CONTENT_VIEW_ACTION_COUNT,
3602 mN.contentView.getSequenceNumber());
3603 }
3604 if (mN.bigContentView == null) {
Julia Reynolds3b848122016-02-26 10:45:32 -05003605 mN.bigContentView = createBigContentView();
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05003606 if (mN.bigContentView != null) {
3607 mN.extras.putInt(EXTRA_REBUILD_BIG_CONTENT_VIEW_ACTION_COUNT,
3608 mN.bigContentView.getSequenceNumber());
3609 }
3610 }
3611 if (mN.headsUpContentView == null) {
Julia Reynolds3b848122016-02-26 10:45:32 -05003612 mN.headsUpContentView = createHeadsUpContentView();
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05003613 if (mN.headsUpContentView != null) {
3614 mN.extras.putInt(EXTRA_REBUILD_HEADS_UP_CONTENT_VIEW_ACTION_COUNT,
3615 mN.headsUpContentView.getSequenceNumber());
3616 }
3617 }
3618 }
3619
Julia Reynolds4c0c2022016-02-02 15:11:59 -05003620 if ((mN.defaults & DEFAULT_LIGHTS) != 0) {
3621 mN.flags |= FLAG_SHOW_LIGHTS;
3622 }
3623
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003624 return mN;
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003625 }
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05003626
3627 /**
3628 * Apply this Builder to an existing {@link Notification} object.
3629 *
3630 * @hide
3631 */
3632 public Notification buildInto(Notification n) {
Daniel Sandler1a497d32013-04-18 14:52:45 -04003633 build().cloneInto(n, true);
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05003634 return n;
3635 }
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01003636
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05003637 /**
Adrian Roos184bfe022016-03-03 13:41:44 -08003638 * Removes RemoteViews that were created for compatibility from {@param n}, if they did not
3639 * change.
3640 *
3641 * @return {@param n}, if no stripping is needed, otherwise a stripped clone of {@param n}.
3642 *
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05003643 * @hide
3644 */
Adrian Roos184bfe022016-03-03 13:41:44 -08003645 public static Notification maybeCloneStrippedForDelivery(Notification n) {
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05003646 String templateClass = n.extras.getString(EXTRA_TEMPLATE);
Adrian Roos184bfe022016-03-03 13:41:44 -08003647
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05003648 // Only strip views for known Styles because we won't know how to
3649 // re-create them otherwise.
Adrian Roos184bfe022016-03-03 13:41:44 -08003650 if (!TextUtils.isEmpty(templateClass)
3651 && getNotificationStyleClass(templateClass) == null) {
3652 return n;
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05003653 }
Adrian Roos184bfe022016-03-03 13:41:44 -08003654
3655 // Only strip unmodified BuilderRemoteViews.
3656 boolean stripContentView = n.contentView instanceof BuilderRemoteViews &&
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05003657 n.extras.getInt(EXTRA_REBUILD_CONTENT_VIEW_ACTION_COUNT, -1) ==
Adrian Roos184bfe022016-03-03 13:41:44 -08003658 n.contentView.getSequenceNumber();
3659 boolean stripBigContentView = n.bigContentView instanceof BuilderRemoteViews &&
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05003660 n.extras.getInt(EXTRA_REBUILD_BIG_CONTENT_VIEW_ACTION_COUNT, -1) ==
Adrian Roos184bfe022016-03-03 13:41:44 -08003661 n.bigContentView.getSequenceNumber();
3662 boolean stripHeadsUpContentView = n.headsUpContentView instanceof BuilderRemoteViews &&
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05003663 n.extras.getInt(EXTRA_REBUILD_HEADS_UP_CONTENT_VIEW_ACTION_COUNT, -1) ==
Adrian Roos184bfe022016-03-03 13:41:44 -08003664 n.headsUpContentView.getSequenceNumber();
3665
3666 // Nothing to do here, no need to clone.
3667 if (!stripContentView && !stripBigContentView && !stripHeadsUpContentView) {
3668 return n;
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05003669 }
Adrian Roos184bfe022016-03-03 13:41:44 -08003670
3671 Notification clone = n.clone();
3672 if (stripContentView) {
3673 clone.contentView = null;
3674 clone.extras.remove(EXTRA_REBUILD_CONTENT_VIEW_ACTION_COUNT);
3675 }
3676 if (stripBigContentView) {
3677 clone.bigContentView = null;
3678 clone.extras.remove(EXTRA_REBUILD_BIG_CONTENT_VIEW_ACTION_COUNT);
3679 }
3680 if (stripHeadsUpContentView) {
3681 clone.headsUpContentView = null;
3682 clone.extras.remove(EXTRA_REBUILD_HEADS_UP_CONTENT_VIEW_ACTION_COUNT);
3683 }
3684 return clone;
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05003685 }
3686
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01003687 private int getBaseLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07003688 return R.layout.notification_template_material_base;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01003689 }
3690
3691 private int getBigBaseLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07003692 return R.layout.notification_template_material_big_base;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01003693 }
3694
3695 private int getBigPictureLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07003696 return R.layout.notification_template_material_big_picture;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01003697 }
3698
3699 private int getBigTextLayoutResource() {
Jorim Jaggi445d3c02014-08-19 22:33:42 +02003700 return R.layout.notification_template_material_big_text;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01003701 }
3702
3703 private int getInboxLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07003704 return R.layout.notification_template_material_inbox;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01003705 }
3706
3707 private int getActionLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07003708 return R.layout.notification_material_action;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01003709 }
3710
3711 private int getActionTombstoneLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07003712 return R.layout.notification_material_action_tombstone;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01003713 }
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003714 }
3715
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003716 /**
3717 * An object that can apply a rich notification style to a {@link Notification.Builder}
3718 * object.
3719 */
Griff Hazendfcb0802014-02-11 12:00:00 -08003720 public static abstract class Style {
Chris Wrend6297db2012-05-03 16:20:13 -04003721 private CharSequence mBigContentTitle;
Jorim Jaggi457a10d2014-09-08 16:18:23 +02003722
3723 /**
3724 * @hide
3725 */
3726 protected CharSequence mSummaryText = null;
3727
3728 /**
3729 * @hide
3730 */
3731 protected boolean mSummaryTextSet = false;
Chris Wrend6297db2012-05-03 16:20:13 -04003732
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003733 protected Builder mBuilder;
3734
Chris Wrend6297db2012-05-03 16:20:13 -04003735 /**
3736 * Overrides ContentTitle in the big form of the template.
3737 * This defaults to the value passed to setContentTitle().
3738 */
3739 protected void internalSetBigContentTitle(CharSequence title) {
3740 mBigContentTitle = title;
3741 }
3742
3743 /**
3744 * Set the first line of text after the detail section in the big form of the template.
3745 */
3746 protected void internalSetSummaryText(CharSequence cs) {
3747 mSummaryText = cs;
Daniel Sandler619738c2012-06-07 16:33:08 -04003748 mSummaryTextSet = true;
Chris Wrend6297db2012-05-03 16:20:13 -04003749 }
3750
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003751 public void setBuilder(Builder builder) {
3752 if (mBuilder != builder) {
3753 mBuilder = builder;
Daniel Sandlerc08dea22012-06-28 08:35:24 -07003754 if (mBuilder != null) {
3755 mBuilder.setStyle(this);
3756 }
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003757 }
3758 }
3759
Chris Wrend6297db2012-05-03 16:20:13 -04003760 protected void checkBuilder() {
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003761 if (mBuilder == null) {
3762 throw new IllegalArgumentException("Style requires a valid Builder object");
3763 }
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003764 }
Chris Wrend6297db2012-05-03 16:20:13 -04003765
3766 protected RemoteViews getStandardView(int layoutId) {
3767 checkBuilder();
3768
Christoph Studer4600f9b2014-07-22 22:44:43 +02003769 // Nasty.
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003770 CharSequence oldBuilderContentTitle =
3771 mBuilder.getAllExtras().getCharSequence(EXTRA_TITLE);
Chris Wrend6297db2012-05-03 16:20:13 -04003772 if (mBigContentTitle != null) {
3773 mBuilder.setContentTitle(mBigContentTitle);
3774 }
3775
Chris Wrend6297db2012-05-03 16:20:13 -04003776 RemoteViews contentView = mBuilder.applyStandardTemplateWithActions(layoutId);
3777
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003778 mBuilder.getAllExtras().putCharSequence(EXTRA_TITLE, oldBuilderContentTitle);
Christoph Studer4600f9b2014-07-22 22:44:43 +02003779
Chris Wrend6297db2012-05-03 16:20:13 -04003780 if (mBigContentTitle != null && mBigContentTitle.equals("")) {
3781 contentView.setViewVisibility(R.id.line1, View.GONE);
Chris Wren67dc9a02012-05-16 01:03:20 -04003782 } else {
3783 contentView.setViewVisibility(R.id.line1, View.VISIBLE);
Chris Wrend6297db2012-05-03 16:20:13 -04003784 }
3785
Chris Wrend6297db2012-05-03 16:20:13 -04003786 return contentView;
3787 }
3788
Daniel Sandlerf45564e2013-04-15 15:05:08 -04003789 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003790 * Construct a Style-specific RemoteViews for the final 1U notification layout.
3791 * The default implementation has nothing additional to add.
3792 * @hide
3793 */
3794 public RemoteViews makeContentView() {
3795 return null;
3796 }
3797
3798 /**
3799 * Construct a Style-specific RemoteViews for the final big notification layout.
3800 * @hide
3801 */
3802 public RemoteViews makeBigContentView() {
3803 return null;
3804 }
3805
3806 /**
3807 * Construct a Style-specific RemoteViews for the final HUN layout.
3808 * @hide
3809 */
3810 public RemoteViews makeHeadsUpContentView() {
3811 return null;
3812 }
3813
3814 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003815 * Apply any style-specific extras to this notification before shipping it out.
Daniel Sandlerf45564e2013-04-15 15:05:08 -04003816 * @hide
3817 */
3818 public void addExtras(Bundle extras) {
3819 if (mSummaryTextSet) {
3820 extras.putCharSequence(EXTRA_SUMMARY_TEXT, mSummaryText);
3821 }
3822 if (mBigContentTitle != null) {
3823 extras.putCharSequence(EXTRA_TITLE_BIG, mBigContentTitle);
3824 }
Chris Wren91ad5632013-06-05 15:05:57 -04003825 extras.putString(EXTRA_TEMPLATE, this.getClass().getName());
Daniel Sandlerf45564e2013-04-15 15:05:08 -04003826 }
3827
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04003828 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003829 * Reconstruct the internal state of this Style object from extras.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04003830 * @hide
3831 */
Christoph Studer4600f9b2014-07-22 22:44:43 +02003832 protected void restoreFromExtras(Bundle extras) {
3833 if (extras.containsKey(EXTRA_SUMMARY_TEXT)) {
3834 mSummaryText = extras.getCharSequence(EXTRA_SUMMARY_TEXT);
3835 mSummaryTextSet = true;
3836 }
3837 if (extras.containsKey(EXTRA_TITLE_BIG)) {
3838 mBigContentTitle = extras.getCharSequence(EXTRA_TITLE_BIG);
3839 }
3840 }
3841
3842
3843 /**
3844 * @hide
3845 */
3846 public Notification buildStyled(Notification wip) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003847 addExtras(wip.extras);
Christoph Studer4600f9b2014-07-22 22:44:43 +02003848 return wip;
3849 }
3850
Daniel Sandler0ec46202015-06-24 01:27:05 -04003851 /**
3852 * @hide
3853 */
Jorim Jaggia0d58ae2015-06-03 11:48:13 -07003854 public void purgeResources() {}
3855
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04003856 /**
3857 * Calls {@link android.app.Notification.Builder#build()} on the Builder this Style is
3858 * attached to.
3859 *
3860 * @return the fully constructed Notification.
3861 */
3862 public Notification build() {
3863 checkBuilder();
3864 return mBuilder.build();
3865 }
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02003866
3867 /**
3868 * @hide
3869 * @return true if the style positions the progress bar on the second line; false if the
3870 * style hides the progress bar
3871 */
3872 protected boolean hasProgress() {
3873 return true;
3874 }
Selim Cinek03d0d652015-11-13 13:18:09 -05003875
3876 /**
3877 * @hide
3878 * @return Whether we should put the summary be put into the notification header
3879 */
3880 public boolean hasSummaryInHeader() {
3881 return true;
3882 }
Selim Cinek593610c2016-02-16 18:42:57 -08003883
3884 /**
3885 * @hide
3886 * @return Whether custom content views are displayed inline in the style
3887 */
3888 public boolean displayCustomViewInline() {
3889 return false;
3890 }
Joe Onorato46439ce2010-11-19 13:56:21 -08003891 }
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003892
3893 /**
Daniel Sandler4dfbe832012-04-11 14:51:46 -04003894 * Helper class for generating large-format notifications that include a large image attachment.
Joe Malin8d40d042012-11-05 11:36:40 -08003895 *
Robert Ly91c5ce32014-06-08 15:37:00 -07003896 * Here's how you'd set the <code>BigPictureStyle</code> on a notification:
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003897 * <pre class="prettyprint">
Robert Ly91c5ce32014-06-08 15:37:00 -07003898 * Notification notif = new Notification.Builder(mContext)
3899 * .setContentTitle(&quot;New photo from &quot; + sender.toString())
3900 * .setContentText(subject)
3901 * .setSmallIcon(R.drawable.new_post)
3902 * .setLargeIcon(aBitmap)
3903 * .setStyle(new Notification.BigPictureStyle()
3904 * .bigPicture(aBigBitmap))
3905 * .build();
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003906 * </pre>
Joe Malin8d40d042012-11-05 11:36:40 -08003907 *
Daniel Sandler4dfbe832012-04-11 14:51:46 -04003908 * @see Notification#bigContentView
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003909 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003910 public static class BigPictureStyle extends Style {
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003911 private Bitmap mPicture;
Dan Sandlerd63f9322015-05-06 15:18:49 -04003912 private Icon mBigLargeIcon;
Chris Wren3745a3d2012-05-22 15:11:52 -04003913 private boolean mBigLargeIconSet = false;
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003914
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003915 public BigPictureStyle() {
3916 }
3917
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003918 public BigPictureStyle(Builder builder) {
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003919 setBuilder(builder);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003920 }
3921
Chris Wrend6297db2012-05-03 16:20:13 -04003922 /**
3923 * Overrides ContentTitle in the big form of the template.
3924 * This defaults to the value passed to setContentTitle().
3925 */
3926 public BigPictureStyle setBigContentTitle(CharSequence title) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04003927 internalSetBigContentTitle(safeCharSequence(title));
Chris Wrend6297db2012-05-03 16:20:13 -04003928 return this;
3929 }
3930
3931 /**
3932 * Set the first line of text after the detail section in the big form of the template.
3933 */
3934 public BigPictureStyle setSummaryText(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04003935 internalSetSummaryText(safeCharSequence(cs));
Chris Wrend6297db2012-05-03 16:20:13 -04003936 return this;
3937 }
3938
Chris Wren0bd664d2012-08-01 13:56:56 -04003939 /**
3940 * Provide the bitmap to be used as the payload for the BigPicture notification.
3941 */
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003942 public BigPictureStyle bigPicture(Bitmap b) {
3943 mPicture = b;
3944 return this;
3945 }
3946
Chris Wren3745a3d2012-05-22 15:11:52 -04003947 /**
Chris Wren3745a3d2012-05-22 15:11:52 -04003948 * Override the large icon when the big notification is shown.
3949 */
3950 public BigPictureStyle bigLargeIcon(Bitmap b) {
Dan Sandlerd63f9322015-05-06 15:18:49 -04003951 return bigLargeIcon(b != null ? Icon.createWithBitmap(b) : null);
3952 }
3953
3954 /**
3955 * Override the large icon when the big notification is shown.
3956 */
3957 public BigPictureStyle bigLargeIcon(Icon icon) {
Chris Wren3745a3d2012-05-22 15:11:52 -04003958 mBigLargeIconSet = true;
Dan Sandlerd63f9322015-05-06 15:18:49 -04003959 mBigLargeIcon = icon;
Chris Wren3745a3d2012-05-22 15:11:52 -04003960 return this;
3961 }
3962
Riley Andrews0394a0c2015-11-03 23:36:52 -08003963 /** @hide */
3964 public static final int MIN_ASHMEM_BITMAP_SIZE = 128 * (1 << 10);
3965
Daniel Sandler0ec46202015-06-24 01:27:05 -04003966 /**
3967 * @hide
3968 */
Jorim Jaggia0d58ae2015-06-03 11:48:13 -07003969 @Override
3970 public void purgeResources() {
3971 super.purgeResources();
Riley Andrews8cee7c12015-11-01 23:36:04 -08003972 if (mPicture != null &&
3973 mPicture.isMutable() &&
Riley Andrews0394a0c2015-11-03 23:36:52 -08003974 mPicture.getAllocationByteCount() >= MIN_ASHMEM_BITMAP_SIZE) {
Jorim Jaggia0d58ae2015-06-03 11:48:13 -07003975 mPicture = mPicture.createAshmemBitmap();
3976 }
3977 if (mBigLargeIcon != null) {
3978 mBigLargeIcon.convertToAshmem();
3979 }
3980 }
Christoph Studer5c510ee2014-12-15 16:32:27 +01003981
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003982 /**
3983 * @hide
3984 */
3985 public RemoteViews makeBigContentView() {
3986 // Replace mN.mLargeIcon with mBigLargeIcon if mBigLargeIconSet
Christoph Studer5c510ee2014-12-15 16:32:27 +01003987 // This covers the following cases:
3988 // 1. mBigLargeIconSet -> mBigLargeIcon (null or non-null) applies, overrides
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003989 // mN.mLargeIcon
3990 // 2. !mBigLargeIconSet -> mN.mLargeIcon applies
Dan Sandlerd63f9322015-05-06 15:18:49 -04003991 Icon oldLargeIcon = null;
Christoph Studer5c510ee2014-12-15 16:32:27 +01003992 if (mBigLargeIconSet) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003993 oldLargeIcon = mBuilder.mN.mLargeIcon;
3994 mBuilder.mN.mLargeIcon = mBigLargeIcon;
Christoph Studer5c510ee2014-12-15 16:32:27 +01003995 }
3996
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01003997 RemoteViews contentView = getStandardView(mBuilder.getBigPictureLayoutResource());
Selim Cinek03d0d652015-11-13 13:18:09 -05003998 if (mSummaryTextSet) {
3999 contentView.setTextViewText(R.id.text, mBuilder.processLegacyText(mSummaryText));
Selim Cinekc848c3a2016-01-13 15:27:30 -08004000 contentView.setViewVisibility(R.id.text, View.VISIBLE);
Selim Cinek03d0d652015-11-13 13:18:09 -05004001 }
Selim Cinek860b6da2015-12-16 19:02:19 -08004002 mBuilder.setContentMinHeight(contentView, mBuilder.mN.mLargeIcon != null);
Selim Cinek53e64a42015-11-16 10:40:56 -08004003
Christoph Studer5c510ee2014-12-15 16:32:27 +01004004 if (mBigLargeIconSet) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004005 mBuilder.mN.mLargeIcon = oldLargeIcon;
Christoph Studer5c510ee2014-12-15 16:32:27 +01004006 }
4007
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004008 contentView.setImageViewBitmap(R.id.big_picture, mPicture);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004009 return contentView;
4010 }
4011
Daniel Sandlerf45564e2013-04-15 15:05:08 -04004012 /**
4013 * @hide
4014 */
4015 public void addExtras(Bundle extras) {
4016 super.addExtras(extras);
4017
4018 if (mBigLargeIconSet) {
4019 extras.putParcelable(EXTRA_LARGE_ICON_BIG, mBigLargeIcon);
4020 }
4021 extras.putParcelable(EXTRA_PICTURE, mPicture);
4022 }
4023
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04004024 /**
4025 * @hide
4026 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004027 @Override
Christoph Studer4600f9b2014-07-22 22:44:43 +02004028 protected void restoreFromExtras(Bundle extras) {
4029 super.restoreFromExtras(extras);
4030
4031 if (extras.containsKey(EXTRA_LARGE_ICON_BIG)) {
Christoph Studer5c510ee2014-12-15 16:32:27 +01004032 mBigLargeIconSet = true;
Christoph Studer4600f9b2014-07-22 22:44:43 +02004033 mBigLargeIcon = extras.getParcelable(EXTRA_LARGE_ICON_BIG);
Chris Wren3745a3d2012-05-22 15:11:52 -04004034 }
Christoph Studer4600f9b2014-07-22 22:44:43 +02004035 mPicture = extras.getParcelable(EXTRA_PICTURE);
4036 }
Selim Cinek03d0d652015-11-13 13:18:09 -05004037
4038 /**
4039 * @hide
4040 */
4041 @Override
4042 public boolean hasSummaryInHeader() {
4043 return false;
4044 }
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004045 }
4046
4047 /**
Daniel Sandler4dfbe832012-04-11 14:51:46 -04004048 * Helper class for generating large-format notifications that include a lot of text.
Joe Malin8d40d042012-11-05 11:36:40 -08004049 *
Robert Ly91c5ce32014-06-08 15:37:00 -07004050 * Here's how you'd set the <code>BigTextStyle</code> on a notification:
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004051 * <pre class="prettyprint">
Robert Ly91c5ce32014-06-08 15:37:00 -07004052 * Notification notif = new Notification.Builder(mContext)
4053 * .setContentTitle(&quot;New mail from &quot; + sender.toString())
4054 * .setContentText(subject)
4055 * .setSmallIcon(R.drawable.new_mail)
4056 * .setLargeIcon(aBitmap)
4057 * .setStyle(new Notification.BigTextStyle()
4058 * .bigText(aVeryLongString))
4059 * .build();
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004060 * </pre>
Joe Malin8d40d042012-11-05 11:36:40 -08004061 *
Daniel Sandler4dfbe832012-04-11 14:51:46 -04004062 * @see Notification#bigContentView
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004063 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004064 public static class BigTextStyle extends Style {
Jorim Jaggi457a10d2014-09-08 16:18:23 +02004065
4066 private static final int MAX_LINES = 13;
Selim Cinek3a2c4b92015-12-17 17:01:17 -08004067 private static final int LINES_CONSUMED_BY_ACTIONS = 4;
Jorim Jaggi457a10d2014-09-08 16:18:23 +02004068
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004069 private CharSequence mBigText;
4070
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004071 public BigTextStyle() {
4072 }
4073
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004074 public BigTextStyle(Builder builder) {
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004075 setBuilder(builder);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004076 }
4077
Chris Wrend6297db2012-05-03 16:20:13 -04004078 /**
4079 * Overrides ContentTitle in the big form of the template.
4080 * This defaults to the value passed to setContentTitle().
4081 */
4082 public BigTextStyle setBigContentTitle(CharSequence title) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04004083 internalSetBigContentTitle(safeCharSequence(title));
Chris Wrend6297db2012-05-03 16:20:13 -04004084 return this;
4085 }
4086
4087 /**
4088 * Set the first line of text after the detail section in the big form of the template.
4089 */
4090 public BigTextStyle setSummaryText(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04004091 internalSetSummaryText(safeCharSequence(cs));
Chris Wrend6297db2012-05-03 16:20:13 -04004092 return this;
4093 }
4094
Chris Wren0bd664d2012-08-01 13:56:56 -04004095 /**
4096 * Provide the longer text to be displayed in the big form of the
4097 * template in place of the content text.
4098 */
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004099 public BigTextStyle bigText(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04004100 mBigText = safeCharSequence(cs);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004101 return this;
4102 }
4103
Daniel Sandlerf45564e2013-04-15 15:05:08 -04004104 /**
4105 * @hide
4106 */
4107 public void addExtras(Bundle extras) {
4108 super.addExtras(extras);
4109
Christoph Studer4600f9b2014-07-22 22:44:43 +02004110 extras.putCharSequence(EXTRA_BIG_TEXT, mBigText);
4111 }
4112
4113 /**
4114 * @hide
4115 */
4116 @Override
4117 protected void restoreFromExtras(Bundle extras) {
4118 super.restoreFromExtras(extras);
4119
4120 mBigText = extras.getCharSequence(EXTRA_BIG_TEXT);
Daniel Sandlerf45564e2013-04-15 15:05:08 -04004121 }
4122
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004123 /**
4124 * @hide
4125 */
4126 public RemoteViews makeBigContentView() {
Christoph Studer4600f9b2014-07-22 22:44:43 +02004127
4128 // Nasty
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004129 CharSequence oldBuilderContentText =
4130 mBuilder.getAllExtras().getCharSequence(EXTRA_TEXT);
4131 mBuilder.getAllExtras().putCharSequence(EXTRA_TEXT, null);
Daniel Sandler916ad912012-06-13 12:17:07 -04004132
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01004133 RemoteViews contentView = getStandardView(mBuilder.getBigTextLayoutResource());
Joe Malin8d40d042012-11-05 11:36:40 -08004134
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004135 mBuilder.getAllExtras().putCharSequence(EXTRA_TEXT, oldBuilderContentText);
Christoph Studer4600f9b2014-07-22 22:44:43 +02004136
Selim Cinek3a2c4b92015-12-17 17:01:17 -08004137 CharSequence bigTextText = mBuilder.processLegacyText(mBigText);
4138 contentView.setTextViewText(R.id.big_text, bigTextText);
4139 contentView.setViewVisibility(R.id.big_text,
4140 TextUtils.isEmpty(bigTextText) ? View.GONE : View.VISIBLE);
Jorim Jaggi457a10d2014-09-08 16:18:23 +02004141 contentView.setInt(R.id.big_text, "setMaxLines", calculateMaxLines());
Selim Cinek4fb12d32015-11-19 18:10:48 -08004142 contentView.setBoolean(R.id.big_text, "setHasImage", mBuilder.mN.mLargeIcon != null);
4143
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004144 return contentView;
4145 }
4146
Jorim Jaggi457a10d2014-09-08 16:18:23 +02004147 private int calculateMaxLines() {
4148 int lineCount = MAX_LINES;
4149 boolean hasActions = mBuilder.mActions.size() > 0;
Jorim Jaggi457a10d2014-09-08 16:18:23 +02004150 if (hasActions) {
4151 lineCount -= LINES_CONSUMED_BY_ACTIONS;
4152 }
Jorim Jaggi457a10d2014-09-08 16:18:23 +02004153 return lineCount;
4154 }
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004155 }
Daniel Sandler879c5e02012-04-17 16:46:51 -04004156
4157 /**
Alex Hillsfc737de2016-03-23 17:33:02 -04004158 * Helper class for generating large-format notifications that include multiple back-and-forth
4159 * messages of varying types between any number of people.
4160 *
4161 * <br>
4162 * If the platform does not provide large-format notifications, this method has no effect. The
4163 * user will always see the normal notification view.
4164 * <br>
4165 * This class is a "rebuilder": It attaches to a Builder object and modifies its behavior, like
4166 * so:
4167 * <pre class="prettyprint">
4168 *
4169 * Notification noti = new Notification.Builder()
4170 * .setContentTitle(&quot;2 new messages wtih &quot; + sender.toString())
4171 * .setContentText(subject)
4172 * .setSmallIcon(R.drawable.new_message)
4173 * .setLargeIcon(aBitmap)
4174 * .setStyle(new Notification.MessagingStyle(resources.getString(R.string.reply_name))
4175 * .addMessage(messages[0].getText(), messages[0].getTime(), messages[0].getSender())
4176 * .addMessage(messages[1].getText(), messages[1].getTime(), messages[1].getSender()))
4177 * .build();
4178 * </pre>
4179 */
4180 public static class MessagingStyle extends Style {
4181
4182 /**
4183 * The maximum number of messages that will be retained in the Notification itself (the
4184 * number displayed is up to the platform).
4185 */
4186 public static final int MAXIMUM_RETAINED_MESSAGES = 25;
4187
4188 CharSequence mUserDisplayName;
4189 CharSequence mConversationTitle;
4190 boolean mAllowGeneratedReplies = true;
4191 ArrayList<Message> mMessages = new ArrayList<>();
4192
4193 MessagingStyle() {
4194 }
4195
4196 /**
4197 * @param userDisplayName the name to be displayed for any replies sent by the user before the
4198 * posting app reposts the notification with those messages after they've been actually
4199 * sent and in previous messages sent by the user added in
4200 * {@link #addMessage(Notification.MessagingStyle.Message)}
4201 */
4202 public MessagingStyle(CharSequence userDisplayName) {
4203 mUserDisplayName = userDisplayName;
4204 }
4205
4206 /**
4207 * Returns the name to be displayed for any replies sent by the user
4208 */
4209 public CharSequence getUserDisplayName() {
4210 return mUserDisplayName;
4211 }
4212
4213 /**
4214 * Set whether the platform should automatically generate possible replies from messages.
4215 * @param allowGeneratedReplies {@code true} to allow generated replies, {@code false}
4216 * otherwise
4217 * @return this object for method chaining
4218 * The default value is {@code true}
4219 */
4220 public MessagingStyle setAllowGeneratedReplies(boolean allowGeneratedReplies) {
4221 mAllowGeneratedReplies = allowGeneratedReplies;
4222 return this;
4223 }
4224
4225 /**
4226 * Return whether the platform should automatically generate possible replies from messages.
4227 */
4228 public boolean getAllowGeneratedReplies() {
4229 return mAllowGeneratedReplies;
4230 }
4231
4232 /**
4233 * Sets the title to be displayed on this conversation. This should only be used for
4234 * group messaging and left unset for one-on-one conversations.
4235 * @param conversationTitle
4236 * @return this object for method chaining.
4237 */
4238 public MessagingStyle setConversationTitle(CharSequence conversationTitle) {
4239 mConversationTitle = conversationTitle;
4240 return this;
4241 }
4242
4243 /**
4244 * Return the title to be displayed on this conversation. Can be <code>null</code> and
4245 * should be for one-on-one conversations
4246 */
4247 public CharSequence getConversationTitle() {
4248 return mConversationTitle;
4249 }
4250
4251 /**
4252 * Adds a message for display by this notification. Convenience call for a simple
4253 * {@link Message} in {@link #addMessage(Notification.MessagingStyle.Message)}.
4254 * @param text A {@link CharSequence} to be displayed as the message content
4255 * @param timestamp Time at which the message arrived
4256 * @param sender A {@link CharSequence} to be used for displaying the name of the
4257 * sender. Should be <code>null</code> for messages by the current user, in which case
4258 * the platform will insert {@link #getUserDisplayName()}.
4259 * Should be unique amongst all individuals in the conversation, and should be
4260 * consistent during re-posts of the notification.
4261 *
4262 * @see Message#Message(CharSequence, long, CharSequence)
4263 *
4264 * @return this object for method chaining
4265 */
4266 public MessagingStyle addMessage(CharSequence text, long timestamp, CharSequence sender) {
4267 mMessages.add(new Message(text, timestamp, sender));
4268 if (mMessages.size() > MAXIMUM_RETAINED_MESSAGES) {
4269 mMessages.remove(0);
4270 }
4271 return this;
4272 }
4273
4274 /**
4275 * Adds a {@link Message} for display in this notification.
4276 * @param message The {@link Message} to be displayed
4277 * @return this object for method chaining
4278 */
4279 public MessagingStyle addMessage(Message message) {
4280 mMessages.add(message);
4281 if (mMessages.size() > MAXIMUM_RETAINED_MESSAGES) {
4282 mMessages.remove(0);
4283 }
4284 return this;
4285 }
4286
4287 /**
4288 * Gets the list of {@code Message} objects that represent the notification
4289 */
4290 public List<Message> getMessages() {
4291 return mMessages;
4292 }
4293
4294 /**
4295 * @hide
4296 */
4297 @Override
4298 public void addExtras(Bundle extras) {
4299 super.addExtras(extras);
4300 if (mUserDisplayName != null) {
4301 extras.putCharSequence(EXTRA_SELF_DISPLAY_NAME, mUserDisplayName);
4302 }
4303 if (mConversationTitle != null) {
4304 extras.putCharSequence(EXTRA_THREAD_TITLE, mConversationTitle);
4305 }
4306 extras.putBoolean(EXTRA_ALLOW_GENERATED_REPLIES, mAllowGeneratedReplies);
4307 if (!mMessages.isEmpty()) {
4308 extras.putParcelableArrayList(EXTRA_MESSAGES, mMessages);
4309 }
4310 }
4311
4312 /**
4313 * @hide
4314 */
4315 @Override
4316 protected void restoreFromExtras(Bundle extras) {
4317 super.restoreFromExtras(extras);
4318
4319 mMessages.clear();
4320 mUserDisplayName = extras.getString(EXTRA_SELF_DISPLAY_NAME);
4321 mConversationTitle = extras.getString(EXTRA_THREAD_TITLE);
4322 mAllowGeneratedReplies = extras.getBoolean(EXTRA_ALLOW_GENERATED_REPLIES,
4323 mAllowGeneratedReplies);
4324 List<Message> messages = extras.getParcelableArrayList(EXTRA_MESSAGES);
4325 if (messages != null) {
4326 mMessages.addAll(messages);
4327 }
4328 }
4329
4330 /**
4331 * @hide
4332 */
4333 public RemoteViews makeBigContentView() {
4334 // TODO handset to write implementation
4335 RemoteViews contentView = getStandardView(mBuilder.getBigTextLayoutResource());
4336
4337 return contentView;
4338 }
4339
4340 public static final class Message implements Parcelable {
4341
4342 private final CharSequence mText;
4343 private final long mTimestamp;
4344 private final CharSequence mSender;
4345
4346 private String mDataMimeType;
4347 private Uri mDataUri;
4348
4349 /**
4350 * Constructor
4351 * @param text A {@link CharSequence} to be displayed as the message content
4352 * @param timestamp Time at which the message arrived
4353 * @param sender A {@link CharSequence} to be used for displaying the name of the
4354 * sender. Should be <code>null</code> for messages by the current user, in which case
4355 * the platform will insert {@link MessagingStyle#getUserDisplayName()}.
4356 * Should be unique amongst all individuals in the conversation, and should be
4357 * consistent during re-posts of the notification.
4358 */
4359 public Message(CharSequence text, long timestamp, CharSequence sender){
4360 mText = text;
4361 mTimestamp = timestamp;
4362 mSender = sender;
4363 }
4364
4365 /**
4366 * Sets a binary blob of data and an associated MIME type for a message. In the case
4367 * where the platform doesn't support the MIME type, the original text provided in the
4368 * constructor will be used.
4369 * @param dataMimeType The MIME type of the content. See
4370 * <a href="{@docRoot}notifications/messaging.html"> for the list of supported MIME
4371 * types on Android and Android Wear.
4372 * @param dataUri The uri containing the content whose type is given by the MIME type.
4373 * <p class="note">
4374 * <ol>
4375 * <li>Notification Listeners including the System UI need permission to access the
4376 * data the Uri points to. The recommended ways to do this are:</li>
4377 * <li>Store the data in your own ContentProvider, making sure that other apps have
4378 * the correct permission to access your provider. The preferred mechanism for
4379 * providing access is to use per-URI permissions which are temporary and only
4380 * grant access to the receiving application. An easy way to create a
4381 * ContentProvider like this is to use the FileProvider helper class.</li>
4382 * <li>Use the system MediaStore. The MediaStore is primarily aimed at video, audio
4383 * and image MIME types, however beginning with Android 3.0 (API level 11) it can
4384 * also store non-media types (see MediaStore.Files for more info). Files can be
4385 * inserted into the MediaStore using scanFile() after which a content:// style
4386 * Uri suitable for sharing is passed to the provided onScanCompleted() callback.
4387 * Note that once added to the system MediaStore the content is accessible to any
4388 * app on the device.</li>
4389 * </ol>
4390 * @return this object for method chaining
4391 */
4392 public Message setData(String dataMimeType, Uri dataUri) {
4393 mDataMimeType = dataMimeType;
4394 mDataUri = dataUri;
4395 return this;
4396 }
4397
4398 private Message(Parcel in) {
4399 if (in.readInt() != 0) {
4400 mText = TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(in);
4401 } else {
4402 mText = null;
4403 }
4404 mTimestamp = in.readLong();
4405 if (in.readInt() != 0) {
4406 mSender = TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(in);
4407 } else {
4408 mSender = null;
4409 }
4410 if (in.readInt() != 0) {
4411 mDataMimeType = in.readString();
4412 }
4413 if (in.readInt() != 0) {
4414 mDataUri = in.readParcelable(Uri.class.getClassLoader());
4415 }
4416 }
4417
4418 /**
4419 * Get the text to be used for this message, or the fallback text if a type and content
4420 * Uri have been set
4421 */
4422 public CharSequence getText() {
4423 return mText;
4424 }
4425
4426 /**
4427 * Get the time at which this message arrived
4428 */
4429 public long getTimestamp() {
4430 return mTimestamp;
4431 }
4432
4433 /**
4434 * Get the text used to display the contact's name in the messaging experience
4435 */
4436 public CharSequence getSender() {
4437 return mSender;
4438 }
4439
4440 /**
4441 * Get the MIME type of the data pointed to by the Uri
4442 */
4443 public String getDataMimeType() {
4444 return mDataMimeType;
4445 }
4446
4447 /**
4448 * Get the the Uri pointing to the content of the message. Can be null, in which case
4449 * {@see #getText()} is used.
4450 */
4451 public Uri getDataUri() {
4452 return mDataUri;
4453 }
4454
4455 @Override
4456 public int describeContents() {
4457 return 0;
4458 }
4459
4460 @Override
4461 public void writeToParcel(Parcel out, int flags) {
4462 if (mText != null) {
4463 out.writeInt(1);
4464 TextUtils.writeToParcel(mText, out, flags);
4465 } else {
4466 out.writeInt(0);
4467 }
4468 out.writeLong(mTimestamp);
4469 if (mSender != null) {
4470 out.writeInt(1);
4471 TextUtils.writeToParcel(mSender, out, flags);
4472 } else {
4473 out.writeInt(0);
4474 }
4475 if (mDataMimeType != null) {
4476 out.writeInt(1);
4477 out.writeString(mDataMimeType);
4478 } else {
4479 out.writeInt(0);
4480 }
4481 if (mDataUri != null) {
4482 out.writeInt(1);
4483 out.writeParcelable(mDataUri, flags);
4484 } else {
4485 out.writeInt(0);
4486 }
4487 }
4488
4489 public static final Parcelable.Creator<Message> CREATOR =
4490 new Parcelable.Creator<Message>() {
4491 public Message createFromParcel(Parcel in) {
4492 return new Message(in);
4493 }
4494 public Message[] newArray(int size) {
4495 return new Message[size];
4496 }
4497 };
4498 }
4499 }
4500
4501 /**
Daniel Sandler879c5e02012-04-17 16:46:51 -04004502 * Helper class for generating large-format notifications that include a list of (up to 5) strings.
Joe Malin8d40d042012-11-05 11:36:40 -08004503 *
Robert Ly91c5ce32014-06-08 15:37:00 -07004504 * Here's how you'd set the <code>InboxStyle</code> on a notification:
Daniel Sandler879c5e02012-04-17 16:46:51 -04004505 * <pre class="prettyprint">
Robert Ly91c5ce32014-06-08 15:37:00 -07004506 * Notification notif = new Notification.Builder(mContext)
4507 * .setContentTitle(&quot;5 New mails from &quot; + sender.toString())
4508 * .setContentText(subject)
4509 * .setSmallIcon(R.drawable.new_mail)
4510 * .setLargeIcon(aBitmap)
4511 * .setStyle(new Notification.InboxStyle()
4512 * .addLine(str1)
4513 * .addLine(str2)
4514 * .setContentTitle(&quot;&quot;)
4515 * .setSummaryText(&quot;+3 more&quot;))
4516 * .build();
Daniel Sandler879c5e02012-04-17 16:46:51 -04004517 * </pre>
Joe Malin8d40d042012-11-05 11:36:40 -08004518 *
Daniel Sandler879c5e02012-04-17 16:46:51 -04004519 * @see Notification#bigContentView
4520 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004521 public static class InboxStyle extends Style {
Daniel Sandler879c5e02012-04-17 16:46:51 -04004522 private ArrayList<CharSequence> mTexts = new ArrayList<CharSequence>(5);
4523
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004524 public InboxStyle() {
4525 }
4526
Daniel Sandler879c5e02012-04-17 16:46:51 -04004527 public InboxStyle(Builder builder) {
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004528 setBuilder(builder);
Daniel Sandler879c5e02012-04-17 16:46:51 -04004529 }
4530
Chris Wrend6297db2012-05-03 16:20:13 -04004531 /**
4532 * Overrides ContentTitle in the big form of the template.
4533 * This defaults to the value passed to setContentTitle().
4534 */
4535 public InboxStyle setBigContentTitle(CharSequence title) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04004536 internalSetBigContentTitle(safeCharSequence(title));
Chris Wrend6297db2012-05-03 16:20:13 -04004537 return this;
4538 }
4539
4540 /**
4541 * Set the first line of text after the detail section in the big form of the template.
4542 */
4543 public InboxStyle setSummaryText(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04004544 internalSetSummaryText(safeCharSequence(cs));
Chris Wrend6297db2012-05-03 16:20:13 -04004545 return this;
4546 }
4547
Chris Wren0bd664d2012-08-01 13:56:56 -04004548 /**
4549 * Append a line to the digest section of the Inbox notification.
4550 */
Daniel Sandler879c5e02012-04-17 16:46:51 -04004551 public InboxStyle addLine(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04004552 mTexts.add(safeCharSequence(cs));
Daniel Sandler879c5e02012-04-17 16:46:51 -04004553 return this;
4554 }
4555
Daniel Sandlerf45564e2013-04-15 15:05:08 -04004556 /**
4557 * @hide
4558 */
4559 public void addExtras(Bundle extras) {
4560 super.addExtras(extras);
Christoph Studer4600f9b2014-07-22 22:44:43 +02004561
Daniel Sandlerf45564e2013-04-15 15:05:08 -04004562 CharSequence[] a = new CharSequence[mTexts.size()];
4563 extras.putCharSequenceArray(EXTRA_TEXT_LINES, mTexts.toArray(a));
4564 }
4565
Christoph Studer4600f9b2014-07-22 22:44:43 +02004566 /**
4567 * @hide
4568 */
4569 @Override
4570 protected void restoreFromExtras(Bundle extras) {
4571 super.restoreFromExtras(extras);
4572
4573 mTexts.clear();
4574 if (extras.containsKey(EXTRA_TEXT_LINES)) {
4575 Collections.addAll(mTexts, extras.getCharSequenceArray(EXTRA_TEXT_LINES));
4576 }
4577 }
4578
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004579 /**
4580 * @hide
4581 */
4582 public RemoteViews makeBigContentView() {
Selim Cinekc848c3a2016-01-13 15:27:30 -08004583 // Remove the content text so it disappears unless you have a summary
Christoph Studer4600f9b2014-07-22 22:44:43 +02004584 // Nasty
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004585 CharSequence oldBuilderContentText = mBuilder.mN.extras.getCharSequence(EXTRA_TEXT);
4586 mBuilder.getAllExtras().putCharSequence(EXTRA_TEXT, null);
Christoph Studer4600f9b2014-07-22 22:44:43 +02004587
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01004588 RemoteViews contentView = getStandardView(mBuilder.getInboxLayoutResource());
Daniel Sandler619738c2012-06-07 16:33:08 -04004589
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004590 mBuilder.getAllExtras().putCharSequence(EXTRA_TEXT, oldBuilderContentText);
Christoph Studer4600f9b2014-07-22 22:44:43 +02004591
Chris Wrend6297db2012-05-03 16:20:13 -04004592 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 -04004593 R.id.inbox_text4, R.id.inbox_text5, R.id.inbox_text6};
Chris Wrend6297db2012-05-03 16:20:13 -04004594
Chris Wren4ed80d52012-05-17 09:30:03 -04004595 // Make sure all rows are gone in case we reuse a view.
4596 for (int rowId : rowIds) {
4597 contentView.setViewVisibility(rowId, View.GONE);
4598 }
4599
Jorim Jaggi445d3c02014-08-19 22:33:42 +02004600 final boolean largeText =
4601 mBuilder.mContext.getResources().getConfiguration().fontScale > 1f;
4602 final float subTextSize = mBuilder.mContext.getResources().getDimensionPixelSize(
4603 R.dimen.notification_subtext_size);
Daniel Sandler879c5e02012-04-17 16:46:51 -04004604 int i=0;
Selim Cinek9d9fc6e2015-11-12 15:49:14 -05004605 final float density = mBuilder.mContext.getResources().getDisplayMetrics().density;
4606 int topPadding = (int) (5 * density);
Selim Cinekc0fac722016-03-07 19:57:06 -08004607 int bottomPadding = mBuilder.mContext.getResources().getDimensionPixelSize(
4608 com.android.internal.R.dimen.notification_content_margin_bottom);
Selim Cinek247fa012016-02-18 09:50:48 -08004609 boolean first = true;
Daniel Sandler879c5e02012-04-17 16:46:51 -04004610 while (i < mTexts.size() && i < rowIds.length) {
4611 CharSequence str = mTexts.get(i);
4612 if (str != null && !str.equals("")) {
4613 contentView.setViewVisibility(rowIds[i], View.VISIBLE);
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01004614 contentView.setTextViewText(rowIds[i], mBuilder.processLegacyText(str));
Jorim Jaggi445d3c02014-08-19 22:33:42 +02004615 if (largeText) {
4616 contentView.setTextViewTextSize(rowIds[i], TypedValue.COMPLEX_UNIT_PX,
4617 subTextSize);
4618 }
Selim Cinek9d9fc6e2015-11-12 15:49:14 -05004619 contentView.setViewPadding(rowIds[i], 0, topPadding, 0,
4620 i == rowIds.length - 1 || i == mTexts.size() - 1 ? bottomPadding : 0);
Selim Cinek247fa012016-02-18 09:50:48 -08004621 handleInboxImageMargin(contentView, rowIds[i], first);
4622 first = false;
Daniel Sandler879c5e02012-04-17 16:46:51 -04004623 }
4624 i++;
4625 }
Kenny Guy98193ea2014-07-24 19:54:37 +01004626
Selim Cinek1e0bf612015-11-20 15:57:26 -08004627
Daniel Sandler879c5e02012-04-17 16:46:51 -04004628 return contentView;
4629 }
Selim Cinek1e0bf612015-11-20 15:57:26 -08004630
Selim Cinek247fa012016-02-18 09:50:48 -08004631 private void handleInboxImageMargin(RemoteViews contentView, int id, boolean first) {
Selim Cinek1e0bf612015-11-20 15:57:26 -08004632 int endMargin = 0;
Selim Cinek247fa012016-02-18 09:50:48 -08004633 if (first) {
4634 final int max = mBuilder.mN.extras.getInt(EXTRA_PROGRESS_MAX, 0);
4635 final boolean ind = mBuilder.mN.extras.getBoolean(EXTRA_PROGRESS_INDETERMINATE);
4636 boolean hasProgress = max != 0 || ind;
4637 if (mBuilder.mN.mLargeIcon != null && !hasProgress) {
4638 endMargin = mBuilder.mContext.getResources().getDimensionPixelSize(
4639 R.dimen.notification_content_picture_margin);
4640 }
Selim Cinek1e0bf612015-11-20 15:57:26 -08004641 }
4642 contentView.setViewLayoutMarginEnd(id, endMargin);
4643 }
Daniel Sandler879c5e02012-04-17 16:46:51 -04004644 }
Dan Sandler842dd772014-05-15 09:36:47 -04004645
4646 /**
4647 * Notification style for media playback notifications.
4648 *
4649 * In the expanded form, {@link Notification#bigContentView}, up to 5
4650 * {@link Notification.Action}s specified with
Dan Sandler86647982015-05-13 23:41:13 -04004651 * {@link Notification.Builder#addAction(Action) addAction} will be
Dan Sandler842dd772014-05-15 09:36:47 -04004652 * shown as icon-only pushbuttons, suitable for transport controls. The Bitmap given to
4653 * {@link Notification.Builder#setLargeIcon(android.graphics.Bitmap) setLargeIcon()} will be
4654 * treated as album artwork.
4655 *
4656 * Unlike the other styles provided here, MediaStyle can also modify the standard-size
4657 * {@link Notification#contentView}; by providing action indices to
Christoph Studerfde6f4d2014-12-12 13:23:26 +01004658 * {@link #setShowActionsInCompactView(int...)} you can promote up to 3 actions to be displayed
Dan Sandler842dd772014-05-15 09:36:47 -04004659 * in the standard view alongside the usual content.
4660 *
Bryan Mawhinney6be8de32014-07-18 10:35:12 +01004661 * Notifications created with MediaStyle will have their category set to
4662 * {@link Notification#CATEGORY_TRANSPORT CATEGORY_TRANSPORT} unless you set a different
4663 * category using {@link Notification.Builder#setCategory(String) setCategory()}.
4664 *
Jeff Browndba34ba2014-06-24 20:46:03 -07004665 * Finally, if you attach a {@link android.media.session.MediaSession.Token} using
4666 * {@link android.app.Notification.MediaStyle#setMediaSession(MediaSession.Token)},
Dan Sandler842dd772014-05-15 09:36:47 -04004667 * the System UI can identify this as a notification representing an active media session
4668 * and respond accordingly (by showing album artwork in the lockscreen, for example).
4669 *
4670 * To use this style with your Notification, feed it to
4671 * {@link Notification.Builder#setStyle(android.app.Notification.Style)} like so:
4672 * <pre class="prettyprint">
4673 * Notification noti = new Notification.Builder()
4674 * .setSmallIcon(R.drawable.ic_stat_player)
Christoph Studere935fe92014-11-24 14:18:06 +01004675 * .setContentTitle(&quot;Track title&quot;)
4676 * .setContentText(&quot;Artist - Album&quot;)
4677 * .setLargeIcon(albumArtBitmap))
Bryan Mawhinney6be8de32014-07-18 10:35:12 +01004678 * .setStyle(<b>new Notification.MediaStyle()</b>
4679 * .setMediaSession(mySession))
Dan Sandler842dd772014-05-15 09:36:47 -04004680 * .build();
4681 * </pre>
4682 *
4683 * @see Notification#bigContentView
4684 */
4685 public static class MediaStyle extends Style {
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02004686 static final int MAX_MEDIA_BUTTONS_IN_COMPACT = 3;
Dan Sandler842dd772014-05-15 09:36:47 -04004687 static final int MAX_MEDIA_BUTTONS = 5;
4688
4689 private int[] mActionsToShowInCompact = null;
Jeff Browndba34ba2014-06-24 20:46:03 -07004690 private MediaSession.Token mToken;
Dan Sandler842dd772014-05-15 09:36:47 -04004691
4692 public MediaStyle() {
4693 }
4694
4695 public MediaStyle(Builder builder) {
4696 setBuilder(builder);
4697 }
4698
4699 /**
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02004700 * 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 -04004701 * notification view.
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02004702 *
4703 * @param actions the indices of the actions to show in the compact notification view
Dan Sandler842dd772014-05-15 09:36:47 -04004704 */
4705 public MediaStyle setShowActionsInCompactView(int...actions) {
4706 mActionsToShowInCompact = actions;
4707 return this;
4708 }
4709
4710 /**
Jeff Browndba34ba2014-06-24 20:46:03 -07004711 * Attach a {@link android.media.session.MediaSession.Token} to this Notification
4712 * to provide additional playback information and control to the SystemUI.
Dan Sandler842dd772014-05-15 09:36:47 -04004713 */
Jeff Browndba34ba2014-06-24 20:46:03 -07004714 public MediaStyle setMediaSession(MediaSession.Token token) {
Dan Sandler842dd772014-05-15 09:36:47 -04004715 mToken = token;
4716 return this;
4717 }
4718
Christoph Studer4600f9b2014-07-22 22:44:43 +02004719 /**
4720 * @hide
4721 */
Dan Sandler842dd772014-05-15 09:36:47 -04004722 @Override
4723 public Notification buildStyled(Notification wip) {
Christoph Studer4600f9b2014-07-22 22:44:43 +02004724 super.buildStyled(wip);
Bryan Mawhinney6be8de32014-07-18 10:35:12 +01004725 if (wip.category == null) {
4726 wip.category = Notification.CATEGORY_TRANSPORT;
4727 }
Dan Sandler842dd772014-05-15 09:36:47 -04004728 return wip;
4729 }
4730
Christoph Studer4600f9b2014-07-22 22:44:43 +02004731 /**
4732 * @hide
4733 */
4734 @Override
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004735 public RemoteViews makeContentView() {
4736 return makeMediaContentView();
Christoph Studer4600f9b2014-07-22 22:44:43 +02004737 }
4738
4739 /**
4740 * @hide
4741 */
4742 @Override
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004743 public RemoteViews makeBigContentView() {
4744 return makeMediaBigContentView();
Christoph Studer4600f9b2014-07-22 22:44:43 +02004745 }
4746
Selim Cinekcc10bfb2016-02-10 16:24:21 -08004747 /**
4748 * @hide
4749 */
4750 @Override
4751 public RemoteViews makeHeadsUpContentView() {
4752 RemoteViews expanded = makeMediaBigContentView();
4753 return expanded != null ? expanded : makeMediaContentView();
4754 }
4755
Dan Sandler842dd772014-05-15 09:36:47 -04004756 /** @hide */
4757 @Override
4758 public void addExtras(Bundle extras) {
4759 super.addExtras(extras);
4760
4761 if (mToken != null) {
4762 extras.putParcelable(EXTRA_MEDIA_SESSION, mToken);
4763 }
Bryan Mawhinneye191f902014-07-22 12:50:09 +01004764 if (mActionsToShowInCompact != null) {
4765 extras.putIntArray(EXTRA_COMPACT_ACTIONS, mActionsToShowInCompact);
4766 }
Dan Sandler842dd772014-05-15 09:36:47 -04004767 }
4768
Christoph Studer4600f9b2014-07-22 22:44:43 +02004769 /**
4770 * @hide
4771 */
4772 @Override
4773 protected void restoreFromExtras(Bundle extras) {
4774 super.restoreFromExtras(extras);
4775
4776 if (extras.containsKey(EXTRA_MEDIA_SESSION)) {
4777 mToken = extras.getParcelable(EXTRA_MEDIA_SESSION);
4778 }
4779 if (extras.containsKey(EXTRA_COMPACT_ACTIONS)) {
4780 mActionsToShowInCompact = extras.getIntArray(EXTRA_COMPACT_ACTIONS);
4781 }
4782 }
4783
Selim Cinek5bf069a2015-11-10 19:14:27 -05004784 private RemoteViews generateMediaActionButton(Action action, int color) {
Dan Sandler842dd772014-05-15 09:36:47 -04004785 final boolean tombstone = (action.actionIntent == null);
Selim Cinekf33b1112015-07-15 17:45:11 -07004786 RemoteViews button = new BuilderRemoteViews(mBuilder.mContext.getApplicationInfo(),
Alan Viverette3cb07a462014-06-06 14:19:53 -07004787 R.layout.notification_material_media_action);
Dan Sandler68079d52015-07-22 10:45:30 -04004788 button.setImageViewIcon(R.id.action0, action.getIcon());
Selim Cinek5bf069a2015-11-10 19:14:27 -05004789 button.setDrawableParameters(R.id.action0, false, -1, color, PorterDuff.Mode.SRC_ATOP,
4790 -1);
Dan Sandler842dd772014-05-15 09:36:47 -04004791 if (!tombstone) {
4792 button.setOnClickPendingIntent(R.id.action0, action.actionIntent);
4793 }
4794 button.setContentDescription(R.id.action0, action.title);
4795 return button;
4796 }
4797
4798 private RemoteViews makeMediaContentView() {
4799 RemoteViews view = mBuilder.applyStandardTemplate(
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02004800 R.layout.notification_template_material_media, false /* hasProgress */);
Dan Sandler842dd772014-05-15 09:36:47 -04004801
4802 final int numActions = mBuilder.mActions.size();
4803 final int N = mActionsToShowInCompact == null
4804 ? 0
4805 : Math.min(mActionsToShowInCompact.length, MAX_MEDIA_BUTTONS_IN_COMPACT);
4806 if (N > 0) {
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02004807 view.removeAllViews(com.android.internal.R.id.media_actions);
Dan Sandler842dd772014-05-15 09:36:47 -04004808 for (int i = 0; i < N; i++) {
4809 if (i >= numActions) {
4810 throw new IllegalArgumentException(String.format(
4811 "setShowActionsInCompactView: action %d out of bounds (max %d)",
4812 i, numActions - 1));
4813 }
4814
4815 final Action action = mBuilder.mActions.get(mActionsToShowInCompact[i]);
Selim Cinek5bf069a2015-11-10 19:14:27 -05004816 final RemoteViews button = generateMediaActionButton(action,
Adrian Roos4ff3b122016-02-01 12:26:13 -08004817 mBuilder.resolveContrastColor());
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02004818 view.addView(com.android.internal.R.id.media_actions, button);
Dan Sandler842dd772014-05-15 09:36:47 -04004819 }
4820 }
Selim Cinekfdc738f2016-01-27 20:04:27 -08004821 handleImage(view);
4822 // handle the content margin
Selim Cinek247fa012016-02-18 09:50:48 -08004823 int endMargin = mBuilder.mContext.getResources().getDimensionPixelSize(
4824 R.dimen.notification_content_margin_end);;
Selim Cinekfdc738f2016-01-27 20:04:27 -08004825 if (mBuilder.mN.mLargeIcon != null) {
Selim Cinek247fa012016-02-18 09:50:48 -08004826 endMargin += mBuilder.mContext.getResources().getDimensionPixelSize(
4827 R.dimen.notification_content_picture_margin);
Selim Cinekfdc738f2016-01-27 20:04:27 -08004828 }
4829 view.setViewLayoutMarginEnd(R.id.notification_main_column, endMargin);
Dan Sandler842dd772014-05-15 09:36:47 -04004830 return view;
4831 }
4832
4833 private RemoteViews makeMediaBigContentView() {
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02004834 final int actionCount = Math.min(mBuilder.mActions.size(), MAX_MEDIA_BUTTONS);
Selim Cinekcc10bfb2016-02-10 16:24:21 -08004835 // Dont add an expanded view if there is no more content to be revealed
4836 int actionsInCompact = mActionsToShowInCompact == null
4837 ? 0
4838 : Math.min(mActionsToShowInCompact.length, MAX_MEDIA_BUTTONS_IN_COMPACT);
4839 if (mBuilder.mN.mLargeIcon == null && actionCount <= actionsInCompact) {
4840 return null;
4841 }
Selim Cinek5bf069a2015-11-10 19:14:27 -05004842 RemoteViews big = mBuilder.applyStandardTemplate(
4843 R.layout.notification_template_material_big_media,
4844 false);
Dan Sandler842dd772014-05-15 09:36:47 -04004845
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02004846 if (actionCount > 0) {
4847 big.removeAllViews(com.android.internal.R.id.media_actions);
4848 for (int i = 0; i < actionCount; i++) {
Selim Cinek5bf069a2015-11-10 19:14:27 -05004849 final RemoteViews button = generateMediaActionButton(mBuilder.mActions.get(i),
Adrian Roos4ff3b122016-02-01 12:26:13 -08004850 mBuilder.resolveContrastColor());
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02004851 big.addView(com.android.internal.R.id.media_actions, button);
Dan Sandler842dd772014-05-15 09:36:47 -04004852 }
4853 }
Selim Cinek5bf069a2015-11-10 19:14:27 -05004854 handleImage(big);
Dan Sandler842dd772014-05-15 09:36:47 -04004855 return big;
4856 }
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02004857
Selim Cinek5bf069a2015-11-10 19:14:27 -05004858 private void handleImage(RemoteViews contentView) {
4859 if (mBuilder.mN.mLargeIcon != null) {
4860 contentView.setViewLayoutMarginEnd(R.id.line1, 0);
Selim Cinekc848c3a2016-01-13 15:27:30 -08004861 contentView.setViewLayoutMarginEnd(R.id.text, 0);
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02004862 }
4863 }
4864
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02004865 /**
4866 * @hide
4867 */
4868 @Override
4869 protected boolean hasProgress() {
4870 return false;
4871 }
Dan Sandler842dd772014-05-15 09:36:47 -04004872 }
Griff Hazen61a9e862014-05-22 16:05:19 -07004873
Selim Cinek593610c2016-02-16 18:42:57 -08004874 /**
4875 * Notification style for custom views that are decorated by the system
4876 *
4877 * <p>Instead of providing a notification that is completely custom, a developer can set this
4878 * style and still obtain system decorations like the notification header with the expand
4879 * affordance and actions.
4880 *
4881 * <p>Use {@link android.app.Notification.Builder#setCustomContentView(RemoteViews)},
4882 * {@link android.app.Notification.Builder#setCustomBigContentView(RemoteViews)} and
4883 * {@link android.app.Notification.Builder#setCustomHeadsUpContentView(RemoteViews)} to set the
4884 * corresponding custom views to display.
4885 *
4886 * To use this style with your Notification, feed it to
4887 * {@link Notification.Builder#setStyle(android.app.Notification.Style)} like so:
4888 * <pre class="prettyprint">
4889 * Notification noti = new Notification.Builder()
4890 * .setSmallIcon(R.drawable.ic_stat_player)
4891 * .setLargeIcon(albumArtBitmap))
4892 * .setCustomContentView(contentView);
4893 * .setStyle(<b>new Notification.DecoratedCustomViewStyle()</b>)
4894 * .build();
4895 * </pre>
4896 */
4897 public static class DecoratedCustomViewStyle extends Style {
4898
4899 public DecoratedCustomViewStyle() {
4900 }
4901
4902 public DecoratedCustomViewStyle(Builder builder) {
4903 setBuilder(builder);
4904 }
4905
4906 /**
4907 * @hide
4908 */
4909 public boolean displayCustomViewInline() {
4910 return true;
4911 }
4912
4913 /**
4914 * @hide
4915 */
4916 @Override
4917 public RemoteViews makeContentView() {
4918 return makeStandardTemplateWithCustomContent(mBuilder.mN.contentView);
4919 }
4920
4921 /**
4922 * @hide
4923 */
4924 @Override
4925 public RemoteViews makeBigContentView() {
4926 return makeDecoratedBigContentView();
4927 }
4928
4929 /**
4930 * @hide
4931 */
4932 @Override
4933 public RemoteViews makeHeadsUpContentView() {
4934 return makeDecoratedHeadsUpContentView();
4935 }
4936
Selim Cinek593610c2016-02-16 18:42:57 -08004937 private RemoteViews makeDecoratedHeadsUpContentView() {
4938 RemoteViews headsUpContentView = mBuilder.mN.headsUpContentView == null
4939 ? mBuilder.mN.contentView
4940 : mBuilder.mN.headsUpContentView;
4941 if (mBuilder.mActions.size() == 0) {
4942 return makeStandardTemplateWithCustomContent(headsUpContentView);
4943 }
4944 RemoteViews remoteViews = mBuilder.applyStandardTemplateWithActions(
4945 mBuilder.getBigBaseLayoutResource());
Selim Cinek247fa012016-02-18 09:50:48 -08004946 buildIntoRemoteViewContent(remoteViews, headsUpContentView);
Selim Cinek593610c2016-02-16 18:42:57 -08004947 return remoteViews;
4948 }
4949
Selim Cinek593610c2016-02-16 18:42:57 -08004950 private RemoteViews makeStandardTemplateWithCustomContent(RemoteViews customContent) {
4951 RemoteViews remoteViews = mBuilder.applyStandardTemplate(
4952 mBuilder.getBaseLayoutResource());
Selim Cinek247fa012016-02-18 09:50:48 -08004953 buildIntoRemoteViewContent(remoteViews, customContent);
Selim Cinek593610c2016-02-16 18:42:57 -08004954 return remoteViews;
4955 }
4956
Selim Cinek593610c2016-02-16 18:42:57 -08004957 private RemoteViews makeDecoratedBigContentView() {
4958 RemoteViews bigContentView = mBuilder.mN.bigContentView == null
4959 ? mBuilder.mN.contentView
4960 : mBuilder.mN.bigContentView;
4961 if (mBuilder.mActions.size() == 0) {
4962 return makeStandardTemplateWithCustomContent(bigContentView);
4963 }
4964 RemoteViews remoteViews = mBuilder.applyStandardTemplateWithActions(
4965 mBuilder.getBigBaseLayoutResource());
Selim Cinek247fa012016-02-18 09:50:48 -08004966 buildIntoRemoteViewContent(remoteViews, bigContentView);
Selim Cinek593610c2016-02-16 18:42:57 -08004967 return remoteViews;
4968 }
Selim Cinek247fa012016-02-18 09:50:48 -08004969
4970 private void buildIntoRemoteViewContent(RemoteViews remoteViews,
4971 RemoteViews customContent) {
Adrian Roos5081c0d2016-02-26 16:04:19 -08004972 if (customContent != null) {
Selim Cinekf91017e2016-03-14 12:25:09 -07004973 // Need to clone customContent before adding, because otherwise it can no longer be
4974 // parceled independently of remoteViews.
Adrian Roos5081c0d2016-02-26 16:04:19 -08004975 customContent = customContent.clone();
Selim Cinekf91017e2016-03-14 12:25:09 -07004976 remoteViews.removeAllViews(R.id.notification_main_column);
4977 remoteViews.addView(R.id.notification_main_column, customContent);
Adrian Roos5081c0d2016-02-26 16:04:19 -08004978 }
Selim Cinek247fa012016-02-18 09:50:48 -08004979 // also update the end margin if there is an image
4980 int endMargin = mBuilder.mContext.getResources().getDimensionPixelSize(
4981 R.dimen.notification_content_margin_end);
4982 if (mBuilder.mN.mLargeIcon != null) {
4983 endMargin += mBuilder.mContext.getResources().getDimensionPixelSize(
4984 R.dimen.notification_content_picture_margin);
4985 }
4986 remoteViews.setViewLayoutMarginEnd(R.id.notification_main_column, endMargin);
4987 }
Selim Cinek593610c2016-02-16 18:42:57 -08004988 }
4989
Selim Cinek03eb3b72016-02-18 10:39:45 -08004990 /**
4991 * Notification style for media custom views that are decorated by the system
4992 *
4993 * <p>Instead of providing a media notification that is completely custom, a developer can set
4994 * this style and still obtain system decorations like the notification header with the expand
4995 * affordance and actions.
4996 *
4997 * <p>Use {@link android.app.Notification.Builder#setCustomContentView(RemoteViews)},
4998 * {@link android.app.Notification.Builder#setCustomBigContentView(RemoteViews)} and
4999 * {@link android.app.Notification.Builder#setCustomHeadsUpContentView(RemoteViews)} to set the
5000 * corresponding custom views to display.
5001 *
5002 * To use this style with your Notification, feed it to
5003 * {@link Notification.Builder#setStyle(android.app.Notification.Style)} like so:
5004 * <pre class="prettyprint">
5005 * Notification noti = new Notification.Builder()
5006 * .setSmallIcon(R.drawable.ic_stat_player)
5007 * .setLargeIcon(albumArtBitmap))
5008 * .setCustomContentView(contentView);
5009 * .setStyle(<b>new Notification.DecoratedMediaCustomViewStyle()</b>
5010 * .setMediaSession(mySession))
5011 * .build();
5012 * </pre>
5013 *
5014 * @see android.app.Notification.DecoratedCustomViewStyle
5015 * @see android.app.Notification.MediaStyle
5016 */
5017 public static class DecoratedMediaCustomViewStyle extends MediaStyle {
5018
5019 public DecoratedMediaCustomViewStyle() {
5020 }
5021
5022 public DecoratedMediaCustomViewStyle(Builder builder) {
5023 setBuilder(builder);
5024 }
5025
5026 /**
5027 * @hide
5028 */
5029 public boolean displayCustomViewInline() {
5030 return true;
5031 }
5032
5033 /**
5034 * @hide
5035 */
5036 @Override
5037 public RemoteViews makeContentView() {
5038 RemoteViews remoteViews = super.makeContentView();
5039 return buildIntoRemoteView(remoteViews, R.id.notification_content_container,
5040 mBuilder.mN.contentView);
5041 }
5042
5043 /**
5044 * @hide
5045 */
5046 @Override
5047 public RemoteViews makeBigContentView() {
5048 RemoteViews customRemoteView = mBuilder.mN.bigContentView != null
5049 ? mBuilder.mN.bigContentView
5050 : mBuilder.mN.contentView;
5051 return makeBigContentViewWithCustomContent(customRemoteView);
5052 }
5053
5054 private RemoteViews makeBigContentViewWithCustomContent(RemoteViews customRemoteView) {
5055 RemoteViews remoteViews = super.makeBigContentView();
5056 if (remoteViews != null) {
5057 return buildIntoRemoteView(remoteViews, R.id.notification_main_column,
5058 customRemoteView);
5059 } else if (customRemoteView != mBuilder.mN.contentView){
5060 remoteViews = super.makeContentView();
5061 return buildIntoRemoteView(remoteViews, R.id.notification_content_container,
5062 customRemoteView);
5063 } else {
5064 return null;
5065 }
5066 }
5067
5068 /**
5069 * @hide
5070 */
5071 @Override
5072 public RemoteViews makeHeadsUpContentView() {
5073 RemoteViews customRemoteView = mBuilder.mN.headsUpContentView != null
5074 ? mBuilder.mN.headsUpContentView
5075 : mBuilder.mN.contentView;
5076 return makeBigContentViewWithCustomContent(customRemoteView);
5077 }
5078
5079 private RemoteViews buildIntoRemoteView(RemoteViews remoteViews, int id,
5080 RemoteViews customContent) {
Adrian Roos5081c0d2016-02-26 16:04:19 -08005081 if (customContent != null) {
Selim Cinekf91017e2016-03-14 12:25:09 -07005082 // Need to clone customContent before adding, because otherwise it can no longer be
5083 // parceled independently of remoteViews.
Adrian Roos5081c0d2016-02-26 16:04:19 -08005084 customContent = customContent.clone();
Selim Cinekf91017e2016-03-14 12:25:09 -07005085 remoteViews.removeAllViews(id);
5086 remoteViews.addView(id, customContent);
Adrian Roos5081c0d2016-02-26 16:04:19 -08005087 }
Selim Cinek03eb3b72016-02-18 10:39:45 -08005088 return remoteViews;
5089 }
5090 }
5091
Christoph Studer4600f9b2014-07-22 22:44:43 +02005092 // When adding a new Style subclass here, don't forget to update
5093 // Builder.getNotificationStyleClass.
5094
Griff Hazen61a9e862014-05-22 16:05:19 -07005095 /**
5096 * Extender interface for use with {@link Builder#extend}. Extenders may be used to add
5097 * metadata or change options on a notification builder.
5098 */
5099 public interface Extender {
5100 /**
5101 * Apply this extender to a notification builder.
5102 * @param builder the builder to be modified.
5103 * @return the build object for chaining.
5104 */
5105 public Builder extend(Builder builder);
5106 }
5107
5108 /**
5109 * Helper class to add wearable extensions to notifications.
5110 * <p class="note"> See
5111 * <a href="{@docRoot}wear/notifications/creating.html">Creating Notifications
5112 * for Android Wear</a> for more information on how to use this class.
5113 * <p>
5114 * To create a notification with wearable extensions:
5115 * <ol>
5116 * <li>Create a {@link android.app.Notification.Builder}, setting any desired
5117 * properties.
5118 * <li>Create a {@link android.app.Notification.WearableExtender}.
5119 * <li>Set wearable-specific properties using the
5120 * {@code add} and {@code set} methods of {@link android.app.Notification.WearableExtender}.
5121 * <li>Call {@link android.app.Notification.Builder#extend} to apply the extensions to a
5122 * notification.
5123 * <li>Post the notification to the notification system with the
5124 * {@code NotificationManager.notify(...)} methods.
5125 * </ol>
5126 *
5127 * <pre class="prettyprint">
5128 * Notification notif = new Notification.Builder(mContext)
5129 * .setContentTitle(&quot;New mail from &quot; + sender.toString())
5130 * .setContentText(subject)
5131 * .setSmallIcon(R.drawable.new_mail)
5132 * .extend(new Notification.WearableExtender()
5133 * .setContentIcon(R.drawable.new_mail))
5134 * .build();
5135 * NotificationManager notificationManger =
5136 * (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
5137 * notificationManger.notify(0, notif);</pre>
5138 *
5139 * <p>Wearable extensions can be accessed on an existing notification by using the
5140 * {@code WearableExtender(Notification)} constructor,
5141 * and then using the {@code get} methods to access values.
5142 *
5143 * <pre class="prettyprint">
5144 * Notification.WearableExtender wearableExtender = new Notification.WearableExtender(
5145 * notification);
Griff Hazen14f57992014-05-26 09:07:14 -07005146 * List&lt;Notification&gt; pages = wearableExtender.getPages();</pre>
Griff Hazen61a9e862014-05-22 16:05:19 -07005147 */
5148 public static final class WearableExtender implements Extender {
5149 /**
5150 * Sentinel value for an action index that is unset.
5151 */
5152 public static final int UNSET_ACTION_INDEX = -1;
5153
5154 /**
5155 * Size value for use with {@link #setCustomSizePreset} to show this notification with
5156 * default sizing.
5157 * <p>For custom display notifications created using {@link #setDisplayIntent},
Paul Soulosaa4f4bf2015-08-04 11:59:45 -07005158 * the default is {@link #SIZE_MEDIUM}. All other notifications size automatically based
Griff Hazen61a9e862014-05-22 16:05:19 -07005159 * on their content.
5160 */
5161 public static final int SIZE_DEFAULT = 0;
5162
5163 /**
5164 * Size value for use with {@link #setCustomSizePreset} to show this notification
5165 * with an extra small size.
5166 * <p>This value is only applicable for custom display notifications created using
5167 * {@link #setDisplayIntent}.
5168 */
5169 public static final int SIZE_XSMALL = 1;
5170
5171 /**
5172 * Size value for use with {@link #setCustomSizePreset} to show this notification
5173 * with a small size.
5174 * <p>This value is only applicable for custom display notifications created using
5175 * {@link #setDisplayIntent}.
5176 */
5177 public static final int SIZE_SMALL = 2;
5178
5179 /**
5180 * Size value for use with {@link #setCustomSizePreset} to show this notification
5181 * with a medium size.
5182 * <p>This value is only applicable for custom display notifications created using
5183 * {@link #setDisplayIntent}.
5184 */
5185 public static final int SIZE_MEDIUM = 3;
5186
5187 /**
5188 * Size value for use with {@link #setCustomSizePreset} to show this notification
5189 * with a large size.
5190 * <p>This value is only applicable for custom display notifications created using
5191 * {@link #setDisplayIntent}.
5192 */
5193 public static final int SIZE_LARGE = 4;
5194
Griff Hazend5f11f92014-05-27 15:40:09 -07005195 /**
5196 * Size value for use with {@link #setCustomSizePreset} to show this notification
5197 * full screen.
5198 * <p>This value is only applicable for custom display notifications created using
5199 * {@link #setDisplayIntent}.
5200 */
5201 public static final int SIZE_FULL_SCREEN = 5;
5202
Griff Hazen5f2edfc2014-09-29 16:28:44 -07005203 /**
5204 * Sentinel value for use with {@link #setHintScreenTimeout} to keep the screen on for a
5205 * short amount of time when this notification is displayed on the screen. This
5206 * is the default value.
5207 */
5208 public static final int SCREEN_TIMEOUT_SHORT = 0;
5209
5210 /**
5211 * Sentinel value for use with {@link #setHintScreenTimeout} to keep the screen on
5212 * for a longer amount of time when this notification is displayed on the screen.
5213 */
5214 public static final int SCREEN_TIMEOUT_LONG = -1;
5215
Griff Hazen61a9e862014-05-22 16:05:19 -07005216 /** Notification extra which contains wearable extensions */
5217 private static final String EXTRA_WEARABLE_EXTENSIONS = "android.wearable.EXTENSIONS";
5218
Pete Gastaf6781d2014-10-07 15:17:05 -04005219 // Keys within EXTRA_WEARABLE_EXTENSIONS for wearable options.
Griff Hazen61a9e862014-05-22 16:05:19 -07005220 private static final String KEY_ACTIONS = "actions";
5221 private static final String KEY_FLAGS = "flags";
5222 private static final String KEY_DISPLAY_INTENT = "displayIntent";
5223 private static final String KEY_PAGES = "pages";
5224 private static final String KEY_BACKGROUND = "background";
5225 private static final String KEY_CONTENT_ICON = "contentIcon";
5226 private static final String KEY_CONTENT_ICON_GRAVITY = "contentIconGravity";
5227 private static final String KEY_CONTENT_ACTION_INDEX = "contentActionIndex";
5228 private static final String KEY_CUSTOM_SIZE_PRESET = "customSizePreset";
5229 private static final String KEY_CUSTOM_CONTENT_HEIGHT = "customContentHeight";
5230 private static final String KEY_GRAVITY = "gravity";
Griff Hazen5f2edfc2014-09-29 16:28:44 -07005231 private static final String KEY_HINT_SCREEN_TIMEOUT = "hintScreenTimeout";
Griff Hazen61a9e862014-05-22 16:05:19 -07005232
5233 // Flags bitwise-ored to mFlags
5234 private static final int FLAG_CONTENT_INTENT_AVAILABLE_OFFLINE = 0x1;
5235 private static final int FLAG_HINT_HIDE_ICON = 1 << 1;
5236 private static final int FLAG_HINT_SHOW_BACKGROUND_ONLY = 1 << 2;
5237 private static final int FLAG_START_SCROLL_BOTTOM = 1 << 3;
Griff Hazen5f2edfc2014-09-29 16:28:44 -07005238 private static final int FLAG_HINT_AVOID_BACKGROUND_CLIPPING = 1 << 4;
Griff Hazen61a9e862014-05-22 16:05:19 -07005239
5240 // Default value for flags integer
5241 private static final int DEFAULT_FLAGS = FLAG_CONTENT_INTENT_AVAILABLE_OFFLINE;
5242
5243 private static final int DEFAULT_CONTENT_ICON_GRAVITY = Gravity.END;
5244 private static final int DEFAULT_GRAVITY = Gravity.BOTTOM;
5245
5246 private ArrayList<Action> mActions = new ArrayList<Action>();
5247 private int mFlags = DEFAULT_FLAGS;
5248 private PendingIntent mDisplayIntent;
5249 private ArrayList<Notification> mPages = new ArrayList<Notification>();
5250 private Bitmap mBackground;
5251 private int mContentIcon;
5252 private int mContentIconGravity = DEFAULT_CONTENT_ICON_GRAVITY;
5253 private int mContentActionIndex = UNSET_ACTION_INDEX;
5254 private int mCustomSizePreset = SIZE_DEFAULT;
5255 private int mCustomContentHeight;
5256 private int mGravity = DEFAULT_GRAVITY;
Griff Hazen5f2edfc2014-09-29 16:28:44 -07005257 private int mHintScreenTimeout;
Griff Hazen61a9e862014-05-22 16:05:19 -07005258
5259 /**
5260 * Create a {@link android.app.Notification.WearableExtender} with default
5261 * options.
5262 */
5263 public WearableExtender() {
5264 }
5265
5266 public WearableExtender(Notification notif) {
5267 Bundle wearableBundle = notif.extras.getBundle(EXTRA_WEARABLE_EXTENSIONS);
5268 if (wearableBundle != null) {
5269 List<Action> actions = wearableBundle.getParcelableArrayList(KEY_ACTIONS);
5270 if (actions != null) {
5271 mActions.addAll(actions);
5272 }
5273
5274 mFlags = wearableBundle.getInt(KEY_FLAGS, DEFAULT_FLAGS);
5275 mDisplayIntent = wearableBundle.getParcelable(KEY_DISPLAY_INTENT);
5276
5277 Notification[] pages = getNotificationArrayFromBundle(
5278 wearableBundle, KEY_PAGES);
5279 if (pages != null) {
5280 Collections.addAll(mPages, pages);
5281 }
5282
5283 mBackground = wearableBundle.getParcelable(KEY_BACKGROUND);
5284 mContentIcon = wearableBundle.getInt(KEY_CONTENT_ICON);
5285 mContentIconGravity = wearableBundle.getInt(KEY_CONTENT_ICON_GRAVITY,
5286 DEFAULT_CONTENT_ICON_GRAVITY);
5287 mContentActionIndex = wearableBundle.getInt(KEY_CONTENT_ACTION_INDEX,
5288 UNSET_ACTION_INDEX);
5289 mCustomSizePreset = wearableBundle.getInt(KEY_CUSTOM_SIZE_PRESET,
5290 SIZE_DEFAULT);
5291 mCustomContentHeight = wearableBundle.getInt(KEY_CUSTOM_CONTENT_HEIGHT);
5292 mGravity = wearableBundle.getInt(KEY_GRAVITY, DEFAULT_GRAVITY);
Griff Hazen5f2edfc2014-09-29 16:28:44 -07005293 mHintScreenTimeout = wearableBundle.getInt(KEY_HINT_SCREEN_TIMEOUT);
Griff Hazen61a9e862014-05-22 16:05:19 -07005294 }
5295 }
5296
5297 /**
5298 * Apply wearable extensions to a notification that is being built. This is typically
5299 * called by the {@link android.app.Notification.Builder#extend} method of
5300 * {@link android.app.Notification.Builder}.
5301 */
5302 @Override
5303 public Notification.Builder extend(Notification.Builder builder) {
5304 Bundle wearableBundle = new Bundle();
5305
5306 if (!mActions.isEmpty()) {
5307 wearableBundle.putParcelableArrayList(KEY_ACTIONS, mActions);
5308 }
5309 if (mFlags != DEFAULT_FLAGS) {
5310 wearableBundle.putInt(KEY_FLAGS, mFlags);
5311 }
5312 if (mDisplayIntent != null) {
5313 wearableBundle.putParcelable(KEY_DISPLAY_INTENT, mDisplayIntent);
5314 }
5315 if (!mPages.isEmpty()) {
5316 wearableBundle.putParcelableArray(KEY_PAGES, mPages.toArray(
5317 new Notification[mPages.size()]));
5318 }
5319 if (mBackground != null) {
5320 wearableBundle.putParcelable(KEY_BACKGROUND, mBackground);
5321 }
5322 if (mContentIcon != 0) {
5323 wearableBundle.putInt(KEY_CONTENT_ICON, mContentIcon);
5324 }
5325 if (mContentIconGravity != DEFAULT_CONTENT_ICON_GRAVITY) {
5326 wearableBundle.putInt(KEY_CONTENT_ICON_GRAVITY, mContentIconGravity);
5327 }
5328 if (mContentActionIndex != UNSET_ACTION_INDEX) {
5329 wearableBundle.putInt(KEY_CONTENT_ACTION_INDEX,
5330 mContentActionIndex);
5331 }
5332 if (mCustomSizePreset != SIZE_DEFAULT) {
5333 wearableBundle.putInt(KEY_CUSTOM_SIZE_PRESET, mCustomSizePreset);
5334 }
5335 if (mCustomContentHeight != 0) {
5336 wearableBundle.putInt(KEY_CUSTOM_CONTENT_HEIGHT, mCustomContentHeight);
5337 }
5338 if (mGravity != DEFAULT_GRAVITY) {
5339 wearableBundle.putInt(KEY_GRAVITY, mGravity);
5340 }
Griff Hazen5f2edfc2014-09-29 16:28:44 -07005341 if (mHintScreenTimeout != 0) {
5342 wearableBundle.putInt(KEY_HINT_SCREEN_TIMEOUT, mHintScreenTimeout);
5343 }
Griff Hazen61a9e862014-05-22 16:05:19 -07005344
5345 builder.getExtras().putBundle(EXTRA_WEARABLE_EXTENSIONS, wearableBundle);
5346 return builder;
5347 }
5348
5349 @Override
5350 public WearableExtender clone() {
5351 WearableExtender that = new WearableExtender();
5352 that.mActions = new ArrayList<Action>(this.mActions);
5353 that.mFlags = this.mFlags;
5354 that.mDisplayIntent = this.mDisplayIntent;
5355 that.mPages = new ArrayList<Notification>(this.mPages);
5356 that.mBackground = this.mBackground;
5357 that.mContentIcon = this.mContentIcon;
5358 that.mContentIconGravity = this.mContentIconGravity;
5359 that.mContentActionIndex = this.mContentActionIndex;
5360 that.mCustomSizePreset = this.mCustomSizePreset;
5361 that.mCustomContentHeight = this.mCustomContentHeight;
5362 that.mGravity = this.mGravity;
Griff Hazen5f2edfc2014-09-29 16:28:44 -07005363 that.mHintScreenTimeout = this.mHintScreenTimeout;
Griff Hazen61a9e862014-05-22 16:05:19 -07005364 return that;
5365 }
5366
5367 /**
5368 * Add a wearable action to this notification.
5369 *
5370 * <p>When wearable actions are added using this method, the set of actions that
5371 * show on a wearable device splits from devices that only show actions added
5372 * using {@link android.app.Notification.Builder#addAction}. This allows for customization
5373 * of which actions display on different devices.
5374 *
5375 * @param action the action to add to this notification
5376 * @return this object for method chaining
5377 * @see android.app.Notification.Action
5378 */
5379 public WearableExtender addAction(Action action) {
5380 mActions.add(action);
5381 return this;
5382 }
5383
5384 /**
5385 * Adds wearable actions to this notification.
5386 *
5387 * <p>When wearable actions are added using this method, the set of actions that
5388 * show on a wearable device splits from devices that only show actions added
5389 * using {@link android.app.Notification.Builder#addAction}. This allows for customization
5390 * of which actions display on different devices.
5391 *
5392 * @param actions the actions to add to this notification
5393 * @return this object for method chaining
5394 * @see android.app.Notification.Action
5395 */
5396 public WearableExtender addActions(List<Action> actions) {
5397 mActions.addAll(actions);
5398 return this;
5399 }
5400
5401 /**
5402 * Clear all wearable actions present on this builder.
5403 * @return this object for method chaining.
5404 * @see #addAction
5405 */
5406 public WearableExtender clearActions() {
5407 mActions.clear();
5408 return this;
5409 }
5410
5411 /**
5412 * Get the wearable actions present on this notification.
5413 */
5414 public List<Action> getActions() {
5415 return mActions;
5416 }
5417
5418 /**
5419 * Set an intent to launch inside of an activity view when displaying
Griff Hazen14f57992014-05-26 09:07:14 -07005420 * this notification. The {@link PendingIntent} provided should be for an activity.
5421 *
5422 * <pre class="prettyprint">
5423 * Intent displayIntent = new Intent(context, MyDisplayActivity.class);
5424 * PendingIntent displayPendingIntent = PendingIntent.getActivity(context,
5425 * 0, displayIntent, PendingIntent.FLAG_UPDATE_CURRENT);
5426 * Notification notif = new Notification.Builder(context)
5427 * .extend(new Notification.WearableExtender()
5428 * .setDisplayIntent(displayPendingIntent)
5429 * .setCustomSizePreset(Notification.WearableExtender.SIZE_MEDIUM))
5430 * .build();</pre>
5431 *
5432 * <p>The activity to launch needs to allow embedding, must be exported, and
Griff Hazen831ca9d2014-06-17 00:38:38 -07005433 * should have an empty task affinity. It is also recommended to use the device
5434 * default light theme.
Griff Hazen14f57992014-05-26 09:07:14 -07005435 *
5436 * <p>Example AndroidManifest.xml entry:
5437 * <pre class="prettyprint">
5438 * &lt;activity android:name=&quot;com.example.MyDisplayActivity&quot;
5439 * android:exported=&quot;true&quot;
5440 * android:allowEmbedded=&quot;true&quot;
Griff Hazen831ca9d2014-06-17 00:38:38 -07005441 * android:taskAffinity=&quot;&quot;
5442 * android:theme=&quot;@android:style/Theme.DeviceDefault.Light&quot; /&gt;</pre>
Griff Hazen61a9e862014-05-22 16:05:19 -07005443 *
5444 * @param intent the {@link PendingIntent} for an activity
5445 * @return this object for method chaining
5446 * @see android.app.Notification.WearableExtender#getDisplayIntent
5447 */
5448 public WearableExtender setDisplayIntent(PendingIntent intent) {
5449 mDisplayIntent = intent;
5450 return this;
5451 }
5452
5453 /**
5454 * Get the intent to launch inside of an activity view when displaying this
5455 * notification. This {@code PendingIntent} should be for an activity.
5456 */
5457 public PendingIntent getDisplayIntent() {
5458 return mDisplayIntent;
5459 }
5460
5461 /**
5462 * Add an additional page of content to display with this notification. The current
5463 * notification forms the first page, and pages added using this function form
5464 * subsequent pages. This field can be used to separate a notification into multiple
5465 * sections.
5466 *
5467 * @param page the notification to add as another page
5468 * @return this object for method chaining
5469 * @see android.app.Notification.WearableExtender#getPages
5470 */
5471 public WearableExtender addPage(Notification page) {
5472 mPages.add(page);
5473 return this;
5474 }
5475
5476 /**
5477 * Add additional pages of content to display with this notification. The current
5478 * notification forms the first page, and pages added using this function form
5479 * subsequent pages. This field can be used to separate a notification into multiple
5480 * sections.
5481 *
5482 * @param pages a list of notifications
5483 * @return this object for method chaining
5484 * @see android.app.Notification.WearableExtender#getPages
5485 */
5486 public WearableExtender addPages(List<Notification> pages) {
5487 mPages.addAll(pages);
5488 return this;
5489 }
5490
5491 /**
5492 * Clear all additional pages present on this builder.
5493 * @return this object for method chaining.
5494 * @see #addPage
5495 */
5496 public WearableExtender clearPages() {
5497 mPages.clear();
5498 return this;
5499 }
5500
5501 /**
5502 * Get the array of additional pages of content for displaying this notification. The
5503 * current notification forms the first page, and elements within this array form
5504 * subsequent pages. This field can be used to separate a notification into multiple
5505 * sections.
5506 * @return the pages for this notification
5507 */
5508 public List<Notification> getPages() {
5509 return mPages;
5510 }
5511
5512 /**
5513 * Set a background image to be displayed behind the notification content.
5514 * Contrary to the {@link android.app.Notification.BigPictureStyle}, this background
5515 * will work with any notification style.
5516 *
5517 * @param background the background bitmap
5518 * @return this object for method chaining
5519 * @see android.app.Notification.WearableExtender#getBackground
5520 */
5521 public WearableExtender setBackground(Bitmap background) {
5522 mBackground = background;
5523 return this;
5524 }
5525
5526 /**
5527 * Get a background image to be displayed behind the notification content.
5528 * Contrary to the {@link android.app.Notification.BigPictureStyle}, this background
5529 * will work with any notification style.
5530 *
5531 * @return the background image
5532 * @see android.app.Notification.WearableExtender#setBackground
5533 */
5534 public Bitmap getBackground() {
5535 return mBackground;
5536 }
5537
5538 /**
5539 * Set an icon that goes with the content of this notification.
5540 */
5541 public WearableExtender setContentIcon(int icon) {
5542 mContentIcon = icon;
5543 return this;
5544 }
5545
5546 /**
5547 * Get an icon that goes with the content of this notification.
5548 */
5549 public int getContentIcon() {
5550 return mContentIcon;
5551 }
5552
5553 /**
5554 * Set the gravity that the content icon should have within the notification display.
5555 * Supported values include {@link android.view.Gravity#START} and
5556 * {@link android.view.Gravity#END}. The default value is {@link android.view.Gravity#END}.
5557 * @see #setContentIcon
5558 */
5559 public WearableExtender setContentIconGravity(int contentIconGravity) {
5560 mContentIconGravity = contentIconGravity;
5561 return this;
5562 }
5563
5564 /**
5565 * Get the gravity that the content icon should have within the notification display.
5566 * Supported values include {@link android.view.Gravity#START} and
5567 * {@link android.view.Gravity#END}. The default value is {@link android.view.Gravity#END}.
5568 * @see #getContentIcon
5569 */
5570 public int getContentIconGravity() {
5571 return mContentIconGravity;
5572 }
5573
5574 /**
5575 * Set an action from this notification's actions to be clickable with the content of
Griff Hazen14f57992014-05-26 09:07:14 -07005576 * this notification. This action will no longer display separately from the
5577 * notification's content.
5578 *
Griff Hazenca48d352014-05-28 22:37:13 -07005579 * <p>For notifications with multiple pages, child pages can also have content actions
Griff Hazen14f57992014-05-26 09:07:14 -07005580 * set, although the list of available actions comes from the main notification and not
5581 * from the child page's notification.
5582 *
5583 * @param actionIndex The index of the action to hoist onto the current notification page.
5584 * If wearable actions were added to the main notification, this index
5585 * will apply to that list, otherwise it will apply to the regular
5586 * actions list.
Griff Hazen61a9e862014-05-22 16:05:19 -07005587 */
5588 public WearableExtender setContentAction(int actionIndex) {
5589 mContentActionIndex = actionIndex;
5590 return this;
5591 }
5592
5593 /**
Griff Hazenca48d352014-05-28 22:37:13 -07005594 * Get the index of the notification action, if any, that was specified as being clickable
5595 * with the content of this notification. This action will no longer display separately
Griff Hazen14f57992014-05-26 09:07:14 -07005596 * from the notification's content.
Griff Hazen61a9e862014-05-22 16:05:19 -07005597 *
Griff Hazenca48d352014-05-28 22:37:13 -07005598 * <p>For notifications with multiple pages, child pages can also have content actions
Griff Hazen14f57992014-05-26 09:07:14 -07005599 * set, although the list of available actions comes from the main notification and not
5600 * from the child page's notification.
5601 *
5602 * <p>If wearable specific actions were added to the main notification, this index will
5603 * apply to that list, otherwise it will apply to the regular actions list.
Griff Hazenca48d352014-05-28 22:37:13 -07005604 *
5605 * @return the action index or {@link #UNSET_ACTION_INDEX} if no action was selected.
Griff Hazen61a9e862014-05-22 16:05:19 -07005606 */
5607 public int getContentAction() {
5608 return mContentActionIndex;
5609 }
5610
5611 /**
5612 * Set the gravity that this notification should have within the available viewport space.
5613 * Supported values include {@link android.view.Gravity#TOP},
5614 * {@link android.view.Gravity#CENTER_VERTICAL} and {@link android.view.Gravity#BOTTOM}.
5615 * The default value is {@link android.view.Gravity#BOTTOM}.
5616 */
5617 public WearableExtender setGravity(int gravity) {
5618 mGravity = gravity;
5619 return this;
5620 }
5621
5622 /**
5623 * Get the gravity that this notification should have within the available viewport space.
5624 * Supported values include {@link android.view.Gravity#TOP},
5625 * {@link android.view.Gravity#CENTER_VERTICAL} and {@link android.view.Gravity#BOTTOM}.
5626 * The default value is {@link android.view.Gravity#BOTTOM}.
5627 */
5628 public int getGravity() {
5629 return mGravity;
5630 }
5631
5632 /**
5633 * Set the custom size preset for the display of this notification out of the available
5634 * presets found in {@link android.app.Notification.WearableExtender}, e.g.
5635 * {@link #SIZE_LARGE}.
5636 * <p>Some custom size presets are only applicable for custom display notifications created
5637 * using {@link android.app.Notification.WearableExtender#setDisplayIntent}. Check the
5638 * documentation for the preset in question. See also
5639 * {@link #setCustomContentHeight} and {@link #getCustomSizePreset}.
5640 */
5641 public WearableExtender setCustomSizePreset(int sizePreset) {
5642 mCustomSizePreset = sizePreset;
5643 return this;
5644 }
5645
5646 /**
5647 * Get the custom size preset for the display of this notification out of the available
5648 * presets found in {@link android.app.Notification.WearableExtender}, e.g.
5649 * {@link #SIZE_LARGE}.
5650 * <p>Some custom size presets are only applicable for custom display notifications created
5651 * using {@link #setDisplayIntent}. Check the documentation for the preset in question.
5652 * See also {@link #setCustomContentHeight} and {@link #setCustomSizePreset}.
5653 */
5654 public int getCustomSizePreset() {
5655 return mCustomSizePreset;
5656 }
5657
5658 /**
5659 * Set the custom height in pixels for the display of this notification's content.
5660 * <p>This option is only available for custom display notifications created
5661 * using {@link android.app.Notification.WearableExtender#setDisplayIntent}. See also
5662 * {@link android.app.Notification.WearableExtender#setCustomSizePreset} and
5663 * {@link #getCustomContentHeight}.
5664 */
5665 public WearableExtender setCustomContentHeight(int height) {
5666 mCustomContentHeight = height;
5667 return this;
5668 }
5669
5670 /**
5671 * Get the custom height in pixels for the display of this notification's content.
5672 * <p>This option is only available for custom display notifications created
5673 * using {@link #setDisplayIntent}. See also {@link #setCustomSizePreset} and
5674 * {@link #setCustomContentHeight}.
5675 */
5676 public int getCustomContentHeight() {
5677 return mCustomContentHeight;
5678 }
5679
5680 /**
5681 * Set whether the scrolling position for the contents of this notification should start
5682 * at the bottom of the contents instead of the top when the contents are too long to
5683 * display within the screen. Default is false (start scroll at the top).
5684 */
5685 public WearableExtender setStartScrollBottom(boolean startScrollBottom) {
5686 setFlag(FLAG_START_SCROLL_BOTTOM, startScrollBottom);
5687 return this;
5688 }
5689
5690 /**
5691 * Get whether the scrolling position for the contents of this notification should start
5692 * at the bottom of the contents instead of the top when the contents are too long to
5693 * display within the screen. Default is false (start scroll at the top).
5694 */
5695 public boolean getStartScrollBottom() {
5696 return (mFlags & FLAG_START_SCROLL_BOTTOM) != 0;
5697 }
5698
5699 /**
5700 * Set whether the content intent is available when the wearable device is not connected
5701 * to a companion device. The user can still trigger this intent when the wearable device
5702 * is offline, but a visual hint will indicate that the content intent may not be available.
5703 * Defaults to true.
5704 */
5705 public WearableExtender setContentIntentAvailableOffline(
5706 boolean contentIntentAvailableOffline) {
5707 setFlag(FLAG_CONTENT_INTENT_AVAILABLE_OFFLINE, contentIntentAvailableOffline);
5708 return this;
5709 }
5710
5711 /**
5712 * Get whether the content intent is available when the wearable device is not connected
5713 * to a companion device. The user can still trigger this intent when the wearable device
5714 * is offline, but a visual hint will indicate that the content intent may not be available.
5715 * Defaults to true.
5716 */
5717 public boolean getContentIntentAvailableOffline() {
5718 return (mFlags & FLAG_CONTENT_INTENT_AVAILABLE_OFFLINE) != 0;
5719 }
5720
5721 /**
5722 * Set a hint that this notification's icon should not be displayed.
5723 * @param hintHideIcon {@code true} to hide the icon, {@code false} otherwise.
5724 * @return this object for method chaining
5725 */
5726 public WearableExtender setHintHideIcon(boolean hintHideIcon) {
5727 setFlag(FLAG_HINT_HIDE_ICON, hintHideIcon);
5728 return this;
5729 }
5730
5731 /**
5732 * Get a hint that this notification's icon should not be displayed.
5733 * @return {@code true} if this icon should not be displayed, false otherwise.
5734 * The default value is {@code false} if this was never set.
5735 */
5736 public boolean getHintHideIcon() {
5737 return (mFlags & FLAG_HINT_HIDE_ICON) != 0;
5738 }
5739
5740 /**
5741 * Set a visual hint that only the background image of this notification should be
5742 * displayed, and other semantic content should be hidden. This hint is only applicable
5743 * to sub-pages added using {@link #addPage}.
5744 */
5745 public WearableExtender setHintShowBackgroundOnly(boolean hintShowBackgroundOnly) {
5746 setFlag(FLAG_HINT_SHOW_BACKGROUND_ONLY, hintShowBackgroundOnly);
5747 return this;
5748 }
5749
5750 /**
5751 * Get a visual hint that only the background image of this notification should be
5752 * displayed, and other semantic content should be hidden. This hint is only applicable
5753 * to sub-pages added using {@link android.app.Notification.WearableExtender#addPage}.
5754 */
5755 public boolean getHintShowBackgroundOnly() {
5756 return (mFlags & FLAG_HINT_SHOW_BACKGROUND_ONLY) != 0;
5757 }
5758
Griff Hazen5f2edfc2014-09-29 16:28:44 -07005759 /**
Griff Hazen9c5be4e2014-11-17 17:47:50 -08005760 * Set a hint that this notification's background should not be clipped if possible,
5761 * and should instead be resized to fully display on the screen, retaining the aspect
5762 * ratio of the image. This can be useful for images like barcodes or qr codes.
Griff Hazen5f2edfc2014-09-29 16:28:44 -07005763 * @param hintAvoidBackgroundClipping {@code true} to avoid clipping if possible.
5764 * @return this object for method chaining
5765 */
5766 public WearableExtender setHintAvoidBackgroundClipping(
5767 boolean hintAvoidBackgroundClipping) {
5768 setFlag(FLAG_HINT_AVOID_BACKGROUND_CLIPPING, hintAvoidBackgroundClipping);
5769 return this;
5770 }
5771
5772 /**
Griff Hazen9c5be4e2014-11-17 17:47:50 -08005773 * Get a hint that this notification's background should not be clipped if possible,
5774 * and should instead be resized to fully display on the screen, retaining the aspect
5775 * ratio of the image. This can be useful for images like barcodes or qr codes.
Griff Hazen5f2edfc2014-09-29 16:28:44 -07005776 * @return {@code true} if it's ok if the background is clipped on the screen, false
5777 * otherwise. The default value is {@code false} if this was never set.
5778 */
5779 public boolean getHintAvoidBackgroundClipping() {
5780 return (mFlags & FLAG_HINT_AVOID_BACKGROUND_CLIPPING) != 0;
5781 }
5782
5783 /**
5784 * Set a hint that the screen should remain on for at least this duration when
5785 * this notification is displayed on the screen.
5786 * @param timeout The requested screen timeout in milliseconds. Can also be either
5787 * {@link #SCREEN_TIMEOUT_SHORT} or {@link #SCREEN_TIMEOUT_LONG}.
5788 * @return this object for method chaining
5789 */
5790 public WearableExtender setHintScreenTimeout(int timeout) {
5791 mHintScreenTimeout = timeout;
5792 return this;
5793 }
5794
5795 /**
5796 * Get the duration, in milliseconds, that the screen should remain on for
5797 * when this notification is displayed.
5798 * @return the duration in milliseconds if > 0, or either one of the sentinel values
5799 * {@link #SCREEN_TIMEOUT_SHORT} or {@link #SCREEN_TIMEOUT_LONG}.
5800 */
5801 public int getHintScreenTimeout() {
5802 return mHintScreenTimeout;
5803 }
5804
Griff Hazen61a9e862014-05-22 16:05:19 -07005805 private void setFlag(int mask, boolean value) {
5806 if (value) {
5807 mFlags |= mask;
5808 } else {
5809 mFlags &= ~mask;
5810 }
5811 }
5812 }
5813
5814 /**
Zhen Yu Song9d5528b2014-11-14 15:34:39 -08005815 * <p>Helper class to add Android Auto extensions to notifications. To create a notification
5816 * with car extensions:
5817 *
5818 * <ol>
5819 * <li>Create an {@link Notification.Builder}, setting any desired
5820 * properties.
5821 * <li>Create a {@link CarExtender}.
5822 * <li>Set car-specific properties using the {@code add} and {@code set} methods of
5823 * {@link CarExtender}.
5824 * <li>Call {@link Notification.Builder#extend(Notification.Extender)}
5825 * to apply the extensions to a notification.
5826 * </ol>
5827 *
5828 * <pre class="prettyprint">
5829 * Notification notification = new Notification.Builder(context)
5830 * ...
5831 * .extend(new CarExtender()
5832 * .set*(...))
5833 * .build();
5834 * </pre>
5835 *
5836 * <p>Car extensions can be accessed on an existing notification by using the
5837 * {@code CarExtender(Notification)} constructor, and then using the {@code get} methods
5838 * to access values.
5839 */
5840 public static final class CarExtender implements Extender {
5841 private static final String TAG = "CarExtender";
5842
5843 private static final String EXTRA_CAR_EXTENDER = "android.car.EXTENSIONS";
5844 private static final String EXTRA_LARGE_ICON = "large_icon";
5845 private static final String EXTRA_CONVERSATION = "car_conversation";
5846 private static final String EXTRA_COLOR = "app_color";
5847
5848 private Bitmap mLargeIcon;
5849 private UnreadConversation mUnreadConversation;
5850 private int mColor = Notification.COLOR_DEFAULT;
5851
5852 /**
5853 * Create a {@link CarExtender} with default options.
5854 */
5855 public CarExtender() {
5856 }
5857
5858 /**
5859 * Create a {@link CarExtender} from the CarExtender options of an existing Notification.
5860 *
5861 * @param notif The notification from which to copy options.
5862 */
5863 public CarExtender(Notification notif) {
5864 Bundle carBundle = notif.extras == null ?
5865 null : notif.extras.getBundle(EXTRA_CAR_EXTENDER);
5866 if (carBundle != null) {
5867 mLargeIcon = carBundle.getParcelable(EXTRA_LARGE_ICON);
5868 mColor = carBundle.getInt(EXTRA_COLOR, Notification.COLOR_DEFAULT);
5869
5870 Bundle b = carBundle.getBundle(EXTRA_CONVERSATION);
5871 mUnreadConversation = UnreadConversation.getUnreadConversationFromBundle(b);
5872 }
5873 }
5874
5875 /**
5876 * Apply car extensions to a notification that is being built. This is typically called by
5877 * the {@link Notification.Builder#extend(Notification.Extender)}
5878 * method of {@link Notification.Builder}.
5879 */
5880 @Override
5881 public Notification.Builder extend(Notification.Builder builder) {
5882 Bundle carExtensions = new Bundle();
5883
5884 if (mLargeIcon != null) {
5885 carExtensions.putParcelable(EXTRA_LARGE_ICON, mLargeIcon);
5886 }
5887 if (mColor != Notification.COLOR_DEFAULT) {
5888 carExtensions.putInt(EXTRA_COLOR, mColor);
5889 }
5890
5891 if (mUnreadConversation != null) {
5892 Bundle b = mUnreadConversation.getBundleForUnreadConversation();
5893 carExtensions.putBundle(EXTRA_CONVERSATION, b);
5894 }
5895
5896 builder.getExtras().putBundle(EXTRA_CAR_EXTENDER, carExtensions);
5897 return builder;
5898 }
5899
5900 /**
5901 * Sets the accent color to use when Android Auto presents the notification.
5902 *
5903 * Android Auto uses the color set with {@link Notification.Builder#setColor(int)}
5904 * to accent the displayed notification. However, not all colors are acceptable in an
5905 * automotive setting. This method can be used to override the color provided in the
5906 * notification in such a situation.
5907 */
Tor Norbye80756e32015-03-02 09:39:27 -08005908 public CarExtender setColor(@ColorInt int color) {
Zhen Yu Song9d5528b2014-11-14 15:34:39 -08005909 mColor = color;
5910 return this;
5911 }
5912
5913 /**
5914 * Gets the accent color.
5915 *
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005916 * @see #setColor
Zhen Yu Song9d5528b2014-11-14 15:34:39 -08005917 */
Tor Norbye80756e32015-03-02 09:39:27 -08005918 @ColorInt
Zhen Yu Song9d5528b2014-11-14 15:34:39 -08005919 public int getColor() {
5920 return mColor;
5921 }
5922
5923 /**
5924 * Sets the large icon of the car notification.
5925 *
5926 * If no large icon is set in the extender, Android Auto will display the icon
5927 * specified by {@link Notification.Builder#setLargeIcon(android.graphics.Bitmap)}
5928 *
5929 * @param largeIcon The large icon to use in the car notification.
5930 * @return This object for method chaining.
5931 */
5932 public CarExtender setLargeIcon(Bitmap largeIcon) {
5933 mLargeIcon = largeIcon;
5934 return this;
5935 }
5936
5937 /**
5938 * Gets the large icon used in this car notification, or null if no icon has been set.
5939 *
5940 * @return The large icon for the car notification.
5941 * @see CarExtender#setLargeIcon
5942 */
5943 public Bitmap getLargeIcon() {
5944 return mLargeIcon;
5945 }
5946
5947 /**
5948 * Sets the unread conversation in a message notification.
5949 *
5950 * @param unreadConversation The unread part of the conversation this notification conveys.
5951 * @return This object for method chaining.
5952 */
5953 public CarExtender setUnreadConversation(UnreadConversation unreadConversation) {
5954 mUnreadConversation = unreadConversation;
5955 return this;
5956 }
5957
5958 /**
5959 * Returns the unread conversation conveyed by this notification.
5960 * @see #setUnreadConversation(UnreadConversation)
5961 */
5962 public UnreadConversation getUnreadConversation() {
5963 return mUnreadConversation;
5964 }
5965
5966 /**
5967 * A class which holds the unread messages from a conversation.
5968 */
5969 public static class UnreadConversation {
5970 private static final String KEY_AUTHOR = "author";
5971 private static final String KEY_TEXT = "text";
5972 private static final String KEY_MESSAGES = "messages";
5973 private static final String KEY_REMOTE_INPUT = "remote_input";
5974 private static final String KEY_ON_REPLY = "on_reply";
5975 private static final String KEY_ON_READ = "on_read";
5976 private static final String KEY_PARTICIPANTS = "participants";
5977 private static final String KEY_TIMESTAMP = "timestamp";
5978
5979 private final String[] mMessages;
5980 private final RemoteInput mRemoteInput;
5981 private final PendingIntent mReplyPendingIntent;
5982 private final PendingIntent mReadPendingIntent;
5983 private final String[] mParticipants;
5984 private final long mLatestTimestamp;
5985
5986 UnreadConversation(String[] messages, RemoteInput remoteInput,
5987 PendingIntent replyPendingIntent, PendingIntent readPendingIntent,
5988 String[] participants, long latestTimestamp) {
5989 mMessages = messages;
5990 mRemoteInput = remoteInput;
5991 mReadPendingIntent = readPendingIntent;
5992 mReplyPendingIntent = replyPendingIntent;
5993 mParticipants = participants;
5994 mLatestTimestamp = latestTimestamp;
5995 }
5996
5997 /**
5998 * Gets the list of messages conveyed by this notification.
5999 */
6000 public String[] getMessages() {
6001 return mMessages;
6002 }
6003
6004 /**
6005 * Gets the remote input that will be used to convey the response to a message list, or
6006 * null if no such remote input exists.
6007 */
6008 public RemoteInput getRemoteInput() {
6009 return mRemoteInput;
6010 }
6011
6012 /**
6013 * Gets the pending intent that will be triggered when the user replies to this
6014 * notification.
6015 */
6016 public PendingIntent getReplyPendingIntent() {
6017 return mReplyPendingIntent;
6018 }
6019
6020 /**
6021 * Gets the pending intent that Android Auto will send after it reads aloud all messages
6022 * in this object's message list.
6023 */
6024 public PendingIntent getReadPendingIntent() {
6025 return mReadPendingIntent;
6026 }
6027
6028 /**
6029 * Gets the participants in the conversation.
6030 */
6031 public String[] getParticipants() {
6032 return mParticipants;
6033 }
6034
6035 /**
6036 * Gets the firs participant in the conversation.
6037 */
6038 public String getParticipant() {
6039 return mParticipants.length > 0 ? mParticipants[0] : null;
6040 }
6041
6042 /**
6043 * Gets the timestamp of the conversation.
6044 */
6045 public long getLatestTimestamp() {
6046 return mLatestTimestamp;
6047 }
6048
6049 Bundle getBundleForUnreadConversation() {
6050 Bundle b = new Bundle();
6051 String author = null;
6052 if (mParticipants != null && mParticipants.length > 1) {
6053 author = mParticipants[0];
6054 }
6055 Parcelable[] messages = new Parcelable[mMessages.length];
6056 for (int i = 0; i < messages.length; i++) {
6057 Bundle m = new Bundle();
6058 m.putString(KEY_TEXT, mMessages[i]);
6059 m.putString(KEY_AUTHOR, author);
6060 messages[i] = m;
6061 }
6062 b.putParcelableArray(KEY_MESSAGES, messages);
6063 if (mRemoteInput != null) {
6064 b.putParcelable(KEY_REMOTE_INPUT, mRemoteInput);
6065 }
6066 b.putParcelable(KEY_ON_REPLY, mReplyPendingIntent);
6067 b.putParcelable(KEY_ON_READ, mReadPendingIntent);
6068 b.putStringArray(KEY_PARTICIPANTS, mParticipants);
6069 b.putLong(KEY_TIMESTAMP, mLatestTimestamp);
6070 return b;
6071 }
6072
6073 static UnreadConversation getUnreadConversationFromBundle(Bundle b) {
6074 if (b == null) {
6075 return null;
6076 }
6077 Parcelable[] parcelableMessages = b.getParcelableArray(KEY_MESSAGES);
6078 String[] messages = null;
6079 if (parcelableMessages != null) {
6080 String[] tmp = new String[parcelableMessages.length];
6081 boolean success = true;
6082 for (int i = 0; i < tmp.length; i++) {
6083 if (!(parcelableMessages[i] instanceof Bundle)) {
6084 success = false;
6085 break;
6086 }
6087 tmp[i] = ((Bundle) parcelableMessages[i]).getString(KEY_TEXT);
6088 if (tmp[i] == null) {
6089 success = false;
6090 break;
6091 }
6092 }
6093 if (success) {
6094 messages = tmp;
6095 } else {
6096 return null;
6097 }
6098 }
6099
6100 PendingIntent onRead = b.getParcelable(KEY_ON_READ);
6101 PendingIntent onReply = b.getParcelable(KEY_ON_REPLY);
6102
6103 RemoteInput remoteInput = b.getParcelable(KEY_REMOTE_INPUT);
6104
6105 String[] participants = b.getStringArray(KEY_PARTICIPANTS);
6106 if (participants == null || participants.length != 1) {
6107 return null;
6108 }
6109
6110 return new UnreadConversation(messages,
6111 remoteInput,
6112 onReply,
6113 onRead,
6114 participants, b.getLong(KEY_TIMESTAMP));
6115 }
6116 };
6117
6118 /**
6119 * Builder class for {@link CarExtender.UnreadConversation} objects.
6120 */
6121 public static class Builder {
6122 private final List<String> mMessages = new ArrayList<String>();
6123 private final String mParticipant;
6124 private RemoteInput mRemoteInput;
6125 private PendingIntent mReadPendingIntent;
6126 private PendingIntent mReplyPendingIntent;
6127 private long mLatestTimestamp;
6128
6129 /**
6130 * Constructs a new builder for {@link CarExtender.UnreadConversation}.
6131 *
6132 * @param name The name of the other participant in the conversation.
6133 */
6134 public Builder(String name) {
6135 mParticipant = name;
6136 }
6137
6138 /**
6139 * Appends a new unread message to the list of messages for this conversation.
6140 *
6141 * The messages should be added from oldest to newest.
6142 *
6143 * @param message The text of the new unread message.
6144 * @return This object for method chaining.
6145 */
6146 public Builder addMessage(String message) {
6147 mMessages.add(message);
6148 return this;
6149 }
6150
6151 /**
6152 * Sets the pending intent and remote input which will convey the reply to this
6153 * notification.
6154 *
6155 * @param pendingIntent The pending intent which will be triggered on a reply.
6156 * @param remoteInput The remote input parcelable which will carry the reply.
6157 * @return This object for method chaining.
6158 *
6159 * @see CarExtender.UnreadConversation#getRemoteInput
6160 * @see CarExtender.UnreadConversation#getReplyPendingIntent
6161 */
6162 public Builder setReplyAction(
6163 PendingIntent pendingIntent, RemoteInput remoteInput) {
6164 mRemoteInput = remoteInput;
6165 mReplyPendingIntent = pendingIntent;
6166
6167 return this;
6168 }
6169
6170 /**
6171 * Sets the pending intent that will be sent once the messages in this notification
6172 * are read.
6173 *
6174 * @param pendingIntent The pending intent to use.
6175 * @return This object for method chaining.
6176 */
6177 public Builder setReadPendingIntent(PendingIntent pendingIntent) {
6178 mReadPendingIntent = pendingIntent;
6179 return this;
6180 }
6181
6182 /**
6183 * Sets the timestamp of the most recent message in an unread conversation.
6184 *
6185 * If a messaging notification has been posted by your application and has not
6186 * yet been cancelled, posting a later notification with the same id and tag
6187 * but without a newer timestamp may result in Android Auto not displaying a
6188 * heads up notification for the later notification.
6189 *
6190 * @param timestamp The timestamp of the most recent message in the conversation.
6191 * @return This object for method chaining.
6192 */
6193 public Builder setLatestTimestamp(long timestamp) {
6194 mLatestTimestamp = timestamp;
6195 return this;
6196 }
6197
6198 /**
6199 * Builds a new unread conversation object.
6200 *
6201 * @return The new unread conversation object.
6202 */
6203 public UnreadConversation build() {
6204 String[] messages = mMessages.toArray(new String[mMessages.size()]);
6205 String[] participants = { mParticipant };
6206 return new UnreadConversation(messages, mRemoteInput, mReplyPendingIntent,
6207 mReadPendingIntent, participants, mLatestTimestamp);
6208 }
6209 }
6210 }
6211
6212 /**
Griff Hazen61a9e862014-05-22 16:05:19 -07006213 * Get an array of Notification objects from a parcelable array bundle field.
6214 * Update the bundle to have a typed array so fetches in the future don't need
6215 * to do an array copy.
6216 */
6217 private static Notification[] getNotificationArrayFromBundle(Bundle bundle, String key) {
6218 Parcelable[] array = bundle.getParcelableArray(key);
6219 if (array instanceof Notification[] || array == null) {
6220 return (Notification[]) array;
6221 }
6222 Notification[] typedArray = Arrays.copyOf(array, array.length,
6223 Notification[].class);
6224 bundle.putParcelableArray(key, typedArray);
6225 return typedArray;
6226 }
Christoph Studer4600f9b2014-07-22 22:44:43 +02006227
6228 private static class BuilderRemoteViews extends RemoteViews {
6229 public BuilderRemoteViews(Parcel parcel) {
6230 super(parcel);
6231 }
6232
Kenny Guy77320062014-08-27 21:37:15 +01006233 public BuilderRemoteViews(ApplicationInfo appInfo, int layoutId) {
6234 super(appInfo, layoutId);
Christoph Studer4600f9b2014-07-22 22:44:43 +02006235 }
6236
6237 @Override
6238 public BuilderRemoteViews clone() {
6239 Parcel p = Parcel.obtain();
6240 writeToParcel(p, 0);
6241 p.setDataPosition(0);
6242 BuilderRemoteViews brv = new BuilderRemoteViews(p);
6243 p.recycle();
6244 return brv;
6245 }
6246 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006247}