blob: a2c2fd7731415c6ae89959e9017c1229b363d390 [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;
Joe Onorato0cbda992010-05-02 16:28:15 -0700224 }
Joe Onorato005847b2010-06-04 16:08:02 -0400225 if (!levelEquals) {
226 setImageLevel(icon.iconLevel);
Joe Onorato0cbda992010-05-02 16:28:15 -0700227 }
Daniel Sandler26c84b12011-07-27 00:09:40 -0400228
Joe Onorato6c01a112010-10-04 17:38:47 -0400229 if (!numberEquals) {
John Spurlock01534782014-01-13 11:59:22 -0500230 if (icon.number > 0 && getContext().getResources().getBoolean(
Joe Onorato8595a3d2010-11-19 18:12:07 -0800231 R.bool.config_statusBarShowNumber)) {
Joe Onorato6c01a112010-10-04 17:38:47 -0400232 if (mNumberBackground == null) {
233 mNumberBackground = getContext().getResources().getDrawable(
234 R.drawable.ic_notification_overlay);
235 }
236 placeNumber();
237 } else {
238 mNumberBackground = null;
239 mNumberText = null;
240 }
241 invalidate();
242 }
Joe Onorato005847b2010-06-04 16:08:02 -0400243 if (!visibilityEquals) {
Jason Monk3b230072015-05-29 11:11:29 -0400244 setVisibility(icon.visible && !mBlocked ? VISIBLE : GONE);
Joe Onorato005847b2010-06-04 16:08:02 -0400245 }
Joe Onorato005847b2010-06-04 16:08:02 -0400246 return true;
Joe Onorato0cbda992010-05-02 16:28:15 -0700247 }
248
Fabrice Di Meglio82fca5d2013-01-09 15:42:31 -0800249 public void updateDrawable() {
250 updateDrawable(true /* with clear */);
251 }
252
253 private boolean updateDrawable(boolean withClear) {
Jorim Jaggi66ac1332015-01-21 19:22:26 +0100254 if (mIcon == null) {
255 return false;
256 }
Fabrice Di Meglio82fca5d2013-01-09 15:42:31 -0800257 Drawable drawable = getIcon(mIcon);
258 if (drawable == null) {
John Spurlockcd686b52013-06-05 10:13:46 -0400259 Log.w(TAG, "No icon for slot " + mSlot);
Fabrice Di Meglio82fca5d2013-01-09 15:42:31 -0800260 return false;
261 }
262 if (withClear) {
263 setImageDrawable(null);
264 }
265 setImageDrawable(drawable);
266 return true;
267 }
268
Joe Onoratof5510542010-06-01 07:46:41 -0700269 private Drawable getIcon(StatusBarIcon icon) {
270 return getIcon(getContext(), icon);
271 }
272
Joe Onorato0cbda992010-05-02 16:28:15 -0700273 /**
Dan Sandlerd63f9322015-05-06 15:18:49 -0400274 * Returns the right icon to use for this item
John Spurlock209bede2013-07-17 12:23:27 -0400275 *
Dan Sandlerd63f9322015-05-06 15:18:49 -0400276 * @param context Context to use to get resources
Joe Onorato0cbda992010-05-02 16:28:15 -0700277 * @return Drawable for this item, or null if the package or item could not
278 * be found
279 */
Anthony Chen55e8e1e2016-01-08 10:31:46 -0800280 public static Drawable getIcon(Context context, StatusBarIcon statusBarIcon) {
281 int userId = statusBarIcon.user.getIdentifier();
Dan Sandlerd63f9322015-05-06 15:18:49 -0400282 if (userId == UserHandle.USER_ALL) {
Xiaohui Chen87d0e252015-07-30 15:38:16 -0700283 userId = UserHandle.USER_SYSTEM;
Joe Onorato0cbda992010-05-02 16:28:15 -0700284 }
Anthony Chen55e8e1e2016-01-08 10:31:46 -0800285
286 Drawable icon = statusBarIcon.icon.loadDrawableAsUser(context, userId);
287
288 TypedValue typedValue = new TypedValue();
289 context.getResources().getValue(R.dimen.status_bar_icon_scale_factor, typedValue, true);
290 float scaleFactor = typedValue.getFloat();
291
292 // No need to scale the icon, so return it as is.
293 if (scaleFactor == 1.f) {
294 return icon;
295 }
296
297 return new ScalingDrawableWrapper(icon, scaleFactor);
Joe Onorato0cbda992010-05-02 16:28:15 -0700298 }
Joe Onoratob77f53b2010-05-28 19:59:51 -0400299
300 public StatusBarIcon getStatusBarIcon() {
301 return mIcon;
302 }
Joe Onoratof9ec03c2010-09-23 15:09:18 -0700303
Svetoslav Ganov6179ea32011-06-28 01:12:41 -0700304 @Override
305 public void onInitializeAccessibilityEvent(AccessibilityEvent event) {
306 super.onInitializeAccessibilityEvent(event);
307 if (mNotification != null) {
308 event.setParcelableData(mNotification);
309 }
310 }
311
312 @Override
Joe Onorato6c01a112010-10-04 17:38:47 -0400313 protected void onSizeChanged(int w, int h, int oldw, int oldh) {
314 super.onSizeChanged(w, h, oldw, oldh);
315 if (mNumberBackground != null) {
316 placeNumber();
317 }
318 }
319
Svetoslav Ganov6179ea32011-06-28 01:12:41 -0700320 @Override
Jorim Jaggi66ac1332015-01-21 19:22:26 +0100321 public void onRtlPropertiesChanged(int layoutDirection) {
322 super.onRtlPropertiesChanged(layoutDirection);
323 updateDrawable();
324 }
325
326 @Override
Joe Onorato6c01a112010-10-04 17:38:47 -0400327 protected void onDraw(Canvas canvas) {
Selim Cinek49014f82016-11-04 14:55:30 -0700328 if (mIconAppearAmount > 0.0f) {
329 canvas.save();
330 canvas.scale(mIconScale * mIconAppearAmount, mIconScale * mIconAppearAmount,
331 getWidth() / 2, getHeight() / 2);
332 super.onDraw(canvas);
333 canvas.restore();
334 }
Joe Onorato6c01a112010-10-04 17:38:47 -0400335
336 if (mNumberBackground != null) {
337 mNumberBackground.draw(canvas);
338 canvas.drawText(mNumberText, mNumberX, mNumberY, mNumberPain);
339 }
Selim Cinek49014f82016-11-04 14:55:30 -0700340 if (mDotAppearAmount != 0.0f) {
341 float radius;
342 float alpha;
343 if (mDotAppearAmount <= 1.0f) {
344 radius = mDotRadius * mDotAppearAmount;
345 alpha = 1.0f;
346 } else {
347 float fadeOutAmount = mDotAppearAmount - 1.0f;
348 alpha = 1.0f - fadeOutAmount;
349 radius = NotificationUtils.interpolate(mDotRadius, getWidth() / 4, fadeOutAmount);
350 }
351 mDotPaint.setAlpha((int) (alpha * 255));
352 canvas.drawCircle(getWidth() / 2, getHeight() / 2, radius, mDotPaint);
353 }
Joe Onorato6c01a112010-10-04 17:38:47 -0400354 }
355
Svetoslav Ganov6179ea32011-06-28 01:12:41 -0700356 @Override
Joe Onoratof9ec03c2010-09-23 15:09:18 -0700357 protected void debug(int depth) {
358 super.debug(depth);
359 Log.d("View", debugIndent(depth) + "slot=" + mSlot);
360 Log.d("View", debugIndent(depth) + "icon=" + mIcon);
361 }
Joe Onorato6c01a112010-10-04 17:38:47 -0400362
363 void placeNumber() {
Daniel Sandlerebce0112011-06-16 16:44:51 -0400364 final String str;
John Spurlock01534782014-01-13 11:59:22 -0500365 final int tooBig = getContext().getResources().getInteger(
Daniel Sandlerebce0112011-06-16 16:44:51 -0400366 android.R.integer.status_bar_notification_info_maxnum);
367 if (mIcon.number > tooBig) {
John Spurlock01534782014-01-13 11:59:22 -0500368 str = getContext().getResources().getString(
Daniel Sandlerebce0112011-06-16 16:44:51 -0400369 android.R.string.status_bar_notification_info_overflow);
370 } else {
371 NumberFormat f = NumberFormat.getIntegerInstance();
372 str = f.format(mIcon.number);
373 }
374 mNumberText = str;
375
Joe Onorato6c01a112010-10-04 17:38:47 -0400376 final int w = getWidth();
377 final int h = getHeight();
378 final Rect r = new Rect();
379 mNumberPain.getTextBounds(str, 0, str.length(), r);
380 final int tw = r.right - r.left;
381 final int th = r.bottom - r.top;
382 mNumberBackground.getPadding(r);
383 int dw = r.left + tw + r.right;
384 if (dw < mNumberBackground.getMinimumWidth()) {
385 dw = mNumberBackground.getMinimumWidth();
386 }
387 mNumberX = w-r.right-((dw-r.right-r.left)/2);
388 int dh = r.top + th + r.bottom;
389 if (dh < mNumberBackground.getMinimumWidth()) {
390 dh = mNumberBackground.getMinimumWidth();
391 }
392 mNumberY = h-r.bottom-((dh-r.top-th-r.bottom)/2);
393 mNumberBackground.setBounds(w-dw, h-dh, w, h);
394 }
Svetoslav Ganov6179ea32011-06-28 01:12:41 -0700395
396 private void setContentDescription(Notification notification) {
397 if (notification != null) {
Adrian Rooseba05822016-04-22 17:09:27 -0700398 String d = contentDescForNotification(mContext, notification);
399 if (!TextUtils.isEmpty(d)) {
400 setContentDescription(d);
Svetoslav Ganov6179ea32011-06-28 01:12:41 -0700401 }
402 }
403 }
Daniel Sandler7579bca2011-08-18 15:47:26 -0400404
405 public String toString() {
John Spurlock209bede2013-07-17 12:23:27 -0400406 return "StatusBarIconView(slot=" + mSlot + " icon=" + mIcon
Daniel Sandler7579bca2011-08-18 15:47:26 -0400407 + " notification=" + mNotification + ")";
408 }
Jason Monk3b230072015-05-29 11:11:29 -0400409
410 public String getSlot() {
411 return mSlot;
412 }
Adrian Rooseba05822016-04-22 17:09:27 -0700413
414
415 public static String contentDescForNotification(Context c, Notification n) {
Adrian Roosa8e18ef2016-05-26 17:17:02 -0700416 String appName = "";
417 try {
418 Notification.Builder builder = Notification.Builder.recoverBuilder(c, n);
419 appName = builder.loadHeaderAppName();
420 } catch (RuntimeException e) {
421 Log.e(TAG, "Unable to recover builder", e);
422 // Trying to get the app name from the app info instead.
423 Parcelable appInfo = n.extras.getParcelable(
424 Notification.EXTRA_BUILDER_APPLICATION_INFO);
425 if (appInfo instanceof ApplicationInfo) {
426 appName = String.valueOf(((ApplicationInfo) appInfo).loadLabel(
427 c.getPackageManager()));
428 }
429 }
Adrian Roos51548e62016-04-28 11:20:26 -0700430
Julia Reynolds7f9ce782016-05-17 15:34:34 -0400431 CharSequence title = n.extras.getCharSequence(Notification.EXTRA_TITLE);
Adrian Rooseba05822016-04-22 17:09:27 -0700432 CharSequence ticker = n.tickerText;
Adrian Roos51548e62016-04-28 11:20:26 -0700433
434 CharSequence desc = !TextUtils.isEmpty(ticker) ? ticker
435 : !TextUtils.isEmpty(title) ? title : "";
436
437 return c.getString(R.string.accessibility_desc_notification_icon, appName, desc);
Adrian Rooseba05822016-04-22 17:09:27 -0700438 }
Adrian Roos51548e62016-04-28 11:20:26 -0700439
Selim Cinek49014f82016-11-04 14:55:30 -0700440 public void setIconTint(int iconTint) {
441 mDotPaint.setColor(iconTint);
442 }
443
Selim Cinek5b5beb012016-11-08 18:11:58 -0800444 public void setVisibleState(int state) {
445 setVisibleState(state, true /* animate */, null /* endRunnable */);
446 }
447
448 public void setVisibleState(int state, boolean animate) {
449 setVisibleState(state, animate, null);
450 }
451
452 @Override
453 public boolean hasOverlappingRendering() {
454 return false;
455 }
456
457 public void setVisibleState(int visibleState, boolean animate, Runnable endRunnable) {
Selim Cinek65d418e2016-11-29 15:42:34 -0800458 boolean runnableAdded = false;
Selim Cinek49014f82016-11-04 14:55:30 -0700459 if (visibleState != mVisibleState) {
460 mVisibleState = visibleState;
Selim Cinek5b5beb012016-11-08 18:11:58 -0800461 if (animate) {
462 if (mIconAppearAnimator != null) {
463 mIconAppearAnimator.cancel();
Selim Cinek49014f82016-11-04 14:55:30 -0700464 }
Selim Cinek5b5beb012016-11-08 18:11:58 -0800465 float targetAmount = 0.0f;
466 Interpolator interpolator = Interpolators.FAST_OUT_LINEAR_IN;
467 if (visibleState == STATE_ICON) {
468 targetAmount = 1.0f;
469 interpolator = Interpolators.LINEAR_OUT_SLOW_IN;
470 }
Selim Cinek65d418e2016-11-29 15:42:34 -0800471 float currentAmount = getIconAppearAmount();
472 if (targetAmount != currentAmount) {
473 mIconAppearAnimator = ObjectAnimator.ofFloat(this, ICON_APPEAR_AMOUNT,
474 currentAmount, targetAmount);
475 mIconAppearAnimator.setInterpolator(interpolator);
476 mIconAppearAnimator.setDuration(100);
477 mIconAppearAnimator.addListener(new AnimatorListenerAdapter() {
478 @Override
479 public void onAnimationEnd(Animator animation) {
480 mIconAppearAnimator = null;
481 runRunnable(endRunnable);
Selim Cinek5b5beb012016-11-08 18:11:58 -0800482 }
Selim Cinek65d418e2016-11-29 15:42:34 -0800483 });
484 mIconAppearAnimator.start();
485 runnableAdded = true;
486 }
Selim Cinek49014f82016-11-04 14:55:30 -0700487
Selim Cinek5b5beb012016-11-08 18:11:58 -0800488 if (mDotAnimator != null) {
489 mDotAnimator.cancel();
Selim Cinek49014f82016-11-04 14:55:30 -0700490 }
Selim Cinek5b5beb012016-11-08 18:11:58 -0800491 targetAmount = visibleState == STATE_ICON ? 2.0f : 0.0f;
492 interpolator = Interpolators.FAST_OUT_LINEAR_IN;
493 if (visibleState == STATE_DOT) {
494 targetAmount = 1.0f;
495 interpolator = Interpolators.LINEAR_OUT_SLOW_IN;
496 }
Selim Cinek65d418e2016-11-29 15:42:34 -0800497 currentAmount = getDotAppearAmount();
498 if (targetAmount != currentAmount) {
499 mDotAnimator = ObjectAnimator.ofFloat(this, DOT_APPEAR_AMOUNT,
500 currentAmount, targetAmount);
501 mDotAnimator.setInterpolator(interpolator);
502 mDotAnimator.setDuration(100);
503 final boolean runRunnable = !runnableAdded;
504 mDotAnimator.addListener(new AnimatorListenerAdapter() {
505 @Override
506 public void onAnimationEnd(Animator animation) {
507 mDotAnimator = null;
508 if (runRunnable) {
509 runRunnable(endRunnable);
510 }
511 }
512 });
513 mDotAnimator.start();
514 runnableAdded = true;
515 }
Selim Cinek5b5beb012016-11-08 18:11:58 -0800516 } else {
517 setIconAppearAmount(visibleState == STATE_ICON ? 1.0f : 0.0f);
Selim Cinek01a73f92016-12-06 16:13:42 -0800518 setDotAppearAmount(visibleState == STATE_DOT ? 1.0f
519 : visibleState == STATE_ICON ? 2.0f
520 : 0.0f);
Selim Cinek5b5beb012016-11-08 18:11:58 -0800521 }
Selim Cinek49014f82016-11-04 14:55:30 -0700522 }
Selim Cinek65d418e2016-11-29 15:42:34 -0800523 if (!runnableAdded) {
524 runRunnable(endRunnable);
525 }
526 }
527
528 private void runRunnable(Runnable runnable) {
529 if (runnable != null) {
530 runnable.run();
531 }
Selim Cinek49014f82016-11-04 14:55:30 -0700532 }
533
Selim Cinek5b5beb012016-11-08 18:11:58 -0800534 public void setIconAppearAmount(float iconAppearAmount) {
Selim Cinek49014f82016-11-04 14:55:30 -0700535 mIconAppearAmount = iconAppearAmount;
536 invalidate();
537 }
538
539 public float getIconAppearAmount() {
540 return mIconAppearAmount;
541 }
542
543 public int getVisibleState() {
544 return mVisibleState;
545 }
546
547 public void setDotAppearAmount(float dotAppearAmount) {
548 mDotAppearAmount = dotAppearAmount;
549 invalidate();
550 }
551
Selim Cinek2b549f42016-11-22 16:38:51 -0800552 @Override
553 public void setVisibility(int visibility) {
554 super.setVisibility(visibility);
555 if (mOnVisibilityChangedListener != null) {
556 mOnVisibilityChangedListener.onVisibilityChanged(visibility);
557 }
558 }
559
Selim Cinek49014f82016-11-04 14:55:30 -0700560 public float getDotAppearAmount() {
561 return mDotAppearAmount;
562 }
Selim Cinek2b549f42016-11-22 16:38:51 -0800563
564 public void setOnVisibilityChangedListener(OnVisibilityChangedListener listener) {
565 mOnVisibilityChangedListener = listener;
566 }
567
568 public interface OnVisibilityChangedListener {
569 void onVisibilityChanged(int newVisibility);
570 }
Joe Onorato0cbda992010-05-02 16:28:15 -0700571}