blob: 2c34371a12c1b5f08386a57b98e1ae7813259d9e [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 */
Julia Reynolds3b848122016-02-26 10:45:32 -05003290 public RemoteViews createContentView() {
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 */
Julia Reynolds3b848122016-02-26 10:45:32 -05003305 public RemoteViews createBigContentView() {
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 */
Julia Reynolds3b848122016-02-26 10:45:32 -05003348 public RemoteViews createHeadsUpContentView() {
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
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01003361 return applyStandardTemplateWithActions(getBigBaseLayoutResource());
Chris Wren8fd39ec2014-02-27 17:43:26 -05003362 }
3363
Selim Cinek624c02db2015-12-14 21:00:02 -08003364 /**
3365 * Construct a RemoteViews for the display in public contexts like on the lockscreen.
3366 *
3367 * @hide
3368 */
3369 public RemoteViews makePublicContentView() {
3370 if (mN.publicVersion != null) {
3371 final Builder builder = recoverBuilder(mContext, mN.publicVersion);
Julia Reynolds3b848122016-02-26 10:45:32 -05003372 return builder.createContentView();
Selim Cinek624c02db2015-12-14 21:00:02 -08003373 }
3374 Bundle savedBundle = mN.extras;
3375 Style style = mStyle;
3376 mStyle = null;
3377 Icon largeIcon = mN.mLargeIcon;
3378 mN.mLargeIcon = null;
3379 Bundle publicExtras = new Bundle();
3380 publicExtras.putBoolean(EXTRA_SHOW_WHEN,
3381 savedBundle.getBoolean(EXTRA_SHOW_WHEN));
3382 publicExtras.putBoolean(EXTRA_SHOW_CHRONOMETER,
3383 savedBundle.getBoolean(EXTRA_SHOW_CHRONOMETER));
Selim Cinek81c23aa2016-02-25 16:23:13 -08003384 publicExtras.putBoolean(EXTRA_CHRONOMETER_COUNTS_DOWN,
3385 savedBundle.getBoolean(EXTRA_CHRONOMETER_COUNTS_DOWN));
Selim Cinek624c02db2015-12-14 21:00:02 -08003386 publicExtras.putCharSequence(EXTRA_TITLE,
3387 mContext.getString(R.string.notification_hidden_text));
3388 mN.extras = publicExtras;
3389 final RemoteViews publicView = applyStandardTemplate(getBaseLayoutResource());
3390 mN.extras = savedBundle;
3391 mN.mLargeIcon = largeIcon;
3392 mStyle = style;
3393 return publicView;
3394 }
3395
3396
Chris Wren8fd39ec2014-02-27 17:43:26 -05003397
Daniel Sandler96fd7c12012-03-30 16:37:36 -04003398 private RemoteViews generateActionButton(Action action) {
Daniel Sandler8680bf82012-05-15 16:52:52 -04003399 final boolean tombstone = (action.actionIntent == null);
Selim Cinekf33b1112015-07-15 17:45:11 -07003400 RemoteViews button = new BuilderRemoteViews(mContext.getApplicationInfo(),
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01003401 tombstone ? getActionTombstoneLayoutResource()
3402 : getActionLayoutResource());
Dan Sandler68079d52015-07-22 10:45:30 -04003403 final Icon ai = action.getIcon();
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01003404 button.setTextViewText(R.id.action0, processLegacyText(action.title));
Daniel Sandler8680bf82012-05-15 16:52:52 -04003405 if (!tombstone) {
Daniel Sandlere5518842012-05-10 16:20:40 -04003406 button.setOnClickPendingIntent(R.id.action0, action.actionIntent);
Daniel Sandlere5518842012-05-10 16:20:40 -04003407 }
Daniel Sandler96fd7c12012-03-30 16:37:36 -04003408 button.setContentDescription(R.id.action0, action.title);
Adrian Roosfe84e1f2015-11-04 15:55:39 -08003409 if (action.mRemoteInputs != null) {
3410 button.setRemoteInputs(R.id.action0, action.mRemoteInputs);
3411 }
3412 if (mN.color != COLOR_DEFAULT) {
3413 button.setTextColor(R.id.action0, mN.color);
3414 }
Daniel Sandler96fd7c12012-03-30 16:37:36 -04003415 return button;
3416 }
3417
Joe Onoratocb109a02011-01-18 17:57:41 -08003418 /**
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01003419 * @return Whether we are currently building a notification from a legacy (an app that
Alan Viverette3cb07a462014-06-06 14:19:53 -07003420 * doesn't create material notifications by itself) app.
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01003421 */
3422 private boolean isLegacy() {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003423 return getColorUtil() != null;
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01003424 }
3425
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01003426 private CharSequence processLegacyText(CharSequence charSequence) {
3427 if (isLegacy()) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003428 return getColorUtil().invertCharSequenceColors(charSequence);
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01003429 } else {
3430 return charSequence;
3431 }
3432 }
3433
Dan Sandler26e81cf2014-05-06 10:01:27 -04003434 /**
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003435 * Apply any necessariy colors to the small icon
Dan Sandler26e81cf2014-05-06 10:01:27 -04003436 */
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003437 private void processSmallIconColor(Icon smallIcon, RemoteViews contentView) {
Selim Cinekea4bef72015-12-02 15:51:10 -08003438 boolean colorable = !isLegacy() || getColorUtil().isGrayscaleIcon(mContext, smallIcon);
3439 if (colorable) {
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003440 contentView.setDrawableParameters(R.id.icon, false, -1, resolveColor(),
Jorim Jaggi92df1f22014-12-16 19:44:41 +01003441 PorterDuff.Mode.SRC_ATOP, -1);
Selim Cinekea4bef72015-12-02 15:51:10 -08003442
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01003443 }
Selim Cinekea4bef72015-12-02 15:51:10 -08003444 contentView.setInt(R.id.notification_header, "setOriginalIconColor",
3445 colorable ? resolveColor() : NotificationHeaderView.NO_COLOR);
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01003446 }
3447
Dan Sandler26e81cf2014-05-06 10:01:27 -04003448 /**
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003449 * Make the largeIcon dark if it's a fake smallIcon (that is,
Dan Sandler26e81cf2014-05-06 10:01:27 -04003450 * if it's grayscale).
3451 */
3452 // TODO: also check bounds, transparency, that sort of thing.
Dan Sandlerd63f9322015-05-06 15:18:49 -04003453 private void processLargeLegacyIcon(Icon largeIcon, RemoteViews contentView) {
3454 if (largeIcon != null && isLegacy()
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003455 && getColorUtil().isGrayscaleIcon(mContext, largeIcon)) {
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003456 // resolve color will fall back to the default when legacy
3457 contentView.setDrawableParameters(R.id.icon, false, -1, resolveColor(),
Dan Sandler190d58d2014-05-15 09:33:39 -04003458 PorterDuff.Mode.SRC_ATOP, -1);
Jorim Jaggi92df1f22014-12-16 19:44:41 +01003459 }
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01003460 }
3461
Julia Reynolds10ee1fc2015-11-09 11:04:55 -05003462 private void sanitizeColor() {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003463 if (mN.color != COLOR_DEFAULT) {
3464 mN.color |= 0xFF000000; // no alpha for custom colors
Jorim Jaggi74419312014-06-10 20:57:21 +02003465 }
Jorim Jaggi74419312014-06-10 20:57:21 +02003466 }
3467
Selim Cinek5bf069a2015-11-10 19:14:27 -05003468 int resolveColor() {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003469 if (mN.color == COLOR_DEFAULT) {
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003470 return mContext.getColor(R.color.notification_icon_default_color);
Dan Sandler26e81cf2014-05-06 10:01:27 -04003471 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003472 return mN.color;
Dan Sandler26e81cf2014-05-06 10:01:27 -04003473 }
3474
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01003475 /**
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003476 * Apply the unstyled operations and return a new {@link Notification} object.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04003477 * @hide
Joe Onoratocb109a02011-01-18 17:57:41 -08003478 */
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04003479 public Notification buildUnstyled() {
Daniel Sandlera0a938c2012-03-15 08:42:37 -04003480 if (mActions.size() > 0) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003481 mN.actions = new Action[mActions.size()];
3482 mActions.toArray(mN.actions);
Daniel Sandlera0a938c2012-03-15 08:42:37 -04003483 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003484 if (!mPersonList.isEmpty()) {
3485 mN.extras.putStringArray(EXTRA_PEOPLE,
3486 mPersonList.toArray(new String[mPersonList.size()]));
Dan Sandler0bf2ed82013-12-21 23:33:41 -06003487 }
Selim Cinek247fa012016-02-18 09:50:48 -08003488 if (mN.bigContentView != null || mN.contentView != null
3489 || mN.headsUpContentView != null) {
3490 mN.extras.putBoolean(EXTRA_CONTAINS_CUSTOM_VIEW, true);
3491 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003492 return mN;
Joe Onorato46439ce2010-11-19 13:56:21 -08003493 }
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003494
Julia Reynolds3b848122016-02-26 10:45:32 -05003495 /**
3496 * Creates a Builder from an existing notification so further changes can be made.
3497 * @param context The context for your application / activity.
3498 * @param n The notification to create a Builder from.
3499 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003500 public static Notification.Builder recoverBuilder(Context context, Notification n) {
Christoph Studer4600f9b2014-07-22 22:44:43 +02003501 // Re-create notification context so we can access app resources.
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003502 ApplicationInfo applicationInfo = n.extras.getParcelable(
3503 EXTRA_BUILDER_APPLICATION_INFO);
Christoph Studer4600f9b2014-07-22 22:44:43 +02003504 Context builderContext;
Julia Reynoldsda303542015-11-23 14:00:20 -05003505 if (applicationInfo != null) {
3506 try {
3507 builderContext = context.createApplicationContext(applicationInfo,
3508 Context.CONTEXT_RESTRICTED);
3509 } catch (NameNotFoundException e) {
3510 Log.e(TAG, "ApplicationInfo " + applicationInfo + " not found");
3511 builderContext = context; // try with our context
3512 }
3513 } else {
3514 builderContext = context; // try with given context
Christoph Studer4600f9b2014-07-22 22:44:43 +02003515 }
3516
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003517 return new Builder(builderContext, n);
Christoph Studer4600f9b2014-07-22 22:44:43 +02003518 }
3519
3520 private static Class<? extends Style> getNotificationStyleClass(String templateClass) {
Selim Cinek593610c2016-02-16 18:42:57 -08003521 Class<? extends Style>[] classes = new Class[] {
3522 BigTextStyle.class, BigPictureStyle.class, InboxStyle.class, MediaStyle.class,
Selim Cinek03eb3b72016-02-18 10:39:45 -08003523 DecoratedCustomViewStyle.class, DecoratedMediaCustomViewStyle.class };
Christoph Studer4600f9b2014-07-22 22:44:43 +02003524 for (Class<? extends Style> innerClass : classes) {
3525 if (templateClass.equals(innerClass.getName())) {
3526 return innerClass;
3527 }
3528 }
3529 return null;
3530 }
3531
Daniel Sandlerf45564e2013-04-15 15:05:08 -04003532 /**
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003533 * @deprecated Use {@link #build()} instead.
3534 */
3535 @Deprecated
3536 public Notification getNotification() {
3537 return build();
3538 }
3539
3540 /**
3541 * Combine all of the options that have been set and return a new {@link Notification}
3542 * object.
3543 */
3544 public Notification build() {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003545 // first, add any extras from the calling code
3546 if (mUserExtras != null) {
3547 mN.extras = getAllExtras();
Jorim Jaggia0d58ae2015-06-03 11:48:13 -07003548 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003549
3550 // lazy stuff from mContext; see comment in Builder(Context, Notification)
Julia Reynoldsda303542015-11-23 14:00:20 -05003551 Notification.addFieldsFromContext(mContext, mN);
Christoph Studer943aa672014-08-03 20:31:16 +02003552
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003553 buildUnstyled();
Daniel Sandlerf45564e2013-04-15 15:05:08 -04003554
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003555 if (mStyle != null) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003556 mStyle.buildStyled(mN);
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003557 }
Daniel Sandlerf45564e2013-04-15 15:05:08 -04003558
Adrian Roos5081c0d2016-02-26 16:04:19 -08003559 if (mContext.getApplicationInfo().targetSdkVersion < Build.VERSION_CODES.N
3560 && (mStyle == null || !mStyle.displayCustomViewInline())) {
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05003561 if (mN.contentView == null) {
Julia Reynolds3b848122016-02-26 10:45:32 -05003562 mN.contentView = createContentView();
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05003563 mN.extras.putInt(EXTRA_REBUILD_CONTENT_VIEW_ACTION_COUNT,
3564 mN.contentView.getSequenceNumber());
3565 }
3566 if (mN.bigContentView == null) {
Julia Reynolds3b848122016-02-26 10:45:32 -05003567 mN.bigContentView = createBigContentView();
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05003568 if (mN.bigContentView != null) {
3569 mN.extras.putInt(EXTRA_REBUILD_BIG_CONTENT_VIEW_ACTION_COUNT,
3570 mN.bigContentView.getSequenceNumber());
3571 }
3572 }
3573 if (mN.headsUpContentView == null) {
Julia Reynolds3b848122016-02-26 10:45:32 -05003574 mN.headsUpContentView = createHeadsUpContentView();
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05003575 if (mN.headsUpContentView != null) {
3576 mN.extras.putInt(EXTRA_REBUILD_HEADS_UP_CONTENT_VIEW_ACTION_COUNT,
3577 mN.headsUpContentView.getSequenceNumber());
3578 }
3579 }
3580 }
3581
Julia Reynolds4c0c2022016-02-02 15:11:59 -05003582 if ((mN.defaults & DEFAULT_LIGHTS) != 0) {
3583 mN.flags |= FLAG_SHOW_LIGHTS;
3584 }
3585
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003586 return mN;
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003587 }
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05003588
3589 /**
3590 * Apply this Builder to an existing {@link Notification} object.
3591 *
3592 * @hide
3593 */
3594 public Notification buildInto(Notification n) {
Daniel Sandler1a497d32013-04-18 14:52:45 -04003595 build().cloneInto(n, true);
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05003596 return n;
3597 }
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01003598
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05003599 /**
3600 * @hide
3601 */
3602 public static void stripForDelivery(Notification n) {
3603 String templateClass = n.extras.getString(EXTRA_TEMPLATE);
3604 if (TextUtils.isEmpty(templateClass)) {
3605 return;
3606 }
3607 // Only strip views for known Styles because we won't know how to
3608 // re-create them otherwise.
3609 if (getNotificationStyleClass(templateClass) == null) {
3610 return;
3611 }
3612 // Get rid of unmodified BuilderRemoteViews.
3613 if (n.contentView instanceof BuilderRemoteViews &&
3614 n.extras.getInt(EXTRA_REBUILD_CONTENT_VIEW_ACTION_COUNT, -1) ==
3615 n.contentView.getSequenceNumber()) {
3616 n.contentView = null;
3617 n.extras.remove(EXTRA_REBUILD_CONTENT_VIEW_ACTION_COUNT);
3618 }
3619 if (n.bigContentView instanceof BuilderRemoteViews &&
3620 n.extras.getInt(EXTRA_REBUILD_BIG_CONTENT_VIEW_ACTION_COUNT, -1) ==
3621 n.bigContentView.getSequenceNumber()) {
3622 n.bigContentView = null;
3623 n.extras.remove(EXTRA_REBUILD_BIG_CONTENT_VIEW_ACTION_COUNT);
3624 }
3625 if (n.headsUpContentView instanceof BuilderRemoteViews &&
3626 n.extras.getInt(EXTRA_REBUILD_HEADS_UP_CONTENT_VIEW_ACTION_COUNT, -1) ==
3627 n.headsUpContentView.getSequenceNumber()) {
3628 n.headsUpContentView = null;
3629 n.extras.remove(EXTRA_REBUILD_HEADS_UP_CONTENT_VIEW_ACTION_COUNT);
3630 }
3631 }
3632
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01003633 private int getBaseLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07003634 return R.layout.notification_template_material_base;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01003635 }
3636
3637 private int getBigBaseLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07003638 return R.layout.notification_template_material_big_base;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01003639 }
3640
3641 private int getBigPictureLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07003642 return R.layout.notification_template_material_big_picture;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01003643 }
3644
3645 private int getBigTextLayoutResource() {
Jorim Jaggi445d3c02014-08-19 22:33:42 +02003646 return R.layout.notification_template_material_big_text;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01003647 }
3648
3649 private int getInboxLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07003650 return R.layout.notification_template_material_inbox;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01003651 }
3652
3653 private int getActionLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07003654 return R.layout.notification_material_action;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01003655 }
3656
3657 private int getActionTombstoneLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07003658 return R.layout.notification_material_action_tombstone;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01003659 }
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003660 }
3661
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003662 /**
3663 * An object that can apply a rich notification style to a {@link Notification.Builder}
3664 * object.
3665 */
Griff Hazendfcb0802014-02-11 12:00:00 -08003666 public static abstract class Style {
Chris Wrend6297db2012-05-03 16:20:13 -04003667 private CharSequence mBigContentTitle;
Jorim Jaggi457a10d2014-09-08 16:18:23 +02003668
3669 /**
3670 * @hide
3671 */
3672 protected CharSequence mSummaryText = null;
3673
3674 /**
3675 * @hide
3676 */
3677 protected boolean mSummaryTextSet = false;
Chris Wrend6297db2012-05-03 16:20:13 -04003678
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003679 protected Builder mBuilder;
3680
Chris Wrend6297db2012-05-03 16:20:13 -04003681 /**
3682 * Overrides ContentTitle in the big form of the template.
3683 * This defaults to the value passed to setContentTitle().
3684 */
3685 protected void internalSetBigContentTitle(CharSequence title) {
3686 mBigContentTitle = title;
3687 }
3688
3689 /**
3690 * Set the first line of text after the detail section in the big form of the template.
3691 */
3692 protected void internalSetSummaryText(CharSequence cs) {
3693 mSummaryText = cs;
Daniel Sandler619738c2012-06-07 16:33:08 -04003694 mSummaryTextSet = true;
Chris Wrend6297db2012-05-03 16:20:13 -04003695 }
3696
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003697 public void setBuilder(Builder builder) {
3698 if (mBuilder != builder) {
3699 mBuilder = builder;
Daniel Sandlerc08dea22012-06-28 08:35:24 -07003700 if (mBuilder != null) {
3701 mBuilder.setStyle(this);
3702 }
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003703 }
3704 }
3705
Chris Wrend6297db2012-05-03 16:20:13 -04003706 protected void checkBuilder() {
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003707 if (mBuilder == null) {
3708 throw new IllegalArgumentException("Style requires a valid Builder object");
3709 }
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003710 }
Chris Wrend6297db2012-05-03 16:20:13 -04003711
3712 protected RemoteViews getStandardView(int layoutId) {
3713 checkBuilder();
3714
Christoph Studer4600f9b2014-07-22 22:44:43 +02003715 // Nasty.
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003716 CharSequence oldBuilderContentTitle =
3717 mBuilder.getAllExtras().getCharSequence(EXTRA_TITLE);
Chris Wrend6297db2012-05-03 16:20:13 -04003718 if (mBigContentTitle != null) {
3719 mBuilder.setContentTitle(mBigContentTitle);
3720 }
3721
Chris Wrend6297db2012-05-03 16:20:13 -04003722 RemoteViews contentView = mBuilder.applyStandardTemplateWithActions(layoutId);
3723
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003724 mBuilder.getAllExtras().putCharSequence(EXTRA_TITLE, oldBuilderContentTitle);
Christoph Studer4600f9b2014-07-22 22:44:43 +02003725
Chris Wrend6297db2012-05-03 16:20:13 -04003726 if (mBigContentTitle != null && mBigContentTitle.equals("")) {
3727 contentView.setViewVisibility(R.id.line1, View.GONE);
Chris Wren67dc9a02012-05-16 01:03:20 -04003728 } else {
3729 contentView.setViewVisibility(R.id.line1, View.VISIBLE);
Chris Wrend6297db2012-05-03 16:20:13 -04003730 }
3731
Chris Wrend6297db2012-05-03 16:20:13 -04003732 return contentView;
3733 }
3734
Daniel Sandlerf45564e2013-04-15 15:05:08 -04003735 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003736 * Construct a Style-specific RemoteViews for the final 1U notification layout.
3737 * The default implementation has nothing additional to add.
3738 * @hide
3739 */
3740 public RemoteViews makeContentView() {
3741 return null;
3742 }
3743
3744 /**
3745 * Construct a Style-specific RemoteViews for the final big notification layout.
3746 * @hide
3747 */
3748 public RemoteViews makeBigContentView() {
3749 return null;
3750 }
3751
3752 /**
3753 * Construct a Style-specific RemoteViews for the final HUN layout.
3754 * @hide
3755 */
3756 public RemoteViews makeHeadsUpContentView() {
3757 return null;
3758 }
3759
3760 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003761 * Apply any style-specific extras to this notification before shipping it out.
Daniel Sandlerf45564e2013-04-15 15:05:08 -04003762 * @hide
3763 */
3764 public void addExtras(Bundle extras) {
3765 if (mSummaryTextSet) {
3766 extras.putCharSequence(EXTRA_SUMMARY_TEXT, mSummaryText);
3767 }
3768 if (mBigContentTitle != null) {
3769 extras.putCharSequence(EXTRA_TITLE_BIG, mBigContentTitle);
3770 }
Chris Wren91ad5632013-06-05 15:05:57 -04003771 extras.putString(EXTRA_TEMPLATE, this.getClass().getName());
Daniel Sandlerf45564e2013-04-15 15:05:08 -04003772 }
3773
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04003774 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003775 * Reconstruct the internal state of this Style object from extras.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04003776 * @hide
3777 */
Christoph Studer4600f9b2014-07-22 22:44:43 +02003778 protected void restoreFromExtras(Bundle extras) {
3779 if (extras.containsKey(EXTRA_SUMMARY_TEXT)) {
3780 mSummaryText = extras.getCharSequence(EXTRA_SUMMARY_TEXT);
3781 mSummaryTextSet = true;
3782 }
3783 if (extras.containsKey(EXTRA_TITLE_BIG)) {
3784 mBigContentTitle = extras.getCharSequence(EXTRA_TITLE_BIG);
3785 }
3786 }
3787
3788
3789 /**
3790 * @hide
3791 */
3792 public Notification buildStyled(Notification wip) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003793 addExtras(wip.extras);
Christoph Studer4600f9b2014-07-22 22:44:43 +02003794 return wip;
3795 }
3796
Daniel Sandler0ec46202015-06-24 01:27:05 -04003797 /**
3798 * @hide
3799 */
Jorim Jaggia0d58ae2015-06-03 11:48:13 -07003800 public void purgeResources() {}
3801
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04003802 /**
3803 * Calls {@link android.app.Notification.Builder#build()} on the Builder this Style is
3804 * attached to.
3805 *
3806 * @return the fully constructed Notification.
3807 */
3808 public Notification build() {
3809 checkBuilder();
3810 return mBuilder.build();
3811 }
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02003812
3813 /**
3814 * @hide
3815 * @return true if the style positions the progress bar on the second line; false if the
3816 * style hides the progress bar
3817 */
3818 protected boolean hasProgress() {
3819 return true;
3820 }
Selim Cinek03d0d652015-11-13 13:18:09 -05003821
3822 /**
3823 * @hide
3824 * @return Whether we should put the summary be put into the notification header
3825 */
3826 public boolean hasSummaryInHeader() {
3827 return true;
3828 }
Selim Cinek593610c2016-02-16 18:42:57 -08003829
3830 /**
3831 * @hide
3832 * @return Whether custom content views are displayed inline in the style
3833 */
3834 public boolean displayCustomViewInline() {
3835 return false;
3836 }
Joe Onorato46439ce2010-11-19 13:56:21 -08003837 }
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003838
3839 /**
Daniel Sandler4dfbe832012-04-11 14:51:46 -04003840 * Helper class for generating large-format notifications that include a large image attachment.
Joe Malin8d40d042012-11-05 11:36:40 -08003841 *
Robert Ly91c5ce32014-06-08 15:37:00 -07003842 * Here's how you'd set the <code>BigPictureStyle</code> on a notification:
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003843 * <pre class="prettyprint">
Robert Ly91c5ce32014-06-08 15:37:00 -07003844 * Notification notif = new Notification.Builder(mContext)
3845 * .setContentTitle(&quot;New photo from &quot; + sender.toString())
3846 * .setContentText(subject)
3847 * .setSmallIcon(R.drawable.new_post)
3848 * .setLargeIcon(aBitmap)
3849 * .setStyle(new Notification.BigPictureStyle()
3850 * .bigPicture(aBigBitmap))
3851 * .build();
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003852 * </pre>
Joe Malin8d40d042012-11-05 11:36:40 -08003853 *
Daniel Sandler4dfbe832012-04-11 14:51:46 -04003854 * @see Notification#bigContentView
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003855 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003856 public static class BigPictureStyle extends Style {
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003857 private Bitmap mPicture;
Dan Sandlerd63f9322015-05-06 15:18:49 -04003858 private Icon mBigLargeIcon;
Chris Wren3745a3d2012-05-22 15:11:52 -04003859 private boolean mBigLargeIconSet = false;
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003860
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003861 public BigPictureStyle() {
3862 }
3863
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003864 public BigPictureStyle(Builder builder) {
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003865 setBuilder(builder);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003866 }
3867
Chris Wrend6297db2012-05-03 16:20:13 -04003868 /**
3869 * Overrides ContentTitle in the big form of the template.
3870 * This defaults to the value passed to setContentTitle().
3871 */
3872 public BigPictureStyle setBigContentTitle(CharSequence title) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04003873 internalSetBigContentTitle(safeCharSequence(title));
Chris Wrend6297db2012-05-03 16:20:13 -04003874 return this;
3875 }
3876
3877 /**
3878 * Set the first line of text after the detail section in the big form of the template.
3879 */
3880 public BigPictureStyle setSummaryText(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04003881 internalSetSummaryText(safeCharSequence(cs));
Chris Wrend6297db2012-05-03 16:20:13 -04003882 return this;
3883 }
3884
Chris Wren0bd664d2012-08-01 13:56:56 -04003885 /**
3886 * Provide the bitmap to be used as the payload for the BigPicture notification.
3887 */
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003888 public BigPictureStyle bigPicture(Bitmap b) {
3889 mPicture = b;
3890 return this;
3891 }
3892
Chris Wren3745a3d2012-05-22 15:11:52 -04003893 /**
Chris Wren3745a3d2012-05-22 15:11:52 -04003894 * Override the large icon when the big notification is shown.
3895 */
3896 public BigPictureStyle bigLargeIcon(Bitmap b) {
Dan Sandlerd63f9322015-05-06 15:18:49 -04003897 return bigLargeIcon(b != null ? Icon.createWithBitmap(b) : null);
3898 }
3899
3900 /**
3901 * Override the large icon when the big notification is shown.
3902 */
3903 public BigPictureStyle bigLargeIcon(Icon icon) {
Chris Wren3745a3d2012-05-22 15:11:52 -04003904 mBigLargeIconSet = true;
Dan Sandlerd63f9322015-05-06 15:18:49 -04003905 mBigLargeIcon = icon;
Chris Wren3745a3d2012-05-22 15:11:52 -04003906 return this;
3907 }
3908
Riley Andrews0394a0c2015-11-03 23:36:52 -08003909 /** @hide */
3910 public static final int MIN_ASHMEM_BITMAP_SIZE = 128 * (1 << 10);
3911
Daniel Sandler0ec46202015-06-24 01:27:05 -04003912 /**
3913 * @hide
3914 */
Jorim Jaggia0d58ae2015-06-03 11:48:13 -07003915 @Override
3916 public void purgeResources() {
3917 super.purgeResources();
Riley Andrews8cee7c12015-11-01 23:36:04 -08003918 if (mPicture != null &&
3919 mPicture.isMutable() &&
Riley Andrews0394a0c2015-11-03 23:36:52 -08003920 mPicture.getAllocationByteCount() >= MIN_ASHMEM_BITMAP_SIZE) {
Jorim Jaggia0d58ae2015-06-03 11:48:13 -07003921 mPicture = mPicture.createAshmemBitmap();
3922 }
3923 if (mBigLargeIcon != null) {
3924 mBigLargeIcon.convertToAshmem();
3925 }
3926 }
Christoph Studer5c510ee2014-12-15 16:32:27 +01003927
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003928 /**
3929 * @hide
3930 */
3931 public RemoteViews makeBigContentView() {
3932 // Replace mN.mLargeIcon with mBigLargeIcon if mBigLargeIconSet
Christoph Studer5c510ee2014-12-15 16:32:27 +01003933 // This covers the following cases:
3934 // 1. mBigLargeIconSet -> mBigLargeIcon (null or non-null) applies, overrides
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003935 // mN.mLargeIcon
3936 // 2. !mBigLargeIconSet -> mN.mLargeIcon applies
Dan Sandlerd63f9322015-05-06 15:18:49 -04003937 Icon oldLargeIcon = null;
Christoph Studer5c510ee2014-12-15 16:32:27 +01003938 if (mBigLargeIconSet) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003939 oldLargeIcon = mBuilder.mN.mLargeIcon;
3940 mBuilder.mN.mLargeIcon = mBigLargeIcon;
Christoph Studer5c510ee2014-12-15 16:32:27 +01003941 }
3942
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01003943 RemoteViews contentView = getStandardView(mBuilder.getBigPictureLayoutResource());
Selim Cinek03d0d652015-11-13 13:18:09 -05003944 if (mSummaryTextSet) {
3945 contentView.setTextViewText(R.id.text, mBuilder.processLegacyText(mSummaryText));
Selim Cinekc848c3a2016-01-13 15:27:30 -08003946 contentView.setViewVisibility(R.id.text, View.VISIBLE);
Selim Cinek03d0d652015-11-13 13:18:09 -05003947 }
Selim Cinek860b6da2015-12-16 19:02:19 -08003948 mBuilder.setContentMinHeight(contentView, mBuilder.mN.mLargeIcon != null);
Selim Cinek53e64a42015-11-16 10:40:56 -08003949
Christoph Studer5c510ee2014-12-15 16:32:27 +01003950 if (mBigLargeIconSet) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003951 mBuilder.mN.mLargeIcon = oldLargeIcon;
Christoph Studer5c510ee2014-12-15 16:32:27 +01003952 }
3953
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003954 contentView.setImageViewBitmap(R.id.big_picture, mPicture);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003955 return contentView;
3956 }
3957
Daniel Sandlerf45564e2013-04-15 15:05:08 -04003958 /**
3959 * @hide
3960 */
3961 public void addExtras(Bundle extras) {
3962 super.addExtras(extras);
3963
3964 if (mBigLargeIconSet) {
3965 extras.putParcelable(EXTRA_LARGE_ICON_BIG, mBigLargeIcon);
3966 }
3967 extras.putParcelable(EXTRA_PICTURE, mPicture);
3968 }
3969
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04003970 /**
3971 * @hide
3972 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003973 @Override
Christoph Studer4600f9b2014-07-22 22:44:43 +02003974 protected void restoreFromExtras(Bundle extras) {
3975 super.restoreFromExtras(extras);
3976
3977 if (extras.containsKey(EXTRA_LARGE_ICON_BIG)) {
Christoph Studer5c510ee2014-12-15 16:32:27 +01003978 mBigLargeIconSet = true;
Christoph Studer4600f9b2014-07-22 22:44:43 +02003979 mBigLargeIcon = extras.getParcelable(EXTRA_LARGE_ICON_BIG);
Chris Wren3745a3d2012-05-22 15:11:52 -04003980 }
Christoph Studer4600f9b2014-07-22 22:44:43 +02003981 mPicture = extras.getParcelable(EXTRA_PICTURE);
3982 }
Selim Cinek03d0d652015-11-13 13:18:09 -05003983
3984 /**
3985 * @hide
3986 */
3987 @Override
3988 public boolean hasSummaryInHeader() {
3989 return false;
3990 }
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003991 }
3992
3993 /**
Daniel Sandler4dfbe832012-04-11 14:51:46 -04003994 * Helper class for generating large-format notifications that include a lot of text.
Joe Malin8d40d042012-11-05 11:36:40 -08003995 *
Robert Ly91c5ce32014-06-08 15:37:00 -07003996 * Here's how you'd set the <code>BigTextStyle</code> on a notification:
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003997 * <pre class="prettyprint">
Robert Ly91c5ce32014-06-08 15:37:00 -07003998 * Notification notif = new Notification.Builder(mContext)
3999 * .setContentTitle(&quot;New mail from &quot; + sender.toString())
4000 * .setContentText(subject)
4001 * .setSmallIcon(R.drawable.new_mail)
4002 * .setLargeIcon(aBitmap)
4003 * .setStyle(new Notification.BigTextStyle()
4004 * .bigText(aVeryLongString))
4005 * .build();
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004006 * </pre>
Joe Malin8d40d042012-11-05 11:36:40 -08004007 *
Daniel Sandler4dfbe832012-04-11 14:51:46 -04004008 * @see Notification#bigContentView
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004009 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004010 public static class BigTextStyle extends Style {
Jorim Jaggi457a10d2014-09-08 16:18:23 +02004011
4012 private static final int MAX_LINES = 13;
Selim Cinek3a2c4b92015-12-17 17:01:17 -08004013 private static final int LINES_CONSUMED_BY_ACTIONS = 4;
Jorim Jaggi457a10d2014-09-08 16:18:23 +02004014
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004015 private CharSequence mBigText;
4016
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004017 public BigTextStyle() {
4018 }
4019
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004020 public BigTextStyle(Builder builder) {
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004021 setBuilder(builder);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004022 }
4023
Chris Wrend6297db2012-05-03 16:20:13 -04004024 /**
4025 * Overrides ContentTitle in the big form of the template.
4026 * This defaults to the value passed to setContentTitle().
4027 */
4028 public BigTextStyle setBigContentTitle(CharSequence title) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04004029 internalSetBigContentTitle(safeCharSequence(title));
Chris Wrend6297db2012-05-03 16:20:13 -04004030 return this;
4031 }
4032
4033 /**
4034 * Set the first line of text after the detail section in the big form of the template.
4035 */
4036 public BigTextStyle setSummaryText(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04004037 internalSetSummaryText(safeCharSequence(cs));
Chris Wrend6297db2012-05-03 16:20:13 -04004038 return this;
4039 }
4040
Chris Wren0bd664d2012-08-01 13:56:56 -04004041 /**
4042 * Provide the longer text to be displayed in the big form of the
4043 * template in place of the content text.
4044 */
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004045 public BigTextStyle bigText(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04004046 mBigText = safeCharSequence(cs);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004047 return this;
4048 }
4049
Daniel Sandlerf45564e2013-04-15 15:05:08 -04004050 /**
4051 * @hide
4052 */
4053 public void addExtras(Bundle extras) {
4054 super.addExtras(extras);
4055
Christoph Studer4600f9b2014-07-22 22:44:43 +02004056 extras.putCharSequence(EXTRA_BIG_TEXT, mBigText);
4057 }
4058
4059 /**
4060 * @hide
4061 */
4062 @Override
4063 protected void restoreFromExtras(Bundle extras) {
4064 super.restoreFromExtras(extras);
4065
4066 mBigText = extras.getCharSequence(EXTRA_BIG_TEXT);
Daniel Sandlerf45564e2013-04-15 15:05:08 -04004067 }
4068
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004069 /**
4070 * @hide
4071 */
4072 public RemoteViews makeBigContentView() {
Christoph Studer4600f9b2014-07-22 22:44:43 +02004073
4074 // Nasty
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004075 CharSequence oldBuilderContentText =
4076 mBuilder.getAllExtras().getCharSequence(EXTRA_TEXT);
4077 mBuilder.getAllExtras().putCharSequence(EXTRA_TEXT, null);
Daniel Sandler916ad912012-06-13 12:17:07 -04004078
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01004079 RemoteViews contentView = getStandardView(mBuilder.getBigTextLayoutResource());
Joe Malin8d40d042012-11-05 11:36:40 -08004080
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004081 mBuilder.getAllExtras().putCharSequence(EXTRA_TEXT, oldBuilderContentText);
Christoph Studer4600f9b2014-07-22 22:44:43 +02004082
Selim Cinek3a2c4b92015-12-17 17:01:17 -08004083 CharSequence bigTextText = mBuilder.processLegacyText(mBigText);
4084 contentView.setTextViewText(R.id.big_text, bigTextText);
4085 contentView.setViewVisibility(R.id.big_text,
4086 TextUtils.isEmpty(bigTextText) ? View.GONE : View.VISIBLE);
Jorim Jaggi457a10d2014-09-08 16:18:23 +02004087 contentView.setInt(R.id.big_text, "setMaxLines", calculateMaxLines());
Selim Cinek4fb12d32015-11-19 18:10:48 -08004088 contentView.setBoolean(R.id.big_text, "setHasImage", mBuilder.mN.mLargeIcon != null);
4089
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004090 return contentView;
4091 }
4092
Jorim Jaggi457a10d2014-09-08 16:18:23 +02004093 private int calculateMaxLines() {
4094 int lineCount = MAX_LINES;
4095 boolean hasActions = mBuilder.mActions.size() > 0;
Jorim Jaggi457a10d2014-09-08 16:18:23 +02004096 if (hasActions) {
4097 lineCount -= LINES_CONSUMED_BY_ACTIONS;
4098 }
Jorim Jaggi457a10d2014-09-08 16:18:23 +02004099 return lineCount;
4100 }
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004101 }
Daniel Sandler879c5e02012-04-17 16:46:51 -04004102
4103 /**
4104 * Helper class for generating large-format notifications that include a list of (up to 5) strings.
Joe Malin8d40d042012-11-05 11:36:40 -08004105 *
Robert Ly91c5ce32014-06-08 15:37:00 -07004106 * Here's how you'd set the <code>InboxStyle</code> on a notification:
Daniel Sandler879c5e02012-04-17 16:46:51 -04004107 * <pre class="prettyprint">
Robert Ly91c5ce32014-06-08 15:37:00 -07004108 * Notification notif = new Notification.Builder(mContext)
4109 * .setContentTitle(&quot;5 New mails from &quot; + sender.toString())
4110 * .setContentText(subject)
4111 * .setSmallIcon(R.drawable.new_mail)
4112 * .setLargeIcon(aBitmap)
4113 * .setStyle(new Notification.InboxStyle()
4114 * .addLine(str1)
4115 * .addLine(str2)
4116 * .setContentTitle(&quot;&quot;)
4117 * .setSummaryText(&quot;+3 more&quot;))
4118 * .build();
Daniel Sandler879c5e02012-04-17 16:46:51 -04004119 * </pre>
Joe Malin8d40d042012-11-05 11:36:40 -08004120 *
Daniel Sandler879c5e02012-04-17 16:46:51 -04004121 * @see Notification#bigContentView
4122 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004123 public static class InboxStyle extends Style {
Daniel Sandler879c5e02012-04-17 16:46:51 -04004124 private ArrayList<CharSequence> mTexts = new ArrayList<CharSequence>(5);
4125
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004126 public InboxStyle() {
4127 }
4128
Daniel Sandler879c5e02012-04-17 16:46:51 -04004129 public InboxStyle(Builder builder) {
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004130 setBuilder(builder);
Daniel Sandler879c5e02012-04-17 16:46:51 -04004131 }
4132
Chris Wrend6297db2012-05-03 16:20:13 -04004133 /**
4134 * Overrides ContentTitle in the big form of the template.
4135 * This defaults to the value passed to setContentTitle().
4136 */
4137 public InboxStyle setBigContentTitle(CharSequence title) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04004138 internalSetBigContentTitle(safeCharSequence(title));
Chris Wrend6297db2012-05-03 16:20:13 -04004139 return this;
4140 }
4141
4142 /**
4143 * Set the first line of text after the detail section in the big form of the template.
4144 */
4145 public InboxStyle setSummaryText(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04004146 internalSetSummaryText(safeCharSequence(cs));
Chris Wrend6297db2012-05-03 16:20:13 -04004147 return this;
4148 }
4149
Chris Wren0bd664d2012-08-01 13:56:56 -04004150 /**
4151 * Append a line to the digest section of the Inbox notification.
4152 */
Daniel Sandler879c5e02012-04-17 16:46:51 -04004153 public InboxStyle addLine(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04004154 mTexts.add(safeCharSequence(cs));
Daniel Sandler879c5e02012-04-17 16:46:51 -04004155 return this;
4156 }
4157
Daniel Sandlerf45564e2013-04-15 15:05:08 -04004158 /**
4159 * @hide
4160 */
4161 public void addExtras(Bundle extras) {
4162 super.addExtras(extras);
Christoph Studer4600f9b2014-07-22 22:44:43 +02004163
Daniel Sandlerf45564e2013-04-15 15:05:08 -04004164 CharSequence[] a = new CharSequence[mTexts.size()];
4165 extras.putCharSequenceArray(EXTRA_TEXT_LINES, mTexts.toArray(a));
4166 }
4167
Christoph Studer4600f9b2014-07-22 22:44:43 +02004168 /**
4169 * @hide
4170 */
4171 @Override
4172 protected void restoreFromExtras(Bundle extras) {
4173 super.restoreFromExtras(extras);
4174
4175 mTexts.clear();
4176 if (extras.containsKey(EXTRA_TEXT_LINES)) {
4177 Collections.addAll(mTexts, extras.getCharSequenceArray(EXTRA_TEXT_LINES));
4178 }
4179 }
4180
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004181 /**
4182 * @hide
4183 */
4184 public RemoteViews makeBigContentView() {
Selim Cinekc848c3a2016-01-13 15:27:30 -08004185 // Remove the content text so it disappears unless you have a summary
Christoph Studer4600f9b2014-07-22 22:44:43 +02004186 // Nasty
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004187 CharSequence oldBuilderContentText = mBuilder.mN.extras.getCharSequence(EXTRA_TEXT);
4188 mBuilder.getAllExtras().putCharSequence(EXTRA_TEXT, null);
Christoph Studer4600f9b2014-07-22 22:44:43 +02004189
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01004190 RemoteViews contentView = getStandardView(mBuilder.getInboxLayoutResource());
Daniel Sandler619738c2012-06-07 16:33:08 -04004191
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004192 mBuilder.getAllExtras().putCharSequence(EXTRA_TEXT, oldBuilderContentText);
Christoph Studer4600f9b2014-07-22 22:44:43 +02004193
Chris Wrend6297db2012-05-03 16:20:13 -04004194 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 -04004195 R.id.inbox_text4, R.id.inbox_text5, R.id.inbox_text6};
Chris Wrend6297db2012-05-03 16:20:13 -04004196
Chris Wren4ed80d52012-05-17 09:30:03 -04004197 // Make sure all rows are gone in case we reuse a view.
4198 for (int rowId : rowIds) {
4199 contentView.setViewVisibility(rowId, View.GONE);
4200 }
4201
Jorim Jaggi445d3c02014-08-19 22:33:42 +02004202 final boolean largeText =
4203 mBuilder.mContext.getResources().getConfiguration().fontScale > 1f;
4204 final float subTextSize = mBuilder.mContext.getResources().getDimensionPixelSize(
4205 R.dimen.notification_subtext_size);
Daniel Sandler879c5e02012-04-17 16:46:51 -04004206 int i=0;
Selim Cinek9d9fc6e2015-11-12 15:49:14 -05004207 final float density = mBuilder.mContext.getResources().getDisplayMetrics().density;
4208 int topPadding = (int) (5 * density);
4209 int bottomPadding = (int) (13 * density);
Selim Cinek247fa012016-02-18 09:50:48 -08004210 boolean first = true;
Daniel Sandler879c5e02012-04-17 16:46:51 -04004211 while (i < mTexts.size() && i < rowIds.length) {
4212 CharSequence str = mTexts.get(i);
4213 if (str != null && !str.equals("")) {
4214 contentView.setViewVisibility(rowIds[i], View.VISIBLE);
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01004215 contentView.setTextViewText(rowIds[i], mBuilder.processLegacyText(str));
Jorim Jaggi445d3c02014-08-19 22:33:42 +02004216 if (largeText) {
4217 contentView.setTextViewTextSize(rowIds[i], TypedValue.COMPLEX_UNIT_PX,
4218 subTextSize);
4219 }
Selim Cinek9d9fc6e2015-11-12 15:49:14 -05004220 contentView.setViewPadding(rowIds[i], 0, topPadding, 0,
4221 i == rowIds.length - 1 || i == mTexts.size() - 1 ? bottomPadding : 0);
Selim Cinek247fa012016-02-18 09:50:48 -08004222 handleInboxImageMargin(contentView, rowIds[i], first);
4223 first = false;
Daniel Sandler879c5e02012-04-17 16:46:51 -04004224 }
4225 i++;
4226 }
Kenny Guy98193ea2014-07-24 19:54:37 +01004227
Selim Cinek1e0bf612015-11-20 15:57:26 -08004228
Daniel Sandler879c5e02012-04-17 16:46:51 -04004229 return contentView;
4230 }
Selim Cinek1e0bf612015-11-20 15:57:26 -08004231
Selim Cinek247fa012016-02-18 09:50:48 -08004232 private void handleInboxImageMargin(RemoteViews contentView, int id, boolean first) {
Selim Cinek1e0bf612015-11-20 15:57:26 -08004233 int endMargin = 0;
Selim Cinek247fa012016-02-18 09:50:48 -08004234 if (first) {
4235 final int max = mBuilder.mN.extras.getInt(EXTRA_PROGRESS_MAX, 0);
4236 final boolean ind = mBuilder.mN.extras.getBoolean(EXTRA_PROGRESS_INDETERMINATE);
4237 boolean hasProgress = max != 0 || ind;
4238 if (mBuilder.mN.mLargeIcon != null && !hasProgress) {
4239 endMargin = mBuilder.mContext.getResources().getDimensionPixelSize(
4240 R.dimen.notification_content_picture_margin);
4241 }
Selim Cinek1e0bf612015-11-20 15:57:26 -08004242 }
4243 contentView.setViewLayoutMarginEnd(id, endMargin);
4244 }
Daniel Sandler879c5e02012-04-17 16:46:51 -04004245 }
Dan Sandler842dd772014-05-15 09:36:47 -04004246
4247 /**
4248 * Notification style for media playback notifications.
4249 *
4250 * In the expanded form, {@link Notification#bigContentView}, up to 5
4251 * {@link Notification.Action}s specified with
Dan Sandler86647982015-05-13 23:41:13 -04004252 * {@link Notification.Builder#addAction(Action) addAction} will be
Dan Sandler842dd772014-05-15 09:36:47 -04004253 * shown as icon-only pushbuttons, suitable for transport controls. The Bitmap given to
4254 * {@link Notification.Builder#setLargeIcon(android.graphics.Bitmap) setLargeIcon()} will be
4255 * treated as album artwork.
4256 *
4257 * Unlike the other styles provided here, MediaStyle can also modify the standard-size
4258 * {@link Notification#contentView}; by providing action indices to
Christoph Studerfde6f4d2014-12-12 13:23:26 +01004259 * {@link #setShowActionsInCompactView(int...)} you can promote up to 3 actions to be displayed
Dan Sandler842dd772014-05-15 09:36:47 -04004260 * in the standard view alongside the usual content.
4261 *
Bryan Mawhinney6be8de32014-07-18 10:35:12 +01004262 * Notifications created with MediaStyle will have their category set to
4263 * {@link Notification#CATEGORY_TRANSPORT CATEGORY_TRANSPORT} unless you set a different
4264 * category using {@link Notification.Builder#setCategory(String) setCategory()}.
4265 *
Jeff Browndba34ba2014-06-24 20:46:03 -07004266 * Finally, if you attach a {@link android.media.session.MediaSession.Token} using
4267 * {@link android.app.Notification.MediaStyle#setMediaSession(MediaSession.Token)},
Dan Sandler842dd772014-05-15 09:36:47 -04004268 * the System UI can identify this as a notification representing an active media session
4269 * and respond accordingly (by showing album artwork in the lockscreen, for example).
4270 *
4271 * To use this style with your Notification, feed it to
4272 * {@link Notification.Builder#setStyle(android.app.Notification.Style)} like so:
4273 * <pre class="prettyprint">
4274 * Notification noti = new Notification.Builder()
4275 * .setSmallIcon(R.drawable.ic_stat_player)
Christoph Studere935fe92014-11-24 14:18:06 +01004276 * .setContentTitle(&quot;Track title&quot;)
4277 * .setContentText(&quot;Artist - Album&quot;)
4278 * .setLargeIcon(albumArtBitmap))
Bryan Mawhinney6be8de32014-07-18 10:35:12 +01004279 * .setStyle(<b>new Notification.MediaStyle()</b>
4280 * .setMediaSession(mySession))
Dan Sandler842dd772014-05-15 09:36:47 -04004281 * .build();
4282 * </pre>
4283 *
4284 * @see Notification#bigContentView
4285 */
4286 public static class MediaStyle extends Style {
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02004287 static final int MAX_MEDIA_BUTTONS_IN_COMPACT = 3;
Dan Sandler842dd772014-05-15 09:36:47 -04004288 static final int MAX_MEDIA_BUTTONS = 5;
4289
4290 private int[] mActionsToShowInCompact = null;
Jeff Browndba34ba2014-06-24 20:46:03 -07004291 private MediaSession.Token mToken;
Dan Sandler842dd772014-05-15 09:36:47 -04004292
4293 public MediaStyle() {
4294 }
4295
4296 public MediaStyle(Builder builder) {
4297 setBuilder(builder);
4298 }
4299
4300 /**
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02004301 * 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 -04004302 * notification view.
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02004303 *
4304 * @param actions the indices of the actions to show in the compact notification view
Dan Sandler842dd772014-05-15 09:36:47 -04004305 */
4306 public MediaStyle setShowActionsInCompactView(int...actions) {
4307 mActionsToShowInCompact = actions;
4308 return this;
4309 }
4310
4311 /**
Jeff Browndba34ba2014-06-24 20:46:03 -07004312 * Attach a {@link android.media.session.MediaSession.Token} to this Notification
4313 * to provide additional playback information and control to the SystemUI.
Dan Sandler842dd772014-05-15 09:36:47 -04004314 */
Jeff Browndba34ba2014-06-24 20:46:03 -07004315 public MediaStyle setMediaSession(MediaSession.Token token) {
Dan Sandler842dd772014-05-15 09:36:47 -04004316 mToken = token;
4317 return this;
4318 }
4319
Christoph Studer4600f9b2014-07-22 22:44:43 +02004320 /**
4321 * @hide
4322 */
Dan Sandler842dd772014-05-15 09:36:47 -04004323 @Override
4324 public Notification buildStyled(Notification wip) {
Christoph Studer4600f9b2014-07-22 22:44:43 +02004325 super.buildStyled(wip);
Bryan Mawhinney6be8de32014-07-18 10:35:12 +01004326 if (wip.category == null) {
4327 wip.category = Notification.CATEGORY_TRANSPORT;
4328 }
Dan Sandler842dd772014-05-15 09:36:47 -04004329 return wip;
4330 }
4331
Christoph Studer4600f9b2014-07-22 22:44:43 +02004332 /**
4333 * @hide
4334 */
4335 @Override
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004336 public RemoteViews makeContentView() {
4337 return makeMediaContentView();
Christoph Studer4600f9b2014-07-22 22:44:43 +02004338 }
4339
4340 /**
4341 * @hide
4342 */
4343 @Override
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004344 public RemoteViews makeBigContentView() {
4345 return makeMediaBigContentView();
Christoph Studer4600f9b2014-07-22 22:44:43 +02004346 }
4347
Selim Cinekcc10bfb2016-02-10 16:24:21 -08004348 /**
4349 * @hide
4350 */
4351 @Override
4352 public RemoteViews makeHeadsUpContentView() {
4353 RemoteViews expanded = makeMediaBigContentView();
4354 return expanded != null ? expanded : makeMediaContentView();
4355 }
4356
Dan Sandler842dd772014-05-15 09:36:47 -04004357 /** @hide */
4358 @Override
4359 public void addExtras(Bundle extras) {
4360 super.addExtras(extras);
4361
4362 if (mToken != null) {
4363 extras.putParcelable(EXTRA_MEDIA_SESSION, mToken);
4364 }
Bryan Mawhinneye191f902014-07-22 12:50:09 +01004365 if (mActionsToShowInCompact != null) {
4366 extras.putIntArray(EXTRA_COMPACT_ACTIONS, mActionsToShowInCompact);
4367 }
Dan Sandler842dd772014-05-15 09:36:47 -04004368 }
4369
Christoph Studer4600f9b2014-07-22 22:44:43 +02004370 /**
4371 * @hide
4372 */
4373 @Override
4374 protected void restoreFromExtras(Bundle extras) {
4375 super.restoreFromExtras(extras);
4376
4377 if (extras.containsKey(EXTRA_MEDIA_SESSION)) {
4378 mToken = extras.getParcelable(EXTRA_MEDIA_SESSION);
4379 }
4380 if (extras.containsKey(EXTRA_COMPACT_ACTIONS)) {
4381 mActionsToShowInCompact = extras.getIntArray(EXTRA_COMPACT_ACTIONS);
4382 }
4383 }
4384
Selim Cinek5bf069a2015-11-10 19:14:27 -05004385 private RemoteViews generateMediaActionButton(Action action, int color) {
Dan Sandler842dd772014-05-15 09:36:47 -04004386 final boolean tombstone = (action.actionIntent == null);
Selim Cinekf33b1112015-07-15 17:45:11 -07004387 RemoteViews button = new BuilderRemoteViews(mBuilder.mContext.getApplicationInfo(),
Alan Viverette3cb07a462014-06-06 14:19:53 -07004388 R.layout.notification_material_media_action);
Dan Sandler68079d52015-07-22 10:45:30 -04004389 button.setImageViewIcon(R.id.action0, action.getIcon());
Selim Cinek5bf069a2015-11-10 19:14:27 -05004390 button.setDrawableParameters(R.id.action0, false, -1, color, PorterDuff.Mode.SRC_ATOP,
4391 -1);
Dan Sandler842dd772014-05-15 09:36:47 -04004392 if (!tombstone) {
4393 button.setOnClickPendingIntent(R.id.action0, action.actionIntent);
4394 }
4395 button.setContentDescription(R.id.action0, action.title);
4396 return button;
4397 }
4398
4399 private RemoteViews makeMediaContentView() {
4400 RemoteViews view = mBuilder.applyStandardTemplate(
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02004401 R.layout.notification_template_material_media, false /* hasProgress */);
Dan Sandler842dd772014-05-15 09:36:47 -04004402
4403 final int numActions = mBuilder.mActions.size();
4404 final int N = mActionsToShowInCompact == null
4405 ? 0
4406 : Math.min(mActionsToShowInCompact.length, MAX_MEDIA_BUTTONS_IN_COMPACT);
4407 if (N > 0) {
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02004408 view.removeAllViews(com.android.internal.R.id.media_actions);
Dan Sandler842dd772014-05-15 09:36:47 -04004409 for (int i = 0; i < N; i++) {
4410 if (i >= numActions) {
4411 throw new IllegalArgumentException(String.format(
4412 "setShowActionsInCompactView: action %d out of bounds (max %d)",
4413 i, numActions - 1));
4414 }
4415
4416 final Action action = mBuilder.mActions.get(mActionsToShowInCompact[i]);
Selim Cinek5bf069a2015-11-10 19:14:27 -05004417 final RemoteViews button = generateMediaActionButton(action,
4418 mBuilder.resolveColor());
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02004419 view.addView(com.android.internal.R.id.media_actions, button);
Dan Sandler842dd772014-05-15 09:36:47 -04004420 }
4421 }
Selim Cinekfdc738f2016-01-27 20:04:27 -08004422 handleImage(view);
4423 // handle the content margin
Selim Cinek247fa012016-02-18 09:50:48 -08004424 int endMargin = mBuilder.mContext.getResources().getDimensionPixelSize(
4425 R.dimen.notification_content_margin_end);;
Selim Cinekfdc738f2016-01-27 20:04:27 -08004426 if (mBuilder.mN.mLargeIcon != null) {
Selim Cinek247fa012016-02-18 09:50:48 -08004427 endMargin += mBuilder.mContext.getResources().getDimensionPixelSize(
4428 R.dimen.notification_content_picture_margin);
Selim Cinekfdc738f2016-01-27 20:04:27 -08004429 }
4430 view.setViewLayoutMarginEnd(R.id.notification_main_column, endMargin);
Dan Sandler842dd772014-05-15 09:36:47 -04004431 return view;
4432 }
4433
4434 private RemoteViews makeMediaBigContentView() {
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02004435 final int actionCount = Math.min(mBuilder.mActions.size(), MAX_MEDIA_BUTTONS);
Selim Cinekcc10bfb2016-02-10 16:24:21 -08004436 // Dont add an expanded view if there is no more content to be revealed
4437 int actionsInCompact = mActionsToShowInCompact == null
4438 ? 0
4439 : Math.min(mActionsToShowInCompact.length, MAX_MEDIA_BUTTONS_IN_COMPACT);
4440 if (mBuilder.mN.mLargeIcon == null && actionCount <= actionsInCompact) {
4441 return null;
4442 }
Selim Cinek5bf069a2015-11-10 19:14:27 -05004443 RemoteViews big = mBuilder.applyStandardTemplate(
4444 R.layout.notification_template_material_big_media,
4445 false);
Dan Sandler842dd772014-05-15 09:36:47 -04004446
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02004447 if (actionCount > 0) {
4448 big.removeAllViews(com.android.internal.R.id.media_actions);
4449 for (int i = 0; i < actionCount; i++) {
Selim Cinek5bf069a2015-11-10 19:14:27 -05004450 final RemoteViews button = generateMediaActionButton(mBuilder.mActions.get(i),
4451 mBuilder.resolveColor());
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02004452 big.addView(com.android.internal.R.id.media_actions, button);
Dan Sandler842dd772014-05-15 09:36:47 -04004453 }
4454 }
Selim Cinek5bf069a2015-11-10 19:14:27 -05004455 handleImage(big);
Dan Sandler842dd772014-05-15 09:36:47 -04004456 return big;
4457 }
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02004458
Selim Cinek5bf069a2015-11-10 19:14:27 -05004459 private void handleImage(RemoteViews contentView) {
4460 if (mBuilder.mN.mLargeIcon != null) {
4461 contentView.setViewLayoutMarginEnd(R.id.line1, 0);
Selim Cinekc848c3a2016-01-13 15:27:30 -08004462 contentView.setViewLayoutMarginEnd(R.id.text, 0);
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02004463 }
4464 }
4465
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02004466 /**
4467 * @hide
4468 */
4469 @Override
4470 protected boolean hasProgress() {
4471 return false;
4472 }
Dan Sandler842dd772014-05-15 09:36:47 -04004473 }
Griff Hazen61a9e862014-05-22 16:05:19 -07004474
Selim Cinek593610c2016-02-16 18:42:57 -08004475 /**
4476 * Notification style for custom views that are decorated by the system
4477 *
4478 * <p>Instead of providing a notification that is completely custom, a developer can set this
4479 * style and still obtain system decorations like the notification header with the expand
4480 * affordance and actions.
4481 *
4482 * <p>Use {@link android.app.Notification.Builder#setCustomContentView(RemoteViews)},
4483 * {@link android.app.Notification.Builder#setCustomBigContentView(RemoteViews)} and
4484 * {@link android.app.Notification.Builder#setCustomHeadsUpContentView(RemoteViews)} to set the
4485 * corresponding custom views to display.
4486 *
4487 * To use this style with your Notification, feed it to
4488 * {@link Notification.Builder#setStyle(android.app.Notification.Style)} like so:
4489 * <pre class="prettyprint">
4490 * Notification noti = new Notification.Builder()
4491 * .setSmallIcon(R.drawable.ic_stat_player)
4492 * .setLargeIcon(albumArtBitmap))
4493 * .setCustomContentView(contentView);
4494 * .setStyle(<b>new Notification.DecoratedCustomViewStyle()</b>)
4495 * .build();
4496 * </pre>
4497 */
4498 public static class DecoratedCustomViewStyle extends Style {
4499
4500 public DecoratedCustomViewStyle() {
4501 }
4502
4503 public DecoratedCustomViewStyle(Builder builder) {
4504 setBuilder(builder);
4505 }
4506
4507 /**
4508 * @hide
4509 */
4510 public boolean displayCustomViewInline() {
4511 return true;
4512 }
4513
4514 /**
4515 * @hide
4516 */
4517 @Override
4518 public RemoteViews makeContentView() {
4519 return makeStandardTemplateWithCustomContent(mBuilder.mN.contentView);
4520 }
4521
4522 /**
4523 * @hide
4524 */
4525 @Override
4526 public RemoteViews makeBigContentView() {
4527 return makeDecoratedBigContentView();
4528 }
4529
4530 /**
4531 * @hide
4532 */
4533 @Override
4534 public RemoteViews makeHeadsUpContentView() {
4535 return makeDecoratedHeadsUpContentView();
4536 }
4537
Selim Cinek593610c2016-02-16 18:42:57 -08004538 private RemoteViews makeDecoratedHeadsUpContentView() {
4539 RemoteViews headsUpContentView = mBuilder.mN.headsUpContentView == null
4540 ? mBuilder.mN.contentView
4541 : mBuilder.mN.headsUpContentView;
4542 if (mBuilder.mActions.size() == 0) {
4543 return makeStandardTemplateWithCustomContent(headsUpContentView);
4544 }
4545 RemoteViews remoteViews = mBuilder.applyStandardTemplateWithActions(
4546 mBuilder.getBigBaseLayoutResource());
Selim Cinek247fa012016-02-18 09:50:48 -08004547 buildIntoRemoteViewContent(remoteViews, headsUpContentView);
Selim Cinek593610c2016-02-16 18:42:57 -08004548 return remoteViews;
4549 }
4550
Selim Cinek593610c2016-02-16 18:42:57 -08004551 private RemoteViews makeStandardTemplateWithCustomContent(RemoteViews customContent) {
4552 RemoteViews remoteViews = mBuilder.applyStandardTemplate(
4553 mBuilder.getBaseLayoutResource());
Selim Cinek247fa012016-02-18 09:50:48 -08004554 buildIntoRemoteViewContent(remoteViews, customContent);
Selim Cinek593610c2016-02-16 18:42:57 -08004555 return remoteViews;
4556 }
4557
Selim Cinek593610c2016-02-16 18:42:57 -08004558 private RemoteViews makeDecoratedBigContentView() {
4559 RemoteViews bigContentView = mBuilder.mN.bigContentView == null
4560 ? mBuilder.mN.contentView
4561 : mBuilder.mN.bigContentView;
4562 if (mBuilder.mActions.size() == 0) {
4563 return makeStandardTemplateWithCustomContent(bigContentView);
4564 }
4565 RemoteViews remoteViews = mBuilder.applyStandardTemplateWithActions(
4566 mBuilder.getBigBaseLayoutResource());
Selim Cinek247fa012016-02-18 09:50:48 -08004567 buildIntoRemoteViewContent(remoteViews, bigContentView);
Selim Cinek593610c2016-02-16 18:42:57 -08004568 return remoteViews;
4569 }
Selim Cinek247fa012016-02-18 09:50:48 -08004570
4571 private void buildIntoRemoteViewContent(RemoteViews remoteViews,
4572 RemoteViews customContent) {
4573 remoteViews.removeAllViews(R.id.notification_main_column);
Adrian Roos5081c0d2016-02-26 16:04:19 -08004574 // Need to clone customContent before adding, because otherwise it can no longer be
4575 // parceled independently of remoteViews.
4576 if (customContent != null) {
4577 customContent = customContent.clone();
4578 }
Selim Cinek247fa012016-02-18 09:50:48 -08004579 remoteViews.addView(R.id.notification_main_column, customContent);
4580 // also update the end margin if there is an image
4581 int endMargin = mBuilder.mContext.getResources().getDimensionPixelSize(
4582 R.dimen.notification_content_margin_end);
4583 if (mBuilder.mN.mLargeIcon != null) {
4584 endMargin += mBuilder.mContext.getResources().getDimensionPixelSize(
4585 R.dimen.notification_content_picture_margin);
4586 }
4587 remoteViews.setViewLayoutMarginEnd(R.id.notification_main_column, endMargin);
4588 }
Selim Cinek593610c2016-02-16 18:42:57 -08004589 }
4590
Selim Cinek03eb3b72016-02-18 10:39:45 -08004591 /**
4592 * Notification style for media custom views that are decorated by the system
4593 *
4594 * <p>Instead of providing a media notification that is completely custom, a developer can set
4595 * this style and still obtain system decorations like the notification header with the expand
4596 * affordance and actions.
4597 *
4598 * <p>Use {@link android.app.Notification.Builder#setCustomContentView(RemoteViews)},
4599 * {@link android.app.Notification.Builder#setCustomBigContentView(RemoteViews)} and
4600 * {@link android.app.Notification.Builder#setCustomHeadsUpContentView(RemoteViews)} to set the
4601 * corresponding custom views to display.
4602 *
4603 * To use this style with your Notification, feed it to
4604 * {@link Notification.Builder#setStyle(android.app.Notification.Style)} like so:
4605 * <pre class="prettyprint">
4606 * Notification noti = new Notification.Builder()
4607 * .setSmallIcon(R.drawable.ic_stat_player)
4608 * .setLargeIcon(albumArtBitmap))
4609 * .setCustomContentView(contentView);
4610 * .setStyle(<b>new Notification.DecoratedMediaCustomViewStyle()</b>
4611 * .setMediaSession(mySession))
4612 * .build();
4613 * </pre>
4614 *
4615 * @see android.app.Notification.DecoratedCustomViewStyle
4616 * @see android.app.Notification.MediaStyle
4617 */
4618 public static class DecoratedMediaCustomViewStyle extends MediaStyle {
4619
4620 public DecoratedMediaCustomViewStyle() {
4621 }
4622
4623 public DecoratedMediaCustomViewStyle(Builder builder) {
4624 setBuilder(builder);
4625 }
4626
4627 /**
4628 * @hide
4629 */
4630 public boolean displayCustomViewInline() {
4631 return true;
4632 }
4633
4634 /**
4635 * @hide
4636 */
4637 @Override
4638 public RemoteViews makeContentView() {
4639 RemoteViews remoteViews = super.makeContentView();
4640 return buildIntoRemoteView(remoteViews, R.id.notification_content_container,
4641 mBuilder.mN.contentView);
4642 }
4643
4644 /**
4645 * @hide
4646 */
4647 @Override
4648 public RemoteViews makeBigContentView() {
4649 RemoteViews customRemoteView = mBuilder.mN.bigContentView != null
4650 ? mBuilder.mN.bigContentView
4651 : mBuilder.mN.contentView;
4652 return makeBigContentViewWithCustomContent(customRemoteView);
4653 }
4654
4655 private RemoteViews makeBigContentViewWithCustomContent(RemoteViews customRemoteView) {
4656 RemoteViews remoteViews = super.makeBigContentView();
4657 if (remoteViews != null) {
4658 return buildIntoRemoteView(remoteViews, R.id.notification_main_column,
4659 customRemoteView);
4660 } else if (customRemoteView != mBuilder.mN.contentView){
4661 remoteViews = super.makeContentView();
4662 return buildIntoRemoteView(remoteViews, R.id.notification_content_container,
4663 customRemoteView);
4664 } else {
4665 return null;
4666 }
4667 }
4668
4669 /**
4670 * @hide
4671 */
4672 @Override
4673 public RemoteViews makeHeadsUpContentView() {
4674 RemoteViews customRemoteView = mBuilder.mN.headsUpContentView != null
4675 ? mBuilder.mN.headsUpContentView
4676 : mBuilder.mN.contentView;
4677 return makeBigContentViewWithCustomContent(customRemoteView);
4678 }
4679
4680 private RemoteViews buildIntoRemoteView(RemoteViews remoteViews, int id,
4681 RemoteViews customContent) {
4682 remoteViews.removeAllViews(id);
Adrian Roos5081c0d2016-02-26 16:04:19 -08004683 // Need to clone customContent before adding, because otherwise it can no longer be
4684 // parceled independently of remoteViews.
4685 if (customContent != null) {
4686 customContent = customContent.clone();
4687 }
Selim Cinek03eb3b72016-02-18 10:39:45 -08004688 remoteViews.addView(id, customContent);
4689 return remoteViews;
4690 }
4691 }
4692
Christoph Studer4600f9b2014-07-22 22:44:43 +02004693 // When adding a new Style subclass here, don't forget to update
4694 // Builder.getNotificationStyleClass.
4695
Griff Hazen61a9e862014-05-22 16:05:19 -07004696 /**
4697 * Extender interface for use with {@link Builder#extend}. Extenders may be used to add
4698 * metadata or change options on a notification builder.
4699 */
4700 public interface Extender {
4701 /**
4702 * Apply this extender to a notification builder.
4703 * @param builder the builder to be modified.
4704 * @return the build object for chaining.
4705 */
4706 public Builder extend(Builder builder);
4707 }
4708
4709 /**
4710 * Helper class to add wearable extensions to notifications.
4711 * <p class="note"> See
4712 * <a href="{@docRoot}wear/notifications/creating.html">Creating Notifications
4713 * for Android Wear</a> for more information on how to use this class.
4714 * <p>
4715 * To create a notification with wearable extensions:
4716 * <ol>
4717 * <li>Create a {@link android.app.Notification.Builder}, setting any desired
4718 * properties.
4719 * <li>Create a {@link android.app.Notification.WearableExtender}.
4720 * <li>Set wearable-specific properties using the
4721 * {@code add} and {@code set} methods of {@link android.app.Notification.WearableExtender}.
4722 * <li>Call {@link android.app.Notification.Builder#extend} to apply the extensions to a
4723 * notification.
4724 * <li>Post the notification to the notification system with the
4725 * {@code NotificationManager.notify(...)} methods.
4726 * </ol>
4727 *
4728 * <pre class="prettyprint">
4729 * Notification notif = new Notification.Builder(mContext)
4730 * .setContentTitle(&quot;New mail from &quot; + sender.toString())
4731 * .setContentText(subject)
4732 * .setSmallIcon(R.drawable.new_mail)
4733 * .extend(new Notification.WearableExtender()
4734 * .setContentIcon(R.drawable.new_mail))
4735 * .build();
4736 * NotificationManager notificationManger =
4737 * (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
4738 * notificationManger.notify(0, notif);</pre>
4739 *
4740 * <p>Wearable extensions can be accessed on an existing notification by using the
4741 * {@code WearableExtender(Notification)} constructor,
4742 * and then using the {@code get} methods to access values.
4743 *
4744 * <pre class="prettyprint">
4745 * Notification.WearableExtender wearableExtender = new Notification.WearableExtender(
4746 * notification);
Griff Hazen14f57992014-05-26 09:07:14 -07004747 * List&lt;Notification&gt; pages = wearableExtender.getPages();</pre>
Griff Hazen61a9e862014-05-22 16:05:19 -07004748 */
4749 public static final class WearableExtender implements Extender {
4750 /**
4751 * Sentinel value for an action index that is unset.
4752 */
4753 public static final int UNSET_ACTION_INDEX = -1;
4754
4755 /**
4756 * Size value for use with {@link #setCustomSizePreset} to show this notification with
4757 * default sizing.
4758 * <p>For custom display notifications created using {@link #setDisplayIntent},
Paul Soulosaa4f4bf2015-08-04 11:59:45 -07004759 * the default is {@link #SIZE_MEDIUM}. All other notifications size automatically based
Griff Hazen61a9e862014-05-22 16:05:19 -07004760 * on their content.
4761 */
4762 public static final int SIZE_DEFAULT = 0;
4763
4764 /**
4765 * Size value for use with {@link #setCustomSizePreset} to show this notification
4766 * with an extra small size.
4767 * <p>This value is only applicable for custom display notifications created using
4768 * {@link #setDisplayIntent}.
4769 */
4770 public static final int SIZE_XSMALL = 1;
4771
4772 /**
4773 * Size value for use with {@link #setCustomSizePreset} to show this notification
4774 * with a small size.
4775 * <p>This value is only applicable for custom display notifications created using
4776 * {@link #setDisplayIntent}.
4777 */
4778 public static final int SIZE_SMALL = 2;
4779
4780 /**
4781 * Size value for use with {@link #setCustomSizePreset} to show this notification
4782 * with a medium size.
4783 * <p>This value is only applicable for custom display notifications created using
4784 * {@link #setDisplayIntent}.
4785 */
4786 public static final int SIZE_MEDIUM = 3;
4787
4788 /**
4789 * Size value for use with {@link #setCustomSizePreset} to show this notification
4790 * with a large size.
4791 * <p>This value is only applicable for custom display notifications created using
4792 * {@link #setDisplayIntent}.
4793 */
4794 public static final int SIZE_LARGE = 4;
4795
Griff Hazend5f11f92014-05-27 15:40:09 -07004796 /**
4797 * Size value for use with {@link #setCustomSizePreset} to show this notification
4798 * full screen.
4799 * <p>This value is only applicable for custom display notifications created using
4800 * {@link #setDisplayIntent}.
4801 */
4802 public static final int SIZE_FULL_SCREEN = 5;
4803
Griff Hazen5f2edfc2014-09-29 16:28:44 -07004804 /**
4805 * Sentinel value for use with {@link #setHintScreenTimeout} to keep the screen on for a
4806 * short amount of time when this notification is displayed on the screen. This
4807 * is the default value.
4808 */
4809 public static final int SCREEN_TIMEOUT_SHORT = 0;
4810
4811 /**
4812 * Sentinel value for use with {@link #setHintScreenTimeout} to keep the screen on
4813 * for a longer amount of time when this notification is displayed on the screen.
4814 */
4815 public static final int SCREEN_TIMEOUT_LONG = -1;
4816
Griff Hazen61a9e862014-05-22 16:05:19 -07004817 /** Notification extra which contains wearable extensions */
4818 private static final String EXTRA_WEARABLE_EXTENSIONS = "android.wearable.EXTENSIONS";
4819
Pete Gastaf6781d2014-10-07 15:17:05 -04004820 // Keys within EXTRA_WEARABLE_EXTENSIONS for wearable options.
Griff Hazen61a9e862014-05-22 16:05:19 -07004821 private static final String KEY_ACTIONS = "actions";
4822 private static final String KEY_FLAGS = "flags";
4823 private static final String KEY_DISPLAY_INTENT = "displayIntent";
4824 private static final String KEY_PAGES = "pages";
4825 private static final String KEY_BACKGROUND = "background";
4826 private static final String KEY_CONTENT_ICON = "contentIcon";
4827 private static final String KEY_CONTENT_ICON_GRAVITY = "contentIconGravity";
4828 private static final String KEY_CONTENT_ACTION_INDEX = "contentActionIndex";
4829 private static final String KEY_CUSTOM_SIZE_PRESET = "customSizePreset";
4830 private static final String KEY_CUSTOM_CONTENT_HEIGHT = "customContentHeight";
4831 private static final String KEY_GRAVITY = "gravity";
Griff Hazen5f2edfc2014-09-29 16:28:44 -07004832 private static final String KEY_HINT_SCREEN_TIMEOUT = "hintScreenTimeout";
Griff Hazen61a9e862014-05-22 16:05:19 -07004833
4834 // Flags bitwise-ored to mFlags
4835 private static final int FLAG_CONTENT_INTENT_AVAILABLE_OFFLINE = 0x1;
4836 private static final int FLAG_HINT_HIDE_ICON = 1 << 1;
4837 private static final int FLAG_HINT_SHOW_BACKGROUND_ONLY = 1 << 2;
4838 private static final int FLAG_START_SCROLL_BOTTOM = 1 << 3;
Griff Hazen5f2edfc2014-09-29 16:28:44 -07004839 private static final int FLAG_HINT_AVOID_BACKGROUND_CLIPPING = 1 << 4;
Griff Hazen61a9e862014-05-22 16:05:19 -07004840
4841 // Default value for flags integer
4842 private static final int DEFAULT_FLAGS = FLAG_CONTENT_INTENT_AVAILABLE_OFFLINE;
4843
4844 private static final int DEFAULT_CONTENT_ICON_GRAVITY = Gravity.END;
4845 private static final int DEFAULT_GRAVITY = Gravity.BOTTOM;
4846
4847 private ArrayList<Action> mActions = new ArrayList<Action>();
4848 private int mFlags = DEFAULT_FLAGS;
4849 private PendingIntent mDisplayIntent;
4850 private ArrayList<Notification> mPages = new ArrayList<Notification>();
4851 private Bitmap mBackground;
4852 private int mContentIcon;
4853 private int mContentIconGravity = DEFAULT_CONTENT_ICON_GRAVITY;
4854 private int mContentActionIndex = UNSET_ACTION_INDEX;
4855 private int mCustomSizePreset = SIZE_DEFAULT;
4856 private int mCustomContentHeight;
4857 private int mGravity = DEFAULT_GRAVITY;
Griff Hazen5f2edfc2014-09-29 16:28:44 -07004858 private int mHintScreenTimeout;
Griff Hazen61a9e862014-05-22 16:05:19 -07004859
4860 /**
4861 * Create a {@link android.app.Notification.WearableExtender} with default
4862 * options.
4863 */
4864 public WearableExtender() {
4865 }
4866
4867 public WearableExtender(Notification notif) {
4868 Bundle wearableBundle = notif.extras.getBundle(EXTRA_WEARABLE_EXTENSIONS);
4869 if (wearableBundle != null) {
4870 List<Action> actions = wearableBundle.getParcelableArrayList(KEY_ACTIONS);
4871 if (actions != null) {
4872 mActions.addAll(actions);
4873 }
4874
4875 mFlags = wearableBundle.getInt(KEY_FLAGS, DEFAULT_FLAGS);
4876 mDisplayIntent = wearableBundle.getParcelable(KEY_DISPLAY_INTENT);
4877
4878 Notification[] pages = getNotificationArrayFromBundle(
4879 wearableBundle, KEY_PAGES);
4880 if (pages != null) {
4881 Collections.addAll(mPages, pages);
4882 }
4883
4884 mBackground = wearableBundle.getParcelable(KEY_BACKGROUND);
4885 mContentIcon = wearableBundle.getInt(KEY_CONTENT_ICON);
4886 mContentIconGravity = wearableBundle.getInt(KEY_CONTENT_ICON_GRAVITY,
4887 DEFAULT_CONTENT_ICON_GRAVITY);
4888 mContentActionIndex = wearableBundle.getInt(KEY_CONTENT_ACTION_INDEX,
4889 UNSET_ACTION_INDEX);
4890 mCustomSizePreset = wearableBundle.getInt(KEY_CUSTOM_SIZE_PRESET,
4891 SIZE_DEFAULT);
4892 mCustomContentHeight = wearableBundle.getInt(KEY_CUSTOM_CONTENT_HEIGHT);
4893 mGravity = wearableBundle.getInt(KEY_GRAVITY, DEFAULT_GRAVITY);
Griff Hazen5f2edfc2014-09-29 16:28:44 -07004894 mHintScreenTimeout = wearableBundle.getInt(KEY_HINT_SCREEN_TIMEOUT);
Griff Hazen61a9e862014-05-22 16:05:19 -07004895 }
4896 }
4897
4898 /**
4899 * Apply wearable extensions to a notification that is being built. This is typically
4900 * called by the {@link android.app.Notification.Builder#extend} method of
4901 * {@link android.app.Notification.Builder}.
4902 */
4903 @Override
4904 public Notification.Builder extend(Notification.Builder builder) {
4905 Bundle wearableBundle = new Bundle();
4906
4907 if (!mActions.isEmpty()) {
4908 wearableBundle.putParcelableArrayList(KEY_ACTIONS, mActions);
4909 }
4910 if (mFlags != DEFAULT_FLAGS) {
4911 wearableBundle.putInt(KEY_FLAGS, mFlags);
4912 }
4913 if (mDisplayIntent != null) {
4914 wearableBundle.putParcelable(KEY_DISPLAY_INTENT, mDisplayIntent);
4915 }
4916 if (!mPages.isEmpty()) {
4917 wearableBundle.putParcelableArray(KEY_PAGES, mPages.toArray(
4918 new Notification[mPages.size()]));
4919 }
4920 if (mBackground != null) {
4921 wearableBundle.putParcelable(KEY_BACKGROUND, mBackground);
4922 }
4923 if (mContentIcon != 0) {
4924 wearableBundle.putInt(KEY_CONTENT_ICON, mContentIcon);
4925 }
4926 if (mContentIconGravity != DEFAULT_CONTENT_ICON_GRAVITY) {
4927 wearableBundle.putInt(KEY_CONTENT_ICON_GRAVITY, mContentIconGravity);
4928 }
4929 if (mContentActionIndex != UNSET_ACTION_INDEX) {
4930 wearableBundle.putInt(KEY_CONTENT_ACTION_INDEX,
4931 mContentActionIndex);
4932 }
4933 if (mCustomSizePreset != SIZE_DEFAULT) {
4934 wearableBundle.putInt(KEY_CUSTOM_SIZE_PRESET, mCustomSizePreset);
4935 }
4936 if (mCustomContentHeight != 0) {
4937 wearableBundle.putInt(KEY_CUSTOM_CONTENT_HEIGHT, mCustomContentHeight);
4938 }
4939 if (mGravity != DEFAULT_GRAVITY) {
4940 wearableBundle.putInt(KEY_GRAVITY, mGravity);
4941 }
Griff Hazen5f2edfc2014-09-29 16:28:44 -07004942 if (mHintScreenTimeout != 0) {
4943 wearableBundle.putInt(KEY_HINT_SCREEN_TIMEOUT, mHintScreenTimeout);
4944 }
Griff Hazen61a9e862014-05-22 16:05:19 -07004945
4946 builder.getExtras().putBundle(EXTRA_WEARABLE_EXTENSIONS, wearableBundle);
4947 return builder;
4948 }
4949
4950 @Override
4951 public WearableExtender clone() {
4952 WearableExtender that = new WearableExtender();
4953 that.mActions = new ArrayList<Action>(this.mActions);
4954 that.mFlags = this.mFlags;
4955 that.mDisplayIntent = this.mDisplayIntent;
4956 that.mPages = new ArrayList<Notification>(this.mPages);
4957 that.mBackground = this.mBackground;
4958 that.mContentIcon = this.mContentIcon;
4959 that.mContentIconGravity = this.mContentIconGravity;
4960 that.mContentActionIndex = this.mContentActionIndex;
4961 that.mCustomSizePreset = this.mCustomSizePreset;
4962 that.mCustomContentHeight = this.mCustomContentHeight;
4963 that.mGravity = this.mGravity;
Griff Hazen5f2edfc2014-09-29 16:28:44 -07004964 that.mHintScreenTimeout = this.mHintScreenTimeout;
Griff Hazen61a9e862014-05-22 16:05:19 -07004965 return that;
4966 }
4967
4968 /**
4969 * Add a wearable action to this notification.
4970 *
4971 * <p>When wearable actions are added using this method, the set of actions that
4972 * show on a wearable device splits from devices that only show actions added
4973 * using {@link android.app.Notification.Builder#addAction}. This allows for customization
4974 * of which actions display on different devices.
4975 *
4976 * @param action the action to add to this notification
4977 * @return this object for method chaining
4978 * @see android.app.Notification.Action
4979 */
4980 public WearableExtender addAction(Action action) {
4981 mActions.add(action);
4982 return this;
4983 }
4984
4985 /**
4986 * Adds wearable actions to this notification.
4987 *
4988 * <p>When wearable actions are added using this method, the set of actions that
4989 * show on a wearable device splits from devices that only show actions added
4990 * using {@link android.app.Notification.Builder#addAction}. This allows for customization
4991 * of which actions display on different devices.
4992 *
4993 * @param actions the actions to add to this notification
4994 * @return this object for method chaining
4995 * @see android.app.Notification.Action
4996 */
4997 public WearableExtender addActions(List<Action> actions) {
4998 mActions.addAll(actions);
4999 return this;
5000 }
5001
5002 /**
5003 * Clear all wearable actions present on this builder.
5004 * @return this object for method chaining.
5005 * @see #addAction
5006 */
5007 public WearableExtender clearActions() {
5008 mActions.clear();
5009 return this;
5010 }
5011
5012 /**
5013 * Get the wearable actions present on this notification.
5014 */
5015 public List<Action> getActions() {
5016 return mActions;
5017 }
5018
5019 /**
5020 * Set an intent to launch inside of an activity view when displaying
Griff Hazen14f57992014-05-26 09:07:14 -07005021 * this notification. The {@link PendingIntent} provided should be for an activity.
5022 *
5023 * <pre class="prettyprint">
5024 * Intent displayIntent = new Intent(context, MyDisplayActivity.class);
5025 * PendingIntent displayPendingIntent = PendingIntent.getActivity(context,
5026 * 0, displayIntent, PendingIntent.FLAG_UPDATE_CURRENT);
5027 * Notification notif = new Notification.Builder(context)
5028 * .extend(new Notification.WearableExtender()
5029 * .setDisplayIntent(displayPendingIntent)
5030 * .setCustomSizePreset(Notification.WearableExtender.SIZE_MEDIUM))
5031 * .build();</pre>
5032 *
5033 * <p>The activity to launch needs to allow embedding, must be exported, and
Griff Hazen831ca9d2014-06-17 00:38:38 -07005034 * should have an empty task affinity. It is also recommended to use the device
5035 * default light theme.
Griff Hazen14f57992014-05-26 09:07:14 -07005036 *
5037 * <p>Example AndroidManifest.xml entry:
5038 * <pre class="prettyprint">
5039 * &lt;activity android:name=&quot;com.example.MyDisplayActivity&quot;
5040 * android:exported=&quot;true&quot;
5041 * android:allowEmbedded=&quot;true&quot;
Griff Hazen831ca9d2014-06-17 00:38:38 -07005042 * android:taskAffinity=&quot;&quot;
5043 * android:theme=&quot;@android:style/Theme.DeviceDefault.Light&quot; /&gt;</pre>
Griff Hazen61a9e862014-05-22 16:05:19 -07005044 *
5045 * @param intent the {@link PendingIntent} for an activity
5046 * @return this object for method chaining
5047 * @see android.app.Notification.WearableExtender#getDisplayIntent
5048 */
5049 public WearableExtender setDisplayIntent(PendingIntent intent) {
5050 mDisplayIntent = intent;
5051 return this;
5052 }
5053
5054 /**
5055 * Get the intent to launch inside of an activity view when displaying this
5056 * notification. This {@code PendingIntent} should be for an activity.
5057 */
5058 public PendingIntent getDisplayIntent() {
5059 return mDisplayIntent;
5060 }
5061
5062 /**
5063 * Add an additional page 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 page the notification to add as another page
5069 * @return this object for method chaining
5070 * @see android.app.Notification.WearableExtender#getPages
5071 */
5072 public WearableExtender addPage(Notification page) {
5073 mPages.add(page);
5074 return this;
5075 }
5076
5077 /**
5078 * Add additional pages of content to display with this notification. The current
5079 * notification forms the first page, and pages added using this function form
5080 * subsequent pages. This field can be used to separate a notification into multiple
5081 * sections.
5082 *
5083 * @param pages a list of notifications
5084 * @return this object for method chaining
5085 * @see android.app.Notification.WearableExtender#getPages
5086 */
5087 public WearableExtender addPages(List<Notification> pages) {
5088 mPages.addAll(pages);
5089 return this;
5090 }
5091
5092 /**
5093 * Clear all additional pages present on this builder.
5094 * @return this object for method chaining.
5095 * @see #addPage
5096 */
5097 public WearableExtender clearPages() {
5098 mPages.clear();
5099 return this;
5100 }
5101
5102 /**
5103 * Get the array of additional pages of content for displaying this notification. The
5104 * current notification forms the first page, and elements within this array form
5105 * subsequent pages. This field can be used to separate a notification into multiple
5106 * sections.
5107 * @return the pages for this notification
5108 */
5109 public List<Notification> getPages() {
5110 return mPages;
5111 }
5112
5113 /**
5114 * Set a background image to be displayed behind the notification content.
5115 * Contrary to the {@link android.app.Notification.BigPictureStyle}, this background
5116 * will work with any notification style.
5117 *
5118 * @param background the background bitmap
5119 * @return this object for method chaining
5120 * @see android.app.Notification.WearableExtender#getBackground
5121 */
5122 public WearableExtender setBackground(Bitmap background) {
5123 mBackground = background;
5124 return this;
5125 }
5126
5127 /**
5128 * Get a background image to be displayed behind the notification content.
5129 * Contrary to the {@link android.app.Notification.BigPictureStyle}, this background
5130 * will work with any notification style.
5131 *
5132 * @return the background image
5133 * @see android.app.Notification.WearableExtender#setBackground
5134 */
5135 public Bitmap getBackground() {
5136 return mBackground;
5137 }
5138
5139 /**
5140 * Set an icon that goes with the content of this notification.
5141 */
5142 public WearableExtender setContentIcon(int icon) {
5143 mContentIcon = icon;
5144 return this;
5145 }
5146
5147 /**
5148 * Get an icon that goes with the content of this notification.
5149 */
5150 public int getContentIcon() {
5151 return mContentIcon;
5152 }
5153
5154 /**
5155 * Set the gravity that the content icon should have within the notification display.
5156 * Supported values include {@link android.view.Gravity#START} and
5157 * {@link android.view.Gravity#END}. The default value is {@link android.view.Gravity#END}.
5158 * @see #setContentIcon
5159 */
5160 public WearableExtender setContentIconGravity(int contentIconGravity) {
5161 mContentIconGravity = contentIconGravity;
5162 return this;
5163 }
5164
5165 /**
5166 * Get the gravity that the content icon should have within the notification display.
5167 * Supported values include {@link android.view.Gravity#START} and
5168 * {@link android.view.Gravity#END}. The default value is {@link android.view.Gravity#END}.
5169 * @see #getContentIcon
5170 */
5171 public int getContentIconGravity() {
5172 return mContentIconGravity;
5173 }
5174
5175 /**
5176 * Set an action from this notification's actions to be clickable with the content of
Griff Hazen14f57992014-05-26 09:07:14 -07005177 * this notification. This action will no longer display separately from the
5178 * notification's content.
5179 *
Griff Hazenca48d352014-05-28 22:37:13 -07005180 * <p>For notifications with multiple pages, child pages can also have content actions
Griff Hazen14f57992014-05-26 09:07:14 -07005181 * set, although the list of available actions comes from the main notification and not
5182 * from the child page's notification.
5183 *
5184 * @param actionIndex The index of the action to hoist onto the current notification page.
5185 * If wearable actions were added to the main notification, this index
5186 * will apply to that list, otherwise it will apply to the regular
5187 * actions list.
Griff Hazen61a9e862014-05-22 16:05:19 -07005188 */
5189 public WearableExtender setContentAction(int actionIndex) {
5190 mContentActionIndex = actionIndex;
5191 return this;
5192 }
5193
5194 /**
Griff Hazenca48d352014-05-28 22:37:13 -07005195 * Get the index of the notification action, if any, that was specified as being clickable
5196 * with the content of this notification. This action will no longer display separately
Griff Hazen14f57992014-05-26 09:07:14 -07005197 * from the notification's content.
Griff Hazen61a9e862014-05-22 16:05:19 -07005198 *
Griff Hazenca48d352014-05-28 22:37:13 -07005199 * <p>For notifications with multiple pages, child pages can also have content actions
Griff Hazen14f57992014-05-26 09:07:14 -07005200 * set, although the list of available actions comes from the main notification and not
5201 * from the child page's notification.
5202 *
5203 * <p>If wearable specific actions were added to the main notification, this index will
5204 * apply to that list, otherwise it will apply to the regular actions list.
Griff Hazenca48d352014-05-28 22:37:13 -07005205 *
5206 * @return the action index or {@link #UNSET_ACTION_INDEX} if no action was selected.
Griff Hazen61a9e862014-05-22 16:05:19 -07005207 */
5208 public int getContentAction() {
5209 return mContentActionIndex;
5210 }
5211
5212 /**
5213 * Set the gravity that this notification should have within the available viewport space.
5214 * Supported values include {@link android.view.Gravity#TOP},
5215 * {@link android.view.Gravity#CENTER_VERTICAL} and {@link android.view.Gravity#BOTTOM}.
5216 * The default value is {@link android.view.Gravity#BOTTOM}.
5217 */
5218 public WearableExtender setGravity(int gravity) {
5219 mGravity = gravity;
5220 return this;
5221 }
5222
5223 /**
5224 * Get the gravity that this notification should have within the available viewport space.
5225 * Supported values include {@link android.view.Gravity#TOP},
5226 * {@link android.view.Gravity#CENTER_VERTICAL} and {@link android.view.Gravity#BOTTOM}.
5227 * The default value is {@link android.view.Gravity#BOTTOM}.
5228 */
5229 public int getGravity() {
5230 return mGravity;
5231 }
5232
5233 /**
5234 * Set the custom size preset for the display of this notification out of the available
5235 * presets found in {@link android.app.Notification.WearableExtender}, e.g.
5236 * {@link #SIZE_LARGE}.
5237 * <p>Some custom size presets are only applicable for custom display notifications created
5238 * using {@link android.app.Notification.WearableExtender#setDisplayIntent}. Check the
5239 * documentation for the preset in question. See also
5240 * {@link #setCustomContentHeight} and {@link #getCustomSizePreset}.
5241 */
5242 public WearableExtender setCustomSizePreset(int sizePreset) {
5243 mCustomSizePreset = sizePreset;
5244 return this;
5245 }
5246
5247 /**
5248 * Get the custom size preset for the display of this notification out of the available
5249 * presets found in {@link android.app.Notification.WearableExtender}, e.g.
5250 * {@link #SIZE_LARGE}.
5251 * <p>Some custom size presets are only applicable for custom display notifications created
5252 * using {@link #setDisplayIntent}. Check the documentation for the preset in question.
5253 * See also {@link #setCustomContentHeight} and {@link #setCustomSizePreset}.
5254 */
5255 public int getCustomSizePreset() {
5256 return mCustomSizePreset;
5257 }
5258
5259 /**
5260 * Set the custom height in pixels for the display of this notification's content.
5261 * <p>This option is only available for custom display notifications created
5262 * using {@link android.app.Notification.WearableExtender#setDisplayIntent}. See also
5263 * {@link android.app.Notification.WearableExtender#setCustomSizePreset} and
5264 * {@link #getCustomContentHeight}.
5265 */
5266 public WearableExtender setCustomContentHeight(int height) {
5267 mCustomContentHeight = height;
5268 return this;
5269 }
5270
5271 /**
5272 * Get the custom height in pixels for the display of this notification's content.
5273 * <p>This option is only available for custom display notifications created
5274 * using {@link #setDisplayIntent}. See also {@link #setCustomSizePreset} and
5275 * {@link #setCustomContentHeight}.
5276 */
5277 public int getCustomContentHeight() {
5278 return mCustomContentHeight;
5279 }
5280
5281 /**
5282 * Set whether the scrolling position for the contents of this notification should start
5283 * at the bottom of the contents instead of the top when the contents are too long to
5284 * display within the screen. Default is false (start scroll at the top).
5285 */
5286 public WearableExtender setStartScrollBottom(boolean startScrollBottom) {
5287 setFlag(FLAG_START_SCROLL_BOTTOM, startScrollBottom);
5288 return this;
5289 }
5290
5291 /**
5292 * Get whether the scrolling position for the contents of this notification should start
5293 * at the bottom of the contents instead of the top when the contents are too long to
5294 * display within the screen. Default is false (start scroll at the top).
5295 */
5296 public boolean getStartScrollBottom() {
5297 return (mFlags & FLAG_START_SCROLL_BOTTOM) != 0;
5298 }
5299
5300 /**
5301 * Set whether the content intent is available when the wearable device is not connected
5302 * to a companion device. The user can still trigger this intent when the wearable device
5303 * is offline, but a visual hint will indicate that the content intent may not be available.
5304 * Defaults to true.
5305 */
5306 public WearableExtender setContentIntentAvailableOffline(
5307 boolean contentIntentAvailableOffline) {
5308 setFlag(FLAG_CONTENT_INTENT_AVAILABLE_OFFLINE, contentIntentAvailableOffline);
5309 return this;
5310 }
5311
5312 /**
5313 * Get whether the content intent is available when the wearable device is not connected
5314 * to a companion device. The user can still trigger this intent when the wearable device
5315 * is offline, but a visual hint will indicate that the content intent may not be available.
5316 * Defaults to true.
5317 */
5318 public boolean getContentIntentAvailableOffline() {
5319 return (mFlags & FLAG_CONTENT_INTENT_AVAILABLE_OFFLINE) != 0;
5320 }
5321
5322 /**
5323 * Set a hint that this notification's icon should not be displayed.
5324 * @param hintHideIcon {@code true} to hide the icon, {@code false} otherwise.
5325 * @return this object for method chaining
5326 */
5327 public WearableExtender setHintHideIcon(boolean hintHideIcon) {
5328 setFlag(FLAG_HINT_HIDE_ICON, hintHideIcon);
5329 return this;
5330 }
5331
5332 /**
5333 * Get a hint that this notification's icon should not be displayed.
5334 * @return {@code true} if this icon should not be displayed, false otherwise.
5335 * The default value is {@code false} if this was never set.
5336 */
5337 public boolean getHintHideIcon() {
5338 return (mFlags & FLAG_HINT_HIDE_ICON) != 0;
5339 }
5340
5341 /**
5342 * Set a visual hint that only the background image of this notification should be
5343 * displayed, and other semantic content should be hidden. This hint is only applicable
5344 * to sub-pages added using {@link #addPage}.
5345 */
5346 public WearableExtender setHintShowBackgroundOnly(boolean hintShowBackgroundOnly) {
5347 setFlag(FLAG_HINT_SHOW_BACKGROUND_ONLY, hintShowBackgroundOnly);
5348 return this;
5349 }
5350
5351 /**
5352 * Get a visual hint that only the background image of this notification should be
5353 * displayed, and other semantic content should be hidden. This hint is only applicable
5354 * to sub-pages added using {@link android.app.Notification.WearableExtender#addPage}.
5355 */
5356 public boolean getHintShowBackgroundOnly() {
5357 return (mFlags & FLAG_HINT_SHOW_BACKGROUND_ONLY) != 0;
5358 }
5359
Griff Hazen5f2edfc2014-09-29 16:28:44 -07005360 /**
Griff Hazen9c5be4e2014-11-17 17:47:50 -08005361 * Set a hint that this notification's background should not be clipped if possible,
5362 * and should instead be resized to fully display on the screen, retaining the aspect
5363 * ratio of the image. This can be useful for images like barcodes or qr codes.
Griff Hazen5f2edfc2014-09-29 16:28:44 -07005364 * @param hintAvoidBackgroundClipping {@code true} to avoid clipping if possible.
5365 * @return this object for method chaining
5366 */
5367 public WearableExtender setHintAvoidBackgroundClipping(
5368 boolean hintAvoidBackgroundClipping) {
5369 setFlag(FLAG_HINT_AVOID_BACKGROUND_CLIPPING, hintAvoidBackgroundClipping);
5370 return this;
5371 }
5372
5373 /**
Griff Hazen9c5be4e2014-11-17 17:47:50 -08005374 * Get a hint that this notification's background should not be clipped if possible,
5375 * and should instead be resized to fully display on the screen, retaining the aspect
5376 * ratio of the image. This can be useful for images like barcodes or qr codes.
Griff Hazen5f2edfc2014-09-29 16:28:44 -07005377 * @return {@code true} if it's ok if the background is clipped on the screen, false
5378 * otherwise. The default value is {@code false} if this was never set.
5379 */
5380 public boolean getHintAvoidBackgroundClipping() {
5381 return (mFlags & FLAG_HINT_AVOID_BACKGROUND_CLIPPING) != 0;
5382 }
5383
5384 /**
5385 * Set a hint that the screen should remain on for at least this duration when
5386 * this notification is displayed on the screen.
5387 * @param timeout The requested screen timeout in milliseconds. Can also be either
5388 * {@link #SCREEN_TIMEOUT_SHORT} or {@link #SCREEN_TIMEOUT_LONG}.
5389 * @return this object for method chaining
5390 */
5391 public WearableExtender setHintScreenTimeout(int timeout) {
5392 mHintScreenTimeout = timeout;
5393 return this;
5394 }
5395
5396 /**
5397 * Get the duration, in milliseconds, that the screen should remain on for
5398 * when this notification is displayed.
5399 * @return the duration in milliseconds if > 0, or either one of the sentinel values
5400 * {@link #SCREEN_TIMEOUT_SHORT} or {@link #SCREEN_TIMEOUT_LONG}.
5401 */
5402 public int getHintScreenTimeout() {
5403 return mHintScreenTimeout;
5404 }
5405
Griff Hazen61a9e862014-05-22 16:05:19 -07005406 private void setFlag(int mask, boolean value) {
5407 if (value) {
5408 mFlags |= mask;
5409 } else {
5410 mFlags &= ~mask;
5411 }
5412 }
5413 }
5414
5415 /**
Zhen Yu Song9d5528b2014-11-14 15:34:39 -08005416 * <p>Helper class to add Android Auto extensions to notifications. To create a notification
5417 * with car extensions:
5418 *
5419 * <ol>
5420 * <li>Create an {@link Notification.Builder}, setting any desired
5421 * properties.
5422 * <li>Create a {@link CarExtender}.
5423 * <li>Set car-specific properties using the {@code add} and {@code set} methods of
5424 * {@link CarExtender}.
5425 * <li>Call {@link Notification.Builder#extend(Notification.Extender)}
5426 * to apply the extensions to a notification.
5427 * </ol>
5428 *
5429 * <pre class="prettyprint">
5430 * Notification notification = new Notification.Builder(context)
5431 * ...
5432 * .extend(new CarExtender()
5433 * .set*(...))
5434 * .build();
5435 * </pre>
5436 *
5437 * <p>Car extensions can be accessed on an existing notification by using the
5438 * {@code CarExtender(Notification)} constructor, and then using the {@code get} methods
5439 * to access values.
5440 */
5441 public static final class CarExtender implements Extender {
5442 private static final String TAG = "CarExtender";
5443
5444 private static final String EXTRA_CAR_EXTENDER = "android.car.EXTENSIONS";
5445 private static final String EXTRA_LARGE_ICON = "large_icon";
5446 private static final String EXTRA_CONVERSATION = "car_conversation";
5447 private static final String EXTRA_COLOR = "app_color";
5448
5449 private Bitmap mLargeIcon;
5450 private UnreadConversation mUnreadConversation;
5451 private int mColor = Notification.COLOR_DEFAULT;
5452
5453 /**
5454 * Create a {@link CarExtender} with default options.
5455 */
5456 public CarExtender() {
5457 }
5458
5459 /**
5460 * Create a {@link CarExtender} from the CarExtender options of an existing Notification.
5461 *
5462 * @param notif The notification from which to copy options.
5463 */
5464 public CarExtender(Notification notif) {
5465 Bundle carBundle = notif.extras == null ?
5466 null : notif.extras.getBundle(EXTRA_CAR_EXTENDER);
5467 if (carBundle != null) {
5468 mLargeIcon = carBundle.getParcelable(EXTRA_LARGE_ICON);
5469 mColor = carBundle.getInt(EXTRA_COLOR, Notification.COLOR_DEFAULT);
5470
5471 Bundle b = carBundle.getBundle(EXTRA_CONVERSATION);
5472 mUnreadConversation = UnreadConversation.getUnreadConversationFromBundle(b);
5473 }
5474 }
5475
5476 /**
5477 * Apply car extensions to a notification that is being built. This is typically called by
5478 * the {@link Notification.Builder#extend(Notification.Extender)}
5479 * method of {@link Notification.Builder}.
5480 */
5481 @Override
5482 public Notification.Builder extend(Notification.Builder builder) {
5483 Bundle carExtensions = new Bundle();
5484
5485 if (mLargeIcon != null) {
5486 carExtensions.putParcelable(EXTRA_LARGE_ICON, mLargeIcon);
5487 }
5488 if (mColor != Notification.COLOR_DEFAULT) {
5489 carExtensions.putInt(EXTRA_COLOR, mColor);
5490 }
5491
5492 if (mUnreadConversation != null) {
5493 Bundle b = mUnreadConversation.getBundleForUnreadConversation();
5494 carExtensions.putBundle(EXTRA_CONVERSATION, b);
5495 }
5496
5497 builder.getExtras().putBundle(EXTRA_CAR_EXTENDER, carExtensions);
5498 return builder;
5499 }
5500
5501 /**
5502 * Sets the accent color to use when Android Auto presents the notification.
5503 *
5504 * Android Auto uses the color set with {@link Notification.Builder#setColor(int)}
5505 * to accent the displayed notification. However, not all colors are acceptable in an
5506 * automotive setting. This method can be used to override the color provided in the
5507 * notification in such a situation.
5508 */
Tor Norbye80756e32015-03-02 09:39:27 -08005509 public CarExtender setColor(@ColorInt int color) {
Zhen Yu Song9d5528b2014-11-14 15:34:39 -08005510 mColor = color;
5511 return this;
5512 }
5513
5514 /**
5515 * Gets the accent color.
5516 *
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005517 * @see #setColor
Zhen Yu Song9d5528b2014-11-14 15:34:39 -08005518 */
Tor Norbye80756e32015-03-02 09:39:27 -08005519 @ColorInt
Zhen Yu Song9d5528b2014-11-14 15:34:39 -08005520 public int getColor() {
5521 return mColor;
5522 }
5523
5524 /**
5525 * Sets the large icon of the car notification.
5526 *
5527 * If no large icon is set in the extender, Android Auto will display the icon
5528 * specified by {@link Notification.Builder#setLargeIcon(android.graphics.Bitmap)}
5529 *
5530 * @param largeIcon The large icon to use in the car notification.
5531 * @return This object for method chaining.
5532 */
5533 public CarExtender setLargeIcon(Bitmap largeIcon) {
5534 mLargeIcon = largeIcon;
5535 return this;
5536 }
5537
5538 /**
5539 * Gets the large icon used in this car notification, or null if no icon has been set.
5540 *
5541 * @return The large icon for the car notification.
5542 * @see CarExtender#setLargeIcon
5543 */
5544 public Bitmap getLargeIcon() {
5545 return mLargeIcon;
5546 }
5547
5548 /**
5549 * Sets the unread conversation in a message notification.
5550 *
5551 * @param unreadConversation The unread part of the conversation this notification conveys.
5552 * @return This object for method chaining.
5553 */
5554 public CarExtender setUnreadConversation(UnreadConversation unreadConversation) {
5555 mUnreadConversation = unreadConversation;
5556 return this;
5557 }
5558
5559 /**
5560 * Returns the unread conversation conveyed by this notification.
5561 * @see #setUnreadConversation(UnreadConversation)
5562 */
5563 public UnreadConversation getUnreadConversation() {
5564 return mUnreadConversation;
5565 }
5566
5567 /**
5568 * A class which holds the unread messages from a conversation.
5569 */
5570 public static class UnreadConversation {
5571 private static final String KEY_AUTHOR = "author";
5572 private static final String KEY_TEXT = "text";
5573 private static final String KEY_MESSAGES = "messages";
5574 private static final String KEY_REMOTE_INPUT = "remote_input";
5575 private static final String KEY_ON_REPLY = "on_reply";
5576 private static final String KEY_ON_READ = "on_read";
5577 private static final String KEY_PARTICIPANTS = "participants";
5578 private static final String KEY_TIMESTAMP = "timestamp";
5579
5580 private final String[] mMessages;
5581 private final RemoteInput mRemoteInput;
5582 private final PendingIntent mReplyPendingIntent;
5583 private final PendingIntent mReadPendingIntent;
5584 private final String[] mParticipants;
5585 private final long mLatestTimestamp;
5586
5587 UnreadConversation(String[] messages, RemoteInput remoteInput,
5588 PendingIntent replyPendingIntent, PendingIntent readPendingIntent,
5589 String[] participants, long latestTimestamp) {
5590 mMessages = messages;
5591 mRemoteInput = remoteInput;
5592 mReadPendingIntent = readPendingIntent;
5593 mReplyPendingIntent = replyPendingIntent;
5594 mParticipants = participants;
5595 mLatestTimestamp = latestTimestamp;
5596 }
5597
5598 /**
5599 * Gets the list of messages conveyed by this notification.
5600 */
5601 public String[] getMessages() {
5602 return mMessages;
5603 }
5604
5605 /**
5606 * Gets the remote input that will be used to convey the response to a message list, or
5607 * null if no such remote input exists.
5608 */
5609 public RemoteInput getRemoteInput() {
5610 return mRemoteInput;
5611 }
5612
5613 /**
5614 * Gets the pending intent that will be triggered when the user replies to this
5615 * notification.
5616 */
5617 public PendingIntent getReplyPendingIntent() {
5618 return mReplyPendingIntent;
5619 }
5620
5621 /**
5622 * Gets the pending intent that Android Auto will send after it reads aloud all messages
5623 * in this object's message list.
5624 */
5625 public PendingIntent getReadPendingIntent() {
5626 return mReadPendingIntent;
5627 }
5628
5629 /**
5630 * Gets the participants in the conversation.
5631 */
5632 public String[] getParticipants() {
5633 return mParticipants;
5634 }
5635
5636 /**
5637 * Gets the firs participant in the conversation.
5638 */
5639 public String getParticipant() {
5640 return mParticipants.length > 0 ? mParticipants[0] : null;
5641 }
5642
5643 /**
5644 * Gets the timestamp of the conversation.
5645 */
5646 public long getLatestTimestamp() {
5647 return mLatestTimestamp;
5648 }
5649
5650 Bundle getBundleForUnreadConversation() {
5651 Bundle b = new Bundle();
5652 String author = null;
5653 if (mParticipants != null && mParticipants.length > 1) {
5654 author = mParticipants[0];
5655 }
5656 Parcelable[] messages = new Parcelable[mMessages.length];
5657 for (int i = 0; i < messages.length; i++) {
5658 Bundle m = new Bundle();
5659 m.putString(KEY_TEXT, mMessages[i]);
5660 m.putString(KEY_AUTHOR, author);
5661 messages[i] = m;
5662 }
5663 b.putParcelableArray(KEY_MESSAGES, messages);
5664 if (mRemoteInput != null) {
5665 b.putParcelable(KEY_REMOTE_INPUT, mRemoteInput);
5666 }
5667 b.putParcelable(KEY_ON_REPLY, mReplyPendingIntent);
5668 b.putParcelable(KEY_ON_READ, mReadPendingIntent);
5669 b.putStringArray(KEY_PARTICIPANTS, mParticipants);
5670 b.putLong(KEY_TIMESTAMP, mLatestTimestamp);
5671 return b;
5672 }
5673
5674 static UnreadConversation getUnreadConversationFromBundle(Bundle b) {
5675 if (b == null) {
5676 return null;
5677 }
5678 Parcelable[] parcelableMessages = b.getParcelableArray(KEY_MESSAGES);
5679 String[] messages = null;
5680 if (parcelableMessages != null) {
5681 String[] tmp = new String[parcelableMessages.length];
5682 boolean success = true;
5683 for (int i = 0; i < tmp.length; i++) {
5684 if (!(parcelableMessages[i] instanceof Bundle)) {
5685 success = false;
5686 break;
5687 }
5688 tmp[i] = ((Bundle) parcelableMessages[i]).getString(KEY_TEXT);
5689 if (tmp[i] == null) {
5690 success = false;
5691 break;
5692 }
5693 }
5694 if (success) {
5695 messages = tmp;
5696 } else {
5697 return null;
5698 }
5699 }
5700
5701 PendingIntent onRead = b.getParcelable(KEY_ON_READ);
5702 PendingIntent onReply = b.getParcelable(KEY_ON_REPLY);
5703
5704 RemoteInput remoteInput = b.getParcelable(KEY_REMOTE_INPUT);
5705
5706 String[] participants = b.getStringArray(KEY_PARTICIPANTS);
5707 if (participants == null || participants.length != 1) {
5708 return null;
5709 }
5710
5711 return new UnreadConversation(messages,
5712 remoteInput,
5713 onReply,
5714 onRead,
5715 participants, b.getLong(KEY_TIMESTAMP));
5716 }
5717 };
5718
5719 /**
5720 * Builder class for {@link CarExtender.UnreadConversation} objects.
5721 */
5722 public static class Builder {
5723 private final List<String> mMessages = new ArrayList<String>();
5724 private final String mParticipant;
5725 private RemoteInput mRemoteInput;
5726 private PendingIntent mReadPendingIntent;
5727 private PendingIntent mReplyPendingIntent;
5728 private long mLatestTimestamp;
5729
5730 /**
5731 * Constructs a new builder for {@link CarExtender.UnreadConversation}.
5732 *
5733 * @param name The name of the other participant in the conversation.
5734 */
5735 public Builder(String name) {
5736 mParticipant = name;
5737 }
5738
5739 /**
5740 * Appends a new unread message to the list of messages for this conversation.
5741 *
5742 * The messages should be added from oldest to newest.
5743 *
5744 * @param message The text of the new unread message.
5745 * @return This object for method chaining.
5746 */
5747 public Builder addMessage(String message) {
5748 mMessages.add(message);
5749 return this;
5750 }
5751
5752 /**
5753 * Sets the pending intent and remote input which will convey the reply to this
5754 * notification.
5755 *
5756 * @param pendingIntent The pending intent which will be triggered on a reply.
5757 * @param remoteInput The remote input parcelable which will carry the reply.
5758 * @return This object for method chaining.
5759 *
5760 * @see CarExtender.UnreadConversation#getRemoteInput
5761 * @see CarExtender.UnreadConversation#getReplyPendingIntent
5762 */
5763 public Builder setReplyAction(
5764 PendingIntent pendingIntent, RemoteInput remoteInput) {
5765 mRemoteInput = remoteInput;
5766 mReplyPendingIntent = pendingIntent;
5767
5768 return this;
5769 }
5770
5771 /**
5772 * Sets the pending intent that will be sent once the messages in this notification
5773 * are read.
5774 *
5775 * @param pendingIntent The pending intent to use.
5776 * @return This object for method chaining.
5777 */
5778 public Builder setReadPendingIntent(PendingIntent pendingIntent) {
5779 mReadPendingIntent = pendingIntent;
5780 return this;
5781 }
5782
5783 /**
5784 * Sets the timestamp of the most recent message in an unread conversation.
5785 *
5786 * If a messaging notification has been posted by your application and has not
5787 * yet been cancelled, posting a later notification with the same id and tag
5788 * but without a newer timestamp may result in Android Auto not displaying a
5789 * heads up notification for the later notification.
5790 *
5791 * @param timestamp The timestamp of the most recent message in the conversation.
5792 * @return This object for method chaining.
5793 */
5794 public Builder setLatestTimestamp(long timestamp) {
5795 mLatestTimestamp = timestamp;
5796 return this;
5797 }
5798
5799 /**
5800 * Builds a new unread conversation object.
5801 *
5802 * @return The new unread conversation object.
5803 */
5804 public UnreadConversation build() {
5805 String[] messages = mMessages.toArray(new String[mMessages.size()]);
5806 String[] participants = { mParticipant };
5807 return new UnreadConversation(messages, mRemoteInput, mReplyPendingIntent,
5808 mReadPendingIntent, participants, mLatestTimestamp);
5809 }
5810 }
5811 }
5812
5813 /**
Griff Hazen61a9e862014-05-22 16:05:19 -07005814 * Get an array of Notification objects from a parcelable array bundle field.
5815 * Update the bundle to have a typed array so fetches in the future don't need
5816 * to do an array copy.
5817 */
5818 private static Notification[] getNotificationArrayFromBundle(Bundle bundle, String key) {
5819 Parcelable[] array = bundle.getParcelableArray(key);
5820 if (array instanceof Notification[] || array == null) {
5821 return (Notification[]) array;
5822 }
5823 Notification[] typedArray = Arrays.copyOf(array, array.length,
5824 Notification[].class);
5825 bundle.putParcelableArray(key, typedArray);
5826 return typedArray;
5827 }
Christoph Studer4600f9b2014-07-22 22:44:43 +02005828
5829 private static class BuilderRemoteViews extends RemoteViews {
5830 public BuilderRemoteViews(Parcel parcel) {
5831 super(parcel);
5832 }
5833
Kenny Guy77320062014-08-27 21:37:15 +01005834 public BuilderRemoteViews(ApplicationInfo appInfo, int layoutId) {
5835 super(appInfo, layoutId);
Christoph Studer4600f9b2014-07-22 22:44:43 +02005836 }
5837
5838 @Override
5839 public BuilderRemoteViews clone() {
5840 Parcel p = Parcel.obtain();
5841 writeToParcel(p, 0);
5842 p.setDataPosition(0);
5843 BuilderRemoteViews brv = new BuilderRemoteViews(p);
5844 p.recycle();
5845 return brv;
5846 }
5847 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005848}