blob: e1dc8bf7e499b5e153258681bb208712622c3cf4 [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);
1427 }
Joe Onorato18e69df2010-05-17 22:26:12 -07001428 }
1429
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04001430 /**
1431 * Make sure this CharSequence is safe to put into a bundle, which basically
1432 * means it had better not be some custom Parcelable implementation.
1433 * @hide
1434 */
1435 public static CharSequence safeCharSequence(CharSequence cs) {
1436 if (cs instanceof Parcelable) {
1437 Log.e(TAG, "warning: " + cs.getClass().getCanonicalName()
1438 + " instance is a custom Parcelable and not allowed in Notification");
1439 return cs.toString();
1440 }
1441
1442 return cs;
1443 }
1444
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001445 public int describeContents() {
1446 return 0;
1447 }
1448
1449 /**
1450 * Flatten this notification from a parcel.
1451 */
1452 public void writeToParcel(Parcel parcel, int flags)
1453 {
1454 parcel.writeInt(1);
1455
1456 parcel.writeLong(when);
1457 parcel.writeInt(icon);
1458 parcel.writeInt(number);
1459 if (contentIntent != null) {
1460 parcel.writeInt(1);
1461 contentIntent.writeToParcel(parcel, 0);
1462 } else {
1463 parcel.writeInt(0);
1464 }
1465 if (deleteIntent != null) {
1466 parcel.writeInt(1);
1467 deleteIntent.writeToParcel(parcel, 0);
1468 } else {
1469 parcel.writeInt(0);
1470 }
1471 if (tickerText != null) {
1472 parcel.writeInt(1);
1473 TextUtils.writeToParcel(tickerText, parcel, flags);
1474 } else {
1475 parcel.writeInt(0);
1476 }
Joe Onorato46439ce2010-11-19 13:56:21 -08001477 if (tickerView != null) {
Joe Onoratoef1e7762010-09-17 18:38:38 -04001478 parcel.writeInt(1);
Joe Onorato46439ce2010-11-19 13:56:21 -08001479 tickerView.writeToParcel(parcel, 0);
Joe Onoratoef1e7762010-09-17 18:38:38 -04001480 } else {
1481 parcel.writeInt(0);
1482 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001483 if (contentView != null) {
1484 parcel.writeInt(1);
1485 contentView.writeToParcel(parcel, 0);
1486 } else {
1487 parcel.writeInt(0);
1488 }
Joe Onorato561d3852010-11-20 18:09:34 -08001489 if (largeIcon != null) {
1490 parcel.writeInt(1);
1491 largeIcon.writeToParcel(parcel, 0);
1492 } else {
1493 parcel.writeInt(0);
1494 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001495
1496 parcel.writeInt(defaults);
1497 parcel.writeInt(this.flags);
1498
1499 if (sound != null) {
1500 parcel.writeInt(1);
1501 sound.writeToParcel(parcel, 0);
1502 } else {
1503 parcel.writeInt(0);
1504 }
1505 parcel.writeInt(audioStreamType);
John Spurlockc0650f022014-07-19 13:22:39 -04001506
1507 if (audioAttributes != null) {
1508 parcel.writeInt(1);
1509 audioAttributes.writeToParcel(parcel, 0);
1510 } else {
1511 parcel.writeInt(0);
1512 }
1513
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001514 parcel.writeLongArray(vibrate);
1515 parcel.writeInt(ledARGB);
1516 parcel.writeInt(ledOnMS);
1517 parcel.writeInt(ledOffMS);
1518 parcel.writeInt(iconLevel);
Daniel Sandlere46cbd32010-06-17 10:35:26 -04001519
1520 if (fullScreenIntent != null) {
1521 parcel.writeInt(1);
1522 fullScreenIntent.writeToParcel(parcel, 0);
1523 } else {
1524 parcel.writeInt(0);
1525 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001526
1527 parcel.writeInt(priority);
Joe Malin8d40d042012-11-05 11:36:40 -08001528
John Spurlockfd7f1e02014-03-18 16:41:57 -04001529 parcel.writeString(category);
Joe Malin8d40d042012-11-05 11:36:40 -08001530
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001531 parcel.writeString(mGroupKey);
1532
1533 parcel.writeString(mSortKey);
1534
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001535 parcel.writeBundle(extras); // null ok
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001536
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001537 parcel.writeTypedArray(actions, 0); // null ok
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001538
Daniel Sandlerf3b73432012-03-27 15:01:25 -04001539 if (bigContentView != null) {
1540 parcel.writeInt(1);
1541 bigContentView.writeToParcel(parcel, 0);
1542 } else {
1543 parcel.writeInt(0);
1544 }
Dan Sandler0bf2ed82013-12-21 23:33:41 -06001545
Chris Wren8fd39ec2014-02-27 17:43:26 -05001546 if (headsUpContentView != null) {
1547 parcel.writeInt(1);
1548 headsUpContentView.writeToParcel(parcel, 0);
1549 } else {
1550 parcel.writeInt(0);
1551 }
1552
Dan Sandler0bf2ed82013-12-21 23:33:41 -06001553 parcel.writeInt(visibility);
1554
1555 if (publicVersion != null) {
1556 parcel.writeInt(1);
1557 publicVersion.writeToParcel(parcel, 0);
1558 } else {
1559 parcel.writeInt(0);
1560 }
Dan Sandler26e81cf2014-05-06 10:01:27 -04001561
1562 parcel.writeInt(color);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001563 }
1564
1565 /**
1566 * Parcelable.Creator that instantiates Notification objects
1567 */
1568 public static final Parcelable.Creator<Notification> CREATOR
1569 = new Parcelable.Creator<Notification>()
1570 {
1571 public Notification createFromParcel(Parcel parcel)
1572 {
1573 return new Notification(parcel);
1574 }
1575
1576 public Notification[] newArray(int size)
1577 {
1578 return new Notification[size];
1579 }
1580 };
1581
1582 /**
1583 * Sets the {@link #contentView} field to be a view with the standard "Latest Event"
1584 * layout.
1585 *
1586 * <p>Uses the {@link #icon} and {@link #when} fields to set the icon and time fields
1587 * in the view.</p>
1588 * @param context The context for your application / activity.
1589 * @param contentTitle The title that goes in the expanded entry.
1590 * @param contentText The text that goes in the expanded entry.
1591 * @param contentIntent The intent to launch when the user clicks the expanded notification.
1592 * If this is an activity, it must include the
1593 * {@link android.content.Intent#FLAG_ACTIVITY_NEW_TASK} flag, which requires
Scott Main7aee61f2011-02-08 11:25:01 -08001594 * that you take care of task management as described in the
1595 * <a href="{@docRoot}guide/topics/fundamentals/tasks-and-back-stack.html">Tasks and Back
1596 * Stack</a> document.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001597 *
Joe Onorato46439ce2010-11-19 13:56:21 -08001598 * @deprecated Use {@link Builder} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001599 */
Joe Onorato46439ce2010-11-19 13:56:21 -08001600 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001601 public void setLatestEventInfo(Context context,
1602 CharSequence contentTitle, CharSequence contentText, PendingIntent contentIntent) {
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001603 Notification.Builder builder = new Notification.Builder(context);
1604
1605 // First, ensure that key pieces of information that may have been set directly
1606 // are preserved
1607 builder.setWhen(this.when);
1608 builder.setSmallIcon(this.icon);
1609 builder.setPriority(this.priority);
1610 builder.setTicker(this.tickerText);
1611 builder.setNumber(this.number);
Selim Cinek255dd042014-08-19 22:29:02 +02001612 builder.setColor(this.color);
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001613 builder.mFlags = this.flags;
1614 builder.setSound(this.sound, this.audioStreamType);
1615 builder.setDefaults(this.defaults);
1616 builder.setVibrate(this.vibrate);
1617
1618 // now apply the latestEventInfo fields
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001619 if (contentTitle != null) {
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001620 builder.setContentTitle(contentTitle);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001621 }
1622 if (contentText != null) {
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001623 builder.setContentText(contentText);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001624 }
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001625 builder.setContentIntent(contentIntent);
1626 builder.buildInto(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001627 }
1628
1629 @Override
1630 public String toString() {
1631 StringBuilder sb = new StringBuilder();
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001632 sb.append("Notification(pri=");
1633 sb.append(priority);
1634 sb.append(" contentView=");
Joe Onoratoc9596d62011-01-12 17:03:11 -08001635 if (contentView != null) {
1636 sb.append(contentView.getPackage());
1637 sb.append("/0x");
1638 sb.append(Integer.toHexString(contentView.getLayoutId()));
1639 } else {
1640 sb.append("null");
1641 }
1642 sb.append(" vibrate=");
Daniel Sandler6738eee2012-11-16 12:03:32 -05001643 if ((this.defaults & DEFAULT_VIBRATE) != 0) {
1644 sb.append("default");
1645 } else if (this.vibrate != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001646 int N = this.vibrate.length-1;
1647 sb.append("[");
1648 for (int i=0; i<N; i++) {
1649 sb.append(this.vibrate[i]);
1650 sb.append(',');
1651 }
Simon Schoar8cf97d92009-06-10 22:08:37 +02001652 if (N != -1) {
1653 sb.append(this.vibrate[N]);
1654 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001655 sb.append("]");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001656 } else {
1657 sb.append("null");
1658 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001659 sb.append(" sound=");
Daniel Sandler6738eee2012-11-16 12:03:32 -05001660 if ((this.defaults & DEFAULT_SOUND) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001661 sb.append("default");
Daniel Sandler6738eee2012-11-16 12:03:32 -05001662 } else if (this.sound != null) {
1663 sb.append(this.sound.toString());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001664 } else {
1665 sb.append("null");
1666 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001667 sb.append(" defaults=0x");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001668 sb.append(Integer.toHexString(this.defaults));
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001669 sb.append(" flags=0x");
Daniel Sandlere46cbd32010-06-17 10:35:26 -04001670 sb.append(Integer.toHexString(this.flags));
Dan Sandler26e81cf2014-05-06 10:01:27 -04001671 sb.append(String.format(" color=0x%08x", this.color));
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001672 if (this.category != null) {
1673 sb.append(" category=");
1674 sb.append(this.category);
1675 }
1676 if (this.mGroupKey != null) {
1677 sb.append(" groupKey=");
1678 sb.append(this.mGroupKey);
1679 }
1680 if (this.mSortKey != null) {
1681 sb.append(" sortKey=");
1682 sb.append(this.mSortKey);
1683 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001684 if (actions != null) {
Dan Sandler1b718782014-07-18 12:43:45 -04001685 sb.append(" actions=");
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001686 sb.append(actions.length);
Dan Sandler1b718782014-07-18 12:43:45 -04001687 }
1688 sb.append(" vis=");
1689 sb.append(visibilityToString(this.visibility));
1690 if (this.publicVersion != null) {
1691 sb.append(" publicVersion=");
1692 sb.append(publicVersion.toString());
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001693 }
1694 sb.append(")");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001695 return sb.toString();
1696 }
Joe Onorato46439ce2010-11-19 13:56:21 -08001697
Dan Sandler1b718782014-07-18 12:43:45 -04001698 /**
1699 * {@hide}
1700 */
1701 public static String visibilityToString(int vis) {
1702 switch (vis) {
1703 case VISIBILITY_PRIVATE:
1704 return "PRIVATE";
1705 case VISIBILITY_PUBLIC:
1706 return "PUBLIC";
1707 case VISIBILITY_SECRET:
1708 return "SECRET";
1709 default:
1710 return "UNKNOWN(" + String.valueOf(vis) + ")";
1711 }
1712 }
1713
Joe Onoratocb109a02011-01-18 17:57:41 -08001714 /**
Christoph Studer4600f9b2014-07-22 22:44:43 +02001715 * @hide
1716 */
1717 public boolean isValid() {
1718 // Would like to check for icon!=0 here, too, but NotificationManagerService accepts that
1719 // for legacy reasons.
1720 return contentView != null || extras.getBoolean(Builder.EXTRA_REBUILD_CONTENT_VIEW);
1721 }
1722
1723 /**
Christoph Studerc8db24b2014-07-25 17:50:30 +02001724 * @hide
1725 */
1726 public boolean isGroupSummary() {
1727 return mGroupKey != null && (flags & FLAG_GROUP_SUMMARY) != 0;
1728 }
1729
1730 /**
1731 * @hide
1732 */
1733 public boolean isGroupChild() {
1734 return mGroupKey != null && (flags & FLAG_GROUP_SUMMARY) == 0;
1735 }
1736
1737 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001738 * Builder class for {@link Notification} objects.
Joe Malin8d40d042012-11-05 11:36:40 -08001739 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001740 * Provides a convenient way to set the various fields of a {@link Notification} and generate
Scott Main183bf112012-08-13 19:12:13 -07001741 * content views using the platform's notification layout template. If your app supports
1742 * versions of Android as old as API level 4, you can instead use
1743 * {@link android.support.v4.app.NotificationCompat.Builder NotificationCompat.Builder},
1744 * available in the <a href="{@docRoot}tools/extras/support-library.html">Android Support
1745 * library</a>.
Joe Malin8d40d042012-11-05 11:36:40 -08001746 *
Scott Main183bf112012-08-13 19:12:13 -07001747 * <p>Example:
Joe Malin8d40d042012-11-05 11:36:40 -08001748 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001749 * <pre class="prettyprint">
Scott Main183bf112012-08-13 19:12:13 -07001750 * Notification noti = new Notification.Builder(mContext)
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001751 * .setContentTitle(&quot;New mail from &quot; + sender.toString())
1752 * .setContentText(subject)
1753 * .setSmallIcon(R.drawable.new_mail)
1754 * .setLargeIcon(aBitmap)
Chris Wrenfbd96ba2012-05-01 12:03:58 -04001755 * .build();
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001756 * </pre>
Joe Onoratocb109a02011-01-18 17:57:41 -08001757 */
Joe Onorato46439ce2010-11-19 13:56:21 -08001758 public static class Builder {
Daniel Sandler602ad1c2012-06-12 16:06:27 -04001759 private static final int MAX_ACTION_BUTTONS = 3;
Jorim Jaggi445d3c02014-08-19 22:33:42 +02001760 private static final float LARGE_TEXT_SCALE = 1.3f;
Daniel Sandler8680bf82012-05-15 16:52:52 -04001761
Christoph Studer4600f9b2014-07-22 22:44:43 +02001762 /**
1763 * @hide
1764 */
1765 public static final String EXTRA_NEEDS_REBUILD = "android.rebuild";
1766
1767 /**
1768 * @hide
1769 */
1770 public static final String EXTRA_REBUILD_LARGE_ICON = "android.rebuild.largeIcon";
1771 /**
1772 * @hide
1773 */
1774 public static final String EXTRA_REBUILD_CONTENT_VIEW = "android.rebuild.contentView";
1775 /**
1776 * @hide
1777 */
1778 public static final String EXTRA_REBUILD_CONTENT_VIEW_ACTION_COUNT =
1779 "android.rebuild.contentViewActionCount";
1780 /**
1781 * @hide
1782 */
1783 public static final String EXTRA_REBUILD_BIG_CONTENT_VIEW
1784 = "android.rebuild.bigView";
1785 /**
1786 * @hide
1787 */
1788 public static final String EXTRA_REBUILD_BIG_CONTENT_VIEW_ACTION_COUNT
1789 = "android.rebuild.bigViewActionCount";
1790 /**
1791 * @hide
1792 */
1793 public static final String EXTRA_REBUILD_HEADS_UP_CONTENT_VIEW
1794 = "android.rebuild.hudView";
1795 /**
1796 * @hide
1797 */
1798 public static final String EXTRA_REBUILD_HEADS_UP_CONTENT_VIEW_ACTION_COUNT
1799 = "android.rebuild.hudViewActionCount";
1800
1801 /**
1802 * The package name of the context used to create the notification via a Builder.
1803 */
1804 private static final String EXTRA_REBUILD_CONTEXT_PACKAGE =
1805 "android.rebuild.contextPackage";
1806
1807 // Whether to enable stripping (at post time) & rebuilding (at listener receive time) of
1808 // memory intensive resources.
1809 private static final boolean STRIP_AND_REBUILD = true;
1810
Joe Onorato46439ce2010-11-19 13:56:21 -08001811 private Context mContext;
1812
1813 private long mWhen;
1814 private int mSmallIcon;
1815 private int mSmallIconLevel;
Joe Onorato8595a3d2010-11-19 18:12:07 -08001816 private int mNumber;
Joe Onorato46439ce2010-11-19 13:56:21 -08001817 private CharSequence mContentTitle;
1818 private CharSequence mContentText;
1819 private CharSequence mContentInfo;
Daniel Sandlerf3b73432012-03-27 15:01:25 -04001820 private CharSequence mSubText;
Joe Onorato46439ce2010-11-19 13:56:21 -08001821 private PendingIntent mContentIntent;
1822 private RemoteViews mContentView;
1823 private PendingIntent mDeleteIntent;
1824 private PendingIntent mFullScreenIntent;
1825 private CharSequence mTickerText;
1826 private RemoteViews mTickerView;
1827 private Bitmap mLargeIcon;
1828 private Uri mSound;
1829 private int mAudioStreamType;
John Spurlockc0650f022014-07-19 13:22:39 -04001830 private AudioAttributes mAudioAttributes;
Joe Onorato46439ce2010-11-19 13:56:21 -08001831 private long[] mVibrate;
1832 private int mLedArgb;
1833 private int mLedOnMs;
1834 private int mLedOffMs;
1835 private int mDefaults;
1836 private int mFlags;
Jeff Sharkey1c400132011-08-05 14:50:13 -07001837 private int mProgressMax;
1838 private int mProgress;
1839 private boolean mProgressIndeterminate;
John Spurlockfd7f1e02014-03-18 16:41:57 -04001840 private String mCategory;
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001841 private String mGroupKey;
1842 private String mSortKey;
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001843 private Bundle mExtras;
1844 private int mPriority;
Daniel Sandler8680bf82012-05-15 16:52:52 -04001845 private ArrayList<Action> mActions = new ArrayList<Action>(MAX_ACTION_BUTTONS);
Daniel Sandlera2985ed2012-04-03 16:42:00 -04001846 private boolean mUseChronometer;
Chris Wrenfbd96ba2012-05-01 12:03:58 -04001847 private Style mStyle;
Daniel Sandler0c890492012-09-12 17:23:10 -07001848 private boolean mShowWhen = true;
Dan Sandler0bf2ed82013-12-21 23:33:41 -06001849 private int mVisibility = VISIBILITY_PRIVATE;
1850 private Notification mPublicVersion = null;
Dan Sandler26e81cf2014-05-06 10:01:27 -04001851 private final NotificationColorUtil mColorUtil;
Chris Wrendde75302014-03-26 17:24:15 -04001852 private ArrayList<String> mPeople;
Dan Sandler26e81cf2014-05-06 10:01:27 -04001853 private int mColor = COLOR_DEFAULT;
Christoph Studer7ac80e62014-08-04 16:01:57 +02001854
1855 /**
1856 * The user that built the notification originally.
1857 */
1858 private int mOriginatingUserId;
Christoph Studer4600f9b2014-07-22 22:44:43 +02001859
1860 /**
1861 * Contains extras related to rebuilding during the build phase.
1862 */
1863 private Bundle mRebuildBundle = new Bundle();
1864 /**
1865 * Contains the notification to rebuild when this Builder is in "rebuild" mode.
1866 * Null otherwise.
1867 */
1868 private Notification mRebuildNotification = null;
1869
Joe Onoratocb109a02011-01-18 17:57:41 -08001870 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001871 * Constructs a new Builder with the defaults:
Joe Onoratocb109a02011-01-18 17:57:41 -08001872 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001873
1874 * <table>
1875 * <tr><th align=right>priority</th>
1876 * <td>{@link #PRIORITY_DEFAULT}</td></tr>
1877 * <tr><th align=right>when</th>
1878 * <td>now ({@link System#currentTimeMillis()})</td></tr>
1879 * <tr><th align=right>audio stream</th>
1880 * <td>{@link #STREAM_DEFAULT}</td></tr>
1881 * </table>
Joe Onoratocb109a02011-01-18 17:57:41 -08001882 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001883
1884 * @param context
1885 * A {@link Context} that will be used by the Builder to construct the
1886 * RemoteViews. The Context will not be held past the lifetime of this Builder
1887 * object.
Joe Onoratocb109a02011-01-18 17:57:41 -08001888 */
Joe Onorato46439ce2010-11-19 13:56:21 -08001889 public Builder(Context context) {
Adam Powellbf06fa02014-05-30 12:32:18 -07001890 /*
1891 * Important compatibility note!
1892 * Some apps out in the wild create a Notification.Builder in their Activity subclass
1893 * constructor for later use. At this point Activities - themselves subclasses of
1894 * ContextWrapper - do not have their inner Context populated yet. This means that
1895 * any calls to Context methods from within this constructor can cause NPEs in existing
1896 * apps. Any data populated from mContext should therefore be populated lazily to
1897 * preserve compatibility.
1898 */
Joe Onorato46439ce2010-11-19 13:56:21 -08001899 mContext = context;
Andy Stadler110988c2010-12-03 14:29:16 -08001900
1901 // Set defaults to match the defaults of a Notification
Joe Onorato46439ce2010-11-19 13:56:21 -08001902 mWhen = System.currentTimeMillis();
Andy Stadler110988c2010-12-03 14:29:16 -08001903 mAudioStreamType = STREAM_DEFAULT;
John Spurlockc0650f022014-07-19 13:22:39 -04001904 mAudioAttributes = AUDIO_ATTRIBUTES_DEFAULT;
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001905 mPriority = PRIORITY_DEFAULT;
Chris Wrendde75302014-03-26 17:24:15 -04001906 mPeople = new ArrayList<String>();
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01001907
Christoph Studer239f8352014-08-25 15:13:18 +02001908 mColorUtil = context.getApplicationInfo().targetSdkVersion < Build.VERSION_CODES.L ?
1909 NotificationColorUtil.getInstance() : null;
Joe Onorato46439ce2010-11-19 13:56:21 -08001910 }
1911
Joe Onoratocb109a02011-01-18 17:57:41 -08001912 /**
Christoph Studer4600f9b2014-07-22 22:44:43 +02001913 * Creates a Builder for rebuilding the given Notification.
1914 * <p>
1915 * Call {@link #rebuild()} to retrieve the rebuilt version of 'n'.
1916 */
1917 private Builder(Context context, Notification n) {
1918 this(context);
1919 mRebuildNotification = n;
1920 restoreFromNotification(n);
1921
1922 Style style = null;
1923 Bundle extras = n.extras;
1924 String templateClass = extras.getString(EXTRA_TEMPLATE);
1925 if (!TextUtils.isEmpty(templateClass)) {
1926 Class<? extends Style> styleClass = getNotificationStyleClass(templateClass);
1927 if (styleClass == null) {
1928 Log.d(TAG, "Unknown style class: " + styleClass);
1929 return;
1930 }
1931
1932 try {
1933 Constructor<? extends Style> constructor = styleClass.getConstructor();
1934 style = constructor.newInstance();
1935 style.restoreFromExtras(extras);
1936 } catch (Throwable t) {
1937 Log.e(TAG, "Could not create Style", t);
1938 return;
1939 }
1940 }
1941 if (style != null) {
1942 setStyle(style);
1943 }
1944 }
1945
1946 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001947 * Add a timestamp pertaining to the notification (usually the time the event occurred).
Daniel Sandler0c890492012-09-12 17:23:10 -07001948 * It will be shown in the notification content view by default; use
Griff Hazen50c11652014-05-16 09:46:31 -07001949 * {@link #setShowWhen(boolean) setShowWhen} to control this.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001950 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001951 * @see Notification#when
Joe Onoratocb109a02011-01-18 17:57:41 -08001952 */
Joe Onorato46439ce2010-11-19 13:56:21 -08001953 public Builder setWhen(long when) {
1954 mWhen = when;
1955 return this;
1956 }
1957
Joe Onoratocb109a02011-01-18 17:57:41 -08001958 /**
Griff Hazen50c11652014-05-16 09:46:31 -07001959 * Control whether the timestamp set with {@link #setWhen(long) setWhen} is shown
Daniel Sandler0c890492012-09-12 17:23:10 -07001960 * in the content view.
1961 */
1962 public Builder setShowWhen(boolean show) {
1963 mShowWhen = show;
1964 return this;
1965 }
1966
1967 /**
Daniel Sandlerd33b8032012-05-10 11:41:48 -04001968 * Show the {@link Notification#when} field as a stopwatch.
Joe Malin8d40d042012-11-05 11:36:40 -08001969 *
1970 * Instead of presenting <code>when</code> as a timestamp, the notification will show an
Daniel Sandlerd33b8032012-05-10 11:41:48 -04001971 * automatically updating display of the minutes and seconds since <code>when</code>.
Daniel Sandlera2985ed2012-04-03 16:42:00 -04001972 *
Daniel Sandlerd33b8032012-05-10 11:41:48 -04001973 * Useful when showing an elapsed time (like an ongoing phone call).
1974 *
1975 * @see android.widget.Chronometer
Daniel Sandlera2985ed2012-04-03 16:42:00 -04001976 * @see Notification#when
1977 */
1978 public Builder setUsesChronometer(boolean b) {
1979 mUseChronometer = b;
1980 return this;
1981 }
1982
1983 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001984 * Set the small icon resource, which will be used to represent the notification in the
1985 * status bar.
Joe Onoratocb109a02011-01-18 17:57:41 -08001986 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001987
1988 * The platform template for the expanded view will draw this icon in the left, unless a
1989 * {@link #setLargeIcon(Bitmap) large icon} has also been specified, in which case the small
1990 * icon will be moved to the right-hand side.
1991 *
1992
1993 * @param icon
1994 * A resource ID in the application's package of the drawable to use.
1995 * @see Notification#icon
Joe Onoratocb109a02011-01-18 17:57:41 -08001996 */
Joe Onorato46439ce2010-11-19 13:56:21 -08001997 public Builder setSmallIcon(int icon) {
1998 mSmallIcon = icon;
1999 return this;
2000 }
2001
Joe Onoratocb109a02011-01-18 17:57:41 -08002002 /**
2003 * A variant of {@link #setSmallIcon(int) setSmallIcon(int)} that takes an additional
2004 * level parameter for when the icon is a {@link android.graphics.drawable.LevelListDrawable
2005 * LevelListDrawable}.
2006 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002007 * @param icon A resource ID in the application's package of the drawable to use.
Joe Onoratocb109a02011-01-18 17:57:41 -08002008 * @param level The level to use for the icon.
2009 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002010 * @see Notification#icon
2011 * @see Notification#iconLevel
Joe Onoratocb109a02011-01-18 17:57:41 -08002012 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002013 public Builder setSmallIcon(int icon, int level) {
2014 mSmallIcon = icon;
2015 mSmallIconLevel = level;
2016 return this;
2017 }
2018
Joe Onoratocb109a02011-01-18 17:57:41 -08002019 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002020 * Set the first line of text in the platform notification template.
Joe Onoratocb109a02011-01-18 17:57:41 -08002021 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002022 public Builder setContentTitle(CharSequence title) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04002023 mContentTitle = safeCharSequence(title);
Joe Onorato46439ce2010-11-19 13:56:21 -08002024 return this;
2025 }
2026
Joe Onoratocb109a02011-01-18 17:57:41 -08002027 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002028 * Set the second line of text in the platform notification template.
Joe Onoratocb109a02011-01-18 17:57:41 -08002029 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002030 public Builder setContentText(CharSequence text) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04002031 mContentText = safeCharSequence(text);
Joe Onorato46439ce2010-11-19 13:56:21 -08002032 return this;
2033 }
2034
Joe Onoratocb109a02011-01-18 17:57:41 -08002035 /**
Joe Malin8d40d042012-11-05 11:36:40 -08002036 * Set the third line of text in the platform notification template.
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04002037 * Don't use if you're also using {@link #setProgress(int, int, boolean)}; they occupy the
2038 * same location in the standard template.
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002039 */
2040 public Builder setSubText(CharSequence text) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04002041 mSubText = safeCharSequence(text);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002042 return this;
2043 }
2044
2045 /**
Joe Onoratocb109a02011-01-18 17:57:41 -08002046 * Set the large number at the right-hand side of the notification. This is
2047 * equivalent to setContentInfo, although it might show the number in a different
2048 * font size for readability.
2049 */
Joe Onorato8595a3d2010-11-19 18:12:07 -08002050 public Builder setNumber(int number) {
2051 mNumber = number;
2052 return this;
2053 }
2054
Joe Onoratocb109a02011-01-18 17:57:41 -08002055 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002056 * A small piece of additional information pertaining to this notification.
2057 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002058 * The platform template will draw this on the last line of the notification, at the far
2059 * right (to the right of a smallIcon if it has been placed there).
Joe Onoratocb109a02011-01-18 17:57:41 -08002060 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002061 public Builder setContentInfo(CharSequence info) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04002062 mContentInfo = safeCharSequence(info);
Joe Onorato46439ce2010-11-19 13:56:21 -08002063 return this;
2064 }
2065
Joe Onoratocb109a02011-01-18 17:57:41 -08002066 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002067 * Set the progress this notification represents.
2068 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002069 * The platform template will represent this using a {@link ProgressBar}.
Jeff Sharkey1c400132011-08-05 14:50:13 -07002070 */
2071 public Builder setProgress(int max, int progress, boolean indeterminate) {
2072 mProgressMax = max;
2073 mProgress = progress;
2074 mProgressIndeterminate = indeterminate;
2075 return this;
2076 }
2077
2078 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002079 * Supply a custom RemoteViews to use instead of the platform template.
2080 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002081 * @see Notification#contentView
Joe Onoratocb109a02011-01-18 17:57:41 -08002082 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002083 public Builder setContent(RemoteViews views) {
2084 mContentView = views;
2085 return this;
2086 }
2087
Joe Onoratocb109a02011-01-18 17:57:41 -08002088 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002089 * Supply a {@link PendingIntent} to be sent when the notification is clicked.
2090 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002091 * As of {@link android.os.Build.VERSION_CODES#HONEYCOMB}, if this field is unset and you
2092 * have specified a custom RemoteViews with {@link #setContent(RemoteViews)}, you can use
2093 * {@link RemoteViews#setOnClickPendingIntent RemoteViews.setOnClickPendingIntent(int,PendingIntent)}
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002094 * to assign PendingIntents to individual views in that custom layout (i.e., to create
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002095 * clickable buttons inside the notification view).
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002096 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002097 * @see Notification#contentIntent Notification.contentIntent
Joe Onoratocb109a02011-01-18 17:57:41 -08002098 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002099 public Builder setContentIntent(PendingIntent intent) {
2100 mContentIntent = intent;
2101 return this;
2102 }
2103
Joe Onoratocb109a02011-01-18 17:57:41 -08002104 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002105 * Supply a {@link PendingIntent} to send when the notification is cleared explicitly by the user.
2106 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002107 * @see Notification#deleteIntent
Joe Onoratocb109a02011-01-18 17:57:41 -08002108 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002109 public Builder setDeleteIntent(PendingIntent intent) {
2110 mDeleteIntent = intent;
2111 return this;
2112 }
2113
Joe Onoratocb109a02011-01-18 17:57:41 -08002114 /**
2115 * An intent to launch instead of posting the notification to the status bar.
2116 * Only for use with extremely high-priority notifications demanding the user's
2117 * <strong>immediate</strong> attention, such as an incoming phone call or
2118 * alarm clock that the user has explicitly set to a particular time.
2119 * If this facility is used for something else, please give the user an option
2120 * to turn it off and use a normal notification, as this can be extremely
2121 * disruptive.
2122 *
Chris Wren47c20a12014-06-18 17:27:29 -04002123 * <p>
2124 * The system UI may choose to display a heads-up notification, instead of
2125 * launching this intent, while the user is using the device.
2126 * </p>
2127 *
Joe Onoratocb109a02011-01-18 17:57:41 -08002128 * @param intent The pending intent to launch.
2129 * @param highPriority Passing true will cause this notification to be sent
2130 * even if other notifications are suppressed.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002131 *
2132 * @see Notification#fullScreenIntent
Joe Onoratocb109a02011-01-18 17:57:41 -08002133 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002134 public Builder setFullScreenIntent(PendingIntent intent, boolean highPriority) {
2135 mFullScreenIntent = intent;
2136 setFlag(FLAG_HIGH_PRIORITY, highPriority);
2137 return this;
2138 }
2139
Joe Onoratocb109a02011-01-18 17:57:41 -08002140 /**
Dan Sandler5fcdf6e2014-07-18 11:31:15 -04002141 * Set the "ticker" text which is sent to accessibility services.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002142 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002143 * @see Notification#tickerText
Joe Onoratocb109a02011-01-18 17:57:41 -08002144 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002145 public Builder setTicker(CharSequence tickerText) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04002146 mTickerText = safeCharSequence(tickerText);
Joe Onorato46439ce2010-11-19 13:56:21 -08002147 return this;
2148 }
2149
Joe Onoratocb109a02011-01-18 17:57:41 -08002150 /**
Dan Sandler5fcdf6e2014-07-18 11:31:15 -04002151 * Obsolete version of {@link #setTicker(CharSequence)}.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002152 *
Joe Onoratocb109a02011-01-18 17:57:41 -08002153 */
Dan Sandler5fcdf6e2014-07-18 11:31:15 -04002154 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -08002155 public Builder setTicker(CharSequence tickerText, RemoteViews views) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04002156 mTickerText = safeCharSequence(tickerText);
Dan Sandler5fcdf6e2014-07-18 11:31:15 -04002157 mTickerView = views; // we'll save it for you anyway
Joe Onorato46439ce2010-11-19 13:56:21 -08002158 return this;
2159 }
2160
Joe Onoratocb109a02011-01-18 17:57:41 -08002161 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002162 * Add a large icon to the notification (and the ticker on some devices).
2163 *
2164 * In the platform template, this image will be shown on the left of the notification view
2165 * in place of the {@link #setSmallIcon(int) small icon} (which will move to the right side).
2166 *
2167 * @see Notification#largeIcon
Joe Onoratocb109a02011-01-18 17:57:41 -08002168 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002169 public Builder setLargeIcon(Bitmap icon) {
2170 mLargeIcon = icon;
2171 return this;
2172 }
2173
Joe Onoratocb109a02011-01-18 17:57:41 -08002174 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002175 * Set the sound to play.
2176 *
John Spurlockc0650f022014-07-19 13:22:39 -04002177 * It will be played using the {@link #AUDIO_ATTRIBUTES_DEFAULT default audio attributes}
2178 * for notifications.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002179 *
Chris Wren47c20a12014-06-18 17:27:29 -04002180 * <p>
2181 * A notification that is noisy is more likely to be presented as a heads-up notification.
2182 * </p>
2183 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002184 * @see Notification#sound
Joe Onoratocb109a02011-01-18 17:57:41 -08002185 */
Joe Onorato52f80cd2010-11-21 15:34:48 -08002186 public Builder setSound(Uri sound) {
2187 mSound = sound;
John Spurlockc0650f022014-07-19 13:22:39 -04002188 mAudioAttributes = AUDIO_ATTRIBUTES_DEFAULT;
Joe Onorato52f80cd2010-11-21 15:34:48 -08002189 return this;
2190 }
2191
Joe Onoratocb109a02011-01-18 17:57:41 -08002192 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002193 * Set the sound to play, along with a specific stream on which to play it.
Joe Onoratocb109a02011-01-18 17:57:41 -08002194 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002195 * See {@link android.media.AudioManager} for the <code>STREAM_</code> constants.
2196 *
Chris Wren47c20a12014-06-18 17:27:29 -04002197 * <p>
2198 * A notification that is noisy is more likely to be presented as a heads-up notification.
2199 * </p>
John Spurlockc0650f022014-07-19 13:22:39 -04002200 * @deprecated use {@link #setSound(Uri, AudioAttributes)} instead.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002201 * @see Notification#sound
Joe Onoratocb109a02011-01-18 17:57:41 -08002202 */
Jean-Michel Trivi81f871e2014-08-06 16:32:38 -07002203 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -08002204 public Builder setSound(Uri sound, int streamType) {
2205 mSound = sound;
2206 mAudioStreamType = streamType;
2207 return this;
2208 }
2209
Joe Onoratocb109a02011-01-18 17:57:41 -08002210 /**
John Spurlockc0650f022014-07-19 13:22:39 -04002211 * Set the sound to play, along with specific {@link AudioAttributes audio attributes} to
2212 * use during playback.
2213 *
2214 * <p>
2215 * A notification that is noisy is more likely to be presented as a heads-up notification.
2216 * </p>
2217 *
2218 * @see Notification#sound
2219 */
2220 public Builder setSound(Uri sound, AudioAttributes audioAttributes) {
2221 mSound = sound;
2222 mAudioAttributes = audioAttributes;
2223 return this;
2224 }
2225
2226 /**
Joe Onoratocb109a02011-01-18 17:57:41 -08002227 * Set the vibration pattern to use.
2228 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002229 * See {@link android.os.Vibrator#vibrate(long[], int)} for a discussion of the
2230 * <code>pattern</code> parameter.
2231 *
Chris Wren47c20a12014-06-18 17:27:29 -04002232 * <p>
2233 * A notification that vibrates is more likely to be presented as a heads-up notification.
2234 * </p>
2235 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002236 * @see Notification#vibrate
Joe Onoratocb109a02011-01-18 17:57:41 -08002237 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002238 public Builder setVibrate(long[] pattern) {
2239 mVibrate = pattern;
2240 return this;
2241 }
2242
Joe Onoratocb109a02011-01-18 17:57:41 -08002243 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002244 * Set the desired color for the indicator LED on the device, as well as the
2245 * blink duty cycle (specified in milliseconds).
2246 *
2247
2248 * Not all devices will honor all (or even any) of these values.
2249 *
2250
2251 * @see Notification#ledARGB
2252 * @see Notification#ledOnMS
2253 * @see Notification#ledOffMS
Joe Onoratocb109a02011-01-18 17:57:41 -08002254 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002255 public Builder setLights(int argb, int onMs, int offMs) {
2256 mLedArgb = argb;
2257 mLedOnMs = onMs;
2258 mLedOffMs = offMs;
Joe Onorato46439ce2010-11-19 13:56:21 -08002259 return this;
2260 }
2261
Joe Onoratocb109a02011-01-18 17:57:41 -08002262 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002263 * Set whether this is an "ongoing" notification.
Joe Onoratocb109a02011-01-18 17:57:41 -08002264 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002265
2266 * Ongoing notifications cannot be dismissed by the user, so your application or service
2267 * must take care of canceling them.
2268 *
2269
2270 * They are typically used to indicate a background task that the user is actively engaged
2271 * with (e.g., playing music) or is pending in some way and therefore occupying the device
2272 * (e.g., a file download, sync operation, active network connection).
2273 *
2274
2275 * @see Notification#FLAG_ONGOING_EVENT
2276 * @see Service#setForeground(boolean)
Joe Onoratocb109a02011-01-18 17:57:41 -08002277 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002278 public Builder setOngoing(boolean ongoing) {
2279 setFlag(FLAG_ONGOING_EVENT, ongoing);
2280 return this;
2281 }
2282
Joe Onoratocb109a02011-01-18 17:57:41 -08002283 /**
2284 * Set this flag if you would only like the sound, vibrate
2285 * and ticker to be played if the notification is not already showing.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002286 *
2287 * @see Notification#FLAG_ONLY_ALERT_ONCE
Joe Onoratocb109a02011-01-18 17:57:41 -08002288 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002289 public Builder setOnlyAlertOnce(boolean onlyAlertOnce) {
2290 setFlag(FLAG_ONLY_ALERT_ONCE, onlyAlertOnce);
2291 return this;
2292 }
2293
Joe Onoratocb109a02011-01-18 17:57:41 -08002294 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002295 * Make this notification automatically dismissed when the user touches it. The
2296 * PendingIntent set with {@link #setDeleteIntent} will be sent when this happens.
2297 *
2298 * @see Notification#FLAG_AUTO_CANCEL
Joe Onoratocb109a02011-01-18 17:57:41 -08002299 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002300 public Builder setAutoCancel(boolean autoCancel) {
Joe Onorato281d83f2011-01-04 17:13:10 -08002301 setFlag(FLAG_AUTO_CANCEL, autoCancel);
Joe Onorato46439ce2010-11-19 13:56:21 -08002302 return this;
2303 }
2304
Joe Onoratocb109a02011-01-18 17:57:41 -08002305 /**
Griff Hazendfcb0802014-02-11 12:00:00 -08002306 * Set whether or not this notification should not bridge to other devices.
2307 *
2308 * <p>Some notifications can be bridged to other devices for remote display.
2309 * This hint can be set to recommend this notification not be bridged.
2310 */
2311 public Builder setLocalOnly(boolean localOnly) {
2312 setFlag(FLAG_LOCAL_ONLY, localOnly);
2313 return this;
2314 }
2315
2316 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002317 * Set which notification properties will be inherited from system defaults.
Joe Onoratocb109a02011-01-18 17:57:41 -08002318 * <p>
2319 * The value should be one or more of the following fields combined with
2320 * bitwise-or:
2321 * {@link #DEFAULT_SOUND}, {@link #DEFAULT_VIBRATE}, {@link #DEFAULT_LIGHTS}.
2322 * <p>
2323 * For all default values, use {@link #DEFAULT_ALL}.
2324 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002325 public Builder setDefaults(int defaults) {
2326 mDefaults = defaults;
2327 return this;
2328 }
2329
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002330 /**
2331 * Set the priority of this notification.
2332 *
2333 * @see Notification#priority
2334 */
Tor Norbyed9273d62013-05-30 15:59:53 -07002335 public Builder setPriority(@Priority int pri) {
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002336 mPriority = pri;
2337 return this;
2338 }
Joe Malin8d40d042012-11-05 11:36:40 -08002339
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002340 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -04002341 * Set the notification category.
Joe Malin8d40d042012-11-05 11:36:40 -08002342 *
John Spurlockfd7f1e02014-03-18 16:41:57 -04002343 * @see Notification#category
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002344 */
John Spurlockfd7f1e02014-03-18 16:41:57 -04002345 public Builder setCategory(String category) {
2346 mCategory = category;
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002347 return this;
2348 }
2349
2350 /**
Chris Wrendde75302014-03-26 17:24:15 -04002351 * Add a person that is relevant to this notification.
2352 *
2353 * @see Notification#EXTRA_PEOPLE
2354 */
2355 public Builder addPerson(String handle) {
2356 mPeople.add(handle);
2357 return this;
2358 }
2359
2360 /**
Griff Hazen5cadc3b2014-05-20 09:55:39 -07002361 * Set this notification to be part of a group of notifications sharing the same key.
2362 * Grouped notifications may display in a cluster or stack on devices which
2363 * support such rendering.
2364 *
2365 * <p>To make this notification the summary for its group, also call
2366 * {@link #setGroupSummary}. A sort order can be specified for group members by using
2367 * {@link #setSortKey}.
2368 * @param groupKey The group key of the group.
2369 * @return this object for method chaining
2370 */
2371 public Builder setGroup(String groupKey) {
2372 mGroupKey = groupKey;
2373 return this;
2374 }
2375
2376 /**
2377 * Set this notification to be the group summary for a group of notifications.
2378 * Grouped notifications may display in a cluster or stack on devices which
2379 * support such rendering. Requires a group key also be set using {@link #setGroup}.
2380 * @param isGroupSummary Whether this notification should be a group summary.
2381 * @return this object for method chaining
2382 */
2383 public Builder setGroupSummary(boolean isGroupSummary) {
2384 setFlag(FLAG_GROUP_SUMMARY, isGroupSummary);
2385 return this;
2386 }
2387
2388 /**
2389 * Set a sort key that orders this notification among other notifications from the
2390 * same package. This can be useful if an external sort was already applied and an app
2391 * would like to preserve this. Notifications will be sorted lexicographically using this
2392 * value, although providing different priorities in addition to providing sort key may
2393 * cause this value to be ignored.
2394 *
2395 * <p>This sort key can also be used to order members of a notification group. See
Griff Hazen9e1379f2014-05-20 12:50:51 -07002396 * {@link #setGroup}.
Griff Hazen5cadc3b2014-05-20 09:55:39 -07002397 *
2398 * @see String#compareTo(String)
2399 */
2400 public Builder setSortKey(String sortKey) {
2401 mSortKey = sortKey;
2402 return this;
2403 }
2404
2405 /**
Griff Hazen720042b2014-02-24 15:46:56 -08002406 * Merge additional metadata into this notification.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002407 *
Griff Hazen720042b2014-02-24 15:46:56 -08002408 * <p>Values within the Bundle will replace existing extras values in this Builder.
2409 *
2410 * @see Notification#extras
2411 */
Griff Hazen959591e2014-05-15 22:26:18 -07002412 public Builder addExtras(Bundle extras) {
2413 if (extras != null) {
2414 if (mExtras == null) {
2415 mExtras = new Bundle(extras);
2416 } else {
2417 mExtras.putAll(extras);
2418 }
Griff Hazen720042b2014-02-24 15:46:56 -08002419 }
2420 return this;
2421 }
2422
2423 /**
2424 * Set metadata for this notification.
2425 *
2426 * <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 -04002427 * current contents are copied into the Notification each time {@link #build()} is
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002428 * called.
2429 *
Griff Hazen720042b2014-02-24 15:46:56 -08002430 * <p>Replaces any existing extras values with those from the provided Bundle.
2431 * Use {@link #addExtras} to merge in metadata instead.
2432 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002433 * @see Notification#extras
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002434 */
Griff Hazen959591e2014-05-15 22:26:18 -07002435 public Builder setExtras(Bundle extras) {
2436 mExtras = extras;
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002437 return this;
2438 }
2439
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002440 /**
Griff Hazen720042b2014-02-24 15:46:56 -08002441 * Get the current metadata Bundle used by this notification Builder.
2442 *
2443 * <p>The returned Bundle is shared with this Builder.
2444 *
2445 * <p>The current contents of this Bundle are copied into the Notification each time
2446 * {@link #build()} is called.
2447 *
2448 * @see Notification#extras
2449 */
2450 public Bundle getExtras() {
2451 if (mExtras == null) {
2452 mExtras = new Bundle();
2453 }
2454 return mExtras;
2455 }
2456
2457 /**
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002458 * Add an action to this notification. Actions are typically displayed by
2459 * the system as a button adjacent to the notification content.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04002460 * <p>
2461 * Every action must have an icon (32dp square and matching the
2462 * <a href="{@docRoot}design/style/iconography.html#action-bar">Holo
2463 * Dark action bar</a> visual style), a textual label, and a {@link PendingIntent}.
2464 * <p>
2465 * A notification in its expanded form can display up to 3 actions, from left to right in
2466 * the order they were added. Actions will not be displayed when the notification is
2467 * collapsed, however, so be sure that any essential functions may be accessed by the user
2468 * in some other way (for example, in the Activity pointed to by {@link #contentIntent}).
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002469 *
2470 * @param icon Resource ID of a drawable that represents the action.
2471 * @param title Text describing the action.
2472 * @param intent PendingIntent to be fired when the action is invoked.
2473 */
2474 public Builder addAction(int icon, CharSequence title, PendingIntent intent) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04002475 mActions.add(new Action(icon, safeCharSequence(title), intent));
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002476 return this;
2477 }
2478
Chris Wrenfbd96ba2012-05-01 12:03:58 -04002479 /**
Griff Hazen959591e2014-05-15 22:26:18 -07002480 * Add an action to this notification. Actions are typically displayed by
2481 * the system as a button adjacent to the notification content.
2482 * <p>
2483 * Every action must have an icon (32dp square and matching the
2484 * <a href="{@docRoot}design/style/iconography.html#action-bar">Holo
2485 * Dark action bar</a> visual style), a textual label, and a {@link PendingIntent}.
2486 * <p>
2487 * A notification in its expanded form can display up to 3 actions, from left to right in
2488 * the order they were added. Actions will not be displayed when the notification is
2489 * collapsed, however, so be sure that any essential functions may be accessed by the user
2490 * in some other way (for example, in the Activity pointed to by {@link #contentIntent}).
2491 *
2492 * @param action The action to add.
2493 */
2494 public Builder addAction(Action action) {
2495 mActions.add(action);
2496 return this;
2497 }
2498
2499 /**
Chris Wrenfbd96ba2012-05-01 12:03:58 -04002500 * Add a rich notification style to be applied at build time.
2501 *
2502 * @param style Object responsible for modifying the notification style.
2503 */
2504 public Builder setStyle(Style style) {
2505 if (mStyle != style) {
2506 mStyle = style;
Daniel Sandlerc08dea22012-06-28 08:35:24 -07002507 if (mStyle != null) {
2508 mStyle.setBuilder(this);
2509 }
Chris Wrenfbd96ba2012-05-01 12:03:58 -04002510 }
2511 return this;
2512 }
2513
Dan Sandler0bf2ed82013-12-21 23:33:41 -06002514 /**
2515 * Specify the value of {@link #visibility}.
Griff Hazenb720abe2014-05-20 13:15:30 -07002516 *
Dan Sandler0bf2ed82013-12-21 23:33:41 -06002517 * @param visibility One of {@link #VISIBILITY_PRIVATE} (the default),
2518 * {@link #VISIBILITY_SECRET}, or {@link #VISIBILITY_PUBLIC}.
2519 *
2520 * @return The same Builder.
2521 */
2522 public Builder setVisibility(int visibility) {
2523 mVisibility = visibility;
2524 return this;
2525 }
2526
2527 /**
2528 * Supply a replacement Notification whose contents should be shown in insecure contexts
2529 * (i.e. atop the secure lockscreen). See {@link #visibility} and {@link #VISIBILITY_PUBLIC}.
2530 * @param n A replacement notification, presumably with some or all info redacted.
2531 * @return The same Builder.
2532 */
2533 public Builder setPublicVersion(Notification n) {
2534 mPublicVersion = n;
2535 return this;
2536 }
2537
Griff Hazenb720abe2014-05-20 13:15:30 -07002538 /**
Griff Hazen5cadc3b2014-05-20 09:55:39 -07002539 * Apply an extender to this notification builder. Extenders may be used to add
2540 * metadata or change options on this builder.
2541 */
Griff Hazen61a9e862014-05-22 16:05:19 -07002542 public Builder extend(Extender extender) {
2543 extender.extend(this);
Griff Hazen5cadc3b2014-05-20 09:55:39 -07002544 return this;
2545 }
2546
Joe Onorato46439ce2010-11-19 13:56:21 -08002547 private void setFlag(int mask, boolean value) {
2548 if (value) {
2549 mFlags |= mask;
2550 } else {
2551 mFlags &= ~mask;
2552 }
2553 }
2554
Dan Sandler26e81cf2014-05-06 10:01:27 -04002555 /**
2556 * Sets {@link Notification#color}.
2557 *
2558 * @param argb The accent color to use
2559 *
2560 * @return The same Builder.
2561 */
2562 public Builder setColor(int argb) {
2563 mColor = argb;
2564 return this;
2565 }
2566
Kenny Guy8a0101b2014-05-08 23:34:12 +01002567 private Bitmap getProfileBadge() {
Christoph Studer7ac80e62014-08-04 16:01:57 +02002568 // Note: This assumes that the current user can read the profile badge of the
2569 // originating user.
Kenny Guy8a0101b2014-05-08 23:34:12 +01002570 UserManager userManager = (UserManager) mContext.getSystemService(Context.USER_SERVICE);
Svetoslavc71c42f2014-08-05 18:57:05 -07002571 Drawable badge = userManager.getBadgeForUser(new UserHandle(mOriginatingUserId), 0);
Kenny Guy8a0101b2014-05-08 23:34:12 +01002572 if (badge == null) {
2573 return null;
2574 }
2575 final int width = badge.getIntrinsicWidth();
2576 final int height = badge.getIntrinsicHeight();
2577 Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
2578 Canvas canvas = new Canvas(bitmap);
2579 badge.setBounds(0, 0, width, height);
2580 badge.draw(canvas);
2581 return bitmap;
2582 }
2583
Kenny Guy98193ea2014-07-24 19:54:37 +01002584 private boolean addProfileBadge(RemoteViews contentView, int resId) {
2585 Bitmap profileBadge = getProfileBadge();
2586
2587 contentView.setViewVisibility(R.id.profile_badge_large_template, View.GONE);
2588 contentView.setViewVisibility(R.id.profile_badge_line2, View.GONE);
2589 contentView.setViewVisibility(R.id.profile_badge_line3, View.GONE);
2590
2591 if (profileBadge != null) {
2592 contentView.setImageViewBitmap(resId, profileBadge);
2593 contentView.setViewVisibility(resId, View.VISIBLE);
2594
2595 // Make sure Line 3 is visible. As badge will be here if there
2596 // is no text to display.
2597 if (resId == R.id.profile_badge_line3) {
2598 contentView.setViewVisibility(R.id.line3, View.VISIBLE);
2599 }
2600 return true;
2601 }
2602 return false;
2603 }
2604
Jorim Jaggi445d3c02014-08-19 22:33:42 +02002605 private RemoteViews applyStandardTemplate(int resId) {
Svet Ganov0da85b62014-08-06 14:11:37 -07002606 RemoteViews contentView = new BuilderRemoteViews(mContext.getPackageName(),
2607 mOriginatingUserId, resId);
Dan Sandler539aad42014-08-04 00:43:39 -04002608
Daniel Sandler9f7936a2012-05-21 16:14:28 -04002609 boolean showLine3 = false;
2610 boolean showLine2 = false;
Kenny Guy98193ea2014-07-24 19:54:37 +01002611 boolean contentTextInLine2 = false;
Dan Sandler190d58d2014-05-15 09:33:39 -04002612
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01002613 if (mLargeIcon != null) {
2614 contentView.setImageViewBitmap(R.id.icon, mLargeIcon);
Christoph Studer239f8352014-08-25 15:13:18 +02002615 processLargeLegacyIcon(mLargeIcon, contentView);
Dan Sandler190d58d2014-05-15 09:33:39 -04002616 contentView.setImageViewResource(R.id.right_icon, mSmallIcon);
2617 contentView.setViewVisibility(R.id.right_icon, View.VISIBLE);
2618 processSmallRightIcon(mSmallIcon, contentView);
2619 } else { // small icon at left
2620 contentView.setImageViewResource(R.id.icon, mSmallIcon);
2621 contentView.setViewVisibility(R.id.icon, View.VISIBLE);
2622 processSmallIconAsLarge(mSmallIcon, contentView);
Joe Onorato561d3852010-11-20 18:09:34 -08002623 }
2624 if (mContentTitle != null) {
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01002625 contentView.setTextViewText(R.id.title, processLegacyText(mContentTitle));
Joe Onorato561d3852010-11-20 18:09:34 -08002626 }
2627 if (mContentText != null) {
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01002628 contentView.setTextViewText(R.id.text, processLegacyText(mContentText));
Daniel Sandler9f7936a2012-05-21 16:14:28 -04002629 showLine3 = true;
Joe Onorato561d3852010-11-20 18:09:34 -08002630 }
2631 if (mContentInfo != null) {
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01002632 contentView.setTextViewText(R.id.info, processLegacyText(mContentInfo));
Jeff Sharkey1c400132011-08-05 14:50:13 -07002633 contentView.setViewVisibility(R.id.info, View.VISIBLE);
Daniel Sandler9f7936a2012-05-21 16:14:28 -04002634 showLine3 = true;
Joe Onorato561d3852010-11-20 18:09:34 -08002635 } else if (mNumber > 0) {
Daniel Sandlerebce0112011-06-16 16:44:51 -04002636 final int tooBig = mContext.getResources().getInteger(
2637 R.integer.status_bar_notification_info_maxnum);
2638 if (mNumber > tooBig) {
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01002639 contentView.setTextViewText(R.id.info, processLegacyText(
2640 mContext.getResources().getString(
2641 R.string.status_bar_notification_info_overflow)));
Joe Onorato059a2f82011-01-04 10:27:01 -08002642 } else {
2643 NumberFormat f = NumberFormat.getIntegerInstance();
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01002644 contentView.setTextViewText(R.id.info, processLegacyText(f.format(mNumber)));
Joe Onorato059a2f82011-01-04 10:27:01 -08002645 }
Jeff Sharkey1c400132011-08-05 14:50:13 -07002646 contentView.setViewVisibility(R.id.info, View.VISIBLE);
Daniel Sandler9f7936a2012-05-21 16:14:28 -04002647 showLine3 = true;
Joe Onorato561d3852010-11-20 18:09:34 -08002648 } else {
2649 contentView.setViewVisibility(R.id.info, View.GONE);
2650 }
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002651
Daniel Sandler9f7936a2012-05-21 16:14:28 -04002652 // Need to show three lines?
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002653 if (mSubText != null) {
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01002654 contentView.setTextViewText(R.id.text, processLegacyText(mSubText));
Daniel Sandler9f7936a2012-05-21 16:14:28 -04002655 if (mContentText != null) {
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01002656 contentView.setTextViewText(R.id.text2, processLegacyText(mContentText));
Daniel Sandler9f7936a2012-05-21 16:14:28 -04002657 contentView.setViewVisibility(R.id.text2, View.VISIBLE);
2658 showLine2 = true;
Kenny Guy98193ea2014-07-24 19:54:37 +01002659 contentTextInLine2 = true;
Daniel Sandler9f7936a2012-05-21 16:14:28 -04002660 } else {
2661 contentView.setViewVisibility(R.id.text2, View.GONE);
2662 }
Jeff Sharkey1c400132011-08-05 14:50:13 -07002663 } else {
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002664 contentView.setViewVisibility(R.id.text2, View.GONE);
2665 if (mProgressMax != 0 || mProgressIndeterminate) {
2666 contentView.setProgressBar(
2667 R.id.progress, mProgressMax, mProgress, mProgressIndeterminate);
2668 contentView.setViewVisibility(R.id.progress, View.VISIBLE);
Daniel Sandler9f7936a2012-05-21 16:14:28 -04002669 showLine2 = true;
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002670 } else {
2671 contentView.setViewVisibility(R.id.progress, View.GONE);
2672 }
Jeff Sharkey1c400132011-08-05 14:50:13 -07002673 }
Daniel Sandler9f7936a2012-05-21 16:14:28 -04002674 if (showLine2) {
Jorim Jaggi445d3c02014-08-19 22:33:42 +02002675
2676 // need to shrink all the type to make sure everything fits
2677 final Resources res = mContext.getResources();
2678 final float subTextSize = res.getDimensionPixelSize(
2679 R.dimen.notification_subtext_size);
2680 contentView.setTextViewTextSize(R.id.text, TypedValue.COMPLEX_UNIT_PX, subTextSize);
Daniel Sandler9f7936a2012-05-21 16:14:28 -04002681 }
2682
Daniel Sandler0c890492012-09-12 17:23:10 -07002683 if (mWhen != 0 && mShowWhen) {
Daniel Sandlera2985ed2012-04-03 16:42:00 -04002684 if (mUseChronometer) {
2685 contentView.setViewVisibility(R.id.chronometer, View.VISIBLE);
2686 contentView.setLong(R.id.chronometer, "setBase",
2687 mWhen + (SystemClock.elapsedRealtime() - System.currentTimeMillis()));
2688 contentView.setBoolean(R.id.chronometer, "setStarted", true);
2689 } else {
2690 contentView.setViewVisibility(R.id.time, View.VISIBLE);
2691 contentView.setLong(R.id.time, "setTime", mWhen);
2692 }
Daniel Sandler0c890492012-09-12 17:23:10 -07002693 } else {
2694 contentView.setViewVisibility(R.id.time, View.GONE);
Joe Onorato561d3852010-11-20 18:09:34 -08002695 }
Daniel Sandler0c890492012-09-12 17:23:10 -07002696
Jorim Jaggi445d3c02014-08-19 22:33:42 +02002697 // Adjust padding depending on line count and font size.
2698 contentView.setViewPadding(R.id.line1, 0, calculateTopPadding(mContext,
2699 hasThreeLines(), mContext.getResources().getConfiguration().fontScale),
2700 0, 0);
2701
Kenny Guy98193ea2014-07-24 19:54:37 +01002702 // We want to add badge to first line of text.
2703 boolean addedBadge = addProfileBadge(contentView,
2704 contentTextInLine2 ? R.id.profile_badge_line2 : R.id.profile_badge_line3);
2705 // If we added the badge to line 3 then we should show line 3.
2706 if (addedBadge && !contentTextInLine2) {
2707 showLine3 = true;
2708 }
2709
2710 // Note getStandardView may hide line 3 again.
Daniel Sandler9f7936a2012-05-21 16:14:28 -04002711 contentView.setViewVisibility(R.id.line3, showLine3 ? View.VISIBLE : View.GONE);
Daniel Sandler6387d2f2012-05-22 13:44:09 -04002712 contentView.setViewVisibility(R.id.overflow_divider, showLine3 ? View.VISIBLE : View.GONE);
Joe Onorato561d3852010-11-20 18:09:34 -08002713 return contentView;
2714 }
2715
Jorim Jaggi445d3c02014-08-19 22:33:42 +02002716 /**
2717 * Logic to find out whether the notification is going to have three lines in the contracted
2718 * layout. This is used to adjust the top padding.
2719 *
2720 * @return true if the notification is going to have three lines; false if the notification
2721 * is going to have one or two lines
2722 */
2723 private boolean hasThreeLines() {
2724 boolean hasLine3 = mContentText != null || mContentInfo != null || mNumber > 0;
2725 boolean hasLine2 = (mSubText != null && mContentText != null) ||
2726 (mSubText == null && (mProgressMax != 0 || mProgressIndeterminate));
2727 return hasLine2 && hasLine3;
2728 }
2729
2730 /**
2731 * @hide
2732 */
2733 public static int calculateTopPadding(Context ctx, boolean hasThreeLines,
2734 float fontScale) {
2735 int padding = ctx.getResources().getDimensionPixelSize(hasThreeLines
2736 ? R.dimen.notification_top_pad_narrow
2737 : R.dimen.notification_top_pad);
2738 int largePadding = ctx.getResources().getDimensionPixelSize(hasThreeLines
2739 ? R.dimen.notification_top_pad_large_text_narrow
2740 : R.dimen.notification_top_pad_large_text);
2741 float largeFactor = (MathUtils.constrain(fontScale, 1.0f, LARGE_TEXT_SCALE) - 1f)
2742 / (LARGE_TEXT_SCALE - 1f);
2743
2744 // Linearly interpolate the padding between large and normal with the font scale ranging
2745 // from 1f to LARGE_TEXT_SCALE
2746 return Math.round((1 - largeFactor) * padding + largeFactor * largePadding);
2747 }
2748
Daniel Sandler96fd7c12012-03-30 16:37:36 -04002749 private RemoteViews applyStandardTemplateWithActions(int layoutId) {
Jorim Jaggi445d3c02014-08-19 22:33:42 +02002750 RemoteViews big = applyStandardTemplate(layoutId);
Daniel Sandler96fd7c12012-03-30 16:37:36 -04002751
2752 int N = mActions.size();
2753 if (N > 0) {
Daniel Sandler96fd7c12012-03-30 16:37:36 -04002754 big.setViewVisibility(R.id.actions, View.VISIBLE);
Daniel Sandler6387d2f2012-05-22 13:44:09 -04002755 big.setViewVisibility(R.id.action_divider, View.VISIBLE);
Daniel Sandler8680bf82012-05-15 16:52:52 -04002756 if (N>MAX_ACTION_BUTTONS) N=MAX_ACTION_BUTTONS;
Chris Wren2c22eb02012-05-08 09:49:13 -04002757 big.removeAllViews(R.id.actions);
Daniel Sandler96fd7c12012-03-30 16:37:36 -04002758 for (int i=0; i<N; i++) {
2759 final RemoteViews button = generateActionButton(mActions.get(i));
Daniel Sandler96fd7c12012-03-30 16:37:36 -04002760 big.addView(R.id.actions, button);
2761 }
2762 }
2763 return big;
2764 }
2765
Joe Onorato46439ce2010-11-19 13:56:21 -08002766 private RemoteViews makeContentView() {
2767 if (mContentView != null) {
2768 return mContentView;
2769 } else {
Jorim Jaggi445d3c02014-08-19 22:33:42 +02002770 return applyStandardTemplate(getBaseLayoutResource());
Joe Onorato46439ce2010-11-19 13:56:21 -08002771 }
2772 }
2773
2774 private RemoteViews makeTickerView() {
2775 if (mTickerView != null) {
2776 return mTickerView;
Joe Onorato46439ce2010-11-19 13:56:21 -08002777 }
Dan Sandler5fcdf6e2014-07-18 11:31:15 -04002778 return null; // tickers are not created by default anymore
Joe Onorato46439ce2010-11-19 13:56:21 -08002779 }
2780
Daniel Sandler96fd7c12012-03-30 16:37:36 -04002781 private RemoteViews makeBigContentView() {
2782 if (mActions.size() == 0) return null;
2783
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01002784 return applyStandardTemplateWithActions(getBigBaseLayoutResource());
Daniel Sandler96fd7c12012-03-30 16:37:36 -04002785 }
2786
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01002787 private RemoteViews makeHeadsUpContentView() {
Chris Wren8fd39ec2014-02-27 17:43:26 -05002788 if (mActions.size() == 0) return null;
2789
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01002790 return applyStandardTemplateWithActions(getBigBaseLayoutResource());
Chris Wren8fd39ec2014-02-27 17:43:26 -05002791 }
2792
2793
Daniel Sandler96fd7c12012-03-30 16:37:36 -04002794 private RemoteViews generateActionButton(Action action) {
Daniel Sandler8680bf82012-05-15 16:52:52 -04002795 final boolean tombstone = (action.actionIntent == null);
Joe Malin8d40d042012-11-05 11:36:40 -08002796 RemoteViews button = new RemoteViews(mContext.getPackageName(),
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01002797 tombstone ? getActionTombstoneLayoutResource()
2798 : getActionLayoutResource());
Chris Wren8749ac8a2013-12-03 14:31:01 -05002799 button.setTextViewCompoundDrawablesRelative(R.id.action0, action.icon, 0, 0, 0);
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01002800 button.setTextViewText(R.id.action0, processLegacyText(action.title));
Daniel Sandler8680bf82012-05-15 16:52:52 -04002801 if (!tombstone) {
Daniel Sandlere5518842012-05-10 16:20:40 -04002802 button.setOnClickPendingIntent(R.id.action0, action.actionIntent);
Daniel Sandlere5518842012-05-10 16:20:40 -04002803 }
Daniel Sandler96fd7c12012-03-30 16:37:36 -04002804 button.setContentDescription(R.id.action0, action.title);
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01002805 processLegacyAction(action, button);
Daniel Sandler96fd7c12012-03-30 16:37:36 -04002806 return button;
2807 }
2808
Joe Onoratocb109a02011-01-18 17:57:41 -08002809 /**
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01002810 * @return Whether we are currently building a notification from a legacy (an app that
Alan Viverette3cb07a462014-06-06 14:19:53 -07002811 * doesn't create material notifications by itself) app.
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01002812 */
2813 private boolean isLegacy() {
Dan Sandler26e81cf2014-05-06 10:01:27 -04002814 return mColorUtil != null;
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01002815 }
2816
2817 private void processLegacyAction(Action action, RemoteViews button) {
Christoph Studer239f8352014-08-25 15:13:18 +02002818 if (!isLegacy() || mColorUtil.isGrayscale(mContext, action.icon)) {
2819 button.setTextViewCompoundDrawablesRelativeColorFilter(R.id.action0, 0,
2820 mContext.getResources().getColor(R.color.notification_action_color_filter),
2821 PorterDuff.Mode.MULTIPLY);
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01002822 }
2823 }
2824
2825 private CharSequence processLegacyText(CharSequence charSequence) {
2826 if (isLegacy()) {
Dan Sandler26e81cf2014-05-06 10:01:27 -04002827 return mColorUtil.invertCharSequenceColors(charSequence);
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01002828 } else {
2829 return charSequence;
2830 }
2831 }
2832
Dan Sandler26e81cf2014-05-06 10:01:27 -04002833 /**
2834 * Apply any necessary background to smallIcons being used in the largeIcon spot.
2835 */
2836 private void processSmallIconAsLarge(int largeIconId, RemoteViews contentView) {
2837 if (!isLegacy() || mColorUtil.isGrayscale(mContext, largeIconId)) {
2838 applyLargeIconBackground(contentView);
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01002839 }
2840 }
2841
Dan Sandler26e81cf2014-05-06 10:01:27 -04002842 /**
2843 * Apply any necessary background to a largeIcon if it's a fake smallIcon (that is,
2844 * if it's grayscale).
2845 */
2846 // TODO: also check bounds, transparency, that sort of thing.
Christoph Studer239f8352014-08-25 15:13:18 +02002847 private void processLargeLegacyIcon(Bitmap largeIcon, RemoteViews contentView) {
2848 if (isLegacy() && mColorUtil.isGrayscale(largeIcon)) {
Dan Sandler26e81cf2014-05-06 10:01:27 -04002849 applyLargeIconBackground(contentView);
Dan Sandler190d58d2014-05-15 09:33:39 -04002850 } else {
2851 removeLargeIconBackground(contentView);
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01002852 }
2853 }
2854
Dan Sandler26e81cf2014-05-06 10:01:27 -04002855 /**
2856 * Add a colored circle behind the largeIcon slot.
2857 */
2858 private void applyLargeIconBackground(RemoteViews contentView) {
2859 contentView.setInt(R.id.icon, "setBackgroundResource",
Jorim Jaggi445d3c02014-08-19 22:33:42 +02002860 R.drawable.notification_icon_legacy_bg);
Dan Sandler26e81cf2014-05-06 10:01:27 -04002861
2862 contentView.setDrawableParameters(
2863 R.id.icon,
2864 true,
2865 -1,
Jorim Jaggi74419312014-06-10 20:57:21 +02002866 resolveColor(),
Dan Sandler26e81cf2014-05-06 10:01:27 -04002867 PorterDuff.Mode.SRC_ATOP,
2868 -1);
Jorim Jaggi445d3c02014-08-19 22:33:42 +02002869
2870 int padding = mContext.getResources().getDimensionPixelSize(
2871 R.dimen.notification_large_icon_circle_padding);
2872 contentView.setViewPadding(R.id.icon, padding, padding, padding, padding);
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01002873 }
2874
Dan Sandler190d58d2014-05-15 09:33:39 -04002875 private void removeLargeIconBackground(RemoteViews contentView) {
2876 contentView.setInt(R.id.icon, "setBackgroundResource", 0);
2877 }
2878
Dan Sandler26e81cf2014-05-06 10:01:27 -04002879 /**
2880 * Recolor small icons when used in the R.id.right_icon slot.
2881 */
Dan Sandler190d58d2014-05-15 09:33:39 -04002882 private void processSmallRightIcon(int smallIconDrawableId,
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01002883 RemoteViews contentView) {
Dan Sandler26e81cf2014-05-06 10:01:27 -04002884 if (!isLegacy() || mColorUtil.isGrayscale(mContext, smallIconDrawableId)) {
Dan Sandler190d58d2014-05-15 09:33:39 -04002885 contentView.setDrawableParameters(R.id.right_icon, false, -1,
2886 0xFFFFFFFF,
2887 PorterDuff.Mode.SRC_ATOP, -1);
2888
2889 contentView.setInt(R.id.right_icon,
2890 "setBackgroundResource",
2891 R.drawable.notification_icon_legacy_bg);
2892
2893 contentView.setDrawableParameters(
2894 R.id.right_icon,
2895 true,
2896 -1,
Jorim Jaggi74419312014-06-10 20:57:21 +02002897 resolveColor(),
Dan Sandler190d58d2014-05-15 09:33:39 -04002898 PorterDuff.Mode.SRC_ATOP,
2899 -1);
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01002900 }
2901 }
2902
Jorim Jaggi74419312014-06-10 20:57:21 +02002903 private int sanitizeColor() {
2904 if (mColor != COLOR_DEFAULT) {
2905 mColor |= 0xFF000000; // no alpha for custom colors
2906 }
2907 return mColor;
2908 }
2909
Dan Sandler26e81cf2014-05-06 10:01:27 -04002910 private int resolveColor() {
2911 if (mColor == COLOR_DEFAULT) {
Jorim Jaggi74419312014-06-10 20:57:21 +02002912 return mContext.getResources().getColor(R.color.notification_icon_bg_color);
Dan Sandler26e81cf2014-05-06 10:01:27 -04002913 }
2914 return mColor;
2915 }
2916
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01002917 /**
Chris Wrenfbd96ba2012-05-01 12:03:58 -04002918 * Apply the unstyled operations and return a new {@link Notification} object.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04002919 * @hide
Joe Onoratocb109a02011-01-18 17:57:41 -08002920 */
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04002921 public Notification buildUnstyled() {
Joe Onorato46439ce2010-11-19 13:56:21 -08002922 Notification n = new Notification();
2923 n.when = mWhen;
2924 n.icon = mSmallIcon;
2925 n.iconLevel = mSmallIconLevel;
Joe Onorato8595a3d2010-11-19 18:12:07 -08002926 n.number = mNumber;
Dan Sandler26e81cf2014-05-06 10:01:27 -04002927
Jorim Jaggi74419312014-06-10 20:57:21 +02002928 n.color = sanitizeColor();
Dan Sandler26e81cf2014-05-06 10:01:27 -04002929
Christoph Studer4600f9b2014-07-22 22:44:43 +02002930 setBuilderContentView(n, makeContentView());
Joe Onorato46439ce2010-11-19 13:56:21 -08002931 n.contentIntent = mContentIntent;
2932 n.deleteIntent = mDeleteIntent;
2933 n.fullScreenIntent = mFullScreenIntent;
2934 n.tickerText = mTickerText;
2935 n.tickerView = makeTickerView();
2936 n.largeIcon = mLargeIcon;
2937 n.sound = mSound;
2938 n.audioStreamType = mAudioStreamType;
John Spurlockc0650f022014-07-19 13:22:39 -04002939 n.audioAttributes = mAudioAttributes;
Joe Onorato46439ce2010-11-19 13:56:21 -08002940 n.vibrate = mVibrate;
2941 n.ledARGB = mLedArgb;
2942 n.ledOnMS = mLedOnMs;
2943 n.ledOffMS = mLedOffMs;
2944 n.defaults = mDefaults;
2945 n.flags = mFlags;
Christoph Studer4600f9b2014-07-22 22:44:43 +02002946 setBuilderBigContentView(n, makeBigContentView());
2947 setBuilderHeadsUpContentView(n, makeHeadsUpContentView());
Daniel Sandler26c13432013-04-04 11:01:04 -04002948 if (mLedOnMs != 0 || mLedOffMs != 0) {
Joe Onorato8d0b6552010-11-22 16:09:29 -08002949 n.flags |= FLAG_SHOW_LIGHTS;
2950 }
2951 if ((mDefaults & DEFAULT_LIGHTS) != 0) {
2952 n.flags |= FLAG_SHOW_LIGHTS;
2953 }
John Spurlockfd7f1e02014-03-18 16:41:57 -04002954 n.category = mCategory;
Griff Hazen5cadc3b2014-05-20 09:55:39 -07002955 n.mGroupKey = mGroupKey;
2956 n.mSortKey = mSortKey;
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002957 n.priority = mPriority;
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002958 if (mActions.size() > 0) {
2959 n.actions = new Action[mActions.size()];
2960 mActions.toArray(n.actions);
2961 }
Dan Sandler0bf2ed82013-12-21 23:33:41 -06002962 n.visibility = mVisibility;
2963
2964 if (mPublicVersion != null) {
2965 n.publicVersion = new Notification();
2966 mPublicVersion.cloneInto(n.publicVersion, true);
2967 }
Christoph Studer4600f9b2014-07-22 22:44:43 +02002968 // Note: If you're adding new fields, also update restoreFromNotitification().
Joe Onorato46439ce2010-11-19 13:56:21 -08002969 return n;
2970 }
Chris Wrenfbd96ba2012-05-01 12:03:58 -04002971
2972 /**
Daniel Sandlerf45564e2013-04-15 15:05:08 -04002973 * Capture, in the provided bundle, semantic information used in the construction of
2974 * this Notification object.
2975 * @hide
2976 */
Griff Hazen720042b2014-02-24 15:46:56 -08002977 public void populateExtras(Bundle extras) {
Daniel Sandlerf45564e2013-04-15 15:05:08 -04002978 // Store original information used in the construction of this object
Christoph Studer7ac80e62014-08-04 16:01:57 +02002979 extras.putInt(EXTRA_ORIGINATING_USERID, mOriginatingUserId);
Christoph Studer4600f9b2014-07-22 22:44:43 +02002980 extras.putString(EXTRA_REBUILD_CONTEXT_PACKAGE, mContext.getPackageName());
Daniel Sandlerf45564e2013-04-15 15:05:08 -04002981 extras.putCharSequence(EXTRA_TITLE, mContentTitle);
2982 extras.putCharSequence(EXTRA_TEXT, mContentText);
2983 extras.putCharSequence(EXTRA_SUB_TEXT, mSubText);
2984 extras.putCharSequence(EXTRA_INFO_TEXT, mContentInfo);
2985 extras.putInt(EXTRA_SMALL_ICON, mSmallIcon);
2986 extras.putInt(EXTRA_PROGRESS, mProgress);
2987 extras.putInt(EXTRA_PROGRESS_MAX, mProgressMax);
2988 extras.putBoolean(EXTRA_PROGRESS_INDETERMINATE, mProgressIndeterminate);
2989 extras.putBoolean(EXTRA_SHOW_CHRONOMETER, mUseChronometer);
2990 extras.putBoolean(EXTRA_SHOW_WHEN, mShowWhen);
John Spurlockac08a472013-06-10 11:37:08 -04002991 if (mLargeIcon != null) {
2992 extras.putParcelable(EXTRA_LARGE_ICON, mLargeIcon);
2993 }
Chris Wrendde75302014-03-26 17:24:15 -04002994 if (!mPeople.isEmpty()) {
2995 extras.putStringArray(EXTRA_PEOPLE, mPeople.toArray(new String[mPeople.size()]));
2996 }
Christoph Studer4600f9b2014-07-22 22:44:43 +02002997 // NOTE: If you're adding new extras also update restoreFromNotification().
2998 }
2999
3000
3001 /**
3002 * @hide
3003 */
3004 public static void stripForDelivery(Notification n) {
3005 if (!STRIP_AND_REBUILD) {
3006 return;
3007 }
3008
3009 String templateClass = n.extras.getString(EXTRA_TEMPLATE);
3010 // Only strip views for known Styles because we won't know how to
3011 // re-create them otherwise.
3012 boolean stripViews = TextUtils.isEmpty(templateClass) ||
3013 getNotificationStyleClass(templateClass) != null;
3014
3015 boolean isStripped = false;
3016
3017 if (n.largeIcon != null && n.extras.containsKey(EXTRA_LARGE_ICON)) {
3018 // TODO: Would like to check for equality here, but if the notification
3019 // has been cloned, we can't.
3020 n.largeIcon = null;
3021 n.extras.putBoolean(EXTRA_REBUILD_LARGE_ICON, true);
3022 isStripped = true;
3023 }
3024 // Get rid of unmodified BuilderRemoteViews.
3025
3026 if (stripViews &&
3027 n.contentView instanceof BuilderRemoteViews &&
3028 n.extras.getInt(EXTRA_REBUILD_CONTENT_VIEW_ACTION_COUNT, -1) ==
3029 n.contentView.getSequenceNumber()) {
3030 n.contentView = null;
3031 n.extras.putBoolean(EXTRA_REBUILD_CONTENT_VIEW, true);
3032 n.extras.remove(EXTRA_REBUILD_CONTENT_VIEW_ACTION_COUNT);
3033 isStripped = true;
3034 }
3035 if (stripViews &&
3036 n.bigContentView instanceof BuilderRemoteViews &&
3037 n.extras.getInt(EXTRA_REBUILD_BIG_CONTENT_VIEW_ACTION_COUNT, -1) ==
3038 n.bigContentView.getSequenceNumber()) {
3039 n.bigContentView = null;
3040 n.extras.putBoolean(EXTRA_REBUILD_BIG_CONTENT_VIEW, true);
3041 n.extras.remove(EXTRA_REBUILD_BIG_CONTENT_VIEW_ACTION_COUNT);
3042 isStripped = true;
3043 }
3044 if (stripViews &&
3045 n.headsUpContentView instanceof BuilderRemoteViews &&
3046 n.extras.getInt(EXTRA_REBUILD_HEADS_UP_CONTENT_VIEW_ACTION_COUNT, -1) ==
3047 n.headsUpContentView.getSequenceNumber()) {
3048 n.headsUpContentView = null;
3049 n.extras.putBoolean(EXTRA_REBUILD_HEADS_UP_CONTENT_VIEW, true);
3050 n.extras.remove(EXTRA_REBUILD_HEADS_UP_CONTENT_VIEW_ACTION_COUNT);
3051 isStripped = true;
3052 }
3053
3054 if (isStripped) {
3055 n.extras.putBoolean(EXTRA_NEEDS_REBUILD, true);
3056 }
3057 }
3058
3059 /**
3060 * @hide
3061 */
3062 public static Notification rebuild(Context context, Notification n) {
3063 Bundle extras = n.extras;
3064 if (!extras.getBoolean(EXTRA_NEEDS_REBUILD)) return n;
3065 extras.remove(EXTRA_NEEDS_REBUILD);
3066
3067 // Re-create notification context so we can access app resources.
3068 String packageName = extras.getString(EXTRA_REBUILD_CONTEXT_PACKAGE);
3069 Context builderContext;
3070 try {
3071 builderContext = context.createPackageContext(packageName,
3072 Context.CONTEXT_RESTRICTED);
3073 } catch (NameNotFoundException e) {
3074 Log.e(TAG, "Package name " + packageName + " not found");
3075 builderContext = context; // try with our context
3076 }
3077
3078 Builder b = new Builder(builderContext, n);
3079 return b.rebuild();
3080 }
3081
3082 /**
3083 * Rebuilds the notification passed in to the rebuild-constructor
3084 * {@link #Builder(Context, Notification)}.
3085 *
3086 * <p>
3087 * Throws IllegalStateException when invoked on a Builder that isn't in rebuild mode.
3088 *
3089 * @hide
3090 */
3091 private Notification rebuild() {
3092 if (mRebuildNotification == null) {
3093 throw new IllegalStateException("rebuild() only valid when in 'rebuild' mode.");
3094 }
3095
3096 Bundle extras = mRebuildNotification.extras;
3097
3098 if (extras.getBoolean(EXTRA_REBUILD_LARGE_ICON)) {
3099 mRebuildNotification.largeIcon = extras.getParcelable(EXTRA_LARGE_ICON);
3100 }
3101 extras.remove(EXTRA_REBUILD_LARGE_ICON);
3102
3103 if (extras.getBoolean(EXTRA_REBUILD_CONTENT_VIEW)) {
3104 setBuilderContentView(mRebuildNotification, makeContentView());
3105 if (mStyle != null) {
3106 mStyle.populateContentView(mRebuildNotification);
3107 }
3108 }
3109 extras.remove(EXTRA_REBUILD_CONTENT_VIEW);
3110
3111 if (extras.getBoolean(EXTRA_REBUILD_BIG_CONTENT_VIEW)) {
3112 setBuilderBigContentView(mRebuildNotification, makeBigContentView());
3113 if (mStyle != null) {
3114 mStyle.populateBigContentView(mRebuildNotification);
3115 }
3116 }
3117 extras.remove(EXTRA_REBUILD_BIG_CONTENT_VIEW);
3118
3119 if (extras.getBoolean(EXTRA_REBUILD_HEADS_UP_CONTENT_VIEW)) {
3120 setBuilderHeadsUpContentView(mRebuildNotification, makeHeadsUpContentView());
3121 if (mStyle != null) {
3122 mStyle.populateHeadsUpContentView(mRebuildNotification);
3123 }
3124 }
3125 extras.remove(EXTRA_REBUILD_HEADS_UP_CONTENT_VIEW);
3126
3127 return mRebuildNotification;
3128 }
3129
3130 private static Class<? extends Style> getNotificationStyleClass(String templateClass) {
3131 Class<? extends Style>[] classes = new Class[]{
3132 BigTextStyle.class, BigPictureStyle.class, InboxStyle.class, MediaStyle.class};
3133 for (Class<? extends Style> innerClass : classes) {
3134 if (templateClass.equals(innerClass.getName())) {
3135 return innerClass;
3136 }
3137 }
3138 return null;
3139 }
3140
3141 private void setBuilderContentView(Notification n, RemoteViews contentView) {
3142 n.contentView = contentView;
3143 if (contentView instanceof BuilderRemoteViews) {
3144 mRebuildBundle.putInt(Builder.EXTRA_REBUILD_CONTENT_VIEW_ACTION_COUNT,
3145 contentView.getSequenceNumber());
3146 }
3147 }
3148
3149 private void setBuilderBigContentView(Notification n, RemoteViews bigContentView) {
3150 n.bigContentView = bigContentView;
3151 if (bigContentView instanceof BuilderRemoteViews) {
3152 mRebuildBundle.putInt(Builder.EXTRA_REBUILD_BIG_CONTENT_VIEW_ACTION_COUNT,
3153 bigContentView.getSequenceNumber());
3154 }
3155 }
3156
3157 private void setBuilderHeadsUpContentView(Notification n,
3158 RemoteViews headsUpContentView) {
3159 n.headsUpContentView = headsUpContentView;
3160 if (headsUpContentView instanceof BuilderRemoteViews) {
3161 mRebuildBundle.putInt(Builder.EXTRA_REBUILD_HEADS_UP_CONTENT_VIEW_ACTION_COUNT,
3162 headsUpContentView.getSequenceNumber());
3163 }
3164 }
3165
3166 private void restoreFromNotification(Notification n) {
3167
3168 // Notification fields.
3169 mWhen = n.when;
3170 mSmallIcon = n.icon;
3171 mSmallIconLevel = n.iconLevel;
3172 mNumber = n.number;
3173
3174 mColor = n.color;
3175
3176 mContentView = n.contentView;
3177 mDeleteIntent = n.deleteIntent;
3178 mFullScreenIntent = n.fullScreenIntent;
3179 mTickerText = n.tickerText;
3180 mTickerView = n.tickerView;
3181 mLargeIcon = n.largeIcon;
3182 mSound = n.sound;
3183 mAudioStreamType = n.audioStreamType;
3184 mAudioAttributes = n.audioAttributes;
3185
3186 mVibrate = n.vibrate;
3187 mLedArgb = n.ledARGB;
3188 mLedOnMs = n.ledOnMS;
3189 mLedOffMs = n.ledOffMS;
3190 mDefaults = n.defaults;
3191 mFlags = n.flags;
3192
3193 mCategory = n.category;
3194 mGroupKey = n.mGroupKey;
3195 mSortKey = n.mSortKey;
3196 mPriority = n.priority;
3197 mActions.clear();
3198 if (n.actions != null) {
3199 Collections.addAll(mActions, n.actions);
3200 }
3201 mVisibility = n.visibility;
3202
3203 mPublicVersion = n.publicVersion;
3204
3205 // Extras.
3206 Bundle extras = n.extras;
Christoph Studer7ac80e62014-08-04 16:01:57 +02003207 mOriginatingUserId = extras.getInt(EXTRA_ORIGINATING_USERID);
Christoph Studer4600f9b2014-07-22 22:44:43 +02003208 mContentTitle = extras.getCharSequence(EXTRA_TITLE);
3209 mContentText = extras.getCharSequence(EXTRA_TEXT);
3210 mSubText = extras.getCharSequence(EXTRA_SUB_TEXT);
3211 mContentInfo = extras.getCharSequence(EXTRA_INFO_TEXT);
3212 mSmallIcon = extras.getInt(EXTRA_SMALL_ICON);
3213 mProgress = extras.getInt(EXTRA_PROGRESS);
3214 mProgressMax = extras.getInt(EXTRA_PROGRESS_MAX);
3215 mProgressIndeterminate = extras.getBoolean(EXTRA_PROGRESS_INDETERMINATE);
3216 mUseChronometer = extras.getBoolean(EXTRA_SHOW_CHRONOMETER);
3217 mShowWhen = extras.getBoolean(EXTRA_SHOW_WHEN);
3218 if (extras.containsKey(EXTRA_LARGE_ICON)) {
3219 mLargeIcon = extras.getParcelable(EXTRA_LARGE_ICON);
3220 }
3221 if (extras.containsKey(EXTRA_PEOPLE)) {
3222 mPeople.clear();
3223 Collections.addAll(mPeople, extras.getStringArray(EXTRA_PEOPLE));
3224 }
Daniel Sandlerf45564e2013-04-15 15:05:08 -04003225 }
3226
3227 /**
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003228 * @deprecated Use {@link #build()} instead.
3229 */
3230 @Deprecated
3231 public Notification getNotification() {
3232 return build();
3233 }
3234
3235 /**
3236 * Combine all of the options that have been set and return a new {@link Notification}
3237 * object.
3238 */
3239 public Notification build() {
Christoph Studer7ac80e62014-08-04 16:01:57 +02003240 mOriginatingUserId = mContext.getUserId();
Christoph Studer943aa672014-08-03 20:31:16 +02003241
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04003242 Notification n = buildUnstyled();
Daniel Sandlerf45564e2013-04-15 15:05:08 -04003243
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003244 if (mStyle != null) {
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04003245 n = mStyle.buildStyled(n);
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003246 }
Daniel Sandlerf45564e2013-04-15 15:05:08 -04003247
Christoph Studer4600f9b2014-07-22 22:44:43 +02003248 if (mExtras != null) {
3249 n.extras.putAll(mExtras);
3250 }
3251
3252 if (mRebuildBundle.size() > 0) {
3253 n.extras.putAll(mRebuildBundle);
3254 mRebuildBundle.clear();
3255 }
Daniel Sandlerf45564e2013-04-15 15:05:08 -04003256
Griff Hazen720042b2014-02-24 15:46:56 -08003257 populateExtras(n.extras);
Daniel Sandlerf45564e2013-04-15 15:05:08 -04003258 if (mStyle != null) {
3259 mStyle.addExtras(n.extras);
3260 }
3261
3262 return n;
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003263 }
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05003264
3265 /**
3266 * Apply this Builder to an existing {@link Notification} object.
3267 *
3268 * @hide
3269 */
3270 public Notification buildInto(Notification n) {
Daniel Sandler1a497d32013-04-18 14:52:45 -04003271 build().cloneInto(n, true);
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05003272 return n;
3273 }
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01003274
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01003275 private int getBaseLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07003276 return R.layout.notification_template_material_base;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01003277 }
3278
3279 private int getBigBaseLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07003280 return R.layout.notification_template_material_big_base;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01003281 }
3282
3283 private int getBigPictureLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07003284 return R.layout.notification_template_material_big_picture;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01003285 }
3286
3287 private int getBigTextLayoutResource() {
Jorim Jaggi445d3c02014-08-19 22:33:42 +02003288 return R.layout.notification_template_material_big_text;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01003289 }
3290
3291 private int getInboxLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07003292 return R.layout.notification_template_material_inbox;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01003293 }
3294
3295 private int getActionLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07003296 return R.layout.notification_material_action;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01003297 }
3298
3299 private int getActionTombstoneLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07003300 return R.layout.notification_material_action_tombstone;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01003301 }
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003302 }
3303
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003304 /**
3305 * An object that can apply a rich notification style to a {@link Notification.Builder}
3306 * object.
3307 */
Griff Hazendfcb0802014-02-11 12:00:00 -08003308 public static abstract class Style {
Chris Wrend6297db2012-05-03 16:20:13 -04003309 private CharSequence mBigContentTitle;
3310 private CharSequence mSummaryText = null;
Daniel Sandler619738c2012-06-07 16:33:08 -04003311 private boolean mSummaryTextSet = false;
Chris Wrend6297db2012-05-03 16:20:13 -04003312
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003313 protected Builder mBuilder;
3314
Chris Wrend6297db2012-05-03 16:20:13 -04003315 /**
3316 * Overrides ContentTitle in the big form of the template.
3317 * This defaults to the value passed to setContentTitle().
3318 */
3319 protected void internalSetBigContentTitle(CharSequence title) {
3320 mBigContentTitle = title;
3321 }
3322
3323 /**
3324 * Set the first line of text after the detail section in the big form of the template.
3325 */
3326 protected void internalSetSummaryText(CharSequence cs) {
3327 mSummaryText = cs;
Daniel Sandler619738c2012-06-07 16:33:08 -04003328 mSummaryTextSet = true;
Chris Wrend6297db2012-05-03 16:20:13 -04003329 }
3330
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003331 public void setBuilder(Builder builder) {
3332 if (mBuilder != builder) {
3333 mBuilder = builder;
Daniel Sandlerc08dea22012-06-28 08:35:24 -07003334 if (mBuilder != null) {
3335 mBuilder.setStyle(this);
3336 }
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003337 }
3338 }
3339
Chris Wrend6297db2012-05-03 16:20:13 -04003340 protected void checkBuilder() {
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003341 if (mBuilder == null) {
3342 throw new IllegalArgumentException("Style requires a valid Builder object");
3343 }
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003344 }
Chris Wrend6297db2012-05-03 16:20:13 -04003345
3346 protected RemoteViews getStandardView(int layoutId) {
3347 checkBuilder();
3348
Christoph Studer4600f9b2014-07-22 22:44:43 +02003349 // Nasty.
3350 CharSequence oldBuilderContentTitle = mBuilder.mContentTitle;
Chris Wrend6297db2012-05-03 16:20:13 -04003351 if (mBigContentTitle != null) {
3352 mBuilder.setContentTitle(mBigContentTitle);
3353 }
3354
Chris Wrend6297db2012-05-03 16:20:13 -04003355 RemoteViews contentView = mBuilder.applyStandardTemplateWithActions(layoutId);
3356
Christoph Studer4600f9b2014-07-22 22:44:43 +02003357 mBuilder.mContentTitle = oldBuilderContentTitle;
3358
Chris Wrend6297db2012-05-03 16:20:13 -04003359 if (mBigContentTitle != null && mBigContentTitle.equals("")) {
3360 contentView.setViewVisibility(R.id.line1, View.GONE);
Chris Wren67dc9a02012-05-16 01:03:20 -04003361 } else {
3362 contentView.setViewVisibility(R.id.line1, View.VISIBLE);
Chris Wrend6297db2012-05-03 16:20:13 -04003363 }
3364
Daniel Sandler619738c2012-06-07 16:33:08 -04003365 // The last line defaults to the subtext, but can be replaced by mSummaryText
3366 final CharSequence overflowText =
3367 mSummaryTextSet ? mSummaryText
3368 : mBuilder.mSubText;
3369 if (overflowText != null) {
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01003370 contentView.setTextViewText(R.id.text, mBuilder.processLegacyText(overflowText));
Daniel Sandler619738c2012-06-07 16:33:08 -04003371 contentView.setViewVisibility(R.id.overflow_divider, View.VISIBLE);
Daniel Sandler9f7936a2012-05-21 16:14:28 -04003372 contentView.setViewVisibility(R.id.line3, View.VISIBLE);
Daniel Sandler916ad912012-06-13 12:17:07 -04003373 } else {
Kenny Guy98193ea2014-07-24 19:54:37 +01003374 // Clear text in case we use the line to show the profile badge.
3375 contentView.setTextViewText(R.id.text, "");
Daniel Sandler916ad912012-06-13 12:17:07 -04003376 contentView.setViewVisibility(R.id.overflow_divider, View.GONE);
3377 contentView.setViewVisibility(R.id.line3, View.GONE);
Chris Wrend6297db2012-05-03 16:20:13 -04003378 }
3379
3380 return contentView;
3381 }
3382
Daniel Sandlerf45564e2013-04-15 15:05:08 -04003383 /**
Jorim Jaggi445d3c02014-08-19 22:33:42 +02003384 * Changes the padding of the first line such that the big and small content view have the
3385 * same top padding.
3386 *
3387 * @hide
3388 */
3389 protected void applyTopPadding(RemoteViews contentView) {
3390 int topPadding = Builder.calculateTopPadding(mBuilder.mContext,
3391 mBuilder.hasThreeLines(),
3392 mBuilder.mContext.getResources().getConfiguration().fontScale);
3393 contentView.setViewPadding(R.id.line1, 0, topPadding, 0, 0);
3394 }
3395
3396 /**
Daniel Sandlerf45564e2013-04-15 15:05:08 -04003397 * @hide
3398 */
3399 public void addExtras(Bundle extras) {
3400 if (mSummaryTextSet) {
3401 extras.putCharSequence(EXTRA_SUMMARY_TEXT, mSummaryText);
3402 }
3403 if (mBigContentTitle != null) {
3404 extras.putCharSequence(EXTRA_TITLE_BIG, mBigContentTitle);
3405 }
Chris Wren91ad5632013-06-05 15:05:57 -04003406 extras.putString(EXTRA_TEMPLATE, this.getClass().getName());
Daniel Sandlerf45564e2013-04-15 15:05:08 -04003407 }
3408
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04003409 /**
3410 * @hide
3411 */
Christoph Studer4600f9b2014-07-22 22:44:43 +02003412 protected void restoreFromExtras(Bundle extras) {
3413 if (extras.containsKey(EXTRA_SUMMARY_TEXT)) {
3414 mSummaryText = extras.getCharSequence(EXTRA_SUMMARY_TEXT);
3415 mSummaryTextSet = true;
3416 }
3417 if (extras.containsKey(EXTRA_TITLE_BIG)) {
3418 mBigContentTitle = extras.getCharSequence(EXTRA_TITLE_BIG);
3419 }
3420 }
3421
3422
3423 /**
3424 * @hide
3425 */
3426 public Notification buildStyled(Notification wip) {
3427 populateTickerView(wip);
3428 populateContentView(wip);
3429 populateBigContentView(wip);
3430 populateHeadsUpContentView(wip);
3431 return wip;
3432 }
3433
3434 // The following methods are split out so we can re-create notification partially.
3435 /**
3436 * @hide
3437 */
3438 protected void populateTickerView(Notification wip) {}
3439 /**
3440 * @hide
3441 */
3442 protected void populateContentView(Notification wip) {}
3443
3444 /**
3445 * @hide
3446 */
3447 protected void populateBigContentView(Notification wip) {}
3448
3449 /**
3450 * @hide
3451 */
3452 protected void populateHeadsUpContentView(Notification wip) {}
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04003453
3454 /**
3455 * Calls {@link android.app.Notification.Builder#build()} on the Builder this Style is
3456 * attached to.
3457 *
3458 * @return the fully constructed Notification.
3459 */
3460 public Notification build() {
3461 checkBuilder();
3462 return mBuilder.build();
3463 }
Joe Onorato46439ce2010-11-19 13:56:21 -08003464 }
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003465
3466 /**
Daniel Sandler4dfbe832012-04-11 14:51:46 -04003467 * Helper class for generating large-format notifications that include a large image attachment.
Joe Malin8d40d042012-11-05 11:36:40 -08003468 *
Robert Ly91c5ce32014-06-08 15:37:00 -07003469 * Here's how you'd set the <code>BigPictureStyle</code> on a notification:
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003470 * <pre class="prettyprint">
Robert Ly91c5ce32014-06-08 15:37:00 -07003471 * Notification notif = new Notification.Builder(mContext)
3472 * .setContentTitle(&quot;New photo from &quot; + sender.toString())
3473 * .setContentText(subject)
3474 * .setSmallIcon(R.drawable.new_post)
3475 * .setLargeIcon(aBitmap)
3476 * .setStyle(new Notification.BigPictureStyle()
3477 * .bigPicture(aBigBitmap))
3478 * .build();
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003479 * </pre>
Joe Malin8d40d042012-11-05 11:36:40 -08003480 *
Daniel Sandler4dfbe832012-04-11 14:51:46 -04003481 * @see Notification#bigContentView
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003482 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003483 public static class BigPictureStyle extends Style {
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003484 private Bitmap mPicture;
Chris Wren3745a3d2012-05-22 15:11:52 -04003485 private Bitmap mBigLargeIcon;
3486 private boolean mBigLargeIconSet = false;
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003487
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003488 public BigPictureStyle() {
3489 }
3490
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003491 public BigPictureStyle(Builder builder) {
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003492 setBuilder(builder);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003493 }
3494
Chris Wrend6297db2012-05-03 16:20:13 -04003495 /**
3496 * Overrides ContentTitle in the big form of the template.
3497 * This defaults to the value passed to setContentTitle().
3498 */
3499 public BigPictureStyle setBigContentTitle(CharSequence title) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04003500 internalSetBigContentTitle(safeCharSequence(title));
Chris Wrend6297db2012-05-03 16:20:13 -04003501 return this;
3502 }
3503
3504 /**
3505 * Set the first line of text after the detail section in the big form of the template.
3506 */
3507 public BigPictureStyle setSummaryText(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04003508 internalSetSummaryText(safeCharSequence(cs));
Chris Wrend6297db2012-05-03 16:20:13 -04003509 return this;
3510 }
3511
Chris Wren0bd664d2012-08-01 13:56:56 -04003512 /**
3513 * Provide the bitmap to be used as the payload for the BigPicture notification.
3514 */
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003515 public BigPictureStyle bigPicture(Bitmap b) {
3516 mPicture = b;
3517 return this;
3518 }
3519
Chris Wren3745a3d2012-05-22 15:11:52 -04003520 /**
Chris Wren3745a3d2012-05-22 15:11:52 -04003521 * Override the large icon when the big notification is shown.
3522 */
3523 public BigPictureStyle bigLargeIcon(Bitmap b) {
3524 mBigLargeIconSet = true;
3525 mBigLargeIcon = b;
3526 return this;
3527 }
3528
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003529 private RemoteViews makeBigContentView() {
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01003530 RemoteViews contentView = getStandardView(mBuilder.getBigPictureLayoutResource());
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003531
3532 contentView.setImageViewBitmap(R.id.big_picture, mPicture);
3533
Jorim Jaggi445d3c02014-08-19 22:33:42 +02003534 applyTopPadding(contentView);
3535
Kenny Guy98193ea2014-07-24 19:54:37 +01003536 boolean twoTextLines = mBuilder.mSubText != null && mBuilder.mContentText != null;
3537 mBuilder.addProfileBadge(contentView,
3538 twoTextLines ? R.id.profile_badge_line2 : R.id.profile_badge_line3);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003539 return contentView;
3540 }
3541
Daniel Sandlerf45564e2013-04-15 15:05:08 -04003542 /**
3543 * @hide
3544 */
3545 public void addExtras(Bundle extras) {
3546 super.addExtras(extras);
3547
3548 if (mBigLargeIconSet) {
3549 extras.putParcelable(EXTRA_LARGE_ICON_BIG, mBigLargeIcon);
3550 }
3551 extras.putParcelable(EXTRA_PICTURE, mPicture);
3552 }
3553
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04003554 /**
3555 * @hide
3556 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003557 @Override
Christoph Studer4600f9b2014-07-22 22:44:43 +02003558 protected void restoreFromExtras(Bundle extras) {
3559 super.restoreFromExtras(extras);
3560
3561 if (extras.containsKey(EXTRA_LARGE_ICON_BIG)) {
3562 mBigLargeIcon = extras.getParcelable(EXTRA_LARGE_ICON_BIG);
Chris Wren3745a3d2012-05-22 15:11:52 -04003563 }
Christoph Studer4600f9b2014-07-22 22:44:43 +02003564 mPicture = extras.getParcelable(EXTRA_PICTURE);
3565 }
3566
3567 /**
3568 * @hide
3569 */
3570 @Override
3571 public void populateBigContentView(Notification wip) {
3572 mBuilder.setBuilderBigContentView(wip, makeBigContentView());
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003573 }
3574 }
3575
3576 /**
Daniel Sandler4dfbe832012-04-11 14:51:46 -04003577 * Helper class for generating large-format notifications that include a lot of text.
Joe Malin8d40d042012-11-05 11:36:40 -08003578 *
Robert Ly91c5ce32014-06-08 15:37:00 -07003579 * Here's how you'd set the <code>BigTextStyle</code> on a notification:
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003580 * <pre class="prettyprint">
Robert Ly91c5ce32014-06-08 15:37:00 -07003581 * Notification notif = new Notification.Builder(mContext)
3582 * .setContentTitle(&quot;New mail from &quot; + sender.toString())
3583 * .setContentText(subject)
3584 * .setSmallIcon(R.drawable.new_mail)
3585 * .setLargeIcon(aBitmap)
3586 * .setStyle(new Notification.BigTextStyle()
3587 * .bigText(aVeryLongString))
3588 * .build();
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003589 * </pre>
Joe Malin8d40d042012-11-05 11:36:40 -08003590 *
Daniel Sandler4dfbe832012-04-11 14:51:46 -04003591 * @see Notification#bigContentView
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003592 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003593 public static class BigTextStyle extends Style {
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003594 private CharSequence mBigText;
3595
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003596 public BigTextStyle() {
3597 }
3598
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003599 public BigTextStyle(Builder builder) {
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003600 setBuilder(builder);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003601 }
3602
Chris Wrend6297db2012-05-03 16:20:13 -04003603 /**
3604 * Overrides ContentTitle in the big form of the template.
3605 * This defaults to the value passed to setContentTitle().
3606 */
3607 public BigTextStyle setBigContentTitle(CharSequence title) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04003608 internalSetBigContentTitle(safeCharSequence(title));
Chris Wrend6297db2012-05-03 16:20:13 -04003609 return this;
3610 }
3611
3612 /**
3613 * Set the first line of text after the detail section in the big form of the template.
3614 */
3615 public BigTextStyle setSummaryText(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04003616 internalSetSummaryText(safeCharSequence(cs));
Chris Wrend6297db2012-05-03 16:20:13 -04003617 return this;
3618 }
3619
Chris Wren0bd664d2012-08-01 13:56:56 -04003620 /**
3621 * Provide the longer text to be displayed in the big form of the
3622 * template in place of the content text.
3623 */
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003624 public BigTextStyle bigText(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04003625 mBigText = safeCharSequence(cs);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003626 return this;
3627 }
3628
Daniel Sandlerf45564e2013-04-15 15:05:08 -04003629 /**
3630 * @hide
3631 */
3632 public void addExtras(Bundle extras) {
3633 super.addExtras(extras);
3634
Christoph Studer4600f9b2014-07-22 22:44:43 +02003635 extras.putCharSequence(EXTRA_BIG_TEXT, mBigText);
3636 }
3637
3638 /**
3639 * @hide
3640 */
3641 @Override
3642 protected void restoreFromExtras(Bundle extras) {
3643 super.restoreFromExtras(extras);
3644
3645 mBigText = extras.getCharSequence(EXTRA_BIG_TEXT);
Daniel Sandlerf45564e2013-04-15 15:05:08 -04003646 }
3647
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003648 private RemoteViews makeBigContentView() {
Christoph Studer4600f9b2014-07-22 22:44:43 +02003649
3650 // Nasty
3651 CharSequence oldBuilderContentText = mBuilder.mContentText;
Daniel Sandler6387d2f2012-05-22 13:44:09 -04003652 mBuilder.mContentText = null;
Daniel Sandler916ad912012-06-13 12:17:07 -04003653
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01003654 RemoteViews contentView = getStandardView(mBuilder.getBigTextLayoutResource());
Joe Malin8d40d042012-11-05 11:36:40 -08003655
Christoph Studer4600f9b2014-07-22 22:44:43 +02003656 mBuilder.mContentText = oldBuilderContentText;
3657
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01003658 contentView.setTextViewText(R.id.big_text, mBuilder.processLegacyText(mBigText));
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003659 contentView.setViewVisibility(R.id.big_text, View.VISIBLE);
Chris Wren3c5f92432012-05-04 16:31:17 -04003660 contentView.setViewVisibility(R.id.text2, View.GONE);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003661
Jorim Jaggi445d3c02014-08-19 22:33:42 +02003662 applyTopPadding(contentView);
3663
Kenny Guy98193ea2014-07-24 19:54:37 +01003664 mBuilder.addProfileBadge(contentView, R.id.profile_badge_large_template);
3665
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003666 return contentView;
3667 }
3668
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04003669 /**
3670 * @hide
3671 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003672 @Override
Christoph Studer4600f9b2014-07-22 22:44:43 +02003673 public void populateBigContentView(Notification wip) {
3674 mBuilder.setBuilderBigContentView(wip, makeBigContentView());
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003675 }
3676 }
Daniel Sandler879c5e02012-04-17 16:46:51 -04003677
3678 /**
3679 * Helper class for generating large-format notifications that include a list of (up to 5) strings.
Joe Malin8d40d042012-11-05 11:36:40 -08003680 *
Robert Ly91c5ce32014-06-08 15:37:00 -07003681 * Here's how you'd set the <code>InboxStyle</code> on a notification:
Daniel Sandler879c5e02012-04-17 16:46:51 -04003682 * <pre class="prettyprint">
Robert Ly91c5ce32014-06-08 15:37:00 -07003683 * Notification notif = new Notification.Builder(mContext)
3684 * .setContentTitle(&quot;5 New mails from &quot; + sender.toString())
3685 * .setContentText(subject)
3686 * .setSmallIcon(R.drawable.new_mail)
3687 * .setLargeIcon(aBitmap)
3688 * .setStyle(new Notification.InboxStyle()
3689 * .addLine(str1)
3690 * .addLine(str2)
3691 * .setContentTitle(&quot;&quot;)
3692 * .setSummaryText(&quot;+3 more&quot;))
3693 * .build();
Daniel Sandler879c5e02012-04-17 16:46:51 -04003694 * </pre>
Joe Malin8d40d042012-11-05 11:36:40 -08003695 *
Daniel Sandler879c5e02012-04-17 16:46:51 -04003696 * @see Notification#bigContentView
3697 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003698 public static class InboxStyle extends Style {
Daniel Sandler879c5e02012-04-17 16:46:51 -04003699 private ArrayList<CharSequence> mTexts = new ArrayList<CharSequence>(5);
3700
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003701 public InboxStyle() {
3702 }
3703
Daniel Sandler879c5e02012-04-17 16:46:51 -04003704 public InboxStyle(Builder builder) {
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003705 setBuilder(builder);
Daniel Sandler879c5e02012-04-17 16:46:51 -04003706 }
3707
Chris Wrend6297db2012-05-03 16:20:13 -04003708 /**
3709 * Overrides ContentTitle in the big form of the template.
3710 * This defaults to the value passed to setContentTitle().
3711 */
3712 public InboxStyle setBigContentTitle(CharSequence title) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04003713 internalSetBigContentTitle(safeCharSequence(title));
Chris Wrend6297db2012-05-03 16:20:13 -04003714 return this;
3715 }
3716
3717 /**
3718 * Set the first line of text after the detail section in the big form of the template.
3719 */
3720 public InboxStyle setSummaryText(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04003721 internalSetSummaryText(safeCharSequence(cs));
Chris Wrend6297db2012-05-03 16:20:13 -04003722 return this;
3723 }
3724
Chris Wren0bd664d2012-08-01 13:56:56 -04003725 /**
3726 * Append a line to the digest section of the Inbox notification.
3727 */
Daniel Sandler879c5e02012-04-17 16:46:51 -04003728 public InboxStyle addLine(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04003729 mTexts.add(safeCharSequence(cs));
Daniel Sandler879c5e02012-04-17 16:46:51 -04003730 return this;
3731 }
3732
Daniel Sandlerf45564e2013-04-15 15:05:08 -04003733 /**
3734 * @hide
3735 */
3736 public void addExtras(Bundle extras) {
3737 super.addExtras(extras);
Christoph Studer4600f9b2014-07-22 22:44:43 +02003738
Daniel Sandlerf45564e2013-04-15 15:05:08 -04003739 CharSequence[] a = new CharSequence[mTexts.size()];
3740 extras.putCharSequenceArray(EXTRA_TEXT_LINES, mTexts.toArray(a));
3741 }
3742
Christoph Studer4600f9b2014-07-22 22:44:43 +02003743 /**
3744 * @hide
3745 */
3746 @Override
3747 protected void restoreFromExtras(Bundle extras) {
3748 super.restoreFromExtras(extras);
3749
3750 mTexts.clear();
3751 if (extras.containsKey(EXTRA_TEXT_LINES)) {
3752 Collections.addAll(mTexts, extras.getCharSequenceArray(EXTRA_TEXT_LINES));
3753 }
3754 }
3755
Daniel Sandler879c5e02012-04-17 16:46:51 -04003756 private RemoteViews makeBigContentView() {
Daniel Sandler619738c2012-06-07 16:33:08 -04003757 // Remove the content text so line3 disappears unless you have a summary
Christoph Studer4600f9b2014-07-22 22:44:43 +02003758
3759 // Nasty
3760 CharSequence oldBuilderContentText = mBuilder.mContentText;
Daniel Sandler619738c2012-06-07 16:33:08 -04003761 mBuilder.mContentText = null;
Christoph Studer4600f9b2014-07-22 22:44:43 +02003762
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01003763 RemoteViews contentView = getStandardView(mBuilder.getInboxLayoutResource());
Daniel Sandler619738c2012-06-07 16:33:08 -04003764
Christoph Studer4600f9b2014-07-22 22:44:43 +02003765 mBuilder.mContentText = oldBuilderContentText;
3766
Chris Wrend6297db2012-05-03 16:20:13 -04003767 contentView.setViewVisibility(R.id.text2, View.GONE);
Daniel Sandler879c5e02012-04-17 16:46:51 -04003768
Chris Wrend6297db2012-05-03 16:20:13 -04003769 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 -04003770 R.id.inbox_text4, R.id.inbox_text5, R.id.inbox_text6};
Chris Wrend6297db2012-05-03 16:20:13 -04003771
Chris Wren4ed80d52012-05-17 09:30:03 -04003772 // Make sure all rows are gone in case we reuse a view.
3773 for (int rowId : rowIds) {
3774 contentView.setViewVisibility(rowId, View.GONE);
3775 }
3776
Jorim Jaggi445d3c02014-08-19 22:33:42 +02003777 final boolean largeText =
3778 mBuilder.mContext.getResources().getConfiguration().fontScale > 1f;
3779 final float subTextSize = mBuilder.mContext.getResources().getDimensionPixelSize(
3780 R.dimen.notification_subtext_size);
Daniel Sandler879c5e02012-04-17 16:46:51 -04003781 int i=0;
3782 while (i < mTexts.size() && i < rowIds.length) {
3783 CharSequence str = mTexts.get(i);
3784 if (str != null && !str.equals("")) {
3785 contentView.setViewVisibility(rowIds[i], View.VISIBLE);
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01003786 contentView.setTextViewText(rowIds[i], mBuilder.processLegacyText(str));
Jorim Jaggi445d3c02014-08-19 22:33:42 +02003787 if (largeText) {
3788 contentView.setTextViewTextSize(rowIds[i], TypedValue.COMPLEX_UNIT_PX,
3789 subTextSize);
3790 }
Daniel Sandler879c5e02012-04-17 16:46:51 -04003791 }
3792 i++;
3793 }
3794
Chris Wren683ab002012-09-20 10:35:54 -04003795 contentView.setViewVisibility(R.id.inbox_end_pad,
3796 mTexts.size() > 0 ? View.VISIBLE : View.GONE);
3797
3798 contentView.setViewVisibility(R.id.inbox_more,
3799 mTexts.size() > rowIds.length ? View.VISIBLE : View.GONE);
Chris Wren29bb6d92012-05-17 18:09:42 -04003800
Jorim Jaggi445d3c02014-08-19 22:33:42 +02003801 applyTopPadding(contentView);
3802
Kenny Guy98193ea2014-07-24 19:54:37 +01003803 mBuilder.addProfileBadge(contentView, R.id.profile_badge_large_template);
3804
Daniel Sandler879c5e02012-04-17 16:46:51 -04003805 return contentView;
3806 }
3807
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04003808 /**
3809 * @hide
3810 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003811 @Override
Christoph Studer4600f9b2014-07-22 22:44:43 +02003812 public void populateBigContentView(Notification wip) {
3813 mBuilder.setBuilderBigContentView(wip, makeBigContentView());
Daniel Sandler879c5e02012-04-17 16:46:51 -04003814 }
3815 }
Dan Sandler842dd772014-05-15 09:36:47 -04003816
3817 /**
3818 * Notification style for media playback notifications.
3819 *
3820 * In the expanded form, {@link Notification#bigContentView}, up to 5
3821 * {@link Notification.Action}s specified with
3822 * {@link Notification.Builder#addAction(int, CharSequence, PendingIntent) addAction} will be
3823 * shown as icon-only pushbuttons, suitable for transport controls. The Bitmap given to
3824 * {@link Notification.Builder#setLargeIcon(android.graphics.Bitmap) setLargeIcon()} will be
3825 * treated as album artwork.
3826 *
3827 * Unlike the other styles provided here, MediaStyle can also modify the standard-size
3828 * {@link Notification#contentView}; by providing action indices to
3829 * {@link #setShowActionsInCompactView(int...)} you can promote up to 2 actions to be displayed
3830 * in the standard view alongside the usual content.
3831 *
Bryan Mawhinney6be8de32014-07-18 10:35:12 +01003832 * Notifications created with MediaStyle will have their category set to
3833 * {@link Notification#CATEGORY_TRANSPORT CATEGORY_TRANSPORT} unless you set a different
3834 * category using {@link Notification.Builder#setCategory(String) setCategory()}.
3835 *
Jeff Browndba34ba2014-06-24 20:46:03 -07003836 * Finally, if you attach a {@link android.media.session.MediaSession.Token} using
3837 * {@link android.app.Notification.MediaStyle#setMediaSession(MediaSession.Token)},
Dan Sandler842dd772014-05-15 09:36:47 -04003838 * the System UI can identify this as a notification representing an active media session
3839 * and respond accordingly (by showing album artwork in the lockscreen, for example).
3840 *
3841 * To use this style with your Notification, feed it to
3842 * {@link Notification.Builder#setStyle(android.app.Notification.Style)} like so:
3843 * <pre class="prettyprint">
3844 * Notification noti = new Notification.Builder()
3845 * .setSmallIcon(R.drawable.ic_stat_player)
3846 * .setContentTitle(&quot;Track title&quot;) // these three lines are optional
3847 * .setContentText(&quot;Artist - Album&quot;) // if you use
Bryan Mawhinney6be8de32014-07-18 10:35:12 +01003848 * .setLargeIcon(albumArtBitmap)) // setMediaSession(token)
3849 * .setStyle(<b>new Notification.MediaStyle()</b>
3850 * .setMediaSession(mySession))
Dan Sandler842dd772014-05-15 09:36:47 -04003851 * .build();
3852 * </pre>
3853 *
3854 * @see Notification#bigContentView
3855 */
3856 public static class MediaStyle extends Style {
3857 static final int MAX_MEDIA_BUTTONS_IN_COMPACT = 2;
3858 static final int MAX_MEDIA_BUTTONS = 5;
3859
3860 private int[] mActionsToShowInCompact = null;
Jeff Browndba34ba2014-06-24 20:46:03 -07003861 private MediaSession.Token mToken;
Dan Sandler842dd772014-05-15 09:36:47 -04003862
3863 public MediaStyle() {
3864 }
3865
3866 public MediaStyle(Builder builder) {
3867 setBuilder(builder);
3868 }
3869
3870 /**
3871 * Request up to 2 actions (by index in the order of addition) to be shown in the compact
3872 * notification view.
3873 */
3874 public MediaStyle setShowActionsInCompactView(int...actions) {
3875 mActionsToShowInCompact = actions;
3876 return this;
3877 }
3878
3879 /**
Jeff Browndba34ba2014-06-24 20:46:03 -07003880 * Attach a {@link android.media.session.MediaSession.Token} to this Notification
3881 * to provide additional playback information and control to the SystemUI.
Dan Sandler842dd772014-05-15 09:36:47 -04003882 */
Jeff Browndba34ba2014-06-24 20:46:03 -07003883 public MediaStyle setMediaSession(MediaSession.Token token) {
Dan Sandler842dd772014-05-15 09:36:47 -04003884 mToken = token;
3885 return this;
3886 }
3887
Christoph Studer4600f9b2014-07-22 22:44:43 +02003888 /**
3889 * @hide
3890 */
Dan Sandler842dd772014-05-15 09:36:47 -04003891 @Override
3892 public Notification buildStyled(Notification wip) {
Christoph Studer4600f9b2014-07-22 22:44:43 +02003893 super.buildStyled(wip);
Bryan Mawhinney6be8de32014-07-18 10:35:12 +01003894 if (wip.category == null) {
3895 wip.category = Notification.CATEGORY_TRANSPORT;
3896 }
Dan Sandler842dd772014-05-15 09:36:47 -04003897 return wip;
3898 }
3899
Christoph Studer4600f9b2014-07-22 22:44:43 +02003900 /**
3901 * @hide
3902 */
3903 @Override
3904 public void populateContentView(Notification wip) {
3905 mBuilder.setBuilderContentView(wip, makeMediaContentView());
3906 }
3907
3908 /**
3909 * @hide
3910 */
3911 @Override
3912 public void populateBigContentView(Notification wip) {
3913 mBuilder.setBuilderBigContentView(wip, makeMediaBigContentView());
3914 }
3915
Dan Sandler842dd772014-05-15 09:36:47 -04003916 /** @hide */
3917 @Override
3918 public void addExtras(Bundle extras) {
3919 super.addExtras(extras);
3920
3921 if (mToken != null) {
3922 extras.putParcelable(EXTRA_MEDIA_SESSION, mToken);
3923 }
Bryan Mawhinneye191f902014-07-22 12:50:09 +01003924 if (mActionsToShowInCompact != null) {
3925 extras.putIntArray(EXTRA_COMPACT_ACTIONS, mActionsToShowInCompact);
3926 }
Dan Sandler842dd772014-05-15 09:36:47 -04003927 }
3928
Christoph Studer4600f9b2014-07-22 22:44:43 +02003929 /**
3930 * @hide
3931 */
3932 @Override
3933 protected void restoreFromExtras(Bundle extras) {
3934 super.restoreFromExtras(extras);
3935
3936 if (extras.containsKey(EXTRA_MEDIA_SESSION)) {
3937 mToken = extras.getParcelable(EXTRA_MEDIA_SESSION);
3938 }
3939 if (extras.containsKey(EXTRA_COMPACT_ACTIONS)) {
3940 mActionsToShowInCompact = extras.getIntArray(EXTRA_COMPACT_ACTIONS);
3941 }
3942 }
3943
Dan Sandler842dd772014-05-15 09:36:47 -04003944 private RemoteViews generateMediaActionButton(Action action) {
3945 final boolean tombstone = (action.actionIntent == null);
3946 RemoteViews button = new RemoteViews(mBuilder.mContext.getPackageName(),
Alan Viverette3cb07a462014-06-06 14:19:53 -07003947 R.layout.notification_material_media_action);
Dan Sandler842dd772014-05-15 09:36:47 -04003948 button.setImageViewResource(R.id.action0, action.icon);
3949 if (!tombstone) {
3950 button.setOnClickPendingIntent(R.id.action0, action.actionIntent);
3951 }
3952 button.setContentDescription(R.id.action0, action.title);
3953 return button;
3954 }
3955
3956 private RemoteViews makeMediaContentView() {
3957 RemoteViews view = mBuilder.applyStandardTemplate(
Jorim Jaggi445d3c02014-08-19 22:33:42 +02003958 R.layout.notification_template_material_media);
Dan Sandler842dd772014-05-15 09:36:47 -04003959
3960 final int numActions = mBuilder.mActions.size();
3961 final int N = mActionsToShowInCompact == null
3962 ? 0
3963 : Math.min(mActionsToShowInCompact.length, MAX_MEDIA_BUTTONS_IN_COMPACT);
3964 if (N > 0) {
3965 view.removeAllViews(R.id.actions);
3966 for (int i = 0; i < N; i++) {
3967 if (i >= numActions) {
3968 throw new IllegalArgumentException(String.format(
3969 "setShowActionsInCompactView: action %d out of bounds (max %d)",
3970 i, numActions - 1));
3971 }
3972
3973 final Action action = mBuilder.mActions.get(mActionsToShowInCompact[i]);
3974 final RemoteViews button = generateMediaActionButton(action);
3975 view.addView(R.id.actions, button);
3976 }
3977 }
3978 return view;
3979 }
3980
3981 private RemoteViews makeMediaBigContentView() {
3982 RemoteViews big = mBuilder.applyStandardTemplate(
Jorim Jaggi445d3c02014-08-19 22:33:42 +02003983 R.layout.notification_template_material_big_media);
Dan Sandler842dd772014-05-15 09:36:47 -04003984
3985 final int N = Math.min(mBuilder.mActions.size(), MAX_MEDIA_BUTTONS);
3986 if (N > 0) {
3987 big.removeAllViews(R.id.actions);
3988 for (int i=0; i<N; i++) {
3989 final RemoteViews button = generateMediaActionButton(mBuilder.mActions.get(i));
3990 big.addView(R.id.actions, button);
3991 }
3992 }
3993 return big;
3994 }
3995 }
Griff Hazen61a9e862014-05-22 16:05:19 -07003996
Christoph Studer4600f9b2014-07-22 22:44:43 +02003997 // When adding a new Style subclass here, don't forget to update
3998 // Builder.getNotificationStyleClass.
3999
Griff Hazen61a9e862014-05-22 16:05:19 -07004000 /**
4001 * Extender interface for use with {@link Builder#extend}. Extenders may be used to add
4002 * metadata or change options on a notification builder.
4003 */
4004 public interface Extender {
4005 /**
4006 * Apply this extender to a notification builder.
4007 * @param builder the builder to be modified.
4008 * @return the build object for chaining.
4009 */
4010 public Builder extend(Builder builder);
4011 }
4012
4013 /**
4014 * Helper class to add wearable extensions to notifications.
4015 * <p class="note"> See
4016 * <a href="{@docRoot}wear/notifications/creating.html">Creating Notifications
4017 * for Android Wear</a> for more information on how to use this class.
4018 * <p>
4019 * To create a notification with wearable extensions:
4020 * <ol>
4021 * <li>Create a {@link android.app.Notification.Builder}, setting any desired
4022 * properties.
4023 * <li>Create a {@link android.app.Notification.WearableExtender}.
4024 * <li>Set wearable-specific properties using the
4025 * {@code add} and {@code set} methods of {@link android.app.Notification.WearableExtender}.
4026 * <li>Call {@link android.app.Notification.Builder#extend} to apply the extensions to a
4027 * notification.
4028 * <li>Post the notification to the notification system with the
4029 * {@code NotificationManager.notify(...)} methods.
4030 * </ol>
4031 *
4032 * <pre class="prettyprint">
4033 * Notification notif = new Notification.Builder(mContext)
4034 * .setContentTitle(&quot;New mail from &quot; + sender.toString())
4035 * .setContentText(subject)
4036 * .setSmallIcon(R.drawable.new_mail)
4037 * .extend(new Notification.WearableExtender()
4038 * .setContentIcon(R.drawable.new_mail))
4039 * .build();
4040 * NotificationManager notificationManger =
4041 * (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
4042 * notificationManger.notify(0, notif);</pre>
4043 *
4044 * <p>Wearable extensions can be accessed on an existing notification by using the
4045 * {@code WearableExtender(Notification)} constructor,
4046 * and then using the {@code get} methods to access values.
4047 *
4048 * <pre class="prettyprint">
4049 * Notification.WearableExtender wearableExtender = new Notification.WearableExtender(
4050 * notification);
Griff Hazen14f57992014-05-26 09:07:14 -07004051 * List&lt;Notification&gt; pages = wearableExtender.getPages();</pre>
Griff Hazen61a9e862014-05-22 16:05:19 -07004052 */
4053 public static final class WearableExtender implements Extender {
4054 /**
4055 * Sentinel value for an action index that is unset.
4056 */
4057 public static final int UNSET_ACTION_INDEX = -1;
4058
4059 /**
4060 * Size value for use with {@link #setCustomSizePreset} to show this notification with
4061 * default sizing.
4062 * <p>For custom display notifications created using {@link #setDisplayIntent},
4063 * the default is {@link #SIZE_LARGE}. All other notifications size automatically based
4064 * on their content.
4065 */
4066 public static final int SIZE_DEFAULT = 0;
4067
4068 /**
4069 * Size value for use with {@link #setCustomSizePreset} to show this notification
4070 * with an extra small size.
4071 * <p>This value is only applicable for custom display notifications created using
4072 * {@link #setDisplayIntent}.
4073 */
4074 public static final int SIZE_XSMALL = 1;
4075
4076 /**
4077 * Size value for use with {@link #setCustomSizePreset} to show this notification
4078 * with a small size.
4079 * <p>This value is only applicable for custom display notifications created using
4080 * {@link #setDisplayIntent}.
4081 */
4082 public static final int SIZE_SMALL = 2;
4083
4084 /**
4085 * Size value for use with {@link #setCustomSizePreset} to show this notification
4086 * with a medium size.
4087 * <p>This value is only applicable for custom display notifications created using
4088 * {@link #setDisplayIntent}.
4089 */
4090 public static final int SIZE_MEDIUM = 3;
4091
4092 /**
4093 * Size value for use with {@link #setCustomSizePreset} to show this notification
4094 * with a large size.
4095 * <p>This value is only applicable for custom display notifications created using
4096 * {@link #setDisplayIntent}.
4097 */
4098 public static final int SIZE_LARGE = 4;
4099
Griff Hazend5f11f92014-05-27 15:40:09 -07004100 /**
4101 * Size value for use with {@link #setCustomSizePreset} to show this notification
4102 * full screen.
4103 * <p>This value is only applicable for custom display notifications created using
4104 * {@link #setDisplayIntent}.
4105 */
4106 public static final int SIZE_FULL_SCREEN = 5;
4107
Griff Hazen61a9e862014-05-22 16:05:19 -07004108 /** Notification extra which contains wearable extensions */
4109 private static final String EXTRA_WEARABLE_EXTENSIONS = "android.wearable.EXTENSIONS";
4110
4111 // Keys within EXTRA_WEARABLE_OPTIONS for wearable options.
4112 private static final String KEY_ACTIONS = "actions";
4113 private static final String KEY_FLAGS = "flags";
4114 private static final String KEY_DISPLAY_INTENT = "displayIntent";
4115 private static final String KEY_PAGES = "pages";
4116 private static final String KEY_BACKGROUND = "background";
4117 private static final String KEY_CONTENT_ICON = "contentIcon";
4118 private static final String KEY_CONTENT_ICON_GRAVITY = "contentIconGravity";
4119 private static final String KEY_CONTENT_ACTION_INDEX = "contentActionIndex";
4120 private static final String KEY_CUSTOM_SIZE_PRESET = "customSizePreset";
4121 private static final String KEY_CUSTOM_CONTENT_HEIGHT = "customContentHeight";
4122 private static final String KEY_GRAVITY = "gravity";
4123
4124 // Flags bitwise-ored to mFlags
4125 private static final int FLAG_CONTENT_INTENT_AVAILABLE_OFFLINE = 0x1;
4126 private static final int FLAG_HINT_HIDE_ICON = 1 << 1;
4127 private static final int FLAG_HINT_SHOW_BACKGROUND_ONLY = 1 << 2;
4128 private static final int FLAG_START_SCROLL_BOTTOM = 1 << 3;
4129
4130 // Default value for flags integer
4131 private static final int DEFAULT_FLAGS = FLAG_CONTENT_INTENT_AVAILABLE_OFFLINE;
4132
4133 private static final int DEFAULT_CONTENT_ICON_GRAVITY = Gravity.END;
4134 private static final int DEFAULT_GRAVITY = Gravity.BOTTOM;
4135
4136 private ArrayList<Action> mActions = new ArrayList<Action>();
4137 private int mFlags = DEFAULT_FLAGS;
4138 private PendingIntent mDisplayIntent;
4139 private ArrayList<Notification> mPages = new ArrayList<Notification>();
4140 private Bitmap mBackground;
4141 private int mContentIcon;
4142 private int mContentIconGravity = DEFAULT_CONTENT_ICON_GRAVITY;
4143 private int mContentActionIndex = UNSET_ACTION_INDEX;
4144 private int mCustomSizePreset = SIZE_DEFAULT;
4145 private int mCustomContentHeight;
4146 private int mGravity = DEFAULT_GRAVITY;
4147
4148 /**
4149 * Create a {@link android.app.Notification.WearableExtender} with default
4150 * options.
4151 */
4152 public WearableExtender() {
4153 }
4154
4155 public WearableExtender(Notification notif) {
4156 Bundle wearableBundle = notif.extras.getBundle(EXTRA_WEARABLE_EXTENSIONS);
4157 if (wearableBundle != null) {
4158 List<Action> actions = wearableBundle.getParcelableArrayList(KEY_ACTIONS);
4159 if (actions != null) {
4160 mActions.addAll(actions);
4161 }
4162
4163 mFlags = wearableBundle.getInt(KEY_FLAGS, DEFAULT_FLAGS);
4164 mDisplayIntent = wearableBundle.getParcelable(KEY_DISPLAY_INTENT);
4165
4166 Notification[] pages = getNotificationArrayFromBundle(
4167 wearableBundle, KEY_PAGES);
4168 if (pages != null) {
4169 Collections.addAll(mPages, pages);
4170 }
4171
4172 mBackground = wearableBundle.getParcelable(KEY_BACKGROUND);
4173 mContentIcon = wearableBundle.getInt(KEY_CONTENT_ICON);
4174 mContentIconGravity = wearableBundle.getInt(KEY_CONTENT_ICON_GRAVITY,
4175 DEFAULT_CONTENT_ICON_GRAVITY);
4176 mContentActionIndex = wearableBundle.getInt(KEY_CONTENT_ACTION_INDEX,
4177 UNSET_ACTION_INDEX);
4178 mCustomSizePreset = wearableBundle.getInt(KEY_CUSTOM_SIZE_PRESET,
4179 SIZE_DEFAULT);
4180 mCustomContentHeight = wearableBundle.getInt(KEY_CUSTOM_CONTENT_HEIGHT);
4181 mGravity = wearableBundle.getInt(KEY_GRAVITY, DEFAULT_GRAVITY);
4182 }
4183 }
4184
4185 /**
4186 * Apply wearable extensions to a notification that is being built. This is typically
4187 * called by the {@link android.app.Notification.Builder#extend} method of
4188 * {@link android.app.Notification.Builder}.
4189 */
4190 @Override
4191 public Notification.Builder extend(Notification.Builder builder) {
4192 Bundle wearableBundle = new Bundle();
4193
4194 if (!mActions.isEmpty()) {
4195 wearableBundle.putParcelableArrayList(KEY_ACTIONS, mActions);
4196 }
4197 if (mFlags != DEFAULT_FLAGS) {
4198 wearableBundle.putInt(KEY_FLAGS, mFlags);
4199 }
4200 if (mDisplayIntent != null) {
4201 wearableBundle.putParcelable(KEY_DISPLAY_INTENT, mDisplayIntent);
4202 }
4203 if (!mPages.isEmpty()) {
4204 wearableBundle.putParcelableArray(KEY_PAGES, mPages.toArray(
4205 new Notification[mPages.size()]));
4206 }
4207 if (mBackground != null) {
4208 wearableBundle.putParcelable(KEY_BACKGROUND, mBackground);
4209 }
4210 if (mContentIcon != 0) {
4211 wearableBundle.putInt(KEY_CONTENT_ICON, mContentIcon);
4212 }
4213 if (mContentIconGravity != DEFAULT_CONTENT_ICON_GRAVITY) {
4214 wearableBundle.putInt(KEY_CONTENT_ICON_GRAVITY, mContentIconGravity);
4215 }
4216 if (mContentActionIndex != UNSET_ACTION_INDEX) {
4217 wearableBundle.putInt(KEY_CONTENT_ACTION_INDEX,
4218 mContentActionIndex);
4219 }
4220 if (mCustomSizePreset != SIZE_DEFAULT) {
4221 wearableBundle.putInt(KEY_CUSTOM_SIZE_PRESET, mCustomSizePreset);
4222 }
4223 if (mCustomContentHeight != 0) {
4224 wearableBundle.putInt(KEY_CUSTOM_CONTENT_HEIGHT, mCustomContentHeight);
4225 }
4226 if (mGravity != DEFAULT_GRAVITY) {
4227 wearableBundle.putInt(KEY_GRAVITY, mGravity);
4228 }
4229
4230 builder.getExtras().putBundle(EXTRA_WEARABLE_EXTENSIONS, wearableBundle);
4231 return builder;
4232 }
4233
4234 @Override
4235 public WearableExtender clone() {
4236 WearableExtender that = new WearableExtender();
4237 that.mActions = new ArrayList<Action>(this.mActions);
4238 that.mFlags = this.mFlags;
4239 that.mDisplayIntent = this.mDisplayIntent;
4240 that.mPages = new ArrayList<Notification>(this.mPages);
4241 that.mBackground = this.mBackground;
4242 that.mContentIcon = this.mContentIcon;
4243 that.mContentIconGravity = this.mContentIconGravity;
4244 that.mContentActionIndex = this.mContentActionIndex;
4245 that.mCustomSizePreset = this.mCustomSizePreset;
4246 that.mCustomContentHeight = this.mCustomContentHeight;
4247 that.mGravity = this.mGravity;
4248 return that;
4249 }
4250
4251 /**
4252 * Add a wearable action to this notification.
4253 *
4254 * <p>When wearable actions are added using this method, the set of actions that
4255 * show on a wearable device splits from devices that only show actions added
4256 * using {@link android.app.Notification.Builder#addAction}. This allows for customization
4257 * of which actions display on different devices.
4258 *
4259 * @param action the action to add to this notification
4260 * @return this object for method chaining
4261 * @see android.app.Notification.Action
4262 */
4263 public WearableExtender addAction(Action action) {
4264 mActions.add(action);
4265 return this;
4266 }
4267
4268 /**
4269 * Adds wearable actions to this notification.
4270 *
4271 * <p>When wearable actions are added using this method, the set of actions that
4272 * show on a wearable device splits from devices that only show actions added
4273 * using {@link android.app.Notification.Builder#addAction}. This allows for customization
4274 * of which actions display on different devices.
4275 *
4276 * @param actions the actions to add to this notification
4277 * @return this object for method chaining
4278 * @see android.app.Notification.Action
4279 */
4280 public WearableExtender addActions(List<Action> actions) {
4281 mActions.addAll(actions);
4282 return this;
4283 }
4284
4285 /**
4286 * Clear all wearable actions present on this builder.
4287 * @return this object for method chaining.
4288 * @see #addAction
4289 */
4290 public WearableExtender clearActions() {
4291 mActions.clear();
4292 return this;
4293 }
4294
4295 /**
4296 * Get the wearable actions present on this notification.
4297 */
4298 public List<Action> getActions() {
4299 return mActions;
4300 }
4301
4302 /**
4303 * Set an intent to launch inside of an activity view when displaying
Griff Hazen14f57992014-05-26 09:07:14 -07004304 * this notification. The {@link PendingIntent} provided should be for an activity.
4305 *
4306 * <pre class="prettyprint">
4307 * Intent displayIntent = new Intent(context, MyDisplayActivity.class);
4308 * PendingIntent displayPendingIntent = PendingIntent.getActivity(context,
4309 * 0, displayIntent, PendingIntent.FLAG_UPDATE_CURRENT);
4310 * Notification notif = new Notification.Builder(context)
4311 * .extend(new Notification.WearableExtender()
4312 * .setDisplayIntent(displayPendingIntent)
4313 * .setCustomSizePreset(Notification.WearableExtender.SIZE_MEDIUM))
4314 * .build();</pre>
4315 *
4316 * <p>The activity to launch needs to allow embedding, must be exported, and
Griff Hazen831ca9d2014-06-17 00:38:38 -07004317 * should have an empty task affinity. It is also recommended to use the device
4318 * default light theme.
Griff Hazen14f57992014-05-26 09:07:14 -07004319 *
4320 * <p>Example AndroidManifest.xml entry:
4321 * <pre class="prettyprint">
4322 * &lt;activity android:name=&quot;com.example.MyDisplayActivity&quot;
4323 * android:exported=&quot;true&quot;
4324 * android:allowEmbedded=&quot;true&quot;
Griff Hazen831ca9d2014-06-17 00:38:38 -07004325 * android:taskAffinity=&quot;&quot;
4326 * android:theme=&quot;@android:style/Theme.DeviceDefault.Light&quot; /&gt;</pre>
Griff Hazen61a9e862014-05-22 16:05:19 -07004327 *
4328 * @param intent the {@link PendingIntent} for an activity
4329 * @return this object for method chaining
4330 * @see android.app.Notification.WearableExtender#getDisplayIntent
4331 */
4332 public WearableExtender setDisplayIntent(PendingIntent intent) {
4333 mDisplayIntent = intent;
4334 return this;
4335 }
4336
4337 /**
4338 * Get the intent to launch inside of an activity view when displaying this
4339 * notification. This {@code PendingIntent} should be for an activity.
4340 */
4341 public PendingIntent getDisplayIntent() {
4342 return mDisplayIntent;
4343 }
4344
4345 /**
4346 * Add an additional page of content to display with this notification. The current
4347 * notification forms the first page, and pages added using this function form
4348 * subsequent pages. This field can be used to separate a notification into multiple
4349 * sections.
4350 *
4351 * @param page the notification to add as another page
4352 * @return this object for method chaining
4353 * @see android.app.Notification.WearableExtender#getPages
4354 */
4355 public WearableExtender addPage(Notification page) {
4356 mPages.add(page);
4357 return this;
4358 }
4359
4360 /**
4361 * Add additional pages of content to display with this notification. The current
4362 * notification forms the first page, and pages added using this function form
4363 * subsequent pages. This field can be used to separate a notification into multiple
4364 * sections.
4365 *
4366 * @param pages a list of notifications
4367 * @return this object for method chaining
4368 * @see android.app.Notification.WearableExtender#getPages
4369 */
4370 public WearableExtender addPages(List<Notification> pages) {
4371 mPages.addAll(pages);
4372 return this;
4373 }
4374
4375 /**
4376 * Clear all additional pages present on this builder.
4377 * @return this object for method chaining.
4378 * @see #addPage
4379 */
4380 public WearableExtender clearPages() {
4381 mPages.clear();
4382 return this;
4383 }
4384
4385 /**
4386 * Get the array of additional pages of content for displaying this notification. The
4387 * current notification forms the first page, and elements within this array form
4388 * subsequent pages. This field can be used to separate a notification into multiple
4389 * sections.
4390 * @return the pages for this notification
4391 */
4392 public List<Notification> getPages() {
4393 return mPages;
4394 }
4395
4396 /**
4397 * Set a background image to be displayed behind the notification content.
4398 * Contrary to the {@link android.app.Notification.BigPictureStyle}, this background
4399 * will work with any notification style.
4400 *
4401 * @param background the background bitmap
4402 * @return this object for method chaining
4403 * @see android.app.Notification.WearableExtender#getBackground
4404 */
4405 public WearableExtender setBackground(Bitmap background) {
4406 mBackground = background;
4407 return this;
4408 }
4409
4410 /**
4411 * Get a background image to be displayed behind the notification content.
4412 * Contrary to the {@link android.app.Notification.BigPictureStyle}, this background
4413 * will work with any notification style.
4414 *
4415 * @return the background image
4416 * @see android.app.Notification.WearableExtender#setBackground
4417 */
4418 public Bitmap getBackground() {
4419 return mBackground;
4420 }
4421
4422 /**
4423 * Set an icon that goes with the content of this notification.
4424 */
4425 public WearableExtender setContentIcon(int icon) {
4426 mContentIcon = icon;
4427 return this;
4428 }
4429
4430 /**
4431 * Get an icon that goes with the content of this notification.
4432 */
4433 public int getContentIcon() {
4434 return mContentIcon;
4435 }
4436
4437 /**
4438 * Set the gravity that the content icon should have within the notification display.
4439 * Supported values include {@link android.view.Gravity#START} and
4440 * {@link android.view.Gravity#END}. The default value is {@link android.view.Gravity#END}.
4441 * @see #setContentIcon
4442 */
4443 public WearableExtender setContentIconGravity(int contentIconGravity) {
4444 mContentIconGravity = contentIconGravity;
4445 return this;
4446 }
4447
4448 /**
4449 * Get the gravity that the content icon should have within the notification display.
4450 * Supported values include {@link android.view.Gravity#START} and
4451 * {@link android.view.Gravity#END}. The default value is {@link android.view.Gravity#END}.
4452 * @see #getContentIcon
4453 */
4454 public int getContentIconGravity() {
4455 return mContentIconGravity;
4456 }
4457
4458 /**
4459 * Set an action from this notification's actions to be clickable with the content of
Griff Hazen14f57992014-05-26 09:07:14 -07004460 * this notification. This action will no longer display separately from the
4461 * notification's content.
4462 *
Griff Hazenca48d352014-05-28 22:37:13 -07004463 * <p>For notifications with multiple pages, child pages can also have content actions
Griff Hazen14f57992014-05-26 09:07:14 -07004464 * set, although the list of available actions comes from the main notification and not
4465 * from the child page's notification.
4466 *
4467 * @param actionIndex The index of the action to hoist onto the current notification page.
4468 * If wearable actions were added to the main notification, this index
4469 * will apply to that list, otherwise it will apply to the regular
4470 * actions list.
Griff Hazen61a9e862014-05-22 16:05:19 -07004471 */
4472 public WearableExtender setContentAction(int actionIndex) {
4473 mContentActionIndex = actionIndex;
4474 return this;
4475 }
4476
4477 /**
Griff Hazenca48d352014-05-28 22:37:13 -07004478 * Get the index of the notification action, if any, that was specified as being clickable
4479 * with the content of this notification. This action will no longer display separately
Griff Hazen14f57992014-05-26 09:07:14 -07004480 * from the notification's content.
Griff Hazen61a9e862014-05-22 16:05:19 -07004481 *
Griff Hazenca48d352014-05-28 22:37:13 -07004482 * <p>For notifications with multiple pages, child pages can also have content actions
Griff Hazen14f57992014-05-26 09:07:14 -07004483 * set, although the list of available actions comes from the main notification and not
4484 * from the child page's notification.
4485 *
4486 * <p>If wearable specific actions were added to the main notification, this index will
4487 * apply to that list, otherwise it will apply to the regular actions list.
Griff Hazenca48d352014-05-28 22:37:13 -07004488 *
4489 * @return the action index or {@link #UNSET_ACTION_INDEX} if no action was selected.
Griff Hazen61a9e862014-05-22 16:05:19 -07004490 */
4491 public int getContentAction() {
4492 return mContentActionIndex;
4493 }
4494
4495 /**
4496 * Set the gravity that this notification should have within the available viewport space.
4497 * Supported values include {@link android.view.Gravity#TOP},
4498 * {@link android.view.Gravity#CENTER_VERTICAL} and {@link android.view.Gravity#BOTTOM}.
4499 * The default value is {@link android.view.Gravity#BOTTOM}.
4500 */
4501 public WearableExtender setGravity(int gravity) {
4502 mGravity = gravity;
4503 return this;
4504 }
4505
4506 /**
4507 * Get the gravity that this notification should have within the available viewport space.
4508 * Supported values include {@link android.view.Gravity#TOP},
4509 * {@link android.view.Gravity#CENTER_VERTICAL} and {@link android.view.Gravity#BOTTOM}.
4510 * The default value is {@link android.view.Gravity#BOTTOM}.
4511 */
4512 public int getGravity() {
4513 return mGravity;
4514 }
4515
4516 /**
4517 * Set the custom size preset for the display of this notification out of the available
4518 * presets found in {@link android.app.Notification.WearableExtender}, e.g.
4519 * {@link #SIZE_LARGE}.
4520 * <p>Some custom size presets are only applicable for custom display notifications created
4521 * using {@link android.app.Notification.WearableExtender#setDisplayIntent}. Check the
4522 * documentation for the preset in question. See also
4523 * {@link #setCustomContentHeight} and {@link #getCustomSizePreset}.
4524 */
4525 public WearableExtender setCustomSizePreset(int sizePreset) {
4526 mCustomSizePreset = sizePreset;
4527 return this;
4528 }
4529
4530 /**
4531 * Get the custom size preset for the display of this notification out of the available
4532 * presets found in {@link android.app.Notification.WearableExtender}, e.g.
4533 * {@link #SIZE_LARGE}.
4534 * <p>Some custom size presets are only applicable for custom display notifications created
4535 * using {@link #setDisplayIntent}. Check the documentation for the preset in question.
4536 * See also {@link #setCustomContentHeight} and {@link #setCustomSizePreset}.
4537 */
4538 public int getCustomSizePreset() {
4539 return mCustomSizePreset;
4540 }
4541
4542 /**
4543 * Set the custom height in pixels for the display of this notification's content.
4544 * <p>This option is only available for custom display notifications created
4545 * using {@link android.app.Notification.WearableExtender#setDisplayIntent}. See also
4546 * {@link android.app.Notification.WearableExtender#setCustomSizePreset} and
4547 * {@link #getCustomContentHeight}.
4548 */
4549 public WearableExtender setCustomContentHeight(int height) {
4550 mCustomContentHeight = height;
4551 return this;
4552 }
4553
4554 /**
4555 * Get the custom height in pixels for the display of this notification's content.
4556 * <p>This option is only available for custom display notifications created
4557 * using {@link #setDisplayIntent}. See also {@link #setCustomSizePreset} and
4558 * {@link #setCustomContentHeight}.
4559 */
4560 public int getCustomContentHeight() {
4561 return mCustomContentHeight;
4562 }
4563
4564 /**
4565 * Set whether the scrolling position for the contents of this notification should start
4566 * at the bottom of the contents instead of the top when the contents are too long to
4567 * display within the screen. Default is false (start scroll at the top).
4568 */
4569 public WearableExtender setStartScrollBottom(boolean startScrollBottom) {
4570 setFlag(FLAG_START_SCROLL_BOTTOM, startScrollBottom);
4571 return this;
4572 }
4573
4574 /**
4575 * Get whether the scrolling position for the contents of this notification should start
4576 * at the bottom of the contents instead of the top when the contents are too long to
4577 * display within the screen. Default is false (start scroll at the top).
4578 */
4579 public boolean getStartScrollBottom() {
4580 return (mFlags & FLAG_START_SCROLL_BOTTOM) != 0;
4581 }
4582
4583 /**
4584 * Set whether the content intent is available when the wearable device is not connected
4585 * to a companion device. The user can still trigger this intent when the wearable device
4586 * is offline, but a visual hint will indicate that the content intent may not be available.
4587 * Defaults to true.
4588 */
4589 public WearableExtender setContentIntentAvailableOffline(
4590 boolean contentIntentAvailableOffline) {
4591 setFlag(FLAG_CONTENT_INTENT_AVAILABLE_OFFLINE, contentIntentAvailableOffline);
4592 return this;
4593 }
4594
4595 /**
4596 * Get whether the content intent is available when the wearable device is not connected
4597 * to a companion device. The user can still trigger this intent when the wearable device
4598 * is offline, but a visual hint will indicate that the content intent may not be available.
4599 * Defaults to true.
4600 */
4601 public boolean getContentIntentAvailableOffline() {
4602 return (mFlags & FLAG_CONTENT_INTENT_AVAILABLE_OFFLINE) != 0;
4603 }
4604
4605 /**
4606 * Set a hint that this notification's icon should not be displayed.
4607 * @param hintHideIcon {@code true} to hide the icon, {@code false} otherwise.
4608 * @return this object for method chaining
4609 */
4610 public WearableExtender setHintHideIcon(boolean hintHideIcon) {
4611 setFlag(FLAG_HINT_HIDE_ICON, hintHideIcon);
4612 return this;
4613 }
4614
4615 /**
4616 * Get a hint that this notification's icon should not be displayed.
4617 * @return {@code true} if this icon should not be displayed, false otherwise.
4618 * The default value is {@code false} if this was never set.
4619 */
4620 public boolean getHintHideIcon() {
4621 return (mFlags & FLAG_HINT_HIDE_ICON) != 0;
4622 }
4623
4624 /**
4625 * Set a visual hint that only the background image of this notification should be
4626 * displayed, and other semantic content should be hidden. This hint is only applicable
4627 * to sub-pages added using {@link #addPage}.
4628 */
4629 public WearableExtender setHintShowBackgroundOnly(boolean hintShowBackgroundOnly) {
4630 setFlag(FLAG_HINT_SHOW_BACKGROUND_ONLY, hintShowBackgroundOnly);
4631 return this;
4632 }
4633
4634 /**
4635 * Get a visual hint that only the background image of this notification should be
4636 * displayed, and other semantic content should be hidden. This hint is only applicable
4637 * to sub-pages added using {@link android.app.Notification.WearableExtender#addPage}.
4638 */
4639 public boolean getHintShowBackgroundOnly() {
4640 return (mFlags & FLAG_HINT_SHOW_BACKGROUND_ONLY) != 0;
4641 }
4642
4643 private void setFlag(int mask, boolean value) {
4644 if (value) {
4645 mFlags |= mask;
4646 } else {
4647 mFlags &= ~mask;
4648 }
4649 }
4650 }
4651
4652 /**
4653 * Get an array of Notification objects from a parcelable array bundle field.
4654 * Update the bundle to have a typed array so fetches in the future don't need
4655 * to do an array copy.
4656 */
4657 private static Notification[] getNotificationArrayFromBundle(Bundle bundle, String key) {
4658 Parcelable[] array = bundle.getParcelableArray(key);
4659 if (array instanceof Notification[] || array == null) {
4660 return (Notification[]) array;
4661 }
4662 Notification[] typedArray = Arrays.copyOf(array, array.length,
4663 Notification[].class);
4664 bundle.putParcelableArray(key, typedArray);
4665 return typedArray;
4666 }
Christoph Studer4600f9b2014-07-22 22:44:43 +02004667
4668 private static class BuilderRemoteViews extends RemoteViews {
4669 public BuilderRemoteViews(Parcel parcel) {
4670 super(parcel);
4671 }
4672
Svet Ganov0da85b62014-08-06 14:11:37 -07004673 public BuilderRemoteViews(String packageName, int userId, int layoutId) {
4674 super(packageName, userId, layoutId);
Christoph Studer4600f9b2014-07-22 22:44:43 +02004675 }
4676
4677 @Override
4678 public BuilderRemoteViews clone() {
4679 Parcel p = Parcel.obtain();
4680 writeToParcel(p, 0);
4681 p.setDataPosition(0);
4682 BuilderRemoteViews brv = new BuilderRemoteViews(p);
4683 p.recycle();
4684 return brv;
4685 }
4686 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004687}