blob: 93acefa07310cc2c5d469fb3ba4159bb0dac5753 [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;
Julia Reynoldse46bb372016-03-17 11:05:58 -040024import android.annotation.SystemApi;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080025import android.content.Context;
26import android.content.Intent;
Kenny Guy77320062014-08-27 21:37:15 +010027import android.content.pm.ApplicationInfo;
Christoph Studer4600f9b2014-07-22 22:44:43 +020028import android.content.pm.PackageManager.NameNotFoundException;
Jorim Jaggief72a192014-08-26 21:57:46 +020029import android.content.res.ColorStateList;
Joe Onoratoef1e7762010-09-17 18:38:38 -040030import android.graphics.Bitmap;
Kenny Guy8a0101b2014-05-08 23:34:12 +010031import android.graphics.Canvas;
Jorim Jaggi5c2d8462014-03-21 17:37:00 +010032import android.graphics.PorterDuff;
Kenny Guy8a0101b2014-05-08 23:34:12 +010033import android.graphics.drawable.Drawable;
Dan Sandlerd63f9322015-05-06 15:18:49 -040034import android.graphics.drawable.Icon;
John Spurlockc0650f022014-07-19 13:22:39 -040035import android.media.AudioAttributes;
Jeff Sharkey098d5802012-04-26 17:30:34 -070036import android.media.AudioManager;
Jeff Browndba34ba2014-06-24 20:46:03 -070037import android.media.session.MediaSession;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080038import android.net.Uri;
Daniel Sandlerdcbaf662013-04-26 16:23:09 -040039import android.os.BadParcelableException;
Christoph Studer239f8352014-08-25 15:13:18 +020040import android.os.Build;
Daniel Sandler2561b0b2012-02-13 21:04:12 -050041import android.os.Bundle;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080042import android.os.Parcel;
43import android.os.Parcelable;
Daniel Sandlera2985ed2012-04-03 16:42:00 -040044import android.os.SystemClock;
Jeff Sharkey6d515712012-09-20 16:06:08 -070045import android.os.UserHandle;
Selim Cinek60a54252016-02-26 17:03:25 -080046import android.text.SpannableStringBuilder;
47import android.text.Spanned;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080048import android.text.TextUtils;
Selim Cinek60a54252016-02-26 17:03:25 -080049import android.text.style.AbsoluteSizeSpan;
Selim Cinek89991a22016-03-07 19:51:54 -080050import android.text.style.CharacterStyle;
Selim Cinek60a54252016-02-26 17:03:25 -080051import android.text.style.RelativeSizeSpan;
52import android.text.style.TextAppearanceSpan;
Daniel Sandlerdcbaf662013-04-26 16:23:09 -040053import android.util.Log;
Dan Sandler50128532015-12-08 15:42:41 -050054import android.util.SparseArray;
Daniel Sandler9f7936a2012-05-21 16:14:28 -040055import android.util.TypedValue;
Griff Hazen61a9e862014-05-22 16:05:19 -070056import android.view.Gravity;
Selim Cinekea4bef72015-12-02 15:51:10 -080057import android.view.NotificationHeaderView;
Joe Onorato8595a3d2010-11-19 18:12:07 -080058import android.view.View;
Adrian Roos9b123cf2016-02-04 14:55:57 -080059import android.view.ViewGroup;
Jeff Sharkey1c400132011-08-05 14:50:13 -070060import android.widget.ProgressBar;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080061import android.widget.RemoteViews;
62
Griff Hazen959591e2014-05-15 22:26:18 -070063import com.android.internal.R;
Griff Hazenc091ba82014-05-16 10:13:26 -070064import com.android.internal.util.NotificationColorUtil;
Griff Hazen959591e2014-05-15 22:26:18 -070065
Tor Norbyed9273d62013-05-30 15:59:53 -070066import java.lang.annotation.Retention;
67import java.lang.annotation.RetentionPolicy;
Christoph Studer4600f9b2014-07-22 22:44:43 +020068import java.lang.reflect.Constructor;
Daniel Sandler2561b0b2012-02-13 21:04:12 -050069import java.util.ArrayList;
Griff Hazen61a9e862014-05-22 16:05:19 -070070import java.util.Arrays;
Griff Hazen5cadc3b2014-05-20 09:55:39 -070071import java.util.Collections;
Griff Hazen61a9e862014-05-22 16:05:19 -070072import java.util.List;
Dan Sandler50128532015-12-08 15:42:41 -050073import java.util.Set;
Joe Onorato561d3852010-11-20 18:09:34 -080074
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080075/**
76 * A class that represents how a persistent notification is to be presented to
77 * the user using the {@link android.app.NotificationManager}.
78 *
Joe Onoratocb109a02011-01-18 17:57:41 -080079 * <p>The {@link Notification.Builder Notification.Builder} has been added to make it
80 * easier to construct Notifications.</p>
81 *
Joe Fernandez558459f2011-10-13 16:47:36 -070082 * <div class="special reference">
83 * <h3>Developer Guides</h3>
84 * <p>For a guide to creating notifications, read the
85 * <a href="{@docRoot}guide/topics/ui/notifiers/notifications.html">Status Bar Notifications</a>
86 * developer guide.</p>
87 * </div>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080088 */
89public class Notification implements Parcelable
90{
Daniel Sandlerdcbaf662013-04-26 16:23:09 -040091 private static final String TAG = "Notification";
92
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080093 /**
Daniel Sandler01df1c62014-06-09 10:54:01 -040094 * An activity that provides a user interface for adjusting notification preferences for its
95 * containing application. Optional but recommended for apps that post
96 * {@link android.app.Notification Notifications}.
97 */
98 @SdkConstant(SdkConstantType.INTENT_CATEGORY)
99 public static final String INTENT_CATEGORY_NOTIFICATION_PREFERENCES
100 = "android.intent.category.NOTIFICATION_PREFERENCES";
101
102 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800103 * Use all default values (where applicable).
104 */
105 public static final int DEFAULT_ALL = ~0;
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500106
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800107 /**
108 * Use the default notification sound. This will ignore any given
109 * {@link #sound}.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500110 *
Chris Wren47c20a12014-06-18 17:27:29 -0400111 * <p>
112 * A notification that is noisy is more likely to be presented as a heads-up notification.
113 * </p>
114 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800115 * @see #defaults
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500116 */
117
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800118 public static final int DEFAULT_SOUND = 1;
119
120 /**
121 * Use the default notification vibrate. This will ignore any given
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500122 * {@link #vibrate}. Using phone vibration requires the
Scott Mainb8b36452009-04-26 15:50:49 -0700123 * {@link android.Manifest.permission#VIBRATE VIBRATE} permission.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500124 *
Chris Wren47c20a12014-06-18 17:27:29 -0400125 * <p>
126 * A notification that vibrates is more likely to be presented as a heads-up notification.
127 * </p>
128 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800129 * @see #defaults
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500130 */
131
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800132 public static final int DEFAULT_VIBRATE = 2;
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500133
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800134 /**
135 * Use the default notification lights. This will ignore the
136 * {@link #FLAG_SHOW_LIGHTS} bit, and {@link #ledARGB}, {@link #ledOffMS}, or
137 * {@link #ledOnMS}.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500138 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800139 * @see #defaults
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500140 */
141
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800142 public static final int DEFAULT_LIGHTS = 4;
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500143
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800144 /**
Christoph Studer535ec612014-09-03 15:47:47 +0200145 * Maximum length of CharSequences accepted by Builder and friends.
146 *
147 * <p>
148 * Avoids spamming the system with overly large strings such as full e-mails.
149 */
150 private static final int MAX_CHARSEQUENCE_LENGTH = 5 * 1024;
151
152 /**
Adrian Roose458aa82015-12-08 16:17:19 -0800153 * Maximum entries of reply text that are accepted by Builder and friends.
154 */
155 private static final int MAX_REPLY_HISTORY = 5;
156
157 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500158 * A timestamp related to this notification, in milliseconds since the epoch.
Joe Malin8d40d042012-11-05 11:36:40 -0800159 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500160 * Default value: {@link System#currentTimeMillis() Now}.
161 *
162 * Choose a timestamp that will be most relevant to the user. For most finite events, this
163 * corresponds to the time the event happened (or will happen, in the case of events that have
164 * yet to occur but about which the user is being informed). Indefinite events should be
Joe Malin8d40d042012-11-05 11:36:40 -0800165 * timestamped according to when the activity began.
166 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500167 * Some examples:
Joe Malin8d40d042012-11-05 11:36:40 -0800168 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500169 * <ul>
170 * <li>Notification of a new chat message should be stamped when the message was received.</li>
171 * <li>Notification of an ongoing file download (with a progress bar, for example) should be stamped when the download started.</li>
172 * <li>Notification of a completed file download should be stamped when the download finished.</li>
173 * <li>Notification of an upcoming meeting should be stamped with the time the meeting will begin (that is, in the future).</li>
174 * <li>Notification of an ongoing stopwatch (increasing timer) should be stamped with the watch's start time.
175 * <li>Notification of an ongoing countdown timer should be stamped with the timer's end time.
Joe Malin8d40d042012-11-05 11:36:40 -0800176 * </ul>
177 *
Selim Cinek0ff1ce602016-04-05 18:27:16 -0700178 * For apps targeting {@link android.os.Build.VERSION_CODES#N} and above, this time is not shown
179 * anymore by default and must be opted into by using
180 * {@link android.app.Notification.Builder#setShowWhen(boolean)}
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800181 */
182 public long when;
183
184 /**
185 * The resource id of a drawable to use as the icon in the status bar.
Dan Sandler86647982015-05-13 23:41:13 -0400186 *
187 * @deprecated Use {@link Builder#setSmallIcon(Icon)} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800188 */
Dan Sandler86647982015-05-13 23:41:13 -0400189 @Deprecated
Tor Norbye7b9c9122013-05-30 16:48:33 -0700190 @DrawableRes
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800191 public int icon;
192
193 /**
Joe Onorato46439ce2010-11-19 13:56:21 -0800194 * If the icon in the status bar is to have more than one level, you can set this. Otherwise,
195 * leave it at its default value of 0.
196 *
197 * @see android.widget.ImageView#setImageLevel
Griff Hazen959591e2014-05-15 22:26:18 -0700198 * @see android.graphics.drawable.Drawable#setLevel
Joe Onorato46439ce2010-11-19 13:56:21 -0800199 */
200 public int iconLevel;
201
202 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500203 * The number of events that this notification represents. For example, in a new mail
204 * notification, this could be the number of unread messages.
Joe Malin8d40d042012-11-05 11:36:40 -0800205 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500206 * The system may or may not use this field to modify the appearance of the notification. For
207 * example, before {@link android.os.Build.VERSION_CODES#HONEYCOMB}, this number was
208 * superimposed over the icon in the status bar. Starting with
209 * {@link android.os.Build.VERSION_CODES#HONEYCOMB}, the template used by
210 * {@link Notification.Builder} has displayed the number in the expanded notification view.
Joe Malin8d40d042012-11-05 11:36:40 -0800211 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500212 * If the number is 0 or negative, it is never shown.
Selim Cinek0f9dd1e2016-04-05 17:03:40 -0700213 *
214 * @deprecated this number is not shown anymore
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800215 */
216 public int number;
217
218 /**
219 * The intent to execute when the expanded status entry is clicked. If
220 * this is an activity, it must include the
221 * {@link android.content.Intent#FLAG_ACTIVITY_NEW_TASK} flag, which requires
Scott Main7aee61f2011-02-08 11:25:01 -0800222 * that you take care of task management as described in the
223 * <a href="{@docRoot}guide/topics/fundamentals/tasks-and-back-stack.html">Tasks and Back
Dianne Hackborn6ceca582012-01-10 15:24:26 -0800224 * Stack</a> document. In particular, make sure to read the notification section
225 * <a href="{@docRoot}guide/topics/ui/notifiers/notifications.html#HandlingNotifications">Handling
226 * Notifications</a> for the correct ways to launch an application from a
227 * notification.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800228 */
229 public PendingIntent contentIntent;
230
231 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500232 * The intent to execute when the notification is explicitly dismissed by the user, either with
233 * the "Clear All" button or by swiping it away individually.
234 *
235 * This probably shouldn't be launching an activity since several of those will be sent
236 * at the same time.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800237 */
238 public PendingIntent deleteIntent;
239
240 /**
Dianne Hackborn170bae72010-09-03 15:14:28 -0700241 * An intent to launch instead of posting the notification to the status bar.
Joe Onoratocb109a02011-01-18 17:57:41 -0800242 *
Chris Wren47c20a12014-06-18 17:27:29 -0400243 * <p>
244 * The system UI may choose to display a heads-up notification, instead of
245 * launching this intent, while the user is using the device.
246 * </p>
247 *
Joe Onoratocb109a02011-01-18 17:57:41 -0800248 * @see Notification.Builder#setFullScreenIntent
Daniel Sandlere46cbd32010-06-17 10:35:26 -0400249 */
250 public PendingIntent fullScreenIntent;
251
252 /**
Dan Sandler5fcdf6e2014-07-18 11:31:15 -0400253 * Text that summarizes this notification for accessibility services.
254 *
255 * As of the L release, this text is no longer shown on screen, but it is still useful to
256 * accessibility services (where it serves as an audible announcement of the notification's
257 * appearance).
Joe Onoratoef1e7762010-09-17 18:38:38 -0400258 *
Joe Onorato46439ce2010-11-19 13:56:21 -0800259 * @see #tickerView
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800260 */
261 public CharSequence tickerText;
262
263 /**
Dan Sandler5fcdf6e2014-07-18 11:31:15 -0400264 * Formerly, a view showing the {@link #tickerText}.
265 *
266 * No longer displayed in the status bar as of API 21.
Joe Onoratoef1e7762010-09-17 18:38:38 -0400267 */
Dan Sandler5fcdf6e2014-07-18 11:31:15 -0400268 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -0800269 public RemoteViews tickerView;
Joe Onoratoef1e7762010-09-17 18:38:38 -0400270
271 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400272 * The view that will represent this notification in the notification list (which is pulled
273 * down from the status bar).
274 *
Julia Reynoldsd4ea7412016-02-17 14:00:56 -0500275 * As of N, this field may be null. The notification view is determined by the inputs
276 * to {@link Notification.Builder}; a custom RemoteViews can optionally be
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400277 * supplied with {@link Notification.Builder#setCustomContentView(RemoteViews)}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800278 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400279 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800280 public RemoteViews contentView;
281
Daniel Sandlera0a938c2012-03-15 08:42:37 -0400282 /**
Daniel Sandler4dfbe832012-04-11 14:51:46 -0400283 * A large-format version of {@link #contentView}, giving the Notification an
Daniel Sandlerf3b73432012-03-27 15:01:25 -0400284 * opportunity to show more detail. The system UI may choose to show this
285 * instead of the normal content view at its discretion.
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400286 *
Julia Reynoldsd4ea7412016-02-17 14:00:56 -0500287 * As of N, this field may be null. The expanded notification view is determined by the
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400288 * inputs to {@link Notification.Builder}; a custom RemoteViews can optionally be
289 * supplied with {@link Notification.Builder#setCustomBigContentView(RemoteViews)}.
Daniel Sandlerf3b73432012-03-27 15:01:25 -0400290 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400291 @Deprecated
Daniel Sandlerf3b73432012-03-27 15:01:25 -0400292 public RemoteViews bigContentView;
293
Chris Wren8fd39ec2014-02-27 17:43:26 -0500294
295 /**
Chris Wren47c20a12014-06-18 17:27:29 -0400296 * A medium-format version of {@link #contentView}, providing the Notification an
297 * opportunity to add action buttons to contentView. At its discretion, the system UI may
298 * choose to show this as a heads-up notification, which will pop up so the user can see
299 * it without leaving their current activity.
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400300 *
Julia Reynoldsd4ea7412016-02-17 14:00:56 -0500301 * As of N, this field may be null. The heads-up notification view is determined by the
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400302 * inputs to {@link Notification.Builder}; a custom RemoteViews can optionally be
303 * supplied with {@link Notification.Builder#setCustomHeadsUpContentView(RemoteViews)}.
Chris Wren8fd39ec2014-02-27 17:43:26 -0500304 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400305 @Deprecated
Chris Wren8fd39ec2014-02-27 17:43:26 -0500306 public RemoteViews headsUpContentView;
307
Daniel Sandlerf3b73432012-03-27 15:01:25 -0400308 /**
Dan Sandler86647982015-05-13 23:41:13 -0400309 * A large bitmap to be shown in the notification content area.
310 *
311 * @deprecated Use {@link Builder#setLargeIcon(Icon)} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800312 */
Dan Sandler86647982015-05-13 23:41:13 -0400313 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -0800314 public Bitmap largeIcon;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800315
316 /**
317 * The sound to play.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500318 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800319 * <p>
Chris Wren47c20a12014-06-18 17:27:29 -0400320 * A notification that is noisy is more likely to be presented as a heads-up notification.
321 * </p>
322 *
323 * <p>
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500324 * To play the default notification sound, see {@link #defaults}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800325 * </p>
326 */
327 public Uri sound;
328
329 /**
330 * Use this constant as the value for audioStreamType to request that
331 * the default stream type for notifications be used. Currently the
Jeff Sharkey098d5802012-04-26 17:30:34 -0700332 * default stream type is {@link AudioManager#STREAM_NOTIFICATION}.
John Spurlockc0650f022014-07-19 13:22:39 -0400333 *
334 * @deprecated Use {@link #audioAttributes} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800335 */
Jean-Michel Trivi81f871e2014-08-06 16:32:38 -0700336 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800337 public static final int STREAM_DEFAULT = -1;
338
339 /**
340 * The audio stream type to use when playing the sound.
341 * Should be one of the STREAM_ constants from
342 * {@link android.media.AudioManager}.
John Spurlockc0650f022014-07-19 13:22:39 -0400343 *
344 * @deprecated Use {@link #audioAttributes} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800345 */
Jean-Michel Trivi81f871e2014-08-06 16:32:38 -0700346 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800347 public int audioStreamType = STREAM_DEFAULT;
348
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800349 /**
John Spurlockc0650f022014-07-19 13:22:39 -0400350 * The default value of {@link #audioAttributes}.
351 */
352 public static final AudioAttributes AUDIO_ATTRIBUTES_DEFAULT = new AudioAttributes.Builder()
353 .setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
354 .setUsage(AudioAttributes.USAGE_NOTIFICATION)
355 .build();
356
357 /**
358 * The {@link AudioAttributes audio attributes} to use when playing the sound.
359 */
360 public AudioAttributes audioAttributes = AUDIO_ATTRIBUTES_DEFAULT;
361
362 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500363 * The pattern with which to vibrate.
364 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800365 * <p>
366 * To vibrate the default pattern, see {@link #defaults}.
367 * </p>
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500368 *
Chris Wren47c20a12014-06-18 17:27:29 -0400369 * <p>
370 * A notification that vibrates is more likely to be presented as a heads-up notification.
371 * </p>
372 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800373 * @see android.os.Vibrator#vibrate(long[],int)
374 */
375 public long[] vibrate;
376
377 /**
378 * The color of the led. The hardware will do its best approximation.
379 *
380 * @see #FLAG_SHOW_LIGHTS
381 * @see #flags
382 */
Tor Norbye80756e32015-03-02 09:39:27 -0800383 @ColorInt
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800384 public int ledARGB;
385
386 /**
387 * The number of milliseconds for the LED to be on while it's flashing.
388 * The hardware will do its best approximation.
389 *
390 * @see #FLAG_SHOW_LIGHTS
391 * @see #flags
392 */
393 public int ledOnMS;
394
395 /**
396 * The number of milliseconds for the LED to be off while it's flashing.
397 * The hardware will do its best approximation.
398 *
399 * @see #FLAG_SHOW_LIGHTS
400 * @see #flags
401 */
402 public int ledOffMS;
403
404 /**
405 * Specifies which values should be taken from the defaults.
406 * <p>
407 * To set, OR the desired from {@link #DEFAULT_SOUND},
408 * {@link #DEFAULT_VIBRATE}, {@link #DEFAULT_LIGHTS}. For all default
409 * values, use {@link #DEFAULT_ALL}.
410 * </p>
411 */
412 public int defaults;
413
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800414 /**
415 * Bit to be bitwise-ored into the {@link #flags} field that should be
416 * set if you want the LED on for this notification.
417 * <ul>
418 * <li>To turn the LED off, pass 0 in the alpha channel for colorARGB
419 * or 0 for both ledOnMS and ledOffMS.</li>
420 * <li>To turn the LED on, pass 1 for ledOnMS and 0 for ledOffMS.</li>
421 * <li>To flash the LED, pass the number of milliseconds that it should
422 * be on and off to ledOnMS and ledOffMS.</li>
423 * </ul>
424 * <p>
425 * Since hardware varies, you are not guaranteed that any of the values
426 * you pass are honored exactly. Use the system defaults (TODO) if possible
427 * because they will be set to values that work on any given hardware.
428 * <p>
429 * The alpha channel must be set for forward compatibility.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500430 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800431 */
432 public static final int FLAG_SHOW_LIGHTS = 0x00000001;
433
434 /**
435 * Bit to be bitwise-ored into the {@link #flags} field that should be
436 * set if this notification is in reference to something that is ongoing,
437 * like a phone call. It should not be set if this notification is in
438 * reference to something that happened at a particular point in time,
439 * like a missed phone call.
440 */
441 public static final int FLAG_ONGOING_EVENT = 0x00000002;
442
443 /**
444 * Bit to be bitwise-ored into the {@link #flags} field that if set,
Scott Mainb8b36452009-04-26 15:50:49 -0700445 * the audio will be repeated until the notification is
446 * cancelled or the notification window is opened.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800447 */
448 public static final int FLAG_INSISTENT = 0x00000004;
449
450 /**
451 * Bit to be bitwise-ored into the {@link #flags} field that should be
Griff Hazen293977b2014-04-28 08:37:20 -0700452 * set if you would only like the sound, vibrate and ticker to be played
453 * if the notification was not already showing.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800454 */
455 public static final int FLAG_ONLY_ALERT_ONCE = 0x00000008;
456
457 /**
458 * Bit to be bitwise-ored into the {@link #flags} field that should be
459 * set if the notification should be canceled when it is clicked by the
Daniel Sandler8aa9ae62012-12-04 23:31:47 -0500460 * user.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800461 */
462 public static final int FLAG_AUTO_CANCEL = 0x00000010;
463
464 /**
465 * Bit to be bitwise-ored into the {@link #flags} field that should be
466 * set if the notification should not be canceled when the user clicks
467 * the Clear all button.
468 */
469 public static final int FLAG_NO_CLEAR = 0x00000020;
470
Dianne Hackbornd8a43f62009-08-17 23:33:56 -0700471 /**
472 * Bit to be bitwise-ored into the {@link #flags} field that should be
473 * set if this notification represents a currently running service. This
474 * will normally be set for you by {@link Service#startForeground}.
475 */
476 public static final int FLAG_FOREGROUND_SERVICE = 0x00000040;
477
Daniel Sandlere46cbd32010-06-17 10:35:26 -0400478 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500479 * Obsolete flag indicating high-priority notifications; use the priority field instead.
Joe Malin8d40d042012-11-05 11:36:40 -0800480 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500481 * @deprecated Use {@link #priority} with a positive value.
Daniel Sandlere46cbd32010-06-17 10:35:26 -0400482 */
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500483 public static final int FLAG_HIGH_PRIORITY = 0x00000080;
Daniel Sandlere46cbd32010-06-17 10:35:26 -0400484
Griff Hazendfcb0802014-02-11 12:00:00 -0800485 /**
486 * Bit to be bitswise-ored into the {@link #flags} field that should be
487 * set if this notification is relevant to the current device only
488 * and it is not recommended that it bridge to other devices.
489 */
490 public static final int FLAG_LOCAL_ONLY = 0x00000100;
491
Griff Hazen5cadc3b2014-05-20 09:55:39 -0700492 /**
493 * Bit to be bitswise-ored into the {@link #flags} field that should be
494 * set if this notification is the group summary for a group of notifications.
495 * Grouped notifications may display in a cluster or stack on devices which
496 * support such rendering. Requires a group key also be set using {@link Builder#setGroup}.
497 */
498 public static final int FLAG_GROUP_SUMMARY = 0x00000200;
499
Julia Reynoldse46bb372016-03-17 11:05:58 -0400500 /**
501 * Bit to be bitswise-ored into the {@link #flags} field that should be
502 * set if this notification is the group summary for an auto-group of notifications.
503 *
504 * @hide
505 */
506 @SystemApi
507 public static final int FLAG_AUTOGROUP_SUMMARY = 0x00000400;
508
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800509 public int flags;
510
Tor Norbyed9273d62013-05-30 15:59:53 -0700511 /** @hide */
512 @IntDef({PRIORITY_DEFAULT,PRIORITY_LOW,PRIORITY_MIN,PRIORITY_HIGH,PRIORITY_MAX})
513 @Retention(RetentionPolicy.SOURCE)
514 public @interface Priority {}
515
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800516 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500517 * Default notification {@link #priority}. If your application does not prioritize its own
518 * notifications, use this value for all notifications.
519 */
520 public static final int PRIORITY_DEFAULT = 0;
521
522 /**
523 * Lower {@link #priority}, for items that are less important. The UI may choose to show these
524 * items smaller, or at a different position in the list, compared with your app's
525 * {@link #PRIORITY_DEFAULT} items.
526 */
527 public static final int PRIORITY_LOW = -1;
528
529 /**
530 * Lowest {@link #priority}; these items might not be shown to the user except under special
531 * circumstances, such as detailed notification logs.
532 */
533 public static final int PRIORITY_MIN = -2;
534
535 /**
536 * Higher {@link #priority}, for more important notifications or alerts. The UI may choose to
537 * show these items larger, or at a different position in notification lists, compared with
538 * your app's {@link #PRIORITY_DEFAULT} items.
539 */
540 public static final int PRIORITY_HIGH = 1;
541
542 /**
543 * Highest {@link #priority}, for your application's most important items that require the
544 * user's prompt attention or input.
545 */
546 public static final int PRIORITY_MAX = 2;
547
548 /**
549 * Relative priority for this notification.
Joe Malin8d40d042012-11-05 11:36:40 -0800550 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500551 * Priority is an indication of how much of the user's valuable attention should be consumed by
552 * this notification. Low-priority notifications may be hidden from the user in certain
553 * situations, while the user might be interrupted for a higher-priority notification. The
Daniel Sandler6738eee2012-11-16 12:03:32 -0500554 * system will make a determination about how to interpret this priority when presenting
555 * the notification.
Chris Wren47c20a12014-06-18 17:27:29 -0400556 *
557 * <p>
558 * A notification that is at least {@link #PRIORITY_HIGH} is more likely to be presented
559 * as a heads-up notification.
560 * </p>
561 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500562 */
Tor Norbyed9273d62013-05-30 15:59:53 -0700563 @Priority
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500564 public int priority;
Joe Malin8d40d042012-11-05 11:36:40 -0800565
Dan Sandler26e81cf2014-05-06 10:01:27 -0400566 /**
567 * Accent color (an ARGB integer like the constants in {@link android.graphics.Color})
568 * to be applied by the standard Style templates when presenting this notification.
569 *
570 * The current template design constructs a colorful header image by overlaying the
571 * {@link #icon} image (stenciled in white) atop a field of this color. Alpha components are
572 * ignored.
573 */
Tor Norbye80756e32015-03-02 09:39:27 -0800574 @ColorInt
Dan Sandler26e81cf2014-05-06 10:01:27 -0400575 public int color = COLOR_DEFAULT;
576
577 /**
578 * Special value of {@link #color} telling the system not to decorate this notification with
579 * any special color but instead use default colors when presenting this notification.
580 */
Tor Norbye80756e32015-03-02 09:39:27 -0800581 @ColorInt
Dan Sandler26e81cf2014-05-06 10:01:27 -0400582 public static final int COLOR_DEFAULT = 0; // AKA Color.TRANSPARENT
Dan Sandler0bf2ed82013-12-21 23:33:41 -0600583
584 /**
Adrian Roos4ff3b122016-02-01 12:26:13 -0800585 * Special value of {@link #color} used as a place holder for an invalid color.
586 */
587 @ColorInt
588 private static final int COLOR_INVALID = 1;
589
590 /**
Dan Sandler0bf2ed82013-12-21 23:33:41 -0600591 * Sphere of visibility of this notification, which affects how and when the SystemUI reveals
592 * the notification's presence and contents in untrusted situations (namely, on the secure
593 * lockscreen).
594 *
595 * The default level, {@link #VISIBILITY_PRIVATE}, behaves exactly as notifications have always
596 * done on Android: The notification's {@link #icon} and {@link #tickerText} (if available) are
597 * shown in all situations, but the contents are only available if the device is unlocked for
598 * the appropriate user.
599 *
600 * A more permissive policy can be expressed by {@link #VISIBILITY_PUBLIC}; such a notification
601 * can be read even in an "insecure" context (that is, above a secure lockscreen).
602 * To modify the public version of this notification—for example, to redact some portions—see
603 * {@link Builder#setPublicVersion(Notification)}.
604 *
605 * Finally, a notification can be made {@link #VISIBILITY_SECRET}, which will suppress its icon
606 * and ticker until the user has bypassed the lockscreen.
607 */
608 public int visibility;
609
Griff Hazenfc3922d2014-08-20 11:56:44 -0700610 /**
611 * Notification visibility: Show this notification in its entirety on all lockscreens.
612 *
613 * {@see #visibility}
614 */
Dan Sandler0bf2ed82013-12-21 23:33:41 -0600615 public static final int VISIBILITY_PUBLIC = 1;
Griff Hazenfc3922d2014-08-20 11:56:44 -0700616
617 /**
618 * Notification visibility: Show this notification on all lockscreens, but conceal sensitive or
619 * private information on secure lockscreens.
620 *
621 * {@see #visibility}
622 */
Dan Sandler0bf2ed82013-12-21 23:33:41 -0600623 public static final int VISIBILITY_PRIVATE = 0;
Griff Hazenfc3922d2014-08-20 11:56:44 -0700624
625 /**
626 * Notification visibility: Do not reveal any part of this notification on a secure lockscreen.
627 *
628 * {@see #visibility}
629 */
Dan Sandler0bf2ed82013-12-21 23:33:41 -0600630 public static final int VISIBILITY_SECRET = -1;
631
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500632 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400633 * Notification category: incoming call (voice or video) or similar synchronous communication request.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500634 */
John Spurlockfd7f1e02014-03-18 16:41:57 -0400635 public static final String CATEGORY_CALL = "call";
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500636
637 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400638 * Notification category: incoming direct message (SMS, instant message, etc.).
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500639 */
John Spurlockfd7f1e02014-03-18 16:41:57 -0400640 public static final String CATEGORY_MESSAGE = "msg";
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500641
642 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400643 * Notification category: asynchronous bulk message (email).
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500644 */
John Spurlockfd7f1e02014-03-18 16:41:57 -0400645 public static final String CATEGORY_EMAIL = "email";
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500646
647 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400648 * Notification category: calendar event.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500649 */
John Spurlockfd7f1e02014-03-18 16:41:57 -0400650 public static final String CATEGORY_EVENT = "event";
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500651
652 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400653 * Notification category: promotion or advertisement.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500654 */
John Spurlockfd7f1e02014-03-18 16:41:57 -0400655 public static final String CATEGORY_PROMO = "promo";
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500656
657 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400658 * Notification category: alarm or timer.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500659 */
John Spurlockfd7f1e02014-03-18 16:41:57 -0400660 public static final String CATEGORY_ALARM = "alarm";
661
662 /**
663 * Notification category: progress of a long-running background operation.
664 */
665 public static final String CATEGORY_PROGRESS = "progress";
666
667 /**
668 * Notification category: social network or sharing update.
669 */
670 public static final String CATEGORY_SOCIAL = "social";
671
672 /**
673 * Notification category: error in background operation or authentication status.
674 */
675 public static final String CATEGORY_ERROR = "err";
676
677 /**
678 * Notification category: media transport control for playback.
679 */
680 public static final String CATEGORY_TRANSPORT = "transport";
681
682 /**
683 * Notification category: system or device status update. Reserved for system use.
684 */
685 public static final String CATEGORY_SYSTEM = "sys";
686
687 /**
688 * Notification category: indication of running background service.
689 */
690 public static final String CATEGORY_SERVICE = "service";
691
692 /**
John Spurlock0a69c8c2014-03-21 13:30:57 -0400693 * Notification category: a specific, timely recommendation for a single thing.
694 * For example, a news app might want to recommend a news story it believes the user will
695 * want to read next.
696 */
697 public static final String CATEGORY_RECOMMENDATION = "recommendation";
698
699 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400700 * Notification category: ongoing information about device or contextual status.
701 */
702 public static final String CATEGORY_STATUS = "status";
703
704 /**
John Spurlock24d3dad2015-04-02 12:24:02 -0400705 * Notification category: user-scheduled reminder.
706 */
707 public static final String CATEGORY_REMINDER = "reminder";
708
709 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400710 * One of the predefined notification categories (see the <code>CATEGORY_*</code> constants)
711 * that best describes this Notification. May be used by the system for ranking and filtering.
712 */
713 public String category;
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500714
Griff Hazen5cadc3b2014-05-20 09:55:39 -0700715 private String mGroupKey;
716
717 /**
718 * Get the key used to group this notification into a cluster or stack
719 * with other notifications on devices which support such rendering.
720 */
721 public String getGroup() {
722 return mGroupKey;
723 }
724
725 private String mSortKey;
726
727 /**
728 * Get a sort key that orders this notification among other notifications from the
729 * same package. This can be useful if an external sort was already applied and an app
730 * would like to preserve this. Notifications will be sorted lexicographically using this
731 * value, although providing different priorities in addition to providing sort key may
732 * cause this value to be ignored.
733 *
734 * <p>This sort key can also be used to order members of a notification group. See
735 * {@link Builder#setGroup}.
736 *
737 * @see String#compareTo(String)
738 */
739 public String getSortKey() {
740 return mSortKey;
741 }
742
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500743 /**
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400744 * Additional semantic data to be carried around with this Notification.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400745 * <p>
746 * The extras keys defined here are intended to capture the original inputs to {@link Builder}
747 * APIs, and are intended to be used by
748 * {@link android.service.notification.NotificationListenerService} implementations to extract
749 * detailed information from notification objects.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500750 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400751 public Bundle extras = new Bundle();
752
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400753 /**
754 * {@link #extras} key: this is the title of the notification,
755 * as supplied to {@link Builder#setContentTitle(CharSequence)}.
756 */
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -0500757 public static final String EXTRA_TITLE = "android.title";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400758
759 /**
760 * {@link #extras} key: this is the title of the notification when shown in expanded form,
761 * e.g. as supplied to {@link BigTextStyle#setBigContentTitle(CharSequence)}.
762 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400763 public static final String EXTRA_TITLE_BIG = EXTRA_TITLE + ".big";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400764
765 /**
766 * {@link #extras} key: this is the main text payload, as supplied to
767 * {@link Builder#setContentText(CharSequence)}.
768 */
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -0500769 public static final String EXTRA_TEXT = "android.text";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400770
771 /**
772 * {@link #extras} key: this is a third line of text, as supplied to
773 * {@link Builder#setSubText(CharSequence)}.
774 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400775 public static final String EXTRA_SUB_TEXT = "android.subText";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400776
777 /**
Adrian Roose458aa82015-12-08 16:17:19 -0800778 * {@link #extras} key: this is the remote input history, as supplied to
779 * {@link Builder#setRemoteInputHistory(CharSequence[])}.
Adrian Roos005e7742016-04-13 15:02:20 -0700780 *
781 * Apps can fill this through {@link Builder#setRemoteInputHistory(CharSequence[])}
782 * with the most recent inputs that have been sent through a {@link RemoteInput} of this
783 * Notification and are expected to clear it once the it is no longer relevant (e.g. for chat
784 * notifications once the other party has responded).
785 *
786 * The extra with this key is of type CharSequence[] and contains the most recent entry at
787 * the 0 index, the second most recent at the 1 index, etc.
788 *
789 * @see Builder#setRemoteInputHistory(CharSequence[])
Adrian Roose458aa82015-12-08 16:17:19 -0800790 */
791 public static final String EXTRA_REMOTE_INPUT_HISTORY = "android.remoteInputHistory";
792
793 /**
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400794 * {@link #extras} key: this is a small piece of additional text as supplied to
795 * {@link Builder#setContentInfo(CharSequence)}.
796 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400797 public static final String EXTRA_INFO_TEXT = "android.infoText";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400798
799 /**
800 * {@link #extras} key: this is a line of summary information intended to be shown
801 * alongside expanded notifications, as supplied to (e.g.)
802 * {@link BigTextStyle#setSummaryText(CharSequence)}.
803 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400804 public static final String EXTRA_SUMMARY_TEXT = "android.summaryText";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400805
806 /**
Christoph Studer4600f9b2014-07-22 22:44:43 +0200807 * {@link #extras} key: this is the longer text shown in the big form of a
808 * {@link BigTextStyle} notification, as supplied to
809 * {@link BigTextStyle#bigText(CharSequence)}.
810 */
811 public static final String EXTRA_BIG_TEXT = "android.bigText";
812
813 /**
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400814 * {@link #extras} key: this is the resource ID of the notification's main small icon, as
815 * supplied to {@link Builder#setSmallIcon(int)}.
816 */
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -0500817 public static final String EXTRA_SMALL_ICON = "android.icon";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400818
819 /**
820 * {@link #extras} key: this is a bitmap to be used instead of the small icon when showing the
821 * notification payload, as
822 * supplied to {@link Builder#setLargeIcon(android.graphics.Bitmap)}.
823 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400824 public static final String EXTRA_LARGE_ICON = "android.largeIcon";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400825
826 /**
827 * {@link #extras} key: this is a bitmap to be used instead of the one from
828 * {@link Builder#setLargeIcon(android.graphics.Bitmap)} when the notification is
829 * shown in its expanded form, as supplied to
830 * {@link BigPictureStyle#bigLargeIcon(android.graphics.Bitmap)}.
831 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400832 public static final String EXTRA_LARGE_ICON_BIG = EXTRA_LARGE_ICON + ".big";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400833
834 /**
835 * {@link #extras} key: this is the progress value supplied to
836 * {@link Builder#setProgress(int, int, boolean)}.
837 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400838 public static final String EXTRA_PROGRESS = "android.progress";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400839
840 /**
841 * {@link #extras} key: this is the maximum value supplied to
842 * {@link Builder#setProgress(int, int, boolean)}.
843 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400844 public static final String EXTRA_PROGRESS_MAX = "android.progressMax";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400845
846 /**
847 * {@link #extras} key: whether the progress bar is indeterminate, supplied to
848 * {@link Builder#setProgress(int, int, boolean)}.
849 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400850 public static final String EXTRA_PROGRESS_INDETERMINATE = "android.progressIndeterminate";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400851
852 /**
853 * {@link #extras} key: whether {@link #when} should be shown as a count-up timer (specifically
854 * a {@link android.widget.Chronometer}) instead of a timestamp, as supplied to
855 * {@link Builder#setUsesChronometer(boolean)}.
856 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400857 public static final String EXTRA_SHOW_CHRONOMETER = "android.showChronometer";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400858
859 /**
Selim Cinek81c23aa2016-02-25 16:23:13 -0800860 * {@link #extras} key: whether the chronometer set on the notification should count down
861 * instead of counting up. Is only relevant if key {@link #EXTRA_SHOW_CHRONOMETER} is present.
862 */
863 public static final String EXTRA_CHRONOMETER_COUNTS_DOWN = "android.chronometerCountsDown";
864
865 /**
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400866 * {@link #extras} key: whether {@link #when} should be shown,
867 * as supplied to {@link Builder#setShowWhen(boolean)}.
868 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400869 public static final String EXTRA_SHOW_WHEN = "android.showWhen";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400870
871 /**
872 * {@link #extras} key: this is a bitmap to be shown in {@link BigPictureStyle} expanded
873 * notifications, supplied to {@link BigPictureStyle#bigPicture(android.graphics.Bitmap)}.
874 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400875 public static final String EXTRA_PICTURE = "android.picture";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400876
877 /**
878 * {@link #extras} key: An array of CharSequences to show in {@link InboxStyle} expanded
879 * notifications, each of which was supplied to {@link InboxStyle#addLine(CharSequence)}.
880 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400881 public static final String EXTRA_TEXT_LINES = "android.textLines";
Dan Sandler842dd772014-05-15 09:36:47 -0400882
883 /**
884 * {@link #extras} key: A string representing the name of the specific
885 * {@link android.app.Notification.Style} used to create this notification.
886 */
Chris Wren91ad5632013-06-05 15:05:57 -0400887 public static final String EXTRA_TEMPLATE = "android.template";
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400888
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400889 /**
Chris Wrene6c48932014-09-29 17:19:27 -0400890 * {@link #extras} key: A String array containing the people that this notification relates to,
891 * each of which was supplied to {@link Builder#addPerson(String)}.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400892 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400893 public static final String EXTRA_PEOPLE = "android.people";
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500894
895 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400896 * Allow certain system-generated notifications to appear before the device is provisioned.
897 * Only available to notifications coming from the android package.
898 * @hide
899 */
900 public static final String EXTRA_ALLOW_DURING_SETUP = "android.allowDuringSetup";
901
902 /**
Jose Limae9e3b3b2014-05-18 23:44:50 -0700903 * {@link #extras} key: A
904 * {@link android.content.ContentUris content URI} pointing to an image that can be displayed
905 * in the background when the notification is selected. The URI must point to an image stream
906 * suitable for passing into
907 * {@link android.graphics.BitmapFactory#decodeStream(java.io.InputStream)
908 * BitmapFactory.decodeStream}; all other content types will be ignored. The content provider
909 * URI used for this purpose must require no permissions to read the image data.
910 */
911 public static final String EXTRA_BACKGROUND_IMAGE_URI = "android.backgroundImageUri";
912
913 /**
Dan Sandler842dd772014-05-15 09:36:47 -0400914 * {@link #extras} key: A
Jeff Browndba34ba2014-06-24 20:46:03 -0700915 * {@link android.media.session.MediaSession.Token} associated with a
Dan Sandler842dd772014-05-15 09:36:47 -0400916 * {@link android.app.Notification.MediaStyle} notification.
917 */
918 public static final String EXTRA_MEDIA_SESSION = "android.mediaSession";
919
920 /**
Bryan Mawhinneye191f902014-07-22 12:50:09 +0100921 * {@link #extras} key: the indices of actions to be shown in the compact view,
922 * as supplied to (e.g.) {@link MediaStyle#setShowActionsInCompactView(int...)}.
923 */
924 public static final String EXTRA_COMPACT_ACTIONS = "android.compactActions";
925
Christoph Studer943aa672014-08-03 20:31:16 +0200926 /**
Alex Hillsfc737de2016-03-23 17:33:02 -0400927 * {@link #extras} key: the username to be displayed for all messages sent by the user including
928 * direct replies
929 * {@link android.app.Notification.MessagingStyle} notification.
930 */
931 public static final String EXTRA_SELF_DISPLAY_NAME = "android.selfDisplayName";
932
933 /**
934 * {@link #extras} key: a boolean describing whether the platform should automatically
935 * generate possible replies to
936 * {@link android.app.Notification.MessagingStyle.Message} objects provided by a
937 * {@link android.app.Notification.MessagingStyle} notification.
938 */
939 public static final String EXTRA_ALLOW_GENERATED_REPLIES = "android.allowGeneratedReplies";
940
941 /**
942 * {@link #extras} key: a {@link String} to be displayed as the title to a thread represented by
943 * a {@link android.app.Notification.MessagingStyle}
944 */
945 public static final String EXTRA_THREAD_TITLE = "android.threadTitle";
946
947 /**
948 * {@link #extras} key: an array of {@link android.app.Notification.MessagingStyle.Message}
949 * bundles provided by a
950 * {@link android.app.Notification.MessagingStyle} notification.
951 */
952 public static final String EXTRA_MESSAGES = "android.messages";
953
954 /**
Kenny Guy8942bcd2014-09-08 21:09:47 +0100955 * {@link #extras} key: the user that built the notification.
956 *
957 * @hide
958 */
959 public static final String EXTRA_ORIGINATING_USERID = "android.originatingUserId";
960
961 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400962 * @hide
963 */
964 public static final String EXTRA_BUILDER_APPLICATION_INFO = "android.appInfo";
965
Selim Cinek247fa012016-02-18 09:50:48 -0800966 /**
967 * @hide
968 */
969 public static final String EXTRA_CONTAINS_CUSTOM_VIEW = "android.contains.customView";
970
Dan Sandlerd63f9322015-05-06 15:18:49 -0400971 private Icon mSmallIcon;
972 private Icon mLargeIcon;
973
Chris Wren51c75102013-07-16 20:49:17 -0400974 /**
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400975 * Structure to encapsulate a named action that can be shown as part of this notification.
976 * It must include an icon, a label, and a {@link PendingIntent} to be fired when the action is
977 * selected by the user.
978 * <p>
Griff Hazen959591e2014-05-15 22:26:18 -0700979 * Apps should use {@link Notification.Builder#addAction(int, CharSequence, PendingIntent)}
980 * or {@link Notification.Builder#addAction(Notification.Action)}
981 * to attach actions.
Daniel Sandlera0a938c2012-03-15 08:42:37 -0400982 */
Daniel Sandlerea2a3172013-02-20 22:24:20 -0500983 public static class Action implements Parcelable {
Griff Hazen959591e2014-05-15 22:26:18 -0700984 private final Bundle mExtras;
Dan Sandler86647982015-05-13 23:41:13 -0400985 private Icon mIcon;
Griff Hazen61a9e862014-05-22 16:05:19 -0700986 private final RemoteInput[] mRemoteInputs;
Griff Hazen959591e2014-05-15 22:26:18 -0700987
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400988 /**
989 * Small icon representing the action.
Dan Sandler86647982015-05-13 23:41:13 -0400990 *
991 * @deprecated Use {@link Action#getIcon()} instead.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400992 */
Dan Sandler86647982015-05-13 23:41:13 -0400993 @Deprecated
Daniel Sandlera0a938c2012-03-15 08:42:37 -0400994 public int icon;
Griff Hazen5cadc3b2014-05-20 09:55:39 -0700995
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400996 /**
997 * Title of the action.
998 */
Daniel Sandlera0a938c2012-03-15 08:42:37 -0400999 public CharSequence title;
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001000
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001001 /**
1002 * Intent to send when the user invokes this action. May be null, in which case the action
1003 * may be rendered in a disabled presentation by the system UI.
1004 */
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001005 public PendingIntent actionIntent;
Griff Hazen959591e2014-05-15 22:26:18 -07001006
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001007 private Action(Parcel in) {
Dan Sandler86647982015-05-13 23:41:13 -04001008 if (in.readInt() != 0) {
1009 mIcon = Icon.CREATOR.createFromParcel(in);
Dan Sandler68079d52015-07-22 10:45:30 -04001010 if (mIcon.getType() == Icon.TYPE_RESOURCE) {
1011 icon = mIcon.getResId();
1012 }
Dan Sandler86647982015-05-13 23:41:13 -04001013 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001014 title = TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(in);
1015 if (in.readInt() == 1) {
1016 actionIntent = PendingIntent.CREATOR.createFromParcel(in);
1017 }
Jeff Sharkeya04c7a72016-03-18 12:20:36 -06001018 mExtras = Bundle.setDefusable(in.readBundle(), true);
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001019 mRemoteInputs = in.createTypedArray(RemoteInput.CREATOR);
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001020 }
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001021
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001022 /**
Dan Sandler86647982015-05-13 23:41:13 -04001023 * @deprecated Use {@link android.app.Notification.Action.Builder}.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001024 */
Dan Sandler86647982015-05-13 23:41:13 -04001025 @Deprecated
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001026 public Action(int icon, CharSequence title, PendingIntent intent) {
Dan Sandler86647982015-05-13 23:41:13 -04001027 this(Icon.createWithResource("", icon), title, intent, new Bundle(), null);
Griff Hazen959591e2014-05-15 22:26:18 -07001028 }
1029
Dan Sandler86647982015-05-13 23:41:13 -04001030 private Action(Icon icon, CharSequence title, PendingIntent intent, Bundle extras,
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001031 RemoteInput[] remoteInputs) {
Dan Sandler86647982015-05-13 23:41:13 -04001032 this.mIcon = icon;
Gus Prevasf5bff46f2015-08-24 10:34:28 -04001033 if (icon != null && icon.getType() == Icon.TYPE_RESOURCE) {
1034 this.icon = icon.getResId();
1035 }
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001036 this.title = title;
1037 this.actionIntent = intent;
Griff Hazen959591e2014-05-15 22:26:18 -07001038 this.mExtras = extras != null ? extras : new Bundle();
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001039 this.mRemoteInputs = remoteInputs;
Griff Hazen959591e2014-05-15 22:26:18 -07001040 }
1041
1042 /**
Dan Sandler86647982015-05-13 23:41:13 -04001043 * Return an icon representing the action.
1044 */
1045 public Icon getIcon() {
1046 if (mIcon == null && icon != 0) {
1047 // you snuck an icon in here without using the builder; let's try to keep it
1048 mIcon = Icon.createWithResource("", icon);
1049 }
1050 return mIcon;
1051 }
1052
1053 /**
Griff Hazen959591e2014-05-15 22:26:18 -07001054 * Get additional metadata carried around with this Action.
1055 */
1056 public Bundle getExtras() {
1057 return mExtras;
1058 }
1059
1060 /**
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001061 * Get the list of inputs to be collected from the user when this action is sent.
1062 * May return null if no remote inputs were added.
1063 */
1064 public RemoteInput[] getRemoteInputs() {
1065 return mRemoteInputs;
1066 }
1067
1068 /**
Griff Hazen959591e2014-05-15 22:26:18 -07001069 * Builder class for {@link Action} objects.
1070 */
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001071 public static final class Builder {
Dan Sandler86647982015-05-13 23:41:13 -04001072 private final Icon mIcon;
Griff Hazen959591e2014-05-15 22:26:18 -07001073 private final CharSequence mTitle;
1074 private final PendingIntent mIntent;
1075 private final Bundle mExtras;
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001076 private ArrayList<RemoteInput> mRemoteInputs;
Griff Hazen959591e2014-05-15 22:26:18 -07001077
1078 /**
1079 * Construct a new builder for {@link Action} object.
1080 * @param icon icon to show for this action
1081 * @param title the title of the action
1082 * @param intent the {@link PendingIntent} to fire when users trigger this action
1083 */
Dan Sandler86647982015-05-13 23:41:13 -04001084 @Deprecated
Griff Hazen959591e2014-05-15 22:26:18 -07001085 public Builder(int icon, CharSequence title, PendingIntent intent) {
Dan Sandler86647982015-05-13 23:41:13 -04001086 this(Icon.createWithResource("", icon), title, intent, new Bundle(), null);
1087 }
1088
1089 /**
1090 * Construct a new builder for {@link Action} object.
1091 * @param icon icon to show for this action
1092 * @param title the title of the action
1093 * @param intent the {@link PendingIntent} to fire when users trigger this action
1094 */
1095 public Builder(Icon icon, CharSequence title, PendingIntent intent) {
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001096 this(icon, title, intent, new Bundle(), null);
Griff Hazen959591e2014-05-15 22:26:18 -07001097 }
1098
1099 /**
1100 * Construct a new builder for {@link Action} object using the fields from an
1101 * {@link Action}.
1102 * @param action the action to read fields from.
1103 */
1104 public Builder(Action action) {
Dan Sandler86647982015-05-13 23:41:13 -04001105 this(action.getIcon(), action.title, action.actionIntent, new Bundle(action.mExtras),
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001106 action.getRemoteInputs());
Griff Hazen959591e2014-05-15 22:26:18 -07001107 }
1108
Dan Sandler86647982015-05-13 23:41:13 -04001109 private Builder(Icon icon, CharSequence title, PendingIntent intent, Bundle extras,
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001110 RemoteInput[] remoteInputs) {
Griff Hazen959591e2014-05-15 22:26:18 -07001111 mIcon = icon;
1112 mTitle = title;
1113 mIntent = intent;
1114 mExtras = extras;
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001115 if (remoteInputs != null) {
1116 mRemoteInputs = new ArrayList<RemoteInput>(remoteInputs.length);
1117 Collections.addAll(mRemoteInputs, remoteInputs);
1118 }
Griff Hazen959591e2014-05-15 22:26:18 -07001119 }
1120
1121 /**
1122 * Merge additional metadata into this builder.
1123 *
1124 * <p>Values within the Bundle will replace existing extras values in this Builder.
1125 *
1126 * @see Notification.Action#extras
1127 */
1128 public Builder addExtras(Bundle extras) {
1129 if (extras != null) {
1130 mExtras.putAll(extras);
1131 }
1132 return this;
1133 }
1134
1135 /**
1136 * Get the metadata Bundle used by this Builder.
1137 *
1138 * <p>The returned Bundle is shared with this Builder.
1139 */
1140 public Bundle getExtras() {
1141 return mExtras;
1142 }
1143
1144 /**
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001145 * Add an input to be collected from the user when this action is sent.
1146 * Response values can be retrieved from the fired intent by using the
1147 * {@link RemoteInput#getResultsFromIntent} function.
1148 * @param remoteInput a {@link RemoteInput} to add to the action
1149 * @return this object for method chaining
1150 */
1151 public Builder addRemoteInput(RemoteInput remoteInput) {
1152 if (mRemoteInputs == null) {
1153 mRemoteInputs = new ArrayList<RemoteInput>();
1154 }
1155 mRemoteInputs.add(remoteInput);
1156 return this;
1157 }
1158
1159 /**
1160 * Apply an extender to this action builder. Extenders may be used to add
1161 * metadata or change options on this builder.
1162 */
Griff Hazen61a9e862014-05-22 16:05:19 -07001163 public Builder extend(Extender extender) {
1164 extender.extend(this);
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001165 return this;
1166 }
1167
1168 /**
Griff Hazen959591e2014-05-15 22:26:18 -07001169 * Combine all of the options that have been set and return a new {@link Action}
1170 * object.
1171 * @return the built action
1172 */
1173 public Action build() {
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001174 RemoteInput[] remoteInputs = mRemoteInputs != null
1175 ? mRemoteInputs.toArray(new RemoteInput[mRemoteInputs.size()]) : null;
1176 return new Action(mIcon, mTitle, mIntent, mExtras, remoteInputs);
Griff Hazen959591e2014-05-15 22:26:18 -07001177 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001178 }
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001179
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001180 @Override
1181 public Action clone() {
1182 return new Action(
Dan Sandler86647982015-05-13 23:41:13 -04001183 getIcon(),
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001184 title,
1185 actionIntent, // safe to alias
1186 new Bundle(mExtras),
1187 getRemoteInputs());
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001188 }
1189 @Override
1190 public int describeContents() {
1191 return 0;
1192 }
1193 @Override
1194 public void writeToParcel(Parcel out, int flags) {
Dan Sandler86647982015-05-13 23:41:13 -04001195 final Icon ic = getIcon();
1196 if (ic != null) {
1197 out.writeInt(1);
1198 ic.writeToParcel(out, 0);
1199 } else {
1200 out.writeInt(0);
1201 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001202 TextUtils.writeToParcel(title, out, flags);
1203 if (actionIntent != null) {
1204 out.writeInt(1);
1205 actionIntent.writeToParcel(out, flags);
1206 } else {
1207 out.writeInt(0);
1208 }
Griff Hazen959591e2014-05-15 22:26:18 -07001209 out.writeBundle(mExtras);
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001210 out.writeTypedArray(mRemoteInputs, flags);
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001211 }
Griff Hazen959591e2014-05-15 22:26:18 -07001212 public static final Parcelable.Creator<Action> CREATOR =
1213 new Parcelable.Creator<Action>() {
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001214 public Action createFromParcel(Parcel in) {
1215 return new Action(in);
1216 }
1217 public Action[] newArray(int size) {
1218 return new Action[size];
1219 }
1220 };
Griff Hazen61a9e862014-05-22 16:05:19 -07001221
1222 /**
1223 * Extender interface for use with {@link Builder#extend}. Extenders may be used to add
1224 * metadata or change options on an action builder.
1225 */
1226 public interface Extender {
1227 /**
1228 * Apply this extender to a notification action builder.
1229 * @param builder the builder to be modified.
1230 * @return the build object for chaining.
1231 */
1232 public Builder extend(Builder builder);
1233 }
1234
1235 /**
1236 * Wearable extender for notification actions. To add extensions to an action,
1237 * create a new {@link android.app.Notification.Action.WearableExtender} object using
1238 * the {@code WearableExtender()} constructor and apply it to a
1239 * {@link android.app.Notification.Action.Builder} using
1240 * {@link android.app.Notification.Action.Builder#extend}.
1241 *
1242 * <pre class="prettyprint">
1243 * Notification.Action action = new Notification.Action.Builder(
1244 * R.drawable.archive_all, "Archive all", actionIntent)
Griff Hazen14f57992014-05-26 09:07:14 -07001245 * .extend(new Notification.Action.WearableExtender()
Griff Hazen61a9e862014-05-22 16:05:19 -07001246 * .setAvailableOffline(false))
Griff Hazen14f57992014-05-26 09:07:14 -07001247 * .build();</pre>
Griff Hazen61a9e862014-05-22 16:05:19 -07001248 */
1249 public static final class WearableExtender implements Extender {
1250 /** Notification action extra which contains wearable extensions */
1251 private static final String EXTRA_WEARABLE_EXTENSIONS = "android.wearable.EXTENSIONS";
1252
Pete Gastaf6781d2014-10-07 15:17:05 -04001253 // Keys within EXTRA_WEARABLE_EXTENSIONS for wearable options.
Griff Hazen61a9e862014-05-22 16:05:19 -07001254 private static final String KEY_FLAGS = "flags";
Pete Gastaf6781d2014-10-07 15:17:05 -04001255 private static final String KEY_IN_PROGRESS_LABEL = "inProgressLabel";
1256 private static final String KEY_CONFIRM_LABEL = "confirmLabel";
1257 private static final String KEY_CANCEL_LABEL = "cancelLabel";
Griff Hazen61a9e862014-05-22 16:05:19 -07001258
1259 // Flags bitwise-ored to mFlags
1260 private static final int FLAG_AVAILABLE_OFFLINE = 0x1;
Alex Hills9ab3a232016-04-05 14:54:56 -04001261 private static final int FLAG_HINT_LAUNCHES_ACTIVITY = 1 << 1;
Griff Hazen61a9e862014-05-22 16:05:19 -07001262
1263 // Default value for flags integer
1264 private static final int DEFAULT_FLAGS = FLAG_AVAILABLE_OFFLINE;
1265
1266 private int mFlags = DEFAULT_FLAGS;
1267
Pete Gastaf6781d2014-10-07 15:17:05 -04001268 private CharSequence mInProgressLabel;
1269 private CharSequence mConfirmLabel;
1270 private CharSequence mCancelLabel;
1271
Griff Hazen61a9e862014-05-22 16:05:19 -07001272 /**
1273 * Create a {@link android.app.Notification.Action.WearableExtender} with default
1274 * options.
1275 */
1276 public WearableExtender() {
1277 }
1278
1279 /**
1280 * Create a {@link android.app.Notification.Action.WearableExtender} by reading
1281 * wearable options present in an existing notification action.
1282 * @param action the notification action to inspect.
1283 */
1284 public WearableExtender(Action action) {
1285 Bundle wearableBundle = action.getExtras().getBundle(EXTRA_WEARABLE_EXTENSIONS);
1286 if (wearableBundle != null) {
1287 mFlags = wearableBundle.getInt(KEY_FLAGS, DEFAULT_FLAGS);
Pete Gastaf6781d2014-10-07 15:17:05 -04001288 mInProgressLabel = wearableBundle.getCharSequence(KEY_IN_PROGRESS_LABEL);
1289 mConfirmLabel = wearableBundle.getCharSequence(KEY_CONFIRM_LABEL);
1290 mCancelLabel = wearableBundle.getCharSequence(KEY_CANCEL_LABEL);
Griff Hazen61a9e862014-05-22 16:05:19 -07001291 }
1292 }
1293
1294 /**
1295 * Apply wearable extensions to a notification action that is being built. This is
1296 * typically called by the {@link android.app.Notification.Action.Builder#extend}
1297 * method of {@link android.app.Notification.Action.Builder}.
1298 */
1299 @Override
1300 public Action.Builder extend(Action.Builder builder) {
1301 Bundle wearableBundle = new Bundle();
1302
1303 if (mFlags != DEFAULT_FLAGS) {
1304 wearableBundle.putInt(KEY_FLAGS, mFlags);
1305 }
Pete Gastaf6781d2014-10-07 15:17:05 -04001306 if (mInProgressLabel != null) {
1307 wearableBundle.putCharSequence(KEY_IN_PROGRESS_LABEL, mInProgressLabel);
1308 }
1309 if (mConfirmLabel != null) {
1310 wearableBundle.putCharSequence(KEY_CONFIRM_LABEL, mConfirmLabel);
1311 }
1312 if (mCancelLabel != null) {
1313 wearableBundle.putCharSequence(KEY_CANCEL_LABEL, mCancelLabel);
1314 }
Griff Hazen61a9e862014-05-22 16:05:19 -07001315
1316 builder.getExtras().putBundle(EXTRA_WEARABLE_EXTENSIONS, wearableBundle);
1317 return builder;
1318 }
1319
1320 @Override
1321 public WearableExtender clone() {
1322 WearableExtender that = new WearableExtender();
1323 that.mFlags = this.mFlags;
Pete Gastaf6781d2014-10-07 15:17:05 -04001324 that.mInProgressLabel = this.mInProgressLabel;
1325 that.mConfirmLabel = this.mConfirmLabel;
1326 that.mCancelLabel = this.mCancelLabel;
Griff Hazen61a9e862014-05-22 16:05:19 -07001327 return that;
1328 }
1329
1330 /**
1331 * Set whether this action is available when the wearable device is not connected to
1332 * a companion device. The user can still trigger this action when the wearable device is
1333 * offline, but a visual hint will indicate that the action may not be available.
1334 * Defaults to true.
1335 */
1336 public WearableExtender setAvailableOffline(boolean availableOffline) {
1337 setFlag(FLAG_AVAILABLE_OFFLINE, availableOffline);
1338 return this;
1339 }
1340
1341 /**
1342 * Get whether this action is available when the wearable device is not connected to
1343 * a companion device. The user can still trigger this action when the wearable device is
1344 * offline, but a visual hint will indicate that the action may not be available.
1345 * Defaults to true.
1346 */
1347 public boolean isAvailableOffline() {
1348 return (mFlags & FLAG_AVAILABLE_OFFLINE) != 0;
1349 }
1350
1351 private void setFlag(int mask, boolean value) {
1352 if (value) {
1353 mFlags |= mask;
1354 } else {
1355 mFlags &= ~mask;
1356 }
1357 }
Pete Gastaf6781d2014-10-07 15:17:05 -04001358
1359 /**
1360 * Set a label to display while the wearable is preparing to automatically execute the
1361 * action. This is usually a 'ing' verb ending in ellipsis like "Sending..."
1362 *
1363 * @param label the label to display while the action is being prepared to execute
1364 * @return this object for method chaining
1365 */
1366 public WearableExtender setInProgressLabel(CharSequence label) {
1367 mInProgressLabel = label;
1368 return this;
1369 }
1370
1371 /**
1372 * Get the label to display while the wearable is preparing to automatically execute
1373 * the action. This is usually a 'ing' verb ending in ellipsis like "Sending..."
1374 *
1375 * @return the label to display while the action is being prepared to execute
1376 */
1377 public CharSequence getInProgressLabel() {
1378 return mInProgressLabel;
1379 }
1380
1381 /**
1382 * Set a label to display to confirm that the action should be executed.
1383 * This is usually an imperative verb like "Send".
1384 *
1385 * @param label the label to confirm the action should be executed
1386 * @return this object for method chaining
1387 */
1388 public WearableExtender setConfirmLabel(CharSequence label) {
1389 mConfirmLabel = label;
1390 return this;
1391 }
1392
1393 /**
1394 * Get the label to display to confirm that the action should be executed.
1395 * This is usually an imperative verb like "Send".
1396 *
1397 * @return the label to confirm the action should be executed
1398 */
1399 public CharSequence getConfirmLabel() {
1400 return mConfirmLabel;
1401 }
1402
1403 /**
1404 * Set a label to display to cancel the action.
1405 * This is usually an imperative verb, like "Cancel".
1406 *
1407 * @param label the label to display to cancel the action
1408 * @return this object for method chaining
1409 */
1410 public WearableExtender setCancelLabel(CharSequence label) {
1411 mCancelLabel = label;
1412 return this;
1413 }
1414
1415 /**
1416 * Get the label to display to cancel the action.
1417 * This is usually an imperative verb like "Cancel".
1418 *
1419 * @return the label to display to cancel the action
1420 */
1421 public CharSequence getCancelLabel() {
1422 return mCancelLabel;
1423 }
Alex Hills9ab3a232016-04-05 14:54:56 -04001424
1425 /**
1426 * Set a hint that this Action will launch an {@link Activity} directly, telling the
1427 * platform that it can generate the appropriate transitions.
1428 * @param hintLaunchesActivity {@code true} if the content intent will launch
1429 * an activity and transitions should be generated, false otherwise.
1430 * @return this object for method chaining
1431 */
Alex Hills4ec3ff42016-04-12 11:36:18 -04001432 public WearableExtender setHintLaunchesActivity(
Alex Hills9ab3a232016-04-05 14:54:56 -04001433 boolean hintLaunchesActivity) {
1434 setFlag(FLAG_HINT_LAUNCHES_ACTIVITY, hintLaunchesActivity);
1435 return this;
1436 }
1437
1438 /**
1439 * Get a hint that this Action will launch an {@link Activity} directly, telling the
1440 * platform that it can generate the appropriate transitions
1441 * @return {@code true} if the content intent will launch an activity and transitions
1442 * should be generated, false otherwise. The default value is {@code false} if this was
1443 * never set.
1444 */
Alex Hills4ec3ff42016-04-12 11:36:18 -04001445 public boolean getHintLaunchesActivity() {
Alex Hills9ab3a232016-04-05 14:54:56 -04001446 return (mFlags & FLAG_HINT_LAUNCHES_ACTIVITY) != 0;
1447 }
Griff Hazen61a9e862014-05-22 16:05:19 -07001448 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001449 }
1450
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001451 /**
1452 * Array of all {@link Action} structures attached to this notification by
1453 * {@link Builder#addAction(int, CharSequence, PendingIntent)}. Mostly useful for instances of
1454 * {@link android.service.notification.NotificationListenerService} that provide an alternative
1455 * interface for invoking actions.
1456 */
Daniel Sandlerea2a3172013-02-20 22:24:20 -05001457 public Action[] actions;
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001458
1459 /**
Dan Sandler0bf2ed82013-12-21 23:33:41 -06001460 * Replacement version of this notification whose content will be shown
1461 * in an insecure context such as atop a secure keyguard. See {@link #visibility}
1462 * and {@link #VISIBILITY_PUBLIC}.
1463 */
1464 public Notification publicVersion;
1465
1466 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001467 * Constructs a Notification object with default values.
Joe Onorato46439ce2010-11-19 13:56:21 -08001468 * You might want to consider using {@link Builder} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001469 */
1470 public Notification()
1471 {
1472 this.when = System.currentTimeMillis();
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001473 this.priority = PRIORITY_DEFAULT;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001474 }
1475
1476 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001477 * @hide
1478 */
1479 public Notification(Context context, int icon, CharSequence tickerText, long when,
1480 CharSequence contentTitle, CharSequence contentText, Intent contentIntent)
1481 {
Chris Wren1ce4b6d2015-06-11 10:19:43 -04001482 new Builder(context)
1483 .setWhen(when)
1484 .setSmallIcon(icon)
1485 .setTicker(tickerText)
1486 .setContentTitle(contentTitle)
1487 .setContentText(contentText)
1488 .setContentIntent(PendingIntent.getActivity(context, 0, contentIntent, 0))
1489 .buildInto(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001490 }
1491
1492 /**
1493 * Constructs a Notification object with the information needed to
1494 * have a status bar icon without the standard expanded view.
1495 *
1496 * @param icon The resource id of the icon to put in the status bar.
1497 * @param tickerText The text that flows by in the status bar when the notification first
1498 * activates.
1499 * @param when The time to show in the time field. In the System.currentTimeMillis
1500 * timebase.
Joe Onorato46439ce2010-11-19 13:56:21 -08001501 *
1502 * @deprecated Use {@link Builder} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001503 */
Joe Onorato46439ce2010-11-19 13:56:21 -08001504 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001505 public Notification(int icon, CharSequence tickerText, long when)
1506 {
1507 this.icon = icon;
1508 this.tickerText = tickerText;
1509 this.when = when;
1510 }
1511
1512 /**
1513 * Unflatten the notification from a parcel.
1514 */
1515 public Notification(Parcel parcel)
1516 {
1517 int version = parcel.readInt();
1518
1519 when = parcel.readLong();
Dan Sandler3936e7a2015-05-19 20:59:12 -04001520 if (parcel.readInt() != 0) {
1521 mSmallIcon = Icon.CREATOR.createFromParcel(parcel);
Dan Sandler4e787062015-06-17 15:09:48 -04001522 if (mSmallIcon.getType() == Icon.TYPE_RESOURCE) {
1523 icon = mSmallIcon.getResId();
1524 }
Dan Sandler3936e7a2015-05-19 20:59:12 -04001525 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001526 number = parcel.readInt();
1527 if (parcel.readInt() != 0) {
1528 contentIntent = PendingIntent.CREATOR.createFromParcel(parcel);
1529 }
1530 if (parcel.readInt() != 0) {
1531 deleteIntent = PendingIntent.CREATOR.createFromParcel(parcel);
1532 }
1533 if (parcel.readInt() != 0) {
1534 tickerText = TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(parcel);
1535 }
1536 if (parcel.readInt() != 0) {
Joe Onorato46439ce2010-11-19 13:56:21 -08001537 tickerView = RemoteViews.CREATOR.createFromParcel(parcel);
Joe Onoratoef1e7762010-09-17 18:38:38 -04001538 }
1539 if (parcel.readInt() != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001540 contentView = RemoteViews.CREATOR.createFromParcel(parcel);
1541 }
Joe Onorato561d3852010-11-20 18:09:34 -08001542 if (parcel.readInt() != 0) {
Dan Sandlerd63f9322015-05-06 15:18:49 -04001543 mLargeIcon = Icon.CREATOR.createFromParcel(parcel);
Joe Onorato561d3852010-11-20 18:09:34 -08001544 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001545 defaults = parcel.readInt();
1546 flags = parcel.readInt();
1547 if (parcel.readInt() != 0) {
1548 sound = Uri.CREATOR.createFromParcel(parcel);
1549 }
1550
1551 audioStreamType = parcel.readInt();
John Spurlockc0650f022014-07-19 13:22:39 -04001552 if (parcel.readInt() != 0) {
1553 audioAttributes = AudioAttributes.CREATOR.createFromParcel(parcel);
1554 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001555 vibrate = parcel.createLongArray();
1556 ledARGB = parcel.readInt();
1557 ledOnMS = parcel.readInt();
1558 ledOffMS = parcel.readInt();
1559 iconLevel = parcel.readInt();
Daniel Sandlere46cbd32010-06-17 10:35:26 -04001560
1561 if (parcel.readInt() != 0) {
1562 fullScreenIntent = PendingIntent.CREATOR.createFromParcel(parcel);
1563 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001564
1565 priority = parcel.readInt();
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001566
John Spurlockfd7f1e02014-03-18 16:41:57 -04001567 category = parcel.readString();
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001568
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001569 mGroupKey = parcel.readString();
1570
1571 mSortKey = parcel.readString();
1572
Jeff Sharkeya04c7a72016-03-18 12:20:36 -06001573 extras = Bundle.setDefusable(parcel.readBundle(), true); // may be null
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001574
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001575 actions = parcel.createTypedArray(Action.CREATOR); // may be null
1576
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001577 if (parcel.readInt() != 0) {
Daniel Sandlerf3b73432012-03-27 15:01:25 -04001578 bigContentView = RemoteViews.CREATOR.createFromParcel(parcel);
1579 }
Dan Sandler0bf2ed82013-12-21 23:33:41 -06001580
Chris Wren8fd39ec2014-02-27 17:43:26 -05001581 if (parcel.readInt() != 0) {
1582 headsUpContentView = RemoteViews.CREATOR.createFromParcel(parcel);
1583 }
1584
Dan Sandler0bf2ed82013-12-21 23:33:41 -06001585 visibility = parcel.readInt();
1586
1587 if (parcel.readInt() != 0) {
1588 publicVersion = Notification.CREATOR.createFromParcel(parcel);
1589 }
Dan Sandler26e81cf2014-05-06 10:01:27 -04001590
1591 color = parcel.readInt();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001592 }
1593
Andy Stadler110988c2010-12-03 14:29:16 -08001594 @Override
Joe Onorato18e69df2010-05-17 22:26:12 -07001595 public Notification clone() {
1596 Notification that = new Notification();
Daniel Sandler1a497d32013-04-18 14:52:45 -04001597 cloneInto(that, true);
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001598 return that;
1599 }
Joe Onorato18e69df2010-05-17 22:26:12 -07001600
Daniel Sandler1a497d32013-04-18 14:52:45 -04001601 /**
1602 * Copy all (or if heavy is false, all except Bitmaps and RemoteViews) members
1603 * of this into that.
1604 * @hide
1605 */
1606 public void cloneInto(Notification that, boolean heavy) {
Joe Onorato18e69df2010-05-17 22:26:12 -07001607 that.when = this.when;
Dan Sandlerd63f9322015-05-06 15:18:49 -04001608 that.mSmallIcon = this.mSmallIcon;
Joe Onorato18e69df2010-05-17 22:26:12 -07001609 that.number = this.number;
1610
1611 // PendingIntents are global, so there's no reason (or way) to clone them.
1612 that.contentIntent = this.contentIntent;
1613 that.deleteIntent = this.deleteIntent;
Daniel Sandlere46cbd32010-06-17 10:35:26 -04001614 that.fullScreenIntent = this.fullScreenIntent;
Joe Onorato18e69df2010-05-17 22:26:12 -07001615
1616 if (this.tickerText != null) {
1617 that.tickerText = this.tickerText.toString();
1618 }
Daniel Sandler1a497d32013-04-18 14:52:45 -04001619 if (heavy && this.tickerView != null) {
Joe Onorato46439ce2010-11-19 13:56:21 -08001620 that.tickerView = this.tickerView.clone();
Joe Onoratoef1e7762010-09-17 18:38:38 -04001621 }
Daniel Sandler1a497d32013-04-18 14:52:45 -04001622 if (heavy && this.contentView != null) {
Joe Onorato18e69df2010-05-17 22:26:12 -07001623 that.contentView = this.contentView.clone();
1624 }
Dan Sandlerd63f9322015-05-06 15:18:49 -04001625 if (heavy && this.mLargeIcon != null) {
1626 that.mLargeIcon = this.mLargeIcon;
Joe Onorato561d3852010-11-20 18:09:34 -08001627 }
Jozef BABJAKa8b91832011-02-22 08:05:08 +01001628 that.iconLevel = this.iconLevel;
Joe Onorato18e69df2010-05-17 22:26:12 -07001629 that.sound = this.sound; // android.net.Uri is immutable
1630 that.audioStreamType = this.audioStreamType;
John Spurlockc0650f022014-07-19 13:22:39 -04001631 if (this.audioAttributes != null) {
1632 that.audioAttributes = new AudioAttributes.Builder(this.audioAttributes).build();
1633 }
Joe Onorato18e69df2010-05-17 22:26:12 -07001634
1635 final long[] vibrate = this.vibrate;
1636 if (vibrate != null) {
1637 final int N = vibrate.length;
1638 final long[] vib = that.vibrate = new long[N];
1639 System.arraycopy(vibrate, 0, vib, 0, N);
1640 }
1641
1642 that.ledARGB = this.ledARGB;
1643 that.ledOnMS = this.ledOnMS;
1644 that.ledOffMS = this.ledOffMS;
1645 that.defaults = this.defaults;
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001646
Joe Onorato18e69df2010-05-17 22:26:12 -07001647 that.flags = this.flags;
1648
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001649 that.priority = this.priority;
Joe Malin8d40d042012-11-05 11:36:40 -08001650
John Spurlockfd7f1e02014-03-18 16:41:57 -04001651 that.category = this.category;
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001652
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001653 that.mGroupKey = this.mGroupKey;
1654
1655 that.mSortKey = this.mSortKey;
1656
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001657 if (this.extras != null) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04001658 try {
1659 that.extras = new Bundle(this.extras);
1660 // will unparcel
1661 that.extras.size();
1662 } catch (BadParcelableException e) {
1663 Log.e(TAG, "could not unparcel extras from notification: " + this, e);
1664 that.extras = null;
1665 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001666 }
1667
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001668 if (this.actions != null) {
1669 that.actions = new Action[this.actions.length];
1670 for(int i=0; i<this.actions.length; i++) {
1671 that.actions[i] = this.actions[i].clone();
1672 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001673 }
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001674
Daniel Sandler1a497d32013-04-18 14:52:45 -04001675 if (heavy && this.bigContentView != null) {
Daniel Sandlerf3b73432012-03-27 15:01:25 -04001676 that.bigContentView = this.bigContentView.clone();
1677 }
Daniel Sandler1a497d32013-04-18 14:52:45 -04001678
Chris Wren8fd39ec2014-02-27 17:43:26 -05001679 if (heavy && this.headsUpContentView != null) {
1680 that.headsUpContentView = this.headsUpContentView.clone();
1681 }
1682
Dan Sandler0bf2ed82013-12-21 23:33:41 -06001683 that.visibility = this.visibility;
1684
1685 if (this.publicVersion != null) {
1686 that.publicVersion = new Notification();
1687 this.publicVersion.cloneInto(that.publicVersion, heavy);
1688 }
1689
Dan Sandler26e81cf2014-05-06 10:01:27 -04001690 that.color = this.color;
1691
Daniel Sandler1a497d32013-04-18 14:52:45 -04001692 if (!heavy) {
1693 that.lightenPayload(); // will clean out extras
1694 }
1695 }
1696
1697 /**
1698 * Removes heavyweight parts of the Notification object for archival or for sending to
1699 * listeners when the full contents are not necessary.
1700 * @hide
1701 */
1702 public final void lightenPayload() {
1703 tickerView = null;
1704 contentView = null;
1705 bigContentView = null;
Chris Wren8fd39ec2014-02-27 17:43:26 -05001706 headsUpContentView = null;
Dan Sandlerd63f9322015-05-06 15:18:49 -04001707 mLargeIcon = null;
Dan Sandler50128532015-12-08 15:42:41 -05001708 if (extras != null && !extras.isEmpty()) {
1709 final Set<String> keyset = extras.keySet();
1710 final int N = keyset.size();
1711 final String[] keys = keyset.toArray(new String[N]);
1712 for (int i=0; i<N; i++) {
1713 final String key = keys[i];
1714 final Object obj = extras.get(key);
1715 if (obj != null &&
1716 ( obj instanceof Parcelable
1717 || obj instanceof Parcelable[]
1718 || obj instanceof SparseArray
1719 || obj instanceof ArrayList)) {
1720 extras.remove(key);
1721 }
1722 }
Daniel Sandler1a497d32013-04-18 14:52:45 -04001723 }
Joe Onorato18e69df2010-05-17 22:26:12 -07001724 }
1725
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04001726 /**
1727 * Make sure this CharSequence is safe to put into a bundle, which basically
1728 * means it had better not be some custom Parcelable implementation.
1729 * @hide
1730 */
1731 public static CharSequence safeCharSequence(CharSequence cs) {
Christoph Studer535ec612014-09-03 15:47:47 +02001732 if (cs == null) return cs;
1733 if (cs.length() > MAX_CHARSEQUENCE_LENGTH) {
1734 cs = cs.subSequence(0, MAX_CHARSEQUENCE_LENGTH);
1735 }
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04001736 if (cs instanceof Parcelable) {
1737 Log.e(TAG, "warning: " + cs.getClass().getCanonicalName()
1738 + " instance is a custom Parcelable and not allowed in Notification");
1739 return cs.toString();
1740 }
Selim Cinek60a54252016-02-26 17:03:25 -08001741 return removeTextSizeSpans(cs);
1742 }
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04001743
Selim Cinek60a54252016-02-26 17:03:25 -08001744 private static CharSequence removeTextSizeSpans(CharSequence charSequence) {
1745 if (charSequence instanceof Spanned) {
1746 Spanned ss = (Spanned) charSequence;
1747 Object[] spans = ss.getSpans(0, ss.length(), Object.class);
1748 SpannableStringBuilder builder = new SpannableStringBuilder(ss.toString());
1749 for (Object span : spans) {
1750 Object resultSpan = span;
Selim Cinek89991a22016-03-07 19:51:54 -08001751 if (resultSpan instanceof CharacterStyle) {
1752 resultSpan = ((CharacterStyle) span).getUnderlying();
1753 }
1754 if (resultSpan instanceof TextAppearanceSpan) {
1755 TextAppearanceSpan originalSpan = (TextAppearanceSpan) resultSpan;
Selim Cinek60a54252016-02-26 17:03:25 -08001756 resultSpan = new TextAppearanceSpan(
1757 originalSpan.getFamily(),
1758 originalSpan.getTextStyle(),
1759 -1,
1760 originalSpan.getTextColor(),
1761 originalSpan.getLinkTextColor());
Selim Cinek89991a22016-03-07 19:51:54 -08001762 } else if (resultSpan instanceof RelativeSizeSpan
1763 || resultSpan instanceof AbsoluteSizeSpan) {
Selim Cinek60a54252016-02-26 17:03:25 -08001764 continue;
Selim Cinek89991a22016-03-07 19:51:54 -08001765 } else {
1766 resultSpan = span;
Selim Cinek60a54252016-02-26 17:03:25 -08001767 }
1768 builder.setSpan(resultSpan, ss.getSpanStart(span), ss.getSpanEnd(span),
1769 ss.getSpanFlags(span));
1770 }
1771 return builder;
1772 }
1773 return charSequence;
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04001774 }
1775
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001776 public int describeContents() {
1777 return 0;
1778 }
1779
1780 /**
Dan Sandler4e787062015-06-17 15:09:48 -04001781 * Flatten this notification into a parcel.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001782 */
1783 public void writeToParcel(Parcel parcel, int flags)
1784 {
1785 parcel.writeInt(1);
1786
1787 parcel.writeLong(when);
Dan Sandler4e787062015-06-17 15:09:48 -04001788 if (mSmallIcon == null && icon != 0) {
1789 // you snuck an icon in here without using the builder; let's try to keep it
1790 mSmallIcon = Icon.createWithResource("", icon);
1791 }
Dan Sandler3936e7a2015-05-19 20:59:12 -04001792 if (mSmallIcon != null) {
1793 parcel.writeInt(1);
1794 mSmallIcon.writeToParcel(parcel, 0);
1795 } else {
1796 parcel.writeInt(0);
1797 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001798 parcel.writeInt(number);
1799 if (contentIntent != null) {
1800 parcel.writeInt(1);
1801 contentIntent.writeToParcel(parcel, 0);
1802 } else {
1803 parcel.writeInt(0);
1804 }
1805 if (deleteIntent != null) {
1806 parcel.writeInt(1);
1807 deleteIntent.writeToParcel(parcel, 0);
1808 } else {
1809 parcel.writeInt(0);
1810 }
1811 if (tickerText != null) {
1812 parcel.writeInt(1);
1813 TextUtils.writeToParcel(tickerText, parcel, flags);
1814 } else {
1815 parcel.writeInt(0);
1816 }
Joe Onorato46439ce2010-11-19 13:56:21 -08001817 if (tickerView != null) {
Joe Onoratoef1e7762010-09-17 18:38:38 -04001818 parcel.writeInt(1);
Joe Onorato46439ce2010-11-19 13:56:21 -08001819 tickerView.writeToParcel(parcel, 0);
Joe Onoratoef1e7762010-09-17 18:38:38 -04001820 } else {
1821 parcel.writeInt(0);
1822 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001823 if (contentView != null) {
1824 parcel.writeInt(1);
1825 contentView.writeToParcel(parcel, 0);
1826 } else {
1827 parcel.writeInt(0);
1828 }
Dan Sandlerd63f9322015-05-06 15:18:49 -04001829 if (mLargeIcon != null) {
Joe Onorato561d3852010-11-20 18:09:34 -08001830 parcel.writeInt(1);
Dan Sandlerd63f9322015-05-06 15:18:49 -04001831 mLargeIcon.writeToParcel(parcel, 0);
Joe Onorato561d3852010-11-20 18:09:34 -08001832 } else {
1833 parcel.writeInt(0);
1834 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001835
1836 parcel.writeInt(defaults);
1837 parcel.writeInt(this.flags);
1838
1839 if (sound != null) {
1840 parcel.writeInt(1);
1841 sound.writeToParcel(parcel, 0);
1842 } else {
1843 parcel.writeInt(0);
1844 }
1845 parcel.writeInt(audioStreamType);
John Spurlockc0650f022014-07-19 13:22:39 -04001846
1847 if (audioAttributes != null) {
1848 parcel.writeInt(1);
1849 audioAttributes.writeToParcel(parcel, 0);
1850 } else {
1851 parcel.writeInt(0);
1852 }
1853
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001854 parcel.writeLongArray(vibrate);
1855 parcel.writeInt(ledARGB);
1856 parcel.writeInt(ledOnMS);
1857 parcel.writeInt(ledOffMS);
1858 parcel.writeInt(iconLevel);
Daniel Sandlere46cbd32010-06-17 10:35:26 -04001859
1860 if (fullScreenIntent != null) {
1861 parcel.writeInt(1);
1862 fullScreenIntent.writeToParcel(parcel, 0);
1863 } else {
1864 parcel.writeInt(0);
1865 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001866
1867 parcel.writeInt(priority);
Joe Malin8d40d042012-11-05 11:36:40 -08001868
John Spurlockfd7f1e02014-03-18 16:41:57 -04001869 parcel.writeString(category);
Joe Malin8d40d042012-11-05 11:36:40 -08001870
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001871 parcel.writeString(mGroupKey);
1872
1873 parcel.writeString(mSortKey);
1874
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001875 parcel.writeBundle(extras); // null ok
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001876
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001877 parcel.writeTypedArray(actions, 0); // null ok
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001878
Daniel Sandlerf3b73432012-03-27 15:01:25 -04001879 if (bigContentView != null) {
1880 parcel.writeInt(1);
1881 bigContentView.writeToParcel(parcel, 0);
1882 } else {
1883 parcel.writeInt(0);
1884 }
Dan Sandler0bf2ed82013-12-21 23:33:41 -06001885
Chris Wren8fd39ec2014-02-27 17:43:26 -05001886 if (headsUpContentView != null) {
1887 parcel.writeInt(1);
1888 headsUpContentView.writeToParcel(parcel, 0);
1889 } else {
1890 parcel.writeInt(0);
1891 }
1892
Dan Sandler0bf2ed82013-12-21 23:33:41 -06001893 parcel.writeInt(visibility);
1894
1895 if (publicVersion != null) {
1896 parcel.writeInt(1);
1897 publicVersion.writeToParcel(parcel, 0);
1898 } else {
1899 parcel.writeInt(0);
1900 }
Dan Sandler26e81cf2014-05-06 10:01:27 -04001901
1902 parcel.writeInt(color);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001903 }
1904
1905 /**
1906 * Parcelable.Creator that instantiates Notification objects
1907 */
1908 public static final Parcelable.Creator<Notification> CREATOR
1909 = new Parcelable.Creator<Notification>()
1910 {
1911 public Notification createFromParcel(Parcel parcel)
1912 {
1913 return new Notification(parcel);
1914 }
1915
1916 public Notification[] newArray(int size)
1917 {
1918 return new Notification[size];
1919 }
1920 };
1921
1922 /**
1923 * Sets the {@link #contentView} field to be a view with the standard "Latest Event"
1924 * layout.
1925 *
1926 * <p>Uses the {@link #icon} and {@link #when} fields to set the icon and time fields
1927 * in the view.</p>
1928 * @param context The context for your application / activity.
1929 * @param contentTitle The title that goes in the expanded entry.
1930 * @param contentText The text that goes in the expanded entry.
1931 * @param contentIntent The intent to launch when the user clicks the expanded notification.
1932 * If this is an activity, it must include the
1933 * {@link android.content.Intent#FLAG_ACTIVITY_NEW_TASK} flag, which requires
Scott Main7aee61f2011-02-08 11:25:01 -08001934 * that you take care of task management as described in the
1935 * <a href="{@docRoot}guide/topics/fundamentals/tasks-and-back-stack.html">Tasks and Back
1936 * Stack</a> document.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001937 *
Joe Onorato46439ce2010-11-19 13:56:21 -08001938 * @deprecated Use {@link Builder} instead.
Chris Wrena05db382015-06-24 15:18:34 -04001939 * @removed
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001940 */
Joe Onorato46439ce2010-11-19 13:56:21 -08001941 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001942 public void setLatestEventInfo(Context context,
1943 CharSequence contentTitle, CharSequence contentText, PendingIntent contentIntent) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04001944 if (context.getApplicationInfo().targetSdkVersion > Build.VERSION_CODES.LOLLIPOP_MR1){
1945 Log.e(TAG, "setLatestEventInfo() is deprecated and you should feel deprecated.",
1946 new Throwable());
1947 }
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001948
Julia Reynoldsd9228f12015-10-20 10:37:27 -04001949 // ensure that any information already set directly is preserved
1950 final Notification.Builder builder = new Notification.Builder(context, this);
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001951
1952 // now apply the latestEventInfo fields
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001953 if (contentTitle != null) {
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001954 builder.setContentTitle(contentTitle);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001955 }
1956 if (contentText != null) {
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001957 builder.setContentText(contentText);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001958 }
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001959 builder.setContentIntent(contentIntent);
Julia Reynoldsd9228f12015-10-20 10:37:27 -04001960
1961 builder.build(); // callers expect this notification to be ready to use
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001962 }
1963
Julia Reynoldsda303542015-11-23 14:00:20 -05001964 /**
1965 * @hide
1966 */
1967 public static void addFieldsFromContext(Context context, Notification notification) {
Jeff Sharkey012bc7b2016-04-11 16:30:27 -06001968 addFieldsFromContext(context.getApplicationInfo(), context.getUserId(), notification);
1969 }
1970
1971 /**
1972 * @hide
1973 */
1974 public static void addFieldsFromContext(ApplicationInfo ai, int userId,
1975 Notification notification) {
1976 notification.extras.putParcelable(EXTRA_BUILDER_APPLICATION_INFO, ai);
1977 notification.extras.putInt(EXTRA_ORIGINATING_USERID, userId);
Julia Reynoldsda303542015-11-23 14:00:20 -05001978 }
1979
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001980 @Override
1981 public String toString() {
1982 StringBuilder sb = new StringBuilder();
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001983 sb.append("Notification(pri=");
1984 sb.append(priority);
1985 sb.append(" contentView=");
Joe Onoratoc9596d62011-01-12 17:03:11 -08001986 if (contentView != null) {
1987 sb.append(contentView.getPackage());
1988 sb.append("/0x");
1989 sb.append(Integer.toHexString(contentView.getLayoutId()));
1990 } else {
1991 sb.append("null");
1992 }
1993 sb.append(" vibrate=");
Daniel Sandler6738eee2012-11-16 12:03:32 -05001994 if ((this.defaults & DEFAULT_VIBRATE) != 0) {
1995 sb.append("default");
1996 } else if (this.vibrate != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001997 int N = this.vibrate.length-1;
1998 sb.append("[");
1999 for (int i=0; i<N; i++) {
2000 sb.append(this.vibrate[i]);
2001 sb.append(',');
2002 }
Simon Schoar8cf97d92009-06-10 22:08:37 +02002003 if (N != -1) {
2004 sb.append(this.vibrate[N]);
2005 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002006 sb.append("]");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002007 } else {
2008 sb.append("null");
2009 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002010 sb.append(" sound=");
Daniel Sandler6738eee2012-11-16 12:03:32 -05002011 if ((this.defaults & DEFAULT_SOUND) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002012 sb.append("default");
Daniel Sandler6738eee2012-11-16 12:03:32 -05002013 } else if (this.sound != null) {
2014 sb.append(this.sound.toString());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002015 } else {
2016 sb.append("null");
2017 }
Chris Wren365b6d32015-07-16 10:39:26 -04002018 if (this.tickerText != null) {
2019 sb.append(" tick");
2020 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002021 sb.append(" defaults=0x");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002022 sb.append(Integer.toHexString(this.defaults));
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002023 sb.append(" flags=0x");
Daniel Sandlere46cbd32010-06-17 10:35:26 -04002024 sb.append(Integer.toHexString(this.flags));
Dan Sandler26e81cf2014-05-06 10:01:27 -04002025 sb.append(String.format(" color=0x%08x", this.color));
Griff Hazen5cadc3b2014-05-20 09:55:39 -07002026 if (this.category != null) {
2027 sb.append(" category=");
2028 sb.append(this.category);
2029 }
2030 if (this.mGroupKey != null) {
2031 sb.append(" groupKey=");
2032 sb.append(this.mGroupKey);
2033 }
2034 if (this.mSortKey != null) {
2035 sb.append(" sortKey=");
2036 sb.append(this.mSortKey);
2037 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002038 if (actions != null) {
Dan Sandler1b718782014-07-18 12:43:45 -04002039 sb.append(" actions=");
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002040 sb.append(actions.length);
Dan Sandler1b718782014-07-18 12:43:45 -04002041 }
2042 sb.append(" vis=");
2043 sb.append(visibilityToString(this.visibility));
2044 if (this.publicVersion != null) {
2045 sb.append(" publicVersion=");
2046 sb.append(publicVersion.toString());
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002047 }
2048 sb.append(")");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002049 return sb.toString();
2050 }
Joe Onorato46439ce2010-11-19 13:56:21 -08002051
Dan Sandler1b718782014-07-18 12:43:45 -04002052 /**
2053 * {@hide}
2054 */
2055 public static String visibilityToString(int vis) {
2056 switch (vis) {
2057 case VISIBILITY_PRIVATE:
2058 return "PRIVATE";
2059 case VISIBILITY_PUBLIC:
2060 return "PUBLIC";
2061 case VISIBILITY_SECRET:
2062 return "SECRET";
2063 default:
2064 return "UNKNOWN(" + String.valueOf(vis) + ")";
2065 }
2066 }
2067
Joe Onoratocb109a02011-01-18 17:57:41 -08002068 /**
John Spurlock1d881a12015-03-18 19:21:54 -04002069 * {@hide}
2070 */
2071 public static String priorityToString(@Priority int pri) {
2072 switch (pri) {
2073 case PRIORITY_MIN:
2074 return "MIN";
2075 case PRIORITY_LOW:
2076 return "LOW";
2077 case PRIORITY_DEFAULT:
2078 return "DEFAULT";
2079 case PRIORITY_HIGH:
2080 return "HIGH";
2081 case PRIORITY_MAX:
2082 return "MAX";
2083 default:
2084 return "UNKNOWN(" + String.valueOf(pri) + ")";
2085 }
2086 }
2087
2088 /**
Dan Sandlerd63f9322015-05-06 15:18:49 -04002089 * The small icon representing this notification in the status bar and content view.
2090 *
2091 * @return the small icon representing this notification.
2092 *
2093 * @see Builder#getSmallIcon()
2094 * @see Builder#setSmallIcon(Icon)
2095 */
2096 public Icon getSmallIcon() {
2097 return mSmallIcon;
2098 }
2099
2100 /**
2101 * Used when notifying to clean up legacy small icons.
2102 * @hide
2103 */
2104 public void setSmallIcon(Icon icon) {
2105 mSmallIcon = icon;
2106 }
2107
2108 /**
2109 * The large icon shown in this notification's content view.
2110 * @see Builder#getLargeIcon()
2111 * @see Builder#setLargeIcon(Icon)
2112 */
2113 public Icon getLargeIcon() {
2114 return mLargeIcon;
2115 }
2116
2117 /**
Christoph Studer4600f9b2014-07-22 22:44:43 +02002118 * @hide
2119 */
Christoph Studerc8db24b2014-07-25 17:50:30 +02002120 public boolean isGroupSummary() {
2121 return mGroupKey != null && (flags & FLAG_GROUP_SUMMARY) != 0;
2122 }
2123
2124 /**
2125 * @hide
2126 */
2127 public boolean isGroupChild() {
2128 return mGroupKey != null && (flags & FLAG_GROUP_SUMMARY) == 0;
2129 }
2130
2131 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002132 * Builder class for {@link Notification} objects.
Joe Malin8d40d042012-11-05 11:36:40 -08002133 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002134 * Provides a convenient way to set the various fields of a {@link Notification} and generate
Scott Main183bf112012-08-13 19:12:13 -07002135 * content views using the platform's notification layout template. If your app supports
2136 * versions of Android as old as API level 4, you can instead use
2137 * {@link android.support.v4.app.NotificationCompat.Builder NotificationCompat.Builder},
2138 * available in the <a href="{@docRoot}tools/extras/support-library.html">Android Support
2139 * library</a>.
Joe Malin8d40d042012-11-05 11:36:40 -08002140 *
Scott Main183bf112012-08-13 19:12:13 -07002141 * <p>Example:
Joe Malin8d40d042012-11-05 11:36:40 -08002142 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002143 * <pre class="prettyprint">
Scott Main183bf112012-08-13 19:12:13 -07002144 * Notification noti = new Notification.Builder(mContext)
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002145 * .setContentTitle(&quot;New mail from &quot; + sender.toString())
2146 * .setContentText(subject)
2147 * .setSmallIcon(R.drawable.new_mail)
2148 * .setLargeIcon(aBitmap)
Chris Wrenfbd96ba2012-05-01 12:03:58 -04002149 * .build();
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002150 * </pre>
Joe Onoratocb109a02011-01-18 17:57:41 -08002151 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002152 public static class Builder {
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05002153 /**
2154 * @hide
2155 */
2156 public static final String EXTRA_REBUILD_CONTENT_VIEW_ACTION_COUNT =
2157 "android.rebuild.contentViewActionCount";
2158 /**
2159 * @hide
2160 */
2161 public static final String EXTRA_REBUILD_BIG_CONTENT_VIEW_ACTION_COUNT
2162 = "android.rebuild.bigViewActionCount";
2163 /**
2164 * @hide
2165 */
2166 public static final String EXTRA_REBUILD_HEADS_UP_CONTENT_VIEW_ACTION_COUNT
2167 = "android.rebuild.hudViewActionCount";
2168
Daniel Sandler602ad1c2012-06-12 16:06:27 -04002169 private static final int MAX_ACTION_BUTTONS = 3;
Daniel Sandler8680bf82012-05-15 16:52:52 -04002170
Joe Onorato46439ce2010-11-19 13:56:21 -08002171 private Context mContext;
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002172 private Notification mN;
2173 private Bundle mUserExtras = new Bundle();
Chris Wrenfbd96ba2012-05-01 12:03:58 -04002174 private Style mStyle;
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002175 private ArrayList<Action> mActions = new ArrayList<Action>(MAX_ACTION_BUTTONS);
2176 private ArrayList<String> mPersonList = new ArrayList<String>();
2177 private NotificationColorUtil mColorUtil;
2178 private boolean mColorUtilInited = false;
Christoph Studer7ac80e62014-08-04 16:01:57 +02002179
2180 /**
Adrian Roos4ff3b122016-02-01 12:26:13 -08002181 * Caches a contrast-enhanced version of {@link #mCachedContrastColorIsFor}.
2182 */
2183 private int mCachedContrastColor = COLOR_INVALID;
2184 private int mCachedContrastColorIsFor = COLOR_INVALID;
2185
2186 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002187 * Constructs a new Builder with the defaults:
Joe Onoratocb109a02011-01-18 17:57:41 -08002188 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002189
2190 * <table>
2191 * <tr><th align=right>priority</th>
2192 * <td>{@link #PRIORITY_DEFAULT}</td></tr>
2193 * <tr><th align=right>when</th>
2194 * <td>now ({@link System#currentTimeMillis()})</td></tr>
2195 * <tr><th align=right>audio stream</th>
2196 * <td>{@link #STREAM_DEFAULT}</td></tr>
2197 * </table>
Joe Onoratocb109a02011-01-18 17:57:41 -08002198 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002199
2200 * @param context
2201 * A {@link Context} that will be used by the Builder to construct the
2202 * RemoteViews. The Context will not be held past the lifetime of this Builder
2203 * object.
Joe Onoratocb109a02011-01-18 17:57:41 -08002204 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002205 public Builder(Context context) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002206 this(context, null);
Joe Onorato46439ce2010-11-19 13:56:21 -08002207 }
2208
Joe Onoratocb109a02011-01-18 17:57:41 -08002209 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002210 * @hide
Christoph Studer4600f9b2014-07-22 22:44:43 +02002211 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002212 public Builder(Context context, Notification toAdopt) {
2213 mContext = context;
Christoph Studer4600f9b2014-07-22 22:44:43 +02002214
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002215 if (toAdopt == null) {
2216 mN = new Notification();
Selim Cinek0ff1ce602016-04-05 18:27:16 -07002217 if (context.getApplicationInfo().targetSdkVersion < Build.VERSION_CODES.N) {
2218 mN.extras.putBoolean(EXTRA_SHOW_WHEN, true);
2219 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002220 mN.priority = PRIORITY_DEFAULT;
2221 mN.visibility = VISIBILITY_PRIVATE;
2222 } else {
2223 mN = toAdopt;
2224 if (mN.actions != null) {
2225 Collections.addAll(mActions, mN.actions);
Christoph Studer4600f9b2014-07-22 22:44:43 +02002226 }
2227
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002228 if (mN.extras.containsKey(EXTRA_PEOPLE)) {
2229 Collections.addAll(mPersonList, mN.extras.getStringArray(EXTRA_PEOPLE));
2230 }
2231
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002232 String templateClass = mN.extras.getString(EXTRA_TEMPLATE);
2233 if (!TextUtils.isEmpty(templateClass)) {
2234 final Class<? extends Style> styleClass
2235 = getNotificationStyleClass(templateClass);
2236 if (styleClass == null) {
2237 Log.d(TAG, "Unknown style class: " + templateClass);
2238 } else {
2239 try {
2240 final Constructor<? extends Style> ctor = styleClass.getConstructor();
2241 ctor.setAccessible(true);
2242 final Style style = ctor.newInstance();
2243 style.restoreFromExtras(mN.extras);
2244
2245 if (style != null) {
2246 setStyle(style);
2247 }
2248 } catch (Throwable t) {
2249 Log.e(TAG, "Could not create Style", t);
2250 }
2251 }
2252 }
2253
2254 }
2255 }
2256
2257 private NotificationColorUtil getColorUtil() {
2258 if (!mColorUtilInited) {
2259 mColorUtilInited = true;
2260 if (mContext.getApplicationInfo().targetSdkVersion < Build.VERSION_CODES.LOLLIPOP) {
2261 mColorUtil = NotificationColorUtil.getInstance(mContext);
Christoph Studer4600f9b2014-07-22 22:44:43 +02002262 }
2263 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002264 return mColorUtil;
Christoph Studer4600f9b2014-07-22 22:44:43 +02002265 }
2266
2267 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002268 * Add a timestamp pertaining to the notification (usually the time the event occurred).
Selim Cinek0ff1ce602016-04-05 18:27:16 -07002269 *
2270 * For apps targeting {@link android.os.Build.VERSION_CODES#N} and above, this time is not
2271 * shown anymore by default and must be opted into by using
2272 * {@link android.app.Notification.Builder#setShowWhen(boolean)}
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002273 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002274 * @see Notification#when
Joe Onoratocb109a02011-01-18 17:57:41 -08002275 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002276 public Builder setWhen(long when) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002277 mN.when = when;
Joe Onorato46439ce2010-11-19 13:56:21 -08002278 return this;
2279 }
2280
Joe Onoratocb109a02011-01-18 17:57:41 -08002281 /**
Griff Hazen50c11652014-05-16 09:46:31 -07002282 * Control whether the timestamp set with {@link #setWhen(long) setWhen} is shown
Daniel Sandler0c890492012-09-12 17:23:10 -07002283 * in the content view.
Selim Cinek0ff1ce602016-04-05 18:27:16 -07002284 * For apps targeting {@link android.os.Build.VERSION_CODES#N} and above, this defaults to
2285 * {@code false}. For earlier apps, the default is {@code true}.
Daniel Sandler0c890492012-09-12 17:23:10 -07002286 */
2287 public Builder setShowWhen(boolean show) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002288 mN.extras.putBoolean(EXTRA_SHOW_WHEN, show);
Daniel Sandler0c890492012-09-12 17:23:10 -07002289 return this;
2290 }
2291
2292 /**
Daniel Sandlerd33b8032012-05-10 11:41:48 -04002293 * Show the {@link Notification#when} field as a stopwatch.
Joe Malin8d40d042012-11-05 11:36:40 -08002294 *
2295 * Instead of presenting <code>when</code> as a timestamp, the notification will show an
Daniel Sandlerd33b8032012-05-10 11:41:48 -04002296 * automatically updating display of the minutes and seconds since <code>when</code>.
Daniel Sandlera2985ed2012-04-03 16:42:00 -04002297 *
Daniel Sandlerd33b8032012-05-10 11:41:48 -04002298 * Useful when showing an elapsed time (like an ongoing phone call).
2299 *
Selim Cinek81c23aa2016-02-25 16:23:13 -08002300 * The counter can also be set to count down to <code>when</code> when using
2301 * {@link #setChronometerCountsDown(boolean)}.
2302 *
Daniel Sandlerd33b8032012-05-10 11:41:48 -04002303 * @see android.widget.Chronometer
Daniel Sandlera2985ed2012-04-03 16:42:00 -04002304 * @see Notification#when
Selim Cinek81c23aa2016-02-25 16:23:13 -08002305 * @see #setChronometerCountsDown(boolean)
Daniel Sandlera2985ed2012-04-03 16:42:00 -04002306 */
2307 public Builder setUsesChronometer(boolean b) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002308 mN.extras.putBoolean(EXTRA_SHOW_CHRONOMETER, b);
Daniel Sandlera2985ed2012-04-03 16:42:00 -04002309 return this;
2310 }
2311
2312 /**
Selim Cinek81c23aa2016-02-25 16:23:13 -08002313 * Sets the Chronometer to count down instead of counting up.
2314 *
2315 * <p>This is only relevant if {@link #setUsesChronometer(boolean)} has been set to true.
2316 * If it isn't set the chronometer will count up.
2317 *
2318 * @see #setUsesChronometer(boolean)
2319 */
2320 public Builder setChronometerCountsDown(boolean countsDown) {
2321 mN.extras.putBoolean(EXTRA_CHRONOMETER_COUNTS_DOWN, countsDown);
2322 return this;
2323 }
2324
2325 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002326 * Set the small icon resource, which will be used to represent the notification in the
2327 * status bar.
Joe Onoratocb109a02011-01-18 17:57:41 -08002328 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002329
2330 * The platform template for the expanded view will draw this icon in the left, unless a
2331 * {@link #setLargeIcon(Bitmap) large icon} has also been specified, in which case the small
2332 * icon will be moved to the right-hand side.
2333 *
2334
2335 * @param icon
2336 * A resource ID in the application's package of the drawable to use.
2337 * @see Notification#icon
Joe Onoratocb109a02011-01-18 17:57:41 -08002338 */
Tor Norbye7b9c9122013-05-30 16:48:33 -07002339 public Builder setSmallIcon(@DrawableRes int icon) {
Dan Sandlerd63f9322015-05-06 15:18:49 -04002340 return setSmallIcon(icon != 0
2341 ? Icon.createWithResource(mContext, icon)
2342 : null);
Joe Onorato46439ce2010-11-19 13:56:21 -08002343 }
2344
Joe Onoratocb109a02011-01-18 17:57:41 -08002345 /**
2346 * A variant of {@link #setSmallIcon(int) setSmallIcon(int)} that takes an additional
2347 * level parameter for when the icon is a {@link android.graphics.drawable.LevelListDrawable
2348 * LevelListDrawable}.
2349 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002350 * @param icon A resource ID in the application's package of the drawable to use.
Joe Onoratocb109a02011-01-18 17:57:41 -08002351 * @param level The level to use for the icon.
2352 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002353 * @see Notification#icon
2354 * @see Notification#iconLevel
Joe Onoratocb109a02011-01-18 17:57:41 -08002355 */
Tor Norbye7b9c9122013-05-30 16:48:33 -07002356 public Builder setSmallIcon(@DrawableRes int icon, int level) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002357 mN.iconLevel = level;
Dan Sandlerd63f9322015-05-06 15:18:49 -04002358 return setSmallIcon(icon);
2359 }
2360
2361 /**
2362 * Set the small icon, which will be used to represent the notification in the
2363 * status bar and content view (unless overriden there by a
2364 * {@link #setLargeIcon(Bitmap) large icon}).
2365 *
2366 * @param icon An Icon object to use.
2367 * @see Notification#icon
2368 */
2369 public Builder setSmallIcon(Icon icon) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002370 mN.setSmallIcon(icon);
2371 if (icon != null && icon.getType() == Icon.TYPE_RESOURCE) {
2372 mN.icon = icon.getResId();
2373 }
Joe Onorato46439ce2010-11-19 13:56:21 -08002374 return this;
2375 }
2376
Joe Onoratocb109a02011-01-18 17:57:41 -08002377 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002378 * Set the first line of text in the platform notification template.
Joe Onoratocb109a02011-01-18 17:57:41 -08002379 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002380 public Builder setContentTitle(CharSequence title) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002381 mN.extras.putCharSequence(EXTRA_TITLE, safeCharSequence(title));
Joe Onorato46439ce2010-11-19 13:56:21 -08002382 return this;
2383 }
2384
Joe Onoratocb109a02011-01-18 17:57:41 -08002385 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002386 * Set the second line of text in the platform notification template.
Joe Onoratocb109a02011-01-18 17:57:41 -08002387 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002388 public Builder setContentText(CharSequence text) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002389 mN.extras.putCharSequence(EXTRA_TEXT, safeCharSequence(text));
Joe Onorato46439ce2010-11-19 13:56:21 -08002390 return this;
2391 }
2392
Joe Onoratocb109a02011-01-18 17:57:41 -08002393 /**
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07002394 * This provides some additional information that is displayed in the notification. No
2395 * guarantees are given where exactly it is displayed.
2396 *
2397 * <p>This information should only be provided if it provides an essential
2398 * benefit to the understanding of the notification. The more text you provide the
2399 * less readable it becomes. For example, an email client should only provide the account
2400 * name here if more than one email account has been added.</p>
2401 *
2402 * <p>As of {@link android.os.Build.VERSION_CODES#N} this information is displayed in the
2403 * notification header area.
2404 *
2405 * On Android versions before {@link android.os.Build.VERSION_CODES#N}
2406 * this will be shown in the third line of text in the platform notification template.
2407 * You should not be using {@link #setProgress(int, int, boolean)} at the
2408 * same time on those versions; they occupy the same place.
2409 * </p>
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002410 */
2411 public Builder setSubText(CharSequence text) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002412 mN.extras.putCharSequence(EXTRA_SUB_TEXT, safeCharSequence(text));
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002413 return this;
2414 }
2415
2416 /**
Adrian Roose458aa82015-12-08 16:17:19 -08002417 * Set the remote input history.
2418 *
2419 * This should be set to the most recent inputs that have been sent
2420 * through a {@link RemoteInput} of this Notification and cleared once the it is no
2421 * longer relevant (e.g. for chat notifications once the other party has responded).
2422 *
2423 * The most recent input must be stored at the 0 index, the second most recent at the
2424 * 1 index, etc. Note that the system will limit both how far back the inputs will be shown
2425 * and how much of each individual input is shown.
2426 *
2427 * <p>Note: The reply text will only be shown on notifications that have least one action
2428 * with a {@code RemoteInput}.</p>
2429 */
2430 public Builder setRemoteInputHistory(CharSequence[] text) {
2431 if (text == null) {
2432 mN.extras.putCharSequenceArray(EXTRA_REMOTE_INPUT_HISTORY, null);
2433 } else {
2434 final int N = Math.min(MAX_REPLY_HISTORY, text.length);
2435 CharSequence[] safe = new CharSequence[N];
2436 for (int i = 0; i < N; i++) {
2437 safe[i] = safeCharSequence(text[i]);
2438 }
2439 mN.extras.putCharSequenceArray(EXTRA_REMOTE_INPUT_HISTORY, safe);
2440 }
2441 return this;
2442 }
2443
2444 /**
Joe Onoratocb109a02011-01-18 17:57:41 -08002445 * Set the large number at the right-hand side of the notification. This is
2446 * equivalent to setContentInfo, although it might show the number in a different
2447 * font size for readability.
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07002448 *
2449 * @deprecated this number is not shown anywhere anymore
Joe Onoratocb109a02011-01-18 17:57:41 -08002450 */
Joe Onorato8595a3d2010-11-19 18:12:07 -08002451 public Builder setNumber(int number) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002452 mN.number = number;
Joe Onorato8595a3d2010-11-19 18:12:07 -08002453 return this;
2454 }
2455
Joe Onoratocb109a02011-01-18 17:57:41 -08002456 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002457 * A small piece of additional information pertaining to this notification.
2458 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002459 * The platform template will draw this on the last line of the notification, at the far
2460 * right (to the right of a smallIcon if it has been placed there).
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07002461 *
2462 * @deprecated use {@link #setSubText(CharSequence)} instead to set a text in the header.
2463 * For legacy apps targeting a version below {@link android.os.Build.VERSION_CODES#N} this
2464 * field will still show up, but the subtext will take precedence.
Joe Onoratocb109a02011-01-18 17:57:41 -08002465 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002466 public Builder setContentInfo(CharSequence info) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002467 mN.extras.putCharSequence(EXTRA_INFO_TEXT, safeCharSequence(info));
Joe Onorato46439ce2010-11-19 13:56:21 -08002468 return this;
2469 }
2470
Joe Onoratocb109a02011-01-18 17:57:41 -08002471 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002472 * Set the progress this notification represents.
2473 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002474 * The platform template will represent this using a {@link ProgressBar}.
Jeff Sharkey1c400132011-08-05 14:50:13 -07002475 */
2476 public Builder setProgress(int max, int progress, boolean indeterminate) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002477 mN.extras.putInt(EXTRA_PROGRESS, progress);
2478 mN.extras.putInt(EXTRA_PROGRESS_MAX, max);
2479 mN.extras.putBoolean(EXTRA_PROGRESS_INDETERMINATE, indeterminate);
Jeff Sharkey1c400132011-08-05 14:50:13 -07002480 return this;
2481 }
2482
2483 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002484 * Supply a custom RemoteViews to use instead of the platform template.
2485 *
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002486 * Use {@link #setCustomContentView(RemoteViews)} instead.
Joe Onoratocb109a02011-01-18 17:57:41 -08002487 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002488 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -08002489 public Builder setContent(RemoteViews views) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002490 return setCustomContentView(views);
2491 }
2492
2493 /**
2494 * Supply custom RemoteViews to use instead of the platform template.
2495 *
2496 * This will override the layout that would otherwise be constructed by this Builder
2497 * object.
2498 */
2499 public Builder setCustomContentView(RemoteViews contentView) {
2500 mN.contentView = contentView;
2501 return this;
2502 }
2503
2504 /**
2505 * Supply custom RemoteViews to use instead of the platform template in the expanded form.
2506 *
2507 * This will override the expanded layout that would otherwise be constructed by this
2508 * Builder object.
2509 */
2510 public Builder setCustomBigContentView(RemoteViews contentView) {
2511 mN.bigContentView = contentView;
2512 return this;
2513 }
2514
2515 /**
2516 * Supply custom RemoteViews to use instead of the platform template in the heads up dialog.
2517 *
2518 * This will override the heads-up layout that would otherwise be constructed by this
2519 * Builder object.
2520 */
2521 public Builder setCustomHeadsUpContentView(RemoteViews contentView) {
2522 mN.headsUpContentView = contentView;
Joe Onorato46439ce2010-11-19 13:56:21 -08002523 return this;
2524 }
2525
Joe Onoratocb109a02011-01-18 17:57:41 -08002526 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002527 * Supply a {@link PendingIntent} to be sent when the notification is clicked.
2528 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002529 * As of {@link android.os.Build.VERSION_CODES#HONEYCOMB}, if this field is unset and you
2530 * have specified a custom RemoteViews with {@link #setContent(RemoteViews)}, you can use
2531 * {@link RemoteViews#setOnClickPendingIntent RemoteViews.setOnClickPendingIntent(int,PendingIntent)}
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002532 * to assign PendingIntents to individual views in that custom layout (i.e., to create
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002533 * clickable buttons inside the notification view).
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002534 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002535 * @see Notification#contentIntent Notification.contentIntent
Joe Onoratocb109a02011-01-18 17:57:41 -08002536 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002537 public Builder setContentIntent(PendingIntent intent) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002538 mN.contentIntent = intent;
Joe Onorato46439ce2010-11-19 13:56:21 -08002539 return this;
2540 }
2541
Joe Onoratocb109a02011-01-18 17:57:41 -08002542 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002543 * Supply a {@link PendingIntent} to send when the notification is cleared explicitly by the user.
2544 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002545 * @see Notification#deleteIntent
Joe Onoratocb109a02011-01-18 17:57:41 -08002546 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002547 public Builder setDeleteIntent(PendingIntent intent) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002548 mN.deleteIntent = intent;
Joe Onorato46439ce2010-11-19 13:56:21 -08002549 return this;
2550 }
2551
Joe Onoratocb109a02011-01-18 17:57:41 -08002552 /**
2553 * An intent to launch instead of posting the notification to the status bar.
2554 * Only for use with extremely high-priority notifications demanding the user's
2555 * <strong>immediate</strong> attention, such as an incoming phone call or
2556 * alarm clock that the user has explicitly set to a particular time.
2557 * If this facility is used for something else, please give the user an option
2558 * to turn it off and use a normal notification, as this can be extremely
2559 * disruptive.
2560 *
Chris Wren47c20a12014-06-18 17:27:29 -04002561 * <p>
2562 * The system UI may choose to display a heads-up notification, instead of
2563 * launching this intent, while the user is using the device.
2564 * </p>
2565 *
Joe Onoratocb109a02011-01-18 17:57:41 -08002566 * @param intent The pending intent to launch.
2567 * @param highPriority Passing true will cause this notification to be sent
2568 * even if other notifications are suppressed.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002569 *
2570 * @see Notification#fullScreenIntent
Joe Onoratocb109a02011-01-18 17:57:41 -08002571 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002572 public Builder setFullScreenIntent(PendingIntent intent, boolean highPriority) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002573 mN.fullScreenIntent = intent;
Joe Onorato46439ce2010-11-19 13:56:21 -08002574 setFlag(FLAG_HIGH_PRIORITY, highPriority);
2575 return this;
2576 }
2577
Joe Onoratocb109a02011-01-18 17:57:41 -08002578 /**
Dan Sandler5fcdf6e2014-07-18 11:31:15 -04002579 * Set the "ticker" text which is sent to accessibility services.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002580 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002581 * @see Notification#tickerText
Joe Onoratocb109a02011-01-18 17:57:41 -08002582 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002583 public Builder setTicker(CharSequence tickerText) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002584 mN.tickerText = safeCharSequence(tickerText);
Joe Onorato46439ce2010-11-19 13:56:21 -08002585 return this;
2586 }
2587
Joe Onoratocb109a02011-01-18 17:57:41 -08002588 /**
Dan Sandler5fcdf6e2014-07-18 11:31:15 -04002589 * Obsolete version of {@link #setTicker(CharSequence)}.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002590 *
Joe Onoratocb109a02011-01-18 17:57:41 -08002591 */
Dan Sandler5fcdf6e2014-07-18 11:31:15 -04002592 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -08002593 public Builder setTicker(CharSequence tickerText, RemoteViews views) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002594 setTicker(tickerText);
2595 // views is ignored
Joe Onorato46439ce2010-11-19 13:56:21 -08002596 return this;
2597 }
2598
Joe Onoratocb109a02011-01-18 17:57:41 -08002599 /**
Dan Sandlerd63f9322015-05-06 15:18:49 -04002600 * Add a large icon to the notification content view.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002601 *
2602 * In the platform template, this image will be shown on the left of the notification view
Dan Sandlerd63f9322015-05-06 15:18:49 -04002603 * in place of the {@link #setSmallIcon(Icon) small icon} (which will be placed in a small
2604 * badge atop the large icon).
Dan Sandler08a04c12015-05-06 15:18:49 -04002605 */
Dan Sandlerd63f9322015-05-06 15:18:49 -04002606 public Builder setLargeIcon(Bitmap b) {
2607 return setLargeIcon(b != null ? Icon.createWithBitmap(b) : null);
2608 }
2609
2610 /**
2611 * Add a large icon to the notification content view.
2612 *
2613 * In the platform template, this image will be shown on the left of the notification view
2614 * in place of the {@link #setSmallIcon(Icon) small icon} (which will be placed in a small
2615 * badge atop the large icon).
2616 */
2617 public Builder setLargeIcon(Icon icon) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002618 mN.mLargeIcon = icon;
2619 mN.extras.putParcelable(EXTRA_LARGE_ICON, icon);
Joe Onorato46439ce2010-11-19 13:56:21 -08002620 return this;
2621 }
2622
Joe Onoratocb109a02011-01-18 17:57:41 -08002623 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002624 * Set the sound to play.
2625 *
John Spurlockc0650f022014-07-19 13:22:39 -04002626 * It will be played using the {@link #AUDIO_ATTRIBUTES_DEFAULT default audio attributes}
2627 * for notifications.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002628 *
Chris Wren47c20a12014-06-18 17:27:29 -04002629 * <p>
2630 * A notification that is noisy is more likely to be presented as a heads-up notification.
2631 * </p>
2632 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002633 * @see Notification#sound
Joe Onoratocb109a02011-01-18 17:57:41 -08002634 */
Joe Onorato52f80cd2010-11-21 15:34:48 -08002635 public Builder setSound(Uri sound) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002636 mN.sound = sound;
2637 mN.audioAttributes = AUDIO_ATTRIBUTES_DEFAULT;
Joe Onorato52f80cd2010-11-21 15:34:48 -08002638 return this;
2639 }
2640
Joe Onoratocb109a02011-01-18 17:57:41 -08002641 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002642 * Set the sound to play, along with a specific stream on which to play it.
Joe Onoratocb109a02011-01-18 17:57:41 -08002643 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002644 * See {@link android.media.AudioManager} for the <code>STREAM_</code> constants.
2645 *
Chris Wren47c20a12014-06-18 17:27:29 -04002646 * <p>
2647 * A notification that is noisy is more likely to be presented as a heads-up notification.
2648 * </p>
John Spurlockc0650f022014-07-19 13:22:39 -04002649 * @deprecated use {@link #setSound(Uri, AudioAttributes)} instead.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002650 * @see Notification#sound
Joe Onoratocb109a02011-01-18 17:57:41 -08002651 */
Jean-Michel Trivi81f871e2014-08-06 16:32:38 -07002652 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -08002653 public Builder setSound(Uri sound, int streamType) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002654 mN.sound = sound;
2655 mN.audioStreamType = streamType;
Joe Onorato46439ce2010-11-19 13:56:21 -08002656 return this;
2657 }
2658
Joe Onoratocb109a02011-01-18 17:57:41 -08002659 /**
John Spurlockc0650f022014-07-19 13:22:39 -04002660 * Set the sound to play, along with specific {@link AudioAttributes audio attributes} to
2661 * use during playback.
2662 *
2663 * <p>
2664 * A notification that is noisy is more likely to be presented as a heads-up notification.
2665 * </p>
2666 *
2667 * @see Notification#sound
2668 */
2669 public Builder setSound(Uri sound, AudioAttributes audioAttributes) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002670 mN.sound = sound;
2671 mN.audioAttributes = audioAttributes;
John Spurlockc0650f022014-07-19 13:22:39 -04002672 return this;
2673 }
2674
2675 /**
Joe Onoratocb109a02011-01-18 17:57:41 -08002676 * Set the vibration pattern to use.
2677 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002678 * See {@link android.os.Vibrator#vibrate(long[], int)} for a discussion of the
2679 * <code>pattern</code> parameter.
2680 *
Chris Wren47c20a12014-06-18 17:27:29 -04002681 * <p>
2682 * A notification that vibrates is more likely to be presented as a heads-up notification.
2683 * </p>
2684 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002685 * @see Notification#vibrate
Joe Onoratocb109a02011-01-18 17:57:41 -08002686 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002687 public Builder setVibrate(long[] pattern) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002688 mN.vibrate = pattern;
Joe Onorato46439ce2010-11-19 13:56:21 -08002689 return this;
2690 }
2691
Joe Onoratocb109a02011-01-18 17:57:41 -08002692 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002693 * Set the desired color for the indicator LED on the device, as well as the
2694 * blink duty cycle (specified in milliseconds).
2695 *
2696
2697 * Not all devices will honor all (or even any) of these values.
2698 *
2699
2700 * @see Notification#ledARGB
2701 * @see Notification#ledOnMS
2702 * @see Notification#ledOffMS
Joe Onoratocb109a02011-01-18 17:57:41 -08002703 */
Tor Norbye80756e32015-03-02 09:39:27 -08002704 public Builder setLights(@ColorInt int argb, int onMs, int offMs) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002705 mN.ledARGB = argb;
2706 mN.ledOnMS = onMs;
2707 mN.ledOffMS = offMs;
Julia Reynolds10ee1fc2015-11-09 11:04:55 -05002708 if (onMs != 0 || offMs != 0) {
2709 mN.flags |= FLAG_SHOW_LIGHTS;
2710 }
Joe Onorato46439ce2010-11-19 13:56:21 -08002711 return this;
2712 }
2713
Joe Onoratocb109a02011-01-18 17:57:41 -08002714 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002715 * Set whether this is an "ongoing" notification.
Joe Onoratocb109a02011-01-18 17:57:41 -08002716 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002717
2718 * Ongoing notifications cannot be dismissed by the user, so your application or service
2719 * must take care of canceling them.
2720 *
2721
2722 * They are typically used to indicate a background task that the user is actively engaged
2723 * with (e.g., playing music) or is pending in some way and therefore occupying the device
2724 * (e.g., a file download, sync operation, active network connection).
2725 *
2726
2727 * @see Notification#FLAG_ONGOING_EVENT
2728 * @see Service#setForeground(boolean)
Joe Onoratocb109a02011-01-18 17:57:41 -08002729 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002730 public Builder setOngoing(boolean ongoing) {
2731 setFlag(FLAG_ONGOING_EVENT, ongoing);
2732 return this;
2733 }
2734
Joe Onoratocb109a02011-01-18 17:57:41 -08002735 /**
2736 * Set this flag if you would only like the sound, vibrate
2737 * and ticker to be played if the notification is not already showing.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002738 *
2739 * @see Notification#FLAG_ONLY_ALERT_ONCE
Joe Onoratocb109a02011-01-18 17:57:41 -08002740 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002741 public Builder setOnlyAlertOnce(boolean onlyAlertOnce) {
2742 setFlag(FLAG_ONLY_ALERT_ONCE, onlyAlertOnce);
2743 return this;
2744 }
2745
Joe Onoratocb109a02011-01-18 17:57:41 -08002746 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002747 * Make this notification automatically dismissed when the user touches it. The
2748 * PendingIntent set with {@link #setDeleteIntent} will be sent when this happens.
2749 *
2750 * @see Notification#FLAG_AUTO_CANCEL
Joe Onoratocb109a02011-01-18 17:57:41 -08002751 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002752 public Builder setAutoCancel(boolean autoCancel) {
Joe Onorato281d83f2011-01-04 17:13:10 -08002753 setFlag(FLAG_AUTO_CANCEL, autoCancel);
Joe Onorato46439ce2010-11-19 13:56:21 -08002754 return this;
2755 }
2756
Joe Onoratocb109a02011-01-18 17:57:41 -08002757 /**
Griff Hazendfcb0802014-02-11 12:00:00 -08002758 * Set whether or not this notification should not bridge to other devices.
2759 *
2760 * <p>Some notifications can be bridged to other devices for remote display.
2761 * This hint can be set to recommend this notification not be bridged.
2762 */
2763 public Builder setLocalOnly(boolean localOnly) {
2764 setFlag(FLAG_LOCAL_ONLY, localOnly);
2765 return this;
2766 }
2767
2768 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002769 * Set which notification properties will be inherited from system defaults.
Joe Onoratocb109a02011-01-18 17:57:41 -08002770 * <p>
2771 * The value should be one or more of the following fields combined with
2772 * bitwise-or:
2773 * {@link #DEFAULT_SOUND}, {@link #DEFAULT_VIBRATE}, {@link #DEFAULT_LIGHTS}.
2774 * <p>
2775 * For all default values, use {@link #DEFAULT_ALL}.
2776 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002777 public Builder setDefaults(int defaults) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002778 mN.defaults = defaults;
Joe Onorato46439ce2010-11-19 13:56:21 -08002779 return this;
2780 }
2781
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002782 /**
2783 * Set the priority of this notification.
2784 *
2785 * @see Notification#priority
2786 */
Tor Norbyed9273d62013-05-30 15:59:53 -07002787 public Builder setPriority(@Priority int pri) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002788 mN.priority = pri;
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002789 return this;
2790 }
Joe Malin8d40d042012-11-05 11:36:40 -08002791
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002792 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -04002793 * Set the notification category.
Joe Malin8d40d042012-11-05 11:36:40 -08002794 *
John Spurlockfd7f1e02014-03-18 16:41:57 -04002795 * @see Notification#category
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002796 */
John Spurlockfd7f1e02014-03-18 16:41:57 -04002797 public Builder setCategory(String category) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002798 mN.category = category;
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002799 return this;
2800 }
2801
2802 /**
Chris Wrendde75302014-03-26 17:24:15 -04002803 * Add a person that is relevant to this notification.
2804 *
Chris Wrene6c48932014-09-29 17:19:27 -04002805 * <P>
2806 * Depending on user preferences, this annotation may allow the notification to pass
2807 * through interruption filters, and to appear more prominently in the user interface.
2808 * </P>
2809 *
2810 * <P>
2811 * The person should be specified by the {@code String} representation of a
2812 * {@link android.provider.ContactsContract.Contacts#CONTENT_LOOKUP_URI}.
2813 * </P>
2814 *
2815 * <P>The system will also attempt to resolve {@code mailto:} and {@code tel:} schema
2816 * URIs. The path part of these URIs must exist in the contacts database, in the
2817 * appropriate column, or the reference will be discarded as invalid. Telephone schema
2818 * URIs will be resolved by {@link android.provider.ContactsContract.PhoneLookup}.
2819 * </P>
2820 *
2821 * @param uri A URI for the person.
Chris Wrendde75302014-03-26 17:24:15 -04002822 * @see Notification#EXTRA_PEOPLE
2823 */
Chris Wrene6c48932014-09-29 17:19:27 -04002824 public Builder addPerson(String uri) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002825 mPersonList.add(uri);
Chris Wrendde75302014-03-26 17:24:15 -04002826 return this;
2827 }
2828
2829 /**
Griff Hazen5cadc3b2014-05-20 09:55:39 -07002830 * Set this notification to be part of a group of notifications sharing the same key.
2831 * Grouped notifications may display in a cluster or stack on devices which
2832 * support such rendering.
2833 *
2834 * <p>To make this notification the summary for its group, also call
2835 * {@link #setGroupSummary}. A sort order can be specified for group members by using
2836 * {@link #setSortKey}.
2837 * @param groupKey The group key of the group.
2838 * @return this object for method chaining
2839 */
2840 public Builder setGroup(String groupKey) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002841 mN.mGroupKey = groupKey;
Griff Hazen5cadc3b2014-05-20 09:55:39 -07002842 return this;
2843 }
2844
2845 /**
2846 * Set this notification to be the group summary for a group of notifications.
2847 * Grouped notifications may display in a cluster or stack on devices which
2848 * support such rendering. Requires a group key also be set using {@link #setGroup}.
2849 * @param isGroupSummary Whether this notification should be a group summary.
2850 * @return this object for method chaining
2851 */
2852 public Builder setGroupSummary(boolean isGroupSummary) {
2853 setFlag(FLAG_GROUP_SUMMARY, isGroupSummary);
2854 return this;
2855 }
2856
2857 /**
2858 * Set a sort key that orders this notification among other notifications from the
2859 * same package. This can be useful if an external sort was already applied and an app
2860 * would like to preserve this. Notifications will be sorted lexicographically using this
2861 * value, although providing different priorities in addition to providing sort key may
2862 * cause this value to be ignored.
2863 *
2864 * <p>This sort key can also be used to order members of a notification group. See
Griff Hazen9e1379f2014-05-20 12:50:51 -07002865 * {@link #setGroup}.
Griff Hazen5cadc3b2014-05-20 09:55:39 -07002866 *
2867 * @see String#compareTo(String)
2868 */
2869 public Builder setSortKey(String sortKey) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002870 mN.mSortKey = sortKey;
Griff Hazen5cadc3b2014-05-20 09:55:39 -07002871 return this;
2872 }
2873
2874 /**
Griff Hazen720042b2014-02-24 15:46:56 -08002875 * Merge additional metadata into this notification.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002876 *
Griff Hazen720042b2014-02-24 15:46:56 -08002877 * <p>Values within the Bundle will replace existing extras values in this Builder.
2878 *
2879 * @see Notification#extras
2880 */
Griff Hazen959591e2014-05-15 22:26:18 -07002881 public Builder addExtras(Bundle extras) {
2882 if (extras != null) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002883 mUserExtras.putAll(extras);
Griff Hazen720042b2014-02-24 15:46:56 -08002884 }
2885 return this;
2886 }
2887
2888 /**
2889 * Set metadata for this notification.
2890 *
2891 * <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 -04002892 * current contents are copied into the Notification each time {@link #build()} is
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002893 * called.
2894 *
Griff Hazen720042b2014-02-24 15:46:56 -08002895 * <p>Replaces any existing extras values with those from the provided Bundle.
2896 * Use {@link #addExtras} to merge in metadata instead.
2897 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002898 * @see Notification#extras
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002899 */
Griff Hazen959591e2014-05-15 22:26:18 -07002900 public Builder setExtras(Bundle extras) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002901 if (extras != null) {
2902 mUserExtras = extras;
2903 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002904 return this;
2905 }
2906
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002907 /**
Griff Hazen720042b2014-02-24 15:46:56 -08002908 * Get the current metadata Bundle used by this notification Builder.
2909 *
2910 * <p>The returned Bundle is shared with this Builder.
2911 *
2912 * <p>The current contents of this Bundle are copied into the Notification each time
2913 * {@link #build()} is called.
2914 *
2915 * @see Notification#extras
2916 */
2917 public Bundle getExtras() {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002918 return mUserExtras;
2919 }
2920
2921 private Bundle getAllExtras() {
2922 final Bundle saveExtras = (Bundle) mUserExtras.clone();
2923 saveExtras.putAll(mN.extras);
2924 return saveExtras;
Griff Hazen720042b2014-02-24 15:46:56 -08002925 }
2926
2927 /**
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002928 * Add an action to this notification. Actions are typically displayed by
2929 * the system as a button adjacent to the notification content.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04002930 * <p>
2931 * Every action must have an icon (32dp square and matching the
2932 * <a href="{@docRoot}design/style/iconography.html#action-bar">Holo
2933 * Dark action bar</a> visual style), a textual label, and a {@link PendingIntent}.
2934 * <p>
2935 * A notification in its expanded form can display up to 3 actions, from left to right in
2936 * the order they were added. Actions will not be displayed when the notification is
2937 * collapsed, however, so be sure that any essential functions may be accessed by the user
2938 * in some other way (for example, in the Activity pointed to by {@link #contentIntent}).
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002939 *
2940 * @param icon Resource ID of a drawable that represents the action.
2941 * @param title Text describing the action.
2942 * @param intent PendingIntent to be fired when the action is invoked.
Dan Sandler86647982015-05-13 23:41:13 -04002943 *
2944 * @deprecated Use {@link #addAction(Action)} instead.
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002945 */
Dan Sandler86647982015-05-13 23:41:13 -04002946 @Deprecated
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002947 public Builder addAction(int icon, CharSequence title, PendingIntent intent) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04002948 mActions.add(new Action(icon, safeCharSequence(title), intent));
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002949 return this;
2950 }
2951
Chris Wrenfbd96ba2012-05-01 12:03:58 -04002952 /**
Griff Hazen959591e2014-05-15 22:26:18 -07002953 * Add an action to this notification. Actions are typically displayed by
2954 * the system as a button adjacent to the notification content.
2955 * <p>
2956 * Every action must have an icon (32dp square and matching the
2957 * <a href="{@docRoot}design/style/iconography.html#action-bar">Holo
2958 * Dark action bar</a> visual style), a textual label, and a {@link PendingIntent}.
2959 * <p>
2960 * A notification in its expanded form can display up to 3 actions, from left to right in
2961 * the order they were added. Actions will not be displayed when the notification is
2962 * collapsed, however, so be sure that any essential functions may be accessed by the user
2963 * in some other way (for example, in the Activity pointed to by {@link #contentIntent}).
2964 *
2965 * @param action The action to add.
2966 */
2967 public Builder addAction(Action action) {
2968 mActions.add(action);
2969 return this;
2970 }
2971
2972 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002973 * Alter the complete list of actions attached to this notification.
2974 * @see #addAction(Action).
2975 *
2976 * @param actions
2977 * @return
2978 */
2979 public Builder setActions(Action... actions) {
2980 mActions.clear();
2981 for (int i = 0; i < actions.length; i++) {
2982 mActions.add(actions[i]);
2983 }
2984 return this;
2985 }
2986
2987 /**
Chris Wrenfbd96ba2012-05-01 12:03:58 -04002988 * Add a rich notification style to be applied at build time.
2989 *
2990 * @param style Object responsible for modifying the notification style.
2991 */
2992 public Builder setStyle(Style style) {
2993 if (mStyle != style) {
2994 mStyle = style;
Daniel Sandlerc08dea22012-06-28 08:35:24 -07002995 if (mStyle != null) {
2996 mStyle.setBuilder(this);
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002997 mN.extras.putString(EXTRA_TEMPLATE, style.getClass().getName());
2998 } else {
2999 mN.extras.remove(EXTRA_TEMPLATE);
Daniel Sandlerc08dea22012-06-28 08:35:24 -07003000 }
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003001 }
3002 return this;
3003 }
3004
Dan Sandler0bf2ed82013-12-21 23:33:41 -06003005 /**
3006 * Specify the value of {@link #visibility}.
Griff Hazenb720abe2014-05-20 13:15:30 -07003007 *
Dan Sandler0bf2ed82013-12-21 23:33:41 -06003008 * @param visibility One of {@link #VISIBILITY_PRIVATE} (the default),
3009 * {@link #VISIBILITY_SECRET}, or {@link #VISIBILITY_PUBLIC}.
3010 *
3011 * @return The same Builder.
3012 */
3013 public Builder setVisibility(int visibility) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003014 mN.visibility = visibility;
Dan Sandler0bf2ed82013-12-21 23:33:41 -06003015 return this;
3016 }
3017
3018 /**
3019 * Supply a replacement Notification whose contents should be shown in insecure contexts
3020 * (i.e. atop the secure lockscreen). See {@link #visibility} and {@link #VISIBILITY_PUBLIC}.
3021 * @param n A replacement notification, presumably with some or all info redacted.
3022 * @return The same Builder.
3023 */
3024 public Builder setPublicVersion(Notification n) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003025 if (n != null) {
3026 mN.publicVersion = new Notification();
3027 n.cloneInto(mN.publicVersion, /*heavy=*/ true);
3028 } else {
3029 mN.publicVersion = null;
3030 }
Dan Sandler0bf2ed82013-12-21 23:33:41 -06003031 return this;
3032 }
3033
Griff Hazenb720abe2014-05-20 13:15:30 -07003034 /**
Griff Hazen5cadc3b2014-05-20 09:55:39 -07003035 * Apply an extender to this notification builder. Extenders may be used to add
3036 * metadata or change options on this builder.
3037 */
Griff Hazen61a9e862014-05-22 16:05:19 -07003038 public Builder extend(Extender extender) {
3039 extender.extend(this);
Griff Hazen5cadc3b2014-05-20 09:55:39 -07003040 return this;
3041 }
3042
Dan Sandler4e787062015-06-17 15:09:48 -04003043 /**
3044 * @hide
3045 */
Julia Reynoldse46bb372016-03-17 11:05:58 -04003046 public Builder setFlag(int mask, boolean value) {
Joe Onorato46439ce2010-11-19 13:56:21 -08003047 if (value) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003048 mN.flags |= mask;
Joe Onorato46439ce2010-11-19 13:56:21 -08003049 } else {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003050 mN.flags &= ~mask;
Joe Onorato46439ce2010-11-19 13:56:21 -08003051 }
Julia Reynoldse46bb372016-03-17 11:05:58 -04003052 return this;
Joe Onorato46439ce2010-11-19 13:56:21 -08003053 }
3054
Dan Sandler26e81cf2014-05-06 10:01:27 -04003055 /**
3056 * Sets {@link Notification#color}.
3057 *
3058 * @param argb The accent color to use
3059 *
3060 * @return The same Builder.
3061 */
Tor Norbye80756e32015-03-02 09:39:27 -08003062 public Builder setColor(@ColorInt int argb) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003063 mN.color = argb;
Julia Reynolds10ee1fc2015-11-09 11:04:55 -05003064 sanitizeColor();
Dan Sandler26e81cf2014-05-06 10:01:27 -04003065 return this;
3066 }
3067
Jorim Jaggid05aa3e2014-08-28 17:52:27 +02003068 private Drawable getProfileBadgeDrawable() {
Christoph Studer7ac80e62014-08-04 16:01:57 +02003069 // Note: This assumes that the current user can read the profile badge of the
3070 // originating user.
Selim Cineke6ff9462016-01-15 15:07:06 -08003071 return mContext.getPackageManager().getUserBadgeForDensityNoBackground(
Julia Reynoldsda303542015-11-23 14:00:20 -05003072 new UserHandle(mContext.getUserId()), 0);
Jorim Jaggid05aa3e2014-08-28 17:52:27 +02003073 }
3074
3075 private Bitmap getProfileBadge() {
3076 Drawable badge = getProfileBadgeDrawable();
Kenny Guy8a0101b2014-05-08 23:34:12 +01003077 if (badge == null) {
3078 return null;
3079 }
Jorim Jaggid05aa3e2014-08-28 17:52:27 +02003080 final int size = mContext.getResources().getDimensionPixelSize(
3081 R.dimen.notification_badge_size);
3082 Bitmap bitmap = Bitmap.createBitmap(size, size, Bitmap.Config.ARGB_8888);
Kenny Guy8a0101b2014-05-08 23:34:12 +01003083 Canvas canvas = new Canvas(bitmap);
Jorim Jaggid05aa3e2014-08-28 17:52:27 +02003084 badge.setBounds(0, 0, size, size);
Kenny Guy8a0101b2014-05-08 23:34:12 +01003085 badge.draw(canvas);
3086 return bitmap;
3087 }
3088
Selim Cinekc848c3a2016-01-13 15:27:30 -08003089 private void bindProfileBadge(RemoteViews contentView) {
Kenny Guy98193ea2014-07-24 19:54:37 +01003090 Bitmap profileBadge = getProfileBadge();
3091
Kenny Guy98193ea2014-07-24 19:54:37 +01003092 if (profileBadge != null) {
Selim Cinekc848c3a2016-01-13 15:27:30 -08003093 contentView.setImageViewBitmap(R.id.profile_badge, profileBadge);
3094 contentView.setViewVisibility(R.id.profile_badge, View.VISIBLE);
Kenny Guy98193ea2014-07-24 19:54:37 +01003095 }
Kenny Guy98193ea2014-07-24 19:54:37 +01003096 }
3097
Christoph Studerfe718432014-09-01 18:21:18 +02003098 private void resetStandardTemplate(RemoteViews contentView) {
Selim Cinekeaa29ca2015-11-23 13:51:13 -08003099 resetNotificationHeader(contentView);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003100 resetContentMargins(contentView);
Christoph Studerfe718432014-09-01 18:21:18 +02003101 contentView.setViewVisibility(R.id.right_icon, View.GONE);
Selim Cinek860b6da2015-12-16 19:02:19 -08003102 contentView.setViewVisibility(R.id.title, View.GONE);
Christoph Studerfe718432014-09-01 18:21:18 +02003103 contentView.setTextViewText(R.id.title, null);
Selim Cinek41598732016-01-11 16:58:37 -08003104 contentView.setViewVisibility(R.id.text, View.GONE);
Christoph Studerfe718432014-09-01 18:21:18 +02003105 contentView.setTextViewText(R.id.text, null);
Selim Cinek29603462015-11-17 19:04:39 -08003106 contentView.setViewVisibility(R.id.text_line_1, View.GONE);
Selim Cinek41598732016-01-11 16:58:37 -08003107 contentView.setTextViewText(R.id.text_line_1, null);
Christoph Studerfe718432014-09-01 18:21:18 +02003108 contentView.setViewVisibility(R.id.progress, View.GONE);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003109 }
3110
Selim Cinekeaa29ca2015-11-23 13:51:13 -08003111 /**
3112 * Resets the notification header to its original state
3113 */
3114 private void resetNotificationHeader(RemoteViews contentView) {
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003115 contentView.setImageViewResource(R.id.icon, 0);
Selim Cinek7b836392015-12-04 20:02:59 -08003116 contentView.setBoolean(R.id.notification_header, "setExpanded", false);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003117 contentView.setTextViewText(R.id.app_name_text, null);
Christoph Studerca1db712014-09-10 17:31:33 +02003118 contentView.setViewVisibility(R.id.chronometer, View.GONE);
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07003119 contentView.setViewVisibility(R.id.header_text, View.GONE);
3120 contentView.setViewVisibility(R.id.header_text_divider, View.GONE);
Selim Cinek29603462015-11-17 19:04:39 -08003121 contentView.setViewVisibility(R.id.time_divider, View.GONE);
Selim Cinekc848c3a2016-01-13 15:27:30 -08003122 contentView.setImageViewIcon(R.id.profile_badge, null);
3123 contentView.setViewVisibility(R.id.profile_badge, View.GONE);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003124 }
3125
3126 private void resetContentMargins(RemoteViews contentView) {
3127 contentView.setViewLayoutMarginEnd(R.id.line1, 0);
Selim Cinekc848c3a2016-01-13 15:27:30 -08003128 contentView.setViewLayoutMarginEnd(R.id.text, 0);
Christoph Studerfe718432014-09-01 18:21:18 +02003129 }
3130
Jorim Jaggi445d3c02014-08-19 22:33:42 +02003131 private RemoteViews applyStandardTemplate(int resId) {
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02003132 return applyStandardTemplate(resId, true /* hasProgress */);
3133 }
3134
3135 /**
3136 * @param hasProgress whether the progress bar should be shown and set
3137 */
3138 private RemoteViews applyStandardTemplate(int resId, boolean hasProgress) {
Kenny Guy77320062014-08-27 21:37:15 +01003139 RemoteViews contentView = new BuilderRemoteViews(mContext.getApplicationInfo(), resId);
Dan Sandler539aad42014-08-04 00:43:39 -04003140
Christoph Studerfe718432014-09-01 18:21:18 +02003141 resetStandardTemplate(contentView);
3142
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003143 final Bundle ex = mN.extras;
Dan Sandler190d58d2014-05-15 09:33:39 -04003144
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003145 bindNotificationHeader(contentView);
3146 bindLargeIcon(contentView);
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003147 if (ex.getCharSequence(EXTRA_TITLE) != null) {
Selim Cinek860b6da2015-12-16 19:02:19 -08003148 contentView.setViewVisibility(R.id.title, View.VISIBLE);
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003149 contentView.setTextViewText(R.id.title,
3150 processLegacyText(ex.getCharSequence(EXTRA_TITLE)));
Joe Onorato561d3852010-11-20 18:09:34 -08003151 }
Selim Cinek29603462015-11-17 19:04:39 -08003152 boolean showProgress = handleProgressBar(hasProgress, contentView, ex);
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003153 if (ex.getCharSequence(EXTRA_TEXT) != null) {
Selim Cinek41598732016-01-11 16:58:37 -08003154 int textId = showProgress ? com.android.internal.R.id.text_line_1
3155 : com.android.internal.R.id.text;
3156 contentView.setTextViewText(textId, processLegacyText(
3157 ex.getCharSequence(EXTRA_TEXT)));
3158 contentView.setViewVisibility(textId, View.VISIBLE);
Joe Onorato561d3852010-11-20 18:09:34 -08003159 }
Selim Cinekc848c3a2016-01-13 15:27:30 -08003160
Selim Cinek860b6da2015-12-16 19:02:19 -08003161 setContentMinHeight(contentView, showProgress || mN.mLargeIcon != null);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003162
Selim Cinek29603462015-11-17 19:04:39 -08003163 return contentView;
3164 }
3165
Selim Cinek860b6da2015-12-16 19:02:19 -08003166 /**
3167 * @param remoteView the remote view to update the minheight in
3168 * @param hasMinHeight does it have a mimHeight
3169 * @hide
3170 */
3171 void setContentMinHeight(RemoteViews remoteView, boolean hasMinHeight) {
3172 int minHeight = 0;
3173 if (hasMinHeight) {
3174 // we need to set the minHeight of the notification
3175 minHeight = mContext.getResources().getDimensionPixelSize(
3176 com.android.internal.R.dimen.notification_min_content_height);
3177 }
3178 remoteView.setInt(R.id.notification_main_column, "setMinimumHeight", minHeight);
3179 }
3180
Selim Cinek29603462015-11-17 19:04:39 -08003181 private boolean handleProgressBar(boolean hasProgress, RemoteViews contentView, Bundle ex) {
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003182 final int max = ex.getInt(EXTRA_PROGRESS_MAX, 0);
3183 final int progress = ex.getInt(EXTRA_PROGRESS, 0);
3184 final boolean ind = ex.getBoolean(EXTRA_PROGRESS_INDETERMINATE);
3185 if (hasProgress && (max != 0 || ind)) {
Selim Cinek29603462015-11-17 19:04:39 -08003186 contentView.setViewVisibility(com.android.internal.R.id.progress, View.VISIBLE);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003187 contentView.setProgressBar(
Selim Cinek29603462015-11-17 19:04:39 -08003188 R.id.progress, max, progress, ind);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003189 contentView.setProgressBackgroundTintList(
3190 R.id.progress, ColorStateList.valueOf(mContext.getColor(
3191 R.color.notification_progress_background_color)));
Selim Cinek29603462015-11-17 19:04:39 -08003192 if (mN.color != COLOR_DEFAULT) {
Adrian Roos4ff3b122016-02-01 12:26:13 -08003193 ColorStateList colorStateList = ColorStateList.valueOf(resolveContrastColor());
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003194 contentView.setProgressTintList(R.id.progress, colorStateList);
3195 contentView.setProgressIndeterminateTintList(R.id.progress, colorStateList);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003196 }
Selim Cinek29603462015-11-17 19:04:39 -08003197 return true;
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003198 } else {
3199 contentView.setViewVisibility(R.id.progress, View.GONE);
Selim Cinek29603462015-11-17 19:04:39 -08003200 return false;
Jeff Sharkey1c400132011-08-05 14:50:13 -07003201 }
Joe Onorato561d3852010-11-20 18:09:34 -08003202 }
3203
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003204 private void bindLargeIcon(RemoteViews contentView) {
3205 if (mN.mLargeIcon != null) {
3206 contentView.setViewVisibility(R.id.right_icon, View.VISIBLE);
3207 contentView.setImageViewIcon(R.id.right_icon, mN.mLargeIcon);
3208 processLargeLegacyIcon(mN.mLargeIcon, contentView);
3209 int endMargin = mContext.getResources().getDimensionPixelSize(
3210 R.dimen.notification_content_picture_margin);
3211 contentView.setViewLayoutMarginEnd(R.id.line1, endMargin);
Selim Cinekc848c3a2016-01-13 15:27:30 -08003212 contentView.setViewLayoutMarginEnd(R.id.text, endMargin);
Selim Cinek29603462015-11-17 19:04:39 -08003213 contentView.setViewLayoutMarginEnd(R.id.progress, endMargin);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003214 }
3215 }
3216
3217 private void bindNotificationHeader(RemoteViews contentView) {
3218 bindSmallIcon(contentView);
3219 bindHeaderAppName(contentView);
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07003220 bindHeaderText(contentView);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003221 bindHeaderChronometerAndTime(contentView);
3222 bindExpandButton(contentView);
Selim Cinekc848c3a2016-01-13 15:27:30 -08003223 bindProfileBadge(contentView);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003224 }
3225
3226 private void bindExpandButton(RemoteViews contentView) {
Adrian Roos4ff3b122016-02-01 12:26:13 -08003227 contentView.setDrawableParameters(R.id.expand_button, false, -1, resolveContrastColor(),
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003228 PorterDuff.Mode.SRC_ATOP, -1);
Selim Cinekea4bef72015-12-02 15:51:10 -08003229 contentView.setInt(R.id.notification_header, "setOriginalNotificationColor",
Adrian Roos4ff3b122016-02-01 12:26:13 -08003230 resolveContrastColor());
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003231 }
3232
3233 private void bindHeaderChronometerAndTime(RemoteViews contentView) {
3234 if (showsTimeOrChronometer()) {
Selim Cinek29603462015-11-17 19:04:39 -08003235 contentView.setViewVisibility(R.id.time_divider, View.VISIBLE);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003236 if (mN.extras.getBoolean(EXTRA_SHOW_CHRONOMETER)) {
3237 contentView.setViewVisibility(R.id.chronometer, View.VISIBLE);
3238 contentView.setLong(R.id.chronometer, "setBase",
3239 mN.when + (SystemClock.elapsedRealtime() - System.currentTimeMillis()));
3240 contentView.setBoolean(R.id.chronometer, "setStarted", true);
Selim Cinek81c23aa2016-02-25 16:23:13 -08003241 boolean countsDown = mN.extras.getBoolean(EXTRA_CHRONOMETER_COUNTS_DOWN);
3242 contentView.setChronometerCountsDown(R.id.chronometer, countsDown);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003243 } else {
3244 contentView.setViewVisibility(R.id.time, View.VISIBLE);
3245 contentView.setLong(R.id.time, "setTime", mN.when);
3246 }
3247 }
3248 }
3249
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07003250 private void bindHeaderText(RemoteViews contentView) {
3251 CharSequence headerText = mN.extras.getCharSequence(EXTRA_SUB_TEXT);
3252 if (headerText == null && mStyle != null && mStyle.mSummaryTextSet
Selim Cinek03d0d652015-11-13 13:18:09 -05003253 && mStyle.hasSummaryInHeader()) {
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07003254 headerText = mStyle.mSummaryText;
Selim Cinek03d0d652015-11-13 13:18:09 -05003255 }
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07003256 if (headerText == null
3257 && mContext.getApplicationInfo().targetSdkVersion < Build.VERSION_CODES.N
3258 && mN.extras.getCharSequence(EXTRA_INFO_TEXT) != null) {
3259 headerText = mN.extras.getCharSequence(EXTRA_INFO_TEXT);
3260 }
3261 if (headerText != null) {
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003262 // TODO: Remove the span entirely to only have the string with propper formating.
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07003263 contentView.setTextViewText(R.id.header_text, processLegacyText(headerText));
3264 contentView.setViewVisibility(R.id.header_text, View.VISIBLE);
3265 contentView.setViewVisibility(R.id.header_text_divider, View.VISIBLE);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003266 }
3267 }
3268
3269 private void bindHeaderAppName(RemoteViews contentView) {
Tony Mak4dc008c2016-03-16 10:46:49 +00003270 CharSequence appName = mContext.getPackageManager()
3271 .getApplicationLabel(mContext.getApplicationInfo());
3272
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003273 if (TextUtils.isEmpty(appName)) {
3274 return;
3275 }
3276 contentView.setTextViewText(R.id.app_name_text, appName);
Adrian Roos4ff3b122016-02-01 12:26:13 -08003277 contentView.setTextColor(R.id.app_name_text, resolveContrastColor());
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003278 }
3279
3280 private void bindSmallIcon(RemoteViews contentView) {
3281 contentView.setImageViewIcon(R.id.icon, mN.mSmallIcon);
3282 processSmallIconColor(mN.mSmallIcon, contentView);
3283 }
3284
Jorim Jaggi445d3c02014-08-19 22:33:42 +02003285 /**
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02003286 * @return true if the built notification will show the time or the chronometer; false
3287 * otherwise
3288 */
3289 private boolean showsTimeOrChronometer() {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003290 return mN.when != 0 && mN.extras.getBoolean(EXTRA_SHOW_WHEN);
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02003291 }
3292
Christoph Studerfe718432014-09-01 18:21:18 +02003293 private void resetStandardTemplateWithActions(RemoteViews big) {
Adrian Roos7052de52016-03-03 15:53:34 -08003294 big.setViewVisibility(R.id.actions_container, View.GONE);
Christoph Studerfe718432014-09-01 18:21:18 +02003295 big.setViewVisibility(R.id.actions, View.GONE);
Christoph Studerfe718432014-09-01 18:21:18 +02003296 big.removeAllViews(R.id.actions);
Adrian Roose458aa82015-12-08 16:17:19 -08003297
3298 big.setViewVisibility(R.id.notification_material_reply_container, View.GONE);
3299 big.setTextViewText(R.id.notification_material_reply_text_1, null);
3300
3301 big.setViewVisibility(R.id.notification_material_reply_text_2, View.GONE);
3302 big.setTextViewText(R.id.notification_material_reply_text_2, null);
3303 big.setViewVisibility(R.id.notification_material_reply_text_3, View.GONE);
3304 big.setTextViewText(R.id.notification_material_reply_text_3, null);
Christoph Studerfe718432014-09-01 18:21:18 +02003305 }
3306
Daniel Sandler96fd7c12012-03-30 16:37:36 -04003307 private RemoteViews applyStandardTemplateWithActions(int layoutId) {
Jorim Jaggi445d3c02014-08-19 22:33:42 +02003308 RemoteViews big = applyStandardTemplate(layoutId);
Daniel Sandler96fd7c12012-03-30 16:37:36 -04003309
Christoph Studerfe718432014-09-01 18:21:18 +02003310 resetStandardTemplateWithActions(big);
3311
Adrian Roose458aa82015-12-08 16:17:19 -08003312 boolean validRemoteInput = false;
3313
Daniel Sandler96fd7c12012-03-30 16:37:36 -04003314 int N = mActions.size();
3315 if (N > 0) {
Adrian Roos7052de52016-03-03 15:53:34 -08003316 big.setViewVisibility(R.id.actions_container, View.VISIBLE);
Daniel Sandler96fd7c12012-03-30 16:37:36 -04003317 big.setViewVisibility(R.id.actions, View.VISIBLE);
Daniel Sandler8680bf82012-05-15 16:52:52 -04003318 if (N>MAX_ACTION_BUTTONS) N=MAX_ACTION_BUTTONS;
Daniel Sandler96fd7c12012-03-30 16:37:36 -04003319 for (int i=0; i<N; i++) {
Adrian Roose458aa82015-12-08 16:17:19 -08003320 Action action = mActions.get(i);
3321 validRemoteInput |= hasValidRemoteInput(action);
3322
3323 final RemoteViews button = generateActionButton(action);
Adrian Roos9b123cf2016-02-04 14:55:57 -08003324 if (i == N - 1) {
3325 button.setViewLayoutWidth(com.android.internal.R.id.action0,
3326 ViewGroup.LayoutParams.MATCH_PARENT);
3327 }
Daniel Sandler96fd7c12012-03-30 16:37:36 -04003328 big.addView(R.id.actions, button);
3329 }
3330 }
Adrian Roose458aa82015-12-08 16:17:19 -08003331
3332 CharSequence[] replyText = mN.extras.getCharSequenceArray(EXTRA_REMOTE_INPUT_HISTORY);
3333 if (validRemoteInput && replyText != null
3334 && replyText.length > 0 && !TextUtils.isEmpty(replyText[0])) {
3335 big.setViewVisibility(R.id.notification_material_reply_container, View.VISIBLE);
3336 big.setTextViewText(R.id.notification_material_reply_text_1, replyText[0]);
3337
3338 if (replyText.length > 1 && !TextUtils.isEmpty(replyText[1])) {
3339 big.setViewVisibility(R.id.notification_material_reply_text_2, View.VISIBLE);
3340 big.setTextViewText(R.id.notification_material_reply_text_2, replyText[1]);
3341
3342 if (replyText.length > 2 && !TextUtils.isEmpty(replyText[2])) {
3343 big.setViewVisibility(
3344 R.id.notification_material_reply_text_3, View.VISIBLE);
3345 big.setTextViewText(R.id.notification_material_reply_text_3, replyText[2]);
3346 }
3347 }
3348 }
3349
Daniel Sandler96fd7c12012-03-30 16:37:36 -04003350 return big;
3351 }
3352
Adrian Roose458aa82015-12-08 16:17:19 -08003353 private boolean hasValidRemoteInput(Action action) {
3354 if (TextUtils.isEmpty(action.title) || action.actionIntent == null) {
3355 // Weird actions
3356 return false;
3357 }
3358
3359 RemoteInput[] remoteInputs = action.getRemoteInputs();
3360 if (remoteInputs == null) {
3361 return false;
3362 }
3363
3364 for (RemoteInput r : remoteInputs) {
3365 CharSequence[] choices = r.getChoices();
3366 if (r.getAllowFreeFormInput() || (choices != null && choices.length != 0)) {
3367 return true;
3368 }
3369 }
3370 return false;
3371 }
3372
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003373 /**
3374 * Construct a RemoteViews for the final 1U notification layout. In order:
3375 * 1. Custom contentView from the caller
3376 * 2. Style's proposed content view
3377 * 3. Standard template view
3378 */
Julia Reynolds3b848122016-02-26 10:45:32 -05003379 public RemoteViews createContentView() {
Selim Cinek593610c2016-02-16 18:42:57 -08003380 if (mN.contentView != null && (mStyle == null || !mStyle.displayCustomViewInline())) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003381 return mN.contentView;
3382 } else if (mStyle != null) {
3383 final RemoteViews styleView = mStyle.makeContentView();
3384 if (styleView != null) {
3385 return styleView;
3386 }
Joe Onorato46439ce2010-11-19 13:56:21 -08003387 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003388 return applyStandardTemplate(getBaseLayoutResource());
Joe Onorato46439ce2010-11-19 13:56:21 -08003389 }
3390
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003391 /**
3392 * Construct a RemoteViews for the final big notification layout.
3393 */
Julia Reynolds3b848122016-02-26 10:45:32 -05003394 public RemoteViews createBigContentView() {
Selim Cinek850a8542015-11-11 11:48:36 -05003395 RemoteViews result = null;
Selim Cinek593610c2016-02-16 18:42:57 -08003396 if (mN.bigContentView != null
3397 && (mStyle == null || !mStyle.displayCustomViewInline())) {
Julia Reynolds089e3e42015-11-18 09:59:57 -05003398 return mN.bigContentView;
3399 } else if (mStyle != null) {
Selim Cinek850a8542015-11-11 11:48:36 -05003400 result = mStyle.makeBigContentView();
Selim Cinek90dcf6d2015-11-18 20:24:13 -08003401 hideLine1Text(result);
Selim Cinekcc10bfb2016-02-10 16:24:21 -08003402 } else if (mActions.size() != 0) {
3403 result = applyStandardTemplateWithActions(getBigBaseLayoutResource());
Selim Cinek850a8542015-11-11 11:48:36 -05003404 }
3405 adaptNotificationHeaderForBigContentView(result);
3406 return result;
3407 }
Daniel Sandler96fd7c12012-03-30 16:37:36 -04003408
Selim Cinekeaa29ca2015-11-23 13:51:13 -08003409 /**
3410 * Construct a RemoteViews for the final notification header only
3411 *
3412 * @hide
3413 */
3414 public RemoteViews makeNotificationHeader() {
3415 RemoteViews header = new BuilderRemoteViews(mContext.getApplicationInfo(),
3416 R.layout.notification_template_header);
3417 resetNotificationHeader(header);
3418 bindNotificationHeader(header);
3419 return header;
3420 }
3421
Selim Cinek29603462015-11-17 19:04:39 -08003422 private void hideLine1Text(RemoteViews result) {
Selim Cinekcc10bfb2016-02-10 16:24:21 -08003423 if (result != null) {
3424 result.setViewVisibility(R.id.text_line_1, View.GONE);
3425 }
Selim Cinek29603462015-11-17 19:04:39 -08003426 }
3427
Selim Cinek850a8542015-11-11 11:48:36 -05003428 private void adaptNotificationHeaderForBigContentView(RemoteViews result) {
Selim Cinekcc10bfb2016-02-10 16:24:21 -08003429 if (result != null) {
3430 result.setBoolean(R.id.notification_header, "setExpanded", true);
3431 }
Daniel Sandler96fd7c12012-03-30 16:37:36 -04003432 }
3433
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003434 /**
3435 * Construct a RemoteViews for the final heads-up notification layout.
3436 */
Julia Reynolds3b848122016-02-26 10:45:32 -05003437 public RemoteViews createHeadsUpContentView() {
Selim Cinek593610c2016-02-16 18:42:57 -08003438 if (mN.headsUpContentView != null
3439 && (mStyle == null || !mStyle.displayCustomViewInline())) {
Julia Reynolds089e3e42015-11-18 09:59:57 -05003440 return mN.headsUpContentView;
3441 } else if (mStyle != null) {
3442 final RemoteViews styleView = mStyle.makeHeadsUpContentView();
3443 if (styleView != null) {
3444 return styleView;
3445 }
3446 } else if (mActions.size() == 0) {
3447 return null;
3448 }
3449
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01003450 return applyStandardTemplateWithActions(getBigBaseLayoutResource());
Chris Wren8fd39ec2014-02-27 17:43:26 -05003451 }
3452
Selim Cinek624c02db2015-12-14 21:00:02 -08003453 /**
3454 * Construct a RemoteViews for the display in public contexts like on the lockscreen.
3455 *
3456 * @hide
3457 */
3458 public RemoteViews makePublicContentView() {
3459 if (mN.publicVersion != null) {
3460 final Builder builder = recoverBuilder(mContext, mN.publicVersion);
Julia Reynolds3b848122016-02-26 10:45:32 -05003461 return builder.createContentView();
Selim Cinek624c02db2015-12-14 21:00:02 -08003462 }
3463 Bundle savedBundle = mN.extras;
3464 Style style = mStyle;
3465 mStyle = null;
3466 Icon largeIcon = mN.mLargeIcon;
3467 mN.mLargeIcon = null;
3468 Bundle publicExtras = new Bundle();
3469 publicExtras.putBoolean(EXTRA_SHOW_WHEN,
3470 savedBundle.getBoolean(EXTRA_SHOW_WHEN));
3471 publicExtras.putBoolean(EXTRA_SHOW_CHRONOMETER,
3472 savedBundle.getBoolean(EXTRA_SHOW_CHRONOMETER));
Selim Cinek81c23aa2016-02-25 16:23:13 -08003473 publicExtras.putBoolean(EXTRA_CHRONOMETER_COUNTS_DOWN,
3474 savedBundle.getBoolean(EXTRA_CHRONOMETER_COUNTS_DOWN));
Selim Cinek624c02db2015-12-14 21:00:02 -08003475 publicExtras.putCharSequence(EXTRA_TITLE,
3476 mContext.getString(R.string.notification_hidden_text));
3477 mN.extras = publicExtras;
3478 final RemoteViews publicView = applyStandardTemplate(getBaseLayoutResource());
3479 mN.extras = savedBundle;
3480 mN.mLargeIcon = largeIcon;
3481 mStyle = style;
3482 return publicView;
3483 }
3484
3485
Chris Wren8fd39ec2014-02-27 17:43:26 -05003486
Daniel Sandler96fd7c12012-03-30 16:37:36 -04003487 private RemoteViews generateActionButton(Action action) {
Daniel Sandler8680bf82012-05-15 16:52:52 -04003488 final boolean tombstone = (action.actionIntent == null);
Selim Cinekf33b1112015-07-15 17:45:11 -07003489 RemoteViews button = new BuilderRemoteViews(mContext.getApplicationInfo(),
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01003490 tombstone ? getActionTombstoneLayoutResource()
3491 : getActionLayoutResource());
Dan Sandler68079d52015-07-22 10:45:30 -04003492 final Icon ai = action.getIcon();
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01003493 button.setTextViewText(R.id.action0, processLegacyText(action.title));
Daniel Sandler8680bf82012-05-15 16:52:52 -04003494 if (!tombstone) {
Daniel Sandlere5518842012-05-10 16:20:40 -04003495 button.setOnClickPendingIntent(R.id.action0, action.actionIntent);
Daniel Sandlere5518842012-05-10 16:20:40 -04003496 }
Daniel Sandler96fd7c12012-03-30 16:37:36 -04003497 button.setContentDescription(R.id.action0, action.title);
Adrian Roosfe84e1f2015-11-04 15:55:39 -08003498 if (action.mRemoteInputs != null) {
3499 button.setRemoteInputs(R.id.action0, action.mRemoteInputs);
3500 }
3501 if (mN.color != COLOR_DEFAULT) {
Adrian Roos4ff3b122016-02-01 12:26:13 -08003502 button.setTextColor(R.id.action0, resolveContrastColor());
Adrian Roosfe84e1f2015-11-04 15:55:39 -08003503 }
Daniel Sandler96fd7c12012-03-30 16:37:36 -04003504 return button;
3505 }
3506
Joe Onoratocb109a02011-01-18 17:57:41 -08003507 /**
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01003508 * @return Whether we are currently building a notification from a legacy (an app that
Alan Viverette3cb07a462014-06-06 14:19:53 -07003509 * doesn't create material notifications by itself) app.
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01003510 */
3511 private boolean isLegacy() {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003512 return getColorUtil() != null;
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01003513 }
3514
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01003515 private CharSequence processLegacyText(CharSequence charSequence) {
3516 if (isLegacy()) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003517 return getColorUtil().invertCharSequenceColors(charSequence);
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01003518 } else {
3519 return charSequence;
3520 }
3521 }
3522
Dan Sandler26e81cf2014-05-06 10:01:27 -04003523 /**
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003524 * Apply any necessariy colors to the small icon
Dan Sandler26e81cf2014-05-06 10:01:27 -04003525 */
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003526 private void processSmallIconColor(Icon smallIcon, RemoteViews contentView) {
Selim Cinekea4bef72015-12-02 15:51:10 -08003527 boolean colorable = !isLegacy() || getColorUtil().isGrayscaleIcon(mContext, smallIcon);
3528 if (colorable) {
Adrian Roos4ff3b122016-02-01 12:26:13 -08003529 contentView.setDrawableParameters(R.id.icon, false, -1, resolveContrastColor(),
Jorim Jaggi92df1f22014-12-16 19:44:41 +01003530 PorterDuff.Mode.SRC_ATOP, -1);
Selim Cinekea4bef72015-12-02 15:51:10 -08003531
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01003532 }
Selim Cinekea4bef72015-12-02 15:51:10 -08003533 contentView.setInt(R.id.notification_header, "setOriginalIconColor",
Adrian Roos4ff3b122016-02-01 12:26:13 -08003534 colorable ? resolveContrastColor() : NotificationHeaderView.NO_COLOR);
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01003535 }
3536
Dan Sandler26e81cf2014-05-06 10:01:27 -04003537 /**
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003538 * Make the largeIcon dark if it's a fake smallIcon (that is,
Dan Sandler26e81cf2014-05-06 10:01:27 -04003539 * if it's grayscale).
3540 */
3541 // TODO: also check bounds, transparency, that sort of thing.
Dan Sandlerd63f9322015-05-06 15:18:49 -04003542 private void processLargeLegacyIcon(Icon largeIcon, RemoteViews contentView) {
3543 if (largeIcon != null && isLegacy()
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003544 && getColorUtil().isGrayscaleIcon(mContext, largeIcon)) {
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003545 // resolve color will fall back to the default when legacy
Adrian Roos4ff3b122016-02-01 12:26:13 -08003546 contentView.setDrawableParameters(R.id.icon, false, -1, resolveContrastColor(),
Dan Sandler190d58d2014-05-15 09:33:39 -04003547 PorterDuff.Mode.SRC_ATOP, -1);
Jorim Jaggi92df1f22014-12-16 19:44:41 +01003548 }
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01003549 }
3550
Julia Reynolds10ee1fc2015-11-09 11:04:55 -05003551 private void sanitizeColor() {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003552 if (mN.color != COLOR_DEFAULT) {
3553 mN.color |= 0xFF000000; // no alpha for custom colors
Jorim Jaggi74419312014-06-10 20:57:21 +02003554 }
Jorim Jaggi74419312014-06-10 20:57:21 +02003555 }
3556
Adrian Roos4ff3b122016-02-01 12:26:13 -08003557 int resolveContrastColor() {
3558 if (mCachedContrastColorIsFor == mN.color && mCachedContrastColor != COLOR_INVALID) {
3559 return mCachedContrastColor;
Dan Sandler26e81cf2014-05-06 10:01:27 -04003560 }
Adrian Roos4ff3b122016-02-01 12:26:13 -08003561 final int contrasted = NotificationColorUtil.resolveContrastColor(mContext, mN.color);
3562
3563 mCachedContrastColorIsFor = mN.color;
3564 return mCachedContrastColor = contrasted;
Dan Sandler26e81cf2014-05-06 10:01:27 -04003565 }
3566
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01003567 /**
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003568 * Apply the unstyled operations and return a new {@link Notification} object.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04003569 * @hide
Joe Onoratocb109a02011-01-18 17:57:41 -08003570 */
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04003571 public Notification buildUnstyled() {
Daniel Sandlera0a938c2012-03-15 08:42:37 -04003572 if (mActions.size() > 0) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003573 mN.actions = new Action[mActions.size()];
3574 mActions.toArray(mN.actions);
Daniel Sandlera0a938c2012-03-15 08:42:37 -04003575 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003576 if (!mPersonList.isEmpty()) {
3577 mN.extras.putStringArray(EXTRA_PEOPLE,
3578 mPersonList.toArray(new String[mPersonList.size()]));
Dan Sandler0bf2ed82013-12-21 23:33:41 -06003579 }
Selim Cinek247fa012016-02-18 09:50:48 -08003580 if (mN.bigContentView != null || mN.contentView != null
3581 || mN.headsUpContentView != null) {
3582 mN.extras.putBoolean(EXTRA_CONTAINS_CUSTOM_VIEW, true);
3583 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003584 return mN;
Joe Onorato46439ce2010-11-19 13:56:21 -08003585 }
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003586
Julia Reynolds3b848122016-02-26 10:45:32 -05003587 /**
3588 * Creates a Builder from an existing notification so further changes can be made.
3589 * @param context The context for your application / activity.
3590 * @param n The notification to create a Builder from.
3591 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003592 public static Notification.Builder recoverBuilder(Context context, Notification n) {
Christoph Studer4600f9b2014-07-22 22:44:43 +02003593 // Re-create notification context so we can access app resources.
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003594 ApplicationInfo applicationInfo = n.extras.getParcelable(
3595 EXTRA_BUILDER_APPLICATION_INFO);
Christoph Studer4600f9b2014-07-22 22:44:43 +02003596 Context builderContext;
Julia Reynoldsda303542015-11-23 14:00:20 -05003597 if (applicationInfo != null) {
3598 try {
3599 builderContext = context.createApplicationContext(applicationInfo,
3600 Context.CONTEXT_RESTRICTED);
3601 } catch (NameNotFoundException e) {
3602 Log.e(TAG, "ApplicationInfo " + applicationInfo + " not found");
3603 builderContext = context; // try with our context
3604 }
3605 } else {
3606 builderContext = context; // try with given context
Christoph Studer4600f9b2014-07-22 22:44:43 +02003607 }
3608
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003609 return new Builder(builderContext, n);
Christoph Studer4600f9b2014-07-22 22:44:43 +02003610 }
3611
3612 private static Class<? extends Style> getNotificationStyleClass(String templateClass) {
Selim Cinek593610c2016-02-16 18:42:57 -08003613 Class<? extends Style>[] classes = new Class[] {
3614 BigTextStyle.class, BigPictureStyle.class, InboxStyle.class, MediaStyle.class,
Alex Hillsfc737de2016-03-23 17:33:02 -04003615 DecoratedCustomViewStyle.class, DecoratedMediaCustomViewStyle.class,
3616 MessagingStyle.class };
Christoph Studer4600f9b2014-07-22 22:44:43 +02003617 for (Class<? extends Style> innerClass : classes) {
3618 if (templateClass.equals(innerClass.getName())) {
3619 return innerClass;
3620 }
3621 }
3622 return null;
3623 }
3624
Daniel Sandlerf45564e2013-04-15 15:05:08 -04003625 /**
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003626 * @deprecated Use {@link #build()} instead.
3627 */
3628 @Deprecated
3629 public Notification getNotification() {
3630 return build();
3631 }
3632
3633 /**
3634 * Combine all of the options that have been set and return a new {@link Notification}
3635 * object.
3636 */
3637 public Notification build() {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003638 // first, add any extras from the calling code
3639 if (mUserExtras != null) {
3640 mN.extras = getAllExtras();
Jorim Jaggia0d58ae2015-06-03 11:48:13 -07003641 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003642
3643 // lazy stuff from mContext; see comment in Builder(Context, Notification)
Julia Reynoldsda303542015-11-23 14:00:20 -05003644 Notification.addFieldsFromContext(mContext, mN);
Christoph Studer943aa672014-08-03 20:31:16 +02003645
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003646 buildUnstyled();
Daniel Sandlerf45564e2013-04-15 15:05:08 -04003647
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003648 if (mStyle != null) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003649 mStyle.buildStyled(mN);
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003650 }
Daniel Sandlerf45564e2013-04-15 15:05:08 -04003651
Adrian Roos5081c0d2016-02-26 16:04:19 -08003652 if (mContext.getApplicationInfo().targetSdkVersion < Build.VERSION_CODES.N
3653 && (mStyle == null || !mStyle.displayCustomViewInline())) {
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05003654 if (mN.contentView == null) {
Julia Reynolds3b848122016-02-26 10:45:32 -05003655 mN.contentView = createContentView();
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05003656 mN.extras.putInt(EXTRA_REBUILD_CONTENT_VIEW_ACTION_COUNT,
3657 mN.contentView.getSequenceNumber());
3658 }
3659 if (mN.bigContentView == null) {
Julia Reynolds3b848122016-02-26 10:45:32 -05003660 mN.bigContentView = createBigContentView();
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05003661 if (mN.bigContentView != null) {
3662 mN.extras.putInt(EXTRA_REBUILD_BIG_CONTENT_VIEW_ACTION_COUNT,
3663 mN.bigContentView.getSequenceNumber());
3664 }
3665 }
3666 if (mN.headsUpContentView == null) {
Julia Reynolds3b848122016-02-26 10:45:32 -05003667 mN.headsUpContentView = createHeadsUpContentView();
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05003668 if (mN.headsUpContentView != null) {
3669 mN.extras.putInt(EXTRA_REBUILD_HEADS_UP_CONTENT_VIEW_ACTION_COUNT,
3670 mN.headsUpContentView.getSequenceNumber());
3671 }
3672 }
3673 }
3674
Julia Reynolds4c0c2022016-02-02 15:11:59 -05003675 if ((mN.defaults & DEFAULT_LIGHTS) != 0) {
3676 mN.flags |= FLAG_SHOW_LIGHTS;
3677 }
3678
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003679 return mN;
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003680 }
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05003681
3682 /**
3683 * Apply this Builder to an existing {@link Notification} object.
3684 *
3685 * @hide
3686 */
3687 public Notification buildInto(Notification n) {
Daniel Sandler1a497d32013-04-18 14:52:45 -04003688 build().cloneInto(n, true);
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05003689 return n;
3690 }
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01003691
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05003692 /**
Adrian Roos184bfe022016-03-03 13:41:44 -08003693 * Removes RemoteViews that were created for compatibility from {@param n}, if they did not
3694 * change.
3695 *
3696 * @return {@param n}, if no stripping is needed, otherwise a stripped clone of {@param n}.
3697 *
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05003698 * @hide
3699 */
Adrian Roos184bfe022016-03-03 13:41:44 -08003700 public static Notification maybeCloneStrippedForDelivery(Notification n) {
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05003701 String templateClass = n.extras.getString(EXTRA_TEMPLATE);
Adrian Roos184bfe022016-03-03 13:41:44 -08003702
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05003703 // Only strip views for known Styles because we won't know how to
3704 // re-create them otherwise.
Adrian Roos184bfe022016-03-03 13:41:44 -08003705 if (!TextUtils.isEmpty(templateClass)
3706 && getNotificationStyleClass(templateClass) == null) {
3707 return n;
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05003708 }
Adrian Roos184bfe022016-03-03 13:41:44 -08003709
3710 // Only strip unmodified BuilderRemoteViews.
3711 boolean stripContentView = n.contentView instanceof BuilderRemoteViews &&
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05003712 n.extras.getInt(EXTRA_REBUILD_CONTENT_VIEW_ACTION_COUNT, -1) ==
Adrian Roos184bfe022016-03-03 13:41:44 -08003713 n.contentView.getSequenceNumber();
3714 boolean stripBigContentView = n.bigContentView instanceof BuilderRemoteViews &&
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05003715 n.extras.getInt(EXTRA_REBUILD_BIG_CONTENT_VIEW_ACTION_COUNT, -1) ==
Adrian Roos184bfe022016-03-03 13:41:44 -08003716 n.bigContentView.getSequenceNumber();
3717 boolean stripHeadsUpContentView = n.headsUpContentView instanceof BuilderRemoteViews &&
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05003718 n.extras.getInt(EXTRA_REBUILD_HEADS_UP_CONTENT_VIEW_ACTION_COUNT, -1) ==
Adrian Roos184bfe022016-03-03 13:41:44 -08003719 n.headsUpContentView.getSequenceNumber();
3720
3721 // Nothing to do here, no need to clone.
3722 if (!stripContentView && !stripBigContentView && !stripHeadsUpContentView) {
3723 return n;
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05003724 }
Adrian Roos184bfe022016-03-03 13:41:44 -08003725
3726 Notification clone = n.clone();
3727 if (stripContentView) {
3728 clone.contentView = null;
3729 clone.extras.remove(EXTRA_REBUILD_CONTENT_VIEW_ACTION_COUNT);
3730 }
3731 if (stripBigContentView) {
3732 clone.bigContentView = null;
3733 clone.extras.remove(EXTRA_REBUILD_BIG_CONTENT_VIEW_ACTION_COUNT);
3734 }
3735 if (stripHeadsUpContentView) {
3736 clone.headsUpContentView = null;
3737 clone.extras.remove(EXTRA_REBUILD_HEADS_UP_CONTENT_VIEW_ACTION_COUNT);
3738 }
3739 return clone;
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05003740 }
3741
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01003742 private int getBaseLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07003743 return R.layout.notification_template_material_base;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01003744 }
3745
3746 private int getBigBaseLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07003747 return R.layout.notification_template_material_big_base;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01003748 }
3749
3750 private int getBigPictureLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07003751 return R.layout.notification_template_material_big_picture;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01003752 }
3753
3754 private int getBigTextLayoutResource() {
Jorim Jaggi445d3c02014-08-19 22:33:42 +02003755 return R.layout.notification_template_material_big_text;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01003756 }
3757
3758 private int getInboxLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07003759 return R.layout.notification_template_material_inbox;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01003760 }
3761
3762 private int getActionLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07003763 return R.layout.notification_material_action;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01003764 }
3765
3766 private int getActionTombstoneLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07003767 return R.layout.notification_material_action_tombstone;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01003768 }
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003769 }
3770
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003771 /**
3772 * An object that can apply a rich notification style to a {@link Notification.Builder}
3773 * object.
3774 */
Griff Hazendfcb0802014-02-11 12:00:00 -08003775 public static abstract class Style {
Chris Wrend6297db2012-05-03 16:20:13 -04003776 private CharSequence mBigContentTitle;
Jorim Jaggi457a10d2014-09-08 16:18:23 +02003777
3778 /**
3779 * @hide
3780 */
3781 protected CharSequence mSummaryText = null;
3782
3783 /**
3784 * @hide
3785 */
3786 protected boolean mSummaryTextSet = false;
Chris Wrend6297db2012-05-03 16:20:13 -04003787
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003788 protected Builder mBuilder;
3789
Chris Wrend6297db2012-05-03 16:20:13 -04003790 /**
3791 * Overrides ContentTitle in the big form of the template.
3792 * This defaults to the value passed to setContentTitle().
3793 */
3794 protected void internalSetBigContentTitle(CharSequence title) {
3795 mBigContentTitle = title;
3796 }
3797
3798 /**
3799 * Set the first line of text after the detail section in the big form of the template.
3800 */
3801 protected void internalSetSummaryText(CharSequence cs) {
3802 mSummaryText = cs;
Daniel Sandler619738c2012-06-07 16:33:08 -04003803 mSummaryTextSet = true;
Chris Wrend6297db2012-05-03 16:20:13 -04003804 }
3805
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003806 public void setBuilder(Builder builder) {
3807 if (mBuilder != builder) {
3808 mBuilder = builder;
Daniel Sandlerc08dea22012-06-28 08:35:24 -07003809 if (mBuilder != null) {
3810 mBuilder.setStyle(this);
3811 }
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003812 }
3813 }
3814
Chris Wrend6297db2012-05-03 16:20:13 -04003815 protected void checkBuilder() {
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003816 if (mBuilder == null) {
3817 throw new IllegalArgumentException("Style requires a valid Builder object");
3818 }
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003819 }
Chris Wrend6297db2012-05-03 16:20:13 -04003820
3821 protected RemoteViews getStandardView(int layoutId) {
3822 checkBuilder();
3823
Christoph Studer4600f9b2014-07-22 22:44:43 +02003824 // Nasty.
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003825 CharSequence oldBuilderContentTitle =
3826 mBuilder.getAllExtras().getCharSequence(EXTRA_TITLE);
Chris Wrend6297db2012-05-03 16:20:13 -04003827 if (mBigContentTitle != null) {
3828 mBuilder.setContentTitle(mBigContentTitle);
3829 }
3830
Chris Wrend6297db2012-05-03 16:20:13 -04003831 RemoteViews contentView = mBuilder.applyStandardTemplateWithActions(layoutId);
3832
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003833 mBuilder.getAllExtras().putCharSequence(EXTRA_TITLE, oldBuilderContentTitle);
Christoph Studer4600f9b2014-07-22 22:44:43 +02003834
Chris Wrend6297db2012-05-03 16:20:13 -04003835 if (mBigContentTitle != null && mBigContentTitle.equals("")) {
3836 contentView.setViewVisibility(R.id.line1, View.GONE);
Chris Wren67dc9a02012-05-16 01:03:20 -04003837 } else {
3838 contentView.setViewVisibility(R.id.line1, View.VISIBLE);
Chris Wrend6297db2012-05-03 16:20:13 -04003839 }
3840
Chris Wrend6297db2012-05-03 16:20:13 -04003841 return contentView;
3842 }
3843
Daniel Sandlerf45564e2013-04-15 15:05:08 -04003844 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003845 * Construct a Style-specific RemoteViews for the final 1U notification layout.
3846 * The default implementation has nothing additional to add.
3847 * @hide
3848 */
3849 public RemoteViews makeContentView() {
3850 return null;
3851 }
3852
3853 /**
3854 * Construct a Style-specific RemoteViews for the final big notification layout.
3855 * @hide
3856 */
3857 public RemoteViews makeBigContentView() {
3858 return null;
3859 }
3860
3861 /**
3862 * Construct a Style-specific RemoteViews for the final HUN layout.
3863 * @hide
3864 */
3865 public RemoteViews makeHeadsUpContentView() {
3866 return null;
3867 }
3868
3869 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003870 * Apply any style-specific extras to this notification before shipping it out.
Daniel Sandlerf45564e2013-04-15 15:05:08 -04003871 * @hide
3872 */
3873 public void addExtras(Bundle extras) {
3874 if (mSummaryTextSet) {
3875 extras.putCharSequence(EXTRA_SUMMARY_TEXT, mSummaryText);
3876 }
3877 if (mBigContentTitle != null) {
3878 extras.putCharSequence(EXTRA_TITLE_BIG, mBigContentTitle);
3879 }
Chris Wren91ad5632013-06-05 15:05:57 -04003880 extras.putString(EXTRA_TEMPLATE, this.getClass().getName());
Daniel Sandlerf45564e2013-04-15 15:05:08 -04003881 }
3882
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04003883 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003884 * Reconstruct the internal state of this Style object from extras.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04003885 * @hide
3886 */
Christoph Studer4600f9b2014-07-22 22:44:43 +02003887 protected void restoreFromExtras(Bundle extras) {
3888 if (extras.containsKey(EXTRA_SUMMARY_TEXT)) {
3889 mSummaryText = extras.getCharSequence(EXTRA_SUMMARY_TEXT);
3890 mSummaryTextSet = true;
3891 }
3892 if (extras.containsKey(EXTRA_TITLE_BIG)) {
3893 mBigContentTitle = extras.getCharSequence(EXTRA_TITLE_BIG);
3894 }
3895 }
3896
3897
3898 /**
3899 * @hide
3900 */
3901 public Notification buildStyled(Notification wip) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003902 addExtras(wip.extras);
Christoph Studer4600f9b2014-07-22 22:44:43 +02003903 return wip;
3904 }
3905
Daniel Sandler0ec46202015-06-24 01:27:05 -04003906 /**
3907 * @hide
3908 */
Jorim Jaggia0d58ae2015-06-03 11:48:13 -07003909 public void purgeResources() {}
3910
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04003911 /**
3912 * Calls {@link android.app.Notification.Builder#build()} on the Builder this Style is
3913 * attached to.
3914 *
3915 * @return the fully constructed Notification.
3916 */
3917 public Notification build() {
3918 checkBuilder();
3919 return mBuilder.build();
3920 }
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02003921
3922 /**
3923 * @hide
3924 * @return true if the style positions the progress bar on the second line; false if the
3925 * style hides the progress bar
3926 */
3927 protected boolean hasProgress() {
3928 return true;
3929 }
Selim Cinek03d0d652015-11-13 13:18:09 -05003930
3931 /**
3932 * @hide
3933 * @return Whether we should put the summary be put into the notification header
3934 */
3935 public boolean hasSummaryInHeader() {
3936 return true;
3937 }
Selim Cinek593610c2016-02-16 18:42:57 -08003938
3939 /**
3940 * @hide
3941 * @return Whether custom content views are displayed inline in the style
3942 */
3943 public boolean displayCustomViewInline() {
3944 return false;
3945 }
Joe Onorato46439ce2010-11-19 13:56:21 -08003946 }
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003947
3948 /**
Daniel Sandler4dfbe832012-04-11 14:51:46 -04003949 * Helper class for generating large-format notifications that include a large image attachment.
Joe Malin8d40d042012-11-05 11:36:40 -08003950 *
Robert Ly91c5ce32014-06-08 15:37:00 -07003951 * Here's how you'd set the <code>BigPictureStyle</code> on a notification:
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003952 * <pre class="prettyprint">
Robert Ly91c5ce32014-06-08 15:37:00 -07003953 * Notification notif = new Notification.Builder(mContext)
3954 * .setContentTitle(&quot;New photo from &quot; + sender.toString())
3955 * .setContentText(subject)
3956 * .setSmallIcon(R.drawable.new_post)
3957 * .setLargeIcon(aBitmap)
3958 * .setStyle(new Notification.BigPictureStyle()
3959 * .bigPicture(aBigBitmap))
3960 * .build();
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003961 * </pre>
Joe Malin8d40d042012-11-05 11:36:40 -08003962 *
Daniel Sandler4dfbe832012-04-11 14:51:46 -04003963 * @see Notification#bigContentView
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003964 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003965 public static class BigPictureStyle extends Style {
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003966 private Bitmap mPicture;
Dan Sandlerd63f9322015-05-06 15:18:49 -04003967 private Icon mBigLargeIcon;
Chris Wren3745a3d2012-05-22 15:11:52 -04003968 private boolean mBigLargeIconSet = false;
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003969
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003970 public BigPictureStyle() {
3971 }
3972
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003973 public BigPictureStyle(Builder builder) {
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003974 setBuilder(builder);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003975 }
3976
Chris Wrend6297db2012-05-03 16:20:13 -04003977 /**
3978 * Overrides ContentTitle in the big form of the template.
3979 * This defaults to the value passed to setContentTitle().
3980 */
3981 public BigPictureStyle setBigContentTitle(CharSequence title) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04003982 internalSetBigContentTitle(safeCharSequence(title));
Chris Wrend6297db2012-05-03 16:20:13 -04003983 return this;
3984 }
3985
3986 /**
3987 * Set the first line of text after the detail section in the big form of the template.
3988 */
3989 public BigPictureStyle setSummaryText(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04003990 internalSetSummaryText(safeCharSequence(cs));
Chris Wrend6297db2012-05-03 16:20:13 -04003991 return this;
3992 }
3993
Chris Wren0bd664d2012-08-01 13:56:56 -04003994 /**
3995 * Provide the bitmap to be used as the payload for the BigPicture notification.
3996 */
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003997 public BigPictureStyle bigPicture(Bitmap b) {
3998 mPicture = b;
3999 return this;
4000 }
4001
Chris Wren3745a3d2012-05-22 15:11:52 -04004002 /**
Chris Wren3745a3d2012-05-22 15:11:52 -04004003 * Override the large icon when the big notification is shown.
4004 */
4005 public BigPictureStyle bigLargeIcon(Bitmap b) {
Dan Sandlerd63f9322015-05-06 15:18:49 -04004006 return bigLargeIcon(b != null ? Icon.createWithBitmap(b) : null);
4007 }
4008
4009 /**
4010 * Override the large icon when the big notification is shown.
4011 */
4012 public BigPictureStyle bigLargeIcon(Icon icon) {
Chris Wren3745a3d2012-05-22 15:11:52 -04004013 mBigLargeIconSet = true;
Dan Sandlerd63f9322015-05-06 15:18:49 -04004014 mBigLargeIcon = icon;
Chris Wren3745a3d2012-05-22 15:11:52 -04004015 return this;
4016 }
4017
Riley Andrews0394a0c2015-11-03 23:36:52 -08004018 /** @hide */
4019 public static final int MIN_ASHMEM_BITMAP_SIZE = 128 * (1 << 10);
4020
Daniel Sandler0ec46202015-06-24 01:27:05 -04004021 /**
4022 * @hide
4023 */
Jorim Jaggia0d58ae2015-06-03 11:48:13 -07004024 @Override
4025 public void purgeResources() {
4026 super.purgeResources();
Riley Andrews8cee7c12015-11-01 23:36:04 -08004027 if (mPicture != null &&
4028 mPicture.isMutable() &&
Riley Andrews0394a0c2015-11-03 23:36:52 -08004029 mPicture.getAllocationByteCount() >= MIN_ASHMEM_BITMAP_SIZE) {
Jorim Jaggia0d58ae2015-06-03 11:48:13 -07004030 mPicture = mPicture.createAshmemBitmap();
4031 }
4032 if (mBigLargeIcon != null) {
4033 mBigLargeIcon.convertToAshmem();
4034 }
4035 }
Christoph Studer5c510ee2014-12-15 16:32:27 +01004036
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004037 /**
4038 * @hide
4039 */
4040 public RemoteViews makeBigContentView() {
4041 // Replace mN.mLargeIcon with mBigLargeIcon if mBigLargeIconSet
Christoph Studer5c510ee2014-12-15 16:32:27 +01004042 // This covers the following cases:
4043 // 1. mBigLargeIconSet -> mBigLargeIcon (null or non-null) applies, overrides
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004044 // mN.mLargeIcon
4045 // 2. !mBigLargeIconSet -> mN.mLargeIcon applies
Dan Sandlerd63f9322015-05-06 15:18:49 -04004046 Icon oldLargeIcon = null;
Christoph Studer5c510ee2014-12-15 16:32:27 +01004047 if (mBigLargeIconSet) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004048 oldLargeIcon = mBuilder.mN.mLargeIcon;
4049 mBuilder.mN.mLargeIcon = mBigLargeIcon;
Christoph Studer5c510ee2014-12-15 16:32:27 +01004050 }
4051
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01004052 RemoteViews contentView = getStandardView(mBuilder.getBigPictureLayoutResource());
Selim Cinek03d0d652015-11-13 13:18:09 -05004053 if (mSummaryTextSet) {
4054 contentView.setTextViewText(R.id.text, mBuilder.processLegacyText(mSummaryText));
Selim Cinekc848c3a2016-01-13 15:27:30 -08004055 contentView.setViewVisibility(R.id.text, View.VISIBLE);
Selim Cinek03d0d652015-11-13 13:18:09 -05004056 }
Selim Cinek860b6da2015-12-16 19:02:19 -08004057 mBuilder.setContentMinHeight(contentView, mBuilder.mN.mLargeIcon != null);
Selim Cinek53e64a42015-11-16 10:40:56 -08004058
Christoph Studer5c510ee2014-12-15 16:32:27 +01004059 if (mBigLargeIconSet) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004060 mBuilder.mN.mLargeIcon = oldLargeIcon;
Christoph Studer5c510ee2014-12-15 16:32:27 +01004061 }
4062
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004063 contentView.setImageViewBitmap(R.id.big_picture, mPicture);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004064 return contentView;
4065 }
4066
Daniel Sandlerf45564e2013-04-15 15:05:08 -04004067 /**
4068 * @hide
4069 */
4070 public void addExtras(Bundle extras) {
4071 super.addExtras(extras);
4072
4073 if (mBigLargeIconSet) {
4074 extras.putParcelable(EXTRA_LARGE_ICON_BIG, mBigLargeIcon);
4075 }
4076 extras.putParcelable(EXTRA_PICTURE, mPicture);
4077 }
4078
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04004079 /**
4080 * @hide
4081 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004082 @Override
Christoph Studer4600f9b2014-07-22 22:44:43 +02004083 protected void restoreFromExtras(Bundle extras) {
4084 super.restoreFromExtras(extras);
4085
4086 if (extras.containsKey(EXTRA_LARGE_ICON_BIG)) {
Christoph Studer5c510ee2014-12-15 16:32:27 +01004087 mBigLargeIconSet = true;
Christoph Studer4600f9b2014-07-22 22:44:43 +02004088 mBigLargeIcon = extras.getParcelable(EXTRA_LARGE_ICON_BIG);
Chris Wren3745a3d2012-05-22 15:11:52 -04004089 }
Christoph Studer4600f9b2014-07-22 22:44:43 +02004090 mPicture = extras.getParcelable(EXTRA_PICTURE);
4091 }
Selim Cinek03d0d652015-11-13 13:18:09 -05004092
4093 /**
4094 * @hide
4095 */
4096 @Override
4097 public boolean hasSummaryInHeader() {
4098 return false;
4099 }
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004100 }
4101
4102 /**
Daniel Sandler4dfbe832012-04-11 14:51:46 -04004103 * Helper class for generating large-format notifications that include a lot of text.
Joe Malin8d40d042012-11-05 11:36:40 -08004104 *
Robert Ly91c5ce32014-06-08 15:37:00 -07004105 * Here's how you'd set the <code>BigTextStyle</code> on a notification:
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004106 * <pre class="prettyprint">
Robert Ly91c5ce32014-06-08 15:37:00 -07004107 * Notification notif = new Notification.Builder(mContext)
4108 * .setContentTitle(&quot;New mail from &quot; + sender.toString())
4109 * .setContentText(subject)
4110 * .setSmallIcon(R.drawable.new_mail)
4111 * .setLargeIcon(aBitmap)
4112 * .setStyle(new Notification.BigTextStyle()
4113 * .bigText(aVeryLongString))
4114 * .build();
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004115 * </pre>
Joe Malin8d40d042012-11-05 11:36:40 -08004116 *
Daniel Sandler4dfbe832012-04-11 14:51:46 -04004117 * @see Notification#bigContentView
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004118 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004119 public static class BigTextStyle extends Style {
Jorim Jaggi457a10d2014-09-08 16:18:23 +02004120
4121 private static final int MAX_LINES = 13;
Selim Cinek3a2c4b92015-12-17 17:01:17 -08004122 private static final int LINES_CONSUMED_BY_ACTIONS = 4;
Jorim Jaggi457a10d2014-09-08 16:18:23 +02004123
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004124 private CharSequence mBigText;
4125
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004126 public BigTextStyle() {
4127 }
4128
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004129 public BigTextStyle(Builder builder) {
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004130 setBuilder(builder);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004131 }
4132
Chris Wrend6297db2012-05-03 16:20:13 -04004133 /**
4134 * Overrides ContentTitle in the big form of the template.
4135 * This defaults to the value passed to setContentTitle().
4136 */
4137 public BigTextStyle setBigContentTitle(CharSequence title) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04004138 internalSetBigContentTitle(safeCharSequence(title));
Chris Wrend6297db2012-05-03 16:20:13 -04004139 return this;
4140 }
4141
4142 /**
4143 * Set the first line of text after the detail section in the big form of the template.
4144 */
4145 public BigTextStyle setSummaryText(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04004146 internalSetSummaryText(safeCharSequence(cs));
Chris Wrend6297db2012-05-03 16:20:13 -04004147 return this;
4148 }
4149
Chris Wren0bd664d2012-08-01 13:56:56 -04004150 /**
4151 * Provide the longer text to be displayed in the big form of the
4152 * template in place of the content text.
4153 */
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004154 public BigTextStyle bigText(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04004155 mBigText = safeCharSequence(cs);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004156 return this;
4157 }
4158
Daniel Sandlerf45564e2013-04-15 15:05:08 -04004159 /**
4160 * @hide
4161 */
4162 public void addExtras(Bundle extras) {
4163 super.addExtras(extras);
4164
Christoph Studer4600f9b2014-07-22 22:44:43 +02004165 extras.putCharSequence(EXTRA_BIG_TEXT, mBigText);
4166 }
4167
4168 /**
4169 * @hide
4170 */
4171 @Override
4172 protected void restoreFromExtras(Bundle extras) {
4173 super.restoreFromExtras(extras);
4174
4175 mBigText = extras.getCharSequence(EXTRA_BIG_TEXT);
Daniel Sandlerf45564e2013-04-15 15:05:08 -04004176 }
4177
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004178 /**
4179 * @hide
4180 */
4181 public RemoteViews makeBigContentView() {
Christoph Studer4600f9b2014-07-22 22:44:43 +02004182
4183 // Nasty
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004184 CharSequence oldBuilderContentText =
4185 mBuilder.getAllExtras().getCharSequence(EXTRA_TEXT);
4186 mBuilder.getAllExtras().putCharSequence(EXTRA_TEXT, null);
Daniel Sandler916ad912012-06-13 12:17:07 -04004187
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01004188 RemoteViews contentView = getStandardView(mBuilder.getBigTextLayoutResource());
Joe Malin8d40d042012-11-05 11:36:40 -08004189
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004190 mBuilder.getAllExtras().putCharSequence(EXTRA_TEXT, oldBuilderContentText);
Christoph Studer4600f9b2014-07-22 22:44:43 +02004191
Selim Cinek3a2c4b92015-12-17 17:01:17 -08004192 CharSequence bigTextText = mBuilder.processLegacyText(mBigText);
4193 contentView.setTextViewText(R.id.big_text, bigTextText);
4194 contentView.setViewVisibility(R.id.big_text,
4195 TextUtils.isEmpty(bigTextText) ? View.GONE : View.VISIBLE);
Jorim Jaggi457a10d2014-09-08 16:18:23 +02004196 contentView.setInt(R.id.big_text, "setMaxLines", calculateMaxLines());
Selim Cinek4fb12d32015-11-19 18:10:48 -08004197 contentView.setBoolean(R.id.big_text, "setHasImage", mBuilder.mN.mLargeIcon != null);
4198
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004199 return contentView;
4200 }
4201
Jorim Jaggi457a10d2014-09-08 16:18:23 +02004202 private int calculateMaxLines() {
4203 int lineCount = MAX_LINES;
4204 boolean hasActions = mBuilder.mActions.size() > 0;
Jorim Jaggi457a10d2014-09-08 16:18:23 +02004205 if (hasActions) {
4206 lineCount -= LINES_CONSUMED_BY_ACTIONS;
4207 }
Jorim Jaggi457a10d2014-09-08 16:18:23 +02004208 return lineCount;
4209 }
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004210 }
Daniel Sandler879c5e02012-04-17 16:46:51 -04004211
4212 /**
Alex Hillsfc737de2016-03-23 17:33:02 -04004213 * Helper class for generating large-format notifications that include multiple back-and-forth
4214 * messages of varying types between any number of people.
4215 *
4216 * <br>
4217 * If the platform does not provide large-format notifications, this method has no effect. The
4218 * user will always see the normal notification view.
4219 * <br>
4220 * This class is a "rebuilder": It attaches to a Builder object and modifies its behavior, like
4221 * so:
4222 * <pre class="prettyprint">
4223 *
4224 * Notification noti = new Notification.Builder()
4225 * .setContentTitle(&quot;2 new messages wtih &quot; + sender.toString())
4226 * .setContentText(subject)
4227 * .setSmallIcon(R.drawable.new_message)
4228 * .setLargeIcon(aBitmap)
4229 * .setStyle(new Notification.MessagingStyle(resources.getString(R.string.reply_name))
4230 * .addMessage(messages[0].getText(), messages[0].getTime(), messages[0].getSender())
4231 * .addMessage(messages[1].getText(), messages[1].getTime(), messages[1].getSender()))
4232 * .build();
4233 * </pre>
4234 */
4235 public static class MessagingStyle extends Style {
4236
4237 /**
4238 * The maximum number of messages that will be retained in the Notification itself (the
4239 * number displayed is up to the platform).
4240 */
4241 public static final int MAXIMUM_RETAINED_MESSAGES = 25;
4242
4243 CharSequence mUserDisplayName;
4244 CharSequence mConversationTitle;
4245 boolean mAllowGeneratedReplies = true;
4246 ArrayList<Message> mMessages = new ArrayList<>();
4247
4248 MessagingStyle() {
4249 }
4250
4251 /**
4252 * @param userDisplayName the name to be displayed for any replies sent by the user before the
4253 * posting app reposts the notification with those messages after they've been actually
4254 * sent and in previous messages sent by the user added in
4255 * {@link #addMessage(Notification.MessagingStyle.Message)}
4256 */
4257 public MessagingStyle(CharSequence userDisplayName) {
4258 mUserDisplayName = userDisplayName;
4259 }
4260
4261 /**
4262 * Returns the name to be displayed for any replies sent by the user
4263 */
4264 public CharSequence getUserDisplayName() {
4265 return mUserDisplayName;
4266 }
4267
4268 /**
4269 * Set whether the platform should automatically generate possible replies from messages.
4270 * @param allowGeneratedReplies {@code true} to allow generated replies, {@code false}
4271 * otherwise
4272 * @return this object for method chaining
4273 * The default value is {@code true}
4274 */
4275 public MessagingStyle setAllowGeneratedReplies(boolean allowGeneratedReplies) {
4276 mAllowGeneratedReplies = allowGeneratedReplies;
4277 return this;
4278 }
4279
4280 /**
4281 * Return whether the platform should automatically generate possible replies from messages.
4282 */
4283 public boolean getAllowGeneratedReplies() {
4284 return mAllowGeneratedReplies;
4285 }
4286
4287 /**
4288 * Sets the title to be displayed on this conversation. This should only be used for
4289 * group messaging and left unset for one-on-one conversations.
4290 * @param conversationTitle
4291 * @return this object for method chaining.
4292 */
4293 public MessagingStyle setConversationTitle(CharSequence conversationTitle) {
4294 mConversationTitle = conversationTitle;
4295 return this;
4296 }
4297
4298 /**
4299 * Return the title to be displayed on this conversation. Can be <code>null</code> and
4300 * should be for one-on-one conversations
4301 */
4302 public CharSequence getConversationTitle() {
4303 return mConversationTitle;
4304 }
4305
4306 /**
4307 * Adds a message for display by this notification. Convenience call for a simple
4308 * {@link Message} in {@link #addMessage(Notification.MessagingStyle.Message)}.
4309 * @param text A {@link CharSequence} to be displayed as the message content
4310 * @param timestamp Time at which the message arrived
4311 * @param sender A {@link CharSequence} to be used for displaying the name of the
4312 * sender. Should be <code>null</code> for messages by the current user, in which case
4313 * the platform will insert {@link #getUserDisplayName()}.
4314 * Should be unique amongst all individuals in the conversation, and should be
4315 * consistent during re-posts of the notification.
4316 *
4317 * @see Message#Message(CharSequence, long, CharSequence)
4318 *
4319 * @return this object for method chaining
4320 */
4321 public MessagingStyle addMessage(CharSequence text, long timestamp, CharSequence sender) {
4322 mMessages.add(new Message(text, timestamp, sender));
4323 if (mMessages.size() > MAXIMUM_RETAINED_MESSAGES) {
4324 mMessages.remove(0);
4325 }
4326 return this;
4327 }
4328
4329 /**
4330 * Adds a {@link Message} for display in this notification.
4331 * @param message The {@link Message} to be displayed
4332 * @return this object for method chaining
4333 */
4334 public MessagingStyle addMessage(Message message) {
4335 mMessages.add(message);
4336 if (mMessages.size() > MAXIMUM_RETAINED_MESSAGES) {
4337 mMessages.remove(0);
4338 }
4339 return this;
4340 }
4341
4342 /**
4343 * Gets the list of {@code Message} objects that represent the notification
4344 */
4345 public List<Message> getMessages() {
4346 return mMessages;
4347 }
4348
4349 /**
4350 * @hide
4351 */
4352 @Override
4353 public void addExtras(Bundle extras) {
4354 super.addExtras(extras);
4355 if (mUserDisplayName != null) {
4356 extras.putCharSequence(EXTRA_SELF_DISPLAY_NAME, mUserDisplayName);
4357 }
4358 if (mConversationTitle != null) {
4359 extras.putCharSequence(EXTRA_THREAD_TITLE, mConversationTitle);
4360 }
4361 extras.putBoolean(EXTRA_ALLOW_GENERATED_REPLIES, mAllowGeneratedReplies);
4362 if (!mMessages.isEmpty()) {
4363 extras.putParcelableArrayList(EXTRA_MESSAGES, mMessages);
4364 }
4365 }
4366
4367 /**
4368 * @hide
4369 */
4370 @Override
4371 protected void restoreFromExtras(Bundle extras) {
4372 super.restoreFromExtras(extras);
4373
4374 mMessages.clear();
4375 mUserDisplayName = extras.getString(EXTRA_SELF_DISPLAY_NAME);
4376 mConversationTitle = extras.getString(EXTRA_THREAD_TITLE);
4377 mAllowGeneratedReplies = extras.getBoolean(EXTRA_ALLOW_GENERATED_REPLIES,
4378 mAllowGeneratedReplies);
4379 List<Message> messages = extras.getParcelableArrayList(EXTRA_MESSAGES);
4380 if (messages != null) {
4381 mMessages.addAll(messages);
4382 }
4383 }
4384
4385 /**
4386 * @hide
4387 */
4388 public RemoteViews makeBigContentView() {
4389 // TODO handset to write implementation
4390 RemoteViews contentView = getStandardView(mBuilder.getBigTextLayoutResource());
4391
4392 return contentView;
4393 }
4394
4395 public static final class Message implements Parcelable {
4396
4397 private final CharSequence mText;
4398 private final long mTimestamp;
4399 private final CharSequence mSender;
4400
4401 private String mDataMimeType;
4402 private Uri mDataUri;
4403
4404 /**
4405 * Constructor
4406 * @param text A {@link CharSequence} to be displayed as the message content
4407 * @param timestamp Time at which the message arrived
4408 * @param sender A {@link CharSequence} to be used for displaying the name of the
4409 * sender. Should be <code>null</code> for messages by the current user, in which case
4410 * the platform will insert {@link MessagingStyle#getUserDisplayName()}.
4411 * Should be unique amongst all individuals in the conversation, and should be
4412 * consistent during re-posts of the notification.
4413 */
4414 public Message(CharSequence text, long timestamp, CharSequence sender){
4415 mText = text;
4416 mTimestamp = timestamp;
4417 mSender = sender;
4418 }
4419
4420 /**
4421 * Sets a binary blob of data and an associated MIME type for a message. In the case
4422 * where the platform doesn't support the MIME type, the original text provided in the
4423 * constructor will be used.
4424 * @param dataMimeType The MIME type of the content. See
4425 * <a href="{@docRoot}notifications/messaging.html"> for the list of supported MIME
4426 * types on Android and Android Wear.
4427 * @param dataUri The uri containing the content whose type is given by the MIME type.
4428 * <p class="note">
4429 * <ol>
4430 * <li>Notification Listeners including the System UI need permission to access the
4431 * data the Uri points to. The recommended ways to do this are:</li>
4432 * <li>Store the data in your own ContentProvider, making sure that other apps have
4433 * the correct permission to access your provider. The preferred mechanism for
4434 * providing access is to use per-URI permissions which are temporary and only
4435 * grant access to the receiving application. An easy way to create a
4436 * ContentProvider like this is to use the FileProvider helper class.</li>
4437 * <li>Use the system MediaStore. The MediaStore is primarily aimed at video, audio
4438 * and image MIME types, however beginning with Android 3.0 (API level 11) it can
4439 * also store non-media types (see MediaStore.Files for more info). Files can be
4440 * inserted into the MediaStore using scanFile() after which a content:// style
4441 * Uri suitable for sharing is passed to the provided onScanCompleted() callback.
4442 * Note that once added to the system MediaStore the content is accessible to any
4443 * app on the device.</li>
4444 * </ol>
4445 * @return this object for method chaining
4446 */
4447 public Message setData(String dataMimeType, Uri dataUri) {
4448 mDataMimeType = dataMimeType;
4449 mDataUri = dataUri;
4450 return this;
4451 }
4452
4453 private Message(Parcel in) {
4454 if (in.readInt() != 0) {
4455 mText = TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(in);
4456 } else {
4457 mText = null;
4458 }
4459 mTimestamp = in.readLong();
4460 if (in.readInt() != 0) {
4461 mSender = TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(in);
4462 } else {
4463 mSender = null;
4464 }
4465 if (in.readInt() != 0) {
4466 mDataMimeType = in.readString();
4467 }
4468 if (in.readInt() != 0) {
4469 mDataUri = in.readParcelable(Uri.class.getClassLoader());
4470 }
4471 }
4472
4473 /**
4474 * Get the text to be used for this message, or the fallback text if a type and content
4475 * Uri have been set
4476 */
4477 public CharSequence getText() {
4478 return mText;
4479 }
4480
4481 /**
4482 * Get the time at which this message arrived
4483 */
4484 public long getTimestamp() {
4485 return mTimestamp;
4486 }
4487
4488 /**
4489 * Get the text used to display the contact's name in the messaging experience
4490 */
4491 public CharSequence getSender() {
4492 return mSender;
4493 }
4494
4495 /**
4496 * Get the MIME type of the data pointed to by the Uri
4497 */
4498 public String getDataMimeType() {
4499 return mDataMimeType;
4500 }
4501
4502 /**
4503 * Get the the Uri pointing to the content of the message. Can be null, in which case
4504 * {@see #getText()} is used.
4505 */
4506 public Uri getDataUri() {
4507 return mDataUri;
4508 }
4509
4510 @Override
4511 public int describeContents() {
4512 return 0;
4513 }
4514
4515 @Override
4516 public void writeToParcel(Parcel out, int flags) {
4517 if (mText != null) {
4518 out.writeInt(1);
4519 TextUtils.writeToParcel(mText, out, flags);
4520 } else {
4521 out.writeInt(0);
4522 }
4523 out.writeLong(mTimestamp);
4524 if (mSender != null) {
4525 out.writeInt(1);
4526 TextUtils.writeToParcel(mSender, out, flags);
4527 } else {
4528 out.writeInt(0);
4529 }
4530 if (mDataMimeType != null) {
4531 out.writeInt(1);
4532 out.writeString(mDataMimeType);
4533 } else {
4534 out.writeInt(0);
4535 }
4536 if (mDataUri != null) {
4537 out.writeInt(1);
4538 out.writeParcelable(mDataUri, flags);
4539 } else {
4540 out.writeInt(0);
4541 }
4542 }
4543
4544 public static final Parcelable.Creator<Message> CREATOR =
4545 new Parcelable.Creator<Message>() {
4546 public Message createFromParcel(Parcel in) {
4547 return new Message(in);
4548 }
4549 public Message[] newArray(int size) {
4550 return new Message[size];
4551 }
4552 };
4553 }
4554 }
4555
4556 /**
Daniel Sandler879c5e02012-04-17 16:46:51 -04004557 * Helper class for generating large-format notifications that include a list of (up to 5) strings.
Joe Malin8d40d042012-11-05 11:36:40 -08004558 *
Robert Ly91c5ce32014-06-08 15:37:00 -07004559 * Here's how you'd set the <code>InboxStyle</code> on a notification:
Daniel Sandler879c5e02012-04-17 16:46:51 -04004560 * <pre class="prettyprint">
Robert Ly91c5ce32014-06-08 15:37:00 -07004561 * Notification notif = new Notification.Builder(mContext)
4562 * .setContentTitle(&quot;5 New mails from &quot; + sender.toString())
4563 * .setContentText(subject)
4564 * .setSmallIcon(R.drawable.new_mail)
4565 * .setLargeIcon(aBitmap)
4566 * .setStyle(new Notification.InboxStyle()
4567 * .addLine(str1)
4568 * .addLine(str2)
4569 * .setContentTitle(&quot;&quot;)
4570 * .setSummaryText(&quot;+3 more&quot;))
4571 * .build();
Daniel Sandler879c5e02012-04-17 16:46:51 -04004572 * </pre>
Joe Malin8d40d042012-11-05 11:36:40 -08004573 *
Daniel Sandler879c5e02012-04-17 16:46:51 -04004574 * @see Notification#bigContentView
4575 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004576 public static class InboxStyle extends Style {
Daniel Sandler879c5e02012-04-17 16:46:51 -04004577 private ArrayList<CharSequence> mTexts = new ArrayList<CharSequence>(5);
4578
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004579 public InboxStyle() {
4580 }
4581
Daniel Sandler879c5e02012-04-17 16:46:51 -04004582 public InboxStyle(Builder builder) {
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004583 setBuilder(builder);
Daniel Sandler879c5e02012-04-17 16:46:51 -04004584 }
4585
Chris Wrend6297db2012-05-03 16:20:13 -04004586 /**
4587 * Overrides ContentTitle in the big form of the template.
4588 * This defaults to the value passed to setContentTitle().
4589 */
4590 public InboxStyle setBigContentTitle(CharSequence title) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04004591 internalSetBigContentTitle(safeCharSequence(title));
Chris Wrend6297db2012-05-03 16:20:13 -04004592 return this;
4593 }
4594
4595 /**
4596 * Set the first line of text after the detail section in the big form of the template.
4597 */
4598 public InboxStyle setSummaryText(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04004599 internalSetSummaryText(safeCharSequence(cs));
Chris Wrend6297db2012-05-03 16:20:13 -04004600 return this;
4601 }
4602
Chris Wren0bd664d2012-08-01 13:56:56 -04004603 /**
4604 * Append a line to the digest section of the Inbox notification.
4605 */
Daniel Sandler879c5e02012-04-17 16:46:51 -04004606 public InboxStyle addLine(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04004607 mTexts.add(safeCharSequence(cs));
Daniel Sandler879c5e02012-04-17 16:46:51 -04004608 return this;
4609 }
4610
Daniel Sandlerf45564e2013-04-15 15:05:08 -04004611 /**
4612 * @hide
4613 */
4614 public void addExtras(Bundle extras) {
4615 super.addExtras(extras);
Christoph Studer4600f9b2014-07-22 22:44:43 +02004616
Daniel Sandlerf45564e2013-04-15 15:05:08 -04004617 CharSequence[] a = new CharSequence[mTexts.size()];
4618 extras.putCharSequenceArray(EXTRA_TEXT_LINES, mTexts.toArray(a));
4619 }
4620
Christoph Studer4600f9b2014-07-22 22:44:43 +02004621 /**
4622 * @hide
4623 */
4624 @Override
4625 protected void restoreFromExtras(Bundle extras) {
4626 super.restoreFromExtras(extras);
4627
4628 mTexts.clear();
4629 if (extras.containsKey(EXTRA_TEXT_LINES)) {
4630 Collections.addAll(mTexts, extras.getCharSequenceArray(EXTRA_TEXT_LINES));
4631 }
4632 }
4633
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004634 /**
4635 * @hide
4636 */
4637 public RemoteViews makeBigContentView() {
Selim Cinekc848c3a2016-01-13 15:27:30 -08004638 // Remove the content text so it disappears unless you have a summary
Christoph Studer4600f9b2014-07-22 22:44:43 +02004639 // Nasty
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004640 CharSequence oldBuilderContentText = mBuilder.mN.extras.getCharSequence(EXTRA_TEXT);
4641 mBuilder.getAllExtras().putCharSequence(EXTRA_TEXT, null);
Christoph Studer4600f9b2014-07-22 22:44:43 +02004642
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01004643 RemoteViews contentView = getStandardView(mBuilder.getInboxLayoutResource());
Daniel Sandler619738c2012-06-07 16:33:08 -04004644
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004645 mBuilder.getAllExtras().putCharSequence(EXTRA_TEXT, oldBuilderContentText);
Christoph Studer4600f9b2014-07-22 22:44:43 +02004646
Chris Wrend6297db2012-05-03 16:20:13 -04004647 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 -04004648 R.id.inbox_text4, R.id.inbox_text5, R.id.inbox_text6};
Chris Wrend6297db2012-05-03 16:20:13 -04004649
Chris Wren4ed80d52012-05-17 09:30:03 -04004650 // Make sure all rows are gone in case we reuse a view.
4651 for (int rowId : rowIds) {
4652 contentView.setViewVisibility(rowId, View.GONE);
4653 }
4654
Jorim Jaggi445d3c02014-08-19 22:33:42 +02004655 final boolean largeText =
4656 mBuilder.mContext.getResources().getConfiguration().fontScale > 1f;
4657 final float subTextSize = mBuilder.mContext.getResources().getDimensionPixelSize(
4658 R.dimen.notification_subtext_size);
Daniel Sandler879c5e02012-04-17 16:46:51 -04004659 int i=0;
Selim Cinek9d9fc6e2015-11-12 15:49:14 -05004660 final float density = mBuilder.mContext.getResources().getDisplayMetrics().density;
4661 int topPadding = (int) (5 * density);
Selim Cinekc0fac722016-03-07 19:57:06 -08004662 int bottomPadding = mBuilder.mContext.getResources().getDimensionPixelSize(
4663 com.android.internal.R.dimen.notification_content_margin_bottom);
Selim Cinek247fa012016-02-18 09:50:48 -08004664 boolean first = true;
Daniel Sandler879c5e02012-04-17 16:46:51 -04004665 while (i < mTexts.size() && i < rowIds.length) {
4666 CharSequence str = mTexts.get(i);
4667 if (str != null && !str.equals("")) {
4668 contentView.setViewVisibility(rowIds[i], View.VISIBLE);
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01004669 contentView.setTextViewText(rowIds[i], mBuilder.processLegacyText(str));
Jorim Jaggi445d3c02014-08-19 22:33:42 +02004670 if (largeText) {
4671 contentView.setTextViewTextSize(rowIds[i], TypedValue.COMPLEX_UNIT_PX,
4672 subTextSize);
4673 }
Selim Cinek9d9fc6e2015-11-12 15:49:14 -05004674 contentView.setViewPadding(rowIds[i], 0, topPadding, 0,
4675 i == rowIds.length - 1 || i == mTexts.size() - 1 ? bottomPadding : 0);
Selim Cinek247fa012016-02-18 09:50:48 -08004676 handleInboxImageMargin(contentView, rowIds[i], first);
4677 first = false;
Daniel Sandler879c5e02012-04-17 16:46:51 -04004678 }
4679 i++;
4680 }
Kenny Guy98193ea2014-07-24 19:54:37 +01004681
Selim Cinek1e0bf612015-11-20 15:57:26 -08004682
Daniel Sandler879c5e02012-04-17 16:46:51 -04004683 return contentView;
4684 }
Selim Cinek1e0bf612015-11-20 15:57:26 -08004685
Selim Cinek247fa012016-02-18 09:50:48 -08004686 private void handleInboxImageMargin(RemoteViews contentView, int id, boolean first) {
Selim Cinek1e0bf612015-11-20 15:57:26 -08004687 int endMargin = 0;
Selim Cinek247fa012016-02-18 09:50:48 -08004688 if (first) {
4689 final int max = mBuilder.mN.extras.getInt(EXTRA_PROGRESS_MAX, 0);
4690 final boolean ind = mBuilder.mN.extras.getBoolean(EXTRA_PROGRESS_INDETERMINATE);
4691 boolean hasProgress = max != 0 || ind;
4692 if (mBuilder.mN.mLargeIcon != null && !hasProgress) {
4693 endMargin = mBuilder.mContext.getResources().getDimensionPixelSize(
4694 R.dimen.notification_content_picture_margin);
4695 }
Selim Cinek1e0bf612015-11-20 15:57:26 -08004696 }
4697 contentView.setViewLayoutMarginEnd(id, endMargin);
4698 }
Daniel Sandler879c5e02012-04-17 16:46:51 -04004699 }
Dan Sandler842dd772014-05-15 09:36:47 -04004700
4701 /**
4702 * Notification style for media playback notifications.
4703 *
4704 * In the expanded form, {@link Notification#bigContentView}, up to 5
4705 * {@link Notification.Action}s specified with
Dan Sandler86647982015-05-13 23:41:13 -04004706 * {@link Notification.Builder#addAction(Action) addAction} will be
Dan Sandler842dd772014-05-15 09:36:47 -04004707 * shown as icon-only pushbuttons, suitable for transport controls. The Bitmap given to
4708 * {@link Notification.Builder#setLargeIcon(android.graphics.Bitmap) setLargeIcon()} will be
4709 * treated as album artwork.
4710 *
4711 * Unlike the other styles provided here, MediaStyle can also modify the standard-size
4712 * {@link Notification#contentView}; by providing action indices to
Christoph Studerfde6f4d2014-12-12 13:23:26 +01004713 * {@link #setShowActionsInCompactView(int...)} you can promote up to 3 actions to be displayed
Dan Sandler842dd772014-05-15 09:36:47 -04004714 * in the standard view alongside the usual content.
4715 *
Bryan Mawhinney6be8de32014-07-18 10:35:12 +01004716 * Notifications created with MediaStyle will have their category set to
4717 * {@link Notification#CATEGORY_TRANSPORT CATEGORY_TRANSPORT} unless you set a different
4718 * category using {@link Notification.Builder#setCategory(String) setCategory()}.
4719 *
Jeff Browndba34ba2014-06-24 20:46:03 -07004720 * Finally, if you attach a {@link android.media.session.MediaSession.Token} using
4721 * {@link android.app.Notification.MediaStyle#setMediaSession(MediaSession.Token)},
Dan Sandler842dd772014-05-15 09:36:47 -04004722 * the System UI can identify this as a notification representing an active media session
4723 * and respond accordingly (by showing album artwork in the lockscreen, for example).
4724 *
4725 * To use this style with your Notification, feed it to
4726 * {@link Notification.Builder#setStyle(android.app.Notification.Style)} like so:
4727 * <pre class="prettyprint">
4728 * Notification noti = new Notification.Builder()
4729 * .setSmallIcon(R.drawable.ic_stat_player)
Christoph Studere935fe92014-11-24 14:18:06 +01004730 * .setContentTitle(&quot;Track title&quot;)
4731 * .setContentText(&quot;Artist - Album&quot;)
4732 * .setLargeIcon(albumArtBitmap))
Bryan Mawhinney6be8de32014-07-18 10:35:12 +01004733 * .setStyle(<b>new Notification.MediaStyle()</b>
4734 * .setMediaSession(mySession))
Dan Sandler842dd772014-05-15 09:36:47 -04004735 * .build();
4736 * </pre>
4737 *
4738 * @see Notification#bigContentView
4739 */
4740 public static class MediaStyle extends Style {
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02004741 static final int MAX_MEDIA_BUTTONS_IN_COMPACT = 3;
Dan Sandler842dd772014-05-15 09:36:47 -04004742 static final int MAX_MEDIA_BUTTONS = 5;
4743
4744 private int[] mActionsToShowInCompact = null;
Jeff Browndba34ba2014-06-24 20:46:03 -07004745 private MediaSession.Token mToken;
Dan Sandler842dd772014-05-15 09:36:47 -04004746
4747 public MediaStyle() {
4748 }
4749
4750 public MediaStyle(Builder builder) {
4751 setBuilder(builder);
4752 }
4753
4754 /**
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02004755 * 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 -04004756 * notification view.
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02004757 *
4758 * @param actions the indices of the actions to show in the compact notification view
Dan Sandler842dd772014-05-15 09:36:47 -04004759 */
4760 public MediaStyle setShowActionsInCompactView(int...actions) {
4761 mActionsToShowInCompact = actions;
4762 return this;
4763 }
4764
4765 /**
Jeff Browndba34ba2014-06-24 20:46:03 -07004766 * Attach a {@link android.media.session.MediaSession.Token} to this Notification
4767 * to provide additional playback information and control to the SystemUI.
Dan Sandler842dd772014-05-15 09:36:47 -04004768 */
Jeff Browndba34ba2014-06-24 20:46:03 -07004769 public MediaStyle setMediaSession(MediaSession.Token token) {
Dan Sandler842dd772014-05-15 09:36:47 -04004770 mToken = token;
4771 return this;
4772 }
4773
Christoph Studer4600f9b2014-07-22 22:44:43 +02004774 /**
4775 * @hide
4776 */
Dan Sandler842dd772014-05-15 09:36:47 -04004777 @Override
4778 public Notification buildStyled(Notification wip) {
Christoph Studer4600f9b2014-07-22 22:44:43 +02004779 super.buildStyled(wip);
Bryan Mawhinney6be8de32014-07-18 10:35:12 +01004780 if (wip.category == null) {
4781 wip.category = Notification.CATEGORY_TRANSPORT;
4782 }
Dan Sandler842dd772014-05-15 09:36:47 -04004783 return wip;
4784 }
4785
Christoph Studer4600f9b2014-07-22 22:44:43 +02004786 /**
4787 * @hide
4788 */
4789 @Override
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004790 public RemoteViews makeContentView() {
4791 return makeMediaContentView();
Christoph Studer4600f9b2014-07-22 22:44:43 +02004792 }
4793
4794 /**
4795 * @hide
4796 */
4797 @Override
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004798 public RemoteViews makeBigContentView() {
4799 return makeMediaBigContentView();
Christoph Studer4600f9b2014-07-22 22:44:43 +02004800 }
4801
Selim Cinekcc10bfb2016-02-10 16:24:21 -08004802 /**
4803 * @hide
4804 */
4805 @Override
4806 public RemoteViews makeHeadsUpContentView() {
4807 RemoteViews expanded = makeMediaBigContentView();
4808 return expanded != null ? expanded : makeMediaContentView();
4809 }
4810
Dan Sandler842dd772014-05-15 09:36:47 -04004811 /** @hide */
4812 @Override
4813 public void addExtras(Bundle extras) {
4814 super.addExtras(extras);
4815
4816 if (mToken != null) {
4817 extras.putParcelable(EXTRA_MEDIA_SESSION, mToken);
4818 }
Bryan Mawhinneye191f902014-07-22 12:50:09 +01004819 if (mActionsToShowInCompact != null) {
4820 extras.putIntArray(EXTRA_COMPACT_ACTIONS, mActionsToShowInCompact);
4821 }
Dan Sandler842dd772014-05-15 09:36:47 -04004822 }
4823
Christoph Studer4600f9b2014-07-22 22:44:43 +02004824 /**
4825 * @hide
4826 */
4827 @Override
4828 protected void restoreFromExtras(Bundle extras) {
4829 super.restoreFromExtras(extras);
4830
4831 if (extras.containsKey(EXTRA_MEDIA_SESSION)) {
4832 mToken = extras.getParcelable(EXTRA_MEDIA_SESSION);
4833 }
4834 if (extras.containsKey(EXTRA_COMPACT_ACTIONS)) {
4835 mActionsToShowInCompact = extras.getIntArray(EXTRA_COMPACT_ACTIONS);
4836 }
4837 }
4838
Selim Cinek5bf069a2015-11-10 19:14:27 -05004839 private RemoteViews generateMediaActionButton(Action action, int color) {
Dan Sandler842dd772014-05-15 09:36:47 -04004840 final boolean tombstone = (action.actionIntent == null);
Selim Cinekf33b1112015-07-15 17:45:11 -07004841 RemoteViews button = new BuilderRemoteViews(mBuilder.mContext.getApplicationInfo(),
Alan Viverette3cb07a462014-06-06 14:19:53 -07004842 R.layout.notification_material_media_action);
Dan Sandler68079d52015-07-22 10:45:30 -04004843 button.setImageViewIcon(R.id.action0, action.getIcon());
Selim Cinek5bf069a2015-11-10 19:14:27 -05004844 button.setDrawableParameters(R.id.action0, false, -1, color, PorterDuff.Mode.SRC_ATOP,
4845 -1);
Dan Sandler842dd772014-05-15 09:36:47 -04004846 if (!tombstone) {
4847 button.setOnClickPendingIntent(R.id.action0, action.actionIntent);
4848 }
4849 button.setContentDescription(R.id.action0, action.title);
4850 return button;
4851 }
4852
4853 private RemoteViews makeMediaContentView() {
4854 RemoteViews view = mBuilder.applyStandardTemplate(
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02004855 R.layout.notification_template_material_media, false /* hasProgress */);
Dan Sandler842dd772014-05-15 09:36:47 -04004856
4857 final int numActions = mBuilder.mActions.size();
4858 final int N = mActionsToShowInCompact == null
4859 ? 0
4860 : Math.min(mActionsToShowInCompact.length, MAX_MEDIA_BUTTONS_IN_COMPACT);
4861 if (N > 0) {
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02004862 view.removeAllViews(com.android.internal.R.id.media_actions);
Dan Sandler842dd772014-05-15 09:36:47 -04004863 for (int i = 0; i < N; i++) {
4864 if (i >= numActions) {
4865 throw new IllegalArgumentException(String.format(
4866 "setShowActionsInCompactView: action %d out of bounds (max %d)",
4867 i, numActions - 1));
4868 }
4869
4870 final Action action = mBuilder.mActions.get(mActionsToShowInCompact[i]);
Selim Cinek5bf069a2015-11-10 19:14:27 -05004871 final RemoteViews button = generateMediaActionButton(action,
Adrian Roos4ff3b122016-02-01 12:26:13 -08004872 mBuilder.resolveContrastColor());
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02004873 view.addView(com.android.internal.R.id.media_actions, button);
Dan Sandler842dd772014-05-15 09:36:47 -04004874 }
4875 }
Selim Cinekfdc738f2016-01-27 20:04:27 -08004876 handleImage(view);
4877 // handle the content margin
Selim Cinek247fa012016-02-18 09:50:48 -08004878 int endMargin = mBuilder.mContext.getResources().getDimensionPixelSize(
4879 R.dimen.notification_content_margin_end);;
Selim Cinekfdc738f2016-01-27 20:04:27 -08004880 if (mBuilder.mN.mLargeIcon != null) {
Selim Cinek247fa012016-02-18 09:50:48 -08004881 endMargin += mBuilder.mContext.getResources().getDimensionPixelSize(
4882 R.dimen.notification_content_picture_margin);
Selim Cinekfdc738f2016-01-27 20:04:27 -08004883 }
4884 view.setViewLayoutMarginEnd(R.id.notification_main_column, endMargin);
Dan Sandler842dd772014-05-15 09:36:47 -04004885 return view;
4886 }
4887
4888 private RemoteViews makeMediaBigContentView() {
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02004889 final int actionCount = Math.min(mBuilder.mActions.size(), MAX_MEDIA_BUTTONS);
Selim Cinekcc10bfb2016-02-10 16:24:21 -08004890 // Dont add an expanded view if there is no more content to be revealed
4891 int actionsInCompact = mActionsToShowInCompact == null
4892 ? 0
4893 : Math.min(mActionsToShowInCompact.length, MAX_MEDIA_BUTTONS_IN_COMPACT);
4894 if (mBuilder.mN.mLargeIcon == null && actionCount <= actionsInCompact) {
4895 return null;
4896 }
Selim Cinek5bf069a2015-11-10 19:14:27 -05004897 RemoteViews big = mBuilder.applyStandardTemplate(
4898 R.layout.notification_template_material_big_media,
4899 false);
Dan Sandler842dd772014-05-15 09:36:47 -04004900
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02004901 if (actionCount > 0) {
4902 big.removeAllViews(com.android.internal.R.id.media_actions);
4903 for (int i = 0; i < actionCount; i++) {
Selim Cinek5bf069a2015-11-10 19:14:27 -05004904 final RemoteViews button = generateMediaActionButton(mBuilder.mActions.get(i),
Adrian Roos4ff3b122016-02-01 12:26:13 -08004905 mBuilder.resolveContrastColor());
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02004906 big.addView(com.android.internal.R.id.media_actions, button);
Dan Sandler842dd772014-05-15 09:36:47 -04004907 }
4908 }
Selim Cinek5bf069a2015-11-10 19:14:27 -05004909 handleImage(big);
Dan Sandler842dd772014-05-15 09:36:47 -04004910 return big;
4911 }
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02004912
Selim Cinek5bf069a2015-11-10 19:14:27 -05004913 private void handleImage(RemoteViews contentView) {
4914 if (mBuilder.mN.mLargeIcon != null) {
4915 contentView.setViewLayoutMarginEnd(R.id.line1, 0);
Selim Cinekc848c3a2016-01-13 15:27:30 -08004916 contentView.setViewLayoutMarginEnd(R.id.text, 0);
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02004917 }
4918 }
4919
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02004920 /**
4921 * @hide
4922 */
4923 @Override
4924 protected boolean hasProgress() {
4925 return false;
4926 }
Dan Sandler842dd772014-05-15 09:36:47 -04004927 }
Griff Hazen61a9e862014-05-22 16:05:19 -07004928
Selim Cinek593610c2016-02-16 18:42:57 -08004929 /**
4930 * Notification style for custom views that are decorated by the system
4931 *
4932 * <p>Instead of providing a notification that is completely custom, a developer can set this
4933 * style and still obtain system decorations like the notification header with the expand
4934 * affordance and actions.
4935 *
4936 * <p>Use {@link android.app.Notification.Builder#setCustomContentView(RemoteViews)},
4937 * {@link android.app.Notification.Builder#setCustomBigContentView(RemoteViews)} and
4938 * {@link android.app.Notification.Builder#setCustomHeadsUpContentView(RemoteViews)} to set the
4939 * corresponding custom views to display.
4940 *
4941 * To use this style with your Notification, feed it to
4942 * {@link Notification.Builder#setStyle(android.app.Notification.Style)} like so:
4943 * <pre class="prettyprint">
4944 * Notification noti = new Notification.Builder()
4945 * .setSmallIcon(R.drawable.ic_stat_player)
4946 * .setLargeIcon(albumArtBitmap))
4947 * .setCustomContentView(contentView);
4948 * .setStyle(<b>new Notification.DecoratedCustomViewStyle()</b>)
4949 * .build();
4950 * </pre>
4951 */
4952 public static class DecoratedCustomViewStyle extends Style {
4953
4954 public DecoratedCustomViewStyle() {
4955 }
4956
4957 public DecoratedCustomViewStyle(Builder builder) {
4958 setBuilder(builder);
4959 }
4960
4961 /**
4962 * @hide
4963 */
4964 public boolean displayCustomViewInline() {
4965 return true;
4966 }
4967
4968 /**
4969 * @hide
4970 */
4971 @Override
4972 public RemoteViews makeContentView() {
4973 return makeStandardTemplateWithCustomContent(mBuilder.mN.contentView);
4974 }
4975
4976 /**
4977 * @hide
4978 */
4979 @Override
4980 public RemoteViews makeBigContentView() {
4981 return makeDecoratedBigContentView();
4982 }
4983
4984 /**
4985 * @hide
4986 */
4987 @Override
4988 public RemoteViews makeHeadsUpContentView() {
4989 return makeDecoratedHeadsUpContentView();
4990 }
4991
Selim Cinek593610c2016-02-16 18:42:57 -08004992 private RemoteViews makeDecoratedHeadsUpContentView() {
4993 RemoteViews headsUpContentView = mBuilder.mN.headsUpContentView == null
4994 ? mBuilder.mN.contentView
4995 : mBuilder.mN.headsUpContentView;
4996 if (mBuilder.mActions.size() == 0) {
4997 return makeStandardTemplateWithCustomContent(headsUpContentView);
4998 }
4999 RemoteViews remoteViews = mBuilder.applyStandardTemplateWithActions(
5000 mBuilder.getBigBaseLayoutResource());
Selim Cinek247fa012016-02-18 09:50:48 -08005001 buildIntoRemoteViewContent(remoteViews, headsUpContentView);
Selim Cinek593610c2016-02-16 18:42:57 -08005002 return remoteViews;
5003 }
5004
Selim Cinek593610c2016-02-16 18:42:57 -08005005 private RemoteViews makeStandardTemplateWithCustomContent(RemoteViews customContent) {
5006 RemoteViews remoteViews = mBuilder.applyStandardTemplate(
5007 mBuilder.getBaseLayoutResource());
Selim Cinek247fa012016-02-18 09:50:48 -08005008 buildIntoRemoteViewContent(remoteViews, customContent);
Selim Cinek593610c2016-02-16 18:42:57 -08005009 return remoteViews;
5010 }
5011
Selim Cinek593610c2016-02-16 18:42:57 -08005012 private RemoteViews makeDecoratedBigContentView() {
5013 RemoteViews bigContentView = mBuilder.mN.bigContentView == null
5014 ? mBuilder.mN.contentView
5015 : mBuilder.mN.bigContentView;
5016 if (mBuilder.mActions.size() == 0) {
5017 return makeStandardTemplateWithCustomContent(bigContentView);
5018 }
5019 RemoteViews remoteViews = mBuilder.applyStandardTemplateWithActions(
5020 mBuilder.getBigBaseLayoutResource());
Selim Cinek247fa012016-02-18 09:50:48 -08005021 buildIntoRemoteViewContent(remoteViews, bigContentView);
Selim Cinek593610c2016-02-16 18:42:57 -08005022 return remoteViews;
5023 }
Selim Cinek247fa012016-02-18 09:50:48 -08005024
5025 private void buildIntoRemoteViewContent(RemoteViews remoteViews,
5026 RemoteViews customContent) {
Adrian Roos5081c0d2016-02-26 16:04:19 -08005027 if (customContent != null) {
Selim Cinekf91017e2016-03-14 12:25:09 -07005028 // Need to clone customContent before adding, because otherwise it can no longer be
5029 // parceled independently of remoteViews.
Adrian Roos5081c0d2016-02-26 16:04:19 -08005030 customContent = customContent.clone();
Selim Cinekf91017e2016-03-14 12:25:09 -07005031 remoteViews.removeAllViews(R.id.notification_main_column);
5032 remoteViews.addView(R.id.notification_main_column, customContent);
Adrian Roos5081c0d2016-02-26 16:04:19 -08005033 }
Selim Cinek247fa012016-02-18 09:50:48 -08005034 // also update the end margin if there is an image
5035 int endMargin = mBuilder.mContext.getResources().getDimensionPixelSize(
5036 R.dimen.notification_content_margin_end);
5037 if (mBuilder.mN.mLargeIcon != null) {
5038 endMargin += mBuilder.mContext.getResources().getDimensionPixelSize(
5039 R.dimen.notification_content_picture_margin);
5040 }
5041 remoteViews.setViewLayoutMarginEnd(R.id.notification_main_column, endMargin);
5042 }
Selim Cinek593610c2016-02-16 18:42:57 -08005043 }
5044
Selim Cinek03eb3b72016-02-18 10:39:45 -08005045 /**
5046 * Notification style for media custom views that are decorated by the system
5047 *
5048 * <p>Instead of providing a media notification that is completely custom, a developer can set
5049 * this style and still obtain system decorations like the notification header with the expand
5050 * affordance and actions.
5051 *
5052 * <p>Use {@link android.app.Notification.Builder#setCustomContentView(RemoteViews)},
5053 * {@link android.app.Notification.Builder#setCustomBigContentView(RemoteViews)} and
5054 * {@link android.app.Notification.Builder#setCustomHeadsUpContentView(RemoteViews)} to set the
5055 * corresponding custom views to display.
5056 *
5057 * To use this style with your Notification, feed it to
5058 * {@link Notification.Builder#setStyle(android.app.Notification.Style)} like so:
5059 * <pre class="prettyprint">
5060 * Notification noti = new Notification.Builder()
5061 * .setSmallIcon(R.drawable.ic_stat_player)
5062 * .setLargeIcon(albumArtBitmap))
5063 * .setCustomContentView(contentView);
5064 * .setStyle(<b>new Notification.DecoratedMediaCustomViewStyle()</b>
5065 * .setMediaSession(mySession))
5066 * .build();
5067 * </pre>
5068 *
5069 * @see android.app.Notification.DecoratedCustomViewStyle
5070 * @see android.app.Notification.MediaStyle
5071 */
5072 public static class DecoratedMediaCustomViewStyle extends MediaStyle {
5073
5074 public DecoratedMediaCustomViewStyle() {
5075 }
5076
5077 public DecoratedMediaCustomViewStyle(Builder builder) {
5078 setBuilder(builder);
5079 }
5080
5081 /**
5082 * @hide
5083 */
5084 public boolean displayCustomViewInline() {
5085 return true;
5086 }
5087
5088 /**
5089 * @hide
5090 */
5091 @Override
5092 public RemoteViews makeContentView() {
5093 RemoteViews remoteViews = super.makeContentView();
5094 return buildIntoRemoteView(remoteViews, R.id.notification_content_container,
5095 mBuilder.mN.contentView);
5096 }
5097
5098 /**
5099 * @hide
5100 */
5101 @Override
5102 public RemoteViews makeBigContentView() {
5103 RemoteViews customRemoteView = mBuilder.mN.bigContentView != null
5104 ? mBuilder.mN.bigContentView
5105 : mBuilder.mN.contentView;
5106 return makeBigContentViewWithCustomContent(customRemoteView);
5107 }
5108
5109 private RemoteViews makeBigContentViewWithCustomContent(RemoteViews customRemoteView) {
5110 RemoteViews remoteViews = super.makeBigContentView();
5111 if (remoteViews != null) {
5112 return buildIntoRemoteView(remoteViews, R.id.notification_main_column,
5113 customRemoteView);
5114 } else if (customRemoteView != mBuilder.mN.contentView){
5115 remoteViews = super.makeContentView();
5116 return buildIntoRemoteView(remoteViews, R.id.notification_content_container,
5117 customRemoteView);
5118 } else {
5119 return null;
5120 }
5121 }
5122
5123 /**
5124 * @hide
5125 */
5126 @Override
5127 public RemoteViews makeHeadsUpContentView() {
5128 RemoteViews customRemoteView = mBuilder.mN.headsUpContentView != null
5129 ? mBuilder.mN.headsUpContentView
5130 : mBuilder.mN.contentView;
5131 return makeBigContentViewWithCustomContent(customRemoteView);
5132 }
5133
5134 private RemoteViews buildIntoRemoteView(RemoteViews remoteViews, int id,
5135 RemoteViews customContent) {
Adrian Roos5081c0d2016-02-26 16:04:19 -08005136 if (customContent != null) {
Selim Cinekf91017e2016-03-14 12:25:09 -07005137 // Need to clone customContent before adding, because otherwise it can no longer be
5138 // parceled independently of remoteViews.
Adrian Roos5081c0d2016-02-26 16:04:19 -08005139 customContent = customContent.clone();
Selim Cinekf91017e2016-03-14 12:25:09 -07005140 remoteViews.removeAllViews(id);
5141 remoteViews.addView(id, customContent);
Adrian Roos5081c0d2016-02-26 16:04:19 -08005142 }
Selim Cinek03eb3b72016-02-18 10:39:45 -08005143 return remoteViews;
5144 }
5145 }
5146
Christoph Studer4600f9b2014-07-22 22:44:43 +02005147 // When adding a new Style subclass here, don't forget to update
5148 // Builder.getNotificationStyleClass.
5149
Griff Hazen61a9e862014-05-22 16:05:19 -07005150 /**
5151 * Extender interface for use with {@link Builder#extend}. Extenders may be used to add
5152 * metadata or change options on a notification builder.
5153 */
5154 public interface Extender {
5155 /**
5156 * Apply this extender to a notification builder.
5157 * @param builder the builder to be modified.
5158 * @return the build object for chaining.
5159 */
5160 public Builder extend(Builder builder);
5161 }
5162
5163 /**
5164 * Helper class to add wearable extensions to notifications.
5165 * <p class="note"> See
5166 * <a href="{@docRoot}wear/notifications/creating.html">Creating Notifications
5167 * for Android Wear</a> for more information on how to use this class.
5168 * <p>
5169 * To create a notification with wearable extensions:
5170 * <ol>
5171 * <li>Create a {@link android.app.Notification.Builder}, setting any desired
5172 * properties.
5173 * <li>Create a {@link android.app.Notification.WearableExtender}.
5174 * <li>Set wearable-specific properties using the
5175 * {@code add} and {@code set} methods of {@link android.app.Notification.WearableExtender}.
5176 * <li>Call {@link android.app.Notification.Builder#extend} to apply the extensions to a
5177 * notification.
5178 * <li>Post the notification to the notification system with the
5179 * {@code NotificationManager.notify(...)} methods.
5180 * </ol>
5181 *
5182 * <pre class="prettyprint">
5183 * Notification notif = new Notification.Builder(mContext)
5184 * .setContentTitle(&quot;New mail from &quot; + sender.toString())
5185 * .setContentText(subject)
5186 * .setSmallIcon(R.drawable.new_mail)
5187 * .extend(new Notification.WearableExtender()
5188 * .setContentIcon(R.drawable.new_mail))
5189 * .build();
5190 * NotificationManager notificationManger =
5191 * (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
5192 * notificationManger.notify(0, notif);</pre>
5193 *
5194 * <p>Wearable extensions can be accessed on an existing notification by using the
5195 * {@code WearableExtender(Notification)} constructor,
5196 * and then using the {@code get} methods to access values.
5197 *
5198 * <pre class="prettyprint">
5199 * Notification.WearableExtender wearableExtender = new Notification.WearableExtender(
5200 * notification);
Griff Hazen14f57992014-05-26 09:07:14 -07005201 * List&lt;Notification&gt; pages = wearableExtender.getPages();</pre>
Griff Hazen61a9e862014-05-22 16:05:19 -07005202 */
5203 public static final class WearableExtender implements Extender {
5204 /**
5205 * Sentinel value for an action index that is unset.
5206 */
5207 public static final int UNSET_ACTION_INDEX = -1;
5208
5209 /**
5210 * Size value for use with {@link #setCustomSizePreset} to show this notification with
5211 * default sizing.
5212 * <p>For custom display notifications created using {@link #setDisplayIntent},
Paul Soulosaa4f4bf2015-08-04 11:59:45 -07005213 * the default is {@link #SIZE_MEDIUM}. All other notifications size automatically based
Griff Hazen61a9e862014-05-22 16:05:19 -07005214 * on their content.
5215 */
5216 public static final int SIZE_DEFAULT = 0;
5217
5218 /**
5219 * Size value for use with {@link #setCustomSizePreset} to show this notification
5220 * with an extra small size.
5221 * <p>This value is only applicable for custom display notifications created using
5222 * {@link #setDisplayIntent}.
5223 */
5224 public static final int SIZE_XSMALL = 1;
5225
5226 /**
5227 * Size value for use with {@link #setCustomSizePreset} to show this notification
5228 * with a small size.
5229 * <p>This value is only applicable for custom display notifications created using
5230 * {@link #setDisplayIntent}.
5231 */
5232 public static final int SIZE_SMALL = 2;
5233
5234 /**
5235 * Size value for use with {@link #setCustomSizePreset} to show this notification
5236 * with a medium size.
5237 * <p>This value is only applicable for custom display notifications created using
5238 * {@link #setDisplayIntent}.
5239 */
5240 public static final int SIZE_MEDIUM = 3;
5241
5242 /**
5243 * Size value for use with {@link #setCustomSizePreset} to show this notification
5244 * with a large size.
5245 * <p>This value is only applicable for custom display notifications created using
5246 * {@link #setDisplayIntent}.
5247 */
5248 public static final int SIZE_LARGE = 4;
5249
Griff Hazend5f11f92014-05-27 15:40:09 -07005250 /**
5251 * Size value for use with {@link #setCustomSizePreset} to show this notification
5252 * full screen.
5253 * <p>This value is only applicable for custom display notifications created using
5254 * {@link #setDisplayIntent}.
5255 */
5256 public static final int SIZE_FULL_SCREEN = 5;
5257
Griff Hazen5f2edfc2014-09-29 16:28:44 -07005258 /**
5259 * Sentinel value for use with {@link #setHintScreenTimeout} to keep the screen on for a
5260 * short amount of time when this notification is displayed on the screen. This
5261 * is the default value.
5262 */
5263 public static final int SCREEN_TIMEOUT_SHORT = 0;
5264
5265 /**
5266 * Sentinel value for use with {@link #setHintScreenTimeout} to keep the screen on
5267 * for a longer amount of time when this notification is displayed on the screen.
5268 */
5269 public static final int SCREEN_TIMEOUT_LONG = -1;
5270
Griff Hazen61a9e862014-05-22 16:05:19 -07005271 /** Notification extra which contains wearable extensions */
5272 private static final String EXTRA_WEARABLE_EXTENSIONS = "android.wearable.EXTENSIONS";
5273
Pete Gastaf6781d2014-10-07 15:17:05 -04005274 // Keys within EXTRA_WEARABLE_EXTENSIONS for wearable options.
Griff Hazen61a9e862014-05-22 16:05:19 -07005275 private static final String KEY_ACTIONS = "actions";
5276 private static final String KEY_FLAGS = "flags";
5277 private static final String KEY_DISPLAY_INTENT = "displayIntent";
5278 private static final String KEY_PAGES = "pages";
5279 private static final String KEY_BACKGROUND = "background";
5280 private static final String KEY_CONTENT_ICON = "contentIcon";
5281 private static final String KEY_CONTENT_ICON_GRAVITY = "contentIconGravity";
5282 private static final String KEY_CONTENT_ACTION_INDEX = "contentActionIndex";
5283 private static final String KEY_CUSTOM_SIZE_PRESET = "customSizePreset";
5284 private static final String KEY_CUSTOM_CONTENT_HEIGHT = "customContentHeight";
5285 private static final String KEY_GRAVITY = "gravity";
Griff Hazen5f2edfc2014-09-29 16:28:44 -07005286 private static final String KEY_HINT_SCREEN_TIMEOUT = "hintScreenTimeout";
Griff Hazen61a9e862014-05-22 16:05:19 -07005287
5288 // Flags bitwise-ored to mFlags
5289 private static final int FLAG_CONTENT_INTENT_AVAILABLE_OFFLINE = 0x1;
5290 private static final int FLAG_HINT_HIDE_ICON = 1 << 1;
5291 private static final int FLAG_HINT_SHOW_BACKGROUND_ONLY = 1 << 2;
5292 private static final int FLAG_START_SCROLL_BOTTOM = 1 << 3;
Griff Hazen5f2edfc2014-09-29 16:28:44 -07005293 private static final int FLAG_HINT_AVOID_BACKGROUND_CLIPPING = 1 << 4;
Alex Hills9ab3a232016-04-05 14:54:56 -04005294 private static final int FLAG_HINT_CONTENT_INTENT_LAUNCHES_ACTIVITY = 1 << 6;
Griff Hazen61a9e862014-05-22 16:05:19 -07005295
5296 // Default value for flags integer
5297 private static final int DEFAULT_FLAGS = FLAG_CONTENT_INTENT_AVAILABLE_OFFLINE;
5298
5299 private static final int DEFAULT_CONTENT_ICON_GRAVITY = Gravity.END;
5300 private static final int DEFAULT_GRAVITY = Gravity.BOTTOM;
5301
5302 private ArrayList<Action> mActions = new ArrayList<Action>();
5303 private int mFlags = DEFAULT_FLAGS;
5304 private PendingIntent mDisplayIntent;
5305 private ArrayList<Notification> mPages = new ArrayList<Notification>();
5306 private Bitmap mBackground;
5307 private int mContentIcon;
5308 private int mContentIconGravity = DEFAULT_CONTENT_ICON_GRAVITY;
5309 private int mContentActionIndex = UNSET_ACTION_INDEX;
5310 private int mCustomSizePreset = SIZE_DEFAULT;
5311 private int mCustomContentHeight;
5312 private int mGravity = DEFAULT_GRAVITY;
Griff Hazen5f2edfc2014-09-29 16:28:44 -07005313 private int mHintScreenTimeout;
Griff Hazen61a9e862014-05-22 16:05:19 -07005314
5315 /**
5316 * Create a {@link android.app.Notification.WearableExtender} with default
5317 * options.
5318 */
5319 public WearableExtender() {
5320 }
5321
5322 public WearableExtender(Notification notif) {
5323 Bundle wearableBundle = notif.extras.getBundle(EXTRA_WEARABLE_EXTENSIONS);
5324 if (wearableBundle != null) {
5325 List<Action> actions = wearableBundle.getParcelableArrayList(KEY_ACTIONS);
5326 if (actions != null) {
5327 mActions.addAll(actions);
5328 }
5329
5330 mFlags = wearableBundle.getInt(KEY_FLAGS, DEFAULT_FLAGS);
5331 mDisplayIntent = wearableBundle.getParcelable(KEY_DISPLAY_INTENT);
5332
5333 Notification[] pages = getNotificationArrayFromBundle(
5334 wearableBundle, KEY_PAGES);
5335 if (pages != null) {
5336 Collections.addAll(mPages, pages);
5337 }
5338
5339 mBackground = wearableBundle.getParcelable(KEY_BACKGROUND);
5340 mContentIcon = wearableBundle.getInt(KEY_CONTENT_ICON);
5341 mContentIconGravity = wearableBundle.getInt(KEY_CONTENT_ICON_GRAVITY,
5342 DEFAULT_CONTENT_ICON_GRAVITY);
5343 mContentActionIndex = wearableBundle.getInt(KEY_CONTENT_ACTION_INDEX,
5344 UNSET_ACTION_INDEX);
5345 mCustomSizePreset = wearableBundle.getInt(KEY_CUSTOM_SIZE_PRESET,
5346 SIZE_DEFAULT);
5347 mCustomContentHeight = wearableBundle.getInt(KEY_CUSTOM_CONTENT_HEIGHT);
5348 mGravity = wearableBundle.getInt(KEY_GRAVITY, DEFAULT_GRAVITY);
Griff Hazen5f2edfc2014-09-29 16:28:44 -07005349 mHintScreenTimeout = wearableBundle.getInt(KEY_HINT_SCREEN_TIMEOUT);
Griff Hazen61a9e862014-05-22 16:05:19 -07005350 }
5351 }
5352
5353 /**
5354 * Apply wearable extensions to a notification that is being built. This is typically
5355 * called by the {@link android.app.Notification.Builder#extend} method of
5356 * {@link android.app.Notification.Builder}.
5357 */
5358 @Override
5359 public Notification.Builder extend(Notification.Builder builder) {
5360 Bundle wearableBundle = new Bundle();
5361
5362 if (!mActions.isEmpty()) {
5363 wearableBundle.putParcelableArrayList(KEY_ACTIONS, mActions);
5364 }
5365 if (mFlags != DEFAULT_FLAGS) {
5366 wearableBundle.putInt(KEY_FLAGS, mFlags);
5367 }
5368 if (mDisplayIntent != null) {
5369 wearableBundle.putParcelable(KEY_DISPLAY_INTENT, mDisplayIntent);
5370 }
5371 if (!mPages.isEmpty()) {
5372 wearableBundle.putParcelableArray(KEY_PAGES, mPages.toArray(
5373 new Notification[mPages.size()]));
5374 }
5375 if (mBackground != null) {
5376 wearableBundle.putParcelable(KEY_BACKGROUND, mBackground);
5377 }
5378 if (mContentIcon != 0) {
5379 wearableBundle.putInt(KEY_CONTENT_ICON, mContentIcon);
5380 }
5381 if (mContentIconGravity != DEFAULT_CONTENT_ICON_GRAVITY) {
5382 wearableBundle.putInt(KEY_CONTENT_ICON_GRAVITY, mContentIconGravity);
5383 }
5384 if (mContentActionIndex != UNSET_ACTION_INDEX) {
5385 wearableBundle.putInt(KEY_CONTENT_ACTION_INDEX,
5386 mContentActionIndex);
5387 }
5388 if (mCustomSizePreset != SIZE_DEFAULT) {
5389 wearableBundle.putInt(KEY_CUSTOM_SIZE_PRESET, mCustomSizePreset);
5390 }
5391 if (mCustomContentHeight != 0) {
5392 wearableBundle.putInt(KEY_CUSTOM_CONTENT_HEIGHT, mCustomContentHeight);
5393 }
5394 if (mGravity != DEFAULT_GRAVITY) {
5395 wearableBundle.putInt(KEY_GRAVITY, mGravity);
5396 }
Griff Hazen5f2edfc2014-09-29 16:28:44 -07005397 if (mHintScreenTimeout != 0) {
5398 wearableBundle.putInt(KEY_HINT_SCREEN_TIMEOUT, mHintScreenTimeout);
5399 }
Griff Hazen61a9e862014-05-22 16:05:19 -07005400
5401 builder.getExtras().putBundle(EXTRA_WEARABLE_EXTENSIONS, wearableBundle);
5402 return builder;
5403 }
5404
5405 @Override
5406 public WearableExtender clone() {
5407 WearableExtender that = new WearableExtender();
5408 that.mActions = new ArrayList<Action>(this.mActions);
5409 that.mFlags = this.mFlags;
5410 that.mDisplayIntent = this.mDisplayIntent;
5411 that.mPages = new ArrayList<Notification>(this.mPages);
5412 that.mBackground = this.mBackground;
5413 that.mContentIcon = this.mContentIcon;
5414 that.mContentIconGravity = this.mContentIconGravity;
5415 that.mContentActionIndex = this.mContentActionIndex;
5416 that.mCustomSizePreset = this.mCustomSizePreset;
5417 that.mCustomContentHeight = this.mCustomContentHeight;
5418 that.mGravity = this.mGravity;
Griff Hazen5f2edfc2014-09-29 16:28:44 -07005419 that.mHintScreenTimeout = this.mHintScreenTimeout;
Griff Hazen61a9e862014-05-22 16:05:19 -07005420 return that;
5421 }
5422
5423 /**
5424 * Add a wearable action to this notification.
5425 *
5426 * <p>When wearable actions are added using this method, the set of actions that
5427 * show on a wearable device splits from devices that only show actions added
5428 * using {@link android.app.Notification.Builder#addAction}. This allows for customization
5429 * of which actions display on different devices.
5430 *
5431 * @param action the action to add to this notification
5432 * @return this object for method chaining
5433 * @see android.app.Notification.Action
5434 */
5435 public WearableExtender addAction(Action action) {
5436 mActions.add(action);
5437 return this;
5438 }
5439
5440 /**
5441 * Adds wearable actions to this notification.
5442 *
5443 * <p>When wearable actions are added using this method, the set of actions that
5444 * show on a wearable device splits from devices that only show actions added
5445 * using {@link android.app.Notification.Builder#addAction}. This allows for customization
5446 * of which actions display on different devices.
5447 *
5448 * @param actions the actions to add to this notification
5449 * @return this object for method chaining
5450 * @see android.app.Notification.Action
5451 */
5452 public WearableExtender addActions(List<Action> actions) {
5453 mActions.addAll(actions);
5454 return this;
5455 }
5456
5457 /**
5458 * Clear all wearable actions present on this builder.
5459 * @return this object for method chaining.
5460 * @see #addAction
5461 */
5462 public WearableExtender clearActions() {
5463 mActions.clear();
5464 return this;
5465 }
5466
5467 /**
5468 * Get the wearable actions present on this notification.
5469 */
5470 public List<Action> getActions() {
5471 return mActions;
5472 }
5473
5474 /**
5475 * Set an intent to launch inside of an activity view when displaying
Griff Hazen14f57992014-05-26 09:07:14 -07005476 * this notification. The {@link PendingIntent} provided should be for an activity.
5477 *
5478 * <pre class="prettyprint">
5479 * Intent displayIntent = new Intent(context, MyDisplayActivity.class);
5480 * PendingIntent displayPendingIntent = PendingIntent.getActivity(context,
5481 * 0, displayIntent, PendingIntent.FLAG_UPDATE_CURRENT);
5482 * Notification notif = new Notification.Builder(context)
5483 * .extend(new Notification.WearableExtender()
5484 * .setDisplayIntent(displayPendingIntent)
5485 * .setCustomSizePreset(Notification.WearableExtender.SIZE_MEDIUM))
5486 * .build();</pre>
5487 *
5488 * <p>The activity to launch needs to allow embedding, must be exported, and
Griff Hazen831ca9d2014-06-17 00:38:38 -07005489 * should have an empty task affinity. It is also recommended to use the device
5490 * default light theme.
Griff Hazen14f57992014-05-26 09:07:14 -07005491 *
5492 * <p>Example AndroidManifest.xml entry:
5493 * <pre class="prettyprint">
5494 * &lt;activity android:name=&quot;com.example.MyDisplayActivity&quot;
5495 * android:exported=&quot;true&quot;
5496 * android:allowEmbedded=&quot;true&quot;
Griff Hazen831ca9d2014-06-17 00:38:38 -07005497 * android:taskAffinity=&quot;&quot;
5498 * android:theme=&quot;@android:style/Theme.DeviceDefault.Light&quot; /&gt;</pre>
Griff Hazen61a9e862014-05-22 16:05:19 -07005499 *
5500 * @param intent the {@link PendingIntent} for an activity
5501 * @return this object for method chaining
5502 * @see android.app.Notification.WearableExtender#getDisplayIntent
5503 */
5504 public WearableExtender setDisplayIntent(PendingIntent intent) {
5505 mDisplayIntent = intent;
5506 return this;
5507 }
5508
5509 /**
5510 * Get the intent to launch inside of an activity view when displaying this
5511 * notification. This {@code PendingIntent} should be for an activity.
5512 */
5513 public PendingIntent getDisplayIntent() {
5514 return mDisplayIntent;
5515 }
5516
5517 /**
5518 * Add an additional page of content to display with this notification. The current
5519 * notification forms the first page, and pages added using this function form
5520 * subsequent pages. This field can be used to separate a notification into multiple
5521 * sections.
5522 *
5523 * @param page the notification to add as another page
5524 * @return this object for method chaining
5525 * @see android.app.Notification.WearableExtender#getPages
5526 */
5527 public WearableExtender addPage(Notification page) {
5528 mPages.add(page);
5529 return this;
5530 }
5531
5532 /**
5533 * Add additional pages of content to display with this notification. The current
5534 * notification forms the first page, and pages added using this function form
5535 * subsequent pages. This field can be used to separate a notification into multiple
5536 * sections.
5537 *
5538 * @param pages a list of notifications
5539 * @return this object for method chaining
5540 * @see android.app.Notification.WearableExtender#getPages
5541 */
5542 public WearableExtender addPages(List<Notification> pages) {
5543 mPages.addAll(pages);
5544 return this;
5545 }
5546
5547 /**
5548 * Clear all additional pages present on this builder.
5549 * @return this object for method chaining.
5550 * @see #addPage
5551 */
5552 public WearableExtender clearPages() {
5553 mPages.clear();
5554 return this;
5555 }
5556
5557 /**
5558 * Get the array of additional pages of content for displaying this notification. The
5559 * current notification forms the first page, and elements within this array form
5560 * subsequent pages. This field can be used to separate a notification into multiple
5561 * sections.
5562 * @return the pages for this notification
5563 */
5564 public List<Notification> getPages() {
5565 return mPages;
5566 }
5567
5568 /**
5569 * Set a background image to be displayed behind the notification content.
5570 * Contrary to the {@link android.app.Notification.BigPictureStyle}, this background
5571 * will work with any notification style.
5572 *
5573 * @param background the background bitmap
5574 * @return this object for method chaining
5575 * @see android.app.Notification.WearableExtender#getBackground
5576 */
5577 public WearableExtender setBackground(Bitmap background) {
5578 mBackground = background;
5579 return this;
5580 }
5581
5582 /**
5583 * Get a background image to be displayed behind the notification content.
5584 * Contrary to the {@link android.app.Notification.BigPictureStyle}, this background
5585 * will work with any notification style.
5586 *
5587 * @return the background image
5588 * @see android.app.Notification.WearableExtender#setBackground
5589 */
5590 public Bitmap getBackground() {
5591 return mBackground;
5592 }
5593
5594 /**
5595 * Set an icon that goes with the content of this notification.
5596 */
5597 public WearableExtender setContentIcon(int icon) {
5598 mContentIcon = icon;
5599 return this;
5600 }
5601
5602 /**
5603 * Get an icon that goes with the content of this notification.
5604 */
5605 public int getContentIcon() {
5606 return mContentIcon;
5607 }
5608
5609 /**
5610 * Set the gravity that the content icon should have within the notification display.
5611 * Supported values include {@link android.view.Gravity#START} and
5612 * {@link android.view.Gravity#END}. The default value is {@link android.view.Gravity#END}.
5613 * @see #setContentIcon
5614 */
5615 public WearableExtender setContentIconGravity(int contentIconGravity) {
5616 mContentIconGravity = contentIconGravity;
5617 return this;
5618 }
5619
5620 /**
5621 * Get the gravity that the content icon should have within the notification display.
5622 * Supported values include {@link android.view.Gravity#START} and
5623 * {@link android.view.Gravity#END}. The default value is {@link android.view.Gravity#END}.
5624 * @see #getContentIcon
5625 */
5626 public int getContentIconGravity() {
5627 return mContentIconGravity;
5628 }
5629
5630 /**
5631 * Set an action from this notification's actions to be clickable with the content of
Griff Hazen14f57992014-05-26 09:07:14 -07005632 * this notification. This action will no longer display separately from the
5633 * notification's content.
5634 *
Griff Hazenca48d352014-05-28 22:37:13 -07005635 * <p>For notifications with multiple pages, child pages can also have content actions
Griff Hazen14f57992014-05-26 09:07:14 -07005636 * set, although the list of available actions comes from the main notification and not
5637 * from the child page's notification.
5638 *
5639 * @param actionIndex The index of the action to hoist onto the current notification page.
5640 * If wearable actions were added to the main notification, this index
5641 * will apply to that list, otherwise it will apply to the regular
5642 * actions list.
Griff Hazen61a9e862014-05-22 16:05:19 -07005643 */
5644 public WearableExtender setContentAction(int actionIndex) {
5645 mContentActionIndex = actionIndex;
5646 return this;
5647 }
5648
5649 /**
Griff Hazenca48d352014-05-28 22:37:13 -07005650 * Get the index of the notification action, if any, that was specified as being clickable
5651 * with the content of this notification. This action will no longer display separately
Griff Hazen14f57992014-05-26 09:07:14 -07005652 * from the notification's content.
Griff Hazen61a9e862014-05-22 16:05:19 -07005653 *
Griff Hazenca48d352014-05-28 22:37:13 -07005654 * <p>For notifications with multiple pages, child pages can also have content actions
Griff Hazen14f57992014-05-26 09:07:14 -07005655 * set, although the list of available actions comes from the main notification and not
5656 * from the child page's notification.
5657 *
5658 * <p>If wearable specific actions were added to the main notification, this index will
5659 * apply to that list, otherwise it will apply to the regular actions list.
Griff Hazenca48d352014-05-28 22:37:13 -07005660 *
5661 * @return the action index or {@link #UNSET_ACTION_INDEX} if no action was selected.
Griff Hazen61a9e862014-05-22 16:05:19 -07005662 */
5663 public int getContentAction() {
5664 return mContentActionIndex;
5665 }
5666
5667 /**
5668 * Set the gravity that this notification should have within the available viewport space.
5669 * Supported values include {@link android.view.Gravity#TOP},
5670 * {@link android.view.Gravity#CENTER_VERTICAL} and {@link android.view.Gravity#BOTTOM}.
5671 * The default value is {@link android.view.Gravity#BOTTOM}.
5672 */
5673 public WearableExtender setGravity(int gravity) {
5674 mGravity = gravity;
5675 return this;
5676 }
5677
5678 /**
5679 * Get the gravity that this notification should have within the available viewport space.
5680 * Supported values include {@link android.view.Gravity#TOP},
5681 * {@link android.view.Gravity#CENTER_VERTICAL} and {@link android.view.Gravity#BOTTOM}.
5682 * The default value is {@link android.view.Gravity#BOTTOM}.
5683 */
5684 public int getGravity() {
5685 return mGravity;
5686 }
5687
5688 /**
5689 * Set the custom size preset for the display of this notification out of the available
5690 * presets found in {@link android.app.Notification.WearableExtender}, e.g.
5691 * {@link #SIZE_LARGE}.
5692 * <p>Some custom size presets are only applicable for custom display notifications created
5693 * using {@link android.app.Notification.WearableExtender#setDisplayIntent}. Check the
5694 * documentation for the preset in question. See also
5695 * {@link #setCustomContentHeight} and {@link #getCustomSizePreset}.
5696 */
5697 public WearableExtender setCustomSizePreset(int sizePreset) {
5698 mCustomSizePreset = sizePreset;
5699 return this;
5700 }
5701
5702 /**
5703 * Get the custom size preset for the display of this notification out of the available
5704 * presets found in {@link android.app.Notification.WearableExtender}, e.g.
5705 * {@link #SIZE_LARGE}.
5706 * <p>Some custom size presets are only applicable for custom display notifications created
5707 * using {@link #setDisplayIntent}. Check the documentation for the preset in question.
5708 * See also {@link #setCustomContentHeight} and {@link #setCustomSizePreset}.
5709 */
5710 public int getCustomSizePreset() {
5711 return mCustomSizePreset;
5712 }
5713
5714 /**
5715 * Set the custom height in pixels for the display of this notification's content.
5716 * <p>This option is only available for custom display notifications created
5717 * using {@link android.app.Notification.WearableExtender#setDisplayIntent}. See also
5718 * {@link android.app.Notification.WearableExtender#setCustomSizePreset} and
5719 * {@link #getCustomContentHeight}.
5720 */
5721 public WearableExtender setCustomContentHeight(int height) {
5722 mCustomContentHeight = height;
5723 return this;
5724 }
5725
5726 /**
5727 * Get the custom height in pixels for the display of this notification's content.
5728 * <p>This option is only available for custom display notifications created
5729 * using {@link #setDisplayIntent}. See also {@link #setCustomSizePreset} and
5730 * {@link #setCustomContentHeight}.
5731 */
5732 public int getCustomContentHeight() {
5733 return mCustomContentHeight;
5734 }
5735
5736 /**
5737 * Set whether the scrolling position for the contents of this notification should start
5738 * at the bottom of the contents instead of the top when the contents are too long to
5739 * display within the screen. Default is false (start scroll at the top).
5740 */
5741 public WearableExtender setStartScrollBottom(boolean startScrollBottom) {
5742 setFlag(FLAG_START_SCROLL_BOTTOM, startScrollBottom);
5743 return this;
5744 }
5745
5746 /**
5747 * Get whether the scrolling position for the contents of this notification should start
5748 * at the bottom of the contents instead of the top when the contents are too long to
5749 * display within the screen. Default is false (start scroll at the top).
5750 */
5751 public boolean getStartScrollBottom() {
5752 return (mFlags & FLAG_START_SCROLL_BOTTOM) != 0;
5753 }
5754
5755 /**
5756 * Set whether the content intent is available when the wearable device is not connected
5757 * to a companion device. The user can still trigger this intent when the wearable device
5758 * is offline, but a visual hint will indicate that the content intent may not be available.
5759 * Defaults to true.
5760 */
5761 public WearableExtender setContentIntentAvailableOffline(
5762 boolean contentIntentAvailableOffline) {
5763 setFlag(FLAG_CONTENT_INTENT_AVAILABLE_OFFLINE, contentIntentAvailableOffline);
5764 return this;
5765 }
5766
5767 /**
5768 * Get whether the content intent is available when the wearable device is not connected
5769 * to a companion device. The user can still trigger this intent when the wearable device
5770 * is offline, but a visual hint will indicate that the content intent may not be available.
5771 * Defaults to true.
5772 */
5773 public boolean getContentIntentAvailableOffline() {
5774 return (mFlags & FLAG_CONTENT_INTENT_AVAILABLE_OFFLINE) != 0;
5775 }
5776
5777 /**
5778 * Set a hint that this notification's icon should not be displayed.
5779 * @param hintHideIcon {@code true} to hide the icon, {@code false} otherwise.
5780 * @return this object for method chaining
5781 */
5782 public WearableExtender setHintHideIcon(boolean hintHideIcon) {
5783 setFlag(FLAG_HINT_HIDE_ICON, hintHideIcon);
5784 return this;
5785 }
5786
5787 /**
5788 * Get a hint that this notification's icon should not be displayed.
5789 * @return {@code true} if this icon should not be displayed, false otherwise.
5790 * The default value is {@code false} if this was never set.
5791 */
5792 public boolean getHintHideIcon() {
5793 return (mFlags & FLAG_HINT_HIDE_ICON) != 0;
5794 }
5795
5796 /**
5797 * Set a visual hint that only the background image of this notification should be
5798 * displayed, and other semantic content should be hidden. This hint is only applicable
5799 * to sub-pages added using {@link #addPage}.
5800 */
5801 public WearableExtender setHintShowBackgroundOnly(boolean hintShowBackgroundOnly) {
5802 setFlag(FLAG_HINT_SHOW_BACKGROUND_ONLY, hintShowBackgroundOnly);
5803 return this;
5804 }
5805
5806 /**
5807 * Get a visual hint that only the background image of this notification should be
5808 * displayed, and other semantic content should be hidden. This hint is only applicable
5809 * to sub-pages added using {@link android.app.Notification.WearableExtender#addPage}.
5810 */
5811 public boolean getHintShowBackgroundOnly() {
5812 return (mFlags & FLAG_HINT_SHOW_BACKGROUND_ONLY) != 0;
5813 }
5814
Griff Hazen5f2edfc2014-09-29 16:28:44 -07005815 /**
Griff Hazen9c5be4e2014-11-17 17:47:50 -08005816 * Set a hint that this notification's background should not be clipped if possible,
5817 * and should instead be resized to fully display on the screen, retaining the aspect
5818 * ratio of the image. This can be useful for images like barcodes or qr codes.
Griff Hazen5f2edfc2014-09-29 16:28:44 -07005819 * @param hintAvoidBackgroundClipping {@code true} to avoid clipping if possible.
5820 * @return this object for method chaining
5821 */
5822 public WearableExtender setHintAvoidBackgroundClipping(
5823 boolean hintAvoidBackgroundClipping) {
5824 setFlag(FLAG_HINT_AVOID_BACKGROUND_CLIPPING, hintAvoidBackgroundClipping);
5825 return this;
5826 }
5827
5828 /**
Griff Hazen9c5be4e2014-11-17 17:47:50 -08005829 * Get a hint that this notification's background should not be clipped if possible,
5830 * and should instead be resized to fully display on the screen, retaining the aspect
5831 * ratio of the image. This can be useful for images like barcodes or qr codes.
Griff Hazen5f2edfc2014-09-29 16:28:44 -07005832 * @return {@code true} if it's ok if the background is clipped on the screen, false
5833 * otherwise. The default value is {@code false} if this was never set.
5834 */
5835 public boolean getHintAvoidBackgroundClipping() {
5836 return (mFlags & FLAG_HINT_AVOID_BACKGROUND_CLIPPING) != 0;
5837 }
5838
5839 /**
5840 * Set a hint that the screen should remain on for at least this duration when
5841 * this notification is displayed on the screen.
5842 * @param timeout The requested screen timeout in milliseconds. Can also be either
5843 * {@link #SCREEN_TIMEOUT_SHORT} or {@link #SCREEN_TIMEOUT_LONG}.
5844 * @return this object for method chaining
5845 */
5846 public WearableExtender setHintScreenTimeout(int timeout) {
5847 mHintScreenTimeout = timeout;
5848 return this;
5849 }
5850
5851 /**
5852 * Get the duration, in milliseconds, that the screen should remain on for
5853 * when this notification is displayed.
5854 * @return the duration in milliseconds if > 0, or either one of the sentinel values
5855 * {@link #SCREEN_TIMEOUT_SHORT} or {@link #SCREEN_TIMEOUT_LONG}.
5856 */
5857 public int getHintScreenTimeout() {
5858 return mHintScreenTimeout;
5859 }
5860
Alex Hills9ab3a232016-04-05 14:54:56 -04005861 /**
5862 * Set a hint that this notification's content intent will launch an {@link Activity}
5863 * directly, telling the platform that it can generate the appropriate transitions.
5864 * @param hintContentIntentLaunchesActivity {@code true} if the content intent will launch
5865 * an activity and transitions should be generated, false otherwise.
5866 * @return this object for method chaining
5867 */
5868 public WearableExtender setHintContentIntentLaunchesActivity(
5869 boolean hintContentIntentLaunchesActivity) {
5870 setFlag(FLAG_HINT_CONTENT_INTENT_LAUNCHES_ACTIVITY, hintContentIntentLaunchesActivity);
5871 return this;
5872 }
5873
5874 /**
5875 * Get a hint that this notification's content intent will launch an {@link Activity}
5876 * directly, telling the platform that it can generate the appropriate transitions
5877 * @return {@code true} if the content intent will launch an activity and transitions should
5878 * be generated, false otherwise. The default value is {@code false} if this was never set.
5879 */
5880 public boolean getHintContentIntentLaunchesActivity() {
5881 return (mFlags & FLAG_HINT_CONTENT_INTENT_LAUNCHES_ACTIVITY) != 0;
5882 }
5883
Griff Hazen61a9e862014-05-22 16:05:19 -07005884 private void setFlag(int mask, boolean value) {
5885 if (value) {
5886 mFlags |= mask;
5887 } else {
5888 mFlags &= ~mask;
5889 }
5890 }
5891 }
5892
5893 /**
Zhen Yu Song9d5528b2014-11-14 15:34:39 -08005894 * <p>Helper class to add Android Auto extensions to notifications. To create a notification
5895 * with car extensions:
5896 *
5897 * <ol>
5898 * <li>Create an {@link Notification.Builder}, setting any desired
5899 * properties.
5900 * <li>Create a {@link CarExtender}.
5901 * <li>Set car-specific properties using the {@code add} and {@code set} methods of
5902 * {@link CarExtender}.
5903 * <li>Call {@link Notification.Builder#extend(Notification.Extender)}
5904 * to apply the extensions to a notification.
5905 * </ol>
5906 *
5907 * <pre class="prettyprint">
5908 * Notification notification = new Notification.Builder(context)
5909 * ...
5910 * .extend(new CarExtender()
5911 * .set*(...))
5912 * .build();
5913 * </pre>
5914 *
5915 * <p>Car extensions can be accessed on an existing notification by using the
5916 * {@code CarExtender(Notification)} constructor, and then using the {@code get} methods
5917 * to access values.
5918 */
5919 public static final class CarExtender implements Extender {
5920 private static final String TAG = "CarExtender";
5921
5922 private static final String EXTRA_CAR_EXTENDER = "android.car.EXTENSIONS";
5923 private static final String EXTRA_LARGE_ICON = "large_icon";
5924 private static final String EXTRA_CONVERSATION = "car_conversation";
5925 private static final String EXTRA_COLOR = "app_color";
5926
5927 private Bitmap mLargeIcon;
5928 private UnreadConversation mUnreadConversation;
5929 private int mColor = Notification.COLOR_DEFAULT;
5930
5931 /**
5932 * Create a {@link CarExtender} with default options.
5933 */
5934 public CarExtender() {
5935 }
5936
5937 /**
5938 * Create a {@link CarExtender} from the CarExtender options of an existing Notification.
5939 *
5940 * @param notif The notification from which to copy options.
5941 */
5942 public CarExtender(Notification notif) {
5943 Bundle carBundle = notif.extras == null ?
5944 null : notif.extras.getBundle(EXTRA_CAR_EXTENDER);
5945 if (carBundle != null) {
5946 mLargeIcon = carBundle.getParcelable(EXTRA_LARGE_ICON);
5947 mColor = carBundle.getInt(EXTRA_COLOR, Notification.COLOR_DEFAULT);
5948
5949 Bundle b = carBundle.getBundle(EXTRA_CONVERSATION);
5950 mUnreadConversation = UnreadConversation.getUnreadConversationFromBundle(b);
5951 }
5952 }
5953
5954 /**
5955 * Apply car extensions to a notification that is being built. This is typically called by
5956 * the {@link Notification.Builder#extend(Notification.Extender)}
5957 * method of {@link Notification.Builder}.
5958 */
5959 @Override
5960 public Notification.Builder extend(Notification.Builder builder) {
5961 Bundle carExtensions = new Bundle();
5962
5963 if (mLargeIcon != null) {
5964 carExtensions.putParcelable(EXTRA_LARGE_ICON, mLargeIcon);
5965 }
5966 if (mColor != Notification.COLOR_DEFAULT) {
5967 carExtensions.putInt(EXTRA_COLOR, mColor);
5968 }
5969
5970 if (mUnreadConversation != null) {
5971 Bundle b = mUnreadConversation.getBundleForUnreadConversation();
5972 carExtensions.putBundle(EXTRA_CONVERSATION, b);
5973 }
5974
5975 builder.getExtras().putBundle(EXTRA_CAR_EXTENDER, carExtensions);
5976 return builder;
5977 }
5978
5979 /**
5980 * Sets the accent color to use when Android Auto presents the notification.
5981 *
5982 * Android Auto uses the color set with {@link Notification.Builder#setColor(int)}
5983 * to accent the displayed notification. However, not all colors are acceptable in an
5984 * automotive setting. This method can be used to override the color provided in the
5985 * notification in such a situation.
5986 */
Tor Norbye80756e32015-03-02 09:39:27 -08005987 public CarExtender setColor(@ColorInt int color) {
Zhen Yu Song9d5528b2014-11-14 15:34:39 -08005988 mColor = color;
5989 return this;
5990 }
5991
5992 /**
5993 * Gets the accent color.
5994 *
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005995 * @see #setColor
Zhen Yu Song9d5528b2014-11-14 15:34:39 -08005996 */
Tor Norbye80756e32015-03-02 09:39:27 -08005997 @ColorInt
Zhen Yu Song9d5528b2014-11-14 15:34:39 -08005998 public int getColor() {
5999 return mColor;
6000 }
6001
6002 /**
6003 * Sets the large icon of the car notification.
6004 *
6005 * If no large icon is set in the extender, Android Auto will display the icon
6006 * specified by {@link Notification.Builder#setLargeIcon(android.graphics.Bitmap)}
6007 *
6008 * @param largeIcon The large icon to use in the car notification.
6009 * @return This object for method chaining.
6010 */
6011 public CarExtender setLargeIcon(Bitmap largeIcon) {
6012 mLargeIcon = largeIcon;
6013 return this;
6014 }
6015
6016 /**
6017 * Gets the large icon used in this car notification, or null if no icon has been set.
6018 *
6019 * @return The large icon for the car notification.
6020 * @see CarExtender#setLargeIcon
6021 */
6022 public Bitmap getLargeIcon() {
6023 return mLargeIcon;
6024 }
6025
6026 /**
6027 * Sets the unread conversation in a message notification.
6028 *
6029 * @param unreadConversation The unread part of the conversation this notification conveys.
6030 * @return This object for method chaining.
6031 */
6032 public CarExtender setUnreadConversation(UnreadConversation unreadConversation) {
6033 mUnreadConversation = unreadConversation;
6034 return this;
6035 }
6036
6037 /**
6038 * Returns the unread conversation conveyed by this notification.
6039 * @see #setUnreadConversation(UnreadConversation)
6040 */
6041 public UnreadConversation getUnreadConversation() {
6042 return mUnreadConversation;
6043 }
6044
6045 /**
6046 * A class which holds the unread messages from a conversation.
6047 */
6048 public static class UnreadConversation {
6049 private static final String KEY_AUTHOR = "author";
6050 private static final String KEY_TEXT = "text";
6051 private static final String KEY_MESSAGES = "messages";
6052 private static final String KEY_REMOTE_INPUT = "remote_input";
6053 private static final String KEY_ON_REPLY = "on_reply";
6054 private static final String KEY_ON_READ = "on_read";
6055 private static final String KEY_PARTICIPANTS = "participants";
6056 private static final String KEY_TIMESTAMP = "timestamp";
6057
6058 private final String[] mMessages;
6059 private final RemoteInput mRemoteInput;
6060 private final PendingIntent mReplyPendingIntent;
6061 private final PendingIntent mReadPendingIntent;
6062 private final String[] mParticipants;
6063 private final long mLatestTimestamp;
6064
6065 UnreadConversation(String[] messages, RemoteInput remoteInput,
6066 PendingIntent replyPendingIntent, PendingIntent readPendingIntent,
6067 String[] participants, long latestTimestamp) {
6068 mMessages = messages;
6069 mRemoteInput = remoteInput;
6070 mReadPendingIntent = readPendingIntent;
6071 mReplyPendingIntent = replyPendingIntent;
6072 mParticipants = participants;
6073 mLatestTimestamp = latestTimestamp;
6074 }
6075
6076 /**
6077 * Gets the list of messages conveyed by this notification.
6078 */
6079 public String[] getMessages() {
6080 return mMessages;
6081 }
6082
6083 /**
6084 * Gets the remote input that will be used to convey the response to a message list, or
6085 * null if no such remote input exists.
6086 */
6087 public RemoteInput getRemoteInput() {
6088 return mRemoteInput;
6089 }
6090
6091 /**
6092 * Gets the pending intent that will be triggered when the user replies to this
6093 * notification.
6094 */
6095 public PendingIntent getReplyPendingIntent() {
6096 return mReplyPendingIntent;
6097 }
6098
6099 /**
6100 * Gets the pending intent that Android Auto will send after it reads aloud all messages
6101 * in this object's message list.
6102 */
6103 public PendingIntent getReadPendingIntent() {
6104 return mReadPendingIntent;
6105 }
6106
6107 /**
6108 * Gets the participants in the conversation.
6109 */
6110 public String[] getParticipants() {
6111 return mParticipants;
6112 }
6113
6114 /**
6115 * Gets the firs participant in the conversation.
6116 */
6117 public String getParticipant() {
6118 return mParticipants.length > 0 ? mParticipants[0] : null;
6119 }
6120
6121 /**
6122 * Gets the timestamp of the conversation.
6123 */
6124 public long getLatestTimestamp() {
6125 return mLatestTimestamp;
6126 }
6127
6128 Bundle getBundleForUnreadConversation() {
6129 Bundle b = new Bundle();
6130 String author = null;
6131 if (mParticipants != null && mParticipants.length > 1) {
6132 author = mParticipants[0];
6133 }
6134 Parcelable[] messages = new Parcelable[mMessages.length];
6135 for (int i = 0; i < messages.length; i++) {
6136 Bundle m = new Bundle();
6137 m.putString(KEY_TEXT, mMessages[i]);
6138 m.putString(KEY_AUTHOR, author);
6139 messages[i] = m;
6140 }
6141 b.putParcelableArray(KEY_MESSAGES, messages);
6142 if (mRemoteInput != null) {
6143 b.putParcelable(KEY_REMOTE_INPUT, mRemoteInput);
6144 }
6145 b.putParcelable(KEY_ON_REPLY, mReplyPendingIntent);
6146 b.putParcelable(KEY_ON_READ, mReadPendingIntent);
6147 b.putStringArray(KEY_PARTICIPANTS, mParticipants);
6148 b.putLong(KEY_TIMESTAMP, mLatestTimestamp);
6149 return b;
6150 }
6151
6152 static UnreadConversation getUnreadConversationFromBundle(Bundle b) {
6153 if (b == null) {
6154 return null;
6155 }
6156 Parcelable[] parcelableMessages = b.getParcelableArray(KEY_MESSAGES);
6157 String[] messages = null;
6158 if (parcelableMessages != null) {
6159 String[] tmp = new String[parcelableMessages.length];
6160 boolean success = true;
6161 for (int i = 0; i < tmp.length; i++) {
6162 if (!(parcelableMessages[i] instanceof Bundle)) {
6163 success = false;
6164 break;
6165 }
6166 tmp[i] = ((Bundle) parcelableMessages[i]).getString(KEY_TEXT);
6167 if (tmp[i] == null) {
6168 success = false;
6169 break;
6170 }
6171 }
6172 if (success) {
6173 messages = tmp;
6174 } else {
6175 return null;
6176 }
6177 }
6178
6179 PendingIntent onRead = b.getParcelable(KEY_ON_READ);
6180 PendingIntent onReply = b.getParcelable(KEY_ON_REPLY);
6181
6182 RemoteInput remoteInput = b.getParcelable(KEY_REMOTE_INPUT);
6183
6184 String[] participants = b.getStringArray(KEY_PARTICIPANTS);
6185 if (participants == null || participants.length != 1) {
6186 return null;
6187 }
6188
6189 return new UnreadConversation(messages,
6190 remoteInput,
6191 onReply,
6192 onRead,
6193 participants, b.getLong(KEY_TIMESTAMP));
6194 }
6195 };
6196
6197 /**
6198 * Builder class for {@link CarExtender.UnreadConversation} objects.
6199 */
6200 public static class Builder {
6201 private final List<String> mMessages = new ArrayList<String>();
6202 private final String mParticipant;
6203 private RemoteInput mRemoteInput;
6204 private PendingIntent mReadPendingIntent;
6205 private PendingIntent mReplyPendingIntent;
6206 private long mLatestTimestamp;
6207
6208 /**
6209 * Constructs a new builder for {@link CarExtender.UnreadConversation}.
6210 *
6211 * @param name The name of the other participant in the conversation.
6212 */
6213 public Builder(String name) {
6214 mParticipant = name;
6215 }
6216
6217 /**
6218 * Appends a new unread message to the list of messages for this conversation.
6219 *
6220 * The messages should be added from oldest to newest.
6221 *
6222 * @param message The text of the new unread message.
6223 * @return This object for method chaining.
6224 */
6225 public Builder addMessage(String message) {
6226 mMessages.add(message);
6227 return this;
6228 }
6229
6230 /**
6231 * Sets the pending intent and remote input which will convey the reply to this
6232 * notification.
6233 *
6234 * @param pendingIntent The pending intent which will be triggered on a reply.
6235 * @param remoteInput The remote input parcelable which will carry the reply.
6236 * @return This object for method chaining.
6237 *
6238 * @see CarExtender.UnreadConversation#getRemoteInput
6239 * @see CarExtender.UnreadConversation#getReplyPendingIntent
6240 */
6241 public Builder setReplyAction(
6242 PendingIntent pendingIntent, RemoteInput remoteInput) {
6243 mRemoteInput = remoteInput;
6244 mReplyPendingIntent = pendingIntent;
6245
6246 return this;
6247 }
6248
6249 /**
6250 * Sets the pending intent that will be sent once the messages in this notification
6251 * are read.
6252 *
6253 * @param pendingIntent The pending intent to use.
6254 * @return This object for method chaining.
6255 */
6256 public Builder setReadPendingIntent(PendingIntent pendingIntent) {
6257 mReadPendingIntent = pendingIntent;
6258 return this;
6259 }
6260
6261 /**
6262 * Sets the timestamp of the most recent message in an unread conversation.
6263 *
6264 * If a messaging notification has been posted by your application and has not
6265 * yet been cancelled, posting a later notification with the same id and tag
6266 * but without a newer timestamp may result in Android Auto not displaying a
6267 * heads up notification for the later notification.
6268 *
6269 * @param timestamp The timestamp of the most recent message in the conversation.
6270 * @return This object for method chaining.
6271 */
6272 public Builder setLatestTimestamp(long timestamp) {
6273 mLatestTimestamp = timestamp;
6274 return this;
6275 }
6276
6277 /**
6278 * Builds a new unread conversation object.
6279 *
6280 * @return The new unread conversation object.
6281 */
6282 public UnreadConversation build() {
6283 String[] messages = mMessages.toArray(new String[mMessages.size()]);
6284 String[] participants = { mParticipant };
6285 return new UnreadConversation(messages, mRemoteInput, mReplyPendingIntent,
6286 mReadPendingIntent, participants, mLatestTimestamp);
6287 }
6288 }
6289 }
6290
6291 /**
Griff Hazen61a9e862014-05-22 16:05:19 -07006292 * Get an array of Notification objects from a parcelable array bundle field.
6293 * Update the bundle to have a typed array so fetches in the future don't need
6294 * to do an array copy.
6295 */
6296 private static Notification[] getNotificationArrayFromBundle(Bundle bundle, String key) {
6297 Parcelable[] array = bundle.getParcelableArray(key);
6298 if (array instanceof Notification[] || array == null) {
6299 return (Notification[]) array;
6300 }
6301 Notification[] typedArray = Arrays.copyOf(array, array.length,
6302 Notification[].class);
6303 bundle.putParcelableArray(key, typedArray);
6304 return typedArray;
6305 }
Christoph Studer4600f9b2014-07-22 22:44:43 +02006306
6307 private static class BuilderRemoteViews extends RemoteViews {
6308 public BuilderRemoteViews(Parcel parcel) {
6309 super(parcel);
6310 }
6311
Kenny Guy77320062014-08-27 21:37:15 +01006312 public BuilderRemoteViews(ApplicationInfo appInfo, int layoutId) {
6313 super(appInfo, layoutId);
Christoph Studer4600f9b2014-07-22 22:44:43 +02006314 }
6315
6316 @Override
6317 public BuilderRemoteViews clone() {
6318 Parcel p = Parcel.obtain();
6319 writeToParcel(p, 0);
6320 p.setDataPosition(0);
6321 BuilderRemoteViews brv = new BuilderRemoteViews(p);
6322 p.recycle();
6323 return brv;
6324 }
6325 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006326}