blob: 19ed13e7a95ef7f119ebabb425e0ce00c833cb4e [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
74 * 17dp * (15 / 17) = 15dp, the new height.
75 */
76 private static final float SYSTEM_ICON_SCALE = 15.f / 17.f;
Selim Cinek875ba9b2017-02-13 16:20:17 -080077 private final int ANIMATION_DURATION_FAST = 100;
78
Selim Cinek49014f82016-11-04 14:55:30 -070079 public static final int STATE_ICON = 0;
80 public static final int STATE_DOT = 1;
81 public static final int STATE_HIDDEN = 2;
Joe Onorato0cbda992010-05-02 16:28:15 -070082
Selim Cinek49014f82016-11-04 14:55:30 -070083 private static final String TAG = "StatusBarIconView";
84 private static final Property<StatusBarIconView, Float> ICON_APPEAR_AMOUNT
85 = new FloatProperty<StatusBarIconView>("iconAppearAmount") {
86
87 @Override
88 public void setValue(StatusBarIconView object, float value) {
89 object.setIconAppearAmount(value);
90 }
91
92 @Override
93 public Float get(StatusBarIconView object) {
94 return object.getIconAppearAmount();
95 }
96 };
Selim Cinek5b5beb012016-11-08 18:11:58 -080097 private static final Property<StatusBarIconView, Float> DOT_APPEAR_AMOUNT
Selim Cinek49014f82016-11-04 14:55:30 -070098 = new FloatProperty<StatusBarIconView>("dot_appear_amount") {
99
100 @Override
101 public void setValue(StatusBarIconView object, float value) {
102 object.setDotAppearAmount(value);
103 }
104
105 @Override
106 public Float get(StatusBarIconView object) {
107 return object.getDotAppearAmount();
108 }
109 };
110
111 private boolean mAlwaysScaleIcon;
Adrian Roos138f0342017-04-28 09:11:28 -0700112 private int mStatusBarIconDrawingSizeDark = 1;
113 private int mStatusBarIconDrawingSize = 1;
114 private int mStatusBarIconSize = 1;
Joe Onorato0cbda992010-05-02 16:28:15 -0700115 private StatusBarIcon mIcon;
116 @ViewDebug.ExportedProperty private String mSlot;
Joe Onorato6c01a112010-10-04 17:38:47 -0400117 private Drawable mNumberBackground;
118 private Paint mNumberPain;
119 private int mNumberX;
120 private int mNumberY;
121 private String mNumberText;
Selim Cinek72fc8db2017-06-06 18:07:47 -0700122 private StatusBarNotification mNotification;
Jason Monk3b230072015-05-29 11:11:29 -0400123 private final boolean mBlocked;
Selim Cinek3e7592d2016-04-11 09:35:54 +0800124 private int mDensity;
Lucas Dupinde07d212018-10-23 17:47:03 -0700125 private boolean mNightMode;
Selim Cinek281c2022016-10-13 19:14:43 -0700126 private float mIconScale = 1.0f;
Evan Lairdbaccb462018-06-01 16:29:02 -0400127 private final Paint mDotPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
Selim Cinek49014f82016-11-04 14:55:30 -0700128 private float mDotRadius;
129 private int mStaticDotRadius;
130 private int mVisibleState = STATE_ICON;
131 private float mIconAppearAmount = 1.0f;
132 private ObjectAnimator mIconAppearAnimator;
133 private ObjectAnimator mDotAnimator;
134 private float mDotAppearAmount;
Selim Cinek2b549f42016-11-22 16:38:51 -0800135 private OnVisibilityChangedListener mOnVisibilityChangedListener;
Selim Cinek875ba9b2017-02-13 16:20:17 -0800136 private int mDrawableColor;
137 private int mIconColor;
Adrian Roos456e0052017-04-04 16:44:29 -0700138 private int mDecorColor;
139 private float mDarkAmount;
Selim Cinek875ba9b2017-02-13 16:20:17 -0800140 private ValueAnimator mColorAnimator;
141 private int mCurrentSetColor = NO_COLOR;
142 private int mAnimationStartColor = NO_COLOR;
143 private final ValueAnimator.AnimatorUpdateListener mColorUpdater
144 = animation -> {
145 int newColor = NotificationUtils.interpolateColors(mAnimationStartColor, mIconColor,
146 animation.getAnimatedFraction());
147 setColorInternal(newColor);
148 };
Adrian Roos456e0052017-04-04 16:44:29 -0700149 private final NotificationIconDozeHelper mDozer;
Lucas Dupin83519da2017-06-21 11:58:31 -0700150 private int mContrastedDrawableColor;
151 private int mCachedContrastBackgroundColor = NO_COLOR;
Adrian Roosc3e8cfc2017-07-17 17:09:39 +0200152 private float[] mMatrix;
153 private ColorMatrixColorFilter mMatrixColorFilter;
Selim Cinekd95ca7c2017-07-26 12:20:38 -0700154 private boolean mIsInShelf;
Selim Cinek887da3c2017-10-06 13:37:32 -0700155 private Runnable mLayoutRunnable;
Selim Cinekd03518c2018-03-15 12:13:51 -0700156 private boolean mDismissed;
157 private Runnable mOnDismissListener;
Joe Onorato0cbda992010-05-02 16:28:15 -0700158
Selim Cinek72fc8db2017-06-06 18:07:47 -0700159 public StatusBarIconView(Context context, String slot, StatusBarNotification sbn) {
160 this(context, slot, sbn, false);
Jason Monk3b230072015-05-29 11:11:29 -0400161 }
162
Selim Cinek72fc8db2017-06-06 18:07:47 -0700163 public StatusBarIconView(Context context, String slot, StatusBarNotification sbn,
Jason Monk3b230072015-05-29 11:11:29 -0400164 boolean blocked) {
Joe Onorato0cbda992010-05-02 16:28:15 -0700165 super(context);
Adrian Roos456e0052017-04-04 16:44:29 -0700166 mDozer = new NotificationIconDozeHelper(context);
Jason Monk3b230072015-05-29 11:11:29 -0400167 mBlocked = blocked;
Joe Onorato0cbda992010-05-02 16:28:15 -0700168 mSlot = slot;
Joe Onorato6c01a112010-10-04 17:38:47 -0400169 mNumberPain = new Paint();
170 mNumberPain.setTextAlign(Paint.Align.CENTER);
Alan Viverette4a357cd2015-03-18 18:37:18 -0700171 mNumberPain.setColor(context.getColor(R.drawable.notification_number_text_color));
Joe Onorato6c01a112010-10-04 17:38:47 -0400172 mNumberPain.setAntiAlias(true);
Selim Cinek72fc8db2017-06-06 18:07:47 -0700173 setNotification(sbn);
Selim Cinek3e7592d2016-04-11 09:35:54 +0800174 setScaleType(ScaleType.CENTER);
175 mDensity = context.getResources().getDisplayMetrics().densityDpi;
Lucas Dupinde07d212018-10-23 17:47:03 -0700176 Configuration configuration = context.getResources().getConfiguration();
177 mNightMode = (configuration.uiMode & Configuration.UI_MODE_NIGHT_MASK)
178 == Configuration.UI_MODE_NIGHT_YES;
179 initializeDecorColor();
Selim Cinek49014f82016-11-04 14:55:30 -0700180 reloadDimens();
Evan Laird20b87bf2018-04-12 09:54:11 -0400181 maybeUpdateIconScaleDimens();
Selim Cinek3e7592d2016-04-11 09:35:54 +0800182 }
Daniel Sandler26c84b12011-07-27 00:09:40 -0400183
Evan Laird20b87bf2018-04-12 09:54:11 -0400184 /** Should always be preceded by {@link #reloadDimens()} */
Adrian Roos138f0342017-04-28 09:11:28 -0700185 private void maybeUpdateIconScaleDimens() {
Daniel Sandler7579bca2011-08-18 15:47:26 -0400186 // We do not resize and scale system icons (on the right), only notification icons (on the
187 // left).
Selim Cinek3e7592d2016-04-11 09:35:54 +0800188 if (mNotification != null || mAlwaysScaleIcon) {
Evan Laird39ea8102018-05-18 19:49:07 -0400189 updateIconScaleForNotifications();
190 } else {
191 updateIconScaleForSystemIcons();
Daniel Sandler7579bca2011-08-18 15:47:26 -0400192 }
Selim Cinek3e7592d2016-04-11 09:35:54 +0800193 }
Daniel Sandlerabff0322011-09-27 11:19:34 -0400194
Evan Laird39ea8102018-05-18 19:49:07 -0400195 private void updateIconScaleForNotifications() {
Adrian Roos138f0342017-04-28 09:11:28 -0700196 final float imageBounds = NotificationUtils.interpolate(
197 mStatusBarIconDrawingSize,
198 mStatusBarIconDrawingSizeDark,
199 mDarkAmount);
200 final int outerBounds = mStatusBarIconSize;
Selim Cinek281c2022016-10-13 19:14:43 -0700201 mIconScale = (float)imageBounds / (float)outerBounds;
Selim Cinek3ddda6d2018-06-08 00:26:48 -0700202 updatePivot();
Selim Cinek281c2022016-10-13 19:14:43 -0700203 }
204
Evan Laird39ea8102018-05-18 19:49:07 -0400205 private void updateIconScaleForSystemIcons() {
206 mIconScale = SYSTEM_ICON_SCALE;
207 }
208
Lucas Dupin7fc9dc12019-01-03 09:19:43 -0800209 public float getIconScaleFullyDark() {
210 return (float) mStatusBarIconDrawingSizeDark / mStatusBarIconDrawingSize;
211 }
212
Selim Cinek281c2022016-10-13 19:14:43 -0700213 public float getIconScale() {
214 return mIconScale;
Selim Cinek3e7592d2016-04-11 09:35:54 +0800215 }
216
217 @Override
218 protected void onConfigurationChanged(Configuration newConfig) {
219 super.onConfigurationChanged(newConfig);
220 int density = newConfig.densityDpi;
221 if (density != mDensity) {
222 mDensity = density;
Evan Laird20b87bf2018-04-12 09:54:11 -0400223 reloadDimens();
Adrian Roos138f0342017-04-28 09:11:28 -0700224 maybeUpdateIconScaleDimens();
Selim Cinek3e7592d2016-04-11 09:35:54 +0800225 updateDrawable();
Selim Cinek49014f82016-11-04 14:55:30 -0700226 }
Lucas Dupinde07d212018-10-23 17:47:03 -0700227 boolean nightMode = (newConfig.uiMode & Configuration.UI_MODE_NIGHT_MASK)
228 == Configuration.UI_MODE_NIGHT_YES;
229 if (nightMode != mNightMode) {
230 mNightMode = nightMode;
231 initializeDecorColor();
232 }
Selim Cinek49014f82016-11-04 14:55:30 -0700233 }
234
235 private void reloadDimens() {
236 boolean applyRadius = mDotRadius == mStaticDotRadius;
Evan Laird20b87bf2018-04-12 09:54:11 -0400237 Resources res = getResources();
238 mStaticDotRadius = res.getDimensionPixelSize(R.dimen.overflow_dot_radius);
239 mStatusBarIconSize = res.getDimensionPixelSize(R.dimen.status_bar_icon_size);
240 mStatusBarIconDrawingSizeDark =
241 res.getDimensionPixelSize(R.dimen.status_bar_icon_drawing_size_dark);
242 mStatusBarIconDrawingSize =
243 res.getDimensionPixelSize(R.dimen.status_bar_icon_drawing_size);
Selim Cinek49014f82016-11-04 14:55:30 -0700244 if (applyRadius) {
245 mDotRadius = mStaticDotRadius;
Selim Cinek3e7592d2016-04-11 09:35:54 +0800246 }
Joe Onorato0cbda992010-05-02 16:28:15 -0700247 }
248
Selim Cinek72fc8db2017-06-06 18:07:47 -0700249 public void setNotification(StatusBarNotification notification) {
Christoph Studera0506e72014-07-31 20:27:39 +0200250 mNotification = notification;
Selim Cinek72fc8db2017-06-06 18:07:47 -0700251 if (notification != null) {
252 setContentDescription(notification.getNotification());
253 }
Christoph Studera0506e72014-07-31 20:27:39 +0200254 }
255
Daniel Sandler05e24142011-11-10 11:56:49 -0500256 public StatusBarIconView(Context context, AttributeSet attrs) {
257 super(context, attrs);
Adrian Roos456e0052017-04-04 16:44:29 -0700258 mDozer = new NotificationIconDozeHelper(context);
Jason Monk3b230072015-05-29 11:11:29 -0400259 mBlocked = false;
Selim Cinek3e7592d2016-04-11 09:35:54 +0800260 mAlwaysScaleIcon = true;
Evan Laird20b87bf2018-04-12 09:54:11 -0400261 reloadDimens();
Evan Laird39ea8102018-05-18 19:49:07 -0400262 updateIconScaleForNotifications();
Selim Cinek3e7592d2016-04-11 09:35:54 +0800263 mDensity = context.getResources().getDisplayMetrics().densityDpi;
Daniel Sandler05e24142011-11-10 11:56:49 -0500264 }
265
Joe Onorato0cbda992010-05-02 16:28:15 -0700266 private static boolean streq(String a, String b) {
Joe Onorato66d7d012010-05-14 10:05:10 -0700267 if (a == b) {
268 return true;
269 }
Joe Onorato0cbda992010-05-02 16:28:15 -0700270 if (a == null && b != null) {
271 return false;
272 }
273 if (a != null && b == null) {
274 return false;
275 }
276 return a.equals(b);
277 }
278
Dan Sandlerd63f9322015-05-06 15:18:49 -0400279 public boolean equalIcons(Icon a, Icon b) {
280 if (a == b) return true;
281 if (a.getType() != b.getType()) return false;
282 switch (a.getType()) {
283 case Icon.TYPE_RESOURCE:
284 return a.getResPackage().equals(b.getResPackage()) && a.getResId() == b.getResId();
285 case Icon.TYPE_URI:
286 return a.getUriString().equals(b.getUriString());
287 default:
288 return false;
289 }
290 }
Joe Onorato005847b2010-06-04 16:08:02 -0400291 /**
292 * Returns whether the set succeeded.
293 */
294 public boolean set(StatusBarIcon icon) {
Dan Sandlerd63f9322015-05-06 15:18:49 -0400295 final boolean iconEquals = mIcon != null && equalIcons(mIcon.icon, icon.icon);
Joe Onorato005847b2010-06-04 16:08:02 -0400296 final boolean levelEquals = iconEquals
297 && mIcon.iconLevel == icon.iconLevel;
298 final boolean visibilityEquals = mIcon != null
299 && mIcon.visible == icon.visible;
Joe Onorato6c01a112010-10-04 17:38:47 -0400300 final boolean numberEquals = mIcon != null
301 && mIcon.number == icon.number;
302 mIcon = icon.clone();
Svetoslav Ganov6179ea32011-06-28 01:12:41 -0700303 setContentDescription(icon.contentDescription);
Joe Onorato005847b2010-06-04 16:08:02 -0400304 if (!iconEquals) {
Fabrice Di Meglio82fca5d2013-01-09 15:42:31 -0800305 if (!updateDrawable(false /* no clear */)) return false;
Selim Cinek2b6eb8032016-12-29 14:22:21 +0100306 // we have to clear the grayscale tag since it may have changed
307 setTag(R.id.icon_is_grayscale, null);
Joe Onorato0cbda992010-05-02 16:28:15 -0700308 }
Joe Onorato005847b2010-06-04 16:08:02 -0400309 if (!levelEquals) {
310 setImageLevel(icon.iconLevel);
Joe Onorato0cbda992010-05-02 16:28:15 -0700311 }
Daniel Sandler26c84b12011-07-27 00:09:40 -0400312
Joe Onorato6c01a112010-10-04 17:38:47 -0400313 if (!numberEquals) {
John Spurlock01534782014-01-13 11:59:22 -0500314 if (icon.number > 0 && getContext().getResources().getBoolean(
Joe Onorato8595a3d2010-11-19 18:12:07 -0800315 R.bool.config_statusBarShowNumber)) {
Joe Onorato6c01a112010-10-04 17:38:47 -0400316 if (mNumberBackground == null) {
317 mNumberBackground = getContext().getResources().getDrawable(
318 R.drawable.ic_notification_overlay);
319 }
320 placeNumber();
321 } else {
322 mNumberBackground = null;
323 mNumberText = null;
324 }
325 invalidate();
326 }
Joe Onorato005847b2010-06-04 16:08:02 -0400327 if (!visibilityEquals) {
Jason Monk3b230072015-05-29 11:11:29 -0400328 setVisibility(icon.visible && !mBlocked ? VISIBLE : GONE);
Joe Onorato005847b2010-06-04 16:08:02 -0400329 }
Joe Onorato005847b2010-06-04 16:08:02 -0400330 return true;
Joe Onorato0cbda992010-05-02 16:28:15 -0700331 }
332
Fabrice Di Meglio82fca5d2013-01-09 15:42:31 -0800333 public void updateDrawable() {
334 updateDrawable(true /* with clear */);
335 }
336
337 private boolean updateDrawable(boolean withClear) {
Jorim Jaggi66ac1332015-01-21 19:22:26 +0100338 if (mIcon == null) {
339 return false;
340 }
Adrian Roosfb2d0cc2017-01-31 15:10:00 -0800341 Drawable drawable;
342 try {
343 drawable = getIcon(mIcon);
344 } catch (OutOfMemoryError e) {
345 Log.w(TAG, "OOM while inflating " + mIcon.icon + " for slot " + mSlot);
346 return false;
347 }
348
Fabrice Di Meglio82fca5d2013-01-09 15:42:31 -0800349 if (drawable == null) {
Adrian Roosfb2d0cc2017-01-31 15:10:00 -0800350 Log.w(TAG, "No icon for slot " + mSlot + "; " + mIcon.icon);
Fabrice Di Meglio82fca5d2013-01-09 15:42:31 -0800351 return false;
352 }
353 if (withClear) {
354 setImageDrawable(null);
355 }
356 setImageDrawable(drawable);
357 return true;
358 }
359
Selim Cinek72fc8db2017-06-06 18:07:47 -0700360 public Icon getSourceIcon() {
361 return mIcon.icon;
362 }
363
Joe Onoratof5510542010-06-01 07:46:41 -0700364 private Drawable getIcon(StatusBarIcon icon) {
365 return getIcon(getContext(), icon);
366 }
367
Joe Onorato0cbda992010-05-02 16:28:15 -0700368 /**
Dan Sandlerd63f9322015-05-06 15:18:49 -0400369 * Returns the right icon to use for this item
John Spurlock209bede2013-07-17 12:23:27 -0400370 *
Dan Sandlerd63f9322015-05-06 15:18:49 -0400371 * @param context Context to use to get resources
Joe Onorato0cbda992010-05-02 16:28:15 -0700372 * @return Drawable for this item, or null if the package or item could not
373 * be found
374 */
Anthony Chen55e8e1e2016-01-08 10:31:46 -0800375 public static Drawable getIcon(Context context, StatusBarIcon statusBarIcon) {
376 int userId = statusBarIcon.user.getIdentifier();
Dan Sandlerd63f9322015-05-06 15:18:49 -0400377 if (userId == UserHandle.USER_ALL) {
Xiaohui Chen87d0e252015-07-30 15:38:16 -0700378 userId = UserHandle.USER_SYSTEM;
Joe Onorato0cbda992010-05-02 16:28:15 -0700379 }
Anthony Chen55e8e1e2016-01-08 10:31:46 -0800380
381 Drawable icon = statusBarIcon.icon.loadDrawableAsUser(context, userId);
382
383 TypedValue typedValue = new TypedValue();
384 context.getResources().getValue(R.dimen.status_bar_icon_scale_factor, typedValue, true);
385 float scaleFactor = typedValue.getFloat();
386
387 // No need to scale the icon, so return it as is.
388 if (scaleFactor == 1.f) {
389 return icon;
390 }
391
392 return new ScalingDrawableWrapper(icon, scaleFactor);
Joe Onorato0cbda992010-05-02 16:28:15 -0700393 }
Joe Onoratob77f53b2010-05-28 19:59:51 -0400394
395 public StatusBarIcon getStatusBarIcon() {
396 return mIcon;
397 }
Joe Onoratof9ec03c2010-09-23 15:09:18 -0700398
Svetoslav Ganov6179ea32011-06-28 01:12:41 -0700399 @Override
400 public void onInitializeAccessibilityEvent(AccessibilityEvent event) {
401 super.onInitializeAccessibilityEvent(event);
402 if (mNotification != null) {
Selim Cinek72fc8db2017-06-06 18:07:47 -0700403 event.setParcelableData(mNotification.getNotification());
Svetoslav Ganov6179ea32011-06-28 01:12:41 -0700404 }
405 }
406
407 @Override
Joe Onorato6c01a112010-10-04 17:38:47 -0400408 protected void onSizeChanged(int w, int h, int oldw, int oldh) {
409 super.onSizeChanged(w, h, oldw, oldh);
410 if (mNumberBackground != null) {
411 placeNumber();
412 }
413 }
414
Svetoslav Ganov6179ea32011-06-28 01:12:41 -0700415 @Override
Jorim Jaggi66ac1332015-01-21 19:22:26 +0100416 public void onRtlPropertiesChanged(int layoutDirection) {
417 super.onRtlPropertiesChanged(layoutDirection);
418 updateDrawable();
419 }
420
421 @Override
Joe Onorato6c01a112010-10-04 17:38:47 -0400422 protected void onDraw(Canvas canvas) {
Selim Cinek49014f82016-11-04 14:55:30 -0700423 if (mIconAppearAmount > 0.0f) {
424 canvas.save();
425 canvas.scale(mIconScale * mIconAppearAmount, mIconScale * mIconAppearAmount,
426 getWidth() / 2, getHeight() / 2);
427 super.onDraw(canvas);
428 canvas.restore();
429 }
Joe Onorato6c01a112010-10-04 17:38:47 -0400430
431 if (mNumberBackground != null) {
432 mNumberBackground.draw(canvas);
433 canvas.drawText(mNumberText, mNumberX, mNumberY, mNumberPain);
434 }
Selim Cinek49014f82016-11-04 14:55:30 -0700435 if (mDotAppearAmount != 0.0f) {
436 float radius;
Evan Lairdbaccb462018-06-01 16:29:02 -0400437 float alpha = Color.alpha(mDecorColor) / 255.f;
Selim Cinek49014f82016-11-04 14:55:30 -0700438 if (mDotAppearAmount <= 1.0f) {
439 radius = mDotRadius * mDotAppearAmount;
Selim Cinek49014f82016-11-04 14:55:30 -0700440 } else {
441 float fadeOutAmount = mDotAppearAmount - 1.0f;
Evan Lairdbaccb462018-06-01 16:29:02 -0400442 alpha = alpha * (1.0f - fadeOutAmount);
Selim Cinek49014f82016-11-04 14:55:30 -0700443 radius = NotificationUtils.interpolate(mDotRadius, getWidth() / 4, fadeOutAmount);
444 }
445 mDotPaint.setAlpha((int) (alpha * 255));
Evan Laird20b87bf2018-04-12 09:54:11 -0400446 canvas.drawCircle(mStatusBarIconSize / 2, getHeight() / 2, radius, mDotPaint);
Selim Cinek49014f82016-11-04 14:55:30 -0700447 }
Joe Onorato6c01a112010-10-04 17:38:47 -0400448 }
449
Svetoslav Ganov6179ea32011-06-28 01:12:41 -0700450 @Override
Joe Onoratof9ec03c2010-09-23 15:09:18 -0700451 protected void debug(int depth) {
452 super.debug(depth);
453 Log.d("View", debugIndent(depth) + "slot=" + mSlot);
454 Log.d("View", debugIndent(depth) + "icon=" + mIcon);
455 }
Joe Onorato6c01a112010-10-04 17:38:47 -0400456
457 void placeNumber() {
Daniel Sandlerebce0112011-06-16 16:44:51 -0400458 final String str;
John Spurlock01534782014-01-13 11:59:22 -0500459 final int tooBig = getContext().getResources().getInteger(
Daniel Sandlerebce0112011-06-16 16:44:51 -0400460 android.R.integer.status_bar_notification_info_maxnum);
461 if (mIcon.number > tooBig) {
John Spurlock01534782014-01-13 11:59:22 -0500462 str = getContext().getResources().getString(
Daniel Sandlerebce0112011-06-16 16:44:51 -0400463 android.R.string.status_bar_notification_info_overflow);
464 } else {
465 NumberFormat f = NumberFormat.getIntegerInstance();
466 str = f.format(mIcon.number);
467 }
468 mNumberText = str;
469
Joe Onorato6c01a112010-10-04 17:38:47 -0400470 final int w = getWidth();
471 final int h = getHeight();
472 final Rect r = new Rect();
473 mNumberPain.getTextBounds(str, 0, str.length(), r);
474 final int tw = r.right - r.left;
475 final int th = r.bottom - r.top;
476 mNumberBackground.getPadding(r);
477 int dw = r.left + tw + r.right;
478 if (dw < mNumberBackground.getMinimumWidth()) {
479 dw = mNumberBackground.getMinimumWidth();
480 }
481 mNumberX = w-r.right-((dw-r.right-r.left)/2);
482 int dh = r.top + th + r.bottom;
483 if (dh < mNumberBackground.getMinimumWidth()) {
484 dh = mNumberBackground.getMinimumWidth();
485 }
486 mNumberY = h-r.bottom-((dh-r.top-th-r.bottom)/2);
487 mNumberBackground.setBounds(w-dw, h-dh, w, h);
488 }
Svetoslav Ganov6179ea32011-06-28 01:12:41 -0700489
490 private void setContentDescription(Notification notification) {
491 if (notification != null) {
Adrian Rooseba05822016-04-22 17:09:27 -0700492 String d = contentDescForNotification(mContext, notification);
493 if (!TextUtils.isEmpty(d)) {
494 setContentDescription(d);
Svetoslav Ganov6179ea32011-06-28 01:12:41 -0700495 }
496 }
497 }
Daniel Sandler7579bca2011-08-18 15:47:26 -0400498
499 public String toString() {
John Spurlock209bede2013-07-17 12:23:27 -0400500 return "StatusBarIconView(slot=" + mSlot + " icon=" + mIcon
Daniel Sandler7579bca2011-08-18 15:47:26 -0400501 + " notification=" + mNotification + ")";
502 }
Jason Monk3b230072015-05-29 11:11:29 -0400503
Selim Cinek72fc8db2017-06-06 18:07:47 -0700504 public StatusBarNotification getNotification() {
505 return mNotification;
506 }
507
Jason Monk3b230072015-05-29 11:11:29 -0400508 public String getSlot() {
509 return mSlot;
510 }
Adrian Rooseba05822016-04-22 17:09:27 -0700511
512
513 public static String contentDescForNotification(Context c, Notification n) {
Adrian Roosa8e18ef2016-05-26 17:17:02 -0700514 String appName = "";
515 try {
516 Notification.Builder builder = Notification.Builder.recoverBuilder(c, n);
517 appName = builder.loadHeaderAppName();
518 } catch (RuntimeException e) {
519 Log.e(TAG, "Unable to recover builder", e);
520 // Trying to get the app name from the app info instead.
521 Parcelable appInfo = n.extras.getParcelable(
522 Notification.EXTRA_BUILDER_APPLICATION_INFO);
523 if (appInfo instanceof ApplicationInfo) {
524 appName = String.valueOf(((ApplicationInfo) appInfo).loadLabel(
525 c.getPackageManager()));
526 }
527 }
Adrian Roos51548e62016-04-28 11:20:26 -0700528
Julia Reynolds7f9ce782016-05-17 15:34:34 -0400529 CharSequence title = n.extras.getCharSequence(Notification.EXTRA_TITLE);
Adrian Roosf76600e2017-04-19 15:32:43 -0700530 CharSequence text = n.extras.getCharSequence(Notification.EXTRA_TEXT);
Adrian Rooseba05822016-04-22 17:09:27 -0700531 CharSequence ticker = n.tickerText;
Adrian Roos51548e62016-04-28 11:20:26 -0700532
Adrian Roosf76600e2017-04-19 15:32:43 -0700533 // Some apps just put the app name into the title
534 CharSequence titleOrText = TextUtils.equals(title, appName) ? text : title;
535
536 CharSequence desc = !TextUtils.isEmpty(titleOrText) ? titleOrText
Adrian Roos6e782a52017-03-30 18:34:10 -0700537 : !TextUtils.isEmpty(ticker) ? ticker : "";
Adrian Roos51548e62016-04-28 11:20:26 -0700538
539 return c.getString(R.string.accessibility_desc_notification_icon, appName, desc);
Adrian Rooseba05822016-04-22 17:09:27 -0700540 }
Adrian Roos51548e62016-04-28 11:20:26 -0700541
Selim Cinek875ba9b2017-02-13 16:20:17 -0800542 /**
543 * Set the color that is used to draw decoration like the overflow dot. This will not be applied
544 * to the drawable.
545 */
546 public void setDecorColor(int iconTint) {
Adrian Roos456e0052017-04-04 16:44:29 -0700547 mDecorColor = iconTint;
548 updateDecorColor();
549 }
550
Lucas Dupinde07d212018-10-23 17:47:03 -0700551 private void initializeDecorColor() {
552 if (mNotification != null) {
553 setDecorColor(getContext().getColor(mNightMode
554 ? com.android.internal.R.color.notification_default_color_dark
555 : com.android.internal.R.color.notification_default_color_light));
556 }
557 }
558
Adrian Roos456e0052017-04-04 16:44:29 -0700559 private void updateDecorColor() {
560 int color = NotificationUtils.interpolateColors(mDecorColor, Color.WHITE, mDarkAmount);
561 if (mDotPaint.getColor() != color) {
562 mDotPaint.setColor(color);
563
564 if (mDotAppearAmount != 0) {
565 invalidate();
566 }
567 }
Selim Cinek49014f82016-11-04 14:55:30 -0700568 }
569
Selim Cinek875ba9b2017-02-13 16:20:17 -0800570 /**
571 * Set the static color that should be used for the drawable of this icon if it's not
572 * transitioning this also immediately sets the color.
573 */
574 public void setStaticDrawableColor(int color) {
575 mDrawableColor = color;
576 setColorInternal(color);
Lucas Dupin83519da2017-06-21 11:58:31 -0700577 updateContrastedStaticColor();
Selim Cinek875ba9b2017-02-13 16:20:17 -0800578 mIconColor = color;
Adrian Roos456e0052017-04-04 16:44:29 -0700579 mDozer.setColor(color);
Selim Cinek875ba9b2017-02-13 16:20:17 -0800580 }
581
582 private void setColorInternal(int color) {
Adrian Roosf3eacd32017-04-14 16:35:58 -0700583 mCurrentSetColor = color;
584 updateIconColor();
585 }
586
587 private void updateIconColor() {
588 if (mCurrentSetColor != NO_COLOR) {
Adrian Roosc3e8cfc2017-07-17 17:09:39 +0200589 if (mMatrixColorFilter == null) {
590 mMatrix = new float[4 * 5];
591 mMatrixColorFilter = new ColorMatrixColorFilter(mMatrix);
592 }
593 int color = NotificationUtils.interpolateColors(
594 mCurrentSetColor, Color.WHITE, mDarkAmount);
595 updateTintMatrix(mMatrix, color, DARK_ALPHA_BOOST * mDarkAmount);
596 mMatrixColorFilter.setColorMatrixArray(mMatrix);
597 setColorFilter(mMatrixColorFilter);
598 invalidate(); // setColorFilter only invalidates if the filter instance changed.
Selim Cinek875ba9b2017-02-13 16:20:17 -0800599 } else {
Adrian Roosf3eacd32017-04-14 16:35:58 -0700600 mDozer.updateGrayscale(this, mDarkAmount);
Selim Cinek875ba9b2017-02-13 16:20:17 -0800601 }
Selim Cinek875ba9b2017-02-13 16:20:17 -0800602 }
603
Adrian Roosc3e8cfc2017-07-17 17:09:39 +0200604 /**
605 * Updates {@param array} such that it represents a matrix that changes RGB to {@param color}
Adrian Roos24dbff32017-08-02 16:57:11 +0200606 * and multiplies the alpha channel with the color's alpha+{@param alphaBoost}.
Adrian Roosc3e8cfc2017-07-17 17:09:39 +0200607 */
608 private static void updateTintMatrix(float[] array, int color, float alphaBoost) {
609 Arrays.fill(array, 0);
610 array[4] = Color.red(color);
611 array[9] = Color.green(color);
612 array[14] = Color.blue(color);
Adrian Roos24dbff32017-08-02 16:57:11 +0200613 array[18] = Color.alpha(color) / 255f + alphaBoost;
Adrian Roosc3e8cfc2017-07-17 17:09:39 +0200614 }
615
Selim Cinek875ba9b2017-02-13 16:20:17 -0800616 public void setIconColor(int iconColor, boolean animate) {
617 if (mIconColor != iconColor) {
618 mIconColor = iconColor;
619 if (mColorAnimator != null) {
620 mColorAnimator.cancel();
621 }
622 if (mCurrentSetColor == iconColor) {
623 return;
624 }
625 if (animate && mCurrentSetColor != NO_COLOR) {
626 mAnimationStartColor = mCurrentSetColor;
627 mColorAnimator = ValueAnimator.ofFloat(0.0f, 1.0f);
628 mColorAnimator.setInterpolator(Interpolators.FAST_OUT_SLOW_IN);
629 mColorAnimator.setDuration(ANIMATION_DURATION_FAST);
630 mColorAnimator.addUpdateListener(mColorUpdater);
631 mColorAnimator.addListener(new AnimatorListenerAdapter() {
632 @Override
633 public void onAnimationEnd(Animator animation) {
634 mColorAnimator = null;
635 mAnimationStartColor = NO_COLOR;
636 }
637 });
638 mColorAnimator.start();
639 } else {
640 setColorInternal(iconColor);
641 }
642 }
643 }
644
645 public int getStaticDrawableColor() {
646 return mDrawableColor;
647 }
648
Lucas Dupin83519da2017-06-21 11:58:31 -0700649 /**
650 * A drawable color that passes GAR on a specific background.
651 * This value is cached.
652 *
653 * @param backgroundColor Background to test against.
654 * @return GAR safe version of {@link StatusBarIconView#getStaticDrawableColor()}.
655 */
656 int getContrastedStaticDrawableColor(int backgroundColor) {
657 if (mCachedContrastBackgroundColor != backgroundColor) {
658 mCachedContrastBackgroundColor = backgroundColor;
659 updateContrastedStaticColor();
660 }
661 return mContrastedDrawableColor;
662 }
663
664 private void updateContrastedStaticColor() {
Lucas Dupinddbaf842017-06-23 09:30:49 -0700665 if (Color.alpha(mCachedContrastBackgroundColor) != 255) {
666 mContrastedDrawableColor = mDrawableColor;
Lucas Dupin83519da2017-06-21 11:58:31 -0700667 return;
668 }
669 // We'll modify the color if it doesn't pass GAR
670 int contrastedColor = mDrawableColor;
Lucas Dupina291d192018-06-07 13:59:42 -0700671 if (!ContrastColorUtil.satisfiesTextContrast(mCachedContrastBackgroundColor,
Lucas Dupin83519da2017-06-21 11:58:31 -0700672 contrastedColor)) {
673 float[] hsl = new float[3];
674 ColorUtils.colorToHSL(mDrawableColor, hsl);
675 // This is basically a light grey, pushing the color will only distort it.
676 // Best thing to do in here is to fallback to the default color.
677 if (hsl[1] < 0.2f) {
678 contrastedColor = Notification.COLOR_DEFAULT;
679 }
Lucas Dupinf03e7522018-06-25 16:21:13 -0700680 boolean isDark = !ContrastColorUtil.isColorLight(mCachedContrastBackgroundColor);
Lucas Dupina291d192018-06-07 13:59:42 -0700681 contrastedColor = ContrastColorUtil.resolveContrastColor(mContext,
Lucas Dupinf03e7522018-06-25 16:21:13 -0700682 contrastedColor, mCachedContrastBackgroundColor, isDark);
Lucas Dupin83519da2017-06-21 11:58:31 -0700683 }
684 mContrastedDrawableColor = contrastedColor;
685 }
686
Evan Laird20b87bf2018-04-12 09:54:11 -0400687 @Override
Selim Cinek5b5beb012016-11-08 18:11:58 -0800688 public void setVisibleState(int state) {
689 setVisibleState(state, true /* animate */, null /* endRunnable */);
690 }
691
692 public void setVisibleState(int state, boolean animate) {
693 setVisibleState(state, animate, null);
694 }
695
696 @Override
697 public boolean hasOverlappingRendering() {
698 return false;
699 }
700
701 public void setVisibleState(int visibleState, boolean animate, Runnable endRunnable) {
Selim Cinekd03518c2018-03-15 12:13:51 -0700702 setVisibleState(visibleState, animate, endRunnable, 0);
703 }
704
705 /**
706 * Set the visibleState of this view.
707 *
708 * @param visibleState The new state.
709 * @param animate Should we animate?
710 * @param endRunnable The runnable to run at the end.
711 * @param duration The duration of an animation or 0 if the default should be taken.
712 */
713 public void setVisibleState(int visibleState, boolean animate, Runnable endRunnable,
714 long duration) {
Selim Cinek65d418e2016-11-29 15:42:34 -0800715 boolean runnableAdded = false;
Selim Cinek49014f82016-11-04 14:55:30 -0700716 if (visibleState != mVisibleState) {
717 mVisibleState = visibleState;
Selim Cinek5ece5a72017-02-15 11:47:42 -0800718 if (mIconAppearAnimator != null) {
719 mIconAppearAnimator.cancel();
720 }
721 if (mDotAnimator != null) {
722 mDotAnimator.cancel();
723 }
Selim Cinek5b5beb012016-11-08 18:11:58 -0800724 if (animate) {
Selim Cinek5b5beb012016-11-08 18:11:58 -0800725 float targetAmount = 0.0f;
726 Interpolator interpolator = Interpolators.FAST_OUT_LINEAR_IN;
727 if (visibleState == STATE_ICON) {
728 targetAmount = 1.0f;
729 interpolator = Interpolators.LINEAR_OUT_SLOW_IN;
730 }
Selim Cinek65d418e2016-11-29 15:42:34 -0800731 float currentAmount = getIconAppearAmount();
732 if (targetAmount != currentAmount) {
733 mIconAppearAnimator = ObjectAnimator.ofFloat(this, ICON_APPEAR_AMOUNT,
734 currentAmount, targetAmount);
735 mIconAppearAnimator.setInterpolator(interpolator);
Selim Cinekd03518c2018-03-15 12:13:51 -0700736 mIconAppearAnimator.setDuration(duration == 0 ? ANIMATION_DURATION_FAST
737 : duration);
Selim Cinek65d418e2016-11-29 15:42:34 -0800738 mIconAppearAnimator.addListener(new AnimatorListenerAdapter() {
739 @Override
740 public void onAnimationEnd(Animator animation) {
741 mIconAppearAnimator = null;
742 runRunnable(endRunnable);
Selim Cinek5b5beb012016-11-08 18:11:58 -0800743 }
Selim Cinek65d418e2016-11-29 15:42:34 -0800744 });
745 mIconAppearAnimator.start();
746 runnableAdded = true;
747 }
Selim Cinek49014f82016-11-04 14:55:30 -0700748
Selim Cinek5b5beb012016-11-08 18:11:58 -0800749 targetAmount = visibleState == STATE_ICON ? 2.0f : 0.0f;
750 interpolator = Interpolators.FAST_OUT_LINEAR_IN;
751 if (visibleState == STATE_DOT) {
752 targetAmount = 1.0f;
753 interpolator = Interpolators.LINEAR_OUT_SLOW_IN;
754 }
Selim Cinek65d418e2016-11-29 15:42:34 -0800755 currentAmount = getDotAppearAmount();
756 if (targetAmount != currentAmount) {
757 mDotAnimator = ObjectAnimator.ofFloat(this, DOT_APPEAR_AMOUNT,
758 currentAmount, targetAmount);
Selim Cinekd03518c2018-03-15 12:13:51 -0700759 mDotAnimator.setInterpolator(interpolator);;
760 mDotAnimator.setDuration(duration == 0 ? ANIMATION_DURATION_FAST
761 : duration);
Selim Cinek65d418e2016-11-29 15:42:34 -0800762 final boolean runRunnable = !runnableAdded;
763 mDotAnimator.addListener(new AnimatorListenerAdapter() {
764 @Override
765 public void onAnimationEnd(Animator animation) {
766 mDotAnimator = null;
767 if (runRunnable) {
768 runRunnable(endRunnable);
769 }
770 }
771 });
772 mDotAnimator.start();
773 runnableAdded = true;
774 }
Selim Cinek5b5beb012016-11-08 18:11:58 -0800775 } else {
776 setIconAppearAmount(visibleState == STATE_ICON ? 1.0f : 0.0f);
Selim Cinek01a73f92016-12-06 16:13:42 -0800777 setDotAppearAmount(visibleState == STATE_DOT ? 1.0f
778 : visibleState == STATE_ICON ? 2.0f
779 : 0.0f);
Selim Cinek5b5beb012016-11-08 18:11:58 -0800780 }
Selim Cinek49014f82016-11-04 14:55:30 -0700781 }
Selim Cinek65d418e2016-11-29 15:42:34 -0800782 if (!runnableAdded) {
783 runRunnable(endRunnable);
784 }
785 }
786
787 private void runRunnable(Runnable runnable) {
788 if (runnable != null) {
789 runnable.run();
790 }
Selim Cinek49014f82016-11-04 14:55:30 -0700791 }
792
Selim Cinek5b5beb012016-11-08 18:11:58 -0800793 public void setIconAppearAmount(float iconAppearAmount) {
Selim Cinek9ef119c2017-03-01 15:13:36 -0800794 if (mIconAppearAmount != iconAppearAmount) {
795 mIconAppearAmount = iconAppearAmount;
796 invalidate();
797 }
Selim Cinek49014f82016-11-04 14:55:30 -0700798 }
799
800 public float getIconAppearAmount() {
801 return mIconAppearAmount;
802 }
803
804 public int getVisibleState() {
805 return mVisibleState;
806 }
807
808 public void setDotAppearAmount(float dotAppearAmount) {
Selim Cinek9ef119c2017-03-01 15:13:36 -0800809 if (mDotAppearAmount != dotAppearAmount) {
810 mDotAppearAmount = dotAppearAmount;
811 invalidate();
812 }
Selim Cinek49014f82016-11-04 14:55:30 -0700813 }
814
Selim Cinek2b549f42016-11-22 16:38:51 -0800815 @Override
816 public void setVisibility(int visibility) {
817 super.setVisibility(visibility);
818 if (mOnVisibilityChangedListener != null) {
819 mOnVisibilityChangedListener.onVisibilityChanged(visibility);
820 }
821 }
822
Selim Cinek49014f82016-11-04 14:55:30 -0700823 public float getDotAppearAmount() {
824 return mDotAppearAmount;
825 }
Selim Cinek2b549f42016-11-22 16:38:51 -0800826
827 public void setOnVisibilityChangedListener(OnVisibilityChangedListener listener) {
828 mOnVisibilityChangedListener = listener;
829 }
830
Adrian Roos456e0052017-04-04 16:44:29 -0700831 public void setDark(boolean dark, boolean fade, long delay) {
Adrian Roos456e0052017-04-04 16:44:29 -0700832 mDozer.setIntensityDark(f -> {
833 mDarkAmount = f;
Evan Laird39ea8102018-05-18 19:49:07 -0400834 updateIconScaleForNotifications();
Adrian Roos456e0052017-04-04 16:44:29 -0700835 updateDecorColor();
Adrian Roosf3eacd32017-04-14 16:35:58 -0700836 updateIconColor();
Adrian Roosfe0071f2017-07-06 18:45:10 +0200837 updateAllowAnimation();
Selim Cinek653c1392018-05-15 13:02:42 -0700838 }, dark, fade, delay, this);
Adrian Roos456e0052017-04-04 16:44:29 -0700839 }
840
Adrian Roosfe0071f2017-07-06 18:45:10 +0200841 private void updateAllowAnimation() {
842 if (mDarkAmount == 0 || mDarkAmount == 1) {
843 setAllowAnimation(mDarkAmount == 0);
844 }
845 }
846
Selim Cinek13859052017-10-05 17:12:18 -0700847 /**
848 * This method returns the drawing rect for the view which is different from the regular
849 * drawing rect, since we layout all children at position 0 and usually the translation is
850 * neglected. The standard implementation doesn't account for translation.
851 *
852 * @param outRect The (scrolled) drawing bounds of the view.
853 */
854 @Override
855 public void getDrawingRect(Rect outRect) {
856 super.getDrawingRect(outRect);
857 float translationX = getTranslationX();
858 float translationY = getTranslationY();
859 outRect.left += translationX;
860 outRect.right += translationX;
861 outRect.top += translationY;
862 outRect.bottom += translationY;
863 }
864
Selim Cinekd95ca7c2017-07-26 12:20:38 -0700865 public void setIsInShelf(boolean isInShelf) {
866 mIsInShelf = isInShelf;
867 }
868
869 public boolean isInShelf() {
870 return mIsInShelf;
871 }
872
Selim Cinek887da3c2017-10-06 13:37:32 -0700873 @Override
874 protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
875 super.onLayout(changed, left, top, right, bottom);
876 if (mLayoutRunnable != null) {
877 mLayoutRunnable.run();
878 mLayoutRunnable = null;
879 }
Selim Cinek3ddda6d2018-06-08 00:26:48 -0700880 updatePivot();
881 }
882
883 private void updatePivot() {
884 setPivotX((1 - mIconScale) / 2.0f * getWidth());
885 setPivotY((getHeight() - mIconScale * getWidth()) / 2.0f);
Selim Cinek887da3c2017-10-06 13:37:32 -0700886 }
887
888 public void executeOnLayout(Runnable runnable) {
889 mLayoutRunnable = runnable;
890 }
891
Selim Cinekd03518c2018-03-15 12:13:51 -0700892 public void setDismissed() {
893 mDismissed = true;
894 if (mOnDismissListener != null) {
895 mOnDismissListener.run();
896 }
897 }
898
899 public boolean isDismissed() {
900 return mDismissed;
901 }
902
903 public void setOnDismissListener(Runnable onDismissListener) {
904 mOnDismissListener = onDismissListener;
905 }
906
Evan Lairde1d13c92018-03-20 16:58:01 -0400907 @Override
908 public void onDarkChanged(Rect area, float darkIntensity, int tint) {
Evan Laird20b87bf2018-04-12 09:54:11 -0400909 int areaTint = getTint(area, this, tint);
910 ColorStateList color = ColorStateList.valueOf(areaTint);
911 setImageTintList(color);
912 setDecorColor(areaTint);
Evan Lairde1d13c92018-03-20 16:58:01 -0400913 }
914
915 @Override
916 public boolean isIconVisible() {
917 return mIcon != null && mIcon.visible;
918 }
919
920 @Override
921 public boolean isIconBlocked() {
922 return mBlocked;
923 }
924
Selim Cinek2b549f42016-11-22 16:38:51 -0800925 public interface OnVisibilityChangedListener {
926 void onVisibilityChanged(int newVisibility);
927 }
Joe Onorato0cbda992010-05-02 16:28:15 -0700928}