blob: 33a47b24b79de19ed57f5e5f68022519a431ae4e [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);
1374 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001375 number = parcel.readInt();
1376 if (parcel.readInt() != 0) {
1377 contentIntent = PendingIntent.CREATOR.createFromParcel(parcel);
1378 }
1379 if (parcel.readInt() != 0) {
1380 deleteIntent = PendingIntent.CREATOR.createFromParcel(parcel);
1381 }
1382 if (parcel.readInt() != 0) {
1383 tickerText = TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(parcel);
1384 }
1385 if (parcel.readInt() != 0) {
Joe Onorato46439ce2010-11-19 13:56:21 -08001386 tickerView = RemoteViews.CREATOR.createFromParcel(parcel);
Joe Onoratoef1e7762010-09-17 18:38:38 -04001387 }
1388 if (parcel.readInt() != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001389 contentView = RemoteViews.CREATOR.createFromParcel(parcel);
1390 }
Joe Onorato561d3852010-11-20 18:09:34 -08001391 if (parcel.readInt() != 0) {
Dan Sandlerd63f9322015-05-06 15:18:49 -04001392 mLargeIcon = Icon.CREATOR.createFromParcel(parcel);
Joe Onorato561d3852010-11-20 18:09:34 -08001393 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001394 defaults = parcel.readInt();
1395 flags = parcel.readInt();
1396 if (parcel.readInt() != 0) {
1397 sound = Uri.CREATOR.createFromParcel(parcel);
1398 }
1399
1400 audioStreamType = parcel.readInt();
John Spurlockc0650f022014-07-19 13:22:39 -04001401 if (parcel.readInt() != 0) {
1402 audioAttributes = AudioAttributes.CREATOR.createFromParcel(parcel);
1403 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001404 vibrate = parcel.createLongArray();
1405 ledARGB = parcel.readInt();
1406 ledOnMS = parcel.readInt();
1407 ledOffMS = parcel.readInt();
1408 iconLevel = parcel.readInt();
Daniel Sandlere46cbd32010-06-17 10:35:26 -04001409
1410 if (parcel.readInt() != 0) {
1411 fullScreenIntent = PendingIntent.CREATOR.createFromParcel(parcel);
1412 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001413
1414 priority = parcel.readInt();
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001415
John Spurlockfd7f1e02014-03-18 16:41:57 -04001416 category = parcel.readString();
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001417
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001418 mGroupKey = parcel.readString();
1419
1420 mSortKey = parcel.readString();
1421
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001422 extras = parcel.readBundle(); // may be null
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001423
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001424 actions = parcel.createTypedArray(Action.CREATOR); // may be null
1425
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001426 if (parcel.readInt() != 0) {
Daniel Sandlerf3b73432012-03-27 15:01:25 -04001427 bigContentView = RemoteViews.CREATOR.createFromParcel(parcel);
1428 }
Dan Sandler0bf2ed82013-12-21 23:33:41 -06001429
Chris Wren8fd39ec2014-02-27 17:43:26 -05001430 if (parcel.readInt() != 0) {
1431 headsUpContentView = RemoteViews.CREATOR.createFromParcel(parcel);
1432 }
1433
Dan Sandler0bf2ed82013-12-21 23:33:41 -06001434 visibility = parcel.readInt();
1435
1436 if (parcel.readInt() != 0) {
1437 publicVersion = Notification.CREATOR.createFromParcel(parcel);
1438 }
Dan Sandler26e81cf2014-05-06 10:01:27 -04001439
1440 color = parcel.readInt();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001441 }
1442
Andy Stadler110988c2010-12-03 14:29:16 -08001443 @Override
Joe Onorato18e69df2010-05-17 22:26:12 -07001444 public Notification clone() {
1445 Notification that = new Notification();
Daniel Sandler1a497d32013-04-18 14:52:45 -04001446 cloneInto(that, true);
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001447 return that;
1448 }
Joe Onorato18e69df2010-05-17 22:26:12 -07001449
Daniel Sandler1a497d32013-04-18 14:52:45 -04001450 /**
1451 * Copy all (or if heavy is false, all except Bitmaps and RemoteViews) members
1452 * of this into that.
1453 * @hide
1454 */
1455 public void cloneInto(Notification that, boolean heavy) {
Joe Onorato18e69df2010-05-17 22:26:12 -07001456 that.when = this.when;
Dan Sandlerd63f9322015-05-06 15:18:49 -04001457 that.mSmallIcon = this.mSmallIcon;
Joe Onorato18e69df2010-05-17 22:26:12 -07001458 that.number = this.number;
1459
1460 // PendingIntents are global, so there's no reason (or way) to clone them.
1461 that.contentIntent = this.contentIntent;
1462 that.deleteIntent = this.deleteIntent;
Daniel Sandlere46cbd32010-06-17 10:35:26 -04001463 that.fullScreenIntent = this.fullScreenIntent;
Joe Onorato18e69df2010-05-17 22:26:12 -07001464
1465 if (this.tickerText != null) {
1466 that.tickerText = this.tickerText.toString();
1467 }
Daniel Sandler1a497d32013-04-18 14:52:45 -04001468 if (heavy && this.tickerView != null) {
Joe Onorato46439ce2010-11-19 13:56:21 -08001469 that.tickerView = this.tickerView.clone();
Joe Onoratoef1e7762010-09-17 18:38:38 -04001470 }
Daniel Sandler1a497d32013-04-18 14:52:45 -04001471 if (heavy && this.contentView != null) {
Joe Onorato18e69df2010-05-17 22:26:12 -07001472 that.contentView = this.contentView.clone();
1473 }
Dan Sandlerd63f9322015-05-06 15:18:49 -04001474 if (heavy && this.mLargeIcon != null) {
1475 that.mLargeIcon = this.mLargeIcon;
Joe Onorato561d3852010-11-20 18:09:34 -08001476 }
Jozef BABJAKa8b91832011-02-22 08:05:08 +01001477 that.iconLevel = this.iconLevel;
Joe Onorato18e69df2010-05-17 22:26:12 -07001478 that.sound = this.sound; // android.net.Uri is immutable
1479 that.audioStreamType = this.audioStreamType;
John Spurlockc0650f022014-07-19 13:22:39 -04001480 if (this.audioAttributes != null) {
1481 that.audioAttributes = new AudioAttributes.Builder(this.audioAttributes).build();
1482 }
Joe Onorato18e69df2010-05-17 22:26:12 -07001483
1484 final long[] vibrate = this.vibrate;
1485 if (vibrate != null) {
1486 final int N = vibrate.length;
1487 final long[] vib = that.vibrate = new long[N];
1488 System.arraycopy(vibrate, 0, vib, 0, N);
1489 }
1490
1491 that.ledARGB = this.ledARGB;
1492 that.ledOnMS = this.ledOnMS;
1493 that.ledOffMS = this.ledOffMS;
1494 that.defaults = this.defaults;
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001495
Joe Onorato18e69df2010-05-17 22:26:12 -07001496 that.flags = this.flags;
1497
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001498 that.priority = this.priority;
Joe Malin8d40d042012-11-05 11:36:40 -08001499
John Spurlockfd7f1e02014-03-18 16:41:57 -04001500 that.category = this.category;
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001501
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001502 that.mGroupKey = this.mGroupKey;
1503
1504 that.mSortKey = this.mSortKey;
1505
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001506 if (this.extras != null) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04001507 try {
1508 that.extras = new Bundle(this.extras);
1509 // will unparcel
1510 that.extras.size();
1511 } catch (BadParcelableException e) {
1512 Log.e(TAG, "could not unparcel extras from notification: " + this, e);
1513 that.extras = null;
1514 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001515 }
1516
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001517 if (this.actions != null) {
1518 that.actions = new Action[this.actions.length];
1519 for(int i=0; i<this.actions.length; i++) {
1520 that.actions[i] = this.actions[i].clone();
1521 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001522 }
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001523
Daniel Sandler1a497d32013-04-18 14:52:45 -04001524 if (heavy && this.bigContentView != null) {
Daniel Sandlerf3b73432012-03-27 15:01:25 -04001525 that.bigContentView = this.bigContentView.clone();
1526 }
Daniel Sandler1a497d32013-04-18 14:52:45 -04001527
Chris Wren8fd39ec2014-02-27 17:43:26 -05001528 if (heavy && this.headsUpContentView != null) {
1529 that.headsUpContentView = this.headsUpContentView.clone();
1530 }
1531
Dan Sandler0bf2ed82013-12-21 23:33:41 -06001532 that.visibility = this.visibility;
1533
1534 if (this.publicVersion != null) {
1535 that.publicVersion = new Notification();
1536 this.publicVersion.cloneInto(that.publicVersion, heavy);
1537 }
1538
Dan Sandler26e81cf2014-05-06 10:01:27 -04001539 that.color = this.color;
1540
Daniel Sandler1a497d32013-04-18 14:52:45 -04001541 if (!heavy) {
1542 that.lightenPayload(); // will clean out extras
1543 }
1544 }
1545
1546 /**
1547 * Removes heavyweight parts of the Notification object for archival or for sending to
1548 * listeners when the full contents are not necessary.
1549 * @hide
1550 */
1551 public final void lightenPayload() {
1552 tickerView = null;
1553 contentView = null;
1554 bigContentView = null;
Chris Wren8fd39ec2014-02-27 17:43:26 -05001555 headsUpContentView = null;
Dan Sandlerd63f9322015-05-06 15:18:49 -04001556 mLargeIcon = null;
Daniel Sandler1a497d32013-04-18 14:52:45 -04001557 if (extras != null) {
1558 extras.remove(Notification.EXTRA_LARGE_ICON);
1559 extras.remove(Notification.EXTRA_LARGE_ICON_BIG);
1560 extras.remove(Notification.EXTRA_PICTURE);
Christoph Studer223f44e2014-09-02 14:59:32 +02001561 extras.remove(Notification.EXTRA_BIG_TEXT);
Christoph Studerb82bc782014-08-20 14:29:43 +02001562 // Prevent light notifications from being rebuilt.
1563 extras.remove(Builder.EXTRA_NEEDS_REBUILD);
Daniel Sandler1a497d32013-04-18 14:52:45 -04001564 }
Joe Onorato18e69df2010-05-17 22:26:12 -07001565 }
1566
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04001567 /**
1568 * Make sure this CharSequence is safe to put into a bundle, which basically
1569 * means it had better not be some custom Parcelable implementation.
1570 * @hide
1571 */
1572 public static CharSequence safeCharSequence(CharSequence cs) {
Christoph Studer535ec612014-09-03 15:47:47 +02001573 if (cs == null) return cs;
1574 if (cs.length() > MAX_CHARSEQUENCE_LENGTH) {
1575 cs = cs.subSequence(0, MAX_CHARSEQUENCE_LENGTH);
1576 }
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04001577 if (cs instanceof Parcelable) {
1578 Log.e(TAG, "warning: " + cs.getClass().getCanonicalName()
1579 + " instance is a custom Parcelable and not allowed in Notification");
1580 return cs.toString();
1581 }
1582
1583 return cs;
1584 }
1585
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001586 public int describeContents() {
1587 return 0;
1588 }
1589
1590 /**
1591 * Flatten this notification from a parcel.
1592 */
1593 public void writeToParcel(Parcel parcel, int flags)
1594 {
1595 parcel.writeInt(1);
1596
1597 parcel.writeLong(when);
Dan Sandler3936e7a2015-05-19 20:59:12 -04001598 if (mSmallIcon != null) {
1599 parcel.writeInt(1);
1600 mSmallIcon.writeToParcel(parcel, 0);
1601 } else {
1602 parcel.writeInt(0);
1603 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001604 parcel.writeInt(number);
1605 if (contentIntent != null) {
1606 parcel.writeInt(1);
1607 contentIntent.writeToParcel(parcel, 0);
1608 } else {
1609 parcel.writeInt(0);
1610 }
1611 if (deleteIntent != null) {
1612 parcel.writeInt(1);
1613 deleteIntent.writeToParcel(parcel, 0);
1614 } else {
1615 parcel.writeInt(0);
1616 }
1617 if (tickerText != null) {
1618 parcel.writeInt(1);
1619 TextUtils.writeToParcel(tickerText, parcel, flags);
1620 } else {
1621 parcel.writeInt(0);
1622 }
Joe Onorato46439ce2010-11-19 13:56:21 -08001623 if (tickerView != null) {
Joe Onoratoef1e7762010-09-17 18:38:38 -04001624 parcel.writeInt(1);
Joe Onorato46439ce2010-11-19 13:56:21 -08001625 tickerView.writeToParcel(parcel, 0);
Joe Onoratoef1e7762010-09-17 18:38:38 -04001626 } else {
1627 parcel.writeInt(0);
1628 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001629 if (contentView != null) {
1630 parcel.writeInt(1);
1631 contentView.writeToParcel(parcel, 0);
1632 } else {
1633 parcel.writeInt(0);
1634 }
Dan Sandlerd63f9322015-05-06 15:18:49 -04001635 if (mLargeIcon != null) {
Joe Onorato561d3852010-11-20 18:09:34 -08001636 parcel.writeInt(1);
Dan Sandlerd63f9322015-05-06 15:18:49 -04001637 mLargeIcon.writeToParcel(parcel, 0);
Joe Onorato561d3852010-11-20 18:09:34 -08001638 } else {
1639 parcel.writeInt(0);
1640 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001641
1642 parcel.writeInt(defaults);
1643 parcel.writeInt(this.flags);
1644
1645 if (sound != null) {
1646 parcel.writeInt(1);
1647 sound.writeToParcel(parcel, 0);
1648 } else {
1649 parcel.writeInt(0);
1650 }
1651 parcel.writeInt(audioStreamType);
John Spurlockc0650f022014-07-19 13:22:39 -04001652
1653 if (audioAttributes != null) {
1654 parcel.writeInt(1);
1655 audioAttributes.writeToParcel(parcel, 0);
1656 } else {
1657 parcel.writeInt(0);
1658 }
1659
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001660 parcel.writeLongArray(vibrate);
1661 parcel.writeInt(ledARGB);
1662 parcel.writeInt(ledOnMS);
1663 parcel.writeInt(ledOffMS);
1664 parcel.writeInt(iconLevel);
Daniel Sandlere46cbd32010-06-17 10:35:26 -04001665
1666 if (fullScreenIntent != null) {
1667 parcel.writeInt(1);
1668 fullScreenIntent.writeToParcel(parcel, 0);
1669 } else {
1670 parcel.writeInt(0);
1671 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001672
1673 parcel.writeInt(priority);
Joe Malin8d40d042012-11-05 11:36:40 -08001674
John Spurlockfd7f1e02014-03-18 16:41:57 -04001675 parcel.writeString(category);
Joe Malin8d40d042012-11-05 11:36:40 -08001676
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001677 parcel.writeString(mGroupKey);
1678
1679 parcel.writeString(mSortKey);
1680
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001681 parcel.writeBundle(extras); // null ok
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001682
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001683 parcel.writeTypedArray(actions, 0); // null ok
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001684
Daniel Sandlerf3b73432012-03-27 15:01:25 -04001685 if (bigContentView != null) {
1686 parcel.writeInt(1);
1687 bigContentView.writeToParcel(parcel, 0);
1688 } else {
1689 parcel.writeInt(0);
1690 }
Dan Sandler0bf2ed82013-12-21 23:33:41 -06001691
Chris Wren8fd39ec2014-02-27 17:43:26 -05001692 if (headsUpContentView != null) {
1693 parcel.writeInt(1);
1694 headsUpContentView.writeToParcel(parcel, 0);
1695 } else {
1696 parcel.writeInt(0);
1697 }
1698
Dan Sandler0bf2ed82013-12-21 23:33:41 -06001699 parcel.writeInt(visibility);
1700
1701 if (publicVersion != null) {
1702 parcel.writeInt(1);
1703 publicVersion.writeToParcel(parcel, 0);
1704 } else {
1705 parcel.writeInt(0);
1706 }
Dan Sandler26e81cf2014-05-06 10:01:27 -04001707
1708 parcel.writeInt(color);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001709 }
1710
1711 /**
1712 * Parcelable.Creator that instantiates Notification objects
1713 */
1714 public static final Parcelable.Creator<Notification> CREATOR
1715 = new Parcelable.Creator<Notification>()
1716 {
1717 public Notification createFromParcel(Parcel parcel)
1718 {
1719 return new Notification(parcel);
1720 }
1721
1722 public Notification[] newArray(int size)
1723 {
1724 return new Notification[size];
1725 }
1726 };
1727
1728 /**
1729 * Sets the {@link #contentView} field to be a view with the standard "Latest Event"
1730 * layout.
1731 *
1732 * <p>Uses the {@link #icon} and {@link #when} fields to set the icon and time fields
1733 * in the view.</p>
1734 * @param context The context for your application / activity.
1735 * @param contentTitle The title that goes in the expanded entry.
1736 * @param contentText The text that goes in the expanded entry.
1737 * @param contentIntent The intent to launch when the user clicks the expanded notification.
1738 * If this is an activity, it must include the
1739 * {@link android.content.Intent#FLAG_ACTIVITY_NEW_TASK} flag, which requires
Scott Main7aee61f2011-02-08 11:25:01 -08001740 * that you take care of task management as described in the
1741 * <a href="{@docRoot}guide/topics/fundamentals/tasks-and-back-stack.html">Tasks and Back
1742 * Stack</a> document.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001743 *
Joe Onorato46439ce2010-11-19 13:56:21 -08001744 * @deprecated Use {@link Builder} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001745 */
Joe Onorato46439ce2010-11-19 13:56:21 -08001746 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001747 public void setLatestEventInfo(Context context,
1748 CharSequence contentTitle, CharSequence contentText, PendingIntent contentIntent) {
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001749 Notification.Builder builder = new Notification.Builder(context);
1750
1751 // First, ensure that key pieces of information that may have been set directly
1752 // are preserved
1753 builder.setWhen(this.when);
1754 builder.setSmallIcon(this.icon);
1755 builder.setPriority(this.priority);
1756 builder.setTicker(this.tickerText);
1757 builder.setNumber(this.number);
Selim Cinek255dd042014-08-19 22:29:02 +02001758 builder.setColor(this.color);
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001759 builder.mFlags = this.flags;
1760 builder.setSound(this.sound, this.audioStreamType);
1761 builder.setDefaults(this.defaults);
1762 builder.setVibrate(this.vibrate);
Sean Barbeau6ae0b1b2014-07-19 21:31:41 -07001763 builder.setDeleteIntent(this.deleteIntent);
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001764
1765 // now apply the latestEventInfo fields
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001766 if (contentTitle != null) {
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001767 builder.setContentTitle(contentTitle);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001768 }
1769 if (contentText != null) {
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001770 builder.setContentText(contentText);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001771 }
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001772 builder.setContentIntent(contentIntent);
1773 builder.buildInto(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001774 }
1775
1776 @Override
1777 public String toString() {
1778 StringBuilder sb = new StringBuilder();
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001779 sb.append("Notification(pri=");
1780 sb.append(priority);
1781 sb.append(" contentView=");
Joe Onoratoc9596d62011-01-12 17:03:11 -08001782 if (contentView != null) {
1783 sb.append(contentView.getPackage());
1784 sb.append("/0x");
1785 sb.append(Integer.toHexString(contentView.getLayoutId()));
1786 } else {
1787 sb.append("null");
1788 }
1789 sb.append(" vibrate=");
Daniel Sandler6738eee2012-11-16 12:03:32 -05001790 if ((this.defaults & DEFAULT_VIBRATE) != 0) {
1791 sb.append("default");
1792 } else if (this.vibrate != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001793 int N = this.vibrate.length-1;
1794 sb.append("[");
1795 for (int i=0; i<N; i++) {
1796 sb.append(this.vibrate[i]);
1797 sb.append(',');
1798 }
Simon Schoar8cf97d92009-06-10 22:08:37 +02001799 if (N != -1) {
1800 sb.append(this.vibrate[N]);
1801 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001802 sb.append("]");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001803 } else {
1804 sb.append("null");
1805 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001806 sb.append(" sound=");
Daniel Sandler6738eee2012-11-16 12:03:32 -05001807 if ((this.defaults & DEFAULT_SOUND) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001808 sb.append("default");
Daniel Sandler6738eee2012-11-16 12:03:32 -05001809 } else if (this.sound != null) {
1810 sb.append(this.sound.toString());
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(" defaults=0x");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001815 sb.append(Integer.toHexString(this.defaults));
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001816 sb.append(" flags=0x");
Daniel Sandlere46cbd32010-06-17 10:35:26 -04001817 sb.append(Integer.toHexString(this.flags));
Dan Sandler26e81cf2014-05-06 10:01:27 -04001818 sb.append(String.format(" color=0x%08x", this.color));
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001819 if (this.category != null) {
1820 sb.append(" category=");
1821 sb.append(this.category);
1822 }
1823 if (this.mGroupKey != null) {
1824 sb.append(" groupKey=");
1825 sb.append(this.mGroupKey);
1826 }
1827 if (this.mSortKey != null) {
1828 sb.append(" sortKey=");
1829 sb.append(this.mSortKey);
1830 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001831 if (actions != null) {
Dan Sandler1b718782014-07-18 12:43:45 -04001832 sb.append(" actions=");
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001833 sb.append(actions.length);
Dan Sandler1b718782014-07-18 12:43:45 -04001834 }
1835 sb.append(" vis=");
1836 sb.append(visibilityToString(this.visibility));
1837 if (this.publicVersion != null) {
1838 sb.append(" publicVersion=");
1839 sb.append(publicVersion.toString());
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001840 }
1841 sb.append(")");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001842 return sb.toString();
1843 }
Joe Onorato46439ce2010-11-19 13:56:21 -08001844
Dan Sandler1b718782014-07-18 12:43:45 -04001845 /**
1846 * {@hide}
1847 */
1848 public static String visibilityToString(int vis) {
1849 switch (vis) {
1850 case VISIBILITY_PRIVATE:
1851 return "PRIVATE";
1852 case VISIBILITY_PUBLIC:
1853 return "PUBLIC";
1854 case VISIBILITY_SECRET:
1855 return "SECRET";
1856 default:
1857 return "UNKNOWN(" + String.valueOf(vis) + ")";
1858 }
1859 }
1860
Joe Onoratocb109a02011-01-18 17:57:41 -08001861 /**
John Spurlock1d881a12015-03-18 19:21:54 -04001862 * {@hide}
1863 */
1864 public static String priorityToString(@Priority int pri) {
1865 switch (pri) {
1866 case PRIORITY_MIN:
1867 return "MIN";
1868 case PRIORITY_LOW:
1869 return "LOW";
1870 case PRIORITY_DEFAULT:
1871 return "DEFAULT";
1872 case PRIORITY_HIGH:
1873 return "HIGH";
1874 case PRIORITY_MAX:
1875 return "MAX";
1876 default:
1877 return "UNKNOWN(" + String.valueOf(pri) + ")";
1878 }
1879 }
1880
1881 /**
Dan Sandlerd63f9322015-05-06 15:18:49 -04001882 * The small icon representing this notification in the status bar and content view.
1883 *
1884 * @return the small icon representing this notification.
1885 *
1886 * @see Builder#getSmallIcon()
1887 * @see Builder#setSmallIcon(Icon)
1888 */
1889 public Icon getSmallIcon() {
1890 return mSmallIcon;
1891 }
1892
1893 /**
1894 * Used when notifying to clean up legacy small icons.
1895 * @hide
1896 */
1897 public void setSmallIcon(Icon icon) {
1898 mSmallIcon = icon;
1899 }
1900
1901 /**
1902 * The large icon shown in this notification's content view.
1903 * @see Builder#getLargeIcon()
1904 * @see Builder#setLargeIcon(Icon)
1905 */
1906 public Icon getLargeIcon() {
1907 return mLargeIcon;
1908 }
1909
1910 /**
Christoph Studer4600f9b2014-07-22 22:44:43 +02001911 * @hide
1912 */
1913 public boolean isValid() {
1914 // Would like to check for icon!=0 here, too, but NotificationManagerService accepts that
1915 // for legacy reasons.
1916 return contentView != null || extras.getBoolean(Builder.EXTRA_REBUILD_CONTENT_VIEW);
1917 }
1918
1919 /**
Christoph Studerc8db24b2014-07-25 17:50:30 +02001920 * @hide
1921 */
1922 public boolean isGroupSummary() {
1923 return mGroupKey != null && (flags & FLAG_GROUP_SUMMARY) != 0;
1924 }
1925
1926 /**
1927 * @hide
1928 */
1929 public boolean isGroupChild() {
1930 return mGroupKey != null && (flags & FLAG_GROUP_SUMMARY) == 0;
1931 }
1932
1933 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001934 * Builder class for {@link Notification} objects.
Joe Malin8d40d042012-11-05 11:36:40 -08001935 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001936 * Provides a convenient way to set the various fields of a {@link Notification} and generate
Scott Main183bf112012-08-13 19:12:13 -07001937 * content views using the platform's notification layout template. If your app supports
1938 * versions of Android as old as API level 4, you can instead use
1939 * {@link android.support.v4.app.NotificationCompat.Builder NotificationCompat.Builder},
1940 * available in the <a href="{@docRoot}tools/extras/support-library.html">Android Support
1941 * library</a>.
Joe Malin8d40d042012-11-05 11:36:40 -08001942 *
Scott Main183bf112012-08-13 19:12:13 -07001943 * <p>Example:
Joe Malin8d40d042012-11-05 11:36:40 -08001944 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001945 * <pre class="prettyprint">
Scott Main183bf112012-08-13 19:12:13 -07001946 * Notification noti = new Notification.Builder(mContext)
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001947 * .setContentTitle(&quot;New mail from &quot; + sender.toString())
1948 * .setContentText(subject)
1949 * .setSmallIcon(R.drawable.new_mail)
1950 * .setLargeIcon(aBitmap)
Chris Wrenfbd96ba2012-05-01 12:03:58 -04001951 * .build();
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001952 * </pre>
Joe Onoratocb109a02011-01-18 17:57:41 -08001953 */
Joe Onorato46439ce2010-11-19 13:56:21 -08001954 public static class Builder {
Daniel Sandler602ad1c2012-06-12 16:06:27 -04001955 private static final int MAX_ACTION_BUTTONS = 3;
Jorim Jaggi445d3c02014-08-19 22:33:42 +02001956 private static final float LARGE_TEXT_SCALE = 1.3f;
Daniel Sandler8680bf82012-05-15 16:52:52 -04001957
Christoph Studer4600f9b2014-07-22 22:44:43 +02001958 /**
1959 * @hide
1960 */
1961 public static final String EXTRA_NEEDS_REBUILD = "android.rebuild";
1962
1963 /**
1964 * @hide
1965 */
1966 public static final String EXTRA_REBUILD_LARGE_ICON = "android.rebuild.largeIcon";
1967 /**
1968 * @hide
1969 */
1970 public static final String EXTRA_REBUILD_CONTENT_VIEW = "android.rebuild.contentView";
1971 /**
1972 * @hide
1973 */
1974 public static final String EXTRA_REBUILD_CONTENT_VIEW_ACTION_COUNT =
1975 "android.rebuild.contentViewActionCount";
1976 /**
1977 * @hide
1978 */
1979 public static final String EXTRA_REBUILD_BIG_CONTENT_VIEW
1980 = "android.rebuild.bigView";
1981 /**
1982 * @hide
1983 */
1984 public static final String EXTRA_REBUILD_BIG_CONTENT_VIEW_ACTION_COUNT
1985 = "android.rebuild.bigViewActionCount";
1986 /**
1987 * @hide
1988 */
1989 public static final String EXTRA_REBUILD_HEADS_UP_CONTENT_VIEW
1990 = "android.rebuild.hudView";
1991 /**
1992 * @hide
1993 */
1994 public static final String EXTRA_REBUILD_HEADS_UP_CONTENT_VIEW_ACTION_COUNT
1995 = "android.rebuild.hudViewActionCount";
1996
1997 /**
Kenny Guy77320062014-08-27 21:37:15 +01001998 * The ApplicationInfo of the package that created the notification, used to create
1999 * a context to rebuild the notification via a Builder.
2000 * @hide
Christoph Studer4600f9b2014-07-22 22:44:43 +02002001 */
Kenny Guy77320062014-08-27 21:37:15 +01002002 private static final String EXTRA_REBUILD_CONTEXT_APPLICATION_INFO =
2003 "android.rebuild.applicationInfo";
Christoph Studer4600f9b2014-07-22 22:44:43 +02002004
2005 // Whether to enable stripping (at post time) & rebuilding (at listener receive time) of
2006 // memory intensive resources.
2007 private static final boolean STRIP_AND_REBUILD = true;
2008
Joe Onorato46439ce2010-11-19 13:56:21 -08002009 private Context mContext;
2010
2011 private long mWhen;
Dan Sandlerd63f9322015-05-06 15:18:49 -04002012 private Icon mSmallIcon, mLargeIcon;
Joe Onorato46439ce2010-11-19 13:56:21 -08002013 private int mSmallIconLevel;
Joe Onorato8595a3d2010-11-19 18:12:07 -08002014 private int mNumber;
Joe Onorato46439ce2010-11-19 13:56:21 -08002015 private CharSequence mContentTitle;
2016 private CharSequence mContentText;
2017 private CharSequence mContentInfo;
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002018 private CharSequence mSubText;
Joe Onorato46439ce2010-11-19 13:56:21 -08002019 private PendingIntent mContentIntent;
2020 private RemoteViews mContentView;
2021 private PendingIntent mDeleteIntent;
2022 private PendingIntent mFullScreenIntent;
2023 private CharSequence mTickerText;
2024 private RemoteViews mTickerView;
Joe Onorato46439ce2010-11-19 13:56:21 -08002025 private Uri mSound;
2026 private int mAudioStreamType;
John Spurlockc0650f022014-07-19 13:22:39 -04002027 private AudioAttributes mAudioAttributes;
Joe Onorato46439ce2010-11-19 13:56:21 -08002028 private long[] mVibrate;
2029 private int mLedArgb;
2030 private int mLedOnMs;
2031 private int mLedOffMs;
2032 private int mDefaults;
2033 private int mFlags;
Jeff Sharkey1c400132011-08-05 14:50:13 -07002034 private int mProgressMax;
2035 private int mProgress;
2036 private boolean mProgressIndeterminate;
John Spurlockfd7f1e02014-03-18 16:41:57 -04002037 private String mCategory;
Griff Hazen5cadc3b2014-05-20 09:55:39 -07002038 private String mGroupKey;
2039 private String mSortKey;
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002040 private Bundle mExtras;
2041 private int mPriority;
Daniel Sandler8680bf82012-05-15 16:52:52 -04002042 private ArrayList<Action> mActions = new ArrayList<Action>(MAX_ACTION_BUTTONS);
Daniel Sandlera2985ed2012-04-03 16:42:00 -04002043 private boolean mUseChronometer;
Chris Wrenfbd96ba2012-05-01 12:03:58 -04002044 private Style mStyle;
Daniel Sandler0c890492012-09-12 17:23:10 -07002045 private boolean mShowWhen = true;
Dan Sandler0bf2ed82013-12-21 23:33:41 -06002046 private int mVisibility = VISIBILITY_PRIVATE;
2047 private Notification mPublicVersion = null;
Dan Sandler26e81cf2014-05-06 10:01:27 -04002048 private final NotificationColorUtil mColorUtil;
Chris Wrendde75302014-03-26 17:24:15 -04002049 private ArrayList<String> mPeople;
Dan Sandler26e81cf2014-05-06 10:01:27 -04002050 private int mColor = COLOR_DEFAULT;
Christoph Studer7ac80e62014-08-04 16:01:57 +02002051
2052 /**
Kenny Guy8942bcd2014-09-08 21:09:47 +01002053 * The user that built the notification originally.
2054 */
2055 private int mOriginatingUserId;
2056
2057 /**
Christoph Studer4600f9b2014-07-22 22:44:43 +02002058 * Contains extras related to rebuilding during the build phase.
2059 */
2060 private Bundle mRebuildBundle = new Bundle();
2061 /**
2062 * Contains the notification to rebuild when this Builder is in "rebuild" mode.
2063 * Null otherwise.
2064 */
2065 private Notification mRebuildNotification = null;
2066
Joe Onoratocb109a02011-01-18 17:57:41 -08002067 /**
Jorim Jaggid05aa3e2014-08-28 17:52:27 +02002068 * Whether the build notification has three lines. This is used to make the top padding for
2069 * both the contracted and expanded layout consistent.
2070 *
2071 * <p>
2072 * This field is only valid during the build phase.
2073 */
2074 private boolean mHasThreeLines;
2075
2076 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002077 * Constructs a new Builder with the defaults:
Joe Onoratocb109a02011-01-18 17:57:41 -08002078 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002079
2080 * <table>
2081 * <tr><th align=right>priority</th>
2082 * <td>{@link #PRIORITY_DEFAULT}</td></tr>
2083 * <tr><th align=right>when</th>
2084 * <td>now ({@link System#currentTimeMillis()})</td></tr>
2085 * <tr><th align=right>audio stream</th>
2086 * <td>{@link #STREAM_DEFAULT}</td></tr>
2087 * </table>
Joe Onoratocb109a02011-01-18 17:57:41 -08002088 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002089
2090 * @param context
2091 * A {@link Context} that will be used by the Builder to construct the
2092 * RemoteViews. The Context will not be held past the lifetime of this Builder
2093 * object.
Joe Onoratocb109a02011-01-18 17:57:41 -08002094 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002095 public Builder(Context context) {
Adam Powellbf06fa02014-05-30 12:32:18 -07002096 /*
2097 * Important compatibility note!
2098 * Some apps out in the wild create a Notification.Builder in their Activity subclass
2099 * constructor for later use. At this point Activities - themselves subclasses of
2100 * ContextWrapper - do not have their inner Context populated yet. This means that
2101 * any calls to Context methods from within this constructor can cause NPEs in existing
2102 * apps. Any data populated from mContext should therefore be populated lazily to
2103 * preserve compatibility.
2104 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002105 mContext = context;
Andy Stadler110988c2010-12-03 14:29:16 -08002106
2107 // Set defaults to match the defaults of a Notification
Joe Onorato46439ce2010-11-19 13:56:21 -08002108 mWhen = System.currentTimeMillis();
Andy Stadler110988c2010-12-03 14:29:16 -08002109 mAudioStreamType = STREAM_DEFAULT;
John Spurlockc0650f022014-07-19 13:22:39 -04002110 mAudioAttributes = AUDIO_ATTRIBUTES_DEFAULT;
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002111 mPriority = PRIORITY_DEFAULT;
Chris Wrendde75302014-03-26 17:24:15 -04002112 mPeople = new ArrayList<String>();
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01002113
Dianne Hackborn955d8d62014-10-07 20:17:19 -07002114 mColorUtil = context.getApplicationInfo().targetSdkVersion < Build.VERSION_CODES.LOLLIPOP ?
Dan Sandler05c362d2014-09-03 00:16:27 +02002115 NotificationColorUtil.getInstance(mContext) : null;
Joe Onorato46439ce2010-11-19 13:56:21 -08002116 }
2117
Joe Onoratocb109a02011-01-18 17:57:41 -08002118 /**
Christoph Studer4600f9b2014-07-22 22:44:43 +02002119 * Creates a Builder for rebuilding the given Notification.
2120 * <p>
2121 * Call {@link #rebuild()} to retrieve the rebuilt version of 'n'.
2122 */
2123 private Builder(Context context, Notification n) {
2124 this(context);
2125 mRebuildNotification = n;
2126 restoreFromNotification(n);
2127
2128 Style style = null;
2129 Bundle extras = n.extras;
2130 String templateClass = extras.getString(EXTRA_TEMPLATE);
2131 if (!TextUtils.isEmpty(templateClass)) {
2132 Class<? extends Style> styleClass = getNotificationStyleClass(templateClass);
2133 if (styleClass == null) {
2134 Log.d(TAG, "Unknown style class: " + styleClass);
2135 return;
2136 }
2137
2138 try {
2139 Constructor<? extends Style> constructor = styleClass.getConstructor();
Alan Viverette904de2e2015-05-04 10:32:57 -07002140 constructor.setAccessible(true);
Christoph Studer4600f9b2014-07-22 22:44:43 +02002141 style = constructor.newInstance();
2142 style.restoreFromExtras(extras);
2143 } catch (Throwable t) {
2144 Log.e(TAG, "Could not create Style", t);
2145 return;
2146 }
2147 }
2148 if (style != null) {
2149 setStyle(style);
2150 }
2151 }
2152
2153 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002154 * Add a timestamp pertaining to the notification (usually the time the event occurred).
Daniel Sandler0c890492012-09-12 17:23:10 -07002155 * It will be shown in the notification content view by default; use
Griff Hazen50c11652014-05-16 09:46:31 -07002156 * {@link #setShowWhen(boolean) setShowWhen} to control this.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002157 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002158 * @see Notification#when
Joe Onoratocb109a02011-01-18 17:57:41 -08002159 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002160 public Builder setWhen(long when) {
2161 mWhen = when;
2162 return this;
2163 }
2164
Joe Onoratocb109a02011-01-18 17:57:41 -08002165 /**
Griff Hazen50c11652014-05-16 09:46:31 -07002166 * Control whether the timestamp set with {@link #setWhen(long) setWhen} is shown
Daniel Sandler0c890492012-09-12 17:23:10 -07002167 * in the content view.
2168 */
2169 public Builder setShowWhen(boolean show) {
2170 mShowWhen = show;
2171 return this;
2172 }
2173
2174 /**
Daniel Sandlerd33b8032012-05-10 11:41:48 -04002175 * Show the {@link Notification#when} field as a stopwatch.
Joe Malin8d40d042012-11-05 11:36:40 -08002176 *
2177 * Instead of presenting <code>when</code> as a timestamp, the notification will show an
Daniel Sandlerd33b8032012-05-10 11:41:48 -04002178 * automatically updating display of the minutes and seconds since <code>when</code>.
Daniel Sandlera2985ed2012-04-03 16:42:00 -04002179 *
Daniel Sandlerd33b8032012-05-10 11:41:48 -04002180 * Useful when showing an elapsed time (like an ongoing phone call).
2181 *
2182 * @see android.widget.Chronometer
Daniel Sandlera2985ed2012-04-03 16:42:00 -04002183 * @see Notification#when
2184 */
2185 public Builder setUsesChronometer(boolean b) {
2186 mUseChronometer = b;
2187 return this;
2188 }
2189
2190 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002191 * Set the small icon resource, which will be used to represent the notification in the
2192 * status bar.
Joe Onoratocb109a02011-01-18 17:57:41 -08002193 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002194
2195 * The platform template for the expanded view will draw this icon in the left, unless a
2196 * {@link #setLargeIcon(Bitmap) large icon} has also been specified, in which case the small
2197 * icon will be moved to the right-hand side.
2198 *
2199
2200 * @param icon
2201 * A resource ID in the application's package of the drawable to use.
2202 * @see Notification#icon
Joe Onoratocb109a02011-01-18 17:57:41 -08002203 */
Tor Norbye7b9c9122013-05-30 16:48:33 -07002204 public Builder setSmallIcon(@DrawableRes int icon) {
Dan Sandlerd63f9322015-05-06 15:18:49 -04002205 return setSmallIcon(icon != 0
2206 ? Icon.createWithResource(mContext, icon)
2207 : null);
Joe Onorato46439ce2010-11-19 13:56:21 -08002208 }
2209
Joe Onoratocb109a02011-01-18 17:57:41 -08002210 /**
2211 * A variant of {@link #setSmallIcon(int) setSmallIcon(int)} that takes an additional
2212 * level parameter for when the icon is a {@link android.graphics.drawable.LevelListDrawable
2213 * LevelListDrawable}.
2214 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002215 * @param icon A resource ID in the application's package of the drawable to use.
Joe Onoratocb109a02011-01-18 17:57:41 -08002216 * @param level The level to use for the icon.
2217 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002218 * @see Notification#icon
2219 * @see Notification#iconLevel
Joe Onoratocb109a02011-01-18 17:57:41 -08002220 */
Tor Norbye7b9c9122013-05-30 16:48:33 -07002221 public Builder setSmallIcon(@DrawableRes int icon, int level) {
Daniel Sandler09e51a72015-05-14 15:32:28 +00002222 mSmallIconLevel = level;
Dan Sandlerd63f9322015-05-06 15:18:49 -04002223 return setSmallIcon(icon);
2224 }
2225
2226 /**
2227 * Set the small icon, which will be used to represent the notification in the
2228 * status bar and content view (unless overriden there by a
2229 * {@link #setLargeIcon(Bitmap) large icon}).
2230 *
2231 * @param icon An Icon object to use.
2232 * @see Notification#icon
2233 */
2234 public Builder setSmallIcon(Icon icon) {
2235 mSmallIcon = icon;
Joe Onorato46439ce2010-11-19 13:56:21 -08002236 return this;
2237 }
2238
Joe Onoratocb109a02011-01-18 17:57:41 -08002239 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002240 * Set the first line of text in the platform notification template.
Joe Onoratocb109a02011-01-18 17:57:41 -08002241 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002242 public Builder setContentTitle(CharSequence title) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04002243 mContentTitle = safeCharSequence(title);
Joe Onorato46439ce2010-11-19 13:56:21 -08002244 return this;
2245 }
2246
Joe Onoratocb109a02011-01-18 17:57:41 -08002247 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002248 * Set the second line of text in the platform notification template.
Joe Onoratocb109a02011-01-18 17:57:41 -08002249 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002250 public Builder setContentText(CharSequence text) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04002251 mContentText = safeCharSequence(text);
Joe Onorato46439ce2010-11-19 13:56:21 -08002252 return this;
2253 }
2254
Joe Onoratocb109a02011-01-18 17:57:41 -08002255 /**
Joe Malin8d40d042012-11-05 11:36:40 -08002256 * Set the third line of text in the platform notification template.
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04002257 * Don't use if you're also using {@link #setProgress(int, int, boolean)}; they occupy the
2258 * same location in the standard template.
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002259 */
2260 public Builder setSubText(CharSequence text) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04002261 mSubText = safeCharSequence(text);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002262 return this;
2263 }
2264
2265 /**
Joe Onoratocb109a02011-01-18 17:57:41 -08002266 * Set the large number at the right-hand side of the notification. This is
2267 * equivalent to setContentInfo, although it might show the number in a different
2268 * font size for readability.
2269 */
Joe Onorato8595a3d2010-11-19 18:12:07 -08002270 public Builder setNumber(int number) {
2271 mNumber = number;
2272 return this;
2273 }
2274
Joe Onoratocb109a02011-01-18 17:57:41 -08002275 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002276 * A small piece of additional information pertaining to this notification.
2277 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002278 * The platform template will draw this on the last line of the notification, at the far
2279 * right (to the right of a smallIcon if it has been placed there).
Joe Onoratocb109a02011-01-18 17:57:41 -08002280 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002281 public Builder setContentInfo(CharSequence info) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04002282 mContentInfo = safeCharSequence(info);
Joe Onorato46439ce2010-11-19 13:56:21 -08002283 return this;
2284 }
2285
Joe Onoratocb109a02011-01-18 17:57:41 -08002286 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002287 * Set the progress this notification represents.
2288 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002289 * The platform template will represent this using a {@link ProgressBar}.
Jeff Sharkey1c400132011-08-05 14:50:13 -07002290 */
2291 public Builder setProgress(int max, int progress, boolean indeterminate) {
2292 mProgressMax = max;
2293 mProgress = progress;
2294 mProgressIndeterminate = indeterminate;
2295 return this;
2296 }
2297
2298 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002299 * Supply a custom RemoteViews to use instead of the platform template.
2300 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002301 * @see Notification#contentView
Joe Onoratocb109a02011-01-18 17:57:41 -08002302 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002303 public Builder setContent(RemoteViews views) {
2304 mContentView = views;
2305 return this;
2306 }
2307
Joe Onoratocb109a02011-01-18 17:57:41 -08002308 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002309 * Supply a {@link PendingIntent} to be sent when the notification is clicked.
2310 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002311 * As of {@link android.os.Build.VERSION_CODES#HONEYCOMB}, if this field is unset and you
2312 * have specified a custom RemoteViews with {@link #setContent(RemoteViews)}, you can use
2313 * {@link RemoteViews#setOnClickPendingIntent RemoteViews.setOnClickPendingIntent(int,PendingIntent)}
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002314 * to assign PendingIntents to individual views in that custom layout (i.e., to create
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002315 * clickable buttons inside the notification view).
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002316 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002317 * @see Notification#contentIntent Notification.contentIntent
Joe Onoratocb109a02011-01-18 17:57:41 -08002318 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002319 public Builder setContentIntent(PendingIntent intent) {
2320 mContentIntent = intent;
2321 return this;
2322 }
2323
Joe Onoratocb109a02011-01-18 17:57:41 -08002324 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002325 * Supply a {@link PendingIntent} to send when the notification is cleared explicitly by the user.
2326 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002327 * @see Notification#deleteIntent
Joe Onoratocb109a02011-01-18 17:57:41 -08002328 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002329 public Builder setDeleteIntent(PendingIntent intent) {
2330 mDeleteIntent = intent;
2331 return this;
2332 }
2333
Joe Onoratocb109a02011-01-18 17:57:41 -08002334 /**
2335 * An intent to launch instead of posting the notification to the status bar.
2336 * Only for use with extremely high-priority notifications demanding the user's
2337 * <strong>immediate</strong> attention, such as an incoming phone call or
2338 * alarm clock that the user has explicitly set to a particular time.
2339 * If this facility is used for something else, please give the user an option
2340 * to turn it off and use a normal notification, as this can be extremely
2341 * disruptive.
2342 *
Chris Wren47c20a12014-06-18 17:27:29 -04002343 * <p>
2344 * The system UI may choose to display a heads-up notification, instead of
2345 * launching this intent, while the user is using the device.
2346 * </p>
2347 *
Joe Onoratocb109a02011-01-18 17:57:41 -08002348 * @param intent The pending intent to launch.
2349 * @param highPriority Passing true will cause this notification to be sent
2350 * even if other notifications are suppressed.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002351 *
2352 * @see Notification#fullScreenIntent
Joe Onoratocb109a02011-01-18 17:57:41 -08002353 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002354 public Builder setFullScreenIntent(PendingIntent intent, boolean highPriority) {
2355 mFullScreenIntent = intent;
2356 setFlag(FLAG_HIGH_PRIORITY, highPriority);
2357 return this;
2358 }
2359
Joe Onoratocb109a02011-01-18 17:57:41 -08002360 /**
Dan Sandler5fcdf6e2014-07-18 11:31:15 -04002361 * Set the "ticker" text which is sent to accessibility services.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002362 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002363 * @see Notification#tickerText
Joe Onoratocb109a02011-01-18 17:57:41 -08002364 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002365 public Builder setTicker(CharSequence tickerText) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04002366 mTickerText = safeCharSequence(tickerText);
Joe Onorato46439ce2010-11-19 13:56:21 -08002367 return this;
2368 }
2369
Joe Onoratocb109a02011-01-18 17:57:41 -08002370 /**
Dan Sandler5fcdf6e2014-07-18 11:31:15 -04002371 * Obsolete version of {@link #setTicker(CharSequence)}.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002372 *
Joe Onoratocb109a02011-01-18 17:57:41 -08002373 */
Dan Sandler5fcdf6e2014-07-18 11:31:15 -04002374 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -08002375 public Builder setTicker(CharSequence tickerText, RemoteViews views) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04002376 mTickerText = safeCharSequence(tickerText);
Dan Sandler5fcdf6e2014-07-18 11:31:15 -04002377 mTickerView = views; // we'll save it for you anyway
Joe Onorato46439ce2010-11-19 13:56:21 -08002378 return this;
2379 }
2380
Joe Onoratocb109a02011-01-18 17:57:41 -08002381 /**
Dan Sandlerd63f9322015-05-06 15:18:49 -04002382 * Add a large icon to the notification content view.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002383 *
2384 * In the platform template, this image will be shown on the left of the notification view
Dan Sandlerd63f9322015-05-06 15:18:49 -04002385 * in place of the {@link #setSmallIcon(Icon) small icon} (which will be placed in a small
2386 * badge atop the large icon).
Dan Sandler08a04c12015-05-06 15:18:49 -04002387 */
Dan Sandlerd63f9322015-05-06 15:18:49 -04002388 public Builder setLargeIcon(Bitmap b) {
2389 return setLargeIcon(b != null ? Icon.createWithBitmap(b) : null);
2390 }
2391
2392 /**
2393 * Add a large icon to the notification content view.
2394 *
2395 * In the platform template, this image will be shown on the left of the notification view
2396 * in place of the {@link #setSmallIcon(Icon) small icon} (which will be placed in a small
2397 * badge atop the large icon).
2398 */
2399 public Builder setLargeIcon(Icon icon) {
Joe Onorato46439ce2010-11-19 13:56:21 -08002400 mLargeIcon = icon;
2401 return this;
2402 }
2403
Joe Onoratocb109a02011-01-18 17:57:41 -08002404 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002405 * Set the sound to play.
2406 *
John Spurlockc0650f022014-07-19 13:22:39 -04002407 * It will be played using the {@link #AUDIO_ATTRIBUTES_DEFAULT default audio attributes}
2408 * for notifications.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002409 *
Chris Wren47c20a12014-06-18 17:27:29 -04002410 * <p>
2411 * A notification that is noisy is more likely to be presented as a heads-up notification.
2412 * </p>
2413 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002414 * @see Notification#sound
Joe Onoratocb109a02011-01-18 17:57:41 -08002415 */
Joe Onorato52f80cd2010-11-21 15:34:48 -08002416 public Builder setSound(Uri sound) {
2417 mSound = sound;
John Spurlockc0650f022014-07-19 13:22:39 -04002418 mAudioAttributes = AUDIO_ATTRIBUTES_DEFAULT;
Joe Onorato52f80cd2010-11-21 15:34:48 -08002419 return this;
2420 }
2421
Joe Onoratocb109a02011-01-18 17:57:41 -08002422 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002423 * Set the sound to play, along with a specific stream on which to play it.
Joe Onoratocb109a02011-01-18 17:57:41 -08002424 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002425 * See {@link android.media.AudioManager} for the <code>STREAM_</code> constants.
2426 *
Chris Wren47c20a12014-06-18 17:27:29 -04002427 * <p>
2428 * A notification that is noisy is more likely to be presented as a heads-up notification.
2429 * </p>
John Spurlockc0650f022014-07-19 13:22:39 -04002430 * @deprecated use {@link #setSound(Uri, AudioAttributes)} instead.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002431 * @see Notification#sound
Joe Onoratocb109a02011-01-18 17:57:41 -08002432 */
Jean-Michel Trivi81f871e2014-08-06 16:32:38 -07002433 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -08002434 public Builder setSound(Uri sound, int streamType) {
2435 mSound = sound;
2436 mAudioStreamType = streamType;
2437 return this;
2438 }
2439
Joe Onoratocb109a02011-01-18 17:57:41 -08002440 /**
John Spurlockc0650f022014-07-19 13:22:39 -04002441 * Set the sound to play, along with specific {@link AudioAttributes audio attributes} to
2442 * use during playback.
2443 *
2444 * <p>
2445 * A notification that is noisy is more likely to be presented as a heads-up notification.
2446 * </p>
2447 *
2448 * @see Notification#sound
2449 */
2450 public Builder setSound(Uri sound, AudioAttributes audioAttributes) {
2451 mSound = sound;
2452 mAudioAttributes = audioAttributes;
2453 return this;
2454 }
2455
2456 /**
Joe Onoratocb109a02011-01-18 17:57:41 -08002457 * Set the vibration pattern to use.
2458 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002459 * See {@link android.os.Vibrator#vibrate(long[], int)} for a discussion of the
2460 * <code>pattern</code> parameter.
2461 *
Chris Wren47c20a12014-06-18 17:27:29 -04002462 * <p>
2463 * A notification that vibrates is more likely to be presented as a heads-up notification.
2464 * </p>
2465 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002466 * @see Notification#vibrate
Joe Onoratocb109a02011-01-18 17:57:41 -08002467 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002468 public Builder setVibrate(long[] pattern) {
2469 mVibrate = pattern;
2470 return this;
2471 }
2472
Joe Onoratocb109a02011-01-18 17:57:41 -08002473 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002474 * Set the desired color for the indicator LED on the device, as well as the
2475 * blink duty cycle (specified in milliseconds).
2476 *
2477
2478 * Not all devices will honor all (or even any) of these values.
2479 *
2480
2481 * @see Notification#ledARGB
2482 * @see Notification#ledOnMS
2483 * @see Notification#ledOffMS
Joe Onoratocb109a02011-01-18 17:57:41 -08002484 */
Tor Norbye80756e32015-03-02 09:39:27 -08002485 public Builder setLights(@ColorInt int argb, int onMs, int offMs) {
Joe Onorato46439ce2010-11-19 13:56:21 -08002486 mLedArgb = argb;
2487 mLedOnMs = onMs;
2488 mLedOffMs = offMs;
Joe Onorato46439ce2010-11-19 13:56:21 -08002489 return this;
2490 }
2491
Joe Onoratocb109a02011-01-18 17:57:41 -08002492 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002493 * Set whether this is an "ongoing" notification.
Joe Onoratocb109a02011-01-18 17:57:41 -08002494 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002495
2496 * Ongoing notifications cannot be dismissed by the user, so your application or service
2497 * must take care of canceling them.
2498 *
2499
2500 * They are typically used to indicate a background task that the user is actively engaged
2501 * with (e.g., playing music) or is pending in some way and therefore occupying the device
2502 * (e.g., a file download, sync operation, active network connection).
2503 *
2504
2505 * @see Notification#FLAG_ONGOING_EVENT
2506 * @see Service#setForeground(boolean)
Joe Onoratocb109a02011-01-18 17:57:41 -08002507 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002508 public Builder setOngoing(boolean ongoing) {
2509 setFlag(FLAG_ONGOING_EVENT, ongoing);
2510 return this;
2511 }
2512
Joe Onoratocb109a02011-01-18 17:57:41 -08002513 /**
2514 * Set this flag if you would only like the sound, vibrate
2515 * and ticker to be played if the notification is not already showing.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002516 *
2517 * @see Notification#FLAG_ONLY_ALERT_ONCE
Joe Onoratocb109a02011-01-18 17:57:41 -08002518 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002519 public Builder setOnlyAlertOnce(boolean onlyAlertOnce) {
2520 setFlag(FLAG_ONLY_ALERT_ONCE, onlyAlertOnce);
2521 return this;
2522 }
2523
Joe Onoratocb109a02011-01-18 17:57:41 -08002524 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002525 * Make this notification automatically dismissed when the user touches it. The
2526 * PendingIntent set with {@link #setDeleteIntent} will be sent when this happens.
2527 *
2528 * @see Notification#FLAG_AUTO_CANCEL
Joe Onoratocb109a02011-01-18 17:57:41 -08002529 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002530 public Builder setAutoCancel(boolean autoCancel) {
Joe Onorato281d83f2011-01-04 17:13:10 -08002531 setFlag(FLAG_AUTO_CANCEL, autoCancel);
Joe Onorato46439ce2010-11-19 13:56:21 -08002532 return this;
2533 }
2534
Joe Onoratocb109a02011-01-18 17:57:41 -08002535 /**
Griff Hazendfcb0802014-02-11 12:00:00 -08002536 * Set whether or not this notification should not bridge to other devices.
2537 *
2538 * <p>Some notifications can be bridged to other devices for remote display.
2539 * This hint can be set to recommend this notification not be bridged.
2540 */
2541 public Builder setLocalOnly(boolean localOnly) {
2542 setFlag(FLAG_LOCAL_ONLY, localOnly);
2543 return this;
2544 }
2545
2546 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002547 * Set which notification properties will be inherited from system defaults.
Joe Onoratocb109a02011-01-18 17:57:41 -08002548 * <p>
2549 * The value should be one or more of the following fields combined with
2550 * bitwise-or:
2551 * {@link #DEFAULT_SOUND}, {@link #DEFAULT_VIBRATE}, {@link #DEFAULT_LIGHTS}.
2552 * <p>
2553 * For all default values, use {@link #DEFAULT_ALL}.
2554 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002555 public Builder setDefaults(int defaults) {
2556 mDefaults = defaults;
2557 return this;
2558 }
2559
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002560 /**
2561 * Set the priority of this notification.
2562 *
2563 * @see Notification#priority
2564 */
Tor Norbyed9273d62013-05-30 15:59:53 -07002565 public Builder setPriority(@Priority int pri) {
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002566 mPriority = pri;
2567 return this;
2568 }
Joe Malin8d40d042012-11-05 11:36:40 -08002569
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002570 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -04002571 * Set the notification category.
Joe Malin8d40d042012-11-05 11:36:40 -08002572 *
John Spurlockfd7f1e02014-03-18 16:41:57 -04002573 * @see Notification#category
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002574 */
John Spurlockfd7f1e02014-03-18 16:41:57 -04002575 public Builder setCategory(String category) {
2576 mCategory = category;
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002577 return this;
2578 }
2579
2580 /**
Chris Wrendde75302014-03-26 17:24:15 -04002581 * Add a person that is relevant to this notification.
2582 *
Chris Wrene6c48932014-09-29 17:19:27 -04002583 * <P>
2584 * Depending on user preferences, this annotation may allow the notification to pass
2585 * through interruption filters, and to appear more prominently in the user interface.
2586 * </P>
2587 *
2588 * <P>
2589 * The person should be specified by the {@code String} representation of a
2590 * {@link android.provider.ContactsContract.Contacts#CONTENT_LOOKUP_URI}.
2591 * </P>
2592 *
2593 * <P>The system will also attempt to resolve {@code mailto:} and {@code tel:} schema
2594 * URIs. The path part of these URIs must exist in the contacts database, in the
2595 * appropriate column, or the reference will be discarded as invalid. Telephone schema
2596 * URIs will be resolved by {@link android.provider.ContactsContract.PhoneLookup}.
2597 * </P>
2598 *
2599 * @param uri A URI for the person.
Chris Wrendde75302014-03-26 17:24:15 -04002600 * @see Notification#EXTRA_PEOPLE
2601 */
Chris Wrene6c48932014-09-29 17:19:27 -04002602 public Builder addPerson(String uri) {
2603 mPeople.add(uri);
Chris Wrendde75302014-03-26 17:24:15 -04002604 return this;
2605 }
2606
2607 /**
Griff Hazen5cadc3b2014-05-20 09:55:39 -07002608 * Set this notification to be part of a group of notifications sharing the same key.
2609 * Grouped notifications may display in a cluster or stack on devices which
2610 * support such rendering.
2611 *
2612 * <p>To make this notification the summary for its group, also call
2613 * {@link #setGroupSummary}. A sort order can be specified for group members by using
2614 * {@link #setSortKey}.
2615 * @param groupKey The group key of the group.
2616 * @return this object for method chaining
2617 */
2618 public Builder setGroup(String groupKey) {
2619 mGroupKey = groupKey;
2620 return this;
2621 }
2622
2623 /**
2624 * Set this notification to be the group summary for a group of notifications.
2625 * Grouped notifications may display in a cluster or stack on devices which
2626 * support such rendering. Requires a group key also be set using {@link #setGroup}.
2627 * @param isGroupSummary Whether this notification should be a group summary.
2628 * @return this object for method chaining
2629 */
2630 public Builder setGroupSummary(boolean isGroupSummary) {
2631 setFlag(FLAG_GROUP_SUMMARY, isGroupSummary);
2632 return this;
2633 }
2634
2635 /**
2636 * Set a sort key that orders this notification among other notifications from the
2637 * same package. This can be useful if an external sort was already applied and an app
2638 * would like to preserve this. Notifications will be sorted lexicographically using this
2639 * value, although providing different priorities in addition to providing sort key may
2640 * cause this value to be ignored.
2641 *
2642 * <p>This sort key can also be used to order members of a notification group. See
Griff Hazen9e1379f2014-05-20 12:50:51 -07002643 * {@link #setGroup}.
Griff Hazen5cadc3b2014-05-20 09:55:39 -07002644 *
2645 * @see String#compareTo(String)
2646 */
2647 public Builder setSortKey(String sortKey) {
2648 mSortKey = sortKey;
2649 return this;
2650 }
2651
2652 /**
Griff Hazen720042b2014-02-24 15:46:56 -08002653 * Merge additional metadata into this notification.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002654 *
Griff Hazen720042b2014-02-24 15:46:56 -08002655 * <p>Values within the Bundle will replace existing extras values in this Builder.
2656 *
2657 * @see Notification#extras
2658 */
Griff Hazen959591e2014-05-15 22:26:18 -07002659 public Builder addExtras(Bundle extras) {
2660 if (extras != null) {
2661 if (mExtras == null) {
2662 mExtras = new Bundle(extras);
2663 } else {
2664 mExtras.putAll(extras);
2665 }
Griff Hazen720042b2014-02-24 15:46:56 -08002666 }
2667 return this;
2668 }
2669
2670 /**
2671 * Set metadata for this notification.
2672 *
2673 * <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 -04002674 * current contents are copied into the Notification each time {@link #build()} is
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002675 * called.
2676 *
Griff Hazen720042b2014-02-24 15:46:56 -08002677 * <p>Replaces any existing extras values with those from the provided Bundle.
2678 * Use {@link #addExtras} to merge in metadata instead.
2679 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002680 * @see Notification#extras
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002681 */
Griff Hazen959591e2014-05-15 22:26:18 -07002682 public Builder setExtras(Bundle extras) {
2683 mExtras = extras;
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002684 return this;
2685 }
2686
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002687 /**
Griff Hazen720042b2014-02-24 15:46:56 -08002688 * Get the current metadata Bundle used by this notification Builder.
2689 *
2690 * <p>The returned Bundle is shared with this Builder.
2691 *
2692 * <p>The current contents of this Bundle are copied into the Notification each time
2693 * {@link #build()} is called.
2694 *
2695 * @see Notification#extras
2696 */
2697 public Bundle getExtras() {
2698 if (mExtras == null) {
2699 mExtras = new Bundle();
2700 }
2701 return mExtras;
2702 }
2703
2704 /**
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002705 * Add an action to this notification. Actions are typically displayed by
2706 * the system as a button adjacent to the notification content.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04002707 * <p>
2708 * Every action must have an icon (32dp square and matching the
2709 * <a href="{@docRoot}design/style/iconography.html#action-bar">Holo
2710 * Dark action bar</a> visual style), a textual label, and a {@link PendingIntent}.
2711 * <p>
2712 * A notification in its expanded form can display up to 3 actions, from left to right in
2713 * the order they were added. Actions will not be displayed when the notification is
2714 * collapsed, however, so be sure that any essential functions may be accessed by the user
2715 * in some other way (for example, in the Activity pointed to by {@link #contentIntent}).
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002716 *
2717 * @param icon Resource ID of a drawable that represents the action.
2718 * @param title Text describing the action.
2719 * @param intent PendingIntent to be fired when the action is invoked.
2720 */
2721 public Builder addAction(int icon, CharSequence title, PendingIntent intent) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04002722 mActions.add(new Action(icon, safeCharSequence(title), intent));
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002723 return this;
2724 }
2725
Chris Wrenfbd96ba2012-05-01 12:03:58 -04002726 /**
Griff Hazen959591e2014-05-15 22:26:18 -07002727 * Add an action to this notification. Actions are typically displayed by
2728 * the system as a button adjacent to the notification content.
2729 * <p>
2730 * Every action must have an icon (32dp square and matching the
2731 * <a href="{@docRoot}design/style/iconography.html#action-bar">Holo
2732 * Dark action bar</a> visual style), a textual label, and a {@link PendingIntent}.
2733 * <p>
2734 * A notification in its expanded form can display up to 3 actions, from left to right in
2735 * the order they were added. Actions will not be displayed when the notification is
2736 * collapsed, however, so be sure that any essential functions may be accessed by the user
2737 * in some other way (for example, in the Activity pointed to by {@link #contentIntent}).
2738 *
2739 * @param action The action to add.
2740 */
2741 public Builder addAction(Action action) {
2742 mActions.add(action);
2743 return this;
2744 }
2745
2746 /**
Chris Wrenfbd96ba2012-05-01 12:03:58 -04002747 * Add a rich notification style to be applied at build time.
2748 *
2749 * @param style Object responsible for modifying the notification style.
2750 */
2751 public Builder setStyle(Style style) {
2752 if (mStyle != style) {
2753 mStyle = style;
Daniel Sandlerc08dea22012-06-28 08:35:24 -07002754 if (mStyle != null) {
2755 mStyle.setBuilder(this);
2756 }
Chris Wrenfbd96ba2012-05-01 12:03:58 -04002757 }
2758 return this;
2759 }
2760
Dan Sandler0bf2ed82013-12-21 23:33:41 -06002761 /**
2762 * Specify the value of {@link #visibility}.
Griff Hazenb720abe2014-05-20 13:15:30 -07002763 *
Dan Sandler0bf2ed82013-12-21 23:33:41 -06002764 * @param visibility One of {@link #VISIBILITY_PRIVATE} (the default),
2765 * {@link #VISIBILITY_SECRET}, or {@link #VISIBILITY_PUBLIC}.
2766 *
2767 * @return The same Builder.
2768 */
2769 public Builder setVisibility(int visibility) {
2770 mVisibility = visibility;
2771 return this;
2772 }
2773
2774 /**
2775 * Supply a replacement Notification whose contents should be shown in insecure contexts
2776 * (i.e. atop the secure lockscreen). See {@link #visibility} and {@link #VISIBILITY_PUBLIC}.
2777 * @param n A replacement notification, presumably with some or all info redacted.
2778 * @return The same Builder.
2779 */
2780 public Builder setPublicVersion(Notification n) {
2781 mPublicVersion = n;
2782 return this;
2783 }
2784
Griff Hazenb720abe2014-05-20 13:15:30 -07002785 /**
Griff Hazen5cadc3b2014-05-20 09:55:39 -07002786 * Apply an extender to this notification builder. Extenders may be used to add
2787 * metadata or change options on this builder.
2788 */
Griff Hazen61a9e862014-05-22 16:05:19 -07002789 public Builder extend(Extender extender) {
2790 extender.extend(this);
Griff Hazen5cadc3b2014-05-20 09:55:39 -07002791 return this;
2792 }
2793
Joe Onorato46439ce2010-11-19 13:56:21 -08002794 private void setFlag(int mask, boolean value) {
2795 if (value) {
2796 mFlags |= mask;
2797 } else {
2798 mFlags &= ~mask;
2799 }
2800 }
2801
Dan Sandler26e81cf2014-05-06 10:01:27 -04002802 /**
2803 * Sets {@link Notification#color}.
2804 *
2805 * @param argb The accent color to use
2806 *
2807 * @return The same Builder.
2808 */
Tor Norbye80756e32015-03-02 09:39:27 -08002809 public Builder setColor(@ColorInt int argb) {
Dan Sandler26e81cf2014-05-06 10:01:27 -04002810 mColor = argb;
2811 return this;
2812 }
2813
Jorim Jaggid05aa3e2014-08-28 17:52:27 +02002814 private Drawable getProfileBadgeDrawable() {
Christoph Studer7ac80e62014-08-04 16:01:57 +02002815 // Note: This assumes that the current user can read the profile badge of the
2816 // originating user.
Svetoslavc7d62f02014-09-04 15:39:54 -07002817 return mContext.getPackageManager().getUserBadgeForDensity(
Kenny Guy8942bcd2014-09-08 21:09:47 +01002818 new UserHandle(mOriginatingUserId), 0);
Jorim Jaggid05aa3e2014-08-28 17:52:27 +02002819 }
2820
2821 private Bitmap getProfileBadge() {
2822 Drawable badge = getProfileBadgeDrawable();
Kenny Guy8a0101b2014-05-08 23:34:12 +01002823 if (badge == null) {
2824 return null;
2825 }
Jorim Jaggid05aa3e2014-08-28 17:52:27 +02002826 final int size = mContext.getResources().getDimensionPixelSize(
2827 R.dimen.notification_badge_size);
2828 Bitmap bitmap = Bitmap.createBitmap(size, size, Bitmap.Config.ARGB_8888);
Kenny Guy8a0101b2014-05-08 23:34:12 +01002829 Canvas canvas = new Canvas(bitmap);
Jorim Jaggid05aa3e2014-08-28 17:52:27 +02002830 badge.setBounds(0, 0, size, size);
Kenny Guy8a0101b2014-05-08 23:34:12 +01002831 badge.draw(canvas);
2832 return bitmap;
2833 }
2834
Kenny Guy98193ea2014-07-24 19:54:37 +01002835 private boolean addProfileBadge(RemoteViews contentView, int resId) {
2836 Bitmap profileBadge = getProfileBadge();
2837
2838 contentView.setViewVisibility(R.id.profile_badge_large_template, View.GONE);
2839 contentView.setViewVisibility(R.id.profile_badge_line2, View.GONE);
2840 contentView.setViewVisibility(R.id.profile_badge_line3, View.GONE);
2841
2842 if (profileBadge != null) {
2843 contentView.setImageViewBitmap(resId, profileBadge);
2844 contentView.setViewVisibility(resId, View.VISIBLE);
2845
2846 // Make sure Line 3 is visible. As badge will be here if there
2847 // is no text to display.
2848 if (resId == R.id.profile_badge_line3) {
2849 contentView.setViewVisibility(R.id.line3, View.VISIBLE);
2850 }
2851 return true;
2852 }
2853 return false;
2854 }
2855
Jorim Jaggid05aa3e2014-08-28 17:52:27 +02002856 private void shrinkLine3Text(RemoteViews contentView) {
2857 float subTextSize = mContext.getResources().getDimensionPixelSize(
2858 R.dimen.notification_subtext_size);
2859 contentView.setTextViewTextSize(R.id.text, TypedValue.COMPLEX_UNIT_PX, subTextSize);
2860 }
2861
Christoph Studerfe718432014-09-01 18:21:18 +02002862 private void unshrinkLine3Text(RemoteViews contentView) {
2863 float regularTextSize = mContext.getResources().getDimensionPixelSize(
2864 com.android.internal.R.dimen.notification_text_size);
2865 contentView.setTextViewTextSize(R.id.text, TypedValue.COMPLEX_UNIT_PX, regularTextSize);
2866 }
2867
2868 private void resetStandardTemplate(RemoteViews contentView) {
2869 removeLargeIconBackground(contentView);
2870 contentView.setViewPadding(R.id.icon, 0, 0, 0, 0);
2871 contentView.setImageViewResource(R.id.icon, 0);
2872 contentView.setInt(R.id.icon, "setBackgroundResource", 0);
2873 contentView.setViewVisibility(R.id.right_icon, View.GONE);
2874 contentView.setInt(R.id.right_icon, "setBackgroundResource", 0);
2875 contentView.setImageViewResource(R.id.right_icon, 0);
2876 contentView.setImageViewResource(R.id.icon, 0);
2877 contentView.setTextViewText(R.id.title, null);
2878 contentView.setTextViewText(R.id.text, null);
2879 unshrinkLine3Text(contentView);
2880 contentView.setTextViewText(R.id.text2, null);
2881 contentView.setViewVisibility(R.id.text2, View.GONE);
2882 contentView.setViewVisibility(R.id.info, View.GONE);
2883 contentView.setViewVisibility(R.id.time, View.GONE);
2884 contentView.setViewVisibility(R.id.line3, View.GONE);
2885 contentView.setViewVisibility(R.id.overflow_divider, View.GONE);
2886 contentView.setViewVisibility(R.id.progress, View.GONE);
Christoph Studerca1db712014-09-10 17:31:33 +02002887 contentView.setViewVisibility(R.id.chronometer, View.GONE);
2888 contentView.setViewVisibility(R.id.time, View.GONE);
Christoph Studerfe718432014-09-01 18:21:18 +02002889 }
2890
Jorim Jaggi445d3c02014-08-19 22:33:42 +02002891 private RemoteViews applyStandardTemplate(int resId) {
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02002892 return applyStandardTemplate(resId, true /* hasProgress */);
2893 }
2894
2895 /**
2896 * @param hasProgress whether the progress bar should be shown and set
2897 */
2898 private RemoteViews applyStandardTemplate(int resId, boolean hasProgress) {
Kenny Guy77320062014-08-27 21:37:15 +01002899 RemoteViews contentView = new BuilderRemoteViews(mContext.getApplicationInfo(), resId);
Dan Sandler539aad42014-08-04 00:43:39 -04002900
Christoph Studerfe718432014-09-01 18:21:18 +02002901 resetStandardTemplate(contentView);
2902
Daniel Sandler9f7936a2012-05-21 16:14:28 -04002903 boolean showLine3 = false;
2904 boolean showLine2 = false;
Kenny Guy98193ea2014-07-24 19:54:37 +01002905 boolean contentTextInLine2 = false;
Dan Sandler190d58d2014-05-15 09:33:39 -04002906
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01002907 if (mLargeIcon != null) {
Dan Sandlerd63f9322015-05-06 15:18:49 -04002908 contentView.setImageViewIcon(R.id.icon, mLargeIcon);
Christoph Studer239f8352014-08-25 15:13:18 +02002909 processLargeLegacyIcon(mLargeIcon, contentView);
Dan Sandlerd63f9322015-05-06 15:18:49 -04002910 contentView.setImageViewIcon(R.id.right_icon, mSmallIcon);
Dan Sandler190d58d2014-05-15 09:33:39 -04002911 contentView.setViewVisibility(R.id.right_icon, View.VISIBLE);
2912 processSmallRightIcon(mSmallIcon, contentView);
2913 } else { // small icon at left
Dan Sandlerd63f9322015-05-06 15:18:49 -04002914 contentView.setImageViewIcon(R.id.icon, mSmallIcon);
Dan Sandler190d58d2014-05-15 09:33:39 -04002915 contentView.setViewVisibility(R.id.icon, View.VISIBLE);
2916 processSmallIconAsLarge(mSmallIcon, contentView);
Joe Onorato561d3852010-11-20 18:09:34 -08002917 }
2918 if (mContentTitle != null) {
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01002919 contentView.setTextViewText(R.id.title, processLegacyText(mContentTitle));
Joe Onorato561d3852010-11-20 18:09:34 -08002920 }
2921 if (mContentText != null) {
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01002922 contentView.setTextViewText(R.id.text, processLegacyText(mContentText));
Daniel Sandler9f7936a2012-05-21 16:14:28 -04002923 showLine3 = true;
Joe Onorato561d3852010-11-20 18:09:34 -08002924 }
2925 if (mContentInfo != null) {
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01002926 contentView.setTextViewText(R.id.info, processLegacyText(mContentInfo));
Jeff Sharkey1c400132011-08-05 14:50:13 -07002927 contentView.setViewVisibility(R.id.info, View.VISIBLE);
Daniel Sandler9f7936a2012-05-21 16:14:28 -04002928 showLine3 = true;
Joe Onorato561d3852010-11-20 18:09:34 -08002929 } else if (mNumber > 0) {
Daniel Sandlerebce0112011-06-16 16:44:51 -04002930 final int tooBig = mContext.getResources().getInteger(
2931 R.integer.status_bar_notification_info_maxnum);
2932 if (mNumber > tooBig) {
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01002933 contentView.setTextViewText(R.id.info, processLegacyText(
2934 mContext.getResources().getString(
2935 R.string.status_bar_notification_info_overflow)));
Joe Onorato059a2f82011-01-04 10:27:01 -08002936 } else {
2937 NumberFormat f = NumberFormat.getIntegerInstance();
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01002938 contentView.setTextViewText(R.id.info, processLegacyText(f.format(mNumber)));
Joe Onorato059a2f82011-01-04 10:27:01 -08002939 }
Jeff Sharkey1c400132011-08-05 14:50:13 -07002940 contentView.setViewVisibility(R.id.info, View.VISIBLE);
Daniel Sandler9f7936a2012-05-21 16:14:28 -04002941 showLine3 = true;
Joe Onorato561d3852010-11-20 18:09:34 -08002942 } else {
2943 contentView.setViewVisibility(R.id.info, View.GONE);
2944 }
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002945
Daniel Sandler9f7936a2012-05-21 16:14:28 -04002946 // Need to show three lines?
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002947 if (mSubText != null) {
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01002948 contentView.setTextViewText(R.id.text, processLegacyText(mSubText));
Daniel Sandler9f7936a2012-05-21 16:14:28 -04002949 if (mContentText != null) {
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01002950 contentView.setTextViewText(R.id.text2, processLegacyText(mContentText));
Daniel Sandler9f7936a2012-05-21 16:14:28 -04002951 contentView.setViewVisibility(R.id.text2, View.VISIBLE);
2952 showLine2 = true;
Kenny Guy98193ea2014-07-24 19:54:37 +01002953 contentTextInLine2 = true;
Daniel Sandler9f7936a2012-05-21 16:14:28 -04002954 } else {
2955 contentView.setViewVisibility(R.id.text2, View.GONE);
2956 }
Jeff Sharkey1c400132011-08-05 14:50:13 -07002957 } else {
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002958 contentView.setViewVisibility(R.id.text2, View.GONE);
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02002959 if (hasProgress && (mProgressMax != 0 || mProgressIndeterminate)) {
Christoph Studeraca4b252014-09-09 15:58:41 +02002960 contentView.setViewVisibility(R.id.progress, View.VISIBLE);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002961 contentView.setProgressBar(
2962 R.id.progress, mProgressMax, mProgress, mProgressIndeterminate);
Jorim Jaggief72a192014-08-26 21:57:46 +02002963 contentView.setProgressBackgroundTintList(
Alan Viverette4a357cd2015-03-18 18:37:18 -07002964 R.id.progress, ColorStateList.valueOf(mContext.getColor(
Jorim Jaggief72a192014-08-26 21:57:46 +02002965 R.color.notification_progress_background_color)));
2966 if (mColor != COLOR_DEFAULT) {
2967 ColorStateList colorStateList = ColorStateList.valueOf(mColor);
2968 contentView.setProgressTintList(R.id.progress, colorStateList);
2969 contentView.setProgressIndeterminateTintList(R.id.progress, colorStateList);
2970 }
Daniel Sandler9f7936a2012-05-21 16:14:28 -04002971 showLine2 = true;
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002972 } else {
2973 contentView.setViewVisibility(R.id.progress, View.GONE);
2974 }
Jeff Sharkey1c400132011-08-05 14:50:13 -07002975 }
Daniel Sandler9f7936a2012-05-21 16:14:28 -04002976 if (showLine2) {
Jorim Jaggi445d3c02014-08-19 22:33:42 +02002977
2978 // need to shrink all the type to make sure everything fits
Jorim Jaggid05aa3e2014-08-28 17:52:27 +02002979 shrinkLine3Text(contentView);
Daniel Sandler9f7936a2012-05-21 16:14:28 -04002980 }
2981
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02002982 if (showsTimeOrChronometer()) {
Daniel Sandlera2985ed2012-04-03 16:42:00 -04002983 if (mUseChronometer) {
2984 contentView.setViewVisibility(R.id.chronometer, View.VISIBLE);
2985 contentView.setLong(R.id.chronometer, "setBase",
2986 mWhen + (SystemClock.elapsedRealtime() - System.currentTimeMillis()));
2987 contentView.setBoolean(R.id.chronometer, "setStarted", true);
2988 } else {
2989 contentView.setViewVisibility(R.id.time, View.VISIBLE);
2990 contentView.setLong(R.id.time, "setTime", mWhen);
2991 }
Joe Onorato561d3852010-11-20 18:09:34 -08002992 }
Daniel Sandler0c890492012-09-12 17:23:10 -07002993
Jorim Jaggi445d3c02014-08-19 22:33:42 +02002994 // Adjust padding depending on line count and font size.
2995 contentView.setViewPadding(R.id.line1, 0, calculateTopPadding(mContext,
Jorim Jaggid05aa3e2014-08-28 17:52:27 +02002996 mHasThreeLines, mContext.getResources().getConfiguration().fontScale),
Jorim Jaggi445d3c02014-08-19 22:33:42 +02002997 0, 0);
2998
Kenny Guy98193ea2014-07-24 19:54:37 +01002999 // We want to add badge to first line of text.
3000 boolean addedBadge = addProfileBadge(contentView,
3001 contentTextInLine2 ? R.id.profile_badge_line2 : R.id.profile_badge_line3);
3002 // If we added the badge to line 3 then we should show line 3.
3003 if (addedBadge && !contentTextInLine2) {
3004 showLine3 = true;
3005 }
3006
3007 // Note getStandardView may hide line 3 again.
Daniel Sandler9f7936a2012-05-21 16:14:28 -04003008 contentView.setViewVisibility(R.id.line3, showLine3 ? View.VISIBLE : View.GONE);
Daniel Sandler6387d2f2012-05-22 13:44:09 -04003009 contentView.setViewVisibility(R.id.overflow_divider, showLine3 ? View.VISIBLE : View.GONE);
Joe Onorato561d3852010-11-20 18:09:34 -08003010 return contentView;
3011 }
3012
Jorim Jaggi445d3c02014-08-19 22:33:42 +02003013 /**
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02003014 * @return true if the built notification will show the time or the chronometer; false
3015 * otherwise
3016 */
3017 private boolean showsTimeOrChronometer() {
3018 return mWhen != 0 && mShowWhen;
3019 }
3020
3021 /**
Jorim Jaggi445d3c02014-08-19 22:33:42 +02003022 * Logic to find out whether the notification is going to have three lines in the contracted
3023 * layout. This is used to adjust the top padding.
3024 *
3025 * @return true if the notification is going to have three lines; false if the notification
3026 * is going to have one or two lines
3027 */
3028 private boolean hasThreeLines() {
Jorim Jaggid05aa3e2014-08-28 17:52:27 +02003029 boolean contentTextInLine2 = mSubText != null && mContentText != null;
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02003030 boolean hasProgress = mStyle == null || mStyle.hasProgress();
Jorim Jaggid05aa3e2014-08-28 17:52:27 +02003031 // If we have content text in line 2, badge goes into line 2, or line 3 otherwise
3032 boolean badgeInLine3 = getProfileBadgeDrawable() != null && !contentTextInLine2;
3033 boolean hasLine3 = mContentText != null || mContentInfo != null || mNumber > 0
3034 || badgeInLine3;
Jorim Jaggi445d3c02014-08-19 22:33:42 +02003035 boolean hasLine2 = (mSubText != null && mContentText != null) ||
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02003036 (hasProgress && mSubText == null
3037 && (mProgressMax != 0 || mProgressIndeterminate));
Jorim Jaggi445d3c02014-08-19 22:33:42 +02003038 return hasLine2 && hasLine3;
3039 }
3040
3041 /**
3042 * @hide
3043 */
3044 public static int calculateTopPadding(Context ctx, boolean hasThreeLines,
3045 float fontScale) {
3046 int padding = ctx.getResources().getDimensionPixelSize(hasThreeLines
3047 ? R.dimen.notification_top_pad_narrow
3048 : R.dimen.notification_top_pad);
3049 int largePadding = ctx.getResources().getDimensionPixelSize(hasThreeLines
3050 ? R.dimen.notification_top_pad_large_text_narrow
3051 : R.dimen.notification_top_pad_large_text);
3052 float largeFactor = (MathUtils.constrain(fontScale, 1.0f, LARGE_TEXT_SCALE) - 1f)
3053 / (LARGE_TEXT_SCALE - 1f);
3054
3055 // Linearly interpolate the padding between large and normal with the font scale ranging
3056 // from 1f to LARGE_TEXT_SCALE
3057 return Math.round((1 - largeFactor) * padding + largeFactor * largePadding);
3058 }
3059
Christoph Studerfe718432014-09-01 18:21:18 +02003060 private void resetStandardTemplateWithActions(RemoteViews big) {
3061 big.setViewVisibility(R.id.actions, View.GONE);
3062 big.setViewVisibility(R.id.action_divider, View.GONE);
3063 big.removeAllViews(R.id.actions);
3064 }
3065
Daniel Sandler96fd7c12012-03-30 16:37:36 -04003066 private RemoteViews applyStandardTemplateWithActions(int layoutId) {
Jorim Jaggi445d3c02014-08-19 22:33:42 +02003067 RemoteViews big = applyStandardTemplate(layoutId);
Daniel Sandler96fd7c12012-03-30 16:37:36 -04003068
Christoph Studerfe718432014-09-01 18:21:18 +02003069 resetStandardTemplateWithActions(big);
3070
Daniel Sandler96fd7c12012-03-30 16:37:36 -04003071 int N = mActions.size();
3072 if (N > 0) {
Daniel Sandler96fd7c12012-03-30 16:37:36 -04003073 big.setViewVisibility(R.id.actions, View.VISIBLE);
Daniel Sandler6387d2f2012-05-22 13:44:09 -04003074 big.setViewVisibility(R.id.action_divider, View.VISIBLE);
Daniel Sandler8680bf82012-05-15 16:52:52 -04003075 if (N>MAX_ACTION_BUTTONS) N=MAX_ACTION_BUTTONS;
Daniel Sandler96fd7c12012-03-30 16:37:36 -04003076 for (int i=0; i<N; i++) {
3077 final RemoteViews button = generateActionButton(mActions.get(i));
Daniel Sandler96fd7c12012-03-30 16:37:36 -04003078 big.addView(R.id.actions, button);
3079 }
3080 }
3081 return big;
3082 }
3083
Joe Onorato46439ce2010-11-19 13:56:21 -08003084 private RemoteViews makeContentView() {
3085 if (mContentView != null) {
3086 return mContentView;
3087 } else {
Jorim Jaggi445d3c02014-08-19 22:33:42 +02003088 return applyStandardTemplate(getBaseLayoutResource());
Joe Onorato46439ce2010-11-19 13:56:21 -08003089 }
3090 }
3091
3092 private RemoteViews makeTickerView() {
3093 if (mTickerView != null) {
3094 return mTickerView;
Joe Onorato46439ce2010-11-19 13:56:21 -08003095 }
Dan Sandler5fcdf6e2014-07-18 11:31:15 -04003096 return null; // tickers are not created by default anymore
Joe Onorato46439ce2010-11-19 13:56:21 -08003097 }
3098
Daniel Sandler96fd7c12012-03-30 16:37:36 -04003099 private RemoteViews makeBigContentView() {
3100 if (mActions.size() == 0) return null;
3101
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01003102 return applyStandardTemplateWithActions(getBigBaseLayoutResource());
Daniel Sandler96fd7c12012-03-30 16:37:36 -04003103 }
3104
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01003105 private RemoteViews makeHeadsUpContentView() {
Chris Wren8fd39ec2014-02-27 17:43:26 -05003106 if (mActions.size() == 0) return null;
3107
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01003108 return applyStandardTemplateWithActions(getBigBaseLayoutResource());
Chris Wren8fd39ec2014-02-27 17:43:26 -05003109 }
3110
3111
Daniel Sandler96fd7c12012-03-30 16:37:36 -04003112 private RemoteViews generateActionButton(Action action) {
Daniel Sandler8680bf82012-05-15 16:52:52 -04003113 final boolean tombstone = (action.actionIntent == null);
Joe Malin8d40d042012-11-05 11:36:40 -08003114 RemoteViews button = new RemoteViews(mContext.getPackageName(),
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01003115 tombstone ? getActionTombstoneLayoutResource()
3116 : getActionLayoutResource());
Chris Wren8749ac8a2013-12-03 14:31:01 -05003117 button.setTextViewCompoundDrawablesRelative(R.id.action0, action.icon, 0, 0, 0);
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01003118 button.setTextViewText(R.id.action0, processLegacyText(action.title));
Daniel Sandler8680bf82012-05-15 16:52:52 -04003119 if (!tombstone) {
Daniel Sandlere5518842012-05-10 16:20:40 -04003120 button.setOnClickPendingIntent(R.id.action0, action.actionIntent);
Daniel Sandlere5518842012-05-10 16:20:40 -04003121 }
Daniel Sandler96fd7c12012-03-30 16:37:36 -04003122 button.setContentDescription(R.id.action0, action.title);
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01003123 processLegacyAction(action, button);
Daniel Sandler96fd7c12012-03-30 16:37:36 -04003124 return button;
3125 }
3126
Joe Onoratocb109a02011-01-18 17:57:41 -08003127 /**
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01003128 * @return Whether we are currently building a notification from a legacy (an app that
Alan Viverette3cb07a462014-06-06 14:19:53 -07003129 * doesn't create material notifications by itself) app.
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01003130 */
3131 private boolean isLegacy() {
Dan Sandler26e81cf2014-05-06 10:01:27 -04003132 return mColorUtil != null;
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01003133 }
3134
3135 private void processLegacyAction(Action action, RemoteViews button) {
Dan Sandler05c362d2014-09-03 00:16:27 +02003136 if (!isLegacy() || mColorUtil.isGrayscaleIcon(mContext, action.icon)) {
Christoph Studer239f8352014-08-25 15:13:18 +02003137 button.setTextViewCompoundDrawablesRelativeColorFilter(R.id.action0, 0,
Alan Viverette4a357cd2015-03-18 18:37:18 -07003138 mContext.getColor(R.color.notification_action_color_filter),
Christoph Studer239f8352014-08-25 15:13:18 +02003139 PorterDuff.Mode.MULTIPLY);
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01003140 }
3141 }
3142
3143 private CharSequence processLegacyText(CharSequence charSequence) {
3144 if (isLegacy()) {
Dan Sandler26e81cf2014-05-06 10:01:27 -04003145 return mColorUtil.invertCharSequenceColors(charSequence);
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01003146 } else {
3147 return charSequence;
3148 }
3149 }
3150
Dan Sandler26e81cf2014-05-06 10:01:27 -04003151 /**
3152 * Apply any necessary background to smallIcons being used in the largeIcon spot.
3153 */
Dan Sandlerd63f9322015-05-06 15:18:49 -04003154 private void processSmallIconAsLarge(Icon largeIcon, RemoteViews contentView) {
Jorim Jaggi92df1f22014-12-16 19:44:41 +01003155 if (!isLegacy()) {
3156 contentView.setDrawableParameters(R.id.icon, false, -1,
3157 0xFFFFFFFF,
3158 PorterDuff.Mode.SRC_ATOP, -1);
Dan Sandler26e81cf2014-05-06 10:01:27 -04003159 applyLargeIconBackground(contentView);
Dan Sandlerd63f9322015-05-06 15:18:49 -04003160 } else {
3161 if (mColorUtil.isGrayscaleIcon(mContext, largeIcon)) {
3162 applyLargeIconBackground(contentView);
3163 }
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01003164 }
3165 }
3166
Dan Sandler26e81cf2014-05-06 10:01:27 -04003167 /**
3168 * Apply any necessary background to a largeIcon if it's a fake smallIcon (that is,
3169 * if it's grayscale).
3170 */
3171 // TODO: also check bounds, transparency, that sort of thing.
Dan Sandlerd63f9322015-05-06 15:18:49 -04003172 private void processLargeLegacyIcon(Icon largeIcon, RemoteViews contentView) {
3173 if (largeIcon != null && isLegacy()
3174 && mColorUtil.isGrayscaleIcon(mContext, largeIcon)) {
Dan Sandler26e81cf2014-05-06 10:01:27 -04003175 applyLargeIconBackground(contentView);
Dan Sandler190d58d2014-05-15 09:33:39 -04003176 } else {
3177 removeLargeIconBackground(contentView);
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01003178 }
3179 }
3180
Dan Sandler26e81cf2014-05-06 10:01:27 -04003181 /**
3182 * Add a colored circle behind the largeIcon slot.
3183 */
3184 private void applyLargeIconBackground(RemoteViews contentView) {
3185 contentView.setInt(R.id.icon, "setBackgroundResource",
Jorim Jaggi445d3c02014-08-19 22:33:42 +02003186 R.drawable.notification_icon_legacy_bg);
Dan Sandler26e81cf2014-05-06 10:01:27 -04003187
3188 contentView.setDrawableParameters(
3189 R.id.icon,
3190 true,
3191 -1,
Jorim Jaggi74419312014-06-10 20:57:21 +02003192 resolveColor(),
Dan Sandler26e81cf2014-05-06 10:01:27 -04003193 PorterDuff.Mode.SRC_ATOP,
3194 -1);
Jorim Jaggi445d3c02014-08-19 22:33:42 +02003195
3196 int padding = mContext.getResources().getDimensionPixelSize(
3197 R.dimen.notification_large_icon_circle_padding);
3198 contentView.setViewPadding(R.id.icon, padding, padding, padding, padding);
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01003199 }
3200
Dan Sandler190d58d2014-05-15 09:33:39 -04003201 private void removeLargeIconBackground(RemoteViews contentView) {
3202 contentView.setInt(R.id.icon, "setBackgroundResource", 0);
3203 }
3204
Dan Sandler26e81cf2014-05-06 10:01:27 -04003205 /**
3206 * Recolor small icons when used in the R.id.right_icon slot.
3207 */
Dan Sandlerd63f9322015-05-06 15:18:49 -04003208 private void processSmallRightIcon(Icon smallIcon, RemoteViews contentView) {
Jorim Jaggi92df1f22014-12-16 19:44:41 +01003209 if (!isLegacy()) {
Dan Sandler190d58d2014-05-15 09:33:39 -04003210 contentView.setDrawableParameters(R.id.right_icon, false, -1,
3211 0xFFFFFFFF,
3212 PorterDuff.Mode.SRC_ATOP, -1);
Jorim Jaggi92df1f22014-12-16 19:44:41 +01003213 }
Dan Sandlerd63f9322015-05-06 15:18:49 -04003214 final boolean gray = isLegacy()
3215 && smallIcon.getType() == Icon.TYPE_RESOURCE
3216 && mColorUtil.isGrayscaleIcon(mContext, smallIcon.getResId());
3217 if (!isLegacy() || gray) {
Dan Sandler190d58d2014-05-15 09:33:39 -04003218 contentView.setInt(R.id.right_icon,
3219 "setBackgroundResource",
3220 R.drawable.notification_icon_legacy_bg);
3221
3222 contentView.setDrawableParameters(
3223 R.id.right_icon,
3224 true,
3225 -1,
Jorim Jaggi74419312014-06-10 20:57:21 +02003226 resolveColor(),
Dan Sandler190d58d2014-05-15 09:33:39 -04003227 PorterDuff.Mode.SRC_ATOP,
3228 -1);
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01003229 }
3230 }
3231
Jorim Jaggi74419312014-06-10 20:57:21 +02003232 private int sanitizeColor() {
3233 if (mColor != COLOR_DEFAULT) {
3234 mColor |= 0xFF000000; // no alpha for custom colors
3235 }
3236 return mColor;
3237 }
3238
Dan Sandler26e81cf2014-05-06 10:01:27 -04003239 private int resolveColor() {
3240 if (mColor == COLOR_DEFAULT) {
Alan Viverette4a357cd2015-03-18 18:37:18 -07003241 return mContext.getColor(R.color.notification_icon_bg_color);
Dan Sandler26e81cf2014-05-06 10:01:27 -04003242 }
3243 return mColor;
3244 }
3245
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01003246 /**
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003247 * Apply the unstyled operations and return a new {@link Notification} object.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04003248 * @hide
Joe Onoratocb109a02011-01-18 17:57:41 -08003249 */
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04003250 public Notification buildUnstyled() {
Joe Onorato46439ce2010-11-19 13:56:21 -08003251 Notification n = new Notification();
3252 n.when = mWhen;
Dan Sandlerd63f9322015-05-06 15:18:49 -04003253 n.mSmallIcon = mSmallIcon;
Dan Sandler3936e7a2015-05-19 20:59:12 -04003254 if (mSmallIcon != null && mSmallIcon.getType() == Icon.TYPE_RESOURCE) {
Dan Sandlerd63f9322015-05-06 15:18:49 -04003255 n.icon = mSmallIcon.getResId();
3256 }
Joe Onorato46439ce2010-11-19 13:56:21 -08003257 n.iconLevel = mSmallIconLevel;
Joe Onorato8595a3d2010-11-19 18:12:07 -08003258 n.number = mNumber;
Dan Sandler26e81cf2014-05-06 10:01:27 -04003259
Jorim Jaggi74419312014-06-10 20:57:21 +02003260 n.color = sanitizeColor();
Dan Sandler26e81cf2014-05-06 10:01:27 -04003261
Christoph Studer4600f9b2014-07-22 22:44:43 +02003262 setBuilderContentView(n, makeContentView());
Joe Onorato46439ce2010-11-19 13:56:21 -08003263 n.contentIntent = mContentIntent;
3264 n.deleteIntent = mDeleteIntent;
3265 n.fullScreenIntent = mFullScreenIntent;
3266 n.tickerText = mTickerText;
3267 n.tickerView = makeTickerView();
Dan Sandlerd63f9322015-05-06 15:18:49 -04003268 n.mLargeIcon = mLargeIcon;
3269 if (mLargeIcon != null && mLargeIcon.getType() == Icon.TYPE_BITMAP) {
3270 n.largeIcon = mLargeIcon.getBitmap();
3271 }
Joe Onorato46439ce2010-11-19 13:56:21 -08003272 n.sound = mSound;
3273 n.audioStreamType = mAudioStreamType;
John Spurlockc0650f022014-07-19 13:22:39 -04003274 n.audioAttributes = mAudioAttributes;
Joe Onorato46439ce2010-11-19 13:56:21 -08003275 n.vibrate = mVibrate;
3276 n.ledARGB = mLedArgb;
3277 n.ledOnMS = mLedOnMs;
3278 n.ledOffMS = mLedOffMs;
3279 n.defaults = mDefaults;
3280 n.flags = mFlags;
Christoph Studer4600f9b2014-07-22 22:44:43 +02003281 setBuilderBigContentView(n, makeBigContentView());
3282 setBuilderHeadsUpContentView(n, makeHeadsUpContentView());
Daniel Sandler26c13432013-04-04 11:01:04 -04003283 if (mLedOnMs != 0 || mLedOffMs != 0) {
Joe Onorato8d0b6552010-11-22 16:09:29 -08003284 n.flags |= FLAG_SHOW_LIGHTS;
3285 }
3286 if ((mDefaults & DEFAULT_LIGHTS) != 0) {
3287 n.flags |= FLAG_SHOW_LIGHTS;
3288 }
John Spurlockfd7f1e02014-03-18 16:41:57 -04003289 n.category = mCategory;
Griff Hazen5cadc3b2014-05-20 09:55:39 -07003290 n.mGroupKey = mGroupKey;
3291 n.mSortKey = mSortKey;
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003292 n.priority = mPriority;
Daniel Sandlera0a938c2012-03-15 08:42:37 -04003293 if (mActions.size() > 0) {
3294 n.actions = new Action[mActions.size()];
3295 mActions.toArray(n.actions);
3296 }
Dan Sandler0bf2ed82013-12-21 23:33:41 -06003297 n.visibility = mVisibility;
3298
3299 if (mPublicVersion != null) {
3300 n.publicVersion = new Notification();
3301 mPublicVersion.cloneInto(n.publicVersion, true);
3302 }
Christoph Studer4600f9b2014-07-22 22:44:43 +02003303 // Note: If you're adding new fields, also update restoreFromNotitification().
Joe Onorato46439ce2010-11-19 13:56:21 -08003304 return n;
3305 }
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003306
3307 /**
Daniel Sandlerf45564e2013-04-15 15:05:08 -04003308 * Capture, in the provided bundle, semantic information used in the construction of
3309 * this Notification object.
3310 * @hide
3311 */
Griff Hazen720042b2014-02-24 15:46:56 -08003312 public void populateExtras(Bundle extras) {
Daniel Sandlerf45564e2013-04-15 15:05:08 -04003313 // Store original information used in the construction of this object
Kenny Guy8942bcd2014-09-08 21:09:47 +01003314 extras.putInt(EXTRA_ORIGINATING_USERID, mOriginatingUserId);
Kenny Guy77320062014-08-27 21:37:15 +01003315 extras.putParcelable(EXTRA_REBUILD_CONTEXT_APPLICATION_INFO,
3316 mContext.getApplicationInfo());
Daniel Sandlerf45564e2013-04-15 15:05:08 -04003317 extras.putCharSequence(EXTRA_TITLE, mContentTitle);
3318 extras.putCharSequence(EXTRA_TEXT, mContentText);
3319 extras.putCharSequence(EXTRA_SUB_TEXT, mSubText);
3320 extras.putCharSequence(EXTRA_INFO_TEXT, mContentInfo);
Dan Sandlerd63f9322015-05-06 15:18:49 -04003321 extras.putParcelable(EXTRA_SMALL_ICON, mSmallIcon);
Daniel Sandlerf45564e2013-04-15 15:05:08 -04003322 extras.putInt(EXTRA_PROGRESS, mProgress);
3323 extras.putInt(EXTRA_PROGRESS_MAX, mProgressMax);
3324 extras.putBoolean(EXTRA_PROGRESS_INDETERMINATE, mProgressIndeterminate);
3325 extras.putBoolean(EXTRA_SHOW_CHRONOMETER, mUseChronometer);
3326 extras.putBoolean(EXTRA_SHOW_WHEN, mShowWhen);
John Spurlockac08a472013-06-10 11:37:08 -04003327 if (mLargeIcon != null) {
3328 extras.putParcelable(EXTRA_LARGE_ICON, mLargeIcon);
3329 }
Chris Wrendde75302014-03-26 17:24:15 -04003330 if (!mPeople.isEmpty()) {
3331 extras.putStringArray(EXTRA_PEOPLE, mPeople.toArray(new String[mPeople.size()]));
3332 }
Christoph Studer4600f9b2014-07-22 22:44:43 +02003333 // NOTE: If you're adding new extras also update restoreFromNotification().
3334 }
3335
3336
3337 /**
3338 * @hide
3339 */
3340 public static void stripForDelivery(Notification n) {
3341 if (!STRIP_AND_REBUILD) {
3342 return;
3343 }
3344
3345 String templateClass = n.extras.getString(EXTRA_TEMPLATE);
3346 // Only strip views for known Styles because we won't know how to
3347 // re-create them otherwise.
3348 boolean stripViews = TextUtils.isEmpty(templateClass) ||
3349 getNotificationStyleClass(templateClass) != null;
3350
3351 boolean isStripped = false;
3352
3353 if (n.largeIcon != null && n.extras.containsKey(EXTRA_LARGE_ICON)) {
3354 // TODO: Would like to check for equality here, but if the notification
3355 // has been cloned, we can't.
3356 n.largeIcon = null;
3357 n.extras.putBoolean(EXTRA_REBUILD_LARGE_ICON, true);
3358 isStripped = true;
3359 }
3360 // Get rid of unmodified BuilderRemoteViews.
3361
3362 if (stripViews &&
3363 n.contentView instanceof BuilderRemoteViews &&
3364 n.extras.getInt(EXTRA_REBUILD_CONTENT_VIEW_ACTION_COUNT, -1) ==
3365 n.contentView.getSequenceNumber()) {
3366 n.contentView = null;
3367 n.extras.putBoolean(EXTRA_REBUILD_CONTENT_VIEW, true);
3368 n.extras.remove(EXTRA_REBUILD_CONTENT_VIEW_ACTION_COUNT);
3369 isStripped = true;
3370 }
3371 if (stripViews &&
3372 n.bigContentView instanceof BuilderRemoteViews &&
3373 n.extras.getInt(EXTRA_REBUILD_BIG_CONTENT_VIEW_ACTION_COUNT, -1) ==
3374 n.bigContentView.getSequenceNumber()) {
3375 n.bigContentView = null;
3376 n.extras.putBoolean(EXTRA_REBUILD_BIG_CONTENT_VIEW, true);
3377 n.extras.remove(EXTRA_REBUILD_BIG_CONTENT_VIEW_ACTION_COUNT);
3378 isStripped = true;
3379 }
3380 if (stripViews &&
3381 n.headsUpContentView instanceof BuilderRemoteViews &&
3382 n.extras.getInt(EXTRA_REBUILD_HEADS_UP_CONTENT_VIEW_ACTION_COUNT, -1) ==
3383 n.headsUpContentView.getSequenceNumber()) {
3384 n.headsUpContentView = null;
3385 n.extras.putBoolean(EXTRA_REBUILD_HEADS_UP_CONTENT_VIEW, true);
3386 n.extras.remove(EXTRA_REBUILD_HEADS_UP_CONTENT_VIEW_ACTION_COUNT);
3387 isStripped = true;
3388 }
3389
3390 if (isStripped) {
3391 n.extras.putBoolean(EXTRA_NEEDS_REBUILD, true);
3392 }
3393 }
3394
3395 /**
3396 * @hide
3397 */
3398 public static Notification rebuild(Context context, Notification n) {
3399 Bundle extras = n.extras;
3400 if (!extras.getBoolean(EXTRA_NEEDS_REBUILD)) return n;
3401 extras.remove(EXTRA_NEEDS_REBUILD);
3402
3403 // Re-create notification context so we can access app resources.
Kenny Guy77320062014-08-27 21:37:15 +01003404 ApplicationInfo applicationInfo = extras.getParcelable(
3405 EXTRA_REBUILD_CONTEXT_APPLICATION_INFO);
Christoph Studer4600f9b2014-07-22 22:44:43 +02003406 Context builderContext;
3407 try {
Kenny Guy77320062014-08-27 21:37:15 +01003408 builderContext = context.createApplicationContext(applicationInfo,
Christoph Studer4600f9b2014-07-22 22:44:43 +02003409 Context.CONTEXT_RESTRICTED);
3410 } catch (NameNotFoundException e) {
Kenny Guy77320062014-08-27 21:37:15 +01003411 Log.e(TAG, "ApplicationInfo " + applicationInfo + " not found");
Christoph Studer4600f9b2014-07-22 22:44:43 +02003412 builderContext = context; // try with our context
3413 }
3414
3415 Builder b = new Builder(builderContext, n);
3416 return b.rebuild();
3417 }
3418
3419 /**
3420 * Rebuilds the notification passed in to the rebuild-constructor
3421 * {@link #Builder(Context, Notification)}.
3422 *
3423 * <p>
3424 * Throws IllegalStateException when invoked on a Builder that isn't in rebuild mode.
3425 *
3426 * @hide
3427 */
3428 private Notification rebuild() {
3429 if (mRebuildNotification == null) {
3430 throw new IllegalStateException("rebuild() only valid when in 'rebuild' mode.");
3431 }
Jorim Jaggid05aa3e2014-08-28 17:52:27 +02003432 mHasThreeLines = hasThreeLines();
Christoph Studer4600f9b2014-07-22 22:44:43 +02003433
3434 Bundle extras = mRebuildNotification.extras;
3435
3436 if (extras.getBoolean(EXTRA_REBUILD_LARGE_ICON)) {
3437 mRebuildNotification.largeIcon = extras.getParcelable(EXTRA_LARGE_ICON);
3438 }
3439 extras.remove(EXTRA_REBUILD_LARGE_ICON);
3440
3441 if (extras.getBoolean(EXTRA_REBUILD_CONTENT_VIEW)) {
3442 setBuilderContentView(mRebuildNotification, makeContentView());
3443 if (mStyle != null) {
3444 mStyle.populateContentView(mRebuildNotification);
3445 }
3446 }
3447 extras.remove(EXTRA_REBUILD_CONTENT_VIEW);
3448
3449 if (extras.getBoolean(EXTRA_REBUILD_BIG_CONTENT_VIEW)) {
3450 setBuilderBigContentView(mRebuildNotification, makeBigContentView());
3451 if (mStyle != null) {
3452 mStyle.populateBigContentView(mRebuildNotification);
3453 }
3454 }
3455 extras.remove(EXTRA_REBUILD_BIG_CONTENT_VIEW);
3456
3457 if (extras.getBoolean(EXTRA_REBUILD_HEADS_UP_CONTENT_VIEW)) {
3458 setBuilderHeadsUpContentView(mRebuildNotification, makeHeadsUpContentView());
3459 if (mStyle != null) {
3460 mStyle.populateHeadsUpContentView(mRebuildNotification);
3461 }
3462 }
3463 extras.remove(EXTRA_REBUILD_HEADS_UP_CONTENT_VIEW);
3464
Jorim Jaggid05aa3e2014-08-28 17:52:27 +02003465 mHasThreeLines = false;
Christoph Studer4600f9b2014-07-22 22:44:43 +02003466 return mRebuildNotification;
3467 }
3468
3469 private static Class<? extends Style> getNotificationStyleClass(String templateClass) {
3470 Class<? extends Style>[] classes = new Class[]{
3471 BigTextStyle.class, BigPictureStyle.class, InboxStyle.class, MediaStyle.class};
3472 for (Class<? extends Style> innerClass : classes) {
3473 if (templateClass.equals(innerClass.getName())) {
3474 return innerClass;
3475 }
3476 }
3477 return null;
3478 }
3479
3480 private void setBuilderContentView(Notification n, RemoteViews contentView) {
3481 n.contentView = contentView;
3482 if (contentView instanceof BuilderRemoteViews) {
3483 mRebuildBundle.putInt(Builder.EXTRA_REBUILD_CONTENT_VIEW_ACTION_COUNT,
3484 contentView.getSequenceNumber());
3485 }
3486 }
3487
3488 private void setBuilderBigContentView(Notification n, RemoteViews bigContentView) {
3489 n.bigContentView = bigContentView;
3490 if (bigContentView instanceof BuilderRemoteViews) {
3491 mRebuildBundle.putInt(Builder.EXTRA_REBUILD_BIG_CONTENT_VIEW_ACTION_COUNT,
3492 bigContentView.getSequenceNumber());
3493 }
3494 }
3495
3496 private void setBuilderHeadsUpContentView(Notification n,
3497 RemoteViews headsUpContentView) {
3498 n.headsUpContentView = headsUpContentView;
3499 if (headsUpContentView instanceof BuilderRemoteViews) {
3500 mRebuildBundle.putInt(Builder.EXTRA_REBUILD_HEADS_UP_CONTENT_VIEW_ACTION_COUNT,
3501 headsUpContentView.getSequenceNumber());
3502 }
3503 }
3504
3505 private void restoreFromNotification(Notification n) {
3506
3507 // Notification fields.
3508 mWhen = n.when;
Dan Sandlerd63f9322015-05-06 15:18:49 -04003509 mSmallIcon = n.mSmallIcon;
Christoph Studer4600f9b2014-07-22 22:44:43 +02003510 mSmallIconLevel = n.iconLevel;
3511 mNumber = n.number;
3512
3513 mColor = n.color;
3514
3515 mContentView = n.contentView;
3516 mDeleteIntent = n.deleteIntent;
3517 mFullScreenIntent = n.fullScreenIntent;
3518 mTickerText = n.tickerText;
3519 mTickerView = n.tickerView;
Dan Sandlerd63f9322015-05-06 15:18:49 -04003520 mLargeIcon = n.mLargeIcon;
Christoph Studer4600f9b2014-07-22 22:44:43 +02003521 mSound = n.sound;
3522 mAudioStreamType = n.audioStreamType;
3523 mAudioAttributes = n.audioAttributes;
3524
3525 mVibrate = n.vibrate;
3526 mLedArgb = n.ledARGB;
3527 mLedOnMs = n.ledOnMS;
3528 mLedOffMs = n.ledOffMS;
3529 mDefaults = n.defaults;
3530 mFlags = n.flags;
3531
3532 mCategory = n.category;
3533 mGroupKey = n.mGroupKey;
3534 mSortKey = n.mSortKey;
3535 mPriority = n.priority;
3536 mActions.clear();
3537 if (n.actions != null) {
3538 Collections.addAll(mActions, n.actions);
3539 }
3540 mVisibility = n.visibility;
3541
3542 mPublicVersion = n.publicVersion;
3543
3544 // Extras.
3545 Bundle extras = n.extras;
Kenny Guy8942bcd2014-09-08 21:09:47 +01003546 mOriginatingUserId = extras.getInt(EXTRA_ORIGINATING_USERID);
Christoph Studer4600f9b2014-07-22 22:44:43 +02003547 mContentTitle = extras.getCharSequence(EXTRA_TITLE);
3548 mContentText = extras.getCharSequence(EXTRA_TEXT);
3549 mSubText = extras.getCharSequence(EXTRA_SUB_TEXT);
3550 mContentInfo = extras.getCharSequence(EXTRA_INFO_TEXT);
Dan Sandlerd63f9322015-05-06 15:18:49 -04003551 mSmallIcon = extras.getParcelable(EXTRA_SMALL_ICON);
Christoph Studer4600f9b2014-07-22 22:44:43 +02003552 mProgress = extras.getInt(EXTRA_PROGRESS);
3553 mProgressMax = extras.getInt(EXTRA_PROGRESS_MAX);
3554 mProgressIndeterminate = extras.getBoolean(EXTRA_PROGRESS_INDETERMINATE);
3555 mUseChronometer = extras.getBoolean(EXTRA_SHOW_CHRONOMETER);
3556 mShowWhen = extras.getBoolean(EXTRA_SHOW_WHEN);
3557 if (extras.containsKey(EXTRA_LARGE_ICON)) {
3558 mLargeIcon = extras.getParcelable(EXTRA_LARGE_ICON);
3559 }
3560 if (extras.containsKey(EXTRA_PEOPLE)) {
3561 mPeople.clear();
3562 Collections.addAll(mPeople, extras.getStringArray(EXTRA_PEOPLE));
3563 }
Daniel Sandlerf45564e2013-04-15 15:05:08 -04003564 }
3565
3566 /**
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003567 * @deprecated Use {@link #build()} instead.
3568 */
3569 @Deprecated
3570 public Notification getNotification() {
3571 return build();
3572 }
3573
3574 /**
3575 * Combine all of the options that have been set and return a new {@link Notification}
3576 * object.
3577 */
3578 public Notification build() {
Kenny Guy8942bcd2014-09-08 21:09:47 +01003579 mOriginatingUserId = mContext.getUserId();
Jorim Jaggid05aa3e2014-08-28 17:52:27 +02003580 mHasThreeLines = hasThreeLines();
Christoph Studer943aa672014-08-03 20:31:16 +02003581
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04003582 Notification n = buildUnstyled();
Daniel Sandlerf45564e2013-04-15 15:05:08 -04003583
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003584 if (mStyle != null) {
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04003585 n = mStyle.buildStyled(n);
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003586 }
Daniel Sandlerf45564e2013-04-15 15:05:08 -04003587
Christoph Studer4600f9b2014-07-22 22:44:43 +02003588 if (mExtras != null) {
3589 n.extras.putAll(mExtras);
3590 }
3591
3592 if (mRebuildBundle.size() > 0) {
3593 n.extras.putAll(mRebuildBundle);
3594 mRebuildBundle.clear();
3595 }
Daniel Sandlerf45564e2013-04-15 15:05:08 -04003596
Griff Hazen720042b2014-02-24 15:46:56 -08003597 populateExtras(n.extras);
Daniel Sandlerf45564e2013-04-15 15:05:08 -04003598 if (mStyle != null) {
3599 mStyle.addExtras(n.extras);
3600 }
3601
Jorim Jaggid05aa3e2014-08-28 17:52:27 +02003602 mHasThreeLines = false;
Daniel Sandlerf45564e2013-04-15 15:05:08 -04003603 return n;
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003604 }
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05003605
3606 /**
3607 * Apply this Builder to an existing {@link Notification} object.
3608 *
3609 * @hide
3610 */
3611 public Notification buildInto(Notification n) {
Daniel Sandler1a497d32013-04-18 14:52:45 -04003612 build().cloneInto(n, true);
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05003613 return n;
3614 }
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01003615
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01003616 private int getBaseLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07003617 return R.layout.notification_template_material_base;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01003618 }
3619
3620 private int getBigBaseLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07003621 return R.layout.notification_template_material_big_base;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01003622 }
3623
3624 private int getBigPictureLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07003625 return R.layout.notification_template_material_big_picture;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01003626 }
3627
3628 private int getBigTextLayoutResource() {
Jorim Jaggi445d3c02014-08-19 22:33:42 +02003629 return R.layout.notification_template_material_big_text;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01003630 }
3631
3632 private int getInboxLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07003633 return R.layout.notification_template_material_inbox;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01003634 }
3635
3636 private int getActionLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07003637 return R.layout.notification_material_action;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01003638 }
3639
3640 private int getActionTombstoneLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07003641 return R.layout.notification_material_action_tombstone;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01003642 }
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003643 }
3644
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003645 /**
3646 * An object that can apply a rich notification style to a {@link Notification.Builder}
3647 * object.
3648 */
Griff Hazendfcb0802014-02-11 12:00:00 -08003649 public static abstract class Style {
Chris Wrend6297db2012-05-03 16:20:13 -04003650 private CharSequence mBigContentTitle;
Jorim Jaggi457a10d2014-09-08 16:18:23 +02003651
3652 /**
3653 * @hide
3654 */
3655 protected CharSequence mSummaryText = null;
3656
3657 /**
3658 * @hide
3659 */
3660 protected boolean mSummaryTextSet = false;
Chris Wrend6297db2012-05-03 16:20:13 -04003661
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003662 protected Builder mBuilder;
3663
Chris Wrend6297db2012-05-03 16:20:13 -04003664 /**
3665 * Overrides ContentTitle in the big form of the template.
3666 * This defaults to the value passed to setContentTitle().
3667 */
3668 protected void internalSetBigContentTitle(CharSequence title) {
3669 mBigContentTitle = title;
3670 }
3671
3672 /**
3673 * Set the first line of text after the detail section in the big form of the template.
3674 */
3675 protected void internalSetSummaryText(CharSequence cs) {
3676 mSummaryText = cs;
Daniel Sandler619738c2012-06-07 16:33:08 -04003677 mSummaryTextSet = true;
Chris Wrend6297db2012-05-03 16:20:13 -04003678 }
3679
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003680 public void setBuilder(Builder builder) {
3681 if (mBuilder != builder) {
3682 mBuilder = builder;
Daniel Sandlerc08dea22012-06-28 08:35:24 -07003683 if (mBuilder != null) {
3684 mBuilder.setStyle(this);
3685 }
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003686 }
3687 }
3688
Chris Wrend6297db2012-05-03 16:20:13 -04003689 protected void checkBuilder() {
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003690 if (mBuilder == null) {
3691 throw new IllegalArgumentException("Style requires a valid Builder object");
3692 }
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003693 }
Chris Wrend6297db2012-05-03 16:20:13 -04003694
3695 protected RemoteViews getStandardView(int layoutId) {
3696 checkBuilder();
3697
Christoph Studer4600f9b2014-07-22 22:44:43 +02003698 // Nasty.
3699 CharSequence oldBuilderContentTitle = mBuilder.mContentTitle;
Chris Wrend6297db2012-05-03 16:20:13 -04003700 if (mBigContentTitle != null) {
3701 mBuilder.setContentTitle(mBigContentTitle);
3702 }
3703
Chris Wrend6297db2012-05-03 16:20:13 -04003704 RemoteViews contentView = mBuilder.applyStandardTemplateWithActions(layoutId);
3705
Christoph Studer4600f9b2014-07-22 22:44:43 +02003706 mBuilder.mContentTitle = oldBuilderContentTitle;
3707
Chris Wrend6297db2012-05-03 16:20:13 -04003708 if (mBigContentTitle != null && mBigContentTitle.equals("")) {
3709 contentView.setViewVisibility(R.id.line1, View.GONE);
Chris Wren67dc9a02012-05-16 01:03:20 -04003710 } else {
3711 contentView.setViewVisibility(R.id.line1, View.VISIBLE);
Chris Wrend6297db2012-05-03 16:20:13 -04003712 }
3713
Daniel Sandler619738c2012-06-07 16:33:08 -04003714 // The last line defaults to the subtext, but can be replaced by mSummaryText
3715 final CharSequence overflowText =
3716 mSummaryTextSet ? mSummaryText
3717 : mBuilder.mSubText;
3718 if (overflowText != null) {
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01003719 contentView.setTextViewText(R.id.text, mBuilder.processLegacyText(overflowText));
Daniel Sandler619738c2012-06-07 16:33:08 -04003720 contentView.setViewVisibility(R.id.overflow_divider, View.VISIBLE);
Daniel Sandler9f7936a2012-05-21 16:14:28 -04003721 contentView.setViewVisibility(R.id.line3, View.VISIBLE);
Daniel Sandler916ad912012-06-13 12:17:07 -04003722 } else {
Kenny Guy98193ea2014-07-24 19:54:37 +01003723 // Clear text in case we use the line to show the profile badge.
3724 contentView.setTextViewText(R.id.text, "");
Daniel Sandler916ad912012-06-13 12:17:07 -04003725 contentView.setViewVisibility(R.id.overflow_divider, View.GONE);
3726 contentView.setViewVisibility(R.id.line3, View.GONE);
Chris Wrend6297db2012-05-03 16:20:13 -04003727 }
3728
3729 return contentView;
3730 }
3731
Daniel Sandlerf45564e2013-04-15 15:05:08 -04003732 /**
Jorim Jaggi445d3c02014-08-19 22:33:42 +02003733 * Changes the padding of the first line such that the big and small content view have the
3734 * same top padding.
3735 *
3736 * @hide
3737 */
3738 protected void applyTopPadding(RemoteViews contentView) {
3739 int topPadding = Builder.calculateTopPadding(mBuilder.mContext,
Jorim Jaggid05aa3e2014-08-28 17:52:27 +02003740 mBuilder.mHasThreeLines,
Jorim Jaggi445d3c02014-08-19 22:33:42 +02003741 mBuilder.mContext.getResources().getConfiguration().fontScale);
3742 contentView.setViewPadding(R.id.line1, 0, topPadding, 0, 0);
3743 }
3744
3745 /**
Daniel Sandlerf45564e2013-04-15 15:05:08 -04003746 * @hide
3747 */
3748 public void addExtras(Bundle extras) {
3749 if (mSummaryTextSet) {
3750 extras.putCharSequence(EXTRA_SUMMARY_TEXT, mSummaryText);
3751 }
3752 if (mBigContentTitle != null) {
3753 extras.putCharSequence(EXTRA_TITLE_BIG, mBigContentTitle);
3754 }
Chris Wren91ad5632013-06-05 15:05:57 -04003755 extras.putString(EXTRA_TEMPLATE, this.getClass().getName());
Daniel Sandlerf45564e2013-04-15 15:05:08 -04003756 }
3757
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04003758 /**
3759 * @hide
3760 */
Christoph Studer4600f9b2014-07-22 22:44:43 +02003761 protected void restoreFromExtras(Bundle extras) {
3762 if (extras.containsKey(EXTRA_SUMMARY_TEXT)) {
3763 mSummaryText = extras.getCharSequence(EXTRA_SUMMARY_TEXT);
3764 mSummaryTextSet = true;
3765 }
3766 if (extras.containsKey(EXTRA_TITLE_BIG)) {
3767 mBigContentTitle = extras.getCharSequence(EXTRA_TITLE_BIG);
3768 }
3769 }
3770
3771
3772 /**
3773 * @hide
3774 */
3775 public Notification buildStyled(Notification wip) {
3776 populateTickerView(wip);
3777 populateContentView(wip);
3778 populateBigContentView(wip);
3779 populateHeadsUpContentView(wip);
3780 return wip;
3781 }
3782
3783 // The following methods are split out so we can re-create notification partially.
3784 /**
3785 * @hide
3786 */
3787 protected void populateTickerView(Notification wip) {}
3788 /**
3789 * @hide
3790 */
3791 protected void populateContentView(Notification wip) {}
3792
3793 /**
3794 * @hide
3795 */
3796 protected void populateBigContentView(Notification wip) {}
3797
3798 /**
3799 * @hide
3800 */
3801 protected void populateHeadsUpContentView(Notification wip) {}
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04003802
3803 /**
3804 * Calls {@link android.app.Notification.Builder#build()} on the Builder this Style is
3805 * attached to.
3806 *
3807 * @return the fully constructed Notification.
3808 */
3809 public Notification build() {
3810 checkBuilder();
3811 return mBuilder.build();
3812 }
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02003813
3814 /**
3815 * @hide
3816 * @return true if the style positions the progress bar on the second line; false if the
3817 * style hides the progress bar
3818 */
3819 protected boolean hasProgress() {
3820 return true;
3821 }
Joe Onorato46439ce2010-11-19 13:56:21 -08003822 }
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003823
3824 /**
Daniel Sandler4dfbe832012-04-11 14:51:46 -04003825 * Helper class for generating large-format notifications that include a large image attachment.
Joe Malin8d40d042012-11-05 11:36:40 -08003826 *
Robert Ly91c5ce32014-06-08 15:37:00 -07003827 * Here's how you'd set the <code>BigPictureStyle</code> on a notification:
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003828 * <pre class="prettyprint">
Robert Ly91c5ce32014-06-08 15:37:00 -07003829 * Notification notif = new Notification.Builder(mContext)
3830 * .setContentTitle(&quot;New photo from &quot; + sender.toString())
3831 * .setContentText(subject)
3832 * .setSmallIcon(R.drawable.new_post)
3833 * .setLargeIcon(aBitmap)
3834 * .setStyle(new Notification.BigPictureStyle()
3835 * .bigPicture(aBigBitmap))
3836 * .build();
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003837 * </pre>
Joe Malin8d40d042012-11-05 11:36:40 -08003838 *
Daniel Sandler4dfbe832012-04-11 14:51:46 -04003839 * @see Notification#bigContentView
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003840 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003841 public static class BigPictureStyle extends Style {
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003842 private Bitmap mPicture;
Dan Sandlerd63f9322015-05-06 15:18:49 -04003843 private Icon mBigLargeIcon;
Chris Wren3745a3d2012-05-22 15:11:52 -04003844 private boolean mBigLargeIconSet = false;
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003845
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003846 public BigPictureStyle() {
3847 }
3848
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003849 public BigPictureStyle(Builder builder) {
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003850 setBuilder(builder);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003851 }
3852
Chris Wrend6297db2012-05-03 16:20:13 -04003853 /**
3854 * Overrides ContentTitle in the big form of the template.
3855 * This defaults to the value passed to setContentTitle().
3856 */
3857 public BigPictureStyle setBigContentTitle(CharSequence title) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04003858 internalSetBigContentTitle(safeCharSequence(title));
Chris Wrend6297db2012-05-03 16:20:13 -04003859 return this;
3860 }
3861
3862 /**
3863 * Set the first line of text after the detail section in the big form of the template.
3864 */
3865 public BigPictureStyle setSummaryText(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04003866 internalSetSummaryText(safeCharSequence(cs));
Chris Wrend6297db2012-05-03 16:20:13 -04003867 return this;
3868 }
3869
Chris Wren0bd664d2012-08-01 13:56:56 -04003870 /**
3871 * Provide the bitmap to be used as the payload for the BigPicture notification.
3872 */
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003873 public BigPictureStyle bigPicture(Bitmap b) {
3874 mPicture = b;
3875 return this;
3876 }
3877
Chris Wren3745a3d2012-05-22 15:11:52 -04003878 /**
Chris Wren3745a3d2012-05-22 15:11:52 -04003879 * Override the large icon when the big notification is shown.
3880 */
3881 public BigPictureStyle bigLargeIcon(Bitmap b) {
Dan Sandlerd63f9322015-05-06 15:18:49 -04003882 return bigLargeIcon(b != null ? Icon.createWithBitmap(b) : null);
3883 }
3884
3885 /**
3886 * Override the large icon when the big notification is shown.
3887 */
3888 public BigPictureStyle bigLargeIcon(Icon icon) {
Chris Wren3745a3d2012-05-22 15:11:52 -04003889 mBigLargeIconSet = true;
Dan Sandlerd63f9322015-05-06 15:18:49 -04003890 mBigLargeIcon = icon;
Chris Wren3745a3d2012-05-22 15:11:52 -04003891 return this;
3892 }
3893
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003894 private RemoteViews makeBigContentView() {
Christoph Studer5c510ee2014-12-15 16:32:27 +01003895
3896 // Replace mLargeIcon with mBigLargeIcon if mBigLargeIconSet
3897 // This covers the following cases:
3898 // 1. mBigLargeIconSet -> mBigLargeIcon (null or non-null) applies, overrides
3899 // mLargeIcon
3900 // 2. !mBigLargeIconSet -> mLargeIcon applies
Dan Sandlerd63f9322015-05-06 15:18:49 -04003901 Icon oldLargeIcon = null;
Christoph Studer5c510ee2014-12-15 16:32:27 +01003902 if (mBigLargeIconSet) {
3903 oldLargeIcon = mBuilder.mLargeIcon;
3904 mBuilder.mLargeIcon = mBigLargeIcon;
3905 }
3906
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01003907 RemoteViews contentView = getStandardView(mBuilder.getBigPictureLayoutResource());
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003908
Christoph Studer5c510ee2014-12-15 16:32:27 +01003909 if (mBigLargeIconSet) {
3910 mBuilder.mLargeIcon = oldLargeIcon;
3911 }
3912
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003913 contentView.setImageViewBitmap(R.id.big_picture, mPicture);
3914
Jorim Jaggi445d3c02014-08-19 22:33:42 +02003915 applyTopPadding(contentView);
3916
Kenny Guy98193ea2014-07-24 19:54:37 +01003917 boolean twoTextLines = mBuilder.mSubText != null && mBuilder.mContentText != null;
3918 mBuilder.addProfileBadge(contentView,
3919 twoTextLines ? R.id.profile_badge_line2 : R.id.profile_badge_line3);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003920 return contentView;
3921 }
3922
Daniel Sandlerf45564e2013-04-15 15:05:08 -04003923 /**
3924 * @hide
3925 */
3926 public void addExtras(Bundle extras) {
3927 super.addExtras(extras);
3928
3929 if (mBigLargeIconSet) {
3930 extras.putParcelable(EXTRA_LARGE_ICON_BIG, mBigLargeIcon);
3931 }
3932 extras.putParcelable(EXTRA_PICTURE, mPicture);
3933 }
3934
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04003935 /**
3936 * @hide
3937 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003938 @Override
Christoph Studer4600f9b2014-07-22 22:44:43 +02003939 protected void restoreFromExtras(Bundle extras) {
3940 super.restoreFromExtras(extras);
3941
3942 if (extras.containsKey(EXTRA_LARGE_ICON_BIG)) {
Christoph Studer5c510ee2014-12-15 16:32:27 +01003943 mBigLargeIconSet = true;
Christoph Studer4600f9b2014-07-22 22:44:43 +02003944 mBigLargeIcon = extras.getParcelable(EXTRA_LARGE_ICON_BIG);
Chris Wren3745a3d2012-05-22 15:11:52 -04003945 }
Christoph Studer4600f9b2014-07-22 22:44:43 +02003946 mPicture = extras.getParcelable(EXTRA_PICTURE);
3947 }
3948
3949 /**
3950 * @hide
3951 */
3952 @Override
3953 public void populateBigContentView(Notification wip) {
3954 mBuilder.setBuilderBigContentView(wip, makeBigContentView());
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003955 }
3956 }
3957
3958 /**
Daniel Sandler4dfbe832012-04-11 14:51:46 -04003959 * Helper class for generating large-format notifications that include a lot of text.
Joe Malin8d40d042012-11-05 11:36:40 -08003960 *
Robert Ly91c5ce32014-06-08 15:37:00 -07003961 * Here's how you'd set the <code>BigTextStyle</code> on a notification:
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003962 * <pre class="prettyprint">
Robert Ly91c5ce32014-06-08 15:37:00 -07003963 * Notification notif = new Notification.Builder(mContext)
3964 * .setContentTitle(&quot;New mail from &quot; + sender.toString())
3965 * .setContentText(subject)
3966 * .setSmallIcon(R.drawable.new_mail)
3967 * .setLargeIcon(aBitmap)
3968 * .setStyle(new Notification.BigTextStyle()
3969 * .bigText(aVeryLongString))
3970 * .build();
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003971 * </pre>
Joe Malin8d40d042012-11-05 11:36:40 -08003972 *
Daniel Sandler4dfbe832012-04-11 14:51:46 -04003973 * @see Notification#bigContentView
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003974 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003975 public static class BigTextStyle extends Style {
Jorim Jaggi457a10d2014-09-08 16:18:23 +02003976
3977 private static final int MAX_LINES = 13;
3978 private static final int LINES_CONSUMED_BY_ACTIONS = 3;
3979 private static final int LINES_CONSUMED_BY_SUMMARY = 2;
3980
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003981 private CharSequence mBigText;
3982
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003983 public BigTextStyle() {
3984 }
3985
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003986 public BigTextStyle(Builder builder) {
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003987 setBuilder(builder);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003988 }
3989
Chris Wrend6297db2012-05-03 16:20:13 -04003990 /**
3991 * Overrides ContentTitle in the big form of the template.
3992 * This defaults to the value passed to setContentTitle().
3993 */
3994 public BigTextStyle setBigContentTitle(CharSequence title) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04003995 internalSetBigContentTitle(safeCharSequence(title));
Chris Wrend6297db2012-05-03 16:20:13 -04003996 return this;
3997 }
3998
3999 /**
4000 * Set the first line of text after the detail section in the big form of the template.
4001 */
4002 public BigTextStyle setSummaryText(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04004003 internalSetSummaryText(safeCharSequence(cs));
Chris Wrend6297db2012-05-03 16:20:13 -04004004 return this;
4005 }
4006
Chris Wren0bd664d2012-08-01 13:56:56 -04004007 /**
4008 * Provide the longer text to be displayed in the big form of the
4009 * template in place of the content text.
4010 */
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004011 public BigTextStyle bigText(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04004012 mBigText = safeCharSequence(cs);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004013 return this;
4014 }
4015
Daniel Sandlerf45564e2013-04-15 15:05:08 -04004016 /**
4017 * @hide
4018 */
4019 public void addExtras(Bundle extras) {
4020 super.addExtras(extras);
4021
Christoph Studer4600f9b2014-07-22 22:44:43 +02004022 extras.putCharSequence(EXTRA_BIG_TEXT, mBigText);
4023 }
4024
4025 /**
4026 * @hide
4027 */
4028 @Override
4029 protected void restoreFromExtras(Bundle extras) {
4030 super.restoreFromExtras(extras);
4031
4032 mBigText = extras.getCharSequence(EXTRA_BIG_TEXT);
Daniel Sandlerf45564e2013-04-15 15:05:08 -04004033 }
4034
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004035 private RemoteViews makeBigContentView() {
Christoph Studer4600f9b2014-07-22 22:44:43 +02004036
4037 // Nasty
4038 CharSequence oldBuilderContentText = mBuilder.mContentText;
Daniel Sandler6387d2f2012-05-22 13:44:09 -04004039 mBuilder.mContentText = null;
Daniel Sandler916ad912012-06-13 12:17:07 -04004040
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01004041 RemoteViews contentView = getStandardView(mBuilder.getBigTextLayoutResource());
Joe Malin8d40d042012-11-05 11:36:40 -08004042
Christoph Studer4600f9b2014-07-22 22:44:43 +02004043 mBuilder.mContentText = oldBuilderContentText;
4044
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01004045 contentView.setTextViewText(R.id.big_text, mBuilder.processLegacyText(mBigText));
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004046 contentView.setViewVisibility(R.id.big_text, View.VISIBLE);
Jorim Jaggi457a10d2014-09-08 16:18:23 +02004047 contentView.setInt(R.id.big_text, "setMaxLines", calculateMaxLines());
Chris Wren3c5f92432012-05-04 16:31:17 -04004048 contentView.setViewVisibility(R.id.text2, View.GONE);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004049
Jorim Jaggi445d3c02014-08-19 22:33:42 +02004050 applyTopPadding(contentView);
4051
Jorim Jaggid05aa3e2014-08-28 17:52:27 +02004052 mBuilder.shrinkLine3Text(contentView);
4053
Kenny Guy98193ea2014-07-24 19:54:37 +01004054 mBuilder.addProfileBadge(contentView, R.id.profile_badge_large_template);
4055
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004056 return contentView;
4057 }
4058
Jorim Jaggi457a10d2014-09-08 16:18:23 +02004059 private int calculateMaxLines() {
4060 int lineCount = MAX_LINES;
4061 boolean hasActions = mBuilder.mActions.size() > 0;
4062 boolean hasSummary = (mSummaryTextSet ? mSummaryText : mBuilder.mSubText) != null;
4063 if (hasActions) {
4064 lineCount -= LINES_CONSUMED_BY_ACTIONS;
4065 }
4066 if (hasSummary) {
4067 lineCount -= LINES_CONSUMED_BY_SUMMARY;
4068 }
4069
4070 // If we have less top padding at the top, we can fit less lines.
4071 if (!mBuilder.mHasThreeLines) {
4072 lineCount--;
4073 }
4074 return lineCount;
4075 }
4076
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04004077 /**
4078 * @hide
4079 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004080 @Override
Christoph Studer4600f9b2014-07-22 22:44:43 +02004081 public void populateBigContentView(Notification wip) {
4082 mBuilder.setBuilderBigContentView(wip, makeBigContentView());
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004083 }
4084 }
Daniel Sandler879c5e02012-04-17 16:46:51 -04004085
4086 /**
4087 * Helper class for generating large-format notifications that include a list of (up to 5) strings.
Joe Malin8d40d042012-11-05 11:36:40 -08004088 *
Robert Ly91c5ce32014-06-08 15:37:00 -07004089 * Here's how you'd set the <code>InboxStyle</code> on a notification:
Daniel Sandler879c5e02012-04-17 16:46:51 -04004090 * <pre class="prettyprint">
Robert Ly91c5ce32014-06-08 15:37:00 -07004091 * Notification notif = new Notification.Builder(mContext)
4092 * .setContentTitle(&quot;5 New mails from &quot; + sender.toString())
4093 * .setContentText(subject)
4094 * .setSmallIcon(R.drawable.new_mail)
4095 * .setLargeIcon(aBitmap)
4096 * .setStyle(new Notification.InboxStyle()
4097 * .addLine(str1)
4098 * .addLine(str2)
4099 * .setContentTitle(&quot;&quot;)
4100 * .setSummaryText(&quot;+3 more&quot;))
4101 * .build();
Daniel Sandler879c5e02012-04-17 16:46:51 -04004102 * </pre>
Joe Malin8d40d042012-11-05 11:36:40 -08004103 *
Daniel Sandler879c5e02012-04-17 16:46:51 -04004104 * @see Notification#bigContentView
4105 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004106 public static class InboxStyle extends Style {
Daniel Sandler879c5e02012-04-17 16:46:51 -04004107 private ArrayList<CharSequence> mTexts = new ArrayList<CharSequence>(5);
4108
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004109 public InboxStyle() {
4110 }
4111
Daniel Sandler879c5e02012-04-17 16:46:51 -04004112 public InboxStyle(Builder builder) {
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004113 setBuilder(builder);
Daniel Sandler879c5e02012-04-17 16:46:51 -04004114 }
4115
Chris Wrend6297db2012-05-03 16:20:13 -04004116 /**
4117 * Overrides ContentTitle in the big form of the template.
4118 * This defaults to the value passed to setContentTitle().
4119 */
4120 public InboxStyle setBigContentTitle(CharSequence title) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04004121 internalSetBigContentTitle(safeCharSequence(title));
Chris Wrend6297db2012-05-03 16:20:13 -04004122 return this;
4123 }
4124
4125 /**
4126 * Set the first line of text after the detail section in the big form of the template.
4127 */
4128 public InboxStyle setSummaryText(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04004129 internalSetSummaryText(safeCharSequence(cs));
Chris Wrend6297db2012-05-03 16:20:13 -04004130 return this;
4131 }
4132
Chris Wren0bd664d2012-08-01 13:56:56 -04004133 /**
4134 * Append a line to the digest section of the Inbox notification.
4135 */
Daniel Sandler879c5e02012-04-17 16:46:51 -04004136 public InboxStyle addLine(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04004137 mTexts.add(safeCharSequence(cs));
Daniel Sandler879c5e02012-04-17 16:46:51 -04004138 return this;
4139 }
4140
Daniel Sandlerf45564e2013-04-15 15:05:08 -04004141 /**
4142 * @hide
4143 */
4144 public void addExtras(Bundle extras) {
4145 super.addExtras(extras);
Christoph Studer4600f9b2014-07-22 22:44:43 +02004146
Daniel Sandlerf45564e2013-04-15 15:05:08 -04004147 CharSequence[] a = new CharSequence[mTexts.size()];
4148 extras.putCharSequenceArray(EXTRA_TEXT_LINES, mTexts.toArray(a));
4149 }
4150
Christoph Studer4600f9b2014-07-22 22:44:43 +02004151 /**
4152 * @hide
4153 */
4154 @Override
4155 protected void restoreFromExtras(Bundle extras) {
4156 super.restoreFromExtras(extras);
4157
4158 mTexts.clear();
4159 if (extras.containsKey(EXTRA_TEXT_LINES)) {
4160 Collections.addAll(mTexts, extras.getCharSequenceArray(EXTRA_TEXT_LINES));
4161 }
4162 }
4163
Daniel Sandler879c5e02012-04-17 16:46:51 -04004164 private RemoteViews makeBigContentView() {
Daniel Sandler619738c2012-06-07 16:33:08 -04004165 // Remove the content text so line3 disappears unless you have a summary
Christoph Studer4600f9b2014-07-22 22:44:43 +02004166
4167 // Nasty
4168 CharSequence oldBuilderContentText = mBuilder.mContentText;
Daniel Sandler619738c2012-06-07 16:33:08 -04004169 mBuilder.mContentText = null;
Christoph Studer4600f9b2014-07-22 22:44:43 +02004170
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01004171 RemoteViews contentView = getStandardView(mBuilder.getInboxLayoutResource());
Daniel Sandler619738c2012-06-07 16:33:08 -04004172
Christoph Studer4600f9b2014-07-22 22:44:43 +02004173 mBuilder.mContentText = oldBuilderContentText;
4174
Chris Wrend6297db2012-05-03 16:20:13 -04004175 contentView.setViewVisibility(R.id.text2, View.GONE);
Daniel Sandler879c5e02012-04-17 16:46:51 -04004176
Chris Wrend6297db2012-05-03 16:20:13 -04004177 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 -04004178 R.id.inbox_text4, R.id.inbox_text5, R.id.inbox_text6};
Chris Wrend6297db2012-05-03 16:20:13 -04004179
Chris Wren4ed80d52012-05-17 09:30:03 -04004180 // Make sure all rows are gone in case we reuse a view.
4181 for (int rowId : rowIds) {
4182 contentView.setViewVisibility(rowId, View.GONE);
4183 }
4184
Jorim Jaggi445d3c02014-08-19 22:33:42 +02004185 final boolean largeText =
4186 mBuilder.mContext.getResources().getConfiguration().fontScale > 1f;
4187 final float subTextSize = mBuilder.mContext.getResources().getDimensionPixelSize(
4188 R.dimen.notification_subtext_size);
Daniel Sandler879c5e02012-04-17 16:46:51 -04004189 int i=0;
4190 while (i < mTexts.size() && i < rowIds.length) {
4191 CharSequence str = mTexts.get(i);
4192 if (str != null && !str.equals("")) {
4193 contentView.setViewVisibility(rowIds[i], View.VISIBLE);
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01004194 contentView.setTextViewText(rowIds[i], mBuilder.processLegacyText(str));
Jorim Jaggi445d3c02014-08-19 22:33:42 +02004195 if (largeText) {
4196 contentView.setTextViewTextSize(rowIds[i], TypedValue.COMPLEX_UNIT_PX,
4197 subTextSize);
4198 }
Daniel Sandler879c5e02012-04-17 16:46:51 -04004199 }
4200 i++;
4201 }
4202
Chris Wren683ab002012-09-20 10:35:54 -04004203 contentView.setViewVisibility(R.id.inbox_end_pad,
4204 mTexts.size() > 0 ? View.VISIBLE : View.GONE);
4205
4206 contentView.setViewVisibility(R.id.inbox_more,
4207 mTexts.size() > rowIds.length ? View.VISIBLE : View.GONE);
Chris Wren29bb6d92012-05-17 18:09:42 -04004208
Jorim Jaggi445d3c02014-08-19 22:33:42 +02004209 applyTopPadding(contentView);
4210
Jorim Jaggid05aa3e2014-08-28 17:52:27 +02004211 mBuilder.shrinkLine3Text(contentView);
4212
Kenny Guy98193ea2014-07-24 19:54:37 +01004213 mBuilder.addProfileBadge(contentView, R.id.profile_badge_large_template);
4214
Daniel Sandler879c5e02012-04-17 16:46:51 -04004215 return contentView;
4216 }
4217
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04004218 /**
4219 * @hide
4220 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004221 @Override
Christoph Studer4600f9b2014-07-22 22:44:43 +02004222 public void populateBigContentView(Notification wip) {
4223 mBuilder.setBuilderBigContentView(wip, makeBigContentView());
Daniel Sandler879c5e02012-04-17 16:46:51 -04004224 }
4225 }
Dan Sandler842dd772014-05-15 09:36:47 -04004226
4227 /**
4228 * Notification style for media playback notifications.
4229 *
4230 * In the expanded form, {@link Notification#bigContentView}, up to 5
4231 * {@link Notification.Action}s specified with
4232 * {@link Notification.Builder#addAction(int, CharSequence, PendingIntent) addAction} will be
4233 * shown as icon-only pushbuttons, suitable for transport controls. The Bitmap given to
4234 * {@link Notification.Builder#setLargeIcon(android.graphics.Bitmap) setLargeIcon()} will be
4235 * treated as album artwork.
4236 *
4237 * Unlike the other styles provided here, MediaStyle can also modify the standard-size
4238 * {@link Notification#contentView}; by providing action indices to
Christoph Studerfde6f4d2014-12-12 13:23:26 +01004239 * {@link #setShowActionsInCompactView(int...)} you can promote up to 3 actions to be displayed
Dan Sandler842dd772014-05-15 09:36:47 -04004240 * in the standard view alongside the usual content.
4241 *
Bryan Mawhinney6be8de32014-07-18 10:35:12 +01004242 * Notifications created with MediaStyle will have their category set to
4243 * {@link Notification#CATEGORY_TRANSPORT CATEGORY_TRANSPORT} unless you set a different
4244 * category using {@link Notification.Builder#setCategory(String) setCategory()}.
4245 *
Jeff Browndba34ba2014-06-24 20:46:03 -07004246 * Finally, if you attach a {@link android.media.session.MediaSession.Token} using
4247 * {@link android.app.Notification.MediaStyle#setMediaSession(MediaSession.Token)},
Dan Sandler842dd772014-05-15 09:36:47 -04004248 * the System UI can identify this as a notification representing an active media session
4249 * and respond accordingly (by showing album artwork in the lockscreen, for example).
4250 *
4251 * To use this style with your Notification, feed it to
4252 * {@link Notification.Builder#setStyle(android.app.Notification.Style)} like so:
4253 * <pre class="prettyprint">
4254 * Notification noti = new Notification.Builder()
4255 * .setSmallIcon(R.drawable.ic_stat_player)
Christoph Studere935fe92014-11-24 14:18:06 +01004256 * .setContentTitle(&quot;Track title&quot;)
4257 * .setContentText(&quot;Artist - Album&quot;)
4258 * .setLargeIcon(albumArtBitmap))
Bryan Mawhinney6be8de32014-07-18 10:35:12 +01004259 * .setStyle(<b>new Notification.MediaStyle()</b>
4260 * .setMediaSession(mySession))
Dan Sandler842dd772014-05-15 09:36:47 -04004261 * .build();
4262 * </pre>
4263 *
4264 * @see Notification#bigContentView
4265 */
4266 public static class MediaStyle extends Style {
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02004267 static final int MAX_MEDIA_BUTTONS_IN_COMPACT = 3;
Dan Sandler842dd772014-05-15 09:36:47 -04004268 static final int MAX_MEDIA_BUTTONS = 5;
4269
4270 private int[] mActionsToShowInCompact = null;
Jeff Browndba34ba2014-06-24 20:46:03 -07004271 private MediaSession.Token mToken;
Dan Sandler842dd772014-05-15 09:36:47 -04004272
4273 public MediaStyle() {
4274 }
4275
4276 public MediaStyle(Builder builder) {
4277 setBuilder(builder);
4278 }
4279
4280 /**
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02004281 * 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 -04004282 * notification view.
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02004283 *
4284 * @param actions the indices of the actions to show in the compact notification view
Dan Sandler842dd772014-05-15 09:36:47 -04004285 */
4286 public MediaStyle setShowActionsInCompactView(int...actions) {
4287 mActionsToShowInCompact = actions;
4288 return this;
4289 }
4290
4291 /**
Jeff Browndba34ba2014-06-24 20:46:03 -07004292 * Attach a {@link android.media.session.MediaSession.Token} to this Notification
4293 * to provide additional playback information and control to the SystemUI.
Dan Sandler842dd772014-05-15 09:36:47 -04004294 */
Jeff Browndba34ba2014-06-24 20:46:03 -07004295 public MediaStyle setMediaSession(MediaSession.Token token) {
Dan Sandler842dd772014-05-15 09:36:47 -04004296 mToken = token;
4297 return this;
4298 }
4299
Christoph Studer4600f9b2014-07-22 22:44:43 +02004300 /**
4301 * @hide
4302 */
Dan Sandler842dd772014-05-15 09:36:47 -04004303 @Override
4304 public Notification buildStyled(Notification wip) {
Christoph Studer4600f9b2014-07-22 22:44:43 +02004305 super.buildStyled(wip);
Bryan Mawhinney6be8de32014-07-18 10:35:12 +01004306 if (wip.category == null) {
4307 wip.category = Notification.CATEGORY_TRANSPORT;
4308 }
Dan Sandler842dd772014-05-15 09:36:47 -04004309 return wip;
4310 }
4311
Christoph Studer4600f9b2014-07-22 22:44:43 +02004312 /**
4313 * @hide
4314 */
4315 @Override
4316 public void populateContentView(Notification wip) {
4317 mBuilder.setBuilderContentView(wip, makeMediaContentView());
4318 }
4319
4320 /**
4321 * @hide
4322 */
4323 @Override
4324 public void populateBigContentView(Notification wip) {
4325 mBuilder.setBuilderBigContentView(wip, makeMediaBigContentView());
4326 }
4327
Dan Sandler842dd772014-05-15 09:36:47 -04004328 /** @hide */
4329 @Override
4330 public void addExtras(Bundle extras) {
4331 super.addExtras(extras);
4332
4333 if (mToken != null) {
4334 extras.putParcelable(EXTRA_MEDIA_SESSION, mToken);
4335 }
Bryan Mawhinneye191f902014-07-22 12:50:09 +01004336 if (mActionsToShowInCompact != null) {
4337 extras.putIntArray(EXTRA_COMPACT_ACTIONS, mActionsToShowInCompact);
4338 }
Dan Sandler842dd772014-05-15 09:36:47 -04004339 }
4340
Christoph Studer4600f9b2014-07-22 22:44:43 +02004341 /**
4342 * @hide
4343 */
4344 @Override
4345 protected void restoreFromExtras(Bundle extras) {
4346 super.restoreFromExtras(extras);
4347
4348 if (extras.containsKey(EXTRA_MEDIA_SESSION)) {
4349 mToken = extras.getParcelable(EXTRA_MEDIA_SESSION);
4350 }
4351 if (extras.containsKey(EXTRA_COMPACT_ACTIONS)) {
4352 mActionsToShowInCompact = extras.getIntArray(EXTRA_COMPACT_ACTIONS);
4353 }
4354 }
4355
Dan Sandler842dd772014-05-15 09:36:47 -04004356 private RemoteViews generateMediaActionButton(Action action) {
4357 final boolean tombstone = (action.actionIntent == null);
4358 RemoteViews button = new RemoteViews(mBuilder.mContext.getPackageName(),
Alan Viverette3cb07a462014-06-06 14:19:53 -07004359 R.layout.notification_material_media_action);
Dan Sandler842dd772014-05-15 09:36:47 -04004360 button.setImageViewResource(R.id.action0, action.icon);
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02004361 button.setDrawableParameters(R.id.action0, false, -1,
4362 0xFFFFFFFF,
4363 PorterDuff.Mode.SRC_ATOP, -1);
Dan Sandler842dd772014-05-15 09:36:47 -04004364 if (!tombstone) {
4365 button.setOnClickPendingIntent(R.id.action0, action.actionIntent);
4366 }
4367 button.setContentDescription(R.id.action0, action.title);
4368 return button;
4369 }
4370
4371 private RemoteViews makeMediaContentView() {
4372 RemoteViews view = mBuilder.applyStandardTemplate(
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02004373 R.layout.notification_template_material_media, false /* hasProgress */);
Dan Sandler842dd772014-05-15 09:36:47 -04004374
4375 final int numActions = mBuilder.mActions.size();
4376 final int N = mActionsToShowInCompact == null
4377 ? 0
4378 : Math.min(mActionsToShowInCompact.length, MAX_MEDIA_BUTTONS_IN_COMPACT);
4379 if (N > 0) {
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02004380 view.removeAllViews(com.android.internal.R.id.media_actions);
Dan Sandler842dd772014-05-15 09:36:47 -04004381 for (int i = 0; i < N; i++) {
4382 if (i >= numActions) {
4383 throw new IllegalArgumentException(String.format(
4384 "setShowActionsInCompactView: action %d out of bounds (max %d)",
4385 i, numActions - 1));
4386 }
4387
4388 final Action action = mBuilder.mActions.get(mActionsToShowInCompact[i]);
4389 final RemoteViews button = generateMediaActionButton(action);
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02004390 view.addView(com.android.internal.R.id.media_actions, button);
Dan Sandler842dd772014-05-15 09:36:47 -04004391 }
4392 }
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02004393 styleText(view);
4394 hideRightIcon(view);
Dan Sandler842dd772014-05-15 09:36:47 -04004395 return view;
4396 }
4397
4398 private RemoteViews makeMediaBigContentView() {
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02004399 final int actionCount = Math.min(mBuilder.mActions.size(), MAX_MEDIA_BUTTONS);
4400 RemoteViews big = mBuilder.applyStandardTemplate(getBigLayoutResource(actionCount),
4401 false /* hasProgress */);
Dan Sandler842dd772014-05-15 09:36:47 -04004402
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02004403 if (actionCount > 0) {
4404 big.removeAllViews(com.android.internal.R.id.media_actions);
4405 for (int i = 0; i < actionCount; i++) {
Dan Sandler842dd772014-05-15 09:36:47 -04004406 final RemoteViews button = generateMediaActionButton(mBuilder.mActions.get(i));
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02004407 big.addView(com.android.internal.R.id.media_actions, button);
Dan Sandler842dd772014-05-15 09:36:47 -04004408 }
4409 }
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02004410 styleText(big);
4411 hideRightIcon(big);
4412 applyTopPadding(big);
4413 big.setViewVisibility(android.R.id.progress, View.GONE);
Dan Sandler842dd772014-05-15 09:36:47 -04004414 return big;
4415 }
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02004416
4417 private int getBigLayoutResource(int actionCount) {
4418 if (actionCount <= 3) {
4419 return R.layout.notification_template_material_big_media_narrow;
4420 } else {
4421 return R.layout.notification_template_material_big_media;
4422 }
4423 }
4424
4425 private void hideRightIcon(RemoteViews contentView) {
4426 contentView.setViewVisibility(R.id.right_icon, View.GONE);
4427 }
4428
4429 /**
4430 * Applies the special text colors for media notifications to all text views.
4431 */
4432 private void styleText(RemoteViews contentView) {
Alan Viverette4a357cd2015-03-18 18:37:18 -07004433 int primaryColor = mBuilder.mContext.getColor(
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02004434 R.color.notification_media_primary_color);
Alan Viverette4a357cd2015-03-18 18:37:18 -07004435 int secondaryColor = mBuilder.mContext.getColor(
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02004436 R.color.notification_media_secondary_color);
4437 contentView.setTextColor(R.id.title, primaryColor);
4438 if (mBuilder.showsTimeOrChronometer()) {
4439 if (mBuilder.mUseChronometer) {
4440 contentView.setTextColor(R.id.chronometer, secondaryColor);
4441 } else {
4442 contentView.setTextColor(R.id.time, secondaryColor);
4443 }
4444 }
4445 contentView.setTextColor(R.id.text2, secondaryColor);
4446 contentView.setTextColor(R.id.text, secondaryColor);
4447 contentView.setTextColor(R.id.info, secondaryColor);
4448 }
4449
4450 /**
4451 * @hide
4452 */
4453 @Override
4454 protected boolean hasProgress() {
4455 return false;
4456 }
Dan Sandler842dd772014-05-15 09:36:47 -04004457 }
Griff Hazen61a9e862014-05-22 16:05:19 -07004458
Christoph Studer4600f9b2014-07-22 22:44:43 +02004459 // When adding a new Style subclass here, don't forget to update
4460 // Builder.getNotificationStyleClass.
4461
Griff Hazen61a9e862014-05-22 16:05:19 -07004462 /**
4463 * Extender interface for use with {@link Builder#extend}. Extenders may be used to add
4464 * metadata or change options on a notification builder.
4465 */
4466 public interface Extender {
4467 /**
4468 * Apply this extender to a notification builder.
4469 * @param builder the builder to be modified.
4470 * @return the build object for chaining.
4471 */
4472 public Builder extend(Builder builder);
4473 }
4474
4475 /**
4476 * Helper class to add wearable extensions to notifications.
4477 * <p class="note"> See
4478 * <a href="{@docRoot}wear/notifications/creating.html">Creating Notifications
4479 * for Android Wear</a> for more information on how to use this class.
4480 * <p>
4481 * To create a notification with wearable extensions:
4482 * <ol>
4483 * <li>Create a {@link android.app.Notification.Builder}, setting any desired
4484 * properties.
4485 * <li>Create a {@link android.app.Notification.WearableExtender}.
4486 * <li>Set wearable-specific properties using the
4487 * {@code add} and {@code set} methods of {@link android.app.Notification.WearableExtender}.
4488 * <li>Call {@link android.app.Notification.Builder#extend} to apply the extensions to a
4489 * notification.
4490 * <li>Post the notification to the notification system with the
4491 * {@code NotificationManager.notify(...)} methods.
4492 * </ol>
4493 *
4494 * <pre class="prettyprint">
4495 * Notification notif = new Notification.Builder(mContext)
4496 * .setContentTitle(&quot;New mail from &quot; + sender.toString())
4497 * .setContentText(subject)
4498 * .setSmallIcon(R.drawable.new_mail)
4499 * .extend(new Notification.WearableExtender()
4500 * .setContentIcon(R.drawable.new_mail))
4501 * .build();
4502 * NotificationManager notificationManger =
4503 * (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
4504 * notificationManger.notify(0, notif);</pre>
4505 *
4506 * <p>Wearable extensions can be accessed on an existing notification by using the
4507 * {@code WearableExtender(Notification)} constructor,
4508 * and then using the {@code get} methods to access values.
4509 *
4510 * <pre class="prettyprint">
4511 * Notification.WearableExtender wearableExtender = new Notification.WearableExtender(
4512 * notification);
Griff Hazen14f57992014-05-26 09:07:14 -07004513 * List&lt;Notification&gt; pages = wearableExtender.getPages();</pre>
Griff Hazen61a9e862014-05-22 16:05:19 -07004514 */
4515 public static final class WearableExtender implements Extender {
4516 /**
4517 * Sentinel value for an action index that is unset.
4518 */
4519 public static final int UNSET_ACTION_INDEX = -1;
4520
4521 /**
4522 * Size value for use with {@link #setCustomSizePreset} to show this notification with
4523 * default sizing.
4524 * <p>For custom display notifications created using {@link #setDisplayIntent},
4525 * the default is {@link #SIZE_LARGE}. All other notifications size automatically based
4526 * on their content.
4527 */
4528 public static final int SIZE_DEFAULT = 0;
4529
4530 /**
4531 * Size value for use with {@link #setCustomSizePreset} to show this notification
4532 * with an extra small size.
4533 * <p>This value is only applicable for custom display notifications created using
4534 * {@link #setDisplayIntent}.
4535 */
4536 public static final int SIZE_XSMALL = 1;
4537
4538 /**
4539 * Size value for use with {@link #setCustomSizePreset} to show this notification
4540 * with a small size.
4541 * <p>This value is only applicable for custom display notifications created using
4542 * {@link #setDisplayIntent}.
4543 */
4544 public static final int SIZE_SMALL = 2;
4545
4546 /**
4547 * Size value for use with {@link #setCustomSizePreset} to show this notification
4548 * with a medium size.
4549 * <p>This value is only applicable for custom display notifications created using
4550 * {@link #setDisplayIntent}.
4551 */
4552 public static final int SIZE_MEDIUM = 3;
4553
4554 /**
4555 * Size value for use with {@link #setCustomSizePreset} to show this notification
4556 * with a large size.
4557 * <p>This value is only applicable for custom display notifications created using
4558 * {@link #setDisplayIntent}.
4559 */
4560 public static final int SIZE_LARGE = 4;
4561
Griff Hazend5f11f92014-05-27 15:40:09 -07004562 /**
4563 * Size value for use with {@link #setCustomSizePreset} to show this notification
4564 * full screen.
4565 * <p>This value is only applicable for custom display notifications created using
4566 * {@link #setDisplayIntent}.
4567 */
4568 public static final int SIZE_FULL_SCREEN = 5;
4569
Griff Hazen5f2edfc2014-09-29 16:28:44 -07004570 /**
4571 * Sentinel value for use with {@link #setHintScreenTimeout} to keep the screen on for a
4572 * short amount of time when this notification is displayed on the screen. This
4573 * is the default value.
4574 */
4575 public static final int SCREEN_TIMEOUT_SHORT = 0;
4576
4577 /**
4578 * Sentinel value for use with {@link #setHintScreenTimeout} to keep the screen on
4579 * for a longer amount of time when this notification is displayed on the screen.
4580 */
4581 public static final int SCREEN_TIMEOUT_LONG = -1;
4582
Griff Hazen61a9e862014-05-22 16:05:19 -07004583 /** Notification extra which contains wearable extensions */
4584 private static final String EXTRA_WEARABLE_EXTENSIONS = "android.wearable.EXTENSIONS";
4585
Pete Gastaf6781d2014-10-07 15:17:05 -04004586 // Keys within EXTRA_WEARABLE_EXTENSIONS for wearable options.
Griff Hazen61a9e862014-05-22 16:05:19 -07004587 private static final String KEY_ACTIONS = "actions";
4588 private static final String KEY_FLAGS = "flags";
4589 private static final String KEY_DISPLAY_INTENT = "displayIntent";
4590 private static final String KEY_PAGES = "pages";
4591 private static final String KEY_BACKGROUND = "background";
4592 private static final String KEY_CONTENT_ICON = "contentIcon";
4593 private static final String KEY_CONTENT_ICON_GRAVITY = "contentIconGravity";
4594 private static final String KEY_CONTENT_ACTION_INDEX = "contentActionIndex";
4595 private static final String KEY_CUSTOM_SIZE_PRESET = "customSizePreset";
4596 private static final String KEY_CUSTOM_CONTENT_HEIGHT = "customContentHeight";
4597 private static final String KEY_GRAVITY = "gravity";
Griff Hazen5f2edfc2014-09-29 16:28:44 -07004598 private static final String KEY_HINT_SCREEN_TIMEOUT = "hintScreenTimeout";
Griff Hazen61a9e862014-05-22 16:05:19 -07004599
4600 // Flags bitwise-ored to mFlags
4601 private static final int FLAG_CONTENT_INTENT_AVAILABLE_OFFLINE = 0x1;
4602 private static final int FLAG_HINT_HIDE_ICON = 1 << 1;
4603 private static final int FLAG_HINT_SHOW_BACKGROUND_ONLY = 1 << 2;
4604 private static final int FLAG_START_SCROLL_BOTTOM = 1 << 3;
Griff Hazen5f2edfc2014-09-29 16:28:44 -07004605 private static final int FLAG_HINT_AVOID_BACKGROUND_CLIPPING = 1 << 4;
Griff Hazen61a9e862014-05-22 16:05:19 -07004606
4607 // Default value for flags integer
4608 private static final int DEFAULT_FLAGS = FLAG_CONTENT_INTENT_AVAILABLE_OFFLINE;
4609
4610 private static final int DEFAULT_CONTENT_ICON_GRAVITY = Gravity.END;
4611 private static final int DEFAULT_GRAVITY = Gravity.BOTTOM;
4612
4613 private ArrayList<Action> mActions = new ArrayList<Action>();
4614 private int mFlags = DEFAULT_FLAGS;
4615 private PendingIntent mDisplayIntent;
4616 private ArrayList<Notification> mPages = new ArrayList<Notification>();
4617 private Bitmap mBackground;
4618 private int mContentIcon;
4619 private int mContentIconGravity = DEFAULT_CONTENT_ICON_GRAVITY;
4620 private int mContentActionIndex = UNSET_ACTION_INDEX;
4621 private int mCustomSizePreset = SIZE_DEFAULT;
4622 private int mCustomContentHeight;
4623 private int mGravity = DEFAULT_GRAVITY;
Griff Hazen5f2edfc2014-09-29 16:28:44 -07004624 private int mHintScreenTimeout;
Griff Hazen61a9e862014-05-22 16:05:19 -07004625
4626 /**
4627 * Create a {@link android.app.Notification.WearableExtender} with default
4628 * options.
4629 */
4630 public WearableExtender() {
4631 }
4632
4633 public WearableExtender(Notification notif) {
4634 Bundle wearableBundle = notif.extras.getBundle(EXTRA_WEARABLE_EXTENSIONS);
4635 if (wearableBundle != null) {
4636 List<Action> actions = wearableBundle.getParcelableArrayList(KEY_ACTIONS);
4637 if (actions != null) {
4638 mActions.addAll(actions);
4639 }
4640
4641 mFlags = wearableBundle.getInt(KEY_FLAGS, DEFAULT_FLAGS);
4642 mDisplayIntent = wearableBundle.getParcelable(KEY_DISPLAY_INTENT);
4643
4644 Notification[] pages = getNotificationArrayFromBundle(
4645 wearableBundle, KEY_PAGES);
4646 if (pages != null) {
4647 Collections.addAll(mPages, pages);
4648 }
4649
4650 mBackground = wearableBundle.getParcelable(KEY_BACKGROUND);
4651 mContentIcon = wearableBundle.getInt(KEY_CONTENT_ICON);
4652 mContentIconGravity = wearableBundle.getInt(KEY_CONTENT_ICON_GRAVITY,
4653 DEFAULT_CONTENT_ICON_GRAVITY);
4654 mContentActionIndex = wearableBundle.getInt(KEY_CONTENT_ACTION_INDEX,
4655 UNSET_ACTION_INDEX);
4656 mCustomSizePreset = wearableBundle.getInt(KEY_CUSTOM_SIZE_PRESET,
4657 SIZE_DEFAULT);
4658 mCustomContentHeight = wearableBundle.getInt(KEY_CUSTOM_CONTENT_HEIGHT);
4659 mGravity = wearableBundle.getInt(KEY_GRAVITY, DEFAULT_GRAVITY);
Griff Hazen5f2edfc2014-09-29 16:28:44 -07004660 mHintScreenTimeout = wearableBundle.getInt(KEY_HINT_SCREEN_TIMEOUT);
Griff Hazen61a9e862014-05-22 16:05:19 -07004661 }
4662 }
4663
4664 /**
4665 * Apply wearable extensions to a notification that is being built. This is typically
4666 * called by the {@link android.app.Notification.Builder#extend} method of
4667 * {@link android.app.Notification.Builder}.
4668 */
4669 @Override
4670 public Notification.Builder extend(Notification.Builder builder) {
4671 Bundle wearableBundle = new Bundle();
4672
4673 if (!mActions.isEmpty()) {
4674 wearableBundle.putParcelableArrayList(KEY_ACTIONS, mActions);
4675 }
4676 if (mFlags != DEFAULT_FLAGS) {
4677 wearableBundle.putInt(KEY_FLAGS, mFlags);
4678 }
4679 if (mDisplayIntent != null) {
4680 wearableBundle.putParcelable(KEY_DISPLAY_INTENT, mDisplayIntent);
4681 }
4682 if (!mPages.isEmpty()) {
4683 wearableBundle.putParcelableArray(KEY_PAGES, mPages.toArray(
4684 new Notification[mPages.size()]));
4685 }
4686 if (mBackground != null) {
4687 wearableBundle.putParcelable(KEY_BACKGROUND, mBackground);
4688 }
4689 if (mContentIcon != 0) {
4690 wearableBundle.putInt(KEY_CONTENT_ICON, mContentIcon);
4691 }
4692 if (mContentIconGravity != DEFAULT_CONTENT_ICON_GRAVITY) {
4693 wearableBundle.putInt(KEY_CONTENT_ICON_GRAVITY, mContentIconGravity);
4694 }
4695 if (mContentActionIndex != UNSET_ACTION_INDEX) {
4696 wearableBundle.putInt(KEY_CONTENT_ACTION_INDEX,
4697 mContentActionIndex);
4698 }
4699 if (mCustomSizePreset != SIZE_DEFAULT) {
4700 wearableBundle.putInt(KEY_CUSTOM_SIZE_PRESET, mCustomSizePreset);
4701 }
4702 if (mCustomContentHeight != 0) {
4703 wearableBundle.putInt(KEY_CUSTOM_CONTENT_HEIGHT, mCustomContentHeight);
4704 }
4705 if (mGravity != DEFAULT_GRAVITY) {
4706 wearableBundle.putInt(KEY_GRAVITY, mGravity);
4707 }
Griff Hazen5f2edfc2014-09-29 16:28:44 -07004708 if (mHintScreenTimeout != 0) {
4709 wearableBundle.putInt(KEY_HINT_SCREEN_TIMEOUT, mHintScreenTimeout);
4710 }
Griff Hazen61a9e862014-05-22 16:05:19 -07004711
4712 builder.getExtras().putBundle(EXTRA_WEARABLE_EXTENSIONS, wearableBundle);
4713 return builder;
4714 }
4715
4716 @Override
4717 public WearableExtender clone() {
4718 WearableExtender that = new WearableExtender();
4719 that.mActions = new ArrayList<Action>(this.mActions);
4720 that.mFlags = this.mFlags;
4721 that.mDisplayIntent = this.mDisplayIntent;
4722 that.mPages = new ArrayList<Notification>(this.mPages);
4723 that.mBackground = this.mBackground;
4724 that.mContentIcon = this.mContentIcon;
4725 that.mContentIconGravity = this.mContentIconGravity;
4726 that.mContentActionIndex = this.mContentActionIndex;
4727 that.mCustomSizePreset = this.mCustomSizePreset;
4728 that.mCustomContentHeight = this.mCustomContentHeight;
4729 that.mGravity = this.mGravity;
Griff Hazen5f2edfc2014-09-29 16:28:44 -07004730 that.mHintScreenTimeout = this.mHintScreenTimeout;
Griff Hazen61a9e862014-05-22 16:05:19 -07004731 return that;
4732 }
4733
4734 /**
4735 * Add a wearable action to this notification.
4736 *
4737 * <p>When wearable actions are added using this method, the set of actions that
4738 * show on a wearable device splits from devices that only show actions added
4739 * using {@link android.app.Notification.Builder#addAction}. This allows for customization
4740 * of which actions display on different devices.
4741 *
4742 * @param action the action to add to this notification
4743 * @return this object for method chaining
4744 * @see android.app.Notification.Action
4745 */
4746 public WearableExtender addAction(Action action) {
4747 mActions.add(action);
4748 return this;
4749 }
4750
4751 /**
4752 * Adds wearable actions to this notification.
4753 *
4754 * <p>When wearable actions are added using this method, the set of actions that
4755 * show on a wearable device splits from devices that only show actions added
4756 * using {@link android.app.Notification.Builder#addAction}. This allows for customization
4757 * of which actions display on different devices.
4758 *
4759 * @param actions the actions to add to this notification
4760 * @return this object for method chaining
4761 * @see android.app.Notification.Action
4762 */
4763 public WearableExtender addActions(List<Action> actions) {
4764 mActions.addAll(actions);
4765 return this;
4766 }
4767
4768 /**
4769 * Clear all wearable actions present on this builder.
4770 * @return this object for method chaining.
4771 * @see #addAction
4772 */
4773 public WearableExtender clearActions() {
4774 mActions.clear();
4775 return this;
4776 }
4777
4778 /**
4779 * Get the wearable actions present on this notification.
4780 */
4781 public List<Action> getActions() {
4782 return mActions;
4783 }
4784
4785 /**
4786 * Set an intent to launch inside of an activity view when displaying
Griff Hazen14f57992014-05-26 09:07:14 -07004787 * this notification. The {@link PendingIntent} provided should be for an activity.
4788 *
4789 * <pre class="prettyprint">
4790 * Intent displayIntent = new Intent(context, MyDisplayActivity.class);
4791 * PendingIntent displayPendingIntent = PendingIntent.getActivity(context,
4792 * 0, displayIntent, PendingIntent.FLAG_UPDATE_CURRENT);
4793 * Notification notif = new Notification.Builder(context)
4794 * .extend(new Notification.WearableExtender()
4795 * .setDisplayIntent(displayPendingIntent)
4796 * .setCustomSizePreset(Notification.WearableExtender.SIZE_MEDIUM))
4797 * .build();</pre>
4798 *
4799 * <p>The activity to launch needs to allow embedding, must be exported, and
Griff Hazen831ca9d2014-06-17 00:38:38 -07004800 * should have an empty task affinity. It is also recommended to use the device
4801 * default light theme.
Griff Hazen14f57992014-05-26 09:07:14 -07004802 *
4803 * <p>Example AndroidManifest.xml entry:
4804 * <pre class="prettyprint">
4805 * &lt;activity android:name=&quot;com.example.MyDisplayActivity&quot;
4806 * android:exported=&quot;true&quot;
4807 * android:allowEmbedded=&quot;true&quot;
Griff Hazen831ca9d2014-06-17 00:38:38 -07004808 * android:taskAffinity=&quot;&quot;
4809 * android:theme=&quot;@android:style/Theme.DeviceDefault.Light&quot; /&gt;</pre>
Griff Hazen61a9e862014-05-22 16:05:19 -07004810 *
4811 * @param intent the {@link PendingIntent} for an activity
4812 * @return this object for method chaining
4813 * @see android.app.Notification.WearableExtender#getDisplayIntent
4814 */
4815 public WearableExtender setDisplayIntent(PendingIntent intent) {
4816 mDisplayIntent = intent;
4817 return this;
4818 }
4819
4820 /**
4821 * Get the intent to launch inside of an activity view when displaying this
4822 * notification. This {@code PendingIntent} should be for an activity.
4823 */
4824 public PendingIntent getDisplayIntent() {
4825 return mDisplayIntent;
4826 }
4827
4828 /**
4829 * Add an additional page of content to display with this notification. The current
4830 * notification forms the first page, and pages added using this function form
4831 * subsequent pages. This field can be used to separate a notification into multiple
4832 * sections.
4833 *
4834 * @param page the notification to add as another page
4835 * @return this object for method chaining
4836 * @see android.app.Notification.WearableExtender#getPages
4837 */
4838 public WearableExtender addPage(Notification page) {
4839 mPages.add(page);
4840 return this;
4841 }
4842
4843 /**
4844 * Add additional pages of content to display with this notification. The current
4845 * notification forms the first page, and pages added using this function form
4846 * subsequent pages. This field can be used to separate a notification into multiple
4847 * sections.
4848 *
4849 * @param pages a list of notifications
4850 * @return this object for method chaining
4851 * @see android.app.Notification.WearableExtender#getPages
4852 */
4853 public WearableExtender addPages(List<Notification> pages) {
4854 mPages.addAll(pages);
4855 return this;
4856 }
4857
4858 /**
4859 * Clear all additional pages present on this builder.
4860 * @return this object for method chaining.
4861 * @see #addPage
4862 */
4863 public WearableExtender clearPages() {
4864 mPages.clear();
4865 return this;
4866 }
4867
4868 /**
4869 * Get the array of additional pages of content for displaying this notification. The
4870 * current notification forms the first page, and elements within this array form
4871 * subsequent pages. This field can be used to separate a notification into multiple
4872 * sections.
4873 * @return the pages for this notification
4874 */
4875 public List<Notification> getPages() {
4876 return mPages;
4877 }
4878
4879 /**
4880 * Set a background image to be displayed behind the notification content.
4881 * Contrary to the {@link android.app.Notification.BigPictureStyle}, this background
4882 * will work with any notification style.
4883 *
4884 * @param background the background bitmap
4885 * @return this object for method chaining
4886 * @see android.app.Notification.WearableExtender#getBackground
4887 */
4888 public WearableExtender setBackground(Bitmap background) {
4889 mBackground = background;
4890 return this;
4891 }
4892
4893 /**
4894 * Get a background image to be displayed behind the notification content.
4895 * Contrary to the {@link android.app.Notification.BigPictureStyle}, this background
4896 * will work with any notification style.
4897 *
4898 * @return the background image
4899 * @see android.app.Notification.WearableExtender#setBackground
4900 */
4901 public Bitmap getBackground() {
4902 return mBackground;
4903 }
4904
4905 /**
4906 * Set an icon that goes with the content of this notification.
4907 */
4908 public WearableExtender setContentIcon(int icon) {
4909 mContentIcon = icon;
4910 return this;
4911 }
4912
4913 /**
4914 * Get an icon that goes with the content of this notification.
4915 */
4916 public int getContentIcon() {
4917 return mContentIcon;
4918 }
4919
4920 /**
4921 * Set the gravity that the content icon should have within the notification display.
4922 * Supported values include {@link android.view.Gravity#START} and
4923 * {@link android.view.Gravity#END}. The default value is {@link android.view.Gravity#END}.
4924 * @see #setContentIcon
4925 */
4926 public WearableExtender setContentIconGravity(int contentIconGravity) {
4927 mContentIconGravity = contentIconGravity;
4928 return this;
4929 }
4930
4931 /**
4932 * Get the gravity that the content icon should have within the notification display.
4933 * Supported values include {@link android.view.Gravity#START} and
4934 * {@link android.view.Gravity#END}. The default value is {@link android.view.Gravity#END}.
4935 * @see #getContentIcon
4936 */
4937 public int getContentIconGravity() {
4938 return mContentIconGravity;
4939 }
4940
4941 /**
4942 * Set an action from this notification's actions to be clickable with the content of
Griff Hazen14f57992014-05-26 09:07:14 -07004943 * this notification. This action will no longer display separately from the
4944 * notification's content.
4945 *
Griff Hazenca48d352014-05-28 22:37:13 -07004946 * <p>For notifications with multiple pages, child pages can also have content actions
Griff Hazen14f57992014-05-26 09:07:14 -07004947 * set, although the list of available actions comes from the main notification and not
4948 * from the child page's notification.
4949 *
4950 * @param actionIndex The index of the action to hoist onto the current notification page.
4951 * If wearable actions were added to the main notification, this index
4952 * will apply to that list, otherwise it will apply to the regular
4953 * actions list.
Griff Hazen61a9e862014-05-22 16:05:19 -07004954 */
4955 public WearableExtender setContentAction(int actionIndex) {
4956 mContentActionIndex = actionIndex;
4957 return this;
4958 }
4959
4960 /**
Griff Hazenca48d352014-05-28 22:37:13 -07004961 * Get the index of the notification action, if any, that was specified as being clickable
4962 * with the content of this notification. This action will no longer display separately
Griff Hazen14f57992014-05-26 09:07:14 -07004963 * from the notification's content.
Griff Hazen61a9e862014-05-22 16:05:19 -07004964 *
Griff Hazenca48d352014-05-28 22:37:13 -07004965 * <p>For notifications with multiple pages, child pages can also have content actions
Griff Hazen14f57992014-05-26 09:07:14 -07004966 * set, although the list of available actions comes from the main notification and not
4967 * from the child page's notification.
4968 *
4969 * <p>If wearable specific actions were added to the main notification, this index will
4970 * apply to that list, otherwise it will apply to the regular actions list.
Griff Hazenca48d352014-05-28 22:37:13 -07004971 *
4972 * @return the action index or {@link #UNSET_ACTION_INDEX} if no action was selected.
Griff Hazen61a9e862014-05-22 16:05:19 -07004973 */
4974 public int getContentAction() {
4975 return mContentActionIndex;
4976 }
4977
4978 /**
4979 * Set the gravity that this notification should have within the available viewport space.
4980 * Supported values include {@link android.view.Gravity#TOP},
4981 * {@link android.view.Gravity#CENTER_VERTICAL} and {@link android.view.Gravity#BOTTOM}.
4982 * The default value is {@link android.view.Gravity#BOTTOM}.
4983 */
4984 public WearableExtender setGravity(int gravity) {
4985 mGravity = gravity;
4986 return this;
4987 }
4988
4989 /**
4990 * Get the gravity that this notification should have within the available viewport space.
4991 * Supported values include {@link android.view.Gravity#TOP},
4992 * {@link android.view.Gravity#CENTER_VERTICAL} and {@link android.view.Gravity#BOTTOM}.
4993 * The default value is {@link android.view.Gravity#BOTTOM}.
4994 */
4995 public int getGravity() {
4996 return mGravity;
4997 }
4998
4999 /**
5000 * Set the custom size preset for the display of this notification out of the available
5001 * presets found in {@link android.app.Notification.WearableExtender}, e.g.
5002 * {@link #SIZE_LARGE}.
5003 * <p>Some custom size presets are only applicable for custom display notifications created
5004 * using {@link android.app.Notification.WearableExtender#setDisplayIntent}. Check the
5005 * documentation for the preset in question. See also
5006 * {@link #setCustomContentHeight} and {@link #getCustomSizePreset}.
5007 */
5008 public WearableExtender setCustomSizePreset(int sizePreset) {
5009 mCustomSizePreset = sizePreset;
5010 return this;
5011 }
5012
5013 /**
5014 * Get the custom size preset for the display of this notification out of the available
5015 * presets found in {@link android.app.Notification.WearableExtender}, e.g.
5016 * {@link #SIZE_LARGE}.
5017 * <p>Some custom size presets are only applicable for custom display notifications created
5018 * using {@link #setDisplayIntent}. Check the documentation for the preset in question.
5019 * See also {@link #setCustomContentHeight} and {@link #setCustomSizePreset}.
5020 */
5021 public int getCustomSizePreset() {
5022 return mCustomSizePreset;
5023 }
5024
5025 /**
5026 * Set the custom height in pixels for the display of this notification's content.
5027 * <p>This option is only available for custom display notifications created
5028 * using {@link android.app.Notification.WearableExtender#setDisplayIntent}. See also
5029 * {@link android.app.Notification.WearableExtender#setCustomSizePreset} and
5030 * {@link #getCustomContentHeight}.
5031 */
5032 public WearableExtender setCustomContentHeight(int height) {
5033 mCustomContentHeight = height;
5034 return this;
5035 }
5036
5037 /**
5038 * Get the custom height in pixels for the display of this notification's content.
5039 * <p>This option is only available for custom display notifications created
5040 * using {@link #setDisplayIntent}. See also {@link #setCustomSizePreset} and
5041 * {@link #setCustomContentHeight}.
5042 */
5043 public int getCustomContentHeight() {
5044 return mCustomContentHeight;
5045 }
5046
5047 /**
5048 * Set whether the scrolling position for the contents of this notification should start
5049 * at the bottom of the contents instead of the top when the contents are too long to
5050 * display within the screen. Default is false (start scroll at the top).
5051 */
5052 public WearableExtender setStartScrollBottom(boolean startScrollBottom) {
5053 setFlag(FLAG_START_SCROLL_BOTTOM, startScrollBottom);
5054 return this;
5055 }
5056
5057 /**
5058 * Get whether the scrolling position for the contents of this notification should start
5059 * at the bottom of the contents instead of the top when the contents are too long to
5060 * display within the screen. Default is false (start scroll at the top).
5061 */
5062 public boolean getStartScrollBottom() {
5063 return (mFlags & FLAG_START_SCROLL_BOTTOM) != 0;
5064 }
5065
5066 /**
5067 * Set whether the content intent is available when the wearable device is not connected
5068 * to a companion device. The user can still trigger this intent when the wearable device
5069 * is offline, but a visual hint will indicate that the content intent may not be available.
5070 * Defaults to true.
5071 */
5072 public WearableExtender setContentIntentAvailableOffline(
5073 boolean contentIntentAvailableOffline) {
5074 setFlag(FLAG_CONTENT_INTENT_AVAILABLE_OFFLINE, contentIntentAvailableOffline);
5075 return this;
5076 }
5077
5078 /**
5079 * Get whether the content intent is available when the wearable device is not connected
5080 * to a companion device. The user can still trigger this intent when the wearable device
5081 * is offline, but a visual hint will indicate that the content intent may not be available.
5082 * Defaults to true.
5083 */
5084 public boolean getContentIntentAvailableOffline() {
5085 return (mFlags & FLAG_CONTENT_INTENT_AVAILABLE_OFFLINE) != 0;
5086 }
5087
5088 /**
5089 * Set a hint that this notification's icon should not be displayed.
5090 * @param hintHideIcon {@code true} to hide the icon, {@code false} otherwise.
5091 * @return this object for method chaining
5092 */
5093 public WearableExtender setHintHideIcon(boolean hintHideIcon) {
5094 setFlag(FLAG_HINT_HIDE_ICON, hintHideIcon);
5095 return this;
5096 }
5097
5098 /**
5099 * Get a hint that this notification's icon should not be displayed.
5100 * @return {@code true} if this icon should not be displayed, false otherwise.
5101 * The default value is {@code false} if this was never set.
5102 */
5103 public boolean getHintHideIcon() {
5104 return (mFlags & FLAG_HINT_HIDE_ICON) != 0;
5105 }
5106
5107 /**
5108 * Set a visual hint that only the background image of this notification should be
5109 * displayed, and other semantic content should be hidden. This hint is only applicable
5110 * to sub-pages added using {@link #addPage}.
5111 */
5112 public WearableExtender setHintShowBackgroundOnly(boolean hintShowBackgroundOnly) {
5113 setFlag(FLAG_HINT_SHOW_BACKGROUND_ONLY, hintShowBackgroundOnly);
5114 return this;
5115 }
5116
5117 /**
5118 * Get a visual hint that only the background image of this notification should be
5119 * displayed, and other semantic content should be hidden. This hint is only applicable
5120 * to sub-pages added using {@link android.app.Notification.WearableExtender#addPage}.
5121 */
5122 public boolean getHintShowBackgroundOnly() {
5123 return (mFlags & FLAG_HINT_SHOW_BACKGROUND_ONLY) != 0;
5124 }
5125
Griff Hazen5f2edfc2014-09-29 16:28:44 -07005126 /**
Griff Hazen9c5be4e2014-11-17 17:47:50 -08005127 * Set a hint that this notification's background should not be clipped if possible,
5128 * and should instead be resized to fully display on the screen, retaining the aspect
5129 * ratio of the image. This can be useful for images like barcodes or qr codes.
Griff Hazen5f2edfc2014-09-29 16:28:44 -07005130 * @param hintAvoidBackgroundClipping {@code true} to avoid clipping if possible.
5131 * @return this object for method chaining
5132 */
5133 public WearableExtender setHintAvoidBackgroundClipping(
5134 boolean hintAvoidBackgroundClipping) {
5135 setFlag(FLAG_HINT_AVOID_BACKGROUND_CLIPPING, hintAvoidBackgroundClipping);
5136 return this;
5137 }
5138
5139 /**
Griff Hazen9c5be4e2014-11-17 17:47:50 -08005140 * Get a hint that this notification's background should not be clipped if possible,
5141 * and should instead be resized to fully display on the screen, retaining the aspect
5142 * ratio of the image. This can be useful for images like barcodes or qr codes.
Griff Hazen5f2edfc2014-09-29 16:28:44 -07005143 * @return {@code true} if it's ok if the background is clipped on the screen, false
5144 * otherwise. The default value is {@code false} if this was never set.
5145 */
5146 public boolean getHintAvoidBackgroundClipping() {
5147 return (mFlags & FLAG_HINT_AVOID_BACKGROUND_CLIPPING) != 0;
5148 }
5149
5150 /**
5151 * Set a hint that the screen should remain on for at least this duration when
5152 * this notification is displayed on the screen.
5153 * @param timeout The requested screen timeout in milliseconds. Can also be either
5154 * {@link #SCREEN_TIMEOUT_SHORT} or {@link #SCREEN_TIMEOUT_LONG}.
5155 * @return this object for method chaining
5156 */
5157 public WearableExtender setHintScreenTimeout(int timeout) {
5158 mHintScreenTimeout = timeout;
5159 return this;
5160 }
5161
5162 /**
5163 * Get the duration, in milliseconds, that the screen should remain on for
5164 * when this notification is displayed.
5165 * @return the duration in milliseconds if > 0, or either one of the sentinel values
5166 * {@link #SCREEN_TIMEOUT_SHORT} or {@link #SCREEN_TIMEOUT_LONG}.
5167 */
5168 public int getHintScreenTimeout() {
5169 return mHintScreenTimeout;
5170 }
5171
Griff Hazen61a9e862014-05-22 16:05:19 -07005172 private void setFlag(int mask, boolean value) {
5173 if (value) {
5174 mFlags |= mask;
5175 } else {
5176 mFlags &= ~mask;
5177 }
5178 }
5179 }
5180
5181 /**
Zhen Yu Song9d5528b2014-11-14 15:34:39 -08005182 * <p>Helper class to add Android Auto extensions to notifications. To create a notification
5183 * with car extensions:
5184 *
5185 * <ol>
5186 * <li>Create an {@link Notification.Builder}, setting any desired
5187 * properties.
5188 * <li>Create a {@link CarExtender}.
5189 * <li>Set car-specific properties using the {@code add} and {@code set} methods of
5190 * {@link CarExtender}.
5191 * <li>Call {@link Notification.Builder#extend(Notification.Extender)}
5192 * to apply the extensions to a notification.
5193 * </ol>
5194 *
5195 * <pre class="prettyprint">
5196 * Notification notification = new Notification.Builder(context)
5197 * ...
5198 * .extend(new CarExtender()
5199 * .set*(...))
5200 * .build();
5201 * </pre>
5202 *
5203 * <p>Car extensions can be accessed on an existing notification by using the
5204 * {@code CarExtender(Notification)} constructor, and then using the {@code get} methods
5205 * to access values.
5206 */
5207 public static final class CarExtender implements Extender {
5208 private static final String TAG = "CarExtender";
5209
5210 private static final String EXTRA_CAR_EXTENDER = "android.car.EXTENSIONS";
5211 private static final String EXTRA_LARGE_ICON = "large_icon";
5212 private static final String EXTRA_CONVERSATION = "car_conversation";
5213 private static final String EXTRA_COLOR = "app_color";
5214
5215 private Bitmap mLargeIcon;
5216 private UnreadConversation mUnreadConversation;
5217 private int mColor = Notification.COLOR_DEFAULT;
5218
5219 /**
5220 * Create a {@link CarExtender} with default options.
5221 */
5222 public CarExtender() {
5223 }
5224
5225 /**
5226 * Create a {@link CarExtender} from the CarExtender options of an existing Notification.
5227 *
5228 * @param notif The notification from which to copy options.
5229 */
5230 public CarExtender(Notification notif) {
5231 Bundle carBundle = notif.extras == null ?
5232 null : notif.extras.getBundle(EXTRA_CAR_EXTENDER);
5233 if (carBundle != null) {
5234 mLargeIcon = carBundle.getParcelable(EXTRA_LARGE_ICON);
5235 mColor = carBundle.getInt(EXTRA_COLOR, Notification.COLOR_DEFAULT);
5236
5237 Bundle b = carBundle.getBundle(EXTRA_CONVERSATION);
5238 mUnreadConversation = UnreadConversation.getUnreadConversationFromBundle(b);
5239 }
5240 }
5241
5242 /**
5243 * Apply car extensions to a notification that is being built. This is typically called by
5244 * the {@link Notification.Builder#extend(Notification.Extender)}
5245 * method of {@link Notification.Builder}.
5246 */
5247 @Override
5248 public Notification.Builder extend(Notification.Builder builder) {
5249 Bundle carExtensions = new Bundle();
5250
5251 if (mLargeIcon != null) {
5252 carExtensions.putParcelable(EXTRA_LARGE_ICON, mLargeIcon);
5253 }
5254 if (mColor != Notification.COLOR_DEFAULT) {
5255 carExtensions.putInt(EXTRA_COLOR, mColor);
5256 }
5257
5258 if (mUnreadConversation != null) {
5259 Bundle b = mUnreadConversation.getBundleForUnreadConversation();
5260 carExtensions.putBundle(EXTRA_CONVERSATION, b);
5261 }
5262
5263 builder.getExtras().putBundle(EXTRA_CAR_EXTENDER, carExtensions);
5264 return builder;
5265 }
5266
5267 /**
5268 * Sets the accent color to use when Android Auto presents the notification.
5269 *
5270 * Android Auto uses the color set with {@link Notification.Builder#setColor(int)}
5271 * to accent the displayed notification. However, not all colors are acceptable in an
5272 * automotive setting. This method can be used to override the color provided in the
5273 * notification in such a situation.
5274 */
Tor Norbye80756e32015-03-02 09:39:27 -08005275 public CarExtender setColor(@ColorInt int color) {
Zhen Yu Song9d5528b2014-11-14 15:34:39 -08005276 mColor = color;
5277 return this;
5278 }
5279
5280 /**
5281 * Gets the accent color.
5282 *
5283 * @see setColor
5284 */
Tor Norbye80756e32015-03-02 09:39:27 -08005285 @ColorInt
Zhen Yu Song9d5528b2014-11-14 15:34:39 -08005286 public int getColor() {
5287 return mColor;
5288 }
5289
5290 /**
5291 * Sets the large icon of the car notification.
5292 *
5293 * If no large icon is set in the extender, Android Auto will display the icon
5294 * specified by {@link Notification.Builder#setLargeIcon(android.graphics.Bitmap)}
5295 *
5296 * @param largeIcon The large icon to use in the car notification.
5297 * @return This object for method chaining.
5298 */
5299 public CarExtender setLargeIcon(Bitmap largeIcon) {
5300 mLargeIcon = largeIcon;
5301 return this;
5302 }
5303
5304 /**
5305 * Gets the large icon used in this car notification, or null if no icon has been set.
5306 *
5307 * @return The large icon for the car notification.
5308 * @see CarExtender#setLargeIcon
5309 */
5310 public Bitmap getLargeIcon() {
5311 return mLargeIcon;
5312 }
5313
5314 /**
5315 * Sets the unread conversation in a message notification.
5316 *
5317 * @param unreadConversation The unread part of the conversation this notification conveys.
5318 * @return This object for method chaining.
5319 */
5320 public CarExtender setUnreadConversation(UnreadConversation unreadConversation) {
5321 mUnreadConversation = unreadConversation;
5322 return this;
5323 }
5324
5325 /**
5326 * Returns the unread conversation conveyed by this notification.
5327 * @see #setUnreadConversation(UnreadConversation)
5328 */
5329 public UnreadConversation getUnreadConversation() {
5330 return mUnreadConversation;
5331 }
5332
5333 /**
5334 * A class which holds the unread messages from a conversation.
5335 */
5336 public static class UnreadConversation {
5337 private static final String KEY_AUTHOR = "author";
5338 private static final String KEY_TEXT = "text";
5339 private static final String KEY_MESSAGES = "messages";
5340 private static final String KEY_REMOTE_INPUT = "remote_input";
5341 private static final String KEY_ON_REPLY = "on_reply";
5342 private static final String KEY_ON_READ = "on_read";
5343 private static final String KEY_PARTICIPANTS = "participants";
5344 private static final String KEY_TIMESTAMP = "timestamp";
5345
5346 private final String[] mMessages;
5347 private final RemoteInput mRemoteInput;
5348 private final PendingIntent mReplyPendingIntent;
5349 private final PendingIntent mReadPendingIntent;
5350 private final String[] mParticipants;
5351 private final long mLatestTimestamp;
5352
5353 UnreadConversation(String[] messages, RemoteInput remoteInput,
5354 PendingIntent replyPendingIntent, PendingIntent readPendingIntent,
5355 String[] participants, long latestTimestamp) {
5356 mMessages = messages;
5357 mRemoteInput = remoteInput;
5358 mReadPendingIntent = readPendingIntent;
5359 mReplyPendingIntent = replyPendingIntent;
5360 mParticipants = participants;
5361 mLatestTimestamp = latestTimestamp;
5362 }
5363
5364 /**
5365 * Gets the list of messages conveyed by this notification.
5366 */
5367 public String[] getMessages() {
5368 return mMessages;
5369 }
5370
5371 /**
5372 * Gets the remote input that will be used to convey the response to a message list, or
5373 * null if no such remote input exists.
5374 */
5375 public RemoteInput getRemoteInput() {
5376 return mRemoteInput;
5377 }
5378
5379 /**
5380 * Gets the pending intent that will be triggered when the user replies to this
5381 * notification.
5382 */
5383 public PendingIntent getReplyPendingIntent() {
5384 return mReplyPendingIntent;
5385 }
5386
5387 /**
5388 * Gets the pending intent that Android Auto will send after it reads aloud all messages
5389 * in this object's message list.
5390 */
5391 public PendingIntent getReadPendingIntent() {
5392 return mReadPendingIntent;
5393 }
5394
5395 /**
5396 * Gets the participants in the conversation.
5397 */
5398 public String[] getParticipants() {
5399 return mParticipants;
5400 }
5401
5402 /**
5403 * Gets the firs participant in the conversation.
5404 */
5405 public String getParticipant() {
5406 return mParticipants.length > 0 ? mParticipants[0] : null;
5407 }
5408
5409 /**
5410 * Gets the timestamp of the conversation.
5411 */
5412 public long getLatestTimestamp() {
5413 return mLatestTimestamp;
5414 }
5415
5416 Bundle getBundleForUnreadConversation() {
5417 Bundle b = new Bundle();
5418 String author = null;
5419 if (mParticipants != null && mParticipants.length > 1) {
5420 author = mParticipants[0];
5421 }
5422 Parcelable[] messages = new Parcelable[mMessages.length];
5423 for (int i = 0; i < messages.length; i++) {
5424 Bundle m = new Bundle();
5425 m.putString(KEY_TEXT, mMessages[i]);
5426 m.putString(KEY_AUTHOR, author);
5427 messages[i] = m;
5428 }
5429 b.putParcelableArray(KEY_MESSAGES, messages);
5430 if (mRemoteInput != null) {
5431 b.putParcelable(KEY_REMOTE_INPUT, mRemoteInput);
5432 }
5433 b.putParcelable(KEY_ON_REPLY, mReplyPendingIntent);
5434 b.putParcelable(KEY_ON_READ, mReadPendingIntent);
5435 b.putStringArray(KEY_PARTICIPANTS, mParticipants);
5436 b.putLong(KEY_TIMESTAMP, mLatestTimestamp);
5437 return b;
5438 }
5439
5440 static UnreadConversation getUnreadConversationFromBundle(Bundle b) {
5441 if (b == null) {
5442 return null;
5443 }
5444 Parcelable[] parcelableMessages = b.getParcelableArray(KEY_MESSAGES);
5445 String[] messages = null;
5446 if (parcelableMessages != null) {
5447 String[] tmp = new String[parcelableMessages.length];
5448 boolean success = true;
5449 for (int i = 0; i < tmp.length; i++) {
5450 if (!(parcelableMessages[i] instanceof Bundle)) {
5451 success = false;
5452 break;
5453 }
5454 tmp[i] = ((Bundle) parcelableMessages[i]).getString(KEY_TEXT);
5455 if (tmp[i] == null) {
5456 success = false;
5457 break;
5458 }
5459 }
5460 if (success) {
5461 messages = tmp;
5462 } else {
5463 return null;
5464 }
5465 }
5466
5467 PendingIntent onRead = b.getParcelable(KEY_ON_READ);
5468 PendingIntent onReply = b.getParcelable(KEY_ON_REPLY);
5469
5470 RemoteInput remoteInput = b.getParcelable(KEY_REMOTE_INPUT);
5471
5472 String[] participants = b.getStringArray(KEY_PARTICIPANTS);
5473 if (participants == null || participants.length != 1) {
5474 return null;
5475 }
5476
5477 return new UnreadConversation(messages,
5478 remoteInput,
5479 onReply,
5480 onRead,
5481 participants, b.getLong(KEY_TIMESTAMP));
5482 }
5483 };
5484
5485 /**
5486 * Builder class for {@link CarExtender.UnreadConversation} objects.
5487 */
5488 public static class Builder {
5489 private final List<String> mMessages = new ArrayList<String>();
5490 private final String mParticipant;
5491 private RemoteInput mRemoteInput;
5492 private PendingIntent mReadPendingIntent;
5493 private PendingIntent mReplyPendingIntent;
5494 private long mLatestTimestamp;
5495
5496 /**
5497 * Constructs a new builder for {@link CarExtender.UnreadConversation}.
5498 *
5499 * @param name The name of the other participant in the conversation.
5500 */
5501 public Builder(String name) {
5502 mParticipant = name;
5503 }
5504
5505 /**
5506 * Appends a new unread message to the list of messages for this conversation.
5507 *
5508 * The messages should be added from oldest to newest.
5509 *
5510 * @param message The text of the new unread message.
5511 * @return This object for method chaining.
5512 */
5513 public Builder addMessage(String message) {
5514 mMessages.add(message);
5515 return this;
5516 }
5517
5518 /**
5519 * Sets the pending intent and remote input which will convey the reply to this
5520 * notification.
5521 *
5522 * @param pendingIntent The pending intent which will be triggered on a reply.
5523 * @param remoteInput The remote input parcelable which will carry the reply.
5524 * @return This object for method chaining.
5525 *
5526 * @see CarExtender.UnreadConversation#getRemoteInput
5527 * @see CarExtender.UnreadConversation#getReplyPendingIntent
5528 */
5529 public Builder setReplyAction(
5530 PendingIntent pendingIntent, RemoteInput remoteInput) {
5531 mRemoteInput = remoteInput;
5532 mReplyPendingIntent = pendingIntent;
5533
5534 return this;
5535 }
5536
5537 /**
5538 * Sets the pending intent that will be sent once the messages in this notification
5539 * are read.
5540 *
5541 * @param pendingIntent The pending intent to use.
5542 * @return This object for method chaining.
5543 */
5544 public Builder setReadPendingIntent(PendingIntent pendingIntent) {
5545 mReadPendingIntent = pendingIntent;
5546 return this;
5547 }
5548
5549 /**
5550 * Sets the timestamp of the most recent message in an unread conversation.
5551 *
5552 * If a messaging notification has been posted by your application and has not
5553 * yet been cancelled, posting a later notification with the same id and tag
5554 * but without a newer timestamp may result in Android Auto not displaying a
5555 * heads up notification for the later notification.
5556 *
5557 * @param timestamp The timestamp of the most recent message in the conversation.
5558 * @return This object for method chaining.
5559 */
5560 public Builder setLatestTimestamp(long timestamp) {
5561 mLatestTimestamp = timestamp;
5562 return this;
5563 }
5564
5565 /**
5566 * Builds a new unread conversation object.
5567 *
5568 * @return The new unread conversation object.
5569 */
5570 public UnreadConversation build() {
5571 String[] messages = mMessages.toArray(new String[mMessages.size()]);
5572 String[] participants = { mParticipant };
5573 return new UnreadConversation(messages, mRemoteInput, mReplyPendingIntent,
5574 mReadPendingIntent, participants, mLatestTimestamp);
5575 }
5576 }
5577 }
5578
5579 /**
Griff Hazen61a9e862014-05-22 16:05:19 -07005580 * Get an array of Notification objects from a parcelable array bundle field.
5581 * Update the bundle to have a typed array so fetches in the future don't need
5582 * to do an array copy.
5583 */
5584 private static Notification[] getNotificationArrayFromBundle(Bundle bundle, String key) {
5585 Parcelable[] array = bundle.getParcelableArray(key);
5586 if (array instanceof Notification[] || array == null) {
5587 return (Notification[]) array;
5588 }
5589 Notification[] typedArray = Arrays.copyOf(array, array.length,
5590 Notification[].class);
5591 bundle.putParcelableArray(key, typedArray);
5592 return typedArray;
5593 }
Christoph Studer4600f9b2014-07-22 22:44:43 +02005594
5595 private static class BuilderRemoteViews extends RemoteViews {
5596 public BuilderRemoteViews(Parcel parcel) {
5597 super(parcel);
5598 }
5599
Kenny Guy77320062014-08-27 21:37:15 +01005600 public BuilderRemoteViews(ApplicationInfo appInfo, int layoutId) {
5601 super(appInfo, layoutId);
Christoph Studer4600f9b2014-07-22 22:44:43 +02005602 }
5603
5604 @Override
5605 public BuilderRemoteViews clone() {
5606 Parcel p = Parcel.obtain();
5607 writeToParcel(p, 0);
5608 p.setDataPosition(0);
5609 BuilderRemoteViews brv = new BuilderRemoteViews(p);
5610 p.recycle();
5611 return brv;
5612 }
5613 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005614}