blob: d8f0ac518151a7f4c65377c105c8c9a9a3a520b8 [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 Cinek29603462015-11-17 19:04:39 -08002997 contentView.setViewVisibility(R.id.sub_text_divider, View.GONE);
2998 contentView.setViewVisibility(R.id.content_info_divider, View.GONE);
2999 contentView.setViewVisibility(R.id.time_divider, View.GONE);
Selim Cinekc848c3a2016-01-13 15:27:30 -08003000 contentView.setImageViewIcon(R.id.profile_badge, null);
3001 contentView.setViewVisibility(R.id.profile_badge, View.GONE);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003002 }
3003
3004 private void resetContentMargins(RemoteViews contentView) {
3005 contentView.setViewLayoutMarginEnd(R.id.line1, 0);
Selim Cinekc848c3a2016-01-13 15:27:30 -08003006 contentView.setViewLayoutMarginEnd(R.id.text, 0);
Christoph Studerfe718432014-09-01 18:21:18 +02003007 }
3008
Jorim Jaggi445d3c02014-08-19 22:33:42 +02003009 private RemoteViews applyStandardTemplate(int resId) {
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02003010 return applyStandardTemplate(resId, true /* hasProgress */);
3011 }
3012
3013 /**
3014 * @param hasProgress whether the progress bar should be shown and set
3015 */
3016 private RemoteViews applyStandardTemplate(int resId, boolean hasProgress) {
Kenny Guy77320062014-08-27 21:37:15 +01003017 RemoteViews contentView = new BuilderRemoteViews(mContext.getApplicationInfo(), resId);
Dan Sandler539aad42014-08-04 00:43:39 -04003018
Christoph Studerfe718432014-09-01 18:21:18 +02003019 resetStandardTemplate(contentView);
3020
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003021 final Bundle ex = mN.extras;
Dan Sandler190d58d2014-05-15 09:33:39 -04003022
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003023 bindNotificationHeader(contentView);
3024 bindLargeIcon(contentView);
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003025 if (ex.getCharSequence(EXTRA_TITLE) != null) {
Selim Cinek860b6da2015-12-16 19:02:19 -08003026 contentView.setViewVisibility(R.id.title, View.VISIBLE);
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003027 contentView.setTextViewText(R.id.title,
3028 processLegacyText(ex.getCharSequence(EXTRA_TITLE)));
Joe Onorato561d3852010-11-20 18:09:34 -08003029 }
Selim Cinek29603462015-11-17 19:04:39 -08003030 boolean showProgress = handleProgressBar(hasProgress, contentView, ex);
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003031 if (ex.getCharSequence(EXTRA_TEXT) != null) {
Selim Cinek41598732016-01-11 16:58:37 -08003032 int textId = showProgress ? com.android.internal.R.id.text_line_1
3033 : com.android.internal.R.id.text;
3034 contentView.setTextViewText(textId, processLegacyText(
3035 ex.getCharSequence(EXTRA_TEXT)));
3036 contentView.setViewVisibility(textId, View.VISIBLE);
Joe Onorato561d3852010-11-20 18:09:34 -08003037 }
Selim Cinekc848c3a2016-01-13 15:27:30 -08003038
Selim Cinek860b6da2015-12-16 19:02:19 -08003039 setContentMinHeight(contentView, showProgress || mN.mLargeIcon != null);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003040
Selim Cinek29603462015-11-17 19:04:39 -08003041 return contentView;
3042 }
3043
Selim Cinek860b6da2015-12-16 19:02:19 -08003044 /**
3045 * @param remoteView the remote view to update the minheight in
3046 * @param hasMinHeight does it have a mimHeight
3047 * @hide
3048 */
3049 void setContentMinHeight(RemoteViews remoteView, boolean hasMinHeight) {
3050 int minHeight = 0;
3051 if (hasMinHeight) {
3052 // we need to set the minHeight of the notification
3053 minHeight = mContext.getResources().getDimensionPixelSize(
3054 com.android.internal.R.dimen.notification_min_content_height);
3055 }
3056 remoteView.setInt(R.id.notification_main_column, "setMinimumHeight", minHeight);
3057 }
3058
Selim Cinek29603462015-11-17 19:04:39 -08003059 private boolean handleProgressBar(boolean hasProgress, RemoteViews contentView, Bundle ex) {
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003060 final int max = ex.getInt(EXTRA_PROGRESS_MAX, 0);
3061 final int progress = ex.getInt(EXTRA_PROGRESS, 0);
3062 final boolean ind = ex.getBoolean(EXTRA_PROGRESS_INDETERMINATE);
3063 if (hasProgress && (max != 0 || ind)) {
Selim Cinek29603462015-11-17 19:04:39 -08003064 contentView.setViewVisibility(com.android.internal.R.id.progress, View.VISIBLE);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003065 contentView.setProgressBar(
Selim Cinek29603462015-11-17 19:04:39 -08003066 R.id.progress, max, progress, ind);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003067 contentView.setProgressBackgroundTintList(
3068 R.id.progress, ColorStateList.valueOf(mContext.getColor(
3069 R.color.notification_progress_background_color)));
Selim Cinek29603462015-11-17 19:04:39 -08003070 if (mN.color != COLOR_DEFAULT) {
3071 ColorStateList colorStateList = ColorStateList.valueOf(mN.color);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003072 contentView.setProgressTintList(R.id.progress, colorStateList);
3073 contentView.setProgressIndeterminateTintList(R.id.progress, colorStateList);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003074 }
Selim Cinek29603462015-11-17 19:04:39 -08003075 return true;
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003076 } else {
3077 contentView.setViewVisibility(R.id.progress, View.GONE);
Selim Cinek29603462015-11-17 19:04:39 -08003078 return false;
Jeff Sharkey1c400132011-08-05 14:50:13 -07003079 }
Joe Onorato561d3852010-11-20 18:09:34 -08003080 }
3081
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003082 private void bindLargeIcon(RemoteViews contentView) {
3083 if (mN.mLargeIcon != null) {
3084 contentView.setViewVisibility(R.id.right_icon, View.VISIBLE);
3085 contentView.setImageViewIcon(R.id.right_icon, mN.mLargeIcon);
3086 processLargeLegacyIcon(mN.mLargeIcon, contentView);
3087 int endMargin = mContext.getResources().getDimensionPixelSize(
3088 R.dimen.notification_content_picture_margin);
3089 contentView.setViewLayoutMarginEnd(R.id.line1, endMargin);
Selim Cinekc848c3a2016-01-13 15:27:30 -08003090 contentView.setViewLayoutMarginEnd(R.id.text, endMargin);
Selim Cinek29603462015-11-17 19:04:39 -08003091 contentView.setViewLayoutMarginEnd(R.id.progress, endMargin);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003092 }
3093 }
3094
3095 private void bindNotificationHeader(RemoteViews contentView) {
3096 bindSmallIcon(contentView);
3097 bindHeaderAppName(contentView);
Selim Cinek03d0d652015-11-13 13:18:09 -05003098 bindHeaderSubText(contentView);
Selim Cinek29603462015-11-17 19:04:39 -08003099 bindContentInfo(contentView);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003100 bindHeaderChronometerAndTime(contentView);
3101 bindExpandButton(contentView);
Selim Cinekc848c3a2016-01-13 15:27:30 -08003102 bindProfileBadge(contentView);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003103 }
3104
Selim Cinek29603462015-11-17 19:04:39 -08003105 private void bindContentInfo(RemoteViews contentView) {
3106 boolean visible = false;
3107 if (mN.extras.getCharSequence(EXTRA_INFO_TEXT) != null) {
3108 contentView.setTextViewText(R.id.header_content_info,
3109 processLegacyText(mN.extras.getCharSequence(EXTRA_INFO_TEXT)));
3110 contentView.setViewVisibility(R.id.header_content_info, View.VISIBLE);
3111 visible = true;
3112 } else if (mN.number > 0) {
3113 final int tooBig = mContext.getResources().getInteger(
3114 R.integer.status_bar_notification_info_maxnum);
3115 if (mN.number > tooBig) {
3116 contentView.setTextViewText(R.id.header_content_info, processLegacyText(
3117 mContext.getResources().getString(
3118 R.string.status_bar_notification_info_overflow)));
3119 } else {
3120 contentView.setTextViewText(R.id.header_content_info,
3121 processLegacyText(String.valueOf(mN.number)));
3122 }
3123 contentView.setViewVisibility(R.id.header_content_info, View.VISIBLE);
3124 visible = true;
3125 }
3126 if (visible) {
3127 contentView.setViewVisibility(R.id.content_info_divider, View.VISIBLE);
3128 }
3129 }
3130
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003131 private void bindExpandButton(RemoteViews contentView) {
3132 contentView.setDrawableParameters(R.id.expand_button, false, -1, resolveColor(),
3133 PorterDuff.Mode.SRC_ATOP, -1);
Selim Cinekea4bef72015-12-02 15:51:10 -08003134 contentView.setInt(R.id.notification_header, "setOriginalNotificationColor",
3135 resolveColor());
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003136 }
3137
3138 private void bindHeaderChronometerAndTime(RemoteViews contentView) {
3139 if (showsTimeOrChronometer()) {
Selim Cinek29603462015-11-17 19:04:39 -08003140 contentView.setViewVisibility(R.id.time_divider, View.VISIBLE);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003141 if (mN.extras.getBoolean(EXTRA_SHOW_CHRONOMETER)) {
3142 contentView.setViewVisibility(R.id.chronometer, View.VISIBLE);
3143 contentView.setLong(R.id.chronometer, "setBase",
3144 mN.when + (SystemClock.elapsedRealtime() - System.currentTimeMillis()));
3145 contentView.setBoolean(R.id.chronometer, "setStarted", true);
Selim Cinek81c23aa2016-02-25 16:23:13 -08003146 boolean countsDown = mN.extras.getBoolean(EXTRA_CHRONOMETER_COUNTS_DOWN);
3147 contentView.setChronometerCountsDown(R.id.chronometer, countsDown);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003148 } else {
3149 contentView.setViewVisibility(R.id.time, View.VISIBLE);
3150 contentView.setLong(R.id.time, "setTime", mN.when);
3151 }
3152 }
3153 }
3154
Selim Cinek03d0d652015-11-13 13:18:09 -05003155 private void bindHeaderSubText(RemoteViews contentView) {
3156 CharSequence subText = mN.extras.getCharSequence(EXTRA_SUB_TEXT);
3157 if (subText == null && mStyle != null && mStyle.mSummaryTextSet
3158 && mStyle.hasSummaryInHeader()) {
3159 subText = mStyle.mSummaryText;
3160 }
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003161 if (subText != null) {
3162 // TODO: Remove the span entirely to only have the string with propper formating.
3163 contentView.setTextViewText(R.id.header_sub_text, processLegacyText(subText));
3164 contentView.setViewVisibility(R.id.header_sub_text, View.VISIBLE);
Selim Cinek29603462015-11-17 19:04:39 -08003165 contentView.setViewVisibility(R.id.sub_text_divider, View.VISIBLE);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003166 }
3167 }
3168
3169 private void bindHeaderAppName(RemoteViews contentView) {
3170 PackageManager packageManager = mContext.getPackageManager();
3171 ApplicationInfo info = null;
3172 try {
3173 info = packageManager.getApplicationInfo(mContext.getApplicationInfo().packageName,
3174 0);
3175 } catch (final NameNotFoundException e) {
3176 return;
3177 }
3178 CharSequence appName = info != null ? packageManager.getApplicationLabel(info)
3179 : null;
3180 if (TextUtils.isEmpty(appName)) {
3181 return;
3182 }
3183 contentView.setTextViewText(R.id.app_name_text, appName);
Selim Cinekaef485a2016-02-05 10:31:20 -08003184 contentView.setTextColor(R.id.app_name_text, resolveColor());
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003185 }
3186
3187 private void bindSmallIcon(RemoteViews contentView) {
3188 contentView.setImageViewIcon(R.id.icon, mN.mSmallIcon);
3189 processSmallIconColor(mN.mSmallIcon, contentView);
3190 }
3191
Jorim Jaggi445d3c02014-08-19 22:33:42 +02003192 /**
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02003193 * @return true if the built notification will show the time or the chronometer; false
3194 * otherwise
3195 */
3196 private boolean showsTimeOrChronometer() {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003197 return mN.when != 0 && mN.extras.getBoolean(EXTRA_SHOW_WHEN);
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02003198 }
3199
Christoph Studerfe718432014-09-01 18:21:18 +02003200 private void resetStandardTemplateWithActions(RemoteViews big) {
3201 big.setViewVisibility(R.id.actions, View.GONE);
Christoph Studerfe718432014-09-01 18:21:18 +02003202 big.removeAllViews(R.id.actions);
Adrian Roose458aa82015-12-08 16:17:19 -08003203
3204 big.setViewVisibility(R.id.notification_material_reply_container, View.GONE);
3205 big.setTextViewText(R.id.notification_material_reply_text_1, null);
3206
3207 big.setViewVisibility(R.id.notification_material_reply_text_2, View.GONE);
3208 big.setTextViewText(R.id.notification_material_reply_text_2, null);
3209 big.setViewVisibility(R.id.notification_material_reply_text_3, View.GONE);
3210 big.setTextViewText(R.id.notification_material_reply_text_3, null);
Christoph Studerfe718432014-09-01 18:21:18 +02003211 }
3212
Daniel Sandler96fd7c12012-03-30 16:37:36 -04003213 private RemoteViews applyStandardTemplateWithActions(int layoutId) {
Jorim Jaggi445d3c02014-08-19 22:33:42 +02003214 RemoteViews big = applyStandardTemplate(layoutId);
Daniel Sandler96fd7c12012-03-30 16:37:36 -04003215
Christoph Studerfe718432014-09-01 18:21:18 +02003216 resetStandardTemplateWithActions(big);
3217
Adrian Roose458aa82015-12-08 16:17:19 -08003218 boolean validRemoteInput = false;
3219
Daniel Sandler96fd7c12012-03-30 16:37:36 -04003220 int N = mActions.size();
3221 if (N > 0) {
Daniel Sandler96fd7c12012-03-30 16:37:36 -04003222 big.setViewVisibility(R.id.actions, View.VISIBLE);
Daniel Sandler8680bf82012-05-15 16:52:52 -04003223 if (N>MAX_ACTION_BUTTONS) N=MAX_ACTION_BUTTONS;
Daniel Sandler96fd7c12012-03-30 16:37:36 -04003224 for (int i=0; i<N; i++) {
Adrian Roose458aa82015-12-08 16:17:19 -08003225 Action action = mActions.get(i);
3226 validRemoteInput |= hasValidRemoteInput(action);
3227
3228 final RemoteViews button = generateActionButton(action);
Adrian Roos9b123cf2016-02-04 14:55:57 -08003229 if (i == N - 1) {
3230 button.setViewLayoutWidth(com.android.internal.R.id.action0,
3231 ViewGroup.LayoutParams.MATCH_PARENT);
3232 }
Daniel Sandler96fd7c12012-03-30 16:37:36 -04003233 big.addView(R.id.actions, button);
3234 }
3235 }
Adrian Roose458aa82015-12-08 16:17:19 -08003236
3237 CharSequence[] replyText = mN.extras.getCharSequenceArray(EXTRA_REMOTE_INPUT_HISTORY);
3238 if (validRemoteInput && replyText != null
3239 && replyText.length > 0 && !TextUtils.isEmpty(replyText[0])) {
3240 big.setViewVisibility(R.id.notification_material_reply_container, View.VISIBLE);
3241 big.setTextViewText(R.id.notification_material_reply_text_1, replyText[0]);
3242
3243 if (replyText.length > 1 && !TextUtils.isEmpty(replyText[1])) {
3244 big.setViewVisibility(R.id.notification_material_reply_text_2, View.VISIBLE);
3245 big.setTextViewText(R.id.notification_material_reply_text_2, replyText[1]);
3246
3247 if (replyText.length > 2 && !TextUtils.isEmpty(replyText[2])) {
3248 big.setViewVisibility(
3249 R.id.notification_material_reply_text_3, View.VISIBLE);
3250 big.setTextViewText(R.id.notification_material_reply_text_3, replyText[2]);
3251 }
3252 }
3253 }
3254
Daniel Sandler96fd7c12012-03-30 16:37:36 -04003255 return big;
3256 }
3257
Adrian Roose458aa82015-12-08 16:17:19 -08003258 private boolean hasValidRemoteInput(Action action) {
3259 if (TextUtils.isEmpty(action.title) || action.actionIntent == null) {
3260 // Weird actions
3261 return false;
3262 }
3263
3264 RemoteInput[] remoteInputs = action.getRemoteInputs();
3265 if (remoteInputs == null) {
3266 return false;
3267 }
3268
3269 for (RemoteInput r : remoteInputs) {
3270 CharSequence[] choices = r.getChoices();
3271 if (r.getAllowFreeFormInput() || (choices != null && choices.length != 0)) {
3272 return true;
3273 }
3274 }
3275 return false;
3276 }
3277
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003278 /**
3279 * Construct a RemoteViews for the final 1U notification layout. In order:
3280 * 1. Custom contentView from the caller
3281 * 2. Style's proposed content view
3282 * 3. Standard template view
3283 */
Julia Reynolds3b848122016-02-26 10:45:32 -05003284 public RemoteViews createContentView() {
Selim Cinek593610c2016-02-16 18:42:57 -08003285 if (mN.contentView != null && (mStyle == null || !mStyle.displayCustomViewInline())) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003286 return mN.contentView;
3287 } else if (mStyle != null) {
3288 final RemoteViews styleView = mStyle.makeContentView();
3289 if (styleView != null) {
3290 return styleView;
3291 }
Joe Onorato46439ce2010-11-19 13:56:21 -08003292 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003293 return applyStandardTemplate(getBaseLayoutResource());
Joe Onorato46439ce2010-11-19 13:56:21 -08003294 }
3295
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003296 /**
3297 * Construct a RemoteViews for the final big notification layout.
3298 */
Julia Reynolds3b848122016-02-26 10:45:32 -05003299 public RemoteViews createBigContentView() {
Selim Cinek850a8542015-11-11 11:48:36 -05003300 RemoteViews result = null;
Selim Cinek593610c2016-02-16 18:42:57 -08003301 if (mN.bigContentView != null
3302 && (mStyle == null || !mStyle.displayCustomViewInline())) {
Julia Reynolds089e3e42015-11-18 09:59:57 -05003303 return mN.bigContentView;
3304 } else if (mStyle != null) {
Selim Cinek850a8542015-11-11 11:48:36 -05003305 result = mStyle.makeBigContentView();
Selim Cinek90dcf6d2015-11-18 20:24:13 -08003306 hideLine1Text(result);
Selim Cinekcc10bfb2016-02-10 16:24:21 -08003307 } else if (mActions.size() != 0) {
3308 result = applyStandardTemplateWithActions(getBigBaseLayoutResource());
Selim Cinek850a8542015-11-11 11:48:36 -05003309 }
3310 adaptNotificationHeaderForBigContentView(result);
3311 return result;
3312 }
Daniel Sandler96fd7c12012-03-30 16:37:36 -04003313
Selim Cinekeaa29ca2015-11-23 13:51:13 -08003314 /**
3315 * Construct a RemoteViews for the final notification header only
3316 *
3317 * @hide
3318 */
3319 public RemoteViews makeNotificationHeader() {
3320 RemoteViews header = new BuilderRemoteViews(mContext.getApplicationInfo(),
3321 R.layout.notification_template_header);
3322 resetNotificationHeader(header);
3323 bindNotificationHeader(header);
3324 return header;
3325 }
3326
Selim Cinek29603462015-11-17 19:04:39 -08003327 private void hideLine1Text(RemoteViews result) {
Selim Cinekcc10bfb2016-02-10 16:24:21 -08003328 if (result != null) {
3329 result.setViewVisibility(R.id.text_line_1, View.GONE);
3330 }
Selim Cinek29603462015-11-17 19:04:39 -08003331 }
3332
Selim Cinek850a8542015-11-11 11:48:36 -05003333 private void adaptNotificationHeaderForBigContentView(RemoteViews result) {
Selim Cinekcc10bfb2016-02-10 16:24:21 -08003334 if (result != null) {
3335 result.setBoolean(R.id.notification_header, "setExpanded", true);
3336 }
Daniel Sandler96fd7c12012-03-30 16:37:36 -04003337 }
3338
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003339 /**
3340 * Construct a RemoteViews for the final heads-up notification layout.
3341 */
Julia Reynolds3b848122016-02-26 10:45:32 -05003342 public RemoteViews createHeadsUpContentView() {
Selim Cinek593610c2016-02-16 18:42:57 -08003343 if (mN.headsUpContentView != null
3344 && (mStyle == null || !mStyle.displayCustomViewInline())) {
Julia Reynolds089e3e42015-11-18 09:59:57 -05003345 return mN.headsUpContentView;
3346 } else if (mStyle != null) {
3347 final RemoteViews styleView = mStyle.makeHeadsUpContentView();
3348 if (styleView != null) {
3349 return styleView;
3350 }
3351 } else if (mActions.size() == 0) {
3352 return null;
3353 }
3354
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01003355 return applyStandardTemplateWithActions(getBigBaseLayoutResource());
Chris Wren8fd39ec2014-02-27 17:43:26 -05003356 }
3357
Selim Cinek624c02db2015-12-14 21:00:02 -08003358 /**
3359 * Construct a RemoteViews for the display in public contexts like on the lockscreen.
3360 *
3361 * @hide
3362 */
3363 public RemoteViews makePublicContentView() {
3364 if (mN.publicVersion != null) {
3365 final Builder builder = recoverBuilder(mContext, mN.publicVersion);
Julia Reynolds3b848122016-02-26 10:45:32 -05003366 return builder.createContentView();
Selim Cinek624c02db2015-12-14 21:00:02 -08003367 }
3368 Bundle savedBundle = mN.extras;
3369 Style style = mStyle;
3370 mStyle = null;
3371 Icon largeIcon = mN.mLargeIcon;
3372 mN.mLargeIcon = null;
3373 Bundle publicExtras = new Bundle();
3374 publicExtras.putBoolean(EXTRA_SHOW_WHEN,
3375 savedBundle.getBoolean(EXTRA_SHOW_WHEN));
3376 publicExtras.putBoolean(EXTRA_SHOW_CHRONOMETER,
3377 savedBundle.getBoolean(EXTRA_SHOW_CHRONOMETER));
Selim Cinek81c23aa2016-02-25 16:23:13 -08003378 publicExtras.putBoolean(EXTRA_CHRONOMETER_COUNTS_DOWN,
3379 savedBundle.getBoolean(EXTRA_CHRONOMETER_COUNTS_DOWN));
Selim Cinek624c02db2015-12-14 21:00:02 -08003380 publicExtras.putCharSequence(EXTRA_TITLE,
3381 mContext.getString(R.string.notification_hidden_text));
3382 mN.extras = publicExtras;
3383 final RemoteViews publicView = applyStandardTemplate(getBaseLayoutResource());
3384 mN.extras = savedBundle;
3385 mN.mLargeIcon = largeIcon;
3386 mStyle = style;
3387 return publicView;
3388 }
3389
3390
Chris Wren8fd39ec2014-02-27 17:43:26 -05003391
Daniel Sandler96fd7c12012-03-30 16:37:36 -04003392 private RemoteViews generateActionButton(Action action) {
Daniel Sandler8680bf82012-05-15 16:52:52 -04003393 final boolean tombstone = (action.actionIntent == null);
Selim Cinekf33b1112015-07-15 17:45:11 -07003394 RemoteViews button = new BuilderRemoteViews(mContext.getApplicationInfo(),
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01003395 tombstone ? getActionTombstoneLayoutResource()
3396 : getActionLayoutResource());
Dan Sandler68079d52015-07-22 10:45:30 -04003397 final Icon ai = action.getIcon();
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01003398 button.setTextViewText(R.id.action0, processLegacyText(action.title));
Daniel Sandler8680bf82012-05-15 16:52:52 -04003399 if (!tombstone) {
Daniel Sandlere5518842012-05-10 16:20:40 -04003400 button.setOnClickPendingIntent(R.id.action0, action.actionIntent);
Daniel Sandlere5518842012-05-10 16:20:40 -04003401 }
Daniel Sandler96fd7c12012-03-30 16:37:36 -04003402 button.setContentDescription(R.id.action0, action.title);
Adrian Roosfe84e1f2015-11-04 15:55:39 -08003403 if (action.mRemoteInputs != null) {
3404 button.setRemoteInputs(R.id.action0, action.mRemoteInputs);
3405 }
3406 if (mN.color != COLOR_DEFAULT) {
3407 button.setTextColor(R.id.action0, mN.color);
3408 }
Daniel Sandler96fd7c12012-03-30 16:37:36 -04003409 return button;
3410 }
3411
Joe Onoratocb109a02011-01-18 17:57:41 -08003412 /**
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01003413 * @return Whether we are currently building a notification from a legacy (an app that
Alan Viverette3cb07a462014-06-06 14:19:53 -07003414 * doesn't create material notifications by itself) app.
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01003415 */
3416 private boolean isLegacy() {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003417 return getColorUtil() != null;
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01003418 }
3419
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01003420 private CharSequence processLegacyText(CharSequence charSequence) {
3421 if (isLegacy()) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003422 return getColorUtil().invertCharSequenceColors(charSequence);
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01003423 } else {
3424 return charSequence;
3425 }
3426 }
3427
Dan Sandler26e81cf2014-05-06 10:01:27 -04003428 /**
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003429 * Apply any necessariy colors to the small icon
Dan Sandler26e81cf2014-05-06 10:01:27 -04003430 */
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003431 private void processSmallIconColor(Icon smallIcon, RemoteViews contentView) {
Selim Cinekea4bef72015-12-02 15:51:10 -08003432 boolean colorable = !isLegacy() || getColorUtil().isGrayscaleIcon(mContext, smallIcon);
3433 if (colorable) {
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003434 contentView.setDrawableParameters(R.id.icon, false, -1, resolveColor(),
Jorim Jaggi92df1f22014-12-16 19:44:41 +01003435 PorterDuff.Mode.SRC_ATOP, -1);
Selim Cinekea4bef72015-12-02 15:51:10 -08003436
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01003437 }
Selim Cinekea4bef72015-12-02 15:51:10 -08003438 contentView.setInt(R.id.notification_header, "setOriginalIconColor",
3439 colorable ? resolveColor() : NotificationHeaderView.NO_COLOR);
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01003440 }
3441
Dan Sandler26e81cf2014-05-06 10:01:27 -04003442 /**
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003443 * Make the largeIcon dark if it's a fake smallIcon (that is,
Dan Sandler26e81cf2014-05-06 10:01:27 -04003444 * if it's grayscale).
3445 */
3446 // TODO: also check bounds, transparency, that sort of thing.
Dan Sandlerd63f9322015-05-06 15:18:49 -04003447 private void processLargeLegacyIcon(Icon largeIcon, RemoteViews contentView) {
3448 if (largeIcon != null && isLegacy()
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003449 && getColorUtil().isGrayscaleIcon(mContext, largeIcon)) {
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003450 // resolve color will fall back to the default when legacy
3451 contentView.setDrawableParameters(R.id.icon, false, -1, resolveColor(),
Dan Sandler190d58d2014-05-15 09:33:39 -04003452 PorterDuff.Mode.SRC_ATOP, -1);
Jorim Jaggi92df1f22014-12-16 19:44:41 +01003453 }
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01003454 }
3455
Julia Reynolds10ee1fc2015-11-09 11:04:55 -05003456 private void sanitizeColor() {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003457 if (mN.color != COLOR_DEFAULT) {
3458 mN.color |= 0xFF000000; // no alpha for custom colors
Jorim Jaggi74419312014-06-10 20:57:21 +02003459 }
Jorim Jaggi74419312014-06-10 20:57:21 +02003460 }
3461
Selim Cinek5bf069a2015-11-10 19:14:27 -05003462 int resolveColor() {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003463 if (mN.color == COLOR_DEFAULT) {
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003464 return mContext.getColor(R.color.notification_icon_default_color);
Dan Sandler26e81cf2014-05-06 10:01:27 -04003465 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003466 return mN.color;
Dan Sandler26e81cf2014-05-06 10:01:27 -04003467 }
3468
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01003469 /**
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003470 * Apply the unstyled operations and return a new {@link Notification} object.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04003471 * @hide
Joe Onoratocb109a02011-01-18 17:57:41 -08003472 */
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04003473 public Notification buildUnstyled() {
Daniel Sandlera0a938c2012-03-15 08:42:37 -04003474 if (mActions.size() > 0) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003475 mN.actions = new Action[mActions.size()];
3476 mActions.toArray(mN.actions);
Daniel Sandlera0a938c2012-03-15 08:42:37 -04003477 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003478 if (!mPersonList.isEmpty()) {
3479 mN.extras.putStringArray(EXTRA_PEOPLE,
3480 mPersonList.toArray(new String[mPersonList.size()]));
Dan Sandler0bf2ed82013-12-21 23:33:41 -06003481 }
Selim Cinek247fa012016-02-18 09:50:48 -08003482 if (mN.bigContentView != null || mN.contentView != null
3483 || mN.headsUpContentView != null) {
3484 mN.extras.putBoolean(EXTRA_CONTAINS_CUSTOM_VIEW, true);
3485 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003486 return mN;
Joe Onorato46439ce2010-11-19 13:56:21 -08003487 }
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003488
Julia Reynolds3b848122016-02-26 10:45:32 -05003489 /**
3490 * Creates a Builder from an existing notification so further changes can be made.
3491 * @param context The context for your application / activity.
3492 * @param n The notification to create a Builder from.
3493 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003494 public static Notification.Builder recoverBuilder(Context context, Notification n) {
Christoph Studer4600f9b2014-07-22 22:44:43 +02003495 // Re-create notification context so we can access app resources.
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003496 ApplicationInfo applicationInfo = n.extras.getParcelable(
3497 EXTRA_BUILDER_APPLICATION_INFO);
Christoph Studer4600f9b2014-07-22 22:44:43 +02003498 Context builderContext;
Julia Reynoldsda303542015-11-23 14:00:20 -05003499 if (applicationInfo != null) {
3500 try {
3501 builderContext = context.createApplicationContext(applicationInfo,
3502 Context.CONTEXT_RESTRICTED);
3503 } catch (NameNotFoundException e) {
3504 Log.e(TAG, "ApplicationInfo " + applicationInfo + " not found");
3505 builderContext = context; // try with our context
3506 }
3507 } else {
3508 builderContext = context; // try with given context
Christoph Studer4600f9b2014-07-22 22:44:43 +02003509 }
3510
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003511 return new Builder(builderContext, n);
Christoph Studer4600f9b2014-07-22 22:44:43 +02003512 }
3513
3514 private static Class<? extends Style> getNotificationStyleClass(String templateClass) {
Selim Cinek593610c2016-02-16 18:42:57 -08003515 Class<? extends Style>[] classes = new Class[] {
3516 BigTextStyle.class, BigPictureStyle.class, InboxStyle.class, MediaStyle.class,
Selim Cinek03eb3b72016-02-18 10:39:45 -08003517 DecoratedCustomViewStyle.class, DecoratedMediaCustomViewStyle.class };
Christoph Studer4600f9b2014-07-22 22:44:43 +02003518 for (Class<? extends Style> innerClass : classes) {
3519 if (templateClass.equals(innerClass.getName())) {
3520 return innerClass;
3521 }
3522 }
3523 return null;
3524 }
3525
Daniel Sandlerf45564e2013-04-15 15:05:08 -04003526 /**
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003527 * @deprecated Use {@link #build()} instead.
3528 */
3529 @Deprecated
3530 public Notification getNotification() {
3531 return build();
3532 }
3533
3534 /**
3535 * Combine all of the options that have been set and return a new {@link Notification}
3536 * object.
3537 */
3538 public Notification build() {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003539 // first, add any extras from the calling code
3540 if (mUserExtras != null) {
3541 mN.extras = getAllExtras();
Jorim Jaggia0d58ae2015-06-03 11:48:13 -07003542 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003543
3544 // lazy stuff from mContext; see comment in Builder(Context, Notification)
Julia Reynoldsda303542015-11-23 14:00:20 -05003545 Notification.addFieldsFromContext(mContext, mN);
Christoph Studer943aa672014-08-03 20:31:16 +02003546
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003547 buildUnstyled();
Daniel Sandlerf45564e2013-04-15 15:05:08 -04003548
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003549 if (mStyle != null) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003550 mStyle.buildStyled(mN);
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003551 }
Daniel Sandlerf45564e2013-04-15 15:05:08 -04003552
Adrian Roos5081c0d2016-02-26 16:04:19 -08003553 if (mContext.getApplicationInfo().targetSdkVersion < Build.VERSION_CODES.N
3554 && (mStyle == null || !mStyle.displayCustomViewInline())) {
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05003555 if (mN.contentView == null) {
Julia Reynolds3b848122016-02-26 10:45:32 -05003556 mN.contentView = createContentView();
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05003557 mN.extras.putInt(EXTRA_REBUILD_CONTENT_VIEW_ACTION_COUNT,
3558 mN.contentView.getSequenceNumber());
3559 }
3560 if (mN.bigContentView == null) {
Julia Reynolds3b848122016-02-26 10:45:32 -05003561 mN.bigContentView = createBigContentView();
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05003562 if (mN.bigContentView != null) {
3563 mN.extras.putInt(EXTRA_REBUILD_BIG_CONTENT_VIEW_ACTION_COUNT,
3564 mN.bigContentView.getSequenceNumber());
3565 }
3566 }
3567 if (mN.headsUpContentView == null) {
Julia Reynolds3b848122016-02-26 10:45:32 -05003568 mN.headsUpContentView = createHeadsUpContentView();
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05003569 if (mN.headsUpContentView != null) {
3570 mN.extras.putInt(EXTRA_REBUILD_HEADS_UP_CONTENT_VIEW_ACTION_COUNT,
3571 mN.headsUpContentView.getSequenceNumber());
3572 }
3573 }
3574 }
3575
Julia Reynolds4c0c2022016-02-02 15:11:59 -05003576 if ((mN.defaults & DEFAULT_LIGHTS) != 0) {
3577 mN.flags |= FLAG_SHOW_LIGHTS;
3578 }
3579
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003580 return mN;
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003581 }
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05003582
3583 /**
3584 * Apply this Builder to an existing {@link Notification} object.
3585 *
3586 * @hide
3587 */
3588 public Notification buildInto(Notification n) {
Daniel Sandler1a497d32013-04-18 14:52:45 -04003589 build().cloneInto(n, true);
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05003590 return n;
3591 }
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01003592
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05003593 /**
3594 * @hide
3595 */
3596 public static void stripForDelivery(Notification n) {
3597 String templateClass = n.extras.getString(EXTRA_TEMPLATE);
3598 if (TextUtils.isEmpty(templateClass)) {
3599 return;
3600 }
3601 // Only strip views for known Styles because we won't know how to
3602 // re-create them otherwise.
3603 if (getNotificationStyleClass(templateClass) == null) {
3604 return;
3605 }
3606 // Get rid of unmodified BuilderRemoteViews.
3607 if (n.contentView instanceof BuilderRemoteViews &&
3608 n.extras.getInt(EXTRA_REBUILD_CONTENT_VIEW_ACTION_COUNT, -1) ==
3609 n.contentView.getSequenceNumber()) {
3610 n.contentView = null;
3611 n.extras.remove(EXTRA_REBUILD_CONTENT_VIEW_ACTION_COUNT);
3612 }
3613 if (n.bigContentView instanceof BuilderRemoteViews &&
3614 n.extras.getInt(EXTRA_REBUILD_BIG_CONTENT_VIEW_ACTION_COUNT, -1) ==
3615 n.bigContentView.getSequenceNumber()) {
3616 n.bigContentView = null;
3617 n.extras.remove(EXTRA_REBUILD_BIG_CONTENT_VIEW_ACTION_COUNT);
3618 }
3619 if (n.headsUpContentView instanceof BuilderRemoteViews &&
3620 n.extras.getInt(EXTRA_REBUILD_HEADS_UP_CONTENT_VIEW_ACTION_COUNT, -1) ==
3621 n.headsUpContentView.getSequenceNumber()) {
3622 n.headsUpContentView = null;
3623 n.extras.remove(EXTRA_REBUILD_HEADS_UP_CONTENT_VIEW_ACTION_COUNT);
3624 }
3625 }
3626
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01003627 private int getBaseLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07003628 return R.layout.notification_template_material_base;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01003629 }
3630
3631 private int getBigBaseLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07003632 return R.layout.notification_template_material_big_base;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01003633 }
3634
3635 private int getBigPictureLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07003636 return R.layout.notification_template_material_big_picture;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01003637 }
3638
3639 private int getBigTextLayoutResource() {
Jorim Jaggi445d3c02014-08-19 22:33:42 +02003640 return R.layout.notification_template_material_big_text;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01003641 }
3642
3643 private int getInboxLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07003644 return R.layout.notification_template_material_inbox;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01003645 }
3646
3647 private int getActionLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07003648 return R.layout.notification_material_action;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01003649 }
3650
3651 private int getActionTombstoneLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07003652 return R.layout.notification_material_action_tombstone;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01003653 }
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003654 }
3655
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003656 /**
3657 * An object that can apply a rich notification style to a {@link Notification.Builder}
3658 * object.
3659 */
Griff Hazendfcb0802014-02-11 12:00:00 -08003660 public static abstract class Style {
Chris Wrend6297db2012-05-03 16:20:13 -04003661 private CharSequence mBigContentTitle;
Jorim Jaggi457a10d2014-09-08 16:18:23 +02003662
3663 /**
3664 * @hide
3665 */
3666 protected CharSequence mSummaryText = null;
3667
3668 /**
3669 * @hide
3670 */
3671 protected boolean mSummaryTextSet = false;
Chris Wrend6297db2012-05-03 16:20:13 -04003672
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003673 protected Builder mBuilder;
3674
Chris Wrend6297db2012-05-03 16:20:13 -04003675 /**
3676 * Overrides ContentTitle in the big form of the template.
3677 * This defaults to the value passed to setContentTitle().
3678 */
3679 protected void internalSetBigContentTitle(CharSequence title) {
3680 mBigContentTitle = title;
3681 }
3682
3683 /**
3684 * Set the first line of text after the detail section in the big form of the template.
3685 */
3686 protected void internalSetSummaryText(CharSequence cs) {
3687 mSummaryText = cs;
Daniel Sandler619738c2012-06-07 16:33:08 -04003688 mSummaryTextSet = true;
Chris Wrend6297db2012-05-03 16:20:13 -04003689 }
3690
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003691 public void setBuilder(Builder builder) {
3692 if (mBuilder != builder) {
3693 mBuilder = builder;
Daniel Sandlerc08dea22012-06-28 08:35:24 -07003694 if (mBuilder != null) {
3695 mBuilder.setStyle(this);
3696 }
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003697 }
3698 }
3699
Chris Wrend6297db2012-05-03 16:20:13 -04003700 protected void checkBuilder() {
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003701 if (mBuilder == null) {
3702 throw new IllegalArgumentException("Style requires a valid Builder object");
3703 }
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003704 }
Chris Wrend6297db2012-05-03 16:20:13 -04003705
3706 protected RemoteViews getStandardView(int layoutId) {
3707 checkBuilder();
3708
Christoph Studer4600f9b2014-07-22 22:44:43 +02003709 // Nasty.
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003710 CharSequence oldBuilderContentTitle =
3711 mBuilder.getAllExtras().getCharSequence(EXTRA_TITLE);
Chris Wrend6297db2012-05-03 16:20:13 -04003712 if (mBigContentTitle != null) {
3713 mBuilder.setContentTitle(mBigContentTitle);
3714 }
3715
Chris Wrend6297db2012-05-03 16:20:13 -04003716 RemoteViews contentView = mBuilder.applyStandardTemplateWithActions(layoutId);
3717
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003718 mBuilder.getAllExtras().putCharSequence(EXTRA_TITLE, oldBuilderContentTitle);
Christoph Studer4600f9b2014-07-22 22:44:43 +02003719
Chris Wrend6297db2012-05-03 16:20:13 -04003720 if (mBigContentTitle != null && mBigContentTitle.equals("")) {
3721 contentView.setViewVisibility(R.id.line1, View.GONE);
Chris Wren67dc9a02012-05-16 01:03:20 -04003722 } else {
3723 contentView.setViewVisibility(R.id.line1, View.VISIBLE);
Chris Wrend6297db2012-05-03 16:20:13 -04003724 }
3725
Chris Wrend6297db2012-05-03 16:20:13 -04003726 return contentView;
3727 }
3728
Daniel Sandlerf45564e2013-04-15 15:05:08 -04003729 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003730 * Construct a Style-specific RemoteViews for the final 1U notification layout.
3731 * The default implementation has nothing additional to add.
3732 * @hide
3733 */
3734 public RemoteViews makeContentView() {
3735 return null;
3736 }
3737
3738 /**
3739 * Construct a Style-specific RemoteViews for the final big notification layout.
3740 * @hide
3741 */
3742 public RemoteViews makeBigContentView() {
3743 return null;
3744 }
3745
3746 /**
3747 * Construct a Style-specific RemoteViews for the final HUN layout.
3748 * @hide
3749 */
3750 public RemoteViews makeHeadsUpContentView() {
3751 return null;
3752 }
3753
3754 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003755 * Apply any style-specific extras to this notification before shipping it out.
Daniel Sandlerf45564e2013-04-15 15:05:08 -04003756 * @hide
3757 */
3758 public void addExtras(Bundle extras) {
3759 if (mSummaryTextSet) {
3760 extras.putCharSequence(EXTRA_SUMMARY_TEXT, mSummaryText);
3761 }
3762 if (mBigContentTitle != null) {
3763 extras.putCharSequence(EXTRA_TITLE_BIG, mBigContentTitle);
3764 }
Chris Wren91ad5632013-06-05 15:05:57 -04003765 extras.putString(EXTRA_TEMPLATE, this.getClass().getName());
Daniel Sandlerf45564e2013-04-15 15:05:08 -04003766 }
3767
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04003768 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003769 * Reconstruct the internal state of this Style object from extras.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04003770 * @hide
3771 */
Christoph Studer4600f9b2014-07-22 22:44:43 +02003772 protected void restoreFromExtras(Bundle extras) {
3773 if (extras.containsKey(EXTRA_SUMMARY_TEXT)) {
3774 mSummaryText = extras.getCharSequence(EXTRA_SUMMARY_TEXT);
3775 mSummaryTextSet = true;
3776 }
3777 if (extras.containsKey(EXTRA_TITLE_BIG)) {
3778 mBigContentTitle = extras.getCharSequence(EXTRA_TITLE_BIG);
3779 }
3780 }
3781
3782
3783 /**
3784 * @hide
3785 */
3786 public Notification buildStyled(Notification wip) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003787 addExtras(wip.extras);
Christoph Studer4600f9b2014-07-22 22:44:43 +02003788 return wip;
3789 }
3790
Daniel Sandler0ec46202015-06-24 01:27:05 -04003791 /**
3792 * @hide
3793 */
Jorim Jaggia0d58ae2015-06-03 11:48:13 -07003794 public void purgeResources() {}
3795
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04003796 /**
3797 * Calls {@link android.app.Notification.Builder#build()} on the Builder this Style is
3798 * attached to.
3799 *
3800 * @return the fully constructed Notification.
3801 */
3802 public Notification build() {
3803 checkBuilder();
3804 return mBuilder.build();
3805 }
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02003806
3807 /**
3808 * @hide
3809 * @return true if the style positions the progress bar on the second line; false if the
3810 * style hides the progress bar
3811 */
3812 protected boolean hasProgress() {
3813 return true;
3814 }
Selim Cinek03d0d652015-11-13 13:18:09 -05003815
3816 /**
3817 * @hide
3818 * @return Whether we should put the summary be put into the notification header
3819 */
3820 public boolean hasSummaryInHeader() {
3821 return true;
3822 }
Selim Cinek593610c2016-02-16 18:42:57 -08003823
3824 /**
3825 * @hide
3826 * @return Whether custom content views are displayed inline in the style
3827 */
3828 public boolean displayCustomViewInline() {
3829 return false;
3830 }
Joe Onorato46439ce2010-11-19 13:56:21 -08003831 }
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003832
3833 /**
Daniel Sandler4dfbe832012-04-11 14:51:46 -04003834 * Helper class for generating large-format notifications that include a large image attachment.
Joe Malin8d40d042012-11-05 11:36:40 -08003835 *
Robert Ly91c5ce32014-06-08 15:37:00 -07003836 * Here's how you'd set the <code>BigPictureStyle</code> on a notification:
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003837 * <pre class="prettyprint">
Robert Ly91c5ce32014-06-08 15:37:00 -07003838 * Notification notif = new Notification.Builder(mContext)
3839 * .setContentTitle(&quot;New photo from &quot; + sender.toString())
3840 * .setContentText(subject)
3841 * .setSmallIcon(R.drawable.new_post)
3842 * .setLargeIcon(aBitmap)
3843 * .setStyle(new Notification.BigPictureStyle()
3844 * .bigPicture(aBigBitmap))
3845 * .build();
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003846 * </pre>
Joe Malin8d40d042012-11-05 11:36:40 -08003847 *
Daniel Sandler4dfbe832012-04-11 14:51:46 -04003848 * @see Notification#bigContentView
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003849 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003850 public static class BigPictureStyle extends Style {
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003851 private Bitmap mPicture;
Dan Sandlerd63f9322015-05-06 15:18:49 -04003852 private Icon mBigLargeIcon;
Chris Wren3745a3d2012-05-22 15:11:52 -04003853 private boolean mBigLargeIconSet = false;
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003854
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003855 public BigPictureStyle() {
3856 }
3857
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003858 public BigPictureStyle(Builder builder) {
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003859 setBuilder(builder);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003860 }
3861
Chris Wrend6297db2012-05-03 16:20:13 -04003862 /**
3863 * Overrides ContentTitle in the big form of the template.
3864 * This defaults to the value passed to setContentTitle().
3865 */
3866 public BigPictureStyle setBigContentTitle(CharSequence title) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04003867 internalSetBigContentTitle(safeCharSequence(title));
Chris Wrend6297db2012-05-03 16:20:13 -04003868 return this;
3869 }
3870
3871 /**
3872 * Set the first line of text after the detail section in the big form of the template.
3873 */
3874 public BigPictureStyle setSummaryText(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04003875 internalSetSummaryText(safeCharSequence(cs));
Chris Wrend6297db2012-05-03 16:20:13 -04003876 return this;
3877 }
3878
Chris Wren0bd664d2012-08-01 13:56:56 -04003879 /**
3880 * Provide the bitmap to be used as the payload for the BigPicture notification.
3881 */
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003882 public BigPictureStyle bigPicture(Bitmap b) {
3883 mPicture = b;
3884 return this;
3885 }
3886
Chris Wren3745a3d2012-05-22 15:11:52 -04003887 /**
Chris Wren3745a3d2012-05-22 15:11:52 -04003888 * Override the large icon when the big notification is shown.
3889 */
3890 public BigPictureStyle bigLargeIcon(Bitmap b) {
Dan Sandlerd63f9322015-05-06 15:18:49 -04003891 return bigLargeIcon(b != null ? Icon.createWithBitmap(b) : null);
3892 }
3893
3894 /**
3895 * Override the large icon when the big notification is shown.
3896 */
3897 public BigPictureStyle bigLargeIcon(Icon icon) {
Chris Wren3745a3d2012-05-22 15:11:52 -04003898 mBigLargeIconSet = true;
Dan Sandlerd63f9322015-05-06 15:18:49 -04003899 mBigLargeIcon = icon;
Chris Wren3745a3d2012-05-22 15:11:52 -04003900 return this;
3901 }
3902
Riley Andrews0394a0c2015-11-03 23:36:52 -08003903 /** @hide */
3904 public static final int MIN_ASHMEM_BITMAP_SIZE = 128 * (1 << 10);
3905
Daniel Sandler0ec46202015-06-24 01:27:05 -04003906 /**
3907 * @hide
3908 */
Jorim Jaggia0d58ae2015-06-03 11:48:13 -07003909 @Override
3910 public void purgeResources() {
3911 super.purgeResources();
Riley Andrews8cee7c12015-11-01 23:36:04 -08003912 if (mPicture != null &&
3913 mPicture.isMutable() &&
Riley Andrews0394a0c2015-11-03 23:36:52 -08003914 mPicture.getAllocationByteCount() >= MIN_ASHMEM_BITMAP_SIZE) {
Jorim Jaggia0d58ae2015-06-03 11:48:13 -07003915 mPicture = mPicture.createAshmemBitmap();
3916 }
3917 if (mBigLargeIcon != null) {
3918 mBigLargeIcon.convertToAshmem();
3919 }
3920 }
Christoph Studer5c510ee2014-12-15 16:32:27 +01003921
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003922 /**
3923 * @hide
3924 */
3925 public RemoteViews makeBigContentView() {
3926 // Replace mN.mLargeIcon with mBigLargeIcon if mBigLargeIconSet
Christoph Studer5c510ee2014-12-15 16:32:27 +01003927 // This covers the following cases:
3928 // 1. mBigLargeIconSet -> mBigLargeIcon (null or non-null) applies, overrides
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003929 // mN.mLargeIcon
3930 // 2. !mBigLargeIconSet -> mN.mLargeIcon applies
Dan Sandlerd63f9322015-05-06 15:18:49 -04003931 Icon oldLargeIcon = null;
Christoph Studer5c510ee2014-12-15 16:32:27 +01003932 if (mBigLargeIconSet) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003933 oldLargeIcon = mBuilder.mN.mLargeIcon;
3934 mBuilder.mN.mLargeIcon = mBigLargeIcon;
Christoph Studer5c510ee2014-12-15 16:32:27 +01003935 }
3936
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01003937 RemoteViews contentView = getStandardView(mBuilder.getBigPictureLayoutResource());
Selim Cinek03d0d652015-11-13 13:18:09 -05003938 if (mSummaryTextSet) {
3939 contentView.setTextViewText(R.id.text, mBuilder.processLegacyText(mSummaryText));
Selim Cinekc848c3a2016-01-13 15:27:30 -08003940 contentView.setViewVisibility(R.id.text, View.VISIBLE);
Selim Cinek03d0d652015-11-13 13:18:09 -05003941 }
Selim Cinek860b6da2015-12-16 19:02:19 -08003942 mBuilder.setContentMinHeight(contentView, mBuilder.mN.mLargeIcon != null);
Selim Cinek53e64a42015-11-16 10:40:56 -08003943
Christoph Studer5c510ee2014-12-15 16:32:27 +01003944 if (mBigLargeIconSet) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003945 mBuilder.mN.mLargeIcon = oldLargeIcon;
Christoph Studer5c510ee2014-12-15 16:32:27 +01003946 }
3947
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003948 contentView.setImageViewBitmap(R.id.big_picture, mPicture);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003949 return contentView;
3950 }
3951
Daniel Sandlerf45564e2013-04-15 15:05:08 -04003952 /**
3953 * @hide
3954 */
3955 public void addExtras(Bundle extras) {
3956 super.addExtras(extras);
3957
3958 if (mBigLargeIconSet) {
3959 extras.putParcelable(EXTRA_LARGE_ICON_BIG, mBigLargeIcon);
3960 }
3961 extras.putParcelable(EXTRA_PICTURE, mPicture);
3962 }
3963
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04003964 /**
3965 * @hide
3966 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003967 @Override
Christoph Studer4600f9b2014-07-22 22:44:43 +02003968 protected void restoreFromExtras(Bundle extras) {
3969 super.restoreFromExtras(extras);
3970
3971 if (extras.containsKey(EXTRA_LARGE_ICON_BIG)) {
Christoph Studer5c510ee2014-12-15 16:32:27 +01003972 mBigLargeIconSet = true;
Christoph Studer4600f9b2014-07-22 22:44:43 +02003973 mBigLargeIcon = extras.getParcelable(EXTRA_LARGE_ICON_BIG);
Chris Wren3745a3d2012-05-22 15:11:52 -04003974 }
Christoph Studer4600f9b2014-07-22 22:44:43 +02003975 mPicture = extras.getParcelable(EXTRA_PICTURE);
3976 }
Selim Cinek03d0d652015-11-13 13:18:09 -05003977
3978 /**
3979 * @hide
3980 */
3981 @Override
3982 public boolean hasSummaryInHeader() {
3983 return false;
3984 }
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003985 }
3986
3987 /**
Daniel Sandler4dfbe832012-04-11 14:51:46 -04003988 * Helper class for generating large-format notifications that include a lot of text.
Joe Malin8d40d042012-11-05 11:36:40 -08003989 *
Robert Ly91c5ce32014-06-08 15:37:00 -07003990 * Here's how you'd set the <code>BigTextStyle</code> on a notification:
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003991 * <pre class="prettyprint">
Robert Ly91c5ce32014-06-08 15:37:00 -07003992 * Notification notif = new Notification.Builder(mContext)
3993 * .setContentTitle(&quot;New mail from &quot; + sender.toString())
3994 * .setContentText(subject)
3995 * .setSmallIcon(R.drawable.new_mail)
3996 * .setLargeIcon(aBitmap)
3997 * .setStyle(new Notification.BigTextStyle()
3998 * .bigText(aVeryLongString))
3999 * .build();
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004000 * </pre>
Joe Malin8d40d042012-11-05 11:36:40 -08004001 *
Daniel Sandler4dfbe832012-04-11 14:51:46 -04004002 * @see Notification#bigContentView
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004003 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004004 public static class BigTextStyle extends Style {
Jorim Jaggi457a10d2014-09-08 16:18:23 +02004005
4006 private static final int MAX_LINES = 13;
Selim Cinek3a2c4b92015-12-17 17:01:17 -08004007 private static final int LINES_CONSUMED_BY_ACTIONS = 4;
Jorim Jaggi457a10d2014-09-08 16:18:23 +02004008
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004009 private CharSequence mBigText;
4010
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004011 public BigTextStyle() {
4012 }
4013
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004014 public BigTextStyle(Builder builder) {
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004015 setBuilder(builder);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004016 }
4017
Chris Wrend6297db2012-05-03 16:20:13 -04004018 /**
4019 * Overrides ContentTitle in the big form of the template.
4020 * This defaults to the value passed to setContentTitle().
4021 */
4022 public BigTextStyle setBigContentTitle(CharSequence title) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04004023 internalSetBigContentTitle(safeCharSequence(title));
Chris Wrend6297db2012-05-03 16:20:13 -04004024 return this;
4025 }
4026
4027 /**
4028 * Set the first line of text after the detail section in the big form of the template.
4029 */
4030 public BigTextStyle setSummaryText(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04004031 internalSetSummaryText(safeCharSequence(cs));
Chris Wrend6297db2012-05-03 16:20:13 -04004032 return this;
4033 }
4034
Chris Wren0bd664d2012-08-01 13:56:56 -04004035 /**
4036 * Provide the longer text to be displayed in the big form of the
4037 * template in place of the content text.
4038 */
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004039 public BigTextStyle bigText(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04004040 mBigText = safeCharSequence(cs);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004041 return this;
4042 }
4043
Daniel Sandlerf45564e2013-04-15 15:05:08 -04004044 /**
4045 * @hide
4046 */
4047 public void addExtras(Bundle extras) {
4048 super.addExtras(extras);
4049
Christoph Studer4600f9b2014-07-22 22:44:43 +02004050 extras.putCharSequence(EXTRA_BIG_TEXT, mBigText);
4051 }
4052
4053 /**
4054 * @hide
4055 */
4056 @Override
4057 protected void restoreFromExtras(Bundle extras) {
4058 super.restoreFromExtras(extras);
4059
4060 mBigText = extras.getCharSequence(EXTRA_BIG_TEXT);
Daniel Sandlerf45564e2013-04-15 15:05:08 -04004061 }
4062
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004063 /**
4064 * @hide
4065 */
4066 public RemoteViews makeBigContentView() {
Christoph Studer4600f9b2014-07-22 22:44:43 +02004067
4068 // Nasty
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004069 CharSequence oldBuilderContentText =
4070 mBuilder.getAllExtras().getCharSequence(EXTRA_TEXT);
4071 mBuilder.getAllExtras().putCharSequence(EXTRA_TEXT, null);
Daniel Sandler916ad912012-06-13 12:17:07 -04004072
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01004073 RemoteViews contentView = getStandardView(mBuilder.getBigTextLayoutResource());
Joe Malin8d40d042012-11-05 11:36:40 -08004074
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004075 mBuilder.getAllExtras().putCharSequence(EXTRA_TEXT, oldBuilderContentText);
Christoph Studer4600f9b2014-07-22 22:44:43 +02004076
Selim Cinek3a2c4b92015-12-17 17:01:17 -08004077 CharSequence bigTextText = mBuilder.processLegacyText(mBigText);
4078 contentView.setTextViewText(R.id.big_text, bigTextText);
4079 contentView.setViewVisibility(R.id.big_text,
4080 TextUtils.isEmpty(bigTextText) ? View.GONE : View.VISIBLE);
Jorim Jaggi457a10d2014-09-08 16:18:23 +02004081 contentView.setInt(R.id.big_text, "setMaxLines", calculateMaxLines());
Selim Cinek4fb12d32015-11-19 18:10:48 -08004082 contentView.setBoolean(R.id.big_text, "setHasImage", mBuilder.mN.mLargeIcon != null);
4083
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004084 return contentView;
4085 }
4086
Jorim Jaggi457a10d2014-09-08 16:18:23 +02004087 private int calculateMaxLines() {
4088 int lineCount = MAX_LINES;
4089 boolean hasActions = mBuilder.mActions.size() > 0;
Jorim Jaggi457a10d2014-09-08 16:18:23 +02004090 if (hasActions) {
4091 lineCount -= LINES_CONSUMED_BY_ACTIONS;
4092 }
Jorim Jaggi457a10d2014-09-08 16:18:23 +02004093 return lineCount;
4094 }
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004095 }
Daniel Sandler879c5e02012-04-17 16:46:51 -04004096
4097 /**
4098 * Helper class for generating large-format notifications that include a list of (up to 5) strings.
Joe Malin8d40d042012-11-05 11:36:40 -08004099 *
Robert Ly91c5ce32014-06-08 15:37:00 -07004100 * Here's how you'd set the <code>InboxStyle</code> on a notification:
Daniel Sandler879c5e02012-04-17 16:46:51 -04004101 * <pre class="prettyprint">
Robert Ly91c5ce32014-06-08 15:37:00 -07004102 * Notification notif = new Notification.Builder(mContext)
4103 * .setContentTitle(&quot;5 New mails from &quot; + sender.toString())
4104 * .setContentText(subject)
4105 * .setSmallIcon(R.drawable.new_mail)
4106 * .setLargeIcon(aBitmap)
4107 * .setStyle(new Notification.InboxStyle()
4108 * .addLine(str1)
4109 * .addLine(str2)
4110 * .setContentTitle(&quot;&quot;)
4111 * .setSummaryText(&quot;+3 more&quot;))
4112 * .build();
Daniel Sandler879c5e02012-04-17 16:46:51 -04004113 * </pre>
Joe Malin8d40d042012-11-05 11:36:40 -08004114 *
Daniel Sandler879c5e02012-04-17 16:46:51 -04004115 * @see Notification#bigContentView
4116 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004117 public static class InboxStyle extends Style {
Daniel Sandler879c5e02012-04-17 16:46:51 -04004118 private ArrayList<CharSequence> mTexts = new ArrayList<CharSequence>(5);
4119
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004120 public InboxStyle() {
4121 }
4122
Daniel Sandler879c5e02012-04-17 16:46:51 -04004123 public InboxStyle(Builder builder) {
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004124 setBuilder(builder);
Daniel Sandler879c5e02012-04-17 16:46:51 -04004125 }
4126
Chris Wrend6297db2012-05-03 16:20:13 -04004127 /**
4128 * Overrides ContentTitle in the big form of the template.
4129 * This defaults to the value passed to setContentTitle().
4130 */
4131 public InboxStyle setBigContentTitle(CharSequence title) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04004132 internalSetBigContentTitle(safeCharSequence(title));
Chris Wrend6297db2012-05-03 16:20:13 -04004133 return this;
4134 }
4135
4136 /**
4137 * Set the first line of text after the detail section in the big form of the template.
4138 */
4139 public InboxStyle setSummaryText(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04004140 internalSetSummaryText(safeCharSequence(cs));
Chris Wrend6297db2012-05-03 16:20:13 -04004141 return this;
4142 }
4143
Chris Wren0bd664d2012-08-01 13:56:56 -04004144 /**
4145 * Append a line to the digest section of the Inbox notification.
4146 */
Daniel Sandler879c5e02012-04-17 16:46:51 -04004147 public InboxStyle addLine(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04004148 mTexts.add(safeCharSequence(cs));
Daniel Sandler879c5e02012-04-17 16:46:51 -04004149 return this;
4150 }
4151
Daniel Sandlerf45564e2013-04-15 15:05:08 -04004152 /**
4153 * @hide
4154 */
4155 public void addExtras(Bundle extras) {
4156 super.addExtras(extras);
Christoph Studer4600f9b2014-07-22 22:44:43 +02004157
Daniel Sandlerf45564e2013-04-15 15:05:08 -04004158 CharSequence[] a = new CharSequence[mTexts.size()];
4159 extras.putCharSequenceArray(EXTRA_TEXT_LINES, mTexts.toArray(a));
4160 }
4161
Christoph Studer4600f9b2014-07-22 22:44:43 +02004162 /**
4163 * @hide
4164 */
4165 @Override
4166 protected void restoreFromExtras(Bundle extras) {
4167 super.restoreFromExtras(extras);
4168
4169 mTexts.clear();
4170 if (extras.containsKey(EXTRA_TEXT_LINES)) {
4171 Collections.addAll(mTexts, extras.getCharSequenceArray(EXTRA_TEXT_LINES));
4172 }
4173 }
4174
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004175 /**
4176 * @hide
4177 */
4178 public RemoteViews makeBigContentView() {
Selim Cinekc848c3a2016-01-13 15:27:30 -08004179 // Remove the content text so it disappears unless you have a summary
Christoph Studer4600f9b2014-07-22 22:44:43 +02004180 // Nasty
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004181 CharSequence oldBuilderContentText = mBuilder.mN.extras.getCharSequence(EXTRA_TEXT);
4182 mBuilder.getAllExtras().putCharSequence(EXTRA_TEXT, null);
Christoph Studer4600f9b2014-07-22 22:44:43 +02004183
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01004184 RemoteViews contentView = getStandardView(mBuilder.getInboxLayoutResource());
Daniel Sandler619738c2012-06-07 16:33:08 -04004185
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004186 mBuilder.getAllExtras().putCharSequence(EXTRA_TEXT, oldBuilderContentText);
Christoph Studer4600f9b2014-07-22 22:44:43 +02004187
Chris Wrend6297db2012-05-03 16:20:13 -04004188 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 -04004189 R.id.inbox_text4, R.id.inbox_text5, R.id.inbox_text6};
Chris Wrend6297db2012-05-03 16:20:13 -04004190
Chris Wren4ed80d52012-05-17 09:30:03 -04004191 // Make sure all rows are gone in case we reuse a view.
4192 for (int rowId : rowIds) {
4193 contentView.setViewVisibility(rowId, View.GONE);
4194 }
4195
Jorim Jaggi445d3c02014-08-19 22:33:42 +02004196 final boolean largeText =
4197 mBuilder.mContext.getResources().getConfiguration().fontScale > 1f;
4198 final float subTextSize = mBuilder.mContext.getResources().getDimensionPixelSize(
4199 R.dimen.notification_subtext_size);
Daniel Sandler879c5e02012-04-17 16:46:51 -04004200 int i=0;
Selim Cinek9d9fc6e2015-11-12 15:49:14 -05004201 final float density = mBuilder.mContext.getResources().getDisplayMetrics().density;
4202 int topPadding = (int) (5 * density);
4203 int bottomPadding = (int) (13 * density);
Selim Cinek247fa012016-02-18 09:50:48 -08004204 boolean first = true;
Daniel Sandler879c5e02012-04-17 16:46:51 -04004205 while (i < mTexts.size() && i < rowIds.length) {
4206 CharSequence str = mTexts.get(i);
4207 if (str != null && !str.equals("")) {
4208 contentView.setViewVisibility(rowIds[i], View.VISIBLE);
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01004209 contentView.setTextViewText(rowIds[i], mBuilder.processLegacyText(str));
Jorim Jaggi445d3c02014-08-19 22:33:42 +02004210 if (largeText) {
4211 contentView.setTextViewTextSize(rowIds[i], TypedValue.COMPLEX_UNIT_PX,
4212 subTextSize);
4213 }
Selim Cinek9d9fc6e2015-11-12 15:49:14 -05004214 contentView.setViewPadding(rowIds[i], 0, topPadding, 0,
4215 i == rowIds.length - 1 || i == mTexts.size() - 1 ? bottomPadding : 0);
Selim Cinek247fa012016-02-18 09:50:48 -08004216 handleInboxImageMargin(contentView, rowIds[i], first);
4217 first = false;
Daniel Sandler879c5e02012-04-17 16:46:51 -04004218 }
4219 i++;
4220 }
Kenny Guy98193ea2014-07-24 19:54:37 +01004221
Selim Cinek1e0bf612015-11-20 15:57:26 -08004222
Daniel Sandler879c5e02012-04-17 16:46:51 -04004223 return contentView;
4224 }
Selim Cinek1e0bf612015-11-20 15:57:26 -08004225
Selim Cinek247fa012016-02-18 09:50:48 -08004226 private void handleInboxImageMargin(RemoteViews contentView, int id, boolean first) {
Selim Cinek1e0bf612015-11-20 15:57:26 -08004227 int endMargin = 0;
Selim Cinek247fa012016-02-18 09:50:48 -08004228 if (first) {
4229 final int max = mBuilder.mN.extras.getInt(EXTRA_PROGRESS_MAX, 0);
4230 final boolean ind = mBuilder.mN.extras.getBoolean(EXTRA_PROGRESS_INDETERMINATE);
4231 boolean hasProgress = max != 0 || ind;
4232 if (mBuilder.mN.mLargeIcon != null && !hasProgress) {
4233 endMargin = mBuilder.mContext.getResources().getDimensionPixelSize(
4234 R.dimen.notification_content_picture_margin);
4235 }
Selim Cinek1e0bf612015-11-20 15:57:26 -08004236 }
4237 contentView.setViewLayoutMarginEnd(id, endMargin);
4238 }
Daniel Sandler879c5e02012-04-17 16:46:51 -04004239 }
Dan Sandler842dd772014-05-15 09:36:47 -04004240
4241 /**
4242 * Notification style for media playback notifications.
4243 *
4244 * In the expanded form, {@link Notification#bigContentView}, up to 5
4245 * {@link Notification.Action}s specified with
Dan Sandler86647982015-05-13 23:41:13 -04004246 * {@link Notification.Builder#addAction(Action) addAction} will be
Dan Sandler842dd772014-05-15 09:36:47 -04004247 * shown as icon-only pushbuttons, suitable for transport controls. The Bitmap given to
4248 * {@link Notification.Builder#setLargeIcon(android.graphics.Bitmap) setLargeIcon()} will be
4249 * treated as album artwork.
4250 *
4251 * Unlike the other styles provided here, MediaStyle can also modify the standard-size
4252 * {@link Notification#contentView}; by providing action indices to
Christoph Studerfde6f4d2014-12-12 13:23:26 +01004253 * {@link #setShowActionsInCompactView(int...)} you can promote up to 3 actions to be displayed
Dan Sandler842dd772014-05-15 09:36:47 -04004254 * in the standard view alongside the usual content.
4255 *
Bryan Mawhinney6be8de32014-07-18 10:35:12 +01004256 * Notifications created with MediaStyle will have their category set to
4257 * {@link Notification#CATEGORY_TRANSPORT CATEGORY_TRANSPORT} unless you set a different
4258 * category using {@link Notification.Builder#setCategory(String) setCategory()}.
4259 *
Jeff Browndba34ba2014-06-24 20:46:03 -07004260 * Finally, if you attach a {@link android.media.session.MediaSession.Token} using
4261 * {@link android.app.Notification.MediaStyle#setMediaSession(MediaSession.Token)},
Dan Sandler842dd772014-05-15 09:36:47 -04004262 * the System UI can identify this as a notification representing an active media session
4263 * and respond accordingly (by showing album artwork in the lockscreen, for example).
4264 *
4265 * To use this style with your Notification, feed it to
4266 * {@link Notification.Builder#setStyle(android.app.Notification.Style)} like so:
4267 * <pre class="prettyprint">
4268 * Notification noti = new Notification.Builder()
4269 * .setSmallIcon(R.drawable.ic_stat_player)
Christoph Studere935fe92014-11-24 14:18:06 +01004270 * .setContentTitle(&quot;Track title&quot;)
4271 * .setContentText(&quot;Artist - Album&quot;)
4272 * .setLargeIcon(albumArtBitmap))
Bryan Mawhinney6be8de32014-07-18 10:35:12 +01004273 * .setStyle(<b>new Notification.MediaStyle()</b>
4274 * .setMediaSession(mySession))
Dan Sandler842dd772014-05-15 09:36:47 -04004275 * .build();
4276 * </pre>
4277 *
4278 * @see Notification#bigContentView
4279 */
4280 public static class MediaStyle extends Style {
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02004281 static final int MAX_MEDIA_BUTTONS_IN_COMPACT = 3;
Dan Sandler842dd772014-05-15 09:36:47 -04004282 static final int MAX_MEDIA_BUTTONS = 5;
4283
4284 private int[] mActionsToShowInCompact = null;
Jeff Browndba34ba2014-06-24 20:46:03 -07004285 private MediaSession.Token mToken;
Dan Sandler842dd772014-05-15 09:36:47 -04004286
4287 public MediaStyle() {
4288 }
4289
4290 public MediaStyle(Builder builder) {
4291 setBuilder(builder);
4292 }
4293
4294 /**
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02004295 * 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 -04004296 * notification view.
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02004297 *
4298 * @param actions the indices of the actions to show in the compact notification view
Dan Sandler842dd772014-05-15 09:36:47 -04004299 */
4300 public MediaStyle setShowActionsInCompactView(int...actions) {
4301 mActionsToShowInCompact = actions;
4302 return this;
4303 }
4304
4305 /**
Jeff Browndba34ba2014-06-24 20:46:03 -07004306 * Attach a {@link android.media.session.MediaSession.Token} to this Notification
4307 * to provide additional playback information and control to the SystemUI.
Dan Sandler842dd772014-05-15 09:36:47 -04004308 */
Jeff Browndba34ba2014-06-24 20:46:03 -07004309 public MediaStyle setMediaSession(MediaSession.Token token) {
Dan Sandler842dd772014-05-15 09:36:47 -04004310 mToken = token;
4311 return this;
4312 }
4313
Christoph Studer4600f9b2014-07-22 22:44:43 +02004314 /**
4315 * @hide
4316 */
Dan Sandler842dd772014-05-15 09:36:47 -04004317 @Override
4318 public Notification buildStyled(Notification wip) {
Christoph Studer4600f9b2014-07-22 22:44:43 +02004319 super.buildStyled(wip);
Bryan Mawhinney6be8de32014-07-18 10:35:12 +01004320 if (wip.category == null) {
4321 wip.category = Notification.CATEGORY_TRANSPORT;
4322 }
Dan Sandler842dd772014-05-15 09:36:47 -04004323 return wip;
4324 }
4325
Christoph Studer4600f9b2014-07-22 22:44:43 +02004326 /**
4327 * @hide
4328 */
4329 @Override
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004330 public RemoteViews makeContentView() {
4331 return makeMediaContentView();
Christoph Studer4600f9b2014-07-22 22:44:43 +02004332 }
4333
4334 /**
4335 * @hide
4336 */
4337 @Override
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004338 public RemoteViews makeBigContentView() {
4339 return makeMediaBigContentView();
Christoph Studer4600f9b2014-07-22 22:44:43 +02004340 }
4341
Selim Cinekcc10bfb2016-02-10 16:24:21 -08004342 /**
4343 * @hide
4344 */
4345 @Override
4346 public RemoteViews makeHeadsUpContentView() {
4347 RemoteViews expanded = makeMediaBigContentView();
4348 return expanded != null ? expanded : makeMediaContentView();
4349 }
4350
Dan Sandler842dd772014-05-15 09:36:47 -04004351 /** @hide */
4352 @Override
4353 public void addExtras(Bundle extras) {
4354 super.addExtras(extras);
4355
4356 if (mToken != null) {
4357 extras.putParcelable(EXTRA_MEDIA_SESSION, mToken);
4358 }
Bryan Mawhinneye191f902014-07-22 12:50:09 +01004359 if (mActionsToShowInCompact != null) {
4360 extras.putIntArray(EXTRA_COMPACT_ACTIONS, mActionsToShowInCompact);
4361 }
Dan Sandler842dd772014-05-15 09:36:47 -04004362 }
4363
Christoph Studer4600f9b2014-07-22 22:44:43 +02004364 /**
4365 * @hide
4366 */
4367 @Override
4368 protected void restoreFromExtras(Bundle extras) {
4369 super.restoreFromExtras(extras);
4370
4371 if (extras.containsKey(EXTRA_MEDIA_SESSION)) {
4372 mToken = extras.getParcelable(EXTRA_MEDIA_SESSION);
4373 }
4374 if (extras.containsKey(EXTRA_COMPACT_ACTIONS)) {
4375 mActionsToShowInCompact = extras.getIntArray(EXTRA_COMPACT_ACTIONS);
4376 }
4377 }
4378
Selim Cinek5bf069a2015-11-10 19:14:27 -05004379 private RemoteViews generateMediaActionButton(Action action, int color) {
Dan Sandler842dd772014-05-15 09:36:47 -04004380 final boolean tombstone = (action.actionIntent == null);
Selim Cinekf33b1112015-07-15 17:45:11 -07004381 RemoteViews button = new BuilderRemoteViews(mBuilder.mContext.getApplicationInfo(),
Alan Viverette3cb07a462014-06-06 14:19:53 -07004382 R.layout.notification_material_media_action);
Dan Sandler68079d52015-07-22 10:45:30 -04004383 button.setImageViewIcon(R.id.action0, action.getIcon());
Selim Cinek5bf069a2015-11-10 19:14:27 -05004384 button.setDrawableParameters(R.id.action0, false, -1, color, PorterDuff.Mode.SRC_ATOP,
4385 -1);
Dan Sandler842dd772014-05-15 09:36:47 -04004386 if (!tombstone) {
4387 button.setOnClickPendingIntent(R.id.action0, action.actionIntent);
4388 }
4389 button.setContentDescription(R.id.action0, action.title);
4390 return button;
4391 }
4392
4393 private RemoteViews makeMediaContentView() {
4394 RemoteViews view = mBuilder.applyStandardTemplate(
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02004395 R.layout.notification_template_material_media, false /* hasProgress */);
Dan Sandler842dd772014-05-15 09:36:47 -04004396
4397 final int numActions = mBuilder.mActions.size();
4398 final int N = mActionsToShowInCompact == null
4399 ? 0
4400 : Math.min(mActionsToShowInCompact.length, MAX_MEDIA_BUTTONS_IN_COMPACT);
4401 if (N > 0) {
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02004402 view.removeAllViews(com.android.internal.R.id.media_actions);
Dan Sandler842dd772014-05-15 09:36:47 -04004403 for (int i = 0; i < N; i++) {
4404 if (i >= numActions) {
4405 throw new IllegalArgumentException(String.format(
4406 "setShowActionsInCompactView: action %d out of bounds (max %d)",
4407 i, numActions - 1));
4408 }
4409
4410 final Action action = mBuilder.mActions.get(mActionsToShowInCompact[i]);
Selim Cinek5bf069a2015-11-10 19:14:27 -05004411 final RemoteViews button = generateMediaActionButton(action,
4412 mBuilder.resolveColor());
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02004413 view.addView(com.android.internal.R.id.media_actions, button);
Dan Sandler842dd772014-05-15 09:36:47 -04004414 }
4415 }
Selim Cinekfdc738f2016-01-27 20:04:27 -08004416 handleImage(view);
4417 // handle the content margin
Selim Cinek247fa012016-02-18 09:50:48 -08004418 int endMargin = mBuilder.mContext.getResources().getDimensionPixelSize(
4419 R.dimen.notification_content_margin_end);;
Selim Cinekfdc738f2016-01-27 20:04:27 -08004420 if (mBuilder.mN.mLargeIcon != null) {
Selim Cinek247fa012016-02-18 09:50:48 -08004421 endMargin += mBuilder.mContext.getResources().getDimensionPixelSize(
4422 R.dimen.notification_content_picture_margin);
Selim Cinekfdc738f2016-01-27 20:04:27 -08004423 }
4424 view.setViewLayoutMarginEnd(R.id.notification_main_column, endMargin);
Dan Sandler842dd772014-05-15 09:36:47 -04004425 return view;
4426 }
4427
4428 private RemoteViews makeMediaBigContentView() {
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02004429 final int actionCount = Math.min(mBuilder.mActions.size(), MAX_MEDIA_BUTTONS);
Selim Cinekcc10bfb2016-02-10 16:24:21 -08004430 // Dont add an expanded view if there is no more content to be revealed
4431 int actionsInCompact = mActionsToShowInCompact == null
4432 ? 0
4433 : Math.min(mActionsToShowInCompact.length, MAX_MEDIA_BUTTONS_IN_COMPACT);
4434 if (mBuilder.mN.mLargeIcon == null && actionCount <= actionsInCompact) {
4435 return null;
4436 }
Selim Cinek5bf069a2015-11-10 19:14:27 -05004437 RemoteViews big = mBuilder.applyStandardTemplate(
4438 R.layout.notification_template_material_big_media,
4439 false);
Dan Sandler842dd772014-05-15 09:36:47 -04004440
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02004441 if (actionCount > 0) {
4442 big.removeAllViews(com.android.internal.R.id.media_actions);
4443 for (int i = 0; i < actionCount; i++) {
Selim Cinek5bf069a2015-11-10 19:14:27 -05004444 final RemoteViews button = generateMediaActionButton(mBuilder.mActions.get(i),
4445 mBuilder.resolveColor());
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02004446 big.addView(com.android.internal.R.id.media_actions, button);
Dan Sandler842dd772014-05-15 09:36:47 -04004447 }
4448 }
Selim Cinek5bf069a2015-11-10 19:14:27 -05004449 handleImage(big);
Dan Sandler842dd772014-05-15 09:36:47 -04004450 return big;
4451 }
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02004452
Selim Cinek5bf069a2015-11-10 19:14:27 -05004453 private void handleImage(RemoteViews contentView) {
4454 if (mBuilder.mN.mLargeIcon != null) {
4455 contentView.setViewLayoutMarginEnd(R.id.line1, 0);
Selim Cinekc848c3a2016-01-13 15:27:30 -08004456 contentView.setViewLayoutMarginEnd(R.id.text, 0);
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02004457 }
4458 }
4459
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02004460 /**
4461 * @hide
4462 */
4463 @Override
4464 protected boolean hasProgress() {
4465 return false;
4466 }
Dan Sandler842dd772014-05-15 09:36:47 -04004467 }
Griff Hazen61a9e862014-05-22 16:05:19 -07004468
Selim Cinek593610c2016-02-16 18:42:57 -08004469 /**
4470 * Notification style for custom views that are decorated by the system
4471 *
4472 * <p>Instead of providing a notification that is completely custom, a developer can set this
4473 * style and still obtain system decorations like the notification header with the expand
4474 * affordance and actions.
4475 *
4476 * <p>Use {@link android.app.Notification.Builder#setCustomContentView(RemoteViews)},
4477 * {@link android.app.Notification.Builder#setCustomBigContentView(RemoteViews)} and
4478 * {@link android.app.Notification.Builder#setCustomHeadsUpContentView(RemoteViews)} to set the
4479 * corresponding custom views to display.
4480 *
4481 * To use this style with your Notification, feed it to
4482 * {@link Notification.Builder#setStyle(android.app.Notification.Style)} like so:
4483 * <pre class="prettyprint">
4484 * Notification noti = new Notification.Builder()
4485 * .setSmallIcon(R.drawable.ic_stat_player)
4486 * .setLargeIcon(albumArtBitmap))
4487 * .setCustomContentView(contentView);
4488 * .setStyle(<b>new Notification.DecoratedCustomViewStyle()</b>)
4489 * .build();
4490 * </pre>
4491 */
4492 public static class DecoratedCustomViewStyle extends Style {
4493
4494 public DecoratedCustomViewStyle() {
4495 }
4496
4497 public DecoratedCustomViewStyle(Builder builder) {
4498 setBuilder(builder);
4499 }
4500
4501 /**
4502 * @hide
4503 */
4504 public boolean displayCustomViewInline() {
4505 return true;
4506 }
4507
4508 /**
4509 * @hide
4510 */
4511 @Override
4512 public RemoteViews makeContentView() {
4513 return makeStandardTemplateWithCustomContent(mBuilder.mN.contentView);
4514 }
4515
4516 /**
4517 * @hide
4518 */
4519 @Override
4520 public RemoteViews makeBigContentView() {
4521 return makeDecoratedBigContentView();
4522 }
4523
4524 /**
4525 * @hide
4526 */
4527 @Override
4528 public RemoteViews makeHeadsUpContentView() {
4529 return makeDecoratedHeadsUpContentView();
4530 }
4531
Selim Cinek593610c2016-02-16 18:42:57 -08004532 private RemoteViews makeDecoratedHeadsUpContentView() {
4533 RemoteViews headsUpContentView = mBuilder.mN.headsUpContentView == null
4534 ? mBuilder.mN.contentView
4535 : mBuilder.mN.headsUpContentView;
4536 if (mBuilder.mActions.size() == 0) {
4537 return makeStandardTemplateWithCustomContent(headsUpContentView);
4538 }
4539 RemoteViews remoteViews = mBuilder.applyStandardTemplateWithActions(
4540 mBuilder.getBigBaseLayoutResource());
Selim Cinek247fa012016-02-18 09:50:48 -08004541 buildIntoRemoteViewContent(remoteViews, headsUpContentView);
Selim Cinek593610c2016-02-16 18:42:57 -08004542 return remoteViews;
4543 }
4544
Selim Cinek593610c2016-02-16 18:42:57 -08004545 private RemoteViews makeStandardTemplateWithCustomContent(RemoteViews customContent) {
4546 RemoteViews remoteViews = mBuilder.applyStandardTemplate(
4547 mBuilder.getBaseLayoutResource());
Selim Cinek247fa012016-02-18 09:50:48 -08004548 buildIntoRemoteViewContent(remoteViews, customContent);
Selim Cinek593610c2016-02-16 18:42:57 -08004549 return remoteViews;
4550 }
4551
Selim Cinek593610c2016-02-16 18:42:57 -08004552 private RemoteViews makeDecoratedBigContentView() {
4553 RemoteViews bigContentView = mBuilder.mN.bigContentView == null
4554 ? mBuilder.mN.contentView
4555 : mBuilder.mN.bigContentView;
4556 if (mBuilder.mActions.size() == 0) {
4557 return makeStandardTemplateWithCustomContent(bigContentView);
4558 }
4559 RemoteViews remoteViews = mBuilder.applyStandardTemplateWithActions(
4560 mBuilder.getBigBaseLayoutResource());
Selim Cinek247fa012016-02-18 09:50:48 -08004561 buildIntoRemoteViewContent(remoteViews, bigContentView);
Selim Cinek593610c2016-02-16 18:42:57 -08004562 return remoteViews;
4563 }
Selim Cinek247fa012016-02-18 09:50:48 -08004564
4565 private void buildIntoRemoteViewContent(RemoteViews remoteViews,
4566 RemoteViews customContent) {
4567 remoteViews.removeAllViews(R.id.notification_main_column);
Adrian Roos5081c0d2016-02-26 16:04:19 -08004568 // Need to clone customContent before adding, because otherwise it can no longer be
4569 // parceled independently of remoteViews.
4570 if (customContent != null) {
4571 customContent = customContent.clone();
4572 }
Selim Cinek247fa012016-02-18 09:50:48 -08004573 remoteViews.addView(R.id.notification_main_column, customContent);
4574 // also update the end margin if there is an image
4575 int endMargin = mBuilder.mContext.getResources().getDimensionPixelSize(
4576 R.dimen.notification_content_margin_end);
4577 if (mBuilder.mN.mLargeIcon != null) {
4578 endMargin += mBuilder.mContext.getResources().getDimensionPixelSize(
4579 R.dimen.notification_content_picture_margin);
4580 }
4581 remoteViews.setViewLayoutMarginEnd(R.id.notification_main_column, endMargin);
4582 }
Selim Cinek593610c2016-02-16 18:42:57 -08004583 }
4584
Selim Cinek03eb3b72016-02-18 10:39:45 -08004585 /**
4586 * Notification style for media custom views that are decorated by the system
4587 *
4588 * <p>Instead of providing a media notification that is completely custom, a developer can set
4589 * this style and still obtain system decorations like the notification header with the expand
4590 * affordance and actions.
4591 *
4592 * <p>Use {@link android.app.Notification.Builder#setCustomContentView(RemoteViews)},
4593 * {@link android.app.Notification.Builder#setCustomBigContentView(RemoteViews)} and
4594 * {@link android.app.Notification.Builder#setCustomHeadsUpContentView(RemoteViews)} to set the
4595 * corresponding custom views to display.
4596 *
4597 * To use this style with your Notification, feed it to
4598 * {@link Notification.Builder#setStyle(android.app.Notification.Style)} like so:
4599 * <pre class="prettyprint">
4600 * Notification noti = new Notification.Builder()
4601 * .setSmallIcon(R.drawable.ic_stat_player)
4602 * .setLargeIcon(albumArtBitmap))
4603 * .setCustomContentView(contentView);
4604 * .setStyle(<b>new Notification.DecoratedMediaCustomViewStyle()</b>
4605 * .setMediaSession(mySession))
4606 * .build();
4607 * </pre>
4608 *
4609 * @see android.app.Notification.DecoratedCustomViewStyle
4610 * @see android.app.Notification.MediaStyle
4611 */
4612 public static class DecoratedMediaCustomViewStyle extends MediaStyle {
4613
4614 public DecoratedMediaCustomViewStyle() {
4615 }
4616
4617 public DecoratedMediaCustomViewStyle(Builder builder) {
4618 setBuilder(builder);
4619 }
4620
4621 /**
4622 * @hide
4623 */
4624 public boolean displayCustomViewInline() {
4625 return true;
4626 }
4627
4628 /**
4629 * @hide
4630 */
4631 @Override
4632 public RemoteViews makeContentView() {
4633 RemoteViews remoteViews = super.makeContentView();
4634 return buildIntoRemoteView(remoteViews, R.id.notification_content_container,
4635 mBuilder.mN.contentView);
4636 }
4637
4638 /**
4639 * @hide
4640 */
4641 @Override
4642 public RemoteViews makeBigContentView() {
4643 RemoteViews customRemoteView = mBuilder.mN.bigContentView != null
4644 ? mBuilder.mN.bigContentView
4645 : mBuilder.mN.contentView;
4646 return makeBigContentViewWithCustomContent(customRemoteView);
4647 }
4648
4649 private RemoteViews makeBigContentViewWithCustomContent(RemoteViews customRemoteView) {
4650 RemoteViews remoteViews = super.makeBigContentView();
4651 if (remoteViews != null) {
4652 return buildIntoRemoteView(remoteViews, R.id.notification_main_column,
4653 customRemoteView);
4654 } else if (customRemoteView != mBuilder.mN.contentView){
4655 remoteViews = super.makeContentView();
4656 return buildIntoRemoteView(remoteViews, R.id.notification_content_container,
4657 customRemoteView);
4658 } else {
4659 return null;
4660 }
4661 }
4662
4663 /**
4664 * @hide
4665 */
4666 @Override
4667 public RemoteViews makeHeadsUpContentView() {
4668 RemoteViews customRemoteView = mBuilder.mN.headsUpContentView != null
4669 ? mBuilder.mN.headsUpContentView
4670 : mBuilder.mN.contentView;
4671 return makeBigContentViewWithCustomContent(customRemoteView);
4672 }
4673
4674 private RemoteViews buildIntoRemoteView(RemoteViews remoteViews, int id,
4675 RemoteViews customContent) {
4676 remoteViews.removeAllViews(id);
Adrian Roos5081c0d2016-02-26 16:04:19 -08004677 // Need to clone customContent before adding, because otherwise it can no longer be
4678 // parceled independently of remoteViews.
4679 if (customContent != null) {
4680 customContent = customContent.clone();
4681 }
Selim Cinek03eb3b72016-02-18 10:39:45 -08004682 remoteViews.addView(id, customContent);
4683 return remoteViews;
4684 }
4685 }
4686
Christoph Studer4600f9b2014-07-22 22:44:43 +02004687 // When adding a new Style subclass here, don't forget to update
4688 // Builder.getNotificationStyleClass.
4689
Griff Hazen61a9e862014-05-22 16:05:19 -07004690 /**
4691 * Extender interface for use with {@link Builder#extend}. Extenders may be used to add
4692 * metadata or change options on a notification builder.
4693 */
4694 public interface Extender {
4695 /**
4696 * Apply this extender to a notification builder.
4697 * @param builder the builder to be modified.
4698 * @return the build object for chaining.
4699 */
4700 public Builder extend(Builder builder);
4701 }
4702
4703 /**
4704 * Helper class to add wearable extensions to notifications.
4705 * <p class="note"> See
4706 * <a href="{@docRoot}wear/notifications/creating.html">Creating Notifications
4707 * for Android Wear</a> for more information on how to use this class.
4708 * <p>
4709 * To create a notification with wearable extensions:
4710 * <ol>
4711 * <li>Create a {@link android.app.Notification.Builder}, setting any desired
4712 * properties.
4713 * <li>Create a {@link android.app.Notification.WearableExtender}.
4714 * <li>Set wearable-specific properties using the
4715 * {@code add} and {@code set} methods of {@link android.app.Notification.WearableExtender}.
4716 * <li>Call {@link android.app.Notification.Builder#extend} to apply the extensions to a
4717 * notification.
4718 * <li>Post the notification to the notification system with the
4719 * {@code NotificationManager.notify(...)} methods.
4720 * </ol>
4721 *
4722 * <pre class="prettyprint">
4723 * Notification notif = new Notification.Builder(mContext)
4724 * .setContentTitle(&quot;New mail from &quot; + sender.toString())
4725 * .setContentText(subject)
4726 * .setSmallIcon(R.drawable.new_mail)
4727 * .extend(new Notification.WearableExtender()
4728 * .setContentIcon(R.drawable.new_mail))
4729 * .build();
4730 * NotificationManager notificationManger =
4731 * (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
4732 * notificationManger.notify(0, notif);</pre>
4733 *
4734 * <p>Wearable extensions can be accessed on an existing notification by using the
4735 * {@code WearableExtender(Notification)} constructor,
4736 * and then using the {@code get} methods to access values.
4737 *
4738 * <pre class="prettyprint">
4739 * Notification.WearableExtender wearableExtender = new Notification.WearableExtender(
4740 * notification);
Griff Hazen14f57992014-05-26 09:07:14 -07004741 * List&lt;Notification&gt; pages = wearableExtender.getPages();</pre>
Griff Hazen61a9e862014-05-22 16:05:19 -07004742 */
4743 public static final class WearableExtender implements Extender {
4744 /**
4745 * Sentinel value for an action index that is unset.
4746 */
4747 public static final int UNSET_ACTION_INDEX = -1;
4748
4749 /**
4750 * Size value for use with {@link #setCustomSizePreset} to show this notification with
4751 * default sizing.
4752 * <p>For custom display notifications created using {@link #setDisplayIntent},
Paul Soulosaa4f4bf2015-08-04 11:59:45 -07004753 * the default is {@link #SIZE_MEDIUM}. All other notifications size automatically based
Griff Hazen61a9e862014-05-22 16:05:19 -07004754 * on their content.
4755 */
4756 public static final int SIZE_DEFAULT = 0;
4757
4758 /**
4759 * Size value for use with {@link #setCustomSizePreset} to show this notification
4760 * with an extra small size.
4761 * <p>This value is only applicable for custom display notifications created using
4762 * {@link #setDisplayIntent}.
4763 */
4764 public static final int SIZE_XSMALL = 1;
4765
4766 /**
4767 * Size value for use with {@link #setCustomSizePreset} to show this notification
4768 * with a small size.
4769 * <p>This value is only applicable for custom display notifications created using
4770 * {@link #setDisplayIntent}.
4771 */
4772 public static final int SIZE_SMALL = 2;
4773
4774 /**
4775 * Size value for use with {@link #setCustomSizePreset} to show this notification
4776 * with a medium size.
4777 * <p>This value is only applicable for custom display notifications created using
4778 * {@link #setDisplayIntent}.
4779 */
4780 public static final int SIZE_MEDIUM = 3;
4781
4782 /**
4783 * Size value for use with {@link #setCustomSizePreset} to show this notification
4784 * with a large size.
4785 * <p>This value is only applicable for custom display notifications created using
4786 * {@link #setDisplayIntent}.
4787 */
4788 public static final int SIZE_LARGE = 4;
4789
Griff Hazend5f11f92014-05-27 15:40:09 -07004790 /**
4791 * Size value for use with {@link #setCustomSizePreset} to show this notification
4792 * full screen.
4793 * <p>This value is only applicable for custom display notifications created using
4794 * {@link #setDisplayIntent}.
4795 */
4796 public static final int SIZE_FULL_SCREEN = 5;
4797
Griff Hazen5f2edfc2014-09-29 16:28:44 -07004798 /**
4799 * Sentinel value for use with {@link #setHintScreenTimeout} to keep the screen on for a
4800 * short amount of time when this notification is displayed on the screen. This
4801 * is the default value.
4802 */
4803 public static final int SCREEN_TIMEOUT_SHORT = 0;
4804
4805 /**
4806 * Sentinel value for use with {@link #setHintScreenTimeout} to keep the screen on
4807 * for a longer amount of time when this notification is displayed on the screen.
4808 */
4809 public static final int SCREEN_TIMEOUT_LONG = -1;
4810
Griff Hazen61a9e862014-05-22 16:05:19 -07004811 /** Notification extra which contains wearable extensions */
4812 private static final String EXTRA_WEARABLE_EXTENSIONS = "android.wearable.EXTENSIONS";
4813
Pete Gastaf6781d2014-10-07 15:17:05 -04004814 // Keys within EXTRA_WEARABLE_EXTENSIONS for wearable options.
Griff Hazen61a9e862014-05-22 16:05:19 -07004815 private static final String KEY_ACTIONS = "actions";
4816 private static final String KEY_FLAGS = "flags";
4817 private static final String KEY_DISPLAY_INTENT = "displayIntent";
4818 private static final String KEY_PAGES = "pages";
4819 private static final String KEY_BACKGROUND = "background";
4820 private static final String KEY_CONTENT_ICON = "contentIcon";
4821 private static final String KEY_CONTENT_ICON_GRAVITY = "contentIconGravity";
4822 private static final String KEY_CONTENT_ACTION_INDEX = "contentActionIndex";
4823 private static final String KEY_CUSTOM_SIZE_PRESET = "customSizePreset";
4824 private static final String KEY_CUSTOM_CONTENT_HEIGHT = "customContentHeight";
4825 private static final String KEY_GRAVITY = "gravity";
Griff Hazen5f2edfc2014-09-29 16:28:44 -07004826 private static final String KEY_HINT_SCREEN_TIMEOUT = "hintScreenTimeout";
Griff Hazen61a9e862014-05-22 16:05:19 -07004827
4828 // Flags bitwise-ored to mFlags
4829 private static final int FLAG_CONTENT_INTENT_AVAILABLE_OFFLINE = 0x1;
4830 private static final int FLAG_HINT_HIDE_ICON = 1 << 1;
4831 private static final int FLAG_HINT_SHOW_BACKGROUND_ONLY = 1 << 2;
4832 private static final int FLAG_START_SCROLL_BOTTOM = 1 << 3;
Griff Hazen5f2edfc2014-09-29 16:28:44 -07004833 private static final int FLAG_HINT_AVOID_BACKGROUND_CLIPPING = 1 << 4;
Griff Hazen61a9e862014-05-22 16:05:19 -07004834
4835 // Default value for flags integer
4836 private static final int DEFAULT_FLAGS = FLAG_CONTENT_INTENT_AVAILABLE_OFFLINE;
4837
4838 private static final int DEFAULT_CONTENT_ICON_GRAVITY = Gravity.END;
4839 private static final int DEFAULT_GRAVITY = Gravity.BOTTOM;
4840
4841 private ArrayList<Action> mActions = new ArrayList<Action>();
4842 private int mFlags = DEFAULT_FLAGS;
4843 private PendingIntent mDisplayIntent;
4844 private ArrayList<Notification> mPages = new ArrayList<Notification>();
4845 private Bitmap mBackground;
4846 private int mContentIcon;
4847 private int mContentIconGravity = DEFAULT_CONTENT_ICON_GRAVITY;
4848 private int mContentActionIndex = UNSET_ACTION_INDEX;
4849 private int mCustomSizePreset = SIZE_DEFAULT;
4850 private int mCustomContentHeight;
4851 private int mGravity = DEFAULT_GRAVITY;
Griff Hazen5f2edfc2014-09-29 16:28:44 -07004852 private int mHintScreenTimeout;
Griff Hazen61a9e862014-05-22 16:05:19 -07004853
4854 /**
4855 * Create a {@link android.app.Notification.WearableExtender} with default
4856 * options.
4857 */
4858 public WearableExtender() {
4859 }
4860
4861 public WearableExtender(Notification notif) {
4862 Bundle wearableBundle = notif.extras.getBundle(EXTRA_WEARABLE_EXTENSIONS);
4863 if (wearableBundle != null) {
4864 List<Action> actions = wearableBundle.getParcelableArrayList(KEY_ACTIONS);
4865 if (actions != null) {
4866 mActions.addAll(actions);
4867 }
4868
4869 mFlags = wearableBundle.getInt(KEY_FLAGS, DEFAULT_FLAGS);
4870 mDisplayIntent = wearableBundle.getParcelable(KEY_DISPLAY_INTENT);
4871
4872 Notification[] pages = getNotificationArrayFromBundle(
4873 wearableBundle, KEY_PAGES);
4874 if (pages != null) {
4875 Collections.addAll(mPages, pages);
4876 }
4877
4878 mBackground = wearableBundle.getParcelable(KEY_BACKGROUND);
4879 mContentIcon = wearableBundle.getInt(KEY_CONTENT_ICON);
4880 mContentIconGravity = wearableBundle.getInt(KEY_CONTENT_ICON_GRAVITY,
4881 DEFAULT_CONTENT_ICON_GRAVITY);
4882 mContentActionIndex = wearableBundle.getInt(KEY_CONTENT_ACTION_INDEX,
4883 UNSET_ACTION_INDEX);
4884 mCustomSizePreset = wearableBundle.getInt(KEY_CUSTOM_SIZE_PRESET,
4885 SIZE_DEFAULT);
4886 mCustomContentHeight = wearableBundle.getInt(KEY_CUSTOM_CONTENT_HEIGHT);
4887 mGravity = wearableBundle.getInt(KEY_GRAVITY, DEFAULT_GRAVITY);
Griff Hazen5f2edfc2014-09-29 16:28:44 -07004888 mHintScreenTimeout = wearableBundle.getInt(KEY_HINT_SCREEN_TIMEOUT);
Griff Hazen61a9e862014-05-22 16:05:19 -07004889 }
4890 }
4891
4892 /**
4893 * Apply wearable extensions to a notification that is being built. This is typically
4894 * called by the {@link android.app.Notification.Builder#extend} method of
4895 * {@link android.app.Notification.Builder}.
4896 */
4897 @Override
4898 public Notification.Builder extend(Notification.Builder builder) {
4899 Bundle wearableBundle = new Bundle();
4900
4901 if (!mActions.isEmpty()) {
4902 wearableBundle.putParcelableArrayList(KEY_ACTIONS, mActions);
4903 }
4904 if (mFlags != DEFAULT_FLAGS) {
4905 wearableBundle.putInt(KEY_FLAGS, mFlags);
4906 }
4907 if (mDisplayIntent != null) {
4908 wearableBundle.putParcelable(KEY_DISPLAY_INTENT, mDisplayIntent);
4909 }
4910 if (!mPages.isEmpty()) {
4911 wearableBundle.putParcelableArray(KEY_PAGES, mPages.toArray(
4912 new Notification[mPages.size()]));
4913 }
4914 if (mBackground != null) {
4915 wearableBundle.putParcelable(KEY_BACKGROUND, mBackground);
4916 }
4917 if (mContentIcon != 0) {
4918 wearableBundle.putInt(KEY_CONTENT_ICON, mContentIcon);
4919 }
4920 if (mContentIconGravity != DEFAULT_CONTENT_ICON_GRAVITY) {
4921 wearableBundle.putInt(KEY_CONTENT_ICON_GRAVITY, mContentIconGravity);
4922 }
4923 if (mContentActionIndex != UNSET_ACTION_INDEX) {
4924 wearableBundle.putInt(KEY_CONTENT_ACTION_INDEX,
4925 mContentActionIndex);
4926 }
4927 if (mCustomSizePreset != SIZE_DEFAULT) {
4928 wearableBundle.putInt(KEY_CUSTOM_SIZE_PRESET, mCustomSizePreset);
4929 }
4930 if (mCustomContentHeight != 0) {
4931 wearableBundle.putInt(KEY_CUSTOM_CONTENT_HEIGHT, mCustomContentHeight);
4932 }
4933 if (mGravity != DEFAULT_GRAVITY) {
4934 wearableBundle.putInt(KEY_GRAVITY, mGravity);
4935 }
Griff Hazen5f2edfc2014-09-29 16:28:44 -07004936 if (mHintScreenTimeout != 0) {
4937 wearableBundle.putInt(KEY_HINT_SCREEN_TIMEOUT, mHintScreenTimeout);
4938 }
Griff Hazen61a9e862014-05-22 16:05:19 -07004939
4940 builder.getExtras().putBundle(EXTRA_WEARABLE_EXTENSIONS, wearableBundle);
4941 return builder;
4942 }
4943
4944 @Override
4945 public WearableExtender clone() {
4946 WearableExtender that = new WearableExtender();
4947 that.mActions = new ArrayList<Action>(this.mActions);
4948 that.mFlags = this.mFlags;
4949 that.mDisplayIntent = this.mDisplayIntent;
4950 that.mPages = new ArrayList<Notification>(this.mPages);
4951 that.mBackground = this.mBackground;
4952 that.mContentIcon = this.mContentIcon;
4953 that.mContentIconGravity = this.mContentIconGravity;
4954 that.mContentActionIndex = this.mContentActionIndex;
4955 that.mCustomSizePreset = this.mCustomSizePreset;
4956 that.mCustomContentHeight = this.mCustomContentHeight;
4957 that.mGravity = this.mGravity;
Griff Hazen5f2edfc2014-09-29 16:28:44 -07004958 that.mHintScreenTimeout = this.mHintScreenTimeout;
Griff Hazen61a9e862014-05-22 16:05:19 -07004959 return that;
4960 }
4961
4962 /**
4963 * Add a wearable action to this notification.
4964 *
4965 * <p>When wearable actions are added using this method, the set of actions that
4966 * show on a wearable device splits from devices that only show actions added
4967 * using {@link android.app.Notification.Builder#addAction}. This allows for customization
4968 * of which actions display on different devices.
4969 *
4970 * @param action the action to add to this notification
4971 * @return this object for method chaining
4972 * @see android.app.Notification.Action
4973 */
4974 public WearableExtender addAction(Action action) {
4975 mActions.add(action);
4976 return this;
4977 }
4978
4979 /**
4980 * Adds wearable actions to this notification.
4981 *
4982 * <p>When wearable actions are added using this method, the set of actions that
4983 * show on a wearable device splits from devices that only show actions added
4984 * using {@link android.app.Notification.Builder#addAction}. This allows for customization
4985 * of which actions display on different devices.
4986 *
4987 * @param actions the actions to add to this notification
4988 * @return this object for method chaining
4989 * @see android.app.Notification.Action
4990 */
4991 public WearableExtender addActions(List<Action> actions) {
4992 mActions.addAll(actions);
4993 return this;
4994 }
4995
4996 /**
4997 * Clear all wearable actions present on this builder.
4998 * @return this object for method chaining.
4999 * @see #addAction
5000 */
5001 public WearableExtender clearActions() {
5002 mActions.clear();
5003 return this;
5004 }
5005
5006 /**
5007 * Get the wearable actions present on this notification.
5008 */
5009 public List<Action> getActions() {
5010 return mActions;
5011 }
5012
5013 /**
5014 * Set an intent to launch inside of an activity view when displaying
Griff Hazen14f57992014-05-26 09:07:14 -07005015 * this notification. The {@link PendingIntent} provided should be for an activity.
5016 *
5017 * <pre class="prettyprint">
5018 * Intent displayIntent = new Intent(context, MyDisplayActivity.class);
5019 * PendingIntent displayPendingIntent = PendingIntent.getActivity(context,
5020 * 0, displayIntent, PendingIntent.FLAG_UPDATE_CURRENT);
5021 * Notification notif = new Notification.Builder(context)
5022 * .extend(new Notification.WearableExtender()
5023 * .setDisplayIntent(displayPendingIntent)
5024 * .setCustomSizePreset(Notification.WearableExtender.SIZE_MEDIUM))
5025 * .build();</pre>
5026 *
5027 * <p>The activity to launch needs to allow embedding, must be exported, and
Griff Hazen831ca9d2014-06-17 00:38:38 -07005028 * should have an empty task affinity. It is also recommended to use the device
5029 * default light theme.
Griff Hazen14f57992014-05-26 09:07:14 -07005030 *
5031 * <p>Example AndroidManifest.xml entry:
5032 * <pre class="prettyprint">
5033 * &lt;activity android:name=&quot;com.example.MyDisplayActivity&quot;
5034 * android:exported=&quot;true&quot;
5035 * android:allowEmbedded=&quot;true&quot;
Griff Hazen831ca9d2014-06-17 00:38:38 -07005036 * android:taskAffinity=&quot;&quot;
5037 * android:theme=&quot;@android:style/Theme.DeviceDefault.Light&quot; /&gt;</pre>
Griff Hazen61a9e862014-05-22 16:05:19 -07005038 *
5039 * @param intent the {@link PendingIntent} for an activity
5040 * @return this object for method chaining
5041 * @see android.app.Notification.WearableExtender#getDisplayIntent
5042 */
5043 public WearableExtender setDisplayIntent(PendingIntent intent) {
5044 mDisplayIntent = intent;
5045 return this;
5046 }
5047
5048 /**
5049 * Get the intent to launch inside of an activity view when displaying this
5050 * notification. This {@code PendingIntent} should be for an activity.
5051 */
5052 public PendingIntent getDisplayIntent() {
5053 return mDisplayIntent;
5054 }
5055
5056 /**
5057 * Add an additional page of content to display with this notification. The current
5058 * notification forms the first page, and pages added using this function form
5059 * subsequent pages. This field can be used to separate a notification into multiple
5060 * sections.
5061 *
5062 * @param page the notification to add as another page
5063 * @return this object for method chaining
5064 * @see android.app.Notification.WearableExtender#getPages
5065 */
5066 public WearableExtender addPage(Notification page) {
5067 mPages.add(page);
5068 return this;
5069 }
5070
5071 /**
5072 * Add additional pages of content to display with this notification. The current
5073 * notification forms the first page, and pages added using this function form
5074 * subsequent pages. This field can be used to separate a notification into multiple
5075 * sections.
5076 *
5077 * @param pages a list of notifications
5078 * @return this object for method chaining
5079 * @see android.app.Notification.WearableExtender#getPages
5080 */
5081 public WearableExtender addPages(List<Notification> pages) {
5082 mPages.addAll(pages);
5083 return this;
5084 }
5085
5086 /**
5087 * Clear all additional pages present on this builder.
5088 * @return this object for method chaining.
5089 * @see #addPage
5090 */
5091 public WearableExtender clearPages() {
5092 mPages.clear();
5093 return this;
5094 }
5095
5096 /**
5097 * Get the array of additional pages of content for displaying this notification. The
5098 * current notification forms the first page, and elements within this array form
5099 * subsequent pages. This field can be used to separate a notification into multiple
5100 * sections.
5101 * @return the pages for this notification
5102 */
5103 public List<Notification> getPages() {
5104 return mPages;
5105 }
5106
5107 /**
5108 * Set a background image to be displayed behind the notification content.
5109 * Contrary to the {@link android.app.Notification.BigPictureStyle}, this background
5110 * will work with any notification style.
5111 *
5112 * @param background the background bitmap
5113 * @return this object for method chaining
5114 * @see android.app.Notification.WearableExtender#getBackground
5115 */
5116 public WearableExtender setBackground(Bitmap background) {
5117 mBackground = background;
5118 return this;
5119 }
5120
5121 /**
5122 * Get a background image to be displayed behind the notification content.
5123 * Contrary to the {@link android.app.Notification.BigPictureStyle}, this background
5124 * will work with any notification style.
5125 *
5126 * @return the background image
5127 * @see android.app.Notification.WearableExtender#setBackground
5128 */
5129 public Bitmap getBackground() {
5130 return mBackground;
5131 }
5132
5133 /**
5134 * Set an icon that goes with the content of this notification.
5135 */
5136 public WearableExtender setContentIcon(int icon) {
5137 mContentIcon = icon;
5138 return this;
5139 }
5140
5141 /**
5142 * Get an icon that goes with the content of this notification.
5143 */
5144 public int getContentIcon() {
5145 return mContentIcon;
5146 }
5147
5148 /**
5149 * Set the gravity that the content icon should have within the notification display.
5150 * Supported values include {@link android.view.Gravity#START} and
5151 * {@link android.view.Gravity#END}. The default value is {@link android.view.Gravity#END}.
5152 * @see #setContentIcon
5153 */
5154 public WearableExtender setContentIconGravity(int contentIconGravity) {
5155 mContentIconGravity = contentIconGravity;
5156 return this;
5157 }
5158
5159 /**
5160 * Get the gravity that the content icon should have within the notification display.
5161 * Supported values include {@link android.view.Gravity#START} and
5162 * {@link android.view.Gravity#END}. The default value is {@link android.view.Gravity#END}.
5163 * @see #getContentIcon
5164 */
5165 public int getContentIconGravity() {
5166 return mContentIconGravity;
5167 }
5168
5169 /**
5170 * Set an action from this notification's actions to be clickable with the content of
Griff Hazen14f57992014-05-26 09:07:14 -07005171 * this notification. This action will no longer display separately from the
5172 * notification's content.
5173 *
Griff Hazenca48d352014-05-28 22:37:13 -07005174 * <p>For notifications with multiple pages, child pages can also have content actions
Griff Hazen14f57992014-05-26 09:07:14 -07005175 * set, although the list of available actions comes from the main notification and not
5176 * from the child page's notification.
5177 *
5178 * @param actionIndex The index of the action to hoist onto the current notification page.
5179 * If wearable actions were added to the main notification, this index
5180 * will apply to that list, otherwise it will apply to the regular
5181 * actions list.
Griff Hazen61a9e862014-05-22 16:05:19 -07005182 */
5183 public WearableExtender setContentAction(int actionIndex) {
5184 mContentActionIndex = actionIndex;
5185 return this;
5186 }
5187
5188 /**
Griff Hazenca48d352014-05-28 22:37:13 -07005189 * Get the index of the notification action, if any, that was specified as being clickable
5190 * with the content of this notification. This action will no longer display separately
Griff Hazen14f57992014-05-26 09:07:14 -07005191 * from the notification's content.
Griff Hazen61a9e862014-05-22 16:05:19 -07005192 *
Griff Hazenca48d352014-05-28 22:37:13 -07005193 * <p>For notifications with multiple pages, child pages can also have content actions
Griff Hazen14f57992014-05-26 09:07:14 -07005194 * set, although the list of available actions comes from the main notification and not
5195 * from the child page's notification.
5196 *
5197 * <p>If wearable specific actions were added to the main notification, this index will
5198 * apply to that list, otherwise it will apply to the regular actions list.
Griff Hazenca48d352014-05-28 22:37:13 -07005199 *
5200 * @return the action index or {@link #UNSET_ACTION_INDEX} if no action was selected.
Griff Hazen61a9e862014-05-22 16:05:19 -07005201 */
5202 public int getContentAction() {
5203 return mContentActionIndex;
5204 }
5205
5206 /**
5207 * Set the gravity that this notification should have within the available viewport space.
5208 * Supported values include {@link android.view.Gravity#TOP},
5209 * {@link android.view.Gravity#CENTER_VERTICAL} and {@link android.view.Gravity#BOTTOM}.
5210 * The default value is {@link android.view.Gravity#BOTTOM}.
5211 */
5212 public WearableExtender setGravity(int gravity) {
5213 mGravity = gravity;
5214 return this;
5215 }
5216
5217 /**
5218 * Get the gravity that this notification should have within the available viewport space.
5219 * Supported values include {@link android.view.Gravity#TOP},
5220 * {@link android.view.Gravity#CENTER_VERTICAL} and {@link android.view.Gravity#BOTTOM}.
5221 * The default value is {@link android.view.Gravity#BOTTOM}.
5222 */
5223 public int getGravity() {
5224 return mGravity;
5225 }
5226
5227 /**
5228 * Set the custom size preset for the display of this notification out of the available
5229 * presets found in {@link android.app.Notification.WearableExtender}, e.g.
5230 * {@link #SIZE_LARGE}.
5231 * <p>Some custom size presets are only applicable for custom display notifications created
5232 * using {@link android.app.Notification.WearableExtender#setDisplayIntent}. Check the
5233 * documentation for the preset in question. See also
5234 * {@link #setCustomContentHeight} and {@link #getCustomSizePreset}.
5235 */
5236 public WearableExtender setCustomSizePreset(int sizePreset) {
5237 mCustomSizePreset = sizePreset;
5238 return this;
5239 }
5240
5241 /**
5242 * Get the custom size preset for the display of this notification out of the available
5243 * presets found in {@link android.app.Notification.WearableExtender}, e.g.
5244 * {@link #SIZE_LARGE}.
5245 * <p>Some custom size presets are only applicable for custom display notifications created
5246 * using {@link #setDisplayIntent}. Check the documentation for the preset in question.
5247 * See also {@link #setCustomContentHeight} and {@link #setCustomSizePreset}.
5248 */
5249 public int getCustomSizePreset() {
5250 return mCustomSizePreset;
5251 }
5252
5253 /**
5254 * Set the custom height in pixels for the display of this notification's content.
5255 * <p>This option is only available for custom display notifications created
5256 * using {@link android.app.Notification.WearableExtender#setDisplayIntent}. See also
5257 * {@link android.app.Notification.WearableExtender#setCustomSizePreset} and
5258 * {@link #getCustomContentHeight}.
5259 */
5260 public WearableExtender setCustomContentHeight(int height) {
5261 mCustomContentHeight = height;
5262 return this;
5263 }
5264
5265 /**
5266 * Get the custom height in pixels for the display of this notification's content.
5267 * <p>This option is only available for custom display notifications created
5268 * using {@link #setDisplayIntent}. See also {@link #setCustomSizePreset} and
5269 * {@link #setCustomContentHeight}.
5270 */
5271 public int getCustomContentHeight() {
5272 return mCustomContentHeight;
5273 }
5274
5275 /**
5276 * Set whether the scrolling position for the contents of this notification should start
5277 * at the bottom of the contents instead of the top when the contents are too long to
5278 * display within the screen. Default is false (start scroll at the top).
5279 */
5280 public WearableExtender setStartScrollBottom(boolean startScrollBottom) {
5281 setFlag(FLAG_START_SCROLL_BOTTOM, startScrollBottom);
5282 return this;
5283 }
5284
5285 /**
5286 * Get whether the scrolling position for the contents of this notification should start
5287 * at the bottom of the contents instead of the top when the contents are too long to
5288 * display within the screen. Default is false (start scroll at the top).
5289 */
5290 public boolean getStartScrollBottom() {
5291 return (mFlags & FLAG_START_SCROLL_BOTTOM) != 0;
5292 }
5293
5294 /**
5295 * Set whether the content intent is available when the wearable device is not connected
5296 * to a companion device. The user can still trigger this intent when the wearable device
5297 * is offline, but a visual hint will indicate that the content intent may not be available.
5298 * Defaults to true.
5299 */
5300 public WearableExtender setContentIntentAvailableOffline(
5301 boolean contentIntentAvailableOffline) {
5302 setFlag(FLAG_CONTENT_INTENT_AVAILABLE_OFFLINE, contentIntentAvailableOffline);
5303 return this;
5304 }
5305
5306 /**
5307 * Get whether the content intent is available when the wearable device is not connected
5308 * to a companion device. The user can still trigger this intent when the wearable device
5309 * is offline, but a visual hint will indicate that the content intent may not be available.
5310 * Defaults to true.
5311 */
5312 public boolean getContentIntentAvailableOffline() {
5313 return (mFlags & FLAG_CONTENT_INTENT_AVAILABLE_OFFLINE) != 0;
5314 }
5315
5316 /**
5317 * Set a hint that this notification's icon should not be displayed.
5318 * @param hintHideIcon {@code true} to hide the icon, {@code false} otherwise.
5319 * @return this object for method chaining
5320 */
5321 public WearableExtender setHintHideIcon(boolean hintHideIcon) {
5322 setFlag(FLAG_HINT_HIDE_ICON, hintHideIcon);
5323 return this;
5324 }
5325
5326 /**
5327 * Get a hint that this notification's icon should not be displayed.
5328 * @return {@code true} if this icon should not be displayed, false otherwise.
5329 * The default value is {@code false} if this was never set.
5330 */
5331 public boolean getHintHideIcon() {
5332 return (mFlags & FLAG_HINT_HIDE_ICON) != 0;
5333 }
5334
5335 /**
5336 * Set a visual hint that only the background image of this notification should be
5337 * displayed, and other semantic content should be hidden. This hint is only applicable
5338 * to sub-pages added using {@link #addPage}.
5339 */
5340 public WearableExtender setHintShowBackgroundOnly(boolean hintShowBackgroundOnly) {
5341 setFlag(FLAG_HINT_SHOW_BACKGROUND_ONLY, hintShowBackgroundOnly);
5342 return this;
5343 }
5344
5345 /**
5346 * Get a visual hint that only the background image of this notification should be
5347 * displayed, and other semantic content should be hidden. This hint is only applicable
5348 * to sub-pages added using {@link android.app.Notification.WearableExtender#addPage}.
5349 */
5350 public boolean getHintShowBackgroundOnly() {
5351 return (mFlags & FLAG_HINT_SHOW_BACKGROUND_ONLY) != 0;
5352 }
5353
Griff Hazen5f2edfc2014-09-29 16:28:44 -07005354 /**
Griff Hazen9c5be4e2014-11-17 17:47:50 -08005355 * Set a hint that this notification's background should not be clipped if possible,
5356 * and should instead be resized to fully display on the screen, retaining the aspect
5357 * ratio of the image. This can be useful for images like barcodes or qr codes.
Griff Hazen5f2edfc2014-09-29 16:28:44 -07005358 * @param hintAvoidBackgroundClipping {@code true} to avoid clipping if possible.
5359 * @return this object for method chaining
5360 */
5361 public WearableExtender setHintAvoidBackgroundClipping(
5362 boolean hintAvoidBackgroundClipping) {
5363 setFlag(FLAG_HINT_AVOID_BACKGROUND_CLIPPING, hintAvoidBackgroundClipping);
5364 return this;
5365 }
5366
5367 /**
Griff Hazen9c5be4e2014-11-17 17:47:50 -08005368 * Get a hint that this notification's background should not be clipped if possible,
5369 * and should instead be resized to fully display on the screen, retaining the aspect
5370 * ratio of the image. This can be useful for images like barcodes or qr codes.
Griff Hazen5f2edfc2014-09-29 16:28:44 -07005371 * @return {@code true} if it's ok if the background is clipped on the screen, false
5372 * otherwise. The default value is {@code false} if this was never set.
5373 */
5374 public boolean getHintAvoidBackgroundClipping() {
5375 return (mFlags & FLAG_HINT_AVOID_BACKGROUND_CLIPPING) != 0;
5376 }
5377
5378 /**
5379 * Set a hint that the screen should remain on for at least this duration when
5380 * this notification is displayed on the screen.
5381 * @param timeout The requested screen timeout in milliseconds. Can also be either
5382 * {@link #SCREEN_TIMEOUT_SHORT} or {@link #SCREEN_TIMEOUT_LONG}.
5383 * @return this object for method chaining
5384 */
5385 public WearableExtender setHintScreenTimeout(int timeout) {
5386 mHintScreenTimeout = timeout;
5387 return this;
5388 }
5389
5390 /**
5391 * Get the duration, in milliseconds, that the screen should remain on for
5392 * when this notification is displayed.
5393 * @return the duration in milliseconds if > 0, or either one of the sentinel values
5394 * {@link #SCREEN_TIMEOUT_SHORT} or {@link #SCREEN_TIMEOUT_LONG}.
5395 */
5396 public int getHintScreenTimeout() {
5397 return mHintScreenTimeout;
5398 }
5399
Griff Hazen61a9e862014-05-22 16:05:19 -07005400 private void setFlag(int mask, boolean value) {
5401 if (value) {
5402 mFlags |= mask;
5403 } else {
5404 mFlags &= ~mask;
5405 }
5406 }
5407 }
5408
5409 /**
Zhen Yu Song9d5528b2014-11-14 15:34:39 -08005410 * <p>Helper class to add Android Auto extensions to notifications. To create a notification
5411 * with car extensions:
5412 *
5413 * <ol>
5414 * <li>Create an {@link Notification.Builder}, setting any desired
5415 * properties.
5416 * <li>Create a {@link CarExtender}.
5417 * <li>Set car-specific properties using the {@code add} and {@code set} methods of
5418 * {@link CarExtender}.
5419 * <li>Call {@link Notification.Builder#extend(Notification.Extender)}
5420 * to apply the extensions to a notification.
5421 * </ol>
5422 *
5423 * <pre class="prettyprint">
5424 * Notification notification = new Notification.Builder(context)
5425 * ...
5426 * .extend(new CarExtender()
5427 * .set*(...))
5428 * .build();
5429 * </pre>
5430 *
5431 * <p>Car extensions can be accessed on an existing notification by using the
5432 * {@code CarExtender(Notification)} constructor, and then using the {@code get} methods
5433 * to access values.
5434 */
5435 public static final class CarExtender implements Extender {
5436 private static final String TAG = "CarExtender";
5437
5438 private static final String EXTRA_CAR_EXTENDER = "android.car.EXTENSIONS";
5439 private static final String EXTRA_LARGE_ICON = "large_icon";
5440 private static final String EXTRA_CONVERSATION = "car_conversation";
5441 private static final String EXTRA_COLOR = "app_color";
5442
5443 private Bitmap mLargeIcon;
5444 private UnreadConversation mUnreadConversation;
5445 private int mColor = Notification.COLOR_DEFAULT;
5446
5447 /**
5448 * Create a {@link CarExtender} with default options.
5449 */
5450 public CarExtender() {
5451 }
5452
5453 /**
5454 * Create a {@link CarExtender} from the CarExtender options of an existing Notification.
5455 *
5456 * @param notif The notification from which to copy options.
5457 */
5458 public CarExtender(Notification notif) {
5459 Bundle carBundle = notif.extras == null ?
5460 null : notif.extras.getBundle(EXTRA_CAR_EXTENDER);
5461 if (carBundle != null) {
5462 mLargeIcon = carBundle.getParcelable(EXTRA_LARGE_ICON);
5463 mColor = carBundle.getInt(EXTRA_COLOR, Notification.COLOR_DEFAULT);
5464
5465 Bundle b = carBundle.getBundle(EXTRA_CONVERSATION);
5466 mUnreadConversation = UnreadConversation.getUnreadConversationFromBundle(b);
5467 }
5468 }
5469
5470 /**
5471 * Apply car extensions to a notification that is being built. This is typically called by
5472 * the {@link Notification.Builder#extend(Notification.Extender)}
5473 * method of {@link Notification.Builder}.
5474 */
5475 @Override
5476 public Notification.Builder extend(Notification.Builder builder) {
5477 Bundle carExtensions = new Bundle();
5478
5479 if (mLargeIcon != null) {
5480 carExtensions.putParcelable(EXTRA_LARGE_ICON, mLargeIcon);
5481 }
5482 if (mColor != Notification.COLOR_DEFAULT) {
5483 carExtensions.putInt(EXTRA_COLOR, mColor);
5484 }
5485
5486 if (mUnreadConversation != null) {
5487 Bundle b = mUnreadConversation.getBundleForUnreadConversation();
5488 carExtensions.putBundle(EXTRA_CONVERSATION, b);
5489 }
5490
5491 builder.getExtras().putBundle(EXTRA_CAR_EXTENDER, carExtensions);
5492 return builder;
5493 }
5494
5495 /**
5496 * Sets the accent color to use when Android Auto presents the notification.
5497 *
5498 * Android Auto uses the color set with {@link Notification.Builder#setColor(int)}
5499 * to accent the displayed notification. However, not all colors are acceptable in an
5500 * automotive setting. This method can be used to override the color provided in the
5501 * notification in such a situation.
5502 */
Tor Norbye80756e32015-03-02 09:39:27 -08005503 public CarExtender setColor(@ColorInt int color) {
Zhen Yu Song9d5528b2014-11-14 15:34:39 -08005504 mColor = color;
5505 return this;
5506 }
5507
5508 /**
5509 * Gets the accent color.
5510 *
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005511 * @see #setColor
Zhen Yu Song9d5528b2014-11-14 15:34:39 -08005512 */
Tor Norbye80756e32015-03-02 09:39:27 -08005513 @ColorInt
Zhen Yu Song9d5528b2014-11-14 15:34:39 -08005514 public int getColor() {
5515 return mColor;
5516 }
5517
5518 /**
5519 * Sets the large icon of the car notification.
5520 *
5521 * If no large icon is set in the extender, Android Auto will display the icon
5522 * specified by {@link Notification.Builder#setLargeIcon(android.graphics.Bitmap)}
5523 *
5524 * @param largeIcon The large icon to use in the car notification.
5525 * @return This object for method chaining.
5526 */
5527 public CarExtender setLargeIcon(Bitmap largeIcon) {
5528 mLargeIcon = largeIcon;
5529 return this;
5530 }
5531
5532 /**
5533 * Gets the large icon used in this car notification, or null if no icon has been set.
5534 *
5535 * @return The large icon for the car notification.
5536 * @see CarExtender#setLargeIcon
5537 */
5538 public Bitmap getLargeIcon() {
5539 return mLargeIcon;
5540 }
5541
5542 /**
5543 * Sets the unread conversation in a message notification.
5544 *
5545 * @param unreadConversation The unread part of the conversation this notification conveys.
5546 * @return This object for method chaining.
5547 */
5548 public CarExtender setUnreadConversation(UnreadConversation unreadConversation) {
5549 mUnreadConversation = unreadConversation;
5550 return this;
5551 }
5552
5553 /**
5554 * Returns the unread conversation conveyed by this notification.
5555 * @see #setUnreadConversation(UnreadConversation)
5556 */
5557 public UnreadConversation getUnreadConversation() {
5558 return mUnreadConversation;
5559 }
5560
5561 /**
5562 * A class which holds the unread messages from a conversation.
5563 */
5564 public static class UnreadConversation {
5565 private static final String KEY_AUTHOR = "author";
5566 private static final String KEY_TEXT = "text";
5567 private static final String KEY_MESSAGES = "messages";
5568 private static final String KEY_REMOTE_INPUT = "remote_input";
5569 private static final String KEY_ON_REPLY = "on_reply";
5570 private static final String KEY_ON_READ = "on_read";
5571 private static final String KEY_PARTICIPANTS = "participants";
5572 private static final String KEY_TIMESTAMP = "timestamp";
5573
5574 private final String[] mMessages;
5575 private final RemoteInput mRemoteInput;
5576 private final PendingIntent mReplyPendingIntent;
5577 private final PendingIntent mReadPendingIntent;
5578 private final String[] mParticipants;
5579 private final long mLatestTimestamp;
5580
5581 UnreadConversation(String[] messages, RemoteInput remoteInput,
5582 PendingIntent replyPendingIntent, PendingIntent readPendingIntent,
5583 String[] participants, long latestTimestamp) {
5584 mMessages = messages;
5585 mRemoteInput = remoteInput;
5586 mReadPendingIntent = readPendingIntent;
5587 mReplyPendingIntent = replyPendingIntent;
5588 mParticipants = participants;
5589 mLatestTimestamp = latestTimestamp;
5590 }
5591
5592 /**
5593 * Gets the list of messages conveyed by this notification.
5594 */
5595 public String[] getMessages() {
5596 return mMessages;
5597 }
5598
5599 /**
5600 * Gets the remote input that will be used to convey the response to a message list, or
5601 * null if no such remote input exists.
5602 */
5603 public RemoteInput getRemoteInput() {
5604 return mRemoteInput;
5605 }
5606
5607 /**
5608 * Gets the pending intent that will be triggered when the user replies to this
5609 * notification.
5610 */
5611 public PendingIntent getReplyPendingIntent() {
5612 return mReplyPendingIntent;
5613 }
5614
5615 /**
5616 * Gets the pending intent that Android Auto will send after it reads aloud all messages
5617 * in this object's message list.
5618 */
5619 public PendingIntent getReadPendingIntent() {
5620 return mReadPendingIntent;
5621 }
5622
5623 /**
5624 * Gets the participants in the conversation.
5625 */
5626 public String[] getParticipants() {
5627 return mParticipants;
5628 }
5629
5630 /**
5631 * Gets the firs participant in the conversation.
5632 */
5633 public String getParticipant() {
5634 return mParticipants.length > 0 ? mParticipants[0] : null;
5635 }
5636
5637 /**
5638 * Gets the timestamp of the conversation.
5639 */
5640 public long getLatestTimestamp() {
5641 return mLatestTimestamp;
5642 }
5643
5644 Bundle getBundleForUnreadConversation() {
5645 Bundle b = new Bundle();
5646 String author = null;
5647 if (mParticipants != null && mParticipants.length > 1) {
5648 author = mParticipants[0];
5649 }
5650 Parcelable[] messages = new Parcelable[mMessages.length];
5651 for (int i = 0; i < messages.length; i++) {
5652 Bundle m = new Bundle();
5653 m.putString(KEY_TEXT, mMessages[i]);
5654 m.putString(KEY_AUTHOR, author);
5655 messages[i] = m;
5656 }
5657 b.putParcelableArray(KEY_MESSAGES, messages);
5658 if (mRemoteInput != null) {
5659 b.putParcelable(KEY_REMOTE_INPUT, mRemoteInput);
5660 }
5661 b.putParcelable(KEY_ON_REPLY, mReplyPendingIntent);
5662 b.putParcelable(KEY_ON_READ, mReadPendingIntent);
5663 b.putStringArray(KEY_PARTICIPANTS, mParticipants);
5664 b.putLong(KEY_TIMESTAMP, mLatestTimestamp);
5665 return b;
5666 }
5667
5668 static UnreadConversation getUnreadConversationFromBundle(Bundle b) {
5669 if (b == null) {
5670 return null;
5671 }
5672 Parcelable[] parcelableMessages = b.getParcelableArray(KEY_MESSAGES);
5673 String[] messages = null;
5674 if (parcelableMessages != null) {
5675 String[] tmp = new String[parcelableMessages.length];
5676 boolean success = true;
5677 for (int i = 0; i < tmp.length; i++) {
5678 if (!(parcelableMessages[i] instanceof Bundle)) {
5679 success = false;
5680 break;
5681 }
5682 tmp[i] = ((Bundle) parcelableMessages[i]).getString(KEY_TEXT);
5683 if (tmp[i] == null) {
5684 success = false;
5685 break;
5686 }
5687 }
5688 if (success) {
5689 messages = tmp;
5690 } else {
5691 return null;
5692 }
5693 }
5694
5695 PendingIntent onRead = b.getParcelable(KEY_ON_READ);
5696 PendingIntent onReply = b.getParcelable(KEY_ON_REPLY);
5697
5698 RemoteInput remoteInput = b.getParcelable(KEY_REMOTE_INPUT);
5699
5700 String[] participants = b.getStringArray(KEY_PARTICIPANTS);
5701 if (participants == null || participants.length != 1) {
5702 return null;
5703 }
5704
5705 return new UnreadConversation(messages,
5706 remoteInput,
5707 onReply,
5708 onRead,
5709 participants, b.getLong(KEY_TIMESTAMP));
5710 }
5711 };
5712
5713 /**
5714 * Builder class for {@link CarExtender.UnreadConversation} objects.
5715 */
5716 public static class Builder {
5717 private final List<String> mMessages = new ArrayList<String>();
5718 private final String mParticipant;
5719 private RemoteInput mRemoteInput;
5720 private PendingIntent mReadPendingIntent;
5721 private PendingIntent mReplyPendingIntent;
5722 private long mLatestTimestamp;
5723
5724 /**
5725 * Constructs a new builder for {@link CarExtender.UnreadConversation}.
5726 *
5727 * @param name The name of the other participant in the conversation.
5728 */
5729 public Builder(String name) {
5730 mParticipant = name;
5731 }
5732
5733 /**
5734 * Appends a new unread message to the list of messages for this conversation.
5735 *
5736 * The messages should be added from oldest to newest.
5737 *
5738 * @param message The text of the new unread message.
5739 * @return This object for method chaining.
5740 */
5741 public Builder addMessage(String message) {
5742 mMessages.add(message);
5743 return this;
5744 }
5745
5746 /**
5747 * Sets the pending intent and remote input which will convey the reply to this
5748 * notification.
5749 *
5750 * @param pendingIntent The pending intent which will be triggered on a reply.
5751 * @param remoteInput The remote input parcelable which will carry the reply.
5752 * @return This object for method chaining.
5753 *
5754 * @see CarExtender.UnreadConversation#getRemoteInput
5755 * @see CarExtender.UnreadConversation#getReplyPendingIntent
5756 */
5757 public Builder setReplyAction(
5758 PendingIntent pendingIntent, RemoteInput remoteInput) {
5759 mRemoteInput = remoteInput;
5760 mReplyPendingIntent = pendingIntent;
5761
5762 return this;
5763 }
5764
5765 /**
5766 * Sets the pending intent that will be sent once the messages in this notification
5767 * are read.
5768 *
5769 * @param pendingIntent The pending intent to use.
5770 * @return This object for method chaining.
5771 */
5772 public Builder setReadPendingIntent(PendingIntent pendingIntent) {
5773 mReadPendingIntent = pendingIntent;
5774 return this;
5775 }
5776
5777 /**
5778 * Sets the timestamp of the most recent message in an unread conversation.
5779 *
5780 * If a messaging notification has been posted by your application and has not
5781 * yet been cancelled, posting a later notification with the same id and tag
5782 * but without a newer timestamp may result in Android Auto not displaying a
5783 * heads up notification for the later notification.
5784 *
5785 * @param timestamp The timestamp of the most recent message in the conversation.
5786 * @return This object for method chaining.
5787 */
5788 public Builder setLatestTimestamp(long timestamp) {
5789 mLatestTimestamp = timestamp;
5790 return this;
5791 }
5792
5793 /**
5794 * Builds a new unread conversation object.
5795 *
5796 * @return The new unread conversation object.
5797 */
5798 public UnreadConversation build() {
5799 String[] messages = mMessages.toArray(new String[mMessages.size()]);
5800 String[] participants = { mParticipant };
5801 return new UnreadConversation(messages, mRemoteInput, mReplyPendingIntent,
5802 mReadPendingIntent, participants, mLatestTimestamp);
5803 }
5804 }
5805 }
5806
5807 /**
Griff Hazen61a9e862014-05-22 16:05:19 -07005808 * Get an array of Notification objects from a parcelable array bundle field.
5809 * Update the bundle to have a typed array so fetches in the future don't need
5810 * to do an array copy.
5811 */
5812 private static Notification[] getNotificationArrayFromBundle(Bundle bundle, String key) {
5813 Parcelable[] array = bundle.getParcelableArray(key);
5814 if (array instanceof Notification[] || array == null) {
5815 return (Notification[]) array;
5816 }
5817 Notification[] typedArray = Arrays.copyOf(array, array.length,
5818 Notification[].class);
5819 bundle.putParcelableArray(key, typedArray);
5820 return typedArray;
5821 }
Christoph Studer4600f9b2014-07-22 22:44:43 +02005822
5823 private static class BuilderRemoteViews extends RemoteViews {
5824 public BuilderRemoteViews(Parcel parcel) {
5825 super(parcel);
5826 }
5827
Kenny Guy77320062014-08-27 21:37:15 +01005828 public BuilderRemoteViews(ApplicationInfo appInfo, int layoutId) {
5829 super(appInfo, layoutId);
Christoph Studer4600f9b2014-07-22 22:44:43 +02005830 }
5831
5832 @Override
5833 public BuilderRemoteViews clone() {
5834 Parcel p = Parcel.obtain();
5835 writeToParcel(p, 0);
5836 p.setDataPosition(0);
5837 BuilderRemoteViews brv = new BuilderRemoteViews(p);
5838 p.recycle();
5839 return brv;
5840 }
5841 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005842}