blob: 55aec5906cac290450f6b04ec34a6e83326dae1d [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
2 * Copyright (C) 2007 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package android.app;
18
Tor Norbye80756e32015-03-02 09:39:27 -080019import android.annotation.ColorInt;
Tor Norbye7b9c9122013-05-30 16:48:33 -070020import android.annotation.DrawableRes;
Tor Norbyed9273d62013-05-30 15:59:53 -070021import android.annotation.IntDef;
Daniel Sandler01df1c62014-06-09 10:54:01 -040022import android.annotation.SdkConstant;
23import android.annotation.SdkConstant.SdkConstantType;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080024import android.content.Context;
25import android.content.Intent;
Kenny Guy77320062014-08-27 21:37:15 +010026import android.content.pm.ApplicationInfo;
Christoph Studer4600f9b2014-07-22 22:44:43 +020027import android.content.pm.PackageManager.NameNotFoundException;
Jorim Jaggief72a192014-08-26 21:57:46 +020028import android.content.res.ColorStateList;
Joe Onoratoef1e7762010-09-17 18:38:38 -040029import android.graphics.Bitmap;
Kenny Guy8a0101b2014-05-08 23:34:12 +010030import android.graphics.Canvas;
Jorim Jaggi5c2d8462014-03-21 17:37:00 +010031import android.graphics.PorterDuff;
Kenny Guy8a0101b2014-05-08 23:34:12 +010032import android.graphics.drawable.Drawable;
Dan Sandlerd63f9322015-05-06 15:18:49 -040033import android.graphics.drawable.Icon;
John Spurlockc0650f022014-07-19 13:22:39 -040034import android.media.AudioAttributes;
Jeff Sharkey098d5802012-04-26 17:30:34 -070035import android.media.AudioManager;
Jeff Browndba34ba2014-06-24 20:46:03 -070036import android.media.session.MediaSession;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080037import android.net.Uri;
Daniel Sandlerdcbaf662013-04-26 16:23:09 -040038import android.os.BadParcelableException;
Christoph Studer239f8352014-08-25 15:13:18 +020039import android.os.Build;
Daniel Sandler2561b0b2012-02-13 21:04:12 -050040import android.os.Bundle;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080041import android.os.Parcel;
42import android.os.Parcelable;
Daniel Sandlera2985ed2012-04-03 16:42:00 -040043import android.os.SystemClock;
Jeff Sharkey6d515712012-09-20 16:06:08 -070044import android.os.UserHandle;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080045import android.text.TextUtils;
Daniel Sandlerdcbaf662013-04-26 16:23:09 -040046import android.util.Log;
Jorim Jaggi445d3c02014-08-19 22:33:42 +020047import android.util.MathUtils;
Daniel Sandler9f7936a2012-05-21 16:14:28 -040048import android.util.TypedValue;
Griff Hazen61a9e862014-05-22 16:05:19 -070049import android.view.Gravity;
Joe Onorato8595a3d2010-11-19 18:12:07 -080050import android.view.View;
Jeff Sharkey1c400132011-08-05 14:50:13 -070051import android.widget.ProgressBar;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080052import android.widget.RemoteViews;
53
Griff Hazen959591e2014-05-15 22:26:18 -070054import com.android.internal.R;
Griff Hazenc091ba82014-05-16 10:13:26 -070055import com.android.internal.util.NotificationColorUtil;
Griff Hazen959591e2014-05-15 22:26:18 -070056
Tor Norbyed9273d62013-05-30 15:59:53 -070057import java.lang.annotation.Retention;
58import java.lang.annotation.RetentionPolicy;
Christoph Studer4600f9b2014-07-22 22:44:43 +020059import java.lang.reflect.Constructor;
Andy Stadler110988c2010-12-03 14:29:16 -080060import java.text.NumberFormat;
Daniel Sandler2561b0b2012-02-13 21:04:12 -050061import java.util.ArrayList;
Griff Hazen61a9e862014-05-22 16:05:19 -070062import java.util.Arrays;
Griff Hazen5cadc3b2014-05-20 09:55:39 -070063import java.util.Collections;
Griff Hazen61a9e862014-05-22 16:05:19 -070064import java.util.List;
Joe Onorato561d3852010-11-20 18:09:34 -080065
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080066/**
67 * A class that represents how a persistent notification is to be presented to
68 * the user using the {@link android.app.NotificationManager}.
69 *
Joe Onoratocb109a02011-01-18 17:57:41 -080070 * <p>The {@link Notification.Builder Notification.Builder} has been added to make it
71 * easier to construct Notifications.</p>
72 *
Joe Fernandez558459f2011-10-13 16:47:36 -070073 * <div class="special reference">
74 * <h3>Developer Guides</h3>
75 * <p>For a guide to creating notifications, read the
76 * <a href="{@docRoot}guide/topics/ui/notifiers/notifications.html">Status Bar Notifications</a>
77 * developer guide.</p>
78 * </div>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080079 */
80public class Notification implements Parcelable
81{
Daniel Sandlerdcbaf662013-04-26 16:23:09 -040082 private static final String TAG = "Notification";
83
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080084 /**
Daniel Sandler01df1c62014-06-09 10:54:01 -040085 * An activity that provides a user interface for adjusting notification preferences for its
86 * containing application. Optional but recommended for apps that post
87 * {@link android.app.Notification Notifications}.
88 */
89 @SdkConstant(SdkConstantType.INTENT_CATEGORY)
90 public static final String INTENT_CATEGORY_NOTIFICATION_PREFERENCES
91 = "android.intent.category.NOTIFICATION_PREFERENCES";
92
93 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080094 * Use all default values (where applicable).
95 */
96 public static final int DEFAULT_ALL = ~0;
Daniel Sandler2561b0b2012-02-13 21:04:12 -050097
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080098 /**
99 * Use the default notification sound. This will ignore any given
100 * {@link #sound}.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500101 *
Chris Wren47c20a12014-06-18 17:27:29 -0400102 * <p>
103 * A notification that is noisy is more likely to be presented as a heads-up notification.
104 * </p>
105 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800106 * @see #defaults
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500107 */
108
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800109 public static final int DEFAULT_SOUND = 1;
110
111 /**
112 * Use the default notification vibrate. This will ignore any given
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500113 * {@link #vibrate}. Using phone vibration requires the
Scott Mainb8b36452009-04-26 15:50:49 -0700114 * {@link android.Manifest.permission#VIBRATE VIBRATE} permission.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500115 *
Chris Wren47c20a12014-06-18 17:27:29 -0400116 * <p>
117 * A notification that vibrates is more likely to be presented as a heads-up notification.
118 * </p>
119 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800120 * @see #defaults
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500121 */
122
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800123 public static final int DEFAULT_VIBRATE = 2;
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500124
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800125 /**
126 * Use the default notification lights. This will ignore the
127 * {@link #FLAG_SHOW_LIGHTS} bit, and {@link #ledARGB}, {@link #ledOffMS}, or
128 * {@link #ledOnMS}.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500129 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800130 * @see #defaults
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500131 */
132
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800133 public static final int DEFAULT_LIGHTS = 4;
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500134
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800135 /**
Christoph Studer535ec612014-09-03 15:47:47 +0200136 * Maximum length of CharSequences accepted by Builder and friends.
137 *
138 * <p>
139 * Avoids spamming the system with overly large strings such as full e-mails.
140 */
141 private static final int MAX_CHARSEQUENCE_LENGTH = 5 * 1024;
142
143 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500144 * A timestamp related to this notification, in milliseconds since the epoch.
Joe Malin8d40d042012-11-05 11:36:40 -0800145 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500146 * Default value: {@link System#currentTimeMillis() Now}.
147 *
148 * Choose a timestamp that will be most relevant to the user. For most finite events, this
149 * corresponds to the time the event happened (or will happen, in the case of events that have
150 * yet to occur but about which the user is being informed). Indefinite events should be
Joe Malin8d40d042012-11-05 11:36:40 -0800151 * timestamped according to when the activity began.
152 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500153 * Some examples:
Joe Malin8d40d042012-11-05 11:36:40 -0800154 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500155 * <ul>
156 * <li>Notification of a new chat message should be stamped when the message was received.</li>
157 * <li>Notification of an ongoing file download (with a progress bar, for example) should be stamped when the download started.</li>
158 * <li>Notification of a completed file download should be stamped when the download finished.</li>
159 * <li>Notification of an upcoming meeting should be stamped with the time the meeting will begin (that is, in the future).</li>
160 * <li>Notification of an ongoing stopwatch (increasing timer) should be stamped with the watch's start time.
161 * <li>Notification of an ongoing countdown timer should be stamped with the timer's end time.
Joe Malin8d40d042012-11-05 11:36:40 -0800162 * </ul>
163 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800164 */
165 public long when;
166
167 /**
168 * The resource id of a drawable to use as the icon in the status bar.
Dan Sandler86647982015-05-13 23:41:13 -0400169 *
170 * @deprecated Use {@link Builder#setSmallIcon(Icon)} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800171 */
Dan Sandler86647982015-05-13 23:41:13 -0400172 @Deprecated
Tor Norbye7b9c9122013-05-30 16:48:33 -0700173 @DrawableRes
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800174 public int icon;
175
176 /**
Joe Onorato46439ce2010-11-19 13:56:21 -0800177 * If the icon in the status bar is to have more than one level, you can set this. Otherwise,
178 * leave it at its default value of 0.
179 *
180 * @see android.widget.ImageView#setImageLevel
Griff Hazen959591e2014-05-15 22:26:18 -0700181 * @see android.graphics.drawable.Drawable#setLevel
Joe Onorato46439ce2010-11-19 13:56:21 -0800182 */
183 public int iconLevel;
184
185 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500186 * The number of events that this notification represents. For example, in a new mail
187 * notification, this could be the number of unread messages.
Joe Malin8d40d042012-11-05 11:36:40 -0800188 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500189 * The system may or may not use this field to modify the appearance of the notification. For
190 * example, before {@link android.os.Build.VERSION_CODES#HONEYCOMB}, this number was
191 * superimposed over the icon in the status bar. Starting with
192 * {@link android.os.Build.VERSION_CODES#HONEYCOMB}, the template used by
193 * {@link Notification.Builder} has displayed the number in the expanded notification view.
Joe Malin8d40d042012-11-05 11:36:40 -0800194 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500195 * If the number is 0 or negative, it is never shown.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800196 */
197 public int number;
198
199 /**
200 * The intent to execute when the expanded status entry is clicked. If
201 * this is an activity, it must include the
202 * {@link android.content.Intent#FLAG_ACTIVITY_NEW_TASK} flag, which requires
Scott Main7aee61f2011-02-08 11:25:01 -0800203 * that you take care of task management as described in the
204 * <a href="{@docRoot}guide/topics/fundamentals/tasks-and-back-stack.html">Tasks and Back
Dianne Hackborn6ceca582012-01-10 15:24:26 -0800205 * Stack</a> document. In particular, make sure to read the notification section
206 * <a href="{@docRoot}guide/topics/ui/notifiers/notifications.html#HandlingNotifications">Handling
207 * Notifications</a> for the correct ways to launch an application from a
208 * notification.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800209 */
210 public PendingIntent contentIntent;
211
212 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500213 * The intent to execute when the notification is explicitly dismissed by the user, either with
214 * the "Clear All" button or by swiping it away individually.
215 *
216 * This probably shouldn't be launching an activity since several of those will be sent
217 * at the same time.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800218 */
219 public PendingIntent deleteIntent;
220
221 /**
Dianne Hackborn170bae72010-09-03 15:14:28 -0700222 * An intent to launch instead of posting the notification to the status bar.
Joe Onoratocb109a02011-01-18 17:57:41 -0800223 *
Chris Wren47c20a12014-06-18 17:27:29 -0400224 * <p>
225 * The system UI may choose to display a heads-up notification, instead of
226 * launching this intent, while the user is using the device.
227 * </p>
228 *
Joe Onoratocb109a02011-01-18 17:57:41 -0800229 * @see Notification.Builder#setFullScreenIntent
Daniel Sandlere46cbd32010-06-17 10:35:26 -0400230 */
231 public PendingIntent fullScreenIntent;
232
233 /**
Dan Sandler5fcdf6e2014-07-18 11:31:15 -0400234 * Text that summarizes this notification for accessibility services.
235 *
236 * As of the L release, this text is no longer shown on screen, but it is still useful to
237 * accessibility services (where it serves as an audible announcement of the notification's
238 * appearance).
Joe Onoratoef1e7762010-09-17 18:38:38 -0400239 *
Joe Onorato46439ce2010-11-19 13:56:21 -0800240 * @see #tickerView
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800241 */
242 public CharSequence tickerText;
243
244 /**
Dan Sandler5fcdf6e2014-07-18 11:31:15 -0400245 * Formerly, a view showing the {@link #tickerText}.
246 *
247 * No longer displayed in the status bar as of API 21.
Joe Onoratoef1e7762010-09-17 18:38:38 -0400248 */
Dan Sandler5fcdf6e2014-07-18 11:31:15 -0400249 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -0800250 public RemoteViews tickerView;
Joe Onoratoef1e7762010-09-17 18:38:38 -0400251
252 /**
Daniel Sandlere46cbd32010-06-17 10:35:26 -0400253 * The view that will represent this notification in the expanded status bar.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800254 */
255 public RemoteViews contentView;
256
Daniel Sandlera0a938c2012-03-15 08:42:37 -0400257 /**
Daniel Sandler4dfbe832012-04-11 14:51:46 -0400258 * A large-format version of {@link #contentView}, giving the Notification an
Daniel Sandlerf3b73432012-03-27 15:01:25 -0400259 * opportunity to show more detail. The system UI may choose to show this
260 * instead of the normal content view at its discretion.
Daniel Sandlerf3b73432012-03-27 15:01:25 -0400261 */
262 public RemoteViews bigContentView;
263
Chris Wren8fd39ec2014-02-27 17:43:26 -0500264
265 /**
Chris Wren47c20a12014-06-18 17:27:29 -0400266 * A medium-format version of {@link #contentView}, providing the Notification an
267 * opportunity to add action buttons to contentView. At its discretion, the system UI may
268 * choose to show this as a heads-up notification, which will pop up so the user can see
269 * it without leaving their current activity.
Chris Wren8fd39ec2014-02-27 17:43:26 -0500270 */
271 public RemoteViews headsUpContentView;
272
Daniel Sandlerf3b73432012-03-27 15:01:25 -0400273 /**
Dan Sandler86647982015-05-13 23:41:13 -0400274 * A large bitmap to be shown in the notification content area.
275 *
276 * @deprecated Use {@link Builder#setLargeIcon(Icon)} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800277 */
Dan Sandler86647982015-05-13 23:41:13 -0400278 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -0800279 public Bitmap largeIcon;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800280
281 /**
282 * The sound to play.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500283 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800284 * <p>
Chris Wren47c20a12014-06-18 17:27:29 -0400285 * A notification that is noisy is more likely to be presented as a heads-up notification.
286 * </p>
287 *
288 * <p>
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500289 * To play the default notification sound, see {@link #defaults}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800290 * </p>
291 */
292 public Uri sound;
293
294 /**
295 * Use this constant as the value for audioStreamType to request that
296 * the default stream type for notifications be used. Currently the
Jeff Sharkey098d5802012-04-26 17:30:34 -0700297 * default stream type is {@link AudioManager#STREAM_NOTIFICATION}.
John Spurlockc0650f022014-07-19 13:22:39 -0400298 *
299 * @deprecated Use {@link #audioAttributes} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800300 */
Jean-Michel Trivi81f871e2014-08-06 16:32:38 -0700301 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800302 public static final int STREAM_DEFAULT = -1;
303
304 /**
305 * The audio stream type to use when playing the sound.
306 * Should be one of the STREAM_ constants from
307 * {@link android.media.AudioManager}.
John Spurlockc0650f022014-07-19 13:22:39 -0400308 *
309 * @deprecated Use {@link #audioAttributes} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800310 */
Jean-Michel Trivi81f871e2014-08-06 16:32:38 -0700311 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800312 public int audioStreamType = STREAM_DEFAULT;
313
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800314 /**
John Spurlockc0650f022014-07-19 13:22:39 -0400315 * The default value of {@link #audioAttributes}.
316 */
317 public static final AudioAttributes AUDIO_ATTRIBUTES_DEFAULT = new AudioAttributes.Builder()
318 .setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
319 .setUsage(AudioAttributes.USAGE_NOTIFICATION)
320 .build();
321
322 /**
323 * The {@link AudioAttributes audio attributes} to use when playing the sound.
324 */
325 public AudioAttributes audioAttributes = AUDIO_ATTRIBUTES_DEFAULT;
326
327 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500328 * The pattern with which to vibrate.
329 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800330 * <p>
331 * To vibrate the default pattern, see {@link #defaults}.
332 * </p>
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500333 *
Chris Wren47c20a12014-06-18 17:27:29 -0400334 * <p>
335 * A notification that vibrates is more likely to be presented as a heads-up notification.
336 * </p>
337 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800338 * @see android.os.Vibrator#vibrate(long[],int)
339 */
340 public long[] vibrate;
341
342 /**
343 * The color of the led. The hardware will do its best approximation.
344 *
345 * @see #FLAG_SHOW_LIGHTS
346 * @see #flags
347 */
Tor Norbye80756e32015-03-02 09:39:27 -0800348 @ColorInt
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800349 public int ledARGB;
350
351 /**
352 * The number of milliseconds for the LED to be on while it's flashing.
353 * The hardware will do its best approximation.
354 *
355 * @see #FLAG_SHOW_LIGHTS
356 * @see #flags
357 */
358 public int ledOnMS;
359
360 /**
361 * The number of milliseconds for the LED to be off while it's flashing.
362 * The hardware will do its best approximation.
363 *
364 * @see #FLAG_SHOW_LIGHTS
365 * @see #flags
366 */
367 public int ledOffMS;
368
369 /**
370 * Specifies which values should be taken from the defaults.
371 * <p>
372 * To set, OR the desired from {@link #DEFAULT_SOUND},
373 * {@link #DEFAULT_VIBRATE}, {@link #DEFAULT_LIGHTS}. For all default
374 * values, use {@link #DEFAULT_ALL}.
375 * </p>
376 */
377 public int defaults;
378
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800379 /**
380 * Bit to be bitwise-ored into the {@link #flags} field that should be
381 * set if you want the LED on for this notification.
382 * <ul>
383 * <li>To turn the LED off, pass 0 in the alpha channel for colorARGB
384 * or 0 for both ledOnMS and ledOffMS.</li>
385 * <li>To turn the LED on, pass 1 for ledOnMS and 0 for ledOffMS.</li>
386 * <li>To flash the LED, pass the number of milliseconds that it should
387 * be on and off to ledOnMS and ledOffMS.</li>
388 * </ul>
389 * <p>
390 * Since hardware varies, you are not guaranteed that any of the values
391 * you pass are honored exactly. Use the system defaults (TODO) if possible
392 * because they will be set to values that work on any given hardware.
393 * <p>
394 * The alpha channel must be set for forward compatibility.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500395 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800396 */
397 public static final int FLAG_SHOW_LIGHTS = 0x00000001;
398
399 /**
400 * Bit to be bitwise-ored into the {@link #flags} field that should be
401 * set if this notification is in reference to something that is ongoing,
402 * like a phone call. It should not be set if this notification is in
403 * reference to something that happened at a particular point in time,
404 * like a missed phone call.
405 */
406 public static final int FLAG_ONGOING_EVENT = 0x00000002;
407
408 /**
409 * Bit to be bitwise-ored into the {@link #flags} field that if set,
Scott Mainb8b36452009-04-26 15:50:49 -0700410 * the audio will be repeated until the notification is
411 * cancelled or the notification window is opened.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800412 */
413 public static final int FLAG_INSISTENT = 0x00000004;
414
415 /**
416 * Bit to be bitwise-ored into the {@link #flags} field that should be
Griff Hazen293977b2014-04-28 08:37:20 -0700417 * set if you would only like the sound, vibrate and ticker to be played
418 * if the notification was not already showing.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800419 */
420 public static final int FLAG_ONLY_ALERT_ONCE = 0x00000008;
421
422 /**
423 * Bit to be bitwise-ored into the {@link #flags} field that should be
424 * set if the notification should be canceled when it is clicked by the
Daniel Sandler8aa9ae62012-12-04 23:31:47 -0500425 * user.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800426 */
427 public static final int FLAG_AUTO_CANCEL = 0x00000010;
428
429 /**
430 * Bit to be bitwise-ored into the {@link #flags} field that should be
431 * set if the notification should not be canceled when the user clicks
432 * the Clear all button.
433 */
434 public static final int FLAG_NO_CLEAR = 0x00000020;
435
Dianne Hackbornd8a43f62009-08-17 23:33:56 -0700436 /**
437 * Bit to be bitwise-ored into the {@link #flags} field that should be
438 * set if this notification represents a currently running service. This
439 * will normally be set for you by {@link Service#startForeground}.
440 */
441 public static final int FLAG_FOREGROUND_SERVICE = 0x00000040;
442
Daniel Sandlere46cbd32010-06-17 10:35:26 -0400443 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500444 * Obsolete flag indicating high-priority notifications; use the priority field instead.
Joe Malin8d40d042012-11-05 11:36:40 -0800445 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500446 * @deprecated Use {@link #priority} with a positive value.
Daniel Sandlere46cbd32010-06-17 10:35:26 -0400447 */
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500448 public static final int FLAG_HIGH_PRIORITY = 0x00000080;
Daniel Sandlere46cbd32010-06-17 10:35:26 -0400449
Griff Hazendfcb0802014-02-11 12:00:00 -0800450 /**
451 * Bit to be bitswise-ored into the {@link #flags} field that should be
452 * set if this notification is relevant to the current device only
453 * and it is not recommended that it bridge to other devices.
454 */
455 public static final int FLAG_LOCAL_ONLY = 0x00000100;
456
Griff Hazen5cadc3b2014-05-20 09:55:39 -0700457 /**
458 * Bit to be bitswise-ored into the {@link #flags} field that should be
459 * set if this notification is the group summary for a group of notifications.
460 * Grouped notifications may display in a cluster or stack on devices which
461 * support such rendering. Requires a group key also be set using {@link Builder#setGroup}.
462 */
463 public static final int FLAG_GROUP_SUMMARY = 0x00000200;
464
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800465 public int flags;
466
Tor Norbyed9273d62013-05-30 15:59:53 -0700467 /** @hide */
468 @IntDef({PRIORITY_DEFAULT,PRIORITY_LOW,PRIORITY_MIN,PRIORITY_HIGH,PRIORITY_MAX})
469 @Retention(RetentionPolicy.SOURCE)
470 public @interface Priority {}
471
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800472 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500473 * Default notification {@link #priority}. If your application does not prioritize its own
474 * notifications, use this value for all notifications.
475 */
476 public static final int PRIORITY_DEFAULT = 0;
477
478 /**
479 * Lower {@link #priority}, for items that are less important. The UI may choose to show these
480 * items smaller, or at a different position in the list, compared with your app's
481 * {@link #PRIORITY_DEFAULT} items.
482 */
483 public static final int PRIORITY_LOW = -1;
484
485 /**
486 * Lowest {@link #priority}; these items might not be shown to the user except under special
487 * circumstances, such as detailed notification logs.
488 */
489 public static final int PRIORITY_MIN = -2;
490
491 /**
492 * Higher {@link #priority}, for more important notifications or alerts. The UI may choose to
493 * show these items larger, or at a different position in notification lists, compared with
494 * your app's {@link #PRIORITY_DEFAULT} items.
495 */
496 public static final int PRIORITY_HIGH = 1;
497
498 /**
499 * Highest {@link #priority}, for your application's most important items that require the
500 * user's prompt attention or input.
501 */
502 public static final int PRIORITY_MAX = 2;
503
504 /**
505 * Relative priority for this notification.
Joe Malin8d40d042012-11-05 11:36:40 -0800506 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500507 * Priority is an indication of how much of the user's valuable attention should be consumed by
508 * this notification. Low-priority notifications may be hidden from the user in certain
509 * situations, while the user might be interrupted for a higher-priority notification. The
Daniel Sandler6738eee2012-11-16 12:03:32 -0500510 * system will make a determination about how to interpret this priority when presenting
511 * the notification.
Chris Wren47c20a12014-06-18 17:27:29 -0400512 *
513 * <p>
514 * A notification that is at least {@link #PRIORITY_HIGH} is more likely to be presented
515 * as a heads-up notification.
516 * </p>
517 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500518 */
Tor Norbyed9273d62013-05-30 15:59:53 -0700519 @Priority
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500520 public int priority;
Joe Malin8d40d042012-11-05 11:36:40 -0800521
Dan Sandler26e81cf2014-05-06 10:01:27 -0400522 /**
523 * Accent color (an ARGB integer like the constants in {@link android.graphics.Color})
524 * to be applied by the standard Style templates when presenting this notification.
525 *
526 * The current template design constructs a colorful header image by overlaying the
527 * {@link #icon} image (stenciled in white) atop a field of this color. Alpha components are
528 * ignored.
529 */
Tor Norbye80756e32015-03-02 09:39:27 -0800530 @ColorInt
Dan Sandler26e81cf2014-05-06 10:01:27 -0400531 public int color = COLOR_DEFAULT;
532
533 /**
534 * Special value of {@link #color} telling the system not to decorate this notification with
535 * any special color but instead use default colors when presenting this notification.
536 */
Tor Norbye80756e32015-03-02 09:39:27 -0800537 @ColorInt
Dan Sandler26e81cf2014-05-06 10:01:27 -0400538 public static final int COLOR_DEFAULT = 0; // AKA Color.TRANSPARENT
Dan Sandler0bf2ed82013-12-21 23:33:41 -0600539
540 /**
541 * Sphere of visibility of this notification, which affects how and when the SystemUI reveals
542 * the notification's presence and contents in untrusted situations (namely, on the secure
543 * lockscreen).
544 *
545 * The default level, {@link #VISIBILITY_PRIVATE}, behaves exactly as notifications have always
546 * done on Android: The notification's {@link #icon} and {@link #tickerText} (if available) are
547 * shown in all situations, but the contents are only available if the device is unlocked for
548 * the appropriate user.
549 *
550 * A more permissive policy can be expressed by {@link #VISIBILITY_PUBLIC}; such a notification
551 * can be read even in an "insecure" context (that is, above a secure lockscreen).
552 * To modify the public version of this notification—for example, to redact some portions—see
553 * {@link Builder#setPublicVersion(Notification)}.
554 *
555 * Finally, a notification can be made {@link #VISIBILITY_SECRET}, which will suppress its icon
556 * and ticker until the user has bypassed the lockscreen.
557 */
558 public int visibility;
559
Griff Hazenfc3922d2014-08-20 11:56:44 -0700560 /**
561 * Notification visibility: Show this notification in its entirety on all lockscreens.
562 *
563 * {@see #visibility}
564 */
Dan Sandler0bf2ed82013-12-21 23:33:41 -0600565 public static final int VISIBILITY_PUBLIC = 1;
Griff Hazenfc3922d2014-08-20 11:56:44 -0700566
567 /**
568 * Notification visibility: Show this notification on all lockscreens, but conceal sensitive or
569 * private information on secure lockscreens.
570 *
571 * {@see #visibility}
572 */
Dan Sandler0bf2ed82013-12-21 23:33:41 -0600573 public static final int VISIBILITY_PRIVATE = 0;
Griff Hazenfc3922d2014-08-20 11:56:44 -0700574
575 /**
576 * Notification visibility: Do not reveal any part of this notification on a secure lockscreen.
577 *
578 * {@see #visibility}
579 */
Dan Sandler0bf2ed82013-12-21 23:33:41 -0600580 public static final int VISIBILITY_SECRET = -1;
581
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500582 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400583 * Notification category: incoming call (voice or video) or similar synchronous communication request.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500584 */
John Spurlockfd7f1e02014-03-18 16:41:57 -0400585 public static final String CATEGORY_CALL = "call";
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500586
587 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400588 * Notification category: incoming direct message (SMS, instant message, etc.).
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500589 */
John Spurlockfd7f1e02014-03-18 16:41:57 -0400590 public static final String CATEGORY_MESSAGE = "msg";
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500591
592 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400593 * Notification category: asynchronous bulk message (email).
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500594 */
John Spurlockfd7f1e02014-03-18 16:41:57 -0400595 public static final String CATEGORY_EMAIL = "email";
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500596
597 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400598 * Notification category: calendar event.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500599 */
John Spurlockfd7f1e02014-03-18 16:41:57 -0400600 public static final String CATEGORY_EVENT = "event";
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500601
602 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400603 * Notification category: promotion or advertisement.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500604 */
John Spurlockfd7f1e02014-03-18 16:41:57 -0400605 public static final String CATEGORY_PROMO = "promo";
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500606
607 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400608 * Notification category: alarm or timer.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500609 */
John Spurlockfd7f1e02014-03-18 16:41:57 -0400610 public static final String CATEGORY_ALARM = "alarm";
611
612 /**
613 * Notification category: progress of a long-running background operation.
614 */
615 public static final String CATEGORY_PROGRESS = "progress";
616
617 /**
618 * Notification category: social network or sharing update.
619 */
620 public static final String CATEGORY_SOCIAL = "social";
621
622 /**
623 * Notification category: error in background operation or authentication status.
624 */
625 public static final String CATEGORY_ERROR = "err";
626
627 /**
628 * Notification category: media transport control for playback.
629 */
630 public static final String CATEGORY_TRANSPORT = "transport";
631
632 /**
633 * Notification category: system or device status update. Reserved for system use.
634 */
635 public static final String CATEGORY_SYSTEM = "sys";
636
637 /**
638 * Notification category: indication of running background service.
639 */
640 public static final String CATEGORY_SERVICE = "service";
641
642 /**
John Spurlock0a69c8c2014-03-21 13:30:57 -0400643 * Notification category: a specific, timely recommendation for a single thing.
644 * For example, a news app might want to recommend a news story it believes the user will
645 * want to read next.
646 */
647 public static final String CATEGORY_RECOMMENDATION = "recommendation";
648
649 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400650 * Notification category: ongoing information about device or contextual status.
651 */
652 public static final String CATEGORY_STATUS = "status";
653
654 /**
John Spurlock24d3dad2015-04-02 12:24:02 -0400655 * Notification category: user-scheduled reminder.
656 */
657 public static final String CATEGORY_REMINDER = "reminder";
658
659 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400660 * One of the predefined notification categories (see the <code>CATEGORY_*</code> constants)
661 * that best describes this Notification. May be used by the system for ranking and filtering.
662 */
663 public String category;
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500664
Griff Hazen5cadc3b2014-05-20 09:55:39 -0700665 private String mGroupKey;
666
667 /**
668 * Get the key used to group this notification into a cluster or stack
669 * with other notifications on devices which support such rendering.
670 */
671 public String getGroup() {
672 return mGroupKey;
673 }
674
675 private String mSortKey;
676
677 /**
678 * Get a sort key that orders this notification among other notifications from the
679 * same package. This can be useful if an external sort was already applied and an app
680 * would like to preserve this. Notifications will be sorted lexicographically using this
681 * value, although providing different priorities in addition to providing sort key may
682 * cause this value to be ignored.
683 *
684 * <p>This sort key can also be used to order members of a notification group. See
685 * {@link Builder#setGroup}.
686 *
687 * @see String#compareTo(String)
688 */
689 public String getSortKey() {
690 return mSortKey;
691 }
692
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500693 /**
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400694 * Additional semantic data to be carried around with this Notification.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400695 * <p>
696 * The extras keys defined here are intended to capture the original inputs to {@link Builder}
697 * APIs, and are intended to be used by
698 * {@link android.service.notification.NotificationListenerService} implementations to extract
699 * detailed information from notification objects.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500700 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400701 public Bundle extras = new Bundle();
702
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400703 /**
704 * {@link #extras} key: this is the title of the notification,
705 * as supplied to {@link Builder#setContentTitle(CharSequence)}.
706 */
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -0500707 public static final String EXTRA_TITLE = "android.title";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400708
709 /**
710 * {@link #extras} key: this is the title of the notification when shown in expanded form,
711 * e.g. as supplied to {@link BigTextStyle#setBigContentTitle(CharSequence)}.
712 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400713 public static final String EXTRA_TITLE_BIG = EXTRA_TITLE + ".big";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400714
715 /**
716 * {@link #extras} key: this is the main text payload, as supplied to
717 * {@link Builder#setContentText(CharSequence)}.
718 */
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -0500719 public static final String EXTRA_TEXT = "android.text";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400720
721 /**
722 * {@link #extras} key: this is a third line of text, as supplied to
723 * {@link Builder#setSubText(CharSequence)}.
724 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400725 public static final String EXTRA_SUB_TEXT = "android.subText";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400726
727 /**
728 * {@link #extras} key: this is a small piece of additional text as supplied to
729 * {@link Builder#setContentInfo(CharSequence)}.
730 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400731 public static final String EXTRA_INFO_TEXT = "android.infoText";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400732
733 /**
734 * {@link #extras} key: this is a line of summary information intended to be shown
735 * alongside expanded notifications, as supplied to (e.g.)
736 * {@link BigTextStyle#setSummaryText(CharSequence)}.
737 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400738 public static final String EXTRA_SUMMARY_TEXT = "android.summaryText";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400739
740 /**
Christoph Studer4600f9b2014-07-22 22:44:43 +0200741 * {@link #extras} key: this is the longer text shown in the big form of a
742 * {@link BigTextStyle} notification, as supplied to
743 * {@link BigTextStyle#bigText(CharSequence)}.
744 */
745 public static final String EXTRA_BIG_TEXT = "android.bigText";
746
747 /**
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400748 * {@link #extras} key: this is the resource ID of the notification's main small icon, as
749 * supplied to {@link Builder#setSmallIcon(int)}.
750 */
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -0500751 public static final String EXTRA_SMALL_ICON = "android.icon";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400752
753 /**
754 * {@link #extras} key: this is a bitmap to be used instead of the small icon when showing the
755 * notification payload, as
756 * supplied to {@link Builder#setLargeIcon(android.graphics.Bitmap)}.
757 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400758 public static final String EXTRA_LARGE_ICON = "android.largeIcon";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400759
760 /**
761 * {@link #extras} key: this is a bitmap to be used instead of the one from
762 * {@link Builder#setLargeIcon(android.graphics.Bitmap)} when the notification is
763 * shown in its expanded form, as supplied to
764 * {@link BigPictureStyle#bigLargeIcon(android.graphics.Bitmap)}.
765 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400766 public static final String EXTRA_LARGE_ICON_BIG = EXTRA_LARGE_ICON + ".big";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400767
768 /**
769 * {@link #extras} key: this is the progress value supplied to
770 * {@link Builder#setProgress(int, int, boolean)}.
771 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400772 public static final String EXTRA_PROGRESS = "android.progress";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400773
774 /**
775 * {@link #extras} key: this is the maximum value supplied to
776 * {@link Builder#setProgress(int, int, boolean)}.
777 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400778 public static final String EXTRA_PROGRESS_MAX = "android.progressMax";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400779
780 /**
781 * {@link #extras} key: whether the progress bar is indeterminate, supplied to
782 * {@link Builder#setProgress(int, int, boolean)}.
783 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400784 public static final String EXTRA_PROGRESS_INDETERMINATE = "android.progressIndeterminate";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400785
786 /**
787 * {@link #extras} key: whether {@link #when} should be shown as a count-up timer (specifically
788 * a {@link android.widget.Chronometer}) instead of a timestamp, as supplied to
789 * {@link Builder#setUsesChronometer(boolean)}.
790 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400791 public static final String EXTRA_SHOW_CHRONOMETER = "android.showChronometer";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400792
793 /**
794 * {@link #extras} key: whether {@link #when} should be shown,
795 * as supplied to {@link Builder#setShowWhen(boolean)}.
796 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400797 public static final String EXTRA_SHOW_WHEN = "android.showWhen";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400798
799 /**
800 * {@link #extras} key: this is a bitmap to be shown in {@link BigPictureStyle} expanded
801 * notifications, supplied to {@link BigPictureStyle#bigPicture(android.graphics.Bitmap)}.
802 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400803 public static final String EXTRA_PICTURE = "android.picture";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400804
805 /**
806 * {@link #extras} key: An array of CharSequences to show in {@link InboxStyle} expanded
807 * notifications, each of which was supplied to {@link InboxStyle#addLine(CharSequence)}.
808 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400809 public static final String EXTRA_TEXT_LINES = "android.textLines";
Dan Sandler842dd772014-05-15 09:36:47 -0400810
811 /**
812 * {@link #extras} key: A string representing the name of the specific
813 * {@link android.app.Notification.Style} used to create this notification.
814 */
Chris Wren91ad5632013-06-05 15:05:57 -0400815 public static final String EXTRA_TEMPLATE = "android.template";
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400816
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400817 /**
Chris Wrene6c48932014-09-29 17:19:27 -0400818 * {@link #extras} key: A String array containing the people that this notification relates to,
819 * each of which was supplied to {@link Builder#addPerson(String)}.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400820 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400821 public static final String EXTRA_PEOPLE = "android.people";
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500822
823 /**
Chris Wren47c20a12014-06-18 17:27:29 -0400824 * {@link #extras} key: used to provide hints about the appropriateness of
825 * displaying this notification as a heads-up notification.
Chris Wren51c75102013-07-16 20:49:17 -0400826 * @hide
827 */
828 public static final String EXTRA_AS_HEADS_UP = "headsup";
829
830 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400831 * Allow certain system-generated notifications to appear before the device is provisioned.
832 * Only available to notifications coming from the android package.
833 * @hide
834 */
835 public static final String EXTRA_ALLOW_DURING_SETUP = "android.allowDuringSetup";
836
837 /**
Jose Limae9e3b3b2014-05-18 23:44:50 -0700838 * {@link #extras} key: A
839 * {@link android.content.ContentUris content URI} pointing to an image that can be displayed
840 * in the background when the notification is selected. The URI must point to an image stream
841 * suitable for passing into
842 * {@link android.graphics.BitmapFactory#decodeStream(java.io.InputStream)
843 * BitmapFactory.decodeStream}; all other content types will be ignored. The content provider
844 * URI used for this purpose must require no permissions to read the image data.
845 */
846 public static final String EXTRA_BACKGROUND_IMAGE_URI = "android.backgroundImageUri";
847
848 /**
Dan Sandler842dd772014-05-15 09:36:47 -0400849 * {@link #extras} key: A
Jeff Browndba34ba2014-06-24 20:46:03 -0700850 * {@link android.media.session.MediaSession.Token} associated with a
Dan Sandler842dd772014-05-15 09:36:47 -0400851 * {@link android.app.Notification.MediaStyle} notification.
852 */
853 public static final String EXTRA_MEDIA_SESSION = "android.mediaSession";
854
855 /**
Bryan Mawhinneye191f902014-07-22 12:50:09 +0100856 * {@link #extras} key: the indices of actions to be shown in the compact view,
857 * as supplied to (e.g.) {@link MediaStyle#setShowActionsInCompactView(int...)}.
858 */
859 public static final String EXTRA_COMPACT_ACTIONS = "android.compactActions";
860
Christoph Studer943aa672014-08-03 20:31:16 +0200861 /**
Kenny Guy8942bcd2014-09-08 21:09:47 +0100862 * {@link #extras} key: the user that built the notification.
863 *
864 * @hide
865 */
866 public static final String EXTRA_ORIGINATING_USERID = "android.originatingUserId";
867
868 /**
Chris Wren47c20a12014-06-18 17:27:29 -0400869 * Value for {@link #EXTRA_AS_HEADS_UP} that indicates this notification should not be
870 * displayed in the heads up space.
871 *
872 * <p>
873 * If this notification has a {@link #fullScreenIntent}, then it will always launch the
874 * full-screen intent when posted.
875 * </p>
Chris Wren51c75102013-07-16 20:49:17 -0400876 * @hide
877 */
878 public static final int HEADS_UP_NEVER = 0;
879
880 /**
Chris Wren47c20a12014-06-18 17:27:29 -0400881 * Default value for {@link #EXTRA_AS_HEADS_UP} that indicates this notification may be
882 * displayed as a heads up.
Chris Wren51c75102013-07-16 20:49:17 -0400883 * @hide
884 */
885 public static final int HEADS_UP_ALLOWED = 1;
886
887 /**
Chris Wren47c20a12014-06-18 17:27:29 -0400888 * Value for {@link #EXTRA_AS_HEADS_UP} that indicates this notification is a
889 * good candidate for display as a heads up.
Chris Wren51c75102013-07-16 20:49:17 -0400890 * @hide
891 */
892 public static final int HEADS_UP_REQUESTED = 2;
893
Dan Sandlerd63f9322015-05-06 15:18:49 -0400894 private Icon mSmallIcon;
895 private Icon mLargeIcon;
896
Chris Wren51c75102013-07-16 20:49:17 -0400897 /**
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400898 * Structure to encapsulate a named action that can be shown as part of this notification.
899 * It must include an icon, a label, and a {@link PendingIntent} to be fired when the action is
900 * selected by the user.
901 * <p>
Griff Hazen959591e2014-05-15 22:26:18 -0700902 * Apps should use {@link Notification.Builder#addAction(int, CharSequence, PendingIntent)}
903 * or {@link Notification.Builder#addAction(Notification.Action)}
904 * to attach actions.
Daniel Sandlera0a938c2012-03-15 08:42:37 -0400905 */
Daniel Sandlerea2a3172013-02-20 22:24:20 -0500906 public static class Action implements Parcelable {
Griff Hazen959591e2014-05-15 22:26:18 -0700907 private final Bundle mExtras;
Dan Sandler86647982015-05-13 23:41:13 -0400908 private Icon mIcon;
Griff Hazen61a9e862014-05-22 16:05:19 -0700909 private final RemoteInput[] mRemoteInputs;
Griff Hazen959591e2014-05-15 22:26:18 -0700910
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400911 /**
912 * Small icon representing the action.
Dan Sandler86647982015-05-13 23:41:13 -0400913 *
914 * @deprecated Use {@link Action#getIcon()} instead.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400915 */
Dan Sandler86647982015-05-13 23:41:13 -0400916 @Deprecated
Daniel Sandlera0a938c2012-03-15 08:42:37 -0400917 public int icon;
Griff Hazen5cadc3b2014-05-20 09:55:39 -0700918
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400919 /**
920 * Title of the action.
921 */
Daniel Sandlera0a938c2012-03-15 08:42:37 -0400922 public CharSequence title;
Griff Hazen5cadc3b2014-05-20 09:55:39 -0700923
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400924 /**
925 * Intent to send when the user invokes this action. May be null, in which case the action
926 * may be rendered in a disabled presentation by the system UI.
927 */
Daniel Sandlera0a938c2012-03-15 08:42:37 -0400928 public PendingIntent actionIntent;
Griff Hazen959591e2014-05-15 22:26:18 -0700929
Daniel Sandlera0a938c2012-03-15 08:42:37 -0400930 private Action(Parcel in) {
Dan Sandler86647982015-05-13 23:41:13 -0400931 if (in.readInt() != 0) {
932 mIcon = Icon.CREATOR.createFromParcel(in);
Dan Sandler68079d52015-07-22 10:45:30 -0400933 if (mIcon.getType() == Icon.TYPE_RESOURCE) {
934 icon = mIcon.getResId();
935 }
Dan Sandler86647982015-05-13 23:41:13 -0400936 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -0400937 title = TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(in);
938 if (in.readInt() == 1) {
939 actionIntent = PendingIntent.CREATOR.createFromParcel(in);
940 }
Griff Hazen959591e2014-05-15 22:26:18 -0700941 mExtras = in.readBundle();
Griff Hazen5cadc3b2014-05-20 09:55:39 -0700942 mRemoteInputs = in.createTypedArray(RemoteInput.CREATOR);
Daniel Sandlera0a938c2012-03-15 08:42:37 -0400943 }
Griff Hazen5cadc3b2014-05-20 09:55:39 -0700944
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400945 /**
Dan Sandler86647982015-05-13 23:41:13 -0400946 * @deprecated Use {@link android.app.Notification.Action.Builder}.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400947 */
Dan Sandler86647982015-05-13 23:41:13 -0400948 @Deprecated
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400949 public Action(int icon, CharSequence title, PendingIntent intent) {
Dan Sandler86647982015-05-13 23:41:13 -0400950 this(Icon.createWithResource("", icon), title, intent, new Bundle(), null);
Griff Hazen959591e2014-05-15 22:26:18 -0700951 }
952
Dan Sandler86647982015-05-13 23:41:13 -0400953 private Action(Icon icon, CharSequence title, PendingIntent intent, Bundle extras,
Griff Hazen5cadc3b2014-05-20 09:55:39 -0700954 RemoteInput[] remoteInputs) {
Dan Sandler86647982015-05-13 23:41:13 -0400955 this.mIcon = icon;
Gus Prevasf5bff46f2015-08-24 10:34:28 -0400956 if (icon != null && icon.getType() == Icon.TYPE_RESOURCE) {
957 this.icon = icon.getResId();
958 }
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400959 this.title = title;
960 this.actionIntent = intent;
Griff Hazen959591e2014-05-15 22:26:18 -0700961 this.mExtras = extras != null ? extras : new Bundle();
Griff Hazen5cadc3b2014-05-20 09:55:39 -0700962 this.mRemoteInputs = remoteInputs;
Griff Hazen959591e2014-05-15 22:26:18 -0700963 }
964
965 /**
Dan Sandler86647982015-05-13 23:41:13 -0400966 * Return an icon representing the action.
967 */
968 public Icon getIcon() {
969 if (mIcon == null && icon != 0) {
970 // you snuck an icon in here without using the builder; let's try to keep it
971 mIcon = Icon.createWithResource("", icon);
972 }
973 return mIcon;
974 }
975
976 /**
Griff Hazen959591e2014-05-15 22:26:18 -0700977 * Get additional metadata carried around with this Action.
978 */
979 public Bundle getExtras() {
980 return mExtras;
981 }
982
983 /**
Griff Hazen5cadc3b2014-05-20 09:55:39 -0700984 * Get the list of inputs to be collected from the user when this action is sent.
985 * May return null if no remote inputs were added.
986 */
987 public RemoteInput[] getRemoteInputs() {
988 return mRemoteInputs;
989 }
990
991 /**
Griff Hazen959591e2014-05-15 22:26:18 -0700992 * Builder class for {@link Action} objects.
993 */
Griff Hazen5cadc3b2014-05-20 09:55:39 -0700994 public static final class Builder {
Dan Sandler86647982015-05-13 23:41:13 -0400995 private final Icon mIcon;
Griff Hazen959591e2014-05-15 22:26:18 -0700996 private final CharSequence mTitle;
997 private final PendingIntent mIntent;
998 private final Bundle mExtras;
Griff Hazen5cadc3b2014-05-20 09:55:39 -0700999 private ArrayList<RemoteInput> mRemoteInputs;
Griff Hazen959591e2014-05-15 22:26:18 -07001000
1001 /**
1002 * Construct a new builder for {@link Action} object.
1003 * @param icon icon to show for this action
1004 * @param title the title of the action
1005 * @param intent the {@link PendingIntent} to fire when users trigger this action
1006 */
Dan Sandler86647982015-05-13 23:41:13 -04001007 @Deprecated
Griff Hazen959591e2014-05-15 22:26:18 -07001008 public Builder(int icon, CharSequence title, PendingIntent intent) {
Dan Sandler86647982015-05-13 23:41:13 -04001009 this(Icon.createWithResource("", icon), title, intent, new Bundle(), null);
1010 }
1011
1012 /**
1013 * Construct a new builder for {@link Action} object.
1014 * @param icon icon to show for this action
1015 * @param title the title of the action
1016 * @param intent the {@link PendingIntent} to fire when users trigger this action
1017 */
1018 public Builder(Icon icon, CharSequence title, PendingIntent intent) {
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001019 this(icon, title, intent, new Bundle(), null);
Griff Hazen959591e2014-05-15 22:26:18 -07001020 }
1021
1022 /**
1023 * Construct a new builder for {@link Action} object using the fields from an
1024 * {@link Action}.
1025 * @param action the action to read fields from.
1026 */
1027 public Builder(Action action) {
Dan Sandler86647982015-05-13 23:41:13 -04001028 this(action.getIcon(), action.title, action.actionIntent, new Bundle(action.mExtras),
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001029 action.getRemoteInputs());
Griff Hazen959591e2014-05-15 22:26:18 -07001030 }
1031
Dan Sandler86647982015-05-13 23:41:13 -04001032 private Builder(Icon icon, CharSequence title, PendingIntent intent, Bundle extras,
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001033 RemoteInput[] remoteInputs) {
Griff Hazen959591e2014-05-15 22:26:18 -07001034 mIcon = icon;
1035 mTitle = title;
1036 mIntent = intent;
1037 mExtras = extras;
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001038 if (remoteInputs != null) {
1039 mRemoteInputs = new ArrayList<RemoteInput>(remoteInputs.length);
1040 Collections.addAll(mRemoteInputs, remoteInputs);
1041 }
Griff Hazen959591e2014-05-15 22:26:18 -07001042 }
1043
1044 /**
1045 * Merge additional metadata into this builder.
1046 *
1047 * <p>Values within the Bundle will replace existing extras values in this Builder.
1048 *
1049 * @see Notification.Action#extras
1050 */
1051 public Builder addExtras(Bundle extras) {
1052 if (extras != null) {
1053 mExtras.putAll(extras);
1054 }
1055 return this;
1056 }
1057
1058 /**
1059 * Get the metadata Bundle used by this Builder.
1060 *
1061 * <p>The returned Bundle is shared with this Builder.
1062 */
1063 public Bundle getExtras() {
1064 return mExtras;
1065 }
1066
1067 /**
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001068 * Add an input to be collected from the user when this action is sent.
1069 * Response values can be retrieved from the fired intent by using the
1070 * {@link RemoteInput#getResultsFromIntent} function.
1071 * @param remoteInput a {@link RemoteInput} to add to the action
1072 * @return this object for method chaining
1073 */
1074 public Builder addRemoteInput(RemoteInput remoteInput) {
1075 if (mRemoteInputs == null) {
1076 mRemoteInputs = new ArrayList<RemoteInput>();
1077 }
1078 mRemoteInputs.add(remoteInput);
1079 return this;
1080 }
1081
1082 /**
1083 * Apply an extender to this action builder. Extenders may be used to add
1084 * metadata or change options on this builder.
1085 */
Griff Hazen61a9e862014-05-22 16:05:19 -07001086 public Builder extend(Extender extender) {
1087 extender.extend(this);
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001088 return this;
1089 }
1090
1091 /**
Griff Hazen959591e2014-05-15 22:26:18 -07001092 * Combine all of the options that have been set and return a new {@link Action}
1093 * object.
1094 * @return the built action
1095 */
1096 public Action build() {
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001097 RemoteInput[] remoteInputs = mRemoteInputs != null
1098 ? mRemoteInputs.toArray(new RemoteInput[mRemoteInputs.size()]) : null;
1099 return new Action(mIcon, mTitle, mIntent, mExtras, remoteInputs);
Griff Hazen959591e2014-05-15 22:26:18 -07001100 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001101 }
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001102
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001103 @Override
1104 public Action clone() {
1105 return new Action(
Dan Sandler86647982015-05-13 23:41:13 -04001106 getIcon(),
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001107 title,
1108 actionIntent, // safe to alias
1109 new Bundle(mExtras),
1110 getRemoteInputs());
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001111 }
1112 @Override
1113 public int describeContents() {
1114 return 0;
1115 }
1116 @Override
1117 public void writeToParcel(Parcel out, int flags) {
Dan Sandler86647982015-05-13 23:41:13 -04001118 final Icon ic = getIcon();
1119 if (ic != null) {
1120 out.writeInt(1);
1121 ic.writeToParcel(out, 0);
1122 } else {
1123 out.writeInt(0);
1124 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001125 TextUtils.writeToParcel(title, out, flags);
1126 if (actionIntent != null) {
1127 out.writeInt(1);
1128 actionIntent.writeToParcel(out, flags);
1129 } else {
1130 out.writeInt(0);
1131 }
Griff Hazen959591e2014-05-15 22:26:18 -07001132 out.writeBundle(mExtras);
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001133 out.writeTypedArray(mRemoteInputs, flags);
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001134 }
Griff Hazen959591e2014-05-15 22:26:18 -07001135 public static final Parcelable.Creator<Action> CREATOR =
1136 new Parcelable.Creator<Action>() {
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001137 public Action createFromParcel(Parcel in) {
1138 return new Action(in);
1139 }
1140 public Action[] newArray(int size) {
1141 return new Action[size];
1142 }
1143 };
Griff Hazen61a9e862014-05-22 16:05:19 -07001144
1145 /**
1146 * Extender interface for use with {@link Builder#extend}. Extenders may be used to add
1147 * metadata or change options on an action builder.
1148 */
1149 public interface Extender {
1150 /**
1151 * Apply this extender to a notification action builder.
1152 * @param builder the builder to be modified.
1153 * @return the build object for chaining.
1154 */
1155 public Builder extend(Builder builder);
1156 }
1157
1158 /**
1159 * Wearable extender for notification actions. To add extensions to an action,
1160 * create a new {@link android.app.Notification.Action.WearableExtender} object using
1161 * the {@code WearableExtender()} constructor and apply it to a
1162 * {@link android.app.Notification.Action.Builder} using
1163 * {@link android.app.Notification.Action.Builder#extend}.
1164 *
1165 * <pre class="prettyprint">
1166 * Notification.Action action = new Notification.Action.Builder(
1167 * R.drawable.archive_all, "Archive all", actionIntent)
Griff Hazen14f57992014-05-26 09:07:14 -07001168 * .extend(new Notification.Action.WearableExtender()
Griff Hazen61a9e862014-05-22 16:05:19 -07001169 * .setAvailableOffline(false))
Griff Hazen14f57992014-05-26 09:07:14 -07001170 * .build();</pre>
Griff Hazen61a9e862014-05-22 16:05:19 -07001171 */
1172 public static final class WearableExtender implements Extender {
1173 /** Notification action extra which contains wearable extensions */
1174 private static final String EXTRA_WEARABLE_EXTENSIONS = "android.wearable.EXTENSIONS";
1175
Pete Gastaf6781d2014-10-07 15:17:05 -04001176 // Keys within EXTRA_WEARABLE_EXTENSIONS for wearable options.
Griff Hazen61a9e862014-05-22 16:05:19 -07001177 private static final String KEY_FLAGS = "flags";
Pete Gastaf6781d2014-10-07 15:17:05 -04001178 private static final String KEY_IN_PROGRESS_LABEL = "inProgressLabel";
1179 private static final String KEY_CONFIRM_LABEL = "confirmLabel";
1180 private static final String KEY_CANCEL_LABEL = "cancelLabel";
Griff Hazen61a9e862014-05-22 16:05:19 -07001181
1182 // Flags bitwise-ored to mFlags
1183 private static final int FLAG_AVAILABLE_OFFLINE = 0x1;
1184
1185 // Default value for flags integer
1186 private static final int DEFAULT_FLAGS = FLAG_AVAILABLE_OFFLINE;
1187
1188 private int mFlags = DEFAULT_FLAGS;
1189
Pete Gastaf6781d2014-10-07 15:17:05 -04001190 private CharSequence mInProgressLabel;
1191 private CharSequence mConfirmLabel;
1192 private CharSequence mCancelLabel;
1193
Griff Hazen61a9e862014-05-22 16:05:19 -07001194 /**
1195 * Create a {@link android.app.Notification.Action.WearableExtender} with default
1196 * options.
1197 */
1198 public WearableExtender() {
1199 }
1200
1201 /**
1202 * Create a {@link android.app.Notification.Action.WearableExtender} by reading
1203 * wearable options present in an existing notification action.
1204 * @param action the notification action to inspect.
1205 */
1206 public WearableExtender(Action action) {
1207 Bundle wearableBundle = action.getExtras().getBundle(EXTRA_WEARABLE_EXTENSIONS);
1208 if (wearableBundle != null) {
1209 mFlags = wearableBundle.getInt(KEY_FLAGS, DEFAULT_FLAGS);
Pete Gastaf6781d2014-10-07 15:17:05 -04001210 mInProgressLabel = wearableBundle.getCharSequence(KEY_IN_PROGRESS_LABEL);
1211 mConfirmLabel = wearableBundle.getCharSequence(KEY_CONFIRM_LABEL);
1212 mCancelLabel = wearableBundle.getCharSequence(KEY_CANCEL_LABEL);
Griff Hazen61a9e862014-05-22 16:05:19 -07001213 }
1214 }
1215
1216 /**
1217 * Apply wearable extensions to a notification action that is being built. This is
1218 * typically called by the {@link android.app.Notification.Action.Builder#extend}
1219 * method of {@link android.app.Notification.Action.Builder}.
1220 */
1221 @Override
1222 public Action.Builder extend(Action.Builder builder) {
1223 Bundle wearableBundle = new Bundle();
1224
1225 if (mFlags != DEFAULT_FLAGS) {
1226 wearableBundle.putInt(KEY_FLAGS, mFlags);
1227 }
Pete Gastaf6781d2014-10-07 15:17:05 -04001228 if (mInProgressLabel != null) {
1229 wearableBundle.putCharSequence(KEY_IN_PROGRESS_LABEL, mInProgressLabel);
1230 }
1231 if (mConfirmLabel != null) {
1232 wearableBundle.putCharSequence(KEY_CONFIRM_LABEL, mConfirmLabel);
1233 }
1234 if (mCancelLabel != null) {
1235 wearableBundle.putCharSequence(KEY_CANCEL_LABEL, mCancelLabel);
1236 }
Griff Hazen61a9e862014-05-22 16:05:19 -07001237
1238 builder.getExtras().putBundle(EXTRA_WEARABLE_EXTENSIONS, wearableBundle);
1239 return builder;
1240 }
1241
1242 @Override
1243 public WearableExtender clone() {
1244 WearableExtender that = new WearableExtender();
1245 that.mFlags = this.mFlags;
Pete Gastaf6781d2014-10-07 15:17:05 -04001246 that.mInProgressLabel = this.mInProgressLabel;
1247 that.mConfirmLabel = this.mConfirmLabel;
1248 that.mCancelLabel = this.mCancelLabel;
Griff Hazen61a9e862014-05-22 16:05:19 -07001249 return that;
1250 }
1251
1252 /**
1253 * Set whether this action is available when the wearable device is not connected to
1254 * a companion device. The user can still trigger this action when the wearable device is
1255 * offline, but a visual hint will indicate that the action may not be available.
1256 * Defaults to true.
1257 */
1258 public WearableExtender setAvailableOffline(boolean availableOffline) {
1259 setFlag(FLAG_AVAILABLE_OFFLINE, availableOffline);
1260 return this;
1261 }
1262
1263 /**
1264 * Get whether this action is available when the wearable device is not connected to
1265 * a companion device. The user can still trigger this action when the wearable device is
1266 * offline, but a visual hint will indicate that the action may not be available.
1267 * Defaults to true.
1268 */
1269 public boolean isAvailableOffline() {
1270 return (mFlags & FLAG_AVAILABLE_OFFLINE) != 0;
1271 }
1272
1273 private void setFlag(int mask, boolean value) {
1274 if (value) {
1275 mFlags |= mask;
1276 } else {
1277 mFlags &= ~mask;
1278 }
1279 }
Pete Gastaf6781d2014-10-07 15:17:05 -04001280
1281 /**
1282 * Set a label to display while the wearable is preparing to automatically execute the
1283 * action. This is usually a 'ing' verb ending in ellipsis like "Sending..."
1284 *
1285 * @param label the label to display while the action is being prepared to execute
1286 * @return this object for method chaining
1287 */
1288 public WearableExtender setInProgressLabel(CharSequence label) {
1289 mInProgressLabel = label;
1290 return this;
1291 }
1292
1293 /**
1294 * Get the label to display while the wearable is preparing to automatically execute
1295 * the action. This is usually a 'ing' verb ending in ellipsis like "Sending..."
1296 *
1297 * @return the label to display while the action is being prepared to execute
1298 */
1299 public CharSequence getInProgressLabel() {
1300 return mInProgressLabel;
1301 }
1302
1303 /**
1304 * Set a label to display to confirm that the action should be executed.
1305 * This is usually an imperative verb like "Send".
1306 *
1307 * @param label the label to confirm the action should be executed
1308 * @return this object for method chaining
1309 */
1310 public WearableExtender setConfirmLabel(CharSequence label) {
1311 mConfirmLabel = label;
1312 return this;
1313 }
1314
1315 /**
1316 * Get the label to display to confirm that the action should be executed.
1317 * This is usually an imperative verb like "Send".
1318 *
1319 * @return the label to confirm the action should be executed
1320 */
1321 public CharSequence getConfirmLabel() {
1322 return mConfirmLabel;
1323 }
1324
1325 /**
1326 * Set a label to display to cancel the action.
1327 * This is usually an imperative verb, like "Cancel".
1328 *
1329 * @param label the label to display to cancel the action
1330 * @return this object for method chaining
1331 */
1332 public WearableExtender setCancelLabel(CharSequence label) {
1333 mCancelLabel = label;
1334 return this;
1335 }
1336
1337 /**
1338 * Get the label to display to cancel the action.
1339 * This is usually an imperative verb like "Cancel".
1340 *
1341 * @return the label to display to cancel the action
1342 */
1343 public CharSequence getCancelLabel() {
1344 return mCancelLabel;
1345 }
Griff Hazen61a9e862014-05-22 16:05:19 -07001346 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001347 }
1348
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001349 /**
1350 * Array of all {@link Action} structures attached to this notification by
1351 * {@link Builder#addAction(int, CharSequence, PendingIntent)}. Mostly useful for instances of
1352 * {@link android.service.notification.NotificationListenerService} that provide an alternative
1353 * interface for invoking actions.
1354 */
Daniel Sandlerea2a3172013-02-20 22:24:20 -05001355 public Action[] actions;
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001356
1357 /**
Dan Sandler0bf2ed82013-12-21 23:33:41 -06001358 * Replacement version of this notification whose content will be shown
1359 * in an insecure context such as atop a secure keyguard. See {@link #visibility}
1360 * and {@link #VISIBILITY_PUBLIC}.
1361 */
1362 public Notification publicVersion;
1363
1364 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001365 * Constructs a Notification object with default values.
Joe Onorato46439ce2010-11-19 13:56:21 -08001366 * You might want to consider using {@link Builder} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001367 */
1368 public Notification()
1369 {
1370 this.when = System.currentTimeMillis();
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001371 this.priority = PRIORITY_DEFAULT;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001372 }
1373
1374 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001375 * @hide
1376 */
1377 public Notification(Context context, int icon, CharSequence tickerText, long when,
1378 CharSequence contentTitle, CharSequence contentText, Intent contentIntent)
1379 {
Chris Wren1ce4b6d2015-06-11 10:19:43 -04001380 new Builder(context)
1381 .setWhen(when)
1382 .setSmallIcon(icon)
1383 .setTicker(tickerText)
1384 .setContentTitle(contentTitle)
1385 .setContentText(contentText)
1386 .setContentIntent(PendingIntent.getActivity(context, 0, contentIntent, 0))
1387 .buildInto(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001388 }
1389
1390 /**
1391 * Constructs a Notification object with the information needed to
1392 * have a status bar icon without the standard expanded view.
1393 *
1394 * @param icon The resource id of the icon to put in the status bar.
1395 * @param tickerText The text that flows by in the status bar when the notification first
1396 * activates.
1397 * @param when The time to show in the time field. In the System.currentTimeMillis
1398 * timebase.
Joe Onorato46439ce2010-11-19 13:56:21 -08001399 *
1400 * @deprecated Use {@link Builder} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001401 */
Joe Onorato46439ce2010-11-19 13:56:21 -08001402 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001403 public Notification(int icon, CharSequence tickerText, long when)
1404 {
1405 this.icon = icon;
1406 this.tickerText = tickerText;
1407 this.when = when;
1408 }
1409
1410 /**
1411 * Unflatten the notification from a parcel.
1412 */
1413 public Notification(Parcel parcel)
1414 {
1415 int version = parcel.readInt();
1416
1417 when = parcel.readLong();
Dan Sandler3936e7a2015-05-19 20:59:12 -04001418 if (parcel.readInt() != 0) {
1419 mSmallIcon = Icon.CREATOR.createFromParcel(parcel);
Dan Sandler4e787062015-06-17 15:09:48 -04001420 if (mSmallIcon.getType() == Icon.TYPE_RESOURCE) {
1421 icon = mSmallIcon.getResId();
1422 }
Dan Sandler3936e7a2015-05-19 20:59:12 -04001423 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001424 number = parcel.readInt();
1425 if (parcel.readInt() != 0) {
1426 contentIntent = PendingIntent.CREATOR.createFromParcel(parcel);
1427 }
1428 if (parcel.readInt() != 0) {
1429 deleteIntent = PendingIntent.CREATOR.createFromParcel(parcel);
1430 }
1431 if (parcel.readInt() != 0) {
1432 tickerText = TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(parcel);
1433 }
1434 if (parcel.readInt() != 0) {
Joe Onorato46439ce2010-11-19 13:56:21 -08001435 tickerView = RemoteViews.CREATOR.createFromParcel(parcel);
Joe Onoratoef1e7762010-09-17 18:38:38 -04001436 }
1437 if (parcel.readInt() != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001438 contentView = RemoteViews.CREATOR.createFromParcel(parcel);
1439 }
Joe Onorato561d3852010-11-20 18:09:34 -08001440 if (parcel.readInt() != 0) {
Dan Sandlerd63f9322015-05-06 15:18:49 -04001441 mLargeIcon = Icon.CREATOR.createFromParcel(parcel);
Joe Onorato561d3852010-11-20 18:09:34 -08001442 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001443 defaults = parcel.readInt();
1444 flags = parcel.readInt();
1445 if (parcel.readInt() != 0) {
1446 sound = Uri.CREATOR.createFromParcel(parcel);
1447 }
1448
1449 audioStreamType = parcel.readInt();
John Spurlockc0650f022014-07-19 13:22:39 -04001450 if (parcel.readInt() != 0) {
1451 audioAttributes = AudioAttributes.CREATOR.createFromParcel(parcel);
1452 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001453 vibrate = parcel.createLongArray();
1454 ledARGB = parcel.readInt();
1455 ledOnMS = parcel.readInt();
1456 ledOffMS = parcel.readInt();
1457 iconLevel = parcel.readInt();
Daniel Sandlere46cbd32010-06-17 10:35:26 -04001458
1459 if (parcel.readInt() != 0) {
1460 fullScreenIntent = PendingIntent.CREATOR.createFromParcel(parcel);
1461 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001462
1463 priority = parcel.readInt();
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001464
John Spurlockfd7f1e02014-03-18 16:41:57 -04001465 category = parcel.readString();
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001466
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001467 mGroupKey = parcel.readString();
1468
1469 mSortKey = parcel.readString();
1470
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001471 extras = parcel.readBundle(); // may be null
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001472
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001473 actions = parcel.createTypedArray(Action.CREATOR); // may be null
1474
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001475 if (parcel.readInt() != 0) {
Daniel Sandlerf3b73432012-03-27 15:01:25 -04001476 bigContentView = RemoteViews.CREATOR.createFromParcel(parcel);
1477 }
Dan Sandler0bf2ed82013-12-21 23:33:41 -06001478
Chris Wren8fd39ec2014-02-27 17:43:26 -05001479 if (parcel.readInt() != 0) {
1480 headsUpContentView = RemoteViews.CREATOR.createFromParcel(parcel);
1481 }
1482
Dan Sandler0bf2ed82013-12-21 23:33:41 -06001483 visibility = parcel.readInt();
1484
1485 if (parcel.readInt() != 0) {
1486 publicVersion = Notification.CREATOR.createFromParcel(parcel);
1487 }
Dan Sandler26e81cf2014-05-06 10:01:27 -04001488
1489 color = parcel.readInt();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001490 }
1491
Andy Stadler110988c2010-12-03 14:29:16 -08001492 @Override
Joe Onorato18e69df2010-05-17 22:26:12 -07001493 public Notification clone() {
1494 Notification that = new Notification();
Daniel Sandler1a497d32013-04-18 14:52:45 -04001495 cloneInto(that, true);
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001496 return that;
1497 }
Joe Onorato18e69df2010-05-17 22:26:12 -07001498
Daniel Sandler1a497d32013-04-18 14:52:45 -04001499 /**
1500 * Copy all (or if heavy is false, all except Bitmaps and RemoteViews) members
1501 * of this into that.
1502 * @hide
1503 */
1504 public void cloneInto(Notification that, boolean heavy) {
Joe Onorato18e69df2010-05-17 22:26:12 -07001505 that.when = this.when;
Dan Sandlerd63f9322015-05-06 15:18:49 -04001506 that.mSmallIcon = this.mSmallIcon;
Joe Onorato18e69df2010-05-17 22:26:12 -07001507 that.number = this.number;
1508
1509 // PendingIntents are global, so there's no reason (or way) to clone them.
1510 that.contentIntent = this.contentIntent;
1511 that.deleteIntent = this.deleteIntent;
Daniel Sandlere46cbd32010-06-17 10:35:26 -04001512 that.fullScreenIntent = this.fullScreenIntent;
Joe Onorato18e69df2010-05-17 22:26:12 -07001513
1514 if (this.tickerText != null) {
1515 that.tickerText = this.tickerText.toString();
1516 }
Daniel Sandler1a497d32013-04-18 14:52:45 -04001517 if (heavy && this.tickerView != null) {
Joe Onorato46439ce2010-11-19 13:56:21 -08001518 that.tickerView = this.tickerView.clone();
Joe Onoratoef1e7762010-09-17 18:38:38 -04001519 }
Daniel Sandler1a497d32013-04-18 14:52:45 -04001520 if (heavy && this.contentView != null) {
Joe Onorato18e69df2010-05-17 22:26:12 -07001521 that.contentView = this.contentView.clone();
1522 }
Dan Sandlerd63f9322015-05-06 15:18:49 -04001523 if (heavy && this.mLargeIcon != null) {
1524 that.mLargeIcon = this.mLargeIcon;
Joe Onorato561d3852010-11-20 18:09:34 -08001525 }
Jozef BABJAKa8b91832011-02-22 08:05:08 +01001526 that.iconLevel = this.iconLevel;
Joe Onorato18e69df2010-05-17 22:26:12 -07001527 that.sound = this.sound; // android.net.Uri is immutable
1528 that.audioStreamType = this.audioStreamType;
John Spurlockc0650f022014-07-19 13:22:39 -04001529 if (this.audioAttributes != null) {
1530 that.audioAttributes = new AudioAttributes.Builder(this.audioAttributes).build();
1531 }
Joe Onorato18e69df2010-05-17 22:26:12 -07001532
1533 final long[] vibrate = this.vibrate;
1534 if (vibrate != null) {
1535 final int N = vibrate.length;
1536 final long[] vib = that.vibrate = new long[N];
1537 System.arraycopy(vibrate, 0, vib, 0, N);
1538 }
1539
1540 that.ledARGB = this.ledARGB;
1541 that.ledOnMS = this.ledOnMS;
1542 that.ledOffMS = this.ledOffMS;
1543 that.defaults = this.defaults;
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001544
Joe Onorato18e69df2010-05-17 22:26:12 -07001545 that.flags = this.flags;
1546
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001547 that.priority = this.priority;
Joe Malin8d40d042012-11-05 11:36:40 -08001548
John Spurlockfd7f1e02014-03-18 16:41:57 -04001549 that.category = this.category;
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001550
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001551 that.mGroupKey = this.mGroupKey;
1552
1553 that.mSortKey = this.mSortKey;
1554
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001555 if (this.extras != null) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04001556 try {
1557 that.extras = new Bundle(this.extras);
1558 // will unparcel
1559 that.extras.size();
1560 } catch (BadParcelableException e) {
1561 Log.e(TAG, "could not unparcel extras from notification: " + this, e);
1562 that.extras = null;
1563 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001564 }
1565
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001566 if (this.actions != null) {
1567 that.actions = new Action[this.actions.length];
1568 for(int i=0; i<this.actions.length; i++) {
1569 that.actions[i] = this.actions[i].clone();
1570 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001571 }
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001572
Daniel Sandler1a497d32013-04-18 14:52:45 -04001573 if (heavy && this.bigContentView != null) {
Daniel Sandlerf3b73432012-03-27 15:01:25 -04001574 that.bigContentView = this.bigContentView.clone();
1575 }
Daniel Sandler1a497d32013-04-18 14:52:45 -04001576
Chris Wren8fd39ec2014-02-27 17:43:26 -05001577 if (heavy && this.headsUpContentView != null) {
1578 that.headsUpContentView = this.headsUpContentView.clone();
1579 }
1580
Dan Sandler0bf2ed82013-12-21 23:33:41 -06001581 that.visibility = this.visibility;
1582
1583 if (this.publicVersion != null) {
1584 that.publicVersion = new Notification();
1585 this.publicVersion.cloneInto(that.publicVersion, heavy);
1586 }
1587
Dan Sandler26e81cf2014-05-06 10:01:27 -04001588 that.color = this.color;
1589
Daniel Sandler1a497d32013-04-18 14:52:45 -04001590 if (!heavy) {
1591 that.lightenPayload(); // will clean out extras
1592 }
1593 }
1594
1595 /**
1596 * Removes heavyweight parts of the Notification object for archival or for sending to
1597 * listeners when the full contents are not necessary.
1598 * @hide
1599 */
1600 public final void lightenPayload() {
1601 tickerView = null;
1602 contentView = null;
1603 bigContentView = null;
Chris Wren8fd39ec2014-02-27 17:43:26 -05001604 headsUpContentView = null;
Dan Sandlerd63f9322015-05-06 15:18:49 -04001605 mLargeIcon = null;
Daniel Sandler1a497d32013-04-18 14:52:45 -04001606 if (extras != null) {
1607 extras.remove(Notification.EXTRA_LARGE_ICON);
1608 extras.remove(Notification.EXTRA_LARGE_ICON_BIG);
1609 extras.remove(Notification.EXTRA_PICTURE);
Christoph Studer223f44e2014-09-02 14:59:32 +02001610 extras.remove(Notification.EXTRA_BIG_TEXT);
Christoph Studerb82bc782014-08-20 14:29:43 +02001611 // Prevent light notifications from being rebuilt.
1612 extras.remove(Builder.EXTRA_NEEDS_REBUILD);
Daniel Sandler1a497d32013-04-18 14:52:45 -04001613 }
Joe Onorato18e69df2010-05-17 22:26:12 -07001614 }
1615
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04001616 /**
1617 * Make sure this CharSequence is safe to put into a bundle, which basically
1618 * means it had better not be some custom Parcelable implementation.
1619 * @hide
1620 */
1621 public static CharSequence safeCharSequence(CharSequence cs) {
Christoph Studer535ec612014-09-03 15:47:47 +02001622 if (cs == null) return cs;
1623 if (cs.length() > MAX_CHARSEQUENCE_LENGTH) {
1624 cs = cs.subSequence(0, MAX_CHARSEQUENCE_LENGTH);
1625 }
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04001626 if (cs instanceof Parcelable) {
1627 Log.e(TAG, "warning: " + cs.getClass().getCanonicalName()
1628 + " instance is a custom Parcelable and not allowed in Notification");
1629 return cs.toString();
1630 }
1631
1632 return cs;
1633 }
1634
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001635 public int describeContents() {
1636 return 0;
1637 }
1638
1639 /**
Dan Sandler4e787062015-06-17 15:09:48 -04001640 * Flatten this notification into a parcel.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001641 */
1642 public void writeToParcel(Parcel parcel, int flags)
1643 {
1644 parcel.writeInt(1);
1645
1646 parcel.writeLong(when);
Dan Sandler4e787062015-06-17 15:09:48 -04001647 if (mSmallIcon == null && icon != 0) {
1648 // you snuck an icon in here without using the builder; let's try to keep it
1649 mSmallIcon = Icon.createWithResource("", icon);
1650 }
Dan Sandler3936e7a2015-05-19 20:59:12 -04001651 if (mSmallIcon != null) {
1652 parcel.writeInt(1);
1653 mSmallIcon.writeToParcel(parcel, 0);
1654 } else {
1655 parcel.writeInt(0);
1656 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001657 parcel.writeInt(number);
1658 if (contentIntent != null) {
1659 parcel.writeInt(1);
1660 contentIntent.writeToParcel(parcel, 0);
1661 } else {
1662 parcel.writeInt(0);
1663 }
1664 if (deleteIntent != null) {
1665 parcel.writeInt(1);
1666 deleteIntent.writeToParcel(parcel, 0);
1667 } else {
1668 parcel.writeInt(0);
1669 }
1670 if (tickerText != null) {
1671 parcel.writeInt(1);
1672 TextUtils.writeToParcel(tickerText, parcel, flags);
1673 } else {
1674 parcel.writeInt(0);
1675 }
Joe Onorato46439ce2010-11-19 13:56:21 -08001676 if (tickerView != null) {
Joe Onoratoef1e7762010-09-17 18:38:38 -04001677 parcel.writeInt(1);
Joe Onorato46439ce2010-11-19 13:56:21 -08001678 tickerView.writeToParcel(parcel, 0);
Joe Onoratoef1e7762010-09-17 18:38:38 -04001679 } else {
1680 parcel.writeInt(0);
1681 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001682 if (contentView != null) {
1683 parcel.writeInt(1);
1684 contentView.writeToParcel(parcel, 0);
1685 } else {
1686 parcel.writeInt(0);
1687 }
Dan Sandlerd63f9322015-05-06 15:18:49 -04001688 if (mLargeIcon != null) {
Joe Onorato561d3852010-11-20 18:09:34 -08001689 parcel.writeInt(1);
Dan Sandlerd63f9322015-05-06 15:18:49 -04001690 mLargeIcon.writeToParcel(parcel, 0);
Joe Onorato561d3852010-11-20 18:09:34 -08001691 } else {
1692 parcel.writeInt(0);
1693 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001694
1695 parcel.writeInt(defaults);
1696 parcel.writeInt(this.flags);
1697
1698 if (sound != null) {
1699 parcel.writeInt(1);
1700 sound.writeToParcel(parcel, 0);
1701 } else {
1702 parcel.writeInt(0);
1703 }
1704 parcel.writeInt(audioStreamType);
John Spurlockc0650f022014-07-19 13:22:39 -04001705
1706 if (audioAttributes != null) {
1707 parcel.writeInt(1);
1708 audioAttributes.writeToParcel(parcel, 0);
1709 } else {
1710 parcel.writeInt(0);
1711 }
1712
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001713 parcel.writeLongArray(vibrate);
1714 parcel.writeInt(ledARGB);
1715 parcel.writeInt(ledOnMS);
1716 parcel.writeInt(ledOffMS);
1717 parcel.writeInt(iconLevel);
Daniel Sandlere46cbd32010-06-17 10:35:26 -04001718
1719 if (fullScreenIntent != null) {
1720 parcel.writeInt(1);
1721 fullScreenIntent.writeToParcel(parcel, 0);
1722 } else {
1723 parcel.writeInt(0);
1724 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001725
1726 parcel.writeInt(priority);
Joe Malin8d40d042012-11-05 11:36:40 -08001727
John Spurlockfd7f1e02014-03-18 16:41:57 -04001728 parcel.writeString(category);
Joe Malin8d40d042012-11-05 11:36:40 -08001729
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001730 parcel.writeString(mGroupKey);
1731
1732 parcel.writeString(mSortKey);
1733
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001734 parcel.writeBundle(extras); // null ok
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001735
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001736 parcel.writeTypedArray(actions, 0); // null ok
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001737
Daniel Sandlerf3b73432012-03-27 15:01:25 -04001738 if (bigContentView != null) {
1739 parcel.writeInt(1);
1740 bigContentView.writeToParcel(parcel, 0);
1741 } else {
1742 parcel.writeInt(0);
1743 }
Dan Sandler0bf2ed82013-12-21 23:33:41 -06001744
Chris Wren8fd39ec2014-02-27 17:43:26 -05001745 if (headsUpContentView != null) {
1746 parcel.writeInt(1);
1747 headsUpContentView.writeToParcel(parcel, 0);
1748 } else {
1749 parcel.writeInt(0);
1750 }
1751
Dan Sandler0bf2ed82013-12-21 23:33:41 -06001752 parcel.writeInt(visibility);
1753
1754 if (publicVersion != null) {
1755 parcel.writeInt(1);
1756 publicVersion.writeToParcel(parcel, 0);
1757 } else {
1758 parcel.writeInt(0);
1759 }
Dan Sandler26e81cf2014-05-06 10:01:27 -04001760
1761 parcel.writeInt(color);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001762 }
1763
1764 /**
1765 * Parcelable.Creator that instantiates Notification objects
1766 */
1767 public static final Parcelable.Creator<Notification> CREATOR
1768 = new Parcelable.Creator<Notification>()
1769 {
1770 public Notification createFromParcel(Parcel parcel)
1771 {
1772 return new Notification(parcel);
1773 }
1774
1775 public Notification[] newArray(int size)
1776 {
1777 return new Notification[size];
1778 }
1779 };
1780
1781 /**
1782 * Sets the {@link #contentView} field to be a view with the standard "Latest Event"
1783 * layout.
1784 *
1785 * <p>Uses the {@link #icon} and {@link #when} fields to set the icon and time fields
1786 * in the view.</p>
1787 * @param context The context for your application / activity.
1788 * @param contentTitle The title that goes in the expanded entry.
1789 * @param contentText The text that goes in the expanded entry.
1790 * @param contentIntent The intent to launch when the user clicks the expanded notification.
1791 * If this is an activity, it must include the
1792 * {@link android.content.Intent#FLAG_ACTIVITY_NEW_TASK} flag, which requires
Scott Main7aee61f2011-02-08 11:25:01 -08001793 * that you take care of task management as described in the
1794 * <a href="{@docRoot}guide/topics/fundamentals/tasks-and-back-stack.html">Tasks and Back
1795 * Stack</a> document.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001796 *
Joe Onorato46439ce2010-11-19 13:56:21 -08001797 * @deprecated Use {@link Builder} instead.
Chris Wrena05db382015-06-24 15:18:34 -04001798 * @removed
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001799 */
Joe Onorato46439ce2010-11-19 13:56:21 -08001800 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001801 public void setLatestEventInfo(Context context,
1802 CharSequence contentTitle, CharSequence contentText, PendingIntent contentIntent) {
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001803 Notification.Builder builder = new Notification.Builder(context);
1804
1805 // First, ensure that key pieces of information that may have been set directly
1806 // are preserved
1807 builder.setWhen(this.when);
1808 builder.setSmallIcon(this.icon);
1809 builder.setPriority(this.priority);
1810 builder.setTicker(this.tickerText);
1811 builder.setNumber(this.number);
Selim Cinek255dd042014-08-19 22:29:02 +02001812 builder.setColor(this.color);
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001813 builder.mFlags = this.flags;
1814 builder.setSound(this.sound, this.audioStreamType);
1815 builder.setDefaults(this.defaults);
1816 builder.setVibrate(this.vibrate);
Sean Barbeau6ae0b1b2014-07-19 21:31:41 -07001817 builder.setDeleteIntent(this.deleteIntent);
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001818
1819 // now apply the latestEventInfo fields
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001820 if (contentTitle != null) {
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001821 builder.setContentTitle(contentTitle);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001822 }
1823 if (contentText != null) {
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001824 builder.setContentText(contentText);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001825 }
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001826 builder.setContentIntent(contentIntent);
1827 builder.buildInto(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001828 }
1829
1830 @Override
1831 public String toString() {
1832 StringBuilder sb = new StringBuilder();
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001833 sb.append("Notification(pri=");
1834 sb.append(priority);
1835 sb.append(" contentView=");
Joe Onoratoc9596d62011-01-12 17:03:11 -08001836 if (contentView != null) {
1837 sb.append(contentView.getPackage());
1838 sb.append("/0x");
1839 sb.append(Integer.toHexString(contentView.getLayoutId()));
1840 } else {
1841 sb.append("null");
1842 }
1843 sb.append(" vibrate=");
Daniel Sandler6738eee2012-11-16 12:03:32 -05001844 if ((this.defaults & DEFAULT_VIBRATE) != 0) {
1845 sb.append("default");
1846 } else if (this.vibrate != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001847 int N = this.vibrate.length-1;
1848 sb.append("[");
1849 for (int i=0; i<N; i++) {
1850 sb.append(this.vibrate[i]);
1851 sb.append(',');
1852 }
Simon Schoar8cf97d92009-06-10 22:08:37 +02001853 if (N != -1) {
1854 sb.append(this.vibrate[N]);
1855 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001856 sb.append("]");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001857 } else {
1858 sb.append("null");
1859 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001860 sb.append(" sound=");
Daniel Sandler6738eee2012-11-16 12:03:32 -05001861 if ((this.defaults & DEFAULT_SOUND) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001862 sb.append("default");
Daniel Sandler6738eee2012-11-16 12:03:32 -05001863 } else if (this.sound != null) {
1864 sb.append(this.sound.toString());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001865 } else {
1866 sb.append("null");
1867 }
Chris Wren365b6d32015-07-16 10:39:26 -04001868 if (this.tickerText != null) {
1869 sb.append(" tick");
1870 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001871 sb.append(" defaults=0x");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001872 sb.append(Integer.toHexString(this.defaults));
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001873 sb.append(" flags=0x");
Daniel Sandlere46cbd32010-06-17 10:35:26 -04001874 sb.append(Integer.toHexString(this.flags));
Dan Sandler26e81cf2014-05-06 10:01:27 -04001875 sb.append(String.format(" color=0x%08x", this.color));
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001876 if (this.category != null) {
1877 sb.append(" category=");
1878 sb.append(this.category);
1879 }
1880 if (this.mGroupKey != null) {
1881 sb.append(" groupKey=");
1882 sb.append(this.mGroupKey);
1883 }
1884 if (this.mSortKey != null) {
1885 sb.append(" sortKey=");
1886 sb.append(this.mSortKey);
1887 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001888 if (actions != null) {
Dan Sandler1b718782014-07-18 12:43:45 -04001889 sb.append(" actions=");
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001890 sb.append(actions.length);
Dan Sandler1b718782014-07-18 12:43:45 -04001891 }
1892 sb.append(" vis=");
1893 sb.append(visibilityToString(this.visibility));
1894 if (this.publicVersion != null) {
1895 sb.append(" publicVersion=");
1896 sb.append(publicVersion.toString());
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001897 }
1898 sb.append(")");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001899 return sb.toString();
1900 }
Joe Onorato46439ce2010-11-19 13:56:21 -08001901
Dan Sandler1b718782014-07-18 12:43:45 -04001902 /**
1903 * {@hide}
1904 */
1905 public static String visibilityToString(int vis) {
1906 switch (vis) {
1907 case VISIBILITY_PRIVATE:
1908 return "PRIVATE";
1909 case VISIBILITY_PUBLIC:
1910 return "PUBLIC";
1911 case VISIBILITY_SECRET:
1912 return "SECRET";
1913 default:
1914 return "UNKNOWN(" + String.valueOf(vis) + ")";
1915 }
1916 }
1917
Joe Onoratocb109a02011-01-18 17:57:41 -08001918 /**
John Spurlock1d881a12015-03-18 19:21:54 -04001919 * {@hide}
1920 */
1921 public static String priorityToString(@Priority int pri) {
1922 switch (pri) {
1923 case PRIORITY_MIN:
1924 return "MIN";
1925 case PRIORITY_LOW:
1926 return "LOW";
1927 case PRIORITY_DEFAULT:
1928 return "DEFAULT";
1929 case PRIORITY_HIGH:
1930 return "HIGH";
1931 case PRIORITY_MAX:
1932 return "MAX";
1933 default:
1934 return "UNKNOWN(" + String.valueOf(pri) + ")";
1935 }
1936 }
1937
1938 /**
Dan Sandlerd63f9322015-05-06 15:18:49 -04001939 * The small icon representing this notification in the status bar and content view.
1940 *
1941 * @return the small icon representing this notification.
1942 *
1943 * @see Builder#getSmallIcon()
1944 * @see Builder#setSmallIcon(Icon)
1945 */
1946 public Icon getSmallIcon() {
1947 return mSmallIcon;
1948 }
1949
1950 /**
1951 * Used when notifying to clean up legacy small icons.
1952 * @hide
1953 */
1954 public void setSmallIcon(Icon icon) {
1955 mSmallIcon = icon;
1956 }
1957
1958 /**
1959 * The large icon shown in this notification's content view.
1960 * @see Builder#getLargeIcon()
1961 * @see Builder#setLargeIcon(Icon)
1962 */
1963 public Icon getLargeIcon() {
1964 return mLargeIcon;
1965 }
1966
1967 /**
Christoph Studer4600f9b2014-07-22 22:44:43 +02001968 * @hide
1969 */
1970 public boolean isValid() {
1971 // Would like to check for icon!=0 here, too, but NotificationManagerService accepts that
1972 // for legacy reasons.
1973 return contentView != null || extras.getBoolean(Builder.EXTRA_REBUILD_CONTENT_VIEW);
1974 }
1975
1976 /**
Christoph Studerc8db24b2014-07-25 17:50:30 +02001977 * @hide
1978 */
1979 public boolean isGroupSummary() {
1980 return mGroupKey != null && (flags & FLAG_GROUP_SUMMARY) != 0;
1981 }
1982
1983 /**
1984 * @hide
1985 */
1986 public boolean isGroupChild() {
1987 return mGroupKey != null && (flags & FLAG_GROUP_SUMMARY) == 0;
1988 }
1989
1990 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001991 * Builder class for {@link Notification} objects.
Joe Malin8d40d042012-11-05 11:36:40 -08001992 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001993 * Provides a convenient way to set the various fields of a {@link Notification} and generate
Scott Main183bf112012-08-13 19:12:13 -07001994 * content views using the platform's notification layout template. If your app supports
1995 * versions of Android as old as API level 4, you can instead use
1996 * {@link android.support.v4.app.NotificationCompat.Builder NotificationCompat.Builder},
1997 * available in the <a href="{@docRoot}tools/extras/support-library.html">Android Support
1998 * library</a>.
Joe Malin8d40d042012-11-05 11:36:40 -08001999 *
Scott Main183bf112012-08-13 19:12:13 -07002000 * <p>Example:
Joe Malin8d40d042012-11-05 11:36:40 -08002001 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002002 * <pre class="prettyprint">
Scott Main183bf112012-08-13 19:12:13 -07002003 * Notification noti = new Notification.Builder(mContext)
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002004 * .setContentTitle(&quot;New mail from &quot; + sender.toString())
2005 * .setContentText(subject)
2006 * .setSmallIcon(R.drawable.new_mail)
2007 * .setLargeIcon(aBitmap)
Chris Wrenfbd96ba2012-05-01 12:03:58 -04002008 * .build();
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002009 * </pre>
Joe Onoratocb109a02011-01-18 17:57:41 -08002010 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002011 public static class Builder {
Daniel Sandler602ad1c2012-06-12 16:06:27 -04002012 private static final int MAX_ACTION_BUTTONS = 3;
Jorim Jaggi445d3c02014-08-19 22:33:42 +02002013 private static final float LARGE_TEXT_SCALE = 1.3f;
Daniel Sandler8680bf82012-05-15 16:52:52 -04002014
Christoph Studer4600f9b2014-07-22 22:44:43 +02002015 /**
2016 * @hide
2017 */
2018 public static final String EXTRA_NEEDS_REBUILD = "android.rebuild";
2019
2020 /**
2021 * @hide
2022 */
2023 public static final String EXTRA_REBUILD_LARGE_ICON = "android.rebuild.largeIcon";
2024 /**
2025 * @hide
2026 */
2027 public static final String EXTRA_REBUILD_CONTENT_VIEW = "android.rebuild.contentView";
2028 /**
2029 * @hide
2030 */
2031 public static final String EXTRA_REBUILD_CONTENT_VIEW_ACTION_COUNT =
2032 "android.rebuild.contentViewActionCount";
2033 /**
2034 * @hide
2035 */
2036 public static final String EXTRA_REBUILD_BIG_CONTENT_VIEW
2037 = "android.rebuild.bigView";
2038 /**
2039 * @hide
2040 */
2041 public static final String EXTRA_REBUILD_BIG_CONTENT_VIEW_ACTION_COUNT
2042 = "android.rebuild.bigViewActionCount";
2043 /**
2044 * @hide
2045 */
2046 public static final String EXTRA_REBUILD_HEADS_UP_CONTENT_VIEW
2047 = "android.rebuild.hudView";
2048 /**
2049 * @hide
2050 */
2051 public static final String EXTRA_REBUILD_HEADS_UP_CONTENT_VIEW_ACTION_COUNT
2052 = "android.rebuild.hudViewActionCount";
2053
2054 /**
Kenny Guy77320062014-08-27 21:37:15 +01002055 * The ApplicationInfo of the package that created the notification, used to create
2056 * a context to rebuild the notification via a Builder.
2057 * @hide
Christoph Studer4600f9b2014-07-22 22:44:43 +02002058 */
Kenny Guy77320062014-08-27 21:37:15 +01002059 private static final String EXTRA_REBUILD_CONTEXT_APPLICATION_INFO =
2060 "android.rebuild.applicationInfo";
Christoph Studer4600f9b2014-07-22 22:44:43 +02002061
2062 // Whether to enable stripping (at post time) & rebuilding (at listener receive time) of
2063 // memory intensive resources.
2064 private static final boolean STRIP_AND_REBUILD = true;
2065
Joe Onorato46439ce2010-11-19 13:56:21 -08002066 private Context mContext;
2067
2068 private long mWhen;
Dan Sandlerd63f9322015-05-06 15:18:49 -04002069 private Icon mSmallIcon, mLargeIcon;
Joe Onorato46439ce2010-11-19 13:56:21 -08002070 private int mSmallIconLevel;
Joe Onorato8595a3d2010-11-19 18:12:07 -08002071 private int mNumber;
Joe Onorato46439ce2010-11-19 13:56:21 -08002072 private CharSequence mContentTitle;
2073 private CharSequence mContentText;
2074 private CharSequence mContentInfo;
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002075 private CharSequence mSubText;
Joe Onorato46439ce2010-11-19 13:56:21 -08002076 private PendingIntent mContentIntent;
2077 private RemoteViews mContentView;
2078 private PendingIntent mDeleteIntent;
2079 private PendingIntent mFullScreenIntent;
2080 private CharSequence mTickerText;
2081 private RemoteViews mTickerView;
Joe Onorato46439ce2010-11-19 13:56:21 -08002082 private Uri mSound;
2083 private int mAudioStreamType;
John Spurlockc0650f022014-07-19 13:22:39 -04002084 private AudioAttributes mAudioAttributes;
Joe Onorato46439ce2010-11-19 13:56:21 -08002085 private long[] mVibrate;
2086 private int mLedArgb;
2087 private int mLedOnMs;
2088 private int mLedOffMs;
2089 private int mDefaults;
2090 private int mFlags;
Jeff Sharkey1c400132011-08-05 14:50:13 -07002091 private int mProgressMax;
2092 private int mProgress;
2093 private boolean mProgressIndeterminate;
John Spurlockfd7f1e02014-03-18 16:41:57 -04002094 private String mCategory;
Griff Hazen5cadc3b2014-05-20 09:55:39 -07002095 private String mGroupKey;
2096 private String mSortKey;
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002097 private Bundle mExtras;
2098 private int mPriority;
Daniel Sandler8680bf82012-05-15 16:52:52 -04002099 private ArrayList<Action> mActions = new ArrayList<Action>(MAX_ACTION_BUTTONS);
Daniel Sandlera2985ed2012-04-03 16:42:00 -04002100 private boolean mUseChronometer;
Chris Wrenfbd96ba2012-05-01 12:03:58 -04002101 private Style mStyle;
Daniel Sandler0c890492012-09-12 17:23:10 -07002102 private boolean mShowWhen = true;
Dan Sandler0bf2ed82013-12-21 23:33:41 -06002103 private int mVisibility = VISIBILITY_PRIVATE;
2104 private Notification mPublicVersion = null;
Dan Sandler26e81cf2014-05-06 10:01:27 -04002105 private final NotificationColorUtil mColorUtil;
Chris Wrendde75302014-03-26 17:24:15 -04002106 private ArrayList<String> mPeople;
Dan Sandler26e81cf2014-05-06 10:01:27 -04002107 private int mColor = COLOR_DEFAULT;
Christoph Studer7ac80e62014-08-04 16:01:57 +02002108
2109 /**
Kenny Guy8942bcd2014-09-08 21:09:47 +01002110 * The user that built the notification originally.
2111 */
2112 private int mOriginatingUserId;
2113
2114 /**
Christoph Studer4600f9b2014-07-22 22:44:43 +02002115 * Contains extras related to rebuilding during the build phase.
2116 */
2117 private Bundle mRebuildBundle = new Bundle();
2118 /**
2119 * Contains the notification to rebuild when this Builder is in "rebuild" mode.
2120 * Null otherwise.
2121 */
2122 private Notification mRebuildNotification = null;
2123
Joe Onoratocb109a02011-01-18 17:57:41 -08002124 /**
Jorim Jaggid05aa3e2014-08-28 17:52:27 +02002125 * Whether the build notification has three lines. This is used to make the top padding for
2126 * both the contracted and expanded layout consistent.
2127 *
2128 * <p>
2129 * This field is only valid during the build phase.
2130 */
2131 private boolean mHasThreeLines;
2132
2133 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002134 * Constructs a new Builder with the defaults:
Joe Onoratocb109a02011-01-18 17:57:41 -08002135 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002136
2137 * <table>
2138 * <tr><th align=right>priority</th>
2139 * <td>{@link #PRIORITY_DEFAULT}</td></tr>
2140 * <tr><th align=right>when</th>
2141 * <td>now ({@link System#currentTimeMillis()})</td></tr>
2142 * <tr><th align=right>audio stream</th>
2143 * <td>{@link #STREAM_DEFAULT}</td></tr>
2144 * </table>
Joe Onoratocb109a02011-01-18 17:57:41 -08002145 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002146
2147 * @param context
2148 * A {@link Context} that will be used by the Builder to construct the
2149 * RemoteViews. The Context will not be held past the lifetime of this Builder
2150 * object.
Joe Onoratocb109a02011-01-18 17:57:41 -08002151 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002152 public Builder(Context context) {
Adam Powellbf06fa02014-05-30 12:32:18 -07002153 /*
2154 * Important compatibility note!
2155 * Some apps out in the wild create a Notification.Builder in their Activity subclass
2156 * constructor for later use. At this point Activities - themselves subclasses of
2157 * ContextWrapper - do not have their inner Context populated yet. This means that
2158 * any calls to Context methods from within this constructor can cause NPEs in existing
2159 * apps. Any data populated from mContext should therefore be populated lazily to
2160 * preserve compatibility.
2161 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002162 mContext = context;
Andy Stadler110988c2010-12-03 14:29:16 -08002163
2164 // Set defaults to match the defaults of a Notification
Joe Onorato46439ce2010-11-19 13:56:21 -08002165 mWhen = System.currentTimeMillis();
Andy Stadler110988c2010-12-03 14:29:16 -08002166 mAudioStreamType = STREAM_DEFAULT;
John Spurlockc0650f022014-07-19 13:22:39 -04002167 mAudioAttributes = AUDIO_ATTRIBUTES_DEFAULT;
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002168 mPriority = PRIORITY_DEFAULT;
Chris Wrendde75302014-03-26 17:24:15 -04002169 mPeople = new ArrayList<String>();
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01002170
Dianne Hackborn955d8d62014-10-07 20:17:19 -07002171 mColorUtil = context.getApplicationInfo().targetSdkVersion < Build.VERSION_CODES.LOLLIPOP ?
Dan Sandler05c362d2014-09-03 00:16:27 +02002172 NotificationColorUtil.getInstance(mContext) : null;
Joe Onorato46439ce2010-11-19 13:56:21 -08002173 }
2174
Joe Onoratocb109a02011-01-18 17:57:41 -08002175 /**
Christoph Studer4600f9b2014-07-22 22:44:43 +02002176 * Creates a Builder for rebuilding the given Notification.
2177 * <p>
2178 * Call {@link #rebuild()} to retrieve the rebuilt version of 'n'.
2179 */
2180 private Builder(Context context, Notification n) {
2181 this(context);
2182 mRebuildNotification = n;
2183 restoreFromNotification(n);
2184
2185 Style style = null;
2186 Bundle extras = n.extras;
2187 String templateClass = extras.getString(EXTRA_TEMPLATE);
2188 if (!TextUtils.isEmpty(templateClass)) {
2189 Class<? extends Style> styleClass = getNotificationStyleClass(templateClass);
2190 if (styleClass == null) {
2191 Log.d(TAG, "Unknown style class: " + styleClass);
2192 return;
2193 }
2194
2195 try {
2196 Constructor<? extends Style> constructor = styleClass.getConstructor();
Alan Viverette904de2e2015-05-04 10:32:57 -07002197 constructor.setAccessible(true);
Christoph Studer4600f9b2014-07-22 22:44:43 +02002198 style = constructor.newInstance();
2199 style.restoreFromExtras(extras);
2200 } catch (Throwable t) {
2201 Log.e(TAG, "Could not create Style", t);
2202 return;
2203 }
2204 }
2205 if (style != null) {
2206 setStyle(style);
2207 }
2208 }
2209
2210 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002211 * Add a timestamp pertaining to the notification (usually the time the event occurred).
Daniel Sandler0c890492012-09-12 17:23:10 -07002212 * It will be shown in the notification content view by default; use
Griff Hazen50c11652014-05-16 09:46:31 -07002213 * {@link #setShowWhen(boolean) setShowWhen} to control this.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002214 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002215 * @see Notification#when
Joe Onoratocb109a02011-01-18 17:57:41 -08002216 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002217 public Builder setWhen(long when) {
2218 mWhen = when;
2219 return this;
2220 }
2221
Joe Onoratocb109a02011-01-18 17:57:41 -08002222 /**
Griff Hazen50c11652014-05-16 09:46:31 -07002223 * Control whether the timestamp set with {@link #setWhen(long) setWhen} is shown
Daniel Sandler0c890492012-09-12 17:23:10 -07002224 * in the content view.
2225 */
2226 public Builder setShowWhen(boolean show) {
2227 mShowWhen = show;
2228 return this;
2229 }
2230
2231 /**
Daniel Sandlerd33b8032012-05-10 11:41:48 -04002232 * Show the {@link Notification#when} field as a stopwatch.
Joe Malin8d40d042012-11-05 11:36:40 -08002233 *
2234 * Instead of presenting <code>when</code> as a timestamp, the notification will show an
Daniel Sandlerd33b8032012-05-10 11:41:48 -04002235 * automatically updating display of the minutes and seconds since <code>when</code>.
Daniel Sandlera2985ed2012-04-03 16:42:00 -04002236 *
Daniel Sandlerd33b8032012-05-10 11:41:48 -04002237 * Useful when showing an elapsed time (like an ongoing phone call).
2238 *
2239 * @see android.widget.Chronometer
Daniel Sandlera2985ed2012-04-03 16:42:00 -04002240 * @see Notification#when
2241 */
2242 public Builder setUsesChronometer(boolean b) {
2243 mUseChronometer = b;
2244 return this;
2245 }
2246
2247 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002248 * Set the small icon resource, which will be used to represent the notification in the
2249 * status bar.
Joe Onoratocb109a02011-01-18 17:57:41 -08002250 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002251
2252 * The platform template for the expanded view will draw this icon in the left, unless a
2253 * {@link #setLargeIcon(Bitmap) large icon} has also been specified, in which case the small
2254 * icon will be moved to the right-hand side.
2255 *
2256
2257 * @param icon
2258 * A resource ID in the application's package of the drawable to use.
2259 * @see Notification#icon
Joe Onoratocb109a02011-01-18 17:57:41 -08002260 */
Tor Norbye7b9c9122013-05-30 16:48:33 -07002261 public Builder setSmallIcon(@DrawableRes int icon) {
Dan Sandlerd63f9322015-05-06 15:18:49 -04002262 return setSmallIcon(icon != 0
2263 ? Icon.createWithResource(mContext, icon)
2264 : null);
Joe Onorato46439ce2010-11-19 13:56:21 -08002265 }
2266
Joe Onoratocb109a02011-01-18 17:57:41 -08002267 /**
2268 * A variant of {@link #setSmallIcon(int) setSmallIcon(int)} that takes an additional
2269 * level parameter for when the icon is a {@link android.graphics.drawable.LevelListDrawable
2270 * LevelListDrawable}.
2271 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002272 * @param icon A resource ID in the application's package of the drawable to use.
Joe Onoratocb109a02011-01-18 17:57:41 -08002273 * @param level The level to use for the icon.
2274 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002275 * @see Notification#icon
2276 * @see Notification#iconLevel
Joe Onoratocb109a02011-01-18 17:57:41 -08002277 */
Tor Norbye7b9c9122013-05-30 16:48:33 -07002278 public Builder setSmallIcon(@DrawableRes int icon, int level) {
Daniel Sandler09e51a72015-05-14 15:32:28 +00002279 mSmallIconLevel = level;
Dan Sandlerd63f9322015-05-06 15:18:49 -04002280 return setSmallIcon(icon);
2281 }
2282
2283 /**
2284 * Set the small icon, which will be used to represent the notification in the
2285 * status bar and content view (unless overriden there by a
2286 * {@link #setLargeIcon(Bitmap) large icon}).
2287 *
2288 * @param icon An Icon object to use.
2289 * @see Notification#icon
2290 */
2291 public Builder setSmallIcon(Icon icon) {
2292 mSmallIcon = icon;
Joe Onorato46439ce2010-11-19 13:56:21 -08002293 return this;
2294 }
2295
Joe Onoratocb109a02011-01-18 17:57:41 -08002296 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002297 * Set the first line of text in the platform notification template.
Joe Onoratocb109a02011-01-18 17:57:41 -08002298 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002299 public Builder setContentTitle(CharSequence title) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04002300 mContentTitle = safeCharSequence(title);
Joe Onorato46439ce2010-11-19 13:56:21 -08002301 return this;
2302 }
2303
Joe Onoratocb109a02011-01-18 17:57:41 -08002304 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002305 * Set the second line of text in the platform notification template.
Joe Onoratocb109a02011-01-18 17:57:41 -08002306 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002307 public Builder setContentText(CharSequence text) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04002308 mContentText = safeCharSequence(text);
Joe Onorato46439ce2010-11-19 13:56:21 -08002309 return this;
2310 }
2311
Joe Onoratocb109a02011-01-18 17:57:41 -08002312 /**
Joe Malin8d40d042012-11-05 11:36:40 -08002313 * Set the third line of text in the platform notification template.
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04002314 * Don't use if you're also using {@link #setProgress(int, int, boolean)}; they occupy the
2315 * same location in the standard template.
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002316 */
2317 public Builder setSubText(CharSequence text) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04002318 mSubText = safeCharSequence(text);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002319 return this;
2320 }
2321
2322 /**
Joe Onoratocb109a02011-01-18 17:57:41 -08002323 * Set the large number at the right-hand side of the notification. This is
2324 * equivalent to setContentInfo, although it might show the number in a different
2325 * font size for readability.
2326 */
Joe Onorato8595a3d2010-11-19 18:12:07 -08002327 public Builder setNumber(int number) {
2328 mNumber = number;
2329 return this;
2330 }
2331
Joe Onoratocb109a02011-01-18 17:57:41 -08002332 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002333 * A small piece of additional information pertaining to this notification.
2334 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002335 * The platform template will draw this on the last line of the notification, at the far
2336 * right (to the right of a smallIcon if it has been placed there).
Joe Onoratocb109a02011-01-18 17:57:41 -08002337 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002338 public Builder setContentInfo(CharSequence info) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04002339 mContentInfo = safeCharSequence(info);
Joe Onorato46439ce2010-11-19 13:56:21 -08002340 return this;
2341 }
2342
Joe Onoratocb109a02011-01-18 17:57:41 -08002343 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002344 * Set the progress this notification represents.
2345 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002346 * The platform template will represent this using a {@link ProgressBar}.
Jeff Sharkey1c400132011-08-05 14:50:13 -07002347 */
2348 public Builder setProgress(int max, int progress, boolean indeterminate) {
2349 mProgressMax = max;
2350 mProgress = progress;
2351 mProgressIndeterminate = indeterminate;
2352 return this;
2353 }
2354
2355 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002356 * Supply a custom RemoteViews to use instead of the platform template.
2357 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002358 * @see Notification#contentView
Joe Onoratocb109a02011-01-18 17:57:41 -08002359 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002360 public Builder setContent(RemoteViews views) {
2361 mContentView = views;
2362 return this;
2363 }
2364
Joe Onoratocb109a02011-01-18 17:57:41 -08002365 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002366 * Supply a {@link PendingIntent} to be sent when the notification is clicked.
2367 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002368 * As of {@link android.os.Build.VERSION_CODES#HONEYCOMB}, if this field is unset and you
2369 * have specified a custom RemoteViews with {@link #setContent(RemoteViews)}, you can use
2370 * {@link RemoteViews#setOnClickPendingIntent RemoteViews.setOnClickPendingIntent(int,PendingIntent)}
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002371 * to assign PendingIntents to individual views in that custom layout (i.e., to create
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002372 * clickable buttons inside the notification view).
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002373 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002374 * @see Notification#contentIntent Notification.contentIntent
Joe Onoratocb109a02011-01-18 17:57:41 -08002375 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002376 public Builder setContentIntent(PendingIntent intent) {
2377 mContentIntent = intent;
2378 return this;
2379 }
2380
Joe Onoratocb109a02011-01-18 17:57:41 -08002381 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002382 * Supply a {@link PendingIntent} to send when the notification is cleared explicitly by the user.
2383 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002384 * @see Notification#deleteIntent
Joe Onoratocb109a02011-01-18 17:57:41 -08002385 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002386 public Builder setDeleteIntent(PendingIntent intent) {
2387 mDeleteIntent = intent;
2388 return this;
2389 }
2390
Joe Onoratocb109a02011-01-18 17:57:41 -08002391 /**
2392 * An intent to launch instead of posting the notification to the status bar.
2393 * Only for use with extremely high-priority notifications demanding the user's
2394 * <strong>immediate</strong> attention, such as an incoming phone call or
2395 * alarm clock that the user has explicitly set to a particular time.
2396 * If this facility is used for something else, please give the user an option
2397 * to turn it off and use a normal notification, as this can be extremely
2398 * disruptive.
2399 *
Chris Wren47c20a12014-06-18 17:27:29 -04002400 * <p>
2401 * The system UI may choose to display a heads-up notification, instead of
2402 * launching this intent, while the user is using the device.
2403 * </p>
2404 *
Joe Onoratocb109a02011-01-18 17:57:41 -08002405 * @param intent The pending intent to launch.
2406 * @param highPriority Passing true will cause this notification to be sent
2407 * even if other notifications are suppressed.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002408 *
2409 * @see Notification#fullScreenIntent
Joe Onoratocb109a02011-01-18 17:57:41 -08002410 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002411 public Builder setFullScreenIntent(PendingIntent intent, boolean highPriority) {
2412 mFullScreenIntent = intent;
2413 setFlag(FLAG_HIGH_PRIORITY, highPriority);
2414 return this;
2415 }
2416
Joe Onoratocb109a02011-01-18 17:57:41 -08002417 /**
Dan Sandler5fcdf6e2014-07-18 11:31:15 -04002418 * Set the "ticker" text which is sent to accessibility services.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002419 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002420 * @see Notification#tickerText
Joe Onoratocb109a02011-01-18 17:57:41 -08002421 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002422 public Builder setTicker(CharSequence tickerText) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04002423 mTickerText = safeCharSequence(tickerText);
Joe Onorato46439ce2010-11-19 13:56:21 -08002424 return this;
2425 }
2426
Joe Onoratocb109a02011-01-18 17:57:41 -08002427 /**
Dan Sandler5fcdf6e2014-07-18 11:31:15 -04002428 * Obsolete version of {@link #setTicker(CharSequence)}.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002429 *
Joe Onoratocb109a02011-01-18 17:57:41 -08002430 */
Dan Sandler5fcdf6e2014-07-18 11:31:15 -04002431 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -08002432 public Builder setTicker(CharSequence tickerText, RemoteViews views) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04002433 mTickerText = safeCharSequence(tickerText);
Dan Sandler5fcdf6e2014-07-18 11:31:15 -04002434 mTickerView = views; // we'll save it for you anyway
Joe Onorato46439ce2010-11-19 13:56:21 -08002435 return this;
2436 }
2437
Joe Onoratocb109a02011-01-18 17:57:41 -08002438 /**
Dan Sandlerd63f9322015-05-06 15:18:49 -04002439 * Add a large icon to the notification content view.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002440 *
2441 * In the platform template, this image will be shown on the left of the notification view
Dan Sandlerd63f9322015-05-06 15:18:49 -04002442 * in place of the {@link #setSmallIcon(Icon) small icon} (which will be placed in a small
2443 * badge atop the large icon).
Dan Sandler08a04c12015-05-06 15:18:49 -04002444 */
Dan Sandlerd63f9322015-05-06 15:18:49 -04002445 public Builder setLargeIcon(Bitmap b) {
2446 return setLargeIcon(b != null ? Icon.createWithBitmap(b) : null);
2447 }
2448
2449 /**
2450 * Add a large icon to the notification content view.
2451 *
2452 * In the platform template, this image will be shown on the left of the notification view
2453 * in place of the {@link #setSmallIcon(Icon) small icon} (which will be placed in a small
2454 * badge atop the large icon).
2455 */
2456 public Builder setLargeIcon(Icon icon) {
Joe Onorato46439ce2010-11-19 13:56:21 -08002457 mLargeIcon = icon;
2458 return this;
2459 }
2460
Joe Onoratocb109a02011-01-18 17:57:41 -08002461 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002462 * Set the sound to play.
2463 *
John Spurlockc0650f022014-07-19 13:22:39 -04002464 * It will be played using the {@link #AUDIO_ATTRIBUTES_DEFAULT default audio attributes}
2465 * for notifications.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002466 *
Chris Wren47c20a12014-06-18 17:27:29 -04002467 * <p>
2468 * A notification that is noisy is more likely to be presented as a heads-up notification.
2469 * </p>
2470 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002471 * @see Notification#sound
Joe Onoratocb109a02011-01-18 17:57:41 -08002472 */
Joe Onorato52f80cd2010-11-21 15:34:48 -08002473 public Builder setSound(Uri sound) {
2474 mSound = sound;
John Spurlockc0650f022014-07-19 13:22:39 -04002475 mAudioAttributes = AUDIO_ATTRIBUTES_DEFAULT;
Joe Onorato52f80cd2010-11-21 15:34:48 -08002476 return this;
2477 }
2478
Joe Onoratocb109a02011-01-18 17:57:41 -08002479 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002480 * Set the sound to play, along with a specific stream on which to play it.
Joe Onoratocb109a02011-01-18 17:57:41 -08002481 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002482 * See {@link android.media.AudioManager} for the <code>STREAM_</code> constants.
2483 *
Chris Wren47c20a12014-06-18 17:27:29 -04002484 * <p>
2485 * A notification that is noisy is more likely to be presented as a heads-up notification.
2486 * </p>
John Spurlockc0650f022014-07-19 13:22:39 -04002487 * @deprecated use {@link #setSound(Uri, AudioAttributes)} instead.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002488 * @see Notification#sound
Joe Onoratocb109a02011-01-18 17:57:41 -08002489 */
Jean-Michel Trivi81f871e2014-08-06 16:32:38 -07002490 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -08002491 public Builder setSound(Uri sound, int streamType) {
2492 mSound = sound;
2493 mAudioStreamType = streamType;
2494 return this;
2495 }
2496
Joe Onoratocb109a02011-01-18 17:57:41 -08002497 /**
John Spurlockc0650f022014-07-19 13:22:39 -04002498 * Set the sound to play, along with specific {@link AudioAttributes audio attributes} to
2499 * use during playback.
2500 *
2501 * <p>
2502 * A notification that is noisy is more likely to be presented as a heads-up notification.
2503 * </p>
2504 *
2505 * @see Notification#sound
2506 */
2507 public Builder setSound(Uri sound, AudioAttributes audioAttributes) {
2508 mSound = sound;
2509 mAudioAttributes = audioAttributes;
2510 return this;
2511 }
2512
2513 /**
Joe Onoratocb109a02011-01-18 17:57:41 -08002514 * Set the vibration pattern to use.
2515 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002516 * See {@link android.os.Vibrator#vibrate(long[], int)} for a discussion of the
2517 * <code>pattern</code> parameter.
2518 *
Chris Wren47c20a12014-06-18 17:27:29 -04002519 * <p>
2520 * A notification that vibrates is more likely to be presented as a heads-up notification.
2521 * </p>
2522 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002523 * @see Notification#vibrate
Joe Onoratocb109a02011-01-18 17:57:41 -08002524 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002525 public Builder setVibrate(long[] pattern) {
2526 mVibrate = pattern;
2527 return this;
2528 }
2529
Joe Onoratocb109a02011-01-18 17:57:41 -08002530 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002531 * Set the desired color for the indicator LED on the device, as well as the
2532 * blink duty cycle (specified in milliseconds).
2533 *
2534
2535 * Not all devices will honor all (or even any) of these values.
2536 *
2537
2538 * @see Notification#ledARGB
2539 * @see Notification#ledOnMS
2540 * @see Notification#ledOffMS
Joe Onoratocb109a02011-01-18 17:57:41 -08002541 */
Tor Norbye80756e32015-03-02 09:39:27 -08002542 public Builder setLights(@ColorInt int argb, int onMs, int offMs) {
Joe Onorato46439ce2010-11-19 13:56:21 -08002543 mLedArgb = argb;
2544 mLedOnMs = onMs;
2545 mLedOffMs = offMs;
Joe Onorato46439ce2010-11-19 13:56:21 -08002546 return this;
2547 }
2548
Joe Onoratocb109a02011-01-18 17:57:41 -08002549 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002550 * Set whether this is an "ongoing" notification.
Joe Onoratocb109a02011-01-18 17:57:41 -08002551 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002552
2553 * Ongoing notifications cannot be dismissed by the user, so your application or service
2554 * must take care of canceling them.
2555 *
2556
2557 * They are typically used to indicate a background task that the user is actively engaged
2558 * with (e.g., playing music) or is pending in some way and therefore occupying the device
2559 * (e.g., a file download, sync operation, active network connection).
2560 *
2561
2562 * @see Notification#FLAG_ONGOING_EVENT
2563 * @see Service#setForeground(boolean)
Joe Onoratocb109a02011-01-18 17:57:41 -08002564 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002565 public Builder setOngoing(boolean ongoing) {
2566 setFlag(FLAG_ONGOING_EVENT, ongoing);
2567 return this;
2568 }
2569
Joe Onoratocb109a02011-01-18 17:57:41 -08002570 /**
2571 * Set this flag if you would only like the sound, vibrate
2572 * and ticker to be played if the notification is not already showing.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002573 *
2574 * @see Notification#FLAG_ONLY_ALERT_ONCE
Joe Onoratocb109a02011-01-18 17:57:41 -08002575 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002576 public Builder setOnlyAlertOnce(boolean onlyAlertOnce) {
2577 setFlag(FLAG_ONLY_ALERT_ONCE, onlyAlertOnce);
2578 return this;
2579 }
2580
Joe Onoratocb109a02011-01-18 17:57:41 -08002581 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002582 * Make this notification automatically dismissed when the user touches it. The
2583 * PendingIntent set with {@link #setDeleteIntent} will be sent when this happens.
2584 *
2585 * @see Notification#FLAG_AUTO_CANCEL
Joe Onoratocb109a02011-01-18 17:57:41 -08002586 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002587 public Builder setAutoCancel(boolean autoCancel) {
Joe Onorato281d83f2011-01-04 17:13:10 -08002588 setFlag(FLAG_AUTO_CANCEL, autoCancel);
Joe Onorato46439ce2010-11-19 13:56:21 -08002589 return this;
2590 }
2591
Joe Onoratocb109a02011-01-18 17:57:41 -08002592 /**
Griff Hazendfcb0802014-02-11 12:00:00 -08002593 * Set whether or not this notification should not bridge to other devices.
2594 *
2595 * <p>Some notifications can be bridged to other devices for remote display.
2596 * This hint can be set to recommend this notification not be bridged.
2597 */
2598 public Builder setLocalOnly(boolean localOnly) {
2599 setFlag(FLAG_LOCAL_ONLY, localOnly);
2600 return this;
2601 }
2602
2603 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002604 * Set which notification properties will be inherited from system defaults.
Joe Onoratocb109a02011-01-18 17:57:41 -08002605 * <p>
2606 * The value should be one or more of the following fields combined with
2607 * bitwise-or:
2608 * {@link #DEFAULT_SOUND}, {@link #DEFAULT_VIBRATE}, {@link #DEFAULT_LIGHTS}.
2609 * <p>
2610 * For all default values, use {@link #DEFAULT_ALL}.
2611 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002612 public Builder setDefaults(int defaults) {
2613 mDefaults = defaults;
2614 return this;
2615 }
2616
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002617 /**
2618 * Set the priority of this notification.
2619 *
2620 * @see Notification#priority
2621 */
Tor Norbyed9273d62013-05-30 15:59:53 -07002622 public Builder setPriority(@Priority int pri) {
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002623 mPriority = pri;
2624 return this;
2625 }
Joe Malin8d40d042012-11-05 11:36:40 -08002626
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002627 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -04002628 * Set the notification category.
Joe Malin8d40d042012-11-05 11:36:40 -08002629 *
John Spurlockfd7f1e02014-03-18 16:41:57 -04002630 * @see Notification#category
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002631 */
John Spurlockfd7f1e02014-03-18 16:41:57 -04002632 public Builder setCategory(String category) {
2633 mCategory = category;
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002634 return this;
2635 }
2636
2637 /**
Chris Wrendde75302014-03-26 17:24:15 -04002638 * Add a person that is relevant to this notification.
2639 *
Chris Wrene6c48932014-09-29 17:19:27 -04002640 * <P>
2641 * Depending on user preferences, this annotation may allow the notification to pass
2642 * through interruption filters, and to appear more prominently in the user interface.
2643 * </P>
2644 *
2645 * <P>
2646 * The person should be specified by the {@code String} representation of a
2647 * {@link android.provider.ContactsContract.Contacts#CONTENT_LOOKUP_URI}.
2648 * </P>
2649 *
2650 * <P>The system will also attempt to resolve {@code mailto:} and {@code tel:} schema
2651 * URIs. The path part of these URIs must exist in the contacts database, in the
2652 * appropriate column, or the reference will be discarded as invalid. Telephone schema
2653 * URIs will be resolved by {@link android.provider.ContactsContract.PhoneLookup}.
2654 * </P>
2655 *
2656 * @param uri A URI for the person.
Chris Wrendde75302014-03-26 17:24:15 -04002657 * @see Notification#EXTRA_PEOPLE
2658 */
Chris Wrene6c48932014-09-29 17:19:27 -04002659 public Builder addPerson(String uri) {
2660 mPeople.add(uri);
Chris Wrendde75302014-03-26 17:24:15 -04002661 return this;
2662 }
2663
2664 /**
Griff Hazen5cadc3b2014-05-20 09:55:39 -07002665 * Set this notification to be part of a group of notifications sharing the same key.
2666 * Grouped notifications may display in a cluster or stack on devices which
2667 * support such rendering.
2668 *
2669 * <p>To make this notification the summary for its group, also call
2670 * {@link #setGroupSummary}. A sort order can be specified for group members by using
2671 * {@link #setSortKey}.
2672 * @param groupKey The group key of the group.
2673 * @return this object for method chaining
2674 */
2675 public Builder setGroup(String groupKey) {
2676 mGroupKey = groupKey;
2677 return this;
2678 }
2679
2680 /**
2681 * Set this notification to be the group summary for a group of notifications.
2682 * Grouped notifications may display in a cluster or stack on devices which
2683 * support such rendering. Requires a group key also be set using {@link #setGroup}.
2684 * @param isGroupSummary Whether this notification should be a group summary.
2685 * @return this object for method chaining
2686 */
2687 public Builder setGroupSummary(boolean isGroupSummary) {
2688 setFlag(FLAG_GROUP_SUMMARY, isGroupSummary);
2689 return this;
2690 }
2691
2692 /**
2693 * Set a sort key that orders this notification among other notifications from the
2694 * same package. This can be useful if an external sort was already applied and an app
2695 * would like to preserve this. Notifications will be sorted lexicographically using this
2696 * value, although providing different priorities in addition to providing sort key may
2697 * cause this value to be ignored.
2698 *
2699 * <p>This sort key can also be used to order members of a notification group. See
Griff Hazen9e1379f2014-05-20 12:50:51 -07002700 * {@link #setGroup}.
Griff Hazen5cadc3b2014-05-20 09:55:39 -07002701 *
2702 * @see String#compareTo(String)
2703 */
2704 public Builder setSortKey(String sortKey) {
2705 mSortKey = sortKey;
2706 return this;
2707 }
2708
2709 /**
Griff Hazen720042b2014-02-24 15:46:56 -08002710 * Merge additional metadata into this notification.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002711 *
Griff Hazen720042b2014-02-24 15:46:56 -08002712 * <p>Values within the Bundle will replace existing extras values in this Builder.
2713 *
2714 * @see Notification#extras
2715 */
Griff Hazen959591e2014-05-15 22:26:18 -07002716 public Builder addExtras(Bundle extras) {
2717 if (extras != null) {
2718 if (mExtras == null) {
2719 mExtras = new Bundle(extras);
2720 } else {
2721 mExtras.putAll(extras);
2722 }
Griff Hazen720042b2014-02-24 15:46:56 -08002723 }
2724 return this;
2725 }
2726
2727 /**
2728 * Set metadata for this notification.
2729 *
2730 * <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 -04002731 * current contents are copied into the Notification each time {@link #build()} is
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002732 * called.
2733 *
Griff Hazen720042b2014-02-24 15:46:56 -08002734 * <p>Replaces any existing extras values with those from the provided Bundle.
2735 * Use {@link #addExtras} to merge in metadata instead.
2736 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002737 * @see Notification#extras
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002738 */
Griff Hazen959591e2014-05-15 22:26:18 -07002739 public Builder setExtras(Bundle extras) {
2740 mExtras = extras;
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002741 return this;
2742 }
2743
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002744 /**
Griff Hazen720042b2014-02-24 15:46:56 -08002745 * Get the current metadata Bundle used by this notification Builder.
2746 *
2747 * <p>The returned Bundle is shared with this Builder.
2748 *
2749 * <p>The current contents of this Bundle are copied into the Notification each time
2750 * {@link #build()} is called.
2751 *
2752 * @see Notification#extras
2753 */
2754 public Bundle getExtras() {
2755 if (mExtras == null) {
2756 mExtras = new Bundle();
2757 }
2758 return mExtras;
2759 }
2760
2761 /**
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002762 * Add an action to this notification. Actions are typically displayed by
2763 * the system as a button adjacent to the notification content.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04002764 * <p>
2765 * Every action must have an icon (32dp square and matching the
2766 * <a href="{@docRoot}design/style/iconography.html#action-bar">Holo
2767 * Dark action bar</a> visual style), a textual label, and a {@link PendingIntent}.
2768 * <p>
2769 * A notification in its expanded form can display up to 3 actions, from left to right in
2770 * the order they were added. Actions will not be displayed when the notification is
2771 * collapsed, however, so be sure that any essential functions may be accessed by the user
2772 * in some other way (for example, in the Activity pointed to by {@link #contentIntent}).
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002773 *
2774 * @param icon Resource ID of a drawable that represents the action.
2775 * @param title Text describing the action.
2776 * @param intent PendingIntent to be fired when the action is invoked.
Dan Sandler86647982015-05-13 23:41:13 -04002777 *
2778 * @deprecated Use {@link #addAction(Action)} instead.
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002779 */
Dan Sandler86647982015-05-13 23:41:13 -04002780 @Deprecated
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002781 public Builder addAction(int icon, CharSequence title, PendingIntent intent) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04002782 mActions.add(new Action(icon, safeCharSequence(title), intent));
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002783 return this;
2784 }
2785
Chris Wrenfbd96ba2012-05-01 12:03:58 -04002786 /**
Griff Hazen959591e2014-05-15 22:26:18 -07002787 * Add an action to this notification. Actions are typically displayed by
2788 * the system as a button adjacent to the notification content.
2789 * <p>
2790 * Every action must have an icon (32dp square and matching the
2791 * <a href="{@docRoot}design/style/iconography.html#action-bar">Holo
2792 * Dark action bar</a> visual style), a textual label, and a {@link PendingIntent}.
2793 * <p>
2794 * A notification in its expanded form can display up to 3 actions, from left to right in
2795 * the order they were added. Actions will not be displayed when the notification is
2796 * collapsed, however, so be sure that any essential functions may be accessed by the user
2797 * in some other way (for example, in the Activity pointed to by {@link #contentIntent}).
2798 *
2799 * @param action The action to add.
2800 */
2801 public Builder addAction(Action action) {
2802 mActions.add(action);
2803 return this;
2804 }
2805
2806 /**
Chris Wrenfbd96ba2012-05-01 12:03:58 -04002807 * Add a rich notification style to be applied at build time.
2808 *
2809 * @param style Object responsible for modifying the notification style.
2810 */
2811 public Builder setStyle(Style style) {
2812 if (mStyle != style) {
2813 mStyle = style;
Daniel Sandlerc08dea22012-06-28 08:35:24 -07002814 if (mStyle != null) {
2815 mStyle.setBuilder(this);
2816 }
Chris Wrenfbd96ba2012-05-01 12:03:58 -04002817 }
2818 return this;
2819 }
2820
Dan Sandler0bf2ed82013-12-21 23:33:41 -06002821 /**
2822 * Specify the value of {@link #visibility}.
Griff Hazenb720abe2014-05-20 13:15:30 -07002823 *
Dan Sandler0bf2ed82013-12-21 23:33:41 -06002824 * @param visibility One of {@link #VISIBILITY_PRIVATE} (the default),
2825 * {@link #VISIBILITY_SECRET}, or {@link #VISIBILITY_PUBLIC}.
2826 *
2827 * @return The same Builder.
2828 */
2829 public Builder setVisibility(int visibility) {
2830 mVisibility = visibility;
2831 return this;
2832 }
2833
2834 /**
2835 * Supply a replacement Notification whose contents should be shown in insecure contexts
2836 * (i.e. atop the secure lockscreen). See {@link #visibility} and {@link #VISIBILITY_PUBLIC}.
2837 * @param n A replacement notification, presumably with some or all info redacted.
2838 * @return The same Builder.
2839 */
2840 public Builder setPublicVersion(Notification n) {
2841 mPublicVersion = n;
2842 return this;
2843 }
2844
Griff Hazenb720abe2014-05-20 13:15:30 -07002845 /**
Griff Hazen5cadc3b2014-05-20 09:55:39 -07002846 * Apply an extender to this notification builder. Extenders may be used to add
2847 * metadata or change options on this builder.
2848 */
Griff Hazen61a9e862014-05-22 16:05:19 -07002849 public Builder extend(Extender extender) {
2850 extender.extend(this);
Griff Hazen5cadc3b2014-05-20 09:55:39 -07002851 return this;
2852 }
2853
Dan Sandler4e787062015-06-17 15:09:48 -04002854 /**
2855 * @hide
2856 */
2857 public void setFlag(int mask, boolean value) {
Joe Onorato46439ce2010-11-19 13:56:21 -08002858 if (value) {
2859 mFlags |= mask;
2860 } else {
2861 mFlags &= ~mask;
2862 }
2863 }
2864
Dan Sandler26e81cf2014-05-06 10:01:27 -04002865 /**
2866 * Sets {@link Notification#color}.
2867 *
2868 * @param argb The accent color to use
2869 *
2870 * @return The same Builder.
2871 */
Tor Norbye80756e32015-03-02 09:39:27 -08002872 public Builder setColor(@ColorInt int argb) {
Dan Sandler26e81cf2014-05-06 10:01:27 -04002873 mColor = argb;
2874 return this;
2875 }
2876
Jorim Jaggid05aa3e2014-08-28 17:52:27 +02002877 private Drawable getProfileBadgeDrawable() {
Christoph Studer7ac80e62014-08-04 16:01:57 +02002878 // Note: This assumes that the current user can read the profile badge of the
2879 // originating user.
Svetoslavc7d62f02014-09-04 15:39:54 -07002880 return mContext.getPackageManager().getUserBadgeForDensity(
Kenny Guy8942bcd2014-09-08 21:09:47 +01002881 new UserHandle(mOriginatingUserId), 0);
Jorim Jaggid05aa3e2014-08-28 17:52:27 +02002882 }
2883
2884 private Bitmap getProfileBadge() {
2885 Drawable badge = getProfileBadgeDrawable();
Kenny Guy8a0101b2014-05-08 23:34:12 +01002886 if (badge == null) {
2887 return null;
2888 }
Jorim Jaggid05aa3e2014-08-28 17:52:27 +02002889 final int size = mContext.getResources().getDimensionPixelSize(
2890 R.dimen.notification_badge_size);
2891 Bitmap bitmap = Bitmap.createBitmap(size, size, Bitmap.Config.ARGB_8888);
Kenny Guy8a0101b2014-05-08 23:34:12 +01002892 Canvas canvas = new Canvas(bitmap);
Jorim Jaggid05aa3e2014-08-28 17:52:27 +02002893 badge.setBounds(0, 0, size, size);
Kenny Guy8a0101b2014-05-08 23:34:12 +01002894 badge.draw(canvas);
2895 return bitmap;
2896 }
2897
Kenny Guy98193ea2014-07-24 19:54:37 +01002898 private boolean addProfileBadge(RemoteViews contentView, int resId) {
2899 Bitmap profileBadge = getProfileBadge();
2900
2901 contentView.setViewVisibility(R.id.profile_badge_large_template, View.GONE);
2902 contentView.setViewVisibility(R.id.profile_badge_line2, View.GONE);
2903 contentView.setViewVisibility(R.id.profile_badge_line3, View.GONE);
2904
2905 if (profileBadge != null) {
2906 contentView.setImageViewBitmap(resId, profileBadge);
2907 contentView.setViewVisibility(resId, View.VISIBLE);
2908
2909 // Make sure Line 3 is visible. As badge will be here if there
2910 // is no text to display.
2911 if (resId == R.id.profile_badge_line3) {
2912 contentView.setViewVisibility(R.id.line3, View.VISIBLE);
2913 }
2914 return true;
2915 }
2916 return false;
2917 }
2918
Jorim Jaggid05aa3e2014-08-28 17:52:27 +02002919 private void shrinkLine3Text(RemoteViews contentView) {
2920 float subTextSize = mContext.getResources().getDimensionPixelSize(
2921 R.dimen.notification_subtext_size);
2922 contentView.setTextViewTextSize(R.id.text, TypedValue.COMPLEX_UNIT_PX, subTextSize);
2923 }
2924
Christoph Studerfe718432014-09-01 18:21:18 +02002925 private void unshrinkLine3Text(RemoteViews contentView) {
2926 float regularTextSize = mContext.getResources().getDimensionPixelSize(
2927 com.android.internal.R.dimen.notification_text_size);
2928 contentView.setTextViewTextSize(R.id.text, TypedValue.COMPLEX_UNIT_PX, regularTextSize);
2929 }
2930
2931 private void resetStandardTemplate(RemoteViews contentView) {
2932 removeLargeIconBackground(contentView);
2933 contentView.setViewPadding(R.id.icon, 0, 0, 0, 0);
2934 contentView.setImageViewResource(R.id.icon, 0);
2935 contentView.setInt(R.id.icon, "setBackgroundResource", 0);
2936 contentView.setViewVisibility(R.id.right_icon, View.GONE);
2937 contentView.setInt(R.id.right_icon, "setBackgroundResource", 0);
2938 contentView.setImageViewResource(R.id.right_icon, 0);
2939 contentView.setImageViewResource(R.id.icon, 0);
2940 contentView.setTextViewText(R.id.title, null);
2941 contentView.setTextViewText(R.id.text, null);
2942 unshrinkLine3Text(contentView);
2943 contentView.setTextViewText(R.id.text2, null);
2944 contentView.setViewVisibility(R.id.text2, View.GONE);
2945 contentView.setViewVisibility(R.id.info, View.GONE);
2946 contentView.setViewVisibility(R.id.time, View.GONE);
2947 contentView.setViewVisibility(R.id.line3, View.GONE);
2948 contentView.setViewVisibility(R.id.overflow_divider, View.GONE);
2949 contentView.setViewVisibility(R.id.progress, View.GONE);
Christoph Studerca1db712014-09-10 17:31:33 +02002950 contentView.setViewVisibility(R.id.chronometer, View.GONE);
2951 contentView.setViewVisibility(R.id.time, View.GONE);
Christoph Studerfe718432014-09-01 18:21:18 +02002952 }
2953
Jorim Jaggi445d3c02014-08-19 22:33:42 +02002954 private RemoteViews applyStandardTemplate(int resId) {
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02002955 return applyStandardTemplate(resId, true /* hasProgress */);
2956 }
2957
2958 /**
2959 * @param hasProgress whether the progress bar should be shown and set
2960 */
2961 private RemoteViews applyStandardTemplate(int resId, boolean hasProgress) {
Kenny Guy77320062014-08-27 21:37:15 +01002962 RemoteViews contentView = new BuilderRemoteViews(mContext.getApplicationInfo(), resId);
Dan Sandler539aad42014-08-04 00:43:39 -04002963
Christoph Studerfe718432014-09-01 18:21:18 +02002964 resetStandardTemplate(contentView);
2965
Daniel Sandler9f7936a2012-05-21 16:14:28 -04002966 boolean showLine3 = false;
2967 boolean showLine2 = false;
Kenny Guy98193ea2014-07-24 19:54:37 +01002968 boolean contentTextInLine2 = false;
Dan Sandler190d58d2014-05-15 09:33:39 -04002969
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01002970 if (mLargeIcon != null) {
Dan Sandlerd63f9322015-05-06 15:18:49 -04002971 contentView.setImageViewIcon(R.id.icon, mLargeIcon);
Christoph Studer239f8352014-08-25 15:13:18 +02002972 processLargeLegacyIcon(mLargeIcon, contentView);
Dan Sandlerd63f9322015-05-06 15:18:49 -04002973 contentView.setImageViewIcon(R.id.right_icon, mSmallIcon);
Dan Sandler190d58d2014-05-15 09:33:39 -04002974 contentView.setViewVisibility(R.id.right_icon, View.VISIBLE);
2975 processSmallRightIcon(mSmallIcon, contentView);
2976 } else { // small icon at left
Dan Sandlerd63f9322015-05-06 15:18:49 -04002977 contentView.setImageViewIcon(R.id.icon, mSmallIcon);
Dan Sandler190d58d2014-05-15 09:33:39 -04002978 contentView.setViewVisibility(R.id.icon, View.VISIBLE);
2979 processSmallIconAsLarge(mSmallIcon, contentView);
Joe Onorato561d3852010-11-20 18:09:34 -08002980 }
2981 if (mContentTitle != null) {
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01002982 contentView.setTextViewText(R.id.title, processLegacyText(mContentTitle));
Joe Onorato561d3852010-11-20 18:09:34 -08002983 }
2984 if (mContentText != null) {
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01002985 contentView.setTextViewText(R.id.text, processLegacyText(mContentText));
Daniel Sandler9f7936a2012-05-21 16:14:28 -04002986 showLine3 = true;
Joe Onorato561d3852010-11-20 18:09:34 -08002987 }
2988 if (mContentInfo != null) {
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01002989 contentView.setTextViewText(R.id.info, processLegacyText(mContentInfo));
Jeff Sharkey1c400132011-08-05 14:50:13 -07002990 contentView.setViewVisibility(R.id.info, View.VISIBLE);
Daniel Sandler9f7936a2012-05-21 16:14:28 -04002991 showLine3 = true;
Joe Onorato561d3852010-11-20 18:09:34 -08002992 } else if (mNumber > 0) {
Daniel Sandlerebce0112011-06-16 16:44:51 -04002993 final int tooBig = mContext.getResources().getInteger(
2994 R.integer.status_bar_notification_info_maxnum);
2995 if (mNumber > tooBig) {
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01002996 contentView.setTextViewText(R.id.info, processLegacyText(
2997 mContext.getResources().getString(
2998 R.string.status_bar_notification_info_overflow)));
Joe Onorato059a2f82011-01-04 10:27:01 -08002999 } else {
3000 NumberFormat f = NumberFormat.getIntegerInstance();
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01003001 contentView.setTextViewText(R.id.info, processLegacyText(f.format(mNumber)));
Joe Onorato059a2f82011-01-04 10:27:01 -08003002 }
Jeff Sharkey1c400132011-08-05 14:50:13 -07003003 contentView.setViewVisibility(R.id.info, View.VISIBLE);
Daniel Sandler9f7936a2012-05-21 16:14:28 -04003004 showLine3 = true;
Joe Onorato561d3852010-11-20 18:09:34 -08003005 } else {
3006 contentView.setViewVisibility(R.id.info, View.GONE);
3007 }
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003008
Daniel Sandler9f7936a2012-05-21 16:14:28 -04003009 // Need to show three lines?
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003010 if (mSubText != null) {
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01003011 contentView.setTextViewText(R.id.text, processLegacyText(mSubText));
Daniel Sandler9f7936a2012-05-21 16:14:28 -04003012 if (mContentText != null) {
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01003013 contentView.setTextViewText(R.id.text2, processLegacyText(mContentText));
Daniel Sandler9f7936a2012-05-21 16:14:28 -04003014 contentView.setViewVisibility(R.id.text2, View.VISIBLE);
3015 showLine2 = true;
Kenny Guy98193ea2014-07-24 19:54:37 +01003016 contentTextInLine2 = true;
Daniel Sandler9f7936a2012-05-21 16:14:28 -04003017 } else {
3018 contentView.setViewVisibility(R.id.text2, View.GONE);
3019 }
Jeff Sharkey1c400132011-08-05 14:50:13 -07003020 } else {
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003021 contentView.setViewVisibility(R.id.text2, View.GONE);
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02003022 if (hasProgress && (mProgressMax != 0 || mProgressIndeterminate)) {
Christoph Studeraca4b252014-09-09 15:58:41 +02003023 contentView.setViewVisibility(R.id.progress, View.VISIBLE);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003024 contentView.setProgressBar(
3025 R.id.progress, mProgressMax, mProgress, mProgressIndeterminate);
Jorim Jaggief72a192014-08-26 21:57:46 +02003026 contentView.setProgressBackgroundTintList(
Alan Viverette4a357cd2015-03-18 18:37:18 -07003027 R.id.progress, ColorStateList.valueOf(mContext.getColor(
Jorim Jaggief72a192014-08-26 21:57:46 +02003028 R.color.notification_progress_background_color)));
3029 if (mColor != COLOR_DEFAULT) {
3030 ColorStateList colorStateList = ColorStateList.valueOf(mColor);
3031 contentView.setProgressTintList(R.id.progress, colorStateList);
3032 contentView.setProgressIndeterminateTintList(R.id.progress, colorStateList);
3033 }
Daniel Sandler9f7936a2012-05-21 16:14:28 -04003034 showLine2 = true;
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003035 } else {
3036 contentView.setViewVisibility(R.id.progress, View.GONE);
3037 }
Jeff Sharkey1c400132011-08-05 14:50:13 -07003038 }
Daniel Sandler9f7936a2012-05-21 16:14:28 -04003039 if (showLine2) {
Jorim Jaggi445d3c02014-08-19 22:33:42 +02003040
3041 // need to shrink all the type to make sure everything fits
Jorim Jaggid05aa3e2014-08-28 17:52:27 +02003042 shrinkLine3Text(contentView);
Daniel Sandler9f7936a2012-05-21 16:14:28 -04003043 }
3044
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02003045 if (showsTimeOrChronometer()) {
Daniel Sandlera2985ed2012-04-03 16:42:00 -04003046 if (mUseChronometer) {
3047 contentView.setViewVisibility(R.id.chronometer, View.VISIBLE);
3048 contentView.setLong(R.id.chronometer, "setBase",
3049 mWhen + (SystemClock.elapsedRealtime() - System.currentTimeMillis()));
3050 contentView.setBoolean(R.id.chronometer, "setStarted", true);
3051 } else {
3052 contentView.setViewVisibility(R.id.time, View.VISIBLE);
3053 contentView.setLong(R.id.time, "setTime", mWhen);
3054 }
Joe Onorato561d3852010-11-20 18:09:34 -08003055 }
Daniel Sandler0c890492012-09-12 17:23:10 -07003056
Jorim Jaggi445d3c02014-08-19 22:33:42 +02003057 // Adjust padding depending on line count and font size.
3058 contentView.setViewPadding(R.id.line1, 0, calculateTopPadding(mContext,
Jorim Jaggid05aa3e2014-08-28 17:52:27 +02003059 mHasThreeLines, mContext.getResources().getConfiguration().fontScale),
Jorim Jaggi445d3c02014-08-19 22:33:42 +02003060 0, 0);
3061
Kenny Guy98193ea2014-07-24 19:54:37 +01003062 // We want to add badge to first line of text.
3063 boolean addedBadge = addProfileBadge(contentView,
3064 contentTextInLine2 ? R.id.profile_badge_line2 : R.id.profile_badge_line3);
3065 // If we added the badge to line 3 then we should show line 3.
3066 if (addedBadge && !contentTextInLine2) {
3067 showLine3 = true;
3068 }
3069
3070 // Note getStandardView may hide line 3 again.
Daniel Sandler9f7936a2012-05-21 16:14:28 -04003071 contentView.setViewVisibility(R.id.line3, showLine3 ? View.VISIBLE : View.GONE);
Daniel Sandler6387d2f2012-05-22 13:44:09 -04003072 contentView.setViewVisibility(R.id.overflow_divider, showLine3 ? View.VISIBLE : View.GONE);
Joe Onorato561d3852010-11-20 18:09:34 -08003073 return contentView;
3074 }
3075
Jorim Jaggi445d3c02014-08-19 22:33:42 +02003076 /**
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02003077 * @return true if the built notification will show the time or the chronometer; false
3078 * otherwise
3079 */
3080 private boolean showsTimeOrChronometer() {
3081 return mWhen != 0 && mShowWhen;
3082 }
3083
3084 /**
Jorim Jaggi445d3c02014-08-19 22:33:42 +02003085 * Logic to find out whether the notification is going to have three lines in the contracted
3086 * layout. This is used to adjust the top padding.
3087 *
3088 * @return true if the notification is going to have three lines; false if the notification
3089 * is going to have one or two lines
3090 */
3091 private boolean hasThreeLines() {
Jorim Jaggid05aa3e2014-08-28 17:52:27 +02003092 boolean contentTextInLine2 = mSubText != null && mContentText != null;
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02003093 boolean hasProgress = mStyle == null || mStyle.hasProgress();
Jorim Jaggid05aa3e2014-08-28 17:52:27 +02003094 // If we have content text in line 2, badge goes into line 2, or line 3 otherwise
3095 boolean badgeInLine3 = getProfileBadgeDrawable() != null && !contentTextInLine2;
3096 boolean hasLine3 = mContentText != null || mContentInfo != null || mNumber > 0
3097 || badgeInLine3;
Jorim Jaggi445d3c02014-08-19 22:33:42 +02003098 boolean hasLine2 = (mSubText != null && mContentText != null) ||
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02003099 (hasProgress && mSubText == null
3100 && (mProgressMax != 0 || mProgressIndeterminate));
Jorim Jaggi445d3c02014-08-19 22:33:42 +02003101 return hasLine2 && hasLine3;
3102 }
3103
3104 /**
3105 * @hide
3106 */
3107 public static int calculateTopPadding(Context ctx, boolean hasThreeLines,
3108 float fontScale) {
3109 int padding = ctx.getResources().getDimensionPixelSize(hasThreeLines
3110 ? R.dimen.notification_top_pad_narrow
3111 : R.dimen.notification_top_pad);
3112 int largePadding = ctx.getResources().getDimensionPixelSize(hasThreeLines
3113 ? R.dimen.notification_top_pad_large_text_narrow
3114 : R.dimen.notification_top_pad_large_text);
3115 float largeFactor = (MathUtils.constrain(fontScale, 1.0f, LARGE_TEXT_SCALE) - 1f)
3116 / (LARGE_TEXT_SCALE - 1f);
3117
3118 // Linearly interpolate the padding between large and normal with the font scale ranging
3119 // from 1f to LARGE_TEXT_SCALE
3120 return Math.round((1 - largeFactor) * padding + largeFactor * largePadding);
3121 }
3122
Christoph Studerfe718432014-09-01 18:21:18 +02003123 private void resetStandardTemplateWithActions(RemoteViews big) {
3124 big.setViewVisibility(R.id.actions, View.GONE);
3125 big.setViewVisibility(R.id.action_divider, View.GONE);
3126 big.removeAllViews(R.id.actions);
3127 }
3128
Daniel Sandler96fd7c12012-03-30 16:37:36 -04003129 private RemoteViews applyStandardTemplateWithActions(int layoutId) {
Jorim Jaggi445d3c02014-08-19 22:33:42 +02003130 RemoteViews big = applyStandardTemplate(layoutId);
Daniel Sandler96fd7c12012-03-30 16:37:36 -04003131
Christoph Studerfe718432014-09-01 18:21:18 +02003132 resetStandardTemplateWithActions(big);
3133
Daniel Sandler96fd7c12012-03-30 16:37:36 -04003134 int N = mActions.size();
3135 if (N > 0) {
Daniel Sandler96fd7c12012-03-30 16:37:36 -04003136 big.setViewVisibility(R.id.actions, View.VISIBLE);
Daniel Sandler6387d2f2012-05-22 13:44:09 -04003137 big.setViewVisibility(R.id.action_divider, View.VISIBLE);
Daniel Sandler8680bf82012-05-15 16:52:52 -04003138 if (N>MAX_ACTION_BUTTONS) N=MAX_ACTION_BUTTONS;
Daniel Sandler96fd7c12012-03-30 16:37:36 -04003139 for (int i=0; i<N; i++) {
3140 final RemoteViews button = generateActionButton(mActions.get(i));
Daniel Sandler96fd7c12012-03-30 16:37:36 -04003141 big.addView(R.id.actions, button);
3142 }
3143 }
3144 return big;
3145 }
3146
Joe Onorato46439ce2010-11-19 13:56:21 -08003147 private RemoteViews makeContentView() {
3148 if (mContentView != null) {
3149 return mContentView;
3150 } else {
Jorim Jaggi445d3c02014-08-19 22:33:42 +02003151 return applyStandardTemplate(getBaseLayoutResource());
Joe Onorato46439ce2010-11-19 13:56:21 -08003152 }
3153 }
3154
3155 private RemoteViews makeTickerView() {
3156 if (mTickerView != null) {
3157 return mTickerView;
Joe Onorato46439ce2010-11-19 13:56:21 -08003158 }
Dan Sandler5fcdf6e2014-07-18 11:31:15 -04003159 return null; // tickers are not created by default anymore
Joe Onorato46439ce2010-11-19 13:56:21 -08003160 }
3161
Daniel Sandler96fd7c12012-03-30 16:37:36 -04003162 private RemoteViews makeBigContentView() {
3163 if (mActions.size() == 0) return null;
3164
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01003165 return applyStandardTemplateWithActions(getBigBaseLayoutResource());
Daniel Sandler96fd7c12012-03-30 16:37:36 -04003166 }
3167
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01003168 private RemoteViews makeHeadsUpContentView() {
Chris Wren8fd39ec2014-02-27 17:43:26 -05003169 if (mActions.size() == 0) return null;
3170
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01003171 return applyStandardTemplateWithActions(getBigBaseLayoutResource());
Chris Wren8fd39ec2014-02-27 17:43:26 -05003172 }
3173
3174
Daniel Sandler96fd7c12012-03-30 16:37:36 -04003175 private RemoteViews generateActionButton(Action action) {
Daniel Sandler8680bf82012-05-15 16:52:52 -04003176 final boolean tombstone = (action.actionIntent == null);
Selim Cinekf33b1112015-07-15 17:45:11 -07003177 RemoteViews button = new BuilderRemoteViews(mContext.getApplicationInfo(),
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01003178 tombstone ? getActionTombstoneLayoutResource()
3179 : getActionLayoutResource());
Dan Sandler68079d52015-07-22 10:45:30 -04003180 final Icon ai = action.getIcon();
Dan Sandler912282e2015-07-28 22:49:30 -04003181 button.setTextViewCompoundDrawablesRelative(R.id.action0, ai, null, null, null);
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01003182 button.setTextViewText(R.id.action0, processLegacyText(action.title));
Daniel Sandler8680bf82012-05-15 16:52:52 -04003183 if (!tombstone) {
Daniel Sandlere5518842012-05-10 16:20:40 -04003184 button.setOnClickPendingIntent(R.id.action0, action.actionIntent);
Daniel Sandlere5518842012-05-10 16:20:40 -04003185 }
Daniel Sandler96fd7c12012-03-30 16:37:36 -04003186 button.setContentDescription(R.id.action0, action.title);
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01003187 processLegacyAction(action, button);
Daniel Sandler96fd7c12012-03-30 16:37:36 -04003188 return button;
3189 }
3190
Joe Onoratocb109a02011-01-18 17:57:41 -08003191 /**
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01003192 * @return Whether we are currently building a notification from a legacy (an app that
Alan Viverette3cb07a462014-06-06 14:19:53 -07003193 * doesn't create material notifications by itself) app.
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01003194 */
3195 private boolean isLegacy() {
Dan Sandler26e81cf2014-05-06 10:01:27 -04003196 return mColorUtil != null;
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01003197 }
3198
3199 private void processLegacyAction(Action action, RemoteViews button) {
Dan Sandler68079d52015-07-22 10:45:30 -04003200 if (!isLegacy() || mColorUtil.isGrayscaleIcon(mContext, action.getIcon())) {
Christoph Studer239f8352014-08-25 15:13:18 +02003201 button.setTextViewCompoundDrawablesRelativeColorFilter(R.id.action0, 0,
Alan Viverette4a357cd2015-03-18 18:37:18 -07003202 mContext.getColor(R.color.notification_action_color_filter),
Christoph Studer239f8352014-08-25 15:13:18 +02003203 PorterDuff.Mode.MULTIPLY);
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01003204 }
3205 }
3206
3207 private CharSequence processLegacyText(CharSequence charSequence) {
3208 if (isLegacy()) {
Dan Sandler26e81cf2014-05-06 10:01:27 -04003209 return mColorUtil.invertCharSequenceColors(charSequence);
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01003210 } else {
3211 return charSequence;
3212 }
3213 }
3214
Dan Sandler26e81cf2014-05-06 10:01:27 -04003215 /**
3216 * Apply any necessary background to smallIcons being used in the largeIcon spot.
3217 */
Dan Sandlerd63f9322015-05-06 15:18:49 -04003218 private void processSmallIconAsLarge(Icon largeIcon, RemoteViews contentView) {
Jorim Jaggi92df1f22014-12-16 19:44:41 +01003219 if (!isLegacy()) {
3220 contentView.setDrawableParameters(R.id.icon, false, -1,
3221 0xFFFFFFFF,
3222 PorterDuff.Mode.SRC_ATOP, -1);
Dan Sandler26e81cf2014-05-06 10:01:27 -04003223 applyLargeIconBackground(contentView);
Dan Sandlerd63f9322015-05-06 15:18:49 -04003224 } else {
3225 if (mColorUtil.isGrayscaleIcon(mContext, largeIcon)) {
3226 applyLargeIconBackground(contentView);
3227 }
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01003228 }
3229 }
3230
Dan Sandler26e81cf2014-05-06 10:01:27 -04003231 /**
3232 * Apply any necessary background to a largeIcon if it's a fake smallIcon (that is,
3233 * if it's grayscale).
3234 */
3235 // TODO: also check bounds, transparency, that sort of thing.
Dan Sandlerd63f9322015-05-06 15:18:49 -04003236 private void processLargeLegacyIcon(Icon largeIcon, RemoteViews contentView) {
3237 if (largeIcon != null && isLegacy()
3238 && mColorUtil.isGrayscaleIcon(mContext, largeIcon)) {
Dan Sandler26e81cf2014-05-06 10:01:27 -04003239 applyLargeIconBackground(contentView);
Dan Sandler190d58d2014-05-15 09:33:39 -04003240 } else {
3241 removeLargeIconBackground(contentView);
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01003242 }
3243 }
3244
Dan Sandler26e81cf2014-05-06 10:01:27 -04003245 /**
3246 * Add a colored circle behind the largeIcon slot.
3247 */
3248 private void applyLargeIconBackground(RemoteViews contentView) {
3249 contentView.setInt(R.id.icon, "setBackgroundResource",
Jorim Jaggi445d3c02014-08-19 22:33:42 +02003250 R.drawable.notification_icon_legacy_bg);
Dan Sandler26e81cf2014-05-06 10:01:27 -04003251
3252 contentView.setDrawableParameters(
3253 R.id.icon,
3254 true,
3255 -1,
Jorim Jaggi74419312014-06-10 20:57:21 +02003256 resolveColor(),
Dan Sandler26e81cf2014-05-06 10:01:27 -04003257 PorterDuff.Mode.SRC_ATOP,
3258 -1);
Jorim Jaggi445d3c02014-08-19 22:33:42 +02003259
3260 int padding = mContext.getResources().getDimensionPixelSize(
3261 R.dimen.notification_large_icon_circle_padding);
3262 contentView.setViewPadding(R.id.icon, padding, padding, padding, padding);
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01003263 }
3264
Dan Sandler190d58d2014-05-15 09:33:39 -04003265 private void removeLargeIconBackground(RemoteViews contentView) {
3266 contentView.setInt(R.id.icon, "setBackgroundResource", 0);
3267 }
3268
Dan Sandler26e81cf2014-05-06 10:01:27 -04003269 /**
3270 * Recolor small icons when used in the R.id.right_icon slot.
3271 */
Dan Sandlerd63f9322015-05-06 15:18:49 -04003272 private void processSmallRightIcon(Icon smallIcon, RemoteViews contentView) {
Jorim Jaggi92df1f22014-12-16 19:44:41 +01003273 if (!isLegacy()) {
Dan Sandler190d58d2014-05-15 09:33:39 -04003274 contentView.setDrawableParameters(R.id.right_icon, false, -1,
3275 0xFFFFFFFF,
3276 PorterDuff.Mode.SRC_ATOP, -1);
Jorim Jaggi92df1f22014-12-16 19:44:41 +01003277 }
Dan Sandlerd63f9322015-05-06 15:18:49 -04003278 final boolean gray = isLegacy()
3279 && smallIcon.getType() == Icon.TYPE_RESOURCE
3280 && mColorUtil.isGrayscaleIcon(mContext, smallIcon.getResId());
3281 if (!isLegacy() || gray) {
Dan Sandler190d58d2014-05-15 09:33:39 -04003282 contentView.setInt(R.id.right_icon,
3283 "setBackgroundResource",
3284 R.drawable.notification_icon_legacy_bg);
3285
3286 contentView.setDrawableParameters(
3287 R.id.right_icon,
3288 true,
3289 -1,
Jorim Jaggi74419312014-06-10 20:57:21 +02003290 resolveColor(),
Dan Sandler190d58d2014-05-15 09:33:39 -04003291 PorterDuff.Mode.SRC_ATOP,
3292 -1);
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01003293 }
3294 }
3295
Jorim Jaggi74419312014-06-10 20:57:21 +02003296 private int sanitizeColor() {
3297 if (mColor != COLOR_DEFAULT) {
3298 mColor |= 0xFF000000; // no alpha for custom colors
3299 }
3300 return mColor;
3301 }
3302
Dan Sandler26e81cf2014-05-06 10:01:27 -04003303 private int resolveColor() {
3304 if (mColor == COLOR_DEFAULT) {
Alan Viverette4a357cd2015-03-18 18:37:18 -07003305 return mContext.getColor(R.color.notification_icon_bg_color);
Dan Sandler26e81cf2014-05-06 10:01:27 -04003306 }
3307 return mColor;
3308 }
3309
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01003310 /**
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003311 * Apply the unstyled operations and return a new {@link Notification} object.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04003312 * @hide
Joe Onoratocb109a02011-01-18 17:57:41 -08003313 */
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04003314 public Notification buildUnstyled() {
Joe Onorato46439ce2010-11-19 13:56:21 -08003315 Notification n = new Notification();
3316 n.when = mWhen;
Dan Sandlerd63f9322015-05-06 15:18:49 -04003317 n.mSmallIcon = mSmallIcon;
Dan Sandler3936e7a2015-05-19 20:59:12 -04003318 if (mSmallIcon != null && mSmallIcon.getType() == Icon.TYPE_RESOURCE) {
Dan Sandlerd63f9322015-05-06 15:18:49 -04003319 n.icon = mSmallIcon.getResId();
3320 }
Joe Onorato46439ce2010-11-19 13:56:21 -08003321 n.iconLevel = mSmallIconLevel;
Joe Onorato8595a3d2010-11-19 18:12:07 -08003322 n.number = mNumber;
Dan Sandler26e81cf2014-05-06 10:01:27 -04003323
Jorim Jaggi74419312014-06-10 20:57:21 +02003324 n.color = sanitizeColor();
Dan Sandler26e81cf2014-05-06 10:01:27 -04003325
Christoph Studer4600f9b2014-07-22 22:44:43 +02003326 setBuilderContentView(n, makeContentView());
Joe Onorato46439ce2010-11-19 13:56:21 -08003327 n.contentIntent = mContentIntent;
3328 n.deleteIntent = mDeleteIntent;
3329 n.fullScreenIntent = mFullScreenIntent;
3330 n.tickerText = mTickerText;
3331 n.tickerView = makeTickerView();
Dan Sandlerd63f9322015-05-06 15:18:49 -04003332 n.mLargeIcon = mLargeIcon;
3333 if (mLargeIcon != null && mLargeIcon.getType() == Icon.TYPE_BITMAP) {
3334 n.largeIcon = mLargeIcon.getBitmap();
3335 }
Joe Onorato46439ce2010-11-19 13:56:21 -08003336 n.sound = mSound;
3337 n.audioStreamType = mAudioStreamType;
John Spurlockc0650f022014-07-19 13:22:39 -04003338 n.audioAttributes = mAudioAttributes;
Joe Onorato46439ce2010-11-19 13:56:21 -08003339 n.vibrate = mVibrate;
3340 n.ledARGB = mLedArgb;
3341 n.ledOnMS = mLedOnMs;
3342 n.ledOffMS = mLedOffMs;
3343 n.defaults = mDefaults;
3344 n.flags = mFlags;
Christoph Studer4600f9b2014-07-22 22:44:43 +02003345 setBuilderBigContentView(n, makeBigContentView());
3346 setBuilderHeadsUpContentView(n, makeHeadsUpContentView());
Daniel Sandler26c13432013-04-04 11:01:04 -04003347 if (mLedOnMs != 0 || mLedOffMs != 0) {
Joe Onorato8d0b6552010-11-22 16:09:29 -08003348 n.flags |= FLAG_SHOW_LIGHTS;
3349 }
3350 if ((mDefaults & DEFAULT_LIGHTS) != 0) {
3351 n.flags |= FLAG_SHOW_LIGHTS;
3352 }
John Spurlockfd7f1e02014-03-18 16:41:57 -04003353 n.category = mCategory;
Griff Hazen5cadc3b2014-05-20 09:55:39 -07003354 n.mGroupKey = mGroupKey;
3355 n.mSortKey = mSortKey;
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003356 n.priority = mPriority;
Daniel Sandlera0a938c2012-03-15 08:42:37 -04003357 if (mActions.size() > 0) {
3358 n.actions = new Action[mActions.size()];
3359 mActions.toArray(n.actions);
3360 }
Dan Sandler0bf2ed82013-12-21 23:33:41 -06003361 n.visibility = mVisibility;
3362
3363 if (mPublicVersion != null) {
3364 n.publicVersion = new Notification();
3365 mPublicVersion.cloneInto(n.publicVersion, true);
3366 }
Christoph Studer4600f9b2014-07-22 22:44:43 +02003367 // Note: If you're adding new fields, also update restoreFromNotitification().
Joe Onorato46439ce2010-11-19 13:56:21 -08003368 return n;
3369 }
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003370
3371 /**
Daniel Sandlerf45564e2013-04-15 15:05:08 -04003372 * Capture, in the provided bundle, semantic information used in the construction of
3373 * this Notification object.
3374 * @hide
3375 */
Griff Hazen720042b2014-02-24 15:46:56 -08003376 public void populateExtras(Bundle extras) {
Daniel Sandlerf45564e2013-04-15 15:05:08 -04003377 // Store original information used in the construction of this object
Kenny Guy8942bcd2014-09-08 21:09:47 +01003378 extras.putInt(EXTRA_ORIGINATING_USERID, mOriginatingUserId);
Kenny Guy77320062014-08-27 21:37:15 +01003379 extras.putParcelable(EXTRA_REBUILD_CONTEXT_APPLICATION_INFO,
3380 mContext.getApplicationInfo());
Daniel Sandlerf45564e2013-04-15 15:05:08 -04003381 extras.putCharSequence(EXTRA_TITLE, mContentTitle);
3382 extras.putCharSequence(EXTRA_TEXT, mContentText);
3383 extras.putCharSequence(EXTRA_SUB_TEXT, mSubText);
3384 extras.putCharSequence(EXTRA_INFO_TEXT, mContentInfo);
Dan Sandlerd63f9322015-05-06 15:18:49 -04003385 extras.putParcelable(EXTRA_SMALL_ICON, mSmallIcon);
Daniel Sandlerf45564e2013-04-15 15:05:08 -04003386 extras.putInt(EXTRA_PROGRESS, mProgress);
3387 extras.putInt(EXTRA_PROGRESS_MAX, mProgressMax);
3388 extras.putBoolean(EXTRA_PROGRESS_INDETERMINATE, mProgressIndeterminate);
3389 extras.putBoolean(EXTRA_SHOW_CHRONOMETER, mUseChronometer);
3390 extras.putBoolean(EXTRA_SHOW_WHEN, mShowWhen);
John Spurlockac08a472013-06-10 11:37:08 -04003391 if (mLargeIcon != null) {
3392 extras.putParcelable(EXTRA_LARGE_ICON, mLargeIcon);
3393 }
Chris Wrendde75302014-03-26 17:24:15 -04003394 if (!mPeople.isEmpty()) {
3395 extras.putStringArray(EXTRA_PEOPLE, mPeople.toArray(new String[mPeople.size()]));
3396 }
Christoph Studer4600f9b2014-07-22 22:44:43 +02003397 // NOTE: If you're adding new extras also update restoreFromNotification().
3398 }
3399
3400
3401 /**
3402 * @hide
3403 */
3404 public static void stripForDelivery(Notification n) {
3405 if (!STRIP_AND_REBUILD) {
3406 return;
3407 }
3408
3409 String templateClass = n.extras.getString(EXTRA_TEMPLATE);
3410 // Only strip views for known Styles because we won't know how to
3411 // re-create them otherwise.
3412 boolean stripViews = TextUtils.isEmpty(templateClass) ||
3413 getNotificationStyleClass(templateClass) != null;
3414
3415 boolean isStripped = false;
3416
3417 if (n.largeIcon != null && n.extras.containsKey(EXTRA_LARGE_ICON)) {
3418 // TODO: Would like to check for equality here, but if the notification
3419 // has been cloned, we can't.
3420 n.largeIcon = null;
3421 n.extras.putBoolean(EXTRA_REBUILD_LARGE_ICON, true);
3422 isStripped = true;
3423 }
3424 // Get rid of unmodified BuilderRemoteViews.
3425
3426 if (stripViews &&
3427 n.contentView instanceof BuilderRemoteViews &&
3428 n.extras.getInt(EXTRA_REBUILD_CONTENT_VIEW_ACTION_COUNT, -1) ==
3429 n.contentView.getSequenceNumber()) {
3430 n.contentView = null;
3431 n.extras.putBoolean(EXTRA_REBUILD_CONTENT_VIEW, true);
3432 n.extras.remove(EXTRA_REBUILD_CONTENT_VIEW_ACTION_COUNT);
3433 isStripped = true;
3434 }
3435 if (stripViews &&
3436 n.bigContentView instanceof BuilderRemoteViews &&
3437 n.extras.getInt(EXTRA_REBUILD_BIG_CONTENT_VIEW_ACTION_COUNT, -1) ==
3438 n.bigContentView.getSequenceNumber()) {
3439 n.bigContentView = null;
3440 n.extras.putBoolean(EXTRA_REBUILD_BIG_CONTENT_VIEW, true);
3441 n.extras.remove(EXTRA_REBUILD_BIG_CONTENT_VIEW_ACTION_COUNT);
3442 isStripped = true;
3443 }
3444 if (stripViews &&
3445 n.headsUpContentView instanceof BuilderRemoteViews &&
3446 n.extras.getInt(EXTRA_REBUILD_HEADS_UP_CONTENT_VIEW_ACTION_COUNT, -1) ==
3447 n.headsUpContentView.getSequenceNumber()) {
3448 n.headsUpContentView = null;
3449 n.extras.putBoolean(EXTRA_REBUILD_HEADS_UP_CONTENT_VIEW, true);
3450 n.extras.remove(EXTRA_REBUILD_HEADS_UP_CONTENT_VIEW_ACTION_COUNT);
3451 isStripped = true;
3452 }
3453
3454 if (isStripped) {
3455 n.extras.putBoolean(EXTRA_NEEDS_REBUILD, true);
3456 }
3457 }
3458
3459 /**
3460 * @hide
3461 */
3462 public static Notification rebuild(Context context, Notification n) {
3463 Bundle extras = n.extras;
3464 if (!extras.getBoolean(EXTRA_NEEDS_REBUILD)) return n;
3465 extras.remove(EXTRA_NEEDS_REBUILD);
3466
3467 // Re-create notification context so we can access app resources.
Kenny Guy77320062014-08-27 21:37:15 +01003468 ApplicationInfo applicationInfo = extras.getParcelable(
3469 EXTRA_REBUILD_CONTEXT_APPLICATION_INFO);
Christoph Studer4600f9b2014-07-22 22:44:43 +02003470 Context builderContext;
3471 try {
Kenny Guy77320062014-08-27 21:37:15 +01003472 builderContext = context.createApplicationContext(applicationInfo,
Christoph Studer4600f9b2014-07-22 22:44:43 +02003473 Context.CONTEXT_RESTRICTED);
3474 } catch (NameNotFoundException e) {
Kenny Guy77320062014-08-27 21:37:15 +01003475 Log.e(TAG, "ApplicationInfo " + applicationInfo + " not found");
Christoph Studer4600f9b2014-07-22 22:44:43 +02003476 builderContext = context; // try with our context
3477 }
3478
3479 Builder b = new Builder(builderContext, n);
3480 return b.rebuild();
3481 }
3482
3483 /**
3484 * Rebuilds the notification passed in to the rebuild-constructor
3485 * {@link #Builder(Context, Notification)}.
3486 *
3487 * <p>
3488 * Throws IllegalStateException when invoked on a Builder that isn't in rebuild mode.
3489 *
3490 * @hide
3491 */
3492 private Notification rebuild() {
3493 if (mRebuildNotification == null) {
3494 throw new IllegalStateException("rebuild() only valid when in 'rebuild' mode.");
3495 }
Jorim Jaggid05aa3e2014-08-28 17:52:27 +02003496 mHasThreeLines = hasThreeLines();
Christoph Studer4600f9b2014-07-22 22:44:43 +02003497
3498 Bundle extras = mRebuildNotification.extras;
3499
3500 if (extras.getBoolean(EXTRA_REBUILD_LARGE_ICON)) {
3501 mRebuildNotification.largeIcon = extras.getParcelable(EXTRA_LARGE_ICON);
3502 }
3503 extras.remove(EXTRA_REBUILD_LARGE_ICON);
3504
3505 if (extras.getBoolean(EXTRA_REBUILD_CONTENT_VIEW)) {
3506 setBuilderContentView(mRebuildNotification, makeContentView());
3507 if (mStyle != null) {
3508 mStyle.populateContentView(mRebuildNotification);
3509 }
3510 }
3511 extras.remove(EXTRA_REBUILD_CONTENT_VIEW);
3512
3513 if (extras.getBoolean(EXTRA_REBUILD_BIG_CONTENT_VIEW)) {
3514 setBuilderBigContentView(mRebuildNotification, makeBigContentView());
3515 if (mStyle != null) {
3516 mStyle.populateBigContentView(mRebuildNotification);
3517 }
3518 }
3519 extras.remove(EXTRA_REBUILD_BIG_CONTENT_VIEW);
3520
3521 if (extras.getBoolean(EXTRA_REBUILD_HEADS_UP_CONTENT_VIEW)) {
3522 setBuilderHeadsUpContentView(mRebuildNotification, makeHeadsUpContentView());
3523 if (mStyle != null) {
3524 mStyle.populateHeadsUpContentView(mRebuildNotification);
3525 }
3526 }
3527 extras.remove(EXTRA_REBUILD_HEADS_UP_CONTENT_VIEW);
3528
Jorim Jaggid05aa3e2014-08-28 17:52:27 +02003529 mHasThreeLines = false;
Christoph Studer4600f9b2014-07-22 22:44:43 +02003530 return mRebuildNotification;
3531 }
3532
3533 private static Class<? extends Style> getNotificationStyleClass(String templateClass) {
3534 Class<? extends Style>[] classes = new Class[]{
3535 BigTextStyle.class, BigPictureStyle.class, InboxStyle.class, MediaStyle.class};
3536 for (Class<? extends Style> innerClass : classes) {
3537 if (templateClass.equals(innerClass.getName())) {
3538 return innerClass;
3539 }
3540 }
3541 return null;
3542 }
3543
3544 private void setBuilderContentView(Notification n, RemoteViews contentView) {
3545 n.contentView = contentView;
3546 if (contentView instanceof BuilderRemoteViews) {
3547 mRebuildBundle.putInt(Builder.EXTRA_REBUILD_CONTENT_VIEW_ACTION_COUNT,
3548 contentView.getSequenceNumber());
3549 }
3550 }
3551
3552 private void setBuilderBigContentView(Notification n, RemoteViews bigContentView) {
3553 n.bigContentView = bigContentView;
3554 if (bigContentView instanceof BuilderRemoteViews) {
3555 mRebuildBundle.putInt(Builder.EXTRA_REBUILD_BIG_CONTENT_VIEW_ACTION_COUNT,
3556 bigContentView.getSequenceNumber());
3557 }
3558 }
3559
3560 private void setBuilderHeadsUpContentView(Notification n,
3561 RemoteViews headsUpContentView) {
3562 n.headsUpContentView = headsUpContentView;
3563 if (headsUpContentView instanceof BuilderRemoteViews) {
3564 mRebuildBundle.putInt(Builder.EXTRA_REBUILD_HEADS_UP_CONTENT_VIEW_ACTION_COUNT,
3565 headsUpContentView.getSequenceNumber());
3566 }
3567 }
3568
3569 private void restoreFromNotification(Notification n) {
3570
3571 // Notification fields.
3572 mWhen = n.when;
Dan Sandlerd63f9322015-05-06 15:18:49 -04003573 mSmallIcon = n.mSmallIcon;
Christoph Studer4600f9b2014-07-22 22:44:43 +02003574 mSmallIconLevel = n.iconLevel;
3575 mNumber = n.number;
3576
3577 mColor = n.color;
3578
3579 mContentView = n.contentView;
3580 mDeleteIntent = n.deleteIntent;
3581 mFullScreenIntent = n.fullScreenIntent;
3582 mTickerText = n.tickerText;
3583 mTickerView = n.tickerView;
Dan Sandlerd63f9322015-05-06 15:18:49 -04003584 mLargeIcon = n.mLargeIcon;
Christoph Studer4600f9b2014-07-22 22:44:43 +02003585 mSound = n.sound;
3586 mAudioStreamType = n.audioStreamType;
3587 mAudioAttributes = n.audioAttributes;
3588
3589 mVibrate = n.vibrate;
3590 mLedArgb = n.ledARGB;
3591 mLedOnMs = n.ledOnMS;
3592 mLedOffMs = n.ledOffMS;
3593 mDefaults = n.defaults;
3594 mFlags = n.flags;
3595
3596 mCategory = n.category;
3597 mGroupKey = n.mGroupKey;
3598 mSortKey = n.mSortKey;
3599 mPriority = n.priority;
3600 mActions.clear();
3601 if (n.actions != null) {
3602 Collections.addAll(mActions, n.actions);
3603 }
3604 mVisibility = n.visibility;
3605
3606 mPublicVersion = n.publicVersion;
3607
3608 // Extras.
3609 Bundle extras = n.extras;
Kenny Guy8942bcd2014-09-08 21:09:47 +01003610 mOriginatingUserId = extras.getInt(EXTRA_ORIGINATING_USERID);
Christoph Studer4600f9b2014-07-22 22:44:43 +02003611 mContentTitle = extras.getCharSequence(EXTRA_TITLE);
3612 mContentText = extras.getCharSequence(EXTRA_TEXT);
3613 mSubText = extras.getCharSequence(EXTRA_SUB_TEXT);
3614 mContentInfo = extras.getCharSequence(EXTRA_INFO_TEXT);
Christoph Studer4600f9b2014-07-22 22:44:43 +02003615 mProgress = extras.getInt(EXTRA_PROGRESS);
3616 mProgressMax = extras.getInt(EXTRA_PROGRESS_MAX);
3617 mProgressIndeterminate = extras.getBoolean(EXTRA_PROGRESS_INDETERMINATE);
3618 mUseChronometer = extras.getBoolean(EXTRA_SHOW_CHRONOMETER);
3619 mShowWhen = extras.getBoolean(EXTRA_SHOW_WHEN);
3620 if (extras.containsKey(EXTRA_LARGE_ICON)) {
3621 mLargeIcon = extras.getParcelable(EXTRA_LARGE_ICON);
3622 }
3623 if (extras.containsKey(EXTRA_PEOPLE)) {
3624 mPeople.clear();
3625 Collections.addAll(mPeople, extras.getStringArray(EXTRA_PEOPLE));
3626 }
Daniel Sandlerf45564e2013-04-15 15:05:08 -04003627 }
3628
3629 /**
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003630 * @deprecated Use {@link #build()} instead.
3631 */
3632 @Deprecated
3633 public Notification getNotification() {
3634 return build();
3635 }
3636
3637 /**
3638 * Combine all of the options that have been set and return a new {@link Notification}
3639 * object.
3640 */
3641 public Notification build() {
Jorim Jaggia0d58ae2015-06-03 11:48:13 -07003642 if (mSmallIcon != null) {
3643 mSmallIcon.convertToAshmem();
3644 }
3645 if (mLargeIcon != null) {
3646 mLargeIcon.convertToAshmem();
3647 }
Kenny Guy8942bcd2014-09-08 21:09:47 +01003648 mOriginatingUserId = mContext.getUserId();
Jorim Jaggid05aa3e2014-08-28 17:52:27 +02003649 mHasThreeLines = hasThreeLines();
Christoph Studer943aa672014-08-03 20:31:16 +02003650
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04003651 Notification n = buildUnstyled();
Daniel Sandlerf45564e2013-04-15 15:05:08 -04003652
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003653 if (mStyle != null) {
Jorim Jaggia0d58ae2015-06-03 11:48:13 -07003654 mStyle.purgeResources();
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04003655 n = mStyle.buildStyled(n);
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003656 }
Daniel Sandlerf45564e2013-04-15 15:05:08 -04003657
Christoph Studer4600f9b2014-07-22 22:44:43 +02003658 if (mExtras != null) {
3659 n.extras.putAll(mExtras);
3660 }
3661
3662 if (mRebuildBundle.size() > 0) {
3663 n.extras.putAll(mRebuildBundle);
3664 mRebuildBundle.clear();
3665 }
Daniel Sandlerf45564e2013-04-15 15:05:08 -04003666
Griff Hazen720042b2014-02-24 15:46:56 -08003667 populateExtras(n.extras);
Daniel Sandlerf45564e2013-04-15 15:05:08 -04003668 if (mStyle != null) {
3669 mStyle.addExtras(n.extras);
3670 }
3671
Jorim Jaggid05aa3e2014-08-28 17:52:27 +02003672 mHasThreeLines = false;
Daniel Sandlerf45564e2013-04-15 15:05:08 -04003673 return n;
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003674 }
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05003675
3676 /**
3677 * Apply this Builder to an existing {@link Notification} object.
3678 *
3679 * @hide
3680 */
3681 public Notification buildInto(Notification n) {
Daniel Sandler1a497d32013-04-18 14:52:45 -04003682 build().cloneInto(n, true);
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05003683 return n;
3684 }
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01003685
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01003686 private int getBaseLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07003687 return R.layout.notification_template_material_base;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01003688 }
3689
3690 private int getBigBaseLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07003691 return R.layout.notification_template_material_big_base;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01003692 }
3693
3694 private int getBigPictureLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07003695 return R.layout.notification_template_material_big_picture;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01003696 }
3697
3698 private int getBigTextLayoutResource() {
Jorim Jaggi445d3c02014-08-19 22:33:42 +02003699 return R.layout.notification_template_material_big_text;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01003700 }
3701
3702 private int getInboxLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07003703 return R.layout.notification_template_material_inbox;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01003704 }
3705
3706 private int getActionLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07003707 return R.layout.notification_material_action;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01003708 }
3709
3710 private int getActionTombstoneLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07003711 return R.layout.notification_material_action_tombstone;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01003712 }
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003713 }
3714
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003715 /**
3716 * An object that can apply a rich notification style to a {@link Notification.Builder}
3717 * object.
3718 */
Griff Hazendfcb0802014-02-11 12:00:00 -08003719 public static abstract class Style {
Chris Wrend6297db2012-05-03 16:20:13 -04003720 private CharSequence mBigContentTitle;
Jorim Jaggi457a10d2014-09-08 16:18:23 +02003721
3722 /**
3723 * @hide
3724 */
3725 protected CharSequence mSummaryText = null;
3726
3727 /**
3728 * @hide
3729 */
3730 protected boolean mSummaryTextSet = false;
Chris Wrend6297db2012-05-03 16:20:13 -04003731
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003732 protected Builder mBuilder;
3733
Chris Wrend6297db2012-05-03 16:20:13 -04003734 /**
3735 * Overrides ContentTitle in the big form of the template.
3736 * This defaults to the value passed to setContentTitle().
3737 */
3738 protected void internalSetBigContentTitle(CharSequence title) {
3739 mBigContentTitle = title;
3740 }
3741
3742 /**
3743 * Set the first line of text after the detail section in the big form of the template.
3744 */
3745 protected void internalSetSummaryText(CharSequence cs) {
3746 mSummaryText = cs;
Daniel Sandler619738c2012-06-07 16:33:08 -04003747 mSummaryTextSet = true;
Chris Wrend6297db2012-05-03 16:20:13 -04003748 }
3749
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003750 public void setBuilder(Builder builder) {
3751 if (mBuilder != builder) {
3752 mBuilder = builder;
Daniel Sandlerc08dea22012-06-28 08:35:24 -07003753 if (mBuilder != null) {
3754 mBuilder.setStyle(this);
3755 }
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003756 }
3757 }
3758
Chris Wrend6297db2012-05-03 16:20:13 -04003759 protected void checkBuilder() {
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003760 if (mBuilder == null) {
3761 throw new IllegalArgumentException("Style requires a valid Builder object");
3762 }
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003763 }
Chris Wrend6297db2012-05-03 16:20:13 -04003764
3765 protected RemoteViews getStandardView(int layoutId) {
3766 checkBuilder();
3767
Christoph Studer4600f9b2014-07-22 22:44:43 +02003768 // Nasty.
3769 CharSequence oldBuilderContentTitle = mBuilder.mContentTitle;
Chris Wrend6297db2012-05-03 16:20:13 -04003770 if (mBigContentTitle != null) {
3771 mBuilder.setContentTitle(mBigContentTitle);
3772 }
3773
Chris Wrend6297db2012-05-03 16:20:13 -04003774 RemoteViews contentView = mBuilder.applyStandardTemplateWithActions(layoutId);
3775
Christoph Studer4600f9b2014-07-22 22:44:43 +02003776 mBuilder.mContentTitle = oldBuilderContentTitle;
3777
Chris Wrend6297db2012-05-03 16:20:13 -04003778 if (mBigContentTitle != null && mBigContentTitle.equals("")) {
3779 contentView.setViewVisibility(R.id.line1, View.GONE);
Chris Wren67dc9a02012-05-16 01:03:20 -04003780 } else {
3781 contentView.setViewVisibility(R.id.line1, View.VISIBLE);
Chris Wrend6297db2012-05-03 16:20:13 -04003782 }
3783
Daniel Sandler619738c2012-06-07 16:33:08 -04003784 // The last line defaults to the subtext, but can be replaced by mSummaryText
3785 final CharSequence overflowText =
3786 mSummaryTextSet ? mSummaryText
3787 : mBuilder.mSubText;
3788 if (overflowText != null) {
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01003789 contentView.setTextViewText(R.id.text, mBuilder.processLegacyText(overflowText));
Daniel Sandler619738c2012-06-07 16:33:08 -04003790 contentView.setViewVisibility(R.id.overflow_divider, View.VISIBLE);
Daniel Sandler9f7936a2012-05-21 16:14:28 -04003791 contentView.setViewVisibility(R.id.line3, View.VISIBLE);
Daniel Sandler916ad912012-06-13 12:17:07 -04003792 } else {
Kenny Guy98193ea2014-07-24 19:54:37 +01003793 // Clear text in case we use the line to show the profile badge.
3794 contentView.setTextViewText(R.id.text, "");
Daniel Sandler916ad912012-06-13 12:17:07 -04003795 contentView.setViewVisibility(R.id.overflow_divider, View.GONE);
3796 contentView.setViewVisibility(R.id.line3, View.GONE);
Chris Wrend6297db2012-05-03 16:20:13 -04003797 }
3798
3799 return contentView;
3800 }
3801
Daniel Sandlerf45564e2013-04-15 15:05:08 -04003802 /**
Jorim Jaggi445d3c02014-08-19 22:33:42 +02003803 * Changes the padding of the first line such that the big and small content view have the
3804 * same top padding.
3805 *
3806 * @hide
3807 */
3808 protected void applyTopPadding(RemoteViews contentView) {
3809 int topPadding = Builder.calculateTopPadding(mBuilder.mContext,
Jorim Jaggid05aa3e2014-08-28 17:52:27 +02003810 mBuilder.mHasThreeLines,
Jorim Jaggi445d3c02014-08-19 22:33:42 +02003811 mBuilder.mContext.getResources().getConfiguration().fontScale);
3812 contentView.setViewPadding(R.id.line1, 0, topPadding, 0, 0);
3813 }
3814
3815 /**
Daniel Sandlerf45564e2013-04-15 15:05:08 -04003816 * @hide
3817 */
3818 public void addExtras(Bundle extras) {
3819 if (mSummaryTextSet) {
3820 extras.putCharSequence(EXTRA_SUMMARY_TEXT, mSummaryText);
3821 }
3822 if (mBigContentTitle != null) {
3823 extras.putCharSequence(EXTRA_TITLE_BIG, mBigContentTitle);
3824 }
Chris Wren91ad5632013-06-05 15:05:57 -04003825 extras.putString(EXTRA_TEMPLATE, this.getClass().getName());
Daniel Sandlerf45564e2013-04-15 15:05:08 -04003826 }
3827
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04003828 /**
3829 * @hide
3830 */
Christoph Studer4600f9b2014-07-22 22:44:43 +02003831 protected void restoreFromExtras(Bundle extras) {
3832 if (extras.containsKey(EXTRA_SUMMARY_TEXT)) {
3833 mSummaryText = extras.getCharSequence(EXTRA_SUMMARY_TEXT);
3834 mSummaryTextSet = true;
3835 }
3836 if (extras.containsKey(EXTRA_TITLE_BIG)) {
3837 mBigContentTitle = extras.getCharSequence(EXTRA_TITLE_BIG);
3838 }
3839 }
3840
3841
3842 /**
3843 * @hide
3844 */
3845 public Notification buildStyled(Notification wip) {
3846 populateTickerView(wip);
3847 populateContentView(wip);
3848 populateBigContentView(wip);
3849 populateHeadsUpContentView(wip);
3850 return wip;
3851 }
3852
Daniel Sandler0ec46202015-06-24 01:27:05 -04003853 /**
3854 * @hide
3855 */
Jorim Jaggia0d58ae2015-06-03 11:48:13 -07003856 public void purgeResources() {}
3857
Christoph Studer4600f9b2014-07-22 22:44:43 +02003858 // The following methods are split out so we can re-create notification partially.
3859 /**
3860 * @hide
3861 */
3862 protected void populateTickerView(Notification wip) {}
3863 /**
3864 * @hide
3865 */
3866 protected void populateContentView(Notification wip) {}
3867
3868 /**
3869 * @hide
3870 */
3871 protected void populateBigContentView(Notification wip) {}
3872
3873 /**
3874 * @hide
3875 */
3876 protected void populateHeadsUpContentView(Notification wip) {}
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04003877
3878 /**
3879 * Calls {@link android.app.Notification.Builder#build()} on the Builder this Style is
3880 * attached to.
3881 *
3882 * @return the fully constructed Notification.
3883 */
3884 public Notification build() {
3885 checkBuilder();
3886 return mBuilder.build();
3887 }
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02003888
3889 /**
3890 * @hide
3891 * @return true if the style positions the progress bar on the second line; false if the
3892 * style hides the progress bar
3893 */
3894 protected boolean hasProgress() {
3895 return true;
3896 }
Joe Onorato46439ce2010-11-19 13:56:21 -08003897 }
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003898
3899 /**
Daniel Sandler4dfbe832012-04-11 14:51:46 -04003900 * Helper class for generating large-format notifications that include a large image attachment.
Joe Malin8d40d042012-11-05 11:36:40 -08003901 *
Robert Ly91c5ce32014-06-08 15:37:00 -07003902 * Here's how you'd set the <code>BigPictureStyle</code> on a notification:
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003903 * <pre class="prettyprint">
Robert Ly91c5ce32014-06-08 15:37:00 -07003904 * Notification notif = new Notification.Builder(mContext)
3905 * .setContentTitle(&quot;New photo from &quot; + sender.toString())
3906 * .setContentText(subject)
3907 * .setSmallIcon(R.drawable.new_post)
3908 * .setLargeIcon(aBitmap)
3909 * .setStyle(new Notification.BigPictureStyle()
3910 * .bigPicture(aBigBitmap))
3911 * .build();
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003912 * </pre>
Joe Malin8d40d042012-11-05 11:36:40 -08003913 *
Daniel Sandler4dfbe832012-04-11 14:51:46 -04003914 * @see Notification#bigContentView
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003915 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003916 public static class BigPictureStyle extends Style {
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003917 private Bitmap mPicture;
Dan Sandlerd63f9322015-05-06 15:18:49 -04003918 private Icon mBigLargeIcon;
Chris Wren3745a3d2012-05-22 15:11:52 -04003919 private boolean mBigLargeIconSet = false;
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003920
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003921 public BigPictureStyle() {
3922 }
3923
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003924 public BigPictureStyle(Builder builder) {
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003925 setBuilder(builder);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003926 }
3927
Chris Wrend6297db2012-05-03 16:20:13 -04003928 /**
3929 * Overrides ContentTitle in the big form of the template.
3930 * This defaults to the value passed to setContentTitle().
3931 */
3932 public BigPictureStyle setBigContentTitle(CharSequence title) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04003933 internalSetBigContentTitle(safeCharSequence(title));
Chris Wrend6297db2012-05-03 16:20:13 -04003934 return this;
3935 }
3936
3937 /**
3938 * Set the first line of text after the detail section in the big form of the template.
3939 */
3940 public BigPictureStyle setSummaryText(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04003941 internalSetSummaryText(safeCharSequence(cs));
Chris Wrend6297db2012-05-03 16:20:13 -04003942 return this;
3943 }
3944
Chris Wren0bd664d2012-08-01 13:56:56 -04003945 /**
3946 * Provide the bitmap to be used as the payload for the BigPicture notification.
3947 */
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003948 public BigPictureStyle bigPicture(Bitmap b) {
3949 mPicture = b;
3950 return this;
3951 }
3952
Chris Wren3745a3d2012-05-22 15:11:52 -04003953 /**
Chris Wren3745a3d2012-05-22 15:11:52 -04003954 * Override the large icon when the big notification is shown.
3955 */
3956 public BigPictureStyle bigLargeIcon(Bitmap b) {
Dan Sandlerd63f9322015-05-06 15:18:49 -04003957 return bigLargeIcon(b != null ? Icon.createWithBitmap(b) : null);
3958 }
3959
3960 /**
3961 * Override the large icon when the big notification is shown.
3962 */
3963 public BigPictureStyle bigLargeIcon(Icon icon) {
Chris Wren3745a3d2012-05-22 15:11:52 -04003964 mBigLargeIconSet = true;
Dan Sandlerd63f9322015-05-06 15:18:49 -04003965 mBigLargeIcon = icon;
Chris Wren3745a3d2012-05-22 15:11:52 -04003966 return this;
3967 }
3968
Daniel Sandler0ec46202015-06-24 01:27:05 -04003969 /**
3970 * @hide
3971 */
Jorim Jaggia0d58ae2015-06-03 11:48:13 -07003972 @Override
3973 public void purgeResources() {
3974 super.purgeResources();
3975 if (mPicture != null && mPicture.isMutable()) {
3976 mPicture = mPicture.createAshmemBitmap();
3977 }
3978 if (mBigLargeIcon != null) {
3979 mBigLargeIcon.convertToAshmem();
3980 }
3981 }
Christoph Studer5c510ee2014-12-15 16:32:27 +01003982
Jorim Jaggia0d58ae2015-06-03 11:48:13 -07003983 private RemoteViews makeBigContentView() {
Christoph Studer5c510ee2014-12-15 16:32:27 +01003984 // Replace mLargeIcon with mBigLargeIcon if mBigLargeIconSet
3985 // This covers the following cases:
3986 // 1. mBigLargeIconSet -> mBigLargeIcon (null or non-null) applies, overrides
3987 // mLargeIcon
3988 // 2. !mBigLargeIconSet -> mLargeIcon applies
Dan Sandlerd63f9322015-05-06 15:18:49 -04003989 Icon oldLargeIcon = null;
Christoph Studer5c510ee2014-12-15 16:32:27 +01003990 if (mBigLargeIconSet) {
3991 oldLargeIcon = mBuilder.mLargeIcon;
3992 mBuilder.mLargeIcon = mBigLargeIcon;
3993 }
3994
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01003995 RemoteViews contentView = getStandardView(mBuilder.getBigPictureLayoutResource());
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003996
Christoph Studer5c510ee2014-12-15 16:32:27 +01003997 if (mBigLargeIconSet) {
3998 mBuilder.mLargeIcon = oldLargeIcon;
3999 }
4000
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004001 contentView.setImageViewBitmap(R.id.big_picture, mPicture);
4002
Jorim Jaggi445d3c02014-08-19 22:33:42 +02004003 applyTopPadding(contentView);
4004
Kenny Guy98193ea2014-07-24 19:54:37 +01004005 boolean twoTextLines = mBuilder.mSubText != null && mBuilder.mContentText != null;
4006 mBuilder.addProfileBadge(contentView,
4007 twoTextLines ? R.id.profile_badge_line2 : R.id.profile_badge_line3);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004008 return contentView;
4009 }
4010
Daniel Sandlerf45564e2013-04-15 15:05:08 -04004011 /**
4012 * @hide
4013 */
4014 public void addExtras(Bundle extras) {
4015 super.addExtras(extras);
4016
4017 if (mBigLargeIconSet) {
4018 extras.putParcelable(EXTRA_LARGE_ICON_BIG, mBigLargeIcon);
4019 }
4020 extras.putParcelable(EXTRA_PICTURE, mPicture);
4021 }
4022
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04004023 /**
4024 * @hide
4025 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004026 @Override
Christoph Studer4600f9b2014-07-22 22:44:43 +02004027 protected void restoreFromExtras(Bundle extras) {
4028 super.restoreFromExtras(extras);
4029
4030 if (extras.containsKey(EXTRA_LARGE_ICON_BIG)) {
Christoph Studer5c510ee2014-12-15 16:32:27 +01004031 mBigLargeIconSet = true;
Christoph Studer4600f9b2014-07-22 22:44:43 +02004032 mBigLargeIcon = extras.getParcelable(EXTRA_LARGE_ICON_BIG);
Chris Wren3745a3d2012-05-22 15:11:52 -04004033 }
Christoph Studer4600f9b2014-07-22 22:44:43 +02004034 mPicture = extras.getParcelable(EXTRA_PICTURE);
4035 }
4036
4037 /**
4038 * @hide
4039 */
4040 @Override
4041 public void populateBigContentView(Notification wip) {
4042 mBuilder.setBuilderBigContentView(wip, makeBigContentView());
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004043 }
4044 }
4045
4046 /**
Daniel Sandler4dfbe832012-04-11 14:51:46 -04004047 * Helper class for generating large-format notifications that include a lot of text.
Joe Malin8d40d042012-11-05 11:36:40 -08004048 *
Robert Ly91c5ce32014-06-08 15:37:00 -07004049 * Here's how you'd set the <code>BigTextStyle</code> on a notification:
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004050 * <pre class="prettyprint">
Robert Ly91c5ce32014-06-08 15:37:00 -07004051 * Notification notif = new Notification.Builder(mContext)
4052 * .setContentTitle(&quot;New mail from &quot; + sender.toString())
4053 * .setContentText(subject)
4054 * .setSmallIcon(R.drawable.new_mail)
4055 * .setLargeIcon(aBitmap)
4056 * .setStyle(new Notification.BigTextStyle()
4057 * .bigText(aVeryLongString))
4058 * .build();
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004059 * </pre>
Joe Malin8d40d042012-11-05 11:36:40 -08004060 *
Daniel Sandler4dfbe832012-04-11 14:51:46 -04004061 * @see Notification#bigContentView
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004062 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004063 public static class BigTextStyle extends Style {
Jorim Jaggi457a10d2014-09-08 16:18:23 +02004064
4065 private static final int MAX_LINES = 13;
4066 private static final int LINES_CONSUMED_BY_ACTIONS = 3;
4067 private static final int LINES_CONSUMED_BY_SUMMARY = 2;
4068
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004069 private CharSequence mBigText;
4070
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004071 public BigTextStyle() {
4072 }
4073
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004074 public BigTextStyle(Builder builder) {
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004075 setBuilder(builder);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004076 }
4077
Chris Wrend6297db2012-05-03 16:20:13 -04004078 /**
4079 * Overrides ContentTitle in the big form of the template.
4080 * This defaults to the value passed to setContentTitle().
4081 */
4082 public BigTextStyle setBigContentTitle(CharSequence title) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04004083 internalSetBigContentTitle(safeCharSequence(title));
Chris Wrend6297db2012-05-03 16:20:13 -04004084 return this;
4085 }
4086
4087 /**
4088 * Set the first line of text after the detail section in the big form of the template.
4089 */
4090 public BigTextStyle setSummaryText(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04004091 internalSetSummaryText(safeCharSequence(cs));
Chris Wrend6297db2012-05-03 16:20:13 -04004092 return this;
4093 }
4094
Chris Wren0bd664d2012-08-01 13:56:56 -04004095 /**
4096 * Provide the longer text to be displayed in the big form of the
4097 * template in place of the content text.
4098 */
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004099 public BigTextStyle bigText(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04004100 mBigText = safeCharSequence(cs);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004101 return this;
4102 }
4103
Daniel Sandlerf45564e2013-04-15 15:05:08 -04004104 /**
4105 * @hide
4106 */
4107 public void addExtras(Bundle extras) {
4108 super.addExtras(extras);
4109
Christoph Studer4600f9b2014-07-22 22:44:43 +02004110 extras.putCharSequence(EXTRA_BIG_TEXT, mBigText);
4111 }
4112
4113 /**
4114 * @hide
4115 */
4116 @Override
4117 protected void restoreFromExtras(Bundle extras) {
4118 super.restoreFromExtras(extras);
4119
4120 mBigText = extras.getCharSequence(EXTRA_BIG_TEXT);
Daniel Sandlerf45564e2013-04-15 15:05:08 -04004121 }
4122
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004123 private RemoteViews makeBigContentView() {
Christoph Studer4600f9b2014-07-22 22:44:43 +02004124
4125 // Nasty
4126 CharSequence oldBuilderContentText = mBuilder.mContentText;
Daniel Sandler6387d2f2012-05-22 13:44:09 -04004127 mBuilder.mContentText = null;
Daniel Sandler916ad912012-06-13 12:17:07 -04004128
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01004129 RemoteViews contentView = getStandardView(mBuilder.getBigTextLayoutResource());
Joe Malin8d40d042012-11-05 11:36:40 -08004130
Christoph Studer4600f9b2014-07-22 22:44:43 +02004131 mBuilder.mContentText = oldBuilderContentText;
4132
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01004133 contentView.setTextViewText(R.id.big_text, mBuilder.processLegacyText(mBigText));
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004134 contentView.setViewVisibility(R.id.big_text, View.VISIBLE);
Jorim Jaggi457a10d2014-09-08 16:18:23 +02004135 contentView.setInt(R.id.big_text, "setMaxLines", calculateMaxLines());
Chris Wren3c5f92432012-05-04 16:31:17 -04004136 contentView.setViewVisibility(R.id.text2, View.GONE);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004137
Jorim Jaggi445d3c02014-08-19 22:33:42 +02004138 applyTopPadding(contentView);
4139
Jorim Jaggid05aa3e2014-08-28 17:52:27 +02004140 mBuilder.shrinkLine3Text(contentView);
4141
Kenny Guy98193ea2014-07-24 19:54:37 +01004142 mBuilder.addProfileBadge(contentView, R.id.profile_badge_large_template);
4143
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004144 return contentView;
4145 }
4146
Jorim Jaggi457a10d2014-09-08 16:18:23 +02004147 private int calculateMaxLines() {
4148 int lineCount = MAX_LINES;
4149 boolean hasActions = mBuilder.mActions.size() > 0;
4150 boolean hasSummary = (mSummaryTextSet ? mSummaryText : mBuilder.mSubText) != null;
4151 if (hasActions) {
4152 lineCount -= LINES_CONSUMED_BY_ACTIONS;
4153 }
4154 if (hasSummary) {
4155 lineCount -= LINES_CONSUMED_BY_SUMMARY;
4156 }
4157
4158 // If we have less top padding at the top, we can fit less lines.
4159 if (!mBuilder.mHasThreeLines) {
4160 lineCount--;
4161 }
4162 return lineCount;
4163 }
4164
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04004165 /**
4166 * @hide
4167 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004168 @Override
Christoph Studer4600f9b2014-07-22 22:44:43 +02004169 public void populateBigContentView(Notification wip) {
4170 mBuilder.setBuilderBigContentView(wip, makeBigContentView());
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004171 }
4172 }
Daniel Sandler879c5e02012-04-17 16:46:51 -04004173
4174 /**
4175 * Helper class for generating large-format notifications that include a list of (up to 5) strings.
Joe Malin8d40d042012-11-05 11:36:40 -08004176 *
Robert Ly91c5ce32014-06-08 15:37:00 -07004177 * Here's how you'd set the <code>InboxStyle</code> on a notification:
Daniel Sandler879c5e02012-04-17 16:46:51 -04004178 * <pre class="prettyprint">
Robert Ly91c5ce32014-06-08 15:37:00 -07004179 * Notification notif = new Notification.Builder(mContext)
4180 * .setContentTitle(&quot;5 New mails from &quot; + sender.toString())
4181 * .setContentText(subject)
4182 * .setSmallIcon(R.drawable.new_mail)
4183 * .setLargeIcon(aBitmap)
4184 * .setStyle(new Notification.InboxStyle()
4185 * .addLine(str1)
4186 * .addLine(str2)
4187 * .setContentTitle(&quot;&quot;)
4188 * .setSummaryText(&quot;+3 more&quot;))
4189 * .build();
Daniel Sandler879c5e02012-04-17 16:46:51 -04004190 * </pre>
Joe Malin8d40d042012-11-05 11:36:40 -08004191 *
Daniel Sandler879c5e02012-04-17 16:46:51 -04004192 * @see Notification#bigContentView
4193 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004194 public static class InboxStyle extends Style {
Daniel Sandler879c5e02012-04-17 16:46:51 -04004195 private ArrayList<CharSequence> mTexts = new ArrayList<CharSequence>(5);
4196
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004197 public InboxStyle() {
4198 }
4199
Daniel Sandler879c5e02012-04-17 16:46:51 -04004200 public InboxStyle(Builder builder) {
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004201 setBuilder(builder);
Daniel Sandler879c5e02012-04-17 16:46:51 -04004202 }
4203
Chris Wrend6297db2012-05-03 16:20:13 -04004204 /**
4205 * Overrides ContentTitle in the big form of the template.
4206 * This defaults to the value passed to setContentTitle().
4207 */
4208 public InboxStyle setBigContentTitle(CharSequence title) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04004209 internalSetBigContentTitle(safeCharSequence(title));
Chris Wrend6297db2012-05-03 16:20:13 -04004210 return this;
4211 }
4212
4213 /**
4214 * Set the first line of text after the detail section in the big form of the template.
4215 */
4216 public InboxStyle setSummaryText(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04004217 internalSetSummaryText(safeCharSequence(cs));
Chris Wrend6297db2012-05-03 16:20:13 -04004218 return this;
4219 }
4220
Chris Wren0bd664d2012-08-01 13:56:56 -04004221 /**
4222 * Append a line to the digest section of the Inbox notification.
4223 */
Daniel Sandler879c5e02012-04-17 16:46:51 -04004224 public InboxStyle addLine(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04004225 mTexts.add(safeCharSequence(cs));
Daniel Sandler879c5e02012-04-17 16:46:51 -04004226 return this;
4227 }
4228
Daniel Sandlerf45564e2013-04-15 15:05:08 -04004229 /**
4230 * @hide
4231 */
4232 public void addExtras(Bundle extras) {
4233 super.addExtras(extras);
Christoph Studer4600f9b2014-07-22 22:44:43 +02004234
Daniel Sandlerf45564e2013-04-15 15:05:08 -04004235 CharSequence[] a = new CharSequence[mTexts.size()];
4236 extras.putCharSequenceArray(EXTRA_TEXT_LINES, mTexts.toArray(a));
4237 }
4238
Christoph Studer4600f9b2014-07-22 22:44:43 +02004239 /**
4240 * @hide
4241 */
4242 @Override
4243 protected void restoreFromExtras(Bundle extras) {
4244 super.restoreFromExtras(extras);
4245
4246 mTexts.clear();
4247 if (extras.containsKey(EXTRA_TEXT_LINES)) {
4248 Collections.addAll(mTexts, extras.getCharSequenceArray(EXTRA_TEXT_LINES));
4249 }
4250 }
4251
Daniel Sandler879c5e02012-04-17 16:46:51 -04004252 private RemoteViews makeBigContentView() {
Daniel Sandler619738c2012-06-07 16:33:08 -04004253 // Remove the content text so line3 disappears unless you have a summary
Christoph Studer4600f9b2014-07-22 22:44:43 +02004254
4255 // Nasty
4256 CharSequence oldBuilderContentText = mBuilder.mContentText;
Daniel Sandler619738c2012-06-07 16:33:08 -04004257 mBuilder.mContentText = null;
Christoph Studer4600f9b2014-07-22 22:44:43 +02004258
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01004259 RemoteViews contentView = getStandardView(mBuilder.getInboxLayoutResource());
Daniel Sandler619738c2012-06-07 16:33:08 -04004260
Christoph Studer4600f9b2014-07-22 22:44:43 +02004261 mBuilder.mContentText = oldBuilderContentText;
4262
Chris Wrend6297db2012-05-03 16:20:13 -04004263 contentView.setViewVisibility(R.id.text2, View.GONE);
Daniel Sandler879c5e02012-04-17 16:46:51 -04004264
Chris Wrend6297db2012-05-03 16:20:13 -04004265 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 -04004266 R.id.inbox_text4, R.id.inbox_text5, R.id.inbox_text6};
Chris Wrend6297db2012-05-03 16:20:13 -04004267
Chris Wren4ed80d52012-05-17 09:30:03 -04004268 // Make sure all rows are gone in case we reuse a view.
4269 for (int rowId : rowIds) {
4270 contentView.setViewVisibility(rowId, View.GONE);
4271 }
4272
Jorim Jaggi445d3c02014-08-19 22:33:42 +02004273 final boolean largeText =
4274 mBuilder.mContext.getResources().getConfiguration().fontScale > 1f;
4275 final float subTextSize = mBuilder.mContext.getResources().getDimensionPixelSize(
4276 R.dimen.notification_subtext_size);
Daniel Sandler879c5e02012-04-17 16:46:51 -04004277 int i=0;
4278 while (i < mTexts.size() && i < rowIds.length) {
4279 CharSequence str = mTexts.get(i);
4280 if (str != null && !str.equals("")) {
4281 contentView.setViewVisibility(rowIds[i], View.VISIBLE);
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01004282 contentView.setTextViewText(rowIds[i], mBuilder.processLegacyText(str));
Jorim Jaggi445d3c02014-08-19 22:33:42 +02004283 if (largeText) {
4284 contentView.setTextViewTextSize(rowIds[i], TypedValue.COMPLEX_UNIT_PX,
4285 subTextSize);
4286 }
Daniel Sandler879c5e02012-04-17 16:46:51 -04004287 }
4288 i++;
4289 }
4290
Chris Wren683ab002012-09-20 10:35:54 -04004291 contentView.setViewVisibility(R.id.inbox_end_pad,
4292 mTexts.size() > 0 ? View.VISIBLE : View.GONE);
4293
4294 contentView.setViewVisibility(R.id.inbox_more,
4295 mTexts.size() > rowIds.length ? View.VISIBLE : View.GONE);
Chris Wren29bb6d92012-05-17 18:09:42 -04004296
Jorim Jaggi445d3c02014-08-19 22:33:42 +02004297 applyTopPadding(contentView);
4298
Jorim Jaggid05aa3e2014-08-28 17:52:27 +02004299 mBuilder.shrinkLine3Text(contentView);
4300
Kenny Guy98193ea2014-07-24 19:54:37 +01004301 mBuilder.addProfileBadge(contentView, R.id.profile_badge_large_template);
4302
Daniel Sandler879c5e02012-04-17 16:46:51 -04004303 return contentView;
4304 }
4305
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04004306 /**
4307 * @hide
4308 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004309 @Override
Christoph Studer4600f9b2014-07-22 22:44:43 +02004310 public void populateBigContentView(Notification wip) {
4311 mBuilder.setBuilderBigContentView(wip, makeBigContentView());
Daniel Sandler879c5e02012-04-17 16:46:51 -04004312 }
4313 }
Dan Sandler842dd772014-05-15 09:36:47 -04004314
4315 /**
4316 * Notification style for media playback notifications.
4317 *
4318 * In the expanded form, {@link Notification#bigContentView}, up to 5
4319 * {@link Notification.Action}s specified with
Dan Sandler86647982015-05-13 23:41:13 -04004320 * {@link Notification.Builder#addAction(Action) addAction} will be
Dan Sandler842dd772014-05-15 09:36:47 -04004321 * shown as icon-only pushbuttons, suitable for transport controls. The Bitmap given to
4322 * {@link Notification.Builder#setLargeIcon(android.graphics.Bitmap) setLargeIcon()} will be
4323 * treated as album artwork.
4324 *
4325 * Unlike the other styles provided here, MediaStyle can also modify the standard-size
4326 * {@link Notification#contentView}; by providing action indices to
Christoph Studerfde6f4d2014-12-12 13:23:26 +01004327 * {@link #setShowActionsInCompactView(int...)} you can promote up to 3 actions to be displayed
Dan Sandler842dd772014-05-15 09:36:47 -04004328 * in the standard view alongside the usual content.
4329 *
Bryan Mawhinney6be8de32014-07-18 10:35:12 +01004330 * Notifications created with MediaStyle will have their category set to
4331 * {@link Notification#CATEGORY_TRANSPORT CATEGORY_TRANSPORT} unless you set a different
4332 * category using {@link Notification.Builder#setCategory(String) setCategory()}.
4333 *
Jeff Browndba34ba2014-06-24 20:46:03 -07004334 * Finally, if you attach a {@link android.media.session.MediaSession.Token} using
4335 * {@link android.app.Notification.MediaStyle#setMediaSession(MediaSession.Token)},
Dan Sandler842dd772014-05-15 09:36:47 -04004336 * the System UI can identify this as a notification representing an active media session
4337 * and respond accordingly (by showing album artwork in the lockscreen, for example).
4338 *
4339 * To use this style with your Notification, feed it to
4340 * {@link Notification.Builder#setStyle(android.app.Notification.Style)} like so:
4341 * <pre class="prettyprint">
4342 * Notification noti = new Notification.Builder()
4343 * .setSmallIcon(R.drawable.ic_stat_player)
Christoph Studere935fe92014-11-24 14:18:06 +01004344 * .setContentTitle(&quot;Track title&quot;)
4345 * .setContentText(&quot;Artist - Album&quot;)
4346 * .setLargeIcon(albumArtBitmap))
Bryan Mawhinney6be8de32014-07-18 10:35:12 +01004347 * .setStyle(<b>new Notification.MediaStyle()</b>
4348 * .setMediaSession(mySession))
Dan Sandler842dd772014-05-15 09:36:47 -04004349 * .build();
4350 * </pre>
4351 *
4352 * @see Notification#bigContentView
4353 */
4354 public static class MediaStyle extends Style {
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02004355 static final int MAX_MEDIA_BUTTONS_IN_COMPACT = 3;
Dan Sandler842dd772014-05-15 09:36:47 -04004356 static final int MAX_MEDIA_BUTTONS = 5;
4357
4358 private int[] mActionsToShowInCompact = null;
Jeff Browndba34ba2014-06-24 20:46:03 -07004359 private MediaSession.Token mToken;
Dan Sandler842dd772014-05-15 09:36:47 -04004360
4361 public MediaStyle() {
4362 }
4363
4364 public MediaStyle(Builder builder) {
4365 setBuilder(builder);
4366 }
4367
4368 /**
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02004369 * Request up to 3 actions (by index in the order of addition) to be shown in the compact
Dan Sandler842dd772014-05-15 09:36:47 -04004370 * notification view.
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02004371 *
4372 * @param actions the indices of the actions to show in the compact notification view
Dan Sandler842dd772014-05-15 09:36:47 -04004373 */
4374 public MediaStyle setShowActionsInCompactView(int...actions) {
4375 mActionsToShowInCompact = actions;
4376 return this;
4377 }
4378
4379 /**
Jeff Browndba34ba2014-06-24 20:46:03 -07004380 * Attach a {@link android.media.session.MediaSession.Token} to this Notification
4381 * to provide additional playback information and control to the SystemUI.
Dan Sandler842dd772014-05-15 09:36:47 -04004382 */
Jeff Browndba34ba2014-06-24 20:46:03 -07004383 public MediaStyle setMediaSession(MediaSession.Token token) {
Dan Sandler842dd772014-05-15 09:36:47 -04004384 mToken = token;
4385 return this;
4386 }
4387
Christoph Studer4600f9b2014-07-22 22:44:43 +02004388 /**
4389 * @hide
4390 */
Dan Sandler842dd772014-05-15 09:36:47 -04004391 @Override
4392 public Notification buildStyled(Notification wip) {
Christoph Studer4600f9b2014-07-22 22:44:43 +02004393 super.buildStyled(wip);
Bryan Mawhinney6be8de32014-07-18 10:35:12 +01004394 if (wip.category == null) {
4395 wip.category = Notification.CATEGORY_TRANSPORT;
4396 }
Dan Sandler842dd772014-05-15 09:36:47 -04004397 return wip;
4398 }
4399
Christoph Studer4600f9b2014-07-22 22:44:43 +02004400 /**
4401 * @hide
4402 */
4403 @Override
4404 public void populateContentView(Notification wip) {
4405 mBuilder.setBuilderContentView(wip, makeMediaContentView());
4406 }
4407
4408 /**
4409 * @hide
4410 */
4411 @Override
4412 public void populateBigContentView(Notification wip) {
4413 mBuilder.setBuilderBigContentView(wip, makeMediaBigContentView());
4414 }
4415
Dan Sandler842dd772014-05-15 09:36:47 -04004416 /** @hide */
4417 @Override
4418 public void addExtras(Bundle extras) {
4419 super.addExtras(extras);
4420
4421 if (mToken != null) {
4422 extras.putParcelable(EXTRA_MEDIA_SESSION, mToken);
4423 }
Bryan Mawhinneye191f902014-07-22 12:50:09 +01004424 if (mActionsToShowInCompact != null) {
4425 extras.putIntArray(EXTRA_COMPACT_ACTIONS, mActionsToShowInCompact);
4426 }
Dan Sandler842dd772014-05-15 09:36:47 -04004427 }
4428
Christoph Studer4600f9b2014-07-22 22:44:43 +02004429 /**
4430 * @hide
4431 */
4432 @Override
4433 protected void restoreFromExtras(Bundle extras) {
4434 super.restoreFromExtras(extras);
4435
4436 if (extras.containsKey(EXTRA_MEDIA_SESSION)) {
4437 mToken = extras.getParcelable(EXTRA_MEDIA_SESSION);
4438 }
4439 if (extras.containsKey(EXTRA_COMPACT_ACTIONS)) {
4440 mActionsToShowInCompact = extras.getIntArray(EXTRA_COMPACT_ACTIONS);
4441 }
4442 }
4443
Dan Sandler842dd772014-05-15 09:36:47 -04004444 private RemoteViews generateMediaActionButton(Action action) {
4445 final boolean tombstone = (action.actionIntent == null);
Selim Cinekf33b1112015-07-15 17:45:11 -07004446 RemoteViews button = new BuilderRemoteViews(mBuilder.mContext.getApplicationInfo(),
Alan Viverette3cb07a462014-06-06 14:19:53 -07004447 R.layout.notification_material_media_action);
Dan Sandler68079d52015-07-22 10:45:30 -04004448 button.setImageViewIcon(R.id.action0, action.getIcon());
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02004449 button.setDrawableParameters(R.id.action0, false, -1,
4450 0xFFFFFFFF,
4451 PorterDuff.Mode.SRC_ATOP, -1);
Dan Sandler842dd772014-05-15 09:36:47 -04004452 if (!tombstone) {
4453 button.setOnClickPendingIntent(R.id.action0, action.actionIntent);
4454 }
4455 button.setContentDescription(R.id.action0, action.title);
4456 return button;
4457 }
4458
4459 private RemoteViews makeMediaContentView() {
4460 RemoteViews view = mBuilder.applyStandardTemplate(
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02004461 R.layout.notification_template_material_media, false /* hasProgress */);
Dan Sandler842dd772014-05-15 09:36:47 -04004462
4463 final int numActions = mBuilder.mActions.size();
4464 final int N = mActionsToShowInCompact == null
4465 ? 0
4466 : Math.min(mActionsToShowInCompact.length, MAX_MEDIA_BUTTONS_IN_COMPACT);
4467 if (N > 0) {
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02004468 view.removeAllViews(com.android.internal.R.id.media_actions);
Dan Sandler842dd772014-05-15 09:36:47 -04004469 for (int i = 0; i < N; i++) {
4470 if (i >= numActions) {
4471 throw new IllegalArgumentException(String.format(
4472 "setShowActionsInCompactView: action %d out of bounds (max %d)",
4473 i, numActions - 1));
4474 }
4475
4476 final Action action = mBuilder.mActions.get(mActionsToShowInCompact[i]);
4477 final RemoteViews button = generateMediaActionButton(action);
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02004478 view.addView(com.android.internal.R.id.media_actions, button);
Dan Sandler842dd772014-05-15 09:36:47 -04004479 }
4480 }
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02004481 styleText(view);
4482 hideRightIcon(view);
Dan Sandler842dd772014-05-15 09:36:47 -04004483 return view;
4484 }
4485
4486 private RemoteViews makeMediaBigContentView() {
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02004487 final int actionCount = Math.min(mBuilder.mActions.size(), MAX_MEDIA_BUTTONS);
4488 RemoteViews big = mBuilder.applyStandardTemplate(getBigLayoutResource(actionCount),
4489 false /* hasProgress */);
Dan Sandler842dd772014-05-15 09:36:47 -04004490
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02004491 if (actionCount > 0) {
4492 big.removeAllViews(com.android.internal.R.id.media_actions);
4493 for (int i = 0; i < actionCount; i++) {
Dan Sandler842dd772014-05-15 09:36:47 -04004494 final RemoteViews button = generateMediaActionButton(mBuilder.mActions.get(i));
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02004495 big.addView(com.android.internal.R.id.media_actions, button);
Dan Sandler842dd772014-05-15 09:36:47 -04004496 }
4497 }
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02004498 styleText(big);
4499 hideRightIcon(big);
4500 applyTopPadding(big);
4501 big.setViewVisibility(android.R.id.progress, View.GONE);
Dan Sandler842dd772014-05-15 09:36:47 -04004502 return big;
4503 }
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02004504
4505 private int getBigLayoutResource(int actionCount) {
4506 if (actionCount <= 3) {
4507 return R.layout.notification_template_material_big_media_narrow;
4508 } else {
4509 return R.layout.notification_template_material_big_media;
4510 }
4511 }
4512
4513 private void hideRightIcon(RemoteViews contentView) {
4514 contentView.setViewVisibility(R.id.right_icon, View.GONE);
4515 }
4516
4517 /**
4518 * Applies the special text colors for media notifications to all text views.
4519 */
4520 private void styleText(RemoteViews contentView) {
Alan Viverette4a357cd2015-03-18 18:37:18 -07004521 int primaryColor = mBuilder.mContext.getColor(
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02004522 R.color.notification_media_primary_color);
Alan Viverette4a357cd2015-03-18 18:37:18 -07004523 int secondaryColor = mBuilder.mContext.getColor(
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02004524 R.color.notification_media_secondary_color);
4525 contentView.setTextColor(R.id.title, primaryColor);
4526 if (mBuilder.showsTimeOrChronometer()) {
4527 if (mBuilder.mUseChronometer) {
4528 contentView.setTextColor(R.id.chronometer, secondaryColor);
4529 } else {
4530 contentView.setTextColor(R.id.time, secondaryColor);
4531 }
4532 }
4533 contentView.setTextColor(R.id.text2, secondaryColor);
4534 contentView.setTextColor(R.id.text, secondaryColor);
4535 contentView.setTextColor(R.id.info, secondaryColor);
4536 }
4537
4538 /**
4539 * @hide
4540 */
4541 @Override
4542 protected boolean hasProgress() {
4543 return false;
4544 }
Dan Sandler842dd772014-05-15 09:36:47 -04004545 }
Griff Hazen61a9e862014-05-22 16:05:19 -07004546
Christoph Studer4600f9b2014-07-22 22:44:43 +02004547 // When adding a new Style subclass here, don't forget to update
4548 // Builder.getNotificationStyleClass.
4549
Griff Hazen61a9e862014-05-22 16:05:19 -07004550 /**
4551 * Extender interface for use with {@link Builder#extend}. Extenders may be used to add
4552 * metadata or change options on a notification builder.
4553 */
4554 public interface Extender {
4555 /**
4556 * Apply this extender to a notification builder.
4557 * @param builder the builder to be modified.
4558 * @return the build object for chaining.
4559 */
4560 public Builder extend(Builder builder);
4561 }
4562
4563 /**
4564 * Helper class to add wearable extensions to notifications.
4565 * <p class="note"> See
4566 * <a href="{@docRoot}wear/notifications/creating.html">Creating Notifications
4567 * for Android Wear</a> for more information on how to use this class.
4568 * <p>
4569 * To create a notification with wearable extensions:
4570 * <ol>
4571 * <li>Create a {@link android.app.Notification.Builder}, setting any desired
4572 * properties.
4573 * <li>Create a {@link android.app.Notification.WearableExtender}.
4574 * <li>Set wearable-specific properties using the
4575 * {@code add} and {@code set} methods of {@link android.app.Notification.WearableExtender}.
4576 * <li>Call {@link android.app.Notification.Builder#extend} to apply the extensions to a
4577 * notification.
4578 * <li>Post the notification to the notification system with the
4579 * {@code NotificationManager.notify(...)} methods.
4580 * </ol>
4581 *
4582 * <pre class="prettyprint">
4583 * Notification notif = new Notification.Builder(mContext)
4584 * .setContentTitle(&quot;New mail from &quot; + sender.toString())
4585 * .setContentText(subject)
4586 * .setSmallIcon(R.drawable.new_mail)
4587 * .extend(new Notification.WearableExtender()
4588 * .setContentIcon(R.drawable.new_mail))
4589 * .build();
4590 * NotificationManager notificationManger =
4591 * (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
4592 * notificationManger.notify(0, notif);</pre>
4593 *
4594 * <p>Wearable extensions can be accessed on an existing notification by using the
4595 * {@code WearableExtender(Notification)} constructor,
4596 * and then using the {@code get} methods to access values.
4597 *
4598 * <pre class="prettyprint">
4599 * Notification.WearableExtender wearableExtender = new Notification.WearableExtender(
4600 * notification);
Griff Hazen14f57992014-05-26 09:07:14 -07004601 * List&lt;Notification&gt; pages = wearableExtender.getPages();</pre>
Griff Hazen61a9e862014-05-22 16:05:19 -07004602 */
4603 public static final class WearableExtender implements Extender {
4604 /**
4605 * Sentinel value for an action index that is unset.
4606 */
4607 public static final int UNSET_ACTION_INDEX = -1;
4608
4609 /**
4610 * Size value for use with {@link #setCustomSizePreset} to show this notification with
4611 * default sizing.
4612 * <p>For custom display notifications created using {@link #setDisplayIntent},
Paul Soulosaa4f4bf2015-08-04 11:59:45 -07004613 * the default is {@link #SIZE_MEDIUM}. All other notifications size automatically based
Griff Hazen61a9e862014-05-22 16:05:19 -07004614 * on their content.
4615 */
4616 public static final int SIZE_DEFAULT = 0;
4617
4618 /**
4619 * Size value for use with {@link #setCustomSizePreset} to show this notification
4620 * with an extra small size.
4621 * <p>This value is only applicable for custom display notifications created using
4622 * {@link #setDisplayIntent}.
4623 */
4624 public static final int SIZE_XSMALL = 1;
4625
4626 /**
4627 * Size value for use with {@link #setCustomSizePreset} to show this notification
4628 * with a small size.
4629 * <p>This value is only applicable for custom display notifications created using
4630 * {@link #setDisplayIntent}.
4631 */
4632 public static final int SIZE_SMALL = 2;
4633
4634 /**
4635 * Size value for use with {@link #setCustomSizePreset} to show this notification
4636 * with a medium size.
4637 * <p>This value is only applicable for custom display notifications created using
4638 * {@link #setDisplayIntent}.
4639 */
4640 public static final int SIZE_MEDIUM = 3;
4641
4642 /**
4643 * Size value for use with {@link #setCustomSizePreset} to show this notification
4644 * with a large size.
4645 * <p>This value is only applicable for custom display notifications created using
4646 * {@link #setDisplayIntent}.
4647 */
4648 public static final int SIZE_LARGE = 4;
4649
Griff Hazend5f11f92014-05-27 15:40:09 -07004650 /**
4651 * Size value for use with {@link #setCustomSizePreset} to show this notification
4652 * full screen.
4653 * <p>This value is only applicable for custom display notifications created using
4654 * {@link #setDisplayIntent}.
4655 */
4656 public static final int SIZE_FULL_SCREEN = 5;
4657
Griff Hazen5f2edfc2014-09-29 16:28:44 -07004658 /**
4659 * Sentinel value for use with {@link #setHintScreenTimeout} to keep the screen on for a
4660 * short amount of time when this notification is displayed on the screen. This
4661 * is the default value.
4662 */
4663 public static final int SCREEN_TIMEOUT_SHORT = 0;
4664
4665 /**
4666 * Sentinel value for use with {@link #setHintScreenTimeout} to keep the screen on
4667 * for a longer amount of time when this notification is displayed on the screen.
4668 */
4669 public static final int SCREEN_TIMEOUT_LONG = -1;
4670
Griff Hazen61a9e862014-05-22 16:05:19 -07004671 /** Notification extra which contains wearable extensions */
4672 private static final String EXTRA_WEARABLE_EXTENSIONS = "android.wearable.EXTENSIONS";
4673
Pete Gastaf6781d2014-10-07 15:17:05 -04004674 // Keys within EXTRA_WEARABLE_EXTENSIONS for wearable options.
Griff Hazen61a9e862014-05-22 16:05:19 -07004675 private static final String KEY_ACTIONS = "actions";
4676 private static final String KEY_FLAGS = "flags";
4677 private static final String KEY_DISPLAY_INTENT = "displayIntent";
4678 private static final String KEY_PAGES = "pages";
4679 private static final String KEY_BACKGROUND = "background";
4680 private static final String KEY_CONTENT_ICON = "contentIcon";
4681 private static final String KEY_CONTENT_ICON_GRAVITY = "contentIconGravity";
4682 private static final String KEY_CONTENT_ACTION_INDEX = "contentActionIndex";
4683 private static final String KEY_CUSTOM_SIZE_PRESET = "customSizePreset";
4684 private static final String KEY_CUSTOM_CONTENT_HEIGHT = "customContentHeight";
4685 private static final String KEY_GRAVITY = "gravity";
Griff Hazen5f2edfc2014-09-29 16:28:44 -07004686 private static final String KEY_HINT_SCREEN_TIMEOUT = "hintScreenTimeout";
Griff Hazen61a9e862014-05-22 16:05:19 -07004687
4688 // Flags bitwise-ored to mFlags
4689 private static final int FLAG_CONTENT_INTENT_AVAILABLE_OFFLINE = 0x1;
4690 private static final int FLAG_HINT_HIDE_ICON = 1 << 1;
4691 private static final int FLAG_HINT_SHOW_BACKGROUND_ONLY = 1 << 2;
4692 private static final int FLAG_START_SCROLL_BOTTOM = 1 << 3;
Griff Hazen5f2edfc2014-09-29 16:28:44 -07004693 private static final int FLAG_HINT_AVOID_BACKGROUND_CLIPPING = 1 << 4;
Griff Hazen61a9e862014-05-22 16:05:19 -07004694
4695 // Default value for flags integer
4696 private static final int DEFAULT_FLAGS = FLAG_CONTENT_INTENT_AVAILABLE_OFFLINE;
4697
4698 private static final int DEFAULT_CONTENT_ICON_GRAVITY = Gravity.END;
4699 private static final int DEFAULT_GRAVITY = Gravity.BOTTOM;
4700
4701 private ArrayList<Action> mActions = new ArrayList<Action>();
4702 private int mFlags = DEFAULT_FLAGS;
4703 private PendingIntent mDisplayIntent;
4704 private ArrayList<Notification> mPages = new ArrayList<Notification>();
4705 private Bitmap mBackground;
4706 private int mContentIcon;
4707 private int mContentIconGravity = DEFAULT_CONTENT_ICON_GRAVITY;
4708 private int mContentActionIndex = UNSET_ACTION_INDEX;
4709 private int mCustomSizePreset = SIZE_DEFAULT;
4710 private int mCustomContentHeight;
4711 private int mGravity = DEFAULT_GRAVITY;
Griff Hazen5f2edfc2014-09-29 16:28:44 -07004712 private int mHintScreenTimeout;
Griff Hazen61a9e862014-05-22 16:05:19 -07004713
4714 /**
4715 * Create a {@link android.app.Notification.WearableExtender} with default
4716 * options.
4717 */
4718 public WearableExtender() {
4719 }
4720
4721 public WearableExtender(Notification notif) {
4722 Bundle wearableBundle = notif.extras.getBundle(EXTRA_WEARABLE_EXTENSIONS);
4723 if (wearableBundle != null) {
4724 List<Action> actions = wearableBundle.getParcelableArrayList(KEY_ACTIONS);
4725 if (actions != null) {
4726 mActions.addAll(actions);
4727 }
4728
4729 mFlags = wearableBundle.getInt(KEY_FLAGS, DEFAULT_FLAGS);
4730 mDisplayIntent = wearableBundle.getParcelable(KEY_DISPLAY_INTENT);
4731
4732 Notification[] pages = getNotificationArrayFromBundle(
4733 wearableBundle, KEY_PAGES);
4734 if (pages != null) {
4735 Collections.addAll(mPages, pages);
4736 }
4737
4738 mBackground = wearableBundle.getParcelable(KEY_BACKGROUND);
4739 mContentIcon = wearableBundle.getInt(KEY_CONTENT_ICON);
4740 mContentIconGravity = wearableBundle.getInt(KEY_CONTENT_ICON_GRAVITY,
4741 DEFAULT_CONTENT_ICON_GRAVITY);
4742 mContentActionIndex = wearableBundle.getInt(KEY_CONTENT_ACTION_INDEX,
4743 UNSET_ACTION_INDEX);
4744 mCustomSizePreset = wearableBundle.getInt(KEY_CUSTOM_SIZE_PRESET,
4745 SIZE_DEFAULT);
4746 mCustomContentHeight = wearableBundle.getInt(KEY_CUSTOM_CONTENT_HEIGHT);
4747 mGravity = wearableBundle.getInt(KEY_GRAVITY, DEFAULT_GRAVITY);
Griff Hazen5f2edfc2014-09-29 16:28:44 -07004748 mHintScreenTimeout = wearableBundle.getInt(KEY_HINT_SCREEN_TIMEOUT);
Griff Hazen61a9e862014-05-22 16:05:19 -07004749 }
4750 }
4751
4752 /**
4753 * Apply wearable extensions to a notification that is being built. This is typically
4754 * called by the {@link android.app.Notification.Builder#extend} method of
4755 * {@link android.app.Notification.Builder}.
4756 */
4757 @Override
4758 public Notification.Builder extend(Notification.Builder builder) {
4759 Bundle wearableBundle = new Bundle();
4760
4761 if (!mActions.isEmpty()) {
4762 wearableBundle.putParcelableArrayList(KEY_ACTIONS, mActions);
4763 }
4764 if (mFlags != DEFAULT_FLAGS) {
4765 wearableBundle.putInt(KEY_FLAGS, mFlags);
4766 }
4767 if (mDisplayIntent != null) {
4768 wearableBundle.putParcelable(KEY_DISPLAY_INTENT, mDisplayIntent);
4769 }
4770 if (!mPages.isEmpty()) {
4771 wearableBundle.putParcelableArray(KEY_PAGES, mPages.toArray(
4772 new Notification[mPages.size()]));
4773 }
4774 if (mBackground != null) {
4775 wearableBundle.putParcelable(KEY_BACKGROUND, mBackground);
4776 }
4777 if (mContentIcon != 0) {
4778 wearableBundle.putInt(KEY_CONTENT_ICON, mContentIcon);
4779 }
4780 if (mContentIconGravity != DEFAULT_CONTENT_ICON_GRAVITY) {
4781 wearableBundle.putInt(KEY_CONTENT_ICON_GRAVITY, mContentIconGravity);
4782 }
4783 if (mContentActionIndex != UNSET_ACTION_INDEX) {
4784 wearableBundle.putInt(KEY_CONTENT_ACTION_INDEX,
4785 mContentActionIndex);
4786 }
4787 if (mCustomSizePreset != SIZE_DEFAULT) {
4788 wearableBundle.putInt(KEY_CUSTOM_SIZE_PRESET, mCustomSizePreset);
4789 }
4790 if (mCustomContentHeight != 0) {
4791 wearableBundle.putInt(KEY_CUSTOM_CONTENT_HEIGHT, mCustomContentHeight);
4792 }
4793 if (mGravity != DEFAULT_GRAVITY) {
4794 wearableBundle.putInt(KEY_GRAVITY, mGravity);
4795 }
Griff Hazen5f2edfc2014-09-29 16:28:44 -07004796 if (mHintScreenTimeout != 0) {
4797 wearableBundle.putInt(KEY_HINT_SCREEN_TIMEOUT, mHintScreenTimeout);
4798 }
Griff Hazen61a9e862014-05-22 16:05:19 -07004799
4800 builder.getExtras().putBundle(EXTRA_WEARABLE_EXTENSIONS, wearableBundle);
4801 return builder;
4802 }
4803
4804 @Override
4805 public WearableExtender clone() {
4806 WearableExtender that = new WearableExtender();
4807 that.mActions = new ArrayList<Action>(this.mActions);
4808 that.mFlags = this.mFlags;
4809 that.mDisplayIntent = this.mDisplayIntent;
4810 that.mPages = new ArrayList<Notification>(this.mPages);
4811 that.mBackground = this.mBackground;
4812 that.mContentIcon = this.mContentIcon;
4813 that.mContentIconGravity = this.mContentIconGravity;
4814 that.mContentActionIndex = this.mContentActionIndex;
4815 that.mCustomSizePreset = this.mCustomSizePreset;
4816 that.mCustomContentHeight = this.mCustomContentHeight;
4817 that.mGravity = this.mGravity;
Griff Hazen5f2edfc2014-09-29 16:28:44 -07004818 that.mHintScreenTimeout = this.mHintScreenTimeout;
Griff Hazen61a9e862014-05-22 16:05:19 -07004819 return that;
4820 }
4821
4822 /**
4823 * Add a wearable action to this notification.
4824 *
4825 * <p>When wearable actions are added using this method, the set of actions that
4826 * show on a wearable device splits from devices that only show actions added
4827 * using {@link android.app.Notification.Builder#addAction}. This allows for customization
4828 * of which actions display on different devices.
4829 *
4830 * @param action the action to add to this notification
4831 * @return this object for method chaining
4832 * @see android.app.Notification.Action
4833 */
4834 public WearableExtender addAction(Action action) {
4835 mActions.add(action);
4836 return this;
4837 }
4838
4839 /**
4840 * Adds wearable actions to this notification.
4841 *
4842 * <p>When wearable actions are added using this method, the set of actions that
4843 * show on a wearable device splits from devices that only show actions added
4844 * using {@link android.app.Notification.Builder#addAction}. This allows for customization
4845 * of which actions display on different devices.
4846 *
4847 * @param actions the actions to add to this notification
4848 * @return this object for method chaining
4849 * @see android.app.Notification.Action
4850 */
4851 public WearableExtender addActions(List<Action> actions) {
4852 mActions.addAll(actions);
4853 return this;
4854 }
4855
4856 /**
4857 * Clear all wearable actions present on this builder.
4858 * @return this object for method chaining.
4859 * @see #addAction
4860 */
4861 public WearableExtender clearActions() {
4862 mActions.clear();
4863 return this;
4864 }
4865
4866 /**
4867 * Get the wearable actions present on this notification.
4868 */
4869 public List<Action> getActions() {
4870 return mActions;
4871 }
4872
4873 /**
4874 * Set an intent to launch inside of an activity view when displaying
Griff Hazen14f57992014-05-26 09:07:14 -07004875 * this notification. The {@link PendingIntent} provided should be for an activity.
4876 *
4877 * <pre class="prettyprint">
4878 * Intent displayIntent = new Intent(context, MyDisplayActivity.class);
4879 * PendingIntent displayPendingIntent = PendingIntent.getActivity(context,
4880 * 0, displayIntent, PendingIntent.FLAG_UPDATE_CURRENT);
4881 * Notification notif = new Notification.Builder(context)
4882 * .extend(new Notification.WearableExtender()
4883 * .setDisplayIntent(displayPendingIntent)
4884 * .setCustomSizePreset(Notification.WearableExtender.SIZE_MEDIUM))
4885 * .build();</pre>
4886 *
4887 * <p>The activity to launch needs to allow embedding, must be exported, and
Griff Hazen831ca9d2014-06-17 00:38:38 -07004888 * should have an empty task affinity. It is also recommended to use the device
4889 * default light theme.
Griff Hazen14f57992014-05-26 09:07:14 -07004890 *
4891 * <p>Example AndroidManifest.xml entry:
4892 * <pre class="prettyprint">
4893 * &lt;activity android:name=&quot;com.example.MyDisplayActivity&quot;
4894 * android:exported=&quot;true&quot;
4895 * android:allowEmbedded=&quot;true&quot;
Griff Hazen831ca9d2014-06-17 00:38:38 -07004896 * android:taskAffinity=&quot;&quot;
4897 * android:theme=&quot;@android:style/Theme.DeviceDefault.Light&quot; /&gt;</pre>
Griff Hazen61a9e862014-05-22 16:05:19 -07004898 *
4899 * @param intent the {@link PendingIntent} for an activity
4900 * @return this object for method chaining
4901 * @see android.app.Notification.WearableExtender#getDisplayIntent
4902 */
4903 public WearableExtender setDisplayIntent(PendingIntent intent) {
4904 mDisplayIntent = intent;
4905 return this;
4906 }
4907
4908 /**
4909 * Get the intent to launch inside of an activity view when displaying this
4910 * notification. This {@code PendingIntent} should be for an activity.
4911 */
4912 public PendingIntent getDisplayIntent() {
4913 return mDisplayIntent;
4914 }
4915
4916 /**
4917 * Add an additional page of content to display with this notification. The current
4918 * notification forms the first page, and pages added using this function form
4919 * subsequent pages. This field can be used to separate a notification into multiple
4920 * sections.
4921 *
4922 * @param page the notification to add as another page
4923 * @return this object for method chaining
4924 * @see android.app.Notification.WearableExtender#getPages
4925 */
4926 public WearableExtender addPage(Notification page) {
4927 mPages.add(page);
4928 return this;
4929 }
4930
4931 /**
4932 * Add additional pages of content to display with this notification. The current
4933 * notification forms the first page, and pages added using this function form
4934 * subsequent pages. This field can be used to separate a notification into multiple
4935 * sections.
4936 *
4937 * @param pages a list of notifications
4938 * @return this object for method chaining
4939 * @see android.app.Notification.WearableExtender#getPages
4940 */
4941 public WearableExtender addPages(List<Notification> pages) {
4942 mPages.addAll(pages);
4943 return this;
4944 }
4945
4946 /**
4947 * Clear all additional pages present on this builder.
4948 * @return this object for method chaining.
4949 * @see #addPage
4950 */
4951 public WearableExtender clearPages() {
4952 mPages.clear();
4953 return this;
4954 }
4955
4956 /**
4957 * Get the array of additional pages of content for displaying this notification. The
4958 * current notification forms the first page, and elements within this array form
4959 * subsequent pages. This field can be used to separate a notification into multiple
4960 * sections.
4961 * @return the pages for this notification
4962 */
4963 public List<Notification> getPages() {
4964 return mPages;
4965 }
4966
4967 /**
4968 * Set a background image to be displayed behind the notification content.
4969 * Contrary to the {@link android.app.Notification.BigPictureStyle}, this background
4970 * will work with any notification style.
4971 *
4972 * @param background the background bitmap
4973 * @return this object for method chaining
4974 * @see android.app.Notification.WearableExtender#getBackground
4975 */
4976 public WearableExtender setBackground(Bitmap background) {
4977 mBackground = background;
4978 return this;
4979 }
4980
4981 /**
4982 * Get a background image to be displayed behind the notification content.
4983 * Contrary to the {@link android.app.Notification.BigPictureStyle}, this background
4984 * will work with any notification style.
4985 *
4986 * @return the background image
4987 * @see android.app.Notification.WearableExtender#setBackground
4988 */
4989 public Bitmap getBackground() {
4990 return mBackground;
4991 }
4992
4993 /**
4994 * Set an icon that goes with the content of this notification.
4995 */
4996 public WearableExtender setContentIcon(int icon) {
4997 mContentIcon = icon;
4998 return this;
4999 }
5000
5001 /**
5002 * Get an icon that goes with the content of this notification.
5003 */
5004 public int getContentIcon() {
5005 return mContentIcon;
5006 }
5007
5008 /**
5009 * Set the gravity that the content icon should have within the notification display.
5010 * Supported values include {@link android.view.Gravity#START} and
5011 * {@link android.view.Gravity#END}. The default value is {@link android.view.Gravity#END}.
5012 * @see #setContentIcon
5013 */
5014 public WearableExtender setContentIconGravity(int contentIconGravity) {
5015 mContentIconGravity = contentIconGravity;
5016 return this;
5017 }
5018
5019 /**
5020 * Get the gravity that the content icon should have within the notification display.
5021 * Supported values include {@link android.view.Gravity#START} and
5022 * {@link android.view.Gravity#END}. The default value is {@link android.view.Gravity#END}.
5023 * @see #getContentIcon
5024 */
5025 public int getContentIconGravity() {
5026 return mContentIconGravity;
5027 }
5028
5029 /**
5030 * Set an action from this notification's actions to be clickable with the content of
Griff Hazen14f57992014-05-26 09:07:14 -07005031 * this notification. This action will no longer display separately from the
5032 * notification's content.
5033 *
Griff Hazenca48d352014-05-28 22:37:13 -07005034 * <p>For notifications with multiple pages, child pages can also have content actions
Griff Hazen14f57992014-05-26 09:07:14 -07005035 * set, although the list of available actions comes from the main notification and not
5036 * from the child page's notification.
5037 *
5038 * @param actionIndex The index of the action to hoist onto the current notification page.
5039 * If wearable actions were added to the main notification, this index
5040 * will apply to that list, otherwise it will apply to the regular
5041 * actions list.
Griff Hazen61a9e862014-05-22 16:05:19 -07005042 */
5043 public WearableExtender setContentAction(int actionIndex) {
5044 mContentActionIndex = actionIndex;
5045 return this;
5046 }
5047
5048 /**
Griff Hazenca48d352014-05-28 22:37:13 -07005049 * Get the index of the notification action, if any, that was specified as being clickable
5050 * with the content of this notification. This action will no longer display separately
Griff Hazen14f57992014-05-26 09:07:14 -07005051 * from the notification's content.
Griff Hazen61a9e862014-05-22 16:05:19 -07005052 *
Griff Hazenca48d352014-05-28 22:37:13 -07005053 * <p>For notifications with multiple pages, child pages can also have content actions
Griff Hazen14f57992014-05-26 09:07:14 -07005054 * set, although the list of available actions comes from the main notification and not
5055 * from the child page's notification.
5056 *
5057 * <p>If wearable specific actions were added to the main notification, this index will
5058 * apply to that list, otherwise it will apply to the regular actions list.
Griff Hazenca48d352014-05-28 22:37:13 -07005059 *
5060 * @return the action index or {@link #UNSET_ACTION_INDEX} if no action was selected.
Griff Hazen61a9e862014-05-22 16:05:19 -07005061 */
5062 public int getContentAction() {
5063 return mContentActionIndex;
5064 }
5065
5066 /**
5067 * Set the gravity that this notification should have within the available viewport space.
5068 * Supported values include {@link android.view.Gravity#TOP},
5069 * {@link android.view.Gravity#CENTER_VERTICAL} and {@link android.view.Gravity#BOTTOM}.
5070 * The default value is {@link android.view.Gravity#BOTTOM}.
5071 */
5072 public WearableExtender setGravity(int gravity) {
5073 mGravity = gravity;
5074 return this;
5075 }
5076
5077 /**
5078 * Get the gravity that this notification should have within the available viewport space.
5079 * Supported values include {@link android.view.Gravity#TOP},
5080 * {@link android.view.Gravity#CENTER_VERTICAL} and {@link android.view.Gravity#BOTTOM}.
5081 * The default value is {@link android.view.Gravity#BOTTOM}.
5082 */
5083 public int getGravity() {
5084 return mGravity;
5085 }
5086
5087 /**
5088 * Set the custom size preset for the display of this notification out of the available
5089 * presets found in {@link android.app.Notification.WearableExtender}, e.g.
5090 * {@link #SIZE_LARGE}.
5091 * <p>Some custom size presets are only applicable for custom display notifications created
5092 * using {@link android.app.Notification.WearableExtender#setDisplayIntent}. Check the
5093 * documentation for the preset in question. See also
5094 * {@link #setCustomContentHeight} and {@link #getCustomSizePreset}.
5095 */
5096 public WearableExtender setCustomSizePreset(int sizePreset) {
5097 mCustomSizePreset = sizePreset;
5098 return this;
5099 }
5100
5101 /**
5102 * Get the custom size preset for the display of this notification out of the available
5103 * presets found in {@link android.app.Notification.WearableExtender}, e.g.
5104 * {@link #SIZE_LARGE}.
5105 * <p>Some custom size presets are only applicable for custom display notifications created
5106 * using {@link #setDisplayIntent}. Check the documentation for the preset in question.
5107 * See also {@link #setCustomContentHeight} and {@link #setCustomSizePreset}.
5108 */
5109 public int getCustomSizePreset() {
5110 return mCustomSizePreset;
5111 }
5112
5113 /**
5114 * Set the custom height in pixels for the display of this notification's content.
5115 * <p>This option is only available for custom display notifications created
5116 * using {@link android.app.Notification.WearableExtender#setDisplayIntent}. See also
5117 * {@link android.app.Notification.WearableExtender#setCustomSizePreset} and
5118 * {@link #getCustomContentHeight}.
5119 */
5120 public WearableExtender setCustomContentHeight(int height) {
5121 mCustomContentHeight = height;
5122 return this;
5123 }
5124
5125 /**
5126 * Get the custom height in pixels for the display of this notification's content.
5127 * <p>This option is only available for custom display notifications created
5128 * using {@link #setDisplayIntent}. See also {@link #setCustomSizePreset} and
5129 * {@link #setCustomContentHeight}.
5130 */
5131 public int getCustomContentHeight() {
5132 return mCustomContentHeight;
5133 }
5134
5135 /**
5136 * Set whether the scrolling position for the contents of this notification should start
5137 * at the bottom of the contents instead of the top when the contents are too long to
5138 * display within the screen. Default is false (start scroll at the top).
5139 */
5140 public WearableExtender setStartScrollBottom(boolean startScrollBottom) {
5141 setFlag(FLAG_START_SCROLL_BOTTOM, startScrollBottom);
5142 return this;
5143 }
5144
5145 /**
5146 * Get whether the scrolling position for the contents of this notification should start
5147 * at the bottom of the contents instead of the top when the contents are too long to
5148 * display within the screen. Default is false (start scroll at the top).
5149 */
5150 public boolean getStartScrollBottom() {
5151 return (mFlags & FLAG_START_SCROLL_BOTTOM) != 0;
5152 }
5153
5154 /**
5155 * Set whether the content intent is available when the wearable device is not connected
5156 * to a companion device. The user can still trigger this intent when the wearable device
5157 * is offline, but a visual hint will indicate that the content intent may not be available.
5158 * Defaults to true.
5159 */
5160 public WearableExtender setContentIntentAvailableOffline(
5161 boolean contentIntentAvailableOffline) {
5162 setFlag(FLAG_CONTENT_INTENT_AVAILABLE_OFFLINE, contentIntentAvailableOffline);
5163 return this;
5164 }
5165
5166 /**
5167 * Get whether the content intent is available when the wearable device is not connected
5168 * to a companion device. The user can still trigger this intent when the wearable device
5169 * is offline, but a visual hint will indicate that the content intent may not be available.
5170 * Defaults to true.
5171 */
5172 public boolean getContentIntentAvailableOffline() {
5173 return (mFlags & FLAG_CONTENT_INTENT_AVAILABLE_OFFLINE) != 0;
5174 }
5175
5176 /**
5177 * Set a hint that this notification's icon should not be displayed.
5178 * @param hintHideIcon {@code true} to hide the icon, {@code false} otherwise.
5179 * @return this object for method chaining
5180 */
5181 public WearableExtender setHintHideIcon(boolean hintHideIcon) {
5182 setFlag(FLAG_HINT_HIDE_ICON, hintHideIcon);
5183 return this;
5184 }
5185
5186 /**
5187 * Get a hint that this notification's icon should not be displayed.
5188 * @return {@code true} if this icon should not be displayed, false otherwise.
5189 * The default value is {@code false} if this was never set.
5190 */
5191 public boolean getHintHideIcon() {
5192 return (mFlags & FLAG_HINT_HIDE_ICON) != 0;
5193 }
5194
5195 /**
5196 * Set a visual hint that only the background image of this notification should be
5197 * displayed, and other semantic content should be hidden. This hint is only applicable
5198 * to sub-pages added using {@link #addPage}.
5199 */
5200 public WearableExtender setHintShowBackgroundOnly(boolean hintShowBackgroundOnly) {
5201 setFlag(FLAG_HINT_SHOW_BACKGROUND_ONLY, hintShowBackgroundOnly);
5202 return this;
5203 }
5204
5205 /**
5206 * Get a visual hint that only the background image of this notification should be
5207 * displayed, and other semantic content should be hidden. This hint is only applicable
5208 * to sub-pages added using {@link android.app.Notification.WearableExtender#addPage}.
5209 */
5210 public boolean getHintShowBackgroundOnly() {
5211 return (mFlags & FLAG_HINT_SHOW_BACKGROUND_ONLY) != 0;
5212 }
5213
Griff Hazen5f2edfc2014-09-29 16:28:44 -07005214 /**
Griff Hazen9c5be4e2014-11-17 17:47:50 -08005215 * Set a hint that this notification's background should not be clipped if possible,
5216 * and should instead be resized to fully display on the screen, retaining the aspect
5217 * ratio of the image. This can be useful for images like barcodes or qr codes.
Griff Hazen5f2edfc2014-09-29 16:28:44 -07005218 * @param hintAvoidBackgroundClipping {@code true} to avoid clipping if possible.
5219 * @return this object for method chaining
5220 */
5221 public WearableExtender setHintAvoidBackgroundClipping(
5222 boolean hintAvoidBackgroundClipping) {
5223 setFlag(FLAG_HINT_AVOID_BACKGROUND_CLIPPING, hintAvoidBackgroundClipping);
5224 return this;
5225 }
5226
5227 /**
Griff Hazen9c5be4e2014-11-17 17:47:50 -08005228 * Get a hint that this notification's background should not be clipped if possible,
5229 * and should instead be resized to fully display on the screen, retaining the aspect
5230 * ratio of the image. This can be useful for images like barcodes or qr codes.
Griff Hazen5f2edfc2014-09-29 16:28:44 -07005231 * @return {@code true} if it's ok if the background is clipped on the screen, false
5232 * otherwise. The default value is {@code false} if this was never set.
5233 */
5234 public boolean getHintAvoidBackgroundClipping() {
5235 return (mFlags & FLAG_HINT_AVOID_BACKGROUND_CLIPPING) != 0;
5236 }
5237
5238 /**
5239 * Set a hint that the screen should remain on for at least this duration when
5240 * this notification is displayed on the screen.
5241 * @param timeout The requested screen timeout in milliseconds. Can also be either
5242 * {@link #SCREEN_TIMEOUT_SHORT} or {@link #SCREEN_TIMEOUT_LONG}.
5243 * @return this object for method chaining
5244 */
5245 public WearableExtender setHintScreenTimeout(int timeout) {
5246 mHintScreenTimeout = timeout;
5247 return this;
5248 }
5249
5250 /**
5251 * Get the duration, in milliseconds, that the screen should remain on for
5252 * when this notification is displayed.
5253 * @return the duration in milliseconds if > 0, or either one of the sentinel values
5254 * {@link #SCREEN_TIMEOUT_SHORT} or {@link #SCREEN_TIMEOUT_LONG}.
5255 */
5256 public int getHintScreenTimeout() {
5257 return mHintScreenTimeout;
5258 }
5259
Griff Hazen61a9e862014-05-22 16:05:19 -07005260 private void setFlag(int mask, boolean value) {
5261 if (value) {
5262 mFlags |= mask;
5263 } else {
5264 mFlags &= ~mask;
5265 }
5266 }
5267 }
5268
5269 /**
Zhen Yu Song9d5528b2014-11-14 15:34:39 -08005270 * <p>Helper class to add Android Auto extensions to notifications. To create a notification
5271 * with car extensions:
5272 *
5273 * <ol>
5274 * <li>Create an {@link Notification.Builder}, setting any desired
5275 * properties.
5276 * <li>Create a {@link CarExtender}.
5277 * <li>Set car-specific properties using the {@code add} and {@code set} methods of
5278 * {@link CarExtender}.
5279 * <li>Call {@link Notification.Builder#extend(Notification.Extender)}
5280 * to apply the extensions to a notification.
5281 * </ol>
5282 *
5283 * <pre class="prettyprint">
5284 * Notification notification = new Notification.Builder(context)
5285 * ...
5286 * .extend(new CarExtender()
5287 * .set*(...))
5288 * .build();
5289 * </pre>
5290 *
5291 * <p>Car extensions can be accessed on an existing notification by using the
5292 * {@code CarExtender(Notification)} constructor, and then using the {@code get} methods
5293 * to access values.
5294 */
5295 public static final class CarExtender implements Extender {
5296 private static final String TAG = "CarExtender";
5297
5298 private static final String EXTRA_CAR_EXTENDER = "android.car.EXTENSIONS";
5299 private static final String EXTRA_LARGE_ICON = "large_icon";
5300 private static final String EXTRA_CONVERSATION = "car_conversation";
5301 private static final String EXTRA_COLOR = "app_color";
5302
5303 private Bitmap mLargeIcon;
5304 private UnreadConversation mUnreadConversation;
5305 private int mColor = Notification.COLOR_DEFAULT;
5306
5307 /**
5308 * Create a {@link CarExtender} with default options.
5309 */
5310 public CarExtender() {
5311 }
5312
5313 /**
5314 * Create a {@link CarExtender} from the CarExtender options of an existing Notification.
5315 *
5316 * @param notif The notification from which to copy options.
5317 */
5318 public CarExtender(Notification notif) {
5319 Bundle carBundle = notif.extras == null ?
5320 null : notif.extras.getBundle(EXTRA_CAR_EXTENDER);
5321 if (carBundle != null) {
5322 mLargeIcon = carBundle.getParcelable(EXTRA_LARGE_ICON);
5323 mColor = carBundle.getInt(EXTRA_COLOR, Notification.COLOR_DEFAULT);
5324
5325 Bundle b = carBundle.getBundle(EXTRA_CONVERSATION);
5326 mUnreadConversation = UnreadConversation.getUnreadConversationFromBundle(b);
5327 }
5328 }
5329
5330 /**
5331 * Apply car extensions to a notification that is being built. This is typically called by
5332 * the {@link Notification.Builder#extend(Notification.Extender)}
5333 * method of {@link Notification.Builder}.
5334 */
5335 @Override
5336 public Notification.Builder extend(Notification.Builder builder) {
5337 Bundle carExtensions = new Bundle();
5338
5339 if (mLargeIcon != null) {
5340 carExtensions.putParcelable(EXTRA_LARGE_ICON, mLargeIcon);
5341 }
5342 if (mColor != Notification.COLOR_DEFAULT) {
5343 carExtensions.putInt(EXTRA_COLOR, mColor);
5344 }
5345
5346 if (mUnreadConversation != null) {
5347 Bundle b = mUnreadConversation.getBundleForUnreadConversation();
5348 carExtensions.putBundle(EXTRA_CONVERSATION, b);
5349 }
5350
5351 builder.getExtras().putBundle(EXTRA_CAR_EXTENDER, carExtensions);
5352 return builder;
5353 }
5354
5355 /**
5356 * Sets the accent color to use when Android Auto presents the notification.
5357 *
5358 * Android Auto uses the color set with {@link Notification.Builder#setColor(int)}
5359 * to accent the displayed notification. However, not all colors are acceptable in an
5360 * automotive setting. This method can be used to override the color provided in the
5361 * notification in such a situation.
5362 */
Tor Norbye80756e32015-03-02 09:39:27 -08005363 public CarExtender setColor(@ColorInt int color) {
Zhen Yu Song9d5528b2014-11-14 15:34:39 -08005364 mColor = color;
5365 return this;
5366 }
5367
5368 /**
5369 * Gets the accent color.
5370 *
5371 * @see setColor
5372 */
Tor Norbye80756e32015-03-02 09:39:27 -08005373 @ColorInt
Zhen Yu Song9d5528b2014-11-14 15:34:39 -08005374 public int getColor() {
5375 return mColor;
5376 }
5377
5378 /**
5379 * Sets the large icon of the car notification.
5380 *
5381 * If no large icon is set in the extender, Android Auto will display the icon
5382 * specified by {@link Notification.Builder#setLargeIcon(android.graphics.Bitmap)}
5383 *
5384 * @param largeIcon The large icon to use in the car notification.
5385 * @return This object for method chaining.
5386 */
5387 public CarExtender setLargeIcon(Bitmap largeIcon) {
5388 mLargeIcon = largeIcon;
5389 return this;
5390 }
5391
5392 /**
5393 * Gets the large icon used in this car notification, or null if no icon has been set.
5394 *
5395 * @return The large icon for the car notification.
5396 * @see CarExtender#setLargeIcon
5397 */
5398 public Bitmap getLargeIcon() {
5399 return mLargeIcon;
5400 }
5401
5402 /**
5403 * Sets the unread conversation in a message notification.
5404 *
5405 * @param unreadConversation The unread part of the conversation this notification conveys.
5406 * @return This object for method chaining.
5407 */
5408 public CarExtender setUnreadConversation(UnreadConversation unreadConversation) {
5409 mUnreadConversation = unreadConversation;
5410 return this;
5411 }
5412
5413 /**
5414 * Returns the unread conversation conveyed by this notification.
5415 * @see #setUnreadConversation(UnreadConversation)
5416 */
5417 public UnreadConversation getUnreadConversation() {
5418 return mUnreadConversation;
5419 }
5420
5421 /**
5422 * A class which holds the unread messages from a conversation.
5423 */
5424 public static class UnreadConversation {
5425 private static final String KEY_AUTHOR = "author";
5426 private static final String KEY_TEXT = "text";
5427 private static final String KEY_MESSAGES = "messages";
5428 private static final String KEY_REMOTE_INPUT = "remote_input";
5429 private static final String KEY_ON_REPLY = "on_reply";
5430 private static final String KEY_ON_READ = "on_read";
5431 private static final String KEY_PARTICIPANTS = "participants";
5432 private static final String KEY_TIMESTAMP = "timestamp";
5433
5434 private final String[] mMessages;
5435 private final RemoteInput mRemoteInput;
5436 private final PendingIntent mReplyPendingIntent;
5437 private final PendingIntent mReadPendingIntent;
5438 private final String[] mParticipants;
5439 private final long mLatestTimestamp;
5440
5441 UnreadConversation(String[] messages, RemoteInput remoteInput,
5442 PendingIntent replyPendingIntent, PendingIntent readPendingIntent,
5443 String[] participants, long latestTimestamp) {
5444 mMessages = messages;
5445 mRemoteInput = remoteInput;
5446 mReadPendingIntent = readPendingIntent;
5447 mReplyPendingIntent = replyPendingIntent;
5448 mParticipants = participants;
5449 mLatestTimestamp = latestTimestamp;
5450 }
5451
5452 /**
5453 * Gets the list of messages conveyed by this notification.
5454 */
5455 public String[] getMessages() {
5456 return mMessages;
5457 }
5458
5459 /**
5460 * Gets the remote input that will be used to convey the response to a message list, or
5461 * null if no such remote input exists.
5462 */
5463 public RemoteInput getRemoteInput() {
5464 return mRemoteInput;
5465 }
5466
5467 /**
5468 * Gets the pending intent that will be triggered when the user replies to this
5469 * notification.
5470 */
5471 public PendingIntent getReplyPendingIntent() {
5472 return mReplyPendingIntent;
5473 }
5474
5475 /**
5476 * Gets the pending intent that Android Auto will send after it reads aloud all messages
5477 * in this object's message list.
5478 */
5479 public PendingIntent getReadPendingIntent() {
5480 return mReadPendingIntent;
5481 }
5482
5483 /**
5484 * Gets the participants in the conversation.
5485 */
5486 public String[] getParticipants() {
5487 return mParticipants;
5488 }
5489
5490 /**
5491 * Gets the firs participant in the conversation.
5492 */
5493 public String getParticipant() {
5494 return mParticipants.length > 0 ? mParticipants[0] : null;
5495 }
5496
5497 /**
5498 * Gets the timestamp of the conversation.
5499 */
5500 public long getLatestTimestamp() {
5501 return mLatestTimestamp;
5502 }
5503
5504 Bundle getBundleForUnreadConversation() {
5505 Bundle b = new Bundle();
5506 String author = null;
5507 if (mParticipants != null && mParticipants.length > 1) {
5508 author = mParticipants[0];
5509 }
5510 Parcelable[] messages = new Parcelable[mMessages.length];
5511 for (int i = 0; i < messages.length; i++) {
5512 Bundle m = new Bundle();
5513 m.putString(KEY_TEXT, mMessages[i]);
5514 m.putString(KEY_AUTHOR, author);
5515 messages[i] = m;
5516 }
5517 b.putParcelableArray(KEY_MESSAGES, messages);
5518 if (mRemoteInput != null) {
5519 b.putParcelable(KEY_REMOTE_INPUT, mRemoteInput);
5520 }
5521 b.putParcelable(KEY_ON_REPLY, mReplyPendingIntent);
5522 b.putParcelable(KEY_ON_READ, mReadPendingIntent);
5523 b.putStringArray(KEY_PARTICIPANTS, mParticipants);
5524 b.putLong(KEY_TIMESTAMP, mLatestTimestamp);
5525 return b;
5526 }
5527
5528 static UnreadConversation getUnreadConversationFromBundle(Bundle b) {
5529 if (b == null) {
5530 return null;
5531 }
5532 Parcelable[] parcelableMessages = b.getParcelableArray(KEY_MESSAGES);
5533 String[] messages = null;
5534 if (parcelableMessages != null) {
5535 String[] tmp = new String[parcelableMessages.length];
5536 boolean success = true;
5537 for (int i = 0; i < tmp.length; i++) {
5538 if (!(parcelableMessages[i] instanceof Bundle)) {
5539 success = false;
5540 break;
5541 }
5542 tmp[i] = ((Bundle) parcelableMessages[i]).getString(KEY_TEXT);
5543 if (tmp[i] == null) {
5544 success = false;
5545 break;
5546 }
5547 }
5548 if (success) {
5549 messages = tmp;
5550 } else {
5551 return null;
5552 }
5553 }
5554
5555 PendingIntent onRead = b.getParcelable(KEY_ON_READ);
5556 PendingIntent onReply = b.getParcelable(KEY_ON_REPLY);
5557
5558 RemoteInput remoteInput = b.getParcelable(KEY_REMOTE_INPUT);
5559
5560 String[] participants = b.getStringArray(KEY_PARTICIPANTS);
5561 if (participants == null || participants.length != 1) {
5562 return null;
5563 }
5564
5565 return new UnreadConversation(messages,
5566 remoteInput,
5567 onReply,
5568 onRead,
5569 participants, b.getLong(KEY_TIMESTAMP));
5570 }
5571 };
5572
5573 /**
5574 * Builder class for {@link CarExtender.UnreadConversation} objects.
5575 */
5576 public static class Builder {
5577 private final List<String> mMessages = new ArrayList<String>();
5578 private final String mParticipant;
5579 private RemoteInput mRemoteInput;
5580 private PendingIntent mReadPendingIntent;
5581 private PendingIntent mReplyPendingIntent;
5582 private long mLatestTimestamp;
5583
5584 /**
5585 * Constructs a new builder for {@link CarExtender.UnreadConversation}.
5586 *
5587 * @param name The name of the other participant in the conversation.
5588 */
5589 public Builder(String name) {
5590 mParticipant = name;
5591 }
5592
5593 /**
5594 * Appends a new unread message to the list of messages for this conversation.
5595 *
5596 * The messages should be added from oldest to newest.
5597 *
5598 * @param message The text of the new unread message.
5599 * @return This object for method chaining.
5600 */
5601 public Builder addMessage(String message) {
5602 mMessages.add(message);
5603 return this;
5604 }
5605
5606 /**
5607 * Sets the pending intent and remote input which will convey the reply to this
5608 * notification.
5609 *
5610 * @param pendingIntent The pending intent which will be triggered on a reply.
5611 * @param remoteInput The remote input parcelable which will carry the reply.
5612 * @return This object for method chaining.
5613 *
5614 * @see CarExtender.UnreadConversation#getRemoteInput
5615 * @see CarExtender.UnreadConversation#getReplyPendingIntent
5616 */
5617 public Builder setReplyAction(
5618 PendingIntent pendingIntent, RemoteInput remoteInput) {
5619 mRemoteInput = remoteInput;
5620 mReplyPendingIntent = pendingIntent;
5621
5622 return this;
5623 }
5624
5625 /**
5626 * Sets the pending intent that will be sent once the messages in this notification
5627 * are read.
5628 *
5629 * @param pendingIntent The pending intent to use.
5630 * @return This object for method chaining.
5631 */
5632 public Builder setReadPendingIntent(PendingIntent pendingIntent) {
5633 mReadPendingIntent = pendingIntent;
5634 return this;
5635 }
5636
5637 /**
5638 * Sets the timestamp of the most recent message in an unread conversation.
5639 *
5640 * If a messaging notification has been posted by your application and has not
5641 * yet been cancelled, posting a later notification with the same id and tag
5642 * but without a newer timestamp may result in Android Auto not displaying a
5643 * heads up notification for the later notification.
5644 *
5645 * @param timestamp The timestamp of the most recent message in the conversation.
5646 * @return This object for method chaining.
5647 */
5648 public Builder setLatestTimestamp(long timestamp) {
5649 mLatestTimestamp = timestamp;
5650 return this;
5651 }
5652
5653 /**
5654 * Builds a new unread conversation object.
5655 *
5656 * @return The new unread conversation object.
5657 */
5658 public UnreadConversation build() {
5659 String[] messages = mMessages.toArray(new String[mMessages.size()]);
5660 String[] participants = { mParticipant };
5661 return new UnreadConversation(messages, mRemoteInput, mReplyPendingIntent,
5662 mReadPendingIntent, participants, mLatestTimestamp);
5663 }
5664 }
5665 }
5666
5667 /**
Griff Hazen61a9e862014-05-22 16:05:19 -07005668 * Get an array of Notification objects from a parcelable array bundle field.
5669 * Update the bundle to have a typed array so fetches in the future don't need
5670 * to do an array copy.
5671 */
5672 private static Notification[] getNotificationArrayFromBundle(Bundle bundle, String key) {
5673 Parcelable[] array = bundle.getParcelableArray(key);
5674 if (array instanceof Notification[] || array == null) {
5675 return (Notification[]) array;
5676 }
5677 Notification[] typedArray = Arrays.copyOf(array, array.length,
5678 Notification[].class);
5679 bundle.putParcelableArray(key, typedArray);
5680 return typedArray;
5681 }
Christoph Studer4600f9b2014-07-22 22:44:43 +02005682
5683 private static class BuilderRemoteViews extends RemoteViews {
5684 public BuilderRemoteViews(Parcel parcel) {
5685 super(parcel);
5686 }
5687
Kenny Guy77320062014-08-27 21:37:15 +01005688 public BuilderRemoteViews(ApplicationInfo appInfo, int layoutId) {
5689 super(appInfo, layoutId);
Christoph Studer4600f9b2014-07-22 22:44:43 +02005690 }
5691
5692 @Override
5693 public BuilderRemoteViews clone() {
5694 Parcel p = Parcel.obtain();
5695 writeToParcel(p, 0);
5696 p.setDataPosition(0);
5697 BuilderRemoteViews brv = new BuilderRemoteViews(p);
5698 p.recycle();
5699 return brv;
5700 }
5701 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005702}