blob: f7300aa1561ba1deee241ce1a083d3b4164f2959 [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
2 * Copyright (C) 2007 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package android.app;
18
Tor Norbyed9273d62013-05-30 15:59:53 -070019import android.annotation.IntDef;
Daniel Sandler01df1c62014-06-09 10:54:01 -040020import android.annotation.SdkConstant;
21import android.annotation.SdkConstant.SdkConstantType;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080022import android.content.Context;
23import android.content.Intent;
Christoph Studer4600f9b2014-07-22 22:44:43 +020024import android.content.pm.PackageManager.NameNotFoundException;
Daniel Sandler9f7936a2012-05-21 16:14:28 -040025import android.content.res.Resources;
Joe Onoratoef1e7762010-09-17 18:38:38 -040026import android.graphics.Bitmap;
Kenny Guy8a0101b2014-05-08 23:34:12 +010027import android.graphics.Canvas;
Jorim Jaggi5c2d8462014-03-21 17:37:00 +010028import android.graphics.PorterDuff;
Kenny Guy8a0101b2014-05-08 23:34:12 +010029import android.graphics.drawable.Drawable;
John Spurlockc0650f022014-07-19 13:22:39 -040030import android.media.AudioAttributes;
Jeff Sharkey098d5802012-04-26 17:30:34 -070031import android.media.AudioManager;
Jeff Browndba34ba2014-06-24 20:46:03 -070032import android.media.session.MediaSession;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080033import android.net.Uri;
Daniel Sandlerdcbaf662013-04-26 16:23:09 -040034import android.os.BadParcelableException;
Daniel Sandler2561b0b2012-02-13 21:04:12 -050035import android.os.Bundle;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080036import android.os.Parcel;
37import android.os.Parcelable;
Daniel Sandlera2985ed2012-04-03 16:42:00 -040038import android.os.SystemClock;
Jeff Sharkey6d515712012-09-20 16:06:08 -070039import android.os.UserHandle;
Kenny Guy8a0101b2014-05-08 23:34:12 +010040import android.os.UserManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080041import android.text.TextUtils;
Daniel Sandlerdcbaf662013-04-26 16:23:09 -040042import android.util.Log;
Jorim Jaggi445d3c02014-08-19 22:33:42 +020043import android.util.MathUtils;
Daniel Sandler9f7936a2012-05-21 16:14:28 -040044import android.util.TypedValue;
Griff Hazen61a9e862014-05-22 16:05:19 -070045import android.view.Gravity;
Joe Onorato8595a3d2010-11-19 18:12:07 -080046import android.view.View;
Jeff Sharkey1c400132011-08-05 14:50:13 -070047import android.widget.ProgressBar;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080048import android.widget.RemoteViews;
49
Griff Hazen959591e2014-05-15 22:26:18 -070050import com.android.internal.R;
Griff Hazenc091ba82014-05-16 10:13:26 -070051import com.android.internal.util.NotificationColorUtil;
Griff Hazen959591e2014-05-15 22:26:18 -070052
Tor Norbyed9273d62013-05-30 15:59:53 -070053import java.lang.annotation.Retention;
54import java.lang.annotation.RetentionPolicy;
Christoph Studer4600f9b2014-07-22 22:44:43 +020055import java.lang.reflect.Constructor;
Andy Stadler110988c2010-12-03 14:29:16 -080056import java.text.NumberFormat;
Daniel Sandler2561b0b2012-02-13 21:04:12 -050057import java.util.ArrayList;
Griff Hazen61a9e862014-05-22 16:05:19 -070058import java.util.Arrays;
Griff Hazen5cadc3b2014-05-20 09:55:39 -070059import java.util.Collections;
Griff Hazen61a9e862014-05-22 16:05:19 -070060import java.util.List;
Joe Onorato561d3852010-11-20 18:09:34 -080061
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080062/**
63 * A class that represents how a persistent notification is to be presented to
64 * the user using the {@link android.app.NotificationManager}.
65 *
Joe Onoratocb109a02011-01-18 17:57:41 -080066 * <p>The {@link Notification.Builder Notification.Builder} has been added to make it
67 * easier to construct Notifications.</p>
68 *
Joe Fernandez558459f2011-10-13 16:47:36 -070069 * <div class="special reference">
70 * <h3>Developer Guides</h3>
71 * <p>For a guide to creating notifications, read the
72 * <a href="{@docRoot}guide/topics/ui/notifiers/notifications.html">Status Bar Notifications</a>
73 * developer guide.</p>
74 * </div>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080075 */
76public class Notification implements Parcelable
77{
Daniel Sandlerdcbaf662013-04-26 16:23:09 -040078 private static final String TAG = "Notification";
79
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080080 /**
Daniel Sandler01df1c62014-06-09 10:54:01 -040081 * An activity that provides a user interface for adjusting notification preferences for its
82 * containing application. Optional but recommended for apps that post
83 * {@link android.app.Notification Notifications}.
84 */
85 @SdkConstant(SdkConstantType.INTENT_CATEGORY)
86 public static final String INTENT_CATEGORY_NOTIFICATION_PREFERENCES
87 = "android.intent.category.NOTIFICATION_PREFERENCES";
88
89 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080090 * Use all default values (where applicable).
91 */
92 public static final int DEFAULT_ALL = ~0;
Daniel Sandler2561b0b2012-02-13 21:04:12 -050093
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080094 /**
95 * Use the default notification sound. This will ignore any given
96 * {@link #sound}.
Daniel Sandler2561b0b2012-02-13 21:04:12 -050097 *
Chris Wren47c20a12014-06-18 17:27:29 -040098 * <p>
99 * A notification that is noisy is more likely to be presented as a heads-up notification.
100 * </p>
101 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800102 * @see #defaults
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500103 */
104
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800105 public static final int DEFAULT_SOUND = 1;
106
107 /**
108 * Use the default notification vibrate. This will ignore any given
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500109 * {@link #vibrate}. Using phone vibration requires the
Scott Mainb8b36452009-04-26 15:50:49 -0700110 * {@link android.Manifest.permission#VIBRATE VIBRATE} permission.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500111 *
Chris Wren47c20a12014-06-18 17:27:29 -0400112 * <p>
113 * A notification that vibrates is more likely to be presented as a heads-up notification.
114 * </p>
115 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800116 * @see #defaults
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500117 */
118
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800119 public static final int DEFAULT_VIBRATE = 2;
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500120
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800121 /**
122 * Use the default notification lights. This will ignore the
123 * {@link #FLAG_SHOW_LIGHTS} bit, and {@link #ledARGB}, {@link #ledOffMS}, or
124 * {@link #ledOnMS}.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500125 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800126 * @see #defaults
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500127 */
128
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800129 public static final int DEFAULT_LIGHTS = 4;
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500130
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800131 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500132 * A timestamp related to this notification, in milliseconds since the epoch.
Joe Malin8d40d042012-11-05 11:36:40 -0800133 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500134 * Default value: {@link System#currentTimeMillis() Now}.
135 *
136 * Choose a timestamp that will be most relevant to the user. For most finite events, this
137 * corresponds to the time the event happened (or will happen, in the case of events that have
138 * yet to occur but about which the user is being informed). Indefinite events should be
Joe Malin8d40d042012-11-05 11:36:40 -0800139 * timestamped according to when the activity began.
140 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500141 * Some examples:
Joe Malin8d40d042012-11-05 11:36:40 -0800142 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500143 * <ul>
144 * <li>Notification of a new chat message should be stamped when the message was received.</li>
145 * <li>Notification of an ongoing file download (with a progress bar, for example) should be stamped when the download started.</li>
146 * <li>Notification of a completed file download should be stamped when the download finished.</li>
147 * <li>Notification of an upcoming meeting should be stamped with the time the meeting will begin (that is, in the future).</li>
148 * <li>Notification of an ongoing stopwatch (increasing timer) should be stamped with the watch's start time.
149 * <li>Notification of an ongoing countdown timer should be stamped with the timer's end time.
Joe Malin8d40d042012-11-05 11:36:40 -0800150 * </ul>
151 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800152 */
153 public long when;
154
155 /**
156 * The resource id of a drawable to use as the icon in the status bar.
Daniel Sandlerd952dae2011-02-07 16:33:36 -0500157 * This is required; notifications with an invalid icon resource will not be shown.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800158 */
159 public int icon;
160
161 /**
Joe Onorato46439ce2010-11-19 13:56:21 -0800162 * If the icon in the status bar is to have more than one level, you can set this. Otherwise,
163 * leave it at its default value of 0.
164 *
165 * @see android.widget.ImageView#setImageLevel
Griff Hazen959591e2014-05-15 22:26:18 -0700166 * @see android.graphics.drawable.Drawable#setLevel
Joe Onorato46439ce2010-11-19 13:56:21 -0800167 */
168 public int iconLevel;
169
170 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500171 * The number of events that this notification represents. For example, in a new mail
172 * notification, this could be the number of unread messages.
Joe Malin8d40d042012-11-05 11:36:40 -0800173 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500174 * The system may or may not use this field to modify the appearance of the notification. For
175 * example, before {@link android.os.Build.VERSION_CODES#HONEYCOMB}, this number was
176 * superimposed over the icon in the status bar. Starting with
177 * {@link android.os.Build.VERSION_CODES#HONEYCOMB}, the template used by
178 * {@link Notification.Builder} has displayed the number in the expanded notification view.
Joe Malin8d40d042012-11-05 11:36:40 -0800179 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500180 * If the number is 0 or negative, it is never shown.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800181 */
182 public int number;
183
184 /**
185 * The intent to execute when the expanded status entry is clicked. If
186 * this is an activity, it must include the
187 * {@link android.content.Intent#FLAG_ACTIVITY_NEW_TASK} flag, which requires
Scott Main7aee61f2011-02-08 11:25:01 -0800188 * that you take care of task management as described in the
189 * <a href="{@docRoot}guide/topics/fundamentals/tasks-and-back-stack.html">Tasks and Back
Dianne Hackborn6ceca582012-01-10 15:24:26 -0800190 * Stack</a> document. In particular, make sure to read the notification section
191 * <a href="{@docRoot}guide/topics/ui/notifiers/notifications.html#HandlingNotifications">Handling
192 * Notifications</a> for the correct ways to launch an application from a
193 * notification.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800194 */
195 public PendingIntent contentIntent;
196
197 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500198 * The intent to execute when the notification is explicitly dismissed by the user, either with
199 * the "Clear All" button or by swiping it away individually.
200 *
201 * This probably shouldn't be launching an activity since several of those will be sent
202 * at the same time.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800203 */
204 public PendingIntent deleteIntent;
205
206 /**
Dianne Hackborn170bae72010-09-03 15:14:28 -0700207 * An intent to launch instead of posting the notification to the status bar.
Joe Onoratocb109a02011-01-18 17:57:41 -0800208 *
Chris Wren47c20a12014-06-18 17:27:29 -0400209 * <p>
210 * The system UI may choose to display a heads-up notification, instead of
211 * launching this intent, while the user is using the device.
212 * </p>
213 *
Joe Onoratocb109a02011-01-18 17:57:41 -0800214 * @see Notification.Builder#setFullScreenIntent
Daniel Sandlere46cbd32010-06-17 10:35:26 -0400215 */
216 public PendingIntent fullScreenIntent;
217
218 /**
Dan Sandler5fcdf6e2014-07-18 11:31:15 -0400219 * Text that summarizes this notification for accessibility services.
220 *
221 * As of the L release, this text is no longer shown on screen, but it is still useful to
222 * accessibility services (where it serves as an audible announcement of the notification's
223 * appearance).
Joe Onoratoef1e7762010-09-17 18:38:38 -0400224 *
Joe Onorato46439ce2010-11-19 13:56:21 -0800225 * @see #tickerView
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800226 */
227 public CharSequence tickerText;
228
229 /**
Dan Sandler5fcdf6e2014-07-18 11:31:15 -0400230 * Formerly, a view showing the {@link #tickerText}.
231 *
232 * No longer displayed in the status bar as of API 21.
Joe Onoratoef1e7762010-09-17 18:38:38 -0400233 */
Dan Sandler5fcdf6e2014-07-18 11:31:15 -0400234 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -0800235 public RemoteViews tickerView;
Joe Onoratoef1e7762010-09-17 18:38:38 -0400236
237 /**
Daniel Sandlere46cbd32010-06-17 10:35:26 -0400238 * The view that will represent this notification in the expanded status bar.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800239 */
240 public RemoteViews contentView;
241
Daniel Sandlera0a938c2012-03-15 08:42:37 -0400242 /**
Daniel Sandler4dfbe832012-04-11 14:51:46 -0400243 * A large-format version of {@link #contentView}, giving the Notification an
Daniel Sandlerf3b73432012-03-27 15:01:25 -0400244 * opportunity to show more detail. The system UI may choose to show this
245 * instead of the normal content view at its discretion.
Daniel Sandlerf3b73432012-03-27 15:01:25 -0400246 */
247 public RemoteViews bigContentView;
248
Chris Wren8fd39ec2014-02-27 17:43:26 -0500249
250 /**
Chris Wren47c20a12014-06-18 17:27:29 -0400251 * A medium-format version of {@link #contentView}, providing the Notification an
252 * opportunity to add action buttons to contentView. At its discretion, the system UI may
253 * choose to show this as a heads-up notification, which will pop up so the user can see
254 * it without leaving their current activity.
Chris Wren8fd39ec2014-02-27 17:43:26 -0500255 */
256 public RemoteViews headsUpContentView;
257
Daniel Sandlerf3b73432012-03-27 15:01:25 -0400258 /**
Joe Onorato46439ce2010-11-19 13:56:21 -0800259 * The bitmap that may escape the bounds of the panel and bar.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800260 */
Joe Onorato46439ce2010-11-19 13:56:21 -0800261 public Bitmap largeIcon;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800262
263 /**
264 * The sound to play.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500265 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800266 * <p>
Chris Wren47c20a12014-06-18 17:27:29 -0400267 * A notification that is noisy is more likely to be presented as a heads-up notification.
268 * </p>
269 *
270 * <p>
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500271 * To play the default notification sound, see {@link #defaults}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800272 * </p>
273 */
274 public Uri sound;
275
276 /**
277 * Use this constant as the value for audioStreamType to request that
278 * the default stream type for notifications be used. Currently the
Jeff Sharkey098d5802012-04-26 17:30:34 -0700279 * default stream type is {@link AudioManager#STREAM_NOTIFICATION}.
John Spurlockc0650f022014-07-19 13:22:39 -0400280 *
281 * @deprecated Use {@link #audioAttributes} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800282 */
Jean-Michel Trivi81f871e2014-08-06 16:32:38 -0700283 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800284 public static final int STREAM_DEFAULT = -1;
285
286 /**
287 * The audio stream type to use when playing the sound.
288 * Should be one of the STREAM_ constants from
289 * {@link android.media.AudioManager}.
John Spurlockc0650f022014-07-19 13:22:39 -0400290 *
291 * @deprecated Use {@link #audioAttributes} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800292 */
Jean-Michel Trivi81f871e2014-08-06 16:32:38 -0700293 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800294 public int audioStreamType = STREAM_DEFAULT;
295
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800296 /**
John Spurlockc0650f022014-07-19 13:22:39 -0400297 * The default value of {@link #audioAttributes}.
298 */
299 public static final AudioAttributes AUDIO_ATTRIBUTES_DEFAULT = new AudioAttributes.Builder()
300 .setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
301 .setUsage(AudioAttributes.USAGE_NOTIFICATION)
302 .build();
303
304 /**
305 * The {@link AudioAttributes audio attributes} to use when playing the sound.
306 */
307 public AudioAttributes audioAttributes = AUDIO_ATTRIBUTES_DEFAULT;
308
309 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500310 * The pattern with which to vibrate.
311 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800312 * <p>
313 * To vibrate the default pattern, see {@link #defaults}.
314 * </p>
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500315 *
Chris Wren47c20a12014-06-18 17:27:29 -0400316 * <p>
317 * A notification that vibrates is more likely to be presented as a heads-up notification.
318 * </p>
319 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800320 * @see android.os.Vibrator#vibrate(long[],int)
321 */
322 public long[] vibrate;
323
324 /**
325 * The color of the led. The hardware will do its best approximation.
326 *
327 * @see #FLAG_SHOW_LIGHTS
328 * @see #flags
329 */
330 public int ledARGB;
331
332 /**
333 * The number of milliseconds for the LED to be on while it's flashing.
334 * The hardware will do its best approximation.
335 *
336 * @see #FLAG_SHOW_LIGHTS
337 * @see #flags
338 */
339 public int ledOnMS;
340
341 /**
342 * The number of milliseconds for the LED to be off while it's flashing.
343 * The hardware will do its best approximation.
344 *
345 * @see #FLAG_SHOW_LIGHTS
346 * @see #flags
347 */
348 public int ledOffMS;
349
350 /**
351 * Specifies which values should be taken from the defaults.
352 * <p>
353 * To set, OR the desired from {@link #DEFAULT_SOUND},
354 * {@link #DEFAULT_VIBRATE}, {@link #DEFAULT_LIGHTS}. For all default
355 * values, use {@link #DEFAULT_ALL}.
356 * </p>
357 */
358 public int defaults;
359
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800360 /**
361 * Bit to be bitwise-ored into the {@link #flags} field that should be
362 * set if you want the LED on for this notification.
363 * <ul>
364 * <li>To turn the LED off, pass 0 in the alpha channel for colorARGB
365 * or 0 for both ledOnMS and ledOffMS.</li>
366 * <li>To turn the LED on, pass 1 for ledOnMS and 0 for ledOffMS.</li>
367 * <li>To flash the LED, pass the number of milliseconds that it should
368 * be on and off to ledOnMS and ledOffMS.</li>
369 * </ul>
370 * <p>
371 * Since hardware varies, you are not guaranteed that any of the values
372 * you pass are honored exactly. Use the system defaults (TODO) if possible
373 * because they will be set to values that work on any given hardware.
374 * <p>
375 * The alpha channel must be set for forward compatibility.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500376 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800377 */
378 public static final int FLAG_SHOW_LIGHTS = 0x00000001;
379
380 /**
381 * Bit to be bitwise-ored into the {@link #flags} field that should be
382 * set if this notification is in reference to something that is ongoing,
383 * like a phone call. It should not be set if this notification is in
384 * reference to something that happened at a particular point in time,
385 * like a missed phone call.
386 */
387 public static final int FLAG_ONGOING_EVENT = 0x00000002;
388
389 /**
390 * Bit to be bitwise-ored into the {@link #flags} field that if set,
Scott Mainb8b36452009-04-26 15:50:49 -0700391 * the audio will be repeated until the notification is
392 * cancelled or the notification window is opened.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800393 */
394 public static final int FLAG_INSISTENT = 0x00000004;
395
396 /**
397 * Bit to be bitwise-ored into the {@link #flags} field that should be
Griff Hazen293977b2014-04-28 08:37:20 -0700398 * set if you would only like the sound, vibrate and ticker to be played
399 * if the notification was not already showing.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800400 */
401 public static final int FLAG_ONLY_ALERT_ONCE = 0x00000008;
402
403 /**
404 * Bit to be bitwise-ored into the {@link #flags} field that should be
405 * set if the notification should be canceled when it is clicked by the
Daniel Sandler8aa9ae62012-12-04 23:31:47 -0500406 * user.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500407
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800408 */
409 public static final int FLAG_AUTO_CANCEL = 0x00000010;
410
411 /**
412 * Bit to be bitwise-ored into the {@link #flags} field that should be
413 * set if the notification should not be canceled when the user clicks
414 * the Clear all button.
415 */
416 public static final int FLAG_NO_CLEAR = 0x00000020;
417
Dianne Hackbornd8a43f62009-08-17 23:33:56 -0700418 /**
419 * Bit to be bitwise-ored into the {@link #flags} field that should be
420 * set if this notification represents a currently running service. This
421 * will normally be set for you by {@link Service#startForeground}.
422 */
423 public static final int FLAG_FOREGROUND_SERVICE = 0x00000040;
424
Daniel Sandlere46cbd32010-06-17 10:35:26 -0400425 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500426 * Obsolete flag indicating high-priority notifications; use the priority field instead.
Joe Malin8d40d042012-11-05 11:36:40 -0800427 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500428 * @deprecated Use {@link #priority} with a positive value.
Daniel Sandlere46cbd32010-06-17 10:35:26 -0400429 */
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500430 public static final int FLAG_HIGH_PRIORITY = 0x00000080;
Daniel Sandlere46cbd32010-06-17 10:35:26 -0400431
Griff Hazendfcb0802014-02-11 12:00:00 -0800432 /**
433 * Bit to be bitswise-ored into the {@link #flags} field that should be
434 * set if this notification is relevant to the current device only
435 * and it is not recommended that it bridge to other devices.
436 */
437 public static final int FLAG_LOCAL_ONLY = 0x00000100;
438
Griff Hazen5cadc3b2014-05-20 09:55:39 -0700439 /**
440 * Bit to be bitswise-ored into the {@link #flags} field that should be
441 * set if this notification is the group summary for a group of notifications.
442 * Grouped notifications may display in a cluster or stack on devices which
443 * support such rendering. Requires a group key also be set using {@link Builder#setGroup}.
444 */
445 public static final int FLAG_GROUP_SUMMARY = 0x00000200;
446
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800447 public int flags;
448
Tor Norbyed9273d62013-05-30 15:59:53 -0700449 /** @hide */
450 @IntDef({PRIORITY_DEFAULT,PRIORITY_LOW,PRIORITY_MIN,PRIORITY_HIGH,PRIORITY_MAX})
451 @Retention(RetentionPolicy.SOURCE)
452 public @interface Priority {}
453
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800454 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500455 * Default notification {@link #priority}. If your application does not prioritize its own
456 * notifications, use this value for all notifications.
457 */
458 public static final int PRIORITY_DEFAULT = 0;
459
460 /**
461 * Lower {@link #priority}, for items that are less important. The UI may choose to show these
462 * items smaller, or at a different position in the list, compared with your app's
463 * {@link #PRIORITY_DEFAULT} items.
464 */
465 public static final int PRIORITY_LOW = -1;
466
467 /**
468 * Lowest {@link #priority}; these items might not be shown to the user except under special
469 * circumstances, such as detailed notification logs.
470 */
471 public static final int PRIORITY_MIN = -2;
472
473 /**
474 * Higher {@link #priority}, for more important notifications or alerts. The UI may choose to
475 * show these items larger, or at a different position in notification lists, compared with
476 * your app's {@link #PRIORITY_DEFAULT} items.
477 */
478 public static final int PRIORITY_HIGH = 1;
479
480 /**
481 * Highest {@link #priority}, for your application's most important items that require the
482 * user's prompt attention or input.
483 */
484 public static final int PRIORITY_MAX = 2;
485
486 /**
487 * Relative priority for this notification.
Joe Malin8d40d042012-11-05 11:36:40 -0800488 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500489 * Priority is an indication of how much of the user's valuable attention should be consumed by
490 * this notification. Low-priority notifications may be hidden from the user in certain
491 * situations, while the user might be interrupted for a higher-priority notification. The
Daniel Sandler6738eee2012-11-16 12:03:32 -0500492 * system will make a determination about how to interpret this priority when presenting
493 * the notification.
Chris Wren47c20a12014-06-18 17:27:29 -0400494 *
495 * <p>
496 * A notification that is at least {@link #PRIORITY_HIGH} is more likely to be presented
497 * as a heads-up notification.
498 * </p>
499 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500500 */
Tor Norbyed9273d62013-05-30 15:59:53 -0700501 @Priority
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500502 public int priority;
Joe Malin8d40d042012-11-05 11:36:40 -0800503
Dan Sandler26e81cf2014-05-06 10:01:27 -0400504 /**
505 * Accent color (an ARGB integer like the constants in {@link android.graphics.Color})
506 * to be applied by the standard Style templates when presenting this notification.
507 *
508 * The current template design constructs a colorful header image by overlaying the
509 * {@link #icon} image (stenciled in white) atop a field of this color. Alpha components are
510 * ignored.
511 */
512 public int color = COLOR_DEFAULT;
513
514 /**
515 * Special value of {@link #color} telling the system not to decorate this notification with
516 * any special color but instead use default colors when presenting this notification.
517 */
518 public static final int COLOR_DEFAULT = 0; // AKA Color.TRANSPARENT
Dan Sandler0bf2ed82013-12-21 23:33:41 -0600519
520 /**
521 * Sphere of visibility of this notification, which affects how and when the SystemUI reveals
522 * the notification's presence and contents in untrusted situations (namely, on the secure
523 * lockscreen).
524 *
525 * The default level, {@link #VISIBILITY_PRIVATE}, behaves exactly as notifications have always
526 * done on Android: The notification's {@link #icon} and {@link #tickerText} (if available) are
527 * shown in all situations, but the contents are only available if the device is unlocked for
528 * the appropriate user.
529 *
530 * A more permissive policy can be expressed by {@link #VISIBILITY_PUBLIC}; such a notification
531 * can be read even in an "insecure" context (that is, above a secure lockscreen).
532 * To modify the public version of this notification—for example, to redact some portions—see
533 * {@link Builder#setPublicVersion(Notification)}.
534 *
535 * Finally, a notification can be made {@link #VISIBILITY_SECRET}, which will suppress its icon
536 * and ticker until the user has bypassed the lockscreen.
537 */
538 public int visibility;
539
540 public static final int VISIBILITY_PUBLIC = 1;
541 public static final int VISIBILITY_PRIVATE = 0;
542 public static final int VISIBILITY_SECRET = -1;
543
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500544 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400545 * Notification category: incoming call (voice or video) or similar synchronous communication request.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500546 */
John Spurlockfd7f1e02014-03-18 16:41:57 -0400547 public static final String CATEGORY_CALL = "call";
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500548
549 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400550 * Notification category: incoming direct message (SMS, instant message, etc.).
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500551 */
John Spurlockfd7f1e02014-03-18 16:41:57 -0400552 public static final String CATEGORY_MESSAGE = "msg";
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500553
554 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400555 * Notification category: asynchronous bulk message (email).
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500556 */
John Spurlockfd7f1e02014-03-18 16:41:57 -0400557 public static final String CATEGORY_EMAIL = "email";
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500558
559 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400560 * Notification category: calendar event.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500561 */
John Spurlockfd7f1e02014-03-18 16:41:57 -0400562 public static final String CATEGORY_EVENT = "event";
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500563
564 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400565 * Notification category: promotion or advertisement.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500566 */
John Spurlockfd7f1e02014-03-18 16:41:57 -0400567 public static final String CATEGORY_PROMO = "promo";
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500568
569 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400570 * Notification category: alarm or timer.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500571 */
John Spurlockfd7f1e02014-03-18 16:41:57 -0400572 public static final String CATEGORY_ALARM = "alarm";
573
574 /**
575 * Notification category: progress of a long-running background operation.
576 */
577 public static final String CATEGORY_PROGRESS = "progress";
578
579 /**
580 * Notification category: social network or sharing update.
581 */
582 public static final String CATEGORY_SOCIAL = "social";
583
584 /**
585 * Notification category: error in background operation or authentication status.
586 */
587 public static final String CATEGORY_ERROR = "err";
588
589 /**
590 * Notification category: media transport control for playback.
591 */
592 public static final String CATEGORY_TRANSPORT = "transport";
593
594 /**
595 * Notification category: system or device status update. Reserved for system use.
596 */
597 public static final String CATEGORY_SYSTEM = "sys";
598
599 /**
600 * Notification category: indication of running background service.
601 */
602 public static final String CATEGORY_SERVICE = "service";
603
604 /**
John Spurlock0a69c8c2014-03-21 13:30:57 -0400605 * Notification category: a specific, timely recommendation for a single thing.
606 * For example, a news app might want to recommend a news story it believes the user will
607 * want to read next.
608 */
609 public static final String CATEGORY_RECOMMENDATION = "recommendation";
610
611 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400612 * Notification category: ongoing information about device or contextual status.
613 */
614 public static final String CATEGORY_STATUS = "status";
615
616 /**
617 * One of the predefined notification categories (see the <code>CATEGORY_*</code> constants)
618 * that best describes this Notification. May be used by the system for ranking and filtering.
619 */
620 public String category;
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500621
Griff Hazen5cadc3b2014-05-20 09:55:39 -0700622 private String mGroupKey;
623
624 /**
625 * Get the key used to group this notification into a cluster or stack
626 * with other notifications on devices which support such rendering.
627 */
628 public String getGroup() {
629 return mGroupKey;
630 }
631
632 private String mSortKey;
633
634 /**
635 * Get a sort key that orders this notification among other notifications from the
636 * same package. This can be useful if an external sort was already applied and an app
637 * would like to preserve this. Notifications will be sorted lexicographically using this
638 * value, although providing different priorities in addition to providing sort key may
639 * cause this value to be ignored.
640 *
641 * <p>This sort key can also be used to order members of a notification group. See
642 * {@link Builder#setGroup}.
643 *
644 * @see String#compareTo(String)
645 */
646 public String getSortKey() {
647 return mSortKey;
648 }
649
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500650 /**
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400651 * Additional semantic data to be carried around with this Notification.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400652 * <p>
653 * The extras keys defined here are intended to capture the original inputs to {@link Builder}
654 * APIs, and are intended to be used by
655 * {@link android.service.notification.NotificationListenerService} implementations to extract
656 * detailed information from notification objects.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500657 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400658 public Bundle extras = new Bundle();
659
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400660 /**
661 * {@link #extras} key: this is the title of the notification,
662 * as supplied to {@link Builder#setContentTitle(CharSequence)}.
663 */
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -0500664 public static final String EXTRA_TITLE = "android.title";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400665
666 /**
667 * {@link #extras} key: this is the title of the notification when shown in expanded form,
668 * e.g. as supplied to {@link BigTextStyle#setBigContentTitle(CharSequence)}.
669 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400670 public static final String EXTRA_TITLE_BIG = EXTRA_TITLE + ".big";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400671
672 /**
673 * {@link #extras} key: this is the main text payload, as supplied to
674 * {@link Builder#setContentText(CharSequence)}.
675 */
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -0500676 public static final String EXTRA_TEXT = "android.text";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400677
678 /**
679 * {@link #extras} key: this is a third line of text, as supplied to
680 * {@link Builder#setSubText(CharSequence)}.
681 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400682 public static final String EXTRA_SUB_TEXT = "android.subText";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400683
684 /**
685 * {@link #extras} key: this is a small piece of additional text as supplied to
686 * {@link Builder#setContentInfo(CharSequence)}.
687 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400688 public static final String EXTRA_INFO_TEXT = "android.infoText";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400689
690 /**
691 * {@link #extras} key: this is a line of summary information intended to be shown
692 * alongside expanded notifications, as supplied to (e.g.)
693 * {@link BigTextStyle#setSummaryText(CharSequence)}.
694 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400695 public static final String EXTRA_SUMMARY_TEXT = "android.summaryText";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400696
697 /**
Christoph Studer4600f9b2014-07-22 22:44:43 +0200698 * {@link #extras} key: this is the longer text shown in the big form of a
699 * {@link BigTextStyle} notification, as supplied to
700 * {@link BigTextStyle#bigText(CharSequence)}.
701 */
702 public static final String EXTRA_BIG_TEXT = "android.bigText";
703
704 /**
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400705 * {@link #extras} key: this is the resource ID of the notification's main small icon, as
706 * supplied to {@link Builder#setSmallIcon(int)}.
707 */
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -0500708 public static final String EXTRA_SMALL_ICON = "android.icon";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400709
710 /**
711 * {@link #extras} key: this is a bitmap to be used instead of the small icon when showing the
712 * notification payload, as
713 * supplied to {@link Builder#setLargeIcon(android.graphics.Bitmap)}.
714 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400715 public static final String EXTRA_LARGE_ICON = "android.largeIcon";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400716
717 /**
718 * {@link #extras} key: this is a bitmap to be used instead of the one from
719 * {@link Builder#setLargeIcon(android.graphics.Bitmap)} when the notification is
720 * shown in its expanded form, as supplied to
721 * {@link BigPictureStyle#bigLargeIcon(android.graphics.Bitmap)}.
722 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400723 public static final String EXTRA_LARGE_ICON_BIG = EXTRA_LARGE_ICON + ".big";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400724
725 /**
726 * {@link #extras} key: this is the progress value supplied to
727 * {@link Builder#setProgress(int, int, boolean)}.
728 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400729 public static final String EXTRA_PROGRESS = "android.progress";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400730
731 /**
732 * {@link #extras} key: this is the maximum value supplied to
733 * {@link Builder#setProgress(int, int, boolean)}.
734 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400735 public static final String EXTRA_PROGRESS_MAX = "android.progressMax";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400736
737 /**
738 * {@link #extras} key: whether the progress bar is indeterminate, supplied to
739 * {@link Builder#setProgress(int, int, boolean)}.
740 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400741 public static final String EXTRA_PROGRESS_INDETERMINATE = "android.progressIndeterminate";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400742
743 /**
744 * {@link #extras} key: whether {@link #when} should be shown as a count-up timer (specifically
745 * a {@link android.widget.Chronometer}) instead of a timestamp, as supplied to
746 * {@link Builder#setUsesChronometer(boolean)}.
747 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400748 public static final String EXTRA_SHOW_CHRONOMETER = "android.showChronometer";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400749
750 /**
751 * {@link #extras} key: whether {@link #when} should be shown,
752 * as supplied to {@link Builder#setShowWhen(boolean)}.
753 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400754 public static final String EXTRA_SHOW_WHEN = "android.showWhen";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400755
756 /**
757 * {@link #extras} key: this is a bitmap to be shown in {@link BigPictureStyle} expanded
758 * notifications, supplied to {@link BigPictureStyle#bigPicture(android.graphics.Bitmap)}.
759 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400760 public static final String EXTRA_PICTURE = "android.picture";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400761
762 /**
763 * {@link #extras} key: An array of CharSequences to show in {@link InboxStyle} expanded
764 * notifications, each of which was supplied to {@link InboxStyle#addLine(CharSequence)}.
765 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400766 public static final String EXTRA_TEXT_LINES = "android.textLines";
Dan Sandler842dd772014-05-15 09:36:47 -0400767
768 /**
769 * {@link #extras} key: A string representing the name of the specific
770 * {@link android.app.Notification.Style} used to create this notification.
771 */
Chris Wren91ad5632013-06-05 15:05:57 -0400772 public static final String EXTRA_TEMPLATE = "android.template";
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400773
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400774 /**
775 * {@link #extras} key: An array of people that this notification relates to, specified
776 * by contacts provider contact URI.
777 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400778 public static final String EXTRA_PEOPLE = "android.people";
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500779
780 /**
Chris Wren47c20a12014-06-18 17:27:29 -0400781 * {@link #extras} key: used to provide hints about the appropriateness of
782 * displaying this notification as a heads-up notification.
Chris Wren51c75102013-07-16 20:49:17 -0400783 * @hide
784 */
785 public static final String EXTRA_AS_HEADS_UP = "headsup";
786
787 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400788 * Allow certain system-generated notifications to appear before the device is provisioned.
789 * Only available to notifications coming from the android package.
790 * @hide
791 */
792 public static final String EXTRA_ALLOW_DURING_SETUP = "android.allowDuringSetup";
793
794 /**
Jose Limae9e3b3b2014-05-18 23:44:50 -0700795 * {@link #extras} key: A
796 * {@link android.content.ContentUris content URI} pointing to an image that can be displayed
797 * in the background when the notification is selected. The URI must point to an image stream
798 * suitable for passing into
799 * {@link android.graphics.BitmapFactory#decodeStream(java.io.InputStream)
800 * BitmapFactory.decodeStream}; all other content types will be ignored. The content provider
801 * URI used for this purpose must require no permissions to read the image data.
802 */
803 public static final String EXTRA_BACKGROUND_IMAGE_URI = "android.backgroundImageUri";
804
805 /**
Dan Sandler842dd772014-05-15 09:36:47 -0400806 * {@link #extras} key: A
Jeff Browndba34ba2014-06-24 20:46:03 -0700807 * {@link android.media.session.MediaSession.Token} associated with a
Dan Sandler842dd772014-05-15 09:36:47 -0400808 * {@link android.app.Notification.MediaStyle} notification.
809 */
810 public static final String EXTRA_MEDIA_SESSION = "android.mediaSession";
811
812 /**
Bryan Mawhinneye191f902014-07-22 12:50:09 +0100813 * {@link #extras} key: the indices of actions to be shown in the compact view,
814 * as supplied to (e.g.) {@link MediaStyle#setShowActionsInCompactView(int...)}.
815 */
816 public static final String EXTRA_COMPACT_ACTIONS = "android.compactActions";
817
Christoph Studer943aa672014-08-03 20:31:16 +0200818 /**
Christoph Studer7ac80e62014-08-04 16:01:57 +0200819 * {@link #extras} key: the user that built the notification.
Christoph Studer943aa672014-08-03 20:31:16 +0200820 *
821 * @hide
822 */
Christoph Studer7ac80e62014-08-04 16:01:57 +0200823 public static final String EXTRA_ORIGINATING_USERID = "android.originatingUserId";
Christoph Studer943aa672014-08-03 20:31:16 +0200824
Bryan Mawhinneye191f902014-07-22 12:50:09 +0100825 /**
Chris Wren47c20a12014-06-18 17:27:29 -0400826 * Value for {@link #EXTRA_AS_HEADS_UP} that indicates this notification should not be
827 * displayed in the heads up space.
828 *
829 * <p>
830 * If this notification has a {@link #fullScreenIntent}, then it will always launch the
831 * full-screen intent when posted.
832 * </p>
Chris Wren51c75102013-07-16 20:49:17 -0400833 * @hide
834 */
835 public static final int HEADS_UP_NEVER = 0;
836
837 /**
Chris Wren47c20a12014-06-18 17:27:29 -0400838 * Default value for {@link #EXTRA_AS_HEADS_UP} that indicates this notification may be
839 * displayed as a heads up.
Chris Wren51c75102013-07-16 20:49:17 -0400840 * @hide
841 */
842 public static final int HEADS_UP_ALLOWED = 1;
843
844 /**
Chris Wren47c20a12014-06-18 17:27:29 -0400845 * Value for {@link #EXTRA_AS_HEADS_UP} that indicates this notification is a
846 * good candidate for display as a heads up.
Chris Wren51c75102013-07-16 20:49:17 -0400847 * @hide
848 */
849 public static final int HEADS_UP_REQUESTED = 2;
850
851 /**
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400852 * Structure to encapsulate a named action that can be shown as part of this notification.
853 * It must include an icon, a label, and a {@link PendingIntent} to be fired when the action is
854 * selected by the user.
855 * <p>
Griff Hazen959591e2014-05-15 22:26:18 -0700856 * Apps should use {@link Notification.Builder#addAction(int, CharSequence, PendingIntent)}
857 * or {@link Notification.Builder#addAction(Notification.Action)}
858 * to attach actions.
Daniel Sandlera0a938c2012-03-15 08:42:37 -0400859 */
Daniel Sandlerea2a3172013-02-20 22:24:20 -0500860 public static class Action implements Parcelable {
Griff Hazen959591e2014-05-15 22:26:18 -0700861 private final Bundle mExtras;
Griff Hazen61a9e862014-05-22 16:05:19 -0700862 private final RemoteInput[] mRemoteInputs;
Griff Hazen959591e2014-05-15 22:26:18 -0700863
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400864 /**
865 * Small icon representing the action.
866 */
Daniel Sandlera0a938c2012-03-15 08:42:37 -0400867 public int icon;
Griff Hazen5cadc3b2014-05-20 09:55:39 -0700868
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400869 /**
870 * Title of the action.
871 */
Daniel Sandlera0a938c2012-03-15 08:42:37 -0400872 public CharSequence title;
Griff Hazen5cadc3b2014-05-20 09:55:39 -0700873
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400874 /**
875 * Intent to send when the user invokes this action. May be null, in which case the action
876 * may be rendered in a disabled presentation by the system UI.
877 */
Daniel Sandlera0a938c2012-03-15 08:42:37 -0400878 public PendingIntent actionIntent;
Griff Hazen959591e2014-05-15 22:26:18 -0700879
Daniel Sandlera0a938c2012-03-15 08:42:37 -0400880 private Action(Parcel in) {
881 icon = in.readInt();
882 title = TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(in);
883 if (in.readInt() == 1) {
884 actionIntent = PendingIntent.CREATOR.createFromParcel(in);
885 }
Griff Hazen959591e2014-05-15 22:26:18 -0700886 mExtras = in.readBundle();
Griff Hazen5cadc3b2014-05-20 09:55:39 -0700887 mRemoteInputs = in.createTypedArray(RemoteInput.CREATOR);
Daniel Sandlera0a938c2012-03-15 08:42:37 -0400888 }
Griff Hazen5cadc3b2014-05-20 09:55:39 -0700889
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400890 /**
Griff Hazen959591e2014-05-15 22:26:18 -0700891 * Use {@link Notification.Builder#addAction(int, CharSequence, PendingIntent)}.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400892 */
893 public Action(int icon, CharSequence title, PendingIntent intent) {
Griff Hazen5cadc3b2014-05-20 09:55:39 -0700894 this(icon, title, intent, new Bundle(), null);
Griff Hazen959591e2014-05-15 22:26:18 -0700895 }
896
Griff Hazen5cadc3b2014-05-20 09:55:39 -0700897 private Action(int icon, CharSequence title, PendingIntent intent, Bundle extras,
898 RemoteInput[] remoteInputs) {
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400899 this.icon = icon;
900 this.title = title;
901 this.actionIntent = intent;
Griff Hazen959591e2014-05-15 22:26:18 -0700902 this.mExtras = extras != null ? extras : new Bundle();
Griff Hazen5cadc3b2014-05-20 09:55:39 -0700903 this.mRemoteInputs = remoteInputs;
Griff Hazen959591e2014-05-15 22:26:18 -0700904 }
905
906 /**
907 * Get additional metadata carried around with this Action.
908 */
909 public Bundle getExtras() {
910 return mExtras;
911 }
912
913 /**
Griff Hazen5cadc3b2014-05-20 09:55:39 -0700914 * Get the list of inputs to be collected from the user when this action is sent.
915 * May return null if no remote inputs were added.
916 */
917 public RemoteInput[] getRemoteInputs() {
918 return mRemoteInputs;
919 }
920
921 /**
Griff Hazen959591e2014-05-15 22:26:18 -0700922 * Builder class for {@link Action} objects.
923 */
Griff Hazen5cadc3b2014-05-20 09:55:39 -0700924 public static final class Builder {
Griff Hazen959591e2014-05-15 22:26:18 -0700925 private final int mIcon;
926 private final CharSequence mTitle;
927 private final PendingIntent mIntent;
928 private final Bundle mExtras;
Griff Hazen5cadc3b2014-05-20 09:55:39 -0700929 private ArrayList<RemoteInput> mRemoteInputs;
Griff Hazen959591e2014-05-15 22:26:18 -0700930
931 /**
932 * Construct a new builder for {@link Action} object.
933 * @param icon icon to show for this action
934 * @param title the title of the action
935 * @param intent the {@link PendingIntent} to fire when users trigger this action
936 */
937 public Builder(int icon, CharSequence title, PendingIntent intent) {
Griff Hazen5cadc3b2014-05-20 09:55:39 -0700938 this(icon, title, intent, new Bundle(), null);
Griff Hazen959591e2014-05-15 22:26:18 -0700939 }
940
941 /**
942 * Construct a new builder for {@link Action} object using the fields from an
943 * {@link Action}.
944 * @param action the action to read fields from.
945 */
946 public Builder(Action action) {
Griff Hazen5cadc3b2014-05-20 09:55:39 -0700947 this(action.icon, action.title, action.actionIntent, new Bundle(action.mExtras),
948 action.getRemoteInputs());
Griff Hazen959591e2014-05-15 22:26:18 -0700949 }
950
Griff Hazen5cadc3b2014-05-20 09:55:39 -0700951 private Builder(int icon, CharSequence title, PendingIntent intent, Bundle extras,
952 RemoteInput[] remoteInputs) {
Griff Hazen959591e2014-05-15 22:26:18 -0700953 mIcon = icon;
954 mTitle = title;
955 mIntent = intent;
956 mExtras = extras;
Griff Hazen5cadc3b2014-05-20 09:55:39 -0700957 if (remoteInputs != null) {
958 mRemoteInputs = new ArrayList<RemoteInput>(remoteInputs.length);
959 Collections.addAll(mRemoteInputs, remoteInputs);
960 }
Griff Hazen959591e2014-05-15 22:26:18 -0700961 }
962
963 /**
964 * Merge additional metadata into this builder.
965 *
966 * <p>Values within the Bundle will replace existing extras values in this Builder.
967 *
968 * @see Notification.Action#extras
969 */
970 public Builder addExtras(Bundle extras) {
971 if (extras != null) {
972 mExtras.putAll(extras);
973 }
974 return this;
975 }
976
977 /**
978 * Get the metadata Bundle used by this Builder.
979 *
980 * <p>The returned Bundle is shared with this Builder.
981 */
982 public Bundle getExtras() {
983 return mExtras;
984 }
985
986 /**
Griff Hazen5cadc3b2014-05-20 09:55:39 -0700987 * Add an input to be collected from the user when this action is sent.
988 * Response values can be retrieved from the fired intent by using the
989 * {@link RemoteInput#getResultsFromIntent} function.
990 * @param remoteInput a {@link RemoteInput} to add to the action
991 * @return this object for method chaining
992 */
993 public Builder addRemoteInput(RemoteInput remoteInput) {
994 if (mRemoteInputs == null) {
995 mRemoteInputs = new ArrayList<RemoteInput>();
996 }
997 mRemoteInputs.add(remoteInput);
998 return this;
999 }
1000
1001 /**
1002 * Apply an extender to this action builder. Extenders may be used to add
1003 * metadata or change options on this builder.
1004 */
Griff Hazen61a9e862014-05-22 16:05:19 -07001005 public Builder extend(Extender extender) {
1006 extender.extend(this);
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001007 return this;
1008 }
1009
1010 /**
Griff Hazen959591e2014-05-15 22:26:18 -07001011 * Combine all of the options that have been set and return a new {@link Action}
1012 * object.
1013 * @return the built action
1014 */
1015 public Action build() {
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001016 RemoteInput[] remoteInputs = mRemoteInputs != null
1017 ? mRemoteInputs.toArray(new RemoteInput[mRemoteInputs.size()]) : null;
1018 return new Action(mIcon, mTitle, mIntent, mExtras, remoteInputs);
Griff Hazen959591e2014-05-15 22:26:18 -07001019 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001020 }
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001021
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001022 @Override
1023 public Action clone() {
1024 return new Action(
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001025 icon,
1026 title,
1027 actionIntent, // safe to alias
1028 new Bundle(mExtras),
1029 getRemoteInputs());
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001030 }
1031 @Override
1032 public int describeContents() {
1033 return 0;
1034 }
1035 @Override
1036 public void writeToParcel(Parcel out, int flags) {
1037 out.writeInt(icon);
1038 TextUtils.writeToParcel(title, out, flags);
1039 if (actionIntent != null) {
1040 out.writeInt(1);
1041 actionIntent.writeToParcel(out, flags);
1042 } else {
1043 out.writeInt(0);
1044 }
Griff Hazen959591e2014-05-15 22:26:18 -07001045 out.writeBundle(mExtras);
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001046 out.writeTypedArray(mRemoteInputs, flags);
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001047 }
Griff Hazen959591e2014-05-15 22:26:18 -07001048 public static final Parcelable.Creator<Action> CREATOR =
1049 new Parcelable.Creator<Action>() {
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001050 public Action createFromParcel(Parcel in) {
1051 return new Action(in);
1052 }
1053 public Action[] newArray(int size) {
1054 return new Action[size];
1055 }
1056 };
Griff Hazen61a9e862014-05-22 16:05:19 -07001057
1058 /**
1059 * Extender interface for use with {@link Builder#extend}. Extenders may be used to add
1060 * metadata or change options on an action builder.
1061 */
1062 public interface Extender {
1063 /**
1064 * Apply this extender to a notification action builder.
1065 * @param builder the builder to be modified.
1066 * @return the build object for chaining.
1067 */
1068 public Builder extend(Builder builder);
1069 }
1070
1071 /**
1072 * Wearable extender for notification actions. To add extensions to an action,
1073 * create a new {@link android.app.Notification.Action.WearableExtender} object using
1074 * the {@code WearableExtender()} constructor and apply it to a
1075 * {@link android.app.Notification.Action.Builder} using
1076 * {@link android.app.Notification.Action.Builder#extend}.
1077 *
1078 * <pre class="prettyprint">
1079 * Notification.Action action = new Notification.Action.Builder(
1080 * R.drawable.archive_all, "Archive all", actionIntent)
Griff Hazen14f57992014-05-26 09:07:14 -07001081 * .extend(new Notification.Action.WearableExtender()
Griff Hazen61a9e862014-05-22 16:05:19 -07001082 * .setAvailableOffline(false))
Griff Hazen14f57992014-05-26 09:07:14 -07001083 * .build();</pre>
Griff Hazen61a9e862014-05-22 16:05:19 -07001084 */
1085 public static final class WearableExtender implements Extender {
1086 /** Notification action extra which contains wearable extensions */
1087 private static final String EXTRA_WEARABLE_EXTENSIONS = "android.wearable.EXTENSIONS";
1088
1089 private static final String KEY_FLAGS = "flags";
1090
1091 // Flags bitwise-ored to mFlags
1092 private static final int FLAG_AVAILABLE_OFFLINE = 0x1;
1093
1094 // Default value for flags integer
1095 private static final int DEFAULT_FLAGS = FLAG_AVAILABLE_OFFLINE;
1096
1097 private int mFlags = DEFAULT_FLAGS;
1098
1099 /**
1100 * Create a {@link android.app.Notification.Action.WearableExtender} with default
1101 * options.
1102 */
1103 public WearableExtender() {
1104 }
1105
1106 /**
1107 * Create a {@link android.app.Notification.Action.WearableExtender} by reading
1108 * wearable options present in an existing notification action.
1109 * @param action the notification action to inspect.
1110 */
1111 public WearableExtender(Action action) {
1112 Bundle wearableBundle = action.getExtras().getBundle(EXTRA_WEARABLE_EXTENSIONS);
1113 if (wearableBundle != null) {
1114 mFlags = wearableBundle.getInt(KEY_FLAGS, DEFAULT_FLAGS);
1115 }
1116 }
1117
1118 /**
1119 * Apply wearable extensions to a notification action that is being built. This is
1120 * typically called by the {@link android.app.Notification.Action.Builder#extend}
1121 * method of {@link android.app.Notification.Action.Builder}.
1122 */
1123 @Override
1124 public Action.Builder extend(Action.Builder builder) {
1125 Bundle wearableBundle = new Bundle();
1126
1127 if (mFlags != DEFAULT_FLAGS) {
1128 wearableBundle.putInt(KEY_FLAGS, mFlags);
1129 }
1130
1131 builder.getExtras().putBundle(EXTRA_WEARABLE_EXTENSIONS, wearableBundle);
1132 return builder;
1133 }
1134
1135 @Override
1136 public WearableExtender clone() {
1137 WearableExtender that = new WearableExtender();
1138 that.mFlags = this.mFlags;
1139 return that;
1140 }
1141
1142 /**
1143 * Set whether this action is available when the wearable device is not connected to
1144 * a companion device. The user can still trigger this action when the wearable device is
1145 * offline, but a visual hint will indicate that the action may not be available.
1146 * Defaults to true.
1147 */
1148 public WearableExtender setAvailableOffline(boolean availableOffline) {
1149 setFlag(FLAG_AVAILABLE_OFFLINE, availableOffline);
1150 return this;
1151 }
1152
1153 /**
1154 * Get whether this action is available when the wearable device is not connected to
1155 * a companion device. The user can still trigger this action when the wearable device is
1156 * offline, but a visual hint will indicate that the action may not be available.
1157 * Defaults to true.
1158 */
1159 public boolean isAvailableOffline() {
1160 return (mFlags & FLAG_AVAILABLE_OFFLINE) != 0;
1161 }
1162
1163 private void setFlag(int mask, boolean value) {
1164 if (value) {
1165 mFlags |= mask;
1166 } else {
1167 mFlags &= ~mask;
1168 }
1169 }
1170 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001171 }
1172
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001173 /**
1174 * Array of all {@link Action} structures attached to this notification by
1175 * {@link Builder#addAction(int, CharSequence, PendingIntent)}. Mostly useful for instances of
1176 * {@link android.service.notification.NotificationListenerService} that provide an alternative
1177 * interface for invoking actions.
1178 */
Daniel Sandlerea2a3172013-02-20 22:24:20 -05001179 public Action[] actions;
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001180
1181 /**
Dan Sandler0bf2ed82013-12-21 23:33:41 -06001182 * Replacement version of this notification whose content will be shown
1183 * in an insecure context such as atop a secure keyguard. See {@link #visibility}
1184 * and {@link #VISIBILITY_PUBLIC}.
1185 */
1186 public Notification publicVersion;
1187
1188 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001189 * Constructs a Notification object with default values.
Joe Onorato46439ce2010-11-19 13:56:21 -08001190 * You might want to consider using {@link Builder} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001191 */
1192 public Notification()
1193 {
1194 this.when = System.currentTimeMillis();
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001195 this.priority = PRIORITY_DEFAULT;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001196 }
1197
1198 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001199 * @hide
1200 */
1201 public Notification(Context context, int icon, CharSequence tickerText, long when,
1202 CharSequence contentTitle, CharSequence contentText, Intent contentIntent)
1203 {
1204 this.when = when;
1205 this.icon = icon;
1206 this.tickerText = tickerText;
1207 setLatestEventInfo(context, contentTitle, contentText,
1208 PendingIntent.getActivity(context, 0, contentIntent, 0));
1209 }
1210
1211 /**
1212 * Constructs a Notification object with the information needed to
1213 * have a status bar icon without the standard expanded view.
1214 *
1215 * @param icon The resource id of the icon to put in the status bar.
1216 * @param tickerText The text that flows by in the status bar when the notification first
1217 * activates.
1218 * @param when The time to show in the time field. In the System.currentTimeMillis
1219 * timebase.
Joe Onorato46439ce2010-11-19 13:56:21 -08001220 *
1221 * @deprecated Use {@link Builder} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001222 */
Joe Onorato46439ce2010-11-19 13:56:21 -08001223 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001224 public Notification(int icon, CharSequence tickerText, long when)
1225 {
1226 this.icon = icon;
1227 this.tickerText = tickerText;
1228 this.when = when;
1229 }
1230
1231 /**
1232 * Unflatten the notification from a parcel.
1233 */
1234 public Notification(Parcel parcel)
1235 {
1236 int version = parcel.readInt();
1237
1238 when = parcel.readLong();
1239 icon = parcel.readInt();
1240 number = parcel.readInt();
1241 if (parcel.readInt() != 0) {
1242 contentIntent = PendingIntent.CREATOR.createFromParcel(parcel);
1243 }
1244 if (parcel.readInt() != 0) {
1245 deleteIntent = PendingIntent.CREATOR.createFromParcel(parcel);
1246 }
1247 if (parcel.readInt() != 0) {
1248 tickerText = TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(parcel);
1249 }
1250 if (parcel.readInt() != 0) {
Joe Onorato46439ce2010-11-19 13:56:21 -08001251 tickerView = RemoteViews.CREATOR.createFromParcel(parcel);
Joe Onoratoef1e7762010-09-17 18:38:38 -04001252 }
1253 if (parcel.readInt() != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001254 contentView = RemoteViews.CREATOR.createFromParcel(parcel);
1255 }
Joe Onorato561d3852010-11-20 18:09:34 -08001256 if (parcel.readInt() != 0) {
1257 largeIcon = Bitmap.CREATOR.createFromParcel(parcel);
1258 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001259 defaults = parcel.readInt();
1260 flags = parcel.readInt();
1261 if (parcel.readInt() != 0) {
1262 sound = Uri.CREATOR.createFromParcel(parcel);
1263 }
1264
1265 audioStreamType = parcel.readInt();
John Spurlockc0650f022014-07-19 13:22:39 -04001266 if (parcel.readInt() != 0) {
1267 audioAttributes = AudioAttributes.CREATOR.createFromParcel(parcel);
1268 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001269 vibrate = parcel.createLongArray();
1270 ledARGB = parcel.readInt();
1271 ledOnMS = parcel.readInt();
1272 ledOffMS = parcel.readInt();
1273 iconLevel = parcel.readInt();
Daniel Sandlere46cbd32010-06-17 10:35:26 -04001274
1275 if (parcel.readInt() != 0) {
1276 fullScreenIntent = PendingIntent.CREATOR.createFromParcel(parcel);
1277 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001278
1279 priority = parcel.readInt();
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001280
John Spurlockfd7f1e02014-03-18 16:41:57 -04001281 category = parcel.readString();
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001282
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001283 mGroupKey = parcel.readString();
1284
1285 mSortKey = parcel.readString();
1286
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001287 extras = parcel.readBundle(); // may be null
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001288
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001289 actions = parcel.createTypedArray(Action.CREATOR); // may be null
1290
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001291 if (parcel.readInt() != 0) {
Daniel Sandlerf3b73432012-03-27 15:01:25 -04001292 bigContentView = RemoteViews.CREATOR.createFromParcel(parcel);
1293 }
Dan Sandler0bf2ed82013-12-21 23:33:41 -06001294
Chris Wren8fd39ec2014-02-27 17:43:26 -05001295 if (parcel.readInt() != 0) {
1296 headsUpContentView = RemoteViews.CREATOR.createFromParcel(parcel);
1297 }
1298
Dan Sandler0bf2ed82013-12-21 23:33:41 -06001299 visibility = parcel.readInt();
1300
1301 if (parcel.readInt() != 0) {
1302 publicVersion = Notification.CREATOR.createFromParcel(parcel);
1303 }
Dan Sandler26e81cf2014-05-06 10:01:27 -04001304
1305 color = parcel.readInt();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001306 }
1307
Andy Stadler110988c2010-12-03 14:29:16 -08001308 @Override
Joe Onorato18e69df2010-05-17 22:26:12 -07001309 public Notification clone() {
1310 Notification that = new Notification();
Daniel Sandler1a497d32013-04-18 14:52:45 -04001311 cloneInto(that, true);
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001312 return that;
1313 }
Joe Onorato18e69df2010-05-17 22:26:12 -07001314
Daniel Sandler1a497d32013-04-18 14:52:45 -04001315 /**
1316 * Copy all (or if heavy is false, all except Bitmaps and RemoteViews) members
1317 * of this into that.
1318 * @hide
1319 */
1320 public void cloneInto(Notification that, boolean heavy) {
Joe Onorato18e69df2010-05-17 22:26:12 -07001321 that.when = this.when;
1322 that.icon = this.icon;
1323 that.number = this.number;
1324
1325 // PendingIntents are global, so there's no reason (or way) to clone them.
1326 that.contentIntent = this.contentIntent;
1327 that.deleteIntent = this.deleteIntent;
Daniel Sandlere46cbd32010-06-17 10:35:26 -04001328 that.fullScreenIntent = this.fullScreenIntent;
Joe Onorato18e69df2010-05-17 22:26:12 -07001329
1330 if (this.tickerText != null) {
1331 that.tickerText = this.tickerText.toString();
1332 }
Daniel Sandler1a497d32013-04-18 14:52:45 -04001333 if (heavy && this.tickerView != null) {
Joe Onorato46439ce2010-11-19 13:56:21 -08001334 that.tickerView = this.tickerView.clone();
Joe Onoratoef1e7762010-09-17 18:38:38 -04001335 }
Daniel Sandler1a497d32013-04-18 14:52:45 -04001336 if (heavy && this.contentView != null) {
Joe Onorato18e69df2010-05-17 22:26:12 -07001337 that.contentView = this.contentView.clone();
1338 }
Daniel Sandler1a497d32013-04-18 14:52:45 -04001339 if (heavy && this.largeIcon != null) {
Joe Onorato561d3852010-11-20 18:09:34 -08001340 that.largeIcon = Bitmap.createBitmap(this.largeIcon);
1341 }
Jozef BABJAKa8b91832011-02-22 08:05:08 +01001342 that.iconLevel = this.iconLevel;
Joe Onorato18e69df2010-05-17 22:26:12 -07001343 that.sound = this.sound; // android.net.Uri is immutable
1344 that.audioStreamType = this.audioStreamType;
John Spurlockc0650f022014-07-19 13:22:39 -04001345 if (this.audioAttributes != null) {
1346 that.audioAttributes = new AudioAttributes.Builder(this.audioAttributes).build();
1347 }
Joe Onorato18e69df2010-05-17 22:26:12 -07001348
1349 final long[] vibrate = this.vibrate;
1350 if (vibrate != null) {
1351 final int N = vibrate.length;
1352 final long[] vib = that.vibrate = new long[N];
1353 System.arraycopy(vibrate, 0, vib, 0, N);
1354 }
1355
1356 that.ledARGB = this.ledARGB;
1357 that.ledOnMS = this.ledOnMS;
1358 that.ledOffMS = this.ledOffMS;
1359 that.defaults = this.defaults;
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001360
Joe Onorato18e69df2010-05-17 22:26:12 -07001361 that.flags = this.flags;
1362
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001363 that.priority = this.priority;
Joe Malin8d40d042012-11-05 11:36:40 -08001364
John Spurlockfd7f1e02014-03-18 16:41:57 -04001365 that.category = this.category;
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001366
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001367 that.mGroupKey = this.mGroupKey;
1368
1369 that.mSortKey = this.mSortKey;
1370
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001371 if (this.extras != null) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04001372 try {
1373 that.extras = new Bundle(this.extras);
1374 // will unparcel
1375 that.extras.size();
1376 } catch (BadParcelableException e) {
1377 Log.e(TAG, "could not unparcel extras from notification: " + this, e);
1378 that.extras = null;
1379 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001380 }
1381
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001382 if (this.actions != null) {
1383 that.actions = new Action[this.actions.length];
1384 for(int i=0; i<this.actions.length; i++) {
1385 that.actions[i] = this.actions[i].clone();
1386 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001387 }
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001388
Daniel Sandler1a497d32013-04-18 14:52:45 -04001389 if (heavy && this.bigContentView != null) {
Daniel Sandlerf3b73432012-03-27 15:01:25 -04001390 that.bigContentView = this.bigContentView.clone();
1391 }
Daniel Sandler1a497d32013-04-18 14:52:45 -04001392
Chris Wren8fd39ec2014-02-27 17:43:26 -05001393 if (heavy && this.headsUpContentView != null) {
1394 that.headsUpContentView = this.headsUpContentView.clone();
1395 }
1396
Dan Sandler0bf2ed82013-12-21 23:33:41 -06001397 that.visibility = this.visibility;
1398
1399 if (this.publicVersion != null) {
1400 that.publicVersion = new Notification();
1401 this.publicVersion.cloneInto(that.publicVersion, heavy);
1402 }
1403
Dan Sandler26e81cf2014-05-06 10:01:27 -04001404 that.color = this.color;
1405
Daniel Sandler1a497d32013-04-18 14:52:45 -04001406 if (!heavy) {
1407 that.lightenPayload(); // will clean out extras
1408 }
1409 }
1410
1411 /**
1412 * Removes heavyweight parts of the Notification object for archival or for sending to
1413 * listeners when the full contents are not necessary.
1414 * @hide
1415 */
1416 public final void lightenPayload() {
1417 tickerView = null;
1418 contentView = null;
1419 bigContentView = null;
Chris Wren8fd39ec2014-02-27 17:43:26 -05001420 headsUpContentView = null;
Daniel Sandler1a497d32013-04-18 14:52:45 -04001421 largeIcon = null;
1422 if (extras != null) {
1423 extras.remove(Notification.EXTRA_LARGE_ICON);
1424 extras.remove(Notification.EXTRA_LARGE_ICON_BIG);
1425 extras.remove(Notification.EXTRA_PICTURE);
1426 }
Joe Onorato18e69df2010-05-17 22:26:12 -07001427 }
1428
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04001429 /**
1430 * Make sure this CharSequence is safe to put into a bundle, which basically
1431 * means it had better not be some custom Parcelable implementation.
1432 * @hide
1433 */
1434 public static CharSequence safeCharSequence(CharSequence cs) {
1435 if (cs instanceof Parcelable) {
1436 Log.e(TAG, "warning: " + cs.getClass().getCanonicalName()
1437 + " instance is a custom Parcelable and not allowed in Notification");
1438 return cs.toString();
1439 }
1440
1441 return cs;
1442 }
1443
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001444 public int describeContents() {
1445 return 0;
1446 }
1447
1448 /**
1449 * Flatten this notification from a parcel.
1450 */
1451 public void writeToParcel(Parcel parcel, int flags)
1452 {
1453 parcel.writeInt(1);
1454
1455 parcel.writeLong(when);
1456 parcel.writeInt(icon);
1457 parcel.writeInt(number);
1458 if (contentIntent != null) {
1459 parcel.writeInt(1);
1460 contentIntent.writeToParcel(parcel, 0);
1461 } else {
1462 parcel.writeInt(0);
1463 }
1464 if (deleteIntent != null) {
1465 parcel.writeInt(1);
1466 deleteIntent.writeToParcel(parcel, 0);
1467 } else {
1468 parcel.writeInt(0);
1469 }
1470 if (tickerText != null) {
1471 parcel.writeInt(1);
1472 TextUtils.writeToParcel(tickerText, parcel, flags);
1473 } else {
1474 parcel.writeInt(0);
1475 }
Joe Onorato46439ce2010-11-19 13:56:21 -08001476 if (tickerView != null) {
Joe Onoratoef1e7762010-09-17 18:38:38 -04001477 parcel.writeInt(1);
Joe Onorato46439ce2010-11-19 13:56:21 -08001478 tickerView.writeToParcel(parcel, 0);
Joe Onoratoef1e7762010-09-17 18:38:38 -04001479 } else {
1480 parcel.writeInt(0);
1481 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001482 if (contentView != null) {
1483 parcel.writeInt(1);
1484 contentView.writeToParcel(parcel, 0);
1485 } else {
1486 parcel.writeInt(0);
1487 }
Joe Onorato561d3852010-11-20 18:09:34 -08001488 if (largeIcon != null) {
1489 parcel.writeInt(1);
1490 largeIcon.writeToParcel(parcel, 0);
1491 } else {
1492 parcel.writeInt(0);
1493 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001494
1495 parcel.writeInt(defaults);
1496 parcel.writeInt(this.flags);
1497
1498 if (sound != null) {
1499 parcel.writeInt(1);
1500 sound.writeToParcel(parcel, 0);
1501 } else {
1502 parcel.writeInt(0);
1503 }
1504 parcel.writeInt(audioStreamType);
John Spurlockc0650f022014-07-19 13:22:39 -04001505
1506 if (audioAttributes != null) {
1507 parcel.writeInt(1);
1508 audioAttributes.writeToParcel(parcel, 0);
1509 } else {
1510 parcel.writeInt(0);
1511 }
1512
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001513 parcel.writeLongArray(vibrate);
1514 parcel.writeInt(ledARGB);
1515 parcel.writeInt(ledOnMS);
1516 parcel.writeInt(ledOffMS);
1517 parcel.writeInt(iconLevel);
Daniel Sandlere46cbd32010-06-17 10:35:26 -04001518
1519 if (fullScreenIntent != null) {
1520 parcel.writeInt(1);
1521 fullScreenIntent.writeToParcel(parcel, 0);
1522 } else {
1523 parcel.writeInt(0);
1524 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001525
1526 parcel.writeInt(priority);
Joe Malin8d40d042012-11-05 11:36:40 -08001527
John Spurlockfd7f1e02014-03-18 16:41:57 -04001528 parcel.writeString(category);
Joe Malin8d40d042012-11-05 11:36:40 -08001529
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001530 parcel.writeString(mGroupKey);
1531
1532 parcel.writeString(mSortKey);
1533
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001534 parcel.writeBundle(extras); // null ok
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001535
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001536 parcel.writeTypedArray(actions, 0); // null ok
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001537
Daniel Sandlerf3b73432012-03-27 15:01:25 -04001538 if (bigContentView != null) {
1539 parcel.writeInt(1);
1540 bigContentView.writeToParcel(parcel, 0);
1541 } else {
1542 parcel.writeInt(0);
1543 }
Dan Sandler0bf2ed82013-12-21 23:33:41 -06001544
Chris Wren8fd39ec2014-02-27 17:43:26 -05001545 if (headsUpContentView != null) {
1546 parcel.writeInt(1);
1547 headsUpContentView.writeToParcel(parcel, 0);
1548 } else {
1549 parcel.writeInt(0);
1550 }
1551
Dan Sandler0bf2ed82013-12-21 23:33:41 -06001552 parcel.writeInt(visibility);
1553
1554 if (publicVersion != null) {
1555 parcel.writeInt(1);
1556 publicVersion.writeToParcel(parcel, 0);
1557 } else {
1558 parcel.writeInt(0);
1559 }
Dan Sandler26e81cf2014-05-06 10:01:27 -04001560
1561 parcel.writeInt(color);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001562 }
1563
1564 /**
1565 * Parcelable.Creator that instantiates Notification objects
1566 */
1567 public static final Parcelable.Creator<Notification> CREATOR
1568 = new Parcelable.Creator<Notification>()
1569 {
1570 public Notification createFromParcel(Parcel parcel)
1571 {
1572 return new Notification(parcel);
1573 }
1574
1575 public Notification[] newArray(int size)
1576 {
1577 return new Notification[size];
1578 }
1579 };
1580
1581 /**
1582 * Sets the {@link #contentView} field to be a view with the standard "Latest Event"
1583 * layout.
1584 *
1585 * <p>Uses the {@link #icon} and {@link #when} fields to set the icon and time fields
1586 * in the view.</p>
1587 * @param context The context for your application / activity.
1588 * @param contentTitle The title that goes in the expanded entry.
1589 * @param contentText The text that goes in the expanded entry.
1590 * @param contentIntent The intent to launch when the user clicks the expanded notification.
1591 * If this is an activity, it must include the
1592 * {@link android.content.Intent#FLAG_ACTIVITY_NEW_TASK} flag, which requires
Scott Main7aee61f2011-02-08 11:25:01 -08001593 * that you take care of task management as described in the
1594 * <a href="{@docRoot}guide/topics/fundamentals/tasks-and-back-stack.html">Tasks and Back
1595 * Stack</a> document.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001596 *
Joe Onorato46439ce2010-11-19 13:56:21 -08001597 * @deprecated Use {@link Builder} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001598 */
Joe Onorato46439ce2010-11-19 13:56:21 -08001599 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001600 public void setLatestEventInfo(Context context,
1601 CharSequence contentTitle, CharSequence contentText, PendingIntent contentIntent) {
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001602 Notification.Builder builder = new Notification.Builder(context);
1603
1604 // First, ensure that key pieces of information that may have been set directly
1605 // are preserved
1606 builder.setWhen(this.when);
1607 builder.setSmallIcon(this.icon);
1608 builder.setPriority(this.priority);
1609 builder.setTicker(this.tickerText);
1610 builder.setNumber(this.number);
Selim Cinek255dd042014-08-19 22:29:02 +02001611 builder.setColor(this.color);
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001612 builder.mFlags = this.flags;
1613 builder.setSound(this.sound, this.audioStreamType);
1614 builder.setDefaults(this.defaults);
1615 builder.setVibrate(this.vibrate);
1616
1617 // now apply the latestEventInfo fields
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001618 if (contentTitle != null) {
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001619 builder.setContentTitle(contentTitle);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001620 }
1621 if (contentText != null) {
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001622 builder.setContentText(contentText);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001623 }
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001624 builder.setContentIntent(contentIntent);
1625 builder.buildInto(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001626 }
1627
1628 @Override
1629 public String toString() {
1630 StringBuilder sb = new StringBuilder();
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001631 sb.append("Notification(pri=");
1632 sb.append(priority);
1633 sb.append(" contentView=");
Joe Onoratoc9596d62011-01-12 17:03:11 -08001634 if (contentView != null) {
1635 sb.append(contentView.getPackage());
1636 sb.append("/0x");
1637 sb.append(Integer.toHexString(contentView.getLayoutId()));
1638 } else {
1639 sb.append("null");
1640 }
1641 sb.append(" vibrate=");
Daniel Sandler6738eee2012-11-16 12:03:32 -05001642 if ((this.defaults & DEFAULT_VIBRATE) != 0) {
1643 sb.append("default");
1644 } else if (this.vibrate != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001645 int N = this.vibrate.length-1;
1646 sb.append("[");
1647 for (int i=0; i<N; i++) {
1648 sb.append(this.vibrate[i]);
1649 sb.append(',');
1650 }
Simon Schoar8cf97d92009-06-10 22:08:37 +02001651 if (N != -1) {
1652 sb.append(this.vibrate[N]);
1653 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001654 sb.append("]");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001655 } else {
1656 sb.append("null");
1657 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001658 sb.append(" sound=");
Daniel Sandler6738eee2012-11-16 12:03:32 -05001659 if ((this.defaults & DEFAULT_SOUND) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001660 sb.append("default");
Daniel Sandler6738eee2012-11-16 12:03:32 -05001661 } else if (this.sound != null) {
1662 sb.append(this.sound.toString());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001663 } else {
1664 sb.append("null");
1665 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001666 sb.append(" defaults=0x");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001667 sb.append(Integer.toHexString(this.defaults));
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001668 sb.append(" flags=0x");
Daniel Sandlere46cbd32010-06-17 10:35:26 -04001669 sb.append(Integer.toHexString(this.flags));
Dan Sandler26e81cf2014-05-06 10:01:27 -04001670 sb.append(String.format(" color=0x%08x", this.color));
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001671 if (this.category != null) {
1672 sb.append(" category=");
1673 sb.append(this.category);
1674 }
1675 if (this.mGroupKey != null) {
1676 sb.append(" groupKey=");
1677 sb.append(this.mGroupKey);
1678 }
1679 if (this.mSortKey != null) {
1680 sb.append(" sortKey=");
1681 sb.append(this.mSortKey);
1682 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001683 if (actions != null) {
Dan Sandler1b718782014-07-18 12:43:45 -04001684 sb.append(" actions=");
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001685 sb.append(actions.length);
Dan Sandler1b718782014-07-18 12:43:45 -04001686 }
1687 sb.append(" vis=");
1688 sb.append(visibilityToString(this.visibility));
1689 if (this.publicVersion != null) {
1690 sb.append(" publicVersion=");
1691 sb.append(publicVersion.toString());
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001692 }
1693 sb.append(")");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001694 return sb.toString();
1695 }
Joe Onorato46439ce2010-11-19 13:56:21 -08001696
Dan Sandler1b718782014-07-18 12:43:45 -04001697 /**
1698 * {@hide}
1699 */
1700 public static String visibilityToString(int vis) {
1701 switch (vis) {
1702 case VISIBILITY_PRIVATE:
1703 return "PRIVATE";
1704 case VISIBILITY_PUBLIC:
1705 return "PUBLIC";
1706 case VISIBILITY_SECRET:
1707 return "SECRET";
1708 default:
1709 return "UNKNOWN(" + String.valueOf(vis) + ")";
1710 }
1711 }
1712
Joe Onoratocb109a02011-01-18 17:57:41 -08001713 /**
Christoph Studer4600f9b2014-07-22 22:44:43 +02001714 * @hide
1715 */
1716 public boolean isValid() {
1717 // Would like to check for icon!=0 here, too, but NotificationManagerService accepts that
1718 // for legacy reasons.
1719 return contentView != null || extras.getBoolean(Builder.EXTRA_REBUILD_CONTENT_VIEW);
1720 }
1721
1722 /**
Christoph Studerc8db24b2014-07-25 17:50:30 +02001723 * @hide
1724 */
1725 public boolean isGroupSummary() {
1726 return mGroupKey != null && (flags & FLAG_GROUP_SUMMARY) != 0;
1727 }
1728
1729 /**
1730 * @hide
1731 */
1732 public boolean isGroupChild() {
1733 return mGroupKey != null && (flags & FLAG_GROUP_SUMMARY) == 0;
1734 }
1735
1736 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001737 * Builder class for {@link Notification} objects.
Joe Malin8d40d042012-11-05 11:36:40 -08001738 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001739 * Provides a convenient way to set the various fields of a {@link Notification} and generate
Scott Main183bf112012-08-13 19:12:13 -07001740 * content views using the platform's notification layout template. If your app supports
1741 * versions of Android as old as API level 4, you can instead use
1742 * {@link android.support.v4.app.NotificationCompat.Builder NotificationCompat.Builder},
1743 * available in the <a href="{@docRoot}tools/extras/support-library.html">Android Support
1744 * library</a>.
Joe Malin8d40d042012-11-05 11:36:40 -08001745 *
Scott Main183bf112012-08-13 19:12:13 -07001746 * <p>Example:
Joe Malin8d40d042012-11-05 11:36:40 -08001747 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001748 * <pre class="prettyprint">
Scott Main183bf112012-08-13 19:12:13 -07001749 * Notification noti = new Notification.Builder(mContext)
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001750 * .setContentTitle(&quot;New mail from &quot; + sender.toString())
1751 * .setContentText(subject)
1752 * .setSmallIcon(R.drawable.new_mail)
1753 * .setLargeIcon(aBitmap)
Chris Wrenfbd96ba2012-05-01 12:03:58 -04001754 * .build();
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001755 * </pre>
Joe Onoratocb109a02011-01-18 17:57:41 -08001756 */
Joe Onorato46439ce2010-11-19 13:56:21 -08001757 public static class Builder {
Daniel Sandler602ad1c2012-06-12 16:06:27 -04001758 private static final int MAX_ACTION_BUTTONS = 3;
Jorim Jaggi445d3c02014-08-19 22:33:42 +02001759 private static final float LARGE_TEXT_SCALE = 1.3f;
Daniel Sandler8680bf82012-05-15 16:52:52 -04001760
Christoph Studer4600f9b2014-07-22 22:44:43 +02001761 /**
1762 * @hide
1763 */
1764 public static final String EXTRA_NEEDS_REBUILD = "android.rebuild";
1765
1766 /**
1767 * @hide
1768 */
1769 public static final String EXTRA_REBUILD_LARGE_ICON = "android.rebuild.largeIcon";
1770 /**
1771 * @hide
1772 */
1773 public static final String EXTRA_REBUILD_CONTENT_VIEW = "android.rebuild.contentView";
1774 /**
1775 * @hide
1776 */
1777 public static final String EXTRA_REBUILD_CONTENT_VIEW_ACTION_COUNT =
1778 "android.rebuild.contentViewActionCount";
1779 /**
1780 * @hide
1781 */
1782 public static final String EXTRA_REBUILD_BIG_CONTENT_VIEW
1783 = "android.rebuild.bigView";
1784 /**
1785 * @hide
1786 */
1787 public static final String EXTRA_REBUILD_BIG_CONTENT_VIEW_ACTION_COUNT
1788 = "android.rebuild.bigViewActionCount";
1789 /**
1790 * @hide
1791 */
1792 public static final String EXTRA_REBUILD_HEADS_UP_CONTENT_VIEW
1793 = "android.rebuild.hudView";
1794 /**
1795 * @hide
1796 */
1797 public static final String EXTRA_REBUILD_HEADS_UP_CONTENT_VIEW_ACTION_COUNT
1798 = "android.rebuild.hudViewActionCount";
1799
1800 /**
1801 * The package name of the context used to create the notification via a Builder.
1802 */
1803 private static final String EXTRA_REBUILD_CONTEXT_PACKAGE =
1804 "android.rebuild.contextPackage";
1805
1806 // Whether to enable stripping (at post time) & rebuilding (at listener receive time) of
1807 // memory intensive resources.
1808 private static final boolean STRIP_AND_REBUILD = true;
1809
Joe Onorato46439ce2010-11-19 13:56:21 -08001810 private Context mContext;
1811
1812 private long mWhen;
1813 private int mSmallIcon;
1814 private int mSmallIconLevel;
Joe Onorato8595a3d2010-11-19 18:12:07 -08001815 private int mNumber;
Joe Onorato46439ce2010-11-19 13:56:21 -08001816 private CharSequence mContentTitle;
1817 private CharSequence mContentText;
1818 private CharSequence mContentInfo;
Daniel Sandlerf3b73432012-03-27 15:01:25 -04001819 private CharSequence mSubText;
Joe Onorato46439ce2010-11-19 13:56:21 -08001820 private PendingIntent mContentIntent;
1821 private RemoteViews mContentView;
1822 private PendingIntent mDeleteIntent;
1823 private PendingIntent mFullScreenIntent;
1824 private CharSequence mTickerText;
1825 private RemoteViews mTickerView;
1826 private Bitmap mLargeIcon;
1827 private Uri mSound;
1828 private int mAudioStreamType;
John Spurlockc0650f022014-07-19 13:22:39 -04001829 private AudioAttributes mAudioAttributes;
Joe Onorato46439ce2010-11-19 13:56:21 -08001830 private long[] mVibrate;
1831 private int mLedArgb;
1832 private int mLedOnMs;
1833 private int mLedOffMs;
1834 private int mDefaults;
1835 private int mFlags;
Jeff Sharkey1c400132011-08-05 14:50:13 -07001836 private int mProgressMax;
1837 private int mProgress;
1838 private boolean mProgressIndeterminate;
John Spurlockfd7f1e02014-03-18 16:41:57 -04001839 private String mCategory;
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001840 private String mGroupKey;
1841 private String mSortKey;
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001842 private Bundle mExtras;
1843 private int mPriority;
Daniel Sandler8680bf82012-05-15 16:52:52 -04001844 private ArrayList<Action> mActions = new ArrayList<Action>(MAX_ACTION_BUTTONS);
Daniel Sandlera2985ed2012-04-03 16:42:00 -04001845 private boolean mUseChronometer;
Chris Wrenfbd96ba2012-05-01 12:03:58 -04001846 private Style mStyle;
Daniel Sandler0c890492012-09-12 17:23:10 -07001847 private boolean mShowWhen = true;
Dan Sandler0bf2ed82013-12-21 23:33:41 -06001848 private int mVisibility = VISIBILITY_PRIVATE;
1849 private Notification mPublicVersion = null;
Dan Sandler26e81cf2014-05-06 10:01:27 -04001850 private final NotificationColorUtil mColorUtil;
Chris Wrendde75302014-03-26 17:24:15 -04001851 private ArrayList<String> mPeople;
Dan Sandler26e81cf2014-05-06 10:01:27 -04001852 private int mColor = COLOR_DEFAULT;
Christoph Studer7ac80e62014-08-04 16:01:57 +02001853
1854 /**
1855 * The user that built the notification originally.
1856 */
1857 private int mOriginatingUserId;
Christoph Studer4600f9b2014-07-22 22:44:43 +02001858
1859 /**
1860 * Contains extras related to rebuilding during the build phase.
1861 */
1862 private Bundle mRebuildBundle = new Bundle();
1863 /**
1864 * Contains the notification to rebuild when this Builder is in "rebuild" mode.
1865 * Null otherwise.
1866 */
1867 private Notification mRebuildNotification = null;
1868
Joe Onoratocb109a02011-01-18 17:57:41 -08001869 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001870 * Constructs a new Builder with the defaults:
Joe Onoratocb109a02011-01-18 17:57:41 -08001871 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001872
1873 * <table>
1874 * <tr><th align=right>priority</th>
1875 * <td>{@link #PRIORITY_DEFAULT}</td></tr>
1876 * <tr><th align=right>when</th>
1877 * <td>now ({@link System#currentTimeMillis()})</td></tr>
1878 * <tr><th align=right>audio stream</th>
1879 * <td>{@link #STREAM_DEFAULT}</td></tr>
1880 * </table>
Joe Onoratocb109a02011-01-18 17:57:41 -08001881 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001882
1883 * @param context
1884 * A {@link Context} that will be used by the Builder to construct the
1885 * RemoteViews. The Context will not be held past the lifetime of this Builder
1886 * object.
Joe Onoratocb109a02011-01-18 17:57:41 -08001887 */
Joe Onorato46439ce2010-11-19 13:56:21 -08001888 public Builder(Context context) {
Adam Powellbf06fa02014-05-30 12:32:18 -07001889 /*
1890 * Important compatibility note!
1891 * Some apps out in the wild create a Notification.Builder in their Activity subclass
1892 * constructor for later use. At this point Activities - themselves subclasses of
1893 * ContextWrapper - do not have their inner Context populated yet. This means that
1894 * any calls to Context methods from within this constructor can cause NPEs in existing
1895 * apps. Any data populated from mContext should therefore be populated lazily to
1896 * preserve compatibility.
1897 */
Joe Onorato46439ce2010-11-19 13:56:21 -08001898 mContext = context;
Andy Stadler110988c2010-12-03 14:29:16 -08001899
1900 // Set defaults to match the defaults of a Notification
Joe Onorato46439ce2010-11-19 13:56:21 -08001901 mWhen = System.currentTimeMillis();
Andy Stadler110988c2010-12-03 14:29:16 -08001902 mAudioStreamType = STREAM_DEFAULT;
John Spurlockc0650f022014-07-19 13:22:39 -04001903 mAudioAttributes = AUDIO_ATTRIBUTES_DEFAULT;
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001904 mPriority = PRIORITY_DEFAULT;
Chris Wrendde75302014-03-26 17:24:15 -04001905 mPeople = new ArrayList<String>();
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01001906
Dan Sandleraa21e392014-08-06 19:26:11 +00001907 mColorUtil = NotificationColorUtil.getInstance();
Joe Onorato46439ce2010-11-19 13:56:21 -08001908 }
1909
Joe Onoratocb109a02011-01-18 17:57:41 -08001910 /**
Christoph Studer4600f9b2014-07-22 22:44:43 +02001911 * Creates a Builder for rebuilding the given Notification.
1912 * <p>
1913 * Call {@link #rebuild()} to retrieve the rebuilt version of 'n'.
1914 */
1915 private Builder(Context context, Notification n) {
1916 this(context);
1917 mRebuildNotification = n;
1918 restoreFromNotification(n);
1919
1920 Style style = null;
1921 Bundle extras = n.extras;
1922 String templateClass = extras.getString(EXTRA_TEMPLATE);
1923 if (!TextUtils.isEmpty(templateClass)) {
1924 Class<? extends Style> styleClass = getNotificationStyleClass(templateClass);
1925 if (styleClass == null) {
1926 Log.d(TAG, "Unknown style class: " + styleClass);
1927 return;
1928 }
1929
1930 try {
1931 Constructor<? extends Style> constructor = styleClass.getConstructor();
1932 style = constructor.newInstance();
1933 style.restoreFromExtras(extras);
1934 } catch (Throwable t) {
1935 Log.e(TAG, "Could not create Style", t);
1936 return;
1937 }
1938 }
1939 if (style != null) {
1940 setStyle(style);
1941 }
1942 }
1943
1944 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001945 * Add a timestamp pertaining to the notification (usually the time the event occurred).
Daniel Sandler0c890492012-09-12 17:23:10 -07001946 * It will be shown in the notification content view by default; use
Griff Hazen50c11652014-05-16 09:46:31 -07001947 * {@link #setShowWhen(boolean) setShowWhen} to control this.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001948 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001949 * @see Notification#when
Joe Onoratocb109a02011-01-18 17:57:41 -08001950 */
Joe Onorato46439ce2010-11-19 13:56:21 -08001951 public Builder setWhen(long when) {
1952 mWhen = when;
1953 return this;
1954 }
1955
Joe Onoratocb109a02011-01-18 17:57:41 -08001956 /**
Griff Hazen50c11652014-05-16 09:46:31 -07001957 * Control whether the timestamp set with {@link #setWhen(long) setWhen} is shown
Daniel Sandler0c890492012-09-12 17:23:10 -07001958 * in the content view.
1959 */
1960 public Builder setShowWhen(boolean show) {
1961 mShowWhen = show;
1962 return this;
1963 }
1964
1965 /**
Daniel Sandlerd33b8032012-05-10 11:41:48 -04001966 * Show the {@link Notification#when} field as a stopwatch.
Joe Malin8d40d042012-11-05 11:36:40 -08001967 *
1968 * Instead of presenting <code>when</code> as a timestamp, the notification will show an
Daniel Sandlerd33b8032012-05-10 11:41:48 -04001969 * automatically updating display of the minutes and seconds since <code>when</code>.
Daniel Sandlera2985ed2012-04-03 16:42:00 -04001970 *
Daniel Sandlerd33b8032012-05-10 11:41:48 -04001971 * Useful when showing an elapsed time (like an ongoing phone call).
1972 *
1973 * @see android.widget.Chronometer
Daniel Sandlera2985ed2012-04-03 16:42:00 -04001974 * @see Notification#when
1975 */
1976 public Builder setUsesChronometer(boolean b) {
1977 mUseChronometer = b;
1978 return this;
1979 }
1980
1981 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001982 * Set the small icon resource, which will be used to represent the notification in the
1983 * status bar.
Joe Onoratocb109a02011-01-18 17:57:41 -08001984 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001985
1986 * The platform template for the expanded view will draw this icon in the left, unless a
1987 * {@link #setLargeIcon(Bitmap) large icon} has also been specified, in which case the small
1988 * icon will be moved to the right-hand side.
1989 *
1990
1991 * @param icon
1992 * A resource ID in the application's package of the drawable to use.
1993 * @see Notification#icon
Joe Onoratocb109a02011-01-18 17:57:41 -08001994 */
Joe Onorato46439ce2010-11-19 13:56:21 -08001995 public Builder setSmallIcon(int icon) {
1996 mSmallIcon = icon;
1997 return this;
1998 }
1999
Joe Onoratocb109a02011-01-18 17:57:41 -08002000 /**
2001 * A variant of {@link #setSmallIcon(int) setSmallIcon(int)} that takes an additional
2002 * level parameter for when the icon is a {@link android.graphics.drawable.LevelListDrawable
2003 * LevelListDrawable}.
2004 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002005 * @param icon A resource ID in the application's package of the drawable to use.
Joe Onoratocb109a02011-01-18 17:57:41 -08002006 * @param level The level to use for the icon.
2007 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002008 * @see Notification#icon
2009 * @see Notification#iconLevel
Joe Onoratocb109a02011-01-18 17:57:41 -08002010 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002011 public Builder setSmallIcon(int icon, int level) {
2012 mSmallIcon = icon;
2013 mSmallIconLevel = level;
2014 return this;
2015 }
2016
Joe Onoratocb109a02011-01-18 17:57:41 -08002017 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002018 * Set the first line of text in the platform notification template.
Joe Onoratocb109a02011-01-18 17:57:41 -08002019 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002020 public Builder setContentTitle(CharSequence title) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04002021 mContentTitle = safeCharSequence(title);
Joe Onorato46439ce2010-11-19 13:56:21 -08002022 return this;
2023 }
2024
Joe Onoratocb109a02011-01-18 17:57:41 -08002025 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002026 * Set the second line of text in the platform notification template.
Joe Onoratocb109a02011-01-18 17:57:41 -08002027 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002028 public Builder setContentText(CharSequence text) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04002029 mContentText = safeCharSequence(text);
Joe Onorato46439ce2010-11-19 13:56:21 -08002030 return this;
2031 }
2032
Joe Onoratocb109a02011-01-18 17:57:41 -08002033 /**
Joe Malin8d40d042012-11-05 11:36:40 -08002034 * Set the third line of text in the platform notification template.
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04002035 * Don't use if you're also using {@link #setProgress(int, int, boolean)}; they occupy the
2036 * same location in the standard template.
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002037 */
2038 public Builder setSubText(CharSequence text) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04002039 mSubText = safeCharSequence(text);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002040 return this;
2041 }
2042
2043 /**
Joe Onoratocb109a02011-01-18 17:57:41 -08002044 * Set the large number at the right-hand side of the notification. This is
2045 * equivalent to setContentInfo, although it might show the number in a different
2046 * font size for readability.
2047 */
Joe Onorato8595a3d2010-11-19 18:12:07 -08002048 public Builder setNumber(int number) {
2049 mNumber = number;
2050 return this;
2051 }
2052
Joe Onoratocb109a02011-01-18 17:57:41 -08002053 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002054 * A small piece of additional information pertaining to this notification.
2055 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002056 * The platform template will draw this on the last line of the notification, at the far
2057 * right (to the right of a smallIcon if it has been placed there).
Joe Onoratocb109a02011-01-18 17:57:41 -08002058 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002059 public Builder setContentInfo(CharSequence info) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04002060 mContentInfo = safeCharSequence(info);
Joe Onorato46439ce2010-11-19 13:56:21 -08002061 return this;
2062 }
2063
Joe Onoratocb109a02011-01-18 17:57:41 -08002064 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002065 * Set the progress this notification represents.
2066 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002067 * The platform template will represent this using a {@link ProgressBar}.
Jeff Sharkey1c400132011-08-05 14:50:13 -07002068 */
2069 public Builder setProgress(int max, int progress, boolean indeterminate) {
2070 mProgressMax = max;
2071 mProgress = progress;
2072 mProgressIndeterminate = indeterminate;
2073 return this;
2074 }
2075
2076 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002077 * Supply a custom RemoteViews to use instead of the platform template.
2078 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002079 * @see Notification#contentView
Joe Onoratocb109a02011-01-18 17:57:41 -08002080 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002081 public Builder setContent(RemoteViews views) {
2082 mContentView = views;
2083 return this;
2084 }
2085
Joe Onoratocb109a02011-01-18 17:57:41 -08002086 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002087 * Supply a {@link PendingIntent} to be sent when the notification is clicked.
2088 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002089 * As of {@link android.os.Build.VERSION_CODES#HONEYCOMB}, if this field is unset and you
2090 * have specified a custom RemoteViews with {@link #setContent(RemoteViews)}, you can use
2091 * {@link RemoteViews#setOnClickPendingIntent RemoteViews.setOnClickPendingIntent(int,PendingIntent)}
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002092 * to assign PendingIntents to individual views in that custom layout (i.e., to create
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002093 * clickable buttons inside the notification view).
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002094 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002095 * @see Notification#contentIntent Notification.contentIntent
Joe Onoratocb109a02011-01-18 17:57:41 -08002096 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002097 public Builder setContentIntent(PendingIntent intent) {
2098 mContentIntent = intent;
2099 return this;
2100 }
2101
Joe Onoratocb109a02011-01-18 17:57:41 -08002102 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002103 * Supply a {@link PendingIntent} to send when the notification is cleared explicitly by the user.
2104 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002105 * @see Notification#deleteIntent
Joe Onoratocb109a02011-01-18 17:57:41 -08002106 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002107 public Builder setDeleteIntent(PendingIntent intent) {
2108 mDeleteIntent = intent;
2109 return this;
2110 }
2111
Joe Onoratocb109a02011-01-18 17:57:41 -08002112 /**
2113 * An intent to launch instead of posting the notification to the status bar.
2114 * Only for use with extremely high-priority notifications demanding the user's
2115 * <strong>immediate</strong> attention, such as an incoming phone call or
2116 * alarm clock that the user has explicitly set to a particular time.
2117 * If this facility is used for something else, please give the user an option
2118 * to turn it off and use a normal notification, as this can be extremely
2119 * disruptive.
2120 *
Chris Wren47c20a12014-06-18 17:27:29 -04002121 * <p>
2122 * The system UI may choose to display a heads-up notification, instead of
2123 * launching this intent, while the user is using the device.
2124 * </p>
2125 *
Joe Onoratocb109a02011-01-18 17:57:41 -08002126 * @param intent The pending intent to launch.
2127 * @param highPriority Passing true will cause this notification to be sent
2128 * even if other notifications are suppressed.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002129 *
2130 * @see Notification#fullScreenIntent
Joe Onoratocb109a02011-01-18 17:57:41 -08002131 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002132 public Builder setFullScreenIntent(PendingIntent intent, boolean highPriority) {
2133 mFullScreenIntent = intent;
2134 setFlag(FLAG_HIGH_PRIORITY, highPriority);
2135 return this;
2136 }
2137
Joe Onoratocb109a02011-01-18 17:57:41 -08002138 /**
Dan Sandler5fcdf6e2014-07-18 11:31:15 -04002139 * Set the "ticker" text which is sent to accessibility services.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002140 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002141 * @see Notification#tickerText
Joe Onoratocb109a02011-01-18 17:57:41 -08002142 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002143 public Builder setTicker(CharSequence tickerText) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04002144 mTickerText = safeCharSequence(tickerText);
Joe Onorato46439ce2010-11-19 13:56:21 -08002145 return this;
2146 }
2147
Joe Onoratocb109a02011-01-18 17:57:41 -08002148 /**
Dan Sandler5fcdf6e2014-07-18 11:31:15 -04002149 * Obsolete version of {@link #setTicker(CharSequence)}.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002150 *
Joe Onoratocb109a02011-01-18 17:57:41 -08002151 */
Dan Sandler5fcdf6e2014-07-18 11:31:15 -04002152 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -08002153 public Builder setTicker(CharSequence tickerText, RemoteViews views) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04002154 mTickerText = safeCharSequence(tickerText);
Dan Sandler5fcdf6e2014-07-18 11:31:15 -04002155 mTickerView = views; // we'll save it for you anyway
Joe Onorato46439ce2010-11-19 13:56:21 -08002156 return this;
2157 }
2158
Joe Onoratocb109a02011-01-18 17:57:41 -08002159 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002160 * Add a large icon to the notification (and the ticker on some devices).
2161 *
2162 * In the platform template, this image will be shown on the left of the notification view
2163 * in place of the {@link #setSmallIcon(int) small icon} (which will move to the right side).
2164 *
2165 * @see Notification#largeIcon
Joe Onoratocb109a02011-01-18 17:57:41 -08002166 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002167 public Builder setLargeIcon(Bitmap icon) {
2168 mLargeIcon = icon;
2169 return this;
2170 }
2171
Joe Onoratocb109a02011-01-18 17:57:41 -08002172 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002173 * Set the sound to play.
2174 *
John Spurlockc0650f022014-07-19 13:22:39 -04002175 * It will be played using the {@link #AUDIO_ATTRIBUTES_DEFAULT default audio attributes}
2176 * for notifications.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002177 *
Chris Wren47c20a12014-06-18 17:27:29 -04002178 * <p>
2179 * A notification that is noisy is more likely to be presented as a heads-up notification.
2180 * </p>
2181 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002182 * @see Notification#sound
Joe Onoratocb109a02011-01-18 17:57:41 -08002183 */
Joe Onorato52f80cd2010-11-21 15:34:48 -08002184 public Builder setSound(Uri sound) {
2185 mSound = sound;
John Spurlockc0650f022014-07-19 13:22:39 -04002186 mAudioAttributes = AUDIO_ATTRIBUTES_DEFAULT;
Joe Onorato52f80cd2010-11-21 15:34:48 -08002187 return this;
2188 }
2189
Joe Onoratocb109a02011-01-18 17:57:41 -08002190 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002191 * Set the sound to play, along with a specific stream on which to play it.
Joe Onoratocb109a02011-01-18 17:57:41 -08002192 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002193 * See {@link android.media.AudioManager} for the <code>STREAM_</code> constants.
2194 *
Chris Wren47c20a12014-06-18 17:27:29 -04002195 * <p>
2196 * A notification that is noisy is more likely to be presented as a heads-up notification.
2197 * </p>
John Spurlockc0650f022014-07-19 13:22:39 -04002198 * @deprecated use {@link #setSound(Uri, AudioAttributes)} instead.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002199 * @see Notification#sound
Joe Onoratocb109a02011-01-18 17:57:41 -08002200 */
Jean-Michel Trivi81f871e2014-08-06 16:32:38 -07002201 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -08002202 public Builder setSound(Uri sound, int streamType) {
2203 mSound = sound;
2204 mAudioStreamType = streamType;
2205 return this;
2206 }
2207
Joe Onoratocb109a02011-01-18 17:57:41 -08002208 /**
John Spurlockc0650f022014-07-19 13:22:39 -04002209 * Set the sound to play, along with specific {@link AudioAttributes audio attributes} to
2210 * use during playback.
2211 *
2212 * <p>
2213 * A notification that is noisy is more likely to be presented as a heads-up notification.
2214 * </p>
2215 *
2216 * @see Notification#sound
2217 */
2218 public Builder setSound(Uri sound, AudioAttributes audioAttributes) {
2219 mSound = sound;
2220 mAudioAttributes = audioAttributes;
2221 return this;
2222 }
2223
2224 /**
Joe Onoratocb109a02011-01-18 17:57:41 -08002225 * Set the vibration pattern to use.
2226 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002227 * See {@link android.os.Vibrator#vibrate(long[], int)} for a discussion of the
2228 * <code>pattern</code> parameter.
2229 *
Chris Wren47c20a12014-06-18 17:27:29 -04002230 * <p>
2231 * A notification that vibrates is more likely to be presented as a heads-up notification.
2232 * </p>
2233 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002234 * @see Notification#vibrate
Joe Onoratocb109a02011-01-18 17:57:41 -08002235 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002236 public Builder setVibrate(long[] pattern) {
2237 mVibrate = pattern;
2238 return this;
2239 }
2240
Joe Onoratocb109a02011-01-18 17:57:41 -08002241 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002242 * Set the desired color for the indicator LED on the device, as well as the
2243 * blink duty cycle (specified in milliseconds).
2244 *
2245
2246 * Not all devices will honor all (or even any) of these values.
2247 *
2248
2249 * @see Notification#ledARGB
2250 * @see Notification#ledOnMS
2251 * @see Notification#ledOffMS
Joe Onoratocb109a02011-01-18 17:57:41 -08002252 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002253 public Builder setLights(int argb, int onMs, int offMs) {
2254 mLedArgb = argb;
2255 mLedOnMs = onMs;
2256 mLedOffMs = offMs;
Joe Onorato46439ce2010-11-19 13:56:21 -08002257 return this;
2258 }
2259
Joe Onoratocb109a02011-01-18 17:57:41 -08002260 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002261 * Set whether this is an "ongoing" notification.
Joe Onoratocb109a02011-01-18 17:57:41 -08002262 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002263
2264 * Ongoing notifications cannot be dismissed by the user, so your application or service
2265 * must take care of canceling them.
2266 *
2267
2268 * They are typically used to indicate a background task that the user is actively engaged
2269 * with (e.g., playing music) or is pending in some way and therefore occupying the device
2270 * (e.g., a file download, sync operation, active network connection).
2271 *
2272
2273 * @see Notification#FLAG_ONGOING_EVENT
2274 * @see Service#setForeground(boolean)
Joe Onoratocb109a02011-01-18 17:57:41 -08002275 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002276 public Builder setOngoing(boolean ongoing) {
2277 setFlag(FLAG_ONGOING_EVENT, ongoing);
2278 return this;
2279 }
2280
Joe Onoratocb109a02011-01-18 17:57:41 -08002281 /**
2282 * Set this flag if you would only like the sound, vibrate
2283 * and ticker to be played if the notification is not already showing.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002284 *
2285 * @see Notification#FLAG_ONLY_ALERT_ONCE
Joe Onoratocb109a02011-01-18 17:57:41 -08002286 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002287 public Builder setOnlyAlertOnce(boolean onlyAlertOnce) {
2288 setFlag(FLAG_ONLY_ALERT_ONCE, onlyAlertOnce);
2289 return this;
2290 }
2291
Joe Onoratocb109a02011-01-18 17:57:41 -08002292 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002293 * Make this notification automatically dismissed when the user touches it. The
2294 * PendingIntent set with {@link #setDeleteIntent} will be sent when this happens.
2295 *
2296 * @see Notification#FLAG_AUTO_CANCEL
Joe Onoratocb109a02011-01-18 17:57:41 -08002297 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002298 public Builder setAutoCancel(boolean autoCancel) {
Joe Onorato281d83f2011-01-04 17:13:10 -08002299 setFlag(FLAG_AUTO_CANCEL, autoCancel);
Joe Onorato46439ce2010-11-19 13:56:21 -08002300 return this;
2301 }
2302
Joe Onoratocb109a02011-01-18 17:57:41 -08002303 /**
Griff Hazendfcb0802014-02-11 12:00:00 -08002304 * Set whether or not this notification should not bridge to other devices.
2305 *
2306 * <p>Some notifications can be bridged to other devices for remote display.
2307 * This hint can be set to recommend this notification not be bridged.
2308 */
2309 public Builder setLocalOnly(boolean localOnly) {
2310 setFlag(FLAG_LOCAL_ONLY, localOnly);
2311 return this;
2312 }
2313
2314 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002315 * Set which notification properties will be inherited from system defaults.
Joe Onoratocb109a02011-01-18 17:57:41 -08002316 * <p>
2317 * The value should be one or more of the following fields combined with
2318 * bitwise-or:
2319 * {@link #DEFAULT_SOUND}, {@link #DEFAULT_VIBRATE}, {@link #DEFAULT_LIGHTS}.
2320 * <p>
2321 * For all default values, use {@link #DEFAULT_ALL}.
2322 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002323 public Builder setDefaults(int defaults) {
2324 mDefaults = defaults;
2325 return this;
2326 }
2327
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002328 /**
2329 * Set the priority of this notification.
2330 *
2331 * @see Notification#priority
2332 */
Tor Norbyed9273d62013-05-30 15:59:53 -07002333 public Builder setPriority(@Priority int pri) {
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002334 mPriority = pri;
2335 return this;
2336 }
Joe Malin8d40d042012-11-05 11:36:40 -08002337
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002338 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -04002339 * Set the notification category.
Joe Malin8d40d042012-11-05 11:36:40 -08002340 *
John Spurlockfd7f1e02014-03-18 16:41:57 -04002341 * @see Notification#category
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002342 */
John Spurlockfd7f1e02014-03-18 16:41:57 -04002343 public Builder setCategory(String category) {
2344 mCategory = category;
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002345 return this;
2346 }
2347
2348 /**
Chris Wrendde75302014-03-26 17:24:15 -04002349 * Add a person that is relevant to this notification.
2350 *
2351 * @see Notification#EXTRA_PEOPLE
2352 */
2353 public Builder addPerson(String handle) {
2354 mPeople.add(handle);
2355 return this;
2356 }
2357
2358 /**
Griff Hazen5cadc3b2014-05-20 09:55:39 -07002359 * Set this notification to be part of a group of notifications sharing the same key.
2360 * Grouped notifications may display in a cluster or stack on devices which
2361 * support such rendering.
2362 *
2363 * <p>To make this notification the summary for its group, also call
2364 * {@link #setGroupSummary}. A sort order can be specified for group members by using
2365 * {@link #setSortKey}.
2366 * @param groupKey The group key of the group.
2367 * @return this object for method chaining
2368 */
2369 public Builder setGroup(String groupKey) {
2370 mGroupKey = groupKey;
2371 return this;
2372 }
2373
2374 /**
2375 * Set this notification to be the group summary for a group of notifications.
2376 * Grouped notifications may display in a cluster or stack on devices which
2377 * support such rendering. Requires a group key also be set using {@link #setGroup}.
2378 * @param isGroupSummary Whether this notification should be a group summary.
2379 * @return this object for method chaining
2380 */
2381 public Builder setGroupSummary(boolean isGroupSummary) {
2382 setFlag(FLAG_GROUP_SUMMARY, isGroupSummary);
2383 return this;
2384 }
2385
2386 /**
2387 * Set a sort key that orders this notification among other notifications from the
2388 * same package. This can be useful if an external sort was already applied and an app
2389 * would like to preserve this. Notifications will be sorted lexicographically using this
2390 * value, although providing different priorities in addition to providing sort key may
2391 * cause this value to be ignored.
2392 *
2393 * <p>This sort key can also be used to order members of a notification group. See
Griff Hazen9e1379f2014-05-20 12:50:51 -07002394 * {@link #setGroup}.
Griff Hazen5cadc3b2014-05-20 09:55:39 -07002395 *
2396 * @see String#compareTo(String)
2397 */
2398 public Builder setSortKey(String sortKey) {
2399 mSortKey = sortKey;
2400 return this;
2401 }
2402
2403 /**
Griff Hazen720042b2014-02-24 15:46:56 -08002404 * Merge additional metadata into this notification.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002405 *
Griff Hazen720042b2014-02-24 15:46:56 -08002406 * <p>Values within the Bundle will replace existing extras values in this Builder.
2407 *
2408 * @see Notification#extras
2409 */
Griff Hazen959591e2014-05-15 22:26:18 -07002410 public Builder addExtras(Bundle extras) {
2411 if (extras != null) {
2412 if (mExtras == null) {
2413 mExtras = new Bundle(extras);
2414 } else {
2415 mExtras.putAll(extras);
2416 }
Griff Hazen720042b2014-02-24 15:46:56 -08002417 }
2418 return this;
2419 }
2420
2421 /**
2422 * Set metadata for this notification.
2423 *
2424 * <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 -04002425 * current contents are copied into the Notification each time {@link #build()} is
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002426 * called.
2427 *
Griff Hazen720042b2014-02-24 15:46:56 -08002428 * <p>Replaces any existing extras values with those from the provided Bundle.
2429 * Use {@link #addExtras} to merge in metadata instead.
2430 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002431 * @see Notification#extras
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002432 */
Griff Hazen959591e2014-05-15 22:26:18 -07002433 public Builder setExtras(Bundle extras) {
2434 mExtras = extras;
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002435 return this;
2436 }
2437
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002438 /**
Griff Hazen720042b2014-02-24 15:46:56 -08002439 * Get the current metadata Bundle used by this notification Builder.
2440 *
2441 * <p>The returned Bundle is shared with this Builder.
2442 *
2443 * <p>The current contents of this Bundle are copied into the Notification each time
2444 * {@link #build()} is called.
2445 *
2446 * @see Notification#extras
2447 */
2448 public Bundle getExtras() {
2449 if (mExtras == null) {
2450 mExtras = new Bundle();
2451 }
2452 return mExtras;
2453 }
2454
2455 /**
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002456 * Add an action to this notification. Actions are typically displayed by
2457 * the system as a button adjacent to the notification content.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04002458 * <p>
2459 * Every action must have an icon (32dp square and matching the
2460 * <a href="{@docRoot}design/style/iconography.html#action-bar">Holo
2461 * Dark action bar</a> visual style), a textual label, and a {@link PendingIntent}.
2462 * <p>
2463 * A notification in its expanded form can display up to 3 actions, from left to right in
2464 * the order they were added. Actions will not be displayed when the notification is
2465 * collapsed, however, so be sure that any essential functions may be accessed by the user
2466 * in some other way (for example, in the Activity pointed to by {@link #contentIntent}).
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002467 *
2468 * @param icon Resource ID of a drawable that represents the action.
2469 * @param title Text describing the action.
2470 * @param intent PendingIntent to be fired when the action is invoked.
2471 */
2472 public Builder addAction(int icon, CharSequence title, PendingIntent intent) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04002473 mActions.add(new Action(icon, safeCharSequence(title), intent));
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002474 return this;
2475 }
2476
Chris Wrenfbd96ba2012-05-01 12:03:58 -04002477 /**
Griff Hazen959591e2014-05-15 22:26:18 -07002478 * Add an action to this notification. Actions are typically displayed by
2479 * the system as a button adjacent to the notification content.
2480 * <p>
2481 * Every action must have an icon (32dp square and matching the
2482 * <a href="{@docRoot}design/style/iconography.html#action-bar">Holo
2483 * Dark action bar</a> visual style), a textual label, and a {@link PendingIntent}.
2484 * <p>
2485 * A notification in its expanded form can display up to 3 actions, from left to right in
2486 * the order they were added. Actions will not be displayed when the notification is
2487 * collapsed, however, so be sure that any essential functions may be accessed by the user
2488 * in some other way (for example, in the Activity pointed to by {@link #contentIntent}).
2489 *
2490 * @param action The action to add.
2491 */
2492 public Builder addAction(Action action) {
2493 mActions.add(action);
2494 return this;
2495 }
2496
2497 /**
Chris Wrenfbd96ba2012-05-01 12:03:58 -04002498 * Add a rich notification style to be applied at build time.
2499 *
2500 * @param style Object responsible for modifying the notification style.
2501 */
2502 public Builder setStyle(Style style) {
2503 if (mStyle != style) {
2504 mStyle = style;
Daniel Sandlerc08dea22012-06-28 08:35:24 -07002505 if (mStyle != null) {
2506 mStyle.setBuilder(this);
2507 }
Chris Wrenfbd96ba2012-05-01 12:03:58 -04002508 }
2509 return this;
2510 }
2511
Dan Sandler0bf2ed82013-12-21 23:33:41 -06002512 /**
2513 * Specify the value of {@link #visibility}.
Griff Hazenb720abe2014-05-20 13:15:30 -07002514 *
Dan Sandler0bf2ed82013-12-21 23:33:41 -06002515 * @param visibility One of {@link #VISIBILITY_PRIVATE} (the default),
2516 * {@link #VISIBILITY_SECRET}, or {@link #VISIBILITY_PUBLIC}.
2517 *
2518 * @return The same Builder.
2519 */
2520 public Builder setVisibility(int visibility) {
2521 mVisibility = visibility;
2522 return this;
2523 }
2524
2525 /**
2526 * Supply a replacement Notification whose contents should be shown in insecure contexts
2527 * (i.e. atop the secure lockscreen). See {@link #visibility} and {@link #VISIBILITY_PUBLIC}.
2528 * @param n A replacement notification, presumably with some or all info redacted.
2529 * @return The same Builder.
2530 */
2531 public Builder setPublicVersion(Notification n) {
2532 mPublicVersion = n;
2533 return this;
2534 }
2535
Griff Hazenb720abe2014-05-20 13:15:30 -07002536 /**
Griff Hazen5cadc3b2014-05-20 09:55:39 -07002537 * Apply an extender to this notification builder. Extenders may be used to add
2538 * metadata or change options on this builder.
2539 */
Griff Hazen61a9e862014-05-22 16:05:19 -07002540 public Builder extend(Extender extender) {
2541 extender.extend(this);
Griff Hazen5cadc3b2014-05-20 09:55:39 -07002542 return this;
2543 }
2544
Joe Onorato46439ce2010-11-19 13:56:21 -08002545 private void setFlag(int mask, boolean value) {
2546 if (value) {
2547 mFlags |= mask;
2548 } else {
2549 mFlags &= ~mask;
2550 }
2551 }
2552
Dan Sandler26e81cf2014-05-06 10:01:27 -04002553 /**
2554 * Sets {@link Notification#color}.
2555 *
2556 * @param argb The accent color to use
2557 *
2558 * @return The same Builder.
2559 */
2560 public Builder setColor(int argb) {
2561 mColor = argb;
2562 return this;
2563 }
2564
Kenny Guy8a0101b2014-05-08 23:34:12 +01002565 private Bitmap getProfileBadge() {
Christoph Studer7ac80e62014-08-04 16:01:57 +02002566 // Note: This assumes that the current user can read the profile badge of the
2567 // originating user.
Kenny Guy8a0101b2014-05-08 23:34:12 +01002568 UserManager userManager = (UserManager) mContext.getSystemService(Context.USER_SERVICE);
Svetoslavc71c42f2014-08-05 18:57:05 -07002569 Drawable badge = userManager.getBadgeForUser(new UserHandle(mOriginatingUserId), 0);
Kenny Guy8a0101b2014-05-08 23:34:12 +01002570 if (badge == null) {
2571 return null;
2572 }
2573 final int width = badge.getIntrinsicWidth();
2574 final int height = badge.getIntrinsicHeight();
2575 Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
2576 Canvas canvas = new Canvas(bitmap);
2577 badge.setBounds(0, 0, width, height);
2578 badge.draw(canvas);
2579 return bitmap;
2580 }
2581
Kenny Guy98193ea2014-07-24 19:54:37 +01002582 private boolean addProfileBadge(RemoteViews contentView, int resId) {
2583 Bitmap profileBadge = getProfileBadge();
2584
2585 contentView.setViewVisibility(R.id.profile_badge_large_template, View.GONE);
2586 contentView.setViewVisibility(R.id.profile_badge_line2, View.GONE);
2587 contentView.setViewVisibility(R.id.profile_badge_line3, View.GONE);
2588
2589 if (profileBadge != null) {
2590 contentView.setImageViewBitmap(resId, profileBadge);
2591 contentView.setViewVisibility(resId, View.VISIBLE);
2592
2593 // Make sure Line 3 is visible. As badge will be here if there
2594 // is no text to display.
2595 if (resId == R.id.profile_badge_line3) {
2596 contentView.setViewVisibility(R.id.line3, View.VISIBLE);
2597 }
2598 return true;
2599 }
2600 return false;
2601 }
2602
Jorim Jaggi445d3c02014-08-19 22:33:42 +02002603 private RemoteViews applyStandardTemplate(int resId) {
Svet Ganov0da85b62014-08-06 14:11:37 -07002604 RemoteViews contentView = new BuilderRemoteViews(mContext.getPackageName(),
2605 mOriginatingUserId, resId);
Dan Sandler539aad42014-08-04 00:43:39 -04002606
Daniel Sandler9f7936a2012-05-21 16:14:28 -04002607 boolean showLine3 = false;
2608 boolean showLine2 = false;
Kenny Guy98193ea2014-07-24 19:54:37 +01002609 boolean contentTextInLine2 = false;
Dan Sandler190d58d2014-05-15 09:33:39 -04002610
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01002611 if (mLargeIcon != null) {
2612 contentView.setImageViewBitmap(R.id.icon, mLargeIcon);
Dan Sandler26e81cf2014-05-06 10:01:27 -04002613 processLargeIcon(mLargeIcon, contentView);
Dan Sandler190d58d2014-05-15 09:33:39 -04002614 contentView.setImageViewResource(R.id.right_icon, mSmallIcon);
2615 contentView.setViewVisibility(R.id.right_icon, View.VISIBLE);
2616 processSmallRightIcon(mSmallIcon, contentView);
2617 } else { // small icon at left
2618 contentView.setImageViewResource(R.id.icon, mSmallIcon);
2619 contentView.setViewVisibility(R.id.icon, View.VISIBLE);
2620 processSmallIconAsLarge(mSmallIcon, contentView);
Joe Onorato561d3852010-11-20 18:09:34 -08002621 }
2622 if (mContentTitle != null) {
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01002623 contentView.setTextViewText(R.id.title, processLegacyText(mContentTitle));
Joe Onorato561d3852010-11-20 18:09:34 -08002624 }
2625 if (mContentText != null) {
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01002626 contentView.setTextViewText(R.id.text, processLegacyText(mContentText));
Daniel Sandler9f7936a2012-05-21 16:14:28 -04002627 showLine3 = true;
Joe Onorato561d3852010-11-20 18:09:34 -08002628 }
2629 if (mContentInfo != null) {
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01002630 contentView.setTextViewText(R.id.info, processLegacyText(mContentInfo));
Jeff Sharkey1c400132011-08-05 14:50:13 -07002631 contentView.setViewVisibility(R.id.info, View.VISIBLE);
Daniel Sandler9f7936a2012-05-21 16:14:28 -04002632 showLine3 = true;
Joe Onorato561d3852010-11-20 18:09:34 -08002633 } else if (mNumber > 0) {
Daniel Sandlerebce0112011-06-16 16:44:51 -04002634 final int tooBig = mContext.getResources().getInteger(
2635 R.integer.status_bar_notification_info_maxnum);
2636 if (mNumber > tooBig) {
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01002637 contentView.setTextViewText(R.id.info, processLegacyText(
2638 mContext.getResources().getString(
2639 R.string.status_bar_notification_info_overflow)));
Joe Onorato059a2f82011-01-04 10:27:01 -08002640 } else {
2641 NumberFormat f = NumberFormat.getIntegerInstance();
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01002642 contentView.setTextViewText(R.id.info, processLegacyText(f.format(mNumber)));
Joe Onorato059a2f82011-01-04 10:27:01 -08002643 }
Jeff Sharkey1c400132011-08-05 14:50:13 -07002644 contentView.setViewVisibility(R.id.info, View.VISIBLE);
Daniel Sandler9f7936a2012-05-21 16:14:28 -04002645 showLine3 = true;
Joe Onorato561d3852010-11-20 18:09:34 -08002646 } else {
2647 contentView.setViewVisibility(R.id.info, View.GONE);
2648 }
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002649
Daniel Sandler9f7936a2012-05-21 16:14:28 -04002650 // Need to show three lines?
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002651 if (mSubText != null) {
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01002652 contentView.setTextViewText(R.id.text, processLegacyText(mSubText));
Daniel Sandler9f7936a2012-05-21 16:14:28 -04002653 if (mContentText != null) {
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01002654 contentView.setTextViewText(R.id.text2, processLegacyText(mContentText));
Daniel Sandler9f7936a2012-05-21 16:14:28 -04002655 contentView.setViewVisibility(R.id.text2, View.VISIBLE);
2656 showLine2 = true;
Kenny Guy98193ea2014-07-24 19:54:37 +01002657 contentTextInLine2 = true;
Daniel Sandler9f7936a2012-05-21 16:14:28 -04002658 } else {
2659 contentView.setViewVisibility(R.id.text2, View.GONE);
2660 }
Jeff Sharkey1c400132011-08-05 14:50:13 -07002661 } else {
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002662 contentView.setViewVisibility(R.id.text2, View.GONE);
2663 if (mProgressMax != 0 || mProgressIndeterminate) {
2664 contentView.setProgressBar(
2665 R.id.progress, mProgressMax, mProgress, mProgressIndeterminate);
2666 contentView.setViewVisibility(R.id.progress, View.VISIBLE);
Daniel Sandler9f7936a2012-05-21 16:14:28 -04002667 showLine2 = true;
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002668 } else {
2669 contentView.setViewVisibility(R.id.progress, View.GONE);
2670 }
Jeff Sharkey1c400132011-08-05 14:50:13 -07002671 }
Daniel Sandler9f7936a2012-05-21 16:14:28 -04002672 if (showLine2) {
Jorim Jaggi445d3c02014-08-19 22:33:42 +02002673
2674 // need to shrink all the type to make sure everything fits
2675 final Resources res = mContext.getResources();
2676 final float subTextSize = res.getDimensionPixelSize(
2677 R.dimen.notification_subtext_size);
2678 contentView.setTextViewTextSize(R.id.text, TypedValue.COMPLEX_UNIT_PX, subTextSize);
Daniel Sandler9f7936a2012-05-21 16:14:28 -04002679 }
2680
Daniel Sandler0c890492012-09-12 17:23:10 -07002681 if (mWhen != 0 && mShowWhen) {
Daniel Sandlera2985ed2012-04-03 16:42:00 -04002682 if (mUseChronometer) {
2683 contentView.setViewVisibility(R.id.chronometer, View.VISIBLE);
2684 contentView.setLong(R.id.chronometer, "setBase",
2685 mWhen + (SystemClock.elapsedRealtime() - System.currentTimeMillis()));
2686 contentView.setBoolean(R.id.chronometer, "setStarted", true);
2687 } else {
2688 contentView.setViewVisibility(R.id.time, View.VISIBLE);
2689 contentView.setLong(R.id.time, "setTime", mWhen);
2690 }
Daniel Sandler0c890492012-09-12 17:23:10 -07002691 } else {
2692 contentView.setViewVisibility(R.id.time, View.GONE);
Joe Onorato561d3852010-11-20 18:09:34 -08002693 }
Daniel Sandler0c890492012-09-12 17:23:10 -07002694
Jorim Jaggi445d3c02014-08-19 22:33:42 +02002695 // Adjust padding depending on line count and font size.
2696 contentView.setViewPadding(R.id.line1, 0, calculateTopPadding(mContext,
2697 hasThreeLines(), mContext.getResources().getConfiguration().fontScale),
2698 0, 0);
2699
Kenny Guy98193ea2014-07-24 19:54:37 +01002700 // We want to add badge to first line of text.
2701 boolean addedBadge = addProfileBadge(contentView,
2702 contentTextInLine2 ? R.id.profile_badge_line2 : R.id.profile_badge_line3);
2703 // If we added the badge to line 3 then we should show line 3.
2704 if (addedBadge && !contentTextInLine2) {
2705 showLine3 = true;
2706 }
2707
2708 // Note getStandardView may hide line 3 again.
Daniel Sandler9f7936a2012-05-21 16:14:28 -04002709 contentView.setViewVisibility(R.id.line3, showLine3 ? View.VISIBLE : View.GONE);
Daniel Sandler6387d2f2012-05-22 13:44:09 -04002710 contentView.setViewVisibility(R.id.overflow_divider, showLine3 ? View.VISIBLE : View.GONE);
Joe Onorato561d3852010-11-20 18:09:34 -08002711 return contentView;
2712 }
2713
Jorim Jaggi445d3c02014-08-19 22:33:42 +02002714 /**
2715 * Logic to find out whether the notification is going to have three lines in the contracted
2716 * layout. This is used to adjust the top padding.
2717 *
2718 * @return true if the notification is going to have three lines; false if the notification
2719 * is going to have one or two lines
2720 */
2721 private boolean hasThreeLines() {
2722 boolean hasLine3 = mContentText != null || mContentInfo != null || mNumber > 0;
2723 boolean hasLine2 = (mSubText != null && mContentText != null) ||
2724 (mSubText == null && (mProgressMax != 0 || mProgressIndeterminate));
2725 return hasLine2 && hasLine3;
2726 }
2727
2728 /**
2729 * @hide
2730 */
2731 public static int calculateTopPadding(Context ctx, boolean hasThreeLines,
2732 float fontScale) {
2733 int padding = ctx.getResources().getDimensionPixelSize(hasThreeLines
2734 ? R.dimen.notification_top_pad_narrow
2735 : R.dimen.notification_top_pad);
2736 int largePadding = ctx.getResources().getDimensionPixelSize(hasThreeLines
2737 ? R.dimen.notification_top_pad_large_text_narrow
2738 : R.dimen.notification_top_pad_large_text);
2739 float largeFactor = (MathUtils.constrain(fontScale, 1.0f, LARGE_TEXT_SCALE) - 1f)
2740 / (LARGE_TEXT_SCALE - 1f);
2741
2742 // Linearly interpolate the padding between large and normal with the font scale ranging
2743 // from 1f to LARGE_TEXT_SCALE
2744 return Math.round((1 - largeFactor) * padding + largeFactor * largePadding);
2745 }
2746
Daniel Sandler96fd7c12012-03-30 16:37:36 -04002747 private RemoteViews applyStandardTemplateWithActions(int layoutId) {
Jorim Jaggi445d3c02014-08-19 22:33:42 +02002748 RemoteViews big = applyStandardTemplate(layoutId);
Daniel Sandler96fd7c12012-03-30 16:37:36 -04002749
2750 int N = mActions.size();
2751 if (N > 0) {
Daniel Sandler96fd7c12012-03-30 16:37:36 -04002752 big.setViewVisibility(R.id.actions, View.VISIBLE);
Daniel Sandler6387d2f2012-05-22 13:44:09 -04002753 big.setViewVisibility(R.id.action_divider, View.VISIBLE);
Daniel Sandler8680bf82012-05-15 16:52:52 -04002754 if (N>MAX_ACTION_BUTTONS) N=MAX_ACTION_BUTTONS;
Chris Wren2c22eb02012-05-08 09:49:13 -04002755 big.removeAllViews(R.id.actions);
Daniel Sandler96fd7c12012-03-30 16:37:36 -04002756 for (int i=0; i<N; i++) {
2757 final RemoteViews button = generateActionButton(mActions.get(i));
Daniel Sandler96fd7c12012-03-30 16:37:36 -04002758 big.addView(R.id.actions, button);
2759 }
2760 }
2761 return big;
2762 }
2763
Joe Onorato46439ce2010-11-19 13:56:21 -08002764 private RemoteViews makeContentView() {
2765 if (mContentView != null) {
2766 return mContentView;
2767 } else {
Jorim Jaggi445d3c02014-08-19 22:33:42 +02002768 return applyStandardTemplate(getBaseLayoutResource());
Joe Onorato46439ce2010-11-19 13:56:21 -08002769 }
2770 }
2771
2772 private RemoteViews makeTickerView() {
2773 if (mTickerView != null) {
2774 return mTickerView;
Joe Onorato46439ce2010-11-19 13:56:21 -08002775 }
Dan Sandler5fcdf6e2014-07-18 11:31:15 -04002776 return null; // tickers are not created by default anymore
Joe Onorato46439ce2010-11-19 13:56:21 -08002777 }
2778
Daniel Sandler96fd7c12012-03-30 16:37:36 -04002779 private RemoteViews makeBigContentView() {
2780 if (mActions.size() == 0) return null;
2781
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01002782 return applyStandardTemplateWithActions(getBigBaseLayoutResource());
Daniel Sandler96fd7c12012-03-30 16:37:36 -04002783 }
2784
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01002785 private RemoteViews makeHeadsUpContentView() {
Chris Wren8fd39ec2014-02-27 17:43:26 -05002786 if (mActions.size() == 0) return null;
2787
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01002788 return applyStandardTemplateWithActions(getBigBaseLayoutResource());
Chris Wren8fd39ec2014-02-27 17:43:26 -05002789 }
2790
2791
Daniel Sandler96fd7c12012-03-30 16:37:36 -04002792 private RemoteViews generateActionButton(Action action) {
Daniel Sandler8680bf82012-05-15 16:52:52 -04002793 final boolean tombstone = (action.actionIntent == null);
Joe Malin8d40d042012-11-05 11:36:40 -08002794 RemoteViews button = new RemoteViews(mContext.getPackageName(),
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01002795 tombstone ? getActionTombstoneLayoutResource()
2796 : getActionLayoutResource());
Chris Wren8749ac8a2013-12-03 14:31:01 -05002797 button.setTextViewCompoundDrawablesRelative(R.id.action0, action.icon, 0, 0, 0);
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01002798 button.setTextViewText(R.id.action0, processLegacyText(action.title));
Daniel Sandler8680bf82012-05-15 16:52:52 -04002799 if (!tombstone) {
Daniel Sandlere5518842012-05-10 16:20:40 -04002800 button.setOnClickPendingIntent(R.id.action0, action.actionIntent);
Daniel Sandlere5518842012-05-10 16:20:40 -04002801 }
Daniel Sandler96fd7c12012-03-30 16:37:36 -04002802 button.setContentDescription(R.id.action0, action.title);
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01002803 processLegacyAction(action, button);
Daniel Sandler96fd7c12012-03-30 16:37:36 -04002804 return button;
2805 }
2806
Joe Onoratocb109a02011-01-18 17:57:41 -08002807 /**
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01002808 * @return Whether we are currently building a notification from a legacy (an app that
Alan Viverette3cb07a462014-06-06 14:19:53 -07002809 * doesn't create material notifications by itself) app.
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01002810 */
2811 private boolean isLegacy() {
Dan Sandler26e81cf2014-05-06 10:01:27 -04002812 return mColorUtil != null;
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01002813 }
2814
2815 private void processLegacyAction(Action action, RemoteViews button) {
2816 if (isLegacy()) {
Dan Sandler26e81cf2014-05-06 10:01:27 -04002817 if (mColorUtil.isGrayscale(mContext, action.icon)) {
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01002818 button.setTextViewCompoundDrawablesRelativeColorFilter(R.id.action0, 0,
2819 mContext.getResources().getColor(
2820 R.color.notification_action_legacy_color_filter),
2821 PorterDuff.Mode.MULTIPLY);
2822 }
2823 }
2824 }
2825
2826 private CharSequence processLegacyText(CharSequence charSequence) {
2827 if (isLegacy()) {
Dan Sandler26e81cf2014-05-06 10:01:27 -04002828 return mColorUtil.invertCharSequenceColors(charSequence);
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01002829 } else {
2830 return charSequence;
2831 }
2832 }
2833
Dan Sandler26e81cf2014-05-06 10:01:27 -04002834 /**
2835 * Apply any necessary background to smallIcons being used in the largeIcon spot.
2836 */
2837 private void processSmallIconAsLarge(int largeIconId, RemoteViews contentView) {
2838 if (!isLegacy() || mColorUtil.isGrayscale(mContext, largeIconId)) {
2839 applyLargeIconBackground(contentView);
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01002840 }
2841 }
2842
Dan Sandler26e81cf2014-05-06 10:01:27 -04002843 /**
2844 * Apply any necessary background to a largeIcon if it's a fake smallIcon (that is,
2845 * if it's grayscale).
2846 */
2847 // TODO: also check bounds, transparency, that sort of thing.
2848 private void processLargeIcon(Bitmap largeIcon, RemoteViews contentView) {
2849 if (!isLegacy() || mColorUtil.isGrayscale(largeIcon)) {
2850 applyLargeIconBackground(contentView);
Dan Sandler190d58d2014-05-15 09:33:39 -04002851 } else {
2852 removeLargeIconBackground(contentView);
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01002853 }
2854 }
2855
Dan Sandler26e81cf2014-05-06 10:01:27 -04002856 /**
2857 * Add a colored circle behind the largeIcon slot.
2858 */
2859 private void applyLargeIconBackground(RemoteViews contentView) {
2860 contentView.setInt(R.id.icon, "setBackgroundResource",
Jorim Jaggi445d3c02014-08-19 22:33:42 +02002861 R.drawable.notification_icon_legacy_bg);
Dan Sandler26e81cf2014-05-06 10:01:27 -04002862
2863 contentView.setDrawableParameters(
2864 R.id.icon,
2865 true,
2866 -1,
Jorim Jaggi74419312014-06-10 20:57:21 +02002867 resolveColor(),
Dan Sandler26e81cf2014-05-06 10:01:27 -04002868 PorterDuff.Mode.SRC_ATOP,
2869 -1);
Jorim Jaggi445d3c02014-08-19 22:33:42 +02002870
2871 int padding = mContext.getResources().getDimensionPixelSize(
2872 R.dimen.notification_large_icon_circle_padding);
2873 contentView.setViewPadding(R.id.icon, padding, padding, padding, padding);
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01002874 }
2875
Dan Sandler190d58d2014-05-15 09:33:39 -04002876 private void removeLargeIconBackground(RemoteViews contentView) {
2877 contentView.setInt(R.id.icon, "setBackgroundResource", 0);
2878 }
2879
Dan Sandler26e81cf2014-05-06 10:01:27 -04002880 /**
2881 * Recolor small icons when used in the R.id.right_icon slot.
2882 */
Dan Sandler190d58d2014-05-15 09:33:39 -04002883 private void processSmallRightIcon(int smallIconDrawableId,
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01002884 RemoteViews contentView) {
Dan Sandler26e81cf2014-05-06 10:01:27 -04002885 if (!isLegacy() || mColorUtil.isGrayscale(mContext, smallIconDrawableId)) {
Dan Sandler190d58d2014-05-15 09:33:39 -04002886 contentView.setDrawableParameters(R.id.right_icon, false, -1,
2887 0xFFFFFFFF,
2888 PorterDuff.Mode.SRC_ATOP, -1);
2889
2890 contentView.setInt(R.id.right_icon,
2891 "setBackgroundResource",
2892 R.drawable.notification_icon_legacy_bg);
2893
2894 contentView.setDrawableParameters(
2895 R.id.right_icon,
2896 true,
2897 -1,
Jorim Jaggi74419312014-06-10 20:57:21 +02002898 resolveColor(),
Dan Sandler190d58d2014-05-15 09:33:39 -04002899 PorterDuff.Mode.SRC_ATOP,
2900 -1);
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01002901 }
2902 }
2903
Jorim Jaggi74419312014-06-10 20:57:21 +02002904 private int sanitizeColor() {
2905 if (mColor != COLOR_DEFAULT) {
2906 mColor |= 0xFF000000; // no alpha for custom colors
2907 }
2908 return mColor;
2909 }
2910
Dan Sandler26e81cf2014-05-06 10:01:27 -04002911 private int resolveColor() {
2912 if (mColor == COLOR_DEFAULT) {
Jorim Jaggi74419312014-06-10 20:57:21 +02002913 return mContext.getResources().getColor(R.color.notification_icon_bg_color);
Dan Sandler26e81cf2014-05-06 10:01:27 -04002914 }
2915 return mColor;
2916 }
2917
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01002918 /**
Chris Wrenfbd96ba2012-05-01 12:03:58 -04002919 * Apply the unstyled operations and return a new {@link Notification} object.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04002920 * @hide
Joe Onoratocb109a02011-01-18 17:57:41 -08002921 */
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04002922 public Notification buildUnstyled() {
Joe Onorato46439ce2010-11-19 13:56:21 -08002923 Notification n = new Notification();
2924 n.when = mWhen;
2925 n.icon = mSmallIcon;
2926 n.iconLevel = mSmallIconLevel;
Joe Onorato8595a3d2010-11-19 18:12:07 -08002927 n.number = mNumber;
Dan Sandler26e81cf2014-05-06 10:01:27 -04002928
Jorim Jaggi74419312014-06-10 20:57:21 +02002929 n.color = sanitizeColor();
Dan Sandler26e81cf2014-05-06 10:01:27 -04002930
Christoph Studer4600f9b2014-07-22 22:44:43 +02002931 setBuilderContentView(n, makeContentView());
Joe Onorato46439ce2010-11-19 13:56:21 -08002932 n.contentIntent = mContentIntent;
2933 n.deleteIntent = mDeleteIntent;
2934 n.fullScreenIntent = mFullScreenIntent;
2935 n.tickerText = mTickerText;
2936 n.tickerView = makeTickerView();
2937 n.largeIcon = mLargeIcon;
2938 n.sound = mSound;
2939 n.audioStreamType = mAudioStreamType;
John Spurlockc0650f022014-07-19 13:22:39 -04002940 n.audioAttributes = mAudioAttributes;
Joe Onorato46439ce2010-11-19 13:56:21 -08002941 n.vibrate = mVibrate;
2942 n.ledARGB = mLedArgb;
2943 n.ledOnMS = mLedOnMs;
2944 n.ledOffMS = mLedOffMs;
2945 n.defaults = mDefaults;
2946 n.flags = mFlags;
Christoph Studer4600f9b2014-07-22 22:44:43 +02002947 setBuilderBigContentView(n, makeBigContentView());
2948 setBuilderHeadsUpContentView(n, makeHeadsUpContentView());
Daniel Sandler26c13432013-04-04 11:01:04 -04002949 if (mLedOnMs != 0 || mLedOffMs != 0) {
Joe Onorato8d0b6552010-11-22 16:09:29 -08002950 n.flags |= FLAG_SHOW_LIGHTS;
2951 }
2952 if ((mDefaults & DEFAULT_LIGHTS) != 0) {
2953 n.flags |= FLAG_SHOW_LIGHTS;
2954 }
John Spurlockfd7f1e02014-03-18 16:41:57 -04002955 n.category = mCategory;
Griff Hazen5cadc3b2014-05-20 09:55:39 -07002956 n.mGroupKey = mGroupKey;
2957 n.mSortKey = mSortKey;
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002958 n.priority = mPriority;
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002959 if (mActions.size() > 0) {
2960 n.actions = new Action[mActions.size()];
2961 mActions.toArray(n.actions);
2962 }
Dan Sandler0bf2ed82013-12-21 23:33:41 -06002963 n.visibility = mVisibility;
2964
2965 if (mPublicVersion != null) {
2966 n.publicVersion = new Notification();
2967 mPublicVersion.cloneInto(n.publicVersion, true);
2968 }
Christoph Studer4600f9b2014-07-22 22:44:43 +02002969 // Note: If you're adding new fields, also update restoreFromNotitification().
Joe Onorato46439ce2010-11-19 13:56:21 -08002970 return n;
2971 }
Chris Wrenfbd96ba2012-05-01 12:03:58 -04002972
2973 /**
Daniel Sandlerf45564e2013-04-15 15:05:08 -04002974 * Capture, in the provided bundle, semantic information used in the construction of
2975 * this Notification object.
2976 * @hide
2977 */
Griff Hazen720042b2014-02-24 15:46:56 -08002978 public void populateExtras(Bundle extras) {
Daniel Sandlerf45564e2013-04-15 15:05:08 -04002979 // Store original information used in the construction of this object
Christoph Studer7ac80e62014-08-04 16:01:57 +02002980 extras.putInt(EXTRA_ORIGINATING_USERID, mOriginatingUserId);
Christoph Studer4600f9b2014-07-22 22:44:43 +02002981 extras.putString(EXTRA_REBUILD_CONTEXT_PACKAGE, mContext.getPackageName());
Daniel Sandlerf45564e2013-04-15 15:05:08 -04002982 extras.putCharSequence(EXTRA_TITLE, mContentTitle);
2983 extras.putCharSequence(EXTRA_TEXT, mContentText);
2984 extras.putCharSequence(EXTRA_SUB_TEXT, mSubText);
2985 extras.putCharSequence(EXTRA_INFO_TEXT, mContentInfo);
2986 extras.putInt(EXTRA_SMALL_ICON, mSmallIcon);
2987 extras.putInt(EXTRA_PROGRESS, mProgress);
2988 extras.putInt(EXTRA_PROGRESS_MAX, mProgressMax);
2989 extras.putBoolean(EXTRA_PROGRESS_INDETERMINATE, mProgressIndeterminate);
2990 extras.putBoolean(EXTRA_SHOW_CHRONOMETER, mUseChronometer);
2991 extras.putBoolean(EXTRA_SHOW_WHEN, mShowWhen);
John Spurlockac08a472013-06-10 11:37:08 -04002992 if (mLargeIcon != null) {
2993 extras.putParcelable(EXTRA_LARGE_ICON, mLargeIcon);
2994 }
Chris Wrendde75302014-03-26 17:24:15 -04002995 if (!mPeople.isEmpty()) {
2996 extras.putStringArray(EXTRA_PEOPLE, mPeople.toArray(new String[mPeople.size()]));
2997 }
Christoph Studer4600f9b2014-07-22 22:44:43 +02002998 // NOTE: If you're adding new extras also update restoreFromNotification().
2999 }
3000
3001
3002 /**
3003 * @hide
3004 */
3005 public static void stripForDelivery(Notification n) {
3006 if (!STRIP_AND_REBUILD) {
3007 return;
3008 }
3009
3010 String templateClass = n.extras.getString(EXTRA_TEMPLATE);
3011 // Only strip views for known Styles because we won't know how to
3012 // re-create them otherwise.
3013 boolean stripViews = TextUtils.isEmpty(templateClass) ||
3014 getNotificationStyleClass(templateClass) != null;
3015
3016 boolean isStripped = false;
3017
3018 if (n.largeIcon != null && n.extras.containsKey(EXTRA_LARGE_ICON)) {
3019 // TODO: Would like to check for equality here, but if the notification
3020 // has been cloned, we can't.
3021 n.largeIcon = null;
3022 n.extras.putBoolean(EXTRA_REBUILD_LARGE_ICON, true);
3023 isStripped = true;
3024 }
3025 // Get rid of unmodified BuilderRemoteViews.
3026
3027 if (stripViews &&
3028 n.contentView instanceof BuilderRemoteViews &&
3029 n.extras.getInt(EXTRA_REBUILD_CONTENT_VIEW_ACTION_COUNT, -1) ==
3030 n.contentView.getSequenceNumber()) {
3031 n.contentView = null;
3032 n.extras.putBoolean(EXTRA_REBUILD_CONTENT_VIEW, true);
3033 n.extras.remove(EXTRA_REBUILD_CONTENT_VIEW_ACTION_COUNT);
3034 isStripped = true;
3035 }
3036 if (stripViews &&
3037 n.bigContentView instanceof BuilderRemoteViews &&
3038 n.extras.getInt(EXTRA_REBUILD_BIG_CONTENT_VIEW_ACTION_COUNT, -1) ==
3039 n.bigContentView.getSequenceNumber()) {
3040 n.bigContentView = null;
3041 n.extras.putBoolean(EXTRA_REBUILD_BIG_CONTENT_VIEW, true);
3042 n.extras.remove(EXTRA_REBUILD_BIG_CONTENT_VIEW_ACTION_COUNT);
3043 isStripped = true;
3044 }
3045 if (stripViews &&
3046 n.headsUpContentView instanceof BuilderRemoteViews &&
3047 n.extras.getInt(EXTRA_REBUILD_HEADS_UP_CONTENT_VIEW_ACTION_COUNT, -1) ==
3048 n.headsUpContentView.getSequenceNumber()) {
3049 n.headsUpContentView = null;
3050 n.extras.putBoolean(EXTRA_REBUILD_HEADS_UP_CONTENT_VIEW, true);
3051 n.extras.remove(EXTRA_REBUILD_HEADS_UP_CONTENT_VIEW_ACTION_COUNT);
3052 isStripped = true;
3053 }
3054
3055 if (isStripped) {
3056 n.extras.putBoolean(EXTRA_NEEDS_REBUILD, true);
3057 }
3058 }
3059
3060 /**
3061 * @hide
3062 */
3063 public static Notification rebuild(Context context, Notification n) {
3064 Bundle extras = n.extras;
3065 if (!extras.getBoolean(EXTRA_NEEDS_REBUILD)) return n;
3066 extras.remove(EXTRA_NEEDS_REBUILD);
3067
3068 // Re-create notification context so we can access app resources.
3069 String packageName = extras.getString(EXTRA_REBUILD_CONTEXT_PACKAGE);
3070 Context builderContext;
3071 try {
3072 builderContext = context.createPackageContext(packageName,
3073 Context.CONTEXT_RESTRICTED);
3074 } catch (NameNotFoundException e) {
3075 Log.e(TAG, "Package name " + packageName + " not found");
3076 builderContext = context; // try with our context
3077 }
3078
3079 Builder b = new Builder(builderContext, n);
3080 return b.rebuild();
3081 }
3082
3083 /**
3084 * Rebuilds the notification passed in to the rebuild-constructor
3085 * {@link #Builder(Context, Notification)}.
3086 *
3087 * <p>
3088 * Throws IllegalStateException when invoked on a Builder that isn't in rebuild mode.
3089 *
3090 * @hide
3091 */
3092 private Notification rebuild() {
3093 if (mRebuildNotification == null) {
3094 throw new IllegalStateException("rebuild() only valid when in 'rebuild' mode.");
3095 }
3096
3097 Bundle extras = mRebuildNotification.extras;
3098
3099 if (extras.getBoolean(EXTRA_REBUILD_LARGE_ICON)) {
3100 mRebuildNotification.largeIcon = extras.getParcelable(EXTRA_LARGE_ICON);
3101 }
3102 extras.remove(EXTRA_REBUILD_LARGE_ICON);
3103
3104 if (extras.getBoolean(EXTRA_REBUILD_CONTENT_VIEW)) {
3105 setBuilderContentView(mRebuildNotification, makeContentView());
3106 if (mStyle != null) {
3107 mStyle.populateContentView(mRebuildNotification);
3108 }
3109 }
3110 extras.remove(EXTRA_REBUILD_CONTENT_VIEW);
3111
3112 if (extras.getBoolean(EXTRA_REBUILD_BIG_CONTENT_VIEW)) {
3113 setBuilderBigContentView(mRebuildNotification, makeBigContentView());
3114 if (mStyle != null) {
3115 mStyle.populateBigContentView(mRebuildNotification);
3116 }
3117 }
3118 extras.remove(EXTRA_REBUILD_BIG_CONTENT_VIEW);
3119
3120 if (extras.getBoolean(EXTRA_REBUILD_HEADS_UP_CONTENT_VIEW)) {
3121 setBuilderHeadsUpContentView(mRebuildNotification, makeHeadsUpContentView());
3122 if (mStyle != null) {
3123 mStyle.populateHeadsUpContentView(mRebuildNotification);
3124 }
3125 }
3126 extras.remove(EXTRA_REBUILD_HEADS_UP_CONTENT_VIEW);
3127
3128 return mRebuildNotification;
3129 }
3130
3131 private static Class<? extends Style> getNotificationStyleClass(String templateClass) {
3132 Class<? extends Style>[] classes = new Class[]{
3133 BigTextStyle.class, BigPictureStyle.class, InboxStyle.class, MediaStyle.class};
3134 for (Class<? extends Style> innerClass : classes) {
3135 if (templateClass.equals(innerClass.getName())) {
3136 return innerClass;
3137 }
3138 }
3139 return null;
3140 }
3141
3142 private void setBuilderContentView(Notification n, RemoteViews contentView) {
3143 n.contentView = contentView;
3144 if (contentView instanceof BuilderRemoteViews) {
3145 mRebuildBundle.putInt(Builder.EXTRA_REBUILD_CONTENT_VIEW_ACTION_COUNT,
3146 contentView.getSequenceNumber());
3147 }
3148 }
3149
3150 private void setBuilderBigContentView(Notification n, RemoteViews bigContentView) {
3151 n.bigContentView = bigContentView;
3152 if (bigContentView instanceof BuilderRemoteViews) {
3153 mRebuildBundle.putInt(Builder.EXTRA_REBUILD_BIG_CONTENT_VIEW_ACTION_COUNT,
3154 bigContentView.getSequenceNumber());
3155 }
3156 }
3157
3158 private void setBuilderHeadsUpContentView(Notification n,
3159 RemoteViews headsUpContentView) {
3160 n.headsUpContentView = headsUpContentView;
3161 if (headsUpContentView instanceof BuilderRemoteViews) {
3162 mRebuildBundle.putInt(Builder.EXTRA_REBUILD_HEADS_UP_CONTENT_VIEW_ACTION_COUNT,
3163 headsUpContentView.getSequenceNumber());
3164 }
3165 }
3166
3167 private void restoreFromNotification(Notification n) {
3168
3169 // Notification fields.
3170 mWhen = n.when;
3171 mSmallIcon = n.icon;
3172 mSmallIconLevel = n.iconLevel;
3173 mNumber = n.number;
3174
3175 mColor = n.color;
3176
3177 mContentView = n.contentView;
3178 mDeleteIntent = n.deleteIntent;
3179 mFullScreenIntent = n.fullScreenIntent;
3180 mTickerText = n.tickerText;
3181 mTickerView = n.tickerView;
3182 mLargeIcon = n.largeIcon;
3183 mSound = n.sound;
3184 mAudioStreamType = n.audioStreamType;
3185 mAudioAttributes = n.audioAttributes;
3186
3187 mVibrate = n.vibrate;
3188 mLedArgb = n.ledARGB;
3189 mLedOnMs = n.ledOnMS;
3190 mLedOffMs = n.ledOffMS;
3191 mDefaults = n.defaults;
3192 mFlags = n.flags;
3193
3194 mCategory = n.category;
3195 mGroupKey = n.mGroupKey;
3196 mSortKey = n.mSortKey;
3197 mPriority = n.priority;
3198 mActions.clear();
3199 if (n.actions != null) {
3200 Collections.addAll(mActions, n.actions);
3201 }
3202 mVisibility = n.visibility;
3203
3204 mPublicVersion = n.publicVersion;
3205
3206 // Extras.
3207 Bundle extras = n.extras;
Christoph Studer7ac80e62014-08-04 16:01:57 +02003208 mOriginatingUserId = extras.getInt(EXTRA_ORIGINATING_USERID);
Christoph Studer4600f9b2014-07-22 22:44:43 +02003209 mContentTitle = extras.getCharSequence(EXTRA_TITLE);
3210 mContentText = extras.getCharSequence(EXTRA_TEXT);
3211 mSubText = extras.getCharSequence(EXTRA_SUB_TEXT);
3212 mContentInfo = extras.getCharSequence(EXTRA_INFO_TEXT);
3213 mSmallIcon = extras.getInt(EXTRA_SMALL_ICON);
3214 mProgress = extras.getInt(EXTRA_PROGRESS);
3215 mProgressMax = extras.getInt(EXTRA_PROGRESS_MAX);
3216 mProgressIndeterminate = extras.getBoolean(EXTRA_PROGRESS_INDETERMINATE);
3217 mUseChronometer = extras.getBoolean(EXTRA_SHOW_CHRONOMETER);
3218 mShowWhen = extras.getBoolean(EXTRA_SHOW_WHEN);
3219 if (extras.containsKey(EXTRA_LARGE_ICON)) {
3220 mLargeIcon = extras.getParcelable(EXTRA_LARGE_ICON);
3221 }
3222 if (extras.containsKey(EXTRA_PEOPLE)) {
3223 mPeople.clear();
3224 Collections.addAll(mPeople, extras.getStringArray(EXTRA_PEOPLE));
3225 }
Daniel Sandlerf45564e2013-04-15 15:05:08 -04003226 }
3227
3228 /**
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003229 * @deprecated Use {@link #build()} instead.
3230 */
3231 @Deprecated
3232 public Notification getNotification() {
3233 return build();
3234 }
3235
3236 /**
3237 * Combine all of the options that have been set and return a new {@link Notification}
3238 * object.
3239 */
3240 public Notification build() {
Christoph Studer7ac80e62014-08-04 16:01:57 +02003241 mOriginatingUserId = mContext.getUserId();
Christoph Studer943aa672014-08-03 20:31:16 +02003242
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04003243 Notification n = buildUnstyled();
Daniel Sandlerf45564e2013-04-15 15:05:08 -04003244
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003245 if (mStyle != null) {
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04003246 n = mStyle.buildStyled(n);
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003247 }
Daniel Sandlerf45564e2013-04-15 15:05:08 -04003248
Christoph Studer4600f9b2014-07-22 22:44:43 +02003249 if (mExtras != null) {
3250 n.extras.putAll(mExtras);
3251 }
3252
3253 if (mRebuildBundle.size() > 0) {
3254 n.extras.putAll(mRebuildBundle);
3255 mRebuildBundle.clear();
3256 }
Daniel Sandlerf45564e2013-04-15 15:05:08 -04003257
Griff Hazen720042b2014-02-24 15:46:56 -08003258 populateExtras(n.extras);
Daniel Sandlerf45564e2013-04-15 15:05:08 -04003259 if (mStyle != null) {
3260 mStyle.addExtras(n.extras);
3261 }
3262
3263 return n;
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003264 }
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05003265
3266 /**
3267 * Apply this Builder to an existing {@link Notification} object.
3268 *
3269 * @hide
3270 */
3271 public Notification buildInto(Notification n) {
Daniel Sandler1a497d32013-04-18 14:52:45 -04003272 build().cloneInto(n, true);
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05003273 return n;
3274 }
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01003275
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01003276 private int getBaseLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07003277 return R.layout.notification_template_material_base;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01003278 }
3279
3280 private int getBigBaseLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07003281 return R.layout.notification_template_material_big_base;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01003282 }
3283
3284 private int getBigPictureLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07003285 return R.layout.notification_template_material_big_picture;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01003286 }
3287
3288 private int getBigTextLayoutResource() {
Jorim Jaggi445d3c02014-08-19 22:33:42 +02003289 return R.layout.notification_template_material_big_text;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01003290 }
3291
3292 private int getInboxLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07003293 return R.layout.notification_template_material_inbox;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01003294 }
3295
3296 private int getActionLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07003297 return R.layout.notification_material_action;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01003298 }
3299
3300 private int getActionTombstoneLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07003301 return R.layout.notification_material_action_tombstone;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01003302 }
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003303 }
3304
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003305 /**
3306 * An object that can apply a rich notification style to a {@link Notification.Builder}
3307 * object.
3308 */
Griff Hazendfcb0802014-02-11 12:00:00 -08003309 public static abstract class Style {
Chris Wrend6297db2012-05-03 16:20:13 -04003310 private CharSequence mBigContentTitle;
3311 private CharSequence mSummaryText = null;
Daniel Sandler619738c2012-06-07 16:33:08 -04003312 private boolean mSummaryTextSet = false;
Chris Wrend6297db2012-05-03 16:20:13 -04003313
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003314 protected Builder mBuilder;
3315
Chris Wrend6297db2012-05-03 16:20:13 -04003316 /**
3317 * Overrides ContentTitle in the big form of the template.
3318 * This defaults to the value passed to setContentTitle().
3319 */
3320 protected void internalSetBigContentTitle(CharSequence title) {
3321 mBigContentTitle = title;
3322 }
3323
3324 /**
3325 * Set the first line of text after the detail section in the big form of the template.
3326 */
3327 protected void internalSetSummaryText(CharSequence cs) {
3328 mSummaryText = cs;
Daniel Sandler619738c2012-06-07 16:33:08 -04003329 mSummaryTextSet = true;
Chris Wrend6297db2012-05-03 16:20:13 -04003330 }
3331
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003332 public void setBuilder(Builder builder) {
3333 if (mBuilder != builder) {
3334 mBuilder = builder;
Daniel Sandlerc08dea22012-06-28 08:35:24 -07003335 if (mBuilder != null) {
3336 mBuilder.setStyle(this);
3337 }
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003338 }
3339 }
3340
Chris Wrend6297db2012-05-03 16:20:13 -04003341 protected void checkBuilder() {
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003342 if (mBuilder == null) {
3343 throw new IllegalArgumentException("Style requires a valid Builder object");
3344 }
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003345 }
Chris Wrend6297db2012-05-03 16:20:13 -04003346
3347 protected RemoteViews getStandardView(int layoutId) {
3348 checkBuilder();
3349
Christoph Studer4600f9b2014-07-22 22:44:43 +02003350 // Nasty.
3351 CharSequence oldBuilderContentTitle = mBuilder.mContentTitle;
Chris Wrend6297db2012-05-03 16:20:13 -04003352 if (mBigContentTitle != null) {
3353 mBuilder.setContentTitle(mBigContentTitle);
3354 }
3355
Chris Wrend6297db2012-05-03 16:20:13 -04003356 RemoteViews contentView = mBuilder.applyStandardTemplateWithActions(layoutId);
3357
Christoph Studer4600f9b2014-07-22 22:44:43 +02003358 mBuilder.mContentTitle = oldBuilderContentTitle;
3359
Chris Wrend6297db2012-05-03 16:20:13 -04003360 if (mBigContentTitle != null && mBigContentTitle.equals("")) {
3361 contentView.setViewVisibility(R.id.line1, View.GONE);
Chris Wren67dc9a02012-05-16 01:03:20 -04003362 } else {
3363 contentView.setViewVisibility(R.id.line1, View.VISIBLE);
Chris Wrend6297db2012-05-03 16:20:13 -04003364 }
3365
Daniel Sandler619738c2012-06-07 16:33:08 -04003366 // The last line defaults to the subtext, but can be replaced by mSummaryText
3367 final CharSequence overflowText =
3368 mSummaryTextSet ? mSummaryText
3369 : mBuilder.mSubText;
3370 if (overflowText != null) {
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01003371 contentView.setTextViewText(R.id.text, mBuilder.processLegacyText(overflowText));
Daniel Sandler619738c2012-06-07 16:33:08 -04003372 contentView.setViewVisibility(R.id.overflow_divider, View.VISIBLE);
Daniel Sandler9f7936a2012-05-21 16:14:28 -04003373 contentView.setViewVisibility(R.id.line3, View.VISIBLE);
Daniel Sandler916ad912012-06-13 12:17:07 -04003374 } else {
Kenny Guy98193ea2014-07-24 19:54:37 +01003375 // Clear text in case we use the line to show the profile badge.
3376 contentView.setTextViewText(R.id.text, "");
Daniel Sandler916ad912012-06-13 12:17:07 -04003377 contentView.setViewVisibility(R.id.overflow_divider, View.GONE);
3378 contentView.setViewVisibility(R.id.line3, View.GONE);
Chris Wrend6297db2012-05-03 16:20:13 -04003379 }
3380
3381 return contentView;
3382 }
3383
Daniel Sandlerf45564e2013-04-15 15:05:08 -04003384 /**
Jorim Jaggi445d3c02014-08-19 22:33:42 +02003385 * Changes the padding of the first line such that the big and small content view have the
3386 * same top padding.
3387 *
3388 * @hide
3389 */
3390 protected void applyTopPadding(RemoteViews contentView) {
3391 int topPadding = Builder.calculateTopPadding(mBuilder.mContext,
3392 mBuilder.hasThreeLines(),
3393 mBuilder.mContext.getResources().getConfiguration().fontScale);
3394 contentView.setViewPadding(R.id.line1, 0, topPadding, 0, 0);
3395 }
3396
3397 /**
Daniel Sandlerf45564e2013-04-15 15:05:08 -04003398 * @hide
3399 */
3400 public void addExtras(Bundle extras) {
3401 if (mSummaryTextSet) {
3402 extras.putCharSequence(EXTRA_SUMMARY_TEXT, mSummaryText);
3403 }
3404 if (mBigContentTitle != null) {
3405 extras.putCharSequence(EXTRA_TITLE_BIG, mBigContentTitle);
3406 }
Chris Wren91ad5632013-06-05 15:05:57 -04003407 extras.putString(EXTRA_TEMPLATE, this.getClass().getName());
Daniel Sandlerf45564e2013-04-15 15:05:08 -04003408 }
3409
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04003410 /**
3411 * @hide
3412 */
Christoph Studer4600f9b2014-07-22 22:44:43 +02003413 protected void restoreFromExtras(Bundle extras) {
3414 if (extras.containsKey(EXTRA_SUMMARY_TEXT)) {
3415 mSummaryText = extras.getCharSequence(EXTRA_SUMMARY_TEXT);
3416 mSummaryTextSet = true;
3417 }
3418 if (extras.containsKey(EXTRA_TITLE_BIG)) {
3419 mBigContentTitle = extras.getCharSequence(EXTRA_TITLE_BIG);
3420 }
3421 }
3422
3423
3424 /**
3425 * @hide
3426 */
3427 public Notification buildStyled(Notification wip) {
3428 populateTickerView(wip);
3429 populateContentView(wip);
3430 populateBigContentView(wip);
3431 populateHeadsUpContentView(wip);
3432 return wip;
3433 }
3434
3435 // The following methods are split out so we can re-create notification partially.
3436 /**
3437 * @hide
3438 */
3439 protected void populateTickerView(Notification wip) {}
3440 /**
3441 * @hide
3442 */
3443 protected void populateContentView(Notification wip) {}
3444
3445 /**
3446 * @hide
3447 */
3448 protected void populateBigContentView(Notification wip) {}
3449
3450 /**
3451 * @hide
3452 */
3453 protected void populateHeadsUpContentView(Notification wip) {}
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04003454
3455 /**
3456 * Calls {@link android.app.Notification.Builder#build()} on the Builder this Style is
3457 * attached to.
3458 *
3459 * @return the fully constructed Notification.
3460 */
3461 public Notification build() {
3462 checkBuilder();
3463 return mBuilder.build();
3464 }
Joe Onorato46439ce2010-11-19 13:56:21 -08003465 }
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003466
3467 /**
Daniel Sandler4dfbe832012-04-11 14:51:46 -04003468 * Helper class for generating large-format notifications that include a large image attachment.
Joe Malin8d40d042012-11-05 11:36:40 -08003469 *
Robert Ly91c5ce32014-06-08 15:37:00 -07003470 * Here's how you'd set the <code>BigPictureStyle</code> on a notification:
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003471 * <pre class="prettyprint">
Robert Ly91c5ce32014-06-08 15:37:00 -07003472 * Notification notif = new Notification.Builder(mContext)
3473 * .setContentTitle(&quot;New photo from &quot; + sender.toString())
3474 * .setContentText(subject)
3475 * .setSmallIcon(R.drawable.new_post)
3476 * .setLargeIcon(aBitmap)
3477 * .setStyle(new Notification.BigPictureStyle()
3478 * .bigPicture(aBigBitmap))
3479 * .build();
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003480 * </pre>
Joe Malin8d40d042012-11-05 11:36:40 -08003481 *
Daniel Sandler4dfbe832012-04-11 14:51:46 -04003482 * @see Notification#bigContentView
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003483 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003484 public static class BigPictureStyle extends Style {
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003485 private Bitmap mPicture;
Chris Wren3745a3d2012-05-22 15:11:52 -04003486 private Bitmap mBigLargeIcon;
3487 private boolean mBigLargeIconSet = false;
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003488
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003489 public BigPictureStyle() {
3490 }
3491
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003492 public BigPictureStyle(Builder builder) {
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003493 setBuilder(builder);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003494 }
3495
Chris Wrend6297db2012-05-03 16:20:13 -04003496 /**
3497 * Overrides ContentTitle in the big form of the template.
3498 * This defaults to the value passed to setContentTitle().
3499 */
3500 public BigPictureStyle setBigContentTitle(CharSequence title) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04003501 internalSetBigContentTitle(safeCharSequence(title));
Chris Wrend6297db2012-05-03 16:20:13 -04003502 return this;
3503 }
3504
3505 /**
3506 * Set the first line of text after the detail section in the big form of the template.
3507 */
3508 public BigPictureStyle setSummaryText(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04003509 internalSetSummaryText(safeCharSequence(cs));
Chris Wrend6297db2012-05-03 16:20:13 -04003510 return this;
3511 }
3512
Chris Wren0bd664d2012-08-01 13:56:56 -04003513 /**
3514 * Provide the bitmap to be used as the payload for the BigPicture notification.
3515 */
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003516 public BigPictureStyle bigPicture(Bitmap b) {
3517 mPicture = b;
3518 return this;
3519 }
3520
Chris Wren3745a3d2012-05-22 15:11:52 -04003521 /**
Chris Wren3745a3d2012-05-22 15:11:52 -04003522 * Override the large icon when the big notification is shown.
3523 */
3524 public BigPictureStyle bigLargeIcon(Bitmap b) {
3525 mBigLargeIconSet = true;
3526 mBigLargeIcon = b;
3527 return this;
3528 }
3529
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003530 private RemoteViews makeBigContentView() {
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01003531 RemoteViews contentView = getStandardView(mBuilder.getBigPictureLayoutResource());
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003532
3533 contentView.setImageViewBitmap(R.id.big_picture, mPicture);
3534
Jorim Jaggi445d3c02014-08-19 22:33:42 +02003535 applyTopPadding(contentView);
3536
Kenny Guy98193ea2014-07-24 19:54:37 +01003537 boolean twoTextLines = mBuilder.mSubText != null && mBuilder.mContentText != null;
3538 mBuilder.addProfileBadge(contentView,
3539 twoTextLines ? R.id.profile_badge_line2 : R.id.profile_badge_line3);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003540 return contentView;
3541 }
3542
Daniel Sandlerf45564e2013-04-15 15:05:08 -04003543 /**
3544 * @hide
3545 */
3546 public void addExtras(Bundle extras) {
3547 super.addExtras(extras);
3548
3549 if (mBigLargeIconSet) {
3550 extras.putParcelable(EXTRA_LARGE_ICON_BIG, mBigLargeIcon);
3551 }
3552 extras.putParcelable(EXTRA_PICTURE, mPicture);
3553 }
3554
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04003555 /**
3556 * @hide
3557 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003558 @Override
Christoph Studer4600f9b2014-07-22 22:44:43 +02003559 protected void restoreFromExtras(Bundle extras) {
3560 super.restoreFromExtras(extras);
3561
3562 if (extras.containsKey(EXTRA_LARGE_ICON_BIG)) {
3563 mBigLargeIcon = extras.getParcelable(EXTRA_LARGE_ICON_BIG);
Chris Wren3745a3d2012-05-22 15:11:52 -04003564 }
Christoph Studer4600f9b2014-07-22 22:44:43 +02003565 mPicture = extras.getParcelable(EXTRA_PICTURE);
3566 }
3567
3568 /**
3569 * @hide
3570 */
3571 @Override
3572 public void populateBigContentView(Notification wip) {
3573 mBuilder.setBuilderBigContentView(wip, makeBigContentView());
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003574 }
3575 }
3576
3577 /**
Daniel Sandler4dfbe832012-04-11 14:51:46 -04003578 * Helper class for generating large-format notifications that include a lot of text.
Joe Malin8d40d042012-11-05 11:36:40 -08003579 *
Robert Ly91c5ce32014-06-08 15:37:00 -07003580 * Here's how you'd set the <code>BigTextStyle</code> on a notification:
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003581 * <pre class="prettyprint">
Robert Ly91c5ce32014-06-08 15:37:00 -07003582 * Notification notif = new Notification.Builder(mContext)
3583 * .setContentTitle(&quot;New mail from &quot; + sender.toString())
3584 * .setContentText(subject)
3585 * .setSmallIcon(R.drawable.new_mail)
3586 * .setLargeIcon(aBitmap)
3587 * .setStyle(new Notification.BigTextStyle()
3588 * .bigText(aVeryLongString))
3589 * .build();
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003590 * </pre>
Joe Malin8d40d042012-11-05 11:36:40 -08003591 *
Daniel Sandler4dfbe832012-04-11 14:51:46 -04003592 * @see Notification#bigContentView
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003593 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003594 public static class BigTextStyle extends Style {
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003595 private CharSequence mBigText;
3596
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003597 public BigTextStyle() {
3598 }
3599
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003600 public BigTextStyle(Builder builder) {
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003601 setBuilder(builder);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003602 }
3603
Chris Wrend6297db2012-05-03 16:20:13 -04003604 /**
3605 * Overrides ContentTitle in the big form of the template.
3606 * This defaults to the value passed to setContentTitle().
3607 */
3608 public BigTextStyle setBigContentTitle(CharSequence title) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04003609 internalSetBigContentTitle(safeCharSequence(title));
Chris Wrend6297db2012-05-03 16:20:13 -04003610 return this;
3611 }
3612
3613 /**
3614 * Set the first line of text after the detail section in the big form of the template.
3615 */
3616 public BigTextStyle setSummaryText(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04003617 internalSetSummaryText(safeCharSequence(cs));
Chris Wrend6297db2012-05-03 16:20:13 -04003618 return this;
3619 }
3620
Chris Wren0bd664d2012-08-01 13:56:56 -04003621 /**
3622 * Provide the longer text to be displayed in the big form of the
3623 * template in place of the content text.
3624 */
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003625 public BigTextStyle bigText(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04003626 mBigText = safeCharSequence(cs);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003627 return this;
3628 }
3629
Daniel Sandlerf45564e2013-04-15 15:05:08 -04003630 /**
3631 * @hide
3632 */
3633 public void addExtras(Bundle extras) {
3634 super.addExtras(extras);
3635
Christoph Studer4600f9b2014-07-22 22:44:43 +02003636 extras.putCharSequence(EXTRA_BIG_TEXT, mBigText);
3637 }
3638
3639 /**
3640 * @hide
3641 */
3642 @Override
3643 protected void restoreFromExtras(Bundle extras) {
3644 super.restoreFromExtras(extras);
3645
3646 mBigText = extras.getCharSequence(EXTRA_BIG_TEXT);
Daniel Sandlerf45564e2013-04-15 15:05:08 -04003647 }
3648
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003649 private RemoteViews makeBigContentView() {
Christoph Studer4600f9b2014-07-22 22:44:43 +02003650
3651 // Nasty
3652 CharSequence oldBuilderContentText = mBuilder.mContentText;
Daniel Sandler6387d2f2012-05-22 13:44:09 -04003653 mBuilder.mContentText = null;
Daniel Sandler916ad912012-06-13 12:17:07 -04003654
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01003655 RemoteViews contentView = getStandardView(mBuilder.getBigTextLayoutResource());
Joe Malin8d40d042012-11-05 11:36:40 -08003656
Christoph Studer4600f9b2014-07-22 22:44:43 +02003657 mBuilder.mContentText = oldBuilderContentText;
3658
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01003659 contentView.setTextViewText(R.id.big_text, mBuilder.processLegacyText(mBigText));
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003660 contentView.setViewVisibility(R.id.big_text, View.VISIBLE);
Chris Wren3c5f92432012-05-04 16:31:17 -04003661 contentView.setViewVisibility(R.id.text2, View.GONE);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003662
Jorim Jaggi445d3c02014-08-19 22:33:42 +02003663 applyTopPadding(contentView);
3664
Kenny Guy98193ea2014-07-24 19:54:37 +01003665 mBuilder.addProfileBadge(contentView, R.id.profile_badge_large_template);
3666
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003667 return contentView;
3668 }
3669
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04003670 /**
3671 * @hide
3672 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003673 @Override
Christoph Studer4600f9b2014-07-22 22:44:43 +02003674 public void populateBigContentView(Notification wip) {
3675 mBuilder.setBuilderBigContentView(wip, makeBigContentView());
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003676 }
3677 }
Daniel Sandler879c5e02012-04-17 16:46:51 -04003678
3679 /**
3680 * Helper class for generating large-format notifications that include a list of (up to 5) strings.
Joe Malin8d40d042012-11-05 11:36:40 -08003681 *
Robert Ly91c5ce32014-06-08 15:37:00 -07003682 * Here's how you'd set the <code>InboxStyle</code> on a notification:
Daniel Sandler879c5e02012-04-17 16:46:51 -04003683 * <pre class="prettyprint">
Robert Ly91c5ce32014-06-08 15:37:00 -07003684 * Notification notif = new Notification.Builder(mContext)
3685 * .setContentTitle(&quot;5 New mails from &quot; + sender.toString())
3686 * .setContentText(subject)
3687 * .setSmallIcon(R.drawable.new_mail)
3688 * .setLargeIcon(aBitmap)
3689 * .setStyle(new Notification.InboxStyle()
3690 * .addLine(str1)
3691 * .addLine(str2)
3692 * .setContentTitle(&quot;&quot;)
3693 * .setSummaryText(&quot;+3 more&quot;))
3694 * .build();
Daniel Sandler879c5e02012-04-17 16:46:51 -04003695 * </pre>
Joe Malin8d40d042012-11-05 11:36:40 -08003696 *
Daniel Sandler879c5e02012-04-17 16:46:51 -04003697 * @see Notification#bigContentView
3698 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003699 public static class InboxStyle extends Style {
Daniel Sandler879c5e02012-04-17 16:46:51 -04003700 private ArrayList<CharSequence> mTexts = new ArrayList<CharSequence>(5);
3701
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003702 public InboxStyle() {
3703 }
3704
Daniel Sandler879c5e02012-04-17 16:46:51 -04003705 public InboxStyle(Builder builder) {
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003706 setBuilder(builder);
Daniel Sandler879c5e02012-04-17 16:46:51 -04003707 }
3708
Chris Wrend6297db2012-05-03 16:20:13 -04003709 /**
3710 * Overrides ContentTitle in the big form of the template.
3711 * This defaults to the value passed to setContentTitle().
3712 */
3713 public InboxStyle setBigContentTitle(CharSequence title) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04003714 internalSetBigContentTitle(safeCharSequence(title));
Chris Wrend6297db2012-05-03 16:20:13 -04003715 return this;
3716 }
3717
3718 /**
3719 * Set the first line of text after the detail section in the big form of the template.
3720 */
3721 public InboxStyle setSummaryText(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04003722 internalSetSummaryText(safeCharSequence(cs));
Chris Wrend6297db2012-05-03 16:20:13 -04003723 return this;
3724 }
3725
Chris Wren0bd664d2012-08-01 13:56:56 -04003726 /**
3727 * Append a line to the digest section of the Inbox notification.
3728 */
Daniel Sandler879c5e02012-04-17 16:46:51 -04003729 public InboxStyle addLine(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04003730 mTexts.add(safeCharSequence(cs));
Daniel Sandler879c5e02012-04-17 16:46:51 -04003731 return this;
3732 }
3733
Daniel Sandlerf45564e2013-04-15 15:05:08 -04003734 /**
3735 * @hide
3736 */
3737 public void addExtras(Bundle extras) {
3738 super.addExtras(extras);
Christoph Studer4600f9b2014-07-22 22:44:43 +02003739
Daniel Sandlerf45564e2013-04-15 15:05:08 -04003740 CharSequence[] a = new CharSequence[mTexts.size()];
3741 extras.putCharSequenceArray(EXTRA_TEXT_LINES, mTexts.toArray(a));
3742 }
3743
Christoph Studer4600f9b2014-07-22 22:44:43 +02003744 /**
3745 * @hide
3746 */
3747 @Override
3748 protected void restoreFromExtras(Bundle extras) {
3749 super.restoreFromExtras(extras);
3750
3751 mTexts.clear();
3752 if (extras.containsKey(EXTRA_TEXT_LINES)) {
3753 Collections.addAll(mTexts, extras.getCharSequenceArray(EXTRA_TEXT_LINES));
3754 }
3755 }
3756
Daniel Sandler879c5e02012-04-17 16:46:51 -04003757 private RemoteViews makeBigContentView() {
Daniel Sandler619738c2012-06-07 16:33:08 -04003758 // Remove the content text so line3 disappears unless you have a summary
Christoph Studer4600f9b2014-07-22 22:44:43 +02003759
3760 // Nasty
3761 CharSequence oldBuilderContentText = mBuilder.mContentText;
Daniel Sandler619738c2012-06-07 16:33:08 -04003762 mBuilder.mContentText = null;
Christoph Studer4600f9b2014-07-22 22:44:43 +02003763
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01003764 RemoteViews contentView = getStandardView(mBuilder.getInboxLayoutResource());
Daniel Sandler619738c2012-06-07 16:33:08 -04003765
Christoph Studer4600f9b2014-07-22 22:44:43 +02003766 mBuilder.mContentText = oldBuilderContentText;
3767
Chris Wrend6297db2012-05-03 16:20:13 -04003768 contentView.setViewVisibility(R.id.text2, View.GONE);
Daniel Sandler879c5e02012-04-17 16:46:51 -04003769
Chris Wrend6297db2012-05-03 16:20:13 -04003770 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 -04003771 R.id.inbox_text4, R.id.inbox_text5, R.id.inbox_text6};
Chris Wrend6297db2012-05-03 16:20:13 -04003772
Chris Wren4ed80d52012-05-17 09:30:03 -04003773 // Make sure all rows are gone in case we reuse a view.
3774 for (int rowId : rowIds) {
3775 contentView.setViewVisibility(rowId, View.GONE);
3776 }
3777
Jorim Jaggi445d3c02014-08-19 22:33:42 +02003778 final boolean largeText =
3779 mBuilder.mContext.getResources().getConfiguration().fontScale > 1f;
3780 final float subTextSize = mBuilder.mContext.getResources().getDimensionPixelSize(
3781 R.dimen.notification_subtext_size);
Daniel Sandler879c5e02012-04-17 16:46:51 -04003782 int i=0;
3783 while (i < mTexts.size() && i < rowIds.length) {
3784 CharSequence str = mTexts.get(i);
3785 if (str != null && !str.equals("")) {
3786 contentView.setViewVisibility(rowIds[i], View.VISIBLE);
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01003787 contentView.setTextViewText(rowIds[i], mBuilder.processLegacyText(str));
Jorim Jaggi445d3c02014-08-19 22:33:42 +02003788 if (largeText) {
3789 contentView.setTextViewTextSize(rowIds[i], TypedValue.COMPLEX_UNIT_PX,
3790 subTextSize);
3791 }
Daniel Sandler879c5e02012-04-17 16:46:51 -04003792 }
3793 i++;
3794 }
3795
Chris Wren683ab002012-09-20 10:35:54 -04003796 contentView.setViewVisibility(R.id.inbox_end_pad,
3797 mTexts.size() > 0 ? View.VISIBLE : View.GONE);
3798
3799 contentView.setViewVisibility(R.id.inbox_more,
3800 mTexts.size() > rowIds.length ? View.VISIBLE : View.GONE);
Chris Wren29bb6d92012-05-17 18:09:42 -04003801
Jorim Jaggi445d3c02014-08-19 22:33:42 +02003802 applyTopPadding(contentView);
3803
Kenny Guy98193ea2014-07-24 19:54:37 +01003804 mBuilder.addProfileBadge(contentView, R.id.profile_badge_large_template);
3805
Daniel Sandler879c5e02012-04-17 16:46:51 -04003806 return contentView;
3807 }
3808
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04003809 /**
3810 * @hide
3811 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003812 @Override
Christoph Studer4600f9b2014-07-22 22:44:43 +02003813 public void populateBigContentView(Notification wip) {
3814 mBuilder.setBuilderBigContentView(wip, makeBigContentView());
Daniel Sandler879c5e02012-04-17 16:46:51 -04003815 }
3816 }
Dan Sandler842dd772014-05-15 09:36:47 -04003817
3818 /**
3819 * Notification style for media playback notifications.
3820 *
3821 * In the expanded form, {@link Notification#bigContentView}, up to 5
3822 * {@link Notification.Action}s specified with
3823 * {@link Notification.Builder#addAction(int, CharSequence, PendingIntent) addAction} will be
3824 * shown as icon-only pushbuttons, suitable for transport controls. The Bitmap given to
3825 * {@link Notification.Builder#setLargeIcon(android.graphics.Bitmap) setLargeIcon()} will be
3826 * treated as album artwork.
3827 *
3828 * Unlike the other styles provided here, MediaStyle can also modify the standard-size
3829 * {@link Notification#contentView}; by providing action indices to
3830 * {@link #setShowActionsInCompactView(int...)} you can promote up to 2 actions to be displayed
3831 * in the standard view alongside the usual content.
3832 *
Bryan Mawhinney6be8de32014-07-18 10:35:12 +01003833 * Notifications created with MediaStyle will have their category set to
3834 * {@link Notification#CATEGORY_TRANSPORT CATEGORY_TRANSPORT} unless you set a different
3835 * category using {@link Notification.Builder#setCategory(String) setCategory()}.
3836 *
Jeff Browndba34ba2014-06-24 20:46:03 -07003837 * Finally, if you attach a {@link android.media.session.MediaSession.Token} using
3838 * {@link android.app.Notification.MediaStyle#setMediaSession(MediaSession.Token)},
Dan Sandler842dd772014-05-15 09:36:47 -04003839 * the System UI can identify this as a notification representing an active media session
3840 * and respond accordingly (by showing album artwork in the lockscreen, for example).
3841 *
3842 * To use this style with your Notification, feed it to
3843 * {@link Notification.Builder#setStyle(android.app.Notification.Style)} like so:
3844 * <pre class="prettyprint">
3845 * Notification noti = new Notification.Builder()
3846 * .setSmallIcon(R.drawable.ic_stat_player)
3847 * .setContentTitle(&quot;Track title&quot;) // these three lines are optional
3848 * .setContentText(&quot;Artist - Album&quot;) // if you use
Bryan Mawhinney6be8de32014-07-18 10:35:12 +01003849 * .setLargeIcon(albumArtBitmap)) // setMediaSession(token)
3850 * .setStyle(<b>new Notification.MediaStyle()</b>
3851 * .setMediaSession(mySession))
Dan Sandler842dd772014-05-15 09:36:47 -04003852 * .build();
3853 * </pre>
3854 *
3855 * @see Notification#bigContentView
3856 */
3857 public static class MediaStyle extends Style {
3858 static final int MAX_MEDIA_BUTTONS_IN_COMPACT = 2;
3859 static final int MAX_MEDIA_BUTTONS = 5;
3860
3861 private int[] mActionsToShowInCompact = null;
Jeff Browndba34ba2014-06-24 20:46:03 -07003862 private MediaSession.Token mToken;
Dan Sandler842dd772014-05-15 09:36:47 -04003863
3864 public MediaStyle() {
3865 }
3866
3867 public MediaStyle(Builder builder) {
3868 setBuilder(builder);
3869 }
3870
3871 /**
3872 * Request up to 2 actions (by index in the order of addition) to be shown in the compact
3873 * notification view.
3874 */
3875 public MediaStyle setShowActionsInCompactView(int...actions) {
3876 mActionsToShowInCompact = actions;
3877 return this;
3878 }
3879
3880 /**
Jeff Browndba34ba2014-06-24 20:46:03 -07003881 * Attach a {@link android.media.session.MediaSession.Token} to this Notification
3882 * to provide additional playback information and control to the SystemUI.
Dan Sandler842dd772014-05-15 09:36:47 -04003883 */
Jeff Browndba34ba2014-06-24 20:46:03 -07003884 public MediaStyle setMediaSession(MediaSession.Token token) {
Dan Sandler842dd772014-05-15 09:36:47 -04003885 mToken = token;
3886 return this;
3887 }
3888
Christoph Studer4600f9b2014-07-22 22:44:43 +02003889 /**
3890 * @hide
3891 */
Dan Sandler842dd772014-05-15 09:36:47 -04003892 @Override
3893 public Notification buildStyled(Notification wip) {
Christoph Studer4600f9b2014-07-22 22:44:43 +02003894 super.buildStyled(wip);
Bryan Mawhinney6be8de32014-07-18 10:35:12 +01003895 if (wip.category == null) {
3896 wip.category = Notification.CATEGORY_TRANSPORT;
3897 }
Dan Sandler842dd772014-05-15 09:36:47 -04003898 return wip;
3899 }
3900
Christoph Studer4600f9b2014-07-22 22:44:43 +02003901 /**
3902 * @hide
3903 */
3904 @Override
3905 public void populateContentView(Notification wip) {
3906 mBuilder.setBuilderContentView(wip, makeMediaContentView());
3907 }
3908
3909 /**
3910 * @hide
3911 */
3912 @Override
3913 public void populateBigContentView(Notification wip) {
3914 mBuilder.setBuilderBigContentView(wip, makeMediaBigContentView());
3915 }
3916
Dan Sandler842dd772014-05-15 09:36:47 -04003917 /** @hide */
3918 @Override
3919 public void addExtras(Bundle extras) {
3920 super.addExtras(extras);
3921
3922 if (mToken != null) {
3923 extras.putParcelable(EXTRA_MEDIA_SESSION, mToken);
3924 }
Bryan Mawhinneye191f902014-07-22 12:50:09 +01003925 if (mActionsToShowInCompact != null) {
3926 extras.putIntArray(EXTRA_COMPACT_ACTIONS, mActionsToShowInCompact);
3927 }
Dan Sandler842dd772014-05-15 09:36:47 -04003928 }
3929
Christoph Studer4600f9b2014-07-22 22:44:43 +02003930 /**
3931 * @hide
3932 */
3933 @Override
3934 protected void restoreFromExtras(Bundle extras) {
3935 super.restoreFromExtras(extras);
3936
3937 if (extras.containsKey(EXTRA_MEDIA_SESSION)) {
3938 mToken = extras.getParcelable(EXTRA_MEDIA_SESSION);
3939 }
3940 if (extras.containsKey(EXTRA_COMPACT_ACTIONS)) {
3941 mActionsToShowInCompact = extras.getIntArray(EXTRA_COMPACT_ACTIONS);
3942 }
3943 }
3944
Dan Sandler842dd772014-05-15 09:36:47 -04003945 private RemoteViews generateMediaActionButton(Action action) {
3946 final boolean tombstone = (action.actionIntent == null);
3947 RemoteViews button = new RemoteViews(mBuilder.mContext.getPackageName(),
Alan Viverette3cb07a462014-06-06 14:19:53 -07003948 R.layout.notification_material_media_action);
Dan Sandler842dd772014-05-15 09:36:47 -04003949 button.setImageViewResource(R.id.action0, action.icon);
3950 if (!tombstone) {
3951 button.setOnClickPendingIntent(R.id.action0, action.actionIntent);
3952 }
3953 button.setContentDescription(R.id.action0, action.title);
3954 return button;
3955 }
3956
3957 private RemoteViews makeMediaContentView() {
3958 RemoteViews view = mBuilder.applyStandardTemplate(
Jorim Jaggi445d3c02014-08-19 22:33:42 +02003959 R.layout.notification_template_material_media);
Dan Sandler842dd772014-05-15 09:36:47 -04003960
3961 final int numActions = mBuilder.mActions.size();
3962 final int N = mActionsToShowInCompact == null
3963 ? 0
3964 : Math.min(mActionsToShowInCompact.length, MAX_MEDIA_BUTTONS_IN_COMPACT);
3965 if (N > 0) {
3966 view.removeAllViews(R.id.actions);
3967 for (int i = 0; i < N; i++) {
3968 if (i >= numActions) {
3969 throw new IllegalArgumentException(String.format(
3970 "setShowActionsInCompactView: action %d out of bounds (max %d)",
3971 i, numActions - 1));
3972 }
3973
3974 final Action action = mBuilder.mActions.get(mActionsToShowInCompact[i]);
3975 final RemoteViews button = generateMediaActionButton(action);
3976 view.addView(R.id.actions, button);
3977 }
3978 }
3979 return view;
3980 }
3981
3982 private RemoteViews makeMediaBigContentView() {
3983 RemoteViews big = mBuilder.applyStandardTemplate(
Jorim Jaggi445d3c02014-08-19 22:33:42 +02003984 R.layout.notification_template_material_big_media);
Dan Sandler842dd772014-05-15 09:36:47 -04003985
3986 final int N = Math.min(mBuilder.mActions.size(), MAX_MEDIA_BUTTONS);
3987 if (N > 0) {
3988 big.removeAllViews(R.id.actions);
3989 for (int i=0; i<N; i++) {
3990 final RemoteViews button = generateMediaActionButton(mBuilder.mActions.get(i));
3991 big.addView(R.id.actions, button);
3992 }
3993 }
3994 return big;
3995 }
3996 }
Griff Hazen61a9e862014-05-22 16:05:19 -07003997
Christoph Studer4600f9b2014-07-22 22:44:43 +02003998 // When adding a new Style subclass here, don't forget to update
3999 // Builder.getNotificationStyleClass.
4000
Griff Hazen61a9e862014-05-22 16:05:19 -07004001 /**
4002 * Extender interface for use with {@link Builder#extend}. Extenders may be used to add
4003 * metadata or change options on a notification builder.
4004 */
4005 public interface Extender {
4006 /**
4007 * Apply this extender to a notification builder.
4008 * @param builder the builder to be modified.
4009 * @return the build object for chaining.
4010 */
4011 public Builder extend(Builder builder);
4012 }
4013
4014 /**
4015 * Helper class to add wearable extensions to notifications.
4016 * <p class="note"> See
4017 * <a href="{@docRoot}wear/notifications/creating.html">Creating Notifications
4018 * for Android Wear</a> for more information on how to use this class.
4019 * <p>
4020 * To create a notification with wearable extensions:
4021 * <ol>
4022 * <li>Create a {@link android.app.Notification.Builder}, setting any desired
4023 * properties.
4024 * <li>Create a {@link android.app.Notification.WearableExtender}.
4025 * <li>Set wearable-specific properties using the
4026 * {@code add} and {@code set} methods of {@link android.app.Notification.WearableExtender}.
4027 * <li>Call {@link android.app.Notification.Builder#extend} to apply the extensions to a
4028 * notification.
4029 * <li>Post the notification to the notification system with the
4030 * {@code NotificationManager.notify(...)} methods.
4031 * </ol>
4032 *
4033 * <pre class="prettyprint">
4034 * Notification notif = new Notification.Builder(mContext)
4035 * .setContentTitle(&quot;New mail from &quot; + sender.toString())
4036 * .setContentText(subject)
4037 * .setSmallIcon(R.drawable.new_mail)
4038 * .extend(new Notification.WearableExtender()
4039 * .setContentIcon(R.drawable.new_mail))
4040 * .build();
4041 * NotificationManager notificationManger =
4042 * (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
4043 * notificationManger.notify(0, notif);</pre>
4044 *
4045 * <p>Wearable extensions can be accessed on an existing notification by using the
4046 * {@code WearableExtender(Notification)} constructor,
4047 * and then using the {@code get} methods to access values.
4048 *
4049 * <pre class="prettyprint">
4050 * Notification.WearableExtender wearableExtender = new Notification.WearableExtender(
4051 * notification);
Griff Hazen14f57992014-05-26 09:07:14 -07004052 * List&lt;Notification&gt; pages = wearableExtender.getPages();</pre>
Griff Hazen61a9e862014-05-22 16:05:19 -07004053 */
4054 public static final class WearableExtender implements Extender {
4055 /**
4056 * Sentinel value for an action index that is unset.
4057 */
4058 public static final int UNSET_ACTION_INDEX = -1;
4059
4060 /**
4061 * Size value for use with {@link #setCustomSizePreset} to show this notification with
4062 * default sizing.
4063 * <p>For custom display notifications created using {@link #setDisplayIntent},
4064 * the default is {@link #SIZE_LARGE}. All other notifications size automatically based
4065 * on their content.
4066 */
4067 public static final int SIZE_DEFAULT = 0;
4068
4069 /**
4070 * Size value for use with {@link #setCustomSizePreset} to show this notification
4071 * with an extra small size.
4072 * <p>This value is only applicable for custom display notifications created using
4073 * {@link #setDisplayIntent}.
4074 */
4075 public static final int SIZE_XSMALL = 1;
4076
4077 /**
4078 * Size value for use with {@link #setCustomSizePreset} to show this notification
4079 * with a small size.
4080 * <p>This value is only applicable for custom display notifications created using
4081 * {@link #setDisplayIntent}.
4082 */
4083 public static final int SIZE_SMALL = 2;
4084
4085 /**
4086 * Size value for use with {@link #setCustomSizePreset} to show this notification
4087 * with a medium size.
4088 * <p>This value is only applicable for custom display notifications created using
4089 * {@link #setDisplayIntent}.
4090 */
4091 public static final int SIZE_MEDIUM = 3;
4092
4093 /**
4094 * Size value for use with {@link #setCustomSizePreset} to show this notification
4095 * with a large size.
4096 * <p>This value is only applicable for custom display notifications created using
4097 * {@link #setDisplayIntent}.
4098 */
4099 public static final int SIZE_LARGE = 4;
4100
Griff Hazend5f11f92014-05-27 15:40:09 -07004101 /**
4102 * Size value for use with {@link #setCustomSizePreset} to show this notification
4103 * full screen.
4104 * <p>This value is only applicable for custom display notifications created using
4105 * {@link #setDisplayIntent}.
4106 */
4107 public static final int SIZE_FULL_SCREEN = 5;
4108
Griff Hazen61a9e862014-05-22 16:05:19 -07004109 /** Notification extra which contains wearable extensions */
4110 private static final String EXTRA_WEARABLE_EXTENSIONS = "android.wearable.EXTENSIONS";
4111
4112 // Keys within EXTRA_WEARABLE_OPTIONS for wearable options.
4113 private static final String KEY_ACTIONS = "actions";
4114 private static final String KEY_FLAGS = "flags";
4115 private static final String KEY_DISPLAY_INTENT = "displayIntent";
4116 private static final String KEY_PAGES = "pages";
4117 private static final String KEY_BACKGROUND = "background";
4118 private static final String KEY_CONTENT_ICON = "contentIcon";
4119 private static final String KEY_CONTENT_ICON_GRAVITY = "contentIconGravity";
4120 private static final String KEY_CONTENT_ACTION_INDEX = "contentActionIndex";
4121 private static final String KEY_CUSTOM_SIZE_PRESET = "customSizePreset";
4122 private static final String KEY_CUSTOM_CONTENT_HEIGHT = "customContentHeight";
4123 private static final String KEY_GRAVITY = "gravity";
4124
4125 // Flags bitwise-ored to mFlags
4126 private static final int FLAG_CONTENT_INTENT_AVAILABLE_OFFLINE = 0x1;
4127 private static final int FLAG_HINT_HIDE_ICON = 1 << 1;
4128 private static final int FLAG_HINT_SHOW_BACKGROUND_ONLY = 1 << 2;
4129 private static final int FLAG_START_SCROLL_BOTTOM = 1 << 3;
4130
4131 // Default value for flags integer
4132 private static final int DEFAULT_FLAGS = FLAG_CONTENT_INTENT_AVAILABLE_OFFLINE;
4133
4134 private static final int DEFAULT_CONTENT_ICON_GRAVITY = Gravity.END;
4135 private static final int DEFAULT_GRAVITY = Gravity.BOTTOM;
4136
4137 private ArrayList<Action> mActions = new ArrayList<Action>();
4138 private int mFlags = DEFAULT_FLAGS;
4139 private PendingIntent mDisplayIntent;
4140 private ArrayList<Notification> mPages = new ArrayList<Notification>();
4141 private Bitmap mBackground;
4142 private int mContentIcon;
4143 private int mContentIconGravity = DEFAULT_CONTENT_ICON_GRAVITY;
4144 private int mContentActionIndex = UNSET_ACTION_INDEX;
4145 private int mCustomSizePreset = SIZE_DEFAULT;
4146 private int mCustomContentHeight;
4147 private int mGravity = DEFAULT_GRAVITY;
4148
4149 /**
4150 * Create a {@link android.app.Notification.WearableExtender} with default
4151 * options.
4152 */
4153 public WearableExtender() {
4154 }
4155
4156 public WearableExtender(Notification notif) {
4157 Bundle wearableBundle = notif.extras.getBundle(EXTRA_WEARABLE_EXTENSIONS);
4158 if (wearableBundle != null) {
4159 List<Action> actions = wearableBundle.getParcelableArrayList(KEY_ACTIONS);
4160 if (actions != null) {
4161 mActions.addAll(actions);
4162 }
4163
4164 mFlags = wearableBundle.getInt(KEY_FLAGS, DEFAULT_FLAGS);
4165 mDisplayIntent = wearableBundle.getParcelable(KEY_DISPLAY_INTENT);
4166
4167 Notification[] pages = getNotificationArrayFromBundle(
4168 wearableBundle, KEY_PAGES);
4169 if (pages != null) {
4170 Collections.addAll(mPages, pages);
4171 }
4172
4173 mBackground = wearableBundle.getParcelable(KEY_BACKGROUND);
4174 mContentIcon = wearableBundle.getInt(KEY_CONTENT_ICON);
4175 mContentIconGravity = wearableBundle.getInt(KEY_CONTENT_ICON_GRAVITY,
4176 DEFAULT_CONTENT_ICON_GRAVITY);
4177 mContentActionIndex = wearableBundle.getInt(KEY_CONTENT_ACTION_INDEX,
4178 UNSET_ACTION_INDEX);
4179 mCustomSizePreset = wearableBundle.getInt(KEY_CUSTOM_SIZE_PRESET,
4180 SIZE_DEFAULT);
4181 mCustomContentHeight = wearableBundle.getInt(KEY_CUSTOM_CONTENT_HEIGHT);
4182 mGravity = wearableBundle.getInt(KEY_GRAVITY, DEFAULT_GRAVITY);
4183 }
4184 }
4185
4186 /**
4187 * Apply wearable extensions to a notification that is being built. This is typically
4188 * called by the {@link android.app.Notification.Builder#extend} method of
4189 * {@link android.app.Notification.Builder}.
4190 */
4191 @Override
4192 public Notification.Builder extend(Notification.Builder builder) {
4193 Bundle wearableBundle = new Bundle();
4194
4195 if (!mActions.isEmpty()) {
4196 wearableBundle.putParcelableArrayList(KEY_ACTIONS, mActions);
4197 }
4198 if (mFlags != DEFAULT_FLAGS) {
4199 wearableBundle.putInt(KEY_FLAGS, mFlags);
4200 }
4201 if (mDisplayIntent != null) {
4202 wearableBundle.putParcelable(KEY_DISPLAY_INTENT, mDisplayIntent);
4203 }
4204 if (!mPages.isEmpty()) {
4205 wearableBundle.putParcelableArray(KEY_PAGES, mPages.toArray(
4206 new Notification[mPages.size()]));
4207 }
4208 if (mBackground != null) {
4209 wearableBundle.putParcelable(KEY_BACKGROUND, mBackground);
4210 }
4211 if (mContentIcon != 0) {
4212 wearableBundle.putInt(KEY_CONTENT_ICON, mContentIcon);
4213 }
4214 if (mContentIconGravity != DEFAULT_CONTENT_ICON_GRAVITY) {
4215 wearableBundle.putInt(KEY_CONTENT_ICON_GRAVITY, mContentIconGravity);
4216 }
4217 if (mContentActionIndex != UNSET_ACTION_INDEX) {
4218 wearableBundle.putInt(KEY_CONTENT_ACTION_INDEX,
4219 mContentActionIndex);
4220 }
4221 if (mCustomSizePreset != SIZE_DEFAULT) {
4222 wearableBundle.putInt(KEY_CUSTOM_SIZE_PRESET, mCustomSizePreset);
4223 }
4224 if (mCustomContentHeight != 0) {
4225 wearableBundle.putInt(KEY_CUSTOM_CONTENT_HEIGHT, mCustomContentHeight);
4226 }
4227 if (mGravity != DEFAULT_GRAVITY) {
4228 wearableBundle.putInt(KEY_GRAVITY, mGravity);
4229 }
4230
4231 builder.getExtras().putBundle(EXTRA_WEARABLE_EXTENSIONS, wearableBundle);
4232 return builder;
4233 }
4234
4235 @Override
4236 public WearableExtender clone() {
4237 WearableExtender that = new WearableExtender();
4238 that.mActions = new ArrayList<Action>(this.mActions);
4239 that.mFlags = this.mFlags;
4240 that.mDisplayIntent = this.mDisplayIntent;
4241 that.mPages = new ArrayList<Notification>(this.mPages);
4242 that.mBackground = this.mBackground;
4243 that.mContentIcon = this.mContentIcon;
4244 that.mContentIconGravity = this.mContentIconGravity;
4245 that.mContentActionIndex = this.mContentActionIndex;
4246 that.mCustomSizePreset = this.mCustomSizePreset;
4247 that.mCustomContentHeight = this.mCustomContentHeight;
4248 that.mGravity = this.mGravity;
4249 return that;
4250 }
4251
4252 /**
4253 * Add a wearable action to this notification.
4254 *
4255 * <p>When wearable actions are added using this method, the set of actions that
4256 * show on a wearable device splits from devices that only show actions added
4257 * using {@link android.app.Notification.Builder#addAction}. This allows for customization
4258 * of which actions display on different devices.
4259 *
4260 * @param action the action to add to this notification
4261 * @return this object for method chaining
4262 * @see android.app.Notification.Action
4263 */
4264 public WearableExtender addAction(Action action) {
4265 mActions.add(action);
4266 return this;
4267 }
4268
4269 /**
4270 * Adds wearable actions to this notification.
4271 *
4272 * <p>When wearable actions are added using this method, the set of actions that
4273 * show on a wearable device splits from devices that only show actions added
4274 * using {@link android.app.Notification.Builder#addAction}. This allows for customization
4275 * of which actions display on different devices.
4276 *
4277 * @param actions the actions to add to this notification
4278 * @return this object for method chaining
4279 * @see android.app.Notification.Action
4280 */
4281 public WearableExtender addActions(List<Action> actions) {
4282 mActions.addAll(actions);
4283 return this;
4284 }
4285
4286 /**
4287 * Clear all wearable actions present on this builder.
4288 * @return this object for method chaining.
4289 * @see #addAction
4290 */
4291 public WearableExtender clearActions() {
4292 mActions.clear();
4293 return this;
4294 }
4295
4296 /**
4297 * Get the wearable actions present on this notification.
4298 */
4299 public List<Action> getActions() {
4300 return mActions;
4301 }
4302
4303 /**
4304 * Set an intent to launch inside of an activity view when displaying
Griff Hazen14f57992014-05-26 09:07:14 -07004305 * this notification. The {@link PendingIntent} provided should be for an activity.
4306 *
4307 * <pre class="prettyprint">
4308 * Intent displayIntent = new Intent(context, MyDisplayActivity.class);
4309 * PendingIntent displayPendingIntent = PendingIntent.getActivity(context,
4310 * 0, displayIntent, PendingIntent.FLAG_UPDATE_CURRENT);
4311 * Notification notif = new Notification.Builder(context)
4312 * .extend(new Notification.WearableExtender()
4313 * .setDisplayIntent(displayPendingIntent)
4314 * .setCustomSizePreset(Notification.WearableExtender.SIZE_MEDIUM))
4315 * .build();</pre>
4316 *
4317 * <p>The activity to launch needs to allow embedding, must be exported, and
Griff Hazen831ca9d2014-06-17 00:38:38 -07004318 * should have an empty task affinity. It is also recommended to use the device
4319 * default light theme.
Griff Hazen14f57992014-05-26 09:07:14 -07004320 *
4321 * <p>Example AndroidManifest.xml entry:
4322 * <pre class="prettyprint">
4323 * &lt;activity android:name=&quot;com.example.MyDisplayActivity&quot;
4324 * android:exported=&quot;true&quot;
4325 * android:allowEmbedded=&quot;true&quot;
Griff Hazen831ca9d2014-06-17 00:38:38 -07004326 * android:taskAffinity=&quot;&quot;
4327 * android:theme=&quot;@android:style/Theme.DeviceDefault.Light&quot; /&gt;</pre>
Griff Hazen61a9e862014-05-22 16:05:19 -07004328 *
4329 * @param intent the {@link PendingIntent} for an activity
4330 * @return this object for method chaining
4331 * @see android.app.Notification.WearableExtender#getDisplayIntent
4332 */
4333 public WearableExtender setDisplayIntent(PendingIntent intent) {
4334 mDisplayIntent = intent;
4335 return this;
4336 }
4337
4338 /**
4339 * Get the intent to launch inside of an activity view when displaying this
4340 * notification. This {@code PendingIntent} should be for an activity.
4341 */
4342 public PendingIntent getDisplayIntent() {
4343 return mDisplayIntent;
4344 }
4345
4346 /**
4347 * Add an additional page of content to display with this notification. The current
4348 * notification forms the first page, and pages added using this function form
4349 * subsequent pages. This field can be used to separate a notification into multiple
4350 * sections.
4351 *
4352 * @param page the notification to add as another page
4353 * @return this object for method chaining
4354 * @see android.app.Notification.WearableExtender#getPages
4355 */
4356 public WearableExtender addPage(Notification page) {
4357 mPages.add(page);
4358 return this;
4359 }
4360
4361 /**
4362 * Add additional pages of content to display with this notification. The current
4363 * notification forms the first page, and pages added using this function form
4364 * subsequent pages. This field can be used to separate a notification into multiple
4365 * sections.
4366 *
4367 * @param pages a list of notifications
4368 * @return this object for method chaining
4369 * @see android.app.Notification.WearableExtender#getPages
4370 */
4371 public WearableExtender addPages(List<Notification> pages) {
4372 mPages.addAll(pages);
4373 return this;
4374 }
4375
4376 /**
4377 * Clear all additional pages present on this builder.
4378 * @return this object for method chaining.
4379 * @see #addPage
4380 */
4381 public WearableExtender clearPages() {
4382 mPages.clear();
4383 return this;
4384 }
4385
4386 /**
4387 * Get the array of additional pages of content for displaying this notification. The
4388 * current notification forms the first page, and elements within this array form
4389 * subsequent pages. This field can be used to separate a notification into multiple
4390 * sections.
4391 * @return the pages for this notification
4392 */
4393 public List<Notification> getPages() {
4394 return mPages;
4395 }
4396
4397 /**
4398 * Set a background image to be displayed behind the notification content.
4399 * Contrary to the {@link android.app.Notification.BigPictureStyle}, this background
4400 * will work with any notification style.
4401 *
4402 * @param background the background bitmap
4403 * @return this object for method chaining
4404 * @see android.app.Notification.WearableExtender#getBackground
4405 */
4406 public WearableExtender setBackground(Bitmap background) {
4407 mBackground = background;
4408 return this;
4409 }
4410
4411 /**
4412 * Get a background image to be displayed behind the notification content.
4413 * Contrary to the {@link android.app.Notification.BigPictureStyle}, this background
4414 * will work with any notification style.
4415 *
4416 * @return the background image
4417 * @see android.app.Notification.WearableExtender#setBackground
4418 */
4419 public Bitmap getBackground() {
4420 return mBackground;
4421 }
4422
4423 /**
4424 * Set an icon that goes with the content of this notification.
4425 */
4426 public WearableExtender setContentIcon(int icon) {
4427 mContentIcon = icon;
4428 return this;
4429 }
4430
4431 /**
4432 * Get an icon that goes with the content of this notification.
4433 */
4434 public int getContentIcon() {
4435 return mContentIcon;
4436 }
4437
4438 /**
4439 * Set the gravity that the content icon should have within the notification display.
4440 * Supported values include {@link android.view.Gravity#START} and
4441 * {@link android.view.Gravity#END}. The default value is {@link android.view.Gravity#END}.
4442 * @see #setContentIcon
4443 */
4444 public WearableExtender setContentIconGravity(int contentIconGravity) {
4445 mContentIconGravity = contentIconGravity;
4446 return this;
4447 }
4448
4449 /**
4450 * Get the gravity that the content icon should have within the notification display.
4451 * Supported values include {@link android.view.Gravity#START} and
4452 * {@link android.view.Gravity#END}. The default value is {@link android.view.Gravity#END}.
4453 * @see #getContentIcon
4454 */
4455 public int getContentIconGravity() {
4456 return mContentIconGravity;
4457 }
4458
4459 /**
4460 * Set an action from this notification's actions to be clickable with the content of
Griff Hazen14f57992014-05-26 09:07:14 -07004461 * this notification. This action will no longer display separately from the
4462 * notification's content.
4463 *
Griff Hazenca48d352014-05-28 22:37:13 -07004464 * <p>For notifications with multiple pages, child pages can also have content actions
Griff Hazen14f57992014-05-26 09:07:14 -07004465 * set, although the list of available actions comes from the main notification and not
4466 * from the child page's notification.
4467 *
4468 * @param actionIndex The index of the action to hoist onto the current notification page.
4469 * If wearable actions were added to the main notification, this index
4470 * will apply to that list, otherwise it will apply to the regular
4471 * actions list.
Griff Hazen61a9e862014-05-22 16:05:19 -07004472 */
4473 public WearableExtender setContentAction(int actionIndex) {
4474 mContentActionIndex = actionIndex;
4475 return this;
4476 }
4477
4478 /**
Griff Hazenca48d352014-05-28 22:37:13 -07004479 * Get the index of the notification action, if any, that was specified as being clickable
4480 * with the content of this notification. This action will no longer display separately
Griff Hazen14f57992014-05-26 09:07:14 -07004481 * from the notification's content.
Griff Hazen61a9e862014-05-22 16:05:19 -07004482 *
Griff Hazenca48d352014-05-28 22:37:13 -07004483 * <p>For notifications with multiple pages, child pages can also have content actions
Griff Hazen14f57992014-05-26 09:07:14 -07004484 * set, although the list of available actions comes from the main notification and not
4485 * from the child page's notification.
4486 *
4487 * <p>If wearable specific actions were added to the main notification, this index will
4488 * apply to that list, otherwise it will apply to the regular actions list.
Griff Hazenca48d352014-05-28 22:37:13 -07004489 *
4490 * @return the action index or {@link #UNSET_ACTION_INDEX} if no action was selected.
Griff Hazen61a9e862014-05-22 16:05:19 -07004491 */
4492 public int getContentAction() {
4493 return mContentActionIndex;
4494 }
4495
4496 /**
4497 * Set the gravity that this notification should have within the available viewport space.
4498 * Supported values include {@link android.view.Gravity#TOP},
4499 * {@link android.view.Gravity#CENTER_VERTICAL} and {@link android.view.Gravity#BOTTOM}.
4500 * The default value is {@link android.view.Gravity#BOTTOM}.
4501 */
4502 public WearableExtender setGravity(int gravity) {
4503 mGravity = gravity;
4504 return this;
4505 }
4506
4507 /**
4508 * Get the gravity that this notification should have within the available viewport space.
4509 * Supported values include {@link android.view.Gravity#TOP},
4510 * {@link android.view.Gravity#CENTER_VERTICAL} and {@link android.view.Gravity#BOTTOM}.
4511 * The default value is {@link android.view.Gravity#BOTTOM}.
4512 */
4513 public int getGravity() {
4514 return mGravity;
4515 }
4516
4517 /**
4518 * Set the custom size preset for the display of this notification out of the available
4519 * presets found in {@link android.app.Notification.WearableExtender}, e.g.
4520 * {@link #SIZE_LARGE}.
4521 * <p>Some custom size presets are only applicable for custom display notifications created
4522 * using {@link android.app.Notification.WearableExtender#setDisplayIntent}. Check the
4523 * documentation for the preset in question. See also
4524 * {@link #setCustomContentHeight} and {@link #getCustomSizePreset}.
4525 */
4526 public WearableExtender setCustomSizePreset(int sizePreset) {
4527 mCustomSizePreset = sizePreset;
4528 return this;
4529 }
4530
4531 /**
4532 * Get the custom size preset for the display of this notification out of the available
4533 * presets found in {@link android.app.Notification.WearableExtender}, e.g.
4534 * {@link #SIZE_LARGE}.
4535 * <p>Some custom size presets are only applicable for custom display notifications created
4536 * using {@link #setDisplayIntent}. Check the documentation for the preset in question.
4537 * See also {@link #setCustomContentHeight} and {@link #setCustomSizePreset}.
4538 */
4539 public int getCustomSizePreset() {
4540 return mCustomSizePreset;
4541 }
4542
4543 /**
4544 * Set the custom height in pixels for the display of this notification's content.
4545 * <p>This option is only available for custom display notifications created
4546 * using {@link android.app.Notification.WearableExtender#setDisplayIntent}. See also
4547 * {@link android.app.Notification.WearableExtender#setCustomSizePreset} and
4548 * {@link #getCustomContentHeight}.
4549 */
4550 public WearableExtender setCustomContentHeight(int height) {
4551 mCustomContentHeight = height;
4552 return this;
4553 }
4554
4555 /**
4556 * Get the custom height in pixels for the display of this notification's content.
4557 * <p>This option is only available for custom display notifications created
4558 * using {@link #setDisplayIntent}. See also {@link #setCustomSizePreset} and
4559 * {@link #setCustomContentHeight}.
4560 */
4561 public int getCustomContentHeight() {
4562 return mCustomContentHeight;
4563 }
4564
4565 /**
4566 * Set whether the scrolling position for the contents of this notification should start
4567 * at the bottom of the contents instead of the top when the contents are too long to
4568 * display within the screen. Default is false (start scroll at the top).
4569 */
4570 public WearableExtender setStartScrollBottom(boolean startScrollBottom) {
4571 setFlag(FLAG_START_SCROLL_BOTTOM, startScrollBottom);
4572 return this;
4573 }
4574
4575 /**
4576 * Get whether the scrolling position for the contents of this notification should start
4577 * at the bottom of the contents instead of the top when the contents are too long to
4578 * display within the screen. Default is false (start scroll at the top).
4579 */
4580 public boolean getStartScrollBottom() {
4581 return (mFlags & FLAG_START_SCROLL_BOTTOM) != 0;
4582 }
4583
4584 /**
4585 * Set whether the content intent is available when the wearable device is not connected
4586 * to a companion device. The user can still trigger this intent when the wearable device
4587 * is offline, but a visual hint will indicate that the content intent may not be available.
4588 * Defaults to true.
4589 */
4590 public WearableExtender setContentIntentAvailableOffline(
4591 boolean contentIntentAvailableOffline) {
4592 setFlag(FLAG_CONTENT_INTENT_AVAILABLE_OFFLINE, contentIntentAvailableOffline);
4593 return this;
4594 }
4595
4596 /**
4597 * Get whether the content intent is available when the wearable device is not connected
4598 * to a companion device. The user can still trigger this intent when the wearable device
4599 * is offline, but a visual hint will indicate that the content intent may not be available.
4600 * Defaults to true.
4601 */
4602 public boolean getContentIntentAvailableOffline() {
4603 return (mFlags & FLAG_CONTENT_INTENT_AVAILABLE_OFFLINE) != 0;
4604 }
4605
4606 /**
4607 * Set a hint that this notification's icon should not be displayed.
4608 * @param hintHideIcon {@code true} to hide the icon, {@code false} otherwise.
4609 * @return this object for method chaining
4610 */
4611 public WearableExtender setHintHideIcon(boolean hintHideIcon) {
4612 setFlag(FLAG_HINT_HIDE_ICON, hintHideIcon);
4613 return this;
4614 }
4615
4616 /**
4617 * Get a hint that this notification's icon should not be displayed.
4618 * @return {@code true} if this icon should not be displayed, false otherwise.
4619 * The default value is {@code false} if this was never set.
4620 */
4621 public boolean getHintHideIcon() {
4622 return (mFlags & FLAG_HINT_HIDE_ICON) != 0;
4623 }
4624
4625 /**
4626 * Set a visual hint that only the background image of this notification should be
4627 * displayed, and other semantic content should be hidden. This hint is only applicable
4628 * to sub-pages added using {@link #addPage}.
4629 */
4630 public WearableExtender setHintShowBackgroundOnly(boolean hintShowBackgroundOnly) {
4631 setFlag(FLAG_HINT_SHOW_BACKGROUND_ONLY, hintShowBackgroundOnly);
4632 return this;
4633 }
4634
4635 /**
4636 * Get a visual hint that only the background image of this notification should be
4637 * displayed, and other semantic content should be hidden. This hint is only applicable
4638 * to sub-pages added using {@link android.app.Notification.WearableExtender#addPage}.
4639 */
4640 public boolean getHintShowBackgroundOnly() {
4641 return (mFlags & FLAG_HINT_SHOW_BACKGROUND_ONLY) != 0;
4642 }
4643
4644 private void setFlag(int mask, boolean value) {
4645 if (value) {
4646 mFlags |= mask;
4647 } else {
4648 mFlags &= ~mask;
4649 }
4650 }
4651 }
4652
4653 /**
4654 * Get an array of Notification objects from a parcelable array bundle field.
4655 * Update the bundle to have a typed array so fetches in the future don't need
4656 * to do an array copy.
4657 */
4658 private static Notification[] getNotificationArrayFromBundle(Bundle bundle, String key) {
4659 Parcelable[] array = bundle.getParcelableArray(key);
4660 if (array instanceof Notification[] || array == null) {
4661 return (Notification[]) array;
4662 }
4663 Notification[] typedArray = Arrays.copyOf(array, array.length,
4664 Notification[].class);
4665 bundle.putParcelableArray(key, typedArray);
4666 return typedArray;
4667 }
Christoph Studer4600f9b2014-07-22 22:44:43 +02004668
4669 private static class BuilderRemoteViews extends RemoteViews {
4670 public BuilderRemoteViews(Parcel parcel) {
4671 super(parcel);
4672 }
4673
Svet Ganov0da85b62014-08-06 14:11:37 -07004674 public BuilderRemoteViews(String packageName, int userId, int layoutId) {
4675 super(packageName, userId, layoutId);
Christoph Studer4600f9b2014-07-22 22:44:43 +02004676 }
4677
4678 @Override
4679 public BuilderRemoteViews clone() {
4680 Parcel p = Parcel.obtain();
4681 writeToParcel(p, 0);
4682 p.setDataPosition(0);
4683 BuilderRemoteViews brv = new BuilderRemoteViews(p);
4684 p.recycle();
4685 return brv;
4686 }
4687 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004688}