blob: c65f0177fce1a60b4049045ae9ad8bbe6c00c5ac [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 Norbyed9273d62013-05-30 15:59:53 -070019import android.annotation.IntDef;
Daniel Sandler01df1c62014-06-09 10:54:01 -040020import android.annotation.SdkConstant;
21import android.annotation.SdkConstant.SdkConstantType;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080022import android.content.Context;
23import android.content.Intent;
Kenny Guy77320062014-08-27 21:37:15 +010024import android.content.pm.ApplicationInfo;
Christoph Studer4600f9b2014-07-22 22:44:43 +020025import android.content.pm.PackageManager.NameNotFoundException;
Jorim Jaggief72a192014-08-26 21:57:46 +020026import android.content.res.ColorStateList;
Joe Onoratoef1e7762010-09-17 18:38:38 -040027import android.graphics.Bitmap;
Kenny Guy8a0101b2014-05-08 23:34:12 +010028import android.graphics.Canvas;
Jorim Jaggi5c2d8462014-03-21 17:37:00 +010029import android.graphics.PorterDuff;
Kenny Guy8a0101b2014-05-08 23:34:12 +010030import android.graphics.drawable.Drawable;
John Spurlockc0650f022014-07-19 13:22:39 -040031import android.media.AudioAttributes;
Jeff Sharkey098d5802012-04-26 17:30:34 -070032import android.media.AudioManager;
Jeff Browndba34ba2014-06-24 20:46:03 -070033import android.media.session.MediaSession;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080034import android.net.Uri;
Daniel Sandlerdcbaf662013-04-26 16:23:09 -040035import android.os.BadParcelableException;
Christoph Studer239f8352014-08-25 15:13:18 +020036import android.os.Build;
Daniel Sandler2561b0b2012-02-13 21:04:12 -050037import android.os.Bundle;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080038import android.os.Parcel;
39import android.os.Parcelable;
Daniel Sandlera2985ed2012-04-03 16:42:00 -040040import android.os.SystemClock;
Jeff Sharkey6d515712012-09-20 16:06:08 -070041import android.os.UserHandle;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080042import android.text.TextUtils;
Daniel Sandlerdcbaf662013-04-26 16:23:09 -040043import android.util.Log;
Jorim Jaggi445d3c02014-08-19 22:33:42 +020044import android.util.MathUtils;
Daniel Sandler9f7936a2012-05-21 16:14:28 -040045import android.util.TypedValue;
Griff Hazen61a9e862014-05-22 16:05:19 -070046import android.view.Gravity;
Joe Onorato8595a3d2010-11-19 18:12:07 -080047import android.view.View;
Jeff Sharkey1c400132011-08-05 14:50:13 -070048import android.widget.ProgressBar;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080049import android.widget.RemoteViews;
50
Griff Hazen959591e2014-05-15 22:26:18 -070051import com.android.internal.R;
Griff Hazenc091ba82014-05-16 10:13:26 -070052import com.android.internal.util.NotificationColorUtil;
Griff Hazen959591e2014-05-15 22:26:18 -070053
Tor Norbyed9273d62013-05-30 15:59:53 -070054import java.lang.annotation.Retention;
55import java.lang.annotation.RetentionPolicy;
Christoph Studer4600f9b2014-07-22 22:44:43 +020056import java.lang.reflect.Constructor;
Andy Stadler110988c2010-12-03 14:29:16 -080057import java.text.NumberFormat;
Daniel Sandler2561b0b2012-02-13 21:04:12 -050058import java.util.ArrayList;
Griff Hazen61a9e862014-05-22 16:05:19 -070059import java.util.Arrays;
Griff Hazen5cadc3b2014-05-20 09:55:39 -070060import java.util.Collections;
Griff Hazen61a9e862014-05-22 16:05:19 -070061import java.util.List;
Joe Onorato561d3852010-11-20 18:09:34 -080062
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080063/**
64 * A class that represents how a persistent notification is to be presented to
65 * the user using the {@link android.app.NotificationManager}.
66 *
Joe Onoratocb109a02011-01-18 17:57:41 -080067 * <p>The {@link Notification.Builder Notification.Builder} has been added to make it
68 * easier to construct Notifications.</p>
69 *
Joe Fernandez558459f2011-10-13 16:47:36 -070070 * <div class="special reference">
71 * <h3>Developer Guides</h3>
72 * <p>For a guide to creating notifications, read the
73 * <a href="{@docRoot}guide/topics/ui/notifiers/notifications.html">Status Bar Notifications</a>
74 * developer guide.</p>
75 * </div>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080076 */
77public class Notification implements Parcelable
78{
Daniel Sandlerdcbaf662013-04-26 16:23:09 -040079 private static final String TAG = "Notification";
80
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080081 /**
Daniel Sandler01df1c62014-06-09 10:54:01 -040082 * An activity that provides a user interface for adjusting notification preferences for its
83 * containing application. Optional but recommended for apps that post
84 * {@link android.app.Notification Notifications}.
85 */
86 @SdkConstant(SdkConstantType.INTENT_CATEGORY)
87 public static final String INTENT_CATEGORY_NOTIFICATION_PREFERENCES
88 = "android.intent.category.NOTIFICATION_PREFERENCES";
89
90 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080091 * Use all default values (where applicable).
92 */
93 public static final int DEFAULT_ALL = ~0;
Daniel Sandler2561b0b2012-02-13 21:04:12 -050094
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080095 /**
96 * Use the default notification sound. This will ignore any given
97 * {@link #sound}.
Daniel Sandler2561b0b2012-02-13 21:04:12 -050098 *
Chris Wren47c20a12014-06-18 17:27:29 -040099 * <p>
100 * A notification that is noisy is more likely to be presented as a heads-up notification.
101 * </p>
102 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800103 * @see #defaults
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500104 */
105
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800106 public static final int DEFAULT_SOUND = 1;
107
108 /**
109 * Use the default notification vibrate. This will ignore any given
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500110 * {@link #vibrate}. Using phone vibration requires the
Scott Mainb8b36452009-04-26 15:50:49 -0700111 * {@link android.Manifest.permission#VIBRATE VIBRATE} permission.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500112 *
Chris Wren47c20a12014-06-18 17:27:29 -0400113 * <p>
114 * A notification that vibrates is more likely to be presented as a heads-up notification.
115 * </p>
116 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800117 * @see #defaults
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500118 */
119
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800120 public static final int DEFAULT_VIBRATE = 2;
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500121
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800122 /**
123 * Use the default notification lights. This will ignore the
124 * {@link #FLAG_SHOW_LIGHTS} bit, and {@link #ledARGB}, {@link #ledOffMS}, or
125 * {@link #ledOnMS}.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500126 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800127 * @see #defaults
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500128 */
129
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800130 public static final int DEFAULT_LIGHTS = 4;
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500131
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800132 /**
Christoph Studer535ec612014-09-03 15:47:47 +0200133 * Maximum length of CharSequences accepted by Builder and friends.
134 *
135 * <p>
136 * Avoids spamming the system with overly large strings such as full e-mails.
137 */
138 private static final int MAX_CHARSEQUENCE_LENGTH = 5 * 1024;
139
140 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500141 * A timestamp related to this notification, in milliseconds since the epoch.
Joe Malin8d40d042012-11-05 11:36:40 -0800142 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500143 * Default value: {@link System#currentTimeMillis() Now}.
144 *
145 * Choose a timestamp that will be most relevant to the user. For most finite events, this
146 * corresponds to the time the event happened (or will happen, in the case of events that have
147 * yet to occur but about which the user is being informed). Indefinite events should be
Joe Malin8d40d042012-11-05 11:36:40 -0800148 * timestamped according to when the activity began.
149 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500150 * Some examples:
Joe Malin8d40d042012-11-05 11:36:40 -0800151 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500152 * <ul>
153 * <li>Notification of a new chat message should be stamped when the message was received.</li>
154 * <li>Notification of an ongoing file download (with a progress bar, for example) should be stamped when the download started.</li>
155 * <li>Notification of a completed file download should be stamped when the download finished.</li>
156 * <li>Notification of an upcoming meeting should be stamped with the time the meeting will begin (that is, in the future).</li>
157 * <li>Notification of an ongoing stopwatch (increasing timer) should be stamped with the watch's start time.
158 * <li>Notification of an ongoing countdown timer should be stamped with the timer's end time.
Joe Malin8d40d042012-11-05 11:36:40 -0800159 * </ul>
160 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800161 */
162 public long when;
163
164 /**
165 * The resource id of a drawable to use as the icon in the status bar.
Daniel Sandlerd952dae2011-02-07 16:33:36 -0500166 * This is required; notifications with an invalid icon resource will not be shown.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800167 */
168 public int icon;
169
170 /**
Joe Onorato46439ce2010-11-19 13:56:21 -0800171 * If the icon in the status bar is to have more than one level, you can set this. Otherwise,
172 * leave it at its default value of 0.
173 *
174 * @see android.widget.ImageView#setImageLevel
Griff Hazen959591e2014-05-15 22:26:18 -0700175 * @see android.graphics.drawable.Drawable#setLevel
Joe Onorato46439ce2010-11-19 13:56:21 -0800176 */
177 public int iconLevel;
178
179 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500180 * The number of events that this notification represents. For example, in a new mail
181 * notification, this could be the number of unread messages.
Joe Malin8d40d042012-11-05 11:36:40 -0800182 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500183 * The system may or may not use this field to modify the appearance of the notification. For
184 * example, before {@link android.os.Build.VERSION_CODES#HONEYCOMB}, this number was
185 * superimposed over the icon in the status bar. Starting with
186 * {@link android.os.Build.VERSION_CODES#HONEYCOMB}, the template used by
187 * {@link Notification.Builder} has displayed the number in the expanded notification view.
Joe Malin8d40d042012-11-05 11:36:40 -0800188 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500189 * If the number is 0 or negative, it is never shown.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800190 */
191 public int number;
192
193 /**
194 * The intent to execute when the expanded status entry is clicked. If
195 * this is an activity, it must include the
196 * {@link android.content.Intent#FLAG_ACTIVITY_NEW_TASK} flag, which requires
Scott Main7aee61f2011-02-08 11:25:01 -0800197 * that you take care of task management as described in the
198 * <a href="{@docRoot}guide/topics/fundamentals/tasks-and-back-stack.html">Tasks and Back
Dianne Hackborn6ceca582012-01-10 15:24:26 -0800199 * Stack</a> document. In particular, make sure to read the notification section
200 * <a href="{@docRoot}guide/topics/ui/notifiers/notifications.html#HandlingNotifications">Handling
201 * Notifications</a> for the correct ways to launch an application from a
202 * notification.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800203 */
204 public PendingIntent contentIntent;
205
206 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500207 * The intent to execute when the notification is explicitly dismissed by the user, either with
208 * the "Clear All" button or by swiping it away individually.
209 *
210 * This probably shouldn't be launching an activity since several of those will be sent
211 * at the same time.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800212 */
213 public PendingIntent deleteIntent;
214
215 /**
Dianne Hackborn170bae72010-09-03 15:14:28 -0700216 * An intent to launch instead of posting the notification to the status bar.
Joe Onoratocb109a02011-01-18 17:57:41 -0800217 *
Chris Wren47c20a12014-06-18 17:27:29 -0400218 * <p>
219 * The system UI may choose to display a heads-up notification, instead of
220 * launching this intent, while the user is using the device.
221 * </p>
222 *
Joe Onoratocb109a02011-01-18 17:57:41 -0800223 * @see Notification.Builder#setFullScreenIntent
Daniel Sandlere46cbd32010-06-17 10:35:26 -0400224 */
225 public PendingIntent fullScreenIntent;
226
227 /**
Dan Sandler5fcdf6e2014-07-18 11:31:15 -0400228 * Text that summarizes this notification for accessibility services.
229 *
230 * As of the L release, this text is no longer shown on screen, but it is still useful to
231 * accessibility services (where it serves as an audible announcement of the notification's
232 * appearance).
Joe Onoratoef1e7762010-09-17 18:38:38 -0400233 *
Joe Onorato46439ce2010-11-19 13:56:21 -0800234 * @see #tickerView
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800235 */
236 public CharSequence tickerText;
237
238 /**
Dan Sandler5fcdf6e2014-07-18 11:31:15 -0400239 * Formerly, a view showing the {@link #tickerText}.
240 *
241 * No longer displayed in the status bar as of API 21.
Joe Onoratoef1e7762010-09-17 18:38:38 -0400242 */
Dan Sandler5fcdf6e2014-07-18 11:31:15 -0400243 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -0800244 public RemoteViews tickerView;
Joe Onoratoef1e7762010-09-17 18:38:38 -0400245
246 /**
Daniel Sandlere46cbd32010-06-17 10:35:26 -0400247 * The view that will represent this notification in the expanded status bar.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800248 */
249 public RemoteViews contentView;
250
Daniel Sandlera0a938c2012-03-15 08:42:37 -0400251 /**
Daniel Sandler4dfbe832012-04-11 14:51:46 -0400252 * A large-format version of {@link #contentView}, giving the Notification an
Daniel Sandlerf3b73432012-03-27 15:01:25 -0400253 * opportunity to show more detail. The system UI may choose to show this
254 * instead of the normal content view at its discretion.
Daniel Sandlerf3b73432012-03-27 15:01:25 -0400255 */
256 public RemoteViews bigContentView;
257
Chris Wren8fd39ec2014-02-27 17:43:26 -0500258
259 /**
Chris Wren47c20a12014-06-18 17:27:29 -0400260 * A medium-format version of {@link #contentView}, providing the Notification an
261 * opportunity to add action buttons to contentView. At its discretion, the system UI may
262 * choose to show this as a heads-up notification, which will pop up so the user can see
263 * it without leaving their current activity.
Chris Wren8fd39ec2014-02-27 17:43:26 -0500264 */
265 public RemoteViews headsUpContentView;
266
Daniel Sandlerf3b73432012-03-27 15:01:25 -0400267 /**
Joe Onorato46439ce2010-11-19 13:56:21 -0800268 * The bitmap that may escape the bounds of the panel and bar.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800269 */
Joe Onorato46439ce2010-11-19 13:56:21 -0800270 public Bitmap largeIcon;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800271
272 /**
273 * The sound to play.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500274 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800275 * <p>
Chris Wren47c20a12014-06-18 17:27:29 -0400276 * A notification that is noisy is more likely to be presented as a heads-up notification.
277 * </p>
278 *
279 * <p>
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500280 * To play the default notification sound, see {@link #defaults}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800281 * </p>
282 */
283 public Uri sound;
284
285 /**
286 * Use this constant as the value for audioStreamType to request that
287 * the default stream type for notifications be used. Currently the
Jeff Sharkey098d5802012-04-26 17:30:34 -0700288 * default stream type is {@link AudioManager#STREAM_NOTIFICATION}.
John Spurlockc0650f022014-07-19 13:22:39 -0400289 *
290 * @deprecated Use {@link #audioAttributes} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800291 */
Jean-Michel Trivi81f871e2014-08-06 16:32:38 -0700292 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800293 public static final int STREAM_DEFAULT = -1;
294
295 /**
296 * The audio stream type to use when playing the sound.
297 * Should be one of the STREAM_ constants from
298 * {@link android.media.AudioManager}.
John Spurlockc0650f022014-07-19 13:22:39 -0400299 *
300 * @deprecated Use {@link #audioAttributes} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800301 */
Jean-Michel Trivi81f871e2014-08-06 16:32:38 -0700302 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800303 public int audioStreamType = STREAM_DEFAULT;
304
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800305 /**
John Spurlockc0650f022014-07-19 13:22:39 -0400306 * The default value of {@link #audioAttributes}.
307 */
308 public static final AudioAttributes AUDIO_ATTRIBUTES_DEFAULT = new AudioAttributes.Builder()
309 .setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
310 .setUsage(AudioAttributes.USAGE_NOTIFICATION)
311 .build();
312
313 /**
314 * The {@link AudioAttributes audio attributes} to use when playing the sound.
315 */
316 public AudioAttributes audioAttributes = AUDIO_ATTRIBUTES_DEFAULT;
317
318 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500319 * The pattern with which to vibrate.
320 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800321 * <p>
322 * To vibrate the default pattern, see {@link #defaults}.
323 * </p>
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500324 *
Chris Wren47c20a12014-06-18 17:27:29 -0400325 * <p>
326 * A notification that vibrates is more likely to be presented as a heads-up notification.
327 * </p>
328 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800329 * @see android.os.Vibrator#vibrate(long[],int)
330 */
331 public long[] vibrate;
332
333 /**
334 * The color of the led. The hardware will do its best approximation.
335 *
336 * @see #FLAG_SHOW_LIGHTS
337 * @see #flags
338 */
339 public int ledARGB;
340
341 /**
342 * The number of milliseconds for the LED to be on while it's flashing.
343 * The hardware will do its best approximation.
344 *
345 * @see #FLAG_SHOW_LIGHTS
346 * @see #flags
347 */
348 public int ledOnMS;
349
350 /**
351 * The number of milliseconds for the LED to be off while it's flashing.
352 * The hardware will do its best approximation.
353 *
354 * @see #FLAG_SHOW_LIGHTS
355 * @see #flags
356 */
357 public int ledOffMS;
358
359 /**
360 * Specifies which values should be taken from the defaults.
361 * <p>
362 * To set, OR the desired from {@link #DEFAULT_SOUND},
363 * {@link #DEFAULT_VIBRATE}, {@link #DEFAULT_LIGHTS}. For all default
364 * values, use {@link #DEFAULT_ALL}.
365 * </p>
366 */
367 public int defaults;
368
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800369 /**
370 * Bit to be bitwise-ored into the {@link #flags} field that should be
371 * set if you want the LED on for this notification.
372 * <ul>
373 * <li>To turn the LED off, pass 0 in the alpha channel for colorARGB
374 * or 0 for both ledOnMS and ledOffMS.</li>
375 * <li>To turn the LED on, pass 1 for ledOnMS and 0 for ledOffMS.</li>
376 * <li>To flash the LED, pass the number of milliseconds that it should
377 * be on and off to ledOnMS and ledOffMS.</li>
378 * </ul>
379 * <p>
380 * Since hardware varies, you are not guaranteed that any of the values
381 * you pass are honored exactly. Use the system defaults (TODO) if possible
382 * because they will be set to values that work on any given hardware.
383 * <p>
384 * The alpha channel must be set for forward compatibility.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500385 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800386 */
387 public static final int FLAG_SHOW_LIGHTS = 0x00000001;
388
389 /**
390 * Bit to be bitwise-ored into the {@link #flags} field that should be
391 * set if this notification is in reference to something that is ongoing,
392 * like a phone call. It should not be set if this notification is in
393 * reference to something that happened at a particular point in time,
394 * like a missed phone call.
395 */
396 public static final int FLAG_ONGOING_EVENT = 0x00000002;
397
398 /**
399 * Bit to be bitwise-ored into the {@link #flags} field that if set,
Scott Mainb8b36452009-04-26 15:50:49 -0700400 * the audio will be repeated until the notification is
401 * cancelled or the notification window is opened.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800402 */
403 public static final int FLAG_INSISTENT = 0x00000004;
404
405 /**
406 * Bit to be bitwise-ored into the {@link #flags} field that should be
Griff Hazen293977b2014-04-28 08:37:20 -0700407 * set if you would only like the sound, vibrate and ticker to be played
408 * if the notification was not already showing.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800409 */
410 public static final int FLAG_ONLY_ALERT_ONCE = 0x00000008;
411
412 /**
413 * Bit to be bitwise-ored into the {@link #flags} field that should be
414 * set if the notification should be canceled when it is clicked by the
Daniel Sandler8aa9ae62012-12-04 23:31:47 -0500415 * user.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500416
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800417 */
418 public static final int FLAG_AUTO_CANCEL = 0x00000010;
419
420 /**
421 * Bit to be bitwise-ored into the {@link #flags} field that should be
422 * set if the notification should not be canceled when the user clicks
423 * the Clear all button.
424 */
425 public static final int FLAG_NO_CLEAR = 0x00000020;
426
Dianne Hackbornd8a43f62009-08-17 23:33:56 -0700427 /**
428 * Bit to be bitwise-ored into the {@link #flags} field that should be
429 * set if this notification represents a currently running service. This
430 * will normally be set for you by {@link Service#startForeground}.
431 */
432 public static final int FLAG_FOREGROUND_SERVICE = 0x00000040;
433
Daniel Sandlere46cbd32010-06-17 10:35:26 -0400434 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500435 * Obsolete flag indicating high-priority notifications; use the priority field instead.
Joe Malin8d40d042012-11-05 11:36:40 -0800436 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500437 * @deprecated Use {@link #priority} with a positive value.
Daniel Sandlere46cbd32010-06-17 10:35:26 -0400438 */
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500439 public static final int FLAG_HIGH_PRIORITY = 0x00000080;
Daniel Sandlere46cbd32010-06-17 10:35:26 -0400440
Griff Hazendfcb0802014-02-11 12:00:00 -0800441 /**
442 * Bit to be bitswise-ored into the {@link #flags} field that should be
443 * set if this notification is relevant to the current device only
444 * and it is not recommended that it bridge to other devices.
445 */
446 public static final int FLAG_LOCAL_ONLY = 0x00000100;
447
Griff Hazen5cadc3b2014-05-20 09:55:39 -0700448 /**
449 * Bit to be bitswise-ored into the {@link #flags} field that should be
450 * set if this notification is the group summary for a group of notifications.
451 * Grouped notifications may display in a cluster or stack on devices which
452 * support such rendering. Requires a group key also be set using {@link Builder#setGroup}.
453 */
454 public static final int FLAG_GROUP_SUMMARY = 0x00000200;
455
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800456 public int flags;
457
Tor Norbyed9273d62013-05-30 15:59:53 -0700458 /** @hide */
459 @IntDef({PRIORITY_DEFAULT,PRIORITY_LOW,PRIORITY_MIN,PRIORITY_HIGH,PRIORITY_MAX})
460 @Retention(RetentionPolicy.SOURCE)
461 public @interface Priority {}
462
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800463 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500464 * Default notification {@link #priority}. If your application does not prioritize its own
465 * notifications, use this value for all notifications.
466 */
467 public static final int PRIORITY_DEFAULT = 0;
468
469 /**
470 * Lower {@link #priority}, for items that are less important. The UI may choose to show these
471 * items smaller, or at a different position in the list, compared with your app's
472 * {@link #PRIORITY_DEFAULT} items.
473 */
474 public static final int PRIORITY_LOW = -1;
475
476 /**
477 * Lowest {@link #priority}; these items might not be shown to the user except under special
478 * circumstances, such as detailed notification logs.
479 */
480 public static final int PRIORITY_MIN = -2;
481
482 /**
483 * Higher {@link #priority}, for more important notifications or alerts. The UI may choose to
484 * show these items larger, or at a different position in notification lists, compared with
485 * your app's {@link #PRIORITY_DEFAULT} items.
486 */
487 public static final int PRIORITY_HIGH = 1;
488
489 /**
490 * Highest {@link #priority}, for your application's most important items that require the
491 * user's prompt attention or input.
492 */
493 public static final int PRIORITY_MAX = 2;
494
495 /**
496 * Relative priority for this notification.
Joe Malin8d40d042012-11-05 11:36:40 -0800497 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500498 * Priority is an indication of how much of the user's valuable attention should be consumed by
499 * this notification. Low-priority notifications may be hidden from the user in certain
500 * situations, while the user might be interrupted for a higher-priority notification. The
Daniel Sandler6738eee2012-11-16 12:03:32 -0500501 * system will make a determination about how to interpret this priority when presenting
502 * the notification.
Chris Wren47c20a12014-06-18 17:27:29 -0400503 *
504 * <p>
505 * A notification that is at least {@link #PRIORITY_HIGH} is more likely to be presented
506 * as a heads-up notification.
507 * </p>
508 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500509 */
Tor Norbyed9273d62013-05-30 15:59:53 -0700510 @Priority
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500511 public int priority;
Joe Malin8d40d042012-11-05 11:36:40 -0800512
Dan Sandler26e81cf2014-05-06 10:01:27 -0400513 /**
514 * Accent color (an ARGB integer like the constants in {@link android.graphics.Color})
515 * to be applied by the standard Style templates when presenting this notification.
516 *
517 * The current template design constructs a colorful header image by overlaying the
518 * {@link #icon} image (stenciled in white) atop a field of this color. Alpha components are
519 * ignored.
520 */
521 public int color = COLOR_DEFAULT;
522
523 /**
524 * Special value of {@link #color} telling the system not to decorate this notification with
525 * any special color but instead use default colors when presenting this notification.
526 */
527 public static final int COLOR_DEFAULT = 0; // AKA Color.TRANSPARENT
Dan Sandler0bf2ed82013-12-21 23:33:41 -0600528
529 /**
530 * Sphere of visibility of this notification, which affects how and when the SystemUI reveals
531 * the notification's presence and contents in untrusted situations (namely, on the secure
532 * lockscreen).
533 *
534 * The default level, {@link #VISIBILITY_PRIVATE}, behaves exactly as notifications have always
535 * done on Android: The notification's {@link #icon} and {@link #tickerText} (if available) are
536 * shown in all situations, but the contents are only available if the device is unlocked for
537 * the appropriate user.
538 *
539 * A more permissive policy can be expressed by {@link #VISIBILITY_PUBLIC}; such a notification
540 * can be read even in an "insecure" context (that is, above a secure lockscreen).
541 * To modify the public version of this notification—for example, to redact some portions—see
542 * {@link Builder#setPublicVersion(Notification)}.
543 *
544 * Finally, a notification can be made {@link #VISIBILITY_SECRET}, which will suppress its icon
545 * and ticker until the user has bypassed the lockscreen.
546 */
547 public int visibility;
548
Griff Hazenfc3922d2014-08-20 11:56:44 -0700549 /**
550 * Notification visibility: Show this notification in its entirety on all lockscreens.
551 *
552 * {@see #visibility}
553 */
Dan Sandler0bf2ed82013-12-21 23:33:41 -0600554 public static final int VISIBILITY_PUBLIC = 1;
Griff Hazenfc3922d2014-08-20 11:56:44 -0700555
556 /**
557 * Notification visibility: Show this notification on all lockscreens, but conceal sensitive or
558 * private information on secure lockscreens.
559 *
560 * {@see #visibility}
561 */
Dan Sandler0bf2ed82013-12-21 23:33:41 -0600562 public static final int VISIBILITY_PRIVATE = 0;
Griff Hazenfc3922d2014-08-20 11:56:44 -0700563
564 /**
565 * Notification visibility: Do not reveal any part of this notification on a secure lockscreen.
566 *
567 * {@see #visibility}
568 */
Dan Sandler0bf2ed82013-12-21 23:33:41 -0600569 public static final int VISIBILITY_SECRET = -1;
570
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500571 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400572 * Notification category: incoming call (voice or video) or similar synchronous communication request.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500573 */
John Spurlockfd7f1e02014-03-18 16:41:57 -0400574 public static final String CATEGORY_CALL = "call";
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500575
576 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400577 * Notification category: incoming direct message (SMS, instant message, etc.).
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500578 */
John Spurlockfd7f1e02014-03-18 16:41:57 -0400579 public static final String CATEGORY_MESSAGE = "msg";
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500580
581 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400582 * Notification category: asynchronous bulk message (email).
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500583 */
John Spurlockfd7f1e02014-03-18 16:41:57 -0400584 public static final String CATEGORY_EMAIL = "email";
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500585
586 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400587 * Notification category: calendar event.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500588 */
John Spurlockfd7f1e02014-03-18 16:41:57 -0400589 public static final String CATEGORY_EVENT = "event";
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500590
591 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400592 * Notification category: promotion or advertisement.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500593 */
John Spurlockfd7f1e02014-03-18 16:41:57 -0400594 public static final String CATEGORY_PROMO = "promo";
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500595
596 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400597 * Notification category: alarm or timer.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500598 */
John Spurlockfd7f1e02014-03-18 16:41:57 -0400599 public static final String CATEGORY_ALARM = "alarm";
600
601 /**
602 * Notification category: progress of a long-running background operation.
603 */
604 public static final String CATEGORY_PROGRESS = "progress";
605
606 /**
607 * Notification category: social network or sharing update.
608 */
609 public static final String CATEGORY_SOCIAL = "social";
610
611 /**
612 * Notification category: error in background operation or authentication status.
613 */
614 public static final String CATEGORY_ERROR = "err";
615
616 /**
617 * Notification category: media transport control for playback.
618 */
619 public static final String CATEGORY_TRANSPORT = "transport";
620
621 /**
622 * Notification category: system or device status update. Reserved for system use.
623 */
624 public static final String CATEGORY_SYSTEM = "sys";
625
626 /**
627 * Notification category: indication of running background service.
628 */
629 public static final String CATEGORY_SERVICE = "service";
630
631 /**
John Spurlock0a69c8c2014-03-21 13:30:57 -0400632 * Notification category: a specific, timely recommendation for a single thing.
633 * For example, a news app might want to recommend a news story it believes the user will
634 * want to read next.
635 */
636 public static final String CATEGORY_RECOMMENDATION = "recommendation";
637
638 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400639 * Notification category: ongoing information about device or contextual status.
640 */
641 public static final String CATEGORY_STATUS = "status";
642
643 /**
644 * One of the predefined notification categories (see the <code>CATEGORY_*</code> constants)
645 * that best describes this Notification. May be used by the system for ranking and filtering.
646 */
647 public String category;
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500648
Griff Hazen5cadc3b2014-05-20 09:55:39 -0700649 private String mGroupKey;
650
651 /**
652 * Get the key used to group this notification into a cluster or stack
653 * with other notifications on devices which support such rendering.
654 */
655 public String getGroup() {
656 return mGroupKey;
657 }
658
659 private String mSortKey;
660
661 /**
662 * Get a sort key that orders this notification among other notifications from the
663 * same package. This can be useful if an external sort was already applied and an app
664 * would like to preserve this. Notifications will be sorted lexicographically using this
665 * value, although providing different priorities in addition to providing sort key may
666 * cause this value to be ignored.
667 *
668 * <p>This sort key can also be used to order members of a notification group. See
669 * {@link Builder#setGroup}.
670 *
671 * @see String#compareTo(String)
672 */
673 public String getSortKey() {
674 return mSortKey;
675 }
676
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500677 /**
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400678 * Additional semantic data to be carried around with this Notification.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400679 * <p>
680 * The extras keys defined here are intended to capture the original inputs to {@link Builder}
681 * APIs, and are intended to be used by
682 * {@link android.service.notification.NotificationListenerService} implementations to extract
683 * detailed information from notification objects.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500684 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400685 public Bundle extras = new Bundle();
686
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400687 /**
688 * {@link #extras} key: this is the title of the notification,
689 * as supplied to {@link Builder#setContentTitle(CharSequence)}.
690 */
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -0500691 public static final String EXTRA_TITLE = "android.title";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400692
693 /**
694 * {@link #extras} key: this is the title of the notification when shown in expanded form,
695 * e.g. as supplied to {@link BigTextStyle#setBigContentTitle(CharSequence)}.
696 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400697 public static final String EXTRA_TITLE_BIG = EXTRA_TITLE + ".big";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400698
699 /**
700 * {@link #extras} key: this is the main text payload, as supplied to
701 * {@link Builder#setContentText(CharSequence)}.
702 */
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -0500703 public static final String EXTRA_TEXT = "android.text";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400704
705 /**
706 * {@link #extras} key: this is a third line of text, as supplied to
707 * {@link Builder#setSubText(CharSequence)}.
708 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400709 public static final String EXTRA_SUB_TEXT = "android.subText";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400710
711 /**
712 * {@link #extras} key: this is a small piece of additional text as supplied to
713 * {@link Builder#setContentInfo(CharSequence)}.
714 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400715 public static final String EXTRA_INFO_TEXT = "android.infoText";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400716
717 /**
718 * {@link #extras} key: this is a line of summary information intended to be shown
719 * alongside expanded notifications, as supplied to (e.g.)
720 * {@link BigTextStyle#setSummaryText(CharSequence)}.
721 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400722 public static final String EXTRA_SUMMARY_TEXT = "android.summaryText";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400723
724 /**
Christoph Studer4600f9b2014-07-22 22:44:43 +0200725 * {@link #extras} key: this is the longer text shown in the big form of a
726 * {@link BigTextStyle} notification, as supplied to
727 * {@link BigTextStyle#bigText(CharSequence)}.
728 */
729 public static final String EXTRA_BIG_TEXT = "android.bigText";
730
731 /**
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400732 * {@link #extras} key: this is the resource ID of the notification's main small icon, as
733 * supplied to {@link Builder#setSmallIcon(int)}.
734 */
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -0500735 public static final String EXTRA_SMALL_ICON = "android.icon";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400736
737 /**
738 * {@link #extras} key: this is a bitmap to be used instead of the small icon when showing the
739 * notification payload, as
740 * supplied to {@link Builder#setLargeIcon(android.graphics.Bitmap)}.
741 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400742 public static final String EXTRA_LARGE_ICON = "android.largeIcon";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400743
744 /**
745 * {@link #extras} key: this is a bitmap to be used instead of the one from
746 * {@link Builder#setLargeIcon(android.graphics.Bitmap)} when the notification is
747 * shown in its expanded form, as supplied to
748 * {@link BigPictureStyle#bigLargeIcon(android.graphics.Bitmap)}.
749 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400750 public static final String EXTRA_LARGE_ICON_BIG = EXTRA_LARGE_ICON + ".big";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400751
752 /**
753 * {@link #extras} key: this is the progress value supplied to
754 * {@link Builder#setProgress(int, int, boolean)}.
755 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400756 public static final String EXTRA_PROGRESS = "android.progress";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400757
758 /**
759 * {@link #extras} key: this is the maximum value supplied to
760 * {@link Builder#setProgress(int, int, boolean)}.
761 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400762 public static final String EXTRA_PROGRESS_MAX = "android.progressMax";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400763
764 /**
765 * {@link #extras} key: whether the progress bar is indeterminate, supplied to
766 * {@link Builder#setProgress(int, int, boolean)}.
767 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400768 public static final String EXTRA_PROGRESS_INDETERMINATE = "android.progressIndeterminate";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400769
770 /**
771 * {@link #extras} key: whether {@link #when} should be shown as a count-up timer (specifically
772 * a {@link android.widget.Chronometer}) instead of a timestamp, as supplied to
773 * {@link Builder#setUsesChronometer(boolean)}.
774 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400775 public static final String EXTRA_SHOW_CHRONOMETER = "android.showChronometer";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400776
777 /**
778 * {@link #extras} key: whether {@link #when} should be shown,
779 * as supplied to {@link Builder#setShowWhen(boolean)}.
780 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400781 public static final String EXTRA_SHOW_WHEN = "android.showWhen";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400782
783 /**
784 * {@link #extras} key: this is a bitmap to be shown in {@link BigPictureStyle} expanded
785 * notifications, supplied to {@link BigPictureStyle#bigPicture(android.graphics.Bitmap)}.
786 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400787 public static final String EXTRA_PICTURE = "android.picture";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400788
789 /**
790 * {@link #extras} key: An array of CharSequences to show in {@link InboxStyle} expanded
791 * notifications, each of which was supplied to {@link InboxStyle#addLine(CharSequence)}.
792 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400793 public static final String EXTRA_TEXT_LINES = "android.textLines";
Dan Sandler842dd772014-05-15 09:36:47 -0400794
795 /**
796 * {@link #extras} key: A string representing the name of the specific
797 * {@link android.app.Notification.Style} used to create this notification.
798 */
Chris Wren91ad5632013-06-05 15:05:57 -0400799 public static final String EXTRA_TEMPLATE = "android.template";
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400800
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400801 /**
Chris Wrene6c48932014-09-29 17:19:27 -0400802 * {@link #extras} key: A String array containing the people that this notification relates to,
803 * each of which was supplied to {@link Builder#addPerson(String)}.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400804 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400805 public static final String EXTRA_PEOPLE = "android.people";
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500806
807 /**
Chris Wren47c20a12014-06-18 17:27:29 -0400808 * {@link #extras} key: used to provide hints about the appropriateness of
809 * displaying this notification as a heads-up notification.
Chris Wren51c75102013-07-16 20:49:17 -0400810 * @hide
811 */
812 public static final String EXTRA_AS_HEADS_UP = "headsup";
813
814 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400815 * Allow certain system-generated notifications to appear before the device is provisioned.
816 * Only available to notifications coming from the android package.
817 * @hide
818 */
819 public static final String EXTRA_ALLOW_DURING_SETUP = "android.allowDuringSetup";
820
821 /**
Jose Limae9e3b3b2014-05-18 23:44:50 -0700822 * {@link #extras} key: A
823 * {@link android.content.ContentUris content URI} pointing to an image that can be displayed
824 * in the background when the notification is selected. The URI must point to an image stream
825 * suitable for passing into
826 * {@link android.graphics.BitmapFactory#decodeStream(java.io.InputStream)
827 * BitmapFactory.decodeStream}; all other content types will be ignored. The content provider
828 * URI used for this purpose must require no permissions to read the image data.
829 */
830 public static final String EXTRA_BACKGROUND_IMAGE_URI = "android.backgroundImageUri";
831
832 /**
Dan Sandler842dd772014-05-15 09:36:47 -0400833 * {@link #extras} key: A
Jeff Browndba34ba2014-06-24 20:46:03 -0700834 * {@link android.media.session.MediaSession.Token} associated with a
Dan Sandler842dd772014-05-15 09:36:47 -0400835 * {@link android.app.Notification.MediaStyle} notification.
836 */
837 public static final String EXTRA_MEDIA_SESSION = "android.mediaSession";
838
839 /**
Bryan Mawhinneye191f902014-07-22 12:50:09 +0100840 * {@link #extras} key: the indices of actions to be shown in the compact view,
841 * as supplied to (e.g.) {@link MediaStyle#setShowActionsInCompactView(int...)}.
842 */
843 public static final String EXTRA_COMPACT_ACTIONS = "android.compactActions";
844
Christoph Studer943aa672014-08-03 20:31:16 +0200845 /**
Kenny Guy8942bcd2014-09-08 21:09:47 +0100846 * {@link #extras} key: the user that built the notification.
847 *
848 * @hide
849 */
850 public static final String EXTRA_ORIGINATING_USERID = "android.originatingUserId";
851
852 /**
Chris Wren47c20a12014-06-18 17:27:29 -0400853 * Value for {@link #EXTRA_AS_HEADS_UP} that indicates this notification should not be
854 * displayed in the heads up space.
855 *
856 * <p>
857 * If this notification has a {@link #fullScreenIntent}, then it will always launch the
858 * full-screen intent when posted.
859 * </p>
Chris Wren51c75102013-07-16 20:49:17 -0400860 * @hide
861 */
862 public static final int HEADS_UP_NEVER = 0;
863
864 /**
Chris Wren47c20a12014-06-18 17:27:29 -0400865 * Default value for {@link #EXTRA_AS_HEADS_UP} that indicates this notification may be
866 * displayed as a heads up.
Chris Wren51c75102013-07-16 20:49:17 -0400867 * @hide
868 */
869 public static final int HEADS_UP_ALLOWED = 1;
870
871 /**
Chris Wren47c20a12014-06-18 17:27:29 -0400872 * Value for {@link #EXTRA_AS_HEADS_UP} that indicates this notification is a
873 * good candidate for display as a heads up.
Chris Wren51c75102013-07-16 20:49:17 -0400874 * @hide
875 */
876 public static final int HEADS_UP_REQUESTED = 2;
877
878 /**
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400879 * Structure to encapsulate a named action that can be shown as part of this notification.
880 * It must include an icon, a label, and a {@link PendingIntent} to be fired when the action is
881 * selected by the user.
882 * <p>
Griff Hazen959591e2014-05-15 22:26:18 -0700883 * Apps should use {@link Notification.Builder#addAction(int, CharSequence, PendingIntent)}
884 * or {@link Notification.Builder#addAction(Notification.Action)}
885 * to attach actions.
Daniel Sandlera0a938c2012-03-15 08:42:37 -0400886 */
Daniel Sandlerea2a3172013-02-20 22:24:20 -0500887 public static class Action implements Parcelable {
Griff Hazen959591e2014-05-15 22:26:18 -0700888 private final Bundle mExtras;
Griff Hazen61a9e862014-05-22 16:05:19 -0700889 private final RemoteInput[] mRemoteInputs;
Griff Hazen959591e2014-05-15 22:26:18 -0700890
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400891 /**
892 * Small icon representing the action.
893 */
Daniel Sandlera0a938c2012-03-15 08:42:37 -0400894 public int icon;
Griff Hazen5cadc3b2014-05-20 09:55:39 -0700895
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400896 /**
897 * Title of the action.
898 */
Daniel Sandlera0a938c2012-03-15 08:42:37 -0400899 public CharSequence title;
Griff Hazen5cadc3b2014-05-20 09:55:39 -0700900
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400901 /**
902 * Intent to send when the user invokes this action. May be null, in which case the action
903 * may be rendered in a disabled presentation by the system UI.
904 */
Daniel Sandlera0a938c2012-03-15 08:42:37 -0400905 public PendingIntent actionIntent;
Griff Hazen959591e2014-05-15 22:26:18 -0700906
Daniel Sandlera0a938c2012-03-15 08:42:37 -0400907 private Action(Parcel in) {
908 icon = in.readInt();
909 title = TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(in);
910 if (in.readInt() == 1) {
911 actionIntent = PendingIntent.CREATOR.createFromParcel(in);
912 }
Griff Hazen959591e2014-05-15 22:26:18 -0700913 mExtras = in.readBundle();
Griff Hazen5cadc3b2014-05-20 09:55:39 -0700914 mRemoteInputs = in.createTypedArray(RemoteInput.CREATOR);
Daniel Sandlera0a938c2012-03-15 08:42:37 -0400915 }
Griff Hazen5cadc3b2014-05-20 09:55:39 -0700916
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400917 /**
Griff Hazen959591e2014-05-15 22:26:18 -0700918 * Use {@link Notification.Builder#addAction(int, CharSequence, PendingIntent)}.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400919 */
920 public Action(int icon, CharSequence title, PendingIntent intent) {
Griff Hazen5cadc3b2014-05-20 09:55:39 -0700921 this(icon, title, intent, new Bundle(), null);
Griff Hazen959591e2014-05-15 22:26:18 -0700922 }
923
Griff Hazen5cadc3b2014-05-20 09:55:39 -0700924 private Action(int icon, CharSequence title, PendingIntent intent, Bundle extras,
925 RemoteInput[] remoteInputs) {
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400926 this.icon = icon;
927 this.title = title;
928 this.actionIntent = intent;
Griff Hazen959591e2014-05-15 22:26:18 -0700929 this.mExtras = extras != null ? extras : new Bundle();
Griff Hazen5cadc3b2014-05-20 09:55:39 -0700930 this.mRemoteInputs = remoteInputs;
Griff Hazen959591e2014-05-15 22:26:18 -0700931 }
932
933 /**
934 * Get additional metadata carried around with this Action.
935 */
936 public Bundle getExtras() {
937 return mExtras;
938 }
939
940 /**
Griff Hazen5cadc3b2014-05-20 09:55:39 -0700941 * Get the list of inputs to be collected from the user when this action is sent.
942 * May return null if no remote inputs were added.
943 */
944 public RemoteInput[] getRemoteInputs() {
945 return mRemoteInputs;
946 }
947
948 /**
Griff Hazen959591e2014-05-15 22:26:18 -0700949 * Builder class for {@link Action} objects.
950 */
Griff Hazen5cadc3b2014-05-20 09:55:39 -0700951 public static final class Builder {
Griff Hazen959591e2014-05-15 22:26:18 -0700952 private final int mIcon;
953 private final CharSequence mTitle;
954 private final PendingIntent mIntent;
955 private final Bundle mExtras;
Griff Hazen5cadc3b2014-05-20 09:55:39 -0700956 private ArrayList<RemoteInput> mRemoteInputs;
Griff Hazen959591e2014-05-15 22:26:18 -0700957
958 /**
959 * Construct a new builder for {@link Action} object.
960 * @param icon icon to show for this action
961 * @param title the title of the action
962 * @param intent the {@link PendingIntent} to fire when users trigger this action
963 */
964 public Builder(int icon, CharSequence title, PendingIntent intent) {
Griff Hazen5cadc3b2014-05-20 09:55:39 -0700965 this(icon, title, intent, new Bundle(), null);
Griff Hazen959591e2014-05-15 22:26:18 -0700966 }
967
968 /**
969 * Construct a new builder for {@link Action} object using the fields from an
970 * {@link Action}.
971 * @param action the action to read fields from.
972 */
973 public Builder(Action action) {
Griff Hazen5cadc3b2014-05-20 09:55:39 -0700974 this(action.icon, action.title, action.actionIntent, new Bundle(action.mExtras),
975 action.getRemoteInputs());
Griff Hazen959591e2014-05-15 22:26:18 -0700976 }
977
Griff Hazen5cadc3b2014-05-20 09:55:39 -0700978 private Builder(int icon, CharSequence title, PendingIntent intent, Bundle extras,
979 RemoteInput[] remoteInputs) {
Griff Hazen959591e2014-05-15 22:26:18 -0700980 mIcon = icon;
981 mTitle = title;
982 mIntent = intent;
983 mExtras = extras;
Griff Hazen5cadc3b2014-05-20 09:55:39 -0700984 if (remoteInputs != null) {
985 mRemoteInputs = new ArrayList<RemoteInput>(remoteInputs.length);
986 Collections.addAll(mRemoteInputs, remoteInputs);
987 }
Griff Hazen959591e2014-05-15 22:26:18 -0700988 }
989
990 /**
991 * Merge additional metadata into this builder.
992 *
993 * <p>Values within the Bundle will replace existing extras values in this Builder.
994 *
995 * @see Notification.Action#extras
996 */
997 public Builder addExtras(Bundle extras) {
998 if (extras != null) {
999 mExtras.putAll(extras);
1000 }
1001 return this;
1002 }
1003
1004 /**
1005 * Get the metadata Bundle used by this Builder.
1006 *
1007 * <p>The returned Bundle is shared with this Builder.
1008 */
1009 public Bundle getExtras() {
1010 return mExtras;
1011 }
1012
1013 /**
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001014 * Add an input to be collected from the user when this action is sent.
1015 * Response values can be retrieved from the fired intent by using the
1016 * {@link RemoteInput#getResultsFromIntent} function.
1017 * @param remoteInput a {@link RemoteInput} to add to the action
1018 * @return this object for method chaining
1019 */
1020 public Builder addRemoteInput(RemoteInput remoteInput) {
1021 if (mRemoteInputs == null) {
1022 mRemoteInputs = new ArrayList<RemoteInput>();
1023 }
1024 mRemoteInputs.add(remoteInput);
1025 return this;
1026 }
1027
1028 /**
1029 * Apply an extender to this action builder. Extenders may be used to add
1030 * metadata or change options on this builder.
1031 */
Griff Hazen61a9e862014-05-22 16:05:19 -07001032 public Builder extend(Extender extender) {
1033 extender.extend(this);
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001034 return this;
1035 }
1036
1037 /**
Griff Hazen959591e2014-05-15 22:26:18 -07001038 * Combine all of the options that have been set and return a new {@link Action}
1039 * object.
1040 * @return the built action
1041 */
1042 public Action build() {
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001043 RemoteInput[] remoteInputs = mRemoteInputs != null
1044 ? mRemoteInputs.toArray(new RemoteInput[mRemoteInputs.size()]) : null;
1045 return new Action(mIcon, mTitle, mIntent, mExtras, remoteInputs);
Griff Hazen959591e2014-05-15 22:26:18 -07001046 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001047 }
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001048
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001049 @Override
1050 public Action clone() {
1051 return new Action(
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001052 icon,
1053 title,
1054 actionIntent, // safe to alias
1055 new Bundle(mExtras),
1056 getRemoteInputs());
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001057 }
1058 @Override
1059 public int describeContents() {
1060 return 0;
1061 }
1062 @Override
1063 public void writeToParcel(Parcel out, int flags) {
1064 out.writeInt(icon);
1065 TextUtils.writeToParcel(title, out, flags);
1066 if (actionIntent != null) {
1067 out.writeInt(1);
1068 actionIntent.writeToParcel(out, flags);
1069 } else {
1070 out.writeInt(0);
1071 }
Griff Hazen959591e2014-05-15 22:26:18 -07001072 out.writeBundle(mExtras);
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001073 out.writeTypedArray(mRemoteInputs, flags);
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001074 }
Griff Hazen959591e2014-05-15 22:26:18 -07001075 public static final Parcelable.Creator<Action> CREATOR =
1076 new Parcelable.Creator<Action>() {
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001077 public Action createFromParcel(Parcel in) {
1078 return new Action(in);
1079 }
1080 public Action[] newArray(int size) {
1081 return new Action[size];
1082 }
1083 };
Griff Hazen61a9e862014-05-22 16:05:19 -07001084
1085 /**
1086 * Extender interface for use with {@link Builder#extend}. Extenders may be used to add
1087 * metadata or change options on an action builder.
1088 */
1089 public interface Extender {
1090 /**
1091 * Apply this extender to a notification action builder.
1092 * @param builder the builder to be modified.
1093 * @return the build object for chaining.
1094 */
1095 public Builder extend(Builder builder);
1096 }
1097
1098 /**
1099 * Wearable extender for notification actions. To add extensions to an action,
1100 * create a new {@link android.app.Notification.Action.WearableExtender} object using
1101 * the {@code WearableExtender()} constructor and apply it to a
1102 * {@link android.app.Notification.Action.Builder} using
1103 * {@link android.app.Notification.Action.Builder#extend}.
1104 *
1105 * <pre class="prettyprint">
1106 * Notification.Action action = new Notification.Action.Builder(
1107 * R.drawable.archive_all, "Archive all", actionIntent)
Griff Hazen14f57992014-05-26 09:07:14 -07001108 * .extend(new Notification.Action.WearableExtender()
Griff Hazen61a9e862014-05-22 16:05:19 -07001109 * .setAvailableOffline(false))
Griff Hazen14f57992014-05-26 09:07:14 -07001110 * .build();</pre>
Griff Hazen61a9e862014-05-22 16:05:19 -07001111 */
1112 public static final class WearableExtender implements Extender {
1113 /** Notification action extra which contains wearable extensions */
1114 private static final String EXTRA_WEARABLE_EXTENSIONS = "android.wearable.EXTENSIONS";
1115
1116 private static final String KEY_FLAGS = "flags";
1117
1118 // Flags bitwise-ored to mFlags
1119 private static final int FLAG_AVAILABLE_OFFLINE = 0x1;
1120
1121 // Default value for flags integer
1122 private static final int DEFAULT_FLAGS = FLAG_AVAILABLE_OFFLINE;
1123
1124 private int mFlags = DEFAULT_FLAGS;
1125
1126 /**
1127 * Create a {@link android.app.Notification.Action.WearableExtender} with default
1128 * options.
1129 */
1130 public WearableExtender() {
1131 }
1132
1133 /**
1134 * Create a {@link android.app.Notification.Action.WearableExtender} by reading
1135 * wearable options present in an existing notification action.
1136 * @param action the notification action to inspect.
1137 */
1138 public WearableExtender(Action action) {
1139 Bundle wearableBundle = action.getExtras().getBundle(EXTRA_WEARABLE_EXTENSIONS);
1140 if (wearableBundle != null) {
1141 mFlags = wearableBundle.getInt(KEY_FLAGS, DEFAULT_FLAGS);
1142 }
1143 }
1144
1145 /**
1146 * Apply wearable extensions to a notification action that is being built. This is
1147 * typically called by the {@link android.app.Notification.Action.Builder#extend}
1148 * method of {@link android.app.Notification.Action.Builder}.
1149 */
1150 @Override
1151 public Action.Builder extend(Action.Builder builder) {
1152 Bundle wearableBundle = new Bundle();
1153
1154 if (mFlags != DEFAULT_FLAGS) {
1155 wearableBundle.putInt(KEY_FLAGS, mFlags);
1156 }
1157
1158 builder.getExtras().putBundle(EXTRA_WEARABLE_EXTENSIONS, wearableBundle);
1159 return builder;
1160 }
1161
1162 @Override
1163 public WearableExtender clone() {
1164 WearableExtender that = new WearableExtender();
1165 that.mFlags = this.mFlags;
1166 return that;
1167 }
1168
1169 /**
1170 * Set whether this action is available when the wearable device is not connected to
1171 * a companion device. The user can still trigger this action when the wearable device is
1172 * offline, but a visual hint will indicate that the action may not be available.
1173 * Defaults to true.
1174 */
1175 public WearableExtender setAvailableOffline(boolean availableOffline) {
1176 setFlag(FLAG_AVAILABLE_OFFLINE, availableOffline);
1177 return this;
1178 }
1179
1180 /**
1181 * Get whether this action is available when the wearable device is not connected to
1182 * a companion device. The user can still trigger this action when the wearable device is
1183 * offline, but a visual hint will indicate that the action may not be available.
1184 * Defaults to true.
1185 */
1186 public boolean isAvailableOffline() {
1187 return (mFlags & FLAG_AVAILABLE_OFFLINE) != 0;
1188 }
1189
1190 private void setFlag(int mask, boolean value) {
1191 if (value) {
1192 mFlags |= mask;
1193 } else {
1194 mFlags &= ~mask;
1195 }
1196 }
1197 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001198 }
1199
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001200 /**
1201 * Array of all {@link Action} structures attached to this notification by
1202 * {@link Builder#addAction(int, CharSequence, PendingIntent)}. Mostly useful for instances of
1203 * {@link android.service.notification.NotificationListenerService} that provide an alternative
1204 * interface for invoking actions.
1205 */
Daniel Sandlerea2a3172013-02-20 22:24:20 -05001206 public Action[] actions;
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001207
1208 /**
Dan Sandler0bf2ed82013-12-21 23:33:41 -06001209 * Replacement version of this notification whose content will be shown
1210 * in an insecure context such as atop a secure keyguard. See {@link #visibility}
1211 * and {@link #VISIBILITY_PUBLIC}.
1212 */
1213 public Notification publicVersion;
1214
1215 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001216 * Constructs a Notification object with default values.
Joe Onorato46439ce2010-11-19 13:56:21 -08001217 * You might want to consider using {@link Builder} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001218 */
1219 public Notification()
1220 {
1221 this.when = System.currentTimeMillis();
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001222 this.priority = PRIORITY_DEFAULT;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001223 }
1224
1225 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001226 * @hide
1227 */
1228 public Notification(Context context, int icon, CharSequence tickerText, long when,
1229 CharSequence contentTitle, CharSequence contentText, Intent contentIntent)
1230 {
1231 this.when = when;
1232 this.icon = icon;
1233 this.tickerText = tickerText;
1234 setLatestEventInfo(context, contentTitle, contentText,
1235 PendingIntent.getActivity(context, 0, contentIntent, 0));
1236 }
1237
1238 /**
1239 * Constructs a Notification object with the information needed to
1240 * have a status bar icon without the standard expanded view.
1241 *
1242 * @param icon The resource id of the icon to put in the status bar.
1243 * @param tickerText The text that flows by in the status bar when the notification first
1244 * activates.
1245 * @param when The time to show in the time field. In the System.currentTimeMillis
1246 * timebase.
Joe Onorato46439ce2010-11-19 13:56:21 -08001247 *
1248 * @deprecated Use {@link Builder} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001249 */
Joe Onorato46439ce2010-11-19 13:56:21 -08001250 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001251 public Notification(int icon, CharSequence tickerText, long when)
1252 {
1253 this.icon = icon;
1254 this.tickerText = tickerText;
1255 this.when = when;
1256 }
1257
1258 /**
1259 * Unflatten the notification from a parcel.
1260 */
1261 public Notification(Parcel parcel)
1262 {
1263 int version = parcel.readInt();
1264
1265 when = parcel.readLong();
1266 icon = parcel.readInt();
1267 number = parcel.readInt();
1268 if (parcel.readInt() != 0) {
1269 contentIntent = PendingIntent.CREATOR.createFromParcel(parcel);
1270 }
1271 if (parcel.readInt() != 0) {
1272 deleteIntent = PendingIntent.CREATOR.createFromParcel(parcel);
1273 }
1274 if (parcel.readInt() != 0) {
1275 tickerText = TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(parcel);
1276 }
1277 if (parcel.readInt() != 0) {
Joe Onorato46439ce2010-11-19 13:56:21 -08001278 tickerView = RemoteViews.CREATOR.createFromParcel(parcel);
Joe Onoratoef1e7762010-09-17 18:38:38 -04001279 }
1280 if (parcel.readInt() != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001281 contentView = RemoteViews.CREATOR.createFromParcel(parcel);
1282 }
Joe Onorato561d3852010-11-20 18:09:34 -08001283 if (parcel.readInt() != 0) {
1284 largeIcon = Bitmap.CREATOR.createFromParcel(parcel);
1285 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001286 defaults = parcel.readInt();
1287 flags = parcel.readInt();
1288 if (parcel.readInt() != 0) {
1289 sound = Uri.CREATOR.createFromParcel(parcel);
1290 }
1291
1292 audioStreamType = parcel.readInt();
John Spurlockc0650f022014-07-19 13:22:39 -04001293 if (parcel.readInt() != 0) {
1294 audioAttributes = AudioAttributes.CREATOR.createFromParcel(parcel);
1295 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001296 vibrate = parcel.createLongArray();
1297 ledARGB = parcel.readInt();
1298 ledOnMS = parcel.readInt();
1299 ledOffMS = parcel.readInt();
1300 iconLevel = parcel.readInt();
Daniel Sandlere46cbd32010-06-17 10:35:26 -04001301
1302 if (parcel.readInt() != 0) {
1303 fullScreenIntent = PendingIntent.CREATOR.createFromParcel(parcel);
1304 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001305
1306 priority = parcel.readInt();
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001307
John Spurlockfd7f1e02014-03-18 16:41:57 -04001308 category = parcel.readString();
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001309
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001310 mGroupKey = parcel.readString();
1311
1312 mSortKey = parcel.readString();
1313
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001314 extras = parcel.readBundle(); // may be null
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001315
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001316 actions = parcel.createTypedArray(Action.CREATOR); // may be null
1317
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001318 if (parcel.readInt() != 0) {
Daniel Sandlerf3b73432012-03-27 15:01:25 -04001319 bigContentView = RemoteViews.CREATOR.createFromParcel(parcel);
1320 }
Dan Sandler0bf2ed82013-12-21 23:33:41 -06001321
Chris Wren8fd39ec2014-02-27 17:43:26 -05001322 if (parcel.readInt() != 0) {
1323 headsUpContentView = RemoteViews.CREATOR.createFromParcel(parcel);
1324 }
1325
Dan Sandler0bf2ed82013-12-21 23:33:41 -06001326 visibility = parcel.readInt();
1327
1328 if (parcel.readInt() != 0) {
1329 publicVersion = Notification.CREATOR.createFromParcel(parcel);
1330 }
Dan Sandler26e81cf2014-05-06 10:01:27 -04001331
1332 color = parcel.readInt();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001333 }
1334
Andy Stadler110988c2010-12-03 14:29:16 -08001335 @Override
Joe Onorato18e69df2010-05-17 22:26:12 -07001336 public Notification clone() {
1337 Notification that = new Notification();
Daniel Sandler1a497d32013-04-18 14:52:45 -04001338 cloneInto(that, true);
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001339 return that;
1340 }
Joe Onorato18e69df2010-05-17 22:26:12 -07001341
Daniel Sandler1a497d32013-04-18 14:52:45 -04001342 /**
1343 * Copy all (or if heavy is false, all except Bitmaps and RemoteViews) members
1344 * of this into that.
1345 * @hide
1346 */
1347 public void cloneInto(Notification that, boolean heavy) {
Joe Onorato18e69df2010-05-17 22:26:12 -07001348 that.when = this.when;
1349 that.icon = this.icon;
1350 that.number = this.number;
1351
1352 // PendingIntents are global, so there's no reason (or way) to clone them.
1353 that.contentIntent = this.contentIntent;
1354 that.deleteIntent = this.deleteIntent;
Daniel Sandlere46cbd32010-06-17 10:35:26 -04001355 that.fullScreenIntent = this.fullScreenIntent;
Joe Onorato18e69df2010-05-17 22:26:12 -07001356
1357 if (this.tickerText != null) {
1358 that.tickerText = this.tickerText.toString();
1359 }
Daniel Sandler1a497d32013-04-18 14:52:45 -04001360 if (heavy && this.tickerView != null) {
Joe Onorato46439ce2010-11-19 13:56:21 -08001361 that.tickerView = this.tickerView.clone();
Joe Onoratoef1e7762010-09-17 18:38:38 -04001362 }
Daniel Sandler1a497d32013-04-18 14:52:45 -04001363 if (heavy && this.contentView != null) {
Joe Onorato18e69df2010-05-17 22:26:12 -07001364 that.contentView = this.contentView.clone();
1365 }
Daniel Sandler1a497d32013-04-18 14:52:45 -04001366 if (heavy && this.largeIcon != null) {
Joe Onorato561d3852010-11-20 18:09:34 -08001367 that.largeIcon = Bitmap.createBitmap(this.largeIcon);
1368 }
Jozef BABJAKa8b91832011-02-22 08:05:08 +01001369 that.iconLevel = this.iconLevel;
Joe Onorato18e69df2010-05-17 22:26:12 -07001370 that.sound = this.sound; // android.net.Uri is immutable
1371 that.audioStreamType = this.audioStreamType;
John Spurlockc0650f022014-07-19 13:22:39 -04001372 if (this.audioAttributes != null) {
1373 that.audioAttributes = new AudioAttributes.Builder(this.audioAttributes).build();
1374 }
Joe Onorato18e69df2010-05-17 22:26:12 -07001375
1376 final long[] vibrate = this.vibrate;
1377 if (vibrate != null) {
1378 final int N = vibrate.length;
1379 final long[] vib = that.vibrate = new long[N];
1380 System.arraycopy(vibrate, 0, vib, 0, N);
1381 }
1382
1383 that.ledARGB = this.ledARGB;
1384 that.ledOnMS = this.ledOnMS;
1385 that.ledOffMS = this.ledOffMS;
1386 that.defaults = this.defaults;
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001387
Joe Onorato18e69df2010-05-17 22:26:12 -07001388 that.flags = this.flags;
1389
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001390 that.priority = this.priority;
Joe Malin8d40d042012-11-05 11:36:40 -08001391
John Spurlockfd7f1e02014-03-18 16:41:57 -04001392 that.category = this.category;
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001393
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001394 that.mGroupKey = this.mGroupKey;
1395
1396 that.mSortKey = this.mSortKey;
1397
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001398 if (this.extras != null) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04001399 try {
1400 that.extras = new Bundle(this.extras);
1401 // will unparcel
1402 that.extras.size();
1403 } catch (BadParcelableException e) {
1404 Log.e(TAG, "could not unparcel extras from notification: " + this, e);
1405 that.extras = null;
1406 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001407 }
1408
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001409 if (this.actions != null) {
1410 that.actions = new Action[this.actions.length];
1411 for(int i=0; i<this.actions.length; i++) {
1412 that.actions[i] = this.actions[i].clone();
1413 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001414 }
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001415
Daniel Sandler1a497d32013-04-18 14:52:45 -04001416 if (heavy && this.bigContentView != null) {
Daniel Sandlerf3b73432012-03-27 15:01:25 -04001417 that.bigContentView = this.bigContentView.clone();
1418 }
Daniel Sandler1a497d32013-04-18 14:52:45 -04001419
Chris Wren8fd39ec2014-02-27 17:43:26 -05001420 if (heavy && this.headsUpContentView != null) {
1421 that.headsUpContentView = this.headsUpContentView.clone();
1422 }
1423
Dan Sandler0bf2ed82013-12-21 23:33:41 -06001424 that.visibility = this.visibility;
1425
1426 if (this.publicVersion != null) {
1427 that.publicVersion = new Notification();
1428 this.publicVersion.cloneInto(that.publicVersion, heavy);
1429 }
1430
Dan Sandler26e81cf2014-05-06 10:01:27 -04001431 that.color = this.color;
1432
Daniel Sandler1a497d32013-04-18 14:52:45 -04001433 if (!heavy) {
1434 that.lightenPayload(); // will clean out extras
1435 }
1436 }
1437
1438 /**
1439 * Removes heavyweight parts of the Notification object for archival or for sending to
1440 * listeners when the full contents are not necessary.
1441 * @hide
1442 */
1443 public final void lightenPayload() {
1444 tickerView = null;
1445 contentView = null;
1446 bigContentView = null;
Chris Wren8fd39ec2014-02-27 17:43:26 -05001447 headsUpContentView = null;
Daniel Sandler1a497d32013-04-18 14:52:45 -04001448 largeIcon = null;
1449 if (extras != null) {
1450 extras.remove(Notification.EXTRA_LARGE_ICON);
1451 extras.remove(Notification.EXTRA_LARGE_ICON_BIG);
1452 extras.remove(Notification.EXTRA_PICTURE);
Christoph Studer223f44e2014-09-02 14:59:32 +02001453 extras.remove(Notification.EXTRA_BIG_TEXT);
Christoph Studerb82bc782014-08-20 14:29:43 +02001454 // Prevent light notifications from being rebuilt.
1455 extras.remove(Builder.EXTRA_NEEDS_REBUILD);
Daniel Sandler1a497d32013-04-18 14:52:45 -04001456 }
Joe Onorato18e69df2010-05-17 22:26:12 -07001457 }
1458
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04001459 /**
1460 * Make sure this CharSequence is safe to put into a bundle, which basically
1461 * means it had better not be some custom Parcelable implementation.
1462 * @hide
1463 */
1464 public static CharSequence safeCharSequence(CharSequence cs) {
Christoph Studer535ec612014-09-03 15:47:47 +02001465 if (cs == null) return cs;
1466 if (cs.length() > MAX_CHARSEQUENCE_LENGTH) {
1467 cs = cs.subSequence(0, MAX_CHARSEQUENCE_LENGTH);
1468 }
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04001469 if (cs instanceof Parcelable) {
1470 Log.e(TAG, "warning: " + cs.getClass().getCanonicalName()
1471 + " instance is a custom Parcelable and not allowed in Notification");
1472 return cs.toString();
1473 }
1474
1475 return cs;
1476 }
1477
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001478 public int describeContents() {
1479 return 0;
1480 }
1481
1482 /**
1483 * Flatten this notification from a parcel.
1484 */
1485 public void writeToParcel(Parcel parcel, int flags)
1486 {
1487 parcel.writeInt(1);
1488
1489 parcel.writeLong(when);
1490 parcel.writeInt(icon);
1491 parcel.writeInt(number);
1492 if (contentIntent != null) {
1493 parcel.writeInt(1);
1494 contentIntent.writeToParcel(parcel, 0);
1495 } else {
1496 parcel.writeInt(0);
1497 }
1498 if (deleteIntent != null) {
1499 parcel.writeInt(1);
1500 deleteIntent.writeToParcel(parcel, 0);
1501 } else {
1502 parcel.writeInt(0);
1503 }
1504 if (tickerText != null) {
1505 parcel.writeInt(1);
1506 TextUtils.writeToParcel(tickerText, parcel, flags);
1507 } else {
1508 parcel.writeInt(0);
1509 }
Joe Onorato46439ce2010-11-19 13:56:21 -08001510 if (tickerView != null) {
Joe Onoratoef1e7762010-09-17 18:38:38 -04001511 parcel.writeInt(1);
Joe Onorato46439ce2010-11-19 13:56:21 -08001512 tickerView.writeToParcel(parcel, 0);
Joe Onoratoef1e7762010-09-17 18:38:38 -04001513 } else {
1514 parcel.writeInt(0);
1515 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001516 if (contentView != null) {
1517 parcel.writeInt(1);
1518 contentView.writeToParcel(parcel, 0);
1519 } else {
1520 parcel.writeInt(0);
1521 }
Joe Onorato561d3852010-11-20 18:09:34 -08001522 if (largeIcon != null) {
1523 parcel.writeInt(1);
1524 largeIcon.writeToParcel(parcel, 0);
1525 } else {
1526 parcel.writeInt(0);
1527 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001528
1529 parcel.writeInt(defaults);
1530 parcel.writeInt(this.flags);
1531
1532 if (sound != null) {
1533 parcel.writeInt(1);
1534 sound.writeToParcel(parcel, 0);
1535 } else {
1536 parcel.writeInt(0);
1537 }
1538 parcel.writeInt(audioStreamType);
John Spurlockc0650f022014-07-19 13:22:39 -04001539
1540 if (audioAttributes != null) {
1541 parcel.writeInt(1);
1542 audioAttributes.writeToParcel(parcel, 0);
1543 } else {
1544 parcel.writeInt(0);
1545 }
1546
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001547 parcel.writeLongArray(vibrate);
1548 parcel.writeInt(ledARGB);
1549 parcel.writeInt(ledOnMS);
1550 parcel.writeInt(ledOffMS);
1551 parcel.writeInt(iconLevel);
Daniel Sandlere46cbd32010-06-17 10:35:26 -04001552
1553 if (fullScreenIntent != null) {
1554 parcel.writeInt(1);
1555 fullScreenIntent.writeToParcel(parcel, 0);
1556 } else {
1557 parcel.writeInt(0);
1558 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001559
1560 parcel.writeInt(priority);
Joe Malin8d40d042012-11-05 11:36:40 -08001561
John Spurlockfd7f1e02014-03-18 16:41:57 -04001562 parcel.writeString(category);
Joe Malin8d40d042012-11-05 11:36:40 -08001563
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001564 parcel.writeString(mGroupKey);
1565
1566 parcel.writeString(mSortKey);
1567
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001568 parcel.writeBundle(extras); // null ok
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001569
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001570 parcel.writeTypedArray(actions, 0); // null ok
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001571
Daniel Sandlerf3b73432012-03-27 15:01:25 -04001572 if (bigContentView != null) {
1573 parcel.writeInt(1);
1574 bigContentView.writeToParcel(parcel, 0);
1575 } else {
1576 parcel.writeInt(0);
1577 }
Dan Sandler0bf2ed82013-12-21 23:33:41 -06001578
Chris Wren8fd39ec2014-02-27 17:43:26 -05001579 if (headsUpContentView != null) {
1580 parcel.writeInt(1);
1581 headsUpContentView.writeToParcel(parcel, 0);
1582 } else {
1583 parcel.writeInt(0);
1584 }
1585
Dan Sandler0bf2ed82013-12-21 23:33:41 -06001586 parcel.writeInt(visibility);
1587
1588 if (publicVersion != null) {
1589 parcel.writeInt(1);
1590 publicVersion.writeToParcel(parcel, 0);
1591 } else {
1592 parcel.writeInt(0);
1593 }
Dan Sandler26e81cf2014-05-06 10:01:27 -04001594
1595 parcel.writeInt(color);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001596 }
1597
1598 /**
1599 * Parcelable.Creator that instantiates Notification objects
1600 */
1601 public static final Parcelable.Creator<Notification> CREATOR
1602 = new Parcelable.Creator<Notification>()
1603 {
1604 public Notification createFromParcel(Parcel parcel)
1605 {
1606 return new Notification(parcel);
1607 }
1608
1609 public Notification[] newArray(int size)
1610 {
1611 return new Notification[size];
1612 }
1613 };
1614
1615 /**
1616 * Sets the {@link #contentView} field to be a view with the standard "Latest Event"
1617 * layout.
1618 *
1619 * <p>Uses the {@link #icon} and {@link #when} fields to set the icon and time fields
1620 * in the view.</p>
1621 * @param context The context for your application / activity.
1622 * @param contentTitle The title that goes in the expanded entry.
1623 * @param contentText The text that goes in the expanded entry.
1624 * @param contentIntent The intent to launch when the user clicks the expanded notification.
1625 * If this is an activity, it must include the
1626 * {@link android.content.Intent#FLAG_ACTIVITY_NEW_TASK} flag, which requires
Scott Main7aee61f2011-02-08 11:25:01 -08001627 * that you take care of task management as described in the
1628 * <a href="{@docRoot}guide/topics/fundamentals/tasks-and-back-stack.html">Tasks and Back
1629 * Stack</a> document.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001630 *
Joe Onorato46439ce2010-11-19 13:56:21 -08001631 * @deprecated Use {@link Builder} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001632 */
Joe Onorato46439ce2010-11-19 13:56:21 -08001633 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001634 public void setLatestEventInfo(Context context,
1635 CharSequence contentTitle, CharSequence contentText, PendingIntent contentIntent) {
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001636 Notification.Builder builder = new Notification.Builder(context);
1637
1638 // First, ensure that key pieces of information that may have been set directly
1639 // are preserved
1640 builder.setWhen(this.when);
1641 builder.setSmallIcon(this.icon);
1642 builder.setPriority(this.priority);
1643 builder.setTicker(this.tickerText);
1644 builder.setNumber(this.number);
Selim Cinek255dd042014-08-19 22:29:02 +02001645 builder.setColor(this.color);
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001646 builder.mFlags = this.flags;
1647 builder.setSound(this.sound, this.audioStreamType);
1648 builder.setDefaults(this.defaults);
1649 builder.setVibrate(this.vibrate);
1650
1651 // now apply the latestEventInfo fields
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001652 if (contentTitle != null) {
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001653 builder.setContentTitle(contentTitle);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001654 }
1655 if (contentText != null) {
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001656 builder.setContentText(contentText);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001657 }
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001658 builder.setContentIntent(contentIntent);
1659 builder.buildInto(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001660 }
1661
1662 @Override
1663 public String toString() {
1664 StringBuilder sb = new StringBuilder();
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001665 sb.append("Notification(pri=");
1666 sb.append(priority);
1667 sb.append(" contentView=");
Joe Onoratoc9596d62011-01-12 17:03:11 -08001668 if (contentView != null) {
1669 sb.append(contentView.getPackage());
1670 sb.append("/0x");
1671 sb.append(Integer.toHexString(contentView.getLayoutId()));
1672 } else {
1673 sb.append("null");
1674 }
1675 sb.append(" vibrate=");
Daniel Sandler6738eee2012-11-16 12:03:32 -05001676 if ((this.defaults & DEFAULT_VIBRATE) != 0) {
1677 sb.append("default");
1678 } else if (this.vibrate != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001679 int N = this.vibrate.length-1;
1680 sb.append("[");
1681 for (int i=0; i<N; i++) {
1682 sb.append(this.vibrate[i]);
1683 sb.append(',');
1684 }
Simon Schoar8cf97d92009-06-10 22:08:37 +02001685 if (N != -1) {
1686 sb.append(this.vibrate[N]);
1687 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001688 sb.append("]");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001689 } else {
1690 sb.append("null");
1691 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001692 sb.append(" sound=");
Daniel Sandler6738eee2012-11-16 12:03:32 -05001693 if ((this.defaults & DEFAULT_SOUND) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001694 sb.append("default");
Daniel Sandler6738eee2012-11-16 12:03:32 -05001695 } else if (this.sound != null) {
1696 sb.append(this.sound.toString());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001697 } else {
1698 sb.append("null");
1699 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001700 sb.append(" defaults=0x");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001701 sb.append(Integer.toHexString(this.defaults));
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001702 sb.append(" flags=0x");
Daniel Sandlere46cbd32010-06-17 10:35:26 -04001703 sb.append(Integer.toHexString(this.flags));
Dan Sandler26e81cf2014-05-06 10:01:27 -04001704 sb.append(String.format(" color=0x%08x", this.color));
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001705 if (this.category != null) {
1706 sb.append(" category=");
1707 sb.append(this.category);
1708 }
1709 if (this.mGroupKey != null) {
1710 sb.append(" groupKey=");
1711 sb.append(this.mGroupKey);
1712 }
1713 if (this.mSortKey != null) {
1714 sb.append(" sortKey=");
1715 sb.append(this.mSortKey);
1716 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001717 if (actions != null) {
Dan Sandler1b718782014-07-18 12:43:45 -04001718 sb.append(" actions=");
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001719 sb.append(actions.length);
Dan Sandler1b718782014-07-18 12:43:45 -04001720 }
1721 sb.append(" vis=");
1722 sb.append(visibilityToString(this.visibility));
1723 if (this.publicVersion != null) {
1724 sb.append(" publicVersion=");
1725 sb.append(publicVersion.toString());
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001726 }
1727 sb.append(")");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001728 return sb.toString();
1729 }
Joe Onorato46439ce2010-11-19 13:56:21 -08001730
Dan Sandler1b718782014-07-18 12:43:45 -04001731 /**
1732 * {@hide}
1733 */
1734 public static String visibilityToString(int vis) {
1735 switch (vis) {
1736 case VISIBILITY_PRIVATE:
1737 return "PRIVATE";
1738 case VISIBILITY_PUBLIC:
1739 return "PUBLIC";
1740 case VISIBILITY_SECRET:
1741 return "SECRET";
1742 default:
1743 return "UNKNOWN(" + String.valueOf(vis) + ")";
1744 }
1745 }
1746
Joe Onoratocb109a02011-01-18 17:57:41 -08001747 /**
Christoph Studer4600f9b2014-07-22 22:44:43 +02001748 * @hide
1749 */
1750 public boolean isValid() {
1751 // Would like to check for icon!=0 here, too, but NotificationManagerService accepts that
1752 // for legacy reasons.
1753 return contentView != null || extras.getBoolean(Builder.EXTRA_REBUILD_CONTENT_VIEW);
1754 }
1755
1756 /**
Christoph Studerc8db24b2014-07-25 17:50:30 +02001757 * @hide
1758 */
1759 public boolean isGroupSummary() {
1760 return mGroupKey != null && (flags & FLAG_GROUP_SUMMARY) != 0;
1761 }
1762
1763 /**
1764 * @hide
1765 */
1766 public boolean isGroupChild() {
1767 return mGroupKey != null && (flags & FLAG_GROUP_SUMMARY) == 0;
1768 }
1769
1770 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001771 * Builder class for {@link Notification} objects.
Joe Malin8d40d042012-11-05 11:36:40 -08001772 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001773 * Provides a convenient way to set the various fields of a {@link Notification} and generate
Scott Main183bf112012-08-13 19:12:13 -07001774 * content views using the platform's notification layout template. If your app supports
1775 * versions of Android as old as API level 4, you can instead use
1776 * {@link android.support.v4.app.NotificationCompat.Builder NotificationCompat.Builder},
1777 * available in the <a href="{@docRoot}tools/extras/support-library.html">Android Support
1778 * library</a>.
Joe Malin8d40d042012-11-05 11:36:40 -08001779 *
Scott Main183bf112012-08-13 19:12:13 -07001780 * <p>Example:
Joe Malin8d40d042012-11-05 11:36:40 -08001781 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001782 * <pre class="prettyprint">
Scott Main183bf112012-08-13 19:12:13 -07001783 * Notification noti = new Notification.Builder(mContext)
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001784 * .setContentTitle(&quot;New mail from &quot; + sender.toString())
1785 * .setContentText(subject)
1786 * .setSmallIcon(R.drawable.new_mail)
1787 * .setLargeIcon(aBitmap)
Chris Wrenfbd96ba2012-05-01 12:03:58 -04001788 * .build();
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001789 * </pre>
Joe Onoratocb109a02011-01-18 17:57:41 -08001790 */
Joe Onorato46439ce2010-11-19 13:56:21 -08001791 public static class Builder {
Daniel Sandler602ad1c2012-06-12 16:06:27 -04001792 private static final int MAX_ACTION_BUTTONS = 3;
Jorim Jaggi445d3c02014-08-19 22:33:42 +02001793 private static final float LARGE_TEXT_SCALE = 1.3f;
Daniel Sandler8680bf82012-05-15 16:52:52 -04001794
Christoph Studer4600f9b2014-07-22 22:44:43 +02001795 /**
1796 * @hide
1797 */
1798 public static final String EXTRA_NEEDS_REBUILD = "android.rebuild";
1799
1800 /**
1801 * @hide
1802 */
1803 public static final String EXTRA_REBUILD_LARGE_ICON = "android.rebuild.largeIcon";
1804 /**
1805 * @hide
1806 */
1807 public static final String EXTRA_REBUILD_CONTENT_VIEW = "android.rebuild.contentView";
1808 /**
1809 * @hide
1810 */
1811 public static final String EXTRA_REBUILD_CONTENT_VIEW_ACTION_COUNT =
1812 "android.rebuild.contentViewActionCount";
1813 /**
1814 * @hide
1815 */
1816 public static final String EXTRA_REBUILD_BIG_CONTENT_VIEW
1817 = "android.rebuild.bigView";
1818 /**
1819 * @hide
1820 */
1821 public static final String EXTRA_REBUILD_BIG_CONTENT_VIEW_ACTION_COUNT
1822 = "android.rebuild.bigViewActionCount";
1823 /**
1824 * @hide
1825 */
1826 public static final String EXTRA_REBUILD_HEADS_UP_CONTENT_VIEW
1827 = "android.rebuild.hudView";
1828 /**
1829 * @hide
1830 */
1831 public static final String EXTRA_REBUILD_HEADS_UP_CONTENT_VIEW_ACTION_COUNT
1832 = "android.rebuild.hudViewActionCount";
1833
1834 /**
Kenny Guy77320062014-08-27 21:37:15 +01001835 * The ApplicationInfo of the package that created the notification, used to create
1836 * a context to rebuild the notification via a Builder.
1837 * @hide
Christoph Studer4600f9b2014-07-22 22:44:43 +02001838 */
Kenny Guy77320062014-08-27 21:37:15 +01001839 private static final String EXTRA_REBUILD_CONTEXT_APPLICATION_INFO =
1840 "android.rebuild.applicationInfo";
Christoph Studer4600f9b2014-07-22 22:44:43 +02001841
1842 // Whether to enable stripping (at post time) & rebuilding (at listener receive time) of
1843 // memory intensive resources.
1844 private static final boolean STRIP_AND_REBUILD = true;
1845
Joe Onorato46439ce2010-11-19 13:56:21 -08001846 private Context mContext;
1847
1848 private long mWhen;
1849 private int mSmallIcon;
1850 private int mSmallIconLevel;
Joe Onorato8595a3d2010-11-19 18:12:07 -08001851 private int mNumber;
Joe Onorato46439ce2010-11-19 13:56:21 -08001852 private CharSequence mContentTitle;
1853 private CharSequence mContentText;
1854 private CharSequence mContentInfo;
Daniel Sandlerf3b73432012-03-27 15:01:25 -04001855 private CharSequence mSubText;
Joe Onorato46439ce2010-11-19 13:56:21 -08001856 private PendingIntent mContentIntent;
1857 private RemoteViews mContentView;
1858 private PendingIntent mDeleteIntent;
1859 private PendingIntent mFullScreenIntent;
1860 private CharSequence mTickerText;
1861 private RemoteViews mTickerView;
1862 private Bitmap mLargeIcon;
1863 private Uri mSound;
1864 private int mAudioStreamType;
John Spurlockc0650f022014-07-19 13:22:39 -04001865 private AudioAttributes mAudioAttributes;
Joe Onorato46439ce2010-11-19 13:56:21 -08001866 private long[] mVibrate;
1867 private int mLedArgb;
1868 private int mLedOnMs;
1869 private int mLedOffMs;
1870 private int mDefaults;
1871 private int mFlags;
Jeff Sharkey1c400132011-08-05 14:50:13 -07001872 private int mProgressMax;
1873 private int mProgress;
1874 private boolean mProgressIndeterminate;
John Spurlockfd7f1e02014-03-18 16:41:57 -04001875 private String mCategory;
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001876 private String mGroupKey;
1877 private String mSortKey;
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001878 private Bundle mExtras;
1879 private int mPriority;
Daniel Sandler8680bf82012-05-15 16:52:52 -04001880 private ArrayList<Action> mActions = new ArrayList<Action>(MAX_ACTION_BUTTONS);
Daniel Sandlera2985ed2012-04-03 16:42:00 -04001881 private boolean mUseChronometer;
Chris Wrenfbd96ba2012-05-01 12:03:58 -04001882 private Style mStyle;
Daniel Sandler0c890492012-09-12 17:23:10 -07001883 private boolean mShowWhen = true;
Dan Sandler0bf2ed82013-12-21 23:33:41 -06001884 private int mVisibility = VISIBILITY_PRIVATE;
1885 private Notification mPublicVersion = null;
Dan Sandler26e81cf2014-05-06 10:01:27 -04001886 private final NotificationColorUtil mColorUtil;
Chris Wrendde75302014-03-26 17:24:15 -04001887 private ArrayList<String> mPeople;
Dan Sandler26e81cf2014-05-06 10:01:27 -04001888 private int mColor = COLOR_DEFAULT;
Christoph Studer7ac80e62014-08-04 16:01:57 +02001889
1890 /**
Kenny Guy8942bcd2014-09-08 21:09:47 +01001891 * The user that built the notification originally.
1892 */
1893 private int mOriginatingUserId;
1894
1895 /**
Christoph Studer4600f9b2014-07-22 22:44:43 +02001896 * Contains extras related to rebuilding during the build phase.
1897 */
1898 private Bundle mRebuildBundle = new Bundle();
1899 /**
1900 * Contains the notification to rebuild when this Builder is in "rebuild" mode.
1901 * Null otherwise.
1902 */
1903 private Notification mRebuildNotification = null;
1904
Joe Onoratocb109a02011-01-18 17:57:41 -08001905 /**
Jorim Jaggid05aa3e2014-08-28 17:52:27 +02001906 * Whether the build notification has three lines. This is used to make the top padding for
1907 * both the contracted and expanded layout consistent.
1908 *
1909 * <p>
1910 * This field is only valid during the build phase.
1911 */
1912 private boolean mHasThreeLines;
1913
1914 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001915 * Constructs a new Builder with the defaults:
Joe Onoratocb109a02011-01-18 17:57:41 -08001916 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001917
1918 * <table>
1919 * <tr><th align=right>priority</th>
1920 * <td>{@link #PRIORITY_DEFAULT}</td></tr>
1921 * <tr><th align=right>when</th>
1922 * <td>now ({@link System#currentTimeMillis()})</td></tr>
1923 * <tr><th align=right>audio stream</th>
1924 * <td>{@link #STREAM_DEFAULT}</td></tr>
1925 * </table>
Joe Onoratocb109a02011-01-18 17:57:41 -08001926 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001927
1928 * @param context
1929 * A {@link Context} that will be used by the Builder to construct the
1930 * RemoteViews. The Context will not be held past the lifetime of this Builder
1931 * object.
Joe Onoratocb109a02011-01-18 17:57:41 -08001932 */
Joe Onorato46439ce2010-11-19 13:56:21 -08001933 public Builder(Context context) {
Adam Powellbf06fa02014-05-30 12:32:18 -07001934 /*
1935 * Important compatibility note!
1936 * Some apps out in the wild create a Notification.Builder in their Activity subclass
1937 * constructor for later use. At this point Activities - themselves subclasses of
1938 * ContextWrapper - do not have their inner Context populated yet. This means that
1939 * any calls to Context methods from within this constructor can cause NPEs in existing
1940 * apps. Any data populated from mContext should therefore be populated lazily to
1941 * preserve compatibility.
1942 */
Joe Onorato46439ce2010-11-19 13:56:21 -08001943 mContext = context;
Andy Stadler110988c2010-12-03 14:29:16 -08001944
1945 // Set defaults to match the defaults of a Notification
Joe Onorato46439ce2010-11-19 13:56:21 -08001946 mWhen = System.currentTimeMillis();
Andy Stadler110988c2010-12-03 14:29:16 -08001947 mAudioStreamType = STREAM_DEFAULT;
John Spurlockc0650f022014-07-19 13:22:39 -04001948 mAudioAttributes = AUDIO_ATTRIBUTES_DEFAULT;
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001949 mPriority = PRIORITY_DEFAULT;
Chris Wrendde75302014-03-26 17:24:15 -04001950 mPeople = new ArrayList<String>();
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01001951
Dianne Hackborn955d8d62014-10-07 20:17:19 -07001952 mColorUtil = context.getApplicationInfo().targetSdkVersion < Build.VERSION_CODES.LOLLIPOP ?
Dan Sandler05c362d2014-09-03 00:16:27 +02001953 NotificationColorUtil.getInstance(mContext) : null;
Joe Onorato46439ce2010-11-19 13:56:21 -08001954 }
1955
Joe Onoratocb109a02011-01-18 17:57:41 -08001956 /**
Christoph Studer4600f9b2014-07-22 22:44:43 +02001957 * Creates a Builder for rebuilding the given Notification.
1958 * <p>
1959 * Call {@link #rebuild()} to retrieve the rebuilt version of 'n'.
1960 */
1961 private Builder(Context context, Notification n) {
1962 this(context);
1963 mRebuildNotification = n;
1964 restoreFromNotification(n);
1965
1966 Style style = null;
1967 Bundle extras = n.extras;
1968 String templateClass = extras.getString(EXTRA_TEMPLATE);
1969 if (!TextUtils.isEmpty(templateClass)) {
1970 Class<? extends Style> styleClass = getNotificationStyleClass(templateClass);
1971 if (styleClass == null) {
1972 Log.d(TAG, "Unknown style class: " + styleClass);
1973 return;
1974 }
1975
1976 try {
1977 Constructor<? extends Style> constructor = styleClass.getConstructor();
1978 style = constructor.newInstance();
1979 style.restoreFromExtras(extras);
1980 } catch (Throwable t) {
1981 Log.e(TAG, "Could not create Style", t);
1982 return;
1983 }
1984 }
1985 if (style != null) {
1986 setStyle(style);
1987 }
1988 }
1989
1990 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001991 * Add a timestamp pertaining to the notification (usually the time the event occurred).
Daniel Sandler0c890492012-09-12 17:23:10 -07001992 * It will be shown in the notification content view by default; use
Griff Hazen50c11652014-05-16 09:46:31 -07001993 * {@link #setShowWhen(boolean) setShowWhen} to control this.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001994 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001995 * @see Notification#when
Joe Onoratocb109a02011-01-18 17:57:41 -08001996 */
Joe Onorato46439ce2010-11-19 13:56:21 -08001997 public Builder setWhen(long when) {
1998 mWhen = when;
1999 return this;
2000 }
2001
Joe Onoratocb109a02011-01-18 17:57:41 -08002002 /**
Griff Hazen50c11652014-05-16 09:46:31 -07002003 * Control whether the timestamp set with {@link #setWhen(long) setWhen} is shown
Daniel Sandler0c890492012-09-12 17:23:10 -07002004 * in the content view.
2005 */
2006 public Builder setShowWhen(boolean show) {
2007 mShowWhen = show;
2008 return this;
2009 }
2010
2011 /**
Daniel Sandlerd33b8032012-05-10 11:41:48 -04002012 * Show the {@link Notification#when} field as a stopwatch.
Joe Malin8d40d042012-11-05 11:36:40 -08002013 *
2014 * Instead of presenting <code>when</code> as a timestamp, the notification will show an
Daniel Sandlerd33b8032012-05-10 11:41:48 -04002015 * automatically updating display of the minutes and seconds since <code>when</code>.
Daniel Sandlera2985ed2012-04-03 16:42:00 -04002016 *
Daniel Sandlerd33b8032012-05-10 11:41:48 -04002017 * Useful when showing an elapsed time (like an ongoing phone call).
2018 *
2019 * @see android.widget.Chronometer
Daniel Sandlera2985ed2012-04-03 16:42:00 -04002020 * @see Notification#when
2021 */
2022 public Builder setUsesChronometer(boolean b) {
2023 mUseChronometer = b;
2024 return this;
2025 }
2026
2027 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002028 * Set the small icon resource, which will be used to represent the notification in the
2029 * status bar.
Joe Onoratocb109a02011-01-18 17:57:41 -08002030 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002031
2032 * The platform template for the expanded view will draw this icon in the left, unless a
2033 * {@link #setLargeIcon(Bitmap) large icon} has also been specified, in which case the small
2034 * icon will be moved to the right-hand side.
2035 *
2036
2037 * @param icon
2038 * A resource ID in the application's package of the drawable to use.
2039 * @see Notification#icon
Joe Onoratocb109a02011-01-18 17:57:41 -08002040 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002041 public Builder setSmallIcon(int icon) {
2042 mSmallIcon = icon;
2043 return this;
2044 }
2045
Joe Onoratocb109a02011-01-18 17:57:41 -08002046 /**
2047 * A variant of {@link #setSmallIcon(int) setSmallIcon(int)} that takes an additional
2048 * level parameter for when the icon is a {@link android.graphics.drawable.LevelListDrawable
2049 * LevelListDrawable}.
2050 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002051 * @param icon A resource ID in the application's package of the drawable to use.
Joe Onoratocb109a02011-01-18 17:57:41 -08002052 * @param level The level to use for the icon.
2053 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002054 * @see Notification#icon
2055 * @see Notification#iconLevel
Joe Onoratocb109a02011-01-18 17:57:41 -08002056 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002057 public Builder setSmallIcon(int icon, int level) {
2058 mSmallIcon = icon;
2059 mSmallIconLevel = level;
2060 return this;
2061 }
2062
Joe Onoratocb109a02011-01-18 17:57:41 -08002063 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002064 * Set the first line of text in the platform notification template.
Joe Onoratocb109a02011-01-18 17:57:41 -08002065 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002066 public Builder setContentTitle(CharSequence title) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04002067 mContentTitle = safeCharSequence(title);
Joe Onorato46439ce2010-11-19 13:56:21 -08002068 return this;
2069 }
2070
Joe Onoratocb109a02011-01-18 17:57:41 -08002071 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002072 * Set the second line of text in the platform notification template.
Joe Onoratocb109a02011-01-18 17:57:41 -08002073 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002074 public Builder setContentText(CharSequence text) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04002075 mContentText = safeCharSequence(text);
Joe Onorato46439ce2010-11-19 13:56:21 -08002076 return this;
2077 }
2078
Joe Onoratocb109a02011-01-18 17:57:41 -08002079 /**
Joe Malin8d40d042012-11-05 11:36:40 -08002080 * Set the third line of text in the platform notification template.
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04002081 * Don't use if you're also using {@link #setProgress(int, int, boolean)}; they occupy the
2082 * same location in the standard template.
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002083 */
2084 public Builder setSubText(CharSequence text) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04002085 mSubText = safeCharSequence(text);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002086 return this;
2087 }
2088
2089 /**
Joe Onoratocb109a02011-01-18 17:57:41 -08002090 * Set the large number at the right-hand side of the notification. This is
2091 * equivalent to setContentInfo, although it might show the number in a different
2092 * font size for readability.
2093 */
Joe Onorato8595a3d2010-11-19 18:12:07 -08002094 public Builder setNumber(int number) {
2095 mNumber = number;
2096 return this;
2097 }
2098
Joe Onoratocb109a02011-01-18 17:57:41 -08002099 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002100 * A small piece of additional information pertaining to this notification.
2101 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002102 * The platform template will draw this on the last line of the notification, at the far
2103 * right (to the right of a smallIcon if it has been placed there).
Joe Onoratocb109a02011-01-18 17:57:41 -08002104 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002105 public Builder setContentInfo(CharSequence info) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04002106 mContentInfo = safeCharSequence(info);
Joe Onorato46439ce2010-11-19 13:56:21 -08002107 return this;
2108 }
2109
Joe Onoratocb109a02011-01-18 17:57:41 -08002110 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002111 * Set the progress this notification represents.
2112 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002113 * The platform template will represent this using a {@link ProgressBar}.
Jeff Sharkey1c400132011-08-05 14:50:13 -07002114 */
2115 public Builder setProgress(int max, int progress, boolean indeterminate) {
2116 mProgressMax = max;
2117 mProgress = progress;
2118 mProgressIndeterminate = indeterminate;
2119 return this;
2120 }
2121
2122 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002123 * Supply a custom RemoteViews to use instead of the platform template.
2124 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002125 * @see Notification#contentView
Joe Onoratocb109a02011-01-18 17:57:41 -08002126 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002127 public Builder setContent(RemoteViews views) {
2128 mContentView = views;
2129 return this;
2130 }
2131
Joe Onoratocb109a02011-01-18 17:57:41 -08002132 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002133 * Supply a {@link PendingIntent} to be sent when the notification is clicked.
2134 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002135 * As of {@link android.os.Build.VERSION_CODES#HONEYCOMB}, if this field is unset and you
2136 * have specified a custom RemoteViews with {@link #setContent(RemoteViews)}, you can use
2137 * {@link RemoteViews#setOnClickPendingIntent RemoteViews.setOnClickPendingIntent(int,PendingIntent)}
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002138 * to assign PendingIntents to individual views in that custom layout (i.e., to create
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002139 * clickable buttons inside the notification view).
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002140 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002141 * @see Notification#contentIntent Notification.contentIntent
Joe Onoratocb109a02011-01-18 17:57:41 -08002142 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002143 public Builder setContentIntent(PendingIntent intent) {
2144 mContentIntent = intent;
2145 return this;
2146 }
2147
Joe Onoratocb109a02011-01-18 17:57:41 -08002148 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002149 * Supply a {@link PendingIntent} to send when the notification is cleared explicitly by the user.
2150 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002151 * @see Notification#deleteIntent
Joe Onoratocb109a02011-01-18 17:57:41 -08002152 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002153 public Builder setDeleteIntent(PendingIntent intent) {
2154 mDeleteIntent = intent;
2155 return this;
2156 }
2157
Joe Onoratocb109a02011-01-18 17:57:41 -08002158 /**
2159 * An intent to launch instead of posting the notification to the status bar.
2160 * Only for use with extremely high-priority notifications demanding the user's
2161 * <strong>immediate</strong> attention, such as an incoming phone call or
2162 * alarm clock that the user has explicitly set to a particular time.
2163 * If this facility is used for something else, please give the user an option
2164 * to turn it off and use a normal notification, as this can be extremely
2165 * disruptive.
2166 *
Chris Wren47c20a12014-06-18 17:27:29 -04002167 * <p>
2168 * The system UI may choose to display a heads-up notification, instead of
2169 * launching this intent, while the user is using the device.
2170 * </p>
2171 *
Joe Onoratocb109a02011-01-18 17:57:41 -08002172 * @param intent The pending intent to launch.
2173 * @param highPriority Passing true will cause this notification to be sent
2174 * even if other notifications are suppressed.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002175 *
2176 * @see Notification#fullScreenIntent
Joe Onoratocb109a02011-01-18 17:57:41 -08002177 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002178 public Builder setFullScreenIntent(PendingIntent intent, boolean highPriority) {
2179 mFullScreenIntent = intent;
2180 setFlag(FLAG_HIGH_PRIORITY, highPriority);
2181 return this;
2182 }
2183
Joe Onoratocb109a02011-01-18 17:57:41 -08002184 /**
Dan Sandler5fcdf6e2014-07-18 11:31:15 -04002185 * Set the "ticker" text which is sent to accessibility services.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002186 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002187 * @see Notification#tickerText
Joe Onoratocb109a02011-01-18 17:57:41 -08002188 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002189 public Builder setTicker(CharSequence tickerText) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04002190 mTickerText = safeCharSequence(tickerText);
Joe Onorato46439ce2010-11-19 13:56:21 -08002191 return this;
2192 }
2193
Joe Onoratocb109a02011-01-18 17:57:41 -08002194 /**
Dan Sandler5fcdf6e2014-07-18 11:31:15 -04002195 * Obsolete version of {@link #setTicker(CharSequence)}.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002196 *
Joe Onoratocb109a02011-01-18 17:57:41 -08002197 */
Dan Sandler5fcdf6e2014-07-18 11:31:15 -04002198 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -08002199 public Builder setTicker(CharSequence tickerText, RemoteViews views) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04002200 mTickerText = safeCharSequence(tickerText);
Dan Sandler5fcdf6e2014-07-18 11:31:15 -04002201 mTickerView = views; // we'll save it for you anyway
Joe Onorato46439ce2010-11-19 13:56:21 -08002202 return this;
2203 }
2204
Joe Onoratocb109a02011-01-18 17:57:41 -08002205 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002206 * Add a large icon to the notification (and the ticker on some devices).
2207 *
2208 * In the platform template, this image will be shown on the left of the notification view
2209 * in place of the {@link #setSmallIcon(int) small icon} (which will move to the right side).
2210 *
2211 * @see Notification#largeIcon
Joe Onoratocb109a02011-01-18 17:57:41 -08002212 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002213 public Builder setLargeIcon(Bitmap icon) {
2214 mLargeIcon = icon;
2215 return this;
2216 }
2217
Joe Onoratocb109a02011-01-18 17:57:41 -08002218 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002219 * Set the sound to play.
2220 *
John Spurlockc0650f022014-07-19 13:22:39 -04002221 * It will be played using the {@link #AUDIO_ATTRIBUTES_DEFAULT default audio attributes}
2222 * for notifications.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002223 *
Chris Wren47c20a12014-06-18 17:27:29 -04002224 * <p>
2225 * A notification that is noisy is more likely to be presented as a heads-up notification.
2226 * </p>
2227 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002228 * @see Notification#sound
Joe Onoratocb109a02011-01-18 17:57:41 -08002229 */
Joe Onorato52f80cd2010-11-21 15:34:48 -08002230 public Builder setSound(Uri sound) {
2231 mSound = sound;
John Spurlockc0650f022014-07-19 13:22:39 -04002232 mAudioAttributes = AUDIO_ATTRIBUTES_DEFAULT;
Joe Onorato52f80cd2010-11-21 15:34:48 -08002233 return this;
2234 }
2235
Joe Onoratocb109a02011-01-18 17:57:41 -08002236 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002237 * Set the sound to play, along with a specific stream on which to play it.
Joe Onoratocb109a02011-01-18 17:57:41 -08002238 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002239 * See {@link android.media.AudioManager} for the <code>STREAM_</code> constants.
2240 *
Chris Wren47c20a12014-06-18 17:27:29 -04002241 * <p>
2242 * A notification that is noisy is more likely to be presented as a heads-up notification.
2243 * </p>
John Spurlockc0650f022014-07-19 13:22:39 -04002244 * @deprecated use {@link #setSound(Uri, AudioAttributes)} instead.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002245 * @see Notification#sound
Joe Onoratocb109a02011-01-18 17:57:41 -08002246 */
Jean-Michel Trivi81f871e2014-08-06 16:32:38 -07002247 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -08002248 public Builder setSound(Uri sound, int streamType) {
2249 mSound = sound;
2250 mAudioStreamType = streamType;
2251 return this;
2252 }
2253
Joe Onoratocb109a02011-01-18 17:57:41 -08002254 /**
John Spurlockc0650f022014-07-19 13:22:39 -04002255 * Set the sound to play, along with specific {@link AudioAttributes audio attributes} to
2256 * use during playback.
2257 *
2258 * <p>
2259 * A notification that is noisy is more likely to be presented as a heads-up notification.
2260 * </p>
2261 *
2262 * @see Notification#sound
2263 */
2264 public Builder setSound(Uri sound, AudioAttributes audioAttributes) {
2265 mSound = sound;
2266 mAudioAttributes = audioAttributes;
2267 return this;
2268 }
2269
2270 /**
Joe Onoratocb109a02011-01-18 17:57:41 -08002271 * Set the vibration pattern to use.
2272 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002273 * See {@link android.os.Vibrator#vibrate(long[], int)} for a discussion of the
2274 * <code>pattern</code> parameter.
2275 *
Chris Wren47c20a12014-06-18 17:27:29 -04002276 * <p>
2277 * A notification that vibrates is more likely to be presented as a heads-up notification.
2278 * </p>
2279 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002280 * @see Notification#vibrate
Joe Onoratocb109a02011-01-18 17:57:41 -08002281 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002282 public Builder setVibrate(long[] pattern) {
2283 mVibrate = pattern;
2284 return this;
2285 }
2286
Joe Onoratocb109a02011-01-18 17:57:41 -08002287 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002288 * Set the desired color for the indicator LED on the device, as well as the
2289 * blink duty cycle (specified in milliseconds).
2290 *
2291
2292 * Not all devices will honor all (or even any) of these values.
2293 *
2294
2295 * @see Notification#ledARGB
2296 * @see Notification#ledOnMS
2297 * @see Notification#ledOffMS
Joe Onoratocb109a02011-01-18 17:57:41 -08002298 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002299 public Builder setLights(int argb, int onMs, int offMs) {
2300 mLedArgb = argb;
2301 mLedOnMs = onMs;
2302 mLedOffMs = offMs;
Joe Onorato46439ce2010-11-19 13:56:21 -08002303 return this;
2304 }
2305
Joe Onoratocb109a02011-01-18 17:57:41 -08002306 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002307 * Set whether this is an "ongoing" notification.
Joe Onoratocb109a02011-01-18 17:57:41 -08002308 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002309
2310 * Ongoing notifications cannot be dismissed by the user, so your application or service
2311 * must take care of canceling them.
2312 *
2313
2314 * They are typically used to indicate a background task that the user is actively engaged
2315 * with (e.g., playing music) or is pending in some way and therefore occupying the device
2316 * (e.g., a file download, sync operation, active network connection).
2317 *
2318
2319 * @see Notification#FLAG_ONGOING_EVENT
2320 * @see Service#setForeground(boolean)
Joe Onoratocb109a02011-01-18 17:57:41 -08002321 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002322 public Builder setOngoing(boolean ongoing) {
2323 setFlag(FLAG_ONGOING_EVENT, ongoing);
2324 return this;
2325 }
2326
Joe Onoratocb109a02011-01-18 17:57:41 -08002327 /**
2328 * Set this flag if you would only like the sound, vibrate
2329 * and ticker to be played if the notification is not already showing.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002330 *
2331 * @see Notification#FLAG_ONLY_ALERT_ONCE
Joe Onoratocb109a02011-01-18 17:57:41 -08002332 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002333 public Builder setOnlyAlertOnce(boolean onlyAlertOnce) {
2334 setFlag(FLAG_ONLY_ALERT_ONCE, onlyAlertOnce);
2335 return this;
2336 }
2337
Joe Onoratocb109a02011-01-18 17:57:41 -08002338 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002339 * Make this notification automatically dismissed when the user touches it. The
2340 * PendingIntent set with {@link #setDeleteIntent} will be sent when this happens.
2341 *
2342 * @see Notification#FLAG_AUTO_CANCEL
Joe Onoratocb109a02011-01-18 17:57:41 -08002343 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002344 public Builder setAutoCancel(boolean autoCancel) {
Joe Onorato281d83f2011-01-04 17:13:10 -08002345 setFlag(FLAG_AUTO_CANCEL, autoCancel);
Joe Onorato46439ce2010-11-19 13:56:21 -08002346 return this;
2347 }
2348
Joe Onoratocb109a02011-01-18 17:57:41 -08002349 /**
Griff Hazendfcb0802014-02-11 12:00:00 -08002350 * Set whether or not this notification should not bridge to other devices.
2351 *
2352 * <p>Some notifications can be bridged to other devices for remote display.
2353 * This hint can be set to recommend this notification not be bridged.
2354 */
2355 public Builder setLocalOnly(boolean localOnly) {
2356 setFlag(FLAG_LOCAL_ONLY, localOnly);
2357 return this;
2358 }
2359
2360 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002361 * Set which notification properties will be inherited from system defaults.
Joe Onoratocb109a02011-01-18 17:57:41 -08002362 * <p>
2363 * The value should be one or more of the following fields combined with
2364 * bitwise-or:
2365 * {@link #DEFAULT_SOUND}, {@link #DEFAULT_VIBRATE}, {@link #DEFAULT_LIGHTS}.
2366 * <p>
2367 * For all default values, use {@link #DEFAULT_ALL}.
2368 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002369 public Builder setDefaults(int defaults) {
2370 mDefaults = defaults;
2371 return this;
2372 }
2373
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002374 /**
2375 * Set the priority of this notification.
2376 *
2377 * @see Notification#priority
2378 */
Tor Norbyed9273d62013-05-30 15:59:53 -07002379 public Builder setPriority(@Priority int pri) {
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002380 mPriority = pri;
2381 return this;
2382 }
Joe Malin8d40d042012-11-05 11:36:40 -08002383
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002384 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -04002385 * Set the notification category.
Joe Malin8d40d042012-11-05 11:36:40 -08002386 *
John Spurlockfd7f1e02014-03-18 16:41:57 -04002387 * @see Notification#category
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002388 */
John Spurlockfd7f1e02014-03-18 16:41:57 -04002389 public Builder setCategory(String category) {
2390 mCategory = category;
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002391 return this;
2392 }
2393
2394 /**
Chris Wrendde75302014-03-26 17:24:15 -04002395 * Add a person that is relevant to this notification.
2396 *
Chris Wrene6c48932014-09-29 17:19:27 -04002397 * <P>
2398 * Depending on user preferences, this annotation may allow the notification to pass
2399 * through interruption filters, and to appear more prominently in the user interface.
2400 * </P>
2401 *
2402 * <P>
2403 * The person should be specified by the {@code String} representation of a
2404 * {@link android.provider.ContactsContract.Contacts#CONTENT_LOOKUP_URI}.
2405 * </P>
2406 *
2407 * <P>The system will also attempt to resolve {@code mailto:} and {@code tel:} schema
2408 * URIs. The path part of these URIs must exist in the contacts database, in the
2409 * appropriate column, or the reference will be discarded as invalid. Telephone schema
2410 * URIs will be resolved by {@link android.provider.ContactsContract.PhoneLookup}.
2411 * </P>
2412 *
2413 * @param uri A URI for the person.
Chris Wrendde75302014-03-26 17:24:15 -04002414 * @see Notification#EXTRA_PEOPLE
2415 */
Chris Wrene6c48932014-09-29 17:19:27 -04002416 public Builder addPerson(String uri) {
2417 mPeople.add(uri);
Chris Wrendde75302014-03-26 17:24:15 -04002418 return this;
2419 }
2420
2421 /**
Griff Hazen5cadc3b2014-05-20 09:55:39 -07002422 * Set this notification to be part of a group of notifications sharing the same key.
2423 * Grouped notifications may display in a cluster or stack on devices which
2424 * support such rendering.
2425 *
2426 * <p>To make this notification the summary for its group, also call
2427 * {@link #setGroupSummary}. A sort order can be specified for group members by using
2428 * {@link #setSortKey}.
2429 * @param groupKey The group key of the group.
2430 * @return this object for method chaining
2431 */
2432 public Builder setGroup(String groupKey) {
2433 mGroupKey = groupKey;
2434 return this;
2435 }
2436
2437 /**
2438 * Set this notification to be the group summary for a group of notifications.
2439 * Grouped notifications may display in a cluster or stack on devices which
2440 * support such rendering. Requires a group key also be set using {@link #setGroup}.
2441 * @param isGroupSummary Whether this notification should be a group summary.
2442 * @return this object for method chaining
2443 */
2444 public Builder setGroupSummary(boolean isGroupSummary) {
2445 setFlag(FLAG_GROUP_SUMMARY, isGroupSummary);
2446 return this;
2447 }
2448
2449 /**
2450 * Set a sort key that orders this notification among other notifications from the
2451 * same package. This can be useful if an external sort was already applied and an app
2452 * would like to preserve this. Notifications will be sorted lexicographically using this
2453 * value, although providing different priorities in addition to providing sort key may
2454 * cause this value to be ignored.
2455 *
2456 * <p>This sort key can also be used to order members of a notification group. See
Griff Hazen9e1379f2014-05-20 12:50:51 -07002457 * {@link #setGroup}.
Griff Hazen5cadc3b2014-05-20 09:55:39 -07002458 *
2459 * @see String#compareTo(String)
2460 */
2461 public Builder setSortKey(String sortKey) {
2462 mSortKey = sortKey;
2463 return this;
2464 }
2465
2466 /**
Griff Hazen720042b2014-02-24 15:46:56 -08002467 * Merge additional metadata into this notification.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002468 *
Griff Hazen720042b2014-02-24 15:46:56 -08002469 * <p>Values within the Bundle will replace existing extras values in this Builder.
2470 *
2471 * @see Notification#extras
2472 */
Griff Hazen959591e2014-05-15 22:26:18 -07002473 public Builder addExtras(Bundle extras) {
2474 if (extras != null) {
2475 if (mExtras == null) {
2476 mExtras = new Bundle(extras);
2477 } else {
2478 mExtras.putAll(extras);
2479 }
Griff Hazen720042b2014-02-24 15:46:56 -08002480 }
2481 return this;
2482 }
2483
2484 /**
2485 * Set metadata for this notification.
2486 *
2487 * <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 -04002488 * current contents are copied into the Notification each time {@link #build()} is
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002489 * called.
2490 *
Griff Hazen720042b2014-02-24 15:46:56 -08002491 * <p>Replaces any existing extras values with those from the provided Bundle.
2492 * Use {@link #addExtras} to merge in metadata instead.
2493 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002494 * @see Notification#extras
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002495 */
Griff Hazen959591e2014-05-15 22:26:18 -07002496 public Builder setExtras(Bundle extras) {
2497 mExtras = extras;
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002498 return this;
2499 }
2500
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002501 /**
Griff Hazen720042b2014-02-24 15:46:56 -08002502 * Get the current metadata Bundle used by this notification Builder.
2503 *
2504 * <p>The returned Bundle is shared with this Builder.
2505 *
2506 * <p>The current contents of this Bundle are copied into the Notification each time
2507 * {@link #build()} is called.
2508 *
2509 * @see Notification#extras
2510 */
2511 public Bundle getExtras() {
2512 if (mExtras == null) {
2513 mExtras = new Bundle();
2514 }
2515 return mExtras;
2516 }
2517
2518 /**
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002519 * Add an action to this notification. Actions are typically displayed by
2520 * the system as a button adjacent to the notification content.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04002521 * <p>
2522 * Every action must have an icon (32dp square and matching the
2523 * <a href="{@docRoot}design/style/iconography.html#action-bar">Holo
2524 * Dark action bar</a> visual style), a textual label, and a {@link PendingIntent}.
2525 * <p>
2526 * A notification in its expanded form can display up to 3 actions, from left to right in
2527 * the order they were added. Actions will not be displayed when the notification is
2528 * collapsed, however, so be sure that any essential functions may be accessed by the user
2529 * in some other way (for example, in the Activity pointed to by {@link #contentIntent}).
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002530 *
2531 * @param icon Resource ID of a drawable that represents the action.
2532 * @param title Text describing the action.
2533 * @param intent PendingIntent to be fired when the action is invoked.
2534 */
2535 public Builder addAction(int icon, CharSequence title, PendingIntent intent) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04002536 mActions.add(new Action(icon, safeCharSequence(title), intent));
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002537 return this;
2538 }
2539
Chris Wrenfbd96ba2012-05-01 12:03:58 -04002540 /**
Griff Hazen959591e2014-05-15 22:26:18 -07002541 * Add an action to this notification. Actions are typically displayed by
2542 * the system as a button adjacent to the notification content.
2543 * <p>
2544 * Every action must have an icon (32dp square and matching the
2545 * <a href="{@docRoot}design/style/iconography.html#action-bar">Holo
2546 * Dark action bar</a> visual style), a textual label, and a {@link PendingIntent}.
2547 * <p>
2548 * A notification in its expanded form can display up to 3 actions, from left to right in
2549 * the order they were added. Actions will not be displayed when the notification is
2550 * collapsed, however, so be sure that any essential functions may be accessed by the user
2551 * in some other way (for example, in the Activity pointed to by {@link #contentIntent}).
2552 *
2553 * @param action The action to add.
2554 */
2555 public Builder addAction(Action action) {
2556 mActions.add(action);
2557 return this;
2558 }
2559
2560 /**
Chris Wrenfbd96ba2012-05-01 12:03:58 -04002561 * Add a rich notification style to be applied at build time.
2562 *
2563 * @param style Object responsible for modifying the notification style.
2564 */
2565 public Builder setStyle(Style style) {
2566 if (mStyle != style) {
2567 mStyle = style;
Daniel Sandlerc08dea22012-06-28 08:35:24 -07002568 if (mStyle != null) {
2569 mStyle.setBuilder(this);
2570 }
Chris Wrenfbd96ba2012-05-01 12:03:58 -04002571 }
2572 return this;
2573 }
2574
Dan Sandler0bf2ed82013-12-21 23:33:41 -06002575 /**
2576 * Specify the value of {@link #visibility}.
Griff Hazenb720abe2014-05-20 13:15:30 -07002577 *
Dan Sandler0bf2ed82013-12-21 23:33:41 -06002578 * @param visibility One of {@link #VISIBILITY_PRIVATE} (the default),
2579 * {@link #VISIBILITY_SECRET}, or {@link #VISIBILITY_PUBLIC}.
2580 *
2581 * @return The same Builder.
2582 */
2583 public Builder setVisibility(int visibility) {
2584 mVisibility = visibility;
2585 return this;
2586 }
2587
2588 /**
2589 * Supply a replacement Notification whose contents should be shown in insecure contexts
2590 * (i.e. atop the secure lockscreen). See {@link #visibility} and {@link #VISIBILITY_PUBLIC}.
2591 * @param n A replacement notification, presumably with some or all info redacted.
2592 * @return The same Builder.
2593 */
2594 public Builder setPublicVersion(Notification n) {
2595 mPublicVersion = n;
2596 return this;
2597 }
2598
Griff Hazenb720abe2014-05-20 13:15:30 -07002599 /**
Griff Hazen5cadc3b2014-05-20 09:55:39 -07002600 * Apply an extender to this notification builder. Extenders may be used to add
2601 * metadata or change options on this builder.
2602 */
Griff Hazen61a9e862014-05-22 16:05:19 -07002603 public Builder extend(Extender extender) {
2604 extender.extend(this);
Griff Hazen5cadc3b2014-05-20 09:55:39 -07002605 return this;
2606 }
2607
Joe Onorato46439ce2010-11-19 13:56:21 -08002608 private void setFlag(int mask, boolean value) {
2609 if (value) {
2610 mFlags |= mask;
2611 } else {
2612 mFlags &= ~mask;
2613 }
2614 }
2615
Dan Sandler26e81cf2014-05-06 10:01:27 -04002616 /**
2617 * Sets {@link Notification#color}.
2618 *
2619 * @param argb The accent color to use
2620 *
2621 * @return The same Builder.
2622 */
2623 public Builder setColor(int argb) {
2624 mColor = argb;
2625 return this;
2626 }
2627
Jorim Jaggid05aa3e2014-08-28 17:52:27 +02002628 private Drawable getProfileBadgeDrawable() {
Christoph Studer7ac80e62014-08-04 16:01:57 +02002629 // Note: This assumes that the current user can read the profile badge of the
2630 // originating user.
Svetoslavc7d62f02014-09-04 15:39:54 -07002631 return mContext.getPackageManager().getUserBadgeForDensity(
Kenny Guy8942bcd2014-09-08 21:09:47 +01002632 new UserHandle(mOriginatingUserId), 0);
Jorim Jaggid05aa3e2014-08-28 17:52:27 +02002633 }
2634
2635 private Bitmap getProfileBadge() {
2636 Drawable badge = getProfileBadgeDrawable();
Kenny Guy8a0101b2014-05-08 23:34:12 +01002637 if (badge == null) {
2638 return null;
2639 }
Jorim Jaggid05aa3e2014-08-28 17:52:27 +02002640 final int size = mContext.getResources().getDimensionPixelSize(
2641 R.dimen.notification_badge_size);
2642 Bitmap bitmap = Bitmap.createBitmap(size, size, Bitmap.Config.ARGB_8888);
Kenny Guy8a0101b2014-05-08 23:34:12 +01002643 Canvas canvas = new Canvas(bitmap);
Jorim Jaggid05aa3e2014-08-28 17:52:27 +02002644 badge.setBounds(0, 0, size, size);
Kenny Guy8a0101b2014-05-08 23:34:12 +01002645 badge.draw(canvas);
2646 return bitmap;
2647 }
2648
Kenny Guy98193ea2014-07-24 19:54:37 +01002649 private boolean addProfileBadge(RemoteViews contentView, int resId) {
2650 Bitmap profileBadge = getProfileBadge();
2651
2652 contentView.setViewVisibility(R.id.profile_badge_large_template, View.GONE);
2653 contentView.setViewVisibility(R.id.profile_badge_line2, View.GONE);
2654 contentView.setViewVisibility(R.id.profile_badge_line3, View.GONE);
2655
2656 if (profileBadge != null) {
2657 contentView.setImageViewBitmap(resId, profileBadge);
2658 contentView.setViewVisibility(resId, View.VISIBLE);
2659
2660 // Make sure Line 3 is visible. As badge will be here if there
2661 // is no text to display.
2662 if (resId == R.id.profile_badge_line3) {
2663 contentView.setViewVisibility(R.id.line3, View.VISIBLE);
2664 }
2665 return true;
2666 }
2667 return false;
2668 }
2669
Jorim Jaggid05aa3e2014-08-28 17:52:27 +02002670 private void shrinkLine3Text(RemoteViews contentView) {
2671 float subTextSize = mContext.getResources().getDimensionPixelSize(
2672 R.dimen.notification_subtext_size);
2673 contentView.setTextViewTextSize(R.id.text, TypedValue.COMPLEX_UNIT_PX, subTextSize);
2674 }
2675
Christoph Studerfe718432014-09-01 18:21:18 +02002676 private void unshrinkLine3Text(RemoteViews contentView) {
2677 float regularTextSize = mContext.getResources().getDimensionPixelSize(
2678 com.android.internal.R.dimen.notification_text_size);
2679 contentView.setTextViewTextSize(R.id.text, TypedValue.COMPLEX_UNIT_PX, regularTextSize);
2680 }
2681
2682 private void resetStandardTemplate(RemoteViews contentView) {
2683 removeLargeIconBackground(contentView);
2684 contentView.setViewPadding(R.id.icon, 0, 0, 0, 0);
2685 contentView.setImageViewResource(R.id.icon, 0);
2686 contentView.setInt(R.id.icon, "setBackgroundResource", 0);
2687 contentView.setViewVisibility(R.id.right_icon, View.GONE);
2688 contentView.setInt(R.id.right_icon, "setBackgroundResource", 0);
2689 contentView.setImageViewResource(R.id.right_icon, 0);
2690 contentView.setImageViewResource(R.id.icon, 0);
2691 contentView.setTextViewText(R.id.title, null);
2692 contentView.setTextViewText(R.id.text, null);
2693 unshrinkLine3Text(contentView);
2694 contentView.setTextViewText(R.id.text2, null);
2695 contentView.setViewVisibility(R.id.text2, View.GONE);
2696 contentView.setViewVisibility(R.id.info, View.GONE);
2697 contentView.setViewVisibility(R.id.time, View.GONE);
2698 contentView.setViewVisibility(R.id.line3, View.GONE);
2699 contentView.setViewVisibility(R.id.overflow_divider, View.GONE);
2700 contentView.setViewVisibility(R.id.progress, View.GONE);
Christoph Studerca1db712014-09-10 17:31:33 +02002701 contentView.setViewVisibility(R.id.chronometer, View.GONE);
2702 contentView.setViewVisibility(R.id.time, View.GONE);
Christoph Studerfe718432014-09-01 18:21:18 +02002703 }
2704
Jorim Jaggi445d3c02014-08-19 22:33:42 +02002705 private RemoteViews applyStandardTemplate(int resId) {
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02002706 return applyStandardTemplate(resId, true /* hasProgress */);
2707 }
2708
2709 /**
2710 * @param hasProgress whether the progress bar should be shown and set
2711 */
2712 private RemoteViews applyStandardTemplate(int resId, boolean hasProgress) {
Kenny Guy77320062014-08-27 21:37:15 +01002713 RemoteViews contentView = new BuilderRemoteViews(mContext.getApplicationInfo(), resId);
Dan Sandler539aad42014-08-04 00:43:39 -04002714
Christoph Studerfe718432014-09-01 18:21:18 +02002715 resetStandardTemplate(contentView);
2716
Daniel Sandler9f7936a2012-05-21 16:14:28 -04002717 boolean showLine3 = false;
2718 boolean showLine2 = false;
Kenny Guy98193ea2014-07-24 19:54:37 +01002719 boolean contentTextInLine2 = false;
Dan Sandler190d58d2014-05-15 09:33:39 -04002720
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01002721 if (mLargeIcon != null) {
2722 contentView.setImageViewBitmap(R.id.icon, mLargeIcon);
Christoph Studer239f8352014-08-25 15:13:18 +02002723 processLargeLegacyIcon(mLargeIcon, contentView);
Dan Sandler190d58d2014-05-15 09:33:39 -04002724 contentView.setImageViewResource(R.id.right_icon, mSmallIcon);
2725 contentView.setViewVisibility(R.id.right_icon, View.VISIBLE);
2726 processSmallRightIcon(mSmallIcon, contentView);
2727 } else { // small icon at left
2728 contentView.setImageViewResource(R.id.icon, mSmallIcon);
2729 contentView.setViewVisibility(R.id.icon, View.VISIBLE);
2730 processSmallIconAsLarge(mSmallIcon, contentView);
Joe Onorato561d3852010-11-20 18:09:34 -08002731 }
2732 if (mContentTitle != null) {
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01002733 contentView.setTextViewText(R.id.title, processLegacyText(mContentTitle));
Joe Onorato561d3852010-11-20 18:09:34 -08002734 }
2735 if (mContentText != null) {
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01002736 contentView.setTextViewText(R.id.text, processLegacyText(mContentText));
Daniel Sandler9f7936a2012-05-21 16:14:28 -04002737 showLine3 = true;
Joe Onorato561d3852010-11-20 18:09:34 -08002738 }
2739 if (mContentInfo != null) {
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01002740 contentView.setTextViewText(R.id.info, processLegacyText(mContentInfo));
Jeff Sharkey1c400132011-08-05 14:50:13 -07002741 contentView.setViewVisibility(R.id.info, View.VISIBLE);
Daniel Sandler9f7936a2012-05-21 16:14:28 -04002742 showLine3 = true;
Joe Onorato561d3852010-11-20 18:09:34 -08002743 } else if (mNumber > 0) {
Daniel Sandlerebce0112011-06-16 16:44:51 -04002744 final int tooBig = mContext.getResources().getInteger(
2745 R.integer.status_bar_notification_info_maxnum);
2746 if (mNumber > tooBig) {
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01002747 contentView.setTextViewText(R.id.info, processLegacyText(
2748 mContext.getResources().getString(
2749 R.string.status_bar_notification_info_overflow)));
Joe Onorato059a2f82011-01-04 10:27:01 -08002750 } else {
2751 NumberFormat f = NumberFormat.getIntegerInstance();
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01002752 contentView.setTextViewText(R.id.info, processLegacyText(f.format(mNumber)));
Joe Onorato059a2f82011-01-04 10:27:01 -08002753 }
Jeff Sharkey1c400132011-08-05 14:50:13 -07002754 contentView.setViewVisibility(R.id.info, View.VISIBLE);
Daniel Sandler9f7936a2012-05-21 16:14:28 -04002755 showLine3 = true;
Joe Onorato561d3852010-11-20 18:09:34 -08002756 } else {
2757 contentView.setViewVisibility(R.id.info, View.GONE);
2758 }
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002759
Daniel Sandler9f7936a2012-05-21 16:14:28 -04002760 // Need to show three lines?
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002761 if (mSubText != null) {
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01002762 contentView.setTextViewText(R.id.text, processLegacyText(mSubText));
Daniel Sandler9f7936a2012-05-21 16:14:28 -04002763 if (mContentText != null) {
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01002764 contentView.setTextViewText(R.id.text2, processLegacyText(mContentText));
Daniel Sandler9f7936a2012-05-21 16:14:28 -04002765 contentView.setViewVisibility(R.id.text2, View.VISIBLE);
2766 showLine2 = true;
Kenny Guy98193ea2014-07-24 19:54:37 +01002767 contentTextInLine2 = true;
Daniel Sandler9f7936a2012-05-21 16:14:28 -04002768 } else {
2769 contentView.setViewVisibility(R.id.text2, View.GONE);
2770 }
Jeff Sharkey1c400132011-08-05 14:50:13 -07002771 } else {
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002772 contentView.setViewVisibility(R.id.text2, View.GONE);
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02002773 if (hasProgress && (mProgressMax != 0 || mProgressIndeterminate)) {
Christoph Studeraca4b252014-09-09 15:58:41 +02002774 contentView.setViewVisibility(R.id.progress, View.VISIBLE);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002775 contentView.setProgressBar(
2776 R.id.progress, mProgressMax, mProgress, mProgressIndeterminate);
Jorim Jaggief72a192014-08-26 21:57:46 +02002777 contentView.setProgressBackgroundTintList(
2778 R.id.progress, ColorStateList.valueOf(mContext.getResources().getColor(
2779 R.color.notification_progress_background_color)));
2780 if (mColor != COLOR_DEFAULT) {
2781 ColorStateList colorStateList = ColorStateList.valueOf(mColor);
2782 contentView.setProgressTintList(R.id.progress, colorStateList);
2783 contentView.setProgressIndeterminateTintList(R.id.progress, colorStateList);
2784 }
Daniel Sandler9f7936a2012-05-21 16:14:28 -04002785 showLine2 = true;
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002786 } else {
2787 contentView.setViewVisibility(R.id.progress, View.GONE);
2788 }
Jeff Sharkey1c400132011-08-05 14:50:13 -07002789 }
Daniel Sandler9f7936a2012-05-21 16:14:28 -04002790 if (showLine2) {
Jorim Jaggi445d3c02014-08-19 22:33:42 +02002791
2792 // need to shrink all the type to make sure everything fits
Jorim Jaggid05aa3e2014-08-28 17:52:27 +02002793 shrinkLine3Text(contentView);
Daniel Sandler9f7936a2012-05-21 16:14:28 -04002794 }
2795
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02002796 if (showsTimeOrChronometer()) {
Daniel Sandlera2985ed2012-04-03 16:42:00 -04002797 if (mUseChronometer) {
2798 contentView.setViewVisibility(R.id.chronometer, View.VISIBLE);
2799 contentView.setLong(R.id.chronometer, "setBase",
2800 mWhen + (SystemClock.elapsedRealtime() - System.currentTimeMillis()));
2801 contentView.setBoolean(R.id.chronometer, "setStarted", true);
2802 } else {
2803 contentView.setViewVisibility(R.id.time, View.VISIBLE);
2804 contentView.setLong(R.id.time, "setTime", mWhen);
2805 }
Joe Onorato561d3852010-11-20 18:09:34 -08002806 }
Daniel Sandler0c890492012-09-12 17:23:10 -07002807
Jorim Jaggi445d3c02014-08-19 22:33:42 +02002808 // Adjust padding depending on line count and font size.
2809 contentView.setViewPadding(R.id.line1, 0, calculateTopPadding(mContext,
Jorim Jaggid05aa3e2014-08-28 17:52:27 +02002810 mHasThreeLines, mContext.getResources().getConfiguration().fontScale),
Jorim Jaggi445d3c02014-08-19 22:33:42 +02002811 0, 0);
2812
Kenny Guy98193ea2014-07-24 19:54:37 +01002813 // We want to add badge to first line of text.
2814 boolean addedBadge = addProfileBadge(contentView,
2815 contentTextInLine2 ? R.id.profile_badge_line2 : R.id.profile_badge_line3);
2816 // If we added the badge to line 3 then we should show line 3.
2817 if (addedBadge && !contentTextInLine2) {
2818 showLine3 = true;
2819 }
2820
2821 // Note getStandardView may hide line 3 again.
Daniel Sandler9f7936a2012-05-21 16:14:28 -04002822 contentView.setViewVisibility(R.id.line3, showLine3 ? View.VISIBLE : View.GONE);
Daniel Sandler6387d2f2012-05-22 13:44:09 -04002823 contentView.setViewVisibility(R.id.overflow_divider, showLine3 ? View.VISIBLE : View.GONE);
Joe Onorato561d3852010-11-20 18:09:34 -08002824 return contentView;
2825 }
2826
Jorim Jaggi445d3c02014-08-19 22:33:42 +02002827 /**
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02002828 * @return true if the built notification will show the time or the chronometer; false
2829 * otherwise
2830 */
2831 private boolean showsTimeOrChronometer() {
2832 return mWhen != 0 && mShowWhen;
2833 }
2834
2835 /**
Jorim Jaggi445d3c02014-08-19 22:33:42 +02002836 * Logic to find out whether the notification is going to have three lines in the contracted
2837 * layout. This is used to adjust the top padding.
2838 *
2839 * @return true if the notification is going to have three lines; false if the notification
2840 * is going to have one or two lines
2841 */
2842 private boolean hasThreeLines() {
Jorim Jaggid05aa3e2014-08-28 17:52:27 +02002843 boolean contentTextInLine2 = mSubText != null && mContentText != null;
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02002844 boolean hasProgress = mStyle == null || mStyle.hasProgress();
Jorim Jaggid05aa3e2014-08-28 17:52:27 +02002845 // If we have content text in line 2, badge goes into line 2, or line 3 otherwise
2846 boolean badgeInLine3 = getProfileBadgeDrawable() != null && !contentTextInLine2;
2847 boolean hasLine3 = mContentText != null || mContentInfo != null || mNumber > 0
2848 || badgeInLine3;
Jorim Jaggi445d3c02014-08-19 22:33:42 +02002849 boolean hasLine2 = (mSubText != null && mContentText != null) ||
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02002850 (hasProgress && mSubText == null
2851 && (mProgressMax != 0 || mProgressIndeterminate));
Jorim Jaggi445d3c02014-08-19 22:33:42 +02002852 return hasLine2 && hasLine3;
2853 }
2854
2855 /**
2856 * @hide
2857 */
2858 public static int calculateTopPadding(Context ctx, boolean hasThreeLines,
2859 float fontScale) {
2860 int padding = ctx.getResources().getDimensionPixelSize(hasThreeLines
2861 ? R.dimen.notification_top_pad_narrow
2862 : R.dimen.notification_top_pad);
2863 int largePadding = ctx.getResources().getDimensionPixelSize(hasThreeLines
2864 ? R.dimen.notification_top_pad_large_text_narrow
2865 : R.dimen.notification_top_pad_large_text);
2866 float largeFactor = (MathUtils.constrain(fontScale, 1.0f, LARGE_TEXT_SCALE) - 1f)
2867 / (LARGE_TEXT_SCALE - 1f);
2868
2869 // Linearly interpolate the padding between large and normal with the font scale ranging
2870 // from 1f to LARGE_TEXT_SCALE
2871 return Math.round((1 - largeFactor) * padding + largeFactor * largePadding);
2872 }
2873
Christoph Studerfe718432014-09-01 18:21:18 +02002874 private void resetStandardTemplateWithActions(RemoteViews big) {
2875 big.setViewVisibility(R.id.actions, View.GONE);
2876 big.setViewVisibility(R.id.action_divider, View.GONE);
2877 big.removeAllViews(R.id.actions);
2878 }
2879
Daniel Sandler96fd7c12012-03-30 16:37:36 -04002880 private RemoteViews applyStandardTemplateWithActions(int layoutId) {
Jorim Jaggi445d3c02014-08-19 22:33:42 +02002881 RemoteViews big = applyStandardTemplate(layoutId);
Daniel Sandler96fd7c12012-03-30 16:37:36 -04002882
Christoph Studerfe718432014-09-01 18:21:18 +02002883 resetStandardTemplateWithActions(big);
2884
Daniel Sandler96fd7c12012-03-30 16:37:36 -04002885 int N = mActions.size();
2886 if (N > 0) {
Daniel Sandler96fd7c12012-03-30 16:37:36 -04002887 big.setViewVisibility(R.id.actions, View.VISIBLE);
Daniel Sandler6387d2f2012-05-22 13:44:09 -04002888 big.setViewVisibility(R.id.action_divider, View.VISIBLE);
Daniel Sandler8680bf82012-05-15 16:52:52 -04002889 if (N>MAX_ACTION_BUTTONS) N=MAX_ACTION_BUTTONS;
Daniel Sandler96fd7c12012-03-30 16:37:36 -04002890 for (int i=0; i<N; i++) {
2891 final RemoteViews button = generateActionButton(mActions.get(i));
Daniel Sandler96fd7c12012-03-30 16:37:36 -04002892 big.addView(R.id.actions, button);
2893 }
2894 }
2895 return big;
2896 }
2897
Joe Onorato46439ce2010-11-19 13:56:21 -08002898 private RemoteViews makeContentView() {
2899 if (mContentView != null) {
2900 return mContentView;
2901 } else {
Jorim Jaggi445d3c02014-08-19 22:33:42 +02002902 return applyStandardTemplate(getBaseLayoutResource());
Joe Onorato46439ce2010-11-19 13:56:21 -08002903 }
2904 }
2905
2906 private RemoteViews makeTickerView() {
2907 if (mTickerView != null) {
2908 return mTickerView;
Joe Onorato46439ce2010-11-19 13:56:21 -08002909 }
Dan Sandler5fcdf6e2014-07-18 11:31:15 -04002910 return null; // tickers are not created by default anymore
Joe Onorato46439ce2010-11-19 13:56:21 -08002911 }
2912
Daniel Sandler96fd7c12012-03-30 16:37:36 -04002913 private RemoteViews makeBigContentView() {
2914 if (mActions.size() == 0) return null;
2915
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01002916 return applyStandardTemplateWithActions(getBigBaseLayoutResource());
Daniel Sandler96fd7c12012-03-30 16:37:36 -04002917 }
2918
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01002919 private RemoteViews makeHeadsUpContentView() {
Chris Wren8fd39ec2014-02-27 17:43:26 -05002920 if (mActions.size() == 0) return null;
2921
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01002922 return applyStandardTemplateWithActions(getBigBaseLayoutResource());
Chris Wren8fd39ec2014-02-27 17:43:26 -05002923 }
2924
2925
Daniel Sandler96fd7c12012-03-30 16:37:36 -04002926 private RemoteViews generateActionButton(Action action) {
Daniel Sandler8680bf82012-05-15 16:52:52 -04002927 final boolean tombstone = (action.actionIntent == null);
Joe Malin8d40d042012-11-05 11:36:40 -08002928 RemoteViews button = new RemoteViews(mContext.getPackageName(),
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01002929 tombstone ? getActionTombstoneLayoutResource()
2930 : getActionLayoutResource());
Chris Wren8749ac8a2013-12-03 14:31:01 -05002931 button.setTextViewCompoundDrawablesRelative(R.id.action0, action.icon, 0, 0, 0);
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01002932 button.setTextViewText(R.id.action0, processLegacyText(action.title));
Daniel Sandler8680bf82012-05-15 16:52:52 -04002933 if (!tombstone) {
Daniel Sandlere5518842012-05-10 16:20:40 -04002934 button.setOnClickPendingIntent(R.id.action0, action.actionIntent);
Daniel Sandlere5518842012-05-10 16:20:40 -04002935 }
Daniel Sandler96fd7c12012-03-30 16:37:36 -04002936 button.setContentDescription(R.id.action0, action.title);
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01002937 processLegacyAction(action, button);
Daniel Sandler96fd7c12012-03-30 16:37:36 -04002938 return button;
2939 }
2940
Joe Onoratocb109a02011-01-18 17:57:41 -08002941 /**
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01002942 * @return Whether we are currently building a notification from a legacy (an app that
Alan Viverette3cb07a462014-06-06 14:19:53 -07002943 * doesn't create material notifications by itself) app.
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01002944 */
2945 private boolean isLegacy() {
Dan Sandler26e81cf2014-05-06 10:01:27 -04002946 return mColorUtil != null;
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01002947 }
2948
2949 private void processLegacyAction(Action action, RemoteViews button) {
Dan Sandler05c362d2014-09-03 00:16:27 +02002950 if (!isLegacy() || mColorUtil.isGrayscaleIcon(mContext, action.icon)) {
Christoph Studer239f8352014-08-25 15:13:18 +02002951 button.setTextViewCompoundDrawablesRelativeColorFilter(R.id.action0, 0,
2952 mContext.getResources().getColor(R.color.notification_action_color_filter),
2953 PorterDuff.Mode.MULTIPLY);
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01002954 }
2955 }
2956
2957 private CharSequence processLegacyText(CharSequence charSequence) {
2958 if (isLegacy()) {
Dan Sandler26e81cf2014-05-06 10:01:27 -04002959 return mColorUtil.invertCharSequenceColors(charSequence);
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01002960 } else {
2961 return charSequence;
2962 }
2963 }
2964
Dan Sandler26e81cf2014-05-06 10:01:27 -04002965 /**
2966 * Apply any necessary background to smallIcons being used in the largeIcon spot.
2967 */
2968 private void processSmallIconAsLarge(int largeIconId, RemoteViews contentView) {
Dan Sandler05c362d2014-09-03 00:16:27 +02002969 if (!isLegacy() || mColorUtil.isGrayscaleIcon(mContext, largeIconId)) {
Dan Sandler26e81cf2014-05-06 10:01:27 -04002970 applyLargeIconBackground(contentView);
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01002971 }
2972 }
2973
Dan Sandler26e81cf2014-05-06 10:01:27 -04002974 /**
2975 * Apply any necessary background to a largeIcon if it's a fake smallIcon (that is,
2976 * if it's grayscale).
2977 */
2978 // TODO: also check bounds, transparency, that sort of thing.
Christoph Studer239f8352014-08-25 15:13:18 +02002979 private void processLargeLegacyIcon(Bitmap largeIcon, RemoteViews contentView) {
Dan Sandler05c362d2014-09-03 00:16:27 +02002980 if (isLegacy() && mColorUtil.isGrayscaleIcon(largeIcon)) {
Dan Sandler26e81cf2014-05-06 10:01:27 -04002981 applyLargeIconBackground(contentView);
Dan Sandler190d58d2014-05-15 09:33:39 -04002982 } else {
2983 removeLargeIconBackground(contentView);
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01002984 }
2985 }
2986
Dan Sandler26e81cf2014-05-06 10:01:27 -04002987 /**
2988 * Add a colored circle behind the largeIcon slot.
2989 */
2990 private void applyLargeIconBackground(RemoteViews contentView) {
2991 contentView.setInt(R.id.icon, "setBackgroundResource",
Jorim Jaggi445d3c02014-08-19 22:33:42 +02002992 R.drawable.notification_icon_legacy_bg);
Dan Sandler26e81cf2014-05-06 10:01:27 -04002993
2994 contentView.setDrawableParameters(
2995 R.id.icon,
2996 true,
2997 -1,
Jorim Jaggi74419312014-06-10 20:57:21 +02002998 resolveColor(),
Dan Sandler26e81cf2014-05-06 10:01:27 -04002999 PorterDuff.Mode.SRC_ATOP,
3000 -1);
Jorim Jaggi445d3c02014-08-19 22:33:42 +02003001
3002 int padding = mContext.getResources().getDimensionPixelSize(
3003 R.dimen.notification_large_icon_circle_padding);
3004 contentView.setViewPadding(R.id.icon, padding, padding, padding, padding);
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01003005 }
3006
Dan Sandler190d58d2014-05-15 09:33:39 -04003007 private void removeLargeIconBackground(RemoteViews contentView) {
3008 contentView.setInt(R.id.icon, "setBackgroundResource", 0);
3009 }
3010
Dan Sandler26e81cf2014-05-06 10:01:27 -04003011 /**
3012 * Recolor small icons when used in the R.id.right_icon slot.
3013 */
Dan Sandler190d58d2014-05-15 09:33:39 -04003014 private void processSmallRightIcon(int smallIconDrawableId,
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01003015 RemoteViews contentView) {
Dan Sandler05c362d2014-09-03 00:16:27 +02003016 if (!isLegacy() || mColorUtil.isGrayscaleIcon(mContext, smallIconDrawableId)) {
Dan Sandler190d58d2014-05-15 09:33:39 -04003017 contentView.setDrawableParameters(R.id.right_icon, false, -1,
3018 0xFFFFFFFF,
3019 PorterDuff.Mode.SRC_ATOP, -1);
3020
3021 contentView.setInt(R.id.right_icon,
3022 "setBackgroundResource",
3023 R.drawable.notification_icon_legacy_bg);
3024
3025 contentView.setDrawableParameters(
3026 R.id.right_icon,
3027 true,
3028 -1,
Jorim Jaggi74419312014-06-10 20:57:21 +02003029 resolveColor(),
Dan Sandler190d58d2014-05-15 09:33:39 -04003030 PorterDuff.Mode.SRC_ATOP,
3031 -1);
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01003032 }
3033 }
3034
Jorim Jaggi74419312014-06-10 20:57:21 +02003035 private int sanitizeColor() {
3036 if (mColor != COLOR_DEFAULT) {
3037 mColor |= 0xFF000000; // no alpha for custom colors
3038 }
3039 return mColor;
3040 }
3041
Dan Sandler26e81cf2014-05-06 10:01:27 -04003042 private int resolveColor() {
3043 if (mColor == COLOR_DEFAULT) {
Jorim Jaggi74419312014-06-10 20:57:21 +02003044 return mContext.getResources().getColor(R.color.notification_icon_bg_color);
Dan Sandler26e81cf2014-05-06 10:01:27 -04003045 }
3046 return mColor;
3047 }
3048
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01003049 /**
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003050 * Apply the unstyled operations and return a new {@link Notification} object.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04003051 * @hide
Joe Onoratocb109a02011-01-18 17:57:41 -08003052 */
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04003053 public Notification buildUnstyled() {
Joe Onorato46439ce2010-11-19 13:56:21 -08003054 Notification n = new Notification();
3055 n.when = mWhen;
3056 n.icon = mSmallIcon;
3057 n.iconLevel = mSmallIconLevel;
Joe Onorato8595a3d2010-11-19 18:12:07 -08003058 n.number = mNumber;
Dan Sandler26e81cf2014-05-06 10:01:27 -04003059
Jorim Jaggi74419312014-06-10 20:57:21 +02003060 n.color = sanitizeColor();
Dan Sandler26e81cf2014-05-06 10:01:27 -04003061
Christoph Studer4600f9b2014-07-22 22:44:43 +02003062 setBuilderContentView(n, makeContentView());
Joe Onorato46439ce2010-11-19 13:56:21 -08003063 n.contentIntent = mContentIntent;
3064 n.deleteIntent = mDeleteIntent;
3065 n.fullScreenIntent = mFullScreenIntent;
3066 n.tickerText = mTickerText;
3067 n.tickerView = makeTickerView();
3068 n.largeIcon = mLargeIcon;
3069 n.sound = mSound;
3070 n.audioStreamType = mAudioStreamType;
John Spurlockc0650f022014-07-19 13:22:39 -04003071 n.audioAttributes = mAudioAttributes;
Joe Onorato46439ce2010-11-19 13:56:21 -08003072 n.vibrate = mVibrate;
3073 n.ledARGB = mLedArgb;
3074 n.ledOnMS = mLedOnMs;
3075 n.ledOffMS = mLedOffMs;
3076 n.defaults = mDefaults;
3077 n.flags = mFlags;
Christoph Studer4600f9b2014-07-22 22:44:43 +02003078 setBuilderBigContentView(n, makeBigContentView());
3079 setBuilderHeadsUpContentView(n, makeHeadsUpContentView());
Daniel Sandler26c13432013-04-04 11:01:04 -04003080 if (mLedOnMs != 0 || mLedOffMs != 0) {
Joe Onorato8d0b6552010-11-22 16:09:29 -08003081 n.flags |= FLAG_SHOW_LIGHTS;
3082 }
3083 if ((mDefaults & DEFAULT_LIGHTS) != 0) {
3084 n.flags |= FLAG_SHOW_LIGHTS;
3085 }
John Spurlockfd7f1e02014-03-18 16:41:57 -04003086 n.category = mCategory;
Griff Hazen5cadc3b2014-05-20 09:55:39 -07003087 n.mGroupKey = mGroupKey;
3088 n.mSortKey = mSortKey;
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003089 n.priority = mPriority;
Daniel Sandlera0a938c2012-03-15 08:42:37 -04003090 if (mActions.size() > 0) {
3091 n.actions = new Action[mActions.size()];
3092 mActions.toArray(n.actions);
3093 }
Dan Sandler0bf2ed82013-12-21 23:33:41 -06003094 n.visibility = mVisibility;
3095
3096 if (mPublicVersion != null) {
3097 n.publicVersion = new Notification();
3098 mPublicVersion.cloneInto(n.publicVersion, true);
3099 }
Christoph Studer4600f9b2014-07-22 22:44:43 +02003100 // Note: If you're adding new fields, also update restoreFromNotitification().
Joe Onorato46439ce2010-11-19 13:56:21 -08003101 return n;
3102 }
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003103
3104 /**
Daniel Sandlerf45564e2013-04-15 15:05:08 -04003105 * Capture, in the provided bundle, semantic information used in the construction of
3106 * this Notification object.
3107 * @hide
3108 */
Griff Hazen720042b2014-02-24 15:46:56 -08003109 public void populateExtras(Bundle extras) {
Daniel Sandlerf45564e2013-04-15 15:05:08 -04003110 // Store original information used in the construction of this object
Kenny Guy8942bcd2014-09-08 21:09:47 +01003111 extras.putInt(EXTRA_ORIGINATING_USERID, mOriginatingUserId);
Kenny Guy77320062014-08-27 21:37:15 +01003112 extras.putParcelable(EXTRA_REBUILD_CONTEXT_APPLICATION_INFO,
3113 mContext.getApplicationInfo());
Daniel Sandlerf45564e2013-04-15 15:05:08 -04003114 extras.putCharSequence(EXTRA_TITLE, mContentTitle);
3115 extras.putCharSequence(EXTRA_TEXT, mContentText);
3116 extras.putCharSequence(EXTRA_SUB_TEXT, mSubText);
3117 extras.putCharSequence(EXTRA_INFO_TEXT, mContentInfo);
3118 extras.putInt(EXTRA_SMALL_ICON, mSmallIcon);
3119 extras.putInt(EXTRA_PROGRESS, mProgress);
3120 extras.putInt(EXTRA_PROGRESS_MAX, mProgressMax);
3121 extras.putBoolean(EXTRA_PROGRESS_INDETERMINATE, mProgressIndeterminate);
3122 extras.putBoolean(EXTRA_SHOW_CHRONOMETER, mUseChronometer);
3123 extras.putBoolean(EXTRA_SHOW_WHEN, mShowWhen);
John Spurlockac08a472013-06-10 11:37:08 -04003124 if (mLargeIcon != null) {
3125 extras.putParcelable(EXTRA_LARGE_ICON, mLargeIcon);
3126 }
Chris Wrendde75302014-03-26 17:24:15 -04003127 if (!mPeople.isEmpty()) {
3128 extras.putStringArray(EXTRA_PEOPLE, mPeople.toArray(new String[mPeople.size()]));
3129 }
Christoph Studer4600f9b2014-07-22 22:44:43 +02003130 // NOTE: If you're adding new extras also update restoreFromNotification().
3131 }
3132
3133
3134 /**
3135 * @hide
3136 */
3137 public static void stripForDelivery(Notification n) {
3138 if (!STRIP_AND_REBUILD) {
3139 return;
3140 }
3141
3142 String templateClass = n.extras.getString(EXTRA_TEMPLATE);
3143 // Only strip views for known Styles because we won't know how to
3144 // re-create them otherwise.
3145 boolean stripViews = TextUtils.isEmpty(templateClass) ||
3146 getNotificationStyleClass(templateClass) != null;
3147
3148 boolean isStripped = false;
3149
3150 if (n.largeIcon != null && n.extras.containsKey(EXTRA_LARGE_ICON)) {
3151 // TODO: Would like to check for equality here, but if the notification
3152 // has been cloned, we can't.
3153 n.largeIcon = null;
3154 n.extras.putBoolean(EXTRA_REBUILD_LARGE_ICON, true);
3155 isStripped = true;
3156 }
3157 // Get rid of unmodified BuilderRemoteViews.
3158
3159 if (stripViews &&
3160 n.contentView instanceof BuilderRemoteViews &&
3161 n.extras.getInt(EXTRA_REBUILD_CONTENT_VIEW_ACTION_COUNT, -1) ==
3162 n.contentView.getSequenceNumber()) {
3163 n.contentView = null;
3164 n.extras.putBoolean(EXTRA_REBUILD_CONTENT_VIEW, true);
3165 n.extras.remove(EXTRA_REBUILD_CONTENT_VIEW_ACTION_COUNT);
3166 isStripped = true;
3167 }
3168 if (stripViews &&
3169 n.bigContentView instanceof BuilderRemoteViews &&
3170 n.extras.getInt(EXTRA_REBUILD_BIG_CONTENT_VIEW_ACTION_COUNT, -1) ==
3171 n.bigContentView.getSequenceNumber()) {
3172 n.bigContentView = null;
3173 n.extras.putBoolean(EXTRA_REBUILD_BIG_CONTENT_VIEW, true);
3174 n.extras.remove(EXTRA_REBUILD_BIG_CONTENT_VIEW_ACTION_COUNT);
3175 isStripped = true;
3176 }
3177 if (stripViews &&
3178 n.headsUpContentView instanceof BuilderRemoteViews &&
3179 n.extras.getInt(EXTRA_REBUILD_HEADS_UP_CONTENT_VIEW_ACTION_COUNT, -1) ==
3180 n.headsUpContentView.getSequenceNumber()) {
3181 n.headsUpContentView = null;
3182 n.extras.putBoolean(EXTRA_REBUILD_HEADS_UP_CONTENT_VIEW, true);
3183 n.extras.remove(EXTRA_REBUILD_HEADS_UP_CONTENT_VIEW_ACTION_COUNT);
3184 isStripped = true;
3185 }
3186
3187 if (isStripped) {
3188 n.extras.putBoolean(EXTRA_NEEDS_REBUILD, true);
3189 }
3190 }
3191
3192 /**
3193 * @hide
3194 */
3195 public static Notification rebuild(Context context, Notification n) {
3196 Bundle extras = n.extras;
3197 if (!extras.getBoolean(EXTRA_NEEDS_REBUILD)) return n;
3198 extras.remove(EXTRA_NEEDS_REBUILD);
3199
3200 // Re-create notification context so we can access app resources.
Kenny Guy77320062014-08-27 21:37:15 +01003201 ApplicationInfo applicationInfo = extras.getParcelable(
3202 EXTRA_REBUILD_CONTEXT_APPLICATION_INFO);
Christoph Studer4600f9b2014-07-22 22:44:43 +02003203 Context builderContext;
3204 try {
Kenny Guy77320062014-08-27 21:37:15 +01003205 builderContext = context.createApplicationContext(applicationInfo,
Christoph Studer4600f9b2014-07-22 22:44:43 +02003206 Context.CONTEXT_RESTRICTED);
3207 } catch (NameNotFoundException e) {
Kenny Guy77320062014-08-27 21:37:15 +01003208 Log.e(TAG, "ApplicationInfo " + applicationInfo + " not found");
Christoph Studer4600f9b2014-07-22 22:44:43 +02003209 builderContext = context; // try with our context
3210 }
3211
3212 Builder b = new Builder(builderContext, n);
3213 return b.rebuild();
3214 }
3215
3216 /**
3217 * Rebuilds the notification passed in to the rebuild-constructor
3218 * {@link #Builder(Context, Notification)}.
3219 *
3220 * <p>
3221 * Throws IllegalStateException when invoked on a Builder that isn't in rebuild mode.
3222 *
3223 * @hide
3224 */
3225 private Notification rebuild() {
3226 if (mRebuildNotification == null) {
3227 throw new IllegalStateException("rebuild() only valid when in 'rebuild' mode.");
3228 }
Jorim Jaggid05aa3e2014-08-28 17:52:27 +02003229 mHasThreeLines = hasThreeLines();
Christoph Studer4600f9b2014-07-22 22:44:43 +02003230
3231 Bundle extras = mRebuildNotification.extras;
3232
3233 if (extras.getBoolean(EXTRA_REBUILD_LARGE_ICON)) {
3234 mRebuildNotification.largeIcon = extras.getParcelable(EXTRA_LARGE_ICON);
3235 }
3236 extras.remove(EXTRA_REBUILD_LARGE_ICON);
3237
3238 if (extras.getBoolean(EXTRA_REBUILD_CONTENT_VIEW)) {
3239 setBuilderContentView(mRebuildNotification, makeContentView());
3240 if (mStyle != null) {
3241 mStyle.populateContentView(mRebuildNotification);
3242 }
3243 }
3244 extras.remove(EXTRA_REBUILD_CONTENT_VIEW);
3245
3246 if (extras.getBoolean(EXTRA_REBUILD_BIG_CONTENT_VIEW)) {
3247 setBuilderBigContentView(mRebuildNotification, makeBigContentView());
3248 if (mStyle != null) {
3249 mStyle.populateBigContentView(mRebuildNotification);
3250 }
3251 }
3252 extras.remove(EXTRA_REBUILD_BIG_CONTENT_VIEW);
3253
3254 if (extras.getBoolean(EXTRA_REBUILD_HEADS_UP_CONTENT_VIEW)) {
3255 setBuilderHeadsUpContentView(mRebuildNotification, makeHeadsUpContentView());
3256 if (mStyle != null) {
3257 mStyle.populateHeadsUpContentView(mRebuildNotification);
3258 }
3259 }
3260 extras.remove(EXTRA_REBUILD_HEADS_UP_CONTENT_VIEW);
3261
Jorim Jaggid05aa3e2014-08-28 17:52:27 +02003262 mHasThreeLines = false;
Christoph Studer4600f9b2014-07-22 22:44:43 +02003263 return mRebuildNotification;
3264 }
3265
3266 private static Class<? extends Style> getNotificationStyleClass(String templateClass) {
3267 Class<? extends Style>[] classes = new Class[]{
3268 BigTextStyle.class, BigPictureStyle.class, InboxStyle.class, MediaStyle.class};
3269 for (Class<? extends Style> innerClass : classes) {
3270 if (templateClass.equals(innerClass.getName())) {
3271 return innerClass;
3272 }
3273 }
3274 return null;
3275 }
3276
3277 private void setBuilderContentView(Notification n, RemoteViews contentView) {
3278 n.contentView = contentView;
3279 if (contentView instanceof BuilderRemoteViews) {
3280 mRebuildBundle.putInt(Builder.EXTRA_REBUILD_CONTENT_VIEW_ACTION_COUNT,
3281 contentView.getSequenceNumber());
3282 }
3283 }
3284
3285 private void setBuilderBigContentView(Notification n, RemoteViews bigContentView) {
3286 n.bigContentView = bigContentView;
3287 if (bigContentView instanceof BuilderRemoteViews) {
3288 mRebuildBundle.putInt(Builder.EXTRA_REBUILD_BIG_CONTENT_VIEW_ACTION_COUNT,
3289 bigContentView.getSequenceNumber());
3290 }
3291 }
3292
3293 private void setBuilderHeadsUpContentView(Notification n,
3294 RemoteViews headsUpContentView) {
3295 n.headsUpContentView = headsUpContentView;
3296 if (headsUpContentView instanceof BuilderRemoteViews) {
3297 mRebuildBundle.putInt(Builder.EXTRA_REBUILD_HEADS_UP_CONTENT_VIEW_ACTION_COUNT,
3298 headsUpContentView.getSequenceNumber());
3299 }
3300 }
3301
3302 private void restoreFromNotification(Notification n) {
3303
3304 // Notification fields.
3305 mWhen = n.when;
3306 mSmallIcon = n.icon;
3307 mSmallIconLevel = n.iconLevel;
3308 mNumber = n.number;
3309
3310 mColor = n.color;
3311
3312 mContentView = n.contentView;
3313 mDeleteIntent = n.deleteIntent;
3314 mFullScreenIntent = n.fullScreenIntent;
3315 mTickerText = n.tickerText;
3316 mTickerView = n.tickerView;
3317 mLargeIcon = n.largeIcon;
3318 mSound = n.sound;
3319 mAudioStreamType = n.audioStreamType;
3320 mAudioAttributes = n.audioAttributes;
3321
3322 mVibrate = n.vibrate;
3323 mLedArgb = n.ledARGB;
3324 mLedOnMs = n.ledOnMS;
3325 mLedOffMs = n.ledOffMS;
3326 mDefaults = n.defaults;
3327 mFlags = n.flags;
3328
3329 mCategory = n.category;
3330 mGroupKey = n.mGroupKey;
3331 mSortKey = n.mSortKey;
3332 mPriority = n.priority;
3333 mActions.clear();
3334 if (n.actions != null) {
3335 Collections.addAll(mActions, n.actions);
3336 }
3337 mVisibility = n.visibility;
3338
3339 mPublicVersion = n.publicVersion;
3340
3341 // Extras.
3342 Bundle extras = n.extras;
Kenny Guy8942bcd2014-09-08 21:09:47 +01003343 mOriginatingUserId = extras.getInt(EXTRA_ORIGINATING_USERID);
Christoph Studer4600f9b2014-07-22 22:44:43 +02003344 mContentTitle = extras.getCharSequence(EXTRA_TITLE);
3345 mContentText = extras.getCharSequence(EXTRA_TEXT);
3346 mSubText = extras.getCharSequence(EXTRA_SUB_TEXT);
3347 mContentInfo = extras.getCharSequence(EXTRA_INFO_TEXT);
3348 mSmallIcon = extras.getInt(EXTRA_SMALL_ICON);
3349 mProgress = extras.getInt(EXTRA_PROGRESS);
3350 mProgressMax = extras.getInt(EXTRA_PROGRESS_MAX);
3351 mProgressIndeterminate = extras.getBoolean(EXTRA_PROGRESS_INDETERMINATE);
3352 mUseChronometer = extras.getBoolean(EXTRA_SHOW_CHRONOMETER);
3353 mShowWhen = extras.getBoolean(EXTRA_SHOW_WHEN);
3354 if (extras.containsKey(EXTRA_LARGE_ICON)) {
3355 mLargeIcon = extras.getParcelable(EXTRA_LARGE_ICON);
3356 }
3357 if (extras.containsKey(EXTRA_PEOPLE)) {
3358 mPeople.clear();
3359 Collections.addAll(mPeople, extras.getStringArray(EXTRA_PEOPLE));
3360 }
Daniel Sandlerf45564e2013-04-15 15:05:08 -04003361 }
3362
3363 /**
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003364 * @deprecated Use {@link #build()} instead.
3365 */
3366 @Deprecated
3367 public Notification getNotification() {
3368 return build();
3369 }
3370
3371 /**
3372 * Combine all of the options that have been set and return a new {@link Notification}
3373 * object.
3374 */
3375 public Notification build() {
Kenny Guy8942bcd2014-09-08 21:09:47 +01003376 mOriginatingUserId = mContext.getUserId();
Jorim Jaggid05aa3e2014-08-28 17:52:27 +02003377 mHasThreeLines = hasThreeLines();
Christoph Studer943aa672014-08-03 20:31:16 +02003378
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04003379 Notification n = buildUnstyled();
Daniel Sandlerf45564e2013-04-15 15:05:08 -04003380
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003381 if (mStyle != null) {
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04003382 n = mStyle.buildStyled(n);
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003383 }
Daniel Sandlerf45564e2013-04-15 15:05:08 -04003384
Christoph Studer4600f9b2014-07-22 22:44:43 +02003385 if (mExtras != null) {
3386 n.extras.putAll(mExtras);
3387 }
3388
3389 if (mRebuildBundle.size() > 0) {
3390 n.extras.putAll(mRebuildBundle);
3391 mRebuildBundle.clear();
3392 }
Daniel Sandlerf45564e2013-04-15 15:05:08 -04003393
Griff Hazen720042b2014-02-24 15:46:56 -08003394 populateExtras(n.extras);
Daniel Sandlerf45564e2013-04-15 15:05:08 -04003395 if (mStyle != null) {
3396 mStyle.addExtras(n.extras);
3397 }
3398
Jorim Jaggid05aa3e2014-08-28 17:52:27 +02003399 mHasThreeLines = false;
Daniel Sandlerf45564e2013-04-15 15:05:08 -04003400 return n;
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003401 }
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05003402
3403 /**
3404 * Apply this Builder to an existing {@link Notification} object.
3405 *
3406 * @hide
3407 */
3408 public Notification buildInto(Notification n) {
Daniel Sandler1a497d32013-04-18 14:52:45 -04003409 build().cloneInto(n, true);
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05003410 return n;
3411 }
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01003412
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01003413 private int getBaseLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07003414 return R.layout.notification_template_material_base;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01003415 }
3416
3417 private int getBigBaseLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07003418 return R.layout.notification_template_material_big_base;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01003419 }
3420
3421 private int getBigPictureLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07003422 return R.layout.notification_template_material_big_picture;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01003423 }
3424
3425 private int getBigTextLayoutResource() {
Jorim Jaggi445d3c02014-08-19 22:33:42 +02003426 return R.layout.notification_template_material_big_text;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01003427 }
3428
3429 private int getInboxLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07003430 return R.layout.notification_template_material_inbox;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01003431 }
3432
3433 private int getActionLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07003434 return R.layout.notification_material_action;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01003435 }
3436
3437 private int getActionTombstoneLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07003438 return R.layout.notification_material_action_tombstone;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01003439 }
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003440 }
3441
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003442 /**
3443 * An object that can apply a rich notification style to a {@link Notification.Builder}
3444 * object.
3445 */
Griff Hazendfcb0802014-02-11 12:00:00 -08003446 public static abstract class Style {
Chris Wrend6297db2012-05-03 16:20:13 -04003447 private CharSequence mBigContentTitle;
Jorim Jaggi457a10d2014-09-08 16:18:23 +02003448
3449 /**
3450 * @hide
3451 */
3452 protected CharSequence mSummaryText = null;
3453
3454 /**
3455 * @hide
3456 */
3457 protected boolean mSummaryTextSet = false;
Chris Wrend6297db2012-05-03 16:20:13 -04003458
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003459 protected Builder mBuilder;
3460
Chris Wrend6297db2012-05-03 16:20:13 -04003461 /**
3462 * Overrides ContentTitle in the big form of the template.
3463 * This defaults to the value passed to setContentTitle().
3464 */
3465 protected void internalSetBigContentTitle(CharSequence title) {
3466 mBigContentTitle = title;
3467 }
3468
3469 /**
3470 * Set the first line of text after the detail section in the big form of the template.
3471 */
3472 protected void internalSetSummaryText(CharSequence cs) {
3473 mSummaryText = cs;
Daniel Sandler619738c2012-06-07 16:33:08 -04003474 mSummaryTextSet = true;
Chris Wrend6297db2012-05-03 16:20:13 -04003475 }
3476
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003477 public void setBuilder(Builder builder) {
3478 if (mBuilder != builder) {
3479 mBuilder = builder;
Daniel Sandlerc08dea22012-06-28 08:35:24 -07003480 if (mBuilder != null) {
3481 mBuilder.setStyle(this);
3482 }
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003483 }
3484 }
3485
Chris Wrend6297db2012-05-03 16:20:13 -04003486 protected void checkBuilder() {
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003487 if (mBuilder == null) {
3488 throw new IllegalArgumentException("Style requires a valid Builder object");
3489 }
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003490 }
Chris Wrend6297db2012-05-03 16:20:13 -04003491
3492 protected RemoteViews getStandardView(int layoutId) {
3493 checkBuilder();
3494
Christoph Studer4600f9b2014-07-22 22:44:43 +02003495 // Nasty.
3496 CharSequence oldBuilderContentTitle = mBuilder.mContentTitle;
Chris Wrend6297db2012-05-03 16:20:13 -04003497 if (mBigContentTitle != null) {
3498 mBuilder.setContentTitle(mBigContentTitle);
3499 }
3500
Chris Wrend6297db2012-05-03 16:20:13 -04003501 RemoteViews contentView = mBuilder.applyStandardTemplateWithActions(layoutId);
3502
Christoph Studer4600f9b2014-07-22 22:44:43 +02003503 mBuilder.mContentTitle = oldBuilderContentTitle;
3504
Chris Wrend6297db2012-05-03 16:20:13 -04003505 if (mBigContentTitle != null && mBigContentTitle.equals("")) {
3506 contentView.setViewVisibility(R.id.line1, View.GONE);
Chris Wren67dc9a02012-05-16 01:03:20 -04003507 } else {
3508 contentView.setViewVisibility(R.id.line1, View.VISIBLE);
Chris Wrend6297db2012-05-03 16:20:13 -04003509 }
3510
Daniel Sandler619738c2012-06-07 16:33:08 -04003511 // The last line defaults to the subtext, but can be replaced by mSummaryText
3512 final CharSequence overflowText =
3513 mSummaryTextSet ? mSummaryText
3514 : mBuilder.mSubText;
3515 if (overflowText != null) {
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01003516 contentView.setTextViewText(R.id.text, mBuilder.processLegacyText(overflowText));
Daniel Sandler619738c2012-06-07 16:33:08 -04003517 contentView.setViewVisibility(R.id.overflow_divider, View.VISIBLE);
Daniel Sandler9f7936a2012-05-21 16:14:28 -04003518 contentView.setViewVisibility(R.id.line3, View.VISIBLE);
Daniel Sandler916ad912012-06-13 12:17:07 -04003519 } else {
Kenny Guy98193ea2014-07-24 19:54:37 +01003520 // Clear text in case we use the line to show the profile badge.
3521 contentView.setTextViewText(R.id.text, "");
Daniel Sandler916ad912012-06-13 12:17:07 -04003522 contentView.setViewVisibility(R.id.overflow_divider, View.GONE);
3523 contentView.setViewVisibility(R.id.line3, View.GONE);
Chris Wrend6297db2012-05-03 16:20:13 -04003524 }
3525
3526 return contentView;
3527 }
3528
Daniel Sandlerf45564e2013-04-15 15:05:08 -04003529 /**
Jorim Jaggi445d3c02014-08-19 22:33:42 +02003530 * Changes the padding of the first line such that the big and small content view have the
3531 * same top padding.
3532 *
3533 * @hide
3534 */
3535 protected void applyTopPadding(RemoteViews contentView) {
3536 int topPadding = Builder.calculateTopPadding(mBuilder.mContext,
Jorim Jaggid05aa3e2014-08-28 17:52:27 +02003537 mBuilder.mHasThreeLines,
Jorim Jaggi445d3c02014-08-19 22:33:42 +02003538 mBuilder.mContext.getResources().getConfiguration().fontScale);
3539 contentView.setViewPadding(R.id.line1, 0, topPadding, 0, 0);
3540 }
3541
3542 /**
Daniel Sandlerf45564e2013-04-15 15:05:08 -04003543 * @hide
3544 */
3545 public void addExtras(Bundle extras) {
3546 if (mSummaryTextSet) {
3547 extras.putCharSequence(EXTRA_SUMMARY_TEXT, mSummaryText);
3548 }
3549 if (mBigContentTitle != null) {
3550 extras.putCharSequence(EXTRA_TITLE_BIG, mBigContentTitle);
3551 }
Chris Wren91ad5632013-06-05 15:05:57 -04003552 extras.putString(EXTRA_TEMPLATE, this.getClass().getName());
Daniel Sandlerf45564e2013-04-15 15:05:08 -04003553 }
3554
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04003555 /**
3556 * @hide
3557 */
Christoph Studer4600f9b2014-07-22 22:44:43 +02003558 protected void restoreFromExtras(Bundle extras) {
3559 if (extras.containsKey(EXTRA_SUMMARY_TEXT)) {
3560 mSummaryText = extras.getCharSequence(EXTRA_SUMMARY_TEXT);
3561 mSummaryTextSet = true;
3562 }
3563 if (extras.containsKey(EXTRA_TITLE_BIG)) {
3564 mBigContentTitle = extras.getCharSequence(EXTRA_TITLE_BIG);
3565 }
3566 }
3567
3568
3569 /**
3570 * @hide
3571 */
3572 public Notification buildStyled(Notification wip) {
3573 populateTickerView(wip);
3574 populateContentView(wip);
3575 populateBigContentView(wip);
3576 populateHeadsUpContentView(wip);
3577 return wip;
3578 }
3579
3580 // The following methods are split out so we can re-create notification partially.
3581 /**
3582 * @hide
3583 */
3584 protected void populateTickerView(Notification wip) {}
3585 /**
3586 * @hide
3587 */
3588 protected void populateContentView(Notification wip) {}
3589
3590 /**
3591 * @hide
3592 */
3593 protected void populateBigContentView(Notification wip) {}
3594
3595 /**
3596 * @hide
3597 */
3598 protected void populateHeadsUpContentView(Notification wip) {}
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04003599
3600 /**
3601 * Calls {@link android.app.Notification.Builder#build()} on the Builder this Style is
3602 * attached to.
3603 *
3604 * @return the fully constructed Notification.
3605 */
3606 public Notification build() {
3607 checkBuilder();
3608 return mBuilder.build();
3609 }
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02003610
3611 /**
3612 * @hide
3613 * @return true if the style positions the progress bar on the second line; false if the
3614 * style hides the progress bar
3615 */
3616 protected boolean hasProgress() {
3617 return true;
3618 }
Joe Onorato46439ce2010-11-19 13:56:21 -08003619 }
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003620
3621 /**
Daniel Sandler4dfbe832012-04-11 14:51:46 -04003622 * Helper class for generating large-format notifications that include a large image attachment.
Joe Malin8d40d042012-11-05 11:36:40 -08003623 *
Robert Ly91c5ce32014-06-08 15:37:00 -07003624 * Here's how you'd set the <code>BigPictureStyle</code> on a notification:
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003625 * <pre class="prettyprint">
Robert Ly91c5ce32014-06-08 15:37:00 -07003626 * Notification notif = new Notification.Builder(mContext)
3627 * .setContentTitle(&quot;New photo from &quot; + sender.toString())
3628 * .setContentText(subject)
3629 * .setSmallIcon(R.drawable.new_post)
3630 * .setLargeIcon(aBitmap)
3631 * .setStyle(new Notification.BigPictureStyle()
3632 * .bigPicture(aBigBitmap))
3633 * .build();
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003634 * </pre>
Joe Malin8d40d042012-11-05 11:36:40 -08003635 *
Daniel Sandler4dfbe832012-04-11 14:51:46 -04003636 * @see Notification#bigContentView
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003637 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003638 public static class BigPictureStyle extends Style {
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003639 private Bitmap mPicture;
Chris Wren3745a3d2012-05-22 15:11:52 -04003640 private Bitmap mBigLargeIcon;
3641 private boolean mBigLargeIconSet = false;
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003642
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003643 public BigPictureStyle() {
3644 }
3645
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003646 public BigPictureStyle(Builder builder) {
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003647 setBuilder(builder);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003648 }
3649
Chris Wrend6297db2012-05-03 16:20:13 -04003650 /**
3651 * Overrides ContentTitle in the big form of the template.
3652 * This defaults to the value passed to setContentTitle().
3653 */
3654 public BigPictureStyle setBigContentTitle(CharSequence title) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04003655 internalSetBigContentTitle(safeCharSequence(title));
Chris Wrend6297db2012-05-03 16:20:13 -04003656 return this;
3657 }
3658
3659 /**
3660 * Set the first line of text after the detail section in the big form of the template.
3661 */
3662 public BigPictureStyle setSummaryText(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04003663 internalSetSummaryText(safeCharSequence(cs));
Chris Wrend6297db2012-05-03 16:20:13 -04003664 return this;
3665 }
3666
Chris Wren0bd664d2012-08-01 13:56:56 -04003667 /**
3668 * Provide the bitmap to be used as the payload for the BigPicture notification.
3669 */
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003670 public BigPictureStyle bigPicture(Bitmap b) {
3671 mPicture = b;
3672 return this;
3673 }
3674
Chris Wren3745a3d2012-05-22 15:11:52 -04003675 /**
Chris Wren3745a3d2012-05-22 15:11:52 -04003676 * Override the large icon when the big notification is shown.
3677 */
3678 public BigPictureStyle bigLargeIcon(Bitmap b) {
3679 mBigLargeIconSet = true;
3680 mBigLargeIcon = b;
3681 return this;
3682 }
3683
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003684 private RemoteViews makeBigContentView() {
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01003685 RemoteViews contentView = getStandardView(mBuilder.getBigPictureLayoutResource());
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003686
3687 contentView.setImageViewBitmap(R.id.big_picture, mPicture);
3688
Jorim Jaggi445d3c02014-08-19 22:33:42 +02003689 applyTopPadding(contentView);
3690
Kenny Guy98193ea2014-07-24 19:54:37 +01003691 boolean twoTextLines = mBuilder.mSubText != null && mBuilder.mContentText != null;
3692 mBuilder.addProfileBadge(contentView,
3693 twoTextLines ? R.id.profile_badge_line2 : R.id.profile_badge_line3);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003694 return contentView;
3695 }
3696
Daniel Sandlerf45564e2013-04-15 15:05:08 -04003697 /**
3698 * @hide
3699 */
3700 public void addExtras(Bundle extras) {
3701 super.addExtras(extras);
3702
3703 if (mBigLargeIconSet) {
3704 extras.putParcelable(EXTRA_LARGE_ICON_BIG, mBigLargeIcon);
3705 }
3706 extras.putParcelable(EXTRA_PICTURE, mPicture);
3707 }
3708
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04003709 /**
3710 * @hide
3711 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003712 @Override
Christoph Studer4600f9b2014-07-22 22:44:43 +02003713 protected void restoreFromExtras(Bundle extras) {
3714 super.restoreFromExtras(extras);
3715
3716 if (extras.containsKey(EXTRA_LARGE_ICON_BIG)) {
3717 mBigLargeIcon = extras.getParcelable(EXTRA_LARGE_ICON_BIG);
Chris Wren3745a3d2012-05-22 15:11:52 -04003718 }
Christoph Studer4600f9b2014-07-22 22:44:43 +02003719 mPicture = extras.getParcelable(EXTRA_PICTURE);
3720 }
3721
3722 /**
3723 * @hide
3724 */
3725 @Override
3726 public void populateBigContentView(Notification wip) {
3727 mBuilder.setBuilderBigContentView(wip, makeBigContentView());
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003728 }
3729 }
3730
3731 /**
Daniel Sandler4dfbe832012-04-11 14:51:46 -04003732 * Helper class for generating large-format notifications that include a lot of text.
Joe Malin8d40d042012-11-05 11:36:40 -08003733 *
Robert Ly91c5ce32014-06-08 15:37:00 -07003734 * Here's how you'd set the <code>BigTextStyle</code> on a notification:
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003735 * <pre class="prettyprint">
Robert Ly91c5ce32014-06-08 15:37:00 -07003736 * Notification notif = new Notification.Builder(mContext)
3737 * .setContentTitle(&quot;New mail from &quot; + sender.toString())
3738 * .setContentText(subject)
3739 * .setSmallIcon(R.drawable.new_mail)
3740 * .setLargeIcon(aBitmap)
3741 * .setStyle(new Notification.BigTextStyle()
3742 * .bigText(aVeryLongString))
3743 * .build();
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003744 * </pre>
Joe Malin8d40d042012-11-05 11:36:40 -08003745 *
Daniel Sandler4dfbe832012-04-11 14:51:46 -04003746 * @see Notification#bigContentView
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003747 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003748 public static class BigTextStyle extends Style {
Jorim Jaggi457a10d2014-09-08 16:18:23 +02003749
3750 private static final int MAX_LINES = 13;
3751 private static final int LINES_CONSUMED_BY_ACTIONS = 3;
3752 private static final int LINES_CONSUMED_BY_SUMMARY = 2;
3753
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003754 private CharSequence mBigText;
3755
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003756 public BigTextStyle() {
3757 }
3758
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003759 public BigTextStyle(Builder builder) {
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003760 setBuilder(builder);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003761 }
3762
Chris Wrend6297db2012-05-03 16:20:13 -04003763 /**
3764 * Overrides ContentTitle in the big form of the template.
3765 * This defaults to the value passed to setContentTitle().
3766 */
3767 public BigTextStyle setBigContentTitle(CharSequence title) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04003768 internalSetBigContentTitle(safeCharSequence(title));
Chris Wrend6297db2012-05-03 16:20:13 -04003769 return this;
3770 }
3771
3772 /**
3773 * Set the first line of text after the detail section in the big form of the template.
3774 */
3775 public BigTextStyle setSummaryText(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04003776 internalSetSummaryText(safeCharSequence(cs));
Chris Wrend6297db2012-05-03 16:20:13 -04003777 return this;
3778 }
3779
Chris Wren0bd664d2012-08-01 13:56:56 -04003780 /**
3781 * Provide the longer text to be displayed in the big form of the
3782 * template in place of the content text.
3783 */
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003784 public BigTextStyle bigText(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04003785 mBigText = safeCharSequence(cs);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003786 return this;
3787 }
3788
Daniel Sandlerf45564e2013-04-15 15:05:08 -04003789 /**
3790 * @hide
3791 */
3792 public void addExtras(Bundle extras) {
3793 super.addExtras(extras);
3794
Christoph Studer4600f9b2014-07-22 22:44:43 +02003795 extras.putCharSequence(EXTRA_BIG_TEXT, mBigText);
3796 }
3797
3798 /**
3799 * @hide
3800 */
3801 @Override
3802 protected void restoreFromExtras(Bundle extras) {
3803 super.restoreFromExtras(extras);
3804
3805 mBigText = extras.getCharSequence(EXTRA_BIG_TEXT);
Daniel Sandlerf45564e2013-04-15 15:05:08 -04003806 }
3807
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003808 private RemoteViews makeBigContentView() {
Christoph Studer4600f9b2014-07-22 22:44:43 +02003809
3810 // Nasty
3811 CharSequence oldBuilderContentText = mBuilder.mContentText;
Daniel Sandler6387d2f2012-05-22 13:44:09 -04003812 mBuilder.mContentText = null;
Daniel Sandler916ad912012-06-13 12:17:07 -04003813
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01003814 RemoteViews contentView = getStandardView(mBuilder.getBigTextLayoutResource());
Joe Malin8d40d042012-11-05 11:36:40 -08003815
Christoph Studer4600f9b2014-07-22 22:44:43 +02003816 mBuilder.mContentText = oldBuilderContentText;
3817
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01003818 contentView.setTextViewText(R.id.big_text, mBuilder.processLegacyText(mBigText));
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003819 contentView.setViewVisibility(R.id.big_text, View.VISIBLE);
Jorim Jaggi457a10d2014-09-08 16:18:23 +02003820 contentView.setInt(R.id.big_text, "setMaxLines", calculateMaxLines());
Chris Wren3c5f92432012-05-04 16:31:17 -04003821 contentView.setViewVisibility(R.id.text2, View.GONE);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003822
Jorim Jaggi445d3c02014-08-19 22:33:42 +02003823 applyTopPadding(contentView);
3824
Jorim Jaggid05aa3e2014-08-28 17:52:27 +02003825 mBuilder.shrinkLine3Text(contentView);
3826
Kenny Guy98193ea2014-07-24 19:54:37 +01003827 mBuilder.addProfileBadge(contentView, R.id.profile_badge_large_template);
3828
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003829 return contentView;
3830 }
3831
Jorim Jaggi457a10d2014-09-08 16:18:23 +02003832 private int calculateMaxLines() {
3833 int lineCount = MAX_LINES;
3834 boolean hasActions = mBuilder.mActions.size() > 0;
3835 boolean hasSummary = (mSummaryTextSet ? mSummaryText : mBuilder.mSubText) != null;
3836 if (hasActions) {
3837 lineCount -= LINES_CONSUMED_BY_ACTIONS;
3838 }
3839 if (hasSummary) {
3840 lineCount -= LINES_CONSUMED_BY_SUMMARY;
3841 }
3842
3843 // If we have less top padding at the top, we can fit less lines.
3844 if (!mBuilder.mHasThreeLines) {
3845 lineCount--;
3846 }
3847 return lineCount;
3848 }
3849
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04003850 /**
3851 * @hide
3852 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003853 @Override
Christoph Studer4600f9b2014-07-22 22:44:43 +02003854 public void populateBigContentView(Notification wip) {
3855 mBuilder.setBuilderBigContentView(wip, makeBigContentView());
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003856 }
3857 }
Daniel Sandler879c5e02012-04-17 16:46:51 -04003858
3859 /**
3860 * Helper class for generating large-format notifications that include a list of (up to 5) strings.
Joe Malin8d40d042012-11-05 11:36:40 -08003861 *
Robert Ly91c5ce32014-06-08 15:37:00 -07003862 * Here's how you'd set the <code>InboxStyle</code> on a notification:
Daniel Sandler879c5e02012-04-17 16:46:51 -04003863 * <pre class="prettyprint">
Robert Ly91c5ce32014-06-08 15:37:00 -07003864 * Notification notif = new Notification.Builder(mContext)
3865 * .setContentTitle(&quot;5 New mails from &quot; + sender.toString())
3866 * .setContentText(subject)
3867 * .setSmallIcon(R.drawable.new_mail)
3868 * .setLargeIcon(aBitmap)
3869 * .setStyle(new Notification.InboxStyle()
3870 * .addLine(str1)
3871 * .addLine(str2)
3872 * .setContentTitle(&quot;&quot;)
3873 * .setSummaryText(&quot;+3 more&quot;))
3874 * .build();
Daniel Sandler879c5e02012-04-17 16:46:51 -04003875 * </pre>
Joe Malin8d40d042012-11-05 11:36:40 -08003876 *
Daniel Sandler879c5e02012-04-17 16:46:51 -04003877 * @see Notification#bigContentView
3878 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003879 public static class InboxStyle extends Style {
Daniel Sandler879c5e02012-04-17 16:46:51 -04003880 private ArrayList<CharSequence> mTexts = new ArrayList<CharSequence>(5);
3881
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003882 public InboxStyle() {
3883 }
3884
Daniel Sandler879c5e02012-04-17 16:46:51 -04003885 public InboxStyle(Builder builder) {
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003886 setBuilder(builder);
Daniel Sandler879c5e02012-04-17 16:46:51 -04003887 }
3888
Chris Wrend6297db2012-05-03 16:20:13 -04003889 /**
3890 * Overrides ContentTitle in the big form of the template.
3891 * This defaults to the value passed to setContentTitle().
3892 */
3893 public InboxStyle setBigContentTitle(CharSequence title) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04003894 internalSetBigContentTitle(safeCharSequence(title));
Chris Wrend6297db2012-05-03 16:20:13 -04003895 return this;
3896 }
3897
3898 /**
3899 * Set the first line of text after the detail section in the big form of the template.
3900 */
3901 public InboxStyle setSummaryText(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04003902 internalSetSummaryText(safeCharSequence(cs));
Chris Wrend6297db2012-05-03 16:20:13 -04003903 return this;
3904 }
3905
Chris Wren0bd664d2012-08-01 13:56:56 -04003906 /**
3907 * Append a line to the digest section of the Inbox notification.
3908 */
Daniel Sandler879c5e02012-04-17 16:46:51 -04003909 public InboxStyle addLine(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04003910 mTexts.add(safeCharSequence(cs));
Daniel Sandler879c5e02012-04-17 16:46:51 -04003911 return this;
3912 }
3913
Daniel Sandlerf45564e2013-04-15 15:05:08 -04003914 /**
3915 * @hide
3916 */
3917 public void addExtras(Bundle extras) {
3918 super.addExtras(extras);
Christoph Studer4600f9b2014-07-22 22:44:43 +02003919
Daniel Sandlerf45564e2013-04-15 15:05:08 -04003920 CharSequence[] a = new CharSequence[mTexts.size()];
3921 extras.putCharSequenceArray(EXTRA_TEXT_LINES, mTexts.toArray(a));
3922 }
3923
Christoph Studer4600f9b2014-07-22 22:44:43 +02003924 /**
3925 * @hide
3926 */
3927 @Override
3928 protected void restoreFromExtras(Bundle extras) {
3929 super.restoreFromExtras(extras);
3930
3931 mTexts.clear();
3932 if (extras.containsKey(EXTRA_TEXT_LINES)) {
3933 Collections.addAll(mTexts, extras.getCharSequenceArray(EXTRA_TEXT_LINES));
3934 }
3935 }
3936
Daniel Sandler879c5e02012-04-17 16:46:51 -04003937 private RemoteViews makeBigContentView() {
Daniel Sandler619738c2012-06-07 16:33:08 -04003938 // Remove the content text so line3 disappears unless you have a summary
Christoph Studer4600f9b2014-07-22 22:44:43 +02003939
3940 // Nasty
3941 CharSequence oldBuilderContentText = mBuilder.mContentText;
Daniel Sandler619738c2012-06-07 16:33:08 -04003942 mBuilder.mContentText = null;
Christoph Studer4600f9b2014-07-22 22:44:43 +02003943
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01003944 RemoteViews contentView = getStandardView(mBuilder.getInboxLayoutResource());
Daniel Sandler619738c2012-06-07 16:33:08 -04003945
Christoph Studer4600f9b2014-07-22 22:44:43 +02003946 mBuilder.mContentText = oldBuilderContentText;
3947
Chris Wrend6297db2012-05-03 16:20:13 -04003948 contentView.setViewVisibility(R.id.text2, View.GONE);
Daniel Sandler879c5e02012-04-17 16:46:51 -04003949
Chris Wrend6297db2012-05-03 16:20:13 -04003950 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 -04003951 R.id.inbox_text4, R.id.inbox_text5, R.id.inbox_text6};
Chris Wrend6297db2012-05-03 16:20:13 -04003952
Chris Wren4ed80d52012-05-17 09:30:03 -04003953 // Make sure all rows are gone in case we reuse a view.
3954 for (int rowId : rowIds) {
3955 contentView.setViewVisibility(rowId, View.GONE);
3956 }
3957
Jorim Jaggi445d3c02014-08-19 22:33:42 +02003958 final boolean largeText =
3959 mBuilder.mContext.getResources().getConfiguration().fontScale > 1f;
3960 final float subTextSize = mBuilder.mContext.getResources().getDimensionPixelSize(
3961 R.dimen.notification_subtext_size);
Daniel Sandler879c5e02012-04-17 16:46:51 -04003962 int i=0;
3963 while (i < mTexts.size() && i < rowIds.length) {
3964 CharSequence str = mTexts.get(i);
3965 if (str != null && !str.equals("")) {
3966 contentView.setViewVisibility(rowIds[i], View.VISIBLE);
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01003967 contentView.setTextViewText(rowIds[i], mBuilder.processLegacyText(str));
Jorim Jaggi445d3c02014-08-19 22:33:42 +02003968 if (largeText) {
3969 contentView.setTextViewTextSize(rowIds[i], TypedValue.COMPLEX_UNIT_PX,
3970 subTextSize);
3971 }
Daniel Sandler879c5e02012-04-17 16:46:51 -04003972 }
3973 i++;
3974 }
3975
Chris Wren683ab002012-09-20 10:35:54 -04003976 contentView.setViewVisibility(R.id.inbox_end_pad,
3977 mTexts.size() > 0 ? View.VISIBLE : View.GONE);
3978
3979 contentView.setViewVisibility(R.id.inbox_more,
3980 mTexts.size() > rowIds.length ? View.VISIBLE : View.GONE);
Chris Wren29bb6d92012-05-17 18:09:42 -04003981
Jorim Jaggi445d3c02014-08-19 22:33:42 +02003982 applyTopPadding(contentView);
3983
Jorim Jaggid05aa3e2014-08-28 17:52:27 +02003984 mBuilder.shrinkLine3Text(contentView);
3985
Kenny Guy98193ea2014-07-24 19:54:37 +01003986 mBuilder.addProfileBadge(contentView, R.id.profile_badge_large_template);
3987
Daniel Sandler879c5e02012-04-17 16:46:51 -04003988 return contentView;
3989 }
3990
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04003991 /**
3992 * @hide
3993 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003994 @Override
Christoph Studer4600f9b2014-07-22 22:44:43 +02003995 public void populateBigContentView(Notification wip) {
3996 mBuilder.setBuilderBigContentView(wip, makeBigContentView());
Daniel Sandler879c5e02012-04-17 16:46:51 -04003997 }
3998 }
Dan Sandler842dd772014-05-15 09:36:47 -04003999
4000 /**
4001 * Notification style for media playback notifications.
4002 *
4003 * In the expanded form, {@link Notification#bigContentView}, up to 5
4004 * {@link Notification.Action}s specified with
4005 * {@link Notification.Builder#addAction(int, CharSequence, PendingIntent) addAction} will be
4006 * shown as icon-only pushbuttons, suitable for transport controls. The Bitmap given to
4007 * {@link Notification.Builder#setLargeIcon(android.graphics.Bitmap) setLargeIcon()} will be
4008 * treated as album artwork.
4009 *
4010 * Unlike the other styles provided here, MediaStyle can also modify the standard-size
4011 * {@link Notification#contentView}; by providing action indices to
4012 * {@link #setShowActionsInCompactView(int...)} you can promote up to 2 actions to be displayed
4013 * in the standard view alongside the usual content.
4014 *
Bryan Mawhinney6be8de32014-07-18 10:35:12 +01004015 * Notifications created with MediaStyle will have their category set to
4016 * {@link Notification#CATEGORY_TRANSPORT CATEGORY_TRANSPORT} unless you set a different
4017 * category using {@link Notification.Builder#setCategory(String) setCategory()}.
4018 *
Jeff Browndba34ba2014-06-24 20:46:03 -07004019 * Finally, if you attach a {@link android.media.session.MediaSession.Token} using
4020 * {@link android.app.Notification.MediaStyle#setMediaSession(MediaSession.Token)},
Dan Sandler842dd772014-05-15 09:36:47 -04004021 * the System UI can identify this as a notification representing an active media session
4022 * and respond accordingly (by showing album artwork in the lockscreen, for example).
4023 *
4024 * To use this style with your Notification, feed it to
4025 * {@link Notification.Builder#setStyle(android.app.Notification.Style)} like so:
4026 * <pre class="prettyprint">
4027 * Notification noti = new Notification.Builder()
4028 * .setSmallIcon(R.drawable.ic_stat_player)
4029 * .setContentTitle(&quot;Track title&quot;) // these three lines are optional
4030 * .setContentText(&quot;Artist - Album&quot;) // if you use
Bryan Mawhinney6be8de32014-07-18 10:35:12 +01004031 * .setLargeIcon(albumArtBitmap)) // setMediaSession(token)
4032 * .setStyle(<b>new Notification.MediaStyle()</b>
4033 * .setMediaSession(mySession))
Dan Sandler842dd772014-05-15 09:36:47 -04004034 * .build();
4035 * </pre>
4036 *
4037 * @see Notification#bigContentView
4038 */
4039 public static class MediaStyle extends Style {
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02004040 static final int MAX_MEDIA_BUTTONS_IN_COMPACT = 3;
Dan Sandler842dd772014-05-15 09:36:47 -04004041 static final int MAX_MEDIA_BUTTONS = 5;
4042
4043 private int[] mActionsToShowInCompact = null;
Jeff Browndba34ba2014-06-24 20:46:03 -07004044 private MediaSession.Token mToken;
Dan Sandler842dd772014-05-15 09:36:47 -04004045
4046 public MediaStyle() {
4047 }
4048
4049 public MediaStyle(Builder builder) {
4050 setBuilder(builder);
4051 }
4052
4053 /**
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02004054 * 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 -04004055 * notification view.
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02004056 *
4057 * @param actions the indices of the actions to show in the compact notification view
Dan Sandler842dd772014-05-15 09:36:47 -04004058 */
4059 public MediaStyle setShowActionsInCompactView(int...actions) {
4060 mActionsToShowInCompact = actions;
4061 return this;
4062 }
4063
4064 /**
Jeff Browndba34ba2014-06-24 20:46:03 -07004065 * Attach a {@link android.media.session.MediaSession.Token} to this Notification
4066 * to provide additional playback information and control to the SystemUI.
Dan Sandler842dd772014-05-15 09:36:47 -04004067 */
Jeff Browndba34ba2014-06-24 20:46:03 -07004068 public MediaStyle setMediaSession(MediaSession.Token token) {
Dan Sandler842dd772014-05-15 09:36:47 -04004069 mToken = token;
4070 return this;
4071 }
4072
Christoph Studer4600f9b2014-07-22 22:44:43 +02004073 /**
4074 * @hide
4075 */
Dan Sandler842dd772014-05-15 09:36:47 -04004076 @Override
4077 public Notification buildStyled(Notification wip) {
Christoph Studer4600f9b2014-07-22 22:44:43 +02004078 super.buildStyled(wip);
Bryan Mawhinney6be8de32014-07-18 10:35:12 +01004079 if (wip.category == null) {
4080 wip.category = Notification.CATEGORY_TRANSPORT;
4081 }
Dan Sandler842dd772014-05-15 09:36:47 -04004082 return wip;
4083 }
4084
Christoph Studer4600f9b2014-07-22 22:44:43 +02004085 /**
4086 * @hide
4087 */
4088 @Override
4089 public void populateContentView(Notification wip) {
4090 mBuilder.setBuilderContentView(wip, makeMediaContentView());
4091 }
4092
4093 /**
4094 * @hide
4095 */
4096 @Override
4097 public void populateBigContentView(Notification wip) {
4098 mBuilder.setBuilderBigContentView(wip, makeMediaBigContentView());
4099 }
4100
Dan Sandler842dd772014-05-15 09:36:47 -04004101 /** @hide */
4102 @Override
4103 public void addExtras(Bundle extras) {
4104 super.addExtras(extras);
4105
4106 if (mToken != null) {
4107 extras.putParcelable(EXTRA_MEDIA_SESSION, mToken);
4108 }
Bryan Mawhinneye191f902014-07-22 12:50:09 +01004109 if (mActionsToShowInCompact != null) {
4110 extras.putIntArray(EXTRA_COMPACT_ACTIONS, mActionsToShowInCompact);
4111 }
Dan Sandler842dd772014-05-15 09:36:47 -04004112 }
4113
Christoph Studer4600f9b2014-07-22 22:44:43 +02004114 /**
4115 * @hide
4116 */
4117 @Override
4118 protected void restoreFromExtras(Bundle extras) {
4119 super.restoreFromExtras(extras);
4120
4121 if (extras.containsKey(EXTRA_MEDIA_SESSION)) {
4122 mToken = extras.getParcelable(EXTRA_MEDIA_SESSION);
4123 }
4124 if (extras.containsKey(EXTRA_COMPACT_ACTIONS)) {
4125 mActionsToShowInCompact = extras.getIntArray(EXTRA_COMPACT_ACTIONS);
4126 }
4127 }
4128
Dan Sandler842dd772014-05-15 09:36:47 -04004129 private RemoteViews generateMediaActionButton(Action action) {
4130 final boolean tombstone = (action.actionIntent == null);
4131 RemoteViews button = new RemoteViews(mBuilder.mContext.getPackageName(),
Alan Viverette3cb07a462014-06-06 14:19:53 -07004132 R.layout.notification_material_media_action);
Dan Sandler842dd772014-05-15 09:36:47 -04004133 button.setImageViewResource(R.id.action0, action.icon);
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02004134 button.setDrawableParameters(R.id.action0, false, -1,
4135 0xFFFFFFFF,
4136 PorterDuff.Mode.SRC_ATOP, -1);
Dan Sandler842dd772014-05-15 09:36:47 -04004137 if (!tombstone) {
4138 button.setOnClickPendingIntent(R.id.action0, action.actionIntent);
4139 }
4140 button.setContentDescription(R.id.action0, action.title);
4141 return button;
4142 }
4143
4144 private RemoteViews makeMediaContentView() {
4145 RemoteViews view = mBuilder.applyStandardTemplate(
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02004146 R.layout.notification_template_material_media, false /* hasProgress */);
Dan Sandler842dd772014-05-15 09:36:47 -04004147
4148 final int numActions = mBuilder.mActions.size();
4149 final int N = mActionsToShowInCompact == null
4150 ? 0
4151 : Math.min(mActionsToShowInCompact.length, MAX_MEDIA_BUTTONS_IN_COMPACT);
4152 if (N > 0) {
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02004153 view.removeAllViews(com.android.internal.R.id.media_actions);
Dan Sandler842dd772014-05-15 09:36:47 -04004154 for (int i = 0; i < N; i++) {
4155 if (i >= numActions) {
4156 throw new IllegalArgumentException(String.format(
4157 "setShowActionsInCompactView: action %d out of bounds (max %d)",
4158 i, numActions - 1));
4159 }
4160
4161 final Action action = mBuilder.mActions.get(mActionsToShowInCompact[i]);
4162 final RemoteViews button = generateMediaActionButton(action);
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02004163 view.addView(com.android.internal.R.id.media_actions, button);
Dan Sandler842dd772014-05-15 09:36:47 -04004164 }
4165 }
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02004166 styleText(view);
4167 hideRightIcon(view);
Dan Sandler842dd772014-05-15 09:36:47 -04004168 return view;
4169 }
4170
4171 private RemoteViews makeMediaBigContentView() {
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02004172 final int actionCount = Math.min(mBuilder.mActions.size(), MAX_MEDIA_BUTTONS);
4173 RemoteViews big = mBuilder.applyStandardTemplate(getBigLayoutResource(actionCount),
4174 false /* hasProgress */);
Dan Sandler842dd772014-05-15 09:36:47 -04004175
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02004176 if (actionCount > 0) {
4177 big.removeAllViews(com.android.internal.R.id.media_actions);
4178 for (int i = 0; i < actionCount; i++) {
Dan Sandler842dd772014-05-15 09:36:47 -04004179 final RemoteViews button = generateMediaActionButton(mBuilder.mActions.get(i));
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02004180 big.addView(com.android.internal.R.id.media_actions, button);
Dan Sandler842dd772014-05-15 09:36:47 -04004181 }
4182 }
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02004183 styleText(big);
4184 hideRightIcon(big);
4185 applyTopPadding(big);
4186 big.setViewVisibility(android.R.id.progress, View.GONE);
Dan Sandler842dd772014-05-15 09:36:47 -04004187 return big;
4188 }
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02004189
4190 private int getBigLayoutResource(int actionCount) {
4191 if (actionCount <= 3) {
4192 return R.layout.notification_template_material_big_media_narrow;
4193 } else {
4194 return R.layout.notification_template_material_big_media;
4195 }
4196 }
4197
4198 private void hideRightIcon(RemoteViews contentView) {
4199 contentView.setViewVisibility(R.id.right_icon, View.GONE);
4200 }
4201
4202 /**
4203 * Applies the special text colors for media notifications to all text views.
4204 */
4205 private void styleText(RemoteViews contentView) {
4206 int primaryColor = mBuilder.mContext.getResources().getColor(
4207 R.color.notification_media_primary_color);
4208 int secondaryColor = mBuilder.mContext.getResources().getColor(
4209 R.color.notification_media_secondary_color);
4210 contentView.setTextColor(R.id.title, primaryColor);
4211 if (mBuilder.showsTimeOrChronometer()) {
4212 if (mBuilder.mUseChronometer) {
4213 contentView.setTextColor(R.id.chronometer, secondaryColor);
4214 } else {
4215 contentView.setTextColor(R.id.time, secondaryColor);
4216 }
4217 }
4218 contentView.setTextColor(R.id.text2, secondaryColor);
4219 contentView.setTextColor(R.id.text, secondaryColor);
4220 contentView.setTextColor(R.id.info, secondaryColor);
4221 }
4222
4223 /**
4224 * @hide
4225 */
4226 @Override
4227 protected boolean hasProgress() {
4228 return false;
4229 }
Dan Sandler842dd772014-05-15 09:36:47 -04004230 }
Griff Hazen61a9e862014-05-22 16:05:19 -07004231
Christoph Studer4600f9b2014-07-22 22:44:43 +02004232 // When adding a new Style subclass here, don't forget to update
4233 // Builder.getNotificationStyleClass.
4234
Griff Hazen61a9e862014-05-22 16:05:19 -07004235 /**
4236 * Extender interface for use with {@link Builder#extend}. Extenders may be used to add
4237 * metadata or change options on a notification builder.
4238 */
4239 public interface Extender {
4240 /**
4241 * Apply this extender to a notification builder.
4242 * @param builder the builder to be modified.
4243 * @return the build object for chaining.
4244 */
4245 public Builder extend(Builder builder);
4246 }
4247
4248 /**
4249 * Helper class to add wearable extensions to notifications.
4250 * <p class="note"> See
4251 * <a href="{@docRoot}wear/notifications/creating.html">Creating Notifications
4252 * for Android Wear</a> for more information on how to use this class.
4253 * <p>
4254 * To create a notification with wearable extensions:
4255 * <ol>
4256 * <li>Create a {@link android.app.Notification.Builder}, setting any desired
4257 * properties.
4258 * <li>Create a {@link android.app.Notification.WearableExtender}.
4259 * <li>Set wearable-specific properties using the
4260 * {@code add} and {@code set} methods of {@link android.app.Notification.WearableExtender}.
4261 * <li>Call {@link android.app.Notification.Builder#extend} to apply the extensions to a
4262 * notification.
4263 * <li>Post the notification to the notification system with the
4264 * {@code NotificationManager.notify(...)} methods.
4265 * </ol>
4266 *
4267 * <pre class="prettyprint">
4268 * Notification notif = new Notification.Builder(mContext)
4269 * .setContentTitle(&quot;New mail from &quot; + sender.toString())
4270 * .setContentText(subject)
4271 * .setSmallIcon(R.drawable.new_mail)
4272 * .extend(new Notification.WearableExtender()
4273 * .setContentIcon(R.drawable.new_mail))
4274 * .build();
4275 * NotificationManager notificationManger =
4276 * (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
4277 * notificationManger.notify(0, notif);</pre>
4278 *
4279 * <p>Wearable extensions can be accessed on an existing notification by using the
4280 * {@code WearableExtender(Notification)} constructor,
4281 * and then using the {@code get} methods to access values.
4282 *
4283 * <pre class="prettyprint">
4284 * Notification.WearableExtender wearableExtender = new Notification.WearableExtender(
4285 * notification);
Griff Hazen14f57992014-05-26 09:07:14 -07004286 * List&lt;Notification&gt; pages = wearableExtender.getPages();</pre>
Griff Hazen61a9e862014-05-22 16:05:19 -07004287 */
4288 public static final class WearableExtender implements Extender {
4289 /**
4290 * Sentinel value for an action index that is unset.
4291 */
4292 public static final int UNSET_ACTION_INDEX = -1;
4293
4294 /**
4295 * Size value for use with {@link #setCustomSizePreset} to show this notification with
4296 * default sizing.
4297 * <p>For custom display notifications created using {@link #setDisplayIntent},
4298 * the default is {@link #SIZE_LARGE}. All other notifications size automatically based
4299 * on their content.
4300 */
4301 public static final int SIZE_DEFAULT = 0;
4302
4303 /**
4304 * Size value for use with {@link #setCustomSizePreset} to show this notification
4305 * with an extra small size.
4306 * <p>This value is only applicable for custom display notifications created using
4307 * {@link #setDisplayIntent}.
4308 */
4309 public static final int SIZE_XSMALL = 1;
4310
4311 /**
4312 * Size value for use with {@link #setCustomSizePreset} to show this notification
4313 * with a small size.
4314 * <p>This value is only applicable for custom display notifications created using
4315 * {@link #setDisplayIntent}.
4316 */
4317 public static final int SIZE_SMALL = 2;
4318
4319 /**
4320 * Size value for use with {@link #setCustomSizePreset} to show this notification
4321 * with a medium size.
4322 * <p>This value is only applicable for custom display notifications created using
4323 * {@link #setDisplayIntent}.
4324 */
4325 public static final int SIZE_MEDIUM = 3;
4326
4327 /**
4328 * Size value for use with {@link #setCustomSizePreset} to show this notification
4329 * with a large size.
4330 * <p>This value is only applicable for custom display notifications created using
4331 * {@link #setDisplayIntent}.
4332 */
4333 public static final int SIZE_LARGE = 4;
4334
Griff Hazend5f11f92014-05-27 15:40:09 -07004335 /**
4336 * Size value for use with {@link #setCustomSizePreset} to show this notification
4337 * full screen.
4338 * <p>This value is only applicable for custom display notifications created using
4339 * {@link #setDisplayIntent}.
4340 */
4341 public static final int SIZE_FULL_SCREEN = 5;
4342
Griff Hazen5f2edfc2014-09-29 16:28:44 -07004343 /**
4344 * Sentinel value for use with {@link #setHintScreenTimeout} to keep the screen on for a
4345 * short amount of time when this notification is displayed on the screen. This
4346 * is the default value.
4347 */
4348 public static final int SCREEN_TIMEOUT_SHORT = 0;
4349
4350 /**
4351 * Sentinel value for use with {@link #setHintScreenTimeout} to keep the screen on
4352 * for a longer amount of time when this notification is displayed on the screen.
4353 */
4354 public static final int SCREEN_TIMEOUT_LONG = -1;
4355
Griff Hazen61a9e862014-05-22 16:05:19 -07004356 /** Notification extra which contains wearable extensions */
4357 private static final String EXTRA_WEARABLE_EXTENSIONS = "android.wearable.EXTENSIONS";
4358
4359 // Keys within EXTRA_WEARABLE_OPTIONS for wearable options.
4360 private static final String KEY_ACTIONS = "actions";
4361 private static final String KEY_FLAGS = "flags";
4362 private static final String KEY_DISPLAY_INTENT = "displayIntent";
4363 private static final String KEY_PAGES = "pages";
4364 private static final String KEY_BACKGROUND = "background";
4365 private static final String KEY_CONTENT_ICON = "contentIcon";
4366 private static final String KEY_CONTENT_ICON_GRAVITY = "contentIconGravity";
4367 private static final String KEY_CONTENT_ACTION_INDEX = "contentActionIndex";
4368 private static final String KEY_CUSTOM_SIZE_PRESET = "customSizePreset";
4369 private static final String KEY_CUSTOM_CONTENT_HEIGHT = "customContentHeight";
4370 private static final String KEY_GRAVITY = "gravity";
Griff Hazen5f2edfc2014-09-29 16:28:44 -07004371 private static final String KEY_HINT_SCREEN_TIMEOUT = "hintScreenTimeout";
Griff Hazen61a9e862014-05-22 16:05:19 -07004372
4373 // Flags bitwise-ored to mFlags
4374 private static final int FLAG_CONTENT_INTENT_AVAILABLE_OFFLINE = 0x1;
4375 private static final int FLAG_HINT_HIDE_ICON = 1 << 1;
4376 private static final int FLAG_HINT_SHOW_BACKGROUND_ONLY = 1 << 2;
4377 private static final int FLAG_START_SCROLL_BOTTOM = 1 << 3;
Griff Hazen5f2edfc2014-09-29 16:28:44 -07004378 private static final int FLAG_HINT_AVOID_BACKGROUND_CLIPPING = 1 << 4;
Griff Hazen61a9e862014-05-22 16:05:19 -07004379
4380 // Default value for flags integer
4381 private static final int DEFAULT_FLAGS = FLAG_CONTENT_INTENT_AVAILABLE_OFFLINE;
4382
4383 private static final int DEFAULT_CONTENT_ICON_GRAVITY = Gravity.END;
4384 private static final int DEFAULT_GRAVITY = Gravity.BOTTOM;
4385
4386 private ArrayList<Action> mActions = new ArrayList<Action>();
4387 private int mFlags = DEFAULT_FLAGS;
4388 private PendingIntent mDisplayIntent;
4389 private ArrayList<Notification> mPages = new ArrayList<Notification>();
4390 private Bitmap mBackground;
4391 private int mContentIcon;
4392 private int mContentIconGravity = DEFAULT_CONTENT_ICON_GRAVITY;
4393 private int mContentActionIndex = UNSET_ACTION_INDEX;
4394 private int mCustomSizePreset = SIZE_DEFAULT;
4395 private int mCustomContentHeight;
4396 private int mGravity = DEFAULT_GRAVITY;
Griff Hazen5f2edfc2014-09-29 16:28:44 -07004397 private int mHintScreenTimeout;
Griff Hazen61a9e862014-05-22 16:05:19 -07004398
4399 /**
4400 * Create a {@link android.app.Notification.WearableExtender} with default
4401 * options.
4402 */
4403 public WearableExtender() {
4404 }
4405
4406 public WearableExtender(Notification notif) {
4407 Bundle wearableBundle = notif.extras.getBundle(EXTRA_WEARABLE_EXTENSIONS);
4408 if (wearableBundle != null) {
4409 List<Action> actions = wearableBundle.getParcelableArrayList(KEY_ACTIONS);
4410 if (actions != null) {
4411 mActions.addAll(actions);
4412 }
4413
4414 mFlags = wearableBundle.getInt(KEY_FLAGS, DEFAULT_FLAGS);
4415 mDisplayIntent = wearableBundle.getParcelable(KEY_DISPLAY_INTENT);
4416
4417 Notification[] pages = getNotificationArrayFromBundle(
4418 wearableBundle, KEY_PAGES);
4419 if (pages != null) {
4420 Collections.addAll(mPages, pages);
4421 }
4422
4423 mBackground = wearableBundle.getParcelable(KEY_BACKGROUND);
4424 mContentIcon = wearableBundle.getInt(KEY_CONTENT_ICON);
4425 mContentIconGravity = wearableBundle.getInt(KEY_CONTENT_ICON_GRAVITY,
4426 DEFAULT_CONTENT_ICON_GRAVITY);
4427 mContentActionIndex = wearableBundle.getInt(KEY_CONTENT_ACTION_INDEX,
4428 UNSET_ACTION_INDEX);
4429 mCustomSizePreset = wearableBundle.getInt(KEY_CUSTOM_SIZE_PRESET,
4430 SIZE_DEFAULT);
4431 mCustomContentHeight = wearableBundle.getInt(KEY_CUSTOM_CONTENT_HEIGHT);
4432 mGravity = wearableBundle.getInt(KEY_GRAVITY, DEFAULT_GRAVITY);
Griff Hazen5f2edfc2014-09-29 16:28:44 -07004433 mHintScreenTimeout = wearableBundle.getInt(KEY_HINT_SCREEN_TIMEOUT);
Griff Hazen61a9e862014-05-22 16:05:19 -07004434 }
4435 }
4436
4437 /**
4438 * Apply wearable extensions to a notification that is being built. This is typically
4439 * called by the {@link android.app.Notification.Builder#extend} method of
4440 * {@link android.app.Notification.Builder}.
4441 */
4442 @Override
4443 public Notification.Builder extend(Notification.Builder builder) {
4444 Bundle wearableBundle = new Bundle();
4445
4446 if (!mActions.isEmpty()) {
4447 wearableBundle.putParcelableArrayList(KEY_ACTIONS, mActions);
4448 }
4449 if (mFlags != DEFAULT_FLAGS) {
4450 wearableBundle.putInt(KEY_FLAGS, mFlags);
4451 }
4452 if (mDisplayIntent != null) {
4453 wearableBundle.putParcelable(KEY_DISPLAY_INTENT, mDisplayIntent);
4454 }
4455 if (!mPages.isEmpty()) {
4456 wearableBundle.putParcelableArray(KEY_PAGES, mPages.toArray(
4457 new Notification[mPages.size()]));
4458 }
4459 if (mBackground != null) {
4460 wearableBundle.putParcelable(KEY_BACKGROUND, mBackground);
4461 }
4462 if (mContentIcon != 0) {
4463 wearableBundle.putInt(KEY_CONTENT_ICON, mContentIcon);
4464 }
4465 if (mContentIconGravity != DEFAULT_CONTENT_ICON_GRAVITY) {
4466 wearableBundle.putInt(KEY_CONTENT_ICON_GRAVITY, mContentIconGravity);
4467 }
4468 if (mContentActionIndex != UNSET_ACTION_INDEX) {
4469 wearableBundle.putInt(KEY_CONTENT_ACTION_INDEX,
4470 mContentActionIndex);
4471 }
4472 if (mCustomSizePreset != SIZE_DEFAULT) {
4473 wearableBundle.putInt(KEY_CUSTOM_SIZE_PRESET, mCustomSizePreset);
4474 }
4475 if (mCustomContentHeight != 0) {
4476 wearableBundle.putInt(KEY_CUSTOM_CONTENT_HEIGHT, mCustomContentHeight);
4477 }
4478 if (mGravity != DEFAULT_GRAVITY) {
4479 wearableBundle.putInt(KEY_GRAVITY, mGravity);
4480 }
Griff Hazen5f2edfc2014-09-29 16:28:44 -07004481 if (mHintScreenTimeout != 0) {
4482 wearableBundle.putInt(KEY_HINT_SCREEN_TIMEOUT, mHintScreenTimeout);
4483 }
Griff Hazen61a9e862014-05-22 16:05:19 -07004484
4485 builder.getExtras().putBundle(EXTRA_WEARABLE_EXTENSIONS, wearableBundle);
4486 return builder;
4487 }
4488
4489 @Override
4490 public WearableExtender clone() {
4491 WearableExtender that = new WearableExtender();
4492 that.mActions = new ArrayList<Action>(this.mActions);
4493 that.mFlags = this.mFlags;
4494 that.mDisplayIntent = this.mDisplayIntent;
4495 that.mPages = new ArrayList<Notification>(this.mPages);
4496 that.mBackground = this.mBackground;
4497 that.mContentIcon = this.mContentIcon;
4498 that.mContentIconGravity = this.mContentIconGravity;
4499 that.mContentActionIndex = this.mContentActionIndex;
4500 that.mCustomSizePreset = this.mCustomSizePreset;
4501 that.mCustomContentHeight = this.mCustomContentHeight;
4502 that.mGravity = this.mGravity;
Griff Hazen5f2edfc2014-09-29 16:28:44 -07004503 that.mHintScreenTimeout = this.mHintScreenTimeout;
Griff Hazen61a9e862014-05-22 16:05:19 -07004504 return that;
4505 }
4506
4507 /**
4508 * Add a wearable action to this notification.
4509 *
4510 * <p>When wearable actions are added using this method, the set of actions that
4511 * show on a wearable device splits from devices that only show actions added
4512 * using {@link android.app.Notification.Builder#addAction}. This allows for customization
4513 * of which actions display on different devices.
4514 *
4515 * @param action the action to add to this notification
4516 * @return this object for method chaining
4517 * @see android.app.Notification.Action
4518 */
4519 public WearableExtender addAction(Action action) {
4520 mActions.add(action);
4521 return this;
4522 }
4523
4524 /**
4525 * Adds wearable actions to this notification.
4526 *
4527 * <p>When wearable actions are added using this method, the set of actions that
4528 * show on a wearable device splits from devices that only show actions added
4529 * using {@link android.app.Notification.Builder#addAction}. This allows for customization
4530 * of which actions display on different devices.
4531 *
4532 * @param actions the actions to add to this notification
4533 * @return this object for method chaining
4534 * @see android.app.Notification.Action
4535 */
4536 public WearableExtender addActions(List<Action> actions) {
4537 mActions.addAll(actions);
4538 return this;
4539 }
4540
4541 /**
4542 * Clear all wearable actions present on this builder.
4543 * @return this object for method chaining.
4544 * @see #addAction
4545 */
4546 public WearableExtender clearActions() {
4547 mActions.clear();
4548 return this;
4549 }
4550
4551 /**
4552 * Get the wearable actions present on this notification.
4553 */
4554 public List<Action> getActions() {
4555 return mActions;
4556 }
4557
4558 /**
4559 * Set an intent to launch inside of an activity view when displaying
Griff Hazen14f57992014-05-26 09:07:14 -07004560 * this notification. The {@link PendingIntent} provided should be for an activity.
4561 *
4562 * <pre class="prettyprint">
4563 * Intent displayIntent = new Intent(context, MyDisplayActivity.class);
4564 * PendingIntent displayPendingIntent = PendingIntent.getActivity(context,
4565 * 0, displayIntent, PendingIntent.FLAG_UPDATE_CURRENT);
4566 * Notification notif = new Notification.Builder(context)
4567 * .extend(new Notification.WearableExtender()
4568 * .setDisplayIntent(displayPendingIntent)
4569 * .setCustomSizePreset(Notification.WearableExtender.SIZE_MEDIUM))
4570 * .build();</pre>
4571 *
4572 * <p>The activity to launch needs to allow embedding, must be exported, and
Griff Hazen831ca9d2014-06-17 00:38:38 -07004573 * should have an empty task affinity. It is also recommended to use the device
4574 * default light theme.
Griff Hazen14f57992014-05-26 09:07:14 -07004575 *
4576 * <p>Example AndroidManifest.xml entry:
4577 * <pre class="prettyprint">
4578 * &lt;activity android:name=&quot;com.example.MyDisplayActivity&quot;
4579 * android:exported=&quot;true&quot;
4580 * android:allowEmbedded=&quot;true&quot;
Griff Hazen831ca9d2014-06-17 00:38:38 -07004581 * android:taskAffinity=&quot;&quot;
4582 * android:theme=&quot;@android:style/Theme.DeviceDefault.Light&quot; /&gt;</pre>
Griff Hazen61a9e862014-05-22 16:05:19 -07004583 *
4584 * @param intent the {@link PendingIntent} for an activity
4585 * @return this object for method chaining
4586 * @see android.app.Notification.WearableExtender#getDisplayIntent
4587 */
4588 public WearableExtender setDisplayIntent(PendingIntent intent) {
4589 mDisplayIntent = intent;
4590 return this;
4591 }
4592
4593 /**
4594 * Get the intent to launch inside of an activity view when displaying this
4595 * notification. This {@code PendingIntent} should be for an activity.
4596 */
4597 public PendingIntent getDisplayIntent() {
4598 return mDisplayIntent;
4599 }
4600
4601 /**
4602 * Add an additional page of content to display with this notification. The current
4603 * notification forms the first page, and pages added using this function form
4604 * subsequent pages. This field can be used to separate a notification into multiple
4605 * sections.
4606 *
4607 * @param page the notification to add as another page
4608 * @return this object for method chaining
4609 * @see android.app.Notification.WearableExtender#getPages
4610 */
4611 public WearableExtender addPage(Notification page) {
4612 mPages.add(page);
4613 return this;
4614 }
4615
4616 /**
4617 * Add additional pages of content to display with this notification. The current
4618 * notification forms the first page, and pages added using this function form
4619 * subsequent pages. This field can be used to separate a notification into multiple
4620 * sections.
4621 *
4622 * @param pages a list of notifications
4623 * @return this object for method chaining
4624 * @see android.app.Notification.WearableExtender#getPages
4625 */
4626 public WearableExtender addPages(List<Notification> pages) {
4627 mPages.addAll(pages);
4628 return this;
4629 }
4630
4631 /**
4632 * Clear all additional pages present on this builder.
4633 * @return this object for method chaining.
4634 * @see #addPage
4635 */
4636 public WearableExtender clearPages() {
4637 mPages.clear();
4638 return this;
4639 }
4640
4641 /**
4642 * Get the array of additional pages of content for displaying this notification. The
4643 * current notification forms the first page, and elements within this array form
4644 * subsequent pages. This field can be used to separate a notification into multiple
4645 * sections.
4646 * @return the pages for this notification
4647 */
4648 public List<Notification> getPages() {
4649 return mPages;
4650 }
4651
4652 /**
4653 * Set a background image to be displayed behind the notification content.
4654 * Contrary to the {@link android.app.Notification.BigPictureStyle}, this background
4655 * will work with any notification style.
4656 *
4657 * @param background the background bitmap
4658 * @return this object for method chaining
4659 * @see android.app.Notification.WearableExtender#getBackground
4660 */
4661 public WearableExtender setBackground(Bitmap background) {
4662 mBackground = background;
4663 return this;
4664 }
4665
4666 /**
4667 * Get a background image to be displayed behind the notification content.
4668 * Contrary to the {@link android.app.Notification.BigPictureStyle}, this background
4669 * will work with any notification style.
4670 *
4671 * @return the background image
4672 * @see android.app.Notification.WearableExtender#setBackground
4673 */
4674 public Bitmap getBackground() {
4675 return mBackground;
4676 }
4677
4678 /**
4679 * Set an icon that goes with the content of this notification.
4680 */
4681 public WearableExtender setContentIcon(int icon) {
4682 mContentIcon = icon;
4683 return this;
4684 }
4685
4686 /**
4687 * Get an icon that goes with the content of this notification.
4688 */
4689 public int getContentIcon() {
4690 return mContentIcon;
4691 }
4692
4693 /**
4694 * Set the gravity that the content icon should have within the notification display.
4695 * Supported values include {@link android.view.Gravity#START} and
4696 * {@link android.view.Gravity#END}. The default value is {@link android.view.Gravity#END}.
4697 * @see #setContentIcon
4698 */
4699 public WearableExtender setContentIconGravity(int contentIconGravity) {
4700 mContentIconGravity = contentIconGravity;
4701 return this;
4702 }
4703
4704 /**
4705 * Get the gravity that the content icon should have within the notification display.
4706 * Supported values include {@link android.view.Gravity#START} and
4707 * {@link android.view.Gravity#END}. The default value is {@link android.view.Gravity#END}.
4708 * @see #getContentIcon
4709 */
4710 public int getContentIconGravity() {
4711 return mContentIconGravity;
4712 }
4713
4714 /**
4715 * Set an action from this notification's actions to be clickable with the content of
Griff Hazen14f57992014-05-26 09:07:14 -07004716 * this notification. This action will no longer display separately from the
4717 * notification's content.
4718 *
Griff Hazenca48d352014-05-28 22:37:13 -07004719 * <p>For notifications with multiple pages, child pages can also have content actions
Griff Hazen14f57992014-05-26 09:07:14 -07004720 * set, although the list of available actions comes from the main notification and not
4721 * from the child page's notification.
4722 *
4723 * @param actionIndex The index of the action to hoist onto the current notification page.
4724 * If wearable actions were added to the main notification, this index
4725 * will apply to that list, otherwise it will apply to the regular
4726 * actions list.
Griff Hazen61a9e862014-05-22 16:05:19 -07004727 */
4728 public WearableExtender setContentAction(int actionIndex) {
4729 mContentActionIndex = actionIndex;
4730 return this;
4731 }
4732
4733 /**
Griff Hazenca48d352014-05-28 22:37:13 -07004734 * Get the index of the notification action, if any, that was specified as being clickable
4735 * with the content of this notification. This action will no longer display separately
Griff Hazen14f57992014-05-26 09:07:14 -07004736 * from the notification's content.
Griff Hazen61a9e862014-05-22 16:05:19 -07004737 *
Griff Hazenca48d352014-05-28 22:37:13 -07004738 * <p>For notifications with multiple pages, child pages can also have content actions
Griff Hazen14f57992014-05-26 09:07:14 -07004739 * set, although the list of available actions comes from the main notification and not
4740 * from the child page's notification.
4741 *
4742 * <p>If wearable specific actions were added to the main notification, this index will
4743 * apply to that list, otherwise it will apply to the regular actions list.
Griff Hazenca48d352014-05-28 22:37:13 -07004744 *
4745 * @return the action index or {@link #UNSET_ACTION_INDEX} if no action was selected.
Griff Hazen61a9e862014-05-22 16:05:19 -07004746 */
4747 public int getContentAction() {
4748 return mContentActionIndex;
4749 }
4750
4751 /**
4752 * Set the gravity that this notification should have within the available viewport space.
4753 * Supported values include {@link android.view.Gravity#TOP},
4754 * {@link android.view.Gravity#CENTER_VERTICAL} and {@link android.view.Gravity#BOTTOM}.
4755 * The default value is {@link android.view.Gravity#BOTTOM}.
4756 */
4757 public WearableExtender setGravity(int gravity) {
4758 mGravity = gravity;
4759 return this;
4760 }
4761
4762 /**
4763 * Get the gravity that this notification should have within the available viewport space.
4764 * Supported values include {@link android.view.Gravity#TOP},
4765 * {@link android.view.Gravity#CENTER_VERTICAL} and {@link android.view.Gravity#BOTTOM}.
4766 * The default value is {@link android.view.Gravity#BOTTOM}.
4767 */
4768 public int getGravity() {
4769 return mGravity;
4770 }
4771
4772 /**
4773 * Set the custom size preset for the display of this notification out of the available
4774 * presets found in {@link android.app.Notification.WearableExtender}, e.g.
4775 * {@link #SIZE_LARGE}.
4776 * <p>Some custom size presets are only applicable for custom display notifications created
4777 * using {@link android.app.Notification.WearableExtender#setDisplayIntent}. Check the
4778 * documentation for the preset in question. See also
4779 * {@link #setCustomContentHeight} and {@link #getCustomSizePreset}.
4780 */
4781 public WearableExtender setCustomSizePreset(int sizePreset) {
4782 mCustomSizePreset = sizePreset;
4783 return this;
4784 }
4785
4786 /**
4787 * Get the custom size preset for the display of this notification out of the available
4788 * presets found in {@link android.app.Notification.WearableExtender}, e.g.
4789 * {@link #SIZE_LARGE}.
4790 * <p>Some custom size presets are only applicable for custom display notifications created
4791 * using {@link #setDisplayIntent}. Check the documentation for the preset in question.
4792 * See also {@link #setCustomContentHeight} and {@link #setCustomSizePreset}.
4793 */
4794 public int getCustomSizePreset() {
4795 return mCustomSizePreset;
4796 }
4797
4798 /**
4799 * Set the custom height in pixels for the display of this notification's content.
4800 * <p>This option is only available for custom display notifications created
4801 * using {@link android.app.Notification.WearableExtender#setDisplayIntent}. See also
4802 * {@link android.app.Notification.WearableExtender#setCustomSizePreset} and
4803 * {@link #getCustomContentHeight}.
4804 */
4805 public WearableExtender setCustomContentHeight(int height) {
4806 mCustomContentHeight = height;
4807 return this;
4808 }
4809
4810 /**
4811 * Get the custom height in pixels for the display of this notification's content.
4812 * <p>This option is only available for custom display notifications created
4813 * using {@link #setDisplayIntent}. See also {@link #setCustomSizePreset} and
4814 * {@link #setCustomContentHeight}.
4815 */
4816 public int getCustomContentHeight() {
4817 return mCustomContentHeight;
4818 }
4819
4820 /**
4821 * Set whether the scrolling position for the contents of this notification should start
4822 * at the bottom of the contents instead of the top when the contents are too long to
4823 * display within the screen. Default is false (start scroll at the top).
4824 */
4825 public WearableExtender setStartScrollBottom(boolean startScrollBottom) {
4826 setFlag(FLAG_START_SCROLL_BOTTOM, startScrollBottom);
4827 return this;
4828 }
4829
4830 /**
4831 * Get whether the scrolling position for the contents of this notification should start
4832 * at the bottom of the contents instead of the top when the contents are too long to
4833 * display within the screen. Default is false (start scroll at the top).
4834 */
4835 public boolean getStartScrollBottom() {
4836 return (mFlags & FLAG_START_SCROLL_BOTTOM) != 0;
4837 }
4838
4839 /**
4840 * Set whether the content intent is available when the wearable device is not connected
4841 * to a companion device. The user can still trigger this intent when the wearable device
4842 * is offline, but a visual hint will indicate that the content intent may not be available.
4843 * Defaults to true.
4844 */
4845 public WearableExtender setContentIntentAvailableOffline(
4846 boolean contentIntentAvailableOffline) {
4847 setFlag(FLAG_CONTENT_INTENT_AVAILABLE_OFFLINE, contentIntentAvailableOffline);
4848 return this;
4849 }
4850
4851 /**
4852 * Get whether the content intent is available when the wearable device is not connected
4853 * to a companion device. The user can still trigger this intent when the wearable device
4854 * is offline, but a visual hint will indicate that the content intent may not be available.
4855 * Defaults to true.
4856 */
4857 public boolean getContentIntentAvailableOffline() {
4858 return (mFlags & FLAG_CONTENT_INTENT_AVAILABLE_OFFLINE) != 0;
4859 }
4860
4861 /**
4862 * Set a hint that this notification's icon should not be displayed.
4863 * @param hintHideIcon {@code true} to hide the icon, {@code false} otherwise.
4864 * @return this object for method chaining
4865 */
4866 public WearableExtender setHintHideIcon(boolean hintHideIcon) {
4867 setFlag(FLAG_HINT_HIDE_ICON, hintHideIcon);
4868 return this;
4869 }
4870
4871 /**
4872 * Get a hint that this notification's icon should not be displayed.
4873 * @return {@code true} if this icon should not be displayed, false otherwise.
4874 * The default value is {@code false} if this was never set.
4875 */
4876 public boolean getHintHideIcon() {
4877 return (mFlags & FLAG_HINT_HIDE_ICON) != 0;
4878 }
4879
4880 /**
4881 * Set a visual hint that only the background image of this notification should be
4882 * displayed, and other semantic content should be hidden. This hint is only applicable
4883 * to sub-pages added using {@link #addPage}.
4884 */
4885 public WearableExtender setHintShowBackgroundOnly(boolean hintShowBackgroundOnly) {
4886 setFlag(FLAG_HINT_SHOW_BACKGROUND_ONLY, hintShowBackgroundOnly);
4887 return this;
4888 }
4889
4890 /**
4891 * Get a visual hint that only the background image of this notification should be
4892 * displayed, and other semantic content should be hidden. This hint is only applicable
4893 * to sub-pages added using {@link android.app.Notification.WearableExtender#addPage}.
4894 */
4895 public boolean getHintShowBackgroundOnly() {
4896 return (mFlags & FLAG_HINT_SHOW_BACKGROUND_ONLY) != 0;
4897 }
4898
Griff Hazen5f2edfc2014-09-29 16:28:44 -07004899 /**
4900 * Set a hint that this notification's background should not be clipped if possible.
4901 * @param hintAvoidBackgroundClipping {@code true} to avoid clipping if possible.
4902 * @return this object for method chaining
4903 */
4904 public WearableExtender setHintAvoidBackgroundClipping(
4905 boolean hintAvoidBackgroundClipping) {
4906 setFlag(FLAG_HINT_AVOID_BACKGROUND_CLIPPING, hintAvoidBackgroundClipping);
4907 return this;
4908 }
4909
4910 /**
4911 * Get a hint that this notification's background should not be clipped if possible.
4912 * @return {@code true} if it's ok if the background is clipped on the screen, false
4913 * otherwise. The default value is {@code false} if this was never set.
4914 */
4915 public boolean getHintAvoidBackgroundClipping() {
4916 return (mFlags & FLAG_HINT_AVOID_BACKGROUND_CLIPPING) != 0;
4917 }
4918
4919 /**
4920 * Set a hint that the screen should remain on for at least this duration when
4921 * this notification is displayed on the screen.
4922 * @param timeout The requested screen timeout in milliseconds. Can also be either
4923 * {@link #SCREEN_TIMEOUT_SHORT} or {@link #SCREEN_TIMEOUT_LONG}.
4924 * @return this object for method chaining
4925 */
4926 public WearableExtender setHintScreenTimeout(int timeout) {
4927 mHintScreenTimeout = timeout;
4928 return this;
4929 }
4930
4931 /**
4932 * Get the duration, in milliseconds, that the screen should remain on for
4933 * when this notification is displayed.
4934 * @return the duration in milliseconds if > 0, or either one of the sentinel values
4935 * {@link #SCREEN_TIMEOUT_SHORT} or {@link #SCREEN_TIMEOUT_LONG}.
4936 */
4937 public int getHintScreenTimeout() {
4938 return mHintScreenTimeout;
4939 }
4940
Griff Hazen61a9e862014-05-22 16:05:19 -07004941 private void setFlag(int mask, boolean value) {
4942 if (value) {
4943 mFlags |= mask;
4944 } else {
4945 mFlags &= ~mask;
4946 }
4947 }
4948 }
4949
4950 /**
4951 * Get an array of Notification objects from a parcelable array bundle field.
4952 * Update the bundle to have a typed array so fetches in the future don't need
4953 * to do an array copy.
4954 */
4955 private static Notification[] getNotificationArrayFromBundle(Bundle bundle, String key) {
4956 Parcelable[] array = bundle.getParcelableArray(key);
4957 if (array instanceof Notification[] || array == null) {
4958 return (Notification[]) array;
4959 }
4960 Notification[] typedArray = Arrays.copyOf(array, array.length,
4961 Notification[].class);
4962 bundle.putParcelableArray(key, typedArray);
4963 return typedArray;
4964 }
Christoph Studer4600f9b2014-07-22 22:44:43 +02004965
4966 private static class BuilderRemoteViews extends RemoteViews {
4967 public BuilderRemoteViews(Parcel parcel) {
4968 super(parcel);
4969 }
4970
Kenny Guy77320062014-08-27 21:37:15 +01004971 public BuilderRemoteViews(ApplicationInfo appInfo, int layoutId) {
4972 super(appInfo, layoutId);
Christoph Studer4600f9b2014-07-22 22:44:43 +02004973 }
4974
4975 @Override
4976 public BuilderRemoteViews clone() {
4977 Parcel p = Parcel.obtain();
4978 writeToParcel(p, 0);
4979 p.setDataPosition(0);
4980 BuilderRemoteViews brv = new BuilderRemoteViews(p);
4981 p.recycle();
4982 return brv;
4983 }
4984 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004985}