blob: 060f1e9e7701581c8398f2f18173b7f5f4395c37 [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
2 * Copyright (C) 2007 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package android.app;
18
Tor Norbye80756e32015-03-02 09:39:27 -080019import android.annotation.ColorInt;
Tor Norbye7b9c9122013-05-30 16:48:33 -070020import android.annotation.DrawableRes;
Tor Norbyed9273d62013-05-30 15:59:53 -070021import android.annotation.IntDef;
Daniel Sandler01df1c62014-06-09 10:54:01 -040022import android.annotation.SdkConstant;
23import android.annotation.SdkConstant.SdkConstantType;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080024import android.content.Context;
25import android.content.Intent;
Kenny Guy77320062014-08-27 21:37:15 +010026import android.content.pm.ApplicationInfo;
Selim Cinek65b2e7c2015-10-26 14:11:31 -070027import android.content.pm.PackageManager;
Christoph Studer4600f9b2014-07-22 22:44:43 +020028import android.content.pm.PackageManager.NameNotFoundException;
Jorim Jaggief72a192014-08-26 21:57:46 +020029import android.content.res.ColorStateList;
Joe Onoratoef1e7762010-09-17 18:38:38 -040030import android.graphics.Bitmap;
Kenny Guy8a0101b2014-05-08 23:34:12 +010031import android.graphics.Canvas;
Jorim Jaggi5c2d8462014-03-21 17:37:00 +010032import android.graphics.PorterDuff;
Kenny Guy8a0101b2014-05-08 23:34:12 +010033import android.graphics.drawable.Drawable;
Dan Sandlerd63f9322015-05-06 15:18:49 -040034import android.graphics.drawable.Icon;
John Spurlockc0650f022014-07-19 13:22:39 -040035import android.media.AudioAttributes;
Jeff Sharkey098d5802012-04-26 17:30:34 -070036import android.media.AudioManager;
Jeff Browndba34ba2014-06-24 20:46:03 -070037import android.media.session.MediaSession;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080038import android.net.Uri;
Daniel Sandlerdcbaf662013-04-26 16:23:09 -040039import android.os.BadParcelableException;
Christoph Studer239f8352014-08-25 15:13:18 +020040import android.os.Build;
Daniel Sandler2561b0b2012-02-13 21:04:12 -050041import android.os.Bundle;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080042import android.os.Parcel;
43import android.os.Parcelable;
Daniel Sandlera2985ed2012-04-03 16:42:00 -040044import android.os.SystemClock;
Jeff Sharkey6d515712012-09-20 16:06:08 -070045import android.os.UserHandle;
Selim Cinek60a54252016-02-26 17:03:25 -080046import android.text.SpannableStringBuilder;
47import android.text.Spanned;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080048import android.text.TextUtils;
Selim Cinek60a54252016-02-26 17:03:25 -080049import android.text.style.AbsoluteSizeSpan;
50import android.text.style.RelativeSizeSpan;
51import android.text.style.TextAppearanceSpan;
Daniel Sandlerdcbaf662013-04-26 16:23:09 -040052import android.util.Log;
Dan Sandler50128532015-12-08 15:42:41 -050053import android.util.SparseArray;
Daniel Sandler9f7936a2012-05-21 16:14:28 -040054import android.util.TypedValue;
Griff Hazen61a9e862014-05-22 16:05:19 -070055import android.view.Gravity;
Selim Cinekea4bef72015-12-02 15:51:10 -080056import android.view.NotificationHeaderView;
Joe Onorato8595a3d2010-11-19 18:12:07 -080057import android.view.View;
Adrian Roos9b123cf2016-02-04 14:55:57 -080058import android.view.ViewGroup;
Jeff Sharkey1c400132011-08-05 14:50:13 -070059import android.widget.ProgressBar;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080060import android.widget.RemoteViews;
61
Griff Hazen959591e2014-05-15 22:26:18 -070062import com.android.internal.R;
Griff Hazenc091ba82014-05-16 10:13:26 -070063import com.android.internal.util.NotificationColorUtil;
Griff Hazen959591e2014-05-15 22:26:18 -070064
Tor Norbyed9273d62013-05-30 15:59:53 -070065import java.lang.annotation.Retention;
66import java.lang.annotation.RetentionPolicy;
Christoph Studer4600f9b2014-07-22 22:44:43 +020067import java.lang.reflect.Constructor;
Daniel Sandler2561b0b2012-02-13 21:04:12 -050068import java.util.ArrayList;
Griff Hazen61a9e862014-05-22 16:05:19 -070069import java.util.Arrays;
Griff Hazen5cadc3b2014-05-20 09:55:39 -070070import java.util.Collections;
Griff Hazen61a9e862014-05-22 16:05:19 -070071import java.util.List;
Dan Sandler50128532015-12-08 15:42:41 -050072import java.util.Set;
Joe Onorato561d3852010-11-20 18:09:34 -080073
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080074/**
75 * A class that represents how a persistent notification is to be presented to
76 * the user using the {@link android.app.NotificationManager}.
77 *
Joe Onoratocb109a02011-01-18 17:57:41 -080078 * <p>The {@link Notification.Builder Notification.Builder} has been added to make it
79 * easier to construct Notifications.</p>
80 *
Joe Fernandez558459f2011-10-13 16:47:36 -070081 * <div class="special reference">
82 * <h3>Developer Guides</h3>
83 * <p>For a guide to creating notifications, read the
84 * <a href="{@docRoot}guide/topics/ui/notifiers/notifications.html">Status Bar Notifications</a>
85 * developer guide.</p>
86 * </div>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080087 */
88public class Notification implements Parcelable
89{
Daniel Sandlerdcbaf662013-04-26 16:23:09 -040090 private static final String TAG = "Notification";
91
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080092 /**
Daniel Sandler01df1c62014-06-09 10:54:01 -040093 * An activity that provides a user interface for adjusting notification preferences for its
94 * containing application. Optional but recommended for apps that post
95 * {@link android.app.Notification Notifications}.
96 */
97 @SdkConstant(SdkConstantType.INTENT_CATEGORY)
98 public static final String INTENT_CATEGORY_NOTIFICATION_PREFERENCES
99 = "android.intent.category.NOTIFICATION_PREFERENCES";
100
101 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800102 * Use all default values (where applicable).
103 */
104 public static final int DEFAULT_ALL = ~0;
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500105
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800106 /**
107 * Use the default notification sound. This will ignore any given
108 * {@link #sound}.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500109 *
Chris Wren47c20a12014-06-18 17:27:29 -0400110 * <p>
111 * A notification that is noisy is more likely to be presented as a heads-up notification.
112 * </p>
113 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800114 * @see #defaults
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500115 */
116
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800117 public static final int DEFAULT_SOUND = 1;
118
119 /**
120 * Use the default notification vibrate. This will ignore any given
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500121 * {@link #vibrate}. Using phone vibration requires the
Scott Mainb8b36452009-04-26 15:50:49 -0700122 * {@link android.Manifest.permission#VIBRATE VIBRATE} permission.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500123 *
Chris Wren47c20a12014-06-18 17:27:29 -0400124 * <p>
125 * A notification that vibrates is more likely to be presented as a heads-up notification.
126 * </p>
127 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800128 * @see #defaults
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500129 */
130
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800131 public static final int DEFAULT_VIBRATE = 2;
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500132
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800133 /**
134 * Use the default notification lights. This will ignore the
135 * {@link #FLAG_SHOW_LIGHTS} bit, and {@link #ledARGB}, {@link #ledOffMS}, or
136 * {@link #ledOnMS}.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500137 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800138 * @see #defaults
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500139 */
140
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800141 public static final int DEFAULT_LIGHTS = 4;
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500142
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800143 /**
Christoph Studer535ec612014-09-03 15:47:47 +0200144 * Maximum length of CharSequences accepted by Builder and friends.
145 *
146 * <p>
147 * Avoids spamming the system with overly large strings such as full e-mails.
148 */
149 private static final int MAX_CHARSEQUENCE_LENGTH = 5 * 1024;
150
151 /**
Adrian Roose458aa82015-12-08 16:17:19 -0800152 * Maximum entries of reply text that are accepted by Builder and friends.
153 */
154 private static final int MAX_REPLY_HISTORY = 5;
155
156 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500157 * A timestamp related to this notification, in milliseconds since the epoch.
Joe Malin8d40d042012-11-05 11:36:40 -0800158 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500159 * Default value: {@link System#currentTimeMillis() Now}.
160 *
161 * Choose a timestamp that will be most relevant to the user. For most finite events, this
162 * corresponds to the time the event happened (or will happen, in the case of events that have
163 * yet to occur but about which the user is being informed). Indefinite events should be
Joe Malin8d40d042012-11-05 11:36:40 -0800164 * timestamped according to when the activity began.
165 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500166 * Some examples:
Joe Malin8d40d042012-11-05 11:36:40 -0800167 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500168 * <ul>
169 * <li>Notification of a new chat message should be stamped when the message was received.</li>
170 * <li>Notification of an ongoing file download (with a progress bar, for example) should be stamped when the download started.</li>
171 * <li>Notification of a completed file download should be stamped when the download finished.</li>
172 * <li>Notification of an upcoming meeting should be stamped with the time the meeting will begin (that is, in the future).</li>
173 * <li>Notification of an ongoing stopwatch (increasing timer) should be stamped with the watch's start time.
174 * <li>Notification of an ongoing countdown timer should be stamped with the timer's end time.
Joe Malin8d40d042012-11-05 11:36:40 -0800175 * </ul>
176 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800177 */
178 public long when;
179
180 /**
181 * The resource id of a drawable to use as the icon in the status bar.
Dan Sandler86647982015-05-13 23:41:13 -0400182 *
183 * @deprecated Use {@link Builder#setSmallIcon(Icon)} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800184 */
Dan Sandler86647982015-05-13 23:41:13 -0400185 @Deprecated
Tor Norbye7b9c9122013-05-30 16:48:33 -0700186 @DrawableRes
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800187 public int icon;
188
189 /**
Joe Onorato46439ce2010-11-19 13:56:21 -0800190 * If the icon in the status bar is to have more than one level, you can set this. Otherwise,
191 * leave it at its default value of 0.
192 *
193 * @see android.widget.ImageView#setImageLevel
Griff Hazen959591e2014-05-15 22:26:18 -0700194 * @see android.graphics.drawable.Drawable#setLevel
Joe Onorato46439ce2010-11-19 13:56:21 -0800195 */
196 public int iconLevel;
197
198 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500199 * The number of events that this notification represents. For example, in a new mail
200 * notification, this could be the number of unread messages.
Joe Malin8d40d042012-11-05 11:36:40 -0800201 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500202 * The system may or may not use this field to modify the appearance of the notification. For
203 * example, before {@link android.os.Build.VERSION_CODES#HONEYCOMB}, this number was
204 * superimposed over the icon in the status bar. Starting with
205 * {@link android.os.Build.VERSION_CODES#HONEYCOMB}, the template used by
206 * {@link Notification.Builder} has displayed the number in the expanded notification view.
Joe Malin8d40d042012-11-05 11:36:40 -0800207 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500208 * If the number is 0 or negative, it is never shown.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800209 */
210 public int number;
211
212 /**
213 * The intent to execute when the expanded status entry is clicked. If
214 * this is an activity, it must include the
215 * {@link android.content.Intent#FLAG_ACTIVITY_NEW_TASK} flag, which requires
Scott Main7aee61f2011-02-08 11:25:01 -0800216 * that you take care of task management as described in the
217 * <a href="{@docRoot}guide/topics/fundamentals/tasks-and-back-stack.html">Tasks and Back
Dianne Hackborn6ceca582012-01-10 15:24:26 -0800218 * Stack</a> document. In particular, make sure to read the notification section
219 * <a href="{@docRoot}guide/topics/ui/notifiers/notifications.html#HandlingNotifications">Handling
220 * Notifications</a> for the correct ways to launch an application from a
221 * notification.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800222 */
223 public PendingIntent contentIntent;
224
225 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500226 * The intent to execute when the notification is explicitly dismissed by the user, either with
227 * the "Clear All" button or by swiping it away individually.
228 *
229 * This probably shouldn't be launching an activity since several of those will be sent
230 * at the same time.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800231 */
232 public PendingIntent deleteIntent;
233
234 /**
Dianne Hackborn170bae72010-09-03 15:14:28 -0700235 * An intent to launch instead of posting the notification to the status bar.
Joe Onoratocb109a02011-01-18 17:57:41 -0800236 *
Chris Wren47c20a12014-06-18 17:27:29 -0400237 * <p>
238 * The system UI may choose to display a heads-up notification, instead of
239 * launching this intent, while the user is using the device.
240 * </p>
241 *
Joe Onoratocb109a02011-01-18 17:57:41 -0800242 * @see Notification.Builder#setFullScreenIntent
Daniel Sandlere46cbd32010-06-17 10:35:26 -0400243 */
244 public PendingIntent fullScreenIntent;
245
246 /**
Dan Sandler5fcdf6e2014-07-18 11:31:15 -0400247 * Text that summarizes this notification for accessibility services.
248 *
249 * As of the L release, this text is no longer shown on screen, but it is still useful to
250 * accessibility services (where it serves as an audible announcement of the notification's
251 * appearance).
Joe Onoratoef1e7762010-09-17 18:38:38 -0400252 *
Joe Onorato46439ce2010-11-19 13:56:21 -0800253 * @see #tickerView
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800254 */
255 public CharSequence tickerText;
256
257 /**
Dan Sandler5fcdf6e2014-07-18 11:31:15 -0400258 * Formerly, a view showing the {@link #tickerText}.
259 *
260 * No longer displayed in the status bar as of API 21.
Joe Onoratoef1e7762010-09-17 18:38:38 -0400261 */
Dan Sandler5fcdf6e2014-07-18 11:31:15 -0400262 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -0800263 public RemoteViews tickerView;
Joe Onoratoef1e7762010-09-17 18:38:38 -0400264
265 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400266 * The view that will represent this notification in the notification list (which is pulled
267 * down from the status bar).
268 *
Julia Reynoldsd4ea7412016-02-17 14:00:56 -0500269 * As of N, this field may be null. The notification view is determined by the inputs
270 * to {@link Notification.Builder}; a custom RemoteViews can optionally be
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400271 * supplied with {@link Notification.Builder#setCustomContentView(RemoteViews)}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800272 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400273 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800274 public RemoteViews contentView;
275
Daniel Sandlera0a938c2012-03-15 08:42:37 -0400276 /**
Daniel Sandler4dfbe832012-04-11 14:51:46 -0400277 * A large-format version of {@link #contentView}, giving the Notification an
Daniel Sandlerf3b73432012-03-27 15:01:25 -0400278 * opportunity to show more detail. The system UI may choose to show this
279 * instead of the normal content view at its discretion.
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400280 *
Julia Reynoldsd4ea7412016-02-17 14:00:56 -0500281 * As of N, this field may be null. The expanded notification view is determined by the
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400282 * inputs to {@link Notification.Builder}; a custom RemoteViews can optionally be
283 * supplied with {@link Notification.Builder#setCustomBigContentView(RemoteViews)}.
Daniel Sandlerf3b73432012-03-27 15:01:25 -0400284 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400285 @Deprecated
Daniel Sandlerf3b73432012-03-27 15:01:25 -0400286 public RemoteViews bigContentView;
287
Chris Wren8fd39ec2014-02-27 17:43:26 -0500288
289 /**
Chris Wren47c20a12014-06-18 17:27:29 -0400290 * A medium-format version of {@link #contentView}, providing the Notification an
291 * opportunity to add action buttons to contentView. At its discretion, the system UI may
292 * choose to show this as a heads-up notification, which will pop up so the user can see
293 * it without leaving their current activity.
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400294 *
Julia Reynoldsd4ea7412016-02-17 14:00:56 -0500295 * As of N, this field may be null. The heads-up notification view is determined by the
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400296 * inputs to {@link Notification.Builder}; a custom RemoteViews can optionally be
297 * supplied with {@link Notification.Builder#setCustomHeadsUpContentView(RemoteViews)}.
Chris Wren8fd39ec2014-02-27 17:43:26 -0500298 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400299 @Deprecated
Chris Wren8fd39ec2014-02-27 17:43:26 -0500300 public RemoteViews headsUpContentView;
301
Daniel Sandlerf3b73432012-03-27 15:01:25 -0400302 /**
Dan Sandler86647982015-05-13 23:41:13 -0400303 * A large bitmap to be shown in the notification content area.
304 *
305 * @deprecated Use {@link Builder#setLargeIcon(Icon)} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800306 */
Dan Sandler86647982015-05-13 23:41:13 -0400307 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -0800308 public Bitmap largeIcon;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800309
310 /**
311 * The sound to play.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500312 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800313 * <p>
Chris Wren47c20a12014-06-18 17:27:29 -0400314 * A notification that is noisy is more likely to be presented as a heads-up notification.
315 * </p>
316 *
317 * <p>
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500318 * To play the default notification sound, see {@link #defaults}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800319 * </p>
320 */
321 public Uri sound;
322
323 /**
324 * Use this constant as the value for audioStreamType to request that
325 * the default stream type for notifications be used. Currently the
Jeff Sharkey098d5802012-04-26 17:30:34 -0700326 * default stream type is {@link AudioManager#STREAM_NOTIFICATION}.
John Spurlockc0650f022014-07-19 13:22:39 -0400327 *
328 * @deprecated Use {@link #audioAttributes} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800329 */
Jean-Michel Trivi81f871e2014-08-06 16:32:38 -0700330 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800331 public static final int STREAM_DEFAULT = -1;
332
333 /**
334 * The audio stream type to use when playing the sound.
335 * Should be one of the STREAM_ constants from
336 * {@link android.media.AudioManager}.
John Spurlockc0650f022014-07-19 13:22:39 -0400337 *
338 * @deprecated Use {@link #audioAttributes} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800339 */
Jean-Michel Trivi81f871e2014-08-06 16:32:38 -0700340 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800341 public int audioStreamType = STREAM_DEFAULT;
342
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800343 /**
John Spurlockc0650f022014-07-19 13:22:39 -0400344 * The default value of {@link #audioAttributes}.
345 */
346 public static final AudioAttributes AUDIO_ATTRIBUTES_DEFAULT = new AudioAttributes.Builder()
347 .setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
348 .setUsage(AudioAttributes.USAGE_NOTIFICATION)
349 .build();
350
351 /**
352 * The {@link AudioAttributes audio attributes} to use when playing the sound.
353 */
354 public AudioAttributes audioAttributes = AUDIO_ATTRIBUTES_DEFAULT;
355
356 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500357 * The pattern with which to vibrate.
358 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800359 * <p>
360 * To vibrate the default pattern, see {@link #defaults}.
361 * </p>
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500362 *
Chris Wren47c20a12014-06-18 17:27:29 -0400363 * <p>
364 * A notification that vibrates is more likely to be presented as a heads-up notification.
365 * </p>
366 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800367 * @see android.os.Vibrator#vibrate(long[],int)
368 */
369 public long[] vibrate;
370
371 /**
372 * The color of the led. The hardware will do its best approximation.
373 *
374 * @see #FLAG_SHOW_LIGHTS
375 * @see #flags
376 */
Tor Norbye80756e32015-03-02 09:39:27 -0800377 @ColorInt
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800378 public int ledARGB;
379
380 /**
381 * The number of milliseconds for the LED to be on while it's flashing.
382 * The hardware will do its best approximation.
383 *
384 * @see #FLAG_SHOW_LIGHTS
385 * @see #flags
386 */
387 public int ledOnMS;
388
389 /**
390 * The number of milliseconds for the LED to be off while it's flashing.
391 * The hardware will do its best approximation.
392 *
393 * @see #FLAG_SHOW_LIGHTS
394 * @see #flags
395 */
396 public int ledOffMS;
397
398 /**
399 * Specifies which values should be taken from the defaults.
400 * <p>
401 * To set, OR the desired from {@link #DEFAULT_SOUND},
402 * {@link #DEFAULT_VIBRATE}, {@link #DEFAULT_LIGHTS}. For all default
403 * values, use {@link #DEFAULT_ALL}.
404 * </p>
405 */
406 public int defaults;
407
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800408 /**
409 * Bit to be bitwise-ored into the {@link #flags} field that should be
410 * set if you want the LED on for this notification.
411 * <ul>
412 * <li>To turn the LED off, pass 0 in the alpha channel for colorARGB
413 * or 0 for both ledOnMS and ledOffMS.</li>
414 * <li>To turn the LED on, pass 1 for ledOnMS and 0 for ledOffMS.</li>
415 * <li>To flash the LED, pass the number of milliseconds that it should
416 * be on and off to ledOnMS and ledOffMS.</li>
417 * </ul>
418 * <p>
419 * Since hardware varies, you are not guaranteed that any of the values
420 * you pass are honored exactly. Use the system defaults (TODO) if possible
421 * because they will be set to values that work on any given hardware.
422 * <p>
423 * The alpha channel must be set for forward compatibility.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500424 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800425 */
426 public static final int FLAG_SHOW_LIGHTS = 0x00000001;
427
428 /**
429 * Bit to be bitwise-ored into the {@link #flags} field that should be
430 * set if this notification is in reference to something that is ongoing,
431 * like a phone call. It should not be set if this notification is in
432 * reference to something that happened at a particular point in time,
433 * like a missed phone call.
434 */
435 public static final int FLAG_ONGOING_EVENT = 0x00000002;
436
437 /**
438 * Bit to be bitwise-ored into the {@link #flags} field that if set,
Scott Mainb8b36452009-04-26 15:50:49 -0700439 * the audio will be repeated until the notification is
440 * cancelled or the notification window is opened.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800441 */
442 public static final int FLAG_INSISTENT = 0x00000004;
443
444 /**
445 * Bit to be bitwise-ored into the {@link #flags} field that should be
Griff Hazen293977b2014-04-28 08:37:20 -0700446 * set if you would only like the sound, vibrate and ticker to be played
447 * if the notification was not already showing.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800448 */
449 public static final int FLAG_ONLY_ALERT_ONCE = 0x00000008;
450
451 /**
452 * Bit to be bitwise-ored into the {@link #flags} field that should be
453 * set if the notification should be canceled when it is clicked by the
Daniel Sandler8aa9ae62012-12-04 23:31:47 -0500454 * user.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800455 */
456 public static final int FLAG_AUTO_CANCEL = 0x00000010;
457
458 /**
459 * Bit to be bitwise-ored into the {@link #flags} field that should be
460 * set if the notification should not be canceled when the user clicks
461 * the Clear all button.
462 */
463 public static final int FLAG_NO_CLEAR = 0x00000020;
464
Dianne Hackbornd8a43f62009-08-17 23:33:56 -0700465 /**
466 * Bit to be bitwise-ored into the {@link #flags} field that should be
467 * set if this notification represents a currently running service. This
468 * will normally be set for you by {@link Service#startForeground}.
469 */
470 public static final int FLAG_FOREGROUND_SERVICE = 0x00000040;
471
Daniel Sandlere46cbd32010-06-17 10:35:26 -0400472 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500473 * Obsolete flag indicating high-priority notifications; use the priority field instead.
Joe Malin8d40d042012-11-05 11:36:40 -0800474 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500475 * @deprecated Use {@link #priority} with a positive value.
Daniel Sandlere46cbd32010-06-17 10:35:26 -0400476 */
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500477 public static final int FLAG_HIGH_PRIORITY = 0x00000080;
Daniel Sandlere46cbd32010-06-17 10:35:26 -0400478
Griff Hazendfcb0802014-02-11 12:00:00 -0800479 /**
480 * Bit to be bitswise-ored into the {@link #flags} field that should be
481 * set if this notification is relevant to the current device only
482 * and it is not recommended that it bridge to other devices.
483 */
484 public static final int FLAG_LOCAL_ONLY = 0x00000100;
485
Griff Hazen5cadc3b2014-05-20 09:55:39 -0700486 /**
487 * Bit to be bitswise-ored into the {@link #flags} field that should be
488 * set if this notification is the group summary for a group of notifications.
489 * Grouped notifications may display in a cluster or stack on devices which
490 * support such rendering. Requires a group key also be set using {@link Builder#setGroup}.
491 */
492 public static final int FLAG_GROUP_SUMMARY = 0x00000200;
493
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800494 public int flags;
495
Tor Norbyed9273d62013-05-30 15:59:53 -0700496 /** @hide */
497 @IntDef({PRIORITY_DEFAULT,PRIORITY_LOW,PRIORITY_MIN,PRIORITY_HIGH,PRIORITY_MAX})
498 @Retention(RetentionPolicy.SOURCE)
499 public @interface Priority {}
500
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800501 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500502 * Default notification {@link #priority}. If your application does not prioritize its own
503 * notifications, use this value for all notifications.
504 */
505 public static final int PRIORITY_DEFAULT = 0;
506
507 /**
508 * Lower {@link #priority}, for items that are less important. The UI may choose to show these
509 * items smaller, or at a different position in the list, compared with your app's
510 * {@link #PRIORITY_DEFAULT} items.
511 */
512 public static final int PRIORITY_LOW = -1;
513
514 /**
515 * Lowest {@link #priority}; these items might not be shown to the user except under special
516 * circumstances, such as detailed notification logs.
517 */
518 public static final int PRIORITY_MIN = -2;
519
520 /**
521 * Higher {@link #priority}, for more important notifications or alerts. The UI may choose to
522 * show these items larger, or at a different position in notification lists, compared with
523 * your app's {@link #PRIORITY_DEFAULT} items.
524 */
525 public static final int PRIORITY_HIGH = 1;
526
527 /**
528 * Highest {@link #priority}, for your application's most important items that require the
529 * user's prompt attention or input.
530 */
531 public static final int PRIORITY_MAX = 2;
532
533 /**
534 * Relative priority for this notification.
Joe Malin8d40d042012-11-05 11:36:40 -0800535 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500536 * Priority is an indication of how much of the user's valuable attention should be consumed by
537 * this notification. Low-priority notifications may be hidden from the user in certain
538 * situations, while the user might be interrupted for a higher-priority notification. The
Daniel Sandler6738eee2012-11-16 12:03:32 -0500539 * system will make a determination about how to interpret this priority when presenting
540 * the notification.
Chris Wren47c20a12014-06-18 17:27:29 -0400541 *
542 * <p>
543 * A notification that is at least {@link #PRIORITY_HIGH} is more likely to be presented
544 * as a heads-up notification.
545 * </p>
546 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500547 */
Tor Norbyed9273d62013-05-30 15:59:53 -0700548 @Priority
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500549 public int priority;
Joe Malin8d40d042012-11-05 11:36:40 -0800550
Dan Sandler26e81cf2014-05-06 10:01:27 -0400551 /**
552 * Accent color (an ARGB integer like the constants in {@link android.graphics.Color})
553 * to be applied by the standard Style templates when presenting this notification.
554 *
555 * The current template design constructs a colorful header image by overlaying the
556 * {@link #icon} image (stenciled in white) atop a field of this color. Alpha components are
557 * ignored.
558 */
Tor Norbye80756e32015-03-02 09:39:27 -0800559 @ColorInt
Dan Sandler26e81cf2014-05-06 10:01:27 -0400560 public int color = COLOR_DEFAULT;
561
562 /**
563 * Special value of {@link #color} telling the system not to decorate this notification with
564 * any special color but instead use default colors when presenting this notification.
565 */
Tor Norbye80756e32015-03-02 09:39:27 -0800566 @ColorInt
Dan Sandler26e81cf2014-05-06 10:01:27 -0400567 public static final int COLOR_DEFAULT = 0; // AKA Color.TRANSPARENT
Dan Sandler0bf2ed82013-12-21 23:33:41 -0600568
569 /**
570 * Sphere of visibility of this notification, which affects how and when the SystemUI reveals
571 * the notification's presence and contents in untrusted situations (namely, on the secure
572 * lockscreen).
573 *
574 * The default level, {@link #VISIBILITY_PRIVATE}, behaves exactly as notifications have always
575 * done on Android: The notification's {@link #icon} and {@link #tickerText} (if available) are
576 * shown in all situations, but the contents are only available if the device is unlocked for
577 * the appropriate user.
578 *
579 * A more permissive policy can be expressed by {@link #VISIBILITY_PUBLIC}; such a notification
580 * can be read even in an "insecure" context (that is, above a secure lockscreen).
581 * To modify the public version of this notification—for example, to redact some portions—see
582 * {@link Builder#setPublicVersion(Notification)}.
583 *
584 * Finally, a notification can be made {@link #VISIBILITY_SECRET}, which will suppress its icon
585 * and ticker until the user has bypassed the lockscreen.
586 */
587 public int visibility;
588
Griff Hazenfc3922d2014-08-20 11:56:44 -0700589 /**
590 * Notification visibility: Show this notification in its entirety on all lockscreens.
591 *
592 * {@see #visibility}
593 */
Dan Sandler0bf2ed82013-12-21 23:33:41 -0600594 public static final int VISIBILITY_PUBLIC = 1;
Griff Hazenfc3922d2014-08-20 11:56:44 -0700595
596 /**
597 * Notification visibility: Show this notification on all lockscreens, but conceal sensitive or
598 * private information on secure lockscreens.
599 *
600 * {@see #visibility}
601 */
Dan Sandler0bf2ed82013-12-21 23:33:41 -0600602 public static final int VISIBILITY_PRIVATE = 0;
Griff Hazenfc3922d2014-08-20 11:56:44 -0700603
604 /**
605 * Notification visibility: Do not reveal any part of this notification on a secure lockscreen.
606 *
607 * {@see #visibility}
608 */
Dan Sandler0bf2ed82013-12-21 23:33:41 -0600609 public static final int VISIBILITY_SECRET = -1;
610
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500611 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400612 * Notification category: incoming call (voice or video) or similar synchronous communication request.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500613 */
John Spurlockfd7f1e02014-03-18 16:41:57 -0400614 public static final String CATEGORY_CALL = "call";
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500615
616 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400617 * Notification category: incoming direct message (SMS, instant message, etc.).
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500618 */
John Spurlockfd7f1e02014-03-18 16:41:57 -0400619 public static final String CATEGORY_MESSAGE = "msg";
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500620
621 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400622 * Notification category: asynchronous bulk message (email).
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500623 */
John Spurlockfd7f1e02014-03-18 16:41:57 -0400624 public static final String CATEGORY_EMAIL = "email";
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500625
626 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400627 * Notification category: calendar event.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500628 */
John Spurlockfd7f1e02014-03-18 16:41:57 -0400629 public static final String CATEGORY_EVENT = "event";
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500630
631 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400632 * Notification category: promotion or advertisement.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500633 */
John Spurlockfd7f1e02014-03-18 16:41:57 -0400634 public static final String CATEGORY_PROMO = "promo";
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500635
636 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400637 * Notification category: alarm or timer.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500638 */
John Spurlockfd7f1e02014-03-18 16:41:57 -0400639 public static final String CATEGORY_ALARM = "alarm";
640
641 /**
642 * Notification category: progress of a long-running background operation.
643 */
644 public static final String CATEGORY_PROGRESS = "progress";
645
646 /**
647 * Notification category: social network or sharing update.
648 */
649 public static final String CATEGORY_SOCIAL = "social";
650
651 /**
652 * Notification category: error in background operation or authentication status.
653 */
654 public static final String CATEGORY_ERROR = "err";
655
656 /**
657 * Notification category: media transport control for playback.
658 */
659 public static final String CATEGORY_TRANSPORT = "transport";
660
661 /**
662 * Notification category: system or device status update. Reserved for system use.
663 */
664 public static final String CATEGORY_SYSTEM = "sys";
665
666 /**
667 * Notification category: indication of running background service.
668 */
669 public static final String CATEGORY_SERVICE = "service";
670
671 /**
John Spurlock0a69c8c2014-03-21 13:30:57 -0400672 * Notification category: a specific, timely recommendation for a single thing.
673 * For example, a news app might want to recommend a news story it believes the user will
674 * want to read next.
675 */
676 public static final String CATEGORY_RECOMMENDATION = "recommendation";
677
678 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400679 * Notification category: ongoing information about device or contextual status.
680 */
681 public static final String CATEGORY_STATUS = "status";
682
683 /**
John Spurlock24d3dad2015-04-02 12:24:02 -0400684 * Notification category: user-scheduled reminder.
685 */
686 public static final String CATEGORY_REMINDER = "reminder";
687
688 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400689 * One of the predefined notification categories (see the <code>CATEGORY_*</code> constants)
690 * that best describes this Notification. May be used by the system for ranking and filtering.
691 */
692 public String category;
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500693
Griff Hazen5cadc3b2014-05-20 09:55:39 -0700694 private String mGroupKey;
695
696 /**
697 * Get the key used to group this notification into a cluster or stack
698 * with other notifications on devices which support such rendering.
699 */
700 public String getGroup() {
701 return mGroupKey;
702 }
703
704 private String mSortKey;
705
706 /**
707 * Get a sort key that orders this notification among other notifications from the
708 * same package. This can be useful if an external sort was already applied and an app
709 * would like to preserve this. Notifications will be sorted lexicographically using this
710 * value, although providing different priorities in addition to providing sort key may
711 * cause this value to be ignored.
712 *
713 * <p>This sort key can also be used to order members of a notification group. See
714 * {@link Builder#setGroup}.
715 *
716 * @see String#compareTo(String)
717 */
718 public String getSortKey() {
719 return mSortKey;
720 }
721
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500722 /**
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400723 * Additional semantic data to be carried around with this Notification.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400724 * <p>
725 * The extras keys defined here are intended to capture the original inputs to {@link Builder}
726 * APIs, and are intended to be used by
727 * {@link android.service.notification.NotificationListenerService} implementations to extract
728 * detailed information from notification objects.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500729 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400730 public Bundle extras = new Bundle();
731
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400732 /**
733 * {@link #extras} key: this is the title of the notification,
734 * as supplied to {@link Builder#setContentTitle(CharSequence)}.
735 */
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -0500736 public static final String EXTRA_TITLE = "android.title";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400737
738 /**
739 * {@link #extras} key: this is the title of the notification when shown in expanded form,
740 * e.g. as supplied to {@link BigTextStyle#setBigContentTitle(CharSequence)}.
741 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400742 public static final String EXTRA_TITLE_BIG = EXTRA_TITLE + ".big";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400743
744 /**
745 * {@link #extras} key: this is the main text payload, as supplied to
746 * {@link Builder#setContentText(CharSequence)}.
747 */
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -0500748 public static final String EXTRA_TEXT = "android.text";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400749
750 /**
751 * {@link #extras} key: this is a third line of text, as supplied to
752 * {@link Builder#setSubText(CharSequence)}.
753 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400754 public static final String EXTRA_SUB_TEXT = "android.subText";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400755
756 /**
Adrian Roose458aa82015-12-08 16:17:19 -0800757 * {@link #extras} key: this is the remote input history, as supplied to
758 * {@link Builder#setRemoteInputHistory(CharSequence[])}.
759 */
760 public static final String EXTRA_REMOTE_INPUT_HISTORY = "android.remoteInputHistory";
761
762 /**
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400763 * {@link #extras} key: this is a small piece of additional text as supplied to
764 * {@link Builder#setContentInfo(CharSequence)}.
765 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400766 public static final String EXTRA_INFO_TEXT = "android.infoText";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400767
768 /**
769 * {@link #extras} key: this is a line of summary information intended to be shown
770 * alongside expanded notifications, as supplied to (e.g.)
771 * {@link BigTextStyle#setSummaryText(CharSequence)}.
772 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400773 public static final String EXTRA_SUMMARY_TEXT = "android.summaryText";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400774
775 /**
Christoph Studer4600f9b2014-07-22 22:44:43 +0200776 * {@link #extras} key: this is the longer text shown in the big form of a
777 * {@link BigTextStyle} notification, as supplied to
778 * {@link BigTextStyle#bigText(CharSequence)}.
779 */
780 public static final String EXTRA_BIG_TEXT = "android.bigText";
781
782 /**
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400783 * {@link #extras} key: this is the resource ID of the notification's main small icon, as
784 * supplied to {@link Builder#setSmallIcon(int)}.
785 */
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -0500786 public static final String EXTRA_SMALL_ICON = "android.icon";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400787
788 /**
789 * {@link #extras} key: this is a bitmap to be used instead of the small icon when showing the
790 * notification payload, as
791 * supplied to {@link Builder#setLargeIcon(android.graphics.Bitmap)}.
792 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400793 public static final String EXTRA_LARGE_ICON = "android.largeIcon";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400794
795 /**
796 * {@link #extras} key: this is a bitmap to be used instead of the one from
797 * {@link Builder#setLargeIcon(android.graphics.Bitmap)} when the notification is
798 * shown in its expanded form, as supplied to
799 * {@link BigPictureStyle#bigLargeIcon(android.graphics.Bitmap)}.
800 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400801 public static final String EXTRA_LARGE_ICON_BIG = EXTRA_LARGE_ICON + ".big";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400802
803 /**
804 * {@link #extras} key: this is the progress value supplied to
805 * {@link Builder#setProgress(int, int, boolean)}.
806 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400807 public static final String EXTRA_PROGRESS = "android.progress";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400808
809 /**
810 * {@link #extras} key: this is the maximum value supplied to
811 * {@link Builder#setProgress(int, int, boolean)}.
812 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400813 public static final String EXTRA_PROGRESS_MAX = "android.progressMax";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400814
815 /**
816 * {@link #extras} key: whether the progress bar is indeterminate, supplied to
817 * {@link Builder#setProgress(int, int, boolean)}.
818 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400819 public static final String EXTRA_PROGRESS_INDETERMINATE = "android.progressIndeterminate";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400820
821 /**
822 * {@link #extras} key: whether {@link #when} should be shown as a count-up timer (specifically
823 * a {@link android.widget.Chronometer}) instead of a timestamp, as supplied to
824 * {@link Builder#setUsesChronometer(boolean)}.
825 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400826 public static final String EXTRA_SHOW_CHRONOMETER = "android.showChronometer";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400827
828 /**
Selim Cinek81c23aa2016-02-25 16:23:13 -0800829 * {@link #extras} key: whether the chronometer set on the notification should count down
830 * instead of counting up. Is only relevant if key {@link #EXTRA_SHOW_CHRONOMETER} is present.
831 */
832 public static final String EXTRA_CHRONOMETER_COUNTS_DOWN = "android.chronometerCountsDown";
833
834 /**
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400835 * {@link #extras} key: whether {@link #when} should be shown,
836 * as supplied to {@link Builder#setShowWhen(boolean)}.
837 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400838 public static final String EXTRA_SHOW_WHEN = "android.showWhen";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400839
840 /**
841 * {@link #extras} key: this is a bitmap to be shown in {@link BigPictureStyle} expanded
842 * notifications, supplied to {@link BigPictureStyle#bigPicture(android.graphics.Bitmap)}.
843 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400844 public static final String EXTRA_PICTURE = "android.picture";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400845
846 /**
847 * {@link #extras} key: An array of CharSequences to show in {@link InboxStyle} expanded
848 * notifications, each of which was supplied to {@link InboxStyle#addLine(CharSequence)}.
849 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400850 public static final String EXTRA_TEXT_LINES = "android.textLines";
Dan Sandler842dd772014-05-15 09:36:47 -0400851
852 /**
853 * {@link #extras} key: A string representing the name of the specific
854 * {@link android.app.Notification.Style} used to create this notification.
855 */
Chris Wren91ad5632013-06-05 15:05:57 -0400856 public static final String EXTRA_TEMPLATE = "android.template";
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400857
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400858 /**
Chris Wrene6c48932014-09-29 17:19:27 -0400859 * {@link #extras} key: A String array containing the people that this notification relates to,
860 * each of which was supplied to {@link Builder#addPerson(String)}.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400861 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400862 public static final String EXTRA_PEOPLE = "android.people";
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500863
864 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400865 * Allow certain system-generated notifications to appear before the device is provisioned.
866 * Only available to notifications coming from the android package.
867 * @hide
868 */
869 public static final String EXTRA_ALLOW_DURING_SETUP = "android.allowDuringSetup";
870
871 /**
Jose Limae9e3b3b2014-05-18 23:44:50 -0700872 * {@link #extras} key: A
873 * {@link android.content.ContentUris content URI} pointing to an image that can be displayed
874 * in the background when the notification is selected. The URI must point to an image stream
875 * suitable for passing into
876 * {@link android.graphics.BitmapFactory#decodeStream(java.io.InputStream)
877 * BitmapFactory.decodeStream}; all other content types will be ignored. The content provider
878 * URI used for this purpose must require no permissions to read the image data.
879 */
880 public static final String EXTRA_BACKGROUND_IMAGE_URI = "android.backgroundImageUri";
881
882 /**
Dan Sandler842dd772014-05-15 09:36:47 -0400883 * {@link #extras} key: A
Jeff Browndba34ba2014-06-24 20:46:03 -0700884 * {@link android.media.session.MediaSession.Token} associated with a
Dan Sandler842dd772014-05-15 09:36:47 -0400885 * {@link android.app.Notification.MediaStyle} notification.
886 */
887 public static final String EXTRA_MEDIA_SESSION = "android.mediaSession";
888
889 /**
Bryan Mawhinneye191f902014-07-22 12:50:09 +0100890 * {@link #extras} key: the indices of actions to be shown in the compact view,
891 * as supplied to (e.g.) {@link MediaStyle#setShowActionsInCompactView(int...)}.
892 */
893 public static final String EXTRA_COMPACT_ACTIONS = "android.compactActions";
894
Christoph Studer943aa672014-08-03 20:31:16 +0200895 /**
Kenny Guy8942bcd2014-09-08 21:09:47 +0100896 * {@link #extras} key: the user that built the notification.
897 *
898 * @hide
899 */
900 public static final String EXTRA_ORIGINATING_USERID = "android.originatingUserId";
901
902 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400903 * @hide
904 */
905 public static final String EXTRA_BUILDER_APPLICATION_INFO = "android.appInfo";
906
Selim Cinek247fa012016-02-18 09:50:48 -0800907 /**
908 * @hide
909 */
910 public static final String EXTRA_CONTAINS_CUSTOM_VIEW = "android.contains.customView";
911
Dan Sandlerd63f9322015-05-06 15:18:49 -0400912 private Icon mSmallIcon;
913 private Icon mLargeIcon;
914
Chris Wren51c75102013-07-16 20:49:17 -0400915 /**
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400916 * Structure to encapsulate a named action that can be shown as part of this notification.
917 * It must include an icon, a label, and a {@link PendingIntent} to be fired when the action is
918 * selected by the user.
919 * <p>
Griff Hazen959591e2014-05-15 22:26:18 -0700920 * Apps should use {@link Notification.Builder#addAction(int, CharSequence, PendingIntent)}
921 * or {@link Notification.Builder#addAction(Notification.Action)}
922 * to attach actions.
Daniel Sandlera0a938c2012-03-15 08:42:37 -0400923 */
Daniel Sandlerea2a3172013-02-20 22:24:20 -0500924 public static class Action implements Parcelable {
Griff Hazen959591e2014-05-15 22:26:18 -0700925 private final Bundle mExtras;
Dan Sandler86647982015-05-13 23:41:13 -0400926 private Icon mIcon;
Griff Hazen61a9e862014-05-22 16:05:19 -0700927 private final RemoteInput[] mRemoteInputs;
Griff Hazen959591e2014-05-15 22:26:18 -0700928
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400929 /**
930 * Small icon representing the action.
Dan Sandler86647982015-05-13 23:41:13 -0400931 *
932 * @deprecated Use {@link Action#getIcon()} instead.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400933 */
Dan Sandler86647982015-05-13 23:41:13 -0400934 @Deprecated
Daniel Sandlera0a938c2012-03-15 08:42:37 -0400935 public int icon;
Griff Hazen5cadc3b2014-05-20 09:55:39 -0700936
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400937 /**
938 * Title of the action.
939 */
Daniel Sandlera0a938c2012-03-15 08:42:37 -0400940 public CharSequence title;
Griff Hazen5cadc3b2014-05-20 09:55:39 -0700941
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400942 /**
943 * Intent to send when the user invokes this action. May be null, in which case the action
944 * may be rendered in a disabled presentation by the system UI.
945 */
Daniel Sandlera0a938c2012-03-15 08:42:37 -0400946 public PendingIntent actionIntent;
Griff Hazen959591e2014-05-15 22:26:18 -0700947
Daniel Sandlera0a938c2012-03-15 08:42:37 -0400948 private Action(Parcel in) {
Dan Sandler86647982015-05-13 23:41:13 -0400949 if (in.readInt() != 0) {
950 mIcon = Icon.CREATOR.createFromParcel(in);
Dan Sandler68079d52015-07-22 10:45:30 -0400951 if (mIcon.getType() == Icon.TYPE_RESOURCE) {
952 icon = mIcon.getResId();
953 }
Dan Sandler86647982015-05-13 23:41:13 -0400954 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -0400955 title = TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(in);
956 if (in.readInt() == 1) {
957 actionIntent = PendingIntent.CREATOR.createFromParcel(in);
958 }
Griff Hazen959591e2014-05-15 22:26:18 -0700959 mExtras = in.readBundle();
Griff Hazen5cadc3b2014-05-20 09:55:39 -0700960 mRemoteInputs = in.createTypedArray(RemoteInput.CREATOR);
Daniel Sandlera0a938c2012-03-15 08:42:37 -0400961 }
Griff Hazen5cadc3b2014-05-20 09:55:39 -0700962
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400963 /**
Dan Sandler86647982015-05-13 23:41:13 -0400964 * @deprecated Use {@link android.app.Notification.Action.Builder}.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400965 */
Dan Sandler86647982015-05-13 23:41:13 -0400966 @Deprecated
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400967 public Action(int icon, CharSequence title, PendingIntent intent) {
Dan Sandler86647982015-05-13 23:41:13 -0400968 this(Icon.createWithResource("", icon), title, intent, new Bundle(), null);
Griff Hazen959591e2014-05-15 22:26:18 -0700969 }
970
Dan Sandler86647982015-05-13 23:41:13 -0400971 private Action(Icon icon, CharSequence title, PendingIntent intent, Bundle extras,
Griff Hazen5cadc3b2014-05-20 09:55:39 -0700972 RemoteInput[] remoteInputs) {
Dan Sandler86647982015-05-13 23:41:13 -0400973 this.mIcon = icon;
Gus Prevasf5bff46f2015-08-24 10:34:28 -0400974 if (icon != null && icon.getType() == Icon.TYPE_RESOURCE) {
975 this.icon = icon.getResId();
976 }
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400977 this.title = title;
978 this.actionIntent = intent;
Griff Hazen959591e2014-05-15 22:26:18 -0700979 this.mExtras = extras != null ? extras : new Bundle();
Griff Hazen5cadc3b2014-05-20 09:55:39 -0700980 this.mRemoteInputs = remoteInputs;
Griff Hazen959591e2014-05-15 22:26:18 -0700981 }
982
983 /**
Dan Sandler86647982015-05-13 23:41:13 -0400984 * Return an icon representing the action.
985 */
986 public Icon getIcon() {
987 if (mIcon == null && icon != 0) {
988 // you snuck an icon in here without using the builder; let's try to keep it
989 mIcon = Icon.createWithResource("", icon);
990 }
991 return mIcon;
992 }
993
994 /**
Griff Hazen959591e2014-05-15 22:26:18 -0700995 * Get additional metadata carried around with this Action.
996 */
997 public Bundle getExtras() {
998 return mExtras;
999 }
1000
1001 /**
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001002 * Get the list of inputs to be collected from the user when this action is sent.
1003 * May return null if no remote inputs were added.
1004 */
1005 public RemoteInput[] getRemoteInputs() {
1006 return mRemoteInputs;
1007 }
1008
1009 /**
Griff Hazen959591e2014-05-15 22:26:18 -07001010 * Builder class for {@link Action} objects.
1011 */
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001012 public static final class Builder {
Dan Sandler86647982015-05-13 23:41:13 -04001013 private final Icon mIcon;
Griff Hazen959591e2014-05-15 22:26:18 -07001014 private final CharSequence mTitle;
1015 private final PendingIntent mIntent;
1016 private final Bundle mExtras;
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001017 private ArrayList<RemoteInput> mRemoteInputs;
Griff Hazen959591e2014-05-15 22:26:18 -07001018
1019 /**
1020 * Construct a new builder for {@link Action} object.
1021 * @param icon icon to show for this action
1022 * @param title the title of the action
1023 * @param intent the {@link PendingIntent} to fire when users trigger this action
1024 */
Dan Sandler86647982015-05-13 23:41:13 -04001025 @Deprecated
Griff Hazen959591e2014-05-15 22:26:18 -07001026 public Builder(int icon, CharSequence title, PendingIntent intent) {
Dan Sandler86647982015-05-13 23:41:13 -04001027 this(Icon.createWithResource("", icon), title, intent, new Bundle(), null);
1028 }
1029
1030 /**
1031 * Construct a new builder for {@link Action} object.
1032 * @param icon icon to show for this action
1033 * @param title the title of the action
1034 * @param intent the {@link PendingIntent} to fire when users trigger this action
1035 */
1036 public Builder(Icon icon, CharSequence title, PendingIntent intent) {
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001037 this(icon, title, intent, new Bundle(), null);
Griff Hazen959591e2014-05-15 22:26:18 -07001038 }
1039
1040 /**
1041 * Construct a new builder for {@link Action} object using the fields from an
1042 * {@link Action}.
1043 * @param action the action to read fields from.
1044 */
1045 public Builder(Action action) {
Dan Sandler86647982015-05-13 23:41:13 -04001046 this(action.getIcon(), action.title, action.actionIntent, new Bundle(action.mExtras),
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001047 action.getRemoteInputs());
Griff Hazen959591e2014-05-15 22:26:18 -07001048 }
1049
Dan Sandler86647982015-05-13 23:41:13 -04001050 private Builder(Icon icon, CharSequence title, PendingIntent intent, Bundle extras,
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001051 RemoteInput[] remoteInputs) {
Griff Hazen959591e2014-05-15 22:26:18 -07001052 mIcon = icon;
1053 mTitle = title;
1054 mIntent = intent;
1055 mExtras = extras;
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001056 if (remoteInputs != null) {
1057 mRemoteInputs = new ArrayList<RemoteInput>(remoteInputs.length);
1058 Collections.addAll(mRemoteInputs, remoteInputs);
1059 }
Griff Hazen959591e2014-05-15 22:26:18 -07001060 }
1061
1062 /**
1063 * Merge additional metadata into this builder.
1064 *
1065 * <p>Values within the Bundle will replace existing extras values in this Builder.
1066 *
1067 * @see Notification.Action#extras
1068 */
1069 public Builder addExtras(Bundle extras) {
1070 if (extras != null) {
1071 mExtras.putAll(extras);
1072 }
1073 return this;
1074 }
1075
1076 /**
1077 * Get the metadata Bundle used by this Builder.
1078 *
1079 * <p>The returned Bundle is shared with this Builder.
1080 */
1081 public Bundle getExtras() {
1082 return mExtras;
1083 }
1084
1085 /**
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001086 * Add an input to be collected from the user when this action is sent.
1087 * Response values can be retrieved from the fired intent by using the
1088 * {@link RemoteInput#getResultsFromIntent} function.
1089 * @param remoteInput a {@link RemoteInput} to add to the action
1090 * @return this object for method chaining
1091 */
1092 public Builder addRemoteInput(RemoteInput remoteInput) {
1093 if (mRemoteInputs == null) {
1094 mRemoteInputs = new ArrayList<RemoteInput>();
1095 }
1096 mRemoteInputs.add(remoteInput);
1097 return this;
1098 }
1099
1100 /**
1101 * Apply an extender to this action builder. Extenders may be used to add
1102 * metadata or change options on this builder.
1103 */
Griff Hazen61a9e862014-05-22 16:05:19 -07001104 public Builder extend(Extender extender) {
1105 extender.extend(this);
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001106 return this;
1107 }
1108
1109 /**
Griff Hazen959591e2014-05-15 22:26:18 -07001110 * Combine all of the options that have been set and return a new {@link Action}
1111 * object.
1112 * @return the built action
1113 */
1114 public Action build() {
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001115 RemoteInput[] remoteInputs = mRemoteInputs != null
1116 ? mRemoteInputs.toArray(new RemoteInput[mRemoteInputs.size()]) : null;
1117 return new Action(mIcon, mTitle, mIntent, mExtras, remoteInputs);
Griff Hazen959591e2014-05-15 22:26:18 -07001118 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001119 }
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001120
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001121 @Override
1122 public Action clone() {
1123 return new Action(
Dan Sandler86647982015-05-13 23:41:13 -04001124 getIcon(),
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001125 title,
1126 actionIntent, // safe to alias
1127 new Bundle(mExtras),
1128 getRemoteInputs());
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001129 }
1130 @Override
1131 public int describeContents() {
1132 return 0;
1133 }
1134 @Override
1135 public void writeToParcel(Parcel out, int flags) {
Dan Sandler86647982015-05-13 23:41:13 -04001136 final Icon ic = getIcon();
1137 if (ic != null) {
1138 out.writeInt(1);
1139 ic.writeToParcel(out, 0);
1140 } else {
1141 out.writeInt(0);
1142 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001143 TextUtils.writeToParcel(title, out, flags);
1144 if (actionIntent != null) {
1145 out.writeInt(1);
1146 actionIntent.writeToParcel(out, flags);
1147 } else {
1148 out.writeInt(0);
1149 }
Griff Hazen959591e2014-05-15 22:26:18 -07001150 out.writeBundle(mExtras);
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001151 out.writeTypedArray(mRemoteInputs, flags);
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001152 }
Griff Hazen959591e2014-05-15 22:26:18 -07001153 public static final Parcelable.Creator<Action> CREATOR =
1154 new Parcelable.Creator<Action>() {
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001155 public Action createFromParcel(Parcel in) {
1156 return new Action(in);
1157 }
1158 public Action[] newArray(int size) {
1159 return new Action[size];
1160 }
1161 };
Griff Hazen61a9e862014-05-22 16:05:19 -07001162
1163 /**
1164 * Extender interface for use with {@link Builder#extend}. Extenders may be used to add
1165 * metadata or change options on an action builder.
1166 */
1167 public interface Extender {
1168 /**
1169 * Apply this extender to a notification action builder.
1170 * @param builder the builder to be modified.
1171 * @return the build object for chaining.
1172 */
1173 public Builder extend(Builder builder);
1174 }
1175
1176 /**
1177 * Wearable extender for notification actions. To add extensions to an action,
1178 * create a new {@link android.app.Notification.Action.WearableExtender} object using
1179 * the {@code WearableExtender()} constructor and apply it to a
1180 * {@link android.app.Notification.Action.Builder} using
1181 * {@link android.app.Notification.Action.Builder#extend}.
1182 *
1183 * <pre class="prettyprint">
1184 * Notification.Action action = new Notification.Action.Builder(
1185 * R.drawable.archive_all, "Archive all", actionIntent)
Griff Hazen14f57992014-05-26 09:07:14 -07001186 * .extend(new Notification.Action.WearableExtender()
Griff Hazen61a9e862014-05-22 16:05:19 -07001187 * .setAvailableOffline(false))
Griff Hazen14f57992014-05-26 09:07:14 -07001188 * .build();</pre>
Griff Hazen61a9e862014-05-22 16:05:19 -07001189 */
1190 public static final class WearableExtender implements Extender {
1191 /** Notification action extra which contains wearable extensions */
1192 private static final String EXTRA_WEARABLE_EXTENSIONS = "android.wearable.EXTENSIONS";
1193
Pete Gastaf6781d2014-10-07 15:17:05 -04001194 // Keys within EXTRA_WEARABLE_EXTENSIONS for wearable options.
Griff Hazen61a9e862014-05-22 16:05:19 -07001195 private static final String KEY_FLAGS = "flags";
Pete Gastaf6781d2014-10-07 15:17:05 -04001196 private static final String KEY_IN_PROGRESS_LABEL = "inProgressLabel";
1197 private static final String KEY_CONFIRM_LABEL = "confirmLabel";
1198 private static final String KEY_CANCEL_LABEL = "cancelLabel";
Griff Hazen61a9e862014-05-22 16:05:19 -07001199
1200 // Flags bitwise-ored to mFlags
1201 private static final int FLAG_AVAILABLE_OFFLINE = 0x1;
1202
1203 // Default value for flags integer
1204 private static final int DEFAULT_FLAGS = FLAG_AVAILABLE_OFFLINE;
1205
1206 private int mFlags = DEFAULT_FLAGS;
1207
Pete Gastaf6781d2014-10-07 15:17:05 -04001208 private CharSequence mInProgressLabel;
1209 private CharSequence mConfirmLabel;
1210 private CharSequence mCancelLabel;
1211
Griff Hazen61a9e862014-05-22 16:05:19 -07001212 /**
1213 * Create a {@link android.app.Notification.Action.WearableExtender} with default
1214 * options.
1215 */
1216 public WearableExtender() {
1217 }
1218
1219 /**
1220 * Create a {@link android.app.Notification.Action.WearableExtender} by reading
1221 * wearable options present in an existing notification action.
1222 * @param action the notification action to inspect.
1223 */
1224 public WearableExtender(Action action) {
1225 Bundle wearableBundle = action.getExtras().getBundle(EXTRA_WEARABLE_EXTENSIONS);
1226 if (wearableBundle != null) {
1227 mFlags = wearableBundle.getInt(KEY_FLAGS, DEFAULT_FLAGS);
Pete Gastaf6781d2014-10-07 15:17:05 -04001228 mInProgressLabel = wearableBundle.getCharSequence(KEY_IN_PROGRESS_LABEL);
1229 mConfirmLabel = wearableBundle.getCharSequence(KEY_CONFIRM_LABEL);
1230 mCancelLabel = wearableBundle.getCharSequence(KEY_CANCEL_LABEL);
Griff Hazen61a9e862014-05-22 16:05:19 -07001231 }
1232 }
1233
1234 /**
1235 * Apply wearable extensions to a notification action that is being built. This is
1236 * typically called by the {@link android.app.Notification.Action.Builder#extend}
1237 * method of {@link android.app.Notification.Action.Builder}.
1238 */
1239 @Override
1240 public Action.Builder extend(Action.Builder builder) {
1241 Bundle wearableBundle = new Bundle();
1242
1243 if (mFlags != DEFAULT_FLAGS) {
1244 wearableBundle.putInt(KEY_FLAGS, mFlags);
1245 }
Pete Gastaf6781d2014-10-07 15:17:05 -04001246 if (mInProgressLabel != null) {
1247 wearableBundle.putCharSequence(KEY_IN_PROGRESS_LABEL, mInProgressLabel);
1248 }
1249 if (mConfirmLabel != null) {
1250 wearableBundle.putCharSequence(KEY_CONFIRM_LABEL, mConfirmLabel);
1251 }
1252 if (mCancelLabel != null) {
1253 wearableBundle.putCharSequence(KEY_CANCEL_LABEL, mCancelLabel);
1254 }
Griff Hazen61a9e862014-05-22 16:05:19 -07001255
1256 builder.getExtras().putBundle(EXTRA_WEARABLE_EXTENSIONS, wearableBundle);
1257 return builder;
1258 }
1259
1260 @Override
1261 public WearableExtender clone() {
1262 WearableExtender that = new WearableExtender();
1263 that.mFlags = this.mFlags;
Pete Gastaf6781d2014-10-07 15:17:05 -04001264 that.mInProgressLabel = this.mInProgressLabel;
1265 that.mConfirmLabel = this.mConfirmLabel;
1266 that.mCancelLabel = this.mCancelLabel;
Griff Hazen61a9e862014-05-22 16:05:19 -07001267 return that;
1268 }
1269
1270 /**
1271 * Set whether this action is available when the wearable device is not connected to
1272 * a companion device. The user can still trigger this action when the wearable device is
1273 * offline, but a visual hint will indicate that the action may not be available.
1274 * Defaults to true.
1275 */
1276 public WearableExtender setAvailableOffline(boolean availableOffline) {
1277 setFlag(FLAG_AVAILABLE_OFFLINE, availableOffline);
1278 return this;
1279 }
1280
1281 /**
1282 * Get whether this action is available when the wearable device is not connected to
1283 * a companion device. The user can still trigger this action when the wearable device is
1284 * offline, but a visual hint will indicate that the action may not be available.
1285 * Defaults to true.
1286 */
1287 public boolean isAvailableOffline() {
1288 return (mFlags & FLAG_AVAILABLE_OFFLINE) != 0;
1289 }
1290
1291 private void setFlag(int mask, boolean value) {
1292 if (value) {
1293 mFlags |= mask;
1294 } else {
1295 mFlags &= ~mask;
1296 }
1297 }
Pete Gastaf6781d2014-10-07 15:17:05 -04001298
1299 /**
1300 * Set a label to display while the wearable is preparing to automatically execute the
1301 * action. This is usually a 'ing' verb ending in ellipsis like "Sending..."
1302 *
1303 * @param label the label to display while the action is being prepared to execute
1304 * @return this object for method chaining
1305 */
1306 public WearableExtender setInProgressLabel(CharSequence label) {
1307 mInProgressLabel = label;
1308 return this;
1309 }
1310
1311 /**
1312 * Get the label to display while the wearable is preparing to automatically execute
1313 * the action. This is usually a 'ing' verb ending in ellipsis like "Sending..."
1314 *
1315 * @return the label to display while the action is being prepared to execute
1316 */
1317 public CharSequence getInProgressLabel() {
1318 return mInProgressLabel;
1319 }
1320
1321 /**
1322 * Set a label to display to confirm that the action should be executed.
1323 * This is usually an imperative verb like "Send".
1324 *
1325 * @param label the label to confirm the action should be executed
1326 * @return this object for method chaining
1327 */
1328 public WearableExtender setConfirmLabel(CharSequence label) {
1329 mConfirmLabel = label;
1330 return this;
1331 }
1332
1333 /**
1334 * Get the label to display to confirm that the action should be executed.
1335 * This is usually an imperative verb like "Send".
1336 *
1337 * @return the label to confirm the action should be executed
1338 */
1339 public CharSequence getConfirmLabel() {
1340 return mConfirmLabel;
1341 }
1342
1343 /**
1344 * Set a label to display to cancel the action.
1345 * This is usually an imperative verb, like "Cancel".
1346 *
1347 * @param label the label to display to cancel the action
1348 * @return this object for method chaining
1349 */
1350 public WearableExtender setCancelLabel(CharSequence label) {
1351 mCancelLabel = label;
1352 return this;
1353 }
1354
1355 /**
1356 * Get the label to display to cancel the action.
1357 * This is usually an imperative verb like "Cancel".
1358 *
1359 * @return the label to display to cancel the action
1360 */
1361 public CharSequence getCancelLabel() {
1362 return mCancelLabel;
1363 }
Griff Hazen61a9e862014-05-22 16:05:19 -07001364 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001365 }
1366
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001367 /**
1368 * Array of all {@link Action} structures attached to this notification by
1369 * {@link Builder#addAction(int, CharSequence, PendingIntent)}. Mostly useful for instances of
1370 * {@link android.service.notification.NotificationListenerService} that provide an alternative
1371 * interface for invoking actions.
1372 */
Daniel Sandlerea2a3172013-02-20 22:24:20 -05001373 public Action[] actions;
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001374
1375 /**
Dan Sandler0bf2ed82013-12-21 23:33:41 -06001376 * Replacement version of this notification whose content will be shown
1377 * in an insecure context such as atop a secure keyguard. See {@link #visibility}
1378 * and {@link #VISIBILITY_PUBLIC}.
1379 */
1380 public Notification publicVersion;
1381
1382 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001383 * Constructs a Notification object with default values.
Joe Onorato46439ce2010-11-19 13:56:21 -08001384 * You might want to consider using {@link Builder} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001385 */
1386 public Notification()
1387 {
1388 this.when = System.currentTimeMillis();
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001389 this.priority = PRIORITY_DEFAULT;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001390 }
1391
1392 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001393 * @hide
1394 */
1395 public Notification(Context context, int icon, CharSequence tickerText, long when,
1396 CharSequence contentTitle, CharSequence contentText, Intent contentIntent)
1397 {
Chris Wren1ce4b6d2015-06-11 10:19:43 -04001398 new Builder(context)
1399 .setWhen(when)
1400 .setSmallIcon(icon)
1401 .setTicker(tickerText)
1402 .setContentTitle(contentTitle)
1403 .setContentText(contentText)
1404 .setContentIntent(PendingIntent.getActivity(context, 0, contentIntent, 0))
1405 .buildInto(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001406 }
1407
1408 /**
1409 * Constructs a Notification object with the information needed to
1410 * have a status bar icon without the standard expanded view.
1411 *
1412 * @param icon The resource id of the icon to put in the status bar.
1413 * @param tickerText The text that flows by in the status bar when the notification first
1414 * activates.
1415 * @param when The time to show in the time field. In the System.currentTimeMillis
1416 * timebase.
Joe Onorato46439ce2010-11-19 13:56:21 -08001417 *
1418 * @deprecated Use {@link Builder} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001419 */
Joe Onorato46439ce2010-11-19 13:56:21 -08001420 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001421 public Notification(int icon, CharSequence tickerText, long when)
1422 {
1423 this.icon = icon;
1424 this.tickerText = tickerText;
1425 this.when = when;
1426 }
1427
1428 /**
1429 * Unflatten the notification from a parcel.
1430 */
1431 public Notification(Parcel parcel)
1432 {
1433 int version = parcel.readInt();
1434
1435 when = parcel.readLong();
Dan Sandler3936e7a2015-05-19 20:59:12 -04001436 if (parcel.readInt() != 0) {
1437 mSmallIcon = Icon.CREATOR.createFromParcel(parcel);
Dan Sandler4e787062015-06-17 15:09:48 -04001438 if (mSmallIcon.getType() == Icon.TYPE_RESOURCE) {
1439 icon = mSmallIcon.getResId();
1440 }
Dan Sandler3936e7a2015-05-19 20:59:12 -04001441 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001442 number = parcel.readInt();
1443 if (parcel.readInt() != 0) {
1444 contentIntent = PendingIntent.CREATOR.createFromParcel(parcel);
1445 }
1446 if (parcel.readInt() != 0) {
1447 deleteIntent = PendingIntent.CREATOR.createFromParcel(parcel);
1448 }
1449 if (parcel.readInt() != 0) {
1450 tickerText = TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(parcel);
1451 }
1452 if (parcel.readInt() != 0) {
Joe Onorato46439ce2010-11-19 13:56:21 -08001453 tickerView = RemoteViews.CREATOR.createFromParcel(parcel);
Joe Onoratoef1e7762010-09-17 18:38:38 -04001454 }
1455 if (parcel.readInt() != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001456 contentView = RemoteViews.CREATOR.createFromParcel(parcel);
1457 }
Joe Onorato561d3852010-11-20 18:09:34 -08001458 if (parcel.readInt() != 0) {
Dan Sandlerd63f9322015-05-06 15:18:49 -04001459 mLargeIcon = Icon.CREATOR.createFromParcel(parcel);
Joe Onorato561d3852010-11-20 18:09:34 -08001460 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001461 defaults = parcel.readInt();
1462 flags = parcel.readInt();
1463 if (parcel.readInt() != 0) {
1464 sound = Uri.CREATOR.createFromParcel(parcel);
1465 }
1466
1467 audioStreamType = parcel.readInt();
John Spurlockc0650f022014-07-19 13:22:39 -04001468 if (parcel.readInt() != 0) {
1469 audioAttributes = AudioAttributes.CREATOR.createFromParcel(parcel);
1470 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001471 vibrate = parcel.createLongArray();
1472 ledARGB = parcel.readInt();
1473 ledOnMS = parcel.readInt();
1474 ledOffMS = parcel.readInt();
1475 iconLevel = parcel.readInt();
Daniel Sandlere46cbd32010-06-17 10:35:26 -04001476
1477 if (parcel.readInt() != 0) {
1478 fullScreenIntent = PendingIntent.CREATOR.createFromParcel(parcel);
1479 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001480
1481 priority = parcel.readInt();
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001482
John Spurlockfd7f1e02014-03-18 16:41:57 -04001483 category = parcel.readString();
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001484
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001485 mGroupKey = parcel.readString();
1486
1487 mSortKey = parcel.readString();
1488
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001489 extras = parcel.readBundle(); // may be null
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001490
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001491 actions = parcel.createTypedArray(Action.CREATOR); // may be null
1492
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001493 if (parcel.readInt() != 0) {
Daniel Sandlerf3b73432012-03-27 15:01:25 -04001494 bigContentView = RemoteViews.CREATOR.createFromParcel(parcel);
1495 }
Dan Sandler0bf2ed82013-12-21 23:33:41 -06001496
Chris Wren8fd39ec2014-02-27 17:43:26 -05001497 if (parcel.readInt() != 0) {
1498 headsUpContentView = RemoteViews.CREATOR.createFromParcel(parcel);
1499 }
1500
Dan Sandler0bf2ed82013-12-21 23:33:41 -06001501 visibility = parcel.readInt();
1502
1503 if (parcel.readInt() != 0) {
1504 publicVersion = Notification.CREATOR.createFromParcel(parcel);
1505 }
Dan Sandler26e81cf2014-05-06 10:01:27 -04001506
1507 color = parcel.readInt();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001508 }
1509
Andy Stadler110988c2010-12-03 14:29:16 -08001510 @Override
Joe Onorato18e69df2010-05-17 22:26:12 -07001511 public Notification clone() {
1512 Notification that = new Notification();
Daniel Sandler1a497d32013-04-18 14:52:45 -04001513 cloneInto(that, true);
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001514 return that;
1515 }
Joe Onorato18e69df2010-05-17 22:26:12 -07001516
Daniel Sandler1a497d32013-04-18 14:52:45 -04001517 /**
1518 * Copy all (or if heavy is false, all except Bitmaps and RemoteViews) members
1519 * of this into that.
1520 * @hide
1521 */
1522 public void cloneInto(Notification that, boolean heavy) {
Joe Onorato18e69df2010-05-17 22:26:12 -07001523 that.when = this.when;
Dan Sandlerd63f9322015-05-06 15:18:49 -04001524 that.mSmallIcon = this.mSmallIcon;
Joe Onorato18e69df2010-05-17 22:26:12 -07001525 that.number = this.number;
1526
1527 // PendingIntents are global, so there's no reason (or way) to clone them.
1528 that.contentIntent = this.contentIntent;
1529 that.deleteIntent = this.deleteIntent;
Daniel Sandlere46cbd32010-06-17 10:35:26 -04001530 that.fullScreenIntent = this.fullScreenIntent;
Joe Onorato18e69df2010-05-17 22:26:12 -07001531
1532 if (this.tickerText != null) {
1533 that.tickerText = this.tickerText.toString();
1534 }
Daniel Sandler1a497d32013-04-18 14:52:45 -04001535 if (heavy && this.tickerView != null) {
Joe Onorato46439ce2010-11-19 13:56:21 -08001536 that.tickerView = this.tickerView.clone();
Joe Onoratoef1e7762010-09-17 18:38:38 -04001537 }
Daniel Sandler1a497d32013-04-18 14:52:45 -04001538 if (heavy && this.contentView != null) {
Joe Onorato18e69df2010-05-17 22:26:12 -07001539 that.contentView = this.contentView.clone();
1540 }
Dan Sandlerd63f9322015-05-06 15:18:49 -04001541 if (heavy && this.mLargeIcon != null) {
1542 that.mLargeIcon = this.mLargeIcon;
Joe Onorato561d3852010-11-20 18:09:34 -08001543 }
Jozef BABJAKa8b91832011-02-22 08:05:08 +01001544 that.iconLevel = this.iconLevel;
Joe Onorato18e69df2010-05-17 22:26:12 -07001545 that.sound = this.sound; // android.net.Uri is immutable
1546 that.audioStreamType = this.audioStreamType;
John Spurlockc0650f022014-07-19 13:22:39 -04001547 if (this.audioAttributes != null) {
1548 that.audioAttributes = new AudioAttributes.Builder(this.audioAttributes).build();
1549 }
Joe Onorato18e69df2010-05-17 22:26:12 -07001550
1551 final long[] vibrate = this.vibrate;
1552 if (vibrate != null) {
1553 final int N = vibrate.length;
1554 final long[] vib = that.vibrate = new long[N];
1555 System.arraycopy(vibrate, 0, vib, 0, N);
1556 }
1557
1558 that.ledARGB = this.ledARGB;
1559 that.ledOnMS = this.ledOnMS;
1560 that.ledOffMS = this.ledOffMS;
1561 that.defaults = this.defaults;
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001562
Joe Onorato18e69df2010-05-17 22:26:12 -07001563 that.flags = this.flags;
1564
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001565 that.priority = this.priority;
Joe Malin8d40d042012-11-05 11:36:40 -08001566
John Spurlockfd7f1e02014-03-18 16:41:57 -04001567 that.category = this.category;
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001568
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001569 that.mGroupKey = this.mGroupKey;
1570
1571 that.mSortKey = this.mSortKey;
1572
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001573 if (this.extras != null) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04001574 try {
1575 that.extras = new Bundle(this.extras);
1576 // will unparcel
1577 that.extras.size();
1578 } catch (BadParcelableException e) {
1579 Log.e(TAG, "could not unparcel extras from notification: " + this, e);
1580 that.extras = null;
1581 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001582 }
1583
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001584 if (this.actions != null) {
1585 that.actions = new Action[this.actions.length];
1586 for(int i=0; i<this.actions.length; i++) {
1587 that.actions[i] = this.actions[i].clone();
1588 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001589 }
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001590
Daniel Sandler1a497d32013-04-18 14:52:45 -04001591 if (heavy && this.bigContentView != null) {
Daniel Sandlerf3b73432012-03-27 15:01:25 -04001592 that.bigContentView = this.bigContentView.clone();
1593 }
Daniel Sandler1a497d32013-04-18 14:52:45 -04001594
Chris Wren8fd39ec2014-02-27 17:43:26 -05001595 if (heavy && this.headsUpContentView != null) {
1596 that.headsUpContentView = this.headsUpContentView.clone();
1597 }
1598
Dan Sandler0bf2ed82013-12-21 23:33:41 -06001599 that.visibility = this.visibility;
1600
1601 if (this.publicVersion != null) {
1602 that.publicVersion = new Notification();
1603 this.publicVersion.cloneInto(that.publicVersion, heavy);
1604 }
1605
Dan Sandler26e81cf2014-05-06 10:01:27 -04001606 that.color = this.color;
1607
Daniel Sandler1a497d32013-04-18 14:52:45 -04001608 if (!heavy) {
1609 that.lightenPayload(); // will clean out extras
1610 }
1611 }
1612
1613 /**
1614 * Removes heavyweight parts of the Notification object for archival or for sending to
1615 * listeners when the full contents are not necessary.
1616 * @hide
1617 */
1618 public final void lightenPayload() {
1619 tickerView = null;
1620 contentView = null;
1621 bigContentView = null;
Chris Wren8fd39ec2014-02-27 17:43:26 -05001622 headsUpContentView = null;
Dan Sandlerd63f9322015-05-06 15:18:49 -04001623 mLargeIcon = null;
Dan Sandler50128532015-12-08 15:42:41 -05001624 if (extras != null && !extras.isEmpty()) {
1625 final Set<String> keyset = extras.keySet();
1626 final int N = keyset.size();
1627 final String[] keys = keyset.toArray(new String[N]);
1628 for (int i=0; i<N; i++) {
1629 final String key = keys[i];
1630 final Object obj = extras.get(key);
1631 if (obj != null &&
1632 ( obj instanceof Parcelable
1633 || obj instanceof Parcelable[]
1634 || obj instanceof SparseArray
1635 || obj instanceof ArrayList)) {
1636 extras.remove(key);
1637 }
1638 }
Daniel Sandler1a497d32013-04-18 14:52:45 -04001639 }
Joe Onorato18e69df2010-05-17 22:26:12 -07001640 }
1641
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04001642 /**
1643 * Make sure this CharSequence is safe to put into a bundle, which basically
1644 * means it had better not be some custom Parcelable implementation.
1645 * @hide
1646 */
1647 public static CharSequence safeCharSequence(CharSequence cs) {
Christoph Studer535ec612014-09-03 15:47:47 +02001648 if (cs == null) return cs;
1649 if (cs.length() > MAX_CHARSEQUENCE_LENGTH) {
1650 cs = cs.subSequence(0, MAX_CHARSEQUENCE_LENGTH);
1651 }
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04001652 if (cs instanceof Parcelable) {
1653 Log.e(TAG, "warning: " + cs.getClass().getCanonicalName()
1654 + " instance is a custom Parcelable and not allowed in Notification");
1655 return cs.toString();
1656 }
Selim Cinek60a54252016-02-26 17:03:25 -08001657 return removeTextSizeSpans(cs);
1658 }
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04001659
Selim Cinek60a54252016-02-26 17:03:25 -08001660 private static CharSequence removeTextSizeSpans(CharSequence charSequence) {
1661 if (charSequence instanceof Spanned) {
1662 Spanned ss = (Spanned) charSequence;
1663 Object[] spans = ss.getSpans(0, ss.length(), Object.class);
1664 SpannableStringBuilder builder = new SpannableStringBuilder(ss.toString());
1665 for (Object span : spans) {
1666 Object resultSpan = span;
1667 if (span instanceof TextAppearanceSpan) {
1668 TextAppearanceSpan originalSpan = (TextAppearanceSpan) span;
1669 resultSpan = new TextAppearanceSpan(
1670 originalSpan.getFamily(),
1671 originalSpan.getTextStyle(),
1672 -1,
1673 originalSpan.getTextColor(),
1674 originalSpan.getLinkTextColor());
1675 } else if (span instanceof RelativeSizeSpan
1676 || span instanceof AbsoluteSizeSpan) {
1677 continue;
1678 }
1679 builder.setSpan(resultSpan, ss.getSpanStart(span), ss.getSpanEnd(span),
1680 ss.getSpanFlags(span));
1681 }
1682 return builder;
1683 }
1684 return charSequence;
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04001685 }
1686
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001687 public int describeContents() {
1688 return 0;
1689 }
1690
1691 /**
Dan Sandler4e787062015-06-17 15:09:48 -04001692 * Flatten this notification into a parcel.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001693 */
1694 public void writeToParcel(Parcel parcel, int flags)
1695 {
1696 parcel.writeInt(1);
1697
1698 parcel.writeLong(when);
Dan Sandler4e787062015-06-17 15:09:48 -04001699 if (mSmallIcon == null && icon != 0) {
1700 // you snuck an icon in here without using the builder; let's try to keep it
1701 mSmallIcon = Icon.createWithResource("", icon);
1702 }
Dan Sandler3936e7a2015-05-19 20:59:12 -04001703 if (mSmallIcon != null) {
1704 parcel.writeInt(1);
1705 mSmallIcon.writeToParcel(parcel, 0);
1706 } else {
1707 parcel.writeInt(0);
1708 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001709 parcel.writeInt(number);
1710 if (contentIntent != null) {
1711 parcel.writeInt(1);
1712 contentIntent.writeToParcel(parcel, 0);
1713 } else {
1714 parcel.writeInt(0);
1715 }
1716 if (deleteIntent != null) {
1717 parcel.writeInt(1);
1718 deleteIntent.writeToParcel(parcel, 0);
1719 } else {
1720 parcel.writeInt(0);
1721 }
1722 if (tickerText != null) {
1723 parcel.writeInt(1);
1724 TextUtils.writeToParcel(tickerText, parcel, flags);
1725 } else {
1726 parcel.writeInt(0);
1727 }
Joe Onorato46439ce2010-11-19 13:56:21 -08001728 if (tickerView != null) {
Joe Onoratoef1e7762010-09-17 18:38:38 -04001729 parcel.writeInt(1);
Joe Onorato46439ce2010-11-19 13:56:21 -08001730 tickerView.writeToParcel(parcel, 0);
Joe Onoratoef1e7762010-09-17 18:38:38 -04001731 } else {
1732 parcel.writeInt(0);
1733 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001734 if (contentView != null) {
1735 parcel.writeInt(1);
1736 contentView.writeToParcel(parcel, 0);
1737 } else {
1738 parcel.writeInt(0);
1739 }
Dan Sandlerd63f9322015-05-06 15:18:49 -04001740 if (mLargeIcon != null) {
Joe Onorato561d3852010-11-20 18:09:34 -08001741 parcel.writeInt(1);
Dan Sandlerd63f9322015-05-06 15:18:49 -04001742 mLargeIcon.writeToParcel(parcel, 0);
Joe Onorato561d3852010-11-20 18:09:34 -08001743 } else {
1744 parcel.writeInt(0);
1745 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001746
1747 parcel.writeInt(defaults);
1748 parcel.writeInt(this.flags);
1749
1750 if (sound != null) {
1751 parcel.writeInt(1);
1752 sound.writeToParcel(parcel, 0);
1753 } else {
1754 parcel.writeInt(0);
1755 }
1756 parcel.writeInt(audioStreamType);
John Spurlockc0650f022014-07-19 13:22:39 -04001757
1758 if (audioAttributes != null) {
1759 parcel.writeInt(1);
1760 audioAttributes.writeToParcel(parcel, 0);
1761 } else {
1762 parcel.writeInt(0);
1763 }
1764
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001765 parcel.writeLongArray(vibrate);
1766 parcel.writeInt(ledARGB);
1767 parcel.writeInt(ledOnMS);
1768 parcel.writeInt(ledOffMS);
1769 parcel.writeInt(iconLevel);
Daniel Sandlere46cbd32010-06-17 10:35:26 -04001770
1771 if (fullScreenIntent != null) {
1772 parcel.writeInt(1);
1773 fullScreenIntent.writeToParcel(parcel, 0);
1774 } else {
1775 parcel.writeInt(0);
1776 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001777
1778 parcel.writeInt(priority);
Joe Malin8d40d042012-11-05 11:36:40 -08001779
John Spurlockfd7f1e02014-03-18 16:41:57 -04001780 parcel.writeString(category);
Joe Malin8d40d042012-11-05 11:36:40 -08001781
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001782 parcel.writeString(mGroupKey);
1783
1784 parcel.writeString(mSortKey);
1785
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001786 parcel.writeBundle(extras); // null ok
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001787
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001788 parcel.writeTypedArray(actions, 0); // null ok
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001789
Daniel Sandlerf3b73432012-03-27 15:01:25 -04001790 if (bigContentView != null) {
1791 parcel.writeInt(1);
1792 bigContentView.writeToParcel(parcel, 0);
1793 } else {
1794 parcel.writeInt(0);
1795 }
Dan Sandler0bf2ed82013-12-21 23:33:41 -06001796
Chris Wren8fd39ec2014-02-27 17:43:26 -05001797 if (headsUpContentView != null) {
1798 parcel.writeInt(1);
1799 headsUpContentView.writeToParcel(parcel, 0);
1800 } else {
1801 parcel.writeInt(0);
1802 }
1803
Dan Sandler0bf2ed82013-12-21 23:33:41 -06001804 parcel.writeInt(visibility);
1805
1806 if (publicVersion != null) {
1807 parcel.writeInt(1);
1808 publicVersion.writeToParcel(parcel, 0);
1809 } else {
1810 parcel.writeInt(0);
1811 }
Dan Sandler26e81cf2014-05-06 10:01:27 -04001812
1813 parcel.writeInt(color);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001814 }
1815
1816 /**
1817 * Parcelable.Creator that instantiates Notification objects
1818 */
1819 public static final Parcelable.Creator<Notification> CREATOR
1820 = new Parcelable.Creator<Notification>()
1821 {
1822 public Notification createFromParcel(Parcel parcel)
1823 {
1824 return new Notification(parcel);
1825 }
1826
1827 public Notification[] newArray(int size)
1828 {
1829 return new Notification[size];
1830 }
1831 };
1832
1833 /**
1834 * Sets the {@link #contentView} field to be a view with the standard "Latest Event"
1835 * layout.
1836 *
1837 * <p>Uses the {@link #icon} and {@link #when} fields to set the icon and time fields
1838 * in the view.</p>
1839 * @param context The context for your application / activity.
1840 * @param contentTitle The title that goes in the expanded entry.
1841 * @param contentText The text that goes in the expanded entry.
1842 * @param contentIntent The intent to launch when the user clicks the expanded notification.
1843 * If this is an activity, it must include the
1844 * {@link android.content.Intent#FLAG_ACTIVITY_NEW_TASK} flag, which requires
Scott Main7aee61f2011-02-08 11:25:01 -08001845 * that you take care of task management as described in the
1846 * <a href="{@docRoot}guide/topics/fundamentals/tasks-and-back-stack.html">Tasks and Back
1847 * Stack</a> document.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001848 *
Joe Onorato46439ce2010-11-19 13:56:21 -08001849 * @deprecated Use {@link Builder} instead.
Chris Wrena05db382015-06-24 15:18:34 -04001850 * @removed
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001851 */
Joe Onorato46439ce2010-11-19 13:56:21 -08001852 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001853 public void setLatestEventInfo(Context context,
1854 CharSequence contentTitle, CharSequence contentText, PendingIntent contentIntent) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04001855 if (context.getApplicationInfo().targetSdkVersion > Build.VERSION_CODES.LOLLIPOP_MR1){
1856 Log.e(TAG, "setLatestEventInfo() is deprecated and you should feel deprecated.",
1857 new Throwable());
1858 }
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001859
Julia Reynoldsd9228f12015-10-20 10:37:27 -04001860 // ensure that any information already set directly is preserved
1861 final Notification.Builder builder = new Notification.Builder(context, this);
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001862
1863 // now apply the latestEventInfo fields
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001864 if (contentTitle != null) {
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001865 builder.setContentTitle(contentTitle);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001866 }
1867 if (contentText != null) {
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001868 builder.setContentText(contentText);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001869 }
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001870 builder.setContentIntent(contentIntent);
Julia Reynoldsd9228f12015-10-20 10:37:27 -04001871
1872 builder.build(); // callers expect this notification to be ready to use
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001873 }
1874
Julia Reynoldsda303542015-11-23 14:00:20 -05001875 /**
1876 * @hide
1877 */
1878 public static void addFieldsFromContext(Context context, Notification notification) {
1879 if (notification.extras.getParcelable(EXTRA_BUILDER_APPLICATION_INFO) == null) {
1880 notification.extras.putParcelable(EXTRA_BUILDER_APPLICATION_INFO,
1881 context.getApplicationInfo());
1882 }
1883 if (!notification.extras.containsKey(EXTRA_ORIGINATING_USERID)) {
1884 notification.extras.putInt(EXTRA_ORIGINATING_USERID, context.getUserId());
1885 }
1886 }
1887
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001888 @Override
1889 public String toString() {
1890 StringBuilder sb = new StringBuilder();
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001891 sb.append("Notification(pri=");
1892 sb.append(priority);
1893 sb.append(" contentView=");
Joe Onoratoc9596d62011-01-12 17:03:11 -08001894 if (contentView != null) {
1895 sb.append(contentView.getPackage());
1896 sb.append("/0x");
1897 sb.append(Integer.toHexString(contentView.getLayoutId()));
1898 } else {
1899 sb.append("null");
1900 }
1901 sb.append(" vibrate=");
Daniel Sandler6738eee2012-11-16 12:03:32 -05001902 if ((this.defaults & DEFAULT_VIBRATE) != 0) {
1903 sb.append("default");
1904 } else if (this.vibrate != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001905 int N = this.vibrate.length-1;
1906 sb.append("[");
1907 for (int i=0; i<N; i++) {
1908 sb.append(this.vibrate[i]);
1909 sb.append(',');
1910 }
Simon Schoar8cf97d92009-06-10 22:08:37 +02001911 if (N != -1) {
1912 sb.append(this.vibrate[N]);
1913 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001914 sb.append("]");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001915 } else {
1916 sb.append("null");
1917 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001918 sb.append(" sound=");
Daniel Sandler6738eee2012-11-16 12:03:32 -05001919 if ((this.defaults & DEFAULT_SOUND) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001920 sb.append("default");
Daniel Sandler6738eee2012-11-16 12:03:32 -05001921 } else if (this.sound != null) {
1922 sb.append(this.sound.toString());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001923 } else {
1924 sb.append("null");
1925 }
Chris Wren365b6d32015-07-16 10:39:26 -04001926 if (this.tickerText != null) {
1927 sb.append(" tick");
1928 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001929 sb.append(" defaults=0x");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001930 sb.append(Integer.toHexString(this.defaults));
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001931 sb.append(" flags=0x");
Daniel Sandlere46cbd32010-06-17 10:35:26 -04001932 sb.append(Integer.toHexString(this.flags));
Dan Sandler26e81cf2014-05-06 10:01:27 -04001933 sb.append(String.format(" color=0x%08x", this.color));
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001934 if (this.category != null) {
1935 sb.append(" category=");
1936 sb.append(this.category);
1937 }
1938 if (this.mGroupKey != null) {
1939 sb.append(" groupKey=");
1940 sb.append(this.mGroupKey);
1941 }
1942 if (this.mSortKey != null) {
1943 sb.append(" sortKey=");
1944 sb.append(this.mSortKey);
1945 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001946 if (actions != null) {
Dan Sandler1b718782014-07-18 12:43:45 -04001947 sb.append(" actions=");
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001948 sb.append(actions.length);
Dan Sandler1b718782014-07-18 12:43:45 -04001949 }
1950 sb.append(" vis=");
1951 sb.append(visibilityToString(this.visibility));
1952 if (this.publicVersion != null) {
1953 sb.append(" publicVersion=");
1954 sb.append(publicVersion.toString());
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001955 }
1956 sb.append(")");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001957 return sb.toString();
1958 }
Joe Onorato46439ce2010-11-19 13:56:21 -08001959
Dan Sandler1b718782014-07-18 12:43:45 -04001960 /**
1961 * {@hide}
1962 */
1963 public static String visibilityToString(int vis) {
1964 switch (vis) {
1965 case VISIBILITY_PRIVATE:
1966 return "PRIVATE";
1967 case VISIBILITY_PUBLIC:
1968 return "PUBLIC";
1969 case VISIBILITY_SECRET:
1970 return "SECRET";
1971 default:
1972 return "UNKNOWN(" + String.valueOf(vis) + ")";
1973 }
1974 }
1975
Joe Onoratocb109a02011-01-18 17:57:41 -08001976 /**
John Spurlock1d881a12015-03-18 19:21:54 -04001977 * {@hide}
1978 */
1979 public static String priorityToString(@Priority int pri) {
1980 switch (pri) {
1981 case PRIORITY_MIN:
1982 return "MIN";
1983 case PRIORITY_LOW:
1984 return "LOW";
1985 case PRIORITY_DEFAULT:
1986 return "DEFAULT";
1987 case PRIORITY_HIGH:
1988 return "HIGH";
1989 case PRIORITY_MAX:
1990 return "MAX";
1991 default:
1992 return "UNKNOWN(" + String.valueOf(pri) + ")";
1993 }
1994 }
1995
1996 /**
Dan Sandlerd63f9322015-05-06 15:18:49 -04001997 * The small icon representing this notification in the status bar and content view.
1998 *
1999 * @return the small icon representing this notification.
2000 *
2001 * @see Builder#getSmallIcon()
2002 * @see Builder#setSmallIcon(Icon)
2003 */
2004 public Icon getSmallIcon() {
2005 return mSmallIcon;
2006 }
2007
2008 /**
2009 * Used when notifying to clean up legacy small icons.
2010 * @hide
2011 */
2012 public void setSmallIcon(Icon icon) {
2013 mSmallIcon = icon;
2014 }
2015
2016 /**
2017 * The large icon shown in this notification's content view.
2018 * @see Builder#getLargeIcon()
2019 * @see Builder#setLargeIcon(Icon)
2020 */
2021 public Icon getLargeIcon() {
2022 return mLargeIcon;
2023 }
2024
2025 /**
Christoph Studer4600f9b2014-07-22 22:44:43 +02002026 * @hide
2027 */
Christoph Studerc8db24b2014-07-25 17:50:30 +02002028 public boolean isGroupSummary() {
2029 return mGroupKey != null && (flags & FLAG_GROUP_SUMMARY) != 0;
2030 }
2031
2032 /**
2033 * @hide
2034 */
2035 public boolean isGroupChild() {
2036 return mGroupKey != null && (flags & FLAG_GROUP_SUMMARY) == 0;
2037 }
2038
2039 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002040 * Builder class for {@link Notification} objects.
Joe Malin8d40d042012-11-05 11:36:40 -08002041 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002042 * Provides a convenient way to set the various fields of a {@link Notification} and generate
Scott Main183bf112012-08-13 19:12:13 -07002043 * content views using the platform's notification layout template. If your app supports
2044 * versions of Android as old as API level 4, you can instead use
2045 * {@link android.support.v4.app.NotificationCompat.Builder NotificationCompat.Builder},
2046 * available in the <a href="{@docRoot}tools/extras/support-library.html">Android Support
2047 * library</a>.
Joe Malin8d40d042012-11-05 11:36:40 -08002048 *
Scott Main183bf112012-08-13 19:12:13 -07002049 * <p>Example:
Joe Malin8d40d042012-11-05 11:36:40 -08002050 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002051 * <pre class="prettyprint">
Scott Main183bf112012-08-13 19:12:13 -07002052 * Notification noti = new Notification.Builder(mContext)
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002053 * .setContentTitle(&quot;New mail from &quot; + sender.toString())
2054 * .setContentText(subject)
2055 * .setSmallIcon(R.drawable.new_mail)
2056 * .setLargeIcon(aBitmap)
Chris Wrenfbd96ba2012-05-01 12:03:58 -04002057 * .build();
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002058 * </pre>
Joe Onoratocb109a02011-01-18 17:57:41 -08002059 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002060 public static class Builder {
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05002061 /**
2062 * @hide
2063 */
2064 public static final String EXTRA_REBUILD_CONTENT_VIEW_ACTION_COUNT =
2065 "android.rebuild.contentViewActionCount";
2066 /**
2067 * @hide
2068 */
2069 public static final String EXTRA_REBUILD_BIG_CONTENT_VIEW_ACTION_COUNT
2070 = "android.rebuild.bigViewActionCount";
2071 /**
2072 * @hide
2073 */
2074 public static final String EXTRA_REBUILD_HEADS_UP_CONTENT_VIEW_ACTION_COUNT
2075 = "android.rebuild.hudViewActionCount";
2076
Daniel Sandler602ad1c2012-06-12 16:06:27 -04002077 private static final int MAX_ACTION_BUTTONS = 3;
Daniel Sandler8680bf82012-05-15 16:52:52 -04002078
Joe Onorato46439ce2010-11-19 13:56:21 -08002079 private Context mContext;
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002080 private Notification mN;
2081 private Bundle mUserExtras = new Bundle();
Chris Wrenfbd96ba2012-05-01 12:03:58 -04002082 private Style mStyle;
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002083 private ArrayList<Action> mActions = new ArrayList<Action>(MAX_ACTION_BUTTONS);
2084 private ArrayList<String> mPersonList = new ArrayList<String>();
2085 private NotificationColorUtil mColorUtil;
2086 private boolean mColorUtilInited = false;
Christoph Studer7ac80e62014-08-04 16:01:57 +02002087
2088 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002089 * Constructs a new Builder with the defaults:
Joe Onoratocb109a02011-01-18 17:57:41 -08002090 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002091
2092 * <table>
2093 * <tr><th align=right>priority</th>
2094 * <td>{@link #PRIORITY_DEFAULT}</td></tr>
2095 * <tr><th align=right>when</th>
2096 * <td>now ({@link System#currentTimeMillis()})</td></tr>
2097 * <tr><th align=right>audio stream</th>
2098 * <td>{@link #STREAM_DEFAULT}</td></tr>
2099 * </table>
Joe Onoratocb109a02011-01-18 17:57:41 -08002100 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002101
2102 * @param context
2103 * A {@link Context} that will be used by the Builder to construct the
2104 * RemoteViews. The Context will not be held past the lifetime of this Builder
2105 * object.
Joe Onoratocb109a02011-01-18 17:57:41 -08002106 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002107 public Builder(Context context) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002108 this(context, null);
Joe Onorato46439ce2010-11-19 13:56:21 -08002109 }
2110
Joe Onoratocb109a02011-01-18 17:57:41 -08002111 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002112 * @hide
Christoph Studer4600f9b2014-07-22 22:44:43 +02002113 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002114 public Builder(Context context, Notification toAdopt) {
2115 mContext = context;
Christoph Studer4600f9b2014-07-22 22:44:43 +02002116
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002117 if (toAdopt == null) {
2118 mN = new Notification();
2119 mN.extras.putBoolean(EXTRA_SHOW_WHEN, true);
2120 mN.priority = PRIORITY_DEFAULT;
2121 mN.visibility = VISIBILITY_PRIVATE;
2122 } else {
2123 mN = toAdopt;
2124 if (mN.actions != null) {
2125 Collections.addAll(mActions, mN.actions);
Christoph Studer4600f9b2014-07-22 22:44:43 +02002126 }
2127
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002128 if (mN.extras.containsKey(EXTRA_PEOPLE)) {
2129 Collections.addAll(mPersonList, mN.extras.getStringArray(EXTRA_PEOPLE));
2130 }
2131
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002132 String templateClass = mN.extras.getString(EXTRA_TEMPLATE);
2133 if (!TextUtils.isEmpty(templateClass)) {
2134 final Class<? extends Style> styleClass
2135 = getNotificationStyleClass(templateClass);
2136 if (styleClass == null) {
2137 Log.d(TAG, "Unknown style class: " + templateClass);
2138 } else {
2139 try {
2140 final Constructor<? extends Style> ctor = styleClass.getConstructor();
2141 ctor.setAccessible(true);
2142 final Style style = ctor.newInstance();
2143 style.restoreFromExtras(mN.extras);
2144
2145 if (style != null) {
2146 setStyle(style);
2147 }
2148 } catch (Throwable t) {
2149 Log.e(TAG, "Could not create Style", t);
2150 }
2151 }
2152 }
2153
2154 }
2155 }
2156
2157 private NotificationColorUtil getColorUtil() {
2158 if (!mColorUtilInited) {
2159 mColorUtilInited = true;
2160 if (mContext.getApplicationInfo().targetSdkVersion < Build.VERSION_CODES.LOLLIPOP) {
2161 mColorUtil = NotificationColorUtil.getInstance(mContext);
Christoph Studer4600f9b2014-07-22 22:44:43 +02002162 }
2163 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002164 return mColorUtil;
Christoph Studer4600f9b2014-07-22 22:44:43 +02002165 }
2166
2167 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002168 * Add a timestamp pertaining to the notification (usually the time the event occurred).
Daniel Sandler0c890492012-09-12 17:23:10 -07002169 * It will be shown in the notification content view by default; use
Griff Hazen50c11652014-05-16 09:46:31 -07002170 * {@link #setShowWhen(boolean) setShowWhen} to control this.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002171 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002172 * @see Notification#when
Joe Onoratocb109a02011-01-18 17:57:41 -08002173 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002174 public Builder setWhen(long when) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002175 mN.when = when;
Joe Onorato46439ce2010-11-19 13:56:21 -08002176 return this;
2177 }
2178
Joe Onoratocb109a02011-01-18 17:57:41 -08002179 /**
Griff Hazen50c11652014-05-16 09:46:31 -07002180 * Control whether the timestamp set with {@link #setWhen(long) setWhen} is shown
Daniel Sandler0c890492012-09-12 17:23:10 -07002181 * in the content view.
2182 */
2183 public Builder setShowWhen(boolean show) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002184 mN.extras.putBoolean(EXTRA_SHOW_WHEN, show);
Daniel Sandler0c890492012-09-12 17:23:10 -07002185 return this;
2186 }
2187
2188 /**
Daniel Sandlerd33b8032012-05-10 11:41:48 -04002189 * Show the {@link Notification#when} field as a stopwatch.
Joe Malin8d40d042012-11-05 11:36:40 -08002190 *
2191 * Instead of presenting <code>when</code> as a timestamp, the notification will show an
Daniel Sandlerd33b8032012-05-10 11:41:48 -04002192 * automatically updating display of the minutes and seconds since <code>when</code>.
Daniel Sandlera2985ed2012-04-03 16:42:00 -04002193 *
Daniel Sandlerd33b8032012-05-10 11:41:48 -04002194 * Useful when showing an elapsed time (like an ongoing phone call).
2195 *
Selim Cinek81c23aa2016-02-25 16:23:13 -08002196 * The counter can also be set to count down to <code>when</code> when using
2197 * {@link #setChronometerCountsDown(boolean)}.
2198 *
Daniel Sandlerd33b8032012-05-10 11:41:48 -04002199 * @see android.widget.Chronometer
Daniel Sandlera2985ed2012-04-03 16:42:00 -04002200 * @see Notification#when
Selim Cinek81c23aa2016-02-25 16:23:13 -08002201 * @see #setChronometerCountsDown(boolean)
Daniel Sandlera2985ed2012-04-03 16:42:00 -04002202 */
2203 public Builder setUsesChronometer(boolean b) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002204 mN.extras.putBoolean(EXTRA_SHOW_CHRONOMETER, b);
Daniel Sandlera2985ed2012-04-03 16:42:00 -04002205 return this;
2206 }
2207
2208 /**
Selim Cinek81c23aa2016-02-25 16:23:13 -08002209 * Sets the Chronometer to count down instead of counting up.
2210 *
2211 * <p>This is only relevant if {@link #setUsesChronometer(boolean)} has been set to true.
2212 * If it isn't set the chronometer will count up.
2213 *
2214 * @see #setUsesChronometer(boolean)
2215 */
2216 public Builder setChronometerCountsDown(boolean countsDown) {
2217 mN.extras.putBoolean(EXTRA_CHRONOMETER_COUNTS_DOWN, countsDown);
2218 return this;
2219 }
2220
2221 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002222 * Set the small icon resource, which will be used to represent the notification in the
2223 * status bar.
Joe Onoratocb109a02011-01-18 17:57:41 -08002224 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002225
2226 * The platform template for the expanded view will draw this icon in the left, unless a
2227 * {@link #setLargeIcon(Bitmap) large icon} has also been specified, in which case the small
2228 * icon will be moved to the right-hand side.
2229 *
2230
2231 * @param icon
2232 * A resource ID in the application's package of the drawable to use.
2233 * @see Notification#icon
Joe Onoratocb109a02011-01-18 17:57:41 -08002234 */
Tor Norbye7b9c9122013-05-30 16:48:33 -07002235 public Builder setSmallIcon(@DrawableRes int icon) {
Dan Sandlerd63f9322015-05-06 15:18:49 -04002236 return setSmallIcon(icon != 0
2237 ? Icon.createWithResource(mContext, icon)
2238 : null);
Joe Onorato46439ce2010-11-19 13:56:21 -08002239 }
2240
Joe Onoratocb109a02011-01-18 17:57:41 -08002241 /**
2242 * A variant of {@link #setSmallIcon(int) setSmallIcon(int)} that takes an additional
2243 * level parameter for when the icon is a {@link android.graphics.drawable.LevelListDrawable
2244 * LevelListDrawable}.
2245 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002246 * @param icon A resource ID in the application's package of the drawable to use.
Joe Onoratocb109a02011-01-18 17:57:41 -08002247 * @param level The level to use for the icon.
2248 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002249 * @see Notification#icon
2250 * @see Notification#iconLevel
Joe Onoratocb109a02011-01-18 17:57:41 -08002251 */
Tor Norbye7b9c9122013-05-30 16:48:33 -07002252 public Builder setSmallIcon(@DrawableRes int icon, int level) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002253 mN.iconLevel = level;
Dan Sandlerd63f9322015-05-06 15:18:49 -04002254 return setSmallIcon(icon);
2255 }
2256
2257 /**
2258 * Set the small icon, which will be used to represent the notification in the
2259 * status bar and content view (unless overriden there by a
2260 * {@link #setLargeIcon(Bitmap) large icon}).
2261 *
2262 * @param icon An Icon object to use.
2263 * @see Notification#icon
2264 */
2265 public Builder setSmallIcon(Icon icon) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002266 mN.setSmallIcon(icon);
2267 if (icon != null && icon.getType() == Icon.TYPE_RESOURCE) {
2268 mN.icon = icon.getResId();
2269 }
Joe Onorato46439ce2010-11-19 13:56:21 -08002270 return this;
2271 }
2272
Joe Onoratocb109a02011-01-18 17:57:41 -08002273 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002274 * Set the first line of text in the platform notification template.
Joe Onoratocb109a02011-01-18 17:57:41 -08002275 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002276 public Builder setContentTitle(CharSequence title) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002277 mN.extras.putCharSequence(EXTRA_TITLE, safeCharSequence(title));
Joe Onorato46439ce2010-11-19 13:56:21 -08002278 return this;
2279 }
2280
Joe Onoratocb109a02011-01-18 17:57:41 -08002281 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002282 * Set the second line of text in the platform notification template.
Joe Onoratocb109a02011-01-18 17:57:41 -08002283 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002284 public Builder setContentText(CharSequence text) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002285 mN.extras.putCharSequence(EXTRA_TEXT, safeCharSequence(text));
Joe Onorato46439ce2010-11-19 13:56:21 -08002286 return this;
2287 }
2288
Joe Onoratocb109a02011-01-18 17:57:41 -08002289 /**
Joe Malin8d40d042012-11-05 11:36:40 -08002290 * Set the third line of text in the platform notification template.
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04002291 * Don't use if you're also using {@link #setProgress(int, int, boolean)}; they occupy the
2292 * same location in the standard template.
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002293 */
2294 public Builder setSubText(CharSequence text) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002295 mN.extras.putCharSequence(EXTRA_SUB_TEXT, safeCharSequence(text));
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002296 return this;
2297 }
2298
2299 /**
Adrian Roose458aa82015-12-08 16:17:19 -08002300 * Set the remote input history.
2301 *
2302 * This should be set to the most recent inputs that have been sent
2303 * through a {@link RemoteInput} of this Notification and cleared once the it is no
2304 * longer relevant (e.g. for chat notifications once the other party has responded).
2305 *
2306 * The most recent input must be stored at the 0 index, the second most recent at the
2307 * 1 index, etc. Note that the system will limit both how far back the inputs will be shown
2308 * and how much of each individual input is shown.
2309 *
2310 * <p>Note: The reply text will only be shown on notifications that have least one action
2311 * with a {@code RemoteInput}.</p>
2312 */
2313 public Builder setRemoteInputHistory(CharSequence[] text) {
2314 if (text == null) {
2315 mN.extras.putCharSequenceArray(EXTRA_REMOTE_INPUT_HISTORY, null);
2316 } else {
2317 final int N = Math.min(MAX_REPLY_HISTORY, text.length);
2318 CharSequence[] safe = new CharSequence[N];
2319 for (int i = 0; i < N; i++) {
2320 safe[i] = safeCharSequence(text[i]);
2321 }
2322 mN.extras.putCharSequenceArray(EXTRA_REMOTE_INPUT_HISTORY, safe);
2323 }
2324 return this;
2325 }
2326
2327 /**
Joe Onoratocb109a02011-01-18 17:57:41 -08002328 * Set the large number at the right-hand side of the notification. This is
2329 * equivalent to setContentInfo, although it might show the number in a different
2330 * font size for readability.
2331 */
Joe Onorato8595a3d2010-11-19 18:12:07 -08002332 public Builder setNumber(int number) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002333 mN.number = number;
Joe Onorato8595a3d2010-11-19 18:12:07 -08002334 return this;
2335 }
2336
Joe Onoratocb109a02011-01-18 17:57:41 -08002337 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002338 * A small piece of additional information pertaining to this notification.
2339 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002340 * The platform template will draw this on the last line of the notification, at the far
2341 * right (to the right of a smallIcon if it has been placed there).
Joe Onoratocb109a02011-01-18 17:57:41 -08002342 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002343 public Builder setContentInfo(CharSequence info) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002344 mN.extras.putCharSequence(EXTRA_INFO_TEXT, safeCharSequence(info));
Joe Onorato46439ce2010-11-19 13:56:21 -08002345 return this;
2346 }
2347
Joe Onoratocb109a02011-01-18 17:57:41 -08002348 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002349 * Set the progress this notification represents.
2350 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002351 * The platform template will represent this using a {@link ProgressBar}.
Jeff Sharkey1c400132011-08-05 14:50:13 -07002352 */
2353 public Builder setProgress(int max, int progress, boolean indeterminate) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002354 mN.extras.putInt(EXTRA_PROGRESS, progress);
2355 mN.extras.putInt(EXTRA_PROGRESS_MAX, max);
2356 mN.extras.putBoolean(EXTRA_PROGRESS_INDETERMINATE, indeterminate);
Jeff Sharkey1c400132011-08-05 14:50:13 -07002357 return this;
2358 }
2359
2360 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002361 * Supply a custom RemoteViews to use instead of the platform template.
2362 *
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002363 * Use {@link #setCustomContentView(RemoteViews)} instead.
Joe Onoratocb109a02011-01-18 17:57:41 -08002364 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002365 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -08002366 public Builder setContent(RemoteViews views) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002367 return setCustomContentView(views);
2368 }
2369
2370 /**
2371 * Supply custom RemoteViews to use instead of the platform template.
2372 *
2373 * This will override the layout that would otherwise be constructed by this Builder
2374 * object.
2375 */
2376 public Builder setCustomContentView(RemoteViews contentView) {
2377 mN.contentView = contentView;
2378 return this;
2379 }
2380
2381 /**
2382 * Supply custom RemoteViews to use instead of the platform template in the expanded form.
2383 *
2384 * This will override the expanded layout that would otherwise be constructed by this
2385 * Builder object.
2386 */
2387 public Builder setCustomBigContentView(RemoteViews contentView) {
2388 mN.bigContentView = contentView;
2389 return this;
2390 }
2391
2392 /**
2393 * Supply custom RemoteViews to use instead of the platform template in the heads up dialog.
2394 *
2395 * This will override the heads-up layout that would otherwise be constructed by this
2396 * Builder object.
2397 */
2398 public Builder setCustomHeadsUpContentView(RemoteViews contentView) {
2399 mN.headsUpContentView = contentView;
Joe Onorato46439ce2010-11-19 13:56:21 -08002400 return this;
2401 }
2402
Joe Onoratocb109a02011-01-18 17:57:41 -08002403 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002404 * Supply a {@link PendingIntent} to be sent when the notification is clicked.
2405 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002406 * As of {@link android.os.Build.VERSION_CODES#HONEYCOMB}, if this field is unset and you
2407 * have specified a custom RemoteViews with {@link #setContent(RemoteViews)}, you can use
2408 * {@link RemoteViews#setOnClickPendingIntent RemoteViews.setOnClickPendingIntent(int,PendingIntent)}
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002409 * to assign PendingIntents to individual views in that custom layout (i.e., to create
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002410 * clickable buttons inside the notification view).
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002411 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002412 * @see Notification#contentIntent Notification.contentIntent
Joe Onoratocb109a02011-01-18 17:57:41 -08002413 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002414 public Builder setContentIntent(PendingIntent intent) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002415 mN.contentIntent = intent;
Joe Onorato46439ce2010-11-19 13:56:21 -08002416 return this;
2417 }
2418
Joe Onoratocb109a02011-01-18 17:57:41 -08002419 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002420 * Supply a {@link PendingIntent} to send when the notification is cleared explicitly by the user.
2421 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002422 * @see Notification#deleteIntent
Joe Onoratocb109a02011-01-18 17:57:41 -08002423 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002424 public Builder setDeleteIntent(PendingIntent intent) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002425 mN.deleteIntent = intent;
Joe Onorato46439ce2010-11-19 13:56:21 -08002426 return this;
2427 }
2428
Joe Onoratocb109a02011-01-18 17:57:41 -08002429 /**
2430 * An intent to launch instead of posting the notification to the status bar.
2431 * Only for use with extremely high-priority notifications demanding the user's
2432 * <strong>immediate</strong> attention, such as an incoming phone call or
2433 * alarm clock that the user has explicitly set to a particular time.
2434 * If this facility is used for something else, please give the user an option
2435 * to turn it off and use a normal notification, as this can be extremely
2436 * disruptive.
2437 *
Chris Wren47c20a12014-06-18 17:27:29 -04002438 * <p>
2439 * The system UI may choose to display a heads-up notification, instead of
2440 * launching this intent, while the user is using the device.
2441 * </p>
2442 *
Joe Onoratocb109a02011-01-18 17:57:41 -08002443 * @param intent The pending intent to launch.
2444 * @param highPriority Passing true will cause this notification to be sent
2445 * even if other notifications are suppressed.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002446 *
2447 * @see Notification#fullScreenIntent
Joe Onoratocb109a02011-01-18 17:57:41 -08002448 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002449 public Builder setFullScreenIntent(PendingIntent intent, boolean highPriority) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002450 mN.fullScreenIntent = intent;
Joe Onorato46439ce2010-11-19 13:56:21 -08002451 setFlag(FLAG_HIGH_PRIORITY, highPriority);
2452 return this;
2453 }
2454
Joe Onoratocb109a02011-01-18 17:57:41 -08002455 /**
Dan Sandler5fcdf6e2014-07-18 11:31:15 -04002456 * Set the "ticker" text which is sent to accessibility services.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002457 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002458 * @see Notification#tickerText
Joe Onoratocb109a02011-01-18 17:57:41 -08002459 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002460 public Builder setTicker(CharSequence tickerText) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002461 mN.tickerText = safeCharSequence(tickerText);
Joe Onorato46439ce2010-11-19 13:56:21 -08002462 return this;
2463 }
2464
Joe Onoratocb109a02011-01-18 17:57:41 -08002465 /**
Dan Sandler5fcdf6e2014-07-18 11:31:15 -04002466 * Obsolete version of {@link #setTicker(CharSequence)}.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002467 *
Joe Onoratocb109a02011-01-18 17:57:41 -08002468 */
Dan Sandler5fcdf6e2014-07-18 11:31:15 -04002469 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -08002470 public Builder setTicker(CharSequence tickerText, RemoteViews views) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002471 setTicker(tickerText);
2472 // views is ignored
Joe Onorato46439ce2010-11-19 13:56:21 -08002473 return this;
2474 }
2475
Joe Onoratocb109a02011-01-18 17:57:41 -08002476 /**
Dan Sandlerd63f9322015-05-06 15:18:49 -04002477 * Add a large icon to the notification content view.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002478 *
2479 * In the platform template, this image will be shown on the left of the notification view
Dan Sandlerd63f9322015-05-06 15:18:49 -04002480 * in place of the {@link #setSmallIcon(Icon) small icon} (which will be placed in a small
2481 * badge atop the large icon).
Dan Sandler08a04c12015-05-06 15:18:49 -04002482 */
Dan Sandlerd63f9322015-05-06 15:18:49 -04002483 public Builder setLargeIcon(Bitmap b) {
2484 return setLargeIcon(b != null ? Icon.createWithBitmap(b) : null);
2485 }
2486
2487 /**
2488 * Add a large icon to the notification content view.
2489 *
2490 * In the platform template, this image will be shown on the left of the notification view
2491 * in place of the {@link #setSmallIcon(Icon) small icon} (which will be placed in a small
2492 * badge atop the large icon).
2493 */
2494 public Builder setLargeIcon(Icon icon) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002495 mN.mLargeIcon = icon;
2496 mN.extras.putParcelable(EXTRA_LARGE_ICON, icon);
Joe Onorato46439ce2010-11-19 13:56:21 -08002497 return this;
2498 }
2499
Joe Onoratocb109a02011-01-18 17:57:41 -08002500 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002501 * Set the sound to play.
2502 *
John Spurlockc0650f022014-07-19 13:22:39 -04002503 * It will be played using the {@link #AUDIO_ATTRIBUTES_DEFAULT default audio attributes}
2504 * for notifications.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002505 *
Chris Wren47c20a12014-06-18 17:27:29 -04002506 * <p>
2507 * A notification that is noisy is more likely to be presented as a heads-up notification.
2508 * </p>
2509 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002510 * @see Notification#sound
Joe Onoratocb109a02011-01-18 17:57:41 -08002511 */
Joe Onorato52f80cd2010-11-21 15:34:48 -08002512 public Builder setSound(Uri sound) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002513 mN.sound = sound;
2514 mN.audioAttributes = AUDIO_ATTRIBUTES_DEFAULT;
Joe Onorato52f80cd2010-11-21 15:34:48 -08002515 return this;
2516 }
2517
Joe Onoratocb109a02011-01-18 17:57:41 -08002518 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002519 * Set the sound to play, along with a specific stream on which to play it.
Joe Onoratocb109a02011-01-18 17:57:41 -08002520 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002521 * See {@link android.media.AudioManager} for the <code>STREAM_</code> constants.
2522 *
Chris Wren47c20a12014-06-18 17:27:29 -04002523 * <p>
2524 * A notification that is noisy is more likely to be presented as a heads-up notification.
2525 * </p>
John Spurlockc0650f022014-07-19 13:22:39 -04002526 * @deprecated use {@link #setSound(Uri, AudioAttributes)} instead.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002527 * @see Notification#sound
Joe Onoratocb109a02011-01-18 17:57:41 -08002528 */
Jean-Michel Trivi81f871e2014-08-06 16:32:38 -07002529 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -08002530 public Builder setSound(Uri sound, int streamType) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002531 mN.sound = sound;
2532 mN.audioStreamType = streamType;
Joe Onorato46439ce2010-11-19 13:56:21 -08002533 return this;
2534 }
2535
Joe Onoratocb109a02011-01-18 17:57:41 -08002536 /**
John Spurlockc0650f022014-07-19 13:22:39 -04002537 * Set the sound to play, along with specific {@link AudioAttributes audio attributes} to
2538 * use during playback.
2539 *
2540 * <p>
2541 * A notification that is noisy is more likely to be presented as a heads-up notification.
2542 * </p>
2543 *
2544 * @see Notification#sound
2545 */
2546 public Builder setSound(Uri sound, AudioAttributes audioAttributes) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002547 mN.sound = sound;
2548 mN.audioAttributes = audioAttributes;
John Spurlockc0650f022014-07-19 13:22:39 -04002549 return this;
2550 }
2551
2552 /**
Joe Onoratocb109a02011-01-18 17:57:41 -08002553 * Set the vibration pattern to use.
2554 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002555 * See {@link android.os.Vibrator#vibrate(long[], int)} for a discussion of the
2556 * <code>pattern</code> parameter.
2557 *
Chris Wren47c20a12014-06-18 17:27:29 -04002558 * <p>
2559 * A notification that vibrates is more likely to be presented as a heads-up notification.
2560 * </p>
2561 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002562 * @see Notification#vibrate
Joe Onoratocb109a02011-01-18 17:57:41 -08002563 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002564 public Builder setVibrate(long[] pattern) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002565 mN.vibrate = pattern;
Joe Onorato46439ce2010-11-19 13:56:21 -08002566 return this;
2567 }
2568
Joe Onoratocb109a02011-01-18 17:57:41 -08002569 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002570 * Set the desired color for the indicator LED on the device, as well as the
2571 * blink duty cycle (specified in milliseconds).
2572 *
2573
2574 * Not all devices will honor all (or even any) of these values.
2575 *
2576
2577 * @see Notification#ledARGB
2578 * @see Notification#ledOnMS
2579 * @see Notification#ledOffMS
Joe Onoratocb109a02011-01-18 17:57:41 -08002580 */
Tor Norbye80756e32015-03-02 09:39:27 -08002581 public Builder setLights(@ColorInt int argb, int onMs, int offMs) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002582 mN.ledARGB = argb;
2583 mN.ledOnMS = onMs;
2584 mN.ledOffMS = offMs;
Julia Reynolds10ee1fc2015-11-09 11:04:55 -05002585 if (onMs != 0 || offMs != 0) {
2586 mN.flags |= FLAG_SHOW_LIGHTS;
2587 }
Joe Onorato46439ce2010-11-19 13:56:21 -08002588 return this;
2589 }
2590
Joe Onoratocb109a02011-01-18 17:57:41 -08002591 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002592 * Set whether this is an "ongoing" notification.
Joe Onoratocb109a02011-01-18 17:57:41 -08002593 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002594
2595 * Ongoing notifications cannot be dismissed by the user, so your application or service
2596 * must take care of canceling them.
2597 *
2598
2599 * They are typically used to indicate a background task that the user is actively engaged
2600 * with (e.g., playing music) or is pending in some way and therefore occupying the device
2601 * (e.g., a file download, sync operation, active network connection).
2602 *
2603
2604 * @see Notification#FLAG_ONGOING_EVENT
2605 * @see Service#setForeground(boolean)
Joe Onoratocb109a02011-01-18 17:57:41 -08002606 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002607 public Builder setOngoing(boolean ongoing) {
2608 setFlag(FLAG_ONGOING_EVENT, ongoing);
2609 return this;
2610 }
2611
Joe Onoratocb109a02011-01-18 17:57:41 -08002612 /**
2613 * Set this flag if you would only like the sound, vibrate
2614 * and ticker to be played if the notification is not already showing.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002615 *
2616 * @see Notification#FLAG_ONLY_ALERT_ONCE
Joe Onoratocb109a02011-01-18 17:57:41 -08002617 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002618 public Builder setOnlyAlertOnce(boolean onlyAlertOnce) {
2619 setFlag(FLAG_ONLY_ALERT_ONCE, onlyAlertOnce);
2620 return this;
2621 }
2622
Joe Onoratocb109a02011-01-18 17:57:41 -08002623 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002624 * Make this notification automatically dismissed when the user touches it. The
2625 * PendingIntent set with {@link #setDeleteIntent} will be sent when this happens.
2626 *
2627 * @see Notification#FLAG_AUTO_CANCEL
Joe Onoratocb109a02011-01-18 17:57:41 -08002628 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002629 public Builder setAutoCancel(boolean autoCancel) {
Joe Onorato281d83f2011-01-04 17:13:10 -08002630 setFlag(FLAG_AUTO_CANCEL, autoCancel);
Joe Onorato46439ce2010-11-19 13:56:21 -08002631 return this;
2632 }
2633
Joe Onoratocb109a02011-01-18 17:57:41 -08002634 /**
Griff Hazendfcb0802014-02-11 12:00:00 -08002635 * Set whether or not this notification should not bridge to other devices.
2636 *
2637 * <p>Some notifications can be bridged to other devices for remote display.
2638 * This hint can be set to recommend this notification not be bridged.
2639 */
2640 public Builder setLocalOnly(boolean localOnly) {
2641 setFlag(FLAG_LOCAL_ONLY, localOnly);
2642 return this;
2643 }
2644
2645 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002646 * Set which notification properties will be inherited from system defaults.
Joe Onoratocb109a02011-01-18 17:57:41 -08002647 * <p>
2648 * The value should be one or more of the following fields combined with
2649 * bitwise-or:
2650 * {@link #DEFAULT_SOUND}, {@link #DEFAULT_VIBRATE}, {@link #DEFAULT_LIGHTS}.
2651 * <p>
2652 * For all default values, use {@link #DEFAULT_ALL}.
2653 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002654 public Builder setDefaults(int defaults) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002655 mN.defaults = defaults;
Joe Onorato46439ce2010-11-19 13:56:21 -08002656 return this;
2657 }
2658
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002659 /**
2660 * Set the priority of this notification.
2661 *
2662 * @see Notification#priority
2663 */
Tor Norbyed9273d62013-05-30 15:59:53 -07002664 public Builder setPriority(@Priority int pri) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002665 mN.priority = pri;
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002666 return this;
2667 }
Joe Malin8d40d042012-11-05 11:36:40 -08002668
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002669 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -04002670 * Set the notification category.
Joe Malin8d40d042012-11-05 11:36:40 -08002671 *
John Spurlockfd7f1e02014-03-18 16:41:57 -04002672 * @see Notification#category
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002673 */
John Spurlockfd7f1e02014-03-18 16:41:57 -04002674 public Builder setCategory(String category) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002675 mN.category = category;
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002676 return this;
2677 }
2678
2679 /**
Chris Wrendde75302014-03-26 17:24:15 -04002680 * Add a person that is relevant to this notification.
2681 *
Chris Wrene6c48932014-09-29 17:19:27 -04002682 * <P>
2683 * Depending on user preferences, this annotation may allow the notification to pass
2684 * through interruption filters, and to appear more prominently in the user interface.
2685 * </P>
2686 *
2687 * <P>
2688 * The person should be specified by the {@code String} representation of a
2689 * {@link android.provider.ContactsContract.Contacts#CONTENT_LOOKUP_URI}.
2690 * </P>
2691 *
2692 * <P>The system will also attempt to resolve {@code mailto:} and {@code tel:} schema
2693 * URIs. The path part of these URIs must exist in the contacts database, in the
2694 * appropriate column, or the reference will be discarded as invalid. Telephone schema
2695 * URIs will be resolved by {@link android.provider.ContactsContract.PhoneLookup}.
2696 * </P>
2697 *
2698 * @param uri A URI for the person.
Chris Wrendde75302014-03-26 17:24:15 -04002699 * @see Notification#EXTRA_PEOPLE
2700 */
Chris Wrene6c48932014-09-29 17:19:27 -04002701 public Builder addPerson(String uri) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002702 mPersonList.add(uri);
Chris Wrendde75302014-03-26 17:24:15 -04002703 return this;
2704 }
2705
2706 /**
Griff Hazen5cadc3b2014-05-20 09:55:39 -07002707 * Set this notification to be part of a group of notifications sharing the same key.
2708 * Grouped notifications may display in a cluster or stack on devices which
2709 * support such rendering.
2710 *
2711 * <p>To make this notification the summary for its group, also call
2712 * {@link #setGroupSummary}. A sort order can be specified for group members by using
2713 * {@link #setSortKey}.
2714 * @param groupKey The group key of the group.
2715 * @return this object for method chaining
2716 */
2717 public Builder setGroup(String groupKey) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002718 mN.mGroupKey = groupKey;
Griff Hazen5cadc3b2014-05-20 09:55:39 -07002719 return this;
2720 }
2721
2722 /**
2723 * Set this notification to be the group summary for a group of notifications.
2724 * Grouped notifications may display in a cluster or stack on devices which
2725 * support such rendering. Requires a group key also be set using {@link #setGroup}.
2726 * @param isGroupSummary Whether this notification should be a group summary.
2727 * @return this object for method chaining
2728 */
2729 public Builder setGroupSummary(boolean isGroupSummary) {
2730 setFlag(FLAG_GROUP_SUMMARY, isGroupSummary);
2731 return this;
2732 }
2733
2734 /**
2735 * Set a sort key that orders this notification among other notifications from the
2736 * same package. This can be useful if an external sort was already applied and an app
2737 * would like to preserve this. Notifications will be sorted lexicographically using this
2738 * value, although providing different priorities in addition to providing sort key may
2739 * cause this value to be ignored.
2740 *
2741 * <p>This sort key can also be used to order members of a notification group. See
Griff Hazen9e1379f2014-05-20 12:50:51 -07002742 * {@link #setGroup}.
Griff Hazen5cadc3b2014-05-20 09:55:39 -07002743 *
2744 * @see String#compareTo(String)
2745 */
2746 public Builder setSortKey(String sortKey) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002747 mN.mSortKey = sortKey;
Griff Hazen5cadc3b2014-05-20 09:55:39 -07002748 return this;
2749 }
2750
2751 /**
Griff Hazen720042b2014-02-24 15:46:56 -08002752 * Merge additional metadata into this notification.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002753 *
Griff Hazen720042b2014-02-24 15:46:56 -08002754 * <p>Values within the Bundle will replace existing extras values in this Builder.
2755 *
2756 * @see Notification#extras
2757 */
Griff Hazen959591e2014-05-15 22:26:18 -07002758 public Builder addExtras(Bundle extras) {
2759 if (extras != null) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002760 mUserExtras.putAll(extras);
Griff Hazen720042b2014-02-24 15:46:56 -08002761 }
2762 return this;
2763 }
2764
2765 /**
2766 * Set metadata for this notification.
2767 *
2768 * <p>A reference to the Bundle is held for the lifetime of this Builder, and the Bundle's
Chris Wrenfbd96ba2012-05-01 12:03:58 -04002769 * current contents are copied into the Notification each time {@link #build()} is
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002770 * called.
2771 *
Griff Hazen720042b2014-02-24 15:46:56 -08002772 * <p>Replaces any existing extras values with those from the provided Bundle.
2773 * Use {@link #addExtras} to merge in metadata instead.
2774 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002775 * @see Notification#extras
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002776 */
Griff Hazen959591e2014-05-15 22:26:18 -07002777 public Builder setExtras(Bundle extras) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002778 if (extras != null) {
2779 mUserExtras = extras;
2780 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002781 return this;
2782 }
2783
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002784 /**
Griff Hazen720042b2014-02-24 15:46:56 -08002785 * Get the current metadata Bundle used by this notification Builder.
2786 *
2787 * <p>The returned Bundle is shared with this Builder.
2788 *
2789 * <p>The current contents of this Bundle are copied into the Notification each time
2790 * {@link #build()} is called.
2791 *
2792 * @see Notification#extras
2793 */
2794 public Bundle getExtras() {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002795 return mUserExtras;
2796 }
2797
2798 private Bundle getAllExtras() {
2799 final Bundle saveExtras = (Bundle) mUserExtras.clone();
2800 saveExtras.putAll(mN.extras);
2801 return saveExtras;
Griff Hazen720042b2014-02-24 15:46:56 -08002802 }
2803
2804 /**
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002805 * Add an action to this notification. Actions are typically displayed by
2806 * the system as a button adjacent to the notification content.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04002807 * <p>
2808 * Every action must have an icon (32dp square and matching the
2809 * <a href="{@docRoot}design/style/iconography.html#action-bar">Holo
2810 * Dark action bar</a> visual style), a textual label, and a {@link PendingIntent}.
2811 * <p>
2812 * A notification in its expanded form can display up to 3 actions, from left to right in
2813 * the order they were added. Actions will not be displayed when the notification is
2814 * collapsed, however, so be sure that any essential functions may be accessed by the user
2815 * in some other way (for example, in the Activity pointed to by {@link #contentIntent}).
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002816 *
2817 * @param icon Resource ID of a drawable that represents the action.
2818 * @param title Text describing the action.
2819 * @param intent PendingIntent to be fired when the action is invoked.
Dan Sandler86647982015-05-13 23:41:13 -04002820 *
2821 * @deprecated Use {@link #addAction(Action)} instead.
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002822 */
Dan Sandler86647982015-05-13 23:41:13 -04002823 @Deprecated
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002824 public Builder addAction(int icon, CharSequence title, PendingIntent intent) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04002825 mActions.add(new Action(icon, safeCharSequence(title), intent));
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002826 return this;
2827 }
2828
Chris Wrenfbd96ba2012-05-01 12:03:58 -04002829 /**
Griff Hazen959591e2014-05-15 22:26:18 -07002830 * Add an action to this notification. Actions are typically displayed by
2831 * the system as a button adjacent to the notification content.
2832 * <p>
2833 * Every action must have an icon (32dp square and matching the
2834 * <a href="{@docRoot}design/style/iconography.html#action-bar">Holo
2835 * Dark action bar</a> visual style), a textual label, and a {@link PendingIntent}.
2836 * <p>
2837 * A notification in its expanded form can display up to 3 actions, from left to right in
2838 * the order they were added. Actions will not be displayed when the notification is
2839 * collapsed, however, so be sure that any essential functions may be accessed by the user
2840 * in some other way (for example, in the Activity pointed to by {@link #contentIntent}).
2841 *
2842 * @param action The action to add.
2843 */
2844 public Builder addAction(Action action) {
2845 mActions.add(action);
2846 return this;
2847 }
2848
2849 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002850 * Alter the complete list of actions attached to this notification.
2851 * @see #addAction(Action).
2852 *
2853 * @param actions
2854 * @return
2855 */
2856 public Builder setActions(Action... actions) {
2857 mActions.clear();
2858 for (int i = 0; i < actions.length; i++) {
2859 mActions.add(actions[i]);
2860 }
2861 return this;
2862 }
2863
2864 /**
Chris Wrenfbd96ba2012-05-01 12:03:58 -04002865 * Add a rich notification style to be applied at build time.
2866 *
2867 * @param style Object responsible for modifying the notification style.
2868 */
2869 public Builder setStyle(Style style) {
2870 if (mStyle != style) {
2871 mStyle = style;
Daniel Sandlerc08dea22012-06-28 08:35:24 -07002872 if (mStyle != null) {
2873 mStyle.setBuilder(this);
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002874 mN.extras.putString(EXTRA_TEMPLATE, style.getClass().getName());
2875 } else {
2876 mN.extras.remove(EXTRA_TEMPLATE);
Daniel Sandlerc08dea22012-06-28 08:35:24 -07002877 }
Chris Wrenfbd96ba2012-05-01 12:03:58 -04002878 }
2879 return this;
2880 }
2881
Dan Sandler0bf2ed82013-12-21 23:33:41 -06002882 /**
2883 * Specify the value of {@link #visibility}.
Griff Hazenb720abe2014-05-20 13:15:30 -07002884 *
Dan Sandler0bf2ed82013-12-21 23:33:41 -06002885 * @param visibility One of {@link #VISIBILITY_PRIVATE} (the default),
2886 * {@link #VISIBILITY_SECRET}, or {@link #VISIBILITY_PUBLIC}.
2887 *
2888 * @return The same Builder.
2889 */
2890 public Builder setVisibility(int visibility) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002891 mN.visibility = visibility;
Dan Sandler0bf2ed82013-12-21 23:33:41 -06002892 return this;
2893 }
2894
2895 /**
2896 * Supply a replacement Notification whose contents should be shown in insecure contexts
2897 * (i.e. atop the secure lockscreen). See {@link #visibility} and {@link #VISIBILITY_PUBLIC}.
2898 * @param n A replacement notification, presumably with some or all info redacted.
2899 * @return The same Builder.
2900 */
2901 public Builder setPublicVersion(Notification n) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002902 if (n != null) {
2903 mN.publicVersion = new Notification();
2904 n.cloneInto(mN.publicVersion, /*heavy=*/ true);
2905 } else {
2906 mN.publicVersion = null;
2907 }
Dan Sandler0bf2ed82013-12-21 23:33:41 -06002908 return this;
2909 }
2910
Griff Hazenb720abe2014-05-20 13:15:30 -07002911 /**
Griff Hazen5cadc3b2014-05-20 09:55:39 -07002912 * Apply an extender to this notification builder. Extenders may be used to add
2913 * metadata or change options on this builder.
2914 */
Griff Hazen61a9e862014-05-22 16:05:19 -07002915 public Builder extend(Extender extender) {
2916 extender.extend(this);
Griff Hazen5cadc3b2014-05-20 09:55:39 -07002917 return this;
2918 }
2919
Dan Sandler4e787062015-06-17 15:09:48 -04002920 /**
2921 * @hide
2922 */
2923 public void setFlag(int mask, boolean value) {
Joe Onorato46439ce2010-11-19 13:56:21 -08002924 if (value) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002925 mN.flags |= mask;
Joe Onorato46439ce2010-11-19 13:56:21 -08002926 } else {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002927 mN.flags &= ~mask;
Joe Onorato46439ce2010-11-19 13:56:21 -08002928 }
2929 }
2930
Dan Sandler26e81cf2014-05-06 10:01:27 -04002931 /**
2932 * Sets {@link Notification#color}.
2933 *
2934 * @param argb The accent color to use
2935 *
2936 * @return The same Builder.
2937 */
Tor Norbye80756e32015-03-02 09:39:27 -08002938 public Builder setColor(@ColorInt int argb) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002939 mN.color = argb;
Julia Reynolds10ee1fc2015-11-09 11:04:55 -05002940 sanitizeColor();
Dan Sandler26e81cf2014-05-06 10:01:27 -04002941 return this;
2942 }
2943
Jorim Jaggid05aa3e2014-08-28 17:52:27 +02002944 private Drawable getProfileBadgeDrawable() {
Christoph Studer7ac80e62014-08-04 16:01:57 +02002945 // Note: This assumes that the current user can read the profile badge of the
2946 // originating user.
Selim Cineke6ff9462016-01-15 15:07:06 -08002947 return mContext.getPackageManager().getUserBadgeForDensityNoBackground(
Julia Reynoldsda303542015-11-23 14:00:20 -05002948 new UserHandle(mContext.getUserId()), 0);
Jorim Jaggid05aa3e2014-08-28 17:52:27 +02002949 }
2950
2951 private Bitmap getProfileBadge() {
2952 Drawable badge = getProfileBadgeDrawable();
Kenny Guy8a0101b2014-05-08 23:34:12 +01002953 if (badge == null) {
2954 return null;
2955 }
Jorim Jaggid05aa3e2014-08-28 17:52:27 +02002956 final int size = mContext.getResources().getDimensionPixelSize(
2957 R.dimen.notification_badge_size);
2958 Bitmap bitmap = Bitmap.createBitmap(size, size, Bitmap.Config.ARGB_8888);
Kenny Guy8a0101b2014-05-08 23:34:12 +01002959 Canvas canvas = new Canvas(bitmap);
Jorim Jaggid05aa3e2014-08-28 17:52:27 +02002960 badge.setBounds(0, 0, size, size);
Kenny Guy8a0101b2014-05-08 23:34:12 +01002961 badge.draw(canvas);
2962 return bitmap;
2963 }
2964
Selim Cinekc848c3a2016-01-13 15:27:30 -08002965 private void bindProfileBadge(RemoteViews contentView) {
Kenny Guy98193ea2014-07-24 19:54:37 +01002966 Bitmap profileBadge = getProfileBadge();
2967
Kenny Guy98193ea2014-07-24 19:54:37 +01002968 if (profileBadge != null) {
Selim Cinekc848c3a2016-01-13 15:27:30 -08002969 contentView.setImageViewBitmap(R.id.profile_badge, profileBadge);
2970 contentView.setViewVisibility(R.id.profile_badge, View.VISIBLE);
Kenny Guy98193ea2014-07-24 19:54:37 +01002971 }
Kenny Guy98193ea2014-07-24 19:54:37 +01002972 }
2973
Christoph Studerfe718432014-09-01 18:21:18 +02002974 private void resetStandardTemplate(RemoteViews contentView) {
Selim Cinekeaa29ca2015-11-23 13:51:13 -08002975 resetNotificationHeader(contentView);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07002976 resetContentMargins(contentView);
Christoph Studerfe718432014-09-01 18:21:18 +02002977 contentView.setViewVisibility(R.id.right_icon, View.GONE);
Selim Cinek860b6da2015-12-16 19:02:19 -08002978 contentView.setViewVisibility(R.id.title, View.GONE);
Christoph Studerfe718432014-09-01 18:21:18 +02002979 contentView.setTextViewText(R.id.title, null);
Selim Cinek41598732016-01-11 16:58:37 -08002980 contentView.setViewVisibility(R.id.text, View.GONE);
Christoph Studerfe718432014-09-01 18:21:18 +02002981 contentView.setTextViewText(R.id.text, null);
Selim Cinek29603462015-11-17 19:04:39 -08002982 contentView.setViewVisibility(R.id.text_line_1, View.GONE);
Selim Cinek41598732016-01-11 16:58:37 -08002983 contentView.setTextViewText(R.id.text_line_1, null);
Christoph Studerfe718432014-09-01 18:21:18 +02002984 contentView.setViewVisibility(R.id.progress, View.GONE);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07002985 }
2986
Selim Cinekeaa29ca2015-11-23 13:51:13 -08002987 /**
2988 * Resets the notification header to its original state
2989 */
2990 private void resetNotificationHeader(RemoteViews contentView) {
Selim Cinek65b2e7c2015-10-26 14:11:31 -07002991 contentView.setImageViewResource(R.id.icon, 0);
Selim Cinek7b836392015-12-04 20:02:59 -08002992 contentView.setBoolean(R.id.notification_header, "setExpanded", false);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07002993 contentView.setTextViewText(R.id.app_name_text, null);
Christoph Studerca1db712014-09-10 17:31:33 +02002994 contentView.setViewVisibility(R.id.chronometer, View.GONE);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07002995 contentView.setViewVisibility(R.id.header_sub_text, View.GONE);
Selim Cinek29603462015-11-17 19:04:39 -08002996 contentView.setViewVisibility(R.id.header_content_info, View.GONE);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07002997 contentView.setViewVisibility(R.id.number_of_children, View.GONE);
Selim Cinek29603462015-11-17 19:04:39 -08002998 contentView.setViewVisibility(R.id.sub_text_divider, View.GONE);
2999 contentView.setViewVisibility(R.id.content_info_divider, View.GONE);
3000 contentView.setViewVisibility(R.id.time_divider, View.GONE);
Selim Cinekc848c3a2016-01-13 15:27:30 -08003001 contentView.setImageViewIcon(R.id.profile_badge, null);
3002 contentView.setViewVisibility(R.id.profile_badge, View.GONE);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003003 }
3004
3005 private void resetContentMargins(RemoteViews contentView) {
3006 contentView.setViewLayoutMarginEnd(R.id.line1, 0);
Selim Cinekc848c3a2016-01-13 15:27:30 -08003007 contentView.setViewLayoutMarginEnd(R.id.text, 0);
Christoph Studerfe718432014-09-01 18:21:18 +02003008 }
3009
Jorim Jaggi445d3c02014-08-19 22:33:42 +02003010 private RemoteViews applyStandardTemplate(int resId) {
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02003011 return applyStandardTemplate(resId, true /* hasProgress */);
3012 }
3013
3014 /**
3015 * @param hasProgress whether the progress bar should be shown and set
3016 */
3017 private RemoteViews applyStandardTemplate(int resId, boolean hasProgress) {
Kenny Guy77320062014-08-27 21:37:15 +01003018 RemoteViews contentView = new BuilderRemoteViews(mContext.getApplicationInfo(), resId);
Dan Sandler539aad42014-08-04 00:43:39 -04003019
Christoph Studerfe718432014-09-01 18:21:18 +02003020 resetStandardTemplate(contentView);
3021
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003022 final Bundle ex = mN.extras;
Dan Sandler190d58d2014-05-15 09:33:39 -04003023
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003024 bindNotificationHeader(contentView);
3025 bindLargeIcon(contentView);
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003026 if (ex.getCharSequence(EXTRA_TITLE) != null) {
Selim Cinek860b6da2015-12-16 19:02:19 -08003027 contentView.setViewVisibility(R.id.title, View.VISIBLE);
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003028 contentView.setTextViewText(R.id.title,
3029 processLegacyText(ex.getCharSequence(EXTRA_TITLE)));
Joe Onorato561d3852010-11-20 18:09:34 -08003030 }
Selim Cinek29603462015-11-17 19:04:39 -08003031 boolean showProgress = handleProgressBar(hasProgress, contentView, ex);
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003032 if (ex.getCharSequence(EXTRA_TEXT) != null) {
Selim Cinek41598732016-01-11 16:58:37 -08003033 int textId = showProgress ? com.android.internal.R.id.text_line_1
3034 : com.android.internal.R.id.text;
3035 contentView.setTextViewText(textId, processLegacyText(
3036 ex.getCharSequence(EXTRA_TEXT)));
3037 contentView.setViewVisibility(textId, View.VISIBLE);
Joe Onorato561d3852010-11-20 18:09:34 -08003038 }
Selim Cinekc848c3a2016-01-13 15:27:30 -08003039
Selim Cinek860b6da2015-12-16 19:02:19 -08003040 setContentMinHeight(contentView, showProgress || mN.mLargeIcon != null);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003041
Selim Cinek29603462015-11-17 19:04:39 -08003042 return contentView;
3043 }
3044
Selim Cinek860b6da2015-12-16 19:02:19 -08003045 /**
3046 * @param remoteView the remote view to update the minheight in
3047 * @param hasMinHeight does it have a mimHeight
3048 * @hide
3049 */
3050 void setContentMinHeight(RemoteViews remoteView, boolean hasMinHeight) {
3051 int minHeight = 0;
3052 if (hasMinHeight) {
3053 // we need to set the minHeight of the notification
3054 minHeight = mContext.getResources().getDimensionPixelSize(
3055 com.android.internal.R.dimen.notification_min_content_height);
3056 }
3057 remoteView.setInt(R.id.notification_main_column, "setMinimumHeight", minHeight);
3058 }
3059
Selim Cinek29603462015-11-17 19:04:39 -08003060 private boolean handleProgressBar(boolean hasProgress, RemoteViews contentView, Bundle ex) {
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003061 final int max = ex.getInt(EXTRA_PROGRESS_MAX, 0);
3062 final int progress = ex.getInt(EXTRA_PROGRESS, 0);
3063 final boolean ind = ex.getBoolean(EXTRA_PROGRESS_INDETERMINATE);
3064 if (hasProgress && (max != 0 || ind)) {
Selim Cinek29603462015-11-17 19:04:39 -08003065 contentView.setViewVisibility(com.android.internal.R.id.progress, View.VISIBLE);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003066 contentView.setProgressBar(
Selim Cinek29603462015-11-17 19:04:39 -08003067 R.id.progress, max, progress, ind);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003068 contentView.setProgressBackgroundTintList(
3069 R.id.progress, ColorStateList.valueOf(mContext.getColor(
3070 R.color.notification_progress_background_color)));
Selim Cinek29603462015-11-17 19:04:39 -08003071 if (mN.color != COLOR_DEFAULT) {
3072 ColorStateList colorStateList = ColorStateList.valueOf(mN.color);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003073 contentView.setProgressTintList(R.id.progress, colorStateList);
3074 contentView.setProgressIndeterminateTintList(R.id.progress, colorStateList);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003075 }
Selim Cinek29603462015-11-17 19:04:39 -08003076 return true;
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003077 } else {
3078 contentView.setViewVisibility(R.id.progress, View.GONE);
Selim Cinek29603462015-11-17 19:04:39 -08003079 return false;
Jeff Sharkey1c400132011-08-05 14:50:13 -07003080 }
Joe Onorato561d3852010-11-20 18:09:34 -08003081 }
3082
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003083 private void bindLargeIcon(RemoteViews contentView) {
3084 if (mN.mLargeIcon != null) {
3085 contentView.setViewVisibility(R.id.right_icon, View.VISIBLE);
3086 contentView.setImageViewIcon(R.id.right_icon, mN.mLargeIcon);
3087 processLargeLegacyIcon(mN.mLargeIcon, contentView);
3088 int endMargin = mContext.getResources().getDimensionPixelSize(
3089 R.dimen.notification_content_picture_margin);
3090 contentView.setViewLayoutMarginEnd(R.id.line1, endMargin);
Selim Cinekc848c3a2016-01-13 15:27:30 -08003091 contentView.setViewLayoutMarginEnd(R.id.text, endMargin);
Selim Cinek29603462015-11-17 19:04:39 -08003092 contentView.setViewLayoutMarginEnd(R.id.progress, endMargin);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003093 }
3094 }
3095
3096 private void bindNotificationHeader(RemoteViews contentView) {
3097 bindSmallIcon(contentView);
Selim Cinekeaa29ca2015-11-23 13:51:13 -08003098 bindChildCountColor(contentView);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003099 bindHeaderAppName(contentView);
Selim Cinek03d0d652015-11-13 13:18:09 -05003100 bindHeaderSubText(contentView);
Selim Cinek29603462015-11-17 19:04:39 -08003101 bindContentInfo(contentView);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003102 bindHeaderChronometerAndTime(contentView);
3103 bindExpandButton(contentView);
Selim Cinekc848c3a2016-01-13 15:27:30 -08003104 bindProfileBadge(contentView);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003105 }
3106
Selim Cinekeaa29ca2015-11-23 13:51:13 -08003107 private void bindChildCountColor(RemoteViews contentView) {
3108 contentView.setTextColor(R.id.number_of_children, resolveColor());
3109 }
3110
Selim Cinek29603462015-11-17 19:04:39 -08003111 private void bindContentInfo(RemoteViews contentView) {
3112 boolean visible = false;
3113 if (mN.extras.getCharSequence(EXTRA_INFO_TEXT) != null) {
3114 contentView.setTextViewText(R.id.header_content_info,
3115 processLegacyText(mN.extras.getCharSequence(EXTRA_INFO_TEXT)));
3116 contentView.setViewVisibility(R.id.header_content_info, View.VISIBLE);
3117 visible = true;
3118 } else if (mN.number > 0) {
3119 final int tooBig = mContext.getResources().getInteger(
3120 R.integer.status_bar_notification_info_maxnum);
3121 if (mN.number > tooBig) {
3122 contentView.setTextViewText(R.id.header_content_info, processLegacyText(
3123 mContext.getResources().getString(
3124 R.string.status_bar_notification_info_overflow)));
3125 } else {
3126 contentView.setTextViewText(R.id.header_content_info,
3127 processLegacyText(String.valueOf(mN.number)));
3128 }
3129 contentView.setViewVisibility(R.id.header_content_info, View.VISIBLE);
3130 visible = true;
3131 }
3132 if (visible) {
3133 contentView.setViewVisibility(R.id.content_info_divider, View.VISIBLE);
3134 }
3135 }
3136
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003137 private void bindExpandButton(RemoteViews contentView) {
3138 contentView.setDrawableParameters(R.id.expand_button, false, -1, resolveColor(),
3139 PorterDuff.Mode.SRC_ATOP, -1);
Selim Cinekea4bef72015-12-02 15:51:10 -08003140 contentView.setInt(R.id.notification_header, "setOriginalNotificationColor",
3141 resolveColor());
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003142 }
3143
3144 private void bindHeaderChronometerAndTime(RemoteViews contentView) {
3145 if (showsTimeOrChronometer()) {
Selim Cinek29603462015-11-17 19:04:39 -08003146 contentView.setViewVisibility(R.id.time_divider, View.VISIBLE);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003147 if (mN.extras.getBoolean(EXTRA_SHOW_CHRONOMETER)) {
3148 contentView.setViewVisibility(R.id.chronometer, View.VISIBLE);
3149 contentView.setLong(R.id.chronometer, "setBase",
3150 mN.when + (SystemClock.elapsedRealtime() - System.currentTimeMillis()));
3151 contentView.setBoolean(R.id.chronometer, "setStarted", true);
Selim Cinek81c23aa2016-02-25 16:23:13 -08003152 boolean countsDown = mN.extras.getBoolean(EXTRA_CHRONOMETER_COUNTS_DOWN);
3153 contentView.setChronometerCountsDown(R.id.chronometer, countsDown);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003154 } else {
3155 contentView.setViewVisibility(R.id.time, View.VISIBLE);
3156 contentView.setLong(R.id.time, "setTime", mN.when);
3157 }
3158 }
3159 }
3160
Selim Cinek03d0d652015-11-13 13:18:09 -05003161 private void bindHeaderSubText(RemoteViews contentView) {
3162 CharSequence subText = mN.extras.getCharSequence(EXTRA_SUB_TEXT);
3163 if (subText == null && mStyle != null && mStyle.mSummaryTextSet
3164 && mStyle.hasSummaryInHeader()) {
3165 subText = mStyle.mSummaryText;
3166 }
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003167 if (subText != null) {
3168 // TODO: Remove the span entirely to only have the string with propper formating.
3169 contentView.setTextViewText(R.id.header_sub_text, processLegacyText(subText));
3170 contentView.setViewVisibility(R.id.header_sub_text, View.VISIBLE);
Selim Cinek29603462015-11-17 19:04:39 -08003171 contentView.setViewVisibility(R.id.sub_text_divider, View.VISIBLE);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003172 }
3173 }
3174
3175 private void bindHeaderAppName(RemoteViews contentView) {
3176 PackageManager packageManager = mContext.getPackageManager();
3177 ApplicationInfo info = null;
3178 try {
3179 info = packageManager.getApplicationInfo(mContext.getApplicationInfo().packageName,
3180 0);
3181 } catch (final NameNotFoundException e) {
3182 return;
3183 }
3184 CharSequence appName = info != null ? packageManager.getApplicationLabel(info)
3185 : null;
3186 if (TextUtils.isEmpty(appName)) {
3187 return;
3188 }
3189 contentView.setTextViewText(R.id.app_name_text, appName);
Selim Cinekaef485a2016-02-05 10:31:20 -08003190 contentView.setTextColor(R.id.app_name_text, resolveColor());
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003191 }
3192
3193 private void bindSmallIcon(RemoteViews contentView) {
3194 contentView.setImageViewIcon(R.id.icon, mN.mSmallIcon);
3195 processSmallIconColor(mN.mSmallIcon, contentView);
3196 }
3197
Jorim Jaggi445d3c02014-08-19 22:33:42 +02003198 /**
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02003199 * @return true if the built notification will show the time or the chronometer; false
3200 * otherwise
3201 */
3202 private boolean showsTimeOrChronometer() {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003203 return mN.when != 0 && mN.extras.getBoolean(EXTRA_SHOW_WHEN);
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02003204 }
3205
Christoph Studerfe718432014-09-01 18:21:18 +02003206 private void resetStandardTemplateWithActions(RemoteViews big) {
Adrian Roos7052de52016-03-03 15:53:34 -08003207 big.setViewVisibility(R.id.actions_container, View.GONE);
Christoph Studerfe718432014-09-01 18:21:18 +02003208 big.setViewVisibility(R.id.actions, View.GONE);
Christoph Studerfe718432014-09-01 18:21:18 +02003209 big.removeAllViews(R.id.actions);
Adrian Roose458aa82015-12-08 16:17:19 -08003210
3211 big.setViewVisibility(R.id.notification_material_reply_container, View.GONE);
3212 big.setTextViewText(R.id.notification_material_reply_text_1, null);
3213
3214 big.setViewVisibility(R.id.notification_material_reply_text_2, View.GONE);
3215 big.setTextViewText(R.id.notification_material_reply_text_2, null);
3216 big.setViewVisibility(R.id.notification_material_reply_text_3, View.GONE);
3217 big.setTextViewText(R.id.notification_material_reply_text_3, null);
Christoph Studerfe718432014-09-01 18:21:18 +02003218 }
3219
Daniel Sandler96fd7c12012-03-30 16:37:36 -04003220 private RemoteViews applyStandardTemplateWithActions(int layoutId) {
Jorim Jaggi445d3c02014-08-19 22:33:42 +02003221 RemoteViews big = applyStandardTemplate(layoutId);
Daniel Sandler96fd7c12012-03-30 16:37:36 -04003222
Christoph Studerfe718432014-09-01 18:21:18 +02003223 resetStandardTemplateWithActions(big);
3224
Adrian Roose458aa82015-12-08 16:17:19 -08003225 boolean validRemoteInput = false;
3226
Daniel Sandler96fd7c12012-03-30 16:37:36 -04003227 int N = mActions.size();
3228 if (N > 0) {
Adrian Roos7052de52016-03-03 15:53:34 -08003229 big.setViewVisibility(R.id.actions_container, View.VISIBLE);
Daniel Sandler96fd7c12012-03-30 16:37:36 -04003230 big.setViewVisibility(R.id.actions, View.VISIBLE);
Daniel Sandler8680bf82012-05-15 16:52:52 -04003231 if (N>MAX_ACTION_BUTTONS) N=MAX_ACTION_BUTTONS;
Daniel Sandler96fd7c12012-03-30 16:37:36 -04003232 for (int i=0; i<N; i++) {
Adrian Roose458aa82015-12-08 16:17:19 -08003233 Action action = mActions.get(i);
3234 validRemoteInput |= hasValidRemoteInput(action);
3235
3236 final RemoteViews button = generateActionButton(action);
Adrian Roos9b123cf2016-02-04 14:55:57 -08003237 if (i == N - 1) {
3238 button.setViewLayoutWidth(com.android.internal.R.id.action0,
3239 ViewGroup.LayoutParams.MATCH_PARENT);
3240 }
Daniel Sandler96fd7c12012-03-30 16:37:36 -04003241 big.addView(R.id.actions, button);
3242 }
3243 }
Adrian Roose458aa82015-12-08 16:17:19 -08003244
3245 CharSequence[] replyText = mN.extras.getCharSequenceArray(EXTRA_REMOTE_INPUT_HISTORY);
3246 if (validRemoteInput && replyText != null
3247 && replyText.length > 0 && !TextUtils.isEmpty(replyText[0])) {
3248 big.setViewVisibility(R.id.notification_material_reply_container, View.VISIBLE);
3249 big.setTextViewText(R.id.notification_material_reply_text_1, replyText[0]);
3250
3251 if (replyText.length > 1 && !TextUtils.isEmpty(replyText[1])) {
3252 big.setViewVisibility(R.id.notification_material_reply_text_2, View.VISIBLE);
3253 big.setTextViewText(R.id.notification_material_reply_text_2, replyText[1]);
3254
3255 if (replyText.length > 2 && !TextUtils.isEmpty(replyText[2])) {
3256 big.setViewVisibility(
3257 R.id.notification_material_reply_text_3, View.VISIBLE);
3258 big.setTextViewText(R.id.notification_material_reply_text_3, replyText[2]);
3259 }
3260 }
3261 }
3262
Daniel Sandler96fd7c12012-03-30 16:37:36 -04003263 return big;
3264 }
3265
Adrian Roose458aa82015-12-08 16:17:19 -08003266 private boolean hasValidRemoteInput(Action action) {
3267 if (TextUtils.isEmpty(action.title) || action.actionIntent == null) {
3268 // Weird actions
3269 return false;
3270 }
3271
3272 RemoteInput[] remoteInputs = action.getRemoteInputs();
3273 if (remoteInputs == null) {
3274 return false;
3275 }
3276
3277 for (RemoteInput r : remoteInputs) {
3278 CharSequence[] choices = r.getChoices();
3279 if (r.getAllowFreeFormInput() || (choices != null && choices.length != 0)) {
3280 return true;
3281 }
3282 }
3283 return false;
3284 }
3285
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003286 /**
3287 * Construct a RemoteViews for the final 1U notification layout. In order:
3288 * 1. Custom contentView from the caller
3289 * 2. Style's proposed content view
3290 * 3. Standard template view
3291 */
Julia Reynolds3b848122016-02-26 10:45:32 -05003292 public RemoteViews createContentView() {
Selim Cinek593610c2016-02-16 18:42:57 -08003293 if (mN.contentView != null && (mStyle == null || !mStyle.displayCustomViewInline())) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003294 return mN.contentView;
3295 } else if (mStyle != null) {
3296 final RemoteViews styleView = mStyle.makeContentView();
3297 if (styleView != null) {
3298 return styleView;
3299 }
Joe Onorato46439ce2010-11-19 13:56:21 -08003300 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003301 return applyStandardTemplate(getBaseLayoutResource());
Joe Onorato46439ce2010-11-19 13:56:21 -08003302 }
3303
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003304 /**
3305 * Construct a RemoteViews for the final big notification layout.
3306 */
Julia Reynolds3b848122016-02-26 10:45:32 -05003307 public RemoteViews createBigContentView() {
Selim Cinek850a8542015-11-11 11:48:36 -05003308 RemoteViews result = null;
Selim Cinek593610c2016-02-16 18:42:57 -08003309 if (mN.bigContentView != null
3310 && (mStyle == null || !mStyle.displayCustomViewInline())) {
Julia Reynolds089e3e42015-11-18 09:59:57 -05003311 return mN.bigContentView;
3312 } else if (mStyle != null) {
Selim Cinek850a8542015-11-11 11:48:36 -05003313 result = mStyle.makeBigContentView();
Selim Cinek90dcf6d2015-11-18 20:24:13 -08003314 hideLine1Text(result);
Selim Cinekcc10bfb2016-02-10 16:24:21 -08003315 } else if (mActions.size() != 0) {
3316 result = applyStandardTemplateWithActions(getBigBaseLayoutResource());
Selim Cinek850a8542015-11-11 11:48:36 -05003317 }
3318 adaptNotificationHeaderForBigContentView(result);
3319 return result;
3320 }
Daniel Sandler96fd7c12012-03-30 16:37:36 -04003321
Selim Cinekeaa29ca2015-11-23 13:51:13 -08003322 /**
3323 * Construct a RemoteViews for the final notification header only
3324 *
3325 * @hide
3326 */
3327 public RemoteViews makeNotificationHeader() {
3328 RemoteViews header = new BuilderRemoteViews(mContext.getApplicationInfo(),
3329 R.layout.notification_template_header);
3330 resetNotificationHeader(header);
3331 bindNotificationHeader(header);
3332 return header;
3333 }
3334
Selim Cinek29603462015-11-17 19:04:39 -08003335 private void hideLine1Text(RemoteViews result) {
Selim Cinekcc10bfb2016-02-10 16:24:21 -08003336 if (result != null) {
3337 result.setViewVisibility(R.id.text_line_1, View.GONE);
3338 }
Selim Cinek29603462015-11-17 19:04:39 -08003339 }
3340
Selim Cinek850a8542015-11-11 11:48:36 -05003341 private void adaptNotificationHeaderForBigContentView(RemoteViews result) {
Selim Cinekcc10bfb2016-02-10 16:24:21 -08003342 if (result != null) {
3343 result.setBoolean(R.id.notification_header, "setExpanded", true);
3344 }
Daniel Sandler96fd7c12012-03-30 16:37:36 -04003345 }
3346
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003347 /**
3348 * Construct a RemoteViews for the final heads-up notification layout.
3349 */
Julia Reynolds3b848122016-02-26 10:45:32 -05003350 public RemoteViews createHeadsUpContentView() {
Selim Cinek593610c2016-02-16 18:42:57 -08003351 if (mN.headsUpContentView != null
3352 && (mStyle == null || !mStyle.displayCustomViewInline())) {
Julia Reynolds089e3e42015-11-18 09:59:57 -05003353 return mN.headsUpContentView;
3354 } else if (mStyle != null) {
3355 final RemoteViews styleView = mStyle.makeHeadsUpContentView();
3356 if (styleView != null) {
3357 return styleView;
3358 }
3359 } else if (mActions.size() == 0) {
3360 return null;
3361 }
3362
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01003363 return applyStandardTemplateWithActions(getBigBaseLayoutResource());
Chris Wren8fd39ec2014-02-27 17:43:26 -05003364 }
3365
Selim Cinek624c02db2015-12-14 21:00:02 -08003366 /**
3367 * Construct a RemoteViews for the display in public contexts like on the lockscreen.
3368 *
3369 * @hide
3370 */
3371 public RemoteViews makePublicContentView() {
3372 if (mN.publicVersion != null) {
3373 final Builder builder = recoverBuilder(mContext, mN.publicVersion);
Julia Reynolds3b848122016-02-26 10:45:32 -05003374 return builder.createContentView();
Selim Cinek624c02db2015-12-14 21:00:02 -08003375 }
3376 Bundle savedBundle = mN.extras;
3377 Style style = mStyle;
3378 mStyle = null;
3379 Icon largeIcon = mN.mLargeIcon;
3380 mN.mLargeIcon = null;
3381 Bundle publicExtras = new Bundle();
3382 publicExtras.putBoolean(EXTRA_SHOW_WHEN,
3383 savedBundle.getBoolean(EXTRA_SHOW_WHEN));
3384 publicExtras.putBoolean(EXTRA_SHOW_CHRONOMETER,
3385 savedBundle.getBoolean(EXTRA_SHOW_CHRONOMETER));
Selim Cinek81c23aa2016-02-25 16:23:13 -08003386 publicExtras.putBoolean(EXTRA_CHRONOMETER_COUNTS_DOWN,
3387 savedBundle.getBoolean(EXTRA_CHRONOMETER_COUNTS_DOWN));
Selim Cinek624c02db2015-12-14 21:00:02 -08003388 publicExtras.putCharSequence(EXTRA_TITLE,
3389 mContext.getString(R.string.notification_hidden_text));
3390 mN.extras = publicExtras;
3391 final RemoteViews publicView = applyStandardTemplate(getBaseLayoutResource());
3392 mN.extras = savedBundle;
3393 mN.mLargeIcon = largeIcon;
3394 mStyle = style;
3395 return publicView;
3396 }
3397
3398
Chris Wren8fd39ec2014-02-27 17:43:26 -05003399
Daniel Sandler96fd7c12012-03-30 16:37:36 -04003400 private RemoteViews generateActionButton(Action action) {
Daniel Sandler8680bf82012-05-15 16:52:52 -04003401 final boolean tombstone = (action.actionIntent == null);
Selim Cinekf33b1112015-07-15 17:45:11 -07003402 RemoteViews button = new BuilderRemoteViews(mContext.getApplicationInfo(),
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01003403 tombstone ? getActionTombstoneLayoutResource()
3404 : getActionLayoutResource());
Dan Sandler68079d52015-07-22 10:45:30 -04003405 final Icon ai = action.getIcon();
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01003406 button.setTextViewText(R.id.action0, processLegacyText(action.title));
Daniel Sandler8680bf82012-05-15 16:52:52 -04003407 if (!tombstone) {
Daniel Sandlere5518842012-05-10 16:20:40 -04003408 button.setOnClickPendingIntent(R.id.action0, action.actionIntent);
Daniel Sandlere5518842012-05-10 16:20:40 -04003409 }
Daniel Sandler96fd7c12012-03-30 16:37:36 -04003410 button.setContentDescription(R.id.action0, action.title);
Adrian Roosfe84e1f2015-11-04 15:55:39 -08003411 if (action.mRemoteInputs != null) {
3412 button.setRemoteInputs(R.id.action0, action.mRemoteInputs);
3413 }
3414 if (mN.color != COLOR_DEFAULT) {
3415 button.setTextColor(R.id.action0, mN.color);
3416 }
Daniel Sandler96fd7c12012-03-30 16:37:36 -04003417 return button;
3418 }
3419
Joe Onoratocb109a02011-01-18 17:57:41 -08003420 /**
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01003421 * @return Whether we are currently building a notification from a legacy (an app that
Alan Viverette3cb07a462014-06-06 14:19:53 -07003422 * doesn't create material notifications by itself) app.
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01003423 */
3424 private boolean isLegacy() {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003425 return getColorUtil() != null;
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01003426 }
3427
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01003428 private CharSequence processLegacyText(CharSequence charSequence) {
3429 if (isLegacy()) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003430 return getColorUtil().invertCharSequenceColors(charSequence);
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01003431 } else {
3432 return charSequence;
3433 }
3434 }
3435
Dan Sandler26e81cf2014-05-06 10:01:27 -04003436 /**
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003437 * Apply any necessariy colors to the small icon
Dan Sandler26e81cf2014-05-06 10:01:27 -04003438 */
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003439 private void processSmallIconColor(Icon smallIcon, RemoteViews contentView) {
Selim Cinekea4bef72015-12-02 15:51:10 -08003440 boolean colorable = !isLegacy() || getColorUtil().isGrayscaleIcon(mContext, smallIcon);
3441 if (colorable) {
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003442 contentView.setDrawableParameters(R.id.icon, false, -1, resolveColor(),
Jorim Jaggi92df1f22014-12-16 19:44:41 +01003443 PorterDuff.Mode.SRC_ATOP, -1);
Selim Cinekea4bef72015-12-02 15:51:10 -08003444
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01003445 }
Selim Cinekea4bef72015-12-02 15:51:10 -08003446 contentView.setInt(R.id.notification_header, "setOriginalIconColor",
3447 colorable ? resolveColor() : NotificationHeaderView.NO_COLOR);
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01003448 }
3449
Dan Sandler26e81cf2014-05-06 10:01:27 -04003450 /**
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003451 * Make the largeIcon dark if it's a fake smallIcon (that is,
Dan Sandler26e81cf2014-05-06 10:01:27 -04003452 * if it's grayscale).
3453 */
3454 // TODO: also check bounds, transparency, that sort of thing.
Dan Sandlerd63f9322015-05-06 15:18:49 -04003455 private void processLargeLegacyIcon(Icon largeIcon, RemoteViews contentView) {
3456 if (largeIcon != null && isLegacy()
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003457 && getColorUtil().isGrayscaleIcon(mContext, largeIcon)) {
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003458 // resolve color will fall back to the default when legacy
3459 contentView.setDrawableParameters(R.id.icon, false, -1, resolveColor(),
Dan Sandler190d58d2014-05-15 09:33:39 -04003460 PorterDuff.Mode.SRC_ATOP, -1);
Jorim Jaggi92df1f22014-12-16 19:44:41 +01003461 }
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01003462 }
3463
Julia Reynolds10ee1fc2015-11-09 11:04:55 -05003464 private void sanitizeColor() {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003465 if (mN.color != COLOR_DEFAULT) {
3466 mN.color |= 0xFF000000; // no alpha for custom colors
Jorim Jaggi74419312014-06-10 20:57:21 +02003467 }
Jorim Jaggi74419312014-06-10 20:57:21 +02003468 }
3469
Selim Cinek5bf069a2015-11-10 19:14:27 -05003470 int resolveColor() {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003471 if (mN.color == COLOR_DEFAULT) {
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003472 return mContext.getColor(R.color.notification_icon_default_color);
Dan Sandler26e81cf2014-05-06 10:01:27 -04003473 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003474 return mN.color;
Dan Sandler26e81cf2014-05-06 10:01:27 -04003475 }
3476
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01003477 /**
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003478 * Apply the unstyled operations and return a new {@link Notification} object.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04003479 * @hide
Joe Onoratocb109a02011-01-18 17:57:41 -08003480 */
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04003481 public Notification buildUnstyled() {
Daniel Sandlera0a938c2012-03-15 08:42:37 -04003482 if (mActions.size() > 0) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003483 mN.actions = new Action[mActions.size()];
3484 mActions.toArray(mN.actions);
Daniel Sandlera0a938c2012-03-15 08:42:37 -04003485 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003486 if (!mPersonList.isEmpty()) {
3487 mN.extras.putStringArray(EXTRA_PEOPLE,
3488 mPersonList.toArray(new String[mPersonList.size()]));
Dan Sandler0bf2ed82013-12-21 23:33:41 -06003489 }
Selim Cinek247fa012016-02-18 09:50:48 -08003490 if (mN.bigContentView != null || mN.contentView != null
3491 || mN.headsUpContentView != null) {
3492 mN.extras.putBoolean(EXTRA_CONTAINS_CUSTOM_VIEW, true);
3493 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003494 return mN;
Joe Onorato46439ce2010-11-19 13:56:21 -08003495 }
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003496
Julia Reynolds3b848122016-02-26 10:45:32 -05003497 /**
3498 * Creates a Builder from an existing notification so further changes can be made.
3499 * @param context The context for your application / activity.
3500 * @param n The notification to create a Builder from.
3501 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003502 public static Notification.Builder recoverBuilder(Context context, Notification n) {
Christoph Studer4600f9b2014-07-22 22:44:43 +02003503 // Re-create notification context so we can access app resources.
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003504 ApplicationInfo applicationInfo = n.extras.getParcelable(
3505 EXTRA_BUILDER_APPLICATION_INFO);
Christoph Studer4600f9b2014-07-22 22:44:43 +02003506 Context builderContext;
Julia Reynoldsda303542015-11-23 14:00:20 -05003507 if (applicationInfo != null) {
3508 try {
3509 builderContext = context.createApplicationContext(applicationInfo,
3510 Context.CONTEXT_RESTRICTED);
3511 } catch (NameNotFoundException e) {
3512 Log.e(TAG, "ApplicationInfo " + applicationInfo + " not found");
3513 builderContext = context; // try with our context
3514 }
3515 } else {
3516 builderContext = context; // try with given context
Christoph Studer4600f9b2014-07-22 22:44:43 +02003517 }
3518
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003519 return new Builder(builderContext, n);
Christoph Studer4600f9b2014-07-22 22:44:43 +02003520 }
3521
3522 private static Class<? extends Style> getNotificationStyleClass(String templateClass) {
Selim Cinek593610c2016-02-16 18:42:57 -08003523 Class<? extends Style>[] classes = new Class[] {
3524 BigTextStyle.class, BigPictureStyle.class, InboxStyle.class, MediaStyle.class,
Selim Cinek03eb3b72016-02-18 10:39:45 -08003525 DecoratedCustomViewStyle.class, DecoratedMediaCustomViewStyle.class };
Christoph Studer4600f9b2014-07-22 22:44:43 +02003526 for (Class<? extends Style> innerClass : classes) {
3527 if (templateClass.equals(innerClass.getName())) {
3528 return innerClass;
3529 }
3530 }
3531 return null;
3532 }
3533
Daniel Sandlerf45564e2013-04-15 15:05:08 -04003534 /**
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003535 * @deprecated Use {@link #build()} instead.
3536 */
3537 @Deprecated
3538 public Notification getNotification() {
3539 return build();
3540 }
3541
3542 /**
3543 * Combine all of the options that have been set and return a new {@link Notification}
3544 * object.
3545 */
3546 public Notification build() {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003547 // first, add any extras from the calling code
3548 if (mUserExtras != null) {
3549 mN.extras = getAllExtras();
Jorim Jaggia0d58ae2015-06-03 11:48:13 -07003550 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003551
3552 // lazy stuff from mContext; see comment in Builder(Context, Notification)
Julia Reynoldsda303542015-11-23 14:00:20 -05003553 Notification.addFieldsFromContext(mContext, mN);
Christoph Studer943aa672014-08-03 20:31:16 +02003554
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003555 buildUnstyled();
Daniel Sandlerf45564e2013-04-15 15:05:08 -04003556
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003557 if (mStyle != null) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003558 mStyle.buildStyled(mN);
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003559 }
Daniel Sandlerf45564e2013-04-15 15:05:08 -04003560
Adrian Roos5081c0d2016-02-26 16:04:19 -08003561 if (mContext.getApplicationInfo().targetSdkVersion < Build.VERSION_CODES.N
3562 && (mStyle == null || !mStyle.displayCustomViewInline())) {
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05003563 if (mN.contentView == null) {
Julia Reynolds3b848122016-02-26 10:45:32 -05003564 mN.contentView = createContentView();
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05003565 mN.extras.putInt(EXTRA_REBUILD_CONTENT_VIEW_ACTION_COUNT,
3566 mN.contentView.getSequenceNumber());
3567 }
3568 if (mN.bigContentView == null) {
Julia Reynolds3b848122016-02-26 10:45:32 -05003569 mN.bigContentView = createBigContentView();
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05003570 if (mN.bigContentView != null) {
3571 mN.extras.putInt(EXTRA_REBUILD_BIG_CONTENT_VIEW_ACTION_COUNT,
3572 mN.bigContentView.getSequenceNumber());
3573 }
3574 }
3575 if (mN.headsUpContentView == null) {
Julia Reynolds3b848122016-02-26 10:45:32 -05003576 mN.headsUpContentView = createHeadsUpContentView();
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05003577 if (mN.headsUpContentView != null) {
3578 mN.extras.putInt(EXTRA_REBUILD_HEADS_UP_CONTENT_VIEW_ACTION_COUNT,
3579 mN.headsUpContentView.getSequenceNumber());
3580 }
3581 }
3582 }
3583
Julia Reynolds4c0c2022016-02-02 15:11:59 -05003584 if ((mN.defaults & DEFAULT_LIGHTS) != 0) {
3585 mN.flags |= FLAG_SHOW_LIGHTS;
3586 }
3587
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003588 return mN;
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003589 }
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05003590
3591 /**
3592 * Apply this Builder to an existing {@link Notification} object.
3593 *
3594 * @hide
3595 */
3596 public Notification buildInto(Notification n) {
Daniel Sandler1a497d32013-04-18 14:52:45 -04003597 build().cloneInto(n, true);
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05003598 return n;
3599 }
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01003600
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05003601 /**
3602 * @hide
3603 */
3604 public static void stripForDelivery(Notification n) {
3605 String templateClass = n.extras.getString(EXTRA_TEMPLATE);
3606 if (TextUtils.isEmpty(templateClass)) {
3607 return;
3608 }
3609 // Only strip views for known Styles because we won't know how to
3610 // re-create them otherwise.
3611 if (getNotificationStyleClass(templateClass) == null) {
3612 return;
3613 }
3614 // Get rid of unmodified BuilderRemoteViews.
3615 if (n.contentView instanceof BuilderRemoteViews &&
3616 n.extras.getInt(EXTRA_REBUILD_CONTENT_VIEW_ACTION_COUNT, -1) ==
3617 n.contentView.getSequenceNumber()) {
3618 n.contentView = null;
3619 n.extras.remove(EXTRA_REBUILD_CONTENT_VIEW_ACTION_COUNT);
3620 }
3621 if (n.bigContentView instanceof BuilderRemoteViews &&
3622 n.extras.getInt(EXTRA_REBUILD_BIG_CONTENT_VIEW_ACTION_COUNT, -1) ==
3623 n.bigContentView.getSequenceNumber()) {
3624 n.bigContentView = null;
3625 n.extras.remove(EXTRA_REBUILD_BIG_CONTENT_VIEW_ACTION_COUNT);
3626 }
3627 if (n.headsUpContentView instanceof BuilderRemoteViews &&
3628 n.extras.getInt(EXTRA_REBUILD_HEADS_UP_CONTENT_VIEW_ACTION_COUNT, -1) ==
3629 n.headsUpContentView.getSequenceNumber()) {
3630 n.headsUpContentView = null;
3631 n.extras.remove(EXTRA_REBUILD_HEADS_UP_CONTENT_VIEW_ACTION_COUNT);
3632 }
3633 }
3634
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01003635 private int getBaseLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07003636 return R.layout.notification_template_material_base;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01003637 }
3638
3639 private int getBigBaseLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07003640 return R.layout.notification_template_material_big_base;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01003641 }
3642
3643 private int getBigPictureLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07003644 return R.layout.notification_template_material_big_picture;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01003645 }
3646
3647 private int getBigTextLayoutResource() {
Jorim Jaggi445d3c02014-08-19 22:33:42 +02003648 return R.layout.notification_template_material_big_text;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01003649 }
3650
3651 private int getInboxLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07003652 return R.layout.notification_template_material_inbox;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01003653 }
3654
3655 private int getActionLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07003656 return R.layout.notification_material_action;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01003657 }
3658
3659 private int getActionTombstoneLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07003660 return R.layout.notification_material_action_tombstone;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01003661 }
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003662 }
3663
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003664 /**
3665 * An object that can apply a rich notification style to a {@link Notification.Builder}
3666 * object.
3667 */
Griff Hazendfcb0802014-02-11 12:00:00 -08003668 public static abstract class Style {
Chris Wrend6297db2012-05-03 16:20:13 -04003669 private CharSequence mBigContentTitle;
Jorim Jaggi457a10d2014-09-08 16:18:23 +02003670
3671 /**
3672 * @hide
3673 */
3674 protected CharSequence mSummaryText = null;
3675
3676 /**
3677 * @hide
3678 */
3679 protected boolean mSummaryTextSet = false;
Chris Wrend6297db2012-05-03 16:20:13 -04003680
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003681 protected Builder mBuilder;
3682
Chris Wrend6297db2012-05-03 16:20:13 -04003683 /**
3684 * Overrides ContentTitle in the big form of the template.
3685 * This defaults to the value passed to setContentTitle().
3686 */
3687 protected void internalSetBigContentTitle(CharSequence title) {
3688 mBigContentTitle = title;
3689 }
3690
3691 /**
3692 * Set the first line of text after the detail section in the big form of the template.
3693 */
3694 protected void internalSetSummaryText(CharSequence cs) {
3695 mSummaryText = cs;
Daniel Sandler619738c2012-06-07 16:33:08 -04003696 mSummaryTextSet = true;
Chris Wrend6297db2012-05-03 16:20:13 -04003697 }
3698
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003699 public void setBuilder(Builder builder) {
3700 if (mBuilder != builder) {
3701 mBuilder = builder;
Daniel Sandlerc08dea22012-06-28 08:35:24 -07003702 if (mBuilder != null) {
3703 mBuilder.setStyle(this);
3704 }
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003705 }
3706 }
3707
Chris Wrend6297db2012-05-03 16:20:13 -04003708 protected void checkBuilder() {
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003709 if (mBuilder == null) {
3710 throw new IllegalArgumentException("Style requires a valid Builder object");
3711 }
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003712 }
Chris Wrend6297db2012-05-03 16:20:13 -04003713
3714 protected RemoteViews getStandardView(int layoutId) {
3715 checkBuilder();
3716
Christoph Studer4600f9b2014-07-22 22:44:43 +02003717 // Nasty.
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003718 CharSequence oldBuilderContentTitle =
3719 mBuilder.getAllExtras().getCharSequence(EXTRA_TITLE);
Chris Wrend6297db2012-05-03 16:20:13 -04003720 if (mBigContentTitle != null) {
3721 mBuilder.setContentTitle(mBigContentTitle);
3722 }
3723
Chris Wrend6297db2012-05-03 16:20:13 -04003724 RemoteViews contentView = mBuilder.applyStandardTemplateWithActions(layoutId);
3725
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003726 mBuilder.getAllExtras().putCharSequence(EXTRA_TITLE, oldBuilderContentTitle);
Christoph Studer4600f9b2014-07-22 22:44:43 +02003727
Chris Wrend6297db2012-05-03 16:20:13 -04003728 if (mBigContentTitle != null && mBigContentTitle.equals("")) {
3729 contentView.setViewVisibility(R.id.line1, View.GONE);
Chris Wren67dc9a02012-05-16 01:03:20 -04003730 } else {
3731 contentView.setViewVisibility(R.id.line1, View.VISIBLE);
Chris Wrend6297db2012-05-03 16:20:13 -04003732 }
3733
Chris Wrend6297db2012-05-03 16:20:13 -04003734 return contentView;
3735 }
3736
Daniel Sandlerf45564e2013-04-15 15:05:08 -04003737 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003738 * Construct a Style-specific RemoteViews for the final 1U notification layout.
3739 * The default implementation has nothing additional to add.
3740 * @hide
3741 */
3742 public RemoteViews makeContentView() {
3743 return null;
3744 }
3745
3746 /**
3747 * Construct a Style-specific RemoteViews for the final big notification layout.
3748 * @hide
3749 */
3750 public RemoteViews makeBigContentView() {
3751 return null;
3752 }
3753
3754 /**
3755 * Construct a Style-specific RemoteViews for the final HUN layout.
3756 * @hide
3757 */
3758 public RemoteViews makeHeadsUpContentView() {
3759 return null;
3760 }
3761
3762 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003763 * Apply any style-specific extras to this notification before shipping it out.
Daniel Sandlerf45564e2013-04-15 15:05:08 -04003764 * @hide
3765 */
3766 public void addExtras(Bundle extras) {
3767 if (mSummaryTextSet) {
3768 extras.putCharSequence(EXTRA_SUMMARY_TEXT, mSummaryText);
3769 }
3770 if (mBigContentTitle != null) {
3771 extras.putCharSequence(EXTRA_TITLE_BIG, mBigContentTitle);
3772 }
Chris Wren91ad5632013-06-05 15:05:57 -04003773 extras.putString(EXTRA_TEMPLATE, this.getClass().getName());
Daniel Sandlerf45564e2013-04-15 15:05:08 -04003774 }
3775
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04003776 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003777 * Reconstruct the internal state of this Style object from extras.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04003778 * @hide
3779 */
Christoph Studer4600f9b2014-07-22 22:44:43 +02003780 protected void restoreFromExtras(Bundle extras) {
3781 if (extras.containsKey(EXTRA_SUMMARY_TEXT)) {
3782 mSummaryText = extras.getCharSequence(EXTRA_SUMMARY_TEXT);
3783 mSummaryTextSet = true;
3784 }
3785 if (extras.containsKey(EXTRA_TITLE_BIG)) {
3786 mBigContentTitle = extras.getCharSequence(EXTRA_TITLE_BIG);
3787 }
3788 }
3789
3790
3791 /**
3792 * @hide
3793 */
3794 public Notification buildStyled(Notification wip) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003795 addExtras(wip.extras);
Christoph Studer4600f9b2014-07-22 22:44:43 +02003796 return wip;
3797 }
3798
Daniel Sandler0ec46202015-06-24 01:27:05 -04003799 /**
3800 * @hide
3801 */
Jorim Jaggia0d58ae2015-06-03 11:48:13 -07003802 public void purgeResources() {}
3803
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04003804 /**
3805 * Calls {@link android.app.Notification.Builder#build()} on the Builder this Style is
3806 * attached to.
3807 *
3808 * @return the fully constructed Notification.
3809 */
3810 public Notification build() {
3811 checkBuilder();
3812 return mBuilder.build();
3813 }
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02003814
3815 /**
3816 * @hide
3817 * @return true if the style positions the progress bar on the second line; false if the
3818 * style hides the progress bar
3819 */
3820 protected boolean hasProgress() {
3821 return true;
3822 }
Selim Cinek03d0d652015-11-13 13:18:09 -05003823
3824 /**
3825 * @hide
3826 * @return Whether we should put the summary be put into the notification header
3827 */
3828 public boolean hasSummaryInHeader() {
3829 return true;
3830 }
Selim Cinek593610c2016-02-16 18:42:57 -08003831
3832 /**
3833 * @hide
3834 * @return Whether custom content views are displayed inline in the style
3835 */
3836 public boolean displayCustomViewInline() {
3837 return false;
3838 }
Joe Onorato46439ce2010-11-19 13:56:21 -08003839 }
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003840
3841 /**
Daniel Sandler4dfbe832012-04-11 14:51:46 -04003842 * Helper class for generating large-format notifications that include a large image attachment.
Joe Malin8d40d042012-11-05 11:36:40 -08003843 *
Robert Ly91c5ce32014-06-08 15:37:00 -07003844 * Here's how you'd set the <code>BigPictureStyle</code> on a notification:
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003845 * <pre class="prettyprint">
Robert Ly91c5ce32014-06-08 15:37:00 -07003846 * Notification notif = new Notification.Builder(mContext)
3847 * .setContentTitle(&quot;New photo from &quot; + sender.toString())
3848 * .setContentText(subject)
3849 * .setSmallIcon(R.drawable.new_post)
3850 * .setLargeIcon(aBitmap)
3851 * .setStyle(new Notification.BigPictureStyle()
3852 * .bigPicture(aBigBitmap))
3853 * .build();
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003854 * </pre>
Joe Malin8d40d042012-11-05 11:36:40 -08003855 *
Daniel Sandler4dfbe832012-04-11 14:51:46 -04003856 * @see Notification#bigContentView
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003857 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003858 public static class BigPictureStyle extends Style {
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003859 private Bitmap mPicture;
Dan Sandlerd63f9322015-05-06 15:18:49 -04003860 private Icon mBigLargeIcon;
Chris Wren3745a3d2012-05-22 15:11:52 -04003861 private boolean mBigLargeIconSet = false;
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003862
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003863 public BigPictureStyle() {
3864 }
3865
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003866 public BigPictureStyle(Builder builder) {
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003867 setBuilder(builder);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003868 }
3869
Chris Wrend6297db2012-05-03 16:20:13 -04003870 /**
3871 * Overrides ContentTitle in the big form of the template.
3872 * This defaults to the value passed to setContentTitle().
3873 */
3874 public BigPictureStyle setBigContentTitle(CharSequence title) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04003875 internalSetBigContentTitle(safeCharSequence(title));
Chris Wrend6297db2012-05-03 16:20:13 -04003876 return this;
3877 }
3878
3879 /**
3880 * Set the first line of text after the detail section in the big form of the template.
3881 */
3882 public BigPictureStyle setSummaryText(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04003883 internalSetSummaryText(safeCharSequence(cs));
Chris Wrend6297db2012-05-03 16:20:13 -04003884 return this;
3885 }
3886
Chris Wren0bd664d2012-08-01 13:56:56 -04003887 /**
3888 * Provide the bitmap to be used as the payload for the BigPicture notification.
3889 */
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003890 public BigPictureStyle bigPicture(Bitmap b) {
3891 mPicture = b;
3892 return this;
3893 }
3894
Chris Wren3745a3d2012-05-22 15:11:52 -04003895 /**
Chris Wren3745a3d2012-05-22 15:11:52 -04003896 * Override the large icon when the big notification is shown.
3897 */
3898 public BigPictureStyle bigLargeIcon(Bitmap b) {
Dan Sandlerd63f9322015-05-06 15:18:49 -04003899 return bigLargeIcon(b != null ? Icon.createWithBitmap(b) : null);
3900 }
3901
3902 /**
3903 * Override the large icon when the big notification is shown.
3904 */
3905 public BigPictureStyle bigLargeIcon(Icon icon) {
Chris Wren3745a3d2012-05-22 15:11:52 -04003906 mBigLargeIconSet = true;
Dan Sandlerd63f9322015-05-06 15:18:49 -04003907 mBigLargeIcon = icon;
Chris Wren3745a3d2012-05-22 15:11:52 -04003908 return this;
3909 }
3910
Riley Andrews0394a0c2015-11-03 23:36:52 -08003911 /** @hide */
3912 public static final int MIN_ASHMEM_BITMAP_SIZE = 128 * (1 << 10);
3913
Daniel Sandler0ec46202015-06-24 01:27:05 -04003914 /**
3915 * @hide
3916 */
Jorim Jaggia0d58ae2015-06-03 11:48:13 -07003917 @Override
3918 public void purgeResources() {
3919 super.purgeResources();
Riley Andrews8cee7c12015-11-01 23:36:04 -08003920 if (mPicture != null &&
3921 mPicture.isMutable() &&
Riley Andrews0394a0c2015-11-03 23:36:52 -08003922 mPicture.getAllocationByteCount() >= MIN_ASHMEM_BITMAP_SIZE) {
Jorim Jaggia0d58ae2015-06-03 11:48:13 -07003923 mPicture = mPicture.createAshmemBitmap();
3924 }
3925 if (mBigLargeIcon != null) {
3926 mBigLargeIcon.convertToAshmem();
3927 }
3928 }
Christoph Studer5c510ee2014-12-15 16:32:27 +01003929
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003930 /**
3931 * @hide
3932 */
3933 public RemoteViews makeBigContentView() {
3934 // Replace mN.mLargeIcon with mBigLargeIcon if mBigLargeIconSet
Christoph Studer5c510ee2014-12-15 16:32:27 +01003935 // This covers the following cases:
3936 // 1. mBigLargeIconSet -> mBigLargeIcon (null or non-null) applies, overrides
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003937 // mN.mLargeIcon
3938 // 2. !mBigLargeIconSet -> mN.mLargeIcon applies
Dan Sandlerd63f9322015-05-06 15:18:49 -04003939 Icon oldLargeIcon = null;
Christoph Studer5c510ee2014-12-15 16:32:27 +01003940 if (mBigLargeIconSet) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003941 oldLargeIcon = mBuilder.mN.mLargeIcon;
3942 mBuilder.mN.mLargeIcon = mBigLargeIcon;
Christoph Studer5c510ee2014-12-15 16:32:27 +01003943 }
3944
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01003945 RemoteViews contentView = getStandardView(mBuilder.getBigPictureLayoutResource());
Selim Cinek03d0d652015-11-13 13:18:09 -05003946 if (mSummaryTextSet) {
3947 contentView.setTextViewText(R.id.text, mBuilder.processLegacyText(mSummaryText));
Selim Cinekc848c3a2016-01-13 15:27:30 -08003948 contentView.setViewVisibility(R.id.text, View.VISIBLE);
Selim Cinek03d0d652015-11-13 13:18:09 -05003949 }
Selim Cinek860b6da2015-12-16 19:02:19 -08003950 mBuilder.setContentMinHeight(contentView, mBuilder.mN.mLargeIcon != null);
Selim Cinek53e64a42015-11-16 10:40:56 -08003951
Christoph Studer5c510ee2014-12-15 16:32:27 +01003952 if (mBigLargeIconSet) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003953 mBuilder.mN.mLargeIcon = oldLargeIcon;
Christoph Studer5c510ee2014-12-15 16:32:27 +01003954 }
3955
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003956 contentView.setImageViewBitmap(R.id.big_picture, mPicture);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003957 return contentView;
3958 }
3959
Daniel Sandlerf45564e2013-04-15 15:05:08 -04003960 /**
3961 * @hide
3962 */
3963 public void addExtras(Bundle extras) {
3964 super.addExtras(extras);
3965
3966 if (mBigLargeIconSet) {
3967 extras.putParcelable(EXTRA_LARGE_ICON_BIG, mBigLargeIcon);
3968 }
3969 extras.putParcelable(EXTRA_PICTURE, mPicture);
3970 }
3971
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04003972 /**
3973 * @hide
3974 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003975 @Override
Christoph Studer4600f9b2014-07-22 22:44:43 +02003976 protected void restoreFromExtras(Bundle extras) {
3977 super.restoreFromExtras(extras);
3978
3979 if (extras.containsKey(EXTRA_LARGE_ICON_BIG)) {
Christoph Studer5c510ee2014-12-15 16:32:27 +01003980 mBigLargeIconSet = true;
Christoph Studer4600f9b2014-07-22 22:44:43 +02003981 mBigLargeIcon = extras.getParcelable(EXTRA_LARGE_ICON_BIG);
Chris Wren3745a3d2012-05-22 15:11:52 -04003982 }
Christoph Studer4600f9b2014-07-22 22:44:43 +02003983 mPicture = extras.getParcelable(EXTRA_PICTURE);
3984 }
Selim Cinek03d0d652015-11-13 13:18:09 -05003985
3986 /**
3987 * @hide
3988 */
3989 @Override
3990 public boolean hasSummaryInHeader() {
3991 return false;
3992 }
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003993 }
3994
3995 /**
Daniel Sandler4dfbe832012-04-11 14:51:46 -04003996 * Helper class for generating large-format notifications that include a lot of text.
Joe Malin8d40d042012-11-05 11:36:40 -08003997 *
Robert Ly91c5ce32014-06-08 15:37:00 -07003998 * Here's how you'd set the <code>BigTextStyle</code> on a notification:
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003999 * <pre class="prettyprint">
Robert Ly91c5ce32014-06-08 15:37:00 -07004000 * Notification notif = new Notification.Builder(mContext)
4001 * .setContentTitle(&quot;New mail from &quot; + sender.toString())
4002 * .setContentText(subject)
4003 * .setSmallIcon(R.drawable.new_mail)
4004 * .setLargeIcon(aBitmap)
4005 * .setStyle(new Notification.BigTextStyle()
4006 * .bigText(aVeryLongString))
4007 * .build();
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004008 * </pre>
Joe Malin8d40d042012-11-05 11:36:40 -08004009 *
Daniel Sandler4dfbe832012-04-11 14:51:46 -04004010 * @see Notification#bigContentView
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004011 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004012 public static class BigTextStyle extends Style {
Jorim Jaggi457a10d2014-09-08 16:18:23 +02004013
4014 private static final int MAX_LINES = 13;
Selim Cinek3a2c4b92015-12-17 17:01:17 -08004015 private static final int LINES_CONSUMED_BY_ACTIONS = 4;
Jorim Jaggi457a10d2014-09-08 16:18:23 +02004016
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004017 private CharSequence mBigText;
4018
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004019 public BigTextStyle() {
4020 }
4021
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004022 public BigTextStyle(Builder builder) {
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004023 setBuilder(builder);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004024 }
4025
Chris Wrend6297db2012-05-03 16:20:13 -04004026 /**
4027 * Overrides ContentTitle in the big form of the template.
4028 * This defaults to the value passed to setContentTitle().
4029 */
4030 public BigTextStyle setBigContentTitle(CharSequence title) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04004031 internalSetBigContentTitle(safeCharSequence(title));
Chris Wrend6297db2012-05-03 16:20:13 -04004032 return this;
4033 }
4034
4035 /**
4036 * Set the first line of text after the detail section in the big form of the template.
4037 */
4038 public BigTextStyle setSummaryText(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04004039 internalSetSummaryText(safeCharSequence(cs));
Chris Wrend6297db2012-05-03 16:20:13 -04004040 return this;
4041 }
4042
Chris Wren0bd664d2012-08-01 13:56:56 -04004043 /**
4044 * Provide the longer text to be displayed in the big form of the
4045 * template in place of the content text.
4046 */
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004047 public BigTextStyle bigText(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04004048 mBigText = safeCharSequence(cs);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004049 return this;
4050 }
4051
Daniel Sandlerf45564e2013-04-15 15:05:08 -04004052 /**
4053 * @hide
4054 */
4055 public void addExtras(Bundle extras) {
4056 super.addExtras(extras);
4057
Christoph Studer4600f9b2014-07-22 22:44:43 +02004058 extras.putCharSequence(EXTRA_BIG_TEXT, mBigText);
4059 }
4060
4061 /**
4062 * @hide
4063 */
4064 @Override
4065 protected void restoreFromExtras(Bundle extras) {
4066 super.restoreFromExtras(extras);
4067
4068 mBigText = extras.getCharSequence(EXTRA_BIG_TEXT);
Daniel Sandlerf45564e2013-04-15 15:05:08 -04004069 }
4070
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004071 /**
4072 * @hide
4073 */
4074 public RemoteViews makeBigContentView() {
Christoph Studer4600f9b2014-07-22 22:44:43 +02004075
4076 // Nasty
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004077 CharSequence oldBuilderContentText =
4078 mBuilder.getAllExtras().getCharSequence(EXTRA_TEXT);
4079 mBuilder.getAllExtras().putCharSequence(EXTRA_TEXT, null);
Daniel Sandler916ad912012-06-13 12:17:07 -04004080
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01004081 RemoteViews contentView = getStandardView(mBuilder.getBigTextLayoutResource());
Joe Malin8d40d042012-11-05 11:36:40 -08004082
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004083 mBuilder.getAllExtras().putCharSequence(EXTRA_TEXT, oldBuilderContentText);
Christoph Studer4600f9b2014-07-22 22:44:43 +02004084
Selim Cinek3a2c4b92015-12-17 17:01:17 -08004085 CharSequence bigTextText = mBuilder.processLegacyText(mBigText);
4086 contentView.setTextViewText(R.id.big_text, bigTextText);
4087 contentView.setViewVisibility(R.id.big_text,
4088 TextUtils.isEmpty(bigTextText) ? View.GONE : View.VISIBLE);
Jorim Jaggi457a10d2014-09-08 16:18:23 +02004089 contentView.setInt(R.id.big_text, "setMaxLines", calculateMaxLines());
Selim Cinek4fb12d32015-11-19 18:10:48 -08004090 contentView.setBoolean(R.id.big_text, "setHasImage", mBuilder.mN.mLargeIcon != null);
4091
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004092 return contentView;
4093 }
4094
Jorim Jaggi457a10d2014-09-08 16:18:23 +02004095 private int calculateMaxLines() {
4096 int lineCount = MAX_LINES;
4097 boolean hasActions = mBuilder.mActions.size() > 0;
Jorim Jaggi457a10d2014-09-08 16:18:23 +02004098 if (hasActions) {
4099 lineCount -= LINES_CONSUMED_BY_ACTIONS;
4100 }
Jorim Jaggi457a10d2014-09-08 16:18:23 +02004101 return lineCount;
4102 }
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004103 }
Daniel Sandler879c5e02012-04-17 16:46:51 -04004104
4105 /**
4106 * Helper class for generating large-format notifications that include a list of (up to 5) strings.
Joe Malin8d40d042012-11-05 11:36:40 -08004107 *
Robert Ly91c5ce32014-06-08 15:37:00 -07004108 * Here's how you'd set the <code>InboxStyle</code> on a notification:
Daniel Sandler879c5e02012-04-17 16:46:51 -04004109 * <pre class="prettyprint">
Robert Ly91c5ce32014-06-08 15:37:00 -07004110 * Notification notif = new Notification.Builder(mContext)
4111 * .setContentTitle(&quot;5 New mails from &quot; + sender.toString())
4112 * .setContentText(subject)
4113 * .setSmallIcon(R.drawable.new_mail)
4114 * .setLargeIcon(aBitmap)
4115 * .setStyle(new Notification.InboxStyle()
4116 * .addLine(str1)
4117 * .addLine(str2)
4118 * .setContentTitle(&quot;&quot;)
4119 * .setSummaryText(&quot;+3 more&quot;))
4120 * .build();
Daniel Sandler879c5e02012-04-17 16:46:51 -04004121 * </pre>
Joe Malin8d40d042012-11-05 11:36:40 -08004122 *
Daniel Sandler879c5e02012-04-17 16:46:51 -04004123 * @see Notification#bigContentView
4124 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004125 public static class InboxStyle extends Style {
Daniel Sandler879c5e02012-04-17 16:46:51 -04004126 private ArrayList<CharSequence> mTexts = new ArrayList<CharSequence>(5);
4127
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004128 public InboxStyle() {
4129 }
4130
Daniel Sandler879c5e02012-04-17 16:46:51 -04004131 public InboxStyle(Builder builder) {
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004132 setBuilder(builder);
Daniel Sandler879c5e02012-04-17 16:46:51 -04004133 }
4134
Chris Wrend6297db2012-05-03 16:20:13 -04004135 /**
4136 * Overrides ContentTitle in the big form of the template.
4137 * This defaults to the value passed to setContentTitle().
4138 */
4139 public InboxStyle setBigContentTitle(CharSequence title) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04004140 internalSetBigContentTitle(safeCharSequence(title));
Chris Wrend6297db2012-05-03 16:20:13 -04004141 return this;
4142 }
4143
4144 /**
4145 * Set the first line of text after the detail section in the big form of the template.
4146 */
4147 public InboxStyle setSummaryText(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04004148 internalSetSummaryText(safeCharSequence(cs));
Chris Wrend6297db2012-05-03 16:20:13 -04004149 return this;
4150 }
4151
Chris Wren0bd664d2012-08-01 13:56:56 -04004152 /**
4153 * Append a line to the digest section of the Inbox notification.
4154 */
Daniel Sandler879c5e02012-04-17 16:46:51 -04004155 public InboxStyle addLine(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04004156 mTexts.add(safeCharSequence(cs));
Daniel Sandler879c5e02012-04-17 16:46:51 -04004157 return this;
4158 }
4159
Daniel Sandlerf45564e2013-04-15 15:05:08 -04004160 /**
4161 * @hide
4162 */
4163 public void addExtras(Bundle extras) {
4164 super.addExtras(extras);
Christoph Studer4600f9b2014-07-22 22:44:43 +02004165
Daniel Sandlerf45564e2013-04-15 15:05:08 -04004166 CharSequence[] a = new CharSequence[mTexts.size()];
4167 extras.putCharSequenceArray(EXTRA_TEXT_LINES, mTexts.toArray(a));
4168 }
4169
Christoph Studer4600f9b2014-07-22 22:44:43 +02004170 /**
4171 * @hide
4172 */
4173 @Override
4174 protected void restoreFromExtras(Bundle extras) {
4175 super.restoreFromExtras(extras);
4176
4177 mTexts.clear();
4178 if (extras.containsKey(EXTRA_TEXT_LINES)) {
4179 Collections.addAll(mTexts, extras.getCharSequenceArray(EXTRA_TEXT_LINES));
4180 }
4181 }
4182
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004183 /**
4184 * @hide
4185 */
4186 public RemoteViews makeBigContentView() {
Selim Cinekc848c3a2016-01-13 15:27:30 -08004187 // Remove the content text so it disappears unless you have a summary
Christoph Studer4600f9b2014-07-22 22:44:43 +02004188 // Nasty
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004189 CharSequence oldBuilderContentText = mBuilder.mN.extras.getCharSequence(EXTRA_TEXT);
4190 mBuilder.getAllExtras().putCharSequence(EXTRA_TEXT, null);
Christoph Studer4600f9b2014-07-22 22:44:43 +02004191
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01004192 RemoteViews contentView = getStandardView(mBuilder.getInboxLayoutResource());
Daniel Sandler619738c2012-06-07 16:33:08 -04004193
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004194 mBuilder.getAllExtras().putCharSequence(EXTRA_TEXT, oldBuilderContentText);
Christoph Studer4600f9b2014-07-22 22:44:43 +02004195
Chris Wrend6297db2012-05-03 16:20:13 -04004196 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 -04004197 R.id.inbox_text4, R.id.inbox_text5, R.id.inbox_text6};
Chris Wrend6297db2012-05-03 16:20:13 -04004198
Chris Wren4ed80d52012-05-17 09:30:03 -04004199 // Make sure all rows are gone in case we reuse a view.
4200 for (int rowId : rowIds) {
4201 contentView.setViewVisibility(rowId, View.GONE);
4202 }
4203
Jorim Jaggi445d3c02014-08-19 22:33:42 +02004204 final boolean largeText =
4205 mBuilder.mContext.getResources().getConfiguration().fontScale > 1f;
4206 final float subTextSize = mBuilder.mContext.getResources().getDimensionPixelSize(
4207 R.dimen.notification_subtext_size);
Daniel Sandler879c5e02012-04-17 16:46:51 -04004208 int i=0;
Selim Cinek9d9fc6e2015-11-12 15:49:14 -05004209 final float density = mBuilder.mContext.getResources().getDisplayMetrics().density;
4210 int topPadding = (int) (5 * density);
4211 int bottomPadding = (int) (13 * density);
Selim Cinek247fa012016-02-18 09:50:48 -08004212 boolean first = true;
Daniel Sandler879c5e02012-04-17 16:46:51 -04004213 while (i < mTexts.size() && i < rowIds.length) {
4214 CharSequence str = mTexts.get(i);
4215 if (str != null && !str.equals("")) {
4216 contentView.setViewVisibility(rowIds[i], View.VISIBLE);
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01004217 contentView.setTextViewText(rowIds[i], mBuilder.processLegacyText(str));
Jorim Jaggi445d3c02014-08-19 22:33:42 +02004218 if (largeText) {
4219 contentView.setTextViewTextSize(rowIds[i], TypedValue.COMPLEX_UNIT_PX,
4220 subTextSize);
4221 }
Selim Cinek9d9fc6e2015-11-12 15:49:14 -05004222 contentView.setViewPadding(rowIds[i], 0, topPadding, 0,
4223 i == rowIds.length - 1 || i == mTexts.size() - 1 ? bottomPadding : 0);
Selim Cinek247fa012016-02-18 09:50:48 -08004224 handleInboxImageMargin(contentView, rowIds[i], first);
4225 first = false;
Daniel Sandler879c5e02012-04-17 16:46:51 -04004226 }
4227 i++;
4228 }
Kenny Guy98193ea2014-07-24 19:54:37 +01004229
Selim Cinek1e0bf612015-11-20 15:57:26 -08004230
Daniel Sandler879c5e02012-04-17 16:46:51 -04004231 return contentView;
4232 }
Selim Cinek1e0bf612015-11-20 15:57:26 -08004233
Selim Cinek247fa012016-02-18 09:50:48 -08004234 private void handleInboxImageMargin(RemoteViews contentView, int id, boolean first) {
Selim Cinek1e0bf612015-11-20 15:57:26 -08004235 int endMargin = 0;
Selim Cinek247fa012016-02-18 09:50:48 -08004236 if (first) {
4237 final int max = mBuilder.mN.extras.getInt(EXTRA_PROGRESS_MAX, 0);
4238 final boolean ind = mBuilder.mN.extras.getBoolean(EXTRA_PROGRESS_INDETERMINATE);
4239 boolean hasProgress = max != 0 || ind;
4240 if (mBuilder.mN.mLargeIcon != null && !hasProgress) {
4241 endMargin = mBuilder.mContext.getResources().getDimensionPixelSize(
4242 R.dimen.notification_content_picture_margin);
4243 }
Selim Cinek1e0bf612015-11-20 15:57:26 -08004244 }
4245 contentView.setViewLayoutMarginEnd(id, endMargin);
4246 }
Daniel Sandler879c5e02012-04-17 16:46:51 -04004247 }
Dan Sandler842dd772014-05-15 09:36:47 -04004248
4249 /**
4250 * Notification style for media playback notifications.
4251 *
4252 * In the expanded form, {@link Notification#bigContentView}, up to 5
4253 * {@link Notification.Action}s specified with
Dan Sandler86647982015-05-13 23:41:13 -04004254 * {@link Notification.Builder#addAction(Action) addAction} will be
Dan Sandler842dd772014-05-15 09:36:47 -04004255 * shown as icon-only pushbuttons, suitable for transport controls. The Bitmap given to
4256 * {@link Notification.Builder#setLargeIcon(android.graphics.Bitmap) setLargeIcon()} will be
4257 * treated as album artwork.
4258 *
4259 * Unlike the other styles provided here, MediaStyle can also modify the standard-size
4260 * {@link Notification#contentView}; by providing action indices to
Christoph Studerfde6f4d2014-12-12 13:23:26 +01004261 * {@link #setShowActionsInCompactView(int...)} you can promote up to 3 actions to be displayed
Dan Sandler842dd772014-05-15 09:36:47 -04004262 * in the standard view alongside the usual content.
4263 *
Bryan Mawhinney6be8de32014-07-18 10:35:12 +01004264 * Notifications created with MediaStyle will have their category set to
4265 * {@link Notification#CATEGORY_TRANSPORT CATEGORY_TRANSPORT} unless you set a different
4266 * category using {@link Notification.Builder#setCategory(String) setCategory()}.
4267 *
Jeff Browndba34ba2014-06-24 20:46:03 -07004268 * Finally, if you attach a {@link android.media.session.MediaSession.Token} using
4269 * {@link android.app.Notification.MediaStyle#setMediaSession(MediaSession.Token)},
Dan Sandler842dd772014-05-15 09:36:47 -04004270 * the System UI can identify this as a notification representing an active media session
4271 * and respond accordingly (by showing album artwork in the lockscreen, for example).
4272 *
4273 * To use this style with your Notification, feed it to
4274 * {@link Notification.Builder#setStyle(android.app.Notification.Style)} like so:
4275 * <pre class="prettyprint">
4276 * Notification noti = new Notification.Builder()
4277 * .setSmallIcon(R.drawable.ic_stat_player)
Christoph Studere935fe92014-11-24 14:18:06 +01004278 * .setContentTitle(&quot;Track title&quot;)
4279 * .setContentText(&quot;Artist - Album&quot;)
4280 * .setLargeIcon(albumArtBitmap))
Bryan Mawhinney6be8de32014-07-18 10:35:12 +01004281 * .setStyle(<b>new Notification.MediaStyle()</b>
4282 * .setMediaSession(mySession))
Dan Sandler842dd772014-05-15 09:36:47 -04004283 * .build();
4284 * </pre>
4285 *
4286 * @see Notification#bigContentView
4287 */
4288 public static class MediaStyle extends Style {
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02004289 static final int MAX_MEDIA_BUTTONS_IN_COMPACT = 3;
Dan Sandler842dd772014-05-15 09:36:47 -04004290 static final int MAX_MEDIA_BUTTONS = 5;
4291
4292 private int[] mActionsToShowInCompact = null;
Jeff Browndba34ba2014-06-24 20:46:03 -07004293 private MediaSession.Token mToken;
Dan Sandler842dd772014-05-15 09:36:47 -04004294
4295 public MediaStyle() {
4296 }
4297
4298 public MediaStyle(Builder builder) {
4299 setBuilder(builder);
4300 }
4301
4302 /**
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02004303 * 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 -04004304 * notification view.
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02004305 *
4306 * @param actions the indices of the actions to show in the compact notification view
Dan Sandler842dd772014-05-15 09:36:47 -04004307 */
4308 public MediaStyle setShowActionsInCompactView(int...actions) {
4309 mActionsToShowInCompact = actions;
4310 return this;
4311 }
4312
4313 /**
Jeff Browndba34ba2014-06-24 20:46:03 -07004314 * Attach a {@link android.media.session.MediaSession.Token} to this Notification
4315 * to provide additional playback information and control to the SystemUI.
Dan Sandler842dd772014-05-15 09:36:47 -04004316 */
Jeff Browndba34ba2014-06-24 20:46:03 -07004317 public MediaStyle setMediaSession(MediaSession.Token token) {
Dan Sandler842dd772014-05-15 09:36:47 -04004318 mToken = token;
4319 return this;
4320 }
4321
Christoph Studer4600f9b2014-07-22 22:44:43 +02004322 /**
4323 * @hide
4324 */
Dan Sandler842dd772014-05-15 09:36:47 -04004325 @Override
4326 public Notification buildStyled(Notification wip) {
Christoph Studer4600f9b2014-07-22 22:44:43 +02004327 super.buildStyled(wip);
Bryan Mawhinney6be8de32014-07-18 10:35:12 +01004328 if (wip.category == null) {
4329 wip.category = Notification.CATEGORY_TRANSPORT;
4330 }
Dan Sandler842dd772014-05-15 09:36:47 -04004331 return wip;
4332 }
4333
Christoph Studer4600f9b2014-07-22 22:44:43 +02004334 /**
4335 * @hide
4336 */
4337 @Override
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004338 public RemoteViews makeContentView() {
4339 return makeMediaContentView();
Christoph Studer4600f9b2014-07-22 22:44:43 +02004340 }
4341
4342 /**
4343 * @hide
4344 */
4345 @Override
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004346 public RemoteViews makeBigContentView() {
4347 return makeMediaBigContentView();
Christoph Studer4600f9b2014-07-22 22:44:43 +02004348 }
4349
Selim Cinekcc10bfb2016-02-10 16:24:21 -08004350 /**
4351 * @hide
4352 */
4353 @Override
4354 public RemoteViews makeHeadsUpContentView() {
4355 RemoteViews expanded = makeMediaBigContentView();
4356 return expanded != null ? expanded : makeMediaContentView();
4357 }
4358
Dan Sandler842dd772014-05-15 09:36:47 -04004359 /** @hide */
4360 @Override
4361 public void addExtras(Bundle extras) {
4362 super.addExtras(extras);
4363
4364 if (mToken != null) {
4365 extras.putParcelable(EXTRA_MEDIA_SESSION, mToken);
4366 }
Bryan Mawhinneye191f902014-07-22 12:50:09 +01004367 if (mActionsToShowInCompact != null) {
4368 extras.putIntArray(EXTRA_COMPACT_ACTIONS, mActionsToShowInCompact);
4369 }
Dan Sandler842dd772014-05-15 09:36:47 -04004370 }
4371
Christoph Studer4600f9b2014-07-22 22:44:43 +02004372 /**
4373 * @hide
4374 */
4375 @Override
4376 protected void restoreFromExtras(Bundle extras) {
4377 super.restoreFromExtras(extras);
4378
4379 if (extras.containsKey(EXTRA_MEDIA_SESSION)) {
4380 mToken = extras.getParcelable(EXTRA_MEDIA_SESSION);
4381 }
4382 if (extras.containsKey(EXTRA_COMPACT_ACTIONS)) {
4383 mActionsToShowInCompact = extras.getIntArray(EXTRA_COMPACT_ACTIONS);
4384 }
4385 }
4386
Selim Cinek5bf069a2015-11-10 19:14:27 -05004387 private RemoteViews generateMediaActionButton(Action action, int color) {
Dan Sandler842dd772014-05-15 09:36:47 -04004388 final boolean tombstone = (action.actionIntent == null);
Selim Cinekf33b1112015-07-15 17:45:11 -07004389 RemoteViews button = new BuilderRemoteViews(mBuilder.mContext.getApplicationInfo(),
Alan Viverette3cb07a462014-06-06 14:19:53 -07004390 R.layout.notification_material_media_action);
Dan Sandler68079d52015-07-22 10:45:30 -04004391 button.setImageViewIcon(R.id.action0, action.getIcon());
Selim Cinek5bf069a2015-11-10 19:14:27 -05004392 button.setDrawableParameters(R.id.action0, false, -1, color, PorterDuff.Mode.SRC_ATOP,
4393 -1);
Dan Sandler842dd772014-05-15 09:36:47 -04004394 if (!tombstone) {
4395 button.setOnClickPendingIntent(R.id.action0, action.actionIntent);
4396 }
4397 button.setContentDescription(R.id.action0, action.title);
4398 return button;
4399 }
4400
4401 private RemoteViews makeMediaContentView() {
4402 RemoteViews view = mBuilder.applyStandardTemplate(
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02004403 R.layout.notification_template_material_media, false /* hasProgress */);
Dan Sandler842dd772014-05-15 09:36:47 -04004404
4405 final int numActions = mBuilder.mActions.size();
4406 final int N = mActionsToShowInCompact == null
4407 ? 0
4408 : Math.min(mActionsToShowInCompact.length, MAX_MEDIA_BUTTONS_IN_COMPACT);
4409 if (N > 0) {
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02004410 view.removeAllViews(com.android.internal.R.id.media_actions);
Dan Sandler842dd772014-05-15 09:36:47 -04004411 for (int i = 0; i < N; i++) {
4412 if (i >= numActions) {
4413 throw new IllegalArgumentException(String.format(
4414 "setShowActionsInCompactView: action %d out of bounds (max %d)",
4415 i, numActions - 1));
4416 }
4417
4418 final Action action = mBuilder.mActions.get(mActionsToShowInCompact[i]);
Selim Cinek5bf069a2015-11-10 19:14:27 -05004419 final RemoteViews button = generateMediaActionButton(action,
4420 mBuilder.resolveColor());
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02004421 view.addView(com.android.internal.R.id.media_actions, button);
Dan Sandler842dd772014-05-15 09:36:47 -04004422 }
4423 }
Selim Cinekfdc738f2016-01-27 20:04:27 -08004424 handleImage(view);
4425 // handle the content margin
Selim Cinek247fa012016-02-18 09:50:48 -08004426 int endMargin = mBuilder.mContext.getResources().getDimensionPixelSize(
4427 R.dimen.notification_content_margin_end);;
Selim Cinekfdc738f2016-01-27 20:04:27 -08004428 if (mBuilder.mN.mLargeIcon != null) {
Selim Cinek247fa012016-02-18 09:50:48 -08004429 endMargin += mBuilder.mContext.getResources().getDimensionPixelSize(
4430 R.dimen.notification_content_picture_margin);
Selim Cinekfdc738f2016-01-27 20:04:27 -08004431 }
4432 view.setViewLayoutMarginEnd(R.id.notification_main_column, endMargin);
Dan Sandler842dd772014-05-15 09:36:47 -04004433 return view;
4434 }
4435
4436 private RemoteViews makeMediaBigContentView() {
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02004437 final int actionCount = Math.min(mBuilder.mActions.size(), MAX_MEDIA_BUTTONS);
Selim Cinekcc10bfb2016-02-10 16:24:21 -08004438 // Dont add an expanded view if there is no more content to be revealed
4439 int actionsInCompact = mActionsToShowInCompact == null
4440 ? 0
4441 : Math.min(mActionsToShowInCompact.length, MAX_MEDIA_BUTTONS_IN_COMPACT);
4442 if (mBuilder.mN.mLargeIcon == null && actionCount <= actionsInCompact) {
4443 return null;
4444 }
Selim Cinek5bf069a2015-11-10 19:14:27 -05004445 RemoteViews big = mBuilder.applyStandardTemplate(
4446 R.layout.notification_template_material_big_media,
4447 false);
Dan Sandler842dd772014-05-15 09:36:47 -04004448
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02004449 if (actionCount > 0) {
4450 big.removeAllViews(com.android.internal.R.id.media_actions);
4451 for (int i = 0; i < actionCount; i++) {
Selim Cinek5bf069a2015-11-10 19:14:27 -05004452 final RemoteViews button = generateMediaActionButton(mBuilder.mActions.get(i),
4453 mBuilder.resolveColor());
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02004454 big.addView(com.android.internal.R.id.media_actions, button);
Dan Sandler842dd772014-05-15 09:36:47 -04004455 }
4456 }
Selim Cinek5bf069a2015-11-10 19:14:27 -05004457 handleImage(big);
Dan Sandler842dd772014-05-15 09:36:47 -04004458 return big;
4459 }
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02004460
Selim Cinek5bf069a2015-11-10 19:14:27 -05004461 private void handleImage(RemoteViews contentView) {
4462 if (mBuilder.mN.mLargeIcon != null) {
4463 contentView.setViewLayoutMarginEnd(R.id.line1, 0);
Selim Cinekc848c3a2016-01-13 15:27:30 -08004464 contentView.setViewLayoutMarginEnd(R.id.text, 0);
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02004465 }
4466 }
4467
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02004468 /**
4469 * @hide
4470 */
4471 @Override
4472 protected boolean hasProgress() {
4473 return false;
4474 }
Dan Sandler842dd772014-05-15 09:36:47 -04004475 }
Griff Hazen61a9e862014-05-22 16:05:19 -07004476
Selim Cinek593610c2016-02-16 18:42:57 -08004477 /**
4478 * Notification style for custom views that are decorated by the system
4479 *
4480 * <p>Instead of providing a notification that is completely custom, a developer can set this
4481 * style and still obtain system decorations like the notification header with the expand
4482 * affordance and actions.
4483 *
4484 * <p>Use {@link android.app.Notification.Builder#setCustomContentView(RemoteViews)},
4485 * {@link android.app.Notification.Builder#setCustomBigContentView(RemoteViews)} and
4486 * {@link android.app.Notification.Builder#setCustomHeadsUpContentView(RemoteViews)} to set the
4487 * corresponding custom views to display.
4488 *
4489 * To use this style with your Notification, feed it to
4490 * {@link Notification.Builder#setStyle(android.app.Notification.Style)} like so:
4491 * <pre class="prettyprint">
4492 * Notification noti = new Notification.Builder()
4493 * .setSmallIcon(R.drawable.ic_stat_player)
4494 * .setLargeIcon(albumArtBitmap))
4495 * .setCustomContentView(contentView);
4496 * .setStyle(<b>new Notification.DecoratedCustomViewStyle()</b>)
4497 * .build();
4498 * </pre>
4499 */
4500 public static class DecoratedCustomViewStyle extends Style {
4501
4502 public DecoratedCustomViewStyle() {
4503 }
4504
4505 public DecoratedCustomViewStyle(Builder builder) {
4506 setBuilder(builder);
4507 }
4508
4509 /**
4510 * @hide
4511 */
4512 public boolean displayCustomViewInline() {
4513 return true;
4514 }
4515
4516 /**
4517 * @hide
4518 */
4519 @Override
4520 public RemoteViews makeContentView() {
4521 return makeStandardTemplateWithCustomContent(mBuilder.mN.contentView);
4522 }
4523
4524 /**
4525 * @hide
4526 */
4527 @Override
4528 public RemoteViews makeBigContentView() {
4529 return makeDecoratedBigContentView();
4530 }
4531
4532 /**
4533 * @hide
4534 */
4535 @Override
4536 public RemoteViews makeHeadsUpContentView() {
4537 return makeDecoratedHeadsUpContentView();
4538 }
4539
Selim Cinek593610c2016-02-16 18:42:57 -08004540 private RemoteViews makeDecoratedHeadsUpContentView() {
4541 RemoteViews headsUpContentView = mBuilder.mN.headsUpContentView == null
4542 ? mBuilder.mN.contentView
4543 : mBuilder.mN.headsUpContentView;
4544 if (mBuilder.mActions.size() == 0) {
4545 return makeStandardTemplateWithCustomContent(headsUpContentView);
4546 }
4547 RemoteViews remoteViews = mBuilder.applyStandardTemplateWithActions(
4548 mBuilder.getBigBaseLayoutResource());
Selim Cinek247fa012016-02-18 09:50:48 -08004549 buildIntoRemoteViewContent(remoteViews, headsUpContentView);
Selim Cinek593610c2016-02-16 18:42:57 -08004550 return remoteViews;
4551 }
4552
Selim Cinek593610c2016-02-16 18:42:57 -08004553 private RemoteViews makeStandardTemplateWithCustomContent(RemoteViews customContent) {
4554 RemoteViews remoteViews = mBuilder.applyStandardTemplate(
4555 mBuilder.getBaseLayoutResource());
Selim Cinek247fa012016-02-18 09:50:48 -08004556 buildIntoRemoteViewContent(remoteViews, customContent);
Selim Cinek593610c2016-02-16 18:42:57 -08004557 return remoteViews;
4558 }
4559
Selim Cinek593610c2016-02-16 18:42:57 -08004560 private RemoteViews makeDecoratedBigContentView() {
4561 RemoteViews bigContentView = mBuilder.mN.bigContentView == null
4562 ? mBuilder.mN.contentView
4563 : mBuilder.mN.bigContentView;
4564 if (mBuilder.mActions.size() == 0) {
4565 return makeStandardTemplateWithCustomContent(bigContentView);
4566 }
4567 RemoteViews remoteViews = mBuilder.applyStandardTemplateWithActions(
4568 mBuilder.getBigBaseLayoutResource());
Selim Cinek247fa012016-02-18 09:50:48 -08004569 buildIntoRemoteViewContent(remoteViews, bigContentView);
Selim Cinek593610c2016-02-16 18:42:57 -08004570 return remoteViews;
4571 }
Selim Cinek247fa012016-02-18 09:50:48 -08004572
4573 private void buildIntoRemoteViewContent(RemoteViews remoteViews,
4574 RemoteViews customContent) {
4575 remoteViews.removeAllViews(R.id.notification_main_column);
Adrian Roos5081c0d2016-02-26 16:04:19 -08004576 // Need to clone customContent before adding, because otherwise it can no longer be
4577 // parceled independently of remoteViews.
4578 if (customContent != null) {
4579 customContent = customContent.clone();
4580 }
Selim Cinek247fa012016-02-18 09:50:48 -08004581 remoteViews.addView(R.id.notification_main_column, customContent);
4582 // also update the end margin if there is an image
4583 int endMargin = mBuilder.mContext.getResources().getDimensionPixelSize(
4584 R.dimen.notification_content_margin_end);
4585 if (mBuilder.mN.mLargeIcon != null) {
4586 endMargin += mBuilder.mContext.getResources().getDimensionPixelSize(
4587 R.dimen.notification_content_picture_margin);
4588 }
4589 remoteViews.setViewLayoutMarginEnd(R.id.notification_main_column, endMargin);
4590 }
Selim Cinek593610c2016-02-16 18:42:57 -08004591 }
4592
Selim Cinek03eb3b72016-02-18 10:39:45 -08004593 /**
4594 * Notification style for media custom views that are decorated by the system
4595 *
4596 * <p>Instead of providing a media notification that is completely custom, a developer can set
4597 * this style and still obtain system decorations like the notification header with the expand
4598 * affordance and actions.
4599 *
4600 * <p>Use {@link android.app.Notification.Builder#setCustomContentView(RemoteViews)},
4601 * {@link android.app.Notification.Builder#setCustomBigContentView(RemoteViews)} and
4602 * {@link android.app.Notification.Builder#setCustomHeadsUpContentView(RemoteViews)} to set the
4603 * corresponding custom views to display.
4604 *
4605 * To use this style with your Notification, feed it to
4606 * {@link Notification.Builder#setStyle(android.app.Notification.Style)} like so:
4607 * <pre class="prettyprint">
4608 * Notification noti = new Notification.Builder()
4609 * .setSmallIcon(R.drawable.ic_stat_player)
4610 * .setLargeIcon(albumArtBitmap))
4611 * .setCustomContentView(contentView);
4612 * .setStyle(<b>new Notification.DecoratedMediaCustomViewStyle()</b>
4613 * .setMediaSession(mySession))
4614 * .build();
4615 * </pre>
4616 *
4617 * @see android.app.Notification.DecoratedCustomViewStyle
4618 * @see android.app.Notification.MediaStyle
4619 */
4620 public static class DecoratedMediaCustomViewStyle extends MediaStyle {
4621
4622 public DecoratedMediaCustomViewStyle() {
4623 }
4624
4625 public DecoratedMediaCustomViewStyle(Builder builder) {
4626 setBuilder(builder);
4627 }
4628
4629 /**
4630 * @hide
4631 */
4632 public boolean displayCustomViewInline() {
4633 return true;
4634 }
4635
4636 /**
4637 * @hide
4638 */
4639 @Override
4640 public RemoteViews makeContentView() {
4641 RemoteViews remoteViews = super.makeContentView();
4642 return buildIntoRemoteView(remoteViews, R.id.notification_content_container,
4643 mBuilder.mN.contentView);
4644 }
4645
4646 /**
4647 * @hide
4648 */
4649 @Override
4650 public RemoteViews makeBigContentView() {
4651 RemoteViews customRemoteView = mBuilder.mN.bigContentView != null
4652 ? mBuilder.mN.bigContentView
4653 : mBuilder.mN.contentView;
4654 return makeBigContentViewWithCustomContent(customRemoteView);
4655 }
4656
4657 private RemoteViews makeBigContentViewWithCustomContent(RemoteViews customRemoteView) {
4658 RemoteViews remoteViews = super.makeBigContentView();
4659 if (remoteViews != null) {
4660 return buildIntoRemoteView(remoteViews, R.id.notification_main_column,
4661 customRemoteView);
4662 } else if (customRemoteView != mBuilder.mN.contentView){
4663 remoteViews = super.makeContentView();
4664 return buildIntoRemoteView(remoteViews, R.id.notification_content_container,
4665 customRemoteView);
4666 } else {
4667 return null;
4668 }
4669 }
4670
4671 /**
4672 * @hide
4673 */
4674 @Override
4675 public RemoteViews makeHeadsUpContentView() {
4676 RemoteViews customRemoteView = mBuilder.mN.headsUpContentView != null
4677 ? mBuilder.mN.headsUpContentView
4678 : mBuilder.mN.contentView;
4679 return makeBigContentViewWithCustomContent(customRemoteView);
4680 }
4681
4682 private RemoteViews buildIntoRemoteView(RemoteViews remoteViews, int id,
4683 RemoteViews customContent) {
4684 remoteViews.removeAllViews(id);
Adrian Roos5081c0d2016-02-26 16:04:19 -08004685 // Need to clone customContent before adding, because otherwise it can no longer be
4686 // parceled independently of remoteViews.
4687 if (customContent != null) {
4688 customContent = customContent.clone();
4689 }
Selim Cinek03eb3b72016-02-18 10:39:45 -08004690 remoteViews.addView(id, customContent);
4691 return remoteViews;
4692 }
4693 }
4694
Christoph Studer4600f9b2014-07-22 22:44:43 +02004695 // When adding a new Style subclass here, don't forget to update
4696 // Builder.getNotificationStyleClass.
4697
Griff Hazen61a9e862014-05-22 16:05:19 -07004698 /**
4699 * Extender interface for use with {@link Builder#extend}. Extenders may be used to add
4700 * metadata or change options on a notification builder.
4701 */
4702 public interface Extender {
4703 /**
4704 * Apply this extender to a notification builder.
4705 * @param builder the builder to be modified.
4706 * @return the build object for chaining.
4707 */
4708 public Builder extend(Builder builder);
4709 }
4710
4711 /**
4712 * Helper class to add wearable extensions to notifications.
4713 * <p class="note"> See
4714 * <a href="{@docRoot}wear/notifications/creating.html">Creating Notifications
4715 * for Android Wear</a> for more information on how to use this class.
4716 * <p>
4717 * To create a notification with wearable extensions:
4718 * <ol>
4719 * <li>Create a {@link android.app.Notification.Builder}, setting any desired
4720 * properties.
4721 * <li>Create a {@link android.app.Notification.WearableExtender}.
4722 * <li>Set wearable-specific properties using the
4723 * {@code add} and {@code set} methods of {@link android.app.Notification.WearableExtender}.
4724 * <li>Call {@link android.app.Notification.Builder#extend} to apply the extensions to a
4725 * notification.
4726 * <li>Post the notification to the notification system with the
4727 * {@code NotificationManager.notify(...)} methods.
4728 * </ol>
4729 *
4730 * <pre class="prettyprint">
4731 * Notification notif = new Notification.Builder(mContext)
4732 * .setContentTitle(&quot;New mail from &quot; + sender.toString())
4733 * .setContentText(subject)
4734 * .setSmallIcon(R.drawable.new_mail)
4735 * .extend(new Notification.WearableExtender()
4736 * .setContentIcon(R.drawable.new_mail))
4737 * .build();
4738 * NotificationManager notificationManger =
4739 * (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
4740 * notificationManger.notify(0, notif);</pre>
4741 *
4742 * <p>Wearable extensions can be accessed on an existing notification by using the
4743 * {@code WearableExtender(Notification)} constructor,
4744 * and then using the {@code get} methods to access values.
4745 *
4746 * <pre class="prettyprint">
4747 * Notification.WearableExtender wearableExtender = new Notification.WearableExtender(
4748 * notification);
Griff Hazen14f57992014-05-26 09:07:14 -07004749 * List&lt;Notification&gt; pages = wearableExtender.getPages();</pre>
Griff Hazen61a9e862014-05-22 16:05:19 -07004750 */
4751 public static final class WearableExtender implements Extender {
4752 /**
4753 * Sentinel value for an action index that is unset.
4754 */
4755 public static final int UNSET_ACTION_INDEX = -1;
4756
4757 /**
4758 * Size value for use with {@link #setCustomSizePreset} to show this notification with
4759 * default sizing.
4760 * <p>For custom display notifications created using {@link #setDisplayIntent},
Paul Soulosaa4f4bf2015-08-04 11:59:45 -07004761 * the default is {@link #SIZE_MEDIUM}. All other notifications size automatically based
Griff Hazen61a9e862014-05-22 16:05:19 -07004762 * on their content.
4763 */
4764 public static final int SIZE_DEFAULT = 0;
4765
4766 /**
4767 * Size value for use with {@link #setCustomSizePreset} to show this notification
4768 * with an extra 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_XSMALL = 1;
4773
4774 /**
4775 * Size value for use with {@link #setCustomSizePreset} to show this notification
4776 * with a small size.
4777 * <p>This value is only applicable for custom display notifications created using
4778 * {@link #setDisplayIntent}.
4779 */
4780 public static final int SIZE_SMALL = 2;
4781
4782 /**
4783 * Size value for use with {@link #setCustomSizePreset} to show this notification
4784 * with a medium size.
4785 * <p>This value is only applicable for custom display notifications created using
4786 * {@link #setDisplayIntent}.
4787 */
4788 public static final int SIZE_MEDIUM = 3;
4789
4790 /**
4791 * Size value for use with {@link #setCustomSizePreset} to show this notification
4792 * with a large size.
4793 * <p>This value is only applicable for custom display notifications created using
4794 * {@link #setDisplayIntent}.
4795 */
4796 public static final int SIZE_LARGE = 4;
4797
Griff Hazend5f11f92014-05-27 15:40:09 -07004798 /**
4799 * Size value for use with {@link #setCustomSizePreset} to show this notification
4800 * full screen.
4801 * <p>This value is only applicable for custom display notifications created using
4802 * {@link #setDisplayIntent}.
4803 */
4804 public static final int SIZE_FULL_SCREEN = 5;
4805
Griff Hazen5f2edfc2014-09-29 16:28:44 -07004806 /**
4807 * Sentinel value for use with {@link #setHintScreenTimeout} to keep the screen on for a
4808 * short amount of time when this notification is displayed on the screen. This
4809 * is the default value.
4810 */
4811 public static final int SCREEN_TIMEOUT_SHORT = 0;
4812
4813 /**
4814 * Sentinel value for use with {@link #setHintScreenTimeout} to keep the screen on
4815 * for a longer amount of time when this notification is displayed on the screen.
4816 */
4817 public static final int SCREEN_TIMEOUT_LONG = -1;
4818
Griff Hazen61a9e862014-05-22 16:05:19 -07004819 /** Notification extra which contains wearable extensions */
4820 private static final String EXTRA_WEARABLE_EXTENSIONS = "android.wearable.EXTENSIONS";
4821
Pete Gastaf6781d2014-10-07 15:17:05 -04004822 // Keys within EXTRA_WEARABLE_EXTENSIONS for wearable options.
Griff Hazen61a9e862014-05-22 16:05:19 -07004823 private static final String KEY_ACTIONS = "actions";
4824 private static final String KEY_FLAGS = "flags";
4825 private static final String KEY_DISPLAY_INTENT = "displayIntent";
4826 private static final String KEY_PAGES = "pages";
4827 private static final String KEY_BACKGROUND = "background";
4828 private static final String KEY_CONTENT_ICON = "contentIcon";
4829 private static final String KEY_CONTENT_ICON_GRAVITY = "contentIconGravity";
4830 private static final String KEY_CONTENT_ACTION_INDEX = "contentActionIndex";
4831 private static final String KEY_CUSTOM_SIZE_PRESET = "customSizePreset";
4832 private static final String KEY_CUSTOM_CONTENT_HEIGHT = "customContentHeight";
4833 private static final String KEY_GRAVITY = "gravity";
Griff Hazen5f2edfc2014-09-29 16:28:44 -07004834 private static final String KEY_HINT_SCREEN_TIMEOUT = "hintScreenTimeout";
Griff Hazen61a9e862014-05-22 16:05:19 -07004835
4836 // Flags bitwise-ored to mFlags
4837 private static final int FLAG_CONTENT_INTENT_AVAILABLE_OFFLINE = 0x1;
4838 private static final int FLAG_HINT_HIDE_ICON = 1 << 1;
4839 private static final int FLAG_HINT_SHOW_BACKGROUND_ONLY = 1 << 2;
4840 private static final int FLAG_START_SCROLL_BOTTOM = 1 << 3;
Griff Hazen5f2edfc2014-09-29 16:28:44 -07004841 private static final int FLAG_HINT_AVOID_BACKGROUND_CLIPPING = 1 << 4;
Griff Hazen61a9e862014-05-22 16:05:19 -07004842
4843 // Default value for flags integer
4844 private static final int DEFAULT_FLAGS = FLAG_CONTENT_INTENT_AVAILABLE_OFFLINE;
4845
4846 private static final int DEFAULT_CONTENT_ICON_GRAVITY = Gravity.END;
4847 private static final int DEFAULT_GRAVITY = Gravity.BOTTOM;
4848
4849 private ArrayList<Action> mActions = new ArrayList<Action>();
4850 private int mFlags = DEFAULT_FLAGS;
4851 private PendingIntent mDisplayIntent;
4852 private ArrayList<Notification> mPages = new ArrayList<Notification>();
4853 private Bitmap mBackground;
4854 private int mContentIcon;
4855 private int mContentIconGravity = DEFAULT_CONTENT_ICON_GRAVITY;
4856 private int mContentActionIndex = UNSET_ACTION_INDEX;
4857 private int mCustomSizePreset = SIZE_DEFAULT;
4858 private int mCustomContentHeight;
4859 private int mGravity = DEFAULT_GRAVITY;
Griff Hazen5f2edfc2014-09-29 16:28:44 -07004860 private int mHintScreenTimeout;
Griff Hazen61a9e862014-05-22 16:05:19 -07004861
4862 /**
4863 * Create a {@link android.app.Notification.WearableExtender} with default
4864 * options.
4865 */
4866 public WearableExtender() {
4867 }
4868
4869 public WearableExtender(Notification notif) {
4870 Bundle wearableBundle = notif.extras.getBundle(EXTRA_WEARABLE_EXTENSIONS);
4871 if (wearableBundle != null) {
4872 List<Action> actions = wearableBundle.getParcelableArrayList(KEY_ACTIONS);
4873 if (actions != null) {
4874 mActions.addAll(actions);
4875 }
4876
4877 mFlags = wearableBundle.getInt(KEY_FLAGS, DEFAULT_FLAGS);
4878 mDisplayIntent = wearableBundle.getParcelable(KEY_DISPLAY_INTENT);
4879
4880 Notification[] pages = getNotificationArrayFromBundle(
4881 wearableBundle, KEY_PAGES);
4882 if (pages != null) {
4883 Collections.addAll(mPages, pages);
4884 }
4885
4886 mBackground = wearableBundle.getParcelable(KEY_BACKGROUND);
4887 mContentIcon = wearableBundle.getInt(KEY_CONTENT_ICON);
4888 mContentIconGravity = wearableBundle.getInt(KEY_CONTENT_ICON_GRAVITY,
4889 DEFAULT_CONTENT_ICON_GRAVITY);
4890 mContentActionIndex = wearableBundle.getInt(KEY_CONTENT_ACTION_INDEX,
4891 UNSET_ACTION_INDEX);
4892 mCustomSizePreset = wearableBundle.getInt(KEY_CUSTOM_SIZE_PRESET,
4893 SIZE_DEFAULT);
4894 mCustomContentHeight = wearableBundle.getInt(KEY_CUSTOM_CONTENT_HEIGHT);
4895 mGravity = wearableBundle.getInt(KEY_GRAVITY, DEFAULT_GRAVITY);
Griff Hazen5f2edfc2014-09-29 16:28:44 -07004896 mHintScreenTimeout = wearableBundle.getInt(KEY_HINT_SCREEN_TIMEOUT);
Griff Hazen61a9e862014-05-22 16:05:19 -07004897 }
4898 }
4899
4900 /**
4901 * Apply wearable extensions to a notification that is being built. This is typically
4902 * called by the {@link android.app.Notification.Builder#extend} method of
4903 * {@link android.app.Notification.Builder}.
4904 */
4905 @Override
4906 public Notification.Builder extend(Notification.Builder builder) {
4907 Bundle wearableBundle = new Bundle();
4908
4909 if (!mActions.isEmpty()) {
4910 wearableBundle.putParcelableArrayList(KEY_ACTIONS, mActions);
4911 }
4912 if (mFlags != DEFAULT_FLAGS) {
4913 wearableBundle.putInt(KEY_FLAGS, mFlags);
4914 }
4915 if (mDisplayIntent != null) {
4916 wearableBundle.putParcelable(KEY_DISPLAY_INTENT, mDisplayIntent);
4917 }
4918 if (!mPages.isEmpty()) {
4919 wearableBundle.putParcelableArray(KEY_PAGES, mPages.toArray(
4920 new Notification[mPages.size()]));
4921 }
4922 if (mBackground != null) {
4923 wearableBundle.putParcelable(KEY_BACKGROUND, mBackground);
4924 }
4925 if (mContentIcon != 0) {
4926 wearableBundle.putInt(KEY_CONTENT_ICON, mContentIcon);
4927 }
4928 if (mContentIconGravity != DEFAULT_CONTENT_ICON_GRAVITY) {
4929 wearableBundle.putInt(KEY_CONTENT_ICON_GRAVITY, mContentIconGravity);
4930 }
4931 if (mContentActionIndex != UNSET_ACTION_INDEX) {
4932 wearableBundle.putInt(KEY_CONTENT_ACTION_INDEX,
4933 mContentActionIndex);
4934 }
4935 if (mCustomSizePreset != SIZE_DEFAULT) {
4936 wearableBundle.putInt(KEY_CUSTOM_SIZE_PRESET, mCustomSizePreset);
4937 }
4938 if (mCustomContentHeight != 0) {
4939 wearableBundle.putInt(KEY_CUSTOM_CONTENT_HEIGHT, mCustomContentHeight);
4940 }
4941 if (mGravity != DEFAULT_GRAVITY) {
4942 wearableBundle.putInt(KEY_GRAVITY, mGravity);
4943 }
Griff Hazen5f2edfc2014-09-29 16:28:44 -07004944 if (mHintScreenTimeout != 0) {
4945 wearableBundle.putInt(KEY_HINT_SCREEN_TIMEOUT, mHintScreenTimeout);
4946 }
Griff Hazen61a9e862014-05-22 16:05:19 -07004947
4948 builder.getExtras().putBundle(EXTRA_WEARABLE_EXTENSIONS, wearableBundle);
4949 return builder;
4950 }
4951
4952 @Override
4953 public WearableExtender clone() {
4954 WearableExtender that = new WearableExtender();
4955 that.mActions = new ArrayList<Action>(this.mActions);
4956 that.mFlags = this.mFlags;
4957 that.mDisplayIntent = this.mDisplayIntent;
4958 that.mPages = new ArrayList<Notification>(this.mPages);
4959 that.mBackground = this.mBackground;
4960 that.mContentIcon = this.mContentIcon;
4961 that.mContentIconGravity = this.mContentIconGravity;
4962 that.mContentActionIndex = this.mContentActionIndex;
4963 that.mCustomSizePreset = this.mCustomSizePreset;
4964 that.mCustomContentHeight = this.mCustomContentHeight;
4965 that.mGravity = this.mGravity;
Griff Hazen5f2edfc2014-09-29 16:28:44 -07004966 that.mHintScreenTimeout = this.mHintScreenTimeout;
Griff Hazen61a9e862014-05-22 16:05:19 -07004967 return that;
4968 }
4969
4970 /**
4971 * Add a wearable action to this notification.
4972 *
4973 * <p>When wearable actions are added using this method, the set of actions that
4974 * show on a wearable device splits from devices that only show actions added
4975 * using {@link android.app.Notification.Builder#addAction}. This allows for customization
4976 * of which actions display on different devices.
4977 *
4978 * @param action the action to add to this notification
4979 * @return this object for method chaining
4980 * @see android.app.Notification.Action
4981 */
4982 public WearableExtender addAction(Action action) {
4983 mActions.add(action);
4984 return this;
4985 }
4986
4987 /**
4988 * Adds wearable actions to this notification.
4989 *
4990 * <p>When wearable actions are added using this method, the set of actions that
4991 * show on a wearable device splits from devices that only show actions added
4992 * using {@link android.app.Notification.Builder#addAction}. This allows for customization
4993 * of which actions display on different devices.
4994 *
4995 * @param actions the actions to add to this notification
4996 * @return this object for method chaining
4997 * @see android.app.Notification.Action
4998 */
4999 public WearableExtender addActions(List<Action> actions) {
5000 mActions.addAll(actions);
5001 return this;
5002 }
5003
5004 /**
5005 * Clear all wearable actions present on this builder.
5006 * @return this object for method chaining.
5007 * @see #addAction
5008 */
5009 public WearableExtender clearActions() {
5010 mActions.clear();
5011 return this;
5012 }
5013
5014 /**
5015 * Get the wearable actions present on this notification.
5016 */
5017 public List<Action> getActions() {
5018 return mActions;
5019 }
5020
5021 /**
5022 * Set an intent to launch inside of an activity view when displaying
Griff Hazen14f57992014-05-26 09:07:14 -07005023 * this notification. The {@link PendingIntent} provided should be for an activity.
5024 *
5025 * <pre class="prettyprint">
5026 * Intent displayIntent = new Intent(context, MyDisplayActivity.class);
5027 * PendingIntent displayPendingIntent = PendingIntent.getActivity(context,
5028 * 0, displayIntent, PendingIntent.FLAG_UPDATE_CURRENT);
5029 * Notification notif = new Notification.Builder(context)
5030 * .extend(new Notification.WearableExtender()
5031 * .setDisplayIntent(displayPendingIntent)
5032 * .setCustomSizePreset(Notification.WearableExtender.SIZE_MEDIUM))
5033 * .build();</pre>
5034 *
5035 * <p>The activity to launch needs to allow embedding, must be exported, and
Griff Hazen831ca9d2014-06-17 00:38:38 -07005036 * should have an empty task affinity. It is also recommended to use the device
5037 * default light theme.
Griff Hazen14f57992014-05-26 09:07:14 -07005038 *
5039 * <p>Example AndroidManifest.xml entry:
5040 * <pre class="prettyprint">
5041 * &lt;activity android:name=&quot;com.example.MyDisplayActivity&quot;
5042 * android:exported=&quot;true&quot;
5043 * android:allowEmbedded=&quot;true&quot;
Griff Hazen831ca9d2014-06-17 00:38:38 -07005044 * android:taskAffinity=&quot;&quot;
5045 * android:theme=&quot;@android:style/Theme.DeviceDefault.Light&quot; /&gt;</pre>
Griff Hazen61a9e862014-05-22 16:05:19 -07005046 *
5047 * @param intent the {@link PendingIntent} for an activity
5048 * @return this object for method chaining
5049 * @see android.app.Notification.WearableExtender#getDisplayIntent
5050 */
5051 public WearableExtender setDisplayIntent(PendingIntent intent) {
5052 mDisplayIntent = intent;
5053 return this;
5054 }
5055
5056 /**
5057 * Get the intent to launch inside of an activity view when displaying this
5058 * notification. This {@code PendingIntent} should be for an activity.
5059 */
5060 public PendingIntent getDisplayIntent() {
5061 return mDisplayIntent;
5062 }
5063
5064 /**
5065 * Add an additional page of content to display with this notification. The current
5066 * notification forms the first page, and pages added using this function form
5067 * subsequent pages. This field can be used to separate a notification into multiple
5068 * sections.
5069 *
5070 * @param page the notification to add as another page
5071 * @return this object for method chaining
5072 * @see android.app.Notification.WearableExtender#getPages
5073 */
5074 public WearableExtender addPage(Notification page) {
5075 mPages.add(page);
5076 return this;
5077 }
5078
5079 /**
5080 * Add additional pages of content to display with this notification. The current
5081 * notification forms the first page, and pages added using this function form
5082 * subsequent pages. This field can be used to separate a notification into multiple
5083 * sections.
5084 *
5085 * @param pages a list of notifications
5086 * @return this object for method chaining
5087 * @see android.app.Notification.WearableExtender#getPages
5088 */
5089 public WearableExtender addPages(List<Notification> pages) {
5090 mPages.addAll(pages);
5091 return this;
5092 }
5093
5094 /**
5095 * Clear all additional pages present on this builder.
5096 * @return this object for method chaining.
5097 * @see #addPage
5098 */
5099 public WearableExtender clearPages() {
5100 mPages.clear();
5101 return this;
5102 }
5103
5104 /**
5105 * Get the array of additional pages of content for displaying this notification. The
5106 * current notification forms the first page, and elements within this array form
5107 * subsequent pages. This field can be used to separate a notification into multiple
5108 * sections.
5109 * @return the pages for this notification
5110 */
5111 public List<Notification> getPages() {
5112 return mPages;
5113 }
5114
5115 /**
5116 * Set a background image to be displayed behind the notification content.
5117 * Contrary to the {@link android.app.Notification.BigPictureStyle}, this background
5118 * will work with any notification style.
5119 *
5120 * @param background the background bitmap
5121 * @return this object for method chaining
5122 * @see android.app.Notification.WearableExtender#getBackground
5123 */
5124 public WearableExtender setBackground(Bitmap background) {
5125 mBackground = background;
5126 return this;
5127 }
5128
5129 /**
5130 * Get a background image to be displayed behind the notification content.
5131 * Contrary to the {@link android.app.Notification.BigPictureStyle}, this background
5132 * will work with any notification style.
5133 *
5134 * @return the background image
5135 * @see android.app.Notification.WearableExtender#setBackground
5136 */
5137 public Bitmap getBackground() {
5138 return mBackground;
5139 }
5140
5141 /**
5142 * Set an icon that goes with the content of this notification.
5143 */
5144 public WearableExtender setContentIcon(int icon) {
5145 mContentIcon = icon;
5146 return this;
5147 }
5148
5149 /**
5150 * Get an icon that goes with the content of this notification.
5151 */
5152 public int getContentIcon() {
5153 return mContentIcon;
5154 }
5155
5156 /**
5157 * Set the gravity that the content icon should have within the notification display.
5158 * Supported values include {@link android.view.Gravity#START} and
5159 * {@link android.view.Gravity#END}. The default value is {@link android.view.Gravity#END}.
5160 * @see #setContentIcon
5161 */
5162 public WearableExtender setContentIconGravity(int contentIconGravity) {
5163 mContentIconGravity = contentIconGravity;
5164 return this;
5165 }
5166
5167 /**
5168 * Get the gravity that the content icon should have within the notification display.
5169 * Supported values include {@link android.view.Gravity#START} and
5170 * {@link android.view.Gravity#END}. The default value is {@link android.view.Gravity#END}.
5171 * @see #getContentIcon
5172 */
5173 public int getContentIconGravity() {
5174 return mContentIconGravity;
5175 }
5176
5177 /**
5178 * Set an action from this notification's actions to be clickable with the content of
Griff Hazen14f57992014-05-26 09:07:14 -07005179 * this notification. This action will no longer display separately from the
5180 * notification's content.
5181 *
Griff Hazenca48d352014-05-28 22:37:13 -07005182 * <p>For notifications with multiple pages, child pages can also have content actions
Griff Hazen14f57992014-05-26 09:07:14 -07005183 * set, although the list of available actions comes from the main notification and not
5184 * from the child page's notification.
5185 *
5186 * @param actionIndex The index of the action to hoist onto the current notification page.
5187 * If wearable actions were added to the main notification, this index
5188 * will apply to that list, otherwise it will apply to the regular
5189 * actions list.
Griff Hazen61a9e862014-05-22 16:05:19 -07005190 */
5191 public WearableExtender setContentAction(int actionIndex) {
5192 mContentActionIndex = actionIndex;
5193 return this;
5194 }
5195
5196 /**
Griff Hazenca48d352014-05-28 22:37:13 -07005197 * Get the index of the notification action, if any, that was specified as being clickable
5198 * with the content of this notification. This action will no longer display separately
Griff Hazen14f57992014-05-26 09:07:14 -07005199 * from the notification's content.
Griff Hazen61a9e862014-05-22 16:05:19 -07005200 *
Griff Hazenca48d352014-05-28 22:37:13 -07005201 * <p>For notifications with multiple pages, child pages can also have content actions
Griff Hazen14f57992014-05-26 09:07:14 -07005202 * set, although the list of available actions comes from the main notification and not
5203 * from the child page's notification.
5204 *
5205 * <p>If wearable specific actions were added to the main notification, this index will
5206 * apply to that list, otherwise it will apply to the regular actions list.
Griff Hazenca48d352014-05-28 22:37:13 -07005207 *
5208 * @return the action index or {@link #UNSET_ACTION_INDEX} if no action was selected.
Griff Hazen61a9e862014-05-22 16:05:19 -07005209 */
5210 public int getContentAction() {
5211 return mContentActionIndex;
5212 }
5213
5214 /**
5215 * Set the gravity that this notification should have within the available viewport space.
5216 * Supported values include {@link android.view.Gravity#TOP},
5217 * {@link android.view.Gravity#CENTER_VERTICAL} and {@link android.view.Gravity#BOTTOM}.
5218 * The default value is {@link android.view.Gravity#BOTTOM}.
5219 */
5220 public WearableExtender setGravity(int gravity) {
5221 mGravity = gravity;
5222 return this;
5223 }
5224
5225 /**
5226 * Get the gravity that this notification should have within the available viewport space.
5227 * Supported values include {@link android.view.Gravity#TOP},
5228 * {@link android.view.Gravity#CENTER_VERTICAL} and {@link android.view.Gravity#BOTTOM}.
5229 * The default value is {@link android.view.Gravity#BOTTOM}.
5230 */
5231 public int getGravity() {
5232 return mGravity;
5233 }
5234
5235 /**
5236 * Set the custom size preset for the display of this notification out of the available
5237 * presets found in {@link android.app.Notification.WearableExtender}, e.g.
5238 * {@link #SIZE_LARGE}.
5239 * <p>Some custom size presets are only applicable for custom display notifications created
5240 * using {@link android.app.Notification.WearableExtender#setDisplayIntent}. Check the
5241 * documentation for the preset in question. See also
5242 * {@link #setCustomContentHeight} and {@link #getCustomSizePreset}.
5243 */
5244 public WearableExtender setCustomSizePreset(int sizePreset) {
5245 mCustomSizePreset = sizePreset;
5246 return this;
5247 }
5248
5249 /**
5250 * Get the custom size preset for the display of this notification out of the available
5251 * presets found in {@link android.app.Notification.WearableExtender}, e.g.
5252 * {@link #SIZE_LARGE}.
5253 * <p>Some custom size presets are only applicable for custom display notifications created
5254 * using {@link #setDisplayIntent}. Check the documentation for the preset in question.
5255 * See also {@link #setCustomContentHeight} and {@link #setCustomSizePreset}.
5256 */
5257 public int getCustomSizePreset() {
5258 return mCustomSizePreset;
5259 }
5260
5261 /**
5262 * Set the custom height in pixels for the display of this notification's content.
5263 * <p>This option is only available for custom display notifications created
5264 * using {@link android.app.Notification.WearableExtender#setDisplayIntent}. See also
5265 * {@link android.app.Notification.WearableExtender#setCustomSizePreset} and
5266 * {@link #getCustomContentHeight}.
5267 */
5268 public WearableExtender setCustomContentHeight(int height) {
5269 mCustomContentHeight = height;
5270 return this;
5271 }
5272
5273 /**
5274 * Get the custom height in pixels for the display of this notification's content.
5275 * <p>This option is only available for custom display notifications created
5276 * using {@link #setDisplayIntent}. See also {@link #setCustomSizePreset} and
5277 * {@link #setCustomContentHeight}.
5278 */
5279 public int getCustomContentHeight() {
5280 return mCustomContentHeight;
5281 }
5282
5283 /**
5284 * Set whether the scrolling position for the contents of this notification should start
5285 * at the bottom of the contents instead of the top when the contents are too long to
5286 * display within the screen. Default is false (start scroll at the top).
5287 */
5288 public WearableExtender setStartScrollBottom(boolean startScrollBottom) {
5289 setFlag(FLAG_START_SCROLL_BOTTOM, startScrollBottom);
5290 return this;
5291 }
5292
5293 /**
5294 * Get whether the scrolling position for the contents of this notification should start
5295 * at the bottom of the contents instead of the top when the contents are too long to
5296 * display within the screen. Default is false (start scroll at the top).
5297 */
5298 public boolean getStartScrollBottom() {
5299 return (mFlags & FLAG_START_SCROLL_BOTTOM) != 0;
5300 }
5301
5302 /**
5303 * Set whether the content intent is available when the wearable device is not connected
5304 * to a companion device. The user can still trigger this intent when the wearable device
5305 * is offline, but a visual hint will indicate that the content intent may not be available.
5306 * Defaults to true.
5307 */
5308 public WearableExtender setContentIntentAvailableOffline(
5309 boolean contentIntentAvailableOffline) {
5310 setFlag(FLAG_CONTENT_INTENT_AVAILABLE_OFFLINE, contentIntentAvailableOffline);
5311 return this;
5312 }
5313
5314 /**
5315 * Get whether the content intent is available when the wearable device is not connected
5316 * to a companion device. The user can still trigger this intent when the wearable device
5317 * is offline, but a visual hint will indicate that the content intent may not be available.
5318 * Defaults to true.
5319 */
5320 public boolean getContentIntentAvailableOffline() {
5321 return (mFlags & FLAG_CONTENT_INTENT_AVAILABLE_OFFLINE) != 0;
5322 }
5323
5324 /**
5325 * Set a hint that this notification's icon should not be displayed.
5326 * @param hintHideIcon {@code true} to hide the icon, {@code false} otherwise.
5327 * @return this object for method chaining
5328 */
5329 public WearableExtender setHintHideIcon(boolean hintHideIcon) {
5330 setFlag(FLAG_HINT_HIDE_ICON, hintHideIcon);
5331 return this;
5332 }
5333
5334 /**
5335 * Get a hint that this notification's icon should not be displayed.
5336 * @return {@code true} if this icon should not be displayed, false otherwise.
5337 * The default value is {@code false} if this was never set.
5338 */
5339 public boolean getHintHideIcon() {
5340 return (mFlags & FLAG_HINT_HIDE_ICON) != 0;
5341 }
5342
5343 /**
5344 * Set a visual hint that only the background image of this notification should be
5345 * displayed, and other semantic content should be hidden. This hint is only applicable
5346 * to sub-pages added using {@link #addPage}.
5347 */
5348 public WearableExtender setHintShowBackgroundOnly(boolean hintShowBackgroundOnly) {
5349 setFlag(FLAG_HINT_SHOW_BACKGROUND_ONLY, hintShowBackgroundOnly);
5350 return this;
5351 }
5352
5353 /**
5354 * Get a visual hint that only the background image of this notification should be
5355 * displayed, and other semantic content should be hidden. This hint is only applicable
5356 * to sub-pages added using {@link android.app.Notification.WearableExtender#addPage}.
5357 */
5358 public boolean getHintShowBackgroundOnly() {
5359 return (mFlags & FLAG_HINT_SHOW_BACKGROUND_ONLY) != 0;
5360 }
5361
Griff Hazen5f2edfc2014-09-29 16:28:44 -07005362 /**
Griff Hazen9c5be4e2014-11-17 17:47:50 -08005363 * Set a hint that this notification's background should not be clipped if possible,
5364 * and should instead be resized to fully display on the screen, retaining the aspect
5365 * ratio of the image. This can be useful for images like barcodes or qr codes.
Griff Hazen5f2edfc2014-09-29 16:28:44 -07005366 * @param hintAvoidBackgroundClipping {@code true} to avoid clipping if possible.
5367 * @return this object for method chaining
5368 */
5369 public WearableExtender setHintAvoidBackgroundClipping(
5370 boolean hintAvoidBackgroundClipping) {
5371 setFlag(FLAG_HINT_AVOID_BACKGROUND_CLIPPING, hintAvoidBackgroundClipping);
5372 return this;
5373 }
5374
5375 /**
Griff Hazen9c5be4e2014-11-17 17:47:50 -08005376 * Get a hint that this notification's background should not be clipped if possible,
5377 * and should instead be resized to fully display on the screen, retaining the aspect
5378 * ratio of the image. This can be useful for images like barcodes or qr codes.
Griff Hazen5f2edfc2014-09-29 16:28:44 -07005379 * @return {@code true} if it's ok if the background is clipped on the screen, false
5380 * otherwise. The default value is {@code false} if this was never set.
5381 */
5382 public boolean getHintAvoidBackgroundClipping() {
5383 return (mFlags & FLAG_HINT_AVOID_BACKGROUND_CLIPPING) != 0;
5384 }
5385
5386 /**
5387 * Set a hint that the screen should remain on for at least this duration when
5388 * this notification is displayed on the screen.
5389 * @param timeout The requested screen timeout in milliseconds. Can also be either
5390 * {@link #SCREEN_TIMEOUT_SHORT} or {@link #SCREEN_TIMEOUT_LONG}.
5391 * @return this object for method chaining
5392 */
5393 public WearableExtender setHintScreenTimeout(int timeout) {
5394 mHintScreenTimeout = timeout;
5395 return this;
5396 }
5397
5398 /**
5399 * Get the duration, in milliseconds, that the screen should remain on for
5400 * when this notification is displayed.
5401 * @return the duration in milliseconds if > 0, or either one of the sentinel values
5402 * {@link #SCREEN_TIMEOUT_SHORT} or {@link #SCREEN_TIMEOUT_LONG}.
5403 */
5404 public int getHintScreenTimeout() {
5405 return mHintScreenTimeout;
5406 }
5407
Griff Hazen61a9e862014-05-22 16:05:19 -07005408 private void setFlag(int mask, boolean value) {
5409 if (value) {
5410 mFlags |= mask;
5411 } else {
5412 mFlags &= ~mask;
5413 }
5414 }
5415 }
5416
5417 /**
Zhen Yu Song9d5528b2014-11-14 15:34:39 -08005418 * <p>Helper class to add Android Auto extensions to notifications. To create a notification
5419 * with car extensions:
5420 *
5421 * <ol>
5422 * <li>Create an {@link Notification.Builder}, setting any desired
5423 * properties.
5424 * <li>Create a {@link CarExtender}.
5425 * <li>Set car-specific properties using the {@code add} and {@code set} methods of
5426 * {@link CarExtender}.
5427 * <li>Call {@link Notification.Builder#extend(Notification.Extender)}
5428 * to apply the extensions to a notification.
5429 * </ol>
5430 *
5431 * <pre class="prettyprint">
5432 * Notification notification = new Notification.Builder(context)
5433 * ...
5434 * .extend(new CarExtender()
5435 * .set*(...))
5436 * .build();
5437 * </pre>
5438 *
5439 * <p>Car extensions can be accessed on an existing notification by using the
5440 * {@code CarExtender(Notification)} constructor, and then using the {@code get} methods
5441 * to access values.
5442 */
5443 public static final class CarExtender implements Extender {
5444 private static final String TAG = "CarExtender";
5445
5446 private static final String EXTRA_CAR_EXTENDER = "android.car.EXTENSIONS";
5447 private static final String EXTRA_LARGE_ICON = "large_icon";
5448 private static final String EXTRA_CONVERSATION = "car_conversation";
5449 private static final String EXTRA_COLOR = "app_color";
5450
5451 private Bitmap mLargeIcon;
5452 private UnreadConversation mUnreadConversation;
5453 private int mColor = Notification.COLOR_DEFAULT;
5454
5455 /**
5456 * Create a {@link CarExtender} with default options.
5457 */
5458 public CarExtender() {
5459 }
5460
5461 /**
5462 * Create a {@link CarExtender} from the CarExtender options of an existing Notification.
5463 *
5464 * @param notif The notification from which to copy options.
5465 */
5466 public CarExtender(Notification notif) {
5467 Bundle carBundle = notif.extras == null ?
5468 null : notif.extras.getBundle(EXTRA_CAR_EXTENDER);
5469 if (carBundle != null) {
5470 mLargeIcon = carBundle.getParcelable(EXTRA_LARGE_ICON);
5471 mColor = carBundle.getInt(EXTRA_COLOR, Notification.COLOR_DEFAULT);
5472
5473 Bundle b = carBundle.getBundle(EXTRA_CONVERSATION);
5474 mUnreadConversation = UnreadConversation.getUnreadConversationFromBundle(b);
5475 }
5476 }
5477
5478 /**
5479 * Apply car extensions to a notification that is being built. This is typically called by
5480 * the {@link Notification.Builder#extend(Notification.Extender)}
5481 * method of {@link Notification.Builder}.
5482 */
5483 @Override
5484 public Notification.Builder extend(Notification.Builder builder) {
5485 Bundle carExtensions = new Bundle();
5486
5487 if (mLargeIcon != null) {
5488 carExtensions.putParcelable(EXTRA_LARGE_ICON, mLargeIcon);
5489 }
5490 if (mColor != Notification.COLOR_DEFAULT) {
5491 carExtensions.putInt(EXTRA_COLOR, mColor);
5492 }
5493
5494 if (mUnreadConversation != null) {
5495 Bundle b = mUnreadConversation.getBundleForUnreadConversation();
5496 carExtensions.putBundle(EXTRA_CONVERSATION, b);
5497 }
5498
5499 builder.getExtras().putBundle(EXTRA_CAR_EXTENDER, carExtensions);
5500 return builder;
5501 }
5502
5503 /**
5504 * Sets the accent color to use when Android Auto presents the notification.
5505 *
5506 * Android Auto uses the color set with {@link Notification.Builder#setColor(int)}
5507 * to accent the displayed notification. However, not all colors are acceptable in an
5508 * automotive setting. This method can be used to override the color provided in the
5509 * notification in such a situation.
5510 */
Tor Norbye80756e32015-03-02 09:39:27 -08005511 public CarExtender setColor(@ColorInt int color) {
Zhen Yu Song9d5528b2014-11-14 15:34:39 -08005512 mColor = color;
5513 return this;
5514 }
5515
5516 /**
5517 * Gets the accent color.
5518 *
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005519 * @see #setColor
Zhen Yu Song9d5528b2014-11-14 15:34:39 -08005520 */
Tor Norbye80756e32015-03-02 09:39:27 -08005521 @ColorInt
Zhen Yu Song9d5528b2014-11-14 15:34:39 -08005522 public int getColor() {
5523 return mColor;
5524 }
5525
5526 /**
5527 * Sets the large icon of the car notification.
5528 *
5529 * If no large icon is set in the extender, Android Auto will display the icon
5530 * specified by {@link Notification.Builder#setLargeIcon(android.graphics.Bitmap)}
5531 *
5532 * @param largeIcon The large icon to use in the car notification.
5533 * @return This object for method chaining.
5534 */
5535 public CarExtender setLargeIcon(Bitmap largeIcon) {
5536 mLargeIcon = largeIcon;
5537 return this;
5538 }
5539
5540 /**
5541 * Gets the large icon used in this car notification, or null if no icon has been set.
5542 *
5543 * @return The large icon for the car notification.
5544 * @see CarExtender#setLargeIcon
5545 */
5546 public Bitmap getLargeIcon() {
5547 return mLargeIcon;
5548 }
5549
5550 /**
5551 * Sets the unread conversation in a message notification.
5552 *
5553 * @param unreadConversation The unread part of the conversation this notification conveys.
5554 * @return This object for method chaining.
5555 */
5556 public CarExtender setUnreadConversation(UnreadConversation unreadConversation) {
5557 mUnreadConversation = unreadConversation;
5558 return this;
5559 }
5560
5561 /**
5562 * Returns the unread conversation conveyed by this notification.
5563 * @see #setUnreadConversation(UnreadConversation)
5564 */
5565 public UnreadConversation getUnreadConversation() {
5566 return mUnreadConversation;
5567 }
5568
5569 /**
5570 * A class which holds the unread messages from a conversation.
5571 */
5572 public static class UnreadConversation {
5573 private static final String KEY_AUTHOR = "author";
5574 private static final String KEY_TEXT = "text";
5575 private static final String KEY_MESSAGES = "messages";
5576 private static final String KEY_REMOTE_INPUT = "remote_input";
5577 private static final String KEY_ON_REPLY = "on_reply";
5578 private static final String KEY_ON_READ = "on_read";
5579 private static final String KEY_PARTICIPANTS = "participants";
5580 private static final String KEY_TIMESTAMP = "timestamp";
5581
5582 private final String[] mMessages;
5583 private final RemoteInput mRemoteInput;
5584 private final PendingIntent mReplyPendingIntent;
5585 private final PendingIntent mReadPendingIntent;
5586 private final String[] mParticipants;
5587 private final long mLatestTimestamp;
5588
5589 UnreadConversation(String[] messages, RemoteInput remoteInput,
5590 PendingIntent replyPendingIntent, PendingIntent readPendingIntent,
5591 String[] participants, long latestTimestamp) {
5592 mMessages = messages;
5593 mRemoteInput = remoteInput;
5594 mReadPendingIntent = readPendingIntent;
5595 mReplyPendingIntent = replyPendingIntent;
5596 mParticipants = participants;
5597 mLatestTimestamp = latestTimestamp;
5598 }
5599
5600 /**
5601 * Gets the list of messages conveyed by this notification.
5602 */
5603 public String[] getMessages() {
5604 return mMessages;
5605 }
5606
5607 /**
5608 * Gets the remote input that will be used to convey the response to a message list, or
5609 * null if no such remote input exists.
5610 */
5611 public RemoteInput getRemoteInput() {
5612 return mRemoteInput;
5613 }
5614
5615 /**
5616 * Gets the pending intent that will be triggered when the user replies to this
5617 * notification.
5618 */
5619 public PendingIntent getReplyPendingIntent() {
5620 return mReplyPendingIntent;
5621 }
5622
5623 /**
5624 * Gets the pending intent that Android Auto will send after it reads aloud all messages
5625 * in this object's message list.
5626 */
5627 public PendingIntent getReadPendingIntent() {
5628 return mReadPendingIntent;
5629 }
5630
5631 /**
5632 * Gets the participants in the conversation.
5633 */
5634 public String[] getParticipants() {
5635 return mParticipants;
5636 }
5637
5638 /**
5639 * Gets the firs participant in the conversation.
5640 */
5641 public String getParticipant() {
5642 return mParticipants.length > 0 ? mParticipants[0] : null;
5643 }
5644
5645 /**
5646 * Gets the timestamp of the conversation.
5647 */
5648 public long getLatestTimestamp() {
5649 return mLatestTimestamp;
5650 }
5651
5652 Bundle getBundleForUnreadConversation() {
5653 Bundle b = new Bundle();
5654 String author = null;
5655 if (mParticipants != null && mParticipants.length > 1) {
5656 author = mParticipants[0];
5657 }
5658 Parcelable[] messages = new Parcelable[mMessages.length];
5659 for (int i = 0; i < messages.length; i++) {
5660 Bundle m = new Bundle();
5661 m.putString(KEY_TEXT, mMessages[i]);
5662 m.putString(KEY_AUTHOR, author);
5663 messages[i] = m;
5664 }
5665 b.putParcelableArray(KEY_MESSAGES, messages);
5666 if (mRemoteInput != null) {
5667 b.putParcelable(KEY_REMOTE_INPUT, mRemoteInput);
5668 }
5669 b.putParcelable(KEY_ON_REPLY, mReplyPendingIntent);
5670 b.putParcelable(KEY_ON_READ, mReadPendingIntent);
5671 b.putStringArray(KEY_PARTICIPANTS, mParticipants);
5672 b.putLong(KEY_TIMESTAMP, mLatestTimestamp);
5673 return b;
5674 }
5675
5676 static UnreadConversation getUnreadConversationFromBundle(Bundle b) {
5677 if (b == null) {
5678 return null;
5679 }
5680 Parcelable[] parcelableMessages = b.getParcelableArray(KEY_MESSAGES);
5681 String[] messages = null;
5682 if (parcelableMessages != null) {
5683 String[] tmp = new String[parcelableMessages.length];
5684 boolean success = true;
5685 for (int i = 0; i < tmp.length; i++) {
5686 if (!(parcelableMessages[i] instanceof Bundle)) {
5687 success = false;
5688 break;
5689 }
5690 tmp[i] = ((Bundle) parcelableMessages[i]).getString(KEY_TEXT);
5691 if (tmp[i] == null) {
5692 success = false;
5693 break;
5694 }
5695 }
5696 if (success) {
5697 messages = tmp;
5698 } else {
5699 return null;
5700 }
5701 }
5702
5703 PendingIntent onRead = b.getParcelable(KEY_ON_READ);
5704 PendingIntent onReply = b.getParcelable(KEY_ON_REPLY);
5705
5706 RemoteInput remoteInput = b.getParcelable(KEY_REMOTE_INPUT);
5707
5708 String[] participants = b.getStringArray(KEY_PARTICIPANTS);
5709 if (participants == null || participants.length != 1) {
5710 return null;
5711 }
5712
5713 return new UnreadConversation(messages,
5714 remoteInput,
5715 onReply,
5716 onRead,
5717 participants, b.getLong(KEY_TIMESTAMP));
5718 }
5719 };
5720
5721 /**
5722 * Builder class for {@link CarExtender.UnreadConversation} objects.
5723 */
5724 public static class Builder {
5725 private final List<String> mMessages = new ArrayList<String>();
5726 private final String mParticipant;
5727 private RemoteInput mRemoteInput;
5728 private PendingIntent mReadPendingIntent;
5729 private PendingIntent mReplyPendingIntent;
5730 private long mLatestTimestamp;
5731
5732 /**
5733 * Constructs a new builder for {@link CarExtender.UnreadConversation}.
5734 *
5735 * @param name The name of the other participant in the conversation.
5736 */
5737 public Builder(String name) {
5738 mParticipant = name;
5739 }
5740
5741 /**
5742 * Appends a new unread message to the list of messages for this conversation.
5743 *
5744 * The messages should be added from oldest to newest.
5745 *
5746 * @param message The text of the new unread message.
5747 * @return This object for method chaining.
5748 */
5749 public Builder addMessage(String message) {
5750 mMessages.add(message);
5751 return this;
5752 }
5753
5754 /**
5755 * Sets the pending intent and remote input which will convey the reply to this
5756 * notification.
5757 *
5758 * @param pendingIntent The pending intent which will be triggered on a reply.
5759 * @param remoteInput The remote input parcelable which will carry the reply.
5760 * @return This object for method chaining.
5761 *
5762 * @see CarExtender.UnreadConversation#getRemoteInput
5763 * @see CarExtender.UnreadConversation#getReplyPendingIntent
5764 */
5765 public Builder setReplyAction(
5766 PendingIntent pendingIntent, RemoteInput remoteInput) {
5767 mRemoteInput = remoteInput;
5768 mReplyPendingIntent = pendingIntent;
5769
5770 return this;
5771 }
5772
5773 /**
5774 * Sets the pending intent that will be sent once the messages in this notification
5775 * are read.
5776 *
5777 * @param pendingIntent The pending intent to use.
5778 * @return This object for method chaining.
5779 */
5780 public Builder setReadPendingIntent(PendingIntent pendingIntent) {
5781 mReadPendingIntent = pendingIntent;
5782 return this;
5783 }
5784
5785 /**
5786 * Sets the timestamp of the most recent message in an unread conversation.
5787 *
5788 * If a messaging notification has been posted by your application and has not
5789 * yet been cancelled, posting a later notification with the same id and tag
5790 * but without a newer timestamp may result in Android Auto not displaying a
5791 * heads up notification for the later notification.
5792 *
5793 * @param timestamp The timestamp of the most recent message in the conversation.
5794 * @return This object for method chaining.
5795 */
5796 public Builder setLatestTimestamp(long timestamp) {
5797 mLatestTimestamp = timestamp;
5798 return this;
5799 }
5800
5801 /**
5802 * Builds a new unread conversation object.
5803 *
5804 * @return The new unread conversation object.
5805 */
5806 public UnreadConversation build() {
5807 String[] messages = mMessages.toArray(new String[mMessages.size()]);
5808 String[] participants = { mParticipant };
5809 return new UnreadConversation(messages, mRemoteInput, mReplyPendingIntent,
5810 mReadPendingIntent, participants, mLatestTimestamp);
5811 }
5812 }
5813 }
5814
5815 /**
Griff Hazen61a9e862014-05-22 16:05:19 -07005816 * Get an array of Notification objects from a parcelable array bundle field.
5817 * Update the bundle to have a typed array so fetches in the future don't need
5818 * to do an array copy.
5819 */
5820 private static Notification[] getNotificationArrayFromBundle(Bundle bundle, String key) {
5821 Parcelable[] array = bundle.getParcelableArray(key);
5822 if (array instanceof Notification[] || array == null) {
5823 return (Notification[]) array;
5824 }
5825 Notification[] typedArray = Arrays.copyOf(array, array.length,
5826 Notification[].class);
5827 bundle.putParcelableArray(key, typedArray);
5828 return typedArray;
5829 }
Christoph Studer4600f9b2014-07-22 22:44:43 +02005830
5831 private static class BuilderRemoteViews extends RemoteViews {
5832 public BuilderRemoteViews(Parcel parcel) {
5833 super(parcel);
5834 }
5835
Kenny Guy77320062014-08-27 21:37:15 +01005836 public BuilderRemoteViews(ApplicationInfo appInfo, int layoutId) {
5837 super(appInfo, layoutId);
Christoph Studer4600f9b2014-07-22 22:44:43 +02005838 }
5839
5840 @Override
5841 public BuilderRemoteViews clone() {
5842 Parcel p = Parcel.obtain();
5843 writeToParcel(p, 0);
5844 p.setDataPosition(0);
5845 BuilderRemoteViews brv = new BuilderRemoteViews(p);
5846 p.recycle();
5847 return brv;
5848 }
5849 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005850}