blob: 65e9f109fec0426b887c94d5053fa98079923a98 [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 *
Selim Cinek0ff1ce602016-04-05 18:27:16 -0700177 * For apps targeting {@link android.os.Build.VERSION_CODES#N} and above, this time is not shown
178 * anymore by default and must be opted into by using
179 * {@link android.app.Notification.Builder#setShowWhen(boolean)}
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800180 */
181 public long when;
182
183 /**
184 * The resource id of a drawable to use as the icon in the status bar.
Dan Sandler86647982015-05-13 23:41:13 -0400185 *
186 * @deprecated Use {@link Builder#setSmallIcon(Icon)} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800187 */
Dan Sandler86647982015-05-13 23:41:13 -0400188 @Deprecated
Tor Norbye7b9c9122013-05-30 16:48:33 -0700189 @DrawableRes
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800190 public int icon;
191
192 /**
Joe Onorato46439ce2010-11-19 13:56:21 -0800193 * If the icon in the status bar is to have more than one level, you can set this. Otherwise,
194 * leave it at its default value of 0.
195 *
196 * @see android.widget.ImageView#setImageLevel
Griff Hazen959591e2014-05-15 22:26:18 -0700197 * @see android.graphics.drawable.Drawable#setLevel
Joe Onorato46439ce2010-11-19 13:56:21 -0800198 */
199 public int iconLevel;
200
201 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500202 * The number of events that this notification represents. For example, in a new mail
203 * notification, this could be the number of unread messages.
Joe Malin8d40d042012-11-05 11:36:40 -0800204 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500205 * The system may or may not use this field to modify the appearance of the notification. For
206 * example, before {@link android.os.Build.VERSION_CODES#HONEYCOMB}, this number was
207 * superimposed over the icon in the status bar. Starting with
208 * {@link android.os.Build.VERSION_CODES#HONEYCOMB}, the template used by
209 * {@link Notification.Builder} has displayed the number in the expanded notification view.
Joe Malin8d40d042012-11-05 11:36:40 -0800210 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500211 * If the number is 0 or negative, it is never shown.
Selim Cinek0f9dd1e2016-04-05 17:03:40 -0700212 *
213 * @deprecated this number is not shown anymore
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800214 */
215 public int number;
216
217 /**
218 * The intent to execute when the expanded status entry is clicked. If
219 * this is an activity, it must include the
220 * {@link android.content.Intent#FLAG_ACTIVITY_NEW_TASK} flag, which requires
Scott Main7aee61f2011-02-08 11:25:01 -0800221 * that you take care of task management as described in the
222 * <a href="{@docRoot}guide/topics/fundamentals/tasks-and-back-stack.html">Tasks and Back
Dianne Hackborn6ceca582012-01-10 15:24:26 -0800223 * Stack</a> document. In particular, make sure to read the notification section
224 * <a href="{@docRoot}guide/topics/ui/notifiers/notifications.html#HandlingNotifications">Handling
225 * Notifications</a> for the correct ways to launch an application from a
226 * notification.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800227 */
228 public PendingIntent contentIntent;
229
230 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500231 * The intent to execute when the notification is explicitly dismissed by the user, either with
232 * the "Clear All" button or by swiping it away individually.
233 *
234 * This probably shouldn't be launching an activity since several of those will be sent
235 * at the same time.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800236 */
237 public PendingIntent deleteIntent;
238
239 /**
Dianne Hackborn170bae72010-09-03 15:14:28 -0700240 * An intent to launch instead of posting the notification to the status bar.
Joe Onoratocb109a02011-01-18 17:57:41 -0800241 *
Chris Wren47c20a12014-06-18 17:27:29 -0400242 * <p>
243 * The system UI may choose to display a heads-up notification, instead of
244 * launching this intent, while the user is using the device.
245 * </p>
246 *
Joe Onoratocb109a02011-01-18 17:57:41 -0800247 * @see Notification.Builder#setFullScreenIntent
Daniel Sandlere46cbd32010-06-17 10:35:26 -0400248 */
249 public PendingIntent fullScreenIntent;
250
251 /**
Dan Sandler5fcdf6e2014-07-18 11:31:15 -0400252 * Text that summarizes this notification for accessibility services.
253 *
254 * As of the L release, this text is no longer shown on screen, but it is still useful to
255 * accessibility services (where it serves as an audible announcement of the notification's
256 * appearance).
Joe Onoratoef1e7762010-09-17 18:38:38 -0400257 *
Joe Onorato46439ce2010-11-19 13:56:21 -0800258 * @see #tickerView
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800259 */
260 public CharSequence tickerText;
261
262 /**
Dan Sandler5fcdf6e2014-07-18 11:31:15 -0400263 * Formerly, a view showing the {@link #tickerText}.
264 *
265 * No longer displayed in the status bar as of API 21.
Joe Onoratoef1e7762010-09-17 18:38:38 -0400266 */
Dan Sandler5fcdf6e2014-07-18 11:31:15 -0400267 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -0800268 public RemoteViews tickerView;
Joe Onoratoef1e7762010-09-17 18:38:38 -0400269
270 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400271 * The view that will represent this notification in the notification list (which is pulled
272 * down from the status bar).
273 *
Julia Reynoldsd4ea7412016-02-17 14:00:56 -0500274 * As of N, this field may be null. The notification view is determined by the inputs
275 * to {@link Notification.Builder}; a custom RemoteViews can optionally be
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400276 * supplied with {@link Notification.Builder#setCustomContentView(RemoteViews)}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800277 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400278 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800279 public RemoteViews contentView;
280
Daniel Sandlera0a938c2012-03-15 08:42:37 -0400281 /**
Daniel Sandler4dfbe832012-04-11 14:51:46 -0400282 * A large-format version of {@link #contentView}, giving the Notification an
Daniel Sandlerf3b73432012-03-27 15:01:25 -0400283 * opportunity to show more detail. The system UI may choose to show this
284 * instead of the normal content view at its discretion.
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400285 *
Julia Reynoldsd4ea7412016-02-17 14:00:56 -0500286 * As of N, this field may be null. The expanded notification view is determined by the
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400287 * inputs to {@link Notification.Builder}; a custom RemoteViews can optionally be
288 * supplied with {@link Notification.Builder#setCustomBigContentView(RemoteViews)}.
Daniel Sandlerf3b73432012-03-27 15:01:25 -0400289 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400290 @Deprecated
Daniel Sandlerf3b73432012-03-27 15:01:25 -0400291 public RemoteViews bigContentView;
292
Chris Wren8fd39ec2014-02-27 17:43:26 -0500293
294 /**
Chris Wren47c20a12014-06-18 17:27:29 -0400295 * A medium-format version of {@link #contentView}, providing the Notification an
296 * opportunity to add action buttons to contentView. At its discretion, the system UI may
297 * choose to show this as a heads-up notification, which will pop up so the user can see
298 * it without leaving their current activity.
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400299 *
Julia Reynoldsd4ea7412016-02-17 14:00:56 -0500300 * As of N, this field may be null. The heads-up notification view is determined by the
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400301 * inputs to {@link Notification.Builder}; a custom RemoteViews can optionally be
302 * supplied with {@link Notification.Builder#setCustomHeadsUpContentView(RemoteViews)}.
Chris Wren8fd39ec2014-02-27 17:43:26 -0500303 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400304 @Deprecated
Chris Wren8fd39ec2014-02-27 17:43:26 -0500305 public RemoteViews headsUpContentView;
306
Daniel Sandlerf3b73432012-03-27 15:01:25 -0400307 /**
Dan Sandler86647982015-05-13 23:41:13 -0400308 * A large bitmap to be shown in the notification content area.
309 *
310 * @deprecated Use {@link Builder#setLargeIcon(Icon)} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800311 */
Dan Sandler86647982015-05-13 23:41:13 -0400312 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -0800313 public Bitmap largeIcon;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800314
315 /**
316 * The sound to play.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500317 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800318 * <p>
Chris Wren47c20a12014-06-18 17:27:29 -0400319 * A notification that is noisy is more likely to be presented as a heads-up notification.
320 * </p>
321 *
322 * <p>
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500323 * To play the default notification sound, see {@link #defaults}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800324 * </p>
325 */
326 public Uri sound;
327
328 /**
329 * Use this constant as the value for audioStreamType to request that
330 * the default stream type for notifications be used. Currently the
Jeff Sharkey098d5802012-04-26 17:30:34 -0700331 * default stream type is {@link AudioManager#STREAM_NOTIFICATION}.
John Spurlockc0650f022014-07-19 13:22:39 -0400332 *
333 * @deprecated Use {@link #audioAttributes} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800334 */
Jean-Michel Trivi81f871e2014-08-06 16:32:38 -0700335 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800336 public static final int STREAM_DEFAULT = -1;
337
338 /**
339 * The audio stream type to use when playing the sound.
340 * Should be one of the STREAM_ constants from
341 * {@link android.media.AudioManager}.
John Spurlockc0650f022014-07-19 13:22:39 -0400342 *
343 * @deprecated Use {@link #audioAttributes} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800344 */
Jean-Michel Trivi81f871e2014-08-06 16:32:38 -0700345 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800346 public int audioStreamType = STREAM_DEFAULT;
347
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800348 /**
John Spurlockc0650f022014-07-19 13:22:39 -0400349 * The default value of {@link #audioAttributes}.
350 */
351 public static final AudioAttributes AUDIO_ATTRIBUTES_DEFAULT = new AudioAttributes.Builder()
352 .setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
353 .setUsage(AudioAttributes.USAGE_NOTIFICATION)
354 .build();
355
356 /**
357 * The {@link AudioAttributes audio attributes} to use when playing the sound.
358 */
359 public AudioAttributes audioAttributes = AUDIO_ATTRIBUTES_DEFAULT;
360
361 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500362 * The pattern with which to vibrate.
363 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800364 * <p>
365 * To vibrate the default pattern, see {@link #defaults}.
366 * </p>
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500367 *
Chris Wren47c20a12014-06-18 17:27:29 -0400368 * <p>
369 * A notification that vibrates is more likely to be presented as a heads-up notification.
370 * </p>
371 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800372 * @see android.os.Vibrator#vibrate(long[],int)
373 */
374 public long[] vibrate;
375
376 /**
377 * The color of the led. The hardware will do its best approximation.
378 *
379 * @see #FLAG_SHOW_LIGHTS
380 * @see #flags
381 */
Tor Norbye80756e32015-03-02 09:39:27 -0800382 @ColorInt
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800383 public int ledARGB;
384
385 /**
386 * The number of milliseconds for the LED to be on while it's flashing.
387 * The hardware will do its best approximation.
388 *
389 * @see #FLAG_SHOW_LIGHTS
390 * @see #flags
391 */
392 public int ledOnMS;
393
394 /**
395 * The number of milliseconds for the LED to be off while it's flashing.
396 * The hardware will do its best approximation.
397 *
398 * @see #FLAG_SHOW_LIGHTS
399 * @see #flags
400 */
401 public int ledOffMS;
402
403 /**
404 * Specifies which values should be taken from the defaults.
405 * <p>
406 * To set, OR the desired from {@link #DEFAULT_SOUND},
407 * {@link #DEFAULT_VIBRATE}, {@link #DEFAULT_LIGHTS}. For all default
408 * values, use {@link #DEFAULT_ALL}.
409 * </p>
410 */
411 public int defaults;
412
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800413 /**
414 * Bit to be bitwise-ored into the {@link #flags} field that should be
415 * set if you want the LED on for this notification.
416 * <ul>
417 * <li>To turn the LED off, pass 0 in the alpha channel for colorARGB
418 * or 0 for both ledOnMS and ledOffMS.</li>
419 * <li>To turn the LED on, pass 1 for ledOnMS and 0 for ledOffMS.</li>
420 * <li>To flash the LED, pass the number of milliseconds that it should
421 * be on and off to ledOnMS and ledOffMS.</li>
422 * </ul>
423 * <p>
424 * Since hardware varies, you are not guaranteed that any of the values
425 * you pass are honored exactly. Use the system defaults (TODO) if possible
426 * because they will be set to values that work on any given hardware.
427 * <p>
428 * The alpha channel must be set for forward compatibility.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500429 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800430 */
431 public static final int FLAG_SHOW_LIGHTS = 0x00000001;
432
433 /**
434 * Bit to be bitwise-ored into the {@link #flags} field that should be
435 * set if this notification is in reference to something that is ongoing,
436 * like a phone call. It should not be set if this notification is in
437 * reference to something that happened at a particular point in time,
438 * like a missed phone call.
439 */
440 public static final int FLAG_ONGOING_EVENT = 0x00000002;
441
442 /**
443 * Bit to be bitwise-ored into the {@link #flags} field that if set,
Scott Mainb8b36452009-04-26 15:50:49 -0700444 * the audio will be repeated until the notification is
445 * cancelled or the notification window is opened.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800446 */
447 public static final int FLAG_INSISTENT = 0x00000004;
448
449 /**
450 * Bit to be bitwise-ored into the {@link #flags} field that should be
Griff Hazen293977b2014-04-28 08:37:20 -0700451 * set if you would only like the sound, vibrate and ticker to be played
452 * if the notification was not already showing.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800453 */
454 public static final int FLAG_ONLY_ALERT_ONCE = 0x00000008;
455
456 /**
457 * Bit to be bitwise-ored into the {@link #flags} field that should be
458 * set if the notification should be canceled when it is clicked by the
Daniel Sandler8aa9ae62012-12-04 23:31:47 -0500459 * user.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800460 */
461 public static final int FLAG_AUTO_CANCEL = 0x00000010;
462
463 /**
464 * Bit to be bitwise-ored into the {@link #flags} field that should be
465 * set if the notification should not be canceled when the user clicks
466 * the Clear all button.
467 */
468 public static final int FLAG_NO_CLEAR = 0x00000020;
469
Dianne Hackbornd8a43f62009-08-17 23:33:56 -0700470 /**
471 * Bit to be bitwise-ored into the {@link #flags} field that should be
472 * set if this notification represents a currently running service. This
473 * will normally be set for you by {@link Service#startForeground}.
474 */
475 public static final int FLAG_FOREGROUND_SERVICE = 0x00000040;
476
Daniel Sandlere46cbd32010-06-17 10:35:26 -0400477 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500478 * Obsolete flag indicating high-priority notifications; use the priority field instead.
Joe Malin8d40d042012-11-05 11:36:40 -0800479 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500480 * @deprecated Use {@link #priority} with a positive value.
Daniel Sandlere46cbd32010-06-17 10:35:26 -0400481 */
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500482 public static final int FLAG_HIGH_PRIORITY = 0x00000080;
Daniel Sandlere46cbd32010-06-17 10:35:26 -0400483
Griff Hazendfcb0802014-02-11 12:00:00 -0800484 /**
485 * Bit to be bitswise-ored into the {@link #flags} field that should be
486 * set if this notification is relevant to the current device only
487 * and it is not recommended that it bridge to other devices.
488 */
489 public static final int FLAG_LOCAL_ONLY = 0x00000100;
490
Griff Hazen5cadc3b2014-05-20 09:55:39 -0700491 /**
492 * Bit to be bitswise-ored into the {@link #flags} field that should be
493 * set if this notification is the group summary for a group of notifications.
494 * Grouped notifications may display in a cluster or stack on devices which
495 * support such rendering. Requires a group key also be set using {@link Builder#setGroup}.
496 */
497 public static final int FLAG_GROUP_SUMMARY = 0x00000200;
498
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800499 public int flags;
500
Tor Norbyed9273d62013-05-30 15:59:53 -0700501 /** @hide */
502 @IntDef({PRIORITY_DEFAULT,PRIORITY_LOW,PRIORITY_MIN,PRIORITY_HIGH,PRIORITY_MAX})
503 @Retention(RetentionPolicy.SOURCE)
504 public @interface Priority {}
505
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800506 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500507 * Default notification {@link #priority}. If your application does not prioritize its own
508 * notifications, use this value for all notifications.
509 */
510 public static final int PRIORITY_DEFAULT = 0;
511
512 /**
513 * Lower {@link #priority}, for items that are less important. The UI may choose to show these
514 * items smaller, or at a different position in the list, compared with your app's
515 * {@link #PRIORITY_DEFAULT} items.
516 */
517 public static final int PRIORITY_LOW = -1;
518
519 /**
520 * Lowest {@link #priority}; these items might not be shown to the user except under special
521 * circumstances, such as detailed notification logs.
522 */
523 public static final int PRIORITY_MIN = -2;
524
525 /**
526 * Higher {@link #priority}, for more important notifications or alerts. The UI may choose to
527 * show these items larger, or at a different position in notification lists, compared with
528 * your app's {@link #PRIORITY_DEFAULT} items.
529 */
530 public static final int PRIORITY_HIGH = 1;
531
532 /**
533 * Highest {@link #priority}, for your application's most important items that require the
534 * user's prompt attention or input.
535 */
536 public static final int PRIORITY_MAX = 2;
537
538 /**
539 * Relative priority for this notification.
Joe Malin8d40d042012-11-05 11:36:40 -0800540 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500541 * Priority is an indication of how much of the user's valuable attention should be consumed by
542 * this notification. Low-priority notifications may be hidden from the user in certain
543 * situations, while the user might be interrupted for a higher-priority notification. The
Daniel Sandler6738eee2012-11-16 12:03:32 -0500544 * system will make a determination about how to interpret this priority when presenting
545 * the notification.
Chris Wren47c20a12014-06-18 17:27:29 -0400546 *
547 * <p>
548 * A notification that is at least {@link #PRIORITY_HIGH} is more likely to be presented
549 * as a heads-up notification.
550 * </p>
551 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500552 */
Tor Norbyed9273d62013-05-30 15:59:53 -0700553 @Priority
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500554 public int priority;
Joe Malin8d40d042012-11-05 11:36:40 -0800555
Dan Sandler26e81cf2014-05-06 10:01:27 -0400556 /**
557 * Accent color (an ARGB integer like the constants in {@link android.graphics.Color})
558 * to be applied by the standard Style templates when presenting this notification.
559 *
560 * The current template design constructs a colorful header image by overlaying the
561 * {@link #icon} image (stenciled in white) atop a field of this color. Alpha components are
562 * ignored.
563 */
Tor Norbye80756e32015-03-02 09:39:27 -0800564 @ColorInt
Dan Sandler26e81cf2014-05-06 10:01:27 -0400565 public int color = COLOR_DEFAULT;
566
567 /**
568 * Special value of {@link #color} telling the system not to decorate this notification with
569 * any special color but instead use default colors when presenting this notification.
570 */
Tor Norbye80756e32015-03-02 09:39:27 -0800571 @ColorInt
Dan Sandler26e81cf2014-05-06 10:01:27 -0400572 public static final int COLOR_DEFAULT = 0; // AKA Color.TRANSPARENT
Dan Sandler0bf2ed82013-12-21 23:33:41 -0600573
574 /**
Adrian Roos4ff3b122016-02-01 12:26:13 -0800575 * Special value of {@link #color} used as a place holder for an invalid color.
576 */
577 @ColorInt
578 private static final int COLOR_INVALID = 1;
579
580 /**
Dan Sandler0bf2ed82013-12-21 23:33:41 -0600581 * Sphere of visibility of this notification, which affects how and when the SystemUI reveals
582 * the notification's presence and contents in untrusted situations (namely, on the secure
583 * lockscreen).
584 *
585 * The default level, {@link #VISIBILITY_PRIVATE}, behaves exactly as notifications have always
586 * done on Android: The notification's {@link #icon} and {@link #tickerText} (if available) are
587 * shown in all situations, but the contents are only available if the device is unlocked for
588 * the appropriate user.
589 *
590 * A more permissive policy can be expressed by {@link #VISIBILITY_PUBLIC}; such a notification
591 * can be read even in an "insecure" context (that is, above a secure lockscreen).
592 * To modify the public version of this notification—for example, to redact some portions—see
593 * {@link Builder#setPublicVersion(Notification)}.
594 *
595 * Finally, a notification can be made {@link #VISIBILITY_SECRET}, which will suppress its icon
596 * and ticker until the user has bypassed the lockscreen.
597 */
598 public int visibility;
599
Griff Hazenfc3922d2014-08-20 11:56:44 -0700600 /**
601 * Notification visibility: Show this notification in its entirety on all lockscreens.
602 *
603 * {@see #visibility}
604 */
Dan Sandler0bf2ed82013-12-21 23:33:41 -0600605 public static final int VISIBILITY_PUBLIC = 1;
Griff Hazenfc3922d2014-08-20 11:56:44 -0700606
607 /**
608 * Notification visibility: Show this notification on all lockscreens, but conceal sensitive or
609 * private information on secure lockscreens.
610 *
611 * {@see #visibility}
612 */
Dan Sandler0bf2ed82013-12-21 23:33:41 -0600613 public static final int VISIBILITY_PRIVATE = 0;
Griff Hazenfc3922d2014-08-20 11:56:44 -0700614
615 /**
616 * Notification visibility: Do not reveal any part of this notification on a secure lockscreen.
617 *
618 * {@see #visibility}
619 */
Dan Sandler0bf2ed82013-12-21 23:33:41 -0600620 public static final int VISIBILITY_SECRET = -1;
621
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500622 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400623 * Notification category: incoming call (voice or video) or similar synchronous communication request.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500624 */
John Spurlockfd7f1e02014-03-18 16:41:57 -0400625 public static final String CATEGORY_CALL = "call";
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500626
627 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400628 * Notification category: incoming direct message (SMS, instant message, etc.).
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500629 */
John Spurlockfd7f1e02014-03-18 16:41:57 -0400630 public static final String CATEGORY_MESSAGE = "msg";
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500631
632 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400633 * Notification category: asynchronous bulk message (email).
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500634 */
John Spurlockfd7f1e02014-03-18 16:41:57 -0400635 public static final String CATEGORY_EMAIL = "email";
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500636
637 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400638 * Notification category: calendar event.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500639 */
John Spurlockfd7f1e02014-03-18 16:41:57 -0400640 public static final String CATEGORY_EVENT = "event";
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500641
642 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400643 * Notification category: promotion or advertisement.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500644 */
John Spurlockfd7f1e02014-03-18 16:41:57 -0400645 public static final String CATEGORY_PROMO = "promo";
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500646
647 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400648 * Notification category: alarm or timer.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500649 */
John Spurlockfd7f1e02014-03-18 16:41:57 -0400650 public static final String CATEGORY_ALARM = "alarm";
651
652 /**
653 * Notification category: progress of a long-running background operation.
654 */
655 public static final String CATEGORY_PROGRESS = "progress";
656
657 /**
658 * Notification category: social network or sharing update.
659 */
660 public static final String CATEGORY_SOCIAL = "social";
661
662 /**
663 * Notification category: error in background operation or authentication status.
664 */
665 public static final String CATEGORY_ERROR = "err";
666
667 /**
668 * Notification category: media transport control for playback.
669 */
670 public static final String CATEGORY_TRANSPORT = "transport";
671
672 /**
673 * Notification category: system or device status update. Reserved for system use.
674 */
675 public static final String CATEGORY_SYSTEM = "sys";
676
677 /**
678 * Notification category: indication of running background service.
679 */
680 public static final String CATEGORY_SERVICE = "service";
681
682 /**
John Spurlock0a69c8c2014-03-21 13:30:57 -0400683 * Notification category: a specific, timely recommendation for a single thing.
684 * For example, a news app might want to recommend a news story it believes the user will
685 * want to read next.
686 */
687 public static final String CATEGORY_RECOMMENDATION = "recommendation";
688
689 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400690 * Notification category: ongoing information about device or contextual status.
691 */
692 public static final String CATEGORY_STATUS = "status";
693
694 /**
John Spurlock24d3dad2015-04-02 12:24:02 -0400695 * Notification category: user-scheduled reminder.
696 */
697 public static final String CATEGORY_REMINDER = "reminder";
698
699 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400700 * One of the predefined notification categories (see the <code>CATEGORY_*</code> constants)
701 * that best describes this Notification. May be used by the system for ranking and filtering.
702 */
703 public String category;
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500704
Griff Hazen5cadc3b2014-05-20 09:55:39 -0700705 private String mGroupKey;
706
707 /**
708 * Get the key used to group this notification into a cluster or stack
709 * with other notifications on devices which support such rendering.
710 */
711 public String getGroup() {
712 return mGroupKey;
713 }
714
715 private String mSortKey;
716
717 /**
718 * Get a sort key that orders this notification among other notifications from the
719 * same package. This can be useful if an external sort was already applied and an app
720 * would like to preserve this. Notifications will be sorted lexicographically using this
721 * value, although providing different priorities in addition to providing sort key may
722 * cause this value to be ignored.
723 *
724 * <p>This sort key can also be used to order members of a notification group. See
725 * {@link Builder#setGroup}.
726 *
727 * @see String#compareTo(String)
728 */
729 public String getSortKey() {
730 return mSortKey;
731 }
732
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500733 /**
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400734 * Additional semantic data to be carried around with this Notification.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400735 * <p>
736 * The extras keys defined here are intended to capture the original inputs to {@link Builder}
737 * APIs, and are intended to be used by
738 * {@link android.service.notification.NotificationListenerService} implementations to extract
739 * detailed information from notification objects.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500740 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400741 public Bundle extras = new Bundle();
742
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400743 /**
744 * {@link #extras} key: this is the title of the notification,
745 * as supplied to {@link Builder#setContentTitle(CharSequence)}.
746 */
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -0500747 public static final String EXTRA_TITLE = "android.title";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400748
749 /**
750 * {@link #extras} key: this is the title of the notification when shown in expanded form,
751 * e.g. as supplied to {@link BigTextStyle#setBigContentTitle(CharSequence)}.
752 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400753 public static final String EXTRA_TITLE_BIG = EXTRA_TITLE + ".big";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400754
755 /**
756 * {@link #extras} key: this is the main text payload, as supplied to
757 * {@link Builder#setContentText(CharSequence)}.
758 */
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -0500759 public static final String EXTRA_TEXT = "android.text";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400760
761 /**
762 * {@link #extras} key: this is a third line of text, as supplied to
763 * {@link Builder#setSubText(CharSequence)}.
764 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400765 public static final String EXTRA_SUB_TEXT = "android.subText";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400766
767 /**
Adrian Roose458aa82015-12-08 16:17:19 -0800768 * {@link #extras} key: this is the remote input history, as supplied to
769 * {@link Builder#setRemoteInputHistory(CharSequence[])}.
770 */
771 public static final String EXTRA_REMOTE_INPUT_HISTORY = "android.remoteInputHistory";
772
773 /**
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400774 * {@link #extras} key: this is a small piece of additional text as supplied to
775 * {@link Builder#setContentInfo(CharSequence)}.
776 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400777 public static final String EXTRA_INFO_TEXT = "android.infoText";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400778
779 /**
780 * {@link #extras} key: this is a line of summary information intended to be shown
781 * alongside expanded notifications, as supplied to (e.g.)
782 * {@link BigTextStyle#setSummaryText(CharSequence)}.
783 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400784 public static final String EXTRA_SUMMARY_TEXT = "android.summaryText";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400785
786 /**
Christoph Studer4600f9b2014-07-22 22:44:43 +0200787 * {@link #extras} key: this is the longer text shown in the big form of a
788 * {@link BigTextStyle} notification, as supplied to
789 * {@link BigTextStyle#bigText(CharSequence)}.
790 */
791 public static final String EXTRA_BIG_TEXT = "android.bigText";
792
793 /**
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400794 * {@link #extras} key: this is the resource ID of the notification's main small icon, as
795 * supplied to {@link Builder#setSmallIcon(int)}.
796 */
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -0500797 public static final String EXTRA_SMALL_ICON = "android.icon";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400798
799 /**
800 * {@link #extras} key: this is a bitmap to be used instead of the small icon when showing the
801 * notification payload, as
802 * supplied to {@link Builder#setLargeIcon(android.graphics.Bitmap)}.
803 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400804 public static final String EXTRA_LARGE_ICON = "android.largeIcon";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400805
806 /**
807 * {@link #extras} key: this is a bitmap to be used instead of the one from
808 * {@link Builder#setLargeIcon(android.graphics.Bitmap)} when the notification is
809 * shown in its expanded form, as supplied to
810 * {@link BigPictureStyle#bigLargeIcon(android.graphics.Bitmap)}.
811 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400812 public static final String EXTRA_LARGE_ICON_BIG = EXTRA_LARGE_ICON + ".big";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400813
814 /**
815 * {@link #extras} key: this is the progress value supplied to
816 * {@link Builder#setProgress(int, int, boolean)}.
817 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400818 public static final String EXTRA_PROGRESS = "android.progress";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400819
820 /**
821 * {@link #extras} key: this is the maximum value supplied to
822 * {@link Builder#setProgress(int, int, boolean)}.
823 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400824 public static final String EXTRA_PROGRESS_MAX = "android.progressMax";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400825
826 /**
827 * {@link #extras} key: whether the progress bar is indeterminate, supplied to
828 * {@link Builder#setProgress(int, int, boolean)}.
829 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400830 public static final String EXTRA_PROGRESS_INDETERMINATE = "android.progressIndeterminate";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400831
832 /**
833 * {@link #extras} key: whether {@link #when} should be shown as a count-up timer (specifically
834 * a {@link android.widget.Chronometer}) instead of a timestamp, as supplied to
835 * {@link Builder#setUsesChronometer(boolean)}.
836 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400837 public static final String EXTRA_SHOW_CHRONOMETER = "android.showChronometer";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400838
839 /**
Selim Cinek81c23aa2016-02-25 16:23:13 -0800840 * {@link #extras} key: whether the chronometer set on the notification should count down
841 * instead of counting up. Is only relevant if key {@link #EXTRA_SHOW_CHRONOMETER} is present.
842 */
843 public static final String EXTRA_CHRONOMETER_COUNTS_DOWN = "android.chronometerCountsDown";
844
845 /**
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400846 * {@link #extras} key: whether {@link #when} should be shown,
847 * as supplied to {@link Builder#setShowWhen(boolean)}.
848 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400849 public static final String EXTRA_SHOW_WHEN = "android.showWhen";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400850
851 /**
852 * {@link #extras} key: this is a bitmap to be shown in {@link BigPictureStyle} expanded
853 * notifications, supplied to {@link BigPictureStyle#bigPicture(android.graphics.Bitmap)}.
854 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400855 public static final String EXTRA_PICTURE = "android.picture";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400856
857 /**
858 * {@link #extras} key: An array of CharSequences to show in {@link InboxStyle} expanded
859 * notifications, each of which was supplied to {@link InboxStyle#addLine(CharSequence)}.
860 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400861 public static final String EXTRA_TEXT_LINES = "android.textLines";
Dan Sandler842dd772014-05-15 09:36:47 -0400862
863 /**
864 * {@link #extras} key: A string representing the name of the specific
865 * {@link android.app.Notification.Style} used to create this notification.
866 */
Chris Wren91ad5632013-06-05 15:05:57 -0400867 public static final String EXTRA_TEMPLATE = "android.template";
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400868
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400869 /**
Chris Wrene6c48932014-09-29 17:19:27 -0400870 * {@link #extras} key: A String array containing the people that this notification relates to,
871 * each of which was supplied to {@link Builder#addPerson(String)}.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400872 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400873 public static final String EXTRA_PEOPLE = "android.people";
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500874
875 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400876 * Allow certain system-generated notifications to appear before the device is provisioned.
877 * Only available to notifications coming from the android package.
878 * @hide
879 */
880 public static final String EXTRA_ALLOW_DURING_SETUP = "android.allowDuringSetup";
881
882 /**
Jose Limae9e3b3b2014-05-18 23:44:50 -0700883 * {@link #extras} key: A
884 * {@link android.content.ContentUris content URI} pointing to an image that can be displayed
885 * in the background when the notification is selected. The URI must point to an image stream
886 * suitable for passing into
887 * {@link android.graphics.BitmapFactory#decodeStream(java.io.InputStream)
888 * BitmapFactory.decodeStream}; all other content types will be ignored. The content provider
889 * URI used for this purpose must require no permissions to read the image data.
890 */
891 public static final String EXTRA_BACKGROUND_IMAGE_URI = "android.backgroundImageUri";
892
893 /**
Dan Sandler842dd772014-05-15 09:36:47 -0400894 * {@link #extras} key: A
Jeff Browndba34ba2014-06-24 20:46:03 -0700895 * {@link android.media.session.MediaSession.Token} associated with a
Dan Sandler842dd772014-05-15 09:36:47 -0400896 * {@link android.app.Notification.MediaStyle} notification.
897 */
898 public static final String EXTRA_MEDIA_SESSION = "android.mediaSession";
899
900 /**
Bryan Mawhinneye191f902014-07-22 12:50:09 +0100901 * {@link #extras} key: the indices of actions to be shown in the compact view,
902 * as supplied to (e.g.) {@link MediaStyle#setShowActionsInCompactView(int...)}.
903 */
904 public static final String EXTRA_COMPACT_ACTIONS = "android.compactActions";
905
Christoph Studer943aa672014-08-03 20:31:16 +0200906 /**
Kenny Guy8942bcd2014-09-08 21:09:47 +0100907 * {@link #extras} key: the user that built the notification.
908 *
909 * @hide
910 */
911 public static final String EXTRA_ORIGINATING_USERID = "android.originatingUserId";
912
913 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400914 * @hide
915 */
916 public static final String EXTRA_BUILDER_APPLICATION_INFO = "android.appInfo";
917
Selim Cinek247fa012016-02-18 09:50:48 -0800918 /**
919 * @hide
920 */
921 public static final String EXTRA_CONTAINS_CUSTOM_VIEW = "android.contains.customView";
922
Dan Sandlerd63f9322015-05-06 15:18:49 -0400923 private Icon mSmallIcon;
924 private Icon mLargeIcon;
925
Chris Wren51c75102013-07-16 20:49:17 -0400926 /**
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400927 * Structure to encapsulate a named action that can be shown as part of this notification.
928 * It must include an icon, a label, and a {@link PendingIntent} to be fired when the action is
929 * selected by the user.
930 * <p>
Griff Hazen959591e2014-05-15 22:26:18 -0700931 * Apps should use {@link Notification.Builder#addAction(int, CharSequence, PendingIntent)}
932 * or {@link Notification.Builder#addAction(Notification.Action)}
933 * to attach actions.
Daniel Sandlera0a938c2012-03-15 08:42:37 -0400934 */
Daniel Sandlerea2a3172013-02-20 22:24:20 -0500935 public static class Action implements Parcelable {
Griff Hazen959591e2014-05-15 22:26:18 -0700936 private final Bundle mExtras;
Dan Sandler86647982015-05-13 23:41:13 -0400937 private Icon mIcon;
Griff Hazen61a9e862014-05-22 16:05:19 -0700938 private final RemoteInput[] mRemoteInputs;
Griff Hazen959591e2014-05-15 22:26:18 -0700939
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400940 /**
941 * Small icon representing the action.
Dan Sandler86647982015-05-13 23:41:13 -0400942 *
943 * @deprecated Use {@link Action#getIcon()} instead.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400944 */
Dan Sandler86647982015-05-13 23:41:13 -0400945 @Deprecated
Daniel Sandlera0a938c2012-03-15 08:42:37 -0400946 public int icon;
Griff Hazen5cadc3b2014-05-20 09:55:39 -0700947
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400948 /**
949 * Title of the action.
950 */
Daniel Sandlera0a938c2012-03-15 08:42:37 -0400951 public CharSequence title;
Griff Hazen5cadc3b2014-05-20 09:55:39 -0700952
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400953 /**
954 * Intent to send when the user invokes this action. May be null, in which case the action
955 * may be rendered in a disabled presentation by the system UI.
956 */
Daniel Sandlera0a938c2012-03-15 08:42:37 -0400957 public PendingIntent actionIntent;
Griff Hazen959591e2014-05-15 22:26:18 -0700958
Daniel Sandlera0a938c2012-03-15 08:42:37 -0400959 private Action(Parcel in) {
Dan Sandler86647982015-05-13 23:41:13 -0400960 if (in.readInt() != 0) {
961 mIcon = Icon.CREATOR.createFromParcel(in);
Dan Sandler68079d52015-07-22 10:45:30 -0400962 if (mIcon.getType() == Icon.TYPE_RESOURCE) {
963 icon = mIcon.getResId();
964 }
Dan Sandler86647982015-05-13 23:41:13 -0400965 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -0400966 title = TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(in);
967 if (in.readInt() == 1) {
968 actionIntent = PendingIntent.CREATOR.createFromParcel(in);
969 }
Jeff Sharkeya04c7a72016-03-18 12:20:36 -0600970 mExtras = Bundle.setDefusable(in.readBundle(), true);
Griff Hazen5cadc3b2014-05-20 09:55:39 -0700971 mRemoteInputs = in.createTypedArray(RemoteInput.CREATOR);
Daniel Sandlera0a938c2012-03-15 08:42:37 -0400972 }
Griff Hazen5cadc3b2014-05-20 09:55:39 -0700973
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400974 /**
Dan Sandler86647982015-05-13 23:41:13 -0400975 * @deprecated Use {@link android.app.Notification.Action.Builder}.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400976 */
Dan Sandler86647982015-05-13 23:41:13 -0400977 @Deprecated
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400978 public Action(int icon, CharSequence title, PendingIntent intent) {
Dan Sandler86647982015-05-13 23:41:13 -0400979 this(Icon.createWithResource("", icon), title, intent, new Bundle(), null);
Griff Hazen959591e2014-05-15 22:26:18 -0700980 }
981
Dan Sandler86647982015-05-13 23:41:13 -0400982 private Action(Icon icon, CharSequence title, PendingIntent intent, Bundle extras,
Griff Hazen5cadc3b2014-05-20 09:55:39 -0700983 RemoteInput[] remoteInputs) {
Dan Sandler86647982015-05-13 23:41:13 -0400984 this.mIcon = icon;
Gus Prevasf5bff46f2015-08-24 10:34:28 -0400985 if (icon != null && icon.getType() == Icon.TYPE_RESOURCE) {
986 this.icon = icon.getResId();
987 }
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400988 this.title = title;
989 this.actionIntent = intent;
Griff Hazen959591e2014-05-15 22:26:18 -0700990 this.mExtras = extras != null ? extras : new Bundle();
Griff Hazen5cadc3b2014-05-20 09:55:39 -0700991 this.mRemoteInputs = remoteInputs;
Griff Hazen959591e2014-05-15 22:26:18 -0700992 }
993
994 /**
Dan Sandler86647982015-05-13 23:41:13 -0400995 * Return an icon representing the action.
996 */
997 public Icon getIcon() {
998 if (mIcon == null && icon != 0) {
999 // you snuck an icon in here without using the builder; let's try to keep it
1000 mIcon = Icon.createWithResource("", icon);
1001 }
1002 return mIcon;
1003 }
1004
1005 /**
Griff Hazen959591e2014-05-15 22:26:18 -07001006 * Get additional metadata carried around with this Action.
1007 */
1008 public Bundle getExtras() {
1009 return mExtras;
1010 }
1011
1012 /**
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001013 * Get the list of inputs to be collected from the user when this action is sent.
1014 * May return null if no remote inputs were added.
1015 */
1016 public RemoteInput[] getRemoteInputs() {
1017 return mRemoteInputs;
1018 }
1019
1020 /**
Griff Hazen959591e2014-05-15 22:26:18 -07001021 * Builder class for {@link Action} objects.
1022 */
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001023 public static final class Builder {
Dan Sandler86647982015-05-13 23:41:13 -04001024 private final Icon mIcon;
Griff Hazen959591e2014-05-15 22:26:18 -07001025 private final CharSequence mTitle;
1026 private final PendingIntent mIntent;
1027 private final Bundle mExtras;
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001028 private ArrayList<RemoteInput> mRemoteInputs;
Griff Hazen959591e2014-05-15 22:26:18 -07001029
1030 /**
1031 * Construct a new builder for {@link Action} object.
1032 * @param icon icon to show for this action
1033 * @param title the title of the action
1034 * @param intent the {@link PendingIntent} to fire when users trigger this action
1035 */
Dan Sandler86647982015-05-13 23:41:13 -04001036 @Deprecated
Griff Hazen959591e2014-05-15 22:26:18 -07001037 public Builder(int icon, CharSequence title, PendingIntent intent) {
Dan Sandler86647982015-05-13 23:41:13 -04001038 this(Icon.createWithResource("", icon), title, intent, new Bundle(), null);
1039 }
1040
1041 /**
1042 * Construct a new builder for {@link Action} object.
1043 * @param icon icon to show for this action
1044 * @param title the title of the action
1045 * @param intent the {@link PendingIntent} to fire when users trigger this action
1046 */
1047 public Builder(Icon icon, CharSequence title, PendingIntent intent) {
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001048 this(icon, title, intent, new Bundle(), null);
Griff Hazen959591e2014-05-15 22:26:18 -07001049 }
1050
1051 /**
1052 * Construct a new builder for {@link Action} object using the fields from an
1053 * {@link Action}.
1054 * @param action the action to read fields from.
1055 */
1056 public Builder(Action action) {
Dan Sandler86647982015-05-13 23:41:13 -04001057 this(action.getIcon(), action.title, action.actionIntent, new Bundle(action.mExtras),
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001058 action.getRemoteInputs());
Griff Hazen959591e2014-05-15 22:26:18 -07001059 }
1060
Dan Sandler86647982015-05-13 23:41:13 -04001061 private Builder(Icon icon, CharSequence title, PendingIntent intent, Bundle extras,
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001062 RemoteInput[] remoteInputs) {
Griff Hazen959591e2014-05-15 22:26:18 -07001063 mIcon = icon;
1064 mTitle = title;
1065 mIntent = intent;
1066 mExtras = extras;
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001067 if (remoteInputs != null) {
1068 mRemoteInputs = new ArrayList<RemoteInput>(remoteInputs.length);
1069 Collections.addAll(mRemoteInputs, remoteInputs);
1070 }
Griff Hazen959591e2014-05-15 22:26:18 -07001071 }
1072
1073 /**
1074 * Merge additional metadata into this builder.
1075 *
1076 * <p>Values within the Bundle will replace existing extras values in this Builder.
1077 *
1078 * @see Notification.Action#extras
1079 */
1080 public Builder addExtras(Bundle extras) {
1081 if (extras != null) {
1082 mExtras.putAll(extras);
1083 }
1084 return this;
1085 }
1086
1087 /**
1088 * Get the metadata Bundle used by this Builder.
1089 *
1090 * <p>The returned Bundle is shared with this Builder.
1091 */
1092 public Bundle getExtras() {
1093 return mExtras;
1094 }
1095
1096 /**
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001097 * Add an input to be collected from the user when this action is sent.
1098 * Response values can be retrieved from the fired intent by using the
1099 * {@link RemoteInput#getResultsFromIntent} function.
1100 * @param remoteInput a {@link RemoteInput} to add to the action
1101 * @return this object for method chaining
1102 */
1103 public Builder addRemoteInput(RemoteInput remoteInput) {
1104 if (mRemoteInputs == null) {
1105 mRemoteInputs = new ArrayList<RemoteInput>();
1106 }
1107 mRemoteInputs.add(remoteInput);
1108 return this;
1109 }
1110
1111 /**
1112 * Apply an extender to this action builder. Extenders may be used to add
1113 * metadata or change options on this builder.
1114 */
Griff Hazen61a9e862014-05-22 16:05:19 -07001115 public Builder extend(Extender extender) {
1116 extender.extend(this);
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001117 return this;
1118 }
1119
1120 /**
Griff Hazen959591e2014-05-15 22:26:18 -07001121 * Combine all of the options that have been set and return a new {@link Action}
1122 * object.
1123 * @return the built action
1124 */
1125 public Action build() {
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001126 RemoteInput[] remoteInputs = mRemoteInputs != null
1127 ? mRemoteInputs.toArray(new RemoteInput[mRemoteInputs.size()]) : null;
1128 return new Action(mIcon, mTitle, mIntent, mExtras, remoteInputs);
Griff Hazen959591e2014-05-15 22:26:18 -07001129 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001130 }
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001131
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001132 @Override
1133 public Action clone() {
1134 return new Action(
Dan Sandler86647982015-05-13 23:41:13 -04001135 getIcon(),
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001136 title,
1137 actionIntent, // safe to alias
1138 new Bundle(mExtras),
1139 getRemoteInputs());
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001140 }
1141 @Override
1142 public int describeContents() {
1143 return 0;
1144 }
1145 @Override
1146 public void writeToParcel(Parcel out, int flags) {
Dan Sandler86647982015-05-13 23:41:13 -04001147 final Icon ic = getIcon();
1148 if (ic != null) {
1149 out.writeInt(1);
1150 ic.writeToParcel(out, 0);
1151 } else {
1152 out.writeInt(0);
1153 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001154 TextUtils.writeToParcel(title, out, flags);
1155 if (actionIntent != null) {
1156 out.writeInt(1);
1157 actionIntent.writeToParcel(out, flags);
1158 } else {
1159 out.writeInt(0);
1160 }
Griff Hazen959591e2014-05-15 22:26:18 -07001161 out.writeBundle(mExtras);
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001162 out.writeTypedArray(mRemoteInputs, flags);
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001163 }
Griff Hazen959591e2014-05-15 22:26:18 -07001164 public static final Parcelable.Creator<Action> CREATOR =
1165 new Parcelable.Creator<Action>() {
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001166 public Action createFromParcel(Parcel in) {
1167 return new Action(in);
1168 }
1169 public Action[] newArray(int size) {
1170 return new Action[size];
1171 }
1172 };
Griff Hazen61a9e862014-05-22 16:05:19 -07001173
1174 /**
1175 * Extender interface for use with {@link Builder#extend}. Extenders may be used to add
1176 * metadata or change options on an action builder.
1177 */
1178 public interface Extender {
1179 /**
1180 * Apply this extender to a notification action builder.
1181 * @param builder the builder to be modified.
1182 * @return the build object for chaining.
1183 */
1184 public Builder extend(Builder builder);
1185 }
1186
1187 /**
1188 * Wearable extender for notification actions. To add extensions to an action,
1189 * create a new {@link android.app.Notification.Action.WearableExtender} object using
1190 * the {@code WearableExtender()} constructor and apply it to a
1191 * {@link android.app.Notification.Action.Builder} using
1192 * {@link android.app.Notification.Action.Builder#extend}.
1193 *
1194 * <pre class="prettyprint">
1195 * Notification.Action action = new Notification.Action.Builder(
1196 * R.drawable.archive_all, "Archive all", actionIntent)
Griff Hazen14f57992014-05-26 09:07:14 -07001197 * .extend(new Notification.Action.WearableExtender()
Griff Hazen61a9e862014-05-22 16:05:19 -07001198 * .setAvailableOffline(false))
Griff Hazen14f57992014-05-26 09:07:14 -07001199 * .build();</pre>
Griff Hazen61a9e862014-05-22 16:05:19 -07001200 */
1201 public static final class WearableExtender implements Extender {
1202 /** Notification action extra which contains wearable extensions */
1203 private static final String EXTRA_WEARABLE_EXTENSIONS = "android.wearable.EXTENSIONS";
1204
Pete Gastaf6781d2014-10-07 15:17:05 -04001205 // Keys within EXTRA_WEARABLE_EXTENSIONS for wearable options.
Griff Hazen61a9e862014-05-22 16:05:19 -07001206 private static final String KEY_FLAGS = "flags";
Pete Gastaf6781d2014-10-07 15:17:05 -04001207 private static final String KEY_IN_PROGRESS_LABEL = "inProgressLabel";
1208 private static final String KEY_CONFIRM_LABEL = "confirmLabel";
1209 private static final String KEY_CANCEL_LABEL = "cancelLabel";
Griff Hazen61a9e862014-05-22 16:05:19 -07001210
1211 // Flags bitwise-ored to mFlags
1212 private static final int FLAG_AVAILABLE_OFFLINE = 0x1;
1213
1214 // Default value for flags integer
1215 private static final int DEFAULT_FLAGS = FLAG_AVAILABLE_OFFLINE;
1216
1217 private int mFlags = DEFAULT_FLAGS;
1218
Pete Gastaf6781d2014-10-07 15:17:05 -04001219 private CharSequence mInProgressLabel;
1220 private CharSequence mConfirmLabel;
1221 private CharSequence mCancelLabel;
1222
Griff Hazen61a9e862014-05-22 16:05:19 -07001223 /**
1224 * Create a {@link android.app.Notification.Action.WearableExtender} with default
1225 * options.
1226 */
1227 public WearableExtender() {
1228 }
1229
1230 /**
1231 * Create a {@link android.app.Notification.Action.WearableExtender} by reading
1232 * wearable options present in an existing notification action.
1233 * @param action the notification action to inspect.
1234 */
1235 public WearableExtender(Action action) {
1236 Bundle wearableBundle = action.getExtras().getBundle(EXTRA_WEARABLE_EXTENSIONS);
1237 if (wearableBundle != null) {
1238 mFlags = wearableBundle.getInt(KEY_FLAGS, DEFAULT_FLAGS);
Pete Gastaf6781d2014-10-07 15:17:05 -04001239 mInProgressLabel = wearableBundle.getCharSequence(KEY_IN_PROGRESS_LABEL);
1240 mConfirmLabel = wearableBundle.getCharSequence(KEY_CONFIRM_LABEL);
1241 mCancelLabel = wearableBundle.getCharSequence(KEY_CANCEL_LABEL);
Griff Hazen61a9e862014-05-22 16:05:19 -07001242 }
1243 }
1244
1245 /**
1246 * Apply wearable extensions to a notification action that is being built. This is
1247 * typically called by the {@link android.app.Notification.Action.Builder#extend}
1248 * method of {@link android.app.Notification.Action.Builder}.
1249 */
1250 @Override
1251 public Action.Builder extend(Action.Builder builder) {
1252 Bundle wearableBundle = new Bundle();
1253
1254 if (mFlags != DEFAULT_FLAGS) {
1255 wearableBundle.putInt(KEY_FLAGS, mFlags);
1256 }
Pete Gastaf6781d2014-10-07 15:17:05 -04001257 if (mInProgressLabel != null) {
1258 wearableBundle.putCharSequence(KEY_IN_PROGRESS_LABEL, mInProgressLabel);
1259 }
1260 if (mConfirmLabel != null) {
1261 wearableBundle.putCharSequence(KEY_CONFIRM_LABEL, mConfirmLabel);
1262 }
1263 if (mCancelLabel != null) {
1264 wearableBundle.putCharSequence(KEY_CANCEL_LABEL, mCancelLabel);
1265 }
Griff Hazen61a9e862014-05-22 16:05:19 -07001266
1267 builder.getExtras().putBundle(EXTRA_WEARABLE_EXTENSIONS, wearableBundle);
1268 return builder;
1269 }
1270
1271 @Override
1272 public WearableExtender clone() {
1273 WearableExtender that = new WearableExtender();
1274 that.mFlags = this.mFlags;
Pete Gastaf6781d2014-10-07 15:17:05 -04001275 that.mInProgressLabel = this.mInProgressLabel;
1276 that.mConfirmLabel = this.mConfirmLabel;
1277 that.mCancelLabel = this.mCancelLabel;
Griff Hazen61a9e862014-05-22 16:05:19 -07001278 return that;
1279 }
1280
1281 /**
1282 * Set whether this action is available when the wearable device is not connected to
1283 * a companion device. The user can still trigger this action when the wearable device is
1284 * offline, but a visual hint will indicate that the action may not be available.
1285 * Defaults to true.
1286 */
1287 public WearableExtender setAvailableOffline(boolean availableOffline) {
1288 setFlag(FLAG_AVAILABLE_OFFLINE, availableOffline);
1289 return this;
1290 }
1291
1292 /**
1293 * Get whether this action is available when the wearable device is not connected to
1294 * a companion device. The user can still trigger this action when the wearable device is
1295 * offline, but a visual hint will indicate that the action may not be available.
1296 * Defaults to true.
1297 */
1298 public boolean isAvailableOffline() {
1299 return (mFlags & FLAG_AVAILABLE_OFFLINE) != 0;
1300 }
1301
1302 private void setFlag(int mask, boolean value) {
1303 if (value) {
1304 mFlags |= mask;
1305 } else {
1306 mFlags &= ~mask;
1307 }
1308 }
Pete Gastaf6781d2014-10-07 15:17:05 -04001309
1310 /**
1311 * Set a label to display while the wearable is preparing to automatically execute the
1312 * action. This is usually a 'ing' verb ending in ellipsis like "Sending..."
1313 *
1314 * @param label the label to display while the action is being prepared to execute
1315 * @return this object for method chaining
1316 */
1317 public WearableExtender setInProgressLabel(CharSequence label) {
1318 mInProgressLabel = label;
1319 return this;
1320 }
1321
1322 /**
1323 * Get the label to display while the wearable is preparing to automatically execute
1324 * the action. This is usually a 'ing' verb ending in ellipsis like "Sending..."
1325 *
1326 * @return the label to display while the action is being prepared to execute
1327 */
1328 public CharSequence getInProgressLabel() {
1329 return mInProgressLabel;
1330 }
1331
1332 /**
1333 * Set a label to display to confirm that the action should be executed.
1334 * This is usually an imperative verb like "Send".
1335 *
1336 * @param label the label to confirm the action should be executed
1337 * @return this object for method chaining
1338 */
1339 public WearableExtender setConfirmLabel(CharSequence label) {
1340 mConfirmLabel = label;
1341 return this;
1342 }
1343
1344 /**
1345 * Get the label to display to confirm that the action should be executed.
1346 * This is usually an imperative verb like "Send".
1347 *
1348 * @return the label to confirm the action should be executed
1349 */
1350 public CharSequence getConfirmLabel() {
1351 return mConfirmLabel;
1352 }
1353
1354 /**
1355 * Set a label to display to cancel the action.
1356 * This is usually an imperative verb, like "Cancel".
1357 *
1358 * @param label the label to display to cancel the action
1359 * @return this object for method chaining
1360 */
1361 public WearableExtender setCancelLabel(CharSequence label) {
1362 mCancelLabel = label;
1363 return this;
1364 }
1365
1366 /**
1367 * Get the label to display to cancel the action.
1368 * This is usually an imperative verb like "Cancel".
1369 *
1370 * @return the label to display to cancel the action
1371 */
1372 public CharSequence getCancelLabel() {
1373 return mCancelLabel;
1374 }
Griff Hazen61a9e862014-05-22 16:05:19 -07001375 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001376 }
1377
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001378 /**
1379 * Array of all {@link Action} structures attached to this notification by
1380 * {@link Builder#addAction(int, CharSequence, PendingIntent)}. Mostly useful for instances of
1381 * {@link android.service.notification.NotificationListenerService} that provide an alternative
1382 * interface for invoking actions.
1383 */
Daniel Sandlerea2a3172013-02-20 22:24:20 -05001384 public Action[] actions;
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001385
1386 /**
Dan Sandler0bf2ed82013-12-21 23:33:41 -06001387 * Replacement version of this notification whose content will be shown
1388 * in an insecure context such as atop a secure keyguard. See {@link #visibility}
1389 * and {@link #VISIBILITY_PUBLIC}.
1390 */
1391 public Notification publicVersion;
1392
1393 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001394 * Constructs a Notification object with default values.
Joe Onorato46439ce2010-11-19 13:56:21 -08001395 * You might want to consider using {@link Builder} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001396 */
1397 public Notification()
1398 {
1399 this.when = System.currentTimeMillis();
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001400 this.priority = PRIORITY_DEFAULT;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001401 }
1402
1403 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001404 * @hide
1405 */
1406 public Notification(Context context, int icon, CharSequence tickerText, long when,
1407 CharSequence contentTitle, CharSequence contentText, Intent contentIntent)
1408 {
Chris Wren1ce4b6d2015-06-11 10:19:43 -04001409 new Builder(context)
1410 .setWhen(when)
1411 .setSmallIcon(icon)
1412 .setTicker(tickerText)
1413 .setContentTitle(contentTitle)
1414 .setContentText(contentText)
1415 .setContentIntent(PendingIntent.getActivity(context, 0, contentIntent, 0))
1416 .buildInto(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001417 }
1418
1419 /**
1420 * Constructs a Notification object with the information needed to
1421 * have a status bar icon without the standard expanded view.
1422 *
1423 * @param icon The resource id of the icon to put in the status bar.
1424 * @param tickerText The text that flows by in the status bar when the notification first
1425 * activates.
1426 * @param when The time to show in the time field. In the System.currentTimeMillis
1427 * timebase.
Joe Onorato46439ce2010-11-19 13:56:21 -08001428 *
1429 * @deprecated Use {@link Builder} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001430 */
Joe Onorato46439ce2010-11-19 13:56:21 -08001431 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001432 public Notification(int icon, CharSequence tickerText, long when)
1433 {
1434 this.icon = icon;
1435 this.tickerText = tickerText;
1436 this.when = when;
1437 }
1438
1439 /**
1440 * Unflatten the notification from a parcel.
1441 */
1442 public Notification(Parcel parcel)
1443 {
1444 int version = parcel.readInt();
1445
1446 when = parcel.readLong();
Dan Sandler3936e7a2015-05-19 20:59:12 -04001447 if (parcel.readInt() != 0) {
1448 mSmallIcon = Icon.CREATOR.createFromParcel(parcel);
Dan Sandler4e787062015-06-17 15:09:48 -04001449 if (mSmallIcon.getType() == Icon.TYPE_RESOURCE) {
1450 icon = mSmallIcon.getResId();
1451 }
Dan Sandler3936e7a2015-05-19 20:59:12 -04001452 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001453 number = parcel.readInt();
1454 if (parcel.readInt() != 0) {
1455 contentIntent = PendingIntent.CREATOR.createFromParcel(parcel);
1456 }
1457 if (parcel.readInt() != 0) {
1458 deleteIntent = PendingIntent.CREATOR.createFromParcel(parcel);
1459 }
1460 if (parcel.readInt() != 0) {
1461 tickerText = TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(parcel);
1462 }
1463 if (parcel.readInt() != 0) {
Joe Onorato46439ce2010-11-19 13:56:21 -08001464 tickerView = RemoteViews.CREATOR.createFromParcel(parcel);
Joe Onoratoef1e7762010-09-17 18:38:38 -04001465 }
1466 if (parcel.readInt() != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001467 contentView = RemoteViews.CREATOR.createFromParcel(parcel);
1468 }
Joe Onorato561d3852010-11-20 18:09:34 -08001469 if (parcel.readInt() != 0) {
Dan Sandlerd63f9322015-05-06 15:18:49 -04001470 mLargeIcon = Icon.CREATOR.createFromParcel(parcel);
Joe Onorato561d3852010-11-20 18:09:34 -08001471 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001472 defaults = parcel.readInt();
1473 flags = parcel.readInt();
1474 if (parcel.readInt() != 0) {
1475 sound = Uri.CREATOR.createFromParcel(parcel);
1476 }
1477
1478 audioStreamType = parcel.readInt();
John Spurlockc0650f022014-07-19 13:22:39 -04001479 if (parcel.readInt() != 0) {
1480 audioAttributes = AudioAttributes.CREATOR.createFromParcel(parcel);
1481 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001482 vibrate = parcel.createLongArray();
1483 ledARGB = parcel.readInt();
1484 ledOnMS = parcel.readInt();
1485 ledOffMS = parcel.readInt();
1486 iconLevel = parcel.readInt();
Daniel Sandlere46cbd32010-06-17 10:35:26 -04001487
1488 if (parcel.readInt() != 0) {
1489 fullScreenIntent = PendingIntent.CREATOR.createFromParcel(parcel);
1490 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001491
1492 priority = parcel.readInt();
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001493
John Spurlockfd7f1e02014-03-18 16:41:57 -04001494 category = parcel.readString();
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001495
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001496 mGroupKey = parcel.readString();
1497
1498 mSortKey = parcel.readString();
1499
Jeff Sharkeya04c7a72016-03-18 12:20:36 -06001500 extras = Bundle.setDefusable(parcel.readBundle(), true); // may be null
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001501
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001502 actions = parcel.createTypedArray(Action.CREATOR); // may be null
1503
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001504 if (parcel.readInt() != 0) {
Daniel Sandlerf3b73432012-03-27 15:01:25 -04001505 bigContentView = RemoteViews.CREATOR.createFromParcel(parcel);
1506 }
Dan Sandler0bf2ed82013-12-21 23:33:41 -06001507
Chris Wren8fd39ec2014-02-27 17:43:26 -05001508 if (parcel.readInt() != 0) {
1509 headsUpContentView = RemoteViews.CREATOR.createFromParcel(parcel);
1510 }
1511
Dan Sandler0bf2ed82013-12-21 23:33:41 -06001512 visibility = parcel.readInt();
1513
1514 if (parcel.readInt() != 0) {
1515 publicVersion = Notification.CREATOR.createFromParcel(parcel);
1516 }
Dan Sandler26e81cf2014-05-06 10:01:27 -04001517
1518 color = parcel.readInt();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001519 }
1520
Andy Stadler110988c2010-12-03 14:29:16 -08001521 @Override
Joe Onorato18e69df2010-05-17 22:26:12 -07001522 public Notification clone() {
1523 Notification that = new Notification();
Daniel Sandler1a497d32013-04-18 14:52:45 -04001524 cloneInto(that, true);
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001525 return that;
1526 }
Joe Onorato18e69df2010-05-17 22:26:12 -07001527
Daniel Sandler1a497d32013-04-18 14:52:45 -04001528 /**
1529 * Copy all (or if heavy is false, all except Bitmaps and RemoteViews) members
1530 * of this into that.
1531 * @hide
1532 */
1533 public void cloneInto(Notification that, boolean heavy) {
Joe Onorato18e69df2010-05-17 22:26:12 -07001534 that.when = this.when;
Dan Sandlerd63f9322015-05-06 15:18:49 -04001535 that.mSmallIcon = this.mSmallIcon;
Joe Onorato18e69df2010-05-17 22:26:12 -07001536 that.number = this.number;
1537
1538 // PendingIntents are global, so there's no reason (or way) to clone them.
1539 that.contentIntent = this.contentIntent;
1540 that.deleteIntent = this.deleteIntent;
Daniel Sandlere46cbd32010-06-17 10:35:26 -04001541 that.fullScreenIntent = this.fullScreenIntent;
Joe Onorato18e69df2010-05-17 22:26:12 -07001542
1543 if (this.tickerText != null) {
1544 that.tickerText = this.tickerText.toString();
1545 }
Daniel Sandler1a497d32013-04-18 14:52:45 -04001546 if (heavy && this.tickerView != null) {
Joe Onorato46439ce2010-11-19 13:56:21 -08001547 that.tickerView = this.tickerView.clone();
Joe Onoratoef1e7762010-09-17 18:38:38 -04001548 }
Daniel Sandler1a497d32013-04-18 14:52:45 -04001549 if (heavy && this.contentView != null) {
Joe Onorato18e69df2010-05-17 22:26:12 -07001550 that.contentView = this.contentView.clone();
1551 }
Dan Sandlerd63f9322015-05-06 15:18:49 -04001552 if (heavy && this.mLargeIcon != null) {
1553 that.mLargeIcon = this.mLargeIcon;
Joe Onorato561d3852010-11-20 18:09:34 -08001554 }
Jozef BABJAKa8b91832011-02-22 08:05:08 +01001555 that.iconLevel = this.iconLevel;
Joe Onorato18e69df2010-05-17 22:26:12 -07001556 that.sound = this.sound; // android.net.Uri is immutable
1557 that.audioStreamType = this.audioStreamType;
John Spurlockc0650f022014-07-19 13:22:39 -04001558 if (this.audioAttributes != null) {
1559 that.audioAttributes = new AudioAttributes.Builder(this.audioAttributes).build();
1560 }
Joe Onorato18e69df2010-05-17 22:26:12 -07001561
1562 final long[] vibrate = this.vibrate;
1563 if (vibrate != null) {
1564 final int N = vibrate.length;
1565 final long[] vib = that.vibrate = new long[N];
1566 System.arraycopy(vibrate, 0, vib, 0, N);
1567 }
1568
1569 that.ledARGB = this.ledARGB;
1570 that.ledOnMS = this.ledOnMS;
1571 that.ledOffMS = this.ledOffMS;
1572 that.defaults = this.defaults;
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001573
Joe Onorato18e69df2010-05-17 22:26:12 -07001574 that.flags = this.flags;
1575
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001576 that.priority = this.priority;
Joe Malin8d40d042012-11-05 11:36:40 -08001577
John Spurlockfd7f1e02014-03-18 16:41:57 -04001578 that.category = this.category;
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001579
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001580 that.mGroupKey = this.mGroupKey;
1581
1582 that.mSortKey = this.mSortKey;
1583
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001584 if (this.extras != null) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04001585 try {
1586 that.extras = new Bundle(this.extras);
1587 // will unparcel
1588 that.extras.size();
1589 } catch (BadParcelableException e) {
1590 Log.e(TAG, "could not unparcel extras from notification: " + this, e);
1591 that.extras = null;
1592 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001593 }
1594
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001595 if (this.actions != null) {
1596 that.actions = new Action[this.actions.length];
1597 for(int i=0; i<this.actions.length; i++) {
1598 that.actions[i] = this.actions[i].clone();
1599 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001600 }
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001601
Daniel Sandler1a497d32013-04-18 14:52:45 -04001602 if (heavy && this.bigContentView != null) {
Daniel Sandlerf3b73432012-03-27 15:01:25 -04001603 that.bigContentView = this.bigContentView.clone();
1604 }
Daniel Sandler1a497d32013-04-18 14:52:45 -04001605
Chris Wren8fd39ec2014-02-27 17:43:26 -05001606 if (heavy && this.headsUpContentView != null) {
1607 that.headsUpContentView = this.headsUpContentView.clone();
1608 }
1609
Dan Sandler0bf2ed82013-12-21 23:33:41 -06001610 that.visibility = this.visibility;
1611
1612 if (this.publicVersion != null) {
1613 that.publicVersion = new Notification();
1614 this.publicVersion.cloneInto(that.publicVersion, heavy);
1615 }
1616
Dan Sandler26e81cf2014-05-06 10:01:27 -04001617 that.color = this.color;
1618
Daniel Sandler1a497d32013-04-18 14:52:45 -04001619 if (!heavy) {
1620 that.lightenPayload(); // will clean out extras
1621 }
1622 }
1623
1624 /**
1625 * Removes heavyweight parts of the Notification object for archival or for sending to
1626 * listeners when the full contents are not necessary.
1627 * @hide
1628 */
1629 public final void lightenPayload() {
1630 tickerView = null;
1631 contentView = null;
1632 bigContentView = null;
Chris Wren8fd39ec2014-02-27 17:43:26 -05001633 headsUpContentView = null;
Dan Sandlerd63f9322015-05-06 15:18:49 -04001634 mLargeIcon = null;
Dan Sandler50128532015-12-08 15:42:41 -05001635 if (extras != null && !extras.isEmpty()) {
1636 final Set<String> keyset = extras.keySet();
1637 final int N = keyset.size();
1638 final String[] keys = keyset.toArray(new String[N]);
1639 for (int i=0; i<N; i++) {
1640 final String key = keys[i];
1641 final Object obj = extras.get(key);
1642 if (obj != null &&
1643 ( obj instanceof Parcelable
1644 || obj instanceof Parcelable[]
1645 || obj instanceof SparseArray
1646 || obj instanceof ArrayList)) {
1647 extras.remove(key);
1648 }
1649 }
Daniel Sandler1a497d32013-04-18 14:52:45 -04001650 }
Joe Onorato18e69df2010-05-17 22:26:12 -07001651 }
1652
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04001653 /**
1654 * Make sure this CharSequence is safe to put into a bundle, which basically
1655 * means it had better not be some custom Parcelable implementation.
1656 * @hide
1657 */
1658 public static CharSequence safeCharSequence(CharSequence cs) {
Christoph Studer535ec612014-09-03 15:47:47 +02001659 if (cs == null) return cs;
1660 if (cs.length() > MAX_CHARSEQUENCE_LENGTH) {
1661 cs = cs.subSequence(0, MAX_CHARSEQUENCE_LENGTH);
1662 }
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04001663 if (cs instanceof Parcelable) {
1664 Log.e(TAG, "warning: " + cs.getClass().getCanonicalName()
1665 + " instance is a custom Parcelable and not allowed in Notification");
1666 return cs.toString();
1667 }
Selim Cinek60a54252016-02-26 17:03:25 -08001668 return removeTextSizeSpans(cs);
1669 }
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04001670
Selim Cinek60a54252016-02-26 17:03:25 -08001671 private static CharSequence removeTextSizeSpans(CharSequence charSequence) {
1672 if (charSequence instanceof Spanned) {
1673 Spanned ss = (Spanned) charSequence;
1674 Object[] spans = ss.getSpans(0, ss.length(), Object.class);
1675 SpannableStringBuilder builder = new SpannableStringBuilder(ss.toString());
1676 for (Object span : spans) {
1677 Object resultSpan = span;
Selim Cinek89991a22016-03-07 19:51:54 -08001678 if (resultSpan instanceof CharacterStyle) {
1679 resultSpan = ((CharacterStyle) span).getUnderlying();
1680 }
1681 if (resultSpan instanceof TextAppearanceSpan) {
1682 TextAppearanceSpan originalSpan = (TextAppearanceSpan) resultSpan;
Selim Cinek60a54252016-02-26 17:03:25 -08001683 resultSpan = new TextAppearanceSpan(
1684 originalSpan.getFamily(),
1685 originalSpan.getTextStyle(),
1686 -1,
1687 originalSpan.getTextColor(),
1688 originalSpan.getLinkTextColor());
Selim Cinek89991a22016-03-07 19:51:54 -08001689 } else if (resultSpan instanceof RelativeSizeSpan
1690 || resultSpan instanceof AbsoluteSizeSpan) {
Selim Cinek60a54252016-02-26 17:03:25 -08001691 continue;
Selim Cinek89991a22016-03-07 19:51:54 -08001692 } else {
1693 resultSpan = span;
Selim Cinek60a54252016-02-26 17:03:25 -08001694 }
1695 builder.setSpan(resultSpan, ss.getSpanStart(span), ss.getSpanEnd(span),
1696 ss.getSpanFlags(span));
1697 }
1698 return builder;
1699 }
1700 return charSequence;
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04001701 }
1702
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001703 public int describeContents() {
1704 return 0;
1705 }
1706
1707 /**
Dan Sandler4e787062015-06-17 15:09:48 -04001708 * Flatten this notification into a parcel.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001709 */
1710 public void writeToParcel(Parcel parcel, int flags)
1711 {
1712 parcel.writeInt(1);
1713
1714 parcel.writeLong(when);
Dan Sandler4e787062015-06-17 15:09:48 -04001715 if (mSmallIcon == null && icon != 0) {
1716 // you snuck an icon in here without using the builder; let's try to keep it
1717 mSmallIcon = Icon.createWithResource("", icon);
1718 }
Dan Sandler3936e7a2015-05-19 20:59:12 -04001719 if (mSmallIcon != null) {
1720 parcel.writeInt(1);
1721 mSmallIcon.writeToParcel(parcel, 0);
1722 } else {
1723 parcel.writeInt(0);
1724 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001725 parcel.writeInt(number);
1726 if (contentIntent != null) {
1727 parcel.writeInt(1);
1728 contentIntent.writeToParcel(parcel, 0);
1729 } else {
1730 parcel.writeInt(0);
1731 }
1732 if (deleteIntent != null) {
1733 parcel.writeInt(1);
1734 deleteIntent.writeToParcel(parcel, 0);
1735 } else {
1736 parcel.writeInt(0);
1737 }
1738 if (tickerText != null) {
1739 parcel.writeInt(1);
1740 TextUtils.writeToParcel(tickerText, parcel, flags);
1741 } else {
1742 parcel.writeInt(0);
1743 }
Joe Onorato46439ce2010-11-19 13:56:21 -08001744 if (tickerView != null) {
Joe Onoratoef1e7762010-09-17 18:38:38 -04001745 parcel.writeInt(1);
Joe Onorato46439ce2010-11-19 13:56:21 -08001746 tickerView.writeToParcel(parcel, 0);
Joe Onoratoef1e7762010-09-17 18:38:38 -04001747 } else {
1748 parcel.writeInt(0);
1749 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001750 if (contentView != null) {
1751 parcel.writeInt(1);
1752 contentView.writeToParcel(parcel, 0);
1753 } else {
1754 parcel.writeInt(0);
1755 }
Dan Sandlerd63f9322015-05-06 15:18:49 -04001756 if (mLargeIcon != null) {
Joe Onorato561d3852010-11-20 18:09:34 -08001757 parcel.writeInt(1);
Dan Sandlerd63f9322015-05-06 15:18:49 -04001758 mLargeIcon.writeToParcel(parcel, 0);
Joe Onorato561d3852010-11-20 18:09:34 -08001759 } else {
1760 parcel.writeInt(0);
1761 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001762
1763 parcel.writeInt(defaults);
1764 parcel.writeInt(this.flags);
1765
1766 if (sound != null) {
1767 parcel.writeInt(1);
1768 sound.writeToParcel(parcel, 0);
1769 } else {
1770 parcel.writeInt(0);
1771 }
1772 parcel.writeInt(audioStreamType);
John Spurlockc0650f022014-07-19 13:22:39 -04001773
1774 if (audioAttributes != null) {
1775 parcel.writeInt(1);
1776 audioAttributes.writeToParcel(parcel, 0);
1777 } else {
1778 parcel.writeInt(0);
1779 }
1780
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001781 parcel.writeLongArray(vibrate);
1782 parcel.writeInt(ledARGB);
1783 parcel.writeInt(ledOnMS);
1784 parcel.writeInt(ledOffMS);
1785 parcel.writeInt(iconLevel);
Daniel Sandlere46cbd32010-06-17 10:35:26 -04001786
1787 if (fullScreenIntent != null) {
1788 parcel.writeInt(1);
1789 fullScreenIntent.writeToParcel(parcel, 0);
1790 } else {
1791 parcel.writeInt(0);
1792 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001793
1794 parcel.writeInt(priority);
Joe Malin8d40d042012-11-05 11:36:40 -08001795
John Spurlockfd7f1e02014-03-18 16:41:57 -04001796 parcel.writeString(category);
Joe Malin8d40d042012-11-05 11:36:40 -08001797
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001798 parcel.writeString(mGroupKey);
1799
1800 parcel.writeString(mSortKey);
1801
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001802 parcel.writeBundle(extras); // null ok
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001803
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001804 parcel.writeTypedArray(actions, 0); // null ok
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001805
Daniel Sandlerf3b73432012-03-27 15:01:25 -04001806 if (bigContentView != null) {
1807 parcel.writeInt(1);
1808 bigContentView.writeToParcel(parcel, 0);
1809 } else {
1810 parcel.writeInt(0);
1811 }
Dan Sandler0bf2ed82013-12-21 23:33:41 -06001812
Chris Wren8fd39ec2014-02-27 17:43:26 -05001813 if (headsUpContentView != null) {
1814 parcel.writeInt(1);
1815 headsUpContentView.writeToParcel(parcel, 0);
1816 } else {
1817 parcel.writeInt(0);
1818 }
1819
Dan Sandler0bf2ed82013-12-21 23:33:41 -06001820 parcel.writeInt(visibility);
1821
1822 if (publicVersion != null) {
1823 parcel.writeInt(1);
1824 publicVersion.writeToParcel(parcel, 0);
1825 } else {
1826 parcel.writeInt(0);
1827 }
Dan Sandler26e81cf2014-05-06 10:01:27 -04001828
1829 parcel.writeInt(color);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001830 }
1831
1832 /**
1833 * Parcelable.Creator that instantiates Notification objects
1834 */
1835 public static final Parcelable.Creator<Notification> CREATOR
1836 = new Parcelable.Creator<Notification>()
1837 {
1838 public Notification createFromParcel(Parcel parcel)
1839 {
1840 return new Notification(parcel);
1841 }
1842
1843 public Notification[] newArray(int size)
1844 {
1845 return new Notification[size];
1846 }
1847 };
1848
1849 /**
1850 * Sets the {@link #contentView} field to be a view with the standard "Latest Event"
1851 * layout.
1852 *
1853 * <p>Uses the {@link #icon} and {@link #when} fields to set the icon and time fields
1854 * in the view.</p>
1855 * @param context The context for your application / activity.
1856 * @param contentTitle The title that goes in the expanded entry.
1857 * @param contentText The text that goes in the expanded entry.
1858 * @param contentIntent The intent to launch when the user clicks the expanded notification.
1859 * If this is an activity, it must include the
1860 * {@link android.content.Intent#FLAG_ACTIVITY_NEW_TASK} flag, which requires
Scott Main7aee61f2011-02-08 11:25:01 -08001861 * that you take care of task management as described in the
1862 * <a href="{@docRoot}guide/topics/fundamentals/tasks-and-back-stack.html">Tasks and Back
1863 * Stack</a> document.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001864 *
Joe Onorato46439ce2010-11-19 13:56:21 -08001865 * @deprecated Use {@link Builder} instead.
Chris Wrena05db382015-06-24 15:18:34 -04001866 * @removed
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001867 */
Joe Onorato46439ce2010-11-19 13:56:21 -08001868 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001869 public void setLatestEventInfo(Context context,
1870 CharSequence contentTitle, CharSequence contentText, PendingIntent contentIntent) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04001871 if (context.getApplicationInfo().targetSdkVersion > Build.VERSION_CODES.LOLLIPOP_MR1){
1872 Log.e(TAG, "setLatestEventInfo() is deprecated and you should feel deprecated.",
1873 new Throwable());
1874 }
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001875
Julia Reynoldsd9228f12015-10-20 10:37:27 -04001876 // ensure that any information already set directly is preserved
1877 final Notification.Builder builder = new Notification.Builder(context, this);
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001878
1879 // now apply the latestEventInfo fields
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001880 if (contentTitle != null) {
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001881 builder.setContentTitle(contentTitle);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001882 }
1883 if (contentText != null) {
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001884 builder.setContentText(contentText);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001885 }
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001886 builder.setContentIntent(contentIntent);
Julia Reynoldsd9228f12015-10-20 10:37:27 -04001887
1888 builder.build(); // callers expect this notification to be ready to use
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001889 }
1890
Julia Reynoldsda303542015-11-23 14:00:20 -05001891 /**
1892 * @hide
1893 */
1894 public static void addFieldsFromContext(Context context, Notification notification) {
1895 if (notification.extras.getParcelable(EXTRA_BUILDER_APPLICATION_INFO) == null) {
1896 notification.extras.putParcelable(EXTRA_BUILDER_APPLICATION_INFO,
1897 context.getApplicationInfo());
1898 }
1899 if (!notification.extras.containsKey(EXTRA_ORIGINATING_USERID)) {
1900 notification.extras.putInt(EXTRA_ORIGINATING_USERID, context.getUserId());
1901 }
1902 }
1903
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001904 @Override
1905 public String toString() {
1906 StringBuilder sb = new StringBuilder();
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001907 sb.append("Notification(pri=");
1908 sb.append(priority);
1909 sb.append(" contentView=");
Joe Onoratoc9596d62011-01-12 17:03:11 -08001910 if (contentView != null) {
1911 sb.append(contentView.getPackage());
1912 sb.append("/0x");
1913 sb.append(Integer.toHexString(contentView.getLayoutId()));
1914 } else {
1915 sb.append("null");
1916 }
1917 sb.append(" vibrate=");
Daniel Sandler6738eee2012-11-16 12:03:32 -05001918 if ((this.defaults & DEFAULT_VIBRATE) != 0) {
1919 sb.append("default");
1920 } else if (this.vibrate != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001921 int N = this.vibrate.length-1;
1922 sb.append("[");
1923 for (int i=0; i<N; i++) {
1924 sb.append(this.vibrate[i]);
1925 sb.append(',');
1926 }
Simon Schoar8cf97d92009-06-10 22:08:37 +02001927 if (N != -1) {
1928 sb.append(this.vibrate[N]);
1929 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001930 sb.append("]");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001931 } else {
1932 sb.append("null");
1933 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001934 sb.append(" sound=");
Daniel Sandler6738eee2012-11-16 12:03:32 -05001935 if ((this.defaults & DEFAULT_SOUND) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001936 sb.append("default");
Daniel Sandler6738eee2012-11-16 12:03:32 -05001937 } else if (this.sound != null) {
1938 sb.append(this.sound.toString());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001939 } else {
1940 sb.append("null");
1941 }
Chris Wren365b6d32015-07-16 10:39:26 -04001942 if (this.tickerText != null) {
1943 sb.append(" tick");
1944 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001945 sb.append(" defaults=0x");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001946 sb.append(Integer.toHexString(this.defaults));
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001947 sb.append(" flags=0x");
Daniel Sandlere46cbd32010-06-17 10:35:26 -04001948 sb.append(Integer.toHexString(this.flags));
Dan Sandler26e81cf2014-05-06 10:01:27 -04001949 sb.append(String.format(" color=0x%08x", this.color));
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001950 if (this.category != null) {
1951 sb.append(" category=");
1952 sb.append(this.category);
1953 }
1954 if (this.mGroupKey != null) {
1955 sb.append(" groupKey=");
1956 sb.append(this.mGroupKey);
1957 }
1958 if (this.mSortKey != null) {
1959 sb.append(" sortKey=");
1960 sb.append(this.mSortKey);
1961 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001962 if (actions != null) {
Dan Sandler1b718782014-07-18 12:43:45 -04001963 sb.append(" actions=");
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001964 sb.append(actions.length);
Dan Sandler1b718782014-07-18 12:43:45 -04001965 }
1966 sb.append(" vis=");
1967 sb.append(visibilityToString(this.visibility));
1968 if (this.publicVersion != null) {
1969 sb.append(" publicVersion=");
1970 sb.append(publicVersion.toString());
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001971 }
1972 sb.append(")");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001973 return sb.toString();
1974 }
Joe Onorato46439ce2010-11-19 13:56:21 -08001975
Dan Sandler1b718782014-07-18 12:43:45 -04001976 /**
1977 * {@hide}
1978 */
1979 public static String visibilityToString(int vis) {
1980 switch (vis) {
1981 case VISIBILITY_PRIVATE:
1982 return "PRIVATE";
1983 case VISIBILITY_PUBLIC:
1984 return "PUBLIC";
1985 case VISIBILITY_SECRET:
1986 return "SECRET";
1987 default:
1988 return "UNKNOWN(" + String.valueOf(vis) + ")";
1989 }
1990 }
1991
Joe Onoratocb109a02011-01-18 17:57:41 -08001992 /**
John Spurlock1d881a12015-03-18 19:21:54 -04001993 * {@hide}
1994 */
1995 public static String priorityToString(@Priority int pri) {
1996 switch (pri) {
1997 case PRIORITY_MIN:
1998 return "MIN";
1999 case PRIORITY_LOW:
2000 return "LOW";
2001 case PRIORITY_DEFAULT:
2002 return "DEFAULT";
2003 case PRIORITY_HIGH:
2004 return "HIGH";
2005 case PRIORITY_MAX:
2006 return "MAX";
2007 default:
2008 return "UNKNOWN(" + String.valueOf(pri) + ")";
2009 }
2010 }
2011
2012 /**
Dan Sandlerd63f9322015-05-06 15:18:49 -04002013 * The small icon representing this notification in the status bar and content view.
2014 *
2015 * @return the small icon representing this notification.
2016 *
2017 * @see Builder#getSmallIcon()
2018 * @see Builder#setSmallIcon(Icon)
2019 */
2020 public Icon getSmallIcon() {
2021 return mSmallIcon;
2022 }
2023
2024 /**
2025 * Used when notifying to clean up legacy small icons.
2026 * @hide
2027 */
2028 public void setSmallIcon(Icon icon) {
2029 mSmallIcon = icon;
2030 }
2031
2032 /**
2033 * The large icon shown in this notification's content view.
2034 * @see Builder#getLargeIcon()
2035 * @see Builder#setLargeIcon(Icon)
2036 */
2037 public Icon getLargeIcon() {
2038 return mLargeIcon;
2039 }
2040
2041 /**
Christoph Studer4600f9b2014-07-22 22:44:43 +02002042 * @hide
2043 */
Christoph Studerc8db24b2014-07-25 17:50:30 +02002044 public boolean isGroupSummary() {
2045 return mGroupKey != null && (flags & FLAG_GROUP_SUMMARY) != 0;
2046 }
2047
2048 /**
2049 * @hide
2050 */
2051 public boolean isGroupChild() {
2052 return mGroupKey != null && (flags & FLAG_GROUP_SUMMARY) == 0;
2053 }
2054
2055 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002056 * Builder class for {@link Notification} objects.
Joe Malin8d40d042012-11-05 11:36:40 -08002057 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002058 * Provides a convenient way to set the various fields of a {@link Notification} and generate
Scott Main183bf112012-08-13 19:12:13 -07002059 * content views using the platform's notification layout template. If your app supports
2060 * versions of Android as old as API level 4, you can instead use
2061 * {@link android.support.v4.app.NotificationCompat.Builder NotificationCompat.Builder},
2062 * available in the <a href="{@docRoot}tools/extras/support-library.html">Android Support
2063 * library</a>.
Joe Malin8d40d042012-11-05 11:36:40 -08002064 *
Scott Main183bf112012-08-13 19:12:13 -07002065 * <p>Example:
Joe Malin8d40d042012-11-05 11:36:40 -08002066 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002067 * <pre class="prettyprint">
Scott Main183bf112012-08-13 19:12:13 -07002068 * Notification noti = new Notification.Builder(mContext)
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002069 * .setContentTitle(&quot;New mail from &quot; + sender.toString())
2070 * .setContentText(subject)
2071 * .setSmallIcon(R.drawable.new_mail)
2072 * .setLargeIcon(aBitmap)
Chris Wrenfbd96ba2012-05-01 12:03:58 -04002073 * .build();
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002074 * </pre>
Joe Onoratocb109a02011-01-18 17:57:41 -08002075 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002076 public static class Builder {
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05002077 /**
2078 * @hide
2079 */
2080 public static final String EXTRA_REBUILD_CONTENT_VIEW_ACTION_COUNT =
2081 "android.rebuild.contentViewActionCount";
2082 /**
2083 * @hide
2084 */
2085 public static final String EXTRA_REBUILD_BIG_CONTENT_VIEW_ACTION_COUNT
2086 = "android.rebuild.bigViewActionCount";
2087 /**
2088 * @hide
2089 */
2090 public static final String EXTRA_REBUILD_HEADS_UP_CONTENT_VIEW_ACTION_COUNT
2091 = "android.rebuild.hudViewActionCount";
2092
Daniel Sandler602ad1c2012-06-12 16:06:27 -04002093 private static final int MAX_ACTION_BUTTONS = 3;
Daniel Sandler8680bf82012-05-15 16:52:52 -04002094
Joe Onorato46439ce2010-11-19 13:56:21 -08002095 private Context mContext;
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002096 private Notification mN;
2097 private Bundle mUserExtras = new Bundle();
Chris Wrenfbd96ba2012-05-01 12:03:58 -04002098 private Style mStyle;
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002099 private ArrayList<Action> mActions = new ArrayList<Action>(MAX_ACTION_BUTTONS);
2100 private ArrayList<String> mPersonList = new ArrayList<String>();
2101 private NotificationColorUtil mColorUtil;
2102 private boolean mColorUtilInited = false;
Christoph Studer7ac80e62014-08-04 16:01:57 +02002103
2104 /**
Adrian Roos4ff3b122016-02-01 12:26:13 -08002105 * Caches a contrast-enhanced version of {@link #mCachedContrastColorIsFor}.
2106 */
2107 private int mCachedContrastColor = COLOR_INVALID;
2108 private int mCachedContrastColorIsFor = COLOR_INVALID;
2109
2110 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002111 * Constructs a new Builder with the defaults:
Joe Onoratocb109a02011-01-18 17:57:41 -08002112 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002113
2114 * <table>
2115 * <tr><th align=right>priority</th>
2116 * <td>{@link #PRIORITY_DEFAULT}</td></tr>
2117 * <tr><th align=right>when</th>
2118 * <td>now ({@link System#currentTimeMillis()})</td></tr>
2119 * <tr><th align=right>audio stream</th>
2120 * <td>{@link #STREAM_DEFAULT}</td></tr>
2121 * </table>
Joe Onoratocb109a02011-01-18 17:57:41 -08002122 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002123
2124 * @param context
2125 * A {@link Context} that will be used by the Builder to construct the
2126 * RemoteViews. The Context will not be held past the lifetime of this Builder
2127 * object.
Joe Onoratocb109a02011-01-18 17:57:41 -08002128 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002129 public Builder(Context context) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002130 this(context, null);
Joe Onorato46439ce2010-11-19 13:56:21 -08002131 }
2132
Joe Onoratocb109a02011-01-18 17:57:41 -08002133 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002134 * @hide
Christoph Studer4600f9b2014-07-22 22:44:43 +02002135 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002136 public Builder(Context context, Notification toAdopt) {
2137 mContext = context;
Christoph Studer4600f9b2014-07-22 22:44:43 +02002138
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002139 if (toAdopt == null) {
2140 mN = new Notification();
Selim Cinek0ff1ce602016-04-05 18:27:16 -07002141 if (context.getApplicationInfo().targetSdkVersion < Build.VERSION_CODES.N) {
2142 mN.extras.putBoolean(EXTRA_SHOW_WHEN, true);
2143 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002144 mN.priority = PRIORITY_DEFAULT;
2145 mN.visibility = VISIBILITY_PRIVATE;
2146 } else {
2147 mN = toAdopt;
2148 if (mN.actions != null) {
2149 Collections.addAll(mActions, mN.actions);
Christoph Studer4600f9b2014-07-22 22:44:43 +02002150 }
2151
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002152 if (mN.extras.containsKey(EXTRA_PEOPLE)) {
2153 Collections.addAll(mPersonList, mN.extras.getStringArray(EXTRA_PEOPLE));
2154 }
2155
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002156 String templateClass = mN.extras.getString(EXTRA_TEMPLATE);
2157 if (!TextUtils.isEmpty(templateClass)) {
2158 final Class<? extends Style> styleClass
2159 = getNotificationStyleClass(templateClass);
2160 if (styleClass == null) {
2161 Log.d(TAG, "Unknown style class: " + templateClass);
2162 } else {
2163 try {
2164 final Constructor<? extends Style> ctor = styleClass.getConstructor();
2165 ctor.setAccessible(true);
2166 final Style style = ctor.newInstance();
2167 style.restoreFromExtras(mN.extras);
2168
2169 if (style != null) {
2170 setStyle(style);
2171 }
2172 } catch (Throwable t) {
2173 Log.e(TAG, "Could not create Style", t);
2174 }
2175 }
2176 }
2177
2178 }
2179 }
2180
2181 private NotificationColorUtil getColorUtil() {
2182 if (!mColorUtilInited) {
2183 mColorUtilInited = true;
2184 if (mContext.getApplicationInfo().targetSdkVersion < Build.VERSION_CODES.LOLLIPOP) {
2185 mColorUtil = NotificationColorUtil.getInstance(mContext);
Christoph Studer4600f9b2014-07-22 22:44:43 +02002186 }
2187 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002188 return mColorUtil;
Christoph Studer4600f9b2014-07-22 22:44:43 +02002189 }
2190
2191 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002192 * Add a timestamp pertaining to the notification (usually the time the event occurred).
Selim Cinek0ff1ce602016-04-05 18:27:16 -07002193 *
2194 * For apps targeting {@link android.os.Build.VERSION_CODES#N} and above, this time is not
2195 * shown anymore by default and must be opted into by using
2196 * {@link android.app.Notification.Builder#setShowWhen(boolean)}
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002197 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002198 * @see Notification#when
Joe Onoratocb109a02011-01-18 17:57:41 -08002199 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002200 public Builder setWhen(long when) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002201 mN.when = when;
Joe Onorato46439ce2010-11-19 13:56:21 -08002202 return this;
2203 }
2204
Joe Onoratocb109a02011-01-18 17:57:41 -08002205 /**
Griff Hazen50c11652014-05-16 09:46:31 -07002206 * Control whether the timestamp set with {@link #setWhen(long) setWhen} is shown
Daniel Sandler0c890492012-09-12 17:23:10 -07002207 * in the content view.
Selim Cinek0ff1ce602016-04-05 18:27:16 -07002208 * For apps targeting {@link android.os.Build.VERSION_CODES#N} and above, this defaults to
2209 * {@code false}. For earlier apps, the default is {@code true}.
Daniel Sandler0c890492012-09-12 17:23:10 -07002210 */
2211 public Builder setShowWhen(boolean show) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002212 mN.extras.putBoolean(EXTRA_SHOW_WHEN, show);
Daniel Sandler0c890492012-09-12 17:23:10 -07002213 return this;
2214 }
2215
2216 /**
Daniel Sandlerd33b8032012-05-10 11:41:48 -04002217 * Show the {@link Notification#when} field as a stopwatch.
Joe Malin8d40d042012-11-05 11:36:40 -08002218 *
2219 * Instead of presenting <code>when</code> as a timestamp, the notification will show an
Daniel Sandlerd33b8032012-05-10 11:41:48 -04002220 * automatically updating display of the minutes and seconds since <code>when</code>.
Daniel Sandlera2985ed2012-04-03 16:42:00 -04002221 *
Daniel Sandlerd33b8032012-05-10 11:41:48 -04002222 * Useful when showing an elapsed time (like an ongoing phone call).
2223 *
Selim Cinek81c23aa2016-02-25 16:23:13 -08002224 * The counter can also be set to count down to <code>when</code> when using
2225 * {@link #setChronometerCountsDown(boolean)}.
2226 *
Daniel Sandlerd33b8032012-05-10 11:41:48 -04002227 * @see android.widget.Chronometer
Daniel Sandlera2985ed2012-04-03 16:42:00 -04002228 * @see Notification#when
Selim Cinek81c23aa2016-02-25 16:23:13 -08002229 * @see #setChronometerCountsDown(boolean)
Daniel Sandlera2985ed2012-04-03 16:42:00 -04002230 */
2231 public Builder setUsesChronometer(boolean b) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002232 mN.extras.putBoolean(EXTRA_SHOW_CHRONOMETER, b);
Daniel Sandlera2985ed2012-04-03 16:42:00 -04002233 return this;
2234 }
2235
2236 /**
Selim Cinek81c23aa2016-02-25 16:23:13 -08002237 * Sets the Chronometer to count down instead of counting up.
2238 *
2239 * <p>This is only relevant if {@link #setUsesChronometer(boolean)} has been set to true.
2240 * If it isn't set the chronometer will count up.
2241 *
2242 * @see #setUsesChronometer(boolean)
2243 */
2244 public Builder setChronometerCountsDown(boolean countsDown) {
2245 mN.extras.putBoolean(EXTRA_CHRONOMETER_COUNTS_DOWN, countsDown);
2246 return this;
2247 }
2248
2249 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002250 * Set the small icon resource, which will be used to represent the notification in the
2251 * status bar.
Joe Onoratocb109a02011-01-18 17:57:41 -08002252 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002253
2254 * The platform template for the expanded view will draw this icon in the left, unless a
2255 * {@link #setLargeIcon(Bitmap) large icon} has also been specified, in which case the small
2256 * icon will be moved to the right-hand side.
2257 *
2258
2259 * @param icon
2260 * A resource ID in the application's package of the drawable to use.
2261 * @see Notification#icon
Joe Onoratocb109a02011-01-18 17:57:41 -08002262 */
Tor Norbye7b9c9122013-05-30 16:48:33 -07002263 public Builder setSmallIcon(@DrawableRes int icon) {
Dan Sandlerd63f9322015-05-06 15:18:49 -04002264 return setSmallIcon(icon != 0
2265 ? Icon.createWithResource(mContext, icon)
2266 : null);
Joe Onorato46439ce2010-11-19 13:56:21 -08002267 }
2268
Joe Onoratocb109a02011-01-18 17:57:41 -08002269 /**
2270 * A variant of {@link #setSmallIcon(int) setSmallIcon(int)} that takes an additional
2271 * level parameter for when the icon is a {@link android.graphics.drawable.LevelListDrawable
2272 * LevelListDrawable}.
2273 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002274 * @param icon A resource ID in the application's package of the drawable to use.
Joe Onoratocb109a02011-01-18 17:57:41 -08002275 * @param level The level to use for the icon.
2276 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002277 * @see Notification#icon
2278 * @see Notification#iconLevel
Joe Onoratocb109a02011-01-18 17:57:41 -08002279 */
Tor Norbye7b9c9122013-05-30 16:48:33 -07002280 public Builder setSmallIcon(@DrawableRes int icon, int level) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002281 mN.iconLevel = level;
Dan Sandlerd63f9322015-05-06 15:18:49 -04002282 return setSmallIcon(icon);
2283 }
2284
2285 /**
2286 * Set the small icon, which will be used to represent the notification in the
2287 * status bar and content view (unless overriden there by a
2288 * {@link #setLargeIcon(Bitmap) large icon}).
2289 *
2290 * @param icon An Icon object to use.
2291 * @see Notification#icon
2292 */
2293 public Builder setSmallIcon(Icon icon) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002294 mN.setSmallIcon(icon);
2295 if (icon != null && icon.getType() == Icon.TYPE_RESOURCE) {
2296 mN.icon = icon.getResId();
2297 }
Joe Onorato46439ce2010-11-19 13:56:21 -08002298 return this;
2299 }
2300
Joe Onoratocb109a02011-01-18 17:57:41 -08002301 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002302 * Set the first line of text in the platform notification template.
Joe Onoratocb109a02011-01-18 17:57:41 -08002303 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002304 public Builder setContentTitle(CharSequence title) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002305 mN.extras.putCharSequence(EXTRA_TITLE, safeCharSequence(title));
Joe Onorato46439ce2010-11-19 13:56:21 -08002306 return this;
2307 }
2308
Joe Onoratocb109a02011-01-18 17:57:41 -08002309 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002310 * Set the second line of text in the platform notification template.
Joe Onoratocb109a02011-01-18 17:57:41 -08002311 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002312 public Builder setContentText(CharSequence text) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002313 mN.extras.putCharSequence(EXTRA_TEXT, safeCharSequence(text));
Joe Onorato46439ce2010-11-19 13:56:21 -08002314 return this;
2315 }
2316
Joe Onoratocb109a02011-01-18 17:57:41 -08002317 /**
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07002318 * This provides some additional information that is displayed in the notification. No
2319 * guarantees are given where exactly it is displayed.
2320 *
2321 * <p>This information should only be provided if it provides an essential
2322 * benefit to the understanding of the notification. The more text you provide the
2323 * less readable it becomes. For example, an email client should only provide the account
2324 * name here if more than one email account has been added.</p>
2325 *
2326 * <p>As of {@link android.os.Build.VERSION_CODES#N} this information is displayed in the
2327 * notification header area.
2328 *
2329 * On Android versions before {@link android.os.Build.VERSION_CODES#N}
2330 * this will be shown in the third line of text in the platform notification template.
2331 * You should not be using {@link #setProgress(int, int, boolean)} at the
2332 * same time on those versions; they occupy the same place.
2333 * </p>
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002334 */
2335 public Builder setSubText(CharSequence text) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002336 mN.extras.putCharSequence(EXTRA_SUB_TEXT, safeCharSequence(text));
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002337 return this;
2338 }
2339
2340 /**
Adrian Roose458aa82015-12-08 16:17:19 -08002341 * Set the remote input history.
2342 *
2343 * This should be set to the most recent inputs that have been sent
2344 * through a {@link RemoteInput} of this Notification and cleared once the it is no
2345 * longer relevant (e.g. for chat notifications once the other party has responded).
2346 *
2347 * The most recent input must be stored at the 0 index, the second most recent at the
2348 * 1 index, etc. Note that the system will limit both how far back the inputs will be shown
2349 * and how much of each individual input is shown.
2350 *
2351 * <p>Note: The reply text will only be shown on notifications that have least one action
2352 * with a {@code RemoteInput}.</p>
2353 */
2354 public Builder setRemoteInputHistory(CharSequence[] text) {
2355 if (text == null) {
2356 mN.extras.putCharSequenceArray(EXTRA_REMOTE_INPUT_HISTORY, null);
2357 } else {
2358 final int N = Math.min(MAX_REPLY_HISTORY, text.length);
2359 CharSequence[] safe = new CharSequence[N];
2360 for (int i = 0; i < N; i++) {
2361 safe[i] = safeCharSequence(text[i]);
2362 }
2363 mN.extras.putCharSequenceArray(EXTRA_REMOTE_INPUT_HISTORY, safe);
2364 }
2365 return this;
2366 }
2367
2368 /**
Joe Onoratocb109a02011-01-18 17:57:41 -08002369 * Set the large number at the right-hand side of the notification. This is
2370 * equivalent to setContentInfo, although it might show the number in a different
2371 * font size for readability.
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07002372 *
2373 * @deprecated this number is not shown anywhere anymore
Joe Onoratocb109a02011-01-18 17:57:41 -08002374 */
Joe Onorato8595a3d2010-11-19 18:12:07 -08002375 public Builder setNumber(int number) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002376 mN.number = number;
Joe Onorato8595a3d2010-11-19 18:12:07 -08002377 return this;
2378 }
2379
Joe Onoratocb109a02011-01-18 17:57:41 -08002380 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002381 * A small piece of additional information pertaining to this notification.
2382 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002383 * The platform template will draw this on the last line of the notification, at the far
2384 * right (to the right of a smallIcon if it has been placed there).
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07002385 *
2386 * @deprecated use {@link #setSubText(CharSequence)} instead to set a text in the header.
2387 * For legacy apps targeting a version below {@link android.os.Build.VERSION_CODES#N} this
2388 * field will still show up, but the subtext will take precedence.
Joe Onoratocb109a02011-01-18 17:57:41 -08002389 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002390 public Builder setContentInfo(CharSequence info) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002391 mN.extras.putCharSequence(EXTRA_INFO_TEXT, safeCharSequence(info));
Joe Onorato46439ce2010-11-19 13:56:21 -08002392 return this;
2393 }
2394
Joe Onoratocb109a02011-01-18 17:57:41 -08002395 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002396 * Set the progress this notification represents.
2397 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002398 * The platform template will represent this using a {@link ProgressBar}.
Jeff Sharkey1c400132011-08-05 14:50:13 -07002399 */
2400 public Builder setProgress(int max, int progress, boolean indeterminate) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002401 mN.extras.putInt(EXTRA_PROGRESS, progress);
2402 mN.extras.putInt(EXTRA_PROGRESS_MAX, max);
2403 mN.extras.putBoolean(EXTRA_PROGRESS_INDETERMINATE, indeterminate);
Jeff Sharkey1c400132011-08-05 14:50:13 -07002404 return this;
2405 }
2406
2407 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002408 * Supply a custom RemoteViews to use instead of the platform template.
2409 *
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002410 * Use {@link #setCustomContentView(RemoteViews)} instead.
Joe Onoratocb109a02011-01-18 17:57:41 -08002411 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002412 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -08002413 public Builder setContent(RemoteViews views) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002414 return setCustomContentView(views);
2415 }
2416
2417 /**
2418 * Supply custom RemoteViews to use instead of the platform template.
2419 *
2420 * This will override the layout that would otherwise be constructed by this Builder
2421 * object.
2422 */
2423 public Builder setCustomContentView(RemoteViews contentView) {
2424 mN.contentView = contentView;
2425 return this;
2426 }
2427
2428 /**
2429 * Supply custom RemoteViews to use instead of the platform template in the expanded form.
2430 *
2431 * This will override the expanded layout that would otherwise be constructed by this
2432 * Builder object.
2433 */
2434 public Builder setCustomBigContentView(RemoteViews contentView) {
2435 mN.bigContentView = contentView;
2436 return this;
2437 }
2438
2439 /**
2440 * Supply custom RemoteViews to use instead of the platform template in the heads up dialog.
2441 *
2442 * This will override the heads-up layout that would otherwise be constructed by this
2443 * Builder object.
2444 */
2445 public Builder setCustomHeadsUpContentView(RemoteViews contentView) {
2446 mN.headsUpContentView = contentView;
Joe Onorato46439ce2010-11-19 13:56:21 -08002447 return this;
2448 }
2449
Joe Onoratocb109a02011-01-18 17:57:41 -08002450 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002451 * Supply a {@link PendingIntent} to be sent when the notification is clicked.
2452 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002453 * As of {@link android.os.Build.VERSION_CODES#HONEYCOMB}, if this field is unset and you
2454 * have specified a custom RemoteViews with {@link #setContent(RemoteViews)}, you can use
2455 * {@link RemoteViews#setOnClickPendingIntent RemoteViews.setOnClickPendingIntent(int,PendingIntent)}
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002456 * to assign PendingIntents to individual views in that custom layout (i.e., to create
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002457 * clickable buttons inside the notification view).
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002458 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002459 * @see Notification#contentIntent Notification.contentIntent
Joe Onoratocb109a02011-01-18 17:57:41 -08002460 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002461 public Builder setContentIntent(PendingIntent intent) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002462 mN.contentIntent = intent;
Joe Onorato46439ce2010-11-19 13:56:21 -08002463 return this;
2464 }
2465
Joe Onoratocb109a02011-01-18 17:57:41 -08002466 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002467 * Supply a {@link PendingIntent} to send when the notification is cleared explicitly by the user.
2468 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002469 * @see Notification#deleteIntent
Joe Onoratocb109a02011-01-18 17:57:41 -08002470 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002471 public Builder setDeleteIntent(PendingIntent intent) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002472 mN.deleteIntent = intent;
Joe Onorato46439ce2010-11-19 13:56:21 -08002473 return this;
2474 }
2475
Joe Onoratocb109a02011-01-18 17:57:41 -08002476 /**
2477 * An intent to launch instead of posting the notification to the status bar.
2478 * Only for use with extremely high-priority notifications demanding the user's
2479 * <strong>immediate</strong> attention, such as an incoming phone call or
2480 * alarm clock that the user has explicitly set to a particular time.
2481 * If this facility is used for something else, please give the user an option
2482 * to turn it off and use a normal notification, as this can be extremely
2483 * disruptive.
2484 *
Chris Wren47c20a12014-06-18 17:27:29 -04002485 * <p>
2486 * The system UI may choose to display a heads-up notification, instead of
2487 * launching this intent, while the user is using the device.
2488 * </p>
2489 *
Joe Onoratocb109a02011-01-18 17:57:41 -08002490 * @param intent The pending intent to launch.
2491 * @param highPriority Passing true will cause this notification to be sent
2492 * even if other notifications are suppressed.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002493 *
2494 * @see Notification#fullScreenIntent
Joe Onoratocb109a02011-01-18 17:57:41 -08002495 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002496 public Builder setFullScreenIntent(PendingIntent intent, boolean highPriority) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002497 mN.fullScreenIntent = intent;
Joe Onorato46439ce2010-11-19 13:56:21 -08002498 setFlag(FLAG_HIGH_PRIORITY, highPriority);
2499 return this;
2500 }
2501
Joe Onoratocb109a02011-01-18 17:57:41 -08002502 /**
Dan Sandler5fcdf6e2014-07-18 11:31:15 -04002503 * Set the "ticker" text which is sent to accessibility services.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002504 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002505 * @see Notification#tickerText
Joe Onoratocb109a02011-01-18 17:57:41 -08002506 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002507 public Builder setTicker(CharSequence tickerText) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002508 mN.tickerText = safeCharSequence(tickerText);
Joe Onorato46439ce2010-11-19 13:56:21 -08002509 return this;
2510 }
2511
Joe Onoratocb109a02011-01-18 17:57:41 -08002512 /**
Dan Sandler5fcdf6e2014-07-18 11:31:15 -04002513 * Obsolete version of {@link #setTicker(CharSequence)}.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002514 *
Joe Onoratocb109a02011-01-18 17:57:41 -08002515 */
Dan Sandler5fcdf6e2014-07-18 11:31:15 -04002516 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -08002517 public Builder setTicker(CharSequence tickerText, RemoteViews views) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002518 setTicker(tickerText);
2519 // views is ignored
Joe Onorato46439ce2010-11-19 13:56:21 -08002520 return this;
2521 }
2522
Joe Onoratocb109a02011-01-18 17:57:41 -08002523 /**
Dan Sandlerd63f9322015-05-06 15:18:49 -04002524 * Add a large icon to the notification content view.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002525 *
2526 * In the platform template, this image will be shown on the left of the notification view
Dan Sandlerd63f9322015-05-06 15:18:49 -04002527 * in place of the {@link #setSmallIcon(Icon) small icon} (which will be placed in a small
2528 * badge atop the large icon).
Dan Sandler08a04c12015-05-06 15:18:49 -04002529 */
Dan Sandlerd63f9322015-05-06 15:18:49 -04002530 public Builder setLargeIcon(Bitmap b) {
2531 return setLargeIcon(b != null ? Icon.createWithBitmap(b) : null);
2532 }
2533
2534 /**
2535 * Add a large icon to the notification content view.
2536 *
2537 * In the platform template, this image will be shown on the left of the notification view
2538 * in place of the {@link #setSmallIcon(Icon) small icon} (which will be placed in a small
2539 * badge atop the large icon).
2540 */
2541 public Builder setLargeIcon(Icon icon) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002542 mN.mLargeIcon = icon;
2543 mN.extras.putParcelable(EXTRA_LARGE_ICON, icon);
Joe Onorato46439ce2010-11-19 13:56:21 -08002544 return this;
2545 }
2546
Joe Onoratocb109a02011-01-18 17:57:41 -08002547 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002548 * Set the sound to play.
2549 *
John Spurlockc0650f022014-07-19 13:22:39 -04002550 * It will be played using the {@link #AUDIO_ATTRIBUTES_DEFAULT default audio attributes}
2551 * for notifications.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002552 *
Chris Wren47c20a12014-06-18 17:27:29 -04002553 * <p>
2554 * A notification that is noisy is more likely to be presented as a heads-up notification.
2555 * </p>
2556 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002557 * @see Notification#sound
Joe Onoratocb109a02011-01-18 17:57:41 -08002558 */
Joe Onorato52f80cd2010-11-21 15:34:48 -08002559 public Builder setSound(Uri sound) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002560 mN.sound = sound;
2561 mN.audioAttributes = AUDIO_ATTRIBUTES_DEFAULT;
Joe Onorato52f80cd2010-11-21 15:34:48 -08002562 return this;
2563 }
2564
Joe Onoratocb109a02011-01-18 17:57:41 -08002565 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002566 * Set the sound to play, along with a specific stream on which to play it.
Joe Onoratocb109a02011-01-18 17:57:41 -08002567 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002568 * See {@link android.media.AudioManager} for the <code>STREAM_</code> constants.
2569 *
Chris Wren47c20a12014-06-18 17:27:29 -04002570 * <p>
2571 * A notification that is noisy is more likely to be presented as a heads-up notification.
2572 * </p>
John Spurlockc0650f022014-07-19 13:22:39 -04002573 * @deprecated use {@link #setSound(Uri, AudioAttributes)} instead.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002574 * @see Notification#sound
Joe Onoratocb109a02011-01-18 17:57:41 -08002575 */
Jean-Michel Trivi81f871e2014-08-06 16:32:38 -07002576 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -08002577 public Builder setSound(Uri sound, int streamType) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002578 mN.sound = sound;
2579 mN.audioStreamType = streamType;
Joe Onorato46439ce2010-11-19 13:56:21 -08002580 return this;
2581 }
2582
Joe Onoratocb109a02011-01-18 17:57:41 -08002583 /**
John Spurlockc0650f022014-07-19 13:22:39 -04002584 * Set the sound to play, along with specific {@link AudioAttributes audio attributes} to
2585 * use during playback.
2586 *
2587 * <p>
2588 * A notification that is noisy is more likely to be presented as a heads-up notification.
2589 * </p>
2590 *
2591 * @see Notification#sound
2592 */
2593 public Builder setSound(Uri sound, AudioAttributes audioAttributes) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002594 mN.sound = sound;
2595 mN.audioAttributes = audioAttributes;
John Spurlockc0650f022014-07-19 13:22:39 -04002596 return this;
2597 }
2598
2599 /**
Joe Onoratocb109a02011-01-18 17:57:41 -08002600 * Set the vibration pattern to use.
2601 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002602 * See {@link android.os.Vibrator#vibrate(long[], int)} for a discussion of the
2603 * <code>pattern</code> parameter.
2604 *
Chris Wren47c20a12014-06-18 17:27:29 -04002605 * <p>
2606 * A notification that vibrates is more likely to be presented as a heads-up notification.
2607 * </p>
2608 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002609 * @see Notification#vibrate
Joe Onoratocb109a02011-01-18 17:57:41 -08002610 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002611 public Builder setVibrate(long[] pattern) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002612 mN.vibrate = pattern;
Joe Onorato46439ce2010-11-19 13:56:21 -08002613 return this;
2614 }
2615
Joe Onoratocb109a02011-01-18 17:57:41 -08002616 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002617 * Set the desired color for the indicator LED on the device, as well as the
2618 * blink duty cycle (specified in milliseconds).
2619 *
2620
2621 * Not all devices will honor all (or even any) of these values.
2622 *
2623
2624 * @see Notification#ledARGB
2625 * @see Notification#ledOnMS
2626 * @see Notification#ledOffMS
Joe Onoratocb109a02011-01-18 17:57:41 -08002627 */
Tor Norbye80756e32015-03-02 09:39:27 -08002628 public Builder setLights(@ColorInt int argb, int onMs, int offMs) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002629 mN.ledARGB = argb;
2630 mN.ledOnMS = onMs;
2631 mN.ledOffMS = offMs;
Julia Reynolds10ee1fc2015-11-09 11:04:55 -05002632 if (onMs != 0 || offMs != 0) {
2633 mN.flags |= FLAG_SHOW_LIGHTS;
2634 }
Joe Onorato46439ce2010-11-19 13:56:21 -08002635 return this;
2636 }
2637
Joe Onoratocb109a02011-01-18 17:57:41 -08002638 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002639 * Set whether this is an "ongoing" notification.
Joe Onoratocb109a02011-01-18 17:57:41 -08002640 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002641
2642 * Ongoing notifications cannot be dismissed by the user, so your application or service
2643 * must take care of canceling them.
2644 *
2645
2646 * They are typically used to indicate a background task that the user is actively engaged
2647 * with (e.g., playing music) or is pending in some way and therefore occupying the device
2648 * (e.g., a file download, sync operation, active network connection).
2649 *
2650
2651 * @see Notification#FLAG_ONGOING_EVENT
2652 * @see Service#setForeground(boolean)
Joe Onoratocb109a02011-01-18 17:57:41 -08002653 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002654 public Builder setOngoing(boolean ongoing) {
2655 setFlag(FLAG_ONGOING_EVENT, ongoing);
2656 return this;
2657 }
2658
Joe Onoratocb109a02011-01-18 17:57:41 -08002659 /**
2660 * Set this flag if you would only like the sound, vibrate
2661 * and ticker to be played if the notification is not already showing.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002662 *
2663 * @see Notification#FLAG_ONLY_ALERT_ONCE
Joe Onoratocb109a02011-01-18 17:57:41 -08002664 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002665 public Builder setOnlyAlertOnce(boolean onlyAlertOnce) {
2666 setFlag(FLAG_ONLY_ALERT_ONCE, onlyAlertOnce);
2667 return this;
2668 }
2669
Joe Onoratocb109a02011-01-18 17:57:41 -08002670 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002671 * Make this notification automatically dismissed when the user touches it. The
2672 * PendingIntent set with {@link #setDeleteIntent} will be sent when this happens.
2673 *
2674 * @see Notification#FLAG_AUTO_CANCEL
Joe Onoratocb109a02011-01-18 17:57:41 -08002675 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002676 public Builder setAutoCancel(boolean autoCancel) {
Joe Onorato281d83f2011-01-04 17:13:10 -08002677 setFlag(FLAG_AUTO_CANCEL, autoCancel);
Joe Onorato46439ce2010-11-19 13:56:21 -08002678 return this;
2679 }
2680
Joe Onoratocb109a02011-01-18 17:57:41 -08002681 /**
Griff Hazendfcb0802014-02-11 12:00:00 -08002682 * Set whether or not this notification should not bridge to other devices.
2683 *
2684 * <p>Some notifications can be bridged to other devices for remote display.
2685 * This hint can be set to recommend this notification not be bridged.
2686 */
2687 public Builder setLocalOnly(boolean localOnly) {
2688 setFlag(FLAG_LOCAL_ONLY, localOnly);
2689 return this;
2690 }
2691
2692 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002693 * Set which notification properties will be inherited from system defaults.
Joe Onoratocb109a02011-01-18 17:57:41 -08002694 * <p>
2695 * The value should be one or more of the following fields combined with
2696 * bitwise-or:
2697 * {@link #DEFAULT_SOUND}, {@link #DEFAULT_VIBRATE}, {@link #DEFAULT_LIGHTS}.
2698 * <p>
2699 * For all default values, use {@link #DEFAULT_ALL}.
2700 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002701 public Builder setDefaults(int defaults) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002702 mN.defaults = defaults;
Joe Onorato46439ce2010-11-19 13:56:21 -08002703 return this;
2704 }
2705
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002706 /**
2707 * Set the priority of this notification.
2708 *
2709 * @see Notification#priority
2710 */
Tor Norbyed9273d62013-05-30 15:59:53 -07002711 public Builder setPriority(@Priority int pri) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002712 mN.priority = pri;
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002713 return this;
2714 }
Joe Malin8d40d042012-11-05 11:36:40 -08002715
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002716 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -04002717 * Set the notification category.
Joe Malin8d40d042012-11-05 11:36:40 -08002718 *
John Spurlockfd7f1e02014-03-18 16:41:57 -04002719 * @see Notification#category
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002720 */
John Spurlockfd7f1e02014-03-18 16:41:57 -04002721 public Builder setCategory(String category) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002722 mN.category = category;
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002723 return this;
2724 }
2725
2726 /**
Chris Wrendde75302014-03-26 17:24:15 -04002727 * Add a person that is relevant to this notification.
2728 *
Chris Wrene6c48932014-09-29 17:19:27 -04002729 * <P>
2730 * Depending on user preferences, this annotation may allow the notification to pass
2731 * through interruption filters, and to appear more prominently in the user interface.
2732 * </P>
2733 *
2734 * <P>
2735 * The person should be specified by the {@code String} representation of a
2736 * {@link android.provider.ContactsContract.Contacts#CONTENT_LOOKUP_URI}.
2737 * </P>
2738 *
2739 * <P>The system will also attempt to resolve {@code mailto:} and {@code tel:} schema
2740 * URIs. The path part of these URIs must exist in the contacts database, in the
2741 * appropriate column, or the reference will be discarded as invalid. Telephone schema
2742 * URIs will be resolved by {@link android.provider.ContactsContract.PhoneLookup}.
2743 * </P>
2744 *
2745 * @param uri A URI for the person.
Chris Wrendde75302014-03-26 17:24:15 -04002746 * @see Notification#EXTRA_PEOPLE
2747 */
Chris Wrene6c48932014-09-29 17:19:27 -04002748 public Builder addPerson(String uri) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002749 mPersonList.add(uri);
Chris Wrendde75302014-03-26 17:24:15 -04002750 return this;
2751 }
2752
2753 /**
Griff Hazen5cadc3b2014-05-20 09:55:39 -07002754 * Set this notification to be part of a group of notifications sharing the same key.
2755 * Grouped notifications may display in a cluster or stack on devices which
2756 * support such rendering.
2757 *
2758 * <p>To make this notification the summary for its group, also call
2759 * {@link #setGroupSummary}. A sort order can be specified for group members by using
2760 * {@link #setSortKey}.
2761 * @param groupKey The group key of the group.
2762 * @return this object for method chaining
2763 */
2764 public Builder setGroup(String groupKey) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002765 mN.mGroupKey = groupKey;
Griff Hazen5cadc3b2014-05-20 09:55:39 -07002766 return this;
2767 }
2768
2769 /**
2770 * Set this notification to be the group summary for a group of notifications.
2771 * Grouped notifications may display in a cluster or stack on devices which
2772 * support such rendering. Requires a group key also be set using {@link #setGroup}.
2773 * @param isGroupSummary Whether this notification should be a group summary.
2774 * @return this object for method chaining
2775 */
2776 public Builder setGroupSummary(boolean isGroupSummary) {
2777 setFlag(FLAG_GROUP_SUMMARY, isGroupSummary);
2778 return this;
2779 }
2780
2781 /**
2782 * Set a sort key that orders this notification among other notifications from the
2783 * same package. This can be useful if an external sort was already applied and an app
2784 * would like to preserve this. Notifications will be sorted lexicographically using this
2785 * value, although providing different priorities in addition to providing sort key may
2786 * cause this value to be ignored.
2787 *
2788 * <p>This sort key can also be used to order members of a notification group. See
Griff Hazen9e1379f2014-05-20 12:50:51 -07002789 * {@link #setGroup}.
Griff Hazen5cadc3b2014-05-20 09:55:39 -07002790 *
2791 * @see String#compareTo(String)
2792 */
2793 public Builder setSortKey(String sortKey) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002794 mN.mSortKey = sortKey;
Griff Hazen5cadc3b2014-05-20 09:55:39 -07002795 return this;
2796 }
2797
2798 /**
Griff Hazen720042b2014-02-24 15:46:56 -08002799 * Merge additional metadata into this notification.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002800 *
Griff Hazen720042b2014-02-24 15:46:56 -08002801 * <p>Values within the Bundle will replace existing extras values in this Builder.
2802 *
2803 * @see Notification#extras
2804 */
Griff Hazen959591e2014-05-15 22:26:18 -07002805 public Builder addExtras(Bundle extras) {
2806 if (extras != null) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002807 mUserExtras.putAll(extras);
Griff Hazen720042b2014-02-24 15:46:56 -08002808 }
2809 return this;
2810 }
2811
2812 /**
2813 * Set metadata for this notification.
2814 *
2815 * <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 -04002816 * current contents are copied into the Notification each time {@link #build()} is
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002817 * called.
2818 *
Griff Hazen720042b2014-02-24 15:46:56 -08002819 * <p>Replaces any existing extras values with those from the provided Bundle.
2820 * Use {@link #addExtras} to merge in metadata instead.
2821 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002822 * @see Notification#extras
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002823 */
Griff Hazen959591e2014-05-15 22:26:18 -07002824 public Builder setExtras(Bundle extras) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002825 if (extras != null) {
2826 mUserExtras = extras;
2827 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002828 return this;
2829 }
2830
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002831 /**
Griff Hazen720042b2014-02-24 15:46:56 -08002832 * Get the current metadata Bundle used by this notification Builder.
2833 *
2834 * <p>The returned Bundle is shared with this Builder.
2835 *
2836 * <p>The current contents of this Bundle are copied into the Notification each time
2837 * {@link #build()} is called.
2838 *
2839 * @see Notification#extras
2840 */
2841 public Bundle getExtras() {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002842 return mUserExtras;
2843 }
2844
2845 private Bundle getAllExtras() {
2846 final Bundle saveExtras = (Bundle) mUserExtras.clone();
2847 saveExtras.putAll(mN.extras);
2848 return saveExtras;
Griff Hazen720042b2014-02-24 15:46:56 -08002849 }
2850
2851 /**
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002852 * Add an action to this notification. Actions are typically displayed by
2853 * the system as a button adjacent to the notification content.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04002854 * <p>
2855 * Every action must have an icon (32dp square and matching the
2856 * <a href="{@docRoot}design/style/iconography.html#action-bar">Holo
2857 * Dark action bar</a> visual style), a textual label, and a {@link PendingIntent}.
2858 * <p>
2859 * A notification in its expanded form can display up to 3 actions, from left to right in
2860 * the order they were added. Actions will not be displayed when the notification is
2861 * collapsed, however, so be sure that any essential functions may be accessed by the user
2862 * in some other way (for example, in the Activity pointed to by {@link #contentIntent}).
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002863 *
2864 * @param icon Resource ID of a drawable that represents the action.
2865 * @param title Text describing the action.
2866 * @param intent PendingIntent to be fired when the action is invoked.
Dan Sandler86647982015-05-13 23:41:13 -04002867 *
2868 * @deprecated Use {@link #addAction(Action)} instead.
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002869 */
Dan Sandler86647982015-05-13 23:41:13 -04002870 @Deprecated
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002871 public Builder addAction(int icon, CharSequence title, PendingIntent intent) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04002872 mActions.add(new Action(icon, safeCharSequence(title), intent));
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002873 return this;
2874 }
2875
Chris Wrenfbd96ba2012-05-01 12:03:58 -04002876 /**
Griff Hazen959591e2014-05-15 22:26:18 -07002877 * Add an action to this notification. Actions are typically displayed by
2878 * the system as a button adjacent to the notification content.
2879 * <p>
2880 * Every action must have an icon (32dp square and matching the
2881 * <a href="{@docRoot}design/style/iconography.html#action-bar">Holo
2882 * Dark action bar</a> visual style), a textual label, and a {@link PendingIntent}.
2883 * <p>
2884 * A notification in its expanded form can display up to 3 actions, from left to right in
2885 * the order they were added. Actions will not be displayed when the notification is
2886 * collapsed, however, so be sure that any essential functions may be accessed by the user
2887 * in some other way (for example, in the Activity pointed to by {@link #contentIntent}).
2888 *
2889 * @param action The action to add.
2890 */
2891 public Builder addAction(Action action) {
2892 mActions.add(action);
2893 return this;
2894 }
2895
2896 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002897 * Alter the complete list of actions attached to this notification.
2898 * @see #addAction(Action).
2899 *
2900 * @param actions
2901 * @return
2902 */
2903 public Builder setActions(Action... actions) {
2904 mActions.clear();
2905 for (int i = 0; i < actions.length; i++) {
2906 mActions.add(actions[i]);
2907 }
2908 return this;
2909 }
2910
2911 /**
Chris Wrenfbd96ba2012-05-01 12:03:58 -04002912 * Add a rich notification style to be applied at build time.
2913 *
2914 * @param style Object responsible for modifying the notification style.
2915 */
2916 public Builder setStyle(Style style) {
2917 if (mStyle != style) {
2918 mStyle = style;
Daniel Sandlerc08dea22012-06-28 08:35:24 -07002919 if (mStyle != null) {
2920 mStyle.setBuilder(this);
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002921 mN.extras.putString(EXTRA_TEMPLATE, style.getClass().getName());
2922 } else {
2923 mN.extras.remove(EXTRA_TEMPLATE);
Daniel Sandlerc08dea22012-06-28 08:35:24 -07002924 }
Chris Wrenfbd96ba2012-05-01 12:03:58 -04002925 }
2926 return this;
2927 }
2928
Dan Sandler0bf2ed82013-12-21 23:33:41 -06002929 /**
2930 * Specify the value of {@link #visibility}.
Griff Hazenb720abe2014-05-20 13:15:30 -07002931 *
Dan Sandler0bf2ed82013-12-21 23:33:41 -06002932 * @param visibility One of {@link #VISIBILITY_PRIVATE} (the default),
2933 * {@link #VISIBILITY_SECRET}, or {@link #VISIBILITY_PUBLIC}.
2934 *
2935 * @return The same Builder.
2936 */
2937 public Builder setVisibility(int visibility) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002938 mN.visibility = visibility;
Dan Sandler0bf2ed82013-12-21 23:33:41 -06002939 return this;
2940 }
2941
2942 /**
2943 * Supply a replacement Notification whose contents should be shown in insecure contexts
2944 * (i.e. atop the secure lockscreen). See {@link #visibility} and {@link #VISIBILITY_PUBLIC}.
2945 * @param n A replacement notification, presumably with some or all info redacted.
2946 * @return The same Builder.
2947 */
2948 public Builder setPublicVersion(Notification n) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002949 if (n != null) {
2950 mN.publicVersion = new Notification();
2951 n.cloneInto(mN.publicVersion, /*heavy=*/ true);
2952 } else {
2953 mN.publicVersion = null;
2954 }
Dan Sandler0bf2ed82013-12-21 23:33:41 -06002955 return this;
2956 }
2957
Griff Hazenb720abe2014-05-20 13:15:30 -07002958 /**
Griff Hazen5cadc3b2014-05-20 09:55:39 -07002959 * Apply an extender to this notification builder. Extenders may be used to add
2960 * metadata or change options on this builder.
2961 */
Griff Hazen61a9e862014-05-22 16:05:19 -07002962 public Builder extend(Extender extender) {
2963 extender.extend(this);
Griff Hazen5cadc3b2014-05-20 09:55:39 -07002964 return this;
2965 }
2966
Dan Sandler4e787062015-06-17 15:09:48 -04002967 /**
2968 * @hide
2969 */
2970 public void setFlag(int mask, boolean value) {
Joe Onorato46439ce2010-11-19 13:56:21 -08002971 if (value) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002972 mN.flags |= mask;
Joe Onorato46439ce2010-11-19 13:56:21 -08002973 } else {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002974 mN.flags &= ~mask;
Joe Onorato46439ce2010-11-19 13:56:21 -08002975 }
2976 }
2977
Dan Sandler26e81cf2014-05-06 10:01:27 -04002978 /**
2979 * Sets {@link Notification#color}.
2980 *
2981 * @param argb The accent color to use
2982 *
2983 * @return The same Builder.
2984 */
Tor Norbye80756e32015-03-02 09:39:27 -08002985 public Builder setColor(@ColorInt int argb) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002986 mN.color = argb;
Julia Reynolds10ee1fc2015-11-09 11:04:55 -05002987 sanitizeColor();
Dan Sandler26e81cf2014-05-06 10:01:27 -04002988 return this;
2989 }
2990
Jorim Jaggid05aa3e2014-08-28 17:52:27 +02002991 private Drawable getProfileBadgeDrawable() {
Christoph Studer7ac80e62014-08-04 16:01:57 +02002992 // Note: This assumes that the current user can read the profile badge of the
2993 // originating user.
Selim Cineke6ff9462016-01-15 15:07:06 -08002994 return mContext.getPackageManager().getUserBadgeForDensityNoBackground(
Julia Reynoldsda303542015-11-23 14:00:20 -05002995 new UserHandle(mContext.getUserId()), 0);
Jorim Jaggid05aa3e2014-08-28 17:52:27 +02002996 }
2997
2998 private Bitmap getProfileBadge() {
2999 Drawable badge = getProfileBadgeDrawable();
Kenny Guy8a0101b2014-05-08 23:34:12 +01003000 if (badge == null) {
3001 return null;
3002 }
Jorim Jaggid05aa3e2014-08-28 17:52:27 +02003003 final int size = mContext.getResources().getDimensionPixelSize(
3004 R.dimen.notification_badge_size);
3005 Bitmap bitmap = Bitmap.createBitmap(size, size, Bitmap.Config.ARGB_8888);
Kenny Guy8a0101b2014-05-08 23:34:12 +01003006 Canvas canvas = new Canvas(bitmap);
Jorim Jaggid05aa3e2014-08-28 17:52:27 +02003007 badge.setBounds(0, 0, size, size);
Kenny Guy8a0101b2014-05-08 23:34:12 +01003008 badge.draw(canvas);
3009 return bitmap;
3010 }
3011
Selim Cinekc848c3a2016-01-13 15:27:30 -08003012 private void bindProfileBadge(RemoteViews contentView) {
Kenny Guy98193ea2014-07-24 19:54:37 +01003013 Bitmap profileBadge = getProfileBadge();
3014
Kenny Guy98193ea2014-07-24 19:54:37 +01003015 if (profileBadge != null) {
Selim Cinekc848c3a2016-01-13 15:27:30 -08003016 contentView.setImageViewBitmap(R.id.profile_badge, profileBadge);
3017 contentView.setViewVisibility(R.id.profile_badge, View.VISIBLE);
Kenny Guy98193ea2014-07-24 19:54:37 +01003018 }
Kenny Guy98193ea2014-07-24 19:54:37 +01003019 }
3020
Christoph Studerfe718432014-09-01 18:21:18 +02003021 private void resetStandardTemplate(RemoteViews contentView) {
Selim Cinekeaa29ca2015-11-23 13:51:13 -08003022 resetNotificationHeader(contentView);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003023 resetContentMargins(contentView);
Christoph Studerfe718432014-09-01 18:21:18 +02003024 contentView.setViewVisibility(R.id.right_icon, View.GONE);
Selim Cinek860b6da2015-12-16 19:02:19 -08003025 contentView.setViewVisibility(R.id.title, View.GONE);
Christoph Studerfe718432014-09-01 18:21:18 +02003026 contentView.setTextViewText(R.id.title, null);
Selim Cinek41598732016-01-11 16:58:37 -08003027 contentView.setViewVisibility(R.id.text, View.GONE);
Christoph Studerfe718432014-09-01 18:21:18 +02003028 contentView.setTextViewText(R.id.text, null);
Selim Cinek29603462015-11-17 19:04:39 -08003029 contentView.setViewVisibility(R.id.text_line_1, View.GONE);
Selim Cinek41598732016-01-11 16:58:37 -08003030 contentView.setTextViewText(R.id.text_line_1, null);
Christoph Studerfe718432014-09-01 18:21:18 +02003031 contentView.setViewVisibility(R.id.progress, View.GONE);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003032 }
3033
Selim Cinekeaa29ca2015-11-23 13:51:13 -08003034 /**
3035 * Resets the notification header to its original state
3036 */
3037 private void resetNotificationHeader(RemoteViews contentView) {
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003038 contentView.setImageViewResource(R.id.icon, 0);
Selim Cinek7b836392015-12-04 20:02:59 -08003039 contentView.setBoolean(R.id.notification_header, "setExpanded", false);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003040 contentView.setTextViewText(R.id.app_name_text, null);
Christoph Studerca1db712014-09-10 17:31:33 +02003041 contentView.setViewVisibility(R.id.chronometer, View.GONE);
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07003042 contentView.setViewVisibility(R.id.header_text, View.GONE);
3043 contentView.setViewVisibility(R.id.header_text_divider, View.GONE);
Selim Cinek29603462015-11-17 19:04:39 -08003044 contentView.setViewVisibility(R.id.time_divider, View.GONE);
Selim Cinekc848c3a2016-01-13 15:27:30 -08003045 contentView.setImageViewIcon(R.id.profile_badge, null);
3046 contentView.setViewVisibility(R.id.profile_badge, View.GONE);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003047 }
3048
3049 private void resetContentMargins(RemoteViews contentView) {
3050 contentView.setViewLayoutMarginEnd(R.id.line1, 0);
Selim Cinekc848c3a2016-01-13 15:27:30 -08003051 contentView.setViewLayoutMarginEnd(R.id.text, 0);
Christoph Studerfe718432014-09-01 18:21:18 +02003052 }
3053
Jorim Jaggi445d3c02014-08-19 22:33:42 +02003054 private RemoteViews applyStandardTemplate(int resId) {
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02003055 return applyStandardTemplate(resId, true /* hasProgress */);
3056 }
3057
3058 /**
3059 * @param hasProgress whether the progress bar should be shown and set
3060 */
3061 private RemoteViews applyStandardTemplate(int resId, boolean hasProgress) {
Kenny Guy77320062014-08-27 21:37:15 +01003062 RemoteViews contentView = new BuilderRemoteViews(mContext.getApplicationInfo(), resId);
Dan Sandler539aad42014-08-04 00:43:39 -04003063
Christoph Studerfe718432014-09-01 18:21:18 +02003064 resetStandardTemplate(contentView);
3065
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003066 final Bundle ex = mN.extras;
Dan Sandler190d58d2014-05-15 09:33:39 -04003067
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003068 bindNotificationHeader(contentView);
3069 bindLargeIcon(contentView);
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003070 if (ex.getCharSequence(EXTRA_TITLE) != null) {
Selim Cinek860b6da2015-12-16 19:02:19 -08003071 contentView.setViewVisibility(R.id.title, View.VISIBLE);
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003072 contentView.setTextViewText(R.id.title,
3073 processLegacyText(ex.getCharSequence(EXTRA_TITLE)));
Joe Onorato561d3852010-11-20 18:09:34 -08003074 }
Selim Cinek29603462015-11-17 19:04:39 -08003075 boolean showProgress = handleProgressBar(hasProgress, contentView, ex);
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003076 if (ex.getCharSequence(EXTRA_TEXT) != null) {
Selim Cinek41598732016-01-11 16:58:37 -08003077 int textId = showProgress ? com.android.internal.R.id.text_line_1
3078 : com.android.internal.R.id.text;
3079 contentView.setTextViewText(textId, processLegacyText(
3080 ex.getCharSequence(EXTRA_TEXT)));
3081 contentView.setViewVisibility(textId, View.VISIBLE);
Joe Onorato561d3852010-11-20 18:09:34 -08003082 }
Selim Cinekc848c3a2016-01-13 15:27:30 -08003083
Selim Cinek860b6da2015-12-16 19:02:19 -08003084 setContentMinHeight(contentView, showProgress || mN.mLargeIcon != null);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003085
Selim Cinek29603462015-11-17 19:04:39 -08003086 return contentView;
3087 }
3088
Selim Cinek860b6da2015-12-16 19:02:19 -08003089 /**
3090 * @param remoteView the remote view to update the minheight in
3091 * @param hasMinHeight does it have a mimHeight
3092 * @hide
3093 */
3094 void setContentMinHeight(RemoteViews remoteView, boolean hasMinHeight) {
3095 int minHeight = 0;
3096 if (hasMinHeight) {
3097 // we need to set the minHeight of the notification
3098 minHeight = mContext.getResources().getDimensionPixelSize(
3099 com.android.internal.R.dimen.notification_min_content_height);
3100 }
3101 remoteView.setInt(R.id.notification_main_column, "setMinimumHeight", minHeight);
3102 }
3103
Selim Cinek29603462015-11-17 19:04:39 -08003104 private boolean handleProgressBar(boolean hasProgress, RemoteViews contentView, Bundle ex) {
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003105 final int max = ex.getInt(EXTRA_PROGRESS_MAX, 0);
3106 final int progress = ex.getInt(EXTRA_PROGRESS, 0);
3107 final boolean ind = ex.getBoolean(EXTRA_PROGRESS_INDETERMINATE);
3108 if (hasProgress && (max != 0 || ind)) {
Selim Cinek29603462015-11-17 19:04:39 -08003109 contentView.setViewVisibility(com.android.internal.R.id.progress, View.VISIBLE);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003110 contentView.setProgressBar(
Selim Cinek29603462015-11-17 19:04:39 -08003111 R.id.progress, max, progress, ind);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003112 contentView.setProgressBackgroundTintList(
3113 R.id.progress, ColorStateList.valueOf(mContext.getColor(
3114 R.color.notification_progress_background_color)));
Selim Cinek29603462015-11-17 19:04:39 -08003115 if (mN.color != COLOR_DEFAULT) {
Adrian Roos4ff3b122016-02-01 12:26:13 -08003116 ColorStateList colorStateList = ColorStateList.valueOf(resolveContrastColor());
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003117 contentView.setProgressTintList(R.id.progress, colorStateList);
3118 contentView.setProgressIndeterminateTintList(R.id.progress, colorStateList);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003119 }
Selim Cinek29603462015-11-17 19:04:39 -08003120 return true;
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003121 } else {
3122 contentView.setViewVisibility(R.id.progress, View.GONE);
Selim Cinek29603462015-11-17 19:04:39 -08003123 return false;
Jeff Sharkey1c400132011-08-05 14:50:13 -07003124 }
Joe Onorato561d3852010-11-20 18:09:34 -08003125 }
3126
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003127 private void bindLargeIcon(RemoteViews contentView) {
3128 if (mN.mLargeIcon != null) {
3129 contentView.setViewVisibility(R.id.right_icon, View.VISIBLE);
3130 contentView.setImageViewIcon(R.id.right_icon, mN.mLargeIcon);
3131 processLargeLegacyIcon(mN.mLargeIcon, contentView);
3132 int endMargin = mContext.getResources().getDimensionPixelSize(
3133 R.dimen.notification_content_picture_margin);
3134 contentView.setViewLayoutMarginEnd(R.id.line1, endMargin);
Selim Cinekc848c3a2016-01-13 15:27:30 -08003135 contentView.setViewLayoutMarginEnd(R.id.text, endMargin);
Selim Cinek29603462015-11-17 19:04:39 -08003136 contentView.setViewLayoutMarginEnd(R.id.progress, endMargin);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003137 }
3138 }
3139
3140 private void bindNotificationHeader(RemoteViews contentView) {
3141 bindSmallIcon(contentView);
3142 bindHeaderAppName(contentView);
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07003143 bindHeaderText(contentView);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003144 bindHeaderChronometerAndTime(contentView);
3145 bindExpandButton(contentView);
Selim Cinekc848c3a2016-01-13 15:27:30 -08003146 bindProfileBadge(contentView);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003147 }
3148
3149 private void bindExpandButton(RemoteViews contentView) {
Adrian Roos4ff3b122016-02-01 12:26:13 -08003150 contentView.setDrawableParameters(R.id.expand_button, false, -1, resolveContrastColor(),
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003151 PorterDuff.Mode.SRC_ATOP, -1);
Selim Cinekea4bef72015-12-02 15:51:10 -08003152 contentView.setInt(R.id.notification_header, "setOriginalNotificationColor",
Adrian Roos4ff3b122016-02-01 12:26:13 -08003153 resolveContrastColor());
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003154 }
3155
3156 private void bindHeaderChronometerAndTime(RemoteViews contentView) {
3157 if (showsTimeOrChronometer()) {
Selim Cinek29603462015-11-17 19:04:39 -08003158 contentView.setViewVisibility(R.id.time_divider, View.VISIBLE);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003159 if (mN.extras.getBoolean(EXTRA_SHOW_CHRONOMETER)) {
3160 contentView.setViewVisibility(R.id.chronometer, View.VISIBLE);
3161 contentView.setLong(R.id.chronometer, "setBase",
3162 mN.when + (SystemClock.elapsedRealtime() - System.currentTimeMillis()));
3163 contentView.setBoolean(R.id.chronometer, "setStarted", true);
Selim Cinek81c23aa2016-02-25 16:23:13 -08003164 boolean countsDown = mN.extras.getBoolean(EXTRA_CHRONOMETER_COUNTS_DOWN);
3165 contentView.setChronometerCountsDown(R.id.chronometer, countsDown);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003166 } else {
3167 contentView.setViewVisibility(R.id.time, View.VISIBLE);
3168 contentView.setLong(R.id.time, "setTime", mN.when);
3169 }
3170 }
3171 }
3172
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07003173 private void bindHeaderText(RemoteViews contentView) {
3174 CharSequence headerText = mN.extras.getCharSequence(EXTRA_SUB_TEXT);
3175 if (headerText == null && mStyle != null && mStyle.mSummaryTextSet
Selim Cinek03d0d652015-11-13 13:18:09 -05003176 && mStyle.hasSummaryInHeader()) {
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07003177 headerText = mStyle.mSummaryText;
Selim Cinek03d0d652015-11-13 13:18:09 -05003178 }
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07003179 if (headerText == null
3180 && mContext.getApplicationInfo().targetSdkVersion < Build.VERSION_CODES.N
3181 && mN.extras.getCharSequence(EXTRA_INFO_TEXT) != null) {
3182 headerText = mN.extras.getCharSequence(EXTRA_INFO_TEXT);
3183 }
3184 if (headerText != null) {
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003185 // TODO: Remove the span entirely to only have the string with propper formating.
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07003186 contentView.setTextViewText(R.id.header_text, processLegacyText(headerText));
3187 contentView.setViewVisibility(R.id.header_text, View.VISIBLE);
3188 contentView.setViewVisibility(R.id.header_text_divider, View.VISIBLE);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003189 }
3190 }
3191
3192 private void bindHeaderAppName(RemoteViews contentView) {
Tony Mak4dc008c2016-03-16 10:46:49 +00003193 CharSequence appName = mContext.getPackageManager()
3194 .getApplicationLabel(mContext.getApplicationInfo());
3195
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003196 if (TextUtils.isEmpty(appName)) {
3197 return;
3198 }
3199 contentView.setTextViewText(R.id.app_name_text, appName);
Adrian Roos4ff3b122016-02-01 12:26:13 -08003200 contentView.setTextColor(R.id.app_name_text, resolveContrastColor());
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003201 }
3202
3203 private void bindSmallIcon(RemoteViews contentView) {
3204 contentView.setImageViewIcon(R.id.icon, mN.mSmallIcon);
3205 processSmallIconColor(mN.mSmallIcon, contentView);
3206 }
3207
Jorim Jaggi445d3c02014-08-19 22:33:42 +02003208 /**
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02003209 * @return true if the built notification will show the time or the chronometer; false
3210 * otherwise
3211 */
3212 private boolean showsTimeOrChronometer() {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003213 return mN.when != 0 && mN.extras.getBoolean(EXTRA_SHOW_WHEN);
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02003214 }
3215
Christoph Studerfe718432014-09-01 18:21:18 +02003216 private void resetStandardTemplateWithActions(RemoteViews big) {
Adrian Roos7052de52016-03-03 15:53:34 -08003217 big.setViewVisibility(R.id.actions_container, View.GONE);
Christoph Studerfe718432014-09-01 18:21:18 +02003218 big.setViewVisibility(R.id.actions, View.GONE);
Christoph Studerfe718432014-09-01 18:21:18 +02003219 big.removeAllViews(R.id.actions);
Adrian Roose458aa82015-12-08 16:17:19 -08003220
3221 big.setViewVisibility(R.id.notification_material_reply_container, View.GONE);
3222 big.setTextViewText(R.id.notification_material_reply_text_1, null);
3223
3224 big.setViewVisibility(R.id.notification_material_reply_text_2, View.GONE);
3225 big.setTextViewText(R.id.notification_material_reply_text_2, null);
3226 big.setViewVisibility(R.id.notification_material_reply_text_3, View.GONE);
3227 big.setTextViewText(R.id.notification_material_reply_text_3, null);
Christoph Studerfe718432014-09-01 18:21:18 +02003228 }
3229
Daniel Sandler96fd7c12012-03-30 16:37:36 -04003230 private RemoteViews applyStandardTemplateWithActions(int layoutId) {
Jorim Jaggi445d3c02014-08-19 22:33:42 +02003231 RemoteViews big = applyStandardTemplate(layoutId);
Daniel Sandler96fd7c12012-03-30 16:37:36 -04003232
Christoph Studerfe718432014-09-01 18:21:18 +02003233 resetStandardTemplateWithActions(big);
3234
Adrian Roose458aa82015-12-08 16:17:19 -08003235 boolean validRemoteInput = false;
3236
Daniel Sandler96fd7c12012-03-30 16:37:36 -04003237 int N = mActions.size();
3238 if (N > 0) {
Adrian Roos7052de52016-03-03 15:53:34 -08003239 big.setViewVisibility(R.id.actions_container, View.VISIBLE);
Daniel Sandler96fd7c12012-03-30 16:37:36 -04003240 big.setViewVisibility(R.id.actions, View.VISIBLE);
Daniel Sandler8680bf82012-05-15 16:52:52 -04003241 if (N>MAX_ACTION_BUTTONS) N=MAX_ACTION_BUTTONS;
Daniel Sandler96fd7c12012-03-30 16:37:36 -04003242 for (int i=0; i<N; i++) {
Adrian Roose458aa82015-12-08 16:17:19 -08003243 Action action = mActions.get(i);
3244 validRemoteInput |= hasValidRemoteInput(action);
3245
3246 final RemoteViews button = generateActionButton(action);
Adrian Roos9b123cf2016-02-04 14:55:57 -08003247 if (i == N - 1) {
3248 button.setViewLayoutWidth(com.android.internal.R.id.action0,
3249 ViewGroup.LayoutParams.MATCH_PARENT);
3250 }
Daniel Sandler96fd7c12012-03-30 16:37:36 -04003251 big.addView(R.id.actions, button);
3252 }
3253 }
Adrian Roose458aa82015-12-08 16:17:19 -08003254
3255 CharSequence[] replyText = mN.extras.getCharSequenceArray(EXTRA_REMOTE_INPUT_HISTORY);
3256 if (validRemoteInput && replyText != null
3257 && replyText.length > 0 && !TextUtils.isEmpty(replyText[0])) {
3258 big.setViewVisibility(R.id.notification_material_reply_container, View.VISIBLE);
3259 big.setTextViewText(R.id.notification_material_reply_text_1, replyText[0]);
3260
3261 if (replyText.length > 1 && !TextUtils.isEmpty(replyText[1])) {
3262 big.setViewVisibility(R.id.notification_material_reply_text_2, View.VISIBLE);
3263 big.setTextViewText(R.id.notification_material_reply_text_2, replyText[1]);
3264
3265 if (replyText.length > 2 && !TextUtils.isEmpty(replyText[2])) {
3266 big.setViewVisibility(
3267 R.id.notification_material_reply_text_3, View.VISIBLE);
3268 big.setTextViewText(R.id.notification_material_reply_text_3, replyText[2]);
3269 }
3270 }
3271 }
3272
Daniel Sandler96fd7c12012-03-30 16:37:36 -04003273 return big;
3274 }
3275
Adrian Roose458aa82015-12-08 16:17:19 -08003276 private boolean hasValidRemoteInput(Action action) {
3277 if (TextUtils.isEmpty(action.title) || action.actionIntent == null) {
3278 // Weird actions
3279 return false;
3280 }
3281
3282 RemoteInput[] remoteInputs = action.getRemoteInputs();
3283 if (remoteInputs == null) {
3284 return false;
3285 }
3286
3287 for (RemoteInput r : remoteInputs) {
3288 CharSequence[] choices = r.getChoices();
3289 if (r.getAllowFreeFormInput() || (choices != null && choices.length != 0)) {
3290 return true;
3291 }
3292 }
3293 return false;
3294 }
3295
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003296 /**
3297 * Construct a RemoteViews for the final 1U notification layout. In order:
3298 * 1. Custom contentView from the caller
3299 * 2. Style's proposed content view
3300 * 3. Standard template view
3301 */
Julia Reynolds3b848122016-02-26 10:45:32 -05003302 public RemoteViews createContentView() {
Selim Cinek593610c2016-02-16 18:42:57 -08003303 if (mN.contentView != null && (mStyle == null || !mStyle.displayCustomViewInline())) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003304 return mN.contentView;
3305 } else if (mStyle != null) {
3306 final RemoteViews styleView = mStyle.makeContentView();
3307 if (styleView != null) {
3308 return styleView;
3309 }
Joe Onorato46439ce2010-11-19 13:56:21 -08003310 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003311 return applyStandardTemplate(getBaseLayoutResource());
Joe Onorato46439ce2010-11-19 13:56:21 -08003312 }
3313
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003314 /**
3315 * Construct a RemoteViews for the final big notification layout.
3316 */
Julia Reynolds3b848122016-02-26 10:45:32 -05003317 public RemoteViews createBigContentView() {
Selim Cinek850a8542015-11-11 11:48:36 -05003318 RemoteViews result = null;
Selim Cinek593610c2016-02-16 18:42:57 -08003319 if (mN.bigContentView != null
3320 && (mStyle == null || !mStyle.displayCustomViewInline())) {
Julia Reynolds089e3e42015-11-18 09:59:57 -05003321 return mN.bigContentView;
3322 } else if (mStyle != null) {
Selim Cinek850a8542015-11-11 11:48:36 -05003323 result = mStyle.makeBigContentView();
Selim Cinek90dcf6d2015-11-18 20:24:13 -08003324 hideLine1Text(result);
Selim Cinekcc10bfb2016-02-10 16:24:21 -08003325 } else if (mActions.size() != 0) {
3326 result = applyStandardTemplateWithActions(getBigBaseLayoutResource());
Selim Cinek850a8542015-11-11 11:48:36 -05003327 }
3328 adaptNotificationHeaderForBigContentView(result);
3329 return result;
3330 }
Daniel Sandler96fd7c12012-03-30 16:37:36 -04003331
Selim Cinekeaa29ca2015-11-23 13:51:13 -08003332 /**
3333 * Construct a RemoteViews for the final notification header only
3334 *
3335 * @hide
3336 */
3337 public RemoteViews makeNotificationHeader() {
3338 RemoteViews header = new BuilderRemoteViews(mContext.getApplicationInfo(),
3339 R.layout.notification_template_header);
3340 resetNotificationHeader(header);
3341 bindNotificationHeader(header);
3342 return header;
3343 }
3344
Selim Cinek29603462015-11-17 19:04:39 -08003345 private void hideLine1Text(RemoteViews result) {
Selim Cinekcc10bfb2016-02-10 16:24:21 -08003346 if (result != null) {
3347 result.setViewVisibility(R.id.text_line_1, View.GONE);
3348 }
Selim Cinek29603462015-11-17 19:04:39 -08003349 }
3350
Selim Cinek850a8542015-11-11 11:48:36 -05003351 private void adaptNotificationHeaderForBigContentView(RemoteViews result) {
Selim Cinekcc10bfb2016-02-10 16:24:21 -08003352 if (result != null) {
3353 result.setBoolean(R.id.notification_header, "setExpanded", true);
3354 }
Daniel Sandler96fd7c12012-03-30 16:37:36 -04003355 }
3356
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003357 /**
3358 * Construct a RemoteViews for the final heads-up notification layout.
3359 */
Julia Reynolds3b848122016-02-26 10:45:32 -05003360 public RemoteViews createHeadsUpContentView() {
Selim Cinek593610c2016-02-16 18:42:57 -08003361 if (mN.headsUpContentView != null
3362 && (mStyle == null || !mStyle.displayCustomViewInline())) {
Julia Reynolds089e3e42015-11-18 09:59:57 -05003363 return mN.headsUpContentView;
3364 } else if (mStyle != null) {
3365 final RemoteViews styleView = mStyle.makeHeadsUpContentView();
3366 if (styleView != null) {
3367 return styleView;
3368 }
3369 } else if (mActions.size() == 0) {
3370 return null;
3371 }
3372
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01003373 return applyStandardTemplateWithActions(getBigBaseLayoutResource());
Chris Wren8fd39ec2014-02-27 17:43:26 -05003374 }
3375
Selim Cinek624c02db2015-12-14 21:00:02 -08003376 /**
3377 * Construct a RemoteViews for the display in public contexts like on the lockscreen.
3378 *
3379 * @hide
3380 */
3381 public RemoteViews makePublicContentView() {
3382 if (mN.publicVersion != null) {
3383 final Builder builder = recoverBuilder(mContext, mN.publicVersion);
Julia Reynolds3b848122016-02-26 10:45:32 -05003384 return builder.createContentView();
Selim Cinek624c02db2015-12-14 21:00:02 -08003385 }
3386 Bundle savedBundle = mN.extras;
3387 Style style = mStyle;
3388 mStyle = null;
3389 Icon largeIcon = mN.mLargeIcon;
3390 mN.mLargeIcon = null;
3391 Bundle publicExtras = new Bundle();
3392 publicExtras.putBoolean(EXTRA_SHOW_WHEN,
3393 savedBundle.getBoolean(EXTRA_SHOW_WHEN));
3394 publicExtras.putBoolean(EXTRA_SHOW_CHRONOMETER,
3395 savedBundle.getBoolean(EXTRA_SHOW_CHRONOMETER));
Selim Cinek81c23aa2016-02-25 16:23:13 -08003396 publicExtras.putBoolean(EXTRA_CHRONOMETER_COUNTS_DOWN,
3397 savedBundle.getBoolean(EXTRA_CHRONOMETER_COUNTS_DOWN));
Selim Cinek624c02db2015-12-14 21:00:02 -08003398 publicExtras.putCharSequence(EXTRA_TITLE,
3399 mContext.getString(R.string.notification_hidden_text));
3400 mN.extras = publicExtras;
3401 final RemoteViews publicView = applyStandardTemplate(getBaseLayoutResource());
3402 mN.extras = savedBundle;
3403 mN.mLargeIcon = largeIcon;
3404 mStyle = style;
3405 return publicView;
3406 }
3407
3408
Chris Wren8fd39ec2014-02-27 17:43:26 -05003409
Daniel Sandler96fd7c12012-03-30 16:37:36 -04003410 private RemoteViews generateActionButton(Action action) {
Daniel Sandler8680bf82012-05-15 16:52:52 -04003411 final boolean tombstone = (action.actionIntent == null);
Selim Cinekf33b1112015-07-15 17:45:11 -07003412 RemoteViews button = new BuilderRemoteViews(mContext.getApplicationInfo(),
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01003413 tombstone ? getActionTombstoneLayoutResource()
3414 : getActionLayoutResource());
Dan Sandler68079d52015-07-22 10:45:30 -04003415 final Icon ai = action.getIcon();
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01003416 button.setTextViewText(R.id.action0, processLegacyText(action.title));
Daniel Sandler8680bf82012-05-15 16:52:52 -04003417 if (!tombstone) {
Daniel Sandlere5518842012-05-10 16:20:40 -04003418 button.setOnClickPendingIntent(R.id.action0, action.actionIntent);
Daniel Sandlere5518842012-05-10 16:20:40 -04003419 }
Daniel Sandler96fd7c12012-03-30 16:37:36 -04003420 button.setContentDescription(R.id.action0, action.title);
Adrian Roosfe84e1f2015-11-04 15:55:39 -08003421 if (action.mRemoteInputs != null) {
3422 button.setRemoteInputs(R.id.action0, action.mRemoteInputs);
3423 }
3424 if (mN.color != COLOR_DEFAULT) {
Adrian Roos4ff3b122016-02-01 12:26:13 -08003425 button.setTextColor(R.id.action0, resolveContrastColor());
Adrian Roosfe84e1f2015-11-04 15:55:39 -08003426 }
Daniel Sandler96fd7c12012-03-30 16:37:36 -04003427 return button;
3428 }
3429
Joe Onoratocb109a02011-01-18 17:57:41 -08003430 /**
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01003431 * @return Whether we are currently building a notification from a legacy (an app that
Alan Viverette3cb07a462014-06-06 14:19:53 -07003432 * doesn't create material notifications by itself) app.
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01003433 */
3434 private boolean isLegacy() {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003435 return getColorUtil() != null;
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01003436 }
3437
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01003438 private CharSequence processLegacyText(CharSequence charSequence) {
3439 if (isLegacy()) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003440 return getColorUtil().invertCharSequenceColors(charSequence);
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01003441 } else {
3442 return charSequence;
3443 }
3444 }
3445
Dan Sandler26e81cf2014-05-06 10:01:27 -04003446 /**
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003447 * Apply any necessariy colors to the small icon
Dan Sandler26e81cf2014-05-06 10:01:27 -04003448 */
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003449 private void processSmallIconColor(Icon smallIcon, RemoteViews contentView) {
Selim Cinekea4bef72015-12-02 15:51:10 -08003450 boolean colorable = !isLegacy() || getColorUtil().isGrayscaleIcon(mContext, smallIcon);
3451 if (colorable) {
Adrian Roos4ff3b122016-02-01 12:26:13 -08003452 contentView.setDrawableParameters(R.id.icon, false, -1, resolveContrastColor(),
Jorim Jaggi92df1f22014-12-16 19:44:41 +01003453 PorterDuff.Mode.SRC_ATOP, -1);
Selim Cinekea4bef72015-12-02 15:51:10 -08003454
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01003455 }
Selim Cinekea4bef72015-12-02 15:51:10 -08003456 contentView.setInt(R.id.notification_header, "setOriginalIconColor",
Adrian Roos4ff3b122016-02-01 12:26:13 -08003457 colorable ? resolveContrastColor() : NotificationHeaderView.NO_COLOR);
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01003458 }
3459
Dan Sandler26e81cf2014-05-06 10:01:27 -04003460 /**
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003461 * Make the largeIcon dark if it's a fake smallIcon (that is,
Dan Sandler26e81cf2014-05-06 10:01:27 -04003462 * if it's grayscale).
3463 */
3464 // TODO: also check bounds, transparency, that sort of thing.
Dan Sandlerd63f9322015-05-06 15:18:49 -04003465 private void processLargeLegacyIcon(Icon largeIcon, RemoteViews contentView) {
3466 if (largeIcon != null && isLegacy()
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003467 && getColorUtil().isGrayscaleIcon(mContext, largeIcon)) {
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003468 // resolve color will fall back to the default when legacy
Adrian Roos4ff3b122016-02-01 12:26:13 -08003469 contentView.setDrawableParameters(R.id.icon, false, -1, resolveContrastColor(),
Dan Sandler190d58d2014-05-15 09:33:39 -04003470 PorterDuff.Mode.SRC_ATOP, -1);
Jorim Jaggi92df1f22014-12-16 19:44:41 +01003471 }
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01003472 }
3473
Julia Reynolds10ee1fc2015-11-09 11:04:55 -05003474 private void sanitizeColor() {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003475 if (mN.color != COLOR_DEFAULT) {
3476 mN.color |= 0xFF000000; // no alpha for custom colors
Jorim Jaggi74419312014-06-10 20:57:21 +02003477 }
Jorim Jaggi74419312014-06-10 20:57:21 +02003478 }
3479
Adrian Roos4ff3b122016-02-01 12:26:13 -08003480 int resolveContrastColor() {
3481 if (mCachedContrastColorIsFor == mN.color && mCachedContrastColor != COLOR_INVALID) {
3482 return mCachedContrastColor;
Dan Sandler26e81cf2014-05-06 10:01:27 -04003483 }
Adrian Roos4ff3b122016-02-01 12:26:13 -08003484 final int contrasted = NotificationColorUtil.resolveContrastColor(mContext, mN.color);
3485
3486 mCachedContrastColorIsFor = mN.color;
3487 return mCachedContrastColor = contrasted;
Dan Sandler26e81cf2014-05-06 10:01:27 -04003488 }
3489
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01003490 /**
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003491 * Apply the unstyled operations and return a new {@link Notification} object.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04003492 * @hide
Joe Onoratocb109a02011-01-18 17:57:41 -08003493 */
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04003494 public Notification buildUnstyled() {
Daniel Sandlera0a938c2012-03-15 08:42:37 -04003495 if (mActions.size() > 0) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003496 mN.actions = new Action[mActions.size()];
3497 mActions.toArray(mN.actions);
Daniel Sandlera0a938c2012-03-15 08:42:37 -04003498 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003499 if (!mPersonList.isEmpty()) {
3500 mN.extras.putStringArray(EXTRA_PEOPLE,
3501 mPersonList.toArray(new String[mPersonList.size()]));
Dan Sandler0bf2ed82013-12-21 23:33:41 -06003502 }
Selim Cinek247fa012016-02-18 09:50:48 -08003503 if (mN.bigContentView != null || mN.contentView != null
3504 || mN.headsUpContentView != null) {
3505 mN.extras.putBoolean(EXTRA_CONTAINS_CUSTOM_VIEW, true);
3506 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003507 return mN;
Joe Onorato46439ce2010-11-19 13:56:21 -08003508 }
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003509
Julia Reynolds3b848122016-02-26 10:45:32 -05003510 /**
3511 * Creates a Builder from an existing notification so further changes can be made.
3512 * @param context The context for your application / activity.
3513 * @param n The notification to create a Builder from.
3514 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003515 public static Notification.Builder recoverBuilder(Context context, Notification n) {
Christoph Studer4600f9b2014-07-22 22:44:43 +02003516 // Re-create notification context so we can access app resources.
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003517 ApplicationInfo applicationInfo = n.extras.getParcelable(
3518 EXTRA_BUILDER_APPLICATION_INFO);
Christoph Studer4600f9b2014-07-22 22:44:43 +02003519 Context builderContext;
Julia Reynoldsda303542015-11-23 14:00:20 -05003520 if (applicationInfo != null) {
3521 try {
3522 builderContext = context.createApplicationContext(applicationInfo,
3523 Context.CONTEXT_RESTRICTED);
3524 } catch (NameNotFoundException e) {
3525 Log.e(TAG, "ApplicationInfo " + applicationInfo + " not found");
3526 builderContext = context; // try with our context
3527 }
3528 } else {
3529 builderContext = context; // try with given context
Christoph Studer4600f9b2014-07-22 22:44:43 +02003530 }
3531
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003532 return new Builder(builderContext, n);
Christoph Studer4600f9b2014-07-22 22:44:43 +02003533 }
3534
3535 private static Class<? extends Style> getNotificationStyleClass(String templateClass) {
Selim Cinek593610c2016-02-16 18:42:57 -08003536 Class<? extends Style>[] classes = new Class[] {
3537 BigTextStyle.class, BigPictureStyle.class, InboxStyle.class, MediaStyle.class,
Selim Cinek03eb3b72016-02-18 10:39:45 -08003538 DecoratedCustomViewStyle.class, DecoratedMediaCustomViewStyle.class };
Christoph Studer4600f9b2014-07-22 22:44:43 +02003539 for (Class<? extends Style> innerClass : classes) {
3540 if (templateClass.equals(innerClass.getName())) {
3541 return innerClass;
3542 }
3543 }
3544 return null;
3545 }
3546
Daniel Sandlerf45564e2013-04-15 15:05:08 -04003547 /**
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003548 * @deprecated Use {@link #build()} instead.
3549 */
3550 @Deprecated
3551 public Notification getNotification() {
3552 return build();
3553 }
3554
3555 /**
3556 * Combine all of the options that have been set and return a new {@link Notification}
3557 * object.
3558 */
3559 public Notification build() {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003560 // first, add any extras from the calling code
3561 if (mUserExtras != null) {
3562 mN.extras = getAllExtras();
Jorim Jaggia0d58ae2015-06-03 11:48:13 -07003563 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003564
3565 // lazy stuff from mContext; see comment in Builder(Context, Notification)
Julia Reynoldsda303542015-11-23 14:00:20 -05003566 Notification.addFieldsFromContext(mContext, mN);
Christoph Studer943aa672014-08-03 20:31:16 +02003567
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003568 buildUnstyled();
Daniel Sandlerf45564e2013-04-15 15:05:08 -04003569
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003570 if (mStyle != null) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003571 mStyle.buildStyled(mN);
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003572 }
Daniel Sandlerf45564e2013-04-15 15:05:08 -04003573
Adrian Roos5081c0d2016-02-26 16:04:19 -08003574 if (mContext.getApplicationInfo().targetSdkVersion < Build.VERSION_CODES.N
3575 && (mStyle == null || !mStyle.displayCustomViewInline())) {
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05003576 if (mN.contentView == null) {
Julia Reynolds3b848122016-02-26 10:45:32 -05003577 mN.contentView = createContentView();
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05003578 mN.extras.putInt(EXTRA_REBUILD_CONTENT_VIEW_ACTION_COUNT,
3579 mN.contentView.getSequenceNumber());
3580 }
3581 if (mN.bigContentView == null) {
Julia Reynolds3b848122016-02-26 10:45:32 -05003582 mN.bigContentView = createBigContentView();
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05003583 if (mN.bigContentView != null) {
3584 mN.extras.putInt(EXTRA_REBUILD_BIG_CONTENT_VIEW_ACTION_COUNT,
3585 mN.bigContentView.getSequenceNumber());
3586 }
3587 }
3588 if (mN.headsUpContentView == null) {
Julia Reynolds3b848122016-02-26 10:45:32 -05003589 mN.headsUpContentView = createHeadsUpContentView();
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05003590 if (mN.headsUpContentView != null) {
3591 mN.extras.putInt(EXTRA_REBUILD_HEADS_UP_CONTENT_VIEW_ACTION_COUNT,
3592 mN.headsUpContentView.getSequenceNumber());
3593 }
3594 }
3595 }
3596
Julia Reynolds4c0c2022016-02-02 15:11:59 -05003597 if ((mN.defaults & DEFAULT_LIGHTS) != 0) {
3598 mN.flags |= FLAG_SHOW_LIGHTS;
3599 }
3600
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003601 return mN;
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003602 }
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05003603
3604 /**
3605 * Apply this Builder to an existing {@link Notification} object.
3606 *
3607 * @hide
3608 */
3609 public Notification buildInto(Notification n) {
Daniel Sandler1a497d32013-04-18 14:52:45 -04003610 build().cloneInto(n, true);
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05003611 return n;
3612 }
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01003613
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05003614 /**
Adrian Roos184bfe022016-03-03 13:41:44 -08003615 * Removes RemoteViews that were created for compatibility from {@param n}, if they did not
3616 * change.
3617 *
3618 * @return {@param n}, if no stripping is needed, otherwise a stripped clone of {@param n}.
3619 *
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05003620 * @hide
3621 */
Adrian Roos184bfe022016-03-03 13:41:44 -08003622 public static Notification maybeCloneStrippedForDelivery(Notification n) {
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05003623 String templateClass = n.extras.getString(EXTRA_TEMPLATE);
Adrian Roos184bfe022016-03-03 13:41:44 -08003624
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05003625 // Only strip views for known Styles because we won't know how to
3626 // re-create them otherwise.
Adrian Roos184bfe022016-03-03 13:41:44 -08003627 if (!TextUtils.isEmpty(templateClass)
3628 && getNotificationStyleClass(templateClass) == null) {
3629 return n;
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05003630 }
Adrian Roos184bfe022016-03-03 13:41:44 -08003631
3632 // Only strip unmodified BuilderRemoteViews.
3633 boolean stripContentView = n.contentView instanceof BuilderRemoteViews &&
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05003634 n.extras.getInt(EXTRA_REBUILD_CONTENT_VIEW_ACTION_COUNT, -1) ==
Adrian Roos184bfe022016-03-03 13:41:44 -08003635 n.contentView.getSequenceNumber();
3636 boolean stripBigContentView = n.bigContentView instanceof BuilderRemoteViews &&
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05003637 n.extras.getInt(EXTRA_REBUILD_BIG_CONTENT_VIEW_ACTION_COUNT, -1) ==
Adrian Roos184bfe022016-03-03 13:41:44 -08003638 n.bigContentView.getSequenceNumber();
3639 boolean stripHeadsUpContentView = n.headsUpContentView instanceof BuilderRemoteViews &&
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05003640 n.extras.getInt(EXTRA_REBUILD_HEADS_UP_CONTENT_VIEW_ACTION_COUNT, -1) ==
Adrian Roos184bfe022016-03-03 13:41:44 -08003641 n.headsUpContentView.getSequenceNumber();
3642
3643 // Nothing to do here, no need to clone.
3644 if (!stripContentView && !stripBigContentView && !stripHeadsUpContentView) {
3645 return n;
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05003646 }
Adrian Roos184bfe022016-03-03 13:41:44 -08003647
3648 Notification clone = n.clone();
3649 if (stripContentView) {
3650 clone.contentView = null;
3651 clone.extras.remove(EXTRA_REBUILD_CONTENT_VIEW_ACTION_COUNT);
3652 }
3653 if (stripBigContentView) {
3654 clone.bigContentView = null;
3655 clone.extras.remove(EXTRA_REBUILD_BIG_CONTENT_VIEW_ACTION_COUNT);
3656 }
3657 if (stripHeadsUpContentView) {
3658 clone.headsUpContentView = null;
3659 clone.extras.remove(EXTRA_REBUILD_HEADS_UP_CONTENT_VIEW_ACTION_COUNT);
3660 }
3661 return clone;
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05003662 }
3663
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01003664 private int getBaseLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07003665 return R.layout.notification_template_material_base;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01003666 }
3667
3668 private int getBigBaseLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07003669 return R.layout.notification_template_material_big_base;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01003670 }
3671
3672 private int getBigPictureLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07003673 return R.layout.notification_template_material_big_picture;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01003674 }
3675
3676 private int getBigTextLayoutResource() {
Jorim Jaggi445d3c02014-08-19 22:33:42 +02003677 return R.layout.notification_template_material_big_text;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01003678 }
3679
3680 private int getInboxLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07003681 return R.layout.notification_template_material_inbox;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01003682 }
3683
3684 private int getActionLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07003685 return R.layout.notification_material_action;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01003686 }
3687
3688 private int getActionTombstoneLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07003689 return R.layout.notification_material_action_tombstone;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01003690 }
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003691 }
3692
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003693 /**
3694 * An object that can apply a rich notification style to a {@link Notification.Builder}
3695 * object.
3696 */
Griff Hazendfcb0802014-02-11 12:00:00 -08003697 public static abstract class Style {
Chris Wrend6297db2012-05-03 16:20:13 -04003698 private CharSequence mBigContentTitle;
Jorim Jaggi457a10d2014-09-08 16:18:23 +02003699
3700 /**
3701 * @hide
3702 */
3703 protected CharSequence mSummaryText = null;
3704
3705 /**
3706 * @hide
3707 */
3708 protected boolean mSummaryTextSet = false;
Chris Wrend6297db2012-05-03 16:20:13 -04003709
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003710 protected Builder mBuilder;
3711
Chris Wrend6297db2012-05-03 16:20:13 -04003712 /**
3713 * Overrides ContentTitle in the big form of the template.
3714 * This defaults to the value passed to setContentTitle().
3715 */
3716 protected void internalSetBigContentTitle(CharSequence title) {
3717 mBigContentTitle = title;
3718 }
3719
3720 /**
3721 * Set the first line of text after the detail section in the big form of the template.
3722 */
3723 protected void internalSetSummaryText(CharSequence cs) {
3724 mSummaryText = cs;
Daniel Sandler619738c2012-06-07 16:33:08 -04003725 mSummaryTextSet = true;
Chris Wrend6297db2012-05-03 16:20:13 -04003726 }
3727
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003728 public void setBuilder(Builder builder) {
3729 if (mBuilder != builder) {
3730 mBuilder = builder;
Daniel Sandlerc08dea22012-06-28 08:35:24 -07003731 if (mBuilder != null) {
3732 mBuilder.setStyle(this);
3733 }
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003734 }
3735 }
3736
Chris Wrend6297db2012-05-03 16:20:13 -04003737 protected void checkBuilder() {
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003738 if (mBuilder == null) {
3739 throw new IllegalArgumentException("Style requires a valid Builder object");
3740 }
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003741 }
Chris Wrend6297db2012-05-03 16:20:13 -04003742
3743 protected RemoteViews getStandardView(int layoutId) {
3744 checkBuilder();
3745
Christoph Studer4600f9b2014-07-22 22:44:43 +02003746 // Nasty.
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003747 CharSequence oldBuilderContentTitle =
3748 mBuilder.getAllExtras().getCharSequence(EXTRA_TITLE);
Chris Wrend6297db2012-05-03 16:20:13 -04003749 if (mBigContentTitle != null) {
3750 mBuilder.setContentTitle(mBigContentTitle);
3751 }
3752
Chris Wrend6297db2012-05-03 16:20:13 -04003753 RemoteViews contentView = mBuilder.applyStandardTemplateWithActions(layoutId);
3754
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003755 mBuilder.getAllExtras().putCharSequence(EXTRA_TITLE, oldBuilderContentTitle);
Christoph Studer4600f9b2014-07-22 22:44:43 +02003756
Chris Wrend6297db2012-05-03 16:20:13 -04003757 if (mBigContentTitle != null && mBigContentTitle.equals("")) {
3758 contentView.setViewVisibility(R.id.line1, View.GONE);
Chris Wren67dc9a02012-05-16 01:03:20 -04003759 } else {
3760 contentView.setViewVisibility(R.id.line1, View.VISIBLE);
Chris Wrend6297db2012-05-03 16:20:13 -04003761 }
3762
Chris Wrend6297db2012-05-03 16:20:13 -04003763 return contentView;
3764 }
3765
Daniel Sandlerf45564e2013-04-15 15:05:08 -04003766 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003767 * Construct a Style-specific RemoteViews for the final 1U notification layout.
3768 * The default implementation has nothing additional to add.
3769 * @hide
3770 */
3771 public RemoteViews makeContentView() {
3772 return null;
3773 }
3774
3775 /**
3776 * Construct a Style-specific RemoteViews for the final big notification layout.
3777 * @hide
3778 */
3779 public RemoteViews makeBigContentView() {
3780 return null;
3781 }
3782
3783 /**
3784 * Construct a Style-specific RemoteViews for the final HUN layout.
3785 * @hide
3786 */
3787 public RemoteViews makeHeadsUpContentView() {
3788 return null;
3789 }
3790
3791 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003792 * Apply any style-specific extras to this notification before shipping it out.
Daniel Sandlerf45564e2013-04-15 15:05:08 -04003793 * @hide
3794 */
3795 public void addExtras(Bundle extras) {
3796 if (mSummaryTextSet) {
3797 extras.putCharSequence(EXTRA_SUMMARY_TEXT, mSummaryText);
3798 }
3799 if (mBigContentTitle != null) {
3800 extras.putCharSequence(EXTRA_TITLE_BIG, mBigContentTitle);
3801 }
Chris Wren91ad5632013-06-05 15:05:57 -04003802 extras.putString(EXTRA_TEMPLATE, this.getClass().getName());
Daniel Sandlerf45564e2013-04-15 15:05:08 -04003803 }
3804
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04003805 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003806 * Reconstruct the internal state of this Style object from extras.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04003807 * @hide
3808 */
Christoph Studer4600f9b2014-07-22 22:44:43 +02003809 protected void restoreFromExtras(Bundle extras) {
3810 if (extras.containsKey(EXTRA_SUMMARY_TEXT)) {
3811 mSummaryText = extras.getCharSequence(EXTRA_SUMMARY_TEXT);
3812 mSummaryTextSet = true;
3813 }
3814 if (extras.containsKey(EXTRA_TITLE_BIG)) {
3815 mBigContentTitle = extras.getCharSequence(EXTRA_TITLE_BIG);
3816 }
3817 }
3818
3819
3820 /**
3821 * @hide
3822 */
3823 public Notification buildStyled(Notification wip) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003824 addExtras(wip.extras);
Christoph Studer4600f9b2014-07-22 22:44:43 +02003825 return wip;
3826 }
3827
Daniel Sandler0ec46202015-06-24 01:27:05 -04003828 /**
3829 * @hide
3830 */
Jorim Jaggia0d58ae2015-06-03 11:48:13 -07003831 public void purgeResources() {}
3832
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04003833 /**
3834 * Calls {@link android.app.Notification.Builder#build()} on the Builder this Style is
3835 * attached to.
3836 *
3837 * @return the fully constructed Notification.
3838 */
3839 public Notification build() {
3840 checkBuilder();
3841 return mBuilder.build();
3842 }
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02003843
3844 /**
3845 * @hide
3846 * @return true if the style positions the progress bar on the second line; false if the
3847 * style hides the progress bar
3848 */
3849 protected boolean hasProgress() {
3850 return true;
3851 }
Selim Cinek03d0d652015-11-13 13:18:09 -05003852
3853 /**
3854 * @hide
3855 * @return Whether we should put the summary be put into the notification header
3856 */
3857 public boolean hasSummaryInHeader() {
3858 return true;
3859 }
Selim Cinek593610c2016-02-16 18:42:57 -08003860
3861 /**
3862 * @hide
3863 * @return Whether custom content views are displayed inline in the style
3864 */
3865 public boolean displayCustomViewInline() {
3866 return false;
3867 }
Joe Onorato46439ce2010-11-19 13:56:21 -08003868 }
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003869
3870 /**
Daniel Sandler4dfbe832012-04-11 14:51:46 -04003871 * Helper class for generating large-format notifications that include a large image attachment.
Joe Malin8d40d042012-11-05 11:36:40 -08003872 *
Robert Ly91c5ce32014-06-08 15:37:00 -07003873 * Here's how you'd set the <code>BigPictureStyle</code> on a notification:
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003874 * <pre class="prettyprint">
Robert Ly91c5ce32014-06-08 15:37:00 -07003875 * Notification notif = new Notification.Builder(mContext)
3876 * .setContentTitle(&quot;New photo from &quot; + sender.toString())
3877 * .setContentText(subject)
3878 * .setSmallIcon(R.drawable.new_post)
3879 * .setLargeIcon(aBitmap)
3880 * .setStyle(new Notification.BigPictureStyle()
3881 * .bigPicture(aBigBitmap))
3882 * .build();
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003883 * </pre>
Joe Malin8d40d042012-11-05 11:36:40 -08003884 *
Daniel Sandler4dfbe832012-04-11 14:51:46 -04003885 * @see Notification#bigContentView
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003886 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003887 public static class BigPictureStyle extends Style {
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003888 private Bitmap mPicture;
Dan Sandlerd63f9322015-05-06 15:18:49 -04003889 private Icon mBigLargeIcon;
Chris Wren3745a3d2012-05-22 15:11:52 -04003890 private boolean mBigLargeIconSet = false;
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003891
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003892 public BigPictureStyle() {
3893 }
3894
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003895 public BigPictureStyle(Builder builder) {
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003896 setBuilder(builder);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003897 }
3898
Chris Wrend6297db2012-05-03 16:20:13 -04003899 /**
3900 * Overrides ContentTitle in the big form of the template.
3901 * This defaults to the value passed to setContentTitle().
3902 */
3903 public BigPictureStyle setBigContentTitle(CharSequence title) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04003904 internalSetBigContentTitle(safeCharSequence(title));
Chris Wrend6297db2012-05-03 16:20:13 -04003905 return this;
3906 }
3907
3908 /**
3909 * Set the first line of text after the detail section in the big form of the template.
3910 */
3911 public BigPictureStyle setSummaryText(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04003912 internalSetSummaryText(safeCharSequence(cs));
Chris Wrend6297db2012-05-03 16:20:13 -04003913 return this;
3914 }
3915
Chris Wren0bd664d2012-08-01 13:56:56 -04003916 /**
3917 * Provide the bitmap to be used as the payload for the BigPicture notification.
3918 */
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003919 public BigPictureStyle bigPicture(Bitmap b) {
3920 mPicture = b;
3921 return this;
3922 }
3923
Chris Wren3745a3d2012-05-22 15:11:52 -04003924 /**
Chris Wren3745a3d2012-05-22 15:11:52 -04003925 * Override the large icon when the big notification is shown.
3926 */
3927 public BigPictureStyle bigLargeIcon(Bitmap b) {
Dan Sandlerd63f9322015-05-06 15:18:49 -04003928 return bigLargeIcon(b != null ? Icon.createWithBitmap(b) : null);
3929 }
3930
3931 /**
3932 * Override the large icon when the big notification is shown.
3933 */
3934 public BigPictureStyle bigLargeIcon(Icon icon) {
Chris Wren3745a3d2012-05-22 15:11:52 -04003935 mBigLargeIconSet = true;
Dan Sandlerd63f9322015-05-06 15:18:49 -04003936 mBigLargeIcon = icon;
Chris Wren3745a3d2012-05-22 15:11:52 -04003937 return this;
3938 }
3939
Riley Andrews0394a0c2015-11-03 23:36:52 -08003940 /** @hide */
3941 public static final int MIN_ASHMEM_BITMAP_SIZE = 128 * (1 << 10);
3942
Daniel Sandler0ec46202015-06-24 01:27:05 -04003943 /**
3944 * @hide
3945 */
Jorim Jaggia0d58ae2015-06-03 11:48:13 -07003946 @Override
3947 public void purgeResources() {
3948 super.purgeResources();
Riley Andrews8cee7c12015-11-01 23:36:04 -08003949 if (mPicture != null &&
3950 mPicture.isMutable() &&
Riley Andrews0394a0c2015-11-03 23:36:52 -08003951 mPicture.getAllocationByteCount() >= MIN_ASHMEM_BITMAP_SIZE) {
Jorim Jaggia0d58ae2015-06-03 11:48:13 -07003952 mPicture = mPicture.createAshmemBitmap();
3953 }
3954 if (mBigLargeIcon != null) {
3955 mBigLargeIcon.convertToAshmem();
3956 }
3957 }
Christoph Studer5c510ee2014-12-15 16:32:27 +01003958
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003959 /**
3960 * @hide
3961 */
3962 public RemoteViews makeBigContentView() {
3963 // Replace mN.mLargeIcon with mBigLargeIcon if mBigLargeIconSet
Christoph Studer5c510ee2014-12-15 16:32:27 +01003964 // This covers the following cases:
3965 // 1. mBigLargeIconSet -> mBigLargeIcon (null or non-null) applies, overrides
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003966 // mN.mLargeIcon
3967 // 2. !mBigLargeIconSet -> mN.mLargeIcon applies
Dan Sandlerd63f9322015-05-06 15:18:49 -04003968 Icon oldLargeIcon = null;
Christoph Studer5c510ee2014-12-15 16:32:27 +01003969 if (mBigLargeIconSet) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003970 oldLargeIcon = mBuilder.mN.mLargeIcon;
3971 mBuilder.mN.mLargeIcon = mBigLargeIcon;
Christoph Studer5c510ee2014-12-15 16:32:27 +01003972 }
3973
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01003974 RemoteViews contentView = getStandardView(mBuilder.getBigPictureLayoutResource());
Selim Cinek03d0d652015-11-13 13:18:09 -05003975 if (mSummaryTextSet) {
3976 contentView.setTextViewText(R.id.text, mBuilder.processLegacyText(mSummaryText));
Selim Cinekc848c3a2016-01-13 15:27:30 -08003977 contentView.setViewVisibility(R.id.text, View.VISIBLE);
Selim Cinek03d0d652015-11-13 13:18:09 -05003978 }
Selim Cinek860b6da2015-12-16 19:02:19 -08003979 mBuilder.setContentMinHeight(contentView, mBuilder.mN.mLargeIcon != null);
Selim Cinek53e64a42015-11-16 10:40:56 -08003980
Christoph Studer5c510ee2014-12-15 16:32:27 +01003981 if (mBigLargeIconSet) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003982 mBuilder.mN.mLargeIcon = oldLargeIcon;
Christoph Studer5c510ee2014-12-15 16:32:27 +01003983 }
3984
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003985 contentView.setImageViewBitmap(R.id.big_picture, mPicture);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003986 return contentView;
3987 }
3988
Daniel Sandlerf45564e2013-04-15 15:05:08 -04003989 /**
3990 * @hide
3991 */
3992 public void addExtras(Bundle extras) {
3993 super.addExtras(extras);
3994
3995 if (mBigLargeIconSet) {
3996 extras.putParcelable(EXTRA_LARGE_ICON_BIG, mBigLargeIcon);
3997 }
3998 extras.putParcelable(EXTRA_PICTURE, mPicture);
3999 }
4000
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04004001 /**
4002 * @hide
4003 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004004 @Override
Christoph Studer4600f9b2014-07-22 22:44:43 +02004005 protected void restoreFromExtras(Bundle extras) {
4006 super.restoreFromExtras(extras);
4007
4008 if (extras.containsKey(EXTRA_LARGE_ICON_BIG)) {
Christoph Studer5c510ee2014-12-15 16:32:27 +01004009 mBigLargeIconSet = true;
Christoph Studer4600f9b2014-07-22 22:44:43 +02004010 mBigLargeIcon = extras.getParcelable(EXTRA_LARGE_ICON_BIG);
Chris Wren3745a3d2012-05-22 15:11:52 -04004011 }
Christoph Studer4600f9b2014-07-22 22:44:43 +02004012 mPicture = extras.getParcelable(EXTRA_PICTURE);
4013 }
Selim Cinek03d0d652015-11-13 13:18:09 -05004014
4015 /**
4016 * @hide
4017 */
4018 @Override
4019 public boolean hasSummaryInHeader() {
4020 return false;
4021 }
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004022 }
4023
4024 /**
Daniel Sandler4dfbe832012-04-11 14:51:46 -04004025 * Helper class for generating large-format notifications that include a lot of text.
Joe Malin8d40d042012-11-05 11:36:40 -08004026 *
Robert Ly91c5ce32014-06-08 15:37:00 -07004027 * Here's how you'd set the <code>BigTextStyle</code> on a notification:
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004028 * <pre class="prettyprint">
Robert Ly91c5ce32014-06-08 15:37:00 -07004029 * Notification notif = new Notification.Builder(mContext)
4030 * .setContentTitle(&quot;New mail from &quot; + sender.toString())
4031 * .setContentText(subject)
4032 * .setSmallIcon(R.drawable.new_mail)
4033 * .setLargeIcon(aBitmap)
4034 * .setStyle(new Notification.BigTextStyle()
4035 * .bigText(aVeryLongString))
4036 * .build();
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004037 * </pre>
Joe Malin8d40d042012-11-05 11:36:40 -08004038 *
Daniel Sandler4dfbe832012-04-11 14:51:46 -04004039 * @see Notification#bigContentView
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004040 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004041 public static class BigTextStyle extends Style {
Jorim Jaggi457a10d2014-09-08 16:18:23 +02004042
4043 private static final int MAX_LINES = 13;
Selim Cinek3a2c4b92015-12-17 17:01:17 -08004044 private static final int LINES_CONSUMED_BY_ACTIONS = 4;
Jorim Jaggi457a10d2014-09-08 16:18:23 +02004045
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004046 private CharSequence mBigText;
4047
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004048 public BigTextStyle() {
4049 }
4050
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004051 public BigTextStyle(Builder builder) {
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004052 setBuilder(builder);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004053 }
4054
Chris Wrend6297db2012-05-03 16:20:13 -04004055 /**
4056 * Overrides ContentTitle in the big form of the template.
4057 * This defaults to the value passed to setContentTitle().
4058 */
4059 public BigTextStyle setBigContentTitle(CharSequence title) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04004060 internalSetBigContentTitle(safeCharSequence(title));
Chris Wrend6297db2012-05-03 16:20:13 -04004061 return this;
4062 }
4063
4064 /**
4065 * Set the first line of text after the detail section in the big form of the template.
4066 */
4067 public BigTextStyle setSummaryText(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04004068 internalSetSummaryText(safeCharSequence(cs));
Chris Wrend6297db2012-05-03 16:20:13 -04004069 return this;
4070 }
4071
Chris Wren0bd664d2012-08-01 13:56:56 -04004072 /**
4073 * Provide the longer text to be displayed in the big form of the
4074 * template in place of the content text.
4075 */
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004076 public BigTextStyle bigText(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04004077 mBigText = safeCharSequence(cs);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004078 return this;
4079 }
4080
Daniel Sandlerf45564e2013-04-15 15:05:08 -04004081 /**
4082 * @hide
4083 */
4084 public void addExtras(Bundle extras) {
4085 super.addExtras(extras);
4086
Christoph Studer4600f9b2014-07-22 22:44:43 +02004087 extras.putCharSequence(EXTRA_BIG_TEXT, mBigText);
4088 }
4089
4090 /**
4091 * @hide
4092 */
4093 @Override
4094 protected void restoreFromExtras(Bundle extras) {
4095 super.restoreFromExtras(extras);
4096
4097 mBigText = extras.getCharSequence(EXTRA_BIG_TEXT);
Daniel Sandlerf45564e2013-04-15 15:05:08 -04004098 }
4099
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004100 /**
4101 * @hide
4102 */
4103 public RemoteViews makeBigContentView() {
Christoph Studer4600f9b2014-07-22 22:44:43 +02004104
4105 // Nasty
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004106 CharSequence oldBuilderContentText =
4107 mBuilder.getAllExtras().getCharSequence(EXTRA_TEXT);
4108 mBuilder.getAllExtras().putCharSequence(EXTRA_TEXT, null);
Daniel Sandler916ad912012-06-13 12:17:07 -04004109
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01004110 RemoteViews contentView = getStandardView(mBuilder.getBigTextLayoutResource());
Joe Malin8d40d042012-11-05 11:36:40 -08004111
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004112 mBuilder.getAllExtras().putCharSequence(EXTRA_TEXT, oldBuilderContentText);
Christoph Studer4600f9b2014-07-22 22:44:43 +02004113
Selim Cinek3a2c4b92015-12-17 17:01:17 -08004114 CharSequence bigTextText = mBuilder.processLegacyText(mBigText);
4115 contentView.setTextViewText(R.id.big_text, bigTextText);
4116 contentView.setViewVisibility(R.id.big_text,
4117 TextUtils.isEmpty(bigTextText) ? View.GONE : View.VISIBLE);
Jorim Jaggi457a10d2014-09-08 16:18:23 +02004118 contentView.setInt(R.id.big_text, "setMaxLines", calculateMaxLines());
Selim Cinek4fb12d32015-11-19 18:10:48 -08004119 contentView.setBoolean(R.id.big_text, "setHasImage", mBuilder.mN.mLargeIcon != null);
4120
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004121 return contentView;
4122 }
4123
Jorim Jaggi457a10d2014-09-08 16:18:23 +02004124 private int calculateMaxLines() {
4125 int lineCount = MAX_LINES;
4126 boolean hasActions = mBuilder.mActions.size() > 0;
Jorim Jaggi457a10d2014-09-08 16:18:23 +02004127 if (hasActions) {
4128 lineCount -= LINES_CONSUMED_BY_ACTIONS;
4129 }
Jorim Jaggi457a10d2014-09-08 16:18:23 +02004130 return lineCount;
4131 }
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004132 }
Daniel Sandler879c5e02012-04-17 16:46:51 -04004133
4134 /**
4135 * Helper class for generating large-format notifications that include a list of (up to 5) strings.
Joe Malin8d40d042012-11-05 11:36:40 -08004136 *
Robert Ly91c5ce32014-06-08 15:37:00 -07004137 * Here's how you'd set the <code>InboxStyle</code> on a notification:
Daniel Sandler879c5e02012-04-17 16:46:51 -04004138 * <pre class="prettyprint">
Robert Ly91c5ce32014-06-08 15:37:00 -07004139 * Notification notif = new Notification.Builder(mContext)
4140 * .setContentTitle(&quot;5 New mails from &quot; + sender.toString())
4141 * .setContentText(subject)
4142 * .setSmallIcon(R.drawable.new_mail)
4143 * .setLargeIcon(aBitmap)
4144 * .setStyle(new Notification.InboxStyle()
4145 * .addLine(str1)
4146 * .addLine(str2)
4147 * .setContentTitle(&quot;&quot;)
4148 * .setSummaryText(&quot;+3 more&quot;))
4149 * .build();
Daniel Sandler879c5e02012-04-17 16:46:51 -04004150 * </pre>
Joe Malin8d40d042012-11-05 11:36:40 -08004151 *
Daniel Sandler879c5e02012-04-17 16:46:51 -04004152 * @see Notification#bigContentView
4153 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004154 public static class InboxStyle extends Style {
Daniel Sandler879c5e02012-04-17 16:46:51 -04004155 private ArrayList<CharSequence> mTexts = new ArrayList<CharSequence>(5);
4156
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004157 public InboxStyle() {
4158 }
4159
Daniel Sandler879c5e02012-04-17 16:46:51 -04004160 public InboxStyle(Builder builder) {
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004161 setBuilder(builder);
Daniel Sandler879c5e02012-04-17 16:46:51 -04004162 }
4163
Chris Wrend6297db2012-05-03 16:20:13 -04004164 /**
4165 * Overrides ContentTitle in the big form of the template.
4166 * This defaults to the value passed to setContentTitle().
4167 */
4168 public InboxStyle setBigContentTitle(CharSequence title) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04004169 internalSetBigContentTitle(safeCharSequence(title));
Chris Wrend6297db2012-05-03 16:20:13 -04004170 return this;
4171 }
4172
4173 /**
4174 * Set the first line of text after the detail section in the big form of the template.
4175 */
4176 public InboxStyle setSummaryText(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04004177 internalSetSummaryText(safeCharSequence(cs));
Chris Wrend6297db2012-05-03 16:20:13 -04004178 return this;
4179 }
4180
Chris Wren0bd664d2012-08-01 13:56:56 -04004181 /**
4182 * Append a line to the digest section of the Inbox notification.
4183 */
Daniel Sandler879c5e02012-04-17 16:46:51 -04004184 public InboxStyle addLine(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04004185 mTexts.add(safeCharSequence(cs));
Daniel Sandler879c5e02012-04-17 16:46:51 -04004186 return this;
4187 }
4188
Daniel Sandlerf45564e2013-04-15 15:05:08 -04004189 /**
4190 * @hide
4191 */
4192 public void addExtras(Bundle extras) {
4193 super.addExtras(extras);
Christoph Studer4600f9b2014-07-22 22:44:43 +02004194
Daniel Sandlerf45564e2013-04-15 15:05:08 -04004195 CharSequence[] a = new CharSequence[mTexts.size()];
4196 extras.putCharSequenceArray(EXTRA_TEXT_LINES, mTexts.toArray(a));
4197 }
4198
Christoph Studer4600f9b2014-07-22 22:44:43 +02004199 /**
4200 * @hide
4201 */
4202 @Override
4203 protected void restoreFromExtras(Bundle extras) {
4204 super.restoreFromExtras(extras);
4205
4206 mTexts.clear();
4207 if (extras.containsKey(EXTRA_TEXT_LINES)) {
4208 Collections.addAll(mTexts, extras.getCharSequenceArray(EXTRA_TEXT_LINES));
4209 }
4210 }
4211
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004212 /**
4213 * @hide
4214 */
4215 public RemoteViews makeBigContentView() {
Selim Cinekc848c3a2016-01-13 15:27:30 -08004216 // Remove the content text so it disappears unless you have a summary
Christoph Studer4600f9b2014-07-22 22:44:43 +02004217 // Nasty
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004218 CharSequence oldBuilderContentText = mBuilder.mN.extras.getCharSequence(EXTRA_TEXT);
4219 mBuilder.getAllExtras().putCharSequence(EXTRA_TEXT, null);
Christoph Studer4600f9b2014-07-22 22:44:43 +02004220
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01004221 RemoteViews contentView = getStandardView(mBuilder.getInboxLayoutResource());
Daniel Sandler619738c2012-06-07 16:33:08 -04004222
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004223 mBuilder.getAllExtras().putCharSequence(EXTRA_TEXT, oldBuilderContentText);
Christoph Studer4600f9b2014-07-22 22:44:43 +02004224
Chris Wrend6297db2012-05-03 16:20:13 -04004225 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 -04004226 R.id.inbox_text4, R.id.inbox_text5, R.id.inbox_text6};
Chris Wrend6297db2012-05-03 16:20:13 -04004227
Chris Wren4ed80d52012-05-17 09:30:03 -04004228 // Make sure all rows are gone in case we reuse a view.
4229 for (int rowId : rowIds) {
4230 contentView.setViewVisibility(rowId, View.GONE);
4231 }
4232
Jorim Jaggi445d3c02014-08-19 22:33:42 +02004233 final boolean largeText =
4234 mBuilder.mContext.getResources().getConfiguration().fontScale > 1f;
4235 final float subTextSize = mBuilder.mContext.getResources().getDimensionPixelSize(
4236 R.dimen.notification_subtext_size);
Daniel Sandler879c5e02012-04-17 16:46:51 -04004237 int i=0;
Selim Cinek9d9fc6e2015-11-12 15:49:14 -05004238 final float density = mBuilder.mContext.getResources().getDisplayMetrics().density;
4239 int topPadding = (int) (5 * density);
Selim Cinekc0fac722016-03-07 19:57:06 -08004240 int bottomPadding = mBuilder.mContext.getResources().getDimensionPixelSize(
4241 com.android.internal.R.dimen.notification_content_margin_bottom);
Selim Cinek247fa012016-02-18 09:50:48 -08004242 boolean first = true;
Daniel Sandler879c5e02012-04-17 16:46:51 -04004243 while (i < mTexts.size() && i < rowIds.length) {
4244 CharSequence str = mTexts.get(i);
4245 if (str != null && !str.equals("")) {
4246 contentView.setViewVisibility(rowIds[i], View.VISIBLE);
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01004247 contentView.setTextViewText(rowIds[i], mBuilder.processLegacyText(str));
Jorim Jaggi445d3c02014-08-19 22:33:42 +02004248 if (largeText) {
4249 contentView.setTextViewTextSize(rowIds[i], TypedValue.COMPLEX_UNIT_PX,
4250 subTextSize);
4251 }
Selim Cinek9d9fc6e2015-11-12 15:49:14 -05004252 contentView.setViewPadding(rowIds[i], 0, topPadding, 0,
4253 i == rowIds.length - 1 || i == mTexts.size() - 1 ? bottomPadding : 0);
Selim Cinek247fa012016-02-18 09:50:48 -08004254 handleInboxImageMargin(contentView, rowIds[i], first);
4255 first = false;
Daniel Sandler879c5e02012-04-17 16:46:51 -04004256 }
4257 i++;
4258 }
Kenny Guy98193ea2014-07-24 19:54:37 +01004259
Selim Cinek1e0bf612015-11-20 15:57:26 -08004260
Daniel Sandler879c5e02012-04-17 16:46:51 -04004261 return contentView;
4262 }
Selim Cinek1e0bf612015-11-20 15:57:26 -08004263
Selim Cinek247fa012016-02-18 09:50:48 -08004264 private void handleInboxImageMargin(RemoteViews contentView, int id, boolean first) {
Selim Cinek1e0bf612015-11-20 15:57:26 -08004265 int endMargin = 0;
Selim Cinek247fa012016-02-18 09:50:48 -08004266 if (first) {
4267 final int max = mBuilder.mN.extras.getInt(EXTRA_PROGRESS_MAX, 0);
4268 final boolean ind = mBuilder.mN.extras.getBoolean(EXTRA_PROGRESS_INDETERMINATE);
4269 boolean hasProgress = max != 0 || ind;
4270 if (mBuilder.mN.mLargeIcon != null && !hasProgress) {
4271 endMargin = mBuilder.mContext.getResources().getDimensionPixelSize(
4272 R.dimen.notification_content_picture_margin);
4273 }
Selim Cinek1e0bf612015-11-20 15:57:26 -08004274 }
4275 contentView.setViewLayoutMarginEnd(id, endMargin);
4276 }
Daniel Sandler879c5e02012-04-17 16:46:51 -04004277 }
Dan Sandler842dd772014-05-15 09:36:47 -04004278
4279 /**
4280 * Notification style for media playback notifications.
4281 *
4282 * In the expanded form, {@link Notification#bigContentView}, up to 5
4283 * {@link Notification.Action}s specified with
Dan Sandler86647982015-05-13 23:41:13 -04004284 * {@link Notification.Builder#addAction(Action) addAction} will be
Dan Sandler842dd772014-05-15 09:36:47 -04004285 * shown as icon-only pushbuttons, suitable for transport controls. The Bitmap given to
4286 * {@link Notification.Builder#setLargeIcon(android.graphics.Bitmap) setLargeIcon()} will be
4287 * treated as album artwork.
4288 *
4289 * Unlike the other styles provided here, MediaStyle can also modify the standard-size
4290 * {@link Notification#contentView}; by providing action indices to
Christoph Studerfde6f4d2014-12-12 13:23:26 +01004291 * {@link #setShowActionsInCompactView(int...)} you can promote up to 3 actions to be displayed
Dan Sandler842dd772014-05-15 09:36:47 -04004292 * in the standard view alongside the usual content.
4293 *
Bryan Mawhinney6be8de32014-07-18 10:35:12 +01004294 * Notifications created with MediaStyle will have their category set to
4295 * {@link Notification#CATEGORY_TRANSPORT CATEGORY_TRANSPORT} unless you set a different
4296 * category using {@link Notification.Builder#setCategory(String) setCategory()}.
4297 *
Jeff Browndba34ba2014-06-24 20:46:03 -07004298 * Finally, if you attach a {@link android.media.session.MediaSession.Token} using
4299 * {@link android.app.Notification.MediaStyle#setMediaSession(MediaSession.Token)},
Dan Sandler842dd772014-05-15 09:36:47 -04004300 * the System UI can identify this as a notification representing an active media session
4301 * and respond accordingly (by showing album artwork in the lockscreen, for example).
4302 *
4303 * To use this style with your Notification, feed it to
4304 * {@link Notification.Builder#setStyle(android.app.Notification.Style)} like so:
4305 * <pre class="prettyprint">
4306 * Notification noti = new Notification.Builder()
4307 * .setSmallIcon(R.drawable.ic_stat_player)
Christoph Studere935fe92014-11-24 14:18:06 +01004308 * .setContentTitle(&quot;Track title&quot;)
4309 * .setContentText(&quot;Artist - Album&quot;)
4310 * .setLargeIcon(albumArtBitmap))
Bryan Mawhinney6be8de32014-07-18 10:35:12 +01004311 * .setStyle(<b>new Notification.MediaStyle()</b>
4312 * .setMediaSession(mySession))
Dan Sandler842dd772014-05-15 09:36:47 -04004313 * .build();
4314 * </pre>
4315 *
4316 * @see Notification#bigContentView
4317 */
4318 public static class MediaStyle extends Style {
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02004319 static final int MAX_MEDIA_BUTTONS_IN_COMPACT = 3;
Dan Sandler842dd772014-05-15 09:36:47 -04004320 static final int MAX_MEDIA_BUTTONS = 5;
4321
4322 private int[] mActionsToShowInCompact = null;
Jeff Browndba34ba2014-06-24 20:46:03 -07004323 private MediaSession.Token mToken;
Dan Sandler842dd772014-05-15 09:36:47 -04004324
4325 public MediaStyle() {
4326 }
4327
4328 public MediaStyle(Builder builder) {
4329 setBuilder(builder);
4330 }
4331
4332 /**
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02004333 * 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 -04004334 * notification view.
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02004335 *
4336 * @param actions the indices of the actions to show in the compact notification view
Dan Sandler842dd772014-05-15 09:36:47 -04004337 */
4338 public MediaStyle setShowActionsInCompactView(int...actions) {
4339 mActionsToShowInCompact = actions;
4340 return this;
4341 }
4342
4343 /**
Jeff Browndba34ba2014-06-24 20:46:03 -07004344 * Attach a {@link android.media.session.MediaSession.Token} to this Notification
4345 * to provide additional playback information and control to the SystemUI.
Dan Sandler842dd772014-05-15 09:36:47 -04004346 */
Jeff Browndba34ba2014-06-24 20:46:03 -07004347 public MediaStyle setMediaSession(MediaSession.Token token) {
Dan Sandler842dd772014-05-15 09:36:47 -04004348 mToken = token;
4349 return this;
4350 }
4351
Christoph Studer4600f9b2014-07-22 22:44:43 +02004352 /**
4353 * @hide
4354 */
Dan Sandler842dd772014-05-15 09:36:47 -04004355 @Override
4356 public Notification buildStyled(Notification wip) {
Christoph Studer4600f9b2014-07-22 22:44:43 +02004357 super.buildStyled(wip);
Bryan Mawhinney6be8de32014-07-18 10:35:12 +01004358 if (wip.category == null) {
4359 wip.category = Notification.CATEGORY_TRANSPORT;
4360 }
Dan Sandler842dd772014-05-15 09:36:47 -04004361 return wip;
4362 }
4363
Christoph Studer4600f9b2014-07-22 22:44:43 +02004364 /**
4365 * @hide
4366 */
4367 @Override
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004368 public RemoteViews makeContentView() {
4369 return makeMediaContentView();
Christoph Studer4600f9b2014-07-22 22:44:43 +02004370 }
4371
4372 /**
4373 * @hide
4374 */
4375 @Override
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004376 public RemoteViews makeBigContentView() {
4377 return makeMediaBigContentView();
Christoph Studer4600f9b2014-07-22 22:44:43 +02004378 }
4379
Selim Cinekcc10bfb2016-02-10 16:24:21 -08004380 /**
4381 * @hide
4382 */
4383 @Override
4384 public RemoteViews makeHeadsUpContentView() {
4385 RemoteViews expanded = makeMediaBigContentView();
4386 return expanded != null ? expanded : makeMediaContentView();
4387 }
4388
Dan Sandler842dd772014-05-15 09:36:47 -04004389 /** @hide */
4390 @Override
4391 public void addExtras(Bundle extras) {
4392 super.addExtras(extras);
4393
4394 if (mToken != null) {
4395 extras.putParcelable(EXTRA_MEDIA_SESSION, mToken);
4396 }
Bryan Mawhinneye191f902014-07-22 12:50:09 +01004397 if (mActionsToShowInCompact != null) {
4398 extras.putIntArray(EXTRA_COMPACT_ACTIONS, mActionsToShowInCompact);
4399 }
Dan Sandler842dd772014-05-15 09:36:47 -04004400 }
4401
Christoph Studer4600f9b2014-07-22 22:44:43 +02004402 /**
4403 * @hide
4404 */
4405 @Override
4406 protected void restoreFromExtras(Bundle extras) {
4407 super.restoreFromExtras(extras);
4408
4409 if (extras.containsKey(EXTRA_MEDIA_SESSION)) {
4410 mToken = extras.getParcelable(EXTRA_MEDIA_SESSION);
4411 }
4412 if (extras.containsKey(EXTRA_COMPACT_ACTIONS)) {
4413 mActionsToShowInCompact = extras.getIntArray(EXTRA_COMPACT_ACTIONS);
4414 }
4415 }
4416
Selim Cinek5bf069a2015-11-10 19:14:27 -05004417 private RemoteViews generateMediaActionButton(Action action, int color) {
Dan Sandler842dd772014-05-15 09:36:47 -04004418 final boolean tombstone = (action.actionIntent == null);
Selim Cinekf33b1112015-07-15 17:45:11 -07004419 RemoteViews button = new BuilderRemoteViews(mBuilder.mContext.getApplicationInfo(),
Alan Viverette3cb07a462014-06-06 14:19:53 -07004420 R.layout.notification_material_media_action);
Dan Sandler68079d52015-07-22 10:45:30 -04004421 button.setImageViewIcon(R.id.action0, action.getIcon());
Selim Cinek5bf069a2015-11-10 19:14:27 -05004422 button.setDrawableParameters(R.id.action0, false, -1, color, PorterDuff.Mode.SRC_ATOP,
4423 -1);
Dan Sandler842dd772014-05-15 09:36:47 -04004424 if (!tombstone) {
4425 button.setOnClickPendingIntent(R.id.action0, action.actionIntent);
4426 }
4427 button.setContentDescription(R.id.action0, action.title);
4428 return button;
4429 }
4430
4431 private RemoteViews makeMediaContentView() {
4432 RemoteViews view = mBuilder.applyStandardTemplate(
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02004433 R.layout.notification_template_material_media, false /* hasProgress */);
Dan Sandler842dd772014-05-15 09:36:47 -04004434
4435 final int numActions = mBuilder.mActions.size();
4436 final int N = mActionsToShowInCompact == null
4437 ? 0
4438 : Math.min(mActionsToShowInCompact.length, MAX_MEDIA_BUTTONS_IN_COMPACT);
4439 if (N > 0) {
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02004440 view.removeAllViews(com.android.internal.R.id.media_actions);
Dan Sandler842dd772014-05-15 09:36:47 -04004441 for (int i = 0; i < N; i++) {
4442 if (i >= numActions) {
4443 throw new IllegalArgumentException(String.format(
4444 "setShowActionsInCompactView: action %d out of bounds (max %d)",
4445 i, numActions - 1));
4446 }
4447
4448 final Action action = mBuilder.mActions.get(mActionsToShowInCompact[i]);
Selim Cinek5bf069a2015-11-10 19:14:27 -05004449 final RemoteViews button = generateMediaActionButton(action,
Adrian Roos4ff3b122016-02-01 12:26:13 -08004450 mBuilder.resolveContrastColor());
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02004451 view.addView(com.android.internal.R.id.media_actions, button);
Dan Sandler842dd772014-05-15 09:36:47 -04004452 }
4453 }
Selim Cinekfdc738f2016-01-27 20:04:27 -08004454 handleImage(view);
4455 // handle the content margin
Selim Cinek247fa012016-02-18 09:50:48 -08004456 int endMargin = mBuilder.mContext.getResources().getDimensionPixelSize(
4457 R.dimen.notification_content_margin_end);;
Selim Cinekfdc738f2016-01-27 20:04:27 -08004458 if (mBuilder.mN.mLargeIcon != null) {
Selim Cinek247fa012016-02-18 09:50:48 -08004459 endMargin += mBuilder.mContext.getResources().getDimensionPixelSize(
4460 R.dimen.notification_content_picture_margin);
Selim Cinekfdc738f2016-01-27 20:04:27 -08004461 }
4462 view.setViewLayoutMarginEnd(R.id.notification_main_column, endMargin);
Dan Sandler842dd772014-05-15 09:36:47 -04004463 return view;
4464 }
4465
4466 private RemoteViews makeMediaBigContentView() {
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02004467 final int actionCount = Math.min(mBuilder.mActions.size(), MAX_MEDIA_BUTTONS);
Selim Cinekcc10bfb2016-02-10 16:24:21 -08004468 // Dont add an expanded view if there is no more content to be revealed
4469 int actionsInCompact = mActionsToShowInCompact == null
4470 ? 0
4471 : Math.min(mActionsToShowInCompact.length, MAX_MEDIA_BUTTONS_IN_COMPACT);
4472 if (mBuilder.mN.mLargeIcon == null && actionCount <= actionsInCompact) {
4473 return null;
4474 }
Selim Cinek5bf069a2015-11-10 19:14:27 -05004475 RemoteViews big = mBuilder.applyStandardTemplate(
4476 R.layout.notification_template_material_big_media,
4477 false);
Dan Sandler842dd772014-05-15 09:36:47 -04004478
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02004479 if (actionCount > 0) {
4480 big.removeAllViews(com.android.internal.R.id.media_actions);
4481 for (int i = 0; i < actionCount; i++) {
Selim Cinek5bf069a2015-11-10 19:14:27 -05004482 final RemoteViews button = generateMediaActionButton(mBuilder.mActions.get(i),
Adrian Roos4ff3b122016-02-01 12:26:13 -08004483 mBuilder.resolveContrastColor());
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02004484 big.addView(com.android.internal.R.id.media_actions, button);
Dan Sandler842dd772014-05-15 09:36:47 -04004485 }
4486 }
Selim Cinek5bf069a2015-11-10 19:14:27 -05004487 handleImage(big);
Dan Sandler842dd772014-05-15 09:36:47 -04004488 return big;
4489 }
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02004490
Selim Cinek5bf069a2015-11-10 19:14:27 -05004491 private void handleImage(RemoteViews contentView) {
4492 if (mBuilder.mN.mLargeIcon != null) {
4493 contentView.setViewLayoutMarginEnd(R.id.line1, 0);
Selim Cinekc848c3a2016-01-13 15:27:30 -08004494 contentView.setViewLayoutMarginEnd(R.id.text, 0);
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02004495 }
4496 }
4497
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02004498 /**
4499 * @hide
4500 */
4501 @Override
4502 protected boolean hasProgress() {
4503 return false;
4504 }
Dan Sandler842dd772014-05-15 09:36:47 -04004505 }
Griff Hazen61a9e862014-05-22 16:05:19 -07004506
Selim Cinek593610c2016-02-16 18:42:57 -08004507 /**
4508 * Notification style for custom views that are decorated by the system
4509 *
4510 * <p>Instead of providing a notification that is completely custom, a developer can set this
4511 * style and still obtain system decorations like the notification header with the expand
4512 * affordance and actions.
4513 *
4514 * <p>Use {@link android.app.Notification.Builder#setCustomContentView(RemoteViews)},
4515 * {@link android.app.Notification.Builder#setCustomBigContentView(RemoteViews)} and
4516 * {@link android.app.Notification.Builder#setCustomHeadsUpContentView(RemoteViews)} to set the
4517 * corresponding custom views to display.
4518 *
4519 * To use this style with your Notification, feed it to
4520 * {@link Notification.Builder#setStyle(android.app.Notification.Style)} like so:
4521 * <pre class="prettyprint">
4522 * Notification noti = new Notification.Builder()
4523 * .setSmallIcon(R.drawable.ic_stat_player)
4524 * .setLargeIcon(albumArtBitmap))
4525 * .setCustomContentView(contentView);
4526 * .setStyle(<b>new Notification.DecoratedCustomViewStyle()</b>)
4527 * .build();
4528 * </pre>
4529 */
4530 public static class DecoratedCustomViewStyle extends Style {
4531
4532 public DecoratedCustomViewStyle() {
4533 }
4534
4535 public DecoratedCustomViewStyle(Builder builder) {
4536 setBuilder(builder);
4537 }
4538
4539 /**
4540 * @hide
4541 */
4542 public boolean displayCustomViewInline() {
4543 return true;
4544 }
4545
4546 /**
4547 * @hide
4548 */
4549 @Override
4550 public RemoteViews makeContentView() {
4551 return makeStandardTemplateWithCustomContent(mBuilder.mN.contentView);
4552 }
4553
4554 /**
4555 * @hide
4556 */
4557 @Override
4558 public RemoteViews makeBigContentView() {
4559 return makeDecoratedBigContentView();
4560 }
4561
4562 /**
4563 * @hide
4564 */
4565 @Override
4566 public RemoteViews makeHeadsUpContentView() {
4567 return makeDecoratedHeadsUpContentView();
4568 }
4569
Selim Cinek593610c2016-02-16 18:42:57 -08004570 private RemoteViews makeDecoratedHeadsUpContentView() {
4571 RemoteViews headsUpContentView = mBuilder.mN.headsUpContentView == null
4572 ? mBuilder.mN.contentView
4573 : mBuilder.mN.headsUpContentView;
4574 if (mBuilder.mActions.size() == 0) {
4575 return makeStandardTemplateWithCustomContent(headsUpContentView);
4576 }
4577 RemoteViews remoteViews = mBuilder.applyStandardTemplateWithActions(
4578 mBuilder.getBigBaseLayoutResource());
Selim Cinek247fa012016-02-18 09:50:48 -08004579 buildIntoRemoteViewContent(remoteViews, headsUpContentView);
Selim Cinek593610c2016-02-16 18:42:57 -08004580 return remoteViews;
4581 }
4582
Selim Cinek593610c2016-02-16 18:42:57 -08004583 private RemoteViews makeStandardTemplateWithCustomContent(RemoteViews customContent) {
4584 RemoteViews remoteViews = mBuilder.applyStandardTemplate(
4585 mBuilder.getBaseLayoutResource());
Selim Cinek247fa012016-02-18 09:50:48 -08004586 buildIntoRemoteViewContent(remoteViews, customContent);
Selim Cinek593610c2016-02-16 18:42:57 -08004587 return remoteViews;
4588 }
4589
Selim Cinek593610c2016-02-16 18:42:57 -08004590 private RemoteViews makeDecoratedBigContentView() {
4591 RemoteViews bigContentView = mBuilder.mN.bigContentView == null
4592 ? mBuilder.mN.contentView
4593 : mBuilder.mN.bigContentView;
4594 if (mBuilder.mActions.size() == 0) {
4595 return makeStandardTemplateWithCustomContent(bigContentView);
4596 }
4597 RemoteViews remoteViews = mBuilder.applyStandardTemplateWithActions(
4598 mBuilder.getBigBaseLayoutResource());
Selim Cinek247fa012016-02-18 09:50:48 -08004599 buildIntoRemoteViewContent(remoteViews, bigContentView);
Selim Cinek593610c2016-02-16 18:42:57 -08004600 return remoteViews;
4601 }
Selim Cinek247fa012016-02-18 09:50:48 -08004602
4603 private void buildIntoRemoteViewContent(RemoteViews remoteViews,
4604 RemoteViews customContent) {
Adrian Roos5081c0d2016-02-26 16:04:19 -08004605 if (customContent != null) {
Selim Cinekf91017e2016-03-14 12:25:09 -07004606 // Need to clone customContent before adding, because otherwise it can no longer be
4607 // parceled independently of remoteViews.
Adrian Roos5081c0d2016-02-26 16:04:19 -08004608 customContent = customContent.clone();
Selim Cinekf91017e2016-03-14 12:25:09 -07004609 remoteViews.removeAllViews(R.id.notification_main_column);
4610 remoteViews.addView(R.id.notification_main_column, customContent);
Adrian Roos5081c0d2016-02-26 16:04:19 -08004611 }
Selim Cinek247fa012016-02-18 09:50:48 -08004612 // also update the end margin if there is an image
4613 int endMargin = mBuilder.mContext.getResources().getDimensionPixelSize(
4614 R.dimen.notification_content_margin_end);
4615 if (mBuilder.mN.mLargeIcon != null) {
4616 endMargin += mBuilder.mContext.getResources().getDimensionPixelSize(
4617 R.dimen.notification_content_picture_margin);
4618 }
4619 remoteViews.setViewLayoutMarginEnd(R.id.notification_main_column, endMargin);
4620 }
Selim Cinek593610c2016-02-16 18:42:57 -08004621 }
4622
Selim Cinek03eb3b72016-02-18 10:39:45 -08004623 /**
4624 * Notification style for media custom views that are decorated by the system
4625 *
4626 * <p>Instead of providing a media notification that is completely custom, a developer can set
4627 * this style and still obtain system decorations like the notification header with the expand
4628 * affordance and actions.
4629 *
4630 * <p>Use {@link android.app.Notification.Builder#setCustomContentView(RemoteViews)},
4631 * {@link android.app.Notification.Builder#setCustomBigContentView(RemoteViews)} and
4632 * {@link android.app.Notification.Builder#setCustomHeadsUpContentView(RemoteViews)} to set the
4633 * corresponding custom views to display.
4634 *
4635 * To use this style with your Notification, feed it to
4636 * {@link Notification.Builder#setStyle(android.app.Notification.Style)} like so:
4637 * <pre class="prettyprint">
4638 * Notification noti = new Notification.Builder()
4639 * .setSmallIcon(R.drawable.ic_stat_player)
4640 * .setLargeIcon(albumArtBitmap))
4641 * .setCustomContentView(contentView);
4642 * .setStyle(<b>new Notification.DecoratedMediaCustomViewStyle()</b>
4643 * .setMediaSession(mySession))
4644 * .build();
4645 * </pre>
4646 *
4647 * @see android.app.Notification.DecoratedCustomViewStyle
4648 * @see android.app.Notification.MediaStyle
4649 */
4650 public static class DecoratedMediaCustomViewStyle extends MediaStyle {
4651
4652 public DecoratedMediaCustomViewStyle() {
4653 }
4654
4655 public DecoratedMediaCustomViewStyle(Builder builder) {
4656 setBuilder(builder);
4657 }
4658
4659 /**
4660 * @hide
4661 */
4662 public boolean displayCustomViewInline() {
4663 return true;
4664 }
4665
4666 /**
4667 * @hide
4668 */
4669 @Override
4670 public RemoteViews makeContentView() {
4671 RemoteViews remoteViews = super.makeContentView();
4672 return buildIntoRemoteView(remoteViews, R.id.notification_content_container,
4673 mBuilder.mN.contentView);
4674 }
4675
4676 /**
4677 * @hide
4678 */
4679 @Override
4680 public RemoteViews makeBigContentView() {
4681 RemoteViews customRemoteView = mBuilder.mN.bigContentView != null
4682 ? mBuilder.mN.bigContentView
4683 : mBuilder.mN.contentView;
4684 return makeBigContentViewWithCustomContent(customRemoteView);
4685 }
4686
4687 private RemoteViews makeBigContentViewWithCustomContent(RemoteViews customRemoteView) {
4688 RemoteViews remoteViews = super.makeBigContentView();
4689 if (remoteViews != null) {
4690 return buildIntoRemoteView(remoteViews, R.id.notification_main_column,
4691 customRemoteView);
4692 } else if (customRemoteView != mBuilder.mN.contentView){
4693 remoteViews = super.makeContentView();
4694 return buildIntoRemoteView(remoteViews, R.id.notification_content_container,
4695 customRemoteView);
4696 } else {
4697 return null;
4698 }
4699 }
4700
4701 /**
4702 * @hide
4703 */
4704 @Override
4705 public RemoteViews makeHeadsUpContentView() {
4706 RemoteViews customRemoteView = mBuilder.mN.headsUpContentView != null
4707 ? mBuilder.mN.headsUpContentView
4708 : mBuilder.mN.contentView;
4709 return makeBigContentViewWithCustomContent(customRemoteView);
4710 }
4711
4712 private RemoteViews buildIntoRemoteView(RemoteViews remoteViews, int id,
4713 RemoteViews customContent) {
Adrian Roos5081c0d2016-02-26 16:04:19 -08004714 if (customContent != null) {
Selim Cinekf91017e2016-03-14 12:25:09 -07004715 // Need to clone customContent before adding, because otherwise it can no longer be
4716 // parceled independently of remoteViews.
Adrian Roos5081c0d2016-02-26 16:04:19 -08004717 customContent = customContent.clone();
Selim Cinekf91017e2016-03-14 12:25:09 -07004718 remoteViews.removeAllViews(id);
4719 remoteViews.addView(id, customContent);
Adrian Roos5081c0d2016-02-26 16:04:19 -08004720 }
Selim Cinek03eb3b72016-02-18 10:39:45 -08004721 return remoteViews;
4722 }
4723 }
4724
Christoph Studer4600f9b2014-07-22 22:44:43 +02004725 // When adding a new Style subclass here, don't forget to update
4726 // Builder.getNotificationStyleClass.
4727
Griff Hazen61a9e862014-05-22 16:05:19 -07004728 /**
4729 * Extender interface for use with {@link Builder#extend}. Extenders may be used to add
4730 * metadata or change options on a notification builder.
4731 */
4732 public interface Extender {
4733 /**
4734 * Apply this extender to a notification builder.
4735 * @param builder the builder to be modified.
4736 * @return the build object for chaining.
4737 */
4738 public Builder extend(Builder builder);
4739 }
4740
4741 /**
4742 * Helper class to add wearable extensions to notifications.
4743 * <p class="note"> See
4744 * <a href="{@docRoot}wear/notifications/creating.html">Creating Notifications
4745 * for Android Wear</a> for more information on how to use this class.
4746 * <p>
4747 * To create a notification with wearable extensions:
4748 * <ol>
4749 * <li>Create a {@link android.app.Notification.Builder}, setting any desired
4750 * properties.
4751 * <li>Create a {@link android.app.Notification.WearableExtender}.
4752 * <li>Set wearable-specific properties using the
4753 * {@code add} and {@code set} methods of {@link android.app.Notification.WearableExtender}.
4754 * <li>Call {@link android.app.Notification.Builder#extend} to apply the extensions to a
4755 * notification.
4756 * <li>Post the notification to the notification system with the
4757 * {@code NotificationManager.notify(...)} methods.
4758 * </ol>
4759 *
4760 * <pre class="prettyprint">
4761 * Notification notif = new Notification.Builder(mContext)
4762 * .setContentTitle(&quot;New mail from &quot; + sender.toString())
4763 * .setContentText(subject)
4764 * .setSmallIcon(R.drawable.new_mail)
4765 * .extend(new Notification.WearableExtender()
4766 * .setContentIcon(R.drawable.new_mail))
4767 * .build();
4768 * NotificationManager notificationManger =
4769 * (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
4770 * notificationManger.notify(0, notif);</pre>
4771 *
4772 * <p>Wearable extensions can be accessed on an existing notification by using the
4773 * {@code WearableExtender(Notification)} constructor,
4774 * and then using the {@code get} methods to access values.
4775 *
4776 * <pre class="prettyprint">
4777 * Notification.WearableExtender wearableExtender = new Notification.WearableExtender(
4778 * notification);
Griff Hazen14f57992014-05-26 09:07:14 -07004779 * List&lt;Notification&gt; pages = wearableExtender.getPages();</pre>
Griff Hazen61a9e862014-05-22 16:05:19 -07004780 */
4781 public static final class WearableExtender implements Extender {
4782 /**
4783 * Sentinel value for an action index that is unset.
4784 */
4785 public static final int UNSET_ACTION_INDEX = -1;
4786
4787 /**
4788 * Size value for use with {@link #setCustomSizePreset} to show this notification with
4789 * default sizing.
4790 * <p>For custom display notifications created using {@link #setDisplayIntent},
Paul Soulosaa4f4bf2015-08-04 11:59:45 -07004791 * the default is {@link #SIZE_MEDIUM}. All other notifications size automatically based
Griff Hazen61a9e862014-05-22 16:05:19 -07004792 * on their content.
4793 */
4794 public static final int SIZE_DEFAULT = 0;
4795
4796 /**
4797 * Size value for use with {@link #setCustomSizePreset} to show this notification
4798 * with an extra small size.
4799 * <p>This value is only applicable for custom display notifications created using
4800 * {@link #setDisplayIntent}.
4801 */
4802 public static final int SIZE_XSMALL = 1;
4803
4804 /**
4805 * Size value for use with {@link #setCustomSizePreset} to show this notification
4806 * with a small size.
4807 * <p>This value is only applicable for custom display notifications created using
4808 * {@link #setDisplayIntent}.
4809 */
4810 public static final int SIZE_SMALL = 2;
4811
4812 /**
4813 * Size value for use with {@link #setCustomSizePreset} to show this notification
4814 * with a medium size.
4815 * <p>This value is only applicable for custom display notifications created using
4816 * {@link #setDisplayIntent}.
4817 */
4818 public static final int SIZE_MEDIUM = 3;
4819
4820 /**
4821 * Size value for use with {@link #setCustomSizePreset} to show this notification
4822 * with a large size.
4823 * <p>This value is only applicable for custom display notifications created using
4824 * {@link #setDisplayIntent}.
4825 */
4826 public static final int SIZE_LARGE = 4;
4827
Griff Hazend5f11f92014-05-27 15:40:09 -07004828 /**
4829 * Size value for use with {@link #setCustomSizePreset} to show this notification
4830 * full screen.
4831 * <p>This value is only applicable for custom display notifications created using
4832 * {@link #setDisplayIntent}.
4833 */
4834 public static final int SIZE_FULL_SCREEN = 5;
4835
Griff Hazen5f2edfc2014-09-29 16:28:44 -07004836 /**
4837 * Sentinel value for use with {@link #setHintScreenTimeout} to keep the screen on for a
4838 * short amount of time when this notification is displayed on the screen. This
4839 * is the default value.
4840 */
4841 public static final int SCREEN_TIMEOUT_SHORT = 0;
4842
4843 /**
4844 * Sentinel value for use with {@link #setHintScreenTimeout} to keep the screen on
4845 * for a longer amount of time when this notification is displayed on the screen.
4846 */
4847 public static final int SCREEN_TIMEOUT_LONG = -1;
4848
Griff Hazen61a9e862014-05-22 16:05:19 -07004849 /** Notification extra which contains wearable extensions */
4850 private static final String EXTRA_WEARABLE_EXTENSIONS = "android.wearable.EXTENSIONS";
4851
Pete Gastaf6781d2014-10-07 15:17:05 -04004852 // Keys within EXTRA_WEARABLE_EXTENSIONS for wearable options.
Griff Hazen61a9e862014-05-22 16:05:19 -07004853 private static final String KEY_ACTIONS = "actions";
4854 private static final String KEY_FLAGS = "flags";
4855 private static final String KEY_DISPLAY_INTENT = "displayIntent";
4856 private static final String KEY_PAGES = "pages";
4857 private static final String KEY_BACKGROUND = "background";
4858 private static final String KEY_CONTENT_ICON = "contentIcon";
4859 private static final String KEY_CONTENT_ICON_GRAVITY = "contentIconGravity";
4860 private static final String KEY_CONTENT_ACTION_INDEX = "contentActionIndex";
4861 private static final String KEY_CUSTOM_SIZE_PRESET = "customSizePreset";
4862 private static final String KEY_CUSTOM_CONTENT_HEIGHT = "customContentHeight";
4863 private static final String KEY_GRAVITY = "gravity";
Griff Hazen5f2edfc2014-09-29 16:28:44 -07004864 private static final String KEY_HINT_SCREEN_TIMEOUT = "hintScreenTimeout";
Griff Hazen61a9e862014-05-22 16:05:19 -07004865
4866 // Flags bitwise-ored to mFlags
4867 private static final int FLAG_CONTENT_INTENT_AVAILABLE_OFFLINE = 0x1;
4868 private static final int FLAG_HINT_HIDE_ICON = 1 << 1;
4869 private static final int FLAG_HINT_SHOW_BACKGROUND_ONLY = 1 << 2;
4870 private static final int FLAG_START_SCROLL_BOTTOM = 1 << 3;
Griff Hazen5f2edfc2014-09-29 16:28:44 -07004871 private static final int FLAG_HINT_AVOID_BACKGROUND_CLIPPING = 1 << 4;
Griff Hazen61a9e862014-05-22 16:05:19 -07004872
4873 // Default value for flags integer
4874 private static final int DEFAULT_FLAGS = FLAG_CONTENT_INTENT_AVAILABLE_OFFLINE;
4875
4876 private static final int DEFAULT_CONTENT_ICON_GRAVITY = Gravity.END;
4877 private static final int DEFAULT_GRAVITY = Gravity.BOTTOM;
4878
4879 private ArrayList<Action> mActions = new ArrayList<Action>();
4880 private int mFlags = DEFAULT_FLAGS;
4881 private PendingIntent mDisplayIntent;
4882 private ArrayList<Notification> mPages = new ArrayList<Notification>();
4883 private Bitmap mBackground;
4884 private int mContentIcon;
4885 private int mContentIconGravity = DEFAULT_CONTENT_ICON_GRAVITY;
4886 private int mContentActionIndex = UNSET_ACTION_INDEX;
4887 private int mCustomSizePreset = SIZE_DEFAULT;
4888 private int mCustomContentHeight;
4889 private int mGravity = DEFAULT_GRAVITY;
Griff Hazen5f2edfc2014-09-29 16:28:44 -07004890 private int mHintScreenTimeout;
Griff Hazen61a9e862014-05-22 16:05:19 -07004891
4892 /**
4893 * Create a {@link android.app.Notification.WearableExtender} with default
4894 * options.
4895 */
4896 public WearableExtender() {
4897 }
4898
4899 public WearableExtender(Notification notif) {
4900 Bundle wearableBundle = notif.extras.getBundle(EXTRA_WEARABLE_EXTENSIONS);
4901 if (wearableBundle != null) {
4902 List<Action> actions = wearableBundle.getParcelableArrayList(KEY_ACTIONS);
4903 if (actions != null) {
4904 mActions.addAll(actions);
4905 }
4906
4907 mFlags = wearableBundle.getInt(KEY_FLAGS, DEFAULT_FLAGS);
4908 mDisplayIntent = wearableBundle.getParcelable(KEY_DISPLAY_INTENT);
4909
4910 Notification[] pages = getNotificationArrayFromBundle(
4911 wearableBundle, KEY_PAGES);
4912 if (pages != null) {
4913 Collections.addAll(mPages, pages);
4914 }
4915
4916 mBackground = wearableBundle.getParcelable(KEY_BACKGROUND);
4917 mContentIcon = wearableBundle.getInt(KEY_CONTENT_ICON);
4918 mContentIconGravity = wearableBundle.getInt(KEY_CONTENT_ICON_GRAVITY,
4919 DEFAULT_CONTENT_ICON_GRAVITY);
4920 mContentActionIndex = wearableBundle.getInt(KEY_CONTENT_ACTION_INDEX,
4921 UNSET_ACTION_INDEX);
4922 mCustomSizePreset = wearableBundle.getInt(KEY_CUSTOM_SIZE_PRESET,
4923 SIZE_DEFAULT);
4924 mCustomContentHeight = wearableBundle.getInt(KEY_CUSTOM_CONTENT_HEIGHT);
4925 mGravity = wearableBundle.getInt(KEY_GRAVITY, DEFAULT_GRAVITY);
Griff Hazen5f2edfc2014-09-29 16:28:44 -07004926 mHintScreenTimeout = wearableBundle.getInt(KEY_HINT_SCREEN_TIMEOUT);
Griff Hazen61a9e862014-05-22 16:05:19 -07004927 }
4928 }
4929
4930 /**
4931 * Apply wearable extensions to a notification that is being built. This is typically
4932 * called by the {@link android.app.Notification.Builder#extend} method of
4933 * {@link android.app.Notification.Builder}.
4934 */
4935 @Override
4936 public Notification.Builder extend(Notification.Builder builder) {
4937 Bundle wearableBundle = new Bundle();
4938
4939 if (!mActions.isEmpty()) {
4940 wearableBundle.putParcelableArrayList(KEY_ACTIONS, mActions);
4941 }
4942 if (mFlags != DEFAULT_FLAGS) {
4943 wearableBundle.putInt(KEY_FLAGS, mFlags);
4944 }
4945 if (mDisplayIntent != null) {
4946 wearableBundle.putParcelable(KEY_DISPLAY_INTENT, mDisplayIntent);
4947 }
4948 if (!mPages.isEmpty()) {
4949 wearableBundle.putParcelableArray(KEY_PAGES, mPages.toArray(
4950 new Notification[mPages.size()]));
4951 }
4952 if (mBackground != null) {
4953 wearableBundle.putParcelable(KEY_BACKGROUND, mBackground);
4954 }
4955 if (mContentIcon != 0) {
4956 wearableBundle.putInt(KEY_CONTENT_ICON, mContentIcon);
4957 }
4958 if (mContentIconGravity != DEFAULT_CONTENT_ICON_GRAVITY) {
4959 wearableBundle.putInt(KEY_CONTENT_ICON_GRAVITY, mContentIconGravity);
4960 }
4961 if (mContentActionIndex != UNSET_ACTION_INDEX) {
4962 wearableBundle.putInt(KEY_CONTENT_ACTION_INDEX,
4963 mContentActionIndex);
4964 }
4965 if (mCustomSizePreset != SIZE_DEFAULT) {
4966 wearableBundle.putInt(KEY_CUSTOM_SIZE_PRESET, mCustomSizePreset);
4967 }
4968 if (mCustomContentHeight != 0) {
4969 wearableBundle.putInt(KEY_CUSTOM_CONTENT_HEIGHT, mCustomContentHeight);
4970 }
4971 if (mGravity != DEFAULT_GRAVITY) {
4972 wearableBundle.putInt(KEY_GRAVITY, mGravity);
4973 }
Griff Hazen5f2edfc2014-09-29 16:28:44 -07004974 if (mHintScreenTimeout != 0) {
4975 wearableBundle.putInt(KEY_HINT_SCREEN_TIMEOUT, mHintScreenTimeout);
4976 }
Griff Hazen61a9e862014-05-22 16:05:19 -07004977
4978 builder.getExtras().putBundle(EXTRA_WEARABLE_EXTENSIONS, wearableBundle);
4979 return builder;
4980 }
4981
4982 @Override
4983 public WearableExtender clone() {
4984 WearableExtender that = new WearableExtender();
4985 that.mActions = new ArrayList<Action>(this.mActions);
4986 that.mFlags = this.mFlags;
4987 that.mDisplayIntent = this.mDisplayIntent;
4988 that.mPages = new ArrayList<Notification>(this.mPages);
4989 that.mBackground = this.mBackground;
4990 that.mContentIcon = this.mContentIcon;
4991 that.mContentIconGravity = this.mContentIconGravity;
4992 that.mContentActionIndex = this.mContentActionIndex;
4993 that.mCustomSizePreset = this.mCustomSizePreset;
4994 that.mCustomContentHeight = this.mCustomContentHeight;
4995 that.mGravity = this.mGravity;
Griff Hazen5f2edfc2014-09-29 16:28:44 -07004996 that.mHintScreenTimeout = this.mHintScreenTimeout;
Griff Hazen61a9e862014-05-22 16:05:19 -07004997 return that;
4998 }
4999
5000 /**
5001 * Add a wearable action to this notification.
5002 *
5003 * <p>When wearable actions are added using this method, the set of actions that
5004 * show on a wearable device splits from devices that only show actions added
5005 * using {@link android.app.Notification.Builder#addAction}. This allows for customization
5006 * of which actions display on different devices.
5007 *
5008 * @param action the action to add to this notification
5009 * @return this object for method chaining
5010 * @see android.app.Notification.Action
5011 */
5012 public WearableExtender addAction(Action action) {
5013 mActions.add(action);
5014 return this;
5015 }
5016
5017 /**
5018 * Adds wearable actions to this notification.
5019 *
5020 * <p>When wearable actions are added using this method, the set of actions that
5021 * show on a wearable device splits from devices that only show actions added
5022 * using {@link android.app.Notification.Builder#addAction}. This allows for customization
5023 * of which actions display on different devices.
5024 *
5025 * @param actions the actions to add to this notification
5026 * @return this object for method chaining
5027 * @see android.app.Notification.Action
5028 */
5029 public WearableExtender addActions(List<Action> actions) {
5030 mActions.addAll(actions);
5031 return this;
5032 }
5033
5034 /**
5035 * Clear all wearable actions present on this builder.
5036 * @return this object for method chaining.
5037 * @see #addAction
5038 */
5039 public WearableExtender clearActions() {
5040 mActions.clear();
5041 return this;
5042 }
5043
5044 /**
5045 * Get the wearable actions present on this notification.
5046 */
5047 public List<Action> getActions() {
5048 return mActions;
5049 }
5050
5051 /**
5052 * Set an intent to launch inside of an activity view when displaying
Griff Hazen14f57992014-05-26 09:07:14 -07005053 * this notification. The {@link PendingIntent} provided should be for an activity.
5054 *
5055 * <pre class="prettyprint">
5056 * Intent displayIntent = new Intent(context, MyDisplayActivity.class);
5057 * PendingIntent displayPendingIntent = PendingIntent.getActivity(context,
5058 * 0, displayIntent, PendingIntent.FLAG_UPDATE_CURRENT);
5059 * Notification notif = new Notification.Builder(context)
5060 * .extend(new Notification.WearableExtender()
5061 * .setDisplayIntent(displayPendingIntent)
5062 * .setCustomSizePreset(Notification.WearableExtender.SIZE_MEDIUM))
5063 * .build();</pre>
5064 *
5065 * <p>The activity to launch needs to allow embedding, must be exported, and
Griff Hazen831ca9d2014-06-17 00:38:38 -07005066 * should have an empty task affinity. It is also recommended to use the device
5067 * default light theme.
Griff Hazen14f57992014-05-26 09:07:14 -07005068 *
5069 * <p>Example AndroidManifest.xml entry:
5070 * <pre class="prettyprint">
5071 * &lt;activity android:name=&quot;com.example.MyDisplayActivity&quot;
5072 * android:exported=&quot;true&quot;
5073 * android:allowEmbedded=&quot;true&quot;
Griff Hazen831ca9d2014-06-17 00:38:38 -07005074 * android:taskAffinity=&quot;&quot;
5075 * android:theme=&quot;@android:style/Theme.DeviceDefault.Light&quot; /&gt;</pre>
Griff Hazen61a9e862014-05-22 16:05:19 -07005076 *
5077 * @param intent the {@link PendingIntent} for an activity
5078 * @return this object for method chaining
5079 * @see android.app.Notification.WearableExtender#getDisplayIntent
5080 */
5081 public WearableExtender setDisplayIntent(PendingIntent intent) {
5082 mDisplayIntent = intent;
5083 return this;
5084 }
5085
5086 /**
5087 * Get the intent to launch inside of an activity view when displaying this
5088 * notification. This {@code PendingIntent} should be for an activity.
5089 */
5090 public PendingIntent getDisplayIntent() {
5091 return mDisplayIntent;
5092 }
5093
5094 /**
5095 * Add an additional page of content to display with this notification. The current
5096 * notification forms the first page, and pages added using this function form
5097 * subsequent pages. This field can be used to separate a notification into multiple
5098 * sections.
5099 *
5100 * @param page the notification to add as another page
5101 * @return this object for method chaining
5102 * @see android.app.Notification.WearableExtender#getPages
5103 */
5104 public WearableExtender addPage(Notification page) {
5105 mPages.add(page);
5106 return this;
5107 }
5108
5109 /**
5110 * Add additional pages of content to display with this notification. The current
5111 * notification forms the first page, and pages added using this function form
5112 * subsequent pages. This field can be used to separate a notification into multiple
5113 * sections.
5114 *
5115 * @param pages a list of notifications
5116 * @return this object for method chaining
5117 * @see android.app.Notification.WearableExtender#getPages
5118 */
5119 public WearableExtender addPages(List<Notification> pages) {
5120 mPages.addAll(pages);
5121 return this;
5122 }
5123
5124 /**
5125 * Clear all additional pages present on this builder.
5126 * @return this object for method chaining.
5127 * @see #addPage
5128 */
5129 public WearableExtender clearPages() {
5130 mPages.clear();
5131 return this;
5132 }
5133
5134 /**
5135 * Get the array of additional pages of content for displaying this notification. The
5136 * current notification forms the first page, and elements within this array form
5137 * subsequent pages. This field can be used to separate a notification into multiple
5138 * sections.
5139 * @return the pages for this notification
5140 */
5141 public List<Notification> getPages() {
5142 return mPages;
5143 }
5144
5145 /**
5146 * Set a background image to be displayed behind the notification content.
5147 * Contrary to the {@link android.app.Notification.BigPictureStyle}, this background
5148 * will work with any notification style.
5149 *
5150 * @param background the background bitmap
5151 * @return this object for method chaining
5152 * @see android.app.Notification.WearableExtender#getBackground
5153 */
5154 public WearableExtender setBackground(Bitmap background) {
5155 mBackground = background;
5156 return this;
5157 }
5158
5159 /**
5160 * Get a background image to be displayed behind the notification content.
5161 * Contrary to the {@link android.app.Notification.BigPictureStyle}, this background
5162 * will work with any notification style.
5163 *
5164 * @return the background image
5165 * @see android.app.Notification.WearableExtender#setBackground
5166 */
5167 public Bitmap getBackground() {
5168 return mBackground;
5169 }
5170
5171 /**
5172 * Set an icon that goes with the content of this notification.
5173 */
5174 public WearableExtender setContentIcon(int icon) {
5175 mContentIcon = icon;
5176 return this;
5177 }
5178
5179 /**
5180 * Get an icon that goes with the content of this notification.
5181 */
5182 public int getContentIcon() {
5183 return mContentIcon;
5184 }
5185
5186 /**
5187 * Set the gravity that the content icon should have within the notification display.
5188 * Supported values include {@link android.view.Gravity#START} and
5189 * {@link android.view.Gravity#END}. The default value is {@link android.view.Gravity#END}.
5190 * @see #setContentIcon
5191 */
5192 public WearableExtender setContentIconGravity(int contentIconGravity) {
5193 mContentIconGravity = contentIconGravity;
5194 return this;
5195 }
5196
5197 /**
5198 * Get the gravity that the content icon should have within the notification display.
5199 * Supported values include {@link android.view.Gravity#START} and
5200 * {@link android.view.Gravity#END}. The default value is {@link android.view.Gravity#END}.
5201 * @see #getContentIcon
5202 */
5203 public int getContentIconGravity() {
5204 return mContentIconGravity;
5205 }
5206
5207 /**
5208 * Set an action from this notification's actions to be clickable with the content of
Griff Hazen14f57992014-05-26 09:07:14 -07005209 * this notification. This action will no longer display separately from the
5210 * notification's content.
5211 *
Griff Hazenca48d352014-05-28 22:37:13 -07005212 * <p>For notifications with multiple pages, child pages can also have content actions
Griff Hazen14f57992014-05-26 09:07:14 -07005213 * set, although the list of available actions comes from the main notification and not
5214 * from the child page's notification.
5215 *
5216 * @param actionIndex The index of the action to hoist onto the current notification page.
5217 * If wearable actions were added to the main notification, this index
5218 * will apply to that list, otherwise it will apply to the regular
5219 * actions list.
Griff Hazen61a9e862014-05-22 16:05:19 -07005220 */
5221 public WearableExtender setContentAction(int actionIndex) {
5222 mContentActionIndex = actionIndex;
5223 return this;
5224 }
5225
5226 /**
Griff Hazenca48d352014-05-28 22:37:13 -07005227 * Get the index of the notification action, if any, that was specified as being clickable
5228 * with the content of this notification. This action will no longer display separately
Griff Hazen14f57992014-05-26 09:07:14 -07005229 * from the notification's content.
Griff Hazen61a9e862014-05-22 16:05:19 -07005230 *
Griff Hazenca48d352014-05-28 22:37:13 -07005231 * <p>For notifications with multiple pages, child pages can also have content actions
Griff Hazen14f57992014-05-26 09:07:14 -07005232 * set, although the list of available actions comes from the main notification and not
5233 * from the child page's notification.
5234 *
5235 * <p>If wearable specific actions were added to the main notification, this index will
5236 * apply to that list, otherwise it will apply to the regular actions list.
Griff Hazenca48d352014-05-28 22:37:13 -07005237 *
5238 * @return the action index or {@link #UNSET_ACTION_INDEX} if no action was selected.
Griff Hazen61a9e862014-05-22 16:05:19 -07005239 */
5240 public int getContentAction() {
5241 return mContentActionIndex;
5242 }
5243
5244 /**
5245 * Set the gravity that this notification should have within the available viewport space.
5246 * Supported values include {@link android.view.Gravity#TOP},
5247 * {@link android.view.Gravity#CENTER_VERTICAL} and {@link android.view.Gravity#BOTTOM}.
5248 * The default value is {@link android.view.Gravity#BOTTOM}.
5249 */
5250 public WearableExtender setGravity(int gravity) {
5251 mGravity = gravity;
5252 return this;
5253 }
5254
5255 /**
5256 * Get the gravity that this notification should have within the available viewport space.
5257 * Supported values include {@link android.view.Gravity#TOP},
5258 * {@link android.view.Gravity#CENTER_VERTICAL} and {@link android.view.Gravity#BOTTOM}.
5259 * The default value is {@link android.view.Gravity#BOTTOM}.
5260 */
5261 public int getGravity() {
5262 return mGravity;
5263 }
5264
5265 /**
5266 * Set the custom size preset for the display of this notification out of the available
5267 * presets found in {@link android.app.Notification.WearableExtender}, e.g.
5268 * {@link #SIZE_LARGE}.
5269 * <p>Some custom size presets are only applicable for custom display notifications created
5270 * using {@link android.app.Notification.WearableExtender#setDisplayIntent}. Check the
5271 * documentation for the preset in question. See also
5272 * {@link #setCustomContentHeight} and {@link #getCustomSizePreset}.
5273 */
5274 public WearableExtender setCustomSizePreset(int sizePreset) {
5275 mCustomSizePreset = sizePreset;
5276 return this;
5277 }
5278
5279 /**
5280 * Get the custom size preset for the display of this notification out of the available
5281 * presets found in {@link android.app.Notification.WearableExtender}, e.g.
5282 * {@link #SIZE_LARGE}.
5283 * <p>Some custom size presets are only applicable for custom display notifications created
5284 * using {@link #setDisplayIntent}. Check the documentation for the preset in question.
5285 * See also {@link #setCustomContentHeight} and {@link #setCustomSizePreset}.
5286 */
5287 public int getCustomSizePreset() {
5288 return mCustomSizePreset;
5289 }
5290
5291 /**
5292 * Set the custom height in pixels for the display of this notification's content.
5293 * <p>This option is only available for custom display notifications created
5294 * using {@link android.app.Notification.WearableExtender#setDisplayIntent}. See also
5295 * {@link android.app.Notification.WearableExtender#setCustomSizePreset} and
5296 * {@link #getCustomContentHeight}.
5297 */
5298 public WearableExtender setCustomContentHeight(int height) {
5299 mCustomContentHeight = height;
5300 return this;
5301 }
5302
5303 /**
5304 * Get the custom height in pixels for the display of this notification's content.
5305 * <p>This option is only available for custom display notifications created
5306 * using {@link #setDisplayIntent}. See also {@link #setCustomSizePreset} and
5307 * {@link #setCustomContentHeight}.
5308 */
5309 public int getCustomContentHeight() {
5310 return mCustomContentHeight;
5311 }
5312
5313 /**
5314 * Set whether the scrolling position for the contents of this notification should start
5315 * at the bottom of the contents instead of the top when the contents are too long to
5316 * display within the screen. Default is false (start scroll at the top).
5317 */
5318 public WearableExtender setStartScrollBottom(boolean startScrollBottom) {
5319 setFlag(FLAG_START_SCROLL_BOTTOM, startScrollBottom);
5320 return this;
5321 }
5322
5323 /**
5324 * Get whether the scrolling position for the contents of this notification should start
5325 * at the bottom of the contents instead of the top when the contents are too long to
5326 * display within the screen. Default is false (start scroll at the top).
5327 */
5328 public boolean getStartScrollBottom() {
5329 return (mFlags & FLAG_START_SCROLL_BOTTOM) != 0;
5330 }
5331
5332 /**
5333 * Set whether the content intent is available when the wearable device is not connected
5334 * to a companion device. The user can still trigger this intent when the wearable device
5335 * is offline, but a visual hint will indicate that the content intent may not be available.
5336 * Defaults to true.
5337 */
5338 public WearableExtender setContentIntentAvailableOffline(
5339 boolean contentIntentAvailableOffline) {
5340 setFlag(FLAG_CONTENT_INTENT_AVAILABLE_OFFLINE, contentIntentAvailableOffline);
5341 return this;
5342 }
5343
5344 /**
5345 * Get whether the content intent is available when the wearable device is not connected
5346 * to a companion device. The user can still trigger this intent when the wearable device
5347 * is offline, but a visual hint will indicate that the content intent may not be available.
5348 * Defaults to true.
5349 */
5350 public boolean getContentIntentAvailableOffline() {
5351 return (mFlags & FLAG_CONTENT_INTENT_AVAILABLE_OFFLINE) != 0;
5352 }
5353
5354 /**
5355 * Set a hint that this notification's icon should not be displayed.
5356 * @param hintHideIcon {@code true} to hide the icon, {@code false} otherwise.
5357 * @return this object for method chaining
5358 */
5359 public WearableExtender setHintHideIcon(boolean hintHideIcon) {
5360 setFlag(FLAG_HINT_HIDE_ICON, hintHideIcon);
5361 return this;
5362 }
5363
5364 /**
5365 * Get a hint that this notification's icon should not be displayed.
5366 * @return {@code true} if this icon should not be displayed, false otherwise.
5367 * The default value is {@code false} if this was never set.
5368 */
5369 public boolean getHintHideIcon() {
5370 return (mFlags & FLAG_HINT_HIDE_ICON) != 0;
5371 }
5372
5373 /**
5374 * Set a visual hint that only the background image of this notification should be
5375 * displayed, and other semantic content should be hidden. This hint is only applicable
5376 * to sub-pages added using {@link #addPage}.
5377 */
5378 public WearableExtender setHintShowBackgroundOnly(boolean hintShowBackgroundOnly) {
5379 setFlag(FLAG_HINT_SHOW_BACKGROUND_ONLY, hintShowBackgroundOnly);
5380 return this;
5381 }
5382
5383 /**
5384 * Get a visual hint that only the background image of this notification should be
5385 * displayed, and other semantic content should be hidden. This hint is only applicable
5386 * to sub-pages added using {@link android.app.Notification.WearableExtender#addPage}.
5387 */
5388 public boolean getHintShowBackgroundOnly() {
5389 return (mFlags & FLAG_HINT_SHOW_BACKGROUND_ONLY) != 0;
5390 }
5391
Griff Hazen5f2edfc2014-09-29 16:28:44 -07005392 /**
Griff Hazen9c5be4e2014-11-17 17:47:50 -08005393 * Set a hint that this notification's background should not be clipped if possible,
5394 * and should instead be resized to fully display on the screen, retaining the aspect
5395 * ratio of the image. This can be useful for images like barcodes or qr codes.
Griff Hazen5f2edfc2014-09-29 16:28:44 -07005396 * @param hintAvoidBackgroundClipping {@code true} to avoid clipping if possible.
5397 * @return this object for method chaining
5398 */
5399 public WearableExtender setHintAvoidBackgroundClipping(
5400 boolean hintAvoidBackgroundClipping) {
5401 setFlag(FLAG_HINT_AVOID_BACKGROUND_CLIPPING, hintAvoidBackgroundClipping);
5402 return this;
5403 }
5404
5405 /**
Griff Hazen9c5be4e2014-11-17 17:47:50 -08005406 * Get a hint that this notification's background should not be clipped if possible,
5407 * and should instead be resized to fully display on the screen, retaining the aspect
5408 * ratio of the image. This can be useful for images like barcodes or qr codes.
Griff Hazen5f2edfc2014-09-29 16:28:44 -07005409 * @return {@code true} if it's ok if the background is clipped on the screen, false
5410 * otherwise. The default value is {@code false} if this was never set.
5411 */
5412 public boolean getHintAvoidBackgroundClipping() {
5413 return (mFlags & FLAG_HINT_AVOID_BACKGROUND_CLIPPING) != 0;
5414 }
5415
5416 /**
5417 * Set a hint that the screen should remain on for at least this duration when
5418 * this notification is displayed on the screen.
5419 * @param timeout The requested screen timeout in milliseconds. Can also be either
5420 * {@link #SCREEN_TIMEOUT_SHORT} or {@link #SCREEN_TIMEOUT_LONG}.
5421 * @return this object for method chaining
5422 */
5423 public WearableExtender setHintScreenTimeout(int timeout) {
5424 mHintScreenTimeout = timeout;
5425 return this;
5426 }
5427
5428 /**
5429 * Get the duration, in milliseconds, that the screen should remain on for
5430 * when this notification is displayed.
5431 * @return the duration in milliseconds if > 0, or either one of the sentinel values
5432 * {@link #SCREEN_TIMEOUT_SHORT} or {@link #SCREEN_TIMEOUT_LONG}.
5433 */
5434 public int getHintScreenTimeout() {
5435 return mHintScreenTimeout;
5436 }
5437
Griff Hazen61a9e862014-05-22 16:05:19 -07005438 private void setFlag(int mask, boolean value) {
5439 if (value) {
5440 mFlags |= mask;
5441 } else {
5442 mFlags &= ~mask;
5443 }
5444 }
5445 }
5446
5447 /**
Zhen Yu Song9d5528b2014-11-14 15:34:39 -08005448 * <p>Helper class to add Android Auto extensions to notifications. To create a notification
5449 * with car extensions:
5450 *
5451 * <ol>
5452 * <li>Create an {@link Notification.Builder}, setting any desired
5453 * properties.
5454 * <li>Create a {@link CarExtender}.
5455 * <li>Set car-specific properties using the {@code add} and {@code set} methods of
5456 * {@link CarExtender}.
5457 * <li>Call {@link Notification.Builder#extend(Notification.Extender)}
5458 * to apply the extensions to a notification.
5459 * </ol>
5460 *
5461 * <pre class="prettyprint">
5462 * Notification notification = new Notification.Builder(context)
5463 * ...
5464 * .extend(new CarExtender()
5465 * .set*(...))
5466 * .build();
5467 * </pre>
5468 *
5469 * <p>Car extensions can be accessed on an existing notification by using the
5470 * {@code CarExtender(Notification)} constructor, and then using the {@code get} methods
5471 * to access values.
5472 */
5473 public static final class CarExtender implements Extender {
5474 private static final String TAG = "CarExtender";
5475
5476 private static final String EXTRA_CAR_EXTENDER = "android.car.EXTENSIONS";
5477 private static final String EXTRA_LARGE_ICON = "large_icon";
5478 private static final String EXTRA_CONVERSATION = "car_conversation";
5479 private static final String EXTRA_COLOR = "app_color";
5480
5481 private Bitmap mLargeIcon;
5482 private UnreadConversation mUnreadConversation;
5483 private int mColor = Notification.COLOR_DEFAULT;
5484
5485 /**
5486 * Create a {@link CarExtender} with default options.
5487 */
5488 public CarExtender() {
5489 }
5490
5491 /**
5492 * Create a {@link CarExtender} from the CarExtender options of an existing Notification.
5493 *
5494 * @param notif The notification from which to copy options.
5495 */
5496 public CarExtender(Notification notif) {
5497 Bundle carBundle = notif.extras == null ?
5498 null : notif.extras.getBundle(EXTRA_CAR_EXTENDER);
5499 if (carBundle != null) {
5500 mLargeIcon = carBundle.getParcelable(EXTRA_LARGE_ICON);
5501 mColor = carBundle.getInt(EXTRA_COLOR, Notification.COLOR_DEFAULT);
5502
5503 Bundle b = carBundle.getBundle(EXTRA_CONVERSATION);
5504 mUnreadConversation = UnreadConversation.getUnreadConversationFromBundle(b);
5505 }
5506 }
5507
5508 /**
5509 * Apply car extensions to a notification that is being built. This is typically called by
5510 * the {@link Notification.Builder#extend(Notification.Extender)}
5511 * method of {@link Notification.Builder}.
5512 */
5513 @Override
5514 public Notification.Builder extend(Notification.Builder builder) {
5515 Bundle carExtensions = new Bundle();
5516
5517 if (mLargeIcon != null) {
5518 carExtensions.putParcelable(EXTRA_LARGE_ICON, mLargeIcon);
5519 }
5520 if (mColor != Notification.COLOR_DEFAULT) {
5521 carExtensions.putInt(EXTRA_COLOR, mColor);
5522 }
5523
5524 if (mUnreadConversation != null) {
5525 Bundle b = mUnreadConversation.getBundleForUnreadConversation();
5526 carExtensions.putBundle(EXTRA_CONVERSATION, b);
5527 }
5528
5529 builder.getExtras().putBundle(EXTRA_CAR_EXTENDER, carExtensions);
5530 return builder;
5531 }
5532
5533 /**
5534 * Sets the accent color to use when Android Auto presents the notification.
5535 *
5536 * Android Auto uses the color set with {@link Notification.Builder#setColor(int)}
5537 * to accent the displayed notification. However, not all colors are acceptable in an
5538 * automotive setting. This method can be used to override the color provided in the
5539 * notification in such a situation.
5540 */
Tor Norbye80756e32015-03-02 09:39:27 -08005541 public CarExtender setColor(@ColorInt int color) {
Zhen Yu Song9d5528b2014-11-14 15:34:39 -08005542 mColor = color;
5543 return this;
5544 }
5545
5546 /**
5547 * Gets the accent color.
5548 *
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005549 * @see #setColor
Zhen Yu Song9d5528b2014-11-14 15:34:39 -08005550 */
Tor Norbye80756e32015-03-02 09:39:27 -08005551 @ColorInt
Zhen Yu Song9d5528b2014-11-14 15:34:39 -08005552 public int getColor() {
5553 return mColor;
5554 }
5555
5556 /**
5557 * Sets the large icon of the car notification.
5558 *
5559 * If no large icon is set in the extender, Android Auto will display the icon
5560 * specified by {@link Notification.Builder#setLargeIcon(android.graphics.Bitmap)}
5561 *
5562 * @param largeIcon The large icon to use in the car notification.
5563 * @return This object for method chaining.
5564 */
5565 public CarExtender setLargeIcon(Bitmap largeIcon) {
5566 mLargeIcon = largeIcon;
5567 return this;
5568 }
5569
5570 /**
5571 * Gets the large icon used in this car notification, or null if no icon has been set.
5572 *
5573 * @return The large icon for the car notification.
5574 * @see CarExtender#setLargeIcon
5575 */
5576 public Bitmap getLargeIcon() {
5577 return mLargeIcon;
5578 }
5579
5580 /**
5581 * Sets the unread conversation in a message notification.
5582 *
5583 * @param unreadConversation The unread part of the conversation this notification conveys.
5584 * @return This object for method chaining.
5585 */
5586 public CarExtender setUnreadConversation(UnreadConversation unreadConversation) {
5587 mUnreadConversation = unreadConversation;
5588 return this;
5589 }
5590
5591 /**
5592 * Returns the unread conversation conveyed by this notification.
5593 * @see #setUnreadConversation(UnreadConversation)
5594 */
5595 public UnreadConversation getUnreadConversation() {
5596 return mUnreadConversation;
5597 }
5598
5599 /**
5600 * A class which holds the unread messages from a conversation.
5601 */
5602 public static class UnreadConversation {
5603 private static final String KEY_AUTHOR = "author";
5604 private static final String KEY_TEXT = "text";
5605 private static final String KEY_MESSAGES = "messages";
5606 private static final String KEY_REMOTE_INPUT = "remote_input";
5607 private static final String KEY_ON_REPLY = "on_reply";
5608 private static final String KEY_ON_READ = "on_read";
5609 private static final String KEY_PARTICIPANTS = "participants";
5610 private static final String KEY_TIMESTAMP = "timestamp";
5611
5612 private final String[] mMessages;
5613 private final RemoteInput mRemoteInput;
5614 private final PendingIntent mReplyPendingIntent;
5615 private final PendingIntent mReadPendingIntent;
5616 private final String[] mParticipants;
5617 private final long mLatestTimestamp;
5618
5619 UnreadConversation(String[] messages, RemoteInput remoteInput,
5620 PendingIntent replyPendingIntent, PendingIntent readPendingIntent,
5621 String[] participants, long latestTimestamp) {
5622 mMessages = messages;
5623 mRemoteInput = remoteInput;
5624 mReadPendingIntent = readPendingIntent;
5625 mReplyPendingIntent = replyPendingIntent;
5626 mParticipants = participants;
5627 mLatestTimestamp = latestTimestamp;
5628 }
5629
5630 /**
5631 * Gets the list of messages conveyed by this notification.
5632 */
5633 public String[] getMessages() {
5634 return mMessages;
5635 }
5636
5637 /**
5638 * Gets the remote input that will be used to convey the response to a message list, or
5639 * null if no such remote input exists.
5640 */
5641 public RemoteInput getRemoteInput() {
5642 return mRemoteInput;
5643 }
5644
5645 /**
5646 * Gets the pending intent that will be triggered when the user replies to this
5647 * notification.
5648 */
5649 public PendingIntent getReplyPendingIntent() {
5650 return mReplyPendingIntent;
5651 }
5652
5653 /**
5654 * Gets the pending intent that Android Auto will send after it reads aloud all messages
5655 * in this object's message list.
5656 */
5657 public PendingIntent getReadPendingIntent() {
5658 return mReadPendingIntent;
5659 }
5660
5661 /**
5662 * Gets the participants in the conversation.
5663 */
5664 public String[] getParticipants() {
5665 return mParticipants;
5666 }
5667
5668 /**
5669 * Gets the firs participant in the conversation.
5670 */
5671 public String getParticipant() {
5672 return mParticipants.length > 0 ? mParticipants[0] : null;
5673 }
5674
5675 /**
5676 * Gets the timestamp of the conversation.
5677 */
5678 public long getLatestTimestamp() {
5679 return mLatestTimestamp;
5680 }
5681
5682 Bundle getBundleForUnreadConversation() {
5683 Bundle b = new Bundle();
5684 String author = null;
5685 if (mParticipants != null && mParticipants.length > 1) {
5686 author = mParticipants[0];
5687 }
5688 Parcelable[] messages = new Parcelable[mMessages.length];
5689 for (int i = 0; i < messages.length; i++) {
5690 Bundle m = new Bundle();
5691 m.putString(KEY_TEXT, mMessages[i]);
5692 m.putString(KEY_AUTHOR, author);
5693 messages[i] = m;
5694 }
5695 b.putParcelableArray(KEY_MESSAGES, messages);
5696 if (mRemoteInput != null) {
5697 b.putParcelable(KEY_REMOTE_INPUT, mRemoteInput);
5698 }
5699 b.putParcelable(KEY_ON_REPLY, mReplyPendingIntent);
5700 b.putParcelable(KEY_ON_READ, mReadPendingIntent);
5701 b.putStringArray(KEY_PARTICIPANTS, mParticipants);
5702 b.putLong(KEY_TIMESTAMP, mLatestTimestamp);
5703 return b;
5704 }
5705
5706 static UnreadConversation getUnreadConversationFromBundle(Bundle b) {
5707 if (b == null) {
5708 return null;
5709 }
5710 Parcelable[] parcelableMessages = b.getParcelableArray(KEY_MESSAGES);
5711 String[] messages = null;
5712 if (parcelableMessages != null) {
5713 String[] tmp = new String[parcelableMessages.length];
5714 boolean success = true;
5715 for (int i = 0; i < tmp.length; i++) {
5716 if (!(parcelableMessages[i] instanceof Bundle)) {
5717 success = false;
5718 break;
5719 }
5720 tmp[i] = ((Bundle) parcelableMessages[i]).getString(KEY_TEXT);
5721 if (tmp[i] == null) {
5722 success = false;
5723 break;
5724 }
5725 }
5726 if (success) {
5727 messages = tmp;
5728 } else {
5729 return null;
5730 }
5731 }
5732
5733 PendingIntent onRead = b.getParcelable(KEY_ON_READ);
5734 PendingIntent onReply = b.getParcelable(KEY_ON_REPLY);
5735
5736 RemoteInput remoteInput = b.getParcelable(KEY_REMOTE_INPUT);
5737
5738 String[] participants = b.getStringArray(KEY_PARTICIPANTS);
5739 if (participants == null || participants.length != 1) {
5740 return null;
5741 }
5742
5743 return new UnreadConversation(messages,
5744 remoteInput,
5745 onReply,
5746 onRead,
5747 participants, b.getLong(KEY_TIMESTAMP));
5748 }
5749 };
5750
5751 /**
5752 * Builder class for {@link CarExtender.UnreadConversation} objects.
5753 */
5754 public static class Builder {
5755 private final List<String> mMessages = new ArrayList<String>();
5756 private final String mParticipant;
5757 private RemoteInput mRemoteInput;
5758 private PendingIntent mReadPendingIntent;
5759 private PendingIntent mReplyPendingIntent;
5760 private long mLatestTimestamp;
5761
5762 /**
5763 * Constructs a new builder for {@link CarExtender.UnreadConversation}.
5764 *
5765 * @param name The name of the other participant in the conversation.
5766 */
5767 public Builder(String name) {
5768 mParticipant = name;
5769 }
5770
5771 /**
5772 * Appends a new unread message to the list of messages for this conversation.
5773 *
5774 * The messages should be added from oldest to newest.
5775 *
5776 * @param message The text of the new unread message.
5777 * @return This object for method chaining.
5778 */
5779 public Builder addMessage(String message) {
5780 mMessages.add(message);
5781 return this;
5782 }
5783
5784 /**
5785 * Sets the pending intent and remote input which will convey the reply to this
5786 * notification.
5787 *
5788 * @param pendingIntent The pending intent which will be triggered on a reply.
5789 * @param remoteInput The remote input parcelable which will carry the reply.
5790 * @return This object for method chaining.
5791 *
5792 * @see CarExtender.UnreadConversation#getRemoteInput
5793 * @see CarExtender.UnreadConversation#getReplyPendingIntent
5794 */
5795 public Builder setReplyAction(
5796 PendingIntent pendingIntent, RemoteInput remoteInput) {
5797 mRemoteInput = remoteInput;
5798 mReplyPendingIntent = pendingIntent;
5799
5800 return this;
5801 }
5802
5803 /**
5804 * Sets the pending intent that will be sent once the messages in this notification
5805 * are read.
5806 *
5807 * @param pendingIntent The pending intent to use.
5808 * @return This object for method chaining.
5809 */
5810 public Builder setReadPendingIntent(PendingIntent pendingIntent) {
5811 mReadPendingIntent = pendingIntent;
5812 return this;
5813 }
5814
5815 /**
5816 * Sets the timestamp of the most recent message in an unread conversation.
5817 *
5818 * If a messaging notification has been posted by your application and has not
5819 * yet been cancelled, posting a later notification with the same id and tag
5820 * but without a newer timestamp may result in Android Auto not displaying a
5821 * heads up notification for the later notification.
5822 *
5823 * @param timestamp The timestamp of the most recent message in the conversation.
5824 * @return This object for method chaining.
5825 */
5826 public Builder setLatestTimestamp(long timestamp) {
5827 mLatestTimestamp = timestamp;
5828 return this;
5829 }
5830
5831 /**
5832 * Builds a new unread conversation object.
5833 *
5834 * @return The new unread conversation object.
5835 */
5836 public UnreadConversation build() {
5837 String[] messages = mMessages.toArray(new String[mMessages.size()]);
5838 String[] participants = { mParticipant };
5839 return new UnreadConversation(messages, mRemoteInput, mReplyPendingIntent,
5840 mReadPendingIntent, participants, mLatestTimestamp);
5841 }
5842 }
5843 }
5844
5845 /**
Griff Hazen61a9e862014-05-22 16:05:19 -07005846 * Get an array of Notification objects from a parcelable array bundle field.
5847 * Update the bundle to have a typed array so fetches in the future don't need
5848 * to do an array copy.
5849 */
5850 private static Notification[] getNotificationArrayFromBundle(Bundle bundle, String key) {
5851 Parcelable[] array = bundle.getParcelableArray(key);
5852 if (array instanceof Notification[] || array == null) {
5853 return (Notification[]) array;
5854 }
5855 Notification[] typedArray = Arrays.copyOf(array, array.length,
5856 Notification[].class);
5857 bundle.putParcelableArray(key, typedArray);
5858 return typedArray;
5859 }
Christoph Studer4600f9b2014-07-22 22:44:43 +02005860
5861 private static class BuilderRemoteViews extends RemoteViews {
5862 public BuilderRemoteViews(Parcel parcel) {
5863 super(parcel);
5864 }
5865
Kenny Guy77320062014-08-27 21:37:15 +01005866 public BuilderRemoteViews(ApplicationInfo appInfo, int layoutId) {
5867 super(appInfo, layoutId);
Christoph Studer4600f9b2014-07-22 22:44:43 +02005868 }
5869
5870 @Override
5871 public BuilderRemoteViews clone() {
5872 Parcel p = Parcel.obtain();
5873 writeToParcel(p, 0);
5874 p.setDataPosition(0);
5875 BuilderRemoteViews brv = new BuilderRemoteViews(p);
5876 p.recycle();
5877 return brv;
5878 }
5879 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005880}