blob: 263df79318eaf8cdfe0476f260e4e3c370ff1a15 [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 Roosf76600e2017-04-19 15:32:43 -0700462 CharSequence text = n.extras.getCharSequence(Notification.EXTRA_TEXT);
Adrian Rooseba05822016-04-22 17:09:27 -0700463 CharSequence ticker = n.tickerText;
Adrian Roos51548e62016-04-28 11:20:26 -0700464
Adrian Roosf76600e2017-04-19 15:32:43 -0700465 // Some apps just put the app name into the title
466 CharSequence titleOrText = TextUtils.equals(title, appName) ? text : title;
467
468 CharSequence desc = !TextUtils.isEmpty(titleOrText) ? titleOrText
Adrian Roos6e782a52017-03-30 18:34:10 -0700469 : !TextUtils.isEmpty(ticker) ? ticker : "";
Adrian Roos51548e62016-04-28 11:20:26 -0700470
471 return c.getString(R.string.accessibility_desc_notification_icon, appName, desc);
Adrian Rooseba05822016-04-22 17:09:27 -0700472 }
Adrian Roos51548e62016-04-28 11:20:26 -0700473
Selim Cinek875ba9b2017-02-13 16:20:17 -0800474 /**
475 * Set the color that is used to draw decoration like the overflow dot. This will not be applied
476 * to the drawable.
477 */
478 public void setDecorColor(int iconTint) {
Adrian Roos456e0052017-04-04 16:44:29 -0700479 mDecorColor = iconTint;
480 updateDecorColor();
481 }
482
483 private void updateDecorColor() {
484 int color = NotificationUtils.interpolateColors(mDecorColor, Color.WHITE, mDarkAmount);
485 if (mDotPaint.getColor() != color) {
486 mDotPaint.setColor(color);
487
488 if (mDotAppearAmount != 0) {
489 invalidate();
490 }
491 }
Selim Cinek49014f82016-11-04 14:55:30 -0700492 }
493
Selim Cinek875ba9b2017-02-13 16:20:17 -0800494 /**
495 * Set the static color that should be used for the drawable of this icon if it's not
496 * transitioning this also immediately sets the color.
497 */
498 public void setStaticDrawableColor(int color) {
499 mDrawableColor = color;
500 setColorInternal(color);
501 mIconColor = color;
Adrian Roos456e0052017-04-04 16:44:29 -0700502 mDozer.setColor(color);
Selim Cinek875ba9b2017-02-13 16:20:17 -0800503 }
504
505 private void setColorInternal(int color) {
Adrian Roosf3eacd32017-04-14 16:35:58 -0700506 mCurrentSetColor = color;
507 updateIconColor();
508 }
509
510 private void updateIconColor() {
511 if (mCurrentSetColor != NO_COLOR) {
512 setImageTintList(ColorStateList.valueOf(NotificationUtils.interpolateColors(
513 mCurrentSetColor, Color.WHITE, mDarkAmount)));
Selim Cinek875ba9b2017-02-13 16:20:17 -0800514 } else {
515 setImageTintList(null);
Adrian Roosf3eacd32017-04-14 16:35:58 -0700516 mDozer.updateGrayscale(this, mDarkAmount);
Selim Cinek875ba9b2017-02-13 16:20:17 -0800517 }
Selim Cinek875ba9b2017-02-13 16:20:17 -0800518 }
519
520 public void setIconColor(int iconColor, boolean animate) {
521 if (mIconColor != iconColor) {
522 mIconColor = iconColor;
523 if (mColorAnimator != null) {
524 mColorAnimator.cancel();
525 }
526 if (mCurrentSetColor == iconColor) {
527 return;
528 }
529 if (animate && mCurrentSetColor != NO_COLOR) {
530 mAnimationStartColor = mCurrentSetColor;
531 mColorAnimator = ValueAnimator.ofFloat(0.0f, 1.0f);
532 mColorAnimator.setInterpolator(Interpolators.FAST_OUT_SLOW_IN);
533 mColorAnimator.setDuration(ANIMATION_DURATION_FAST);
534 mColorAnimator.addUpdateListener(mColorUpdater);
535 mColorAnimator.addListener(new AnimatorListenerAdapter() {
536 @Override
537 public void onAnimationEnd(Animator animation) {
538 mColorAnimator = null;
539 mAnimationStartColor = NO_COLOR;
540 }
541 });
542 mColorAnimator.start();
543 } else {
544 setColorInternal(iconColor);
545 }
546 }
547 }
548
549 public int getStaticDrawableColor() {
550 return mDrawableColor;
551 }
552
Selim Cinek5b5beb012016-11-08 18:11:58 -0800553 public void setVisibleState(int state) {
554 setVisibleState(state, true /* animate */, null /* endRunnable */);
555 }
556
557 public void setVisibleState(int state, boolean animate) {
558 setVisibleState(state, animate, null);
559 }
560
561 @Override
562 public boolean hasOverlappingRendering() {
563 return false;
564 }
565
566 public void setVisibleState(int visibleState, boolean animate, Runnable endRunnable) {
Selim Cinek65d418e2016-11-29 15:42:34 -0800567 boolean runnableAdded = false;
Selim Cinek49014f82016-11-04 14:55:30 -0700568 if (visibleState != mVisibleState) {
569 mVisibleState = visibleState;
Selim Cinek5ece5a72017-02-15 11:47:42 -0800570 if (mIconAppearAnimator != null) {
571 mIconAppearAnimator.cancel();
572 }
573 if (mDotAnimator != null) {
574 mDotAnimator.cancel();
575 }
Selim Cinek5b5beb012016-11-08 18:11:58 -0800576 if (animate) {
Selim Cinek5b5beb012016-11-08 18:11:58 -0800577 float targetAmount = 0.0f;
578 Interpolator interpolator = Interpolators.FAST_OUT_LINEAR_IN;
579 if (visibleState == STATE_ICON) {
580 targetAmount = 1.0f;
581 interpolator = Interpolators.LINEAR_OUT_SLOW_IN;
582 }
Selim Cinek65d418e2016-11-29 15:42:34 -0800583 float currentAmount = getIconAppearAmount();
584 if (targetAmount != currentAmount) {
585 mIconAppearAnimator = ObjectAnimator.ofFloat(this, ICON_APPEAR_AMOUNT,
586 currentAmount, targetAmount);
587 mIconAppearAnimator.setInterpolator(interpolator);
Selim Cinek875ba9b2017-02-13 16:20:17 -0800588 mIconAppearAnimator.setDuration(ANIMATION_DURATION_FAST);
Selim Cinek65d418e2016-11-29 15:42:34 -0800589 mIconAppearAnimator.addListener(new AnimatorListenerAdapter() {
590 @Override
591 public void onAnimationEnd(Animator animation) {
592 mIconAppearAnimator = null;
593 runRunnable(endRunnable);
Selim Cinek5b5beb012016-11-08 18:11:58 -0800594 }
Selim Cinek65d418e2016-11-29 15:42:34 -0800595 });
596 mIconAppearAnimator.start();
597 runnableAdded = true;
598 }
Selim Cinek49014f82016-11-04 14:55:30 -0700599
Selim Cinek5b5beb012016-11-08 18:11:58 -0800600 targetAmount = visibleState == STATE_ICON ? 2.0f : 0.0f;
601 interpolator = Interpolators.FAST_OUT_LINEAR_IN;
602 if (visibleState == STATE_DOT) {
603 targetAmount = 1.0f;
604 interpolator = Interpolators.LINEAR_OUT_SLOW_IN;
605 }
Selim Cinek65d418e2016-11-29 15:42:34 -0800606 currentAmount = getDotAppearAmount();
607 if (targetAmount != currentAmount) {
608 mDotAnimator = ObjectAnimator.ofFloat(this, DOT_APPEAR_AMOUNT,
609 currentAmount, targetAmount);
610 mDotAnimator.setInterpolator(interpolator);
Selim Cinek875ba9b2017-02-13 16:20:17 -0800611 mDotAnimator.setDuration(ANIMATION_DURATION_FAST);
Selim Cinek65d418e2016-11-29 15:42:34 -0800612 final boolean runRunnable = !runnableAdded;
613 mDotAnimator.addListener(new AnimatorListenerAdapter() {
614 @Override
615 public void onAnimationEnd(Animator animation) {
616 mDotAnimator = null;
617 if (runRunnable) {
618 runRunnable(endRunnable);
619 }
620 }
621 });
622 mDotAnimator.start();
623 runnableAdded = true;
624 }
Selim Cinek5b5beb012016-11-08 18:11:58 -0800625 } else {
626 setIconAppearAmount(visibleState == STATE_ICON ? 1.0f : 0.0f);
Selim Cinek01a73f92016-12-06 16:13:42 -0800627 setDotAppearAmount(visibleState == STATE_DOT ? 1.0f
628 : visibleState == STATE_ICON ? 2.0f
629 : 0.0f);
Selim Cinek5b5beb012016-11-08 18:11:58 -0800630 }
Selim Cinek49014f82016-11-04 14:55:30 -0700631 }
Selim Cinek65d418e2016-11-29 15:42:34 -0800632 if (!runnableAdded) {
633 runRunnable(endRunnable);
634 }
635 }
636
637 private void runRunnable(Runnable runnable) {
638 if (runnable != null) {
639 runnable.run();
640 }
Selim Cinek49014f82016-11-04 14:55:30 -0700641 }
642
Selim Cinek5b5beb012016-11-08 18:11:58 -0800643 public void setIconAppearAmount(float iconAppearAmount) {
Selim Cinek9ef119c2017-03-01 15:13:36 -0800644 if (mIconAppearAmount != iconAppearAmount) {
645 mIconAppearAmount = iconAppearAmount;
646 invalidate();
647 }
Selim Cinek49014f82016-11-04 14:55:30 -0700648 }
649
650 public float getIconAppearAmount() {
651 return mIconAppearAmount;
652 }
653
654 public int getVisibleState() {
655 return mVisibleState;
656 }
657
658 public void setDotAppearAmount(float dotAppearAmount) {
Selim Cinek9ef119c2017-03-01 15:13:36 -0800659 if (mDotAppearAmount != dotAppearAmount) {
660 mDotAppearAmount = dotAppearAmount;
661 invalidate();
662 }
Selim Cinek49014f82016-11-04 14:55:30 -0700663 }
664
Selim Cinek2b549f42016-11-22 16:38:51 -0800665 @Override
666 public void setVisibility(int visibility) {
667 super.setVisibility(visibility);
668 if (mOnVisibilityChangedListener != null) {
669 mOnVisibilityChangedListener.onVisibilityChanged(visibility);
670 }
671 }
672
Selim Cinek49014f82016-11-04 14:55:30 -0700673 public float getDotAppearAmount() {
674 return mDotAppearAmount;
675 }
Selim Cinek2b549f42016-11-22 16:38:51 -0800676
677 public void setOnVisibilityChangedListener(OnVisibilityChangedListener listener) {
678 mOnVisibilityChangedListener = listener;
679 }
680
Adrian Roos456e0052017-04-04 16:44:29 -0700681 public void setDark(boolean dark, boolean fade, long delay) {
Adrian Roos456e0052017-04-04 16:44:29 -0700682 mDozer.setIntensityDark(f -> {
683 mDarkAmount = f;
684 updateDecorColor();
Adrian Roosf3eacd32017-04-14 16:35:58 -0700685 updateIconColor();
Adrian Roos456e0052017-04-04 16:44:29 -0700686 }, dark, fade, delay);
687 }
688
Selim Cinek2b549f42016-11-22 16:38:51 -0800689 public interface OnVisibilityChangedListener {
690 void onVisibilityChanged(int newVisibility);
691 }
Joe Onorato0cbda992010-05-02 16:28:15 -0700692}