blob: 399b0d293ac2edff17d5eef15f4eea05db9676ce [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;
Svetoslav Ganov6179ea32011-06-28 01:12:41 -070022import android.app.Notification;
Joe Onorato0cbda992010-05-02 16:28:15 -070023import android.content.Context;
Adrian Roosa8e18ef2016-05-26 17:17:02 -070024import android.content.pm.ApplicationInfo;
Selim Cinek3e7592d2016-04-11 09:35:54 +080025import android.content.res.Configuration;
Joe Onorato0cbda992010-05-02 16:28:15 -070026import android.content.res.Resources;
Joe Onorato0cbda992010-05-02 16:28:15 -070027import android.graphics.Canvas;
Joe Onorato6c01a112010-10-04 17:38:47 -040028import android.graphics.Paint;
29import android.graphics.Rect;
John Spurlockde84f0e2013-06-12 12:41:00 -040030import android.graphics.drawable.Drawable;
Dan Sandlerd63f9322015-05-06 15:18:49 -040031import android.graphics.drawable.Icon;
Adrian Roosa8e18ef2016-05-26 17:17:02 -070032import android.os.Parcelable;
Jeff Sharkeyded653b2012-09-27 19:09:24 -070033import android.os.UserHandle;
Svetoslav Ganov6179ea32011-06-28 01:12:41 -070034import android.text.TextUtils;
Daniel Sandler05e24142011-11-10 11:56:49 -050035import android.util.AttributeSet;
Selim Cinek49014f82016-11-04 14:55:30 -070036import android.util.FloatProperty;
Joe Onoratof9ec03c2010-09-23 15:09:18 -070037import android.util.Log;
Selim Cinek49014f82016-11-04 14:55:30 -070038import android.util.Property;
Anthony Chen55e8e1e2016-01-08 10:31:46 -080039import android.util.TypedValue;
Selim Cinek2b549f42016-11-22 16:38:51 -080040import android.view.View;
Joe Onorato0cbda992010-05-02 16:28:15 -070041import android.view.ViewDebug;
Svetoslav Ganov6179ea32011-06-28 01:12:41 -070042import android.view.accessibility.AccessibilityEvent;
Selim Cinek49014f82016-11-04 14:55:30 -070043import android.view.animation.Interpolator;
Winsonc0d70582016-01-29 10:24:39 -080044
Joe Onorato0cbda992010-05-02 16:28:15 -070045import com.android.internal.statusbar.StatusBarIcon;
Selim Cinek49014f82016-11-04 14:55:30 -070046import com.android.systemui.Interpolators;
Joe Onorato6c01a112010-10-04 17:38:47 -040047import com.android.systemui.R;
Selim Cinek49014f82016-11-04 14:55:30 -070048import com.android.systemui.statusbar.notification.NotificationUtils;
Joe Onorato6c01a112010-10-04 17:38:47 -040049
John Spurlockde84f0e2013-06-12 12:41:00 -040050import java.text.NumberFormat;
51
Joe Onorato0cbda992010-05-02 16:28:15 -070052public class StatusBarIconView extends AnimatedImageView {
Selim Cinek49014f82016-11-04 14:55:30 -070053 public static final int STATE_ICON = 0;
54 public static final int STATE_DOT = 1;
55 public static final int STATE_HIDDEN = 2;
Joe Onorato0cbda992010-05-02 16:28:15 -070056
Selim Cinek49014f82016-11-04 14:55:30 -070057 private static final String TAG = "StatusBarIconView";
58 private static final Property<StatusBarIconView, Float> ICON_APPEAR_AMOUNT
59 = new FloatProperty<StatusBarIconView>("iconAppearAmount") {
60
61 @Override
62 public void setValue(StatusBarIconView object, float value) {
63 object.setIconAppearAmount(value);
64 }
65
66 @Override
67 public Float get(StatusBarIconView object) {
68 return object.getIconAppearAmount();
69 }
70 };
Selim Cinek5b5beb012016-11-08 18:11:58 -080071 private static final Property<StatusBarIconView, Float> DOT_APPEAR_AMOUNT
Selim Cinek49014f82016-11-04 14:55:30 -070072 = new FloatProperty<StatusBarIconView>("dot_appear_amount") {
73
74 @Override
75 public void setValue(StatusBarIconView object, float value) {
76 object.setDotAppearAmount(value);
77 }
78
79 @Override
80 public Float get(StatusBarIconView object) {
81 return object.getDotAppearAmount();
82 }
83 };
84
85 private boolean mAlwaysScaleIcon;
Joe Onorato0cbda992010-05-02 16:28:15 -070086 private StatusBarIcon mIcon;
87 @ViewDebug.ExportedProperty private String mSlot;
Joe Onorato6c01a112010-10-04 17:38:47 -040088 private Drawable mNumberBackground;
89 private Paint mNumberPain;
90 private int mNumberX;
91 private int mNumberY;
92 private String mNumberText;
Svetoslav Ganov6179ea32011-06-28 01:12:41 -070093 private Notification mNotification;
Jason Monk3b230072015-05-29 11:11:29 -040094 private final boolean mBlocked;
Selim Cinek3e7592d2016-04-11 09:35:54 +080095 private int mDensity;
Selim Cinek281c2022016-10-13 19:14:43 -070096 private float mIconScale = 1.0f;
Selim Cinek49014f82016-11-04 14:55:30 -070097 private final Paint mDotPaint = new Paint();
98 private boolean mDotVisible;
99 private float mDotRadius;
100 private int mStaticDotRadius;
101 private int mVisibleState = STATE_ICON;
102 private float mIconAppearAmount = 1.0f;
103 private ObjectAnimator mIconAppearAnimator;
104 private ObjectAnimator mDotAnimator;
105 private float mDotAppearAmount;
Selim Cinek2b549f42016-11-22 16:38:51 -0800106 private OnVisibilityChangedListener mOnVisibilityChangedListener;
Joe Onorato0cbda992010-05-02 16:28:15 -0700107
John Spurlocke6f0a712013-09-03 16:23:49 -0400108 public StatusBarIconView(Context context, String slot, Notification notification) {
Jason Monk3b230072015-05-29 11:11:29 -0400109 this(context, slot, notification, false);
110 }
111
112 public StatusBarIconView(Context context, String slot, Notification notification,
113 boolean blocked) {
Joe Onorato0cbda992010-05-02 16:28:15 -0700114 super(context);
Jason Monk3b230072015-05-29 11:11:29 -0400115 mBlocked = blocked;
Joe Onorato0cbda992010-05-02 16:28:15 -0700116 mSlot = slot;
Joe Onorato6c01a112010-10-04 17:38:47 -0400117 mNumberPain = new Paint();
118 mNumberPain.setTextAlign(Paint.Align.CENTER);
Alan Viverette4a357cd2015-03-18 18:37:18 -0700119 mNumberPain.setColor(context.getColor(R.drawable.notification_number_text_color));
Joe Onorato6c01a112010-10-04 17:38:47 -0400120 mNumberPain.setAntiAlias(true);
Christoph Studera0506e72014-07-31 20:27:39 +0200121 setNotification(notification);
Selim Cinek3e7592d2016-04-11 09:35:54 +0800122 maybeUpdateIconScale();
123 setScaleType(ScaleType.CENTER);
124 mDensity = context.getResources().getDisplayMetrics().densityDpi;
Selim Cinek49014f82016-11-04 14:55:30 -0700125 if (mNotification != null) {
126 setIconTint(getContext().getColor(
127 com.android.internal.R.color.notification_icon_default_color));
128 }
129 reloadDimens();
Selim Cinek3e7592d2016-04-11 09:35:54 +0800130 }
Daniel Sandler26c84b12011-07-27 00:09:40 -0400131
Selim Cinek3e7592d2016-04-11 09:35:54 +0800132 private void maybeUpdateIconScale() {
Daniel Sandler7579bca2011-08-18 15:47:26 -0400133 // We do not resize and scale system icons (on the right), only notification icons (on the
134 // left).
Selim Cinek3e7592d2016-04-11 09:35:54 +0800135 if (mNotification != null || mAlwaysScaleIcon) {
136 updateIconScale();
Daniel Sandler7579bca2011-08-18 15:47:26 -0400137 }
Selim Cinek3e7592d2016-04-11 09:35:54 +0800138 }
Daniel Sandlerabff0322011-09-27 11:19:34 -0400139
Selim Cinek3e7592d2016-04-11 09:35:54 +0800140 private void updateIconScale() {
141 Resources res = mContext.getResources();
142 final int outerBounds = res.getDimensionPixelSize(R.dimen.status_bar_icon_size);
143 final int imageBounds = res.getDimensionPixelSize(R.dimen.status_bar_icon_drawing_size);
Selim Cinek281c2022016-10-13 19:14:43 -0700144 mIconScale = (float)imageBounds / (float)outerBounds;
Selim Cinek281c2022016-10-13 19:14:43 -0700145 }
146
147 public float getIconScale() {
148 return mIconScale;
Selim Cinek3e7592d2016-04-11 09:35:54 +0800149 }
150
151 @Override
152 protected void onConfigurationChanged(Configuration newConfig) {
153 super.onConfigurationChanged(newConfig);
154 int density = newConfig.densityDpi;
155 if (density != mDensity) {
156 mDensity = density;
157 maybeUpdateIconScale();
158 updateDrawable();
Selim Cinek49014f82016-11-04 14:55:30 -0700159 reloadDimens();
160 }
161 }
162
163 private void reloadDimens() {
164 boolean applyRadius = mDotRadius == mStaticDotRadius;
165 mStaticDotRadius = getResources().getDimensionPixelSize(R.dimen.overflow_dot_radius);
166 if (applyRadius) {
167 mDotRadius = mStaticDotRadius;
Selim Cinek3e7592d2016-04-11 09:35:54 +0800168 }
Joe Onorato0cbda992010-05-02 16:28:15 -0700169 }
170
Christoph Studera0506e72014-07-31 20:27:39 +0200171 public void setNotification(Notification notification) {
172 mNotification = notification;
173 setContentDescription(notification);
174 }
175
Daniel Sandler05e24142011-11-10 11:56:49 -0500176 public StatusBarIconView(Context context, AttributeSet attrs) {
177 super(context, attrs);
Jason Monk3b230072015-05-29 11:11:29 -0400178 mBlocked = false;
Selim Cinek3e7592d2016-04-11 09:35:54 +0800179 mAlwaysScaleIcon = true;
180 updateIconScale();
181 mDensity = context.getResources().getDisplayMetrics().densityDpi;
Daniel Sandler05e24142011-11-10 11:56:49 -0500182 }
183
Joe Onorato0cbda992010-05-02 16:28:15 -0700184 private static boolean streq(String a, String b) {
Joe Onorato66d7d012010-05-14 10:05:10 -0700185 if (a == b) {
186 return true;
187 }
Joe Onorato0cbda992010-05-02 16:28:15 -0700188 if (a == null && b != null) {
189 return false;
190 }
191 if (a != null && b == null) {
192 return false;
193 }
194 return a.equals(b);
195 }
196
Dan Sandlerd63f9322015-05-06 15:18:49 -0400197 public boolean equalIcons(Icon a, Icon b) {
198 if (a == b) return true;
199 if (a.getType() != b.getType()) return false;
200 switch (a.getType()) {
201 case Icon.TYPE_RESOURCE:
202 return a.getResPackage().equals(b.getResPackage()) && a.getResId() == b.getResId();
203 case Icon.TYPE_URI:
204 return a.getUriString().equals(b.getUriString());
205 default:
206 return false;
207 }
208 }
Joe Onorato005847b2010-06-04 16:08:02 -0400209 /**
210 * Returns whether the set succeeded.
211 */
212 public boolean set(StatusBarIcon icon) {
Dan Sandlerd63f9322015-05-06 15:18:49 -0400213 final boolean iconEquals = mIcon != null && equalIcons(mIcon.icon, icon.icon);
Joe Onorato005847b2010-06-04 16:08:02 -0400214 final boolean levelEquals = iconEquals
215 && mIcon.iconLevel == icon.iconLevel;
216 final boolean visibilityEquals = mIcon != null
217 && mIcon.visible == icon.visible;
Joe Onorato6c01a112010-10-04 17:38:47 -0400218 final boolean numberEquals = mIcon != null
219 && mIcon.number == icon.number;
220 mIcon = icon.clone();
Svetoslav Ganov6179ea32011-06-28 01:12:41 -0700221 setContentDescription(icon.contentDescription);
Joe Onorato005847b2010-06-04 16:08:02 -0400222 if (!iconEquals) {
Fabrice Di Meglio82fca5d2013-01-09 15:42:31 -0800223 if (!updateDrawable(false /* no clear */)) return false;
Selim Cinek2b6eb8032016-12-29 14:22:21 +0100224 // we have to clear the grayscale tag since it may have changed
225 setTag(R.id.icon_is_grayscale, null);
Joe Onorato0cbda992010-05-02 16:28:15 -0700226 }
Joe Onorato005847b2010-06-04 16:08:02 -0400227 if (!levelEquals) {
228 setImageLevel(icon.iconLevel);
Joe Onorato0cbda992010-05-02 16:28:15 -0700229 }
Daniel Sandler26c84b12011-07-27 00:09:40 -0400230
Joe Onorato6c01a112010-10-04 17:38:47 -0400231 if (!numberEquals) {
John Spurlock01534782014-01-13 11:59:22 -0500232 if (icon.number > 0 && getContext().getResources().getBoolean(
Joe Onorato8595a3d2010-11-19 18:12:07 -0800233 R.bool.config_statusBarShowNumber)) {
Joe Onorato6c01a112010-10-04 17:38:47 -0400234 if (mNumberBackground == null) {
235 mNumberBackground = getContext().getResources().getDrawable(
236 R.drawable.ic_notification_overlay);
237 }
238 placeNumber();
239 } else {
240 mNumberBackground = null;
241 mNumberText = null;
242 }
243 invalidate();
244 }
Joe Onorato005847b2010-06-04 16:08:02 -0400245 if (!visibilityEquals) {
Jason Monk3b230072015-05-29 11:11:29 -0400246 setVisibility(icon.visible && !mBlocked ? VISIBLE : GONE);
Joe Onorato005847b2010-06-04 16:08:02 -0400247 }
Joe Onorato005847b2010-06-04 16:08:02 -0400248 return true;
Joe Onorato0cbda992010-05-02 16:28:15 -0700249 }
250
Fabrice Di Meglio82fca5d2013-01-09 15:42:31 -0800251 public void updateDrawable() {
252 updateDrawable(true /* with clear */);
253 }
254
255 private boolean updateDrawable(boolean withClear) {
Jorim Jaggi66ac1332015-01-21 19:22:26 +0100256 if (mIcon == null) {
257 return false;
258 }
Fabrice Di Meglio82fca5d2013-01-09 15:42:31 -0800259 Drawable drawable = getIcon(mIcon);
260 if (drawable == null) {
John Spurlockcd686b52013-06-05 10:13:46 -0400261 Log.w(TAG, "No icon for slot " + mSlot);
Fabrice Di Meglio82fca5d2013-01-09 15:42:31 -0800262 return false;
263 }
264 if (withClear) {
265 setImageDrawable(null);
266 }
267 setImageDrawable(drawable);
268 return true;
269 }
270
Joe Onoratof5510542010-06-01 07:46:41 -0700271 private Drawable getIcon(StatusBarIcon icon) {
272 return getIcon(getContext(), icon);
273 }
274
Joe Onorato0cbda992010-05-02 16:28:15 -0700275 /**
Dan Sandlerd63f9322015-05-06 15:18:49 -0400276 * Returns the right icon to use for this item
John Spurlock209bede2013-07-17 12:23:27 -0400277 *
Dan Sandlerd63f9322015-05-06 15:18:49 -0400278 * @param context Context to use to get resources
Joe Onorato0cbda992010-05-02 16:28:15 -0700279 * @return Drawable for this item, or null if the package or item could not
280 * be found
281 */
Anthony Chen55e8e1e2016-01-08 10:31:46 -0800282 public static Drawable getIcon(Context context, StatusBarIcon statusBarIcon) {
283 int userId = statusBarIcon.user.getIdentifier();
Dan Sandlerd63f9322015-05-06 15:18:49 -0400284 if (userId == UserHandle.USER_ALL) {
Xiaohui Chen87d0e252015-07-30 15:38:16 -0700285 userId = UserHandle.USER_SYSTEM;
Joe Onorato0cbda992010-05-02 16:28:15 -0700286 }
Anthony Chen55e8e1e2016-01-08 10:31:46 -0800287
288 Drawable icon = statusBarIcon.icon.loadDrawableAsUser(context, userId);
289
290 TypedValue typedValue = new TypedValue();
291 context.getResources().getValue(R.dimen.status_bar_icon_scale_factor, typedValue, true);
292 float scaleFactor = typedValue.getFloat();
293
294 // No need to scale the icon, so return it as is.
295 if (scaleFactor == 1.f) {
296 return icon;
297 }
298
299 return new ScalingDrawableWrapper(icon, scaleFactor);
Joe Onorato0cbda992010-05-02 16:28:15 -0700300 }
Joe Onoratob77f53b2010-05-28 19:59:51 -0400301
302 public StatusBarIcon getStatusBarIcon() {
303 return mIcon;
304 }
Joe Onoratof9ec03c2010-09-23 15:09:18 -0700305
Svetoslav Ganov6179ea32011-06-28 01:12:41 -0700306 @Override
307 public void onInitializeAccessibilityEvent(AccessibilityEvent event) {
308 super.onInitializeAccessibilityEvent(event);
309 if (mNotification != null) {
310 event.setParcelableData(mNotification);
311 }
312 }
313
314 @Override
Joe Onorato6c01a112010-10-04 17:38:47 -0400315 protected void onSizeChanged(int w, int h, int oldw, int oldh) {
316 super.onSizeChanged(w, h, oldw, oldh);
317 if (mNumberBackground != null) {
318 placeNumber();
319 }
320 }
321
Svetoslav Ganov6179ea32011-06-28 01:12:41 -0700322 @Override
Jorim Jaggi66ac1332015-01-21 19:22:26 +0100323 public void onRtlPropertiesChanged(int layoutDirection) {
324 super.onRtlPropertiesChanged(layoutDirection);
325 updateDrawable();
326 }
327
328 @Override
Joe Onorato6c01a112010-10-04 17:38:47 -0400329 protected void onDraw(Canvas canvas) {
Selim Cinek49014f82016-11-04 14:55:30 -0700330 if (mIconAppearAmount > 0.0f) {
331 canvas.save();
332 canvas.scale(mIconScale * mIconAppearAmount, mIconScale * mIconAppearAmount,
333 getWidth() / 2, getHeight() / 2);
334 super.onDraw(canvas);
335 canvas.restore();
336 }
Joe Onorato6c01a112010-10-04 17:38:47 -0400337
338 if (mNumberBackground != null) {
339 mNumberBackground.draw(canvas);
340 canvas.drawText(mNumberText, mNumberX, mNumberY, mNumberPain);
341 }
Selim Cinek49014f82016-11-04 14:55:30 -0700342 if (mDotAppearAmount != 0.0f) {
343 float radius;
344 float alpha;
345 if (mDotAppearAmount <= 1.0f) {
346 radius = mDotRadius * mDotAppearAmount;
347 alpha = 1.0f;
348 } else {
349 float fadeOutAmount = mDotAppearAmount - 1.0f;
350 alpha = 1.0f - fadeOutAmount;
351 radius = NotificationUtils.interpolate(mDotRadius, getWidth() / 4, fadeOutAmount);
352 }
353 mDotPaint.setAlpha((int) (alpha * 255));
354 canvas.drawCircle(getWidth() / 2, getHeight() / 2, radius, mDotPaint);
355 }
Joe Onorato6c01a112010-10-04 17:38:47 -0400356 }
357
Svetoslav Ganov6179ea32011-06-28 01:12:41 -0700358 @Override
Joe Onoratof9ec03c2010-09-23 15:09:18 -0700359 protected void debug(int depth) {
360 super.debug(depth);
361 Log.d("View", debugIndent(depth) + "slot=" + mSlot);
362 Log.d("View", debugIndent(depth) + "icon=" + mIcon);
363 }
Joe Onorato6c01a112010-10-04 17:38:47 -0400364
365 void placeNumber() {
Daniel Sandlerebce0112011-06-16 16:44:51 -0400366 final String str;
John Spurlock01534782014-01-13 11:59:22 -0500367 final int tooBig = getContext().getResources().getInteger(
Daniel Sandlerebce0112011-06-16 16:44:51 -0400368 android.R.integer.status_bar_notification_info_maxnum);
369 if (mIcon.number > tooBig) {
John Spurlock01534782014-01-13 11:59:22 -0500370 str = getContext().getResources().getString(
Daniel Sandlerebce0112011-06-16 16:44:51 -0400371 android.R.string.status_bar_notification_info_overflow);
372 } else {
373 NumberFormat f = NumberFormat.getIntegerInstance();
374 str = f.format(mIcon.number);
375 }
376 mNumberText = str;
377
Joe Onorato6c01a112010-10-04 17:38:47 -0400378 final int w = getWidth();
379 final int h = getHeight();
380 final Rect r = new Rect();
381 mNumberPain.getTextBounds(str, 0, str.length(), r);
382 final int tw = r.right - r.left;
383 final int th = r.bottom - r.top;
384 mNumberBackground.getPadding(r);
385 int dw = r.left + tw + r.right;
386 if (dw < mNumberBackground.getMinimumWidth()) {
387 dw = mNumberBackground.getMinimumWidth();
388 }
389 mNumberX = w-r.right-((dw-r.right-r.left)/2);
390 int dh = r.top + th + r.bottom;
391 if (dh < mNumberBackground.getMinimumWidth()) {
392 dh = mNumberBackground.getMinimumWidth();
393 }
394 mNumberY = h-r.bottom-((dh-r.top-th-r.bottom)/2);
395 mNumberBackground.setBounds(w-dw, h-dh, w, h);
396 }
Svetoslav Ganov6179ea32011-06-28 01:12:41 -0700397
398 private void setContentDescription(Notification notification) {
399 if (notification != null) {
Adrian Rooseba05822016-04-22 17:09:27 -0700400 String d = contentDescForNotification(mContext, notification);
401 if (!TextUtils.isEmpty(d)) {
402 setContentDescription(d);
Svetoslav Ganov6179ea32011-06-28 01:12:41 -0700403 }
404 }
405 }
Daniel Sandler7579bca2011-08-18 15:47:26 -0400406
407 public String toString() {
John Spurlock209bede2013-07-17 12:23:27 -0400408 return "StatusBarIconView(slot=" + mSlot + " icon=" + mIcon
Daniel Sandler7579bca2011-08-18 15:47:26 -0400409 + " notification=" + mNotification + ")";
410 }
Jason Monk3b230072015-05-29 11:11:29 -0400411
412 public String getSlot() {
413 return mSlot;
414 }
Adrian Rooseba05822016-04-22 17:09:27 -0700415
416
417 public static String contentDescForNotification(Context c, Notification n) {
Adrian Roosa8e18ef2016-05-26 17:17:02 -0700418 String appName = "";
419 try {
420 Notification.Builder builder = Notification.Builder.recoverBuilder(c, n);
421 appName = builder.loadHeaderAppName();
422 } catch (RuntimeException e) {
423 Log.e(TAG, "Unable to recover builder", e);
424 // Trying to get the app name from the app info instead.
425 Parcelable appInfo = n.extras.getParcelable(
426 Notification.EXTRA_BUILDER_APPLICATION_INFO);
427 if (appInfo instanceof ApplicationInfo) {
428 appName = String.valueOf(((ApplicationInfo) appInfo).loadLabel(
429 c.getPackageManager()));
430 }
431 }
Adrian Roos51548e62016-04-28 11:20:26 -0700432
Julia Reynolds7f9ce782016-05-17 15:34:34 -0400433 CharSequence title = n.extras.getCharSequence(Notification.EXTRA_TITLE);
Adrian Rooseba05822016-04-22 17:09:27 -0700434 CharSequence ticker = n.tickerText;
Adrian Roos51548e62016-04-28 11:20:26 -0700435
436 CharSequence desc = !TextUtils.isEmpty(ticker) ? ticker
437 : !TextUtils.isEmpty(title) ? title : "";
438
439 return c.getString(R.string.accessibility_desc_notification_icon, appName, desc);
Adrian Rooseba05822016-04-22 17:09:27 -0700440 }
Adrian Roos51548e62016-04-28 11:20:26 -0700441
Selim Cinek49014f82016-11-04 14:55:30 -0700442 public void setIconTint(int iconTint) {
443 mDotPaint.setColor(iconTint);
444 }
445
Selim Cinek5b5beb012016-11-08 18:11:58 -0800446 public void setVisibleState(int state) {
447 setVisibleState(state, true /* animate */, null /* endRunnable */);
448 }
449
450 public void setVisibleState(int state, boolean animate) {
451 setVisibleState(state, animate, null);
452 }
453
454 @Override
455 public boolean hasOverlappingRendering() {
456 return false;
457 }
458
459 public void setVisibleState(int visibleState, boolean animate, Runnable endRunnable) {
Selim Cinek65d418e2016-11-29 15:42:34 -0800460 boolean runnableAdded = false;
Selim Cinek49014f82016-11-04 14:55:30 -0700461 if (visibleState != mVisibleState) {
462 mVisibleState = visibleState;
Selim Cinek5b5beb012016-11-08 18:11:58 -0800463 if (animate) {
464 if (mIconAppearAnimator != null) {
465 mIconAppearAnimator.cancel();
Selim Cinek49014f82016-11-04 14:55:30 -0700466 }
Selim Cinek5b5beb012016-11-08 18:11:58 -0800467 float targetAmount = 0.0f;
468 Interpolator interpolator = Interpolators.FAST_OUT_LINEAR_IN;
469 if (visibleState == STATE_ICON) {
470 targetAmount = 1.0f;
471 interpolator = Interpolators.LINEAR_OUT_SLOW_IN;
472 }
Selim Cinek65d418e2016-11-29 15:42:34 -0800473 float currentAmount = getIconAppearAmount();
474 if (targetAmount != currentAmount) {
475 mIconAppearAnimator = ObjectAnimator.ofFloat(this, ICON_APPEAR_AMOUNT,
476 currentAmount, targetAmount);
477 mIconAppearAnimator.setInterpolator(interpolator);
478 mIconAppearAnimator.setDuration(100);
479 mIconAppearAnimator.addListener(new AnimatorListenerAdapter() {
480 @Override
481 public void onAnimationEnd(Animator animation) {
482 mIconAppearAnimator = null;
483 runRunnable(endRunnable);
Selim Cinek5b5beb012016-11-08 18:11:58 -0800484 }
Selim Cinek65d418e2016-11-29 15:42:34 -0800485 });
486 mIconAppearAnimator.start();
487 runnableAdded = true;
488 }
Selim Cinek49014f82016-11-04 14:55:30 -0700489
Selim Cinek5b5beb012016-11-08 18:11:58 -0800490 if (mDotAnimator != null) {
491 mDotAnimator.cancel();
Selim Cinek49014f82016-11-04 14:55:30 -0700492 }
Selim Cinek5b5beb012016-11-08 18:11:58 -0800493 targetAmount = visibleState == STATE_ICON ? 2.0f : 0.0f;
494 interpolator = Interpolators.FAST_OUT_LINEAR_IN;
495 if (visibleState == STATE_DOT) {
496 targetAmount = 1.0f;
497 interpolator = Interpolators.LINEAR_OUT_SLOW_IN;
498 }
Selim Cinek65d418e2016-11-29 15:42:34 -0800499 currentAmount = getDotAppearAmount();
500 if (targetAmount != currentAmount) {
501 mDotAnimator = ObjectAnimator.ofFloat(this, DOT_APPEAR_AMOUNT,
502 currentAmount, targetAmount);
503 mDotAnimator.setInterpolator(interpolator);
504 mDotAnimator.setDuration(100);
505 final boolean runRunnable = !runnableAdded;
506 mDotAnimator.addListener(new AnimatorListenerAdapter() {
507 @Override
508 public void onAnimationEnd(Animator animation) {
509 mDotAnimator = null;
510 if (runRunnable) {
511 runRunnable(endRunnable);
512 }
513 }
514 });
515 mDotAnimator.start();
516 runnableAdded = true;
517 }
Selim Cinek5b5beb012016-11-08 18:11:58 -0800518 } else {
519 setIconAppearAmount(visibleState == STATE_ICON ? 1.0f : 0.0f);
Selim Cinek01a73f92016-12-06 16:13:42 -0800520 setDotAppearAmount(visibleState == STATE_DOT ? 1.0f
521 : visibleState == STATE_ICON ? 2.0f
522 : 0.0f);
Selim Cinek5b5beb012016-11-08 18:11:58 -0800523 }
Selim Cinek49014f82016-11-04 14:55:30 -0700524 }
Selim Cinek65d418e2016-11-29 15:42:34 -0800525 if (!runnableAdded) {
526 runRunnable(endRunnable);
527 }
528 }
529
530 private void runRunnable(Runnable runnable) {
531 if (runnable != null) {
532 runnable.run();
533 }
Selim Cinek49014f82016-11-04 14:55:30 -0700534 }
535
Selim Cinek5b5beb012016-11-08 18:11:58 -0800536 public void setIconAppearAmount(float iconAppearAmount) {
Selim Cinek49014f82016-11-04 14:55:30 -0700537 mIconAppearAmount = iconAppearAmount;
538 invalidate();
539 }
540
541 public float getIconAppearAmount() {
542 return mIconAppearAmount;
543 }
544
545 public int getVisibleState() {
546 return mVisibleState;
547 }
548
549 public void setDotAppearAmount(float dotAppearAmount) {
550 mDotAppearAmount = dotAppearAmount;
551 invalidate();
552 }
553
Selim Cinek2b549f42016-11-22 16:38:51 -0800554 @Override
555 public void setVisibility(int visibility) {
556 super.setVisibility(visibility);
557 if (mOnVisibilityChangedListener != null) {
558 mOnVisibilityChangedListener.onVisibilityChanged(visibility);
559 }
560 }
561
Selim Cinek49014f82016-11-04 14:55:30 -0700562 public float getDotAppearAmount() {
563 return mDotAppearAmount;
564 }
Selim Cinek2b549f42016-11-22 16:38:51 -0800565
566 public void setOnVisibilityChangedListener(OnVisibilityChangedListener listener) {
567 mOnVisibilityChangedListener = listener;
568 }
569
570 public interface OnVisibilityChangedListener {
571 void onVisibilityChanged(int newVisibility);
572 }
Joe Onorato0cbda992010-05-02 16:28:15 -0700573}