blob: 27fe33e6b12f09c9f96c1429813269b9665f8d1d [file] [log] [blame]
Joe Onorato0cbda992010-05-02 16:28:15 -07001/*
2 * Copyright (C) 2008 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
Joe Onorato79de0c52010-05-26 17:03:26 -040017package com.android.systemui.statusbar;
Joe Onorato0cbda992010-05-02 16:28:15 -070018
Selim Cinek49014f82016-11-04 14:55:30 -070019import android.animation.Animator;
20import android.animation.AnimatorListenerAdapter;
21import android.animation.ObjectAnimator;
Selim Cinek875ba9b2017-02-13 16:20:17 -080022import android.animation.ValueAnimator;
Svetoslav Ganov6179ea32011-06-28 01:12:41 -070023import android.app.Notification;
Joe Onorato0cbda992010-05-02 16:28:15 -070024import android.content.Context;
Adrian Roosa8e18ef2016-05-26 17:17:02 -070025import android.content.pm.ApplicationInfo;
Selim Cinek3e7592d2016-04-11 09:35:54 +080026import android.content.res.Configuration;
Joe Onorato0cbda992010-05-02 16:28:15 -070027import android.content.res.Resources;
Joe Onorato0cbda992010-05-02 16:28:15 -070028import android.graphics.Canvas;
Adrian Roos456e0052017-04-04 16:44:29 -070029import android.graphics.Color;
Adrian Roosc3e8cfc2017-07-17 17:09:39 +020030import android.graphics.ColorMatrix;
31import android.graphics.ColorMatrixColorFilter;
Joe Onorato6c01a112010-10-04 17:38:47 -040032import android.graphics.Paint;
33import android.graphics.Rect;
John Spurlockde84f0e2013-06-12 12:41:00 -040034import android.graphics.drawable.Drawable;
Dan Sandlerd63f9322015-05-06 15:18:49 -040035import android.graphics.drawable.Icon;
Adrian Roosa8e18ef2016-05-26 17:17:02 -070036import android.os.Parcelable;
Jeff Sharkeyded653b2012-09-27 19:09:24 -070037import android.os.UserHandle;
Selim Cinek72fc8db2017-06-06 18:07:47 -070038import android.service.notification.StatusBarNotification;
Lucas Dupin83519da2017-06-21 11:58:31 -070039import android.support.v4.graphics.ColorUtils;
Svetoslav Ganov6179ea32011-06-28 01:12:41 -070040import android.text.TextUtils;
Daniel Sandler05e24142011-11-10 11:56:49 -050041import android.util.AttributeSet;
Selim Cinek49014f82016-11-04 14:55:30 -070042import android.util.FloatProperty;
Joe Onoratof9ec03c2010-09-23 15:09:18 -070043import android.util.Log;
Selim Cinek49014f82016-11-04 14:55:30 -070044import android.util.Property;
Anthony Chen55e8e1e2016-01-08 10:31:46 -080045import android.util.TypedValue;
Joe Onorato0cbda992010-05-02 16:28:15 -070046import android.view.ViewDebug;
Svetoslav Ganov6179ea32011-06-28 01:12:41 -070047import android.view.accessibility.AccessibilityEvent;
Selim Cinek49014f82016-11-04 14:55:30 -070048import android.view.animation.Interpolator;
Winsonc0d70582016-01-29 10:24:39 -080049
Joe Onorato0cbda992010-05-02 16:28:15 -070050import com.android.internal.statusbar.StatusBarIcon;
Lucas Dupin83519da2017-06-21 11:58:31 -070051import com.android.internal.util.NotificationColorUtil;
Selim Cinek49014f82016-11-04 14:55:30 -070052import com.android.systemui.Interpolators;
Joe Onorato6c01a112010-10-04 17:38:47 -040053import com.android.systemui.R;
Adrian Roos456e0052017-04-04 16:44:29 -070054import com.android.systemui.statusbar.notification.NotificationIconDozeHelper;
Selim Cinek49014f82016-11-04 14:55:30 -070055import com.android.systemui.statusbar.notification.NotificationUtils;
Joe Onorato6c01a112010-10-04 17:38:47 -040056
John Spurlockde84f0e2013-06-12 12:41:00 -040057import java.text.NumberFormat;
Adrian Roosc3e8cfc2017-07-17 17:09:39 +020058import java.util.Arrays;
John Spurlockde84f0e2013-06-12 12:41:00 -040059
Joe Onorato0cbda992010-05-02 16:28:15 -070060public class StatusBarIconView extends AnimatedImageView {
Selim Cinek875ba9b2017-02-13 16:20:17 -080061 public static final int NO_COLOR = 0;
Adrian Roosc3e8cfc2017-07-17 17:09:39 +020062
63 /**
64 * Multiply alpha values with (1+DARK_ALPHA_BOOST) when dozing. The chosen value boosts
65 * everything above 30% to 50%, making it appear on 1bit color depths.
66 */
67 private static final float DARK_ALPHA_BOOST = 0.67f;
Selim Cinek875ba9b2017-02-13 16:20:17 -080068 private final int ANIMATION_DURATION_FAST = 100;
69
Selim Cinek49014f82016-11-04 14:55:30 -070070 public static final int STATE_ICON = 0;
71 public static final int STATE_DOT = 1;
72 public static final int STATE_HIDDEN = 2;
Joe Onorato0cbda992010-05-02 16:28:15 -070073
Selim Cinek49014f82016-11-04 14:55:30 -070074 private static final String TAG = "StatusBarIconView";
75 private static final Property<StatusBarIconView, Float> ICON_APPEAR_AMOUNT
76 = new FloatProperty<StatusBarIconView>("iconAppearAmount") {
77
78 @Override
79 public void setValue(StatusBarIconView object, float value) {
80 object.setIconAppearAmount(value);
81 }
82
83 @Override
84 public Float get(StatusBarIconView object) {
85 return object.getIconAppearAmount();
86 }
87 };
Selim Cinek5b5beb012016-11-08 18:11:58 -080088 private static final Property<StatusBarIconView, Float> DOT_APPEAR_AMOUNT
Selim Cinek49014f82016-11-04 14:55:30 -070089 = new FloatProperty<StatusBarIconView>("dot_appear_amount") {
90
91 @Override
92 public void setValue(StatusBarIconView object, float value) {
93 object.setDotAppearAmount(value);
94 }
95
96 @Override
97 public Float get(StatusBarIconView object) {
98 return object.getDotAppearAmount();
99 }
100 };
101
102 private boolean mAlwaysScaleIcon;
Adrian Roos138f0342017-04-28 09:11:28 -0700103 private int mStatusBarIconDrawingSizeDark = 1;
104 private int mStatusBarIconDrawingSize = 1;
105 private int mStatusBarIconSize = 1;
Joe Onorato0cbda992010-05-02 16:28:15 -0700106 private StatusBarIcon mIcon;
107 @ViewDebug.ExportedProperty private String mSlot;
Joe Onorato6c01a112010-10-04 17:38:47 -0400108 private Drawable mNumberBackground;
109 private Paint mNumberPain;
110 private int mNumberX;
111 private int mNumberY;
112 private String mNumberText;
Selim Cinek72fc8db2017-06-06 18:07:47 -0700113 private StatusBarNotification mNotification;
Jason Monk3b230072015-05-29 11:11:29 -0400114 private final boolean mBlocked;
Selim Cinek3e7592d2016-04-11 09:35:54 +0800115 private int mDensity;
Selim Cinek281c2022016-10-13 19:14:43 -0700116 private float mIconScale = 1.0f;
Selim Cinek49014f82016-11-04 14:55:30 -0700117 private final Paint mDotPaint = new Paint();
Selim Cinek49014f82016-11-04 14:55:30 -0700118 private float mDotRadius;
119 private int mStaticDotRadius;
120 private int mVisibleState = STATE_ICON;
121 private float mIconAppearAmount = 1.0f;
122 private ObjectAnimator mIconAppearAnimator;
123 private ObjectAnimator mDotAnimator;
124 private float mDotAppearAmount;
Selim Cinek2b549f42016-11-22 16:38:51 -0800125 private OnVisibilityChangedListener mOnVisibilityChangedListener;
Selim Cinek875ba9b2017-02-13 16:20:17 -0800126 private int mDrawableColor;
127 private int mIconColor;
Adrian Roos456e0052017-04-04 16:44:29 -0700128 private int mDecorColor;
129 private float mDarkAmount;
Selim Cinek875ba9b2017-02-13 16:20:17 -0800130 private ValueAnimator mColorAnimator;
131 private int mCurrentSetColor = NO_COLOR;
132 private int mAnimationStartColor = NO_COLOR;
133 private final ValueAnimator.AnimatorUpdateListener mColorUpdater
134 = animation -> {
135 int newColor = NotificationUtils.interpolateColors(mAnimationStartColor, mIconColor,
136 animation.getAnimatedFraction());
137 setColorInternal(newColor);
138 };
Adrian Roos456e0052017-04-04 16:44:29 -0700139 private final NotificationIconDozeHelper mDozer;
Lucas Dupin83519da2017-06-21 11:58:31 -0700140 private int mContrastedDrawableColor;
141 private int mCachedContrastBackgroundColor = NO_COLOR;
Adrian Roosc3e8cfc2017-07-17 17:09:39 +0200142 private float[] mMatrix;
143 private ColorMatrixColorFilter mMatrixColorFilter;
Joe Onorato0cbda992010-05-02 16:28:15 -0700144
Selim Cinek72fc8db2017-06-06 18:07:47 -0700145 public StatusBarIconView(Context context, String slot, StatusBarNotification sbn) {
146 this(context, slot, sbn, false);
Jason Monk3b230072015-05-29 11:11:29 -0400147 }
148
Selim Cinek72fc8db2017-06-06 18:07:47 -0700149 public StatusBarIconView(Context context, String slot, StatusBarNotification sbn,
Jason Monk3b230072015-05-29 11:11:29 -0400150 boolean blocked) {
Joe Onorato0cbda992010-05-02 16:28:15 -0700151 super(context);
Adrian Roos456e0052017-04-04 16:44:29 -0700152 mDozer = new NotificationIconDozeHelper(context);
Jason Monk3b230072015-05-29 11:11:29 -0400153 mBlocked = blocked;
Joe Onorato0cbda992010-05-02 16:28:15 -0700154 mSlot = slot;
Joe Onorato6c01a112010-10-04 17:38:47 -0400155 mNumberPain = new Paint();
156 mNumberPain.setTextAlign(Paint.Align.CENTER);
Alan Viverette4a357cd2015-03-18 18:37:18 -0700157 mNumberPain.setColor(context.getColor(R.drawable.notification_number_text_color));
Joe Onorato6c01a112010-10-04 17:38:47 -0400158 mNumberPain.setAntiAlias(true);
Selim Cinek72fc8db2017-06-06 18:07:47 -0700159 setNotification(sbn);
Adrian Roos138f0342017-04-28 09:11:28 -0700160 maybeUpdateIconScaleDimens();
Selim Cinek3e7592d2016-04-11 09:35:54 +0800161 setScaleType(ScaleType.CENTER);
162 mDensity = context.getResources().getDisplayMetrics().densityDpi;
Selim Cinek49014f82016-11-04 14:55:30 -0700163 if (mNotification != null) {
Selim Cinek875ba9b2017-02-13 16:20:17 -0800164 setDecorColor(getContext().getColor(
Selim Cinek49014f82016-11-04 14:55:30 -0700165 com.android.internal.R.color.notification_icon_default_color));
166 }
167 reloadDimens();
Selim Cinek3e7592d2016-04-11 09:35:54 +0800168 }
Daniel Sandler26c84b12011-07-27 00:09:40 -0400169
Adrian Roos138f0342017-04-28 09:11:28 -0700170 private void maybeUpdateIconScaleDimens() {
Daniel Sandler7579bca2011-08-18 15:47:26 -0400171 // We do not resize and scale system icons (on the right), only notification icons (on the
172 // left).
Selim Cinek3e7592d2016-04-11 09:35:54 +0800173 if (mNotification != null || mAlwaysScaleIcon) {
Adrian Roos138f0342017-04-28 09:11:28 -0700174 updateIconScaleDimens();
Daniel Sandler7579bca2011-08-18 15:47:26 -0400175 }
Selim Cinek3e7592d2016-04-11 09:35:54 +0800176 }
Daniel Sandlerabff0322011-09-27 11:19:34 -0400177
Adrian Roos138f0342017-04-28 09:11:28 -0700178 private void updateIconScaleDimens() {
Selim Cinek3e7592d2016-04-11 09:35:54 +0800179 Resources res = mContext.getResources();
Adrian Roos138f0342017-04-28 09:11:28 -0700180 mStatusBarIconSize = res.getDimensionPixelSize(R.dimen.status_bar_icon_size);
181 mStatusBarIconDrawingSizeDark =
182 res.getDimensionPixelSize(R.dimen.status_bar_icon_drawing_size_dark);
183 mStatusBarIconDrawingSize =
184 res.getDimensionPixelSize(R.dimen.status_bar_icon_drawing_size);
185 updateIconScale();
186 }
187
188 private void updateIconScale() {
189 final float imageBounds = NotificationUtils.interpolate(
190 mStatusBarIconDrawingSize,
191 mStatusBarIconDrawingSizeDark,
192 mDarkAmount);
193 final int outerBounds = mStatusBarIconSize;
Selim Cinek281c2022016-10-13 19:14:43 -0700194 mIconScale = (float)imageBounds / (float)outerBounds;
Selim Cinek281c2022016-10-13 19:14:43 -0700195 }
196
Adrian Roosceac4a02017-05-30 20:25:52 -0700197 public float getIconScaleFullyDark() {
198 return (float) mStatusBarIconDrawingSizeDark / mStatusBarIconDrawingSize;
199 }
200
Selim Cinek281c2022016-10-13 19:14:43 -0700201 public float getIconScale() {
202 return mIconScale;
Selim Cinek3e7592d2016-04-11 09:35:54 +0800203 }
204
205 @Override
206 protected void onConfigurationChanged(Configuration newConfig) {
207 super.onConfigurationChanged(newConfig);
208 int density = newConfig.densityDpi;
209 if (density != mDensity) {
210 mDensity = density;
Adrian Roos138f0342017-04-28 09:11:28 -0700211 maybeUpdateIconScaleDimens();
Selim Cinek3e7592d2016-04-11 09:35:54 +0800212 updateDrawable();
Selim Cinek49014f82016-11-04 14:55:30 -0700213 reloadDimens();
214 }
215 }
216
217 private void reloadDimens() {
218 boolean applyRadius = mDotRadius == mStaticDotRadius;
219 mStaticDotRadius = getResources().getDimensionPixelSize(R.dimen.overflow_dot_radius);
220 if (applyRadius) {
221 mDotRadius = mStaticDotRadius;
Selim Cinek3e7592d2016-04-11 09:35:54 +0800222 }
Joe Onorato0cbda992010-05-02 16:28:15 -0700223 }
224
Selim Cinek72fc8db2017-06-06 18:07:47 -0700225 public void setNotification(StatusBarNotification notification) {
Christoph Studera0506e72014-07-31 20:27:39 +0200226 mNotification = notification;
Selim Cinek72fc8db2017-06-06 18:07:47 -0700227 if (notification != null) {
228 setContentDescription(notification.getNotification());
229 }
Christoph Studera0506e72014-07-31 20:27:39 +0200230 }
231
Daniel Sandler05e24142011-11-10 11:56:49 -0500232 public StatusBarIconView(Context context, AttributeSet attrs) {
233 super(context, attrs);
Adrian Roos456e0052017-04-04 16:44:29 -0700234 mDozer = new NotificationIconDozeHelper(context);
Jason Monk3b230072015-05-29 11:11:29 -0400235 mBlocked = false;
Selim Cinek3e7592d2016-04-11 09:35:54 +0800236 mAlwaysScaleIcon = true;
Adrian Roos138f0342017-04-28 09:11:28 -0700237 updateIconScaleDimens();
Selim Cinek3e7592d2016-04-11 09:35:54 +0800238 mDensity = context.getResources().getDisplayMetrics().densityDpi;
Daniel Sandler05e24142011-11-10 11:56:49 -0500239 }
240
Joe Onorato0cbda992010-05-02 16:28:15 -0700241 private static boolean streq(String a, String b) {
Joe Onorato66d7d012010-05-14 10:05:10 -0700242 if (a == b) {
243 return true;
244 }
Joe Onorato0cbda992010-05-02 16:28:15 -0700245 if (a == null && b != null) {
246 return false;
247 }
248 if (a != null && b == null) {
249 return false;
250 }
251 return a.equals(b);
252 }
253
Dan Sandlerd63f9322015-05-06 15:18:49 -0400254 public boolean equalIcons(Icon a, Icon b) {
255 if (a == b) return true;
256 if (a.getType() != b.getType()) return false;
257 switch (a.getType()) {
258 case Icon.TYPE_RESOURCE:
259 return a.getResPackage().equals(b.getResPackage()) && a.getResId() == b.getResId();
260 case Icon.TYPE_URI:
261 return a.getUriString().equals(b.getUriString());
262 default:
263 return false;
264 }
265 }
Joe Onorato005847b2010-06-04 16:08:02 -0400266 /**
267 * Returns whether the set succeeded.
268 */
269 public boolean set(StatusBarIcon icon) {
Dan Sandlerd63f9322015-05-06 15:18:49 -0400270 final boolean iconEquals = mIcon != null && equalIcons(mIcon.icon, icon.icon);
Joe Onorato005847b2010-06-04 16:08:02 -0400271 final boolean levelEquals = iconEquals
272 && mIcon.iconLevel == icon.iconLevel;
273 final boolean visibilityEquals = mIcon != null
274 && mIcon.visible == icon.visible;
Joe Onorato6c01a112010-10-04 17:38:47 -0400275 final boolean numberEquals = mIcon != null
276 && mIcon.number == icon.number;
277 mIcon = icon.clone();
Svetoslav Ganov6179ea32011-06-28 01:12:41 -0700278 setContentDescription(icon.contentDescription);
Joe Onorato005847b2010-06-04 16:08:02 -0400279 if (!iconEquals) {
Fabrice Di Meglio82fca5d2013-01-09 15:42:31 -0800280 if (!updateDrawable(false /* no clear */)) return false;
Selim Cinek2b6eb8032016-12-29 14:22:21 +0100281 // we have to clear the grayscale tag since it may have changed
282 setTag(R.id.icon_is_grayscale, null);
Joe Onorato0cbda992010-05-02 16:28:15 -0700283 }
Joe Onorato005847b2010-06-04 16:08:02 -0400284 if (!levelEquals) {
285 setImageLevel(icon.iconLevel);
Joe Onorato0cbda992010-05-02 16:28:15 -0700286 }
Daniel Sandler26c84b12011-07-27 00:09:40 -0400287
Joe Onorato6c01a112010-10-04 17:38:47 -0400288 if (!numberEquals) {
John Spurlock01534782014-01-13 11:59:22 -0500289 if (icon.number > 0 && getContext().getResources().getBoolean(
Joe Onorato8595a3d2010-11-19 18:12:07 -0800290 R.bool.config_statusBarShowNumber)) {
Joe Onorato6c01a112010-10-04 17:38:47 -0400291 if (mNumberBackground == null) {
292 mNumberBackground = getContext().getResources().getDrawable(
293 R.drawable.ic_notification_overlay);
294 }
295 placeNumber();
296 } else {
297 mNumberBackground = null;
298 mNumberText = null;
299 }
300 invalidate();
301 }
Joe Onorato005847b2010-06-04 16:08:02 -0400302 if (!visibilityEquals) {
Jason Monk3b230072015-05-29 11:11:29 -0400303 setVisibility(icon.visible && !mBlocked ? VISIBLE : GONE);
Joe Onorato005847b2010-06-04 16:08:02 -0400304 }
Joe Onorato005847b2010-06-04 16:08:02 -0400305 return true;
Joe Onorato0cbda992010-05-02 16:28:15 -0700306 }
307
Fabrice Di Meglio82fca5d2013-01-09 15:42:31 -0800308 public void updateDrawable() {
309 updateDrawable(true /* with clear */);
310 }
311
312 private boolean updateDrawable(boolean withClear) {
Jorim Jaggi66ac1332015-01-21 19:22:26 +0100313 if (mIcon == null) {
314 return false;
315 }
Adrian Roosfb2d0cc2017-01-31 15:10:00 -0800316 Drawable drawable;
317 try {
318 drawable = getIcon(mIcon);
319 } catch (OutOfMemoryError e) {
320 Log.w(TAG, "OOM while inflating " + mIcon.icon + " for slot " + mSlot);
321 return false;
322 }
323
Fabrice Di Meglio82fca5d2013-01-09 15:42:31 -0800324 if (drawable == null) {
Adrian Roosfb2d0cc2017-01-31 15:10:00 -0800325 Log.w(TAG, "No icon for slot " + mSlot + "; " + mIcon.icon);
Fabrice Di Meglio82fca5d2013-01-09 15:42:31 -0800326 return false;
327 }
328 if (withClear) {
329 setImageDrawable(null);
330 }
331 setImageDrawable(drawable);
332 return true;
333 }
334
Selim Cinek72fc8db2017-06-06 18:07:47 -0700335 public Icon getSourceIcon() {
336 return mIcon.icon;
337 }
338
Joe Onoratof5510542010-06-01 07:46:41 -0700339 private Drawable getIcon(StatusBarIcon icon) {
340 return getIcon(getContext(), icon);
341 }
342
Joe Onorato0cbda992010-05-02 16:28:15 -0700343 /**
Dan Sandlerd63f9322015-05-06 15:18:49 -0400344 * Returns the right icon to use for this item
John Spurlock209bede2013-07-17 12:23:27 -0400345 *
Dan Sandlerd63f9322015-05-06 15:18:49 -0400346 * @param context Context to use to get resources
Joe Onorato0cbda992010-05-02 16:28:15 -0700347 * @return Drawable for this item, or null if the package or item could not
348 * be found
349 */
Anthony Chen55e8e1e2016-01-08 10:31:46 -0800350 public static Drawable getIcon(Context context, StatusBarIcon statusBarIcon) {
351 int userId = statusBarIcon.user.getIdentifier();
Dan Sandlerd63f9322015-05-06 15:18:49 -0400352 if (userId == UserHandle.USER_ALL) {
Xiaohui Chen87d0e252015-07-30 15:38:16 -0700353 userId = UserHandle.USER_SYSTEM;
Joe Onorato0cbda992010-05-02 16:28:15 -0700354 }
Anthony Chen55e8e1e2016-01-08 10:31:46 -0800355
356 Drawable icon = statusBarIcon.icon.loadDrawableAsUser(context, userId);
357
358 TypedValue typedValue = new TypedValue();
359 context.getResources().getValue(R.dimen.status_bar_icon_scale_factor, typedValue, true);
360 float scaleFactor = typedValue.getFloat();
361
362 // No need to scale the icon, so return it as is.
363 if (scaleFactor == 1.f) {
364 return icon;
365 }
366
367 return new ScalingDrawableWrapper(icon, scaleFactor);
Joe Onorato0cbda992010-05-02 16:28:15 -0700368 }
Joe Onoratob77f53b2010-05-28 19:59:51 -0400369
370 public StatusBarIcon getStatusBarIcon() {
371 return mIcon;
372 }
Joe Onoratof9ec03c2010-09-23 15:09:18 -0700373
Svetoslav Ganov6179ea32011-06-28 01:12:41 -0700374 @Override
375 public void onInitializeAccessibilityEvent(AccessibilityEvent event) {
376 super.onInitializeAccessibilityEvent(event);
377 if (mNotification != null) {
Selim Cinek72fc8db2017-06-06 18:07:47 -0700378 event.setParcelableData(mNotification.getNotification());
Svetoslav Ganov6179ea32011-06-28 01:12:41 -0700379 }
380 }
381
382 @Override
Joe Onorato6c01a112010-10-04 17:38:47 -0400383 protected void onSizeChanged(int w, int h, int oldw, int oldh) {
384 super.onSizeChanged(w, h, oldw, oldh);
385 if (mNumberBackground != null) {
386 placeNumber();
387 }
388 }
389
Svetoslav Ganov6179ea32011-06-28 01:12:41 -0700390 @Override
Jorim Jaggi66ac1332015-01-21 19:22:26 +0100391 public void onRtlPropertiesChanged(int layoutDirection) {
392 super.onRtlPropertiesChanged(layoutDirection);
393 updateDrawable();
394 }
395
396 @Override
Joe Onorato6c01a112010-10-04 17:38:47 -0400397 protected void onDraw(Canvas canvas) {
Selim Cinek49014f82016-11-04 14:55:30 -0700398 if (mIconAppearAmount > 0.0f) {
399 canvas.save();
400 canvas.scale(mIconScale * mIconAppearAmount, mIconScale * mIconAppearAmount,
401 getWidth() / 2, getHeight() / 2);
402 super.onDraw(canvas);
403 canvas.restore();
404 }
Joe Onorato6c01a112010-10-04 17:38:47 -0400405
406 if (mNumberBackground != null) {
407 mNumberBackground.draw(canvas);
408 canvas.drawText(mNumberText, mNumberX, mNumberY, mNumberPain);
409 }
Selim Cinek49014f82016-11-04 14:55:30 -0700410 if (mDotAppearAmount != 0.0f) {
411 float radius;
412 float alpha;
413 if (mDotAppearAmount <= 1.0f) {
414 radius = mDotRadius * mDotAppearAmount;
415 alpha = 1.0f;
416 } else {
417 float fadeOutAmount = mDotAppearAmount - 1.0f;
418 alpha = 1.0f - fadeOutAmount;
419 radius = NotificationUtils.interpolate(mDotRadius, getWidth() / 4, fadeOutAmount);
420 }
421 mDotPaint.setAlpha((int) (alpha * 255));
422 canvas.drawCircle(getWidth() / 2, getHeight() / 2, radius, mDotPaint);
423 }
Joe Onorato6c01a112010-10-04 17:38:47 -0400424 }
425
Svetoslav Ganov6179ea32011-06-28 01:12:41 -0700426 @Override
Joe Onoratof9ec03c2010-09-23 15:09:18 -0700427 protected void debug(int depth) {
428 super.debug(depth);
429 Log.d("View", debugIndent(depth) + "slot=" + mSlot);
430 Log.d("View", debugIndent(depth) + "icon=" + mIcon);
431 }
Joe Onorato6c01a112010-10-04 17:38:47 -0400432
433 void placeNumber() {
Daniel Sandlerebce0112011-06-16 16:44:51 -0400434 final String str;
John Spurlock01534782014-01-13 11:59:22 -0500435 final int tooBig = getContext().getResources().getInteger(
Daniel Sandlerebce0112011-06-16 16:44:51 -0400436 android.R.integer.status_bar_notification_info_maxnum);
437 if (mIcon.number > tooBig) {
John Spurlock01534782014-01-13 11:59:22 -0500438 str = getContext().getResources().getString(
Daniel Sandlerebce0112011-06-16 16:44:51 -0400439 android.R.string.status_bar_notification_info_overflow);
440 } else {
441 NumberFormat f = NumberFormat.getIntegerInstance();
442 str = f.format(mIcon.number);
443 }
444 mNumberText = str;
445
Joe Onorato6c01a112010-10-04 17:38:47 -0400446 final int w = getWidth();
447 final int h = getHeight();
448 final Rect r = new Rect();
449 mNumberPain.getTextBounds(str, 0, str.length(), r);
450 final int tw = r.right - r.left;
451 final int th = r.bottom - r.top;
452 mNumberBackground.getPadding(r);
453 int dw = r.left + tw + r.right;
454 if (dw < mNumberBackground.getMinimumWidth()) {
455 dw = mNumberBackground.getMinimumWidth();
456 }
457 mNumberX = w-r.right-((dw-r.right-r.left)/2);
458 int dh = r.top + th + r.bottom;
459 if (dh < mNumberBackground.getMinimumWidth()) {
460 dh = mNumberBackground.getMinimumWidth();
461 }
462 mNumberY = h-r.bottom-((dh-r.top-th-r.bottom)/2);
463 mNumberBackground.setBounds(w-dw, h-dh, w, h);
464 }
Svetoslav Ganov6179ea32011-06-28 01:12:41 -0700465
466 private void setContentDescription(Notification notification) {
467 if (notification != null) {
Adrian Rooseba05822016-04-22 17:09:27 -0700468 String d = contentDescForNotification(mContext, notification);
469 if (!TextUtils.isEmpty(d)) {
470 setContentDescription(d);
Svetoslav Ganov6179ea32011-06-28 01:12:41 -0700471 }
472 }
473 }
Daniel Sandler7579bca2011-08-18 15:47:26 -0400474
475 public String toString() {
John Spurlock209bede2013-07-17 12:23:27 -0400476 return "StatusBarIconView(slot=" + mSlot + " icon=" + mIcon
Daniel Sandler7579bca2011-08-18 15:47:26 -0400477 + " notification=" + mNotification + ")";
478 }
Jason Monk3b230072015-05-29 11:11:29 -0400479
Selim Cinek72fc8db2017-06-06 18:07:47 -0700480 public StatusBarNotification getNotification() {
481 return mNotification;
482 }
483
Jason Monk3b230072015-05-29 11:11:29 -0400484 public String getSlot() {
485 return mSlot;
486 }
Adrian Rooseba05822016-04-22 17:09:27 -0700487
488
489 public static String contentDescForNotification(Context c, Notification n) {
Adrian Roosa8e18ef2016-05-26 17:17:02 -0700490 String appName = "";
491 try {
492 Notification.Builder builder = Notification.Builder.recoverBuilder(c, n);
493 appName = builder.loadHeaderAppName();
494 } catch (RuntimeException e) {
495 Log.e(TAG, "Unable to recover builder", e);
496 // Trying to get the app name from the app info instead.
497 Parcelable appInfo = n.extras.getParcelable(
498 Notification.EXTRA_BUILDER_APPLICATION_INFO);
499 if (appInfo instanceof ApplicationInfo) {
500 appName = String.valueOf(((ApplicationInfo) appInfo).loadLabel(
501 c.getPackageManager()));
502 }
503 }
Adrian Roos51548e62016-04-28 11:20:26 -0700504
Julia Reynolds7f9ce782016-05-17 15:34:34 -0400505 CharSequence title = n.extras.getCharSequence(Notification.EXTRA_TITLE);
Adrian Roosf76600e2017-04-19 15:32:43 -0700506 CharSequence text = n.extras.getCharSequence(Notification.EXTRA_TEXT);
Adrian Rooseba05822016-04-22 17:09:27 -0700507 CharSequence ticker = n.tickerText;
Adrian Roos51548e62016-04-28 11:20:26 -0700508
Adrian Roosf76600e2017-04-19 15:32:43 -0700509 // Some apps just put the app name into the title
510 CharSequence titleOrText = TextUtils.equals(title, appName) ? text : title;
511
512 CharSequence desc = !TextUtils.isEmpty(titleOrText) ? titleOrText
Adrian Roos6e782a52017-03-30 18:34:10 -0700513 : !TextUtils.isEmpty(ticker) ? ticker : "";
Adrian Roos51548e62016-04-28 11:20:26 -0700514
515 return c.getString(R.string.accessibility_desc_notification_icon, appName, desc);
Adrian Rooseba05822016-04-22 17:09:27 -0700516 }
Adrian Roos51548e62016-04-28 11:20:26 -0700517
Selim Cinek875ba9b2017-02-13 16:20:17 -0800518 /**
519 * Set the color that is used to draw decoration like the overflow dot. This will not be applied
520 * to the drawable.
521 */
522 public void setDecorColor(int iconTint) {
Adrian Roos456e0052017-04-04 16:44:29 -0700523 mDecorColor = iconTint;
524 updateDecorColor();
525 }
526
527 private void updateDecorColor() {
528 int color = NotificationUtils.interpolateColors(mDecorColor, Color.WHITE, mDarkAmount);
529 if (mDotPaint.getColor() != color) {
530 mDotPaint.setColor(color);
531
532 if (mDotAppearAmount != 0) {
533 invalidate();
534 }
535 }
Selim Cinek49014f82016-11-04 14:55:30 -0700536 }
537
Selim Cinek875ba9b2017-02-13 16:20:17 -0800538 /**
539 * Set the static color that should be used for the drawable of this icon if it's not
540 * transitioning this also immediately sets the color.
541 */
542 public void setStaticDrawableColor(int color) {
543 mDrawableColor = color;
544 setColorInternal(color);
Lucas Dupin83519da2017-06-21 11:58:31 -0700545 updateContrastedStaticColor();
Selim Cinek875ba9b2017-02-13 16:20:17 -0800546 mIconColor = color;
Adrian Roos456e0052017-04-04 16:44:29 -0700547 mDozer.setColor(color);
Selim Cinek875ba9b2017-02-13 16:20:17 -0800548 }
549
550 private void setColorInternal(int color) {
Adrian Roosf3eacd32017-04-14 16:35:58 -0700551 mCurrentSetColor = color;
552 updateIconColor();
553 }
554
555 private void updateIconColor() {
556 if (mCurrentSetColor != NO_COLOR) {
Adrian Roosc3e8cfc2017-07-17 17:09:39 +0200557 if (mMatrixColorFilter == null) {
558 mMatrix = new float[4 * 5];
559 mMatrixColorFilter = new ColorMatrixColorFilter(mMatrix);
560 }
561 int color = NotificationUtils.interpolateColors(
562 mCurrentSetColor, Color.WHITE, mDarkAmount);
563 updateTintMatrix(mMatrix, color, DARK_ALPHA_BOOST * mDarkAmount);
564 mMatrixColorFilter.setColorMatrixArray(mMatrix);
565 setColorFilter(mMatrixColorFilter);
566 invalidate(); // setColorFilter only invalidates if the filter instance changed.
Selim Cinek875ba9b2017-02-13 16:20:17 -0800567 } else {
Adrian Roosf3eacd32017-04-14 16:35:58 -0700568 mDozer.updateGrayscale(this, mDarkAmount);
Selim Cinek875ba9b2017-02-13 16:20:17 -0800569 }
Selim Cinek875ba9b2017-02-13 16:20:17 -0800570 }
571
Adrian Roosc3e8cfc2017-07-17 17:09:39 +0200572 /**
573 * Updates {@param array} such that it represents a matrix that changes RGB to {@param color}
Adrian Roos24dbff32017-08-02 16:57:11 +0200574 * and multiplies the alpha channel with the color's alpha+{@param alphaBoost}.
Adrian Roosc3e8cfc2017-07-17 17:09:39 +0200575 */
576 private static void updateTintMatrix(float[] array, int color, float alphaBoost) {
577 Arrays.fill(array, 0);
578 array[4] = Color.red(color);
579 array[9] = Color.green(color);
580 array[14] = Color.blue(color);
Adrian Roos24dbff32017-08-02 16:57:11 +0200581 array[18] = Color.alpha(color) / 255f + alphaBoost;
Adrian Roosc3e8cfc2017-07-17 17:09:39 +0200582 }
583
Selim Cinek875ba9b2017-02-13 16:20:17 -0800584 public void setIconColor(int iconColor, boolean animate) {
585 if (mIconColor != iconColor) {
586 mIconColor = iconColor;
587 if (mColorAnimator != null) {
588 mColorAnimator.cancel();
589 }
590 if (mCurrentSetColor == iconColor) {
591 return;
592 }
593 if (animate && mCurrentSetColor != NO_COLOR) {
594 mAnimationStartColor = mCurrentSetColor;
595 mColorAnimator = ValueAnimator.ofFloat(0.0f, 1.0f);
596 mColorAnimator.setInterpolator(Interpolators.FAST_OUT_SLOW_IN);
597 mColorAnimator.setDuration(ANIMATION_DURATION_FAST);
598 mColorAnimator.addUpdateListener(mColorUpdater);
599 mColorAnimator.addListener(new AnimatorListenerAdapter() {
600 @Override
601 public void onAnimationEnd(Animator animation) {
602 mColorAnimator = null;
603 mAnimationStartColor = NO_COLOR;
604 }
605 });
606 mColorAnimator.start();
607 } else {
608 setColorInternal(iconColor);
609 }
610 }
611 }
612
613 public int getStaticDrawableColor() {
614 return mDrawableColor;
615 }
616
Lucas Dupin83519da2017-06-21 11:58:31 -0700617 /**
618 * A drawable color that passes GAR on a specific background.
619 * This value is cached.
620 *
621 * @param backgroundColor Background to test against.
622 * @return GAR safe version of {@link StatusBarIconView#getStaticDrawableColor()}.
623 */
624 int getContrastedStaticDrawableColor(int backgroundColor) {
625 if (mCachedContrastBackgroundColor != backgroundColor) {
626 mCachedContrastBackgroundColor = backgroundColor;
627 updateContrastedStaticColor();
628 }
629 return mContrastedDrawableColor;
630 }
631
632 private void updateContrastedStaticColor() {
Lucas Dupinddbaf842017-06-23 09:30:49 -0700633 if (Color.alpha(mCachedContrastBackgroundColor) != 255) {
634 mContrastedDrawableColor = mDrawableColor;
Lucas Dupin83519da2017-06-21 11:58:31 -0700635 return;
636 }
637 // We'll modify the color if it doesn't pass GAR
638 int contrastedColor = mDrawableColor;
639 if (!NotificationColorUtil.satisfiesTextContrast(mCachedContrastBackgroundColor,
640 contrastedColor)) {
641 float[] hsl = new float[3];
642 ColorUtils.colorToHSL(mDrawableColor, hsl);
643 // This is basically a light grey, pushing the color will only distort it.
644 // Best thing to do in here is to fallback to the default color.
645 if (hsl[1] < 0.2f) {
646 contrastedColor = Notification.COLOR_DEFAULT;
647 }
648 contrastedColor = NotificationColorUtil.resolveContrastColor(mContext,
649 contrastedColor, mCachedContrastBackgroundColor);
650 }
651 mContrastedDrawableColor = contrastedColor;
652 }
653
Selim Cinek5b5beb012016-11-08 18:11:58 -0800654 public void setVisibleState(int state) {
655 setVisibleState(state, true /* animate */, null /* endRunnable */);
656 }
657
658 public void setVisibleState(int state, boolean animate) {
659 setVisibleState(state, animate, null);
660 }
661
662 @Override
663 public boolean hasOverlappingRendering() {
664 return false;
665 }
666
667 public void setVisibleState(int visibleState, boolean animate, Runnable endRunnable) {
Selim Cinek65d418e2016-11-29 15:42:34 -0800668 boolean runnableAdded = false;
Selim Cinek49014f82016-11-04 14:55:30 -0700669 if (visibleState != mVisibleState) {
670 mVisibleState = visibleState;
Selim Cinek5ece5a72017-02-15 11:47:42 -0800671 if (mIconAppearAnimator != null) {
672 mIconAppearAnimator.cancel();
673 }
674 if (mDotAnimator != null) {
675 mDotAnimator.cancel();
676 }
Selim Cinek5b5beb012016-11-08 18:11:58 -0800677 if (animate) {
Selim Cinek5b5beb012016-11-08 18:11:58 -0800678 float targetAmount = 0.0f;
679 Interpolator interpolator = Interpolators.FAST_OUT_LINEAR_IN;
680 if (visibleState == STATE_ICON) {
681 targetAmount = 1.0f;
682 interpolator = Interpolators.LINEAR_OUT_SLOW_IN;
683 }
Selim Cinek65d418e2016-11-29 15:42:34 -0800684 float currentAmount = getIconAppearAmount();
685 if (targetAmount != currentAmount) {
686 mIconAppearAnimator = ObjectAnimator.ofFloat(this, ICON_APPEAR_AMOUNT,
687 currentAmount, targetAmount);
688 mIconAppearAnimator.setInterpolator(interpolator);
Selim Cinek875ba9b2017-02-13 16:20:17 -0800689 mIconAppearAnimator.setDuration(ANIMATION_DURATION_FAST);
Selim Cinek65d418e2016-11-29 15:42:34 -0800690 mIconAppearAnimator.addListener(new AnimatorListenerAdapter() {
691 @Override
692 public void onAnimationEnd(Animator animation) {
693 mIconAppearAnimator = null;
694 runRunnable(endRunnable);
Selim Cinek5b5beb012016-11-08 18:11:58 -0800695 }
Selim Cinek65d418e2016-11-29 15:42:34 -0800696 });
697 mIconAppearAnimator.start();
698 runnableAdded = true;
699 }
Selim Cinek49014f82016-11-04 14:55:30 -0700700
Selim Cinek5b5beb012016-11-08 18:11:58 -0800701 targetAmount = visibleState == STATE_ICON ? 2.0f : 0.0f;
702 interpolator = Interpolators.FAST_OUT_LINEAR_IN;
703 if (visibleState == STATE_DOT) {
704 targetAmount = 1.0f;
705 interpolator = Interpolators.LINEAR_OUT_SLOW_IN;
706 }
Selim Cinek65d418e2016-11-29 15:42:34 -0800707 currentAmount = getDotAppearAmount();
708 if (targetAmount != currentAmount) {
709 mDotAnimator = ObjectAnimator.ofFloat(this, DOT_APPEAR_AMOUNT,
710 currentAmount, targetAmount);
711 mDotAnimator.setInterpolator(interpolator);
Selim Cinek875ba9b2017-02-13 16:20:17 -0800712 mDotAnimator.setDuration(ANIMATION_DURATION_FAST);
Selim Cinek65d418e2016-11-29 15:42:34 -0800713 final boolean runRunnable = !runnableAdded;
714 mDotAnimator.addListener(new AnimatorListenerAdapter() {
715 @Override
716 public void onAnimationEnd(Animator animation) {
717 mDotAnimator = null;
718 if (runRunnable) {
719 runRunnable(endRunnable);
720 }
721 }
722 });
723 mDotAnimator.start();
724 runnableAdded = true;
725 }
Selim Cinek5b5beb012016-11-08 18:11:58 -0800726 } else {
727 setIconAppearAmount(visibleState == STATE_ICON ? 1.0f : 0.0f);
Selim Cinek01a73f92016-12-06 16:13:42 -0800728 setDotAppearAmount(visibleState == STATE_DOT ? 1.0f
729 : visibleState == STATE_ICON ? 2.0f
730 : 0.0f);
Selim Cinek5b5beb012016-11-08 18:11:58 -0800731 }
Selim Cinek49014f82016-11-04 14:55:30 -0700732 }
Selim Cinek65d418e2016-11-29 15:42:34 -0800733 if (!runnableAdded) {
734 runRunnable(endRunnable);
735 }
736 }
737
738 private void runRunnable(Runnable runnable) {
739 if (runnable != null) {
740 runnable.run();
741 }
Selim Cinek49014f82016-11-04 14:55:30 -0700742 }
743
Selim Cinek5b5beb012016-11-08 18:11:58 -0800744 public void setIconAppearAmount(float iconAppearAmount) {
Selim Cinek9ef119c2017-03-01 15:13:36 -0800745 if (mIconAppearAmount != iconAppearAmount) {
746 mIconAppearAmount = iconAppearAmount;
747 invalidate();
748 }
Selim Cinek49014f82016-11-04 14:55:30 -0700749 }
750
751 public float getIconAppearAmount() {
752 return mIconAppearAmount;
753 }
754
755 public int getVisibleState() {
756 return mVisibleState;
757 }
758
759 public void setDotAppearAmount(float dotAppearAmount) {
Selim Cinek9ef119c2017-03-01 15:13:36 -0800760 if (mDotAppearAmount != dotAppearAmount) {
761 mDotAppearAmount = dotAppearAmount;
762 invalidate();
763 }
Selim Cinek49014f82016-11-04 14:55:30 -0700764 }
765
Selim Cinek2b549f42016-11-22 16:38:51 -0800766 @Override
767 public void setVisibility(int visibility) {
768 super.setVisibility(visibility);
769 if (mOnVisibilityChangedListener != null) {
770 mOnVisibilityChangedListener.onVisibilityChanged(visibility);
771 }
772 }
773
Selim Cinek49014f82016-11-04 14:55:30 -0700774 public float getDotAppearAmount() {
775 return mDotAppearAmount;
776 }
Selim Cinek2b549f42016-11-22 16:38:51 -0800777
778 public void setOnVisibilityChangedListener(OnVisibilityChangedListener listener) {
779 mOnVisibilityChangedListener = listener;
780 }
781
Adrian Roos456e0052017-04-04 16:44:29 -0700782 public void setDark(boolean dark, boolean fade, long delay) {
Adrian Roos456e0052017-04-04 16:44:29 -0700783 mDozer.setIntensityDark(f -> {
784 mDarkAmount = f;
Adrian Roos138f0342017-04-28 09:11:28 -0700785 updateIconScale();
Adrian Roos456e0052017-04-04 16:44:29 -0700786 updateDecorColor();
Adrian Roosf3eacd32017-04-14 16:35:58 -0700787 updateIconColor();
Adrian Roosfe0071f2017-07-06 18:45:10 +0200788 updateAllowAnimation();
Adrian Roos456e0052017-04-04 16:44:29 -0700789 }, dark, fade, delay);
790 }
791
Adrian Roosfe0071f2017-07-06 18:45:10 +0200792 private void updateAllowAnimation() {
793 if (mDarkAmount == 0 || mDarkAmount == 1) {
794 setAllowAnimation(mDarkAmount == 0);
795 }
796 }
797
Selim Cinek2b549f42016-11-22 16:38:51 -0800798 public interface OnVisibilityChangedListener {
799 void onVisibilityChanged(int newVisibility);
800 }
Joe Onorato0cbda992010-05-02 16:28:15 -0700801}