blob: 25194d416d715780eb9164165953c42db60d7567 [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
2 * Copyright (C) 2007 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package android.app;
18
Tor Norbye80756e32015-03-02 09:39:27 -080019import android.annotation.ColorInt;
Tor Norbye7b9c9122013-05-30 16:48:33 -070020import android.annotation.DrawableRes;
Tor Norbyed9273d62013-05-30 15:59:53 -070021import android.annotation.IntDef;
Daniel Sandler01df1c62014-06-09 10:54:01 -040022import android.annotation.SdkConstant;
23import android.annotation.SdkConstant.SdkConstantType;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080024import android.content.Context;
25import android.content.Intent;
Kenny Guy77320062014-08-27 21:37:15 +010026import android.content.pm.ApplicationInfo;
Christoph Studer4600f9b2014-07-22 22:44:43 +020027import android.content.pm.PackageManager.NameNotFoundException;
Jorim Jaggief72a192014-08-26 21:57:46 +020028import android.content.res.ColorStateList;
Joe Onoratoef1e7762010-09-17 18:38:38 -040029import android.graphics.Bitmap;
Kenny Guy8a0101b2014-05-08 23:34:12 +010030import android.graphics.Canvas;
Jorim Jaggi5c2d8462014-03-21 17:37:00 +010031import android.graphics.PorterDuff;
Kenny Guy8a0101b2014-05-08 23:34:12 +010032import android.graphics.drawable.Drawable;
Dan Sandlerd63f9322015-05-06 15:18:49 -040033import android.graphics.drawable.Icon;
John Spurlockc0650f022014-07-19 13:22:39 -040034import android.media.AudioAttributes;
Jeff Sharkey098d5802012-04-26 17:30:34 -070035import android.media.AudioManager;
Jeff Browndba34ba2014-06-24 20:46:03 -070036import android.media.session.MediaSession;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080037import android.net.Uri;
Daniel Sandlerdcbaf662013-04-26 16:23:09 -040038import android.os.BadParcelableException;
Christoph Studer239f8352014-08-25 15:13:18 +020039import android.os.Build;
Daniel Sandler2561b0b2012-02-13 21:04:12 -050040import android.os.Bundle;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080041import android.os.Parcel;
42import android.os.Parcelable;
Daniel Sandlera2985ed2012-04-03 16:42:00 -040043import android.os.SystemClock;
Jeff Sharkey6d515712012-09-20 16:06:08 -070044import android.os.UserHandle;
Selim Cinek60a54252016-02-26 17:03:25 -080045import android.text.SpannableStringBuilder;
46import android.text.Spanned;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080047import android.text.TextUtils;
Selim Cinek60a54252016-02-26 17:03:25 -080048import android.text.style.AbsoluteSizeSpan;
Selim Cinek89991a22016-03-07 19:51:54 -080049import android.text.style.CharacterStyle;
Selim Cinek60a54252016-02-26 17:03:25 -080050import android.text.style.RelativeSizeSpan;
51import android.text.style.TextAppearanceSpan;
Daniel Sandlerdcbaf662013-04-26 16:23:09 -040052import android.util.Log;
Dan Sandler50128532015-12-08 15:42:41 -050053import android.util.SparseArray;
Daniel Sandler9f7936a2012-05-21 16:14:28 -040054import android.util.TypedValue;
Griff Hazen61a9e862014-05-22 16:05:19 -070055import android.view.Gravity;
Selim Cinekea4bef72015-12-02 15:51:10 -080056import android.view.NotificationHeaderView;
Joe Onorato8595a3d2010-11-19 18:12:07 -080057import android.view.View;
Adrian Roos9b123cf2016-02-04 14:55:57 -080058import android.view.ViewGroup;
Jeff Sharkey1c400132011-08-05 14:50:13 -070059import android.widget.ProgressBar;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080060import android.widget.RemoteViews;
61
Griff Hazen959591e2014-05-15 22:26:18 -070062import com.android.internal.R;
Griff Hazenc091ba82014-05-16 10:13:26 -070063import com.android.internal.util.NotificationColorUtil;
Griff Hazen959591e2014-05-15 22:26:18 -070064
Tor Norbyed9273d62013-05-30 15:59:53 -070065import java.lang.annotation.Retention;
66import java.lang.annotation.RetentionPolicy;
Christoph Studer4600f9b2014-07-22 22:44:43 +020067import java.lang.reflect.Constructor;
Daniel Sandler2561b0b2012-02-13 21:04:12 -050068import java.util.ArrayList;
Griff Hazen61a9e862014-05-22 16:05:19 -070069import java.util.Arrays;
Griff Hazen5cadc3b2014-05-20 09:55:39 -070070import java.util.Collections;
Griff Hazen61a9e862014-05-22 16:05:19 -070071import java.util.List;
Dan Sandler50128532015-12-08 15:42:41 -050072import java.util.Set;
Joe Onorato561d3852010-11-20 18:09:34 -080073
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080074/**
75 * A class that represents how a persistent notification is to be presented to
76 * the user using the {@link android.app.NotificationManager}.
77 *
Joe Onoratocb109a02011-01-18 17:57:41 -080078 * <p>The {@link Notification.Builder Notification.Builder} has been added to make it
79 * easier to construct Notifications.</p>
80 *
Joe Fernandez558459f2011-10-13 16:47:36 -070081 * <div class="special reference">
82 * <h3>Developer Guides</h3>
83 * <p>For a guide to creating notifications, read the
84 * <a href="{@docRoot}guide/topics/ui/notifiers/notifications.html">Status Bar Notifications</a>
85 * developer guide.</p>
86 * </div>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080087 */
88public class Notification implements Parcelable
89{
Daniel Sandlerdcbaf662013-04-26 16:23:09 -040090 private static final String TAG = "Notification";
91
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080092 /**
Daniel Sandler01df1c62014-06-09 10:54:01 -040093 * An activity that provides a user interface for adjusting notification preferences for its
94 * containing application. Optional but recommended for apps that post
95 * {@link android.app.Notification Notifications}.
96 */
97 @SdkConstant(SdkConstantType.INTENT_CATEGORY)
98 public static final String INTENT_CATEGORY_NOTIFICATION_PREFERENCES
99 = "android.intent.category.NOTIFICATION_PREFERENCES";
100
101 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800102 * Use all default values (where applicable).
103 */
104 public static final int DEFAULT_ALL = ~0;
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500105
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800106 /**
107 * Use the default notification sound. This will ignore any given
108 * {@link #sound}.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500109 *
Chris Wren47c20a12014-06-18 17:27:29 -0400110 * <p>
111 * A notification that is noisy is more likely to be presented as a heads-up notification.
112 * </p>
113 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800114 * @see #defaults
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500115 */
116
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800117 public static final int DEFAULT_SOUND = 1;
118
119 /**
120 * Use the default notification vibrate. This will ignore any given
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500121 * {@link #vibrate}. Using phone vibration requires the
Scott Mainb8b36452009-04-26 15:50:49 -0700122 * {@link android.Manifest.permission#VIBRATE VIBRATE} permission.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500123 *
Chris Wren47c20a12014-06-18 17:27:29 -0400124 * <p>
125 * A notification that vibrates is more likely to be presented as a heads-up notification.
126 * </p>
127 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800128 * @see #defaults
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500129 */
130
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800131 public static final int DEFAULT_VIBRATE = 2;
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500132
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800133 /**
134 * Use the default notification lights. This will ignore the
135 * {@link #FLAG_SHOW_LIGHTS} bit, and {@link #ledARGB}, {@link #ledOffMS}, or
136 * {@link #ledOnMS}.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500137 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800138 * @see #defaults
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500139 */
140
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800141 public static final int DEFAULT_LIGHTS = 4;
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500142
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800143 /**
Christoph Studer535ec612014-09-03 15:47:47 +0200144 * Maximum length of CharSequences accepted by Builder and friends.
145 *
146 * <p>
147 * Avoids spamming the system with overly large strings such as full e-mails.
148 */
149 private static final int MAX_CHARSEQUENCE_LENGTH = 5 * 1024;
150
151 /**
Adrian Roose458aa82015-12-08 16:17:19 -0800152 * Maximum entries of reply text that are accepted by Builder and friends.
153 */
154 private static final int MAX_REPLY_HISTORY = 5;
155
156 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500157 * A timestamp related to this notification, in milliseconds since the epoch.
Joe Malin8d40d042012-11-05 11:36:40 -0800158 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500159 * Default value: {@link System#currentTimeMillis() Now}.
160 *
161 * Choose a timestamp that will be most relevant to the user. For most finite events, this
162 * corresponds to the time the event happened (or will happen, in the case of events that have
163 * yet to occur but about which the user is being informed). Indefinite events should be
Joe Malin8d40d042012-11-05 11:36:40 -0800164 * timestamped according to when the activity began.
165 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500166 * Some examples:
Joe Malin8d40d042012-11-05 11:36:40 -0800167 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500168 * <ul>
169 * <li>Notification of a new chat message should be stamped when the message was received.</li>
170 * <li>Notification of an ongoing file download (with a progress bar, for example) should be stamped when the download started.</li>
171 * <li>Notification of a completed file download should be stamped when the download finished.</li>
172 * <li>Notification of an upcoming meeting should be stamped with the time the meeting will begin (that is, in the future).</li>
173 * <li>Notification of an ongoing stopwatch (increasing timer) should be stamped with the watch's start time.
174 * <li>Notification of an ongoing countdown timer should be stamped with the timer's end time.
Joe Malin8d40d042012-11-05 11:36:40 -0800175 * </ul>
176 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800177 */
178 public long when;
179
180 /**
181 * The resource id of a drawable to use as the icon in the status bar.
Dan Sandler86647982015-05-13 23:41:13 -0400182 *
183 * @deprecated Use {@link Builder#setSmallIcon(Icon)} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800184 */
Dan Sandler86647982015-05-13 23:41:13 -0400185 @Deprecated
Tor Norbye7b9c9122013-05-30 16:48:33 -0700186 @DrawableRes
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800187 public int icon;
188
189 /**
Joe Onorato46439ce2010-11-19 13:56:21 -0800190 * If the icon in the status bar is to have more than one level, you can set this. Otherwise,
191 * leave it at its default value of 0.
192 *
193 * @see android.widget.ImageView#setImageLevel
Griff Hazen959591e2014-05-15 22:26:18 -0700194 * @see android.graphics.drawable.Drawable#setLevel
Joe Onorato46439ce2010-11-19 13:56:21 -0800195 */
196 public int iconLevel;
197
198 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500199 * The number of events that this notification represents. For example, in a new mail
200 * notification, this could be the number of unread messages.
Joe Malin8d40d042012-11-05 11:36:40 -0800201 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500202 * The system may or may not use this field to modify the appearance of the notification. For
203 * example, before {@link android.os.Build.VERSION_CODES#HONEYCOMB}, this number was
204 * superimposed over the icon in the status bar. Starting with
205 * {@link android.os.Build.VERSION_CODES#HONEYCOMB}, the template used by
206 * {@link Notification.Builder} has displayed the number in the expanded notification view.
Joe Malin8d40d042012-11-05 11:36:40 -0800207 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500208 * If the number is 0 or negative, it is never shown.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800209 */
210 public int number;
211
212 /**
213 * The intent to execute when the expanded status entry is clicked. If
214 * this is an activity, it must include the
215 * {@link android.content.Intent#FLAG_ACTIVITY_NEW_TASK} flag, which requires
Scott Main7aee61f2011-02-08 11:25:01 -0800216 * that you take care of task management as described in the
217 * <a href="{@docRoot}guide/topics/fundamentals/tasks-and-back-stack.html">Tasks and Back
Dianne Hackborn6ceca582012-01-10 15:24:26 -0800218 * Stack</a> document. In particular, make sure to read the notification section
219 * <a href="{@docRoot}guide/topics/ui/notifiers/notifications.html#HandlingNotifications">Handling
220 * Notifications</a> for the correct ways to launch an application from a
221 * notification.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800222 */
223 public PendingIntent contentIntent;
224
225 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500226 * The intent to execute when the notification is explicitly dismissed by the user, either with
227 * the "Clear All" button or by swiping it away individually.
228 *
229 * This probably shouldn't be launching an activity since several of those will be sent
230 * at the same time.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800231 */
232 public PendingIntent deleteIntent;
233
234 /**
Dianne Hackborn170bae72010-09-03 15:14:28 -0700235 * An intent to launch instead of posting the notification to the status bar.
Joe Onoratocb109a02011-01-18 17:57:41 -0800236 *
Chris Wren47c20a12014-06-18 17:27:29 -0400237 * <p>
238 * The system UI may choose to display a heads-up notification, instead of
239 * launching this intent, while the user is using the device.
240 * </p>
241 *
Joe Onoratocb109a02011-01-18 17:57:41 -0800242 * @see Notification.Builder#setFullScreenIntent
Daniel Sandlere46cbd32010-06-17 10:35:26 -0400243 */
244 public PendingIntent fullScreenIntent;
245
246 /**
Dan Sandler5fcdf6e2014-07-18 11:31:15 -0400247 * Text that summarizes this notification for accessibility services.
248 *
249 * As of the L release, this text is no longer shown on screen, but it is still useful to
250 * accessibility services (where it serves as an audible announcement of the notification's
251 * appearance).
Joe Onoratoef1e7762010-09-17 18:38:38 -0400252 *
Joe Onorato46439ce2010-11-19 13:56:21 -0800253 * @see #tickerView
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800254 */
255 public CharSequence tickerText;
256
257 /**
Dan Sandler5fcdf6e2014-07-18 11:31:15 -0400258 * Formerly, a view showing the {@link #tickerText}.
259 *
260 * No longer displayed in the status bar as of API 21.
Joe Onoratoef1e7762010-09-17 18:38:38 -0400261 */
Dan Sandler5fcdf6e2014-07-18 11:31:15 -0400262 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -0800263 public RemoteViews tickerView;
Joe Onoratoef1e7762010-09-17 18:38:38 -0400264
265 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400266 * The view that will represent this notification in the notification list (which is pulled
267 * down from the status bar).
268 *
Julia Reynoldsd4ea7412016-02-17 14:00:56 -0500269 * As of N, this field may be null. The notification view is determined by the inputs
270 * to {@link Notification.Builder}; a custom RemoteViews can optionally be
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400271 * supplied with {@link Notification.Builder#setCustomContentView(RemoteViews)}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800272 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400273 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800274 public RemoteViews contentView;
275
Daniel Sandlera0a938c2012-03-15 08:42:37 -0400276 /**
Daniel Sandler4dfbe832012-04-11 14:51:46 -0400277 * A large-format version of {@link #contentView}, giving the Notification an
Daniel Sandlerf3b73432012-03-27 15:01:25 -0400278 * opportunity to show more detail. The system UI may choose to show this
279 * instead of the normal content view at its discretion.
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400280 *
Julia Reynoldsd4ea7412016-02-17 14:00:56 -0500281 * As of N, this field may be null. The expanded notification view is determined by the
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400282 * inputs to {@link Notification.Builder}; a custom RemoteViews can optionally be
283 * supplied with {@link Notification.Builder#setCustomBigContentView(RemoteViews)}.
Daniel Sandlerf3b73432012-03-27 15:01:25 -0400284 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400285 @Deprecated
Daniel Sandlerf3b73432012-03-27 15:01:25 -0400286 public RemoteViews bigContentView;
287
Chris Wren8fd39ec2014-02-27 17:43:26 -0500288
289 /**
Chris Wren47c20a12014-06-18 17:27:29 -0400290 * A medium-format version of {@link #contentView}, providing the Notification an
291 * opportunity to add action buttons to contentView. At its discretion, the system UI may
292 * choose to show this as a heads-up notification, which will pop up so the user can see
293 * it without leaving their current activity.
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400294 *
Julia Reynoldsd4ea7412016-02-17 14:00:56 -0500295 * As of N, this field may be null. The heads-up notification view is determined by the
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400296 * inputs to {@link Notification.Builder}; a custom RemoteViews can optionally be
297 * supplied with {@link Notification.Builder#setCustomHeadsUpContentView(RemoteViews)}.
Chris Wren8fd39ec2014-02-27 17:43:26 -0500298 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400299 @Deprecated
Chris Wren8fd39ec2014-02-27 17:43:26 -0500300 public RemoteViews headsUpContentView;
301
Daniel Sandlerf3b73432012-03-27 15:01:25 -0400302 /**
Dan Sandler86647982015-05-13 23:41:13 -0400303 * A large bitmap to be shown in the notification content area.
304 *
305 * @deprecated Use {@link Builder#setLargeIcon(Icon)} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800306 */
Dan Sandler86647982015-05-13 23:41:13 -0400307 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -0800308 public Bitmap largeIcon;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800309
310 /**
311 * The sound to play.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500312 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800313 * <p>
Chris Wren47c20a12014-06-18 17:27:29 -0400314 * A notification that is noisy is more likely to be presented as a heads-up notification.
315 * </p>
316 *
317 * <p>
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500318 * To play the default notification sound, see {@link #defaults}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800319 * </p>
320 */
321 public Uri sound;
322
323 /**
324 * Use this constant as the value for audioStreamType to request that
325 * the default stream type for notifications be used. Currently the
Jeff Sharkey098d5802012-04-26 17:30:34 -0700326 * default stream type is {@link AudioManager#STREAM_NOTIFICATION}.
John Spurlockc0650f022014-07-19 13:22:39 -0400327 *
328 * @deprecated Use {@link #audioAttributes} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800329 */
Jean-Michel Trivi81f871e2014-08-06 16:32:38 -0700330 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800331 public static final int STREAM_DEFAULT = -1;
332
333 /**
334 * The audio stream type to use when playing the sound.
335 * Should be one of the STREAM_ constants from
336 * {@link android.media.AudioManager}.
John Spurlockc0650f022014-07-19 13:22:39 -0400337 *
338 * @deprecated Use {@link #audioAttributes} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800339 */
Jean-Michel Trivi81f871e2014-08-06 16:32:38 -0700340 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800341 public int audioStreamType = STREAM_DEFAULT;
342
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800343 /**
John Spurlockc0650f022014-07-19 13:22:39 -0400344 * The default value of {@link #audioAttributes}.
345 */
346 public static final AudioAttributes AUDIO_ATTRIBUTES_DEFAULT = new AudioAttributes.Builder()
347 .setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
348 .setUsage(AudioAttributes.USAGE_NOTIFICATION)
349 .build();
350
351 /**
352 * The {@link AudioAttributes audio attributes} to use when playing the sound.
353 */
354 public AudioAttributes audioAttributes = AUDIO_ATTRIBUTES_DEFAULT;
355
356 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500357 * The pattern with which to vibrate.
358 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800359 * <p>
360 * To vibrate the default pattern, see {@link #defaults}.
361 * </p>
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500362 *
Chris Wren47c20a12014-06-18 17:27:29 -0400363 * <p>
364 * A notification that vibrates is more likely to be presented as a heads-up notification.
365 * </p>
366 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800367 * @see android.os.Vibrator#vibrate(long[],int)
368 */
369 public long[] vibrate;
370
371 /**
372 * The color of the led. The hardware will do its best approximation.
373 *
374 * @see #FLAG_SHOW_LIGHTS
375 * @see #flags
376 */
Tor Norbye80756e32015-03-02 09:39:27 -0800377 @ColorInt
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800378 public int ledARGB;
379
380 /**
381 * The number of milliseconds for the LED to be on while it's flashing.
382 * The hardware will do its best approximation.
383 *
384 * @see #FLAG_SHOW_LIGHTS
385 * @see #flags
386 */
387 public int ledOnMS;
388
389 /**
390 * The number of milliseconds for the LED to be off while it's flashing.
391 * The hardware will do its best approximation.
392 *
393 * @see #FLAG_SHOW_LIGHTS
394 * @see #flags
395 */
396 public int ledOffMS;
397
398 /**
399 * Specifies which values should be taken from the defaults.
400 * <p>
401 * To set, OR the desired from {@link #DEFAULT_SOUND},
402 * {@link #DEFAULT_VIBRATE}, {@link #DEFAULT_LIGHTS}. For all default
403 * values, use {@link #DEFAULT_ALL}.
404 * </p>
405 */
406 public int defaults;
407
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800408 /**
409 * Bit to be bitwise-ored into the {@link #flags} field that should be
410 * set if you want the LED on for this notification.
411 * <ul>
412 * <li>To turn the LED off, pass 0 in the alpha channel for colorARGB
413 * or 0 for both ledOnMS and ledOffMS.</li>
414 * <li>To turn the LED on, pass 1 for ledOnMS and 0 for ledOffMS.</li>
415 * <li>To flash the LED, pass the number of milliseconds that it should
416 * be on and off to ledOnMS and ledOffMS.</li>
417 * </ul>
418 * <p>
419 * Since hardware varies, you are not guaranteed that any of the values
420 * you pass are honored exactly. Use the system defaults (TODO) if possible
421 * because they will be set to values that work on any given hardware.
422 * <p>
423 * The alpha channel must be set for forward compatibility.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500424 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800425 */
426 public static final int FLAG_SHOW_LIGHTS = 0x00000001;
427
428 /**
429 * Bit to be bitwise-ored into the {@link #flags} field that should be
430 * set if this notification is in reference to something that is ongoing,
431 * like a phone call. It should not be set if this notification is in
432 * reference to something that happened at a particular point in time,
433 * like a missed phone call.
434 */
435 public static final int FLAG_ONGOING_EVENT = 0x00000002;
436
437 /**
438 * Bit to be bitwise-ored into the {@link #flags} field that if set,
Scott Mainb8b36452009-04-26 15:50:49 -0700439 * the audio will be repeated until the notification is
440 * cancelled or the notification window is opened.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800441 */
442 public static final int FLAG_INSISTENT = 0x00000004;
443
444 /**
445 * Bit to be bitwise-ored into the {@link #flags} field that should be
Griff Hazen293977b2014-04-28 08:37:20 -0700446 * set if you would only like the sound, vibrate and ticker to be played
447 * if the notification was not already showing.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800448 */
449 public static final int FLAG_ONLY_ALERT_ONCE = 0x00000008;
450
451 /**
452 * Bit to be bitwise-ored into the {@link #flags} field that should be
453 * set if the notification should be canceled when it is clicked by the
Daniel Sandler8aa9ae62012-12-04 23:31:47 -0500454 * user.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800455 */
456 public static final int FLAG_AUTO_CANCEL = 0x00000010;
457
458 /**
459 * Bit to be bitwise-ored into the {@link #flags} field that should be
460 * set if the notification should not be canceled when the user clicks
461 * the Clear all button.
462 */
463 public static final int FLAG_NO_CLEAR = 0x00000020;
464
Dianne Hackbornd8a43f62009-08-17 23:33:56 -0700465 /**
466 * Bit to be bitwise-ored into the {@link #flags} field that should be
467 * set if this notification represents a currently running service. This
468 * will normally be set for you by {@link Service#startForeground}.
469 */
470 public static final int FLAG_FOREGROUND_SERVICE = 0x00000040;
471
Daniel Sandlere46cbd32010-06-17 10:35:26 -0400472 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500473 * Obsolete flag indicating high-priority notifications; use the priority field instead.
Joe Malin8d40d042012-11-05 11:36:40 -0800474 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500475 * @deprecated Use {@link #priority} with a positive value.
Daniel Sandlere46cbd32010-06-17 10:35:26 -0400476 */
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500477 public static final int FLAG_HIGH_PRIORITY = 0x00000080;
Daniel Sandlere46cbd32010-06-17 10:35:26 -0400478
Griff Hazendfcb0802014-02-11 12:00:00 -0800479 /**
480 * Bit to be bitswise-ored into the {@link #flags} field that should be
481 * set if this notification is relevant to the current device only
482 * and it is not recommended that it bridge to other devices.
483 */
484 public static final int FLAG_LOCAL_ONLY = 0x00000100;
485
Griff Hazen5cadc3b2014-05-20 09:55:39 -0700486 /**
487 * Bit to be bitswise-ored into the {@link #flags} field that should be
488 * set if this notification is the group summary for a group of notifications.
489 * Grouped notifications may display in a cluster or stack on devices which
490 * support such rendering. Requires a group key also be set using {@link Builder#setGroup}.
491 */
492 public static final int FLAG_GROUP_SUMMARY = 0x00000200;
493
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800494 public int flags;
495
Tor Norbyed9273d62013-05-30 15:59:53 -0700496 /** @hide */
497 @IntDef({PRIORITY_DEFAULT,PRIORITY_LOW,PRIORITY_MIN,PRIORITY_HIGH,PRIORITY_MAX})
498 @Retention(RetentionPolicy.SOURCE)
499 public @interface Priority {}
500
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800501 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500502 * Default notification {@link #priority}. If your application does not prioritize its own
503 * notifications, use this value for all notifications.
504 */
505 public static final int PRIORITY_DEFAULT = 0;
506
507 /**
508 * Lower {@link #priority}, for items that are less important. The UI may choose to show these
509 * items smaller, or at a different position in the list, compared with your app's
510 * {@link #PRIORITY_DEFAULT} items.
511 */
512 public static final int PRIORITY_LOW = -1;
513
514 /**
515 * Lowest {@link #priority}; these items might not be shown to the user except under special
516 * circumstances, such as detailed notification logs.
517 */
518 public static final int PRIORITY_MIN = -2;
519
520 /**
521 * Higher {@link #priority}, for more important notifications or alerts. The UI may choose to
522 * show these items larger, or at a different position in notification lists, compared with
523 * your app's {@link #PRIORITY_DEFAULT} items.
524 */
525 public static final int PRIORITY_HIGH = 1;
526
527 /**
528 * Highest {@link #priority}, for your application's most important items that require the
529 * user's prompt attention or input.
530 */
531 public static final int PRIORITY_MAX = 2;
532
533 /**
534 * Relative priority for this notification.
Joe Malin8d40d042012-11-05 11:36:40 -0800535 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500536 * Priority is an indication of how much of the user's valuable attention should be consumed by
537 * this notification. Low-priority notifications may be hidden from the user in certain
538 * situations, while the user might be interrupted for a higher-priority notification. The
Daniel Sandler6738eee2012-11-16 12:03:32 -0500539 * system will make a determination about how to interpret this priority when presenting
540 * the notification.
Chris Wren47c20a12014-06-18 17:27:29 -0400541 *
542 * <p>
543 * A notification that is at least {@link #PRIORITY_HIGH} is more likely to be presented
544 * as a heads-up notification.
545 * </p>
546 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500547 */
Tor Norbyed9273d62013-05-30 15:59:53 -0700548 @Priority
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500549 public int priority;
Joe Malin8d40d042012-11-05 11:36:40 -0800550
Dan Sandler26e81cf2014-05-06 10:01:27 -0400551 /**
552 * Accent color (an ARGB integer like the constants in {@link android.graphics.Color})
553 * to be applied by the standard Style templates when presenting this notification.
554 *
555 * The current template design constructs a colorful header image by overlaying the
556 * {@link #icon} image (stenciled in white) atop a field of this color. Alpha components are
557 * ignored.
558 */
Tor Norbye80756e32015-03-02 09:39:27 -0800559 @ColorInt
Dan Sandler26e81cf2014-05-06 10:01:27 -0400560 public int color = COLOR_DEFAULT;
561
562 /**
563 * Special value of {@link #color} telling the system not to decorate this notification with
564 * any special color but instead use default colors when presenting this notification.
565 */
Tor Norbye80756e32015-03-02 09:39:27 -0800566 @ColorInt
Dan Sandler26e81cf2014-05-06 10:01:27 -0400567 public static final int COLOR_DEFAULT = 0; // AKA Color.TRANSPARENT
Dan Sandler0bf2ed82013-12-21 23:33:41 -0600568
569 /**
Adrian Roos4ff3b122016-02-01 12:26:13 -0800570 * Special value of {@link #color} used as a place holder for an invalid color.
571 */
572 @ColorInt
573 private static final int COLOR_INVALID = 1;
574
575 /**
Dan Sandler0bf2ed82013-12-21 23:33:41 -0600576 * Sphere of visibility of this notification, which affects how and when the SystemUI reveals
577 * the notification's presence and contents in untrusted situations (namely, on the secure
578 * lockscreen).
579 *
580 * The default level, {@link #VISIBILITY_PRIVATE}, behaves exactly as notifications have always
581 * done on Android: The notification's {@link #icon} and {@link #tickerText} (if available) are
582 * shown in all situations, but the contents are only available if the device is unlocked for
583 * the appropriate user.
584 *
585 * A more permissive policy can be expressed by {@link #VISIBILITY_PUBLIC}; such a notification
586 * can be read even in an "insecure" context (that is, above a secure lockscreen).
587 * To modify the public version of this notification—for example, to redact some portions—see
588 * {@link Builder#setPublicVersion(Notification)}.
589 *
590 * Finally, a notification can be made {@link #VISIBILITY_SECRET}, which will suppress its icon
591 * and ticker until the user has bypassed the lockscreen.
592 */
593 public int visibility;
594
Griff Hazenfc3922d2014-08-20 11:56:44 -0700595 /**
596 * Notification visibility: Show this notification in its entirety on all lockscreens.
597 *
598 * {@see #visibility}
599 */
Dan Sandler0bf2ed82013-12-21 23:33:41 -0600600 public static final int VISIBILITY_PUBLIC = 1;
Griff Hazenfc3922d2014-08-20 11:56:44 -0700601
602 /**
603 * Notification visibility: Show this notification on all lockscreens, but conceal sensitive or
604 * private information on secure lockscreens.
605 *
606 * {@see #visibility}
607 */
Dan Sandler0bf2ed82013-12-21 23:33:41 -0600608 public static final int VISIBILITY_PRIVATE = 0;
Griff Hazenfc3922d2014-08-20 11:56:44 -0700609
610 /**
611 * Notification visibility: Do not reveal any part of this notification on a secure lockscreen.
612 *
613 * {@see #visibility}
614 */
Dan Sandler0bf2ed82013-12-21 23:33:41 -0600615 public static final int VISIBILITY_SECRET = -1;
616
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500617 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400618 * Notification category: incoming call (voice or video) or similar synchronous communication request.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500619 */
John Spurlockfd7f1e02014-03-18 16:41:57 -0400620 public static final String CATEGORY_CALL = "call";
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500621
622 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400623 * Notification category: incoming direct message (SMS, instant message, etc.).
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500624 */
John Spurlockfd7f1e02014-03-18 16:41:57 -0400625 public static final String CATEGORY_MESSAGE = "msg";
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500626
627 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400628 * Notification category: asynchronous bulk message (email).
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500629 */
John Spurlockfd7f1e02014-03-18 16:41:57 -0400630 public static final String CATEGORY_EMAIL = "email";
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500631
632 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400633 * Notification category: calendar event.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500634 */
John Spurlockfd7f1e02014-03-18 16:41:57 -0400635 public static final String CATEGORY_EVENT = "event";
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500636
637 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400638 * Notification category: promotion or advertisement.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500639 */
John Spurlockfd7f1e02014-03-18 16:41:57 -0400640 public static final String CATEGORY_PROMO = "promo";
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500641
642 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400643 * Notification category: alarm or timer.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500644 */
John Spurlockfd7f1e02014-03-18 16:41:57 -0400645 public static final String CATEGORY_ALARM = "alarm";
646
647 /**
648 * Notification category: progress of a long-running background operation.
649 */
650 public static final String CATEGORY_PROGRESS = "progress";
651
652 /**
653 * Notification category: social network or sharing update.
654 */
655 public static final String CATEGORY_SOCIAL = "social";
656
657 /**
658 * Notification category: error in background operation or authentication status.
659 */
660 public static final String CATEGORY_ERROR = "err";
661
662 /**
663 * Notification category: media transport control for playback.
664 */
665 public static final String CATEGORY_TRANSPORT = "transport";
666
667 /**
668 * Notification category: system or device status update. Reserved for system use.
669 */
670 public static final String CATEGORY_SYSTEM = "sys";
671
672 /**
673 * Notification category: indication of running background service.
674 */
675 public static final String CATEGORY_SERVICE = "service";
676
677 /**
John Spurlock0a69c8c2014-03-21 13:30:57 -0400678 * Notification category: a specific, timely recommendation for a single thing.
679 * For example, a news app might want to recommend a news story it believes the user will
680 * want to read next.
681 */
682 public static final String CATEGORY_RECOMMENDATION = "recommendation";
683
684 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400685 * Notification category: ongoing information about device or contextual status.
686 */
687 public static final String CATEGORY_STATUS = "status";
688
689 /**
John Spurlock24d3dad2015-04-02 12:24:02 -0400690 * Notification category: user-scheduled reminder.
691 */
692 public static final String CATEGORY_REMINDER = "reminder";
693
694 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400695 * One of the predefined notification categories (see the <code>CATEGORY_*</code> constants)
696 * that best describes this Notification. May be used by the system for ranking and filtering.
697 */
698 public String category;
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500699
Griff Hazen5cadc3b2014-05-20 09:55:39 -0700700 private String mGroupKey;
701
702 /**
703 * Get the key used to group this notification into a cluster or stack
704 * with other notifications on devices which support such rendering.
705 */
706 public String getGroup() {
707 return mGroupKey;
708 }
709
710 private String mSortKey;
711
712 /**
713 * Get a sort key that orders this notification among other notifications from the
714 * same package. This can be useful if an external sort was already applied and an app
715 * would like to preserve this. Notifications will be sorted lexicographically using this
716 * value, although providing different priorities in addition to providing sort key may
717 * cause this value to be ignored.
718 *
719 * <p>This sort key can also be used to order members of a notification group. See
720 * {@link Builder#setGroup}.
721 *
722 * @see String#compareTo(String)
723 */
724 public String getSortKey() {
725 return mSortKey;
726 }
727
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500728 /**
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400729 * Additional semantic data to be carried around with this Notification.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400730 * <p>
731 * The extras keys defined here are intended to capture the original inputs to {@link Builder}
732 * APIs, and are intended to be used by
733 * {@link android.service.notification.NotificationListenerService} implementations to extract
734 * detailed information from notification objects.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500735 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400736 public Bundle extras = new Bundle();
737
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400738 /**
739 * {@link #extras} key: this is the title of the notification,
740 * as supplied to {@link Builder#setContentTitle(CharSequence)}.
741 */
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -0500742 public static final String EXTRA_TITLE = "android.title";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400743
744 /**
745 * {@link #extras} key: this is the title of the notification when shown in expanded form,
746 * e.g. as supplied to {@link BigTextStyle#setBigContentTitle(CharSequence)}.
747 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400748 public static final String EXTRA_TITLE_BIG = EXTRA_TITLE + ".big";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400749
750 /**
751 * {@link #extras} key: this is the main text payload, as supplied to
752 * {@link Builder#setContentText(CharSequence)}.
753 */
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -0500754 public static final String EXTRA_TEXT = "android.text";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400755
756 /**
757 * {@link #extras} key: this is a third line of text, as supplied to
758 * {@link Builder#setSubText(CharSequence)}.
759 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400760 public static final String EXTRA_SUB_TEXT = "android.subText";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400761
762 /**
Adrian Roose458aa82015-12-08 16:17:19 -0800763 * {@link #extras} key: this is the remote input history, as supplied to
764 * {@link Builder#setRemoteInputHistory(CharSequence[])}.
765 */
766 public static final String EXTRA_REMOTE_INPUT_HISTORY = "android.remoteInputHistory";
767
768 /**
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400769 * {@link #extras} key: this is a small piece of additional text as supplied to
770 * {@link Builder#setContentInfo(CharSequence)}.
771 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400772 public static final String EXTRA_INFO_TEXT = "android.infoText";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400773
774 /**
775 * {@link #extras} key: this is a line of summary information intended to be shown
776 * alongside expanded notifications, as supplied to (e.g.)
777 * {@link BigTextStyle#setSummaryText(CharSequence)}.
778 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400779 public static final String EXTRA_SUMMARY_TEXT = "android.summaryText";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400780
781 /**
Christoph Studer4600f9b2014-07-22 22:44:43 +0200782 * {@link #extras} key: this is the longer text shown in the big form of a
783 * {@link BigTextStyle} notification, as supplied to
784 * {@link BigTextStyle#bigText(CharSequence)}.
785 */
786 public static final String EXTRA_BIG_TEXT = "android.bigText";
787
788 /**
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400789 * {@link #extras} key: this is the resource ID of the notification's main small icon, as
790 * supplied to {@link Builder#setSmallIcon(int)}.
791 */
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -0500792 public static final String EXTRA_SMALL_ICON = "android.icon";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400793
794 /**
795 * {@link #extras} key: this is a bitmap to be used instead of the small icon when showing the
796 * notification payload, as
797 * supplied to {@link Builder#setLargeIcon(android.graphics.Bitmap)}.
798 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400799 public static final String EXTRA_LARGE_ICON = "android.largeIcon";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400800
801 /**
802 * {@link #extras} key: this is a bitmap to be used instead of the one from
803 * {@link Builder#setLargeIcon(android.graphics.Bitmap)} when the notification is
804 * shown in its expanded form, as supplied to
805 * {@link BigPictureStyle#bigLargeIcon(android.graphics.Bitmap)}.
806 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400807 public static final String EXTRA_LARGE_ICON_BIG = EXTRA_LARGE_ICON + ".big";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400808
809 /**
810 * {@link #extras} key: this is the progress value supplied to
811 * {@link Builder#setProgress(int, int, boolean)}.
812 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400813 public static final String EXTRA_PROGRESS = "android.progress";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400814
815 /**
816 * {@link #extras} key: this is the maximum value supplied to
817 * {@link Builder#setProgress(int, int, boolean)}.
818 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400819 public static final String EXTRA_PROGRESS_MAX = "android.progressMax";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400820
821 /**
822 * {@link #extras} key: whether the progress bar is indeterminate, supplied to
823 * {@link Builder#setProgress(int, int, boolean)}.
824 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400825 public static final String EXTRA_PROGRESS_INDETERMINATE = "android.progressIndeterminate";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400826
827 /**
828 * {@link #extras} key: whether {@link #when} should be shown as a count-up timer (specifically
829 * a {@link android.widget.Chronometer}) instead of a timestamp, as supplied to
830 * {@link Builder#setUsesChronometer(boolean)}.
831 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400832 public static final String EXTRA_SHOW_CHRONOMETER = "android.showChronometer";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400833
834 /**
Selim Cinek81c23aa2016-02-25 16:23:13 -0800835 * {@link #extras} key: whether the chronometer set on the notification should count down
836 * instead of counting up. Is only relevant if key {@link #EXTRA_SHOW_CHRONOMETER} is present.
837 */
838 public static final String EXTRA_CHRONOMETER_COUNTS_DOWN = "android.chronometerCountsDown";
839
840 /**
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400841 * {@link #extras} key: whether {@link #when} should be shown,
842 * as supplied to {@link Builder#setShowWhen(boolean)}.
843 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400844 public static final String EXTRA_SHOW_WHEN = "android.showWhen";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400845
846 /**
847 * {@link #extras} key: this is a bitmap to be shown in {@link BigPictureStyle} expanded
848 * notifications, supplied to {@link BigPictureStyle#bigPicture(android.graphics.Bitmap)}.
849 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400850 public static final String EXTRA_PICTURE = "android.picture";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400851
852 /**
853 * {@link #extras} key: An array of CharSequences to show in {@link InboxStyle} expanded
854 * notifications, each of which was supplied to {@link InboxStyle#addLine(CharSequence)}.
855 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400856 public static final String EXTRA_TEXT_LINES = "android.textLines";
Dan Sandler842dd772014-05-15 09:36:47 -0400857
858 /**
859 * {@link #extras} key: A string representing the name of the specific
860 * {@link android.app.Notification.Style} used to create this notification.
861 */
Chris Wren91ad5632013-06-05 15:05:57 -0400862 public static final String EXTRA_TEMPLATE = "android.template";
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400863
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400864 /**
Chris Wrene6c48932014-09-29 17:19:27 -0400865 * {@link #extras} key: A String array containing the people that this notification relates to,
866 * each of which was supplied to {@link Builder#addPerson(String)}.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400867 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400868 public static final String EXTRA_PEOPLE = "android.people";
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500869
870 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400871 * Allow certain system-generated notifications to appear before the device is provisioned.
872 * Only available to notifications coming from the android package.
873 * @hide
874 */
875 public static final String EXTRA_ALLOW_DURING_SETUP = "android.allowDuringSetup";
876
877 /**
Jose Limae9e3b3b2014-05-18 23:44:50 -0700878 * {@link #extras} key: A
879 * {@link android.content.ContentUris content URI} pointing to an image that can be displayed
880 * in the background when the notification is selected. The URI must point to an image stream
881 * suitable for passing into
882 * {@link android.graphics.BitmapFactory#decodeStream(java.io.InputStream)
883 * BitmapFactory.decodeStream}; all other content types will be ignored. The content provider
884 * URI used for this purpose must require no permissions to read the image data.
885 */
886 public static final String EXTRA_BACKGROUND_IMAGE_URI = "android.backgroundImageUri";
887
888 /**
Dan Sandler842dd772014-05-15 09:36:47 -0400889 * {@link #extras} key: A
Jeff Browndba34ba2014-06-24 20:46:03 -0700890 * {@link android.media.session.MediaSession.Token} associated with a
Dan Sandler842dd772014-05-15 09:36:47 -0400891 * {@link android.app.Notification.MediaStyle} notification.
892 */
893 public static final String EXTRA_MEDIA_SESSION = "android.mediaSession";
894
895 /**
Bryan Mawhinneye191f902014-07-22 12:50:09 +0100896 * {@link #extras} key: the indices of actions to be shown in the compact view,
897 * as supplied to (e.g.) {@link MediaStyle#setShowActionsInCompactView(int...)}.
898 */
899 public static final String EXTRA_COMPACT_ACTIONS = "android.compactActions";
900
Christoph Studer943aa672014-08-03 20:31:16 +0200901 /**
Kenny Guy8942bcd2014-09-08 21:09:47 +0100902 * {@link #extras} key: the user that built the notification.
903 *
904 * @hide
905 */
906 public static final String EXTRA_ORIGINATING_USERID = "android.originatingUserId";
907
908 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400909 * @hide
910 */
911 public static final String EXTRA_BUILDER_APPLICATION_INFO = "android.appInfo";
912
Selim Cinek247fa012016-02-18 09:50:48 -0800913 /**
914 * @hide
915 */
916 public static final String EXTRA_CONTAINS_CUSTOM_VIEW = "android.contains.customView";
917
Dan Sandlerd63f9322015-05-06 15:18:49 -0400918 private Icon mSmallIcon;
919 private Icon mLargeIcon;
920
Chris Wren51c75102013-07-16 20:49:17 -0400921 /**
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400922 * Structure to encapsulate a named action that can be shown as part of this notification.
923 * It must include an icon, a label, and a {@link PendingIntent} to be fired when the action is
924 * selected by the user.
925 * <p>
Griff Hazen959591e2014-05-15 22:26:18 -0700926 * Apps should use {@link Notification.Builder#addAction(int, CharSequence, PendingIntent)}
927 * or {@link Notification.Builder#addAction(Notification.Action)}
928 * to attach actions.
Daniel Sandlera0a938c2012-03-15 08:42:37 -0400929 */
Daniel Sandlerea2a3172013-02-20 22:24:20 -0500930 public static class Action implements Parcelable {
Griff Hazen959591e2014-05-15 22:26:18 -0700931 private final Bundle mExtras;
Dan Sandler86647982015-05-13 23:41:13 -0400932 private Icon mIcon;
Griff Hazen61a9e862014-05-22 16:05:19 -0700933 private final RemoteInput[] mRemoteInputs;
Griff Hazen959591e2014-05-15 22:26:18 -0700934
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400935 /**
936 * Small icon representing the action.
Dan Sandler86647982015-05-13 23:41:13 -0400937 *
938 * @deprecated Use {@link Action#getIcon()} instead.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400939 */
Dan Sandler86647982015-05-13 23:41:13 -0400940 @Deprecated
Daniel Sandlera0a938c2012-03-15 08:42:37 -0400941 public int icon;
Griff Hazen5cadc3b2014-05-20 09:55:39 -0700942
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400943 /**
944 * Title of the action.
945 */
Daniel Sandlera0a938c2012-03-15 08:42:37 -0400946 public CharSequence title;
Griff Hazen5cadc3b2014-05-20 09:55:39 -0700947
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400948 /**
949 * Intent to send when the user invokes this action. May be null, in which case the action
950 * may be rendered in a disabled presentation by the system UI.
951 */
Daniel Sandlera0a938c2012-03-15 08:42:37 -0400952 public PendingIntent actionIntent;
Griff Hazen959591e2014-05-15 22:26:18 -0700953
Daniel Sandlera0a938c2012-03-15 08:42:37 -0400954 private Action(Parcel in) {
Dan Sandler86647982015-05-13 23:41:13 -0400955 if (in.readInt() != 0) {
956 mIcon = Icon.CREATOR.createFromParcel(in);
Dan Sandler68079d52015-07-22 10:45:30 -0400957 if (mIcon.getType() == Icon.TYPE_RESOURCE) {
958 icon = mIcon.getResId();
959 }
Dan Sandler86647982015-05-13 23:41:13 -0400960 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -0400961 title = TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(in);
962 if (in.readInt() == 1) {
963 actionIntent = PendingIntent.CREATOR.createFromParcel(in);
964 }
Jeff Sharkeya04c7a72016-03-18 12:20:36 -0600965 mExtras = Bundle.setDefusable(in.readBundle(), true);
Griff Hazen5cadc3b2014-05-20 09:55:39 -0700966 mRemoteInputs = in.createTypedArray(RemoteInput.CREATOR);
Daniel Sandlera0a938c2012-03-15 08:42:37 -0400967 }
Griff Hazen5cadc3b2014-05-20 09:55:39 -0700968
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400969 /**
Dan Sandler86647982015-05-13 23:41:13 -0400970 * @deprecated Use {@link android.app.Notification.Action.Builder}.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400971 */
Dan Sandler86647982015-05-13 23:41:13 -0400972 @Deprecated
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400973 public Action(int icon, CharSequence title, PendingIntent intent) {
Dan Sandler86647982015-05-13 23:41:13 -0400974 this(Icon.createWithResource("", icon), title, intent, new Bundle(), null);
Griff Hazen959591e2014-05-15 22:26:18 -0700975 }
976
Dan Sandler86647982015-05-13 23:41:13 -0400977 private Action(Icon icon, CharSequence title, PendingIntent intent, Bundle extras,
Griff Hazen5cadc3b2014-05-20 09:55:39 -0700978 RemoteInput[] remoteInputs) {
Dan Sandler86647982015-05-13 23:41:13 -0400979 this.mIcon = icon;
Gus Prevasf5bff46f2015-08-24 10:34:28 -0400980 if (icon != null && icon.getType() == Icon.TYPE_RESOURCE) {
981 this.icon = icon.getResId();
982 }
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400983 this.title = title;
984 this.actionIntent = intent;
Griff Hazen959591e2014-05-15 22:26:18 -0700985 this.mExtras = extras != null ? extras : new Bundle();
Griff Hazen5cadc3b2014-05-20 09:55:39 -0700986 this.mRemoteInputs = remoteInputs;
Griff Hazen959591e2014-05-15 22:26:18 -0700987 }
988
989 /**
Dan Sandler86647982015-05-13 23:41:13 -0400990 * Return an icon representing the action.
991 */
992 public Icon getIcon() {
993 if (mIcon == null && icon != 0) {
994 // you snuck an icon in here without using the builder; let's try to keep it
995 mIcon = Icon.createWithResource("", icon);
996 }
997 return mIcon;
998 }
999
1000 /**
Griff Hazen959591e2014-05-15 22:26:18 -07001001 * Get additional metadata carried around with this Action.
1002 */
1003 public Bundle getExtras() {
1004 return mExtras;
1005 }
1006
1007 /**
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001008 * Get the list of inputs to be collected from the user when this action is sent.
1009 * May return null if no remote inputs were added.
1010 */
1011 public RemoteInput[] getRemoteInputs() {
1012 return mRemoteInputs;
1013 }
1014
1015 /**
Griff Hazen959591e2014-05-15 22:26:18 -07001016 * Builder class for {@link Action} objects.
1017 */
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001018 public static final class Builder {
Dan Sandler86647982015-05-13 23:41:13 -04001019 private final Icon mIcon;
Griff Hazen959591e2014-05-15 22:26:18 -07001020 private final CharSequence mTitle;
1021 private final PendingIntent mIntent;
1022 private final Bundle mExtras;
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001023 private ArrayList<RemoteInput> mRemoteInputs;
Griff Hazen959591e2014-05-15 22:26:18 -07001024
1025 /**
1026 * Construct a new builder for {@link Action} object.
1027 * @param icon icon to show for this action
1028 * @param title the title of the action
1029 * @param intent the {@link PendingIntent} to fire when users trigger this action
1030 */
Dan Sandler86647982015-05-13 23:41:13 -04001031 @Deprecated
Griff Hazen959591e2014-05-15 22:26:18 -07001032 public Builder(int icon, CharSequence title, PendingIntent intent) {
Dan Sandler86647982015-05-13 23:41:13 -04001033 this(Icon.createWithResource("", icon), title, intent, new Bundle(), null);
1034 }
1035
1036 /**
1037 * Construct a new builder for {@link Action} object.
1038 * @param icon icon to show for this action
1039 * @param title the title of the action
1040 * @param intent the {@link PendingIntent} to fire when users trigger this action
1041 */
1042 public Builder(Icon icon, CharSequence title, PendingIntent intent) {
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001043 this(icon, title, intent, new Bundle(), null);
Griff Hazen959591e2014-05-15 22:26:18 -07001044 }
1045
1046 /**
1047 * Construct a new builder for {@link Action} object using the fields from an
1048 * {@link Action}.
1049 * @param action the action to read fields from.
1050 */
1051 public Builder(Action action) {
Dan Sandler86647982015-05-13 23:41:13 -04001052 this(action.getIcon(), action.title, action.actionIntent, new Bundle(action.mExtras),
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001053 action.getRemoteInputs());
Griff Hazen959591e2014-05-15 22:26:18 -07001054 }
1055
Dan Sandler86647982015-05-13 23:41:13 -04001056 private Builder(Icon icon, CharSequence title, PendingIntent intent, Bundle extras,
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001057 RemoteInput[] remoteInputs) {
Griff Hazen959591e2014-05-15 22:26:18 -07001058 mIcon = icon;
1059 mTitle = title;
1060 mIntent = intent;
1061 mExtras = extras;
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001062 if (remoteInputs != null) {
1063 mRemoteInputs = new ArrayList<RemoteInput>(remoteInputs.length);
1064 Collections.addAll(mRemoteInputs, remoteInputs);
1065 }
Griff Hazen959591e2014-05-15 22:26:18 -07001066 }
1067
1068 /**
1069 * Merge additional metadata into this builder.
1070 *
1071 * <p>Values within the Bundle will replace existing extras values in this Builder.
1072 *
1073 * @see Notification.Action#extras
1074 */
1075 public Builder addExtras(Bundle extras) {
1076 if (extras != null) {
1077 mExtras.putAll(extras);
1078 }
1079 return this;
1080 }
1081
1082 /**
1083 * Get the metadata Bundle used by this Builder.
1084 *
1085 * <p>The returned Bundle is shared with this Builder.
1086 */
1087 public Bundle getExtras() {
1088 return mExtras;
1089 }
1090
1091 /**
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001092 * Add an input to be collected from the user when this action is sent.
1093 * Response values can be retrieved from the fired intent by using the
1094 * {@link RemoteInput#getResultsFromIntent} function.
1095 * @param remoteInput a {@link RemoteInput} to add to the action
1096 * @return this object for method chaining
1097 */
1098 public Builder addRemoteInput(RemoteInput remoteInput) {
1099 if (mRemoteInputs == null) {
1100 mRemoteInputs = new ArrayList<RemoteInput>();
1101 }
1102 mRemoteInputs.add(remoteInput);
1103 return this;
1104 }
1105
1106 /**
1107 * Apply an extender to this action builder. Extenders may be used to add
1108 * metadata or change options on this builder.
1109 */
Griff Hazen61a9e862014-05-22 16:05:19 -07001110 public Builder extend(Extender extender) {
1111 extender.extend(this);
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001112 return this;
1113 }
1114
1115 /**
Griff Hazen959591e2014-05-15 22:26:18 -07001116 * Combine all of the options that have been set and return a new {@link Action}
1117 * object.
1118 * @return the built action
1119 */
1120 public Action build() {
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001121 RemoteInput[] remoteInputs = mRemoteInputs != null
1122 ? mRemoteInputs.toArray(new RemoteInput[mRemoteInputs.size()]) : null;
1123 return new Action(mIcon, mTitle, mIntent, mExtras, remoteInputs);
Griff Hazen959591e2014-05-15 22:26:18 -07001124 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001125 }
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001126
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001127 @Override
1128 public Action clone() {
1129 return new Action(
Dan Sandler86647982015-05-13 23:41:13 -04001130 getIcon(),
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001131 title,
1132 actionIntent, // safe to alias
1133 new Bundle(mExtras),
1134 getRemoteInputs());
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001135 }
1136 @Override
1137 public int describeContents() {
1138 return 0;
1139 }
1140 @Override
1141 public void writeToParcel(Parcel out, int flags) {
Dan Sandler86647982015-05-13 23:41:13 -04001142 final Icon ic = getIcon();
1143 if (ic != null) {
1144 out.writeInt(1);
1145 ic.writeToParcel(out, 0);
1146 } else {
1147 out.writeInt(0);
1148 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001149 TextUtils.writeToParcel(title, out, flags);
1150 if (actionIntent != null) {
1151 out.writeInt(1);
1152 actionIntent.writeToParcel(out, flags);
1153 } else {
1154 out.writeInt(0);
1155 }
Griff Hazen959591e2014-05-15 22:26:18 -07001156 out.writeBundle(mExtras);
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001157 out.writeTypedArray(mRemoteInputs, flags);
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001158 }
Griff Hazen959591e2014-05-15 22:26:18 -07001159 public static final Parcelable.Creator<Action> CREATOR =
1160 new Parcelable.Creator<Action>() {
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001161 public Action createFromParcel(Parcel in) {
1162 return new Action(in);
1163 }
1164 public Action[] newArray(int size) {
1165 return new Action[size];
1166 }
1167 };
Griff Hazen61a9e862014-05-22 16:05:19 -07001168
1169 /**
1170 * Extender interface for use with {@link Builder#extend}. Extenders may be used to add
1171 * metadata or change options on an action builder.
1172 */
1173 public interface Extender {
1174 /**
1175 * Apply this extender to a notification action builder.
1176 * @param builder the builder to be modified.
1177 * @return the build object for chaining.
1178 */
1179 public Builder extend(Builder builder);
1180 }
1181
1182 /**
1183 * Wearable extender for notification actions. To add extensions to an action,
1184 * create a new {@link android.app.Notification.Action.WearableExtender} object using
1185 * the {@code WearableExtender()} constructor and apply it to a
1186 * {@link android.app.Notification.Action.Builder} using
1187 * {@link android.app.Notification.Action.Builder#extend}.
1188 *
1189 * <pre class="prettyprint">
1190 * Notification.Action action = new Notification.Action.Builder(
1191 * R.drawable.archive_all, "Archive all", actionIntent)
Griff Hazen14f57992014-05-26 09:07:14 -07001192 * .extend(new Notification.Action.WearableExtender()
Griff Hazen61a9e862014-05-22 16:05:19 -07001193 * .setAvailableOffline(false))
Griff Hazen14f57992014-05-26 09:07:14 -07001194 * .build();</pre>
Griff Hazen61a9e862014-05-22 16:05:19 -07001195 */
1196 public static final class WearableExtender implements Extender {
1197 /** Notification action extra which contains wearable extensions */
1198 private static final String EXTRA_WEARABLE_EXTENSIONS = "android.wearable.EXTENSIONS";
1199
Pete Gastaf6781d2014-10-07 15:17:05 -04001200 // Keys within EXTRA_WEARABLE_EXTENSIONS for wearable options.
Griff Hazen61a9e862014-05-22 16:05:19 -07001201 private static final String KEY_FLAGS = "flags";
Pete Gastaf6781d2014-10-07 15:17:05 -04001202 private static final String KEY_IN_PROGRESS_LABEL = "inProgressLabel";
1203 private static final String KEY_CONFIRM_LABEL = "confirmLabel";
1204 private static final String KEY_CANCEL_LABEL = "cancelLabel";
Griff Hazen61a9e862014-05-22 16:05:19 -07001205
1206 // Flags bitwise-ored to mFlags
1207 private static final int FLAG_AVAILABLE_OFFLINE = 0x1;
Alex Hills9ab3a232016-04-05 14:54:56 -04001208 private static final int FLAG_HINT_LAUNCHES_ACTIVITY = 1 << 1;
Griff Hazen61a9e862014-05-22 16:05:19 -07001209
1210 // Default value for flags integer
1211 private static final int DEFAULT_FLAGS = FLAG_AVAILABLE_OFFLINE;
1212
1213 private int mFlags = DEFAULT_FLAGS;
1214
Pete Gastaf6781d2014-10-07 15:17:05 -04001215 private CharSequence mInProgressLabel;
1216 private CharSequence mConfirmLabel;
1217 private CharSequence mCancelLabel;
1218
Griff Hazen61a9e862014-05-22 16:05:19 -07001219 /**
1220 * Create a {@link android.app.Notification.Action.WearableExtender} with default
1221 * options.
1222 */
1223 public WearableExtender() {
1224 }
1225
1226 /**
1227 * Create a {@link android.app.Notification.Action.WearableExtender} by reading
1228 * wearable options present in an existing notification action.
1229 * @param action the notification action to inspect.
1230 */
1231 public WearableExtender(Action action) {
1232 Bundle wearableBundle = action.getExtras().getBundle(EXTRA_WEARABLE_EXTENSIONS);
1233 if (wearableBundle != null) {
1234 mFlags = wearableBundle.getInt(KEY_FLAGS, DEFAULT_FLAGS);
Pete Gastaf6781d2014-10-07 15:17:05 -04001235 mInProgressLabel = wearableBundle.getCharSequence(KEY_IN_PROGRESS_LABEL);
1236 mConfirmLabel = wearableBundle.getCharSequence(KEY_CONFIRM_LABEL);
1237 mCancelLabel = wearableBundle.getCharSequence(KEY_CANCEL_LABEL);
Griff Hazen61a9e862014-05-22 16:05:19 -07001238 }
1239 }
1240
1241 /**
1242 * Apply wearable extensions to a notification action that is being built. This is
1243 * typically called by the {@link android.app.Notification.Action.Builder#extend}
1244 * method of {@link android.app.Notification.Action.Builder}.
1245 */
1246 @Override
1247 public Action.Builder extend(Action.Builder builder) {
1248 Bundle wearableBundle = new Bundle();
1249
1250 if (mFlags != DEFAULT_FLAGS) {
1251 wearableBundle.putInt(KEY_FLAGS, mFlags);
1252 }
Pete Gastaf6781d2014-10-07 15:17:05 -04001253 if (mInProgressLabel != null) {
1254 wearableBundle.putCharSequence(KEY_IN_PROGRESS_LABEL, mInProgressLabel);
1255 }
1256 if (mConfirmLabel != null) {
1257 wearableBundle.putCharSequence(KEY_CONFIRM_LABEL, mConfirmLabel);
1258 }
1259 if (mCancelLabel != null) {
1260 wearableBundle.putCharSequence(KEY_CANCEL_LABEL, mCancelLabel);
1261 }
Griff Hazen61a9e862014-05-22 16:05:19 -07001262
1263 builder.getExtras().putBundle(EXTRA_WEARABLE_EXTENSIONS, wearableBundle);
1264 return builder;
1265 }
1266
1267 @Override
1268 public WearableExtender clone() {
1269 WearableExtender that = new WearableExtender();
1270 that.mFlags = this.mFlags;
Pete Gastaf6781d2014-10-07 15:17:05 -04001271 that.mInProgressLabel = this.mInProgressLabel;
1272 that.mConfirmLabel = this.mConfirmLabel;
1273 that.mCancelLabel = this.mCancelLabel;
Griff Hazen61a9e862014-05-22 16:05:19 -07001274 return that;
1275 }
1276
1277 /**
1278 * Set whether this action is available when the wearable device is not connected to
1279 * a companion device. The user can still trigger this action when the wearable device is
1280 * offline, but a visual hint will indicate that the action may not be available.
1281 * Defaults to true.
1282 */
1283 public WearableExtender setAvailableOffline(boolean availableOffline) {
1284 setFlag(FLAG_AVAILABLE_OFFLINE, availableOffline);
1285 return this;
1286 }
1287
1288 /**
1289 * Get whether this action is available when the wearable device is not connected to
1290 * a companion device. The user can still trigger this action when the wearable device is
1291 * offline, but a visual hint will indicate that the action may not be available.
1292 * Defaults to true.
1293 */
1294 public boolean isAvailableOffline() {
1295 return (mFlags & FLAG_AVAILABLE_OFFLINE) != 0;
1296 }
1297
1298 private void setFlag(int mask, boolean value) {
1299 if (value) {
1300 mFlags |= mask;
1301 } else {
1302 mFlags &= ~mask;
1303 }
1304 }
Pete Gastaf6781d2014-10-07 15:17:05 -04001305
1306 /**
1307 * Set a label to display while the wearable is preparing to automatically execute the
1308 * action. This is usually a 'ing' verb ending in ellipsis like "Sending..."
1309 *
1310 * @param label the label to display while the action is being prepared to execute
1311 * @return this object for method chaining
1312 */
1313 public WearableExtender setInProgressLabel(CharSequence label) {
1314 mInProgressLabel = label;
1315 return this;
1316 }
1317
1318 /**
1319 * Get the label to display while the wearable is preparing to automatically execute
1320 * the action. This is usually a 'ing' verb ending in ellipsis like "Sending..."
1321 *
1322 * @return the label to display while the action is being prepared to execute
1323 */
1324 public CharSequence getInProgressLabel() {
1325 return mInProgressLabel;
1326 }
1327
1328 /**
1329 * Set a label to display to confirm that the action should be executed.
1330 * This is usually an imperative verb like "Send".
1331 *
1332 * @param label the label to confirm the action should be executed
1333 * @return this object for method chaining
1334 */
1335 public WearableExtender setConfirmLabel(CharSequence label) {
1336 mConfirmLabel = label;
1337 return this;
1338 }
1339
1340 /**
1341 * Get the label to display to confirm that the action should be executed.
1342 * This is usually an imperative verb like "Send".
1343 *
1344 * @return the label to confirm the action should be executed
1345 */
1346 public CharSequence getConfirmLabel() {
1347 return mConfirmLabel;
1348 }
1349
1350 /**
1351 * Set a label to display to cancel the action.
1352 * This is usually an imperative verb, like "Cancel".
1353 *
1354 * @param label the label to display to cancel the action
1355 * @return this object for method chaining
1356 */
1357 public WearableExtender setCancelLabel(CharSequence label) {
1358 mCancelLabel = label;
1359 return this;
1360 }
1361
1362 /**
1363 * Get the label to display to cancel the action.
1364 * This is usually an imperative verb like "Cancel".
1365 *
1366 * @return the label to display to cancel the action
1367 */
1368 public CharSequence getCancelLabel() {
1369 return mCancelLabel;
1370 }
Alex Hills9ab3a232016-04-05 14:54:56 -04001371
1372 /**
1373 * Set a hint that this Action will launch an {@link Activity} directly, telling the
1374 * platform that it can generate the appropriate transitions.
1375 * @param hintLaunchesActivity {@code true} if the content intent will launch
1376 * an activity and transitions should be generated, false otherwise.
1377 * @return this object for method chaining
1378 */
1379 public WearableExtender setHintContentIntentLaunchesActivity(
1380 boolean hintLaunchesActivity) {
1381 setFlag(FLAG_HINT_LAUNCHES_ACTIVITY, hintLaunchesActivity);
1382 return this;
1383 }
1384
1385 /**
1386 * Get a hint that this Action will launch an {@link Activity} directly, telling the
1387 * platform that it can generate the appropriate transitions
1388 * @return {@code true} if the content intent will launch an activity and transitions
1389 * should be generated, false otherwise. The default value is {@code false} if this was
1390 * never set.
1391 */
1392 public boolean getHintContentIntentLaunchesActivity() {
1393 return (mFlags & FLAG_HINT_LAUNCHES_ACTIVITY) != 0;
1394 }
Griff Hazen61a9e862014-05-22 16:05:19 -07001395 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001396 }
1397
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001398 /**
1399 * Array of all {@link Action} structures attached to this notification by
1400 * {@link Builder#addAction(int, CharSequence, PendingIntent)}. Mostly useful for instances of
1401 * {@link android.service.notification.NotificationListenerService} that provide an alternative
1402 * interface for invoking actions.
1403 */
Daniel Sandlerea2a3172013-02-20 22:24:20 -05001404 public Action[] actions;
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001405
1406 /**
Dan Sandler0bf2ed82013-12-21 23:33:41 -06001407 * Replacement version of this notification whose content will be shown
1408 * in an insecure context such as atop a secure keyguard. See {@link #visibility}
1409 * and {@link #VISIBILITY_PUBLIC}.
1410 */
1411 public Notification publicVersion;
1412
1413 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001414 * Constructs a Notification object with default values.
Joe Onorato46439ce2010-11-19 13:56:21 -08001415 * You might want to consider using {@link Builder} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001416 */
1417 public Notification()
1418 {
1419 this.when = System.currentTimeMillis();
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001420 this.priority = PRIORITY_DEFAULT;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001421 }
1422
1423 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001424 * @hide
1425 */
1426 public Notification(Context context, int icon, CharSequence tickerText, long when,
1427 CharSequence contentTitle, CharSequence contentText, Intent contentIntent)
1428 {
Chris Wren1ce4b6d2015-06-11 10:19:43 -04001429 new Builder(context)
1430 .setWhen(when)
1431 .setSmallIcon(icon)
1432 .setTicker(tickerText)
1433 .setContentTitle(contentTitle)
1434 .setContentText(contentText)
1435 .setContentIntent(PendingIntent.getActivity(context, 0, contentIntent, 0))
1436 .buildInto(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001437 }
1438
1439 /**
1440 * Constructs a Notification object with the information needed to
1441 * have a status bar icon without the standard expanded view.
1442 *
1443 * @param icon The resource id of the icon to put in the status bar.
1444 * @param tickerText The text that flows by in the status bar when the notification first
1445 * activates.
1446 * @param when The time to show in the time field. In the System.currentTimeMillis
1447 * timebase.
Joe Onorato46439ce2010-11-19 13:56:21 -08001448 *
1449 * @deprecated Use {@link Builder} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001450 */
Joe Onorato46439ce2010-11-19 13:56:21 -08001451 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001452 public Notification(int icon, CharSequence tickerText, long when)
1453 {
1454 this.icon = icon;
1455 this.tickerText = tickerText;
1456 this.when = when;
1457 }
1458
1459 /**
1460 * Unflatten the notification from a parcel.
1461 */
1462 public Notification(Parcel parcel)
1463 {
1464 int version = parcel.readInt();
1465
1466 when = parcel.readLong();
Dan Sandler3936e7a2015-05-19 20:59:12 -04001467 if (parcel.readInt() != 0) {
1468 mSmallIcon = Icon.CREATOR.createFromParcel(parcel);
Dan Sandler4e787062015-06-17 15:09:48 -04001469 if (mSmallIcon.getType() == Icon.TYPE_RESOURCE) {
1470 icon = mSmallIcon.getResId();
1471 }
Dan Sandler3936e7a2015-05-19 20:59:12 -04001472 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001473 number = parcel.readInt();
1474 if (parcel.readInt() != 0) {
1475 contentIntent = PendingIntent.CREATOR.createFromParcel(parcel);
1476 }
1477 if (parcel.readInt() != 0) {
1478 deleteIntent = PendingIntent.CREATOR.createFromParcel(parcel);
1479 }
1480 if (parcel.readInt() != 0) {
1481 tickerText = TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(parcel);
1482 }
1483 if (parcel.readInt() != 0) {
Joe Onorato46439ce2010-11-19 13:56:21 -08001484 tickerView = RemoteViews.CREATOR.createFromParcel(parcel);
Joe Onoratoef1e7762010-09-17 18:38:38 -04001485 }
1486 if (parcel.readInt() != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001487 contentView = RemoteViews.CREATOR.createFromParcel(parcel);
1488 }
Joe Onorato561d3852010-11-20 18:09:34 -08001489 if (parcel.readInt() != 0) {
Dan Sandlerd63f9322015-05-06 15:18:49 -04001490 mLargeIcon = Icon.CREATOR.createFromParcel(parcel);
Joe Onorato561d3852010-11-20 18:09:34 -08001491 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001492 defaults = parcel.readInt();
1493 flags = parcel.readInt();
1494 if (parcel.readInt() != 0) {
1495 sound = Uri.CREATOR.createFromParcel(parcel);
1496 }
1497
1498 audioStreamType = parcel.readInt();
John Spurlockc0650f022014-07-19 13:22:39 -04001499 if (parcel.readInt() != 0) {
1500 audioAttributes = AudioAttributes.CREATOR.createFromParcel(parcel);
1501 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001502 vibrate = parcel.createLongArray();
1503 ledARGB = parcel.readInt();
1504 ledOnMS = parcel.readInt();
1505 ledOffMS = parcel.readInt();
1506 iconLevel = parcel.readInt();
Daniel Sandlere46cbd32010-06-17 10:35:26 -04001507
1508 if (parcel.readInt() != 0) {
1509 fullScreenIntent = PendingIntent.CREATOR.createFromParcel(parcel);
1510 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001511
1512 priority = parcel.readInt();
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001513
John Spurlockfd7f1e02014-03-18 16:41:57 -04001514 category = parcel.readString();
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001515
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001516 mGroupKey = parcel.readString();
1517
1518 mSortKey = parcel.readString();
1519
Jeff Sharkeya04c7a72016-03-18 12:20:36 -06001520 extras = Bundle.setDefusable(parcel.readBundle(), true); // may be null
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001521
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001522 actions = parcel.createTypedArray(Action.CREATOR); // may be null
1523
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001524 if (parcel.readInt() != 0) {
Daniel Sandlerf3b73432012-03-27 15:01:25 -04001525 bigContentView = RemoteViews.CREATOR.createFromParcel(parcel);
1526 }
Dan Sandler0bf2ed82013-12-21 23:33:41 -06001527
Chris Wren8fd39ec2014-02-27 17:43:26 -05001528 if (parcel.readInt() != 0) {
1529 headsUpContentView = RemoteViews.CREATOR.createFromParcel(parcel);
1530 }
1531
Dan Sandler0bf2ed82013-12-21 23:33:41 -06001532 visibility = parcel.readInt();
1533
1534 if (parcel.readInt() != 0) {
1535 publicVersion = Notification.CREATOR.createFromParcel(parcel);
1536 }
Dan Sandler26e81cf2014-05-06 10:01:27 -04001537
1538 color = parcel.readInt();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001539 }
1540
Andy Stadler110988c2010-12-03 14:29:16 -08001541 @Override
Joe Onorato18e69df2010-05-17 22:26:12 -07001542 public Notification clone() {
1543 Notification that = new Notification();
Daniel Sandler1a497d32013-04-18 14:52:45 -04001544 cloneInto(that, true);
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001545 return that;
1546 }
Joe Onorato18e69df2010-05-17 22:26:12 -07001547
Daniel Sandler1a497d32013-04-18 14:52:45 -04001548 /**
1549 * Copy all (or if heavy is false, all except Bitmaps and RemoteViews) members
1550 * of this into that.
1551 * @hide
1552 */
1553 public void cloneInto(Notification that, boolean heavy) {
Joe Onorato18e69df2010-05-17 22:26:12 -07001554 that.when = this.when;
Dan Sandlerd63f9322015-05-06 15:18:49 -04001555 that.mSmallIcon = this.mSmallIcon;
Joe Onorato18e69df2010-05-17 22:26:12 -07001556 that.number = this.number;
1557
1558 // PendingIntents are global, so there's no reason (or way) to clone them.
1559 that.contentIntent = this.contentIntent;
1560 that.deleteIntent = this.deleteIntent;
Daniel Sandlere46cbd32010-06-17 10:35:26 -04001561 that.fullScreenIntent = this.fullScreenIntent;
Joe Onorato18e69df2010-05-17 22:26:12 -07001562
1563 if (this.tickerText != null) {
1564 that.tickerText = this.tickerText.toString();
1565 }
Daniel Sandler1a497d32013-04-18 14:52:45 -04001566 if (heavy && this.tickerView != null) {
Joe Onorato46439ce2010-11-19 13:56:21 -08001567 that.tickerView = this.tickerView.clone();
Joe Onoratoef1e7762010-09-17 18:38:38 -04001568 }
Daniel Sandler1a497d32013-04-18 14:52:45 -04001569 if (heavy && this.contentView != null) {
Joe Onorato18e69df2010-05-17 22:26:12 -07001570 that.contentView = this.contentView.clone();
1571 }
Dan Sandlerd63f9322015-05-06 15:18:49 -04001572 if (heavy && this.mLargeIcon != null) {
1573 that.mLargeIcon = this.mLargeIcon;
Joe Onorato561d3852010-11-20 18:09:34 -08001574 }
Jozef BABJAKa8b91832011-02-22 08:05:08 +01001575 that.iconLevel = this.iconLevel;
Joe Onorato18e69df2010-05-17 22:26:12 -07001576 that.sound = this.sound; // android.net.Uri is immutable
1577 that.audioStreamType = this.audioStreamType;
John Spurlockc0650f022014-07-19 13:22:39 -04001578 if (this.audioAttributes != null) {
1579 that.audioAttributes = new AudioAttributes.Builder(this.audioAttributes).build();
1580 }
Joe Onorato18e69df2010-05-17 22:26:12 -07001581
1582 final long[] vibrate = this.vibrate;
1583 if (vibrate != null) {
1584 final int N = vibrate.length;
1585 final long[] vib = that.vibrate = new long[N];
1586 System.arraycopy(vibrate, 0, vib, 0, N);
1587 }
1588
1589 that.ledARGB = this.ledARGB;
1590 that.ledOnMS = this.ledOnMS;
1591 that.ledOffMS = this.ledOffMS;
1592 that.defaults = this.defaults;
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001593
Joe Onorato18e69df2010-05-17 22:26:12 -07001594 that.flags = this.flags;
1595
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001596 that.priority = this.priority;
Joe Malin8d40d042012-11-05 11:36:40 -08001597
John Spurlockfd7f1e02014-03-18 16:41:57 -04001598 that.category = this.category;
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001599
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001600 that.mGroupKey = this.mGroupKey;
1601
1602 that.mSortKey = this.mSortKey;
1603
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001604 if (this.extras != null) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04001605 try {
1606 that.extras = new Bundle(this.extras);
1607 // will unparcel
1608 that.extras.size();
1609 } catch (BadParcelableException e) {
1610 Log.e(TAG, "could not unparcel extras from notification: " + this, e);
1611 that.extras = null;
1612 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001613 }
1614
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001615 if (this.actions != null) {
1616 that.actions = new Action[this.actions.length];
1617 for(int i=0; i<this.actions.length; i++) {
1618 that.actions[i] = this.actions[i].clone();
1619 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001620 }
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001621
Daniel Sandler1a497d32013-04-18 14:52:45 -04001622 if (heavy && this.bigContentView != null) {
Daniel Sandlerf3b73432012-03-27 15:01:25 -04001623 that.bigContentView = this.bigContentView.clone();
1624 }
Daniel Sandler1a497d32013-04-18 14:52:45 -04001625
Chris Wren8fd39ec2014-02-27 17:43:26 -05001626 if (heavy && this.headsUpContentView != null) {
1627 that.headsUpContentView = this.headsUpContentView.clone();
1628 }
1629
Dan Sandler0bf2ed82013-12-21 23:33:41 -06001630 that.visibility = this.visibility;
1631
1632 if (this.publicVersion != null) {
1633 that.publicVersion = new Notification();
1634 this.publicVersion.cloneInto(that.publicVersion, heavy);
1635 }
1636
Dan Sandler26e81cf2014-05-06 10:01:27 -04001637 that.color = this.color;
1638
Daniel Sandler1a497d32013-04-18 14:52:45 -04001639 if (!heavy) {
1640 that.lightenPayload(); // will clean out extras
1641 }
1642 }
1643
1644 /**
1645 * Removes heavyweight parts of the Notification object for archival or for sending to
1646 * listeners when the full contents are not necessary.
1647 * @hide
1648 */
1649 public final void lightenPayload() {
1650 tickerView = null;
1651 contentView = null;
1652 bigContentView = null;
Chris Wren8fd39ec2014-02-27 17:43:26 -05001653 headsUpContentView = null;
Dan Sandlerd63f9322015-05-06 15:18:49 -04001654 mLargeIcon = null;
Dan Sandler50128532015-12-08 15:42:41 -05001655 if (extras != null && !extras.isEmpty()) {
1656 final Set<String> keyset = extras.keySet();
1657 final int N = keyset.size();
1658 final String[] keys = keyset.toArray(new String[N]);
1659 for (int i=0; i<N; i++) {
1660 final String key = keys[i];
1661 final Object obj = extras.get(key);
1662 if (obj != null &&
1663 ( obj instanceof Parcelable
1664 || obj instanceof Parcelable[]
1665 || obj instanceof SparseArray
1666 || obj instanceof ArrayList)) {
1667 extras.remove(key);
1668 }
1669 }
Daniel Sandler1a497d32013-04-18 14:52:45 -04001670 }
Joe Onorato18e69df2010-05-17 22:26:12 -07001671 }
1672
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04001673 /**
1674 * Make sure this CharSequence is safe to put into a bundle, which basically
1675 * means it had better not be some custom Parcelable implementation.
1676 * @hide
1677 */
1678 public static CharSequence safeCharSequence(CharSequence cs) {
Christoph Studer535ec612014-09-03 15:47:47 +02001679 if (cs == null) return cs;
1680 if (cs.length() > MAX_CHARSEQUENCE_LENGTH) {
1681 cs = cs.subSequence(0, MAX_CHARSEQUENCE_LENGTH);
1682 }
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04001683 if (cs instanceof Parcelable) {
1684 Log.e(TAG, "warning: " + cs.getClass().getCanonicalName()
1685 + " instance is a custom Parcelable and not allowed in Notification");
1686 return cs.toString();
1687 }
Selim Cinek60a54252016-02-26 17:03:25 -08001688 return removeTextSizeSpans(cs);
1689 }
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04001690
Selim Cinek60a54252016-02-26 17:03:25 -08001691 private static CharSequence removeTextSizeSpans(CharSequence charSequence) {
1692 if (charSequence instanceof Spanned) {
1693 Spanned ss = (Spanned) charSequence;
1694 Object[] spans = ss.getSpans(0, ss.length(), Object.class);
1695 SpannableStringBuilder builder = new SpannableStringBuilder(ss.toString());
1696 for (Object span : spans) {
1697 Object resultSpan = span;
Selim Cinek89991a22016-03-07 19:51:54 -08001698 if (resultSpan instanceof CharacterStyle) {
1699 resultSpan = ((CharacterStyle) span).getUnderlying();
1700 }
1701 if (resultSpan instanceof TextAppearanceSpan) {
1702 TextAppearanceSpan originalSpan = (TextAppearanceSpan) resultSpan;
Selim Cinek60a54252016-02-26 17:03:25 -08001703 resultSpan = new TextAppearanceSpan(
1704 originalSpan.getFamily(),
1705 originalSpan.getTextStyle(),
1706 -1,
1707 originalSpan.getTextColor(),
1708 originalSpan.getLinkTextColor());
Selim Cinek89991a22016-03-07 19:51:54 -08001709 } else if (resultSpan instanceof RelativeSizeSpan
1710 || resultSpan instanceof AbsoluteSizeSpan) {
Selim Cinek60a54252016-02-26 17:03:25 -08001711 continue;
Selim Cinek89991a22016-03-07 19:51:54 -08001712 } else {
1713 resultSpan = span;
Selim Cinek60a54252016-02-26 17:03:25 -08001714 }
1715 builder.setSpan(resultSpan, ss.getSpanStart(span), ss.getSpanEnd(span),
1716 ss.getSpanFlags(span));
1717 }
1718 return builder;
1719 }
1720 return charSequence;
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04001721 }
1722
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001723 public int describeContents() {
1724 return 0;
1725 }
1726
1727 /**
Dan Sandler4e787062015-06-17 15:09:48 -04001728 * Flatten this notification into a parcel.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001729 */
1730 public void writeToParcel(Parcel parcel, int flags)
1731 {
1732 parcel.writeInt(1);
1733
1734 parcel.writeLong(when);
Dan Sandler4e787062015-06-17 15:09:48 -04001735 if (mSmallIcon == null && icon != 0) {
1736 // you snuck an icon in here without using the builder; let's try to keep it
1737 mSmallIcon = Icon.createWithResource("", icon);
1738 }
Dan Sandler3936e7a2015-05-19 20:59:12 -04001739 if (mSmallIcon != null) {
1740 parcel.writeInt(1);
1741 mSmallIcon.writeToParcel(parcel, 0);
1742 } else {
1743 parcel.writeInt(0);
1744 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001745 parcel.writeInt(number);
1746 if (contentIntent != null) {
1747 parcel.writeInt(1);
1748 contentIntent.writeToParcel(parcel, 0);
1749 } else {
1750 parcel.writeInt(0);
1751 }
1752 if (deleteIntent != null) {
1753 parcel.writeInt(1);
1754 deleteIntent.writeToParcel(parcel, 0);
1755 } else {
1756 parcel.writeInt(0);
1757 }
1758 if (tickerText != null) {
1759 parcel.writeInt(1);
1760 TextUtils.writeToParcel(tickerText, parcel, flags);
1761 } else {
1762 parcel.writeInt(0);
1763 }
Joe Onorato46439ce2010-11-19 13:56:21 -08001764 if (tickerView != null) {
Joe Onoratoef1e7762010-09-17 18:38:38 -04001765 parcel.writeInt(1);
Joe Onorato46439ce2010-11-19 13:56:21 -08001766 tickerView.writeToParcel(parcel, 0);
Joe Onoratoef1e7762010-09-17 18:38:38 -04001767 } else {
1768 parcel.writeInt(0);
1769 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001770 if (contentView != null) {
1771 parcel.writeInt(1);
1772 contentView.writeToParcel(parcel, 0);
1773 } else {
1774 parcel.writeInt(0);
1775 }
Dan Sandlerd63f9322015-05-06 15:18:49 -04001776 if (mLargeIcon != null) {
Joe Onorato561d3852010-11-20 18:09:34 -08001777 parcel.writeInt(1);
Dan Sandlerd63f9322015-05-06 15:18:49 -04001778 mLargeIcon.writeToParcel(parcel, 0);
Joe Onorato561d3852010-11-20 18:09:34 -08001779 } else {
1780 parcel.writeInt(0);
1781 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001782
1783 parcel.writeInt(defaults);
1784 parcel.writeInt(this.flags);
1785
1786 if (sound != null) {
1787 parcel.writeInt(1);
1788 sound.writeToParcel(parcel, 0);
1789 } else {
1790 parcel.writeInt(0);
1791 }
1792 parcel.writeInt(audioStreamType);
John Spurlockc0650f022014-07-19 13:22:39 -04001793
1794 if (audioAttributes != null) {
1795 parcel.writeInt(1);
1796 audioAttributes.writeToParcel(parcel, 0);
1797 } else {
1798 parcel.writeInt(0);
1799 }
1800
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001801 parcel.writeLongArray(vibrate);
1802 parcel.writeInt(ledARGB);
1803 parcel.writeInt(ledOnMS);
1804 parcel.writeInt(ledOffMS);
1805 parcel.writeInt(iconLevel);
Daniel Sandlere46cbd32010-06-17 10:35:26 -04001806
1807 if (fullScreenIntent != null) {
1808 parcel.writeInt(1);
1809 fullScreenIntent.writeToParcel(parcel, 0);
1810 } else {
1811 parcel.writeInt(0);
1812 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001813
1814 parcel.writeInt(priority);
Joe Malin8d40d042012-11-05 11:36:40 -08001815
John Spurlockfd7f1e02014-03-18 16:41:57 -04001816 parcel.writeString(category);
Joe Malin8d40d042012-11-05 11:36:40 -08001817
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001818 parcel.writeString(mGroupKey);
1819
1820 parcel.writeString(mSortKey);
1821
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001822 parcel.writeBundle(extras); // null ok
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001823
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001824 parcel.writeTypedArray(actions, 0); // null ok
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001825
Daniel Sandlerf3b73432012-03-27 15:01:25 -04001826 if (bigContentView != null) {
1827 parcel.writeInt(1);
1828 bigContentView.writeToParcel(parcel, 0);
1829 } else {
1830 parcel.writeInt(0);
1831 }
Dan Sandler0bf2ed82013-12-21 23:33:41 -06001832
Chris Wren8fd39ec2014-02-27 17:43:26 -05001833 if (headsUpContentView != null) {
1834 parcel.writeInt(1);
1835 headsUpContentView.writeToParcel(parcel, 0);
1836 } else {
1837 parcel.writeInt(0);
1838 }
1839
Dan Sandler0bf2ed82013-12-21 23:33:41 -06001840 parcel.writeInt(visibility);
1841
1842 if (publicVersion != null) {
1843 parcel.writeInt(1);
1844 publicVersion.writeToParcel(parcel, 0);
1845 } else {
1846 parcel.writeInt(0);
1847 }
Dan Sandler26e81cf2014-05-06 10:01:27 -04001848
1849 parcel.writeInt(color);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001850 }
1851
1852 /**
1853 * Parcelable.Creator that instantiates Notification objects
1854 */
1855 public static final Parcelable.Creator<Notification> CREATOR
1856 = new Parcelable.Creator<Notification>()
1857 {
1858 public Notification createFromParcel(Parcel parcel)
1859 {
1860 return new Notification(parcel);
1861 }
1862
1863 public Notification[] newArray(int size)
1864 {
1865 return new Notification[size];
1866 }
1867 };
1868
1869 /**
1870 * Sets the {@link #contentView} field to be a view with the standard "Latest Event"
1871 * layout.
1872 *
1873 * <p>Uses the {@link #icon} and {@link #when} fields to set the icon and time fields
1874 * in the view.</p>
1875 * @param context The context for your application / activity.
1876 * @param contentTitle The title that goes in the expanded entry.
1877 * @param contentText The text that goes in the expanded entry.
1878 * @param contentIntent The intent to launch when the user clicks the expanded notification.
1879 * If this is an activity, it must include the
1880 * {@link android.content.Intent#FLAG_ACTIVITY_NEW_TASK} flag, which requires
Scott Main7aee61f2011-02-08 11:25:01 -08001881 * that you take care of task management as described in the
1882 * <a href="{@docRoot}guide/topics/fundamentals/tasks-and-back-stack.html">Tasks and Back
1883 * Stack</a> document.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001884 *
Joe Onorato46439ce2010-11-19 13:56:21 -08001885 * @deprecated Use {@link Builder} instead.
Chris Wrena05db382015-06-24 15:18:34 -04001886 * @removed
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001887 */
Joe Onorato46439ce2010-11-19 13:56:21 -08001888 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001889 public void setLatestEventInfo(Context context,
1890 CharSequence contentTitle, CharSequence contentText, PendingIntent contentIntent) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04001891 if (context.getApplicationInfo().targetSdkVersion > Build.VERSION_CODES.LOLLIPOP_MR1){
1892 Log.e(TAG, "setLatestEventInfo() is deprecated and you should feel deprecated.",
1893 new Throwable());
1894 }
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001895
Julia Reynoldsd9228f12015-10-20 10:37:27 -04001896 // ensure that any information already set directly is preserved
1897 final Notification.Builder builder = new Notification.Builder(context, this);
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001898
1899 // now apply the latestEventInfo fields
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001900 if (contentTitle != null) {
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001901 builder.setContentTitle(contentTitle);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001902 }
1903 if (contentText != null) {
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001904 builder.setContentText(contentText);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001905 }
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001906 builder.setContentIntent(contentIntent);
Julia Reynoldsd9228f12015-10-20 10:37:27 -04001907
1908 builder.build(); // callers expect this notification to be ready to use
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001909 }
1910
Julia Reynoldsda303542015-11-23 14:00:20 -05001911 /**
1912 * @hide
1913 */
1914 public static void addFieldsFromContext(Context context, Notification notification) {
1915 if (notification.extras.getParcelable(EXTRA_BUILDER_APPLICATION_INFO) == null) {
1916 notification.extras.putParcelable(EXTRA_BUILDER_APPLICATION_INFO,
1917 context.getApplicationInfo());
1918 }
1919 if (!notification.extras.containsKey(EXTRA_ORIGINATING_USERID)) {
1920 notification.extras.putInt(EXTRA_ORIGINATING_USERID, context.getUserId());
1921 }
1922 }
1923
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001924 @Override
1925 public String toString() {
1926 StringBuilder sb = new StringBuilder();
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001927 sb.append("Notification(pri=");
1928 sb.append(priority);
1929 sb.append(" contentView=");
Joe Onoratoc9596d62011-01-12 17:03:11 -08001930 if (contentView != null) {
1931 sb.append(contentView.getPackage());
1932 sb.append("/0x");
1933 sb.append(Integer.toHexString(contentView.getLayoutId()));
1934 } else {
1935 sb.append("null");
1936 }
1937 sb.append(" vibrate=");
Daniel Sandler6738eee2012-11-16 12:03:32 -05001938 if ((this.defaults & DEFAULT_VIBRATE) != 0) {
1939 sb.append("default");
1940 } else if (this.vibrate != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001941 int N = this.vibrate.length-1;
1942 sb.append("[");
1943 for (int i=0; i<N; i++) {
1944 sb.append(this.vibrate[i]);
1945 sb.append(',');
1946 }
Simon Schoar8cf97d92009-06-10 22:08:37 +02001947 if (N != -1) {
1948 sb.append(this.vibrate[N]);
1949 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001950 sb.append("]");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001951 } else {
1952 sb.append("null");
1953 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001954 sb.append(" sound=");
Daniel Sandler6738eee2012-11-16 12:03:32 -05001955 if ((this.defaults & DEFAULT_SOUND) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001956 sb.append("default");
Daniel Sandler6738eee2012-11-16 12:03:32 -05001957 } else if (this.sound != null) {
1958 sb.append(this.sound.toString());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001959 } else {
1960 sb.append("null");
1961 }
Chris Wren365b6d32015-07-16 10:39:26 -04001962 if (this.tickerText != null) {
1963 sb.append(" tick");
1964 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001965 sb.append(" defaults=0x");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001966 sb.append(Integer.toHexString(this.defaults));
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001967 sb.append(" flags=0x");
Daniel Sandlere46cbd32010-06-17 10:35:26 -04001968 sb.append(Integer.toHexString(this.flags));
Dan Sandler26e81cf2014-05-06 10:01:27 -04001969 sb.append(String.format(" color=0x%08x", this.color));
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001970 if (this.category != null) {
1971 sb.append(" category=");
1972 sb.append(this.category);
1973 }
1974 if (this.mGroupKey != null) {
1975 sb.append(" groupKey=");
1976 sb.append(this.mGroupKey);
1977 }
1978 if (this.mSortKey != null) {
1979 sb.append(" sortKey=");
1980 sb.append(this.mSortKey);
1981 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001982 if (actions != null) {
Dan Sandler1b718782014-07-18 12:43:45 -04001983 sb.append(" actions=");
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001984 sb.append(actions.length);
Dan Sandler1b718782014-07-18 12:43:45 -04001985 }
1986 sb.append(" vis=");
1987 sb.append(visibilityToString(this.visibility));
1988 if (this.publicVersion != null) {
1989 sb.append(" publicVersion=");
1990 sb.append(publicVersion.toString());
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001991 }
1992 sb.append(")");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001993 return sb.toString();
1994 }
Joe Onorato46439ce2010-11-19 13:56:21 -08001995
Dan Sandler1b718782014-07-18 12:43:45 -04001996 /**
1997 * {@hide}
1998 */
1999 public static String visibilityToString(int vis) {
2000 switch (vis) {
2001 case VISIBILITY_PRIVATE:
2002 return "PRIVATE";
2003 case VISIBILITY_PUBLIC:
2004 return "PUBLIC";
2005 case VISIBILITY_SECRET:
2006 return "SECRET";
2007 default:
2008 return "UNKNOWN(" + String.valueOf(vis) + ")";
2009 }
2010 }
2011
Joe Onoratocb109a02011-01-18 17:57:41 -08002012 /**
John Spurlock1d881a12015-03-18 19:21:54 -04002013 * {@hide}
2014 */
2015 public static String priorityToString(@Priority int pri) {
2016 switch (pri) {
2017 case PRIORITY_MIN:
2018 return "MIN";
2019 case PRIORITY_LOW:
2020 return "LOW";
2021 case PRIORITY_DEFAULT:
2022 return "DEFAULT";
2023 case PRIORITY_HIGH:
2024 return "HIGH";
2025 case PRIORITY_MAX:
2026 return "MAX";
2027 default:
2028 return "UNKNOWN(" + String.valueOf(pri) + ")";
2029 }
2030 }
2031
2032 /**
Dan Sandlerd63f9322015-05-06 15:18:49 -04002033 * The small icon representing this notification in the status bar and content view.
2034 *
2035 * @return the small icon representing this notification.
2036 *
2037 * @see Builder#getSmallIcon()
2038 * @see Builder#setSmallIcon(Icon)
2039 */
2040 public Icon getSmallIcon() {
2041 return mSmallIcon;
2042 }
2043
2044 /**
2045 * Used when notifying to clean up legacy small icons.
2046 * @hide
2047 */
2048 public void setSmallIcon(Icon icon) {
2049 mSmallIcon = icon;
2050 }
2051
2052 /**
2053 * The large icon shown in this notification's content view.
2054 * @see Builder#getLargeIcon()
2055 * @see Builder#setLargeIcon(Icon)
2056 */
2057 public Icon getLargeIcon() {
2058 return mLargeIcon;
2059 }
2060
2061 /**
Christoph Studer4600f9b2014-07-22 22:44:43 +02002062 * @hide
2063 */
Christoph Studerc8db24b2014-07-25 17:50:30 +02002064 public boolean isGroupSummary() {
2065 return mGroupKey != null && (flags & FLAG_GROUP_SUMMARY) != 0;
2066 }
2067
2068 /**
2069 * @hide
2070 */
2071 public boolean isGroupChild() {
2072 return mGroupKey != null && (flags & FLAG_GROUP_SUMMARY) == 0;
2073 }
2074
2075 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002076 * Builder class for {@link Notification} objects.
Joe Malin8d40d042012-11-05 11:36:40 -08002077 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002078 * Provides a convenient way to set the various fields of a {@link Notification} and generate
Scott Main183bf112012-08-13 19:12:13 -07002079 * content views using the platform's notification layout template. If your app supports
2080 * versions of Android as old as API level 4, you can instead use
2081 * {@link android.support.v4.app.NotificationCompat.Builder NotificationCompat.Builder},
2082 * available in the <a href="{@docRoot}tools/extras/support-library.html">Android Support
2083 * library</a>.
Joe Malin8d40d042012-11-05 11:36:40 -08002084 *
Scott Main183bf112012-08-13 19:12:13 -07002085 * <p>Example:
Joe Malin8d40d042012-11-05 11:36:40 -08002086 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002087 * <pre class="prettyprint">
Scott Main183bf112012-08-13 19:12:13 -07002088 * Notification noti = new Notification.Builder(mContext)
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002089 * .setContentTitle(&quot;New mail from &quot; + sender.toString())
2090 * .setContentText(subject)
2091 * .setSmallIcon(R.drawable.new_mail)
2092 * .setLargeIcon(aBitmap)
Chris Wrenfbd96ba2012-05-01 12:03:58 -04002093 * .build();
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002094 * </pre>
Joe Onoratocb109a02011-01-18 17:57:41 -08002095 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002096 public static class Builder {
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05002097 /**
2098 * @hide
2099 */
2100 public static final String EXTRA_REBUILD_CONTENT_VIEW_ACTION_COUNT =
2101 "android.rebuild.contentViewActionCount";
2102 /**
2103 * @hide
2104 */
2105 public static final String EXTRA_REBUILD_BIG_CONTENT_VIEW_ACTION_COUNT
2106 = "android.rebuild.bigViewActionCount";
2107 /**
2108 * @hide
2109 */
2110 public static final String EXTRA_REBUILD_HEADS_UP_CONTENT_VIEW_ACTION_COUNT
2111 = "android.rebuild.hudViewActionCount";
2112
Daniel Sandler602ad1c2012-06-12 16:06:27 -04002113 private static final int MAX_ACTION_BUTTONS = 3;
Daniel Sandler8680bf82012-05-15 16:52:52 -04002114
Joe Onorato46439ce2010-11-19 13:56:21 -08002115 private Context mContext;
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002116 private Notification mN;
2117 private Bundle mUserExtras = new Bundle();
Chris Wrenfbd96ba2012-05-01 12:03:58 -04002118 private Style mStyle;
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002119 private ArrayList<Action> mActions = new ArrayList<Action>(MAX_ACTION_BUTTONS);
2120 private ArrayList<String> mPersonList = new ArrayList<String>();
2121 private NotificationColorUtil mColorUtil;
2122 private boolean mColorUtilInited = false;
Christoph Studer7ac80e62014-08-04 16:01:57 +02002123
2124 /**
Adrian Roos4ff3b122016-02-01 12:26:13 -08002125 * Caches a contrast-enhanced version of {@link #mCachedContrastColorIsFor}.
2126 */
2127 private int mCachedContrastColor = COLOR_INVALID;
2128 private int mCachedContrastColorIsFor = COLOR_INVALID;
2129
2130 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002131 * Constructs a new Builder with the defaults:
Joe Onoratocb109a02011-01-18 17:57:41 -08002132 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002133
2134 * <table>
2135 * <tr><th align=right>priority</th>
2136 * <td>{@link #PRIORITY_DEFAULT}</td></tr>
2137 * <tr><th align=right>when</th>
2138 * <td>now ({@link System#currentTimeMillis()})</td></tr>
2139 * <tr><th align=right>audio stream</th>
2140 * <td>{@link #STREAM_DEFAULT}</td></tr>
2141 * </table>
Joe Onoratocb109a02011-01-18 17:57:41 -08002142 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002143
2144 * @param context
2145 * A {@link Context} that will be used by the Builder to construct the
2146 * RemoteViews. The Context will not be held past the lifetime of this Builder
2147 * object.
Joe Onoratocb109a02011-01-18 17:57:41 -08002148 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002149 public Builder(Context context) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002150 this(context, null);
Joe Onorato46439ce2010-11-19 13:56:21 -08002151 }
2152
Joe Onoratocb109a02011-01-18 17:57:41 -08002153 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002154 * @hide
Christoph Studer4600f9b2014-07-22 22:44:43 +02002155 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002156 public Builder(Context context, Notification toAdopt) {
2157 mContext = context;
Christoph Studer4600f9b2014-07-22 22:44:43 +02002158
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002159 if (toAdopt == null) {
2160 mN = new Notification();
2161 mN.extras.putBoolean(EXTRA_SHOW_WHEN, true);
2162 mN.priority = PRIORITY_DEFAULT;
2163 mN.visibility = VISIBILITY_PRIVATE;
2164 } else {
2165 mN = toAdopt;
2166 if (mN.actions != null) {
2167 Collections.addAll(mActions, mN.actions);
Christoph Studer4600f9b2014-07-22 22:44:43 +02002168 }
2169
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002170 if (mN.extras.containsKey(EXTRA_PEOPLE)) {
2171 Collections.addAll(mPersonList, mN.extras.getStringArray(EXTRA_PEOPLE));
2172 }
2173
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002174 String templateClass = mN.extras.getString(EXTRA_TEMPLATE);
2175 if (!TextUtils.isEmpty(templateClass)) {
2176 final Class<? extends Style> styleClass
2177 = getNotificationStyleClass(templateClass);
2178 if (styleClass == null) {
2179 Log.d(TAG, "Unknown style class: " + templateClass);
2180 } else {
2181 try {
2182 final Constructor<? extends Style> ctor = styleClass.getConstructor();
2183 ctor.setAccessible(true);
2184 final Style style = ctor.newInstance();
2185 style.restoreFromExtras(mN.extras);
2186
2187 if (style != null) {
2188 setStyle(style);
2189 }
2190 } catch (Throwable t) {
2191 Log.e(TAG, "Could not create Style", t);
2192 }
2193 }
2194 }
2195
2196 }
2197 }
2198
2199 private NotificationColorUtil getColorUtil() {
2200 if (!mColorUtilInited) {
2201 mColorUtilInited = true;
2202 if (mContext.getApplicationInfo().targetSdkVersion < Build.VERSION_CODES.LOLLIPOP) {
2203 mColorUtil = NotificationColorUtil.getInstance(mContext);
Christoph Studer4600f9b2014-07-22 22:44:43 +02002204 }
2205 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002206 return mColorUtil;
Christoph Studer4600f9b2014-07-22 22:44:43 +02002207 }
2208
2209 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002210 * Add a timestamp pertaining to the notification (usually the time the event occurred).
Daniel Sandler0c890492012-09-12 17:23:10 -07002211 * It will be shown in the notification content view by default; use
Griff Hazen50c11652014-05-16 09:46:31 -07002212 * {@link #setShowWhen(boolean) setShowWhen} to control this.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002213 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002214 * @see Notification#when
Joe Onoratocb109a02011-01-18 17:57:41 -08002215 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002216 public Builder setWhen(long when) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002217 mN.when = when;
Joe Onorato46439ce2010-11-19 13:56:21 -08002218 return this;
2219 }
2220
Joe Onoratocb109a02011-01-18 17:57:41 -08002221 /**
Griff Hazen50c11652014-05-16 09:46:31 -07002222 * Control whether the timestamp set with {@link #setWhen(long) setWhen} is shown
Daniel Sandler0c890492012-09-12 17:23:10 -07002223 * in the content view.
2224 */
2225 public Builder setShowWhen(boolean show) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002226 mN.extras.putBoolean(EXTRA_SHOW_WHEN, show);
Daniel Sandler0c890492012-09-12 17:23:10 -07002227 return this;
2228 }
2229
2230 /**
Daniel Sandlerd33b8032012-05-10 11:41:48 -04002231 * Show the {@link Notification#when} field as a stopwatch.
Joe Malin8d40d042012-11-05 11:36:40 -08002232 *
2233 * Instead of presenting <code>when</code> as a timestamp, the notification will show an
Daniel Sandlerd33b8032012-05-10 11:41:48 -04002234 * automatically updating display of the minutes and seconds since <code>when</code>.
Daniel Sandlera2985ed2012-04-03 16:42:00 -04002235 *
Daniel Sandlerd33b8032012-05-10 11:41:48 -04002236 * Useful when showing an elapsed time (like an ongoing phone call).
2237 *
Selim Cinek81c23aa2016-02-25 16:23:13 -08002238 * The counter can also be set to count down to <code>when</code> when using
2239 * {@link #setChronometerCountsDown(boolean)}.
2240 *
Daniel Sandlerd33b8032012-05-10 11:41:48 -04002241 * @see android.widget.Chronometer
Daniel Sandlera2985ed2012-04-03 16:42:00 -04002242 * @see Notification#when
Selim Cinek81c23aa2016-02-25 16:23:13 -08002243 * @see #setChronometerCountsDown(boolean)
Daniel Sandlera2985ed2012-04-03 16:42:00 -04002244 */
2245 public Builder setUsesChronometer(boolean b) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002246 mN.extras.putBoolean(EXTRA_SHOW_CHRONOMETER, b);
Daniel Sandlera2985ed2012-04-03 16:42:00 -04002247 return this;
2248 }
2249
2250 /**
Selim Cinek81c23aa2016-02-25 16:23:13 -08002251 * Sets the Chronometer to count down instead of counting up.
2252 *
2253 * <p>This is only relevant if {@link #setUsesChronometer(boolean)} has been set to true.
2254 * If it isn't set the chronometer will count up.
2255 *
2256 * @see #setUsesChronometer(boolean)
2257 */
2258 public Builder setChronometerCountsDown(boolean countsDown) {
2259 mN.extras.putBoolean(EXTRA_CHRONOMETER_COUNTS_DOWN, countsDown);
2260 return this;
2261 }
2262
2263 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002264 * Set the small icon resource, which will be used to represent the notification in the
2265 * status bar.
Joe Onoratocb109a02011-01-18 17:57:41 -08002266 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002267
2268 * The platform template for the expanded view will draw this icon in the left, unless a
2269 * {@link #setLargeIcon(Bitmap) large icon} has also been specified, in which case the small
2270 * icon will be moved to the right-hand side.
2271 *
2272
2273 * @param icon
2274 * A resource ID in the application's package of the drawable to use.
2275 * @see Notification#icon
Joe Onoratocb109a02011-01-18 17:57:41 -08002276 */
Tor Norbye7b9c9122013-05-30 16:48:33 -07002277 public Builder setSmallIcon(@DrawableRes int icon) {
Dan Sandlerd63f9322015-05-06 15:18:49 -04002278 return setSmallIcon(icon != 0
2279 ? Icon.createWithResource(mContext, icon)
2280 : null);
Joe Onorato46439ce2010-11-19 13:56:21 -08002281 }
2282
Joe Onoratocb109a02011-01-18 17:57:41 -08002283 /**
2284 * A variant of {@link #setSmallIcon(int) setSmallIcon(int)} that takes an additional
2285 * level parameter for when the icon is a {@link android.graphics.drawable.LevelListDrawable
2286 * LevelListDrawable}.
2287 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002288 * @param icon A resource ID in the application's package of the drawable to use.
Joe Onoratocb109a02011-01-18 17:57:41 -08002289 * @param level The level to use for the icon.
2290 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002291 * @see Notification#icon
2292 * @see Notification#iconLevel
Joe Onoratocb109a02011-01-18 17:57:41 -08002293 */
Tor Norbye7b9c9122013-05-30 16:48:33 -07002294 public Builder setSmallIcon(@DrawableRes int icon, int level) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002295 mN.iconLevel = level;
Dan Sandlerd63f9322015-05-06 15:18:49 -04002296 return setSmallIcon(icon);
2297 }
2298
2299 /**
2300 * Set the small icon, which will be used to represent the notification in the
2301 * status bar and content view (unless overriden there by a
2302 * {@link #setLargeIcon(Bitmap) large icon}).
2303 *
2304 * @param icon An Icon object to use.
2305 * @see Notification#icon
2306 */
2307 public Builder setSmallIcon(Icon icon) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002308 mN.setSmallIcon(icon);
2309 if (icon != null && icon.getType() == Icon.TYPE_RESOURCE) {
2310 mN.icon = icon.getResId();
2311 }
Joe Onorato46439ce2010-11-19 13:56:21 -08002312 return this;
2313 }
2314
Joe Onoratocb109a02011-01-18 17:57:41 -08002315 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002316 * Set the first line of text in the platform notification template.
Joe Onoratocb109a02011-01-18 17:57:41 -08002317 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002318 public Builder setContentTitle(CharSequence title) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002319 mN.extras.putCharSequence(EXTRA_TITLE, safeCharSequence(title));
Joe Onorato46439ce2010-11-19 13:56:21 -08002320 return this;
2321 }
2322
Joe Onoratocb109a02011-01-18 17:57:41 -08002323 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002324 * Set the second line of text in the platform notification template.
Joe Onoratocb109a02011-01-18 17:57:41 -08002325 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002326 public Builder setContentText(CharSequence text) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002327 mN.extras.putCharSequence(EXTRA_TEXT, safeCharSequence(text));
Joe Onorato46439ce2010-11-19 13:56:21 -08002328 return this;
2329 }
2330
Joe Onoratocb109a02011-01-18 17:57:41 -08002331 /**
Joe Malin8d40d042012-11-05 11:36:40 -08002332 * Set the third line of text in the platform notification template.
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04002333 * Don't use if you're also using {@link #setProgress(int, int, boolean)}; they occupy the
2334 * same location in the standard template.
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002335 */
2336 public Builder setSubText(CharSequence text) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002337 mN.extras.putCharSequence(EXTRA_SUB_TEXT, safeCharSequence(text));
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002338 return this;
2339 }
2340
2341 /**
Adrian Roose458aa82015-12-08 16:17:19 -08002342 * Set the remote input history.
2343 *
2344 * This should be set to the most recent inputs that have been sent
2345 * through a {@link RemoteInput} of this Notification and cleared once the it is no
2346 * longer relevant (e.g. for chat notifications once the other party has responded).
2347 *
2348 * The most recent input must be stored at the 0 index, the second most recent at the
2349 * 1 index, etc. Note that the system will limit both how far back the inputs will be shown
2350 * and how much of each individual input is shown.
2351 *
2352 * <p>Note: The reply text will only be shown on notifications that have least one action
2353 * with a {@code RemoteInput}.</p>
2354 */
2355 public Builder setRemoteInputHistory(CharSequence[] text) {
2356 if (text == null) {
2357 mN.extras.putCharSequenceArray(EXTRA_REMOTE_INPUT_HISTORY, null);
2358 } else {
2359 final int N = Math.min(MAX_REPLY_HISTORY, text.length);
2360 CharSequence[] safe = new CharSequence[N];
2361 for (int i = 0; i < N; i++) {
2362 safe[i] = safeCharSequence(text[i]);
2363 }
2364 mN.extras.putCharSequenceArray(EXTRA_REMOTE_INPUT_HISTORY, safe);
2365 }
2366 return this;
2367 }
2368
2369 /**
Joe Onoratocb109a02011-01-18 17:57:41 -08002370 * Set the large number at the right-hand side of the notification. This is
2371 * equivalent to setContentInfo, although it might show the number in a different
2372 * font size for readability.
2373 */
Joe Onorato8595a3d2010-11-19 18:12:07 -08002374 public Builder setNumber(int number) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002375 mN.number = number;
Joe Onorato8595a3d2010-11-19 18:12:07 -08002376 return this;
2377 }
2378
Joe Onoratocb109a02011-01-18 17:57:41 -08002379 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002380 * A small piece of additional information pertaining to this notification.
2381 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002382 * The platform template will draw this on the last line of the notification, at the far
2383 * right (to the right of a smallIcon if it has been placed there).
Joe Onoratocb109a02011-01-18 17:57:41 -08002384 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002385 public Builder setContentInfo(CharSequence info) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002386 mN.extras.putCharSequence(EXTRA_INFO_TEXT, safeCharSequence(info));
Joe Onorato46439ce2010-11-19 13:56:21 -08002387 return this;
2388 }
2389
Joe Onoratocb109a02011-01-18 17:57:41 -08002390 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002391 * Set the progress this notification represents.
2392 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002393 * The platform template will represent this using a {@link ProgressBar}.
Jeff Sharkey1c400132011-08-05 14:50:13 -07002394 */
2395 public Builder setProgress(int max, int progress, boolean indeterminate) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002396 mN.extras.putInt(EXTRA_PROGRESS, progress);
2397 mN.extras.putInt(EXTRA_PROGRESS_MAX, max);
2398 mN.extras.putBoolean(EXTRA_PROGRESS_INDETERMINATE, indeterminate);
Jeff Sharkey1c400132011-08-05 14:50:13 -07002399 return this;
2400 }
2401
2402 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002403 * Supply a custom RemoteViews to use instead of the platform template.
2404 *
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002405 * Use {@link #setCustomContentView(RemoteViews)} instead.
Joe Onoratocb109a02011-01-18 17:57:41 -08002406 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002407 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -08002408 public Builder setContent(RemoteViews views) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002409 return setCustomContentView(views);
2410 }
2411
2412 /**
2413 * Supply custom RemoteViews to use instead of the platform template.
2414 *
2415 * This will override the layout that would otherwise be constructed by this Builder
2416 * object.
2417 */
2418 public Builder setCustomContentView(RemoteViews contentView) {
2419 mN.contentView = contentView;
2420 return this;
2421 }
2422
2423 /**
2424 * Supply custom RemoteViews to use instead of the platform template in the expanded form.
2425 *
2426 * This will override the expanded layout that would otherwise be constructed by this
2427 * Builder object.
2428 */
2429 public Builder setCustomBigContentView(RemoteViews contentView) {
2430 mN.bigContentView = contentView;
2431 return this;
2432 }
2433
2434 /**
2435 * Supply custom RemoteViews to use instead of the platform template in the heads up dialog.
2436 *
2437 * This will override the heads-up layout that would otherwise be constructed by this
2438 * Builder object.
2439 */
2440 public Builder setCustomHeadsUpContentView(RemoteViews contentView) {
2441 mN.headsUpContentView = contentView;
Joe Onorato46439ce2010-11-19 13:56:21 -08002442 return this;
2443 }
2444
Joe Onoratocb109a02011-01-18 17:57:41 -08002445 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002446 * Supply a {@link PendingIntent} to be sent when the notification is clicked.
2447 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002448 * As of {@link android.os.Build.VERSION_CODES#HONEYCOMB}, if this field is unset and you
2449 * have specified a custom RemoteViews with {@link #setContent(RemoteViews)}, you can use
2450 * {@link RemoteViews#setOnClickPendingIntent RemoteViews.setOnClickPendingIntent(int,PendingIntent)}
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002451 * to assign PendingIntents to individual views in that custom layout (i.e., to create
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002452 * clickable buttons inside the notification view).
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002453 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002454 * @see Notification#contentIntent Notification.contentIntent
Joe Onoratocb109a02011-01-18 17:57:41 -08002455 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002456 public Builder setContentIntent(PendingIntent intent) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002457 mN.contentIntent = intent;
Joe Onorato46439ce2010-11-19 13:56:21 -08002458 return this;
2459 }
2460
Joe Onoratocb109a02011-01-18 17:57:41 -08002461 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002462 * Supply a {@link PendingIntent} to send when the notification is cleared explicitly by the user.
2463 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002464 * @see Notification#deleteIntent
Joe Onoratocb109a02011-01-18 17:57:41 -08002465 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002466 public Builder setDeleteIntent(PendingIntent intent) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002467 mN.deleteIntent = intent;
Joe Onorato46439ce2010-11-19 13:56:21 -08002468 return this;
2469 }
2470
Joe Onoratocb109a02011-01-18 17:57:41 -08002471 /**
2472 * An intent to launch instead of posting the notification to the status bar.
2473 * Only for use with extremely high-priority notifications demanding the user's
2474 * <strong>immediate</strong> attention, such as an incoming phone call or
2475 * alarm clock that the user has explicitly set to a particular time.
2476 * If this facility is used for something else, please give the user an option
2477 * to turn it off and use a normal notification, as this can be extremely
2478 * disruptive.
2479 *
Chris Wren47c20a12014-06-18 17:27:29 -04002480 * <p>
2481 * The system UI may choose to display a heads-up notification, instead of
2482 * launching this intent, while the user is using the device.
2483 * </p>
2484 *
Joe Onoratocb109a02011-01-18 17:57:41 -08002485 * @param intent The pending intent to launch.
2486 * @param highPriority Passing true will cause this notification to be sent
2487 * even if other notifications are suppressed.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002488 *
2489 * @see Notification#fullScreenIntent
Joe Onoratocb109a02011-01-18 17:57:41 -08002490 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002491 public Builder setFullScreenIntent(PendingIntent intent, boolean highPriority) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002492 mN.fullScreenIntent = intent;
Joe Onorato46439ce2010-11-19 13:56:21 -08002493 setFlag(FLAG_HIGH_PRIORITY, highPriority);
2494 return this;
2495 }
2496
Joe Onoratocb109a02011-01-18 17:57:41 -08002497 /**
Dan Sandler5fcdf6e2014-07-18 11:31:15 -04002498 * Set the "ticker" text which is sent to accessibility services.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002499 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002500 * @see Notification#tickerText
Joe Onoratocb109a02011-01-18 17:57:41 -08002501 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002502 public Builder setTicker(CharSequence tickerText) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002503 mN.tickerText = safeCharSequence(tickerText);
Joe Onorato46439ce2010-11-19 13:56:21 -08002504 return this;
2505 }
2506
Joe Onoratocb109a02011-01-18 17:57:41 -08002507 /**
Dan Sandler5fcdf6e2014-07-18 11:31:15 -04002508 * Obsolete version of {@link #setTicker(CharSequence)}.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002509 *
Joe Onoratocb109a02011-01-18 17:57:41 -08002510 */
Dan Sandler5fcdf6e2014-07-18 11:31:15 -04002511 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -08002512 public Builder setTicker(CharSequence tickerText, RemoteViews views) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002513 setTicker(tickerText);
2514 // views is ignored
Joe Onorato46439ce2010-11-19 13:56:21 -08002515 return this;
2516 }
2517
Joe Onoratocb109a02011-01-18 17:57:41 -08002518 /**
Dan Sandlerd63f9322015-05-06 15:18:49 -04002519 * Add a large icon to the notification content view.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002520 *
2521 * In the platform template, this image will be shown on the left of the notification view
Dan Sandlerd63f9322015-05-06 15:18:49 -04002522 * in place of the {@link #setSmallIcon(Icon) small icon} (which will be placed in a small
2523 * badge atop the large icon).
Dan Sandler08a04c12015-05-06 15:18:49 -04002524 */
Dan Sandlerd63f9322015-05-06 15:18:49 -04002525 public Builder setLargeIcon(Bitmap b) {
2526 return setLargeIcon(b != null ? Icon.createWithBitmap(b) : null);
2527 }
2528
2529 /**
2530 * Add a large icon to the notification content view.
2531 *
2532 * In the platform template, this image will be shown on the left of the notification view
2533 * in place of the {@link #setSmallIcon(Icon) small icon} (which will be placed in a small
2534 * badge atop the large icon).
2535 */
2536 public Builder setLargeIcon(Icon icon) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002537 mN.mLargeIcon = icon;
2538 mN.extras.putParcelable(EXTRA_LARGE_ICON, icon);
Joe Onorato46439ce2010-11-19 13:56:21 -08002539 return this;
2540 }
2541
Joe Onoratocb109a02011-01-18 17:57:41 -08002542 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002543 * Set the sound to play.
2544 *
John Spurlockc0650f022014-07-19 13:22:39 -04002545 * It will be played using the {@link #AUDIO_ATTRIBUTES_DEFAULT default audio attributes}
2546 * for notifications.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002547 *
Chris Wren47c20a12014-06-18 17:27:29 -04002548 * <p>
2549 * A notification that is noisy is more likely to be presented as a heads-up notification.
2550 * </p>
2551 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002552 * @see Notification#sound
Joe Onoratocb109a02011-01-18 17:57:41 -08002553 */
Joe Onorato52f80cd2010-11-21 15:34:48 -08002554 public Builder setSound(Uri sound) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002555 mN.sound = sound;
2556 mN.audioAttributes = AUDIO_ATTRIBUTES_DEFAULT;
Joe Onorato52f80cd2010-11-21 15:34:48 -08002557 return this;
2558 }
2559
Joe Onoratocb109a02011-01-18 17:57:41 -08002560 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002561 * Set the sound to play, along with a specific stream on which to play it.
Joe Onoratocb109a02011-01-18 17:57:41 -08002562 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002563 * See {@link android.media.AudioManager} for the <code>STREAM_</code> constants.
2564 *
Chris Wren47c20a12014-06-18 17:27:29 -04002565 * <p>
2566 * A notification that is noisy is more likely to be presented as a heads-up notification.
2567 * </p>
John Spurlockc0650f022014-07-19 13:22:39 -04002568 * @deprecated use {@link #setSound(Uri, AudioAttributes)} instead.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002569 * @see Notification#sound
Joe Onoratocb109a02011-01-18 17:57:41 -08002570 */
Jean-Michel Trivi81f871e2014-08-06 16:32:38 -07002571 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -08002572 public Builder setSound(Uri sound, int streamType) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002573 mN.sound = sound;
2574 mN.audioStreamType = streamType;
Joe Onorato46439ce2010-11-19 13:56:21 -08002575 return this;
2576 }
2577
Joe Onoratocb109a02011-01-18 17:57:41 -08002578 /**
John Spurlockc0650f022014-07-19 13:22:39 -04002579 * Set the sound to play, along with specific {@link AudioAttributes audio attributes} to
2580 * use during playback.
2581 *
2582 * <p>
2583 * A notification that is noisy is more likely to be presented as a heads-up notification.
2584 * </p>
2585 *
2586 * @see Notification#sound
2587 */
2588 public Builder setSound(Uri sound, AudioAttributes audioAttributes) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002589 mN.sound = sound;
2590 mN.audioAttributes = audioAttributes;
John Spurlockc0650f022014-07-19 13:22:39 -04002591 return this;
2592 }
2593
2594 /**
Joe Onoratocb109a02011-01-18 17:57:41 -08002595 * Set the vibration pattern to use.
2596 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002597 * See {@link android.os.Vibrator#vibrate(long[], int)} for a discussion of the
2598 * <code>pattern</code> parameter.
2599 *
Chris Wren47c20a12014-06-18 17:27:29 -04002600 * <p>
2601 * A notification that vibrates is more likely to be presented as a heads-up notification.
2602 * </p>
2603 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002604 * @see Notification#vibrate
Joe Onoratocb109a02011-01-18 17:57:41 -08002605 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002606 public Builder setVibrate(long[] pattern) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002607 mN.vibrate = pattern;
Joe Onorato46439ce2010-11-19 13:56:21 -08002608 return this;
2609 }
2610
Joe Onoratocb109a02011-01-18 17:57:41 -08002611 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002612 * Set the desired color for the indicator LED on the device, as well as the
2613 * blink duty cycle (specified in milliseconds).
2614 *
2615
2616 * Not all devices will honor all (or even any) of these values.
2617 *
2618
2619 * @see Notification#ledARGB
2620 * @see Notification#ledOnMS
2621 * @see Notification#ledOffMS
Joe Onoratocb109a02011-01-18 17:57:41 -08002622 */
Tor Norbye80756e32015-03-02 09:39:27 -08002623 public Builder setLights(@ColorInt int argb, int onMs, int offMs) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002624 mN.ledARGB = argb;
2625 mN.ledOnMS = onMs;
2626 mN.ledOffMS = offMs;
Julia Reynolds10ee1fc2015-11-09 11:04:55 -05002627 if (onMs != 0 || offMs != 0) {
2628 mN.flags |= FLAG_SHOW_LIGHTS;
2629 }
Joe Onorato46439ce2010-11-19 13:56:21 -08002630 return this;
2631 }
2632
Joe Onoratocb109a02011-01-18 17:57:41 -08002633 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002634 * Set whether this is an "ongoing" notification.
Joe Onoratocb109a02011-01-18 17:57:41 -08002635 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002636
2637 * Ongoing notifications cannot be dismissed by the user, so your application or service
2638 * must take care of canceling them.
2639 *
2640
2641 * They are typically used to indicate a background task that the user is actively engaged
2642 * with (e.g., playing music) or is pending in some way and therefore occupying the device
2643 * (e.g., a file download, sync operation, active network connection).
2644 *
2645
2646 * @see Notification#FLAG_ONGOING_EVENT
2647 * @see Service#setForeground(boolean)
Joe Onoratocb109a02011-01-18 17:57:41 -08002648 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002649 public Builder setOngoing(boolean ongoing) {
2650 setFlag(FLAG_ONGOING_EVENT, ongoing);
2651 return this;
2652 }
2653
Joe Onoratocb109a02011-01-18 17:57:41 -08002654 /**
2655 * Set this flag if you would only like the sound, vibrate
2656 * and ticker to be played if the notification is not already showing.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002657 *
2658 * @see Notification#FLAG_ONLY_ALERT_ONCE
Joe Onoratocb109a02011-01-18 17:57:41 -08002659 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002660 public Builder setOnlyAlertOnce(boolean onlyAlertOnce) {
2661 setFlag(FLAG_ONLY_ALERT_ONCE, onlyAlertOnce);
2662 return this;
2663 }
2664
Joe Onoratocb109a02011-01-18 17:57:41 -08002665 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002666 * Make this notification automatically dismissed when the user touches it. The
2667 * PendingIntent set with {@link #setDeleteIntent} will be sent when this happens.
2668 *
2669 * @see Notification#FLAG_AUTO_CANCEL
Joe Onoratocb109a02011-01-18 17:57:41 -08002670 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002671 public Builder setAutoCancel(boolean autoCancel) {
Joe Onorato281d83f2011-01-04 17:13:10 -08002672 setFlag(FLAG_AUTO_CANCEL, autoCancel);
Joe Onorato46439ce2010-11-19 13:56:21 -08002673 return this;
2674 }
2675
Joe Onoratocb109a02011-01-18 17:57:41 -08002676 /**
Griff Hazendfcb0802014-02-11 12:00:00 -08002677 * Set whether or not this notification should not bridge to other devices.
2678 *
2679 * <p>Some notifications can be bridged to other devices for remote display.
2680 * This hint can be set to recommend this notification not be bridged.
2681 */
2682 public Builder setLocalOnly(boolean localOnly) {
2683 setFlag(FLAG_LOCAL_ONLY, localOnly);
2684 return this;
2685 }
2686
2687 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002688 * Set which notification properties will be inherited from system defaults.
Joe Onoratocb109a02011-01-18 17:57:41 -08002689 * <p>
2690 * The value should be one or more of the following fields combined with
2691 * bitwise-or:
2692 * {@link #DEFAULT_SOUND}, {@link #DEFAULT_VIBRATE}, {@link #DEFAULT_LIGHTS}.
2693 * <p>
2694 * For all default values, use {@link #DEFAULT_ALL}.
2695 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002696 public Builder setDefaults(int defaults) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002697 mN.defaults = defaults;
Joe Onorato46439ce2010-11-19 13:56:21 -08002698 return this;
2699 }
2700
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002701 /**
2702 * Set the priority of this notification.
2703 *
2704 * @see Notification#priority
2705 */
Tor Norbyed9273d62013-05-30 15:59:53 -07002706 public Builder setPriority(@Priority int pri) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002707 mN.priority = pri;
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002708 return this;
2709 }
Joe Malin8d40d042012-11-05 11:36:40 -08002710
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002711 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -04002712 * Set the notification category.
Joe Malin8d40d042012-11-05 11:36:40 -08002713 *
John Spurlockfd7f1e02014-03-18 16:41:57 -04002714 * @see Notification#category
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002715 */
John Spurlockfd7f1e02014-03-18 16:41:57 -04002716 public Builder setCategory(String category) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002717 mN.category = category;
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002718 return this;
2719 }
2720
2721 /**
Chris Wrendde75302014-03-26 17:24:15 -04002722 * Add a person that is relevant to this notification.
2723 *
Chris Wrene6c48932014-09-29 17:19:27 -04002724 * <P>
2725 * Depending on user preferences, this annotation may allow the notification to pass
2726 * through interruption filters, and to appear more prominently in the user interface.
2727 * </P>
2728 *
2729 * <P>
2730 * The person should be specified by the {@code String} representation of a
2731 * {@link android.provider.ContactsContract.Contacts#CONTENT_LOOKUP_URI}.
2732 * </P>
2733 *
2734 * <P>The system will also attempt to resolve {@code mailto:} and {@code tel:} schema
2735 * URIs. The path part of these URIs must exist in the contacts database, in the
2736 * appropriate column, or the reference will be discarded as invalid. Telephone schema
2737 * URIs will be resolved by {@link android.provider.ContactsContract.PhoneLookup}.
2738 * </P>
2739 *
2740 * @param uri A URI for the person.
Chris Wrendde75302014-03-26 17:24:15 -04002741 * @see Notification#EXTRA_PEOPLE
2742 */
Chris Wrene6c48932014-09-29 17:19:27 -04002743 public Builder addPerson(String uri) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002744 mPersonList.add(uri);
Chris Wrendde75302014-03-26 17:24:15 -04002745 return this;
2746 }
2747
2748 /**
Griff Hazen5cadc3b2014-05-20 09:55:39 -07002749 * Set this notification to be part of a group of notifications sharing the same key.
2750 * Grouped notifications may display in a cluster or stack on devices which
2751 * support such rendering.
2752 *
2753 * <p>To make this notification the summary for its group, also call
2754 * {@link #setGroupSummary}. A sort order can be specified for group members by using
2755 * {@link #setSortKey}.
2756 * @param groupKey The group key of the group.
2757 * @return this object for method chaining
2758 */
2759 public Builder setGroup(String groupKey) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002760 mN.mGroupKey = groupKey;
Griff Hazen5cadc3b2014-05-20 09:55:39 -07002761 return this;
2762 }
2763
2764 /**
2765 * Set this notification to be the group summary for a group of notifications.
2766 * Grouped notifications may display in a cluster or stack on devices which
2767 * support such rendering. Requires a group key also be set using {@link #setGroup}.
2768 * @param isGroupSummary Whether this notification should be a group summary.
2769 * @return this object for method chaining
2770 */
2771 public Builder setGroupSummary(boolean isGroupSummary) {
2772 setFlag(FLAG_GROUP_SUMMARY, isGroupSummary);
2773 return this;
2774 }
2775
2776 /**
2777 * Set a sort key that orders this notification among other notifications from the
2778 * same package. This can be useful if an external sort was already applied and an app
2779 * would like to preserve this. Notifications will be sorted lexicographically using this
2780 * value, although providing different priorities in addition to providing sort key may
2781 * cause this value to be ignored.
2782 *
2783 * <p>This sort key can also be used to order members of a notification group. See
Griff Hazen9e1379f2014-05-20 12:50:51 -07002784 * {@link #setGroup}.
Griff Hazen5cadc3b2014-05-20 09:55:39 -07002785 *
2786 * @see String#compareTo(String)
2787 */
2788 public Builder setSortKey(String sortKey) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002789 mN.mSortKey = sortKey;
Griff Hazen5cadc3b2014-05-20 09:55:39 -07002790 return this;
2791 }
2792
2793 /**
Griff Hazen720042b2014-02-24 15:46:56 -08002794 * Merge additional metadata into this notification.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002795 *
Griff Hazen720042b2014-02-24 15:46:56 -08002796 * <p>Values within the Bundle will replace existing extras values in this Builder.
2797 *
2798 * @see Notification#extras
2799 */
Griff Hazen959591e2014-05-15 22:26:18 -07002800 public Builder addExtras(Bundle extras) {
2801 if (extras != null) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002802 mUserExtras.putAll(extras);
Griff Hazen720042b2014-02-24 15:46:56 -08002803 }
2804 return this;
2805 }
2806
2807 /**
2808 * Set metadata for this notification.
2809 *
2810 * <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 -04002811 * current contents are copied into the Notification each time {@link #build()} is
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002812 * called.
2813 *
Griff Hazen720042b2014-02-24 15:46:56 -08002814 * <p>Replaces any existing extras values with those from the provided Bundle.
2815 * Use {@link #addExtras} to merge in metadata instead.
2816 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002817 * @see Notification#extras
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002818 */
Griff Hazen959591e2014-05-15 22:26:18 -07002819 public Builder setExtras(Bundle extras) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002820 if (extras != null) {
2821 mUserExtras = extras;
2822 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002823 return this;
2824 }
2825
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002826 /**
Griff Hazen720042b2014-02-24 15:46:56 -08002827 * Get the current metadata Bundle used by this notification Builder.
2828 *
2829 * <p>The returned Bundle is shared with this Builder.
2830 *
2831 * <p>The current contents of this Bundle are copied into the Notification each time
2832 * {@link #build()} is called.
2833 *
2834 * @see Notification#extras
2835 */
2836 public Bundle getExtras() {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002837 return mUserExtras;
2838 }
2839
2840 private Bundle getAllExtras() {
2841 final Bundle saveExtras = (Bundle) mUserExtras.clone();
2842 saveExtras.putAll(mN.extras);
2843 return saveExtras;
Griff Hazen720042b2014-02-24 15:46:56 -08002844 }
2845
2846 /**
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002847 * Add an action to this notification. Actions are typically displayed by
2848 * the system as a button adjacent to the notification content.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04002849 * <p>
2850 * Every action must have an icon (32dp square and matching the
2851 * <a href="{@docRoot}design/style/iconography.html#action-bar">Holo
2852 * Dark action bar</a> visual style), a textual label, and a {@link PendingIntent}.
2853 * <p>
2854 * A notification in its expanded form can display up to 3 actions, from left to right in
2855 * the order they were added. Actions will not be displayed when the notification is
2856 * collapsed, however, so be sure that any essential functions may be accessed by the user
2857 * in some other way (for example, in the Activity pointed to by {@link #contentIntent}).
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002858 *
2859 * @param icon Resource ID of a drawable that represents the action.
2860 * @param title Text describing the action.
2861 * @param intent PendingIntent to be fired when the action is invoked.
Dan Sandler86647982015-05-13 23:41:13 -04002862 *
2863 * @deprecated Use {@link #addAction(Action)} instead.
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002864 */
Dan Sandler86647982015-05-13 23:41:13 -04002865 @Deprecated
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002866 public Builder addAction(int icon, CharSequence title, PendingIntent intent) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04002867 mActions.add(new Action(icon, safeCharSequence(title), intent));
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002868 return this;
2869 }
2870
Chris Wrenfbd96ba2012-05-01 12:03:58 -04002871 /**
Griff Hazen959591e2014-05-15 22:26:18 -07002872 * Add an action to this notification. Actions are typically displayed by
2873 * the system as a button adjacent to the notification content.
2874 * <p>
2875 * Every action must have an icon (32dp square and matching the
2876 * <a href="{@docRoot}design/style/iconography.html#action-bar">Holo
2877 * Dark action bar</a> visual style), a textual label, and a {@link PendingIntent}.
2878 * <p>
2879 * A notification in its expanded form can display up to 3 actions, from left to right in
2880 * the order they were added. Actions will not be displayed when the notification is
2881 * collapsed, however, so be sure that any essential functions may be accessed by the user
2882 * in some other way (for example, in the Activity pointed to by {@link #contentIntent}).
2883 *
2884 * @param action The action to add.
2885 */
2886 public Builder addAction(Action action) {
2887 mActions.add(action);
2888 return this;
2889 }
2890
2891 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002892 * Alter the complete list of actions attached to this notification.
2893 * @see #addAction(Action).
2894 *
2895 * @param actions
2896 * @return
2897 */
2898 public Builder setActions(Action... actions) {
2899 mActions.clear();
2900 for (int i = 0; i < actions.length; i++) {
2901 mActions.add(actions[i]);
2902 }
2903 return this;
2904 }
2905
2906 /**
Chris Wrenfbd96ba2012-05-01 12:03:58 -04002907 * Add a rich notification style to be applied at build time.
2908 *
2909 * @param style Object responsible for modifying the notification style.
2910 */
2911 public Builder setStyle(Style style) {
2912 if (mStyle != style) {
2913 mStyle = style;
Daniel Sandlerc08dea22012-06-28 08:35:24 -07002914 if (mStyle != null) {
2915 mStyle.setBuilder(this);
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002916 mN.extras.putString(EXTRA_TEMPLATE, style.getClass().getName());
2917 } else {
2918 mN.extras.remove(EXTRA_TEMPLATE);
Daniel Sandlerc08dea22012-06-28 08:35:24 -07002919 }
Chris Wrenfbd96ba2012-05-01 12:03:58 -04002920 }
2921 return this;
2922 }
2923
Dan Sandler0bf2ed82013-12-21 23:33:41 -06002924 /**
2925 * Specify the value of {@link #visibility}.
Griff Hazenb720abe2014-05-20 13:15:30 -07002926 *
Dan Sandler0bf2ed82013-12-21 23:33:41 -06002927 * @param visibility One of {@link #VISIBILITY_PRIVATE} (the default),
2928 * {@link #VISIBILITY_SECRET}, or {@link #VISIBILITY_PUBLIC}.
2929 *
2930 * @return The same Builder.
2931 */
2932 public Builder setVisibility(int visibility) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002933 mN.visibility = visibility;
Dan Sandler0bf2ed82013-12-21 23:33:41 -06002934 return this;
2935 }
2936
2937 /**
2938 * Supply a replacement Notification whose contents should be shown in insecure contexts
2939 * (i.e. atop the secure lockscreen). See {@link #visibility} and {@link #VISIBILITY_PUBLIC}.
2940 * @param n A replacement notification, presumably with some or all info redacted.
2941 * @return The same Builder.
2942 */
2943 public Builder setPublicVersion(Notification n) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002944 if (n != null) {
2945 mN.publicVersion = new Notification();
2946 n.cloneInto(mN.publicVersion, /*heavy=*/ true);
2947 } else {
2948 mN.publicVersion = null;
2949 }
Dan Sandler0bf2ed82013-12-21 23:33:41 -06002950 return this;
2951 }
2952
Griff Hazenb720abe2014-05-20 13:15:30 -07002953 /**
Griff Hazen5cadc3b2014-05-20 09:55:39 -07002954 * Apply an extender to this notification builder. Extenders may be used to add
2955 * metadata or change options on this builder.
2956 */
Griff Hazen61a9e862014-05-22 16:05:19 -07002957 public Builder extend(Extender extender) {
2958 extender.extend(this);
Griff Hazen5cadc3b2014-05-20 09:55:39 -07002959 return this;
2960 }
2961
Dan Sandler4e787062015-06-17 15:09:48 -04002962 /**
2963 * @hide
2964 */
2965 public void setFlag(int mask, boolean value) {
Joe Onorato46439ce2010-11-19 13:56:21 -08002966 if (value) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002967 mN.flags |= mask;
Joe Onorato46439ce2010-11-19 13:56:21 -08002968 } else {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002969 mN.flags &= ~mask;
Joe Onorato46439ce2010-11-19 13:56:21 -08002970 }
2971 }
2972
Dan Sandler26e81cf2014-05-06 10:01:27 -04002973 /**
2974 * Sets {@link Notification#color}.
2975 *
2976 * @param argb The accent color to use
2977 *
2978 * @return The same Builder.
2979 */
Tor Norbye80756e32015-03-02 09:39:27 -08002980 public Builder setColor(@ColorInt int argb) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002981 mN.color = argb;
Julia Reynolds10ee1fc2015-11-09 11:04:55 -05002982 sanitizeColor();
Dan Sandler26e81cf2014-05-06 10:01:27 -04002983 return this;
2984 }
2985
Jorim Jaggid05aa3e2014-08-28 17:52:27 +02002986 private Drawable getProfileBadgeDrawable() {
Christoph Studer7ac80e62014-08-04 16:01:57 +02002987 // Note: This assumes that the current user can read the profile badge of the
2988 // originating user.
Selim Cineke6ff9462016-01-15 15:07:06 -08002989 return mContext.getPackageManager().getUserBadgeForDensityNoBackground(
Julia Reynoldsda303542015-11-23 14:00:20 -05002990 new UserHandle(mContext.getUserId()), 0);
Jorim Jaggid05aa3e2014-08-28 17:52:27 +02002991 }
2992
2993 private Bitmap getProfileBadge() {
2994 Drawable badge = getProfileBadgeDrawable();
Kenny Guy8a0101b2014-05-08 23:34:12 +01002995 if (badge == null) {
2996 return null;
2997 }
Jorim Jaggid05aa3e2014-08-28 17:52:27 +02002998 final int size = mContext.getResources().getDimensionPixelSize(
2999 R.dimen.notification_badge_size);
3000 Bitmap bitmap = Bitmap.createBitmap(size, size, Bitmap.Config.ARGB_8888);
Kenny Guy8a0101b2014-05-08 23:34:12 +01003001 Canvas canvas = new Canvas(bitmap);
Jorim Jaggid05aa3e2014-08-28 17:52:27 +02003002 badge.setBounds(0, 0, size, size);
Kenny Guy8a0101b2014-05-08 23:34:12 +01003003 badge.draw(canvas);
3004 return bitmap;
3005 }
3006
Selim Cinekc848c3a2016-01-13 15:27:30 -08003007 private void bindProfileBadge(RemoteViews contentView) {
Kenny Guy98193ea2014-07-24 19:54:37 +01003008 Bitmap profileBadge = getProfileBadge();
3009
Kenny Guy98193ea2014-07-24 19:54:37 +01003010 if (profileBadge != null) {
Selim Cinekc848c3a2016-01-13 15:27:30 -08003011 contentView.setImageViewBitmap(R.id.profile_badge, profileBadge);
3012 contentView.setViewVisibility(R.id.profile_badge, View.VISIBLE);
Kenny Guy98193ea2014-07-24 19:54:37 +01003013 }
Kenny Guy98193ea2014-07-24 19:54:37 +01003014 }
3015
Christoph Studerfe718432014-09-01 18:21:18 +02003016 private void resetStandardTemplate(RemoteViews contentView) {
Selim Cinekeaa29ca2015-11-23 13:51:13 -08003017 resetNotificationHeader(contentView);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003018 resetContentMargins(contentView);
Christoph Studerfe718432014-09-01 18:21:18 +02003019 contentView.setViewVisibility(R.id.right_icon, View.GONE);
Selim Cinek860b6da2015-12-16 19:02:19 -08003020 contentView.setViewVisibility(R.id.title, View.GONE);
Christoph Studerfe718432014-09-01 18:21:18 +02003021 contentView.setTextViewText(R.id.title, null);
Selim Cinek41598732016-01-11 16:58:37 -08003022 contentView.setViewVisibility(R.id.text, View.GONE);
Christoph Studerfe718432014-09-01 18:21:18 +02003023 contentView.setTextViewText(R.id.text, null);
Selim Cinek29603462015-11-17 19:04:39 -08003024 contentView.setViewVisibility(R.id.text_line_1, View.GONE);
Selim Cinek41598732016-01-11 16:58:37 -08003025 contentView.setTextViewText(R.id.text_line_1, null);
Christoph Studerfe718432014-09-01 18:21:18 +02003026 contentView.setViewVisibility(R.id.progress, View.GONE);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003027 }
3028
Selim Cinekeaa29ca2015-11-23 13:51:13 -08003029 /**
3030 * Resets the notification header to its original state
3031 */
3032 private void resetNotificationHeader(RemoteViews contentView) {
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003033 contentView.setImageViewResource(R.id.icon, 0);
Selim Cinek7b836392015-12-04 20:02:59 -08003034 contentView.setBoolean(R.id.notification_header, "setExpanded", false);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003035 contentView.setTextViewText(R.id.app_name_text, null);
Christoph Studerca1db712014-09-10 17:31:33 +02003036 contentView.setViewVisibility(R.id.chronometer, View.GONE);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003037 contentView.setViewVisibility(R.id.header_sub_text, View.GONE);
Selim Cinek29603462015-11-17 19:04:39 -08003038 contentView.setViewVisibility(R.id.header_content_info, View.GONE);
Selim Cinek29603462015-11-17 19:04:39 -08003039 contentView.setViewVisibility(R.id.sub_text_divider, View.GONE);
3040 contentView.setViewVisibility(R.id.content_info_divider, View.GONE);
3041 contentView.setViewVisibility(R.id.time_divider, View.GONE);
Selim Cinekc848c3a2016-01-13 15:27:30 -08003042 contentView.setImageViewIcon(R.id.profile_badge, null);
3043 contentView.setViewVisibility(R.id.profile_badge, View.GONE);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003044 }
3045
3046 private void resetContentMargins(RemoteViews contentView) {
3047 contentView.setViewLayoutMarginEnd(R.id.line1, 0);
Selim Cinekc848c3a2016-01-13 15:27:30 -08003048 contentView.setViewLayoutMarginEnd(R.id.text, 0);
Christoph Studerfe718432014-09-01 18:21:18 +02003049 }
3050
Jorim Jaggi445d3c02014-08-19 22:33:42 +02003051 private RemoteViews applyStandardTemplate(int resId) {
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02003052 return applyStandardTemplate(resId, true /* hasProgress */);
3053 }
3054
3055 /**
3056 * @param hasProgress whether the progress bar should be shown and set
3057 */
3058 private RemoteViews applyStandardTemplate(int resId, boolean hasProgress) {
Kenny Guy77320062014-08-27 21:37:15 +01003059 RemoteViews contentView = new BuilderRemoteViews(mContext.getApplicationInfo(), resId);
Dan Sandler539aad42014-08-04 00:43:39 -04003060
Christoph Studerfe718432014-09-01 18:21:18 +02003061 resetStandardTemplate(contentView);
3062
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003063 final Bundle ex = mN.extras;
Dan Sandler190d58d2014-05-15 09:33:39 -04003064
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003065 bindNotificationHeader(contentView);
3066 bindLargeIcon(contentView);
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003067 if (ex.getCharSequence(EXTRA_TITLE) != null) {
Selim Cinek860b6da2015-12-16 19:02:19 -08003068 contentView.setViewVisibility(R.id.title, View.VISIBLE);
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003069 contentView.setTextViewText(R.id.title,
3070 processLegacyText(ex.getCharSequence(EXTRA_TITLE)));
Joe Onorato561d3852010-11-20 18:09:34 -08003071 }
Selim Cinek29603462015-11-17 19:04:39 -08003072 boolean showProgress = handleProgressBar(hasProgress, contentView, ex);
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003073 if (ex.getCharSequence(EXTRA_TEXT) != null) {
Selim Cinek41598732016-01-11 16:58:37 -08003074 int textId = showProgress ? com.android.internal.R.id.text_line_1
3075 : com.android.internal.R.id.text;
3076 contentView.setTextViewText(textId, processLegacyText(
3077 ex.getCharSequence(EXTRA_TEXT)));
3078 contentView.setViewVisibility(textId, View.VISIBLE);
Joe Onorato561d3852010-11-20 18:09:34 -08003079 }
Selim Cinekc848c3a2016-01-13 15:27:30 -08003080
Selim Cinek860b6da2015-12-16 19:02:19 -08003081 setContentMinHeight(contentView, showProgress || mN.mLargeIcon != null);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003082
Selim Cinek29603462015-11-17 19:04:39 -08003083 return contentView;
3084 }
3085
Selim Cinek860b6da2015-12-16 19:02:19 -08003086 /**
3087 * @param remoteView the remote view to update the minheight in
3088 * @param hasMinHeight does it have a mimHeight
3089 * @hide
3090 */
3091 void setContentMinHeight(RemoteViews remoteView, boolean hasMinHeight) {
3092 int minHeight = 0;
3093 if (hasMinHeight) {
3094 // we need to set the minHeight of the notification
3095 minHeight = mContext.getResources().getDimensionPixelSize(
3096 com.android.internal.R.dimen.notification_min_content_height);
3097 }
3098 remoteView.setInt(R.id.notification_main_column, "setMinimumHeight", minHeight);
3099 }
3100
Selim Cinek29603462015-11-17 19:04:39 -08003101 private boolean handleProgressBar(boolean hasProgress, RemoteViews contentView, Bundle ex) {
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003102 final int max = ex.getInt(EXTRA_PROGRESS_MAX, 0);
3103 final int progress = ex.getInt(EXTRA_PROGRESS, 0);
3104 final boolean ind = ex.getBoolean(EXTRA_PROGRESS_INDETERMINATE);
3105 if (hasProgress && (max != 0 || ind)) {
Selim Cinek29603462015-11-17 19:04:39 -08003106 contentView.setViewVisibility(com.android.internal.R.id.progress, View.VISIBLE);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003107 contentView.setProgressBar(
Selim Cinek29603462015-11-17 19:04:39 -08003108 R.id.progress, max, progress, ind);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003109 contentView.setProgressBackgroundTintList(
3110 R.id.progress, ColorStateList.valueOf(mContext.getColor(
3111 R.color.notification_progress_background_color)));
Selim Cinek29603462015-11-17 19:04:39 -08003112 if (mN.color != COLOR_DEFAULT) {
Adrian Roos4ff3b122016-02-01 12:26:13 -08003113 ColorStateList colorStateList = ColorStateList.valueOf(resolveContrastColor());
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003114 contentView.setProgressTintList(R.id.progress, colorStateList);
3115 contentView.setProgressIndeterminateTintList(R.id.progress, colorStateList);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003116 }
Selim Cinek29603462015-11-17 19:04:39 -08003117 return true;
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003118 } else {
3119 contentView.setViewVisibility(R.id.progress, View.GONE);
Selim Cinek29603462015-11-17 19:04:39 -08003120 return false;
Jeff Sharkey1c400132011-08-05 14:50:13 -07003121 }
Joe Onorato561d3852010-11-20 18:09:34 -08003122 }
3123
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003124 private void bindLargeIcon(RemoteViews contentView) {
3125 if (mN.mLargeIcon != null) {
3126 contentView.setViewVisibility(R.id.right_icon, View.VISIBLE);
3127 contentView.setImageViewIcon(R.id.right_icon, mN.mLargeIcon);
3128 processLargeLegacyIcon(mN.mLargeIcon, contentView);
3129 int endMargin = mContext.getResources().getDimensionPixelSize(
3130 R.dimen.notification_content_picture_margin);
3131 contentView.setViewLayoutMarginEnd(R.id.line1, endMargin);
Selim Cinekc848c3a2016-01-13 15:27:30 -08003132 contentView.setViewLayoutMarginEnd(R.id.text, endMargin);
Selim Cinek29603462015-11-17 19:04:39 -08003133 contentView.setViewLayoutMarginEnd(R.id.progress, endMargin);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003134 }
3135 }
3136
3137 private void bindNotificationHeader(RemoteViews contentView) {
3138 bindSmallIcon(contentView);
3139 bindHeaderAppName(contentView);
Selim Cinek03d0d652015-11-13 13:18:09 -05003140 bindHeaderSubText(contentView);
Selim Cinek29603462015-11-17 19:04:39 -08003141 bindContentInfo(contentView);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003142 bindHeaderChronometerAndTime(contentView);
3143 bindExpandButton(contentView);
Selim Cinekc848c3a2016-01-13 15:27:30 -08003144 bindProfileBadge(contentView);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003145 }
3146
Selim Cinek29603462015-11-17 19:04:39 -08003147 private void bindContentInfo(RemoteViews contentView) {
3148 boolean visible = false;
3149 if (mN.extras.getCharSequence(EXTRA_INFO_TEXT) != null) {
3150 contentView.setTextViewText(R.id.header_content_info,
3151 processLegacyText(mN.extras.getCharSequence(EXTRA_INFO_TEXT)));
3152 contentView.setViewVisibility(R.id.header_content_info, View.VISIBLE);
3153 visible = true;
3154 } else if (mN.number > 0) {
3155 final int tooBig = mContext.getResources().getInteger(
3156 R.integer.status_bar_notification_info_maxnum);
3157 if (mN.number > tooBig) {
3158 contentView.setTextViewText(R.id.header_content_info, processLegacyText(
3159 mContext.getResources().getString(
3160 R.string.status_bar_notification_info_overflow)));
3161 } else {
3162 contentView.setTextViewText(R.id.header_content_info,
3163 processLegacyText(String.valueOf(mN.number)));
3164 }
3165 contentView.setViewVisibility(R.id.header_content_info, View.VISIBLE);
3166 visible = true;
3167 }
3168 if (visible) {
3169 contentView.setViewVisibility(R.id.content_info_divider, View.VISIBLE);
3170 }
3171 }
3172
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003173 private void bindExpandButton(RemoteViews contentView) {
Adrian Roos4ff3b122016-02-01 12:26:13 -08003174 contentView.setDrawableParameters(R.id.expand_button, false, -1, resolveContrastColor(),
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003175 PorterDuff.Mode.SRC_ATOP, -1);
Selim Cinekea4bef72015-12-02 15:51:10 -08003176 contentView.setInt(R.id.notification_header, "setOriginalNotificationColor",
Adrian Roos4ff3b122016-02-01 12:26:13 -08003177 resolveContrastColor());
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003178 }
3179
3180 private void bindHeaderChronometerAndTime(RemoteViews contentView) {
3181 if (showsTimeOrChronometer()) {
Selim Cinek29603462015-11-17 19:04:39 -08003182 contentView.setViewVisibility(R.id.time_divider, View.VISIBLE);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003183 if (mN.extras.getBoolean(EXTRA_SHOW_CHRONOMETER)) {
3184 contentView.setViewVisibility(R.id.chronometer, View.VISIBLE);
3185 contentView.setLong(R.id.chronometer, "setBase",
3186 mN.when + (SystemClock.elapsedRealtime() - System.currentTimeMillis()));
3187 contentView.setBoolean(R.id.chronometer, "setStarted", true);
Selim Cinek81c23aa2016-02-25 16:23:13 -08003188 boolean countsDown = mN.extras.getBoolean(EXTRA_CHRONOMETER_COUNTS_DOWN);
3189 contentView.setChronometerCountsDown(R.id.chronometer, countsDown);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003190 } else {
3191 contentView.setViewVisibility(R.id.time, View.VISIBLE);
3192 contentView.setLong(R.id.time, "setTime", mN.when);
3193 }
3194 }
3195 }
3196
Selim Cinek03d0d652015-11-13 13:18:09 -05003197 private void bindHeaderSubText(RemoteViews contentView) {
3198 CharSequence subText = mN.extras.getCharSequence(EXTRA_SUB_TEXT);
3199 if (subText == null && mStyle != null && mStyle.mSummaryTextSet
3200 && mStyle.hasSummaryInHeader()) {
3201 subText = mStyle.mSummaryText;
3202 }
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003203 if (subText != null) {
3204 // TODO: Remove the span entirely to only have the string with propper formating.
3205 contentView.setTextViewText(R.id.header_sub_text, processLegacyText(subText));
3206 contentView.setViewVisibility(R.id.header_sub_text, View.VISIBLE);
Selim Cinek29603462015-11-17 19:04:39 -08003207 contentView.setViewVisibility(R.id.sub_text_divider, View.VISIBLE);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003208 }
3209 }
3210
3211 private void bindHeaderAppName(RemoteViews contentView) {
Tony Mak4dc008c2016-03-16 10:46:49 +00003212 CharSequence appName = mContext.getPackageManager()
3213 .getApplicationLabel(mContext.getApplicationInfo());
3214
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003215 if (TextUtils.isEmpty(appName)) {
3216 return;
3217 }
3218 contentView.setTextViewText(R.id.app_name_text, appName);
Adrian Roos4ff3b122016-02-01 12:26:13 -08003219 contentView.setTextColor(R.id.app_name_text, resolveContrastColor());
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003220 }
3221
3222 private void bindSmallIcon(RemoteViews contentView) {
3223 contentView.setImageViewIcon(R.id.icon, mN.mSmallIcon);
3224 processSmallIconColor(mN.mSmallIcon, contentView);
3225 }
3226
Jorim Jaggi445d3c02014-08-19 22:33:42 +02003227 /**
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02003228 * @return true if the built notification will show the time or the chronometer; false
3229 * otherwise
3230 */
3231 private boolean showsTimeOrChronometer() {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003232 return mN.when != 0 && mN.extras.getBoolean(EXTRA_SHOW_WHEN);
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02003233 }
3234
Christoph Studerfe718432014-09-01 18:21:18 +02003235 private void resetStandardTemplateWithActions(RemoteViews big) {
Adrian Roos7052de52016-03-03 15:53:34 -08003236 big.setViewVisibility(R.id.actions_container, View.GONE);
Christoph Studerfe718432014-09-01 18:21:18 +02003237 big.setViewVisibility(R.id.actions, View.GONE);
Christoph Studerfe718432014-09-01 18:21:18 +02003238 big.removeAllViews(R.id.actions);
Adrian Roose458aa82015-12-08 16:17:19 -08003239
3240 big.setViewVisibility(R.id.notification_material_reply_container, View.GONE);
3241 big.setTextViewText(R.id.notification_material_reply_text_1, null);
3242
3243 big.setViewVisibility(R.id.notification_material_reply_text_2, View.GONE);
3244 big.setTextViewText(R.id.notification_material_reply_text_2, null);
3245 big.setViewVisibility(R.id.notification_material_reply_text_3, View.GONE);
3246 big.setTextViewText(R.id.notification_material_reply_text_3, null);
Christoph Studerfe718432014-09-01 18:21:18 +02003247 }
3248
Daniel Sandler96fd7c12012-03-30 16:37:36 -04003249 private RemoteViews applyStandardTemplateWithActions(int layoutId) {
Jorim Jaggi445d3c02014-08-19 22:33:42 +02003250 RemoteViews big = applyStandardTemplate(layoutId);
Daniel Sandler96fd7c12012-03-30 16:37:36 -04003251
Christoph Studerfe718432014-09-01 18:21:18 +02003252 resetStandardTemplateWithActions(big);
3253
Adrian Roose458aa82015-12-08 16:17:19 -08003254 boolean validRemoteInput = false;
3255
Daniel Sandler96fd7c12012-03-30 16:37:36 -04003256 int N = mActions.size();
3257 if (N > 0) {
Adrian Roos7052de52016-03-03 15:53:34 -08003258 big.setViewVisibility(R.id.actions_container, View.VISIBLE);
Daniel Sandler96fd7c12012-03-30 16:37:36 -04003259 big.setViewVisibility(R.id.actions, View.VISIBLE);
Daniel Sandler8680bf82012-05-15 16:52:52 -04003260 if (N>MAX_ACTION_BUTTONS) N=MAX_ACTION_BUTTONS;
Daniel Sandler96fd7c12012-03-30 16:37:36 -04003261 for (int i=0; i<N; i++) {
Adrian Roose458aa82015-12-08 16:17:19 -08003262 Action action = mActions.get(i);
3263 validRemoteInput |= hasValidRemoteInput(action);
3264
3265 final RemoteViews button = generateActionButton(action);
Adrian Roos9b123cf2016-02-04 14:55:57 -08003266 if (i == N - 1) {
3267 button.setViewLayoutWidth(com.android.internal.R.id.action0,
3268 ViewGroup.LayoutParams.MATCH_PARENT);
3269 }
Daniel Sandler96fd7c12012-03-30 16:37:36 -04003270 big.addView(R.id.actions, button);
3271 }
3272 }
Adrian Roose458aa82015-12-08 16:17:19 -08003273
3274 CharSequence[] replyText = mN.extras.getCharSequenceArray(EXTRA_REMOTE_INPUT_HISTORY);
3275 if (validRemoteInput && replyText != null
3276 && replyText.length > 0 && !TextUtils.isEmpty(replyText[0])) {
3277 big.setViewVisibility(R.id.notification_material_reply_container, View.VISIBLE);
3278 big.setTextViewText(R.id.notification_material_reply_text_1, replyText[0]);
3279
3280 if (replyText.length > 1 && !TextUtils.isEmpty(replyText[1])) {
3281 big.setViewVisibility(R.id.notification_material_reply_text_2, View.VISIBLE);
3282 big.setTextViewText(R.id.notification_material_reply_text_2, replyText[1]);
3283
3284 if (replyText.length > 2 && !TextUtils.isEmpty(replyText[2])) {
3285 big.setViewVisibility(
3286 R.id.notification_material_reply_text_3, View.VISIBLE);
3287 big.setTextViewText(R.id.notification_material_reply_text_3, replyText[2]);
3288 }
3289 }
3290 }
3291
Daniel Sandler96fd7c12012-03-30 16:37:36 -04003292 return big;
3293 }
3294
Adrian Roose458aa82015-12-08 16:17:19 -08003295 private boolean hasValidRemoteInput(Action action) {
3296 if (TextUtils.isEmpty(action.title) || action.actionIntent == null) {
3297 // Weird actions
3298 return false;
3299 }
3300
3301 RemoteInput[] remoteInputs = action.getRemoteInputs();
3302 if (remoteInputs == null) {
3303 return false;
3304 }
3305
3306 for (RemoteInput r : remoteInputs) {
3307 CharSequence[] choices = r.getChoices();
3308 if (r.getAllowFreeFormInput() || (choices != null && choices.length != 0)) {
3309 return true;
3310 }
3311 }
3312 return false;
3313 }
3314
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003315 /**
3316 * Construct a RemoteViews for the final 1U notification layout. In order:
3317 * 1. Custom contentView from the caller
3318 * 2. Style's proposed content view
3319 * 3. Standard template view
3320 */
Julia Reynolds3b848122016-02-26 10:45:32 -05003321 public RemoteViews createContentView() {
Selim Cinek593610c2016-02-16 18:42:57 -08003322 if (mN.contentView != null && (mStyle == null || !mStyle.displayCustomViewInline())) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003323 return mN.contentView;
3324 } else if (mStyle != null) {
3325 final RemoteViews styleView = mStyle.makeContentView();
3326 if (styleView != null) {
3327 return styleView;
3328 }
Joe Onorato46439ce2010-11-19 13:56:21 -08003329 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003330 return applyStandardTemplate(getBaseLayoutResource());
Joe Onorato46439ce2010-11-19 13:56:21 -08003331 }
3332
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003333 /**
3334 * Construct a RemoteViews for the final big notification layout.
3335 */
Julia Reynolds3b848122016-02-26 10:45:32 -05003336 public RemoteViews createBigContentView() {
Selim Cinek850a8542015-11-11 11:48:36 -05003337 RemoteViews result = null;
Selim Cinek593610c2016-02-16 18:42:57 -08003338 if (mN.bigContentView != null
3339 && (mStyle == null || !mStyle.displayCustomViewInline())) {
Julia Reynolds089e3e42015-11-18 09:59:57 -05003340 return mN.bigContentView;
3341 } else if (mStyle != null) {
Selim Cinek850a8542015-11-11 11:48:36 -05003342 result = mStyle.makeBigContentView();
Selim Cinek90dcf6d2015-11-18 20:24:13 -08003343 hideLine1Text(result);
Selim Cinekcc10bfb2016-02-10 16:24:21 -08003344 } else if (mActions.size() != 0) {
3345 result = applyStandardTemplateWithActions(getBigBaseLayoutResource());
Selim Cinek850a8542015-11-11 11:48:36 -05003346 }
3347 adaptNotificationHeaderForBigContentView(result);
3348 return result;
3349 }
Daniel Sandler96fd7c12012-03-30 16:37:36 -04003350
Selim Cinekeaa29ca2015-11-23 13:51:13 -08003351 /**
3352 * Construct a RemoteViews for the final notification header only
3353 *
3354 * @hide
3355 */
3356 public RemoteViews makeNotificationHeader() {
3357 RemoteViews header = new BuilderRemoteViews(mContext.getApplicationInfo(),
3358 R.layout.notification_template_header);
3359 resetNotificationHeader(header);
3360 bindNotificationHeader(header);
3361 return header;
3362 }
3363
Selim Cinek29603462015-11-17 19:04:39 -08003364 private void hideLine1Text(RemoteViews result) {
Selim Cinekcc10bfb2016-02-10 16:24:21 -08003365 if (result != null) {
3366 result.setViewVisibility(R.id.text_line_1, View.GONE);
3367 }
Selim Cinek29603462015-11-17 19:04:39 -08003368 }
3369
Selim Cinek850a8542015-11-11 11:48:36 -05003370 private void adaptNotificationHeaderForBigContentView(RemoteViews result) {
Selim Cinekcc10bfb2016-02-10 16:24:21 -08003371 if (result != null) {
3372 result.setBoolean(R.id.notification_header, "setExpanded", true);
3373 }
Daniel Sandler96fd7c12012-03-30 16:37:36 -04003374 }
3375
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003376 /**
3377 * Construct a RemoteViews for the final heads-up notification layout.
3378 */
Julia Reynolds3b848122016-02-26 10:45:32 -05003379 public RemoteViews createHeadsUpContentView() {
Selim Cinek593610c2016-02-16 18:42:57 -08003380 if (mN.headsUpContentView != null
3381 && (mStyle == null || !mStyle.displayCustomViewInline())) {
Julia Reynolds089e3e42015-11-18 09:59:57 -05003382 return mN.headsUpContentView;
3383 } else if (mStyle != null) {
3384 final RemoteViews styleView = mStyle.makeHeadsUpContentView();
3385 if (styleView != null) {
3386 return styleView;
3387 }
3388 } else if (mActions.size() == 0) {
3389 return null;
3390 }
3391
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01003392 return applyStandardTemplateWithActions(getBigBaseLayoutResource());
Chris Wren8fd39ec2014-02-27 17:43:26 -05003393 }
3394
Selim Cinek624c02db2015-12-14 21:00:02 -08003395 /**
3396 * Construct a RemoteViews for the display in public contexts like on the lockscreen.
3397 *
3398 * @hide
3399 */
3400 public RemoteViews makePublicContentView() {
3401 if (mN.publicVersion != null) {
3402 final Builder builder = recoverBuilder(mContext, mN.publicVersion);
Julia Reynolds3b848122016-02-26 10:45:32 -05003403 return builder.createContentView();
Selim Cinek624c02db2015-12-14 21:00:02 -08003404 }
3405 Bundle savedBundle = mN.extras;
3406 Style style = mStyle;
3407 mStyle = null;
3408 Icon largeIcon = mN.mLargeIcon;
3409 mN.mLargeIcon = null;
3410 Bundle publicExtras = new Bundle();
3411 publicExtras.putBoolean(EXTRA_SHOW_WHEN,
3412 savedBundle.getBoolean(EXTRA_SHOW_WHEN));
3413 publicExtras.putBoolean(EXTRA_SHOW_CHRONOMETER,
3414 savedBundle.getBoolean(EXTRA_SHOW_CHRONOMETER));
Selim Cinek81c23aa2016-02-25 16:23:13 -08003415 publicExtras.putBoolean(EXTRA_CHRONOMETER_COUNTS_DOWN,
3416 savedBundle.getBoolean(EXTRA_CHRONOMETER_COUNTS_DOWN));
Selim Cinek624c02db2015-12-14 21:00:02 -08003417 publicExtras.putCharSequence(EXTRA_TITLE,
3418 mContext.getString(R.string.notification_hidden_text));
3419 mN.extras = publicExtras;
3420 final RemoteViews publicView = applyStandardTemplate(getBaseLayoutResource());
3421 mN.extras = savedBundle;
3422 mN.mLargeIcon = largeIcon;
3423 mStyle = style;
3424 return publicView;
3425 }
3426
3427
Chris Wren8fd39ec2014-02-27 17:43:26 -05003428
Daniel Sandler96fd7c12012-03-30 16:37:36 -04003429 private RemoteViews generateActionButton(Action action) {
Daniel Sandler8680bf82012-05-15 16:52:52 -04003430 final boolean tombstone = (action.actionIntent == null);
Selim Cinekf33b1112015-07-15 17:45:11 -07003431 RemoteViews button = new BuilderRemoteViews(mContext.getApplicationInfo(),
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01003432 tombstone ? getActionTombstoneLayoutResource()
3433 : getActionLayoutResource());
Dan Sandler68079d52015-07-22 10:45:30 -04003434 final Icon ai = action.getIcon();
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01003435 button.setTextViewText(R.id.action0, processLegacyText(action.title));
Daniel Sandler8680bf82012-05-15 16:52:52 -04003436 if (!tombstone) {
Daniel Sandlere5518842012-05-10 16:20:40 -04003437 button.setOnClickPendingIntent(R.id.action0, action.actionIntent);
Daniel Sandlere5518842012-05-10 16:20:40 -04003438 }
Daniel Sandler96fd7c12012-03-30 16:37:36 -04003439 button.setContentDescription(R.id.action0, action.title);
Adrian Roosfe84e1f2015-11-04 15:55:39 -08003440 if (action.mRemoteInputs != null) {
3441 button.setRemoteInputs(R.id.action0, action.mRemoteInputs);
3442 }
3443 if (mN.color != COLOR_DEFAULT) {
Adrian Roos4ff3b122016-02-01 12:26:13 -08003444 button.setTextColor(R.id.action0, resolveContrastColor());
Adrian Roosfe84e1f2015-11-04 15:55:39 -08003445 }
Daniel Sandler96fd7c12012-03-30 16:37:36 -04003446 return button;
3447 }
3448
Joe Onoratocb109a02011-01-18 17:57:41 -08003449 /**
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01003450 * @return Whether we are currently building a notification from a legacy (an app that
Alan Viverette3cb07a462014-06-06 14:19:53 -07003451 * doesn't create material notifications by itself) app.
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01003452 */
3453 private boolean isLegacy() {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003454 return getColorUtil() != null;
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01003455 }
3456
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01003457 private CharSequence processLegacyText(CharSequence charSequence) {
3458 if (isLegacy()) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003459 return getColorUtil().invertCharSequenceColors(charSequence);
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01003460 } else {
3461 return charSequence;
3462 }
3463 }
3464
Dan Sandler26e81cf2014-05-06 10:01:27 -04003465 /**
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003466 * Apply any necessariy colors to the small icon
Dan Sandler26e81cf2014-05-06 10:01:27 -04003467 */
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003468 private void processSmallIconColor(Icon smallIcon, RemoteViews contentView) {
Selim Cinekea4bef72015-12-02 15:51:10 -08003469 boolean colorable = !isLegacy() || getColorUtil().isGrayscaleIcon(mContext, smallIcon);
3470 if (colorable) {
Adrian Roos4ff3b122016-02-01 12:26:13 -08003471 contentView.setDrawableParameters(R.id.icon, false, -1, resolveContrastColor(),
Jorim Jaggi92df1f22014-12-16 19:44:41 +01003472 PorterDuff.Mode.SRC_ATOP, -1);
Selim Cinekea4bef72015-12-02 15:51:10 -08003473
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01003474 }
Selim Cinekea4bef72015-12-02 15:51:10 -08003475 contentView.setInt(R.id.notification_header, "setOriginalIconColor",
Adrian Roos4ff3b122016-02-01 12:26:13 -08003476 colorable ? resolveContrastColor() : NotificationHeaderView.NO_COLOR);
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01003477 }
3478
Dan Sandler26e81cf2014-05-06 10:01:27 -04003479 /**
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003480 * Make the largeIcon dark if it's a fake smallIcon (that is,
Dan Sandler26e81cf2014-05-06 10:01:27 -04003481 * if it's grayscale).
3482 */
3483 // TODO: also check bounds, transparency, that sort of thing.
Dan Sandlerd63f9322015-05-06 15:18:49 -04003484 private void processLargeLegacyIcon(Icon largeIcon, RemoteViews contentView) {
3485 if (largeIcon != null && isLegacy()
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003486 && getColorUtil().isGrayscaleIcon(mContext, largeIcon)) {
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003487 // resolve color will fall back to the default when legacy
Adrian Roos4ff3b122016-02-01 12:26:13 -08003488 contentView.setDrawableParameters(R.id.icon, false, -1, resolveContrastColor(),
Dan Sandler190d58d2014-05-15 09:33:39 -04003489 PorterDuff.Mode.SRC_ATOP, -1);
Jorim Jaggi92df1f22014-12-16 19:44:41 +01003490 }
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01003491 }
3492
Julia Reynolds10ee1fc2015-11-09 11:04:55 -05003493 private void sanitizeColor() {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003494 if (mN.color != COLOR_DEFAULT) {
3495 mN.color |= 0xFF000000; // no alpha for custom colors
Jorim Jaggi74419312014-06-10 20:57:21 +02003496 }
Jorim Jaggi74419312014-06-10 20:57:21 +02003497 }
3498
Adrian Roos4ff3b122016-02-01 12:26:13 -08003499 int resolveContrastColor() {
3500 if (mCachedContrastColorIsFor == mN.color && mCachedContrastColor != COLOR_INVALID) {
3501 return mCachedContrastColor;
Dan Sandler26e81cf2014-05-06 10:01:27 -04003502 }
Adrian Roos4ff3b122016-02-01 12:26:13 -08003503 final int contrasted = NotificationColorUtil.resolveContrastColor(mContext, mN.color);
3504
3505 mCachedContrastColorIsFor = mN.color;
3506 return mCachedContrastColor = contrasted;
Dan Sandler26e81cf2014-05-06 10:01:27 -04003507 }
3508
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01003509 /**
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003510 * Apply the unstyled operations and return a new {@link Notification} object.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04003511 * @hide
Joe Onoratocb109a02011-01-18 17:57:41 -08003512 */
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04003513 public Notification buildUnstyled() {
Daniel Sandlera0a938c2012-03-15 08:42:37 -04003514 if (mActions.size() > 0) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003515 mN.actions = new Action[mActions.size()];
3516 mActions.toArray(mN.actions);
Daniel Sandlera0a938c2012-03-15 08:42:37 -04003517 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003518 if (!mPersonList.isEmpty()) {
3519 mN.extras.putStringArray(EXTRA_PEOPLE,
3520 mPersonList.toArray(new String[mPersonList.size()]));
Dan Sandler0bf2ed82013-12-21 23:33:41 -06003521 }
Selim Cinek247fa012016-02-18 09:50:48 -08003522 if (mN.bigContentView != null || mN.contentView != null
3523 || mN.headsUpContentView != null) {
3524 mN.extras.putBoolean(EXTRA_CONTAINS_CUSTOM_VIEW, true);
3525 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003526 return mN;
Joe Onorato46439ce2010-11-19 13:56:21 -08003527 }
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003528
Julia Reynolds3b848122016-02-26 10:45:32 -05003529 /**
3530 * Creates a Builder from an existing notification so further changes can be made.
3531 * @param context The context for your application / activity.
3532 * @param n The notification to create a Builder from.
3533 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003534 public static Notification.Builder recoverBuilder(Context context, Notification n) {
Christoph Studer4600f9b2014-07-22 22:44:43 +02003535 // Re-create notification context so we can access app resources.
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003536 ApplicationInfo applicationInfo = n.extras.getParcelable(
3537 EXTRA_BUILDER_APPLICATION_INFO);
Christoph Studer4600f9b2014-07-22 22:44:43 +02003538 Context builderContext;
Julia Reynoldsda303542015-11-23 14:00:20 -05003539 if (applicationInfo != null) {
3540 try {
3541 builderContext = context.createApplicationContext(applicationInfo,
3542 Context.CONTEXT_RESTRICTED);
3543 } catch (NameNotFoundException e) {
3544 Log.e(TAG, "ApplicationInfo " + applicationInfo + " not found");
3545 builderContext = context; // try with our context
3546 }
3547 } else {
3548 builderContext = context; // try with given context
Christoph Studer4600f9b2014-07-22 22:44:43 +02003549 }
3550
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003551 return new Builder(builderContext, n);
Christoph Studer4600f9b2014-07-22 22:44:43 +02003552 }
3553
3554 private static Class<? extends Style> getNotificationStyleClass(String templateClass) {
Selim Cinek593610c2016-02-16 18:42:57 -08003555 Class<? extends Style>[] classes = new Class[] {
3556 BigTextStyle.class, BigPictureStyle.class, InboxStyle.class, MediaStyle.class,
Selim Cinek03eb3b72016-02-18 10:39:45 -08003557 DecoratedCustomViewStyle.class, DecoratedMediaCustomViewStyle.class };
Christoph Studer4600f9b2014-07-22 22:44:43 +02003558 for (Class<? extends Style> innerClass : classes) {
3559 if (templateClass.equals(innerClass.getName())) {
3560 return innerClass;
3561 }
3562 }
3563 return null;
3564 }
3565
Daniel Sandlerf45564e2013-04-15 15:05:08 -04003566 /**
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003567 * @deprecated Use {@link #build()} instead.
3568 */
3569 @Deprecated
3570 public Notification getNotification() {
3571 return build();
3572 }
3573
3574 /**
3575 * Combine all of the options that have been set and return a new {@link Notification}
3576 * object.
3577 */
3578 public Notification build() {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003579 // first, add any extras from the calling code
3580 if (mUserExtras != null) {
3581 mN.extras = getAllExtras();
Jorim Jaggia0d58ae2015-06-03 11:48:13 -07003582 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003583
3584 // lazy stuff from mContext; see comment in Builder(Context, Notification)
Julia Reynoldsda303542015-11-23 14:00:20 -05003585 Notification.addFieldsFromContext(mContext, mN);
Christoph Studer943aa672014-08-03 20:31:16 +02003586
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003587 buildUnstyled();
Daniel Sandlerf45564e2013-04-15 15:05:08 -04003588
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003589 if (mStyle != null) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003590 mStyle.buildStyled(mN);
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003591 }
Daniel Sandlerf45564e2013-04-15 15:05:08 -04003592
Adrian Roos5081c0d2016-02-26 16:04:19 -08003593 if (mContext.getApplicationInfo().targetSdkVersion < Build.VERSION_CODES.N
3594 && (mStyle == null || !mStyle.displayCustomViewInline())) {
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05003595 if (mN.contentView == null) {
Julia Reynolds3b848122016-02-26 10:45:32 -05003596 mN.contentView = createContentView();
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05003597 mN.extras.putInt(EXTRA_REBUILD_CONTENT_VIEW_ACTION_COUNT,
3598 mN.contentView.getSequenceNumber());
3599 }
3600 if (mN.bigContentView == null) {
Julia Reynolds3b848122016-02-26 10:45:32 -05003601 mN.bigContentView = createBigContentView();
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05003602 if (mN.bigContentView != null) {
3603 mN.extras.putInt(EXTRA_REBUILD_BIG_CONTENT_VIEW_ACTION_COUNT,
3604 mN.bigContentView.getSequenceNumber());
3605 }
3606 }
3607 if (mN.headsUpContentView == null) {
Julia Reynolds3b848122016-02-26 10:45:32 -05003608 mN.headsUpContentView = createHeadsUpContentView();
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05003609 if (mN.headsUpContentView != null) {
3610 mN.extras.putInt(EXTRA_REBUILD_HEADS_UP_CONTENT_VIEW_ACTION_COUNT,
3611 mN.headsUpContentView.getSequenceNumber());
3612 }
3613 }
3614 }
3615
Julia Reynolds4c0c2022016-02-02 15:11:59 -05003616 if ((mN.defaults & DEFAULT_LIGHTS) != 0) {
3617 mN.flags |= FLAG_SHOW_LIGHTS;
3618 }
3619
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003620 return mN;
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003621 }
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05003622
3623 /**
3624 * Apply this Builder to an existing {@link Notification} object.
3625 *
3626 * @hide
3627 */
3628 public Notification buildInto(Notification n) {
Daniel Sandler1a497d32013-04-18 14:52:45 -04003629 build().cloneInto(n, true);
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05003630 return n;
3631 }
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01003632
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05003633 /**
Adrian Roos184bfe022016-03-03 13:41:44 -08003634 * Removes RemoteViews that were created for compatibility from {@param n}, if they did not
3635 * change.
3636 *
3637 * @return {@param n}, if no stripping is needed, otherwise a stripped clone of {@param n}.
3638 *
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05003639 * @hide
3640 */
Adrian Roos184bfe022016-03-03 13:41:44 -08003641 public static Notification maybeCloneStrippedForDelivery(Notification n) {
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05003642 String templateClass = n.extras.getString(EXTRA_TEMPLATE);
Adrian Roos184bfe022016-03-03 13:41:44 -08003643
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05003644 // Only strip views for known Styles because we won't know how to
3645 // re-create them otherwise.
Adrian Roos184bfe022016-03-03 13:41:44 -08003646 if (!TextUtils.isEmpty(templateClass)
3647 && getNotificationStyleClass(templateClass) == null) {
3648 return n;
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05003649 }
Adrian Roos184bfe022016-03-03 13:41:44 -08003650
3651 // Only strip unmodified BuilderRemoteViews.
3652 boolean stripContentView = n.contentView instanceof BuilderRemoteViews &&
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05003653 n.extras.getInt(EXTRA_REBUILD_CONTENT_VIEW_ACTION_COUNT, -1) ==
Adrian Roos184bfe022016-03-03 13:41:44 -08003654 n.contentView.getSequenceNumber();
3655 boolean stripBigContentView = n.bigContentView instanceof BuilderRemoteViews &&
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05003656 n.extras.getInt(EXTRA_REBUILD_BIG_CONTENT_VIEW_ACTION_COUNT, -1) ==
Adrian Roos184bfe022016-03-03 13:41:44 -08003657 n.bigContentView.getSequenceNumber();
3658 boolean stripHeadsUpContentView = n.headsUpContentView instanceof BuilderRemoteViews &&
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05003659 n.extras.getInt(EXTRA_REBUILD_HEADS_UP_CONTENT_VIEW_ACTION_COUNT, -1) ==
Adrian Roos184bfe022016-03-03 13:41:44 -08003660 n.headsUpContentView.getSequenceNumber();
3661
3662 // Nothing to do here, no need to clone.
3663 if (!stripContentView && !stripBigContentView && !stripHeadsUpContentView) {
3664 return n;
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05003665 }
Adrian Roos184bfe022016-03-03 13:41:44 -08003666
3667 Notification clone = n.clone();
3668 if (stripContentView) {
3669 clone.contentView = null;
3670 clone.extras.remove(EXTRA_REBUILD_CONTENT_VIEW_ACTION_COUNT);
3671 }
3672 if (stripBigContentView) {
3673 clone.bigContentView = null;
3674 clone.extras.remove(EXTRA_REBUILD_BIG_CONTENT_VIEW_ACTION_COUNT);
3675 }
3676 if (stripHeadsUpContentView) {
3677 clone.headsUpContentView = null;
3678 clone.extras.remove(EXTRA_REBUILD_HEADS_UP_CONTENT_VIEW_ACTION_COUNT);
3679 }
3680 return clone;
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05003681 }
3682
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01003683 private int getBaseLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07003684 return R.layout.notification_template_material_base;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01003685 }
3686
3687 private int getBigBaseLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07003688 return R.layout.notification_template_material_big_base;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01003689 }
3690
3691 private int getBigPictureLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07003692 return R.layout.notification_template_material_big_picture;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01003693 }
3694
3695 private int getBigTextLayoutResource() {
Jorim Jaggi445d3c02014-08-19 22:33:42 +02003696 return R.layout.notification_template_material_big_text;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01003697 }
3698
3699 private int getInboxLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07003700 return R.layout.notification_template_material_inbox;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01003701 }
3702
3703 private int getActionLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07003704 return R.layout.notification_material_action;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01003705 }
3706
3707 private int getActionTombstoneLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07003708 return R.layout.notification_material_action_tombstone;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01003709 }
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003710 }
3711
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003712 /**
3713 * An object that can apply a rich notification style to a {@link Notification.Builder}
3714 * object.
3715 */
Griff Hazendfcb0802014-02-11 12:00:00 -08003716 public static abstract class Style {
Chris Wrend6297db2012-05-03 16:20:13 -04003717 private CharSequence mBigContentTitle;
Jorim Jaggi457a10d2014-09-08 16:18:23 +02003718
3719 /**
3720 * @hide
3721 */
3722 protected CharSequence mSummaryText = null;
3723
3724 /**
3725 * @hide
3726 */
3727 protected boolean mSummaryTextSet = false;
Chris Wrend6297db2012-05-03 16:20:13 -04003728
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003729 protected Builder mBuilder;
3730
Chris Wrend6297db2012-05-03 16:20:13 -04003731 /**
3732 * Overrides ContentTitle in the big form of the template.
3733 * This defaults to the value passed to setContentTitle().
3734 */
3735 protected void internalSetBigContentTitle(CharSequence title) {
3736 mBigContentTitle = title;
3737 }
3738
3739 /**
3740 * Set the first line of text after the detail section in the big form of the template.
3741 */
3742 protected void internalSetSummaryText(CharSequence cs) {
3743 mSummaryText = cs;
Daniel Sandler619738c2012-06-07 16:33:08 -04003744 mSummaryTextSet = true;
Chris Wrend6297db2012-05-03 16:20:13 -04003745 }
3746
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003747 public void setBuilder(Builder builder) {
3748 if (mBuilder != builder) {
3749 mBuilder = builder;
Daniel Sandlerc08dea22012-06-28 08:35:24 -07003750 if (mBuilder != null) {
3751 mBuilder.setStyle(this);
3752 }
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003753 }
3754 }
3755
Chris Wrend6297db2012-05-03 16:20:13 -04003756 protected void checkBuilder() {
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003757 if (mBuilder == null) {
3758 throw new IllegalArgumentException("Style requires a valid Builder object");
3759 }
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003760 }
Chris Wrend6297db2012-05-03 16:20:13 -04003761
3762 protected RemoteViews getStandardView(int layoutId) {
3763 checkBuilder();
3764
Christoph Studer4600f9b2014-07-22 22:44:43 +02003765 // Nasty.
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003766 CharSequence oldBuilderContentTitle =
3767 mBuilder.getAllExtras().getCharSequence(EXTRA_TITLE);
Chris Wrend6297db2012-05-03 16:20:13 -04003768 if (mBigContentTitle != null) {
3769 mBuilder.setContentTitle(mBigContentTitle);
3770 }
3771
Chris Wrend6297db2012-05-03 16:20:13 -04003772 RemoteViews contentView = mBuilder.applyStandardTemplateWithActions(layoutId);
3773
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003774 mBuilder.getAllExtras().putCharSequence(EXTRA_TITLE, oldBuilderContentTitle);
Christoph Studer4600f9b2014-07-22 22:44:43 +02003775
Chris Wrend6297db2012-05-03 16:20:13 -04003776 if (mBigContentTitle != null && mBigContentTitle.equals("")) {
3777 contentView.setViewVisibility(R.id.line1, View.GONE);
Chris Wren67dc9a02012-05-16 01:03:20 -04003778 } else {
3779 contentView.setViewVisibility(R.id.line1, View.VISIBLE);
Chris Wrend6297db2012-05-03 16:20:13 -04003780 }
3781
Chris Wrend6297db2012-05-03 16:20:13 -04003782 return contentView;
3783 }
3784
Daniel Sandlerf45564e2013-04-15 15:05:08 -04003785 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003786 * Construct a Style-specific RemoteViews for the final 1U notification layout.
3787 * The default implementation has nothing additional to add.
3788 * @hide
3789 */
3790 public RemoteViews makeContentView() {
3791 return null;
3792 }
3793
3794 /**
3795 * Construct a Style-specific RemoteViews for the final big notification layout.
3796 * @hide
3797 */
3798 public RemoteViews makeBigContentView() {
3799 return null;
3800 }
3801
3802 /**
3803 * Construct a Style-specific RemoteViews for the final HUN layout.
3804 * @hide
3805 */
3806 public RemoteViews makeHeadsUpContentView() {
3807 return null;
3808 }
3809
3810 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003811 * Apply any style-specific extras to this notification before shipping it out.
Daniel Sandlerf45564e2013-04-15 15:05:08 -04003812 * @hide
3813 */
3814 public void addExtras(Bundle extras) {
3815 if (mSummaryTextSet) {
3816 extras.putCharSequence(EXTRA_SUMMARY_TEXT, mSummaryText);
3817 }
3818 if (mBigContentTitle != null) {
3819 extras.putCharSequence(EXTRA_TITLE_BIG, mBigContentTitle);
3820 }
Chris Wren91ad5632013-06-05 15:05:57 -04003821 extras.putString(EXTRA_TEMPLATE, this.getClass().getName());
Daniel Sandlerf45564e2013-04-15 15:05:08 -04003822 }
3823
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04003824 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003825 * Reconstruct the internal state of this Style object from extras.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04003826 * @hide
3827 */
Christoph Studer4600f9b2014-07-22 22:44:43 +02003828 protected void restoreFromExtras(Bundle extras) {
3829 if (extras.containsKey(EXTRA_SUMMARY_TEXT)) {
3830 mSummaryText = extras.getCharSequence(EXTRA_SUMMARY_TEXT);
3831 mSummaryTextSet = true;
3832 }
3833 if (extras.containsKey(EXTRA_TITLE_BIG)) {
3834 mBigContentTitle = extras.getCharSequence(EXTRA_TITLE_BIG);
3835 }
3836 }
3837
3838
3839 /**
3840 * @hide
3841 */
3842 public Notification buildStyled(Notification wip) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003843 addExtras(wip.extras);
Christoph Studer4600f9b2014-07-22 22:44:43 +02003844 return wip;
3845 }
3846
Daniel Sandler0ec46202015-06-24 01:27:05 -04003847 /**
3848 * @hide
3849 */
Jorim Jaggia0d58ae2015-06-03 11:48:13 -07003850 public void purgeResources() {}
3851
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04003852 /**
3853 * Calls {@link android.app.Notification.Builder#build()} on the Builder this Style is
3854 * attached to.
3855 *
3856 * @return the fully constructed Notification.
3857 */
3858 public Notification build() {
3859 checkBuilder();
3860 return mBuilder.build();
3861 }
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02003862
3863 /**
3864 * @hide
3865 * @return true if the style positions the progress bar on the second line; false if the
3866 * style hides the progress bar
3867 */
3868 protected boolean hasProgress() {
3869 return true;
3870 }
Selim Cinek03d0d652015-11-13 13:18:09 -05003871
3872 /**
3873 * @hide
3874 * @return Whether we should put the summary be put into the notification header
3875 */
3876 public boolean hasSummaryInHeader() {
3877 return true;
3878 }
Selim Cinek593610c2016-02-16 18:42:57 -08003879
3880 /**
3881 * @hide
3882 * @return Whether custom content views are displayed inline in the style
3883 */
3884 public boolean displayCustomViewInline() {
3885 return false;
3886 }
Joe Onorato46439ce2010-11-19 13:56:21 -08003887 }
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003888
3889 /**
Daniel Sandler4dfbe832012-04-11 14:51:46 -04003890 * Helper class for generating large-format notifications that include a large image attachment.
Joe Malin8d40d042012-11-05 11:36:40 -08003891 *
Robert Ly91c5ce32014-06-08 15:37:00 -07003892 * Here's how you'd set the <code>BigPictureStyle</code> on a notification:
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003893 * <pre class="prettyprint">
Robert Ly91c5ce32014-06-08 15:37:00 -07003894 * Notification notif = new Notification.Builder(mContext)
3895 * .setContentTitle(&quot;New photo from &quot; + sender.toString())
3896 * .setContentText(subject)
3897 * .setSmallIcon(R.drawable.new_post)
3898 * .setLargeIcon(aBitmap)
3899 * .setStyle(new Notification.BigPictureStyle()
3900 * .bigPicture(aBigBitmap))
3901 * .build();
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003902 * </pre>
Joe Malin8d40d042012-11-05 11:36:40 -08003903 *
Daniel Sandler4dfbe832012-04-11 14:51:46 -04003904 * @see Notification#bigContentView
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003905 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003906 public static class BigPictureStyle extends Style {
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003907 private Bitmap mPicture;
Dan Sandlerd63f9322015-05-06 15:18:49 -04003908 private Icon mBigLargeIcon;
Chris Wren3745a3d2012-05-22 15:11:52 -04003909 private boolean mBigLargeIconSet = false;
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003910
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003911 public BigPictureStyle() {
3912 }
3913
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003914 public BigPictureStyle(Builder builder) {
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003915 setBuilder(builder);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003916 }
3917
Chris Wrend6297db2012-05-03 16:20:13 -04003918 /**
3919 * Overrides ContentTitle in the big form of the template.
3920 * This defaults to the value passed to setContentTitle().
3921 */
3922 public BigPictureStyle setBigContentTitle(CharSequence title) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04003923 internalSetBigContentTitle(safeCharSequence(title));
Chris Wrend6297db2012-05-03 16:20:13 -04003924 return this;
3925 }
3926
3927 /**
3928 * Set the first line of text after the detail section in the big form of the template.
3929 */
3930 public BigPictureStyle setSummaryText(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04003931 internalSetSummaryText(safeCharSequence(cs));
Chris Wrend6297db2012-05-03 16:20:13 -04003932 return this;
3933 }
3934
Chris Wren0bd664d2012-08-01 13:56:56 -04003935 /**
3936 * Provide the bitmap to be used as the payload for the BigPicture notification.
3937 */
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003938 public BigPictureStyle bigPicture(Bitmap b) {
3939 mPicture = b;
3940 return this;
3941 }
3942
Chris Wren3745a3d2012-05-22 15:11:52 -04003943 /**
Chris Wren3745a3d2012-05-22 15:11:52 -04003944 * Override the large icon when the big notification is shown.
3945 */
3946 public BigPictureStyle bigLargeIcon(Bitmap b) {
Dan Sandlerd63f9322015-05-06 15:18:49 -04003947 return bigLargeIcon(b != null ? Icon.createWithBitmap(b) : null);
3948 }
3949
3950 /**
3951 * Override the large icon when the big notification is shown.
3952 */
3953 public BigPictureStyle bigLargeIcon(Icon icon) {
Chris Wren3745a3d2012-05-22 15:11:52 -04003954 mBigLargeIconSet = true;
Dan Sandlerd63f9322015-05-06 15:18:49 -04003955 mBigLargeIcon = icon;
Chris Wren3745a3d2012-05-22 15:11:52 -04003956 return this;
3957 }
3958
Riley Andrews0394a0c2015-11-03 23:36:52 -08003959 /** @hide */
3960 public static final int MIN_ASHMEM_BITMAP_SIZE = 128 * (1 << 10);
3961
Daniel Sandler0ec46202015-06-24 01:27:05 -04003962 /**
3963 * @hide
3964 */
Jorim Jaggia0d58ae2015-06-03 11:48:13 -07003965 @Override
3966 public void purgeResources() {
3967 super.purgeResources();
Riley Andrews8cee7c12015-11-01 23:36:04 -08003968 if (mPicture != null &&
3969 mPicture.isMutable() &&
Riley Andrews0394a0c2015-11-03 23:36:52 -08003970 mPicture.getAllocationByteCount() >= MIN_ASHMEM_BITMAP_SIZE) {
Jorim Jaggia0d58ae2015-06-03 11:48:13 -07003971 mPicture = mPicture.createAshmemBitmap();
3972 }
3973 if (mBigLargeIcon != null) {
3974 mBigLargeIcon.convertToAshmem();
3975 }
3976 }
Christoph Studer5c510ee2014-12-15 16:32:27 +01003977
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003978 /**
3979 * @hide
3980 */
3981 public RemoteViews makeBigContentView() {
3982 // Replace mN.mLargeIcon with mBigLargeIcon if mBigLargeIconSet
Christoph Studer5c510ee2014-12-15 16:32:27 +01003983 // This covers the following cases:
3984 // 1. mBigLargeIconSet -> mBigLargeIcon (null or non-null) applies, overrides
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003985 // mN.mLargeIcon
3986 // 2. !mBigLargeIconSet -> mN.mLargeIcon applies
Dan Sandlerd63f9322015-05-06 15:18:49 -04003987 Icon oldLargeIcon = null;
Christoph Studer5c510ee2014-12-15 16:32:27 +01003988 if (mBigLargeIconSet) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003989 oldLargeIcon = mBuilder.mN.mLargeIcon;
3990 mBuilder.mN.mLargeIcon = mBigLargeIcon;
Christoph Studer5c510ee2014-12-15 16:32:27 +01003991 }
3992
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01003993 RemoteViews contentView = getStandardView(mBuilder.getBigPictureLayoutResource());
Selim Cinek03d0d652015-11-13 13:18:09 -05003994 if (mSummaryTextSet) {
3995 contentView.setTextViewText(R.id.text, mBuilder.processLegacyText(mSummaryText));
Selim Cinekc848c3a2016-01-13 15:27:30 -08003996 contentView.setViewVisibility(R.id.text, View.VISIBLE);
Selim Cinek03d0d652015-11-13 13:18:09 -05003997 }
Selim Cinek860b6da2015-12-16 19:02:19 -08003998 mBuilder.setContentMinHeight(contentView, mBuilder.mN.mLargeIcon != null);
Selim Cinek53e64a42015-11-16 10:40:56 -08003999
Christoph Studer5c510ee2014-12-15 16:32:27 +01004000 if (mBigLargeIconSet) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004001 mBuilder.mN.mLargeIcon = oldLargeIcon;
Christoph Studer5c510ee2014-12-15 16:32:27 +01004002 }
4003
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004004 contentView.setImageViewBitmap(R.id.big_picture, mPicture);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004005 return contentView;
4006 }
4007
Daniel Sandlerf45564e2013-04-15 15:05:08 -04004008 /**
4009 * @hide
4010 */
4011 public void addExtras(Bundle extras) {
4012 super.addExtras(extras);
4013
4014 if (mBigLargeIconSet) {
4015 extras.putParcelable(EXTRA_LARGE_ICON_BIG, mBigLargeIcon);
4016 }
4017 extras.putParcelable(EXTRA_PICTURE, mPicture);
4018 }
4019
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04004020 /**
4021 * @hide
4022 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004023 @Override
Christoph Studer4600f9b2014-07-22 22:44:43 +02004024 protected void restoreFromExtras(Bundle extras) {
4025 super.restoreFromExtras(extras);
4026
4027 if (extras.containsKey(EXTRA_LARGE_ICON_BIG)) {
Christoph Studer5c510ee2014-12-15 16:32:27 +01004028 mBigLargeIconSet = true;
Christoph Studer4600f9b2014-07-22 22:44:43 +02004029 mBigLargeIcon = extras.getParcelable(EXTRA_LARGE_ICON_BIG);
Chris Wren3745a3d2012-05-22 15:11:52 -04004030 }
Christoph Studer4600f9b2014-07-22 22:44:43 +02004031 mPicture = extras.getParcelable(EXTRA_PICTURE);
4032 }
Selim Cinek03d0d652015-11-13 13:18:09 -05004033
4034 /**
4035 * @hide
4036 */
4037 @Override
4038 public boolean hasSummaryInHeader() {
4039 return false;
4040 }
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004041 }
4042
4043 /**
Daniel Sandler4dfbe832012-04-11 14:51:46 -04004044 * Helper class for generating large-format notifications that include a lot of text.
Joe Malin8d40d042012-11-05 11:36:40 -08004045 *
Robert Ly91c5ce32014-06-08 15:37:00 -07004046 * Here's how you'd set the <code>BigTextStyle</code> on a notification:
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004047 * <pre class="prettyprint">
Robert Ly91c5ce32014-06-08 15:37:00 -07004048 * Notification notif = new Notification.Builder(mContext)
4049 * .setContentTitle(&quot;New mail from &quot; + sender.toString())
4050 * .setContentText(subject)
4051 * .setSmallIcon(R.drawable.new_mail)
4052 * .setLargeIcon(aBitmap)
4053 * .setStyle(new Notification.BigTextStyle()
4054 * .bigText(aVeryLongString))
4055 * .build();
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004056 * </pre>
Joe Malin8d40d042012-11-05 11:36:40 -08004057 *
Daniel Sandler4dfbe832012-04-11 14:51:46 -04004058 * @see Notification#bigContentView
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004059 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004060 public static class BigTextStyle extends Style {
Jorim Jaggi457a10d2014-09-08 16:18:23 +02004061
4062 private static final int MAX_LINES = 13;
Selim Cinek3a2c4b92015-12-17 17:01:17 -08004063 private static final int LINES_CONSUMED_BY_ACTIONS = 4;
Jorim Jaggi457a10d2014-09-08 16:18:23 +02004064
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004065 private CharSequence mBigText;
4066
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004067 public BigTextStyle() {
4068 }
4069
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004070 public BigTextStyle(Builder builder) {
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004071 setBuilder(builder);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004072 }
4073
Chris Wrend6297db2012-05-03 16:20:13 -04004074 /**
4075 * Overrides ContentTitle in the big form of the template.
4076 * This defaults to the value passed to setContentTitle().
4077 */
4078 public BigTextStyle setBigContentTitle(CharSequence title) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04004079 internalSetBigContentTitle(safeCharSequence(title));
Chris Wrend6297db2012-05-03 16:20:13 -04004080 return this;
4081 }
4082
4083 /**
4084 * Set the first line of text after the detail section in the big form of the template.
4085 */
4086 public BigTextStyle setSummaryText(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04004087 internalSetSummaryText(safeCharSequence(cs));
Chris Wrend6297db2012-05-03 16:20:13 -04004088 return this;
4089 }
4090
Chris Wren0bd664d2012-08-01 13:56:56 -04004091 /**
4092 * Provide the longer text to be displayed in the big form of the
4093 * template in place of the content text.
4094 */
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004095 public BigTextStyle bigText(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04004096 mBigText = safeCharSequence(cs);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004097 return this;
4098 }
4099
Daniel Sandlerf45564e2013-04-15 15:05:08 -04004100 /**
4101 * @hide
4102 */
4103 public void addExtras(Bundle extras) {
4104 super.addExtras(extras);
4105
Christoph Studer4600f9b2014-07-22 22:44:43 +02004106 extras.putCharSequence(EXTRA_BIG_TEXT, mBigText);
4107 }
4108
4109 /**
4110 * @hide
4111 */
4112 @Override
4113 protected void restoreFromExtras(Bundle extras) {
4114 super.restoreFromExtras(extras);
4115
4116 mBigText = extras.getCharSequence(EXTRA_BIG_TEXT);
Daniel Sandlerf45564e2013-04-15 15:05:08 -04004117 }
4118
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004119 /**
4120 * @hide
4121 */
4122 public RemoteViews makeBigContentView() {
Christoph Studer4600f9b2014-07-22 22:44:43 +02004123
4124 // Nasty
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004125 CharSequence oldBuilderContentText =
4126 mBuilder.getAllExtras().getCharSequence(EXTRA_TEXT);
4127 mBuilder.getAllExtras().putCharSequence(EXTRA_TEXT, null);
Daniel Sandler916ad912012-06-13 12:17:07 -04004128
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01004129 RemoteViews contentView = getStandardView(mBuilder.getBigTextLayoutResource());
Joe Malin8d40d042012-11-05 11:36:40 -08004130
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004131 mBuilder.getAllExtras().putCharSequence(EXTRA_TEXT, oldBuilderContentText);
Christoph Studer4600f9b2014-07-22 22:44:43 +02004132
Selim Cinek3a2c4b92015-12-17 17:01:17 -08004133 CharSequence bigTextText = mBuilder.processLegacyText(mBigText);
4134 contentView.setTextViewText(R.id.big_text, bigTextText);
4135 contentView.setViewVisibility(R.id.big_text,
4136 TextUtils.isEmpty(bigTextText) ? View.GONE : View.VISIBLE);
Jorim Jaggi457a10d2014-09-08 16:18:23 +02004137 contentView.setInt(R.id.big_text, "setMaxLines", calculateMaxLines());
Selim Cinek4fb12d32015-11-19 18:10:48 -08004138 contentView.setBoolean(R.id.big_text, "setHasImage", mBuilder.mN.mLargeIcon != null);
4139
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004140 return contentView;
4141 }
4142
Jorim Jaggi457a10d2014-09-08 16:18:23 +02004143 private int calculateMaxLines() {
4144 int lineCount = MAX_LINES;
4145 boolean hasActions = mBuilder.mActions.size() > 0;
Jorim Jaggi457a10d2014-09-08 16:18:23 +02004146 if (hasActions) {
4147 lineCount -= LINES_CONSUMED_BY_ACTIONS;
4148 }
Jorim Jaggi457a10d2014-09-08 16:18:23 +02004149 return lineCount;
4150 }
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004151 }
Daniel Sandler879c5e02012-04-17 16:46:51 -04004152
4153 /**
4154 * Helper class for generating large-format notifications that include a list of (up to 5) strings.
Joe Malin8d40d042012-11-05 11:36:40 -08004155 *
Robert Ly91c5ce32014-06-08 15:37:00 -07004156 * Here's how you'd set the <code>InboxStyle</code> on a notification:
Daniel Sandler879c5e02012-04-17 16:46:51 -04004157 * <pre class="prettyprint">
Robert Ly91c5ce32014-06-08 15:37:00 -07004158 * Notification notif = new Notification.Builder(mContext)
4159 * .setContentTitle(&quot;5 New mails from &quot; + sender.toString())
4160 * .setContentText(subject)
4161 * .setSmallIcon(R.drawable.new_mail)
4162 * .setLargeIcon(aBitmap)
4163 * .setStyle(new Notification.InboxStyle()
4164 * .addLine(str1)
4165 * .addLine(str2)
4166 * .setContentTitle(&quot;&quot;)
4167 * .setSummaryText(&quot;+3 more&quot;))
4168 * .build();
Daniel Sandler879c5e02012-04-17 16:46:51 -04004169 * </pre>
Joe Malin8d40d042012-11-05 11:36:40 -08004170 *
Daniel Sandler879c5e02012-04-17 16:46:51 -04004171 * @see Notification#bigContentView
4172 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004173 public static class InboxStyle extends Style {
Daniel Sandler879c5e02012-04-17 16:46:51 -04004174 private ArrayList<CharSequence> mTexts = new ArrayList<CharSequence>(5);
4175
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004176 public InboxStyle() {
4177 }
4178
Daniel Sandler879c5e02012-04-17 16:46:51 -04004179 public InboxStyle(Builder builder) {
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004180 setBuilder(builder);
Daniel Sandler879c5e02012-04-17 16:46:51 -04004181 }
4182
Chris Wrend6297db2012-05-03 16:20:13 -04004183 /**
4184 * Overrides ContentTitle in the big form of the template.
4185 * This defaults to the value passed to setContentTitle().
4186 */
4187 public InboxStyle setBigContentTitle(CharSequence title) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04004188 internalSetBigContentTitle(safeCharSequence(title));
Chris Wrend6297db2012-05-03 16:20:13 -04004189 return this;
4190 }
4191
4192 /**
4193 * Set the first line of text after the detail section in the big form of the template.
4194 */
4195 public InboxStyle setSummaryText(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04004196 internalSetSummaryText(safeCharSequence(cs));
Chris Wrend6297db2012-05-03 16:20:13 -04004197 return this;
4198 }
4199
Chris Wren0bd664d2012-08-01 13:56:56 -04004200 /**
4201 * Append a line to the digest section of the Inbox notification.
4202 */
Daniel Sandler879c5e02012-04-17 16:46:51 -04004203 public InboxStyle addLine(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04004204 mTexts.add(safeCharSequence(cs));
Daniel Sandler879c5e02012-04-17 16:46:51 -04004205 return this;
4206 }
4207
Daniel Sandlerf45564e2013-04-15 15:05:08 -04004208 /**
4209 * @hide
4210 */
4211 public void addExtras(Bundle extras) {
4212 super.addExtras(extras);
Christoph Studer4600f9b2014-07-22 22:44:43 +02004213
Daniel Sandlerf45564e2013-04-15 15:05:08 -04004214 CharSequence[] a = new CharSequence[mTexts.size()];
4215 extras.putCharSequenceArray(EXTRA_TEXT_LINES, mTexts.toArray(a));
4216 }
4217
Christoph Studer4600f9b2014-07-22 22:44:43 +02004218 /**
4219 * @hide
4220 */
4221 @Override
4222 protected void restoreFromExtras(Bundle extras) {
4223 super.restoreFromExtras(extras);
4224
4225 mTexts.clear();
4226 if (extras.containsKey(EXTRA_TEXT_LINES)) {
4227 Collections.addAll(mTexts, extras.getCharSequenceArray(EXTRA_TEXT_LINES));
4228 }
4229 }
4230
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004231 /**
4232 * @hide
4233 */
4234 public RemoteViews makeBigContentView() {
Selim Cinekc848c3a2016-01-13 15:27:30 -08004235 // Remove the content text so it disappears unless you have a summary
Christoph Studer4600f9b2014-07-22 22:44:43 +02004236 // Nasty
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004237 CharSequence oldBuilderContentText = mBuilder.mN.extras.getCharSequence(EXTRA_TEXT);
4238 mBuilder.getAllExtras().putCharSequence(EXTRA_TEXT, null);
Christoph Studer4600f9b2014-07-22 22:44:43 +02004239
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01004240 RemoteViews contentView = getStandardView(mBuilder.getInboxLayoutResource());
Daniel Sandler619738c2012-06-07 16:33:08 -04004241
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004242 mBuilder.getAllExtras().putCharSequence(EXTRA_TEXT, oldBuilderContentText);
Christoph Studer4600f9b2014-07-22 22:44:43 +02004243
Chris Wrend6297db2012-05-03 16:20:13 -04004244 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 -04004245 R.id.inbox_text4, R.id.inbox_text5, R.id.inbox_text6};
Chris Wrend6297db2012-05-03 16:20:13 -04004246
Chris Wren4ed80d52012-05-17 09:30:03 -04004247 // Make sure all rows are gone in case we reuse a view.
4248 for (int rowId : rowIds) {
4249 contentView.setViewVisibility(rowId, View.GONE);
4250 }
4251
Jorim Jaggi445d3c02014-08-19 22:33:42 +02004252 final boolean largeText =
4253 mBuilder.mContext.getResources().getConfiguration().fontScale > 1f;
4254 final float subTextSize = mBuilder.mContext.getResources().getDimensionPixelSize(
4255 R.dimen.notification_subtext_size);
Daniel Sandler879c5e02012-04-17 16:46:51 -04004256 int i=0;
Selim Cinek9d9fc6e2015-11-12 15:49:14 -05004257 final float density = mBuilder.mContext.getResources().getDisplayMetrics().density;
4258 int topPadding = (int) (5 * density);
Selim Cinekc0fac722016-03-07 19:57:06 -08004259 int bottomPadding = mBuilder.mContext.getResources().getDimensionPixelSize(
4260 com.android.internal.R.dimen.notification_content_margin_bottom);
Selim Cinek247fa012016-02-18 09:50:48 -08004261 boolean first = true;
Daniel Sandler879c5e02012-04-17 16:46:51 -04004262 while (i < mTexts.size() && i < rowIds.length) {
4263 CharSequence str = mTexts.get(i);
4264 if (str != null && !str.equals("")) {
4265 contentView.setViewVisibility(rowIds[i], View.VISIBLE);
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01004266 contentView.setTextViewText(rowIds[i], mBuilder.processLegacyText(str));
Jorim Jaggi445d3c02014-08-19 22:33:42 +02004267 if (largeText) {
4268 contentView.setTextViewTextSize(rowIds[i], TypedValue.COMPLEX_UNIT_PX,
4269 subTextSize);
4270 }
Selim Cinek9d9fc6e2015-11-12 15:49:14 -05004271 contentView.setViewPadding(rowIds[i], 0, topPadding, 0,
4272 i == rowIds.length - 1 || i == mTexts.size() - 1 ? bottomPadding : 0);
Selim Cinek247fa012016-02-18 09:50:48 -08004273 handleInboxImageMargin(contentView, rowIds[i], first);
4274 first = false;
Daniel Sandler879c5e02012-04-17 16:46:51 -04004275 }
4276 i++;
4277 }
Kenny Guy98193ea2014-07-24 19:54:37 +01004278
Selim Cinek1e0bf612015-11-20 15:57:26 -08004279
Daniel Sandler879c5e02012-04-17 16:46:51 -04004280 return contentView;
4281 }
Selim Cinek1e0bf612015-11-20 15:57:26 -08004282
Selim Cinek247fa012016-02-18 09:50:48 -08004283 private void handleInboxImageMargin(RemoteViews contentView, int id, boolean first) {
Selim Cinek1e0bf612015-11-20 15:57:26 -08004284 int endMargin = 0;
Selim Cinek247fa012016-02-18 09:50:48 -08004285 if (first) {
4286 final int max = mBuilder.mN.extras.getInt(EXTRA_PROGRESS_MAX, 0);
4287 final boolean ind = mBuilder.mN.extras.getBoolean(EXTRA_PROGRESS_INDETERMINATE);
4288 boolean hasProgress = max != 0 || ind;
4289 if (mBuilder.mN.mLargeIcon != null && !hasProgress) {
4290 endMargin = mBuilder.mContext.getResources().getDimensionPixelSize(
4291 R.dimen.notification_content_picture_margin);
4292 }
Selim Cinek1e0bf612015-11-20 15:57:26 -08004293 }
4294 contentView.setViewLayoutMarginEnd(id, endMargin);
4295 }
Daniel Sandler879c5e02012-04-17 16:46:51 -04004296 }
Dan Sandler842dd772014-05-15 09:36:47 -04004297
4298 /**
4299 * Notification style for media playback notifications.
4300 *
4301 * In the expanded form, {@link Notification#bigContentView}, up to 5
4302 * {@link Notification.Action}s specified with
Dan Sandler86647982015-05-13 23:41:13 -04004303 * {@link Notification.Builder#addAction(Action) addAction} will be
Dan Sandler842dd772014-05-15 09:36:47 -04004304 * shown as icon-only pushbuttons, suitable for transport controls. The Bitmap given to
4305 * {@link Notification.Builder#setLargeIcon(android.graphics.Bitmap) setLargeIcon()} will be
4306 * treated as album artwork.
4307 *
4308 * Unlike the other styles provided here, MediaStyle can also modify the standard-size
4309 * {@link Notification#contentView}; by providing action indices to
Christoph Studerfde6f4d2014-12-12 13:23:26 +01004310 * {@link #setShowActionsInCompactView(int...)} you can promote up to 3 actions to be displayed
Dan Sandler842dd772014-05-15 09:36:47 -04004311 * in the standard view alongside the usual content.
4312 *
Bryan Mawhinney6be8de32014-07-18 10:35:12 +01004313 * Notifications created with MediaStyle will have their category set to
4314 * {@link Notification#CATEGORY_TRANSPORT CATEGORY_TRANSPORT} unless you set a different
4315 * category using {@link Notification.Builder#setCategory(String) setCategory()}.
4316 *
Jeff Browndba34ba2014-06-24 20:46:03 -07004317 * Finally, if you attach a {@link android.media.session.MediaSession.Token} using
4318 * {@link android.app.Notification.MediaStyle#setMediaSession(MediaSession.Token)},
Dan Sandler842dd772014-05-15 09:36:47 -04004319 * the System UI can identify this as a notification representing an active media session
4320 * and respond accordingly (by showing album artwork in the lockscreen, for example).
4321 *
4322 * To use this style with your Notification, feed it to
4323 * {@link Notification.Builder#setStyle(android.app.Notification.Style)} like so:
4324 * <pre class="prettyprint">
4325 * Notification noti = new Notification.Builder()
4326 * .setSmallIcon(R.drawable.ic_stat_player)
Christoph Studere935fe92014-11-24 14:18:06 +01004327 * .setContentTitle(&quot;Track title&quot;)
4328 * .setContentText(&quot;Artist - Album&quot;)
4329 * .setLargeIcon(albumArtBitmap))
Bryan Mawhinney6be8de32014-07-18 10:35:12 +01004330 * .setStyle(<b>new Notification.MediaStyle()</b>
4331 * .setMediaSession(mySession))
Dan Sandler842dd772014-05-15 09:36:47 -04004332 * .build();
4333 * </pre>
4334 *
4335 * @see Notification#bigContentView
4336 */
4337 public static class MediaStyle extends Style {
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02004338 static final int MAX_MEDIA_BUTTONS_IN_COMPACT = 3;
Dan Sandler842dd772014-05-15 09:36:47 -04004339 static final int MAX_MEDIA_BUTTONS = 5;
4340
4341 private int[] mActionsToShowInCompact = null;
Jeff Browndba34ba2014-06-24 20:46:03 -07004342 private MediaSession.Token mToken;
Dan Sandler842dd772014-05-15 09:36:47 -04004343
4344 public MediaStyle() {
4345 }
4346
4347 public MediaStyle(Builder builder) {
4348 setBuilder(builder);
4349 }
4350
4351 /**
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02004352 * 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 -04004353 * notification view.
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02004354 *
4355 * @param actions the indices of the actions to show in the compact notification view
Dan Sandler842dd772014-05-15 09:36:47 -04004356 */
4357 public MediaStyle setShowActionsInCompactView(int...actions) {
4358 mActionsToShowInCompact = actions;
4359 return this;
4360 }
4361
4362 /**
Jeff Browndba34ba2014-06-24 20:46:03 -07004363 * Attach a {@link android.media.session.MediaSession.Token} to this Notification
4364 * to provide additional playback information and control to the SystemUI.
Dan Sandler842dd772014-05-15 09:36:47 -04004365 */
Jeff Browndba34ba2014-06-24 20:46:03 -07004366 public MediaStyle setMediaSession(MediaSession.Token token) {
Dan Sandler842dd772014-05-15 09:36:47 -04004367 mToken = token;
4368 return this;
4369 }
4370
Christoph Studer4600f9b2014-07-22 22:44:43 +02004371 /**
4372 * @hide
4373 */
Dan Sandler842dd772014-05-15 09:36:47 -04004374 @Override
4375 public Notification buildStyled(Notification wip) {
Christoph Studer4600f9b2014-07-22 22:44:43 +02004376 super.buildStyled(wip);
Bryan Mawhinney6be8de32014-07-18 10:35:12 +01004377 if (wip.category == null) {
4378 wip.category = Notification.CATEGORY_TRANSPORT;
4379 }
Dan Sandler842dd772014-05-15 09:36:47 -04004380 return wip;
4381 }
4382
Christoph Studer4600f9b2014-07-22 22:44:43 +02004383 /**
4384 * @hide
4385 */
4386 @Override
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004387 public RemoteViews makeContentView() {
4388 return makeMediaContentView();
Christoph Studer4600f9b2014-07-22 22:44:43 +02004389 }
4390
4391 /**
4392 * @hide
4393 */
4394 @Override
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004395 public RemoteViews makeBigContentView() {
4396 return makeMediaBigContentView();
Christoph Studer4600f9b2014-07-22 22:44:43 +02004397 }
4398
Selim Cinekcc10bfb2016-02-10 16:24:21 -08004399 /**
4400 * @hide
4401 */
4402 @Override
4403 public RemoteViews makeHeadsUpContentView() {
4404 RemoteViews expanded = makeMediaBigContentView();
4405 return expanded != null ? expanded : makeMediaContentView();
4406 }
4407
Dan Sandler842dd772014-05-15 09:36:47 -04004408 /** @hide */
4409 @Override
4410 public void addExtras(Bundle extras) {
4411 super.addExtras(extras);
4412
4413 if (mToken != null) {
4414 extras.putParcelable(EXTRA_MEDIA_SESSION, mToken);
4415 }
Bryan Mawhinneye191f902014-07-22 12:50:09 +01004416 if (mActionsToShowInCompact != null) {
4417 extras.putIntArray(EXTRA_COMPACT_ACTIONS, mActionsToShowInCompact);
4418 }
Dan Sandler842dd772014-05-15 09:36:47 -04004419 }
4420
Christoph Studer4600f9b2014-07-22 22:44:43 +02004421 /**
4422 * @hide
4423 */
4424 @Override
4425 protected void restoreFromExtras(Bundle extras) {
4426 super.restoreFromExtras(extras);
4427
4428 if (extras.containsKey(EXTRA_MEDIA_SESSION)) {
4429 mToken = extras.getParcelable(EXTRA_MEDIA_SESSION);
4430 }
4431 if (extras.containsKey(EXTRA_COMPACT_ACTIONS)) {
4432 mActionsToShowInCompact = extras.getIntArray(EXTRA_COMPACT_ACTIONS);
4433 }
4434 }
4435
Selim Cinek5bf069a2015-11-10 19:14:27 -05004436 private RemoteViews generateMediaActionButton(Action action, int color) {
Dan Sandler842dd772014-05-15 09:36:47 -04004437 final boolean tombstone = (action.actionIntent == null);
Selim Cinekf33b1112015-07-15 17:45:11 -07004438 RemoteViews button = new BuilderRemoteViews(mBuilder.mContext.getApplicationInfo(),
Alan Viverette3cb07a462014-06-06 14:19:53 -07004439 R.layout.notification_material_media_action);
Dan Sandler68079d52015-07-22 10:45:30 -04004440 button.setImageViewIcon(R.id.action0, action.getIcon());
Selim Cinek5bf069a2015-11-10 19:14:27 -05004441 button.setDrawableParameters(R.id.action0, false, -1, color, PorterDuff.Mode.SRC_ATOP,
4442 -1);
Dan Sandler842dd772014-05-15 09:36:47 -04004443 if (!tombstone) {
4444 button.setOnClickPendingIntent(R.id.action0, action.actionIntent);
4445 }
4446 button.setContentDescription(R.id.action0, action.title);
4447 return button;
4448 }
4449
4450 private RemoteViews makeMediaContentView() {
4451 RemoteViews view = mBuilder.applyStandardTemplate(
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02004452 R.layout.notification_template_material_media, false /* hasProgress */);
Dan Sandler842dd772014-05-15 09:36:47 -04004453
4454 final int numActions = mBuilder.mActions.size();
4455 final int N = mActionsToShowInCompact == null
4456 ? 0
4457 : Math.min(mActionsToShowInCompact.length, MAX_MEDIA_BUTTONS_IN_COMPACT);
4458 if (N > 0) {
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02004459 view.removeAllViews(com.android.internal.R.id.media_actions);
Dan Sandler842dd772014-05-15 09:36:47 -04004460 for (int i = 0; i < N; i++) {
4461 if (i >= numActions) {
4462 throw new IllegalArgumentException(String.format(
4463 "setShowActionsInCompactView: action %d out of bounds (max %d)",
4464 i, numActions - 1));
4465 }
4466
4467 final Action action = mBuilder.mActions.get(mActionsToShowInCompact[i]);
Selim Cinek5bf069a2015-11-10 19:14:27 -05004468 final RemoteViews button = generateMediaActionButton(action,
Adrian Roos4ff3b122016-02-01 12:26:13 -08004469 mBuilder.resolveContrastColor());
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02004470 view.addView(com.android.internal.R.id.media_actions, button);
Dan Sandler842dd772014-05-15 09:36:47 -04004471 }
4472 }
Selim Cinekfdc738f2016-01-27 20:04:27 -08004473 handleImage(view);
4474 // handle the content margin
Selim Cinek247fa012016-02-18 09:50:48 -08004475 int endMargin = mBuilder.mContext.getResources().getDimensionPixelSize(
4476 R.dimen.notification_content_margin_end);;
Selim Cinekfdc738f2016-01-27 20:04:27 -08004477 if (mBuilder.mN.mLargeIcon != null) {
Selim Cinek247fa012016-02-18 09:50:48 -08004478 endMargin += mBuilder.mContext.getResources().getDimensionPixelSize(
4479 R.dimen.notification_content_picture_margin);
Selim Cinekfdc738f2016-01-27 20:04:27 -08004480 }
4481 view.setViewLayoutMarginEnd(R.id.notification_main_column, endMargin);
Dan Sandler842dd772014-05-15 09:36:47 -04004482 return view;
4483 }
4484
4485 private RemoteViews makeMediaBigContentView() {
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02004486 final int actionCount = Math.min(mBuilder.mActions.size(), MAX_MEDIA_BUTTONS);
Selim Cinekcc10bfb2016-02-10 16:24:21 -08004487 // Dont add an expanded view if there is no more content to be revealed
4488 int actionsInCompact = mActionsToShowInCompact == null
4489 ? 0
4490 : Math.min(mActionsToShowInCompact.length, MAX_MEDIA_BUTTONS_IN_COMPACT);
4491 if (mBuilder.mN.mLargeIcon == null && actionCount <= actionsInCompact) {
4492 return null;
4493 }
Selim Cinek5bf069a2015-11-10 19:14:27 -05004494 RemoteViews big = mBuilder.applyStandardTemplate(
4495 R.layout.notification_template_material_big_media,
4496 false);
Dan Sandler842dd772014-05-15 09:36:47 -04004497
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02004498 if (actionCount > 0) {
4499 big.removeAllViews(com.android.internal.R.id.media_actions);
4500 for (int i = 0; i < actionCount; i++) {
Selim Cinek5bf069a2015-11-10 19:14:27 -05004501 final RemoteViews button = generateMediaActionButton(mBuilder.mActions.get(i),
Adrian Roos4ff3b122016-02-01 12:26:13 -08004502 mBuilder.resolveContrastColor());
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02004503 big.addView(com.android.internal.R.id.media_actions, button);
Dan Sandler842dd772014-05-15 09:36:47 -04004504 }
4505 }
Selim Cinek5bf069a2015-11-10 19:14:27 -05004506 handleImage(big);
Dan Sandler842dd772014-05-15 09:36:47 -04004507 return big;
4508 }
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02004509
Selim Cinek5bf069a2015-11-10 19:14:27 -05004510 private void handleImage(RemoteViews contentView) {
4511 if (mBuilder.mN.mLargeIcon != null) {
4512 contentView.setViewLayoutMarginEnd(R.id.line1, 0);
Selim Cinekc848c3a2016-01-13 15:27:30 -08004513 contentView.setViewLayoutMarginEnd(R.id.text, 0);
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02004514 }
4515 }
4516
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02004517 /**
4518 * @hide
4519 */
4520 @Override
4521 protected boolean hasProgress() {
4522 return false;
4523 }
Dan Sandler842dd772014-05-15 09:36:47 -04004524 }
Griff Hazen61a9e862014-05-22 16:05:19 -07004525
Selim Cinek593610c2016-02-16 18:42:57 -08004526 /**
4527 * Notification style for custom views that are decorated by the system
4528 *
4529 * <p>Instead of providing a notification that is completely custom, a developer can set this
4530 * style and still obtain system decorations like the notification header with the expand
4531 * affordance and actions.
4532 *
4533 * <p>Use {@link android.app.Notification.Builder#setCustomContentView(RemoteViews)},
4534 * {@link android.app.Notification.Builder#setCustomBigContentView(RemoteViews)} and
4535 * {@link android.app.Notification.Builder#setCustomHeadsUpContentView(RemoteViews)} to set the
4536 * corresponding custom views to display.
4537 *
4538 * To use this style with your Notification, feed it to
4539 * {@link Notification.Builder#setStyle(android.app.Notification.Style)} like so:
4540 * <pre class="prettyprint">
4541 * Notification noti = new Notification.Builder()
4542 * .setSmallIcon(R.drawable.ic_stat_player)
4543 * .setLargeIcon(albumArtBitmap))
4544 * .setCustomContentView(contentView);
4545 * .setStyle(<b>new Notification.DecoratedCustomViewStyle()</b>)
4546 * .build();
4547 * </pre>
4548 */
4549 public static class DecoratedCustomViewStyle extends Style {
4550
4551 public DecoratedCustomViewStyle() {
4552 }
4553
4554 public DecoratedCustomViewStyle(Builder builder) {
4555 setBuilder(builder);
4556 }
4557
4558 /**
4559 * @hide
4560 */
4561 public boolean displayCustomViewInline() {
4562 return true;
4563 }
4564
4565 /**
4566 * @hide
4567 */
4568 @Override
4569 public RemoteViews makeContentView() {
4570 return makeStandardTemplateWithCustomContent(mBuilder.mN.contentView);
4571 }
4572
4573 /**
4574 * @hide
4575 */
4576 @Override
4577 public RemoteViews makeBigContentView() {
4578 return makeDecoratedBigContentView();
4579 }
4580
4581 /**
4582 * @hide
4583 */
4584 @Override
4585 public RemoteViews makeHeadsUpContentView() {
4586 return makeDecoratedHeadsUpContentView();
4587 }
4588
Selim Cinek593610c2016-02-16 18:42:57 -08004589 private RemoteViews makeDecoratedHeadsUpContentView() {
4590 RemoteViews headsUpContentView = mBuilder.mN.headsUpContentView == null
4591 ? mBuilder.mN.contentView
4592 : mBuilder.mN.headsUpContentView;
4593 if (mBuilder.mActions.size() == 0) {
4594 return makeStandardTemplateWithCustomContent(headsUpContentView);
4595 }
4596 RemoteViews remoteViews = mBuilder.applyStandardTemplateWithActions(
4597 mBuilder.getBigBaseLayoutResource());
Selim Cinek247fa012016-02-18 09:50:48 -08004598 buildIntoRemoteViewContent(remoteViews, headsUpContentView);
Selim Cinek593610c2016-02-16 18:42:57 -08004599 return remoteViews;
4600 }
4601
Selim Cinek593610c2016-02-16 18:42:57 -08004602 private RemoteViews makeStandardTemplateWithCustomContent(RemoteViews customContent) {
4603 RemoteViews remoteViews = mBuilder.applyStandardTemplate(
4604 mBuilder.getBaseLayoutResource());
Selim Cinek247fa012016-02-18 09:50:48 -08004605 buildIntoRemoteViewContent(remoteViews, customContent);
Selim Cinek593610c2016-02-16 18:42:57 -08004606 return remoteViews;
4607 }
4608
Selim Cinek593610c2016-02-16 18:42:57 -08004609 private RemoteViews makeDecoratedBigContentView() {
4610 RemoteViews bigContentView = mBuilder.mN.bigContentView == null
4611 ? mBuilder.mN.contentView
4612 : mBuilder.mN.bigContentView;
4613 if (mBuilder.mActions.size() == 0) {
4614 return makeStandardTemplateWithCustomContent(bigContentView);
4615 }
4616 RemoteViews remoteViews = mBuilder.applyStandardTemplateWithActions(
4617 mBuilder.getBigBaseLayoutResource());
Selim Cinek247fa012016-02-18 09:50:48 -08004618 buildIntoRemoteViewContent(remoteViews, bigContentView);
Selim Cinek593610c2016-02-16 18:42:57 -08004619 return remoteViews;
4620 }
Selim Cinek247fa012016-02-18 09:50:48 -08004621
4622 private void buildIntoRemoteViewContent(RemoteViews remoteViews,
4623 RemoteViews customContent) {
Adrian Roos5081c0d2016-02-26 16:04:19 -08004624 if (customContent != null) {
Selim Cinekf91017e2016-03-14 12:25:09 -07004625 // Need to clone customContent before adding, because otherwise it can no longer be
4626 // parceled independently of remoteViews.
Adrian Roos5081c0d2016-02-26 16:04:19 -08004627 customContent = customContent.clone();
Selim Cinekf91017e2016-03-14 12:25:09 -07004628 remoteViews.removeAllViews(R.id.notification_main_column);
4629 remoteViews.addView(R.id.notification_main_column, customContent);
Adrian Roos5081c0d2016-02-26 16:04:19 -08004630 }
Selim Cinek247fa012016-02-18 09:50:48 -08004631 // also update the end margin if there is an image
4632 int endMargin = mBuilder.mContext.getResources().getDimensionPixelSize(
4633 R.dimen.notification_content_margin_end);
4634 if (mBuilder.mN.mLargeIcon != null) {
4635 endMargin += mBuilder.mContext.getResources().getDimensionPixelSize(
4636 R.dimen.notification_content_picture_margin);
4637 }
4638 remoteViews.setViewLayoutMarginEnd(R.id.notification_main_column, endMargin);
4639 }
Selim Cinek593610c2016-02-16 18:42:57 -08004640 }
4641
Selim Cinek03eb3b72016-02-18 10:39:45 -08004642 /**
4643 * Notification style for media custom views that are decorated by the system
4644 *
4645 * <p>Instead of providing a media notification that is completely custom, a developer can set
4646 * this style and still obtain system decorations like the notification header with the expand
4647 * affordance and actions.
4648 *
4649 * <p>Use {@link android.app.Notification.Builder#setCustomContentView(RemoteViews)},
4650 * {@link android.app.Notification.Builder#setCustomBigContentView(RemoteViews)} and
4651 * {@link android.app.Notification.Builder#setCustomHeadsUpContentView(RemoteViews)} to set the
4652 * corresponding custom views to display.
4653 *
4654 * To use this style with your Notification, feed it to
4655 * {@link Notification.Builder#setStyle(android.app.Notification.Style)} like so:
4656 * <pre class="prettyprint">
4657 * Notification noti = new Notification.Builder()
4658 * .setSmallIcon(R.drawable.ic_stat_player)
4659 * .setLargeIcon(albumArtBitmap))
4660 * .setCustomContentView(contentView);
4661 * .setStyle(<b>new Notification.DecoratedMediaCustomViewStyle()</b>
4662 * .setMediaSession(mySession))
4663 * .build();
4664 * </pre>
4665 *
4666 * @see android.app.Notification.DecoratedCustomViewStyle
4667 * @see android.app.Notification.MediaStyle
4668 */
4669 public static class DecoratedMediaCustomViewStyle extends MediaStyle {
4670
4671 public DecoratedMediaCustomViewStyle() {
4672 }
4673
4674 public DecoratedMediaCustomViewStyle(Builder builder) {
4675 setBuilder(builder);
4676 }
4677
4678 /**
4679 * @hide
4680 */
4681 public boolean displayCustomViewInline() {
4682 return true;
4683 }
4684
4685 /**
4686 * @hide
4687 */
4688 @Override
4689 public RemoteViews makeContentView() {
4690 RemoteViews remoteViews = super.makeContentView();
4691 return buildIntoRemoteView(remoteViews, R.id.notification_content_container,
4692 mBuilder.mN.contentView);
4693 }
4694
4695 /**
4696 * @hide
4697 */
4698 @Override
4699 public RemoteViews makeBigContentView() {
4700 RemoteViews customRemoteView = mBuilder.mN.bigContentView != null
4701 ? mBuilder.mN.bigContentView
4702 : mBuilder.mN.contentView;
4703 return makeBigContentViewWithCustomContent(customRemoteView);
4704 }
4705
4706 private RemoteViews makeBigContentViewWithCustomContent(RemoteViews customRemoteView) {
4707 RemoteViews remoteViews = super.makeBigContentView();
4708 if (remoteViews != null) {
4709 return buildIntoRemoteView(remoteViews, R.id.notification_main_column,
4710 customRemoteView);
4711 } else if (customRemoteView != mBuilder.mN.contentView){
4712 remoteViews = super.makeContentView();
4713 return buildIntoRemoteView(remoteViews, R.id.notification_content_container,
4714 customRemoteView);
4715 } else {
4716 return null;
4717 }
4718 }
4719
4720 /**
4721 * @hide
4722 */
4723 @Override
4724 public RemoteViews makeHeadsUpContentView() {
4725 RemoteViews customRemoteView = mBuilder.mN.headsUpContentView != null
4726 ? mBuilder.mN.headsUpContentView
4727 : mBuilder.mN.contentView;
4728 return makeBigContentViewWithCustomContent(customRemoteView);
4729 }
4730
4731 private RemoteViews buildIntoRemoteView(RemoteViews remoteViews, int id,
4732 RemoteViews customContent) {
Adrian Roos5081c0d2016-02-26 16:04:19 -08004733 if (customContent != null) {
Selim Cinekf91017e2016-03-14 12:25:09 -07004734 // Need to clone customContent before adding, because otherwise it can no longer be
4735 // parceled independently of remoteViews.
Adrian Roos5081c0d2016-02-26 16:04:19 -08004736 customContent = customContent.clone();
Selim Cinekf91017e2016-03-14 12:25:09 -07004737 remoteViews.removeAllViews(id);
4738 remoteViews.addView(id, customContent);
Adrian Roos5081c0d2016-02-26 16:04:19 -08004739 }
Selim Cinek03eb3b72016-02-18 10:39:45 -08004740 return remoteViews;
4741 }
4742 }
4743
Christoph Studer4600f9b2014-07-22 22:44:43 +02004744 // When adding a new Style subclass here, don't forget to update
4745 // Builder.getNotificationStyleClass.
4746
Griff Hazen61a9e862014-05-22 16:05:19 -07004747 /**
4748 * Extender interface for use with {@link Builder#extend}. Extenders may be used to add
4749 * metadata or change options on a notification builder.
4750 */
4751 public interface Extender {
4752 /**
4753 * Apply this extender to a notification builder.
4754 * @param builder the builder to be modified.
4755 * @return the build object for chaining.
4756 */
4757 public Builder extend(Builder builder);
4758 }
4759
4760 /**
4761 * Helper class to add wearable extensions to notifications.
4762 * <p class="note"> See
4763 * <a href="{@docRoot}wear/notifications/creating.html">Creating Notifications
4764 * for Android Wear</a> for more information on how to use this class.
4765 * <p>
4766 * To create a notification with wearable extensions:
4767 * <ol>
4768 * <li>Create a {@link android.app.Notification.Builder}, setting any desired
4769 * properties.
4770 * <li>Create a {@link android.app.Notification.WearableExtender}.
4771 * <li>Set wearable-specific properties using the
4772 * {@code add} and {@code set} methods of {@link android.app.Notification.WearableExtender}.
4773 * <li>Call {@link android.app.Notification.Builder#extend} to apply the extensions to a
4774 * notification.
4775 * <li>Post the notification to the notification system with the
4776 * {@code NotificationManager.notify(...)} methods.
4777 * </ol>
4778 *
4779 * <pre class="prettyprint">
4780 * Notification notif = new Notification.Builder(mContext)
4781 * .setContentTitle(&quot;New mail from &quot; + sender.toString())
4782 * .setContentText(subject)
4783 * .setSmallIcon(R.drawable.new_mail)
4784 * .extend(new Notification.WearableExtender()
4785 * .setContentIcon(R.drawable.new_mail))
4786 * .build();
4787 * NotificationManager notificationManger =
4788 * (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
4789 * notificationManger.notify(0, notif);</pre>
4790 *
4791 * <p>Wearable extensions can be accessed on an existing notification by using the
4792 * {@code WearableExtender(Notification)} constructor,
4793 * and then using the {@code get} methods to access values.
4794 *
4795 * <pre class="prettyprint">
4796 * Notification.WearableExtender wearableExtender = new Notification.WearableExtender(
4797 * notification);
Griff Hazen14f57992014-05-26 09:07:14 -07004798 * List&lt;Notification&gt; pages = wearableExtender.getPages();</pre>
Griff Hazen61a9e862014-05-22 16:05:19 -07004799 */
4800 public static final class WearableExtender implements Extender {
4801 /**
4802 * Sentinel value for an action index that is unset.
4803 */
4804 public static final int UNSET_ACTION_INDEX = -1;
4805
4806 /**
4807 * Size value for use with {@link #setCustomSizePreset} to show this notification with
4808 * default sizing.
4809 * <p>For custom display notifications created using {@link #setDisplayIntent},
Paul Soulosaa4f4bf2015-08-04 11:59:45 -07004810 * the default is {@link #SIZE_MEDIUM}. All other notifications size automatically based
Griff Hazen61a9e862014-05-22 16:05:19 -07004811 * on their content.
4812 */
4813 public static final int SIZE_DEFAULT = 0;
4814
4815 /**
4816 * Size value for use with {@link #setCustomSizePreset} to show this notification
4817 * with an extra small size.
4818 * <p>This value is only applicable for custom display notifications created using
4819 * {@link #setDisplayIntent}.
4820 */
4821 public static final int SIZE_XSMALL = 1;
4822
4823 /**
4824 * Size value for use with {@link #setCustomSizePreset} to show this notification
4825 * with a small size.
4826 * <p>This value is only applicable for custom display notifications created using
4827 * {@link #setDisplayIntent}.
4828 */
4829 public static final int SIZE_SMALL = 2;
4830
4831 /**
4832 * Size value for use with {@link #setCustomSizePreset} to show this notification
4833 * with a medium size.
4834 * <p>This value is only applicable for custom display notifications created using
4835 * {@link #setDisplayIntent}.
4836 */
4837 public static final int SIZE_MEDIUM = 3;
4838
4839 /**
4840 * Size value for use with {@link #setCustomSizePreset} to show this notification
4841 * with a large size.
4842 * <p>This value is only applicable for custom display notifications created using
4843 * {@link #setDisplayIntent}.
4844 */
4845 public static final int SIZE_LARGE = 4;
4846
Griff Hazend5f11f92014-05-27 15:40:09 -07004847 /**
4848 * Size value for use with {@link #setCustomSizePreset} to show this notification
4849 * full screen.
4850 * <p>This value is only applicable for custom display notifications created using
4851 * {@link #setDisplayIntent}.
4852 */
4853 public static final int SIZE_FULL_SCREEN = 5;
4854
Griff Hazen5f2edfc2014-09-29 16:28:44 -07004855 /**
4856 * Sentinel value for use with {@link #setHintScreenTimeout} to keep the screen on for a
4857 * short amount of time when this notification is displayed on the screen. This
4858 * is the default value.
4859 */
4860 public static final int SCREEN_TIMEOUT_SHORT = 0;
4861
4862 /**
4863 * Sentinel value for use with {@link #setHintScreenTimeout} to keep the screen on
4864 * for a longer amount of time when this notification is displayed on the screen.
4865 */
4866 public static final int SCREEN_TIMEOUT_LONG = -1;
4867
Griff Hazen61a9e862014-05-22 16:05:19 -07004868 /** Notification extra which contains wearable extensions */
4869 private static final String EXTRA_WEARABLE_EXTENSIONS = "android.wearable.EXTENSIONS";
4870
Pete Gastaf6781d2014-10-07 15:17:05 -04004871 // Keys within EXTRA_WEARABLE_EXTENSIONS for wearable options.
Griff Hazen61a9e862014-05-22 16:05:19 -07004872 private static final String KEY_ACTIONS = "actions";
4873 private static final String KEY_FLAGS = "flags";
4874 private static final String KEY_DISPLAY_INTENT = "displayIntent";
4875 private static final String KEY_PAGES = "pages";
4876 private static final String KEY_BACKGROUND = "background";
4877 private static final String KEY_CONTENT_ICON = "contentIcon";
4878 private static final String KEY_CONTENT_ICON_GRAVITY = "contentIconGravity";
4879 private static final String KEY_CONTENT_ACTION_INDEX = "contentActionIndex";
4880 private static final String KEY_CUSTOM_SIZE_PRESET = "customSizePreset";
4881 private static final String KEY_CUSTOM_CONTENT_HEIGHT = "customContentHeight";
4882 private static final String KEY_GRAVITY = "gravity";
Griff Hazen5f2edfc2014-09-29 16:28:44 -07004883 private static final String KEY_HINT_SCREEN_TIMEOUT = "hintScreenTimeout";
Griff Hazen61a9e862014-05-22 16:05:19 -07004884
4885 // Flags bitwise-ored to mFlags
4886 private static final int FLAG_CONTENT_INTENT_AVAILABLE_OFFLINE = 0x1;
4887 private static final int FLAG_HINT_HIDE_ICON = 1 << 1;
4888 private static final int FLAG_HINT_SHOW_BACKGROUND_ONLY = 1 << 2;
4889 private static final int FLAG_START_SCROLL_BOTTOM = 1 << 3;
Griff Hazen5f2edfc2014-09-29 16:28:44 -07004890 private static final int FLAG_HINT_AVOID_BACKGROUND_CLIPPING = 1 << 4;
Alex Hills9ab3a232016-04-05 14:54:56 -04004891 private static final int FLAG_HINT_CONTENT_INTENT_LAUNCHES_ACTIVITY = 1 << 6;
Griff Hazen61a9e862014-05-22 16:05:19 -07004892
4893 // Default value for flags integer
4894 private static final int DEFAULT_FLAGS = FLAG_CONTENT_INTENT_AVAILABLE_OFFLINE;
4895
4896 private static final int DEFAULT_CONTENT_ICON_GRAVITY = Gravity.END;
4897 private static final int DEFAULT_GRAVITY = Gravity.BOTTOM;
4898
4899 private ArrayList<Action> mActions = new ArrayList<Action>();
4900 private int mFlags = DEFAULT_FLAGS;
4901 private PendingIntent mDisplayIntent;
4902 private ArrayList<Notification> mPages = new ArrayList<Notification>();
4903 private Bitmap mBackground;
4904 private int mContentIcon;
4905 private int mContentIconGravity = DEFAULT_CONTENT_ICON_GRAVITY;
4906 private int mContentActionIndex = UNSET_ACTION_INDEX;
4907 private int mCustomSizePreset = SIZE_DEFAULT;
4908 private int mCustomContentHeight;
4909 private int mGravity = DEFAULT_GRAVITY;
Griff Hazen5f2edfc2014-09-29 16:28:44 -07004910 private int mHintScreenTimeout;
Griff Hazen61a9e862014-05-22 16:05:19 -07004911
4912 /**
4913 * Create a {@link android.app.Notification.WearableExtender} with default
4914 * options.
4915 */
4916 public WearableExtender() {
4917 }
4918
4919 public WearableExtender(Notification notif) {
4920 Bundle wearableBundle = notif.extras.getBundle(EXTRA_WEARABLE_EXTENSIONS);
4921 if (wearableBundle != null) {
4922 List<Action> actions = wearableBundle.getParcelableArrayList(KEY_ACTIONS);
4923 if (actions != null) {
4924 mActions.addAll(actions);
4925 }
4926
4927 mFlags = wearableBundle.getInt(KEY_FLAGS, DEFAULT_FLAGS);
4928 mDisplayIntent = wearableBundle.getParcelable(KEY_DISPLAY_INTENT);
4929
4930 Notification[] pages = getNotificationArrayFromBundle(
4931 wearableBundle, KEY_PAGES);
4932 if (pages != null) {
4933 Collections.addAll(mPages, pages);
4934 }
4935
4936 mBackground = wearableBundle.getParcelable(KEY_BACKGROUND);
4937 mContentIcon = wearableBundle.getInt(KEY_CONTENT_ICON);
4938 mContentIconGravity = wearableBundle.getInt(KEY_CONTENT_ICON_GRAVITY,
4939 DEFAULT_CONTENT_ICON_GRAVITY);
4940 mContentActionIndex = wearableBundle.getInt(KEY_CONTENT_ACTION_INDEX,
4941 UNSET_ACTION_INDEX);
4942 mCustomSizePreset = wearableBundle.getInt(KEY_CUSTOM_SIZE_PRESET,
4943 SIZE_DEFAULT);
4944 mCustomContentHeight = wearableBundle.getInt(KEY_CUSTOM_CONTENT_HEIGHT);
4945 mGravity = wearableBundle.getInt(KEY_GRAVITY, DEFAULT_GRAVITY);
Griff Hazen5f2edfc2014-09-29 16:28:44 -07004946 mHintScreenTimeout = wearableBundle.getInt(KEY_HINT_SCREEN_TIMEOUT);
Griff Hazen61a9e862014-05-22 16:05:19 -07004947 }
4948 }
4949
4950 /**
4951 * Apply wearable extensions to a notification that is being built. This is typically
4952 * called by the {@link android.app.Notification.Builder#extend} method of
4953 * {@link android.app.Notification.Builder}.
4954 */
4955 @Override
4956 public Notification.Builder extend(Notification.Builder builder) {
4957 Bundle wearableBundle = new Bundle();
4958
4959 if (!mActions.isEmpty()) {
4960 wearableBundle.putParcelableArrayList(KEY_ACTIONS, mActions);
4961 }
4962 if (mFlags != DEFAULT_FLAGS) {
4963 wearableBundle.putInt(KEY_FLAGS, mFlags);
4964 }
4965 if (mDisplayIntent != null) {
4966 wearableBundle.putParcelable(KEY_DISPLAY_INTENT, mDisplayIntent);
4967 }
4968 if (!mPages.isEmpty()) {
4969 wearableBundle.putParcelableArray(KEY_PAGES, mPages.toArray(
4970 new Notification[mPages.size()]));
4971 }
4972 if (mBackground != null) {
4973 wearableBundle.putParcelable(KEY_BACKGROUND, mBackground);
4974 }
4975 if (mContentIcon != 0) {
4976 wearableBundle.putInt(KEY_CONTENT_ICON, mContentIcon);
4977 }
4978 if (mContentIconGravity != DEFAULT_CONTENT_ICON_GRAVITY) {
4979 wearableBundle.putInt(KEY_CONTENT_ICON_GRAVITY, mContentIconGravity);
4980 }
4981 if (mContentActionIndex != UNSET_ACTION_INDEX) {
4982 wearableBundle.putInt(KEY_CONTENT_ACTION_INDEX,
4983 mContentActionIndex);
4984 }
4985 if (mCustomSizePreset != SIZE_DEFAULT) {
4986 wearableBundle.putInt(KEY_CUSTOM_SIZE_PRESET, mCustomSizePreset);
4987 }
4988 if (mCustomContentHeight != 0) {
4989 wearableBundle.putInt(KEY_CUSTOM_CONTENT_HEIGHT, mCustomContentHeight);
4990 }
4991 if (mGravity != DEFAULT_GRAVITY) {
4992 wearableBundle.putInt(KEY_GRAVITY, mGravity);
4993 }
Griff Hazen5f2edfc2014-09-29 16:28:44 -07004994 if (mHintScreenTimeout != 0) {
4995 wearableBundle.putInt(KEY_HINT_SCREEN_TIMEOUT, mHintScreenTimeout);
4996 }
Griff Hazen61a9e862014-05-22 16:05:19 -07004997
4998 builder.getExtras().putBundle(EXTRA_WEARABLE_EXTENSIONS, wearableBundle);
4999 return builder;
5000 }
5001
5002 @Override
5003 public WearableExtender clone() {
5004 WearableExtender that = new WearableExtender();
5005 that.mActions = new ArrayList<Action>(this.mActions);
5006 that.mFlags = this.mFlags;
5007 that.mDisplayIntent = this.mDisplayIntent;
5008 that.mPages = new ArrayList<Notification>(this.mPages);
5009 that.mBackground = this.mBackground;
5010 that.mContentIcon = this.mContentIcon;
5011 that.mContentIconGravity = this.mContentIconGravity;
5012 that.mContentActionIndex = this.mContentActionIndex;
5013 that.mCustomSizePreset = this.mCustomSizePreset;
5014 that.mCustomContentHeight = this.mCustomContentHeight;
5015 that.mGravity = this.mGravity;
Griff Hazen5f2edfc2014-09-29 16:28:44 -07005016 that.mHintScreenTimeout = this.mHintScreenTimeout;
Griff Hazen61a9e862014-05-22 16:05:19 -07005017 return that;
5018 }
5019
5020 /**
5021 * Add a wearable action to this notification.
5022 *
5023 * <p>When wearable actions are added using this method, the set of actions that
5024 * show on a wearable device splits from devices that only show actions added
5025 * using {@link android.app.Notification.Builder#addAction}. This allows for customization
5026 * of which actions display on different devices.
5027 *
5028 * @param action the action to add to this notification
5029 * @return this object for method chaining
5030 * @see android.app.Notification.Action
5031 */
5032 public WearableExtender addAction(Action action) {
5033 mActions.add(action);
5034 return this;
5035 }
5036
5037 /**
5038 * Adds wearable actions to this notification.
5039 *
5040 * <p>When wearable actions are added using this method, the set of actions that
5041 * show on a wearable device splits from devices that only show actions added
5042 * using {@link android.app.Notification.Builder#addAction}. This allows for customization
5043 * of which actions display on different devices.
5044 *
5045 * @param actions the actions to add to this notification
5046 * @return this object for method chaining
5047 * @see android.app.Notification.Action
5048 */
5049 public WearableExtender addActions(List<Action> actions) {
5050 mActions.addAll(actions);
5051 return this;
5052 }
5053
5054 /**
5055 * Clear all wearable actions present on this builder.
5056 * @return this object for method chaining.
5057 * @see #addAction
5058 */
5059 public WearableExtender clearActions() {
5060 mActions.clear();
5061 return this;
5062 }
5063
5064 /**
5065 * Get the wearable actions present on this notification.
5066 */
5067 public List<Action> getActions() {
5068 return mActions;
5069 }
5070
5071 /**
5072 * Set an intent to launch inside of an activity view when displaying
Griff Hazen14f57992014-05-26 09:07:14 -07005073 * this notification. The {@link PendingIntent} provided should be for an activity.
5074 *
5075 * <pre class="prettyprint">
5076 * Intent displayIntent = new Intent(context, MyDisplayActivity.class);
5077 * PendingIntent displayPendingIntent = PendingIntent.getActivity(context,
5078 * 0, displayIntent, PendingIntent.FLAG_UPDATE_CURRENT);
5079 * Notification notif = new Notification.Builder(context)
5080 * .extend(new Notification.WearableExtender()
5081 * .setDisplayIntent(displayPendingIntent)
5082 * .setCustomSizePreset(Notification.WearableExtender.SIZE_MEDIUM))
5083 * .build();</pre>
5084 *
5085 * <p>The activity to launch needs to allow embedding, must be exported, and
Griff Hazen831ca9d2014-06-17 00:38:38 -07005086 * should have an empty task affinity. It is also recommended to use the device
5087 * default light theme.
Griff Hazen14f57992014-05-26 09:07:14 -07005088 *
5089 * <p>Example AndroidManifest.xml entry:
5090 * <pre class="prettyprint">
5091 * &lt;activity android:name=&quot;com.example.MyDisplayActivity&quot;
5092 * android:exported=&quot;true&quot;
5093 * android:allowEmbedded=&quot;true&quot;
Griff Hazen831ca9d2014-06-17 00:38:38 -07005094 * android:taskAffinity=&quot;&quot;
5095 * android:theme=&quot;@android:style/Theme.DeviceDefault.Light&quot; /&gt;</pre>
Griff Hazen61a9e862014-05-22 16:05:19 -07005096 *
5097 * @param intent the {@link PendingIntent} for an activity
5098 * @return this object for method chaining
5099 * @see android.app.Notification.WearableExtender#getDisplayIntent
5100 */
5101 public WearableExtender setDisplayIntent(PendingIntent intent) {
5102 mDisplayIntent = intent;
5103 return this;
5104 }
5105
5106 /**
5107 * Get the intent to launch inside of an activity view when displaying this
5108 * notification. This {@code PendingIntent} should be for an activity.
5109 */
5110 public PendingIntent getDisplayIntent() {
5111 return mDisplayIntent;
5112 }
5113
5114 /**
5115 * Add an additional page of content to display with this notification. The current
5116 * notification forms the first page, and pages added using this function form
5117 * subsequent pages. This field can be used to separate a notification into multiple
5118 * sections.
5119 *
5120 * @param page the notification to add as another page
5121 * @return this object for method chaining
5122 * @see android.app.Notification.WearableExtender#getPages
5123 */
5124 public WearableExtender addPage(Notification page) {
5125 mPages.add(page);
5126 return this;
5127 }
5128
5129 /**
5130 * Add additional pages of content to display with this notification. The current
5131 * notification forms the first page, and pages added using this function form
5132 * subsequent pages. This field can be used to separate a notification into multiple
5133 * sections.
5134 *
5135 * @param pages a list of notifications
5136 * @return this object for method chaining
5137 * @see android.app.Notification.WearableExtender#getPages
5138 */
5139 public WearableExtender addPages(List<Notification> pages) {
5140 mPages.addAll(pages);
5141 return this;
5142 }
5143
5144 /**
5145 * Clear all additional pages present on this builder.
5146 * @return this object for method chaining.
5147 * @see #addPage
5148 */
5149 public WearableExtender clearPages() {
5150 mPages.clear();
5151 return this;
5152 }
5153
5154 /**
5155 * Get the array of additional pages of content for displaying this notification. The
5156 * current notification forms the first page, and elements within this array form
5157 * subsequent pages. This field can be used to separate a notification into multiple
5158 * sections.
5159 * @return the pages for this notification
5160 */
5161 public List<Notification> getPages() {
5162 return mPages;
5163 }
5164
5165 /**
5166 * Set a background image to be displayed behind the notification content.
5167 * Contrary to the {@link android.app.Notification.BigPictureStyle}, this background
5168 * will work with any notification style.
5169 *
5170 * @param background the background bitmap
5171 * @return this object for method chaining
5172 * @see android.app.Notification.WearableExtender#getBackground
5173 */
5174 public WearableExtender setBackground(Bitmap background) {
5175 mBackground = background;
5176 return this;
5177 }
5178
5179 /**
5180 * Get a background image to be displayed behind the notification content.
5181 * Contrary to the {@link android.app.Notification.BigPictureStyle}, this background
5182 * will work with any notification style.
5183 *
5184 * @return the background image
5185 * @see android.app.Notification.WearableExtender#setBackground
5186 */
5187 public Bitmap getBackground() {
5188 return mBackground;
5189 }
5190
5191 /**
5192 * Set an icon that goes with the content of this notification.
5193 */
5194 public WearableExtender setContentIcon(int icon) {
5195 mContentIcon = icon;
5196 return this;
5197 }
5198
5199 /**
5200 * Get an icon that goes with the content of this notification.
5201 */
5202 public int getContentIcon() {
5203 return mContentIcon;
5204 }
5205
5206 /**
5207 * Set the gravity that the content icon should have within the notification display.
5208 * Supported values include {@link android.view.Gravity#START} and
5209 * {@link android.view.Gravity#END}. The default value is {@link android.view.Gravity#END}.
5210 * @see #setContentIcon
5211 */
5212 public WearableExtender setContentIconGravity(int contentIconGravity) {
5213 mContentIconGravity = contentIconGravity;
5214 return this;
5215 }
5216
5217 /**
5218 * Get the gravity that the content icon should have within the notification display.
5219 * Supported values include {@link android.view.Gravity#START} and
5220 * {@link android.view.Gravity#END}. The default value is {@link android.view.Gravity#END}.
5221 * @see #getContentIcon
5222 */
5223 public int getContentIconGravity() {
5224 return mContentIconGravity;
5225 }
5226
5227 /**
5228 * Set an action from this notification's actions to be clickable with the content of
Griff Hazen14f57992014-05-26 09:07:14 -07005229 * this notification. This action will no longer display separately from the
5230 * notification's content.
5231 *
Griff Hazenca48d352014-05-28 22:37:13 -07005232 * <p>For notifications with multiple pages, child pages can also have content actions
Griff Hazen14f57992014-05-26 09:07:14 -07005233 * set, although the list of available actions comes from the main notification and not
5234 * from the child page's notification.
5235 *
5236 * @param actionIndex The index of the action to hoist onto the current notification page.
5237 * If wearable actions were added to the main notification, this index
5238 * will apply to that list, otherwise it will apply to the regular
5239 * actions list.
Griff Hazen61a9e862014-05-22 16:05:19 -07005240 */
5241 public WearableExtender setContentAction(int actionIndex) {
5242 mContentActionIndex = actionIndex;
5243 return this;
5244 }
5245
5246 /**
Griff Hazenca48d352014-05-28 22:37:13 -07005247 * Get the index of the notification action, if any, that was specified as being clickable
5248 * with the content of this notification. This action will no longer display separately
Griff Hazen14f57992014-05-26 09:07:14 -07005249 * from the notification's content.
Griff Hazen61a9e862014-05-22 16:05:19 -07005250 *
Griff Hazenca48d352014-05-28 22:37:13 -07005251 * <p>For notifications with multiple pages, child pages can also have content actions
Griff Hazen14f57992014-05-26 09:07:14 -07005252 * set, although the list of available actions comes from the main notification and not
5253 * from the child page's notification.
5254 *
5255 * <p>If wearable specific actions were added to the main notification, this index will
5256 * apply to that list, otherwise it will apply to the regular actions list.
Griff Hazenca48d352014-05-28 22:37:13 -07005257 *
5258 * @return the action index or {@link #UNSET_ACTION_INDEX} if no action was selected.
Griff Hazen61a9e862014-05-22 16:05:19 -07005259 */
5260 public int getContentAction() {
5261 return mContentActionIndex;
5262 }
5263
5264 /**
5265 * Set the gravity that this notification should have within the available viewport space.
5266 * Supported values include {@link android.view.Gravity#TOP},
5267 * {@link android.view.Gravity#CENTER_VERTICAL} and {@link android.view.Gravity#BOTTOM}.
5268 * The default value is {@link android.view.Gravity#BOTTOM}.
5269 */
5270 public WearableExtender setGravity(int gravity) {
5271 mGravity = gravity;
5272 return this;
5273 }
5274
5275 /**
5276 * Get the gravity that this notification should have within the available viewport space.
5277 * Supported values include {@link android.view.Gravity#TOP},
5278 * {@link android.view.Gravity#CENTER_VERTICAL} and {@link android.view.Gravity#BOTTOM}.
5279 * The default value is {@link android.view.Gravity#BOTTOM}.
5280 */
5281 public int getGravity() {
5282 return mGravity;
5283 }
5284
5285 /**
5286 * Set the custom size preset for the display of this notification out of the available
5287 * presets found in {@link android.app.Notification.WearableExtender}, e.g.
5288 * {@link #SIZE_LARGE}.
5289 * <p>Some custom size presets are only applicable for custom display notifications created
5290 * using {@link android.app.Notification.WearableExtender#setDisplayIntent}. Check the
5291 * documentation for the preset in question. See also
5292 * {@link #setCustomContentHeight} and {@link #getCustomSizePreset}.
5293 */
5294 public WearableExtender setCustomSizePreset(int sizePreset) {
5295 mCustomSizePreset = sizePreset;
5296 return this;
5297 }
5298
5299 /**
5300 * Get the custom size preset for the display of this notification out of the available
5301 * presets found in {@link android.app.Notification.WearableExtender}, e.g.
5302 * {@link #SIZE_LARGE}.
5303 * <p>Some custom size presets are only applicable for custom display notifications created
5304 * using {@link #setDisplayIntent}. Check the documentation for the preset in question.
5305 * See also {@link #setCustomContentHeight} and {@link #setCustomSizePreset}.
5306 */
5307 public int getCustomSizePreset() {
5308 return mCustomSizePreset;
5309 }
5310
5311 /**
5312 * Set the custom height in pixels for the display of this notification's content.
5313 * <p>This option is only available for custom display notifications created
5314 * using {@link android.app.Notification.WearableExtender#setDisplayIntent}. See also
5315 * {@link android.app.Notification.WearableExtender#setCustomSizePreset} and
5316 * {@link #getCustomContentHeight}.
5317 */
5318 public WearableExtender setCustomContentHeight(int height) {
5319 mCustomContentHeight = height;
5320 return this;
5321 }
5322
5323 /**
5324 * Get the custom height in pixels for the display of this notification's content.
5325 * <p>This option is only available for custom display notifications created
5326 * using {@link #setDisplayIntent}. See also {@link #setCustomSizePreset} and
5327 * {@link #setCustomContentHeight}.
5328 */
5329 public int getCustomContentHeight() {
5330 return mCustomContentHeight;
5331 }
5332
5333 /**
5334 * Set whether the scrolling position for the contents of this notification should start
5335 * at the bottom of the contents instead of the top when the contents are too long to
5336 * display within the screen. Default is false (start scroll at the top).
5337 */
5338 public WearableExtender setStartScrollBottom(boolean startScrollBottom) {
5339 setFlag(FLAG_START_SCROLL_BOTTOM, startScrollBottom);
5340 return this;
5341 }
5342
5343 /**
5344 * Get whether the scrolling position for the contents of this notification should start
5345 * at the bottom of the contents instead of the top when the contents are too long to
5346 * display within the screen. Default is false (start scroll at the top).
5347 */
5348 public boolean getStartScrollBottom() {
5349 return (mFlags & FLAG_START_SCROLL_BOTTOM) != 0;
5350 }
5351
5352 /**
5353 * Set whether the content intent is available when the wearable device is not connected
5354 * to a companion device. The user can still trigger this intent when the wearable device
5355 * is offline, but a visual hint will indicate that the content intent may not be available.
5356 * Defaults to true.
5357 */
5358 public WearableExtender setContentIntentAvailableOffline(
5359 boolean contentIntentAvailableOffline) {
5360 setFlag(FLAG_CONTENT_INTENT_AVAILABLE_OFFLINE, contentIntentAvailableOffline);
5361 return this;
5362 }
5363
5364 /**
5365 * Get whether the content intent is available when the wearable device is not connected
5366 * to a companion device. The user can still trigger this intent when the wearable device
5367 * is offline, but a visual hint will indicate that the content intent may not be available.
5368 * Defaults to true.
5369 */
5370 public boolean getContentIntentAvailableOffline() {
5371 return (mFlags & FLAG_CONTENT_INTENT_AVAILABLE_OFFLINE) != 0;
5372 }
5373
5374 /**
5375 * Set a hint that this notification's icon should not be displayed.
5376 * @param hintHideIcon {@code true} to hide the icon, {@code false} otherwise.
5377 * @return this object for method chaining
5378 */
5379 public WearableExtender setHintHideIcon(boolean hintHideIcon) {
5380 setFlag(FLAG_HINT_HIDE_ICON, hintHideIcon);
5381 return this;
5382 }
5383
5384 /**
5385 * Get a hint that this notification's icon should not be displayed.
5386 * @return {@code true} if this icon should not be displayed, false otherwise.
5387 * The default value is {@code false} if this was never set.
5388 */
5389 public boolean getHintHideIcon() {
5390 return (mFlags & FLAG_HINT_HIDE_ICON) != 0;
5391 }
5392
5393 /**
5394 * Set a visual hint that only the background image of this notification should be
5395 * displayed, and other semantic content should be hidden. This hint is only applicable
5396 * to sub-pages added using {@link #addPage}.
5397 */
5398 public WearableExtender setHintShowBackgroundOnly(boolean hintShowBackgroundOnly) {
5399 setFlag(FLAG_HINT_SHOW_BACKGROUND_ONLY, hintShowBackgroundOnly);
5400 return this;
5401 }
5402
5403 /**
5404 * Get a visual hint that only the background image of this notification should be
5405 * displayed, and other semantic content should be hidden. This hint is only applicable
5406 * to sub-pages added using {@link android.app.Notification.WearableExtender#addPage}.
5407 */
5408 public boolean getHintShowBackgroundOnly() {
5409 return (mFlags & FLAG_HINT_SHOW_BACKGROUND_ONLY) != 0;
5410 }
5411
Griff Hazen5f2edfc2014-09-29 16:28:44 -07005412 /**
Griff Hazen9c5be4e2014-11-17 17:47:50 -08005413 * Set a hint that this notification's background should not be clipped if possible,
5414 * and should instead be resized to fully display on the screen, retaining the aspect
5415 * ratio of the image. This can be useful for images like barcodes or qr codes.
Griff Hazen5f2edfc2014-09-29 16:28:44 -07005416 * @param hintAvoidBackgroundClipping {@code true} to avoid clipping if possible.
5417 * @return this object for method chaining
5418 */
5419 public WearableExtender setHintAvoidBackgroundClipping(
5420 boolean hintAvoidBackgroundClipping) {
5421 setFlag(FLAG_HINT_AVOID_BACKGROUND_CLIPPING, hintAvoidBackgroundClipping);
5422 return this;
5423 }
5424
5425 /**
Griff Hazen9c5be4e2014-11-17 17:47:50 -08005426 * Get a hint that this notification's background should not be clipped if possible,
5427 * and should instead be resized to fully display on the screen, retaining the aspect
5428 * ratio of the image. This can be useful for images like barcodes or qr codes.
Griff Hazen5f2edfc2014-09-29 16:28:44 -07005429 * @return {@code true} if it's ok if the background is clipped on the screen, false
5430 * otherwise. The default value is {@code false} if this was never set.
5431 */
5432 public boolean getHintAvoidBackgroundClipping() {
5433 return (mFlags & FLAG_HINT_AVOID_BACKGROUND_CLIPPING) != 0;
5434 }
5435
5436 /**
5437 * Set a hint that the screen should remain on for at least this duration when
5438 * this notification is displayed on the screen.
5439 * @param timeout The requested screen timeout in milliseconds. Can also be either
5440 * {@link #SCREEN_TIMEOUT_SHORT} or {@link #SCREEN_TIMEOUT_LONG}.
5441 * @return this object for method chaining
5442 */
5443 public WearableExtender setHintScreenTimeout(int timeout) {
5444 mHintScreenTimeout = timeout;
5445 return this;
5446 }
5447
5448 /**
5449 * Get the duration, in milliseconds, that the screen should remain on for
5450 * when this notification is displayed.
5451 * @return the duration in milliseconds if > 0, or either one of the sentinel values
5452 * {@link #SCREEN_TIMEOUT_SHORT} or {@link #SCREEN_TIMEOUT_LONG}.
5453 */
5454 public int getHintScreenTimeout() {
5455 return mHintScreenTimeout;
5456 }
5457
Alex Hills9ab3a232016-04-05 14:54:56 -04005458 /**
5459 * Set a hint that this notification's content intent will launch an {@link Activity}
5460 * directly, telling the platform that it can generate the appropriate transitions.
5461 * @param hintContentIntentLaunchesActivity {@code true} if the content intent will launch
5462 * an activity and transitions should be generated, false otherwise.
5463 * @return this object for method chaining
5464 */
5465 public WearableExtender setHintContentIntentLaunchesActivity(
5466 boolean hintContentIntentLaunchesActivity) {
5467 setFlag(FLAG_HINT_CONTENT_INTENT_LAUNCHES_ACTIVITY, hintContentIntentLaunchesActivity);
5468 return this;
5469 }
5470
5471 /**
5472 * Get a hint that this notification's content intent will launch an {@link Activity}
5473 * directly, telling the platform that it can generate the appropriate transitions
5474 * @return {@code true} if the content intent will launch an activity and transitions should
5475 * be generated, false otherwise. The default value is {@code false} if this was never set.
5476 */
5477 public boolean getHintContentIntentLaunchesActivity() {
5478 return (mFlags & FLAG_HINT_CONTENT_INTENT_LAUNCHES_ACTIVITY) != 0;
5479 }
5480
Griff Hazen61a9e862014-05-22 16:05:19 -07005481 private void setFlag(int mask, boolean value) {
5482 if (value) {
5483 mFlags |= mask;
5484 } else {
5485 mFlags &= ~mask;
5486 }
5487 }
5488 }
5489
5490 /**
Zhen Yu Song9d5528b2014-11-14 15:34:39 -08005491 * <p>Helper class to add Android Auto extensions to notifications. To create a notification
5492 * with car extensions:
5493 *
5494 * <ol>
5495 * <li>Create an {@link Notification.Builder}, setting any desired
5496 * properties.
5497 * <li>Create a {@link CarExtender}.
5498 * <li>Set car-specific properties using the {@code add} and {@code set} methods of
5499 * {@link CarExtender}.
5500 * <li>Call {@link Notification.Builder#extend(Notification.Extender)}
5501 * to apply the extensions to a notification.
5502 * </ol>
5503 *
5504 * <pre class="prettyprint">
5505 * Notification notification = new Notification.Builder(context)
5506 * ...
5507 * .extend(new CarExtender()
5508 * .set*(...))
5509 * .build();
5510 * </pre>
5511 *
5512 * <p>Car extensions can be accessed on an existing notification by using the
5513 * {@code CarExtender(Notification)} constructor, and then using the {@code get} methods
5514 * to access values.
5515 */
5516 public static final class CarExtender implements Extender {
5517 private static final String TAG = "CarExtender";
5518
5519 private static final String EXTRA_CAR_EXTENDER = "android.car.EXTENSIONS";
5520 private static final String EXTRA_LARGE_ICON = "large_icon";
5521 private static final String EXTRA_CONVERSATION = "car_conversation";
5522 private static final String EXTRA_COLOR = "app_color";
5523
5524 private Bitmap mLargeIcon;
5525 private UnreadConversation mUnreadConversation;
5526 private int mColor = Notification.COLOR_DEFAULT;
5527
5528 /**
5529 * Create a {@link CarExtender} with default options.
5530 */
5531 public CarExtender() {
5532 }
5533
5534 /**
5535 * Create a {@link CarExtender} from the CarExtender options of an existing Notification.
5536 *
5537 * @param notif The notification from which to copy options.
5538 */
5539 public CarExtender(Notification notif) {
5540 Bundle carBundle = notif.extras == null ?
5541 null : notif.extras.getBundle(EXTRA_CAR_EXTENDER);
5542 if (carBundle != null) {
5543 mLargeIcon = carBundle.getParcelable(EXTRA_LARGE_ICON);
5544 mColor = carBundle.getInt(EXTRA_COLOR, Notification.COLOR_DEFAULT);
5545
5546 Bundle b = carBundle.getBundle(EXTRA_CONVERSATION);
5547 mUnreadConversation = UnreadConversation.getUnreadConversationFromBundle(b);
5548 }
5549 }
5550
5551 /**
5552 * Apply car extensions to a notification that is being built. This is typically called by
5553 * the {@link Notification.Builder#extend(Notification.Extender)}
5554 * method of {@link Notification.Builder}.
5555 */
5556 @Override
5557 public Notification.Builder extend(Notification.Builder builder) {
5558 Bundle carExtensions = new Bundle();
5559
5560 if (mLargeIcon != null) {
5561 carExtensions.putParcelable(EXTRA_LARGE_ICON, mLargeIcon);
5562 }
5563 if (mColor != Notification.COLOR_DEFAULT) {
5564 carExtensions.putInt(EXTRA_COLOR, mColor);
5565 }
5566
5567 if (mUnreadConversation != null) {
5568 Bundle b = mUnreadConversation.getBundleForUnreadConversation();
5569 carExtensions.putBundle(EXTRA_CONVERSATION, b);
5570 }
5571
5572 builder.getExtras().putBundle(EXTRA_CAR_EXTENDER, carExtensions);
5573 return builder;
5574 }
5575
5576 /**
5577 * Sets the accent color to use when Android Auto presents the notification.
5578 *
5579 * Android Auto uses the color set with {@link Notification.Builder#setColor(int)}
5580 * to accent the displayed notification. However, not all colors are acceptable in an
5581 * automotive setting. This method can be used to override the color provided in the
5582 * notification in such a situation.
5583 */
Tor Norbye80756e32015-03-02 09:39:27 -08005584 public CarExtender setColor(@ColorInt int color) {
Zhen Yu Song9d5528b2014-11-14 15:34:39 -08005585 mColor = color;
5586 return this;
5587 }
5588
5589 /**
5590 * Gets the accent color.
5591 *
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005592 * @see #setColor
Zhen Yu Song9d5528b2014-11-14 15:34:39 -08005593 */
Tor Norbye80756e32015-03-02 09:39:27 -08005594 @ColorInt
Zhen Yu Song9d5528b2014-11-14 15:34:39 -08005595 public int getColor() {
5596 return mColor;
5597 }
5598
5599 /**
5600 * Sets the large icon of the car notification.
5601 *
5602 * If no large icon is set in the extender, Android Auto will display the icon
5603 * specified by {@link Notification.Builder#setLargeIcon(android.graphics.Bitmap)}
5604 *
5605 * @param largeIcon The large icon to use in the car notification.
5606 * @return This object for method chaining.
5607 */
5608 public CarExtender setLargeIcon(Bitmap largeIcon) {
5609 mLargeIcon = largeIcon;
5610 return this;
5611 }
5612
5613 /**
5614 * Gets the large icon used in this car notification, or null if no icon has been set.
5615 *
5616 * @return The large icon for the car notification.
5617 * @see CarExtender#setLargeIcon
5618 */
5619 public Bitmap getLargeIcon() {
5620 return mLargeIcon;
5621 }
5622
5623 /**
5624 * Sets the unread conversation in a message notification.
5625 *
5626 * @param unreadConversation The unread part of the conversation this notification conveys.
5627 * @return This object for method chaining.
5628 */
5629 public CarExtender setUnreadConversation(UnreadConversation unreadConversation) {
5630 mUnreadConversation = unreadConversation;
5631 return this;
5632 }
5633
5634 /**
5635 * Returns the unread conversation conveyed by this notification.
5636 * @see #setUnreadConversation(UnreadConversation)
5637 */
5638 public UnreadConversation getUnreadConversation() {
5639 return mUnreadConversation;
5640 }
5641
5642 /**
5643 * A class which holds the unread messages from a conversation.
5644 */
5645 public static class UnreadConversation {
5646 private static final String KEY_AUTHOR = "author";
5647 private static final String KEY_TEXT = "text";
5648 private static final String KEY_MESSAGES = "messages";
5649 private static final String KEY_REMOTE_INPUT = "remote_input";
5650 private static final String KEY_ON_REPLY = "on_reply";
5651 private static final String KEY_ON_READ = "on_read";
5652 private static final String KEY_PARTICIPANTS = "participants";
5653 private static final String KEY_TIMESTAMP = "timestamp";
5654
5655 private final String[] mMessages;
5656 private final RemoteInput mRemoteInput;
5657 private final PendingIntent mReplyPendingIntent;
5658 private final PendingIntent mReadPendingIntent;
5659 private final String[] mParticipants;
5660 private final long mLatestTimestamp;
5661
5662 UnreadConversation(String[] messages, RemoteInput remoteInput,
5663 PendingIntent replyPendingIntent, PendingIntent readPendingIntent,
5664 String[] participants, long latestTimestamp) {
5665 mMessages = messages;
5666 mRemoteInput = remoteInput;
5667 mReadPendingIntent = readPendingIntent;
5668 mReplyPendingIntent = replyPendingIntent;
5669 mParticipants = participants;
5670 mLatestTimestamp = latestTimestamp;
5671 }
5672
5673 /**
5674 * Gets the list of messages conveyed by this notification.
5675 */
5676 public String[] getMessages() {
5677 return mMessages;
5678 }
5679
5680 /**
5681 * Gets the remote input that will be used to convey the response to a message list, or
5682 * null if no such remote input exists.
5683 */
5684 public RemoteInput getRemoteInput() {
5685 return mRemoteInput;
5686 }
5687
5688 /**
5689 * Gets the pending intent that will be triggered when the user replies to this
5690 * notification.
5691 */
5692 public PendingIntent getReplyPendingIntent() {
5693 return mReplyPendingIntent;
5694 }
5695
5696 /**
5697 * Gets the pending intent that Android Auto will send after it reads aloud all messages
5698 * in this object's message list.
5699 */
5700 public PendingIntent getReadPendingIntent() {
5701 return mReadPendingIntent;
5702 }
5703
5704 /**
5705 * Gets the participants in the conversation.
5706 */
5707 public String[] getParticipants() {
5708 return mParticipants;
5709 }
5710
5711 /**
5712 * Gets the firs participant in the conversation.
5713 */
5714 public String getParticipant() {
5715 return mParticipants.length > 0 ? mParticipants[0] : null;
5716 }
5717
5718 /**
5719 * Gets the timestamp of the conversation.
5720 */
5721 public long getLatestTimestamp() {
5722 return mLatestTimestamp;
5723 }
5724
5725 Bundle getBundleForUnreadConversation() {
5726 Bundle b = new Bundle();
5727 String author = null;
5728 if (mParticipants != null && mParticipants.length > 1) {
5729 author = mParticipants[0];
5730 }
5731 Parcelable[] messages = new Parcelable[mMessages.length];
5732 for (int i = 0; i < messages.length; i++) {
5733 Bundle m = new Bundle();
5734 m.putString(KEY_TEXT, mMessages[i]);
5735 m.putString(KEY_AUTHOR, author);
5736 messages[i] = m;
5737 }
5738 b.putParcelableArray(KEY_MESSAGES, messages);
5739 if (mRemoteInput != null) {
5740 b.putParcelable(KEY_REMOTE_INPUT, mRemoteInput);
5741 }
5742 b.putParcelable(KEY_ON_REPLY, mReplyPendingIntent);
5743 b.putParcelable(KEY_ON_READ, mReadPendingIntent);
5744 b.putStringArray(KEY_PARTICIPANTS, mParticipants);
5745 b.putLong(KEY_TIMESTAMP, mLatestTimestamp);
5746 return b;
5747 }
5748
5749 static UnreadConversation getUnreadConversationFromBundle(Bundle b) {
5750 if (b == null) {
5751 return null;
5752 }
5753 Parcelable[] parcelableMessages = b.getParcelableArray(KEY_MESSAGES);
5754 String[] messages = null;
5755 if (parcelableMessages != null) {
5756 String[] tmp = new String[parcelableMessages.length];
5757 boolean success = true;
5758 for (int i = 0; i < tmp.length; i++) {
5759 if (!(parcelableMessages[i] instanceof Bundle)) {
5760 success = false;
5761 break;
5762 }
5763 tmp[i] = ((Bundle) parcelableMessages[i]).getString(KEY_TEXT);
5764 if (tmp[i] == null) {
5765 success = false;
5766 break;
5767 }
5768 }
5769 if (success) {
5770 messages = tmp;
5771 } else {
5772 return null;
5773 }
5774 }
5775
5776 PendingIntent onRead = b.getParcelable(KEY_ON_READ);
5777 PendingIntent onReply = b.getParcelable(KEY_ON_REPLY);
5778
5779 RemoteInput remoteInput = b.getParcelable(KEY_REMOTE_INPUT);
5780
5781 String[] participants = b.getStringArray(KEY_PARTICIPANTS);
5782 if (participants == null || participants.length != 1) {
5783 return null;
5784 }
5785
5786 return new UnreadConversation(messages,
5787 remoteInput,
5788 onReply,
5789 onRead,
5790 participants, b.getLong(KEY_TIMESTAMP));
5791 }
5792 };
5793
5794 /**
5795 * Builder class for {@link CarExtender.UnreadConversation} objects.
5796 */
5797 public static class Builder {
5798 private final List<String> mMessages = new ArrayList<String>();
5799 private final String mParticipant;
5800 private RemoteInput mRemoteInput;
5801 private PendingIntent mReadPendingIntent;
5802 private PendingIntent mReplyPendingIntent;
5803 private long mLatestTimestamp;
5804
5805 /**
5806 * Constructs a new builder for {@link CarExtender.UnreadConversation}.
5807 *
5808 * @param name The name of the other participant in the conversation.
5809 */
5810 public Builder(String name) {
5811 mParticipant = name;
5812 }
5813
5814 /**
5815 * Appends a new unread message to the list of messages for this conversation.
5816 *
5817 * The messages should be added from oldest to newest.
5818 *
5819 * @param message The text of the new unread message.
5820 * @return This object for method chaining.
5821 */
5822 public Builder addMessage(String message) {
5823 mMessages.add(message);
5824 return this;
5825 }
5826
5827 /**
5828 * Sets the pending intent and remote input which will convey the reply to this
5829 * notification.
5830 *
5831 * @param pendingIntent The pending intent which will be triggered on a reply.
5832 * @param remoteInput The remote input parcelable which will carry the reply.
5833 * @return This object for method chaining.
5834 *
5835 * @see CarExtender.UnreadConversation#getRemoteInput
5836 * @see CarExtender.UnreadConversation#getReplyPendingIntent
5837 */
5838 public Builder setReplyAction(
5839 PendingIntent pendingIntent, RemoteInput remoteInput) {
5840 mRemoteInput = remoteInput;
5841 mReplyPendingIntent = pendingIntent;
5842
5843 return this;
5844 }
5845
5846 /**
5847 * Sets the pending intent that will be sent once the messages in this notification
5848 * are read.
5849 *
5850 * @param pendingIntent The pending intent to use.
5851 * @return This object for method chaining.
5852 */
5853 public Builder setReadPendingIntent(PendingIntent pendingIntent) {
5854 mReadPendingIntent = pendingIntent;
5855 return this;
5856 }
5857
5858 /**
5859 * Sets the timestamp of the most recent message in an unread conversation.
5860 *
5861 * If a messaging notification has been posted by your application and has not
5862 * yet been cancelled, posting a later notification with the same id and tag
5863 * but without a newer timestamp may result in Android Auto not displaying a
5864 * heads up notification for the later notification.
5865 *
5866 * @param timestamp The timestamp of the most recent message in the conversation.
5867 * @return This object for method chaining.
5868 */
5869 public Builder setLatestTimestamp(long timestamp) {
5870 mLatestTimestamp = timestamp;
5871 return this;
5872 }
5873
5874 /**
5875 * Builds a new unread conversation object.
5876 *
5877 * @return The new unread conversation object.
5878 */
5879 public UnreadConversation build() {
5880 String[] messages = mMessages.toArray(new String[mMessages.size()]);
5881 String[] participants = { mParticipant };
5882 return new UnreadConversation(messages, mRemoteInput, mReplyPendingIntent,
5883 mReadPendingIntent, participants, mLatestTimestamp);
5884 }
5885 }
5886 }
5887
5888 /**
Griff Hazen61a9e862014-05-22 16:05:19 -07005889 * Get an array of Notification objects from a parcelable array bundle field.
5890 * Update the bundle to have a typed array so fetches in the future don't need
5891 * to do an array copy.
5892 */
5893 private static Notification[] getNotificationArrayFromBundle(Bundle bundle, String key) {
5894 Parcelable[] array = bundle.getParcelableArray(key);
5895 if (array instanceof Notification[] || array == null) {
5896 return (Notification[]) array;
5897 }
5898 Notification[] typedArray = Arrays.copyOf(array, array.length,
5899 Notification[].class);
5900 bundle.putParcelableArray(key, typedArray);
5901 return typedArray;
5902 }
Christoph Studer4600f9b2014-07-22 22:44:43 +02005903
5904 private static class BuilderRemoteViews extends RemoteViews {
5905 public BuilderRemoteViews(Parcel parcel) {
5906 super(parcel);
5907 }
5908
Kenny Guy77320062014-08-27 21:37:15 +01005909 public BuilderRemoteViews(ApplicationInfo appInfo, int layoutId) {
5910 super(appInfo, layoutId);
Christoph Studer4600f9b2014-07-22 22:44:43 +02005911 }
5912
5913 @Override
5914 public BuilderRemoteViews clone() {
5915 Parcel p = Parcel.obtain();
5916 writeToParcel(p, 0);
5917 p.setDataPosition(0);
5918 BuilderRemoteViews brv = new BuilderRemoteViews(p);
5919 p.recycle();
5920 return brv;
5921 }
5922 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005923}