blob: 464212e55dd7b345851bf1734bd8cf3b800b0e77 [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.
Daniel Sandlerd952dae2011-02-07 16:33:36 -0500169 * This is required; notifications with an invalid icon resource will not be shown.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800170 */
Tor Norbye7b9c9122013-05-30 16:48:33 -0700171 @DrawableRes
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800172 public int icon;
173
174 /**
Joe Onorato46439ce2010-11-19 13:56:21 -0800175 * If the icon in the status bar is to have more than one level, you can set this. Otherwise,
176 * leave it at its default value of 0.
177 *
178 * @see android.widget.ImageView#setImageLevel
Griff Hazen959591e2014-05-15 22:26:18 -0700179 * @see android.graphics.drawable.Drawable#setLevel
Joe Onorato46439ce2010-11-19 13:56:21 -0800180 */
181 public int iconLevel;
182
183 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500184 * The number of events that this notification represents. For example, in a new mail
185 * notification, this could be the number of unread messages.
Joe Malin8d40d042012-11-05 11:36:40 -0800186 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500187 * The system may or may not use this field to modify the appearance of the notification. For
188 * example, before {@link android.os.Build.VERSION_CODES#HONEYCOMB}, this number was
189 * superimposed over the icon in the status bar. Starting with
190 * {@link android.os.Build.VERSION_CODES#HONEYCOMB}, the template used by
191 * {@link Notification.Builder} has displayed the number in the expanded notification view.
Joe Malin8d40d042012-11-05 11:36:40 -0800192 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500193 * If the number is 0 or negative, it is never shown.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800194 */
195 public int number;
196
197 /**
198 * The intent to execute when the expanded status entry is clicked. If
199 * this is an activity, it must include the
200 * {@link android.content.Intent#FLAG_ACTIVITY_NEW_TASK} flag, which requires
Scott Main7aee61f2011-02-08 11:25:01 -0800201 * that you take care of task management as described in the
202 * <a href="{@docRoot}guide/topics/fundamentals/tasks-and-back-stack.html">Tasks and Back
Dianne Hackborn6ceca582012-01-10 15:24:26 -0800203 * Stack</a> document. In particular, make sure to read the notification section
204 * <a href="{@docRoot}guide/topics/ui/notifiers/notifications.html#HandlingNotifications">Handling
205 * Notifications</a> for the correct ways to launch an application from a
206 * notification.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800207 */
208 public PendingIntent contentIntent;
209
210 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500211 * The intent to execute when the notification is explicitly dismissed by the user, either with
212 * the "Clear All" button or by swiping it away individually.
213 *
214 * This probably shouldn't be launching an activity since several of those will be sent
215 * at the same time.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800216 */
217 public PendingIntent deleteIntent;
218
219 /**
Dianne Hackborn170bae72010-09-03 15:14:28 -0700220 * An intent to launch instead of posting the notification to the status bar.
Joe Onoratocb109a02011-01-18 17:57:41 -0800221 *
Chris Wren47c20a12014-06-18 17:27:29 -0400222 * <p>
223 * The system UI may choose to display a heads-up notification, instead of
224 * launching this intent, while the user is using the device.
225 * </p>
226 *
Joe Onoratocb109a02011-01-18 17:57:41 -0800227 * @see Notification.Builder#setFullScreenIntent
Daniel Sandlere46cbd32010-06-17 10:35:26 -0400228 */
229 public PendingIntent fullScreenIntent;
230
231 /**
Dan Sandler5fcdf6e2014-07-18 11:31:15 -0400232 * Text that summarizes this notification for accessibility services.
233 *
234 * As of the L release, this text is no longer shown on screen, but it is still useful to
235 * accessibility services (where it serves as an audible announcement of the notification's
236 * appearance).
Joe Onoratoef1e7762010-09-17 18:38:38 -0400237 *
Joe Onorato46439ce2010-11-19 13:56:21 -0800238 * @see #tickerView
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800239 */
240 public CharSequence tickerText;
241
242 /**
Dan Sandler5fcdf6e2014-07-18 11:31:15 -0400243 * Formerly, a view showing the {@link #tickerText}.
244 *
245 * No longer displayed in the status bar as of API 21.
Joe Onoratoef1e7762010-09-17 18:38:38 -0400246 */
Dan Sandler5fcdf6e2014-07-18 11:31:15 -0400247 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -0800248 public RemoteViews tickerView;
Joe Onoratoef1e7762010-09-17 18:38:38 -0400249
250 /**
Daniel Sandlere46cbd32010-06-17 10:35:26 -0400251 * The view that will represent this notification in the expanded status bar.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800252 */
253 public RemoteViews contentView;
254
Daniel Sandlera0a938c2012-03-15 08:42:37 -0400255 /**
Daniel Sandler4dfbe832012-04-11 14:51:46 -0400256 * A large-format version of {@link #contentView}, giving the Notification an
Daniel Sandlerf3b73432012-03-27 15:01:25 -0400257 * opportunity to show more detail. The system UI may choose to show this
258 * instead of the normal content view at its discretion.
Daniel Sandlerf3b73432012-03-27 15:01:25 -0400259 */
260 public RemoteViews bigContentView;
261
Chris Wren8fd39ec2014-02-27 17:43:26 -0500262
263 /**
Chris Wren47c20a12014-06-18 17:27:29 -0400264 * A medium-format version of {@link #contentView}, providing the Notification an
265 * opportunity to add action buttons to contentView. At its discretion, the system UI may
266 * choose to show this as a heads-up notification, which will pop up so the user can see
267 * it without leaving their current activity.
Chris Wren8fd39ec2014-02-27 17:43:26 -0500268 */
269 public RemoteViews headsUpContentView;
270
Daniel Sandlerf3b73432012-03-27 15:01:25 -0400271 /**
Joe Onorato46439ce2010-11-19 13:56:21 -0800272 * The bitmap that may escape the bounds of the panel and bar.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800273 */
Joe Onorato46439ce2010-11-19 13:56:21 -0800274 public Bitmap largeIcon;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800275
276 /**
277 * The sound to play.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500278 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800279 * <p>
Chris Wren47c20a12014-06-18 17:27:29 -0400280 * A notification that is noisy is more likely to be presented as a heads-up notification.
281 * </p>
282 *
283 * <p>
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500284 * To play the default notification sound, see {@link #defaults}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800285 * </p>
286 */
287 public Uri sound;
288
289 /**
290 * Use this constant as the value for audioStreamType to request that
291 * the default stream type for notifications be used. Currently the
Jeff Sharkey098d5802012-04-26 17:30:34 -0700292 * default stream type is {@link AudioManager#STREAM_NOTIFICATION}.
John Spurlockc0650f022014-07-19 13:22:39 -0400293 *
294 * @deprecated Use {@link #audioAttributes} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800295 */
Jean-Michel Trivi81f871e2014-08-06 16:32:38 -0700296 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800297 public static final int STREAM_DEFAULT = -1;
298
299 /**
300 * The audio stream type to use when playing the sound.
301 * Should be one of the STREAM_ constants from
302 * {@link android.media.AudioManager}.
John Spurlockc0650f022014-07-19 13:22:39 -0400303 *
304 * @deprecated Use {@link #audioAttributes} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800305 */
Jean-Michel Trivi81f871e2014-08-06 16:32:38 -0700306 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800307 public int audioStreamType = STREAM_DEFAULT;
308
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800309 /**
John Spurlockc0650f022014-07-19 13:22:39 -0400310 * The default value of {@link #audioAttributes}.
311 */
312 public static final AudioAttributes AUDIO_ATTRIBUTES_DEFAULT = new AudioAttributes.Builder()
313 .setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
314 .setUsage(AudioAttributes.USAGE_NOTIFICATION)
315 .build();
316
317 /**
318 * The {@link AudioAttributes audio attributes} to use when playing the sound.
319 */
320 public AudioAttributes audioAttributes = AUDIO_ATTRIBUTES_DEFAULT;
321
322 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500323 * The pattern with which to vibrate.
324 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800325 * <p>
326 * To vibrate the default pattern, see {@link #defaults}.
327 * </p>
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500328 *
Chris Wren47c20a12014-06-18 17:27:29 -0400329 * <p>
330 * A notification that vibrates is more likely to be presented as a heads-up notification.
331 * </p>
332 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800333 * @see android.os.Vibrator#vibrate(long[],int)
334 */
335 public long[] vibrate;
336
337 /**
338 * The color of the led. The hardware will do its best approximation.
339 *
340 * @see #FLAG_SHOW_LIGHTS
341 * @see #flags
342 */
Tor Norbye80756e32015-03-02 09:39:27 -0800343 @ColorInt
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800344 public int ledARGB;
345
346 /**
347 * The number of milliseconds for the LED to be on while it's flashing.
348 * The hardware will do its best approximation.
349 *
350 * @see #FLAG_SHOW_LIGHTS
351 * @see #flags
352 */
353 public int ledOnMS;
354
355 /**
356 * The number of milliseconds for the LED to be off while it's flashing.
357 * The hardware will do its best approximation.
358 *
359 * @see #FLAG_SHOW_LIGHTS
360 * @see #flags
361 */
362 public int ledOffMS;
363
364 /**
365 * Specifies which values should be taken from the defaults.
366 * <p>
367 * To set, OR the desired from {@link #DEFAULT_SOUND},
368 * {@link #DEFAULT_VIBRATE}, {@link #DEFAULT_LIGHTS}. For all default
369 * values, use {@link #DEFAULT_ALL}.
370 * </p>
371 */
372 public int defaults;
373
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800374 /**
375 * Bit to be bitwise-ored into the {@link #flags} field that should be
376 * set if you want the LED on for this notification.
377 * <ul>
378 * <li>To turn the LED off, pass 0 in the alpha channel for colorARGB
379 * or 0 for both ledOnMS and ledOffMS.</li>
380 * <li>To turn the LED on, pass 1 for ledOnMS and 0 for ledOffMS.</li>
381 * <li>To flash the LED, pass the number of milliseconds that it should
382 * be on and off to ledOnMS and ledOffMS.</li>
383 * </ul>
384 * <p>
385 * Since hardware varies, you are not guaranteed that any of the values
386 * you pass are honored exactly. Use the system defaults (TODO) if possible
387 * because they will be set to values that work on any given hardware.
388 * <p>
389 * The alpha channel must be set for forward compatibility.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500390 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800391 */
392 public static final int FLAG_SHOW_LIGHTS = 0x00000001;
393
394 /**
395 * Bit to be bitwise-ored into the {@link #flags} field that should be
396 * set if this notification is in reference to something that is ongoing,
397 * like a phone call. It should not be set if this notification is in
398 * reference to something that happened at a particular point in time,
399 * like a missed phone call.
400 */
401 public static final int FLAG_ONGOING_EVENT = 0x00000002;
402
403 /**
404 * Bit to be bitwise-ored into the {@link #flags} field that if set,
Scott Mainb8b36452009-04-26 15:50:49 -0700405 * the audio will be repeated until the notification is
406 * cancelled or the notification window is opened.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800407 */
408 public static final int FLAG_INSISTENT = 0x00000004;
409
410 /**
411 * Bit to be bitwise-ored into the {@link #flags} field that should be
Griff Hazen293977b2014-04-28 08:37:20 -0700412 * set if you would only like the sound, vibrate and ticker to be played
413 * if the notification was not already showing.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800414 */
415 public static final int FLAG_ONLY_ALERT_ONCE = 0x00000008;
416
417 /**
418 * Bit to be bitwise-ored into the {@link #flags} field that should be
419 * set if the notification should be canceled when it is clicked by the
Daniel Sandler8aa9ae62012-12-04 23:31:47 -0500420 * user.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800421 */
422 public static final int FLAG_AUTO_CANCEL = 0x00000010;
423
424 /**
425 * Bit to be bitwise-ored into the {@link #flags} field that should be
426 * set if the notification should not be canceled when the user clicks
427 * the Clear all button.
428 */
429 public static final int FLAG_NO_CLEAR = 0x00000020;
430
Dianne Hackbornd8a43f62009-08-17 23:33:56 -0700431 /**
432 * Bit to be bitwise-ored into the {@link #flags} field that should be
433 * set if this notification represents a currently running service. This
434 * will normally be set for you by {@link Service#startForeground}.
435 */
436 public static final int FLAG_FOREGROUND_SERVICE = 0x00000040;
437
Daniel Sandlere46cbd32010-06-17 10:35:26 -0400438 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500439 * Obsolete flag indicating high-priority notifications; use the priority field instead.
Joe Malin8d40d042012-11-05 11:36:40 -0800440 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500441 * @deprecated Use {@link #priority} with a positive value.
Daniel Sandlere46cbd32010-06-17 10:35:26 -0400442 */
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500443 public static final int FLAG_HIGH_PRIORITY = 0x00000080;
Daniel Sandlere46cbd32010-06-17 10:35:26 -0400444
Griff Hazendfcb0802014-02-11 12:00:00 -0800445 /**
446 * Bit to be bitswise-ored into the {@link #flags} field that should be
447 * set if this notification is relevant to the current device only
448 * and it is not recommended that it bridge to other devices.
449 */
450 public static final int FLAG_LOCAL_ONLY = 0x00000100;
451
Griff Hazen5cadc3b2014-05-20 09:55:39 -0700452 /**
453 * Bit to be bitswise-ored into the {@link #flags} field that should be
454 * set if this notification is the group summary for a group of notifications.
455 * Grouped notifications may display in a cluster or stack on devices which
456 * support such rendering. Requires a group key also be set using {@link Builder#setGroup}.
457 */
458 public static final int FLAG_GROUP_SUMMARY = 0x00000200;
459
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800460 public int flags;
461
Tor Norbyed9273d62013-05-30 15:59:53 -0700462 /** @hide */
463 @IntDef({PRIORITY_DEFAULT,PRIORITY_LOW,PRIORITY_MIN,PRIORITY_HIGH,PRIORITY_MAX})
464 @Retention(RetentionPolicy.SOURCE)
465 public @interface Priority {}
466
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800467 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500468 * Default notification {@link #priority}. If your application does not prioritize its own
469 * notifications, use this value for all notifications.
470 */
471 public static final int PRIORITY_DEFAULT = 0;
472
473 /**
474 * Lower {@link #priority}, for items that are less important. The UI may choose to show these
475 * items smaller, or at a different position in the list, compared with your app's
476 * {@link #PRIORITY_DEFAULT} items.
477 */
478 public static final int PRIORITY_LOW = -1;
479
480 /**
481 * Lowest {@link #priority}; these items might not be shown to the user except under special
482 * circumstances, such as detailed notification logs.
483 */
484 public static final int PRIORITY_MIN = -2;
485
486 /**
487 * Higher {@link #priority}, for more important notifications or alerts. The UI may choose to
488 * show these items larger, or at a different position in notification lists, compared with
489 * your app's {@link #PRIORITY_DEFAULT} items.
490 */
491 public static final int PRIORITY_HIGH = 1;
492
493 /**
494 * Highest {@link #priority}, for your application's most important items that require the
495 * user's prompt attention or input.
496 */
497 public static final int PRIORITY_MAX = 2;
498
499 /**
500 * Relative priority for this notification.
Joe Malin8d40d042012-11-05 11:36:40 -0800501 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500502 * Priority is an indication of how much of the user's valuable attention should be consumed by
503 * this notification. Low-priority notifications may be hidden from the user in certain
504 * situations, while the user might be interrupted for a higher-priority notification. The
Daniel Sandler6738eee2012-11-16 12:03:32 -0500505 * system will make a determination about how to interpret this priority when presenting
506 * the notification.
Chris Wren47c20a12014-06-18 17:27:29 -0400507 *
508 * <p>
509 * A notification that is at least {@link #PRIORITY_HIGH} is more likely to be presented
510 * as a heads-up notification.
511 * </p>
512 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500513 */
Tor Norbyed9273d62013-05-30 15:59:53 -0700514 @Priority
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500515 public int priority;
Joe Malin8d40d042012-11-05 11:36:40 -0800516
Dan Sandler26e81cf2014-05-06 10:01:27 -0400517 /**
518 * Accent color (an ARGB integer like the constants in {@link android.graphics.Color})
519 * to be applied by the standard Style templates when presenting this notification.
520 *
521 * The current template design constructs a colorful header image by overlaying the
522 * {@link #icon} image (stenciled in white) atop a field of this color. Alpha components are
523 * ignored.
524 */
Tor Norbye80756e32015-03-02 09:39:27 -0800525 @ColorInt
Dan Sandler26e81cf2014-05-06 10:01:27 -0400526 public int color = COLOR_DEFAULT;
527
528 /**
529 * Special value of {@link #color} telling the system not to decorate this notification with
530 * any special color but instead use default colors when presenting this notification.
531 */
Tor Norbye80756e32015-03-02 09:39:27 -0800532 @ColorInt
Dan Sandler26e81cf2014-05-06 10:01:27 -0400533 public static final int COLOR_DEFAULT = 0; // AKA Color.TRANSPARENT
Dan Sandler0bf2ed82013-12-21 23:33:41 -0600534
535 /**
536 * Sphere of visibility of this notification, which affects how and when the SystemUI reveals
537 * the notification's presence and contents in untrusted situations (namely, on the secure
538 * lockscreen).
539 *
540 * The default level, {@link #VISIBILITY_PRIVATE}, behaves exactly as notifications have always
541 * done on Android: The notification's {@link #icon} and {@link #tickerText} (if available) are
542 * shown in all situations, but the contents are only available if the device is unlocked for
543 * the appropriate user.
544 *
545 * A more permissive policy can be expressed by {@link #VISIBILITY_PUBLIC}; such a notification
546 * can be read even in an "insecure" context (that is, above a secure lockscreen).
547 * To modify the public version of this notification—for example, to redact some portions—see
548 * {@link Builder#setPublicVersion(Notification)}.
549 *
550 * Finally, a notification can be made {@link #VISIBILITY_SECRET}, which will suppress its icon
551 * and ticker until the user has bypassed the lockscreen.
552 */
553 public int visibility;
554
Griff Hazenfc3922d2014-08-20 11:56:44 -0700555 /**
556 * Notification visibility: Show this notification in its entirety on all lockscreens.
557 *
558 * {@see #visibility}
559 */
Dan Sandler0bf2ed82013-12-21 23:33:41 -0600560 public static final int VISIBILITY_PUBLIC = 1;
Griff Hazenfc3922d2014-08-20 11:56:44 -0700561
562 /**
563 * Notification visibility: Show this notification on all lockscreens, but conceal sensitive or
564 * private information on secure lockscreens.
565 *
566 * {@see #visibility}
567 */
Dan Sandler0bf2ed82013-12-21 23:33:41 -0600568 public static final int VISIBILITY_PRIVATE = 0;
Griff Hazenfc3922d2014-08-20 11:56:44 -0700569
570 /**
571 * Notification visibility: Do not reveal any part of this notification on a secure lockscreen.
572 *
573 * {@see #visibility}
574 */
Dan Sandler0bf2ed82013-12-21 23:33:41 -0600575 public static final int VISIBILITY_SECRET = -1;
576
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500577 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400578 * Notification category: incoming call (voice or video) or similar synchronous communication request.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500579 */
John Spurlockfd7f1e02014-03-18 16:41:57 -0400580 public static final String CATEGORY_CALL = "call";
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500581
582 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400583 * Notification category: incoming direct message (SMS, instant message, etc.).
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500584 */
John Spurlockfd7f1e02014-03-18 16:41:57 -0400585 public static final String CATEGORY_MESSAGE = "msg";
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500586
587 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400588 * Notification category: asynchronous bulk message (email).
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500589 */
John Spurlockfd7f1e02014-03-18 16:41:57 -0400590 public static final String CATEGORY_EMAIL = "email";
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500591
592 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400593 * Notification category: calendar event.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500594 */
John Spurlockfd7f1e02014-03-18 16:41:57 -0400595 public static final String CATEGORY_EVENT = "event";
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500596
597 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400598 * Notification category: promotion or advertisement.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500599 */
John Spurlockfd7f1e02014-03-18 16:41:57 -0400600 public static final String CATEGORY_PROMO = "promo";
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500601
602 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400603 * Notification category: alarm or timer.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500604 */
John Spurlockfd7f1e02014-03-18 16:41:57 -0400605 public static final String CATEGORY_ALARM = "alarm";
606
607 /**
608 * Notification category: progress of a long-running background operation.
609 */
610 public static final String CATEGORY_PROGRESS = "progress";
611
612 /**
613 * Notification category: social network or sharing update.
614 */
615 public static final String CATEGORY_SOCIAL = "social";
616
617 /**
618 * Notification category: error in background operation or authentication status.
619 */
620 public static final String CATEGORY_ERROR = "err";
621
622 /**
623 * Notification category: media transport control for playback.
624 */
625 public static final String CATEGORY_TRANSPORT = "transport";
626
627 /**
628 * Notification category: system or device status update. Reserved for system use.
629 */
630 public static final String CATEGORY_SYSTEM = "sys";
631
632 /**
633 * Notification category: indication of running background service.
634 */
635 public static final String CATEGORY_SERVICE = "service";
636
637 /**
John Spurlock0a69c8c2014-03-21 13:30:57 -0400638 * Notification category: a specific, timely recommendation for a single thing.
639 * For example, a news app might want to recommend a news story it believes the user will
640 * want to read next.
641 */
642 public static final String CATEGORY_RECOMMENDATION = "recommendation";
643
644 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400645 * Notification category: ongoing information about device or contextual status.
646 */
647 public static final String CATEGORY_STATUS = "status";
648
649 /**
John Spurlock24d3dad2015-04-02 12:24:02 -0400650 * Notification category: user-scheduled reminder.
651 */
652 public static final String CATEGORY_REMINDER = "reminder";
653
654 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400655 * One of the predefined notification categories (see the <code>CATEGORY_*</code> constants)
656 * that best describes this Notification. May be used by the system for ranking and filtering.
657 */
658 public String category;
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500659
Griff Hazen5cadc3b2014-05-20 09:55:39 -0700660 private String mGroupKey;
661
662 /**
663 * Get the key used to group this notification into a cluster or stack
664 * with other notifications on devices which support such rendering.
665 */
666 public String getGroup() {
667 return mGroupKey;
668 }
669
670 private String mSortKey;
671
672 /**
673 * Get a sort key that orders this notification among other notifications from the
674 * same package. This can be useful if an external sort was already applied and an app
675 * would like to preserve this. Notifications will be sorted lexicographically using this
676 * value, although providing different priorities in addition to providing sort key may
677 * cause this value to be ignored.
678 *
679 * <p>This sort key can also be used to order members of a notification group. See
680 * {@link Builder#setGroup}.
681 *
682 * @see String#compareTo(String)
683 */
684 public String getSortKey() {
685 return mSortKey;
686 }
687
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500688 /**
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400689 * Additional semantic data to be carried around with this Notification.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400690 * <p>
691 * The extras keys defined here are intended to capture the original inputs to {@link Builder}
692 * APIs, and are intended to be used by
693 * {@link android.service.notification.NotificationListenerService} implementations to extract
694 * detailed information from notification objects.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500695 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400696 public Bundle extras = new Bundle();
697
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400698 /**
699 * {@link #extras} key: this is the title of the notification,
700 * as supplied to {@link Builder#setContentTitle(CharSequence)}.
701 */
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -0500702 public static final String EXTRA_TITLE = "android.title";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400703
704 /**
705 * {@link #extras} key: this is the title of the notification when shown in expanded form,
706 * e.g. as supplied to {@link BigTextStyle#setBigContentTitle(CharSequence)}.
707 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400708 public static final String EXTRA_TITLE_BIG = EXTRA_TITLE + ".big";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400709
710 /**
711 * {@link #extras} key: this is the main text payload, as supplied to
712 * {@link Builder#setContentText(CharSequence)}.
713 */
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -0500714 public static final String EXTRA_TEXT = "android.text";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400715
716 /**
717 * {@link #extras} key: this is a third line of text, as supplied to
718 * {@link Builder#setSubText(CharSequence)}.
719 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400720 public static final String EXTRA_SUB_TEXT = "android.subText";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400721
722 /**
723 * {@link #extras} key: this is a small piece of additional text as supplied to
724 * {@link Builder#setContentInfo(CharSequence)}.
725 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400726 public static final String EXTRA_INFO_TEXT = "android.infoText";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400727
728 /**
729 * {@link #extras} key: this is a line of summary information intended to be shown
730 * alongside expanded notifications, as supplied to (e.g.)
731 * {@link BigTextStyle#setSummaryText(CharSequence)}.
732 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400733 public static final String EXTRA_SUMMARY_TEXT = "android.summaryText";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400734
735 /**
Christoph Studer4600f9b2014-07-22 22:44:43 +0200736 * {@link #extras} key: this is the longer text shown in the big form of a
737 * {@link BigTextStyle} notification, as supplied to
738 * {@link BigTextStyle#bigText(CharSequence)}.
739 */
740 public static final String EXTRA_BIG_TEXT = "android.bigText";
741
742 /**
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400743 * {@link #extras} key: this is the resource ID of the notification's main small icon, as
744 * supplied to {@link Builder#setSmallIcon(int)}.
745 */
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -0500746 public static final String EXTRA_SMALL_ICON = "android.icon";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400747
748 /**
749 * {@link #extras} key: this is a bitmap to be used instead of the small icon when showing the
750 * notification payload, as
751 * supplied to {@link Builder#setLargeIcon(android.graphics.Bitmap)}.
752 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400753 public static final String EXTRA_LARGE_ICON = "android.largeIcon";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400754
755 /**
756 * {@link #extras} key: this is a bitmap to be used instead of the one from
757 * {@link Builder#setLargeIcon(android.graphics.Bitmap)} when the notification is
758 * shown in its expanded form, as supplied to
759 * {@link BigPictureStyle#bigLargeIcon(android.graphics.Bitmap)}.
760 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400761 public static final String EXTRA_LARGE_ICON_BIG = EXTRA_LARGE_ICON + ".big";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400762
763 /**
764 * {@link #extras} key: this is the progress value supplied to
765 * {@link Builder#setProgress(int, int, boolean)}.
766 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400767 public static final String EXTRA_PROGRESS = "android.progress";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400768
769 /**
770 * {@link #extras} key: this is the maximum value supplied to
771 * {@link Builder#setProgress(int, int, boolean)}.
772 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400773 public static final String EXTRA_PROGRESS_MAX = "android.progressMax";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400774
775 /**
776 * {@link #extras} key: whether the progress bar is indeterminate, supplied to
777 * {@link Builder#setProgress(int, int, boolean)}.
778 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400779 public static final String EXTRA_PROGRESS_INDETERMINATE = "android.progressIndeterminate";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400780
781 /**
782 * {@link #extras} key: whether {@link #when} should be shown as a count-up timer (specifically
783 * a {@link android.widget.Chronometer}) instead of a timestamp, as supplied to
784 * {@link Builder#setUsesChronometer(boolean)}.
785 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400786 public static final String EXTRA_SHOW_CHRONOMETER = "android.showChronometer";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400787
788 /**
789 * {@link #extras} key: whether {@link #when} should be shown,
790 * as supplied to {@link Builder#setShowWhen(boolean)}.
791 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400792 public static final String EXTRA_SHOW_WHEN = "android.showWhen";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400793
794 /**
795 * {@link #extras} key: this is a bitmap to be shown in {@link BigPictureStyle} expanded
796 * notifications, supplied to {@link BigPictureStyle#bigPicture(android.graphics.Bitmap)}.
797 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400798 public static final String EXTRA_PICTURE = "android.picture";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400799
800 /**
801 * {@link #extras} key: An array of CharSequences to show in {@link InboxStyle} expanded
802 * notifications, each of which was supplied to {@link InboxStyle#addLine(CharSequence)}.
803 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400804 public static final String EXTRA_TEXT_LINES = "android.textLines";
Dan Sandler842dd772014-05-15 09:36:47 -0400805
806 /**
807 * {@link #extras} key: A string representing the name of the specific
808 * {@link android.app.Notification.Style} used to create this notification.
809 */
Chris Wren91ad5632013-06-05 15:05:57 -0400810 public static final String EXTRA_TEMPLATE = "android.template";
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400811
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400812 /**
Chris Wrene6c48932014-09-29 17:19:27 -0400813 * {@link #extras} key: A String array containing the people that this notification relates to,
814 * each of which was supplied to {@link Builder#addPerson(String)}.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400815 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400816 public static final String EXTRA_PEOPLE = "android.people";
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500817
818 /**
Chris Wren47c20a12014-06-18 17:27:29 -0400819 * {@link #extras} key: used to provide hints about the appropriateness of
820 * displaying this notification as a heads-up notification.
Chris Wren51c75102013-07-16 20:49:17 -0400821 * @hide
822 */
823 public static final String EXTRA_AS_HEADS_UP = "headsup";
824
825 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400826 * Allow certain system-generated notifications to appear before the device is provisioned.
827 * Only available to notifications coming from the android package.
828 * @hide
829 */
830 public static final String EXTRA_ALLOW_DURING_SETUP = "android.allowDuringSetup";
831
832 /**
Jose Limae9e3b3b2014-05-18 23:44:50 -0700833 * {@link #extras} key: A
834 * {@link android.content.ContentUris content URI} pointing to an image that can be displayed
835 * in the background when the notification is selected. The URI must point to an image stream
836 * suitable for passing into
837 * {@link android.graphics.BitmapFactory#decodeStream(java.io.InputStream)
838 * BitmapFactory.decodeStream}; all other content types will be ignored. The content provider
839 * URI used for this purpose must require no permissions to read the image data.
840 */
841 public static final String EXTRA_BACKGROUND_IMAGE_URI = "android.backgroundImageUri";
842
843 /**
Dan Sandler842dd772014-05-15 09:36:47 -0400844 * {@link #extras} key: A
Jeff Browndba34ba2014-06-24 20:46:03 -0700845 * {@link android.media.session.MediaSession.Token} associated with a
Dan Sandler842dd772014-05-15 09:36:47 -0400846 * {@link android.app.Notification.MediaStyle} notification.
847 */
848 public static final String EXTRA_MEDIA_SESSION = "android.mediaSession";
849
850 /**
Bryan Mawhinneye191f902014-07-22 12:50:09 +0100851 * {@link #extras} key: the indices of actions to be shown in the compact view,
852 * as supplied to (e.g.) {@link MediaStyle#setShowActionsInCompactView(int...)}.
853 */
854 public static final String EXTRA_COMPACT_ACTIONS = "android.compactActions";
855
Christoph Studer943aa672014-08-03 20:31:16 +0200856 /**
Kenny Guy8942bcd2014-09-08 21:09:47 +0100857 * {@link #extras} key: the user that built the notification.
858 *
859 * @hide
860 */
861 public static final String EXTRA_ORIGINATING_USERID = "android.originatingUserId";
862
863 /**
Chris Wren47c20a12014-06-18 17:27:29 -0400864 * Value for {@link #EXTRA_AS_HEADS_UP} that indicates this notification should not be
865 * displayed in the heads up space.
866 *
867 * <p>
868 * If this notification has a {@link #fullScreenIntent}, then it will always launch the
869 * full-screen intent when posted.
870 * </p>
Chris Wren51c75102013-07-16 20:49:17 -0400871 * @hide
872 */
873 public static final int HEADS_UP_NEVER = 0;
874
875 /**
Chris Wren47c20a12014-06-18 17:27:29 -0400876 * Default value for {@link #EXTRA_AS_HEADS_UP} that indicates this notification may be
877 * displayed as a heads up.
Chris Wren51c75102013-07-16 20:49:17 -0400878 * @hide
879 */
880 public static final int HEADS_UP_ALLOWED = 1;
881
882 /**
Chris Wren47c20a12014-06-18 17:27:29 -0400883 * Value for {@link #EXTRA_AS_HEADS_UP} that indicates this notification is a
884 * good candidate for display as a heads up.
Chris Wren51c75102013-07-16 20:49:17 -0400885 * @hide
886 */
887 public static final int HEADS_UP_REQUESTED = 2;
888
Dan Sandlerd63f9322015-05-06 15:18:49 -0400889 private Icon mSmallIcon;
890 private Icon mLargeIcon;
891
Chris Wren51c75102013-07-16 20:49:17 -0400892 /**
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400893 * Structure to encapsulate a named action that can be shown as part of this notification.
894 * It must include an icon, a label, and a {@link PendingIntent} to be fired when the action is
895 * selected by the user.
896 * <p>
Griff Hazen959591e2014-05-15 22:26:18 -0700897 * Apps should use {@link Notification.Builder#addAction(int, CharSequence, PendingIntent)}
898 * or {@link Notification.Builder#addAction(Notification.Action)}
899 * to attach actions.
Daniel Sandlera0a938c2012-03-15 08:42:37 -0400900 */
Daniel Sandlerea2a3172013-02-20 22:24:20 -0500901 public static class Action implements Parcelable {
Griff Hazen959591e2014-05-15 22:26:18 -0700902 private final Bundle mExtras;
Griff Hazen61a9e862014-05-22 16:05:19 -0700903 private final RemoteInput[] mRemoteInputs;
Griff Hazen959591e2014-05-15 22:26:18 -0700904
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400905 /**
906 * Small icon representing the action.
907 */
Daniel Sandlera0a938c2012-03-15 08:42:37 -0400908 public int icon;
Griff Hazen5cadc3b2014-05-20 09:55:39 -0700909
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400910 /**
911 * Title of the action.
912 */
Daniel Sandlera0a938c2012-03-15 08:42:37 -0400913 public CharSequence title;
Griff Hazen5cadc3b2014-05-20 09:55:39 -0700914
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400915 /**
916 * Intent to send when the user invokes this action. May be null, in which case the action
917 * may be rendered in a disabled presentation by the system UI.
918 */
Daniel Sandlera0a938c2012-03-15 08:42:37 -0400919 public PendingIntent actionIntent;
Griff Hazen959591e2014-05-15 22:26:18 -0700920
Daniel Sandlera0a938c2012-03-15 08:42:37 -0400921 private Action(Parcel in) {
922 icon = in.readInt();
923 title = TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(in);
924 if (in.readInt() == 1) {
925 actionIntent = PendingIntent.CREATOR.createFromParcel(in);
926 }
Griff Hazen959591e2014-05-15 22:26:18 -0700927 mExtras = in.readBundle();
Griff Hazen5cadc3b2014-05-20 09:55:39 -0700928 mRemoteInputs = in.createTypedArray(RemoteInput.CREATOR);
Daniel Sandlera0a938c2012-03-15 08:42:37 -0400929 }
Griff Hazen5cadc3b2014-05-20 09:55:39 -0700930
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400931 /**
Griff Hazen959591e2014-05-15 22:26:18 -0700932 * Use {@link Notification.Builder#addAction(int, CharSequence, PendingIntent)}.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400933 */
934 public Action(int icon, CharSequence title, PendingIntent intent) {
Griff Hazen5cadc3b2014-05-20 09:55:39 -0700935 this(icon, title, intent, new Bundle(), null);
Griff Hazen959591e2014-05-15 22:26:18 -0700936 }
937
Griff Hazen5cadc3b2014-05-20 09:55:39 -0700938 private Action(int icon, CharSequence title, PendingIntent intent, Bundle extras,
939 RemoteInput[] remoteInputs) {
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400940 this.icon = icon;
941 this.title = title;
942 this.actionIntent = intent;
Griff Hazen959591e2014-05-15 22:26:18 -0700943 this.mExtras = extras != null ? extras : new Bundle();
Griff Hazen5cadc3b2014-05-20 09:55:39 -0700944 this.mRemoteInputs = remoteInputs;
Griff Hazen959591e2014-05-15 22:26:18 -0700945 }
946
947 /**
948 * Get additional metadata carried around with this Action.
949 */
950 public Bundle getExtras() {
951 return mExtras;
952 }
953
954 /**
Griff Hazen5cadc3b2014-05-20 09:55:39 -0700955 * Get the list of inputs to be collected from the user when this action is sent.
956 * May return null if no remote inputs were added.
957 */
958 public RemoteInput[] getRemoteInputs() {
959 return mRemoteInputs;
960 }
961
962 /**
Griff Hazen959591e2014-05-15 22:26:18 -0700963 * Builder class for {@link Action} objects.
964 */
Griff Hazen5cadc3b2014-05-20 09:55:39 -0700965 public static final class Builder {
Griff Hazen959591e2014-05-15 22:26:18 -0700966 private final int mIcon;
967 private final CharSequence mTitle;
968 private final PendingIntent mIntent;
969 private final Bundle mExtras;
Griff Hazen5cadc3b2014-05-20 09:55:39 -0700970 private ArrayList<RemoteInput> mRemoteInputs;
Griff Hazen959591e2014-05-15 22:26:18 -0700971
972 /**
973 * Construct a new builder for {@link Action} object.
974 * @param icon icon to show for this action
975 * @param title the title of the action
976 * @param intent the {@link PendingIntent} to fire when users trigger this action
977 */
978 public Builder(int icon, CharSequence title, PendingIntent intent) {
Griff Hazen5cadc3b2014-05-20 09:55:39 -0700979 this(icon, title, intent, new Bundle(), null);
Griff Hazen959591e2014-05-15 22:26:18 -0700980 }
981
982 /**
983 * Construct a new builder for {@link Action} object using the fields from an
984 * {@link Action}.
985 * @param action the action to read fields from.
986 */
987 public Builder(Action action) {
Griff Hazen5cadc3b2014-05-20 09:55:39 -0700988 this(action.icon, action.title, action.actionIntent, new Bundle(action.mExtras),
989 action.getRemoteInputs());
Griff Hazen959591e2014-05-15 22:26:18 -0700990 }
991
Griff Hazen5cadc3b2014-05-20 09:55:39 -0700992 private Builder(int icon, CharSequence title, PendingIntent intent, Bundle extras,
993 RemoteInput[] remoteInputs) {
Griff Hazen959591e2014-05-15 22:26:18 -0700994 mIcon = icon;
995 mTitle = title;
996 mIntent = intent;
997 mExtras = extras;
Griff Hazen5cadc3b2014-05-20 09:55:39 -0700998 if (remoteInputs != null) {
999 mRemoteInputs = new ArrayList<RemoteInput>(remoteInputs.length);
1000 Collections.addAll(mRemoteInputs, remoteInputs);
1001 }
Griff Hazen959591e2014-05-15 22:26:18 -07001002 }
1003
1004 /**
1005 * Merge additional metadata into this builder.
1006 *
1007 * <p>Values within the Bundle will replace existing extras values in this Builder.
1008 *
1009 * @see Notification.Action#extras
1010 */
1011 public Builder addExtras(Bundle extras) {
1012 if (extras != null) {
1013 mExtras.putAll(extras);
1014 }
1015 return this;
1016 }
1017
1018 /**
1019 * Get the metadata Bundle used by this Builder.
1020 *
1021 * <p>The returned Bundle is shared with this Builder.
1022 */
1023 public Bundle getExtras() {
1024 return mExtras;
1025 }
1026
1027 /**
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001028 * Add an input to be collected from the user when this action is sent.
1029 * Response values can be retrieved from the fired intent by using the
1030 * {@link RemoteInput#getResultsFromIntent} function.
1031 * @param remoteInput a {@link RemoteInput} to add to the action
1032 * @return this object for method chaining
1033 */
1034 public Builder addRemoteInput(RemoteInput remoteInput) {
1035 if (mRemoteInputs == null) {
1036 mRemoteInputs = new ArrayList<RemoteInput>();
1037 }
1038 mRemoteInputs.add(remoteInput);
1039 return this;
1040 }
1041
1042 /**
1043 * Apply an extender to this action builder. Extenders may be used to add
1044 * metadata or change options on this builder.
1045 */
Griff Hazen61a9e862014-05-22 16:05:19 -07001046 public Builder extend(Extender extender) {
1047 extender.extend(this);
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001048 return this;
1049 }
1050
1051 /**
Griff Hazen959591e2014-05-15 22:26:18 -07001052 * Combine all of the options that have been set and return a new {@link Action}
1053 * object.
1054 * @return the built action
1055 */
1056 public Action build() {
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001057 RemoteInput[] remoteInputs = mRemoteInputs != null
1058 ? mRemoteInputs.toArray(new RemoteInput[mRemoteInputs.size()]) : null;
1059 return new Action(mIcon, mTitle, mIntent, mExtras, remoteInputs);
Griff Hazen959591e2014-05-15 22:26:18 -07001060 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001061 }
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001062
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001063 @Override
1064 public Action clone() {
1065 return new Action(
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001066 icon,
1067 title,
1068 actionIntent, // safe to alias
1069 new Bundle(mExtras),
1070 getRemoteInputs());
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001071 }
1072 @Override
1073 public int describeContents() {
1074 return 0;
1075 }
1076 @Override
1077 public void writeToParcel(Parcel out, int flags) {
1078 out.writeInt(icon);
1079 TextUtils.writeToParcel(title, out, flags);
1080 if (actionIntent != null) {
1081 out.writeInt(1);
1082 actionIntent.writeToParcel(out, flags);
1083 } else {
1084 out.writeInt(0);
1085 }
Griff Hazen959591e2014-05-15 22:26:18 -07001086 out.writeBundle(mExtras);
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001087 out.writeTypedArray(mRemoteInputs, flags);
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001088 }
Griff Hazen959591e2014-05-15 22:26:18 -07001089 public static final Parcelable.Creator<Action> CREATOR =
1090 new Parcelable.Creator<Action>() {
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001091 public Action createFromParcel(Parcel in) {
1092 return new Action(in);
1093 }
1094 public Action[] newArray(int size) {
1095 return new Action[size];
1096 }
1097 };
Griff Hazen61a9e862014-05-22 16:05:19 -07001098
1099 /**
1100 * Extender interface for use with {@link Builder#extend}. Extenders may be used to add
1101 * metadata or change options on an action builder.
1102 */
1103 public interface Extender {
1104 /**
1105 * Apply this extender to a notification action builder.
1106 * @param builder the builder to be modified.
1107 * @return the build object for chaining.
1108 */
1109 public Builder extend(Builder builder);
1110 }
1111
1112 /**
1113 * Wearable extender for notification actions. To add extensions to an action,
1114 * create a new {@link android.app.Notification.Action.WearableExtender} object using
1115 * the {@code WearableExtender()} constructor and apply it to a
1116 * {@link android.app.Notification.Action.Builder} using
1117 * {@link android.app.Notification.Action.Builder#extend}.
1118 *
1119 * <pre class="prettyprint">
1120 * Notification.Action action = new Notification.Action.Builder(
1121 * R.drawable.archive_all, "Archive all", actionIntent)
Griff Hazen14f57992014-05-26 09:07:14 -07001122 * .extend(new Notification.Action.WearableExtender()
Griff Hazen61a9e862014-05-22 16:05:19 -07001123 * .setAvailableOffline(false))
Griff Hazen14f57992014-05-26 09:07:14 -07001124 * .build();</pre>
Griff Hazen61a9e862014-05-22 16:05:19 -07001125 */
1126 public static final class WearableExtender implements Extender {
1127 /** Notification action extra which contains wearable extensions */
1128 private static final String EXTRA_WEARABLE_EXTENSIONS = "android.wearable.EXTENSIONS";
1129
Pete Gastaf6781d2014-10-07 15:17:05 -04001130 // Keys within EXTRA_WEARABLE_EXTENSIONS for wearable options.
Griff Hazen61a9e862014-05-22 16:05:19 -07001131 private static final String KEY_FLAGS = "flags";
Pete Gastaf6781d2014-10-07 15:17:05 -04001132 private static final String KEY_IN_PROGRESS_LABEL = "inProgressLabel";
1133 private static final String KEY_CONFIRM_LABEL = "confirmLabel";
1134 private static final String KEY_CANCEL_LABEL = "cancelLabel";
Griff Hazen61a9e862014-05-22 16:05:19 -07001135
1136 // Flags bitwise-ored to mFlags
1137 private static final int FLAG_AVAILABLE_OFFLINE = 0x1;
1138
1139 // Default value for flags integer
1140 private static final int DEFAULT_FLAGS = FLAG_AVAILABLE_OFFLINE;
1141
1142 private int mFlags = DEFAULT_FLAGS;
1143
Pete Gastaf6781d2014-10-07 15:17:05 -04001144 private CharSequence mInProgressLabel;
1145 private CharSequence mConfirmLabel;
1146 private CharSequence mCancelLabel;
1147
Griff Hazen61a9e862014-05-22 16:05:19 -07001148 /**
1149 * Create a {@link android.app.Notification.Action.WearableExtender} with default
1150 * options.
1151 */
1152 public WearableExtender() {
1153 }
1154
1155 /**
1156 * Create a {@link android.app.Notification.Action.WearableExtender} by reading
1157 * wearable options present in an existing notification action.
1158 * @param action the notification action to inspect.
1159 */
1160 public WearableExtender(Action action) {
1161 Bundle wearableBundle = action.getExtras().getBundle(EXTRA_WEARABLE_EXTENSIONS);
1162 if (wearableBundle != null) {
1163 mFlags = wearableBundle.getInt(KEY_FLAGS, DEFAULT_FLAGS);
Pete Gastaf6781d2014-10-07 15:17:05 -04001164 mInProgressLabel = wearableBundle.getCharSequence(KEY_IN_PROGRESS_LABEL);
1165 mConfirmLabel = wearableBundle.getCharSequence(KEY_CONFIRM_LABEL);
1166 mCancelLabel = wearableBundle.getCharSequence(KEY_CANCEL_LABEL);
Griff Hazen61a9e862014-05-22 16:05:19 -07001167 }
1168 }
1169
1170 /**
1171 * Apply wearable extensions to a notification action that is being built. This is
1172 * typically called by the {@link android.app.Notification.Action.Builder#extend}
1173 * method of {@link android.app.Notification.Action.Builder}.
1174 */
1175 @Override
1176 public Action.Builder extend(Action.Builder builder) {
1177 Bundle wearableBundle = new Bundle();
1178
1179 if (mFlags != DEFAULT_FLAGS) {
1180 wearableBundle.putInt(KEY_FLAGS, mFlags);
1181 }
Pete Gastaf6781d2014-10-07 15:17:05 -04001182 if (mInProgressLabel != null) {
1183 wearableBundle.putCharSequence(KEY_IN_PROGRESS_LABEL, mInProgressLabel);
1184 }
1185 if (mConfirmLabel != null) {
1186 wearableBundle.putCharSequence(KEY_CONFIRM_LABEL, mConfirmLabel);
1187 }
1188 if (mCancelLabel != null) {
1189 wearableBundle.putCharSequence(KEY_CANCEL_LABEL, mCancelLabel);
1190 }
Griff Hazen61a9e862014-05-22 16:05:19 -07001191
1192 builder.getExtras().putBundle(EXTRA_WEARABLE_EXTENSIONS, wearableBundle);
1193 return builder;
1194 }
1195
1196 @Override
1197 public WearableExtender clone() {
1198 WearableExtender that = new WearableExtender();
1199 that.mFlags = this.mFlags;
Pete Gastaf6781d2014-10-07 15:17:05 -04001200 that.mInProgressLabel = this.mInProgressLabel;
1201 that.mConfirmLabel = this.mConfirmLabel;
1202 that.mCancelLabel = this.mCancelLabel;
Griff Hazen61a9e862014-05-22 16:05:19 -07001203 return that;
1204 }
1205
1206 /**
1207 * Set whether this action is available when the wearable device is not connected to
1208 * a companion device. The user can still trigger this action when the wearable device is
1209 * offline, but a visual hint will indicate that the action may not be available.
1210 * Defaults to true.
1211 */
1212 public WearableExtender setAvailableOffline(boolean availableOffline) {
1213 setFlag(FLAG_AVAILABLE_OFFLINE, availableOffline);
1214 return this;
1215 }
1216
1217 /**
1218 * Get whether this action is available when the wearable device is not connected to
1219 * a companion device. The user can still trigger this action when the wearable device is
1220 * offline, but a visual hint will indicate that the action may not be available.
1221 * Defaults to true.
1222 */
1223 public boolean isAvailableOffline() {
1224 return (mFlags & FLAG_AVAILABLE_OFFLINE) != 0;
1225 }
1226
1227 private void setFlag(int mask, boolean value) {
1228 if (value) {
1229 mFlags |= mask;
1230 } else {
1231 mFlags &= ~mask;
1232 }
1233 }
Pete Gastaf6781d2014-10-07 15:17:05 -04001234
1235 /**
1236 * Set a label to display while the wearable is preparing to automatically execute the
1237 * action. This is usually a 'ing' verb ending in ellipsis like "Sending..."
1238 *
1239 * @param label the label to display while the action is being prepared to execute
1240 * @return this object for method chaining
1241 */
1242 public WearableExtender setInProgressLabel(CharSequence label) {
1243 mInProgressLabel = label;
1244 return this;
1245 }
1246
1247 /**
1248 * Get the label to display while the wearable is preparing to automatically execute
1249 * the action. This is usually a 'ing' verb ending in ellipsis like "Sending..."
1250 *
1251 * @return the label to display while the action is being prepared to execute
1252 */
1253 public CharSequence getInProgressLabel() {
1254 return mInProgressLabel;
1255 }
1256
1257 /**
1258 * Set a label to display to confirm that the action should be executed.
1259 * This is usually an imperative verb like "Send".
1260 *
1261 * @param label the label to confirm the action should be executed
1262 * @return this object for method chaining
1263 */
1264 public WearableExtender setConfirmLabel(CharSequence label) {
1265 mConfirmLabel = label;
1266 return this;
1267 }
1268
1269 /**
1270 * Get the label to display to confirm that the action should be executed.
1271 * This is usually an imperative verb like "Send".
1272 *
1273 * @return the label to confirm the action should be executed
1274 */
1275 public CharSequence getConfirmLabel() {
1276 return mConfirmLabel;
1277 }
1278
1279 /**
1280 * Set a label to display to cancel the action.
1281 * This is usually an imperative verb, like "Cancel".
1282 *
1283 * @param label the label to display to cancel the action
1284 * @return this object for method chaining
1285 */
1286 public WearableExtender setCancelLabel(CharSequence label) {
1287 mCancelLabel = label;
1288 return this;
1289 }
1290
1291 /**
1292 * Get the label to display to cancel the action.
1293 * This is usually an imperative verb like "Cancel".
1294 *
1295 * @return the label to display to cancel the action
1296 */
1297 public CharSequence getCancelLabel() {
1298 return mCancelLabel;
1299 }
Griff Hazen61a9e862014-05-22 16:05:19 -07001300 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001301 }
1302
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001303 /**
1304 * Array of all {@link Action} structures attached to this notification by
1305 * {@link Builder#addAction(int, CharSequence, PendingIntent)}. Mostly useful for instances of
1306 * {@link android.service.notification.NotificationListenerService} that provide an alternative
1307 * interface for invoking actions.
1308 */
Daniel Sandlerea2a3172013-02-20 22:24:20 -05001309 public Action[] actions;
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001310
1311 /**
Dan Sandler0bf2ed82013-12-21 23:33:41 -06001312 * Replacement version of this notification whose content will be shown
1313 * in an insecure context such as atop a secure keyguard. See {@link #visibility}
1314 * and {@link #VISIBILITY_PUBLIC}.
1315 */
1316 public Notification publicVersion;
1317
1318 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001319 * Constructs a Notification object with default values.
Joe Onorato46439ce2010-11-19 13:56:21 -08001320 * You might want to consider using {@link Builder} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001321 */
1322 public Notification()
1323 {
1324 this.when = System.currentTimeMillis();
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001325 this.priority = PRIORITY_DEFAULT;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001326 }
1327
1328 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001329 * @hide
1330 */
1331 public Notification(Context context, int icon, CharSequence tickerText, long when,
1332 CharSequence contentTitle, CharSequence contentText, Intent contentIntent)
1333 {
Chris Wren1ce4b6d2015-06-11 10:19:43 -04001334 new Builder(context)
1335 .setWhen(when)
1336 .setSmallIcon(icon)
1337 .setTicker(tickerText)
1338 .setContentTitle(contentTitle)
1339 .setContentText(contentText)
1340 .setContentIntent(PendingIntent.getActivity(context, 0, contentIntent, 0))
1341 .buildInto(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001342 }
1343
1344 /**
1345 * Constructs a Notification object with the information needed to
1346 * have a status bar icon without the standard expanded view.
1347 *
1348 * @param icon The resource id of the icon to put in the status bar.
1349 * @param tickerText The text that flows by in the status bar when the notification first
1350 * activates.
1351 * @param when The time to show in the time field. In the System.currentTimeMillis
1352 * timebase.
Joe Onorato46439ce2010-11-19 13:56:21 -08001353 *
1354 * @deprecated Use {@link Builder} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001355 */
Joe Onorato46439ce2010-11-19 13:56:21 -08001356 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001357 public Notification(int icon, CharSequence tickerText, long when)
1358 {
1359 this.icon = icon;
1360 this.tickerText = tickerText;
1361 this.when = when;
1362 }
1363
1364 /**
1365 * Unflatten the notification from a parcel.
1366 */
1367 public Notification(Parcel parcel)
1368 {
1369 int version = parcel.readInt();
1370
1371 when = parcel.readLong();
Dan Sandler3936e7a2015-05-19 20:59:12 -04001372 if (parcel.readInt() != 0) {
1373 mSmallIcon = Icon.CREATOR.createFromParcel(parcel);
Dan Sandler4e787062015-06-17 15:09:48 -04001374 if (mSmallIcon.getType() == Icon.TYPE_RESOURCE) {
1375 icon = mSmallIcon.getResId();
1376 }
Dan Sandler3936e7a2015-05-19 20:59:12 -04001377 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001378 number = parcel.readInt();
1379 if (parcel.readInt() != 0) {
1380 contentIntent = PendingIntent.CREATOR.createFromParcel(parcel);
1381 }
1382 if (parcel.readInt() != 0) {
1383 deleteIntent = PendingIntent.CREATOR.createFromParcel(parcel);
1384 }
1385 if (parcel.readInt() != 0) {
1386 tickerText = TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(parcel);
1387 }
1388 if (parcel.readInt() != 0) {
Joe Onorato46439ce2010-11-19 13:56:21 -08001389 tickerView = RemoteViews.CREATOR.createFromParcel(parcel);
Joe Onoratoef1e7762010-09-17 18:38:38 -04001390 }
1391 if (parcel.readInt() != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001392 contentView = RemoteViews.CREATOR.createFromParcel(parcel);
1393 }
Joe Onorato561d3852010-11-20 18:09:34 -08001394 if (parcel.readInt() != 0) {
Dan Sandlerd63f9322015-05-06 15:18:49 -04001395 mLargeIcon = Icon.CREATOR.createFromParcel(parcel);
Joe Onorato561d3852010-11-20 18:09:34 -08001396 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001397 defaults = parcel.readInt();
1398 flags = parcel.readInt();
1399 if (parcel.readInt() != 0) {
1400 sound = Uri.CREATOR.createFromParcel(parcel);
1401 }
1402
1403 audioStreamType = parcel.readInt();
John Spurlockc0650f022014-07-19 13:22:39 -04001404 if (parcel.readInt() != 0) {
1405 audioAttributes = AudioAttributes.CREATOR.createFromParcel(parcel);
1406 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001407 vibrate = parcel.createLongArray();
1408 ledARGB = parcel.readInt();
1409 ledOnMS = parcel.readInt();
1410 ledOffMS = parcel.readInt();
1411 iconLevel = parcel.readInt();
Daniel Sandlere46cbd32010-06-17 10:35:26 -04001412
1413 if (parcel.readInt() != 0) {
1414 fullScreenIntent = PendingIntent.CREATOR.createFromParcel(parcel);
1415 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001416
1417 priority = parcel.readInt();
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001418
John Spurlockfd7f1e02014-03-18 16:41:57 -04001419 category = parcel.readString();
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001420
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001421 mGroupKey = parcel.readString();
1422
1423 mSortKey = parcel.readString();
1424
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001425 extras = parcel.readBundle(); // may be null
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001426
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001427 actions = parcel.createTypedArray(Action.CREATOR); // may be null
1428
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001429 if (parcel.readInt() != 0) {
Daniel Sandlerf3b73432012-03-27 15:01:25 -04001430 bigContentView = RemoteViews.CREATOR.createFromParcel(parcel);
1431 }
Dan Sandler0bf2ed82013-12-21 23:33:41 -06001432
Chris Wren8fd39ec2014-02-27 17:43:26 -05001433 if (parcel.readInt() != 0) {
1434 headsUpContentView = RemoteViews.CREATOR.createFromParcel(parcel);
1435 }
1436
Dan Sandler0bf2ed82013-12-21 23:33:41 -06001437 visibility = parcel.readInt();
1438
1439 if (parcel.readInt() != 0) {
1440 publicVersion = Notification.CREATOR.createFromParcel(parcel);
1441 }
Dan Sandler26e81cf2014-05-06 10:01:27 -04001442
1443 color = parcel.readInt();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001444 }
1445
Andy Stadler110988c2010-12-03 14:29:16 -08001446 @Override
Joe Onorato18e69df2010-05-17 22:26:12 -07001447 public Notification clone() {
1448 Notification that = new Notification();
Daniel Sandler1a497d32013-04-18 14:52:45 -04001449 cloneInto(that, true);
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001450 return that;
1451 }
Joe Onorato18e69df2010-05-17 22:26:12 -07001452
Daniel Sandler1a497d32013-04-18 14:52:45 -04001453 /**
1454 * Copy all (or if heavy is false, all except Bitmaps and RemoteViews) members
1455 * of this into that.
1456 * @hide
1457 */
1458 public void cloneInto(Notification that, boolean heavy) {
Joe Onorato18e69df2010-05-17 22:26:12 -07001459 that.when = this.when;
Dan Sandlerd63f9322015-05-06 15:18:49 -04001460 that.mSmallIcon = this.mSmallIcon;
Joe Onorato18e69df2010-05-17 22:26:12 -07001461 that.number = this.number;
1462
1463 // PendingIntents are global, so there's no reason (or way) to clone them.
1464 that.contentIntent = this.contentIntent;
1465 that.deleteIntent = this.deleteIntent;
Daniel Sandlere46cbd32010-06-17 10:35:26 -04001466 that.fullScreenIntent = this.fullScreenIntent;
Joe Onorato18e69df2010-05-17 22:26:12 -07001467
1468 if (this.tickerText != null) {
1469 that.tickerText = this.tickerText.toString();
1470 }
Daniel Sandler1a497d32013-04-18 14:52:45 -04001471 if (heavy && this.tickerView != null) {
Joe Onorato46439ce2010-11-19 13:56:21 -08001472 that.tickerView = this.tickerView.clone();
Joe Onoratoef1e7762010-09-17 18:38:38 -04001473 }
Daniel Sandler1a497d32013-04-18 14:52:45 -04001474 if (heavy && this.contentView != null) {
Joe Onorato18e69df2010-05-17 22:26:12 -07001475 that.contentView = this.contentView.clone();
1476 }
Dan Sandlerd63f9322015-05-06 15:18:49 -04001477 if (heavy && this.mLargeIcon != null) {
1478 that.mLargeIcon = this.mLargeIcon;
Joe Onorato561d3852010-11-20 18:09:34 -08001479 }
Jozef BABJAKa8b91832011-02-22 08:05:08 +01001480 that.iconLevel = this.iconLevel;
Joe Onorato18e69df2010-05-17 22:26:12 -07001481 that.sound = this.sound; // android.net.Uri is immutable
1482 that.audioStreamType = this.audioStreamType;
John Spurlockc0650f022014-07-19 13:22:39 -04001483 if (this.audioAttributes != null) {
1484 that.audioAttributes = new AudioAttributes.Builder(this.audioAttributes).build();
1485 }
Joe Onorato18e69df2010-05-17 22:26:12 -07001486
1487 final long[] vibrate = this.vibrate;
1488 if (vibrate != null) {
1489 final int N = vibrate.length;
1490 final long[] vib = that.vibrate = new long[N];
1491 System.arraycopy(vibrate, 0, vib, 0, N);
1492 }
1493
1494 that.ledARGB = this.ledARGB;
1495 that.ledOnMS = this.ledOnMS;
1496 that.ledOffMS = this.ledOffMS;
1497 that.defaults = this.defaults;
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001498
Joe Onorato18e69df2010-05-17 22:26:12 -07001499 that.flags = this.flags;
1500
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001501 that.priority = this.priority;
Joe Malin8d40d042012-11-05 11:36:40 -08001502
John Spurlockfd7f1e02014-03-18 16:41:57 -04001503 that.category = this.category;
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001504
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001505 that.mGroupKey = this.mGroupKey;
1506
1507 that.mSortKey = this.mSortKey;
1508
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001509 if (this.extras != null) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04001510 try {
1511 that.extras = new Bundle(this.extras);
1512 // will unparcel
1513 that.extras.size();
1514 } catch (BadParcelableException e) {
1515 Log.e(TAG, "could not unparcel extras from notification: " + this, e);
1516 that.extras = null;
1517 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001518 }
1519
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001520 if (this.actions != null) {
1521 that.actions = new Action[this.actions.length];
1522 for(int i=0; i<this.actions.length; i++) {
1523 that.actions[i] = this.actions[i].clone();
1524 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001525 }
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001526
Daniel Sandler1a497d32013-04-18 14:52:45 -04001527 if (heavy && this.bigContentView != null) {
Daniel Sandlerf3b73432012-03-27 15:01:25 -04001528 that.bigContentView = this.bigContentView.clone();
1529 }
Daniel Sandler1a497d32013-04-18 14:52:45 -04001530
Chris Wren8fd39ec2014-02-27 17:43:26 -05001531 if (heavy && this.headsUpContentView != null) {
1532 that.headsUpContentView = this.headsUpContentView.clone();
1533 }
1534
Dan Sandler0bf2ed82013-12-21 23:33:41 -06001535 that.visibility = this.visibility;
1536
1537 if (this.publicVersion != null) {
1538 that.publicVersion = new Notification();
1539 this.publicVersion.cloneInto(that.publicVersion, heavy);
1540 }
1541
Dan Sandler26e81cf2014-05-06 10:01:27 -04001542 that.color = this.color;
1543
Daniel Sandler1a497d32013-04-18 14:52:45 -04001544 if (!heavy) {
1545 that.lightenPayload(); // will clean out extras
1546 }
1547 }
1548
1549 /**
1550 * Removes heavyweight parts of the Notification object for archival or for sending to
1551 * listeners when the full contents are not necessary.
1552 * @hide
1553 */
1554 public final void lightenPayload() {
1555 tickerView = null;
1556 contentView = null;
1557 bigContentView = null;
Chris Wren8fd39ec2014-02-27 17:43:26 -05001558 headsUpContentView = null;
Dan Sandlerd63f9322015-05-06 15:18:49 -04001559 mLargeIcon = null;
Daniel Sandler1a497d32013-04-18 14:52:45 -04001560 if (extras != null) {
1561 extras.remove(Notification.EXTRA_LARGE_ICON);
1562 extras.remove(Notification.EXTRA_LARGE_ICON_BIG);
1563 extras.remove(Notification.EXTRA_PICTURE);
Christoph Studer223f44e2014-09-02 14:59:32 +02001564 extras.remove(Notification.EXTRA_BIG_TEXT);
Christoph Studerb82bc782014-08-20 14:29:43 +02001565 // Prevent light notifications from being rebuilt.
1566 extras.remove(Builder.EXTRA_NEEDS_REBUILD);
Daniel Sandler1a497d32013-04-18 14:52:45 -04001567 }
Joe Onorato18e69df2010-05-17 22:26:12 -07001568 }
1569
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04001570 /**
1571 * Make sure this CharSequence is safe to put into a bundle, which basically
1572 * means it had better not be some custom Parcelable implementation.
1573 * @hide
1574 */
1575 public static CharSequence safeCharSequence(CharSequence cs) {
Christoph Studer535ec612014-09-03 15:47:47 +02001576 if (cs == null) return cs;
1577 if (cs.length() > MAX_CHARSEQUENCE_LENGTH) {
1578 cs = cs.subSequence(0, MAX_CHARSEQUENCE_LENGTH);
1579 }
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04001580 if (cs instanceof Parcelable) {
1581 Log.e(TAG, "warning: " + cs.getClass().getCanonicalName()
1582 + " instance is a custom Parcelable and not allowed in Notification");
1583 return cs.toString();
1584 }
1585
1586 return cs;
1587 }
1588
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001589 public int describeContents() {
1590 return 0;
1591 }
1592
1593 /**
Dan Sandler4e787062015-06-17 15:09:48 -04001594 * Flatten this notification into a parcel.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001595 */
1596 public void writeToParcel(Parcel parcel, int flags)
1597 {
1598 parcel.writeInt(1);
1599
1600 parcel.writeLong(when);
Dan Sandler4e787062015-06-17 15:09:48 -04001601 if (mSmallIcon == null && icon != 0) {
1602 // you snuck an icon in here without using the builder; let's try to keep it
1603 mSmallIcon = Icon.createWithResource("", icon);
1604 }
Dan Sandler3936e7a2015-05-19 20:59:12 -04001605 if (mSmallIcon != null) {
1606 parcel.writeInt(1);
1607 mSmallIcon.writeToParcel(parcel, 0);
1608 } else {
1609 parcel.writeInt(0);
1610 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001611 parcel.writeInt(number);
1612 if (contentIntent != null) {
1613 parcel.writeInt(1);
1614 contentIntent.writeToParcel(parcel, 0);
1615 } else {
1616 parcel.writeInt(0);
1617 }
1618 if (deleteIntent != null) {
1619 parcel.writeInt(1);
1620 deleteIntent.writeToParcel(parcel, 0);
1621 } else {
1622 parcel.writeInt(0);
1623 }
1624 if (tickerText != null) {
1625 parcel.writeInt(1);
1626 TextUtils.writeToParcel(tickerText, parcel, flags);
1627 } else {
1628 parcel.writeInt(0);
1629 }
Joe Onorato46439ce2010-11-19 13:56:21 -08001630 if (tickerView != null) {
Joe Onoratoef1e7762010-09-17 18:38:38 -04001631 parcel.writeInt(1);
Joe Onorato46439ce2010-11-19 13:56:21 -08001632 tickerView.writeToParcel(parcel, 0);
Joe Onoratoef1e7762010-09-17 18:38:38 -04001633 } else {
1634 parcel.writeInt(0);
1635 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001636 if (contentView != null) {
1637 parcel.writeInt(1);
1638 contentView.writeToParcel(parcel, 0);
1639 } else {
1640 parcel.writeInt(0);
1641 }
Dan Sandlerd63f9322015-05-06 15:18:49 -04001642 if (mLargeIcon != null) {
Joe Onorato561d3852010-11-20 18:09:34 -08001643 parcel.writeInt(1);
Dan Sandlerd63f9322015-05-06 15:18:49 -04001644 mLargeIcon.writeToParcel(parcel, 0);
Joe Onorato561d3852010-11-20 18:09:34 -08001645 } else {
1646 parcel.writeInt(0);
1647 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001648
1649 parcel.writeInt(defaults);
1650 parcel.writeInt(this.flags);
1651
1652 if (sound != null) {
1653 parcel.writeInt(1);
1654 sound.writeToParcel(parcel, 0);
1655 } else {
1656 parcel.writeInt(0);
1657 }
1658 parcel.writeInt(audioStreamType);
John Spurlockc0650f022014-07-19 13:22:39 -04001659
1660 if (audioAttributes != null) {
1661 parcel.writeInt(1);
1662 audioAttributes.writeToParcel(parcel, 0);
1663 } else {
1664 parcel.writeInt(0);
1665 }
1666
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001667 parcel.writeLongArray(vibrate);
1668 parcel.writeInt(ledARGB);
1669 parcel.writeInt(ledOnMS);
1670 parcel.writeInt(ledOffMS);
1671 parcel.writeInt(iconLevel);
Daniel Sandlere46cbd32010-06-17 10:35:26 -04001672
1673 if (fullScreenIntent != null) {
1674 parcel.writeInt(1);
1675 fullScreenIntent.writeToParcel(parcel, 0);
1676 } else {
1677 parcel.writeInt(0);
1678 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001679
1680 parcel.writeInt(priority);
Joe Malin8d40d042012-11-05 11:36:40 -08001681
John Spurlockfd7f1e02014-03-18 16:41:57 -04001682 parcel.writeString(category);
Joe Malin8d40d042012-11-05 11:36:40 -08001683
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001684 parcel.writeString(mGroupKey);
1685
1686 parcel.writeString(mSortKey);
1687
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001688 parcel.writeBundle(extras); // null ok
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001689
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001690 parcel.writeTypedArray(actions, 0); // null ok
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001691
Daniel Sandlerf3b73432012-03-27 15:01:25 -04001692 if (bigContentView != null) {
1693 parcel.writeInt(1);
1694 bigContentView.writeToParcel(parcel, 0);
1695 } else {
1696 parcel.writeInt(0);
1697 }
Dan Sandler0bf2ed82013-12-21 23:33:41 -06001698
Chris Wren8fd39ec2014-02-27 17:43:26 -05001699 if (headsUpContentView != null) {
1700 parcel.writeInt(1);
1701 headsUpContentView.writeToParcel(parcel, 0);
1702 } else {
1703 parcel.writeInt(0);
1704 }
1705
Dan Sandler0bf2ed82013-12-21 23:33:41 -06001706 parcel.writeInt(visibility);
1707
1708 if (publicVersion != null) {
1709 parcel.writeInt(1);
1710 publicVersion.writeToParcel(parcel, 0);
1711 } else {
1712 parcel.writeInt(0);
1713 }
Dan Sandler26e81cf2014-05-06 10:01:27 -04001714
1715 parcel.writeInt(color);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001716 }
1717
1718 /**
1719 * Parcelable.Creator that instantiates Notification objects
1720 */
1721 public static final Parcelable.Creator<Notification> CREATOR
1722 = new Parcelable.Creator<Notification>()
1723 {
1724 public Notification createFromParcel(Parcel parcel)
1725 {
1726 return new Notification(parcel);
1727 }
1728
1729 public Notification[] newArray(int size)
1730 {
1731 return new Notification[size];
1732 }
1733 };
1734
1735 /**
1736 * Sets the {@link #contentView} field to be a view with the standard "Latest Event"
1737 * layout.
1738 *
1739 * <p>Uses the {@link #icon} and {@link #when} fields to set the icon and time fields
1740 * in the view.</p>
1741 * @param context The context for your application / activity.
1742 * @param contentTitle The title that goes in the expanded entry.
1743 * @param contentText The text that goes in the expanded entry.
1744 * @param contentIntent The intent to launch when the user clicks the expanded notification.
1745 * If this is an activity, it must include the
1746 * {@link android.content.Intent#FLAG_ACTIVITY_NEW_TASK} flag, which requires
Scott Main7aee61f2011-02-08 11:25:01 -08001747 * that you take care of task management as described in the
1748 * <a href="{@docRoot}guide/topics/fundamentals/tasks-and-back-stack.html">Tasks and Back
1749 * Stack</a> document.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001750 *
Joe Onorato46439ce2010-11-19 13:56:21 -08001751 * @deprecated Use {@link Builder} instead.
Chris Wrena05db382015-06-24 15:18:34 -04001752 * @removed
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001753 */
Joe Onorato46439ce2010-11-19 13:56:21 -08001754 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001755 public void setLatestEventInfo(Context context,
1756 CharSequence contentTitle, CharSequence contentText, PendingIntent contentIntent) {
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001757 Notification.Builder builder = new Notification.Builder(context);
1758
1759 // First, ensure that key pieces of information that may have been set directly
1760 // are preserved
1761 builder.setWhen(this.when);
1762 builder.setSmallIcon(this.icon);
1763 builder.setPriority(this.priority);
1764 builder.setTicker(this.tickerText);
1765 builder.setNumber(this.number);
Selim Cinek255dd042014-08-19 22:29:02 +02001766 builder.setColor(this.color);
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001767 builder.mFlags = this.flags;
1768 builder.setSound(this.sound, this.audioStreamType);
1769 builder.setDefaults(this.defaults);
1770 builder.setVibrate(this.vibrate);
Sean Barbeau6ae0b1b2014-07-19 21:31:41 -07001771 builder.setDeleteIntent(this.deleteIntent);
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001772
1773 // now apply the latestEventInfo fields
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001774 if (contentTitle != null) {
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001775 builder.setContentTitle(contentTitle);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001776 }
1777 if (contentText != null) {
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001778 builder.setContentText(contentText);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001779 }
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001780 builder.setContentIntent(contentIntent);
1781 builder.buildInto(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001782 }
1783
1784 @Override
1785 public String toString() {
1786 StringBuilder sb = new StringBuilder();
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001787 sb.append("Notification(pri=");
1788 sb.append(priority);
1789 sb.append(" contentView=");
Joe Onoratoc9596d62011-01-12 17:03:11 -08001790 if (contentView != null) {
1791 sb.append(contentView.getPackage());
1792 sb.append("/0x");
1793 sb.append(Integer.toHexString(contentView.getLayoutId()));
1794 } else {
1795 sb.append("null");
1796 }
1797 sb.append(" vibrate=");
Daniel Sandler6738eee2012-11-16 12:03:32 -05001798 if ((this.defaults & DEFAULT_VIBRATE) != 0) {
1799 sb.append("default");
1800 } else if (this.vibrate != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001801 int N = this.vibrate.length-1;
1802 sb.append("[");
1803 for (int i=0; i<N; i++) {
1804 sb.append(this.vibrate[i]);
1805 sb.append(',');
1806 }
Simon Schoar8cf97d92009-06-10 22:08:37 +02001807 if (N != -1) {
1808 sb.append(this.vibrate[N]);
1809 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001810 sb.append("]");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001811 } else {
1812 sb.append("null");
1813 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001814 sb.append(" sound=");
Daniel Sandler6738eee2012-11-16 12:03:32 -05001815 if ((this.defaults & DEFAULT_SOUND) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001816 sb.append("default");
Daniel Sandler6738eee2012-11-16 12:03:32 -05001817 } else if (this.sound != null) {
1818 sb.append(this.sound.toString());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001819 } else {
1820 sb.append("null");
1821 }
Chris Wren365b6d32015-07-16 10:39:26 -04001822 if (this.tickerText != null) {
1823 sb.append(" tick");
1824 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001825 sb.append(" defaults=0x");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001826 sb.append(Integer.toHexString(this.defaults));
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001827 sb.append(" flags=0x");
Daniel Sandlere46cbd32010-06-17 10:35:26 -04001828 sb.append(Integer.toHexString(this.flags));
Dan Sandler26e81cf2014-05-06 10:01:27 -04001829 sb.append(String.format(" color=0x%08x", this.color));
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001830 if (this.category != null) {
1831 sb.append(" category=");
1832 sb.append(this.category);
1833 }
1834 if (this.mGroupKey != null) {
1835 sb.append(" groupKey=");
1836 sb.append(this.mGroupKey);
1837 }
1838 if (this.mSortKey != null) {
1839 sb.append(" sortKey=");
1840 sb.append(this.mSortKey);
1841 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001842 if (actions != null) {
Dan Sandler1b718782014-07-18 12:43:45 -04001843 sb.append(" actions=");
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001844 sb.append(actions.length);
Dan Sandler1b718782014-07-18 12:43:45 -04001845 }
1846 sb.append(" vis=");
1847 sb.append(visibilityToString(this.visibility));
1848 if (this.publicVersion != null) {
1849 sb.append(" publicVersion=");
1850 sb.append(publicVersion.toString());
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001851 }
1852 sb.append(")");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001853 return sb.toString();
1854 }
Joe Onorato46439ce2010-11-19 13:56:21 -08001855
Dan Sandler1b718782014-07-18 12:43:45 -04001856 /**
1857 * {@hide}
1858 */
1859 public static String visibilityToString(int vis) {
1860 switch (vis) {
1861 case VISIBILITY_PRIVATE:
1862 return "PRIVATE";
1863 case VISIBILITY_PUBLIC:
1864 return "PUBLIC";
1865 case VISIBILITY_SECRET:
1866 return "SECRET";
1867 default:
1868 return "UNKNOWN(" + String.valueOf(vis) + ")";
1869 }
1870 }
1871
Joe Onoratocb109a02011-01-18 17:57:41 -08001872 /**
John Spurlock1d881a12015-03-18 19:21:54 -04001873 * {@hide}
1874 */
1875 public static String priorityToString(@Priority int pri) {
1876 switch (pri) {
1877 case PRIORITY_MIN:
1878 return "MIN";
1879 case PRIORITY_LOW:
1880 return "LOW";
1881 case PRIORITY_DEFAULT:
1882 return "DEFAULT";
1883 case PRIORITY_HIGH:
1884 return "HIGH";
1885 case PRIORITY_MAX:
1886 return "MAX";
1887 default:
1888 return "UNKNOWN(" + String.valueOf(pri) + ")";
1889 }
1890 }
1891
1892 /**
Dan Sandlerd63f9322015-05-06 15:18:49 -04001893 * The small icon representing this notification in the status bar and content view.
1894 *
1895 * @return the small icon representing this notification.
1896 *
1897 * @see Builder#getSmallIcon()
1898 * @see Builder#setSmallIcon(Icon)
1899 */
1900 public Icon getSmallIcon() {
1901 return mSmallIcon;
1902 }
1903
1904 /**
1905 * Used when notifying to clean up legacy small icons.
1906 * @hide
1907 */
1908 public void setSmallIcon(Icon icon) {
1909 mSmallIcon = icon;
1910 }
1911
1912 /**
1913 * The large icon shown in this notification's content view.
1914 * @see Builder#getLargeIcon()
1915 * @see Builder#setLargeIcon(Icon)
1916 */
1917 public Icon getLargeIcon() {
1918 return mLargeIcon;
1919 }
1920
1921 /**
Christoph Studer4600f9b2014-07-22 22:44:43 +02001922 * @hide
1923 */
1924 public boolean isValid() {
1925 // Would like to check for icon!=0 here, too, but NotificationManagerService accepts that
1926 // for legacy reasons.
1927 return contentView != null || extras.getBoolean(Builder.EXTRA_REBUILD_CONTENT_VIEW);
1928 }
1929
1930 /**
Christoph Studerc8db24b2014-07-25 17:50:30 +02001931 * @hide
1932 */
1933 public boolean isGroupSummary() {
1934 return mGroupKey != null && (flags & FLAG_GROUP_SUMMARY) != 0;
1935 }
1936
1937 /**
1938 * @hide
1939 */
1940 public boolean isGroupChild() {
1941 return mGroupKey != null && (flags & FLAG_GROUP_SUMMARY) == 0;
1942 }
1943
1944 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001945 * Builder class for {@link Notification} objects.
Joe Malin8d40d042012-11-05 11:36:40 -08001946 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001947 * Provides a convenient way to set the various fields of a {@link Notification} and generate
Scott Main183bf112012-08-13 19:12:13 -07001948 * content views using the platform's notification layout template. If your app supports
1949 * versions of Android as old as API level 4, you can instead use
1950 * {@link android.support.v4.app.NotificationCompat.Builder NotificationCompat.Builder},
1951 * available in the <a href="{@docRoot}tools/extras/support-library.html">Android Support
1952 * library</a>.
Joe Malin8d40d042012-11-05 11:36:40 -08001953 *
Scott Main183bf112012-08-13 19:12:13 -07001954 * <p>Example:
Joe Malin8d40d042012-11-05 11:36:40 -08001955 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001956 * <pre class="prettyprint">
Scott Main183bf112012-08-13 19:12:13 -07001957 * Notification noti = new Notification.Builder(mContext)
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001958 * .setContentTitle(&quot;New mail from &quot; + sender.toString())
1959 * .setContentText(subject)
1960 * .setSmallIcon(R.drawable.new_mail)
1961 * .setLargeIcon(aBitmap)
Chris Wrenfbd96ba2012-05-01 12:03:58 -04001962 * .build();
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001963 * </pre>
Joe Onoratocb109a02011-01-18 17:57:41 -08001964 */
Joe Onorato46439ce2010-11-19 13:56:21 -08001965 public static class Builder {
Daniel Sandler602ad1c2012-06-12 16:06:27 -04001966 private static final int MAX_ACTION_BUTTONS = 3;
Jorim Jaggi445d3c02014-08-19 22:33:42 +02001967 private static final float LARGE_TEXT_SCALE = 1.3f;
Daniel Sandler8680bf82012-05-15 16:52:52 -04001968
Christoph Studer4600f9b2014-07-22 22:44:43 +02001969 /**
1970 * @hide
1971 */
1972 public static final String EXTRA_NEEDS_REBUILD = "android.rebuild";
1973
1974 /**
1975 * @hide
1976 */
1977 public static final String EXTRA_REBUILD_LARGE_ICON = "android.rebuild.largeIcon";
1978 /**
1979 * @hide
1980 */
1981 public static final String EXTRA_REBUILD_CONTENT_VIEW = "android.rebuild.contentView";
1982 /**
1983 * @hide
1984 */
1985 public static final String EXTRA_REBUILD_CONTENT_VIEW_ACTION_COUNT =
1986 "android.rebuild.contentViewActionCount";
1987 /**
1988 * @hide
1989 */
1990 public static final String EXTRA_REBUILD_BIG_CONTENT_VIEW
1991 = "android.rebuild.bigView";
1992 /**
1993 * @hide
1994 */
1995 public static final String EXTRA_REBUILD_BIG_CONTENT_VIEW_ACTION_COUNT
1996 = "android.rebuild.bigViewActionCount";
1997 /**
1998 * @hide
1999 */
2000 public static final String EXTRA_REBUILD_HEADS_UP_CONTENT_VIEW
2001 = "android.rebuild.hudView";
2002 /**
2003 * @hide
2004 */
2005 public static final String EXTRA_REBUILD_HEADS_UP_CONTENT_VIEW_ACTION_COUNT
2006 = "android.rebuild.hudViewActionCount";
2007
2008 /**
Kenny Guy77320062014-08-27 21:37:15 +01002009 * The ApplicationInfo of the package that created the notification, used to create
2010 * a context to rebuild the notification via a Builder.
2011 * @hide
Christoph Studer4600f9b2014-07-22 22:44:43 +02002012 */
Kenny Guy77320062014-08-27 21:37:15 +01002013 private static final String EXTRA_REBUILD_CONTEXT_APPLICATION_INFO =
2014 "android.rebuild.applicationInfo";
Christoph Studer4600f9b2014-07-22 22:44:43 +02002015
2016 // Whether to enable stripping (at post time) & rebuilding (at listener receive time) of
2017 // memory intensive resources.
2018 private static final boolean STRIP_AND_REBUILD = true;
2019
Joe Onorato46439ce2010-11-19 13:56:21 -08002020 private Context mContext;
2021
2022 private long mWhen;
Dan Sandlerd63f9322015-05-06 15:18:49 -04002023 private Icon mSmallIcon, mLargeIcon;
Joe Onorato46439ce2010-11-19 13:56:21 -08002024 private int mSmallIconLevel;
Joe Onorato8595a3d2010-11-19 18:12:07 -08002025 private int mNumber;
Joe Onorato46439ce2010-11-19 13:56:21 -08002026 private CharSequence mContentTitle;
2027 private CharSequence mContentText;
2028 private CharSequence mContentInfo;
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002029 private CharSequence mSubText;
Joe Onorato46439ce2010-11-19 13:56:21 -08002030 private PendingIntent mContentIntent;
2031 private RemoteViews mContentView;
2032 private PendingIntent mDeleteIntent;
2033 private PendingIntent mFullScreenIntent;
2034 private CharSequence mTickerText;
2035 private RemoteViews mTickerView;
Joe Onorato46439ce2010-11-19 13:56:21 -08002036 private Uri mSound;
2037 private int mAudioStreamType;
John Spurlockc0650f022014-07-19 13:22:39 -04002038 private AudioAttributes mAudioAttributes;
Joe Onorato46439ce2010-11-19 13:56:21 -08002039 private long[] mVibrate;
2040 private int mLedArgb;
2041 private int mLedOnMs;
2042 private int mLedOffMs;
2043 private int mDefaults;
2044 private int mFlags;
Jeff Sharkey1c400132011-08-05 14:50:13 -07002045 private int mProgressMax;
2046 private int mProgress;
2047 private boolean mProgressIndeterminate;
John Spurlockfd7f1e02014-03-18 16:41:57 -04002048 private String mCategory;
Griff Hazen5cadc3b2014-05-20 09:55:39 -07002049 private String mGroupKey;
2050 private String mSortKey;
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002051 private Bundle mExtras;
2052 private int mPriority;
Daniel Sandler8680bf82012-05-15 16:52:52 -04002053 private ArrayList<Action> mActions = new ArrayList<Action>(MAX_ACTION_BUTTONS);
Daniel Sandlera2985ed2012-04-03 16:42:00 -04002054 private boolean mUseChronometer;
Chris Wrenfbd96ba2012-05-01 12:03:58 -04002055 private Style mStyle;
Daniel Sandler0c890492012-09-12 17:23:10 -07002056 private boolean mShowWhen = true;
Dan Sandler0bf2ed82013-12-21 23:33:41 -06002057 private int mVisibility = VISIBILITY_PRIVATE;
2058 private Notification mPublicVersion = null;
Dan Sandler26e81cf2014-05-06 10:01:27 -04002059 private final NotificationColorUtil mColorUtil;
Chris Wrendde75302014-03-26 17:24:15 -04002060 private ArrayList<String> mPeople;
Dan Sandler26e81cf2014-05-06 10:01:27 -04002061 private int mColor = COLOR_DEFAULT;
Christoph Studer7ac80e62014-08-04 16:01:57 +02002062
2063 /**
Kenny Guy8942bcd2014-09-08 21:09:47 +01002064 * The user that built the notification originally.
2065 */
2066 private int mOriginatingUserId;
2067
2068 /**
Christoph Studer4600f9b2014-07-22 22:44:43 +02002069 * Contains extras related to rebuilding during the build phase.
2070 */
2071 private Bundle mRebuildBundle = new Bundle();
2072 /**
2073 * Contains the notification to rebuild when this Builder is in "rebuild" mode.
2074 * Null otherwise.
2075 */
2076 private Notification mRebuildNotification = null;
2077
Joe Onoratocb109a02011-01-18 17:57:41 -08002078 /**
Jorim Jaggid05aa3e2014-08-28 17:52:27 +02002079 * Whether the build notification has three lines. This is used to make the top padding for
2080 * both the contracted and expanded layout consistent.
2081 *
2082 * <p>
2083 * This field is only valid during the build phase.
2084 */
2085 private boolean mHasThreeLines;
2086
2087 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002088 * Constructs a new Builder with the defaults:
Joe Onoratocb109a02011-01-18 17:57:41 -08002089 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002090
2091 * <table>
2092 * <tr><th align=right>priority</th>
2093 * <td>{@link #PRIORITY_DEFAULT}</td></tr>
2094 * <tr><th align=right>when</th>
2095 * <td>now ({@link System#currentTimeMillis()})</td></tr>
2096 * <tr><th align=right>audio stream</th>
2097 * <td>{@link #STREAM_DEFAULT}</td></tr>
2098 * </table>
Joe Onoratocb109a02011-01-18 17:57:41 -08002099 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002100
2101 * @param context
2102 * A {@link Context} that will be used by the Builder to construct the
2103 * RemoteViews. The Context will not be held past the lifetime of this Builder
2104 * object.
Joe Onoratocb109a02011-01-18 17:57:41 -08002105 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002106 public Builder(Context context) {
Adam Powellbf06fa02014-05-30 12:32:18 -07002107 /*
2108 * Important compatibility note!
2109 * Some apps out in the wild create a Notification.Builder in their Activity subclass
2110 * constructor for later use. At this point Activities - themselves subclasses of
2111 * ContextWrapper - do not have their inner Context populated yet. This means that
2112 * any calls to Context methods from within this constructor can cause NPEs in existing
2113 * apps. Any data populated from mContext should therefore be populated lazily to
2114 * preserve compatibility.
2115 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002116 mContext = context;
Andy Stadler110988c2010-12-03 14:29:16 -08002117
2118 // Set defaults to match the defaults of a Notification
Joe Onorato46439ce2010-11-19 13:56:21 -08002119 mWhen = System.currentTimeMillis();
Andy Stadler110988c2010-12-03 14:29:16 -08002120 mAudioStreamType = STREAM_DEFAULT;
John Spurlockc0650f022014-07-19 13:22:39 -04002121 mAudioAttributes = AUDIO_ATTRIBUTES_DEFAULT;
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002122 mPriority = PRIORITY_DEFAULT;
Chris Wrendde75302014-03-26 17:24:15 -04002123 mPeople = new ArrayList<String>();
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01002124
Dianne Hackborn955d8d62014-10-07 20:17:19 -07002125 mColorUtil = context.getApplicationInfo().targetSdkVersion < Build.VERSION_CODES.LOLLIPOP ?
Dan Sandler05c362d2014-09-03 00:16:27 +02002126 NotificationColorUtil.getInstance(mContext) : null;
Joe Onorato46439ce2010-11-19 13:56:21 -08002127 }
2128
Joe Onoratocb109a02011-01-18 17:57:41 -08002129 /**
Christoph Studer4600f9b2014-07-22 22:44:43 +02002130 * Creates a Builder for rebuilding the given Notification.
2131 * <p>
2132 * Call {@link #rebuild()} to retrieve the rebuilt version of 'n'.
2133 */
2134 private Builder(Context context, Notification n) {
2135 this(context);
2136 mRebuildNotification = n;
2137 restoreFromNotification(n);
2138
2139 Style style = null;
2140 Bundle extras = n.extras;
2141 String templateClass = extras.getString(EXTRA_TEMPLATE);
2142 if (!TextUtils.isEmpty(templateClass)) {
2143 Class<? extends Style> styleClass = getNotificationStyleClass(templateClass);
2144 if (styleClass == null) {
2145 Log.d(TAG, "Unknown style class: " + styleClass);
2146 return;
2147 }
2148
2149 try {
2150 Constructor<? extends Style> constructor = styleClass.getConstructor();
Alan Viverette904de2e2015-05-04 10:32:57 -07002151 constructor.setAccessible(true);
Christoph Studer4600f9b2014-07-22 22:44:43 +02002152 style = constructor.newInstance();
2153 style.restoreFromExtras(extras);
2154 } catch (Throwable t) {
2155 Log.e(TAG, "Could not create Style", t);
2156 return;
2157 }
2158 }
2159 if (style != null) {
2160 setStyle(style);
2161 }
2162 }
2163
2164 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002165 * Add a timestamp pertaining to the notification (usually the time the event occurred).
Daniel Sandler0c890492012-09-12 17:23:10 -07002166 * It will be shown in the notification content view by default; use
Griff Hazen50c11652014-05-16 09:46:31 -07002167 * {@link #setShowWhen(boolean) setShowWhen} to control this.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002168 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002169 * @see Notification#when
Joe Onoratocb109a02011-01-18 17:57:41 -08002170 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002171 public Builder setWhen(long when) {
2172 mWhen = when;
2173 return this;
2174 }
2175
Joe Onoratocb109a02011-01-18 17:57:41 -08002176 /**
Griff Hazen50c11652014-05-16 09:46:31 -07002177 * Control whether the timestamp set with {@link #setWhen(long) setWhen} is shown
Daniel Sandler0c890492012-09-12 17:23:10 -07002178 * in the content view.
2179 */
2180 public Builder setShowWhen(boolean show) {
2181 mShowWhen = show;
2182 return this;
2183 }
2184
2185 /**
Daniel Sandlerd33b8032012-05-10 11:41:48 -04002186 * Show the {@link Notification#when} field as a stopwatch.
Joe Malin8d40d042012-11-05 11:36:40 -08002187 *
2188 * Instead of presenting <code>when</code> as a timestamp, the notification will show an
Daniel Sandlerd33b8032012-05-10 11:41:48 -04002189 * automatically updating display of the minutes and seconds since <code>when</code>.
Daniel Sandlera2985ed2012-04-03 16:42:00 -04002190 *
Daniel Sandlerd33b8032012-05-10 11:41:48 -04002191 * Useful when showing an elapsed time (like an ongoing phone call).
2192 *
2193 * @see android.widget.Chronometer
Daniel Sandlera2985ed2012-04-03 16:42:00 -04002194 * @see Notification#when
2195 */
2196 public Builder setUsesChronometer(boolean b) {
2197 mUseChronometer = b;
2198 return this;
2199 }
2200
2201 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002202 * Set the small icon resource, which will be used to represent the notification in the
2203 * status bar.
Joe Onoratocb109a02011-01-18 17:57:41 -08002204 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002205
2206 * The platform template for the expanded view will draw this icon in the left, unless a
2207 * {@link #setLargeIcon(Bitmap) large icon} has also been specified, in which case the small
2208 * icon will be moved to the right-hand side.
2209 *
2210
2211 * @param icon
2212 * A resource ID in the application's package of the drawable to use.
2213 * @see Notification#icon
Joe Onoratocb109a02011-01-18 17:57:41 -08002214 */
Tor Norbye7b9c9122013-05-30 16:48:33 -07002215 public Builder setSmallIcon(@DrawableRes int icon) {
Dan Sandlerd63f9322015-05-06 15:18:49 -04002216 return setSmallIcon(icon != 0
2217 ? Icon.createWithResource(mContext, icon)
2218 : null);
Joe Onorato46439ce2010-11-19 13:56:21 -08002219 }
2220
Joe Onoratocb109a02011-01-18 17:57:41 -08002221 /**
2222 * A variant of {@link #setSmallIcon(int) setSmallIcon(int)} that takes an additional
2223 * level parameter for when the icon is a {@link android.graphics.drawable.LevelListDrawable
2224 * LevelListDrawable}.
2225 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002226 * @param icon A resource ID in the application's package of the drawable to use.
Joe Onoratocb109a02011-01-18 17:57:41 -08002227 * @param level The level to use for the icon.
2228 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002229 * @see Notification#icon
2230 * @see Notification#iconLevel
Joe Onoratocb109a02011-01-18 17:57:41 -08002231 */
Tor Norbye7b9c9122013-05-30 16:48:33 -07002232 public Builder setSmallIcon(@DrawableRes int icon, int level) {
Daniel Sandler09e51a72015-05-14 15:32:28 +00002233 mSmallIconLevel = level;
Dan Sandlerd63f9322015-05-06 15:18:49 -04002234 return setSmallIcon(icon);
2235 }
2236
2237 /**
2238 * Set the small icon, which will be used to represent the notification in the
2239 * status bar and content view (unless overriden there by a
2240 * {@link #setLargeIcon(Bitmap) large icon}).
2241 *
2242 * @param icon An Icon object to use.
2243 * @see Notification#icon
2244 */
2245 public Builder setSmallIcon(Icon icon) {
2246 mSmallIcon = icon;
Joe Onorato46439ce2010-11-19 13:56:21 -08002247 return this;
2248 }
2249
Joe Onoratocb109a02011-01-18 17:57:41 -08002250 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002251 * Set the first line of text in the platform notification template.
Joe Onoratocb109a02011-01-18 17:57:41 -08002252 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002253 public Builder setContentTitle(CharSequence title) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04002254 mContentTitle = safeCharSequence(title);
Joe Onorato46439ce2010-11-19 13:56:21 -08002255 return this;
2256 }
2257
Joe Onoratocb109a02011-01-18 17:57:41 -08002258 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002259 * Set the second line of text in the platform notification template.
Joe Onoratocb109a02011-01-18 17:57:41 -08002260 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002261 public Builder setContentText(CharSequence text) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04002262 mContentText = safeCharSequence(text);
Joe Onorato46439ce2010-11-19 13:56:21 -08002263 return this;
2264 }
2265
Joe Onoratocb109a02011-01-18 17:57:41 -08002266 /**
Joe Malin8d40d042012-11-05 11:36:40 -08002267 * Set the third line of text in the platform notification template.
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04002268 * Don't use if you're also using {@link #setProgress(int, int, boolean)}; they occupy the
2269 * same location in the standard template.
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002270 */
2271 public Builder setSubText(CharSequence text) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04002272 mSubText = safeCharSequence(text);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002273 return this;
2274 }
2275
2276 /**
Joe Onoratocb109a02011-01-18 17:57:41 -08002277 * Set the large number at the right-hand side of the notification. This is
2278 * equivalent to setContentInfo, although it might show the number in a different
2279 * font size for readability.
2280 */
Joe Onorato8595a3d2010-11-19 18:12:07 -08002281 public Builder setNumber(int number) {
2282 mNumber = number;
2283 return this;
2284 }
2285
Joe Onoratocb109a02011-01-18 17:57:41 -08002286 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002287 * A small piece of additional information pertaining to this notification.
2288 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002289 * The platform template will draw this on the last line of the notification, at the far
2290 * right (to the right of a smallIcon if it has been placed there).
Joe Onoratocb109a02011-01-18 17:57:41 -08002291 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002292 public Builder setContentInfo(CharSequence info) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04002293 mContentInfo = safeCharSequence(info);
Joe Onorato46439ce2010-11-19 13:56:21 -08002294 return this;
2295 }
2296
Joe Onoratocb109a02011-01-18 17:57:41 -08002297 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002298 * Set the progress this notification represents.
2299 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002300 * The platform template will represent this using a {@link ProgressBar}.
Jeff Sharkey1c400132011-08-05 14:50:13 -07002301 */
2302 public Builder setProgress(int max, int progress, boolean indeterminate) {
2303 mProgressMax = max;
2304 mProgress = progress;
2305 mProgressIndeterminate = indeterminate;
2306 return this;
2307 }
2308
2309 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002310 * Supply a custom RemoteViews to use instead of the platform template.
2311 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002312 * @see Notification#contentView
Joe Onoratocb109a02011-01-18 17:57:41 -08002313 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002314 public Builder setContent(RemoteViews views) {
2315 mContentView = views;
2316 return this;
2317 }
2318
Joe Onoratocb109a02011-01-18 17:57:41 -08002319 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002320 * Supply a {@link PendingIntent} to be sent when the notification is clicked.
2321 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002322 * As of {@link android.os.Build.VERSION_CODES#HONEYCOMB}, if this field is unset and you
2323 * have specified a custom RemoteViews with {@link #setContent(RemoteViews)}, you can use
2324 * {@link RemoteViews#setOnClickPendingIntent RemoteViews.setOnClickPendingIntent(int,PendingIntent)}
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002325 * to assign PendingIntents to individual views in that custom layout (i.e., to create
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002326 * clickable buttons inside the notification view).
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002327 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002328 * @see Notification#contentIntent Notification.contentIntent
Joe Onoratocb109a02011-01-18 17:57:41 -08002329 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002330 public Builder setContentIntent(PendingIntent intent) {
2331 mContentIntent = intent;
2332 return this;
2333 }
2334
Joe Onoratocb109a02011-01-18 17:57:41 -08002335 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002336 * Supply a {@link PendingIntent} to send when the notification is cleared explicitly by the user.
2337 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002338 * @see Notification#deleteIntent
Joe Onoratocb109a02011-01-18 17:57:41 -08002339 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002340 public Builder setDeleteIntent(PendingIntent intent) {
2341 mDeleteIntent = intent;
2342 return this;
2343 }
2344
Joe Onoratocb109a02011-01-18 17:57:41 -08002345 /**
2346 * An intent to launch instead of posting the notification to the status bar.
2347 * Only for use with extremely high-priority notifications demanding the user's
2348 * <strong>immediate</strong> attention, such as an incoming phone call or
2349 * alarm clock that the user has explicitly set to a particular time.
2350 * If this facility is used for something else, please give the user an option
2351 * to turn it off and use a normal notification, as this can be extremely
2352 * disruptive.
2353 *
Chris Wren47c20a12014-06-18 17:27:29 -04002354 * <p>
2355 * The system UI may choose to display a heads-up notification, instead of
2356 * launching this intent, while the user is using the device.
2357 * </p>
2358 *
Joe Onoratocb109a02011-01-18 17:57:41 -08002359 * @param intent The pending intent to launch.
2360 * @param highPriority Passing true will cause this notification to be sent
2361 * even if other notifications are suppressed.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002362 *
2363 * @see Notification#fullScreenIntent
Joe Onoratocb109a02011-01-18 17:57:41 -08002364 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002365 public Builder setFullScreenIntent(PendingIntent intent, boolean highPriority) {
2366 mFullScreenIntent = intent;
2367 setFlag(FLAG_HIGH_PRIORITY, highPriority);
2368 return this;
2369 }
2370
Joe Onoratocb109a02011-01-18 17:57:41 -08002371 /**
Dan Sandler5fcdf6e2014-07-18 11:31:15 -04002372 * Set the "ticker" text which is sent to accessibility services.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002373 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002374 * @see Notification#tickerText
Joe Onoratocb109a02011-01-18 17:57:41 -08002375 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002376 public Builder setTicker(CharSequence tickerText) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04002377 mTickerText = safeCharSequence(tickerText);
Joe Onorato46439ce2010-11-19 13:56:21 -08002378 return this;
2379 }
2380
Joe Onoratocb109a02011-01-18 17:57:41 -08002381 /**
Dan Sandler5fcdf6e2014-07-18 11:31:15 -04002382 * Obsolete version of {@link #setTicker(CharSequence)}.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002383 *
Joe Onoratocb109a02011-01-18 17:57:41 -08002384 */
Dan Sandler5fcdf6e2014-07-18 11:31:15 -04002385 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -08002386 public Builder setTicker(CharSequence tickerText, RemoteViews views) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04002387 mTickerText = safeCharSequence(tickerText);
Dan Sandler5fcdf6e2014-07-18 11:31:15 -04002388 mTickerView = views; // we'll save it for you anyway
Joe Onorato46439ce2010-11-19 13:56:21 -08002389 return this;
2390 }
2391
Joe Onoratocb109a02011-01-18 17:57:41 -08002392 /**
Dan Sandlerd63f9322015-05-06 15:18:49 -04002393 * Add a large icon to the notification content view.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002394 *
2395 * In the platform template, this image will be shown on the left of the notification view
Dan Sandlerd63f9322015-05-06 15:18:49 -04002396 * in place of the {@link #setSmallIcon(Icon) small icon} (which will be placed in a small
2397 * badge atop the large icon).
Dan Sandler08a04c12015-05-06 15:18:49 -04002398 */
Dan Sandlerd63f9322015-05-06 15:18:49 -04002399 public Builder setLargeIcon(Bitmap b) {
2400 return setLargeIcon(b != null ? Icon.createWithBitmap(b) : null);
2401 }
2402
2403 /**
2404 * Add a large icon to the notification content view.
2405 *
2406 * In the platform template, this image will be shown on the left of the notification view
2407 * in place of the {@link #setSmallIcon(Icon) small icon} (which will be placed in a small
2408 * badge atop the large icon).
2409 */
2410 public Builder setLargeIcon(Icon icon) {
Joe Onorato46439ce2010-11-19 13:56:21 -08002411 mLargeIcon = icon;
2412 return this;
2413 }
2414
Joe Onoratocb109a02011-01-18 17:57:41 -08002415 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002416 * Set the sound to play.
2417 *
John Spurlockc0650f022014-07-19 13:22:39 -04002418 * It will be played using the {@link #AUDIO_ATTRIBUTES_DEFAULT default audio attributes}
2419 * for notifications.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002420 *
Chris Wren47c20a12014-06-18 17:27:29 -04002421 * <p>
2422 * A notification that is noisy is more likely to be presented as a heads-up notification.
2423 * </p>
2424 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002425 * @see Notification#sound
Joe Onoratocb109a02011-01-18 17:57:41 -08002426 */
Joe Onorato52f80cd2010-11-21 15:34:48 -08002427 public Builder setSound(Uri sound) {
2428 mSound = sound;
John Spurlockc0650f022014-07-19 13:22:39 -04002429 mAudioAttributes = AUDIO_ATTRIBUTES_DEFAULT;
Joe Onorato52f80cd2010-11-21 15:34:48 -08002430 return this;
2431 }
2432
Joe Onoratocb109a02011-01-18 17:57:41 -08002433 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002434 * Set the sound to play, along with a specific stream on which to play it.
Joe Onoratocb109a02011-01-18 17:57:41 -08002435 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002436 * See {@link android.media.AudioManager} for the <code>STREAM_</code> constants.
2437 *
Chris Wren47c20a12014-06-18 17:27:29 -04002438 * <p>
2439 * A notification that is noisy is more likely to be presented as a heads-up notification.
2440 * </p>
John Spurlockc0650f022014-07-19 13:22:39 -04002441 * @deprecated use {@link #setSound(Uri, AudioAttributes)} instead.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002442 * @see Notification#sound
Joe Onoratocb109a02011-01-18 17:57:41 -08002443 */
Jean-Michel Trivi81f871e2014-08-06 16:32:38 -07002444 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -08002445 public Builder setSound(Uri sound, int streamType) {
2446 mSound = sound;
2447 mAudioStreamType = streamType;
2448 return this;
2449 }
2450
Joe Onoratocb109a02011-01-18 17:57:41 -08002451 /**
John Spurlockc0650f022014-07-19 13:22:39 -04002452 * Set the sound to play, along with specific {@link AudioAttributes audio attributes} to
2453 * use during playback.
2454 *
2455 * <p>
2456 * A notification that is noisy is more likely to be presented as a heads-up notification.
2457 * </p>
2458 *
2459 * @see Notification#sound
2460 */
2461 public Builder setSound(Uri sound, AudioAttributes audioAttributes) {
2462 mSound = sound;
2463 mAudioAttributes = audioAttributes;
2464 return this;
2465 }
2466
2467 /**
Joe Onoratocb109a02011-01-18 17:57:41 -08002468 * Set the vibration pattern to use.
2469 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002470 * See {@link android.os.Vibrator#vibrate(long[], int)} for a discussion of the
2471 * <code>pattern</code> parameter.
2472 *
Chris Wren47c20a12014-06-18 17:27:29 -04002473 * <p>
2474 * A notification that vibrates is more likely to be presented as a heads-up notification.
2475 * </p>
2476 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002477 * @see Notification#vibrate
Joe Onoratocb109a02011-01-18 17:57:41 -08002478 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002479 public Builder setVibrate(long[] pattern) {
2480 mVibrate = pattern;
2481 return this;
2482 }
2483
Joe Onoratocb109a02011-01-18 17:57:41 -08002484 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002485 * Set the desired color for the indicator LED on the device, as well as the
2486 * blink duty cycle (specified in milliseconds).
2487 *
2488
2489 * Not all devices will honor all (or even any) of these values.
2490 *
2491
2492 * @see Notification#ledARGB
2493 * @see Notification#ledOnMS
2494 * @see Notification#ledOffMS
Joe Onoratocb109a02011-01-18 17:57:41 -08002495 */
Tor Norbye80756e32015-03-02 09:39:27 -08002496 public Builder setLights(@ColorInt int argb, int onMs, int offMs) {
Joe Onorato46439ce2010-11-19 13:56:21 -08002497 mLedArgb = argb;
2498 mLedOnMs = onMs;
2499 mLedOffMs = offMs;
Joe Onorato46439ce2010-11-19 13:56:21 -08002500 return this;
2501 }
2502
Joe Onoratocb109a02011-01-18 17:57:41 -08002503 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002504 * Set whether this is an "ongoing" notification.
Joe Onoratocb109a02011-01-18 17:57:41 -08002505 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002506
2507 * Ongoing notifications cannot be dismissed by the user, so your application or service
2508 * must take care of canceling them.
2509 *
2510
2511 * They are typically used to indicate a background task that the user is actively engaged
2512 * with (e.g., playing music) or is pending in some way and therefore occupying the device
2513 * (e.g., a file download, sync operation, active network connection).
2514 *
2515
2516 * @see Notification#FLAG_ONGOING_EVENT
2517 * @see Service#setForeground(boolean)
Joe Onoratocb109a02011-01-18 17:57:41 -08002518 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002519 public Builder setOngoing(boolean ongoing) {
2520 setFlag(FLAG_ONGOING_EVENT, ongoing);
2521 return this;
2522 }
2523
Joe Onoratocb109a02011-01-18 17:57:41 -08002524 /**
2525 * Set this flag if you would only like the sound, vibrate
2526 * and ticker to be played if the notification is not already showing.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002527 *
2528 * @see Notification#FLAG_ONLY_ALERT_ONCE
Joe Onoratocb109a02011-01-18 17:57:41 -08002529 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002530 public Builder setOnlyAlertOnce(boolean onlyAlertOnce) {
2531 setFlag(FLAG_ONLY_ALERT_ONCE, onlyAlertOnce);
2532 return this;
2533 }
2534
Joe Onoratocb109a02011-01-18 17:57:41 -08002535 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002536 * Make this notification automatically dismissed when the user touches it. The
2537 * PendingIntent set with {@link #setDeleteIntent} will be sent when this happens.
2538 *
2539 * @see Notification#FLAG_AUTO_CANCEL
Joe Onoratocb109a02011-01-18 17:57:41 -08002540 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002541 public Builder setAutoCancel(boolean autoCancel) {
Joe Onorato281d83f2011-01-04 17:13:10 -08002542 setFlag(FLAG_AUTO_CANCEL, autoCancel);
Joe Onorato46439ce2010-11-19 13:56:21 -08002543 return this;
2544 }
2545
Joe Onoratocb109a02011-01-18 17:57:41 -08002546 /**
Griff Hazendfcb0802014-02-11 12:00:00 -08002547 * Set whether or not this notification should not bridge to other devices.
2548 *
2549 * <p>Some notifications can be bridged to other devices for remote display.
2550 * This hint can be set to recommend this notification not be bridged.
2551 */
2552 public Builder setLocalOnly(boolean localOnly) {
2553 setFlag(FLAG_LOCAL_ONLY, localOnly);
2554 return this;
2555 }
2556
2557 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002558 * Set which notification properties will be inherited from system defaults.
Joe Onoratocb109a02011-01-18 17:57:41 -08002559 * <p>
2560 * The value should be one or more of the following fields combined with
2561 * bitwise-or:
2562 * {@link #DEFAULT_SOUND}, {@link #DEFAULT_VIBRATE}, {@link #DEFAULT_LIGHTS}.
2563 * <p>
2564 * For all default values, use {@link #DEFAULT_ALL}.
2565 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002566 public Builder setDefaults(int defaults) {
2567 mDefaults = defaults;
2568 return this;
2569 }
2570
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002571 /**
2572 * Set the priority of this notification.
2573 *
2574 * @see Notification#priority
2575 */
Tor Norbyed9273d62013-05-30 15:59:53 -07002576 public Builder setPriority(@Priority int pri) {
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002577 mPriority = pri;
2578 return this;
2579 }
Joe Malin8d40d042012-11-05 11:36:40 -08002580
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002581 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -04002582 * Set the notification category.
Joe Malin8d40d042012-11-05 11:36:40 -08002583 *
John Spurlockfd7f1e02014-03-18 16:41:57 -04002584 * @see Notification#category
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002585 */
John Spurlockfd7f1e02014-03-18 16:41:57 -04002586 public Builder setCategory(String category) {
2587 mCategory = category;
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002588 return this;
2589 }
2590
2591 /**
Chris Wrendde75302014-03-26 17:24:15 -04002592 * Add a person that is relevant to this notification.
2593 *
Chris Wrene6c48932014-09-29 17:19:27 -04002594 * <P>
2595 * Depending on user preferences, this annotation may allow the notification to pass
2596 * through interruption filters, and to appear more prominently in the user interface.
2597 * </P>
2598 *
2599 * <P>
2600 * The person should be specified by the {@code String} representation of a
2601 * {@link android.provider.ContactsContract.Contacts#CONTENT_LOOKUP_URI}.
2602 * </P>
2603 *
2604 * <P>The system will also attempt to resolve {@code mailto:} and {@code tel:} schema
2605 * URIs. The path part of these URIs must exist in the contacts database, in the
2606 * appropriate column, or the reference will be discarded as invalid. Telephone schema
2607 * URIs will be resolved by {@link android.provider.ContactsContract.PhoneLookup}.
2608 * </P>
2609 *
2610 * @param uri A URI for the person.
Chris Wrendde75302014-03-26 17:24:15 -04002611 * @see Notification#EXTRA_PEOPLE
2612 */
Chris Wrene6c48932014-09-29 17:19:27 -04002613 public Builder addPerson(String uri) {
2614 mPeople.add(uri);
Chris Wrendde75302014-03-26 17:24:15 -04002615 return this;
2616 }
2617
2618 /**
Griff Hazen5cadc3b2014-05-20 09:55:39 -07002619 * Set this notification to be part of a group of notifications sharing the same key.
2620 * Grouped notifications may display in a cluster or stack on devices which
2621 * support such rendering.
2622 *
2623 * <p>To make this notification the summary for its group, also call
2624 * {@link #setGroupSummary}. A sort order can be specified for group members by using
2625 * {@link #setSortKey}.
2626 * @param groupKey The group key of the group.
2627 * @return this object for method chaining
2628 */
2629 public Builder setGroup(String groupKey) {
2630 mGroupKey = groupKey;
2631 return this;
2632 }
2633
2634 /**
2635 * Set this notification to be the group summary for a group of notifications.
2636 * Grouped notifications may display in a cluster or stack on devices which
2637 * support such rendering. Requires a group key also be set using {@link #setGroup}.
2638 * @param isGroupSummary Whether this notification should be a group summary.
2639 * @return this object for method chaining
2640 */
2641 public Builder setGroupSummary(boolean isGroupSummary) {
2642 setFlag(FLAG_GROUP_SUMMARY, isGroupSummary);
2643 return this;
2644 }
2645
2646 /**
2647 * Set a sort key that orders this notification among other notifications from the
2648 * same package. This can be useful if an external sort was already applied and an app
2649 * would like to preserve this. Notifications will be sorted lexicographically using this
2650 * value, although providing different priorities in addition to providing sort key may
2651 * cause this value to be ignored.
2652 *
2653 * <p>This sort key can also be used to order members of a notification group. See
Griff Hazen9e1379f2014-05-20 12:50:51 -07002654 * {@link #setGroup}.
Griff Hazen5cadc3b2014-05-20 09:55:39 -07002655 *
2656 * @see String#compareTo(String)
2657 */
2658 public Builder setSortKey(String sortKey) {
2659 mSortKey = sortKey;
2660 return this;
2661 }
2662
2663 /**
Griff Hazen720042b2014-02-24 15:46:56 -08002664 * Merge additional metadata into this notification.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002665 *
Griff Hazen720042b2014-02-24 15:46:56 -08002666 * <p>Values within the Bundle will replace existing extras values in this Builder.
2667 *
2668 * @see Notification#extras
2669 */
Griff Hazen959591e2014-05-15 22:26:18 -07002670 public Builder addExtras(Bundle extras) {
2671 if (extras != null) {
2672 if (mExtras == null) {
2673 mExtras = new Bundle(extras);
2674 } else {
2675 mExtras.putAll(extras);
2676 }
Griff Hazen720042b2014-02-24 15:46:56 -08002677 }
2678 return this;
2679 }
2680
2681 /**
2682 * Set metadata for this notification.
2683 *
2684 * <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 -04002685 * current contents are copied into the Notification each time {@link #build()} is
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002686 * called.
2687 *
Griff Hazen720042b2014-02-24 15:46:56 -08002688 * <p>Replaces any existing extras values with those from the provided Bundle.
2689 * Use {@link #addExtras} to merge in metadata instead.
2690 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002691 * @see Notification#extras
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002692 */
Griff Hazen959591e2014-05-15 22:26:18 -07002693 public Builder setExtras(Bundle extras) {
2694 mExtras = extras;
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002695 return this;
2696 }
2697
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002698 /**
Griff Hazen720042b2014-02-24 15:46:56 -08002699 * Get the current metadata Bundle used by this notification Builder.
2700 *
2701 * <p>The returned Bundle is shared with this Builder.
2702 *
2703 * <p>The current contents of this Bundle are copied into the Notification each time
2704 * {@link #build()} is called.
2705 *
2706 * @see Notification#extras
2707 */
2708 public Bundle getExtras() {
2709 if (mExtras == null) {
2710 mExtras = new Bundle();
2711 }
2712 return mExtras;
2713 }
2714
2715 /**
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002716 * Add an action to this notification. Actions are typically displayed by
2717 * the system as a button adjacent to the notification content.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04002718 * <p>
2719 * Every action must have an icon (32dp square and matching the
2720 * <a href="{@docRoot}design/style/iconography.html#action-bar">Holo
2721 * Dark action bar</a> visual style), a textual label, and a {@link PendingIntent}.
2722 * <p>
2723 * A notification in its expanded form can display up to 3 actions, from left to right in
2724 * the order they were added. Actions will not be displayed when the notification is
2725 * collapsed, however, so be sure that any essential functions may be accessed by the user
2726 * in some other way (for example, in the Activity pointed to by {@link #contentIntent}).
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002727 *
2728 * @param icon Resource ID of a drawable that represents the action.
2729 * @param title Text describing the action.
2730 * @param intent PendingIntent to be fired when the action is invoked.
2731 */
2732 public Builder addAction(int icon, CharSequence title, PendingIntent intent) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04002733 mActions.add(new Action(icon, safeCharSequence(title), intent));
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002734 return this;
2735 }
2736
Chris Wrenfbd96ba2012-05-01 12:03:58 -04002737 /**
Griff Hazen959591e2014-05-15 22:26:18 -07002738 * Add an action to this notification. Actions are typically displayed by
2739 * the system as a button adjacent to the notification content.
2740 * <p>
2741 * Every action must have an icon (32dp square and matching the
2742 * <a href="{@docRoot}design/style/iconography.html#action-bar">Holo
2743 * Dark action bar</a> visual style), a textual label, and a {@link PendingIntent}.
2744 * <p>
2745 * A notification in its expanded form can display up to 3 actions, from left to right in
2746 * the order they were added. Actions will not be displayed when the notification is
2747 * collapsed, however, so be sure that any essential functions may be accessed by the user
2748 * in some other way (for example, in the Activity pointed to by {@link #contentIntent}).
2749 *
2750 * @param action The action to add.
2751 */
2752 public Builder addAction(Action action) {
2753 mActions.add(action);
2754 return this;
2755 }
2756
2757 /**
Chris Wrenfbd96ba2012-05-01 12:03:58 -04002758 * Add a rich notification style to be applied at build time.
2759 *
2760 * @param style Object responsible for modifying the notification style.
2761 */
2762 public Builder setStyle(Style style) {
2763 if (mStyle != style) {
2764 mStyle = style;
Daniel Sandlerc08dea22012-06-28 08:35:24 -07002765 if (mStyle != null) {
2766 mStyle.setBuilder(this);
2767 }
Chris Wrenfbd96ba2012-05-01 12:03:58 -04002768 }
2769 return this;
2770 }
2771
Dan Sandler0bf2ed82013-12-21 23:33:41 -06002772 /**
2773 * Specify the value of {@link #visibility}.
Griff Hazenb720abe2014-05-20 13:15:30 -07002774 *
Dan Sandler0bf2ed82013-12-21 23:33:41 -06002775 * @param visibility One of {@link #VISIBILITY_PRIVATE} (the default),
2776 * {@link #VISIBILITY_SECRET}, or {@link #VISIBILITY_PUBLIC}.
2777 *
2778 * @return The same Builder.
2779 */
2780 public Builder setVisibility(int visibility) {
2781 mVisibility = visibility;
2782 return this;
2783 }
2784
2785 /**
2786 * Supply a replacement Notification whose contents should be shown in insecure contexts
2787 * (i.e. atop the secure lockscreen). See {@link #visibility} and {@link #VISIBILITY_PUBLIC}.
2788 * @param n A replacement notification, presumably with some or all info redacted.
2789 * @return The same Builder.
2790 */
2791 public Builder setPublicVersion(Notification n) {
2792 mPublicVersion = n;
2793 return this;
2794 }
2795
Griff Hazenb720abe2014-05-20 13:15:30 -07002796 /**
Griff Hazen5cadc3b2014-05-20 09:55:39 -07002797 * Apply an extender to this notification builder. Extenders may be used to add
2798 * metadata or change options on this builder.
2799 */
Griff Hazen61a9e862014-05-22 16:05:19 -07002800 public Builder extend(Extender extender) {
2801 extender.extend(this);
Griff Hazen5cadc3b2014-05-20 09:55:39 -07002802 return this;
2803 }
2804
Dan Sandler4e787062015-06-17 15:09:48 -04002805 /**
2806 * @hide
2807 */
2808 public void setFlag(int mask, boolean value) {
Joe Onorato46439ce2010-11-19 13:56:21 -08002809 if (value) {
2810 mFlags |= mask;
2811 } else {
2812 mFlags &= ~mask;
2813 }
2814 }
2815
Dan Sandler26e81cf2014-05-06 10:01:27 -04002816 /**
2817 * Sets {@link Notification#color}.
2818 *
2819 * @param argb The accent color to use
2820 *
2821 * @return The same Builder.
2822 */
Tor Norbye80756e32015-03-02 09:39:27 -08002823 public Builder setColor(@ColorInt int argb) {
Dan Sandler26e81cf2014-05-06 10:01:27 -04002824 mColor = argb;
2825 return this;
2826 }
2827
Jorim Jaggid05aa3e2014-08-28 17:52:27 +02002828 private Drawable getProfileBadgeDrawable() {
Christoph Studer7ac80e62014-08-04 16:01:57 +02002829 // Note: This assumes that the current user can read the profile badge of the
2830 // originating user.
Svetoslavc7d62f02014-09-04 15:39:54 -07002831 return mContext.getPackageManager().getUserBadgeForDensity(
Kenny Guy8942bcd2014-09-08 21:09:47 +01002832 new UserHandle(mOriginatingUserId), 0);
Jorim Jaggid05aa3e2014-08-28 17:52:27 +02002833 }
2834
2835 private Bitmap getProfileBadge() {
2836 Drawable badge = getProfileBadgeDrawable();
Kenny Guy8a0101b2014-05-08 23:34:12 +01002837 if (badge == null) {
2838 return null;
2839 }
Jorim Jaggid05aa3e2014-08-28 17:52:27 +02002840 final int size = mContext.getResources().getDimensionPixelSize(
2841 R.dimen.notification_badge_size);
2842 Bitmap bitmap = Bitmap.createBitmap(size, size, Bitmap.Config.ARGB_8888);
Kenny Guy8a0101b2014-05-08 23:34:12 +01002843 Canvas canvas = new Canvas(bitmap);
Jorim Jaggid05aa3e2014-08-28 17:52:27 +02002844 badge.setBounds(0, 0, size, size);
Kenny Guy8a0101b2014-05-08 23:34:12 +01002845 badge.draw(canvas);
2846 return bitmap;
2847 }
2848
Kenny Guy98193ea2014-07-24 19:54:37 +01002849 private boolean addProfileBadge(RemoteViews contentView, int resId) {
2850 Bitmap profileBadge = getProfileBadge();
2851
2852 contentView.setViewVisibility(R.id.profile_badge_large_template, View.GONE);
2853 contentView.setViewVisibility(R.id.profile_badge_line2, View.GONE);
2854 contentView.setViewVisibility(R.id.profile_badge_line3, View.GONE);
2855
2856 if (profileBadge != null) {
2857 contentView.setImageViewBitmap(resId, profileBadge);
2858 contentView.setViewVisibility(resId, View.VISIBLE);
2859
2860 // Make sure Line 3 is visible. As badge will be here if there
2861 // is no text to display.
2862 if (resId == R.id.profile_badge_line3) {
2863 contentView.setViewVisibility(R.id.line3, View.VISIBLE);
2864 }
2865 return true;
2866 }
2867 return false;
2868 }
2869
Jorim Jaggid05aa3e2014-08-28 17:52:27 +02002870 private void shrinkLine3Text(RemoteViews contentView) {
2871 float subTextSize = mContext.getResources().getDimensionPixelSize(
2872 R.dimen.notification_subtext_size);
2873 contentView.setTextViewTextSize(R.id.text, TypedValue.COMPLEX_UNIT_PX, subTextSize);
2874 }
2875
Christoph Studerfe718432014-09-01 18:21:18 +02002876 private void unshrinkLine3Text(RemoteViews contentView) {
2877 float regularTextSize = mContext.getResources().getDimensionPixelSize(
2878 com.android.internal.R.dimen.notification_text_size);
2879 contentView.setTextViewTextSize(R.id.text, TypedValue.COMPLEX_UNIT_PX, regularTextSize);
2880 }
2881
2882 private void resetStandardTemplate(RemoteViews contentView) {
2883 removeLargeIconBackground(contentView);
2884 contentView.setViewPadding(R.id.icon, 0, 0, 0, 0);
2885 contentView.setImageViewResource(R.id.icon, 0);
2886 contentView.setInt(R.id.icon, "setBackgroundResource", 0);
2887 contentView.setViewVisibility(R.id.right_icon, View.GONE);
2888 contentView.setInt(R.id.right_icon, "setBackgroundResource", 0);
2889 contentView.setImageViewResource(R.id.right_icon, 0);
2890 contentView.setImageViewResource(R.id.icon, 0);
2891 contentView.setTextViewText(R.id.title, null);
2892 contentView.setTextViewText(R.id.text, null);
2893 unshrinkLine3Text(contentView);
2894 contentView.setTextViewText(R.id.text2, null);
2895 contentView.setViewVisibility(R.id.text2, View.GONE);
2896 contentView.setViewVisibility(R.id.info, View.GONE);
2897 contentView.setViewVisibility(R.id.time, View.GONE);
2898 contentView.setViewVisibility(R.id.line3, View.GONE);
2899 contentView.setViewVisibility(R.id.overflow_divider, View.GONE);
2900 contentView.setViewVisibility(R.id.progress, View.GONE);
Christoph Studerca1db712014-09-10 17:31:33 +02002901 contentView.setViewVisibility(R.id.chronometer, View.GONE);
2902 contentView.setViewVisibility(R.id.time, View.GONE);
Christoph Studerfe718432014-09-01 18:21:18 +02002903 }
2904
Jorim Jaggi445d3c02014-08-19 22:33:42 +02002905 private RemoteViews applyStandardTemplate(int resId) {
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02002906 return applyStandardTemplate(resId, true /* hasProgress */);
2907 }
2908
2909 /**
2910 * @param hasProgress whether the progress bar should be shown and set
2911 */
2912 private RemoteViews applyStandardTemplate(int resId, boolean hasProgress) {
Kenny Guy77320062014-08-27 21:37:15 +01002913 RemoteViews contentView = new BuilderRemoteViews(mContext.getApplicationInfo(), resId);
Dan Sandler539aad42014-08-04 00:43:39 -04002914
Christoph Studerfe718432014-09-01 18:21:18 +02002915 resetStandardTemplate(contentView);
2916
Daniel Sandler9f7936a2012-05-21 16:14:28 -04002917 boolean showLine3 = false;
2918 boolean showLine2 = false;
Kenny Guy98193ea2014-07-24 19:54:37 +01002919 boolean contentTextInLine2 = false;
Dan Sandler190d58d2014-05-15 09:33:39 -04002920
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01002921 if (mLargeIcon != null) {
Dan Sandlerd63f9322015-05-06 15:18:49 -04002922 contentView.setImageViewIcon(R.id.icon, mLargeIcon);
Christoph Studer239f8352014-08-25 15:13:18 +02002923 processLargeLegacyIcon(mLargeIcon, contentView);
Dan Sandlerd63f9322015-05-06 15:18:49 -04002924 contentView.setImageViewIcon(R.id.right_icon, mSmallIcon);
Dan Sandler190d58d2014-05-15 09:33:39 -04002925 contentView.setViewVisibility(R.id.right_icon, View.VISIBLE);
2926 processSmallRightIcon(mSmallIcon, contentView);
2927 } else { // small icon at left
Dan Sandlerd63f9322015-05-06 15:18:49 -04002928 contentView.setImageViewIcon(R.id.icon, mSmallIcon);
Dan Sandler190d58d2014-05-15 09:33:39 -04002929 contentView.setViewVisibility(R.id.icon, View.VISIBLE);
2930 processSmallIconAsLarge(mSmallIcon, contentView);
Joe Onorato561d3852010-11-20 18:09:34 -08002931 }
2932 if (mContentTitle != null) {
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01002933 contentView.setTextViewText(R.id.title, processLegacyText(mContentTitle));
Joe Onorato561d3852010-11-20 18:09:34 -08002934 }
2935 if (mContentText != null) {
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01002936 contentView.setTextViewText(R.id.text, processLegacyText(mContentText));
Daniel Sandler9f7936a2012-05-21 16:14:28 -04002937 showLine3 = true;
Joe Onorato561d3852010-11-20 18:09:34 -08002938 }
2939 if (mContentInfo != null) {
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01002940 contentView.setTextViewText(R.id.info, processLegacyText(mContentInfo));
Jeff Sharkey1c400132011-08-05 14:50:13 -07002941 contentView.setViewVisibility(R.id.info, View.VISIBLE);
Daniel Sandler9f7936a2012-05-21 16:14:28 -04002942 showLine3 = true;
Joe Onorato561d3852010-11-20 18:09:34 -08002943 } else if (mNumber > 0) {
Daniel Sandlerebce0112011-06-16 16:44:51 -04002944 final int tooBig = mContext.getResources().getInteger(
2945 R.integer.status_bar_notification_info_maxnum);
2946 if (mNumber > tooBig) {
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01002947 contentView.setTextViewText(R.id.info, processLegacyText(
2948 mContext.getResources().getString(
2949 R.string.status_bar_notification_info_overflow)));
Joe Onorato059a2f82011-01-04 10:27:01 -08002950 } else {
2951 NumberFormat f = NumberFormat.getIntegerInstance();
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01002952 contentView.setTextViewText(R.id.info, processLegacyText(f.format(mNumber)));
Joe Onorato059a2f82011-01-04 10:27:01 -08002953 }
Jeff Sharkey1c400132011-08-05 14:50:13 -07002954 contentView.setViewVisibility(R.id.info, View.VISIBLE);
Daniel Sandler9f7936a2012-05-21 16:14:28 -04002955 showLine3 = true;
Joe Onorato561d3852010-11-20 18:09:34 -08002956 } else {
2957 contentView.setViewVisibility(R.id.info, View.GONE);
2958 }
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002959
Daniel Sandler9f7936a2012-05-21 16:14:28 -04002960 // Need to show three lines?
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002961 if (mSubText != null) {
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01002962 contentView.setTextViewText(R.id.text, processLegacyText(mSubText));
Daniel Sandler9f7936a2012-05-21 16:14:28 -04002963 if (mContentText != null) {
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01002964 contentView.setTextViewText(R.id.text2, processLegacyText(mContentText));
Daniel Sandler9f7936a2012-05-21 16:14:28 -04002965 contentView.setViewVisibility(R.id.text2, View.VISIBLE);
2966 showLine2 = true;
Kenny Guy98193ea2014-07-24 19:54:37 +01002967 contentTextInLine2 = true;
Daniel Sandler9f7936a2012-05-21 16:14:28 -04002968 } else {
2969 contentView.setViewVisibility(R.id.text2, View.GONE);
2970 }
Jeff Sharkey1c400132011-08-05 14:50:13 -07002971 } else {
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002972 contentView.setViewVisibility(R.id.text2, View.GONE);
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02002973 if (hasProgress && (mProgressMax != 0 || mProgressIndeterminate)) {
Christoph Studeraca4b252014-09-09 15:58:41 +02002974 contentView.setViewVisibility(R.id.progress, View.VISIBLE);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002975 contentView.setProgressBar(
2976 R.id.progress, mProgressMax, mProgress, mProgressIndeterminate);
Jorim Jaggief72a192014-08-26 21:57:46 +02002977 contentView.setProgressBackgroundTintList(
Alan Viverette4a357cd2015-03-18 18:37:18 -07002978 R.id.progress, ColorStateList.valueOf(mContext.getColor(
Jorim Jaggief72a192014-08-26 21:57:46 +02002979 R.color.notification_progress_background_color)));
2980 if (mColor != COLOR_DEFAULT) {
2981 ColorStateList colorStateList = ColorStateList.valueOf(mColor);
2982 contentView.setProgressTintList(R.id.progress, colorStateList);
2983 contentView.setProgressIndeterminateTintList(R.id.progress, colorStateList);
2984 }
Daniel Sandler9f7936a2012-05-21 16:14:28 -04002985 showLine2 = true;
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002986 } else {
2987 contentView.setViewVisibility(R.id.progress, View.GONE);
2988 }
Jeff Sharkey1c400132011-08-05 14:50:13 -07002989 }
Daniel Sandler9f7936a2012-05-21 16:14:28 -04002990 if (showLine2) {
Jorim Jaggi445d3c02014-08-19 22:33:42 +02002991
2992 // need to shrink all the type to make sure everything fits
Jorim Jaggid05aa3e2014-08-28 17:52:27 +02002993 shrinkLine3Text(contentView);
Daniel Sandler9f7936a2012-05-21 16:14:28 -04002994 }
2995
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02002996 if (showsTimeOrChronometer()) {
Daniel Sandlera2985ed2012-04-03 16:42:00 -04002997 if (mUseChronometer) {
2998 contentView.setViewVisibility(R.id.chronometer, View.VISIBLE);
2999 contentView.setLong(R.id.chronometer, "setBase",
3000 mWhen + (SystemClock.elapsedRealtime() - System.currentTimeMillis()));
3001 contentView.setBoolean(R.id.chronometer, "setStarted", true);
3002 } else {
3003 contentView.setViewVisibility(R.id.time, View.VISIBLE);
3004 contentView.setLong(R.id.time, "setTime", mWhen);
3005 }
Joe Onorato561d3852010-11-20 18:09:34 -08003006 }
Daniel Sandler0c890492012-09-12 17:23:10 -07003007
Jorim Jaggi445d3c02014-08-19 22:33:42 +02003008 // Adjust padding depending on line count and font size.
3009 contentView.setViewPadding(R.id.line1, 0, calculateTopPadding(mContext,
Jorim Jaggid05aa3e2014-08-28 17:52:27 +02003010 mHasThreeLines, mContext.getResources().getConfiguration().fontScale),
Jorim Jaggi445d3c02014-08-19 22:33:42 +02003011 0, 0);
3012
Kenny Guy98193ea2014-07-24 19:54:37 +01003013 // We want to add badge to first line of text.
3014 boolean addedBadge = addProfileBadge(contentView,
3015 contentTextInLine2 ? R.id.profile_badge_line2 : R.id.profile_badge_line3);
3016 // If we added the badge to line 3 then we should show line 3.
3017 if (addedBadge && !contentTextInLine2) {
3018 showLine3 = true;
3019 }
3020
3021 // Note getStandardView may hide line 3 again.
Daniel Sandler9f7936a2012-05-21 16:14:28 -04003022 contentView.setViewVisibility(R.id.line3, showLine3 ? View.VISIBLE : View.GONE);
Daniel Sandler6387d2f2012-05-22 13:44:09 -04003023 contentView.setViewVisibility(R.id.overflow_divider, showLine3 ? View.VISIBLE : View.GONE);
Joe Onorato561d3852010-11-20 18:09:34 -08003024 return contentView;
3025 }
3026
Jorim Jaggi445d3c02014-08-19 22:33:42 +02003027 /**
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02003028 * @return true if the built notification will show the time or the chronometer; false
3029 * otherwise
3030 */
3031 private boolean showsTimeOrChronometer() {
3032 return mWhen != 0 && mShowWhen;
3033 }
3034
3035 /**
Jorim Jaggi445d3c02014-08-19 22:33:42 +02003036 * Logic to find out whether the notification is going to have three lines in the contracted
3037 * layout. This is used to adjust the top padding.
3038 *
3039 * @return true if the notification is going to have three lines; false if the notification
3040 * is going to have one or two lines
3041 */
3042 private boolean hasThreeLines() {
Jorim Jaggid05aa3e2014-08-28 17:52:27 +02003043 boolean contentTextInLine2 = mSubText != null && mContentText != null;
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02003044 boolean hasProgress = mStyle == null || mStyle.hasProgress();
Jorim Jaggid05aa3e2014-08-28 17:52:27 +02003045 // If we have content text in line 2, badge goes into line 2, or line 3 otherwise
3046 boolean badgeInLine3 = getProfileBadgeDrawable() != null && !contentTextInLine2;
3047 boolean hasLine3 = mContentText != null || mContentInfo != null || mNumber > 0
3048 || badgeInLine3;
Jorim Jaggi445d3c02014-08-19 22:33:42 +02003049 boolean hasLine2 = (mSubText != null && mContentText != null) ||
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02003050 (hasProgress && mSubText == null
3051 && (mProgressMax != 0 || mProgressIndeterminate));
Jorim Jaggi445d3c02014-08-19 22:33:42 +02003052 return hasLine2 && hasLine3;
3053 }
3054
3055 /**
3056 * @hide
3057 */
3058 public static int calculateTopPadding(Context ctx, boolean hasThreeLines,
3059 float fontScale) {
3060 int padding = ctx.getResources().getDimensionPixelSize(hasThreeLines
3061 ? R.dimen.notification_top_pad_narrow
3062 : R.dimen.notification_top_pad);
3063 int largePadding = ctx.getResources().getDimensionPixelSize(hasThreeLines
3064 ? R.dimen.notification_top_pad_large_text_narrow
3065 : R.dimen.notification_top_pad_large_text);
3066 float largeFactor = (MathUtils.constrain(fontScale, 1.0f, LARGE_TEXT_SCALE) - 1f)
3067 / (LARGE_TEXT_SCALE - 1f);
3068
3069 // Linearly interpolate the padding between large and normal with the font scale ranging
3070 // from 1f to LARGE_TEXT_SCALE
3071 return Math.round((1 - largeFactor) * padding + largeFactor * largePadding);
3072 }
3073
Christoph Studerfe718432014-09-01 18:21:18 +02003074 private void resetStandardTemplateWithActions(RemoteViews big) {
3075 big.setViewVisibility(R.id.actions, View.GONE);
3076 big.setViewVisibility(R.id.action_divider, View.GONE);
3077 big.removeAllViews(R.id.actions);
3078 }
3079
Daniel Sandler96fd7c12012-03-30 16:37:36 -04003080 private RemoteViews applyStandardTemplateWithActions(int layoutId) {
Jorim Jaggi445d3c02014-08-19 22:33:42 +02003081 RemoteViews big = applyStandardTemplate(layoutId);
Daniel Sandler96fd7c12012-03-30 16:37:36 -04003082
Christoph Studerfe718432014-09-01 18:21:18 +02003083 resetStandardTemplateWithActions(big);
3084
Daniel Sandler96fd7c12012-03-30 16:37:36 -04003085 int N = mActions.size();
3086 if (N > 0) {
Daniel Sandler96fd7c12012-03-30 16:37:36 -04003087 big.setViewVisibility(R.id.actions, View.VISIBLE);
Daniel Sandler6387d2f2012-05-22 13:44:09 -04003088 big.setViewVisibility(R.id.action_divider, View.VISIBLE);
Daniel Sandler8680bf82012-05-15 16:52:52 -04003089 if (N>MAX_ACTION_BUTTONS) N=MAX_ACTION_BUTTONS;
Daniel Sandler96fd7c12012-03-30 16:37:36 -04003090 for (int i=0; i<N; i++) {
3091 final RemoteViews button = generateActionButton(mActions.get(i));
Daniel Sandler96fd7c12012-03-30 16:37:36 -04003092 big.addView(R.id.actions, button);
3093 }
3094 }
3095 return big;
3096 }
3097
Joe Onorato46439ce2010-11-19 13:56:21 -08003098 private RemoteViews makeContentView() {
3099 if (mContentView != null) {
3100 return mContentView;
3101 } else {
Jorim Jaggi445d3c02014-08-19 22:33:42 +02003102 return applyStandardTemplate(getBaseLayoutResource());
Joe Onorato46439ce2010-11-19 13:56:21 -08003103 }
3104 }
3105
3106 private RemoteViews makeTickerView() {
3107 if (mTickerView != null) {
3108 return mTickerView;
Joe Onorato46439ce2010-11-19 13:56:21 -08003109 }
Dan Sandler5fcdf6e2014-07-18 11:31:15 -04003110 return null; // tickers are not created by default anymore
Joe Onorato46439ce2010-11-19 13:56:21 -08003111 }
3112
Daniel Sandler96fd7c12012-03-30 16:37:36 -04003113 private RemoteViews makeBigContentView() {
3114 if (mActions.size() == 0) return null;
3115
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01003116 return applyStandardTemplateWithActions(getBigBaseLayoutResource());
Daniel Sandler96fd7c12012-03-30 16:37:36 -04003117 }
3118
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01003119 private RemoteViews makeHeadsUpContentView() {
Chris Wren8fd39ec2014-02-27 17:43:26 -05003120 if (mActions.size() == 0) return null;
3121
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01003122 return applyStandardTemplateWithActions(getBigBaseLayoutResource());
Chris Wren8fd39ec2014-02-27 17:43:26 -05003123 }
3124
3125
Daniel Sandler96fd7c12012-03-30 16:37:36 -04003126 private RemoteViews generateActionButton(Action action) {
Daniel Sandler8680bf82012-05-15 16:52:52 -04003127 final boolean tombstone = (action.actionIntent == null);
Joe Malin8d40d042012-11-05 11:36:40 -08003128 RemoteViews button = new RemoteViews(mContext.getPackageName(),
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01003129 tombstone ? getActionTombstoneLayoutResource()
3130 : getActionLayoutResource());
Chris Wren8749ac8a2013-12-03 14:31:01 -05003131 button.setTextViewCompoundDrawablesRelative(R.id.action0, action.icon, 0, 0, 0);
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01003132 button.setTextViewText(R.id.action0, processLegacyText(action.title));
Daniel Sandler8680bf82012-05-15 16:52:52 -04003133 if (!tombstone) {
Daniel Sandlere5518842012-05-10 16:20:40 -04003134 button.setOnClickPendingIntent(R.id.action0, action.actionIntent);
Daniel Sandlere5518842012-05-10 16:20:40 -04003135 }
Daniel Sandler96fd7c12012-03-30 16:37:36 -04003136 button.setContentDescription(R.id.action0, action.title);
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01003137 processLegacyAction(action, button);
Daniel Sandler96fd7c12012-03-30 16:37:36 -04003138 return button;
3139 }
3140
Joe Onoratocb109a02011-01-18 17:57:41 -08003141 /**
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01003142 * @return Whether we are currently building a notification from a legacy (an app that
Alan Viverette3cb07a462014-06-06 14:19:53 -07003143 * doesn't create material notifications by itself) app.
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01003144 */
3145 private boolean isLegacy() {
Dan Sandler26e81cf2014-05-06 10:01:27 -04003146 return mColorUtil != null;
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01003147 }
3148
3149 private void processLegacyAction(Action action, RemoteViews button) {
Dan Sandler05c362d2014-09-03 00:16:27 +02003150 if (!isLegacy() || mColorUtil.isGrayscaleIcon(mContext, action.icon)) {
Christoph Studer239f8352014-08-25 15:13:18 +02003151 button.setTextViewCompoundDrawablesRelativeColorFilter(R.id.action0, 0,
Alan Viverette4a357cd2015-03-18 18:37:18 -07003152 mContext.getColor(R.color.notification_action_color_filter),
Christoph Studer239f8352014-08-25 15:13:18 +02003153 PorterDuff.Mode.MULTIPLY);
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01003154 }
3155 }
3156
3157 private CharSequence processLegacyText(CharSequence charSequence) {
3158 if (isLegacy()) {
Dan Sandler26e81cf2014-05-06 10:01:27 -04003159 return mColorUtil.invertCharSequenceColors(charSequence);
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01003160 } else {
3161 return charSequence;
3162 }
3163 }
3164
Dan Sandler26e81cf2014-05-06 10:01:27 -04003165 /**
3166 * Apply any necessary background to smallIcons being used in the largeIcon spot.
3167 */
Dan Sandlerd63f9322015-05-06 15:18:49 -04003168 private void processSmallIconAsLarge(Icon largeIcon, RemoteViews contentView) {
Jorim Jaggi92df1f22014-12-16 19:44:41 +01003169 if (!isLegacy()) {
3170 contentView.setDrawableParameters(R.id.icon, false, -1,
3171 0xFFFFFFFF,
3172 PorterDuff.Mode.SRC_ATOP, -1);
Dan Sandler26e81cf2014-05-06 10:01:27 -04003173 applyLargeIconBackground(contentView);
Dan Sandlerd63f9322015-05-06 15:18:49 -04003174 } else {
3175 if (mColorUtil.isGrayscaleIcon(mContext, largeIcon)) {
3176 applyLargeIconBackground(contentView);
3177 }
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01003178 }
3179 }
3180
Dan Sandler26e81cf2014-05-06 10:01:27 -04003181 /**
3182 * Apply any necessary background to a largeIcon if it's a fake smallIcon (that is,
3183 * if it's grayscale).
3184 */
3185 // TODO: also check bounds, transparency, that sort of thing.
Dan Sandlerd63f9322015-05-06 15:18:49 -04003186 private void processLargeLegacyIcon(Icon largeIcon, RemoteViews contentView) {
3187 if (largeIcon != null && isLegacy()
3188 && mColorUtil.isGrayscaleIcon(mContext, largeIcon)) {
Dan Sandler26e81cf2014-05-06 10:01:27 -04003189 applyLargeIconBackground(contentView);
Dan Sandler190d58d2014-05-15 09:33:39 -04003190 } else {
3191 removeLargeIconBackground(contentView);
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01003192 }
3193 }
3194
Dan Sandler26e81cf2014-05-06 10:01:27 -04003195 /**
3196 * Add a colored circle behind the largeIcon slot.
3197 */
3198 private void applyLargeIconBackground(RemoteViews contentView) {
3199 contentView.setInt(R.id.icon, "setBackgroundResource",
Jorim Jaggi445d3c02014-08-19 22:33:42 +02003200 R.drawable.notification_icon_legacy_bg);
Dan Sandler26e81cf2014-05-06 10:01:27 -04003201
3202 contentView.setDrawableParameters(
3203 R.id.icon,
3204 true,
3205 -1,
Jorim Jaggi74419312014-06-10 20:57:21 +02003206 resolveColor(),
Dan Sandler26e81cf2014-05-06 10:01:27 -04003207 PorterDuff.Mode.SRC_ATOP,
3208 -1);
Jorim Jaggi445d3c02014-08-19 22:33:42 +02003209
3210 int padding = mContext.getResources().getDimensionPixelSize(
3211 R.dimen.notification_large_icon_circle_padding);
3212 contentView.setViewPadding(R.id.icon, padding, padding, padding, padding);
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01003213 }
3214
Dan Sandler190d58d2014-05-15 09:33:39 -04003215 private void removeLargeIconBackground(RemoteViews contentView) {
3216 contentView.setInt(R.id.icon, "setBackgroundResource", 0);
3217 }
3218
Dan Sandler26e81cf2014-05-06 10:01:27 -04003219 /**
3220 * Recolor small icons when used in the R.id.right_icon slot.
3221 */
Dan Sandlerd63f9322015-05-06 15:18:49 -04003222 private void processSmallRightIcon(Icon smallIcon, RemoteViews contentView) {
Jorim Jaggi92df1f22014-12-16 19:44:41 +01003223 if (!isLegacy()) {
Dan Sandler190d58d2014-05-15 09:33:39 -04003224 contentView.setDrawableParameters(R.id.right_icon, false, -1,
3225 0xFFFFFFFF,
3226 PorterDuff.Mode.SRC_ATOP, -1);
Jorim Jaggi92df1f22014-12-16 19:44:41 +01003227 }
Dan Sandlerd63f9322015-05-06 15:18:49 -04003228 final boolean gray = isLegacy()
3229 && smallIcon.getType() == Icon.TYPE_RESOURCE
3230 && mColorUtil.isGrayscaleIcon(mContext, smallIcon.getResId());
3231 if (!isLegacy() || gray) {
Dan Sandler190d58d2014-05-15 09:33:39 -04003232 contentView.setInt(R.id.right_icon,
3233 "setBackgroundResource",
3234 R.drawable.notification_icon_legacy_bg);
3235
3236 contentView.setDrawableParameters(
3237 R.id.right_icon,
3238 true,
3239 -1,
Jorim Jaggi74419312014-06-10 20:57:21 +02003240 resolveColor(),
Dan Sandler190d58d2014-05-15 09:33:39 -04003241 PorterDuff.Mode.SRC_ATOP,
3242 -1);
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01003243 }
3244 }
3245
Jorim Jaggi74419312014-06-10 20:57:21 +02003246 private int sanitizeColor() {
3247 if (mColor != COLOR_DEFAULT) {
3248 mColor |= 0xFF000000; // no alpha for custom colors
3249 }
3250 return mColor;
3251 }
3252
Dan Sandler26e81cf2014-05-06 10:01:27 -04003253 private int resolveColor() {
3254 if (mColor == COLOR_DEFAULT) {
Alan Viverette4a357cd2015-03-18 18:37:18 -07003255 return mContext.getColor(R.color.notification_icon_bg_color);
Dan Sandler26e81cf2014-05-06 10:01:27 -04003256 }
3257 return mColor;
3258 }
3259
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01003260 /**
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003261 * Apply the unstyled operations and return a new {@link Notification} object.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04003262 * @hide
Joe Onoratocb109a02011-01-18 17:57:41 -08003263 */
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04003264 public Notification buildUnstyled() {
Joe Onorato46439ce2010-11-19 13:56:21 -08003265 Notification n = new Notification();
3266 n.when = mWhen;
Dan Sandlerd63f9322015-05-06 15:18:49 -04003267 n.mSmallIcon = mSmallIcon;
Dan Sandler3936e7a2015-05-19 20:59:12 -04003268 if (mSmallIcon != null && mSmallIcon.getType() == Icon.TYPE_RESOURCE) {
Dan Sandlerd63f9322015-05-06 15:18:49 -04003269 n.icon = mSmallIcon.getResId();
3270 }
Joe Onorato46439ce2010-11-19 13:56:21 -08003271 n.iconLevel = mSmallIconLevel;
Joe Onorato8595a3d2010-11-19 18:12:07 -08003272 n.number = mNumber;
Dan Sandler26e81cf2014-05-06 10:01:27 -04003273
Jorim Jaggi74419312014-06-10 20:57:21 +02003274 n.color = sanitizeColor();
Dan Sandler26e81cf2014-05-06 10:01:27 -04003275
Christoph Studer4600f9b2014-07-22 22:44:43 +02003276 setBuilderContentView(n, makeContentView());
Joe Onorato46439ce2010-11-19 13:56:21 -08003277 n.contentIntent = mContentIntent;
3278 n.deleteIntent = mDeleteIntent;
3279 n.fullScreenIntent = mFullScreenIntent;
3280 n.tickerText = mTickerText;
3281 n.tickerView = makeTickerView();
Dan Sandlerd63f9322015-05-06 15:18:49 -04003282 n.mLargeIcon = mLargeIcon;
3283 if (mLargeIcon != null && mLargeIcon.getType() == Icon.TYPE_BITMAP) {
3284 n.largeIcon = mLargeIcon.getBitmap();
3285 }
Joe Onorato46439ce2010-11-19 13:56:21 -08003286 n.sound = mSound;
3287 n.audioStreamType = mAudioStreamType;
John Spurlockc0650f022014-07-19 13:22:39 -04003288 n.audioAttributes = mAudioAttributes;
Joe Onorato46439ce2010-11-19 13:56:21 -08003289 n.vibrate = mVibrate;
3290 n.ledARGB = mLedArgb;
3291 n.ledOnMS = mLedOnMs;
3292 n.ledOffMS = mLedOffMs;
3293 n.defaults = mDefaults;
3294 n.flags = mFlags;
Christoph Studer4600f9b2014-07-22 22:44:43 +02003295 setBuilderBigContentView(n, makeBigContentView());
3296 setBuilderHeadsUpContentView(n, makeHeadsUpContentView());
Daniel Sandler26c13432013-04-04 11:01:04 -04003297 if (mLedOnMs != 0 || mLedOffMs != 0) {
Joe Onorato8d0b6552010-11-22 16:09:29 -08003298 n.flags |= FLAG_SHOW_LIGHTS;
3299 }
3300 if ((mDefaults & DEFAULT_LIGHTS) != 0) {
3301 n.flags |= FLAG_SHOW_LIGHTS;
3302 }
John Spurlockfd7f1e02014-03-18 16:41:57 -04003303 n.category = mCategory;
Griff Hazen5cadc3b2014-05-20 09:55:39 -07003304 n.mGroupKey = mGroupKey;
3305 n.mSortKey = mSortKey;
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003306 n.priority = mPriority;
Daniel Sandlera0a938c2012-03-15 08:42:37 -04003307 if (mActions.size() > 0) {
3308 n.actions = new Action[mActions.size()];
3309 mActions.toArray(n.actions);
3310 }
Dan Sandler0bf2ed82013-12-21 23:33:41 -06003311 n.visibility = mVisibility;
3312
3313 if (mPublicVersion != null) {
3314 n.publicVersion = new Notification();
3315 mPublicVersion.cloneInto(n.publicVersion, true);
3316 }
Christoph Studer4600f9b2014-07-22 22:44:43 +02003317 // Note: If you're adding new fields, also update restoreFromNotitification().
Joe Onorato46439ce2010-11-19 13:56:21 -08003318 return n;
3319 }
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003320
3321 /**
Daniel Sandlerf45564e2013-04-15 15:05:08 -04003322 * Capture, in the provided bundle, semantic information used in the construction of
3323 * this Notification object.
3324 * @hide
3325 */
Griff Hazen720042b2014-02-24 15:46:56 -08003326 public void populateExtras(Bundle extras) {
Daniel Sandlerf45564e2013-04-15 15:05:08 -04003327 // Store original information used in the construction of this object
Kenny Guy8942bcd2014-09-08 21:09:47 +01003328 extras.putInt(EXTRA_ORIGINATING_USERID, mOriginatingUserId);
Kenny Guy77320062014-08-27 21:37:15 +01003329 extras.putParcelable(EXTRA_REBUILD_CONTEXT_APPLICATION_INFO,
3330 mContext.getApplicationInfo());
Daniel Sandlerf45564e2013-04-15 15:05:08 -04003331 extras.putCharSequence(EXTRA_TITLE, mContentTitle);
3332 extras.putCharSequence(EXTRA_TEXT, mContentText);
3333 extras.putCharSequence(EXTRA_SUB_TEXT, mSubText);
3334 extras.putCharSequence(EXTRA_INFO_TEXT, mContentInfo);
Dan Sandlerd63f9322015-05-06 15:18:49 -04003335 extras.putParcelable(EXTRA_SMALL_ICON, mSmallIcon);
Daniel Sandlerf45564e2013-04-15 15:05:08 -04003336 extras.putInt(EXTRA_PROGRESS, mProgress);
3337 extras.putInt(EXTRA_PROGRESS_MAX, mProgressMax);
3338 extras.putBoolean(EXTRA_PROGRESS_INDETERMINATE, mProgressIndeterminate);
3339 extras.putBoolean(EXTRA_SHOW_CHRONOMETER, mUseChronometer);
3340 extras.putBoolean(EXTRA_SHOW_WHEN, mShowWhen);
John Spurlockac08a472013-06-10 11:37:08 -04003341 if (mLargeIcon != null) {
3342 extras.putParcelable(EXTRA_LARGE_ICON, mLargeIcon);
3343 }
Chris Wrendde75302014-03-26 17:24:15 -04003344 if (!mPeople.isEmpty()) {
3345 extras.putStringArray(EXTRA_PEOPLE, mPeople.toArray(new String[mPeople.size()]));
3346 }
Christoph Studer4600f9b2014-07-22 22:44:43 +02003347 // NOTE: If you're adding new extras also update restoreFromNotification().
3348 }
3349
3350
3351 /**
3352 * @hide
3353 */
3354 public static void stripForDelivery(Notification n) {
3355 if (!STRIP_AND_REBUILD) {
3356 return;
3357 }
3358
3359 String templateClass = n.extras.getString(EXTRA_TEMPLATE);
3360 // Only strip views for known Styles because we won't know how to
3361 // re-create them otherwise.
3362 boolean stripViews = TextUtils.isEmpty(templateClass) ||
3363 getNotificationStyleClass(templateClass) != null;
3364
3365 boolean isStripped = false;
3366
3367 if (n.largeIcon != null && n.extras.containsKey(EXTRA_LARGE_ICON)) {
3368 // TODO: Would like to check for equality here, but if the notification
3369 // has been cloned, we can't.
3370 n.largeIcon = null;
3371 n.extras.putBoolean(EXTRA_REBUILD_LARGE_ICON, true);
3372 isStripped = true;
3373 }
3374 // Get rid of unmodified BuilderRemoteViews.
3375
3376 if (stripViews &&
3377 n.contentView instanceof BuilderRemoteViews &&
3378 n.extras.getInt(EXTRA_REBUILD_CONTENT_VIEW_ACTION_COUNT, -1) ==
3379 n.contentView.getSequenceNumber()) {
3380 n.contentView = null;
3381 n.extras.putBoolean(EXTRA_REBUILD_CONTENT_VIEW, true);
3382 n.extras.remove(EXTRA_REBUILD_CONTENT_VIEW_ACTION_COUNT);
3383 isStripped = true;
3384 }
3385 if (stripViews &&
3386 n.bigContentView instanceof BuilderRemoteViews &&
3387 n.extras.getInt(EXTRA_REBUILD_BIG_CONTENT_VIEW_ACTION_COUNT, -1) ==
3388 n.bigContentView.getSequenceNumber()) {
3389 n.bigContentView = null;
3390 n.extras.putBoolean(EXTRA_REBUILD_BIG_CONTENT_VIEW, true);
3391 n.extras.remove(EXTRA_REBUILD_BIG_CONTENT_VIEW_ACTION_COUNT);
3392 isStripped = true;
3393 }
3394 if (stripViews &&
3395 n.headsUpContentView instanceof BuilderRemoteViews &&
3396 n.extras.getInt(EXTRA_REBUILD_HEADS_UP_CONTENT_VIEW_ACTION_COUNT, -1) ==
3397 n.headsUpContentView.getSequenceNumber()) {
3398 n.headsUpContentView = null;
3399 n.extras.putBoolean(EXTRA_REBUILD_HEADS_UP_CONTENT_VIEW, true);
3400 n.extras.remove(EXTRA_REBUILD_HEADS_UP_CONTENT_VIEW_ACTION_COUNT);
3401 isStripped = true;
3402 }
3403
3404 if (isStripped) {
3405 n.extras.putBoolean(EXTRA_NEEDS_REBUILD, true);
3406 }
3407 }
3408
3409 /**
3410 * @hide
3411 */
3412 public static Notification rebuild(Context context, Notification n) {
3413 Bundle extras = n.extras;
3414 if (!extras.getBoolean(EXTRA_NEEDS_REBUILD)) return n;
3415 extras.remove(EXTRA_NEEDS_REBUILD);
3416
3417 // Re-create notification context so we can access app resources.
Kenny Guy77320062014-08-27 21:37:15 +01003418 ApplicationInfo applicationInfo = extras.getParcelable(
3419 EXTRA_REBUILD_CONTEXT_APPLICATION_INFO);
Christoph Studer4600f9b2014-07-22 22:44:43 +02003420 Context builderContext;
3421 try {
Kenny Guy77320062014-08-27 21:37:15 +01003422 builderContext = context.createApplicationContext(applicationInfo,
Christoph Studer4600f9b2014-07-22 22:44:43 +02003423 Context.CONTEXT_RESTRICTED);
3424 } catch (NameNotFoundException e) {
Kenny Guy77320062014-08-27 21:37:15 +01003425 Log.e(TAG, "ApplicationInfo " + applicationInfo + " not found");
Christoph Studer4600f9b2014-07-22 22:44:43 +02003426 builderContext = context; // try with our context
3427 }
3428
3429 Builder b = new Builder(builderContext, n);
3430 return b.rebuild();
3431 }
3432
3433 /**
3434 * Rebuilds the notification passed in to the rebuild-constructor
3435 * {@link #Builder(Context, Notification)}.
3436 *
3437 * <p>
3438 * Throws IllegalStateException when invoked on a Builder that isn't in rebuild mode.
3439 *
3440 * @hide
3441 */
3442 private Notification rebuild() {
3443 if (mRebuildNotification == null) {
3444 throw new IllegalStateException("rebuild() only valid when in 'rebuild' mode.");
3445 }
Jorim Jaggid05aa3e2014-08-28 17:52:27 +02003446 mHasThreeLines = hasThreeLines();
Christoph Studer4600f9b2014-07-22 22:44:43 +02003447
3448 Bundle extras = mRebuildNotification.extras;
3449
3450 if (extras.getBoolean(EXTRA_REBUILD_LARGE_ICON)) {
3451 mRebuildNotification.largeIcon = extras.getParcelable(EXTRA_LARGE_ICON);
3452 }
3453 extras.remove(EXTRA_REBUILD_LARGE_ICON);
3454
3455 if (extras.getBoolean(EXTRA_REBUILD_CONTENT_VIEW)) {
3456 setBuilderContentView(mRebuildNotification, makeContentView());
3457 if (mStyle != null) {
3458 mStyle.populateContentView(mRebuildNotification);
3459 }
3460 }
3461 extras.remove(EXTRA_REBUILD_CONTENT_VIEW);
3462
3463 if (extras.getBoolean(EXTRA_REBUILD_BIG_CONTENT_VIEW)) {
3464 setBuilderBigContentView(mRebuildNotification, makeBigContentView());
3465 if (mStyle != null) {
3466 mStyle.populateBigContentView(mRebuildNotification);
3467 }
3468 }
3469 extras.remove(EXTRA_REBUILD_BIG_CONTENT_VIEW);
3470
3471 if (extras.getBoolean(EXTRA_REBUILD_HEADS_UP_CONTENT_VIEW)) {
3472 setBuilderHeadsUpContentView(mRebuildNotification, makeHeadsUpContentView());
3473 if (mStyle != null) {
3474 mStyle.populateHeadsUpContentView(mRebuildNotification);
3475 }
3476 }
3477 extras.remove(EXTRA_REBUILD_HEADS_UP_CONTENT_VIEW);
3478
Jorim Jaggid05aa3e2014-08-28 17:52:27 +02003479 mHasThreeLines = false;
Christoph Studer4600f9b2014-07-22 22:44:43 +02003480 return mRebuildNotification;
3481 }
3482
3483 private static Class<? extends Style> getNotificationStyleClass(String templateClass) {
3484 Class<? extends Style>[] classes = new Class[]{
3485 BigTextStyle.class, BigPictureStyle.class, InboxStyle.class, MediaStyle.class};
3486 for (Class<? extends Style> innerClass : classes) {
3487 if (templateClass.equals(innerClass.getName())) {
3488 return innerClass;
3489 }
3490 }
3491 return null;
3492 }
3493
3494 private void setBuilderContentView(Notification n, RemoteViews contentView) {
3495 n.contentView = contentView;
3496 if (contentView instanceof BuilderRemoteViews) {
3497 mRebuildBundle.putInt(Builder.EXTRA_REBUILD_CONTENT_VIEW_ACTION_COUNT,
3498 contentView.getSequenceNumber());
3499 }
3500 }
3501
3502 private void setBuilderBigContentView(Notification n, RemoteViews bigContentView) {
3503 n.bigContentView = bigContentView;
3504 if (bigContentView instanceof BuilderRemoteViews) {
3505 mRebuildBundle.putInt(Builder.EXTRA_REBUILD_BIG_CONTENT_VIEW_ACTION_COUNT,
3506 bigContentView.getSequenceNumber());
3507 }
3508 }
3509
3510 private void setBuilderHeadsUpContentView(Notification n,
3511 RemoteViews headsUpContentView) {
3512 n.headsUpContentView = headsUpContentView;
3513 if (headsUpContentView instanceof BuilderRemoteViews) {
3514 mRebuildBundle.putInt(Builder.EXTRA_REBUILD_HEADS_UP_CONTENT_VIEW_ACTION_COUNT,
3515 headsUpContentView.getSequenceNumber());
3516 }
3517 }
3518
3519 private void restoreFromNotification(Notification n) {
3520
3521 // Notification fields.
3522 mWhen = n.when;
Dan Sandlerd63f9322015-05-06 15:18:49 -04003523 mSmallIcon = n.mSmallIcon;
Christoph Studer4600f9b2014-07-22 22:44:43 +02003524 mSmallIconLevel = n.iconLevel;
3525 mNumber = n.number;
3526
3527 mColor = n.color;
3528
3529 mContentView = n.contentView;
3530 mDeleteIntent = n.deleteIntent;
3531 mFullScreenIntent = n.fullScreenIntent;
3532 mTickerText = n.tickerText;
3533 mTickerView = n.tickerView;
Dan Sandlerd63f9322015-05-06 15:18:49 -04003534 mLargeIcon = n.mLargeIcon;
Christoph Studer4600f9b2014-07-22 22:44:43 +02003535 mSound = n.sound;
3536 mAudioStreamType = n.audioStreamType;
3537 mAudioAttributes = n.audioAttributes;
3538
3539 mVibrate = n.vibrate;
3540 mLedArgb = n.ledARGB;
3541 mLedOnMs = n.ledOnMS;
3542 mLedOffMs = n.ledOffMS;
3543 mDefaults = n.defaults;
3544 mFlags = n.flags;
3545
3546 mCategory = n.category;
3547 mGroupKey = n.mGroupKey;
3548 mSortKey = n.mSortKey;
3549 mPriority = n.priority;
3550 mActions.clear();
3551 if (n.actions != null) {
3552 Collections.addAll(mActions, n.actions);
3553 }
3554 mVisibility = n.visibility;
3555
3556 mPublicVersion = n.publicVersion;
3557
3558 // Extras.
3559 Bundle extras = n.extras;
Kenny Guy8942bcd2014-09-08 21:09:47 +01003560 mOriginatingUserId = extras.getInt(EXTRA_ORIGINATING_USERID);
Christoph Studer4600f9b2014-07-22 22:44:43 +02003561 mContentTitle = extras.getCharSequence(EXTRA_TITLE);
3562 mContentText = extras.getCharSequence(EXTRA_TEXT);
3563 mSubText = extras.getCharSequence(EXTRA_SUB_TEXT);
3564 mContentInfo = extras.getCharSequence(EXTRA_INFO_TEXT);
Dan Sandlerd63f9322015-05-06 15:18:49 -04003565 mSmallIcon = extras.getParcelable(EXTRA_SMALL_ICON);
Christoph Studer4600f9b2014-07-22 22:44:43 +02003566 mProgress = extras.getInt(EXTRA_PROGRESS);
3567 mProgressMax = extras.getInt(EXTRA_PROGRESS_MAX);
3568 mProgressIndeterminate = extras.getBoolean(EXTRA_PROGRESS_INDETERMINATE);
3569 mUseChronometer = extras.getBoolean(EXTRA_SHOW_CHRONOMETER);
3570 mShowWhen = extras.getBoolean(EXTRA_SHOW_WHEN);
3571 if (extras.containsKey(EXTRA_LARGE_ICON)) {
3572 mLargeIcon = extras.getParcelable(EXTRA_LARGE_ICON);
3573 }
3574 if (extras.containsKey(EXTRA_PEOPLE)) {
3575 mPeople.clear();
3576 Collections.addAll(mPeople, extras.getStringArray(EXTRA_PEOPLE));
3577 }
Daniel Sandlerf45564e2013-04-15 15:05:08 -04003578 }
3579
3580 /**
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003581 * @deprecated Use {@link #build()} instead.
3582 */
3583 @Deprecated
3584 public Notification getNotification() {
3585 return build();
3586 }
3587
3588 /**
3589 * Combine all of the options that have been set and return a new {@link Notification}
3590 * object.
3591 */
3592 public Notification build() {
Jorim Jaggia0d58ae2015-06-03 11:48:13 -07003593 if (mSmallIcon != null) {
3594 mSmallIcon.convertToAshmem();
3595 }
3596 if (mLargeIcon != null) {
3597 mLargeIcon.convertToAshmem();
3598 }
Kenny Guy8942bcd2014-09-08 21:09:47 +01003599 mOriginatingUserId = mContext.getUserId();
Jorim Jaggid05aa3e2014-08-28 17:52:27 +02003600 mHasThreeLines = hasThreeLines();
Christoph Studer943aa672014-08-03 20:31:16 +02003601
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04003602 Notification n = buildUnstyled();
Daniel Sandlerf45564e2013-04-15 15:05:08 -04003603
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003604 if (mStyle != null) {
Jorim Jaggia0d58ae2015-06-03 11:48:13 -07003605 mStyle.purgeResources();
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04003606 n = mStyle.buildStyled(n);
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003607 }
Daniel Sandlerf45564e2013-04-15 15:05:08 -04003608
Christoph Studer4600f9b2014-07-22 22:44:43 +02003609 if (mExtras != null) {
3610 n.extras.putAll(mExtras);
3611 }
3612
3613 if (mRebuildBundle.size() > 0) {
3614 n.extras.putAll(mRebuildBundle);
3615 mRebuildBundle.clear();
3616 }
Daniel Sandlerf45564e2013-04-15 15:05:08 -04003617
Griff Hazen720042b2014-02-24 15:46:56 -08003618 populateExtras(n.extras);
Daniel Sandlerf45564e2013-04-15 15:05:08 -04003619 if (mStyle != null) {
3620 mStyle.addExtras(n.extras);
3621 }
3622
Jorim Jaggid05aa3e2014-08-28 17:52:27 +02003623 mHasThreeLines = false;
Daniel Sandlerf45564e2013-04-15 15:05:08 -04003624 return n;
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003625 }
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05003626
3627 /**
3628 * Apply this Builder to an existing {@link Notification} object.
3629 *
3630 * @hide
3631 */
3632 public Notification buildInto(Notification n) {
Daniel Sandler1a497d32013-04-18 14:52:45 -04003633 build().cloneInto(n, true);
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05003634 return n;
3635 }
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01003636
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01003637 private int getBaseLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07003638 return R.layout.notification_template_material_base;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01003639 }
3640
3641 private int getBigBaseLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07003642 return R.layout.notification_template_material_big_base;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01003643 }
3644
3645 private int getBigPictureLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07003646 return R.layout.notification_template_material_big_picture;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01003647 }
3648
3649 private int getBigTextLayoutResource() {
Jorim Jaggi445d3c02014-08-19 22:33:42 +02003650 return R.layout.notification_template_material_big_text;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01003651 }
3652
3653 private int getInboxLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07003654 return R.layout.notification_template_material_inbox;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01003655 }
3656
3657 private int getActionLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07003658 return R.layout.notification_material_action;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01003659 }
3660
3661 private int getActionTombstoneLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07003662 return R.layout.notification_material_action_tombstone;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01003663 }
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003664 }
3665
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003666 /**
3667 * An object that can apply a rich notification style to a {@link Notification.Builder}
3668 * object.
3669 */
Griff Hazendfcb0802014-02-11 12:00:00 -08003670 public static abstract class Style {
Chris Wrend6297db2012-05-03 16:20:13 -04003671 private CharSequence mBigContentTitle;
Jorim Jaggi457a10d2014-09-08 16:18:23 +02003672
3673 /**
3674 * @hide
3675 */
3676 protected CharSequence mSummaryText = null;
3677
3678 /**
3679 * @hide
3680 */
3681 protected boolean mSummaryTextSet = false;
Chris Wrend6297db2012-05-03 16:20:13 -04003682
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003683 protected Builder mBuilder;
3684
Chris Wrend6297db2012-05-03 16:20:13 -04003685 /**
3686 * Overrides ContentTitle in the big form of the template.
3687 * This defaults to the value passed to setContentTitle().
3688 */
3689 protected void internalSetBigContentTitle(CharSequence title) {
3690 mBigContentTitle = title;
3691 }
3692
3693 /**
3694 * Set the first line of text after the detail section in the big form of the template.
3695 */
3696 protected void internalSetSummaryText(CharSequence cs) {
3697 mSummaryText = cs;
Daniel Sandler619738c2012-06-07 16:33:08 -04003698 mSummaryTextSet = true;
Chris Wrend6297db2012-05-03 16:20:13 -04003699 }
3700
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003701 public void setBuilder(Builder builder) {
3702 if (mBuilder != builder) {
3703 mBuilder = builder;
Daniel Sandlerc08dea22012-06-28 08:35:24 -07003704 if (mBuilder != null) {
3705 mBuilder.setStyle(this);
3706 }
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003707 }
3708 }
3709
Chris Wrend6297db2012-05-03 16:20:13 -04003710 protected void checkBuilder() {
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003711 if (mBuilder == null) {
3712 throw new IllegalArgumentException("Style requires a valid Builder object");
3713 }
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003714 }
Chris Wrend6297db2012-05-03 16:20:13 -04003715
3716 protected RemoteViews getStandardView(int layoutId) {
3717 checkBuilder();
3718
Christoph Studer4600f9b2014-07-22 22:44:43 +02003719 // Nasty.
3720 CharSequence oldBuilderContentTitle = mBuilder.mContentTitle;
Chris Wrend6297db2012-05-03 16:20:13 -04003721 if (mBigContentTitle != null) {
3722 mBuilder.setContentTitle(mBigContentTitle);
3723 }
3724
Chris Wrend6297db2012-05-03 16:20:13 -04003725 RemoteViews contentView = mBuilder.applyStandardTemplateWithActions(layoutId);
3726
Christoph Studer4600f9b2014-07-22 22:44:43 +02003727 mBuilder.mContentTitle = oldBuilderContentTitle;
3728
Chris Wrend6297db2012-05-03 16:20:13 -04003729 if (mBigContentTitle != null && mBigContentTitle.equals("")) {
3730 contentView.setViewVisibility(R.id.line1, View.GONE);
Chris Wren67dc9a02012-05-16 01:03:20 -04003731 } else {
3732 contentView.setViewVisibility(R.id.line1, View.VISIBLE);
Chris Wrend6297db2012-05-03 16:20:13 -04003733 }
3734
Daniel Sandler619738c2012-06-07 16:33:08 -04003735 // The last line defaults to the subtext, but can be replaced by mSummaryText
3736 final CharSequence overflowText =
3737 mSummaryTextSet ? mSummaryText
3738 : mBuilder.mSubText;
3739 if (overflowText != null) {
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01003740 contentView.setTextViewText(R.id.text, mBuilder.processLegacyText(overflowText));
Daniel Sandler619738c2012-06-07 16:33:08 -04003741 contentView.setViewVisibility(R.id.overflow_divider, View.VISIBLE);
Daniel Sandler9f7936a2012-05-21 16:14:28 -04003742 contentView.setViewVisibility(R.id.line3, View.VISIBLE);
Daniel Sandler916ad912012-06-13 12:17:07 -04003743 } else {
Kenny Guy98193ea2014-07-24 19:54:37 +01003744 // Clear text in case we use the line to show the profile badge.
3745 contentView.setTextViewText(R.id.text, "");
Daniel Sandler916ad912012-06-13 12:17:07 -04003746 contentView.setViewVisibility(R.id.overflow_divider, View.GONE);
3747 contentView.setViewVisibility(R.id.line3, View.GONE);
Chris Wrend6297db2012-05-03 16:20:13 -04003748 }
3749
3750 return contentView;
3751 }
3752
Daniel Sandlerf45564e2013-04-15 15:05:08 -04003753 /**
Jorim Jaggi445d3c02014-08-19 22:33:42 +02003754 * Changes the padding of the first line such that the big and small content view have the
3755 * same top padding.
3756 *
3757 * @hide
3758 */
3759 protected void applyTopPadding(RemoteViews contentView) {
3760 int topPadding = Builder.calculateTopPadding(mBuilder.mContext,
Jorim Jaggid05aa3e2014-08-28 17:52:27 +02003761 mBuilder.mHasThreeLines,
Jorim Jaggi445d3c02014-08-19 22:33:42 +02003762 mBuilder.mContext.getResources().getConfiguration().fontScale);
3763 contentView.setViewPadding(R.id.line1, 0, topPadding, 0, 0);
3764 }
3765
3766 /**
Daniel Sandlerf45564e2013-04-15 15:05:08 -04003767 * @hide
3768 */
3769 public void addExtras(Bundle extras) {
3770 if (mSummaryTextSet) {
3771 extras.putCharSequence(EXTRA_SUMMARY_TEXT, mSummaryText);
3772 }
3773 if (mBigContentTitle != null) {
3774 extras.putCharSequence(EXTRA_TITLE_BIG, mBigContentTitle);
3775 }
Chris Wren91ad5632013-06-05 15:05:57 -04003776 extras.putString(EXTRA_TEMPLATE, this.getClass().getName());
Daniel Sandlerf45564e2013-04-15 15:05:08 -04003777 }
3778
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04003779 /**
3780 * @hide
3781 */
Christoph Studer4600f9b2014-07-22 22:44:43 +02003782 protected void restoreFromExtras(Bundle extras) {
3783 if (extras.containsKey(EXTRA_SUMMARY_TEXT)) {
3784 mSummaryText = extras.getCharSequence(EXTRA_SUMMARY_TEXT);
3785 mSummaryTextSet = true;
3786 }
3787 if (extras.containsKey(EXTRA_TITLE_BIG)) {
3788 mBigContentTitle = extras.getCharSequence(EXTRA_TITLE_BIG);
3789 }
3790 }
3791
3792
3793 /**
3794 * @hide
3795 */
3796 public Notification buildStyled(Notification wip) {
3797 populateTickerView(wip);
3798 populateContentView(wip);
3799 populateBigContentView(wip);
3800 populateHeadsUpContentView(wip);
3801 return wip;
3802 }
3803
Daniel Sandler0ec46202015-06-24 01:27:05 -04003804 /**
3805 * @hide
3806 */
Jorim Jaggia0d58ae2015-06-03 11:48:13 -07003807 public void purgeResources() {}
3808
Christoph Studer4600f9b2014-07-22 22:44:43 +02003809 // The following methods are split out so we can re-create notification partially.
3810 /**
3811 * @hide
3812 */
3813 protected void populateTickerView(Notification wip) {}
3814 /**
3815 * @hide
3816 */
3817 protected void populateContentView(Notification wip) {}
3818
3819 /**
3820 * @hide
3821 */
3822 protected void populateBigContentView(Notification wip) {}
3823
3824 /**
3825 * @hide
3826 */
3827 protected void populateHeadsUpContentView(Notification wip) {}
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04003828
3829 /**
3830 * Calls {@link android.app.Notification.Builder#build()} on the Builder this Style is
3831 * attached to.
3832 *
3833 * @return the fully constructed Notification.
3834 */
3835 public Notification build() {
3836 checkBuilder();
3837 return mBuilder.build();
3838 }
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02003839
3840 /**
3841 * @hide
3842 * @return true if the style positions the progress bar on the second line; false if the
3843 * style hides the progress bar
3844 */
3845 protected boolean hasProgress() {
3846 return true;
3847 }
Joe Onorato46439ce2010-11-19 13:56:21 -08003848 }
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003849
3850 /**
Daniel Sandler4dfbe832012-04-11 14:51:46 -04003851 * Helper class for generating large-format notifications that include a large image attachment.
Joe Malin8d40d042012-11-05 11:36:40 -08003852 *
Robert Ly91c5ce32014-06-08 15:37:00 -07003853 * Here's how you'd set the <code>BigPictureStyle</code> on a notification:
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003854 * <pre class="prettyprint">
Robert Ly91c5ce32014-06-08 15:37:00 -07003855 * Notification notif = new Notification.Builder(mContext)
3856 * .setContentTitle(&quot;New photo from &quot; + sender.toString())
3857 * .setContentText(subject)
3858 * .setSmallIcon(R.drawable.new_post)
3859 * .setLargeIcon(aBitmap)
3860 * .setStyle(new Notification.BigPictureStyle()
3861 * .bigPicture(aBigBitmap))
3862 * .build();
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003863 * </pre>
Joe Malin8d40d042012-11-05 11:36:40 -08003864 *
Daniel Sandler4dfbe832012-04-11 14:51:46 -04003865 * @see Notification#bigContentView
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003866 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003867 public static class BigPictureStyle extends Style {
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003868 private Bitmap mPicture;
Dan Sandlerd63f9322015-05-06 15:18:49 -04003869 private Icon mBigLargeIcon;
Chris Wren3745a3d2012-05-22 15:11:52 -04003870 private boolean mBigLargeIconSet = false;
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003871
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003872 public BigPictureStyle() {
3873 }
3874
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003875 public BigPictureStyle(Builder builder) {
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003876 setBuilder(builder);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003877 }
3878
Chris Wrend6297db2012-05-03 16:20:13 -04003879 /**
3880 * Overrides ContentTitle in the big form of the template.
3881 * This defaults to the value passed to setContentTitle().
3882 */
3883 public BigPictureStyle setBigContentTitle(CharSequence title) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04003884 internalSetBigContentTitle(safeCharSequence(title));
Chris Wrend6297db2012-05-03 16:20:13 -04003885 return this;
3886 }
3887
3888 /**
3889 * Set the first line of text after the detail section in the big form of the template.
3890 */
3891 public BigPictureStyle setSummaryText(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04003892 internalSetSummaryText(safeCharSequence(cs));
Chris Wrend6297db2012-05-03 16:20:13 -04003893 return this;
3894 }
3895
Chris Wren0bd664d2012-08-01 13:56:56 -04003896 /**
3897 * Provide the bitmap to be used as the payload for the BigPicture notification.
3898 */
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003899 public BigPictureStyle bigPicture(Bitmap b) {
3900 mPicture = b;
3901 return this;
3902 }
3903
Chris Wren3745a3d2012-05-22 15:11:52 -04003904 /**
Chris Wren3745a3d2012-05-22 15:11:52 -04003905 * Override the large icon when the big notification is shown.
3906 */
3907 public BigPictureStyle bigLargeIcon(Bitmap b) {
Dan Sandlerd63f9322015-05-06 15:18:49 -04003908 return bigLargeIcon(b != null ? Icon.createWithBitmap(b) : null);
3909 }
3910
3911 /**
3912 * Override the large icon when the big notification is shown.
3913 */
3914 public BigPictureStyle bigLargeIcon(Icon icon) {
Chris Wren3745a3d2012-05-22 15:11:52 -04003915 mBigLargeIconSet = true;
Dan Sandlerd63f9322015-05-06 15:18:49 -04003916 mBigLargeIcon = icon;
Chris Wren3745a3d2012-05-22 15:11:52 -04003917 return this;
3918 }
3919
Daniel Sandler0ec46202015-06-24 01:27:05 -04003920 /**
3921 * @hide
3922 */
Jorim Jaggia0d58ae2015-06-03 11:48:13 -07003923 @Override
3924 public void purgeResources() {
3925 super.purgeResources();
3926 if (mPicture != null && mPicture.isMutable()) {
3927 mPicture = mPicture.createAshmemBitmap();
3928 }
3929 if (mBigLargeIcon != null) {
3930 mBigLargeIcon.convertToAshmem();
3931 }
3932 }
Christoph Studer5c510ee2014-12-15 16:32:27 +01003933
Jorim Jaggia0d58ae2015-06-03 11:48:13 -07003934 private RemoteViews makeBigContentView() {
Christoph Studer5c510ee2014-12-15 16:32:27 +01003935 // Replace mLargeIcon with mBigLargeIcon if mBigLargeIconSet
3936 // This covers the following cases:
3937 // 1. mBigLargeIconSet -> mBigLargeIcon (null or non-null) applies, overrides
3938 // mLargeIcon
3939 // 2. !mBigLargeIconSet -> mLargeIcon applies
Dan Sandlerd63f9322015-05-06 15:18:49 -04003940 Icon oldLargeIcon = null;
Christoph Studer5c510ee2014-12-15 16:32:27 +01003941 if (mBigLargeIconSet) {
3942 oldLargeIcon = mBuilder.mLargeIcon;
3943 mBuilder.mLargeIcon = mBigLargeIcon;
3944 }
3945
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01003946 RemoteViews contentView = getStandardView(mBuilder.getBigPictureLayoutResource());
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003947
Christoph Studer5c510ee2014-12-15 16:32:27 +01003948 if (mBigLargeIconSet) {
3949 mBuilder.mLargeIcon = oldLargeIcon;
3950 }
3951
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003952 contentView.setImageViewBitmap(R.id.big_picture, mPicture);
3953
Jorim Jaggi445d3c02014-08-19 22:33:42 +02003954 applyTopPadding(contentView);
3955
Kenny Guy98193ea2014-07-24 19:54:37 +01003956 boolean twoTextLines = mBuilder.mSubText != null && mBuilder.mContentText != null;
3957 mBuilder.addProfileBadge(contentView,
3958 twoTextLines ? R.id.profile_badge_line2 : R.id.profile_badge_line3);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003959 return contentView;
3960 }
3961
Daniel Sandlerf45564e2013-04-15 15:05:08 -04003962 /**
3963 * @hide
3964 */
3965 public void addExtras(Bundle extras) {
3966 super.addExtras(extras);
3967
3968 if (mBigLargeIconSet) {
3969 extras.putParcelable(EXTRA_LARGE_ICON_BIG, mBigLargeIcon);
3970 }
3971 extras.putParcelable(EXTRA_PICTURE, mPicture);
3972 }
3973
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04003974 /**
3975 * @hide
3976 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003977 @Override
Christoph Studer4600f9b2014-07-22 22:44:43 +02003978 protected void restoreFromExtras(Bundle extras) {
3979 super.restoreFromExtras(extras);
3980
3981 if (extras.containsKey(EXTRA_LARGE_ICON_BIG)) {
Christoph Studer5c510ee2014-12-15 16:32:27 +01003982 mBigLargeIconSet = true;
Christoph Studer4600f9b2014-07-22 22:44:43 +02003983 mBigLargeIcon = extras.getParcelable(EXTRA_LARGE_ICON_BIG);
Chris Wren3745a3d2012-05-22 15:11:52 -04003984 }
Christoph Studer4600f9b2014-07-22 22:44:43 +02003985 mPicture = extras.getParcelable(EXTRA_PICTURE);
3986 }
3987
3988 /**
3989 * @hide
3990 */
3991 @Override
3992 public void populateBigContentView(Notification wip) {
3993 mBuilder.setBuilderBigContentView(wip, makeBigContentView());
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003994 }
3995 }
3996
3997 /**
Daniel Sandler4dfbe832012-04-11 14:51:46 -04003998 * Helper class for generating large-format notifications that include a lot of text.
Joe Malin8d40d042012-11-05 11:36:40 -08003999 *
Robert Ly91c5ce32014-06-08 15:37:00 -07004000 * Here's how you'd set the <code>BigTextStyle</code> on a notification:
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004001 * <pre class="prettyprint">
Robert Ly91c5ce32014-06-08 15:37:00 -07004002 * Notification notif = new Notification.Builder(mContext)
4003 * .setContentTitle(&quot;New mail from &quot; + sender.toString())
4004 * .setContentText(subject)
4005 * .setSmallIcon(R.drawable.new_mail)
4006 * .setLargeIcon(aBitmap)
4007 * .setStyle(new Notification.BigTextStyle()
4008 * .bigText(aVeryLongString))
4009 * .build();
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004010 * </pre>
Joe Malin8d40d042012-11-05 11:36:40 -08004011 *
Daniel Sandler4dfbe832012-04-11 14:51:46 -04004012 * @see Notification#bigContentView
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004013 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004014 public static class BigTextStyle extends Style {
Jorim Jaggi457a10d2014-09-08 16:18:23 +02004015
4016 private static final int MAX_LINES = 13;
4017 private static final int LINES_CONSUMED_BY_ACTIONS = 3;
4018 private static final int LINES_CONSUMED_BY_SUMMARY = 2;
4019
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004020 private CharSequence mBigText;
4021
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004022 public BigTextStyle() {
4023 }
4024
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004025 public BigTextStyle(Builder builder) {
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004026 setBuilder(builder);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004027 }
4028
Chris Wrend6297db2012-05-03 16:20:13 -04004029 /**
4030 * Overrides ContentTitle in the big form of the template.
4031 * This defaults to the value passed to setContentTitle().
4032 */
4033 public BigTextStyle setBigContentTitle(CharSequence title) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04004034 internalSetBigContentTitle(safeCharSequence(title));
Chris Wrend6297db2012-05-03 16:20:13 -04004035 return this;
4036 }
4037
4038 /**
4039 * Set the first line of text after the detail section in the big form of the template.
4040 */
4041 public BigTextStyle setSummaryText(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04004042 internalSetSummaryText(safeCharSequence(cs));
Chris Wrend6297db2012-05-03 16:20:13 -04004043 return this;
4044 }
4045
Chris Wren0bd664d2012-08-01 13:56:56 -04004046 /**
4047 * Provide the longer text to be displayed in the big form of the
4048 * template in place of the content text.
4049 */
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004050 public BigTextStyle bigText(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04004051 mBigText = safeCharSequence(cs);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004052 return this;
4053 }
4054
Daniel Sandlerf45564e2013-04-15 15:05:08 -04004055 /**
4056 * @hide
4057 */
4058 public void addExtras(Bundle extras) {
4059 super.addExtras(extras);
4060
Christoph Studer4600f9b2014-07-22 22:44:43 +02004061 extras.putCharSequence(EXTRA_BIG_TEXT, mBigText);
4062 }
4063
4064 /**
4065 * @hide
4066 */
4067 @Override
4068 protected void restoreFromExtras(Bundle extras) {
4069 super.restoreFromExtras(extras);
4070
4071 mBigText = extras.getCharSequence(EXTRA_BIG_TEXT);
Daniel Sandlerf45564e2013-04-15 15:05:08 -04004072 }
4073
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004074 private RemoteViews makeBigContentView() {
Christoph Studer4600f9b2014-07-22 22:44:43 +02004075
4076 // Nasty
4077 CharSequence oldBuilderContentText = mBuilder.mContentText;
Daniel Sandler6387d2f2012-05-22 13:44:09 -04004078 mBuilder.mContentText = null;
Daniel Sandler916ad912012-06-13 12:17:07 -04004079
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01004080 RemoteViews contentView = getStandardView(mBuilder.getBigTextLayoutResource());
Joe Malin8d40d042012-11-05 11:36:40 -08004081
Christoph Studer4600f9b2014-07-22 22:44:43 +02004082 mBuilder.mContentText = oldBuilderContentText;
4083
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01004084 contentView.setTextViewText(R.id.big_text, mBuilder.processLegacyText(mBigText));
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004085 contentView.setViewVisibility(R.id.big_text, View.VISIBLE);
Jorim Jaggi457a10d2014-09-08 16:18:23 +02004086 contentView.setInt(R.id.big_text, "setMaxLines", calculateMaxLines());
Chris Wren3c5f92432012-05-04 16:31:17 -04004087 contentView.setViewVisibility(R.id.text2, View.GONE);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004088
Jorim Jaggi445d3c02014-08-19 22:33:42 +02004089 applyTopPadding(contentView);
4090
Jorim Jaggid05aa3e2014-08-28 17:52:27 +02004091 mBuilder.shrinkLine3Text(contentView);
4092
Kenny Guy98193ea2014-07-24 19:54:37 +01004093 mBuilder.addProfileBadge(contentView, R.id.profile_badge_large_template);
4094
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004095 return contentView;
4096 }
4097
Jorim Jaggi457a10d2014-09-08 16:18:23 +02004098 private int calculateMaxLines() {
4099 int lineCount = MAX_LINES;
4100 boolean hasActions = mBuilder.mActions.size() > 0;
4101 boolean hasSummary = (mSummaryTextSet ? mSummaryText : mBuilder.mSubText) != null;
4102 if (hasActions) {
4103 lineCount -= LINES_CONSUMED_BY_ACTIONS;
4104 }
4105 if (hasSummary) {
4106 lineCount -= LINES_CONSUMED_BY_SUMMARY;
4107 }
4108
4109 // If we have less top padding at the top, we can fit less lines.
4110 if (!mBuilder.mHasThreeLines) {
4111 lineCount--;
4112 }
4113 return lineCount;
4114 }
4115
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04004116 /**
4117 * @hide
4118 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004119 @Override
Christoph Studer4600f9b2014-07-22 22:44:43 +02004120 public void populateBigContentView(Notification wip) {
4121 mBuilder.setBuilderBigContentView(wip, makeBigContentView());
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004122 }
4123 }
Daniel Sandler879c5e02012-04-17 16:46:51 -04004124
4125 /**
4126 * Helper class for generating large-format notifications that include a list of (up to 5) strings.
Joe Malin8d40d042012-11-05 11:36:40 -08004127 *
Robert Ly91c5ce32014-06-08 15:37:00 -07004128 * Here's how you'd set the <code>InboxStyle</code> on a notification:
Daniel Sandler879c5e02012-04-17 16:46:51 -04004129 * <pre class="prettyprint">
Robert Ly91c5ce32014-06-08 15:37:00 -07004130 * Notification notif = new Notification.Builder(mContext)
4131 * .setContentTitle(&quot;5 New mails from &quot; + sender.toString())
4132 * .setContentText(subject)
4133 * .setSmallIcon(R.drawable.new_mail)
4134 * .setLargeIcon(aBitmap)
4135 * .setStyle(new Notification.InboxStyle()
4136 * .addLine(str1)
4137 * .addLine(str2)
4138 * .setContentTitle(&quot;&quot;)
4139 * .setSummaryText(&quot;+3 more&quot;))
4140 * .build();
Daniel Sandler879c5e02012-04-17 16:46:51 -04004141 * </pre>
Joe Malin8d40d042012-11-05 11:36:40 -08004142 *
Daniel Sandler879c5e02012-04-17 16:46:51 -04004143 * @see Notification#bigContentView
4144 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004145 public static class InboxStyle extends Style {
Daniel Sandler879c5e02012-04-17 16:46:51 -04004146 private ArrayList<CharSequence> mTexts = new ArrayList<CharSequence>(5);
4147
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004148 public InboxStyle() {
4149 }
4150
Daniel Sandler879c5e02012-04-17 16:46:51 -04004151 public InboxStyle(Builder builder) {
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004152 setBuilder(builder);
Daniel Sandler879c5e02012-04-17 16:46:51 -04004153 }
4154
Chris Wrend6297db2012-05-03 16:20:13 -04004155 /**
4156 * Overrides ContentTitle in the big form of the template.
4157 * This defaults to the value passed to setContentTitle().
4158 */
4159 public InboxStyle setBigContentTitle(CharSequence title) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04004160 internalSetBigContentTitle(safeCharSequence(title));
Chris Wrend6297db2012-05-03 16:20:13 -04004161 return this;
4162 }
4163
4164 /**
4165 * Set the first line of text after the detail section in the big form of the template.
4166 */
4167 public InboxStyle setSummaryText(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04004168 internalSetSummaryText(safeCharSequence(cs));
Chris Wrend6297db2012-05-03 16:20:13 -04004169 return this;
4170 }
4171
Chris Wren0bd664d2012-08-01 13:56:56 -04004172 /**
4173 * Append a line to the digest section of the Inbox notification.
4174 */
Daniel Sandler879c5e02012-04-17 16:46:51 -04004175 public InboxStyle addLine(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04004176 mTexts.add(safeCharSequence(cs));
Daniel Sandler879c5e02012-04-17 16:46:51 -04004177 return this;
4178 }
4179
Daniel Sandlerf45564e2013-04-15 15:05:08 -04004180 /**
4181 * @hide
4182 */
4183 public void addExtras(Bundle extras) {
4184 super.addExtras(extras);
Christoph Studer4600f9b2014-07-22 22:44:43 +02004185
Daniel Sandlerf45564e2013-04-15 15:05:08 -04004186 CharSequence[] a = new CharSequence[mTexts.size()];
4187 extras.putCharSequenceArray(EXTRA_TEXT_LINES, mTexts.toArray(a));
4188 }
4189
Christoph Studer4600f9b2014-07-22 22:44:43 +02004190 /**
4191 * @hide
4192 */
4193 @Override
4194 protected void restoreFromExtras(Bundle extras) {
4195 super.restoreFromExtras(extras);
4196
4197 mTexts.clear();
4198 if (extras.containsKey(EXTRA_TEXT_LINES)) {
4199 Collections.addAll(mTexts, extras.getCharSequenceArray(EXTRA_TEXT_LINES));
4200 }
4201 }
4202
Daniel Sandler879c5e02012-04-17 16:46:51 -04004203 private RemoteViews makeBigContentView() {
Daniel Sandler619738c2012-06-07 16:33:08 -04004204 // Remove the content text so line3 disappears unless you have a summary
Christoph Studer4600f9b2014-07-22 22:44:43 +02004205
4206 // Nasty
4207 CharSequence oldBuilderContentText = mBuilder.mContentText;
Daniel Sandler619738c2012-06-07 16:33:08 -04004208 mBuilder.mContentText = null;
Christoph Studer4600f9b2014-07-22 22:44:43 +02004209
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01004210 RemoteViews contentView = getStandardView(mBuilder.getInboxLayoutResource());
Daniel Sandler619738c2012-06-07 16:33:08 -04004211
Christoph Studer4600f9b2014-07-22 22:44:43 +02004212 mBuilder.mContentText = oldBuilderContentText;
4213
Chris Wrend6297db2012-05-03 16:20:13 -04004214 contentView.setViewVisibility(R.id.text2, View.GONE);
Daniel Sandler879c5e02012-04-17 16:46:51 -04004215
Chris Wrend6297db2012-05-03 16:20:13 -04004216 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 -04004217 R.id.inbox_text4, R.id.inbox_text5, R.id.inbox_text6};
Chris Wrend6297db2012-05-03 16:20:13 -04004218
Chris Wren4ed80d52012-05-17 09:30:03 -04004219 // Make sure all rows are gone in case we reuse a view.
4220 for (int rowId : rowIds) {
4221 contentView.setViewVisibility(rowId, View.GONE);
4222 }
4223
Jorim Jaggi445d3c02014-08-19 22:33:42 +02004224 final boolean largeText =
4225 mBuilder.mContext.getResources().getConfiguration().fontScale > 1f;
4226 final float subTextSize = mBuilder.mContext.getResources().getDimensionPixelSize(
4227 R.dimen.notification_subtext_size);
Daniel Sandler879c5e02012-04-17 16:46:51 -04004228 int i=0;
4229 while (i < mTexts.size() && i < rowIds.length) {
4230 CharSequence str = mTexts.get(i);
4231 if (str != null && !str.equals("")) {
4232 contentView.setViewVisibility(rowIds[i], View.VISIBLE);
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01004233 contentView.setTextViewText(rowIds[i], mBuilder.processLegacyText(str));
Jorim Jaggi445d3c02014-08-19 22:33:42 +02004234 if (largeText) {
4235 contentView.setTextViewTextSize(rowIds[i], TypedValue.COMPLEX_UNIT_PX,
4236 subTextSize);
4237 }
Daniel Sandler879c5e02012-04-17 16:46:51 -04004238 }
4239 i++;
4240 }
4241
Chris Wren683ab002012-09-20 10:35:54 -04004242 contentView.setViewVisibility(R.id.inbox_end_pad,
4243 mTexts.size() > 0 ? View.VISIBLE : View.GONE);
4244
4245 contentView.setViewVisibility(R.id.inbox_more,
4246 mTexts.size() > rowIds.length ? View.VISIBLE : View.GONE);
Chris Wren29bb6d92012-05-17 18:09:42 -04004247
Jorim Jaggi445d3c02014-08-19 22:33:42 +02004248 applyTopPadding(contentView);
4249
Jorim Jaggid05aa3e2014-08-28 17:52:27 +02004250 mBuilder.shrinkLine3Text(contentView);
4251
Kenny Guy98193ea2014-07-24 19:54:37 +01004252 mBuilder.addProfileBadge(contentView, R.id.profile_badge_large_template);
4253
Daniel Sandler879c5e02012-04-17 16:46:51 -04004254 return contentView;
4255 }
4256
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04004257 /**
4258 * @hide
4259 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004260 @Override
Christoph Studer4600f9b2014-07-22 22:44:43 +02004261 public void populateBigContentView(Notification wip) {
4262 mBuilder.setBuilderBigContentView(wip, makeBigContentView());
Daniel Sandler879c5e02012-04-17 16:46:51 -04004263 }
4264 }
Dan Sandler842dd772014-05-15 09:36:47 -04004265
4266 /**
4267 * Notification style for media playback notifications.
4268 *
4269 * In the expanded form, {@link Notification#bigContentView}, up to 5
4270 * {@link Notification.Action}s specified with
4271 * {@link Notification.Builder#addAction(int, CharSequence, PendingIntent) addAction} will be
4272 * shown as icon-only pushbuttons, suitable for transport controls. The Bitmap given to
4273 * {@link Notification.Builder#setLargeIcon(android.graphics.Bitmap) setLargeIcon()} will be
4274 * treated as album artwork.
4275 *
4276 * Unlike the other styles provided here, MediaStyle can also modify the standard-size
4277 * {@link Notification#contentView}; by providing action indices to
Christoph Studerfde6f4d2014-12-12 13:23:26 +01004278 * {@link #setShowActionsInCompactView(int...)} you can promote up to 3 actions to be displayed
Dan Sandler842dd772014-05-15 09:36:47 -04004279 * in the standard view alongside the usual content.
4280 *
Bryan Mawhinney6be8de32014-07-18 10:35:12 +01004281 * Notifications created with MediaStyle will have their category set to
4282 * {@link Notification#CATEGORY_TRANSPORT CATEGORY_TRANSPORT} unless you set a different
4283 * category using {@link Notification.Builder#setCategory(String) setCategory()}.
4284 *
Jeff Browndba34ba2014-06-24 20:46:03 -07004285 * Finally, if you attach a {@link android.media.session.MediaSession.Token} using
4286 * {@link android.app.Notification.MediaStyle#setMediaSession(MediaSession.Token)},
Dan Sandler842dd772014-05-15 09:36:47 -04004287 * the System UI can identify this as a notification representing an active media session
4288 * and respond accordingly (by showing album artwork in the lockscreen, for example).
4289 *
4290 * To use this style with your Notification, feed it to
4291 * {@link Notification.Builder#setStyle(android.app.Notification.Style)} like so:
4292 * <pre class="prettyprint">
4293 * Notification noti = new Notification.Builder()
4294 * .setSmallIcon(R.drawable.ic_stat_player)
Christoph Studere935fe92014-11-24 14:18:06 +01004295 * .setContentTitle(&quot;Track title&quot;)
4296 * .setContentText(&quot;Artist - Album&quot;)
4297 * .setLargeIcon(albumArtBitmap))
Bryan Mawhinney6be8de32014-07-18 10:35:12 +01004298 * .setStyle(<b>new Notification.MediaStyle()</b>
4299 * .setMediaSession(mySession))
Dan Sandler842dd772014-05-15 09:36:47 -04004300 * .build();
4301 * </pre>
4302 *
4303 * @see Notification#bigContentView
4304 */
4305 public static class MediaStyle extends Style {
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02004306 static final int MAX_MEDIA_BUTTONS_IN_COMPACT = 3;
Dan Sandler842dd772014-05-15 09:36:47 -04004307 static final int MAX_MEDIA_BUTTONS = 5;
4308
4309 private int[] mActionsToShowInCompact = null;
Jeff Browndba34ba2014-06-24 20:46:03 -07004310 private MediaSession.Token mToken;
Dan Sandler842dd772014-05-15 09:36:47 -04004311
4312 public MediaStyle() {
4313 }
4314
4315 public MediaStyle(Builder builder) {
4316 setBuilder(builder);
4317 }
4318
4319 /**
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02004320 * 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 -04004321 * notification view.
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02004322 *
4323 * @param actions the indices of the actions to show in the compact notification view
Dan Sandler842dd772014-05-15 09:36:47 -04004324 */
4325 public MediaStyle setShowActionsInCompactView(int...actions) {
4326 mActionsToShowInCompact = actions;
4327 return this;
4328 }
4329
4330 /**
Jeff Browndba34ba2014-06-24 20:46:03 -07004331 * Attach a {@link android.media.session.MediaSession.Token} to this Notification
4332 * to provide additional playback information and control to the SystemUI.
Dan Sandler842dd772014-05-15 09:36:47 -04004333 */
Jeff Browndba34ba2014-06-24 20:46:03 -07004334 public MediaStyle setMediaSession(MediaSession.Token token) {
Dan Sandler842dd772014-05-15 09:36:47 -04004335 mToken = token;
4336 return this;
4337 }
4338
Christoph Studer4600f9b2014-07-22 22:44:43 +02004339 /**
4340 * @hide
4341 */
Dan Sandler842dd772014-05-15 09:36:47 -04004342 @Override
4343 public Notification buildStyled(Notification wip) {
Christoph Studer4600f9b2014-07-22 22:44:43 +02004344 super.buildStyled(wip);
Bryan Mawhinney6be8de32014-07-18 10:35:12 +01004345 if (wip.category == null) {
4346 wip.category = Notification.CATEGORY_TRANSPORT;
4347 }
Dan Sandler842dd772014-05-15 09:36:47 -04004348 return wip;
4349 }
4350
Christoph Studer4600f9b2014-07-22 22:44:43 +02004351 /**
4352 * @hide
4353 */
4354 @Override
4355 public void populateContentView(Notification wip) {
4356 mBuilder.setBuilderContentView(wip, makeMediaContentView());
4357 }
4358
4359 /**
4360 * @hide
4361 */
4362 @Override
4363 public void populateBigContentView(Notification wip) {
4364 mBuilder.setBuilderBigContentView(wip, makeMediaBigContentView());
4365 }
4366
Dan Sandler842dd772014-05-15 09:36:47 -04004367 /** @hide */
4368 @Override
4369 public void addExtras(Bundle extras) {
4370 super.addExtras(extras);
4371
4372 if (mToken != null) {
4373 extras.putParcelable(EXTRA_MEDIA_SESSION, mToken);
4374 }
Bryan Mawhinneye191f902014-07-22 12:50:09 +01004375 if (mActionsToShowInCompact != null) {
4376 extras.putIntArray(EXTRA_COMPACT_ACTIONS, mActionsToShowInCompact);
4377 }
Dan Sandler842dd772014-05-15 09:36:47 -04004378 }
4379
Christoph Studer4600f9b2014-07-22 22:44:43 +02004380 /**
4381 * @hide
4382 */
4383 @Override
4384 protected void restoreFromExtras(Bundle extras) {
4385 super.restoreFromExtras(extras);
4386
4387 if (extras.containsKey(EXTRA_MEDIA_SESSION)) {
4388 mToken = extras.getParcelable(EXTRA_MEDIA_SESSION);
4389 }
4390 if (extras.containsKey(EXTRA_COMPACT_ACTIONS)) {
4391 mActionsToShowInCompact = extras.getIntArray(EXTRA_COMPACT_ACTIONS);
4392 }
4393 }
4394
Dan Sandler842dd772014-05-15 09:36:47 -04004395 private RemoteViews generateMediaActionButton(Action action) {
4396 final boolean tombstone = (action.actionIntent == null);
4397 RemoteViews button = new RemoteViews(mBuilder.mContext.getPackageName(),
Alan Viverette3cb07a462014-06-06 14:19:53 -07004398 R.layout.notification_material_media_action);
Dan Sandler842dd772014-05-15 09:36:47 -04004399 button.setImageViewResource(R.id.action0, action.icon);
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02004400 button.setDrawableParameters(R.id.action0, false, -1,
4401 0xFFFFFFFF,
4402 PorterDuff.Mode.SRC_ATOP, -1);
Dan Sandler842dd772014-05-15 09:36:47 -04004403 if (!tombstone) {
4404 button.setOnClickPendingIntent(R.id.action0, action.actionIntent);
4405 }
4406 button.setContentDescription(R.id.action0, action.title);
4407 return button;
4408 }
4409
4410 private RemoteViews makeMediaContentView() {
4411 RemoteViews view = mBuilder.applyStandardTemplate(
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02004412 R.layout.notification_template_material_media, false /* hasProgress */);
Dan Sandler842dd772014-05-15 09:36:47 -04004413
4414 final int numActions = mBuilder.mActions.size();
4415 final int N = mActionsToShowInCompact == null
4416 ? 0
4417 : Math.min(mActionsToShowInCompact.length, MAX_MEDIA_BUTTONS_IN_COMPACT);
4418 if (N > 0) {
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02004419 view.removeAllViews(com.android.internal.R.id.media_actions);
Dan Sandler842dd772014-05-15 09:36:47 -04004420 for (int i = 0; i < N; i++) {
4421 if (i >= numActions) {
4422 throw new IllegalArgumentException(String.format(
4423 "setShowActionsInCompactView: action %d out of bounds (max %d)",
4424 i, numActions - 1));
4425 }
4426
4427 final Action action = mBuilder.mActions.get(mActionsToShowInCompact[i]);
4428 final RemoteViews button = generateMediaActionButton(action);
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02004429 view.addView(com.android.internal.R.id.media_actions, button);
Dan Sandler842dd772014-05-15 09:36:47 -04004430 }
4431 }
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02004432 styleText(view);
4433 hideRightIcon(view);
Dan Sandler842dd772014-05-15 09:36:47 -04004434 return view;
4435 }
4436
4437 private RemoteViews makeMediaBigContentView() {
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02004438 final int actionCount = Math.min(mBuilder.mActions.size(), MAX_MEDIA_BUTTONS);
4439 RemoteViews big = mBuilder.applyStandardTemplate(getBigLayoutResource(actionCount),
4440 false /* hasProgress */);
Dan Sandler842dd772014-05-15 09:36:47 -04004441
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02004442 if (actionCount > 0) {
4443 big.removeAllViews(com.android.internal.R.id.media_actions);
4444 for (int i = 0; i < actionCount; i++) {
Dan Sandler842dd772014-05-15 09:36:47 -04004445 final RemoteViews button = generateMediaActionButton(mBuilder.mActions.get(i));
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02004446 big.addView(com.android.internal.R.id.media_actions, button);
Dan Sandler842dd772014-05-15 09:36:47 -04004447 }
4448 }
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02004449 styleText(big);
4450 hideRightIcon(big);
4451 applyTopPadding(big);
4452 big.setViewVisibility(android.R.id.progress, View.GONE);
Dan Sandler842dd772014-05-15 09:36:47 -04004453 return big;
4454 }
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02004455
4456 private int getBigLayoutResource(int actionCount) {
4457 if (actionCount <= 3) {
4458 return R.layout.notification_template_material_big_media_narrow;
4459 } else {
4460 return R.layout.notification_template_material_big_media;
4461 }
4462 }
4463
4464 private void hideRightIcon(RemoteViews contentView) {
4465 contentView.setViewVisibility(R.id.right_icon, View.GONE);
4466 }
4467
4468 /**
4469 * Applies the special text colors for media notifications to all text views.
4470 */
4471 private void styleText(RemoteViews contentView) {
Alan Viverette4a357cd2015-03-18 18:37:18 -07004472 int primaryColor = mBuilder.mContext.getColor(
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02004473 R.color.notification_media_primary_color);
Alan Viverette4a357cd2015-03-18 18:37:18 -07004474 int secondaryColor = mBuilder.mContext.getColor(
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02004475 R.color.notification_media_secondary_color);
4476 contentView.setTextColor(R.id.title, primaryColor);
4477 if (mBuilder.showsTimeOrChronometer()) {
4478 if (mBuilder.mUseChronometer) {
4479 contentView.setTextColor(R.id.chronometer, secondaryColor);
4480 } else {
4481 contentView.setTextColor(R.id.time, secondaryColor);
4482 }
4483 }
4484 contentView.setTextColor(R.id.text2, secondaryColor);
4485 contentView.setTextColor(R.id.text, secondaryColor);
4486 contentView.setTextColor(R.id.info, secondaryColor);
4487 }
4488
4489 /**
4490 * @hide
4491 */
4492 @Override
4493 protected boolean hasProgress() {
4494 return false;
4495 }
Dan Sandler842dd772014-05-15 09:36:47 -04004496 }
Griff Hazen61a9e862014-05-22 16:05:19 -07004497
Christoph Studer4600f9b2014-07-22 22:44:43 +02004498 // When adding a new Style subclass here, don't forget to update
4499 // Builder.getNotificationStyleClass.
4500
Griff Hazen61a9e862014-05-22 16:05:19 -07004501 /**
4502 * Extender interface for use with {@link Builder#extend}. Extenders may be used to add
4503 * metadata or change options on a notification builder.
4504 */
4505 public interface Extender {
4506 /**
4507 * Apply this extender to a notification builder.
4508 * @param builder the builder to be modified.
4509 * @return the build object for chaining.
4510 */
4511 public Builder extend(Builder builder);
4512 }
4513
4514 /**
4515 * Helper class to add wearable extensions to notifications.
4516 * <p class="note"> See
4517 * <a href="{@docRoot}wear/notifications/creating.html">Creating Notifications
4518 * for Android Wear</a> for more information on how to use this class.
4519 * <p>
4520 * To create a notification with wearable extensions:
4521 * <ol>
4522 * <li>Create a {@link android.app.Notification.Builder}, setting any desired
4523 * properties.
4524 * <li>Create a {@link android.app.Notification.WearableExtender}.
4525 * <li>Set wearable-specific properties using the
4526 * {@code add} and {@code set} methods of {@link android.app.Notification.WearableExtender}.
4527 * <li>Call {@link android.app.Notification.Builder#extend} to apply the extensions to a
4528 * notification.
4529 * <li>Post the notification to the notification system with the
4530 * {@code NotificationManager.notify(...)} methods.
4531 * </ol>
4532 *
4533 * <pre class="prettyprint">
4534 * Notification notif = new Notification.Builder(mContext)
4535 * .setContentTitle(&quot;New mail from &quot; + sender.toString())
4536 * .setContentText(subject)
4537 * .setSmallIcon(R.drawable.new_mail)
4538 * .extend(new Notification.WearableExtender()
4539 * .setContentIcon(R.drawable.new_mail))
4540 * .build();
4541 * NotificationManager notificationManger =
4542 * (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
4543 * notificationManger.notify(0, notif);</pre>
4544 *
4545 * <p>Wearable extensions can be accessed on an existing notification by using the
4546 * {@code WearableExtender(Notification)} constructor,
4547 * and then using the {@code get} methods to access values.
4548 *
4549 * <pre class="prettyprint">
4550 * Notification.WearableExtender wearableExtender = new Notification.WearableExtender(
4551 * notification);
Griff Hazen14f57992014-05-26 09:07:14 -07004552 * List&lt;Notification&gt; pages = wearableExtender.getPages();</pre>
Griff Hazen61a9e862014-05-22 16:05:19 -07004553 */
4554 public static final class WearableExtender implements Extender {
4555 /**
4556 * Sentinel value for an action index that is unset.
4557 */
4558 public static final int UNSET_ACTION_INDEX = -1;
4559
4560 /**
4561 * Size value for use with {@link #setCustomSizePreset} to show this notification with
4562 * default sizing.
4563 * <p>For custom display notifications created using {@link #setDisplayIntent},
4564 * the default is {@link #SIZE_LARGE}. All other notifications size automatically based
4565 * on their content.
4566 */
4567 public static final int SIZE_DEFAULT = 0;
4568
4569 /**
4570 * Size value for use with {@link #setCustomSizePreset} to show this notification
4571 * with an extra small size.
4572 * <p>This value is only applicable for custom display notifications created using
4573 * {@link #setDisplayIntent}.
4574 */
4575 public static final int SIZE_XSMALL = 1;
4576
4577 /**
4578 * Size value for use with {@link #setCustomSizePreset} to show this notification
4579 * with a small size.
4580 * <p>This value is only applicable for custom display notifications created using
4581 * {@link #setDisplayIntent}.
4582 */
4583 public static final int SIZE_SMALL = 2;
4584
4585 /**
4586 * Size value for use with {@link #setCustomSizePreset} to show this notification
4587 * with a medium size.
4588 * <p>This value is only applicable for custom display notifications created using
4589 * {@link #setDisplayIntent}.
4590 */
4591 public static final int SIZE_MEDIUM = 3;
4592
4593 /**
4594 * Size value for use with {@link #setCustomSizePreset} to show this notification
4595 * with a large size.
4596 * <p>This value is only applicable for custom display notifications created using
4597 * {@link #setDisplayIntent}.
4598 */
4599 public static final int SIZE_LARGE = 4;
4600
Griff Hazend5f11f92014-05-27 15:40:09 -07004601 /**
4602 * Size value for use with {@link #setCustomSizePreset} to show this notification
4603 * full screen.
4604 * <p>This value is only applicable for custom display notifications created using
4605 * {@link #setDisplayIntent}.
4606 */
4607 public static final int SIZE_FULL_SCREEN = 5;
4608
Griff Hazen5f2edfc2014-09-29 16:28:44 -07004609 /**
4610 * Sentinel value for use with {@link #setHintScreenTimeout} to keep the screen on for a
4611 * short amount of time when this notification is displayed on the screen. This
4612 * is the default value.
4613 */
4614 public static final int SCREEN_TIMEOUT_SHORT = 0;
4615
4616 /**
4617 * Sentinel value for use with {@link #setHintScreenTimeout} to keep the screen on
4618 * for a longer amount of time when this notification is displayed on the screen.
4619 */
4620 public static final int SCREEN_TIMEOUT_LONG = -1;
4621
Griff Hazen61a9e862014-05-22 16:05:19 -07004622 /** Notification extra which contains wearable extensions */
4623 private static final String EXTRA_WEARABLE_EXTENSIONS = "android.wearable.EXTENSIONS";
4624
Pete Gastaf6781d2014-10-07 15:17:05 -04004625 // Keys within EXTRA_WEARABLE_EXTENSIONS for wearable options.
Griff Hazen61a9e862014-05-22 16:05:19 -07004626 private static final String KEY_ACTIONS = "actions";
4627 private static final String KEY_FLAGS = "flags";
4628 private static final String KEY_DISPLAY_INTENT = "displayIntent";
4629 private static final String KEY_PAGES = "pages";
4630 private static final String KEY_BACKGROUND = "background";
4631 private static final String KEY_CONTENT_ICON = "contentIcon";
4632 private static final String KEY_CONTENT_ICON_GRAVITY = "contentIconGravity";
4633 private static final String KEY_CONTENT_ACTION_INDEX = "contentActionIndex";
4634 private static final String KEY_CUSTOM_SIZE_PRESET = "customSizePreset";
4635 private static final String KEY_CUSTOM_CONTENT_HEIGHT = "customContentHeight";
4636 private static final String KEY_GRAVITY = "gravity";
Griff Hazen5f2edfc2014-09-29 16:28:44 -07004637 private static final String KEY_HINT_SCREEN_TIMEOUT = "hintScreenTimeout";
Griff Hazen61a9e862014-05-22 16:05:19 -07004638
4639 // Flags bitwise-ored to mFlags
4640 private static final int FLAG_CONTENT_INTENT_AVAILABLE_OFFLINE = 0x1;
4641 private static final int FLAG_HINT_HIDE_ICON = 1 << 1;
4642 private static final int FLAG_HINT_SHOW_BACKGROUND_ONLY = 1 << 2;
4643 private static final int FLAG_START_SCROLL_BOTTOM = 1 << 3;
Griff Hazen5f2edfc2014-09-29 16:28:44 -07004644 private static final int FLAG_HINT_AVOID_BACKGROUND_CLIPPING = 1 << 4;
Griff Hazen61a9e862014-05-22 16:05:19 -07004645
4646 // Default value for flags integer
4647 private static final int DEFAULT_FLAGS = FLAG_CONTENT_INTENT_AVAILABLE_OFFLINE;
4648
4649 private static final int DEFAULT_CONTENT_ICON_GRAVITY = Gravity.END;
4650 private static final int DEFAULT_GRAVITY = Gravity.BOTTOM;
4651
4652 private ArrayList<Action> mActions = new ArrayList<Action>();
4653 private int mFlags = DEFAULT_FLAGS;
4654 private PendingIntent mDisplayIntent;
4655 private ArrayList<Notification> mPages = new ArrayList<Notification>();
4656 private Bitmap mBackground;
4657 private int mContentIcon;
4658 private int mContentIconGravity = DEFAULT_CONTENT_ICON_GRAVITY;
4659 private int mContentActionIndex = UNSET_ACTION_INDEX;
4660 private int mCustomSizePreset = SIZE_DEFAULT;
4661 private int mCustomContentHeight;
4662 private int mGravity = DEFAULT_GRAVITY;
Griff Hazen5f2edfc2014-09-29 16:28:44 -07004663 private int mHintScreenTimeout;
Griff Hazen61a9e862014-05-22 16:05:19 -07004664
4665 /**
4666 * Create a {@link android.app.Notification.WearableExtender} with default
4667 * options.
4668 */
4669 public WearableExtender() {
4670 }
4671
4672 public WearableExtender(Notification notif) {
4673 Bundle wearableBundle = notif.extras.getBundle(EXTRA_WEARABLE_EXTENSIONS);
4674 if (wearableBundle != null) {
4675 List<Action> actions = wearableBundle.getParcelableArrayList(KEY_ACTIONS);
4676 if (actions != null) {
4677 mActions.addAll(actions);
4678 }
4679
4680 mFlags = wearableBundle.getInt(KEY_FLAGS, DEFAULT_FLAGS);
4681 mDisplayIntent = wearableBundle.getParcelable(KEY_DISPLAY_INTENT);
4682
4683 Notification[] pages = getNotificationArrayFromBundle(
4684 wearableBundle, KEY_PAGES);
4685 if (pages != null) {
4686 Collections.addAll(mPages, pages);
4687 }
4688
4689 mBackground = wearableBundle.getParcelable(KEY_BACKGROUND);
4690 mContentIcon = wearableBundle.getInt(KEY_CONTENT_ICON);
4691 mContentIconGravity = wearableBundle.getInt(KEY_CONTENT_ICON_GRAVITY,
4692 DEFAULT_CONTENT_ICON_GRAVITY);
4693 mContentActionIndex = wearableBundle.getInt(KEY_CONTENT_ACTION_INDEX,
4694 UNSET_ACTION_INDEX);
4695 mCustomSizePreset = wearableBundle.getInt(KEY_CUSTOM_SIZE_PRESET,
4696 SIZE_DEFAULT);
4697 mCustomContentHeight = wearableBundle.getInt(KEY_CUSTOM_CONTENT_HEIGHT);
4698 mGravity = wearableBundle.getInt(KEY_GRAVITY, DEFAULT_GRAVITY);
Griff Hazen5f2edfc2014-09-29 16:28:44 -07004699 mHintScreenTimeout = wearableBundle.getInt(KEY_HINT_SCREEN_TIMEOUT);
Griff Hazen61a9e862014-05-22 16:05:19 -07004700 }
4701 }
4702
4703 /**
4704 * Apply wearable extensions to a notification that is being built. This is typically
4705 * called by the {@link android.app.Notification.Builder#extend} method of
4706 * {@link android.app.Notification.Builder}.
4707 */
4708 @Override
4709 public Notification.Builder extend(Notification.Builder builder) {
4710 Bundle wearableBundle = new Bundle();
4711
4712 if (!mActions.isEmpty()) {
4713 wearableBundle.putParcelableArrayList(KEY_ACTIONS, mActions);
4714 }
4715 if (mFlags != DEFAULT_FLAGS) {
4716 wearableBundle.putInt(KEY_FLAGS, mFlags);
4717 }
4718 if (mDisplayIntent != null) {
4719 wearableBundle.putParcelable(KEY_DISPLAY_INTENT, mDisplayIntent);
4720 }
4721 if (!mPages.isEmpty()) {
4722 wearableBundle.putParcelableArray(KEY_PAGES, mPages.toArray(
4723 new Notification[mPages.size()]));
4724 }
4725 if (mBackground != null) {
4726 wearableBundle.putParcelable(KEY_BACKGROUND, mBackground);
4727 }
4728 if (mContentIcon != 0) {
4729 wearableBundle.putInt(KEY_CONTENT_ICON, mContentIcon);
4730 }
4731 if (mContentIconGravity != DEFAULT_CONTENT_ICON_GRAVITY) {
4732 wearableBundle.putInt(KEY_CONTENT_ICON_GRAVITY, mContentIconGravity);
4733 }
4734 if (mContentActionIndex != UNSET_ACTION_INDEX) {
4735 wearableBundle.putInt(KEY_CONTENT_ACTION_INDEX,
4736 mContentActionIndex);
4737 }
4738 if (mCustomSizePreset != SIZE_DEFAULT) {
4739 wearableBundle.putInt(KEY_CUSTOM_SIZE_PRESET, mCustomSizePreset);
4740 }
4741 if (mCustomContentHeight != 0) {
4742 wearableBundle.putInt(KEY_CUSTOM_CONTENT_HEIGHT, mCustomContentHeight);
4743 }
4744 if (mGravity != DEFAULT_GRAVITY) {
4745 wearableBundle.putInt(KEY_GRAVITY, mGravity);
4746 }
Griff Hazen5f2edfc2014-09-29 16:28:44 -07004747 if (mHintScreenTimeout != 0) {
4748 wearableBundle.putInt(KEY_HINT_SCREEN_TIMEOUT, mHintScreenTimeout);
4749 }
Griff Hazen61a9e862014-05-22 16:05:19 -07004750
4751 builder.getExtras().putBundle(EXTRA_WEARABLE_EXTENSIONS, wearableBundle);
4752 return builder;
4753 }
4754
4755 @Override
4756 public WearableExtender clone() {
4757 WearableExtender that = new WearableExtender();
4758 that.mActions = new ArrayList<Action>(this.mActions);
4759 that.mFlags = this.mFlags;
4760 that.mDisplayIntent = this.mDisplayIntent;
4761 that.mPages = new ArrayList<Notification>(this.mPages);
4762 that.mBackground = this.mBackground;
4763 that.mContentIcon = this.mContentIcon;
4764 that.mContentIconGravity = this.mContentIconGravity;
4765 that.mContentActionIndex = this.mContentActionIndex;
4766 that.mCustomSizePreset = this.mCustomSizePreset;
4767 that.mCustomContentHeight = this.mCustomContentHeight;
4768 that.mGravity = this.mGravity;
Griff Hazen5f2edfc2014-09-29 16:28:44 -07004769 that.mHintScreenTimeout = this.mHintScreenTimeout;
Griff Hazen61a9e862014-05-22 16:05:19 -07004770 return that;
4771 }
4772
4773 /**
4774 * Add a wearable action to this notification.
4775 *
4776 * <p>When wearable actions are added using this method, the set of actions that
4777 * show on a wearable device splits from devices that only show actions added
4778 * using {@link android.app.Notification.Builder#addAction}. This allows for customization
4779 * of which actions display on different devices.
4780 *
4781 * @param action the action to add to this notification
4782 * @return this object for method chaining
4783 * @see android.app.Notification.Action
4784 */
4785 public WearableExtender addAction(Action action) {
4786 mActions.add(action);
4787 return this;
4788 }
4789
4790 /**
4791 * Adds wearable actions to this notification.
4792 *
4793 * <p>When wearable actions are added using this method, the set of actions that
4794 * show on a wearable device splits from devices that only show actions added
4795 * using {@link android.app.Notification.Builder#addAction}. This allows for customization
4796 * of which actions display on different devices.
4797 *
4798 * @param actions the actions to add to this notification
4799 * @return this object for method chaining
4800 * @see android.app.Notification.Action
4801 */
4802 public WearableExtender addActions(List<Action> actions) {
4803 mActions.addAll(actions);
4804 return this;
4805 }
4806
4807 /**
4808 * Clear all wearable actions present on this builder.
4809 * @return this object for method chaining.
4810 * @see #addAction
4811 */
4812 public WearableExtender clearActions() {
4813 mActions.clear();
4814 return this;
4815 }
4816
4817 /**
4818 * Get the wearable actions present on this notification.
4819 */
4820 public List<Action> getActions() {
4821 return mActions;
4822 }
4823
4824 /**
4825 * Set an intent to launch inside of an activity view when displaying
Griff Hazen14f57992014-05-26 09:07:14 -07004826 * this notification. The {@link PendingIntent} provided should be for an activity.
4827 *
4828 * <pre class="prettyprint">
4829 * Intent displayIntent = new Intent(context, MyDisplayActivity.class);
4830 * PendingIntent displayPendingIntent = PendingIntent.getActivity(context,
4831 * 0, displayIntent, PendingIntent.FLAG_UPDATE_CURRENT);
4832 * Notification notif = new Notification.Builder(context)
4833 * .extend(new Notification.WearableExtender()
4834 * .setDisplayIntent(displayPendingIntent)
4835 * .setCustomSizePreset(Notification.WearableExtender.SIZE_MEDIUM))
4836 * .build();</pre>
4837 *
4838 * <p>The activity to launch needs to allow embedding, must be exported, and
Griff Hazen831ca9d2014-06-17 00:38:38 -07004839 * should have an empty task affinity. It is also recommended to use the device
4840 * default light theme.
Griff Hazen14f57992014-05-26 09:07:14 -07004841 *
4842 * <p>Example AndroidManifest.xml entry:
4843 * <pre class="prettyprint">
4844 * &lt;activity android:name=&quot;com.example.MyDisplayActivity&quot;
4845 * android:exported=&quot;true&quot;
4846 * android:allowEmbedded=&quot;true&quot;
Griff Hazen831ca9d2014-06-17 00:38:38 -07004847 * android:taskAffinity=&quot;&quot;
4848 * android:theme=&quot;@android:style/Theme.DeviceDefault.Light&quot; /&gt;</pre>
Griff Hazen61a9e862014-05-22 16:05:19 -07004849 *
4850 * @param intent the {@link PendingIntent} for an activity
4851 * @return this object for method chaining
4852 * @see android.app.Notification.WearableExtender#getDisplayIntent
4853 */
4854 public WearableExtender setDisplayIntent(PendingIntent intent) {
4855 mDisplayIntent = intent;
4856 return this;
4857 }
4858
4859 /**
4860 * Get the intent to launch inside of an activity view when displaying this
4861 * notification. This {@code PendingIntent} should be for an activity.
4862 */
4863 public PendingIntent getDisplayIntent() {
4864 return mDisplayIntent;
4865 }
4866
4867 /**
4868 * Add an additional page of content to display with this notification. The current
4869 * notification forms the first page, and pages added using this function form
4870 * subsequent pages. This field can be used to separate a notification into multiple
4871 * sections.
4872 *
4873 * @param page the notification to add as another page
4874 * @return this object for method chaining
4875 * @see android.app.Notification.WearableExtender#getPages
4876 */
4877 public WearableExtender addPage(Notification page) {
4878 mPages.add(page);
4879 return this;
4880 }
4881
4882 /**
4883 * Add additional pages of content to display with this notification. The current
4884 * notification forms the first page, and pages added using this function form
4885 * subsequent pages. This field can be used to separate a notification into multiple
4886 * sections.
4887 *
4888 * @param pages a list of notifications
4889 * @return this object for method chaining
4890 * @see android.app.Notification.WearableExtender#getPages
4891 */
4892 public WearableExtender addPages(List<Notification> pages) {
4893 mPages.addAll(pages);
4894 return this;
4895 }
4896
4897 /**
4898 * Clear all additional pages present on this builder.
4899 * @return this object for method chaining.
4900 * @see #addPage
4901 */
4902 public WearableExtender clearPages() {
4903 mPages.clear();
4904 return this;
4905 }
4906
4907 /**
4908 * Get the array of additional pages of content for displaying this notification. The
4909 * current notification forms the first page, and elements within this array form
4910 * subsequent pages. This field can be used to separate a notification into multiple
4911 * sections.
4912 * @return the pages for this notification
4913 */
4914 public List<Notification> getPages() {
4915 return mPages;
4916 }
4917
4918 /**
4919 * Set a background image to be displayed behind the notification content.
4920 * Contrary to the {@link android.app.Notification.BigPictureStyle}, this background
4921 * will work with any notification style.
4922 *
4923 * @param background the background bitmap
4924 * @return this object for method chaining
4925 * @see android.app.Notification.WearableExtender#getBackground
4926 */
4927 public WearableExtender setBackground(Bitmap background) {
4928 mBackground = background;
4929 return this;
4930 }
4931
4932 /**
4933 * Get a background image to be displayed behind the notification content.
4934 * Contrary to the {@link android.app.Notification.BigPictureStyle}, this background
4935 * will work with any notification style.
4936 *
4937 * @return the background image
4938 * @see android.app.Notification.WearableExtender#setBackground
4939 */
4940 public Bitmap getBackground() {
4941 return mBackground;
4942 }
4943
4944 /**
4945 * Set an icon that goes with the content of this notification.
4946 */
4947 public WearableExtender setContentIcon(int icon) {
4948 mContentIcon = icon;
4949 return this;
4950 }
4951
4952 /**
4953 * Get an icon that goes with the content of this notification.
4954 */
4955 public int getContentIcon() {
4956 return mContentIcon;
4957 }
4958
4959 /**
4960 * Set the gravity that the content icon should have within the notification display.
4961 * Supported values include {@link android.view.Gravity#START} and
4962 * {@link android.view.Gravity#END}. The default value is {@link android.view.Gravity#END}.
4963 * @see #setContentIcon
4964 */
4965 public WearableExtender setContentIconGravity(int contentIconGravity) {
4966 mContentIconGravity = contentIconGravity;
4967 return this;
4968 }
4969
4970 /**
4971 * Get the gravity that the content icon should have within the notification display.
4972 * Supported values include {@link android.view.Gravity#START} and
4973 * {@link android.view.Gravity#END}. The default value is {@link android.view.Gravity#END}.
4974 * @see #getContentIcon
4975 */
4976 public int getContentIconGravity() {
4977 return mContentIconGravity;
4978 }
4979
4980 /**
4981 * Set an action from this notification's actions to be clickable with the content of
Griff Hazen14f57992014-05-26 09:07:14 -07004982 * this notification. This action will no longer display separately from the
4983 * notification's content.
4984 *
Griff Hazenca48d352014-05-28 22:37:13 -07004985 * <p>For notifications with multiple pages, child pages can also have content actions
Griff Hazen14f57992014-05-26 09:07:14 -07004986 * set, although the list of available actions comes from the main notification and not
4987 * from the child page's notification.
4988 *
4989 * @param actionIndex The index of the action to hoist onto the current notification page.
4990 * If wearable actions were added to the main notification, this index
4991 * will apply to that list, otherwise it will apply to the regular
4992 * actions list.
Griff Hazen61a9e862014-05-22 16:05:19 -07004993 */
4994 public WearableExtender setContentAction(int actionIndex) {
4995 mContentActionIndex = actionIndex;
4996 return this;
4997 }
4998
4999 /**
Griff Hazenca48d352014-05-28 22:37:13 -07005000 * Get the index of the notification action, if any, that was specified as being clickable
5001 * with the content of this notification. This action will no longer display separately
Griff Hazen14f57992014-05-26 09:07:14 -07005002 * from the notification's content.
Griff Hazen61a9e862014-05-22 16:05:19 -07005003 *
Griff Hazenca48d352014-05-28 22:37:13 -07005004 * <p>For notifications with multiple pages, child pages can also have content actions
Griff Hazen14f57992014-05-26 09:07:14 -07005005 * set, although the list of available actions comes from the main notification and not
5006 * from the child page's notification.
5007 *
5008 * <p>If wearable specific actions were added to the main notification, this index will
5009 * apply to that list, otherwise it will apply to the regular actions list.
Griff Hazenca48d352014-05-28 22:37:13 -07005010 *
5011 * @return the action index or {@link #UNSET_ACTION_INDEX} if no action was selected.
Griff Hazen61a9e862014-05-22 16:05:19 -07005012 */
5013 public int getContentAction() {
5014 return mContentActionIndex;
5015 }
5016
5017 /**
5018 * Set the gravity that this notification should have within the available viewport space.
5019 * Supported values include {@link android.view.Gravity#TOP},
5020 * {@link android.view.Gravity#CENTER_VERTICAL} and {@link android.view.Gravity#BOTTOM}.
5021 * The default value is {@link android.view.Gravity#BOTTOM}.
5022 */
5023 public WearableExtender setGravity(int gravity) {
5024 mGravity = gravity;
5025 return this;
5026 }
5027
5028 /**
5029 * Get the gravity that this notification should have within the available viewport space.
5030 * Supported values include {@link android.view.Gravity#TOP},
5031 * {@link android.view.Gravity#CENTER_VERTICAL} and {@link android.view.Gravity#BOTTOM}.
5032 * The default value is {@link android.view.Gravity#BOTTOM}.
5033 */
5034 public int getGravity() {
5035 return mGravity;
5036 }
5037
5038 /**
5039 * Set the custom size preset for the display of this notification out of the available
5040 * presets found in {@link android.app.Notification.WearableExtender}, e.g.
5041 * {@link #SIZE_LARGE}.
5042 * <p>Some custom size presets are only applicable for custom display notifications created
5043 * using {@link android.app.Notification.WearableExtender#setDisplayIntent}. Check the
5044 * documentation for the preset in question. See also
5045 * {@link #setCustomContentHeight} and {@link #getCustomSizePreset}.
5046 */
5047 public WearableExtender setCustomSizePreset(int sizePreset) {
5048 mCustomSizePreset = sizePreset;
5049 return this;
5050 }
5051
5052 /**
5053 * Get the custom size preset for the display of this notification out of the available
5054 * presets found in {@link android.app.Notification.WearableExtender}, e.g.
5055 * {@link #SIZE_LARGE}.
5056 * <p>Some custom size presets are only applicable for custom display notifications created
5057 * using {@link #setDisplayIntent}. Check the documentation for the preset in question.
5058 * See also {@link #setCustomContentHeight} and {@link #setCustomSizePreset}.
5059 */
5060 public int getCustomSizePreset() {
5061 return mCustomSizePreset;
5062 }
5063
5064 /**
5065 * Set the custom height in pixels for the display of this notification's content.
5066 * <p>This option is only available for custom display notifications created
5067 * using {@link android.app.Notification.WearableExtender#setDisplayIntent}. See also
5068 * {@link android.app.Notification.WearableExtender#setCustomSizePreset} and
5069 * {@link #getCustomContentHeight}.
5070 */
5071 public WearableExtender setCustomContentHeight(int height) {
5072 mCustomContentHeight = height;
5073 return this;
5074 }
5075
5076 /**
5077 * Get the custom height in pixels for the display of this notification's content.
5078 * <p>This option is only available for custom display notifications created
5079 * using {@link #setDisplayIntent}. See also {@link #setCustomSizePreset} and
5080 * {@link #setCustomContentHeight}.
5081 */
5082 public int getCustomContentHeight() {
5083 return mCustomContentHeight;
5084 }
5085
5086 /**
5087 * Set whether the scrolling position for the contents of this notification should start
5088 * at the bottom of the contents instead of the top when the contents are too long to
5089 * display within the screen. Default is false (start scroll at the top).
5090 */
5091 public WearableExtender setStartScrollBottom(boolean startScrollBottom) {
5092 setFlag(FLAG_START_SCROLL_BOTTOM, startScrollBottom);
5093 return this;
5094 }
5095
5096 /**
5097 * Get whether the scrolling position for the contents of this notification should start
5098 * at the bottom of the contents instead of the top when the contents are too long to
5099 * display within the screen. Default is false (start scroll at the top).
5100 */
5101 public boolean getStartScrollBottom() {
5102 return (mFlags & FLAG_START_SCROLL_BOTTOM) != 0;
5103 }
5104
5105 /**
5106 * Set whether the content intent is available when the wearable device is not connected
5107 * to a companion device. The user can still trigger this intent when the wearable device
5108 * is offline, but a visual hint will indicate that the content intent may not be available.
5109 * Defaults to true.
5110 */
5111 public WearableExtender setContentIntentAvailableOffline(
5112 boolean contentIntentAvailableOffline) {
5113 setFlag(FLAG_CONTENT_INTENT_AVAILABLE_OFFLINE, contentIntentAvailableOffline);
5114 return this;
5115 }
5116
5117 /**
5118 * Get whether the content intent is available when the wearable device is not connected
5119 * to a companion device. The user can still trigger this intent when the wearable device
5120 * is offline, but a visual hint will indicate that the content intent may not be available.
5121 * Defaults to true.
5122 */
5123 public boolean getContentIntentAvailableOffline() {
5124 return (mFlags & FLAG_CONTENT_INTENT_AVAILABLE_OFFLINE) != 0;
5125 }
5126
5127 /**
5128 * Set a hint that this notification's icon should not be displayed.
5129 * @param hintHideIcon {@code true} to hide the icon, {@code false} otherwise.
5130 * @return this object for method chaining
5131 */
5132 public WearableExtender setHintHideIcon(boolean hintHideIcon) {
5133 setFlag(FLAG_HINT_HIDE_ICON, hintHideIcon);
5134 return this;
5135 }
5136
5137 /**
5138 * Get a hint that this notification's icon should not be displayed.
5139 * @return {@code true} if this icon should not be displayed, false otherwise.
5140 * The default value is {@code false} if this was never set.
5141 */
5142 public boolean getHintHideIcon() {
5143 return (mFlags & FLAG_HINT_HIDE_ICON) != 0;
5144 }
5145
5146 /**
5147 * Set a visual hint that only the background image of this notification should be
5148 * displayed, and other semantic content should be hidden. This hint is only applicable
5149 * to sub-pages added using {@link #addPage}.
5150 */
5151 public WearableExtender setHintShowBackgroundOnly(boolean hintShowBackgroundOnly) {
5152 setFlag(FLAG_HINT_SHOW_BACKGROUND_ONLY, hintShowBackgroundOnly);
5153 return this;
5154 }
5155
5156 /**
5157 * Get a visual hint that only the background image of this notification should be
5158 * displayed, and other semantic content should be hidden. This hint is only applicable
5159 * to sub-pages added using {@link android.app.Notification.WearableExtender#addPage}.
5160 */
5161 public boolean getHintShowBackgroundOnly() {
5162 return (mFlags & FLAG_HINT_SHOW_BACKGROUND_ONLY) != 0;
5163 }
5164
Griff Hazen5f2edfc2014-09-29 16:28:44 -07005165 /**
Griff Hazen9c5be4e2014-11-17 17:47:50 -08005166 * Set a hint that this notification's background should not be clipped if possible,
5167 * and should instead be resized to fully display on the screen, retaining the aspect
5168 * ratio of the image. This can be useful for images like barcodes or qr codes.
Griff Hazen5f2edfc2014-09-29 16:28:44 -07005169 * @param hintAvoidBackgroundClipping {@code true} to avoid clipping if possible.
5170 * @return this object for method chaining
5171 */
5172 public WearableExtender setHintAvoidBackgroundClipping(
5173 boolean hintAvoidBackgroundClipping) {
5174 setFlag(FLAG_HINT_AVOID_BACKGROUND_CLIPPING, hintAvoidBackgroundClipping);
5175 return this;
5176 }
5177
5178 /**
Griff Hazen9c5be4e2014-11-17 17:47:50 -08005179 * Get a hint that this notification's background should not be clipped if possible,
5180 * and should instead be resized to fully display on the screen, retaining the aspect
5181 * ratio of the image. This can be useful for images like barcodes or qr codes.
Griff Hazen5f2edfc2014-09-29 16:28:44 -07005182 * @return {@code true} if it's ok if the background is clipped on the screen, false
5183 * otherwise. The default value is {@code false} if this was never set.
5184 */
5185 public boolean getHintAvoidBackgroundClipping() {
5186 return (mFlags & FLAG_HINT_AVOID_BACKGROUND_CLIPPING) != 0;
5187 }
5188
5189 /**
5190 * Set a hint that the screen should remain on for at least this duration when
5191 * this notification is displayed on the screen.
5192 * @param timeout The requested screen timeout in milliseconds. Can also be either
5193 * {@link #SCREEN_TIMEOUT_SHORT} or {@link #SCREEN_TIMEOUT_LONG}.
5194 * @return this object for method chaining
5195 */
5196 public WearableExtender setHintScreenTimeout(int timeout) {
5197 mHintScreenTimeout = timeout;
5198 return this;
5199 }
5200
5201 /**
5202 * Get the duration, in milliseconds, that the screen should remain on for
5203 * when this notification is displayed.
5204 * @return the duration in milliseconds if > 0, or either one of the sentinel values
5205 * {@link #SCREEN_TIMEOUT_SHORT} or {@link #SCREEN_TIMEOUT_LONG}.
5206 */
5207 public int getHintScreenTimeout() {
5208 return mHintScreenTimeout;
5209 }
5210
Griff Hazen61a9e862014-05-22 16:05:19 -07005211 private void setFlag(int mask, boolean value) {
5212 if (value) {
5213 mFlags |= mask;
5214 } else {
5215 mFlags &= ~mask;
5216 }
5217 }
5218 }
5219
5220 /**
Zhen Yu Song9d5528b2014-11-14 15:34:39 -08005221 * <p>Helper class to add Android Auto extensions to notifications. To create a notification
5222 * with car extensions:
5223 *
5224 * <ol>
5225 * <li>Create an {@link Notification.Builder}, setting any desired
5226 * properties.
5227 * <li>Create a {@link CarExtender}.
5228 * <li>Set car-specific properties using the {@code add} and {@code set} methods of
5229 * {@link CarExtender}.
5230 * <li>Call {@link Notification.Builder#extend(Notification.Extender)}
5231 * to apply the extensions to a notification.
5232 * </ol>
5233 *
5234 * <pre class="prettyprint">
5235 * Notification notification = new Notification.Builder(context)
5236 * ...
5237 * .extend(new CarExtender()
5238 * .set*(...))
5239 * .build();
5240 * </pre>
5241 *
5242 * <p>Car extensions can be accessed on an existing notification by using the
5243 * {@code CarExtender(Notification)} constructor, and then using the {@code get} methods
5244 * to access values.
5245 */
5246 public static final class CarExtender implements Extender {
5247 private static final String TAG = "CarExtender";
5248
5249 private static final String EXTRA_CAR_EXTENDER = "android.car.EXTENSIONS";
5250 private static final String EXTRA_LARGE_ICON = "large_icon";
5251 private static final String EXTRA_CONVERSATION = "car_conversation";
5252 private static final String EXTRA_COLOR = "app_color";
5253
5254 private Bitmap mLargeIcon;
5255 private UnreadConversation mUnreadConversation;
5256 private int mColor = Notification.COLOR_DEFAULT;
5257
5258 /**
5259 * Create a {@link CarExtender} with default options.
5260 */
5261 public CarExtender() {
5262 }
5263
5264 /**
5265 * Create a {@link CarExtender} from the CarExtender options of an existing Notification.
5266 *
5267 * @param notif The notification from which to copy options.
5268 */
5269 public CarExtender(Notification notif) {
5270 Bundle carBundle = notif.extras == null ?
5271 null : notif.extras.getBundle(EXTRA_CAR_EXTENDER);
5272 if (carBundle != null) {
5273 mLargeIcon = carBundle.getParcelable(EXTRA_LARGE_ICON);
5274 mColor = carBundle.getInt(EXTRA_COLOR, Notification.COLOR_DEFAULT);
5275
5276 Bundle b = carBundle.getBundle(EXTRA_CONVERSATION);
5277 mUnreadConversation = UnreadConversation.getUnreadConversationFromBundle(b);
5278 }
5279 }
5280
5281 /**
5282 * Apply car extensions to a notification that is being built. This is typically called by
5283 * the {@link Notification.Builder#extend(Notification.Extender)}
5284 * method of {@link Notification.Builder}.
5285 */
5286 @Override
5287 public Notification.Builder extend(Notification.Builder builder) {
5288 Bundle carExtensions = new Bundle();
5289
5290 if (mLargeIcon != null) {
5291 carExtensions.putParcelable(EXTRA_LARGE_ICON, mLargeIcon);
5292 }
5293 if (mColor != Notification.COLOR_DEFAULT) {
5294 carExtensions.putInt(EXTRA_COLOR, mColor);
5295 }
5296
5297 if (mUnreadConversation != null) {
5298 Bundle b = mUnreadConversation.getBundleForUnreadConversation();
5299 carExtensions.putBundle(EXTRA_CONVERSATION, b);
5300 }
5301
5302 builder.getExtras().putBundle(EXTRA_CAR_EXTENDER, carExtensions);
5303 return builder;
5304 }
5305
5306 /**
5307 * Sets the accent color to use when Android Auto presents the notification.
5308 *
5309 * Android Auto uses the color set with {@link Notification.Builder#setColor(int)}
5310 * to accent the displayed notification. However, not all colors are acceptable in an
5311 * automotive setting. This method can be used to override the color provided in the
5312 * notification in such a situation.
5313 */
Tor Norbye80756e32015-03-02 09:39:27 -08005314 public CarExtender setColor(@ColorInt int color) {
Zhen Yu Song9d5528b2014-11-14 15:34:39 -08005315 mColor = color;
5316 return this;
5317 }
5318
5319 /**
5320 * Gets the accent color.
5321 *
5322 * @see setColor
5323 */
Tor Norbye80756e32015-03-02 09:39:27 -08005324 @ColorInt
Zhen Yu Song9d5528b2014-11-14 15:34:39 -08005325 public int getColor() {
5326 return mColor;
5327 }
5328
5329 /**
5330 * Sets the large icon of the car notification.
5331 *
5332 * If no large icon is set in the extender, Android Auto will display the icon
5333 * specified by {@link Notification.Builder#setLargeIcon(android.graphics.Bitmap)}
5334 *
5335 * @param largeIcon The large icon to use in the car notification.
5336 * @return This object for method chaining.
5337 */
5338 public CarExtender setLargeIcon(Bitmap largeIcon) {
5339 mLargeIcon = largeIcon;
5340 return this;
5341 }
5342
5343 /**
5344 * Gets the large icon used in this car notification, or null if no icon has been set.
5345 *
5346 * @return The large icon for the car notification.
5347 * @see CarExtender#setLargeIcon
5348 */
5349 public Bitmap getLargeIcon() {
5350 return mLargeIcon;
5351 }
5352
5353 /**
5354 * Sets the unread conversation in a message notification.
5355 *
5356 * @param unreadConversation The unread part of the conversation this notification conveys.
5357 * @return This object for method chaining.
5358 */
5359 public CarExtender setUnreadConversation(UnreadConversation unreadConversation) {
5360 mUnreadConversation = unreadConversation;
5361 return this;
5362 }
5363
5364 /**
5365 * Returns the unread conversation conveyed by this notification.
5366 * @see #setUnreadConversation(UnreadConversation)
5367 */
5368 public UnreadConversation getUnreadConversation() {
5369 return mUnreadConversation;
5370 }
5371
5372 /**
5373 * A class which holds the unread messages from a conversation.
5374 */
5375 public static class UnreadConversation {
5376 private static final String KEY_AUTHOR = "author";
5377 private static final String KEY_TEXT = "text";
5378 private static final String KEY_MESSAGES = "messages";
5379 private static final String KEY_REMOTE_INPUT = "remote_input";
5380 private static final String KEY_ON_REPLY = "on_reply";
5381 private static final String KEY_ON_READ = "on_read";
5382 private static final String KEY_PARTICIPANTS = "participants";
5383 private static final String KEY_TIMESTAMP = "timestamp";
5384
5385 private final String[] mMessages;
5386 private final RemoteInput mRemoteInput;
5387 private final PendingIntent mReplyPendingIntent;
5388 private final PendingIntent mReadPendingIntent;
5389 private final String[] mParticipants;
5390 private final long mLatestTimestamp;
5391
5392 UnreadConversation(String[] messages, RemoteInput remoteInput,
5393 PendingIntent replyPendingIntent, PendingIntent readPendingIntent,
5394 String[] participants, long latestTimestamp) {
5395 mMessages = messages;
5396 mRemoteInput = remoteInput;
5397 mReadPendingIntent = readPendingIntent;
5398 mReplyPendingIntent = replyPendingIntent;
5399 mParticipants = participants;
5400 mLatestTimestamp = latestTimestamp;
5401 }
5402
5403 /**
5404 * Gets the list of messages conveyed by this notification.
5405 */
5406 public String[] getMessages() {
5407 return mMessages;
5408 }
5409
5410 /**
5411 * Gets the remote input that will be used to convey the response to a message list, or
5412 * null if no such remote input exists.
5413 */
5414 public RemoteInput getRemoteInput() {
5415 return mRemoteInput;
5416 }
5417
5418 /**
5419 * Gets the pending intent that will be triggered when the user replies to this
5420 * notification.
5421 */
5422 public PendingIntent getReplyPendingIntent() {
5423 return mReplyPendingIntent;
5424 }
5425
5426 /**
5427 * Gets the pending intent that Android Auto will send after it reads aloud all messages
5428 * in this object's message list.
5429 */
5430 public PendingIntent getReadPendingIntent() {
5431 return mReadPendingIntent;
5432 }
5433
5434 /**
5435 * Gets the participants in the conversation.
5436 */
5437 public String[] getParticipants() {
5438 return mParticipants;
5439 }
5440
5441 /**
5442 * Gets the firs participant in the conversation.
5443 */
5444 public String getParticipant() {
5445 return mParticipants.length > 0 ? mParticipants[0] : null;
5446 }
5447
5448 /**
5449 * Gets the timestamp of the conversation.
5450 */
5451 public long getLatestTimestamp() {
5452 return mLatestTimestamp;
5453 }
5454
5455 Bundle getBundleForUnreadConversation() {
5456 Bundle b = new Bundle();
5457 String author = null;
5458 if (mParticipants != null && mParticipants.length > 1) {
5459 author = mParticipants[0];
5460 }
5461 Parcelable[] messages = new Parcelable[mMessages.length];
5462 for (int i = 0; i < messages.length; i++) {
5463 Bundle m = new Bundle();
5464 m.putString(KEY_TEXT, mMessages[i]);
5465 m.putString(KEY_AUTHOR, author);
5466 messages[i] = m;
5467 }
5468 b.putParcelableArray(KEY_MESSAGES, messages);
5469 if (mRemoteInput != null) {
5470 b.putParcelable(KEY_REMOTE_INPUT, mRemoteInput);
5471 }
5472 b.putParcelable(KEY_ON_REPLY, mReplyPendingIntent);
5473 b.putParcelable(KEY_ON_READ, mReadPendingIntent);
5474 b.putStringArray(KEY_PARTICIPANTS, mParticipants);
5475 b.putLong(KEY_TIMESTAMP, mLatestTimestamp);
5476 return b;
5477 }
5478
5479 static UnreadConversation getUnreadConversationFromBundle(Bundle b) {
5480 if (b == null) {
5481 return null;
5482 }
5483 Parcelable[] parcelableMessages = b.getParcelableArray(KEY_MESSAGES);
5484 String[] messages = null;
5485 if (parcelableMessages != null) {
5486 String[] tmp = new String[parcelableMessages.length];
5487 boolean success = true;
5488 for (int i = 0; i < tmp.length; i++) {
5489 if (!(parcelableMessages[i] instanceof Bundle)) {
5490 success = false;
5491 break;
5492 }
5493 tmp[i] = ((Bundle) parcelableMessages[i]).getString(KEY_TEXT);
5494 if (tmp[i] == null) {
5495 success = false;
5496 break;
5497 }
5498 }
5499 if (success) {
5500 messages = tmp;
5501 } else {
5502 return null;
5503 }
5504 }
5505
5506 PendingIntent onRead = b.getParcelable(KEY_ON_READ);
5507 PendingIntent onReply = b.getParcelable(KEY_ON_REPLY);
5508
5509 RemoteInput remoteInput = b.getParcelable(KEY_REMOTE_INPUT);
5510
5511 String[] participants = b.getStringArray(KEY_PARTICIPANTS);
5512 if (participants == null || participants.length != 1) {
5513 return null;
5514 }
5515
5516 return new UnreadConversation(messages,
5517 remoteInput,
5518 onReply,
5519 onRead,
5520 participants, b.getLong(KEY_TIMESTAMP));
5521 }
5522 };
5523
5524 /**
5525 * Builder class for {@link CarExtender.UnreadConversation} objects.
5526 */
5527 public static class Builder {
5528 private final List<String> mMessages = new ArrayList<String>();
5529 private final String mParticipant;
5530 private RemoteInput mRemoteInput;
5531 private PendingIntent mReadPendingIntent;
5532 private PendingIntent mReplyPendingIntent;
5533 private long mLatestTimestamp;
5534
5535 /**
5536 * Constructs a new builder for {@link CarExtender.UnreadConversation}.
5537 *
5538 * @param name The name of the other participant in the conversation.
5539 */
5540 public Builder(String name) {
5541 mParticipant = name;
5542 }
5543
5544 /**
5545 * Appends a new unread message to the list of messages for this conversation.
5546 *
5547 * The messages should be added from oldest to newest.
5548 *
5549 * @param message The text of the new unread message.
5550 * @return This object for method chaining.
5551 */
5552 public Builder addMessage(String message) {
5553 mMessages.add(message);
5554 return this;
5555 }
5556
5557 /**
5558 * Sets the pending intent and remote input which will convey the reply to this
5559 * notification.
5560 *
5561 * @param pendingIntent The pending intent which will be triggered on a reply.
5562 * @param remoteInput The remote input parcelable which will carry the reply.
5563 * @return This object for method chaining.
5564 *
5565 * @see CarExtender.UnreadConversation#getRemoteInput
5566 * @see CarExtender.UnreadConversation#getReplyPendingIntent
5567 */
5568 public Builder setReplyAction(
5569 PendingIntent pendingIntent, RemoteInput remoteInput) {
5570 mRemoteInput = remoteInput;
5571 mReplyPendingIntent = pendingIntent;
5572
5573 return this;
5574 }
5575
5576 /**
5577 * Sets the pending intent that will be sent once the messages in this notification
5578 * are read.
5579 *
5580 * @param pendingIntent The pending intent to use.
5581 * @return This object for method chaining.
5582 */
5583 public Builder setReadPendingIntent(PendingIntent pendingIntent) {
5584 mReadPendingIntent = pendingIntent;
5585 return this;
5586 }
5587
5588 /**
5589 * Sets the timestamp of the most recent message in an unread conversation.
5590 *
5591 * If a messaging notification has been posted by your application and has not
5592 * yet been cancelled, posting a later notification with the same id and tag
5593 * but without a newer timestamp may result in Android Auto not displaying a
5594 * heads up notification for the later notification.
5595 *
5596 * @param timestamp The timestamp of the most recent message in the conversation.
5597 * @return This object for method chaining.
5598 */
5599 public Builder setLatestTimestamp(long timestamp) {
5600 mLatestTimestamp = timestamp;
5601 return this;
5602 }
5603
5604 /**
5605 * Builds a new unread conversation object.
5606 *
5607 * @return The new unread conversation object.
5608 */
5609 public UnreadConversation build() {
5610 String[] messages = mMessages.toArray(new String[mMessages.size()]);
5611 String[] participants = { mParticipant };
5612 return new UnreadConversation(messages, mRemoteInput, mReplyPendingIntent,
5613 mReadPendingIntent, participants, mLatestTimestamp);
5614 }
5615 }
5616 }
5617
5618 /**
Griff Hazen61a9e862014-05-22 16:05:19 -07005619 * Get an array of Notification objects from a parcelable array bundle field.
5620 * Update the bundle to have a typed array so fetches in the future don't need
5621 * to do an array copy.
5622 */
5623 private static Notification[] getNotificationArrayFromBundle(Bundle bundle, String key) {
5624 Parcelable[] array = bundle.getParcelableArray(key);
5625 if (array instanceof Notification[] || array == null) {
5626 return (Notification[]) array;
5627 }
5628 Notification[] typedArray = Arrays.copyOf(array, array.length,
5629 Notification[].class);
5630 bundle.putParcelableArray(key, typedArray);
5631 return typedArray;
5632 }
Christoph Studer4600f9b2014-07-22 22:44:43 +02005633
5634 private static class BuilderRemoteViews extends RemoteViews {
5635 public BuilderRemoteViews(Parcel parcel) {
5636 super(parcel);
5637 }
5638
Kenny Guy77320062014-08-27 21:37:15 +01005639 public BuilderRemoteViews(ApplicationInfo appInfo, int layoutId) {
5640 super(appInfo, layoutId);
Christoph Studer4600f9b2014-07-22 22:44:43 +02005641 }
5642
5643 @Override
5644 public BuilderRemoteViews clone() {
5645 Parcel p = Parcel.obtain();
5646 writeToParcel(p, 0);
5647 p.setDataPosition(0);
5648 BuilderRemoteViews brv = new BuilderRemoteViews(p);
5649 p.recycle();
5650 return brv;
5651 }
5652 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005653}