blob: ffc4e8d2f0187ba30553a673ab39458ac6deec01 [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 Cinek875ba9b2017-02-13 16:20:17 -080026import android.content.res.ColorStateList;
Selim Cinek3e7592d2016-04-11 09:35:54 +080027import android.content.res.Configuration;
Joe Onorato0cbda992010-05-02 16:28:15 -070028import android.content.res.Resources;
Joe Onorato0cbda992010-05-02 16:28:15 -070029import android.graphics.Canvas;
Adrian Roos456e0052017-04-04 16:44:29 -070030import android.graphics.Color;
Joe Onorato6c01a112010-10-04 17:38:47 -040031import android.graphics.Paint;
32import android.graphics.Rect;
John Spurlockde84f0e2013-06-12 12:41:00 -040033import android.graphics.drawable.Drawable;
Dan Sandlerd63f9322015-05-06 15:18:49 -040034import android.graphics.drawable.Icon;
Adrian Roosa8e18ef2016-05-26 17:17:02 -070035import android.os.Parcelable;
Jeff Sharkeyded653b2012-09-27 19:09:24 -070036import android.os.UserHandle;
Svetoslav Ganov6179ea32011-06-28 01:12:41 -070037import android.text.TextUtils;
Daniel Sandler05e24142011-11-10 11:56:49 -050038import android.util.AttributeSet;
Selim Cinek49014f82016-11-04 14:55:30 -070039import android.util.FloatProperty;
Joe Onoratof9ec03c2010-09-23 15:09:18 -070040import android.util.Log;
Selim Cinek49014f82016-11-04 14:55:30 -070041import android.util.Property;
Anthony Chen55e8e1e2016-01-08 10:31:46 -080042import android.util.TypedValue;
Joe Onorato0cbda992010-05-02 16:28:15 -070043import android.view.ViewDebug;
Svetoslav Ganov6179ea32011-06-28 01:12:41 -070044import android.view.accessibility.AccessibilityEvent;
Selim Cinek49014f82016-11-04 14:55:30 -070045import android.view.animation.Interpolator;
Winsonc0d70582016-01-29 10:24:39 -080046
Joe Onorato0cbda992010-05-02 16:28:15 -070047import com.android.internal.statusbar.StatusBarIcon;
Selim Cinek49014f82016-11-04 14:55:30 -070048import com.android.systemui.Interpolators;
Joe Onorato6c01a112010-10-04 17:38:47 -040049import com.android.systemui.R;
Adrian Roos456e0052017-04-04 16:44:29 -070050import com.android.systemui.statusbar.notification.NotificationIconDozeHelper;
Selim Cinek49014f82016-11-04 14:55:30 -070051import com.android.systemui.statusbar.notification.NotificationUtils;
Joe Onorato6c01a112010-10-04 17:38:47 -040052
John Spurlockde84f0e2013-06-12 12:41:00 -040053import java.text.NumberFormat;
54
Joe Onorato0cbda992010-05-02 16:28:15 -070055public class StatusBarIconView extends AnimatedImageView {
Selim Cinek875ba9b2017-02-13 16:20:17 -080056 public static final int NO_COLOR = 0;
57 private final int ANIMATION_DURATION_FAST = 100;
58
Selim Cinek49014f82016-11-04 14:55:30 -070059 public static final int STATE_ICON = 0;
60 public static final int STATE_DOT = 1;
61 public static final int STATE_HIDDEN = 2;
Joe Onorato0cbda992010-05-02 16:28:15 -070062
Selim Cinek49014f82016-11-04 14:55:30 -070063 private static final String TAG = "StatusBarIconView";
64 private static final Property<StatusBarIconView, Float> ICON_APPEAR_AMOUNT
65 = new FloatProperty<StatusBarIconView>("iconAppearAmount") {
66
67 @Override
68 public void setValue(StatusBarIconView object, float value) {
69 object.setIconAppearAmount(value);
70 }
71
72 @Override
73 public Float get(StatusBarIconView object) {
74 return object.getIconAppearAmount();
75 }
76 };
Selim Cinek5b5beb012016-11-08 18:11:58 -080077 private static final Property<StatusBarIconView, Float> DOT_APPEAR_AMOUNT
Selim Cinek49014f82016-11-04 14:55:30 -070078 = new FloatProperty<StatusBarIconView>("dot_appear_amount") {
79
80 @Override
81 public void setValue(StatusBarIconView object, float value) {
82 object.setDotAppearAmount(value);
83 }
84
85 @Override
86 public Float get(StatusBarIconView object) {
87 return object.getDotAppearAmount();
88 }
89 };
90
91 private boolean mAlwaysScaleIcon;
Joe Onorato0cbda992010-05-02 16:28:15 -070092 private StatusBarIcon mIcon;
93 @ViewDebug.ExportedProperty private String mSlot;
Joe Onorato6c01a112010-10-04 17:38:47 -040094 private Drawable mNumberBackground;
95 private Paint mNumberPain;
96 private int mNumberX;
97 private int mNumberY;
98 private String mNumberText;
Svetoslav Ganov6179ea32011-06-28 01:12:41 -070099 private Notification mNotification;
Jason Monk3b230072015-05-29 11:11:29 -0400100 private final boolean mBlocked;
Selim Cinek3e7592d2016-04-11 09:35:54 +0800101 private int mDensity;
Selim Cinek281c2022016-10-13 19:14:43 -0700102 private float mIconScale = 1.0f;
Selim Cinek49014f82016-11-04 14:55:30 -0700103 private final Paint mDotPaint = new Paint();
Selim Cinek49014f82016-11-04 14:55:30 -0700104 private float mDotRadius;
105 private int mStaticDotRadius;
106 private int mVisibleState = STATE_ICON;
107 private float mIconAppearAmount = 1.0f;
108 private ObjectAnimator mIconAppearAnimator;
109 private ObjectAnimator mDotAnimator;
110 private float mDotAppearAmount;
Selim Cinek2b549f42016-11-22 16:38:51 -0800111 private OnVisibilityChangedListener mOnVisibilityChangedListener;
Selim Cinek875ba9b2017-02-13 16:20:17 -0800112 private int mDrawableColor;
113 private int mIconColor;
Adrian Roos456e0052017-04-04 16:44:29 -0700114 private int mDecorColor;
115 private float mDarkAmount;
Selim Cinek875ba9b2017-02-13 16:20:17 -0800116 private ValueAnimator mColorAnimator;
117 private int mCurrentSetColor = NO_COLOR;
118 private int mAnimationStartColor = NO_COLOR;
119 private final ValueAnimator.AnimatorUpdateListener mColorUpdater
120 = animation -> {
121 int newColor = NotificationUtils.interpolateColors(mAnimationStartColor, mIconColor,
122 animation.getAnimatedFraction());
123 setColorInternal(newColor);
124 };
Adrian Roos456e0052017-04-04 16:44:29 -0700125 private final NotificationIconDozeHelper mDozer;
Joe Onorato0cbda992010-05-02 16:28:15 -0700126
John Spurlocke6f0a712013-09-03 16:23:49 -0400127 public StatusBarIconView(Context context, String slot, Notification notification) {
Jason Monk3b230072015-05-29 11:11:29 -0400128 this(context, slot, notification, false);
129 }
130
131 public StatusBarIconView(Context context, String slot, Notification notification,
132 boolean blocked) {
Joe Onorato0cbda992010-05-02 16:28:15 -0700133 super(context);
Adrian Roos456e0052017-04-04 16:44:29 -0700134 mDozer = new NotificationIconDozeHelper(context);
Jason Monk3b230072015-05-29 11:11:29 -0400135 mBlocked = blocked;
Joe Onorato0cbda992010-05-02 16:28:15 -0700136 mSlot = slot;
Joe Onorato6c01a112010-10-04 17:38:47 -0400137 mNumberPain = new Paint();
138 mNumberPain.setTextAlign(Paint.Align.CENTER);
Alan Viverette4a357cd2015-03-18 18:37:18 -0700139 mNumberPain.setColor(context.getColor(R.drawable.notification_number_text_color));
Joe Onorato6c01a112010-10-04 17:38:47 -0400140 mNumberPain.setAntiAlias(true);
Christoph Studera0506e72014-07-31 20:27:39 +0200141 setNotification(notification);
Selim Cinek3e7592d2016-04-11 09:35:54 +0800142 maybeUpdateIconScale();
143 setScaleType(ScaleType.CENTER);
144 mDensity = context.getResources().getDisplayMetrics().densityDpi;
Selim Cinek49014f82016-11-04 14:55:30 -0700145 if (mNotification != null) {
Selim Cinek875ba9b2017-02-13 16:20:17 -0800146 setDecorColor(getContext().getColor(
Selim Cinek49014f82016-11-04 14:55:30 -0700147 com.android.internal.R.color.notification_icon_default_color));
148 }
149 reloadDimens();
Selim Cinek3e7592d2016-04-11 09:35:54 +0800150 }
Daniel Sandler26c84b12011-07-27 00:09:40 -0400151
Selim Cinek3e7592d2016-04-11 09:35:54 +0800152 private void maybeUpdateIconScale() {
Daniel Sandler7579bca2011-08-18 15:47:26 -0400153 // We do not resize and scale system icons (on the right), only notification icons (on the
154 // left).
Selim Cinek3e7592d2016-04-11 09:35:54 +0800155 if (mNotification != null || mAlwaysScaleIcon) {
156 updateIconScale();
Daniel Sandler7579bca2011-08-18 15:47:26 -0400157 }
Selim Cinek3e7592d2016-04-11 09:35:54 +0800158 }
Daniel Sandlerabff0322011-09-27 11:19:34 -0400159
Selim Cinek3e7592d2016-04-11 09:35:54 +0800160 private void updateIconScale() {
161 Resources res = mContext.getResources();
162 final int outerBounds = res.getDimensionPixelSize(R.dimen.status_bar_icon_size);
163 final int imageBounds = res.getDimensionPixelSize(R.dimen.status_bar_icon_drawing_size);
Selim Cinek281c2022016-10-13 19:14:43 -0700164 mIconScale = (float)imageBounds / (float)outerBounds;
Selim Cinek281c2022016-10-13 19:14:43 -0700165 }
166
167 public float getIconScale() {
168 return mIconScale;
Selim Cinek3e7592d2016-04-11 09:35:54 +0800169 }
170
171 @Override
172 protected void onConfigurationChanged(Configuration newConfig) {
173 super.onConfigurationChanged(newConfig);
174 int density = newConfig.densityDpi;
175 if (density != mDensity) {
176 mDensity = density;
177 maybeUpdateIconScale();
178 updateDrawable();
Selim Cinek49014f82016-11-04 14:55:30 -0700179 reloadDimens();
180 }
181 }
182
183 private void reloadDimens() {
184 boolean applyRadius = mDotRadius == mStaticDotRadius;
185 mStaticDotRadius = getResources().getDimensionPixelSize(R.dimen.overflow_dot_radius);
186 if (applyRadius) {
187 mDotRadius = mStaticDotRadius;
Selim Cinek3e7592d2016-04-11 09:35:54 +0800188 }
Joe Onorato0cbda992010-05-02 16:28:15 -0700189 }
190
Christoph Studera0506e72014-07-31 20:27:39 +0200191 public void setNotification(Notification notification) {
192 mNotification = notification;
193 setContentDescription(notification);
194 }
195
Daniel Sandler05e24142011-11-10 11:56:49 -0500196 public StatusBarIconView(Context context, AttributeSet attrs) {
197 super(context, attrs);
Adrian Roos456e0052017-04-04 16:44:29 -0700198 mDozer = new NotificationIconDozeHelper(context);
Jason Monk3b230072015-05-29 11:11:29 -0400199 mBlocked = false;
Selim Cinek3e7592d2016-04-11 09:35:54 +0800200 mAlwaysScaleIcon = true;
201 updateIconScale();
202 mDensity = context.getResources().getDisplayMetrics().densityDpi;
Daniel Sandler05e24142011-11-10 11:56:49 -0500203 }
204
Joe Onorato0cbda992010-05-02 16:28:15 -0700205 private static boolean streq(String a, String b) {
Joe Onorato66d7d012010-05-14 10:05:10 -0700206 if (a == b) {
207 return true;
208 }
Joe Onorato0cbda992010-05-02 16:28:15 -0700209 if (a == null && b != null) {
210 return false;
211 }
212 if (a != null && b == null) {
213 return false;
214 }
215 return a.equals(b);
216 }
217
Dan Sandlerd63f9322015-05-06 15:18:49 -0400218 public boolean equalIcons(Icon a, Icon b) {
219 if (a == b) return true;
220 if (a.getType() != b.getType()) return false;
221 switch (a.getType()) {
222 case Icon.TYPE_RESOURCE:
223 return a.getResPackage().equals(b.getResPackage()) && a.getResId() == b.getResId();
224 case Icon.TYPE_URI:
225 return a.getUriString().equals(b.getUriString());
226 default:
227 return false;
228 }
229 }
Joe Onorato005847b2010-06-04 16:08:02 -0400230 /**
231 * Returns whether the set succeeded.
232 */
233 public boolean set(StatusBarIcon icon) {
Dan Sandlerd63f9322015-05-06 15:18:49 -0400234 final boolean iconEquals = mIcon != null && equalIcons(mIcon.icon, icon.icon);
Joe Onorato005847b2010-06-04 16:08:02 -0400235 final boolean levelEquals = iconEquals
236 && mIcon.iconLevel == icon.iconLevel;
237 final boolean visibilityEquals = mIcon != null
238 && mIcon.visible == icon.visible;
Joe Onorato6c01a112010-10-04 17:38:47 -0400239 final boolean numberEquals = mIcon != null
240 && mIcon.number == icon.number;
241 mIcon = icon.clone();
Svetoslav Ganov6179ea32011-06-28 01:12:41 -0700242 setContentDescription(icon.contentDescription);
Joe Onorato005847b2010-06-04 16:08:02 -0400243 if (!iconEquals) {
Fabrice Di Meglio82fca5d2013-01-09 15:42:31 -0800244 if (!updateDrawable(false /* no clear */)) return false;
Selim Cinek2b6eb8032016-12-29 14:22:21 +0100245 // we have to clear the grayscale tag since it may have changed
246 setTag(R.id.icon_is_grayscale, null);
Joe Onorato0cbda992010-05-02 16:28:15 -0700247 }
Joe Onorato005847b2010-06-04 16:08:02 -0400248 if (!levelEquals) {
249 setImageLevel(icon.iconLevel);
Joe Onorato0cbda992010-05-02 16:28:15 -0700250 }
Daniel Sandler26c84b12011-07-27 00:09:40 -0400251
Joe Onorato6c01a112010-10-04 17:38:47 -0400252 if (!numberEquals) {
John Spurlock01534782014-01-13 11:59:22 -0500253 if (icon.number > 0 && getContext().getResources().getBoolean(
Joe Onorato8595a3d2010-11-19 18:12:07 -0800254 R.bool.config_statusBarShowNumber)) {
Joe Onorato6c01a112010-10-04 17:38:47 -0400255 if (mNumberBackground == null) {
256 mNumberBackground = getContext().getResources().getDrawable(
257 R.drawable.ic_notification_overlay);
258 }
259 placeNumber();
260 } else {
261 mNumberBackground = null;
262 mNumberText = null;
263 }
264 invalidate();
265 }
Joe Onorato005847b2010-06-04 16:08:02 -0400266 if (!visibilityEquals) {
Jason Monk3b230072015-05-29 11:11:29 -0400267 setVisibility(icon.visible && !mBlocked ? VISIBLE : GONE);
Joe Onorato005847b2010-06-04 16:08:02 -0400268 }
Joe Onorato005847b2010-06-04 16:08:02 -0400269 return true;
Joe Onorato0cbda992010-05-02 16:28:15 -0700270 }
271
Fabrice Di Meglio82fca5d2013-01-09 15:42:31 -0800272 public void updateDrawable() {
273 updateDrawable(true /* with clear */);
274 }
275
276 private boolean updateDrawable(boolean withClear) {
Jorim Jaggi66ac1332015-01-21 19:22:26 +0100277 if (mIcon == null) {
278 return false;
279 }
Adrian Roosfb2d0cc2017-01-31 15:10:00 -0800280 Drawable drawable;
281 try {
282 drawable = getIcon(mIcon);
283 } catch (OutOfMemoryError e) {
284 Log.w(TAG, "OOM while inflating " + mIcon.icon + " for slot " + mSlot);
285 return false;
286 }
287
Fabrice Di Meglio82fca5d2013-01-09 15:42:31 -0800288 if (drawable == null) {
Adrian Roosfb2d0cc2017-01-31 15:10:00 -0800289 Log.w(TAG, "No icon for slot " + mSlot + "; " + mIcon.icon);
Fabrice Di Meglio82fca5d2013-01-09 15:42:31 -0800290 return false;
291 }
292 if (withClear) {
293 setImageDrawable(null);
294 }
295 setImageDrawable(drawable);
296 return true;
297 }
298
Joe Onoratof5510542010-06-01 07:46:41 -0700299 private Drawable getIcon(StatusBarIcon icon) {
300 return getIcon(getContext(), icon);
301 }
302
Joe Onorato0cbda992010-05-02 16:28:15 -0700303 /**
Dan Sandlerd63f9322015-05-06 15:18:49 -0400304 * Returns the right icon to use for this item
John Spurlock209bede2013-07-17 12:23:27 -0400305 *
Dan Sandlerd63f9322015-05-06 15:18:49 -0400306 * @param context Context to use to get resources
Joe Onorato0cbda992010-05-02 16:28:15 -0700307 * @return Drawable for this item, or null if the package or item could not
308 * be found
309 */
Anthony Chen55e8e1e2016-01-08 10:31:46 -0800310 public static Drawable getIcon(Context context, StatusBarIcon statusBarIcon) {
311 int userId = statusBarIcon.user.getIdentifier();
Dan Sandlerd63f9322015-05-06 15:18:49 -0400312 if (userId == UserHandle.USER_ALL) {
Xiaohui Chen87d0e252015-07-30 15:38:16 -0700313 userId = UserHandle.USER_SYSTEM;
Joe Onorato0cbda992010-05-02 16:28:15 -0700314 }
Anthony Chen55e8e1e2016-01-08 10:31:46 -0800315
316 Drawable icon = statusBarIcon.icon.loadDrawableAsUser(context, userId);
317
318 TypedValue typedValue = new TypedValue();
319 context.getResources().getValue(R.dimen.status_bar_icon_scale_factor, typedValue, true);
320 float scaleFactor = typedValue.getFloat();
321
322 // No need to scale the icon, so return it as is.
323 if (scaleFactor == 1.f) {
324 return icon;
325 }
326
327 return new ScalingDrawableWrapper(icon, scaleFactor);
Joe Onorato0cbda992010-05-02 16:28:15 -0700328 }
Joe Onoratob77f53b2010-05-28 19:59:51 -0400329
330 public StatusBarIcon getStatusBarIcon() {
331 return mIcon;
332 }
Joe Onoratof9ec03c2010-09-23 15:09:18 -0700333
Svetoslav Ganov6179ea32011-06-28 01:12:41 -0700334 @Override
335 public void onInitializeAccessibilityEvent(AccessibilityEvent event) {
336 super.onInitializeAccessibilityEvent(event);
337 if (mNotification != null) {
338 event.setParcelableData(mNotification);
339 }
340 }
341
342 @Override
Joe Onorato6c01a112010-10-04 17:38:47 -0400343 protected void onSizeChanged(int w, int h, int oldw, int oldh) {
344 super.onSizeChanged(w, h, oldw, oldh);
345 if (mNumberBackground != null) {
346 placeNumber();
347 }
348 }
349
Svetoslav Ganov6179ea32011-06-28 01:12:41 -0700350 @Override
Jorim Jaggi66ac1332015-01-21 19:22:26 +0100351 public void onRtlPropertiesChanged(int layoutDirection) {
352 super.onRtlPropertiesChanged(layoutDirection);
353 updateDrawable();
354 }
355
356 @Override
Joe Onorato6c01a112010-10-04 17:38:47 -0400357 protected void onDraw(Canvas canvas) {
Selim Cinek49014f82016-11-04 14:55:30 -0700358 if (mIconAppearAmount > 0.0f) {
359 canvas.save();
360 canvas.scale(mIconScale * mIconAppearAmount, mIconScale * mIconAppearAmount,
361 getWidth() / 2, getHeight() / 2);
362 super.onDraw(canvas);
363 canvas.restore();
364 }
Joe Onorato6c01a112010-10-04 17:38:47 -0400365
366 if (mNumberBackground != null) {
367 mNumberBackground.draw(canvas);
368 canvas.drawText(mNumberText, mNumberX, mNumberY, mNumberPain);
369 }
Selim Cinek49014f82016-11-04 14:55:30 -0700370 if (mDotAppearAmount != 0.0f) {
371 float radius;
372 float alpha;
373 if (mDotAppearAmount <= 1.0f) {
374 radius = mDotRadius * mDotAppearAmount;
375 alpha = 1.0f;
376 } else {
377 float fadeOutAmount = mDotAppearAmount - 1.0f;
378 alpha = 1.0f - fadeOutAmount;
379 radius = NotificationUtils.interpolate(mDotRadius, getWidth() / 4, fadeOutAmount);
380 }
381 mDotPaint.setAlpha((int) (alpha * 255));
382 canvas.drawCircle(getWidth() / 2, getHeight() / 2, radius, mDotPaint);
383 }
Joe Onorato6c01a112010-10-04 17:38:47 -0400384 }
385
Svetoslav Ganov6179ea32011-06-28 01:12:41 -0700386 @Override
Joe Onoratof9ec03c2010-09-23 15:09:18 -0700387 protected void debug(int depth) {
388 super.debug(depth);
389 Log.d("View", debugIndent(depth) + "slot=" + mSlot);
390 Log.d("View", debugIndent(depth) + "icon=" + mIcon);
391 }
Joe Onorato6c01a112010-10-04 17:38:47 -0400392
393 void placeNumber() {
Daniel Sandlerebce0112011-06-16 16:44:51 -0400394 final String str;
John Spurlock01534782014-01-13 11:59:22 -0500395 final int tooBig = getContext().getResources().getInteger(
Daniel Sandlerebce0112011-06-16 16:44:51 -0400396 android.R.integer.status_bar_notification_info_maxnum);
397 if (mIcon.number > tooBig) {
John Spurlock01534782014-01-13 11:59:22 -0500398 str = getContext().getResources().getString(
Daniel Sandlerebce0112011-06-16 16:44:51 -0400399 android.R.string.status_bar_notification_info_overflow);
400 } else {
401 NumberFormat f = NumberFormat.getIntegerInstance();
402 str = f.format(mIcon.number);
403 }
404 mNumberText = str;
405
Joe Onorato6c01a112010-10-04 17:38:47 -0400406 final int w = getWidth();
407 final int h = getHeight();
408 final Rect r = new Rect();
409 mNumberPain.getTextBounds(str, 0, str.length(), r);
410 final int tw = r.right - r.left;
411 final int th = r.bottom - r.top;
412 mNumberBackground.getPadding(r);
413 int dw = r.left + tw + r.right;
414 if (dw < mNumberBackground.getMinimumWidth()) {
415 dw = mNumberBackground.getMinimumWidth();
416 }
417 mNumberX = w-r.right-((dw-r.right-r.left)/2);
418 int dh = r.top + th + r.bottom;
419 if (dh < mNumberBackground.getMinimumWidth()) {
420 dh = mNumberBackground.getMinimumWidth();
421 }
422 mNumberY = h-r.bottom-((dh-r.top-th-r.bottom)/2);
423 mNumberBackground.setBounds(w-dw, h-dh, w, h);
424 }
Svetoslav Ganov6179ea32011-06-28 01:12:41 -0700425
426 private void setContentDescription(Notification notification) {
427 if (notification != null) {
Adrian Rooseba05822016-04-22 17:09:27 -0700428 String d = contentDescForNotification(mContext, notification);
429 if (!TextUtils.isEmpty(d)) {
430 setContentDescription(d);
Svetoslav Ganov6179ea32011-06-28 01:12:41 -0700431 }
432 }
433 }
Daniel Sandler7579bca2011-08-18 15:47:26 -0400434
435 public String toString() {
John Spurlock209bede2013-07-17 12:23:27 -0400436 return "StatusBarIconView(slot=" + mSlot + " icon=" + mIcon
Daniel Sandler7579bca2011-08-18 15:47:26 -0400437 + " notification=" + mNotification + ")";
438 }
Jason Monk3b230072015-05-29 11:11:29 -0400439
440 public String getSlot() {
441 return mSlot;
442 }
Adrian Rooseba05822016-04-22 17:09:27 -0700443
444
445 public static String contentDescForNotification(Context c, Notification n) {
Adrian Roosa8e18ef2016-05-26 17:17:02 -0700446 String appName = "";
447 try {
448 Notification.Builder builder = Notification.Builder.recoverBuilder(c, n);
449 appName = builder.loadHeaderAppName();
450 } catch (RuntimeException e) {
451 Log.e(TAG, "Unable to recover builder", e);
452 // Trying to get the app name from the app info instead.
453 Parcelable appInfo = n.extras.getParcelable(
454 Notification.EXTRA_BUILDER_APPLICATION_INFO);
455 if (appInfo instanceof ApplicationInfo) {
456 appName = String.valueOf(((ApplicationInfo) appInfo).loadLabel(
457 c.getPackageManager()));
458 }
459 }
Adrian Roos51548e62016-04-28 11:20:26 -0700460
Julia Reynolds7f9ce782016-05-17 15:34:34 -0400461 CharSequence title = n.extras.getCharSequence(Notification.EXTRA_TITLE);
Adrian Rooseba05822016-04-22 17:09:27 -0700462 CharSequence ticker = n.tickerText;
Adrian Roos51548e62016-04-28 11:20:26 -0700463
464 CharSequence desc = !TextUtils.isEmpty(ticker) ? ticker
465 : !TextUtils.isEmpty(title) ? title : "";
466
467 return c.getString(R.string.accessibility_desc_notification_icon, appName, desc);
Adrian Rooseba05822016-04-22 17:09:27 -0700468 }
Adrian Roos51548e62016-04-28 11:20:26 -0700469
Selim Cinek875ba9b2017-02-13 16:20:17 -0800470 /**
471 * Set the color that is used to draw decoration like the overflow dot. This will not be applied
472 * to the drawable.
473 */
474 public void setDecorColor(int iconTint) {
Adrian Roos456e0052017-04-04 16:44:29 -0700475 mDecorColor = iconTint;
476 updateDecorColor();
477 }
478
479 private void updateDecorColor() {
480 int color = NotificationUtils.interpolateColors(mDecorColor, Color.WHITE, mDarkAmount);
481 if (mDotPaint.getColor() != color) {
482 mDotPaint.setColor(color);
483
484 if (mDotAppearAmount != 0) {
485 invalidate();
486 }
487 }
Selim Cinek49014f82016-11-04 14:55:30 -0700488 }
489
Selim Cinek875ba9b2017-02-13 16:20:17 -0800490 /**
491 * Set the static color that should be used for the drawable of this icon if it's not
492 * transitioning this also immediately sets the color.
493 */
494 public void setStaticDrawableColor(int color) {
495 mDrawableColor = color;
496 setColorInternal(color);
497 mIconColor = color;
Adrian Roos456e0052017-04-04 16:44:29 -0700498 mDozer.setColor(color);
Selim Cinek875ba9b2017-02-13 16:20:17 -0800499 }
500
501 private void setColorInternal(int color) {
502 if (color != NO_COLOR) {
503 setImageTintList(ColorStateList.valueOf(color));
504 } else {
505 setImageTintList(null);
506 }
507 mCurrentSetColor = color;
508 }
509
510 public void setIconColor(int iconColor, boolean animate) {
511 if (mIconColor != iconColor) {
512 mIconColor = iconColor;
513 if (mColorAnimator != null) {
514 mColorAnimator.cancel();
515 }
516 if (mCurrentSetColor == iconColor) {
517 return;
518 }
519 if (animate && mCurrentSetColor != NO_COLOR) {
520 mAnimationStartColor = mCurrentSetColor;
521 mColorAnimator = ValueAnimator.ofFloat(0.0f, 1.0f);
522 mColorAnimator.setInterpolator(Interpolators.FAST_OUT_SLOW_IN);
523 mColorAnimator.setDuration(ANIMATION_DURATION_FAST);
524 mColorAnimator.addUpdateListener(mColorUpdater);
525 mColorAnimator.addListener(new AnimatorListenerAdapter() {
526 @Override
527 public void onAnimationEnd(Animator animation) {
528 mColorAnimator = null;
529 mAnimationStartColor = NO_COLOR;
530 }
531 });
532 mColorAnimator.start();
533 } else {
534 setColorInternal(iconColor);
535 }
536 }
537 }
538
539 public int getStaticDrawableColor() {
540 return mDrawableColor;
541 }
542
Selim Cinek5b5beb012016-11-08 18:11:58 -0800543 public void setVisibleState(int state) {
544 setVisibleState(state, true /* animate */, null /* endRunnable */);
545 }
546
547 public void setVisibleState(int state, boolean animate) {
548 setVisibleState(state, animate, null);
549 }
550
551 @Override
552 public boolean hasOverlappingRendering() {
553 return false;
554 }
555
556 public void setVisibleState(int visibleState, boolean animate, Runnable endRunnable) {
Selim Cinek65d418e2016-11-29 15:42:34 -0800557 boolean runnableAdded = false;
Selim Cinek49014f82016-11-04 14:55:30 -0700558 if (visibleState != mVisibleState) {
559 mVisibleState = visibleState;
Selim Cinek5ece5a72017-02-15 11:47:42 -0800560 if (mIconAppearAnimator != null) {
561 mIconAppearAnimator.cancel();
562 }
563 if (mDotAnimator != null) {
564 mDotAnimator.cancel();
565 }
Selim Cinek5b5beb012016-11-08 18:11:58 -0800566 if (animate) {
Selim Cinek5b5beb012016-11-08 18:11:58 -0800567 float targetAmount = 0.0f;
568 Interpolator interpolator = Interpolators.FAST_OUT_LINEAR_IN;
569 if (visibleState == STATE_ICON) {
570 targetAmount = 1.0f;
571 interpolator = Interpolators.LINEAR_OUT_SLOW_IN;
572 }
Selim Cinek65d418e2016-11-29 15:42:34 -0800573 float currentAmount = getIconAppearAmount();
574 if (targetAmount != currentAmount) {
575 mIconAppearAnimator = ObjectAnimator.ofFloat(this, ICON_APPEAR_AMOUNT,
576 currentAmount, targetAmount);
577 mIconAppearAnimator.setInterpolator(interpolator);
Selim Cinek875ba9b2017-02-13 16:20:17 -0800578 mIconAppearAnimator.setDuration(ANIMATION_DURATION_FAST);
Selim Cinek65d418e2016-11-29 15:42:34 -0800579 mIconAppearAnimator.addListener(new AnimatorListenerAdapter() {
580 @Override
581 public void onAnimationEnd(Animator animation) {
582 mIconAppearAnimator = null;
583 runRunnable(endRunnable);
Selim Cinek5b5beb012016-11-08 18:11:58 -0800584 }
Selim Cinek65d418e2016-11-29 15:42:34 -0800585 });
586 mIconAppearAnimator.start();
587 runnableAdded = true;
588 }
Selim Cinek49014f82016-11-04 14:55:30 -0700589
Selim Cinek5b5beb012016-11-08 18:11:58 -0800590 targetAmount = visibleState == STATE_ICON ? 2.0f : 0.0f;
591 interpolator = Interpolators.FAST_OUT_LINEAR_IN;
592 if (visibleState == STATE_DOT) {
593 targetAmount = 1.0f;
594 interpolator = Interpolators.LINEAR_OUT_SLOW_IN;
595 }
Selim Cinek65d418e2016-11-29 15:42:34 -0800596 currentAmount = getDotAppearAmount();
597 if (targetAmount != currentAmount) {
598 mDotAnimator = ObjectAnimator.ofFloat(this, DOT_APPEAR_AMOUNT,
599 currentAmount, targetAmount);
600 mDotAnimator.setInterpolator(interpolator);
Selim Cinek875ba9b2017-02-13 16:20:17 -0800601 mDotAnimator.setDuration(ANIMATION_DURATION_FAST);
Selim Cinek65d418e2016-11-29 15:42:34 -0800602 final boolean runRunnable = !runnableAdded;
603 mDotAnimator.addListener(new AnimatorListenerAdapter() {
604 @Override
605 public void onAnimationEnd(Animator animation) {
606 mDotAnimator = null;
607 if (runRunnable) {
608 runRunnable(endRunnable);
609 }
610 }
611 });
612 mDotAnimator.start();
613 runnableAdded = true;
614 }
Selim Cinek5b5beb012016-11-08 18:11:58 -0800615 } else {
616 setIconAppearAmount(visibleState == STATE_ICON ? 1.0f : 0.0f);
Selim Cinek01a73f92016-12-06 16:13:42 -0800617 setDotAppearAmount(visibleState == STATE_DOT ? 1.0f
618 : visibleState == STATE_ICON ? 2.0f
619 : 0.0f);
Selim Cinek5b5beb012016-11-08 18:11:58 -0800620 }
Selim Cinek49014f82016-11-04 14:55:30 -0700621 }
Selim Cinek65d418e2016-11-29 15:42:34 -0800622 if (!runnableAdded) {
623 runRunnable(endRunnable);
624 }
625 }
626
627 private void runRunnable(Runnable runnable) {
628 if (runnable != null) {
629 runnable.run();
630 }
Selim Cinek49014f82016-11-04 14:55:30 -0700631 }
632
Selim Cinek5b5beb012016-11-08 18:11:58 -0800633 public void setIconAppearAmount(float iconAppearAmount) {
Selim Cinek9ef119c2017-03-01 15:13:36 -0800634 if (mIconAppearAmount != iconAppearAmount) {
635 mIconAppearAmount = iconAppearAmount;
636 invalidate();
637 }
Selim Cinek49014f82016-11-04 14:55:30 -0700638 }
639
640 public float getIconAppearAmount() {
641 return mIconAppearAmount;
642 }
643
644 public int getVisibleState() {
645 return mVisibleState;
646 }
647
648 public void setDotAppearAmount(float dotAppearAmount) {
Selim Cinek9ef119c2017-03-01 15:13:36 -0800649 if (mDotAppearAmount != dotAppearAmount) {
650 mDotAppearAmount = dotAppearAmount;
651 invalidate();
652 }
Selim Cinek49014f82016-11-04 14:55:30 -0700653 }
654
Selim Cinek2b549f42016-11-22 16:38:51 -0800655 @Override
656 public void setVisibility(int visibility) {
657 super.setVisibility(visibility);
658 if (mOnVisibilityChangedListener != null) {
659 mOnVisibilityChangedListener.onVisibilityChanged(visibility);
660 }
661 }
662
Selim Cinek49014f82016-11-04 14:55:30 -0700663 public float getDotAppearAmount() {
664 return mDotAppearAmount;
665 }
Selim Cinek2b549f42016-11-22 16:38:51 -0800666
667 public void setOnVisibilityChangedListener(OnVisibilityChangedListener listener) {
668 mOnVisibilityChangedListener = listener;
669 }
670
Adrian Roos456e0052017-04-04 16:44:29 -0700671 public void setDark(boolean dark, boolean fade, long delay) {
672 mDozer.setImageDark(this, dark, fade, delay, mIconColor != NO_COLOR);
673 mDozer.setIntensityDark(f -> {
674 mDarkAmount = f;
675 updateDecorColor();
676 }, dark, fade, delay);
677 }
678
Selim Cinek2b549f42016-11-22 16:38:51 -0800679 public interface OnVisibilityChangedListener {
680 void onVisibilityChanged(int newVisibility);
681 }
Joe Onorato0cbda992010-05-02 16:28:15 -0700682}