blob: 4f73170635117499159c32cff6845cf522fb3bf2 [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
2 * Copyright (C) 2007 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package android.app;
18
Tor Norbye80756e32015-03-02 09:39:27 -080019import android.annotation.ColorInt;
Tor Norbye7b9c9122013-05-30 16:48:33 -070020import android.annotation.DrawableRes;
Tor Norbyed9273d62013-05-30 15:59:53 -070021import android.annotation.IntDef;
Daniel Sandler01df1c62014-06-09 10:54:01 -040022import android.annotation.SdkConstant;
23import android.annotation.SdkConstant.SdkConstantType;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080024import android.content.Context;
25import android.content.Intent;
Kenny Guy77320062014-08-27 21:37:15 +010026import android.content.pm.ApplicationInfo;
Christoph Studer4600f9b2014-07-22 22:44:43 +020027import android.content.pm.PackageManager.NameNotFoundException;
Jorim Jaggief72a192014-08-26 21:57:46 +020028import android.content.res.ColorStateList;
Joe Onoratoef1e7762010-09-17 18:38:38 -040029import android.graphics.Bitmap;
Kenny Guy8a0101b2014-05-08 23:34:12 +010030import android.graphics.Canvas;
Jorim Jaggi5c2d8462014-03-21 17:37:00 +010031import android.graphics.PorterDuff;
Kenny Guy8a0101b2014-05-08 23:34:12 +010032import android.graphics.drawable.Drawable;
Dan Sandlerd63f9322015-05-06 15:18:49 -040033import android.graphics.drawable.Icon;
John Spurlockc0650f022014-07-19 13:22:39 -040034import android.media.AudioAttributes;
Jeff Sharkey098d5802012-04-26 17:30:34 -070035import android.media.AudioManager;
Jeff Browndba34ba2014-06-24 20:46:03 -070036import android.media.session.MediaSession;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080037import android.net.Uri;
Daniel Sandlerdcbaf662013-04-26 16:23:09 -040038import android.os.BadParcelableException;
Christoph Studer239f8352014-08-25 15:13:18 +020039import android.os.Build;
Daniel Sandler2561b0b2012-02-13 21:04:12 -050040import android.os.Bundle;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080041import android.os.Parcel;
42import android.os.Parcelable;
Daniel Sandlera2985ed2012-04-03 16:42:00 -040043import android.os.SystemClock;
Jeff Sharkey6d515712012-09-20 16:06:08 -070044import android.os.UserHandle;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080045import android.text.TextUtils;
Daniel Sandlerdcbaf662013-04-26 16:23:09 -040046import android.util.Log;
Jorim Jaggi445d3c02014-08-19 22:33:42 +020047import android.util.MathUtils;
Daniel Sandler9f7936a2012-05-21 16:14:28 -040048import android.util.TypedValue;
Griff Hazen61a9e862014-05-22 16:05:19 -070049import android.view.Gravity;
Joe Onorato8595a3d2010-11-19 18:12:07 -080050import android.view.View;
Jeff Sharkey1c400132011-08-05 14:50:13 -070051import android.widget.ProgressBar;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080052import android.widget.RemoteViews;
53
Griff Hazen959591e2014-05-15 22:26:18 -070054import com.android.internal.R;
Griff Hazenc091ba82014-05-16 10:13:26 -070055import com.android.internal.util.NotificationColorUtil;
Griff Hazen959591e2014-05-15 22:26:18 -070056
Tor Norbyed9273d62013-05-30 15:59:53 -070057import java.lang.annotation.Retention;
58import java.lang.annotation.RetentionPolicy;
Christoph Studer4600f9b2014-07-22 22:44:43 +020059import java.lang.reflect.Constructor;
Andy Stadler110988c2010-12-03 14:29:16 -080060import java.text.NumberFormat;
Daniel Sandler2561b0b2012-02-13 21:04:12 -050061import java.util.ArrayList;
Griff Hazen61a9e862014-05-22 16:05:19 -070062import java.util.Arrays;
Griff Hazen5cadc3b2014-05-20 09:55:39 -070063import java.util.Collections;
Griff Hazen61a9e862014-05-22 16:05:19 -070064import java.util.List;
Joe Onorato561d3852010-11-20 18:09:34 -080065
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080066/**
67 * A class that represents how a persistent notification is to be presented to
68 * the user using the {@link android.app.NotificationManager}.
69 *
Joe Onoratocb109a02011-01-18 17:57:41 -080070 * <p>The {@link Notification.Builder Notification.Builder} has been added to make it
71 * easier to construct Notifications.</p>
72 *
Joe Fernandez558459f2011-10-13 16:47:36 -070073 * <div class="special reference">
74 * <h3>Developer Guides</h3>
75 * <p>For a guide to creating notifications, read the
76 * <a href="{@docRoot}guide/topics/ui/notifiers/notifications.html">Status Bar Notifications</a>
77 * developer guide.</p>
78 * </div>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080079 */
80public class Notification implements Parcelable
81{
Daniel Sandlerdcbaf662013-04-26 16:23:09 -040082 private static final String TAG = "Notification";
83
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080084 /**
Daniel Sandler01df1c62014-06-09 10:54:01 -040085 * An activity that provides a user interface for adjusting notification preferences for its
86 * containing application. Optional but recommended for apps that post
87 * {@link android.app.Notification Notifications}.
88 */
89 @SdkConstant(SdkConstantType.INTENT_CATEGORY)
90 public static final String INTENT_CATEGORY_NOTIFICATION_PREFERENCES
91 = "android.intent.category.NOTIFICATION_PREFERENCES";
92
93 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080094 * Use all default values (where applicable).
95 */
96 public static final int DEFAULT_ALL = ~0;
Daniel Sandler2561b0b2012-02-13 21:04:12 -050097
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080098 /**
99 * Use the default notification sound. This will ignore any given
100 * {@link #sound}.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500101 *
Chris Wren47c20a12014-06-18 17:27:29 -0400102 * <p>
103 * A notification that is noisy is more likely to be presented as a heads-up notification.
104 * </p>
105 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800106 * @see #defaults
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500107 */
108
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800109 public static final int DEFAULT_SOUND = 1;
110
111 /**
112 * Use the default notification vibrate. This will ignore any given
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500113 * {@link #vibrate}. Using phone vibration requires the
Scott Mainb8b36452009-04-26 15:50:49 -0700114 * {@link android.Manifest.permission#VIBRATE VIBRATE} permission.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500115 *
Chris Wren47c20a12014-06-18 17:27:29 -0400116 * <p>
117 * A notification that vibrates is more likely to be presented as a heads-up notification.
118 * </p>
119 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800120 * @see #defaults
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500121 */
122
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800123 public static final int DEFAULT_VIBRATE = 2;
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500124
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800125 /**
126 * Use the default notification lights. This will ignore the
127 * {@link #FLAG_SHOW_LIGHTS} bit, and {@link #ledARGB}, {@link #ledOffMS}, or
128 * {@link #ledOnMS}.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500129 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800130 * @see #defaults
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500131 */
132
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800133 public static final int DEFAULT_LIGHTS = 4;
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500134
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800135 /**
Christoph Studer535ec612014-09-03 15:47:47 +0200136 * Maximum length of CharSequences accepted by Builder and friends.
137 *
138 * <p>
139 * Avoids spamming the system with overly large strings such as full e-mails.
140 */
141 private static final int MAX_CHARSEQUENCE_LENGTH = 5 * 1024;
142
143 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500144 * A timestamp related to this notification, in milliseconds since the epoch.
Joe Malin8d40d042012-11-05 11:36:40 -0800145 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500146 * Default value: {@link System#currentTimeMillis() Now}.
147 *
148 * Choose a timestamp that will be most relevant to the user. For most finite events, this
149 * corresponds to the time the event happened (or will happen, in the case of events that have
150 * yet to occur but about which the user is being informed). Indefinite events should be
Joe Malin8d40d042012-11-05 11:36:40 -0800151 * timestamped according to when the activity began.
152 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500153 * Some examples:
Joe Malin8d40d042012-11-05 11:36:40 -0800154 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500155 * <ul>
156 * <li>Notification of a new chat message should be stamped when the message was received.</li>
157 * <li>Notification of an ongoing file download (with a progress bar, for example) should be stamped when the download started.</li>
158 * <li>Notification of a completed file download should be stamped when the download finished.</li>
159 * <li>Notification of an upcoming meeting should be stamped with the time the meeting will begin (that is, in the future).</li>
160 * <li>Notification of an ongoing stopwatch (increasing timer) should be stamped with the watch's start time.
161 * <li>Notification of an ongoing countdown timer should be stamped with the timer's end time.
Joe Malin8d40d042012-11-05 11:36:40 -0800162 * </ul>
163 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800164 */
165 public long when;
166
167 /**
168 * The resource id of a drawable to use as the icon in the status bar.
Dan Sandler86647982015-05-13 23:41:13 -0400169 *
170 * @deprecated Use {@link Builder#setSmallIcon(Icon)} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800171 */
Dan Sandler86647982015-05-13 23:41:13 -0400172 @Deprecated
Tor Norbye7b9c9122013-05-30 16:48:33 -0700173 @DrawableRes
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800174 public int icon;
175
176 /**
Joe Onorato46439ce2010-11-19 13:56:21 -0800177 * If the icon in the status bar is to have more than one level, you can set this. Otherwise,
178 * leave it at its default value of 0.
179 *
180 * @see android.widget.ImageView#setImageLevel
Griff Hazen959591e2014-05-15 22:26:18 -0700181 * @see android.graphics.drawable.Drawable#setLevel
Joe Onorato46439ce2010-11-19 13:56:21 -0800182 */
183 public int iconLevel;
184
185 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500186 * The number of events that this notification represents. For example, in a new mail
187 * notification, this could be the number of unread messages.
Joe Malin8d40d042012-11-05 11:36:40 -0800188 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500189 * The system may or may not use this field to modify the appearance of the notification. For
190 * example, before {@link android.os.Build.VERSION_CODES#HONEYCOMB}, this number was
191 * superimposed over the icon in the status bar. Starting with
192 * {@link android.os.Build.VERSION_CODES#HONEYCOMB}, the template used by
193 * {@link Notification.Builder} has displayed the number in the expanded notification view.
Joe Malin8d40d042012-11-05 11:36:40 -0800194 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500195 * If the number is 0 or negative, it is never shown.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800196 */
197 public int number;
198
199 /**
200 * The intent to execute when the expanded status entry is clicked. If
201 * this is an activity, it must include the
202 * {@link android.content.Intent#FLAG_ACTIVITY_NEW_TASK} flag, which requires
Scott Main7aee61f2011-02-08 11:25:01 -0800203 * that you take care of task management as described in the
204 * <a href="{@docRoot}guide/topics/fundamentals/tasks-and-back-stack.html">Tasks and Back
Dianne Hackborn6ceca582012-01-10 15:24:26 -0800205 * Stack</a> document. In particular, make sure to read the notification section
206 * <a href="{@docRoot}guide/topics/ui/notifiers/notifications.html#HandlingNotifications">Handling
207 * Notifications</a> for the correct ways to launch an application from a
208 * notification.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800209 */
210 public PendingIntent contentIntent;
211
212 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500213 * The intent to execute when the notification is explicitly dismissed by the user, either with
214 * the "Clear All" button or by swiping it away individually.
215 *
216 * This probably shouldn't be launching an activity since several of those will be sent
217 * at the same time.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800218 */
219 public PendingIntent deleteIntent;
220
221 /**
Dianne Hackborn170bae72010-09-03 15:14:28 -0700222 * An intent to launch instead of posting the notification to the status bar.
Joe Onoratocb109a02011-01-18 17:57:41 -0800223 *
Chris Wren47c20a12014-06-18 17:27:29 -0400224 * <p>
225 * The system UI may choose to display a heads-up notification, instead of
226 * launching this intent, while the user is using the device.
227 * </p>
228 *
Joe Onoratocb109a02011-01-18 17:57:41 -0800229 * @see Notification.Builder#setFullScreenIntent
Daniel Sandlere46cbd32010-06-17 10:35:26 -0400230 */
231 public PendingIntent fullScreenIntent;
232
233 /**
Dan Sandler5fcdf6e2014-07-18 11:31:15 -0400234 * Text that summarizes this notification for accessibility services.
235 *
236 * As of the L release, this text is no longer shown on screen, but it is still useful to
237 * accessibility services (where it serves as an audible announcement of the notification's
238 * appearance).
Joe Onoratoef1e7762010-09-17 18:38:38 -0400239 *
Joe Onorato46439ce2010-11-19 13:56:21 -0800240 * @see #tickerView
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800241 */
242 public CharSequence tickerText;
243
244 /**
Dan Sandler5fcdf6e2014-07-18 11:31:15 -0400245 * Formerly, a view showing the {@link #tickerText}.
246 *
247 * No longer displayed in the status bar as of API 21.
Joe Onoratoef1e7762010-09-17 18:38:38 -0400248 */
Dan Sandler5fcdf6e2014-07-18 11:31:15 -0400249 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -0800250 public RemoteViews tickerView;
Joe Onoratoef1e7762010-09-17 18:38:38 -0400251
252 /**
Daniel Sandlere46cbd32010-06-17 10:35:26 -0400253 * The view that will represent this notification in the expanded status bar.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800254 */
255 public RemoteViews contentView;
256
Daniel Sandlera0a938c2012-03-15 08:42:37 -0400257 /**
Daniel Sandler4dfbe832012-04-11 14:51:46 -0400258 * A large-format version of {@link #contentView}, giving the Notification an
Daniel Sandlerf3b73432012-03-27 15:01:25 -0400259 * opportunity to show more detail. The system UI may choose to show this
260 * instead of the normal content view at its discretion.
Daniel Sandlerf3b73432012-03-27 15:01:25 -0400261 */
262 public RemoteViews bigContentView;
263
Chris Wren8fd39ec2014-02-27 17:43:26 -0500264
265 /**
Chris Wren47c20a12014-06-18 17:27:29 -0400266 * A medium-format version of {@link #contentView}, providing the Notification an
267 * opportunity to add action buttons to contentView. At its discretion, the system UI may
268 * choose to show this as a heads-up notification, which will pop up so the user can see
269 * it without leaving their current activity.
Chris Wren8fd39ec2014-02-27 17:43:26 -0500270 */
271 public RemoteViews headsUpContentView;
272
Daniel Sandlerf3b73432012-03-27 15:01:25 -0400273 /**
Dan Sandler86647982015-05-13 23:41:13 -0400274 * A large bitmap to be shown in the notification content area.
275 *
276 * @deprecated Use {@link Builder#setLargeIcon(Icon)} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800277 */
Dan Sandler86647982015-05-13 23:41:13 -0400278 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -0800279 public Bitmap largeIcon;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800280
281 /**
282 * The sound to play.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500283 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800284 * <p>
Chris Wren47c20a12014-06-18 17:27:29 -0400285 * A notification that is noisy is more likely to be presented as a heads-up notification.
286 * </p>
287 *
288 * <p>
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500289 * To play the default notification sound, see {@link #defaults}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800290 * </p>
291 */
292 public Uri sound;
293
294 /**
295 * Use this constant as the value for audioStreamType to request that
296 * the default stream type for notifications be used. Currently the
Jeff Sharkey098d5802012-04-26 17:30:34 -0700297 * default stream type is {@link AudioManager#STREAM_NOTIFICATION}.
John Spurlockc0650f022014-07-19 13:22:39 -0400298 *
299 * @deprecated Use {@link #audioAttributes} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800300 */
Jean-Michel Trivi81f871e2014-08-06 16:32:38 -0700301 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800302 public static final int STREAM_DEFAULT = -1;
303
304 /**
305 * The audio stream type to use when playing the sound.
306 * Should be one of the STREAM_ constants from
307 * {@link android.media.AudioManager}.
John Spurlockc0650f022014-07-19 13:22:39 -0400308 *
309 * @deprecated Use {@link #audioAttributes} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800310 */
Jean-Michel Trivi81f871e2014-08-06 16:32:38 -0700311 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800312 public int audioStreamType = STREAM_DEFAULT;
313
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800314 /**
John Spurlockc0650f022014-07-19 13:22:39 -0400315 * The default value of {@link #audioAttributes}.
316 */
317 public static final AudioAttributes AUDIO_ATTRIBUTES_DEFAULT = new AudioAttributes.Builder()
318 .setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
319 .setUsage(AudioAttributes.USAGE_NOTIFICATION)
320 .build();
321
322 /**
323 * The {@link AudioAttributes audio attributes} to use when playing the sound.
324 */
325 public AudioAttributes audioAttributes = AUDIO_ATTRIBUTES_DEFAULT;
326
327 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500328 * The pattern with which to vibrate.
329 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800330 * <p>
331 * To vibrate the default pattern, see {@link #defaults}.
332 * </p>
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500333 *
Chris Wren47c20a12014-06-18 17:27:29 -0400334 * <p>
335 * A notification that vibrates is more likely to be presented as a heads-up notification.
336 * </p>
337 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800338 * @see android.os.Vibrator#vibrate(long[],int)
339 */
340 public long[] vibrate;
341
342 /**
343 * The color of the led. The hardware will do its best approximation.
344 *
345 * @see #FLAG_SHOW_LIGHTS
346 * @see #flags
347 */
Tor Norbye80756e32015-03-02 09:39:27 -0800348 @ColorInt
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800349 public int ledARGB;
350
351 /**
352 * The number of milliseconds for the LED to be on while it's flashing.
353 * The hardware will do its best approximation.
354 *
355 * @see #FLAG_SHOW_LIGHTS
356 * @see #flags
357 */
358 public int ledOnMS;
359
360 /**
361 * The number of milliseconds for the LED to be off while it's flashing.
362 * The hardware will do its best approximation.
363 *
364 * @see #FLAG_SHOW_LIGHTS
365 * @see #flags
366 */
367 public int ledOffMS;
368
369 /**
370 * Specifies which values should be taken from the defaults.
371 * <p>
372 * To set, OR the desired from {@link #DEFAULT_SOUND},
373 * {@link #DEFAULT_VIBRATE}, {@link #DEFAULT_LIGHTS}. For all default
374 * values, use {@link #DEFAULT_ALL}.
375 * </p>
376 */
377 public int defaults;
378
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800379 /**
380 * Bit to be bitwise-ored into the {@link #flags} field that should be
381 * set if you want the LED on for this notification.
382 * <ul>
383 * <li>To turn the LED off, pass 0 in the alpha channel for colorARGB
384 * or 0 for both ledOnMS and ledOffMS.</li>
385 * <li>To turn the LED on, pass 1 for ledOnMS and 0 for ledOffMS.</li>
386 * <li>To flash the LED, pass the number of milliseconds that it should
387 * be on and off to ledOnMS and ledOffMS.</li>
388 * </ul>
389 * <p>
390 * Since hardware varies, you are not guaranteed that any of the values
391 * you pass are honored exactly. Use the system defaults (TODO) if possible
392 * because they will be set to values that work on any given hardware.
393 * <p>
394 * The alpha channel must be set for forward compatibility.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500395 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800396 */
397 public static final int FLAG_SHOW_LIGHTS = 0x00000001;
398
399 /**
400 * Bit to be bitwise-ored into the {@link #flags} field that should be
401 * set if this notification is in reference to something that is ongoing,
402 * like a phone call. It should not be set if this notification is in
403 * reference to something that happened at a particular point in time,
404 * like a missed phone call.
405 */
406 public static final int FLAG_ONGOING_EVENT = 0x00000002;
407
408 /**
409 * Bit to be bitwise-ored into the {@link #flags} field that if set,
Scott Mainb8b36452009-04-26 15:50:49 -0700410 * the audio will be repeated until the notification is
411 * cancelled or the notification window is opened.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800412 */
413 public static final int FLAG_INSISTENT = 0x00000004;
414
415 /**
416 * Bit to be bitwise-ored into the {@link #flags} field that should be
Griff Hazen293977b2014-04-28 08:37:20 -0700417 * set if you would only like the sound, vibrate and ticker to be played
418 * if the notification was not already showing.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800419 */
420 public static final int FLAG_ONLY_ALERT_ONCE = 0x00000008;
421
422 /**
423 * Bit to be bitwise-ored into the {@link #flags} field that should be
424 * set if the notification should be canceled when it is clicked by the
Daniel Sandler8aa9ae62012-12-04 23:31:47 -0500425 * user.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800426 */
427 public static final int FLAG_AUTO_CANCEL = 0x00000010;
428
429 /**
430 * Bit to be bitwise-ored into the {@link #flags} field that should be
431 * set if the notification should not be canceled when the user clicks
432 * the Clear all button.
433 */
434 public static final int FLAG_NO_CLEAR = 0x00000020;
435
Dianne Hackbornd8a43f62009-08-17 23:33:56 -0700436 /**
437 * Bit to be bitwise-ored into the {@link #flags} field that should be
438 * set if this notification represents a currently running service. This
439 * will normally be set for you by {@link Service#startForeground}.
440 */
441 public static final int FLAG_FOREGROUND_SERVICE = 0x00000040;
442
Daniel Sandlere46cbd32010-06-17 10:35:26 -0400443 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500444 * Obsolete flag indicating high-priority notifications; use the priority field instead.
Joe Malin8d40d042012-11-05 11:36:40 -0800445 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500446 * @deprecated Use {@link #priority} with a positive value.
Daniel Sandlere46cbd32010-06-17 10:35:26 -0400447 */
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500448 public static final int FLAG_HIGH_PRIORITY = 0x00000080;
Daniel Sandlere46cbd32010-06-17 10:35:26 -0400449
Griff Hazendfcb0802014-02-11 12:00:00 -0800450 /**
451 * Bit to be bitswise-ored into the {@link #flags} field that should be
452 * set if this notification is relevant to the current device only
453 * and it is not recommended that it bridge to other devices.
454 */
455 public static final int FLAG_LOCAL_ONLY = 0x00000100;
456
Griff Hazen5cadc3b2014-05-20 09:55:39 -0700457 /**
458 * Bit to be bitswise-ored into the {@link #flags} field that should be
459 * set if this notification is the group summary for a group of notifications.
460 * Grouped notifications may display in a cluster or stack on devices which
461 * support such rendering. Requires a group key also be set using {@link Builder#setGroup}.
462 */
463 public static final int FLAG_GROUP_SUMMARY = 0x00000200;
464
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800465 public int flags;
466
Tor Norbyed9273d62013-05-30 15:59:53 -0700467 /** @hide */
468 @IntDef({PRIORITY_DEFAULT,PRIORITY_LOW,PRIORITY_MIN,PRIORITY_HIGH,PRIORITY_MAX})
469 @Retention(RetentionPolicy.SOURCE)
470 public @interface Priority {}
471
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800472 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500473 * Default notification {@link #priority}. If your application does not prioritize its own
474 * notifications, use this value for all notifications.
475 */
476 public static final int PRIORITY_DEFAULT = 0;
477
478 /**
479 * Lower {@link #priority}, for items that are less important. The UI may choose to show these
480 * items smaller, or at a different position in the list, compared with your app's
481 * {@link #PRIORITY_DEFAULT} items.
482 */
483 public static final int PRIORITY_LOW = -1;
484
485 /**
486 * Lowest {@link #priority}; these items might not be shown to the user except under special
487 * circumstances, such as detailed notification logs.
488 */
489 public static final int PRIORITY_MIN = -2;
490
491 /**
492 * Higher {@link #priority}, for more important notifications or alerts. The UI may choose to
493 * show these items larger, or at a different position in notification lists, compared with
494 * your app's {@link #PRIORITY_DEFAULT} items.
495 */
496 public static final int PRIORITY_HIGH = 1;
497
498 /**
499 * Highest {@link #priority}, for your application's most important items that require the
500 * user's prompt attention or input.
501 */
502 public static final int PRIORITY_MAX = 2;
503
504 /**
505 * Relative priority for this notification.
Joe Malin8d40d042012-11-05 11:36:40 -0800506 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500507 * Priority is an indication of how much of the user's valuable attention should be consumed by
508 * this notification. Low-priority notifications may be hidden from the user in certain
509 * situations, while the user might be interrupted for a higher-priority notification. The
Daniel Sandler6738eee2012-11-16 12:03:32 -0500510 * system will make a determination about how to interpret this priority when presenting
511 * the notification.
Chris Wren47c20a12014-06-18 17:27:29 -0400512 *
513 * <p>
514 * A notification that is at least {@link #PRIORITY_HIGH} is more likely to be presented
515 * as a heads-up notification.
516 * </p>
517 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500518 */
Tor Norbyed9273d62013-05-30 15:59:53 -0700519 @Priority
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500520 public int priority;
Joe Malin8d40d042012-11-05 11:36:40 -0800521
Dan Sandler26e81cf2014-05-06 10:01:27 -0400522 /**
523 * Accent color (an ARGB integer like the constants in {@link android.graphics.Color})
524 * to be applied by the standard Style templates when presenting this notification.
525 *
526 * The current template design constructs a colorful header image by overlaying the
527 * {@link #icon} image (stenciled in white) atop a field of this color. Alpha components are
528 * ignored.
529 */
Tor Norbye80756e32015-03-02 09:39:27 -0800530 @ColorInt
Dan Sandler26e81cf2014-05-06 10:01:27 -0400531 public int color = COLOR_DEFAULT;
532
533 /**
534 * Special value of {@link #color} telling the system not to decorate this notification with
535 * any special color but instead use default colors when presenting this notification.
536 */
Tor Norbye80756e32015-03-02 09:39:27 -0800537 @ColorInt
Dan Sandler26e81cf2014-05-06 10:01:27 -0400538 public static final int COLOR_DEFAULT = 0; // AKA Color.TRANSPARENT
Dan Sandler0bf2ed82013-12-21 23:33:41 -0600539
540 /**
541 * Sphere of visibility of this notification, which affects how and when the SystemUI reveals
542 * the notification's presence and contents in untrusted situations (namely, on the secure
543 * lockscreen).
544 *
545 * The default level, {@link #VISIBILITY_PRIVATE}, behaves exactly as notifications have always
546 * done on Android: The notification's {@link #icon} and {@link #tickerText} (if available) are
547 * shown in all situations, but the contents are only available if the device is unlocked for
548 * the appropriate user.
549 *
550 * A more permissive policy can be expressed by {@link #VISIBILITY_PUBLIC}; such a notification
551 * can be read even in an "insecure" context (that is, above a secure lockscreen).
552 * To modify the public version of this notification—for example, to redact some portions—see
553 * {@link Builder#setPublicVersion(Notification)}.
554 *
555 * Finally, a notification can be made {@link #VISIBILITY_SECRET}, which will suppress its icon
556 * and ticker until the user has bypassed the lockscreen.
557 */
558 public int visibility;
559
Griff Hazenfc3922d2014-08-20 11:56:44 -0700560 /**
561 * Notification visibility: Show this notification in its entirety on all lockscreens.
562 *
563 * {@see #visibility}
564 */
Dan Sandler0bf2ed82013-12-21 23:33:41 -0600565 public static final int VISIBILITY_PUBLIC = 1;
Griff Hazenfc3922d2014-08-20 11:56:44 -0700566
567 /**
568 * Notification visibility: Show this notification on all lockscreens, but conceal sensitive or
569 * private information on secure lockscreens.
570 *
571 * {@see #visibility}
572 */
Dan Sandler0bf2ed82013-12-21 23:33:41 -0600573 public static final int VISIBILITY_PRIVATE = 0;
Griff Hazenfc3922d2014-08-20 11:56:44 -0700574
575 /**
576 * Notification visibility: Do not reveal any part of this notification on a secure lockscreen.
577 *
578 * {@see #visibility}
579 */
Dan Sandler0bf2ed82013-12-21 23:33:41 -0600580 public static final int VISIBILITY_SECRET = -1;
581
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500582 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400583 * Notification category: incoming call (voice or video) or similar synchronous communication request.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500584 */
John Spurlockfd7f1e02014-03-18 16:41:57 -0400585 public static final String CATEGORY_CALL = "call";
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500586
587 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400588 * Notification category: incoming direct message (SMS, instant message, etc.).
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500589 */
John Spurlockfd7f1e02014-03-18 16:41:57 -0400590 public static final String CATEGORY_MESSAGE = "msg";
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500591
592 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400593 * Notification category: asynchronous bulk message (email).
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500594 */
John Spurlockfd7f1e02014-03-18 16:41:57 -0400595 public static final String CATEGORY_EMAIL = "email";
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500596
597 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400598 * Notification category: calendar event.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500599 */
John Spurlockfd7f1e02014-03-18 16:41:57 -0400600 public static final String CATEGORY_EVENT = "event";
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500601
602 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400603 * Notification category: promotion or advertisement.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500604 */
John Spurlockfd7f1e02014-03-18 16:41:57 -0400605 public static final String CATEGORY_PROMO = "promo";
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500606
607 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400608 * Notification category: alarm or timer.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500609 */
John Spurlockfd7f1e02014-03-18 16:41:57 -0400610 public static final String CATEGORY_ALARM = "alarm";
611
612 /**
613 * Notification category: progress of a long-running background operation.
614 */
615 public static final String CATEGORY_PROGRESS = "progress";
616
617 /**
618 * Notification category: social network or sharing update.
619 */
620 public static final String CATEGORY_SOCIAL = "social";
621
622 /**
623 * Notification category: error in background operation or authentication status.
624 */
625 public static final String CATEGORY_ERROR = "err";
626
627 /**
628 * Notification category: media transport control for playback.
629 */
630 public static final String CATEGORY_TRANSPORT = "transport";
631
632 /**
633 * Notification category: system or device status update. Reserved for system use.
634 */
635 public static final String CATEGORY_SYSTEM = "sys";
636
637 /**
638 * Notification category: indication of running background service.
639 */
640 public static final String CATEGORY_SERVICE = "service";
641
642 /**
John Spurlock0a69c8c2014-03-21 13:30:57 -0400643 * Notification category: a specific, timely recommendation for a single thing.
644 * For example, a news app might want to recommend a news story it believes the user will
645 * want to read next.
646 */
647 public static final String CATEGORY_RECOMMENDATION = "recommendation";
648
649 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400650 * Notification category: ongoing information about device or contextual status.
651 */
652 public static final String CATEGORY_STATUS = "status";
653
654 /**
John Spurlock24d3dad2015-04-02 12:24:02 -0400655 * Notification category: user-scheduled reminder.
656 */
657 public static final String CATEGORY_REMINDER = "reminder";
658
659 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400660 * One of the predefined notification categories (see the <code>CATEGORY_*</code> constants)
661 * that best describes this Notification. May be used by the system for ranking and filtering.
662 */
663 public String category;
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500664
Griff Hazen5cadc3b2014-05-20 09:55:39 -0700665 private String mGroupKey;
666
667 /**
668 * Get the key used to group this notification into a cluster or stack
669 * with other notifications on devices which support such rendering.
670 */
671 public String getGroup() {
672 return mGroupKey;
673 }
674
675 private String mSortKey;
676
677 /**
678 * Get a sort key that orders this notification among other notifications from the
679 * same package. This can be useful if an external sort was already applied and an app
680 * would like to preserve this. Notifications will be sorted lexicographically using this
681 * value, although providing different priorities in addition to providing sort key may
682 * cause this value to be ignored.
683 *
684 * <p>This sort key can also be used to order members of a notification group. See
685 * {@link Builder#setGroup}.
686 *
687 * @see String#compareTo(String)
688 */
689 public String getSortKey() {
690 return mSortKey;
691 }
692
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500693 /**
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400694 * Additional semantic data to be carried around with this Notification.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400695 * <p>
696 * The extras keys defined here are intended to capture the original inputs to {@link Builder}
697 * APIs, and are intended to be used by
698 * {@link android.service.notification.NotificationListenerService} implementations to extract
699 * detailed information from notification objects.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500700 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400701 public Bundle extras = new Bundle();
702
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400703 /**
704 * {@link #extras} key: this is the title of the notification,
705 * as supplied to {@link Builder#setContentTitle(CharSequence)}.
706 */
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -0500707 public static final String EXTRA_TITLE = "android.title";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400708
709 /**
710 * {@link #extras} key: this is the title of the notification when shown in expanded form,
711 * e.g. as supplied to {@link BigTextStyle#setBigContentTitle(CharSequence)}.
712 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400713 public static final String EXTRA_TITLE_BIG = EXTRA_TITLE + ".big";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400714
715 /**
716 * {@link #extras} key: this is the main text payload, as supplied to
717 * {@link Builder#setContentText(CharSequence)}.
718 */
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -0500719 public static final String EXTRA_TEXT = "android.text";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400720
721 /**
722 * {@link #extras} key: this is a third line of text, as supplied to
723 * {@link Builder#setSubText(CharSequence)}.
724 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400725 public static final String EXTRA_SUB_TEXT = "android.subText";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400726
727 /**
728 * {@link #extras} key: this is a small piece of additional text as supplied to
729 * {@link Builder#setContentInfo(CharSequence)}.
730 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400731 public static final String EXTRA_INFO_TEXT = "android.infoText";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400732
733 /**
734 * {@link #extras} key: this is a line of summary information intended to be shown
735 * alongside expanded notifications, as supplied to (e.g.)
736 * {@link BigTextStyle#setSummaryText(CharSequence)}.
737 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400738 public static final String EXTRA_SUMMARY_TEXT = "android.summaryText";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400739
740 /**
Christoph Studer4600f9b2014-07-22 22:44:43 +0200741 * {@link #extras} key: this is the longer text shown in the big form of a
742 * {@link BigTextStyle} notification, as supplied to
743 * {@link BigTextStyle#bigText(CharSequence)}.
744 */
745 public static final String EXTRA_BIG_TEXT = "android.bigText";
746
747 /**
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400748 * {@link #extras} key: this is the resource ID of the notification's main small icon, as
749 * supplied to {@link Builder#setSmallIcon(int)}.
750 */
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -0500751 public static final String EXTRA_SMALL_ICON = "android.icon";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400752
753 /**
754 * {@link #extras} key: this is a bitmap to be used instead of the small icon when showing the
755 * notification payload, as
756 * supplied to {@link Builder#setLargeIcon(android.graphics.Bitmap)}.
757 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400758 public static final String EXTRA_LARGE_ICON = "android.largeIcon";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400759
760 /**
761 * {@link #extras} key: this is a bitmap to be used instead of the one from
762 * {@link Builder#setLargeIcon(android.graphics.Bitmap)} when the notification is
763 * shown in its expanded form, as supplied to
764 * {@link BigPictureStyle#bigLargeIcon(android.graphics.Bitmap)}.
765 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400766 public static final String EXTRA_LARGE_ICON_BIG = EXTRA_LARGE_ICON + ".big";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400767
768 /**
769 * {@link #extras} key: this is the progress value supplied to
770 * {@link Builder#setProgress(int, int, boolean)}.
771 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400772 public static final String EXTRA_PROGRESS = "android.progress";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400773
774 /**
775 * {@link #extras} key: this is the maximum value supplied to
776 * {@link Builder#setProgress(int, int, boolean)}.
777 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400778 public static final String EXTRA_PROGRESS_MAX = "android.progressMax";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400779
780 /**
781 * {@link #extras} key: whether the progress bar is indeterminate, supplied to
782 * {@link Builder#setProgress(int, int, boolean)}.
783 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400784 public static final String EXTRA_PROGRESS_INDETERMINATE = "android.progressIndeterminate";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400785
786 /**
787 * {@link #extras} key: whether {@link #when} should be shown as a count-up timer (specifically
788 * a {@link android.widget.Chronometer}) instead of a timestamp, as supplied to
789 * {@link Builder#setUsesChronometer(boolean)}.
790 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400791 public static final String EXTRA_SHOW_CHRONOMETER = "android.showChronometer";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400792
793 /**
794 * {@link #extras} key: whether {@link #when} should be shown,
795 * as supplied to {@link Builder#setShowWhen(boolean)}.
796 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400797 public static final String EXTRA_SHOW_WHEN = "android.showWhen";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400798
799 /**
800 * {@link #extras} key: this is a bitmap to be shown in {@link BigPictureStyle} expanded
801 * notifications, supplied to {@link BigPictureStyle#bigPicture(android.graphics.Bitmap)}.
802 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400803 public static final String EXTRA_PICTURE = "android.picture";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400804
805 /**
806 * {@link #extras} key: An array of CharSequences to show in {@link InboxStyle} expanded
807 * notifications, each of which was supplied to {@link InboxStyle#addLine(CharSequence)}.
808 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400809 public static final String EXTRA_TEXT_LINES = "android.textLines";
Dan Sandler842dd772014-05-15 09:36:47 -0400810
811 /**
812 * {@link #extras} key: A string representing the name of the specific
813 * {@link android.app.Notification.Style} used to create this notification.
814 */
Chris Wren91ad5632013-06-05 15:05:57 -0400815 public static final String EXTRA_TEMPLATE = "android.template";
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400816
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400817 /**
Chris Wrene6c48932014-09-29 17:19:27 -0400818 * {@link #extras} key: A String array containing the people that this notification relates to,
819 * each of which was supplied to {@link Builder#addPerson(String)}.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400820 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400821 public static final String EXTRA_PEOPLE = "android.people";
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500822
823 /**
Chris Wren47c20a12014-06-18 17:27:29 -0400824 * {@link #extras} key: used to provide hints about the appropriateness of
825 * displaying this notification as a heads-up notification.
Chris Wren51c75102013-07-16 20:49:17 -0400826 * @hide
827 */
828 public static final String EXTRA_AS_HEADS_UP = "headsup";
829
830 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400831 * Allow certain system-generated notifications to appear before the device is provisioned.
832 * Only available to notifications coming from the android package.
833 * @hide
834 */
835 public static final String EXTRA_ALLOW_DURING_SETUP = "android.allowDuringSetup";
836
837 /**
Jose Limae9e3b3b2014-05-18 23:44:50 -0700838 * {@link #extras} key: A
839 * {@link android.content.ContentUris content URI} pointing to an image that can be displayed
840 * in the background when the notification is selected. The URI must point to an image stream
841 * suitable for passing into
842 * {@link android.graphics.BitmapFactory#decodeStream(java.io.InputStream)
843 * BitmapFactory.decodeStream}; all other content types will be ignored. The content provider
844 * URI used for this purpose must require no permissions to read the image data.
845 */
846 public static final String EXTRA_BACKGROUND_IMAGE_URI = "android.backgroundImageUri";
847
848 /**
Dan Sandler842dd772014-05-15 09:36:47 -0400849 * {@link #extras} key: A
Jeff Browndba34ba2014-06-24 20:46:03 -0700850 * {@link android.media.session.MediaSession.Token} associated with a
Dan Sandler842dd772014-05-15 09:36:47 -0400851 * {@link android.app.Notification.MediaStyle} notification.
852 */
853 public static final String EXTRA_MEDIA_SESSION = "android.mediaSession";
854
855 /**
Bryan Mawhinneye191f902014-07-22 12:50:09 +0100856 * {@link #extras} key: the indices of actions to be shown in the compact view,
857 * as supplied to (e.g.) {@link MediaStyle#setShowActionsInCompactView(int...)}.
858 */
859 public static final String EXTRA_COMPACT_ACTIONS = "android.compactActions";
860
Christoph Studer943aa672014-08-03 20:31:16 +0200861 /**
Kenny Guy8942bcd2014-09-08 21:09:47 +0100862 * {@link #extras} key: the user that built the notification.
863 *
864 * @hide
865 */
866 public static final String EXTRA_ORIGINATING_USERID = "android.originatingUserId";
867
868 /**
Chris Wren47c20a12014-06-18 17:27:29 -0400869 * Value for {@link #EXTRA_AS_HEADS_UP} that indicates this notification should not be
870 * displayed in the heads up space.
871 *
872 * <p>
873 * If this notification has a {@link #fullScreenIntent}, then it will always launch the
874 * full-screen intent when posted.
875 * </p>
Chris Wren51c75102013-07-16 20:49:17 -0400876 * @hide
877 */
878 public static final int HEADS_UP_NEVER = 0;
879
880 /**
Chris Wren47c20a12014-06-18 17:27:29 -0400881 * Default value for {@link #EXTRA_AS_HEADS_UP} that indicates this notification may be
882 * displayed as a heads up.
Chris Wren51c75102013-07-16 20:49:17 -0400883 * @hide
884 */
885 public static final int HEADS_UP_ALLOWED = 1;
886
887 /**
Chris Wren47c20a12014-06-18 17:27:29 -0400888 * Value for {@link #EXTRA_AS_HEADS_UP} that indicates this notification is a
889 * good candidate for display as a heads up.
Chris Wren51c75102013-07-16 20:49:17 -0400890 * @hide
891 */
892 public static final int HEADS_UP_REQUESTED = 2;
893
Dan Sandlerd63f9322015-05-06 15:18:49 -0400894 private Icon mSmallIcon;
895 private Icon mLargeIcon;
896
Chris Wren51c75102013-07-16 20:49:17 -0400897 /**
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400898 * Structure to encapsulate a named action that can be shown as part of this notification.
899 * It must include an icon, a label, and a {@link PendingIntent} to be fired when the action is
900 * selected by the user.
901 * <p>
Griff Hazen959591e2014-05-15 22:26:18 -0700902 * Apps should use {@link Notification.Builder#addAction(int, CharSequence, PendingIntent)}
903 * or {@link Notification.Builder#addAction(Notification.Action)}
904 * to attach actions.
Daniel Sandlera0a938c2012-03-15 08:42:37 -0400905 */
Daniel Sandlerea2a3172013-02-20 22:24:20 -0500906 public static class Action implements Parcelable {
Griff Hazen959591e2014-05-15 22:26:18 -0700907 private final Bundle mExtras;
Dan Sandler86647982015-05-13 23:41:13 -0400908 private Icon mIcon;
Griff Hazen61a9e862014-05-22 16:05:19 -0700909 private final RemoteInput[] mRemoteInputs;
Griff Hazen959591e2014-05-15 22:26:18 -0700910
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400911 /**
912 * Small icon representing the action.
Dan Sandler86647982015-05-13 23:41:13 -0400913 *
914 * @deprecated Use {@link Action#getIcon()} instead.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400915 */
Dan Sandler86647982015-05-13 23:41:13 -0400916 @Deprecated
Daniel Sandlera0a938c2012-03-15 08:42:37 -0400917 public int icon;
Griff Hazen5cadc3b2014-05-20 09:55:39 -0700918
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400919 /**
920 * Title of the action.
921 */
Daniel Sandlera0a938c2012-03-15 08:42:37 -0400922 public CharSequence title;
Griff Hazen5cadc3b2014-05-20 09:55:39 -0700923
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400924 /**
925 * Intent to send when the user invokes this action. May be null, in which case the action
926 * may be rendered in a disabled presentation by the system UI.
927 */
Daniel Sandlera0a938c2012-03-15 08:42:37 -0400928 public PendingIntent actionIntent;
Griff Hazen959591e2014-05-15 22:26:18 -0700929
Daniel Sandlera0a938c2012-03-15 08:42:37 -0400930 private Action(Parcel in) {
Dan Sandler86647982015-05-13 23:41:13 -0400931 if (in.readInt() != 0) {
932 mIcon = Icon.CREATOR.createFromParcel(in);
933 }
934 if (mIcon.getType() == Icon.TYPE_RESOURCE) {
935 icon = mIcon.getResId();
936 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -0400937 title = TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(in);
938 if (in.readInt() == 1) {
939 actionIntent = PendingIntent.CREATOR.createFromParcel(in);
940 }
Griff Hazen959591e2014-05-15 22:26:18 -0700941 mExtras = in.readBundle();
Griff Hazen5cadc3b2014-05-20 09:55:39 -0700942 mRemoteInputs = in.createTypedArray(RemoteInput.CREATOR);
Daniel Sandlera0a938c2012-03-15 08:42:37 -0400943 }
Griff Hazen5cadc3b2014-05-20 09:55:39 -0700944
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400945 /**
Dan Sandler86647982015-05-13 23:41:13 -0400946 * @deprecated Use {@link android.app.Notification.Action.Builder}.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400947 */
Dan Sandler86647982015-05-13 23:41:13 -0400948 @Deprecated
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400949 public Action(int icon, CharSequence title, PendingIntent intent) {
Dan Sandler86647982015-05-13 23:41:13 -0400950 this(Icon.createWithResource("", icon), title, intent, new Bundle(), null);
Griff Hazen959591e2014-05-15 22:26:18 -0700951 }
952
Dan Sandler86647982015-05-13 23:41:13 -0400953 private Action(Icon icon, CharSequence title, PendingIntent intent, Bundle extras,
Griff Hazen5cadc3b2014-05-20 09:55:39 -0700954 RemoteInput[] remoteInputs) {
Dan Sandler86647982015-05-13 23:41:13 -0400955 this.mIcon = icon;
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400956 this.title = title;
957 this.actionIntent = intent;
Griff Hazen959591e2014-05-15 22:26:18 -0700958 this.mExtras = extras != null ? extras : new Bundle();
Griff Hazen5cadc3b2014-05-20 09:55:39 -0700959 this.mRemoteInputs = remoteInputs;
Griff Hazen959591e2014-05-15 22:26:18 -0700960 }
961
962 /**
Dan Sandler86647982015-05-13 23:41:13 -0400963 * Return an icon representing the action.
964 */
965 public Icon getIcon() {
966 if (mIcon == null && icon != 0) {
967 // you snuck an icon in here without using the builder; let's try to keep it
968 mIcon = Icon.createWithResource("", icon);
969 }
970 return mIcon;
971 }
972
973 /**
Griff Hazen959591e2014-05-15 22:26:18 -0700974 * Get additional metadata carried around with this Action.
975 */
976 public Bundle getExtras() {
977 return mExtras;
978 }
979
980 /**
Griff Hazen5cadc3b2014-05-20 09:55:39 -0700981 * Get the list of inputs to be collected from the user when this action is sent.
982 * May return null if no remote inputs were added.
983 */
984 public RemoteInput[] getRemoteInputs() {
985 return mRemoteInputs;
986 }
987
988 /**
Griff Hazen959591e2014-05-15 22:26:18 -0700989 * Builder class for {@link Action} objects.
990 */
Griff Hazen5cadc3b2014-05-20 09:55:39 -0700991 public static final class Builder {
Dan Sandler86647982015-05-13 23:41:13 -0400992 private final Icon mIcon;
Griff Hazen959591e2014-05-15 22:26:18 -0700993 private final CharSequence mTitle;
994 private final PendingIntent mIntent;
995 private final Bundle mExtras;
Griff Hazen5cadc3b2014-05-20 09:55:39 -0700996 private ArrayList<RemoteInput> mRemoteInputs;
Griff Hazen959591e2014-05-15 22:26:18 -0700997
998 /**
999 * Construct a new builder for {@link Action} object.
1000 * @param icon icon to show for this action
1001 * @param title the title of the action
1002 * @param intent the {@link PendingIntent} to fire when users trigger this action
1003 */
Dan Sandler86647982015-05-13 23:41:13 -04001004 @Deprecated
Griff Hazen959591e2014-05-15 22:26:18 -07001005 public Builder(int icon, CharSequence title, PendingIntent intent) {
Dan Sandler86647982015-05-13 23:41:13 -04001006 this(Icon.createWithResource("", icon), title, intent, new Bundle(), null);
1007 }
1008
1009 /**
1010 * Construct a new builder for {@link Action} object.
1011 * @param icon icon to show for this action
1012 * @param title the title of the action
1013 * @param intent the {@link PendingIntent} to fire when users trigger this action
1014 */
1015 public Builder(Icon icon, CharSequence title, PendingIntent intent) {
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001016 this(icon, title, intent, new Bundle(), null);
Griff Hazen959591e2014-05-15 22:26:18 -07001017 }
1018
1019 /**
1020 * Construct a new builder for {@link Action} object using the fields from an
1021 * {@link Action}.
1022 * @param action the action to read fields from.
1023 */
1024 public Builder(Action action) {
Dan Sandler86647982015-05-13 23:41:13 -04001025 this(action.getIcon(), action.title, action.actionIntent, new Bundle(action.mExtras),
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001026 action.getRemoteInputs());
Griff Hazen959591e2014-05-15 22:26:18 -07001027 }
1028
Dan Sandler86647982015-05-13 23:41:13 -04001029 private Builder(Icon icon, CharSequence title, PendingIntent intent, Bundle extras,
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001030 RemoteInput[] remoteInputs) {
Griff Hazen959591e2014-05-15 22:26:18 -07001031 mIcon = icon;
1032 mTitle = title;
1033 mIntent = intent;
1034 mExtras = extras;
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001035 if (remoteInputs != null) {
1036 mRemoteInputs = new ArrayList<RemoteInput>(remoteInputs.length);
1037 Collections.addAll(mRemoteInputs, remoteInputs);
1038 }
Griff Hazen959591e2014-05-15 22:26:18 -07001039 }
1040
1041 /**
1042 * Merge additional metadata into this builder.
1043 *
1044 * <p>Values within the Bundle will replace existing extras values in this Builder.
1045 *
1046 * @see Notification.Action#extras
1047 */
1048 public Builder addExtras(Bundle extras) {
1049 if (extras != null) {
1050 mExtras.putAll(extras);
1051 }
1052 return this;
1053 }
1054
1055 /**
1056 * Get the metadata Bundle used by this Builder.
1057 *
1058 * <p>The returned Bundle is shared with this Builder.
1059 */
1060 public Bundle getExtras() {
1061 return mExtras;
1062 }
1063
1064 /**
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001065 * Add an input to be collected from the user when this action is sent.
1066 * Response values can be retrieved from the fired intent by using the
1067 * {@link RemoteInput#getResultsFromIntent} function.
1068 * @param remoteInput a {@link RemoteInput} to add to the action
1069 * @return this object for method chaining
1070 */
1071 public Builder addRemoteInput(RemoteInput remoteInput) {
1072 if (mRemoteInputs == null) {
1073 mRemoteInputs = new ArrayList<RemoteInput>();
1074 }
1075 mRemoteInputs.add(remoteInput);
1076 return this;
1077 }
1078
1079 /**
1080 * Apply an extender to this action builder. Extenders may be used to add
1081 * metadata or change options on this builder.
1082 */
Griff Hazen61a9e862014-05-22 16:05:19 -07001083 public Builder extend(Extender extender) {
1084 extender.extend(this);
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001085 return this;
1086 }
1087
1088 /**
Griff Hazen959591e2014-05-15 22:26:18 -07001089 * Combine all of the options that have been set and return a new {@link Action}
1090 * object.
1091 * @return the built action
1092 */
1093 public Action build() {
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001094 RemoteInput[] remoteInputs = mRemoteInputs != null
1095 ? mRemoteInputs.toArray(new RemoteInput[mRemoteInputs.size()]) : null;
1096 return new Action(mIcon, mTitle, mIntent, mExtras, remoteInputs);
Griff Hazen959591e2014-05-15 22:26:18 -07001097 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001098 }
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001099
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001100 @Override
1101 public Action clone() {
1102 return new Action(
Dan Sandler86647982015-05-13 23:41:13 -04001103 getIcon(),
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001104 title,
1105 actionIntent, // safe to alias
1106 new Bundle(mExtras),
1107 getRemoteInputs());
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001108 }
1109 @Override
1110 public int describeContents() {
1111 return 0;
1112 }
1113 @Override
1114 public void writeToParcel(Parcel out, int flags) {
Dan Sandler86647982015-05-13 23:41:13 -04001115 final Icon ic = getIcon();
1116 if (ic != null) {
1117 out.writeInt(1);
1118 ic.writeToParcel(out, 0);
1119 } else {
1120 out.writeInt(0);
1121 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001122 TextUtils.writeToParcel(title, out, flags);
1123 if (actionIntent != null) {
1124 out.writeInt(1);
1125 actionIntent.writeToParcel(out, flags);
1126 } else {
1127 out.writeInt(0);
1128 }
Griff Hazen959591e2014-05-15 22:26:18 -07001129 out.writeBundle(mExtras);
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001130 out.writeTypedArray(mRemoteInputs, flags);
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001131 }
Griff Hazen959591e2014-05-15 22:26:18 -07001132 public static final Parcelable.Creator<Action> CREATOR =
1133 new Parcelable.Creator<Action>() {
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001134 public Action createFromParcel(Parcel in) {
1135 return new Action(in);
1136 }
1137 public Action[] newArray(int size) {
1138 return new Action[size];
1139 }
1140 };
Griff Hazen61a9e862014-05-22 16:05:19 -07001141
1142 /**
1143 * Extender interface for use with {@link Builder#extend}. Extenders may be used to add
1144 * metadata or change options on an action builder.
1145 */
1146 public interface Extender {
1147 /**
1148 * Apply this extender to a notification action builder.
1149 * @param builder the builder to be modified.
1150 * @return the build object for chaining.
1151 */
1152 public Builder extend(Builder builder);
1153 }
1154
1155 /**
1156 * Wearable extender for notification actions. To add extensions to an action,
1157 * create a new {@link android.app.Notification.Action.WearableExtender} object using
1158 * the {@code WearableExtender()} constructor and apply it to a
1159 * {@link android.app.Notification.Action.Builder} using
1160 * {@link android.app.Notification.Action.Builder#extend}.
1161 *
1162 * <pre class="prettyprint">
1163 * Notification.Action action = new Notification.Action.Builder(
1164 * R.drawable.archive_all, "Archive all", actionIntent)
Griff Hazen14f57992014-05-26 09:07:14 -07001165 * .extend(new Notification.Action.WearableExtender()
Griff Hazen61a9e862014-05-22 16:05:19 -07001166 * .setAvailableOffline(false))
Griff Hazen14f57992014-05-26 09:07:14 -07001167 * .build();</pre>
Griff Hazen61a9e862014-05-22 16:05:19 -07001168 */
1169 public static final class WearableExtender implements Extender {
1170 /** Notification action extra which contains wearable extensions */
1171 private static final String EXTRA_WEARABLE_EXTENSIONS = "android.wearable.EXTENSIONS";
1172
Pete Gastaf6781d2014-10-07 15:17:05 -04001173 // Keys within EXTRA_WEARABLE_EXTENSIONS for wearable options.
Griff Hazen61a9e862014-05-22 16:05:19 -07001174 private static final String KEY_FLAGS = "flags";
Pete Gastaf6781d2014-10-07 15:17:05 -04001175 private static final String KEY_IN_PROGRESS_LABEL = "inProgressLabel";
1176 private static final String KEY_CONFIRM_LABEL = "confirmLabel";
1177 private static final String KEY_CANCEL_LABEL = "cancelLabel";
Griff Hazen61a9e862014-05-22 16:05:19 -07001178
1179 // Flags bitwise-ored to mFlags
1180 private static final int FLAG_AVAILABLE_OFFLINE = 0x1;
1181
1182 // Default value for flags integer
1183 private static final int DEFAULT_FLAGS = FLAG_AVAILABLE_OFFLINE;
1184
1185 private int mFlags = DEFAULT_FLAGS;
1186
Pete Gastaf6781d2014-10-07 15:17:05 -04001187 private CharSequence mInProgressLabel;
1188 private CharSequence mConfirmLabel;
1189 private CharSequence mCancelLabel;
1190
Griff Hazen61a9e862014-05-22 16:05:19 -07001191 /**
1192 * Create a {@link android.app.Notification.Action.WearableExtender} with default
1193 * options.
1194 */
1195 public WearableExtender() {
1196 }
1197
1198 /**
1199 * Create a {@link android.app.Notification.Action.WearableExtender} by reading
1200 * wearable options present in an existing notification action.
1201 * @param action the notification action to inspect.
1202 */
1203 public WearableExtender(Action action) {
1204 Bundle wearableBundle = action.getExtras().getBundle(EXTRA_WEARABLE_EXTENSIONS);
1205 if (wearableBundle != null) {
1206 mFlags = wearableBundle.getInt(KEY_FLAGS, DEFAULT_FLAGS);
Pete Gastaf6781d2014-10-07 15:17:05 -04001207 mInProgressLabel = wearableBundle.getCharSequence(KEY_IN_PROGRESS_LABEL);
1208 mConfirmLabel = wearableBundle.getCharSequence(KEY_CONFIRM_LABEL);
1209 mCancelLabel = wearableBundle.getCharSequence(KEY_CANCEL_LABEL);
Griff Hazen61a9e862014-05-22 16:05:19 -07001210 }
1211 }
1212
1213 /**
1214 * Apply wearable extensions to a notification action that is being built. This is
1215 * typically called by the {@link android.app.Notification.Action.Builder#extend}
1216 * method of {@link android.app.Notification.Action.Builder}.
1217 */
1218 @Override
1219 public Action.Builder extend(Action.Builder builder) {
1220 Bundle wearableBundle = new Bundle();
1221
1222 if (mFlags != DEFAULT_FLAGS) {
1223 wearableBundle.putInt(KEY_FLAGS, mFlags);
1224 }
Pete Gastaf6781d2014-10-07 15:17:05 -04001225 if (mInProgressLabel != null) {
1226 wearableBundle.putCharSequence(KEY_IN_PROGRESS_LABEL, mInProgressLabel);
1227 }
1228 if (mConfirmLabel != null) {
1229 wearableBundle.putCharSequence(KEY_CONFIRM_LABEL, mConfirmLabel);
1230 }
1231 if (mCancelLabel != null) {
1232 wearableBundle.putCharSequence(KEY_CANCEL_LABEL, mCancelLabel);
1233 }
Griff Hazen61a9e862014-05-22 16:05:19 -07001234
1235 builder.getExtras().putBundle(EXTRA_WEARABLE_EXTENSIONS, wearableBundle);
1236 return builder;
1237 }
1238
1239 @Override
1240 public WearableExtender clone() {
1241 WearableExtender that = new WearableExtender();
1242 that.mFlags = this.mFlags;
Pete Gastaf6781d2014-10-07 15:17:05 -04001243 that.mInProgressLabel = this.mInProgressLabel;
1244 that.mConfirmLabel = this.mConfirmLabel;
1245 that.mCancelLabel = this.mCancelLabel;
Griff Hazen61a9e862014-05-22 16:05:19 -07001246 return that;
1247 }
1248
1249 /**
1250 * Set whether this action is available when the wearable device is not connected to
1251 * a companion device. The user can still trigger this action when the wearable device is
1252 * offline, but a visual hint will indicate that the action may not be available.
1253 * Defaults to true.
1254 */
1255 public WearableExtender setAvailableOffline(boolean availableOffline) {
1256 setFlag(FLAG_AVAILABLE_OFFLINE, availableOffline);
1257 return this;
1258 }
1259
1260 /**
1261 * Get whether this action is available when the wearable device is not connected to
1262 * a companion device. The user can still trigger this action when the wearable device is
1263 * offline, but a visual hint will indicate that the action may not be available.
1264 * Defaults to true.
1265 */
1266 public boolean isAvailableOffline() {
1267 return (mFlags & FLAG_AVAILABLE_OFFLINE) != 0;
1268 }
1269
1270 private void setFlag(int mask, boolean value) {
1271 if (value) {
1272 mFlags |= mask;
1273 } else {
1274 mFlags &= ~mask;
1275 }
1276 }
Pete Gastaf6781d2014-10-07 15:17:05 -04001277
1278 /**
1279 * Set a label to display while the wearable is preparing to automatically execute the
1280 * action. This is usually a 'ing' verb ending in ellipsis like "Sending..."
1281 *
1282 * @param label the label to display while the action is being prepared to execute
1283 * @return this object for method chaining
1284 */
1285 public WearableExtender setInProgressLabel(CharSequence label) {
1286 mInProgressLabel = label;
1287 return this;
1288 }
1289
1290 /**
1291 * Get the label to display while the wearable is preparing to automatically execute
1292 * the action. This is usually a 'ing' verb ending in ellipsis like "Sending..."
1293 *
1294 * @return the label to display while the action is being prepared to execute
1295 */
1296 public CharSequence getInProgressLabel() {
1297 return mInProgressLabel;
1298 }
1299
1300 /**
1301 * Set a label to display to confirm that the action should be executed.
1302 * This is usually an imperative verb like "Send".
1303 *
1304 * @param label the label to confirm the action should be executed
1305 * @return this object for method chaining
1306 */
1307 public WearableExtender setConfirmLabel(CharSequence label) {
1308 mConfirmLabel = label;
1309 return this;
1310 }
1311
1312 /**
1313 * Get the label to display to confirm that the action should be executed.
1314 * This is usually an imperative verb like "Send".
1315 *
1316 * @return the label to confirm the action should be executed
1317 */
1318 public CharSequence getConfirmLabel() {
1319 return mConfirmLabel;
1320 }
1321
1322 /**
1323 * Set a label to display to cancel the action.
1324 * This is usually an imperative verb, like "Cancel".
1325 *
1326 * @param label the label to display to cancel the action
1327 * @return this object for method chaining
1328 */
1329 public WearableExtender setCancelLabel(CharSequence label) {
1330 mCancelLabel = label;
1331 return this;
1332 }
1333
1334 /**
1335 * Get the label to display to cancel the action.
1336 * This is usually an imperative verb like "Cancel".
1337 *
1338 * @return the label to display to cancel the action
1339 */
1340 public CharSequence getCancelLabel() {
1341 return mCancelLabel;
1342 }
Griff Hazen61a9e862014-05-22 16:05:19 -07001343 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001344 }
1345
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001346 /**
1347 * Array of all {@link Action} structures attached to this notification by
1348 * {@link Builder#addAction(int, CharSequence, PendingIntent)}. Mostly useful for instances of
1349 * {@link android.service.notification.NotificationListenerService} that provide an alternative
1350 * interface for invoking actions.
1351 */
Daniel Sandlerea2a3172013-02-20 22:24:20 -05001352 public Action[] actions;
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001353
1354 /**
Dan Sandler0bf2ed82013-12-21 23:33:41 -06001355 * Replacement version of this notification whose content will be shown
1356 * in an insecure context such as atop a secure keyguard. See {@link #visibility}
1357 * and {@link #VISIBILITY_PUBLIC}.
1358 */
1359 public Notification publicVersion;
1360
1361 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001362 * Constructs a Notification object with default values.
Joe Onorato46439ce2010-11-19 13:56:21 -08001363 * You might want to consider using {@link Builder} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001364 */
1365 public Notification()
1366 {
1367 this.when = System.currentTimeMillis();
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001368 this.priority = PRIORITY_DEFAULT;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001369 }
1370
1371 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001372 * @hide
1373 */
1374 public Notification(Context context, int icon, CharSequence tickerText, long when,
1375 CharSequence contentTitle, CharSequence contentText, Intent contentIntent)
1376 {
Chris Wren1ce4b6d2015-06-11 10:19:43 -04001377 new Builder(context)
1378 .setWhen(when)
1379 .setSmallIcon(icon)
1380 .setTicker(tickerText)
1381 .setContentTitle(contentTitle)
1382 .setContentText(contentText)
1383 .setContentIntent(PendingIntent.getActivity(context, 0, contentIntent, 0))
1384 .buildInto(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001385 }
1386
1387 /**
1388 * Constructs a Notification object with the information needed to
1389 * have a status bar icon without the standard expanded view.
1390 *
1391 * @param icon The resource id of the icon to put in the status bar.
1392 * @param tickerText The text that flows by in the status bar when the notification first
1393 * activates.
1394 * @param when The time to show in the time field. In the System.currentTimeMillis
1395 * timebase.
Joe Onorato46439ce2010-11-19 13:56:21 -08001396 *
1397 * @deprecated Use {@link Builder} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001398 */
Joe Onorato46439ce2010-11-19 13:56:21 -08001399 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001400 public Notification(int icon, CharSequence tickerText, long when)
1401 {
1402 this.icon = icon;
1403 this.tickerText = tickerText;
1404 this.when = when;
1405 }
1406
1407 /**
1408 * Unflatten the notification from a parcel.
1409 */
1410 public Notification(Parcel parcel)
1411 {
1412 int version = parcel.readInt();
1413
1414 when = parcel.readLong();
Dan Sandler3936e7a2015-05-19 20:59:12 -04001415 if (parcel.readInt() != 0) {
1416 mSmallIcon = Icon.CREATOR.createFromParcel(parcel);
Dan Sandler4e787062015-06-17 15:09:48 -04001417 if (mSmallIcon.getType() == Icon.TYPE_RESOURCE) {
1418 icon = mSmallIcon.getResId();
1419 }
Dan Sandler3936e7a2015-05-19 20:59:12 -04001420 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001421 number = parcel.readInt();
1422 if (parcel.readInt() != 0) {
1423 contentIntent = PendingIntent.CREATOR.createFromParcel(parcel);
1424 }
1425 if (parcel.readInt() != 0) {
1426 deleteIntent = PendingIntent.CREATOR.createFromParcel(parcel);
1427 }
1428 if (parcel.readInt() != 0) {
1429 tickerText = TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(parcel);
1430 }
1431 if (parcel.readInt() != 0) {
Joe Onorato46439ce2010-11-19 13:56:21 -08001432 tickerView = RemoteViews.CREATOR.createFromParcel(parcel);
Joe Onoratoef1e7762010-09-17 18:38:38 -04001433 }
1434 if (parcel.readInt() != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001435 contentView = RemoteViews.CREATOR.createFromParcel(parcel);
1436 }
Joe Onorato561d3852010-11-20 18:09:34 -08001437 if (parcel.readInt() != 0) {
Dan Sandlerd63f9322015-05-06 15:18:49 -04001438 mLargeIcon = Icon.CREATOR.createFromParcel(parcel);
Joe Onorato561d3852010-11-20 18:09:34 -08001439 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001440 defaults = parcel.readInt();
1441 flags = parcel.readInt();
1442 if (parcel.readInt() != 0) {
1443 sound = Uri.CREATOR.createFromParcel(parcel);
1444 }
1445
1446 audioStreamType = parcel.readInt();
John Spurlockc0650f022014-07-19 13:22:39 -04001447 if (parcel.readInt() != 0) {
1448 audioAttributes = AudioAttributes.CREATOR.createFromParcel(parcel);
1449 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001450 vibrate = parcel.createLongArray();
1451 ledARGB = parcel.readInt();
1452 ledOnMS = parcel.readInt();
1453 ledOffMS = parcel.readInt();
1454 iconLevel = parcel.readInt();
Daniel Sandlere46cbd32010-06-17 10:35:26 -04001455
1456 if (parcel.readInt() != 0) {
1457 fullScreenIntent = PendingIntent.CREATOR.createFromParcel(parcel);
1458 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001459
1460 priority = parcel.readInt();
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001461
John Spurlockfd7f1e02014-03-18 16:41:57 -04001462 category = parcel.readString();
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001463
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001464 mGroupKey = parcel.readString();
1465
1466 mSortKey = parcel.readString();
1467
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001468 extras = parcel.readBundle(); // may be null
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001469
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001470 actions = parcel.createTypedArray(Action.CREATOR); // may be null
1471
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001472 if (parcel.readInt() != 0) {
Daniel Sandlerf3b73432012-03-27 15:01:25 -04001473 bigContentView = RemoteViews.CREATOR.createFromParcel(parcel);
1474 }
Dan Sandler0bf2ed82013-12-21 23:33:41 -06001475
Chris Wren8fd39ec2014-02-27 17:43:26 -05001476 if (parcel.readInt() != 0) {
1477 headsUpContentView = RemoteViews.CREATOR.createFromParcel(parcel);
1478 }
1479
Dan Sandler0bf2ed82013-12-21 23:33:41 -06001480 visibility = parcel.readInt();
1481
1482 if (parcel.readInt() != 0) {
1483 publicVersion = Notification.CREATOR.createFromParcel(parcel);
1484 }
Dan Sandler26e81cf2014-05-06 10:01:27 -04001485
1486 color = parcel.readInt();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001487 }
1488
Andy Stadler110988c2010-12-03 14:29:16 -08001489 @Override
Joe Onorato18e69df2010-05-17 22:26:12 -07001490 public Notification clone() {
1491 Notification that = new Notification();
Daniel Sandler1a497d32013-04-18 14:52:45 -04001492 cloneInto(that, true);
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001493 return that;
1494 }
Joe Onorato18e69df2010-05-17 22:26:12 -07001495
Daniel Sandler1a497d32013-04-18 14:52:45 -04001496 /**
1497 * Copy all (or if heavy is false, all except Bitmaps and RemoteViews) members
1498 * of this into that.
1499 * @hide
1500 */
1501 public void cloneInto(Notification that, boolean heavy) {
Joe Onorato18e69df2010-05-17 22:26:12 -07001502 that.when = this.when;
Dan Sandlerd63f9322015-05-06 15:18:49 -04001503 that.mSmallIcon = this.mSmallIcon;
Joe Onorato18e69df2010-05-17 22:26:12 -07001504 that.number = this.number;
1505
1506 // PendingIntents are global, so there's no reason (or way) to clone them.
1507 that.contentIntent = this.contentIntent;
1508 that.deleteIntent = this.deleteIntent;
Daniel Sandlere46cbd32010-06-17 10:35:26 -04001509 that.fullScreenIntent = this.fullScreenIntent;
Joe Onorato18e69df2010-05-17 22:26:12 -07001510
1511 if (this.tickerText != null) {
1512 that.tickerText = this.tickerText.toString();
1513 }
Daniel Sandler1a497d32013-04-18 14:52:45 -04001514 if (heavy && this.tickerView != null) {
Joe Onorato46439ce2010-11-19 13:56:21 -08001515 that.tickerView = this.tickerView.clone();
Joe Onoratoef1e7762010-09-17 18:38:38 -04001516 }
Daniel Sandler1a497d32013-04-18 14:52:45 -04001517 if (heavy && this.contentView != null) {
Joe Onorato18e69df2010-05-17 22:26:12 -07001518 that.contentView = this.contentView.clone();
1519 }
Dan Sandlerd63f9322015-05-06 15:18:49 -04001520 if (heavy && this.mLargeIcon != null) {
1521 that.mLargeIcon = this.mLargeIcon;
Joe Onorato561d3852010-11-20 18:09:34 -08001522 }
Jozef BABJAKa8b91832011-02-22 08:05:08 +01001523 that.iconLevel = this.iconLevel;
Joe Onorato18e69df2010-05-17 22:26:12 -07001524 that.sound = this.sound; // android.net.Uri is immutable
1525 that.audioStreamType = this.audioStreamType;
John Spurlockc0650f022014-07-19 13:22:39 -04001526 if (this.audioAttributes != null) {
1527 that.audioAttributes = new AudioAttributes.Builder(this.audioAttributes).build();
1528 }
Joe Onorato18e69df2010-05-17 22:26:12 -07001529
1530 final long[] vibrate = this.vibrate;
1531 if (vibrate != null) {
1532 final int N = vibrate.length;
1533 final long[] vib = that.vibrate = new long[N];
1534 System.arraycopy(vibrate, 0, vib, 0, N);
1535 }
1536
1537 that.ledARGB = this.ledARGB;
1538 that.ledOnMS = this.ledOnMS;
1539 that.ledOffMS = this.ledOffMS;
1540 that.defaults = this.defaults;
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001541
Joe Onorato18e69df2010-05-17 22:26:12 -07001542 that.flags = this.flags;
1543
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001544 that.priority = this.priority;
Joe Malin8d40d042012-11-05 11:36:40 -08001545
John Spurlockfd7f1e02014-03-18 16:41:57 -04001546 that.category = this.category;
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001547
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001548 that.mGroupKey = this.mGroupKey;
1549
1550 that.mSortKey = this.mSortKey;
1551
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001552 if (this.extras != null) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04001553 try {
1554 that.extras = new Bundle(this.extras);
1555 // will unparcel
1556 that.extras.size();
1557 } catch (BadParcelableException e) {
1558 Log.e(TAG, "could not unparcel extras from notification: " + this, e);
1559 that.extras = null;
1560 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001561 }
1562
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001563 if (this.actions != null) {
1564 that.actions = new Action[this.actions.length];
1565 for(int i=0; i<this.actions.length; i++) {
1566 that.actions[i] = this.actions[i].clone();
1567 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001568 }
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001569
Daniel Sandler1a497d32013-04-18 14:52:45 -04001570 if (heavy && this.bigContentView != null) {
Daniel Sandlerf3b73432012-03-27 15:01:25 -04001571 that.bigContentView = this.bigContentView.clone();
1572 }
Daniel Sandler1a497d32013-04-18 14:52:45 -04001573
Chris Wren8fd39ec2014-02-27 17:43:26 -05001574 if (heavy && this.headsUpContentView != null) {
1575 that.headsUpContentView = this.headsUpContentView.clone();
1576 }
1577
Dan Sandler0bf2ed82013-12-21 23:33:41 -06001578 that.visibility = this.visibility;
1579
1580 if (this.publicVersion != null) {
1581 that.publicVersion = new Notification();
1582 this.publicVersion.cloneInto(that.publicVersion, heavy);
1583 }
1584
Dan Sandler26e81cf2014-05-06 10:01:27 -04001585 that.color = this.color;
1586
Daniel Sandler1a497d32013-04-18 14:52:45 -04001587 if (!heavy) {
1588 that.lightenPayload(); // will clean out extras
1589 }
1590 }
1591
1592 /**
1593 * Removes heavyweight parts of the Notification object for archival or for sending to
1594 * listeners when the full contents are not necessary.
1595 * @hide
1596 */
1597 public final void lightenPayload() {
1598 tickerView = null;
1599 contentView = null;
1600 bigContentView = null;
Chris Wren8fd39ec2014-02-27 17:43:26 -05001601 headsUpContentView = null;
Dan Sandlerd63f9322015-05-06 15:18:49 -04001602 mLargeIcon = null;
Daniel Sandler1a497d32013-04-18 14:52:45 -04001603 if (extras != null) {
1604 extras.remove(Notification.EXTRA_LARGE_ICON);
1605 extras.remove(Notification.EXTRA_LARGE_ICON_BIG);
1606 extras.remove(Notification.EXTRA_PICTURE);
Christoph Studer223f44e2014-09-02 14:59:32 +02001607 extras.remove(Notification.EXTRA_BIG_TEXT);
Christoph Studerb82bc782014-08-20 14:29:43 +02001608 // Prevent light notifications from being rebuilt.
1609 extras.remove(Builder.EXTRA_NEEDS_REBUILD);
Daniel Sandler1a497d32013-04-18 14:52:45 -04001610 }
Joe Onorato18e69df2010-05-17 22:26:12 -07001611 }
1612
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04001613 /**
1614 * Make sure this CharSequence is safe to put into a bundle, which basically
1615 * means it had better not be some custom Parcelable implementation.
1616 * @hide
1617 */
1618 public static CharSequence safeCharSequence(CharSequence cs) {
Christoph Studer535ec612014-09-03 15:47:47 +02001619 if (cs == null) return cs;
1620 if (cs.length() > MAX_CHARSEQUENCE_LENGTH) {
1621 cs = cs.subSequence(0, MAX_CHARSEQUENCE_LENGTH);
1622 }
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04001623 if (cs instanceof Parcelable) {
1624 Log.e(TAG, "warning: " + cs.getClass().getCanonicalName()
1625 + " instance is a custom Parcelable and not allowed in Notification");
1626 return cs.toString();
1627 }
1628
1629 return cs;
1630 }
1631
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001632 public int describeContents() {
1633 return 0;
1634 }
1635
1636 /**
Dan Sandler4e787062015-06-17 15:09:48 -04001637 * Flatten this notification into a parcel.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001638 */
1639 public void writeToParcel(Parcel parcel, int flags)
1640 {
1641 parcel.writeInt(1);
1642
1643 parcel.writeLong(when);
Dan Sandler4e787062015-06-17 15:09:48 -04001644 if (mSmallIcon == null && icon != 0) {
1645 // you snuck an icon in here without using the builder; let's try to keep it
1646 mSmallIcon = Icon.createWithResource("", icon);
1647 }
Dan Sandler3936e7a2015-05-19 20:59:12 -04001648 if (mSmallIcon != null) {
1649 parcel.writeInt(1);
1650 mSmallIcon.writeToParcel(parcel, 0);
1651 } else {
1652 parcel.writeInt(0);
1653 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001654 parcel.writeInt(number);
1655 if (contentIntent != null) {
1656 parcel.writeInt(1);
1657 contentIntent.writeToParcel(parcel, 0);
1658 } else {
1659 parcel.writeInt(0);
1660 }
1661 if (deleteIntent != null) {
1662 parcel.writeInt(1);
1663 deleteIntent.writeToParcel(parcel, 0);
1664 } else {
1665 parcel.writeInt(0);
1666 }
1667 if (tickerText != null) {
1668 parcel.writeInt(1);
1669 TextUtils.writeToParcel(tickerText, parcel, flags);
1670 } else {
1671 parcel.writeInt(0);
1672 }
Joe Onorato46439ce2010-11-19 13:56:21 -08001673 if (tickerView != null) {
Joe Onoratoef1e7762010-09-17 18:38:38 -04001674 parcel.writeInt(1);
Joe Onorato46439ce2010-11-19 13:56:21 -08001675 tickerView.writeToParcel(parcel, 0);
Joe Onoratoef1e7762010-09-17 18:38:38 -04001676 } else {
1677 parcel.writeInt(0);
1678 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001679 if (contentView != null) {
1680 parcel.writeInt(1);
1681 contentView.writeToParcel(parcel, 0);
1682 } else {
1683 parcel.writeInt(0);
1684 }
Dan Sandlerd63f9322015-05-06 15:18:49 -04001685 if (mLargeIcon != null) {
Joe Onorato561d3852010-11-20 18:09:34 -08001686 parcel.writeInt(1);
Dan Sandlerd63f9322015-05-06 15:18:49 -04001687 mLargeIcon.writeToParcel(parcel, 0);
Joe Onorato561d3852010-11-20 18:09:34 -08001688 } else {
1689 parcel.writeInt(0);
1690 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001691
1692 parcel.writeInt(defaults);
1693 parcel.writeInt(this.flags);
1694
1695 if (sound != null) {
1696 parcel.writeInt(1);
1697 sound.writeToParcel(parcel, 0);
1698 } else {
1699 parcel.writeInt(0);
1700 }
1701 parcel.writeInt(audioStreamType);
John Spurlockc0650f022014-07-19 13:22:39 -04001702
1703 if (audioAttributes != null) {
1704 parcel.writeInt(1);
1705 audioAttributes.writeToParcel(parcel, 0);
1706 } else {
1707 parcel.writeInt(0);
1708 }
1709
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001710 parcel.writeLongArray(vibrate);
1711 parcel.writeInt(ledARGB);
1712 parcel.writeInt(ledOnMS);
1713 parcel.writeInt(ledOffMS);
1714 parcel.writeInt(iconLevel);
Daniel Sandlere46cbd32010-06-17 10:35:26 -04001715
1716 if (fullScreenIntent != null) {
1717 parcel.writeInt(1);
1718 fullScreenIntent.writeToParcel(parcel, 0);
1719 } else {
1720 parcel.writeInt(0);
1721 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001722
1723 parcel.writeInt(priority);
Joe Malin8d40d042012-11-05 11:36:40 -08001724
John Spurlockfd7f1e02014-03-18 16:41:57 -04001725 parcel.writeString(category);
Joe Malin8d40d042012-11-05 11:36:40 -08001726
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001727 parcel.writeString(mGroupKey);
1728
1729 parcel.writeString(mSortKey);
1730
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001731 parcel.writeBundle(extras); // null ok
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001732
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001733 parcel.writeTypedArray(actions, 0); // null ok
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001734
Daniel Sandlerf3b73432012-03-27 15:01:25 -04001735 if (bigContentView != null) {
1736 parcel.writeInt(1);
1737 bigContentView.writeToParcel(parcel, 0);
1738 } else {
1739 parcel.writeInt(0);
1740 }
Dan Sandler0bf2ed82013-12-21 23:33:41 -06001741
Chris Wren8fd39ec2014-02-27 17:43:26 -05001742 if (headsUpContentView != null) {
1743 parcel.writeInt(1);
1744 headsUpContentView.writeToParcel(parcel, 0);
1745 } else {
1746 parcel.writeInt(0);
1747 }
1748
Dan Sandler0bf2ed82013-12-21 23:33:41 -06001749 parcel.writeInt(visibility);
1750
1751 if (publicVersion != null) {
1752 parcel.writeInt(1);
1753 publicVersion.writeToParcel(parcel, 0);
1754 } else {
1755 parcel.writeInt(0);
1756 }
Dan Sandler26e81cf2014-05-06 10:01:27 -04001757
1758 parcel.writeInt(color);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001759 }
1760
1761 /**
1762 * Parcelable.Creator that instantiates Notification objects
1763 */
1764 public static final Parcelable.Creator<Notification> CREATOR
1765 = new Parcelable.Creator<Notification>()
1766 {
1767 public Notification createFromParcel(Parcel parcel)
1768 {
1769 return new Notification(parcel);
1770 }
1771
1772 public Notification[] newArray(int size)
1773 {
1774 return new Notification[size];
1775 }
1776 };
1777
1778 /**
1779 * Sets the {@link #contentView} field to be a view with the standard "Latest Event"
1780 * layout.
1781 *
1782 * <p>Uses the {@link #icon} and {@link #when} fields to set the icon and time fields
1783 * in the view.</p>
1784 * @param context The context for your application / activity.
1785 * @param contentTitle The title that goes in the expanded entry.
1786 * @param contentText The text that goes in the expanded entry.
1787 * @param contentIntent The intent to launch when the user clicks the expanded notification.
1788 * If this is an activity, it must include the
1789 * {@link android.content.Intent#FLAG_ACTIVITY_NEW_TASK} flag, which requires
Scott Main7aee61f2011-02-08 11:25:01 -08001790 * that you take care of task management as described in the
1791 * <a href="{@docRoot}guide/topics/fundamentals/tasks-and-back-stack.html">Tasks and Back
1792 * Stack</a> document.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001793 *
Joe Onorato46439ce2010-11-19 13:56:21 -08001794 * @deprecated Use {@link Builder} instead.
Chris Wrena05db382015-06-24 15:18:34 -04001795 * @removed
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001796 */
Joe Onorato46439ce2010-11-19 13:56:21 -08001797 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001798 public void setLatestEventInfo(Context context,
1799 CharSequence contentTitle, CharSequence contentText, PendingIntent contentIntent) {
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001800 Notification.Builder builder = new Notification.Builder(context);
1801
1802 // First, ensure that key pieces of information that may have been set directly
1803 // are preserved
1804 builder.setWhen(this.when);
1805 builder.setSmallIcon(this.icon);
1806 builder.setPriority(this.priority);
1807 builder.setTicker(this.tickerText);
1808 builder.setNumber(this.number);
Selim Cinek255dd042014-08-19 22:29:02 +02001809 builder.setColor(this.color);
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001810 builder.mFlags = this.flags;
1811 builder.setSound(this.sound, this.audioStreamType);
1812 builder.setDefaults(this.defaults);
1813 builder.setVibrate(this.vibrate);
Sean Barbeau6ae0b1b2014-07-19 21:31:41 -07001814 builder.setDeleteIntent(this.deleteIntent);
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001815
1816 // now apply the latestEventInfo fields
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001817 if (contentTitle != null) {
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001818 builder.setContentTitle(contentTitle);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001819 }
1820 if (contentText != null) {
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001821 builder.setContentText(contentText);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001822 }
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001823 builder.setContentIntent(contentIntent);
1824 builder.buildInto(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001825 }
1826
1827 @Override
1828 public String toString() {
1829 StringBuilder sb = new StringBuilder();
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001830 sb.append("Notification(pri=");
1831 sb.append(priority);
1832 sb.append(" contentView=");
Joe Onoratoc9596d62011-01-12 17:03:11 -08001833 if (contentView != null) {
1834 sb.append(contentView.getPackage());
1835 sb.append("/0x");
1836 sb.append(Integer.toHexString(contentView.getLayoutId()));
1837 } else {
1838 sb.append("null");
1839 }
1840 sb.append(" vibrate=");
Daniel Sandler6738eee2012-11-16 12:03:32 -05001841 if ((this.defaults & DEFAULT_VIBRATE) != 0) {
1842 sb.append("default");
1843 } else if (this.vibrate != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001844 int N = this.vibrate.length-1;
1845 sb.append("[");
1846 for (int i=0; i<N; i++) {
1847 sb.append(this.vibrate[i]);
1848 sb.append(',');
1849 }
Simon Schoar8cf97d92009-06-10 22:08:37 +02001850 if (N != -1) {
1851 sb.append(this.vibrate[N]);
1852 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001853 sb.append("]");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001854 } else {
1855 sb.append("null");
1856 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001857 sb.append(" sound=");
Daniel Sandler6738eee2012-11-16 12:03:32 -05001858 if ((this.defaults & DEFAULT_SOUND) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001859 sb.append("default");
Daniel Sandler6738eee2012-11-16 12:03:32 -05001860 } else if (this.sound != null) {
1861 sb.append(this.sound.toString());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001862 } else {
1863 sb.append("null");
1864 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001865 sb.append(" defaults=0x");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001866 sb.append(Integer.toHexString(this.defaults));
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001867 sb.append(" flags=0x");
Daniel Sandlere46cbd32010-06-17 10:35:26 -04001868 sb.append(Integer.toHexString(this.flags));
Dan Sandler26e81cf2014-05-06 10:01:27 -04001869 sb.append(String.format(" color=0x%08x", this.color));
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001870 if (this.category != null) {
1871 sb.append(" category=");
1872 sb.append(this.category);
1873 }
1874 if (this.mGroupKey != null) {
1875 sb.append(" groupKey=");
1876 sb.append(this.mGroupKey);
1877 }
1878 if (this.mSortKey != null) {
1879 sb.append(" sortKey=");
1880 sb.append(this.mSortKey);
1881 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001882 if (actions != null) {
Dan Sandler1b718782014-07-18 12:43:45 -04001883 sb.append(" actions=");
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001884 sb.append(actions.length);
Dan Sandler1b718782014-07-18 12:43:45 -04001885 }
1886 sb.append(" vis=");
1887 sb.append(visibilityToString(this.visibility));
1888 if (this.publicVersion != null) {
1889 sb.append(" publicVersion=");
1890 sb.append(publicVersion.toString());
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001891 }
1892 sb.append(")");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001893 return sb.toString();
1894 }
Joe Onorato46439ce2010-11-19 13:56:21 -08001895
Dan Sandler1b718782014-07-18 12:43:45 -04001896 /**
1897 * {@hide}
1898 */
1899 public static String visibilityToString(int vis) {
1900 switch (vis) {
1901 case VISIBILITY_PRIVATE:
1902 return "PRIVATE";
1903 case VISIBILITY_PUBLIC:
1904 return "PUBLIC";
1905 case VISIBILITY_SECRET:
1906 return "SECRET";
1907 default:
1908 return "UNKNOWN(" + String.valueOf(vis) + ")";
1909 }
1910 }
1911
Joe Onoratocb109a02011-01-18 17:57:41 -08001912 /**
John Spurlock1d881a12015-03-18 19:21:54 -04001913 * {@hide}
1914 */
1915 public static String priorityToString(@Priority int pri) {
1916 switch (pri) {
1917 case PRIORITY_MIN:
1918 return "MIN";
1919 case PRIORITY_LOW:
1920 return "LOW";
1921 case PRIORITY_DEFAULT:
1922 return "DEFAULT";
1923 case PRIORITY_HIGH:
1924 return "HIGH";
1925 case PRIORITY_MAX:
1926 return "MAX";
1927 default:
1928 return "UNKNOWN(" + String.valueOf(pri) + ")";
1929 }
1930 }
1931
1932 /**
Dan Sandlerd63f9322015-05-06 15:18:49 -04001933 * The small icon representing this notification in the status bar and content view.
1934 *
1935 * @return the small icon representing this notification.
1936 *
1937 * @see Builder#getSmallIcon()
1938 * @see Builder#setSmallIcon(Icon)
1939 */
1940 public Icon getSmallIcon() {
1941 return mSmallIcon;
1942 }
1943
1944 /**
1945 * Used when notifying to clean up legacy small icons.
1946 * @hide
1947 */
1948 public void setSmallIcon(Icon icon) {
1949 mSmallIcon = icon;
1950 }
1951
1952 /**
1953 * The large icon shown in this notification's content view.
1954 * @see Builder#getLargeIcon()
1955 * @see Builder#setLargeIcon(Icon)
1956 */
1957 public Icon getLargeIcon() {
1958 return mLargeIcon;
1959 }
1960
1961 /**
Christoph Studer4600f9b2014-07-22 22:44:43 +02001962 * @hide
1963 */
1964 public boolean isValid() {
1965 // Would like to check for icon!=0 here, too, but NotificationManagerService accepts that
1966 // for legacy reasons.
1967 return contentView != null || extras.getBoolean(Builder.EXTRA_REBUILD_CONTENT_VIEW);
1968 }
1969
1970 /**
Christoph Studerc8db24b2014-07-25 17:50:30 +02001971 * @hide
1972 */
1973 public boolean isGroupSummary() {
1974 return mGroupKey != null && (flags & FLAG_GROUP_SUMMARY) != 0;
1975 }
1976
1977 /**
1978 * @hide
1979 */
1980 public boolean isGroupChild() {
1981 return mGroupKey != null && (flags & FLAG_GROUP_SUMMARY) == 0;
1982 }
1983
1984 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001985 * Builder class for {@link Notification} objects.
Joe Malin8d40d042012-11-05 11:36:40 -08001986 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001987 * Provides a convenient way to set the various fields of a {@link Notification} and generate
Scott Main183bf112012-08-13 19:12:13 -07001988 * content views using the platform's notification layout template. If your app supports
1989 * versions of Android as old as API level 4, you can instead use
1990 * {@link android.support.v4.app.NotificationCompat.Builder NotificationCompat.Builder},
1991 * available in the <a href="{@docRoot}tools/extras/support-library.html">Android Support
1992 * library</a>.
Joe Malin8d40d042012-11-05 11:36:40 -08001993 *
Scott Main183bf112012-08-13 19:12:13 -07001994 * <p>Example:
Joe Malin8d40d042012-11-05 11:36:40 -08001995 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001996 * <pre class="prettyprint">
Scott Main183bf112012-08-13 19:12:13 -07001997 * Notification noti = new Notification.Builder(mContext)
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001998 * .setContentTitle(&quot;New mail from &quot; + sender.toString())
1999 * .setContentText(subject)
2000 * .setSmallIcon(R.drawable.new_mail)
2001 * .setLargeIcon(aBitmap)
Chris Wrenfbd96ba2012-05-01 12:03:58 -04002002 * .build();
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002003 * </pre>
Joe Onoratocb109a02011-01-18 17:57:41 -08002004 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002005 public static class Builder {
Daniel Sandler602ad1c2012-06-12 16:06:27 -04002006 private static final int MAX_ACTION_BUTTONS = 3;
Jorim Jaggi445d3c02014-08-19 22:33:42 +02002007 private static final float LARGE_TEXT_SCALE = 1.3f;
Daniel Sandler8680bf82012-05-15 16:52:52 -04002008
Christoph Studer4600f9b2014-07-22 22:44:43 +02002009 /**
2010 * @hide
2011 */
2012 public static final String EXTRA_NEEDS_REBUILD = "android.rebuild";
2013
2014 /**
2015 * @hide
2016 */
2017 public static final String EXTRA_REBUILD_LARGE_ICON = "android.rebuild.largeIcon";
2018 /**
2019 * @hide
2020 */
2021 public static final String EXTRA_REBUILD_CONTENT_VIEW = "android.rebuild.contentView";
2022 /**
2023 * @hide
2024 */
2025 public static final String EXTRA_REBUILD_CONTENT_VIEW_ACTION_COUNT =
2026 "android.rebuild.contentViewActionCount";
2027 /**
2028 * @hide
2029 */
2030 public static final String EXTRA_REBUILD_BIG_CONTENT_VIEW
2031 = "android.rebuild.bigView";
2032 /**
2033 * @hide
2034 */
2035 public static final String EXTRA_REBUILD_BIG_CONTENT_VIEW_ACTION_COUNT
2036 = "android.rebuild.bigViewActionCount";
2037 /**
2038 * @hide
2039 */
2040 public static final String EXTRA_REBUILD_HEADS_UP_CONTENT_VIEW
2041 = "android.rebuild.hudView";
2042 /**
2043 * @hide
2044 */
2045 public static final String EXTRA_REBUILD_HEADS_UP_CONTENT_VIEW_ACTION_COUNT
2046 = "android.rebuild.hudViewActionCount";
2047
2048 /**
Kenny Guy77320062014-08-27 21:37:15 +01002049 * The ApplicationInfo of the package that created the notification, used to create
2050 * a context to rebuild the notification via a Builder.
2051 * @hide
Christoph Studer4600f9b2014-07-22 22:44:43 +02002052 */
Kenny Guy77320062014-08-27 21:37:15 +01002053 private static final String EXTRA_REBUILD_CONTEXT_APPLICATION_INFO =
2054 "android.rebuild.applicationInfo";
Christoph Studer4600f9b2014-07-22 22:44:43 +02002055
2056 // Whether to enable stripping (at post time) & rebuilding (at listener receive time) of
2057 // memory intensive resources.
2058 private static final boolean STRIP_AND_REBUILD = true;
2059
Joe Onorato46439ce2010-11-19 13:56:21 -08002060 private Context mContext;
2061
2062 private long mWhen;
Dan Sandlerd63f9322015-05-06 15:18:49 -04002063 private Icon mSmallIcon, mLargeIcon;
Joe Onorato46439ce2010-11-19 13:56:21 -08002064 private int mSmallIconLevel;
Joe Onorato8595a3d2010-11-19 18:12:07 -08002065 private int mNumber;
Joe Onorato46439ce2010-11-19 13:56:21 -08002066 private CharSequence mContentTitle;
2067 private CharSequence mContentText;
2068 private CharSequence mContentInfo;
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002069 private CharSequence mSubText;
Joe Onorato46439ce2010-11-19 13:56:21 -08002070 private PendingIntent mContentIntent;
2071 private RemoteViews mContentView;
2072 private PendingIntent mDeleteIntent;
2073 private PendingIntent mFullScreenIntent;
2074 private CharSequence mTickerText;
2075 private RemoteViews mTickerView;
Joe Onorato46439ce2010-11-19 13:56:21 -08002076 private Uri mSound;
2077 private int mAudioStreamType;
John Spurlockc0650f022014-07-19 13:22:39 -04002078 private AudioAttributes mAudioAttributes;
Joe Onorato46439ce2010-11-19 13:56:21 -08002079 private long[] mVibrate;
2080 private int mLedArgb;
2081 private int mLedOnMs;
2082 private int mLedOffMs;
2083 private int mDefaults;
2084 private int mFlags;
Jeff Sharkey1c400132011-08-05 14:50:13 -07002085 private int mProgressMax;
2086 private int mProgress;
2087 private boolean mProgressIndeterminate;
John Spurlockfd7f1e02014-03-18 16:41:57 -04002088 private String mCategory;
Griff Hazen5cadc3b2014-05-20 09:55:39 -07002089 private String mGroupKey;
2090 private String mSortKey;
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002091 private Bundle mExtras;
2092 private int mPriority;
Daniel Sandler8680bf82012-05-15 16:52:52 -04002093 private ArrayList<Action> mActions = new ArrayList<Action>(MAX_ACTION_BUTTONS);
Daniel Sandlera2985ed2012-04-03 16:42:00 -04002094 private boolean mUseChronometer;
Chris Wrenfbd96ba2012-05-01 12:03:58 -04002095 private Style mStyle;
Daniel Sandler0c890492012-09-12 17:23:10 -07002096 private boolean mShowWhen = true;
Dan Sandler0bf2ed82013-12-21 23:33:41 -06002097 private int mVisibility = VISIBILITY_PRIVATE;
2098 private Notification mPublicVersion = null;
Dan Sandler26e81cf2014-05-06 10:01:27 -04002099 private final NotificationColorUtil mColorUtil;
Chris Wrendde75302014-03-26 17:24:15 -04002100 private ArrayList<String> mPeople;
Dan Sandler26e81cf2014-05-06 10:01:27 -04002101 private int mColor = COLOR_DEFAULT;
Christoph Studer7ac80e62014-08-04 16:01:57 +02002102
2103 /**
Kenny Guy8942bcd2014-09-08 21:09:47 +01002104 * The user that built the notification originally.
2105 */
2106 private int mOriginatingUserId;
2107
2108 /**
Christoph Studer4600f9b2014-07-22 22:44:43 +02002109 * Contains extras related to rebuilding during the build phase.
2110 */
2111 private Bundle mRebuildBundle = new Bundle();
2112 /**
2113 * Contains the notification to rebuild when this Builder is in "rebuild" mode.
2114 * Null otherwise.
2115 */
2116 private Notification mRebuildNotification = null;
2117
Joe Onoratocb109a02011-01-18 17:57:41 -08002118 /**
Jorim Jaggid05aa3e2014-08-28 17:52:27 +02002119 * Whether the build notification has three lines. This is used to make the top padding for
2120 * both the contracted and expanded layout consistent.
2121 *
2122 * <p>
2123 * This field is only valid during the build phase.
2124 */
2125 private boolean mHasThreeLines;
2126
2127 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002128 * Constructs a new Builder with the defaults:
Joe Onoratocb109a02011-01-18 17:57:41 -08002129 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002130
2131 * <table>
2132 * <tr><th align=right>priority</th>
2133 * <td>{@link #PRIORITY_DEFAULT}</td></tr>
2134 * <tr><th align=right>when</th>
2135 * <td>now ({@link System#currentTimeMillis()})</td></tr>
2136 * <tr><th align=right>audio stream</th>
2137 * <td>{@link #STREAM_DEFAULT}</td></tr>
2138 * </table>
Joe Onoratocb109a02011-01-18 17:57:41 -08002139 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002140
2141 * @param context
2142 * A {@link Context} that will be used by the Builder to construct the
2143 * RemoteViews. The Context will not be held past the lifetime of this Builder
2144 * object.
Joe Onoratocb109a02011-01-18 17:57:41 -08002145 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002146 public Builder(Context context) {
Adam Powellbf06fa02014-05-30 12:32:18 -07002147 /*
2148 * Important compatibility note!
2149 * Some apps out in the wild create a Notification.Builder in their Activity subclass
2150 * constructor for later use. At this point Activities - themselves subclasses of
2151 * ContextWrapper - do not have their inner Context populated yet. This means that
2152 * any calls to Context methods from within this constructor can cause NPEs in existing
2153 * apps. Any data populated from mContext should therefore be populated lazily to
2154 * preserve compatibility.
2155 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002156 mContext = context;
Andy Stadler110988c2010-12-03 14:29:16 -08002157
2158 // Set defaults to match the defaults of a Notification
Joe Onorato46439ce2010-11-19 13:56:21 -08002159 mWhen = System.currentTimeMillis();
Andy Stadler110988c2010-12-03 14:29:16 -08002160 mAudioStreamType = STREAM_DEFAULT;
John Spurlockc0650f022014-07-19 13:22:39 -04002161 mAudioAttributes = AUDIO_ATTRIBUTES_DEFAULT;
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002162 mPriority = PRIORITY_DEFAULT;
Chris Wrendde75302014-03-26 17:24:15 -04002163 mPeople = new ArrayList<String>();
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01002164
Dianne Hackborn955d8d62014-10-07 20:17:19 -07002165 mColorUtil = context.getApplicationInfo().targetSdkVersion < Build.VERSION_CODES.LOLLIPOP ?
Dan Sandler05c362d2014-09-03 00:16:27 +02002166 NotificationColorUtil.getInstance(mContext) : null;
Joe Onorato46439ce2010-11-19 13:56:21 -08002167 }
2168
Joe Onoratocb109a02011-01-18 17:57:41 -08002169 /**
Christoph Studer4600f9b2014-07-22 22:44:43 +02002170 * Creates a Builder for rebuilding the given Notification.
2171 * <p>
2172 * Call {@link #rebuild()} to retrieve the rebuilt version of 'n'.
2173 */
2174 private Builder(Context context, Notification n) {
2175 this(context);
2176 mRebuildNotification = n;
2177 restoreFromNotification(n);
2178
2179 Style style = null;
2180 Bundle extras = n.extras;
2181 String templateClass = extras.getString(EXTRA_TEMPLATE);
2182 if (!TextUtils.isEmpty(templateClass)) {
2183 Class<? extends Style> styleClass = getNotificationStyleClass(templateClass);
2184 if (styleClass == null) {
2185 Log.d(TAG, "Unknown style class: " + styleClass);
2186 return;
2187 }
2188
2189 try {
2190 Constructor<? extends Style> constructor = styleClass.getConstructor();
Alan Viverette904de2e2015-05-04 10:32:57 -07002191 constructor.setAccessible(true);
Christoph Studer4600f9b2014-07-22 22:44:43 +02002192 style = constructor.newInstance();
2193 style.restoreFromExtras(extras);
2194 } catch (Throwable t) {
2195 Log.e(TAG, "Could not create Style", t);
2196 return;
2197 }
2198 }
2199 if (style != null) {
2200 setStyle(style);
2201 }
2202 }
2203
2204 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002205 * Add a timestamp pertaining to the notification (usually the time the event occurred).
Daniel Sandler0c890492012-09-12 17:23:10 -07002206 * It will be shown in the notification content view by default; use
Griff Hazen50c11652014-05-16 09:46:31 -07002207 * {@link #setShowWhen(boolean) setShowWhen} to control this.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002208 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002209 * @see Notification#when
Joe Onoratocb109a02011-01-18 17:57:41 -08002210 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002211 public Builder setWhen(long when) {
2212 mWhen = when;
2213 return this;
2214 }
2215
Joe Onoratocb109a02011-01-18 17:57:41 -08002216 /**
Griff Hazen50c11652014-05-16 09:46:31 -07002217 * Control whether the timestamp set with {@link #setWhen(long) setWhen} is shown
Daniel Sandler0c890492012-09-12 17:23:10 -07002218 * in the content view.
2219 */
2220 public Builder setShowWhen(boolean show) {
2221 mShowWhen = show;
2222 return this;
2223 }
2224
2225 /**
Daniel Sandlerd33b8032012-05-10 11:41:48 -04002226 * Show the {@link Notification#when} field as a stopwatch.
Joe Malin8d40d042012-11-05 11:36:40 -08002227 *
2228 * Instead of presenting <code>when</code> as a timestamp, the notification will show an
Daniel Sandlerd33b8032012-05-10 11:41:48 -04002229 * automatically updating display of the minutes and seconds since <code>when</code>.
Daniel Sandlera2985ed2012-04-03 16:42:00 -04002230 *
Daniel Sandlerd33b8032012-05-10 11:41:48 -04002231 * Useful when showing an elapsed time (like an ongoing phone call).
2232 *
2233 * @see android.widget.Chronometer
Daniel Sandlera2985ed2012-04-03 16:42:00 -04002234 * @see Notification#when
2235 */
2236 public Builder setUsesChronometer(boolean b) {
2237 mUseChronometer = b;
2238 return this;
2239 }
2240
2241 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002242 * Set the small icon resource, which will be used to represent the notification in the
2243 * status bar.
Joe Onoratocb109a02011-01-18 17:57:41 -08002244 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002245
2246 * The platform template for the expanded view will draw this icon in the left, unless a
2247 * {@link #setLargeIcon(Bitmap) large icon} has also been specified, in which case the small
2248 * icon will be moved to the right-hand side.
2249 *
2250
2251 * @param icon
2252 * A resource ID in the application's package of the drawable to use.
2253 * @see Notification#icon
Joe Onoratocb109a02011-01-18 17:57:41 -08002254 */
Tor Norbye7b9c9122013-05-30 16:48:33 -07002255 public Builder setSmallIcon(@DrawableRes int icon) {
Dan Sandlerd63f9322015-05-06 15:18:49 -04002256 return setSmallIcon(icon != 0
2257 ? Icon.createWithResource(mContext, icon)
2258 : null);
Joe Onorato46439ce2010-11-19 13:56:21 -08002259 }
2260
Joe Onoratocb109a02011-01-18 17:57:41 -08002261 /**
2262 * A variant of {@link #setSmallIcon(int) setSmallIcon(int)} that takes an additional
2263 * level parameter for when the icon is a {@link android.graphics.drawable.LevelListDrawable
2264 * LevelListDrawable}.
2265 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002266 * @param icon A resource ID in the application's package of the drawable to use.
Joe Onoratocb109a02011-01-18 17:57:41 -08002267 * @param level The level to use for the icon.
2268 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002269 * @see Notification#icon
2270 * @see Notification#iconLevel
Joe Onoratocb109a02011-01-18 17:57:41 -08002271 */
Tor Norbye7b9c9122013-05-30 16:48:33 -07002272 public Builder setSmallIcon(@DrawableRes int icon, int level) {
Daniel Sandler09e51a72015-05-14 15:32:28 +00002273 mSmallIconLevel = level;
Dan Sandlerd63f9322015-05-06 15:18:49 -04002274 return setSmallIcon(icon);
2275 }
2276
2277 /**
2278 * Set the small icon, which will be used to represent the notification in the
2279 * status bar and content view (unless overriden there by a
2280 * {@link #setLargeIcon(Bitmap) large icon}).
2281 *
2282 * @param icon An Icon object to use.
2283 * @see Notification#icon
2284 */
2285 public Builder setSmallIcon(Icon icon) {
2286 mSmallIcon = icon;
Joe Onorato46439ce2010-11-19 13:56:21 -08002287 return this;
2288 }
2289
Joe Onoratocb109a02011-01-18 17:57:41 -08002290 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002291 * Set the first line of text in the platform notification template.
Joe Onoratocb109a02011-01-18 17:57:41 -08002292 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002293 public Builder setContentTitle(CharSequence title) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04002294 mContentTitle = safeCharSequence(title);
Joe Onorato46439ce2010-11-19 13:56:21 -08002295 return this;
2296 }
2297
Joe Onoratocb109a02011-01-18 17:57:41 -08002298 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002299 * Set the second line of text in the platform notification template.
Joe Onoratocb109a02011-01-18 17:57:41 -08002300 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002301 public Builder setContentText(CharSequence text) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04002302 mContentText = safeCharSequence(text);
Joe Onorato46439ce2010-11-19 13:56:21 -08002303 return this;
2304 }
2305
Joe Onoratocb109a02011-01-18 17:57:41 -08002306 /**
Joe Malin8d40d042012-11-05 11:36:40 -08002307 * Set the third line of text in the platform notification template.
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04002308 * Don't use if you're also using {@link #setProgress(int, int, boolean)}; they occupy the
2309 * same location in the standard template.
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002310 */
2311 public Builder setSubText(CharSequence text) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04002312 mSubText = safeCharSequence(text);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002313 return this;
2314 }
2315
2316 /**
Joe Onoratocb109a02011-01-18 17:57:41 -08002317 * Set the large number at the right-hand side of the notification. This is
2318 * equivalent to setContentInfo, although it might show the number in a different
2319 * font size for readability.
2320 */
Joe Onorato8595a3d2010-11-19 18:12:07 -08002321 public Builder setNumber(int number) {
2322 mNumber = number;
2323 return this;
2324 }
2325
Joe Onoratocb109a02011-01-18 17:57:41 -08002326 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002327 * A small piece of additional information pertaining to this notification.
2328 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002329 * The platform template will draw this on the last line of the notification, at the far
2330 * right (to the right of a smallIcon if it has been placed there).
Joe Onoratocb109a02011-01-18 17:57:41 -08002331 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002332 public Builder setContentInfo(CharSequence info) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04002333 mContentInfo = safeCharSequence(info);
Joe Onorato46439ce2010-11-19 13:56:21 -08002334 return this;
2335 }
2336
Joe Onoratocb109a02011-01-18 17:57:41 -08002337 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002338 * Set the progress this notification represents.
2339 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002340 * The platform template will represent this using a {@link ProgressBar}.
Jeff Sharkey1c400132011-08-05 14:50:13 -07002341 */
2342 public Builder setProgress(int max, int progress, boolean indeterminate) {
2343 mProgressMax = max;
2344 mProgress = progress;
2345 mProgressIndeterminate = indeterminate;
2346 return this;
2347 }
2348
2349 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002350 * Supply a custom RemoteViews to use instead of the platform template.
2351 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002352 * @see Notification#contentView
Joe Onoratocb109a02011-01-18 17:57:41 -08002353 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002354 public Builder setContent(RemoteViews views) {
2355 mContentView = views;
2356 return this;
2357 }
2358
Joe Onoratocb109a02011-01-18 17:57:41 -08002359 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002360 * Supply a {@link PendingIntent} to be sent when the notification is clicked.
2361 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002362 * As of {@link android.os.Build.VERSION_CODES#HONEYCOMB}, if this field is unset and you
2363 * have specified a custom RemoteViews with {@link #setContent(RemoteViews)}, you can use
2364 * {@link RemoteViews#setOnClickPendingIntent RemoteViews.setOnClickPendingIntent(int,PendingIntent)}
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002365 * to assign PendingIntents to individual views in that custom layout (i.e., to create
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002366 * clickable buttons inside the notification view).
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002367 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002368 * @see Notification#contentIntent Notification.contentIntent
Joe Onoratocb109a02011-01-18 17:57:41 -08002369 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002370 public Builder setContentIntent(PendingIntent intent) {
2371 mContentIntent = intent;
2372 return this;
2373 }
2374
Joe Onoratocb109a02011-01-18 17:57:41 -08002375 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002376 * Supply a {@link PendingIntent} to send when the notification is cleared explicitly by the user.
2377 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002378 * @see Notification#deleteIntent
Joe Onoratocb109a02011-01-18 17:57:41 -08002379 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002380 public Builder setDeleteIntent(PendingIntent intent) {
2381 mDeleteIntent = intent;
2382 return this;
2383 }
2384
Joe Onoratocb109a02011-01-18 17:57:41 -08002385 /**
2386 * An intent to launch instead of posting the notification to the status bar.
2387 * Only for use with extremely high-priority notifications demanding the user's
2388 * <strong>immediate</strong> attention, such as an incoming phone call or
2389 * alarm clock that the user has explicitly set to a particular time.
2390 * If this facility is used for something else, please give the user an option
2391 * to turn it off and use a normal notification, as this can be extremely
2392 * disruptive.
2393 *
Chris Wren47c20a12014-06-18 17:27:29 -04002394 * <p>
2395 * The system UI may choose to display a heads-up notification, instead of
2396 * launching this intent, while the user is using the device.
2397 * </p>
2398 *
Joe Onoratocb109a02011-01-18 17:57:41 -08002399 * @param intent The pending intent to launch.
2400 * @param highPriority Passing true will cause this notification to be sent
2401 * even if other notifications are suppressed.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002402 *
2403 * @see Notification#fullScreenIntent
Joe Onoratocb109a02011-01-18 17:57:41 -08002404 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002405 public Builder setFullScreenIntent(PendingIntent intent, boolean highPriority) {
2406 mFullScreenIntent = intent;
2407 setFlag(FLAG_HIGH_PRIORITY, highPriority);
2408 return this;
2409 }
2410
Joe Onoratocb109a02011-01-18 17:57:41 -08002411 /**
Dan Sandler5fcdf6e2014-07-18 11:31:15 -04002412 * Set the "ticker" text which is sent to accessibility services.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002413 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002414 * @see Notification#tickerText
Joe Onoratocb109a02011-01-18 17:57:41 -08002415 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002416 public Builder setTicker(CharSequence tickerText) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04002417 mTickerText = safeCharSequence(tickerText);
Joe Onorato46439ce2010-11-19 13:56:21 -08002418 return this;
2419 }
2420
Joe Onoratocb109a02011-01-18 17:57:41 -08002421 /**
Dan Sandler5fcdf6e2014-07-18 11:31:15 -04002422 * Obsolete version of {@link #setTicker(CharSequence)}.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002423 *
Joe Onoratocb109a02011-01-18 17:57:41 -08002424 */
Dan Sandler5fcdf6e2014-07-18 11:31:15 -04002425 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -08002426 public Builder setTicker(CharSequence tickerText, RemoteViews views) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04002427 mTickerText = safeCharSequence(tickerText);
Dan Sandler5fcdf6e2014-07-18 11:31:15 -04002428 mTickerView = views; // we'll save it for you anyway
Joe Onorato46439ce2010-11-19 13:56:21 -08002429 return this;
2430 }
2431
Joe Onoratocb109a02011-01-18 17:57:41 -08002432 /**
Dan Sandlerd63f9322015-05-06 15:18:49 -04002433 * Add a large icon to the notification content view.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002434 *
2435 * In the platform template, this image will be shown on the left of the notification view
Dan Sandlerd63f9322015-05-06 15:18:49 -04002436 * in place of the {@link #setSmallIcon(Icon) small icon} (which will be placed in a small
2437 * badge atop the large icon).
Dan Sandler08a04c12015-05-06 15:18:49 -04002438 */
Dan Sandlerd63f9322015-05-06 15:18:49 -04002439 public Builder setLargeIcon(Bitmap b) {
2440 return setLargeIcon(b != null ? Icon.createWithBitmap(b) : null);
2441 }
2442
2443 /**
2444 * Add a large icon to the notification content view.
2445 *
2446 * In the platform template, this image will be shown on the left of the notification view
2447 * in place of the {@link #setSmallIcon(Icon) small icon} (which will be placed in a small
2448 * badge atop the large icon).
2449 */
2450 public Builder setLargeIcon(Icon icon) {
Joe Onorato46439ce2010-11-19 13:56:21 -08002451 mLargeIcon = icon;
2452 return this;
2453 }
2454
Joe Onoratocb109a02011-01-18 17:57:41 -08002455 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002456 * Set the sound to play.
2457 *
John Spurlockc0650f022014-07-19 13:22:39 -04002458 * It will be played using the {@link #AUDIO_ATTRIBUTES_DEFAULT default audio attributes}
2459 * for notifications.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002460 *
Chris Wren47c20a12014-06-18 17:27:29 -04002461 * <p>
2462 * A notification that is noisy is more likely to be presented as a heads-up notification.
2463 * </p>
2464 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002465 * @see Notification#sound
Joe Onoratocb109a02011-01-18 17:57:41 -08002466 */
Joe Onorato52f80cd2010-11-21 15:34:48 -08002467 public Builder setSound(Uri sound) {
2468 mSound = sound;
John Spurlockc0650f022014-07-19 13:22:39 -04002469 mAudioAttributes = AUDIO_ATTRIBUTES_DEFAULT;
Joe Onorato52f80cd2010-11-21 15:34:48 -08002470 return this;
2471 }
2472
Joe Onoratocb109a02011-01-18 17:57:41 -08002473 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002474 * Set the sound to play, along with a specific stream on which to play it.
Joe Onoratocb109a02011-01-18 17:57:41 -08002475 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002476 * See {@link android.media.AudioManager} for the <code>STREAM_</code> constants.
2477 *
Chris Wren47c20a12014-06-18 17:27:29 -04002478 * <p>
2479 * A notification that is noisy is more likely to be presented as a heads-up notification.
2480 * </p>
John Spurlockc0650f022014-07-19 13:22:39 -04002481 * @deprecated use {@link #setSound(Uri, AudioAttributes)} instead.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002482 * @see Notification#sound
Joe Onoratocb109a02011-01-18 17:57:41 -08002483 */
Jean-Michel Trivi81f871e2014-08-06 16:32:38 -07002484 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -08002485 public Builder setSound(Uri sound, int streamType) {
2486 mSound = sound;
2487 mAudioStreamType = streamType;
2488 return this;
2489 }
2490
Joe Onoratocb109a02011-01-18 17:57:41 -08002491 /**
John Spurlockc0650f022014-07-19 13:22:39 -04002492 * Set the sound to play, along with specific {@link AudioAttributes audio attributes} to
2493 * use during playback.
2494 *
2495 * <p>
2496 * A notification that is noisy is more likely to be presented as a heads-up notification.
2497 * </p>
2498 *
2499 * @see Notification#sound
2500 */
2501 public Builder setSound(Uri sound, AudioAttributes audioAttributes) {
2502 mSound = sound;
2503 mAudioAttributes = audioAttributes;
2504 return this;
2505 }
2506
2507 /**
Joe Onoratocb109a02011-01-18 17:57:41 -08002508 * Set the vibration pattern to use.
2509 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002510 * See {@link android.os.Vibrator#vibrate(long[], int)} for a discussion of the
2511 * <code>pattern</code> parameter.
2512 *
Chris Wren47c20a12014-06-18 17:27:29 -04002513 * <p>
2514 * A notification that vibrates is more likely to be presented as a heads-up notification.
2515 * </p>
2516 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002517 * @see Notification#vibrate
Joe Onoratocb109a02011-01-18 17:57:41 -08002518 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002519 public Builder setVibrate(long[] pattern) {
2520 mVibrate = pattern;
2521 return this;
2522 }
2523
Joe Onoratocb109a02011-01-18 17:57:41 -08002524 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002525 * Set the desired color for the indicator LED on the device, as well as the
2526 * blink duty cycle (specified in milliseconds).
2527 *
2528
2529 * Not all devices will honor all (or even any) of these values.
2530 *
2531
2532 * @see Notification#ledARGB
2533 * @see Notification#ledOnMS
2534 * @see Notification#ledOffMS
Joe Onoratocb109a02011-01-18 17:57:41 -08002535 */
Tor Norbye80756e32015-03-02 09:39:27 -08002536 public Builder setLights(@ColorInt int argb, int onMs, int offMs) {
Joe Onorato46439ce2010-11-19 13:56:21 -08002537 mLedArgb = argb;
2538 mLedOnMs = onMs;
2539 mLedOffMs = offMs;
Joe Onorato46439ce2010-11-19 13:56:21 -08002540 return this;
2541 }
2542
Joe Onoratocb109a02011-01-18 17:57:41 -08002543 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002544 * Set whether this is an "ongoing" notification.
Joe Onoratocb109a02011-01-18 17:57:41 -08002545 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002546
2547 * Ongoing notifications cannot be dismissed by the user, so your application or service
2548 * must take care of canceling them.
2549 *
2550
2551 * They are typically used to indicate a background task that the user is actively engaged
2552 * with (e.g., playing music) or is pending in some way and therefore occupying the device
2553 * (e.g., a file download, sync operation, active network connection).
2554 *
2555
2556 * @see Notification#FLAG_ONGOING_EVENT
2557 * @see Service#setForeground(boolean)
Joe Onoratocb109a02011-01-18 17:57:41 -08002558 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002559 public Builder setOngoing(boolean ongoing) {
2560 setFlag(FLAG_ONGOING_EVENT, ongoing);
2561 return this;
2562 }
2563
Joe Onoratocb109a02011-01-18 17:57:41 -08002564 /**
2565 * Set this flag if you would only like the sound, vibrate
2566 * and ticker to be played if the notification is not already showing.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002567 *
2568 * @see Notification#FLAG_ONLY_ALERT_ONCE
Joe Onoratocb109a02011-01-18 17:57:41 -08002569 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002570 public Builder setOnlyAlertOnce(boolean onlyAlertOnce) {
2571 setFlag(FLAG_ONLY_ALERT_ONCE, onlyAlertOnce);
2572 return this;
2573 }
2574
Joe Onoratocb109a02011-01-18 17:57:41 -08002575 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002576 * Make this notification automatically dismissed when the user touches it. The
2577 * PendingIntent set with {@link #setDeleteIntent} will be sent when this happens.
2578 *
2579 * @see Notification#FLAG_AUTO_CANCEL
Joe Onoratocb109a02011-01-18 17:57:41 -08002580 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002581 public Builder setAutoCancel(boolean autoCancel) {
Joe Onorato281d83f2011-01-04 17:13:10 -08002582 setFlag(FLAG_AUTO_CANCEL, autoCancel);
Joe Onorato46439ce2010-11-19 13:56:21 -08002583 return this;
2584 }
2585
Joe Onoratocb109a02011-01-18 17:57:41 -08002586 /**
Griff Hazendfcb0802014-02-11 12:00:00 -08002587 * Set whether or not this notification should not bridge to other devices.
2588 *
2589 * <p>Some notifications can be bridged to other devices for remote display.
2590 * This hint can be set to recommend this notification not be bridged.
2591 */
2592 public Builder setLocalOnly(boolean localOnly) {
2593 setFlag(FLAG_LOCAL_ONLY, localOnly);
2594 return this;
2595 }
2596
2597 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002598 * Set which notification properties will be inherited from system defaults.
Joe Onoratocb109a02011-01-18 17:57:41 -08002599 * <p>
2600 * The value should be one or more of the following fields combined with
2601 * bitwise-or:
2602 * {@link #DEFAULT_SOUND}, {@link #DEFAULT_VIBRATE}, {@link #DEFAULT_LIGHTS}.
2603 * <p>
2604 * For all default values, use {@link #DEFAULT_ALL}.
2605 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002606 public Builder setDefaults(int defaults) {
2607 mDefaults = defaults;
2608 return this;
2609 }
2610
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002611 /**
2612 * Set the priority of this notification.
2613 *
2614 * @see Notification#priority
2615 */
Tor Norbyed9273d62013-05-30 15:59:53 -07002616 public Builder setPriority(@Priority int pri) {
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002617 mPriority = pri;
2618 return this;
2619 }
Joe Malin8d40d042012-11-05 11:36:40 -08002620
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002621 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -04002622 * Set the notification category.
Joe Malin8d40d042012-11-05 11:36:40 -08002623 *
John Spurlockfd7f1e02014-03-18 16:41:57 -04002624 * @see Notification#category
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002625 */
John Spurlockfd7f1e02014-03-18 16:41:57 -04002626 public Builder setCategory(String category) {
2627 mCategory = category;
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002628 return this;
2629 }
2630
2631 /**
Chris Wrendde75302014-03-26 17:24:15 -04002632 * Add a person that is relevant to this notification.
2633 *
Chris Wrene6c48932014-09-29 17:19:27 -04002634 * <P>
2635 * Depending on user preferences, this annotation may allow the notification to pass
2636 * through interruption filters, and to appear more prominently in the user interface.
2637 * </P>
2638 *
2639 * <P>
2640 * The person should be specified by the {@code String} representation of a
2641 * {@link android.provider.ContactsContract.Contacts#CONTENT_LOOKUP_URI}.
2642 * </P>
2643 *
2644 * <P>The system will also attempt to resolve {@code mailto:} and {@code tel:} schema
2645 * URIs. The path part of these URIs must exist in the contacts database, in the
2646 * appropriate column, or the reference will be discarded as invalid. Telephone schema
2647 * URIs will be resolved by {@link android.provider.ContactsContract.PhoneLookup}.
2648 * </P>
2649 *
2650 * @param uri A URI for the person.
Chris Wrendde75302014-03-26 17:24:15 -04002651 * @see Notification#EXTRA_PEOPLE
2652 */
Chris Wrene6c48932014-09-29 17:19:27 -04002653 public Builder addPerson(String uri) {
2654 mPeople.add(uri);
Chris Wrendde75302014-03-26 17:24:15 -04002655 return this;
2656 }
2657
2658 /**
Griff Hazen5cadc3b2014-05-20 09:55:39 -07002659 * Set this notification to be part of a group of notifications sharing the same key.
2660 * Grouped notifications may display in a cluster or stack on devices which
2661 * support such rendering.
2662 *
2663 * <p>To make this notification the summary for its group, also call
2664 * {@link #setGroupSummary}. A sort order can be specified for group members by using
2665 * {@link #setSortKey}.
2666 * @param groupKey The group key of the group.
2667 * @return this object for method chaining
2668 */
2669 public Builder setGroup(String groupKey) {
2670 mGroupKey = groupKey;
2671 return this;
2672 }
2673
2674 /**
2675 * Set this notification to be the group summary for a group of notifications.
2676 * Grouped notifications may display in a cluster or stack on devices which
2677 * support such rendering. Requires a group key also be set using {@link #setGroup}.
2678 * @param isGroupSummary Whether this notification should be a group summary.
2679 * @return this object for method chaining
2680 */
2681 public Builder setGroupSummary(boolean isGroupSummary) {
2682 setFlag(FLAG_GROUP_SUMMARY, isGroupSummary);
2683 return this;
2684 }
2685
2686 /**
2687 * Set a sort key that orders this notification among other notifications from the
2688 * same package. This can be useful if an external sort was already applied and an app
2689 * would like to preserve this. Notifications will be sorted lexicographically using this
2690 * value, although providing different priorities in addition to providing sort key may
2691 * cause this value to be ignored.
2692 *
2693 * <p>This sort key can also be used to order members of a notification group. See
Griff Hazen9e1379f2014-05-20 12:50:51 -07002694 * {@link #setGroup}.
Griff Hazen5cadc3b2014-05-20 09:55:39 -07002695 *
2696 * @see String#compareTo(String)
2697 */
2698 public Builder setSortKey(String sortKey) {
2699 mSortKey = sortKey;
2700 return this;
2701 }
2702
2703 /**
Griff Hazen720042b2014-02-24 15:46:56 -08002704 * Merge additional metadata into this notification.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002705 *
Griff Hazen720042b2014-02-24 15:46:56 -08002706 * <p>Values within the Bundle will replace existing extras values in this Builder.
2707 *
2708 * @see Notification#extras
2709 */
Griff Hazen959591e2014-05-15 22:26:18 -07002710 public Builder addExtras(Bundle extras) {
2711 if (extras != null) {
2712 if (mExtras == null) {
2713 mExtras = new Bundle(extras);
2714 } else {
2715 mExtras.putAll(extras);
2716 }
Griff Hazen720042b2014-02-24 15:46:56 -08002717 }
2718 return this;
2719 }
2720
2721 /**
2722 * Set metadata for this notification.
2723 *
2724 * <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 -04002725 * current contents are copied into the Notification each time {@link #build()} is
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002726 * called.
2727 *
Griff Hazen720042b2014-02-24 15:46:56 -08002728 * <p>Replaces any existing extras values with those from the provided Bundle.
2729 * Use {@link #addExtras} to merge in metadata instead.
2730 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002731 * @see Notification#extras
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002732 */
Griff Hazen959591e2014-05-15 22:26:18 -07002733 public Builder setExtras(Bundle extras) {
2734 mExtras = extras;
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002735 return this;
2736 }
2737
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002738 /**
Griff Hazen720042b2014-02-24 15:46:56 -08002739 * Get the current metadata Bundle used by this notification Builder.
2740 *
2741 * <p>The returned Bundle is shared with this Builder.
2742 *
2743 * <p>The current contents of this Bundle are copied into the Notification each time
2744 * {@link #build()} is called.
2745 *
2746 * @see Notification#extras
2747 */
2748 public Bundle getExtras() {
2749 if (mExtras == null) {
2750 mExtras = new Bundle();
2751 }
2752 return mExtras;
2753 }
2754
2755 /**
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002756 * Add an action to this notification. Actions are typically displayed by
2757 * the system as a button adjacent to the notification content.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04002758 * <p>
2759 * Every action must have an icon (32dp square and matching the
2760 * <a href="{@docRoot}design/style/iconography.html#action-bar">Holo
2761 * Dark action bar</a> visual style), a textual label, and a {@link PendingIntent}.
2762 * <p>
2763 * A notification in its expanded form can display up to 3 actions, from left to right in
2764 * the order they were added. Actions will not be displayed when the notification is
2765 * collapsed, however, so be sure that any essential functions may be accessed by the user
2766 * in some other way (for example, in the Activity pointed to by {@link #contentIntent}).
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002767 *
2768 * @param icon Resource ID of a drawable that represents the action.
2769 * @param title Text describing the action.
2770 * @param intent PendingIntent to be fired when the action is invoked.
Dan Sandler86647982015-05-13 23:41:13 -04002771 *
2772 * @deprecated Use {@link #addAction(Action)} instead.
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002773 */
Dan Sandler86647982015-05-13 23:41:13 -04002774 @Deprecated
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002775 public Builder addAction(int icon, CharSequence title, PendingIntent intent) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04002776 mActions.add(new Action(icon, safeCharSequence(title), intent));
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002777 return this;
2778 }
2779
Chris Wrenfbd96ba2012-05-01 12:03:58 -04002780 /**
Griff Hazen959591e2014-05-15 22:26:18 -07002781 * Add an action to this notification. Actions are typically displayed by
2782 * the system as a button adjacent to the notification content.
2783 * <p>
2784 * Every action must have an icon (32dp square and matching the
2785 * <a href="{@docRoot}design/style/iconography.html#action-bar">Holo
2786 * Dark action bar</a> visual style), a textual label, and a {@link PendingIntent}.
2787 * <p>
2788 * A notification in its expanded form can display up to 3 actions, from left to right in
2789 * the order they were added. Actions will not be displayed when the notification is
2790 * collapsed, however, so be sure that any essential functions may be accessed by the user
2791 * in some other way (for example, in the Activity pointed to by {@link #contentIntent}).
2792 *
2793 * @param action The action to add.
2794 */
2795 public Builder addAction(Action action) {
2796 mActions.add(action);
2797 return this;
2798 }
2799
2800 /**
Chris Wrenfbd96ba2012-05-01 12:03:58 -04002801 * Add a rich notification style to be applied at build time.
2802 *
2803 * @param style Object responsible for modifying the notification style.
2804 */
2805 public Builder setStyle(Style style) {
2806 if (mStyle != style) {
2807 mStyle = style;
Daniel Sandlerc08dea22012-06-28 08:35:24 -07002808 if (mStyle != null) {
2809 mStyle.setBuilder(this);
2810 }
Chris Wrenfbd96ba2012-05-01 12:03:58 -04002811 }
2812 return this;
2813 }
2814
Dan Sandler0bf2ed82013-12-21 23:33:41 -06002815 /**
2816 * Specify the value of {@link #visibility}.
Griff Hazenb720abe2014-05-20 13:15:30 -07002817 *
Dan Sandler0bf2ed82013-12-21 23:33:41 -06002818 * @param visibility One of {@link #VISIBILITY_PRIVATE} (the default),
2819 * {@link #VISIBILITY_SECRET}, or {@link #VISIBILITY_PUBLIC}.
2820 *
2821 * @return The same Builder.
2822 */
2823 public Builder setVisibility(int visibility) {
2824 mVisibility = visibility;
2825 return this;
2826 }
2827
2828 /**
2829 * Supply a replacement Notification whose contents should be shown in insecure contexts
2830 * (i.e. atop the secure lockscreen). See {@link #visibility} and {@link #VISIBILITY_PUBLIC}.
2831 * @param n A replacement notification, presumably with some or all info redacted.
2832 * @return The same Builder.
2833 */
2834 public Builder setPublicVersion(Notification n) {
2835 mPublicVersion = n;
2836 return this;
2837 }
2838
Griff Hazenb720abe2014-05-20 13:15:30 -07002839 /**
Griff Hazen5cadc3b2014-05-20 09:55:39 -07002840 * Apply an extender to this notification builder. Extenders may be used to add
2841 * metadata or change options on this builder.
2842 */
Griff Hazen61a9e862014-05-22 16:05:19 -07002843 public Builder extend(Extender extender) {
2844 extender.extend(this);
Griff Hazen5cadc3b2014-05-20 09:55:39 -07002845 return this;
2846 }
2847
Dan Sandler4e787062015-06-17 15:09:48 -04002848 /**
2849 * @hide
2850 */
2851 public void setFlag(int mask, boolean value) {
Joe Onorato46439ce2010-11-19 13:56:21 -08002852 if (value) {
2853 mFlags |= mask;
2854 } else {
2855 mFlags &= ~mask;
2856 }
2857 }
2858
Dan Sandler26e81cf2014-05-06 10:01:27 -04002859 /**
2860 * Sets {@link Notification#color}.
2861 *
2862 * @param argb The accent color to use
2863 *
2864 * @return The same Builder.
2865 */
Tor Norbye80756e32015-03-02 09:39:27 -08002866 public Builder setColor(@ColorInt int argb) {
Dan Sandler26e81cf2014-05-06 10:01:27 -04002867 mColor = argb;
2868 return this;
2869 }
2870
Jorim Jaggid05aa3e2014-08-28 17:52:27 +02002871 private Drawable getProfileBadgeDrawable() {
Christoph Studer7ac80e62014-08-04 16:01:57 +02002872 // Note: This assumes that the current user can read the profile badge of the
2873 // originating user.
Svetoslavc7d62f02014-09-04 15:39:54 -07002874 return mContext.getPackageManager().getUserBadgeForDensity(
Kenny Guy8942bcd2014-09-08 21:09:47 +01002875 new UserHandle(mOriginatingUserId), 0);
Jorim Jaggid05aa3e2014-08-28 17:52:27 +02002876 }
2877
2878 private Bitmap getProfileBadge() {
2879 Drawable badge = getProfileBadgeDrawable();
Kenny Guy8a0101b2014-05-08 23:34:12 +01002880 if (badge == null) {
2881 return null;
2882 }
Jorim Jaggid05aa3e2014-08-28 17:52:27 +02002883 final int size = mContext.getResources().getDimensionPixelSize(
2884 R.dimen.notification_badge_size);
2885 Bitmap bitmap = Bitmap.createBitmap(size, size, Bitmap.Config.ARGB_8888);
Kenny Guy8a0101b2014-05-08 23:34:12 +01002886 Canvas canvas = new Canvas(bitmap);
Jorim Jaggid05aa3e2014-08-28 17:52:27 +02002887 badge.setBounds(0, 0, size, size);
Kenny Guy8a0101b2014-05-08 23:34:12 +01002888 badge.draw(canvas);
2889 return bitmap;
2890 }
2891
Kenny Guy98193ea2014-07-24 19:54:37 +01002892 private boolean addProfileBadge(RemoteViews contentView, int resId) {
2893 Bitmap profileBadge = getProfileBadge();
2894
2895 contentView.setViewVisibility(R.id.profile_badge_large_template, View.GONE);
2896 contentView.setViewVisibility(R.id.profile_badge_line2, View.GONE);
2897 contentView.setViewVisibility(R.id.profile_badge_line3, View.GONE);
2898
2899 if (profileBadge != null) {
2900 contentView.setImageViewBitmap(resId, profileBadge);
2901 contentView.setViewVisibility(resId, View.VISIBLE);
2902
2903 // Make sure Line 3 is visible. As badge will be here if there
2904 // is no text to display.
2905 if (resId == R.id.profile_badge_line3) {
2906 contentView.setViewVisibility(R.id.line3, View.VISIBLE);
2907 }
2908 return true;
2909 }
2910 return false;
2911 }
2912
Jorim Jaggid05aa3e2014-08-28 17:52:27 +02002913 private void shrinkLine3Text(RemoteViews contentView) {
2914 float subTextSize = mContext.getResources().getDimensionPixelSize(
2915 R.dimen.notification_subtext_size);
2916 contentView.setTextViewTextSize(R.id.text, TypedValue.COMPLEX_UNIT_PX, subTextSize);
2917 }
2918
Christoph Studerfe718432014-09-01 18:21:18 +02002919 private void unshrinkLine3Text(RemoteViews contentView) {
2920 float regularTextSize = mContext.getResources().getDimensionPixelSize(
2921 com.android.internal.R.dimen.notification_text_size);
2922 contentView.setTextViewTextSize(R.id.text, TypedValue.COMPLEX_UNIT_PX, regularTextSize);
2923 }
2924
2925 private void resetStandardTemplate(RemoteViews contentView) {
2926 removeLargeIconBackground(contentView);
2927 contentView.setViewPadding(R.id.icon, 0, 0, 0, 0);
2928 contentView.setImageViewResource(R.id.icon, 0);
2929 contentView.setInt(R.id.icon, "setBackgroundResource", 0);
2930 contentView.setViewVisibility(R.id.right_icon, View.GONE);
2931 contentView.setInt(R.id.right_icon, "setBackgroundResource", 0);
2932 contentView.setImageViewResource(R.id.right_icon, 0);
2933 contentView.setImageViewResource(R.id.icon, 0);
2934 contentView.setTextViewText(R.id.title, null);
2935 contentView.setTextViewText(R.id.text, null);
2936 unshrinkLine3Text(contentView);
2937 contentView.setTextViewText(R.id.text2, null);
2938 contentView.setViewVisibility(R.id.text2, View.GONE);
2939 contentView.setViewVisibility(R.id.info, View.GONE);
2940 contentView.setViewVisibility(R.id.time, View.GONE);
2941 contentView.setViewVisibility(R.id.line3, View.GONE);
2942 contentView.setViewVisibility(R.id.overflow_divider, View.GONE);
2943 contentView.setViewVisibility(R.id.progress, View.GONE);
Christoph Studerca1db712014-09-10 17:31:33 +02002944 contentView.setViewVisibility(R.id.chronometer, View.GONE);
2945 contentView.setViewVisibility(R.id.time, View.GONE);
Christoph Studerfe718432014-09-01 18:21:18 +02002946 }
2947
Jorim Jaggi445d3c02014-08-19 22:33:42 +02002948 private RemoteViews applyStandardTemplate(int resId) {
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02002949 return applyStandardTemplate(resId, true /* hasProgress */);
2950 }
2951
2952 /**
2953 * @param hasProgress whether the progress bar should be shown and set
2954 */
2955 private RemoteViews applyStandardTemplate(int resId, boolean hasProgress) {
Kenny Guy77320062014-08-27 21:37:15 +01002956 RemoteViews contentView = new BuilderRemoteViews(mContext.getApplicationInfo(), resId);
Dan Sandler539aad42014-08-04 00:43:39 -04002957
Christoph Studerfe718432014-09-01 18:21:18 +02002958 resetStandardTemplate(contentView);
2959
Daniel Sandler9f7936a2012-05-21 16:14:28 -04002960 boolean showLine3 = false;
2961 boolean showLine2 = false;
Kenny Guy98193ea2014-07-24 19:54:37 +01002962 boolean contentTextInLine2 = false;
Dan Sandler190d58d2014-05-15 09:33:39 -04002963
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01002964 if (mLargeIcon != null) {
Dan Sandlerd63f9322015-05-06 15:18:49 -04002965 contentView.setImageViewIcon(R.id.icon, mLargeIcon);
Christoph Studer239f8352014-08-25 15:13:18 +02002966 processLargeLegacyIcon(mLargeIcon, contentView);
Dan Sandlerd63f9322015-05-06 15:18:49 -04002967 contentView.setImageViewIcon(R.id.right_icon, mSmallIcon);
Dan Sandler190d58d2014-05-15 09:33:39 -04002968 contentView.setViewVisibility(R.id.right_icon, View.VISIBLE);
2969 processSmallRightIcon(mSmallIcon, contentView);
2970 } else { // small icon at left
Dan Sandlerd63f9322015-05-06 15:18:49 -04002971 contentView.setImageViewIcon(R.id.icon, mSmallIcon);
Dan Sandler190d58d2014-05-15 09:33:39 -04002972 contentView.setViewVisibility(R.id.icon, View.VISIBLE);
2973 processSmallIconAsLarge(mSmallIcon, contentView);
Joe Onorato561d3852010-11-20 18:09:34 -08002974 }
2975 if (mContentTitle != null) {
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01002976 contentView.setTextViewText(R.id.title, processLegacyText(mContentTitle));
Joe Onorato561d3852010-11-20 18:09:34 -08002977 }
2978 if (mContentText != null) {
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01002979 contentView.setTextViewText(R.id.text, processLegacyText(mContentText));
Daniel Sandler9f7936a2012-05-21 16:14:28 -04002980 showLine3 = true;
Joe Onorato561d3852010-11-20 18:09:34 -08002981 }
2982 if (mContentInfo != null) {
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01002983 contentView.setTextViewText(R.id.info, processLegacyText(mContentInfo));
Jeff Sharkey1c400132011-08-05 14:50:13 -07002984 contentView.setViewVisibility(R.id.info, View.VISIBLE);
Daniel Sandler9f7936a2012-05-21 16:14:28 -04002985 showLine3 = true;
Joe Onorato561d3852010-11-20 18:09:34 -08002986 } else if (mNumber > 0) {
Daniel Sandlerebce0112011-06-16 16:44:51 -04002987 final int tooBig = mContext.getResources().getInteger(
2988 R.integer.status_bar_notification_info_maxnum);
2989 if (mNumber > tooBig) {
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01002990 contentView.setTextViewText(R.id.info, processLegacyText(
2991 mContext.getResources().getString(
2992 R.string.status_bar_notification_info_overflow)));
Joe Onorato059a2f82011-01-04 10:27:01 -08002993 } else {
2994 NumberFormat f = NumberFormat.getIntegerInstance();
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01002995 contentView.setTextViewText(R.id.info, processLegacyText(f.format(mNumber)));
Joe Onorato059a2f82011-01-04 10:27:01 -08002996 }
Jeff Sharkey1c400132011-08-05 14:50:13 -07002997 contentView.setViewVisibility(R.id.info, View.VISIBLE);
Daniel Sandler9f7936a2012-05-21 16:14:28 -04002998 showLine3 = true;
Joe Onorato561d3852010-11-20 18:09:34 -08002999 } else {
3000 contentView.setViewVisibility(R.id.info, View.GONE);
3001 }
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003002
Daniel Sandler9f7936a2012-05-21 16:14:28 -04003003 // Need to show three lines?
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003004 if (mSubText != null) {
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01003005 contentView.setTextViewText(R.id.text, processLegacyText(mSubText));
Daniel Sandler9f7936a2012-05-21 16:14:28 -04003006 if (mContentText != null) {
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01003007 contentView.setTextViewText(R.id.text2, processLegacyText(mContentText));
Daniel Sandler9f7936a2012-05-21 16:14:28 -04003008 contentView.setViewVisibility(R.id.text2, View.VISIBLE);
3009 showLine2 = true;
Kenny Guy98193ea2014-07-24 19:54:37 +01003010 contentTextInLine2 = true;
Daniel Sandler9f7936a2012-05-21 16:14:28 -04003011 } else {
3012 contentView.setViewVisibility(R.id.text2, View.GONE);
3013 }
Jeff Sharkey1c400132011-08-05 14:50:13 -07003014 } else {
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003015 contentView.setViewVisibility(R.id.text2, View.GONE);
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02003016 if (hasProgress && (mProgressMax != 0 || mProgressIndeterminate)) {
Christoph Studeraca4b252014-09-09 15:58:41 +02003017 contentView.setViewVisibility(R.id.progress, View.VISIBLE);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003018 contentView.setProgressBar(
3019 R.id.progress, mProgressMax, mProgress, mProgressIndeterminate);
Jorim Jaggief72a192014-08-26 21:57:46 +02003020 contentView.setProgressBackgroundTintList(
Alan Viverette4a357cd2015-03-18 18:37:18 -07003021 R.id.progress, ColorStateList.valueOf(mContext.getColor(
Jorim Jaggief72a192014-08-26 21:57:46 +02003022 R.color.notification_progress_background_color)));
3023 if (mColor != COLOR_DEFAULT) {
3024 ColorStateList colorStateList = ColorStateList.valueOf(mColor);
3025 contentView.setProgressTintList(R.id.progress, colorStateList);
3026 contentView.setProgressIndeterminateTintList(R.id.progress, colorStateList);
3027 }
Daniel Sandler9f7936a2012-05-21 16:14:28 -04003028 showLine2 = true;
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003029 } else {
3030 contentView.setViewVisibility(R.id.progress, View.GONE);
3031 }
Jeff Sharkey1c400132011-08-05 14:50:13 -07003032 }
Daniel Sandler9f7936a2012-05-21 16:14:28 -04003033 if (showLine2) {
Jorim Jaggi445d3c02014-08-19 22:33:42 +02003034
3035 // need to shrink all the type to make sure everything fits
Jorim Jaggid05aa3e2014-08-28 17:52:27 +02003036 shrinkLine3Text(contentView);
Daniel Sandler9f7936a2012-05-21 16:14:28 -04003037 }
3038
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02003039 if (showsTimeOrChronometer()) {
Daniel Sandlera2985ed2012-04-03 16:42:00 -04003040 if (mUseChronometer) {
3041 contentView.setViewVisibility(R.id.chronometer, View.VISIBLE);
3042 contentView.setLong(R.id.chronometer, "setBase",
3043 mWhen + (SystemClock.elapsedRealtime() - System.currentTimeMillis()));
3044 contentView.setBoolean(R.id.chronometer, "setStarted", true);
3045 } else {
3046 contentView.setViewVisibility(R.id.time, View.VISIBLE);
3047 contentView.setLong(R.id.time, "setTime", mWhen);
3048 }
Joe Onorato561d3852010-11-20 18:09:34 -08003049 }
Daniel Sandler0c890492012-09-12 17:23:10 -07003050
Jorim Jaggi445d3c02014-08-19 22:33:42 +02003051 // Adjust padding depending on line count and font size.
3052 contentView.setViewPadding(R.id.line1, 0, calculateTopPadding(mContext,
Jorim Jaggid05aa3e2014-08-28 17:52:27 +02003053 mHasThreeLines, mContext.getResources().getConfiguration().fontScale),
Jorim Jaggi445d3c02014-08-19 22:33:42 +02003054 0, 0);
3055
Kenny Guy98193ea2014-07-24 19:54:37 +01003056 // We want to add badge to first line of text.
3057 boolean addedBadge = addProfileBadge(contentView,
3058 contentTextInLine2 ? R.id.profile_badge_line2 : R.id.profile_badge_line3);
3059 // If we added the badge to line 3 then we should show line 3.
3060 if (addedBadge && !contentTextInLine2) {
3061 showLine3 = true;
3062 }
3063
3064 // Note getStandardView may hide line 3 again.
Daniel Sandler9f7936a2012-05-21 16:14:28 -04003065 contentView.setViewVisibility(R.id.line3, showLine3 ? View.VISIBLE : View.GONE);
Daniel Sandler6387d2f2012-05-22 13:44:09 -04003066 contentView.setViewVisibility(R.id.overflow_divider, showLine3 ? View.VISIBLE : View.GONE);
Joe Onorato561d3852010-11-20 18:09:34 -08003067 return contentView;
3068 }
3069
Jorim Jaggi445d3c02014-08-19 22:33:42 +02003070 /**
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02003071 * @return true if the built notification will show the time or the chronometer; false
3072 * otherwise
3073 */
3074 private boolean showsTimeOrChronometer() {
3075 return mWhen != 0 && mShowWhen;
3076 }
3077
3078 /**
Jorim Jaggi445d3c02014-08-19 22:33:42 +02003079 * Logic to find out whether the notification is going to have three lines in the contracted
3080 * layout. This is used to adjust the top padding.
3081 *
3082 * @return true if the notification is going to have three lines; false if the notification
3083 * is going to have one or two lines
3084 */
3085 private boolean hasThreeLines() {
Jorim Jaggid05aa3e2014-08-28 17:52:27 +02003086 boolean contentTextInLine2 = mSubText != null && mContentText != null;
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02003087 boolean hasProgress = mStyle == null || mStyle.hasProgress();
Jorim Jaggid05aa3e2014-08-28 17:52:27 +02003088 // If we have content text in line 2, badge goes into line 2, or line 3 otherwise
3089 boolean badgeInLine3 = getProfileBadgeDrawable() != null && !contentTextInLine2;
3090 boolean hasLine3 = mContentText != null || mContentInfo != null || mNumber > 0
3091 || badgeInLine3;
Jorim Jaggi445d3c02014-08-19 22:33:42 +02003092 boolean hasLine2 = (mSubText != null && mContentText != null) ||
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02003093 (hasProgress && mSubText == null
3094 && (mProgressMax != 0 || mProgressIndeterminate));
Jorim Jaggi445d3c02014-08-19 22:33:42 +02003095 return hasLine2 && hasLine3;
3096 }
3097
3098 /**
3099 * @hide
3100 */
3101 public static int calculateTopPadding(Context ctx, boolean hasThreeLines,
3102 float fontScale) {
3103 int padding = ctx.getResources().getDimensionPixelSize(hasThreeLines
3104 ? R.dimen.notification_top_pad_narrow
3105 : R.dimen.notification_top_pad);
3106 int largePadding = ctx.getResources().getDimensionPixelSize(hasThreeLines
3107 ? R.dimen.notification_top_pad_large_text_narrow
3108 : R.dimen.notification_top_pad_large_text);
3109 float largeFactor = (MathUtils.constrain(fontScale, 1.0f, LARGE_TEXT_SCALE) - 1f)
3110 / (LARGE_TEXT_SCALE - 1f);
3111
3112 // Linearly interpolate the padding between large and normal with the font scale ranging
3113 // from 1f to LARGE_TEXT_SCALE
3114 return Math.round((1 - largeFactor) * padding + largeFactor * largePadding);
3115 }
3116
Christoph Studerfe718432014-09-01 18:21:18 +02003117 private void resetStandardTemplateWithActions(RemoteViews big) {
3118 big.setViewVisibility(R.id.actions, View.GONE);
3119 big.setViewVisibility(R.id.action_divider, View.GONE);
3120 big.removeAllViews(R.id.actions);
3121 }
3122
Daniel Sandler96fd7c12012-03-30 16:37:36 -04003123 private RemoteViews applyStandardTemplateWithActions(int layoutId) {
Jorim Jaggi445d3c02014-08-19 22:33:42 +02003124 RemoteViews big = applyStandardTemplate(layoutId);
Daniel Sandler96fd7c12012-03-30 16:37:36 -04003125
Christoph Studerfe718432014-09-01 18:21:18 +02003126 resetStandardTemplateWithActions(big);
3127
Daniel Sandler96fd7c12012-03-30 16:37:36 -04003128 int N = mActions.size();
3129 if (N > 0) {
Daniel Sandler96fd7c12012-03-30 16:37:36 -04003130 big.setViewVisibility(R.id.actions, View.VISIBLE);
Daniel Sandler6387d2f2012-05-22 13:44:09 -04003131 big.setViewVisibility(R.id.action_divider, View.VISIBLE);
Daniel Sandler8680bf82012-05-15 16:52:52 -04003132 if (N>MAX_ACTION_BUTTONS) N=MAX_ACTION_BUTTONS;
Daniel Sandler96fd7c12012-03-30 16:37:36 -04003133 for (int i=0; i<N; i++) {
3134 final RemoteViews button = generateActionButton(mActions.get(i));
Daniel Sandler96fd7c12012-03-30 16:37:36 -04003135 big.addView(R.id.actions, button);
3136 }
3137 }
3138 return big;
3139 }
3140
Joe Onorato46439ce2010-11-19 13:56:21 -08003141 private RemoteViews makeContentView() {
3142 if (mContentView != null) {
3143 return mContentView;
3144 } else {
Jorim Jaggi445d3c02014-08-19 22:33:42 +02003145 return applyStandardTemplate(getBaseLayoutResource());
Joe Onorato46439ce2010-11-19 13:56:21 -08003146 }
3147 }
3148
3149 private RemoteViews makeTickerView() {
3150 if (mTickerView != null) {
3151 return mTickerView;
Joe Onorato46439ce2010-11-19 13:56:21 -08003152 }
Dan Sandler5fcdf6e2014-07-18 11:31:15 -04003153 return null; // tickers are not created by default anymore
Joe Onorato46439ce2010-11-19 13:56:21 -08003154 }
3155
Daniel Sandler96fd7c12012-03-30 16:37:36 -04003156 private RemoteViews makeBigContentView() {
3157 if (mActions.size() == 0) return null;
3158
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01003159 return applyStandardTemplateWithActions(getBigBaseLayoutResource());
Daniel Sandler96fd7c12012-03-30 16:37:36 -04003160 }
3161
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01003162 private RemoteViews makeHeadsUpContentView() {
Chris Wren8fd39ec2014-02-27 17:43:26 -05003163 if (mActions.size() == 0) return null;
3164
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01003165 return applyStandardTemplateWithActions(getBigBaseLayoutResource());
Chris Wren8fd39ec2014-02-27 17:43:26 -05003166 }
3167
3168
Daniel Sandler96fd7c12012-03-30 16:37:36 -04003169 private RemoteViews generateActionButton(Action action) {
Daniel Sandler8680bf82012-05-15 16:52:52 -04003170 final boolean tombstone = (action.actionIntent == null);
Joe Malin8d40d042012-11-05 11:36:40 -08003171 RemoteViews button = new RemoteViews(mContext.getPackageName(),
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01003172 tombstone ? getActionTombstoneLayoutResource()
3173 : getActionLayoutResource());
Chris Wren8749ac8a2013-12-03 14:31:01 -05003174 button.setTextViewCompoundDrawablesRelative(R.id.action0, action.icon, 0, 0, 0);
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01003175 button.setTextViewText(R.id.action0, processLegacyText(action.title));
Daniel Sandler8680bf82012-05-15 16:52:52 -04003176 if (!tombstone) {
Daniel Sandlere5518842012-05-10 16:20:40 -04003177 button.setOnClickPendingIntent(R.id.action0, action.actionIntent);
Daniel Sandlere5518842012-05-10 16:20:40 -04003178 }
Daniel Sandler96fd7c12012-03-30 16:37:36 -04003179 button.setContentDescription(R.id.action0, action.title);
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01003180 processLegacyAction(action, button);
Daniel Sandler96fd7c12012-03-30 16:37:36 -04003181 return button;
3182 }
3183
Joe Onoratocb109a02011-01-18 17:57:41 -08003184 /**
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01003185 * @return Whether we are currently building a notification from a legacy (an app that
Alan Viverette3cb07a462014-06-06 14:19:53 -07003186 * doesn't create material notifications by itself) app.
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01003187 */
3188 private boolean isLegacy() {
Dan Sandler26e81cf2014-05-06 10:01:27 -04003189 return mColorUtil != null;
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01003190 }
3191
3192 private void processLegacyAction(Action action, RemoteViews button) {
Dan Sandler05c362d2014-09-03 00:16:27 +02003193 if (!isLegacy() || mColorUtil.isGrayscaleIcon(mContext, action.icon)) {
Christoph Studer239f8352014-08-25 15:13:18 +02003194 button.setTextViewCompoundDrawablesRelativeColorFilter(R.id.action0, 0,
Alan Viverette4a357cd2015-03-18 18:37:18 -07003195 mContext.getColor(R.color.notification_action_color_filter),
Christoph Studer239f8352014-08-25 15:13:18 +02003196 PorterDuff.Mode.MULTIPLY);
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01003197 }
3198 }
3199
3200 private CharSequence processLegacyText(CharSequence charSequence) {
3201 if (isLegacy()) {
Dan Sandler26e81cf2014-05-06 10:01:27 -04003202 return mColorUtil.invertCharSequenceColors(charSequence);
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01003203 } else {
3204 return charSequence;
3205 }
3206 }
3207
Dan Sandler26e81cf2014-05-06 10:01:27 -04003208 /**
3209 * Apply any necessary background to smallIcons being used in the largeIcon spot.
3210 */
Dan Sandlerd63f9322015-05-06 15:18:49 -04003211 private void processSmallIconAsLarge(Icon largeIcon, RemoteViews contentView) {
Jorim Jaggi92df1f22014-12-16 19:44:41 +01003212 if (!isLegacy()) {
3213 contentView.setDrawableParameters(R.id.icon, false, -1,
3214 0xFFFFFFFF,
3215 PorterDuff.Mode.SRC_ATOP, -1);
Dan Sandler26e81cf2014-05-06 10:01:27 -04003216 applyLargeIconBackground(contentView);
Dan Sandlerd63f9322015-05-06 15:18:49 -04003217 } else {
3218 if (mColorUtil.isGrayscaleIcon(mContext, largeIcon)) {
3219 applyLargeIconBackground(contentView);
3220 }
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01003221 }
3222 }
3223
Dan Sandler26e81cf2014-05-06 10:01:27 -04003224 /**
3225 * Apply any necessary background to a largeIcon if it's a fake smallIcon (that is,
3226 * if it's grayscale).
3227 */
3228 // TODO: also check bounds, transparency, that sort of thing.
Dan Sandlerd63f9322015-05-06 15:18:49 -04003229 private void processLargeLegacyIcon(Icon largeIcon, RemoteViews contentView) {
3230 if (largeIcon != null && isLegacy()
3231 && mColorUtil.isGrayscaleIcon(mContext, largeIcon)) {
Dan Sandler26e81cf2014-05-06 10:01:27 -04003232 applyLargeIconBackground(contentView);
Dan Sandler190d58d2014-05-15 09:33:39 -04003233 } else {
3234 removeLargeIconBackground(contentView);
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01003235 }
3236 }
3237
Dan Sandler26e81cf2014-05-06 10:01:27 -04003238 /**
3239 * Add a colored circle behind the largeIcon slot.
3240 */
3241 private void applyLargeIconBackground(RemoteViews contentView) {
3242 contentView.setInt(R.id.icon, "setBackgroundResource",
Jorim Jaggi445d3c02014-08-19 22:33:42 +02003243 R.drawable.notification_icon_legacy_bg);
Dan Sandler26e81cf2014-05-06 10:01:27 -04003244
3245 contentView.setDrawableParameters(
3246 R.id.icon,
3247 true,
3248 -1,
Jorim Jaggi74419312014-06-10 20:57:21 +02003249 resolveColor(),
Dan Sandler26e81cf2014-05-06 10:01:27 -04003250 PorterDuff.Mode.SRC_ATOP,
3251 -1);
Jorim Jaggi445d3c02014-08-19 22:33:42 +02003252
3253 int padding = mContext.getResources().getDimensionPixelSize(
3254 R.dimen.notification_large_icon_circle_padding);
3255 contentView.setViewPadding(R.id.icon, padding, padding, padding, padding);
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01003256 }
3257
Dan Sandler190d58d2014-05-15 09:33:39 -04003258 private void removeLargeIconBackground(RemoteViews contentView) {
3259 contentView.setInt(R.id.icon, "setBackgroundResource", 0);
3260 }
3261
Dan Sandler26e81cf2014-05-06 10:01:27 -04003262 /**
3263 * Recolor small icons when used in the R.id.right_icon slot.
3264 */
Dan Sandlerd63f9322015-05-06 15:18:49 -04003265 private void processSmallRightIcon(Icon smallIcon, RemoteViews contentView) {
Jorim Jaggi92df1f22014-12-16 19:44:41 +01003266 if (!isLegacy()) {
Dan Sandler190d58d2014-05-15 09:33:39 -04003267 contentView.setDrawableParameters(R.id.right_icon, false, -1,
3268 0xFFFFFFFF,
3269 PorterDuff.Mode.SRC_ATOP, -1);
Jorim Jaggi92df1f22014-12-16 19:44:41 +01003270 }
Dan Sandlerd63f9322015-05-06 15:18:49 -04003271 final boolean gray = isLegacy()
3272 && smallIcon.getType() == Icon.TYPE_RESOURCE
3273 && mColorUtil.isGrayscaleIcon(mContext, smallIcon.getResId());
3274 if (!isLegacy() || gray) {
Dan Sandler190d58d2014-05-15 09:33:39 -04003275 contentView.setInt(R.id.right_icon,
3276 "setBackgroundResource",
3277 R.drawable.notification_icon_legacy_bg);
3278
3279 contentView.setDrawableParameters(
3280 R.id.right_icon,
3281 true,
3282 -1,
Jorim Jaggi74419312014-06-10 20:57:21 +02003283 resolveColor(),
Dan Sandler190d58d2014-05-15 09:33:39 -04003284 PorterDuff.Mode.SRC_ATOP,
3285 -1);
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01003286 }
3287 }
3288
Jorim Jaggi74419312014-06-10 20:57:21 +02003289 private int sanitizeColor() {
3290 if (mColor != COLOR_DEFAULT) {
3291 mColor |= 0xFF000000; // no alpha for custom colors
3292 }
3293 return mColor;
3294 }
3295
Dan Sandler26e81cf2014-05-06 10:01:27 -04003296 private int resolveColor() {
3297 if (mColor == COLOR_DEFAULT) {
Alan Viverette4a357cd2015-03-18 18:37:18 -07003298 return mContext.getColor(R.color.notification_icon_bg_color);
Dan Sandler26e81cf2014-05-06 10:01:27 -04003299 }
3300 return mColor;
3301 }
3302
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01003303 /**
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003304 * Apply the unstyled operations and return a new {@link Notification} object.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04003305 * @hide
Joe Onoratocb109a02011-01-18 17:57:41 -08003306 */
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04003307 public Notification buildUnstyled() {
Joe Onorato46439ce2010-11-19 13:56:21 -08003308 Notification n = new Notification();
3309 n.when = mWhen;
Dan Sandlerd63f9322015-05-06 15:18:49 -04003310 n.mSmallIcon = mSmallIcon;
Dan Sandler3936e7a2015-05-19 20:59:12 -04003311 if (mSmallIcon != null && mSmallIcon.getType() == Icon.TYPE_RESOURCE) {
Dan Sandlerd63f9322015-05-06 15:18:49 -04003312 n.icon = mSmallIcon.getResId();
3313 }
Joe Onorato46439ce2010-11-19 13:56:21 -08003314 n.iconLevel = mSmallIconLevel;
Joe Onorato8595a3d2010-11-19 18:12:07 -08003315 n.number = mNumber;
Dan Sandler26e81cf2014-05-06 10:01:27 -04003316
Jorim Jaggi74419312014-06-10 20:57:21 +02003317 n.color = sanitizeColor();
Dan Sandler26e81cf2014-05-06 10:01:27 -04003318
Christoph Studer4600f9b2014-07-22 22:44:43 +02003319 setBuilderContentView(n, makeContentView());
Joe Onorato46439ce2010-11-19 13:56:21 -08003320 n.contentIntent = mContentIntent;
3321 n.deleteIntent = mDeleteIntent;
3322 n.fullScreenIntent = mFullScreenIntent;
3323 n.tickerText = mTickerText;
3324 n.tickerView = makeTickerView();
Dan Sandlerd63f9322015-05-06 15:18:49 -04003325 n.mLargeIcon = mLargeIcon;
3326 if (mLargeIcon != null && mLargeIcon.getType() == Icon.TYPE_BITMAP) {
3327 n.largeIcon = mLargeIcon.getBitmap();
3328 }
Joe Onorato46439ce2010-11-19 13:56:21 -08003329 n.sound = mSound;
3330 n.audioStreamType = mAudioStreamType;
John Spurlockc0650f022014-07-19 13:22:39 -04003331 n.audioAttributes = mAudioAttributes;
Joe Onorato46439ce2010-11-19 13:56:21 -08003332 n.vibrate = mVibrate;
3333 n.ledARGB = mLedArgb;
3334 n.ledOnMS = mLedOnMs;
3335 n.ledOffMS = mLedOffMs;
3336 n.defaults = mDefaults;
3337 n.flags = mFlags;
Christoph Studer4600f9b2014-07-22 22:44:43 +02003338 setBuilderBigContentView(n, makeBigContentView());
3339 setBuilderHeadsUpContentView(n, makeHeadsUpContentView());
Daniel Sandler26c13432013-04-04 11:01:04 -04003340 if (mLedOnMs != 0 || mLedOffMs != 0) {
Joe Onorato8d0b6552010-11-22 16:09:29 -08003341 n.flags |= FLAG_SHOW_LIGHTS;
3342 }
3343 if ((mDefaults & DEFAULT_LIGHTS) != 0) {
3344 n.flags |= FLAG_SHOW_LIGHTS;
3345 }
John Spurlockfd7f1e02014-03-18 16:41:57 -04003346 n.category = mCategory;
Griff Hazen5cadc3b2014-05-20 09:55:39 -07003347 n.mGroupKey = mGroupKey;
3348 n.mSortKey = mSortKey;
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003349 n.priority = mPriority;
Daniel Sandlera0a938c2012-03-15 08:42:37 -04003350 if (mActions.size() > 0) {
3351 n.actions = new Action[mActions.size()];
3352 mActions.toArray(n.actions);
3353 }
Dan Sandler0bf2ed82013-12-21 23:33:41 -06003354 n.visibility = mVisibility;
3355
3356 if (mPublicVersion != null) {
3357 n.publicVersion = new Notification();
3358 mPublicVersion.cloneInto(n.publicVersion, true);
3359 }
Christoph Studer4600f9b2014-07-22 22:44:43 +02003360 // Note: If you're adding new fields, also update restoreFromNotitification().
Joe Onorato46439ce2010-11-19 13:56:21 -08003361 return n;
3362 }
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003363
3364 /**
Daniel Sandlerf45564e2013-04-15 15:05:08 -04003365 * Capture, in the provided bundle, semantic information used in the construction of
3366 * this Notification object.
3367 * @hide
3368 */
Griff Hazen720042b2014-02-24 15:46:56 -08003369 public void populateExtras(Bundle extras) {
Daniel Sandlerf45564e2013-04-15 15:05:08 -04003370 // Store original information used in the construction of this object
Kenny Guy8942bcd2014-09-08 21:09:47 +01003371 extras.putInt(EXTRA_ORIGINATING_USERID, mOriginatingUserId);
Kenny Guy77320062014-08-27 21:37:15 +01003372 extras.putParcelable(EXTRA_REBUILD_CONTEXT_APPLICATION_INFO,
3373 mContext.getApplicationInfo());
Daniel Sandlerf45564e2013-04-15 15:05:08 -04003374 extras.putCharSequence(EXTRA_TITLE, mContentTitle);
3375 extras.putCharSequence(EXTRA_TEXT, mContentText);
3376 extras.putCharSequence(EXTRA_SUB_TEXT, mSubText);
3377 extras.putCharSequence(EXTRA_INFO_TEXT, mContentInfo);
Dan Sandlerd63f9322015-05-06 15:18:49 -04003378 extras.putParcelable(EXTRA_SMALL_ICON, mSmallIcon);
Daniel Sandlerf45564e2013-04-15 15:05:08 -04003379 extras.putInt(EXTRA_PROGRESS, mProgress);
3380 extras.putInt(EXTRA_PROGRESS_MAX, mProgressMax);
3381 extras.putBoolean(EXTRA_PROGRESS_INDETERMINATE, mProgressIndeterminate);
3382 extras.putBoolean(EXTRA_SHOW_CHRONOMETER, mUseChronometer);
3383 extras.putBoolean(EXTRA_SHOW_WHEN, mShowWhen);
John Spurlockac08a472013-06-10 11:37:08 -04003384 if (mLargeIcon != null) {
3385 extras.putParcelable(EXTRA_LARGE_ICON, mLargeIcon);
3386 }
Chris Wrendde75302014-03-26 17:24:15 -04003387 if (!mPeople.isEmpty()) {
3388 extras.putStringArray(EXTRA_PEOPLE, mPeople.toArray(new String[mPeople.size()]));
3389 }
Christoph Studer4600f9b2014-07-22 22:44:43 +02003390 // NOTE: If you're adding new extras also update restoreFromNotification().
3391 }
3392
3393
3394 /**
3395 * @hide
3396 */
3397 public static void stripForDelivery(Notification n) {
3398 if (!STRIP_AND_REBUILD) {
3399 return;
3400 }
3401
3402 String templateClass = n.extras.getString(EXTRA_TEMPLATE);
3403 // Only strip views for known Styles because we won't know how to
3404 // re-create them otherwise.
3405 boolean stripViews = TextUtils.isEmpty(templateClass) ||
3406 getNotificationStyleClass(templateClass) != null;
3407
3408 boolean isStripped = false;
3409
3410 if (n.largeIcon != null && n.extras.containsKey(EXTRA_LARGE_ICON)) {
3411 // TODO: Would like to check for equality here, but if the notification
3412 // has been cloned, we can't.
3413 n.largeIcon = null;
3414 n.extras.putBoolean(EXTRA_REBUILD_LARGE_ICON, true);
3415 isStripped = true;
3416 }
3417 // Get rid of unmodified BuilderRemoteViews.
3418
3419 if (stripViews &&
3420 n.contentView instanceof BuilderRemoteViews &&
3421 n.extras.getInt(EXTRA_REBUILD_CONTENT_VIEW_ACTION_COUNT, -1) ==
3422 n.contentView.getSequenceNumber()) {
3423 n.contentView = null;
3424 n.extras.putBoolean(EXTRA_REBUILD_CONTENT_VIEW, true);
3425 n.extras.remove(EXTRA_REBUILD_CONTENT_VIEW_ACTION_COUNT);
3426 isStripped = true;
3427 }
3428 if (stripViews &&
3429 n.bigContentView instanceof BuilderRemoteViews &&
3430 n.extras.getInt(EXTRA_REBUILD_BIG_CONTENT_VIEW_ACTION_COUNT, -1) ==
3431 n.bigContentView.getSequenceNumber()) {
3432 n.bigContentView = null;
3433 n.extras.putBoolean(EXTRA_REBUILD_BIG_CONTENT_VIEW, true);
3434 n.extras.remove(EXTRA_REBUILD_BIG_CONTENT_VIEW_ACTION_COUNT);
3435 isStripped = true;
3436 }
3437 if (stripViews &&
3438 n.headsUpContentView instanceof BuilderRemoteViews &&
3439 n.extras.getInt(EXTRA_REBUILD_HEADS_UP_CONTENT_VIEW_ACTION_COUNT, -1) ==
3440 n.headsUpContentView.getSequenceNumber()) {
3441 n.headsUpContentView = null;
3442 n.extras.putBoolean(EXTRA_REBUILD_HEADS_UP_CONTENT_VIEW, true);
3443 n.extras.remove(EXTRA_REBUILD_HEADS_UP_CONTENT_VIEW_ACTION_COUNT);
3444 isStripped = true;
3445 }
3446
3447 if (isStripped) {
3448 n.extras.putBoolean(EXTRA_NEEDS_REBUILD, true);
3449 }
3450 }
3451
3452 /**
3453 * @hide
3454 */
3455 public static Notification rebuild(Context context, Notification n) {
3456 Bundle extras = n.extras;
3457 if (!extras.getBoolean(EXTRA_NEEDS_REBUILD)) return n;
3458 extras.remove(EXTRA_NEEDS_REBUILD);
3459
3460 // Re-create notification context so we can access app resources.
Kenny Guy77320062014-08-27 21:37:15 +01003461 ApplicationInfo applicationInfo = extras.getParcelable(
3462 EXTRA_REBUILD_CONTEXT_APPLICATION_INFO);
Christoph Studer4600f9b2014-07-22 22:44:43 +02003463 Context builderContext;
3464 try {
Kenny Guy77320062014-08-27 21:37:15 +01003465 builderContext = context.createApplicationContext(applicationInfo,
Christoph Studer4600f9b2014-07-22 22:44:43 +02003466 Context.CONTEXT_RESTRICTED);
3467 } catch (NameNotFoundException e) {
Kenny Guy77320062014-08-27 21:37:15 +01003468 Log.e(TAG, "ApplicationInfo " + applicationInfo + " not found");
Christoph Studer4600f9b2014-07-22 22:44:43 +02003469 builderContext = context; // try with our context
3470 }
3471
3472 Builder b = new Builder(builderContext, n);
3473 return b.rebuild();
3474 }
3475
3476 /**
3477 * Rebuilds the notification passed in to the rebuild-constructor
3478 * {@link #Builder(Context, Notification)}.
3479 *
3480 * <p>
3481 * Throws IllegalStateException when invoked on a Builder that isn't in rebuild mode.
3482 *
3483 * @hide
3484 */
3485 private Notification rebuild() {
3486 if (mRebuildNotification == null) {
3487 throw new IllegalStateException("rebuild() only valid when in 'rebuild' mode.");
3488 }
Jorim Jaggid05aa3e2014-08-28 17:52:27 +02003489 mHasThreeLines = hasThreeLines();
Christoph Studer4600f9b2014-07-22 22:44:43 +02003490
3491 Bundle extras = mRebuildNotification.extras;
3492
3493 if (extras.getBoolean(EXTRA_REBUILD_LARGE_ICON)) {
3494 mRebuildNotification.largeIcon = extras.getParcelable(EXTRA_LARGE_ICON);
3495 }
3496 extras.remove(EXTRA_REBUILD_LARGE_ICON);
3497
3498 if (extras.getBoolean(EXTRA_REBUILD_CONTENT_VIEW)) {
3499 setBuilderContentView(mRebuildNotification, makeContentView());
3500 if (mStyle != null) {
3501 mStyle.populateContentView(mRebuildNotification);
3502 }
3503 }
3504 extras.remove(EXTRA_REBUILD_CONTENT_VIEW);
3505
3506 if (extras.getBoolean(EXTRA_REBUILD_BIG_CONTENT_VIEW)) {
3507 setBuilderBigContentView(mRebuildNotification, makeBigContentView());
3508 if (mStyle != null) {
3509 mStyle.populateBigContentView(mRebuildNotification);
3510 }
3511 }
3512 extras.remove(EXTRA_REBUILD_BIG_CONTENT_VIEW);
3513
3514 if (extras.getBoolean(EXTRA_REBUILD_HEADS_UP_CONTENT_VIEW)) {
3515 setBuilderHeadsUpContentView(mRebuildNotification, makeHeadsUpContentView());
3516 if (mStyle != null) {
3517 mStyle.populateHeadsUpContentView(mRebuildNotification);
3518 }
3519 }
3520 extras.remove(EXTRA_REBUILD_HEADS_UP_CONTENT_VIEW);
3521
Jorim Jaggid05aa3e2014-08-28 17:52:27 +02003522 mHasThreeLines = false;
Christoph Studer4600f9b2014-07-22 22:44:43 +02003523 return mRebuildNotification;
3524 }
3525
3526 private static Class<? extends Style> getNotificationStyleClass(String templateClass) {
3527 Class<? extends Style>[] classes = new Class[]{
3528 BigTextStyle.class, BigPictureStyle.class, InboxStyle.class, MediaStyle.class};
3529 for (Class<? extends Style> innerClass : classes) {
3530 if (templateClass.equals(innerClass.getName())) {
3531 return innerClass;
3532 }
3533 }
3534 return null;
3535 }
3536
3537 private void setBuilderContentView(Notification n, RemoteViews contentView) {
3538 n.contentView = contentView;
3539 if (contentView instanceof BuilderRemoteViews) {
3540 mRebuildBundle.putInt(Builder.EXTRA_REBUILD_CONTENT_VIEW_ACTION_COUNT,
3541 contentView.getSequenceNumber());
3542 }
3543 }
3544
3545 private void setBuilderBigContentView(Notification n, RemoteViews bigContentView) {
3546 n.bigContentView = bigContentView;
3547 if (bigContentView instanceof BuilderRemoteViews) {
3548 mRebuildBundle.putInt(Builder.EXTRA_REBUILD_BIG_CONTENT_VIEW_ACTION_COUNT,
3549 bigContentView.getSequenceNumber());
3550 }
3551 }
3552
3553 private void setBuilderHeadsUpContentView(Notification n,
3554 RemoteViews headsUpContentView) {
3555 n.headsUpContentView = headsUpContentView;
3556 if (headsUpContentView instanceof BuilderRemoteViews) {
3557 mRebuildBundle.putInt(Builder.EXTRA_REBUILD_HEADS_UP_CONTENT_VIEW_ACTION_COUNT,
3558 headsUpContentView.getSequenceNumber());
3559 }
3560 }
3561
3562 private void restoreFromNotification(Notification n) {
3563
3564 // Notification fields.
3565 mWhen = n.when;
Dan Sandlerd63f9322015-05-06 15:18:49 -04003566 mSmallIcon = n.mSmallIcon;
Christoph Studer4600f9b2014-07-22 22:44:43 +02003567 mSmallIconLevel = n.iconLevel;
3568 mNumber = n.number;
3569
3570 mColor = n.color;
3571
3572 mContentView = n.contentView;
3573 mDeleteIntent = n.deleteIntent;
3574 mFullScreenIntent = n.fullScreenIntent;
3575 mTickerText = n.tickerText;
3576 mTickerView = n.tickerView;
Dan Sandlerd63f9322015-05-06 15:18:49 -04003577 mLargeIcon = n.mLargeIcon;
Christoph Studer4600f9b2014-07-22 22:44:43 +02003578 mSound = n.sound;
3579 mAudioStreamType = n.audioStreamType;
3580 mAudioAttributes = n.audioAttributes;
3581
3582 mVibrate = n.vibrate;
3583 mLedArgb = n.ledARGB;
3584 mLedOnMs = n.ledOnMS;
3585 mLedOffMs = n.ledOffMS;
3586 mDefaults = n.defaults;
3587 mFlags = n.flags;
3588
3589 mCategory = n.category;
3590 mGroupKey = n.mGroupKey;
3591 mSortKey = n.mSortKey;
3592 mPriority = n.priority;
3593 mActions.clear();
3594 if (n.actions != null) {
3595 Collections.addAll(mActions, n.actions);
3596 }
3597 mVisibility = n.visibility;
3598
3599 mPublicVersion = n.publicVersion;
3600
3601 // Extras.
3602 Bundle extras = n.extras;
Kenny Guy8942bcd2014-09-08 21:09:47 +01003603 mOriginatingUserId = extras.getInt(EXTRA_ORIGINATING_USERID);
Christoph Studer4600f9b2014-07-22 22:44:43 +02003604 mContentTitle = extras.getCharSequence(EXTRA_TITLE);
3605 mContentText = extras.getCharSequence(EXTRA_TEXT);
3606 mSubText = extras.getCharSequence(EXTRA_SUB_TEXT);
3607 mContentInfo = extras.getCharSequence(EXTRA_INFO_TEXT);
Dan Sandlerd63f9322015-05-06 15:18:49 -04003608 mSmallIcon = extras.getParcelable(EXTRA_SMALL_ICON);
Christoph Studer4600f9b2014-07-22 22:44:43 +02003609 mProgress = extras.getInt(EXTRA_PROGRESS);
3610 mProgressMax = extras.getInt(EXTRA_PROGRESS_MAX);
3611 mProgressIndeterminate = extras.getBoolean(EXTRA_PROGRESS_INDETERMINATE);
3612 mUseChronometer = extras.getBoolean(EXTRA_SHOW_CHRONOMETER);
3613 mShowWhen = extras.getBoolean(EXTRA_SHOW_WHEN);
3614 if (extras.containsKey(EXTRA_LARGE_ICON)) {
3615 mLargeIcon = extras.getParcelable(EXTRA_LARGE_ICON);
3616 }
3617 if (extras.containsKey(EXTRA_PEOPLE)) {
3618 mPeople.clear();
3619 Collections.addAll(mPeople, extras.getStringArray(EXTRA_PEOPLE));
3620 }
Daniel Sandlerf45564e2013-04-15 15:05:08 -04003621 }
3622
3623 /**
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003624 * @deprecated Use {@link #build()} instead.
3625 */
3626 @Deprecated
3627 public Notification getNotification() {
3628 return build();
3629 }
3630
3631 /**
3632 * Combine all of the options that have been set and return a new {@link Notification}
3633 * object.
3634 */
3635 public Notification build() {
Jorim Jaggia0d58ae2015-06-03 11:48:13 -07003636 if (mSmallIcon != null) {
3637 mSmallIcon.convertToAshmem();
3638 }
3639 if (mLargeIcon != null) {
3640 mLargeIcon.convertToAshmem();
3641 }
Kenny Guy8942bcd2014-09-08 21:09:47 +01003642 mOriginatingUserId = mContext.getUserId();
Jorim Jaggid05aa3e2014-08-28 17:52:27 +02003643 mHasThreeLines = hasThreeLines();
Christoph Studer943aa672014-08-03 20:31:16 +02003644
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04003645 Notification n = buildUnstyled();
Daniel Sandlerf45564e2013-04-15 15:05:08 -04003646
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003647 if (mStyle != null) {
Jorim Jaggia0d58ae2015-06-03 11:48:13 -07003648 mStyle.purgeResources();
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04003649 n = mStyle.buildStyled(n);
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003650 }
Daniel Sandlerf45564e2013-04-15 15:05:08 -04003651
Christoph Studer4600f9b2014-07-22 22:44:43 +02003652 if (mExtras != null) {
3653 n.extras.putAll(mExtras);
3654 }
3655
3656 if (mRebuildBundle.size() > 0) {
3657 n.extras.putAll(mRebuildBundle);
3658 mRebuildBundle.clear();
3659 }
Daniel Sandlerf45564e2013-04-15 15:05:08 -04003660
Griff Hazen720042b2014-02-24 15:46:56 -08003661 populateExtras(n.extras);
Daniel Sandlerf45564e2013-04-15 15:05:08 -04003662 if (mStyle != null) {
3663 mStyle.addExtras(n.extras);
3664 }
3665
Jorim Jaggid05aa3e2014-08-28 17:52:27 +02003666 mHasThreeLines = false;
Daniel Sandlerf45564e2013-04-15 15:05:08 -04003667 return n;
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003668 }
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05003669
3670 /**
3671 * Apply this Builder to an existing {@link Notification} object.
3672 *
3673 * @hide
3674 */
3675 public Notification buildInto(Notification n) {
Daniel Sandler1a497d32013-04-18 14:52:45 -04003676 build().cloneInto(n, true);
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05003677 return n;
3678 }
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01003679
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01003680 private int getBaseLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07003681 return R.layout.notification_template_material_base;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01003682 }
3683
3684 private int getBigBaseLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07003685 return R.layout.notification_template_material_big_base;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01003686 }
3687
3688 private int getBigPictureLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07003689 return R.layout.notification_template_material_big_picture;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01003690 }
3691
3692 private int getBigTextLayoutResource() {
Jorim Jaggi445d3c02014-08-19 22:33:42 +02003693 return R.layout.notification_template_material_big_text;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01003694 }
3695
3696 private int getInboxLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07003697 return R.layout.notification_template_material_inbox;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01003698 }
3699
3700 private int getActionLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07003701 return R.layout.notification_material_action;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01003702 }
3703
3704 private int getActionTombstoneLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07003705 return R.layout.notification_material_action_tombstone;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01003706 }
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003707 }
3708
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003709 /**
3710 * An object that can apply a rich notification style to a {@link Notification.Builder}
3711 * object.
3712 */
Griff Hazendfcb0802014-02-11 12:00:00 -08003713 public static abstract class Style {
Chris Wrend6297db2012-05-03 16:20:13 -04003714 private CharSequence mBigContentTitle;
Jorim Jaggi457a10d2014-09-08 16:18:23 +02003715
3716 /**
3717 * @hide
3718 */
3719 protected CharSequence mSummaryText = null;
3720
3721 /**
3722 * @hide
3723 */
3724 protected boolean mSummaryTextSet = false;
Chris Wrend6297db2012-05-03 16:20:13 -04003725
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003726 protected Builder mBuilder;
3727
Chris Wrend6297db2012-05-03 16:20:13 -04003728 /**
3729 * Overrides ContentTitle in the big form of the template.
3730 * This defaults to the value passed to setContentTitle().
3731 */
3732 protected void internalSetBigContentTitle(CharSequence title) {
3733 mBigContentTitle = title;
3734 }
3735
3736 /**
3737 * Set the first line of text after the detail section in the big form of the template.
3738 */
3739 protected void internalSetSummaryText(CharSequence cs) {
3740 mSummaryText = cs;
Daniel Sandler619738c2012-06-07 16:33:08 -04003741 mSummaryTextSet = true;
Chris Wrend6297db2012-05-03 16:20:13 -04003742 }
3743
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003744 public void setBuilder(Builder builder) {
3745 if (mBuilder != builder) {
3746 mBuilder = builder;
Daniel Sandlerc08dea22012-06-28 08:35:24 -07003747 if (mBuilder != null) {
3748 mBuilder.setStyle(this);
3749 }
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003750 }
3751 }
3752
Chris Wrend6297db2012-05-03 16:20:13 -04003753 protected void checkBuilder() {
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003754 if (mBuilder == null) {
3755 throw new IllegalArgumentException("Style requires a valid Builder object");
3756 }
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003757 }
Chris Wrend6297db2012-05-03 16:20:13 -04003758
3759 protected RemoteViews getStandardView(int layoutId) {
3760 checkBuilder();
3761
Christoph Studer4600f9b2014-07-22 22:44:43 +02003762 // Nasty.
3763 CharSequence oldBuilderContentTitle = mBuilder.mContentTitle;
Chris Wrend6297db2012-05-03 16:20:13 -04003764 if (mBigContentTitle != null) {
3765 mBuilder.setContentTitle(mBigContentTitle);
3766 }
3767
Chris Wrend6297db2012-05-03 16:20:13 -04003768 RemoteViews contentView = mBuilder.applyStandardTemplateWithActions(layoutId);
3769
Christoph Studer4600f9b2014-07-22 22:44:43 +02003770 mBuilder.mContentTitle = oldBuilderContentTitle;
3771
Chris Wrend6297db2012-05-03 16:20:13 -04003772 if (mBigContentTitle != null && mBigContentTitle.equals("")) {
3773 contentView.setViewVisibility(R.id.line1, View.GONE);
Chris Wren67dc9a02012-05-16 01:03:20 -04003774 } else {
3775 contentView.setViewVisibility(R.id.line1, View.VISIBLE);
Chris Wrend6297db2012-05-03 16:20:13 -04003776 }
3777
Daniel Sandler619738c2012-06-07 16:33:08 -04003778 // The last line defaults to the subtext, but can be replaced by mSummaryText
3779 final CharSequence overflowText =
3780 mSummaryTextSet ? mSummaryText
3781 : mBuilder.mSubText;
3782 if (overflowText != null) {
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01003783 contentView.setTextViewText(R.id.text, mBuilder.processLegacyText(overflowText));
Daniel Sandler619738c2012-06-07 16:33:08 -04003784 contentView.setViewVisibility(R.id.overflow_divider, View.VISIBLE);
Daniel Sandler9f7936a2012-05-21 16:14:28 -04003785 contentView.setViewVisibility(R.id.line3, View.VISIBLE);
Daniel Sandler916ad912012-06-13 12:17:07 -04003786 } else {
Kenny Guy98193ea2014-07-24 19:54:37 +01003787 // Clear text in case we use the line to show the profile badge.
3788 contentView.setTextViewText(R.id.text, "");
Daniel Sandler916ad912012-06-13 12:17:07 -04003789 contentView.setViewVisibility(R.id.overflow_divider, View.GONE);
3790 contentView.setViewVisibility(R.id.line3, View.GONE);
Chris Wrend6297db2012-05-03 16:20:13 -04003791 }
3792
3793 return contentView;
3794 }
3795
Daniel Sandlerf45564e2013-04-15 15:05:08 -04003796 /**
Jorim Jaggi445d3c02014-08-19 22:33:42 +02003797 * Changes the padding of the first line such that the big and small content view have the
3798 * same top padding.
3799 *
3800 * @hide
3801 */
3802 protected void applyTopPadding(RemoteViews contentView) {
3803 int topPadding = Builder.calculateTopPadding(mBuilder.mContext,
Jorim Jaggid05aa3e2014-08-28 17:52:27 +02003804 mBuilder.mHasThreeLines,
Jorim Jaggi445d3c02014-08-19 22:33:42 +02003805 mBuilder.mContext.getResources().getConfiguration().fontScale);
3806 contentView.setViewPadding(R.id.line1, 0, topPadding, 0, 0);
3807 }
3808
3809 /**
Daniel Sandlerf45564e2013-04-15 15:05:08 -04003810 * @hide
3811 */
3812 public void addExtras(Bundle extras) {
3813 if (mSummaryTextSet) {
3814 extras.putCharSequence(EXTRA_SUMMARY_TEXT, mSummaryText);
3815 }
3816 if (mBigContentTitle != null) {
3817 extras.putCharSequence(EXTRA_TITLE_BIG, mBigContentTitle);
3818 }
Chris Wren91ad5632013-06-05 15:05:57 -04003819 extras.putString(EXTRA_TEMPLATE, this.getClass().getName());
Daniel Sandlerf45564e2013-04-15 15:05:08 -04003820 }
3821
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04003822 /**
3823 * @hide
3824 */
Christoph Studer4600f9b2014-07-22 22:44:43 +02003825 protected void restoreFromExtras(Bundle extras) {
3826 if (extras.containsKey(EXTRA_SUMMARY_TEXT)) {
3827 mSummaryText = extras.getCharSequence(EXTRA_SUMMARY_TEXT);
3828 mSummaryTextSet = true;
3829 }
3830 if (extras.containsKey(EXTRA_TITLE_BIG)) {
3831 mBigContentTitle = extras.getCharSequence(EXTRA_TITLE_BIG);
3832 }
3833 }
3834
3835
3836 /**
3837 * @hide
3838 */
3839 public Notification buildStyled(Notification wip) {
3840 populateTickerView(wip);
3841 populateContentView(wip);
3842 populateBigContentView(wip);
3843 populateHeadsUpContentView(wip);
3844 return wip;
3845 }
3846
Daniel Sandler0ec46202015-06-24 01:27:05 -04003847 /**
3848 * @hide
3849 */
Jorim Jaggia0d58ae2015-06-03 11:48:13 -07003850 public void purgeResources() {}
3851
Christoph Studer4600f9b2014-07-22 22:44:43 +02003852 // The following methods are split out so we can re-create notification partially.
3853 /**
3854 * @hide
3855 */
3856 protected void populateTickerView(Notification wip) {}
3857 /**
3858 * @hide
3859 */
3860 protected void populateContentView(Notification wip) {}
3861
3862 /**
3863 * @hide
3864 */
3865 protected void populateBigContentView(Notification wip) {}
3866
3867 /**
3868 * @hide
3869 */
3870 protected void populateHeadsUpContentView(Notification wip) {}
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04003871
3872 /**
3873 * Calls {@link android.app.Notification.Builder#build()} on the Builder this Style is
3874 * attached to.
3875 *
3876 * @return the fully constructed Notification.
3877 */
3878 public Notification build() {
3879 checkBuilder();
3880 return mBuilder.build();
3881 }
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02003882
3883 /**
3884 * @hide
3885 * @return true if the style positions the progress bar on the second line; false if the
3886 * style hides the progress bar
3887 */
3888 protected boolean hasProgress() {
3889 return true;
3890 }
Joe Onorato46439ce2010-11-19 13:56:21 -08003891 }
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003892
3893 /**
Daniel Sandler4dfbe832012-04-11 14:51:46 -04003894 * Helper class for generating large-format notifications that include a large image attachment.
Joe Malin8d40d042012-11-05 11:36:40 -08003895 *
Robert Ly91c5ce32014-06-08 15:37:00 -07003896 * Here's how you'd set the <code>BigPictureStyle</code> on a notification:
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003897 * <pre class="prettyprint">
Robert Ly91c5ce32014-06-08 15:37:00 -07003898 * Notification notif = new Notification.Builder(mContext)
3899 * .setContentTitle(&quot;New photo from &quot; + sender.toString())
3900 * .setContentText(subject)
3901 * .setSmallIcon(R.drawable.new_post)
3902 * .setLargeIcon(aBitmap)
3903 * .setStyle(new Notification.BigPictureStyle()
3904 * .bigPicture(aBigBitmap))
3905 * .build();
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003906 * </pre>
Joe Malin8d40d042012-11-05 11:36:40 -08003907 *
Daniel Sandler4dfbe832012-04-11 14:51:46 -04003908 * @see Notification#bigContentView
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003909 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003910 public static class BigPictureStyle extends Style {
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003911 private Bitmap mPicture;
Dan Sandlerd63f9322015-05-06 15:18:49 -04003912 private Icon mBigLargeIcon;
Chris Wren3745a3d2012-05-22 15:11:52 -04003913 private boolean mBigLargeIconSet = false;
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003914
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003915 public BigPictureStyle() {
3916 }
3917
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003918 public BigPictureStyle(Builder builder) {
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003919 setBuilder(builder);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003920 }
3921
Chris Wrend6297db2012-05-03 16:20:13 -04003922 /**
3923 * Overrides ContentTitle in the big form of the template.
3924 * This defaults to the value passed to setContentTitle().
3925 */
3926 public BigPictureStyle setBigContentTitle(CharSequence title) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04003927 internalSetBigContentTitle(safeCharSequence(title));
Chris Wrend6297db2012-05-03 16:20:13 -04003928 return this;
3929 }
3930
3931 /**
3932 * Set the first line of text after the detail section in the big form of the template.
3933 */
3934 public BigPictureStyle setSummaryText(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04003935 internalSetSummaryText(safeCharSequence(cs));
Chris Wrend6297db2012-05-03 16:20:13 -04003936 return this;
3937 }
3938
Chris Wren0bd664d2012-08-01 13:56:56 -04003939 /**
3940 * Provide the bitmap to be used as the payload for the BigPicture notification.
3941 */
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003942 public BigPictureStyle bigPicture(Bitmap b) {
3943 mPicture = b;
3944 return this;
3945 }
3946
Chris Wren3745a3d2012-05-22 15:11:52 -04003947 /**
Chris Wren3745a3d2012-05-22 15:11:52 -04003948 * Override the large icon when the big notification is shown.
3949 */
3950 public BigPictureStyle bigLargeIcon(Bitmap b) {
Dan Sandlerd63f9322015-05-06 15:18:49 -04003951 return bigLargeIcon(b != null ? Icon.createWithBitmap(b) : null);
3952 }
3953
3954 /**
3955 * Override the large icon when the big notification is shown.
3956 */
3957 public BigPictureStyle bigLargeIcon(Icon icon) {
Chris Wren3745a3d2012-05-22 15:11:52 -04003958 mBigLargeIconSet = true;
Dan Sandlerd63f9322015-05-06 15:18:49 -04003959 mBigLargeIcon = icon;
Chris Wren3745a3d2012-05-22 15:11:52 -04003960 return this;
3961 }
3962
Daniel Sandler0ec46202015-06-24 01:27:05 -04003963 /**
3964 * @hide
3965 */
Jorim Jaggia0d58ae2015-06-03 11:48:13 -07003966 @Override
3967 public void purgeResources() {
3968 super.purgeResources();
3969 if (mPicture != null && mPicture.isMutable()) {
3970 mPicture = mPicture.createAshmemBitmap();
3971 }
3972 if (mBigLargeIcon != null) {
3973 mBigLargeIcon.convertToAshmem();
3974 }
3975 }
Christoph Studer5c510ee2014-12-15 16:32:27 +01003976
Jorim Jaggia0d58ae2015-06-03 11:48:13 -07003977 private RemoteViews makeBigContentView() {
Christoph Studer5c510ee2014-12-15 16:32:27 +01003978 // Replace mLargeIcon with mBigLargeIcon if mBigLargeIconSet
3979 // This covers the following cases:
3980 // 1. mBigLargeIconSet -> mBigLargeIcon (null or non-null) applies, overrides
3981 // mLargeIcon
3982 // 2. !mBigLargeIconSet -> mLargeIcon applies
Dan Sandlerd63f9322015-05-06 15:18:49 -04003983 Icon oldLargeIcon = null;
Christoph Studer5c510ee2014-12-15 16:32:27 +01003984 if (mBigLargeIconSet) {
3985 oldLargeIcon = mBuilder.mLargeIcon;
3986 mBuilder.mLargeIcon = mBigLargeIcon;
3987 }
3988
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01003989 RemoteViews contentView = getStandardView(mBuilder.getBigPictureLayoutResource());
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003990
Christoph Studer5c510ee2014-12-15 16:32:27 +01003991 if (mBigLargeIconSet) {
3992 mBuilder.mLargeIcon = oldLargeIcon;
3993 }
3994
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003995 contentView.setImageViewBitmap(R.id.big_picture, mPicture);
3996
Jorim Jaggi445d3c02014-08-19 22:33:42 +02003997 applyTopPadding(contentView);
3998
Kenny Guy98193ea2014-07-24 19:54:37 +01003999 boolean twoTextLines = mBuilder.mSubText != null && mBuilder.mContentText != null;
4000 mBuilder.addProfileBadge(contentView,
4001 twoTextLines ? R.id.profile_badge_line2 : R.id.profile_badge_line3);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004002 return contentView;
4003 }
4004
Daniel Sandlerf45564e2013-04-15 15:05:08 -04004005 /**
4006 * @hide
4007 */
4008 public void addExtras(Bundle extras) {
4009 super.addExtras(extras);
4010
4011 if (mBigLargeIconSet) {
4012 extras.putParcelable(EXTRA_LARGE_ICON_BIG, mBigLargeIcon);
4013 }
4014 extras.putParcelable(EXTRA_PICTURE, mPicture);
4015 }
4016
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04004017 /**
4018 * @hide
4019 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004020 @Override
Christoph Studer4600f9b2014-07-22 22:44:43 +02004021 protected void restoreFromExtras(Bundle extras) {
4022 super.restoreFromExtras(extras);
4023
4024 if (extras.containsKey(EXTRA_LARGE_ICON_BIG)) {
Christoph Studer5c510ee2014-12-15 16:32:27 +01004025 mBigLargeIconSet = true;
Christoph Studer4600f9b2014-07-22 22:44:43 +02004026 mBigLargeIcon = extras.getParcelable(EXTRA_LARGE_ICON_BIG);
Chris Wren3745a3d2012-05-22 15:11:52 -04004027 }
Christoph Studer4600f9b2014-07-22 22:44:43 +02004028 mPicture = extras.getParcelable(EXTRA_PICTURE);
4029 }
4030
4031 /**
4032 * @hide
4033 */
4034 @Override
4035 public void populateBigContentView(Notification wip) {
4036 mBuilder.setBuilderBigContentView(wip, makeBigContentView());
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004037 }
4038 }
4039
4040 /**
Daniel Sandler4dfbe832012-04-11 14:51:46 -04004041 * Helper class for generating large-format notifications that include a lot of text.
Joe Malin8d40d042012-11-05 11:36:40 -08004042 *
Robert Ly91c5ce32014-06-08 15:37:00 -07004043 * Here's how you'd set the <code>BigTextStyle</code> on a notification:
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004044 * <pre class="prettyprint">
Robert Ly91c5ce32014-06-08 15:37:00 -07004045 * Notification notif = new Notification.Builder(mContext)
4046 * .setContentTitle(&quot;New mail from &quot; + sender.toString())
4047 * .setContentText(subject)
4048 * .setSmallIcon(R.drawable.new_mail)
4049 * .setLargeIcon(aBitmap)
4050 * .setStyle(new Notification.BigTextStyle()
4051 * .bigText(aVeryLongString))
4052 * .build();
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004053 * </pre>
Joe Malin8d40d042012-11-05 11:36:40 -08004054 *
Daniel Sandler4dfbe832012-04-11 14:51:46 -04004055 * @see Notification#bigContentView
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004056 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004057 public static class BigTextStyle extends Style {
Jorim Jaggi457a10d2014-09-08 16:18:23 +02004058
4059 private static final int MAX_LINES = 13;
4060 private static final int LINES_CONSUMED_BY_ACTIONS = 3;
4061 private static final int LINES_CONSUMED_BY_SUMMARY = 2;
4062
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004063 private CharSequence mBigText;
4064
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004065 public BigTextStyle() {
4066 }
4067
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004068 public BigTextStyle(Builder builder) {
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004069 setBuilder(builder);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004070 }
4071
Chris Wrend6297db2012-05-03 16:20:13 -04004072 /**
4073 * Overrides ContentTitle in the big form of the template.
4074 * This defaults to the value passed to setContentTitle().
4075 */
4076 public BigTextStyle setBigContentTitle(CharSequence title) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04004077 internalSetBigContentTitle(safeCharSequence(title));
Chris Wrend6297db2012-05-03 16:20:13 -04004078 return this;
4079 }
4080
4081 /**
4082 * Set the first line of text after the detail section in the big form of the template.
4083 */
4084 public BigTextStyle setSummaryText(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04004085 internalSetSummaryText(safeCharSequence(cs));
Chris Wrend6297db2012-05-03 16:20:13 -04004086 return this;
4087 }
4088
Chris Wren0bd664d2012-08-01 13:56:56 -04004089 /**
4090 * Provide the longer text to be displayed in the big form of the
4091 * template in place of the content text.
4092 */
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004093 public BigTextStyle bigText(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04004094 mBigText = safeCharSequence(cs);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004095 return this;
4096 }
4097
Daniel Sandlerf45564e2013-04-15 15:05:08 -04004098 /**
4099 * @hide
4100 */
4101 public void addExtras(Bundle extras) {
4102 super.addExtras(extras);
4103
Christoph Studer4600f9b2014-07-22 22:44:43 +02004104 extras.putCharSequence(EXTRA_BIG_TEXT, mBigText);
4105 }
4106
4107 /**
4108 * @hide
4109 */
4110 @Override
4111 protected void restoreFromExtras(Bundle extras) {
4112 super.restoreFromExtras(extras);
4113
4114 mBigText = extras.getCharSequence(EXTRA_BIG_TEXT);
Daniel Sandlerf45564e2013-04-15 15:05:08 -04004115 }
4116
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004117 private RemoteViews makeBigContentView() {
Christoph Studer4600f9b2014-07-22 22:44:43 +02004118
4119 // Nasty
4120 CharSequence oldBuilderContentText = mBuilder.mContentText;
Daniel Sandler6387d2f2012-05-22 13:44:09 -04004121 mBuilder.mContentText = null;
Daniel Sandler916ad912012-06-13 12:17:07 -04004122
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01004123 RemoteViews contentView = getStandardView(mBuilder.getBigTextLayoutResource());
Joe Malin8d40d042012-11-05 11:36:40 -08004124
Christoph Studer4600f9b2014-07-22 22:44:43 +02004125 mBuilder.mContentText = oldBuilderContentText;
4126
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01004127 contentView.setTextViewText(R.id.big_text, mBuilder.processLegacyText(mBigText));
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004128 contentView.setViewVisibility(R.id.big_text, View.VISIBLE);
Jorim Jaggi457a10d2014-09-08 16:18:23 +02004129 contentView.setInt(R.id.big_text, "setMaxLines", calculateMaxLines());
Chris Wren3c5f92432012-05-04 16:31:17 -04004130 contentView.setViewVisibility(R.id.text2, View.GONE);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004131
Jorim Jaggi445d3c02014-08-19 22:33:42 +02004132 applyTopPadding(contentView);
4133
Jorim Jaggid05aa3e2014-08-28 17:52:27 +02004134 mBuilder.shrinkLine3Text(contentView);
4135
Kenny Guy98193ea2014-07-24 19:54:37 +01004136 mBuilder.addProfileBadge(contentView, R.id.profile_badge_large_template);
4137
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004138 return contentView;
4139 }
4140
Jorim Jaggi457a10d2014-09-08 16:18:23 +02004141 private int calculateMaxLines() {
4142 int lineCount = MAX_LINES;
4143 boolean hasActions = mBuilder.mActions.size() > 0;
4144 boolean hasSummary = (mSummaryTextSet ? mSummaryText : mBuilder.mSubText) != null;
4145 if (hasActions) {
4146 lineCount -= LINES_CONSUMED_BY_ACTIONS;
4147 }
4148 if (hasSummary) {
4149 lineCount -= LINES_CONSUMED_BY_SUMMARY;
4150 }
4151
4152 // If we have less top padding at the top, we can fit less lines.
4153 if (!mBuilder.mHasThreeLines) {
4154 lineCount--;
4155 }
4156 return lineCount;
4157 }
4158
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04004159 /**
4160 * @hide
4161 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004162 @Override
Christoph Studer4600f9b2014-07-22 22:44:43 +02004163 public void populateBigContentView(Notification wip) {
4164 mBuilder.setBuilderBigContentView(wip, makeBigContentView());
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004165 }
4166 }
Daniel Sandler879c5e02012-04-17 16:46:51 -04004167
4168 /**
4169 * Helper class for generating large-format notifications that include a list of (up to 5) strings.
Joe Malin8d40d042012-11-05 11:36:40 -08004170 *
Robert Ly91c5ce32014-06-08 15:37:00 -07004171 * Here's how you'd set the <code>InboxStyle</code> on a notification:
Daniel Sandler879c5e02012-04-17 16:46:51 -04004172 * <pre class="prettyprint">
Robert Ly91c5ce32014-06-08 15:37:00 -07004173 * Notification notif = new Notification.Builder(mContext)
4174 * .setContentTitle(&quot;5 New mails from &quot; + sender.toString())
4175 * .setContentText(subject)
4176 * .setSmallIcon(R.drawable.new_mail)
4177 * .setLargeIcon(aBitmap)
4178 * .setStyle(new Notification.InboxStyle()
4179 * .addLine(str1)
4180 * .addLine(str2)
4181 * .setContentTitle(&quot;&quot;)
4182 * .setSummaryText(&quot;+3 more&quot;))
4183 * .build();
Daniel Sandler879c5e02012-04-17 16:46:51 -04004184 * </pre>
Joe Malin8d40d042012-11-05 11:36:40 -08004185 *
Daniel Sandler879c5e02012-04-17 16:46:51 -04004186 * @see Notification#bigContentView
4187 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004188 public static class InboxStyle extends Style {
Daniel Sandler879c5e02012-04-17 16:46:51 -04004189 private ArrayList<CharSequence> mTexts = new ArrayList<CharSequence>(5);
4190
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004191 public InboxStyle() {
4192 }
4193
Daniel Sandler879c5e02012-04-17 16:46:51 -04004194 public InboxStyle(Builder builder) {
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004195 setBuilder(builder);
Daniel Sandler879c5e02012-04-17 16:46:51 -04004196 }
4197
Chris Wrend6297db2012-05-03 16:20:13 -04004198 /**
4199 * Overrides ContentTitle in the big form of the template.
4200 * This defaults to the value passed to setContentTitle().
4201 */
4202 public InboxStyle setBigContentTitle(CharSequence title) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04004203 internalSetBigContentTitle(safeCharSequence(title));
Chris Wrend6297db2012-05-03 16:20:13 -04004204 return this;
4205 }
4206
4207 /**
4208 * Set the first line of text after the detail section in the big form of the template.
4209 */
4210 public InboxStyle setSummaryText(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04004211 internalSetSummaryText(safeCharSequence(cs));
Chris Wrend6297db2012-05-03 16:20:13 -04004212 return this;
4213 }
4214
Chris Wren0bd664d2012-08-01 13:56:56 -04004215 /**
4216 * Append a line to the digest section of the Inbox notification.
4217 */
Daniel Sandler879c5e02012-04-17 16:46:51 -04004218 public InboxStyle addLine(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04004219 mTexts.add(safeCharSequence(cs));
Daniel Sandler879c5e02012-04-17 16:46:51 -04004220 return this;
4221 }
4222
Daniel Sandlerf45564e2013-04-15 15:05:08 -04004223 /**
4224 * @hide
4225 */
4226 public void addExtras(Bundle extras) {
4227 super.addExtras(extras);
Christoph Studer4600f9b2014-07-22 22:44:43 +02004228
Daniel Sandlerf45564e2013-04-15 15:05:08 -04004229 CharSequence[] a = new CharSequence[mTexts.size()];
4230 extras.putCharSequenceArray(EXTRA_TEXT_LINES, mTexts.toArray(a));
4231 }
4232
Christoph Studer4600f9b2014-07-22 22:44:43 +02004233 /**
4234 * @hide
4235 */
4236 @Override
4237 protected void restoreFromExtras(Bundle extras) {
4238 super.restoreFromExtras(extras);
4239
4240 mTexts.clear();
4241 if (extras.containsKey(EXTRA_TEXT_LINES)) {
4242 Collections.addAll(mTexts, extras.getCharSequenceArray(EXTRA_TEXT_LINES));
4243 }
4244 }
4245
Daniel Sandler879c5e02012-04-17 16:46:51 -04004246 private RemoteViews makeBigContentView() {
Daniel Sandler619738c2012-06-07 16:33:08 -04004247 // Remove the content text so line3 disappears unless you have a summary
Christoph Studer4600f9b2014-07-22 22:44:43 +02004248
4249 // Nasty
4250 CharSequence oldBuilderContentText = mBuilder.mContentText;
Daniel Sandler619738c2012-06-07 16:33:08 -04004251 mBuilder.mContentText = null;
Christoph Studer4600f9b2014-07-22 22:44:43 +02004252
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01004253 RemoteViews contentView = getStandardView(mBuilder.getInboxLayoutResource());
Daniel Sandler619738c2012-06-07 16:33:08 -04004254
Christoph Studer4600f9b2014-07-22 22:44:43 +02004255 mBuilder.mContentText = oldBuilderContentText;
4256
Chris Wrend6297db2012-05-03 16:20:13 -04004257 contentView.setViewVisibility(R.id.text2, View.GONE);
Daniel Sandler879c5e02012-04-17 16:46:51 -04004258
Chris Wrend6297db2012-05-03 16:20:13 -04004259 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 -04004260 R.id.inbox_text4, R.id.inbox_text5, R.id.inbox_text6};
Chris Wrend6297db2012-05-03 16:20:13 -04004261
Chris Wren4ed80d52012-05-17 09:30:03 -04004262 // Make sure all rows are gone in case we reuse a view.
4263 for (int rowId : rowIds) {
4264 contentView.setViewVisibility(rowId, View.GONE);
4265 }
4266
Jorim Jaggi445d3c02014-08-19 22:33:42 +02004267 final boolean largeText =
4268 mBuilder.mContext.getResources().getConfiguration().fontScale > 1f;
4269 final float subTextSize = mBuilder.mContext.getResources().getDimensionPixelSize(
4270 R.dimen.notification_subtext_size);
Daniel Sandler879c5e02012-04-17 16:46:51 -04004271 int i=0;
4272 while (i < mTexts.size() && i < rowIds.length) {
4273 CharSequence str = mTexts.get(i);
4274 if (str != null && !str.equals("")) {
4275 contentView.setViewVisibility(rowIds[i], View.VISIBLE);
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01004276 contentView.setTextViewText(rowIds[i], mBuilder.processLegacyText(str));
Jorim Jaggi445d3c02014-08-19 22:33:42 +02004277 if (largeText) {
4278 contentView.setTextViewTextSize(rowIds[i], TypedValue.COMPLEX_UNIT_PX,
4279 subTextSize);
4280 }
Daniel Sandler879c5e02012-04-17 16:46:51 -04004281 }
4282 i++;
4283 }
4284
Chris Wren683ab002012-09-20 10:35:54 -04004285 contentView.setViewVisibility(R.id.inbox_end_pad,
4286 mTexts.size() > 0 ? View.VISIBLE : View.GONE);
4287
4288 contentView.setViewVisibility(R.id.inbox_more,
4289 mTexts.size() > rowIds.length ? View.VISIBLE : View.GONE);
Chris Wren29bb6d92012-05-17 18:09:42 -04004290
Jorim Jaggi445d3c02014-08-19 22:33:42 +02004291 applyTopPadding(contentView);
4292
Jorim Jaggid05aa3e2014-08-28 17:52:27 +02004293 mBuilder.shrinkLine3Text(contentView);
4294
Kenny Guy98193ea2014-07-24 19:54:37 +01004295 mBuilder.addProfileBadge(contentView, R.id.profile_badge_large_template);
4296
Daniel Sandler879c5e02012-04-17 16:46:51 -04004297 return contentView;
4298 }
4299
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04004300 /**
4301 * @hide
4302 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004303 @Override
Christoph Studer4600f9b2014-07-22 22:44:43 +02004304 public void populateBigContentView(Notification wip) {
4305 mBuilder.setBuilderBigContentView(wip, makeBigContentView());
Daniel Sandler879c5e02012-04-17 16:46:51 -04004306 }
4307 }
Dan Sandler842dd772014-05-15 09:36:47 -04004308
4309 /**
4310 * Notification style for media playback notifications.
4311 *
4312 * In the expanded form, {@link Notification#bigContentView}, up to 5
4313 * {@link Notification.Action}s specified with
Dan Sandler86647982015-05-13 23:41:13 -04004314 * {@link Notification.Builder#addAction(Action) addAction} will be
Dan Sandler842dd772014-05-15 09:36:47 -04004315 * shown as icon-only pushbuttons, suitable for transport controls. The Bitmap given to
4316 * {@link Notification.Builder#setLargeIcon(android.graphics.Bitmap) setLargeIcon()} will be
4317 * treated as album artwork.
4318 *
4319 * Unlike the other styles provided here, MediaStyle can also modify the standard-size
4320 * {@link Notification#contentView}; by providing action indices to
Christoph Studerfde6f4d2014-12-12 13:23:26 +01004321 * {@link #setShowActionsInCompactView(int...)} you can promote up to 3 actions to be displayed
Dan Sandler842dd772014-05-15 09:36:47 -04004322 * in the standard view alongside the usual content.
4323 *
Bryan Mawhinney6be8de32014-07-18 10:35:12 +01004324 * Notifications created with MediaStyle will have their category set to
4325 * {@link Notification#CATEGORY_TRANSPORT CATEGORY_TRANSPORT} unless you set a different
4326 * category using {@link Notification.Builder#setCategory(String) setCategory()}.
4327 *
Jeff Browndba34ba2014-06-24 20:46:03 -07004328 * Finally, if you attach a {@link android.media.session.MediaSession.Token} using
4329 * {@link android.app.Notification.MediaStyle#setMediaSession(MediaSession.Token)},
Dan Sandler842dd772014-05-15 09:36:47 -04004330 * the System UI can identify this as a notification representing an active media session
4331 * and respond accordingly (by showing album artwork in the lockscreen, for example).
4332 *
4333 * To use this style with your Notification, feed it to
4334 * {@link Notification.Builder#setStyle(android.app.Notification.Style)} like so:
4335 * <pre class="prettyprint">
4336 * Notification noti = new Notification.Builder()
4337 * .setSmallIcon(R.drawable.ic_stat_player)
Christoph Studere935fe92014-11-24 14:18:06 +01004338 * .setContentTitle(&quot;Track title&quot;)
4339 * .setContentText(&quot;Artist - Album&quot;)
4340 * .setLargeIcon(albumArtBitmap))
Bryan Mawhinney6be8de32014-07-18 10:35:12 +01004341 * .setStyle(<b>new Notification.MediaStyle()</b>
4342 * .setMediaSession(mySession))
Dan Sandler842dd772014-05-15 09:36:47 -04004343 * .build();
4344 * </pre>
4345 *
4346 * @see Notification#bigContentView
4347 */
4348 public static class MediaStyle extends Style {
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02004349 static final int MAX_MEDIA_BUTTONS_IN_COMPACT = 3;
Dan Sandler842dd772014-05-15 09:36:47 -04004350 static final int MAX_MEDIA_BUTTONS = 5;
4351
4352 private int[] mActionsToShowInCompact = null;
Jeff Browndba34ba2014-06-24 20:46:03 -07004353 private MediaSession.Token mToken;
Dan Sandler842dd772014-05-15 09:36:47 -04004354
4355 public MediaStyle() {
4356 }
4357
4358 public MediaStyle(Builder builder) {
4359 setBuilder(builder);
4360 }
4361
4362 /**
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02004363 * 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 -04004364 * notification view.
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02004365 *
4366 * @param actions the indices of the actions to show in the compact notification view
Dan Sandler842dd772014-05-15 09:36:47 -04004367 */
4368 public MediaStyle setShowActionsInCompactView(int...actions) {
4369 mActionsToShowInCompact = actions;
4370 return this;
4371 }
4372
4373 /**
Jeff Browndba34ba2014-06-24 20:46:03 -07004374 * Attach a {@link android.media.session.MediaSession.Token} to this Notification
4375 * to provide additional playback information and control to the SystemUI.
Dan Sandler842dd772014-05-15 09:36:47 -04004376 */
Jeff Browndba34ba2014-06-24 20:46:03 -07004377 public MediaStyle setMediaSession(MediaSession.Token token) {
Dan Sandler842dd772014-05-15 09:36:47 -04004378 mToken = token;
4379 return this;
4380 }
4381
Christoph Studer4600f9b2014-07-22 22:44:43 +02004382 /**
4383 * @hide
4384 */
Dan Sandler842dd772014-05-15 09:36:47 -04004385 @Override
4386 public Notification buildStyled(Notification wip) {
Christoph Studer4600f9b2014-07-22 22:44:43 +02004387 super.buildStyled(wip);
Bryan Mawhinney6be8de32014-07-18 10:35:12 +01004388 if (wip.category == null) {
4389 wip.category = Notification.CATEGORY_TRANSPORT;
4390 }
Dan Sandler842dd772014-05-15 09:36:47 -04004391 return wip;
4392 }
4393
Christoph Studer4600f9b2014-07-22 22:44:43 +02004394 /**
4395 * @hide
4396 */
4397 @Override
4398 public void populateContentView(Notification wip) {
4399 mBuilder.setBuilderContentView(wip, makeMediaContentView());
4400 }
4401
4402 /**
4403 * @hide
4404 */
4405 @Override
4406 public void populateBigContentView(Notification wip) {
4407 mBuilder.setBuilderBigContentView(wip, makeMediaBigContentView());
4408 }
4409
Dan Sandler842dd772014-05-15 09:36:47 -04004410 /** @hide */
4411 @Override
4412 public void addExtras(Bundle extras) {
4413 super.addExtras(extras);
4414
4415 if (mToken != null) {
4416 extras.putParcelable(EXTRA_MEDIA_SESSION, mToken);
4417 }
Bryan Mawhinneye191f902014-07-22 12:50:09 +01004418 if (mActionsToShowInCompact != null) {
4419 extras.putIntArray(EXTRA_COMPACT_ACTIONS, mActionsToShowInCompact);
4420 }
Dan Sandler842dd772014-05-15 09:36:47 -04004421 }
4422
Christoph Studer4600f9b2014-07-22 22:44:43 +02004423 /**
4424 * @hide
4425 */
4426 @Override
4427 protected void restoreFromExtras(Bundle extras) {
4428 super.restoreFromExtras(extras);
4429
4430 if (extras.containsKey(EXTRA_MEDIA_SESSION)) {
4431 mToken = extras.getParcelable(EXTRA_MEDIA_SESSION);
4432 }
4433 if (extras.containsKey(EXTRA_COMPACT_ACTIONS)) {
4434 mActionsToShowInCompact = extras.getIntArray(EXTRA_COMPACT_ACTIONS);
4435 }
4436 }
4437
Dan Sandler842dd772014-05-15 09:36:47 -04004438 private RemoteViews generateMediaActionButton(Action action) {
4439 final boolean tombstone = (action.actionIntent == null);
4440 RemoteViews button = new RemoteViews(mBuilder.mContext.getPackageName(),
Alan Viverette3cb07a462014-06-06 14:19:53 -07004441 R.layout.notification_material_media_action);
Dan Sandler842dd772014-05-15 09:36:47 -04004442 button.setImageViewResource(R.id.action0, action.icon);
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02004443 button.setDrawableParameters(R.id.action0, false, -1,
4444 0xFFFFFFFF,
4445 PorterDuff.Mode.SRC_ATOP, -1);
Dan Sandler842dd772014-05-15 09:36:47 -04004446 if (!tombstone) {
4447 button.setOnClickPendingIntent(R.id.action0, action.actionIntent);
4448 }
4449 button.setContentDescription(R.id.action0, action.title);
4450 return button;
4451 }
4452
4453 private RemoteViews makeMediaContentView() {
4454 RemoteViews view = mBuilder.applyStandardTemplate(
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02004455 R.layout.notification_template_material_media, false /* hasProgress */);
Dan Sandler842dd772014-05-15 09:36:47 -04004456
4457 final int numActions = mBuilder.mActions.size();
4458 final int N = mActionsToShowInCompact == null
4459 ? 0
4460 : Math.min(mActionsToShowInCompact.length, MAX_MEDIA_BUTTONS_IN_COMPACT);
4461 if (N > 0) {
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02004462 view.removeAllViews(com.android.internal.R.id.media_actions);
Dan Sandler842dd772014-05-15 09:36:47 -04004463 for (int i = 0; i < N; i++) {
4464 if (i >= numActions) {
4465 throw new IllegalArgumentException(String.format(
4466 "setShowActionsInCompactView: action %d out of bounds (max %d)",
4467 i, numActions - 1));
4468 }
4469
4470 final Action action = mBuilder.mActions.get(mActionsToShowInCompact[i]);
4471 final RemoteViews button = generateMediaActionButton(action);
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02004472 view.addView(com.android.internal.R.id.media_actions, button);
Dan Sandler842dd772014-05-15 09:36:47 -04004473 }
4474 }
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02004475 styleText(view);
4476 hideRightIcon(view);
Dan Sandler842dd772014-05-15 09:36:47 -04004477 return view;
4478 }
4479
4480 private RemoteViews makeMediaBigContentView() {
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02004481 final int actionCount = Math.min(mBuilder.mActions.size(), MAX_MEDIA_BUTTONS);
4482 RemoteViews big = mBuilder.applyStandardTemplate(getBigLayoutResource(actionCount),
4483 false /* hasProgress */);
Dan Sandler842dd772014-05-15 09:36:47 -04004484
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02004485 if (actionCount > 0) {
4486 big.removeAllViews(com.android.internal.R.id.media_actions);
4487 for (int i = 0; i < actionCount; i++) {
Dan Sandler842dd772014-05-15 09:36:47 -04004488 final RemoteViews button = generateMediaActionButton(mBuilder.mActions.get(i));
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02004489 big.addView(com.android.internal.R.id.media_actions, button);
Dan Sandler842dd772014-05-15 09:36:47 -04004490 }
4491 }
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02004492 styleText(big);
4493 hideRightIcon(big);
4494 applyTopPadding(big);
4495 big.setViewVisibility(android.R.id.progress, View.GONE);
Dan Sandler842dd772014-05-15 09:36:47 -04004496 return big;
4497 }
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02004498
4499 private int getBigLayoutResource(int actionCount) {
4500 if (actionCount <= 3) {
4501 return R.layout.notification_template_material_big_media_narrow;
4502 } else {
4503 return R.layout.notification_template_material_big_media;
4504 }
4505 }
4506
4507 private void hideRightIcon(RemoteViews contentView) {
4508 contentView.setViewVisibility(R.id.right_icon, View.GONE);
4509 }
4510
4511 /**
4512 * Applies the special text colors for media notifications to all text views.
4513 */
4514 private void styleText(RemoteViews contentView) {
Alan Viverette4a357cd2015-03-18 18:37:18 -07004515 int primaryColor = mBuilder.mContext.getColor(
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02004516 R.color.notification_media_primary_color);
Alan Viverette4a357cd2015-03-18 18:37:18 -07004517 int secondaryColor = mBuilder.mContext.getColor(
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02004518 R.color.notification_media_secondary_color);
4519 contentView.setTextColor(R.id.title, primaryColor);
4520 if (mBuilder.showsTimeOrChronometer()) {
4521 if (mBuilder.mUseChronometer) {
4522 contentView.setTextColor(R.id.chronometer, secondaryColor);
4523 } else {
4524 contentView.setTextColor(R.id.time, secondaryColor);
4525 }
4526 }
4527 contentView.setTextColor(R.id.text2, secondaryColor);
4528 contentView.setTextColor(R.id.text, secondaryColor);
4529 contentView.setTextColor(R.id.info, secondaryColor);
4530 }
4531
4532 /**
4533 * @hide
4534 */
4535 @Override
4536 protected boolean hasProgress() {
4537 return false;
4538 }
Dan Sandler842dd772014-05-15 09:36:47 -04004539 }
Griff Hazen61a9e862014-05-22 16:05:19 -07004540
Christoph Studer4600f9b2014-07-22 22:44:43 +02004541 // When adding a new Style subclass here, don't forget to update
4542 // Builder.getNotificationStyleClass.
4543
Griff Hazen61a9e862014-05-22 16:05:19 -07004544 /**
4545 * Extender interface for use with {@link Builder#extend}. Extenders may be used to add
4546 * metadata or change options on a notification builder.
4547 */
4548 public interface Extender {
4549 /**
4550 * Apply this extender to a notification builder.
4551 * @param builder the builder to be modified.
4552 * @return the build object for chaining.
4553 */
4554 public Builder extend(Builder builder);
4555 }
4556
4557 /**
4558 * Helper class to add wearable extensions to notifications.
4559 * <p class="note"> See
4560 * <a href="{@docRoot}wear/notifications/creating.html">Creating Notifications
4561 * for Android Wear</a> for more information on how to use this class.
4562 * <p>
4563 * To create a notification with wearable extensions:
4564 * <ol>
4565 * <li>Create a {@link android.app.Notification.Builder}, setting any desired
4566 * properties.
4567 * <li>Create a {@link android.app.Notification.WearableExtender}.
4568 * <li>Set wearable-specific properties using the
4569 * {@code add} and {@code set} methods of {@link android.app.Notification.WearableExtender}.
4570 * <li>Call {@link android.app.Notification.Builder#extend} to apply the extensions to a
4571 * notification.
4572 * <li>Post the notification to the notification system with the
4573 * {@code NotificationManager.notify(...)} methods.
4574 * </ol>
4575 *
4576 * <pre class="prettyprint">
4577 * Notification notif = new Notification.Builder(mContext)
4578 * .setContentTitle(&quot;New mail from &quot; + sender.toString())
4579 * .setContentText(subject)
4580 * .setSmallIcon(R.drawable.new_mail)
4581 * .extend(new Notification.WearableExtender()
4582 * .setContentIcon(R.drawable.new_mail))
4583 * .build();
4584 * NotificationManager notificationManger =
4585 * (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
4586 * notificationManger.notify(0, notif);</pre>
4587 *
4588 * <p>Wearable extensions can be accessed on an existing notification by using the
4589 * {@code WearableExtender(Notification)} constructor,
4590 * and then using the {@code get} methods to access values.
4591 *
4592 * <pre class="prettyprint">
4593 * Notification.WearableExtender wearableExtender = new Notification.WearableExtender(
4594 * notification);
Griff Hazen14f57992014-05-26 09:07:14 -07004595 * List&lt;Notification&gt; pages = wearableExtender.getPages();</pre>
Griff Hazen61a9e862014-05-22 16:05:19 -07004596 */
4597 public static final class WearableExtender implements Extender {
4598 /**
4599 * Sentinel value for an action index that is unset.
4600 */
4601 public static final int UNSET_ACTION_INDEX = -1;
4602
4603 /**
4604 * Size value for use with {@link #setCustomSizePreset} to show this notification with
4605 * default sizing.
4606 * <p>For custom display notifications created using {@link #setDisplayIntent},
4607 * the default is {@link #SIZE_LARGE}. All other notifications size automatically based
4608 * on their content.
4609 */
4610 public static final int SIZE_DEFAULT = 0;
4611
4612 /**
4613 * Size value for use with {@link #setCustomSizePreset} to show this notification
4614 * with an extra small size.
4615 * <p>This value is only applicable for custom display notifications created using
4616 * {@link #setDisplayIntent}.
4617 */
4618 public static final int SIZE_XSMALL = 1;
4619
4620 /**
4621 * Size value for use with {@link #setCustomSizePreset} to show this notification
4622 * with a small size.
4623 * <p>This value is only applicable for custom display notifications created using
4624 * {@link #setDisplayIntent}.
4625 */
4626 public static final int SIZE_SMALL = 2;
4627
4628 /**
4629 * Size value for use with {@link #setCustomSizePreset} to show this notification
4630 * with a medium size.
4631 * <p>This value is only applicable for custom display notifications created using
4632 * {@link #setDisplayIntent}.
4633 */
4634 public static final int SIZE_MEDIUM = 3;
4635
4636 /**
4637 * Size value for use with {@link #setCustomSizePreset} to show this notification
4638 * with a large size.
4639 * <p>This value is only applicable for custom display notifications created using
4640 * {@link #setDisplayIntent}.
4641 */
4642 public static final int SIZE_LARGE = 4;
4643
Griff Hazend5f11f92014-05-27 15:40:09 -07004644 /**
4645 * Size value for use with {@link #setCustomSizePreset} to show this notification
4646 * full screen.
4647 * <p>This value is only applicable for custom display notifications created using
4648 * {@link #setDisplayIntent}.
4649 */
4650 public static final int SIZE_FULL_SCREEN = 5;
4651
Griff Hazen5f2edfc2014-09-29 16:28:44 -07004652 /**
4653 * Sentinel value for use with {@link #setHintScreenTimeout} to keep the screen on for a
4654 * short amount of time when this notification is displayed on the screen. This
4655 * is the default value.
4656 */
4657 public static final int SCREEN_TIMEOUT_SHORT = 0;
4658
4659 /**
4660 * Sentinel value for use with {@link #setHintScreenTimeout} to keep the screen on
4661 * for a longer amount of time when this notification is displayed on the screen.
4662 */
4663 public static final int SCREEN_TIMEOUT_LONG = -1;
4664
Griff Hazen61a9e862014-05-22 16:05:19 -07004665 /** Notification extra which contains wearable extensions */
4666 private static final String EXTRA_WEARABLE_EXTENSIONS = "android.wearable.EXTENSIONS";
4667
Pete Gastaf6781d2014-10-07 15:17:05 -04004668 // Keys within EXTRA_WEARABLE_EXTENSIONS for wearable options.
Griff Hazen61a9e862014-05-22 16:05:19 -07004669 private static final String KEY_ACTIONS = "actions";
4670 private static final String KEY_FLAGS = "flags";
4671 private static final String KEY_DISPLAY_INTENT = "displayIntent";
4672 private static final String KEY_PAGES = "pages";
4673 private static final String KEY_BACKGROUND = "background";
4674 private static final String KEY_CONTENT_ICON = "contentIcon";
4675 private static final String KEY_CONTENT_ICON_GRAVITY = "contentIconGravity";
4676 private static final String KEY_CONTENT_ACTION_INDEX = "contentActionIndex";
4677 private static final String KEY_CUSTOM_SIZE_PRESET = "customSizePreset";
4678 private static final String KEY_CUSTOM_CONTENT_HEIGHT = "customContentHeight";
4679 private static final String KEY_GRAVITY = "gravity";
Griff Hazen5f2edfc2014-09-29 16:28:44 -07004680 private static final String KEY_HINT_SCREEN_TIMEOUT = "hintScreenTimeout";
Griff Hazen61a9e862014-05-22 16:05:19 -07004681
4682 // Flags bitwise-ored to mFlags
4683 private static final int FLAG_CONTENT_INTENT_AVAILABLE_OFFLINE = 0x1;
4684 private static final int FLAG_HINT_HIDE_ICON = 1 << 1;
4685 private static final int FLAG_HINT_SHOW_BACKGROUND_ONLY = 1 << 2;
4686 private static final int FLAG_START_SCROLL_BOTTOM = 1 << 3;
Griff Hazen5f2edfc2014-09-29 16:28:44 -07004687 private static final int FLAG_HINT_AVOID_BACKGROUND_CLIPPING = 1 << 4;
Griff Hazen61a9e862014-05-22 16:05:19 -07004688
4689 // Default value for flags integer
4690 private static final int DEFAULT_FLAGS = FLAG_CONTENT_INTENT_AVAILABLE_OFFLINE;
4691
4692 private static final int DEFAULT_CONTENT_ICON_GRAVITY = Gravity.END;
4693 private static final int DEFAULT_GRAVITY = Gravity.BOTTOM;
4694
4695 private ArrayList<Action> mActions = new ArrayList<Action>();
4696 private int mFlags = DEFAULT_FLAGS;
4697 private PendingIntent mDisplayIntent;
4698 private ArrayList<Notification> mPages = new ArrayList<Notification>();
4699 private Bitmap mBackground;
4700 private int mContentIcon;
4701 private int mContentIconGravity = DEFAULT_CONTENT_ICON_GRAVITY;
4702 private int mContentActionIndex = UNSET_ACTION_INDEX;
4703 private int mCustomSizePreset = SIZE_DEFAULT;
4704 private int mCustomContentHeight;
4705 private int mGravity = DEFAULT_GRAVITY;
Griff Hazen5f2edfc2014-09-29 16:28:44 -07004706 private int mHintScreenTimeout;
Griff Hazen61a9e862014-05-22 16:05:19 -07004707
4708 /**
4709 * Create a {@link android.app.Notification.WearableExtender} with default
4710 * options.
4711 */
4712 public WearableExtender() {
4713 }
4714
4715 public WearableExtender(Notification notif) {
4716 Bundle wearableBundle = notif.extras.getBundle(EXTRA_WEARABLE_EXTENSIONS);
4717 if (wearableBundle != null) {
4718 List<Action> actions = wearableBundle.getParcelableArrayList(KEY_ACTIONS);
4719 if (actions != null) {
4720 mActions.addAll(actions);
4721 }
4722
4723 mFlags = wearableBundle.getInt(KEY_FLAGS, DEFAULT_FLAGS);
4724 mDisplayIntent = wearableBundle.getParcelable(KEY_DISPLAY_INTENT);
4725
4726 Notification[] pages = getNotificationArrayFromBundle(
4727 wearableBundle, KEY_PAGES);
4728 if (pages != null) {
4729 Collections.addAll(mPages, pages);
4730 }
4731
4732 mBackground = wearableBundle.getParcelable(KEY_BACKGROUND);
4733 mContentIcon = wearableBundle.getInt(KEY_CONTENT_ICON);
4734 mContentIconGravity = wearableBundle.getInt(KEY_CONTENT_ICON_GRAVITY,
4735 DEFAULT_CONTENT_ICON_GRAVITY);
4736 mContentActionIndex = wearableBundle.getInt(KEY_CONTENT_ACTION_INDEX,
4737 UNSET_ACTION_INDEX);
4738 mCustomSizePreset = wearableBundle.getInt(KEY_CUSTOM_SIZE_PRESET,
4739 SIZE_DEFAULT);
4740 mCustomContentHeight = wearableBundle.getInt(KEY_CUSTOM_CONTENT_HEIGHT);
4741 mGravity = wearableBundle.getInt(KEY_GRAVITY, DEFAULT_GRAVITY);
Griff Hazen5f2edfc2014-09-29 16:28:44 -07004742 mHintScreenTimeout = wearableBundle.getInt(KEY_HINT_SCREEN_TIMEOUT);
Griff Hazen61a9e862014-05-22 16:05:19 -07004743 }
4744 }
4745
4746 /**
4747 * Apply wearable extensions to a notification that is being built. This is typically
4748 * called by the {@link android.app.Notification.Builder#extend} method of
4749 * {@link android.app.Notification.Builder}.
4750 */
4751 @Override
4752 public Notification.Builder extend(Notification.Builder builder) {
4753 Bundle wearableBundle = new Bundle();
4754
4755 if (!mActions.isEmpty()) {
4756 wearableBundle.putParcelableArrayList(KEY_ACTIONS, mActions);
4757 }
4758 if (mFlags != DEFAULT_FLAGS) {
4759 wearableBundle.putInt(KEY_FLAGS, mFlags);
4760 }
4761 if (mDisplayIntent != null) {
4762 wearableBundle.putParcelable(KEY_DISPLAY_INTENT, mDisplayIntent);
4763 }
4764 if (!mPages.isEmpty()) {
4765 wearableBundle.putParcelableArray(KEY_PAGES, mPages.toArray(
4766 new Notification[mPages.size()]));
4767 }
4768 if (mBackground != null) {
4769 wearableBundle.putParcelable(KEY_BACKGROUND, mBackground);
4770 }
4771 if (mContentIcon != 0) {
4772 wearableBundle.putInt(KEY_CONTENT_ICON, mContentIcon);
4773 }
4774 if (mContentIconGravity != DEFAULT_CONTENT_ICON_GRAVITY) {
4775 wearableBundle.putInt(KEY_CONTENT_ICON_GRAVITY, mContentIconGravity);
4776 }
4777 if (mContentActionIndex != UNSET_ACTION_INDEX) {
4778 wearableBundle.putInt(KEY_CONTENT_ACTION_INDEX,
4779 mContentActionIndex);
4780 }
4781 if (mCustomSizePreset != SIZE_DEFAULT) {
4782 wearableBundle.putInt(KEY_CUSTOM_SIZE_PRESET, mCustomSizePreset);
4783 }
4784 if (mCustomContentHeight != 0) {
4785 wearableBundle.putInt(KEY_CUSTOM_CONTENT_HEIGHT, mCustomContentHeight);
4786 }
4787 if (mGravity != DEFAULT_GRAVITY) {
4788 wearableBundle.putInt(KEY_GRAVITY, mGravity);
4789 }
Griff Hazen5f2edfc2014-09-29 16:28:44 -07004790 if (mHintScreenTimeout != 0) {
4791 wearableBundle.putInt(KEY_HINT_SCREEN_TIMEOUT, mHintScreenTimeout);
4792 }
Griff Hazen61a9e862014-05-22 16:05:19 -07004793
4794 builder.getExtras().putBundle(EXTRA_WEARABLE_EXTENSIONS, wearableBundle);
4795 return builder;
4796 }
4797
4798 @Override
4799 public WearableExtender clone() {
4800 WearableExtender that = new WearableExtender();
4801 that.mActions = new ArrayList<Action>(this.mActions);
4802 that.mFlags = this.mFlags;
4803 that.mDisplayIntent = this.mDisplayIntent;
4804 that.mPages = new ArrayList<Notification>(this.mPages);
4805 that.mBackground = this.mBackground;
4806 that.mContentIcon = this.mContentIcon;
4807 that.mContentIconGravity = this.mContentIconGravity;
4808 that.mContentActionIndex = this.mContentActionIndex;
4809 that.mCustomSizePreset = this.mCustomSizePreset;
4810 that.mCustomContentHeight = this.mCustomContentHeight;
4811 that.mGravity = this.mGravity;
Griff Hazen5f2edfc2014-09-29 16:28:44 -07004812 that.mHintScreenTimeout = this.mHintScreenTimeout;
Griff Hazen61a9e862014-05-22 16:05:19 -07004813 return that;
4814 }
4815
4816 /**
4817 * Add a wearable action to this notification.
4818 *
4819 * <p>When wearable actions are added using this method, the set of actions that
4820 * show on a wearable device splits from devices that only show actions added
4821 * using {@link android.app.Notification.Builder#addAction}. This allows for customization
4822 * of which actions display on different devices.
4823 *
4824 * @param action the action to add to this notification
4825 * @return this object for method chaining
4826 * @see android.app.Notification.Action
4827 */
4828 public WearableExtender addAction(Action action) {
4829 mActions.add(action);
4830 return this;
4831 }
4832
4833 /**
4834 * Adds wearable actions to this notification.
4835 *
4836 * <p>When wearable actions are added using this method, the set of actions that
4837 * show on a wearable device splits from devices that only show actions added
4838 * using {@link android.app.Notification.Builder#addAction}. This allows for customization
4839 * of which actions display on different devices.
4840 *
4841 * @param actions the actions to add to this notification
4842 * @return this object for method chaining
4843 * @see android.app.Notification.Action
4844 */
4845 public WearableExtender addActions(List<Action> actions) {
4846 mActions.addAll(actions);
4847 return this;
4848 }
4849
4850 /**
4851 * Clear all wearable actions present on this builder.
4852 * @return this object for method chaining.
4853 * @see #addAction
4854 */
4855 public WearableExtender clearActions() {
4856 mActions.clear();
4857 return this;
4858 }
4859
4860 /**
4861 * Get the wearable actions present on this notification.
4862 */
4863 public List<Action> getActions() {
4864 return mActions;
4865 }
4866
4867 /**
4868 * Set an intent to launch inside of an activity view when displaying
Griff Hazen14f57992014-05-26 09:07:14 -07004869 * this notification. The {@link PendingIntent} provided should be for an activity.
4870 *
4871 * <pre class="prettyprint">
4872 * Intent displayIntent = new Intent(context, MyDisplayActivity.class);
4873 * PendingIntent displayPendingIntent = PendingIntent.getActivity(context,
4874 * 0, displayIntent, PendingIntent.FLAG_UPDATE_CURRENT);
4875 * Notification notif = new Notification.Builder(context)
4876 * .extend(new Notification.WearableExtender()
4877 * .setDisplayIntent(displayPendingIntent)
4878 * .setCustomSizePreset(Notification.WearableExtender.SIZE_MEDIUM))
4879 * .build();</pre>
4880 *
4881 * <p>The activity to launch needs to allow embedding, must be exported, and
Griff Hazen831ca9d2014-06-17 00:38:38 -07004882 * should have an empty task affinity. It is also recommended to use the device
4883 * default light theme.
Griff Hazen14f57992014-05-26 09:07:14 -07004884 *
4885 * <p>Example AndroidManifest.xml entry:
4886 * <pre class="prettyprint">
4887 * &lt;activity android:name=&quot;com.example.MyDisplayActivity&quot;
4888 * android:exported=&quot;true&quot;
4889 * android:allowEmbedded=&quot;true&quot;
Griff Hazen831ca9d2014-06-17 00:38:38 -07004890 * android:taskAffinity=&quot;&quot;
4891 * android:theme=&quot;@android:style/Theme.DeviceDefault.Light&quot; /&gt;</pre>
Griff Hazen61a9e862014-05-22 16:05:19 -07004892 *
4893 * @param intent the {@link PendingIntent} for an activity
4894 * @return this object for method chaining
4895 * @see android.app.Notification.WearableExtender#getDisplayIntent
4896 */
4897 public WearableExtender setDisplayIntent(PendingIntent intent) {
4898 mDisplayIntent = intent;
4899 return this;
4900 }
4901
4902 /**
4903 * Get the intent to launch inside of an activity view when displaying this
4904 * notification. This {@code PendingIntent} should be for an activity.
4905 */
4906 public PendingIntent getDisplayIntent() {
4907 return mDisplayIntent;
4908 }
4909
4910 /**
4911 * Add an additional page of content to display with this notification. The current
4912 * notification forms the first page, and pages added using this function form
4913 * subsequent pages. This field can be used to separate a notification into multiple
4914 * sections.
4915 *
4916 * @param page the notification to add as another page
4917 * @return this object for method chaining
4918 * @see android.app.Notification.WearableExtender#getPages
4919 */
4920 public WearableExtender addPage(Notification page) {
4921 mPages.add(page);
4922 return this;
4923 }
4924
4925 /**
4926 * Add additional pages of content to display with this notification. The current
4927 * notification forms the first page, and pages added using this function form
4928 * subsequent pages. This field can be used to separate a notification into multiple
4929 * sections.
4930 *
4931 * @param pages a list of notifications
4932 * @return this object for method chaining
4933 * @see android.app.Notification.WearableExtender#getPages
4934 */
4935 public WearableExtender addPages(List<Notification> pages) {
4936 mPages.addAll(pages);
4937 return this;
4938 }
4939
4940 /**
4941 * Clear all additional pages present on this builder.
4942 * @return this object for method chaining.
4943 * @see #addPage
4944 */
4945 public WearableExtender clearPages() {
4946 mPages.clear();
4947 return this;
4948 }
4949
4950 /**
4951 * Get the array of additional pages of content for displaying this notification. The
4952 * current notification forms the first page, and elements within this array form
4953 * subsequent pages. This field can be used to separate a notification into multiple
4954 * sections.
4955 * @return the pages for this notification
4956 */
4957 public List<Notification> getPages() {
4958 return mPages;
4959 }
4960
4961 /**
4962 * Set a background image to be displayed behind the notification content.
4963 * Contrary to the {@link android.app.Notification.BigPictureStyle}, this background
4964 * will work with any notification style.
4965 *
4966 * @param background the background bitmap
4967 * @return this object for method chaining
4968 * @see android.app.Notification.WearableExtender#getBackground
4969 */
4970 public WearableExtender setBackground(Bitmap background) {
4971 mBackground = background;
4972 return this;
4973 }
4974
4975 /**
4976 * Get a background image to be displayed behind the notification content.
4977 * Contrary to the {@link android.app.Notification.BigPictureStyle}, this background
4978 * will work with any notification style.
4979 *
4980 * @return the background image
4981 * @see android.app.Notification.WearableExtender#setBackground
4982 */
4983 public Bitmap getBackground() {
4984 return mBackground;
4985 }
4986
4987 /**
4988 * Set an icon that goes with the content of this notification.
4989 */
4990 public WearableExtender setContentIcon(int icon) {
4991 mContentIcon = icon;
4992 return this;
4993 }
4994
4995 /**
4996 * Get an icon that goes with the content of this notification.
4997 */
4998 public int getContentIcon() {
4999 return mContentIcon;
5000 }
5001
5002 /**
5003 * Set the gravity that the content icon should have within the notification display.
5004 * Supported values include {@link android.view.Gravity#START} and
5005 * {@link android.view.Gravity#END}. The default value is {@link android.view.Gravity#END}.
5006 * @see #setContentIcon
5007 */
5008 public WearableExtender setContentIconGravity(int contentIconGravity) {
5009 mContentIconGravity = contentIconGravity;
5010 return this;
5011 }
5012
5013 /**
5014 * Get the gravity that the content icon should have within the notification display.
5015 * Supported values include {@link android.view.Gravity#START} and
5016 * {@link android.view.Gravity#END}. The default value is {@link android.view.Gravity#END}.
5017 * @see #getContentIcon
5018 */
5019 public int getContentIconGravity() {
5020 return mContentIconGravity;
5021 }
5022
5023 /**
5024 * Set an action from this notification's actions to be clickable with the content of
Griff Hazen14f57992014-05-26 09:07:14 -07005025 * this notification. This action will no longer display separately from the
5026 * notification's content.
5027 *
Griff Hazenca48d352014-05-28 22:37:13 -07005028 * <p>For notifications with multiple pages, child pages can also have content actions
Griff Hazen14f57992014-05-26 09:07:14 -07005029 * set, although the list of available actions comes from the main notification and not
5030 * from the child page's notification.
5031 *
5032 * @param actionIndex The index of the action to hoist onto the current notification page.
5033 * If wearable actions were added to the main notification, this index
5034 * will apply to that list, otherwise it will apply to the regular
5035 * actions list.
Griff Hazen61a9e862014-05-22 16:05:19 -07005036 */
5037 public WearableExtender setContentAction(int actionIndex) {
5038 mContentActionIndex = actionIndex;
5039 return this;
5040 }
5041
5042 /**
Griff Hazenca48d352014-05-28 22:37:13 -07005043 * Get the index of the notification action, if any, that was specified as being clickable
5044 * with the content of this notification. This action will no longer display separately
Griff Hazen14f57992014-05-26 09:07:14 -07005045 * from the notification's content.
Griff Hazen61a9e862014-05-22 16:05:19 -07005046 *
Griff Hazenca48d352014-05-28 22:37:13 -07005047 * <p>For notifications with multiple pages, child pages can also have content actions
Griff Hazen14f57992014-05-26 09:07:14 -07005048 * set, although the list of available actions comes from the main notification and not
5049 * from the child page's notification.
5050 *
5051 * <p>If wearable specific actions were added to the main notification, this index will
5052 * apply to that list, otherwise it will apply to the regular actions list.
Griff Hazenca48d352014-05-28 22:37:13 -07005053 *
5054 * @return the action index or {@link #UNSET_ACTION_INDEX} if no action was selected.
Griff Hazen61a9e862014-05-22 16:05:19 -07005055 */
5056 public int getContentAction() {
5057 return mContentActionIndex;
5058 }
5059
5060 /**
5061 * Set the gravity that this notification should have within the available viewport space.
5062 * Supported values include {@link android.view.Gravity#TOP},
5063 * {@link android.view.Gravity#CENTER_VERTICAL} and {@link android.view.Gravity#BOTTOM}.
5064 * The default value is {@link android.view.Gravity#BOTTOM}.
5065 */
5066 public WearableExtender setGravity(int gravity) {
5067 mGravity = gravity;
5068 return this;
5069 }
5070
5071 /**
5072 * Get the gravity that this notification should have within the available viewport space.
5073 * Supported values include {@link android.view.Gravity#TOP},
5074 * {@link android.view.Gravity#CENTER_VERTICAL} and {@link android.view.Gravity#BOTTOM}.
5075 * The default value is {@link android.view.Gravity#BOTTOM}.
5076 */
5077 public int getGravity() {
5078 return mGravity;
5079 }
5080
5081 /**
5082 * Set the custom size preset for the display of this notification out of the available
5083 * presets found in {@link android.app.Notification.WearableExtender}, e.g.
5084 * {@link #SIZE_LARGE}.
5085 * <p>Some custom size presets are only applicable for custom display notifications created
5086 * using {@link android.app.Notification.WearableExtender#setDisplayIntent}. Check the
5087 * documentation for the preset in question. See also
5088 * {@link #setCustomContentHeight} and {@link #getCustomSizePreset}.
5089 */
5090 public WearableExtender setCustomSizePreset(int sizePreset) {
5091 mCustomSizePreset = sizePreset;
5092 return this;
5093 }
5094
5095 /**
5096 * Get the custom size preset for the display of this notification out of the available
5097 * presets found in {@link android.app.Notification.WearableExtender}, e.g.
5098 * {@link #SIZE_LARGE}.
5099 * <p>Some custom size presets are only applicable for custom display notifications created
5100 * using {@link #setDisplayIntent}. Check the documentation for the preset in question.
5101 * See also {@link #setCustomContentHeight} and {@link #setCustomSizePreset}.
5102 */
5103 public int getCustomSizePreset() {
5104 return mCustomSizePreset;
5105 }
5106
5107 /**
5108 * Set the custom height in pixels for the display of this notification's content.
5109 * <p>This option is only available for custom display notifications created
5110 * using {@link android.app.Notification.WearableExtender#setDisplayIntent}. See also
5111 * {@link android.app.Notification.WearableExtender#setCustomSizePreset} and
5112 * {@link #getCustomContentHeight}.
5113 */
5114 public WearableExtender setCustomContentHeight(int height) {
5115 mCustomContentHeight = height;
5116 return this;
5117 }
5118
5119 /**
5120 * Get the custom height in pixels for the display of this notification's content.
5121 * <p>This option is only available for custom display notifications created
5122 * using {@link #setDisplayIntent}. See also {@link #setCustomSizePreset} and
5123 * {@link #setCustomContentHeight}.
5124 */
5125 public int getCustomContentHeight() {
5126 return mCustomContentHeight;
5127 }
5128
5129 /**
5130 * Set whether the scrolling position for the contents of this notification should start
5131 * at the bottom of the contents instead of the top when the contents are too long to
5132 * display within the screen. Default is false (start scroll at the top).
5133 */
5134 public WearableExtender setStartScrollBottom(boolean startScrollBottom) {
5135 setFlag(FLAG_START_SCROLL_BOTTOM, startScrollBottom);
5136 return this;
5137 }
5138
5139 /**
5140 * Get whether the scrolling position for the contents of this notification should start
5141 * at the bottom of the contents instead of the top when the contents are too long to
5142 * display within the screen. Default is false (start scroll at the top).
5143 */
5144 public boolean getStartScrollBottom() {
5145 return (mFlags & FLAG_START_SCROLL_BOTTOM) != 0;
5146 }
5147
5148 /**
5149 * Set whether the content intent is available when the wearable device is not connected
5150 * to a companion device. The user can still trigger this intent when the wearable device
5151 * is offline, but a visual hint will indicate that the content intent may not be available.
5152 * Defaults to true.
5153 */
5154 public WearableExtender setContentIntentAvailableOffline(
5155 boolean contentIntentAvailableOffline) {
5156 setFlag(FLAG_CONTENT_INTENT_AVAILABLE_OFFLINE, contentIntentAvailableOffline);
5157 return this;
5158 }
5159
5160 /**
5161 * Get whether the content intent is available when the wearable device is not connected
5162 * to a companion device. The user can still trigger this intent when the wearable device
5163 * is offline, but a visual hint will indicate that the content intent may not be available.
5164 * Defaults to true.
5165 */
5166 public boolean getContentIntentAvailableOffline() {
5167 return (mFlags & FLAG_CONTENT_INTENT_AVAILABLE_OFFLINE) != 0;
5168 }
5169
5170 /**
5171 * Set a hint that this notification's icon should not be displayed.
5172 * @param hintHideIcon {@code true} to hide the icon, {@code false} otherwise.
5173 * @return this object for method chaining
5174 */
5175 public WearableExtender setHintHideIcon(boolean hintHideIcon) {
5176 setFlag(FLAG_HINT_HIDE_ICON, hintHideIcon);
5177 return this;
5178 }
5179
5180 /**
5181 * Get a hint that this notification's icon should not be displayed.
5182 * @return {@code true} if this icon should not be displayed, false otherwise.
5183 * The default value is {@code false} if this was never set.
5184 */
5185 public boolean getHintHideIcon() {
5186 return (mFlags & FLAG_HINT_HIDE_ICON) != 0;
5187 }
5188
5189 /**
5190 * Set a visual hint that only the background image of this notification should be
5191 * displayed, and other semantic content should be hidden. This hint is only applicable
5192 * to sub-pages added using {@link #addPage}.
5193 */
5194 public WearableExtender setHintShowBackgroundOnly(boolean hintShowBackgroundOnly) {
5195 setFlag(FLAG_HINT_SHOW_BACKGROUND_ONLY, hintShowBackgroundOnly);
5196 return this;
5197 }
5198
5199 /**
5200 * Get a visual hint that only the background image of this notification should be
5201 * displayed, and other semantic content should be hidden. This hint is only applicable
5202 * to sub-pages added using {@link android.app.Notification.WearableExtender#addPage}.
5203 */
5204 public boolean getHintShowBackgroundOnly() {
5205 return (mFlags & FLAG_HINT_SHOW_BACKGROUND_ONLY) != 0;
5206 }
5207
Griff Hazen5f2edfc2014-09-29 16:28:44 -07005208 /**
Griff Hazen9c5be4e2014-11-17 17:47:50 -08005209 * Set a hint that this notification's background should not be clipped if possible,
5210 * and should instead be resized to fully display on the screen, retaining the aspect
5211 * ratio of the image. This can be useful for images like barcodes or qr codes.
Griff Hazen5f2edfc2014-09-29 16:28:44 -07005212 * @param hintAvoidBackgroundClipping {@code true} to avoid clipping if possible.
5213 * @return this object for method chaining
5214 */
5215 public WearableExtender setHintAvoidBackgroundClipping(
5216 boolean hintAvoidBackgroundClipping) {
5217 setFlag(FLAG_HINT_AVOID_BACKGROUND_CLIPPING, hintAvoidBackgroundClipping);
5218 return this;
5219 }
5220
5221 /**
Griff Hazen9c5be4e2014-11-17 17:47:50 -08005222 * Get a hint that this notification's background should not be clipped if possible,
5223 * and should instead be resized to fully display on the screen, retaining the aspect
5224 * ratio of the image. This can be useful for images like barcodes or qr codes.
Griff Hazen5f2edfc2014-09-29 16:28:44 -07005225 * @return {@code true} if it's ok if the background is clipped on the screen, false
5226 * otherwise. The default value is {@code false} if this was never set.
5227 */
5228 public boolean getHintAvoidBackgroundClipping() {
5229 return (mFlags & FLAG_HINT_AVOID_BACKGROUND_CLIPPING) != 0;
5230 }
5231
5232 /**
5233 * Set a hint that the screen should remain on for at least this duration when
5234 * this notification is displayed on the screen.
5235 * @param timeout The requested screen timeout in milliseconds. Can also be either
5236 * {@link #SCREEN_TIMEOUT_SHORT} or {@link #SCREEN_TIMEOUT_LONG}.
5237 * @return this object for method chaining
5238 */
5239 public WearableExtender setHintScreenTimeout(int timeout) {
5240 mHintScreenTimeout = timeout;
5241 return this;
5242 }
5243
5244 /**
5245 * Get the duration, in milliseconds, that the screen should remain on for
5246 * when this notification is displayed.
5247 * @return the duration in milliseconds if > 0, or either one of the sentinel values
5248 * {@link #SCREEN_TIMEOUT_SHORT} or {@link #SCREEN_TIMEOUT_LONG}.
5249 */
5250 public int getHintScreenTimeout() {
5251 return mHintScreenTimeout;
5252 }
5253
Griff Hazen61a9e862014-05-22 16:05:19 -07005254 private void setFlag(int mask, boolean value) {
5255 if (value) {
5256 mFlags |= mask;
5257 } else {
5258 mFlags &= ~mask;
5259 }
5260 }
5261 }
5262
5263 /**
Zhen Yu Song9d5528b2014-11-14 15:34:39 -08005264 * <p>Helper class to add Android Auto extensions to notifications. To create a notification
5265 * with car extensions:
5266 *
5267 * <ol>
5268 * <li>Create an {@link Notification.Builder}, setting any desired
5269 * properties.
5270 * <li>Create a {@link CarExtender}.
5271 * <li>Set car-specific properties using the {@code add} and {@code set} methods of
5272 * {@link CarExtender}.
5273 * <li>Call {@link Notification.Builder#extend(Notification.Extender)}
5274 * to apply the extensions to a notification.
5275 * </ol>
5276 *
5277 * <pre class="prettyprint">
5278 * Notification notification = new Notification.Builder(context)
5279 * ...
5280 * .extend(new CarExtender()
5281 * .set*(...))
5282 * .build();
5283 * </pre>
5284 *
5285 * <p>Car extensions can be accessed on an existing notification by using the
5286 * {@code CarExtender(Notification)} constructor, and then using the {@code get} methods
5287 * to access values.
5288 */
5289 public static final class CarExtender implements Extender {
5290 private static final String TAG = "CarExtender";
5291
5292 private static final String EXTRA_CAR_EXTENDER = "android.car.EXTENSIONS";
5293 private static final String EXTRA_LARGE_ICON = "large_icon";
5294 private static final String EXTRA_CONVERSATION = "car_conversation";
5295 private static final String EXTRA_COLOR = "app_color";
5296
5297 private Bitmap mLargeIcon;
5298 private UnreadConversation mUnreadConversation;
5299 private int mColor = Notification.COLOR_DEFAULT;
5300
5301 /**
5302 * Create a {@link CarExtender} with default options.
5303 */
5304 public CarExtender() {
5305 }
5306
5307 /**
5308 * Create a {@link CarExtender} from the CarExtender options of an existing Notification.
5309 *
5310 * @param notif The notification from which to copy options.
5311 */
5312 public CarExtender(Notification notif) {
5313 Bundle carBundle = notif.extras == null ?
5314 null : notif.extras.getBundle(EXTRA_CAR_EXTENDER);
5315 if (carBundle != null) {
5316 mLargeIcon = carBundle.getParcelable(EXTRA_LARGE_ICON);
5317 mColor = carBundle.getInt(EXTRA_COLOR, Notification.COLOR_DEFAULT);
5318
5319 Bundle b = carBundle.getBundle(EXTRA_CONVERSATION);
5320 mUnreadConversation = UnreadConversation.getUnreadConversationFromBundle(b);
5321 }
5322 }
5323
5324 /**
5325 * Apply car extensions to a notification that is being built. This is typically called by
5326 * the {@link Notification.Builder#extend(Notification.Extender)}
5327 * method of {@link Notification.Builder}.
5328 */
5329 @Override
5330 public Notification.Builder extend(Notification.Builder builder) {
5331 Bundle carExtensions = new Bundle();
5332
5333 if (mLargeIcon != null) {
5334 carExtensions.putParcelable(EXTRA_LARGE_ICON, mLargeIcon);
5335 }
5336 if (mColor != Notification.COLOR_DEFAULT) {
5337 carExtensions.putInt(EXTRA_COLOR, mColor);
5338 }
5339
5340 if (mUnreadConversation != null) {
5341 Bundle b = mUnreadConversation.getBundleForUnreadConversation();
5342 carExtensions.putBundle(EXTRA_CONVERSATION, b);
5343 }
5344
5345 builder.getExtras().putBundle(EXTRA_CAR_EXTENDER, carExtensions);
5346 return builder;
5347 }
5348
5349 /**
5350 * Sets the accent color to use when Android Auto presents the notification.
5351 *
5352 * Android Auto uses the color set with {@link Notification.Builder#setColor(int)}
5353 * to accent the displayed notification. However, not all colors are acceptable in an
5354 * automotive setting. This method can be used to override the color provided in the
5355 * notification in such a situation.
5356 */
Tor Norbye80756e32015-03-02 09:39:27 -08005357 public CarExtender setColor(@ColorInt int color) {
Zhen Yu Song9d5528b2014-11-14 15:34:39 -08005358 mColor = color;
5359 return this;
5360 }
5361
5362 /**
5363 * Gets the accent color.
5364 *
5365 * @see setColor
5366 */
Tor Norbye80756e32015-03-02 09:39:27 -08005367 @ColorInt
Zhen Yu Song9d5528b2014-11-14 15:34:39 -08005368 public int getColor() {
5369 return mColor;
5370 }
5371
5372 /**
5373 * Sets the large icon of the car notification.
5374 *
5375 * If no large icon is set in the extender, Android Auto will display the icon
5376 * specified by {@link Notification.Builder#setLargeIcon(android.graphics.Bitmap)}
5377 *
5378 * @param largeIcon The large icon to use in the car notification.
5379 * @return This object for method chaining.
5380 */
5381 public CarExtender setLargeIcon(Bitmap largeIcon) {
5382 mLargeIcon = largeIcon;
5383 return this;
5384 }
5385
5386 /**
5387 * Gets the large icon used in this car notification, or null if no icon has been set.
5388 *
5389 * @return The large icon for the car notification.
5390 * @see CarExtender#setLargeIcon
5391 */
5392 public Bitmap getLargeIcon() {
5393 return mLargeIcon;
5394 }
5395
5396 /**
5397 * Sets the unread conversation in a message notification.
5398 *
5399 * @param unreadConversation The unread part of the conversation this notification conveys.
5400 * @return This object for method chaining.
5401 */
5402 public CarExtender setUnreadConversation(UnreadConversation unreadConversation) {
5403 mUnreadConversation = unreadConversation;
5404 return this;
5405 }
5406
5407 /**
5408 * Returns the unread conversation conveyed by this notification.
5409 * @see #setUnreadConversation(UnreadConversation)
5410 */
5411 public UnreadConversation getUnreadConversation() {
5412 return mUnreadConversation;
5413 }
5414
5415 /**
5416 * A class which holds the unread messages from a conversation.
5417 */
5418 public static class UnreadConversation {
5419 private static final String KEY_AUTHOR = "author";
5420 private static final String KEY_TEXT = "text";
5421 private static final String KEY_MESSAGES = "messages";
5422 private static final String KEY_REMOTE_INPUT = "remote_input";
5423 private static final String KEY_ON_REPLY = "on_reply";
5424 private static final String KEY_ON_READ = "on_read";
5425 private static final String KEY_PARTICIPANTS = "participants";
5426 private static final String KEY_TIMESTAMP = "timestamp";
5427
5428 private final String[] mMessages;
5429 private final RemoteInput mRemoteInput;
5430 private final PendingIntent mReplyPendingIntent;
5431 private final PendingIntent mReadPendingIntent;
5432 private final String[] mParticipants;
5433 private final long mLatestTimestamp;
5434
5435 UnreadConversation(String[] messages, RemoteInput remoteInput,
5436 PendingIntent replyPendingIntent, PendingIntent readPendingIntent,
5437 String[] participants, long latestTimestamp) {
5438 mMessages = messages;
5439 mRemoteInput = remoteInput;
5440 mReadPendingIntent = readPendingIntent;
5441 mReplyPendingIntent = replyPendingIntent;
5442 mParticipants = participants;
5443 mLatestTimestamp = latestTimestamp;
5444 }
5445
5446 /**
5447 * Gets the list of messages conveyed by this notification.
5448 */
5449 public String[] getMessages() {
5450 return mMessages;
5451 }
5452
5453 /**
5454 * Gets the remote input that will be used to convey the response to a message list, or
5455 * null if no such remote input exists.
5456 */
5457 public RemoteInput getRemoteInput() {
5458 return mRemoteInput;
5459 }
5460
5461 /**
5462 * Gets the pending intent that will be triggered when the user replies to this
5463 * notification.
5464 */
5465 public PendingIntent getReplyPendingIntent() {
5466 return mReplyPendingIntent;
5467 }
5468
5469 /**
5470 * Gets the pending intent that Android Auto will send after it reads aloud all messages
5471 * in this object's message list.
5472 */
5473 public PendingIntent getReadPendingIntent() {
5474 return mReadPendingIntent;
5475 }
5476
5477 /**
5478 * Gets the participants in the conversation.
5479 */
5480 public String[] getParticipants() {
5481 return mParticipants;
5482 }
5483
5484 /**
5485 * Gets the firs participant in the conversation.
5486 */
5487 public String getParticipant() {
5488 return mParticipants.length > 0 ? mParticipants[0] : null;
5489 }
5490
5491 /**
5492 * Gets the timestamp of the conversation.
5493 */
5494 public long getLatestTimestamp() {
5495 return mLatestTimestamp;
5496 }
5497
5498 Bundle getBundleForUnreadConversation() {
5499 Bundle b = new Bundle();
5500 String author = null;
5501 if (mParticipants != null && mParticipants.length > 1) {
5502 author = mParticipants[0];
5503 }
5504 Parcelable[] messages = new Parcelable[mMessages.length];
5505 for (int i = 0; i < messages.length; i++) {
5506 Bundle m = new Bundle();
5507 m.putString(KEY_TEXT, mMessages[i]);
5508 m.putString(KEY_AUTHOR, author);
5509 messages[i] = m;
5510 }
5511 b.putParcelableArray(KEY_MESSAGES, messages);
5512 if (mRemoteInput != null) {
5513 b.putParcelable(KEY_REMOTE_INPUT, mRemoteInput);
5514 }
5515 b.putParcelable(KEY_ON_REPLY, mReplyPendingIntent);
5516 b.putParcelable(KEY_ON_READ, mReadPendingIntent);
5517 b.putStringArray(KEY_PARTICIPANTS, mParticipants);
5518 b.putLong(KEY_TIMESTAMP, mLatestTimestamp);
5519 return b;
5520 }
5521
5522 static UnreadConversation getUnreadConversationFromBundle(Bundle b) {
5523 if (b == null) {
5524 return null;
5525 }
5526 Parcelable[] parcelableMessages = b.getParcelableArray(KEY_MESSAGES);
5527 String[] messages = null;
5528 if (parcelableMessages != null) {
5529 String[] tmp = new String[parcelableMessages.length];
5530 boolean success = true;
5531 for (int i = 0; i < tmp.length; i++) {
5532 if (!(parcelableMessages[i] instanceof Bundle)) {
5533 success = false;
5534 break;
5535 }
5536 tmp[i] = ((Bundle) parcelableMessages[i]).getString(KEY_TEXT);
5537 if (tmp[i] == null) {
5538 success = false;
5539 break;
5540 }
5541 }
5542 if (success) {
5543 messages = tmp;
5544 } else {
5545 return null;
5546 }
5547 }
5548
5549 PendingIntent onRead = b.getParcelable(KEY_ON_READ);
5550 PendingIntent onReply = b.getParcelable(KEY_ON_REPLY);
5551
5552 RemoteInput remoteInput = b.getParcelable(KEY_REMOTE_INPUT);
5553
5554 String[] participants = b.getStringArray(KEY_PARTICIPANTS);
5555 if (participants == null || participants.length != 1) {
5556 return null;
5557 }
5558
5559 return new UnreadConversation(messages,
5560 remoteInput,
5561 onReply,
5562 onRead,
5563 participants, b.getLong(KEY_TIMESTAMP));
5564 }
5565 };
5566
5567 /**
5568 * Builder class for {@link CarExtender.UnreadConversation} objects.
5569 */
5570 public static class Builder {
5571 private final List<String> mMessages = new ArrayList<String>();
5572 private final String mParticipant;
5573 private RemoteInput mRemoteInput;
5574 private PendingIntent mReadPendingIntent;
5575 private PendingIntent mReplyPendingIntent;
5576 private long mLatestTimestamp;
5577
5578 /**
5579 * Constructs a new builder for {@link CarExtender.UnreadConversation}.
5580 *
5581 * @param name The name of the other participant in the conversation.
5582 */
5583 public Builder(String name) {
5584 mParticipant = name;
5585 }
5586
5587 /**
5588 * Appends a new unread message to the list of messages for this conversation.
5589 *
5590 * The messages should be added from oldest to newest.
5591 *
5592 * @param message The text of the new unread message.
5593 * @return This object for method chaining.
5594 */
5595 public Builder addMessage(String message) {
5596 mMessages.add(message);
5597 return this;
5598 }
5599
5600 /**
5601 * Sets the pending intent and remote input which will convey the reply to this
5602 * notification.
5603 *
5604 * @param pendingIntent The pending intent which will be triggered on a reply.
5605 * @param remoteInput The remote input parcelable which will carry the reply.
5606 * @return This object for method chaining.
5607 *
5608 * @see CarExtender.UnreadConversation#getRemoteInput
5609 * @see CarExtender.UnreadConversation#getReplyPendingIntent
5610 */
5611 public Builder setReplyAction(
5612 PendingIntent pendingIntent, RemoteInput remoteInput) {
5613 mRemoteInput = remoteInput;
5614 mReplyPendingIntent = pendingIntent;
5615
5616 return this;
5617 }
5618
5619 /**
5620 * Sets the pending intent that will be sent once the messages in this notification
5621 * are read.
5622 *
5623 * @param pendingIntent The pending intent to use.
5624 * @return This object for method chaining.
5625 */
5626 public Builder setReadPendingIntent(PendingIntent pendingIntent) {
5627 mReadPendingIntent = pendingIntent;
5628 return this;
5629 }
5630
5631 /**
5632 * Sets the timestamp of the most recent message in an unread conversation.
5633 *
5634 * If a messaging notification has been posted by your application and has not
5635 * yet been cancelled, posting a later notification with the same id and tag
5636 * but without a newer timestamp may result in Android Auto not displaying a
5637 * heads up notification for the later notification.
5638 *
5639 * @param timestamp The timestamp of the most recent message in the conversation.
5640 * @return This object for method chaining.
5641 */
5642 public Builder setLatestTimestamp(long timestamp) {
5643 mLatestTimestamp = timestamp;
5644 return this;
5645 }
5646
5647 /**
5648 * Builds a new unread conversation object.
5649 *
5650 * @return The new unread conversation object.
5651 */
5652 public UnreadConversation build() {
5653 String[] messages = mMessages.toArray(new String[mMessages.size()]);
5654 String[] participants = { mParticipant };
5655 return new UnreadConversation(messages, mRemoteInput, mReplyPendingIntent,
5656 mReadPendingIntent, participants, mLatestTimestamp);
5657 }
5658 }
5659 }
5660
5661 /**
Griff Hazen61a9e862014-05-22 16:05:19 -07005662 * Get an array of Notification objects from a parcelable array bundle field.
5663 * Update the bundle to have a typed array so fetches in the future don't need
5664 * to do an array copy.
5665 */
5666 private static Notification[] getNotificationArrayFromBundle(Bundle bundle, String key) {
5667 Parcelable[] array = bundle.getParcelableArray(key);
5668 if (array instanceof Notification[] || array == null) {
5669 return (Notification[]) array;
5670 }
5671 Notification[] typedArray = Arrays.copyOf(array, array.length,
5672 Notification[].class);
5673 bundle.putParcelableArray(key, typedArray);
5674 return typedArray;
5675 }
Christoph Studer4600f9b2014-07-22 22:44:43 +02005676
5677 private static class BuilderRemoteViews extends RemoteViews {
5678 public BuilderRemoteViews(Parcel parcel) {
5679 super(parcel);
5680 }
5681
Kenny Guy77320062014-08-27 21:37:15 +01005682 public BuilderRemoteViews(ApplicationInfo appInfo, int layoutId) {
5683 super(appInfo, layoutId);
Christoph Studer4600f9b2014-07-22 22:44:43 +02005684 }
5685
5686 @Override
5687 public BuilderRemoteViews clone() {
5688 Parcel p = Parcel.obtain();
5689 writeToParcel(p, 0);
5690 p.setDataPosition(0);
5691 BuilderRemoteViews brv = new BuilderRemoteViews(p);
5692 p.recycle();
5693 return brv;
5694 }
5695 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005696}