blob: 6552fe671794ffe652be4d057b08116f897203bd [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
Beverly1be62f42018-12-19 17:17:48 -050019import static com.android.systemui.plugins.DarkIconDispatcher.getTint;
Evan Lairde1d13c92018-03-20 16:58:01 -040020
Selim Cinek49014f82016-11-04 14:55:30 -070021import android.animation.Animator;
22import android.animation.AnimatorListenerAdapter;
23import android.animation.ObjectAnimator;
Selim Cinek875ba9b2017-02-13 16:20:17 -080024import android.animation.ValueAnimator;
Svetoslav Ganov6179ea32011-06-28 01:12:41 -070025import android.app.Notification;
Joe Onorato0cbda992010-05-02 16:28:15 -070026import android.content.Context;
Adrian Roosa8e18ef2016-05-26 17:17:02 -070027import android.content.pm.ApplicationInfo;
Evan Lairde1d13c92018-03-20 16:58:01 -040028import android.content.res.ColorStateList;
Selim Cinek3e7592d2016-04-11 09:35:54 +080029import android.content.res.Configuration;
Joe Onorato0cbda992010-05-02 16:28:15 -070030import android.content.res.Resources;
Joe Onorato0cbda992010-05-02 16:28:15 -070031import android.graphics.Canvas;
Adrian Roos456e0052017-04-04 16:44:29 -070032import android.graphics.Color;
Adrian Roosc3e8cfc2017-07-17 17:09:39 +020033import android.graphics.ColorMatrixColorFilter;
Joe Onorato6c01a112010-10-04 17:38:47 -040034import android.graphics.Paint;
35import android.graphics.Rect;
John Spurlockde84f0e2013-06-12 12:41:00 -040036import android.graphics.drawable.Drawable;
Dan Sandlerd63f9322015-05-06 15:18:49 -040037import android.graphics.drawable.Icon;
Adrian Roosa8e18ef2016-05-26 17:17:02 -070038import android.os.Parcelable;
Jeff Sharkeyded653b2012-09-27 19:09:24 -070039import android.os.UserHandle;
Selim Cinek72fc8db2017-06-06 18:07:47 -070040import android.service.notification.StatusBarNotification;
Svetoslav Ganov6179ea32011-06-28 01:12:41 -070041import android.text.TextUtils;
Daniel Sandler05e24142011-11-10 11:56:49 -050042import android.util.AttributeSet;
Selim Cinek49014f82016-11-04 14:55:30 -070043import android.util.FloatProperty;
Joe Onoratof9ec03c2010-09-23 15:09:18 -070044import android.util.Log;
Selim Cinek49014f82016-11-04 14:55:30 -070045import android.util.Property;
Anthony Chen55e8e1e2016-01-08 10:31:46 -080046import android.util.TypedValue;
Joe Onorato0cbda992010-05-02 16:28:15 -070047import android.view.ViewDebug;
Svetoslav Ganov6179ea32011-06-28 01:12:41 -070048import android.view.accessibility.AccessibilityEvent;
Selim Cinek49014f82016-11-04 14:55:30 -070049import android.view.animation.Interpolator;
Winsonc0d70582016-01-29 10:24:39 -080050
Lucas Dupinde07d212018-10-23 17:47:03 -070051import androidx.core.graphics.ColorUtils;
52
Joe Onorato0cbda992010-05-02 16:28:15 -070053import com.android.internal.statusbar.StatusBarIcon;
Lucas Dupina291d192018-06-07 13:59:42 -070054import com.android.internal.util.ContrastColorUtil;
Selim Cinek49014f82016-11-04 14:55:30 -070055import com.android.systemui.Interpolators;
Joe Onorato6c01a112010-10-04 17:38:47 -040056import com.android.systemui.R;
Adrian Roos456e0052017-04-04 16:44:29 -070057import com.android.systemui.statusbar.notification.NotificationIconDozeHelper;
Selim Cinek49014f82016-11-04 14:55:30 -070058import com.android.systemui.statusbar.notification.NotificationUtils;
Joe Onorato6c01a112010-10-04 17:38:47 -040059
John Spurlockde84f0e2013-06-12 12:41:00 -040060import java.text.NumberFormat;
Adrian Roosc3e8cfc2017-07-17 17:09:39 +020061import java.util.Arrays;
John Spurlockde84f0e2013-06-12 12:41:00 -040062
Evan Lairde1d13c92018-03-20 16:58:01 -040063public class StatusBarIconView extends AnimatedImageView implements StatusIconDisplayable {
Selim Cinek875ba9b2017-02-13 16:20:17 -080064 public static final int NO_COLOR = 0;
Adrian Roosc3e8cfc2017-07-17 17:09:39 +020065
66 /**
67 * Multiply alpha values with (1+DARK_ALPHA_BOOST) when dozing. The chosen value boosts
68 * everything above 30% to 50%, making it appear on 1bit color depths.
69 */
70 private static final float DARK_ALPHA_BOOST = 0.67f;
Evan Laird39ea8102018-05-18 19:49:07 -040071 /**
72 * Status icons are currently drawn with the intention of being 17dp tall, but we
73 * want to scale them (in a way that doesn't require an asset dump) down 2dp. So
Fabian Kozynski61e1ba62019-05-15 10:04:39 -040074 * 17dp * (15 / 17) = 15dp, the new height. After the first call to {@link #reloadDimens} all
75 * values will be in px.
Evan Laird39ea8102018-05-18 19:49:07 -040076 */
Fabian Kozynski61e1ba62019-05-15 10:04:39 -040077 private float mSystemIconDesiredHeight = 15f;
78 private float mSystemIconIntrinsicHeight = 17f;
79 private float mSystemIconDefaultScale = mSystemIconDesiredHeight / mSystemIconIntrinsicHeight;
Selim Cinek875ba9b2017-02-13 16:20:17 -080080 private final int ANIMATION_DURATION_FAST = 100;
81
Selim Cinek49014f82016-11-04 14:55:30 -070082 public static final int STATE_ICON = 0;
83 public static final int STATE_DOT = 1;
84 public static final int STATE_HIDDEN = 2;
Joe Onorato0cbda992010-05-02 16:28:15 -070085
Selim Cinek49014f82016-11-04 14:55:30 -070086 private static final String TAG = "StatusBarIconView";
87 private static final Property<StatusBarIconView, Float> ICON_APPEAR_AMOUNT
88 = new FloatProperty<StatusBarIconView>("iconAppearAmount") {
89
90 @Override
91 public void setValue(StatusBarIconView object, float value) {
92 object.setIconAppearAmount(value);
93 }
94
95 @Override
96 public Float get(StatusBarIconView object) {
97 return object.getIconAppearAmount();
98 }
99 };
Selim Cinek5b5beb012016-11-08 18:11:58 -0800100 private static final Property<StatusBarIconView, Float> DOT_APPEAR_AMOUNT
Selim Cinek49014f82016-11-04 14:55:30 -0700101 = new FloatProperty<StatusBarIconView>("dot_appear_amount") {
102
103 @Override
104 public void setValue(StatusBarIconView object, float value) {
105 object.setDotAppearAmount(value);
106 }
107
108 @Override
109 public Float get(StatusBarIconView object) {
110 return object.getDotAppearAmount();
111 }
112 };
113
114 private boolean mAlwaysScaleIcon;
Adrian Roos138f0342017-04-28 09:11:28 -0700115 private int mStatusBarIconDrawingSizeDark = 1;
116 private int mStatusBarIconDrawingSize = 1;
117 private int mStatusBarIconSize = 1;
Joe Onorato0cbda992010-05-02 16:28:15 -0700118 private StatusBarIcon mIcon;
119 @ViewDebug.ExportedProperty private String mSlot;
Joe Onorato6c01a112010-10-04 17:38:47 -0400120 private Drawable mNumberBackground;
121 private Paint mNumberPain;
122 private int mNumberX;
123 private int mNumberY;
124 private String mNumberText;
Selim Cinek72fc8db2017-06-06 18:07:47 -0700125 private StatusBarNotification mNotification;
Jason Monk3b230072015-05-29 11:11:29 -0400126 private final boolean mBlocked;
Selim Cinek3e7592d2016-04-11 09:35:54 +0800127 private int mDensity;
Lucas Dupinde07d212018-10-23 17:47:03 -0700128 private boolean mNightMode;
Selim Cinek281c2022016-10-13 19:14:43 -0700129 private float mIconScale = 1.0f;
Evan Lairdbaccb462018-06-01 16:29:02 -0400130 private final Paint mDotPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
Selim Cinek49014f82016-11-04 14:55:30 -0700131 private float mDotRadius;
132 private int mStaticDotRadius;
133 private int mVisibleState = STATE_ICON;
134 private float mIconAppearAmount = 1.0f;
135 private ObjectAnimator mIconAppearAnimator;
136 private ObjectAnimator mDotAnimator;
137 private float mDotAppearAmount;
Selim Cinek2b549f42016-11-22 16:38:51 -0800138 private OnVisibilityChangedListener mOnVisibilityChangedListener;
Selim Cinek875ba9b2017-02-13 16:20:17 -0800139 private int mDrawableColor;
140 private int mIconColor;
Adrian Roos456e0052017-04-04 16:44:29 -0700141 private int mDecorColor;
142 private float mDarkAmount;
Selim Cinek875ba9b2017-02-13 16:20:17 -0800143 private ValueAnimator mColorAnimator;
144 private int mCurrentSetColor = NO_COLOR;
145 private int mAnimationStartColor = NO_COLOR;
146 private final ValueAnimator.AnimatorUpdateListener mColorUpdater
147 = animation -> {
148 int newColor = NotificationUtils.interpolateColors(mAnimationStartColor, mIconColor,
149 animation.getAnimatedFraction());
150 setColorInternal(newColor);
151 };
Adrian Roos456e0052017-04-04 16:44:29 -0700152 private final NotificationIconDozeHelper mDozer;
Lucas Dupin83519da2017-06-21 11:58:31 -0700153 private int mContrastedDrawableColor;
154 private int mCachedContrastBackgroundColor = NO_COLOR;
Adrian Roosc3e8cfc2017-07-17 17:09:39 +0200155 private float[] mMatrix;
156 private ColorMatrixColorFilter mMatrixColorFilter;
Selim Cinekd95ca7c2017-07-26 12:20:38 -0700157 private boolean mIsInShelf;
Selim Cinek887da3c2017-10-06 13:37:32 -0700158 private Runnable mLayoutRunnable;
Selim Cinekd03518c2018-03-15 12:13:51 -0700159 private boolean mDismissed;
160 private Runnable mOnDismissListener;
Joe Onorato0cbda992010-05-02 16:28:15 -0700161
Selim Cinek72fc8db2017-06-06 18:07:47 -0700162 public StatusBarIconView(Context context, String slot, StatusBarNotification sbn) {
163 this(context, slot, sbn, false);
Jason Monk3b230072015-05-29 11:11:29 -0400164 }
165
Selim Cinek72fc8db2017-06-06 18:07:47 -0700166 public StatusBarIconView(Context context, String slot, StatusBarNotification sbn,
Jason Monk3b230072015-05-29 11:11:29 -0400167 boolean blocked) {
Joe Onorato0cbda992010-05-02 16:28:15 -0700168 super(context);
Adrian Roos456e0052017-04-04 16:44:29 -0700169 mDozer = new NotificationIconDozeHelper(context);
Jason Monk3b230072015-05-29 11:11:29 -0400170 mBlocked = blocked;
Joe Onorato0cbda992010-05-02 16:28:15 -0700171 mSlot = slot;
Joe Onorato6c01a112010-10-04 17:38:47 -0400172 mNumberPain = new Paint();
173 mNumberPain.setTextAlign(Paint.Align.CENTER);
Alan Viverette4a357cd2015-03-18 18:37:18 -0700174 mNumberPain.setColor(context.getColor(R.drawable.notification_number_text_color));
Joe Onorato6c01a112010-10-04 17:38:47 -0400175 mNumberPain.setAntiAlias(true);
Selim Cinek72fc8db2017-06-06 18:07:47 -0700176 setNotification(sbn);
Selim Cinek3e7592d2016-04-11 09:35:54 +0800177 setScaleType(ScaleType.CENTER);
178 mDensity = context.getResources().getDisplayMetrics().densityDpi;
Lucas Dupinde07d212018-10-23 17:47:03 -0700179 Configuration configuration = context.getResources().getConfiguration();
180 mNightMode = (configuration.uiMode & Configuration.UI_MODE_NIGHT_MASK)
181 == Configuration.UI_MODE_NIGHT_YES;
182 initializeDecorColor();
Selim Cinek49014f82016-11-04 14:55:30 -0700183 reloadDimens();
Evan Laird20b87bf2018-04-12 09:54:11 -0400184 maybeUpdateIconScaleDimens();
Selim Cinek3e7592d2016-04-11 09:35:54 +0800185 }
Daniel Sandler26c84b12011-07-27 00:09:40 -0400186
Evan Laird20b87bf2018-04-12 09:54:11 -0400187 /** Should always be preceded by {@link #reloadDimens()} */
Adrian Roos138f0342017-04-28 09:11:28 -0700188 private void maybeUpdateIconScaleDimens() {
Daniel Sandler7579bca2011-08-18 15:47:26 -0400189 // We do not resize and scale system icons (on the right), only notification icons (on the
190 // left).
Selim Cinek3e7592d2016-04-11 09:35:54 +0800191 if (mNotification != null || mAlwaysScaleIcon) {
Evan Laird39ea8102018-05-18 19:49:07 -0400192 updateIconScaleForNotifications();
193 } else {
194 updateIconScaleForSystemIcons();
Daniel Sandler7579bca2011-08-18 15:47:26 -0400195 }
Selim Cinek3e7592d2016-04-11 09:35:54 +0800196 }
Daniel Sandlerabff0322011-09-27 11:19:34 -0400197
Evan Laird39ea8102018-05-18 19:49:07 -0400198 private void updateIconScaleForNotifications() {
Adrian Roos138f0342017-04-28 09:11:28 -0700199 final float imageBounds = NotificationUtils.interpolate(
200 mStatusBarIconDrawingSize,
201 mStatusBarIconDrawingSizeDark,
202 mDarkAmount);
203 final int outerBounds = mStatusBarIconSize;
Selim Cinek281c2022016-10-13 19:14:43 -0700204 mIconScale = (float)imageBounds / (float)outerBounds;
Selim Cinek3ddda6d2018-06-08 00:26:48 -0700205 updatePivot();
Selim Cinek281c2022016-10-13 19:14:43 -0700206 }
207
Fabian Kozynski718dab92019-05-02 17:28:02 -0400208 // Makes sure that all icons are scaled to the same height (15dp). If we cannot get a height
209 // for the icon, it uses the default SCALE (15f / 17f) which is the old behavior
Evan Laird39ea8102018-05-18 19:49:07 -0400210 private void updateIconScaleForSystemIcons() {
Fabian Kozynski61e1ba62019-05-15 10:04:39 -0400211 float iconHeight = getIconHeight();
Fabian Kozynski718dab92019-05-02 17:28:02 -0400212 if (iconHeight != 0) {
Fabian Kozynski61e1ba62019-05-15 10:04:39 -0400213 mIconScale = mSystemIconDesiredHeight / iconHeight;
Fabian Kozynski718dab92019-05-02 17:28:02 -0400214 } else {
Fabian Kozynski61e1ba62019-05-15 10:04:39 -0400215 mIconScale = mSystemIconDefaultScale;
Fabian Kozynski718dab92019-05-02 17:28:02 -0400216 }
217 }
218
Fabian Kozynski61e1ba62019-05-15 10:04:39 -0400219 private float getIconHeight() {
Fabian Kozynski718dab92019-05-02 17:28:02 -0400220 Drawable d = getDrawable();
221 if (d != null) {
Fabian Kozynski61e1ba62019-05-15 10:04:39 -0400222 return (float) getDrawable().getIntrinsicHeight();
Fabian Kozynski718dab92019-05-02 17:28:02 -0400223 } else {
Fabian Kozynski61e1ba62019-05-15 10:04:39 -0400224 return mSystemIconIntrinsicHeight;
Fabian Kozynski718dab92019-05-02 17:28:02 -0400225 }
Evan Laird39ea8102018-05-18 19:49:07 -0400226 }
227
Lucas Dupin7fc9dc12019-01-03 09:19:43 -0800228 public float getIconScaleFullyDark() {
229 return (float) mStatusBarIconDrawingSizeDark / mStatusBarIconDrawingSize;
230 }
231
Selim Cinek281c2022016-10-13 19:14:43 -0700232 public float getIconScale() {
233 return mIconScale;
Selim Cinek3e7592d2016-04-11 09:35:54 +0800234 }
235
236 @Override
237 protected void onConfigurationChanged(Configuration newConfig) {
238 super.onConfigurationChanged(newConfig);
239 int density = newConfig.densityDpi;
240 if (density != mDensity) {
241 mDensity = density;
Evan Laird20b87bf2018-04-12 09:54:11 -0400242 reloadDimens();
Selim Cinek3e7592d2016-04-11 09:35:54 +0800243 updateDrawable();
Fabian Kozynski718dab92019-05-02 17:28:02 -0400244 maybeUpdateIconScaleDimens();
Selim Cinek49014f82016-11-04 14:55:30 -0700245 }
Lucas Dupinde07d212018-10-23 17:47:03 -0700246 boolean nightMode = (newConfig.uiMode & Configuration.UI_MODE_NIGHT_MASK)
247 == Configuration.UI_MODE_NIGHT_YES;
248 if (nightMode != mNightMode) {
249 mNightMode = nightMode;
250 initializeDecorColor();
251 }
Selim Cinek49014f82016-11-04 14:55:30 -0700252 }
253
254 private void reloadDimens() {
255 boolean applyRadius = mDotRadius == mStaticDotRadius;
Evan Laird20b87bf2018-04-12 09:54:11 -0400256 Resources res = getResources();
257 mStaticDotRadius = res.getDimensionPixelSize(R.dimen.overflow_dot_radius);
258 mStatusBarIconSize = res.getDimensionPixelSize(R.dimen.status_bar_icon_size);
259 mStatusBarIconDrawingSizeDark =
260 res.getDimensionPixelSize(R.dimen.status_bar_icon_drawing_size_dark);
261 mStatusBarIconDrawingSize =
262 res.getDimensionPixelSize(R.dimen.status_bar_icon_drawing_size);
Selim Cinek49014f82016-11-04 14:55:30 -0700263 if (applyRadius) {
264 mDotRadius = mStaticDotRadius;
Selim Cinek3e7592d2016-04-11 09:35:54 +0800265 }
Fabian Kozynski61e1ba62019-05-15 10:04:39 -0400266 mSystemIconDesiredHeight = res.getDimension(
267 com.android.internal.R.dimen.status_bar_system_icon_size);
268 mSystemIconIntrinsicHeight = res.getDimension(
269 com.android.internal.R.dimen.status_bar_system_icon_intrinsic_size);
270 mSystemIconDefaultScale = mSystemIconDesiredHeight / mSystemIconIntrinsicHeight;
Joe Onorato0cbda992010-05-02 16:28:15 -0700271 }
272
Selim Cinek72fc8db2017-06-06 18:07:47 -0700273 public void setNotification(StatusBarNotification notification) {
Christoph Studera0506e72014-07-31 20:27:39 +0200274 mNotification = notification;
Selim Cinek72fc8db2017-06-06 18:07:47 -0700275 if (notification != null) {
276 setContentDescription(notification.getNotification());
277 }
Fabian Kozynski61e1ba62019-05-15 10:04:39 -0400278 maybeUpdateIconScaleDimens();
Christoph Studera0506e72014-07-31 20:27:39 +0200279 }
280
Daniel Sandler05e24142011-11-10 11:56:49 -0500281 public StatusBarIconView(Context context, AttributeSet attrs) {
282 super(context, attrs);
Adrian Roos456e0052017-04-04 16:44:29 -0700283 mDozer = new NotificationIconDozeHelper(context);
Jason Monk3b230072015-05-29 11:11:29 -0400284 mBlocked = false;
Selim Cinek3e7592d2016-04-11 09:35:54 +0800285 mAlwaysScaleIcon = true;
Evan Laird20b87bf2018-04-12 09:54:11 -0400286 reloadDimens();
Fabian Kozynski61e1ba62019-05-15 10:04:39 -0400287 maybeUpdateIconScaleDimens();
Selim Cinek3e7592d2016-04-11 09:35:54 +0800288 mDensity = context.getResources().getDisplayMetrics().densityDpi;
Daniel Sandler05e24142011-11-10 11:56:49 -0500289 }
290
Joe Onorato0cbda992010-05-02 16:28:15 -0700291 private static boolean streq(String a, String b) {
Joe Onorato66d7d012010-05-14 10:05:10 -0700292 if (a == b) {
293 return true;
294 }
Joe Onorato0cbda992010-05-02 16:28:15 -0700295 if (a == null && b != null) {
296 return false;
297 }
298 if (a != null && b == null) {
299 return false;
300 }
301 return a.equals(b);
302 }
303
Dan Sandlerd63f9322015-05-06 15:18:49 -0400304 public boolean equalIcons(Icon a, Icon b) {
305 if (a == b) return true;
306 if (a.getType() != b.getType()) return false;
307 switch (a.getType()) {
308 case Icon.TYPE_RESOURCE:
309 return a.getResPackage().equals(b.getResPackage()) && a.getResId() == b.getResId();
310 case Icon.TYPE_URI:
311 return a.getUriString().equals(b.getUriString());
312 default:
313 return false;
314 }
315 }
Joe Onorato005847b2010-06-04 16:08:02 -0400316 /**
317 * Returns whether the set succeeded.
318 */
319 public boolean set(StatusBarIcon icon) {
Dan Sandlerd63f9322015-05-06 15:18:49 -0400320 final boolean iconEquals = mIcon != null && equalIcons(mIcon.icon, icon.icon);
Joe Onorato005847b2010-06-04 16:08:02 -0400321 final boolean levelEquals = iconEquals
322 && mIcon.iconLevel == icon.iconLevel;
323 final boolean visibilityEquals = mIcon != null
324 && mIcon.visible == icon.visible;
Joe Onorato6c01a112010-10-04 17:38:47 -0400325 final boolean numberEquals = mIcon != null
326 && mIcon.number == icon.number;
327 mIcon = icon.clone();
Svetoslav Ganov6179ea32011-06-28 01:12:41 -0700328 setContentDescription(icon.contentDescription);
Joe Onorato005847b2010-06-04 16:08:02 -0400329 if (!iconEquals) {
Fabrice Di Meglio82fca5d2013-01-09 15:42:31 -0800330 if (!updateDrawable(false /* no clear */)) return false;
Selim Cinek2b6eb8032016-12-29 14:22:21 +0100331 // we have to clear the grayscale tag since it may have changed
332 setTag(R.id.icon_is_grayscale, null);
Fabian Kozynski718dab92019-05-02 17:28:02 -0400333 // Maybe set scale based on icon height
334 maybeUpdateIconScaleDimens();
Joe Onorato0cbda992010-05-02 16:28:15 -0700335 }
Joe Onorato005847b2010-06-04 16:08:02 -0400336 if (!levelEquals) {
337 setImageLevel(icon.iconLevel);
Joe Onorato0cbda992010-05-02 16:28:15 -0700338 }
Daniel Sandler26c84b12011-07-27 00:09:40 -0400339
Joe Onorato6c01a112010-10-04 17:38:47 -0400340 if (!numberEquals) {
John Spurlock01534782014-01-13 11:59:22 -0500341 if (icon.number > 0 && getContext().getResources().getBoolean(
Joe Onorato8595a3d2010-11-19 18:12:07 -0800342 R.bool.config_statusBarShowNumber)) {
Joe Onorato6c01a112010-10-04 17:38:47 -0400343 if (mNumberBackground == null) {
344 mNumberBackground = getContext().getResources().getDrawable(
345 R.drawable.ic_notification_overlay);
346 }
347 placeNumber();
348 } else {
349 mNumberBackground = null;
350 mNumberText = null;
351 }
352 invalidate();
353 }
Joe Onorato005847b2010-06-04 16:08:02 -0400354 if (!visibilityEquals) {
Jason Monk3b230072015-05-29 11:11:29 -0400355 setVisibility(icon.visible && !mBlocked ? VISIBLE : GONE);
Joe Onorato005847b2010-06-04 16:08:02 -0400356 }
Joe Onorato005847b2010-06-04 16:08:02 -0400357 return true;
Joe Onorato0cbda992010-05-02 16:28:15 -0700358 }
359
Fabrice Di Meglio82fca5d2013-01-09 15:42:31 -0800360 public void updateDrawable() {
361 updateDrawable(true /* with clear */);
362 }
363
364 private boolean updateDrawable(boolean withClear) {
Jorim Jaggi66ac1332015-01-21 19:22:26 +0100365 if (mIcon == null) {
366 return false;
367 }
Adrian Roosfb2d0cc2017-01-31 15:10:00 -0800368 Drawable drawable;
369 try {
370 drawable = getIcon(mIcon);
371 } catch (OutOfMemoryError e) {
372 Log.w(TAG, "OOM while inflating " + mIcon.icon + " for slot " + mSlot);
373 return false;
374 }
375
Fabrice Di Meglio82fca5d2013-01-09 15:42:31 -0800376 if (drawable == null) {
Adrian Roosfb2d0cc2017-01-31 15:10:00 -0800377 Log.w(TAG, "No icon for slot " + mSlot + "; " + mIcon.icon);
Fabrice Di Meglio82fca5d2013-01-09 15:42:31 -0800378 return false;
379 }
380 if (withClear) {
381 setImageDrawable(null);
382 }
383 setImageDrawable(drawable);
384 return true;
385 }
386
Selim Cinek72fc8db2017-06-06 18:07:47 -0700387 public Icon getSourceIcon() {
388 return mIcon.icon;
389 }
390
Joe Onoratof5510542010-06-01 07:46:41 -0700391 private Drawable getIcon(StatusBarIcon icon) {
392 return getIcon(getContext(), icon);
393 }
394
Joe Onorato0cbda992010-05-02 16:28:15 -0700395 /**
Dan Sandlerd63f9322015-05-06 15:18:49 -0400396 * Returns the right icon to use for this item
John Spurlock209bede2013-07-17 12:23:27 -0400397 *
Dan Sandlerd63f9322015-05-06 15:18:49 -0400398 * @param context Context to use to get resources
Joe Onorato0cbda992010-05-02 16:28:15 -0700399 * @return Drawable for this item, or null if the package or item could not
400 * be found
401 */
Anthony Chen55e8e1e2016-01-08 10:31:46 -0800402 public static Drawable getIcon(Context context, StatusBarIcon statusBarIcon) {
403 int userId = statusBarIcon.user.getIdentifier();
Dan Sandlerd63f9322015-05-06 15:18:49 -0400404 if (userId == UserHandle.USER_ALL) {
Xiaohui Chen87d0e252015-07-30 15:38:16 -0700405 userId = UserHandle.USER_SYSTEM;
Joe Onorato0cbda992010-05-02 16:28:15 -0700406 }
Anthony Chen55e8e1e2016-01-08 10:31:46 -0800407
408 Drawable icon = statusBarIcon.icon.loadDrawableAsUser(context, userId);
409
410 TypedValue typedValue = new TypedValue();
411 context.getResources().getValue(R.dimen.status_bar_icon_scale_factor, typedValue, true);
412 float scaleFactor = typedValue.getFloat();
413
414 // No need to scale the icon, so return it as is.
415 if (scaleFactor == 1.f) {
416 return icon;
417 }
418
419 return new ScalingDrawableWrapper(icon, scaleFactor);
Joe Onorato0cbda992010-05-02 16:28:15 -0700420 }
Joe Onoratob77f53b2010-05-28 19:59:51 -0400421
422 public StatusBarIcon getStatusBarIcon() {
423 return mIcon;
424 }
Joe Onoratof9ec03c2010-09-23 15:09:18 -0700425
Svetoslav Ganov6179ea32011-06-28 01:12:41 -0700426 @Override
427 public void onInitializeAccessibilityEvent(AccessibilityEvent event) {
428 super.onInitializeAccessibilityEvent(event);
429 if (mNotification != null) {
Selim Cinek72fc8db2017-06-06 18:07:47 -0700430 event.setParcelableData(mNotification.getNotification());
Svetoslav Ganov6179ea32011-06-28 01:12:41 -0700431 }
432 }
433
434 @Override
Joe Onorato6c01a112010-10-04 17:38:47 -0400435 protected void onSizeChanged(int w, int h, int oldw, int oldh) {
436 super.onSizeChanged(w, h, oldw, oldh);
437 if (mNumberBackground != null) {
438 placeNumber();
439 }
440 }
441
Svetoslav Ganov6179ea32011-06-28 01:12:41 -0700442 @Override
Jorim Jaggi66ac1332015-01-21 19:22:26 +0100443 public void onRtlPropertiesChanged(int layoutDirection) {
444 super.onRtlPropertiesChanged(layoutDirection);
445 updateDrawable();
446 }
447
448 @Override
Joe Onorato6c01a112010-10-04 17:38:47 -0400449 protected void onDraw(Canvas canvas) {
Selim Cinek49014f82016-11-04 14:55:30 -0700450 if (mIconAppearAmount > 0.0f) {
451 canvas.save();
452 canvas.scale(mIconScale * mIconAppearAmount, mIconScale * mIconAppearAmount,
453 getWidth() / 2, getHeight() / 2);
454 super.onDraw(canvas);
455 canvas.restore();
456 }
Joe Onorato6c01a112010-10-04 17:38:47 -0400457
458 if (mNumberBackground != null) {
459 mNumberBackground.draw(canvas);
460 canvas.drawText(mNumberText, mNumberX, mNumberY, mNumberPain);
461 }
Selim Cinek49014f82016-11-04 14:55:30 -0700462 if (mDotAppearAmount != 0.0f) {
463 float radius;
Evan Lairdbaccb462018-06-01 16:29:02 -0400464 float alpha = Color.alpha(mDecorColor) / 255.f;
Selim Cinek49014f82016-11-04 14:55:30 -0700465 if (mDotAppearAmount <= 1.0f) {
466 radius = mDotRadius * mDotAppearAmount;
Selim Cinek49014f82016-11-04 14:55:30 -0700467 } else {
468 float fadeOutAmount = mDotAppearAmount - 1.0f;
Evan Lairdbaccb462018-06-01 16:29:02 -0400469 alpha = alpha * (1.0f - fadeOutAmount);
Selim Cinek49014f82016-11-04 14:55:30 -0700470 radius = NotificationUtils.interpolate(mDotRadius, getWidth() / 4, fadeOutAmount);
471 }
472 mDotPaint.setAlpha((int) (alpha * 255));
Evan Laird20b87bf2018-04-12 09:54:11 -0400473 canvas.drawCircle(mStatusBarIconSize / 2, getHeight() / 2, radius, mDotPaint);
Selim Cinek49014f82016-11-04 14:55:30 -0700474 }
Joe Onorato6c01a112010-10-04 17:38:47 -0400475 }
476
Svetoslav Ganov6179ea32011-06-28 01:12:41 -0700477 @Override
Joe Onoratof9ec03c2010-09-23 15:09:18 -0700478 protected void debug(int depth) {
479 super.debug(depth);
480 Log.d("View", debugIndent(depth) + "slot=" + mSlot);
481 Log.d("View", debugIndent(depth) + "icon=" + mIcon);
482 }
Joe Onorato6c01a112010-10-04 17:38:47 -0400483
484 void placeNumber() {
Daniel Sandlerebce0112011-06-16 16:44:51 -0400485 final String str;
John Spurlock01534782014-01-13 11:59:22 -0500486 final int tooBig = getContext().getResources().getInteger(
Daniel Sandlerebce0112011-06-16 16:44:51 -0400487 android.R.integer.status_bar_notification_info_maxnum);
488 if (mIcon.number > tooBig) {
John Spurlock01534782014-01-13 11:59:22 -0500489 str = getContext().getResources().getString(
Daniel Sandlerebce0112011-06-16 16:44:51 -0400490 android.R.string.status_bar_notification_info_overflow);
491 } else {
492 NumberFormat f = NumberFormat.getIntegerInstance();
493 str = f.format(mIcon.number);
494 }
495 mNumberText = str;
496
Joe Onorato6c01a112010-10-04 17:38:47 -0400497 final int w = getWidth();
498 final int h = getHeight();
499 final Rect r = new Rect();
500 mNumberPain.getTextBounds(str, 0, str.length(), r);
501 final int tw = r.right - r.left;
502 final int th = r.bottom - r.top;
503 mNumberBackground.getPadding(r);
504 int dw = r.left + tw + r.right;
505 if (dw < mNumberBackground.getMinimumWidth()) {
506 dw = mNumberBackground.getMinimumWidth();
507 }
508 mNumberX = w-r.right-((dw-r.right-r.left)/2);
509 int dh = r.top + th + r.bottom;
510 if (dh < mNumberBackground.getMinimumWidth()) {
511 dh = mNumberBackground.getMinimumWidth();
512 }
513 mNumberY = h-r.bottom-((dh-r.top-th-r.bottom)/2);
514 mNumberBackground.setBounds(w-dw, h-dh, w, h);
515 }
Svetoslav Ganov6179ea32011-06-28 01:12:41 -0700516
517 private void setContentDescription(Notification notification) {
518 if (notification != null) {
Adrian Rooseba05822016-04-22 17:09:27 -0700519 String d = contentDescForNotification(mContext, notification);
520 if (!TextUtils.isEmpty(d)) {
521 setContentDescription(d);
Svetoslav Ganov6179ea32011-06-28 01:12:41 -0700522 }
523 }
524 }
Daniel Sandler7579bca2011-08-18 15:47:26 -0400525
526 public String toString() {
John Spurlock209bede2013-07-17 12:23:27 -0400527 return "StatusBarIconView(slot=" + mSlot + " icon=" + mIcon
Daniel Sandler7579bca2011-08-18 15:47:26 -0400528 + " notification=" + mNotification + ")";
529 }
Jason Monk3b230072015-05-29 11:11:29 -0400530
Selim Cinek72fc8db2017-06-06 18:07:47 -0700531 public StatusBarNotification getNotification() {
532 return mNotification;
533 }
534
Jason Monk3b230072015-05-29 11:11:29 -0400535 public String getSlot() {
536 return mSlot;
537 }
Adrian Rooseba05822016-04-22 17:09:27 -0700538
539
540 public static String contentDescForNotification(Context c, Notification n) {
Adrian Roosa8e18ef2016-05-26 17:17:02 -0700541 String appName = "";
542 try {
543 Notification.Builder builder = Notification.Builder.recoverBuilder(c, n);
544 appName = builder.loadHeaderAppName();
545 } catch (RuntimeException e) {
546 Log.e(TAG, "Unable to recover builder", e);
547 // Trying to get the app name from the app info instead.
548 Parcelable appInfo = n.extras.getParcelable(
549 Notification.EXTRA_BUILDER_APPLICATION_INFO);
550 if (appInfo instanceof ApplicationInfo) {
551 appName = String.valueOf(((ApplicationInfo) appInfo).loadLabel(
552 c.getPackageManager()));
553 }
554 }
Adrian Roos51548e62016-04-28 11:20:26 -0700555
Julia Reynolds7f9ce782016-05-17 15:34:34 -0400556 CharSequence title = n.extras.getCharSequence(Notification.EXTRA_TITLE);
Adrian Roosf76600e2017-04-19 15:32:43 -0700557 CharSequence text = n.extras.getCharSequence(Notification.EXTRA_TEXT);
Adrian Rooseba05822016-04-22 17:09:27 -0700558 CharSequence ticker = n.tickerText;
Adrian Roos51548e62016-04-28 11:20:26 -0700559
Adrian Roosf76600e2017-04-19 15:32:43 -0700560 // Some apps just put the app name into the title
561 CharSequence titleOrText = TextUtils.equals(title, appName) ? text : title;
562
563 CharSequence desc = !TextUtils.isEmpty(titleOrText) ? titleOrText
Adrian Roos6e782a52017-03-30 18:34:10 -0700564 : !TextUtils.isEmpty(ticker) ? ticker : "";
Adrian Roos51548e62016-04-28 11:20:26 -0700565
566 return c.getString(R.string.accessibility_desc_notification_icon, appName, desc);
Adrian Rooseba05822016-04-22 17:09:27 -0700567 }
Adrian Roos51548e62016-04-28 11:20:26 -0700568
Selim Cinek875ba9b2017-02-13 16:20:17 -0800569 /**
570 * Set the color that is used to draw decoration like the overflow dot. This will not be applied
571 * to the drawable.
572 */
573 public void setDecorColor(int iconTint) {
Adrian Roos456e0052017-04-04 16:44:29 -0700574 mDecorColor = iconTint;
575 updateDecorColor();
576 }
577
Lucas Dupinde07d212018-10-23 17:47:03 -0700578 private void initializeDecorColor() {
579 if (mNotification != null) {
580 setDecorColor(getContext().getColor(mNightMode
581 ? com.android.internal.R.color.notification_default_color_dark
582 : com.android.internal.R.color.notification_default_color_light));
583 }
584 }
585
Adrian Roos456e0052017-04-04 16:44:29 -0700586 private void updateDecorColor() {
587 int color = NotificationUtils.interpolateColors(mDecorColor, Color.WHITE, mDarkAmount);
588 if (mDotPaint.getColor() != color) {
589 mDotPaint.setColor(color);
590
591 if (mDotAppearAmount != 0) {
592 invalidate();
593 }
594 }
Selim Cinek49014f82016-11-04 14:55:30 -0700595 }
596
Selim Cinek875ba9b2017-02-13 16:20:17 -0800597 /**
598 * Set the static color that should be used for the drawable of this icon if it's not
599 * transitioning this also immediately sets the color.
600 */
601 public void setStaticDrawableColor(int color) {
602 mDrawableColor = color;
603 setColorInternal(color);
Lucas Dupin83519da2017-06-21 11:58:31 -0700604 updateContrastedStaticColor();
Selim Cinek875ba9b2017-02-13 16:20:17 -0800605 mIconColor = color;
Adrian Roos456e0052017-04-04 16:44:29 -0700606 mDozer.setColor(color);
Selim Cinek875ba9b2017-02-13 16:20:17 -0800607 }
608
609 private void setColorInternal(int color) {
Adrian Roosf3eacd32017-04-14 16:35:58 -0700610 mCurrentSetColor = color;
611 updateIconColor();
612 }
613
614 private void updateIconColor() {
615 if (mCurrentSetColor != NO_COLOR) {
Adrian Roosc3e8cfc2017-07-17 17:09:39 +0200616 if (mMatrixColorFilter == null) {
617 mMatrix = new float[4 * 5];
618 mMatrixColorFilter = new ColorMatrixColorFilter(mMatrix);
619 }
620 int color = NotificationUtils.interpolateColors(
621 mCurrentSetColor, Color.WHITE, mDarkAmount);
622 updateTintMatrix(mMatrix, color, DARK_ALPHA_BOOST * mDarkAmount);
623 mMatrixColorFilter.setColorMatrixArray(mMatrix);
Selim Cinek1280a722019-01-29 13:08:23 -0800624 setColorFilter(null); // setColorFilter only invalidates if the instance changed.
Adrian Roosc3e8cfc2017-07-17 17:09:39 +0200625 setColorFilter(mMatrixColorFilter);
Selim Cinek875ba9b2017-02-13 16:20:17 -0800626 } else {
Adrian Roosf3eacd32017-04-14 16:35:58 -0700627 mDozer.updateGrayscale(this, mDarkAmount);
Selim Cinek875ba9b2017-02-13 16:20:17 -0800628 }
Selim Cinek875ba9b2017-02-13 16:20:17 -0800629 }
630
Adrian Roosc3e8cfc2017-07-17 17:09:39 +0200631 /**
632 * Updates {@param array} such that it represents a matrix that changes RGB to {@param color}
Adrian Roos24dbff32017-08-02 16:57:11 +0200633 * and multiplies the alpha channel with the color's alpha+{@param alphaBoost}.
Adrian Roosc3e8cfc2017-07-17 17:09:39 +0200634 */
635 private static void updateTintMatrix(float[] array, int color, float alphaBoost) {
636 Arrays.fill(array, 0);
637 array[4] = Color.red(color);
638 array[9] = Color.green(color);
639 array[14] = Color.blue(color);
Adrian Roos24dbff32017-08-02 16:57:11 +0200640 array[18] = Color.alpha(color) / 255f + alphaBoost;
Adrian Roosc3e8cfc2017-07-17 17:09:39 +0200641 }
642
Selim Cinek875ba9b2017-02-13 16:20:17 -0800643 public void setIconColor(int iconColor, boolean animate) {
644 if (mIconColor != iconColor) {
645 mIconColor = iconColor;
646 if (mColorAnimator != null) {
647 mColorAnimator.cancel();
648 }
649 if (mCurrentSetColor == iconColor) {
650 return;
651 }
652 if (animate && mCurrentSetColor != NO_COLOR) {
653 mAnimationStartColor = mCurrentSetColor;
654 mColorAnimator = ValueAnimator.ofFloat(0.0f, 1.0f);
655 mColorAnimator.setInterpolator(Interpolators.FAST_OUT_SLOW_IN);
656 mColorAnimator.setDuration(ANIMATION_DURATION_FAST);
657 mColorAnimator.addUpdateListener(mColorUpdater);
658 mColorAnimator.addListener(new AnimatorListenerAdapter() {
659 @Override
660 public void onAnimationEnd(Animator animation) {
661 mColorAnimator = null;
662 mAnimationStartColor = NO_COLOR;
663 }
664 });
665 mColorAnimator.start();
666 } else {
667 setColorInternal(iconColor);
668 }
669 }
670 }
671
672 public int getStaticDrawableColor() {
673 return mDrawableColor;
674 }
675
Lucas Dupin83519da2017-06-21 11:58:31 -0700676 /**
677 * A drawable color that passes GAR on a specific background.
678 * This value is cached.
679 *
680 * @param backgroundColor Background to test against.
681 * @return GAR safe version of {@link StatusBarIconView#getStaticDrawableColor()}.
682 */
683 int getContrastedStaticDrawableColor(int backgroundColor) {
684 if (mCachedContrastBackgroundColor != backgroundColor) {
685 mCachedContrastBackgroundColor = backgroundColor;
686 updateContrastedStaticColor();
687 }
688 return mContrastedDrawableColor;
689 }
690
691 private void updateContrastedStaticColor() {
Lucas Dupinddbaf842017-06-23 09:30:49 -0700692 if (Color.alpha(mCachedContrastBackgroundColor) != 255) {
693 mContrastedDrawableColor = mDrawableColor;
Lucas Dupin83519da2017-06-21 11:58:31 -0700694 return;
695 }
696 // We'll modify the color if it doesn't pass GAR
697 int contrastedColor = mDrawableColor;
Lucas Dupina291d192018-06-07 13:59:42 -0700698 if (!ContrastColorUtil.satisfiesTextContrast(mCachedContrastBackgroundColor,
Lucas Dupin83519da2017-06-21 11:58:31 -0700699 contrastedColor)) {
700 float[] hsl = new float[3];
701 ColorUtils.colorToHSL(mDrawableColor, hsl);
702 // This is basically a light grey, pushing the color will only distort it.
703 // Best thing to do in here is to fallback to the default color.
704 if (hsl[1] < 0.2f) {
705 contrastedColor = Notification.COLOR_DEFAULT;
706 }
Lucas Dupinf03e7522018-06-25 16:21:13 -0700707 boolean isDark = !ContrastColorUtil.isColorLight(mCachedContrastBackgroundColor);
Lucas Dupina291d192018-06-07 13:59:42 -0700708 contrastedColor = ContrastColorUtil.resolveContrastColor(mContext,
Lucas Dupinf03e7522018-06-25 16:21:13 -0700709 contrastedColor, mCachedContrastBackgroundColor, isDark);
Lucas Dupin83519da2017-06-21 11:58:31 -0700710 }
711 mContrastedDrawableColor = contrastedColor;
712 }
713
Evan Laird20b87bf2018-04-12 09:54:11 -0400714 @Override
Selim Cinek5b5beb012016-11-08 18:11:58 -0800715 public void setVisibleState(int state) {
716 setVisibleState(state, true /* animate */, null /* endRunnable */);
717 }
718
719 public void setVisibleState(int state, boolean animate) {
720 setVisibleState(state, animate, null);
721 }
722
723 @Override
724 public boolean hasOverlappingRendering() {
725 return false;
726 }
727
728 public void setVisibleState(int visibleState, boolean animate, Runnable endRunnable) {
Selim Cinekd03518c2018-03-15 12:13:51 -0700729 setVisibleState(visibleState, animate, endRunnable, 0);
730 }
731
732 /**
733 * Set the visibleState of this view.
734 *
735 * @param visibleState The new state.
736 * @param animate Should we animate?
737 * @param endRunnable The runnable to run at the end.
738 * @param duration The duration of an animation or 0 if the default should be taken.
739 */
740 public void setVisibleState(int visibleState, boolean animate, Runnable endRunnable,
741 long duration) {
Selim Cinek65d418e2016-11-29 15:42:34 -0800742 boolean runnableAdded = false;
Selim Cinek49014f82016-11-04 14:55:30 -0700743 if (visibleState != mVisibleState) {
744 mVisibleState = visibleState;
Selim Cinek5ece5a72017-02-15 11:47:42 -0800745 if (mIconAppearAnimator != null) {
746 mIconAppearAnimator.cancel();
747 }
748 if (mDotAnimator != null) {
749 mDotAnimator.cancel();
750 }
Selim Cinek5b5beb012016-11-08 18:11:58 -0800751 if (animate) {
Selim Cinek5b5beb012016-11-08 18:11:58 -0800752 float targetAmount = 0.0f;
753 Interpolator interpolator = Interpolators.FAST_OUT_LINEAR_IN;
754 if (visibleState == STATE_ICON) {
755 targetAmount = 1.0f;
756 interpolator = Interpolators.LINEAR_OUT_SLOW_IN;
757 }
Selim Cinek65d418e2016-11-29 15:42:34 -0800758 float currentAmount = getIconAppearAmount();
759 if (targetAmount != currentAmount) {
760 mIconAppearAnimator = ObjectAnimator.ofFloat(this, ICON_APPEAR_AMOUNT,
761 currentAmount, targetAmount);
762 mIconAppearAnimator.setInterpolator(interpolator);
Selim Cinekd03518c2018-03-15 12:13:51 -0700763 mIconAppearAnimator.setDuration(duration == 0 ? ANIMATION_DURATION_FAST
764 : duration);
Selim Cinek65d418e2016-11-29 15:42:34 -0800765 mIconAppearAnimator.addListener(new AnimatorListenerAdapter() {
766 @Override
767 public void onAnimationEnd(Animator animation) {
768 mIconAppearAnimator = null;
769 runRunnable(endRunnable);
Selim Cinek5b5beb012016-11-08 18:11:58 -0800770 }
Selim Cinek65d418e2016-11-29 15:42:34 -0800771 });
772 mIconAppearAnimator.start();
773 runnableAdded = true;
774 }
Selim Cinek49014f82016-11-04 14:55:30 -0700775
Selim Cinek5b5beb012016-11-08 18:11:58 -0800776 targetAmount = visibleState == STATE_ICON ? 2.0f : 0.0f;
777 interpolator = Interpolators.FAST_OUT_LINEAR_IN;
778 if (visibleState == STATE_DOT) {
779 targetAmount = 1.0f;
780 interpolator = Interpolators.LINEAR_OUT_SLOW_IN;
781 }
Selim Cinek65d418e2016-11-29 15:42:34 -0800782 currentAmount = getDotAppearAmount();
783 if (targetAmount != currentAmount) {
784 mDotAnimator = ObjectAnimator.ofFloat(this, DOT_APPEAR_AMOUNT,
785 currentAmount, targetAmount);
Selim Cinekd03518c2018-03-15 12:13:51 -0700786 mDotAnimator.setInterpolator(interpolator);;
787 mDotAnimator.setDuration(duration == 0 ? ANIMATION_DURATION_FAST
788 : duration);
Selim Cinek65d418e2016-11-29 15:42:34 -0800789 final boolean runRunnable = !runnableAdded;
790 mDotAnimator.addListener(new AnimatorListenerAdapter() {
791 @Override
792 public void onAnimationEnd(Animator animation) {
793 mDotAnimator = null;
794 if (runRunnable) {
795 runRunnable(endRunnable);
796 }
797 }
798 });
799 mDotAnimator.start();
800 runnableAdded = true;
801 }
Selim Cinek5b5beb012016-11-08 18:11:58 -0800802 } else {
803 setIconAppearAmount(visibleState == STATE_ICON ? 1.0f : 0.0f);
Selim Cinek01a73f92016-12-06 16:13:42 -0800804 setDotAppearAmount(visibleState == STATE_DOT ? 1.0f
805 : visibleState == STATE_ICON ? 2.0f
806 : 0.0f);
Selim Cinek5b5beb012016-11-08 18:11:58 -0800807 }
Selim Cinek49014f82016-11-04 14:55:30 -0700808 }
Selim Cinek65d418e2016-11-29 15:42:34 -0800809 if (!runnableAdded) {
810 runRunnable(endRunnable);
811 }
812 }
813
814 private void runRunnable(Runnable runnable) {
815 if (runnable != null) {
816 runnable.run();
817 }
Selim Cinek49014f82016-11-04 14:55:30 -0700818 }
819
Selim Cinek5b5beb012016-11-08 18:11:58 -0800820 public void setIconAppearAmount(float iconAppearAmount) {
Selim Cinek9ef119c2017-03-01 15:13:36 -0800821 if (mIconAppearAmount != iconAppearAmount) {
822 mIconAppearAmount = iconAppearAmount;
823 invalidate();
824 }
Selim Cinek49014f82016-11-04 14:55:30 -0700825 }
826
827 public float getIconAppearAmount() {
828 return mIconAppearAmount;
829 }
830
831 public int getVisibleState() {
832 return mVisibleState;
833 }
834
835 public void setDotAppearAmount(float dotAppearAmount) {
Selim Cinek9ef119c2017-03-01 15:13:36 -0800836 if (mDotAppearAmount != dotAppearAmount) {
837 mDotAppearAmount = dotAppearAmount;
838 invalidate();
839 }
Selim Cinek49014f82016-11-04 14:55:30 -0700840 }
841
Selim Cinek2b549f42016-11-22 16:38:51 -0800842 @Override
843 public void setVisibility(int visibility) {
844 super.setVisibility(visibility);
845 if (mOnVisibilityChangedListener != null) {
846 mOnVisibilityChangedListener.onVisibilityChanged(visibility);
847 }
848 }
849
Selim Cinek49014f82016-11-04 14:55:30 -0700850 public float getDotAppearAmount() {
851 return mDotAppearAmount;
852 }
Selim Cinek2b549f42016-11-22 16:38:51 -0800853
854 public void setOnVisibilityChangedListener(OnVisibilityChangedListener listener) {
855 mOnVisibilityChangedListener = listener;
856 }
857
Adrian Roos456e0052017-04-04 16:44:29 -0700858 public void setDark(boolean dark, boolean fade, long delay) {
Adrian Roos456e0052017-04-04 16:44:29 -0700859 mDozer.setIntensityDark(f -> {
860 mDarkAmount = f;
Fabian Kozynski61e1ba62019-05-15 10:04:39 -0400861 maybeUpdateIconScaleDimens();
Adrian Roos456e0052017-04-04 16:44:29 -0700862 updateDecorColor();
Adrian Roosf3eacd32017-04-14 16:35:58 -0700863 updateIconColor();
Adrian Roosfe0071f2017-07-06 18:45:10 +0200864 updateAllowAnimation();
Selim Cinek653c1392018-05-15 13:02:42 -0700865 }, dark, fade, delay, this);
Adrian Roos456e0052017-04-04 16:44:29 -0700866 }
867
Adrian Roosfe0071f2017-07-06 18:45:10 +0200868 private void updateAllowAnimation() {
869 if (mDarkAmount == 0 || mDarkAmount == 1) {
870 setAllowAnimation(mDarkAmount == 0);
871 }
872 }
873
Selim Cinek13859052017-10-05 17:12:18 -0700874 /**
875 * This method returns the drawing rect for the view which is different from the regular
876 * drawing rect, since we layout all children at position 0 and usually the translation is
877 * neglected. The standard implementation doesn't account for translation.
878 *
879 * @param outRect The (scrolled) drawing bounds of the view.
880 */
881 @Override
882 public void getDrawingRect(Rect outRect) {
883 super.getDrawingRect(outRect);
884 float translationX = getTranslationX();
885 float translationY = getTranslationY();
886 outRect.left += translationX;
887 outRect.right += translationX;
888 outRect.top += translationY;
889 outRect.bottom += translationY;
890 }
891
Selim Cinekd95ca7c2017-07-26 12:20:38 -0700892 public void setIsInShelf(boolean isInShelf) {
893 mIsInShelf = isInShelf;
894 }
895
896 public boolean isInShelf() {
897 return mIsInShelf;
898 }
899
Selim Cinek887da3c2017-10-06 13:37:32 -0700900 @Override
901 protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
902 super.onLayout(changed, left, top, right, bottom);
903 if (mLayoutRunnable != null) {
904 mLayoutRunnable.run();
905 mLayoutRunnable = null;
906 }
Selim Cinek3ddda6d2018-06-08 00:26:48 -0700907 updatePivot();
908 }
909
910 private void updatePivot() {
911 setPivotX((1 - mIconScale) / 2.0f * getWidth());
912 setPivotY((getHeight() - mIconScale * getWidth()) / 2.0f);
Selim Cinek887da3c2017-10-06 13:37:32 -0700913 }
914
915 public void executeOnLayout(Runnable runnable) {
916 mLayoutRunnable = runnable;
917 }
918
Selim Cinekd03518c2018-03-15 12:13:51 -0700919 public void setDismissed() {
920 mDismissed = true;
921 if (mOnDismissListener != null) {
922 mOnDismissListener.run();
923 }
924 }
925
926 public boolean isDismissed() {
927 return mDismissed;
928 }
929
930 public void setOnDismissListener(Runnable onDismissListener) {
931 mOnDismissListener = onDismissListener;
932 }
933
Evan Lairde1d13c92018-03-20 16:58:01 -0400934 @Override
935 public void onDarkChanged(Rect area, float darkIntensity, int tint) {
Evan Laird20b87bf2018-04-12 09:54:11 -0400936 int areaTint = getTint(area, this, tint);
937 ColorStateList color = ColorStateList.valueOf(areaTint);
938 setImageTintList(color);
939 setDecorColor(areaTint);
Evan Lairde1d13c92018-03-20 16:58:01 -0400940 }
941
942 @Override
943 public boolean isIconVisible() {
944 return mIcon != null && mIcon.visible;
945 }
946
947 @Override
948 public boolean isIconBlocked() {
949 return mBlocked;
950 }
951
Selim Cinek2b549f42016-11-22 16:38:51 -0800952 public interface OnVisibilityChangedListener {
953 void onVisibilityChanged(int newVisibility);
954 }
Joe Onorato0cbda992010-05-02 16:28:15 -0700955}