blob: 28a5c53a3061194715f429354efd1b596ab0dd16 [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;
Selim Cinek65b2e7c2015-10-26 14:11:31 -070027import android.content.pm.PackageManager;
Christoph Studer4600f9b2014-07-22 22:44:43 +020028import android.content.pm.PackageManager.NameNotFoundException;
Jorim Jaggief72a192014-08-26 21:57:46 +020029import android.content.res.ColorStateList;
Joe Onoratoef1e7762010-09-17 18:38:38 -040030import android.graphics.Bitmap;
Kenny Guy8a0101b2014-05-08 23:34:12 +010031import android.graphics.Canvas;
Jorim Jaggi5c2d8462014-03-21 17:37:00 +010032import android.graphics.PorterDuff;
Kenny Guy8a0101b2014-05-08 23:34:12 +010033import android.graphics.drawable.Drawable;
Dan Sandlerd63f9322015-05-06 15:18:49 -040034import android.graphics.drawable.Icon;
John Spurlockc0650f022014-07-19 13:22:39 -040035import android.media.AudioAttributes;
Jeff Sharkey098d5802012-04-26 17:30:34 -070036import android.media.AudioManager;
Jeff Browndba34ba2014-06-24 20:46:03 -070037import android.media.session.MediaSession;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080038import android.net.Uri;
Daniel Sandlerdcbaf662013-04-26 16:23:09 -040039import android.os.BadParcelableException;
Christoph Studer239f8352014-08-25 15:13:18 +020040import android.os.Build;
Daniel Sandler2561b0b2012-02-13 21:04:12 -050041import android.os.Bundle;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080042import android.os.Parcel;
43import android.os.Parcelable;
Daniel Sandlera2985ed2012-04-03 16:42:00 -040044import android.os.SystemClock;
Jeff Sharkey6d515712012-09-20 16:06:08 -070045import android.os.UserHandle;
Selim Cinek60a54252016-02-26 17:03:25 -080046import android.text.SpannableStringBuilder;
47import android.text.Spanned;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080048import android.text.TextUtils;
Selim Cinek60a54252016-02-26 17:03:25 -080049import android.text.style.AbsoluteSizeSpan;
50import 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 /**
570 * Sphere of visibility of this notification, which affects how and when the SystemUI reveals
571 * the notification's presence and contents in untrusted situations (namely, on the secure
572 * lockscreen).
573 *
574 * The default level, {@link #VISIBILITY_PRIVATE}, behaves exactly as notifications have always
575 * done on Android: The notification's {@link #icon} and {@link #tickerText} (if available) are
576 * shown in all situations, but the contents are only available if the device is unlocked for
577 * the appropriate user.
578 *
579 * A more permissive policy can be expressed by {@link #VISIBILITY_PUBLIC}; such a notification
580 * can be read even in an "insecure" context (that is, above a secure lockscreen).
581 * To modify the public version of this notification—for example, to redact some portions—see
582 * {@link Builder#setPublicVersion(Notification)}.
583 *
584 * Finally, a notification can be made {@link #VISIBILITY_SECRET}, which will suppress its icon
585 * and ticker until the user has bypassed the lockscreen.
586 */
587 public int visibility;
588
Griff Hazenfc3922d2014-08-20 11:56:44 -0700589 /**
590 * Notification visibility: Show this notification in its entirety on all lockscreens.
591 *
592 * {@see #visibility}
593 */
Dan Sandler0bf2ed82013-12-21 23:33:41 -0600594 public static final int VISIBILITY_PUBLIC = 1;
Griff Hazenfc3922d2014-08-20 11:56:44 -0700595
596 /**
597 * Notification visibility: Show this notification on all lockscreens, but conceal sensitive or
598 * private information on secure lockscreens.
599 *
600 * {@see #visibility}
601 */
Dan Sandler0bf2ed82013-12-21 23:33:41 -0600602 public static final int VISIBILITY_PRIVATE = 0;
Griff Hazenfc3922d2014-08-20 11:56:44 -0700603
604 /**
605 * Notification visibility: Do not reveal any part of this notification on a secure lockscreen.
606 *
607 * {@see #visibility}
608 */
Dan Sandler0bf2ed82013-12-21 23:33:41 -0600609 public static final int VISIBILITY_SECRET = -1;
610
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500611 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400612 * Notification category: incoming call (voice or video) or similar synchronous communication request.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500613 */
John Spurlockfd7f1e02014-03-18 16:41:57 -0400614 public static final String CATEGORY_CALL = "call";
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500615
616 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400617 * Notification category: incoming direct message (SMS, instant message, etc.).
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500618 */
John Spurlockfd7f1e02014-03-18 16:41:57 -0400619 public static final String CATEGORY_MESSAGE = "msg";
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500620
621 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400622 * Notification category: asynchronous bulk message (email).
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500623 */
John Spurlockfd7f1e02014-03-18 16:41:57 -0400624 public static final String CATEGORY_EMAIL = "email";
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500625
626 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400627 * Notification category: calendar event.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500628 */
John Spurlockfd7f1e02014-03-18 16:41:57 -0400629 public static final String CATEGORY_EVENT = "event";
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500630
631 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400632 * Notification category: promotion or advertisement.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500633 */
John Spurlockfd7f1e02014-03-18 16:41:57 -0400634 public static final String CATEGORY_PROMO = "promo";
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500635
636 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400637 * Notification category: alarm or timer.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500638 */
John Spurlockfd7f1e02014-03-18 16:41:57 -0400639 public static final String CATEGORY_ALARM = "alarm";
640
641 /**
642 * Notification category: progress of a long-running background operation.
643 */
644 public static final String CATEGORY_PROGRESS = "progress";
645
646 /**
647 * Notification category: social network or sharing update.
648 */
649 public static final String CATEGORY_SOCIAL = "social";
650
651 /**
652 * Notification category: error in background operation or authentication status.
653 */
654 public static final String CATEGORY_ERROR = "err";
655
656 /**
657 * Notification category: media transport control for playback.
658 */
659 public static final String CATEGORY_TRANSPORT = "transport";
660
661 /**
662 * Notification category: system or device status update. Reserved for system use.
663 */
664 public static final String CATEGORY_SYSTEM = "sys";
665
666 /**
667 * Notification category: indication of running background service.
668 */
669 public static final String CATEGORY_SERVICE = "service";
670
671 /**
John Spurlock0a69c8c2014-03-21 13:30:57 -0400672 * Notification category: a specific, timely recommendation for a single thing.
673 * For example, a news app might want to recommend a news story it believes the user will
674 * want to read next.
675 */
676 public static final String CATEGORY_RECOMMENDATION = "recommendation";
677
678 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400679 * Notification category: ongoing information about device or contextual status.
680 */
681 public static final String CATEGORY_STATUS = "status";
682
683 /**
John Spurlock24d3dad2015-04-02 12:24:02 -0400684 * Notification category: user-scheduled reminder.
685 */
686 public static final String CATEGORY_REMINDER = "reminder";
687
688 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400689 * One of the predefined notification categories (see the <code>CATEGORY_*</code> constants)
690 * that best describes this Notification. May be used by the system for ranking and filtering.
691 */
692 public String category;
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500693
Griff Hazen5cadc3b2014-05-20 09:55:39 -0700694 private String mGroupKey;
695
696 /**
697 * Get the key used to group this notification into a cluster or stack
698 * with other notifications on devices which support such rendering.
699 */
700 public String getGroup() {
701 return mGroupKey;
702 }
703
704 private String mSortKey;
705
706 /**
707 * Get a sort key that orders this notification among other notifications from the
708 * same package. This can be useful if an external sort was already applied and an app
709 * would like to preserve this. Notifications will be sorted lexicographically using this
710 * value, although providing different priorities in addition to providing sort key may
711 * cause this value to be ignored.
712 *
713 * <p>This sort key can also be used to order members of a notification group. See
714 * {@link Builder#setGroup}.
715 *
716 * @see String#compareTo(String)
717 */
718 public String getSortKey() {
719 return mSortKey;
720 }
721
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500722 /**
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400723 * Additional semantic data to be carried around with this Notification.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400724 * <p>
725 * The extras keys defined here are intended to capture the original inputs to {@link Builder}
726 * APIs, and are intended to be used by
727 * {@link android.service.notification.NotificationListenerService} implementations to extract
728 * detailed information from notification objects.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500729 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400730 public Bundle extras = new Bundle();
731
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400732 /**
733 * {@link #extras} key: this is the title of the notification,
734 * as supplied to {@link Builder#setContentTitle(CharSequence)}.
735 */
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -0500736 public static final String EXTRA_TITLE = "android.title";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400737
738 /**
739 * {@link #extras} key: this is the title of the notification when shown in expanded form,
740 * e.g. as supplied to {@link BigTextStyle#setBigContentTitle(CharSequence)}.
741 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400742 public static final String EXTRA_TITLE_BIG = EXTRA_TITLE + ".big";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400743
744 /**
745 * {@link #extras} key: this is the main text payload, as supplied to
746 * {@link Builder#setContentText(CharSequence)}.
747 */
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -0500748 public static final String EXTRA_TEXT = "android.text";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400749
750 /**
751 * {@link #extras} key: this is a third line of text, as supplied to
752 * {@link Builder#setSubText(CharSequence)}.
753 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400754 public static final String EXTRA_SUB_TEXT = "android.subText";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400755
756 /**
Adrian Roose458aa82015-12-08 16:17:19 -0800757 * {@link #extras} key: this is the remote input history, as supplied to
758 * {@link Builder#setRemoteInputHistory(CharSequence[])}.
759 */
760 public static final String EXTRA_REMOTE_INPUT_HISTORY = "android.remoteInputHistory";
761
762 /**
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400763 * {@link #extras} key: this is a small piece of additional text as supplied to
764 * {@link Builder#setContentInfo(CharSequence)}.
765 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400766 public static final String EXTRA_INFO_TEXT = "android.infoText";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400767
768 /**
769 * {@link #extras} key: this is a line of summary information intended to be shown
770 * alongside expanded notifications, as supplied to (e.g.)
771 * {@link BigTextStyle#setSummaryText(CharSequence)}.
772 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400773 public static final String EXTRA_SUMMARY_TEXT = "android.summaryText";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400774
775 /**
Christoph Studer4600f9b2014-07-22 22:44:43 +0200776 * {@link #extras} key: this is the longer text shown in the big form of a
777 * {@link BigTextStyle} notification, as supplied to
778 * {@link BigTextStyle#bigText(CharSequence)}.
779 */
780 public static final String EXTRA_BIG_TEXT = "android.bigText";
781
782 /**
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400783 * {@link #extras} key: this is the resource ID of the notification's main small icon, as
784 * supplied to {@link Builder#setSmallIcon(int)}.
785 */
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -0500786 public static final String EXTRA_SMALL_ICON = "android.icon";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400787
788 /**
789 * {@link #extras} key: this is a bitmap to be used instead of the small icon when showing the
790 * notification payload, as
791 * supplied to {@link Builder#setLargeIcon(android.graphics.Bitmap)}.
792 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400793 public static final String EXTRA_LARGE_ICON = "android.largeIcon";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400794
795 /**
796 * {@link #extras} key: this is a bitmap to be used instead of the one from
797 * {@link Builder#setLargeIcon(android.graphics.Bitmap)} when the notification is
798 * shown in its expanded form, as supplied to
799 * {@link BigPictureStyle#bigLargeIcon(android.graphics.Bitmap)}.
800 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400801 public static final String EXTRA_LARGE_ICON_BIG = EXTRA_LARGE_ICON + ".big";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400802
803 /**
804 * {@link #extras} key: this is the progress value supplied to
805 * {@link Builder#setProgress(int, int, boolean)}.
806 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400807 public static final String EXTRA_PROGRESS = "android.progress";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400808
809 /**
810 * {@link #extras} key: this is the maximum 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_MAX = "android.progressMax";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400814
815 /**
816 * {@link #extras} key: whether the progress bar is indeterminate, supplied to
817 * {@link Builder#setProgress(int, int, boolean)}.
818 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400819 public static final String EXTRA_PROGRESS_INDETERMINATE = "android.progressIndeterminate";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400820
821 /**
822 * {@link #extras} key: whether {@link #when} should be shown as a count-up timer (specifically
823 * a {@link android.widget.Chronometer}) instead of a timestamp, as supplied to
824 * {@link Builder#setUsesChronometer(boolean)}.
825 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400826 public static final String EXTRA_SHOW_CHRONOMETER = "android.showChronometer";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400827
828 /**
Selim Cinek81c23aa2016-02-25 16:23:13 -0800829 * {@link #extras} key: whether the chronometer set on the notification should count down
830 * instead of counting up. Is only relevant if key {@link #EXTRA_SHOW_CHRONOMETER} is present.
831 */
832 public static final String EXTRA_CHRONOMETER_COUNTS_DOWN = "android.chronometerCountsDown";
833
834 /**
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400835 * {@link #extras} key: whether {@link #when} should be shown,
836 * as supplied to {@link Builder#setShowWhen(boolean)}.
837 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400838 public static final String EXTRA_SHOW_WHEN = "android.showWhen";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400839
840 /**
841 * {@link #extras} key: this is a bitmap to be shown in {@link BigPictureStyle} expanded
842 * notifications, supplied to {@link BigPictureStyle#bigPicture(android.graphics.Bitmap)}.
843 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400844 public static final String EXTRA_PICTURE = "android.picture";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400845
846 /**
847 * {@link #extras} key: An array of CharSequences to show in {@link InboxStyle} expanded
848 * notifications, each of which was supplied to {@link InboxStyle#addLine(CharSequence)}.
849 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400850 public static final String EXTRA_TEXT_LINES = "android.textLines";
Dan Sandler842dd772014-05-15 09:36:47 -0400851
852 /**
853 * {@link #extras} key: A string representing the name of the specific
854 * {@link android.app.Notification.Style} used to create this notification.
855 */
Chris Wren91ad5632013-06-05 15:05:57 -0400856 public static final String EXTRA_TEMPLATE = "android.template";
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400857
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400858 /**
Chris Wrene6c48932014-09-29 17:19:27 -0400859 * {@link #extras} key: A String array containing the people that this notification relates to,
860 * each of which was supplied to {@link Builder#addPerson(String)}.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400861 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400862 public static final String EXTRA_PEOPLE = "android.people";
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500863
864 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400865 * Allow certain system-generated notifications to appear before the device is provisioned.
866 * Only available to notifications coming from the android package.
867 * @hide
868 */
869 public static final String EXTRA_ALLOW_DURING_SETUP = "android.allowDuringSetup";
870
871 /**
Jose Limae9e3b3b2014-05-18 23:44:50 -0700872 * {@link #extras} key: A
873 * {@link android.content.ContentUris content URI} pointing to an image that can be displayed
874 * in the background when the notification is selected. The URI must point to an image stream
875 * suitable for passing into
876 * {@link android.graphics.BitmapFactory#decodeStream(java.io.InputStream)
877 * BitmapFactory.decodeStream}; all other content types will be ignored. The content provider
878 * URI used for this purpose must require no permissions to read the image data.
879 */
880 public static final String EXTRA_BACKGROUND_IMAGE_URI = "android.backgroundImageUri";
881
882 /**
Dan Sandler842dd772014-05-15 09:36:47 -0400883 * {@link #extras} key: A
Jeff Browndba34ba2014-06-24 20:46:03 -0700884 * {@link android.media.session.MediaSession.Token} associated with a
Dan Sandler842dd772014-05-15 09:36:47 -0400885 * {@link android.app.Notification.MediaStyle} notification.
886 */
887 public static final String EXTRA_MEDIA_SESSION = "android.mediaSession";
888
889 /**
Bryan Mawhinneye191f902014-07-22 12:50:09 +0100890 * {@link #extras} key: the indices of actions to be shown in the compact view,
891 * as supplied to (e.g.) {@link MediaStyle#setShowActionsInCompactView(int...)}.
892 */
893 public static final String EXTRA_COMPACT_ACTIONS = "android.compactActions";
894
Christoph Studer943aa672014-08-03 20:31:16 +0200895 /**
Kenny Guy8942bcd2014-09-08 21:09:47 +0100896 * {@link #extras} key: the user that built the notification.
897 *
898 * @hide
899 */
900 public static final String EXTRA_ORIGINATING_USERID = "android.originatingUserId";
901
902 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400903 * @hide
904 */
905 public static final String EXTRA_BUILDER_APPLICATION_INFO = "android.appInfo";
906
Selim Cinek247fa012016-02-18 09:50:48 -0800907 /**
908 * @hide
909 */
910 public static final String EXTRA_CONTAINS_CUSTOM_VIEW = "android.contains.customView";
911
Dan Sandlerd63f9322015-05-06 15:18:49 -0400912 private Icon mSmallIcon;
913 private Icon mLargeIcon;
914
Chris Wren51c75102013-07-16 20:49:17 -0400915 /**
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400916 * Structure to encapsulate a named action that can be shown as part of this notification.
917 * It must include an icon, a label, and a {@link PendingIntent} to be fired when the action is
918 * selected by the user.
919 * <p>
Griff Hazen959591e2014-05-15 22:26:18 -0700920 * Apps should use {@link Notification.Builder#addAction(int, CharSequence, PendingIntent)}
921 * or {@link Notification.Builder#addAction(Notification.Action)}
922 * to attach actions.
Daniel Sandlera0a938c2012-03-15 08:42:37 -0400923 */
Daniel Sandlerea2a3172013-02-20 22:24:20 -0500924 public static class Action implements Parcelable {
Griff Hazen959591e2014-05-15 22:26:18 -0700925 private final Bundle mExtras;
Dan Sandler86647982015-05-13 23:41:13 -0400926 private Icon mIcon;
Griff Hazen61a9e862014-05-22 16:05:19 -0700927 private final RemoteInput[] mRemoteInputs;
Griff Hazen959591e2014-05-15 22:26:18 -0700928
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400929 /**
930 * Small icon representing the action.
Dan Sandler86647982015-05-13 23:41:13 -0400931 *
932 * @deprecated Use {@link Action#getIcon()} instead.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400933 */
Dan Sandler86647982015-05-13 23:41:13 -0400934 @Deprecated
Daniel Sandlera0a938c2012-03-15 08:42:37 -0400935 public int icon;
Griff Hazen5cadc3b2014-05-20 09:55:39 -0700936
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400937 /**
938 * Title of the action.
939 */
Daniel Sandlera0a938c2012-03-15 08:42:37 -0400940 public CharSequence title;
Griff Hazen5cadc3b2014-05-20 09:55:39 -0700941
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400942 /**
943 * Intent to send when the user invokes this action. May be null, in which case the action
944 * may be rendered in a disabled presentation by the system UI.
945 */
Daniel Sandlera0a938c2012-03-15 08:42:37 -0400946 public PendingIntent actionIntent;
Griff Hazen959591e2014-05-15 22:26:18 -0700947
Daniel Sandlera0a938c2012-03-15 08:42:37 -0400948 private Action(Parcel in) {
Dan Sandler86647982015-05-13 23:41:13 -0400949 if (in.readInt() != 0) {
950 mIcon = Icon.CREATOR.createFromParcel(in);
Dan Sandler68079d52015-07-22 10:45:30 -0400951 if (mIcon.getType() == Icon.TYPE_RESOURCE) {
952 icon = mIcon.getResId();
953 }
Dan Sandler86647982015-05-13 23:41:13 -0400954 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -0400955 title = TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(in);
956 if (in.readInt() == 1) {
957 actionIntent = PendingIntent.CREATOR.createFromParcel(in);
958 }
Griff Hazen959591e2014-05-15 22:26:18 -0700959 mExtras = in.readBundle();
Griff Hazen5cadc3b2014-05-20 09:55:39 -0700960 mRemoteInputs = in.createTypedArray(RemoteInput.CREATOR);
Daniel Sandlera0a938c2012-03-15 08:42:37 -0400961 }
Griff Hazen5cadc3b2014-05-20 09:55:39 -0700962
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400963 /**
Dan Sandler86647982015-05-13 23:41:13 -0400964 * @deprecated Use {@link android.app.Notification.Action.Builder}.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400965 */
Dan Sandler86647982015-05-13 23:41:13 -0400966 @Deprecated
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400967 public Action(int icon, CharSequence title, PendingIntent intent) {
Dan Sandler86647982015-05-13 23:41:13 -0400968 this(Icon.createWithResource("", icon), title, intent, new Bundle(), null);
Griff Hazen959591e2014-05-15 22:26:18 -0700969 }
970
Dan Sandler86647982015-05-13 23:41:13 -0400971 private Action(Icon icon, CharSequence title, PendingIntent intent, Bundle extras,
Griff Hazen5cadc3b2014-05-20 09:55:39 -0700972 RemoteInput[] remoteInputs) {
Dan Sandler86647982015-05-13 23:41:13 -0400973 this.mIcon = icon;
Gus Prevasf5bff46f2015-08-24 10:34:28 -0400974 if (icon != null && icon.getType() == Icon.TYPE_RESOURCE) {
975 this.icon = icon.getResId();
976 }
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400977 this.title = title;
978 this.actionIntent = intent;
Griff Hazen959591e2014-05-15 22:26:18 -0700979 this.mExtras = extras != null ? extras : new Bundle();
Griff Hazen5cadc3b2014-05-20 09:55:39 -0700980 this.mRemoteInputs = remoteInputs;
Griff Hazen959591e2014-05-15 22:26:18 -0700981 }
982
983 /**
Dan Sandler86647982015-05-13 23:41:13 -0400984 * Return an icon representing the action.
985 */
986 public Icon getIcon() {
987 if (mIcon == null && icon != 0) {
988 // you snuck an icon in here without using the builder; let's try to keep it
989 mIcon = Icon.createWithResource("", icon);
990 }
991 return mIcon;
992 }
993
994 /**
Griff Hazen959591e2014-05-15 22:26:18 -0700995 * Get additional metadata carried around with this Action.
996 */
997 public Bundle getExtras() {
998 return mExtras;
999 }
1000
1001 /**
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001002 * Get the list of inputs to be collected from the user when this action is sent.
1003 * May return null if no remote inputs were added.
1004 */
1005 public RemoteInput[] getRemoteInputs() {
1006 return mRemoteInputs;
1007 }
1008
1009 /**
Griff Hazen959591e2014-05-15 22:26:18 -07001010 * Builder class for {@link Action} objects.
1011 */
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001012 public static final class Builder {
Dan Sandler86647982015-05-13 23:41:13 -04001013 private final Icon mIcon;
Griff Hazen959591e2014-05-15 22:26:18 -07001014 private final CharSequence mTitle;
1015 private final PendingIntent mIntent;
1016 private final Bundle mExtras;
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001017 private ArrayList<RemoteInput> mRemoteInputs;
Griff Hazen959591e2014-05-15 22:26:18 -07001018
1019 /**
1020 * Construct a new builder for {@link Action} object.
1021 * @param icon icon to show for this action
1022 * @param title the title of the action
1023 * @param intent the {@link PendingIntent} to fire when users trigger this action
1024 */
Dan Sandler86647982015-05-13 23:41:13 -04001025 @Deprecated
Griff Hazen959591e2014-05-15 22:26:18 -07001026 public Builder(int icon, CharSequence title, PendingIntent intent) {
Dan Sandler86647982015-05-13 23:41:13 -04001027 this(Icon.createWithResource("", icon), title, intent, new Bundle(), null);
1028 }
1029
1030 /**
1031 * Construct a new builder for {@link Action} object.
1032 * @param icon icon to show for this action
1033 * @param title the title of the action
1034 * @param intent the {@link PendingIntent} to fire when users trigger this action
1035 */
1036 public Builder(Icon icon, CharSequence title, PendingIntent intent) {
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001037 this(icon, title, intent, new Bundle(), null);
Griff Hazen959591e2014-05-15 22:26:18 -07001038 }
1039
1040 /**
1041 * Construct a new builder for {@link Action} object using the fields from an
1042 * {@link Action}.
1043 * @param action the action to read fields from.
1044 */
1045 public Builder(Action action) {
Dan Sandler86647982015-05-13 23:41:13 -04001046 this(action.getIcon(), action.title, action.actionIntent, new Bundle(action.mExtras),
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001047 action.getRemoteInputs());
Griff Hazen959591e2014-05-15 22:26:18 -07001048 }
1049
Dan Sandler86647982015-05-13 23:41:13 -04001050 private Builder(Icon icon, CharSequence title, PendingIntent intent, Bundle extras,
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001051 RemoteInput[] remoteInputs) {
Griff Hazen959591e2014-05-15 22:26:18 -07001052 mIcon = icon;
1053 mTitle = title;
1054 mIntent = intent;
1055 mExtras = extras;
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001056 if (remoteInputs != null) {
1057 mRemoteInputs = new ArrayList<RemoteInput>(remoteInputs.length);
1058 Collections.addAll(mRemoteInputs, remoteInputs);
1059 }
Griff Hazen959591e2014-05-15 22:26:18 -07001060 }
1061
1062 /**
1063 * Merge additional metadata into this builder.
1064 *
1065 * <p>Values within the Bundle will replace existing extras values in this Builder.
1066 *
1067 * @see Notification.Action#extras
1068 */
1069 public Builder addExtras(Bundle extras) {
1070 if (extras != null) {
1071 mExtras.putAll(extras);
1072 }
1073 return this;
1074 }
1075
1076 /**
1077 * Get the metadata Bundle used by this Builder.
1078 *
1079 * <p>The returned Bundle is shared with this Builder.
1080 */
1081 public Bundle getExtras() {
1082 return mExtras;
1083 }
1084
1085 /**
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001086 * Add an input to be collected from the user when this action is sent.
1087 * Response values can be retrieved from the fired intent by using the
1088 * {@link RemoteInput#getResultsFromIntent} function.
1089 * @param remoteInput a {@link RemoteInput} to add to the action
1090 * @return this object for method chaining
1091 */
1092 public Builder addRemoteInput(RemoteInput remoteInput) {
1093 if (mRemoteInputs == null) {
1094 mRemoteInputs = new ArrayList<RemoteInput>();
1095 }
1096 mRemoteInputs.add(remoteInput);
1097 return this;
1098 }
1099
1100 /**
1101 * Apply an extender to this action builder. Extenders may be used to add
1102 * metadata or change options on this builder.
1103 */
Griff Hazen61a9e862014-05-22 16:05:19 -07001104 public Builder extend(Extender extender) {
1105 extender.extend(this);
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001106 return this;
1107 }
1108
1109 /**
Griff Hazen959591e2014-05-15 22:26:18 -07001110 * Combine all of the options that have been set and return a new {@link Action}
1111 * object.
1112 * @return the built action
1113 */
1114 public Action build() {
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001115 RemoteInput[] remoteInputs = mRemoteInputs != null
1116 ? mRemoteInputs.toArray(new RemoteInput[mRemoteInputs.size()]) : null;
1117 return new Action(mIcon, mTitle, mIntent, mExtras, remoteInputs);
Griff Hazen959591e2014-05-15 22:26:18 -07001118 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001119 }
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001120
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001121 @Override
1122 public Action clone() {
1123 return new Action(
Dan Sandler86647982015-05-13 23:41:13 -04001124 getIcon(),
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001125 title,
1126 actionIntent, // safe to alias
1127 new Bundle(mExtras),
1128 getRemoteInputs());
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001129 }
1130 @Override
1131 public int describeContents() {
1132 return 0;
1133 }
1134 @Override
1135 public void writeToParcel(Parcel out, int flags) {
Dan Sandler86647982015-05-13 23:41:13 -04001136 final Icon ic = getIcon();
1137 if (ic != null) {
1138 out.writeInt(1);
1139 ic.writeToParcel(out, 0);
1140 } else {
1141 out.writeInt(0);
1142 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001143 TextUtils.writeToParcel(title, out, flags);
1144 if (actionIntent != null) {
1145 out.writeInt(1);
1146 actionIntent.writeToParcel(out, flags);
1147 } else {
1148 out.writeInt(0);
1149 }
Griff Hazen959591e2014-05-15 22:26:18 -07001150 out.writeBundle(mExtras);
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001151 out.writeTypedArray(mRemoteInputs, flags);
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001152 }
Griff Hazen959591e2014-05-15 22:26:18 -07001153 public static final Parcelable.Creator<Action> CREATOR =
1154 new Parcelable.Creator<Action>() {
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001155 public Action createFromParcel(Parcel in) {
1156 return new Action(in);
1157 }
1158 public Action[] newArray(int size) {
1159 return new Action[size];
1160 }
1161 };
Griff Hazen61a9e862014-05-22 16:05:19 -07001162
1163 /**
1164 * Extender interface for use with {@link Builder#extend}. Extenders may be used to add
1165 * metadata or change options on an action builder.
1166 */
1167 public interface Extender {
1168 /**
1169 * Apply this extender to a notification action builder.
1170 * @param builder the builder to be modified.
1171 * @return the build object for chaining.
1172 */
1173 public Builder extend(Builder builder);
1174 }
1175
1176 /**
1177 * Wearable extender for notification actions. To add extensions to an action,
1178 * create a new {@link android.app.Notification.Action.WearableExtender} object using
1179 * the {@code WearableExtender()} constructor and apply it to a
1180 * {@link android.app.Notification.Action.Builder} using
1181 * {@link android.app.Notification.Action.Builder#extend}.
1182 *
1183 * <pre class="prettyprint">
1184 * Notification.Action action = new Notification.Action.Builder(
1185 * R.drawable.archive_all, "Archive all", actionIntent)
Griff Hazen14f57992014-05-26 09:07:14 -07001186 * .extend(new Notification.Action.WearableExtender()
Griff Hazen61a9e862014-05-22 16:05:19 -07001187 * .setAvailableOffline(false))
Griff Hazen14f57992014-05-26 09:07:14 -07001188 * .build();</pre>
Griff Hazen61a9e862014-05-22 16:05:19 -07001189 */
1190 public static final class WearableExtender implements Extender {
1191 /** Notification action extra which contains wearable extensions */
1192 private static final String EXTRA_WEARABLE_EXTENSIONS = "android.wearable.EXTENSIONS";
1193
Pete Gastaf6781d2014-10-07 15:17:05 -04001194 // Keys within EXTRA_WEARABLE_EXTENSIONS for wearable options.
Griff Hazen61a9e862014-05-22 16:05:19 -07001195 private static final String KEY_FLAGS = "flags";
Pete Gastaf6781d2014-10-07 15:17:05 -04001196 private static final String KEY_IN_PROGRESS_LABEL = "inProgressLabel";
1197 private static final String KEY_CONFIRM_LABEL = "confirmLabel";
1198 private static final String KEY_CANCEL_LABEL = "cancelLabel";
Griff Hazen61a9e862014-05-22 16:05:19 -07001199
1200 // Flags bitwise-ored to mFlags
1201 private static final int FLAG_AVAILABLE_OFFLINE = 0x1;
1202
1203 // Default value for flags integer
1204 private static final int DEFAULT_FLAGS = FLAG_AVAILABLE_OFFLINE;
1205
1206 private int mFlags = DEFAULT_FLAGS;
1207
Pete Gastaf6781d2014-10-07 15:17:05 -04001208 private CharSequence mInProgressLabel;
1209 private CharSequence mConfirmLabel;
1210 private CharSequence mCancelLabel;
1211
Griff Hazen61a9e862014-05-22 16:05:19 -07001212 /**
1213 * Create a {@link android.app.Notification.Action.WearableExtender} with default
1214 * options.
1215 */
1216 public WearableExtender() {
1217 }
1218
1219 /**
1220 * Create a {@link android.app.Notification.Action.WearableExtender} by reading
1221 * wearable options present in an existing notification action.
1222 * @param action the notification action to inspect.
1223 */
1224 public WearableExtender(Action action) {
1225 Bundle wearableBundle = action.getExtras().getBundle(EXTRA_WEARABLE_EXTENSIONS);
1226 if (wearableBundle != null) {
1227 mFlags = wearableBundle.getInt(KEY_FLAGS, DEFAULT_FLAGS);
Pete Gastaf6781d2014-10-07 15:17:05 -04001228 mInProgressLabel = wearableBundle.getCharSequence(KEY_IN_PROGRESS_LABEL);
1229 mConfirmLabel = wearableBundle.getCharSequence(KEY_CONFIRM_LABEL);
1230 mCancelLabel = wearableBundle.getCharSequence(KEY_CANCEL_LABEL);
Griff Hazen61a9e862014-05-22 16:05:19 -07001231 }
1232 }
1233
1234 /**
1235 * Apply wearable extensions to a notification action that is being built. This is
1236 * typically called by the {@link android.app.Notification.Action.Builder#extend}
1237 * method of {@link android.app.Notification.Action.Builder}.
1238 */
1239 @Override
1240 public Action.Builder extend(Action.Builder builder) {
1241 Bundle wearableBundle = new Bundle();
1242
1243 if (mFlags != DEFAULT_FLAGS) {
1244 wearableBundle.putInt(KEY_FLAGS, mFlags);
1245 }
Pete Gastaf6781d2014-10-07 15:17:05 -04001246 if (mInProgressLabel != null) {
1247 wearableBundle.putCharSequence(KEY_IN_PROGRESS_LABEL, mInProgressLabel);
1248 }
1249 if (mConfirmLabel != null) {
1250 wearableBundle.putCharSequence(KEY_CONFIRM_LABEL, mConfirmLabel);
1251 }
1252 if (mCancelLabel != null) {
1253 wearableBundle.putCharSequence(KEY_CANCEL_LABEL, mCancelLabel);
1254 }
Griff Hazen61a9e862014-05-22 16:05:19 -07001255
1256 builder.getExtras().putBundle(EXTRA_WEARABLE_EXTENSIONS, wearableBundle);
1257 return builder;
1258 }
1259
1260 @Override
1261 public WearableExtender clone() {
1262 WearableExtender that = new WearableExtender();
1263 that.mFlags = this.mFlags;
Pete Gastaf6781d2014-10-07 15:17:05 -04001264 that.mInProgressLabel = this.mInProgressLabel;
1265 that.mConfirmLabel = this.mConfirmLabel;
1266 that.mCancelLabel = this.mCancelLabel;
Griff Hazen61a9e862014-05-22 16:05:19 -07001267 return that;
1268 }
1269
1270 /**
1271 * Set whether this action is available when the wearable device is not connected to
1272 * a companion device. The user can still trigger this action when the wearable device is
1273 * offline, but a visual hint will indicate that the action may not be available.
1274 * Defaults to true.
1275 */
1276 public WearableExtender setAvailableOffline(boolean availableOffline) {
1277 setFlag(FLAG_AVAILABLE_OFFLINE, availableOffline);
1278 return this;
1279 }
1280
1281 /**
1282 * Get whether this action is available when the wearable device is not connected to
1283 * a companion device. The user can still trigger this action when the wearable device is
1284 * offline, but a visual hint will indicate that the action may not be available.
1285 * Defaults to true.
1286 */
1287 public boolean isAvailableOffline() {
1288 return (mFlags & FLAG_AVAILABLE_OFFLINE) != 0;
1289 }
1290
1291 private void setFlag(int mask, boolean value) {
1292 if (value) {
1293 mFlags |= mask;
1294 } else {
1295 mFlags &= ~mask;
1296 }
1297 }
Pete Gastaf6781d2014-10-07 15:17:05 -04001298
1299 /**
1300 * Set a label to display while the wearable is preparing to automatically execute the
1301 * action. This is usually a 'ing' verb ending in ellipsis like "Sending..."
1302 *
1303 * @param label the label to display while the action is being prepared to execute
1304 * @return this object for method chaining
1305 */
1306 public WearableExtender setInProgressLabel(CharSequence label) {
1307 mInProgressLabel = label;
1308 return this;
1309 }
1310
1311 /**
1312 * Get the label to display while the wearable is preparing to automatically execute
1313 * the action. This is usually a 'ing' verb ending in ellipsis like "Sending..."
1314 *
1315 * @return the label to display while the action is being prepared to execute
1316 */
1317 public CharSequence getInProgressLabel() {
1318 return mInProgressLabel;
1319 }
1320
1321 /**
1322 * Set a label to display to confirm that the action should be executed.
1323 * This is usually an imperative verb like "Send".
1324 *
1325 * @param label the label to confirm the action should be executed
1326 * @return this object for method chaining
1327 */
1328 public WearableExtender setConfirmLabel(CharSequence label) {
1329 mConfirmLabel = label;
1330 return this;
1331 }
1332
1333 /**
1334 * Get the label to display to confirm that the action should be executed.
1335 * This is usually an imperative verb like "Send".
1336 *
1337 * @return the label to confirm the action should be executed
1338 */
1339 public CharSequence getConfirmLabel() {
1340 return mConfirmLabel;
1341 }
1342
1343 /**
1344 * Set a label to display to cancel the action.
1345 * This is usually an imperative verb, like "Cancel".
1346 *
1347 * @param label the label to display to cancel the action
1348 * @return this object for method chaining
1349 */
1350 public WearableExtender setCancelLabel(CharSequence label) {
1351 mCancelLabel = label;
1352 return this;
1353 }
1354
1355 /**
1356 * Get the label to display to cancel the action.
1357 * This is usually an imperative verb like "Cancel".
1358 *
1359 * @return the label to display to cancel the action
1360 */
1361 public CharSequence getCancelLabel() {
1362 return mCancelLabel;
1363 }
Griff Hazen61a9e862014-05-22 16:05:19 -07001364 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001365 }
1366
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001367 /**
1368 * Array of all {@link Action} structures attached to this notification by
1369 * {@link Builder#addAction(int, CharSequence, PendingIntent)}. Mostly useful for instances of
1370 * {@link android.service.notification.NotificationListenerService} that provide an alternative
1371 * interface for invoking actions.
1372 */
Daniel Sandlerea2a3172013-02-20 22:24:20 -05001373 public Action[] actions;
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001374
1375 /**
Dan Sandler0bf2ed82013-12-21 23:33:41 -06001376 * Replacement version of this notification whose content will be shown
1377 * in an insecure context such as atop a secure keyguard. See {@link #visibility}
1378 * and {@link #VISIBILITY_PUBLIC}.
1379 */
1380 public Notification publicVersion;
1381
1382 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001383 * Constructs a Notification object with default values.
Joe Onorato46439ce2010-11-19 13:56:21 -08001384 * You might want to consider using {@link Builder} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001385 */
1386 public Notification()
1387 {
1388 this.when = System.currentTimeMillis();
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001389 this.priority = PRIORITY_DEFAULT;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001390 }
1391
1392 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001393 * @hide
1394 */
1395 public Notification(Context context, int icon, CharSequence tickerText, long when,
1396 CharSequence contentTitle, CharSequence contentText, Intent contentIntent)
1397 {
Chris Wren1ce4b6d2015-06-11 10:19:43 -04001398 new Builder(context)
1399 .setWhen(when)
1400 .setSmallIcon(icon)
1401 .setTicker(tickerText)
1402 .setContentTitle(contentTitle)
1403 .setContentText(contentText)
1404 .setContentIntent(PendingIntent.getActivity(context, 0, contentIntent, 0))
1405 .buildInto(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001406 }
1407
1408 /**
1409 * Constructs a Notification object with the information needed to
1410 * have a status bar icon without the standard expanded view.
1411 *
1412 * @param icon The resource id of the icon to put in the status bar.
1413 * @param tickerText The text that flows by in the status bar when the notification first
1414 * activates.
1415 * @param when The time to show in the time field. In the System.currentTimeMillis
1416 * timebase.
Joe Onorato46439ce2010-11-19 13:56:21 -08001417 *
1418 * @deprecated Use {@link Builder} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001419 */
Joe Onorato46439ce2010-11-19 13:56:21 -08001420 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001421 public Notification(int icon, CharSequence tickerText, long when)
1422 {
1423 this.icon = icon;
1424 this.tickerText = tickerText;
1425 this.when = when;
1426 }
1427
1428 /**
1429 * Unflatten the notification from a parcel.
1430 */
1431 public Notification(Parcel parcel)
1432 {
1433 int version = parcel.readInt();
1434
1435 when = parcel.readLong();
Dan Sandler3936e7a2015-05-19 20:59:12 -04001436 if (parcel.readInt() != 0) {
1437 mSmallIcon = Icon.CREATOR.createFromParcel(parcel);
Dan Sandler4e787062015-06-17 15:09:48 -04001438 if (mSmallIcon.getType() == Icon.TYPE_RESOURCE) {
1439 icon = mSmallIcon.getResId();
1440 }
Dan Sandler3936e7a2015-05-19 20:59:12 -04001441 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001442 number = parcel.readInt();
1443 if (parcel.readInt() != 0) {
1444 contentIntent = PendingIntent.CREATOR.createFromParcel(parcel);
1445 }
1446 if (parcel.readInt() != 0) {
1447 deleteIntent = PendingIntent.CREATOR.createFromParcel(parcel);
1448 }
1449 if (parcel.readInt() != 0) {
1450 tickerText = TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(parcel);
1451 }
1452 if (parcel.readInt() != 0) {
Joe Onorato46439ce2010-11-19 13:56:21 -08001453 tickerView = RemoteViews.CREATOR.createFromParcel(parcel);
Joe Onoratoef1e7762010-09-17 18:38:38 -04001454 }
1455 if (parcel.readInt() != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001456 contentView = RemoteViews.CREATOR.createFromParcel(parcel);
1457 }
Joe Onorato561d3852010-11-20 18:09:34 -08001458 if (parcel.readInt() != 0) {
Dan Sandlerd63f9322015-05-06 15:18:49 -04001459 mLargeIcon = Icon.CREATOR.createFromParcel(parcel);
Joe Onorato561d3852010-11-20 18:09:34 -08001460 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001461 defaults = parcel.readInt();
1462 flags = parcel.readInt();
1463 if (parcel.readInt() != 0) {
1464 sound = Uri.CREATOR.createFromParcel(parcel);
1465 }
1466
1467 audioStreamType = parcel.readInt();
John Spurlockc0650f022014-07-19 13:22:39 -04001468 if (parcel.readInt() != 0) {
1469 audioAttributes = AudioAttributes.CREATOR.createFromParcel(parcel);
1470 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001471 vibrate = parcel.createLongArray();
1472 ledARGB = parcel.readInt();
1473 ledOnMS = parcel.readInt();
1474 ledOffMS = parcel.readInt();
1475 iconLevel = parcel.readInt();
Daniel Sandlere46cbd32010-06-17 10:35:26 -04001476
1477 if (parcel.readInt() != 0) {
1478 fullScreenIntent = PendingIntent.CREATOR.createFromParcel(parcel);
1479 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001480
1481 priority = parcel.readInt();
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001482
John Spurlockfd7f1e02014-03-18 16:41:57 -04001483 category = parcel.readString();
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001484
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001485 mGroupKey = parcel.readString();
1486
1487 mSortKey = parcel.readString();
1488
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001489 extras = parcel.readBundle(); // may be null
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001490
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001491 actions = parcel.createTypedArray(Action.CREATOR); // may be null
1492
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001493 if (parcel.readInt() != 0) {
Daniel Sandlerf3b73432012-03-27 15:01:25 -04001494 bigContentView = RemoteViews.CREATOR.createFromParcel(parcel);
1495 }
Dan Sandler0bf2ed82013-12-21 23:33:41 -06001496
Chris Wren8fd39ec2014-02-27 17:43:26 -05001497 if (parcel.readInt() != 0) {
1498 headsUpContentView = RemoteViews.CREATOR.createFromParcel(parcel);
1499 }
1500
Dan Sandler0bf2ed82013-12-21 23:33:41 -06001501 visibility = parcel.readInt();
1502
1503 if (parcel.readInt() != 0) {
1504 publicVersion = Notification.CREATOR.createFromParcel(parcel);
1505 }
Dan Sandler26e81cf2014-05-06 10:01:27 -04001506
1507 color = parcel.readInt();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001508 }
1509
Andy Stadler110988c2010-12-03 14:29:16 -08001510 @Override
Joe Onorato18e69df2010-05-17 22:26:12 -07001511 public Notification clone() {
1512 Notification that = new Notification();
Daniel Sandler1a497d32013-04-18 14:52:45 -04001513 cloneInto(that, true);
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001514 return that;
1515 }
Joe Onorato18e69df2010-05-17 22:26:12 -07001516
Daniel Sandler1a497d32013-04-18 14:52:45 -04001517 /**
1518 * Copy all (or if heavy is false, all except Bitmaps and RemoteViews) members
1519 * of this into that.
1520 * @hide
1521 */
1522 public void cloneInto(Notification that, boolean heavy) {
Joe Onorato18e69df2010-05-17 22:26:12 -07001523 that.when = this.when;
Dan Sandlerd63f9322015-05-06 15:18:49 -04001524 that.mSmallIcon = this.mSmallIcon;
Joe Onorato18e69df2010-05-17 22:26:12 -07001525 that.number = this.number;
1526
1527 // PendingIntents are global, so there's no reason (or way) to clone them.
1528 that.contentIntent = this.contentIntent;
1529 that.deleteIntent = this.deleteIntent;
Daniel Sandlere46cbd32010-06-17 10:35:26 -04001530 that.fullScreenIntent = this.fullScreenIntent;
Joe Onorato18e69df2010-05-17 22:26:12 -07001531
1532 if (this.tickerText != null) {
1533 that.tickerText = this.tickerText.toString();
1534 }
Daniel Sandler1a497d32013-04-18 14:52:45 -04001535 if (heavy && this.tickerView != null) {
Joe Onorato46439ce2010-11-19 13:56:21 -08001536 that.tickerView = this.tickerView.clone();
Joe Onoratoef1e7762010-09-17 18:38:38 -04001537 }
Daniel Sandler1a497d32013-04-18 14:52:45 -04001538 if (heavy && this.contentView != null) {
Joe Onorato18e69df2010-05-17 22:26:12 -07001539 that.contentView = this.contentView.clone();
1540 }
Dan Sandlerd63f9322015-05-06 15:18:49 -04001541 if (heavy && this.mLargeIcon != null) {
1542 that.mLargeIcon = this.mLargeIcon;
Joe Onorato561d3852010-11-20 18:09:34 -08001543 }
Jozef BABJAKa8b91832011-02-22 08:05:08 +01001544 that.iconLevel = this.iconLevel;
Joe Onorato18e69df2010-05-17 22:26:12 -07001545 that.sound = this.sound; // android.net.Uri is immutable
1546 that.audioStreamType = this.audioStreamType;
John Spurlockc0650f022014-07-19 13:22:39 -04001547 if (this.audioAttributes != null) {
1548 that.audioAttributes = new AudioAttributes.Builder(this.audioAttributes).build();
1549 }
Joe Onorato18e69df2010-05-17 22:26:12 -07001550
1551 final long[] vibrate = this.vibrate;
1552 if (vibrate != null) {
1553 final int N = vibrate.length;
1554 final long[] vib = that.vibrate = new long[N];
1555 System.arraycopy(vibrate, 0, vib, 0, N);
1556 }
1557
1558 that.ledARGB = this.ledARGB;
1559 that.ledOnMS = this.ledOnMS;
1560 that.ledOffMS = this.ledOffMS;
1561 that.defaults = this.defaults;
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001562
Joe Onorato18e69df2010-05-17 22:26:12 -07001563 that.flags = this.flags;
1564
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001565 that.priority = this.priority;
Joe Malin8d40d042012-11-05 11:36:40 -08001566
John Spurlockfd7f1e02014-03-18 16:41:57 -04001567 that.category = this.category;
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001568
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001569 that.mGroupKey = this.mGroupKey;
1570
1571 that.mSortKey = this.mSortKey;
1572
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001573 if (this.extras != null) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04001574 try {
1575 that.extras = new Bundle(this.extras);
1576 // will unparcel
1577 that.extras.size();
1578 } catch (BadParcelableException e) {
1579 Log.e(TAG, "could not unparcel extras from notification: " + this, e);
1580 that.extras = null;
1581 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001582 }
1583
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001584 if (this.actions != null) {
1585 that.actions = new Action[this.actions.length];
1586 for(int i=0; i<this.actions.length; i++) {
1587 that.actions[i] = this.actions[i].clone();
1588 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001589 }
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001590
Daniel Sandler1a497d32013-04-18 14:52:45 -04001591 if (heavy && this.bigContentView != null) {
Daniel Sandlerf3b73432012-03-27 15:01:25 -04001592 that.bigContentView = this.bigContentView.clone();
1593 }
Daniel Sandler1a497d32013-04-18 14:52:45 -04001594
Chris Wren8fd39ec2014-02-27 17:43:26 -05001595 if (heavy && this.headsUpContentView != null) {
1596 that.headsUpContentView = this.headsUpContentView.clone();
1597 }
1598
Dan Sandler0bf2ed82013-12-21 23:33:41 -06001599 that.visibility = this.visibility;
1600
1601 if (this.publicVersion != null) {
1602 that.publicVersion = new Notification();
1603 this.publicVersion.cloneInto(that.publicVersion, heavy);
1604 }
1605
Dan Sandler26e81cf2014-05-06 10:01:27 -04001606 that.color = this.color;
1607
Daniel Sandler1a497d32013-04-18 14:52:45 -04001608 if (!heavy) {
1609 that.lightenPayload(); // will clean out extras
1610 }
1611 }
1612
1613 /**
1614 * Removes heavyweight parts of the Notification object for archival or for sending to
1615 * listeners when the full contents are not necessary.
1616 * @hide
1617 */
1618 public final void lightenPayload() {
1619 tickerView = null;
1620 contentView = null;
1621 bigContentView = null;
Chris Wren8fd39ec2014-02-27 17:43:26 -05001622 headsUpContentView = null;
Dan Sandlerd63f9322015-05-06 15:18:49 -04001623 mLargeIcon = null;
Dan Sandler50128532015-12-08 15:42:41 -05001624 if (extras != null && !extras.isEmpty()) {
1625 final Set<String> keyset = extras.keySet();
1626 final int N = keyset.size();
1627 final String[] keys = keyset.toArray(new String[N]);
1628 for (int i=0; i<N; i++) {
1629 final String key = keys[i];
1630 final Object obj = extras.get(key);
1631 if (obj != null &&
1632 ( obj instanceof Parcelable
1633 || obj instanceof Parcelable[]
1634 || obj instanceof SparseArray
1635 || obj instanceof ArrayList)) {
1636 extras.remove(key);
1637 }
1638 }
Daniel Sandler1a497d32013-04-18 14:52:45 -04001639 }
Joe Onorato18e69df2010-05-17 22:26:12 -07001640 }
1641
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04001642 /**
1643 * Make sure this CharSequence is safe to put into a bundle, which basically
1644 * means it had better not be some custom Parcelable implementation.
1645 * @hide
1646 */
1647 public static CharSequence safeCharSequence(CharSequence cs) {
Christoph Studer535ec612014-09-03 15:47:47 +02001648 if (cs == null) return cs;
1649 if (cs.length() > MAX_CHARSEQUENCE_LENGTH) {
1650 cs = cs.subSequence(0, MAX_CHARSEQUENCE_LENGTH);
1651 }
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04001652 if (cs instanceof Parcelable) {
1653 Log.e(TAG, "warning: " + cs.getClass().getCanonicalName()
1654 + " instance is a custom Parcelable and not allowed in Notification");
1655 return cs.toString();
1656 }
Selim Cinek60a54252016-02-26 17:03:25 -08001657 return removeTextSizeSpans(cs);
1658 }
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04001659
Selim Cinek60a54252016-02-26 17:03:25 -08001660 private static CharSequence removeTextSizeSpans(CharSequence charSequence) {
1661 if (charSequence instanceof Spanned) {
1662 Spanned ss = (Spanned) charSequence;
1663 Object[] spans = ss.getSpans(0, ss.length(), Object.class);
1664 SpannableStringBuilder builder = new SpannableStringBuilder(ss.toString());
1665 for (Object span : spans) {
1666 Object resultSpan = span;
1667 if (span instanceof TextAppearanceSpan) {
1668 TextAppearanceSpan originalSpan = (TextAppearanceSpan) span;
1669 resultSpan = new TextAppearanceSpan(
1670 originalSpan.getFamily(),
1671 originalSpan.getTextStyle(),
1672 -1,
1673 originalSpan.getTextColor(),
1674 originalSpan.getLinkTextColor());
1675 } else if (span instanceof RelativeSizeSpan
1676 || span instanceof AbsoluteSizeSpan) {
1677 continue;
1678 }
1679 builder.setSpan(resultSpan, ss.getSpanStart(span), ss.getSpanEnd(span),
1680 ss.getSpanFlags(span));
1681 }
1682 return builder;
1683 }
1684 return charSequence;
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04001685 }
1686
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001687 public int describeContents() {
1688 return 0;
1689 }
1690
1691 /**
Dan Sandler4e787062015-06-17 15:09:48 -04001692 * Flatten this notification into a parcel.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001693 */
1694 public void writeToParcel(Parcel parcel, int flags)
1695 {
1696 parcel.writeInt(1);
1697
1698 parcel.writeLong(when);
Dan Sandler4e787062015-06-17 15:09:48 -04001699 if (mSmallIcon == null && icon != 0) {
1700 // you snuck an icon in here without using the builder; let's try to keep it
1701 mSmallIcon = Icon.createWithResource("", icon);
1702 }
Dan Sandler3936e7a2015-05-19 20:59:12 -04001703 if (mSmallIcon != null) {
1704 parcel.writeInt(1);
1705 mSmallIcon.writeToParcel(parcel, 0);
1706 } else {
1707 parcel.writeInt(0);
1708 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001709 parcel.writeInt(number);
1710 if (contentIntent != null) {
1711 parcel.writeInt(1);
1712 contentIntent.writeToParcel(parcel, 0);
1713 } else {
1714 parcel.writeInt(0);
1715 }
1716 if (deleteIntent != null) {
1717 parcel.writeInt(1);
1718 deleteIntent.writeToParcel(parcel, 0);
1719 } else {
1720 parcel.writeInt(0);
1721 }
1722 if (tickerText != null) {
1723 parcel.writeInt(1);
1724 TextUtils.writeToParcel(tickerText, parcel, flags);
1725 } else {
1726 parcel.writeInt(0);
1727 }
Joe Onorato46439ce2010-11-19 13:56:21 -08001728 if (tickerView != null) {
Joe Onoratoef1e7762010-09-17 18:38:38 -04001729 parcel.writeInt(1);
Joe Onorato46439ce2010-11-19 13:56:21 -08001730 tickerView.writeToParcel(parcel, 0);
Joe Onoratoef1e7762010-09-17 18:38:38 -04001731 } else {
1732 parcel.writeInt(0);
1733 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001734 if (contentView != null) {
1735 parcel.writeInt(1);
1736 contentView.writeToParcel(parcel, 0);
1737 } else {
1738 parcel.writeInt(0);
1739 }
Dan Sandlerd63f9322015-05-06 15:18:49 -04001740 if (mLargeIcon != null) {
Joe Onorato561d3852010-11-20 18:09:34 -08001741 parcel.writeInt(1);
Dan Sandlerd63f9322015-05-06 15:18:49 -04001742 mLargeIcon.writeToParcel(parcel, 0);
Joe Onorato561d3852010-11-20 18:09:34 -08001743 } else {
1744 parcel.writeInt(0);
1745 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001746
1747 parcel.writeInt(defaults);
1748 parcel.writeInt(this.flags);
1749
1750 if (sound != null) {
1751 parcel.writeInt(1);
1752 sound.writeToParcel(parcel, 0);
1753 } else {
1754 parcel.writeInt(0);
1755 }
1756 parcel.writeInt(audioStreamType);
John Spurlockc0650f022014-07-19 13:22:39 -04001757
1758 if (audioAttributes != null) {
1759 parcel.writeInt(1);
1760 audioAttributes.writeToParcel(parcel, 0);
1761 } else {
1762 parcel.writeInt(0);
1763 }
1764
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001765 parcel.writeLongArray(vibrate);
1766 parcel.writeInt(ledARGB);
1767 parcel.writeInt(ledOnMS);
1768 parcel.writeInt(ledOffMS);
1769 parcel.writeInt(iconLevel);
Daniel Sandlere46cbd32010-06-17 10:35:26 -04001770
1771 if (fullScreenIntent != null) {
1772 parcel.writeInt(1);
1773 fullScreenIntent.writeToParcel(parcel, 0);
1774 } else {
1775 parcel.writeInt(0);
1776 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001777
1778 parcel.writeInt(priority);
Joe Malin8d40d042012-11-05 11:36:40 -08001779
John Spurlockfd7f1e02014-03-18 16:41:57 -04001780 parcel.writeString(category);
Joe Malin8d40d042012-11-05 11:36:40 -08001781
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001782 parcel.writeString(mGroupKey);
1783
1784 parcel.writeString(mSortKey);
1785
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001786 parcel.writeBundle(extras); // null ok
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001787
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001788 parcel.writeTypedArray(actions, 0); // null ok
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001789
Daniel Sandlerf3b73432012-03-27 15:01:25 -04001790 if (bigContentView != null) {
1791 parcel.writeInt(1);
1792 bigContentView.writeToParcel(parcel, 0);
1793 } else {
1794 parcel.writeInt(0);
1795 }
Dan Sandler0bf2ed82013-12-21 23:33:41 -06001796
Chris Wren8fd39ec2014-02-27 17:43:26 -05001797 if (headsUpContentView != null) {
1798 parcel.writeInt(1);
1799 headsUpContentView.writeToParcel(parcel, 0);
1800 } else {
1801 parcel.writeInt(0);
1802 }
1803
Dan Sandler0bf2ed82013-12-21 23:33:41 -06001804 parcel.writeInt(visibility);
1805
1806 if (publicVersion != null) {
1807 parcel.writeInt(1);
1808 publicVersion.writeToParcel(parcel, 0);
1809 } else {
1810 parcel.writeInt(0);
1811 }
Dan Sandler26e81cf2014-05-06 10:01:27 -04001812
1813 parcel.writeInt(color);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001814 }
1815
1816 /**
1817 * Parcelable.Creator that instantiates Notification objects
1818 */
1819 public static final Parcelable.Creator<Notification> CREATOR
1820 = new Parcelable.Creator<Notification>()
1821 {
1822 public Notification createFromParcel(Parcel parcel)
1823 {
1824 return new Notification(parcel);
1825 }
1826
1827 public Notification[] newArray(int size)
1828 {
1829 return new Notification[size];
1830 }
1831 };
1832
1833 /**
1834 * Sets the {@link #contentView} field to be a view with the standard "Latest Event"
1835 * layout.
1836 *
1837 * <p>Uses the {@link #icon} and {@link #when} fields to set the icon and time fields
1838 * in the view.</p>
1839 * @param context The context for your application / activity.
1840 * @param contentTitle The title that goes in the expanded entry.
1841 * @param contentText The text that goes in the expanded entry.
1842 * @param contentIntent The intent to launch when the user clicks the expanded notification.
1843 * If this is an activity, it must include the
1844 * {@link android.content.Intent#FLAG_ACTIVITY_NEW_TASK} flag, which requires
Scott Main7aee61f2011-02-08 11:25:01 -08001845 * that you take care of task management as described in the
1846 * <a href="{@docRoot}guide/topics/fundamentals/tasks-and-back-stack.html">Tasks and Back
1847 * Stack</a> document.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001848 *
Joe Onorato46439ce2010-11-19 13:56:21 -08001849 * @deprecated Use {@link Builder} instead.
Chris Wrena05db382015-06-24 15:18:34 -04001850 * @removed
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001851 */
Joe Onorato46439ce2010-11-19 13:56:21 -08001852 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001853 public void setLatestEventInfo(Context context,
1854 CharSequence contentTitle, CharSequence contentText, PendingIntent contentIntent) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04001855 if (context.getApplicationInfo().targetSdkVersion > Build.VERSION_CODES.LOLLIPOP_MR1){
1856 Log.e(TAG, "setLatestEventInfo() is deprecated and you should feel deprecated.",
1857 new Throwable());
1858 }
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001859
Julia Reynoldsd9228f12015-10-20 10:37:27 -04001860 // ensure that any information already set directly is preserved
1861 final Notification.Builder builder = new Notification.Builder(context, this);
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001862
1863 // now apply the latestEventInfo fields
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001864 if (contentTitle != null) {
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001865 builder.setContentTitle(contentTitle);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001866 }
1867 if (contentText != null) {
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001868 builder.setContentText(contentText);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001869 }
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001870 builder.setContentIntent(contentIntent);
Julia Reynoldsd9228f12015-10-20 10:37:27 -04001871
1872 builder.build(); // callers expect this notification to be ready to use
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001873 }
1874
Julia Reynoldsda303542015-11-23 14:00:20 -05001875 /**
1876 * @hide
1877 */
1878 public static void addFieldsFromContext(Context context, Notification notification) {
1879 if (notification.extras.getParcelable(EXTRA_BUILDER_APPLICATION_INFO) == null) {
1880 notification.extras.putParcelable(EXTRA_BUILDER_APPLICATION_INFO,
1881 context.getApplicationInfo());
1882 }
1883 if (!notification.extras.containsKey(EXTRA_ORIGINATING_USERID)) {
1884 notification.extras.putInt(EXTRA_ORIGINATING_USERID, context.getUserId());
1885 }
1886 }
1887
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001888 @Override
1889 public String toString() {
1890 StringBuilder sb = new StringBuilder();
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001891 sb.append("Notification(pri=");
1892 sb.append(priority);
1893 sb.append(" contentView=");
Joe Onoratoc9596d62011-01-12 17:03:11 -08001894 if (contentView != null) {
1895 sb.append(contentView.getPackage());
1896 sb.append("/0x");
1897 sb.append(Integer.toHexString(contentView.getLayoutId()));
1898 } else {
1899 sb.append("null");
1900 }
1901 sb.append(" vibrate=");
Daniel Sandler6738eee2012-11-16 12:03:32 -05001902 if ((this.defaults & DEFAULT_VIBRATE) != 0) {
1903 sb.append("default");
1904 } else if (this.vibrate != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001905 int N = this.vibrate.length-1;
1906 sb.append("[");
1907 for (int i=0; i<N; i++) {
1908 sb.append(this.vibrate[i]);
1909 sb.append(',');
1910 }
Simon Schoar8cf97d92009-06-10 22:08:37 +02001911 if (N != -1) {
1912 sb.append(this.vibrate[N]);
1913 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001914 sb.append("]");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001915 } else {
1916 sb.append("null");
1917 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001918 sb.append(" sound=");
Daniel Sandler6738eee2012-11-16 12:03:32 -05001919 if ((this.defaults & DEFAULT_SOUND) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001920 sb.append("default");
Daniel Sandler6738eee2012-11-16 12:03:32 -05001921 } else if (this.sound != null) {
1922 sb.append(this.sound.toString());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001923 } else {
1924 sb.append("null");
1925 }
Chris Wren365b6d32015-07-16 10:39:26 -04001926 if (this.tickerText != null) {
1927 sb.append(" tick");
1928 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001929 sb.append(" defaults=0x");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001930 sb.append(Integer.toHexString(this.defaults));
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001931 sb.append(" flags=0x");
Daniel Sandlere46cbd32010-06-17 10:35:26 -04001932 sb.append(Integer.toHexString(this.flags));
Dan Sandler26e81cf2014-05-06 10:01:27 -04001933 sb.append(String.format(" color=0x%08x", this.color));
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001934 if (this.category != null) {
1935 sb.append(" category=");
1936 sb.append(this.category);
1937 }
1938 if (this.mGroupKey != null) {
1939 sb.append(" groupKey=");
1940 sb.append(this.mGroupKey);
1941 }
1942 if (this.mSortKey != null) {
1943 sb.append(" sortKey=");
1944 sb.append(this.mSortKey);
1945 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001946 if (actions != null) {
Dan Sandler1b718782014-07-18 12:43:45 -04001947 sb.append(" actions=");
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001948 sb.append(actions.length);
Dan Sandler1b718782014-07-18 12:43:45 -04001949 }
1950 sb.append(" vis=");
1951 sb.append(visibilityToString(this.visibility));
1952 if (this.publicVersion != null) {
1953 sb.append(" publicVersion=");
1954 sb.append(publicVersion.toString());
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001955 }
1956 sb.append(")");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001957 return sb.toString();
1958 }
Joe Onorato46439ce2010-11-19 13:56:21 -08001959
Dan Sandler1b718782014-07-18 12:43:45 -04001960 /**
1961 * {@hide}
1962 */
1963 public static String visibilityToString(int vis) {
1964 switch (vis) {
1965 case VISIBILITY_PRIVATE:
1966 return "PRIVATE";
1967 case VISIBILITY_PUBLIC:
1968 return "PUBLIC";
1969 case VISIBILITY_SECRET:
1970 return "SECRET";
1971 default:
1972 return "UNKNOWN(" + String.valueOf(vis) + ")";
1973 }
1974 }
1975
Joe Onoratocb109a02011-01-18 17:57:41 -08001976 /**
John Spurlock1d881a12015-03-18 19:21:54 -04001977 * {@hide}
1978 */
1979 public static String priorityToString(@Priority int pri) {
1980 switch (pri) {
1981 case PRIORITY_MIN:
1982 return "MIN";
1983 case PRIORITY_LOW:
1984 return "LOW";
1985 case PRIORITY_DEFAULT:
1986 return "DEFAULT";
1987 case PRIORITY_HIGH:
1988 return "HIGH";
1989 case PRIORITY_MAX:
1990 return "MAX";
1991 default:
1992 return "UNKNOWN(" + String.valueOf(pri) + ")";
1993 }
1994 }
1995
1996 /**
Dan Sandlerd63f9322015-05-06 15:18:49 -04001997 * The small icon representing this notification in the status bar and content view.
1998 *
1999 * @return the small icon representing this notification.
2000 *
2001 * @see Builder#getSmallIcon()
2002 * @see Builder#setSmallIcon(Icon)
2003 */
2004 public Icon getSmallIcon() {
2005 return mSmallIcon;
2006 }
2007
2008 /**
2009 * Used when notifying to clean up legacy small icons.
2010 * @hide
2011 */
2012 public void setSmallIcon(Icon icon) {
2013 mSmallIcon = icon;
2014 }
2015
2016 /**
2017 * The large icon shown in this notification's content view.
2018 * @see Builder#getLargeIcon()
2019 * @see Builder#setLargeIcon(Icon)
2020 */
2021 public Icon getLargeIcon() {
2022 return mLargeIcon;
2023 }
2024
2025 /**
Christoph Studer4600f9b2014-07-22 22:44:43 +02002026 * @hide
2027 */
Christoph Studerc8db24b2014-07-25 17:50:30 +02002028 public boolean isGroupSummary() {
2029 return mGroupKey != null && (flags & FLAG_GROUP_SUMMARY) != 0;
2030 }
2031
2032 /**
2033 * @hide
2034 */
2035 public boolean isGroupChild() {
2036 return mGroupKey != null && (flags & FLAG_GROUP_SUMMARY) == 0;
2037 }
2038
2039 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002040 * Builder class for {@link Notification} objects.
Joe Malin8d40d042012-11-05 11:36:40 -08002041 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002042 * Provides a convenient way to set the various fields of a {@link Notification} and generate
Scott Main183bf112012-08-13 19:12:13 -07002043 * content views using the platform's notification layout template. If your app supports
2044 * versions of Android as old as API level 4, you can instead use
2045 * {@link android.support.v4.app.NotificationCompat.Builder NotificationCompat.Builder},
2046 * available in the <a href="{@docRoot}tools/extras/support-library.html">Android Support
2047 * library</a>.
Joe Malin8d40d042012-11-05 11:36:40 -08002048 *
Scott Main183bf112012-08-13 19:12:13 -07002049 * <p>Example:
Joe Malin8d40d042012-11-05 11:36:40 -08002050 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002051 * <pre class="prettyprint">
Scott Main183bf112012-08-13 19:12:13 -07002052 * Notification noti = new Notification.Builder(mContext)
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002053 * .setContentTitle(&quot;New mail from &quot; + sender.toString())
2054 * .setContentText(subject)
2055 * .setSmallIcon(R.drawable.new_mail)
2056 * .setLargeIcon(aBitmap)
Chris Wrenfbd96ba2012-05-01 12:03:58 -04002057 * .build();
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002058 * </pre>
Joe Onoratocb109a02011-01-18 17:57:41 -08002059 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002060 public static class Builder {
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05002061 /**
2062 * @hide
2063 */
2064 public static final String EXTRA_REBUILD_CONTENT_VIEW_ACTION_COUNT =
2065 "android.rebuild.contentViewActionCount";
2066 /**
2067 * @hide
2068 */
2069 public static final String EXTRA_REBUILD_BIG_CONTENT_VIEW_ACTION_COUNT
2070 = "android.rebuild.bigViewActionCount";
2071 /**
2072 * @hide
2073 */
2074 public static final String EXTRA_REBUILD_HEADS_UP_CONTENT_VIEW_ACTION_COUNT
2075 = "android.rebuild.hudViewActionCount";
2076
Daniel Sandler602ad1c2012-06-12 16:06:27 -04002077 private static final int MAX_ACTION_BUTTONS = 3;
Daniel Sandler8680bf82012-05-15 16:52:52 -04002078
Joe Onorato46439ce2010-11-19 13:56:21 -08002079 private Context mContext;
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002080 private Notification mN;
2081 private Bundle mUserExtras = new Bundle();
Chris Wrenfbd96ba2012-05-01 12:03:58 -04002082 private Style mStyle;
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002083 private ArrayList<Action> mActions = new ArrayList<Action>(MAX_ACTION_BUTTONS);
2084 private ArrayList<String> mPersonList = new ArrayList<String>();
2085 private NotificationColorUtil mColorUtil;
2086 private boolean mColorUtilInited = false;
Christoph Studer7ac80e62014-08-04 16:01:57 +02002087
2088 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002089 * Constructs a new Builder with the defaults:
Joe Onoratocb109a02011-01-18 17:57:41 -08002090 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002091
2092 * <table>
2093 * <tr><th align=right>priority</th>
2094 * <td>{@link #PRIORITY_DEFAULT}</td></tr>
2095 * <tr><th align=right>when</th>
2096 * <td>now ({@link System#currentTimeMillis()})</td></tr>
2097 * <tr><th align=right>audio stream</th>
2098 * <td>{@link #STREAM_DEFAULT}</td></tr>
2099 * </table>
Joe Onoratocb109a02011-01-18 17:57:41 -08002100 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002101
2102 * @param context
2103 * A {@link Context} that will be used by the Builder to construct the
2104 * RemoteViews. The Context will not be held past the lifetime of this Builder
2105 * object.
Joe Onoratocb109a02011-01-18 17:57:41 -08002106 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002107 public Builder(Context context) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002108 this(context, null);
Joe Onorato46439ce2010-11-19 13:56:21 -08002109 }
2110
Joe Onoratocb109a02011-01-18 17:57:41 -08002111 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002112 * @hide
Christoph Studer4600f9b2014-07-22 22:44:43 +02002113 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002114 public Builder(Context context, Notification toAdopt) {
2115 mContext = context;
Christoph Studer4600f9b2014-07-22 22:44:43 +02002116
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002117 if (toAdopt == null) {
2118 mN = new Notification();
2119 mN.extras.putBoolean(EXTRA_SHOW_WHEN, true);
2120 mN.priority = PRIORITY_DEFAULT;
2121 mN.visibility = VISIBILITY_PRIVATE;
2122 } else {
2123 mN = toAdopt;
2124 if (mN.actions != null) {
2125 Collections.addAll(mActions, mN.actions);
Christoph Studer4600f9b2014-07-22 22:44:43 +02002126 }
2127
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002128 if (mN.extras.containsKey(EXTRA_PEOPLE)) {
2129 Collections.addAll(mPersonList, mN.extras.getStringArray(EXTRA_PEOPLE));
2130 }
2131
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002132 String templateClass = mN.extras.getString(EXTRA_TEMPLATE);
2133 if (!TextUtils.isEmpty(templateClass)) {
2134 final Class<? extends Style> styleClass
2135 = getNotificationStyleClass(templateClass);
2136 if (styleClass == null) {
2137 Log.d(TAG, "Unknown style class: " + templateClass);
2138 } else {
2139 try {
2140 final Constructor<? extends Style> ctor = styleClass.getConstructor();
2141 ctor.setAccessible(true);
2142 final Style style = ctor.newInstance();
2143 style.restoreFromExtras(mN.extras);
2144
2145 if (style != null) {
2146 setStyle(style);
2147 }
2148 } catch (Throwable t) {
2149 Log.e(TAG, "Could not create Style", t);
2150 }
2151 }
2152 }
2153
2154 }
2155 }
2156
2157 private NotificationColorUtil getColorUtil() {
2158 if (!mColorUtilInited) {
2159 mColorUtilInited = true;
2160 if (mContext.getApplicationInfo().targetSdkVersion < Build.VERSION_CODES.LOLLIPOP) {
2161 mColorUtil = NotificationColorUtil.getInstance(mContext);
Christoph Studer4600f9b2014-07-22 22:44:43 +02002162 }
2163 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002164 return mColorUtil;
Christoph Studer4600f9b2014-07-22 22:44:43 +02002165 }
2166
2167 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002168 * Add a timestamp pertaining to the notification (usually the time the event occurred).
Daniel Sandler0c890492012-09-12 17:23:10 -07002169 * It will be shown in the notification content view by default; use
Griff Hazen50c11652014-05-16 09:46:31 -07002170 * {@link #setShowWhen(boolean) setShowWhen} to control this.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002171 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002172 * @see Notification#when
Joe Onoratocb109a02011-01-18 17:57:41 -08002173 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002174 public Builder setWhen(long when) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002175 mN.when = when;
Joe Onorato46439ce2010-11-19 13:56:21 -08002176 return this;
2177 }
2178
Joe Onoratocb109a02011-01-18 17:57:41 -08002179 /**
Griff Hazen50c11652014-05-16 09:46:31 -07002180 * Control whether the timestamp set with {@link #setWhen(long) setWhen} is shown
Daniel Sandler0c890492012-09-12 17:23:10 -07002181 * in the content view.
2182 */
2183 public Builder setShowWhen(boolean show) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002184 mN.extras.putBoolean(EXTRA_SHOW_WHEN, show);
Daniel Sandler0c890492012-09-12 17:23:10 -07002185 return this;
2186 }
2187
2188 /**
Daniel Sandlerd33b8032012-05-10 11:41:48 -04002189 * Show the {@link Notification#when} field as a stopwatch.
Joe Malin8d40d042012-11-05 11:36:40 -08002190 *
2191 * Instead of presenting <code>when</code> as a timestamp, the notification will show an
Daniel Sandlerd33b8032012-05-10 11:41:48 -04002192 * automatically updating display of the minutes and seconds since <code>when</code>.
Daniel Sandlera2985ed2012-04-03 16:42:00 -04002193 *
Daniel Sandlerd33b8032012-05-10 11:41:48 -04002194 * Useful when showing an elapsed time (like an ongoing phone call).
2195 *
Selim Cinek81c23aa2016-02-25 16:23:13 -08002196 * The counter can also be set to count down to <code>when</code> when using
2197 * {@link #setChronometerCountsDown(boolean)}.
2198 *
Daniel Sandlerd33b8032012-05-10 11:41:48 -04002199 * @see android.widget.Chronometer
Daniel Sandlera2985ed2012-04-03 16:42:00 -04002200 * @see Notification#when
Selim Cinek81c23aa2016-02-25 16:23:13 -08002201 * @see #setChronometerCountsDown(boolean)
Daniel Sandlera2985ed2012-04-03 16:42:00 -04002202 */
2203 public Builder setUsesChronometer(boolean b) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002204 mN.extras.putBoolean(EXTRA_SHOW_CHRONOMETER, b);
Daniel Sandlera2985ed2012-04-03 16:42:00 -04002205 return this;
2206 }
2207
2208 /**
Selim Cinek81c23aa2016-02-25 16:23:13 -08002209 * Sets the Chronometer to count down instead of counting up.
2210 *
2211 * <p>This is only relevant if {@link #setUsesChronometer(boolean)} has been set to true.
2212 * If it isn't set the chronometer will count up.
2213 *
2214 * @see #setUsesChronometer(boolean)
2215 */
2216 public Builder setChronometerCountsDown(boolean countsDown) {
2217 mN.extras.putBoolean(EXTRA_CHRONOMETER_COUNTS_DOWN, countsDown);
2218 return this;
2219 }
2220
2221 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002222 * Set the small icon resource, which will be used to represent the notification in the
2223 * status bar.
Joe Onoratocb109a02011-01-18 17:57:41 -08002224 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002225
2226 * The platform template for the expanded view will draw this icon in the left, unless a
2227 * {@link #setLargeIcon(Bitmap) large icon} has also been specified, in which case the small
2228 * icon will be moved to the right-hand side.
2229 *
2230
2231 * @param icon
2232 * A resource ID in the application's package of the drawable to use.
2233 * @see Notification#icon
Joe Onoratocb109a02011-01-18 17:57:41 -08002234 */
Tor Norbye7b9c9122013-05-30 16:48:33 -07002235 public Builder setSmallIcon(@DrawableRes int icon) {
Dan Sandlerd63f9322015-05-06 15:18:49 -04002236 return setSmallIcon(icon != 0
2237 ? Icon.createWithResource(mContext, icon)
2238 : null);
Joe Onorato46439ce2010-11-19 13:56:21 -08002239 }
2240
Joe Onoratocb109a02011-01-18 17:57:41 -08002241 /**
2242 * A variant of {@link #setSmallIcon(int) setSmallIcon(int)} that takes an additional
2243 * level parameter for when the icon is a {@link android.graphics.drawable.LevelListDrawable
2244 * LevelListDrawable}.
2245 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002246 * @param icon A resource ID in the application's package of the drawable to use.
Joe Onoratocb109a02011-01-18 17:57:41 -08002247 * @param level The level to use for the icon.
2248 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002249 * @see Notification#icon
2250 * @see Notification#iconLevel
Joe Onoratocb109a02011-01-18 17:57:41 -08002251 */
Tor Norbye7b9c9122013-05-30 16:48:33 -07002252 public Builder setSmallIcon(@DrawableRes int icon, int level) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002253 mN.iconLevel = level;
Dan Sandlerd63f9322015-05-06 15:18:49 -04002254 return setSmallIcon(icon);
2255 }
2256
2257 /**
2258 * Set the small icon, which will be used to represent the notification in the
2259 * status bar and content view (unless overriden there by a
2260 * {@link #setLargeIcon(Bitmap) large icon}).
2261 *
2262 * @param icon An Icon object to use.
2263 * @see Notification#icon
2264 */
2265 public Builder setSmallIcon(Icon icon) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002266 mN.setSmallIcon(icon);
2267 if (icon != null && icon.getType() == Icon.TYPE_RESOURCE) {
2268 mN.icon = icon.getResId();
2269 }
Joe Onorato46439ce2010-11-19 13:56:21 -08002270 return this;
2271 }
2272
Joe Onoratocb109a02011-01-18 17:57:41 -08002273 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002274 * Set the first line of text in the platform notification template.
Joe Onoratocb109a02011-01-18 17:57:41 -08002275 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002276 public Builder setContentTitle(CharSequence title) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002277 mN.extras.putCharSequence(EXTRA_TITLE, safeCharSequence(title));
Joe Onorato46439ce2010-11-19 13:56:21 -08002278 return this;
2279 }
2280
Joe Onoratocb109a02011-01-18 17:57:41 -08002281 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002282 * Set the second line of text in the platform notification template.
Joe Onoratocb109a02011-01-18 17:57:41 -08002283 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002284 public Builder setContentText(CharSequence text) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002285 mN.extras.putCharSequence(EXTRA_TEXT, safeCharSequence(text));
Joe Onorato46439ce2010-11-19 13:56:21 -08002286 return this;
2287 }
2288
Joe Onoratocb109a02011-01-18 17:57:41 -08002289 /**
Joe Malin8d40d042012-11-05 11:36:40 -08002290 * Set the third line of text in the platform notification template.
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04002291 * Don't use if you're also using {@link #setProgress(int, int, boolean)}; they occupy the
2292 * same location in the standard template.
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002293 */
2294 public Builder setSubText(CharSequence text) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002295 mN.extras.putCharSequence(EXTRA_SUB_TEXT, safeCharSequence(text));
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002296 return this;
2297 }
2298
2299 /**
Adrian Roose458aa82015-12-08 16:17:19 -08002300 * Set the remote input history.
2301 *
2302 * This should be set to the most recent inputs that have been sent
2303 * through a {@link RemoteInput} of this Notification and cleared once the it is no
2304 * longer relevant (e.g. for chat notifications once the other party has responded).
2305 *
2306 * The most recent input must be stored at the 0 index, the second most recent at the
2307 * 1 index, etc. Note that the system will limit both how far back the inputs will be shown
2308 * and how much of each individual input is shown.
2309 *
2310 * <p>Note: The reply text will only be shown on notifications that have least one action
2311 * with a {@code RemoteInput}.</p>
2312 */
2313 public Builder setRemoteInputHistory(CharSequence[] text) {
2314 if (text == null) {
2315 mN.extras.putCharSequenceArray(EXTRA_REMOTE_INPUT_HISTORY, null);
2316 } else {
2317 final int N = Math.min(MAX_REPLY_HISTORY, text.length);
2318 CharSequence[] safe = new CharSequence[N];
2319 for (int i = 0; i < N; i++) {
2320 safe[i] = safeCharSequence(text[i]);
2321 }
2322 mN.extras.putCharSequenceArray(EXTRA_REMOTE_INPUT_HISTORY, safe);
2323 }
2324 return this;
2325 }
2326
2327 /**
Joe Onoratocb109a02011-01-18 17:57:41 -08002328 * Set the large number at the right-hand side of the notification. This is
2329 * equivalent to setContentInfo, although it might show the number in a different
2330 * font size for readability.
2331 */
Joe Onorato8595a3d2010-11-19 18:12:07 -08002332 public Builder setNumber(int number) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002333 mN.number = number;
Joe Onorato8595a3d2010-11-19 18:12:07 -08002334 return this;
2335 }
2336
Joe Onoratocb109a02011-01-18 17:57:41 -08002337 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002338 * A small piece of additional information pertaining to this notification.
2339 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002340 * The platform template will draw this on the last line of the notification, at the far
2341 * right (to the right of a smallIcon if it has been placed there).
Joe Onoratocb109a02011-01-18 17:57:41 -08002342 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002343 public Builder setContentInfo(CharSequence info) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002344 mN.extras.putCharSequence(EXTRA_INFO_TEXT, safeCharSequence(info));
Joe Onorato46439ce2010-11-19 13:56:21 -08002345 return this;
2346 }
2347
Joe Onoratocb109a02011-01-18 17:57:41 -08002348 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002349 * Set the progress this notification represents.
2350 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002351 * The platform template will represent this using a {@link ProgressBar}.
Jeff Sharkey1c400132011-08-05 14:50:13 -07002352 */
2353 public Builder setProgress(int max, int progress, boolean indeterminate) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002354 mN.extras.putInt(EXTRA_PROGRESS, progress);
2355 mN.extras.putInt(EXTRA_PROGRESS_MAX, max);
2356 mN.extras.putBoolean(EXTRA_PROGRESS_INDETERMINATE, indeterminate);
Jeff Sharkey1c400132011-08-05 14:50:13 -07002357 return this;
2358 }
2359
2360 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002361 * Supply a custom RemoteViews to use instead of the platform template.
2362 *
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002363 * Use {@link #setCustomContentView(RemoteViews)} instead.
Joe Onoratocb109a02011-01-18 17:57:41 -08002364 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002365 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -08002366 public Builder setContent(RemoteViews views) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002367 return setCustomContentView(views);
2368 }
2369
2370 /**
2371 * Supply custom RemoteViews to use instead of the platform template.
2372 *
2373 * This will override the layout that would otherwise be constructed by this Builder
2374 * object.
2375 */
2376 public Builder setCustomContentView(RemoteViews contentView) {
2377 mN.contentView = contentView;
2378 return this;
2379 }
2380
2381 /**
2382 * Supply custom RemoteViews to use instead of the platform template in the expanded form.
2383 *
2384 * This will override the expanded layout that would otherwise be constructed by this
2385 * Builder object.
2386 */
2387 public Builder setCustomBigContentView(RemoteViews contentView) {
2388 mN.bigContentView = contentView;
2389 return this;
2390 }
2391
2392 /**
2393 * Supply custom RemoteViews to use instead of the platform template in the heads up dialog.
2394 *
2395 * This will override the heads-up layout that would otherwise be constructed by this
2396 * Builder object.
2397 */
2398 public Builder setCustomHeadsUpContentView(RemoteViews contentView) {
2399 mN.headsUpContentView = contentView;
Joe Onorato46439ce2010-11-19 13:56:21 -08002400 return this;
2401 }
2402
Joe Onoratocb109a02011-01-18 17:57:41 -08002403 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002404 * Supply a {@link PendingIntent} to be sent when the notification is clicked.
2405 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002406 * As of {@link android.os.Build.VERSION_CODES#HONEYCOMB}, if this field is unset and you
2407 * have specified a custom RemoteViews with {@link #setContent(RemoteViews)}, you can use
2408 * {@link RemoteViews#setOnClickPendingIntent RemoteViews.setOnClickPendingIntent(int,PendingIntent)}
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002409 * to assign PendingIntents to individual views in that custom layout (i.e., to create
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002410 * clickable buttons inside the notification view).
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002411 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002412 * @see Notification#contentIntent Notification.contentIntent
Joe Onoratocb109a02011-01-18 17:57:41 -08002413 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002414 public Builder setContentIntent(PendingIntent intent) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002415 mN.contentIntent = intent;
Joe Onorato46439ce2010-11-19 13:56:21 -08002416 return this;
2417 }
2418
Joe Onoratocb109a02011-01-18 17:57:41 -08002419 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002420 * Supply a {@link PendingIntent} to send when the notification is cleared explicitly by the user.
2421 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002422 * @see Notification#deleteIntent
Joe Onoratocb109a02011-01-18 17:57:41 -08002423 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002424 public Builder setDeleteIntent(PendingIntent intent) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002425 mN.deleteIntent = intent;
Joe Onorato46439ce2010-11-19 13:56:21 -08002426 return this;
2427 }
2428
Joe Onoratocb109a02011-01-18 17:57:41 -08002429 /**
2430 * An intent to launch instead of posting the notification to the status bar.
2431 * Only for use with extremely high-priority notifications demanding the user's
2432 * <strong>immediate</strong> attention, such as an incoming phone call or
2433 * alarm clock that the user has explicitly set to a particular time.
2434 * If this facility is used for something else, please give the user an option
2435 * to turn it off and use a normal notification, as this can be extremely
2436 * disruptive.
2437 *
Chris Wren47c20a12014-06-18 17:27:29 -04002438 * <p>
2439 * The system UI may choose to display a heads-up notification, instead of
2440 * launching this intent, while the user is using the device.
2441 * </p>
2442 *
Joe Onoratocb109a02011-01-18 17:57:41 -08002443 * @param intent The pending intent to launch.
2444 * @param highPriority Passing true will cause this notification to be sent
2445 * even if other notifications are suppressed.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002446 *
2447 * @see Notification#fullScreenIntent
Joe Onoratocb109a02011-01-18 17:57:41 -08002448 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002449 public Builder setFullScreenIntent(PendingIntent intent, boolean highPriority) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002450 mN.fullScreenIntent = intent;
Joe Onorato46439ce2010-11-19 13:56:21 -08002451 setFlag(FLAG_HIGH_PRIORITY, highPriority);
2452 return this;
2453 }
2454
Joe Onoratocb109a02011-01-18 17:57:41 -08002455 /**
Dan Sandler5fcdf6e2014-07-18 11:31:15 -04002456 * Set the "ticker" text which is sent to accessibility services.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002457 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002458 * @see Notification#tickerText
Joe Onoratocb109a02011-01-18 17:57:41 -08002459 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002460 public Builder setTicker(CharSequence tickerText) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002461 mN.tickerText = safeCharSequence(tickerText);
Joe Onorato46439ce2010-11-19 13:56:21 -08002462 return this;
2463 }
2464
Joe Onoratocb109a02011-01-18 17:57:41 -08002465 /**
Dan Sandler5fcdf6e2014-07-18 11:31:15 -04002466 * Obsolete version of {@link #setTicker(CharSequence)}.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002467 *
Joe Onoratocb109a02011-01-18 17:57:41 -08002468 */
Dan Sandler5fcdf6e2014-07-18 11:31:15 -04002469 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -08002470 public Builder setTicker(CharSequence tickerText, RemoteViews views) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002471 setTicker(tickerText);
2472 // views is ignored
Joe Onorato46439ce2010-11-19 13:56:21 -08002473 return this;
2474 }
2475
Joe Onoratocb109a02011-01-18 17:57:41 -08002476 /**
Dan Sandlerd63f9322015-05-06 15:18:49 -04002477 * Add a large icon to the notification content view.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002478 *
2479 * In the platform template, this image will be shown on the left of the notification view
Dan Sandlerd63f9322015-05-06 15:18:49 -04002480 * in place of the {@link #setSmallIcon(Icon) small icon} (which will be placed in a small
2481 * badge atop the large icon).
Dan Sandler08a04c12015-05-06 15:18:49 -04002482 */
Dan Sandlerd63f9322015-05-06 15:18:49 -04002483 public Builder setLargeIcon(Bitmap b) {
2484 return setLargeIcon(b != null ? Icon.createWithBitmap(b) : null);
2485 }
2486
2487 /**
2488 * Add a large icon to the notification content view.
2489 *
2490 * In the platform template, this image will be shown on the left of the notification view
2491 * in place of the {@link #setSmallIcon(Icon) small icon} (which will be placed in a small
2492 * badge atop the large icon).
2493 */
2494 public Builder setLargeIcon(Icon icon) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002495 mN.mLargeIcon = icon;
2496 mN.extras.putParcelable(EXTRA_LARGE_ICON, icon);
Joe Onorato46439ce2010-11-19 13:56:21 -08002497 return this;
2498 }
2499
Joe Onoratocb109a02011-01-18 17:57:41 -08002500 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002501 * Set the sound to play.
2502 *
John Spurlockc0650f022014-07-19 13:22:39 -04002503 * It will be played using the {@link #AUDIO_ATTRIBUTES_DEFAULT default audio attributes}
2504 * for notifications.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002505 *
Chris Wren47c20a12014-06-18 17:27:29 -04002506 * <p>
2507 * A notification that is noisy is more likely to be presented as a heads-up notification.
2508 * </p>
2509 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002510 * @see Notification#sound
Joe Onoratocb109a02011-01-18 17:57:41 -08002511 */
Joe Onorato52f80cd2010-11-21 15:34:48 -08002512 public Builder setSound(Uri sound) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002513 mN.sound = sound;
2514 mN.audioAttributes = AUDIO_ATTRIBUTES_DEFAULT;
Joe Onorato52f80cd2010-11-21 15:34:48 -08002515 return this;
2516 }
2517
Joe Onoratocb109a02011-01-18 17:57:41 -08002518 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002519 * Set the sound to play, along with a specific stream on which to play it.
Joe Onoratocb109a02011-01-18 17:57:41 -08002520 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002521 * See {@link android.media.AudioManager} for the <code>STREAM_</code> constants.
2522 *
Chris Wren47c20a12014-06-18 17:27:29 -04002523 * <p>
2524 * A notification that is noisy is more likely to be presented as a heads-up notification.
2525 * </p>
John Spurlockc0650f022014-07-19 13:22:39 -04002526 * @deprecated use {@link #setSound(Uri, AudioAttributes)} instead.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002527 * @see Notification#sound
Joe Onoratocb109a02011-01-18 17:57:41 -08002528 */
Jean-Michel Trivi81f871e2014-08-06 16:32:38 -07002529 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -08002530 public Builder setSound(Uri sound, int streamType) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002531 mN.sound = sound;
2532 mN.audioStreamType = streamType;
Joe Onorato46439ce2010-11-19 13:56:21 -08002533 return this;
2534 }
2535
Joe Onoratocb109a02011-01-18 17:57:41 -08002536 /**
John Spurlockc0650f022014-07-19 13:22:39 -04002537 * Set the sound to play, along with specific {@link AudioAttributes audio attributes} to
2538 * use during playback.
2539 *
2540 * <p>
2541 * A notification that is noisy is more likely to be presented as a heads-up notification.
2542 * </p>
2543 *
2544 * @see Notification#sound
2545 */
2546 public Builder setSound(Uri sound, AudioAttributes audioAttributes) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002547 mN.sound = sound;
2548 mN.audioAttributes = audioAttributes;
John Spurlockc0650f022014-07-19 13:22:39 -04002549 return this;
2550 }
2551
2552 /**
Joe Onoratocb109a02011-01-18 17:57:41 -08002553 * Set the vibration pattern to use.
2554 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002555 * See {@link android.os.Vibrator#vibrate(long[], int)} for a discussion of the
2556 * <code>pattern</code> parameter.
2557 *
Chris Wren47c20a12014-06-18 17:27:29 -04002558 * <p>
2559 * A notification that vibrates is more likely to be presented as a heads-up notification.
2560 * </p>
2561 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002562 * @see Notification#vibrate
Joe Onoratocb109a02011-01-18 17:57:41 -08002563 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002564 public Builder setVibrate(long[] pattern) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002565 mN.vibrate = pattern;
Joe Onorato46439ce2010-11-19 13:56:21 -08002566 return this;
2567 }
2568
Joe Onoratocb109a02011-01-18 17:57:41 -08002569 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002570 * Set the desired color for the indicator LED on the device, as well as the
2571 * blink duty cycle (specified in milliseconds).
2572 *
2573
2574 * Not all devices will honor all (or even any) of these values.
2575 *
2576
2577 * @see Notification#ledARGB
2578 * @see Notification#ledOnMS
2579 * @see Notification#ledOffMS
Joe Onoratocb109a02011-01-18 17:57:41 -08002580 */
Tor Norbye80756e32015-03-02 09:39:27 -08002581 public Builder setLights(@ColorInt int argb, int onMs, int offMs) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002582 mN.ledARGB = argb;
2583 mN.ledOnMS = onMs;
2584 mN.ledOffMS = offMs;
Julia Reynolds10ee1fc2015-11-09 11:04:55 -05002585 if (onMs != 0 || offMs != 0) {
2586 mN.flags |= FLAG_SHOW_LIGHTS;
2587 }
Joe Onorato46439ce2010-11-19 13:56:21 -08002588 return this;
2589 }
2590
Joe Onoratocb109a02011-01-18 17:57:41 -08002591 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002592 * Set whether this is an "ongoing" notification.
Joe Onoratocb109a02011-01-18 17:57:41 -08002593 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002594
2595 * Ongoing notifications cannot be dismissed by the user, so your application or service
2596 * must take care of canceling them.
2597 *
2598
2599 * They are typically used to indicate a background task that the user is actively engaged
2600 * with (e.g., playing music) or is pending in some way and therefore occupying the device
2601 * (e.g., a file download, sync operation, active network connection).
2602 *
2603
2604 * @see Notification#FLAG_ONGOING_EVENT
2605 * @see Service#setForeground(boolean)
Joe Onoratocb109a02011-01-18 17:57:41 -08002606 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002607 public Builder setOngoing(boolean ongoing) {
2608 setFlag(FLAG_ONGOING_EVENT, ongoing);
2609 return this;
2610 }
2611
Joe Onoratocb109a02011-01-18 17:57:41 -08002612 /**
2613 * Set this flag if you would only like the sound, vibrate
2614 * and ticker to be played if the notification is not already showing.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002615 *
2616 * @see Notification#FLAG_ONLY_ALERT_ONCE
Joe Onoratocb109a02011-01-18 17:57:41 -08002617 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002618 public Builder setOnlyAlertOnce(boolean onlyAlertOnce) {
2619 setFlag(FLAG_ONLY_ALERT_ONCE, onlyAlertOnce);
2620 return this;
2621 }
2622
Joe Onoratocb109a02011-01-18 17:57:41 -08002623 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002624 * Make this notification automatically dismissed when the user touches it. The
2625 * PendingIntent set with {@link #setDeleteIntent} will be sent when this happens.
2626 *
2627 * @see Notification#FLAG_AUTO_CANCEL
Joe Onoratocb109a02011-01-18 17:57:41 -08002628 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002629 public Builder setAutoCancel(boolean autoCancel) {
Joe Onorato281d83f2011-01-04 17:13:10 -08002630 setFlag(FLAG_AUTO_CANCEL, autoCancel);
Joe Onorato46439ce2010-11-19 13:56:21 -08002631 return this;
2632 }
2633
Joe Onoratocb109a02011-01-18 17:57:41 -08002634 /**
Griff Hazendfcb0802014-02-11 12:00:00 -08002635 * Set whether or not this notification should not bridge to other devices.
2636 *
2637 * <p>Some notifications can be bridged to other devices for remote display.
2638 * This hint can be set to recommend this notification not be bridged.
2639 */
2640 public Builder setLocalOnly(boolean localOnly) {
2641 setFlag(FLAG_LOCAL_ONLY, localOnly);
2642 return this;
2643 }
2644
2645 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002646 * Set which notification properties will be inherited from system defaults.
Joe Onoratocb109a02011-01-18 17:57:41 -08002647 * <p>
2648 * The value should be one or more of the following fields combined with
2649 * bitwise-or:
2650 * {@link #DEFAULT_SOUND}, {@link #DEFAULT_VIBRATE}, {@link #DEFAULT_LIGHTS}.
2651 * <p>
2652 * For all default values, use {@link #DEFAULT_ALL}.
2653 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002654 public Builder setDefaults(int defaults) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002655 mN.defaults = defaults;
Joe Onorato46439ce2010-11-19 13:56:21 -08002656 return this;
2657 }
2658
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002659 /**
2660 * Set the priority of this notification.
2661 *
2662 * @see Notification#priority
2663 */
Tor Norbyed9273d62013-05-30 15:59:53 -07002664 public Builder setPriority(@Priority int pri) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002665 mN.priority = pri;
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002666 return this;
2667 }
Joe Malin8d40d042012-11-05 11:36:40 -08002668
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002669 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -04002670 * Set the notification category.
Joe Malin8d40d042012-11-05 11:36:40 -08002671 *
John Spurlockfd7f1e02014-03-18 16:41:57 -04002672 * @see Notification#category
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002673 */
John Spurlockfd7f1e02014-03-18 16:41:57 -04002674 public Builder setCategory(String category) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002675 mN.category = category;
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002676 return this;
2677 }
2678
2679 /**
Chris Wrendde75302014-03-26 17:24:15 -04002680 * Add a person that is relevant to this notification.
2681 *
Chris Wrene6c48932014-09-29 17:19:27 -04002682 * <P>
2683 * Depending on user preferences, this annotation may allow the notification to pass
2684 * through interruption filters, and to appear more prominently in the user interface.
2685 * </P>
2686 *
2687 * <P>
2688 * The person should be specified by the {@code String} representation of a
2689 * {@link android.provider.ContactsContract.Contacts#CONTENT_LOOKUP_URI}.
2690 * </P>
2691 *
2692 * <P>The system will also attempt to resolve {@code mailto:} and {@code tel:} schema
2693 * URIs. The path part of these URIs must exist in the contacts database, in the
2694 * appropriate column, or the reference will be discarded as invalid. Telephone schema
2695 * URIs will be resolved by {@link android.provider.ContactsContract.PhoneLookup}.
2696 * </P>
2697 *
2698 * @param uri A URI for the person.
Chris Wrendde75302014-03-26 17:24:15 -04002699 * @see Notification#EXTRA_PEOPLE
2700 */
Chris Wrene6c48932014-09-29 17:19:27 -04002701 public Builder addPerson(String uri) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002702 mPersonList.add(uri);
Chris Wrendde75302014-03-26 17:24:15 -04002703 return this;
2704 }
2705
2706 /**
Griff Hazen5cadc3b2014-05-20 09:55:39 -07002707 * Set this notification to be part of a group of notifications sharing the same key.
2708 * Grouped notifications may display in a cluster or stack on devices which
2709 * support such rendering.
2710 *
2711 * <p>To make this notification the summary for its group, also call
2712 * {@link #setGroupSummary}. A sort order can be specified for group members by using
2713 * {@link #setSortKey}.
2714 * @param groupKey The group key of the group.
2715 * @return this object for method chaining
2716 */
2717 public Builder setGroup(String groupKey) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002718 mN.mGroupKey = groupKey;
Griff Hazen5cadc3b2014-05-20 09:55:39 -07002719 return this;
2720 }
2721
2722 /**
2723 * Set this notification to be the group summary for a group of notifications.
2724 * Grouped notifications may display in a cluster or stack on devices which
2725 * support such rendering. Requires a group key also be set using {@link #setGroup}.
2726 * @param isGroupSummary Whether this notification should be a group summary.
2727 * @return this object for method chaining
2728 */
2729 public Builder setGroupSummary(boolean isGroupSummary) {
2730 setFlag(FLAG_GROUP_SUMMARY, isGroupSummary);
2731 return this;
2732 }
2733
2734 /**
2735 * Set a sort key that orders this notification among other notifications from the
2736 * same package. This can be useful if an external sort was already applied and an app
2737 * would like to preserve this. Notifications will be sorted lexicographically using this
2738 * value, although providing different priorities in addition to providing sort key may
2739 * cause this value to be ignored.
2740 *
2741 * <p>This sort key can also be used to order members of a notification group. See
Griff Hazen9e1379f2014-05-20 12:50:51 -07002742 * {@link #setGroup}.
Griff Hazen5cadc3b2014-05-20 09:55:39 -07002743 *
2744 * @see String#compareTo(String)
2745 */
2746 public Builder setSortKey(String sortKey) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002747 mN.mSortKey = sortKey;
Griff Hazen5cadc3b2014-05-20 09:55:39 -07002748 return this;
2749 }
2750
2751 /**
Griff Hazen720042b2014-02-24 15:46:56 -08002752 * Merge additional metadata into this notification.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002753 *
Griff Hazen720042b2014-02-24 15:46:56 -08002754 * <p>Values within the Bundle will replace existing extras values in this Builder.
2755 *
2756 * @see Notification#extras
2757 */
Griff Hazen959591e2014-05-15 22:26:18 -07002758 public Builder addExtras(Bundle extras) {
2759 if (extras != null) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002760 mUserExtras.putAll(extras);
Griff Hazen720042b2014-02-24 15:46:56 -08002761 }
2762 return this;
2763 }
2764
2765 /**
2766 * Set metadata for this notification.
2767 *
2768 * <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 -04002769 * current contents are copied into the Notification each time {@link #build()} is
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002770 * called.
2771 *
Griff Hazen720042b2014-02-24 15:46:56 -08002772 * <p>Replaces any existing extras values with those from the provided Bundle.
2773 * Use {@link #addExtras} to merge in metadata instead.
2774 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002775 * @see Notification#extras
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002776 */
Griff Hazen959591e2014-05-15 22:26:18 -07002777 public Builder setExtras(Bundle extras) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002778 if (extras != null) {
2779 mUserExtras = extras;
2780 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002781 return this;
2782 }
2783
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002784 /**
Griff Hazen720042b2014-02-24 15:46:56 -08002785 * Get the current metadata Bundle used by this notification Builder.
2786 *
2787 * <p>The returned Bundle is shared with this Builder.
2788 *
2789 * <p>The current contents of this Bundle are copied into the Notification each time
2790 * {@link #build()} is called.
2791 *
2792 * @see Notification#extras
2793 */
2794 public Bundle getExtras() {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002795 return mUserExtras;
2796 }
2797
2798 private Bundle getAllExtras() {
2799 final Bundle saveExtras = (Bundle) mUserExtras.clone();
2800 saveExtras.putAll(mN.extras);
2801 return saveExtras;
Griff Hazen720042b2014-02-24 15:46:56 -08002802 }
2803
2804 /**
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002805 * Add an action to this notification. Actions are typically displayed by
2806 * the system as a button adjacent to the notification content.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04002807 * <p>
2808 * Every action must have an icon (32dp square and matching the
2809 * <a href="{@docRoot}design/style/iconography.html#action-bar">Holo
2810 * Dark action bar</a> visual style), a textual label, and a {@link PendingIntent}.
2811 * <p>
2812 * A notification in its expanded form can display up to 3 actions, from left to right in
2813 * the order they were added. Actions will not be displayed when the notification is
2814 * collapsed, however, so be sure that any essential functions may be accessed by the user
2815 * in some other way (for example, in the Activity pointed to by {@link #contentIntent}).
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002816 *
2817 * @param icon Resource ID of a drawable that represents the action.
2818 * @param title Text describing the action.
2819 * @param intent PendingIntent to be fired when the action is invoked.
Dan Sandler86647982015-05-13 23:41:13 -04002820 *
2821 * @deprecated Use {@link #addAction(Action)} instead.
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002822 */
Dan Sandler86647982015-05-13 23:41:13 -04002823 @Deprecated
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002824 public Builder addAction(int icon, CharSequence title, PendingIntent intent) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04002825 mActions.add(new Action(icon, safeCharSequence(title), intent));
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002826 return this;
2827 }
2828
Chris Wrenfbd96ba2012-05-01 12:03:58 -04002829 /**
Griff Hazen959591e2014-05-15 22:26:18 -07002830 * Add an action to this notification. Actions are typically displayed by
2831 * the system as a button adjacent to the notification content.
2832 * <p>
2833 * Every action must have an icon (32dp square and matching the
2834 * <a href="{@docRoot}design/style/iconography.html#action-bar">Holo
2835 * Dark action bar</a> visual style), a textual label, and a {@link PendingIntent}.
2836 * <p>
2837 * A notification in its expanded form can display up to 3 actions, from left to right in
2838 * the order they were added. Actions will not be displayed when the notification is
2839 * collapsed, however, so be sure that any essential functions may be accessed by the user
2840 * in some other way (for example, in the Activity pointed to by {@link #contentIntent}).
2841 *
2842 * @param action The action to add.
2843 */
2844 public Builder addAction(Action action) {
2845 mActions.add(action);
2846 return this;
2847 }
2848
2849 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002850 * Alter the complete list of actions attached to this notification.
2851 * @see #addAction(Action).
2852 *
2853 * @param actions
2854 * @return
2855 */
2856 public Builder setActions(Action... actions) {
2857 mActions.clear();
2858 for (int i = 0; i < actions.length; i++) {
2859 mActions.add(actions[i]);
2860 }
2861 return this;
2862 }
2863
2864 /**
Chris Wrenfbd96ba2012-05-01 12:03:58 -04002865 * Add a rich notification style to be applied at build time.
2866 *
2867 * @param style Object responsible for modifying the notification style.
2868 */
2869 public Builder setStyle(Style style) {
2870 if (mStyle != style) {
2871 mStyle = style;
Daniel Sandlerc08dea22012-06-28 08:35:24 -07002872 if (mStyle != null) {
2873 mStyle.setBuilder(this);
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002874 mN.extras.putString(EXTRA_TEMPLATE, style.getClass().getName());
2875 } else {
2876 mN.extras.remove(EXTRA_TEMPLATE);
Daniel Sandlerc08dea22012-06-28 08:35:24 -07002877 }
Chris Wrenfbd96ba2012-05-01 12:03:58 -04002878 }
2879 return this;
2880 }
2881
Dan Sandler0bf2ed82013-12-21 23:33:41 -06002882 /**
2883 * Specify the value of {@link #visibility}.
Griff Hazenb720abe2014-05-20 13:15:30 -07002884 *
Dan Sandler0bf2ed82013-12-21 23:33:41 -06002885 * @param visibility One of {@link #VISIBILITY_PRIVATE} (the default),
2886 * {@link #VISIBILITY_SECRET}, or {@link #VISIBILITY_PUBLIC}.
2887 *
2888 * @return The same Builder.
2889 */
2890 public Builder setVisibility(int visibility) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002891 mN.visibility = visibility;
Dan Sandler0bf2ed82013-12-21 23:33:41 -06002892 return this;
2893 }
2894
2895 /**
2896 * Supply a replacement Notification whose contents should be shown in insecure contexts
2897 * (i.e. atop the secure lockscreen). See {@link #visibility} and {@link #VISIBILITY_PUBLIC}.
2898 * @param n A replacement notification, presumably with some or all info redacted.
2899 * @return The same Builder.
2900 */
2901 public Builder setPublicVersion(Notification n) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002902 if (n != null) {
2903 mN.publicVersion = new Notification();
2904 n.cloneInto(mN.publicVersion, /*heavy=*/ true);
2905 } else {
2906 mN.publicVersion = null;
2907 }
Dan Sandler0bf2ed82013-12-21 23:33:41 -06002908 return this;
2909 }
2910
Griff Hazenb720abe2014-05-20 13:15:30 -07002911 /**
Griff Hazen5cadc3b2014-05-20 09:55:39 -07002912 * Apply an extender to this notification builder. Extenders may be used to add
2913 * metadata or change options on this builder.
2914 */
Griff Hazen61a9e862014-05-22 16:05:19 -07002915 public Builder extend(Extender extender) {
2916 extender.extend(this);
Griff Hazen5cadc3b2014-05-20 09:55:39 -07002917 return this;
2918 }
2919
Dan Sandler4e787062015-06-17 15:09:48 -04002920 /**
2921 * @hide
2922 */
2923 public void setFlag(int mask, boolean value) {
Joe Onorato46439ce2010-11-19 13:56:21 -08002924 if (value) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002925 mN.flags |= mask;
Joe Onorato46439ce2010-11-19 13:56:21 -08002926 } else {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002927 mN.flags &= ~mask;
Joe Onorato46439ce2010-11-19 13:56:21 -08002928 }
2929 }
2930
Dan Sandler26e81cf2014-05-06 10:01:27 -04002931 /**
2932 * Sets {@link Notification#color}.
2933 *
2934 * @param argb The accent color to use
2935 *
2936 * @return The same Builder.
2937 */
Tor Norbye80756e32015-03-02 09:39:27 -08002938 public Builder setColor(@ColorInt int argb) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002939 mN.color = argb;
Julia Reynolds10ee1fc2015-11-09 11:04:55 -05002940 sanitizeColor();
Dan Sandler26e81cf2014-05-06 10:01:27 -04002941 return this;
2942 }
2943
Jorim Jaggid05aa3e2014-08-28 17:52:27 +02002944 private Drawable getProfileBadgeDrawable() {
Christoph Studer7ac80e62014-08-04 16:01:57 +02002945 // Note: This assumes that the current user can read the profile badge of the
2946 // originating user.
Selim Cineke6ff9462016-01-15 15:07:06 -08002947 return mContext.getPackageManager().getUserBadgeForDensityNoBackground(
Julia Reynoldsda303542015-11-23 14:00:20 -05002948 new UserHandle(mContext.getUserId()), 0);
Jorim Jaggid05aa3e2014-08-28 17:52:27 +02002949 }
2950
2951 private Bitmap getProfileBadge() {
2952 Drawable badge = getProfileBadgeDrawable();
Kenny Guy8a0101b2014-05-08 23:34:12 +01002953 if (badge == null) {
2954 return null;
2955 }
Jorim Jaggid05aa3e2014-08-28 17:52:27 +02002956 final int size = mContext.getResources().getDimensionPixelSize(
2957 R.dimen.notification_badge_size);
2958 Bitmap bitmap = Bitmap.createBitmap(size, size, Bitmap.Config.ARGB_8888);
Kenny Guy8a0101b2014-05-08 23:34:12 +01002959 Canvas canvas = new Canvas(bitmap);
Jorim Jaggid05aa3e2014-08-28 17:52:27 +02002960 badge.setBounds(0, 0, size, size);
Kenny Guy8a0101b2014-05-08 23:34:12 +01002961 badge.draw(canvas);
2962 return bitmap;
2963 }
2964
Selim Cinekc848c3a2016-01-13 15:27:30 -08002965 private void bindProfileBadge(RemoteViews contentView) {
Kenny Guy98193ea2014-07-24 19:54:37 +01002966 Bitmap profileBadge = getProfileBadge();
2967
Kenny Guy98193ea2014-07-24 19:54:37 +01002968 if (profileBadge != null) {
Selim Cinekc848c3a2016-01-13 15:27:30 -08002969 contentView.setImageViewBitmap(R.id.profile_badge, profileBadge);
2970 contentView.setViewVisibility(R.id.profile_badge, View.VISIBLE);
Kenny Guy98193ea2014-07-24 19:54:37 +01002971 }
Kenny Guy98193ea2014-07-24 19:54:37 +01002972 }
2973
Christoph Studerfe718432014-09-01 18:21:18 +02002974 private void resetStandardTemplate(RemoteViews contentView) {
Selim Cinekeaa29ca2015-11-23 13:51:13 -08002975 resetNotificationHeader(contentView);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07002976 resetContentMargins(contentView);
Christoph Studerfe718432014-09-01 18:21:18 +02002977 contentView.setViewVisibility(R.id.right_icon, View.GONE);
Selim Cinek860b6da2015-12-16 19:02:19 -08002978 contentView.setViewVisibility(R.id.title, View.GONE);
Christoph Studerfe718432014-09-01 18:21:18 +02002979 contentView.setTextViewText(R.id.title, null);
Selim Cinek41598732016-01-11 16:58:37 -08002980 contentView.setViewVisibility(R.id.text, View.GONE);
Christoph Studerfe718432014-09-01 18:21:18 +02002981 contentView.setTextViewText(R.id.text, null);
Selim Cinek29603462015-11-17 19:04:39 -08002982 contentView.setViewVisibility(R.id.text_line_1, View.GONE);
Selim Cinek41598732016-01-11 16:58:37 -08002983 contentView.setTextViewText(R.id.text_line_1, null);
Christoph Studerfe718432014-09-01 18:21:18 +02002984 contentView.setViewVisibility(R.id.progress, View.GONE);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07002985 }
2986
Selim Cinekeaa29ca2015-11-23 13:51:13 -08002987 /**
2988 * Resets the notification header to its original state
2989 */
2990 private void resetNotificationHeader(RemoteViews contentView) {
Selim Cinek65b2e7c2015-10-26 14:11:31 -07002991 contentView.setImageViewResource(R.id.icon, 0);
Selim Cinek7b836392015-12-04 20:02:59 -08002992 contentView.setBoolean(R.id.notification_header, "setExpanded", false);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07002993 contentView.setTextViewText(R.id.app_name_text, null);
Christoph Studerca1db712014-09-10 17:31:33 +02002994 contentView.setViewVisibility(R.id.chronometer, View.GONE);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07002995 contentView.setViewVisibility(R.id.header_sub_text, View.GONE);
Selim Cinek29603462015-11-17 19:04:39 -08002996 contentView.setViewVisibility(R.id.header_content_info, View.GONE);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07002997 contentView.setViewVisibility(R.id.number_of_children, View.GONE);
Selim Cinek29603462015-11-17 19:04:39 -08002998 contentView.setViewVisibility(R.id.sub_text_divider, View.GONE);
2999 contentView.setViewVisibility(R.id.content_info_divider, View.GONE);
3000 contentView.setViewVisibility(R.id.time_divider, View.GONE);
Selim Cinekc848c3a2016-01-13 15:27:30 -08003001 contentView.setImageViewIcon(R.id.profile_badge, null);
3002 contentView.setViewVisibility(R.id.profile_badge, View.GONE);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003003 }
3004
3005 private void resetContentMargins(RemoteViews contentView) {
3006 contentView.setViewLayoutMarginEnd(R.id.line1, 0);
Selim Cinekc848c3a2016-01-13 15:27:30 -08003007 contentView.setViewLayoutMarginEnd(R.id.text, 0);
Christoph Studerfe718432014-09-01 18:21:18 +02003008 }
3009
Jorim Jaggi445d3c02014-08-19 22:33:42 +02003010 private RemoteViews applyStandardTemplate(int resId) {
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02003011 return applyStandardTemplate(resId, true /* hasProgress */);
3012 }
3013
3014 /**
3015 * @param hasProgress whether the progress bar should be shown and set
3016 */
3017 private RemoteViews applyStandardTemplate(int resId, boolean hasProgress) {
Kenny Guy77320062014-08-27 21:37:15 +01003018 RemoteViews contentView = new BuilderRemoteViews(mContext.getApplicationInfo(), resId);
Dan Sandler539aad42014-08-04 00:43:39 -04003019
Christoph Studerfe718432014-09-01 18:21:18 +02003020 resetStandardTemplate(contentView);
3021
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003022 final Bundle ex = mN.extras;
Dan Sandler190d58d2014-05-15 09:33:39 -04003023
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003024 bindNotificationHeader(contentView);
3025 bindLargeIcon(contentView);
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003026 if (ex.getCharSequence(EXTRA_TITLE) != null) {
Selim Cinek860b6da2015-12-16 19:02:19 -08003027 contentView.setViewVisibility(R.id.title, View.VISIBLE);
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003028 contentView.setTextViewText(R.id.title,
3029 processLegacyText(ex.getCharSequence(EXTRA_TITLE)));
Joe Onorato561d3852010-11-20 18:09:34 -08003030 }
Selim Cinek29603462015-11-17 19:04:39 -08003031 boolean showProgress = handleProgressBar(hasProgress, contentView, ex);
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003032 if (ex.getCharSequence(EXTRA_TEXT) != null) {
Selim Cinek41598732016-01-11 16:58:37 -08003033 int textId = showProgress ? com.android.internal.R.id.text_line_1
3034 : com.android.internal.R.id.text;
3035 contentView.setTextViewText(textId, processLegacyText(
3036 ex.getCharSequence(EXTRA_TEXT)));
3037 contentView.setViewVisibility(textId, View.VISIBLE);
Joe Onorato561d3852010-11-20 18:09:34 -08003038 }
Selim Cinekc848c3a2016-01-13 15:27:30 -08003039
Selim Cinek860b6da2015-12-16 19:02:19 -08003040 setContentMinHeight(contentView, showProgress || mN.mLargeIcon != null);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003041
Selim Cinek29603462015-11-17 19:04:39 -08003042 return contentView;
3043 }
3044
Selim Cinek860b6da2015-12-16 19:02:19 -08003045 /**
3046 * @param remoteView the remote view to update the minheight in
3047 * @param hasMinHeight does it have a mimHeight
3048 * @hide
3049 */
3050 void setContentMinHeight(RemoteViews remoteView, boolean hasMinHeight) {
3051 int minHeight = 0;
3052 if (hasMinHeight) {
3053 // we need to set the minHeight of the notification
3054 minHeight = mContext.getResources().getDimensionPixelSize(
3055 com.android.internal.R.dimen.notification_min_content_height);
3056 }
3057 remoteView.setInt(R.id.notification_main_column, "setMinimumHeight", minHeight);
3058 }
3059
Selim Cinek29603462015-11-17 19:04:39 -08003060 private boolean handleProgressBar(boolean hasProgress, RemoteViews contentView, Bundle ex) {
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003061 final int max = ex.getInt(EXTRA_PROGRESS_MAX, 0);
3062 final int progress = ex.getInt(EXTRA_PROGRESS, 0);
3063 final boolean ind = ex.getBoolean(EXTRA_PROGRESS_INDETERMINATE);
3064 if (hasProgress && (max != 0 || ind)) {
Selim Cinek29603462015-11-17 19:04:39 -08003065 contentView.setViewVisibility(com.android.internal.R.id.progress, View.VISIBLE);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003066 contentView.setProgressBar(
Selim Cinek29603462015-11-17 19:04:39 -08003067 R.id.progress, max, progress, ind);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003068 contentView.setProgressBackgroundTintList(
3069 R.id.progress, ColorStateList.valueOf(mContext.getColor(
3070 R.color.notification_progress_background_color)));
Selim Cinek29603462015-11-17 19:04:39 -08003071 if (mN.color != COLOR_DEFAULT) {
3072 ColorStateList colorStateList = ColorStateList.valueOf(mN.color);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003073 contentView.setProgressTintList(R.id.progress, colorStateList);
3074 contentView.setProgressIndeterminateTintList(R.id.progress, colorStateList);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003075 }
Selim Cinek29603462015-11-17 19:04:39 -08003076 return true;
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003077 } else {
3078 contentView.setViewVisibility(R.id.progress, View.GONE);
Selim Cinek29603462015-11-17 19:04:39 -08003079 return false;
Jeff Sharkey1c400132011-08-05 14:50:13 -07003080 }
Joe Onorato561d3852010-11-20 18:09:34 -08003081 }
3082
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003083 private void bindLargeIcon(RemoteViews contentView) {
3084 if (mN.mLargeIcon != null) {
3085 contentView.setViewVisibility(R.id.right_icon, View.VISIBLE);
3086 contentView.setImageViewIcon(R.id.right_icon, mN.mLargeIcon);
3087 processLargeLegacyIcon(mN.mLargeIcon, contentView);
3088 int endMargin = mContext.getResources().getDimensionPixelSize(
3089 R.dimen.notification_content_picture_margin);
3090 contentView.setViewLayoutMarginEnd(R.id.line1, endMargin);
Selim Cinekc848c3a2016-01-13 15:27:30 -08003091 contentView.setViewLayoutMarginEnd(R.id.text, endMargin);
Selim Cinek29603462015-11-17 19:04:39 -08003092 contentView.setViewLayoutMarginEnd(R.id.progress, endMargin);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003093 }
3094 }
3095
3096 private void bindNotificationHeader(RemoteViews contentView) {
3097 bindSmallIcon(contentView);
Selim Cinekeaa29ca2015-11-23 13:51:13 -08003098 bindChildCountColor(contentView);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003099 bindHeaderAppName(contentView);
Selim Cinek03d0d652015-11-13 13:18:09 -05003100 bindHeaderSubText(contentView);
Selim Cinek29603462015-11-17 19:04:39 -08003101 bindContentInfo(contentView);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003102 bindHeaderChronometerAndTime(contentView);
3103 bindExpandButton(contentView);
Selim Cinekc848c3a2016-01-13 15:27:30 -08003104 bindProfileBadge(contentView);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003105 }
3106
Selim Cinekeaa29ca2015-11-23 13:51:13 -08003107 private void bindChildCountColor(RemoteViews contentView) {
3108 contentView.setTextColor(R.id.number_of_children, resolveColor());
3109 }
3110
Selim Cinek29603462015-11-17 19:04:39 -08003111 private void bindContentInfo(RemoteViews contentView) {
3112 boolean visible = false;
3113 if (mN.extras.getCharSequence(EXTRA_INFO_TEXT) != null) {
3114 contentView.setTextViewText(R.id.header_content_info,
3115 processLegacyText(mN.extras.getCharSequence(EXTRA_INFO_TEXT)));
3116 contentView.setViewVisibility(R.id.header_content_info, View.VISIBLE);
3117 visible = true;
3118 } else if (mN.number > 0) {
3119 final int tooBig = mContext.getResources().getInteger(
3120 R.integer.status_bar_notification_info_maxnum);
3121 if (mN.number > tooBig) {
3122 contentView.setTextViewText(R.id.header_content_info, processLegacyText(
3123 mContext.getResources().getString(
3124 R.string.status_bar_notification_info_overflow)));
3125 } else {
3126 contentView.setTextViewText(R.id.header_content_info,
3127 processLegacyText(String.valueOf(mN.number)));
3128 }
3129 contentView.setViewVisibility(R.id.header_content_info, View.VISIBLE);
3130 visible = true;
3131 }
3132 if (visible) {
3133 contentView.setViewVisibility(R.id.content_info_divider, View.VISIBLE);
3134 }
3135 }
3136
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003137 private void bindExpandButton(RemoteViews contentView) {
3138 contentView.setDrawableParameters(R.id.expand_button, false, -1, resolveColor(),
3139 PorterDuff.Mode.SRC_ATOP, -1);
Selim Cinekea4bef72015-12-02 15:51:10 -08003140 contentView.setInt(R.id.notification_header, "setOriginalNotificationColor",
3141 resolveColor());
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003142 }
3143
3144 private void bindHeaderChronometerAndTime(RemoteViews contentView) {
3145 if (showsTimeOrChronometer()) {
Selim Cinek29603462015-11-17 19:04:39 -08003146 contentView.setViewVisibility(R.id.time_divider, View.VISIBLE);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003147 if (mN.extras.getBoolean(EXTRA_SHOW_CHRONOMETER)) {
3148 contentView.setViewVisibility(R.id.chronometer, View.VISIBLE);
3149 contentView.setLong(R.id.chronometer, "setBase",
3150 mN.when + (SystemClock.elapsedRealtime() - System.currentTimeMillis()));
3151 contentView.setBoolean(R.id.chronometer, "setStarted", true);
Selim Cinek81c23aa2016-02-25 16:23:13 -08003152 boolean countsDown = mN.extras.getBoolean(EXTRA_CHRONOMETER_COUNTS_DOWN);
3153 contentView.setChronometerCountsDown(R.id.chronometer, countsDown);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003154 } else {
3155 contentView.setViewVisibility(R.id.time, View.VISIBLE);
3156 contentView.setLong(R.id.time, "setTime", mN.when);
3157 }
3158 }
3159 }
3160
Selim Cinek03d0d652015-11-13 13:18:09 -05003161 private void bindHeaderSubText(RemoteViews contentView) {
3162 CharSequence subText = mN.extras.getCharSequence(EXTRA_SUB_TEXT);
3163 if (subText == null && mStyle != null && mStyle.mSummaryTextSet
3164 && mStyle.hasSummaryInHeader()) {
3165 subText = mStyle.mSummaryText;
3166 }
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003167 if (subText != null) {
3168 // TODO: Remove the span entirely to only have the string with propper formating.
3169 contentView.setTextViewText(R.id.header_sub_text, processLegacyText(subText));
3170 contentView.setViewVisibility(R.id.header_sub_text, View.VISIBLE);
Selim Cinek29603462015-11-17 19:04:39 -08003171 contentView.setViewVisibility(R.id.sub_text_divider, View.VISIBLE);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003172 }
3173 }
3174
3175 private void bindHeaderAppName(RemoteViews contentView) {
3176 PackageManager packageManager = mContext.getPackageManager();
3177 ApplicationInfo info = null;
3178 try {
3179 info = packageManager.getApplicationInfo(mContext.getApplicationInfo().packageName,
3180 0);
3181 } catch (final NameNotFoundException e) {
3182 return;
3183 }
3184 CharSequence appName = info != null ? packageManager.getApplicationLabel(info)
3185 : null;
3186 if (TextUtils.isEmpty(appName)) {
3187 return;
3188 }
3189 contentView.setTextViewText(R.id.app_name_text, appName);
Selim Cinekaef485a2016-02-05 10:31:20 -08003190 contentView.setTextColor(R.id.app_name_text, resolveColor());
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003191 }
3192
3193 private void bindSmallIcon(RemoteViews contentView) {
3194 contentView.setImageViewIcon(R.id.icon, mN.mSmallIcon);
3195 processSmallIconColor(mN.mSmallIcon, contentView);
3196 }
3197
Jorim Jaggi445d3c02014-08-19 22:33:42 +02003198 /**
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02003199 * @return true if the built notification will show the time or the chronometer; false
3200 * otherwise
3201 */
3202 private boolean showsTimeOrChronometer() {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003203 return mN.when != 0 && mN.extras.getBoolean(EXTRA_SHOW_WHEN);
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02003204 }
3205
Christoph Studerfe718432014-09-01 18:21:18 +02003206 private void resetStandardTemplateWithActions(RemoteViews big) {
3207 big.setViewVisibility(R.id.actions, View.GONE);
Christoph Studerfe718432014-09-01 18:21:18 +02003208 big.removeAllViews(R.id.actions);
Adrian Roose458aa82015-12-08 16:17:19 -08003209
3210 big.setViewVisibility(R.id.notification_material_reply_container, View.GONE);
3211 big.setTextViewText(R.id.notification_material_reply_text_1, null);
3212
3213 big.setViewVisibility(R.id.notification_material_reply_text_2, View.GONE);
3214 big.setTextViewText(R.id.notification_material_reply_text_2, null);
3215 big.setViewVisibility(R.id.notification_material_reply_text_3, View.GONE);
3216 big.setTextViewText(R.id.notification_material_reply_text_3, null);
Christoph Studerfe718432014-09-01 18:21:18 +02003217 }
3218
Daniel Sandler96fd7c12012-03-30 16:37:36 -04003219 private RemoteViews applyStandardTemplateWithActions(int layoutId) {
Jorim Jaggi445d3c02014-08-19 22:33:42 +02003220 RemoteViews big = applyStandardTemplate(layoutId);
Daniel Sandler96fd7c12012-03-30 16:37:36 -04003221
Christoph Studerfe718432014-09-01 18:21:18 +02003222 resetStandardTemplateWithActions(big);
3223
Adrian Roose458aa82015-12-08 16:17:19 -08003224 boolean validRemoteInput = false;
3225
Daniel Sandler96fd7c12012-03-30 16:37:36 -04003226 int N = mActions.size();
3227 if (N > 0) {
Daniel Sandler96fd7c12012-03-30 16:37:36 -04003228 big.setViewVisibility(R.id.actions, View.VISIBLE);
Daniel Sandler8680bf82012-05-15 16:52:52 -04003229 if (N>MAX_ACTION_BUTTONS) N=MAX_ACTION_BUTTONS;
Daniel Sandler96fd7c12012-03-30 16:37:36 -04003230 for (int i=0; i<N; i++) {
Adrian Roose458aa82015-12-08 16:17:19 -08003231 Action action = mActions.get(i);
3232 validRemoteInput |= hasValidRemoteInput(action);
3233
3234 final RemoteViews button = generateActionButton(action);
Adrian Roos9b123cf2016-02-04 14:55:57 -08003235 if (i == N - 1) {
3236 button.setViewLayoutWidth(com.android.internal.R.id.action0,
3237 ViewGroup.LayoutParams.MATCH_PARENT);
3238 }
Daniel Sandler96fd7c12012-03-30 16:37:36 -04003239 big.addView(R.id.actions, button);
3240 }
3241 }
Adrian Roose458aa82015-12-08 16:17:19 -08003242
3243 CharSequence[] replyText = mN.extras.getCharSequenceArray(EXTRA_REMOTE_INPUT_HISTORY);
3244 if (validRemoteInput && replyText != null
3245 && replyText.length > 0 && !TextUtils.isEmpty(replyText[0])) {
3246 big.setViewVisibility(R.id.notification_material_reply_container, View.VISIBLE);
3247 big.setTextViewText(R.id.notification_material_reply_text_1, replyText[0]);
3248
3249 if (replyText.length > 1 && !TextUtils.isEmpty(replyText[1])) {
3250 big.setViewVisibility(R.id.notification_material_reply_text_2, View.VISIBLE);
3251 big.setTextViewText(R.id.notification_material_reply_text_2, replyText[1]);
3252
3253 if (replyText.length > 2 && !TextUtils.isEmpty(replyText[2])) {
3254 big.setViewVisibility(
3255 R.id.notification_material_reply_text_3, View.VISIBLE);
3256 big.setTextViewText(R.id.notification_material_reply_text_3, replyText[2]);
3257 }
3258 }
3259 }
3260
Daniel Sandler96fd7c12012-03-30 16:37:36 -04003261 return big;
3262 }
3263
Adrian Roose458aa82015-12-08 16:17:19 -08003264 private boolean hasValidRemoteInput(Action action) {
3265 if (TextUtils.isEmpty(action.title) || action.actionIntent == null) {
3266 // Weird actions
3267 return false;
3268 }
3269
3270 RemoteInput[] remoteInputs = action.getRemoteInputs();
3271 if (remoteInputs == null) {
3272 return false;
3273 }
3274
3275 for (RemoteInput r : remoteInputs) {
3276 CharSequence[] choices = r.getChoices();
3277 if (r.getAllowFreeFormInput() || (choices != null && choices.length != 0)) {
3278 return true;
3279 }
3280 }
3281 return false;
3282 }
3283
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003284 /**
3285 * Construct a RemoteViews for the final 1U notification layout. In order:
3286 * 1. Custom contentView from the caller
3287 * 2. Style's proposed content view
3288 * 3. Standard template view
3289 */
3290 public RemoteViews makeContentView() {
Selim Cinek593610c2016-02-16 18:42:57 -08003291 if (mN.contentView != null && (mStyle == null || !mStyle.displayCustomViewInline())) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003292 return mN.contentView;
3293 } else if (mStyle != null) {
3294 final RemoteViews styleView = mStyle.makeContentView();
3295 if (styleView != null) {
3296 return styleView;
3297 }
Joe Onorato46439ce2010-11-19 13:56:21 -08003298 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003299 return applyStandardTemplate(getBaseLayoutResource());
Joe Onorato46439ce2010-11-19 13:56:21 -08003300 }
3301
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003302 /**
3303 * Construct a RemoteViews for the final big notification layout.
3304 */
3305 public RemoteViews makeBigContentView() {
Selim Cinek850a8542015-11-11 11:48:36 -05003306 RemoteViews result = null;
Selim Cinek593610c2016-02-16 18:42:57 -08003307 if (mN.bigContentView != null
3308 && (mStyle == null || !mStyle.displayCustomViewInline())) {
Julia Reynolds089e3e42015-11-18 09:59:57 -05003309 return mN.bigContentView;
3310 } else if (mStyle != null) {
Selim Cinek850a8542015-11-11 11:48:36 -05003311 result = mStyle.makeBigContentView();
Selim Cinek90dcf6d2015-11-18 20:24:13 -08003312 hideLine1Text(result);
Selim Cinekcc10bfb2016-02-10 16:24:21 -08003313 } else if (mActions.size() != 0) {
3314 result = applyStandardTemplateWithActions(getBigBaseLayoutResource());
Selim Cinek850a8542015-11-11 11:48:36 -05003315 }
3316 adaptNotificationHeaderForBigContentView(result);
3317 return result;
3318 }
Daniel Sandler96fd7c12012-03-30 16:37:36 -04003319
Selim Cinekeaa29ca2015-11-23 13:51:13 -08003320 /**
3321 * Construct a RemoteViews for the final notification header only
3322 *
3323 * @hide
3324 */
3325 public RemoteViews makeNotificationHeader() {
3326 RemoteViews header = new BuilderRemoteViews(mContext.getApplicationInfo(),
3327 R.layout.notification_template_header);
3328 resetNotificationHeader(header);
3329 bindNotificationHeader(header);
3330 return header;
3331 }
3332
Selim Cinek29603462015-11-17 19:04:39 -08003333 private void hideLine1Text(RemoteViews result) {
Selim Cinekcc10bfb2016-02-10 16:24:21 -08003334 if (result != null) {
3335 result.setViewVisibility(R.id.text_line_1, View.GONE);
3336 }
Selim Cinek29603462015-11-17 19:04:39 -08003337 }
3338
Selim Cinek850a8542015-11-11 11:48:36 -05003339 private void adaptNotificationHeaderForBigContentView(RemoteViews result) {
Selim Cinekcc10bfb2016-02-10 16:24:21 -08003340 if (result != null) {
3341 result.setBoolean(R.id.notification_header, "setExpanded", true);
3342 }
Daniel Sandler96fd7c12012-03-30 16:37:36 -04003343 }
3344
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003345 /**
3346 * Construct a RemoteViews for the final heads-up notification layout.
3347 */
3348 public RemoteViews makeHeadsUpContentView() {
Selim Cinek593610c2016-02-16 18:42:57 -08003349 if (mN.headsUpContentView != null
3350 && (mStyle == null || !mStyle.displayCustomViewInline())) {
Julia Reynolds089e3e42015-11-18 09:59:57 -05003351 return mN.headsUpContentView;
3352 } else if (mStyle != null) {
3353 final RemoteViews styleView = mStyle.makeHeadsUpContentView();
3354 if (styleView != null) {
3355 return styleView;
3356 }
3357 } else if (mActions.size() == 0) {
3358 return null;
3359 }
3360
Chris Wren8fd39ec2014-02-27 17:43:26 -05003361
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01003362 return applyStandardTemplateWithActions(getBigBaseLayoutResource());
Chris Wren8fd39ec2014-02-27 17:43:26 -05003363 }
3364
Selim Cinek624c02db2015-12-14 21:00:02 -08003365 /**
3366 * Construct a RemoteViews for the display in public contexts like on the lockscreen.
3367 *
3368 * @hide
3369 */
3370 public RemoteViews makePublicContentView() {
3371 if (mN.publicVersion != null) {
3372 final Builder builder = recoverBuilder(mContext, mN.publicVersion);
3373 return builder.makeContentView();
3374 }
3375 Bundle savedBundle = mN.extras;
3376 Style style = mStyle;
3377 mStyle = null;
3378 Icon largeIcon = mN.mLargeIcon;
3379 mN.mLargeIcon = null;
3380 Bundle publicExtras = new Bundle();
3381 publicExtras.putBoolean(EXTRA_SHOW_WHEN,
3382 savedBundle.getBoolean(EXTRA_SHOW_WHEN));
3383 publicExtras.putBoolean(EXTRA_SHOW_CHRONOMETER,
3384 savedBundle.getBoolean(EXTRA_SHOW_CHRONOMETER));
Selim Cinek81c23aa2016-02-25 16:23:13 -08003385 publicExtras.putBoolean(EXTRA_CHRONOMETER_COUNTS_DOWN,
3386 savedBundle.getBoolean(EXTRA_CHRONOMETER_COUNTS_DOWN));
Selim Cinek624c02db2015-12-14 21:00:02 -08003387 publicExtras.putCharSequence(EXTRA_TITLE,
3388 mContext.getString(R.string.notification_hidden_text));
3389 mN.extras = publicExtras;
3390 final RemoteViews publicView = applyStandardTemplate(getBaseLayoutResource());
3391 mN.extras = savedBundle;
3392 mN.mLargeIcon = largeIcon;
3393 mStyle = style;
3394 return publicView;
3395 }
3396
3397
Chris Wren8fd39ec2014-02-27 17:43:26 -05003398
Daniel Sandler96fd7c12012-03-30 16:37:36 -04003399 private RemoteViews generateActionButton(Action action) {
Daniel Sandler8680bf82012-05-15 16:52:52 -04003400 final boolean tombstone = (action.actionIntent == null);
Selim Cinekf33b1112015-07-15 17:45:11 -07003401 RemoteViews button = new BuilderRemoteViews(mContext.getApplicationInfo(),
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01003402 tombstone ? getActionTombstoneLayoutResource()
3403 : getActionLayoutResource());
Dan Sandler68079d52015-07-22 10:45:30 -04003404 final Icon ai = action.getIcon();
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01003405 button.setTextViewText(R.id.action0, processLegacyText(action.title));
Daniel Sandler8680bf82012-05-15 16:52:52 -04003406 if (!tombstone) {
Daniel Sandlere5518842012-05-10 16:20:40 -04003407 button.setOnClickPendingIntent(R.id.action0, action.actionIntent);
Daniel Sandlere5518842012-05-10 16:20:40 -04003408 }
Daniel Sandler96fd7c12012-03-30 16:37:36 -04003409 button.setContentDescription(R.id.action0, action.title);
Adrian Roosfe84e1f2015-11-04 15:55:39 -08003410 if (action.mRemoteInputs != null) {
3411 button.setRemoteInputs(R.id.action0, action.mRemoteInputs);
3412 }
3413 if (mN.color != COLOR_DEFAULT) {
3414 button.setTextColor(R.id.action0, mN.color);
3415 }
Daniel Sandler96fd7c12012-03-30 16:37:36 -04003416 return button;
3417 }
3418
Joe Onoratocb109a02011-01-18 17:57:41 -08003419 /**
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01003420 * @return Whether we are currently building a notification from a legacy (an app that
Alan Viverette3cb07a462014-06-06 14:19:53 -07003421 * doesn't create material notifications by itself) app.
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01003422 */
3423 private boolean isLegacy() {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003424 return getColorUtil() != null;
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01003425 }
3426
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01003427 private CharSequence processLegacyText(CharSequence charSequence) {
3428 if (isLegacy()) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003429 return getColorUtil().invertCharSequenceColors(charSequence);
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01003430 } else {
3431 return charSequence;
3432 }
3433 }
3434
Dan Sandler26e81cf2014-05-06 10:01:27 -04003435 /**
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003436 * Apply any necessariy colors to the small icon
Dan Sandler26e81cf2014-05-06 10:01:27 -04003437 */
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003438 private void processSmallIconColor(Icon smallIcon, RemoteViews contentView) {
Selim Cinekea4bef72015-12-02 15:51:10 -08003439 boolean colorable = !isLegacy() || getColorUtil().isGrayscaleIcon(mContext, smallIcon);
3440 if (colorable) {
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003441 contentView.setDrawableParameters(R.id.icon, false, -1, resolveColor(),
Jorim Jaggi92df1f22014-12-16 19:44:41 +01003442 PorterDuff.Mode.SRC_ATOP, -1);
Selim Cinekea4bef72015-12-02 15:51:10 -08003443
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01003444 }
Selim Cinekea4bef72015-12-02 15:51:10 -08003445 contentView.setInt(R.id.notification_header, "setOriginalIconColor",
3446 colorable ? resolveColor() : NotificationHeaderView.NO_COLOR);
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01003447 }
3448
Dan Sandler26e81cf2014-05-06 10:01:27 -04003449 /**
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003450 * Make the largeIcon dark if it's a fake smallIcon (that is,
Dan Sandler26e81cf2014-05-06 10:01:27 -04003451 * if it's grayscale).
3452 */
3453 // TODO: also check bounds, transparency, that sort of thing.
Dan Sandlerd63f9322015-05-06 15:18:49 -04003454 private void processLargeLegacyIcon(Icon largeIcon, RemoteViews contentView) {
3455 if (largeIcon != null && isLegacy()
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003456 && getColorUtil().isGrayscaleIcon(mContext, largeIcon)) {
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003457 // resolve color will fall back to the default when legacy
3458 contentView.setDrawableParameters(R.id.icon, false, -1, resolveColor(),
Dan Sandler190d58d2014-05-15 09:33:39 -04003459 PorterDuff.Mode.SRC_ATOP, -1);
Jorim Jaggi92df1f22014-12-16 19:44:41 +01003460 }
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01003461 }
3462
Julia Reynolds10ee1fc2015-11-09 11:04:55 -05003463 private void sanitizeColor() {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003464 if (mN.color != COLOR_DEFAULT) {
3465 mN.color |= 0xFF000000; // no alpha for custom colors
Jorim Jaggi74419312014-06-10 20:57:21 +02003466 }
Jorim Jaggi74419312014-06-10 20:57:21 +02003467 }
3468
Selim Cinek5bf069a2015-11-10 19:14:27 -05003469 int resolveColor() {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003470 if (mN.color == COLOR_DEFAULT) {
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003471 return mContext.getColor(R.color.notification_icon_default_color);
Dan Sandler26e81cf2014-05-06 10:01:27 -04003472 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003473 return mN.color;
Dan Sandler26e81cf2014-05-06 10:01:27 -04003474 }
3475
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01003476 /**
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003477 * Apply the unstyled operations and return a new {@link Notification} object.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04003478 * @hide
Joe Onoratocb109a02011-01-18 17:57:41 -08003479 */
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04003480 public Notification buildUnstyled() {
Daniel Sandlera0a938c2012-03-15 08:42:37 -04003481 if (mActions.size() > 0) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003482 mN.actions = new Action[mActions.size()];
3483 mActions.toArray(mN.actions);
Daniel Sandlera0a938c2012-03-15 08:42:37 -04003484 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003485 if (!mPersonList.isEmpty()) {
3486 mN.extras.putStringArray(EXTRA_PEOPLE,
3487 mPersonList.toArray(new String[mPersonList.size()]));
Dan Sandler0bf2ed82013-12-21 23:33:41 -06003488 }
Selim Cinek247fa012016-02-18 09:50:48 -08003489 if (mN.bigContentView != null || mN.contentView != null
3490 || mN.headsUpContentView != null) {
3491 mN.extras.putBoolean(EXTRA_CONTAINS_CUSTOM_VIEW, true);
3492 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003493 return mN;
Joe Onorato46439ce2010-11-19 13:56:21 -08003494 }
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003495
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003496 public static Notification.Builder recoverBuilder(Context context, Notification n) {
Christoph Studer4600f9b2014-07-22 22:44:43 +02003497 // Re-create notification context so we can access app resources.
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003498 ApplicationInfo applicationInfo = n.extras.getParcelable(
3499 EXTRA_BUILDER_APPLICATION_INFO);
Christoph Studer4600f9b2014-07-22 22:44:43 +02003500 Context builderContext;
Julia Reynoldsda303542015-11-23 14:00:20 -05003501 if (applicationInfo != null) {
3502 try {
3503 builderContext = context.createApplicationContext(applicationInfo,
3504 Context.CONTEXT_RESTRICTED);
3505 } catch (NameNotFoundException e) {
3506 Log.e(TAG, "ApplicationInfo " + applicationInfo + " not found");
3507 builderContext = context; // try with our context
3508 }
3509 } else {
3510 builderContext = context; // try with given context
Christoph Studer4600f9b2014-07-22 22:44:43 +02003511 }
3512
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003513 return new Builder(builderContext, n);
Christoph Studer4600f9b2014-07-22 22:44:43 +02003514 }
3515
3516 private static Class<? extends Style> getNotificationStyleClass(String templateClass) {
Selim Cinek593610c2016-02-16 18:42:57 -08003517 Class<? extends Style>[] classes = new Class[] {
3518 BigTextStyle.class, BigPictureStyle.class, InboxStyle.class, MediaStyle.class,
Selim Cinek03eb3b72016-02-18 10:39:45 -08003519 DecoratedCustomViewStyle.class, DecoratedMediaCustomViewStyle.class };
Christoph Studer4600f9b2014-07-22 22:44:43 +02003520 for (Class<? extends Style> innerClass : classes) {
3521 if (templateClass.equals(innerClass.getName())) {
3522 return innerClass;
3523 }
3524 }
3525 return null;
3526 }
3527
Daniel Sandlerf45564e2013-04-15 15:05:08 -04003528 /**
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003529 * @deprecated Use {@link #build()} instead.
3530 */
3531 @Deprecated
3532 public Notification getNotification() {
3533 return build();
3534 }
3535
3536 /**
3537 * Combine all of the options that have been set and return a new {@link Notification}
3538 * object.
3539 */
3540 public Notification build() {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003541 // first, add any extras from the calling code
3542 if (mUserExtras != null) {
3543 mN.extras = getAllExtras();
Jorim Jaggia0d58ae2015-06-03 11:48:13 -07003544 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003545
3546 // lazy stuff from mContext; see comment in Builder(Context, Notification)
Julia Reynoldsda303542015-11-23 14:00:20 -05003547 Notification.addFieldsFromContext(mContext, mN);
Christoph Studer943aa672014-08-03 20:31:16 +02003548
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003549 buildUnstyled();
Daniel Sandlerf45564e2013-04-15 15:05:08 -04003550
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003551 if (mStyle != null) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003552 mStyle.buildStyled(mN);
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003553 }
Daniel Sandlerf45564e2013-04-15 15:05:08 -04003554
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05003555 if (mContext.getApplicationInfo().targetSdkVersion < Build.VERSION_CODES.N) {
3556 if (mN.contentView == null) {
3557 mN.contentView = makeContentView();
3558 mN.extras.putInt(EXTRA_REBUILD_CONTENT_VIEW_ACTION_COUNT,
3559 mN.contentView.getSequenceNumber());
3560 }
3561 if (mN.bigContentView == null) {
3562 mN.bigContentView = makeBigContentView();
3563 if (mN.bigContentView != null) {
3564 mN.extras.putInt(EXTRA_REBUILD_BIG_CONTENT_VIEW_ACTION_COUNT,
3565 mN.bigContentView.getSequenceNumber());
3566 }
3567 }
3568 if (mN.headsUpContentView == null) {
3569 mN.headsUpContentView = makeHeadsUpContentView();
3570 if (mN.headsUpContentView != null) {
3571 mN.extras.putInt(EXTRA_REBUILD_HEADS_UP_CONTENT_VIEW_ACTION_COUNT,
3572 mN.headsUpContentView.getSequenceNumber());
3573 }
3574 }
3575 }
3576
Julia Reynolds4c0c2022016-02-02 15:11:59 -05003577 if ((mN.defaults & DEFAULT_LIGHTS) != 0) {
3578 mN.flags |= FLAG_SHOW_LIGHTS;
3579 }
3580
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003581 return mN;
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003582 }
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05003583
3584 /**
3585 * Apply this Builder to an existing {@link Notification} object.
3586 *
3587 * @hide
3588 */
3589 public Notification buildInto(Notification n) {
Daniel Sandler1a497d32013-04-18 14:52:45 -04003590 build().cloneInto(n, true);
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05003591 return n;
3592 }
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01003593
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05003594 /**
3595 * @hide
3596 */
3597 public static void stripForDelivery(Notification n) {
3598 String templateClass = n.extras.getString(EXTRA_TEMPLATE);
3599 if (TextUtils.isEmpty(templateClass)) {
3600 return;
3601 }
3602 // Only strip views for known Styles because we won't know how to
3603 // re-create them otherwise.
3604 if (getNotificationStyleClass(templateClass) == null) {
3605 return;
3606 }
3607 // Get rid of unmodified BuilderRemoteViews.
3608 if (n.contentView instanceof BuilderRemoteViews &&
3609 n.extras.getInt(EXTRA_REBUILD_CONTENT_VIEW_ACTION_COUNT, -1) ==
3610 n.contentView.getSequenceNumber()) {
3611 n.contentView = null;
3612 n.extras.remove(EXTRA_REBUILD_CONTENT_VIEW_ACTION_COUNT);
3613 }
3614 if (n.bigContentView instanceof BuilderRemoteViews &&
3615 n.extras.getInt(EXTRA_REBUILD_BIG_CONTENT_VIEW_ACTION_COUNT, -1) ==
3616 n.bigContentView.getSequenceNumber()) {
3617 n.bigContentView = null;
3618 n.extras.remove(EXTRA_REBUILD_BIG_CONTENT_VIEW_ACTION_COUNT);
3619 }
3620 if (n.headsUpContentView instanceof BuilderRemoteViews &&
3621 n.extras.getInt(EXTRA_REBUILD_HEADS_UP_CONTENT_VIEW_ACTION_COUNT, -1) ==
3622 n.headsUpContentView.getSequenceNumber()) {
3623 n.headsUpContentView = null;
3624 n.extras.remove(EXTRA_REBUILD_HEADS_UP_CONTENT_VIEW_ACTION_COUNT);
3625 }
3626 }
3627
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01003628 private int getBaseLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07003629 return R.layout.notification_template_material_base;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01003630 }
3631
3632 private int getBigBaseLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07003633 return R.layout.notification_template_material_big_base;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01003634 }
3635
3636 private int getBigPictureLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07003637 return R.layout.notification_template_material_big_picture;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01003638 }
3639
3640 private int getBigTextLayoutResource() {
Jorim Jaggi445d3c02014-08-19 22:33:42 +02003641 return R.layout.notification_template_material_big_text;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01003642 }
3643
3644 private int getInboxLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07003645 return R.layout.notification_template_material_inbox;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01003646 }
3647
3648 private int getActionLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07003649 return R.layout.notification_material_action;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01003650 }
3651
3652 private int getActionTombstoneLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07003653 return R.layout.notification_material_action_tombstone;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01003654 }
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003655 }
3656
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003657 /**
3658 * An object that can apply a rich notification style to a {@link Notification.Builder}
3659 * object.
3660 */
Griff Hazendfcb0802014-02-11 12:00:00 -08003661 public static abstract class Style {
Chris Wrend6297db2012-05-03 16:20:13 -04003662 private CharSequence mBigContentTitle;
Jorim Jaggi457a10d2014-09-08 16:18:23 +02003663
3664 /**
3665 * @hide
3666 */
3667 protected CharSequence mSummaryText = null;
3668
3669 /**
3670 * @hide
3671 */
3672 protected boolean mSummaryTextSet = false;
Chris Wrend6297db2012-05-03 16:20:13 -04003673
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003674 protected Builder mBuilder;
3675
Chris Wrend6297db2012-05-03 16:20:13 -04003676 /**
3677 * Overrides ContentTitle in the big form of the template.
3678 * This defaults to the value passed to setContentTitle().
3679 */
3680 protected void internalSetBigContentTitle(CharSequence title) {
3681 mBigContentTitle = title;
3682 }
3683
3684 /**
3685 * Set the first line of text after the detail section in the big form of the template.
3686 */
3687 protected void internalSetSummaryText(CharSequence cs) {
3688 mSummaryText = cs;
Daniel Sandler619738c2012-06-07 16:33:08 -04003689 mSummaryTextSet = true;
Chris Wrend6297db2012-05-03 16:20:13 -04003690 }
3691
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003692 public void setBuilder(Builder builder) {
3693 if (mBuilder != builder) {
3694 mBuilder = builder;
Daniel Sandlerc08dea22012-06-28 08:35:24 -07003695 if (mBuilder != null) {
3696 mBuilder.setStyle(this);
3697 }
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003698 }
3699 }
3700
Chris Wrend6297db2012-05-03 16:20:13 -04003701 protected void checkBuilder() {
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003702 if (mBuilder == null) {
3703 throw new IllegalArgumentException("Style requires a valid Builder object");
3704 }
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003705 }
Chris Wrend6297db2012-05-03 16:20:13 -04003706
3707 protected RemoteViews getStandardView(int layoutId) {
3708 checkBuilder();
3709
Christoph Studer4600f9b2014-07-22 22:44:43 +02003710 // Nasty.
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003711 CharSequence oldBuilderContentTitle =
3712 mBuilder.getAllExtras().getCharSequence(EXTRA_TITLE);
Chris Wrend6297db2012-05-03 16:20:13 -04003713 if (mBigContentTitle != null) {
3714 mBuilder.setContentTitle(mBigContentTitle);
3715 }
3716
Chris Wrend6297db2012-05-03 16:20:13 -04003717 RemoteViews contentView = mBuilder.applyStandardTemplateWithActions(layoutId);
3718
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003719 mBuilder.getAllExtras().putCharSequence(EXTRA_TITLE, oldBuilderContentTitle);
Christoph Studer4600f9b2014-07-22 22:44:43 +02003720
Chris Wrend6297db2012-05-03 16:20:13 -04003721 if (mBigContentTitle != null && mBigContentTitle.equals("")) {
3722 contentView.setViewVisibility(R.id.line1, View.GONE);
Chris Wren67dc9a02012-05-16 01:03:20 -04003723 } else {
3724 contentView.setViewVisibility(R.id.line1, View.VISIBLE);
Chris Wrend6297db2012-05-03 16:20:13 -04003725 }
3726
Chris Wrend6297db2012-05-03 16:20:13 -04003727 return contentView;
3728 }
3729
Daniel Sandlerf45564e2013-04-15 15:05:08 -04003730 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003731 * Construct a Style-specific RemoteViews for the final 1U notification layout.
3732 * The default implementation has nothing additional to add.
3733 * @hide
3734 */
3735 public RemoteViews makeContentView() {
3736 return null;
3737 }
3738
3739 /**
3740 * Construct a Style-specific RemoteViews for the final big notification layout.
3741 * @hide
3742 */
3743 public RemoteViews makeBigContentView() {
3744 return null;
3745 }
3746
3747 /**
3748 * Construct a Style-specific RemoteViews for the final HUN layout.
3749 * @hide
3750 */
3751 public RemoteViews makeHeadsUpContentView() {
3752 return null;
3753 }
3754
3755 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003756 * Apply any style-specific extras to this notification before shipping it out.
Daniel Sandlerf45564e2013-04-15 15:05:08 -04003757 * @hide
3758 */
3759 public void addExtras(Bundle extras) {
3760 if (mSummaryTextSet) {
3761 extras.putCharSequence(EXTRA_SUMMARY_TEXT, mSummaryText);
3762 }
3763 if (mBigContentTitle != null) {
3764 extras.putCharSequence(EXTRA_TITLE_BIG, mBigContentTitle);
3765 }
Chris Wren91ad5632013-06-05 15:05:57 -04003766 extras.putString(EXTRA_TEMPLATE, this.getClass().getName());
Daniel Sandlerf45564e2013-04-15 15:05:08 -04003767 }
3768
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04003769 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003770 * Reconstruct the internal state of this Style object from extras.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04003771 * @hide
3772 */
Christoph Studer4600f9b2014-07-22 22:44:43 +02003773 protected void restoreFromExtras(Bundle extras) {
3774 if (extras.containsKey(EXTRA_SUMMARY_TEXT)) {
3775 mSummaryText = extras.getCharSequence(EXTRA_SUMMARY_TEXT);
3776 mSummaryTextSet = true;
3777 }
3778 if (extras.containsKey(EXTRA_TITLE_BIG)) {
3779 mBigContentTitle = extras.getCharSequence(EXTRA_TITLE_BIG);
3780 }
3781 }
3782
3783
3784 /**
3785 * @hide
3786 */
3787 public Notification buildStyled(Notification wip) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003788 addExtras(wip.extras);
Christoph Studer4600f9b2014-07-22 22:44:43 +02003789 return wip;
3790 }
3791
Daniel Sandler0ec46202015-06-24 01:27:05 -04003792 /**
3793 * @hide
3794 */
Jorim Jaggia0d58ae2015-06-03 11:48:13 -07003795 public void purgeResources() {}
3796
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04003797 /**
3798 * Calls {@link android.app.Notification.Builder#build()} on the Builder this Style is
3799 * attached to.
3800 *
3801 * @return the fully constructed Notification.
3802 */
3803 public Notification build() {
3804 checkBuilder();
3805 return mBuilder.build();
3806 }
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02003807
3808 /**
3809 * @hide
3810 * @return true if the style positions the progress bar on the second line; false if the
3811 * style hides the progress bar
3812 */
3813 protected boolean hasProgress() {
3814 return true;
3815 }
Selim Cinek03d0d652015-11-13 13:18:09 -05003816
3817 /**
3818 * @hide
3819 * @return Whether we should put the summary be put into the notification header
3820 */
3821 public boolean hasSummaryInHeader() {
3822 return true;
3823 }
Selim Cinek593610c2016-02-16 18:42:57 -08003824
3825 /**
3826 * @hide
3827 * @return Whether custom content views are displayed inline in the style
3828 */
3829 public boolean displayCustomViewInline() {
3830 return false;
3831 }
Joe Onorato46439ce2010-11-19 13:56:21 -08003832 }
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003833
3834 /**
Daniel Sandler4dfbe832012-04-11 14:51:46 -04003835 * Helper class for generating large-format notifications that include a large image attachment.
Joe Malin8d40d042012-11-05 11:36:40 -08003836 *
Robert Ly91c5ce32014-06-08 15:37:00 -07003837 * Here's how you'd set the <code>BigPictureStyle</code> on a notification:
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003838 * <pre class="prettyprint">
Robert Ly91c5ce32014-06-08 15:37:00 -07003839 * Notification notif = new Notification.Builder(mContext)
3840 * .setContentTitle(&quot;New photo from &quot; + sender.toString())
3841 * .setContentText(subject)
3842 * .setSmallIcon(R.drawable.new_post)
3843 * .setLargeIcon(aBitmap)
3844 * .setStyle(new Notification.BigPictureStyle()
3845 * .bigPicture(aBigBitmap))
3846 * .build();
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003847 * </pre>
Joe Malin8d40d042012-11-05 11:36:40 -08003848 *
Daniel Sandler4dfbe832012-04-11 14:51:46 -04003849 * @see Notification#bigContentView
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003850 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003851 public static class BigPictureStyle extends Style {
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003852 private Bitmap mPicture;
Dan Sandlerd63f9322015-05-06 15:18:49 -04003853 private Icon mBigLargeIcon;
Chris Wren3745a3d2012-05-22 15:11:52 -04003854 private boolean mBigLargeIconSet = false;
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003855
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003856 public BigPictureStyle() {
3857 }
3858
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003859 public BigPictureStyle(Builder builder) {
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003860 setBuilder(builder);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003861 }
3862
Chris Wrend6297db2012-05-03 16:20:13 -04003863 /**
3864 * Overrides ContentTitle in the big form of the template.
3865 * This defaults to the value passed to setContentTitle().
3866 */
3867 public BigPictureStyle setBigContentTitle(CharSequence title) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04003868 internalSetBigContentTitle(safeCharSequence(title));
Chris Wrend6297db2012-05-03 16:20:13 -04003869 return this;
3870 }
3871
3872 /**
3873 * Set the first line of text after the detail section in the big form of the template.
3874 */
3875 public BigPictureStyle setSummaryText(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04003876 internalSetSummaryText(safeCharSequence(cs));
Chris Wrend6297db2012-05-03 16:20:13 -04003877 return this;
3878 }
3879
Chris Wren0bd664d2012-08-01 13:56:56 -04003880 /**
3881 * Provide the bitmap to be used as the payload for the BigPicture notification.
3882 */
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003883 public BigPictureStyle bigPicture(Bitmap b) {
3884 mPicture = b;
3885 return this;
3886 }
3887
Chris Wren3745a3d2012-05-22 15:11:52 -04003888 /**
Chris Wren3745a3d2012-05-22 15:11:52 -04003889 * Override the large icon when the big notification is shown.
3890 */
3891 public BigPictureStyle bigLargeIcon(Bitmap b) {
Dan Sandlerd63f9322015-05-06 15:18:49 -04003892 return bigLargeIcon(b != null ? Icon.createWithBitmap(b) : null);
3893 }
3894
3895 /**
3896 * Override the large icon when the big notification is shown.
3897 */
3898 public BigPictureStyle bigLargeIcon(Icon icon) {
Chris Wren3745a3d2012-05-22 15:11:52 -04003899 mBigLargeIconSet = true;
Dan Sandlerd63f9322015-05-06 15:18:49 -04003900 mBigLargeIcon = icon;
Chris Wren3745a3d2012-05-22 15:11:52 -04003901 return this;
3902 }
3903
Riley Andrews0394a0c2015-11-03 23:36:52 -08003904 /** @hide */
3905 public static final int MIN_ASHMEM_BITMAP_SIZE = 128 * (1 << 10);
3906
Daniel Sandler0ec46202015-06-24 01:27:05 -04003907 /**
3908 * @hide
3909 */
Jorim Jaggia0d58ae2015-06-03 11:48:13 -07003910 @Override
3911 public void purgeResources() {
3912 super.purgeResources();
Riley Andrews8cee7c12015-11-01 23:36:04 -08003913 if (mPicture != null &&
3914 mPicture.isMutable() &&
Riley Andrews0394a0c2015-11-03 23:36:52 -08003915 mPicture.getAllocationByteCount() >= MIN_ASHMEM_BITMAP_SIZE) {
Jorim Jaggia0d58ae2015-06-03 11:48:13 -07003916 mPicture = mPicture.createAshmemBitmap();
3917 }
3918 if (mBigLargeIcon != null) {
3919 mBigLargeIcon.convertToAshmem();
3920 }
3921 }
Christoph Studer5c510ee2014-12-15 16:32:27 +01003922
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003923 /**
3924 * @hide
3925 */
3926 public RemoteViews makeBigContentView() {
3927 // Replace mN.mLargeIcon with mBigLargeIcon if mBigLargeIconSet
Christoph Studer5c510ee2014-12-15 16:32:27 +01003928 // This covers the following cases:
3929 // 1. mBigLargeIconSet -> mBigLargeIcon (null or non-null) applies, overrides
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003930 // mN.mLargeIcon
3931 // 2. !mBigLargeIconSet -> mN.mLargeIcon applies
Dan Sandlerd63f9322015-05-06 15:18:49 -04003932 Icon oldLargeIcon = null;
Christoph Studer5c510ee2014-12-15 16:32:27 +01003933 if (mBigLargeIconSet) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003934 oldLargeIcon = mBuilder.mN.mLargeIcon;
3935 mBuilder.mN.mLargeIcon = mBigLargeIcon;
Christoph Studer5c510ee2014-12-15 16:32:27 +01003936 }
3937
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01003938 RemoteViews contentView = getStandardView(mBuilder.getBigPictureLayoutResource());
Selim Cinek03d0d652015-11-13 13:18:09 -05003939 if (mSummaryTextSet) {
3940 contentView.setTextViewText(R.id.text, mBuilder.processLegacyText(mSummaryText));
Selim Cinekc848c3a2016-01-13 15:27:30 -08003941 contentView.setViewVisibility(R.id.text, View.VISIBLE);
Selim Cinek03d0d652015-11-13 13:18:09 -05003942 }
Selim Cinek860b6da2015-12-16 19:02:19 -08003943 mBuilder.setContentMinHeight(contentView, mBuilder.mN.mLargeIcon != null);
Selim Cinek53e64a42015-11-16 10:40:56 -08003944
Christoph Studer5c510ee2014-12-15 16:32:27 +01003945 if (mBigLargeIconSet) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003946 mBuilder.mN.mLargeIcon = oldLargeIcon;
Christoph Studer5c510ee2014-12-15 16:32:27 +01003947 }
3948
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003949 contentView.setImageViewBitmap(R.id.big_picture, mPicture);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003950 return contentView;
3951 }
3952
Daniel Sandlerf45564e2013-04-15 15:05:08 -04003953 /**
3954 * @hide
3955 */
3956 public void addExtras(Bundle extras) {
3957 super.addExtras(extras);
3958
3959 if (mBigLargeIconSet) {
3960 extras.putParcelable(EXTRA_LARGE_ICON_BIG, mBigLargeIcon);
3961 }
3962 extras.putParcelable(EXTRA_PICTURE, mPicture);
3963 }
3964
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04003965 /**
3966 * @hide
3967 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003968 @Override
Christoph Studer4600f9b2014-07-22 22:44:43 +02003969 protected void restoreFromExtras(Bundle extras) {
3970 super.restoreFromExtras(extras);
3971
3972 if (extras.containsKey(EXTRA_LARGE_ICON_BIG)) {
Christoph Studer5c510ee2014-12-15 16:32:27 +01003973 mBigLargeIconSet = true;
Christoph Studer4600f9b2014-07-22 22:44:43 +02003974 mBigLargeIcon = extras.getParcelable(EXTRA_LARGE_ICON_BIG);
Chris Wren3745a3d2012-05-22 15:11:52 -04003975 }
Christoph Studer4600f9b2014-07-22 22:44:43 +02003976 mPicture = extras.getParcelable(EXTRA_PICTURE);
3977 }
Selim Cinek03d0d652015-11-13 13:18:09 -05003978
3979 /**
3980 * @hide
3981 */
3982 @Override
3983 public boolean hasSummaryInHeader() {
3984 return false;
3985 }
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003986 }
3987
3988 /**
Daniel Sandler4dfbe832012-04-11 14:51:46 -04003989 * Helper class for generating large-format notifications that include a lot of text.
Joe Malin8d40d042012-11-05 11:36:40 -08003990 *
Robert Ly91c5ce32014-06-08 15:37:00 -07003991 * Here's how you'd set the <code>BigTextStyle</code> on a notification:
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003992 * <pre class="prettyprint">
Robert Ly91c5ce32014-06-08 15:37:00 -07003993 * Notification notif = new Notification.Builder(mContext)
3994 * .setContentTitle(&quot;New mail from &quot; + sender.toString())
3995 * .setContentText(subject)
3996 * .setSmallIcon(R.drawable.new_mail)
3997 * .setLargeIcon(aBitmap)
3998 * .setStyle(new Notification.BigTextStyle()
3999 * .bigText(aVeryLongString))
4000 * .build();
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004001 * </pre>
Joe Malin8d40d042012-11-05 11:36:40 -08004002 *
Daniel Sandler4dfbe832012-04-11 14:51:46 -04004003 * @see Notification#bigContentView
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004004 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004005 public static class BigTextStyle extends Style {
Jorim Jaggi457a10d2014-09-08 16:18:23 +02004006
4007 private static final int MAX_LINES = 13;
Selim Cinek3a2c4b92015-12-17 17:01:17 -08004008 private static final int LINES_CONSUMED_BY_ACTIONS = 4;
Jorim Jaggi457a10d2014-09-08 16:18:23 +02004009
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004010 private CharSequence mBigText;
4011
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004012 public BigTextStyle() {
4013 }
4014
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004015 public BigTextStyle(Builder builder) {
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004016 setBuilder(builder);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004017 }
4018
Chris Wrend6297db2012-05-03 16:20:13 -04004019 /**
4020 * Overrides ContentTitle in the big form of the template.
4021 * This defaults to the value passed to setContentTitle().
4022 */
4023 public BigTextStyle setBigContentTitle(CharSequence title) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04004024 internalSetBigContentTitle(safeCharSequence(title));
Chris Wrend6297db2012-05-03 16:20:13 -04004025 return this;
4026 }
4027
4028 /**
4029 * Set the first line of text after the detail section in the big form of the template.
4030 */
4031 public BigTextStyle setSummaryText(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04004032 internalSetSummaryText(safeCharSequence(cs));
Chris Wrend6297db2012-05-03 16:20:13 -04004033 return this;
4034 }
4035
Chris Wren0bd664d2012-08-01 13:56:56 -04004036 /**
4037 * Provide the longer text to be displayed in the big form of the
4038 * template in place of the content text.
4039 */
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004040 public BigTextStyle bigText(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04004041 mBigText = safeCharSequence(cs);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004042 return this;
4043 }
4044
Daniel Sandlerf45564e2013-04-15 15:05:08 -04004045 /**
4046 * @hide
4047 */
4048 public void addExtras(Bundle extras) {
4049 super.addExtras(extras);
4050
Christoph Studer4600f9b2014-07-22 22:44:43 +02004051 extras.putCharSequence(EXTRA_BIG_TEXT, mBigText);
4052 }
4053
4054 /**
4055 * @hide
4056 */
4057 @Override
4058 protected void restoreFromExtras(Bundle extras) {
4059 super.restoreFromExtras(extras);
4060
4061 mBigText = extras.getCharSequence(EXTRA_BIG_TEXT);
Daniel Sandlerf45564e2013-04-15 15:05:08 -04004062 }
4063
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004064 /**
4065 * @hide
4066 */
4067 public RemoteViews makeBigContentView() {
Christoph Studer4600f9b2014-07-22 22:44:43 +02004068
4069 // Nasty
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004070 CharSequence oldBuilderContentText =
4071 mBuilder.getAllExtras().getCharSequence(EXTRA_TEXT);
4072 mBuilder.getAllExtras().putCharSequence(EXTRA_TEXT, null);
Daniel Sandler916ad912012-06-13 12:17:07 -04004073
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01004074 RemoteViews contentView = getStandardView(mBuilder.getBigTextLayoutResource());
Joe Malin8d40d042012-11-05 11:36:40 -08004075
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004076 mBuilder.getAllExtras().putCharSequence(EXTRA_TEXT, oldBuilderContentText);
Christoph Studer4600f9b2014-07-22 22:44:43 +02004077
Selim Cinek3a2c4b92015-12-17 17:01:17 -08004078 CharSequence bigTextText = mBuilder.processLegacyText(mBigText);
4079 contentView.setTextViewText(R.id.big_text, bigTextText);
4080 contentView.setViewVisibility(R.id.big_text,
4081 TextUtils.isEmpty(bigTextText) ? View.GONE : View.VISIBLE);
Jorim Jaggi457a10d2014-09-08 16:18:23 +02004082 contentView.setInt(R.id.big_text, "setMaxLines", calculateMaxLines());
Selim Cinek4fb12d32015-11-19 18:10:48 -08004083 contentView.setBoolean(R.id.big_text, "setHasImage", mBuilder.mN.mLargeIcon != null);
4084
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004085 return contentView;
4086 }
4087
Jorim Jaggi457a10d2014-09-08 16:18:23 +02004088 private int calculateMaxLines() {
4089 int lineCount = MAX_LINES;
4090 boolean hasActions = mBuilder.mActions.size() > 0;
Jorim Jaggi457a10d2014-09-08 16:18:23 +02004091 if (hasActions) {
4092 lineCount -= LINES_CONSUMED_BY_ACTIONS;
4093 }
Jorim Jaggi457a10d2014-09-08 16:18:23 +02004094 return lineCount;
4095 }
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004096 }
Daniel Sandler879c5e02012-04-17 16:46:51 -04004097
4098 /**
4099 * Helper class for generating large-format notifications that include a list of (up to 5) strings.
Joe Malin8d40d042012-11-05 11:36:40 -08004100 *
Robert Ly91c5ce32014-06-08 15:37:00 -07004101 * Here's how you'd set the <code>InboxStyle</code> on a notification:
Daniel Sandler879c5e02012-04-17 16:46:51 -04004102 * <pre class="prettyprint">
Robert Ly91c5ce32014-06-08 15:37:00 -07004103 * Notification notif = new Notification.Builder(mContext)
4104 * .setContentTitle(&quot;5 New mails from &quot; + sender.toString())
4105 * .setContentText(subject)
4106 * .setSmallIcon(R.drawable.new_mail)
4107 * .setLargeIcon(aBitmap)
4108 * .setStyle(new Notification.InboxStyle()
4109 * .addLine(str1)
4110 * .addLine(str2)
4111 * .setContentTitle(&quot;&quot;)
4112 * .setSummaryText(&quot;+3 more&quot;))
4113 * .build();
Daniel Sandler879c5e02012-04-17 16:46:51 -04004114 * </pre>
Joe Malin8d40d042012-11-05 11:36:40 -08004115 *
Daniel Sandler879c5e02012-04-17 16:46:51 -04004116 * @see Notification#bigContentView
4117 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004118 public static class InboxStyle extends Style {
Daniel Sandler879c5e02012-04-17 16:46:51 -04004119 private ArrayList<CharSequence> mTexts = new ArrayList<CharSequence>(5);
4120
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004121 public InboxStyle() {
4122 }
4123
Daniel Sandler879c5e02012-04-17 16:46:51 -04004124 public InboxStyle(Builder builder) {
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004125 setBuilder(builder);
Daniel Sandler879c5e02012-04-17 16:46:51 -04004126 }
4127
Chris Wrend6297db2012-05-03 16:20:13 -04004128 /**
4129 * Overrides ContentTitle in the big form of the template.
4130 * This defaults to the value passed to setContentTitle().
4131 */
4132 public InboxStyle setBigContentTitle(CharSequence title) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04004133 internalSetBigContentTitle(safeCharSequence(title));
Chris Wrend6297db2012-05-03 16:20:13 -04004134 return this;
4135 }
4136
4137 /**
4138 * Set the first line of text after the detail section in the big form of the template.
4139 */
4140 public InboxStyle setSummaryText(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04004141 internalSetSummaryText(safeCharSequence(cs));
Chris Wrend6297db2012-05-03 16:20:13 -04004142 return this;
4143 }
4144
Chris Wren0bd664d2012-08-01 13:56:56 -04004145 /**
4146 * Append a line to the digest section of the Inbox notification.
4147 */
Daniel Sandler879c5e02012-04-17 16:46:51 -04004148 public InboxStyle addLine(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04004149 mTexts.add(safeCharSequence(cs));
Daniel Sandler879c5e02012-04-17 16:46:51 -04004150 return this;
4151 }
4152
Daniel Sandlerf45564e2013-04-15 15:05:08 -04004153 /**
4154 * @hide
4155 */
4156 public void addExtras(Bundle extras) {
4157 super.addExtras(extras);
Christoph Studer4600f9b2014-07-22 22:44:43 +02004158
Daniel Sandlerf45564e2013-04-15 15:05:08 -04004159 CharSequence[] a = new CharSequence[mTexts.size()];
4160 extras.putCharSequenceArray(EXTRA_TEXT_LINES, mTexts.toArray(a));
4161 }
4162
Christoph Studer4600f9b2014-07-22 22:44:43 +02004163 /**
4164 * @hide
4165 */
4166 @Override
4167 protected void restoreFromExtras(Bundle extras) {
4168 super.restoreFromExtras(extras);
4169
4170 mTexts.clear();
4171 if (extras.containsKey(EXTRA_TEXT_LINES)) {
4172 Collections.addAll(mTexts, extras.getCharSequenceArray(EXTRA_TEXT_LINES));
4173 }
4174 }
4175
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004176 /**
4177 * @hide
4178 */
4179 public RemoteViews makeBigContentView() {
Selim Cinekc848c3a2016-01-13 15:27:30 -08004180 // Remove the content text so it disappears unless you have a summary
Christoph Studer4600f9b2014-07-22 22:44:43 +02004181 // Nasty
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004182 CharSequence oldBuilderContentText = mBuilder.mN.extras.getCharSequence(EXTRA_TEXT);
4183 mBuilder.getAllExtras().putCharSequence(EXTRA_TEXT, null);
Christoph Studer4600f9b2014-07-22 22:44:43 +02004184
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01004185 RemoteViews contentView = getStandardView(mBuilder.getInboxLayoutResource());
Daniel Sandler619738c2012-06-07 16:33:08 -04004186
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004187 mBuilder.getAllExtras().putCharSequence(EXTRA_TEXT, oldBuilderContentText);
Christoph Studer4600f9b2014-07-22 22:44:43 +02004188
Chris Wrend6297db2012-05-03 16:20:13 -04004189 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 -04004190 R.id.inbox_text4, R.id.inbox_text5, R.id.inbox_text6};
Chris Wrend6297db2012-05-03 16:20:13 -04004191
Chris Wren4ed80d52012-05-17 09:30:03 -04004192 // Make sure all rows are gone in case we reuse a view.
4193 for (int rowId : rowIds) {
4194 contentView.setViewVisibility(rowId, View.GONE);
4195 }
4196
Jorim Jaggi445d3c02014-08-19 22:33:42 +02004197 final boolean largeText =
4198 mBuilder.mContext.getResources().getConfiguration().fontScale > 1f;
4199 final float subTextSize = mBuilder.mContext.getResources().getDimensionPixelSize(
4200 R.dimen.notification_subtext_size);
Daniel Sandler879c5e02012-04-17 16:46:51 -04004201 int i=0;
Selim Cinek9d9fc6e2015-11-12 15:49:14 -05004202 final float density = mBuilder.mContext.getResources().getDisplayMetrics().density;
4203 int topPadding = (int) (5 * density);
4204 int bottomPadding = (int) (13 * density);
Selim Cinek247fa012016-02-18 09:50:48 -08004205 boolean first = true;
Daniel Sandler879c5e02012-04-17 16:46:51 -04004206 while (i < mTexts.size() && i < rowIds.length) {
4207 CharSequence str = mTexts.get(i);
4208 if (str != null && !str.equals("")) {
4209 contentView.setViewVisibility(rowIds[i], View.VISIBLE);
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01004210 contentView.setTextViewText(rowIds[i], mBuilder.processLegacyText(str));
Jorim Jaggi445d3c02014-08-19 22:33:42 +02004211 if (largeText) {
4212 contentView.setTextViewTextSize(rowIds[i], TypedValue.COMPLEX_UNIT_PX,
4213 subTextSize);
4214 }
Selim Cinek9d9fc6e2015-11-12 15:49:14 -05004215 contentView.setViewPadding(rowIds[i], 0, topPadding, 0,
4216 i == rowIds.length - 1 || i == mTexts.size() - 1 ? bottomPadding : 0);
Selim Cinek247fa012016-02-18 09:50:48 -08004217 handleInboxImageMargin(contentView, rowIds[i], first);
4218 first = false;
Daniel Sandler879c5e02012-04-17 16:46:51 -04004219 }
4220 i++;
4221 }
Kenny Guy98193ea2014-07-24 19:54:37 +01004222
Selim Cinek1e0bf612015-11-20 15:57:26 -08004223
Daniel Sandler879c5e02012-04-17 16:46:51 -04004224 return contentView;
4225 }
Selim Cinek1e0bf612015-11-20 15:57:26 -08004226
Selim Cinek247fa012016-02-18 09:50:48 -08004227 private void handleInboxImageMargin(RemoteViews contentView, int id, boolean first) {
Selim Cinek1e0bf612015-11-20 15:57:26 -08004228 int endMargin = 0;
Selim Cinek247fa012016-02-18 09:50:48 -08004229 if (first) {
4230 final int max = mBuilder.mN.extras.getInt(EXTRA_PROGRESS_MAX, 0);
4231 final boolean ind = mBuilder.mN.extras.getBoolean(EXTRA_PROGRESS_INDETERMINATE);
4232 boolean hasProgress = max != 0 || ind;
4233 if (mBuilder.mN.mLargeIcon != null && !hasProgress) {
4234 endMargin = mBuilder.mContext.getResources().getDimensionPixelSize(
4235 R.dimen.notification_content_picture_margin);
4236 }
Selim Cinek1e0bf612015-11-20 15:57:26 -08004237 }
4238 contentView.setViewLayoutMarginEnd(id, endMargin);
4239 }
Daniel Sandler879c5e02012-04-17 16:46:51 -04004240 }
Dan Sandler842dd772014-05-15 09:36:47 -04004241
4242 /**
4243 * Notification style for media playback notifications.
4244 *
4245 * In the expanded form, {@link Notification#bigContentView}, up to 5
4246 * {@link Notification.Action}s specified with
Dan Sandler86647982015-05-13 23:41:13 -04004247 * {@link Notification.Builder#addAction(Action) addAction} will be
Dan Sandler842dd772014-05-15 09:36:47 -04004248 * shown as icon-only pushbuttons, suitable for transport controls. The Bitmap given to
4249 * {@link Notification.Builder#setLargeIcon(android.graphics.Bitmap) setLargeIcon()} will be
4250 * treated as album artwork.
4251 *
4252 * Unlike the other styles provided here, MediaStyle can also modify the standard-size
4253 * {@link Notification#contentView}; by providing action indices to
Christoph Studerfde6f4d2014-12-12 13:23:26 +01004254 * {@link #setShowActionsInCompactView(int...)} you can promote up to 3 actions to be displayed
Dan Sandler842dd772014-05-15 09:36:47 -04004255 * in the standard view alongside the usual content.
4256 *
Bryan Mawhinney6be8de32014-07-18 10:35:12 +01004257 * Notifications created with MediaStyle will have their category set to
4258 * {@link Notification#CATEGORY_TRANSPORT CATEGORY_TRANSPORT} unless you set a different
4259 * category using {@link Notification.Builder#setCategory(String) setCategory()}.
4260 *
Jeff Browndba34ba2014-06-24 20:46:03 -07004261 * Finally, if you attach a {@link android.media.session.MediaSession.Token} using
4262 * {@link android.app.Notification.MediaStyle#setMediaSession(MediaSession.Token)},
Dan Sandler842dd772014-05-15 09:36:47 -04004263 * the System UI can identify this as a notification representing an active media session
4264 * and respond accordingly (by showing album artwork in the lockscreen, for example).
4265 *
4266 * To use this style with your Notification, feed it to
4267 * {@link Notification.Builder#setStyle(android.app.Notification.Style)} like so:
4268 * <pre class="prettyprint">
4269 * Notification noti = new Notification.Builder()
4270 * .setSmallIcon(R.drawable.ic_stat_player)
Christoph Studere935fe92014-11-24 14:18:06 +01004271 * .setContentTitle(&quot;Track title&quot;)
4272 * .setContentText(&quot;Artist - Album&quot;)
4273 * .setLargeIcon(albumArtBitmap))
Bryan Mawhinney6be8de32014-07-18 10:35:12 +01004274 * .setStyle(<b>new Notification.MediaStyle()</b>
4275 * .setMediaSession(mySession))
Dan Sandler842dd772014-05-15 09:36:47 -04004276 * .build();
4277 * </pre>
4278 *
4279 * @see Notification#bigContentView
4280 */
4281 public static class MediaStyle extends Style {
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02004282 static final int MAX_MEDIA_BUTTONS_IN_COMPACT = 3;
Dan Sandler842dd772014-05-15 09:36:47 -04004283 static final int MAX_MEDIA_BUTTONS = 5;
4284
4285 private int[] mActionsToShowInCompact = null;
Jeff Browndba34ba2014-06-24 20:46:03 -07004286 private MediaSession.Token mToken;
Dan Sandler842dd772014-05-15 09:36:47 -04004287
4288 public MediaStyle() {
4289 }
4290
4291 public MediaStyle(Builder builder) {
4292 setBuilder(builder);
4293 }
4294
4295 /**
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02004296 * 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 -04004297 * notification view.
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02004298 *
4299 * @param actions the indices of the actions to show in the compact notification view
Dan Sandler842dd772014-05-15 09:36:47 -04004300 */
4301 public MediaStyle setShowActionsInCompactView(int...actions) {
4302 mActionsToShowInCompact = actions;
4303 return this;
4304 }
4305
4306 /**
Jeff Browndba34ba2014-06-24 20:46:03 -07004307 * Attach a {@link android.media.session.MediaSession.Token} to this Notification
4308 * to provide additional playback information and control to the SystemUI.
Dan Sandler842dd772014-05-15 09:36:47 -04004309 */
Jeff Browndba34ba2014-06-24 20:46:03 -07004310 public MediaStyle setMediaSession(MediaSession.Token token) {
Dan Sandler842dd772014-05-15 09:36:47 -04004311 mToken = token;
4312 return this;
4313 }
4314
Christoph Studer4600f9b2014-07-22 22:44:43 +02004315 /**
4316 * @hide
4317 */
Dan Sandler842dd772014-05-15 09:36:47 -04004318 @Override
4319 public Notification buildStyled(Notification wip) {
Christoph Studer4600f9b2014-07-22 22:44:43 +02004320 super.buildStyled(wip);
Bryan Mawhinney6be8de32014-07-18 10:35:12 +01004321 if (wip.category == null) {
4322 wip.category = Notification.CATEGORY_TRANSPORT;
4323 }
Dan Sandler842dd772014-05-15 09:36:47 -04004324 return wip;
4325 }
4326
Christoph Studer4600f9b2014-07-22 22:44:43 +02004327 /**
4328 * @hide
4329 */
4330 @Override
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004331 public RemoteViews makeContentView() {
4332 return makeMediaContentView();
Christoph Studer4600f9b2014-07-22 22:44:43 +02004333 }
4334
4335 /**
4336 * @hide
4337 */
4338 @Override
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004339 public RemoteViews makeBigContentView() {
4340 return makeMediaBigContentView();
Christoph Studer4600f9b2014-07-22 22:44:43 +02004341 }
4342
Selim Cinekcc10bfb2016-02-10 16:24:21 -08004343 /**
4344 * @hide
4345 */
4346 @Override
4347 public RemoteViews makeHeadsUpContentView() {
4348 RemoteViews expanded = makeMediaBigContentView();
4349 return expanded != null ? expanded : makeMediaContentView();
4350 }
4351
Dan Sandler842dd772014-05-15 09:36:47 -04004352 /** @hide */
4353 @Override
4354 public void addExtras(Bundle extras) {
4355 super.addExtras(extras);
4356
4357 if (mToken != null) {
4358 extras.putParcelable(EXTRA_MEDIA_SESSION, mToken);
4359 }
Bryan Mawhinneye191f902014-07-22 12:50:09 +01004360 if (mActionsToShowInCompact != null) {
4361 extras.putIntArray(EXTRA_COMPACT_ACTIONS, mActionsToShowInCompact);
4362 }
Dan Sandler842dd772014-05-15 09:36:47 -04004363 }
4364
Christoph Studer4600f9b2014-07-22 22:44:43 +02004365 /**
4366 * @hide
4367 */
4368 @Override
4369 protected void restoreFromExtras(Bundle extras) {
4370 super.restoreFromExtras(extras);
4371
4372 if (extras.containsKey(EXTRA_MEDIA_SESSION)) {
4373 mToken = extras.getParcelable(EXTRA_MEDIA_SESSION);
4374 }
4375 if (extras.containsKey(EXTRA_COMPACT_ACTIONS)) {
4376 mActionsToShowInCompact = extras.getIntArray(EXTRA_COMPACT_ACTIONS);
4377 }
4378 }
4379
Selim Cinek5bf069a2015-11-10 19:14:27 -05004380 private RemoteViews generateMediaActionButton(Action action, int color) {
Dan Sandler842dd772014-05-15 09:36:47 -04004381 final boolean tombstone = (action.actionIntent == null);
Selim Cinekf33b1112015-07-15 17:45:11 -07004382 RemoteViews button = new BuilderRemoteViews(mBuilder.mContext.getApplicationInfo(),
Alan Viverette3cb07a462014-06-06 14:19:53 -07004383 R.layout.notification_material_media_action);
Dan Sandler68079d52015-07-22 10:45:30 -04004384 button.setImageViewIcon(R.id.action0, action.getIcon());
Selim Cinek5bf069a2015-11-10 19:14:27 -05004385 button.setDrawableParameters(R.id.action0, false, -1, color, PorterDuff.Mode.SRC_ATOP,
4386 -1);
Dan Sandler842dd772014-05-15 09:36:47 -04004387 if (!tombstone) {
4388 button.setOnClickPendingIntent(R.id.action0, action.actionIntent);
4389 }
4390 button.setContentDescription(R.id.action0, action.title);
4391 return button;
4392 }
4393
4394 private RemoteViews makeMediaContentView() {
4395 RemoteViews view = mBuilder.applyStandardTemplate(
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02004396 R.layout.notification_template_material_media, false /* hasProgress */);
Dan Sandler842dd772014-05-15 09:36:47 -04004397
4398 final int numActions = mBuilder.mActions.size();
4399 final int N = mActionsToShowInCompact == null
4400 ? 0
4401 : Math.min(mActionsToShowInCompact.length, MAX_MEDIA_BUTTONS_IN_COMPACT);
4402 if (N > 0) {
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02004403 view.removeAllViews(com.android.internal.R.id.media_actions);
Dan Sandler842dd772014-05-15 09:36:47 -04004404 for (int i = 0; i < N; i++) {
4405 if (i >= numActions) {
4406 throw new IllegalArgumentException(String.format(
4407 "setShowActionsInCompactView: action %d out of bounds (max %d)",
4408 i, numActions - 1));
4409 }
4410
4411 final Action action = mBuilder.mActions.get(mActionsToShowInCompact[i]);
Selim Cinek5bf069a2015-11-10 19:14:27 -05004412 final RemoteViews button = generateMediaActionButton(action,
4413 mBuilder.resolveColor());
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02004414 view.addView(com.android.internal.R.id.media_actions, button);
Dan Sandler842dd772014-05-15 09:36:47 -04004415 }
4416 }
Selim Cinekfdc738f2016-01-27 20:04:27 -08004417 handleImage(view);
4418 // handle the content margin
Selim Cinek247fa012016-02-18 09:50:48 -08004419 int endMargin = mBuilder.mContext.getResources().getDimensionPixelSize(
4420 R.dimen.notification_content_margin_end);;
Selim Cinekfdc738f2016-01-27 20:04:27 -08004421 if (mBuilder.mN.mLargeIcon != null) {
Selim Cinek247fa012016-02-18 09:50:48 -08004422 endMargin += mBuilder.mContext.getResources().getDimensionPixelSize(
4423 R.dimen.notification_content_picture_margin);
Selim Cinekfdc738f2016-01-27 20:04:27 -08004424 }
4425 view.setViewLayoutMarginEnd(R.id.notification_main_column, endMargin);
Dan Sandler842dd772014-05-15 09:36:47 -04004426 return view;
4427 }
4428
4429 private RemoteViews makeMediaBigContentView() {
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02004430 final int actionCount = Math.min(mBuilder.mActions.size(), MAX_MEDIA_BUTTONS);
Selim Cinekcc10bfb2016-02-10 16:24:21 -08004431 // Dont add an expanded view if there is no more content to be revealed
4432 int actionsInCompact = mActionsToShowInCompact == null
4433 ? 0
4434 : Math.min(mActionsToShowInCompact.length, MAX_MEDIA_BUTTONS_IN_COMPACT);
4435 if (mBuilder.mN.mLargeIcon == null && actionCount <= actionsInCompact) {
4436 return null;
4437 }
Selim Cinek5bf069a2015-11-10 19:14:27 -05004438 RemoteViews big = mBuilder.applyStandardTemplate(
4439 R.layout.notification_template_material_big_media,
4440 false);
Dan Sandler842dd772014-05-15 09:36:47 -04004441
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02004442 if (actionCount > 0) {
4443 big.removeAllViews(com.android.internal.R.id.media_actions);
4444 for (int i = 0; i < actionCount; i++) {
Selim Cinek5bf069a2015-11-10 19:14:27 -05004445 final RemoteViews button = generateMediaActionButton(mBuilder.mActions.get(i),
4446 mBuilder.resolveColor());
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02004447 big.addView(com.android.internal.R.id.media_actions, button);
Dan Sandler842dd772014-05-15 09:36:47 -04004448 }
4449 }
Selim Cinek5bf069a2015-11-10 19:14:27 -05004450 handleImage(big);
Dan Sandler842dd772014-05-15 09:36:47 -04004451 return big;
4452 }
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02004453
Selim Cinek5bf069a2015-11-10 19:14:27 -05004454 private void handleImage(RemoteViews contentView) {
4455 if (mBuilder.mN.mLargeIcon != null) {
4456 contentView.setViewLayoutMarginEnd(R.id.line1, 0);
Selim Cinekc848c3a2016-01-13 15:27:30 -08004457 contentView.setViewLayoutMarginEnd(R.id.text, 0);
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02004458 }
4459 }
4460
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02004461 /**
4462 * @hide
4463 */
4464 @Override
4465 protected boolean hasProgress() {
4466 return false;
4467 }
Dan Sandler842dd772014-05-15 09:36:47 -04004468 }
Griff Hazen61a9e862014-05-22 16:05:19 -07004469
Selim Cinek593610c2016-02-16 18:42:57 -08004470 /**
4471 * Notification style for custom views that are decorated by the system
4472 *
4473 * <p>Instead of providing a notification that is completely custom, a developer can set this
4474 * style and still obtain system decorations like the notification header with the expand
4475 * affordance and actions.
4476 *
4477 * <p>Use {@link android.app.Notification.Builder#setCustomContentView(RemoteViews)},
4478 * {@link android.app.Notification.Builder#setCustomBigContentView(RemoteViews)} and
4479 * {@link android.app.Notification.Builder#setCustomHeadsUpContentView(RemoteViews)} to set the
4480 * corresponding custom views to display.
4481 *
4482 * To use this style with your Notification, feed it to
4483 * {@link Notification.Builder#setStyle(android.app.Notification.Style)} like so:
4484 * <pre class="prettyprint">
4485 * Notification noti = new Notification.Builder()
4486 * .setSmallIcon(R.drawable.ic_stat_player)
4487 * .setLargeIcon(albumArtBitmap))
4488 * .setCustomContentView(contentView);
4489 * .setStyle(<b>new Notification.DecoratedCustomViewStyle()</b>)
4490 * .build();
4491 * </pre>
4492 */
4493 public static class DecoratedCustomViewStyle extends Style {
4494
4495 public DecoratedCustomViewStyle() {
4496 }
4497
4498 public DecoratedCustomViewStyle(Builder builder) {
4499 setBuilder(builder);
4500 }
4501
4502 /**
4503 * @hide
4504 */
4505 public boolean displayCustomViewInline() {
4506 return true;
4507 }
4508
4509 /**
4510 * @hide
4511 */
4512 @Override
4513 public RemoteViews makeContentView() {
4514 return makeStandardTemplateWithCustomContent(mBuilder.mN.contentView);
4515 }
4516
4517 /**
4518 * @hide
4519 */
4520 @Override
4521 public RemoteViews makeBigContentView() {
4522 return makeDecoratedBigContentView();
4523 }
4524
4525 /**
4526 * @hide
4527 */
4528 @Override
4529 public RemoteViews makeHeadsUpContentView() {
4530 return makeDecoratedHeadsUpContentView();
4531 }
4532
Selim Cinek593610c2016-02-16 18:42:57 -08004533 private RemoteViews makeDecoratedHeadsUpContentView() {
4534 RemoteViews headsUpContentView = mBuilder.mN.headsUpContentView == null
4535 ? mBuilder.mN.contentView
4536 : mBuilder.mN.headsUpContentView;
4537 if (mBuilder.mActions.size() == 0) {
4538 return makeStandardTemplateWithCustomContent(headsUpContentView);
4539 }
4540 RemoteViews remoteViews = mBuilder.applyStandardTemplateWithActions(
4541 mBuilder.getBigBaseLayoutResource());
Selim Cinek247fa012016-02-18 09:50:48 -08004542 buildIntoRemoteViewContent(remoteViews, headsUpContentView);
Selim Cinek593610c2016-02-16 18:42:57 -08004543 return remoteViews;
4544 }
4545
Selim Cinek593610c2016-02-16 18:42:57 -08004546 private RemoteViews makeStandardTemplateWithCustomContent(RemoteViews customContent) {
4547 RemoteViews remoteViews = mBuilder.applyStandardTemplate(
4548 mBuilder.getBaseLayoutResource());
Selim Cinek247fa012016-02-18 09:50:48 -08004549 buildIntoRemoteViewContent(remoteViews, customContent);
Selim Cinek593610c2016-02-16 18:42:57 -08004550 return remoteViews;
4551 }
4552
Selim Cinek593610c2016-02-16 18:42:57 -08004553 private RemoteViews makeDecoratedBigContentView() {
4554 RemoteViews bigContentView = mBuilder.mN.bigContentView == null
4555 ? mBuilder.mN.contentView
4556 : mBuilder.mN.bigContentView;
4557 if (mBuilder.mActions.size() == 0) {
4558 return makeStandardTemplateWithCustomContent(bigContentView);
4559 }
4560 RemoteViews remoteViews = mBuilder.applyStandardTemplateWithActions(
4561 mBuilder.getBigBaseLayoutResource());
Selim Cinek247fa012016-02-18 09:50:48 -08004562 buildIntoRemoteViewContent(remoteViews, bigContentView);
Selim Cinek593610c2016-02-16 18:42:57 -08004563 return remoteViews;
4564 }
Selim Cinek247fa012016-02-18 09:50:48 -08004565
4566 private void buildIntoRemoteViewContent(RemoteViews remoteViews,
4567 RemoteViews customContent) {
4568 remoteViews.removeAllViews(R.id.notification_main_column);
4569 remoteViews.addView(R.id.notification_main_column, customContent);
4570 // also update the end margin if there is an image
4571 int endMargin = mBuilder.mContext.getResources().getDimensionPixelSize(
4572 R.dimen.notification_content_margin_end);
4573 if (mBuilder.mN.mLargeIcon != null) {
4574 endMargin += mBuilder.mContext.getResources().getDimensionPixelSize(
4575 R.dimen.notification_content_picture_margin);
4576 }
4577 remoteViews.setViewLayoutMarginEnd(R.id.notification_main_column, endMargin);
4578 }
Selim Cinek593610c2016-02-16 18:42:57 -08004579 }
4580
Selim Cinek03eb3b72016-02-18 10:39:45 -08004581 /**
4582 * Notification style for media custom views that are decorated by the system
4583 *
4584 * <p>Instead of providing a media notification that is completely custom, a developer can set
4585 * this style and still obtain system decorations like the notification header with the expand
4586 * affordance and actions.
4587 *
4588 * <p>Use {@link android.app.Notification.Builder#setCustomContentView(RemoteViews)},
4589 * {@link android.app.Notification.Builder#setCustomBigContentView(RemoteViews)} and
4590 * {@link android.app.Notification.Builder#setCustomHeadsUpContentView(RemoteViews)} to set the
4591 * corresponding custom views to display.
4592 *
4593 * To use this style with your Notification, feed it to
4594 * {@link Notification.Builder#setStyle(android.app.Notification.Style)} like so:
4595 * <pre class="prettyprint">
4596 * Notification noti = new Notification.Builder()
4597 * .setSmallIcon(R.drawable.ic_stat_player)
4598 * .setLargeIcon(albumArtBitmap))
4599 * .setCustomContentView(contentView);
4600 * .setStyle(<b>new Notification.DecoratedMediaCustomViewStyle()</b>
4601 * .setMediaSession(mySession))
4602 * .build();
4603 * </pre>
4604 *
4605 * @see android.app.Notification.DecoratedCustomViewStyle
4606 * @see android.app.Notification.MediaStyle
4607 */
4608 public static class DecoratedMediaCustomViewStyle extends MediaStyle {
4609
4610 public DecoratedMediaCustomViewStyle() {
4611 }
4612
4613 public DecoratedMediaCustomViewStyle(Builder builder) {
4614 setBuilder(builder);
4615 }
4616
4617 /**
4618 * @hide
4619 */
4620 public boolean displayCustomViewInline() {
4621 return true;
4622 }
4623
4624 /**
4625 * @hide
4626 */
4627 @Override
4628 public RemoteViews makeContentView() {
4629 RemoteViews remoteViews = super.makeContentView();
4630 return buildIntoRemoteView(remoteViews, R.id.notification_content_container,
4631 mBuilder.mN.contentView);
4632 }
4633
4634 /**
4635 * @hide
4636 */
4637 @Override
4638 public RemoteViews makeBigContentView() {
4639 RemoteViews customRemoteView = mBuilder.mN.bigContentView != null
4640 ? mBuilder.mN.bigContentView
4641 : mBuilder.mN.contentView;
4642 return makeBigContentViewWithCustomContent(customRemoteView);
4643 }
4644
4645 private RemoteViews makeBigContentViewWithCustomContent(RemoteViews customRemoteView) {
4646 RemoteViews remoteViews = super.makeBigContentView();
4647 if (remoteViews != null) {
4648 return buildIntoRemoteView(remoteViews, R.id.notification_main_column,
4649 customRemoteView);
4650 } else if (customRemoteView != mBuilder.mN.contentView){
4651 remoteViews = super.makeContentView();
4652 return buildIntoRemoteView(remoteViews, R.id.notification_content_container,
4653 customRemoteView);
4654 } else {
4655 return null;
4656 }
4657 }
4658
4659 /**
4660 * @hide
4661 */
4662 @Override
4663 public RemoteViews makeHeadsUpContentView() {
4664 RemoteViews customRemoteView = mBuilder.mN.headsUpContentView != null
4665 ? mBuilder.mN.headsUpContentView
4666 : mBuilder.mN.contentView;
4667 return makeBigContentViewWithCustomContent(customRemoteView);
4668 }
4669
4670 private RemoteViews buildIntoRemoteView(RemoteViews remoteViews, int id,
4671 RemoteViews customContent) {
4672 remoteViews.removeAllViews(id);
4673 remoteViews.addView(id, customContent);
4674 return remoteViews;
4675 }
4676 }
4677
Christoph Studer4600f9b2014-07-22 22:44:43 +02004678 // When adding a new Style subclass here, don't forget to update
4679 // Builder.getNotificationStyleClass.
4680
Griff Hazen61a9e862014-05-22 16:05:19 -07004681 /**
4682 * Extender interface for use with {@link Builder#extend}. Extenders may be used to add
4683 * metadata or change options on a notification builder.
4684 */
4685 public interface Extender {
4686 /**
4687 * Apply this extender to a notification builder.
4688 * @param builder the builder to be modified.
4689 * @return the build object for chaining.
4690 */
4691 public Builder extend(Builder builder);
4692 }
4693
4694 /**
4695 * Helper class to add wearable extensions to notifications.
4696 * <p class="note"> See
4697 * <a href="{@docRoot}wear/notifications/creating.html">Creating Notifications
4698 * for Android Wear</a> for more information on how to use this class.
4699 * <p>
4700 * To create a notification with wearable extensions:
4701 * <ol>
4702 * <li>Create a {@link android.app.Notification.Builder}, setting any desired
4703 * properties.
4704 * <li>Create a {@link android.app.Notification.WearableExtender}.
4705 * <li>Set wearable-specific properties using the
4706 * {@code add} and {@code set} methods of {@link android.app.Notification.WearableExtender}.
4707 * <li>Call {@link android.app.Notification.Builder#extend} to apply the extensions to a
4708 * notification.
4709 * <li>Post the notification to the notification system with the
4710 * {@code NotificationManager.notify(...)} methods.
4711 * </ol>
4712 *
4713 * <pre class="prettyprint">
4714 * Notification notif = new Notification.Builder(mContext)
4715 * .setContentTitle(&quot;New mail from &quot; + sender.toString())
4716 * .setContentText(subject)
4717 * .setSmallIcon(R.drawable.new_mail)
4718 * .extend(new Notification.WearableExtender()
4719 * .setContentIcon(R.drawable.new_mail))
4720 * .build();
4721 * NotificationManager notificationManger =
4722 * (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
4723 * notificationManger.notify(0, notif);</pre>
4724 *
4725 * <p>Wearable extensions can be accessed on an existing notification by using the
4726 * {@code WearableExtender(Notification)} constructor,
4727 * and then using the {@code get} methods to access values.
4728 *
4729 * <pre class="prettyprint">
4730 * Notification.WearableExtender wearableExtender = new Notification.WearableExtender(
4731 * notification);
Griff Hazen14f57992014-05-26 09:07:14 -07004732 * List&lt;Notification&gt; pages = wearableExtender.getPages();</pre>
Griff Hazen61a9e862014-05-22 16:05:19 -07004733 */
4734 public static final class WearableExtender implements Extender {
4735 /**
4736 * Sentinel value for an action index that is unset.
4737 */
4738 public static final int UNSET_ACTION_INDEX = -1;
4739
4740 /**
4741 * Size value for use with {@link #setCustomSizePreset} to show this notification with
4742 * default sizing.
4743 * <p>For custom display notifications created using {@link #setDisplayIntent},
Paul Soulosaa4f4bf2015-08-04 11:59:45 -07004744 * the default is {@link #SIZE_MEDIUM}. All other notifications size automatically based
Griff Hazen61a9e862014-05-22 16:05:19 -07004745 * on their content.
4746 */
4747 public static final int SIZE_DEFAULT = 0;
4748
4749 /**
4750 * Size value for use with {@link #setCustomSizePreset} to show this notification
4751 * with an extra small size.
4752 * <p>This value is only applicable for custom display notifications created using
4753 * {@link #setDisplayIntent}.
4754 */
4755 public static final int SIZE_XSMALL = 1;
4756
4757 /**
4758 * Size value for use with {@link #setCustomSizePreset} to show this notification
4759 * with a small size.
4760 * <p>This value is only applicable for custom display notifications created using
4761 * {@link #setDisplayIntent}.
4762 */
4763 public static final int SIZE_SMALL = 2;
4764
4765 /**
4766 * Size value for use with {@link #setCustomSizePreset} to show this notification
4767 * with a medium size.
4768 * <p>This value is only applicable for custom display notifications created using
4769 * {@link #setDisplayIntent}.
4770 */
4771 public static final int SIZE_MEDIUM = 3;
4772
4773 /**
4774 * Size value for use with {@link #setCustomSizePreset} to show this notification
4775 * with a large size.
4776 * <p>This value is only applicable for custom display notifications created using
4777 * {@link #setDisplayIntent}.
4778 */
4779 public static final int SIZE_LARGE = 4;
4780
Griff Hazend5f11f92014-05-27 15:40:09 -07004781 /**
4782 * Size value for use with {@link #setCustomSizePreset} to show this notification
4783 * full screen.
4784 * <p>This value is only applicable for custom display notifications created using
4785 * {@link #setDisplayIntent}.
4786 */
4787 public static final int SIZE_FULL_SCREEN = 5;
4788
Griff Hazen5f2edfc2014-09-29 16:28:44 -07004789 /**
4790 * Sentinel value for use with {@link #setHintScreenTimeout} to keep the screen on for a
4791 * short amount of time when this notification is displayed on the screen. This
4792 * is the default value.
4793 */
4794 public static final int SCREEN_TIMEOUT_SHORT = 0;
4795
4796 /**
4797 * Sentinel value for use with {@link #setHintScreenTimeout} to keep the screen on
4798 * for a longer amount of time when this notification is displayed on the screen.
4799 */
4800 public static final int SCREEN_TIMEOUT_LONG = -1;
4801
Griff Hazen61a9e862014-05-22 16:05:19 -07004802 /** Notification extra which contains wearable extensions */
4803 private static final String EXTRA_WEARABLE_EXTENSIONS = "android.wearable.EXTENSIONS";
4804
Pete Gastaf6781d2014-10-07 15:17:05 -04004805 // Keys within EXTRA_WEARABLE_EXTENSIONS for wearable options.
Griff Hazen61a9e862014-05-22 16:05:19 -07004806 private static final String KEY_ACTIONS = "actions";
4807 private static final String KEY_FLAGS = "flags";
4808 private static final String KEY_DISPLAY_INTENT = "displayIntent";
4809 private static final String KEY_PAGES = "pages";
4810 private static final String KEY_BACKGROUND = "background";
4811 private static final String KEY_CONTENT_ICON = "contentIcon";
4812 private static final String KEY_CONTENT_ICON_GRAVITY = "contentIconGravity";
4813 private static final String KEY_CONTENT_ACTION_INDEX = "contentActionIndex";
4814 private static final String KEY_CUSTOM_SIZE_PRESET = "customSizePreset";
4815 private static final String KEY_CUSTOM_CONTENT_HEIGHT = "customContentHeight";
4816 private static final String KEY_GRAVITY = "gravity";
Griff Hazen5f2edfc2014-09-29 16:28:44 -07004817 private static final String KEY_HINT_SCREEN_TIMEOUT = "hintScreenTimeout";
Griff Hazen61a9e862014-05-22 16:05:19 -07004818
4819 // Flags bitwise-ored to mFlags
4820 private static final int FLAG_CONTENT_INTENT_AVAILABLE_OFFLINE = 0x1;
4821 private static final int FLAG_HINT_HIDE_ICON = 1 << 1;
4822 private static final int FLAG_HINT_SHOW_BACKGROUND_ONLY = 1 << 2;
4823 private static final int FLAG_START_SCROLL_BOTTOM = 1 << 3;
Griff Hazen5f2edfc2014-09-29 16:28:44 -07004824 private static final int FLAG_HINT_AVOID_BACKGROUND_CLIPPING = 1 << 4;
Griff Hazen61a9e862014-05-22 16:05:19 -07004825
4826 // Default value for flags integer
4827 private static final int DEFAULT_FLAGS = FLAG_CONTENT_INTENT_AVAILABLE_OFFLINE;
4828
4829 private static final int DEFAULT_CONTENT_ICON_GRAVITY = Gravity.END;
4830 private static final int DEFAULT_GRAVITY = Gravity.BOTTOM;
4831
4832 private ArrayList<Action> mActions = new ArrayList<Action>();
4833 private int mFlags = DEFAULT_FLAGS;
4834 private PendingIntent mDisplayIntent;
4835 private ArrayList<Notification> mPages = new ArrayList<Notification>();
4836 private Bitmap mBackground;
4837 private int mContentIcon;
4838 private int mContentIconGravity = DEFAULT_CONTENT_ICON_GRAVITY;
4839 private int mContentActionIndex = UNSET_ACTION_INDEX;
4840 private int mCustomSizePreset = SIZE_DEFAULT;
4841 private int mCustomContentHeight;
4842 private int mGravity = DEFAULT_GRAVITY;
Griff Hazen5f2edfc2014-09-29 16:28:44 -07004843 private int mHintScreenTimeout;
Griff Hazen61a9e862014-05-22 16:05:19 -07004844
4845 /**
4846 * Create a {@link android.app.Notification.WearableExtender} with default
4847 * options.
4848 */
4849 public WearableExtender() {
4850 }
4851
4852 public WearableExtender(Notification notif) {
4853 Bundle wearableBundle = notif.extras.getBundle(EXTRA_WEARABLE_EXTENSIONS);
4854 if (wearableBundle != null) {
4855 List<Action> actions = wearableBundle.getParcelableArrayList(KEY_ACTIONS);
4856 if (actions != null) {
4857 mActions.addAll(actions);
4858 }
4859
4860 mFlags = wearableBundle.getInt(KEY_FLAGS, DEFAULT_FLAGS);
4861 mDisplayIntent = wearableBundle.getParcelable(KEY_DISPLAY_INTENT);
4862
4863 Notification[] pages = getNotificationArrayFromBundle(
4864 wearableBundle, KEY_PAGES);
4865 if (pages != null) {
4866 Collections.addAll(mPages, pages);
4867 }
4868
4869 mBackground = wearableBundle.getParcelable(KEY_BACKGROUND);
4870 mContentIcon = wearableBundle.getInt(KEY_CONTENT_ICON);
4871 mContentIconGravity = wearableBundle.getInt(KEY_CONTENT_ICON_GRAVITY,
4872 DEFAULT_CONTENT_ICON_GRAVITY);
4873 mContentActionIndex = wearableBundle.getInt(KEY_CONTENT_ACTION_INDEX,
4874 UNSET_ACTION_INDEX);
4875 mCustomSizePreset = wearableBundle.getInt(KEY_CUSTOM_SIZE_PRESET,
4876 SIZE_DEFAULT);
4877 mCustomContentHeight = wearableBundle.getInt(KEY_CUSTOM_CONTENT_HEIGHT);
4878 mGravity = wearableBundle.getInt(KEY_GRAVITY, DEFAULT_GRAVITY);
Griff Hazen5f2edfc2014-09-29 16:28:44 -07004879 mHintScreenTimeout = wearableBundle.getInt(KEY_HINT_SCREEN_TIMEOUT);
Griff Hazen61a9e862014-05-22 16:05:19 -07004880 }
4881 }
4882
4883 /**
4884 * Apply wearable extensions to a notification that is being built. This is typically
4885 * called by the {@link android.app.Notification.Builder#extend} method of
4886 * {@link android.app.Notification.Builder}.
4887 */
4888 @Override
4889 public Notification.Builder extend(Notification.Builder builder) {
4890 Bundle wearableBundle = new Bundle();
4891
4892 if (!mActions.isEmpty()) {
4893 wearableBundle.putParcelableArrayList(KEY_ACTIONS, mActions);
4894 }
4895 if (mFlags != DEFAULT_FLAGS) {
4896 wearableBundle.putInt(KEY_FLAGS, mFlags);
4897 }
4898 if (mDisplayIntent != null) {
4899 wearableBundle.putParcelable(KEY_DISPLAY_INTENT, mDisplayIntent);
4900 }
4901 if (!mPages.isEmpty()) {
4902 wearableBundle.putParcelableArray(KEY_PAGES, mPages.toArray(
4903 new Notification[mPages.size()]));
4904 }
4905 if (mBackground != null) {
4906 wearableBundle.putParcelable(KEY_BACKGROUND, mBackground);
4907 }
4908 if (mContentIcon != 0) {
4909 wearableBundle.putInt(KEY_CONTENT_ICON, mContentIcon);
4910 }
4911 if (mContentIconGravity != DEFAULT_CONTENT_ICON_GRAVITY) {
4912 wearableBundle.putInt(KEY_CONTENT_ICON_GRAVITY, mContentIconGravity);
4913 }
4914 if (mContentActionIndex != UNSET_ACTION_INDEX) {
4915 wearableBundle.putInt(KEY_CONTENT_ACTION_INDEX,
4916 mContentActionIndex);
4917 }
4918 if (mCustomSizePreset != SIZE_DEFAULT) {
4919 wearableBundle.putInt(KEY_CUSTOM_SIZE_PRESET, mCustomSizePreset);
4920 }
4921 if (mCustomContentHeight != 0) {
4922 wearableBundle.putInt(KEY_CUSTOM_CONTENT_HEIGHT, mCustomContentHeight);
4923 }
4924 if (mGravity != DEFAULT_GRAVITY) {
4925 wearableBundle.putInt(KEY_GRAVITY, mGravity);
4926 }
Griff Hazen5f2edfc2014-09-29 16:28:44 -07004927 if (mHintScreenTimeout != 0) {
4928 wearableBundle.putInt(KEY_HINT_SCREEN_TIMEOUT, mHintScreenTimeout);
4929 }
Griff Hazen61a9e862014-05-22 16:05:19 -07004930
4931 builder.getExtras().putBundle(EXTRA_WEARABLE_EXTENSIONS, wearableBundle);
4932 return builder;
4933 }
4934
4935 @Override
4936 public WearableExtender clone() {
4937 WearableExtender that = new WearableExtender();
4938 that.mActions = new ArrayList<Action>(this.mActions);
4939 that.mFlags = this.mFlags;
4940 that.mDisplayIntent = this.mDisplayIntent;
4941 that.mPages = new ArrayList<Notification>(this.mPages);
4942 that.mBackground = this.mBackground;
4943 that.mContentIcon = this.mContentIcon;
4944 that.mContentIconGravity = this.mContentIconGravity;
4945 that.mContentActionIndex = this.mContentActionIndex;
4946 that.mCustomSizePreset = this.mCustomSizePreset;
4947 that.mCustomContentHeight = this.mCustomContentHeight;
4948 that.mGravity = this.mGravity;
Griff Hazen5f2edfc2014-09-29 16:28:44 -07004949 that.mHintScreenTimeout = this.mHintScreenTimeout;
Griff Hazen61a9e862014-05-22 16:05:19 -07004950 return that;
4951 }
4952
4953 /**
4954 * Add a wearable action to this notification.
4955 *
4956 * <p>When wearable actions are added using this method, the set of actions that
4957 * show on a wearable device splits from devices that only show actions added
4958 * using {@link android.app.Notification.Builder#addAction}. This allows for customization
4959 * of which actions display on different devices.
4960 *
4961 * @param action the action to add to this notification
4962 * @return this object for method chaining
4963 * @see android.app.Notification.Action
4964 */
4965 public WearableExtender addAction(Action action) {
4966 mActions.add(action);
4967 return this;
4968 }
4969
4970 /**
4971 * Adds wearable actions to this notification.
4972 *
4973 * <p>When wearable actions are added using this method, the set of actions that
4974 * show on a wearable device splits from devices that only show actions added
4975 * using {@link android.app.Notification.Builder#addAction}. This allows for customization
4976 * of which actions display on different devices.
4977 *
4978 * @param actions the actions to add to this notification
4979 * @return this object for method chaining
4980 * @see android.app.Notification.Action
4981 */
4982 public WearableExtender addActions(List<Action> actions) {
4983 mActions.addAll(actions);
4984 return this;
4985 }
4986
4987 /**
4988 * Clear all wearable actions present on this builder.
4989 * @return this object for method chaining.
4990 * @see #addAction
4991 */
4992 public WearableExtender clearActions() {
4993 mActions.clear();
4994 return this;
4995 }
4996
4997 /**
4998 * Get the wearable actions present on this notification.
4999 */
5000 public List<Action> getActions() {
5001 return mActions;
5002 }
5003
5004 /**
5005 * Set an intent to launch inside of an activity view when displaying
Griff Hazen14f57992014-05-26 09:07:14 -07005006 * this notification. The {@link PendingIntent} provided should be for an activity.
5007 *
5008 * <pre class="prettyprint">
5009 * Intent displayIntent = new Intent(context, MyDisplayActivity.class);
5010 * PendingIntent displayPendingIntent = PendingIntent.getActivity(context,
5011 * 0, displayIntent, PendingIntent.FLAG_UPDATE_CURRENT);
5012 * Notification notif = new Notification.Builder(context)
5013 * .extend(new Notification.WearableExtender()
5014 * .setDisplayIntent(displayPendingIntent)
5015 * .setCustomSizePreset(Notification.WearableExtender.SIZE_MEDIUM))
5016 * .build();</pre>
5017 *
5018 * <p>The activity to launch needs to allow embedding, must be exported, and
Griff Hazen831ca9d2014-06-17 00:38:38 -07005019 * should have an empty task affinity. It is also recommended to use the device
5020 * default light theme.
Griff Hazen14f57992014-05-26 09:07:14 -07005021 *
5022 * <p>Example AndroidManifest.xml entry:
5023 * <pre class="prettyprint">
5024 * &lt;activity android:name=&quot;com.example.MyDisplayActivity&quot;
5025 * android:exported=&quot;true&quot;
5026 * android:allowEmbedded=&quot;true&quot;
Griff Hazen831ca9d2014-06-17 00:38:38 -07005027 * android:taskAffinity=&quot;&quot;
5028 * android:theme=&quot;@android:style/Theme.DeviceDefault.Light&quot; /&gt;</pre>
Griff Hazen61a9e862014-05-22 16:05:19 -07005029 *
5030 * @param intent the {@link PendingIntent} for an activity
5031 * @return this object for method chaining
5032 * @see android.app.Notification.WearableExtender#getDisplayIntent
5033 */
5034 public WearableExtender setDisplayIntent(PendingIntent intent) {
5035 mDisplayIntent = intent;
5036 return this;
5037 }
5038
5039 /**
5040 * Get the intent to launch inside of an activity view when displaying this
5041 * notification. This {@code PendingIntent} should be for an activity.
5042 */
5043 public PendingIntent getDisplayIntent() {
5044 return mDisplayIntent;
5045 }
5046
5047 /**
5048 * Add an additional page of content to display with this notification. The current
5049 * notification forms the first page, and pages added using this function form
5050 * subsequent pages. This field can be used to separate a notification into multiple
5051 * sections.
5052 *
5053 * @param page the notification to add as another page
5054 * @return this object for method chaining
5055 * @see android.app.Notification.WearableExtender#getPages
5056 */
5057 public WearableExtender addPage(Notification page) {
5058 mPages.add(page);
5059 return this;
5060 }
5061
5062 /**
5063 * Add additional pages of content to display with this notification. The current
5064 * notification forms the first page, and pages added using this function form
5065 * subsequent pages. This field can be used to separate a notification into multiple
5066 * sections.
5067 *
5068 * @param pages a list of notifications
5069 * @return this object for method chaining
5070 * @see android.app.Notification.WearableExtender#getPages
5071 */
5072 public WearableExtender addPages(List<Notification> pages) {
5073 mPages.addAll(pages);
5074 return this;
5075 }
5076
5077 /**
5078 * Clear all additional pages present on this builder.
5079 * @return this object for method chaining.
5080 * @see #addPage
5081 */
5082 public WearableExtender clearPages() {
5083 mPages.clear();
5084 return this;
5085 }
5086
5087 /**
5088 * Get the array of additional pages of content for displaying this notification. The
5089 * current notification forms the first page, and elements within this array form
5090 * subsequent pages. This field can be used to separate a notification into multiple
5091 * sections.
5092 * @return the pages for this notification
5093 */
5094 public List<Notification> getPages() {
5095 return mPages;
5096 }
5097
5098 /**
5099 * Set a background image to be displayed behind the notification content.
5100 * Contrary to the {@link android.app.Notification.BigPictureStyle}, this background
5101 * will work with any notification style.
5102 *
5103 * @param background the background bitmap
5104 * @return this object for method chaining
5105 * @see android.app.Notification.WearableExtender#getBackground
5106 */
5107 public WearableExtender setBackground(Bitmap background) {
5108 mBackground = background;
5109 return this;
5110 }
5111
5112 /**
5113 * Get a background image to be displayed behind the notification content.
5114 * Contrary to the {@link android.app.Notification.BigPictureStyle}, this background
5115 * will work with any notification style.
5116 *
5117 * @return the background image
5118 * @see android.app.Notification.WearableExtender#setBackground
5119 */
5120 public Bitmap getBackground() {
5121 return mBackground;
5122 }
5123
5124 /**
5125 * Set an icon that goes with the content of this notification.
5126 */
5127 public WearableExtender setContentIcon(int icon) {
5128 mContentIcon = icon;
5129 return this;
5130 }
5131
5132 /**
5133 * Get an icon that goes with the content of this notification.
5134 */
5135 public int getContentIcon() {
5136 return mContentIcon;
5137 }
5138
5139 /**
5140 * Set the gravity that the content icon should have within the notification display.
5141 * Supported values include {@link android.view.Gravity#START} and
5142 * {@link android.view.Gravity#END}. The default value is {@link android.view.Gravity#END}.
5143 * @see #setContentIcon
5144 */
5145 public WearableExtender setContentIconGravity(int contentIconGravity) {
5146 mContentIconGravity = contentIconGravity;
5147 return this;
5148 }
5149
5150 /**
5151 * Get the gravity that the content icon should have within the notification display.
5152 * Supported values include {@link android.view.Gravity#START} and
5153 * {@link android.view.Gravity#END}. The default value is {@link android.view.Gravity#END}.
5154 * @see #getContentIcon
5155 */
5156 public int getContentIconGravity() {
5157 return mContentIconGravity;
5158 }
5159
5160 /**
5161 * Set an action from this notification's actions to be clickable with the content of
Griff Hazen14f57992014-05-26 09:07:14 -07005162 * this notification. This action will no longer display separately from the
5163 * notification's content.
5164 *
Griff Hazenca48d352014-05-28 22:37:13 -07005165 * <p>For notifications with multiple pages, child pages can also have content actions
Griff Hazen14f57992014-05-26 09:07:14 -07005166 * set, although the list of available actions comes from the main notification and not
5167 * from the child page's notification.
5168 *
5169 * @param actionIndex The index of the action to hoist onto the current notification page.
5170 * If wearable actions were added to the main notification, this index
5171 * will apply to that list, otherwise it will apply to the regular
5172 * actions list.
Griff Hazen61a9e862014-05-22 16:05:19 -07005173 */
5174 public WearableExtender setContentAction(int actionIndex) {
5175 mContentActionIndex = actionIndex;
5176 return this;
5177 }
5178
5179 /**
Griff Hazenca48d352014-05-28 22:37:13 -07005180 * Get the index of the notification action, if any, that was specified as being clickable
5181 * with the content of this notification. This action will no longer display separately
Griff Hazen14f57992014-05-26 09:07:14 -07005182 * from the notification's content.
Griff Hazen61a9e862014-05-22 16:05:19 -07005183 *
Griff Hazenca48d352014-05-28 22:37:13 -07005184 * <p>For notifications with multiple pages, child pages can also have content actions
Griff Hazen14f57992014-05-26 09:07:14 -07005185 * set, although the list of available actions comes from the main notification and not
5186 * from the child page's notification.
5187 *
5188 * <p>If wearable specific actions were added to the main notification, this index will
5189 * apply to that list, otherwise it will apply to the regular actions list.
Griff Hazenca48d352014-05-28 22:37:13 -07005190 *
5191 * @return the action index or {@link #UNSET_ACTION_INDEX} if no action was selected.
Griff Hazen61a9e862014-05-22 16:05:19 -07005192 */
5193 public int getContentAction() {
5194 return mContentActionIndex;
5195 }
5196
5197 /**
5198 * Set the gravity that this notification should have within the available viewport space.
5199 * Supported values include {@link android.view.Gravity#TOP},
5200 * {@link android.view.Gravity#CENTER_VERTICAL} and {@link android.view.Gravity#BOTTOM}.
5201 * The default value is {@link android.view.Gravity#BOTTOM}.
5202 */
5203 public WearableExtender setGravity(int gravity) {
5204 mGravity = gravity;
5205 return this;
5206 }
5207
5208 /**
5209 * Get the gravity that this notification should have within the available viewport space.
5210 * Supported values include {@link android.view.Gravity#TOP},
5211 * {@link android.view.Gravity#CENTER_VERTICAL} and {@link android.view.Gravity#BOTTOM}.
5212 * The default value is {@link android.view.Gravity#BOTTOM}.
5213 */
5214 public int getGravity() {
5215 return mGravity;
5216 }
5217
5218 /**
5219 * Set the custom size preset for the display of this notification out of the available
5220 * presets found in {@link android.app.Notification.WearableExtender}, e.g.
5221 * {@link #SIZE_LARGE}.
5222 * <p>Some custom size presets are only applicable for custom display notifications created
5223 * using {@link android.app.Notification.WearableExtender#setDisplayIntent}. Check the
5224 * documentation for the preset in question. See also
5225 * {@link #setCustomContentHeight} and {@link #getCustomSizePreset}.
5226 */
5227 public WearableExtender setCustomSizePreset(int sizePreset) {
5228 mCustomSizePreset = sizePreset;
5229 return this;
5230 }
5231
5232 /**
5233 * Get the custom size preset for the display of this notification out of the available
5234 * presets found in {@link android.app.Notification.WearableExtender}, e.g.
5235 * {@link #SIZE_LARGE}.
5236 * <p>Some custom size presets are only applicable for custom display notifications created
5237 * using {@link #setDisplayIntent}. Check the documentation for the preset in question.
5238 * See also {@link #setCustomContentHeight} and {@link #setCustomSizePreset}.
5239 */
5240 public int getCustomSizePreset() {
5241 return mCustomSizePreset;
5242 }
5243
5244 /**
5245 * Set the custom height in pixels for the display of this notification's content.
5246 * <p>This option is only available for custom display notifications created
5247 * using {@link android.app.Notification.WearableExtender#setDisplayIntent}. See also
5248 * {@link android.app.Notification.WearableExtender#setCustomSizePreset} and
5249 * {@link #getCustomContentHeight}.
5250 */
5251 public WearableExtender setCustomContentHeight(int height) {
5252 mCustomContentHeight = height;
5253 return this;
5254 }
5255
5256 /**
5257 * Get the custom height in pixels for the display of this notification's content.
5258 * <p>This option is only available for custom display notifications created
5259 * using {@link #setDisplayIntent}. See also {@link #setCustomSizePreset} and
5260 * {@link #setCustomContentHeight}.
5261 */
5262 public int getCustomContentHeight() {
5263 return mCustomContentHeight;
5264 }
5265
5266 /**
5267 * Set whether the scrolling position for the contents of this notification should start
5268 * at the bottom of the contents instead of the top when the contents are too long to
5269 * display within the screen. Default is false (start scroll at the top).
5270 */
5271 public WearableExtender setStartScrollBottom(boolean startScrollBottom) {
5272 setFlag(FLAG_START_SCROLL_BOTTOM, startScrollBottom);
5273 return this;
5274 }
5275
5276 /**
5277 * Get whether the scrolling position for the contents of this notification should start
5278 * at the bottom of the contents instead of the top when the contents are too long to
5279 * display within the screen. Default is false (start scroll at the top).
5280 */
5281 public boolean getStartScrollBottom() {
5282 return (mFlags & FLAG_START_SCROLL_BOTTOM) != 0;
5283 }
5284
5285 /**
5286 * Set whether the content intent is available when the wearable device is not connected
5287 * to a companion device. The user can still trigger this intent when the wearable device
5288 * is offline, but a visual hint will indicate that the content intent may not be available.
5289 * Defaults to true.
5290 */
5291 public WearableExtender setContentIntentAvailableOffline(
5292 boolean contentIntentAvailableOffline) {
5293 setFlag(FLAG_CONTENT_INTENT_AVAILABLE_OFFLINE, contentIntentAvailableOffline);
5294 return this;
5295 }
5296
5297 /**
5298 * Get whether the content intent is available when the wearable device is not connected
5299 * to a companion device. The user can still trigger this intent when the wearable device
5300 * is offline, but a visual hint will indicate that the content intent may not be available.
5301 * Defaults to true.
5302 */
5303 public boolean getContentIntentAvailableOffline() {
5304 return (mFlags & FLAG_CONTENT_INTENT_AVAILABLE_OFFLINE) != 0;
5305 }
5306
5307 /**
5308 * Set a hint that this notification's icon should not be displayed.
5309 * @param hintHideIcon {@code true} to hide the icon, {@code false} otherwise.
5310 * @return this object for method chaining
5311 */
5312 public WearableExtender setHintHideIcon(boolean hintHideIcon) {
5313 setFlag(FLAG_HINT_HIDE_ICON, hintHideIcon);
5314 return this;
5315 }
5316
5317 /**
5318 * Get a hint that this notification's icon should not be displayed.
5319 * @return {@code true} if this icon should not be displayed, false otherwise.
5320 * The default value is {@code false} if this was never set.
5321 */
5322 public boolean getHintHideIcon() {
5323 return (mFlags & FLAG_HINT_HIDE_ICON) != 0;
5324 }
5325
5326 /**
5327 * Set a visual hint that only the background image of this notification should be
5328 * displayed, and other semantic content should be hidden. This hint is only applicable
5329 * to sub-pages added using {@link #addPage}.
5330 */
5331 public WearableExtender setHintShowBackgroundOnly(boolean hintShowBackgroundOnly) {
5332 setFlag(FLAG_HINT_SHOW_BACKGROUND_ONLY, hintShowBackgroundOnly);
5333 return this;
5334 }
5335
5336 /**
5337 * Get a visual hint that only the background image of this notification should be
5338 * displayed, and other semantic content should be hidden. This hint is only applicable
5339 * to sub-pages added using {@link android.app.Notification.WearableExtender#addPage}.
5340 */
5341 public boolean getHintShowBackgroundOnly() {
5342 return (mFlags & FLAG_HINT_SHOW_BACKGROUND_ONLY) != 0;
5343 }
5344
Griff Hazen5f2edfc2014-09-29 16:28:44 -07005345 /**
Griff Hazen9c5be4e2014-11-17 17:47:50 -08005346 * Set a hint that this notification's background should not be clipped if possible,
5347 * and should instead be resized to fully display on the screen, retaining the aspect
5348 * ratio of the image. This can be useful for images like barcodes or qr codes.
Griff Hazen5f2edfc2014-09-29 16:28:44 -07005349 * @param hintAvoidBackgroundClipping {@code true} to avoid clipping if possible.
5350 * @return this object for method chaining
5351 */
5352 public WearableExtender setHintAvoidBackgroundClipping(
5353 boolean hintAvoidBackgroundClipping) {
5354 setFlag(FLAG_HINT_AVOID_BACKGROUND_CLIPPING, hintAvoidBackgroundClipping);
5355 return this;
5356 }
5357
5358 /**
Griff Hazen9c5be4e2014-11-17 17:47:50 -08005359 * Get a hint that this notification's background should not be clipped if possible,
5360 * and should instead be resized to fully display on the screen, retaining the aspect
5361 * ratio of the image. This can be useful for images like barcodes or qr codes.
Griff Hazen5f2edfc2014-09-29 16:28:44 -07005362 * @return {@code true} if it's ok if the background is clipped on the screen, false
5363 * otherwise. The default value is {@code false} if this was never set.
5364 */
5365 public boolean getHintAvoidBackgroundClipping() {
5366 return (mFlags & FLAG_HINT_AVOID_BACKGROUND_CLIPPING) != 0;
5367 }
5368
5369 /**
5370 * Set a hint that the screen should remain on for at least this duration when
5371 * this notification is displayed on the screen.
5372 * @param timeout The requested screen timeout in milliseconds. Can also be either
5373 * {@link #SCREEN_TIMEOUT_SHORT} or {@link #SCREEN_TIMEOUT_LONG}.
5374 * @return this object for method chaining
5375 */
5376 public WearableExtender setHintScreenTimeout(int timeout) {
5377 mHintScreenTimeout = timeout;
5378 return this;
5379 }
5380
5381 /**
5382 * Get the duration, in milliseconds, that the screen should remain on for
5383 * when this notification is displayed.
5384 * @return the duration in milliseconds if > 0, or either one of the sentinel values
5385 * {@link #SCREEN_TIMEOUT_SHORT} or {@link #SCREEN_TIMEOUT_LONG}.
5386 */
5387 public int getHintScreenTimeout() {
5388 return mHintScreenTimeout;
5389 }
5390
Griff Hazen61a9e862014-05-22 16:05:19 -07005391 private void setFlag(int mask, boolean value) {
5392 if (value) {
5393 mFlags |= mask;
5394 } else {
5395 mFlags &= ~mask;
5396 }
5397 }
5398 }
5399
5400 /**
Zhen Yu Song9d5528b2014-11-14 15:34:39 -08005401 * <p>Helper class to add Android Auto extensions to notifications. To create a notification
5402 * with car extensions:
5403 *
5404 * <ol>
5405 * <li>Create an {@link Notification.Builder}, setting any desired
5406 * properties.
5407 * <li>Create a {@link CarExtender}.
5408 * <li>Set car-specific properties using the {@code add} and {@code set} methods of
5409 * {@link CarExtender}.
5410 * <li>Call {@link Notification.Builder#extend(Notification.Extender)}
5411 * to apply the extensions to a notification.
5412 * </ol>
5413 *
5414 * <pre class="prettyprint">
5415 * Notification notification = new Notification.Builder(context)
5416 * ...
5417 * .extend(new CarExtender()
5418 * .set*(...))
5419 * .build();
5420 * </pre>
5421 *
5422 * <p>Car extensions can be accessed on an existing notification by using the
5423 * {@code CarExtender(Notification)} constructor, and then using the {@code get} methods
5424 * to access values.
5425 */
5426 public static final class CarExtender implements Extender {
5427 private static final String TAG = "CarExtender";
5428
5429 private static final String EXTRA_CAR_EXTENDER = "android.car.EXTENSIONS";
5430 private static final String EXTRA_LARGE_ICON = "large_icon";
5431 private static final String EXTRA_CONVERSATION = "car_conversation";
5432 private static final String EXTRA_COLOR = "app_color";
5433
5434 private Bitmap mLargeIcon;
5435 private UnreadConversation mUnreadConversation;
5436 private int mColor = Notification.COLOR_DEFAULT;
5437
5438 /**
5439 * Create a {@link CarExtender} with default options.
5440 */
5441 public CarExtender() {
5442 }
5443
5444 /**
5445 * Create a {@link CarExtender} from the CarExtender options of an existing Notification.
5446 *
5447 * @param notif The notification from which to copy options.
5448 */
5449 public CarExtender(Notification notif) {
5450 Bundle carBundle = notif.extras == null ?
5451 null : notif.extras.getBundle(EXTRA_CAR_EXTENDER);
5452 if (carBundle != null) {
5453 mLargeIcon = carBundle.getParcelable(EXTRA_LARGE_ICON);
5454 mColor = carBundle.getInt(EXTRA_COLOR, Notification.COLOR_DEFAULT);
5455
5456 Bundle b = carBundle.getBundle(EXTRA_CONVERSATION);
5457 mUnreadConversation = UnreadConversation.getUnreadConversationFromBundle(b);
5458 }
5459 }
5460
5461 /**
5462 * Apply car extensions to a notification that is being built. This is typically called by
5463 * the {@link Notification.Builder#extend(Notification.Extender)}
5464 * method of {@link Notification.Builder}.
5465 */
5466 @Override
5467 public Notification.Builder extend(Notification.Builder builder) {
5468 Bundle carExtensions = new Bundle();
5469
5470 if (mLargeIcon != null) {
5471 carExtensions.putParcelable(EXTRA_LARGE_ICON, mLargeIcon);
5472 }
5473 if (mColor != Notification.COLOR_DEFAULT) {
5474 carExtensions.putInt(EXTRA_COLOR, mColor);
5475 }
5476
5477 if (mUnreadConversation != null) {
5478 Bundle b = mUnreadConversation.getBundleForUnreadConversation();
5479 carExtensions.putBundle(EXTRA_CONVERSATION, b);
5480 }
5481
5482 builder.getExtras().putBundle(EXTRA_CAR_EXTENDER, carExtensions);
5483 return builder;
5484 }
5485
5486 /**
5487 * Sets the accent color to use when Android Auto presents the notification.
5488 *
5489 * Android Auto uses the color set with {@link Notification.Builder#setColor(int)}
5490 * to accent the displayed notification. However, not all colors are acceptable in an
5491 * automotive setting. This method can be used to override the color provided in the
5492 * notification in such a situation.
5493 */
Tor Norbye80756e32015-03-02 09:39:27 -08005494 public CarExtender setColor(@ColorInt int color) {
Zhen Yu Song9d5528b2014-11-14 15:34:39 -08005495 mColor = color;
5496 return this;
5497 }
5498
5499 /**
5500 * Gets the accent color.
5501 *
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005502 * @see #setColor
Zhen Yu Song9d5528b2014-11-14 15:34:39 -08005503 */
Tor Norbye80756e32015-03-02 09:39:27 -08005504 @ColorInt
Zhen Yu Song9d5528b2014-11-14 15:34:39 -08005505 public int getColor() {
5506 return mColor;
5507 }
5508
5509 /**
5510 * Sets the large icon of the car notification.
5511 *
5512 * If no large icon is set in the extender, Android Auto will display the icon
5513 * specified by {@link Notification.Builder#setLargeIcon(android.graphics.Bitmap)}
5514 *
5515 * @param largeIcon The large icon to use in the car notification.
5516 * @return This object for method chaining.
5517 */
5518 public CarExtender setLargeIcon(Bitmap largeIcon) {
5519 mLargeIcon = largeIcon;
5520 return this;
5521 }
5522
5523 /**
5524 * Gets the large icon used in this car notification, or null if no icon has been set.
5525 *
5526 * @return The large icon for the car notification.
5527 * @see CarExtender#setLargeIcon
5528 */
5529 public Bitmap getLargeIcon() {
5530 return mLargeIcon;
5531 }
5532
5533 /**
5534 * Sets the unread conversation in a message notification.
5535 *
5536 * @param unreadConversation The unread part of the conversation this notification conveys.
5537 * @return This object for method chaining.
5538 */
5539 public CarExtender setUnreadConversation(UnreadConversation unreadConversation) {
5540 mUnreadConversation = unreadConversation;
5541 return this;
5542 }
5543
5544 /**
5545 * Returns the unread conversation conveyed by this notification.
5546 * @see #setUnreadConversation(UnreadConversation)
5547 */
5548 public UnreadConversation getUnreadConversation() {
5549 return mUnreadConversation;
5550 }
5551
5552 /**
5553 * A class which holds the unread messages from a conversation.
5554 */
5555 public static class UnreadConversation {
5556 private static final String KEY_AUTHOR = "author";
5557 private static final String KEY_TEXT = "text";
5558 private static final String KEY_MESSAGES = "messages";
5559 private static final String KEY_REMOTE_INPUT = "remote_input";
5560 private static final String KEY_ON_REPLY = "on_reply";
5561 private static final String KEY_ON_READ = "on_read";
5562 private static final String KEY_PARTICIPANTS = "participants";
5563 private static final String KEY_TIMESTAMP = "timestamp";
5564
5565 private final String[] mMessages;
5566 private final RemoteInput mRemoteInput;
5567 private final PendingIntent mReplyPendingIntent;
5568 private final PendingIntent mReadPendingIntent;
5569 private final String[] mParticipants;
5570 private final long mLatestTimestamp;
5571
5572 UnreadConversation(String[] messages, RemoteInput remoteInput,
5573 PendingIntent replyPendingIntent, PendingIntent readPendingIntent,
5574 String[] participants, long latestTimestamp) {
5575 mMessages = messages;
5576 mRemoteInput = remoteInput;
5577 mReadPendingIntent = readPendingIntent;
5578 mReplyPendingIntent = replyPendingIntent;
5579 mParticipants = participants;
5580 mLatestTimestamp = latestTimestamp;
5581 }
5582
5583 /**
5584 * Gets the list of messages conveyed by this notification.
5585 */
5586 public String[] getMessages() {
5587 return mMessages;
5588 }
5589
5590 /**
5591 * Gets the remote input that will be used to convey the response to a message list, or
5592 * null if no such remote input exists.
5593 */
5594 public RemoteInput getRemoteInput() {
5595 return mRemoteInput;
5596 }
5597
5598 /**
5599 * Gets the pending intent that will be triggered when the user replies to this
5600 * notification.
5601 */
5602 public PendingIntent getReplyPendingIntent() {
5603 return mReplyPendingIntent;
5604 }
5605
5606 /**
5607 * Gets the pending intent that Android Auto will send after it reads aloud all messages
5608 * in this object's message list.
5609 */
5610 public PendingIntent getReadPendingIntent() {
5611 return mReadPendingIntent;
5612 }
5613
5614 /**
5615 * Gets the participants in the conversation.
5616 */
5617 public String[] getParticipants() {
5618 return mParticipants;
5619 }
5620
5621 /**
5622 * Gets the firs participant in the conversation.
5623 */
5624 public String getParticipant() {
5625 return mParticipants.length > 0 ? mParticipants[0] : null;
5626 }
5627
5628 /**
5629 * Gets the timestamp of the conversation.
5630 */
5631 public long getLatestTimestamp() {
5632 return mLatestTimestamp;
5633 }
5634
5635 Bundle getBundleForUnreadConversation() {
5636 Bundle b = new Bundle();
5637 String author = null;
5638 if (mParticipants != null && mParticipants.length > 1) {
5639 author = mParticipants[0];
5640 }
5641 Parcelable[] messages = new Parcelable[mMessages.length];
5642 for (int i = 0; i < messages.length; i++) {
5643 Bundle m = new Bundle();
5644 m.putString(KEY_TEXT, mMessages[i]);
5645 m.putString(KEY_AUTHOR, author);
5646 messages[i] = m;
5647 }
5648 b.putParcelableArray(KEY_MESSAGES, messages);
5649 if (mRemoteInput != null) {
5650 b.putParcelable(KEY_REMOTE_INPUT, mRemoteInput);
5651 }
5652 b.putParcelable(KEY_ON_REPLY, mReplyPendingIntent);
5653 b.putParcelable(KEY_ON_READ, mReadPendingIntent);
5654 b.putStringArray(KEY_PARTICIPANTS, mParticipants);
5655 b.putLong(KEY_TIMESTAMP, mLatestTimestamp);
5656 return b;
5657 }
5658
5659 static UnreadConversation getUnreadConversationFromBundle(Bundle b) {
5660 if (b == null) {
5661 return null;
5662 }
5663 Parcelable[] parcelableMessages = b.getParcelableArray(KEY_MESSAGES);
5664 String[] messages = null;
5665 if (parcelableMessages != null) {
5666 String[] tmp = new String[parcelableMessages.length];
5667 boolean success = true;
5668 for (int i = 0; i < tmp.length; i++) {
5669 if (!(parcelableMessages[i] instanceof Bundle)) {
5670 success = false;
5671 break;
5672 }
5673 tmp[i] = ((Bundle) parcelableMessages[i]).getString(KEY_TEXT);
5674 if (tmp[i] == null) {
5675 success = false;
5676 break;
5677 }
5678 }
5679 if (success) {
5680 messages = tmp;
5681 } else {
5682 return null;
5683 }
5684 }
5685
5686 PendingIntent onRead = b.getParcelable(KEY_ON_READ);
5687 PendingIntent onReply = b.getParcelable(KEY_ON_REPLY);
5688
5689 RemoteInput remoteInput = b.getParcelable(KEY_REMOTE_INPUT);
5690
5691 String[] participants = b.getStringArray(KEY_PARTICIPANTS);
5692 if (participants == null || participants.length != 1) {
5693 return null;
5694 }
5695
5696 return new UnreadConversation(messages,
5697 remoteInput,
5698 onReply,
5699 onRead,
5700 participants, b.getLong(KEY_TIMESTAMP));
5701 }
5702 };
5703
5704 /**
5705 * Builder class for {@link CarExtender.UnreadConversation} objects.
5706 */
5707 public static class Builder {
5708 private final List<String> mMessages = new ArrayList<String>();
5709 private final String mParticipant;
5710 private RemoteInput mRemoteInput;
5711 private PendingIntent mReadPendingIntent;
5712 private PendingIntent mReplyPendingIntent;
5713 private long mLatestTimestamp;
5714
5715 /**
5716 * Constructs a new builder for {@link CarExtender.UnreadConversation}.
5717 *
5718 * @param name The name of the other participant in the conversation.
5719 */
5720 public Builder(String name) {
5721 mParticipant = name;
5722 }
5723
5724 /**
5725 * Appends a new unread message to the list of messages for this conversation.
5726 *
5727 * The messages should be added from oldest to newest.
5728 *
5729 * @param message The text of the new unread message.
5730 * @return This object for method chaining.
5731 */
5732 public Builder addMessage(String message) {
5733 mMessages.add(message);
5734 return this;
5735 }
5736
5737 /**
5738 * Sets the pending intent and remote input which will convey the reply to this
5739 * notification.
5740 *
5741 * @param pendingIntent The pending intent which will be triggered on a reply.
5742 * @param remoteInput The remote input parcelable which will carry the reply.
5743 * @return This object for method chaining.
5744 *
5745 * @see CarExtender.UnreadConversation#getRemoteInput
5746 * @see CarExtender.UnreadConversation#getReplyPendingIntent
5747 */
5748 public Builder setReplyAction(
5749 PendingIntent pendingIntent, RemoteInput remoteInput) {
5750 mRemoteInput = remoteInput;
5751 mReplyPendingIntent = pendingIntent;
5752
5753 return this;
5754 }
5755
5756 /**
5757 * Sets the pending intent that will be sent once the messages in this notification
5758 * are read.
5759 *
5760 * @param pendingIntent The pending intent to use.
5761 * @return This object for method chaining.
5762 */
5763 public Builder setReadPendingIntent(PendingIntent pendingIntent) {
5764 mReadPendingIntent = pendingIntent;
5765 return this;
5766 }
5767
5768 /**
5769 * Sets the timestamp of the most recent message in an unread conversation.
5770 *
5771 * If a messaging notification has been posted by your application and has not
5772 * yet been cancelled, posting a later notification with the same id and tag
5773 * but without a newer timestamp may result in Android Auto not displaying a
5774 * heads up notification for the later notification.
5775 *
5776 * @param timestamp The timestamp of the most recent message in the conversation.
5777 * @return This object for method chaining.
5778 */
5779 public Builder setLatestTimestamp(long timestamp) {
5780 mLatestTimestamp = timestamp;
5781 return this;
5782 }
5783
5784 /**
5785 * Builds a new unread conversation object.
5786 *
5787 * @return The new unread conversation object.
5788 */
5789 public UnreadConversation build() {
5790 String[] messages = mMessages.toArray(new String[mMessages.size()]);
5791 String[] participants = { mParticipant };
5792 return new UnreadConversation(messages, mRemoteInput, mReplyPendingIntent,
5793 mReadPendingIntent, participants, mLatestTimestamp);
5794 }
5795 }
5796 }
5797
5798 /**
Griff Hazen61a9e862014-05-22 16:05:19 -07005799 * Get an array of Notification objects from a parcelable array bundle field.
5800 * Update the bundle to have a typed array so fetches in the future don't need
5801 * to do an array copy.
5802 */
5803 private static Notification[] getNotificationArrayFromBundle(Bundle bundle, String key) {
5804 Parcelable[] array = bundle.getParcelableArray(key);
5805 if (array instanceof Notification[] || array == null) {
5806 return (Notification[]) array;
5807 }
5808 Notification[] typedArray = Arrays.copyOf(array, array.length,
5809 Notification[].class);
5810 bundle.putParcelableArray(key, typedArray);
5811 return typedArray;
5812 }
Christoph Studer4600f9b2014-07-22 22:44:43 +02005813
5814 private static class BuilderRemoteViews extends RemoteViews {
5815 public BuilderRemoteViews(Parcel parcel) {
5816 super(parcel);
5817 }
5818
Kenny Guy77320062014-08-27 21:37:15 +01005819 public BuilderRemoteViews(ApplicationInfo appInfo, int layoutId) {
5820 super(appInfo, layoutId);
Christoph Studer4600f9b2014-07-22 22:44:43 +02005821 }
5822
5823 @Override
5824 public BuilderRemoteViews clone() {
5825 Parcel p = Parcel.obtain();
5826 writeToParcel(p, 0);
5827 p.setDataPosition(0);
5828 BuilderRemoteViews brv = new BuilderRemoteViews(p);
5829 p.recycle();
5830 return brv;
5831 }
5832 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005833}