blob: 83e3b5fd1e95234fd593a15f726136598b9f482a [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;
Christoph Studer239f8352014-08-25 15:13:18 +020035import android.os.Build;
Daniel Sandler2561b0b2012-02-13 21:04:12 -050036import android.os.Bundle;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080037import android.os.Parcel;
38import android.os.Parcelable;
Daniel Sandlera2985ed2012-04-03 16:42:00 -040039import android.os.SystemClock;
Jeff Sharkey6d515712012-09-20 16:06:08 -070040import android.os.UserHandle;
Kenny Guy8a0101b2014-05-08 23:34:12 +010041import android.os.UserManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080042import android.text.TextUtils;
Daniel Sandlerdcbaf662013-04-26 16:23:09 -040043import android.util.Log;
Jorim Jaggi445d3c02014-08-19 22:33:42 +020044import android.util.MathUtils;
Daniel Sandler9f7936a2012-05-21 16:14:28 -040045import android.util.TypedValue;
Griff Hazen61a9e862014-05-22 16:05:19 -070046import android.view.Gravity;
Joe Onorato8595a3d2010-11-19 18:12:07 -080047import android.view.View;
Jeff Sharkey1c400132011-08-05 14:50:13 -070048import android.widget.ProgressBar;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080049import android.widget.RemoteViews;
50
Griff Hazen959591e2014-05-15 22:26:18 -070051import com.android.internal.R;
Griff Hazenc091ba82014-05-16 10:13:26 -070052import com.android.internal.util.NotificationColorUtil;
Griff Hazen959591e2014-05-15 22:26:18 -070053
Tor Norbyed9273d62013-05-30 15:59:53 -070054import java.lang.annotation.Retention;
55import java.lang.annotation.RetentionPolicy;
Christoph Studer4600f9b2014-07-22 22:44:43 +020056import java.lang.reflect.Constructor;
Andy Stadler110988c2010-12-03 14:29:16 -080057import java.text.NumberFormat;
Daniel Sandler2561b0b2012-02-13 21:04:12 -050058import java.util.ArrayList;
Griff Hazen61a9e862014-05-22 16:05:19 -070059import java.util.Arrays;
Griff Hazen5cadc3b2014-05-20 09:55:39 -070060import java.util.Collections;
Griff Hazen61a9e862014-05-22 16:05:19 -070061import java.util.List;
Joe Onorato561d3852010-11-20 18:09:34 -080062
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080063/**
64 * A class that represents how a persistent notification is to be presented to
65 * the user using the {@link android.app.NotificationManager}.
66 *
Joe Onoratocb109a02011-01-18 17:57:41 -080067 * <p>The {@link Notification.Builder Notification.Builder} has been added to make it
68 * easier to construct Notifications.</p>
69 *
Joe Fernandez558459f2011-10-13 16:47:36 -070070 * <div class="special reference">
71 * <h3>Developer Guides</h3>
72 * <p>For a guide to creating notifications, read the
73 * <a href="{@docRoot}guide/topics/ui/notifiers/notifications.html">Status Bar Notifications</a>
74 * developer guide.</p>
75 * </div>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080076 */
77public class Notification implements Parcelable
78{
Daniel Sandlerdcbaf662013-04-26 16:23:09 -040079 private static final String TAG = "Notification";
80
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080081 /**
Daniel Sandler01df1c62014-06-09 10:54:01 -040082 * An activity that provides a user interface for adjusting notification preferences for its
83 * containing application. Optional but recommended for apps that post
84 * {@link android.app.Notification Notifications}.
85 */
86 @SdkConstant(SdkConstantType.INTENT_CATEGORY)
87 public static final String INTENT_CATEGORY_NOTIFICATION_PREFERENCES
88 = "android.intent.category.NOTIFICATION_PREFERENCES";
89
90 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080091 * Use all default values (where applicable).
92 */
93 public static final int DEFAULT_ALL = ~0;
Daniel Sandler2561b0b2012-02-13 21:04:12 -050094
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080095 /**
96 * Use the default notification sound. This will ignore any given
97 * {@link #sound}.
Daniel Sandler2561b0b2012-02-13 21:04:12 -050098 *
Chris Wren47c20a12014-06-18 17:27:29 -040099 * <p>
100 * A notification that is noisy is more likely to be presented as a heads-up notification.
101 * </p>
102 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800103 * @see #defaults
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500104 */
105
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800106 public static final int DEFAULT_SOUND = 1;
107
108 /**
109 * Use the default notification vibrate. This will ignore any given
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500110 * {@link #vibrate}. Using phone vibration requires the
Scott Mainb8b36452009-04-26 15:50:49 -0700111 * {@link android.Manifest.permission#VIBRATE VIBRATE} permission.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500112 *
Chris Wren47c20a12014-06-18 17:27:29 -0400113 * <p>
114 * A notification that vibrates is more likely to be presented as a heads-up notification.
115 * </p>
116 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800117 * @see #defaults
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500118 */
119
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800120 public static final int DEFAULT_VIBRATE = 2;
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500121
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800122 /**
123 * Use the default notification lights. This will ignore the
124 * {@link #FLAG_SHOW_LIGHTS} bit, and {@link #ledARGB}, {@link #ledOffMS}, or
125 * {@link #ledOnMS}.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500126 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800127 * @see #defaults
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500128 */
129
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800130 public static final int DEFAULT_LIGHTS = 4;
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500131
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800132 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500133 * A timestamp related to this notification, in milliseconds since the epoch.
Joe Malin8d40d042012-11-05 11:36:40 -0800134 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500135 * Default value: {@link System#currentTimeMillis() Now}.
136 *
137 * Choose a timestamp that will be most relevant to the user. For most finite events, this
138 * corresponds to the time the event happened (or will happen, in the case of events that have
139 * yet to occur but about which the user is being informed). Indefinite events should be
Joe Malin8d40d042012-11-05 11:36:40 -0800140 * timestamped according to when the activity began.
141 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500142 * Some examples:
Joe Malin8d40d042012-11-05 11:36:40 -0800143 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500144 * <ul>
145 * <li>Notification of a new chat message should be stamped when the message was received.</li>
146 * <li>Notification of an ongoing file download (with a progress bar, for example) should be stamped when the download started.</li>
147 * <li>Notification of a completed file download should be stamped when the download finished.</li>
148 * <li>Notification of an upcoming meeting should be stamped with the time the meeting will begin (that is, in the future).</li>
149 * <li>Notification of an ongoing stopwatch (increasing timer) should be stamped with the watch's start time.
150 * <li>Notification of an ongoing countdown timer should be stamped with the timer's end time.
Joe Malin8d40d042012-11-05 11:36:40 -0800151 * </ul>
152 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800153 */
154 public long when;
155
156 /**
157 * The resource id of a drawable to use as the icon in the status bar.
Daniel Sandlerd952dae2011-02-07 16:33:36 -0500158 * This is required; notifications with an invalid icon resource will not be shown.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800159 */
160 public int icon;
161
162 /**
Joe Onorato46439ce2010-11-19 13:56:21 -0800163 * If the icon in the status bar is to have more than one level, you can set this. Otherwise,
164 * leave it at its default value of 0.
165 *
166 * @see android.widget.ImageView#setImageLevel
Griff Hazen959591e2014-05-15 22:26:18 -0700167 * @see android.graphics.drawable.Drawable#setLevel
Joe Onorato46439ce2010-11-19 13:56:21 -0800168 */
169 public int iconLevel;
170
171 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500172 * The number of events that this notification represents. For example, in a new mail
173 * notification, this could be the number of unread messages.
Joe Malin8d40d042012-11-05 11:36:40 -0800174 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500175 * The system may or may not use this field to modify the appearance of the notification. For
176 * example, before {@link android.os.Build.VERSION_CODES#HONEYCOMB}, this number was
177 * superimposed over the icon in the status bar. Starting with
178 * {@link android.os.Build.VERSION_CODES#HONEYCOMB}, the template used by
179 * {@link Notification.Builder} has displayed the number in the expanded notification view.
Joe Malin8d40d042012-11-05 11:36:40 -0800180 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500181 * If the number is 0 or negative, it is never shown.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800182 */
183 public int number;
184
185 /**
186 * The intent to execute when the expanded status entry is clicked. If
187 * this is an activity, it must include the
188 * {@link android.content.Intent#FLAG_ACTIVITY_NEW_TASK} flag, which requires
Scott Main7aee61f2011-02-08 11:25:01 -0800189 * that you take care of task management as described in the
190 * <a href="{@docRoot}guide/topics/fundamentals/tasks-and-back-stack.html">Tasks and Back
Dianne Hackborn6ceca582012-01-10 15:24:26 -0800191 * Stack</a> document. In particular, make sure to read the notification section
192 * <a href="{@docRoot}guide/topics/ui/notifiers/notifications.html#HandlingNotifications">Handling
193 * Notifications</a> for the correct ways to launch an application from a
194 * notification.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800195 */
196 public PendingIntent contentIntent;
197
198 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500199 * The intent to execute when the notification is explicitly dismissed by the user, either with
200 * the "Clear All" button or by swiping it away individually.
201 *
202 * This probably shouldn't be launching an activity since several of those will be sent
203 * at the same time.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800204 */
205 public PendingIntent deleteIntent;
206
207 /**
Dianne Hackborn170bae72010-09-03 15:14:28 -0700208 * An intent to launch instead of posting the notification to the status bar.
Joe Onoratocb109a02011-01-18 17:57:41 -0800209 *
Chris Wren47c20a12014-06-18 17:27:29 -0400210 * <p>
211 * The system UI may choose to display a heads-up notification, instead of
212 * launching this intent, while the user is using the device.
213 * </p>
214 *
Joe Onoratocb109a02011-01-18 17:57:41 -0800215 * @see Notification.Builder#setFullScreenIntent
Daniel Sandlere46cbd32010-06-17 10:35:26 -0400216 */
217 public PendingIntent fullScreenIntent;
218
219 /**
Dan Sandler5fcdf6e2014-07-18 11:31:15 -0400220 * Text that summarizes this notification for accessibility services.
221 *
222 * As of the L release, this text is no longer shown on screen, but it is still useful to
223 * accessibility services (where it serves as an audible announcement of the notification's
224 * appearance).
Joe Onoratoef1e7762010-09-17 18:38:38 -0400225 *
Joe Onorato46439ce2010-11-19 13:56:21 -0800226 * @see #tickerView
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800227 */
228 public CharSequence tickerText;
229
230 /**
Dan Sandler5fcdf6e2014-07-18 11:31:15 -0400231 * Formerly, a view showing the {@link #tickerText}.
232 *
233 * No longer displayed in the status bar as of API 21.
Joe Onoratoef1e7762010-09-17 18:38:38 -0400234 */
Dan Sandler5fcdf6e2014-07-18 11:31:15 -0400235 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -0800236 public RemoteViews tickerView;
Joe Onoratoef1e7762010-09-17 18:38:38 -0400237
238 /**
Daniel Sandlere46cbd32010-06-17 10:35:26 -0400239 * The view that will represent this notification in the expanded status bar.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800240 */
241 public RemoteViews contentView;
242
Daniel Sandlera0a938c2012-03-15 08:42:37 -0400243 /**
Daniel Sandler4dfbe832012-04-11 14:51:46 -0400244 * A large-format version of {@link #contentView}, giving the Notification an
Daniel Sandlerf3b73432012-03-27 15:01:25 -0400245 * opportunity to show more detail. The system UI may choose to show this
246 * instead of the normal content view at its discretion.
Daniel Sandlerf3b73432012-03-27 15:01:25 -0400247 */
248 public RemoteViews bigContentView;
249
Chris Wren8fd39ec2014-02-27 17:43:26 -0500250
251 /**
Chris Wren47c20a12014-06-18 17:27:29 -0400252 * A medium-format version of {@link #contentView}, providing the Notification an
253 * opportunity to add action buttons to contentView. At its discretion, the system UI may
254 * choose to show this as a heads-up notification, which will pop up so the user can see
255 * it without leaving their current activity.
Chris Wren8fd39ec2014-02-27 17:43:26 -0500256 */
257 public RemoteViews headsUpContentView;
258
Daniel Sandlerf3b73432012-03-27 15:01:25 -0400259 /**
Joe Onorato46439ce2010-11-19 13:56:21 -0800260 * The bitmap that may escape the bounds of the panel and bar.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800261 */
Joe Onorato46439ce2010-11-19 13:56:21 -0800262 public Bitmap largeIcon;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800263
264 /**
265 * The sound to play.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500266 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800267 * <p>
Chris Wren47c20a12014-06-18 17:27:29 -0400268 * A notification that is noisy is more likely to be presented as a heads-up notification.
269 * </p>
270 *
271 * <p>
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500272 * To play the default notification sound, see {@link #defaults}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800273 * </p>
274 */
275 public Uri sound;
276
277 /**
278 * Use this constant as the value for audioStreamType to request that
279 * the default stream type for notifications be used. Currently the
Jeff Sharkey098d5802012-04-26 17:30:34 -0700280 * default stream type is {@link AudioManager#STREAM_NOTIFICATION}.
John Spurlockc0650f022014-07-19 13:22:39 -0400281 *
282 * @deprecated Use {@link #audioAttributes} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800283 */
Jean-Michel Trivi81f871e2014-08-06 16:32:38 -0700284 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800285 public static final int STREAM_DEFAULT = -1;
286
287 /**
288 * The audio stream type to use when playing the sound.
289 * Should be one of the STREAM_ constants from
290 * {@link android.media.AudioManager}.
John Spurlockc0650f022014-07-19 13:22:39 -0400291 *
292 * @deprecated Use {@link #audioAttributes} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800293 */
Jean-Michel Trivi81f871e2014-08-06 16:32:38 -0700294 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800295 public int audioStreamType = STREAM_DEFAULT;
296
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800297 /**
John Spurlockc0650f022014-07-19 13:22:39 -0400298 * The default value of {@link #audioAttributes}.
299 */
300 public static final AudioAttributes AUDIO_ATTRIBUTES_DEFAULT = new AudioAttributes.Builder()
301 .setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
302 .setUsage(AudioAttributes.USAGE_NOTIFICATION)
303 .build();
304
305 /**
306 * The {@link AudioAttributes audio attributes} to use when playing the sound.
307 */
308 public AudioAttributes audioAttributes = AUDIO_ATTRIBUTES_DEFAULT;
309
310 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500311 * The pattern with which to vibrate.
312 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800313 * <p>
314 * To vibrate the default pattern, see {@link #defaults}.
315 * </p>
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500316 *
Chris Wren47c20a12014-06-18 17:27:29 -0400317 * <p>
318 * A notification that vibrates is more likely to be presented as a heads-up notification.
319 * </p>
320 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800321 * @see android.os.Vibrator#vibrate(long[],int)
322 */
323 public long[] vibrate;
324
325 /**
326 * The color of the led. The hardware will do its best approximation.
327 *
328 * @see #FLAG_SHOW_LIGHTS
329 * @see #flags
330 */
331 public int ledARGB;
332
333 /**
334 * The number of milliseconds for the LED to be on while it's flashing.
335 * The hardware will do its best approximation.
336 *
337 * @see #FLAG_SHOW_LIGHTS
338 * @see #flags
339 */
340 public int ledOnMS;
341
342 /**
343 * The number of milliseconds for the LED to be off while it's flashing.
344 * The hardware will do its best approximation.
345 *
346 * @see #FLAG_SHOW_LIGHTS
347 * @see #flags
348 */
349 public int ledOffMS;
350
351 /**
352 * Specifies which values should be taken from the defaults.
353 * <p>
354 * To set, OR the desired from {@link #DEFAULT_SOUND},
355 * {@link #DEFAULT_VIBRATE}, {@link #DEFAULT_LIGHTS}. For all default
356 * values, use {@link #DEFAULT_ALL}.
357 * </p>
358 */
359 public int defaults;
360
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800361 /**
362 * Bit to be bitwise-ored into the {@link #flags} field that should be
363 * set if you want the LED on for this notification.
364 * <ul>
365 * <li>To turn the LED off, pass 0 in the alpha channel for colorARGB
366 * or 0 for both ledOnMS and ledOffMS.</li>
367 * <li>To turn the LED on, pass 1 for ledOnMS and 0 for ledOffMS.</li>
368 * <li>To flash the LED, pass the number of milliseconds that it should
369 * be on and off to ledOnMS and ledOffMS.</li>
370 * </ul>
371 * <p>
372 * Since hardware varies, you are not guaranteed that any of the values
373 * you pass are honored exactly. Use the system defaults (TODO) if possible
374 * because they will be set to values that work on any given hardware.
375 * <p>
376 * The alpha channel must be set for forward compatibility.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500377 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800378 */
379 public static final int FLAG_SHOW_LIGHTS = 0x00000001;
380
381 /**
382 * Bit to be bitwise-ored into the {@link #flags} field that should be
383 * set if this notification is in reference to something that is ongoing,
384 * like a phone call. It should not be set if this notification is in
385 * reference to something that happened at a particular point in time,
386 * like a missed phone call.
387 */
388 public static final int FLAG_ONGOING_EVENT = 0x00000002;
389
390 /**
391 * Bit to be bitwise-ored into the {@link #flags} field that if set,
Scott Mainb8b36452009-04-26 15:50:49 -0700392 * the audio will be repeated until the notification is
393 * cancelled or the notification window is opened.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800394 */
395 public static final int FLAG_INSISTENT = 0x00000004;
396
397 /**
398 * Bit to be bitwise-ored into the {@link #flags} field that should be
Griff Hazen293977b2014-04-28 08:37:20 -0700399 * set if you would only like the sound, vibrate and ticker to be played
400 * if the notification was not already showing.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800401 */
402 public static final int FLAG_ONLY_ALERT_ONCE = 0x00000008;
403
404 /**
405 * Bit to be bitwise-ored into the {@link #flags} field that should be
406 * set if the notification should be canceled when it is clicked by the
Daniel Sandler8aa9ae62012-12-04 23:31:47 -0500407 * user.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500408
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800409 */
410 public static final int FLAG_AUTO_CANCEL = 0x00000010;
411
412 /**
413 * Bit to be bitwise-ored into the {@link #flags} field that should be
414 * set if the notification should not be canceled when the user clicks
415 * the Clear all button.
416 */
417 public static final int FLAG_NO_CLEAR = 0x00000020;
418
Dianne Hackbornd8a43f62009-08-17 23:33:56 -0700419 /**
420 * Bit to be bitwise-ored into the {@link #flags} field that should be
421 * set if this notification represents a currently running service. This
422 * will normally be set for you by {@link Service#startForeground}.
423 */
424 public static final int FLAG_FOREGROUND_SERVICE = 0x00000040;
425
Daniel Sandlere46cbd32010-06-17 10:35:26 -0400426 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500427 * Obsolete flag indicating high-priority notifications; use the priority field instead.
Joe Malin8d40d042012-11-05 11:36:40 -0800428 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500429 * @deprecated Use {@link #priority} with a positive value.
Daniel Sandlere46cbd32010-06-17 10:35:26 -0400430 */
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500431 public static final int FLAG_HIGH_PRIORITY = 0x00000080;
Daniel Sandlere46cbd32010-06-17 10:35:26 -0400432
Griff Hazendfcb0802014-02-11 12:00:00 -0800433 /**
434 * Bit to be bitswise-ored into the {@link #flags} field that should be
435 * set if this notification is relevant to the current device only
436 * and it is not recommended that it bridge to other devices.
437 */
438 public static final int FLAG_LOCAL_ONLY = 0x00000100;
439
Griff Hazen5cadc3b2014-05-20 09:55:39 -0700440 /**
441 * Bit to be bitswise-ored into the {@link #flags} field that should be
442 * set if this notification is the group summary for a group of notifications.
443 * Grouped notifications may display in a cluster or stack on devices which
444 * support such rendering. Requires a group key also be set using {@link Builder#setGroup}.
445 */
446 public static final int FLAG_GROUP_SUMMARY = 0x00000200;
447
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800448 public int flags;
449
Tor Norbyed9273d62013-05-30 15:59:53 -0700450 /** @hide */
451 @IntDef({PRIORITY_DEFAULT,PRIORITY_LOW,PRIORITY_MIN,PRIORITY_HIGH,PRIORITY_MAX})
452 @Retention(RetentionPolicy.SOURCE)
453 public @interface Priority {}
454
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800455 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500456 * Default notification {@link #priority}. If your application does not prioritize its own
457 * notifications, use this value for all notifications.
458 */
459 public static final int PRIORITY_DEFAULT = 0;
460
461 /**
462 * Lower {@link #priority}, for items that are less important. The UI may choose to show these
463 * items smaller, or at a different position in the list, compared with your app's
464 * {@link #PRIORITY_DEFAULT} items.
465 */
466 public static final int PRIORITY_LOW = -1;
467
468 /**
469 * Lowest {@link #priority}; these items might not be shown to the user except under special
470 * circumstances, such as detailed notification logs.
471 */
472 public static final int PRIORITY_MIN = -2;
473
474 /**
475 * Higher {@link #priority}, for more important notifications or alerts. The UI may choose to
476 * show these items larger, or at a different position in notification lists, compared with
477 * your app's {@link #PRIORITY_DEFAULT} items.
478 */
479 public static final int PRIORITY_HIGH = 1;
480
481 /**
482 * Highest {@link #priority}, for your application's most important items that require the
483 * user's prompt attention or input.
484 */
485 public static final int PRIORITY_MAX = 2;
486
487 /**
488 * Relative priority for this notification.
Joe Malin8d40d042012-11-05 11:36:40 -0800489 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500490 * Priority is an indication of how much of the user's valuable attention should be consumed by
491 * this notification. Low-priority notifications may be hidden from the user in certain
492 * situations, while the user might be interrupted for a higher-priority notification. The
Daniel Sandler6738eee2012-11-16 12:03:32 -0500493 * system will make a determination about how to interpret this priority when presenting
494 * the notification.
Chris Wren47c20a12014-06-18 17:27:29 -0400495 *
496 * <p>
497 * A notification that is at least {@link #PRIORITY_HIGH} is more likely to be presented
498 * as a heads-up notification.
499 * </p>
500 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500501 */
Tor Norbyed9273d62013-05-30 15:59:53 -0700502 @Priority
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500503 public int priority;
Joe Malin8d40d042012-11-05 11:36:40 -0800504
Dan Sandler26e81cf2014-05-06 10:01:27 -0400505 /**
506 * Accent color (an ARGB integer like the constants in {@link android.graphics.Color})
507 * to be applied by the standard Style templates when presenting this notification.
508 *
509 * The current template design constructs a colorful header image by overlaying the
510 * {@link #icon} image (stenciled in white) atop a field of this color. Alpha components are
511 * ignored.
512 */
513 public int color = COLOR_DEFAULT;
514
515 /**
516 * Special value of {@link #color} telling the system not to decorate this notification with
517 * any special color but instead use default colors when presenting this notification.
518 */
519 public static final int COLOR_DEFAULT = 0; // AKA Color.TRANSPARENT
Dan Sandler0bf2ed82013-12-21 23:33:41 -0600520
521 /**
522 * Sphere of visibility of this notification, which affects how and when the SystemUI reveals
523 * the notification's presence and contents in untrusted situations (namely, on the secure
524 * lockscreen).
525 *
526 * The default level, {@link #VISIBILITY_PRIVATE}, behaves exactly as notifications have always
527 * done on Android: The notification's {@link #icon} and {@link #tickerText} (if available) are
528 * shown in all situations, but the contents are only available if the device is unlocked for
529 * the appropriate user.
530 *
531 * A more permissive policy can be expressed by {@link #VISIBILITY_PUBLIC}; such a notification
532 * can be read even in an "insecure" context (that is, above a secure lockscreen).
533 * To modify the public version of this notification—for example, to redact some portions—see
534 * {@link Builder#setPublicVersion(Notification)}.
535 *
536 * Finally, a notification can be made {@link #VISIBILITY_SECRET}, which will suppress its icon
537 * and ticker until the user has bypassed the lockscreen.
538 */
539 public int visibility;
540
541 public static final int VISIBILITY_PUBLIC = 1;
542 public static final int VISIBILITY_PRIVATE = 0;
543 public static final int VISIBILITY_SECRET = -1;
544
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500545 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400546 * Notification category: incoming call (voice or video) or similar synchronous communication request.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500547 */
John Spurlockfd7f1e02014-03-18 16:41:57 -0400548 public static final String CATEGORY_CALL = "call";
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500549
550 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400551 * Notification category: incoming direct message (SMS, instant message, etc.).
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500552 */
John Spurlockfd7f1e02014-03-18 16:41:57 -0400553 public static final String CATEGORY_MESSAGE = "msg";
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500554
555 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400556 * Notification category: asynchronous bulk message (email).
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500557 */
John Spurlockfd7f1e02014-03-18 16:41:57 -0400558 public static final String CATEGORY_EMAIL = "email";
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500559
560 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400561 * Notification category: calendar event.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500562 */
John Spurlockfd7f1e02014-03-18 16:41:57 -0400563 public static final String CATEGORY_EVENT = "event";
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500564
565 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400566 * Notification category: promotion or advertisement.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500567 */
John Spurlockfd7f1e02014-03-18 16:41:57 -0400568 public static final String CATEGORY_PROMO = "promo";
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500569
570 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400571 * Notification category: alarm or timer.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500572 */
John Spurlockfd7f1e02014-03-18 16:41:57 -0400573 public static final String CATEGORY_ALARM = "alarm";
574
575 /**
576 * Notification category: progress of a long-running background operation.
577 */
578 public static final String CATEGORY_PROGRESS = "progress";
579
580 /**
581 * Notification category: social network or sharing update.
582 */
583 public static final String CATEGORY_SOCIAL = "social";
584
585 /**
586 * Notification category: error in background operation or authentication status.
587 */
588 public static final String CATEGORY_ERROR = "err";
589
590 /**
591 * Notification category: media transport control for playback.
592 */
593 public static final String CATEGORY_TRANSPORT = "transport";
594
595 /**
596 * Notification category: system or device status update. Reserved for system use.
597 */
598 public static final String CATEGORY_SYSTEM = "sys";
599
600 /**
601 * Notification category: indication of running background service.
602 */
603 public static final String CATEGORY_SERVICE = "service";
604
605 /**
John Spurlock0a69c8c2014-03-21 13:30:57 -0400606 * Notification category: a specific, timely recommendation for a single thing.
607 * For example, a news app might want to recommend a news story it believes the user will
608 * want to read next.
609 */
610 public static final String CATEGORY_RECOMMENDATION = "recommendation";
611
612 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400613 * Notification category: ongoing information about device or contextual status.
614 */
615 public static final String CATEGORY_STATUS = "status";
616
617 /**
618 * One of the predefined notification categories (see the <code>CATEGORY_*</code> constants)
619 * that best describes this Notification. May be used by the system for ranking and filtering.
620 */
621 public String category;
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500622
Griff Hazen5cadc3b2014-05-20 09:55:39 -0700623 private String mGroupKey;
624
625 /**
626 * Get the key used to group this notification into a cluster or stack
627 * with other notifications on devices which support such rendering.
628 */
629 public String getGroup() {
630 return mGroupKey;
631 }
632
633 private String mSortKey;
634
635 /**
636 * Get a sort key that orders this notification among other notifications from the
637 * same package. This can be useful if an external sort was already applied and an app
638 * would like to preserve this. Notifications will be sorted lexicographically using this
639 * value, although providing different priorities in addition to providing sort key may
640 * cause this value to be ignored.
641 *
642 * <p>This sort key can also be used to order members of a notification group. See
643 * {@link Builder#setGroup}.
644 *
645 * @see String#compareTo(String)
646 */
647 public String getSortKey() {
648 return mSortKey;
649 }
650
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500651 /**
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400652 * Additional semantic data to be carried around with this Notification.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400653 * <p>
654 * The extras keys defined here are intended to capture the original inputs to {@link Builder}
655 * APIs, and are intended to be used by
656 * {@link android.service.notification.NotificationListenerService} implementations to extract
657 * detailed information from notification objects.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500658 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400659 public Bundle extras = new Bundle();
660
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400661 /**
662 * {@link #extras} key: this is the title of the notification,
663 * as supplied to {@link Builder#setContentTitle(CharSequence)}.
664 */
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -0500665 public static final String EXTRA_TITLE = "android.title";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400666
667 /**
668 * {@link #extras} key: this is the title of the notification when shown in expanded form,
669 * e.g. as supplied to {@link BigTextStyle#setBigContentTitle(CharSequence)}.
670 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400671 public static final String EXTRA_TITLE_BIG = EXTRA_TITLE + ".big";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400672
673 /**
674 * {@link #extras} key: this is the main text payload, as supplied to
675 * {@link Builder#setContentText(CharSequence)}.
676 */
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -0500677 public static final String EXTRA_TEXT = "android.text";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400678
679 /**
680 * {@link #extras} key: this is a third line of text, as supplied to
681 * {@link Builder#setSubText(CharSequence)}.
682 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400683 public static final String EXTRA_SUB_TEXT = "android.subText";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400684
685 /**
686 * {@link #extras} key: this is a small piece of additional text as supplied to
687 * {@link Builder#setContentInfo(CharSequence)}.
688 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400689 public static final String EXTRA_INFO_TEXT = "android.infoText";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400690
691 /**
692 * {@link #extras} key: this is a line of summary information intended to be shown
693 * alongside expanded notifications, as supplied to (e.g.)
694 * {@link BigTextStyle#setSummaryText(CharSequence)}.
695 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400696 public static final String EXTRA_SUMMARY_TEXT = "android.summaryText";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400697
698 /**
Christoph Studer4600f9b2014-07-22 22:44:43 +0200699 * {@link #extras} key: this is the longer text shown in the big form of a
700 * {@link BigTextStyle} notification, as supplied to
701 * {@link BigTextStyle#bigText(CharSequence)}.
702 */
703 public static final String EXTRA_BIG_TEXT = "android.bigText";
704
705 /**
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400706 * {@link #extras} key: this is the resource ID of the notification's main small icon, as
707 * supplied to {@link Builder#setSmallIcon(int)}.
708 */
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -0500709 public static final String EXTRA_SMALL_ICON = "android.icon";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400710
711 /**
712 * {@link #extras} key: this is a bitmap to be used instead of the small icon when showing the
713 * notification payload, as
714 * supplied to {@link Builder#setLargeIcon(android.graphics.Bitmap)}.
715 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400716 public static final String EXTRA_LARGE_ICON = "android.largeIcon";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400717
718 /**
719 * {@link #extras} key: this is a bitmap to be used instead of the one from
720 * {@link Builder#setLargeIcon(android.graphics.Bitmap)} when the notification is
721 * shown in its expanded form, as supplied to
722 * {@link BigPictureStyle#bigLargeIcon(android.graphics.Bitmap)}.
723 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400724 public static final String EXTRA_LARGE_ICON_BIG = EXTRA_LARGE_ICON + ".big";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400725
726 /**
727 * {@link #extras} key: this is the progress value supplied to
728 * {@link Builder#setProgress(int, int, boolean)}.
729 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400730 public static final String EXTRA_PROGRESS = "android.progress";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400731
732 /**
733 * {@link #extras} key: this is the maximum value supplied to
734 * {@link Builder#setProgress(int, int, boolean)}.
735 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400736 public static final String EXTRA_PROGRESS_MAX = "android.progressMax";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400737
738 /**
739 * {@link #extras} key: whether the progress bar is indeterminate, supplied to
740 * {@link Builder#setProgress(int, int, boolean)}.
741 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400742 public static final String EXTRA_PROGRESS_INDETERMINATE = "android.progressIndeterminate";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400743
744 /**
745 * {@link #extras} key: whether {@link #when} should be shown as a count-up timer (specifically
746 * a {@link android.widget.Chronometer}) instead of a timestamp, as supplied to
747 * {@link Builder#setUsesChronometer(boolean)}.
748 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400749 public static final String EXTRA_SHOW_CHRONOMETER = "android.showChronometer";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400750
751 /**
752 * {@link #extras} key: whether {@link #when} should be shown,
753 * as supplied to {@link Builder#setShowWhen(boolean)}.
754 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400755 public static final String EXTRA_SHOW_WHEN = "android.showWhen";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400756
757 /**
758 * {@link #extras} key: this is a bitmap to be shown in {@link BigPictureStyle} expanded
759 * notifications, supplied to {@link BigPictureStyle#bigPicture(android.graphics.Bitmap)}.
760 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400761 public static final String EXTRA_PICTURE = "android.picture";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400762
763 /**
764 * {@link #extras} key: An array of CharSequences to show in {@link InboxStyle} expanded
765 * notifications, each of which was supplied to {@link InboxStyle#addLine(CharSequence)}.
766 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400767 public static final String EXTRA_TEXT_LINES = "android.textLines";
Dan Sandler842dd772014-05-15 09:36:47 -0400768
769 /**
770 * {@link #extras} key: A string representing the name of the specific
771 * {@link android.app.Notification.Style} used to create this notification.
772 */
Chris Wren91ad5632013-06-05 15:05:57 -0400773 public static final String EXTRA_TEMPLATE = "android.template";
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400774
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400775 /**
776 * {@link #extras} key: An array of people that this notification relates to, specified
777 * by contacts provider contact URI.
778 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400779 public static final String EXTRA_PEOPLE = "android.people";
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500780
781 /**
Chris Wren47c20a12014-06-18 17:27:29 -0400782 * {@link #extras} key: used to provide hints about the appropriateness of
783 * displaying this notification as a heads-up notification.
Chris Wren51c75102013-07-16 20:49:17 -0400784 * @hide
785 */
786 public static final String EXTRA_AS_HEADS_UP = "headsup";
787
788 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400789 * Allow certain system-generated notifications to appear before the device is provisioned.
790 * Only available to notifications coming from the android package.
791 * @hide
792 */
793 public static final String EXTRA_ALLOW_DURING_SETUP = "android.allowDuringSetup";
794
795 /**
Jose Limae9e3b3b2014-05-18 23:44:50 -0700796 * {@link #extras} key: A
797 * {@link android.content.ContentUris content URI} pointing to an image that can be displayed
798 * in the background when the notification is selected. The URI must point to an image stream
799 * suitable for passing into
800 * {@link android.graphics.BitmapFactory#decodeStream(java.io.InputStream)
801 * BitmapFactory.decodeStream}; all other content types will be ignored. The content provider
802 * URI used for this purpose must require no permissions to read the image data.
803 */
804 public static final String EXTRA_BACKGROUND_IMAGE_URI = "android.backgroundImageUri";
805
806 /**
Dan Sandler842dd772014-05-15 09:36:47 -0400807 * {@link #extras} key: A
Jeff Browndba34ba2014-06-24 20:46:03 -0700808 * {@link android.media.session.MediaSession.Token} associated with a
Dan Sandler842dd772014-05-15 09:36:47 -0400809 * {@link android.app.Notification.MediaStyle} notification.
810 */
811 public static final String EXTRA_MEDIA_SESSION = "android.mediaSession";
812
813 /**
Bryan Mawhinneye191f902014-07-22 12:50:09 +0100814 * {@link #extras} key: the indices of actions to be shown in the compact view,
815 * as supplied to (e.g.) {@link MediaStyle#setShowActionsInCompactView(int...)}.
816 */
817 public static final String EXTRA_COMPACT_ACTIONS = "android.compactActions";
818
Christoph Studer943aa672014-08-03 20:31:16 +0200819 /**
Christoph Studer7ac80e62014-08-04 16:01:57 +0200820 * {@link #extras} key: the user that built the notification.
Christoph Studer943aa672014-08-03 20:31:16 +0200821 *
822 * @hide
823 */
Christoph Studer7ac80e62014-08-04 16:01:57 +0200824 public static final String EXTRA_ORIGINATING_USERID = "android.originatingUserId";
Christoph Studer943aa672014-08-03 20:31:16 +0200825
Bryan Mawhinneye191f902014-07-22 12:50:09 +0100826 /**
Chris Wren47c20a12014-06-18 17:27:29 -0400827 * Value for {@link #EXTRA_AS_HEADS_UP} that indicates this notification should not be
828 * displayed in the heads up space.
829 *
830 * <p>
831 * If this notification has a {@link #fullScreenIntent}, then it will always launch the
832 * full-screen intent when posted.
833 * </p>
Chris Wren51c75102013-07-16 20:49:17 -0400834 * @hide
835 */
836 public static final int HEADS_UP_NEVER = 0;
837
838 /**
Chris Wren47c20a12014-06-18 17:27:29 -0400839 * Default value for {@link #EXTRA_AS_HEADS_UP} that indicates this notification may be
840 * displayed as a heads up.
Chris Wren51c75102013-07-16 20:49:17 -0400841 * @hide
842 */
843 public static final int HEADS_UP_ALLOWED = 1;
844
845 /**
Chris Wren47c20a12014-06-18 17:27:29 -0400846 * Value for {@link #EXTRA_AS_HEADS_UP} that indicates this notification is a
847 * good candidate for display as a heads up.
Chris Wren51c75102013-07-16 20:49:17 -0400848 * @hide
849 */
850 public static final int HEADS_UP_REQUESTED = 2;
851
852 /**
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400853 * Structure to encapsulate a named action that can be shown as part of this notification.
854 * It must include an icon, a label, and a {@link PendingIntent} to be fired when the action is
855 * selected by the user.
856 * <p>
Griff Hazen959591e2014-05-15 22:26:18 -0700857 * Apps should use {@link Notification.Builder#addAction(int, CharSequence, PendingIntent)}
858 * or {@link Notification.Builder#addAction(Notification.Action)}
859 * to attach actions.
Daniel Sandlera0a938c2012-03-15 08:42:37 -0400860 */
Daniel Sandlerea2a3172013-02-20 22:24:20 -0500861 public static class Action implements Parcelable {
Griff Hazen959591e2014-05-15 22:26:18 -0700862 private final Bundle mExtras;
Griff Hazen61a9e862014-05-22 16:05:19 -0700863 private final RemoteInput[] mRemoteInputs;
Griff Hazen959591e2014-05-15 22:26:18 -0700864
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400865 /**
866 * Small icon representing the action.
867 */
Daniel Sandlera0a938c2012-03-15 08:42:37 -0400868 public int icon;
Griff Hazen5cadc3b2014-05-20 09:55:39 -0700869
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400870 /**
871 * Title of the action.
872 */
Daniel Sandlera0a938c2012-03-15 08:42:37 -0400873 public CharSequence title;
Griff Hazen5cadc3b2014-05-20 09:55:39 -0700874
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400875 /**
876 * Intent to send when the user invokes this action. May be null, in which case the action
877 * may be rendered in a disabled presentation by the system UI.
878 */
Daniel Sandlera0a938c2012-03-15 08:42:37 -0400879 public PendingIntent actionIntent;
Griff Hazen959591e2014-05-15 22:26:18 -0700880
Daniel Sandlera0a938c2012-03-15 08:42:37 -0400881 private Action(Parcel in) {
882 icon = in.readInt();
883 title = TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(in);
884 if (in.readInt() == 1) {
885 actionIntent = PendingIntent.CREATOR.createFromParcel(in);
886 }
Griff Hazen959591e2014-05-15 22:26:18 -0700887 mExtras = in.readBundle();
Griff Hazen5cadc3b2014-05-20 09:55:39 -0700888 mRemoteInputs = in.createTypedArray(RemoteInput.CREATOR);
Daniel Sandlera0a938c2012-03-15 08:42:37 -0400889 }
Griff Hazen5cadc3b2014-05-20 09:55:39 -0700890
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400891 /**
Griff Hazen959591e2014-05-15 22:26:18 -0700892 * Use {@link Notification.Builder#addAction(int, CharSequence, PendingIntent)}.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400893 */
894 public Action(int icon, CharSequence title, PendingIntent intent) {
Griff Hazen5cadc3b2014-05-20 09:55:39 -0700895 this(icon, title, intent, new Bundle(), null);
Griff Hazen959591e2014-05-15 22:26:18 -0700896 }
897
Griff Hazen5cadc3b2014-05-20 09:55:39 -0700898 private Action(int icon, CharSequence title, PendingIntent intent, Bundle extras,
899 RemoteInput[] remoteInputs) {
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400900 this.icon = icon;
901 this.title = title;
902 this.actionIntent = intent;
Griff Hazen959591e2014-05-15 22:26:18 -0700903 this.mExtras = extras != null ? extras : new Bundle();
Griff Hazen5cadc3b2014-05-20 09:55:39 -0700904 this.mRemoteInputs = remoteInputs;
Griff Hazen959591e2014-05-15 22:26:18 -0700905 }
906
907 /**
908 * Get additional metadata carried around with this Action.
909 */
910 public Bundle getExtras() {
911 return mExtras;
912 }
913
914 /**
Griff Hazen5cadc3b2014-05-20 09:55:39 -0700915 * Get the list of inputs to be collected from the user when this action is sent.
916 * May return null if no remote inputs were added.
917 */
918 public RemoteInput[] getRemoteInputs() {
919 return mRemoteInputs;
920 }
921
922 /**
Griff Hazen959591e2014-05-15 22:26:18 -0700923 * Builder class for {@link Action} objects.
924 */
Griff Hazen5cadc3b2014-05-20 09:55:39 -0700925 public static final class Builder {
Griff Hazen959591e2014-05-15 22:26:18 -0700926 private final int mIcon;
927 private final CharSequence mTitle;
928 private final PendingIntent mIntent;
929 private final Bundle mExtras;
Griff Hazen5cadc3b2014-05-20 09:55:39 -0700930 private ArrayList<RemoteInput> mRemoteInputs;
Griff Hazen959591e2014-05-15 22:26:18 -0700931
932 /**
933 * Construct a new builder for {@link Action} object.
934 * @param icon icon to show for this action
935 * @param title the title of the action
936 * @param intent the {@link PendingIntent} to fire when users trigger this action
937 */
938 public Builder(int icon, CharSequence title, PendingIntent intent) {
Griff Hazen5cadc3b2014-05-20 09:55:39 -0700939 this(icon, title, intent, new Bundle(), null);
Griff Hazen959591e2014-05-15 22:26:18 -0700940 }
941
942 /**
943 * Construct a new builder for {@link Action} object using the fields from an
944 * {@link Action}.
945 * @param action the action to read fields from.
946 */
947 public Builder(Action action) {
Griff Hazen5cadc3b2014-05-20 09:55:39 -0700948 this(action.icon, action.title, action.actionIntent, new Bundle(action.mExtras),
949 action.getRemoteInputs());
Griff Hazen959591e2014-05-15 22:26:18 -0700950 }
951
Griff Hazen5cadc3b2014-05-20 09:55:39 -0700952 private Builder(int icon, CharSequence title, PendingIntent intent, Bundle extras,
953 RemoteInput[] remoteInputs) {
Griff Hazen959591e2014-05-15 22:26:18 -0700954 mIcon = icon;
955 mTitle = title;
956 mIntent = intent;
957 mExtras = extras;
Griff Hazen5cadc3b2014-05-20 09:55:39 -0700958 if (remoteInputs != null) {
959 mRemoteInputs = new ArrayList<RemoteInput>(remoteInputs.length);
960 Collections.addAll(mRemoteInputs, remoteInputs);
961 }
Griff Hazen959591e2014-05-15 22:26:18 -0700962 }
963
964 /**
965 * Merge additional metadata into this builder.
966 *
967 * <p>Values within the Bundle will replace existing extras values in this Builder.
968 *
969 * @see Notification.Action#extras
970 */
971 public Builder addExtras(Bundle extras) {
972 if (extras != null) {
973 mExtras.putAll(extras);
974 }
975 return this;
976 }
977
978 /**
979 * Get the metadata Bundle used by this Builder.
980 *
981 * <p>The returned Bundle is shared with this Builder.
982 */
983 public Bundle getExtras() {
984 return mExtras;
985 }
986
987 /**
Griff Hazen5cadc3b2014-05-20 09:55:39 -0700988 * Add an input to be collected from the user when this action is sent.
989 * Response values can be retrieved from the fired intent by using the
990 * {@link RemoteInput#getResultsFromIntent} function.
991 * @param remoteInput a {@link RemoteInput} to add to the action
992 * @return this object for method chaining
993 */
994 public Builder addRemoteInput(RemoteInput remoteInput) {
995 if (mRemoteInputs == null) {
996 mRemoteInputs = new ArrayList<RemoteInput>();
997 }
998 mRemoteInputs.add(remoteInput);
999 return this;
1000 }
1001
1002 /**
1003 * Apply an extender to this action builder. Extenders may be used to add
1004 * metadata or change options on this builder.
1005 */
Griff Hazen61a9e862014-05-22 16:05:19 -07001006 public Builder extend(Extender extender) {
1007 extender.extend(this);
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001008 return this;
1009 }
1010
1011 /**
Griff Hazen959591e2014-05-15 22:26:18 -07001012 * Combine all of the options that have been set and return a new {@link Action}
1013 * object.
1014 * @return the built action
1015 */
1016 public Action build() {
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001017 RemoteInput[] remoteInputs = mRemoteInputs != null
1018 ? mRemoteInputs.toArray(new RemoteInput[mRemoteInputs.size()]) : null;
1019 return new Action(mIcon, mTitle, mIntent, mExtras, remoteInputs);
Griff Hazen959591e2014-05-15 22:26:18 -07001020 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001021 }
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001022
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001023 @Override
1024 public Action clone() {
1025 return new Action(
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001026 icon,
1027 title,
1028 actionIntent, // safe to alias
1029 new Bundle(mExtras),
1030 getRemoteInputs());
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001031 }
1032 @Override
1033 public int describeContents() {
1034 return 0;
1035 }
1036 @Override
1037 public void writeToParcel(Parcel out, int flags) {
1038 out.writeInt(icon);
1039 TextUtils.writeToParcel(title, out, flags);
1040 if (actionIntent != null) {
1041 out.writeInt(1);
1042 actionIntent.writeToParcel(out, flags);
1043 } else {
1044 out.writeInt(0);
1045 }
Griff Hazen959591e2014-05-15 22:26:18 -07001046 out.writeBundle(mExtras);
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001047 out.writeTypedArray(mRemoteInputs, flags);
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001048 }
Griff Hazen959591e2014-05-15 22:26:18 -07001049 public static final Parcelable.Creator<Action> CREATOR =
1050 new Parcelable.Creator<Action>() {
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001051 public Action createFromParcel(Parcel in) {
1052 return new Action(in);
1053 }
1054 public Action[] newArray(int size) {
1055 return new Action[size];
1056 }
1057 };
Griff Hazen61a9e862014-05-22 16:05:19 -07001058
1059 /**
1060 * Extender interface for use with {@link Builder#extend}. Extenders may be used to add
1061 * metadata or change options on an action builder.
1062 */
1063 public interface Extender {
1064 /**
1065 * Apply this extender to a notification action builder.
1066 * @param builder the builder to be modified.
1067 * @return the build object for chaining.
1068 */
1069 public Builder extend(Builder builder);
1070 }
1071
1072 /**
1073 * Wearable extender for notification actions. To add extensions to an action,
1074 * create a new {@link android.app.Notification.Action.WearableExtender} object using
1075 * the {@code WearableExtender()} constructor and apply it to a
1076 * {@link android.app.Notification.Action.Builder} using
1077 * {@link android.app.Notification.Action.Builder#extend}.
1078 *
1079 * <pre class="prettyprint">
1080 * Notification.Action action = new Notification.Action.Builder(
1081 * R.drawable.archive_all, "Archive all", actionIntent)
Griff Hazen14f57992014-05-26 09:07:14 -07001082 * .extend(new Notification.Action.WearableExtender()
Griff Hazen61a9e862014-05-22 16:05:19 -07001083 * .setAvailableOffline(false))
Griff Hazen14f57992014-05-26 09:07:14 -07001084 * .build();</pre>
Griff Hazen61a9e862014-05-22 16:05:19 -07001085 */
1086 public static final class WearableExtender implements Extender {
1087 /** Notification action extra which contains wearable extensions */
1088 private static final String EXTRA_WEARABLE_EXTENSIONS = "android.wearable.EXTENSIONS";
1089
1090 private static final String KEY_FLAGS = "flags";
1091
1092 // Flags bitwise-ored to mFlags
1093 private static final int FLAG_AVAILABLE_OFFLINE = 0x1;
1094
1095 // Default value for flags integer
1096 private static final int DEFAULT_FLAGS = FLAG_AVAILABLE_OFFLINE;
1097
1098 private int mFlags = DEFAULT_FLAGS;
1099
1100 /**
1101 * Create a {@link android.app.Notification.Action.WearableExtender} with default
1102 * options.
1103 */
1104 public WearableExtender() {
1105 }
1106
1107 /**
1108 * Create a {@link android.app.Notification.Action.WearableExtender} by reading
1109 * wearable options present in an existing notification action.
1110 * @param action the notification action to inspect.
1111 */
1112 public WearableExtender(Action action) {
1113 Bundle wearableBundle = action.getExtras().getBundle(EXTRA_WEARABLE_EXTENSIONS);
1114 if (wearableBundle != null) {
1115 mFlags = wearableBundle.getInt(KEY_FLAGS, DEFAULT_FLAGS);
1116 }
1117 }
1118
1119 /**
1120 * Apply wearable extensions to a notification action that is being built. This is
1121 * typically called by the {@link android.app.Notification.Action.Builder#extend}
1122 * method of {@link android.app.Notification.Action.Builder}.
1123 */
1124 @Override
1125 public Action.Builder extend(Action.Builder builder) {
1126 Bundle wearableBundle = new Bundle();
1127
1128 if (mFlags != DEFAULT_FLAGS) {
1129 wearableBundle.putInt(KEY_FLAGS, mFlags);
1130 }
1131
1132 builder.getExtras().putBundle(EXTRA_WEARABLE_EXTENSIONS, wearableBundle);
1133 return builder;
1134 }
1135
1136 @Override
1137 public WearableExtender clone() {
1138 WearableExtender that = new WearableExtender();
1139 that.mFlags = this.mFlags;
1140 return that;
1141 }
1142
1143 /**
1144 * Set whether this action is available when the wearable device is not connected to
1145 * a companion device. The user can still trigger this action when the wearable device is
1146 * offline, but a visual hint will indicate that the action may not be available.
1147 * Defaults to true.
1148 */
1149 public WearableExtender setAvailableOffline(boolean availableOffline) {
1150 setFlag(FLAG_AVAILABLE_OFFLINE, availableOffline);
1151 return this;
1152 }
1153
1154 /**
1155 * Get whether this action is available when the wearable device is not connected to
1156 * a companion device. The user can still trigger this action when the wearable device is
1157 * offline, but a visual hint will indicate that the action may not be available.
1158 * Defaults to true.
1159 */
1160 public boolean isAvailableOffline() {
1161 return (mFlags & FLAG_AVAILABLE_OFFLINE) != 0;
1162 }
1163
1164 private void setFlag(int mask, boolean value) {
1165 if (value) {
1166 mFlags |= mask;
1167 } else {
1168 mFlags &= ~mask;
1169 }
1170 }
1171 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001172 }
1173
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001174 /**
1175 * Array of all {@link Action} structures attached to this notification by
1176 * {@link Builder#addAction(int, CharSequence, PendingIntent)}. Mostly useful for instances of
1177 * {@link android.service.notification.NotificationListenerService} that provide an alternative
1178 * interface for invoking actions.
1179 */
Daniel Sandlerea2a3172013-02-20 22:24:20 -05001180 public Action[] actions;
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001181
1182 /**
Dan Sandler0bf2ed82013-12-21 23:33:41 -06001183 * Replacement version of this notification whose content will be shown
1184 * in an insecure context such as atop a secure keyguard. See {@link #visibility}
1185 * and {@link #VISIBILITY_PUBLIC}.
1186 */
1187 public Notification publicVersion;
1188
1189 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001190 * Constructs a Notification object with default values.
Joe Onorato46439ce2010-11-19 13:56:21 -08001191 * You might want to consider using {@link Builder} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001192 */
1193 public Notification()
1194 {
1195 this.when = System.currentTimeMillis();
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001196 this.priority = PRIORITY_DEFAULT;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001197 }
1198
1199 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001200 * @hide
1201 */
1202 public Notification(Context context, int icon, CharSequence tickerText, long when,
1203 CharSequence contentTitle, CharSequence contentText, Intent contentIntent)
1204 {
1205 this.when = when;
1206 this.icon = icon;
1207 this.tickerText = tickerText;
1208 setLatestEventInfo(context, contentTitle, contentText,
1209 PendingIntent.getActivity(context, 0, contentIntent, 0));
1210 }
1211
1212 /**
1213 * Constructs a Notification object with the information needed to
1214 * have a status bar icon without the standard expanded view.
1215 *
1216 * @param icon The resource id of the icon to put in the status bar.
1217 * @param tickerText The text that flows by in the status bar when the notification first
1218 * activates.
1219 * @param when The time to show in the time field. In the System.currentTimeMillis
1220 * timebase.
Joe Onorato46439ce2010-11-19 13:56:21 -08001221 *
1222 * @deprecated Use {@link Builder} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001223 */
Joe Onorato46439ce2010-11-19 13:56:21 -08001224 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001225 public Notification(int icon, CharSequence tickerText, long when)
1226 {
1227 this.icon = icon;
1228 this.tickerText = tickerText;
1229 this.when = when;
1230 }
1231
1232 /**
1233 * Unflatten the notification from a parcel.
1234 */
1235 public Notification(Parcel parcel)
1236 {
1237 int version = parcel.readInt();
1238
1239 when = parcel.readLong();
1240 icon = parcel.readInt();
1241 number = parcel.readInt();
1242 if (parcel.readInt() != 0) {
1243 contentIntent = PendingIntent.CREATOR.createFromParcel(parcel);
1244 }
1245 if (parcel.readInt() != 0) {
1246 deleteIntent = PendingIntent.CREATOR.createFromParcel(parcel);
1247 }
1248 if (parcel.readInt() != 0) {
1249 tickerText = TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(parcel);
1250 }
1251 if (parcel.readInt() != 0) {
Joe Onorato46439ce2010-11-19 13:56:21 -08001252 tickerView = RemoteViews.CREATOR.createFromParcel(parcel);
Joe Onoratoef1e7762010-09-17 18:38:38 -04001253 }
1254 if (parcel.readInt() != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001255 contentView = RemoteViews.CREATOR.createFromParcel(parcel);
1256 }
Joe Onorato561d3852010-11-20 18:09:34 -08001257 if (parcel.readInt() != 0) {
1258 largeIcon = Bitmap.CREATOR.createFromParcel(parcel);
1259 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001260 defaults = parcel.readInt();
1261 flags = parcel.readInt();
1262 if (parcel.readInt() != 0) {
1263 sound = Uri.CREATOR.createFromParcel(parcel);
1264 }
1265
1266 audioStreamType = parcel.readInt();
John Spurlockc0650f022014-07-19 13:22:39 -04001267 if (parcel.readInt() != 0) {
1268 audioAttributes = AudioAttributes.CREATOR.createFromParcel(parcel);
1269 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001270 vibrate = parcel.createLongArray();
1271 ledARGB = parcel.readInt();
1272 ledOnMS = parcel.readInt();
1273 ledOffMS = parcel.readInt();
1274 iconLevel = parcel.readInt();
Daniel Sandlere46cbd32010-06-17 10:35:26 -04001275
1276 if (parcel.readInt() != 0) {
1277 fullScreenIntent = PendingIntent.CREATOR.createFromParcel(parcel);
1278 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001279
1280 priority = parcel.readInt();
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001281
John Spurlockfd7f1e02014-03-18 16:41:57 -04001282 category = parcel.readString();
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001283
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001284 mGroupKey = parcel.readString();
1285
1286 mSortKey = parcel.readString();
1287
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001288 extras = parcel.readBundle(); // may be null
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001289
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001290 actions = parcel.createTypedArray(Action.CREATOR); // may be null
1291
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001292 if (parcel.readInt() != 0) {
Daniel Sandlerf3b73432012-03-27 15:01:25 -04001293 bigContentView = RemoteViews.CREATOR.createFromParcel(parcel);
1294 }
Dan Sandler0bf2ed82013-12-21 23:33:41 -06001295
Chris Wren8fd39ec2014-02-27 17:43:26 -05001296 if (parcel.readInt() != 0) {
1297 headsUpContentView = RemoteViews.CREATOR.createFromParcel(parcel);
1298 }
1299
Dan Sandler0bf2ed82013-12-21 23:33:41 -06001300 visibility = parcel.readInt();
1301
1302 if (parcel.readInt() != 0) {
1303 publicVersion = Notification.CREATOR.createFromParcel(parcel);
1304 }
Dan Sandler26e81cf2014-05-06 10:01:27 -04001305
1306 color = parcel.readInt();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001307 }
1308
Andy Stadler110988c2010-12-03 14:29:16 -08001309 @Override
Joe Onorato18e69df2010-05-17 22:26:12 -07001310 public Notification clone() {
1311 Notification that = new Notification();
Daniel Sandler1a497d32013-04-18 14:52:45 -04001312 cloneInto(that, true);
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001313 return that;
1314 }
Joe Onorato18e69df2010-05-17 22:26:12 -07001315
Daniel Sandler1a497d32013-04-18 14:52:45 -04001316 /**
1317 * Copy all (or if heavy is false, all except Bitmaps and RemoteViews) members
1318 * of this into that.
1319 * @hide
1320 */
1321 public void cloneInto(Notification that, boolean heavy) {
Joe Onorato18e69df2010-05-17 22:26:12 -07001322 that.when = this.when;
1323 that.icon = this.icon;
1324 that.number = this.number;
1325
1326 // PendingIntents are global, so there's no reason (or way) to clone them.
1327 that.contentIntent = this.contentIntent;
1328 that.deleteIntent = this.deleteIntent;
Daniel Sandlere46cbd32010-06-17 10:35:26 -04001329 that.fullScreenIntent = this.fullScreenIntent;
Joe Onorato18e69df2010-05-17 22:26:12 -07001330
1331 if (this.tickerText != null) {
1332 that.tickerText = this.tickerText.toString();
1333 }
Daniel Sandler1a497d32013-04-18 14:52:45 -04001334 if (heavy && this.tickerView != null) {
Joe Onorato46439ce2010-11-19 13:56:21 -08001335 that.tickerView = this.tickerView.clone();
Joe Onoratoef1e7762010-09-17 18:38:38 -04001336 }
Daniel Sandler1a497d32013-04-18 14:52:45 -04001337 if (heavy && this.contentView != null) {
Joe Onorato18e69df2010-05-17 22:26:12 -07001338 that.contentView = this.contentView.clone();
1339 }
Daniel Sandler1a497d32013-04-18 14:52:45 -04001340 if (heavy && this.largeIcon != null) {
Joe Onorato561d3852010-11-20 18:09:34 -08001341 that.largeIcon = Bitmap.createBitmap(this.largeIcon);
1342 }
Jozef BABJAKa8b91832011-02-22 08:05:08 +01001343 that.iconLevel = this.iconLevel;
Joe Onorato18e69df2010-05-17 22:26:12 -07001344 that.sound = this.sound; // android.net.Uri is immutable
1345 that.audioStreamType = this.audioStreamType;
John Spurlockc0650f022014-07-19 13:22:39 -04001346 if (this.audioAttributes != null) {
1347 that.audioAttributes = new AudioAttributes.Builder(this.audioAttributes).build();
1348 }
Joe Onorato18e69df2010-05-17 22:26:12 -07001349
1350 final long[] vibrate = this.vibrate;
1351 if (vibrate != null) {
1352 final int N = vibrate.length;
1353 final long[] vib = that.vibrate = new long[N];
1354 System.arraycopy(vibrate, 0, vib, 0, N);
1355 }
1356
1357 that.ledARGB = this.ledARGB;
1358 that.ledOnMS = this.ledOnMS;
1359 that.ledOffMS = this.ledOffMS;
1360 that.defaults = this.defaults;
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001361
Joe Onorato18e69df2010-05-17 22:26:12 -07001362 that.flags = this.flags;
1363
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001364 that.priority = this.priority;
Joe Malin8d40d042012-11-05 11:36:40 -08001365
John Spurlockfd7f1e02014-03-18 16:41:57 -04001366 that.category = this.category;
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001367
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001368 that.mGroupKey = this.mGroupKey;
1369
1370 that.mSortKey = this.mSortKey;
1371
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001372 if (this.extras != null) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04001373 try {
1374 that.extras = new Bundle(this.extras);
1375 // will unparcel
1376 that.extras.size();
1377 } catch (BadParcelableException e) {
1378 Log.e(TAG, "could not unparcel extras from notification: " + this, e);
1379 that.extras = null;
1380 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001381 }
1382
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001383 if (this.actions != null) {
1384 that.actions = new Action[this.actions.length];
1385 for(int i=0; i<this.actions.length; i++) {
1386 that.actions[i] = this.actions[i].clone();
1387 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001388 }
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001389
Daniel Sandler1a497d32013-04-18 14:52:45 -04001390 if (heavy && this.bigContentView != null) {
Daniel Sandlerf3b73432012-03-27 15:01:25 -04001391 that.bigContentView = this.bigContentView.clone();
1392 }
Daniel Sandler1a497d32013-04-18 14:52:45 -04001393
Chris Wren8fd39ec2014-02-27 17:43:26 -05001394 if (heavy && this.headsUpContentView != null) {
1395 that.headsUpContentView = this.headsUpContentView.clone();
1396 }
1397
Dan Sandler0bf2ed82013-12-21 23:33:41 -06001398 that.visibility = this.visibility;
1399
1400 if (this.publicVersion != null) {
1401 that.publicVersion = new Notification();
1402 this.publicVersion.cloneInto(that.publicVersion, heavy);
1403 }
1404
Dan Sandler26e81cf2014-05-06 10:01:27 -04001405 that.color = this.color;
1406
Daniel Sandler1a497d32013-04-18 14:52:45 -04001407 if (!heavy) {
1408 that.lightenPayload(); // will clean out extras
1409 }
1410 }
1411
1412 /**
1413 * Removes heavyweight parts of the Notification object for archival or for sending to
1414 * listeners when the full contents are not necessary.
1415 * @hide
1416 */
1417 public final void lightenPayload() {
1418 tickerView = null;
1419 contentView = null;
1420 bigContentView = null;
Chris Wren8fd39ec2014-02-27 17:43:26 -05001421 headsUpContentView = null;
Daniel Sandler1a497d32013-04-18 14:52:45 -04001422 largeIcon = null;
1423 if (extras != null) {
1424 extras.remove(Notification.EXTRA_LARGE_ICON);
1425 extras.remove(Notification.EXTRA_LARGE_ICON_BIG);
1426 extras.remove(Notification.EXTRA_PICTURE);
Christoph Studerb82bc782014-08-20 14:29:43 +02001427 // Prevent light notifications from being rebuilt.
1428 extras.remove(Builder.EXTRA_NEEDS_REBUILD);
Daniel Sandler1a497d32013-04-18 14:52:45 -04001429 }
Joe Onorato18e69df2010-05-17 22:26:12 -07001430 }
1431
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04001432 /**
1433 * Make sure this CharSequence is safe to put into a bundle, which basically
1434 * means it had better not be some custom Parcelable implementation.
1435 * @hide
1436 */
1437 public static CharSequence safeCharSequence(CharSequence cs) {
1438 if (cs instanceof Parcelable) {
1439 Log.e(TAG, "warning: " + cs.getClass().getCanonicalName()
1440 + " instance is a custom Parcelable and not allowed in Notification");
1441 return cs.toString();
1442 }
1443
1444 return cs;
1445 }
1446
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001447 public int describeContents() {
1448 return 0;
1449 }
1450
1451 /**
1452 * Flatten this notification from a parcel.
1453 */
1454 public void writeToParcel(Parcel parcel, int flags)
1455 {
1456 parcel.writeInt(1);
1457
1458 parcel.writeLong(when);
1459 parcel.writeInt(icon);
1460 parcel.writeInt(number);
1461 if (contentIntent != null) {
1462 parcel.writeInt(1);
1463 contentIntent.writeToParcel(parcel, 0);
1464 } else {
1465 parcel.writeInt(0);
1466 }
1467 if (deleteIntent != null) {
1468 parcel.writeInt(1);
1469 deleteIntent.writeToParcel(parcel, 0);
1470 } else {
1471 parcel.writeInt(0);
1472 }
1473 if (tickerText != null) {
1474 parcel.writeInt(1);
1475 TextUtils.writeToParcel(tickerText, parcel, flags);
1476 } else {
1477 parcel.writeInt(0);
1478 }
Joe Onorato46439ce2010-11-19 13:56:21 -08001479 if (tickerView != null) {
Joe Onoratoef1e7762010-09-17 18:38:38 -04001480 parcel.writeInt(1);
Joe Onorato46439ce2010-11-19 13:56:21 -08001481 tickerView.writeToParcel(parcel, 0);
Joe Onoratoef1e7762010-09-17 18:38:38 -04001482 } else {
1483 parcel.writeInt(0);
1484 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001485 if (contentView != null) {
1486 parcel.writeInt(1);
1487 contentView.writeToParcel(parcel, 0);
1488 } else {
1489 parcel.writeInt(0);
1490 }
Joe Onorato561d3852010-11-20 18:09:34 -08001491 if (largeIcon != null) {
1492 parcel.writeInt(1);
1493 largeIcon.writeToParcel(parcel, 0);
1494 } else {
1495 parcel.writeInt(0);
1496 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001497
1498 parcel.writeInt(defaults);
1499 parcel.writeInt(this.flags);
1500
1501 if (sound != null) {
1502 parcel.writeInt(1);
1503 sound.writeToParcel(parcel, 0);
1504 } else {
1505 parcel.writeInt(0);
1506 }
1507 parcel.writeInt(audioStreamType);
John Spurlockc0650f022014-07-19 13:22:39 -04001508
1509 if (audioAttributes != null) {
1510 parcel.writeInt(1);
1511 audioAttributes.writeToParcel(parcel, 0);
1512 } else {
1513 parcel.writeInt(0);
1514 }
1515
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001516 parcel.writeLongArray(vibrate);
1517 parcel.writeInt(ledARGB);
1518 parcel.writeInt(ledOnMS);
1519 parcel.writeInt(ledOffMS);
1520 parcel.writeInt(iconLevel);
Daniel Sandlere46cbd32010-06-17 10:35:26 -04001521
1522 if (fullScreenIntent != null) {
1523 parcel.writeInt(1);
1524 fullScreenIntent.writeToParcel(parcel, 0);
1525 } else {
1526 parcel.writeInt(0);
1527 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001528
1529 parcel.writeInt(priority);
Joe Malin8d40d042012-11-05 11:36:40 -08001530
John Spurlockfd7f1e02014-03-18 16:41:57 -04001531 parcel.writeString(category);
Joe Malin8d40d042012-11-05 11:36:40 -08001532
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001533 parcel.writeString(mGroupKey);
1534
1535 parcel.writeString(mSortKey);
1536
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001537 parcel.writeBundle(extras); // null ok
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001538
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001539 parcel.writeTypedArray(actions, 0); // null ok
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001540
Daniel Sandlerf3b73432012-03-27 15:01:25 -04001541 if (bigContentView != null) {
1542 parcel.writeInt(1);
1543 bigContentView.writeToParcel(parcel, 0);
1544 } else {
1545 parcel.writeInt(0);
1546 }
Dan Sandler0bf2ed82013-12-21 23:33:41 -06001547
Chris Wren8fd39ec2014-02-27 17:43:26 -05001548 if (headsUpContentView != null) {
1549 parcel.writeInt(1);
1550 headsUpContentView.writeToParcel(parcel, 0);
1551 } else {
1552 parcel.writeInt(0);
1553 }
1554
Dan Sandler0bf2ed82013-12-21 23:33:41 -06001555 parcel.writeInt(visibility);
1556
1557 if (publicVersion != null) {
1558 parcel.writeInt(1);
1559 publicVersion.writeToParcel(parcel, 0);
1560 } else {
1561 parcel.writeInt(0);
1562 }
Dan Sandler26e81cf2014-05-06 10:01:27 -04001563
1564 parcel.writeInt(color);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001565 }
1566
1567 /**
1568 * Parcelable.Creator that instantiates Notification objects
1569 */
1570 public static final Parcelable.Creator<Notification> CREATOR
1571 = new Parcelable.Creator<Notification>()
1572 {
1573 public Notification createFromParcel(Parcel parcel)
1574 {
1575 return new Notification(parcel);
1576 }
1577
1578 public Notification[] newArray(int size)
1579 {
1580 return new Notification[size];
1581 }
1582 };
1583
1584 /**
1585 * Sets the {@link #contentView} field to be a view with the standard "Latest Event"
1586 * layout.
1587 *
1588 * <p>Uses the {@link #icon} and {@link #when} fields to set the icon and time fields
1589 * in the view.</p>
1590 * @param context The context for your application / activity.
1591 * @param contentTitle The title that goes in the expanded entry.
1592 * @param contentText The text that goes in the expanded entry.
1593 * @param contentIntent The intent to launch when the user clicks the expanded notification.
1594 * If this is an activity, it must include the
1595 * {@link android.content.Intent#FLAG_ACTIVITY_NEW_TASK} flag, which requires
Scott Main7aee61f2011-02-08 11:25:01 -08001596 * that you take care of task management as described in the
1597 * <a href="{@docRoot}guide/topics/fundamentals/tasks-and-back-stack.html">Tasks and Back
1598 * Stack</a> document.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001599 *
Joe Onorato46439ce2010-11-19 13:56:21 -08001600 * @deprecated Use {@link Builder} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001601 */
Joe Onorato46439ce2010-11-19 13:56:21 -08001602 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001603 public void setLatestEventInfo(Context context,
1604 CharSequence contentTitle, CharSequence contentText, PendingIntent contentIntent) {
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001605 Notification.Builder builder = new Notification.Builder(context);
1606
1607 // First, ensure that key pieces of information that may have been set directly
1608 // are preserved
1609 builder.setWhen(this.when);
1610 builder.setSmallIcon(this.icon);
1611 builder.setPriority(this.priority);
1612 builder.setTicker(this.tickerText);
1613 builder.setNumber(this.number);
Selim Cinek255dd042014-08-19 22:29:02 +02001614 builder.setColor(this.color);
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001615 builder.mFlags = this.flags;
1616 builder.setSound(this.sound, this.audioStreamType);
1617 builder.setDefaults(this.defaults);
1618 builder.setVibrate(this.vibrate);
1619
1620 // now apply the latestEventInfo fields
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001621 if (contentTitle != null) {
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001622 builder.setContentTitle(contentTitle);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001623 }
1624 if (contentText != null) {
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001625 builder.setContentText(contentText);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001626 }
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001627 builder.setContentIntent(contentIntent);
1628 builder.buildInto(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001629 }
1630
1631 @Override
1632 public String toString() {
1633 StringBuilder sb = new StringBuilder();
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001634 sb.append("Notification(pri=");
1635 sb.append(priority);
1636 sb.append(" contentView=");
Joe Onoratoc9596d62011-01-12 17:03:11 -08001637 if (contentView != null) {
1638 sb.append(contentView.getPackage());
1639 sb.append("/0x");
1640 sb.append(Integer.toHexString(contentView.getLayoutId()));
1641 } else {
1642 sb.append("null");
1643 }
1644 sb.append(" vibrate=");
Daniel Sandler6738eee2012-11-16 12:03:32 -05001645 if ((this.defaults & DEFAULT_VIBRATE) != 0) {
1646 sb.append("default");
1647 } else if (this.vibrate != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001648 int N = this.vibrate.length-1;
1649 sb.append("[");
1650 for (int i=0; i<N; i++) {
1651 sb.append(this.vibrate[i]);
1652 sb.append(',');
1653 }
Simon Schoar8cf97d92009-06-10 22:08:37 +02001654 if (N != -1) {
1655 sb.append(this.vibrate[N]);
1656 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001657 sb.append("]");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001658 } else {
1659 sb.append("null");
1660 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001661 sb.append(" sound=");
Daniel Sandler6738eee2012-11-16 12:03:32 -05001662 if ((this.defaults & DEFAULT_SOUND) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001663 sb.append("default");
Daniel Sandler6738eee2012-11-16 12:03:32 -05001664 } else if (this.sound != null) {
1665 sb.append(this.sound.toString());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001666 } else {
1667 sb.append("null");
1668 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001669 sb.append(" defaults=0x");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001670 sb.append(Integer.toHexString(this.defaults));
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001671 sb.append(" flags=0x");
Daniel Sandlere46cbd32010-06-17 10:35:26 -04001672 sb.append(Integer.toHexString(this.flags));
Dan Sandler26e81cf2014-05-06 10:01:27 -04001673 sb.append(String.format(" color=0x%08x", this.color));
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001674 if (this.category != null) {
1675 sb.append(" category=");
1676 sb.append(this.category);
1677 }
1678 if (this.mGroupKey != null) {
1679 sb.append(" groupKey=");
1680 sb.append(this.mGroupKey);
1681 }
1682 if (this.mSortKey != null) {
1683 sb.append(" sortKey=");
1684 sb.append(this.mSortKey);
1685 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001686 if (actions != null) {
Dan Sandler1b718782014-07-18 12:43:45 -04001687 sb.append(" actions=");
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001688 sb.append(actions.length);
Dan Sandler1b718782014-07-18 12:43:45 -04001689 }
1690 sb.append(" vis=");
1691 sb.append(visibilityToString(this.visibility));
1692 if (this.publicVersion != null) {
1693 sb.append(" publicVersion=");
1694 sb.append(publicVersion.toString());
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001695 }
1696 sb.append(")");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001697 return sb.toString();
1698 }
Joe Onorato46439ce2010-11-19 13:56:21 -08001699
Dan Sandler1b718782014-07-18 12:43:45 -04001700 /**
1701 * {@hide}
1702 */
1703 public static String visibilityToString(int vis) {
1704 switch (vis) {
1705 case VISIBILITY_PRIVATE:
1706 return "PRIVATE";
1707 case VISIBILITY_PUBLIC:
1708 return "PUBLIC";
1709 case VISIBILITY_SECRET:
1710 return "SECRET";
1711 default:
1712 return "UNKNOWN(" + String.valueOf(vis) + ")";
1713 }
1714 }
1715
Joe Onoratocb109a02011-01-18 17:57:41 -08001716 /**
Christoph Studer4600f9b2014-07-22 22:44:43 +02001717 * @hide
1718 */
1719 public boolean isValid() {
1720 // Would like to check for icon!=0 here, too, but NotificationManagerService accepts that
1721 // for legacy reasons.
1722 return contentView != null || extras.getBoolean(Builder.EXTRA_REBUILD_CONTENT_VIEW);
1723 }
1724
1725 /**
Christoph Studerc8db24b2014-07-25 17:50:30 +02001726 * @hide
1727 */
1728 public boolean isGroupSummary() {
1729 return mGroupKey != null && (flags & FLAG_GROUP_SUMMARY) != 0;
1730 }
1731
1732 /**
1733 * @hide
1734 */
1735 public boolean isGroupChild() {
1736 return mGroupKey != null && (flags & FLAG_GROUP_SUMMARY) == 0;
1737 }
1738
1739 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001740 * Builder class for {@link Notification} objects.
Joe Malin8d40d042012-11-05 11:36:40 -08001741 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001742 * Provides a convenient way to set the various fields of a {@link Notification} and generate
Scott Main183bf112012-08-13 19:12:13 -07001743 * content views using the platform's notification layout template. If your app supports
1744 * versions of Android as old as API level 4, you can instead use
1745 * {@link android.support.v4.app.NotificationCompat.Builder NotificationCompat.Builder},
1746 * available in the <a href="{@docRoot}tools/extras/support-library.html">Android Support
1747 * library</a>.
Joe Malin8d40d042012-11-05 11:36:40 -08001748 *
Scott Main183bf112012-08-13 19:12:13 -07001749 * <p>Example:
Joe Malin8d40d042012-11-05 11:36:40 -08001750 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001751 * <pre class="prettyprint">
Scott Main183bf112012-08-13 19:12:13 -07001752 * Notification noti = new Notification.Builder(mContext)
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001753 * .setContentTitle(&quot;New mail from &quot; + sender.toString())
1754 * .setContentText(subject)
1755 * .setSmallIcon(R.drawable.new_mail)
1756 * .setLargeIcon(aBitmap)
Chris Wrenfbd96ba2012-05-01 12:03:58 -04001757 * .build();
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001758 * </pre>
Joe Onoratocb109a02011-01-18 17:57:41 -08001759 */
Joe Onorato46439ce2010-11-19 13:56:21 -08001760 public static class Builder {
Daniel Sandler602ad1c2012-06-12 16:06:27 -04001761 private static final int MAX_ACTION_BUTTONS = 3;
Jorim Jaggi445d3c02014-08-19 22:33:42 +02001762 private static final float LARGE_TEXT_SCALE = 1.3f;
Daniel Sandler8680bf82012-05-15 16:52:52 -04001763
Christoph Studer4600f9b2014-07-22 22:44:43 +02001764 /**
1765 * @hide
1766 */
1767 public static final String EXTRA_NEEDS_REBUILD = "android.rebuild";
1768
1769 /**
1770 * @hide
1771 */
1772 public static final String EXTRA_REBUILD_LARGE_ICON = "android.rebuild.largeIcon";
1773 /**
1774 * @hide
1775 */
1776 public static final String EXTRA_REBUILD_CONTENT_VIEW = "android.rebuild.contentView";
1777 /**
1778 * @hide
1779 */
1780 public static final String EXTRA_REBUILD_CONTENT_VIEW_ACTION_COUNT =
1781 "android.rebuild.contentViewActionCount";
1782 /**
1783 * @hide
1784 */
1785 public static final String EXTRA_REBUILD_BIG_CONTENT_VIEW
1786 = "android.rebuild.bigView";
1787 /**
1788 * @hide
1789 */
1790 public static final String EXTRA_REBUILD_BIG_CONTENT_VIEW_ACTION_COUNT
1791 = "android.rebuild.bigViewActionCount";
1792 /**
1793 * @hide
1794 */
1795 public static final String EXTRA_REBUILD_HEADS_UP_CONTENT_VIEW
1796 = "android.rebuild.hudView";
1797 /**
1798 * @hide
1799 */
1800 public static final String EXTRA_REBUILD_HEADS_UP_CONTENT_VIEW_ACTION_COUNT
1801 = "android.rebuild.hudViewActionCount";
1802
1803 /**
1804 * The package name of the context used to create the notification via a Builder.
1805 */
1806 private static final String EXTRA_REBUILD_CONTEXT_PACKAGE =
1807 "android.rebuild.contextPackage";
1808
1809 // Whether to enable stripping (at post time) & rebuilding (at listener receive time) of
1810 // memory intensive resources.
1811 private static final boolean STRIP_AND_REBUILD = true;
1812
Joe Onorato46439ce2010-11-19 13:56:21 -08001813 private Context mContext;
1814
1815 private long mWhen;
1816 private int mSmallIcon;
1817 private int mSmallIconLevel;
Joe Onorato8595a3d2010-11-19 18:12:07 -08001818 private int mNumber;
Joe Onorato46439ce2010-11-19 13:56:21 -08001819 private CharSequence mContentTitle;
1820 private CharSequence mContentText;
1821 private CharSequence mContentInfo;
Daniel Sandlerf3b73432012-03-27 15:01:25 -04001822 private CharSequence mSubText;
Joe Onorato46439ce2010-11-19 13:56:21 -08001823 private PendingIntent mContentIntent;
1824 private RemoteViews mContentView;
1825 private PendingIntent mDeleteIntent;
1826 private PendingIntent mFullScreenIntent;
1827 private CharSequence mTickerText;
1828 private RemoteViews mTickerView;
1829 private Bitmap mLargeIcon;
1830 private Uri mSound;
1831 private int mAudioStreamType;
John Spurlockc0650f022014-07-19 13:22:39 -04001832 private AudioAttributes mAudioAttributes;
Joe Onorato46439ce2010-11-19 13:56:21 -08001833 private long[] mVibrate;
1834 private int mLedArgb;
1835 private int mLedOnMs;
1836 private int mLedOffMs;
1837 private int mDefaults;
1838 private int mFlags;
Jeff Sharkey1c400132011-08-05 14:50:13 -07001839 private int mProgressMax;
1840 private int mProgress;
1841 private boolean mProgressIndeterminate;
John Spurlockfd7f1e02014-03-18 16:41:57 -04001842 private String mCategory;
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001843 private String mGroupKey;
1844 private String mSortKey;
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001845 private Bundle mExtras;
1846 private int mPriority;
Daniel Sandler8680bf82012-05-15 16:52:52 -04001847 private ArrayList<Action> mActions = new ArrayList<Action>(MAX_ACTION_BUTTONS);
Daniel Sandlera2985ed2012-04-03 16:42:00 -04001848 private boolean mUseChronometer;
Chris Wrenfbd96ba2012-05-01 12:03:58 -04001849 private Style mStyle;
Daniel Sandler0c890492012-09-12 17:23:10 -07001850 private boolean mShowWhen = true;
Dan Sandler0bf2ed82013-12-21 23:33:41 -06001851 private int mVisibility = VISIBILITY_PRIVATE;
1852 private Notification mPublicVersion = null;
Dan Sandler26e81cf2014-05-06 10:01:27 -04001853 private final NotificationColorUtil mColorUtil;
Chris Wrendde75302014-03-26 17:24:15 -04001854 private ArrayList<String> mPeople;
Dan Sandler26e81cf2014-05-06 10:01:27 -04001855 private int mColor = COLOR_DEFAULT;
Christoph Studer7ac80e62014-08-04 16:01:57 +02001856
1857 /**
1858 * The user that built the notification originally.
1859 */
1860 private int mOriginatingUserId;
Christoph Studer4600f9b2014-07-22 22:44:43 +02001861
1862 /**
1863 * Contains extras related to rebuilding during the build phase.
1864 */
1865 private Bundle mRebuildBundle = new Bundle();
1866 /**
1867 * Contains the notification to rebuild when this Builder is in "rebuild" mode.
1868 * Null otherwise.
1869 */
1870 private Notification mRebuildNotification = null;
1871
Joe Onoratocb109a02011-01-18 17:57:41 -08001872 /**
Jorim Jaggid05aa3e2014-08-28 17:52:27 +02001873 * Whether the build notification has three lines. This is used to make the top padding for
1874 * both the contracted and expanded layout consistent.
1875 *
1876 * <p>
1877 * This field is only valid during the build phase.
1878 */
1879 private boolean mHasThreeLines;
1880
1881 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001882 * Constructs a new Builder with the defaults:
Joe Onoratocb109a02011-01-18 17:57:41 -08001883 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001884
1885 * <table>
1886 * <tr><th align=right>priority</th>
1887 * <td>{@link #PRIORITY_DEFAULT}</td></tr>
1888 * <tr><th align=right>when</th>
1889 * <td>now ({@link System#currentTimeMillis()})</td></tr>
1890 * <tr><th align=right>audio stream</th>
1891 * <td>{@link #STREAM_DEFAULT}</td></tr>
1892 * </table>
Joe Onoratocb109a02011-01-18 17:57:41 -08001893 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001894
1895 * @param context
1896 * A {@link Context} that will be used by the Builder to construct the
1897 * RemoteViews. The Context will not be held past the lifetime of this Builder
1898 * object.
Joe Onoratocb109a02011-01-18 17:57:41 -08001899 */
Joe Onorato46439ce2010-11-19 13:56:21 -08001900 public Builder(Context context) {
Adam Powellbf06fa02014-05-30 12:32:18 -07001901 /*
1902 * Important compatibility note!
1903 * Some apps out in the wild create a Notification.Builder in their Activity subclass
1904 * constructor for later use. At this point Activities - themselves subclasses of
1905 * ContextWrapper - do not have their inner Context populated yet. This means that
1906 * any calls to Context methods from within this constructor can cause NPEs in existing
1907 * apps. Any data populated from mContext should therefore be populated lazily to
1908 * preserve compatibility.
1909 */
Joe Onorato46439ce2010-11-19 13:56:21 -08001910 mContext = context;
Andy Stadler110988c2010-12-03 14:29:16 -08001911
1912 // Set defaults to match the defaults of a Notification
Joe Onorato46439ce2010-11-19 13:56:21 -08001913 mWhen = System.currentTimeMillis();
Andy Stadler110988c2010-12-03 14:29:16 -08001914 mAudioStreamType = STREAM_DEFAULT;
John Spurlockc0650f022014-07-19 13:22:39 -04001915 mAudioAttributes = AUDIO_ATTRIBUTES_DEFAULT;
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001916 mPriority = PRIORITY_DEFAULT;
Chris Wrendde75302014-03-26 17:24:15 -04001917 mPeople = new ArrayList<String>();
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01001918
Christoph Studer239f8352014-08-25 15:13:18 +02001919 mColorUtil = context.getApplicationInfo().targetSdkVersion < Build.VERSION_CODES.L ?
1920 NotificationColorUtil.getInstance() : null;
Joe Onorato46439ce2010-11-19 13:56:21 -08001921 }
1922
Joe Onoratocb109a02011-01-18 17:57:41 -08001923 /**
Christoph Studer4600f9b2014-07-22 22:44:43 +02001924 * Creates a Builder for rebuilding the given Notification.
1925 * <p>
1926 * Call {@link #rebuild()} to retrieve the rebuilt version of 'n'.
1927 */
1928 private Builder(Context context, Notification n) {
1929 this(context);
1930 mRebuildNotification = n;
1931 restoreFromNotification(n);
1932
1933 Style style = null;
1934 Bundle extras = n.extras;
1935 String templateClass = extras.getString(EXTRA_TEMPLATE);
1936 if (!TextUtils.isEmpty(templateClass)) {
1937 Class<? extends Style> styleClass = getNotificationStyleClass(templateClass);
1938 if (styleClass == null) {
1939 Log.d(TAG, "Unknown style class: " + styleClass);
1940 return;
1941 }
1942
1943 try {
1944 Constructor<? extends Style> constructor = styleClass.getConstructor();
1945 style = constructor.newInstance();
1946 style.restoreFromExtras(extras);
1947 } catch (Throwable t) {
1948 Log.e(TAG, "Could not create Style", t);
1949 return;
1950 }
1951 }
1952 if (style != null) {
1953 setStyle(style);
1954 }
1955 }
1956
1957 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001958 * Add a timestamp pertaining to the notification (usually the time the event occurred).
Daniel Sandler0c890492012-09-12 17:23:10 -07001959 * It will be shown in the notification content view by default; use
Griff Hazen50c11652014-05-16 09:46:31 -07001960 * {@link #setShowWhen(boolean) setShowWhen} to control this.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001961 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001962 * @see Notification#when
Joe Onoratocb109a02011-01-18 17:57:41 -08001963 */
Joe Onorato46439ce2010-11-19 13:56:21 -08001964 public Builder setWhen(long when) {
1965 mWhen = when;
1966 return this;
1967 }
1968
Joe Onoratocb109a02011-01-18 17:57:41 -08001969 /**
Griff Hazen50c11652014-05-16 09:46:31 -07001970 * Control whether the timestamp set with {@link #setWhen(long) setWhen} is shown
Daniel Sandler0c890492012-09-12 17:23:10 -07001971 * in the content view.
1972 */
1973 public Builder setShowWhen(boolean show) {
1974 mShowWhen = show;
1975 return this;
1976 }
1977
1978 /**
Daniel Sandlerd33b8032012-05-10 11:41:48 -04001979 * Show the {@link Notification#when} field as a stopwatch.
Joe Malin8d40d042012-11-05 11:36:40 -08001980 *
1981 * Instead of presenting <code>when</code> as a timestamp, the notification will show an
Daniel Sandlerd33b8032012-05-10 11:41:48 -04001982 * automatically updating display of the minutes and seconds since <code>when</code>.
Daniel Sandlera2985ed2012-04-03 16:42:00 -04001983 *
Daniel Sandlerd33b8032012-05-10 11:41:48 -04001984 * Useful when showing an elapsed time (like an ongoing phone call).
1985 *
1986 * @see android.widget.Chronometer
Daniel Sandlera2985ed2012-04-03 16:42:00 -04001987 * @see Notification#when
1988 */
1989 public Builder setUsesChronometer(boolean b) {
1990 mUseChronometer = b;
1991 return this;
1992 }
1993
1994 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001995 * Set the small icon resource, which will be used to represent the notification in the
1996 * status bar.
Joe Onoratocb109a02011-01-18 17:57:41 -08001997 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001998
1999 * The platform template for the expanded view will draw this icon in the left, unless a
2000 * {@link #setLargeIcon(Bitmap) large icon} has also been specified, in which case the small
2001 * icon will be moved to the right-hand side.
2002 *
2003
2004 * @param icon
2005 * A resource ID in the application's package of the drawable to use.
2006 * @see Notification#icon
Joe Onoratocb109a02011-01-18 17:57:41 -08002007 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002008 public Builder setSmallIcon(int icon) {
2009 mSmallIcon = icon;
2010 return this;
2011 }
2012
Joe Onoratocb109a02011-01-18 17:57:41 -08002013 /**
2014 * A variant of {@link #setSmallIcon(int) setSmallIcon(int)} that takes an additional
2015 * level parameter for when the icon is a {@link android.graphics.drawable.LevelListDrawable
2016 * LevelListDrawable}.
2017 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002018 * @param icon A resource ID in the application's package of the drawable to use.
Joe Onoratocb109a02011-01-18 17:57:41 -08002019 * @param level The level to use for the icon.
2020 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002021 * @see Notification#icon
2022 * @see Notification#iconLevel
Joe Onoratocb109a02011-01-18 17:57:41 -08002023 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002024 public Builder setSmallIcon(int icon, int level) {
2025 mSmallIcon = icon;
2026 mSmallIconLevel = level;
2027 return this;
2028 }
2029
Joe Onoratocb109a02011-01-18 17:57:41 -08002030 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002031 * Set the first line of text in the platform notification template.
Joe Onoratocb109a02011-01-18 17:57:41 -08002032 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002033 public Builder setContentTitle(CharSequence title) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04002034 mContentTitle = safeCharSequence(title);
Joe Onorato46439ce2010-11-19 13:56:21 -08002035 return this;
2036 }
2037
Joe Onoratocb109a02011-01-18 17:57:41 -08002038 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002039 * Set the second line of text in the platform notification template.
Joe Onoratocb109a02011-01-18 17:57:41 -08002040 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002041 public Builder setContentText(CharSequence text) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04002042 mContentText = safeCharSequence(text);
Joe Onorato46439ce2010-11-19 13:56:21 -08002043 return this;
2044 }
2045
Joe Onoratocb109a02011-01-18 17:57:41 -08002046 /**
Joe Malin8d40d042012-11-05 11:36:40 -08002047 * Set the third line of text in the platform notification template.
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04002048 * Don't use if you're also using {@link #setProgress(int, int, boolean)}; they occupy the
2049 * same location in the standard template.
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002050 */
2051 public Builder setSubText(CharSequence text) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04002052 mSubText = safeCharSequence(text);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002053 return this;
2054 }
2055
2056 /**
Joe Onoratocb109a02011-01-18 17:57:41 -08002057 * Set the large number at the right-hand side of the notification. This is
2058 * equivalent to setContentInfo, although it might show the number in a different
2059 * font size for readability.
2060 */
Joe Onorato8595a3d2010-11-19 18:12:07 -08002061 public Builder setNumber(int number) {
2062 mNumber = number;
2063 return this;
2064 }
2065
Joe Onoratocb109a02011-01-18 17:57:41 -08002066 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002067 * A small piece of additional information pertaining to this notification.
2068 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002069 * The platform template will draw this on the last line of the notification, at the far
2070 * right (to the right of a smallIcon if it has been placed there).
Joe Onoratocb109a02011-01-18 17:57:41 -08002071 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002072 public Builder setContentInfo(CharSequence info) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04002073 mContentInfo = safeCharSequence(info);
Joe Onorato46439ce2010-11-19 13:56:21 -08002074 return this;
2075 }
2076
Joe Onoratocb109a02011-01-18 17:57:41 -08002077 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002078 * Set the progress this notification represents.
2079 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002080 * The platform template will represent this using a {@link ProgressBar}.
Jeff Sharkey1c400132011-08-05 14:50:13 -07002081 */
2082 public Builder setProgress(int max, int progress, boolean indeterminate) {
2083 mProgressMax = max;
2084 mProgress = progress;
2085 mProgressIndeterminate = indeterminate;
2086 return this;
2087 }
2088
2089 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002090 * Supply a custom RemoteViews to use instead of the platform template.
2091 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002092 * @see Notification#contentView
Joe Onoratocb109a02011-01-18 17:57:41 -08002093 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002094 public Builder setContent(RemoteViews views) {
2095 mContentView = views;
2096 return this;
2097 }
2098
Joe Onoratocb109a02011-01-18 17:57:41 -08002099 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002100 * Supply a {@link PendingIntent} to be sent when the notification is clicked.
2101 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002102 * As of {@link android.os.Build.VERSION_CODES#HONEYCOMB}, if this field is unset and you
2103 * have specified a custom RemoteViews with {@link #setContent(RemoteViews)}, you can use
2104 * {@link RemoteViews#setOnClickPendingIntent RemoteViews.setOnClickPendingIntent(int,PendingIntent)}
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002105 * to assign PendingIntents to individual views in that custom layout (i.e., to create
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002106 * clickable buttons inside the notification view).
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002107 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002108 * @see Notification#contentIntent Notification.contentIntent
Joe Onoratocb109a02011-01-18 17:57:41 -08002109 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002110 public Builder setContentIntent(PendingIntent intent) {
2111 mContentIntent = intent;
2112 return this;
2113 }
2114
Joe Onoratocb109a02011-01-18 17:57:41 -08002115 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002116 * Supply a {@link PendingIntent} to send when the notification is cleared explicitly by the user.
2117 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002118 * @see Notification#deleteIntent
Joe Onoratocb109a02011-01-18 17:57:41 -08002119 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002120 public Builder setDeleteIntent(PendingIntent intent) {
2121 mDeleteIntent = intent;
2122 return this;
2123 }
2124
Joe Onoratocb109a02011-01-18 17:57:41 -08002125 /**
2126 * An intent to launch instead of posting the notification to the status bar.
2127 * Only for use with extremely high-priority notifications demanding the user's
2128 * <strong>immediate</strong> attention, such as an incoming phone call or
2129 * alarm clock that the user has explicitly set to a particular time.
2130 * If this facility is used for something else, please give the user an option
2131 * to turn it off and use a normal notification, as this can be extremely
2132 * disruptive.
2133 *
Chris Wren47c20a12014-06-18 17:27:29 -04002134 * <p>
2135 * The system UI may choose to display a heads-up notification, instead of
2136 * launching this intent, while the user is using the device.
2137 * </p>
2138 *
Joe Onoratocb109a02011-01-18 17:57:41 -08002139 * @param intent The pending intent to launch.
2140 * @param highPriority Passing true will cause this notification to be sent
2141 * even if other notifications are suppressed.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002142 *
2143 * @see Notification#fullScreenIntent
Joe Onoratocb109a02011-01-18 17:57:41 -08002144 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002145 public Builder setFullScreenIntent(PendingIntent intent, boolean highPriority) {
2146 mFullScreenIntent = intent;
2147 setFlag(FLAG_HIGH_PRIORITY, highPriority);
2148 return this;
2149 }
2150
Joe Onoratocb109a02011-01-18 17:57:41 -08002151 /**
Dan Sandler5fcdf6e2014-07-18 11:31:15 -04002152 * Set the "ticker" text which is sent to accessibility services.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002153 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002154 * @see Notification#tickerText
Joe Onoratocb109a02011-01-18 17:57:41 -08002155 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002156 public Builder setTicker(CharSequence tickerText) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04002157 mTickerText = safeCharSequence(tickerText);
Joe Onorato46439ce2010-11-19 13:56:21 -08002158 return this;
2159 }
2160
Joe Onoratocb109a02011-01-18 17:57:41 -08002161 /**
Dan Sandler5fcdf6e2014-07-18 11:31:15 -04002162 * Obsolete version of {@link #setTicker(CharSequence)}.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002163 *
Joe Onoratocb109a02011-01-18 17:57:41 -08002164 */
Dan Sandler5fcdf6e2014-07-18 11:31:15 -04002165 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -08002166 public Builder setTicker(CharSequence tickerText, RemoteViews views) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04002167 mTickerText = safeCharSequence(tickerText);
Dan Sandler5fcdf6e2014-07-18 11:31:15 -04002168 mTickerView = views; // we'll save it for you anyway
Joe Onorato46439ce2010-11-19 13:56:21 -08002169 return this;
2170 }
2171
Joe Onoratocb109a02011-01-18 17:57:41 -08002172 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002173 * Add a large icon to the notification (and the ticker on some devices).
2174 *
2175 * In the platform template, this image will be shown on the left of the notification view
2176 * in place of the {@link #setSmallIcon(int) small icon} (which will move to the right side).
2177 *
2178 * @see Notification#largeIcon
Joe Onoratocb109a02011-01-18 17:57:41 -08002179 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002180 public Builder setLargeIcon(Bitmap icon) {
2181 mLargeIcon = icon;
2182 return this;
2183 }
2184
Joe Onoratocb109a02011-01-18 17:57:41 -08002185 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002186 * Set the sound to play.
2187 *
John Spurlockc0650f022014-07-19 13:22:39 -04002188 * It will be played using the {@link #AUDIO_ATTRIBUTES_DEFAULT default audio attributes}
2189 * for notifications.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002190 *
Chris Wren47c20a12014-06-18 17:27:29 -04002191 * <p>
2192 * A notification that is noisy is more likely to be presented as a heads-up notification.
2193 * </p>
2194 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002195 * @see Notification#sound
Joe Onoratocb109a02011-01-18 17:57:41 -08002196 */
Joe Onorato52f80cd2010-11-21 15:34:48 -08002197 public Builder setSound(Uri sound) {
2198 mSound = sound;
John Spurlockc0650f022014-07-19 13:22:39 -04002199 mAudioAttributes = AUDIO_ATTRIBUTES_DEFAULT;
Joe Onorato52f80cd2010-11-21 15:34:48 -08002200 return this;
2201 }
2202
Joe Onoratocb109a02011-01-18 17:57:41 -08002203 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002204 * Set the sound to play, along with a specific stream on which to play it.
Joe Onoratocb109a02011-01-18 17:57:41 -08002205 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002206 * See {@link android.media.AudioManager} for the <code>STREAM_</code> constants.
2207 *
Chris Wren47c20a12014-06-18 17:27:29 -04002208 * <p>
2209 * A notification that is noisy is more likely to be presented as a heads-up notification.
2210 * </p>
John Spurlockc0650f022014-07-19 13:22:39 -04002211 * @deprecated use {@link #setSound(Uri, AudioAttributes)} instead.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002212 * @see Notification#sound
Joe Onoratocb109a02011-01-18 17:57:41 -08002213 */
Jean-Michel Trivi81f871e2014-08-06 16:32:38 -07002214 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -08002215 public Builder setSound(Uri sound, int streamType) {
2216 mSound = sound;
2217 mAudioStreamType = streamType;
2218 return this;
2219 }
2220
Joe Onoratocb109a02011-01-18 17:57:41 -08002221 /**
John Spurlockc0650f022014-07-19 13:22:39 -04002222 * Set the sound to play, along with specific {@link AudioAttributes audio attributes} to
2223 * use during playback.
2224 *
2225 * <p>
2226 * A notification that is noisy is more likely to be presented as a heads-up notification.
2227 * </p>
2228 *
2229 * @see Notification#sound
2230 */
2231 public Builder setSound(Uri sound, AudioAttributes audioAttributes) {
2232 mSound = sound;
2233 mAudioAttributes = audioAttributes;
2234 return this;
2235 }
2236
2237 /**
Joe Onoratocb109a02011-01-18 17:57:41 -08002238 * Set the vibration pattern to use.
2239 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002240 * See {@link android.os.Vibrator#vibrate(long[], int)} for a discussion of the
2241 * <code>pattern</code> parameter.
2242 *
Chris Wren47c20a12014-06-18 17:27:29 -04002243 * <p>
2244 * A notification that vibrates is more likely to be presented as a heads-up notification.
2245 * </p>
2246 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002247 * @see Notification#vibrate
Joe Onoratocb109a02011-01-18 17:57:41 -08002248 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002249 public Builder setVibrate(long[] pattern) {
2250 mVibrate = pattern;
2251 return this;
2252 }
2253
Joe Onoratocb109a02011-01-18 17:57:41 -08002254 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002255 * Set the desired color for the indicator LED on the device, as well as the
2256 * blink duty cycle (specified in milliseconds).
2257 *
2258
2259 * Not all devices will honor all (or even any) of these values.
2260 *
2261
2262 * @see Notification#ledARGB
2263 * @see Notification#ledOnMS
2264 * @see Notification#ledOffMS
Joe Onoratocb109a02011-01-18 17:57:41 -08002265 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002266 public Builder setLights(int argb, int onMs, int offMs) {
2267 mLedArgb = argb;
2268 mLedOnMs = onMs;
2269 mLedOffMs = offMs;
Joe Onorato46439ce2010-11-19 13:56:21 -08002270 return this;
2271 }
2272
Joe Onoratocb109a02011-01-18 17:57:41 -08002273 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002274 * Set whether this is an "ongoing" notification.
Joe Onoratocb109a02011-01-18 17:57:41 -08002275 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002276
2277 * Ongoing notifications cannot be dismissed by the user, so your application or service
2278 * must take care of canceling them.
2279 *
2280
2281 * They are typically used to indicate a background task that the user is actively engaged
2282 * with (e.g., playing music) or is pending in some way and therefore occupying the device
2283 * (e.g., a file download, sync operation, active network connection).
2284 *
2285
2286 * @see Notification#FLAG_ONGOING_EVENT
2287 * @see Service#setForeground(boolean)
Joe Onoratocb109a02011-01-18 17:57:41 -08002288 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002289 public Builder setOngoing(boolean ongoing) {
2290 setFlag(FLAG_ONGOING_EVENT, ongoing);
2291 return this;
2292 }
2293
Joe Onoratocb109a02011-01-18 17:57:41 -08002294 /**
2295 * Set this flag if you would only like the sound, vibrate
2296 * and ticker to be played if the notification is not already showing.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002297 *
2298 * @see Notification#FLAG_ONLY_ALERT_ONCE
Joe Onoratocb109a02011-01-18 17:57:41 -08002299 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002300 public Builder setOnlyAlertOnce(boolean onlyAlertOnce) {
2301 setFlag(FLAG_ONLY_ALERT_ONCE, onlyAlertOnce);
2302 return this;
2303 }
2304
Joe Onoratocb109a02011-01-18 17:57:41 -08002305 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002306 * Make this notification automatically dismissed when the user touches it. The
2307 * PendingIntent set with {@link #setDeleteIntent} will be sent when this happens.
2308 *
2309 * @see Notification#FLAG_AUTO_CANCEL
Joe Onoratocb109a02011-01-18 17:57:41 -08002310 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002311 public Builder setAutoCancel(boolean autoCancel) {
Joe Onorato281d83f2011-01-04 17:13:10 -08002312 setFlag(FLAG_AUTO_CANCEL, autoCancel);
Joe Onorato46439ce2010-11-19 13:56:21 -08002313 return this;
2314 }
2315
Joe Onoratocb109a02011-01-18 17:57:41 -08002316 /**
Griff Hazendfcb0802014-02-11 12:00:00 -08002317 * Set whether or not this notification should not bridge to other devices.
2318 *
2319 * <p>Some notifications can be bridged to other devices for remote display.
2320 * This hint can be set to recommend this notification not be bridged.
2321 */
2322 public Builder setLocalOnly(boolean localOnly) {
2323 setFlag(FLAG_LOCAL_ONLY, localOnly);
2324 return this;
2325 }
2326
2327 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002328 * Set which notification properties will be inherited from system defaults.
Joe Onoratocb109a02011-01-18 17:57:41 -08002329 * <p>
2330 * The value should be one or more of the following fields combined with
2331 * bitwise-or:
2332 * {@link #DEFAULT_SOUND}, {@link #DEFAULT_VIBRATE}, {@link #DEFAULT_LIGHTS}.
2333 * <p>
2334 * For all default values, use {@link #DEFAULT_ALL}.
2335 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002336 public Builder setDefaults(int defaults) {
2337 mDefaults = defaults;
2338 return this;
2339 }
2340
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002341 /**
2342 * Set the priority of this notification.
2343 *
2344 * @see Notification#priority
2345 */
Tor Norbyed9273d62013-05-30 15:59:53 -07002346 public Builder setPriority(@Priority int pri) {
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002347 mPriority = pri;
2348 return this;
2349 }
Joe Malin8d40d042012-11-05 11:36:40 -08002350
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002351 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -04002352 * Set the notification category.
Joe Malin8d40d042012-11-05 11:36:40 -08002353 *
John Spurlockfd7f1e02014-03-18 16:41:57 -04002354 * @see Notification#category
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002355 */
John Spurlockfd7f1e02014-03-18 16:41:57 -04002356 public Builder setCategory(String category) {
2357 mCategory = category;
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002358 return this;
2359 }
2360
2361 /**
Chris Wrendde75302014-03-26 17:24:15 -04002362 * Add a person that is relevant to this notification.
2363 *
2364 * @see Notification#EXTRA_PEOPLE
2365 */
2366 public Builder addPerson(String handle) {
2367 mPeople.add(handle);
2368 return this;
2369 }
2370
2371 /**
Griff Hazen5cadc3b2014-05-20 09:55:39 -07002372 * Set this notification to be part of a group of notifications sharing the same key.
2373 * Grouped notifications may display in a cluster or stack on devices which
2374 * support such rendering.
2375 *
2376 * <p>To make this notification the summary for its group, also call
2377 * {@link #setGroupSummary}. A sort order can be specified for group members by using
2378 * {@link #setSortKey}.
2379 * @param groupKey The group key of the group.
2380 * @return this object for method chaining
2381 */
2382 public Builder setGroup(String groupKey) {
2383 mGroupKey = groupKey;
2384 return this;
2385 }
2386
2387 /**
2388 * Set this notification to be the group summary for a group of notifications.
2389 * Grouped notifications may display in a cluster or stack on devices which
2390 * support such rendering. Requires a group key also be set using {@link #setGroup}.
2391 * @param isGroupSummary Whether this notification should be a group summary.
2392 * @return this object for method chaining
2393 */
2394 public Builder setGroupSummary(boolean isGroupSummary) {
2395 setFlag(FLAG_GROUP_SUMMARY, isGroupSummary);
2396 return this;
2397 }
2398
2399 /**
2400 * Set a sort key that orders this notification among other notifications from the
2401 * same package. This can be useful if an external sort was already applied and an app
2402 * would like to preserve this. Notifications will be sorted lexicographically using this
2403 * value, although providing different priorities in addition to providing sort key may
2404 * cause this value to be ignored.
2405 *
2406 * <p>This sort key can also be used to order members of a notification group. See
Griff Hazen9e1379f2014-05-20 12:50:51 -07002407 * {@link #setGroup}.
Griff Hazen5cadc3b2014-05-20 09:55:39 -07002408 *
2409 * @see String#compareTo(String)
2410 */
2411 public Builder setSortKey(String sortKey) {
2412 mSortKey = sortKey;
2413 return this;
2414 }
2415
2416 /**
Griff Hazen720042b2014-02-24 15:46:56 -08002417 * Merge additional metadata into this notification.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002418 *
Griff Hazen720042b2014-02-24 15:46:56 -08002419 * <p>Values within the Bundle will replace existing extras values in this Builder.
2420 *
2421 * @see Notification#extras
2422 */
Griff Hazen959591e2014-05-15 22:26:18 -07002423 public Builder addExtras(Bundle extras) {
2424 if (extras != null) {
2425 if (mExtras == null) {
2426 mExtras = new Bundle(extras);
2427 } else {
2428 mExtras.putAll(extras);
2429 }
Griff Hazen720042b2014-02-24 15:46:56 -08002430 }
2431 return this;
2432 }
2433
2434 /**
2435 * Set metadata for this notification.
2436 *
2437 * <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 -04002438 * current contents are copied into the Notification each time {@link #build()} is
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002439 * called.
2440 *
Griff Hazen720042b2014-02-24 15:46:56 -08002441 * <p>Replaces any existing extras values with those from the provided Bundle.
2442 * Use {@link #addExtras} to merge in metadata instead.
2443 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002444 * @see Notification#extras
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002445 */
Griff Hazen959591e2014-05-15 22:26:18 -07002446 public Builder setExtras(Bundle extras) {
2447 mExtras = extras;
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002448 return this;
2449 }
2450
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002451 /**
Griff Hazen720042b2014-02-24 15:46:56 -08002452 * Get the current metadata Bundle used by this notification Builder.
2453 *
2454 * <p>The returned Bundle is shared with this Builder.
2455 *
2456 * <p>The current contents of this Bundle are copied into the Notification each time
2457 * {@link #build()} is called.
2458 *
2459 * @see Notification#extras
2460 */
2461 public Bundle getExtras() {
2462 if (mExtras == null) {
2463 mExtras = new Bundle();
2464 }
2465 return mExtras;
2466 }
2467
2468 /**
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002469 * Add an action to this notification. Actions are typically displayed by
2470 * the system as a button adjacent to the notification content.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04002471 * <p>
2472 * Every action must have an icon (32dp square and matching the
2473 * <a href="{@docRoot}design/style/iconography.html#action-bar">Holo
2474 * Dark action bar</a> visual style), a textual label, and a {@link PendingIntent}.
2475 * <p>
2476 * A notification in its expanded form can display up to 3 actions, from left to right in
2477 * the order they were added. Actions will not be displayed when the notification is
2478 * collapsed, however, so be sure that any essential functions may be accessed by the user
2479 * in some other way (for example, in the Activity pointed to by {@link #contentIntent}).
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002480 *
2481 * @param icon Resource ID of a drawable that represents the action.
2482 * @param title Text describing the action.
2483 * @param intent PendingIntent to be fired when the action is invoked.
2484 */
2485 public Builder addAction(int icon, CharSequence title, PendingIntent intent) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04002486 mActions.add(new Action(icon, safeCharSequence(title), intent));
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002487 return this;
2488 }
2489
Chris Wrenfbd96ba2012-05-01 12:03:58 -04002490 /**
Griff Hazen959591e2014-05-15 22:26:18 -07002491 * Add an action to this notification. Actions are typically displayed by
2492 * the system as a button adjacent to the notification content.
2493 * <p>
2494 * Every action must have an icon (32dp square and matching the
2495 * <a href="{@docRoot}design/style/iconography.html#action-bar">Holo
2496 * Dark action bar</a> visual style), a textual label, and a {@link PendingIntent}.
2497 * <p>
2498 * A notification in its expanded form can display up to 3 actions, from left to right in
2499 * the order they were added. Actions will not be displayed when the notification is
2500 * collapsed, however, so be sure that any essential functions may be accessed by the user
2501 * in some other way (for example, in the Activity pointed to by {@link #contentIntent}).
2502 *
2503 * @param action The action to add.
2504 */
2505 public Builder addAction(Action action) {
2506 mActions.add(action);
2507 return this;
2508 }
2509
2510 /**
Chris Wrenfbd96ba2012-05-01 12:03:58 -04002511 * Add a rich notification style to be applied at build time.
2512 *
2513 * @param style Object responsible for modifying the notification style.
2514 */
2515 public Builder setStyle(Style style) {
2516 if (mStyle != style) {
2517 mStyle = style;
Daniel Sandlerc08dea22012-06-28 08:35:24 -07002518 if (mStyle != null) {
2519 mStyle.setBuilder(this);
2520 }
Chris Wrenfbd96ba2012-05-01 12:03:58 -04002521 }
2522 return this;
2523 }
2524
Dan Sandler0bf2ed82013-12-21 23:33:41 -06002525 /**
2526 * Specify the value of {@link #visibility}.
Griff Hazenb720abe2014-05-20 13:15:30 -07002527 *
Dan Sandler0bf2ed82013-12-21 23:33:41 -06002528 * @param visibility One of {@link #VISIBILITY_PRIVATE} (the default),
2529 * {@link #VISIBILITY_SECRET}, or {@link #VISIBILITY_PUBLIC}.
2530 *
2531 * @return The same Builder.
2532 */
2533 public Builder setVisibility(int visibility) {
2534 mVisibility = visibility;
2535 return this;
2536 }
2537
2538 /**
2539 * Supply a replacement Notification whose contents should be shown in insecure contexts
2540 * (i.e. atop the secure lockscreen). See {@link #visibility} and {@link #VISIBILITY_PUBLIC}.
2541 * @param n A replacement notification, presumably with some or all info redacted.
2542 * @return The same Builder.
2543 */
2544 public Builder setPublicVersion(Notification n) {
2545 mPublicVersion = n;
2546 return this;
2547 }
2548
Griff Hazenb720abe2014-05-20 13:15:30 -07002549 /**
Griff Hazen5cadc3b2014-05-20 09:55:39 -07002550 * Apply an extender to this notification builder. Extenders may be used to add
2551 * metadata or change options on this builder.
2552 */
Griff Hazen61a9e862014-05-22 16:05:19 -07002553 public Builder extend(Extender extender) {
2554 extender.extend(this);
Griff Hazen5cadc3b2014-05-20 09:55:39 -07002555 return this;
2556 }
2557
Joe Onorato46439ce2010-11-19 13:56:21 -08002558 private void setFlag(int mask, boolean value) {
2559 if (value) {
2560 mFlags |= mask;
2561 } else {
2562 mFlags &= ~mask;
2563 }
2564 }
2565
Dan Sandler26e81cf2014-05-06 10:01:27 -04002566 /**
2567 * Sets {@link Notification#color}.
2568 *
2569 * @param argb The accent color to use
2570 *
2571 * @return The same Builder.
2572 */
2573 public Builder setColor(int argb) {
2574 mColor = argb;
2575 return this;
2576 }
2577
Jorim Jaggid05aa3e2014-08-28 17:52:27 +02002578 private Drawable getProfileBadgeDrawable() {
Christoph Studer7ac80e62014-08-04 16:01:57 +02002579 // Note: This assumes that the current user can read the profile badge of the
2580 // originating user.
Kenny Guy8a0101b2014-05-08 23:34:12 +01002581 UserManager userManager = (UserManager) mContext.getSystemService(Context.USER_SERVICE);
Jorim Jaggid05aa3e2014-08-28 17:52:27 +02002582 return userManager.getBadgeForUser(new UserHandle(mOriginatingUserId), 0);
2583 }
2584
2585 private Bitmap getProfileBadge() {
2586 Drawable badge = getProfileBadgeDrawable();
Kenny Guy8a0101b2014-05-08 23:34:12 +01002587 if (badge == null) {
2588 return null;
2589 }
Jorim Jaggid05aa3e2014-08-28 17:52:27 +02002590 final int size = mContext.getResources().getDimensionPixelSize(
2591 R.dimen.notification_badge_size);
2592 Bitmap bitmap = Bitmap.createBitmap(size, size, Bitmap.Config.ARGB_8888);
Kenny Guy8a0101b2014-05-08 23:34:12 +01002593 Canvas canvas = new Canvas(bitmap);
Jorim Jaggid05aa3e2014-08-28 17:52:27 +02002594 badge.setBounds(0, 0, size, size);
Kenny Guy8a0101b2014-05-08 23:34:12 +01002595 badge.draw(canvas);
2596 return bitmap;
2597 }
2598
Kenny Guy98193ea2014-07-24 19:54:37 +01002599 private boolean addProfileBadge(RemoteViews contentView, int resId) {
2600 Bitmap profileBadge = getProfileBadge();
2601
2602 contentView.setViewVisibility(R.id.profile_badge_large_template, View.GONE);
2603 contentView.setViewVisibility(R.id.profile_badge_line2, View.GONE);
2604 contentView.setViewVisibility(R.id.profile_badge_line3, View.GONE);
2605
2606 if (profileBadge != null) {
2607 contentView.setImageViewBitmap(resId, profileBadge);
2608 contentView.setViewVisibility(resId, View.VISIBLE);
2609
2610 // Make sure Line 3 is visible. As badge will be here if there
2611 // is no text to display.
2612 if (resId == R.id.profile_badge_line3) {
2613 contentView.setViewVisibility(R.id.line3, View.VISIBLE);
2614 }
2615 return true;
2616 }
2617 return false;
2618 }
2619
Jorim Jaggid05aa3e2014-08-28 17:52:27 +02002620 private void shrinkLine3Text(RemoteViews contentView) {
2621 float subTextSize = mContext.getResources().getDimensionPixelSize(
2622 R.dimen.notification_subtext_size);
2623 contentView.setTextViewTextSize(R.id.text, TypedValue.COMPLEX_UNIT_PX, subTextSize);
2624 }
2625
Christoph Studerfe718432014-09-01 18:21:18 +02002626 private void unshrinkLine3Text(RemoteViews contentView) {
2627 float regularTextSize = mContext.getResources().getDimensionPixelSize(
2628 com.android.internal.R.dimen.notification_text_size);
2629 contentView.setTextViewTextSize(R.id.text, TypedValue.COMPLEX_UNIT_PX, regularTextSize);
2630 }
2631
2632 private void resetStandardTemplate(RemoteViews contentView) {
2633 removeLargeIconBackground(contentView);
2634 contentView.setViewPadding(R.id.icon, 0, 0, 0, 0);
2635 contentView.setImageViewResource(R.id.icon, 0);
2636 contentView.setInt(R.id.icon, "setBackgroundResource", 0);
2637 contentView.setViewVisibility(R.id.right_icon, View.GONE);
2638 contentView.setInt(R.id.right_icon, "setBackgroundResource", 0);
2639 contentView.setImageViewResource(R.id.right_icon, 0);
2640 contentView.setImageViewResource(R.id.icon, 0);
2641 contentView.setTextViewText(R.id.title, null);
2642 contentView.setTextViewText(R.id.text, null);
2643 unshrinkLine3Text(contentView);
2644 contentView.setTextViewText(R.id.text2, null);
2645 contentView.setViewVisibility(R.id.text2, View.GONE);
2646 contentView.setViewVisibility(R.id.info, View.GONE);
2647 contentView.setViewVisibility(R.id.time, View.GONE);
2648 contentView.setViewVisibility(R.id.line3, View.GONE);
2649 contentView.setViewVisibility(R.id.overflow_divider, View.GONE);
2650 contentView.setViewVisibility(R.id.progress, View.GONE);
2651 }
2652
Jorim Jaggi445d3c02014-08-19 22:33:42 +02002653 private RemoteViews applyStandardTemplate(int resId) {
Svet Ganov0da85b62014-08-06 14:11:37 -07002654 RemoteViews contentView = new BuilderRemoteViews(mContext.getPackageName(),
2655 mOriginatingUserId, resId);
Dan Sandler539aad42014-08-04 00:43:39 -04002656
Christoph Studerfe718432014-09-01 18:21:18 +02002657 resetStandardTemplate(contentView);
2658
Daniel Sandler9f7936a2012-05-21 16:14:28 -04002659 boolean showLine3 = false;
2660 boolean showLine2 = false;
Kenny Guy98193ea2014-07-24 19:54:37 +01002661 boolean contentTextInLine2 = false;
Dan Sandler190d58d2014-05-15 09:33:39 -04002662
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01002663 if (mLargeIcon != null) {
2664 contentView.setImageViewBitmap(R.id.icon, mLargeIcon);
Christoph Studer239f8352014-08-25 15:13:18 +02002665 processLargeLegacyIcon(mLargeIcon, contentView);
Dan Sandler190d58d2014-05-15 09:33:39 -04002666 contentView.setImageViewResource(R.id.right_icon, mSmallIcon);
2667 contentView.setViewVisibility(R.id.right_icon, View.VISIBLE);
2668 processSmallRightIcon(mSmallIcon, contentView);
2669 } else { // small icon at left
2670 contentView.setImageViewResource(R.id.icon, mSmallIcon);
2671 contentView.setViewVisibility(R.id.icon, View.VISIBLE);
2672 processSmallIconAsLarge(mSmallIcon, contentView);
Joe Onorato561d3852010-11-20 18:09:34 -08002673 }
2674 if (mContentTitle != null) {
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01002675 contentView.setTextViewText(R.id.title, processLegacyText(mContentTitle));
Joe Onorato561d3852010-11-20 18:09:34 -08002676 }
2677 if (mContentText != null) {
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01002678 contentView.setTextViewText(R.id.text, processLegacyText(mContentText));
Daniel Sandler9f7936a2012-05-21 16:14:28 -04002679 showLine3 = true;
Joe Onorato561d3852010-11-20 18:09:34 -08002680 }
2681 if (mContentInfo != null) {
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01002682 contentView.setTextViewText(R.id.info, processLegacyText(mContentInfo));
Jeff Sharkey1c400132011-08-05 14:50:13 -07002683 contentView.setViewVisibility(R.id.info, View.VISIBLE);
Daniel Sandler9f7936a2012-05-21 16:14:28 -04002684 showLine3 = true;
Joe Onorato561d3852010-11-20 18:09:34 -08002685 } else if (mNumber > 0) {
Daniel Sandlerebce0112011-06-16 16:44:51 -04002686 final int tooBig = mContext.getResources().getInteger(
2687 R.integer.status_bar_notification_info_maxnum);
2688 if (mNumber > tooBig) {
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01002689 contentView.setTextViewText(R.id.info, processLegacyText(
2690 mContext.getResources().getString(
2691 R.string.status_bar_notification_info_overflow)));
Joe Onorato059a2f82011-01-04 10:27:01 -08002692 } else {
2693 NumberFormat f = NumberFormat.getIntegerInstance();
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01002694 contentView.setTextViewText(R.id.info, processLegacyText(f.format(mNumber)));
Joe Onorato059a2f82011-01-04 10:27:01 -08002695 }
Jeff Sharkey1c400132011-08-05 14:50:13 -07002696 contentView.setViewVisibility(R.id.info, View.VISIBLE);
Daniel Sandler9f7936a2012-05-21 16:14:28 -04002697 showLine3 = true;
Joe Onorato561d3852010-11-20 18:09:34 -08002698 } else {
2699 contentView.setViewVisibility(R.id.info, View.GONE);
2700 }
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002701
Daniel Sandler9f7936a2012-05-21 16:14:28 -04002702 // Need to show three lines?
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002703 if (mSubText != null) {
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01002704 contentView.setTextViewText(R.id.text, processLegacyText(mSubText));
Daniel Sandler9f7936a2012-05-21 16:14:28 -04002705 if (mContentText != null) {
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01002706 contentView.setTextViewText(R.id.text2, processLegacyText(mContentText));
Daniel Sandler9f7936a2012-05-21 16:14:28 -04002707 contentView.setViewVisibility(R.id.text2, View.VISIBLE);
2708 showLine2 = true;
Kenny Guy98193ea2014-07-24 19:54:37 +01002709 contentTextInLine2 = true;
Daniel Sandler9f7936a2012-05-21 16:14:28 -04002710 } else {
2711 contentView.setViewVisibility(R.id.text2, View.GONE);
2712 }
Jeff Sharkey1c400132011-08-05 14:50:13 -07002713 } else {
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002714 contentView.setViewVisibility(R.id.text2, View.GONE);
2715 if (mProgressMax != 0 || mProgressIndeterminate) {
2716 contentView.setProgressBar(
2717 R.id.progress, mProgressMax, mProgress, mProgressIndeterminate);
2718 contentView.setViewVisibility(R.id.progress, View.VISIBLE);
Daniel Sandler9f7936a2012-05-21 16:14:28 -04002719 showLine2 = true;
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002720 } else {
2721 contentView.setViewVisibility(R.id.progress, View.GONE);
2722 }
Jeff Sharkey1c400132011-08-05 14:50:13 -07002723 }
Daniel Sandler9f7936a2012-05-21 16:14:28 -04002724 if (showLine2) {
Jorim Jaggi445d3c02014-08-19 22:33:42 +02002725
2726 // need to shrink all the type to make sure everything fits
Jorim Jaggid05aa3e2014-08-28 17:52:27 +02002727 shrinkLine3Text(contentView);
Daniel Sandler9f7936a2012-05-21 16:14:28 -04002728 }
2729
Daniel Sandler0c890492012-09-12 17:23:10 -07002730 if (mWhen != 0 && mShowWhen) {
Daniel Sandlera2985ed2012-04-03 16:42:00 -04002731 if (mUseChronometer) {
2732 contentView.setViewVisibility(R.id.chronometer, View.VISIBLE);
2733 contentView.setLong(R.id.chronometer, "setBase",
2734 mWhen + (SystemClock.elapsedRealtime() - System.currentTimeMillis()));
2735 contentView.setBoolean(R.id.chronometer, "setStarted", true);
2736 } else {
2737 contentView.setViewVisibility(R.id.time, View.VISIBLE);
2738 contentView.setLong(R.id.time, "setTime", mWhen);
2739 }
Daniel Sandler0c890492012-09-12 17:23:10 -07002740 } else {
2741 contentView.setViewVisibility(R.id.time, View.GONE);
Joe Onorato561d3852010-11-20 18:09:34 -08002742 }
Daniel Sandler0c890492012-09-12 17:23:10 -07002743
Jorim Jaggi445d3c02014-08-19 22:33:42 +02002744 // Adjust padding depending on line count and font size.
2745 contentView.setViewPadding(R.id.line1, 0, calculateTopPadding(mContext,
Jorim Jaggid05aa3e2014-08-28 17:52:27 +02002746 mHasThreeLines, mContext.getResources().getConfiguration().fontScale),
Jorim Jaggi445d3c02014-08-19 22:33:42 +02002747 0, 0);
2748
Kenny Guy98193ea2014-07-24 19:54:37 +01002749 // We want to add badge to first line of text.
2750 boolean addedBadge = addProfileBadge(contentView,
2751 contentTextInLine2 ? R.id.profile_badge_line2 : R.id.profile_badge_line3);
2752 // If we added the badge to line 3 then we should show line 3.
2753 if (addedBadge && !contentTextInLine2) {
2754 showLine3 = true;
2755 }
2756
2757 // Note getStandardView may hide line 3 again.
Daniel Sandler9f7936a2012-05-21 16:14:28 -04002758 contentView.setViewVisibility(R.id.line3, showLine3 ? View.VISIBLE : View.GONE);
Daniel Sandler6387d2f2012-05-22 13:44:09 -04002759 contentView.setViewVisibility(R.id.overflow_divider, showLine3 ? View.VISIBLE : View.GONE);
Joe Onorato561d3852010-11-20 18:09:34 -08002760 return contentView;
2761 }
2762
Jorim Jaggi445d3c02014-08-19 22:33:42 +02002763 /**
2764 * Logic to find out whether the notification is going to have three lines in the contracted
2765 * layout. This is used to adjust the top padding.
2766 *
2767 * @return true if the notification is going to have three lines; false if the notification
2768 * is going to have one or two lines
2769 */
2770 private boolean hasThreeLines() {
Jorim Jaggid05aa3e2014-08-28 17:52:27 +02002771 boolean contentTextInLine2 = mSubText != null && mContentText != null;
2772
2773 // If we have content text in line 2, badge goes into line 2, or line 3 otherwise
2774 boolean badgeInLine3 = getProfileBadgeDrawable() != null && !contentTextInLine2;
2775 boolean hasLine3 = mContentText != null || mContentInfo != null || mNumber > 0
2776 || badgeInLine3;
Jorim Jaggi445d3c02014-08-19 22:33:42 +02002777 boolean hasLine2 = (mSubText != null && mContentText != null) ||
2778 (mSubText == null && (mProgressMax != 0 || mProgressIndeterminate));
2779 return hasLine2 && hasLine3;
2780 }
2781
2782 /**
2783 * @hide
2784 */
2785 public static int calculateTopPadding(Context ctx, boolean hasThreeLines,
2786 float fontScale) {
2787 int padding = ctx.getResources().getDimensionPixelSize(hasThreeLines
2788 ? R.dimen.notification_top_pad_narrow
2789 : R.dimen.notification_top_pad);
2790 int largePadding = ctx.getResources().getDimensionPixelSize(hasThreeLines
2791 ? R.dimen.notification_top_pad_large_text_narrow
2792 : R.dimen.notification_top_pad_large_text);
2793 float largeFactor = (MathUtils.constrain(fontScale, 1.0f, LARGE_TEXT_SCALE) - 1f)
2794 / (LARGE_TEXT_SCALE - 1f);
2795
2796 // Linearly interpolate the padding between large and normal with the font scale ranging
2797 // from 1f to LARGE_TEXT_SCALE
2798 return Math.round((1 - largeFactor) * padding + largeFactor * largePadding);
2799 }
2800
Christoph Studerfe718432014-09-01 18:21:18 +02002801 private void resetStandardTemplateWithActions(RemoteViews big) {
2802 big.setViewVisibility(R.id.actions, View.GONE);
2803 big.setViewVisibility(R.id.action_divider, View.GONE);
2804 big.removeAllViews(R.id.actions);
2805 }
2806
Daniel Sandler96fd7c12012-03-30 16:37:36 -04002807 private RemoteViews applyStandardTemplateWithActions(int layoutId) {
Jorim Jaggi445d3c02014-08-19 22:33:42 +02002808 RemoteViews big = applyStandardTemplate(layoutId);
Daniel Sandler96fd7c12012-03-30 16:37:36 -04002809
Christoph Studerfe718432014-09-01 18:21:18 +02002810 resetStandardTemplateWithActions(big);
2811
Daniel Sandler96fd7c12012-03-30 16:37:36 -04002812 int N = mActions.size();
2813 if (N > 0) {
Daniel Sandler96fd7c12012-03-30 16:37:36 -04002814 big.setViewVisibility(R.id.actions, View.VISIBLE);
Daniel Sandler6387d2f2012-05-22 13:44:09 -04002815 big.setViewVisibility(R.id.action_divider, View.VISIBLE);
Daniel Sandler8680bf82012-05-15 16:52:52 -04002816 if (N>MAX_ACTION_BUTTONS) N=MAX_ACTION_BUTTONS;
Daniel Sandler96fd7c12012-03-30 16:37:36 -04002817 for (int i=0; i<N; i++) {
2818 final RemoteViews button = generateActionButton(mActions.get(i));
Daniel Sandler96fd7c12012-03-30 16:37:36 -04002819 big.addView(R.id.actions, button);
2820 }
2821 }
2822 return big;
2823 }
2824
Joe Onorato46439ce2010-11-19 13:56:21 -08002825 private RemoteViews makeContentView() {
2826 if (mContentView != null) {
2827 return mContentView;
2828 } else {
Jorim Jaggi445d3c02014-08-19 22:33:42 +02002829 return applyStandardTemplate(getBaseLayoutResource());
Joe Onorato46439ce2010-11-19 13:56:21 -08002830 }
2831 }
2832
2833 private RemoteViews makeTickerView() {
2834 if (mTickerView != null) {
2835 return mTickerView;
Joe Onorato46439ce2010-11-19 13:56:21 -08002836 }
Dan Sandler5fcdf6e2014-07-18 11:31:15 -04002837 return null; // tickers are not created by default anymore
Joe Onorato46439ce2010-11-19 13:56:21 -08002838 }
2839
Daniel Sandler96fd7c12012-03-30 16:37:36 -04002840 private RemoteViews makeBigContentView() {
2841 if (mActions.size() == 0) return null;
2842
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01002843 return applyStandardTemplateWithActions(getBigBaseLayoutResource());
Daniel Sandler96fd7c12012-03-30 16:37:36 -04002844 }
2845
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01002846 private RemoteViews makeHeadsUpContentView() {
Chris Wren8fd39ec2014-02-27 17:43:26 -05002847 if (mActions.size() == 0) return null;
2848
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01002849 return applyStandardTemplateWithActions(getBigBaseLayoutResource());
Chris Wren8fd39ec2014-02-27 17:43:26 -05002850 }
2851
2852
Daniel Sandler96fd7c12012-03-30 16:37:36 -04002853 private RemoteViews generateActionButton(Action action) {
Daniel Sandler8680bf82012-05-15 16:52:52 -04002854 final boolean tombstone = (action.actionIntent == null);
Joe Malin8d40d042012-11-05 11:36:40 -08002855 RemoteViews button = new RemoteViews(mContext.getPackageName(),
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01002856 tombstone ? getActionTombstoneLayoutResource()
2857 : getActionLayoutResource());
Chris Wren8749ac8a2013-12-03 14:31:01 -05002858 button.setTextViewCompoundDrawablesRelative(R.id.action0, action.icon, 0, 0, 0);
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01002859 button.setTextViewText(R.id.action0, processLegacyText(action.title));
Daniel Sandler8680bf82012-05-15 16:52:52 -04002860 if (!tombstone) {
Daniel Sandlere5518842012-05-10 16:20:40 -04002861 button.setOnClickPendingIntent(R.id.action0, action.actionIntent);
Daniel Sandlere5518842012-05-10 16:20:40 -04002862 }
Daniel Sandler96fd7c12012-03-30 16:37:36 -04002863 button.setContentDescription(R.id.action0, action.title);
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01002864 processLegacyAction(action, button);
Daniel Sandler96fd7c12012-03-30 16:37:36 -04002865 return button;
2866 }
2867
Joe Onoratocb109a02011-01-18 17:57:41 -08002868 /**
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01002869 * @return Whether we are currently building a notification from a legacy (an app that
Alan Viverette3cb07a462014-06-06 14:19:53 -07002870 * doesn't create material notifications by itself) app.
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01002871 */
2872 private boolean isLegacy() {
Dan Sandler26e81cf2014-05-06 10:01:27 -04002873 return mColorUtil != null;
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01002874 }
2875
2876 private void processLegacyAction(Action action, RemoteViews button) {
Christoph Studer239f8352014-08-25 15:13:18 +02002877 if (!isLegacy() || mColorUtil.isGrayscale(mContext, action.icon)) {
2878 button.setTextViewCompoundDrawablesRelativeColorFilter(R.id.action0, 0,
2879 mContext.getResources().getColor(R.color.notification_action_color_filter),
2880 PorterDuff.Mode.MULTIPLY);
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01002881 }
2882 }
2883
2884 private CharSequence processLegacyText(CharSequence charSequence) {
2885 if (isLegacy()) {
Dan Sandler26e81cf2014-05-06 10:01:27 -04002886 return mColorUtil.invertCharSequenceColors(charSequence);
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01002887 } else {
2888 return charSequence;
2889 }
2890 }
2891
Dan Sandler26e81cf2014-05-06 10:01:27 -04002892 /**
2893 * Apply any necessary background to smallIcons being used in the largeIcon spot.
2894 */
2895 private void processSmallIconAsLarge(int largeIconId, RemoteViews contentView) {
2896 if (!isLegacy() || mColorUtil.isGrayscale(mContext, largeIconId)) {
2897 applyLargeIconBackground(contentView);
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01002898 }
2899 }
2900
Dan Sandler26e81cf2014-05-06 10:01:27 -04002901 /**
2902 * Apply any necessary background to a largeIcon if it's a fake smallIcon (that is,
2903 * if it's grayscale).
2904 */
2905 // TODO: also check bounds, transparency, that sort of thing.
Christoph Studer239f8352014-08-25 15:13:18 +02002906 private void processLargeLegacyIcon(Bitmap largeIcon, RemoteViews contentView) {
2907 if (isLegacy() && mColorUtil.isGrayscale(largeIcon)) {
Dan Sandler26e81cf2014-05-06 10:01:27 -04002908 applyLargeIconBackground(contentView);
Dan Sandler190d58d2014-05-15 09:33:39 -04002909 } else {
2910 removeLargeIconBackground(contentView);
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01002911 }
2912 }
2913
Dan Sandler26e81cf2014-05-06 10:01:27 -04002914 /**
2915 * Add a colored circle behind the largeIcon slot.
2916 */
2917 private void applyLargeIconBackground(RemoteViews contentView) {
2918 contentView.setInt(R.id.icon, "setBackgroundResource",
Jorim Jaggi445d3c02014-08-19 22:33:42 +02002919 R.drawable.notification_icon_legacy_bg);
Dan Sandler26e81cf2014-05-06 10:01:27 -04002920
2921 contentView.setDrawableParameters(
2922 R.id.icon,
2923 true,
2924 -1,
Jorim Jaggi74419312014-06-10 20:57:21 +02002925 resolveColor(),
Dan Sandler26e81cf2014-05-06 10:01:27 -04002926 PorterDuff.Mode.SRC_ATOP,
2927 -1);
Jorim Jaggi445d3c02014-08-19 22:33:42 +02002928
2929 int padding = mContext.getResources().getDimensionPixelSize(
2930 R.dimen.notification_large_icon_circle_padding);
2931 contentView.setViewPadding(R.id.icon, padding, padding, padding, padding);
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01002932 }
2933
Dan Sandler190d58d2014-05-15 09:33:39 -04002934 private void removeLargeIconBackground(RemoteViews contentView) {
2935 contentView.setInt(R.id.icon, "setBackgroundResource", 0);
2936 }
2937
Dan Sandler26e81cf2014-05-06 10:01:27 -04002938 /**
2939 * Recolor small icons when used in the R.id.right_icon slot.
2940 */
Dan Sandler190d58d2014-05-15 09:33:39 -04002941 private void processSmallRightIcon(int smallIconDrawableId,
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01002942 RemoteViews contentView) {
Dan Sandler26e81cf2014-05-06 10:01:27 -04002943 if (!isLegacy() || mColorUtil.isGrayscale(mContext, smallIconDrawableId)) {
Dan Sandler190d58d2014-05-15 09:33:39 -04002944 contentView.setDrawableParameters(R.id.right_icon, false, -1,
2945 0xFFFFFFFF,
2946 PorterDuff.Mode.SRC_ATOP, -1);
2947
2948 contentView.setInt(R.id.right_icon,
2949 "setBackgroundResource",
2950 R.drawable.notification_icon_legacy_bg);
2951
2952 contentView.setDrawableParameters(
2953 R.id.right_icon,
2954 true,
2955 -1,
Jorim Jaggi74419312014-06-10 20:57:21 +02002956 resolveColor(),
Dan Sandler190d58d2014-05-15 09:33:39 -04002957 PorterDuff.Mode.SRC_ATOP,
2958 -1);
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01002959 }
2960 }
2961
Jorim Jaggi74419312014-06-10 20:57:21 +02002962 private int sanitizeColor() {
2963 if (mColor != COLOR_DEFAULT) {
2964 mColor |= 0xFF000000; // no alpha for custom colors
2965 }
2966 return mColor;
2967 }
2968
Dan Sandler26e81cf2014-05-06 10:01:27 -04002969 private int resolveColor() {
2970 if (mColor == COLOR_DEFAULT) {
Jorim Jaggi74419312014-06-10 20:57:21 +02002971 return mContext.getResources().getColor(R.color.notification_icon_bg_color);
Dan Sandler26e81cf2014-05-06 10:01:27 -04002972 }
2973 return mColor;
2974 }
2975
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01002976 /**
Chris Wrenfbd96ba2012-05-01 12:03:58 -04002977 * Apply the unstyled operations and return a new {@link Notification} object.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04002978 * @hide
Joe Onoratocb109a02011-01-18 17:57:41 -08002979 */
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04002980 public Notification buildUnstyled() {
Joe Onorato46439ce2010-11-19 13:56:21 -08002981 Notification n = new Notification();
2982 n.when = mWhen;
2983 n.icon = mSmallIcon;
2984 n.iconLevel = mSmallIconLevel;
Joe Onorato8595a3d2010-11-19 18:12:07 -08002985 n.number = mNumber;
Dan Sandler26e81cf2014-05-06 10:01:27 -04002986
Jorim Jaggi74419312014-06-10 20:57:21 +02002987 n.color = sanitizeColor();
Dan Sandler26e81cf2014-05-06 10:01:27 -04002988
Christoph Studer4600f9b2014-07-22 22:44:43 +02002989 setBuilderContentView(n, makeContentView());
Joe Onorato46439ce2010-11-19 13:56:21 -08002990 n.contentIntent = mContentIntent;
2991 n.deleteIntent = mDeleteIntent;
2992 n.fullScreenIntent = mFullScreenIntent;
2993 n.tickerText = mTickerText;
2994 n.tickerView = makeTickerView();
2995 n.largeIcon = mLargeIcon;
2996 n.sound = mSound;
2997 n.audioStreamType = mAudioStreamType;
John Spurlockc0650f022014-07-19 13:22:39 -04002998 n.audioAttributes = mAudioAttributes;
Joe Onorato46439ce2010-11-19 13:56:21 -08002999 n.vibrate = mVibrate;
3000 n.ledARGB = mLedArgb;
3001 n.ledOnMS = mLedOnMs;
3002 n.ledOffMS = mLedOffMs;
3003 n.defaults = mDefaults;
3004 n.flags = mFlags;
Christoph Studer4600f9b2014-07-22 22:44:43 +02003005 setBuilderBigContentView(n, makeBigContentView());
3006 setBuilderHeadsUpContentView(n, makeHeadsUpContentView());
Daniel Sandler26c13432013-04-04 11:01:04 -04003007 if (mLedOnMs != 0 || mLedOffMs != 0) {
Joe Onorato8d0b6552010-11-22 16:09:29 -08003008 n.flags |= FLAG_SHOW_LIGHTS;
3009 }
3010 if ((mDefaults & DEFAULT_LIGHTS) != 0) {
3011 n.flags |= FLAG_SHOW_LIGHTS;
3012 }
John Spurlockfd7f1e02014-03-18 16:41:57 -04003013 n.category = mCategory;
Griff Hazen5cadc3b2014-05-20 09:55:39 -07003014 n.mGroupKey = mGroupKey;
3015 n.mSortKey = mSortKey;
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003016 n.priority = mPriority;
Daniel Sandlera0a938c2012-03-15 08:42:37 -04003017 if (mActions.size() > 0) {
3018 n.actions = new Action[mActions.size()];
3019 mActions.toArray(n.actions);
3020 }
Dan Sandler0bf2ed82013-12-21 23:33:41 -06003021 n.visibility = mVisibility;
3022
3023 if (mPublicVersion != null) {
3024 n.publicVersion = new Notification();
3025 mPublicVersion.cloneInto(n.publicVersion, true);
3026 }
Christoph Studer4600f9b2014-07-22 22:44:43 +02003027 // Note: If you're adding new fields, also update restoreFromNotitification().
Joe Onorato46439ce2010-11-19 13:56:21 -08003028 return n;
3029 }
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003030
3031 /**
Daniel Sandlerf45564e2013-04-15 15:05:08 -04003032 * Capture, in the provided bundle, semantic information used in the construction of
3033 * this Notification object.
3034 * @hide
3035 */
Griff Hazen720042b2014-02-24 15:46:56 -08003036 public void populateExtras(Bundle extras) {
Daniel Sandlerf45564e2013-04-15 15:05:08 -04003037 // Store original information used in the construction of this object
Christoph Studer7ac80e62014-08-04 16:01:57 +02003038 extras.putInt(EXTRA_ORIGINATING_USERID, mOriginatingUserId);
Christoph Studer4600f9b2014-07-22 22:44:43 +02003039 extras.putString(EXTRA_REBUILD_CONTEXT_PACKAGE, mContext.getPackageName());
Daniel Sandlerf45564e2013-04-15 15:05:08 -04003040 extras.putCharSequence(EXTRA_TITLE, mContentTitle);
3041 extras.putCharSequence(EXTRA_TEXT, mContentText);
3042 extras.putCharSequence(EXTRA_SUB_TEXT, mSubText);
3043 extras.putCharSequence(EXTRA_INFO_TEXT, mContentInfo);
3044 extras.putInt(EXTRA_SMALL_ICON, mSmallIcon);
3045 extras.putInt(EXTRA_PROGRESS, mProgress);
3046 extras.putInt(EXTRA_PROGRESS_MAX, mProgressMax);
3047 extras.putBoolean(EXTRA_PROGRESS_INDETERMINATE, mProgressIndeterminate);
3048 extras.putBoolean(EXTRA_SHOW_CHRONOMETER, mUseChronometer);
3049 extras.putBoolean(EXTRA_SHOW_WHEN, mShowWhen);
John Spurlockac08a472013-06-10 11:37:08 -04003050 if (mLargeIcon != null) {
3051 extras.putParcelable(EXTRA_LARGE_ICON, mLargeIcon);
3052 }
Chris Wrendde75302014-03-26 17:24:15 -04003053 if (!mPeople.isEmpty()) {
3054 extras.putStringArray(EXTRA_PEOPLE, mPeople.toArray(new String[mPeople.size()]));
3055 }
Christoph Studer4600f9b2014-07-22 22:44:43 +02003056 // NOTE: If you're adding new extras also update restoreFromNotification().
3057 }
3058
3059
3060 /**
3061 * @hide
3062 */
3063 public static void stripForDelivery(Notification n) {
3064 if (!STRIP_AND_REBUILD) {
3065 return;
3066 }
3067
3068 String templateClass = n.extras.getString(EXTRA_TEMPLATE);
3069 // Only strip views for known Styles because we won't know how to
3070 // re-create them otherwise.
3071 boolean stripViews = TextUtils.isEmpty(templateClass) ||
3072 getNotificationStyleClass(templateClass) != null;
3073
3074 boolean isStripped = false;
3075
3076 if (n.largeIcon != null && n.extras.containsKey(EXTRA_LARGE_ICON)) {
3077 // TODO: Would like to check for equality here, but if the notification
3078 // has been cloned, we can't.
3079 n.largeIcon = null;
3080 n.extras.putBoolean(EXTRA_REBUILD_LARGE_ICON, true);
3081 isStripped = true;
3082 }
3083 // Get rid of unmodified BuilderRemoteViews.
3084
3085 if (stripViews &&
3086 n.contentView instanceof BuilderRemoteViews &&
3087 n.extras.getInt(EXTRA_REBUILD_CONTENT_VIEW_ACTION_COUNT, -1) ==
3088 n.contentView.getSequenceNumber()) {
3089 n.contentView = null;
3090 n.extras.putBoolean(EXTRA_REBUILD_CONTENT_VIEW, true);
3091 n.extras.remove(EXTRA_REBUILD_CONTENT_VIEW_ACTION_COUNT);
3092 isStripped = true;
3093 }
3094 if (stripViews &&
3095 n.bigContentView instanceof BuilderRemoteViews &&
3096 n.extras.getInt(EXTRA_REBUILD_BIG_CONTENT_VIEW_ACTION_COUNT, -1) ==
3097 n.bigContentView.getSequenceNumber()) {
3098 n.bigContentView = null;
3099 n.extras.putBoolean(EXTRA_REBUILD_BIG_CONTENT_VIEW, true);
3100 n.extras.remove(EXTRA_REBUILD_BIG_CONTENT_VIEW_ACTION_COUNT);
3101 isStripped = true;
3102 }
3103 if (stripViews &&
3104 n.headsUpContentView instanceof BuilderRemoteViews &&
3105 n.extras.getInt(EXTRA_REBUILD_HEADS_UP_CONTENT_VIEW_ACTION_COUNT, -1) ==
3106 n.headsUpContentView.getSequenceNumber()) {
3107 n.headsUpContentView = null;
3108 n.extras.putBoolean(EXTRA_REBUILD_HEADS_UP_CONTENT_VIEW, true);
3109 n.extras.remove(EXTRA_REBUILD_HEADS_UP_CONTENT_VIEW_ACTION_COUNT);
3110 isStripped = true;
3111 }
3112
3113 if (isStripped) {
3114 n.extras.putBoolean(EXTRA_NEEDS_REBUILD, true);
3115 }
3116 }
3117
3118 /**
3119 * @hide
3120 */
3121 public static Notification rebuild(Context context, Notification n) {
3122 Bundle extras = n.extras;
3123 if (!extras.getBoolean(EXTRA_NEEDS_REBUILD)) return n;
3124 extras.remove(EXTRA_NEEDS_REBUILD);
3125
3126 // Re-create notification context so we can access app resources.
3127 String packageName = extras.getString(EXTRA_REBUILD_CONTEXT_PACKAGE);
3128 Context builderContext;
3129 try {
3130 builderContext = context.createPackageContext(packageName,
3131 Context.CONTEXT_RESTRICTED);
3132 } catch (NameNotFoundException e) {
3133 Log.e(TAG, "Package name " + packageName + " not found");
3134 builderContext = context; // try with our context
3135 }
3136
3137 Builder b = new Builder(builderContext, n);
3138 return b.rebuild();
3139 }
3140
3141 /**
3142 * Rebuilds the notification passed in to the rebuild-constructor
3143 * {@link #Builder(Context, Notification)}.
3144 *
3145 * <p>
3146 * Throws IllegalStateException when invoked on a Builder that isn't in rebuild mode.
3147 *
3148 * @hide
3149 */
3150 private Notification rebuild() {
3151 if (mRebuildNotification == null) {
3152 throw new IllegalStateException("rebuild() only valid when in 'rebuild' mode.");
3153 }
Jorim Jaggid05aa3e2014-08-28 17:52:27 +02003154 mHasThreeLines = hasThreeLines();
Christoph Studer4600f9b2014-07-22 22:44:43 +02003155
3156 Bundle extras = mRebuildNotification.extras;
3157
3158 if (extras.getBoolean(EXTRA_REBUILD_LARGE_ICON)) {
3159 mRebuildNotification.largeIcon = extras.getParcelable(EXTRA_LARGE_ICON);
3160 }
3161 extras.remove(EXTRA_REBUILD_LARGE_ICON);
3162
3163 if (extras.getBoolean(EXTRA_REBUILD_CONTENT_VIEW)) {
3164 setBuilderContentView(mRebuildNotification, makeContentView());
3165 if (mStyle != null) {
3166 mStyle.populateContentView(mRebuildNotification);
3167 }
3168 }
3169 extras.remove(EXTRA_REBUILD_CONTENT_VIEW);
3170
3171 if (extras.getBoolean(EXTRA_REBUILD_BIG_CONTENT_VIEW)) {
3172 setBuilderBigContentView(mRebuildNotification, makeBigContentView());
3173 if (mStyle != null) {
3174 mStyle.populateBigContentView(mRebuildNotification);
3175 }
3176 }
3177 extras.remove(EXTRA_REBUILD_BIG_CONTENT_VIEW);
3178
3179 if (extras.getBoolean(EXTRA_REBUILD_HEADS_UP_CONTENT_VIEW)) {
3180 setBuilderHeadsUpContentView(mRebuildNotification, makeHeadsUpContentView());
3181 if (mStyle != null) {
3182 mStyle.populateHeadsUpContentView(mRebuildNotification);
3183 }
3184 }
3185 extras.remove(EXTRA_REBUILD_HEADS_UP_CONTENT_VIEW);
3186
Jorim Jaggid05aa3e2014-08-28 17:52:27 +02003187 mHasThreeLines = false;
Christoph Studer4600f9b2014-07-22 22:44:43 +02003188 return mRebuildNotification;
3189 }
3190
3191 private static Class<? extends Style> getNotificationStyleClass(String templateClass) {
3192 Class<? extends Style>[] classes = new Class[]{
3193 BigTextStyle.class, BigPictureStyle.class, InboxStyle.class, MediaStyle.class};
3194 for (Class<? extends Style> innerClass : classes) {
3195 if (templateClass.equals(innerClass.getName())) {
3196 return innerClass;
3197 }
3198 }
3199 return null;
3200 }
3201
3202 private void setBuilderContentView(Notification n, RemoteViews contentView) {
3203 n.contentView = contentView;
3204 if (contentView instanceof BuilderRemoteViews) {
3205 mRebuildBundle.putInt(Builder.EXTRA_REBUILD_CONTENT_VIEW_ACTION_COUNT,
3206 contentView.getSequenceNumber());
3207 }
3208 }
3209
3210 private void setBuilderBigContentView(Notification n, RemoteViews bigContentView) {
3211 n.bigContentView = bigContentView;
3212 if (bigContentView instanceof BuilderRemoteViews) {
3213 mRebuildBundle.putInt(Builder.EXTRA_REBUILD_BIG_CONTENT_VIEW_ACTION_COUNT,
3214 bigContentView.getSequenceNumber());
3215 }
3216 }
3217
3218 private void setBuilderHeadsUpContentView(Notification n,
3219 RemoteViews headsUpContentView) {
3220 n.headsUpContentView = headsUpContentView;
3221 if (headsUpContentView instanceof BuilderRemoteViews) {
3222 mRebuildBundle.putInt(Builder.EXTRA_REBUILD_HEADS_UP_CONTENT_VIEW_ACTION_COUNT,
3223 headsUpContentView.getSequenceNumber());
3224 }
3225 }
3226
3227 private void restoreFromNotification(Notification n) {
3228
3229 // Notification fields.
3230 mWhen = n.when;
3231 mSmallIcon = n.icon;
3232 mSmallIconLevel = n.iconLevel;
3233 mNumber = n.number;
3234
3235 mColor = n.color;
3236
3237 mContentView = n.contentView;
3238 mDeleteIntent = n.deleteIntent;
3239 mFullScreenIntent = n.fullScreenIntent;
3240 mTickerText = n.tickerText;
3241 mTickerView = n.tickerView;
3242 mLargeIcon = n.largeIcon;
3243 mSound = n.sound;
3244 mAudioStreamType = n.audioStreamType;
3245 mAudioAttributes = n.audioAttributes;
3246
3247 mVibrate = n.vibrate;
3248 mLedArgb = n.ledARGB;
3249 mLedOnMs = n.ledOnMS;
3250 mLedOffMs = n.ledOffMS;
3251 mDefaults = n.defaults;
3252 mFlags = n.flags;
3253
3254 mCategory = n.category;
3255 mGroupKey = n.mGroupKey;
3256 mSortKey = n.mSortKey;
3257 mPriority = n.priority;
3258 mActions.clear();
3259 if (n.actions != null) {
3260 Collections.addAll(mActions, n.actions);
3261 }
3262 mVisibility = n.visibility;
3263
3264 mPublicVersion = n.publicVersion;
3265
3266 // Extras.
3267 Bundle extras = n.extras;
Christoph Studer7ac80e62014-08-04 16:01:57 +02003268 mOriginatingUserId = extras.getInt(EXTRA_ORIGINATING_USERID);
Christoph Studer4600f9b2014-07-22 22:44:43 +02003269 mContentTitle = extras.getCharSequence(EXTRA_TITLE);
3270 mContentText = extras.getCharSequence(EXTRA_TEXT);
3271 mSubText = extras.getCharSequence(EXTRA_SUB_TEXT);
3272 mContentInfo = extras.getCharSequence(EXTRA_INFO_TEXT);
3273 mSmallIcon = extras.getInt(EXTRA_SMALL_ICON);
3274 mProgress = extras.getInt(EXTRA_PROGRESS);
3275 mProgressMax = extras.getInt(EXTRA_PROGRESS_MAX);
3276 mProgressIndeterminate = extras.getBoolean(EXTRA_PROGRESS_INDETERMINATE);
3277 mUseChronometer = extras.getBoolean(EXTRA_SHOW_CHRONOMETER);
3278 mShowWhen = extras.getBoolean(EXTRA_SHOW_WHEN);
3279 if (extras.containsKey(EXTRA_LARGE_ICON)) {
3280 mLargeIcon = extras.getParcelable(EXTRA_LARGE_ICON);
3281 }
3282 if (extras.containsKey(EXTRA_PEOPLE)) {
3283 mPeople.clear();
3284 Collections.addAll(mPeople, extras.getStringArray(EXTRA_PEOPLE));
3285 }
Daniel Sandlerf45564e2013-04-15 15:05:08 -04003286 }
3287
3288 /**
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003289 * @deprecated Use {@link #build()} instead.
3290 */
3291 @Deprecated
3292 public Notification getNotification() {
3293 return build();
3294 }
3295
3296 /**
3297 * Combine all of the options that have been set and return a new {@link Notification}
3298 * object.
3299 */
3300 public Notification build() {
Christoph Studer7ac80e62014-08-04 16:01:57 +02003301 mOriginatingUserId = mContext.getUserId();
Jorim Jaggid05aa3e2014-08-28 17:52:27 +02003302 mHasThreeLines = hasThreeLines();
Christoph Studer943aa672014-08-03 20:31:16 +02003303
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04003304 Notification n = buildUnstyled();
Daniel Sandlerf45564e2013-04-15 15:05:08 -04003305
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003306 if (mStyle != null) {
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04003307 n = mStyle.buildStyled(n);
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003308 }
Daniel Sandlerf45564e2013-04-15 15:05:08 -04003309
Christoph Studer4600f9b2014-07-22 22:44:43 +02003310 if (mExtras != null) {
3311 n.extras.putAll(mExtras);
3312 }
3313
3314 if (mRebuildBundle.size() > 0) {
3315 n.extras.putAll(mRebuildBundle);
3316 mRebuildBundle.clear();
3317 }
Daniel Sandlerf45564e2013-04-15 15:05:08 -04003318
Griff Hazen720042b2014-02-24 15:46:56 -08003319 populateExtras(n.extras);
Daniel Sandlerf45564e2013-04-15 15:05:08 -04003320 if (mStyle != null) {
3321 mStyle.addExtras(n.extras);
3322 }
3323
Jorim Jaggid05aa3e2014-08-28 17:52:27 +02003324 mHasThreeLines = false;
Daniel Sandlerf45564e2013-04-15 15:05:08 -04003325 return n;
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003326 }
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05003327
3328 /**
3329 * Apply this Builder to an existing {@link Notification} object.
3330 *
3331 * @hide
3332 */
3333 public Notification buildInto(Notification n) {
Daniel Sandler1a497d32013-04-18 14:52:45 -04003334 build().cloneInto(n, true);
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05003335 return n;
3336 }
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01003337
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01003338 private int getBaseLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07003339 return R.layout.notification_template_material_base;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01003340 }
3341
3342 private int getBigBaseLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07003343 return R.layout.notification_template_material_big_base;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01003344 }
3345
3346 private int getBigPictureLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07003347 return R.layout.notification_template_material_big_picture;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01003348 }
3349
3350 private int getBigTextLayoutResource() {
Jorim Jaggi445d3c02014-08-19 22:33:42 +02003351 return R.layout.notification_template_material_big_text;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01003352 }
3353
3354 private int getInboxLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07003355 return R.layout.notification_template_material_inbox;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01003356 }
3357
3358 private int getActionLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07003359 return R.layout.notification_material_action;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01003360 }
3361
3362 private int getActionTombstoneLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07003363 return R.layout.notification_material_action_tombstone;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01003364 }
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003365 }
3366
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003367 /**
3368 * An object that can apply a rich notification style to a {@link Notification.Builder}
3369 * object.
3370 */
Griff Hazendfcb0802014-02-11 12:00:00 -08003371 public static abstract class Style {
Chris Wrend6297db2012-05-03 16:20:13 -04003372 private CharSequence mBigContentTitle;
3373 private CharSequence mSummaryText = null;
Daniel Sandler619738c2012-06-07 16:33:08 -04003374 private boolean mSummaryTextSet = false;
Chris Wrend6297db2012-05-03 16:20:13 -04003375
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003376 protected Builder mBuilder;
3377
Chris Wrend6297db2012-05-03 16:20:13 -04003378 /**
3379 * Overrides ContentTitle in the big form of the template.
3380 * This defaults to the value passed to setContentTitle().
3381 */
3382 protected void internalSetBigContentTitle(CharSequence title) {
3383 mBigContentTitle = title;
3384 }
3385
3386 /**
3387 * Set the first line of text after the detail section in the big form of the template.
3388 */
3389 protected void internalSetSummaryText(CharSequence cs) {
3390 mSummaryText = cs;
Daniel Sandler619738c2012-06-07 16:33:08 -04003391 mSummaryTextSet = true;
Chris Wrend6297db2012-05-03 16:20:13 -04003392 }
3393
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003394 public void setBuilder(Builder builder) {
3395 if (mBuilder != builder) {
3396 mBuilder = builder;
Daniel Sandlerc08dea22012-06-28 08:35:24 -07003397 if (mBuilder != null) {
3398 mBuilder.setStyle(this);
3399 }
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003400 }
3401 }
3402
Chris Wrend6297db2012-05-03 16:20:13 -04003403 protected void checkBuilder() {
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003404 if (mBuilder == null) {
3405 throw new IllegalArgumentException("Style requires a valid Builder object");
3406 }
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003407 }
Chris Wrend6297db2012-05-03 16:20:13 -04003408
3409 protected RemoteViews getStandardView(int layoutId) {
3410 checkBuilder();
3411
Christoph Studer4600f9b2014-07-22 22:44:43 +02003412 // Nasty.
3413 CharSequence oldBuilderContentTitle = mBuilder.mContentTitle;
Chris Wrend6297db2012-05-03 16:20:13 -04003414 if (mBigContentTitle != null) {
3415 mBuilder.setContentTitle(mBigContentTitle);
3416 }
3417
Chris Wrend6297db2012-05-03 16:20:13 -04003418 RemoteViews contentView = mBuilder.applyStandardTemplateWithActions(layoutId);
3419
Christoph Studer4600f9b2014-07-22 22:44:43 +02003420 mBuilder.mContentTitle = oldBuilderContentTitle;
3421
Chris Wrend6297db2012-05-03 16:20:13 -04003422 if (mBigContentTitle != null && mBigContentTitle.equals("")) {
3423 contentView.setViewVisibility(R.id.line1, View.GONE);
Chris Wren67dc9a02012-05-16 01:03:20 -04003424 } else {
3425 contentView.setViewVisibility(R.id.line1, View.VISIBLE);
Chris Wrend6297db2012-05-03 16:20:13 -04003426 }
3427
Daniel Sandler619738c2012-06-07 16:33:08 -04003428 // The last line defaults to the subtext, but can be replaced by mSummaryText
3429 final CharSequence overflowText =
3430 mSummaryTextSet ? mSummaryText
3431 : mBuilder.mSubText;
3432 if (overflowText != null) {
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01003433 contentView.setTextViewText(R.id.text, mBuilder.processLegacyText(overflowText));
Daniel Sandler619738c2012-06-07 16:33:08 -04003434 contentView.setViewVisibility(R.id.overflow_divider, View.VISIBLE);
Daniel Sandler9f7936a2012-05-21 16:14:28 -04003435 contentView.setViewVisibility(R.id.line3, View.VISIBLE);
Daniel Sandler916ad912012-06-13 12:17:07 -04003436 } else {
Kenny Guy98193ea2014-07-24 19:54:37 +01003437 // Clear text in case we use the line to show the profile badge.
3438 contentView.setTextViewText(R.id.text, "");
Daniel Sandler916ad912012-06-13 12:17:07 -04003439 contentView.setViewVisibility(R.id.overflow_divider, View.GONE);
3440 contentView.setViewVisibility(R.id.line3, View.GONE);
Chris Wrend6297db2012-05-03 16:20:13 -04003441 }
3442
3443 return contentView;
3444 }
3445
Daniel Sandlerf45564e2013-04-15 15:05:08 -04003446 /**
Jorim Jaggi445d3c02014-08-19 22:33:42 +02003447 * Changes the padding of the first line such that the big and small content view have the
3448 * same top padding.
3449 *
3450 * @hide
3451 */
3452 protected void applyTopPadding(RemoteViews contentView) {
3453 int topPadding = Builder.calculateTopPadding(mBuilder.mContext,
Jorim Jaggid05aa3e2014-08-28 17:52:27 +02003454 mBuilder.mHasThreeLines,
Jorim Jaggi445d3c02014-08-19 22:33:42 +02003455 mBuilder.mContext.getResources().getConfiguration().fontScale);
3456 contentView.setViewPadding(R.id.line1, 0, topPadding, 0, 0);
3457 }
3458
3459 /**
Daniel Sandlerf45564e2013-04-15 15:05:08 -04003460 * @hide
3461 */
3462 public void addExtras(Bundle extras) {
3463 if (mSummaryTextSet) {
3464 extras.putCharSequence(EXTRA_SUMMARY_TEXT, mSummaryText);
3465 }
3466 if (mBigContentTitle != null) {
3467 extras.putCharSequence(EXTRA_TITLE_BIG, mBigContentTitle);
3468 }
Chris Wren91ad5632013-06-05 15:05:57 -04003469 extras.putString(EXTRA_TEMPLATE, this.getClass().getName());
Daniel Sandlerf45564e2013-04-15 15:05:08 -04003470 }
3471
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04003472 /**
3473 * @hide
3474 */
Christoph Studer4600f9b2014-07-22 22:44:43 +02003475 protected void restoreFromExtras(Bundle extras) {
3476 if (extras.containsKey(EXTRA_SUMMARY_TEXT)) {
3477 mSummaryText = extras.getCharSequence(EXTRA_SUMMARY_TEXT);
3478 mSummaryTextSet = true;
3479 }
3480 if (extras.containsKey(EXTRA_TITLE_BIG)) {
3481 mBigContentTitle = extras.getCharSequence(EXTRA_TITLE_BIG);
3482 }
3483 }
3484
3485
3486 /**
3487 * @hide
3488 */
3489 public Notification buildStyled(Notification wip) {
3490 populateTickerView(wip);
3491 populateContentView(wip);
3492 populateBigContentView(wip);
3493 populateHeadsUpContentView(wip);
3494 return wip;
3495 }
3496
3497 // The following methods are split out so we can re-create notification partially.
3498 /**
3499 * @hide
3500 */
3501 protected void populateTickerView(Notification wip) {}
3502 /**
3503 * @hide
3504 */
3505 protected void populateContentView(Notification wip) {}
3506
3507 /**
3508 * @hide
3509 */
3510 protected void populateBigContentView(Notification wip) {}
3511
3512 /**
3513 * @hide
3514 */
3515 protected void populateHeadsUpContentView(Notification wip) {}
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04003516
3517 /**
3518 * Calls {@link android.app.Notification.Builder#build()} on the Builder this Style is
3519 * attached to.
3520 *
3521 * @return the fully constructed Notification.
3522 */
3523 public Notification build() {
3524 checkBuilder();
3525 return mBuilder.build();
3526 }
Joe Onorato46439ce2010-11-19 13:56:21 -08003527 }
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003528
3529 /**
Daniel Sandler4dfbe832012-04-11 14:51:46 -04003530 * Helper class for generating large-format notifications that include a large image attachment.
Joe Malin8d40d042012-11-05 11:36:40 -08003531 *
Robert Ly91c5ce32014-06-08 15:37:00 -07003532 * Here's how you'd set the <code>BigPictureStyle</code> on a notification:
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003533 * <pre class="prettyprint">
Robert Ly91c5ce32014-06-08 15:37:00 -07003534 * Notification notif = new Notification.Builder(mContext)
3535 * .setContentTitle(&quot;New photo from &quot; + sender.toString())
3536 * .setContentText(subject)
3537 * .setSmallIcon(R.drawable.new_post)
3538 * .setLargeIcon(aBitmap)
3539 * .setStyle(new Notification.BigPictureStyle()
3540 * .bigPicture(aBigBitmap))
3541 * .build();
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003542 * </pre>
Joe Malin8d40d042012-11-05 11:36:40 -08003543 *
Daniel Sandler4dfbe832012-04-11 14:51:46 -04003544 * @see Notification#bigContentView
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003545 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003546 public static class BigPictureStyle extends Style {
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003547 private Bitmap mPicture;
Chris Wren3745a3d2012-05-22 15:11:52 -04003548 private Bitmap mBigLargeIcon;
3549 private boolean mBigLargeIconSet = false;
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003550
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003551 public BigPictureStyle() {
3552 }
3553
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003554 public BigPictureStyle(Builder builder) {
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003555 setBuilder(builder);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003556 }
3557
Chris Wrend6297db2012-05-03 16:20:13 -04003558 /**
3559 * Overrides ContentTitle in the big form of the template.
3560 * This defaults to the value passed to setContentTitle().
3561 */
3562 public BigPictureStyle setBigContentTitle(CharSequence title) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04003563 internalSetBigContentTitle(safeCharSequence(title));
Chris Wrend6297db2012-05-03 16:20:13 -04003564 return this;
3565 }
3566
3567 /**
3568 * Set the first line of text after the detail section in the big form of the template.
3569 */
3570 public BigPictureStyle setSummaryText(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04003571 internalSetSummaryText(safeCharSequence(cs));
Chris Wrend6297db2012-05-03 16:20:13 -04003572 return this;
3573 }
3574
Chris Wren0bd664d2012-08-01 13:56:56 -04003575 /**
3576 * Provide the bitmap to be used as the payload for the BigPicture notification.
3577 */
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003578 public BigPictureStyle bigPicture(Bitmap b) {
3579 mPicture = b;
3580 return this;
3581 }
3582
Chris Wren3745a3d2012-05-22 15:11:52 -04003583 /**
Chris Wren3745a3d2012-05-22 15:11:52 -04003584 * Override the large icon when the big notification is shown.
3585 */
3586 public BigPictureStyle bigLargeIcon(Bitmap b) {
3587 mBigLargeIconSet = true;
3588 mBigLargeIcon = b;
3589 return this;
3590 }
3591
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003592 private RemoteViews makeBigContentView() {
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01003593 RemoteViews contentView = getStandardView(mBuilder.getBigPictureLayoutResource());
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003594
3595 contentView.setImageViewBitmap(R.id.big_picture, mPicture);
3596
Jorim Jaggi445d3c02014-08-19 22:33:42 +02003597 applyTopPadding(contentView);
3598
Kenny Guy98193ea2014-07-24 19:54:37 +01003599 boolean twoTextLines = mBuilder.mSubText != null && mBuilder.mContentText != null;
3600 mBuilder.addProfileBadge(contentView,
3601 twoTextLines ? R.id.profile_badge_line2 : R.id.profile_badge_line3);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003602 return contentView;
3603 }
3604
Daniel Sandlerf45564e2013-04-15 15:05:08 -04003605 /**
3606 * @hide
3607 */
3608 public void addExtras(Bundle extras) {
3609 super.addExtras(extras);
3610
3611 if (mBigLargeIconSet) {
3612 extras.putParcelable(EXTRA_LARGE_ICON_BIG, mBigLargeIcon);
3613 }
3614 extras.putParcelable(EXTRA_PICTURE, mPicture);
3615 }
3616
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04003617 /**
3618 * @hide
3619 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003620 @Override
Christoph Studer4600f9b2014-07-22 22:44:43 +02003621 protected void restoreFromExtras(Bundle extras) {
3622 super.restoreFromExtras(extras);
3623
3624 if (extras.containsKey(EXTRA_LARGE_ICON_BIG)) {
3625 mBigLargeIcon = extras.getParcelable(EXTRA_LARGE_ICON_BIG);
Chris Wren3745a3d2012-05-22 15:11:52 -04003626 }
Christoph Studer4600f9b2014-07-22 22:44:43 +02003627 mPicture = extras.getParcelable(EXTRA_PICTURE);
3628 }
3629
3630 /**
3631 * @hide
3632 */
3633 @Override
3634 public void populateBigContentView(Notification wip) {
3635 mBuilder.setBuilderBigContentView(wip, makeBigContentView());
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003636 }
3637 }
3638
3639 /**
Daniel Sandler4dfbe832012-04-11 14:51:46 -04003640 * Helper class for generating large-format notifications that include a lot of text.
Joe Malin8d40d042012-11-05 11:36:40 -08003641 *
Robert Ly91c5ce32014-06-08 15:37:00 -07003642 * Here's how you'd set the <code>BigTextStyle</code> on a notification:
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003643 * <pre class="prettyprint">
Robert Ly91c5ce32014-06-08 15:37:00 -07003644 * Notification notif = new Notification.Builder(mContext)
3645 * .setContentTitle(&quot;New mail from &quot; + sender.toString())
3646 * .setContentText(subject)
3647 * .setSmallIcon(R.drawable.new_mail)
3648 * .setLargeIcon(aBitmap)
3649 * .setStyle(new Notification.BigTextStyle()
3650 * .bigText(aVeryLongString))
3651 * .build();
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003652 * </pre>
Joe Malin8d40d042012-11-05 11:36:40 -08003653 *
Daniel Sandler4dfbe832012-04-11 14:51:46 -04003654 * @see Notification#bigContentView
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003655 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003656 public static class BigTextStyle extends Style {
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003657 private CharSequence mBigText;
3658
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003659 public BigTextStyle() {
3660 }
3661
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003662 public BigTextStyle(Builder builder) {
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003663 setBuilder(builder);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003664 }
3665
Chris Wrend6297db2012-05-03 16:20:13 -04003666 /**
3667 * Overrides ContentTitle in the big form of the template.
3668 * This defaults to the value passed to setContentTitle().
3669 */
3670 public BigTextStyle setBigContentTitle(CharSequence title) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04003671 internalSetBigContentTitle(safeCharSequence(title));
Chris Wrend6297db2012-05-03 16:20:13 -04003672 return this;
3673 }
3674
3675 /**
3676 * Set the first line of text after the detail section in the big form of the template.
3677 */
3678 public BigTextStyle setSummaryText(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04003679 internalSetSummaryText(safeCharSequence(cs));
Chris Wrend6297db2012-05-03 16:20:13 -04003680 return this;
3681 }
3682
Chris Wren0bd664d2012-08-01 13:56:56 -04003683 /**
3684 * Provide the longer text to be displayed in the big form of the
3685 * template in place of the content text.
3686 */
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003687 public BigTextStyle bigText(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04003688 mBigText = safeCharSequence(cs);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003689 return this;
3690 }
3691
Daniel Sandlerf45564e2013-04-15 15:05:08 -04003692 /**
3693 * @hide
3694 */
3695 public void addExtras(Bundle extras) {
3696 super.addExtras(extras);
3697
Christoph Studer4600f9b2014-07-22 22:44:43 +02003698 extras.putCharSequence(EXTRA_BIG_TEXT, mBigText);
3699 }
3700
3701 /**
3702 * @hide
3703 */
3704 @Override
3705 protected void restoreFromExtras(Bundle extras) {
3706 super.restoreFromExtras(extras);
3707
3708 mBigText = extras.getCharSequence(EXTRA_BIG_TEXT);
Daniel Sandlerf45564e2013-04-15 15:05:08 -04003709 }
3710
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003711 private RemoteViews makeBigContentView() {
Christoph Studer4600f9b2014-07-22 22:44:43 +02003712
3713 // Nasty
3714 CharSequence oldBuilderContentText = mBuilder.mContentText;
Daniel Sandler6387d2f2012-05-22 13:44:09 -04003715 mBuilder.mContentText = null;
Daniel Sandler916ad912012-06-13 12:17:07 -04003716
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01003717 RemoteViews contentView = getStandardView(mBuilder.getBigTextLayoutResource());
Joe Malin8d40d042012-11-05 11:36:40 -08003718
Christoph Studer4600f9b2014-07-22 22:44:43 +02003719 mBuilder.mContentText = oldBuilderContentText;
3720
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01003721 contentView.setTextViewText(R.id.big_text, mBuilder.processLegacyText(mBigText));
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003722 contentView.setViewVisibility(R.id.big_text, View.VISIBLE);
Chris Wren3c5f92432012-05-04 16:31:17 -04003723 contentView.setViewVisibility(R.id.text2, View.GONE);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003724
Jorim Jaggi445d3c02014-08-19 22:33:42 +02003725 applyTopPadding(contentView);
3726
Jorim Jaggid05aa3e2014-08-28 17:52:27 +02003727 mBuilder.shrinkLine3Text(contentView);
3728
Kenny Guy98193ea2014-07-24 19:54:37 +01003729 mBuilder.addProfileBadge(contentView, R.id.profile_badge_large_template);
3730
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003731 return contentView;
3732 }
3733
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04003734 /**
3735 * @hide
3736 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003737 @Override
Christoph Studer4600f9b2014-07-22 22:44:43 +02003738 public void populateBigContentView(Notification wip) {
3739 mBuilder.setBuilderBigContentView(wip, makeBigContentView());
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003740 }
3741 }
Daniel Sandler879c5e02012-04-17 16:46:51 -04003742
3743 /**
3744 * Helper class for generating large-format notifications that include a list of (up to 5) strings.
Joe Malin8d40d042012-11-05 11:36:40 -08003745 *
Robert Ly91c5ce32014-06-08 15:37:00 -07003746 * Here's how you'd set the <code>InboxStyle</code> on a notification:
Daniel Sandler879c5e02012-04-17 16:46:51 -04003747 * <pre class="prettyprint">
Robert Ly91c5ce32014-06-08 15:37:00 -07003748 * Notification notif = new Notification.Builder(mContext)
3749 * .setContentTitle(&quot;5 New mails from &quot; + sender.toString())
3750 * .setContentText(subject)
3751 * .setSmallIcon(R.drawable.new_mail)
3752 * .setLargeIcon(aBitmap)
3753 * .setStyle(new Notification.InboxStyle()
3754 * .addLine(str1)
3755 * .addLine(str2)
3756 * .setContentTitle(&quot;&quot;)
3757 * .setSummaryText(&quot;+3 more&quot;))
3758 * .build();
Daniel Sandler879c5e02012-04-17 16:46:51 -04003759 * </pre>
Joe Malin8d40d042012-11-05 11:36:40 -08003760 *
Daniel Sandler879c5e02012-04-17 16:46:51 -04003761 * @see Notification#bigContentView
3762 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003763 public static class InboxStyle extends Style {
Daniel Sandler879c5e02012-04-17 16:46:51 -04003764 private ArrayList<CharSequence> mTexts = new ArrayList<CharSequence>(5);
3765
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003766 public InboxStyle() {
3767 }
3768
Daniel Sandler879c5e02012-04-17 16:46:51 -04003769 public InboxStyle(Builder builder) {
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003770 setBuilder(builder);
Daniel Sandler879c5e02012-04-17 16:46:51 -04003771 }
3772
Chris Wrend6297db2012-05-03 16:20:13 -04003773 /**
3774 * Overrides ContentTitle in the big form of the template.
3775 * This defaults to the value passed to setContentTitle().
3776 */
3777 public InboxStyle setBigContentTitle(CharSequence title) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04003778 internalSetBigContentTitle(safeCharSequence(title));
Chris Wrend6297db2012-05-03 16:20:13 -04003779 return this;
3780 }
3781
3782 /**
3783 * Set the first line of text after the detail section in the big form of the template.
3784 */
3785 public InboxStyle setSummaryText(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04003786 internalSetSummaryText(safeCharSequence(cs));
Chris Wrend6297db2012-05-03 16:20:13 -04003787 return this;
3788 }
3789
Chris Wren0bd664d2012-08-01 13:56:56 -04003790 /**
3791 * Append a line to the digest section of the Inbox notification.
3792 */
Daniel Sandler879c5e02012-04-17 16:46:51 -04003793 public InboxStyle addLine(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04003794 mTexts.add(safeCharSequence(cs));
Daniel Sandler879c5e02012-04-17 16:46:51 -04003795 return this;
3796 }
3797
Daniel Sandlerf45564e2013-04-15 15:05:08 -04003798 /**
3799 * @hide
3800 */
3801 public void addExtras(Bundle extras) {
3802 super.addExtras(extras);
Christoph Studer4600f9b2014-07-22 22:44:43 +02003803
Daniel Sandlerf45564e2013-04-15 15:05:08 -04003804 CharSequence[] a = new CharSequence[mTexts.size()];
3805 extras.putCharSequenceArray(EXTRA_TEXT_LINES, mTexts.toArray(a));
3806 }
3807
Christoph Studer4600f9b2014-07-22 22:44:43 +02003808 /**
3809 * @hide
3810 */
3811 @Override
3812 protected void restoreFromExtras(Bundle extras) {
3813 super.restoreFromExtras(extras);
3814
3815 mTexts.clear();
3816 if (extras.containsKey(EXTRA_TEXT_LINES)) {
3817 Collections.addAll(mTexts, extras.getCharSequenceArray(EXTRA_TEXT_LINES));
3818 }
3819 }
3820
Daniel Sandler879c5e02012-04-17 16:46:51 -04003821 private RemoteViews makeBigContentView() {
Daniel Sandler619738c2012-06-07 16:33:08 -04003822 // Remove the content text so line3 disappears unless you have a summary
Christoph Studer4600f9b2014-07-22 22:44:43 +02003823
3824 // Nasty
3825 CharSequence oldBuilderContentText = mBuilder.mContentText;
Daniel Sandler619738c2012-06-07 16:33:08 -04003826 mBuilder.mContentText = null;
Christoph Studer4600f9b2014-07-22 22:44:43 +02003827
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01003828 RemoteViews contentView = getStandardView(mBuilder.getInboxLayoutResource());
Daniel Sandler619738c2012-06-07 16:33:08 -04003829
Christoph Studer4600f9b2014-07-22 22:44:43 +02003830 mBuilder.mContentText = oldBuilderContentText;
3831
Chris Wrend6297db2012-05-03 16:20:13 -04003832 contentView.setViewVisibility(R.id.text2, View.GONE);
Daniel Sandler879c5e02012-04-17 16:46:51 -04003833
Chris Wrend6297db2012-05-03 16:20:13 -04003834 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 -04003835 R.id.inbox_text4, R.id.inbox_text5, R.id.inbox_text6};
Chris Wrend6297db2012-05-03 16:20:13 -04003836
Chris Wren4ed80d52012-05-17 09:30:03 -04003837 // Make sure all rows are gone in case we reuse a view.
3838 for (int rowId : rowIds) {
3839 contentView.setViewVisibility(rowId, View.GONE);
3840 }
3841
Jorim Jaggi445d3c02014-08-19 22:33:42 +02003842 final boolean largeText =
3843 mBuilder.mContext.getResources().getConfiguration().fontScale > 1f;
3844 final float subTextSize = mBuilder.mContext.getResources().getDimensionPixelSize(
3845 R.dimen.notification_subtext_size);
Daniel Sandler879c5e02012-04-17 16:46:51 -04003846 int i=0;
3847 while (i < mTexts.size() && i < rowIds.length) {
3848 CharSequence str = mTexts.get(i);
3849 if (str != null && !str.equals("")) {
3850 contentView.setViewVisibility(rowIds[i], View.VISIBLE);
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01003851 contentView.setTextViewText(rowIds[i], mBuilder.processLegacyText(str));
Jorim Jaggi445d3c02014-08-19 22:33:42 +02003852 if (largeText) {
3853 contentView.setTextViewTextSize(rowIds[i], TypedValue.COMPLEX_UNIT_PX,
3854 subTextSize);
3855 }
Daniel Sandler879c5e02012-04-17 16:46:51 -04003856 }
3857 i++;
3858 }
3859
Chris Wren683ab002012-09-20 10:35:54 -04003860 contentView.setViewVisibility(R.id.inbox_end_pad,
3861 mTexts.size() > 0 ? View.VISIBLE : View.GONE);
3862
3863 contentView.setViewVisibility(R.id.inbox_more,
3864 mTexts.size() > rowIds.length ? View.VISIBLE : View.GONE);
Chris Wren29bb6d92012-05-17 18:09:42 -04003865
Jorim Jaggi445d3c02014-08-19 22:33:42 +02003866 applyTopPadding(contentView);
3867
Jorim Jaggid05aa3e2014-08-28 17:52:27 +02003868 mBuilder.shrinkLine3Text(contentView);
3869
Kenny Guy98193ea2014-07-24 19:54:37 +01003870 mBuilder.addProfileBadge(contentView, R.id.profile_badge_large_template);
3871
Daniel Sandler879c5e02012-04-17 16:46:51 -04003872 return contentView;
3873 }
3874
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04003875 /**
3876 * @hide
3877 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003878 @Override
Christoph Studer4600f9b2014-07-22 22:44:43 +02003879 public void populateBigContentView(Notification wip) {
3880 mBuilder.setBuilderBigContentView(wip, makeBigContentView());
Daniel Sandler879c5e02012-04-17 16:46:51 -04003881 }
3882 }
Dan Sandler842dd772014-05-15 09:36:47 -04003883
3884 /**
3885 * Notification style for media playback notifications.
3886 *
3887 * In the expanded form, {@link Notification#bigContentView}, up to 5
3888 * {@link Notification.Action}s specified with
3889 * {@link Notification.Builder#addAction(int, CharSequence, PendingIntent) addAction} will be
3890 * shown as icon-only pushbuttons, suitable for transport controls. The Bitmap given to
3891 * {@link Notification.Builder#setLargeIcon(android.graphics.Bitmap) setLargeIcon()} will be
3892 * treated as album artwork.
3893 *
3894 * Unlike the other styles provided here, MediaStyle can also modify the standard-size
3895 * {@link Notification#contentView}; by providing action indices to
3896 * {@link #setShowActionsInCompactView(int...)} you can promote up to 2 actions to be displayed
3897 * in the standard view alongside the usual content.
3898 *
Bryan Mawhinney6be8de32014-07-18 10:35:12 +01003899 * Notifications created with MediaStyle will have their category set to
3900 * {@link Notification#CATEGORY_TRANSPORT CATEGORY_TRANSPORT} unless you set a different
3901 * category using {@link Notification.Builder#setCategory(String) setCategory()}.
3902 *
Jeff Browndba34ba2014-06-24 20:46:03 -07003903 * Finally, if you attach a {@link android.media.session.MediaSession.Token} using
3904 * {@link android.app.Notification.MediaStyle#setMediaSession(MediaSession.Token)},
Dan Sandler842dd772014-05-15 09:36:47 -04003905 * the System UI can identify this as a notification representing an active media session
3906 * and respond accordingly (by showing album artwork in the lockscreen, for example).
3907 *
3908 * To use this style with your Notification, feed it to
3909 * {@link Notification.Builder#setStyle(android.app.Notification.Style)} like so:
3910 * <pre class="prettyprint">
3911 * Notification noti = new Notification.Builder()
3912 * .setSmallIcon(R.drawable.ic_stat_player)
3913 * .setContentTitle(&quot;Track title&quot;) // these three lines are optional
3914 * .setContentText(&quot;Artist - Album&quot;) // if you use
Bryan Mawhinney6be8de32014-07-18 10:35:12 +01003915 * .setLargeIcon(albumArtBitmap)) // setMediaSession(token)
3916 * .setStyle(<b>new Notification.MediaStyle()</b>
3917 * .setMediaSession(mySession))
Dan Sandler842dd772014-05-15 09:36:47 -04003918 * .build();
3919 * </pre>
3920 *
3921 * @see Notification#bigContentView
3922 */
3923 public static class MediaStyle extends Style {
3924 static final int MAX_MEDIA_BUTTONS_IN_COMPACT = 2;
3925 static final int MAX_MEDIA_BUTTONS = 5;
3926
3927 private int[] mActionsToShowInCompact = null;
Jeff Browndba34ba2014-06-24 20:46:03 -07003928 private MediaSession.Token mToken;
Dan Sandler842dd772014-05-15 09:36:47 -04003929
3930 public MediaStyle() {
3931 }
3932
3933 public MediaStyle(Builder builder) {
3934 setBuilder(builder);
3935 }
3936
3937 /**
3938 * Request up to 2 actions (by index in the order of addition) to be shown in the compact
3939 * notification view.
3940 */
3941 public MediaStyle setShowActionsInCompactView(int...actions) {
3942 mActionsToShowInCompact = actions;
3943 return this;
3944 }
3945
3946 /**
Jeff Browndba34ba2014-06-24 20:46:03 -07003947 * Attach a {@link android.media.session.MediaSession.Token} to this Notification
3948 * to provide additional playback information and control to the SystemUI.
Dan Sandler842dd772014-05-15 09:36:47 -04003949 */
Jeff Browndba34ba2014-06-24 20:46:03 -07003950 public MediaStyle setMediaSession(MediaSession.Token token) {
Dan Sandler842dd772014-05-15 09:36:47 -04003951 mToken = token;
3952 return this;
3953 }
3954
Christoph Studer4600f9b2014-07-22 22:44:43 +02003955 /**
3956 * @hide
3957 */
Dan Sandler842dd772014-05-15 09:36:47 -04003958 @Override
3959 public Notification buildStyled(Notification wip) {
Christoph Studer4600f9b2014-07-22 22:44:43 +02003960 super.buildStyled(wip);
Bryan Mawhinney6be8de32014-07-18 10:35:12 +01003961 if (wip.category == null) {
3962 wip.category = Notification.CATEGORY_TRANSPORT;
3963 }
Dan Sandler842dd772014-05-15 09:36:47 -04003964 return wip;
3965 }
3966
Christoph Studer4600f9b2014-07-22 22:44:43 +02003967 /**
3968 * @hide
3969 */
3970 @Override
3971 public void populateContentView(Notification wip) {
3972 mBuilder.setBuilderContentView(wip, makeMediaContentView());
3973 }
3974
3975 /**
3976 * @hide
3977 */
3978 @Override
3979 public void populateBigContentView(Notification wip) {
3980 mBuilder.setBuilderBigContentView(wip, makeMediaBigContentView());
3981 }
3982
Dan Sandler842dd772014-05-15 09:36:47 -04003983 /** @hide */
3984 @Override
3985 public void addExtras(Bundle extras) {
3986 super.addExtras(extras);
3987
3988 if (mToken != null) {
3989 extras.putParcelable(EXTRA_MEDIA_SESSION, mToken);
3990 }
Bryan Mawhinneye191f902014-07-22 12:50:09 +01003991 if (mActionsToShowInCompact != null) {
3992 extras.putIntArray(EXTRA_COMPACT_ACTIONS, mActionsToShowInCompact);
3993 }
Dan Sandler842dd772014-05-15 09:36:47 -04003994 }
3995
Christoph Studer4600f9b2014-07-22 22:44:43 +02003996 /**
3997 * @hide
3998 */
3999 @Override
4000 protected void restoreFromExtras(Bundle extras) {
4001 super.restoreFromExtras(extras);
4002
4003 if (extras.containsKey(EXTRA_MEDIA_SESSION)) {
4004 mToken = extras.getParcelable(EXTRA_MEDIA_SESSION);
4005 }
4006 if (extras.containsKey(EXTRA_COMPACT_ACTIONS)) {
4007 mActionsToShowInCompact = extras.getIntArray(EXTRA_COMPACT_ACTIONS);
4008 }
4009 }
4010
Dan Sandler842dd772014-05-15 09:36:47 -04004011 private RemoteViews generateMediaActionButton(Action action) {
4012 final boolean tombstone = (action.actionIntent == null);
4013 RemoteViews button = new RemoteViews(mBuilder.mContext.getPackageName(),
Alan Viverette3cb07a462014-06-06 14:19:53 -07004014 R.layout.notification_material_media_action);
Dan Sandler842dd772014-05-15 09:36:47 -04004015 button.setImageViewResource(R.id.action0, action.icon);
4016 if (!tombstone) {
4017 button.setOnClickPendingIntent(R.id.action0, action.actionIntent);
4018 }
4019 button.setContentDescription(R.id.action0, action.title);
4020 return button;
4021 }
4022
4023 private RemoteViews makeMediaContentView() {
4024 RemoteViews view = mBuilder.applyStandardTemplate(
Jorim Jaggi445d3c02014-08-19 22:33:42 +02004025 R.layout.notification_template_material_media);
Dan Sandler842dd772014-05-15 09:36:47 -04004026
4027 final int numActions = mBuilder.mActions.size();
4028 final int N = mActionsToShowInCompact == null
4029 ? 0
4030 : Math.min(mActionsToShowInCompact.length, MAX_MEDIA_BUTTONS_IN_COMPACT);
4031 if (N > 0) {
4032 view.removeAllViews(R.id.actions);
4033 for (int i = 0; i < N; i++) {
4034 if (i >= numActions) {
4035 throw new IllegalArgumentException(String.format(
4036 "setShowActionsInCompactView: action %d out of bounds (max %d)",
4037 i, numActions - 1));
4038 }
4039
4040 final Action action = mBuilder.mActions.get(mActionsToShowInCompact[i]);
4041 final RemoteViews button = generateMediaActionButton(action);
4042 view.addView(R.id.actions, button);
4043 }
4044 }
4045 return view;
4046 }
4047
4048 private RemoteViews makeMediaBigContentView() {
4049 RemoteViews big = mBuilder.applyStandardTemplate(
Jorim Jaggi445d3c02014-08-19 22:33:42 +02004050 R.layout.notification_template_material_big_media);
Dan Sandler842dd772014-05-15 09:36:47 -04004051
4052 final int N = Math.min(mBuilder.mActions.size(), MAX_MEDIA_BUTTONS);
4053 if (N > 0) {
4054 big.removeAllViews(R.id.actions);
4055 for (int i=0; i<N; i++) {
4056 final RemoteViews button = generateMediaActionButton(mBuilder.mActions.get(i));
4057 big.addView(R.id.actions, button);
4058 }
4059 }
4060 return big;
4061 }
4062 }
Griff Hazen61a9e862014-05-22 16:05:19 -07004063
Christoph Studer4600f9b2014-07-22 22:44:43 +02004064 // When adding a new Style subclass here, don't forget to update
4065 // Builder.getNotificationStyleClass.
4066
Griff Hazen61a9e862014-05-22 16:05:19 -07004067 /**
4068 * Extender interface for use with {@link Builder#extend}. Extenders may be used to add
4069 * metadata or change options on a notification builder.
4070 */
4071 public interface Extender {
4072 /**
4073 * Apply this extender to a notification builder.
4074 * @param builder the builder to be modified.
4075 * @return the build object for chaining.
4076 */
4077 public Builder extend(Builder builder);
4078 }
4079
4080 /**
4081 * Helper class to add wearable extensions to notifications.
4082 * <p class="note"> See
4083 * <a href="{@docRoot}wear/notifications/creating.html">Creating Notifications
4084 * for Android Wear</a> for more information on how to use this class.
4085 * <p>
4086 * To create a notification with wearable extensions:
4087 * <ol>
4088 * <li>Create a {@link android.app.Notification.Builder}, setting any desired
4089 * properties.
4090 * <li>Create a {@link android.app.Notification.WearableExtender}.
4091 * <li>Set wearable-specific properties using the
4092 * {@code add} and {@code set} methods of {@link android.app.Notification.WearableExtender}.
4093 * <li>Call {@link android.app.Notification.Builder#extend} to apply the extensions to a
4094 * notification.
4095 * <li>Post the notification to the notification system with the
4096 * {@code NotificationManager.notify(...)} methods.
4097 * </ol>
4098 *
4099 * <pre class="prettyprint">
4100 * Notification notif = new Notification.Builder(mContext)
4101 * .setContentTitle(&quot;New mail from &quot; + sender.toString())
4102 * .setContentText(subject)
4103 * .setSmallIcon(R.drawable.new_mail)
4104 * .extend(new Notification.WearableExtender()
4105 * .setContentIcon(R.drawable.new_mail))
4106 * .build();
4107 * NotificationManager notificationManger =
4108 * (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
4109 * notificationManger.notify(0, notif);</pre>
4110 *
4111 * <p>Wearable extensions can be accessed on an existing notification by using the
4112 * {@code WearableExtender(Notification)} constructor,
4113 * and then using the {@code get} methods to access values.
4114 *
4115 * <pre class="prettyprint">
4116 * Notification.WearableExtender wearableExtender = new Notification.WearableExtender(
4117 * notification);
Griff Hazen14f57992014-05-26 09:07:14 -07004118 * List&lt;Notification&gt; pages = wearableExtender.getPages();</pre>
Griff Hazen61a9e862014-05-22 16:05:19 -07004119 */
4120 public static final class WearableExtender implements Extender {
4121 /**
4122 * Sentinel value for an action index that is unset.
4123 */
4124 public static final int UNSET_ACTION_INDEX = -1;
4125
4126 /**
4127 * Size value for use with {@link #setCustomSizePreset} to show this notification with
4128 * default sizing.
4129 * <p>For custom display notifications created using {@link #setDisplayIntent},
4130 * the default is {@link #SIZE_LARGE}. All other notifications size automatically based
4131 * on their content.
4132 */
4133 public static final int SIZE_DEFAULT = 0;
4134
4135 /**
4136 * Size value for use with {@link #setCustomSizePreset} to show this notification
4137 * with an extra small size.
4138 * <p>This value is only applicable for custom display notifications created using
4139 * {@link #setDisplayIntent}.
4140 */
4141 public static final int SIZE_XSMALL = 1;
4142
4143 /**
4144 * Size value for use with {@link #setCustomSizePreset} to show this notification
4145 * with a small size.
4146 * <p>This value is only applicable for custom display notifications created using
4147 * {@link #setDisplayIntent}.
4148 */
4149 public static final int SIZE_SMALL = 2;
4150
4151 /**
4152 * Size value for use with {@link #setCustomSizePreset} to show this notification
4153 * with a medium size.
4154 * <p>This value is only applicable for custom display notifications created using
4155 * {@link #setDisplayIntent}.
4156 */
4157 public static final int SIZE_MEDIUM = 3;
4158
4159 /**
4160 * Size value for use with {@link #setCustomSizePreset} to show this notification
4161 * with a large size.
4162 * <p>This value is only applicable for custom display notifications created using
4163 * {@link #setDisplayIntent}.
4164 */
4165 public static final int SIZE_LARGE = 4;
4166
Griff Hazend5f11f92014-05-27 15:40:09 -07004167 /**
4168 * Size value for use with {@link #setCustomSizePreset} to show this notification
4169 * full screen.
4170 * <p>This value is only applicable for custom display notifications created using
4171 * {@link #setDisplayIntent}.
4172 */
4173 public static final int SIZE_FULL_SCREEN = 5;
4174
Griff Hazen61a9e862014-05-22 16:05:19 -07004175 /** Notification extra which contains wearable extensions */
4176 private static final String EXTRA_WEARABLE_EXTENSIONS = "android.wearable.EXTENSIONS";
4177
4178 // Keys within EXTRA_WEARABLE_OPTIONS for wearable options.
4179 private static final String KEY_ACTIONS = "actions";
4180 private static final String KEY_FLAGS = "flags";
4181 private static final String KEY_DISPLAY_INTENT = "displayIntent";
4182 private static final String KEY_PAGES = "pages";
4183 private static final String KEY_BACKGROUND = "background";
4184 private static final String KEY_CONTENT_ICON = "contentIcon";
4185 private static final String KEY_CONTENT_ICON_GRAVITY = "contentIconGravity";
4186 private static final String KEY_CONTENT_ACTION_INDEX = "contentActionIndex";
4187 private static final String KEY_CUSTOM_SIZE_PRESET = "customSizePreset";
4188 private static final String KEY_CUSTOM_CONTENT_HEIGHT = "customContentHeight";
4189 private static final String KEY_GRAVITY = "gravity";
4190
4191 // Flags bitwise-ored to mFlags
4192 private static final int FLAG_CONTENT_INTENT_AVAILABLE_OFFLINE = 0x1;
4193 private static final int FLAG_HINT_HIDE_ICON = 1 << 1;
4194 private static final int FLAG_HINT_SHOW_BACKGROUND_ONLY = 1 << 2;
4195 private static final int FLAG_START_SCROLL_BOTTOM = 1 << 3;
4196
4197 // Default value for flags integer
4198 private static final int DEFAULT_FLAGS = FLAG_CONTENT_INTENT_AVAILABLE_OFFLINE;
4199
4200 private static final int DEFAULT_CONTENT_ICON_GRAVITY = Gravity.END;
4201 private static final int DEFAULT_GRAVITY = Gravity.BOTTOM;
4202
4203 private ArrayList<Action> mActions = new ArrayList<Action>();
4204 private int mFlags = DEFAULT_FLAGS;
4205 private PendingIntent mDisplayIntent;
4206 private ArrayList<Notification> mPages = new ArrayList<Notification>();
4207 private Bitmap mBackground;
4208 private int mContentIcon;
4209 private int mContentIconGravity = DEFAULT_CONTENT_ICON_GRAVITY;
4210 private int mContentActionIndex = UNSET_ACTION_INDEX;
4211 private int mCustomSizePreset = SIZE_DEFAULT;
4212 private int mCustomContentHeight;
4213 private int mGravity = DEFAULT_GRAVITY;
4214
4215 /**
4216 * Create a {@link android.app.Notification.WearableExtender} with default
4217 * options.
4218 */
4219 public WearableExtender() {
4220 }
4221
4222 public WearableExtender(Notification notif) {
4223 Bundle wearableBundle = notif.extras.getBundle(EXTRA_WEARABLE_EXTENSIONS);
4224 if (wearableBundle != null) {
4225 List<Action> actions = wearableBundle.getParcelableArrayList(KEY_ACTIONS);
4226 if (actions != null) {
4227 mActions.addAll(actions);
4228 }
4229
4230 mFlags = wearableBundle.getInt(KEY_FLAGS, DEFAULT_FLAGS);
4231 mDisplayIntent = wearableBundle.getParcelable(KEY_DISPLAY_INTENT);
4232
4233 Notification[] pages = getNotificationArrayFromBundle(
4234 wearableBundle, KEY_PAGES);
4235 if (pages != null) {
4236 Collections.addAll(mPages, pages);
4237 }
4238
4239 mBackground = wearableBundle.getParcelable(KEY_BACKGROUND);
4240 mContentIcon = wearableBundle.getInt(KEY_CONTENT_ICON);
4241 mContentIconGravity = wearableBundle.getInt(KEY_CONTENT_ICON_GRAVITY,
4242 DEFAULT_CONTENT_ICON_GRAVITY);
4243 mContentActionIndex = wearableBundle.getInt(KEY_CONTENT_ACTION_INDEX,
4244 UNSET_ACTION_INDEX);
4245 mCustomSizePreset = wearableBundle.getInt(KEY_CUSTOM_SIZE_PRESET,
4246 SIZE_DEFAULT);
4247 mCustomContentHeight = wearableBundle.getInt(KEY_CUSTOM_CONTENT_HEIGHT);
4248 mGravity = wearableBundle.getInt(KEY_GRAVITY, DEFAULT_GRAVITY);
4249 }
4250 }
4251
4252 /**
4253 * Apply wearable extensions to a notification that is being built. This is typically
4254 * called by the {@link android.app.Notification.Builder#extend} method of
4255 * {@link android.app.Notification.Builder}.
4256 */
4257 @Override
4258 public Notification.Builder extend(Notification.Builder builder) {
4259 Bundle wearableBundle = new Bundle();
4260
4261 if (!mActions.isEmpty()) {
4262 wearableBundle.putParcelableArrayList(KEY_ACTIONS, mActions);
4263 }
4264 if (mFlags != DEFAULT_FLAGS) {
4265 wearableBundle.putInt(KEY_FLAGS, mFlags);
4266 }
4267 if (mDisplayIntent != null) {
4268 wearableBundle.putParcelable(KEY_DISPLAY_INTENT, mDisplayIntent);
4269 }
4270 if (!mPages.isEmpty()) {
4271 wearableBundle.putParcelableArray(KEY_PAGES, mPages.toArray(
4272 new Notification[mPages.size()]));
4273 }
4274 if (mBackground != null) {
4275 wearableBundle.putParcelable(KEY_BACKGROUND, mBackground);
4276 }
4277 if (mContentIcon != 0) {
4278 wearableBundle.putInt(KEY_CONTENT_ICON, mContentIcon);
4279 }
4280 if (mContentIconGravity != DEFAULT_CONTENT_ICON_GRAVITY) {
4281 wearableBundle.putInt(KEY_CONTENT_ICON_GRAVITY, mContentIconGravity);
4282 }
4283 if (mContentActionIndex != UNSET_ACTION_INDEX) {
4284 wearableBundle.putInt(KEY_CONTENT_ACTION_INDEX,
4285 mContentActionIndex);
4286 }
4287 if (mCustomSizePreset != SIZE_DEFAULT) {
4288 wearableBundle.putInt(KEY_CUSTOM_SIZE_PRESET, mCustomSizePreset);
4289 }
4290 if (mCustomContentHeight != 0) {
4291 wearableBundle.putInt(KEY_CUSTOM_CONTENT_HEIGHT, mCustomContentHeight);
4292 }
4293 if (mGravity != DEFAULT_GRAVITY) {
4294 wearableBundle.putInt(KEY_GRAVITY, mGravity);
4295 }
4296
4297 builder.getExtras().putBundle(EXTRA_WEARABLE_EXTENSIONS, wearableBundle);
4298 return builder;
4299 }
4300
4301 @Override
4302 public WearableExtender clone() {
4303 WearableExtender that = new WearableExtender();
4304 that.mActions = new ArrayList<Action>(this.mActions);
4305 that.mFlags = this.mFlags;
4306 that.mDisplayIntent = this.mDisplayIntent;
4307 that.mPages = new ArrayList<Notification>(this.mPages);
4308 that.mBackground = this.mBackground;
4309 that.mContentIcon = this.mContentIcon;
4310 that.mContentIconGravity = this.mContentIconGravity;
4311 that.mContentActionIndex = this.mContentActionIndex;
4312 that.mCustomSizePreset = this.mCustomSizePreset;
4313 that.mCustomContentHeight = this.mCustomContentHeight;
4314 that.mGravity = this.mGravity;
4315 return that;
4316 }
4317
4318 /**
4319 * Add a wearable action to this notification.
4320 *
4321 * <p>When wearable actions are added using this method, the set of actions that
4322 * show on a wearable device splits from devices that only show actions added
4323 * using {@link android.app.Notification.Builder#addAction}. This allows for customization
4324 * of which actions display on different devices.
4325 *
4326 * @param action the action to add to this notification
4327 * @return this object for method chaining
4328 * @see android.app.Notification.Action
4329 */
4330 public WearableExtender addAction(Action action) {
4331 mActions.add(action);
4332 return this;
4333 }
4334
4335 /**
4336 * Adds wearable actions to this notification.
4337 *
4338 * <p>When wearable actions are added using this method, the set of actions that
4339 * show on a wearable device splits from devices that only show actions added
4340 * using {@link android.app.Notification.Builder#addAction}. This allows for customization
4341 * of which actions display on different devices.
4342 *
4343 * @param actions the actions to add to this notification
4344 * @return this object for method chaining
4345 * @see android.app.Notification.Action
4346 */
4347 public WearableExtender addActions(List<Action> actions) {
4348 mActions.addAll(actions);
4349 return this;
4350 }
4351
4352 /**
4353 * Clear all wearable actions present on this builder.
4354 * @return this object for method chaining.
4355 * @see #addAction
4356 */
4357 public WearableExtender clearActions() {
4358 mActions.clear();
4359 return this;
4360 }
4361
4362 /**
4363 * Get the wearable actions present on this notification.
4364 */
4365 public List<Action> getActions() {
4366 return mActions;
4367 }
4368
4369 /**
4370 * Set an intent to launch inside of an activity view when displaying
Griff Hazen14f57992014-05-26 09:07:14 -07004371 * this notification. The {@link PendingIntent} provided should be for an activity.
4372 *
4373 * <pre class="prettyprint">
4374 * Intent displayIntent = new Intent(context, MyDisplayActivity.class);
4375 * PendingIntent displayPendingIntent = PendingIntent.getActivity(context,
4376 * 0, displayIntent, PendingIntent.FLAG_UPDATE_CURRENT);
4377 * Notification notif = new Notification.Builder(context)
4378 * .extend(new Notification.WearableExtender()
4379 * .setDisplayIntent(displayPendingIntent)
4380 * .setCustomSizePreset(Notification.WearableExtender.SIZE_MEDIUM))
4381 * .build();</pre>
4382 *
4383 * <p>The activity to launch needs to allow embedding, must be exported, and
Griff Hazen831ca9d2014-06-17 00:38:38 -07004384 * should have an empty task affinity. It is also recommended to use the device
4385 * default light theme.
Griff Hazen14f57992014-05-26 09:07:14 -07004386 *
4387 * <p>Example AndroidManifest.xml entry:
4388 * <pre class="prettyprint">
4389 * &lt;activity android:name=&quot;com.example.MyDisplayActivity&quot;
4390 * android:exported=&quot;true&quot;
4391 * android:allowEmbedded=&quot;true&quot;
Griff Hazen831ca9d2014-06-17 00:38:38 -07004392 * android:taskAffinity=&quot;&quot;
4393 * android:theme=&quot;@android:style/Theme.DeviceDefault.Light&quot; /&gt;</pre>
Griff Hazen61a9e862014-05-22 16:05:19 -07004394 *
4395 * @param intent the {@link PendingIntent} for an activity
4396 * @return this object for method chaining
4397 * @see android.app.Notification.WearableExtender#getDisplayIntent
4398 */
4399 public WearableExtender setDisplayIntent(PendingIntent intent) {
4400 mDisplayIntent = intent;
4401 return this;
4402 }
4403
4404 /**
4405 * Get the intent to launch inside of an activity view when displaying this
4406 * notification. This {@code PendingIntent} should be for an activity.
4407 */
4408 public PendingIntent getDisplayIntent() {
4409 return mDisplayIntent;
4410 }
4411
4412 /**
4413 * Add an additional page of content to display with this notification. The current
4414 * notification forms the first page, and pages added using this function form
4415 * subsequent pages. This field can be used to separate a notification into multiple
4416 * sections.
4417 *
4418 * @param page the notification to add as another page
4419 * @return this object for method chaining
4420 * @see android.app.Notification.WearableExtender#getPages
4421 */
4422 public WearableExtender addPage(Notification page) {
4423 mPages.add(page);
4424 return this;
4425 }
4426
4427 /**
4428 * Add additional pages of content to display with this notification. The current
4429 * notification forms the first page, and pages added using this function form
4430 * subsequent pages. This field can be used to separate a notification into multiple
4431 * sections.
4432 *
4433 * @param pages a list of notifications
4434 * @return this object for method chaining
4435 * @see android.app.Notification.WearableExtender#getPages
4436 */
4437 public WearableExtender addPages(List<Notification> pages) {
4438 mPages.addAll(pages);
4439 return this;
4440 }
4441
4442 /**
4443 * Clear all additional pages present on this builder.
4444 * @return this object for method chaining.
4445 * @see #addPage
4446 */
4447 public WearableExtender clearPages() {
4448 mPages.clear();
4449 return this;
4450 }
4451
4452 /**
4453 * Get the array of additional pages of content for displaying this notification. The
4454 * current notification forms the first page, and elements within this array form
4455 * subsequent pages. This field can be used to separate a notification into multiple
4456 * sections.
4457 * @return the pages for this notification
4458 */
4459 public List<Notification> getPages() {
4460 return mPages;
4461 }
4462
4463 /**
4464 * Set a background image to be displayed behind the notification content.
4465 * Contrary to the {@link android.app.Notification.BigPictureStyle}, this background
4466 * will work with any notification style.
4467 *
4468 * @param background the background bitmap
4469 * @return this object for method chaining
4470 * @see android.app.Notification.WearableExtender#getBackground
4471 */
4472 public WearableExtender setBackground(Bitmap background) {
4473 mBackground = background;
4474 return this;
4475 }
4476
4477 /**
4478 * Get a background image to be displayed behind the notification content.
4479 * Contrary to the {@link android.app.Notification.BigPictureStyle}, this background
4480 * will work with any notification style.
4481 *
4482 * @return the background image
4483 * @see android.app.Notification.WearableExtender#setBackground
4484 */
4485 public Bitmap getBackground() {
4486 return mBackground;
4487 }
4488
4489 /**
4490 * Set an icon that goes with the content of this notification.
4491 */
4492 public WearableExtender setContentIcon(int icon) {
4493 mContentIcon = icon;
4494 return this;
4495 }
4496
4497 /**
4498 * Get an icon that goes with the content of this notification.
4499 */
4500 public int getContentIcon() {
4501 return mContentIcon;
4502 }
4503
4504 /**
4505 * Set the gravity that the content icon should have within the notification display.
4506 * Supported values include {@link android.view.Gravity#START} and
4507 * {@link android.view.Gravity#END}. The default value is {@link android.view.Gravity#END}.
4508 * @see #setContentIcon
4509 */
4510 public WearableExtender setContentIconGravity(int contentIconGravity) {
4511 mContentIconGravity = contentIconGravity;
4512 return this;
4513 }
4514
4515 /**
4516 * Get the gravity that the content icon should have within the notification display.
4517 * Supported values include {@link android.view.Gravity#START} and
4518 * {@link android.view.Gravity#END}. The default value is {@link android.view.Gravity#END}.
4519 * @see #getContentIcon
4520 */
4521 public int getContentIconGravity() {
4522 return mContentIconGravity;
4523 }
4524
4525 /**
4526 * Set an action from this notification's actions to be clickable with the content of
Griff Hazen14f57992014-05-26 09:07:14 -07004527 * this notification. This action will no longer display separately from the
4528 * notification's content.
4529 *
Griff Hazenca48d352014-05-28 22:37:13 -07004530 * <p>For notifications with multiple pages, child pages can also have content actions
Griff Hazen14f57992014-05-26 09:07:14 -07004531 * set, although the list of available actions comes from the main notification and not
4532 * from the child page's notification.
4533 *
4534 * @param actionIndex The index of the action to hoist onto the current notification page.
4535 * If wearable actions were added to the main notification, this index
4536 * will apply to that list, otherwise it will apply to the regular
4537 * actions list.
Griff Hazen61a9e862014-05-22 16:05:19 -07004538 */
4539 public WearableExtender setContentAction(int actionIndex) {
4540 mContentActionIndex = actionIndex;
4541 return this;
4542 }
4543
4544 /**
Griff Hazenca48d352014-05-28 22:37:13 -07004545 * Get the index of the notification action, if any, that was specified as being clickable
4546 * with the content of this notification. This action will no longer display separately
Griff Hazen14f57992014-05-26 09:07:14 -07004547 * from the notification's content.
Griff Hazen61a9e862014-05-22 16:05:19 -07004548 *
Griff Hazenca48d352014-05-28 22:37:13 -07004549 * <p>For notifications with multiple pages, child pages can also have content actions
Griff Hazen14f57992014-05-26 09:07:14 -07004550 * set, although the list of available actions comes from the main notification and not
4551 * from the child page's notification.
4552 *
4553 * <p>If wearable specific actions were added to the main notification, this index will
4554 * apply to that list, otherwise it will apply to the regular actions list.
Griff Hazenca48d352014-05-28 22:37:13 -07004555 *
4556 * @return the action index or {@link #UNSET_ACTION_INDEX} if no action was selected.
Griff Hazen61a9e862014-05-22 16:05:19 -07004557 */
4558 public int getContentAction() {
4559 return mContentActionIndex;
4560 }
4561
4562 /**
4563 * Set the gravity that this notification should have within the available viewport space.
4564 * Supported values include {@link android.view.Gravity#TOP},
4565 * {@link android.view.Gravity#CENTER_VERTICAL} and {@link android.view.Gravity#BOTTOM}.
4566 * The default value is {@link android.view.Gravity#BOTTOM}.
4567 */
4568 public WearableExtender setGravity(int gravity) {
4569 mGravity = gravity;
4570 return this;
4571 }
4572
4573 /**
4574 * Get the gravity that this notification should have within the available viewport space.
4575 * Supported values include {@link android.view.Gravity#TOP},
4576 * {@link android.view.Gravity#CENTER_VERTICAL} and {@link android.view.Gravity#BOTTOM}.
4577 * The default value is {@link android.view.Gravity#BOTTOM}.
4578 */
4579 public int getGravity() {
4580 return mGravity;
4581 }
4582
4583 /**
4584 * Set the custom size preset for the display of this notification out of the available
4585 * presets found in {@link android.app.Notification.WearableExtender}, e.g.
4586 * {@link #SIZE_LARGE}.
4587 * <p>Some custom size presets are only applicable for custom display notifications created
4588 * using {@link android.app.Notification.WearableExtender#setDisplayIntent}. Check the
4589 * documentation for the preset in question. See also
4590 * {@link #setCustomContentHeight} and {@link #getCustomSizePreset}.
4591 */
4592 public WearableExtender setCustomSizePreset(int sizePreset) {
4593 mCustomSizePreset = sizePreset;
4594 return this;
4595 }
4596
4597 /**
4598 * Get the custom size preset for the display of this notification out of the available
4599 * presets found in {@link android.app.Notification.WearableExtender}, e.g.
4600 * {@link #SIZE_LARGE}.
4601 * <p>Some custom size presets are only applicable for custom display notifications created
4602 * using {@link #setDisplayIntent}. Check the documentation for the preset in question.
4603 * See also {@link #setCustomContentHeight} and {@link #setCustomSizePreset}.
4604 */
4605 public int getCustomSizePreset() {
4606 return mCustomSizePreset;
4607 }
4608
4609 /**
4610 * Set the custom height in pixels for the display of this notification's content.
4611 * <p>This option is only available for custom display notifications created
4612 * using {@link android.app.Notification.WearableExtender#setDisplayIntent}. See also
4613 * {@link android.app.Notification.WearableExtender#setCustomSizePreset} and
4614 * {@link #getCustomContentHeight}.
4615 */
4616 public WearableExtender setCustomContentHeight(int height) {
4617 mCustomContentHeight = height;
4618 return this;
4619 }
4620
4621 /**
4622 * Get the custom height in pixels for the display of this notification's content.
4623 * <p>This option is only available for custom display notifications created
4624 * using {@link #setDisplayIntent}. See also {@link #setCustomSizePreset} and
4625 * {@link #setCustomContentHeight}.
4626 */
4627 public int getCustomContentHeight() {
4628 return mCustomContentHeight;
4629 }
4630
4631 /**
4632 * Set whether the scrolling position for the contents of this notification should start
4633 * at the bottom of the contents instead of the top when the contents are too long to
4634 * display within the screen. Default is false (start scroll at the top).
4635 */
4636 public WearableExtender setStartScrollBottom(boolean startScrollBottom) {
4637 setFlag(FLAG_START_SCROLL_BOTTOM, startScrollBottom);
4638 return this;
4639 }
4640
4641 /**
4642 * Get whether the scrolling position for the contents of this notification should start
4643 * at the bottom of the contents instead of the top when the contents are too long to
4644 * display within the screen. Default is false (start scroll at the top).
4645 */
4646 public boolean getStartScrollBottom() {
4647 return (mFlags & FLAG_START_SCROLL_BOTTOM) != 0;
4648 }
4649
4650 /**
4651 * Set whether the content intent is available when the wearable device is not connected
4652 * to a companion device. The user can still trigger this intent when the wearable device
4653 * is offline, but a visual hint will indicate that the content intent may not be available.
4654 * Defaults to true.
4655 */
4656 public WearableExtender setContentIntentAvailableOffline(
4657 boolean contentIntentAvailableOffline) {
4658 setFlag(FLAG_CONTENT_INTENT_AVAILABLE_OFFLINE, contentIntentAvailableOffline);
4659 return this;
4660 }
4661
4662 /**
4663 * Get whether the content intent is available when the wearable device is not connected
4664 * to a companion device. The user can still trigger this intent when the wearable device
4665 * is offline, but a visual hint will indicate that the content intent may not be available.
4666 * Defaults to true.
4667 */
4668 public boolean getContentIntentAvailableOffline() {
4669 return (mFlags & FLAG_CONTENT_INTENT_AVAILABLE_OFFLINE) != 0;
4670 }
4671
4672 /**
4673 * Set a hint that this notification's icon should not be displayed.
4674 * @param hintHideIcon {@code true} to hide the icon, {@code false} otherwise.
4675 * @return this object for method chaining
4676 */
4677 public WearableExtender setHintHideIcon(boolean hintHideIcon) {
4678 setFlag(FLAG_HINT_HIDE_ICON, hintHideIcon);
4679 return this;
4680 }
4681
4682 /**
4683 * Get a hint that this notification's icon should not be displayed.
4684 * @return {@code true} if this icon should not be displayed, false otherwise.
4685 * The default value is {@code false} if this was never set.
4686 */
4687 public boolean getHintHideIcon() {
4688 return (mFlags & FLAG_HINT_HIDE_ICON) != 0;
4689 }
4690
4691 /**
4692 * Set a visual hint that only the background image of this notification should be
4693 * displayed, and other semantic content should be hidden. This hint is only applicable
4694 * to sub-pages added using {@link #addPage}.
4695 */
4696 public WearableExtender setHintShowBackgroundOnly(boolean hintShowBackgroundOnly) {
4697 setFlag(FLAG_HINT_SHOW_BACKGROUND_ONLY, hintShowBackgroundOnly);
4698 return this;
4699 }
4700
4701 /**
4702 * Get a visual hint that only the background image of this notification should be
4703 * displayed, and other semantic content should be hidden. This hint is only applicable
4704 * to sub-pages added using {@link android.app.Notification.WearableExtender#addPage}.
4705 */
4706 public boolean getHintShowBackgroundOnly() {
4707 return (mFlags & FLAG_HINT_SHOW_BACKGROUND_ONLY) != 0;
4708 }
4709
4710 private void setFlag(int mask, boolean value) {
4711 if (value) {
4712 mFlags |= mask;
4713 } else {
4714 mFlags &= ~mask;
4715 }
4716 }
4717 }
4718
4719 /**
4720 * Get an array of Notification objects from a parcelable array bundle field.
4721 * Update the bundle to have a typed array so fetches in the future don't need
4722 * to do an array copy.
4723 */
4724 private static Notification[] getNotificationArrayFromBundle(Bundle bundle, String key) {
4725 Parcelable[] array = bundle.getParcelableArray(key);
4726 if (array instanceof Notification[] || array == null) {
4727 return (Notification[]) array;
4728 }
4729 Notification[] typedArray = Arrays.copyOf(array, array.length,
4730 Notification[].class);
4731 bundle.putParcelableArray(key, typedArray);
4732 return typedArray;
4733 }
Christoph Studer4600f9b2014-07-22 22:44:43 +02004734
4735 private static class BuilderRemoteViews extends RemoteViews {
4736 public BuilderRemoteViews(Parcel parcel) {
4737 super(parcel);
4738 }
4739
Svet Ganov0da85b62014-08-06 14:11:37 -07004740 public BuilderRemoteViews(String packageName, int userId, int layoutId) {
4741 super(packageName, userId, layoutId);
Christoph Studer4600f9b2014-07-22 22:44:43 +02004742 }
4743
4744 @Override
4745 public BuilderRemoteViews clone() {
4746 Parcel p = Parcel.obtain();
4747 writeToParcel(p, 0);
4748 p.setDataPosition(0);
4749 BuilderRemoteViews brv = new BuilderRemoteViews(p);
4750 p.recycle();
4751 return brv;
4752 }
4753 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004754}