blob: 9c00e1cff60788c09872a4abd11614e8ba472b61 [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 Norbye7b9c9122013-05-30 16:48:33 -070019import android.annotation.DrawableRes;
Tor Norbyed9273d62013-05-30 15:59:53 -070020import android.annotation.IntDef;
Daniel Sandler01df1c62014-06-09 10:54:01 -040021import android.annotation.SdkConstant;
22import android.annotation.SdkConstant.SdkConstantType;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080023import android.content.Context;
24import android.content.Intent;
Kenny Guy77320062014-08-27 21:37:15 +010025import android.content.pm.ApplicationInfo;
Christoph Studer4600f9b2014-07-22 22:44:43 +020026import android.content.pm.PackageManager.NameNotFoundException;
Jorim Jaggief72a192014-08-26 21:57:46 +020027import android.content.res.ColorStateList;
Joe Onoratoef1e7762010-09-17 18:38:38 -040028import android.graphics.Bitmap;
Kenny Guy8a0101b2014-05-08 23:34:12 +010029import android.graphics.Canvas;
Jorim Jaggi5c2d8462014-03-21 17:37:00 +010030import android.graphics.PorterDuff;
Kenny Guy8a0101b2014-05-08 23:34:12 +010031import android.graphics.drawable.Drawable;
John Spurlockc0650f022014-07-19 13:22:39 -040032import android.media.AudioAttributes;
Jeff Sharkey098d5802012-04-26 17:30:34 -070033import android.media.AudioManager;
Jeff Browndba34ba2014-06-24 20:46:03 -070034import android.media.session.MediaSession;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080035import android.net.Uri;
Daniel Sandlerdcbaf662013-04-26 16:23:09 -040036import android.os.BadParcelableException;
Christoph Studer239f8352014-08-25 15:13:18 +020037import android.os.Build;
Daniel Sandler2561b0b2012-02-13 21:04:12 -050038import android.os.Bundle;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080039import android.os.Parcel;
40import android.os.Parcelable;
Daniel Sandlera2985ed2012-04-03 16:42:00 -040041import android.os.SystemClock;
Jeff Sharkey6d515712012-09-20 16:06:08 -070042import android.os.UserHandle;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080043import android.text.TextUtils;
Daniel Sandlerdcbaf662013-04-26 16:23:09 -040044import android.util.Log;
Jorim Jaggi445d3c02014-08-19 22:33:42 +020045import android.util.MathUtils;
Daniel Sandler9f7936a2012-05-21 16:14:28 -040046import android.util.TypedValue;
Griff Hazen61a9e862014-05-22 16:05:19 -070047import android.view.Gravity;
Joe Onorato8595a3d2010-11-19 18:12:07 -080048import android.view.View;
Jeff Sharkey1c400132011-08-05 14:50:13 -070049import android.widget.ProgressBar;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080050import android.widget.RemoteViews;
51
Griff Hazen959591e2014-05-15 22:26:18 -070052import com.android.internal.R;
Griff Hazenc091ba82014-05-16 10:13:26 -070053import com.android.internal.util.NotificationColorUtil;
Griff Hazen959591e2014-05-15 22:26:18 -070054
Tor Norbyed9273d62013-05-30 15:59:53 -070055import java.lang.annotation.Retention;
56import java.lang.annotation.RetentionPolicy;
Christoph Studer4600f9b2014-07-22 22:44:43 +020057import java.lang.reflect.Constructor;
Andy Stadler110988c2010-12-03 14:29:16 -080058import java.text.NumberFormat;
Daniel Sandler2561b0b2012-02-13 21:04:12 -050059import java.util.ArrayList;
Griff Hazen61a9e862014-05-22 16:05:19 -070060import java.util.Arrays;
Griff Hazen5cadc3b2014-05-20 09:55:39 -070061import java.util.Collections;
Griff Hazen61a9e862014-05-22 16:05:19 -070062import java.util.List;
Joe Onorato561d3852010-11-20 18:09:34 -080063
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080064/**
65 * A class that represents how a persistent notification is to be presented to
66 * the user using the {@link android.app.NotificationManager}.
67 *
Joe Onoratocb109a02011-01-18 17:57:41 -080068 * <p>The {@link Notification.Builder Notification.Builder} has been added to make it
69 * easier to construct Notifications.</p>
70 *
Joe Fernandez558459f2011-10-13 16:47:36 -070071 * <div class="special reference">
72 * <h3>Developer Guides</h3>
73 * <p>For a guide to creating notifications, read the
74 * <a href="{@docRoot}guide/topics/ui/notifiers/notifications.html">Status Bar Notifications</a>
75 * developer guide.</p>
76 * </div>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080077 */
78public class Notification implements Parcelable
79{
Daniel Sandlerdcbaf662013-04-26 16:23:09 -040080 private static final String TAG = "Notification";
81
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080082 /**
Daniel Sandler01df1c62014-06-09 10:54:01 -040083 * An activity that provides a user interface for adjusting notification preferences for its
84 * containing application. Optional but recommended for apps that post
85 * {@link android.app.Notification Notifications}.
86 */
87 @SdkConstant(SdkConstantType.INTENT_CATEGORY)
88 public static final String INTENT_CATEGORY_NOTIFICATION_PREFERENCES
89 = "android.intent.category.NOTIFICATION_PREFERENCES";
90
91 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080092 * Use all default values (where applicable).
93 */
94 public static final int DEFAULT_ALL = ~0;
Daniel Sandler2561b0b2012-02-13 21:04:12 -050095
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080096 /**
97 * Use the default notification sound. This will ignore any given
98 * {@link #sound}.
Daniel Sandler2561b0b2012-02-13 21:04:12 -050099 *
Chris Wren47c20a12014-06-18 17:27:29 -0400100 * <p>
101 * A notification that is noisy is more likely to be presented as a heads-up notification.
102 * </p>
103 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800104 * @see #defaults
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500105 */
106
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800107 public static final int DEFAULT_SOUND = 1;
108
109 /**
110 * Use the default notification vibrate. This will ignore any given
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500111 * {@link #vibrate}. Using phone vibration requires the
Scott Mainb8b36452009-04-26 15:50:49 -0700112 * {@link android.Manifest.permission#VIBRATE VIBRATE} permission.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500113 *
Chris Wren47c20a12014-06-18 17:27:29 -0400114 * <p>
115 * A notification that vibrates is more likely to be presented as a heads-up notification.
116 * </p>
117 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800118 * @see #defaults
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500119 */
120
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800121 public static final int DEFAULT_VIBRATE = 2;
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500122
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800123 /**
124 * Use the default notification lights. This will ignore the
125 * {@link #FLAG_SHOW_LIGHTS} bit, and {@link #ledARGB}, {@link #ledOffMS}, or
126 * {@link #ledOnMS}.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500127 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800128 * @see #defaults
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500129 */
130
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800131 public static final int DEFAULT_LIGHTS = 4;
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500132
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800133 /**
Christoph Studer535ec612014-09-03 15:47:47 +0200134 * Maximum length of CharSequences accepted by Builder and friends.
135 *
136 * <p>
137 * Avoids spamming the system with overly large strings such as full e-mails.
138 */
139 private static final int MAX_CHARSEQUENCE_LENGTH = 5 * 1024;
140
141 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500142 * A timestamp related to this notification, in milliseconds since the epoch.
Joe Malin8d40d042012-11-05 11:36:40 -0800143 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500144 * Default value: {@link System#currentTimeMillis() Now}.
145 *
146 * Choose a timestamp that will be most relevant to the user. For most finite events, this
147 * corresponds to the time the event happened (or will happen, in the case of events that have
148 * yet to occur but about which the user is being informed). Indefinite events should be
Joe Malin8d40d042012-11-05 11:36:40 -0800149 * timestamped according to when the activity began.
150 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500151 * Some examples:
Joe Malin8d40d042012-11-05 11:36:40 -0800152 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500153 * <ul>
154 * <li>Notification of a new chat message should be stamped when the message was received.</li>
155 * <li>Notification of an ongoing file download (with a progress bar, for example) should be stamped when the download started.</li>
156 * <li>Notification of a completed file download should be stamped when the download finished.</li>
157 * <li>Notification of an upcoming meeting should be stamped with the time the meeting will begin (that is, in the future).</li>
158 * <li>Notification of an ongoing stopwatch (increasing timer) should be stamped with the watch's start time.
159 * <li>Notification of an ongoing countdown timer should be stamped with the timer's end time.
Joe Malin8d40d042012-11-05 11:36:40 -0800160 * </ul>
161 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800162 */
163 public long when;
164
165 /**
166 * The resource id of a drawable to use as the icon in the status bar.
Daniel Sandlerd952dae2011-02-07 16:33:36 -0500167 * This is required; notifications with an invalid icon resource will not be shown.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800168 */
Tor Norbye7b9c9122013-05-30 16:48:33 -0700169 @DrawableRes
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800170 public int icon;
171
172 /**
Joe Onorato46439ce2010-11-19 13:56:21 -0800173 * If the icon in the status bar is to have more than one level, you can set this. Otherwise,
174 * leave it at its default value of 0.
175 *
176 * @see android.widget.ImageView#setImageLevel
Griff Hazen959591e2014-05-15 22:26:18 -0700177 * @see android.graphics.drawable.Drawable#setLevel
Joe Onorato46439ce2010-11-19 13:56:21 -0800178 */
179 public int iconLevel;
180
181 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500182 * The number of events that this notification represents. For example, in a new mail
183 * notification, this could be the number of unread messages.
Joe Malin8d40d042012-11-05 11:36:40 -0800184 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500185 * The system may or may not use this field to modify the appearance of the notification. For
186 * example, before {@link android.os.Build.VERSION_CODES#HONEYCOMB}, this number was
187 * superimposed over the icon in the status bar. Starting with
188 * {@link android.os.Build.VERSION_CODES#HONEYCOMB}, the template used by
189 * {@link Notification.Builder} has displayed the number in the expanded notification view.
Joe Malin8d40d042012-11-05 11:36:40 -0800190 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500191 * If the number is 0 or negative, it is never shown.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800192 */
193 public int number;
194
195 /**
196 * The intent to execute when the expanded status entry is clicked. If
197 * this is an activity, it must include the
198 * {@link android.content.Intent#FLAG_ACTIVITY_NEW_TASK} flag, which requires
Scott Main7aee61f2011-02-08 11:25:01 -0800199 * that you take care of task management as described in the
200 * <a href="{@docRoot}guide/topics/fundamentals/tasks-and-back-stack.html">Tasks and Back
Dianne Hackborn6ceca582012-01-10 15:24:26 -0800201 * Stack</a> document. In particular, make sure to read the notification section
202 * <a href="{@docRoot}guide/topics/ui/notifiers/notifications.html#HandlingNotifications">Handling
203 * Notifications</a> for the correct ways to launch an application from a
204 * notification.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800205 */
206 public PendingIntent contentIntent;
207
208 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500209 * The intent to execute when the notification is explicitly dismissed by the user, either with
210 * the "Clear All" button or by swiping it away individually.
211 *
212 * This probably shouldn't be launching an activity since several of those will be sent
213 * at the same time.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800214 */
215 public PendingIntent deleteIntent;
216
217 /**
Dianne Hackborn170bae72010-09-03 15:14:28 -0700218 * An intent to launch instead of posting the notification to the status bar.
Joe Onoratocb109a02011-01-18 17:57:41 -0800219 *
Chris Wren47c20a12014-06-18 17:27:29 -0400220 * <p>
221 * The system UI may choose to display a heads-up notification, instead of
222 * launching this intent, while the user is using the device.
223 * </p>
224 *
Joe Onoratocb109a02011-01-18 17:57:41 -0800225 * @see Notification.Builder#setFullScreenIntent
Daniel Sandlere46cbd32010-06-17 10:35:26 -0400226 */
227 public PendingIntent fullScreenIntent;
228
229 /**
Dan Sandler5fcdf6e2014-07-18 11:31:15 -0400230 * Text that summarizes this notification for accessibility services.
231 *
232 * As of the L release, this text is no longer shown on screen, but it is still useful to
233 * accessibility services (where it serves as an audible announcement of the notification's
234 * appearance).
Joe Onoratoef1e7762010-09-17 18:38:38 -0400235 *
Joe Onorato46439ce2010-11-19 13:56:21 -0800236 * @see #tickerView
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800237 */
238 public CharSequence tickerText;
239
240 /**
Dan Sandler5fcdf6e2014-07-18 11:31:15 -0400241 * Formerly, a view showing the {@link #tickerText}.
242 *
243 * No longer displayed in the status bar as of API 21.
Joe Onoratoef1e7762010-09-17 18:38:38 -0400244 */
Dan Sandler5fcdf6e2014-07-18 11:31:15 -0400245 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -0800246 public RemoteViews tickerView;
Joe Onoratoef1e7762010-09-17 18:38:38 -0400247
248 /**
Daniel Sandlere46cbd32010-06-17 10:35:26 -0400249 * The view that will represent this notification in the expanded status bar.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800250 */
251 public RemoteViews contentView;
252
Daniel Sandlera0a938c2012-03-15 08:42:37 -0400253 /**
Daniel Sandler4dfbe832012-04-11 14:51:46 -0400254 * A large-format version of {@link #contentView}, giving the Notification an
Daniel Sandlerf3b73432012-03-27 15:01:25 -0400255 * opportunity to show more detail. The system UI may choose to show this
256 * instead of the normal content view at its discretion.
Daniel Sandlerf3b73432012-03-27 15:01:25 -0400257 */
258 public RemoteViews bigContentView;
259
Chris Wren8fd39ec2014-02-27 17:43:26 -0500260
261 /**
Chris Wren47c20a12014-06-18 17:27:29 -0400262 * A medium-format version of {@link #contentView}, providing the Notification an
263 * opportunity to add action buttons to contentView. At its discretion, the system UI may
264 * choose to show this as a heads-up notification, which will pop up so the user can see
265 * it without leaving their current activity.
Chris Wren8fd39ec2014-02-27 17:43:26 -0500266 */
267 public RemoteViews headsUpContentView;
268
Daniel Sandlerf3b73432012-03-27 15:01:25 -0400269 /**
Joe Onorato46439ce2010-11-19 13:56:21 -0800270 * The bitmap that may escape the bounds of the panel and bar.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800271 */
Joe Onorato46439ce2010-11-19 13:56:21 -0800272 public Bitmap largeIcon;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800273
274 /**
275 * The sound to play.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500276 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800277 * <p>
Chris Wren47c20a12014-06-18 17:27:29 -0400278 * A notification that is noisy is more likely to be presented as a heads-up notification.
279 * </p>
280 *
281 * <p>
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500282 * To play the default notification sound, see {@link #defaults}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800283 * </p>
284 */
285 public Uri sound;
286
287 /**
288 * Use this constant as the value for audioStreamType to request that
289 * the default stream type for notifications be used. Currently the
Jeff Sharkey098d5802012-04-26 17:30:34 -0700290 * default stream type is {@link AudioManager#STREAM_NOTIFICATION}.
John Spurlockc0650f022014-07-19 13:22:39 -0400291 *
292 * @deprecated Use {@link #audioAttributes} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800293 */
Jean-Michel Trivi81f871e2014-08-06 16:32:38 -0700294 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800295 public static final int STREAM_DEFAULT = -1;
296
297 /**
298 * The audio stream type to use when playing the sound.
299 * Should be one of the STREAM_ constants from
300 * {@link android.media.AudioManager}.
John Spurlockc0650f022014-07-19 13:22:39 -0400301 *
302 * @deprecated Use {@link #audioAttributes} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800303 */
Jean-Michel Trivi81f871e2014-08-06 16:32:38 -0700304 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800305 public int audioStreamType = STREAM_DEFAULT;
306
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800307 /**
John Spurlockc0650f022014-07-19 13:22:39 -0400308 * The default value of {@link #audioAttributes}.
309 */
310 public static final AudioAttributes AUDIO_ATTRIBUTES_DEFAULT = new AudioAttributes.Builder()
311 .setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
312 .setUsage(AudioAttributes.USAGE_NOTIFICATION)
313 .build();
314
315 /**
316 * The {@link AudioAttributes audio attributes} to use when playing the sound.
317 */
318 public AudioAttributes audioAttributes = AUDIO_ATTRIBUTES_DEFAULT;
319
320 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500321 * The pattern with which to vibrate.
322 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800323 * <p>
324 * To vibrate the default pattern, see {@link #defaults}.
325 * </p>
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500326 *
Chris Wren47c20a12014-06-18 17:27:29 -0400327 * <p>
328 * A notification that vibrates is more likely to be presented as a heads-up notification.
329 * </p>
330 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800331 * @see android.os.Vibrator#vibrate(long[],int)
332 */
333 public long[] vibrate;
334
335 /**
336 * The color of the led. The hardware will do its best approximation.
337 *
338 * @see #FLAG_SHOW_LIGHTS
339 * @see #flags
340 */
341 public int ledARGB;
342
343 /**
344 * The number of milliseconds for the LED to be on while it's flashing.
345 * The hardware will do its best approximation.
346 *
347 * @see #FLAG_SHOW_LIGHTS
348 * @see #flags
349 */
350 public int ledOnMS;
351
352 /**
353 * The number of milliseconds for the LED to be off while it's flashing.
354 * The hardware will do its best approximation.
355 *
356 * @see #FLAG_SHOW_LIGHTS
357 * @see #flags
358 */
359 public int ledOffMS;
360
361 /**
362 * Specifies which values should be taken from the defaults.
363 * <p>
364 * To set, OR the desired from {@link #DEFAULT_SOUND},
365 * {@link #DEFAULT_VIBRATE}, {@link #DEFAULT_LIGHTS}. For all default
366 * values, use {@link #DEFAULT_ALL}.
367 * </p>
368 */
369 public int defaults;
370
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800371 /**
372 * Bit to be bitwise-ored into the {@link #flags} field that should be
373 * set if you want the LED on for this notification.
374 * <ul>
375 * <li>To turn the LED off, pass 0 in the alpha channel for colorARGB
376 * or 0 for both ledOnMS and ledOffMS.</li>
377 * <li>To turn the LED on, pass 1 for ledOnMS and 0 for ledOffMS.</li>
378 * <li>To flash the LED, pass the number of milliseconds that it should
379 * be on and off to ledOnMS and ledOffMS.</li>
380 * </ul>
381 * <p>
382 * Since hardware varies, you are not guaranteed that any of the values
383 * you pass are honored exactly. Use the system defaults (TODO) if possible
384 * because they will be set to values that work on any given hardware.
385 * <p>
386 * The alpha channel must be set for forward compatibility.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500387 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800388 */
389 public static final int FLAG_SHOW_LIGHTS = 0x00000001;
390
391 /**
392 * Bit to be bitwise-ored into the {@link #flags} field that should be
393 * set if this notification is in reference to something that is ongoing,
394 * like a phone call. It should not be set if this notification is in
395 * reference to something that happened at a particular point in time,
396 * like a missed phone call.
397 */
398 public static final int FLAG_ONGOING_EVENT = 0x00000002;
399
400 /**
401 * Bit to be bitwise-ored into the {@link #flags} field that if set,
Scott Mainb8b36452009-04-26 15:50:49 -0700402 * the audio will be repeated until the notification is
403 * cancelled or the notification window is opened.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800404 */
405 public static final int FLAG_INSISTENT = 0x00000004;
406
407 /**
408 * Bit to be bitwise-ored into the {@link #flags} field that should be
Griff Hazen293977b2014-04-28 08:37:20 -0700409 * set if you would only like the sound, vibrate and ticker to be played
410 * if the notification was not already showing.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800411 */
412 public static final int FLAG_ONLY_ALERT_ONCE = 0x00000008;
413
414 /**
415 * Bit to be bitwise-ored into the {@link #flags} field that should be
416 * set if the notification should be canceled when it is clicked by the
Daniel Sandler8aa9ae62012-12-04 23:31:47 -0500417 * user.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500418
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800419 */
420 public static final int FLAG_AUTO_CANCEL = 0x00000010;
421
422 /**
423 * Bit to be bitwise-ored into the {@link #flags} field that should be
424 * set if the notification should not be canceled when the user clicks
425 * the Clear all button.
426 */
427 public static final int FLAG_NO_CLEAR = 0x00000020;
428
Dianne Hackbornd8a43f62009-08-17 23:33:56 -0700429 /**
430 * Bit to be bitwise-ored into the {@link #flags} field that should be
431 * set if this notification represents a currently running service. This
432 * will normally be set for you by {@link Service#startForeground}.
433 */
434 public static final int FLAG_FOREGROUND_SERVICE = 0x00000040;
435
Daniel Sandlere46cbd32010-06-17 10:35:26 -0400436 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500437 * Obsolete flag indicating high-priority notifications; use the priority field instead.
Joe Malin8d40d042012-11-05 11:36:40 -0800438 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500439 * @deprecated Use {@link #priority} with a positive value.
Daniel Sandlere46cbd32010-06-17 10:35:26 -0400440 */
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500441 public static final int FLAG_HIGH_PRIORITY = 0x00000080;
Daniel Sandlere46cbd32010-06-17 10:35:26 -0400442
Griff Hazendfcb0802014-02-11 12:00:00 -0800443 /**
444 * Bit to be bitswise-ored into the {@link #flags} field that should be
445 * set if this notification is relevant to the current device only
446 * and it is not recommended that it bridge to other devices.
447 */
448 public static final int FLAG_LOCAL_ONLY = 0x00000100;
449
Griff Hazen5cadc3b2014-05-20 09:55:39 -0700450 /**
451 * Bit to be bitswise-ored into the {@link #flags} field that should be
452 * set if this notification is the group summary for a group of notifications.
453 * Grouped notifications may display in a cluster or stack on devices which
454 * support such rendering. Requires a group key also be set using {@link Builder#setGroup}.
455 */
456 public static final int FLAG_GROUP_SUMMARY = 0x00000200;
457
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800458 public int flags;
459
Tor Norbyed9273d62013-05-30 15:59:53 -0700460 /** @hide */
461 @IntDef({PRIORITY_DEFAULT,PRIORITY_LOW,PRIORITY_MIN,PRIORITY_HIGH,PRIORITY_MAX})
462 @Retention(RetentionPolicy.SOURCE)
463 public @interface Priority {}
464
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800465 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500466 * Default notification {@link #priority}. If your application does not prioritize its own
467 * notifications, use this value for all notifications.
468 */
469 public static final int PRIORITY_DEFAULT = 0;
470
471 /**
472 * Lower {@link #priority}, for items that are less important. The UI may choose to show these
473 * items smaller, or at a different position in the list, compared with your app's
474 * {@link #PRIORITY_DEFAULT} items.
475 */
476 public static final int PRIORITY_LOW = -1;
477
478 /**
479 * Lowest {@link #priority}; these items might not be shown to the user except under special
480 * circumstances, such as detailed notification logs.
481 */
482 public static final int PRIORITY_MIN = -2;
483
484 /**
485 * Higher {@link #priority}, for more important notifications or alerts. The UI may choose to
486 * show these items larger, or at a different position in notification lists, compared with
487 * your app's {@link #PRIORITY_DEFAULT} items.
488 */
489 public static final int PRIORITY_HIGH = 1;
490
491 /**
492 * Highest {@link #priority}, for your application's most important items that require the
493 * user's prompt attention or input.
494 */
495 public static final int PRIORITY_MAX = 2;
496
497 /**
498 * Relative priority for this notification.
Joe Malin8d40d042012-11-05 11:36:40 -0800499 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500500 * Priority is an indication of how much of the user's valuable attention should be consumed by
501 * this notification. Low-priority notifications may be hidden from the user in certain
502 * situations, while the user might be interrupted for a higher-priority notification. The
Daniel Sandler6738eee2012-11-16 12:03:32 -0500503 * system will make a determination about how to interpret this priority when presenting
504 * the notification.
Chris Wren47c20a12014-06-18 17:27:29 -0400505 *
506 * <p>
507 * A notification that is at least {@link #PRIORITY_HIGH} is more likely to be presented
508 * as a heads-up notification.
509 * </p>
510 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500511 */
Tor Norbyed9273d62013-05-30 15:59:53 -0700512 @Priority
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500513 public int priority;
Joe Malin8d40d042012-11-05 11:36:40 -0800514
Dan Sandler26e81cf2014-05-06 10:01:27 -0400515 /**
516 * Accent color (an ARGB integer like the constants in {@link android.graphics.Color})
517 * to be applied by the standard Style templates when presenting this notification.
518 *
519 * The current template design constructs a colorful header image by overlaying the
520 * {@link #icon} image (stenciled in white) atop a field of this color. Alpha components are
521 * ignored.
522 */
523 public int color = COLOR_DEFAULT;
524
525 /**
526 * Special value of {@link #color} telling the system not to decorate this notification with
527 * any special color but instead use default colors when presenting this notification.
528 */
529 public static final int COLOR_DEFAULT = 0; // AKA Color.TRANSPARENT
Dan Sandler0bf2ed82013-12-21 23:33:41 -0600530
531 /**
532 * Sphere of visibility of this notification, which affects how and when the SystemUI reveals
533 * the notification's presence and contents in untrusted situations (namely, on the secure
534 * lockscreen).
535 *
536 * The default level, {@link #VISIBILITY_PRIVATE}, behaves exactly as notifications have always
537 * done on Android: The notification's {@link #icon} and {@link #tickerText} (if available) are
538 * shown in all situations, but the contents are only available if the device is unlocked for
539 * the appropriate user.
540 *
541 * A more permissive policy can be expressed by {@link #VISIBILITY_PUBLIC}; such a notification
542 * can be read even in an "insecure" context (that is, above a secure lockscreen).
543 * To modify the public version of this notification—for example, to redact some portions—see
544 * {@link Builder#setPublicVersion(Notification)}.
545 *
546 * Finally, a notification can be made {@link #VISIBILITY_SECRET}, which will suppress its icon
547 * and ticker until the user has bypassed the lockscreen.
548 */
549 public int visibility;
550
Griff Hazenfc3922d2014-08-20 11:56:44 -0700551 /**
552 * Notification visibility: Show this notification in its entirety on all lockscreens.
553 *
554 * {@see #visibility}
555 */
Dan Sandler0bf2ed82013-12-21 23:33:41 -0600556 public static final int VISIBILITY_PUBLIC = 1;
Griff Hazenfc3922d2014-08-20 11:56:44 -0700557
558 /**
559 * Notification visibility: Show this notification on all lockscreens, but conceal sensitive or
560 * private information on secure lockscreens.
561 *
562 * {@see #visibility}
563 */
Dan Sandler0bf2ed82013-12-21 23:33:41 -0600564 public static final int VISIBILITY_PRIVATE = 0;
Griff Hazenfc3922d2014-08-20 11:56:44 -0700565
566 /**
567 * Notification visibility: Do not reveal any part of this notification on a secure lockscreen.
568 *
569 * {@see #visibility}
570 */
Dan Sandler0bf2ed82013-12-21 23:33:41 -0600571 public static final int VISIBILITY_SECRET = -1;
572
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500573 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400574 * Notification category: incoming call (voice or video) or similar synchronous communication request.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500575 */
John Spurlockfd7f1e02014-03-18 16:41:57 -0400576 public static final String CATEGORY_CALL = "call";
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500577
578 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400579 * Notification category: incoming direct message (SMS, instant message, etc.).
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500580 */
John Spurlockfd7f1e02014-03-18 16:41:57 -0400581 public static final String CATEGORY_MESSAGE = "msg";
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500582
583 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400584 * Notification category: asynchronous bulk message (email).
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500585 */
John Spurlockfd7f1e02014-03-18 16:41:57 -0400586 public static final String CATEGORY_EMAIL = "email";
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500587
588 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400589 * Notification category: calendar event.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500590 */
John Spurlockfd7f1e02014-03-18 16:41:57 -0400591 public static final String CATEGORY_EVENT = "event";
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500592
593 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400594 * Notification category: promotion or advertisement.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500595 */
John Spurlockfd7f1e02014-03-18 16:41:57 -0400596 public static final String CATEGORY_PROMO = "promo";
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500597
598 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400599 * Notification category: alarm or timer.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500600 */
John Spurlockfd7f1e02014-03-18 16:41:57 -0400601 public static final String CATEGORY_ALARM = "alarm";
602
603 /**
604 * Notification category: progress of a long-running background operation.
605 */
606 public static final String CATEGORY_PROGRESS = "progress";
607
608 /**
609 * Notification category: social network or sharing update.
610 */
611 public static final String CATEGORY_SOCIAL = "social";
612
613 /**
614 * Notification category: error in background operation or authentication status.
615 */
616 public static final String CATEGORY_ERROR = "err";
617
618 /**
619 * Notification category: media transport control for playback.
620 */
621 public static final String CATEGORY_TRANSPORT = "transport";
622
623 /**
624 * Notification category: system or device status update. Reserved for system use.
625 */
626 public static final String CATEGORY_SYSTEM = "sys";
627
628 /**
629 * Notification category: indication of running background service.
630 */
631 public static final String CATEGORY_SERVICE = "service";
632
633 /**
John Spurlock0a69c8c2014-03-21 13:30:57 -0400634 * Notification category: a specific, timely recommendation for a single thing.
635 * For example, a news app might want to recommend a news story it believes the user will
636 * want to read next.
637 */
638 public static final String CATEGORY_RECOMMENDATION = "recommendation";
639
640 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400641 * Notification category: ongoing information about device or contextual status.
642 */
643 public static final String CATEGORY_STATUS = "status";
644
645 /**
646 * One of the predefined notification categories (see the <code>CATEGORY_*</code> constants)
647 * that best describes this Notification. May be used by the system for ranking and filtering.
648 */
649 public String category;
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500650
Griff Hazen5cadc3b2014-05-20 09:55:39 -0700651 private String mGroupKey;
652
653 /**
654 * Get the key used to group this notification into a cluster or stack
655 * with other notifications on devices which support such rendering.
656 */
657 public String getGroup() {
658 return mGroupKey;
659 }
660
661 private String mSortKey;
662
663 /**
664 * Get a sort key that orders this notification among other notifications from the
665 * same package. This can be useful if an external sort was already applied and an app
666 * would like to preserve this. Notifications will be sorted lexicographically using this
667 * value, although providing different priorities in addition to providing sort key may
668 * cause this value to be ignored.
669 *
670 * <p>This sort key can also be used to order members of a notification group. See
671 * {@link Builder#setGroup}.
672 *
673 * @see String#compareTo(String)
674 */
675 public String getSortKey() {
676 return mSortKey;
677 }
678
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500679 /**
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400680 * Additional semantic data to be carried around with this Notification.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400681 * <p>
682 * The extras keys defined here are intended to capture the original inputs to {@link Builder}
683 * APIs, and are intended to be used by
684 * {@link android.service.notification.NotificationListenerService} implementations to extract
685 * detailed information from notification objects.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500686 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400687 public Bundle extras = new Bundle();
688
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400689 /**
690 * {@link #extras} key: this is the title of the notification,
691 * as supplied to {@link Builder#setContentTitle(CharSequence)}.
692 */
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -0500693 public static final String EXTRA_TITLE = "android.title";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400694
695 /**
696 * {@link #extras} key: this is the title of the notification when shown in expanded form,
697 * e.g. as supplied to {@link BigTextStyle#setBigContentTitle(CharSequence)}.
698 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400699 public static final String EXTRA_TITLE_BIG = EXTRA_TITLE + ".big";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400700
701 /**
702 * {@link #extras} key: this is the main text payload, as supplied to
703 * {@link Builder#setContentText(CharSequence)}.
704 */
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -0500705 public static final String EXTRA_TEXT = "android.text";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400706
707 /**
708 * {@link #extras} key: this is a third line of text, as supplied to
709 * {@link Builder#setSubText(CharSequence)}.
710 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400711 public static final String EXTRA_SUB_TEXT = "android.subText";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400712
713 /**
714 * {@link #extras} key: this is a small piece of additional text as supplied to
715 * {@link Builder#setContentInfo(CharSequence)}.
716 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400717 public static final String EXTRA_INFO_TEXT = "android.infoText";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400718
719 /**
720 * {@link #extras} key: this is a line of summary information intended to be shown
721 * alongside expanded notifications, as supplied to (e.g.)
722 * {@link BigTextStyle#setSummaryText(CharSequence)}.
723 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400724 public static final String EXTRA_SUMMARY_TEXT = "android.summaryText";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400725
726 /**
Christoph Studer4600f9b2014-07-22 22:44:43 +0200727 * {@link #extras} key: this is the longer text shown in the big form of a
728 * {@link BigTextStyle} notification, as supplied to
729 * {@link BigTextStyle#bigText(CharSequence)}.
730 */
731 public static final String EXTRA_BIG_TEXT = "android.bigText";
732
733 /**
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400734 * {@link #extras} key: this is the resource ID of the notification's main small icon, as
735 * supplied to {@link Builder#setSmallIcon(int)}.
736 */
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -0500737 public static final String EXTRA_SMALL_ICON = "android.icon";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400738
739 /**
740 * {@link #extras} key: this is a bitmap to be used instead of the small icon when showing the
741 * notification payload, as
742 * supplied to {@link Builder#setLargeIcon(android.graphics.Bitmap)}.
743 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400744 public static final String EXTRA_LARGE_ICON = "android.largeIcon";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400745
746 /**
747 * {@link #extras} key: this is a bitmap to be used instead of the one from
748 * {@link Builder#setLargeIcon(android.graphics.Bitmap)} when the notification is
749 * shown in its expanded form, as supplied to
750 * {@link BigPictureStyle#bigLargeIcon(android.graphics.Bitmap)}.
751 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400752 public static final String EXTRA_LARGE_ICON_BIG = EXTRA_LARGE_ICON + ".big";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400753
754 /**
755 * {@link #extras} key: this is the progress value supplied to
756 * {@link Builder#setProgress(int, int, boolean)}.
757 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400758 public static final String EXTRA_PROGRESS = "android.progress";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400759
760 /**
761 * {@link #extras} key: this is the maximum value supplied to
762 * {@link Builder#setProgress(int, int, boolean)}.
763 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400764 public static final String EXTRA_PROGRESS_MAX = "android.progressMax";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400765
766 /**
767 * {@link #extras} key: whether the progress bar is indeterminate, supplied to
768 * {@link Builder#setProgress(int, int, boolean)}.
769 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400770 public static final String EXTRA_PROGRESS_INDETERMINATE = "android.progressIndeterminate";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400771
772 /**
773 * {@link #extras} key: whether {@link #when} should be shown as a count-up timer (specifically
774 * a {@link android.widget.Chronometer}) instead of a timestamp, as supplied to
775 * {@link Builder#setUsesChronometer(boolean)}.
776 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400777 public static final String EXTRA_SHOW_CHRONOMETER = "android.showChronometer";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400778
779 /**
780 * {@link #extras} key: whether {@link #when} should be shown,
781 * as supplied to {@link Builder#setShowWhen(boolean)}.
782 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400783 public static final String EXTRA_SHOW_WHEN = "android.showWhen";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400784
785 /**
786 * {@link #extras} key: this is a bitmap to be shown in {@link BigPictureStyle} expanded
787 * notifications, supplied to {@link BigPictureStyle#bigPicture(android.graphics.Bitmap)}.
788 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400789 public static final String EXTRA_PICTURE = "android.picture";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400790
791 /**
792 * {@link #extras} key: An array of CharSequences to show in {@link InboxStyle} expanded
793 * notifications, each of which was supplied to {@link InboxStyle#addLine(CharSequence)}.
794 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400795 public static final String EXTRA_TEXT_LINES = "android.textLines";
Dan Sandler842dd772014-05-15 09:36:47 -0400796
797 /**
798 * {@link #extras} key: A string representing the name of the specific
799 * {@link android.app.Notification.Style} used to create this notification.
800 */
Chris Wren91ad5632013-06-05 15:05:57 -0400801 public static final String EXTRA_TEMPLATE = "android.template";
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400802
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400803 /**
Chris Wrene6c48932014-09-29 17:19:27 -0400804 * {@link #extras} key: A String array containing the people that this notification relates to,
805 * each of which was supplied to {@link Builder#addPerson(String)}.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400806 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400807 public static final String EXTRA_PEOPLE = "android.people";
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500808
809 /**
Chris Wren47c20a12014-06-18 17:27:29 -0400810 * {@link #extras} key: used to provide hints about the appropriateness of
811 * displaying this notification as a heads-up notification.
Chris Wren51c75102013-07-16 20:49:17 -0400812 * @hide
813 */
814 public static final String EXTRA_AS_HEADS_UP = "headsup";
815
816 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400817 * Allow certain system-generated notifications to appear before the device is provisioned.
818 * Only available to notifications coming from the android package.
819 * @hide
820 */
821 public static final String EXTRA_ALLOW_DURING_SETUP = "android.allowDuringSetup";
822
823 /**
Jose Limae9e3b3b2014-05-18 23:44:50 -0700824 * {@link #extras} key: A
825 * {@link android.content.ContentUris content URI} pointing to an image that can be displayed
826 * in the background when the notification is selected. The URI must point to an image stream
827 * suitable for passing into
828 * {@link android.graphics.BitmapFactory#decodeStream(java.io.InputStream)
829 * BitmapFactory.decodeStream}; all other content types will be ignored. The content provider
830 * URI used for this purpose must require no permissions to read the image data.
831 */
832 public static final String EXTRA_BACKGROUND_IMAGE_URI = "android.backgroundImageUri";
833
834 /**
Dan Sandler842dd772014-05-15 09:36:47 -0400835 * {@link #extras} key: A
Jeff Browndba34ba2014-06-24 20:46:03 -0700836 * {@link android.media.session.MediaSession.Token} associated with a
Dan Sandler842dd772014-05-15 09:36:47 -0400837 * {@link android.app.Notification.MediaStyle} notification.
838 */
839 public static final String EXTRA_MEDIA_SESSION = "android.mediaSession";
840
841 /**
Bryan Mawhinneye191f902014-07-22 12:50:09 +0100842 * {@link #extras} key: the indices of actions to be shown in the compact view,
843 * as supplied to (e.g.) {@link MediaStyle#setShowActionsInCompactView(int...)}.
844 */
845 public static final String EXTRA_COMPACT_ACTIONS = "android.compactActions";
846
Christoph Studer943aa672014-08-03 20:31:16 +0200847 /**
Kenny Guy8942bcd2014-09-08 21:09:47 +0100848 * {@link #extras} key: the user that built the notification.
849 *
850 * @hide
851 */
852 public static final String EXTRA_ORIGINATING_USERID = "android.originatingUserId";
853
854 /**
Chris Wren47c20a12014-06-18 17:27:29 -0400855 * Value for {@link #EXTRA_AS_HEADS_UP} that indicates this notification should not be
856 * displayed in the heads up space.
857 *
858 * <p>
859 * If this notification has a {@link #fullScreenIntent}, then it will always launch the
860 * full-screen intent when posted.
861 * </p>
Chris Wren51c75102013-07-16 20:49:17 -0400862 * @hide
863 */
864 public static final int HEADS_UP_NEVER = 0;
865
866 /**
Chris Wren47c20a12014-06-18 17:27:29 -0400867 * Default value for {@link #EXTRA_AS_HEADS_UP} that indicates this notification may be
868 * displayed as a heads up.
Chris Wren51c75102013-07-16 20:49:17 -0400869 * @hide
870 */
871 public static final int HEADS_UP_ALLOWED = 1;
872
873 /**
Chris Wren47c20a12014-06-18 17:27:29 -0400874 * Value for {@link #EXTRA_AS_HEADS_UP} that indicates this notification is a
875 * good candidate for display as a heads up.
Chris Wren51c75102013-07-16 20:49:17 -0400876 * @hide
877 */
878 public static final int HEADS_UP_REQUESTED = 2;
879
880 /**
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400881 * Structure to encapsulate a named action that can be shown as part of this notification.
882 * It must include an icon, a label, and a {@link PendingIntent} to be fired when the action is
883 * selected by the user.
884 * <p>
Griff Hazen959591e2014-05-15 22:26:18 -0700885 * Apps should use {@link Notification.Builder#addAction(int, CharSequence, PendingIntent)}
886 * or {@link Notification.Builder#addAction(Notification.Action)}
887 * to attach actions.
Daniel Sandlera0a938c2012-03-15 08:42:37 -0400888 */
Daniel Sandlerea2a3172013-02-20 22:24:20 -0500889 public static class Action implements Parcelable {
Griff Hazen959591e2014-05-15 22:26:18 -0700890 private final Bundle mExtras;
Griff Hazen61a9e862014-05-22 16:05:19 -0700891 private final RemoteInput[] mRemoteInputs;
Griff Hazen959591e2014-05-15 22:26:18 -0700892
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400893 /**
894 * Small icon representing the action.
895 */
Daniel Sandlera0a938c2012-03-15 08:42:37 -0400896 public int icon;
Griff Hazen5cadc3b2014-05-20 09:55:39 -0700897
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400898 /**
899 * Title of the action.
900 */
Daniel Sandlera0a938c2012-03-15 08:42:37 -0400901 public CharSequence title;
Griff Hazen5cadc3b2014-05-20 09:55:39 -0700902
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400903 /**
904 * Intent to send when the user invokes this action. May be null, in which case the action
905 * may be rendered in a disabled presentation by the system UI.
906 */
Daniel Sandlera0a938c2012-03-15 08:42:37 -0400907 public PendingIntent actionIntent;
Griff Hazen959591e2014-05-15 22:26:18 -0700908
Daniel Sandlera0a938c2012-03-15 08:42:37 -0400909 private Action(Parcel in) {
910 icon = in.readInt();
911 title = TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(in);
912 if (in.readInt() == 1) {
913 actionIntent = PendingIntent.CREATOR.createFromParcel(in);
914 }
Griff Hazen959591e2014-05-15 22:26:18 -0700915 mExtras = in.readBundle();
Griff Hazen5cadc3b2014-05-20 09:55:39 -0700916 mRemoteInputs = in.createTypedArray(RemoteInput.CREATOR);
Daniel Sandlera0a938c2012-03-15 08:42:37 -0400917 }
Griff Hazen5cadc3b2014-05-20 09:55:39 -0700918
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400919 /**
Griff Hazen959591e2014-05-15 22:26:18 -0700920 * Use {@link Notification.Builder#addAction(int, CharSequence, PendingIntent)}.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400921 */
922 public Action(int icon, CharSequence title, PendingIntent intent) {
Griff Hazen5cadc3b2014-05-20 09:55:39 -0700923 this(icon, title, intent, new Bundle(), null);
Griff Hazen959591e2014-05-15 22:26:18 -0700924 }
925
Griff Hazen5cadc3b2014-05-20 09:55:39 -0700926 private Action(int icon, CharSequence title, PendingIntent intent, Bundle extras,
927 RemoteInput[] remoteInputs) {
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400928 this.icon = icon;
929 this.title = title;
930 this.actionIntent = intent;
Griff Hazen959591e2014-05-15 22:26:18 -0700931 this.mExtras = extras != null ? extras : new Bundle();
Griff Hazen5cadc3b2014-05-20 09:55:39 -0700932 this.mRemoteInputs = remoteInputs;
Griff Hazen959591e2014-05-15 22:26:18 -0700933 }
934
935 /**
936 * Get additional metadata carried around with this Action.
937 */
938 public Bundle getExtras() {
939 return mExtras;
940 }
941
942 /**
Griff Hazen5cadc3b2014-05-20 09:55:39 -0700943 * Get the list of inputs to be collected from the user when this action is sent.
944 * May return null if no remote inputs were added.
945 */
946 public RemoteInput[] getRemoteInputs() {
947 return mRemoteInputs;
948 }
949
950 /**
Griff Hazen959591e2014-05-15 22:26:18 -0700951 * Builder class for {@link Action} objects.
952 */
Griff Hazen5cadc3b2014-05-20 09:55:39 -0700953 public static final class Builder {
Griff Hazen959591e2014-05-15 22:26:18 -0700954 private final int mIcon;
955 private final CharSequence mTitle;
956 private final PendingIntent mIntent;
957 private final Bundle mExtras;
Griff Hazen5cadc3b2014-05-20 09:55:39 -0700958 private ArrayList<RemoteInput> mRemoteInputs;
Griff Hazen959591e2014-05-15 22:26:18 -0700959
960 /**
961 * Construct a new builder for {@link Action} object.
962 * @param icon icon to show for this action
963 * @param title the title of the action
964 * @param intent the {@link PendingIntent} to fire when users trigger this action
965 */
966 public Builder(int icon, CharSequence title, PendingIntent intent) {
Griff Hazen5cadc3b2014-05-20 09:55:39 -0700967 this(icon, title, intent, new Bundle(), null);
Griff Hazen959591e2014-05-15 22:26:18 -0700968 }
969
970 /**
971 * Construct a new builder for {@link Action} object using the fields from an
972 * {@link Action}.
973 * @param action the action to read fields from.
974 */
975 public Builder(Action action) {
Griff Hazen5cadc3b2014-05-20 09:55:39 -0700976 this(action.icon, action.title, action.actionIntent, new Bundle(action.mExtras),
977 action.getRemoteInputs());
Griff Hazen959591e2014-05-15 22:26:18 -0700978 }
979
Griff Hazen5cadc3b2014-05-20 09:55:39 -0700980 private Builder(int icon, CharSequence title, PendingIntent intent, Bundle extras,
981 RemoteInput[] remoteInputs) {
Griff Hazen959591e2014-05-15 22:26:18 -0700982 mIcon = icon;
983 mTitle = title;
984 mIntent = intent;
985 mExtras = extras;
Griff Hazen5cadc3b2014-05-20 09:55:39 -0700986 if (remoteInputs != null) {
987 mRemoteInputs = new ArrayList<RemoteInput>(remoteInputs.length);
988 Collections.addAll(mRemoteInputs, remoteInputs);
989 }
Griff Hazen959591e2014-05-15 22:26:18 -0700990 }
991
992 /**
993 * Merge additional metadata into this builder.
994 *
995 * <p>Values within the Bundle will replace existing extras values in this Builder.
996 *
997 * @see Notification.Action#extras
998 */
999 public Builder addExtras(Bundle extras) {
1000 if (extras != null) {
1001 mExtras.putAll(extras);
1002 }
1003 return this;
1004 }
1005
1006 /**
1007 * Get the metadata Bundle used by this Builder.
1008 *
1009 * <p>The returned Bundle is shared with this Builder.
1010 */
1011 public Bundle getExtras() {
1012 return mExtras;
1013 }
1014
1015 /**
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001016 * Add an input to be collected from the user when this action is sent.
1017 * Response values can be retrieved from the fired intent by using the
1018 * {@link RemoteInput#getResultsFromIntent} function.
1019 * @param remoteInput a {@link RemoteInput} to add to the action
1020 * @return this object for method chaining
1021 */
1022 public Builder addRemoteInput(RemoteInput remoteInput) {
1023 if (mRemoteInputs == null) {
1024 mRemoteInputs = new ArrayList<RemoteInput>();
1025 }
1026 mRemoteInputs.add(remoteInput);
1027 return this;
1028 }
1029
1030 /**
1031 * Apply an extender to this action builder. Extenders may be used to add
1032 * metadata or change options on this builder.
1033 */
Griff Hazen61a9e862014-05-22 16:05:19 -07001034 public Builder extend(Extender extender) {
1035 extender.extend(this);
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001036 return this;
1037 }
1038
1039 /**
Griff Hazen959591e2014-05-15 22:26:18 -07001040 * Combine all of the options that have been set and return a new {@link Action}
1041 * object.
1042 * @return the built action
1043 */
1044 public Action build() {
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001045 RemoteInput[] remoteInputs = mRemoteInputs != null
1046 ? mRemoteInputs.toArray(new RemoteInput[mRemoteInputs.size()]) : null;
1047 return new Action(mIcon, mTitle, mIntent, mExtras, remoteInputs);
Griff Hazen959591e2014-05-15 22:26:18 -07001048 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001049 }
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001050
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001051 @Override
1052 public Action clone() {
1053 return new Action(
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001054 icon,
1055 title,
1056 actionIntent, // safe to alias
1057 new Bundle(mExtras),
1058 getRemoteInputs());
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001059 }
1060 @Override
1061 public int describeContents() {
1062 return 0;
1063 }
1064 @Override
1065 public void writeToParcel(Parcel out, int flags) {
1066 out.writeInt(icon);
1067 TextUtils.writeToParcel(title, out, flags);
1068 if (actionIntent != null) {
1069 out.writeInt(1);
1070 actionIntent.writeToParcel(out, flags);
1071 } else {
1072 out.writeInt(0);
1073 }
Griff Hazen959591e2014-05-15 22:26:18 -07001074 out.writeBundle(mExtras);
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001075 out.writeTypedArray(mRemoteInputs, flags);
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001076 }
Griff Hazen959591e2014-05-15 22:26:18 -07001077 public static final Parcelable.Creator<Action> CREATOR =
1078 new Parcelable.Creator<Action>() {
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001079 public Action createFromParcel(Parcel in) {
1080 return new Action(in);
1081 }
1082 public Action[] newArray(int size) {
1083 return new Action[size];
1084 }
1085 };
Griff Hazen61a9e862014-05-22 16:05:19 -07001086
1087 /**
1088 * Extender interface for use with {@link Builder#extend}. Extenders may be used to add
1089 * metadata or change options on an action builder.
1090 */
1091 public interface Extender {
1092 /**
1093 * Apply this extender to a notification action builder.
1094 * @param builder the builder to be modified.
1095 * @return the build object for chaining.
1096 */
1097 public Builder extend(Builder builder);
1098 }
1099
1100 /**
1101 * Wearable extender for notification actions. To add extensions to an action,
1102 * create a new {@link android.app.Notification.Action.WearableExtender} object using
1103 * the {@code WearableExtender()} constructor and apply it to a
1104 * {@link android.app.Notification.Action.Builder} using
1105 * {@link android.app.Notification.Action.Builder#extend}.
1106 *
1107 * <pre class="prettyprint">
1108 * Notification.Action action = new Notification.Action.Builder(
1109 * R.drawable.archive_all, "Archive all", actionIntent)
Griff Hazen14f57992014-05-26 09:07:14 -07001110 * .extend(new Notification.Action.WearableExtender()
Griff Hazen61a9e862014-05-22 16:05:19 -07001111 * .setAvailableOffline(false))
Griff Hazen14f57992014-05-26 09:07:14 -07001112 * .build();</pre>
Griff Hazen61a9e862014-05-22 16:05:19 -07001113 */
1114 public static final class WearableExtender implements Extender {
1115 /** Notification action extra which contains wearable extensions */
1116 private static final String EXTRA_WEARABLE_EXTENSIONS = "android.wearable.EXTENSIONS";
1117
Pete Gastaf6781d2014-10-07 15:17:05 -04001118 // Keys within EXTRA_WEARABLE_EXTENSIONS for wearable options.
Griff Hazen61a9e862014-05-22 16:05:19 -07001119 private static final String KEY_FLAGS = "flags";
Pete Gastaf6781d2014-10-07 15:17:05 -04001120 private static final String KEY_IN_PROGRESS_LABEL = "inProgressLabel";
1121 private static final String KEY_CONFIRM_LABEL = "confirmLabel";
1122 private static final String KEY_CANCEL_LABEL = "cancelLabel";
Griff Hazen61a9e862014-05-22 16:05:19 -07001123
1124 // Flags bitwise-ored to mFlags
1125 private static final int FLAG_AVAILABLE_OFFLINE = 0x1;
1126
1127 // Default value for flags integer
1128 private static final int DEFAULT_FLAGS = FLAG_AVAILABLE_OFFLINE;
1129
1130 private int mFlags = DEFAULT_FLAGS;
1131
Pete Gastaf6781d2014-10-07 15:17:05 -04001132 private CharSequence mInProgressLabel;
1133 private CharSequence mConfirmLabel;
1134 private CharSequence mCancelLabel;
1135
Griff Hazen61a9e862014-05-22 16:05:19 -07001136 /**
1137 * Create a {@link android.app.Notification.Action.WearableExtender} with default
1138 * options.
1139 */
1140 public WearableExtender() {
1141 }
1142
1143 /**
1144 * Create a {@link android.app.Notification.Action.WearableExtender} by reading
1145 * wearable options present in an existing notification action.
1146 * @param action the notification action to inspect.
1147 */
1148 public WearableExtender(Action action) {
1149 Bundle wearableBundle = action.getExtras().getBundle(EXTRA_WEARABLE_EXTENSIONS);
1150 if (wearableBundle != null) {
1151 mFlags = wearableBundle.getInt(KEY_FLAGS, DEFAULT_FLAGS);
Pete Gastaf6781d2014-10-07 15:17:05 -04001152 mInProgressLabel = wearableBundle.getCharSequence(KEY_IN_PROGRESS_LABEL);
1153 mConfirmLabel = wearableBundle.getCharSequence(KEY_CONFIRM_LABEL);
1154 mCancelLabel = wearableBundle.getCharSequence(KEY_CANCEL_LABEL);
Griff Hazen61a9e862014-05-22 16:05:19 -07001155 }
1156 }
1157
1158 /**
1159 * Apply wearable extensions to a notification action that is being built. This is
1160 * typically called by the {@link android.app.Notification.Action.Builder#extend}
1161 * method of {@link android.app.Notification.Action.Builder}.
1162 */
1163 @Override
1164 public Action.Builder extend(Action.Builder builder) {
1165 Bundle wearableBundle = new Bundle();
1166
1167 if (mFlags != DEFAULT_FLAGS) {
1168 wearableBundle.putInt(KEY_FLAGS, mFlags);
1169 }
Pete Gastaf6781d2014-10-07 15:17:05 -04001170 if (mInProgressLabel != null) {
1171 wearableBundle.putCharSequence(KEY_IN_PROGRESS_LABEL, mInProgressLabel);
1172 }
1173 if (mConfirmLabel != null) {
1174 wearableBundle.putCharSequence(KEY_CONFIRM_LABEL, mConfirmLabel);
1175 }
1176 if (mCancelLabel != null) {
1177 wearableBundle.putCharSequence(KEY_CANCEL_LABEL, mCancelLabel);
1178 }
Griff Hazen61a9e862014-05-22 16:05:19 -07001179
1180 builder.getExtras().putBundle(EXTRA_WEARABLE_EXTENSIONS, wearableBundle);
1181 return builder;
1182 }
1183
1184 @Override
1185 public WearableExtender clone() {
1186 WearableExtender that = new WearableExtender();
1187 that.mFlags = this.mFlags;
Pete Gastaf6781d2014-10-07 15:17:05 -04001188 that.mInProgressLabel = this.mInProgressLabel;
1189 that.mConfirmLabel = this.mConfirmLabel;
1190 that.mCancelLabel = this.mCancelLabel;
Griff Hazen61a9e862014-05-22 16:05:19 -07001191 return that;
1192 }
1193
1194 /**
1195 * Set whether this action is available when the wearable device is not connected to
1196 * a companion device. The user can still trigger this action when the wearable device is
1197 * offline, but a visual hint will indicate that the action may not be available.
1198 * Defaults to true.
1199 */
1200 public WearableExtender setAvailableOffline(boolean availableOffline) {
1201 setFlag(FLAG_AVAILABLE_OFFLINE, availableOffline);
1202 return this;
1203 }
1204
1205 /**
1206 * Get whether this action is available when the wearable device is not connected to
1207 * a companion device. The user can still trigger this action when the wearable device is
1208 * offline, but a visual hint will indicate that the action may not be available.
1209 * Defaults to true.
1210 */
1211 public boolean isAvailableOffline() {
1212 return (mFlags & FLAG_AVAILABLE_OFFLINE) != 0;
1213 }
1214
1215 private void setFlag(int mask, boolean value) {
1216 if (value) {
1217 mFlags |= mask;
1218 } else {
1219 mFlags &= ~mask;
1220 }
1221 }
Pete Gastaf6781d2014-10-07 15:17:05 -04001222
1223 /**
1224 * Set a label to display while the wearable is preparing to automatically execute the
1225 * action. This is usually a 'ing' verb ending in ellipsis like "Sending..."
1226 *
1227 * @param label the label to display while the action is being prepared to execute
1228 * @return this object for method chaining
1229 */
1230 public WearableExtender setInProgressLabel(CharSequence label) {
1231 mInProgressLabel = label;
1232 return this;
1233 }
1234
1235 /**
1236 * Get the label to display while the wearable is preparing to automatically execute
1237 * the action. This is usually a 'ing' verb ending in ellipsis like "Sending..."
1238 *
1239 * @return the label to display while the action is being prepared to execute
1240 */
1241 public CharSequence getInProgressLabel() {
1242 return mInProgressLabel;
1243 }
1244
1245 /**
1246 * Set a label to display to confirm that the action should be executed.
1247 * This is usually an imperative verb like "Send".
1248 *
1249 * @param label the label to confirm the action should be executed
1250 * @return this object for method chaining
1251 */
1252 public WearableExtender setConfirmLabel(CharSequence label) {
1253 mConfirmLabel = label;
1254 return this;
1255 }
1256
1257 /**
1258 * Get the label to display to confirm that the action should be executed.
1259 * This is usually an imperative verb like "Send".
1260 *
1261 * @return the label to confirm the action should be executed
1262 */
1263 public CharSequence getConfirmLabel() {
1264 return mConfirmLabel;
1265 }
1266
1267 /**
1268 * Set a label to display to cancel the action.
1269 * This is usually an imperative verb, like "Cancel".
1270 *
1271 * @param label the label to display to cancel the action
1272 * @return this object for method chaining
1273 */
1274 public WearableExtender setCancelLabel(CharSequence label) {
1275 mCancelLabel = label;
1276 return this;
1277 }
1278
1279 /**
1280 * Get the label to display to cancel the action.
1281 * This is usually an imperative verb like "Cancel".
1282 *
1283 * @return the label to display to cancel the action
1284 */
1285 public CharSequence getCancelLabel() {
1286 return mCancelLabel;
1287 }
Griff Hazen61a9e862014-05-22 16:05:19 -07001288 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001289 }
1290
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001291 /**
1292 * Array of all {@link Action} structures attached to this notification by
1293 * {@link Builder#addAction(int, CharSequence, PendingIntent)}. Mostly useful for instances of
1294 * {@link android.service.notification.NotificationListenerService} that provide an alternative
1295 * interface for invoking actions.
1296 */
Daniel Sandlerea2a3172013-02-20 22:24:20 -05001297 public Action[] actions;
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001298
1299 /**
Dan Sandler0bf2ed82013-12-21 23:33:41 -06001300 * Replacement version of this notification whose content will be shown
1301 * in an insecure context such as atop a secure keyguard. See {@link #visibility}
1302 * and {@link #VISIBILITY_PUBLIC}.
1303 */
1304 public Notification publicVersion;
1305
1306 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001307 * Constructs a Notification object with default values.
Joe Onorato46439ce2010-11-19 13:56:21 -08001308 * You might want to consider using {@link Builder} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001309 */
1310 public Notification()
1311 {
1312 this.when = System.currentTimeMillis();
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001313 this.priority = PRIORITY_DEFAULT;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001314 }
1315
1316 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001317 * @hide
1318 */
1319 public Notification(Context context, int icon, CharSequence tickerText, long when,
1320 CharSequence contentTitle, CharSequence contentText, Intent contentIntent)
1321 {
1322 this.when = when;
1323 this.icon = icon;
1324 this.tickerText = tickerText;
1325 setLatestEventInfo(context, contentTitle, contentText,
1326 PendingIntent.getActivity(context, 0, contentIntent, 0));
1327 }
1328
1329 /**
1330 * Constructs a Notification object with the information needed to
1331 * have a status bar icon without the standard expanded view.
1332 *
1333 * @param icon The resource id of the icon to put in the status bar.
1334 * @param tickerText The text that flows by in the status bar when the notification first
1335 * activates.
1336 * @param when The time to show in the time field. In the System.currentTimeMillis
1337 * timebase.
Joe Onorato46439ce2010-11-19 13:56:21 -08001338 *
1339 * @deprecated Use {@link Builder} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001340 */
Joe Onorato46439ce2010-11-19 13:56:21 -08001341 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001342 public Notification(int icon, CharSequence tickerText, long when)
1343 {
1344 this.icon = icon;
1345 this.tickerText = tickerText;
1346 this.when = when;
1347 }
1348
1349 /**
1350 * Unflatten the notification from a parcel.
1351 */
1352 public Notification(Parcel parcel)
1353 {
1354 int version = parcel.readInt();
1355
1356 when = parcel.readLong();
1357 icon = parcel.readInt();
1358 number = parcel.readInt();
1359 if (parcel.readInt() != 0) {
1360 contentIntent = PendingIntent.CREATOR.createFromParcel(parcel);
1361 }
1362 if (parcel.readInt() != 0) {
1363 deleteIntent = PendingIntent.CREATOR.createFromParcel(parcel);
1364 }
1365 if (parcel.readInt() != 0) {
1366 tickerText = TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(parcel);
1367 }
1368 if (parcel.readInt() != 0) {
Joe Onorato46439ce2010-11-19 13:56:21 -08001369 tickerView = RemoteViews.CREATOR.createFromParcel(parcel);
Joe Onoratoef1e7762010-09-17 18:38:38 -04001370 }
1371 if (parcel.readInt() != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001372 contentView = RemoteViews.CREATOR.createFromParcel(parcel);
1373 }
Joe Onorato561d3852010-11-20 18:09:34 -08001374 if (parcel.readInt() != 0) {
1375 largeIcon = Bitmap.CREATOR.createFromParcel(parcel);
1376 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001377 defaults = parcel.readInt();
1378 flags = parcel.readInt();
1379 if (parcel.readInt() != 0) {
1380 sound = Uri.CREATOR.createFromParcel(parcel);
1381 }
1382
1383 audioStreamType = parcel.readInt();
John Spurlockc0650f022014-07-19 13:22:39 -04001384 if (parcel.readInt() != 0) {
1385 audioAttributes = AudioAttributes.CREATOR.createFromParcel(parcel);
1386 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001387 vibrate = parcel.createLongArray();
1388 ledARGB = parcel.readInt();
1389 ledOnMS = parcel.readInt();
1390 ledOffMS = parcel.readInt();
1391 iconLevel = parcel.readInt();
Daniel Sandlere46cbd32010-06-17 10:35:26 -04001392
1393 if (parcel.readInt() != 0) {
1394 fullScreenIntent = PendingIntent.CREATOR.createFromParcel(parcel);
1395 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001396
1397 priority = parcel.readInt();
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001398
John Spurlockfd7f1e02014-03-18 16:41:57 -04001399 category = parcel.readString();
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001400
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001401 mGroupKey = parcel.readString();
1402
1403 mSortKey = parcel.readString();
1404
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001405 extras = parcel.readBundle(); // may be null
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001406
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001407 actions = parcel.createTypedArray(Action.CREATOR); // may be null
1408
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001409 if (parcel.readInt() != 0) {
Daniel Sandlerf3b73432012-03-27 15:01:25 -04001410 bigContentView = RemoteViews.CREATOR.createFromParcel(parcel);
1411 }
Dan Sandler0bf2ed82013-12-21 23:33:41 -06001412
Chris Wren8fd39ec2014-02-27 17:43:26 -05001413 if (parcel.readInt() != 0) {
1414 headsUpContentView = RemoteViews.CREATOR.createFromParcel(parcel);
1415 }
1416
Dan Sandler0bf2ed82013-12-21 23:33:41 -06001417 visibility = parcel.readInt();
1418
1419 if (parcel.readInt() != 0) {
1420 publicVersion = Notification.CREATOR.createFromParcel(parcel);
1421 }
Dan Sandler26e81cf2014-05-06 10:01:27 -04001422
1423 color = parcel.readInt();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001424 }
1425
Andy Stadler110988c2010-12-03 14:29:16 -08001426 @Override
Joe Onorato18e69df2010-05-17 22:26:12 -07001427 public Notification clone() {
1428 Notification that = new Notification();
Daniel Sandler1a497d32013-04-18 14:52:45 -04001429 cloneInto(that, true);
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001430 return that;
1431 }
Joe Onorato18e69df2010-05-17 22:26:12 -07001432
Daniel Sandler1a497d32013-04-18 14:52:45 -04001433 /**
1434 * Copy all (or if heavy is false, all except Bitmaps and RemoteViews) members
1435 * of this into that.
1436 * @hide
1437 */
1438 public void cloneInto(Notification that, boolean heavy) {
Joe Onorato18e69df2010-05-17 22:26:12 -07001439 that.when = this.when;
1440 that.icon = this.icon;
1441 that.number = this.number;
1442
1443 // PendingIntents are global, so there's no reason (or way) to clone them.
1444 that.contentIntent = this.contentIntent;
1445 that.deleteIntent = this.deleteIntent;
Daniel Sandlere46cbd32010-06-17 10:35:26 -04001446 that.fullScreenIntent = this.fullScreenIntent;
Joe Onorato18e69df2010-05-17 22:26:12 -07001447
1448 if (this.tickerText != null) {
1449 that.tickerText = this.tickerText.toString();
1450 }
Daniel Sandler1a497d32013-04-18 14:52:45 -04001451 if (heavy && this.tickerView != null) {
Joe Onorato46439ce2010-11-19 13:56:21 -08001452 that.tickerView = this.tickerView.clone();
Joe Onoratoef1e7762010-09-17 18:38:38 -04001453 }
Daniel Sandler1a497d32013-04-18 14:52:45 -04001454 if (heavy && this.contentView != null) {
Joe Onorato18e69df2010-05-17 22:26:12 -07001455 that.contentView = this.contentView.clone();
1456 }
Daniel Sandler1a497d32013-04-18 14:52:45 -04001457 if (heavy && this.largeIcon != null) {
Joe Onorato561d3852010-11-20 18:09:34 -08001458 that.largeIcon = Bitmap.createBitmap(this.largeIcon);
1459 }
Jozef BABJAKa8b91832011-02-22 08:05:08 +01001460 that.iconLevel = this.iconLevel;
Joe Onorato18e69df2010-05-17 22:26:12 -07001461 that.sound = this.sound; // android.net.Uri is immutable
1462 that.audioStreamType = this.audioStreamType;
John Spurlockc0650f022014-07-19 13:22:39 -04001463 if (this.audioAttributes != null) {
1464 that.audioAttributes = new AudioAttributes.Builder(this.audioAttributes).build();
1465 }
Joe Onorato18e69df2010-05-17 22:26:12 -07001466
1467 final long[] vibrate = this.vibrate;
1468 if (vibrate != null) {
1469 final int N = vibrate.length;
1470 final long[] vib = that.vibrate = new long[N];
1471 System.arraycopy(vibrate, 0, vib, 0, N);
1472 }
1473
1474 that.ledARGB = this.ledARGB;
1475 that.ledOnMS = this.ledOnMS;
1476 that.ledOffMS = this.ledOffMS;
1477 that.defaults = this.defaults;
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001478
Joe Onorato18e69df2010-05-17 22:26:12 -07001479 that.flags = this.flags;
1480
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001481 that.priority = this.priority;
Joe Malin8d40d042012-11-05 11:36:40 -08001482
John Spurlockfd7f1e02014-03-18 16:41:57 -04001483 that.category = this.category;
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001484
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001485 that.mGroupKey = this.mGroupKey;
1486
1487 that.mSortKey = this.mSortKey;
1488
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001489 if (this.extras != null) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04001490 try {
1491 that.extras = new Bundle(this.extras);
1492 // will unparcel
1493 that.extras.size();
1494 } catch (BadParcelableException e) {
1495 Log.e(TAG, "could not unparcel extras from notification: " + this, e);
1496 that.extras = null;
1497 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001498 }
1499
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001500 if (this.actions != null) {
1501 that.actions = new Action[this.actions.length];
1502 for(int i=0; i<this.actions.length; i++) {
1503 that.actions[i] = this.actions[i].clone();
1504 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001505 }
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001506
Daniel Sandler1a497d32013-04-18 14:52:45 -04001507 if (heavy && this.bigContentView != null) {
Daniel Sandlerf3b73432012-03-27 15:01:25 -04001508 that.bigContentView = this.bigContentView.clone();
1509 }
Daniel Sandler1a497d32013-04-18 14:52:45 -04001510
Chris Wren8fd39ec2014-02-27 17:43:26 -05001511 if (heavy && this.headsUpContentView != null) {
1512 that.headsUpContentView = this.headsUpContentView.clone();
1513 }
1514
Dan Sandler0bf2ed82013-12-21 23:33:41 -06001515 that.visibility = this.visibility;
1516
1517 if (this.publicVersion != null) {
1518 that.publicVersion = new Notification();
1519 this.publicVersion.cloneInto(that.publicVersion, heavy);
1520 }
1521
Dan Sandler26e81cf2014-05-06 10:01:27 -04001522 that.color = this.color;
1523
Daniel Sandler1a497d32013-04-18 14:52:45 -04001524 if (!heavy) {
1525 that.lightenPayload(); // will clean out extras
1526 }
1527 }
1528
1529 /**
1530 * Removes heavyweight parts of the Notification object for archival or for sending to
1531 * listeners when the full contents are not necessary.
1532 * @hide
1533 */
1534 public final void lightenPayload() {
1535 tickerView = null;
1536 contentView = null;
1537 bigContentView = null;
Chris Wren8fd39ec2014-02-27 17:43:26 -05001538 headsUpContentView = null;
Daniel Sandler1a497d32013-04-18 14:52:45 -04001539 largeIcon = null;
1540 if (extras != null) {
1541 extras.remove(Notification.EXTRA_LARGE_ICON);
1542 extras.remove(Notification.EXTRA_LARGE_ICON_BIG);
1543 extras.remove(Notification.EXTRA_PICTURE);
Christoph Studer223f44e2014-09-02 14:59:32 +02001544 extras.remove(Notification.EXTRA_BIG_TEXT);
Christoph Studerb82bc782014-08-20 14:29:43 +02001545 // Prevent light notifications from being rebuilt.
1546 extras.remove(Builder.EXTRA_NEEDS_REBUILD);
Daniel Sandler1a497d32013-04-18 14:52:45 -04001547 }
Joe Onorato18e69df2010-05-17 22:26:12 -07001548 }
1549
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04001550 /**
1551 * Make sure this CharSequence is safe to put into a bundle, which basically
1552 * means it had better not be some custom Parcelable implementation.
1553 * @hide
1554 */
1555 public static CharSequence safeCharSequence(CharSequence cs) {
Christoph Studer535ec612014-09-03 15:47:47 +02001556 if (cs == null) return cs;
1557 if (cs.length() > MAX_CHARSEQUENCE_LENGTH) {
1558 cs = cs.subSequence(0, MAX_CHARSEQUENCE_LENGTH);
1559 }
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04001560 if (cs instanceof Parcelable) {
1561 Log.e(TAG, "warning: " + cs.getClass().getCanonicalName()
1562 + " instance is a custom Parcelable and not allowed in Notification");
1563 return cs.toString();
1564 }
1565
1566 return cs;
1567 }
1568
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001569 public int describeContents() {
1570 return 0;
1571 }
1572
1573 /**
1574 * Flatten this notification from a parcel.
1575 */
1576 public void writeToParcel(Parcel parcel, int flags)
1577 {
1578 parcel.writeInt(1);
1579
1580 parcel.writeLong(when);
1581 parcel.writeInt(icon);
1582 parcel.writeInt(number);
1583 if (contentIntent != null) {
1584 parcel.writeInt(1);
1585 contentIntent.writeToParcel(parcel, 0);
1586 } else {
1587 parcel.writeInt(0);
1588 }
1589 if (deleteIntent != null) {
1590 parcel.writeInt(1);
1591 deleteIntent.writeToParcel(parcel, 0);
1592 } else {
1593 parcel.writeInt(0);
1594 }
1595 if (tickerText != null) {
1596 parcel.writeInt(1);
1597 TextUtils.writeToParcel(tickerText, parcel, flags);
1598 } else {
1599 parcel.writeInt(0);
1600 }
Joe Onorato46439ce2010-11-19 13:56:21 -08001601 if (tickerView != null) {
Joe Onoratoef1e7762010-09-17 18:38:38 -04001602 parcel.writeInt(1);
Joe Onorato46439ce2010-11-19 13:56:21 -08001603 tickerView.writeToParcel(parcel, 0);
Joe Onoratoef1e7762010-09-17 18:38:38 -04001604 } else {
1605 parcel.writeInt(0);
1606 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001607 if (contentView != null) {
1608 parcel.writeInt(1);
1609 contentView.writeToParcel(parcel, 0);
1610 } else {
1611 parcel.writeInt(0);
1612 }
Joe Onorato561d3852010-11-20 18:09:34 -08001613 if (largeIcon != null) {
1614 parcel.writeInt(1);
1615 largeIcon.writeToParcel(parcel, 0);
1616 } else {
1617 parcel.writeInt(0);
1618 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001619
1620 parcel.writeInt(defaults);
1621 parcel.writeInt(this.flags);
1622
1623 if (sound != null) {
1624 parcel.writeInt(1);
1625 sound.writeToParcel(parcel, 0);
1626 } else {
1627 parcel.writeInt(0);
1628 }
1629 parcel.writeInt(audioStreamType);
John Spurlockc0650f022014-07-19 13:22:39 -04001630
1631 if (audioAttributes != null) {
1632 parcel.writeInt(1);
1633 audioAttributes.writeToParcel(parcel, 0);
1634 } else {
1635 parcel.writeInt(0);
1636 }
1637
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001638 parcel.writeLongArray(vibrate);
1639 parcel.writeInt(ledARGB);
1640 parcel.writeInt(ledOnMS);
1641 parcel.writeInt(ledOffMS);
1642 parcel.writeInt(iconLevel);
Daniel Sandlere46cbd32010-06-17 10:35:26 -04001643
1644 if (fullScreenIntent != null) {
1645 parcel.writeInt(1);
1646 fullScreenIntent.writeToParcel(parcel, 0);
1647 } else {
1648 parcel.writeInt(0);
1649 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001650
1651 parcel.writeInt(priority);
Joe Malin8d40d042012-11-05 11:36:40 -08001652
John Spurlockfd7f1e02014-03-18 16:41:57 -04001653 parcel.writeString(category);
Joe Malin8d40d042012-11-05 11:36:40 -08001654
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001655 parcel.writeString(mGroupKey);
1656
1657 parcel.writeString(mSortKey);
1658
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001659 parcel.writeBundle(extras); // null ok
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001660
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001661 parcel.writeTypedArray(actions, 0); // null ok
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001662
Daniel Sandlerf3b73432012-03-27 15:01:25 -04001663 if (bigContentView != null) {
1664 parcel.writeInt(1);
1665 bigContentView.writeToParcel(parcel, 0);
1666 } else {
1667 parcel.writeInt(0);
1668 }
Dan Sandler0bf2ed82013-12-21 23:33:41 -06001669
Chris Wren8fd39ec2014-02-27 17:43:26 -05001670 if (headsUpContentView != null) {
1671 parcel.writeInt(1);
1672 headsUpContentView.writeToParcel(parcel, 0);
1673 } else {
1674 parcel.writeInt(0);
1675 }
1676
Dan Sandler0bf2ed82013-12-21 23:33:41 -06001677 parcel.writeInt(visibility);
1678
1679 if (publicVersion != null) {
1680 parcel.writeInt(1);
1681 publicVersion.writeToParcel(parcel, 0);
1682 } else {
1683 parcel.writeInt(0);
1684 }
Dan Sandler26e81cf2014-05-06 10:01:27 -04001685
1686 parcel.writeInt(color);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001687 }
1688
1689 /**
1690 * Parcelable.Creator that instantiates Notification objects
1691 */
1692 public static final Parcelable.Creator<Notification> CREATOR
1693 = new Parcelable.Creator<Notification>()
1694 {
1695 public Notification createFromParcel(Parcel parcel)
1696 {
1697 return new Notification(parcel);
1698 }
1699
1700 public Notification[] newArray(int size)
1701 {
1702 return new Notification[size];
1703 }
1704 };
1705
1706 /**
1707 * Sets the {@link #contentView} field to be a view with the standard "Latest Event"
1708 * layout.
1709 *
1710 * <p>Uses the {@link #icon} and {@link #when} fields to set the icon and time fields
1711 * in the view.</p>
1712 * @param context The context for your application / activity.
1713 * @param contentTitle The title that goes in the expanded entry.
1714 * @param contentText The text that goes in the expanded entry.
1715 * @param contentIntent The intent to launch when the user clicks the expanded notification.
1716 * If this is an activity, it must include the
1717 * {@link android.content.Intent#FLAG_ACTIVITY_NEW_TASK} flag, which requires
Scott Main7aee61f2011-02-08 11:25:01 -08001718 * that you take care of task management as described in the
1719 * <a href="{@docRoot}guide/topics/fundamentals/tasks-and-back-stack.html">Tasks and Back
1720 * Stack</a> document.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001721 *
Joe Onorato46439ce2010-11-19 13:56:21 -08001722 * @deprecated Use {@link Builder} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001723 */
Joe Onorato46439ce2010-11-19 13:56:21 -08001724 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001725 public void setLatestEventInfo(Context context,
1726 CharSequence contentTitle, CharSequence contentText, PendingIntent contentIntent) {
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001727 Notification.Builder builder = new Notification.Builder(context);
1728
1729 // First, ensure that key pieces of information that may have been set directly
1730 // are preserved
1731 builder.setWhen(this.when);
1732 builder.setSmallIcon(this.icon);
1733 builder.setPriority(this.priority);
1734 builder.setTicker(this.tickerText);
1735 builder.setNumber(this.number);
Selim Cinek255dd042014-08-19 22:29:02 +02001736 builder.setColor(this.color);
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001737 builder.mFlags = this.flags;
1738 builder.setSound(this.sound, this.audioStreamType);
1739 builder.setDefaults(this.defaults);
1740 builder.setVibrate(this.vibrate);
Sean Barbeau6ae0b1b2014-07-19 21:31:41 -07001741 builder.setDeleteIntent(this.deleteIntent);
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001742
1743 // now apply the latestEventInfo fields
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001744 if (contentTitle != null) {
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001745 builder.setContentTitle(contentTitle);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001746 }
1747 if (contentText != null) {
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001748 builder.setContentText(contentText);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001749 }
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001750 builder.setContentIntent(contentIntent);
1751 builder.buildInto(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001752 }
1753
1754 @Override
1755 public String toString() {
1756 StringBuilder sb = new StringBuilder();
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001757 sb.append("Notification(pri=");
1758 sb.append(priority);
1759 sb.append(" contentView=");
Joe Onoratoc9596d62011-01-12 17:03:11 -08001760 if (contentView != null) {
1761 sb.append(contentView.getPackage());
1762 sb.append("/0x");
1763 sb.append(Integer.toHexString(contentView.getLayoutId()));
1764 } else {
1765 sb.append("null");
1766 }
1767 sb.append(" vibrate=");
Daniel Sandler6738eee2012-11-16 12:03:32 -05001768 if ((this.defaults & DEFAULT_VIBRATE) != 0) {
1769 sb.append("default");
1770 } else if (this.vibrate != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001771 int N = this.vibrate.length-1;
1772 sb.append("[");
1773 for (int i=0; i<N; i++) {
1774 sb.append(this.vibrate[i]);
1775 sb.append(',');
1776 }
Simon Schoar8cf97d92009-06-10 22:08:37 +02001777 if (N != -1) {
1778 sb.append(this.vibrate[N]);
1779 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001780 sb.append("]");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001781 } else {
1782 sb.append("null");
1783 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001784 sb.append(" sound=");
Daniel Sandler6738eee2012-11-16 12:03:32 -05001785 if ((this.defaults & DEFAULT_SOUND) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001786 sb.append("default");
Daniel Sandler6738eee2012-11-16 12:03:32 -05001787 } else if (this.sound != null) {
1788 sb.append(this.sound.toString());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001789 } else {
1790 sb.append("null");
1791 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001792 sb.append(" defaults=0x");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001793 sb.append(Integer.toHexString(this.defaults));
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001794 sb.append(" flags=0x");
Daniel Sandlere46cbd32010-06-17 10:35:26 -04001795 sb.append(Integer.toHexString(this.flags));
Dan Sandler26e81cf2014-05-06 10:01:27 -04001796 sb.append(String.format(" color=0x%08x", this.color));
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001797 if (this.category != null) {
1798 sb.append(" category=");
1799 sb.append(this.category);
1800 }
1801 if (this.mGroupKey != null) {
1802 sb.append(" groupKey=");
1803 sb.append(this.mGroupKey);
1804 }
1805 if (this.mSortKey != null) {
1806 sb.append(" sortKey=");
1807 sb.append(this.mSortKey);
1808 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001809 if (actions != null) {
Dan Sandler1b718782014-07-18 12:43:45 -04001810 sb.append(" actions=");
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001811 sb.append(actions.length);
Dan Sandler1b718782014-07-18 12:43:45 -04001812 }
1813 sb.append(" vis=");
1814 sb.append(visibilityToString(this.visibility));
1815 if (this.publicVersion != null) {
1816 sb.append(" publicVersion=");
1817 sb.append(publicVersion.toString());
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001818 }
1819 sb.append(")");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001820 return sb.toString();
1821 }
Joe Onorato46439ce2010-11-19 13:56:21 -08001822
Dan Sandler1b718782014-07-18 12:43:45 -04001823 /**
1824 * {@hide}
1825 */
1826 public static String visibilityToString(int vis) {
1827 switch (vis) {
1828 case VISIBILITY_PRIVATE:
1829 return "PRIVATE";
1830 case VISIBILITY_PUBLIC:
1831 return "PUBLIC";
1832 case VISIBILITY_SECRET:
1833 return "SECRET";
1834 default:
1835 return "UNKNOWN(" + String.valueOf(vis) + ")";
1836 }
1837 }
1838
Joe Onoratocb109a02011-01-18 17:57:41 -08001839 /**
Christoph Studer4600f9b2014-07-22 22:44:43 +02001840 * @hide
1841 */
1842 public boolean isValid() {
1843 // Would like to check for icon!=0 here, too, but NotificationManagerService accepts that
1844 // for legacy reasons.
1845 return contentView != null || extras.getBoolean(Builder.EXTRA_REBUILD_CONTENT_VIEW);
1846 }
1847
1848 /**
Christoph Studerc8db24b2014-07-25 17:50:30 +02001849 * @hide
1850 */
1851 public boolean isGroupSummary() {
1852 return mGroupKey != null && (flags & FLAG_GROUP_SUMMARY) != 0;
1853 }
1854
1855 /**
1856 * @hide
1857 */
1858 public boolean isGroupChild() {
1859 return mGroupKey != null && (flags & FLAG_GROUP_SUMMARY) == 0;
1860 }
1861
1862 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001863 * Builder class for {@link Notification} objects.
Joe Malin8d40d042012-11-05 11:36:40 -08001864 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001865 * Provides a convenient way to set the various fields of a {@link Notification} and generate
Scott Main183bf112012-08-13 19:12:13 -07001866 * content views using the platform's notification layout template. If your app supports
1867 * versions of Android as old as API level 4, you can instead use
1868 * {@link android.support.v4.app.NotificationCompat.Builder NotificationCompat.Builder},
1869 * available in the <a href="{@docRoot}tools/extras/support-library.html">Android Support
1870 * library</a>.
Joe Malin8d40d042012-11-05 11:36:40 -08001871 *
Scott Main183bf112012-08-13 19:12:13 -07001872 * <p>Example:
Joe Malin8d40d042012-11-05 11:36:40 -08001873 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001874 * <pre class="prettyprint">
Scott Main183bf112012-08-13 19:12:13 -07001875 * Notification noti = new Notification.Builder(mContext)
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001876 * .setContentTitle(&quot;New mail from &quot; + sender.toString())
1877 * .setContentText(subject)
1878 * .setSmallIcon(R.drawable.new_mail)
1879 * .setLargeIcon(aBitmap)
Chris Wrenfbd96ba2012-05-01 12:03:58 -04001880 * .build();
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001881 * </pre>
Joe Onoratocb109a02011-01-18 17:57:41 -08001882 */
Joe Onorato46439ce2010-11-19 13:56:21 -08001883 public static class Builder {
Daniel Sandler602ad1c2012-06-12 16:06:27 -04001884 private static final int MAX_ACTION_BUTTONS = 3;
Jorim Jaggi445d3c02014-08-19 22:33:42 +02001885 private static final float LARGE_TEXT_SCALE = 1.3f;
Daniel Sandler8680bf82012-05-15 16:52:52 -04001886
Christoph Studer4600f9b2014-07-22 22:44:43 +02001887 /**
1888 * @hide
1889 */
1890 public static final String EXTRA_NEEDS_REBUILD = "android.rebuild";
1891
1892 /**
1893 * @hide
1894 */
1895 public static final String EXTRA_REBUILD_LARGE_ICON = "android.rebuild.largeIcon";
1896 /**
1897 * @hide
1898 */
1899 public static final String EXTRA_REBUILD_CONTENT_VIEW = "android.rebuild.contentView";
1900 /**
1901 * @hide
1902 */
1903 public static final String EXTRA_REBUILD_CONTENT_VIEW_ACTION_COUNT =
1904 "android.rebuild.contentViewActionCount";
1905 /**
1906 * @hide
1907 */
1908 public static final String EXTRA_REBUILD_BIG_CONTENT_VIEW
1909 = "android.rebuild.bigView";
1910 /**
1911 * @hide
1912 */
1913 public static final String EXTRA_REBUILD_BIG_CONTENT_VIEW_ACTION_COUNT
1914 = "android.rebuild.bigViewActionCount";
1915 /**
1916 * @hide
1917 */
1918 public static final String EXTRA_REBUILD_HEADS_UP_CONTENT_VIEW
1919 = "android.rebuild.hudView";
1920 /**
1921 * @hide
1922 */
1923 public static final String EXTRA_REBUILD_HEADS_UP_CONTENT_VIEW_ACTION_COUNT
1924 = "android.rebuild.hudViewActionCount";
1925
1926 /**
Kenny Guy77320062014-08-27 21:37:15 +01001927 * The ApplicationInfo of the package that created the notification, used to create
1928 * a context to rebuild the notification via a Builder.
1929 * @hide
Christoph Studer4600f9b2014-07-22 22:44:43 +02001930 */
Kenny Guy77320062014-08-27 21:37:15 +01001931 private static final String EXTRA_REBUILD_CONTEXT_APPLICATION_INFO =
1932 "android.rebuild.applicationInfo";
Christoph Studer4600f9b2014-07-22 22:44:43 +02001933
1934 // Whether to enable stripping (at post time) & rebuilding (at listener receive time) of
1935 // memory intensive resources.
1936 private static final boolean STRIP_AND_REBUILD = true;
1937
Joe Onorato46439ce2010-11-19 13:56:21 -08001938 private Context mContext;
1939
1940 private long mWhen;
1941 private int mSmallIcon;
1942 private int mSmallIconLevel;
Joe Onorato8595a3d2010-11-19 18:12:07 -08001943 private int mNumber;
Joe Onorato46439ce2010-11-19 13:56:21 -08001944 private CharSequence mContentTitle;
1945 private CharSequence mContentText;
1946 private CharSequence mContentInfo;
Daniel Sandlerf3b73432012-03-27 15:01:25 -04001947 private CharSequence mSubText;
Joe Onorato46439ce2010-11-19 13:56:21 -08001948 private PendingIntent mContentIntent;
1949 private RemoteViews mContentView;
1950 private PendingIntent mDeleteIntent;
1951 private PendingIntent mFullScreenIntent;
1952 private CharSequence mTickerText;
1953 private RemoteViews mTickerView;
1954 private Bitmap mLargeIcon;
1955 private Uri mSound;
1956 private int mAudioStreamType;
John Spurlockc0650f022014-07-19 13:22:39 -04001957 private AudioAttributes mAudioAttributes;
Joe Onorato46439ce2010-11-19 13:56:21 -08001958 private long[] mVibrate;
1959 private int mLedArgb;
1960 private int mLedOnMs;
1961 private int mLedOffMs;
1962 private int mDefaults;
1963 private int mFlags;
Jeff Sharkey1c400132011-08-05 14:50:13 -07001964 private int mProgressMax;
1965 private int mProgress;
1966 private boolean mProgressIndeterminate;
John Spurlockfd7f1e02014-03-18 16:41:57 -04001967 private String mCategory;
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001968 private String mGroupKey;
1969 private String mSortKey;
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001970 private Bundle mExtras;
1971 private int mPriority;
Daniel Sandler8680bf82012-05-15 16:52:52 -04001972 private ArrayList<Action> mActions = new ArrayList<Action>(MAX_ACTION_BUTTONS);
Daniel Sandlera2985ed2012-04-03 16:42:00 -04001973 private boolean mUseChronometer;
Chris Wrenfbd96ba2012-05-01 12:03:58 -04001974 private Style mStyle;
Daniel Sandler0c890492012-09-12 17:23:10 -07001975 private boolean mShowWhen = true;
Dan Sandler0bf2ed82013-12-21 23:33:41 -06001976 private int mVisibility = VISIBILITY_PRIVATE;
1977 private Notification mPublicVersion = null;
Dan Sandler26e81cf2014-05-06 10:01:27 -04001978 private final NotificationColorUtil mColorUtil;
Chris Wrendde75302014-03-26 17:24:15 -04001979 private ArrayList<String> mPeople;
Dan Sandler26e81cf2014-05-06 10:01:27 -04001980 private int mColor = COLOR_DEFAULT;
Christoph Studer7ac80e62014-08-04 16:01:57 +02001981
1982 /**
Kenny Guy8942bcd2014-09-08 21:09:47 +01001983 * The user that built the notification originally.
1984 */
1985 private int mOriginatingUserId;
1986
1987 /**
Christoph Studer4600f9b2014-07-22 22:44:43 +02001988 * Contains extras related to rebuilding during the build phase.
1989 */
1990 private Bundle mRebuildBundle = new Bundle();
1991 /**
1992 * Contains the notification to rebuild when this Builder is in "rebuild" mode.
1993 * Null otherwise.
1994 */
1995 private Notification mRebuildNotification = null;
1996
Joe Onoratocb109a02011-01-18 17:57:41 -08001997 /**
Jorim Jaggid05aa3e2014-08-28 17:52:27 +02001998 * Whether the build notification has three lines. This is used to make the top padding for
1999 * both the contracted and expanded layout consistent.
2000 *
2001 * <p>
2002 * This field is only valid during the build phase.
2003 */
2004 private boolean mHasThreeLines;
2005
2006 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002007 * Constructs a new Builder with the defaults:
Joe Onoratocb109a02011-01-18 17:57:41 -08002008 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002009
2010 * <table>
2011 * <tr><th align=right>priority</th>
2012 * <td>{@link #PRIORITY_DEFAULT}</td></tr>
2013 * <tr><th align=right>when</th>
2014 * <td>now ({@link System#currentTimeMillis()})</td></tr>
2015 * <tr><th align=right>audio stream</th>
2016 * <td>{@link #STREAM_DEFAULT}</td></tr>
2017 * </table>
Joe Onoratocb109a02011-01-18 17:57:41 -08002018 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002019
2020 * @param context
2021 * A {@link Context} that will be used by the Builder to construct the
2022 * RemoteViews. The Context will not be held past the lifetime of this Builder
2023 * object.
Joe Onoratocb109a02011-01-18 17:57:41 -08002024 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002025 public Builder(Context context) {
Adam Powellbf06fa02014-05-30 12:32:18 -07002026 /*
2027 * Important compatibility note!
2028 * Some apps out in the wild create a Notification.Builder in their Activity subclass
2029 * constructor for later use. At this point Activities - themselves subclasses of
2030 * ContextWrapper - do not have their inner Context populated yet. This means that
2031 * any calls to Context methods from within this constructor can cause NPEs in existing
2032 * apps. Any data populated from mContext should therefore be populated lazily to
2033 * preserve compatibility.
2034 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002035 mContext = context;
Andy Stadler110988c2010-12-03 14:29:16 -08002036
2037 // Set defaults to match the defaults of a Notification
Joe Onorato46439ce2010-11-19 13:56:21 -08002038 mWhen = System.currentTimeMillis();
Andy Stadler110988c2010-12-03 14:29:16 -08002039 mAudioStreamType = STREAM_DEFAULT;
John Spurlockc0650f022014-07-19 13:22:39 -04002040 mAudioAttributes = AUDIO_ATTRIBUTES_DEFAULT;
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002041 mPriority = PRIORITY_DEFAULT;
Chris Wrendde75302014-03-26 17:24:15 -04002042 mPeople = new ArrayList<String>();
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01002043
Dianne Hackborn955d8d62014-10-07 20:17:19 -07002044 mColorUtil = context.getApplicationInfo().targetSdkVersion < Build.VERSION_CODES.LOLLIPOP ?
Dan Sandler05c362d2014-09-03 00:16:27 +02002045 NotificationColorUtil.getInstance(mContext) : null;
Joe Onorato46439ce2010-11-19 13:56:21 -08002046 }
2047
Joe Onoratocb109a02011-01-18 17:57:41 -08002048 /**
Christoph Studer4600f9b2014-07-22 22:44:43 +02002049 * Creates a Builder for rebuilding the given Notification.
2050 * <p>
2051 * Call {@link #rebuild()} to retrieve the rebuilt version of 'n'.
2052 */
2053 private Builder(Context context, Notification n) {
2054 this(context);
2055 mRebuildNotification = n;
2056 restoreFromNotification(n);
2057
2058 Style style = null;
2059 Bundle extras = n.extras;
2060 String templateClass = extras.getString(EXTRA_TEMPLATE);
2061 if (!TextUtils.isEmpty(templateClass)) {
2062 Class<? extends Style> styleClass = getNotificationStyleClass(templateClass);
2063 if (styleClass == null) {
2064 Log.d(TAG, "Unknown style class: " + styleClass);
2065 return;
2066 }
2067
2068 try {
2069 Constructor<? extends Style> constructor = styleClass.getConstructor();
2070 style = constructor.newInstance();
2071 style.restoreFromExtras(extras);
2072 } catch (Throwable t) {
2073 Log.e(TAG, "Could not create Style", t);
2074 return;
2075 }
2076 }
2077 if (style != null) {
2078 setStyle(style);
2079 }
2080 }
2081
2082 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002083 * Add a timestamp pertaining to the notification (usually the time the event occurred).
Daniel Sandler0c890492012-09-12 17:23:10 -07002084 * It will be shown in the notification content view by default; use
Griff Hazen50c11652014-05-16 09:46:31 -07002085 * {@link #setShowWhen(boolean) setShowWhen} to control this.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002086 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002087 * @see Notification#when
Joe Onoratocb109a02011-01-18 17:57:41 -08002088 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002089 public Builder setWhen(long when) {
2090 mWhen = when;
2091 return this;
2092 }
2093
Joe Onoratocb109a02011-01-18 17:57:41 -08002094 /**
Griff Hazen50c11652014-05-16 09:46:31 -07002095 * Control whether the timestamp set with {@link #setWhen(long) setWhen} is shown
Daniel Sandler0c890492012-09-12 17:23:10 -07002096 * in the content view.
2097 */
2098 public Builder setShowWhen(boolean show) {
2099 mShowWhen = show;
2100 return this;
2101 }
2102
2103 /**
Daniel Sandlerd33b8032012-05-10 11:41:48 -04002104 * Show the {@link Notification#when} field as a stopwatch.
Joe Malin8d40d042012-11-05 11:36:40 -08002105 *
2106 * Instead of presenting <code>when</code> as a timestamp, the notification will show an
Daniel Sandlerd33b8032012-05-10 11:41:48 -04002107 * automatically updating display of the minutes and seconds since <code>when</code>.
Daniel Sandlera2985ed2012-04-03 16:42:00 -04002108 *
Daniel Sandlerd33b8032012-05-10 11:41:48 -04002109 * Useful when showing an elapsed time (like an ongoing phone call).
2110 *
2111 * @see android.widget.Chronometer
Daniel Sandlera2985ed2012-04-03 16:42:00 -04002112 * @see Notification#when
2113 */
2114 public Builder setUsesChronometer(boolean b) {
2115 mUseChronometer = b;
2116 return this;
2117 }
2118
2119 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002120 * Set the small icon resource, which will be used to represent the notification in the
2121 * status bar.
Joe Onoratocb109a02011-01-18 17:57:41 -08002122 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002123
2124 * The platform template for the expanded view will draw this icon in the left, unless a
2125 * {@link #setLargeIcon(Bitmap) large icon} has also been specified, in which case the small
2126 * icon will be moved to the right-hand side.
2127 *
2128
2129 * @param icon
2130 * A resource ID in the application's package of the drawable to use.
2131 * @see Notification#icon
Joe Onoratocb109a02011-01-18 17:57:41 -08002132 */
Tor Norbye7b9c9122013-05-30 16:48:33 -07002133 public Builder setSmallIcon(@DrawableRes int icon) {
Joe Onorato46439ce2010-11-19 13:56:21 -08002134 mSmallIcon = icon;
2135 return this;
2136 }
2137
Joe Onoratocb109a02011-01-18 17:57:41 -08002138 /**
2139 * A variant of {@link #setSmallIcon(int) setSmallIcon(int)} that takes an additional
2140 * level parameter for when the icon is a {@link android.graphics.drawable.LevelListDrawable
2141 * LevelListDrawable}.
2142 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002143 * @param icon A resource ID in the application's package of the drawable to use.
Joe Onoratocb109a02011-01-18 17:57:41 -08002144 * @param level The level to use for the icon.
2145 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002146 * @see Notification#icon
2147 * @see Notification#iconLevel
Joe Onoratocb109a02011-01-18 17:57:41 -08002148 */
Tor Norbye7b9c9122013-05-30 16:48:33 -07002149 public Builder setSmallIcon(@DrawableRes int icon, int level) {
Joe Onorato46439ce2010-11-19 13:56:21 -08002150 mSmallIcon = icon;
2151 mSmallIconLevel = level;
2152 return this;
2153 }
2154
Joe Onoratocb109a02011-01-18 17:57:41 -08002155 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002156 * Set the first line of text in the platform notification template.
Joe Onoratocb109a02011-01-18 17:57:41 -08002157 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002158 public Builder setContentTitle(CharSequence title) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04002159 mContentTitle = safeCharSequence(title);
Joe Onorato46439ce2010-11-19 13:56:21 -08002160 return this;
2161 }
2162
Joe Onoratocb109a02011-01-18 17:57:41 -08002163 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002164 * Set the second line of text in the platform notification template.
Joe Onoratocb109a02011-01-18 17:57:41 -08002165 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002166 public Builder setContentText(CharSequence text) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04002167 mContentText = safeCharSequence(text);
Joe Onorato46439ce2010-11-19 13:56:21 -08002168 return this;
2169 }
2170
Joe Onoratocb109a02011-01-18 17:57:41 -08002171 /**
Joe Malin8d40d042012-11-05 11:36:40 -08002172 * Set the third line of text in the platform notification template.
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04002173 * Don't use if you're also using {@link #setProgress(int, int, boolean)}; they occupy the
2174 * same location in the standard template.
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002175 */
2176 public Builder setSubText(CharSequence text) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04002177 mSubText = safeCharSequence(text);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002178 return this;
2179 }
2180
2181 /**
Joe Onoratocb109a02011-01-18 17:57:41 -08002182 * Set the large number at the right-hand side of the notification. This is
2183 * equivalent to setContentInfo, although it might show the number in a different
2184 * font size for readability.
2185 */
Joe Onorato8595a3d2010-11-19 18:12:07 -08002186 public Builder setNumber(int number) {
2187 mNumber = number;
2188 return this;
2189 }
2190
Joe Onoratocb109a02011-01-18 17:57:41 -08002191 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002192 * A small piece of additional information pertaining to this notification.
2193 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002194 * The platform template will draw this on the last line of the notification, at the far
2195 * right (to the right of a smallIcon if it has been placed there).
Joe Onoratocb109a02011-01-18 17:57:41 -08002196 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002197 public Builder setContentInfo(CharSequence info) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04002198 mContentInfo = safeCharSequence(info);
Joe Onorato46439ce2010-11-19 13:56:21 -08002199 return this;
2200 }
2201
Joe Onoratocb109a02011-01-18 17:57:41 -08002202 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002203 * Set the progress this notification represents.
2204 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002205 * The platform template will represent this using a {@link ProgressBar}.
Jeff Sharkey1c400132011-08-05 14:50:13 -07002206 */
2207 public Builder setProgress(int max, int progress, boolean indeterminate) {
2208 mProgressMax = max;
2209 mProgress = progress;
2210 mProgressIndeterminate = indeterminate;
2211 return this;
2212 }
2213
2214 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002215 * Supply a custom RemoteViews to use instead of the platform template.
2216 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002217 * @see Notification#contentView
Joe Onoratocb109a02011-01-18 17:57:41 -08002218 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002219 public Builder setContent(RemoteViews views) {
2220 mContentView = views;
2221 return this;
2222 }
2223
Joe Onoratocb109a02011-01-18 17:57:41 -08002224 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002225 * Supply a {@link PendingIntent} to be sent when the notification is clicked.
2226 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002227 * As of {@link android.os.Build.VERSION_CODES#HONEYCOMB}, if this field is unset and you
2228 * have specified a custom RemoteViews with {@link #setContent(RemoteViews)}, you can use
2229 * {@link RemoteViews#setOnClickPendingIntent RemoteViews.setOnClickPendingIntent(int,PendingIntent)}
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002230 * to assign PendingIntents to individual views in that custom layout (i.e., to create
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002231 * clickable buttons inside the notification view).
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002232 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002233 * @see Notification#contentIntent Notification.contentIntent
Joe Onoratocb109a02011-01-18 17:57:41 -08002234 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002235 public Builder setContentIntent(PendingIntent intent) {
2236 mContentIntent = intent;
2237 return this;
2238 }
2239
Joe Onoratocb109a02011-01-18 17:57:41 -08002240 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002241 * Supply a {@link PendingIntent} to send when the notification is cleared explicitly by the user.
2242 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002243 * @see Notification#deleteIntent
Joe Onoratocb109a02011-01-18 17:57:41 -08002244 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002245 public Builder setDeleteIntent(PendingIntent intent) {
2246 mDeleteIntent = intent;
2247 return this;
2248 }
2249
Joe Onoratocb109a02011-01-18 17:57:41 -08002250 /**
2251 * An intent to launch instead of posting the notification to the status bar.
2252 * Only for use with extremely high-priority notifications demanding the user's
2253 * <strong>immediate</strong> attention, such as an incoming phone call or
2254 * alarm clock that the user has explicitly set to a particular time.
2255 * If this facility is used for something else, please give the user an option
2256 * to turn it off and use a normal notification, as this can be extremely
2257 * disruptive.
2258 *
Chris Wren47c20a12014-06-18 17:27:29 -04002259 * <p>
2260 * The system UI may choose to display a heads-up notification, instead of
2261 * launching this intent, while the user is using the device.
2262 * </p>
2263 *
Joe Onoratocb109a02011-01-18 17:57:41 -08002264 * @param intent The pending intent to launch.
2265 * @param highPriority Passing true will cause this notification to be sent
2266 * even if other notifications are suppressed.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002267 *
2268 * @see Notification#fullScreenIntent
Joe Onoratocb109a02011-01-18 17:57:41 -08002269 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002270 public Builder setFullScreenIntent(PendingIntent intent, boolean highPriority) {
2271 mFullScreenIntent = intent;
2272 setFlag(FLAG_HIGH_PRIORITY, highPriority);
2273 return this;
2274 }
2275
Joe Onoratocb109a02011-01-18 17:57:41 -08002276 /**
Dan Sandler5fcdf6e2014-07-18 11:31:15 -04002277 * Set the "ticker" text which is sent to accessibility services.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002278 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002279 * @see Notification#tickerText
Joe Onoratocb109a02011-01-18 17:57:41 -08002280 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002281 public Builder setTicker(CharSequence tickerText) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04002282 mTickerText = safeCharSequence(tickerText);
Joe Onorato46439ce2010-11-19 13:56:21 -08002283 return this;
2284 }
2285
Joe Onoratocb109a02011-01-18 17:57:41 -08002286 /**
Dan Sandler5fcdf6e2014-07-18 11:31:15 -04002287 * Obsolete version of {@link #setTicker(CharSequence)}.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002288 *
Joe Onoratocb109a02011-01-18 17:57:41 -08002289 */
Dan Sandler5fcdf6e2014-07-18 11:31:15 -04002290 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -08002291 public Builder setTicker(CharSequence tickerText, RemoteViews views) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04002292 mTickerText = safeCharSequence(tickerText);
Dan Sandler5fcdf6e2014-07-18 11:31:15 -04002293 mTickerView = views; // we'll save it for you anyway
Joe Onorato46439ce2010-11-19 13:56:21 -08002294 return this;
2295 }
2296
Joe Onoratocb109a02011-01-18 17:57:41 -08002297 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002298 * Add a large icon to the notification (and the ticker on some devices).
2299 *
2300 * In the platform template, this image will be shown on the left of the notification view
2301 * in place of the {@link #setSmallIcon(int) small icon} (which will move to the right side).
2302 *
2303 * @see Notification#largeIcon
Joe Onoratocb109a02011-01-18 17:57:41 -08002304 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002305 public Builder setLargeIcon(Bitmap icon) {
2306 mLargeIcon = icon;
2307 return this;
2308 }
2309
Joe Onoratocb109a02011-01-18 17:57:41 -08002310 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002311 * Set the sound to play.
2312 *
John Spurlockc0650f022014-07-19 13:22:39 -04002313 * It will be played using the {@link #AUDIO_ATTRIBUTES_DEFAULT default audio attributes}
2314 * for notifications.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002315 *
Chris Wren47c20a12014-06-18 17:27:29 -04002316 * <p>
2317 * A notification that is noisy is more likely to be presented as a heads-up notification.
2318 * </p>
2319 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002320 * @see Notification#sound
Joe Onoratocb109a02011-01-18 17:57:41 -08002321 */
Joe Onorato52f80cd2010-11-21 15:34:48 -08002322 public Builder setSound(Uri sound) {
2323 mSound = sound;
John Spurlockc0650f022014-07-19 13:22:39 -04002324 mAudioAttributes = AUDIO_ATTRIBUTES_DEFAULT;
Joe Onorato52f80cd2010-11-21 15:34:48 -08002325 return this;
2326 }
2327
Joe Onoratocb109a02011-01-18 17:57:41 -08002328 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002329 * Set the sound to play, along with a specific stream on which to play it.
Joe Onoratocb109a02011-01-18 17:57:41 -08002330 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002331 * See {@link android.media.AudioManager} for the <code>STREAM_</code> constants.
2332 *
Chris Wren47c20a12014-06-18 17:27:29 -04002333 * <p>
2334 * A notification that is noisy is more likely to be presented as a heads-up notification.
2335 * </p>
John Spurlockc0650f022014-07-19 13:22:39 -04002336 * @deprecated use {@link #setSound(Uri, AudioAttributes)} instead.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002337 * @see Notification#sound
Joe Onoratocb109a02011-01-18 17:57:41 -08002338 */
Jean-Michel Trivi81f871e2014-08-06 16:32:38 -07002339 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -08002340 public Builder setSound(Uri sound, int streamType) {
2341 mSound = sound;
2342 mAudioStreamType = streamType;
2343 return this;
2344 }
2345
Joe Onoratocb109a02011-01-18 17:57:41 -08002346 /**
John Spurlockc0650f022014-07-19 13:22:39 -04002347 * Set the sound to play, along with specific {@link AudioAttributes audio attributes} to
2348 * use during playback.
2349 *
2350 * <p>
2351 * A notification that is noisy is more likely to be presented as a heads-up notification.
2352 * </p>
2353 *
2354 * @see Notification#sound
2355 */
2356 public Builder setSound(Uri sound, AudioAttributes audioAttributes) {
2357 mSound = sound;
2358 mAudioAttributes = audioAttributes;
2359 return this;
2360 }
2361
2362 /**
Joe Onoratocb109a02011-01-18 17:57:41 -08002363 * Set the vibration pattern to use.
2364 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002365 * See {@link android.os.Vibrator#vibrate(long[], int)} for a discussion of the
2366 * <code>pattern</code> parameter.
2367 *
Chris Wren47c20a12014-06-18 17:27:29 -04002368 * <p>
2369 * A notification that vibrates is more likely to be presented as a heads-up notification.
2370 * </p>
2371 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002372 * @see Notification#vibrate
Joe Onoratocb109a02011-01-18 17:57:41 -08002373 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002374 public Builder setVibrate(long[] pattern) {
2375 mVibrate = pattern;
2376 return this;
2377 }
2378
Joe Onoratocb109a02011-01-18 17:57:41 -08002379 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002380 * Set the desired color for the indicator LED on the device, as well as the
2381 * blink duty cycle (specified in milliseconds).
2382 *
2383
2384 * Not all devices will honor all (or even any) of these values.
2385 *
2386
2387 * @see Notification#ledARGB
2388 * @see Notification#ledOnMS
2389 * @see Notification#ledOffMS
Joe Onoratocb109a02011-01-18 17:57:41 -08002390 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002391 public Builder setLights(int argb, int onMs, int offMs) {
2392 mLedArgb = argb;
2393 mLedOnMs = onMs;
2394 mLedOffMs = offMs;
Joe Onorato46439ce2010-11-19 13:56:21 -08002395 return this;
2396 }
2397
Joe Onoratocb109a02011-01-18 17:57:41 -08002398 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002399 * Set whether this is an "ongoing" notification.
Joe Onoratocb109a02011-01-18 17:57:41 -08002400 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002401
2402 * Ongoing notifications cannot be dismissed by the user, so your application or service
2403 * must take care of canceling them.
2404 *
2405
2406 * They are typically used to indicate a background task that the user is actively engaged
2407 * with (e.g., playing music) or is pending in some way and therefore occupying the device
2408 * (e.g., a file download, sync operation, active network connection).
2409 *
2410
2411 * @see Notification#FLAG_ONGOING_EVENT
2412 * @see Service#setForeground(boolean)
Joe Onoratocb109a02011-01-18 17:57:41 -08002413 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002414 public Builder setOngoing(boolean ongoing) {
2415 setFlag(FLAG_ONGOING_EVENT, ongoing);
2416 return this;
2417 }
2418
Joe Onoratocb109a02011-01-18 17:57:41 -08002419 /**
2420 * Set this flag if you would only like the sound, vibrate
2421 * and ticker to be played if the notification is not already showing.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002422 *
2423 * @see Notification#FLAG_ONLY_ALERT_ONCE
Joe Onoratocb109a02011-01-18 17:57:41 -08002424 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002425 public Builder setOnlyAlertOnce(boolean onlyAlertOnce) {
2426 setFlag(FLAG_ONLY_ALERT_ONCE, onlyAlertOnce);
2427 return this;
2428 }
2429
Joe Onoratocb109a02011-01-18 17:57:41 -08002430 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002431 * Make this notification automatically dismissed when the user touches it. The
2432 * PendingIntent set with {@link #setDeleteIntent} will be sent when this happens.
2433 *
2434 * @see Notification#FLAG_AUTO_CANCEL
Joe Onoratocb109a02011-01-18 17:57:41 -08002435 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002436 public Builder setAutoCancel(boolean autoCancel) {
Joe Onorato281d83f2011-01-04 17:13:10 -08002437 setFlag(FLAG_AUTO_CANCEL, autoCancel);
Joe Onorato46439ce2010-11-19 13:56:21 -08002438 return this;
2439 }
2440
Joe Onoratocb109a02011-01-18 17:57:41 -08002441 /**
Griff Hazendfcb0802014-02-11 12:00:00 -08002442 * Set whether or not this notification should not bridge to other devices.
2443 *
2444 * <p>Some notifications can be bridged to other devices for remote display.
2445 * This hint can be set to recommend this notification not be bridged.
2446 */
2447 public Builder setLocalOnly(boolean localOnly) {
2448 setFlag(FLAG_LOCAL_ONLY, localOnly);
2449 return this;
2450 }
2451
2452 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002453 * Set which notification properties will be inherited from system defaults.
Joe Onoratocb109a02011-01-18 17:57:41 -08002454 * <p>
2455 * The value should be one or more of the following fields combined with
2456 * bitwise-or:
2457 * {@link #DEFAULT_SOUND}, {@link #DEFAULT_VIBRATE}, {@link #DEFAULT_LIGHTS}.
2458 * <p>
2459 * For all default values, use {@link #DEFAULT_ALL}.
2460 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002461 public Builder setDefaults(int defaults) {
2462 mDefaults = defaults;
2463 return this;
2464 }
2465
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002466 /**
2467 * Set the priority of this notification.
2468 *
2469 * @see Notification#priority
2470 */
Tor Norbyed9273d62013-05-30 15:59:53 -07002471 public Builder setPriority(@Priority int pri) {
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002472 mPriority = pri;
2473 return this;
2474 }
Joe Malin8d40d042012-11-05 11:36:40 -08002475
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002476 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -04002477 * Set the notification category.
Joe Malin8d40d042012-11-05 11:36:40 -08002478 *
John Spurlockfd7f1e02014-03-18 16:41:57 -04002479 * @see Notification#category
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002480 */
John Spurlockfd7f1e02014-03-18 16:41:57 -04002481 public Builder setCategory(String category) {
2482 mCategory = category;
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002483 return this;
2484 }
2485
2486 /**
Chris Wrendde75302014-03-26 17:24:15 -04002487 * Add a person that is relevant to this notification.
2488 *
Chris Wrene6c48932014-09-29 17:19:27 -04002489 * <P>
2490 * Depending on user preferences, this annotation may allow the notification to pass
2491 * through interruption filters, and to appear more prominently in the user interface.
2492 * </P>
2493 *
2494 * <P>
2495 * The person should be specified by the {@code String} representation of a
2496 * {@link android.provider.ContactsContract.Contacts#CONTENT_LOOKUP_URI}.
2497 * </P>
2498 *
2499 * <P>The system will also attempt to resolve {@code mailto:} and {@code tel:} schema
2500 * URIs. The path part of these URIs must exist in the contacts database, in the
2501 * appropriate column, or the reference will be discarded as invalid. Telephone schema
2502 * URIs will be resolved by {@link android.provider.ContactsContract.PhoneLookup}.
2503 * </P>
2504 *
2505 * @param uri A URI for the person.
Chris Wrendde75302014-03-26 17:24:15 -04002506 * @see Notification#EXTRA_PEOPLE
2507 */
Chris Wrene6c48932014-09-29 17:19:27 -04002508 public Builder addPerson(String uri) {
2509 mPeople.add(uri);
Chris Wrendde75302014-03-26 17:24:15 -04002510 return this;
2511 }
2512
2513 /**
Griff Hazen5cadc3b2014-05-20 09:55:39 -07002514 * Set this notification to be part of a group of notifications sharing the same key.
2515 * Grouped notifications may display in a cluster or stack on devices which
2516 * support such rendering.
2517 *
2518 * <p>To make this notification the summary for its group, also call
2519 * {@link #setGroupSummary}. A sort order can be specified for group members by using
2520 * {@link #setSortKey}.
2521 * @param groupKey The group key of the group.
2522 * @return this object for method chaining
2523 */
2524 public Builder setGroup(String groupKey) {
2525 mGroupKey = groupKey;
2526 return this;
2527 }
2528
2529 /**
2530 * Set this notification to be the group summary for a group of notifications.
2531 * Grouped notifications may display in a cluster or stack on devices which
2532 * support such rendering. Requires a group key also be set using {@link #setGroup}.
2533 * @param isGroupSummary Whether this notification should be a group summary.
2534 * @return this object for method chaining
2535 */
2536 public Builder setGroupSummary(boolean isGroupSummary) {
2537 setFlag(FLAG_GROUP_SUMMARY, isGroupSummary);
2538 return this;
2539 }
2540
2541 /**
2542 * Set a sort key that orders this notification among other notifications from the
2543 * same package. This can be useful if an external sort was already applied and an app
2544 * would like to preserve this. Notifications will be sorted lexicographically using this
2545 * value, although providing different priorities in addition to providing sort key may
2546 * cause this value to be ignored.
2547 *
2548 * <p>This sort key can also be used to order members of a notification group. See
Griff Hazen9e1379f2014-05-20 12:50:51 -07002549 * {@link #setGroup}.
Griff Hazen5cadc3b2014-05-20 09:55:39 -07002550 *
2551 * @see String#compareTo(String)
2552 */
2553 public Builder setSortKey(String sortKey) {
2554 mSortKey = sortKey;
2555 return this;
2556 }
2557
2558 /**
Griff Hazen720042b2014-02-24 15:46:56 -08002559 * Merge additional metadata into this notification.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002560 *
Griff Hazen720042b2014-02-24 15:46:56 -08002561 * <p>Values within the Bundle will replace existing extras values in this Builder.
2562 *
2563 * @see Notification#extras
2564 */
Griff Hazen959591e2014-05-15 22:26:18 -07002565 public Builder addExtras(Bundle extras) {
2566 if (extras != null) {
2567 if (mExtras == null) {
2568 mExtras = new Bundle(extras);
2569 } else {
2570 mExtras.putAll(extras);
2571 }
Griff Hazen720042b2014-02-24 15:46:56 -08002572 }
2573 return this;
2574 }
2575
2576 /**
2577 * Set metadata for this notification.
2578 *
2579 * <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 -04002580 * current contents are copied into the Notification each time {@link #build()} is
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002581 * called.
2582 *
Griff Hazen720042b2014-02-24 15:46:56 -08002583 * <p>Replaces any existing extras values with those from the provided Bundle.
2584 * Use {@link #addExtras} to merge in metadata instead.
2585 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002586 * @see Notification#extras
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002587 */
Griff Hazen959591e2014-05-15 22:26:18 -07002588 public Builder setExtras(Bundle extras) {
2589 mExtras = extras;
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002590 return this;
2591 }
2592
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002593 /**
Griff Hazen720042b2014-02-24 15:46:56 -08002594 * Get the current metadata Bundle used by this notification Builder.
2595 *
2596 * <p>The returned Bundle is shared with this Builder.
2597 *
2598 * <p>The current contents of this Bundle are copied into the Notification each time
2599 * {@link #build()} is called.
2600 *
2601 * @see Notification#extras
2602 */
2603 public Bundle getExtras() {
2604 if (mExtras == null) {
2605 mExtras = new Bundle();
2606 }
2607 return mExtras;
2608 }
2609
2610 /**
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002611 * Add an action to this notification. Actions are typically displayed by
2612 * the system as a button adjacent to the notification content.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04002613 * <p>
2614 * Every action must have an icon (32dp square and matching the
2615 * <a href="{@docRoot}design/style/iconography.html#action-bar">Holo
2616 * Dark action bar</a> visual style), a textual label, and a {@link PendingIntent}.
2617 * <p>
2618 * A notification in its expanded form can display up to 3 actions, from left to right in
2619 * the order they were added. Actions will not be displayed when the notification is
2620 * collapsed, however, so be sure that any essential functions may be accessed by the user
2621 * in some other way (for example, in the Activity pointed to by {@link #contentIntent}).
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002622 *
2623 * @param icon Resource ID of a drawable that represents the action.
2624 * @param title Text describing the action.
2625 * @param intent PendingIntent to be fired when the action is invoked.
2626 */
2627 public Builder addAction(int icon, CharSequence title, PendingIntent intent) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04002628 mActions.add(new Action(icon, safeCharSequence(title), intent));
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002629 return this;
2630 }
2631
Chris Wrenfbd96ba2012-05-01 12:03:58 -04002632 /**
Griff Hazen959591e2014-05-15 22:26:18 -07002633 * Add an action to this notification. Actions are typically displayed by
2634 * the system as a button adjacent to the notification content.
2635 * <p>
2636 * Every action must have an icon (32dp square and matching the
2637 * <a href="{@docRoot}design/style/iconography.html#action-bar">Holo
2638 * Dark action bar</a> visual style), a textual label, and a {@link PendingIntent}.
2639 * <p>
2640 * A notification in its expanded form can display up to 3 actions, from left to right in
2641 * the order they were added. Actions will not be displayed when the notification is
2642 * collapsed, however, so be sure that any essential functions may be accessed by the user
2643 * in some other way (for example, in the Activity pointed to by {@link #contentIntent}).
2644 *
2645 * @param action The action to add.
2646 */
2647 public Builder addAction(Action action) {
2648 mActions.add(action);
2649 return this;
2650 }
2651
2652 /**
Chris Wrenfbd96ba2012-05-01 12:03:58 -04002653 * Add a rich notification style to be applied at build time.
2654 *
2655 * @param style Object responsible for modifying the notification style.
2656 */
2657 public Builder setStyle(Style style) {
2658 if (mStyle != style) {
2659 mStyle = style;
Daniel Sandlerc08dea22012-06-28 08:35:24 -07002660 if (mStyle != null) {
2661 mStyle.setBuilder(this);
2662 }
Chris Wrenfbd96ba2012-05-01 12:03:58 -04002663 }
2664 return this;
2665 }
2666
Dan Sandler0bf2ed82013-12-21 23:33:41 -06002667 /**
2668 * Specify the value of {@link #visibility}.
Griff Hazenb720abe2014-05-20 13:15:30 -07002669 *
Dan Sandler0bf2ed82013-12-21 23:33:41 -06002670 * @param visibility One of {@link #VISIBILITY_PRIVATE} (the default),
2671 * {@link #VISIBILITY_SECRET}, or {@link #VISIBILITY_PUBLIC}.
2672 *
2673 * @return The same Builder.
2674 */
2675 public Builder setVisibility(int visibility) {
2676 mVisibility = visibility;
2677 return this;
2678 }
2679
2680 /**
2681 * Supply a replacement Notification whose contents should be shown in insecure contexts
2682 * (i.e. atop the secure lockscreen). See {@link #visibility} and {@link #VISIBILITY_PUBLIC}.
2683 * @param n A replacement notification, presumably with some or all info redacted.
2684 * @return The same Builder.
2685 */
2686 public Builder setPublicVersion(Notification n) {
2687 mPublicVersion = n;
2688 return this;
2689 }
2690
Griff Hazenb720abe2014-05-20 13:15:30 -07002691 /**
Griff Hazen5cadc3b2014-05-20 09:55:39 -07002692 * Apply an extender to this notification builder. Extenders may be used to add
2693 * metadata or change options on this builder.
2694 */
Griff Hazen61a9e862014-05-22 16:05:19 -07002695 public Builder extend(Extender extender) {
2696 extender.extend(this);
Griff Hazen5cadc3b2014-05-20 09:55:39 -07002697 return this;
2698 }
2699
Joe Onorato46439ce2010-11-19 13:56:21 -08002700 private void setFlag(int mask, boolean value) {
2701 if (value) {
2702 mFlags |= mask;
2703 } else {
2704 mFlags &= ~mask;
2705 }
2706 }
2707
Dan Sandler26e81cf2014-05-06 10:01:27 -04002708 /**
2709 * Sets {@link Notification#color}.
2710 *
2711 * @param argb The accent color to use
2712 *
2713 * @return The same Builder.
2714 */
2715 public Builder setColor(int argb) {
2716 mColor = argb;
2717 return this;
2718 }
2719
Jorim Jaggid05aa3e2014-08-28 17:52:27 +02002720 private Drawable getProfileBadgeDrawable() {
Christoph Studer7ac80e62014-08-04 16:01:57 +02002721 // Note: This assumes that the current user can read the profile badge of the
2722 // originating user.
Svetoslavc7d62f02014-09-04 15:39:54 -07002723 return mContext.getPackageManager().getUserBadgeForDensity(
Kenny Guy8942bcd2014-09-08 21:09:47 +01002724 new UserHandle(mOriginatingUserId), 0);
Jorim Jaggid05aa3e2014-08-28 17:52:27 +02002725 }
2726
2727 private Bitmap getProfileBadge() {
2728 Drawable badge = getProfileBadgeDrawable();
Kenny Guy8a0101b2014-05-08 23:34:12 +01002729 if (badge == null) {
2730 return null;
2731 }
Jorim Jaggid05aa3e2014-08-28 17:52:27 +02002732 final int size = mContext.getResources().getDimensionPixelSize(
2733 R.dimen.notification_badge_size);
2734 Bitmap bitmap = Bitmap.createBitmap(size, size, Bitmap.Config.ARGB_8888);
Kenny Guy8a0101b2014-05-08 23:34:12 +01002735 Canvas canvas = new Canvas(bitmap);
Jorim Jaggid05aa3e2014-08-28 17:52:27 +02002736 badge.setBounds(0, 0, size, size);
Kenny Guy8a0101b2014-05-08 23:34:12 +01002737 badge.draw(canvas);
2738 return bitmap;
2739 }
2740
Kenny Guy98193ea2014-07-24 19:54:37 +01002741 private boolean addProfileBadge(RemoteViews contentView, int resId) {
2742 Bitmap profileBadge = getProfileBadge();
2743
2744 contentView.setViewVisibility(R.id.profile_badge_large_template, View.GONE);
2745 contentView.setViewVisibility(R.id.profile_badge_line2, View.GONE);
2746 contentView.setViewVisibility(R.id.profile_badge_line3, View.GONE);
2747
2748 if (profileBadge != null) {
2749 contentView.setImageViewBitmap(resId, profileBadge);
2750 contentView.setViewVisibility(resId, View.VISIBLE);
2751
2752 // Make sure Line 3 is visible. As badge will be here if there
2753 // is no text to display.
2754 if (resId == R.id.profile_badge_line3) {
2755 contentView.setViewVisibility(R.id.line3, View.VISIBLE);
2756 }
2757 return true;
2758 }
2759 return false;
2760 }
2761
Jorim Jaggid05aa3e2014-08-28 17:52:27 +02002762 private void shrinkLine3Text(RemoteViews contentView) {
2763 float subTextSize = mContext.getResources().getDimensionPixelSize(
2764 R.dimen.notification_subtext_size);
2765 contentView.setTextViewTextSize(R.id.text, TypedValue.COMPLEX_UNIT_PX, subTextSize);
2766 }
2767
Christoph Studerfe718432014-09-01 18:21:18 +02002768 private void unshrinkLine3Text(RemoteViews contentView) {
2769 float regularTextSize = mContext.getResources().getDimensionPixelSize(
2770 com.android.internal.R.dimen.notification_text_size);
2771 contentView.setTextViewTextSize(R.id.text, TypedValue.COMPLEX_UNIT_PX, regularTextSize);
2772 }
2773
2774 private void resetStandardTemplate(RemoteViews contentView) {
2775 removeLargeIconBackground(contentView);
2776 contentView.setViewPadding(R.id.icon, 0, 0, 0, 0);
2777 contentView.setImageViewResource(R.id.icon, 0);
2778 contentView.setInt(R.id.icon, "setBackgroundResource", 0);
2779 contentView.setViewVisibility(R.id.right_icon, View.GONE);
2780 contentView.setInt(R.id.right_icon, "setBackgroundResource", 0);
2781 contentView.setImageViewResource(R.id.right_icon, 0);
2782 contentView.setImageViewResource(R.id.icon, 0);
2783 contentView.setTextViewText(R.id.title, null);
2784 contentView.setTextViewText(R.id.text, null);
2785 unshrinkLine3Text(contentView);
2786 contentView.setTextViewText(R.id.text2, null);
2787 contentView.setViewVisibility(R.id.text2, View.GONE);
2788 contentView.setViewVisibility(R.id.info, View.GONE);
2789 contentView.setViewVisibility(R.id.time, View.GONE);
2790 contentView.setViewVisibility(R.id.line3, View.GONE);
2791 contentView.setViewVisibility(R.id.overflow_divider, View.GONE);
2792 contentView.setViewVisibility(R.id.progress, View.GONE);
Christoph Studerca1db712014-09-10 17:31:33 +02002793 contentView.setViewVisibility(R.id.chronometer, View.GONE);
2794 contentView.setViewVisibility(R.id.time, View.GONE);
Christoph Studerfe718432014-09-01 18:21:18 +02002795 }
2796
Jorim Jaggi445d3c02014-08-19 22:33:42 +02002797 private RemoteViews applyStandardTemplate(int resId) {
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02002798 return applyStandardTemplate(resId, true /* hasProgress */);
2799 }
2800
2801 /**
2802 * @param hasProgress whether the progress bar should be shown and set
2803 */
2804 private RemoteViews applyStandardTemplate(int resId, boolean hasProgress) {
Kenny Guy77320062014-08-27 21:37:15 +01002805 RemoteViews contentView = new BuilderRemoteViews(mContext.getApplicationInfo(), resId);
Dan Sandler539aad42014-08-04 00:43:39 -04002806
Christoph Studerfe718432014-09-01 18:21:18 +02002807 resetStandardTemplate(contentView);
2808
Daniel Sandler9f7936a2012-05-21 16:14:28 -04002809 boolean showLine3 = false;
2810 boolean showLine2 = false;
Kenny Guy98193ea2014-07-24 19:54:37 +01002811 boolean contentTextInLine2 = false;
Dan Sandler190d58d2014-05-15 09:33:39 -04002812
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01002813 if (mLargeIcon != null) {
2814 contentView.setImageViewBitmap(R.id.icon, mLargeIcon);
Christoph Studer239f8352014-08-25 15:13:18 +02002815 processLargeLegacyIcon(mLargeIcon, contentView);
Dan Sandler190d58d2014-05-15 09:33:39 -04002816 contentView.setImageViewResource(R.id.right_icon, mSmallIcon);
2817 contentView.setViewVisibility(R.id.right_icon, View.VISIBLE);
2818 processSmallRightIcon(mSmallIcon, contentView);
2819 } else { // small icon at left
2820 contentView.setImageViewResource(R.id.icon, mSmallIcon);
2821 contentView.setViewVisibility(R.id.icon, View.VISIBLE);
2822 processSmallIconAsLarge(mSmallIcon, contentView);
Joe Onorato561d3852010-11-20 18:09:34 -08002823 }
2824 if (mContentTitle != null) {
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01002825 contentView.setTextViewText(R.id.title, processLegacyText(mContentTitle));
Joe Onorato561d3852010-11-20 18:09:34 -08002826 }
2827 if (mContentText != null) {
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01002828 contentView.setTextViewText(R.id.text, processLegacyText(mContentText));
Daniel Sandler9f7936a2012-05-21 16:14:28 -04002829 showLine3 = true;
Joe Onorato561d3852010-11-20 18:09:34 -08002830 }
2831 if (mContentInfo != null) {
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01002832 contentView.setTextViewText(R.id.info, processLegacyText(mContentInfo));
Jeff Sharkey1c400132011-08-05 14:50:13 -07002833 contentView.setViewVisibility(R.id.info, View.VISIBLE);
Daniel Sandler9f7936a2012-05-21 16:14:28 -04002834 showLine3 = true;
Joe Onorato561d3852010-11-20 18:09:34 -08002835 } else if (mNumber > 0) {
Daniel Sandlerebce0112011-06-16 16:44:51 -04002836 final int tooBig = mContext.getResources().getInteger(
2837 R.integer.status_bar_notification_info_maxnum);
2838 if (mNumber > tooBig) {
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01002839 contentView.setTextViewText(R.id.info, processLegacyText(
2840 mContext.getResources().getString(
2841 R.string.status_bar_notification_info_overflow)));
Joe Onorato059a2f82011-01-04 10:27:01 -08002842 } else {
2843 NumberFormat f = NumberFormat.getIntegerInstance();
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01002844 contentView.setTextViewText(R.id.info, processLegacyText(f.format(mNumber)));
Joe Onorato059a2f82011-01-04 10:27:01 -08002845 }
Jeff Sharkey1c400132011-08-05 14:50:13 -07002846 contentView.setViewVisibility(R.id.info, View.VISIBLE);
Daniel Sandler9f7936a2012-05-21 16:14:28 -04002847 showLine3 = true;
Joe Onorato561d3852010-11-20 18:09:34 -08002848 } else {
2849 contentView.setViewVisibility(R.id.info, View.GONE);
2850 }
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002851
Daniel Sandler9f7936a2012-05-21 16:14:28 -04002852 // Need to show three lines?
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002853 if (mSubText != null) {
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01002854 contentView.setTextViewText(R.id.text, processLegacyText(mSubText));
Daniel Sandler9f7936a2012-05-21 16:14:28 -04002855 if (mContentText != null) {
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01002856 contentView.setTextViewText(R.id.text2, processLegacyText(mContentText));
Daniel Sandler9f7936a2012-05-21 16:14:28 -04002857 contentView.setViewVisibility(R.id.text2, View.VISIBLE);
2858 showLine2 = true;
Kenny Guy98193ea2014-07-24 19:54:37 +01002859 contentTextInLine2 = true;
Daniel Sandler9f7936a2012-05-21 16:14:28 -04002860 } else {
2861 contentView.setViewVisibility(R.id.text2, View.GONE);
2862 }
Jeff Sharkey1c400132011-08-05 14:50:13 -07002863 } else {
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002864 contentView.setViewVisibility(R.id.text2, View.GONE);
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02002865 if (hasProgress && (mProgressMax != 0 || mProgressIndeterminate)) {
Christoph Studeraca4b252014-09-09 15:58:41 +02002866 contentView.setViewVisibility(R.id.progress, View.VISIBLE);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002867 contentView.setProgressBar(
2868 R.id.progress, mProgressMax, mProgress, mProgressIndeterminate);
Jorim Jaggief72a192014-08-26 21:57:46 +02002869 contentView.setProgressBackgroundTintList(
2870 R.id.progress, ColorStateList.valueOf(mContext.getResources().getColor(
2871 R.color.notification_progress_background_color)));
2872 if (mColor != COLOR_DEFAULT) {
2873 ColorStateList colorStateList = ColorStateList.valueOf(mColor);
2874 contentView.setProgressTintList(R.id.progress, colorStateList);
2875 contentView.setProgressIndeterminateTintList(R.id.progress, colorStateList);
2876 }
Daniel Sandler9f7936a2012-05-21 16:14:28 -04002877 showLine2 = true;
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002878 } else {
2879 contentView.setViewVisibility(R.id.progress, View.GONE);
2880 }
Jeff Sharkey1c400132011-08-05 14:50:13 -07002881 }
Daniel Sandler9f7936a2012-05-21 16:14:28 -04002882 if (showLine2) {
Jorim Jaggi445d3c02014-08-19 22:33:42 +02002883
2884 // need to shrink all the type to make sure everything fits
Jorim Jaggid05aa3e2014-08-28 17:52:27 +02002885 shrinkLine3Text(contentView);
Daniel Sandler9f7936a2012-05-21 16:14:28 -04002886 }
2887
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02002888 if (showsTimeOrChronometer()) {
Daniel Sandlera2985ed2012-04-03 16:42:00 -04002889 if (mUseChronometer) {
2890 contentView.setViewVisibility(R.id.chronometer, View.VISIBLE);
2891 contentView.setLong(R.id.chronometer, "setBase",
2892 mWhen + (SystemClock.elapsedRealtime() - System.currentTimeMillis()));
2893 contentView.setBoolean(R.id.chronometer, "setStarted", true);
2894 } else {
2895 contentView.setViewVisibility(R.id.time, View.VISIBLE);
2896 contentView.setLong(R.id.time, "setTime", mWhen);
2897 }
Joe Onorato561d3852010-11-20 18:09:34 -08002898 }
Daniel Sandler0c890492012-09-12 17:23:10 -07002899
Jorim Jaggi445d3c02014-08-19 22:33:42 +02002900 // Adjust padding depending on line count and font size.
2901 contentView.setViewPadding(R.id.line1, 0, calculateTopPadding(mContext,
Jorim Jaggid05aa3e2014-08-28 17:52:27 +02002902 mHasThreeLines, mContext.getResources().getConfiguration().fontScale),
Jorim Jaggi445d3c02014-08-19 22:33:42 +02002903 0, 0);
2904
Kenny Guy98193ea2014-07-24 19:54:37 +01002905 // We want to add badge to first line of text.
2906 boolean addedBadge = addProfileBadge(contentView,
2907 contentTextInLine2 ? R.id.profile_badge_line2 : R.id.profile_badge_line3);
2908 // If we added the badge to line 3 then we should show line 3.
2909 if (addedBadge && !contentTextInLine2) {
2910 showLine3 = true;
2911 }
2912
2913 // Note getStandardView may hide line 3 again.
Daniel Sandler9f7936a2012-05-21 16:14:28 -04002914 contentView.setViewVisibility(R.id.line3, showLine3 ? View.VISIBLE : View.GONE);
Daniel Sandler6387d2f2012-05-22 13:44:09 -04002915 contentView.setViewVisibility(R.id.overflow_divider, showLine3 ? View.VISIBLE : View.GONE);
Joe Onorato561d3852010-11-20 18:09:34 -08002916 return contentView;
2917 }
2918
Jorim Jaggi445d3c02014-08-19 22:33:42 +02002919 /**
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02002920 * @return true if the built notification will show the time or the chronometer; false
2921 * otherwise
2922 */
2923 private boolean showsTimeOrChronometer() {
2924 return mWhen != 0 && mShowWhen;
2925 }
2926
2927 /**
Jorim Jaggi445d3c02014-08-19 22:33:42 +02002928 * Logic to find out whether the notification is going to have three lines in the contracted
2929 * layout. This is used to adjust the top padding.
2930 *
2931 * @return true if the notification is going to have three lines; false if the notification
2932 * is going to have one or two lines
2933 */
2934 private boolean hasThreeLines() {
Jorim Jaggid05aa3e2014-08-28 17:52:27 +02002935 boolean contentTextInLine2 = mSubText != null && mContentText != null;
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02002936 boolean hasProgress = mStyle == null || mStyle.hasProgress();
Jorim Jaggid05aa3e2014-08-28 17:52:27 +02002937 // If we have content text in line 2, badge goes into line 2, or line 3 otherwise
2938 boolean badgeInLine3 = getProfileBadgeDrawable() != null && !contentTextInLine2;
2939 boolean hasLine3 = mContentText != null || mContentInfo != null || mNumber > 0
2940 || badgeInLine3;
Jorim Jaggi445d3c02014-08-19 22:33:42 +02002941 boolean hasLine2 = (mSubText != null && mContentText != null) ||
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02002942 (hasProgress && mSubText == null
2943 && (mProgressMax != 0 || mProgressIndeterminate));
Jorim Jaggi445d3c02014-08-19 22:33:42 +02002944 return hasLine2 && hasLine3;
2945 }
2946
2947 /**
2948 * @hide
2949 */
2950 public static int calculateTopPadding(Context ctx, boolean hasThreeLines,
2951 float fontScale) {
2952 int padding = ctx.getResources().getDimensionPixelSize(hasThreeLines
2953 ? R.dimen.notification_top_pad_narrow
2954 : R.dimen.notification_top_pad);
2955 int largePadding = ctx.getResources().getDimensionPixelSize(hasThreeLines
2956 ? R.dimen.notification_top_pad_large_text_narrow
2957 : R.dimen.notification_top_pad_large_text);
2958 float largeFactor = (MathUtils.constrain(fontScale, 1.0f, LARGE_TEXT_SCALE) - 1f)
2959 / (LARGE_TEXT_SCALE - 1f);
2960
2961 // Linearly interpolate the padding between large and normal with the font scale ranging
2962 // from 1f to LARGE_TEXT_SCALE
2963 return Math.round((1 - largeFactor) * padding + largeFactor * largePadding);
2964 }
2965
Christoph Studerfe718432014-09-01 18:21:18 +02002966 private void resetStandardTemplateWithActions(RemoteViews big) {
2967 big.setViewVisibility(R.id.actions, View.GONE);
2968 big.setViewVisibility(R.id.action_divider, View.GONE);
2969 big.removeAllViews(R.id.actions);
2970 }
2971
Daniel Sandler96fd7c12012-03-30 16:37:36 -04002972 private RemoteViews applyStandardTemplateWithActions(int layoutId) {
Jorim Jaggi445d3c02014-08-19 22:33:42 +02002973 RemoteViews big = applyStandardTemplate(layoutId);
Daniel Sandler96fd7c12012-03-30 16:37:36 -04002974
Christoph Studerfe718432014-09-01 18:21:18 +02002975 resetStandardTemplateWithActions(big);
2976
Daniel Sandler96fd7c12012-03-30 16:37:36 -04002977 int N = mActions.size();
2978 if (N > 0) {
Daniel Sandler96fd7c12012-03-30 16:37:36 -04002979 big.setViewVisibility(R.id.actions, View.VISIBLE);
Daniel Sandler6387d2f2012-05-22 13:44:09 -04002980 big.setViewVisibility(R.id.action_divider, View.VISIBLE);
Daniel Sandler8680bf82012-05-15 16:52:52 -04002981 if (N>MAX_ACTION_BUTTONS) N=MAX_ACTION_BUTTONS;
Daniel Sandler96fd7c12012-03-30 16:37:36 -04002982 for (int i=0; i<N; i++) {
2983 final RemoteViews button = generateActionButton(mActions.get(i));
Daniel Sandler96fd7c12012-03-30 16:37:36 -04002984 big.addView(R.id.actions, button);
2985 }
2986 }
2987 return big;
2988 }
2989
Joe Onorato46439ce2010-11-19 13:56:21 -08002990 private RemoteViews makeContentView() {
2991 if (mContentView != null) {
2992 return mContentView;
2993 } else {
Jorim Jaggi445d3c02014-08-19 22:33:42 +02002994 return applyStandardTemplate(getBaseLayoutResource());
Joe Onorato46439ce2010-11-19 13:56:21 -08002995 }
2996 }
2997
2998 private RemoteViews makeTickerView() {
2999 if (mTickerView != null) {
3000 return mTickerView;
Joe Onorato46439ce2010-11-19 13:56:21 -08003001 }
Dan Sandler5fcdf6e2014-07-18 11:31:15 -04003002 return null; // tickers are not created by default anymore
Joe Onorato46439ce2010-11-19 13:56:21 -08003003 }
3004
Daniel Sandler96fd7c12012-03-30 16:37:36 -04003005 private RemoteViews makeBigContentView() {
3006 if (mActions.size() == 0) return null;
3007
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01003008 return applyStandardTemplateWithActions(getBigBaseLayoutResource());
Daniel Sandler96fd7c12012-03-30 16:37:36 -04003009 }
3010
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01003011 private RemoteViews makeHeadsUpContentView() {
Chris Wren8fd39ec2014-02-27 17:43:26 -05003012 if (mActions.size() == 0) return null;
3013
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01003014 return applyStandardTemplateWithActions(getBigBaseLayoutResource());
Chris Wren8fd39ec2014-02-27 17:43:26 -05003015 }
3016
3017
Daniel Sandler96fd7c12012-03-30 16:37:36 -04003018 private RemoteViews generateActionButton(Action action) {
Daniel Sandler8680bf82012-05-15 16:52:52 -04003019 final boolean tombstone = (action.actionIntent == null);
Joe Malin8d40d042012-11-05 11:36:40 -08003020 RemoteViews button = new RemoteViews(mContext.getPackageName(),
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01003021 tombstone ? getActionTombstoneLayoutResource()
3022 : getActionLayoutResource());
Chris Wren8749ac8a2013-12-03 14:31:01 -05003023 button.setTextViewCompoundDrawablesRelative(R.id.action0, action.icon, 0, 0, 0);
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01003024 button.setTextViewText(R.id.action0, processLegacyText(action.title));
Daniel Sandler8680bf82012-05-15 16:52:52 -04003025 if (!tombstone) {
Daniel Sandlere5518842012-05-10 16:20:40 -04003026 button.setOnClickPendingIntent(R.id.action0, action.actionIntent);
Daniel Sandlere5518842012-05-10 16:20:40 -04003027 }
Daniel Sandler96fd7c12012-03-30 16:37:36 -04003028 button.setContentDescription(R.id.action0, action.title);
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01003029 processLegacyAction(action, button);
Daniel Sandler96fd7c12012-03-30 16:37:36 -04003030 return button;
3031 }
3032
Joe Onoratocb109a02011-01-18 17:57:41 -08003033 /**
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01003034 * @return Whether we are currently building a notification from a legacy (an app that
Alan Viverette3cb07a462014-06-06 14:19:53 -07003035 * doesn't create material notifications by itself) app.
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01003036 */
3037 private boolean isLegacy() {
Dan Sandler26e81cf2014-05-06 10:01:27 -04003038 return mColorUtil != null;
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01003039 }
3040
3041 private void processLegacyAction(Action action, RemoteViews button) {
Dan Sandler05c362d2014-09-03 00:16:27 +02003042 if (!isLegacy() || mColorUtil.isGrayscaleIcon(mContext, action.icon)) {
Christoph Studer239f8352014-08-25 15:13:18 +02003043 button.setTextViewCompoundDrawablesRelativeColorFilter(R.id.action0, 0,
3044 mContext.getResources().getColor(R.color.notification_action_color_filter),
3045 PorterDuff.Mode.MULTIPLY);
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01003046 }
3047 }
3048
3049 private CharSequence processLegacyText(CharSequence charSequence) {
3050 if (isLegacy()) {
Dan Sandler26e81cf2014-05-06 10:01:27 -04003051 return mColorUtil.invertCharSequenceColors(charSequence);
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01003052 } else {
3053 return charSequence;
3054 }
3055 }
3056
Dan Sandler26e81cf2014-05-06 10:01:27 -04003057 /**
3058 * Apply any necessary background to smallIcons being used in the largeIcon spot.
3059 */
3060 private void processSmallIconAsLarge(int largeIconId, RemoteViews contentView) {
Jorim Jaggi92df1f22014-12-16 19:44:41 +01003061 if (!isLegacy()) {
3062 contentView.setDrawableParameters(R.id.icon, false, -1,
3063 0xFFFFFFFF,
3064 PorterDuff.Mode.SRC_ATOP, -1);
3065 }
Dan Sandler05c362d2014-09-03 00:16:27 +02003066 if (!isLegacy() || mColorUtil.isGrayscaleIcon(mContext, largeIconId)) {
Dan Sandler26e81cf2014-05-06 10:01:27 -04003067 applyLargeIconBackground(contentView);
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01003068 }
3069 }
3070
Dan Sandler26e81cf2014-05-06 10:01:27 -04003071 /**
3072 * Apply any necessary background to a largeIcon if it's a fake smallIcon (that is,
3073 * if it's grayscale).
3074 */
3075 // TODO: also check bounds, transparency, that sort of thing.
Christoph Studer239f8352014-08-25 15:13:18 +02003076 private void processLargeLegacyIcon(Bitmap largeIcon, RemoteViews contentView) {
Dan Sandler05c362d2014-09-03 00:16:27 +02003077 if (isLegacy() && mColorUtil.isGrayscaleIcon(largeIcon)) {
Dan Sandler26e81cf2014-05-06 10:01:27 -04003078 applyLargeIconBackground(contentView);
Dan Sandler190d58d2014-05-15 09:33:39 -04003079 } else {
3080 removeLargeIconBackground(contentView);
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01003081 }
3082 }
3083
Dan Sandler26e81cf2014-05-06 10:01:27 -04003084 /**
3085 * Add a colored circle behind the largeIcon slot.
3086 */
3087 private void applyLargeIconBackground(RemoteViews contentView) {
3088 contentView.setInt(R.id.icon, "setBackgroundResource",
Jorim Jaggi445d3c02014-08-19 22:33:42 +02003089 R.drawable.notification_icon_legacy_bg);
Dan Sandler26e81cf2014-05-06 10:01:27 -04003090
3091 contentView.setDrawableParameters(
3092 R.id.icon,
3093 true,
3094 -1,
Jorim Jaggi74419312014-06-10 20:57:21 +02003095 resolveColor(),
Dan Sandler26e81cf2014-05-06 10:01:27 -04003096 PorterDuff.Mode.SRC_ATOP,
3097 -1);
Jorim Jaggi445d3c02014-08-19 22:33:42 +02003098
3099 int padding = mContext.getResources().getDimensionPixelSize(
3100 R.dimen.notification_large_icon_circle_padding);
3101 contentView.setViewPadding(R.id.icon, padding, padding, padding, padding);
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01003102 }
3103
Dan Sandler190d58d2014-05-15 09:33:39 -04003104 private void removeLargeIconBackground(RemoteViews contentView) {
3105 contentView.setInt(R.id.icon, "setBackgroundResource", 0);
3106 }
3107
Dan Sandler26e81cf2014-05-06 10:01:27 -04003108 /**
3109 * Recolor small icons when used in the R.id.right_icon slot.
3110 */
Dan Sandler190d58d2014-05-15 09:33:39 -04003111 private void processSmallRightIcon(int smallIconDrawableId,
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01003112 RemoteViews contentView) {
Jorim Jaggi92df1f22014-12-16 19:44:41 +01003113 if (!isLegacy()) {
Dan Sandler190d58d2014-05-15 09:33:39 -04003114 contentView.setDrawableParameters(R.id.right_icon, false, -1,
3115 0xFFFFFFFF,
3116 PorterDuff.Mode.SRC_ATOP, -1);
Jorim Jaggi92df1f22014-12-16 19:44:41 +01003117 }
3118 if (!isLegacy() || mColorUtil.isGrayscaleIcon(mContext, smallIconDrawableId)) {
Dan Sandler190d58d2014-05-15 09:33:39 -04003119 contentView.setInt(R.id.right_icon,
3120 "setBackgroundResource",
3121 R.drawable.notification_icon_legacy_bg);
3122
3123 contentView.setDrawableParameters(
3124 R.id.right_icon,
3125 true,
3126 -1,
Jorim Jaggi74419312014-06-10 20:57:21 +02003127 resolveColor(),
Dan Sandler190d58d2014-05-15 09:33:39 -04003128 PorterDuff.Mode.SRC_ATOP,
3129 -1);
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01003130 }
3131 }
3132
Jorim Jaggi74419312014-06-10 20:57:21 +02003133 private int sanitizeColor() {
3134 if (mColor != COLOR_DEFAULT) {
3135 mColor |= 0xFF000000; // no alpha for custom colors
3136 }
3137 return mColor;
3138 }
3139
Dan Sandler26e81cf2014-05-06 10:01:27 -04003140 private int resolveColor() {
3141 if (mColor == COLOR_DEFAULT) {
Jorim Jaggi74419312014-06-10 20:57:21 +02003142 return mContext.getResources().getColor(R.color.notification_icon_bg_color);
Dan Sandler26e81cf2014-05-06 10:01:27 -04003143 }
3144 return mColor;
3145 }
3146
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01003147 /**
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003148 * Apply the unstyled operations and return a new {@link Notification} object.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04003149 * @hide
Joe Onoratocb109a02011-01-18 17:57:41 -08003150 */
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04003151 public Notification buildUnstyled() {
Joe Onorato46439ce2010-11-19 13:56:21 -08003152 Notification n = new Notification();
3153 n.when = mWhen;
3154 n.icon = mSmallIcon;
3155 n.iconLevel = mSmallIconLevel;
Joe Onorato8595a3d2010-11-19 18:12:07 -08003156 n.number = mNumber;
Dan Sandler26e81cf2014-05-06 10:01:27 -04003157
Jorim Jaggi74419312014-06-10 20:57:21 +02003158 n.color = sanitizeColor();
Dan Sandler26e81cf2014-05-06 10:01:27 -04003159
Christoph Studer4600f9b2014-07-22 22:44:43 +02003160 setBuilderContentView(n, makeContentView());
Joe Onorato46439ce2010-11-19 13:56:21 -08003161 n.contentIntent = mContentIntent;
3162 n.deleteIntent = mDeleteIntent;
3163 n.fullScreenIntent = mFullScreenIntent;
3164 n.tickerText = mTickerText;
3165 n.tickerView = makeTickerView();
3166 n.largeIcon = mLargeIcon;
3167 n.sound = mSound;
3168 n.audioStreamType = mAudioStreamType;
John Spurlockc0650f022014-07-19 13:22:39 -04003169 n.audioAttributes = mAudioAttributes;
Joe Onorato46439ce2010-11-19 13:56:21 -08003170 n.vibrate = mVibrate;
3171 n.ledARGB = mLedArgb;
3172 n.ledOnMS = mLedOnMs;
3173 n.ledOffMS = mLedOffMs;
3174 n.defaults = mDefaults;
3175 n.flags = mFlags;
Christoph Studer4600f9b2014-07-22 22:44:43 +02003176 setBuilderBigContentView(n, makeBigContentView());
3177 setBuilderHeadsUpContentView(n, makeHeadsUpContentView());
Daniel Sandler26c13432013-04-04 11:01:04 -04003178 if (mLedOnMs != 0 || mLedOffMs != 0) {
Joe Onorato8d0b6552010-11-22 16:09:29 -08003179 n.flags |= FLAG_SHOW_LIGHTS;
3180 }
3181 if ((mDefaults & DEFAULT_LIGHTS) != 0) {
3182 n.flags |= FLAG_SHOW_LIGHTS;
3183 }
John Spurlockfd7f1e02014-03-18 16:41:57 -04003184 n.category = mCategory;
Griff Hazen5cadc3b2014-05-20 09:55:39 -07003185 n.mGroupKey = mGroupKey;
3186 n.mSortKey = mSortKey;
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003187 n.priority = mPriority;
Daniel Sandlera0a938c2012-03-15 08:42:37 -04003188 if (mActions.size() > 0) {
3189 n.actions = new Action[mActions.size()];
3190 mActions.toArray(n.actions);
3191 }
Dan Sandler0bf2ed82013-12-21 23:33:41 -06003192 n.visibility = mVisibility;
3193
3194 if (mPublicVersion != null) {
3195 n.publicVersion = new Notification();
3196 mPublicVersion.cloneInto(n.publicVersion, true);
3197 }
Christoph Studer4600f9b2014-07-22 22:44:43 +02003198 // Note: If you're adding new fields, also update restoreFromNotitification().
Joe Onorato46439ce2010-11-19 13:56:21 -08003199 return n;
3200 }
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003201
3202 /**
Daniel Sandlerf45564e2013-04-15 15:05:08 -04003203 * Capture, in the provided bundle, semantic information used in the construction of
3204 * this Notification object.
3205 * @hide
3206 */
Griff Hazen720042b2014-02-24 15:46:56 -08003207 public void populateExtras(Bundle extras) {
Daniel Sandlerf45564e2013-04-15 15:05:08 -04003208 // Store original information used in the construction of this object
Kenny Guy8942bcd2014-09-08 21:09:47 +01003209 extras.putInt(EXTRA_ORIGINATING_USERID, mOriginatingUserId);
Kenny Guy77320062014-08-27 21:37:15 +01003210 extras.putParcelable(EXTRA_REBUILD_CONTEXT_APPLICATION_INFO,
3211 mContext.getApplicationInfo());
Daniel Sandlerf45564e2013-04-15 15:05:08 -04003212 extras.putCharSequence(EXTRA_TITLE, mContentTitle);
3213 extras.putCharSequence(EXTRA_TEXT, mContentText);
3214 extras.putCharSequence(EXTRA_SUB_TEXT, mSubText);
3215 extras.putCharSequence(EXTRA_INFO_TEXT, mContentInfo);
3216 extras.putInt(EXTRA_SMALL_ICON, mSmallIcon);
3217 extras.putInt(EXTRA_PROGRESS, mProgress);
3218 extras.putInt(EXTRA_PROGRESS_MAX, mProgressMax);
3219 extras.putBoolean(EXTRA_PROGRESS_INDETERMINATE, mProgressIndeterminate);
3220 extras.putBoolean(EXTRA_SHOW_CHRONOMETER, mUseChronometer);
3221 extras.putBoolean(EXTRA_SHOW_WHEN, mShowWhen);
John Spurlockac08a472013-06-10 11:37:08 -04003222 if (mLargeIcon != null) {
3223 extras.putParcelable(EXTRA_LARGE_ICON, mLargeIcon);
3224 }
Chris Wrendde75302014-03-26 17:24:15 -04003225 if (!mPeople.isEmpty()) {
3226 extras.putStringArray(EXTRA_PEOPLE, mPeople.toArray(new String[mPeople.size()]));
3227 }
Christoph Studer4600f9b2014-07-22 22:44:43 +02003228 // NOTE: If you're adding new extras also update restoreFromNotification().
3229 }
3230
3231
3232 /**
3233 * @hide
3234 */
3235 public static void stripForDelivery(Notification n) {
3236 if (!STRIP_AND_REBUILD) {
3237 return;
3238 }
3239
3240 String templateClass = n.extras.getString(EXTRA_TEMPLATE);
3241 // Only strip views for known Styles because we won't know how to
3242 // re-create them otherwise.
3243 boolean stripViews = TextUtils.isEmpty(templateClass) ||
3244 getNotificationStyleClass(templateClass) != null;
3245
3246 boolean isStripped = false;
3247
3248 if (n.largeIcon != null && n.extras.containsKey(EXTRA_LARGE_ICON)) {
3249 // TODO: Would like to check for equality here, but if the notification
3250 // has been cloned, we can't.
3251 n.largeIcon = null;
3252 n.extras.putBoolean(EXTRA_REBUILD_LARGE_ICON, true);
3253 isStripped = true;
3254 }
3255 // Get rid of unmodified BuilderRemoteViews.
3256
3257 if (stripViews &&
3258 n.contentView instanceof BuilderRemoteViews &&
3259 n.extras.getInt(EXTRA_REBUILD_CONTENT_VIEW_ACTION_COUNT, -1) ==
3260 n.contentView.getSequenceNumber()) {
3261 n.contentView = null;
3262 n.extras.putBoolean(EXTRA_REBUILD_CONTENT_VIEW, true);
3263 n.extras.remove(EXTRA_REBUILD_CONTENT_VIEW_ACTION_COUNT);
3264 isStripped = true;
3265 }
3266 if (stripViews &&
3267 n.bigContentView instanceof BuilderRemoteViews &&
3268 n.extras.getInt(EXTRA_REBUILD_BIG_CONTENT_VIEW_ACTION_COUNT, -1) ==
3269 n.bigContentView.getSequenceNumber()) {
3270 n.bigContentView = null;
3271 n.extras.putBoolean(EXTRA_REBUILD_BIG_CONTENT_VIEW, true);
3272 n.extras.remove(EXTRA_REBUILD_BIG_CONTENT_VIEW_ACTION_COUNT);
3273 isStripped = true;
3274 }
3275 if (stripViews &&
3276 n.headsUpContentView instanceof BuilderRemoteViews &&
3277 n.extras.getInt(EXTRA_REBUILD_HEADS_UP_CONTENT_VIEW_ACTION_COUNT, -1) ==
3278 n.headsUpContentView.getSequenceNumber()) {
3279 n.headsUpContentView = null;
3280 n.extras.putBoolean(EXTRA_REBUILD_HEADS_UP_CONTENT_VIEW, true);
3281 n.extras.remove(EXTRA_REBUILD_HEADS_UP_CONTENT_VIEW_ACTION_COUNT);
3282 isStripped = true;
3283 }
3284
3285 if (isStripped) {
3286 n.extras.putBoolean(EXTRA_NEEDS_REBUILD, true);
3287 }
3288 }
3289
3290 /**
3291 * @hide
3292 */
3293 public static Notification rebuild(Context context, Notification n) {
3294 Bundle extras = n.extras;
3295 if (!extras.getBoolean(EXTRA_NEEDS_REBUILD)) return n;
3296 extras.remove(EXTRA_NEEDS_REBUILD);
3297
3298 // Re-create notification context so we can access app resources.
Kenny Guy77320062014-08-27 21:37:15 +01003299 ApplicationInfo applicationInfo = extras.getParcelable(
3300 EXTRA_REBUILD_CONTEXT_APPLICATION_INFO);
Christoph Studer4600f9b2014-07-22 22:44:43 +02003301 Context builderContext;
3302 try {
Kenny Guy77320062014-08-27 21:37:15 +01003303 builderContext = context.createApplicationContext(applicationInfo,
Christoph Studer4600f9b2014-07-22 22:44:43 +02003304 Context.CONTEXT_RESTRICTED);
3305 } catch (NameNotFoundException e) {
Kenny Guy77320062014-08-27 21:37:15 +01003306 Log.e(TAG, "ApplicationInfo " + applicationInfo + " not found");
Christoph Studer4600f9b2014-07-22 22:44:43 +02003307 builderContext = context; // try with our context
3308 }
3309
3310 Builder b = new Builder(builderContext, n);
3311 return b.rebuild();
3312 }
3313
3314 /**
3315 * Rebuilds the notification passed in to the rebuild-constructor
3316 * {@link #Builder(Context, Notification)}.
3317 *
3318 * <p>
3319 * Throws IllegalStateException when invoked on a Builder that isn't in rebuild mode.
3320 *
3321 * @hide
3322 */
3323 private Notification rebuild() {
3324 if (mRebuildNotification == null) {
3325 throw new IllegalStateException("rebuild() only valid when in 'rebuild' mode.");
3326 }
Jorim Jaggid05aa3e2014-08-28 17:52:27 +02003327 mHasThreeLines = hasThreeLines();
Christoph Studer4600f9b2014-07-22 22:44:43 +02003328
3329 Bundle extras = mRebuildNotification.extras;
3330
3331 if (extras.getBoolean(EXTRA_REBUILD_LARGE_ICON)) {
3332 mRebuildNotification.largeIcon = extras.getParcelable(EXTRA_LARGE_ICON);
3333 }
3334 extras.remove(EXTRA_REBUILD_LARGE_ICON);
3335
3336 if (extras.getBoolean(EXTRA_REBUILD_CONTENT_VIEW)) {
3337 setBuilderContentView(mRebuildNotification, makeContentView());
3338 if (mStyle != null) {
3339 mStyle.populateContentView(mRebuildNotification);
3340 }
3341 }
3342 extras.remove(EXTRA_REBUILD_CONTENT_VIEW);
3343
3344 if (extras.getBoolean(EXTRA_REBUILD_BIG_CONTENT_VIEW)) {
3345 setBuilderBigContentView(mRebuildNotification, makeBigContentView());
3346 if (mStyle != null) {
3347 mStyle.populateBigContentView(mRebuildNotification);
3348 }
3349 }
3350 extras.remove(EXTRA_REBUILD_BIG_CONTENT_VIEW);
3351
3352 if (extras.getBoolean(EXTRA_REBUILD_HEADS_UP_CONTENT_VIEW)) {
3353 setBuilderHeadsUpContentView(mRebuildNotification, makeHeadsUpContentView());
3354 if (mStyle != null) {
3355 mStyle.populateHeadsUpContentView(mRebuildNotification);
3356 }
3357 }
3358 extras.remove(EXTRA_REBUILD_HEADS_UP_CONTENT_VIEW);
3359
Jorim Jaggid05aa3e2014-08-28 17:52:27 +02003360 mHasThreeLines = false;
Christoph Studer4600f9b2014-07-22 22:44:43 +02003361 return mRebuildNotification;
3362 }
3363
3364 private static Class<? extends Style> getNotificationStyleClass(String templateClass) {
3365 Class<? extends Style>[] classes = new Class[]{
3366 BigTextStyle.class, BigPictureStyle.class, InboxStyle.class, MediaStyle.class};
3367 for (Class<? extends Style> innerClass : classes) {
3368 if (templateClass.equals(innerClass.getName())) {
3369 return innerClass;
3370 }
3371 }
3372 return null;
3373 }
3374
3375 private void setBuilderContentView(Notification n, RemoteViews contentView) {
3376 n.contentView = contentView;
3377 if (contentView instanceof BuilderRemoteViews) {
3378 mRebuildBundle.putInt(Builder.EXTRA_REBUILD_CONTENT_VIEW_ACTION_COUNT,
3379 contentView.getSequenceNumber());
3380 }
3381 }
3382
3383 private void setBuilderBigContentView(Notification n, RemoteViews bigContentView) {
3384 n.bigContentView = bigContentView;
3385 if (bigContentView instanceof BuilderRemoteViews) {
3386 mRebuildBundle.putInt(Builder.EXTRA_REBUILD_BIG_CONTENT_VIEW_ACTION_COUNT,
3387 bigContentView.getSequenceNumber());
3388 }
3389 }
3390
3391 private void setBuilderHeadsUpContentView(Notification n,
3392 RemoteViews headsUpContentView) {
3393 n.headsUpContentView = headsUpContentView;
3394 if (headsUpContentView instanceof BuilderRemoteViews) {
3395 mRebuildBundle.putInt(Builder.EXTRA_REBUILD_HEADS_UP_CONTENT_VIEW_ACTION_COUNT,
3396 headsUpContentView.getSequenceNumber());
3397 }
3398 }
3399
3400 private void restoreFromNotification(Notification n) {
3401
3402 // Notification fields.
3403 mWhen = n.when;
3404 mSmallIcon = n.icon;
3405 mSmallIconLevel = n.iconLevel;
3406 mNumber = n.number;
3407
3408 mColor = n.color;
3409
3410 mContentView = n.contentView;
3411 mDeleteIntent = n.deleteIntent;
3412 mFullScreenIntent = n.fullScreenIntent;
3413 mTickerText = n.tickerText;
3414 mTickerView = n.tickerView;
3415 mLargeIcon = n.largeIcon;
3416 mSound = n.sound;
3417 mAudioStreamType = n.audioStreamType;
3418 mAudioAttributes = n.audioAttributes;
3419
3420 mVibrate = n.vibrate;
3421 mLedArgb = n.ledARGB;
3422 mLedOnMs = n.ledOnMS;
3423 mLedOffMs = n.ledOffMS;
3424 mDefaults = n.defaults;
3425 mFlags = n.flags;
3426
3427 mCategory = n.category;
3428 mGroupKey = n.mGroupKey;
3429 mSortKey = n.mSortKey;
3430 mPriority = n.priority;
3431 mActions.clear();
3432 if (n.actions != null) {
3433 Collections.addAll(mActions, n.actions);
3434 }
3435 mVisibility = n.visibility;
3436
3437 mPublicVersion = n.publicVersion;
3438
3439 // Extras.
3440 Bundle extras = n.extras;
Kenny Guy8942bcd2014-09-08 21:09:47 +01003441 mOriginatingUserId = extras.getInt(EXTRA_ORIGINATING_USERID);
Christoph Studer4600f9b2014-07-22 22:44:43 +02003442 mContentTitle = extras.getCharSequence(EXTRA_TITLE);
3443 mContentText = extras.getCharSequence(EXTRA_TEXT);
3444 mSubText = extras.getCharSequence(EXTRA_SUB_TEXT);
3445 mContentInfo = extras.getCharSequence(EXTRA_INFO_TEXT);
3446 mSmallIcon = extras.getInt(EXTRA_SMALL_ICON);
3447 mProgress = extras.getInt(EXTRA_PROGRESS);
3448 mProgressMax = extras.getInt(EXTRA_PROGRESS_MAX);
3449 mProgressIndeterminate = extras.getBoolean(EXTRA_PROGRESS_INDETERMINATE);
3450 mUseChronometer = extras.getBoolean(EXTRA_SHOW_CHRONOMETER);
3451 mShowWhen = extras.getBoolean(EXTRA_SHOW_WHEN);
3452 if (extras.containsKey(EXTRA_LARGE_ICON)) {
3453 mLargeIcon = extras.getParcelable(EXTRA_LARGE_ICON);
3454 }
3455 if (extras.containsKey(EXTRA_PEOPLE)) {
3456 mPeople.clear();
3457 Collections.addAll(mPeople, extras.getStringArray(EXTRA_PEOPLE));
3458 }
Daniel Sandlerf45564e2013-04-15 15:05:08 -04003459 }
3460
3461 /**
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003462 * @deprecated Use {@link #build()} instead.
3463 */
3464 @Deprecated
3465 public Notification getNotification() {
3466 return build();
3467 }
3468
3469 /**
3470 * Combine all of the options that have been set and return a new {@link Notification}
3471 * object.
3472 */
3473 public Notification build() {
Kenny Guy8942bcd2014-09-08 21:09:47 +01003474 mOriginatingUserId = mContext.getUserId();
Jorim Jaggid05aa3e2014-08-28 17:52:27 +02003475 mHasThreeLines = hasThreeLines();
Christoph Studer943aa672014-08-03 20:31:16 +02003476
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04003477 Notification n = buildUnstyled();
Daniel Sandlerf45564e2013-04-15 15:05:08 -04003478
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003479 if (mStyle != null) {
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04003480 n = mStyle.buildStyled(n);
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003481 }
Daniel Sandlerf45564e2013-04-15 15:05:08 -04003482
Christoph Studer4600f9b2014-07-22 22:44:43 +02003483 if (mExtras != null) {
3484 n.extras.putAll(mExtras);
3485 }
3486
3487 if (mRebuildBundle.size() > 0) {
3488 n.extras.putAll(mRebuildBundle);
3489 mRebuildBundle.clear();
3490 }
Daniel Sandlerf45564e2013-04-15 15:05:08 -04003491
Griff Hazen720042b2014-02-24 15:46:56 -08003492 populateExtras(n.extras);
Daniel Sandlerf45564e2013-04-15 15:05:08 -04003493 if (mStyle != null) {
3494 mStyle.addExtras(n.extras);
3495 }
3496
Jorim Jaggid05aa3e2014-08-28 17:52:27 +02003497 mHasThreeLines = false;
Daniel Sandlerf45564e2013-04-15 15:05:08 -04003498 return n;
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003499 }
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05003500
3501 /**
3502 * Apply this Builder to an existing {@link Notification} object.
3503 *
3504 * @hide
3505 */
3506 public Notification buildInto(Notification n) {
Daniel Sandler1a497d32013-04-18 14:52:45 -04003507 build().cloneInto(n, true);
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05003508 return n;
3509 }
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01003510
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01003511 private int getBaseLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07003512 return R.layout.notification_template_material_base;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01003513 }
3514
3515 private int getBigBaseLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07003516 return R.layout.notification_template_material_big_base;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01003517 }
3518
3519 private int getBigPictureLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07003520 return R.layout.notification_template_material_big_picture;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01003521 }
3522
3523 private int getBigTextLayoutResource() {
Jorim Jaggi445d3c02014-08-19 22:33:42 +02003524 return R.layout.notification_template_material_big_text;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01003525 }
3526
3527 private int getInboxLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07003528 return R.layout.notification_template_material_inbox;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01003529 }
3530
3531 private int getActionLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07003532 return R.layout.notification_material_action;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01003533 }
3534
3535 private int getActionTombstoneLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07003536 return R.layout.notification_material_action_tombstone;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01003537 }
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003538 }
3539
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003540 /**
3541 * An object that can apply a rich notification style to a {@link Notification.Builder}
3542 * object.
3543 */
Griff Hazendfcb0802014-02-11 12:00:00 -08003544 public static abstract class Style {
Chris Wrend6297db2012-05-03 16:20:13 -04003545 private CharSequence mBigContentTitle;
Jorim Jaggi457a10d2014-09-08 16:18:23 +02003546
3547 /**
3548 * @hide
3549 */
3550 protected CharSequence mSummaryText = null;
3551
3552 /**
3553 * @hide
3554 */
3555 protected boolean mSummaryTextSet = false;
Chris Wrend6297db2012-05-03 16:20:13 -04003556
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003557 protected Builder mBuilder;
3558
Chris Wrend6297db2012-05-03 16:20:13 -04003559 /**
3560 * Overrides ContentTitle in the big form of the template.
3561 * This defaults to the value passed to setContentTitle().
3562 */
3563 protected void internalSetBigContentTitle(CharSequence title) {
3564 mBigContentTitle = title;
3565 }
3566
3567 /**
3568 * Set the first line of text after the detail section in the big form of the template.
3569 */
3570 protected void internalSetSummaryText(CharSequence cs) {
3571 mSummaryText = cs;
Daniel Sandler619738c2012-06-07 16:33:08 -04003572 mSummaryTextSet = true;
Chris Wrend6297db2012-05-03 16:20:13 -04003573 }
3574
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003575 public void setBuilder(Builder builder) {
3576 if (mBuilder != builder) {
3577 mBuilder = builder;
Daniel Sandlerc08dea22012-06-28 08:35:24 -07003578 if (mBuilder != null) {
3579 mBuilder.setStyle(this);
3580 }
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003581 }
3582 }
3583
Chris Wrend6297db2012-05-03 16:20:13 -04003584 protected void checkBuilder() {
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003585 if (mBuilder == null) {
3586 throw new IllegalArgumentException("Style requires a valid Builder object");
3587 }
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003588 }
Chris Wrend6297db2012-05-03 16:20:13 -04003589
3590 protected RemoteViews getStandardView(int layoutId) {
3591 checkBuilder();
3592
Christoph Studer4600f9b2014-07-22 22:44:43 +02003593 // Nasty.
3594 CharSequence oldBuilderContentTitle = mBuilder.mContentTitle;
Chris Wrend6297db2012-05-03 16:20:13 -04003595 if (mBigContentTitle != null) {
3596 mBuilder.setContentTitle(mBigContentTitle);
3597 }
3598
Chris Wrend6297db2012-05-03 16:20:13 -04003599 RemoteViews contentView = mBuilder.applyStandardTemplateWithActions(layoutId);
3600
Christoph Studer4600f9b2014-07-22 22:44:43 +02003601 mBuilder.mContentTitle = oldBuilderContentTitle;
3602
Chris Wrend6297db2012-05-03 16:20:13 -04003603 if (mBigContentTitle != null && mBigContentTitle.equals("")) {
3604 contentView.setViewVisibility(R.id.line1, View.GONE);
Chris Wren67dc9a02012-05-16 01:03:20 -04003605 } else {
3606 contentView.setViewVisibility(R.id.line1, View.VISIBLE);
Chris Wrend6297db2012-05-03 16:20:13 -04003607 }
3608
Daniel Sandler619738c2012-06-07 16:33:08 -04003609 // The last line defaults to the subtext, but can be replaced by mSummaryText
3610 final CharSequence overflowText =
3611 mSummaryTextSet ? mSummaryText
3612 : mBuilder.mSubText;
3613 if (overflowText != null) {
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01003614 contentView.setTextViewText(R.id.text, mBuilder.processLegacyText(overflowText));
Daniel Sandler619738c2012-06-07 16:33:08 -04003615 contentView.setViewVisibility(R.id.overflow_divider, View.VISIBLE);
Daniel Sandler9f7936a2012-05-21 16:14:28 -04003616 contentView.setViewVisibility(R.id.line3, View.VISIBLE);
Daniel Sandler916ad912012-06-13 12:17:07 -04003617 } else {
Kenny Guy98193ea2014-07-24 19:54:37 +01003618 // Clear text in case we use the line to show the profile badge.
3619 contentView.setTextViewText(R.id.text, "");
Daniel Sandler916ad912012-06-13 12:17:07 -04003620 contentView.setViewVisibility(R.id.overflow_divider, View.GONE);
3621 contentView.setViewVisibility(R.id.line3, View.GONE);
Chris Wrend6297db2012-05-03 16:20:13 -04003622 }
3623
3624 return contentView;
3625 }
3626
Daniel Sandlerf45564e2013-04-15 15:05:08 -04003627 /**
Jorim Jaggi445d3c02014-08-19 22:33:42 +02003628 * Changes the padding of the first line such that the big and small content view have the
3629 * same top padding.
3630 *
3631 * @hide
3632 */
3633 protected void applyTopPadding(RemoteViews contentView) {
3634 int topPadding = Builder.calculateTopPadding(mBuilder.mContext,
Jorim Jaggid05aa3e2014-08-28 17:52:27 +02003635 mBuilder.mHasThreeLines,
Jorim Jaggi445d3c02014-08-19 22:33:42 +02003636 mBuilder.mContext.getResources().getConfiguration().fontScale);
3637 contentView.setViewPadding(R.id.line1, 0, topPadding, 0, 0);
3638 }
3639
3640 /**
Daniel Sandlerf45564e2013-04-15 15:05:08 -04003641 * @hide
3642 */
3643 public void addExtras(Bundle extras) {
3644 if (mSummaryTextSet) {
3645 extras.putCharSequence(EXTRA_SUMMARY_TEXT, mSummaryText);
3646 }
3647 if (mBigContentTitle != null) {
3648 extras.putCharSequence(EXTRA_TITLE_BIG, mBigContentTitle);
3649 }
Chris Wren91ad5632013-06-05 15:05:57 -04003650 extras.putString(EXTRA_TEMPLATE, this.getClass().getName());
Daniel Sandlerf45564e2013-04-15 15:05:08 -04003651 }
3652
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04003653 /**
3654 * @hide
3655 */
Christoph Studer4600f9b2014-07-22 22:44:43 +02003656 protected void restoreFromExtras(Bundle extras) {
3657 if (extras.containsKey(EXTRA_SUMMARY_TEXT)) {
3658 mSummaryText = extras.getCharSequence(EXTRA_SUMMARY_TEXT);
3659 mSummaryTextSet = true;
3660 }
3661 if (extras.containsKey(EXTRA_TITLE_BIG)) {
3662 mBigContentTitle = extras.getCharSequence(EXTRA_TITLE_BIG);
3663 }
3664 }
3665
3666
3667 /**
3668 * @hide
3669 */
3670 public Notification buildStyled(Notification wip) {
3671 populateTickerView(wip);
3672 populateContentView(wip);
3673 populateBigContentView(wip);
3674 populateHeadsUpContentView(wip);
3675 return wip;
3676 }
3677
3678 // The following methods are split out so we can re-create notification partially.
3679 /**
3680 * @hide
3681 */
3682 protected void populateTickerView(Notification wip) {}
3683 /**
3684 * @hide
3685 */
3686 protected void populateContentView(Notification wip) {}
3687
3688 /**
3689 * @hide
3690 */
3691 protected void populateBigContentView(Notification wip) {}
3692
3693 /**
3694 * @hide
3695 */
3696 protected void populateHeadsUpContentView(Notification wip) {}
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04003697
3698 /**
3699 * Calls {@link android.app.Notification.Builder#build()} on the Builder this Style is
3700 * attached to.
3701 *
3702 * @return the fully constructed Notification.
3703 */
3704 public Notification build() {
3705 checkBuilder();
3706 return mBuilder.build();
3707 }
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02003708
3709 /**
3710 * @hide
3711 * @return true if the style positions the progress bar on the second line; false if the
3712 * style hides the progress bar
3713 */
3714 protected boolean hasProgress() {
3715 return true;
3716 }
Joe Onorato46439ce2010-11-19 13:56:21 -08003717 }
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003718
3719 /**
Daniel Sandler4dfbe832012-04-11 14:51:46 -04003720 * Helper class for generating large-format notifications that include a large image attachment.
Joe Malin8d40d042012-11-05 11:36:40 -08003721 *
Robert Ly91c5ce32014-06-08 15:37:00 -07003722 * Here's how you'd set the <code>BigPictureStyle</code> on a notification:
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003723 * <pre class="prettyprint">
Robert Ly91c5ce32014-06-08 15:37:00 -07003724 * Notification notif = new Notification.Builder(mContext)
3725 * .setContentTitle(&quot;New photo from &quot; + sender.toString())
3726 * .setContentText(subject)
3727 * .setSmallIcon(R.drawable.new_post)
3728 * .setLargeIcon(aBitmap)
3729 * .setStyle(new Notification.BigPictureStyle()
3730 * .bigPicture(aBigBitmap))
3731 * .build();
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003732 * </pre>
Joe Malin8d40d042012-11-05 11:36:40 -08003733 *
Daniel Sandler4dfbe832012-04-11 14:51:46 -04003734 * @see Notification#bigContentView
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003735 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003736 public static class BigPictureStyle extends Style {
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003737 private Bitmap mPicture;
Chris Wren3745a3d2012-05-22 15:11:52 -04003738 private Bitmap mBigLargeIcon;
3739 private boolean mBigLargeIconSet = false;
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003740
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003741 public BigPictureStyle() {
3742 }
3743
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003744 public BigPictureStyle(Builder builder) {
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003745 setBuilder(builder);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003746 }
3747
Chris Wrend6297db2012-05-03 16:20:13 -04003748 /**
3749 * Overrides ContentTitle in the big form of the template.
3750 * This defaults to the value passed to setContentTitle().
3751 */
3752 public BigPictureStyle setBigContentTitle(CharSequence title) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04003753 internalSetBigContentTitle(safeCharSequence(title));
Chris Wrend6297db2012-05-03 16:20:13 -04003754 return this;
3755 }
3756
3757 /**
3758 * Set the first line of text after the detail section in the big form of the template.
3759 */
3760 public BigPictureStyle setSummaryText(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04003761 internalSetSummaryText(safeCharSequence(cs));
Chris Wrend6297db2012-05-03 16:20:13 -04003762 return this;
3763 }
3764
Chris Wren0bd664d2012-08-01 13:56:56 -04003765 /**
3766 * Provide the bitmap to be used as the payload for the BigPicture notification.
3767 */
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003768 public BigPictureStyle bigPicture(Bitmap b) {
3769 mPicture = b;
3770 return this;
3771 }
3772
Chris Wren3745a3d2012-05-22 15:11:52 -04003773 /**
Chris Wren3745a3d2012-05-22 15:11:52 -04003774 * Override the large icon when the big notification is shown.
3775 */
3776 public BigPictureStyle bigLargeIcon(Bitmap b) {
3777 mBigLargeIconSet = true;
3778 mBigLargeIcon = b;
3779 return this;
3780 }
3781
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003782 private RemoteViews makeBigContentView() {
Christoph Studer5c510ee2014-12-15 16:32:27 +01003783
3784 // Replace mLargeIcon with mBigLargeIcon if mBigLargeIconSet
3785 // This covers the following cases:
3786 // 1. mBigLargeIconSet -> mBigLargeIcon (null or non-null) applies, overrides
3787 // mLargeIcon
3788 // 2. !mBigLargeIconSet -> mLargeIcon applies
3789 Bitmap oldLargeIcon = null;
3790 if (mBigLargeIconSet) {
3791 oldLargeIcon = mBuilder.mLargeIcon;
3792 mBuilder.mLargeIcon = mBigLargeIcon;
3793 }
3794
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01003795 RemoteViews contentView = getStandardView(mBuilder.getBigPictureLayoutResource());
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003796
Christoph Studer5c510ee2014-12-15 16:32:27 +01003797 if (mBigLargeIconSet) {
3798 mBuilder.mLargeIcon = oldLargeIcon;
3799 }
3800
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003801 contentView.setImageViewBitmap(R.id.big_picture, mPicture);
3802
Jorim Jaggi445d3c02014-08-19 22:33:42 +02003803 applyTopPadding(contentView);
3804
Kenny Guy98193ea2014-07-24 19:54:37 +01003805 boolean twoTextLines = mBuilder.mSubText != null && mBuilder.mContentText != null;
3806 mBuilder.addProfileBadge(contentView,
3807 twoTextLines ? R.id.profile_badge_line2 : R.id.profile_badge_line3);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003808 return contentView;
3809 }
3810
Daniel Sandlerf45564e2013-04-15 15:05:08 -04003811 /**
3812 * @hide
3813 */
3814 public void addExtras(Bundle extras) {
3815 super.addExtras(extras);
3816
3817 if (mBigLargeIconSet) {
3818 extras.putParcelable(EXTRA_LARGE_ICON_BIG, mBigLargeIcon);
3819 }
3820 extras.putParcelable(EXTRA_PICTURE, mPicture);
3821 }
3822
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04003823 /**
3824 * @hide
3825 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003826 @Override
Christoph Studer4600f9b2014-07-22 22:44:43 +02003827 protected void restoreFromExtras(Bundle extras) {
3828 super.restoreFromExtras(extras);
3829
3830 if (extras.containsKey(EXTRA_LARGE_ICON_BIG)) {
Christoph Studer5c510ee2014-12-15 16:32:27 +01003831 mBigLargeIconSet = true;
Christoph Studer4600f9b2014-07-22 22:44:43 +02003832 mBigLargeIcon = extras.getParcelable(EXTRA_LARGE_ICON_BIG);
Chris Wren3745a3d2012-05-22 15:11:52 -04003833 }
Christoph Studer4600f9b2014-07-22 22:44:43 +02003834 mPicture = extras.getParcelable(EXTRA_PICTURE);
3835 }
3836
3837 /**
3838 * @hide
3839 */
3840 @Override
3841 public void populateBigContentView(Notification wip) {
3842 mBuilder.setBuilderBigContentView(wip, makeBigContentView());
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003843 }
3844 }
3845
3846 /**
Daniel Sandler4dfbe832012-04-11 14:51:46 -04003847 * Helper class for generating large-format notifications that include a lot of text.
Joe Malin8d40d042012-11-05 11:36:40 -08003848 *
Robert Ly91c5ce32014-06-08 15:37:00 -07003849 * Here's how you'd set the <code>BigTextStyle</code> on a notification:
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003850 * <pre class="prettyprint">
Robert Ly91c5ce32014-06-08 15:37:00 -07003851 * Notification notif = new Notification.Builder(mContext)
3852 * .setContentTitle(&quot;New mail from &quot; + sender.toString())
3853 * .setContentText(subject)
3854 * .setSmallIcon(R.drawable.new_mail)
3855 * .setLargeIcon(aBitmap)
3856 * .setStyle(new Notification.BigTextStyle()
3857 * .bigText(aVeryLongString))
3858 * .build();
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003859 * </pre>
Joe Malin8d40d042012-11-05 11:36:40 -08003860 *
Daniel Sandler4dfbe832012-04-11 14:51:46 -04003861 * @see Notification#bigContentView
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003862 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003863 public static class BigTextStyle extends Style {
Jorim Jaggi457a10d2014-09-08 16:18:23 +02003864
3865 private static final int MAX_LINES = 13;
3866 private static final int LINES_CONSUMED_BY_ACTIONS = 3;
3867 private static final int LINES_CONSUMED_BY_SUMMARY = 2;
3868
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003869 private CharSequence mBigText;
3870
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003871 public BigTextStyle() {
3872 }
3873
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003874 public BigTextStyle(Builder builder) {
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003875 setBuilder(builder);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003876 }
3877
Chris Wrend6297db2012-05-03 16:20:13 -04003878 /**
3879 * Overrides ContentTitle in the big form of the template.
3880 * This defaults to the value passed to setContentTitle().
3881 */
3882 public BigTextStyle setBigContentTitle(CharSequence title) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04003883 internalSetBigContentTitle(safeCharSequence(title));
Chris Wrend6297db2012-05-03 16:20:13 -04003884 return this;
3885 }
3886
3887 /**
3888 * Set the first line of text after the detail section in the big form of the template.
3889 */
3890 public BigTextStyle setSummaryText(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04003891 internalSetSummaryText(safeCharSequence(cs));
Chris Wrend6297db2012-05-03 16:20:13 -04003892 return this;
3893 }
3894
Chris Wren0bd664d2012-08-01 13:56:56 -04003895 /**
3896 * Provide the longer text to be displayed in the big form of the
3897 * template in place of the content text.
3898 */
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003899 public BigTextStyle bigText(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04003900 mBigText = safeCharSequence(cs);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003901 return this;
3902 }
3903
Daniel Sandlerf45564e2013-04-15 15:05:08 -04003904 /**
3905 * @hide
3906 */
3907 public void addExtras(Bundle extras) {
3908 super.addExtras(extras);
3909
Christoph Studer4600f9b2014-07-22 22:44:43 +02003910 extras.putCharSequence(EXTRA_BIG_TEXT, mBigText);
3911 }
3912
3913 /**
3914 * @hide
3915 */
3916 @Override
3917 protected void restoreFromExtras(Bundle extras) {
3918 super.restoreFromExtras(extras);
3919
3920 mBigText = extras.getCharSequence(EXTRA_BIG_TEXT);
Daniel Sandlerf45564e2013-04-15 15:05:08 -04003921 }
3922
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003923 private RemoteViews makeBigContentView() {
Christoph Studer4600f9b2014-07-22 22:44:43 +02003924
3925 // Nasty
3926 CharSequence oldBuilderContentText = mBuilder.mContentText;
Daniel Sandler6387d2f2012-05-22 13:44:09 -04003927 mBuilder.mContentText = null;
Daniel Sandler916ad912012-06-13 12:17:07 -04003928
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01003929 RemoteViews contentView = getStandardView(mBuilder.getBigTextLayoutResource());
Joe Malin8d40d042012-11-05 11:36:40 -08003930
Christoph Studer4600f9b2014-07-22 22:44:43 +02003931 mBuilder.mContentText = oldBuilderContentText;
3932
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01003933 contentView.setTextViewText(R.id.big_text, mBuilder.processLegacyText(mBigText));
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003934 contentView.setViewVisibility(R.id.big_text, View.VISIBLE);
Jorim Jaggi457a10d2014-09-08 16:18:23 +02003935 contentView.setInt(R.id.big_text, "setMaxLines", calculateMaxLines());
Chris Wren3c5f92432012-05-04 16:31:17 -04003936 contentView.setViewVisibility(R.id.text2, View.GONE);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003937
Jorim Jaggi445d3c02014-08-19 22:33:42 +02003938 applyTopPadding(contentView);
3939
Jorim Jaggid05aa3e2014-08-28 17:52:27 +02003940 mBuilder.shrinkLine3Text(contentView);
3941
Kenny Guy98193ea2014-07-24 19:54:37 +01003942 mBuilder.addProfileBadge(contentView, R.id.profile_badge_large_template);
3943
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003944 return contentView;
3945 }
3946
Jorim Jaggi457a10d2014-09-08 16:18:23 +02003947 private int calculateMaxLines() {
3948 int lineCount = MAX_LINES;
3949 boolean hasActions = mBuilder.mActions.size() > 0;
3950 boolean hasSummary = (mSummaryTextSet ? mSummaryText : mBuilder.mSubText) != null;
3951 if (hasActions) {
3952 lineCount -= LINES_CONSUMED_BY_ACTIONS;
3953 }
3954 if (hasSummary) {
3955 lineCount -= LINES_CONSUMED_BY_SUMMARY;
3956 }
3957
3958 // If we have less top padding at the top, we can fit less lines.
3959 if (!mBuilder.mHasThreeLines) {
3960 lineCount--;
3961 }
3962 return lineCount;
3963 }
3964
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04003965 /**
3966 * @hide
3967 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003968 @Override
Christoph Studer4600f9b2014-07-22 22:44:43 +02003969 public void populateBigContentView(Notification wip) {
3970 mBuilder.setBuilderBigContentView(wip, makeBigContentView());
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003971 }
3972 }
Daniel Sandler879c5e02012-04-17 16:46:51 -04003973
3974 /**
3975 * Helper class for generating large-format notifications that include a list of (up to 5) strings.
Joe Malin8d40d042012-11-05 11:36:40 -08003976 *
Robert Ly91c5ce32014-06-08 15:37:00 -07003977 * Here's how you'd set the <code>InboxStyle</code> on a notification:
Daniel Sandler879c5e02012-04-17 16:46:51 -04003978 * <pre class="prettyprint">
Robert Ly91c5ce32014-06-08 15:37:00 -07003979 * Notification notif = new Notification.Builder(mContext)
3980 * .setContentTitle(&quot;5 New mails from &quot; + sender.toString())
3981 * .setContentText(subject)
3982 * .setSmallIcon(R.drawable.new_mail)
3983 * .setLargeIcon(aBitmap)
3984 * .setStyle(new Notification.InboxStyle()
3985 * .addLine(str1)
3986 * .addLine(str2)
3987 * .setContentTitle(&quot;&quot;)
3988 * .setSummaryText(&quot;+3 more&quot;))
3989 * .build();
Daniel Sandler879c5e02012-04-17 16:46:51 -04003990 * </pre>
Joe Malin8d40d042012-11-05 11:36:40 -08003991 *
Daniel Sandler879c5e02012-04-17 16:46:51 -04003992 * @see Notification#bigContentView
3993 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003994 public static class InboxStyle extends Style {
Daniel Sandler879c5e02012-04-17 16:46:51 -04003995 private ArrayList<CharSequence> mTexts = new ArrayList<CharSequence>(5);
3996
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003997 public InboxStyle() {
3998 }
3999
Daniel Sandler879c5e02012-04-17 16:46:51 -04004000 public InboxStyle(Builder builder) {
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004001 setBuilder(builder);
Daniel Sandler879c5e02012-04-17 16:46:51 -04004002 }
4003
Chris Wrend6297db2012-05-03 16:20:13 -04004004 /**
4005 * Overrides ContentTitle in the big form of the template.
4006 * This defaults to the value passed to setContentTitle().
4007 */
4008 public InboxStyle setBigContentTitle(CharSequence title) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04004009 internalSetBigContentTitle(safeCharSequence(title));
Chris Wrend6297db2012-05-03 16:20:13 -04004010 return this;
4011 }
4012
4013 /**
4014 * Set the first line of text after the detail section in the big form of the template.
4015 */
4016 public InboxStyle setSummaryText(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04004017 internalSetSummaryText(safeCharSequence(cs));
Chris Wrend6297db2012-05-03 16:20:13 -04004018 return this;
4019 }
4020
Chris Wren0bd664d2012-08-01 13:56:56 -04004021 /**
4022 * Append a line to the digest section of the Inbox notification.
4023 */
Daniel Sandler879c5e02012-04-17 16:46:51 -04004024 public InboxStyle addLine(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04004025 mTexts.add(safeCharSequence(cs));
Daniel Sandler879c5e02012-04-17 16:46:51 -04004026 return this;
4027 }
4028
Daniel Sandlerf45564e2013-04-15 15:05:08 -04004029 /**
4030 * @hide
4031 */
4032 public void addExtras(Bundle extras) {
4033 super.addExtras(extras);
Christoph Studer4600f9b2014-07-22 22:44:43 +02004034
Daniel Sandlerf45564e2013-04-15 15:05:08 -04004035 CharSequence[] a = new CharSequence[mTexts.size()];
4036 extras.putCharSequenceArray(EXTRA_TEXT_LINES, mTexts.toArray(a));
4037 }
4038
Christoph Studer4600f9b2014-07-22 22:44:43 +02004039 /**
4040 * @hide
4041 */
4042 @Override
4043 protected void restoreFromExtras(Bundle extras) {
4044 super.restoreFromExtras(extras);
4045
4046 mTexts.clear();
4047 if (extras.containsKey(EXTRA_TEXT_LINES)) {
4048 Collections.addAll(mTexts, extras.getCharSequenceArray(EXTRA_TEXT_LINES));
4049 }
4050 }
4051
Daniel Sandler879c5e02012-04-17 16:46:51 -04004052 private RemoteViews makeBigContentView() {
Daniel Sandler619738c2012-06-07 16:33:08 -04004053 // Remove the content text so line3 disappears unless you have a summary
Christoph Studer4600f9b2014-07-22 22:44:43 +02004054
4055 // Nasty
4056 CharSequence oldBuilderContentText = mBuilder.mContentText;
Daniel Sandler619738c2012-06-07 16:33:08 -04004057 mBuilder.mContentText = null;
Christoph Studer4600f9b2014-07-22 22:44:43 +02004058
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01004059 RemoteViews contentView = getStandardView(mBuilder.getInboxLayoutResource());
Daniel Sandler619738c2012-06-07 16:33:08 -04004060
Christoph Studer4600f9b2014-07-22 22:44:43 +02004061 mBuilder.mContentText = oldBuilderContentText;
4062
Chris Wrend6297db2012-05-03 16:20:13 -04004063 contentView.setViewVisibility(R.id.text2, View.GONE);
Daniel Sandler879c5e02012-04-17 16:46:51 -04004064
Chris Wrend6297db2012-05-03 16:20:13 -04004065 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 -04004066 R.id.inbox_text4, R.id.inbox_text5, R.id.inbox_text6};
Chris Wrend6297db2012-05-03 16:20:13 -04004067
Chris Wren4ed80d52012-05-17 09:30:03 -04004068 // Make sure all rows are gone in case we reuse a view.
4069 for (int rowId : rowIds) {
4070 contentView.setViewVisibility(rowId, View.GONE);
4071 }
4072
Jorim Jaggi445d3c02014-08-19 22:33:42 +02004073 final boolean largeText =
4074 mBuilder.mContext.getResources().getConfiguration().fontScale > 1f;
4075 final float subTextSize = mBuilder.mContext.getResources().getDimensionPixelSize(
4076 R.dimen.notification_subtext_size);
Daniel Sandler879c5e02012-04-17 16:46:51 -04004077 int i=0;
4078 while (i < mTexts.size() && i < rowIds.length) {
4079 CharSequence str = mTexts.get(i);
4080 if (str != null && !str.equals("")) {
4081 contentView.setViewVisibility(rowIds[i], View.VISIBLE);
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01004082 contentView.setTextViewText(rowIds[i], mBuilder.processLegacyText(str));
Jorim Jaggi445d3c02014-08-19 22:33:42 +02004083 if (largeText) {
4084 contentView.setTextViewTextSize(rowIds[i], TypedValue.COMPLEX_UNIT_PX,
4085 subTextSize);
4086 }
Daniel Sandler879c5e02012-04-17 16:46:51 -04004087 }
4088 i++;
4089 }
4090
Chris Wren683ab002012-09-20 10:35:54 -04004091 contentView.setViewVisibility(R.id.inbox_end_pad,
4092 mTexts.size() > 0 ? View.VISIBLE : View.GONE);
4093
4094 contentView.setViewVisibility(R.id.inbox_more,
4095 mTexts.size() > rowIds.length ? View.VISIBLE : View.GONE);
Chris Wren29bb6d92012-05-17 18:09:42 -04004096
Jorim Jaggi445d3c02014-08-19 22:33:42 +02004097 applyTopPadding(contentView);
4098
Jorim Jaggid05aa3e2014-08-28 17:52:27 +02004099 mBuilder.shrinkLine3Text(contentView);
4100
Kenny Guy98193ea2014-07-24 19:54:37 +01004101 mBuilder.addProfileBadge(contentView, R.id.profile_badge_large_template);
4102
Daniel Sandler879c5e02012-04-17 16:46:51 -04004103 return contentView;
4104 }
4105
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04004106 /**
4107 * @hide
4108 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004109 @Override
Christoph Studer4600f9b2014-07-22 22:44:43 +02004110 public void populateBigContentView(Notification wip) {
4111 mBuilder.setBuilderBigContentView(wip, makeBigContentView());
Daniel Sandler879c5e02012-04-17 16:46:51 -04004112 }
4113 }
Dan Sandler842dd772014-05-15 09:36:47 -04004114
4115 /**
4116 * Notification style for media playback notifications.
4117 *
4118 * In the expanded form, {@link Notification#bigContentView}, up to 5
4119 * {@link Notification.Action}s specified with
4120 * {@link Notification.Builder#addAction(int, CharSequence, PendingIntent) addAction} will be
4121 * shown as icon-only pushbuttons, suitable for transport controls. The Bitmap given to
4122 * {@link Notification.Builder#setLargeIcon(android.graphics.Bitmap) setLargeIcon()} will be
4123 * treated as album artwork.
4124 *
4125 * Unlike the other styles provided here, MediaStyle can also modify the standard-size
4126 * {@link Notification#contentView}; by providing action indices to
Christoph Studerfde6f4d2014-12-12 13:23:26 +01004127 * {@link #setShowActionsInCompactView(int...)} you can promote up to 3 actions to be displayed
Dan Sandler842dd772014-05-15 09:36:47 -04004128 * in the standard view alongside the usual content.
4129 *
Bryan Mawhinney6be8de32014-07-18 10:35:12 +01004130 * Notifications created with MediaStyle will have their category set to
4131 * {@link Notification#CATEGORY_TRANSPORT CATEGORY_TRANSPORT} unless you set a different
4132 * category using {@link Notification.Builder#setCategory(String) setCategory()}.
4133 *
Jeff Browndba34ba2014-06-24 20:46:03 -07004134 * Finally, if you attach a {@link android.media.session.MediaSession.Token} using
4135 * {@link android.app.Notification.MediaStyle#setMediaSession(MediaSession.Token)},
Dan Sandler842dd772014-05-15 09:36:47 -04004136 * the System UI can identify this as a notification representing an active media session
4137 * and respond accordingly (by showing album artwork in the lockscreen, for example).
4138 *
4139 * To use this style with your Notification, feed it to
4140 * {@link Notification.Builder#setStyle(android.app.Notification.Style)} like so:
4141 * <pre class="prettyprint">
4142 * Notification noti = new Notification.Builder()
4143 * .setSmallIcon(R.drawable.ic_stat_player)
Christoph Studere935fe92014-11-24 14:18:06 +01004144 * .setContentTitle(&quot;Track title&quot;)
4145 * .setContentText(&quot;Artist - Album&quot;)
4146 * .setLargeIcon(albumArtBitmap))
Bryan Mawhinney6be8de32014-07-18 10:35:12 +01004147 * .setStyle(<b>new Notification.MediaStyle()</b>
4148 * .setMediaSession(mySession))
Dan Sandler842dd772014-05-15 09:36:47 -04004149 * .build();
4150 * </pre>
4151 *
4152 * @see Notification#bigContentView
4153 */
4154 public static class MediaStyle extends Style {
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02004155 static final int MAX_MEDIA_BUTTONS_IN_COMPACT = 3;
Dan Sandler842dd772014-05-15 09:36:47 -04004156 static final int MAX_MEDIA_BUTTONS = 5;
4157
4158 private int[] mActionsToShowInCompact = null;
Jeff Browndba34ba2014-06-24 20:46:03 -07004159 private MediaSession.Token mToken;
Dan Sandler842dd772014-05-15 09:36:47 -04004160
4161 public MediaStyle() {
4162 }
4163
4164 public MediaStyle(Builder builder) {
4165 setBuilder(builder);
4166 }
4167
4168 /**
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02004169 * 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 -04004170 * notification view.
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02004171 *
4172 * @param actions the indices of the actions to show in the compact notification view
Dan Sandler842dd772014-05-15 09:36:47 -04004173 */
4174 public MediaStyle setShowActionsInCompactView(int...actions) {
4175 mActionsToShowInCompact = actions;
4176 return this;
4177 }
4178
4179 /**
Jeff Browndba34ba2014-06-24 20:46:03 -07004180 * Attach a {@link android.media.session.MediaSession.Token} to this Notification
4181 * to provide additional playback information and control to the SystemUI.
Dan Sandler842dd772014-05-15 09:36:47 -04004182 */
Jeff Browndba34ba2014-06-24 20:46:03 -07004183 public MediaStyle setMediaSession(MediaSession.Token token) {
Dan Sandler842dd772014-05-15 09:36:47 -04004184 mToken = token;
4185 return this;
4186 }
4187
Christoph Studer4600f9b2014-07-22 22:44:43 +02004188 /**
4189 * @hide
4190 */
Dan Sandler842dd772014-05-15 09:36:47 -04004191 @Override
4192 public Notification buildStyled(Notification wip) {
Christoph Studer4600f9b2014-07-22 22:44:43 +02004193 super.buildStyled(wip);
Bryan Mawhinney6be8de32014-07-18 10:35:12 +01004194 if (wip.category == null) {
4195 wip.category = Notification.CATEGORY_TRANSPORT;
4196 }
Dan Sandler842dd772014-05-15 09:36:47 -04004197 return wip;
4198 }
4199
Christoph Studer4600f9b2014-07-22 22:44:43 +02004200 /**
4201 * @hide
4202 */
4203 @Override
4204 public void populateContentView(Notification wip) {
4205 mBuilder.setBuilderContentView(wip, makeMediaContentView());
4206 }
4207
4208 /**
4209 * @hide
4210 */
4211 @Override
4212 public void populateBigContentView(Notification wip) {
4213 mBuilder.setBuilderBigContentView(wip, makeMediaBigContentView());
4214 }
4215
Dan Sandler842dd772014-05-15 09:36:47 -04004216 /** @hide */
4217 @Override
4218 public void addExtras(Bundle extras) {
4219 super.addExtras(extras);
4220
4221 if (mToken != null) {
4222 extras.putParcelable(EXTRA_MEDIA_SESSION, mToken);
4223 }
Bryan Mawhinneye191f902014-07-22 12:50:09 +01004224 if (mActionsToShowInCompact != null) {
4225 extras.putIntArray(EXTRA_COMPACT_ACTIONS, mActionsToShowInCompact);
4226 }
Dan Sandler842dd772014-05-15 09:36:47 -04004227 }
4228
Christoph Studer4600f9b2014-07-22 22:44:43 +02004229 /**
4230 * @hide
4231 */
4232 @Override
4233 protected void restoreFromExtras(Bundle extras) {
4234 super.restoreFromExtras(extras);
4235
4236 if (extras.containsKey(EXTRA_MEDIA_SESSION)) {
4237 mToken = extras.getParcelable(EXTRA_MEDIA_SESSION);
4238 }
4239 if (extras.containsKey(EXTRA_COMPACT_ACTIONS)) {
4240 mActionsToShowInCompact = extras.getIntArray(EXTRA_COMPACT_ACTIONS);
4241 }
4242 }
4243
Dan Sandler842dd772014-05-15 09:36:47 -04004244 private RemoteViews generateMediaActionButton(Action action) {
4245 final boolean tombstone = (action.actionIntent == null);
4246 RemoteViews button = new RemoteViews(mBuilder.mContext.getPackageName(),
Alan Viverette3cb07a462014-06-06 14:19:53 -07004247 R.layout.notification_material_media_action);
Dan Sandler842dd772014-05-15 09:36:47 -04004248 button.setImageViewResource(R.id.action0, action.icon);
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02004249 button.setDrawableParameters(R.id.action0, false, -1,
4250 0xFFFFFFFF,
4251 PorterDuff.Mode.SRC_ATOP, -1);
Dan Sandler842dd772014-05-15 09:36:47 -04004252 if (!tombstone) {
4253 button.setOnClickPendingIntent(R.id.action0, action.actionIntent);
4254 }
4255 button.setContentDescription(R.id.action0, action.title);
4256 return button;
4257 }
4258
4259 private RemoteViews makeMediaContentView() {
4260 RemoteViews view = mBuilder.applyStandardTemplate(
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02004261 R.layout.notification_template_material_media, false /* hasProgress */);
Dan Sandler842dd772014-05-15 09:36:47 -04004262
4263 final int numActions = mBuilder.mActions.size();
4264 final int N = mActionsToShowInCompact == null
4265 ? 0
4266 : Math.min(mActionsToShowInCompact.length, MAX_MEDIA_BUTTONS_IN_COMPACT);
4267 if (N > 0) {
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02004268 view.removeAllViews(com.android.internal.R.id.media_actions);
Dan Sandler842dd772014-05-15 09:36:47 -04004269 for (int i = 0; i < N; i++) {
4270 if (i >= numActions) {
4271 throw new IllegalArgumentException(String.format(
4272 "setShowActionsInCompactView: action %d out of bounds (max %d)",
4273 i, numActions - 1));
4274 }
4275
4276 final Action action = mBuilder.mActions.get(mActionsToShowInCompact[i]);
4277 final RemoteViews button = generateMediaActionButton(action);
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02004278 view.addView(com.android.internal.R.id.media_actions, button);
Dan Sandler842dd772014-05-15 09:36:47 -04004279 }
4280 }
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02004281 styleText(view);
4282 hideRightIcon(view);
Dan Sandler842dd772014-05-15 09:36:47 -04004283 return view;
4284 }
4285
4286 private RemoteViews makeMediaBigContentView() {
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02004287 final int actionCount = Math.min(mBuilder.mActions.size(), MAX_MEDIA_BUTTONS);
4288 RemoteViews big = mBuilder.applyStandardTemplate(getBigLayoutResource(actionCount),
4289 false /* hasProgress */);
Dan Sandler842dd772014-05-15 09:36:47 -04004290
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02004291 if (actionCount > 0) {
4292 big.removeAllViews(com.android.internal.R.id.media_actions);
4293 for (int i = 0; i < actionCount; i++) {
Dan Sandler842dd772014-05-15 09:36:47 -04004294 final RemoteViews button = generateMediaActionButton(mBuilder.mActions.get(i));
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02004295 big.addView(com.android.internal.R.id.media_actions, button);
Dan Sandler842dd772014-05-15 09:36:47 -04004296 }
4297 }
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02004298 styleText(big);
4299 hideRightIcon(big);
4300 applyTopPadding(big);
4301 big.setViewVisibility(android.R.id.progress, View.GONE);
Dan Sandler842dd772014-05-15 09:36:47 -04004302 return big;
4303 }
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02004304
4305 private int getBigLayoutResource(int actionCount) {
4306 if (actionCount <= 3) {
4307 return R.layout.notification_template_material_big_media_narrow;
4308 } else {
4309 return R.layout.notification_template_material_big_media;
4310 }
4311 }
4312
4313 private void hideRightIcon(RemoteViews contentView) {
4314 contentView.setViewVisibility(R.id.right_icon, View.GONE);
4315 }
4316
4317 /**
4318 * Applies the special text colors for media notifications to all text views.
4319 */
4320 private void styleText(RemoteViews contentView) {
4321 int primaryColor = mBuilder.mContext.getResources().getColor(
4322 R.color.notification_media_primary_color);
4323 int secondaryColor = mBuilder.mContext.getResources().getColor(
4324 R.color.notification_media_secondary_color);
4325 contentView.setTextColor(R.id.title, primaryColor);
4326 if (mBuilder.showsTimeOrChronometer()) {
4327 if (mBuilder.mUseChronometer) {
4328 contentView.setTextColor(R.id.chronometer, secondaryColor);
4329 } else {
4330 contentView.setTextColor(R.id.time, secondaryColor);
4331 }
4332 }
4333 contentView.setTextColor(R.id.text2, secondaryColor);
4334 contentView.setTextColor(R.id.text, secondaryColor);
4335 contentView.setTextColor(R.id.info, secondaryColor);
4336 }
4337
4338 /**
4339 * @hide
4340 */
4341 @Override
4342 protected boolean hasProgress() {
4343 return false;
4344 }
Dan Sandler842dd772014-05-15 09:36:47 -04004345 }
Griff Hazen61a9e862014-05-22 16:05:19 -07004346
Christoph Studer4600f9b2014-07-22 22:44:43 +02004347 // When adding a new Style subclass here, don't forget to update
4348 // Builder.getNotificationStyleClass.
4349
Griff Hazen61a9e862014-05-22 16:05:19 -07004350 /**
4351 * Extender interface for use with {@link Builder#extend}. Extenders may be used to add
4352 * metadata or change options on a notification builder.
4353 */
4354 public interface Extender {
4355 /**
4356 * Apply this extender to a notification builder.
4357 * @param builder the builder to be modified.
4358 * @return the build object for chaining.
4359 */
4360 public Builder extend(Builder builder);
4361 }
4362
4363 /**
4364 * Helper class to add wearable extensions to notifications.
4365 * <p class="note"> See
4366 * <a href="{@docRoot}wear/notifications/creating.html">Creating Notifications
4367 * for Android Wear</a> for more information on how to use this class.
4368 * <p>
4369 * To create a notification with wearable extensions:
4370 * <ol>
4371 * <li>Create a {@link android.app.Notification.Builder}, setting any desired
4372 * properties.
4373 * <li>Create a {@link android.app.Notification.WearableExtender}.
4374 * <li>Set wearable-specific properties using the
4375 * {@code add} and {@code set} methods of {@link android.app.Notification.WearableExtender}.
4376 * <li>Call {@link android.app.Notification.Builder#extend} to apply the extensions to a
4377 * notification.
4378 * <li>Post the notification to the notification system with the
4379 * {@code NotificationManager.notify(...)} methods.
4380 * </ol>
4381 *
4382 * <pre class="prettyprint">
4383 * Notification notif = new Notification.Builder(mContext)
4384 * .setContentTitle(&quot;New mail from &quot; + sender.toString())
4385 * .setContentText(subject)
4386 * .setSmallIcon(R.drawable.new_mail)
4387 * .extend(new Notification.WearableExtender()
4388 * .setContentIcon(R.drawable.new_mail))
4389 * .build();
4390 * NotificationManager notificationManger =
4391 * (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
4392 * notificationManger.notify(0, notif);</pre>
4393 *
4394 * <p>Wearable extensions can be accessed on an existing notification by using the
4395 * {@code WearableExtender(Notification)} constructor,
4396 * and then using the {@code get} methods to access values.
4397 *
4398 * <pre class="prettyprint">
4399 * Notification.WearableExtender wearableExtender = new Notification.WearableExtender(
4400 * notification);
Griff Hazen14f57992014-05-26 09:07:14 -07004401 * List&lt;Notification&gt; pages = wearableExtender.getPages();</pre>
Griff Hazen61a9e862014-05-22 16:05:19 -07004402 */
4403 public static final class WearableExtender implements Extender {
4404 /**
4405 * Sentinel value for an action index that is unset.
4406 */
4407 public static final int UNSET_ACTION_INDEX = -1;
4408
4409 /**
4410 * Size value for use with {@link #setCustomSizePreset} to show this notification with
4411 * default sizing.
4412 * <p>For custom display notifications created using {@link #setDisplayIntent},
4413 * the default is {@link #SIZE_LARGE}. All other notifications size automatically based
4414 * on their content.
4415 */
4416 public static final int SIZE_DEFAULT = 0;
4417
4418 /**
4419 * Size value for use with {@link #setCustomSizePreset} to show this notification
4420 * with an extra small size.
4421 * <p>This value is only applicable for custom display notifications created using
4422 * {@link #setDisplayIntent}.
4423 */
4424 public static final int SIZE_XSMALL = 1;
4425
4426 /**
4427 * Size value for use with {@link #setCustomSizePreset} to show this notification
4428 * with a small size.
4429 * <p>This value is only applicable for custom display notifications created using
4430 * {@link #setDisplayIntent}.
4431 */
4432 public static final int SIZE_SMALL = 2;
4433
4434 /**
4435 * Size value for use with {@link #setCustomSizePreset} to show this notification
4436 * with a medium size.
4437 * <p>This value is only applicable for custom display notifications created using
4438 * {@link #setDisplayIntent}.
4439 */
4440 public static final int SIZE_MEDIUM = 3;
4441
4442 /**
4443 * Size value for use with {@link #setCustomSizePreset} to show this notification
4444 * with a large size.
4445 * <p>This value is only applicable for custom display notifications created using
4446 * {@link #setDisplayIntent}.
4447 */
4448 public static final int SIZE_LARGE = 4;
4449
Griff Hazend5f11f92014-05-27 15:40:09 -07004450 /**
4451 * Size value for use with {@link #setCustomSizePreset} to show this notification
4452 * full screen.
4453 * <p>This value is only applicable for custom display notifications created using
4454 * {@link #setDisplayIntent}.
4455 */
4456 public static final int SIZE_FULL_SCREEN = 5;
4457
Griff Hazen5f2edfc2014-09-29 16:28:44 -07004458 /**
4459 * Sentinel value for use with {@link #setHintScreenTimeout} to keep the screen on for a
4460 * short amount of time when this notification is displayed on the screen. This
4461 * is the default value.
4462 */
4463 public static final int SCREEN_TIMEOUT_SHORT = 0;
4464
4465 /**
4466 * Sentinel value for use with {@link #setHintScreenTimeout} to keep the screen on
4467 * for a longer amount of time when this notification is displayed on the screen.
4468 */
4469 public static final int SCREEN_TIMEOUT_LONG = -1;
4470
Griff Hazen61a9e862014-05-22 16:05:19 -07004471 /** Notification extra which contains wearable extensions */
4472 private static final String EXTRA_WEARABLE_EXTENSIONS = "android.wearable.EXTENSIONS";
4473
Pete Gastaf6781d2014-10-07 15:17:05 -04004474 // Keys within EXTRA_WEARABLE_EXTENSIONS for wearable options.
Griff Hazen61a9e862014-05-22 16:05:19 -07004475 private static final String KEY_ACTIONS = "actions";
4476 private static final String KEY_FLAGS = "flags";
4477 private static final String KEY_DISPLAY_INTENT = "displayIntent";
4478 private static final String KEY_PAGES = "pages";
4479 private static final String KEY_BACKGROUND = "background";
4480 private static final String KEY_CONTENT_ICON = "contentIcon";
4481 private static final String KEY_CONTENT_ICON_GRAVITY = "contentIconGravity";
4482 private static final String KEY_CONTENT_ACTION_INDEX = "contentActionIndex";
4483 private static final String KEY_CUSTOM_SIZE_PRESET = "customSizePreset";
4484 private static final String KEY_CUSTOM_CONTENT_HEIGHT = "customContentHeight";
4485 private static final String KEY_GRAVITY = "gravity";
Griff Hazen5f2edfc2014-09-29 16:28:44 -07004486 private static final String KEY_HINT_SCREEN_TIMEOUT = "hintScreenTimeout";
Griff Hazen61a9e862014-05-22 16:05:19 -07004487
4488 // Flags bitwise-ored to mFlags
4489 private static final int FLAG_CONTENT_INTENT_AVAILABLE_OFFLINE = 0x1;
4490 private static final int FLAG_HINT_HIDE_ICON = 1 << 1;
4491 private static final int FLAG_HINT_SHOW_BACKGROUND_ONLY = 1 << 2;
4492 private static final int FLAG_START_SCROLL_BOTTOM = 1 << 3;
Griff Hazen5f2edfc2014-09-29 16:28:44 -07004493 private static final int FLAG_HINT_AVOID_BACKGROUND_CLIPPING = 1 << 4;
Griff Hazen61a9e862014-05-22 16:05:19 -07004494
4495 // Default value for flags integer
4496 private static final int DEFAULT_FLAGS = FLAG_CONTENT_INTENT_AVAILABLE_OFFLINE;
4497
4498 private static final int DEFAULT_CONTENT_ICON_GRAVITY = Gravity.END;
4499 private static final int DEFAULT_GRAVITY = Gravity.BOTTOM;
4500
4501 private ArrayList<Action> mActions = new ArrayList<Action>();
4502 private int mFlags = DEFAULT_FLAGS;
4503 private PendingIntent mDisplayIntent;
4504 private ArrayList<Notification> mPages = new ArrayList<Notification>();
4505 private Bitmap mBackground;
4506 private int mContentIcon;
4507 private int mContentIconGravity = DEFAULT_CONTENT_ICON_GRAVITY;
4508 private int mContentActionIndex = UNSET_ACTION_INDEX;
4509 private int mCustomSizePreset = SIZE_DEFAULT;
4510 private int mCustomContentHeight;
4511 private int mGravity = DEFAULT_GRAVITY;
Griff Hazen5f2edfc2014-09-29 16:28:44 -07004512 private int mHintScreenTimeout;
Griff Hazen61a9e862014-05-22 16:05:19 -07004513
4514 /**
4515 * Create a {@link android.app.Notification.WearableExtender} with default
4516 * options.
4517 */
4518 public WearableExtender() {
4519 }
4520
4521 public WearableExtender(Notification notif) {
4522 Bundle wearableBundle = notif.extras.getBundle(EXTRA_WEARABLE_EXTENSIONS);
4523 if (wearableBundle != null) {
4524 List<Action> actions = wearableBundle.getParcelableArrayList(KEY_ACTIONS);
4525 if (actions != null) {
4526 mActions.addAll(actions);
4527 }
4528
4529 mFlags = wearableBundle.getInt(KEY_FLAGS, DEFAULT_FLAGS);
4530 mDisplayIntent = wearableBundle.getParcelable(KEY_DISPLAY_INTENT);
4531
4532 Notification[] pages = getNotificationArrayFromBundle(
4533 wearableBundle, KEY_PAGES);
4534 if (pages != null) {
4535 Collections.addAll(mPages, pages);
4536 }
4537
4538 mBackground = wearableBundle.getParcelable(KEY_BACKGROUND);
4539 mContentIcon = wearableBundle.getInt(KEY_CONTENT_ICON);
4540 mContentIconGravity = wearableBundle.getInt(KEY_CONTENT_ICON_GRAVITY,
4541 DEFAULT_CONTENT_ICON_GRAVITY);
4542 mContentActionIndex = wearableBundle.getInt(KEY_CONTENT_ACTION_INDEX,
4543 UNSET_ACTION_INDEX);
4544 mCustomSizePreset = wearableBundle.getInt(KEY_CUSTOM_SIZE_PRESET,
4545 SIZE_DEFAULT);
4546 mCustomContentHeight = wearableBundle.getInt(KEY_CUSTOM_CONTENT_HEIGHT);
4547 mGravity = wearableBundle.getInt(KEY_GRAVITY, DEFAULT_GRAVITY);
Griff Hazen5f2edfc2014-09-29 16:28:44 -07004548 mHintScreenTimeout = wearableBundle.getInt(KEY_HINT_SCREEN_TIMEOUT);
Griff Hazen61a9e862014-05-22 16:05:19 -07004549 }
4550 }
4551
4552 /**
4553 * Apply wearable extensions to a notification that is being built. This is typically
4554 * called by the {@link android.app.Notification.Builder#extend} method of
4555 * {@link android.app.Notification.Builder}.
4556 */
4557 @Override
4558 public Notification.Builder extend(Notification.Builder builder) {
4559 Bundle wearableBundle = new Bundle();
4560
4561 if (!mActions.isEmpty()) {
4562 wearableBundle.putParcelableArrayList(KEY_ACTIONS, mActions);
4563 }
4564 if (mFlags != DEFAULT_FLAGS) {
4565 wearableBundle.putInt(KEY_FLAGS, mFlags);
4566 }
4567 if (mDisplayIntent != null) {
4568 wearableBundle.putParcelable(KEY_DISPLAY_INTENT, mDisplayIntent);
4569 }
4570 if (!mPages.isEmpty()) {
4571 wearableBundle.putParcelableArray(KEY_PAGES, mPages.toArray(
4572 new Notification[mPages.size()]));
4573 }
4574 if (mBackground != null) {
4575 wearableBundle.putParcelable(KEY_BACKGROUND, mBackground);
4576 }
4577 if (mContentIcon != 0) {
4578 wearableBundle.putInt(KEY_CONTENT_ICON, mContentIcon);
4579 }
4580 if (mContentIconGravity != DEFAULT_CONTENT_ICON_GRAVITY) {
4581 wearableBundle.putInt(KEY_CONTENT_ICON_GRAVITY, mContentIconGravity);
4582 }
4583 if (mContentActionIndex != UNSET_ACTION_INDEX) {
4584 wearableBundle.putInt(KEY_CONTENT_ACTION_INDEX,
4585 mContentActionIndex);
4586 }
4587 if (mCustomSizePreset != SIZE_DEFAULT) {
4588 wearableBundle.putInt(KEY_CUSTOM_SIZE_PRESET, mCustomSizePreset);
4589 }
4590 if (mCustomContentHeight != 0) {
4591 wearableBundle.putInt(KEY_CUSTOM_CONTENT_HEIGHT, mCustomContentHeight);
4592 }
4593 if (mGravity != DEFAULT_GRAVITY) {
4594 wearableBundle.putInt(KEY_GRAVITY, mGravity);
4595 }
Griff Hazen5f2edfc2014-09-29 16:28:44 -07004596 if (mHintScreenTimeout != 0) {
4597 wearableBundle.putInt(KEY_HINT_SCREEN_TIMEOUT, mHintScreenTimeout);
4598 }
Griff Hazen61a9e862014-05-22 16:05:19 -07004599
4600 builder.getExtras().putBundle(EXTRA_WEARABLE_EXTENSIONS, wearableBundle);
4601 return builder;
4602 }
4603
4604 @Override
4605 public WearableExtender clone() {
4606 WearableExtender that = new WearableExtender();
4607 that.mActions = new ArrayList<Action>(this.mActions);
4608 that.mFlags = this.mFlags;
4609 that.mDisplayIntent = this.mDisplayIntent;
4610 that.mPages = new ArrayList<Notification>(this.mPages);
4611 that.mBackground = this.mBackground;
4612 that.mContentIcon = this.mContentIcon;
4613 that.mContentIconGravity = this.mContentIconGravity;
4614 that.mContentActionIndex = this.mContentActionIndex;
4615 that.mCustomSizePreset = this.mCustomSizePreset;
4616 that.mCustomContentHeight = this.mCustomContentHeight;
4617 that.mGravity = this.mGravity;
Griff Hazen5f2edfc2014-09-29 16:28:44 -07004618 that.mHintScreenTimeout = this.mHintScreenTimeout;
Griff Hazen61a9e862014-05-22 16:05:19 -07004619 return that;
4620 }
4621
4622 /**
4623 * Add a wearable action to this notification.
4624 *
4625 * <p>When wearable actions are added using this method, the set of actions that
4626 * show on a wearable device splits from devices that only show actions added
4627 * using {@link android.app.Notification.Builder#addAction}. This allows for customization
4628 * of which actions display on different devices.
4629 *
4630 * @param action the action to add to this notification
4631 * @return this object for method chaining
4632 * @see android.app.Notification.Action
4633 */
4634 public WearableExtender addAction(Action action) {
4635 mActions.add(action);
4636 return this;
4637 }
4638
4639 /**
4640 * Adds wearable actions to this notification.
4641 *
4642 * <p>When wearable actions are added using this method, the set of actions that
4643 * show on a wearable device splits from devices that only show actions added
4644 * using {@link android.app.Notification.Builder#addAction}. This allows for customization
4645 * of which actions display on different devices.
4646 *
4647 * @param actions the actions to add to this notification
4648 * @return this object for method chaining
4649 * @see android.app.Notification.Action
4650 */
4651 public WearableExtender addActions(List<Action> actions) {
4652 mActions.addAll(actions);
4653 return this;
4654 }
4655
4656 /**
4657 * Clear all wearable actions present on this builder.
4658 * @return this object for method chaining.
4659 * @see #addAction
4660 */
4661 public WearableExtender clearActions() {
4662 mActions.clear();
4663 return this;
4664 }
4665
4666 /**
4667 * Get the wearable actions present on this notification.
4668 */
4669 public List<Action> getActions() {
4670 return mActions;
4671 }
4672
4673 /**
4674 * Set an intent to launch inside of an activity view when displaying
Griff Hazen14f57992014-05-26 09:07:14 -07004675 * this notification. The {@link PendingIntent} provided should be for an activity.
4676 *
4677 * <pre class="prettyprint">
4678 * Intent displayIntent = new Intent(context, MyDisplayActivity.class);
4679 * PendingIntent displayPendingIntent = PendingIntent.getActivity(context,
4680 * 0, displayIntent, PendingIntent.FLAG_UPDATE_CURRENT);
4681 * Notification notif = new Notification.Builder(context)
4682 * .extend(new Notification.WearableExtender()
4683 * .setDisplayIntent(displayPendingIntent)
4684 * .setCustomSizePreset(Notification.WearableExtender.SIZE_MEDIUM))
4685 * .build();</pre>
4686 *
4687 * <p>The activity to launch needs to allow embedding, must be exported, and
Griff Hazen831ca9d2014-06-17 00:38:38 -07004688 * should have an empty task affinity. It is also recommended to use the device
4689 * default light theme.
Griff Hazen14f57992014-05-26 09:07:14 -07004690 *
4691 * <p>Example AndroidManifest.xml entry:
4692 * <pre class="prettyprint">
4693 * &lt;activity android:name=&quot;com.example.MyDisplayActivity&quot;
4694 * android:exported=&quot;true&quot;
4695 * android:allowEmbedded=&quot;true&quot;
Griff Hazen831ca9d2014-06-17 00:38:38 -07004696 * android:taskAffinity=&quot;&quot;
4697 * android:theme=&quot;@android:style/Theme.DeviceDefault.Light&quot; /&gt;</pre>
Griff Hazen61a9e862014-05-22 16:05:19 -07004698 *
4699 * @param intent the {@link PendingIntent} for an activity
4700 * @return this object for method chaining
4701 * @see android.app.Notification.WearableExtender#getDisplayIntent
4702 */
4703 public WearableExtender setDisplayIntent(PendingIntent intent) {
4704 mDisplayIntent = intent;
4705 return this;
4706 }
4707
4708 /**
4709 * Get the intent to launch inside of an activity view when displaying this
4710 * notification. This {@code PendingIntent} should be for an activity.
4711 */
4712 public PendingIntent getDisplayIntent() {
4713 return mDisplayIntent;
4714 }
4715
4716 /**
4717 * Add an additional page of content to display with this notification. The current
4718 * notification forms the first page, and pages added using this function form
4719 * subsequent pages. This field can be used to separate a notification into multiple
4720 * sections.
4721 *
4722 * @param page the notification to add as another page
4723 * @return this object for method chaining
4724 * @see android.app.Notification.WearableExtender#getPages
4725 */
4726 public WearableExtender addPage(Notification page) {
4727 mPages.add(page);
4728 return this;
4729 }
4730
4731 /**
4732 * Add additional pages of content to display with this notification. The current
4733 * notification forms the first page, and pages added using this function form
4734 * subsequent pages. This field can be used to separate a notification into multiple
4735 * sections.
4736 *
4737 * @param pages a list of notifications
4738 * @return this object for method chaining
4739 * @see android.app.Notification.WearableExtender#getPages
4740 */
4741 public WearableExtender addPages(List<Notification> pages) {
4742 mPages.addAll(pages);
4743 return this;
4744 }
4745
4746 /**
4747 * Clear all additional pages present on this builder.
4748 * @return this object for method chaining.
4749 * @see #addPage
4750 */
4751 public WearableExtender clearPages() {
4752 mPages.clear();
4753 return this;
4754 }
4755
4756 /**
4757 * Get the array of additional pages of content for displaying this notification. The
4758 * current notification forms the first page, and elements within this array form
4759 * subsequent pages. This field can be used to separate a notification into multiple
4760 * sections.
4761 * @return the pages for this notification
4762 */
4763 public List<Notification> getPages() {
4764 return mPages;
4765 }
4766
4767 /**
4768 * Set a background image to be displayed behind the notification content.
4769 * Contrary to the {@link android.app.Notification.BigPictureStyle}, this background
4770 * will work with any notification style.
4771 *
4772 * @param background the background bitmap
4773 * @return this object for method chaining
4774 * @see android.app.Notification.WearableExtender#getBackground
4775 */
4776 public WearableExtender setBackground(Bitmap background) {
4777 mBackground = background;
4778 return this;
4779 }
4780
4781 /**
4782 * Get a background image to be displayed behind the notification content.
4783 * Contrary to the {@link android.app.Notification.BigPictureStyle}, this background
4784 * will work with any notification style.
4785 *
4786 * @return the background image
4787 * @see android.app.Notification.WearableExtender#setBackground
4788 */
4789 public Bitmap getBackground() {
4790 return mBackground;
4791 }
4792
4793 /**
4794 * Set an icon that goes with the content of this notification.
4795 */
4796 public WearableExtender setContentIcon(int icon) {
4797 mContentIcon = icon;
4798 return this;
4799 }
4800
4801 /**
4802 * Get an icon that goes with the content of this notification.
4803 */
4804 public int getContentIcon() {
4805 return mContentIcon;
4806 }
4807
4808 /**
4809 * Set the gravity that the content icon should have within the notification display.
4810 * Supported values include {@link android.view.Gravity#START} and
4811 * {@link android.view.Gravity#END}. The default value is {@link android.view.Gravity#END}.
4812 * @see #setContentIcon
4813 */
4814 public WearableExtender setContentIconGravity(int contentIconGravity) {
4815 mContentIconGravity = contentIconGravity;
4816 return this;
4817 }
4818
4819 /**
4820 * Get the gravity that the content icon should have within the notification display.
4821 * Supported values include {@link android.view.Gravity#START} and
4822 * {@link android.view.Gravity#END}. The default value is {@link android.view.Gravity#END}.
4823 * @see #getContentIcon
4824 */
4825 public int getContentIconGravity() {
4826 return mContentIconGravity;
4827 }
4828
4829 /**
4830 * Set an action from this notification's actions to be clickable with the content of
Griff Hazen14f57992014-05-26 09:07:14 -07004831 * this notification. This action will no longer display separately from the
4832 * notification's content.
4833 *
Griff Hazenca48d352014-05-28 22:37:13 -07004834 * <p>For notifications with multiple pages, child pages can also have content actions
Griff Hazen14f57992014-05-26 09:07:14 -07004835 * set, although the list of available actions comes from the main notification and not
4836 * from the child page's notification.
4837 *
4838 * @param actionIndex The index of the action to hoist onto the current notification page.
4839 * If wearable actions were added to the main notification, this index
4840 * will apply to that list, otherwise it will apply to the regular
4841 * actions list.
Griff Hazen61a9e862014-05-22 16:05:19 -07004842 */
4843 public WearableExtender setContentAction(int actionIndex) {
4844 mContentActionIndex = actionIndex;
4845 return this;
4846 }
4847
4848 /**
Griff Hazenca48d352014-05-28 22:37:13 -07004849 * Get the index of the notification action, if any, that was specified as being clickable
4850 * with the content of this notification. This action will no longer display separately
Griff Hazen14f57992014-05-26 09:07:14 -07004851 * from the notification's content.
Griff Hazen61a9e862014-05-22 16:05:19 -07004852 *
Griff Hazenca48d352014-05-28 22:37:13 -07004853 * <p>For notifications with multiple pages, child pages can also have content actions
Griff Hazen14f57992014-05-26 09:07:14 -07004854 * set, although the list of available actions comes from the main notification and not
4855 * from the child page's notification.
4856 *
4857 * <p>If wearable specific actions were added to the main notification, this index will
4858 * apply to that list, otherwise it will apply to the regular actions list.
Griff Hazenca48d352014-05-28 22:37:13 -07004859 *
4860 * @return the action index or {@link #UNSET_ACTION_INDEX} if no action was selected.
Griff Hazen61a9e862014-05-22 16:05:19 -07004861 */
4862 public int getContentAction() {
4863 return mContentActionIndex;
4864 }
4865
4866 /**
4867 * Set the gravity that this notification should have within the available viewport space.
4868 * Supported values include {@link android.view.Gravity#TOP},
4869 * {@link android.view.Gravity#CENTER_VERTICAL} and {@link android.view.Gravity#BOTTOM}.
4870 * The default value is {@link android.view.Gravity#BOTTOM}.
4871 */
4872 public WearableExtender setGravity(int gravity) {
4873 mGravity = gravity;
4874 return this;
4875 }
4876
4877 /**
4878 * Get the gravity that this notification should have within the available viewport space.
4879 * Supported values include {@link android.view.Gravity#TOP},
4880 * {@link android.view.Gravity#CENTER_VERTICAL} and {@link android.view.Gravity#BOTTOM}.
4881 * The default value is {@link android.view.Gravity#BOTTOM}.
4882 */
4883 public int getGravity() {
4884 return mGravity;
4885 }
4886
4887 /**
4888 * Set the custom size preset for the display of this notification out of the available
4889 * presets found in {@link android.app.Notification.WearableExtender}, e.g.
4890 * {@link #SIZE_LARGE}.
4891 * <p>Some custom size presets are only applicable for custom display notifications created
4892 * using {@link android.app.Notification.WearableExtender#setDisplayIntent}. Check the
4893 * documentation for the preset in question. See also
4894 * {@link #setCustomContentHeight} and {@link #getCustomSizePreset}.
4895 */
4896 public WearableExtender setCustomSizePreset(int sizePreset) {
4897 mCustomSizePreset = sizePreset;
4898 return this;
4899 }
4900
4901 /**
4902 * Get the custom size preset for the display of this notification out of the available
4903 * presets found in {@link android.app.Notification.WearableExtender}, e.g.
4904 * {@link #SIZE_LARGE}.
4905 * <p>Some custom size presets are only applicable for custom display notifications created
4906 * using {@link #setDisplayIntent}. Check the documentation for the preset in question.
4907 * See also {@link #setCustomContentHeight} and {@link #setCustomSizePreset}.
4908 */
4909 public int getCustomSizePreset() {
4910 return mCustomSizePreset;
4911 }
4912
4913 /**
4914 * Set the custom height in pixels for the display of this notification's content.
4915 * <p>This option is only available for custom display notifications created
4916 * using {@link android.app.Notification.WearableExtender#setDisplayIntent}. See also
4917 * {@link android.app.Notification.WearableExtender#setCustomSizePreset} and
4918 * {@link #getCustomContentHeight}.
4919 */
4920 public WearableExtender setCustomContentHeight(int height) {
4921 mCustomContentHeight = height;
4922 return this;
4923 }
4924
4925 /**
4926 * Get the custom height in pixels for the display of this notification's content.
4927 * <p>This option is only available for custom display notifications created
4928 * using {@link #setDisplayIntent}. See also {@link #setCustomSizePreset} and
4929 * {@link #setCustomContentHeight}.
4930 */
4931 public int getCustomContentHeight() {
4932 return mCustomContentHeight;
4933 }
4934
4935 /**
4936 * Set whether the scrolling position for the contents of this notification should start
4937 * at the bottom of the contents instead of the top when the contents are too long to
4938 * display within the screen. Default is false (start scroll at the top).
4939 */
4940 public WearableExtender setStartScrollBottom(boolean startScrollBottom) {
4941 setFlag(FLAG_START_SCROLL_BOTTOM, startScrollBottom);
4942 return this;
4943 }
4944
4945 /**
4946 * Get whether the scrolling position for the contents of this notification should start
4947 * at the bottom of the contents instead of the top when the contents are too long to
4948 * display within the screen. Default is false (start scroll at the top).
4949 */
4950 public boolean getStartScrollBottom() {
4951 return (mFlags & FLAG_START_SCROLL_BOTTOM) != 0;
4952 }
4953
4954 /**
4955 * Set whether the content intent is available when the wearable device is not connected
4956 * to a companion device. The user can still trigger this intent when the wearable device
4957 * is offline, but a visual hint will indicate that the content intent may not be available.
4958 * Defaults to true.
4959 */
4960 public WearableExtender setContentIntentAvailableOffline(
4961 boolean contentIntentAvailableOffline) {
4962 setFlag(FLAG_CONTENT_INTENT_AVAILABLE_OFFLINE, contentIntentAvailableOffline);
4963 return this;
4964 }
4965
4966 /**
4967 * Get whether the content intent is available when the wearable device is not connected
4968 * to a companion device. The user can still trigger this intent when the wearable device
4969 * is offline, but a visual hint will indicate that the content intent may not be available.
4970 * Defaults to true.
4971 */
4972 public boolean getContentIntentAvailableOffline() {
4973 return (mFlags & FLAG_CONTENT_INTENT_AVAILABLE_OFFLINE) != 0;
4974 }
4975
4976 /**
4977 * Set a hint that this notification's icon should not be displayed.
4978 * @param hintHideIcon {@code true} to hide the icon, {@code false} otherwise.
4979 * @return this object for method chaining
4980 */
4981 public WearableExtender setHintHideIcon(boolean hintHideIcon) {
4982 setFlag(FLAG_HINT_HIDE_ICON, hintHideIcon);
4983 return this;
4984 }
4985
4986 /**
4987 * Get a hint that this notification's icon should not be displayed.
4988 * @return {@code true} if this icon should not be displayed, false otherwise.
4989 * The default value is {@code false} if this was never set.
4990 */
4991 public boolean getHintHideIcon() {
4992 return (mFlags & FLAG_HINT_HIDE_ICON) != 0;
4993 }
4994
4995 /**
4996 * Set a visual hint that only the background image of this notification should be
4997 * displayed, and other semantic content should be hidden. This hint is only applicable
4998 * to sub-pages added using {@link #addPage}.
4999 */
5000 public WearableExtender setHintShowBackgroundOnly(boolean hintShowBackgroundOnly) {
5001 setFlag(FLAG_HINT_SHOW_BACKGROUND_ONLY, hintShowBackgroundOnly);
5002 return this;
5003 }
5004
5005 /**
5006 * Get a visual hint that only the background image of this notification should be
5007 * displayed, and other semantic content should be hidden. This hint is only applicable
5008 * to sub-pages added using {@link android.app.Notification.WearableExtender#addPage}.
5009 */
5010 public boolean getHintShowBackgroundOnly() {
5011 return (mFlags & FLAG_HINT_SHOW_BACKGROUND_ONLY) != 0;
5012 }
5013
Griff Hazen5f2edfc2014-09-29 16:28:44 -07005014 /**
Griff Hazen9c5be4e2014-11-17 17:47:50 -08005015 * Set a hint that this notification's background should not be clipped if possible,
5016 * and should instead be resized to fully display on the screen, retaining the aspect
5017 * ratio of the image. This can be useful for images like barcodes or qr codes.
Griff Hazen5f2edfc2014-09-29 16:28:44 -07005018 * @param hintAvoidBackgroundClipping {@code true} to avoid clipping if possible.
5019 * @return this object for method chaining
5020 */
5021 public WearableExtender setHintAvoidBackgroundClipping(
5022 boolean hintAvoidBackgroundClipping) {
5023 setFlag(FLAG_HINT_AVOID_BACKGROUND_CLIPPING, hintAvoidBackgroundClipping);
5024 return this;
5025 }
5026
5027 /**
Griff Hazen9c5be4e2014-11-17 17:47:50 -08005028 * Get a hint that this notification's background should not be clipped if possible,
5029 * and should instead be resized to fully display on the screen, retaining the aspect
5030 * ratio of the image. This can be useful for images like barcodes or qr codes.
Griff Hazen5f2edfc2014-09-29 16:28:44 -07005031 * @return {@code true} if it's ok if the background is clipped on the screen, false
5032 * otherwise. The default value is {@code false} if this was never set.
5033 */
5034 public boolean getHintAvoidBackgroundClipping() {
5035 return (mFlags & FLAG_HINT_AVOID_BACKGROUND_CLIPPING) != 0;
5036 }
5037
5038 /**
5039 * Set a hint that the screen should remain on for at least this duration when
5040 * this notification is displayed on the screen.
5041 * @param timeout The requested screen timeout in milliseconds. Can also be either
5042 * {@link #SCREEN_TIMEOUT_SHORT} or {@link #SCREEN_TIMEOUT_LONG}.
5043 * @return this object for method chaining
5044 */
5045 public WearableExtender setHintScreenTimeout(int timeout) {
5046 mHintScreenTimeout = timeout;
5047 return this;
5048 }
5049
5050 /**
5051 * Get the duration, in milliseconds, that the screen should remain on for
5052 * when this notification is displayed.
5053 * @return the duration in milliseconds if > 0, or either one of the sentinel values
5054 * {@link #SCREEN_TIMEOUT_SHORT} or {@link #SCREEN_TIMEOUT_LONG}.
5055 */
5056 public int getHintScreenTimeout() {
5057 return mHintScreenTimeout;
5058 }
5059
Griff Hazen61a9e862014-05-22 16:05:19 -07005060 private void setFlag(int mask, boolean value) {
5061 if (value) {
5062 mFlags |= mask;
5063 } else {
5064 mFlags &= ~mask;
5065 }
5066 }
5067 }
5068
5069 /**
Zhen Yu Song9d5528b2014-11-14 15:34:39 -08005070 * <p>Helper class to add Android Auto extensions to notifications. To create a notification
5071 * with car extensions:
5072 *
5073 * <ol>
5074 * <li>Create an {@link Notification.Builder}, setting any desired
5075 * properties.
5076 * <li>Create a {@link CarExtender}.
5077 * <li>Set car-specific properties using the {@code add} and {@code set} methods of
5078 * {@link CarExtender}.
5079 * <li>Call {@link Notification.Builder#extend(Notification.Extender)}
5080 * to apply the extensions to a notification.
5081 * </ol>
5082 *
5083 * <pre class="prettyprint">
5084 * Notification notification = new Notification.Builder(context)
5085 * ...
5086 * .extend(new CarExtender()
5087 * .set*(...))
5088 * .build();
5089 * </pre>
5090 *
5091 * <p>Car extensions can be accessed on an existing notification by using the
5092 * {@code CarExtender(Notification)} constructor, and then using the {@code get} methods
5093 * to access values.
5094 */
5095 public static final class CarExtender implements Extender {
5096 private static final String TAG = "CarExtender";
5097
5098 private static final String EXTRA_CAR_EXTENDER = "android.car.EXTENSIONS";
5099 private static final String EXTRA_LARGE_ICON = "large_icon";
5100 private static final String EXTRA_CONVERSATION = "car_conversation";
5101 private static final String EXTRA_COLOR = "app_color";
5102
5103 private Bitmap mLargeIcon;
5104 private UnreadConversation mUnreadConversation;
5105 private int mColor = Notification.COLOR_DEFAULT;
5106
5107 /**
5108 * Create a {@link CarExtender} with default options.
5109 */
5110 public CarExtender() {
5111 }
5112
5113 /**
5114 * Create a {@link CarExtender} from the CarExtender options of an existing Notification.
5115 *
5116 * @param notif The notification from which to copy options.
5117 */
5118 public CarExtender(Notification notif) {
5119 Bundle carBundle = notif.extras == null ?
5120 null : notif.extras.getBundle(EXTRA_CAR_EXTENDER);
5121 if (carBundle != null) {
5122 mLargeIcon = carBundle.getParcelable(EXTRA_LARGE_ICON);
5123 mColor = carBundle.getInt(EXTRA_COLOR, Notification.COLOR_DEFAULT);
5124
5125 Bundle b = carBundle.getBundle(EXTRA_CONVERSATION);
5126 mUnreadConversation = UnreadConversation.getUnreadConversationFromBundle(b);
5127 }
5128 }
5129
5130 /**
5131 * Apply car extensions to a notification that is being built. This is typically called by
5132 * the {@link Notification.Builder#extend(Notification.Extender)}
5133 * method of {@link Notification.Builder}.
5134 */
5135 @Override
5136 public Notification.Builder extend(Notification.Builder builder) {
5137 Bundle carExtensions = new Bundle();
5138
5139 if (mLargeIcon != null) {
5140 carExtensions.putParcelable(EXTRA_LARGE_ICON, mLargeIcon);
5141 }
5142 if (mColor != Notification.COLOR_DEFAULT) {
5143 carExtensions.putInt(EXTRA_COLOR, mColor);
5144 }
5145
5146 if (mUnreadConversation != null) {
5147 Bundle b = mUnreadConversation.getBundleForUnreadConversation();
5148 carExtensions.putBundle(EXTRA_CONVERSATION, b);
5149 }
5150
5151 builder.getExtras().putBundle(EXTRA_CAR_EXTENDER, carExtensions);
5152 return builder;
5153 }
5154
5155 /**
5156 * Sets the accent color to use when Android Auto presents the notification.
5157 *
5158 * Android Auto uses the color set with {@link Notification.Builder#setColor(int)}
5159 * to accent the displayed notification. However, not all colors are acceptable in an
5160 * automotive setting. This method can be used to override the color provided in the
5161 * notification in such a situation.
5162 */
5163 public CarExtender setColor(int color) {
5164 mColor = color;
5165 return this;
5166 }
5167
5168 /**
5169 * Gets the accent color.
5170 *
5171 * @see setColor
5172 */
5173 public int getColor() {
5174 return mColor;
5175 }
5176
5177 /**
5178 * Sets the large icon of the car notification.
5179 *
5180 * If no large icon is set in the extender, Android Auto will display the icon
5181 * specified by {@link Notification.Builder#setLargeIcon(android.graphics.Bitmap)}
5182 *
5183 * @param largeIcon The large icon to use in the car notification.
5184 * @return This object for method chaining.
5185 */
5186 public CarExtender setLargeIcon(Bitmap largeIcon) {
5187 mLargeIcon = largeIcon;
5188 return this;
5189 }
5190
5191 /**
5192 * Gets the large icon used in this car notification, or null if no icon has been set.
5193 *
5194 * @return The large icon for the car notification.
5195 * @see CarExtender#setLargeIcon
5196 */
5197 public Bitmap getLargeIcon() {
5198 return mLargeIcon;
5199 }
5200
5201 /**
5202 * Sets the unread conversation in a message notification.
5203 *
5204 * @param unreadConversation The unread part of the conversation this notification conveys.
5205 * @return This object for method chaining.
5206 */
5207 public CarExtender setUnreadConversation(UnreadConversation unreadConversation) {
5208 mUnreadConversation = unreadConversation;
5209 return this;
5210 }
5211
5212 /**
5213 * Returns the unread conversation conveyed by this notification.
5214 * @see #setUnreadConversation(UnreadConversation)
5215 */
5216 public UnreadConversation getUnreadConversation() {
5217 return mUnreadConversation;
5218 }
5219
5220 /**
5221 * A class which holds the unread messages from a conversation.
5222 */
5223 public static class UnreadConversation {
5224 private static final String KEY_AUTHOR = "author";
5225 private static final String KEY_TEXT = "text";
5226 private static final String KEY_MESSAGES = "messages";
5227 private static final String KEY_REMOTE_INPUT = "remote_input";
5228 private static final String KEY_ON_REPLY = "on_reply";
5229 private static final String KEY_ON_READ = "on_read";
5230 private static final String KEY_PARTICIPANTS = "participants";
5231 private static final String KEY_TIMESTAMP = "timestamp";
5232
5233 private final String[] mMessages;
5234 private final RemoteInput mRemoteInput;
5235 private final PendingIntent mReplyPendingIntent;
5236 private final PendingIntent mReadPendingIntent;
5237 private final String[] mParticipants;
5238 private final long mLatestTimestamp;
5239
5240 UnreadConversation(String[] messages, RemoteInput remoteInput,
5241 PendingIntent replyPendingIntent, PendingIntent readPendingIntent,
5242 String[] participants, long latestTimestamp) {
5243 mMessages = messages;
5244 mRemoteInput = remoteInput;
5245 mReadPendingIntent = readPendingIntent;
5246 mReplyPendingIntent = replyPendingIntent;
5247 mParticipants = participants;
5248 mLatestTimestamp = latestTimestamp;
5249 }
5250
5251 /**
5252 * Gets the list of messages conveyed by this notification.
5253 */
5254 public String[] getMessages() {
5255 return mMessages;
5256 }
5257
5258 /**
5259 * Gets the remote input that will be used to convey the response to a message list, or
5260 * null if no such remote input exists.
5261 */
5262 public RemoteInput getRemoteInput() {
5263 return mRemoteInput;
5264 }
5265
5266 /**
5267 * Gets the pending intent that will be triggered when the user replies to this
5268 * notification.
5269 */
5270 public PendingIntent getReplyPendingIntent() {
5271 return mReplyPendingIntent;
5272 }
5273
5274 /**
5275 * Gets the pending intent that Android Auto will send after it reads aloud all messages
5276 * in this object's message list.
5277 */
5278 public PendingIntent getReadPendingIntent() {
5279 return mReadPendingIntent;
5280 }
5281
5282 /**
5283 * Gets the participants in the conversation.
5284 */
5285 public String[] getParticipants() {
5286 return mParticipants;
5287 }
5288
5289 /**
5290 * Gets the firs participant in the conversation.
5291 */
5292 public String getParticipant() {
5293 return mParticipants.length > 0 ? mParticipants[0] : null;
5294 }
5295
5296 /**
5297 * Gets the timestamp of the conversation.
5298 */
5299 public long getLatestTimestamp() {
5300 return mLatestTimestamp;
5301 }
5302
5303 Bundle getBundleForUnreadConversation() {
5304 Bundle b = new Bundle();
5305 String author = null;
5306 if (mParticipants != null && mParticipants.length > 1) {
5307 author = mParticipants[0];
5308 }
5309 Parcelable[] messages = new Parcelable[mMessages.length];
5310 for (int i = 0; i < messages.length; i++) {
5311 Bundle m = new Bundle();
5312 m.putString(KEY_TEXT, mMessages[i]);
5313 m.putString(KEY_AUTHOR, author);
5314 messages[i] = m;
5315 }
5316 b.putParcelableArray(KEY_MESSAGES, messages);
5317 if (mRemoteInput != null) {
5318 b.putParcelable(KEY_REMOTE_INPUT, mRemoteInput);
5319 }
5320 b.putParcelable(KEY_ON_REPLY, mReplyPendingIntent);
5321 b.putParcelable(KEY_ON_READ, mReadPendingIntent);
5322 b.putStringArray(KEY_PARTICIPANTS, mParticipants);
5323 b.putLong(KEY_TIMESTAMP, mLatestTimestamp);
5324 return b;
5325 }
5326
5327 static UnreadConversation getUnreadConversationFromBundle(Bundle b) {
5328 if (b == null) {
5329 return null;
5330 }
5331 Parcelable[] parcelableMessages = b.getParcelableArray(KEY_MESSAGES);
5332 String[] messages = null;
5333 if (parcelableMessages != null) {
5334 String[] tmp = new String[parcelableMessages.length];
5335 boolean success = true;
5336 for (int i = 0; i < tmp.length; i++) {
5337 if (!(parcelableMessages[i] instanceof Bundle)) {
5338 success = false;
5339 break;
5340 }
5341 tmp[i] = ((Bundle) parcelableMessages[i]).getString(KEY_TEXT);
5342 if (tmp[i] == null) {
5343 success = false;
5344 break;
5345 }
5346 }
5347 if (success) {
5348 messages = tmp;
5349 } else {
5350 return null;
5351 }
5352 }
5353
5354 PendingIntent onRead = b.getParcelable(KEY_ON_READ);
5355 PendingIntent onReply = b.getParcelable(KEY_ON_REPLY);
5356
5357 RemoteInput remoteInput = b.getParcelable(KEY_REMOTE_INPUT);
5358
5359 String[] participants = b.getStringArray(KEY_PARTICIPANTS);
5360 if (participants == null || participants.length != 1) {
5361 return null;
5362 }
5363
5364 return new UnreadConversation(messages,
5365 remoteInput,
5366 onReply,
5367 onRead,
5368 participants, b.getLong(KEY_TIMESTAMP));
5369 }
5370 };
5371
5372 /**
5373 * Builder class for {@link CarExtender.UnreadConversation} objects.
5374 */
5375 public static class Builder {
5376 private final List<String> mMessages = new ArrayList<String>();
5377 private final String mParticipant;
5378 private RemoteInput mRemoteInput;
5379 private PendingIntent mReadPendingIntent;
5380 private PendingIntent mReplyPendingIntent;
5381 private long mLatestTimestamp;
5382
5383 /**
5384 * Constructs a new builder for {@link CarExtender.UnreadConversation}.
5385 *
5386 * @param name The name of the other participant in the conversation.
5387 */
5388 public Builder(String name) {
5389 mParticipant = name;
5390 }
5391
5392 /**
5393 * Appends a new unread message to the list of messages for this conversation.
5394 *
5395 * The messages should be added from oldest to newest.
5396 *
5397 * @param message The text of the new unread message.
5398 * @return This object for method chaining.
5399 */
5400 public Builder addMessage(String message) {
5401 mMessages.add(message);
5402 return this;
5403 }
5404
5405 /**
5406 * Sets the pending intent and remote input which will convey the reply to this
5407 * notification.
5408 *
5409 * @param pendingIntent The pending intent which will be triggered on a reply.
5410 * @param remoteInput The remote input parcelable which will carry the reply.
5411 * @return This object for method chaining.
5412 *
5413 * @see CarExtender.UnreadConversation#getRemoteInput
5414 * @see CarExtender.UnreadConversation#getReplyPendingIntent
5415 */
5416 public Builder setReplyAction(
5417 PendingIntent pendingIntent, RemoteInput remoteInput) {
5418 mRemoteInput = remoteInput;
5419 mReplyPendingIntent = pendingIntent;
5420
5421 return this;
5422 }
5423
5424 /**
5425 * Sets the pending intent that will be sent once the messages in this notification
5426 * are read.
5427 *
5428 * @param pendingIntent The pending intent to use.
5429 * @return This object for method chaining.
5430 */
5431 public Builder setReadPendingIntent(PendingIntent pendingIntent) {
5432 mReadPendingIntent = pendingIntent;
5433 return this;
5434 }
5435
5436 /**
5437 * Sets the timestamp of the most recent message in an unread conversation.
5438 *
5439 * If a messaging notification has been posted by your application and has not
5440 * yet been cancelled, posting a later notification with the same id and tag
5441 * but without a newer timestamp may result in Android Auto not displaying a
5442 * heads up notification for the later notification.
5443 *
5444 * @param timestamp The timestamp of the most recent message in the conversation.
5445 * @return This object for method chaining.
5446 */
5447 public Builder setLatestTimestamp(long timestamp) {
5448 mLatestTimestamp = timestamp;
5449 return this;
5450 }
5451
5452 /**
5453 * Builds a new unread conversation object.
5454 *
5455 * @return The new unread conversation object.
5456 */
5457 public UnreadConversation build() {
5458 String[] messages = mMessages.toArray(new String[mMessages.size()]);
5459 String[] participants = { mParticipant };
5460 return new UnreadConversation(messages, mRemoteInput, mReplyPendingIntent,
5461 mReadPendingIntent, participants, mLatestTimestamp);
5462 }
5463 }
5464 }
5465
5466 /**
Griff Hazen61a9e862014-05-22 16:05:19 -07005467 * Get an array of Notification objects from a parcelable array bundle field.
5468 * Update the bundle to have a typed array so fetches in the future don't need
5469 * to do an array copy.
5470 */
5471 private static Notification[] getNotificationArrayFromBundle(Bundle bundle, String key) {
5472 Parcelable[] array = bundle.getParcelableArray(key);
5473 if (array instanceof Notification[] || array == null) {
5474 return (Notification[]) array;
5475 }
5476 Notification[] typedArray = Arrays.copyOf(array, array.length,
5477 Notification[].class);
5478 bundle.putParcelableArray(key, typedArray);
5479 return typedArray;
5480 }
Christoph Studer4600f9b2014-07-22 22:44:43 +02005481
5482 private static class BuilderRemoteViews extends RemoteViews {
5483 public BuilderRemoteViews(Parcel parcel) {
5484 super(parcel);
5485 }
5486
Kenny Guy77320062014-08-27 21:37:15 +01005487 public BuilderRemoteViews(ApplicationInfo appInfo, int layoutId) {
5488 super(appInfo, layoutId);
Christoph Studer4600f9b2014-07-22 22:44:43 +02005489 }
5490
5491 @Override
5492 public BuilderRemoteViews clone() {
5493 Parcel p = Parcel.obtain();
5494 writeToParcel(p, 0);
5495 p.setDataPosition(0);
5496 BuilderRemoteViews brv = new BuilderRemoteViews(p);
5497 p.recycle();
5498 return brv;
5499 }
5500 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005501}