Joe Onorato | 0cbda99 | 2010-05-02 16:28:15 -0700 | [diff] [blame] | 1 | /* |
| 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 Onorato | 79de0c5 | 2010-05-26 17:03:26 -0400 | [diff] [blame] | 17 | package com.android.systemui.statusbar; |
Joe Onorato | 0cbda99 | 2010-05-02 16:28:15 -0700 | [diff] [blame] | 18 | |
Selim Cinek | 49014f8 | 2016-11-04 14:55:30 -0700 | [diff] [blame] | 19 | import android.animation.Animator; |
| 20 | import android.animation.AnimatorListenerAdapter; |
| 21 | import android.animation.ObjectAnimator; |
Selim Cinek | 875ba9b | 2017-02-13 16:20:17 -0800 | [diff] [blame] | 22 | import android.animation.ValueAnimator; |
Svetoslav Ganov | 6179ea3 | 2011-06-28 01:12:41 -0700 | [diff] [blame] | 23 | import android.app.Notification; |
Joe Onorato | 0cbda99 | 2010-05-02 16:28:15 -0700 | [diff] [blame] | 24 | import android.content.Context; |
Adrian Roos | a8e18ef | 2016-05-26 17:17:02 -0700 | [diff] [blame] | 25 | import android.content.pm.ApplicationInfo; |
Selim Cinek | 875ba9b | 2017-02-13 16:20:17 -0800 | [diff] [blame] | 26 | import android.content.res.ColorStateList; |
Selim Cinek | 3e7592d | 2016-04-11 09:35:54 +0800 | [diff] [blame] | 27 | import android.content.res.Configuration; |
Joe Onorato | 0cbda99 | 2010-05-02 16:28:15 -0700 | [diff] [blame] | 28 | import android.content.res.Resources; |
Joe Onorato | 0cbda99 | 2010-05-02 16:28:15 -0700 | [diff] [blame] | 29 | import android.graphics.Canvas; |
Joe Onorato | 6c01a11 | 2010-10-04 17:38:47 -0400 | [diff] [blame] | 30 | import android.graphics.Paint; |
| 31 | import android.graphics.Rect; |
John Spurlock | de84f0e | 2013-06-12 12:41:00 -0400 | [diff] [blame] | 32 | import android.graphics.drawable.Drawable; |
Dan Sandler | d63f932 | 2015-05-06 15:18:49 -0400 | [diff] [blame] | 33 | import android.graphics.drawable.Icon; |
Adrian Roos | a8e18ef | 2016-05-26 17:17:02 -0700 | [diff] [blame] | 34 | import android.os.Parcelable; |
Jeff Sharkey | ded653b | 2012-09-27 19:09:24 -0700 | [diff] [blame] | 35 | import android.os.UserHandle; |
Svetoslav Ganov | 6179ea3 | 2011-06-28 01:12:41 -0700 | [diff] [blame] | 36 | import android.text.TextUtils; |
Daniel Sandler | 05e2414 | 2011-11-10 11:56:49 -0500 | [diff] [blame] | 37 | import android.util.AttributeSet; |
Selim Cinek | 49014f8 | 2016-11-04 14:55:30 -0700 | [diff] [blame] | 38 | import android.util.FloatProperty; |
Joe Onorato | f9ec03c | 2010-09-23 15:09:18 -0700 | [diff] [blame] | 39 | import android.util.Log; |
Selim Cinek | 49014f8 | 2016-11-04 14:55:30 -0700 | [diff] [blame] | 40 | import android.util.Property; |
Anthony Chen | 55e8e1e | 2016-01-08 10:31:46 -0800 | [diff] [blame] | 41 | import android.util.TypedValue; |
Joe Onorato | 0cbda99 | 2010-05-02 16:28:15 -0700 | [diff] [blame] | 42 | import android.view.ViewDebug; |
Svetoslav Ganov | 6179ea3 | 2011-06-28 01:12:41 -0700 | [diff] [blame] | 43 | import android.view.accessibility.AccessibilityEvent; |
Selim Cinek | 49014f8 | 2016-11-04 14:55:30 -0700 | [diff] [blame] | 44 | import android.view.animation.Interpolator; |
Winson | c0d7058 | 2016-01-29 10:24:39 -0800 | [diff] [blame] | 45 | |
Joe Onorato | 0cbda99 | 2010-05-02 16:28:15 -0700 | [diff] [blame] | 46 | import com.android.internal.statusbar.StatusBarIcon; |
Selim Cinek | 49014f8 | 2016-11-04 14:55:30 -0700 | [diff] [blame] | 47 | import com.android.systemui.Interpolators; |
Joe Onorato | 6c01a11 | 2010-10-04 17:38:47 -0400 | [diff] [blame] | 48 | import com.android.systemui.R; |
Selim Cinek | 49014f8 | 2016-11-04 14:55:30 -0700 | [diff] [blame] | 49 | import com.android.systemui.statusbar.notification.NotificationUtils; |
Joe Onorato | 6c01a11 | 2010-10-04 17:38:47 -0400 | [diff] [blame] | 50 | |
John Spurlock | de84f0e | 2013-06-12 12:41:00 -0400 | [diff] [blame] | 51 | import java.text.NumberFormat; |
| 52 | |
Joe Onorato | 0cbda99 | 2010-05-02 16:28:15 -0700 | [diff] [blame] | 53 | public class StatusBarIconView extends AnimatedImageView { |
Selim Cinek | 875ba9b | 2017-02-13 16:20:17 -0800 | [diff] [blame] | 54 | public static final int NO_COLOR = 0; |
| 55 | private final int ANIMATION_DURATION_FAST = 100; |
| 56 | |
Selim Cinek | 49014f8 | 2016-11-04 14:55:30 -0700 | [diff] [blame] | 57 | public static final int STATE_ICON = 0; |
| 58 | public static final int STATE_DOT = 1; |
| 59 | public static final int STATE_HIDDEN = 2; |
Joe Onorato | 0cbda99 | 2010-05-02 16:28:15 -0700 | [diff] [blame] | 60 | |
Selim Cinek | 49014f8 | 2016-11-04 14:55:30 -0700 | [diff] [blame] | 61 | private static final String TAG = "StatusBarIconView"; |
| 62 | private static final Property<StatusBarIconView, Float> ICON_APPEAR_AMOUNT |
| 63 | = new FloatProperty<StatusBarIconView>("iconAppearAmount") { |
| 64 | |
| 65 | @Override |
| 66 | public void setValue(StatusBarIconView object, float value) { |
| 67 | object.setIconAppearAmount(value); |
| 68 | } |
| 69 | |
| 70 | @Override |
| 71 | public Float get(StatusBarIconView object) { |
| 72 | return object.getIconAppearAmount(); |
| 73 | } |
| 74 | }; |
Selim Cinek | 5b5beb01 | 2016-11-08 18:11:58 -0800 | [diff] [blame] | 75 | private static final Property<StatusBarIconView, Float> DOT_APPEAR_AMOUNT |
Selim Cinek | 49014f8 | 2016-11-04 14:55:30 -0700 | [diff] [blame] | 76 | = new FloatProperty<StatusBarIconView>("dot_appear_amount") { |
| 77 | |
| 78 | @Override |
| 79 | public void setValue(StatusBarIconView object, float value) { |
| 80 | object.setDotAppearAmount(value); |
| 81 | } |
| 82 | |
| 83 | @Override |
| 84 | public Float get(StatusBarIconView object) { |
| 85 | return object.getDotAppearAmount(); |
| 86 | } |
| 87 | }; |
| 88 | |
| 89 | private boolean mAlwaysScaleIcon; |
Joe Onorato | 0cbda99 | 2010-05-02 16:28:15 -0700 | [diff] [blame] | 90 | private StatusBarIcon mIcon; |
| 91 | @ViewDebug.ExportedProperty private String mSlot; |
Joe Onorato | 6c01a11 | 2010-10-04 17:38:47 -0400 | [diff] [blame] | 92 | private Drawable mNumberBackground; |
| 93 | private Paint mNumberPain; |
| 94 | private int mNumberX; |
| 95 | private int mNumberY; |
| 96 | private String mNumberText; |
Svetoslav Ganov | 6179ea3 | 2011-06-28 01:12:41 -0700 | [diff] [blame] | 97 | private Notification mNotification; |
Jason Monk | 3b23007 | 2015-05-29 11:11:29 -0400 | [diff] [blame] | 98 | private final boolean mBlocked; |
Selim Cinek | 3e7592d | 2016-04-11 09:35:54 +0800 | [diff] [blame] | 99 | private int mDensity; |
Selim Cinek | 281c202 | 2016-10-13 19:14:43 -0700 | [diff] [blame] | 100 | private float mIconScale = 1.0f; |
Selim Cinek | 49014f8 | 2016-11-04 14:55:30 -0700 | [diff] [blame] | 101 | private final Paint mDotPaint = new Paint(); |
Selim Cinek | 30d9bad7 | 2017-03-20 17:43:50 +0000 | [diff] [blame^] | 102 | private boolean mDotVisible; |
Selim Cinek | 49014f8 | 2016-11-04 14:55:30 -0700 | [diff] [blame] | 103 | private float mDotRadius; |
| 104 | private int mStaticDotRadius; |
| 105 | private int mVisibleState = STATE_ICON; |
| 106 | private float mIconAppearAmount = 1.0f; |
| 107 | private ObjectAnimator mIconAppearAnimator; |
| 108 | private ObjectAnimator mDotAnimator; |
| 109 | private float mDotAppearAmount; |
Selim Cinek | 2b549f4 | 2016-11-22 16:38:51 -0800 | [diff] [blame] | 110 | private OnVisibilityChangedListener mOnVisibilityChangedListener; |
Selim Cinek | 875ba9b | 2017-02-13 16:20:17 -0800 | [diff] [blame] | 111 | private int mDrawableColor; |
| 112 | private int mIconColor; |
| 113 | private ValueAnimator mColorAnimator; |
| 114 | private int mCurrentSetColor = NO_COLOR; |
| 115 | private int mAnimationStartColor = NO_COLOR; |
| 116 | private final ValueAnimator.AnimatorUpdateListener mColorUpdater |
| 117 | = animation -> { |
| 118 | int newColor = NotificationUtils.interpolateColors(mAnimationStartColor, mIconColor, |
| 119 | animation.getAnimatedFraction()); |
| 120 | setColorInternal(newColor); |
| 121 | }; |
Joe Onorato | 0cbda99 | 2010-05-02 16:28:15 -0700 | [diff] [blame] | 122 | |
John Spurlock | e6f0a71 | 2013-09-03 16:23:49 -0400 | [diff] [blame] | 123 | public StatusBarIconView(Context context, String slot, Notification notification) { |
Jason Monk | 3b23007 | 2015-05-29 11:11:29 -0400 | [diff] [blame] | 124 | this(context, slot, notification, false); |
| 125 | } |
| 126 | |
| 127 | public StatusBarIconView(Context context, String slot, Notification notification, |
| 128 | boolean blocked) { |
Joe Onorato | 0cbda99 | 2010-05-02 16:28:15 -0700 | [diff] [blame] | 129 | super(context); |
Jason Monk | 3b23007 | 2015-05-29 11:11:29 -0400 | [diff] [blame] | 130 | mBlocked = blocked; |
Joe Onorato | 0cbda99 | 2010-05-02 16:28:15 -0700 | [diff] [blame] | 131 | mSlot = slot; |
Joe Onorato | 6c01a11 | 2010-10-04 17:38:47 -0400 | [diff] [blame] | 132 | mNumberPain = new Paint(); |
| 133 | mNumberPain.setTextAlign(Paint.Align.CENTER); |
Alan Viverette | 4a357cd | 2015-03-18 18:37:18 -0700 | [diff] [blame] | 134 | mNumberPain.setColor(context.getColor(R.drawable.notification_number_text_color)); |
Joe Onorato | 6c01a11 | 2010-10-04 17:38:47 -0400 | [diff] [blame] | 135 | mNumberPain.setAntiAlias(true); |
Christoph Studer | a0506e7 | 2014-07-31 20:27:39 +0200 | [diff] [blame] | 136 | setNotification(notification); |
Selim Cinek | 3e7592d | 2016-04-11 09:35:54 +0800 | [diff] [blame] | 137 | maybeUpdateIconScale(); |
| 138 | setScaleType(ScaleType.CENTER); |
| 139 | mDensity = context.getResources().getDisplayMetrics().densityDpi; |
Selim Cinek | 49014f8 | 2016-11-04 14:55:30 -0700 | [diff] [blame] | 140 | if (mNotification != null) { |
Selim Cinek | 875ba9b | 2017-02-13 16:20:17 -0800 | [diff] [blame] | 141 | setDecorColor(getContext().getColor( |
Selim Cinek | 49014f8 | 2016-11-04 14:55:30 -0700 | [diff] [blame] | 142 | com.android.internal.R.color.notification_icon_default_color)); |
| 143 | } |
| 144 | reloadDimens(); |
Selim Cinek | 3e7592d | 2016-04-11 09:35:54 +0800 | [diff] [blame] | 145 | } |
Daniel Sandler | 26c84b1 | 2011-07-27 00:09:40 -0400 | [diff] [blame] | 146 | |
Selim Cinek | 3e7592d | 2016-04-11 09:35:54 +0800 | [diff] [blame] | 147 | private void maybeUpdateIconScale() { |
Daniel Sandler | 7579bca | 2011-08-18 15:47:26 -0400 | [diff] [blame] | 148 | // We do not resize and scale system icons (on the right), only notification icons (on the |
| 149 | // left). |
Selim Cinek | 3e7592d | 2016-04-11 09:35:54 +0800 | [diff] [blame] | 150 | if (mNotification != null || mAlwaysScaleIcon) { |
| 151 | updateIconScale(); |
Daniel Sandler | 7579bca | 2011-08-18 15:47:26 -0400 | [diff] [blame] | 152 | } |
Selim Cinek | 3e7592d | 2016-04-11 09:35:54 +0800 | [diff] [blame] | 153 | } |
Daniel Sandler | abff032 | 2011-09-27 11:19:34 -0400 | [diff] [blame] | 154 | |
Selim Cinek | 3e7592d | 2016-04-11 09:35:54 +0800 | [diff] [blame] | 155 | private void updateIconScale() { |
| 156 | Resources res = mContext.getResources(); |
| 157 | final int outerBounds = res.getDimensionPixelSize(R.dimen.status_bar_icon_size); |
| 158 | final int imageBounds = res.getDimensionPixelSize(R.dimen.status_bar_icon_drawing_size); |
Selim Cinek | 281c202 | 2016-10-13 19:14:43 -0700 | [diff] [blame] | 159 | mIconScale = (float)imageBounds / (float)outerBounds; |
Selim Cinek | 281c202 | 2016-10-13 19:14:43 -0700 | [diff] [blame] | 160 | } |
| 161 | |
| 162 | public float getIconScale() { |
| 163 | return mIconScale; |
Selim Cinek | 3e7592d | 2016-04-11 09:35:54 +0800 | [diff] [blame] | 164 | } |
| 165 | |
| 166 | @Override |
| 167 | protected void onConfigurationChanged(Configuration newConfig) { |
| 168 | super.onConfigurationChanged(newConfig); |
| 169 | int density = newConfig.densityDpi; |
| 170 | if (density != mDensity) { |
| 171 | mDensity = density; |
| 172 | maybeUpdateIconScale(); |
| 173 | updateDrawable(); |
Selim Cinek | 49014f8 | 2016-11-04 14:55:30 -0700 | [diff] [blame] | 174 | reloadDimens(); |
| 175 | } |
| 176 | } |
| 177 | |
| 178 | private void reloadDimens() { |
| 179 | boolean applyRadius = mDotRadius == mStaticDotRadius; |
| 180 | mStaticDotRadius = getResources().getDimensionPixelSize(R.dimen.overflow_dot_radius); |
| 181 | if (applyRadius) { |
| 182 | mDotRadius = mStaticDotRadius; |
Selim Cinek | 3e7592d | 2016-04-11 09:35:54 +0800 | [diff] [blame] | 183 | } |
Joe Onorato | 0cbda99 | 2010-05-02 16:28:15 -0700 | [diff] [blame] | 184 | } |
| 185 | |
Christoph Studer | a0506e7 | 2014-07-31 20:27:39 +0200 | [diff] [blame] | 186 | public void setNotification(Notification notification) { |
| 187 | mNotification = notification; |
| 188 | setContentDescription(notification); |
| 189 | } |
| 190 | |
Daniel Sandler | 05e2414 | 2011-11-10 11:56:49 -0500 | [diff] [blame] | 191 | public StatusBarIconView(Context context, AttributeSet attrs) { |
| 192 | super(context, attrs); |
Jason Monk | 3b23007 | 2015-05-29 11:11:29 -0400 | [diff] [blame] | 193 | mBlocked = false; |
Selim Cinek | 3e7592d | 2016-04-11 09:35:54 +0800 | [diff] [blame] | 194 | mAlwaysScaleIcon = true; |
| 195 | updateIconScale(); |
| 196 | mDensity = context.getResources().getDisplayMetrics().densityDpi; |
Daniel Sandler | 05e2414 | 2011-11-10 11:56:49 -0500 | [diff] [blame] | 197 | } |
| 198 | |
Joe Onorato | 0cbda99 | 2010-05-02 16:28:15 -0700 | [diff] [blame] | 199 | private static boolean streq(String a, String b) { |
Joe Onorato | 66d7d01 | 2010-05-14 10:05:10 -0700 | [diff] [blame] | 200 | if (a == b) { |
| 201 | return true; |
| 202 | } |
Joe Onorato | 0cbda99 | 2010-05-02 16:28:15 -0700 | [diff] [blame] | 203 | if (a == null && b != null) { |
| 204 | return false; |
| 205 | } |
| 206 | if (a != null && b == null) { |
| 207 | return false; |
| 208 | } |
| 209 | return a.equals(b); |
| 210 | } |
| 211 | |
Dan Sandler | d63f932 | 2015-05-06 15:18:49 -0400 | [diff] [blame] | 212 | public boolean equalIcons(Icon a, Icon b) { |
| 213 | if (a == b) return true; |
| 214 | if (a.getType() != b.getType()) return false; |
| 215 | switch (a.getType()) { |
| 216 | case Icon.TYPE_RESOURCE: |
| 217 | return a.getResPackage().equals(b.getResPackage()) && a.getResId() == b.getResId(); |
| 218 | case Icon.TYPE_URI: |
| 219 | return a.getUriString().equals(b.getUriString()); |
| 220 | default: |
| 221 | return false; |
| 222 | } |
| 223 | } |
Joe Onorato | 005847b | 2010-06-04 16:08:02 -0400 | [diff] [blame] | 224 | /** |
| 225 | * Returns whether the set succeeded. |
| 226 | */ |
| 227 | public boolean set(StatusBarIcon icon) { |
Dan Sandler | d63f932 | 2015-05-06 15:18:49 -0400 | [diff] [blame] | 228 | final boolean iconEquals = mIcon != null && equalIcons(mIcon.icon, icon.icon); |
Joe Onorato | 005847b | 2010-06-04 16:08:02 -0400 | [diff] [blame] | 229 | final boolean levelEquals = iconEquals |
| 230 | && mIcon.iconLevel == icon.iconLevel; |
| 231 | final boolean visibilityEquals = mIcon != null |
| 232 | && mIcon.visible == icon.visible; |
Joe Onorato | 6c01a11 | 2010-10-04 17:38:47 -0400 | [diff] [blame] | 233 | final boolean numberEquals = mIcon != null |
| 234 | && mIcon.number == icon.number; |
| 235 | mIcon = icon.clone(); |
Svetoslav Ganov | 6179ea3 | 2011-06-28 01:12:41 -0700 | [diff] [blame] | 236 | setContentDescription(icon.contentDescription); |
Joe Onorato | 005847b | 2010-06-04 16:08:02 -0400 | [diff] [blame] | 237 | if (!iconEquals) { |
Fabrice Di Meglio | 82fca5d | 2013-01-09 15:42:31 -0800 | [diff] [blame] | 238 | if (!updateDrawable(false /* no clear */)) return false; |
Selim Cinek | 2b6eb803 | 2016-12-29 14:22:21 +0100 | [diff] [blame] | 239 | // we have to clear the grayscale tag since it may have changed |
| 240 | setTag(R.id.icon_is_grayscale, null); |
Joe Onorato | 0cbda99 | 2010-05-02 16:28:15 -0700 | [diff] [blame] | 241 | } |
Joe Onorato | 005847b | 2010-06-04 16:08:02 -0400 | [diff] [blame] | 242 | if (!levelEquals) { |
| 243 | setImageLevel(icon.iconLevel); |
Joe Onorato | 0cbda99 | 2010-05-02 16:28:15 -0700 | [diff] [blame] | 244 | } |
Daniel Sandler | 26c84b1 | 2011-07-27 00:09:40 -0400 | [diff] [blame] | 245 | |
Joe Onorato | 6c01a11 | 2010-10-04 17:38:47 -0400 | [diff] [blame] | 246 | if (!numberEquals) { |
John Spurlock | 0153478 | 2014-01-13 11:59:22 -0500 | [diff] [blame] | 247 | if (icon.number > 0 && getContext().getResources().getBoolean( |
Joe Onorato | 8595a3d | 2010-11-19 18:12:07 -0800 | [diff] [blame] | 248 | R.bool.config_statusBarShowNumber)) { |
Joe Onorato | 6c01a11 | 2010-10-04 17:38:47 -0400 | [diff] [blame] | 249 | if (mNumberBackground == null) { |
| 250 | mNumberBackground = getContext().getResources().getDrawable( |
| 251 | R.drawable.ic_notification_overlay); |
| 252 | } |
| 253 | placeNumber(); |
| 254 | } else { |
| 255 | mNumberBackground = null; |
| 256 | mNumberText = null; |
| 257 | } |
| 258 | invalidate(); |
| 259 | } |
Joe Onorato | 005847b | 2010-06-04 16:08:02 -0400 | [diff] [blame] | 260 | if (!visibilityEquals) { |
Jason Monk | 3b23007 | 2015-05-29 11:11:29 -0400 | [diff] [blame] | 261 | setVisibility(icon.visible && !mBlocked ? VISIBLE : GONE); |
Joe Onorato | 005847b | 2010-06-04 16:08:02 -0400 | [diff] [blame] | 262 | } |
Joe Onorato | 005847b | 2010-06-04 16:08:02 -0400 | [diff] [blame] | 263 | return true; |
Joe Onorato | 0cbda99 | 2010-05-02 16:28:15 -0700 | [diff] [blame] | 264 | } |
| 265 | |
Fabrice Di Meglio | 82fca5d | 2013-01-09 15:42:31 -0800 | [diff] [blame] | 266 | public void updateDrawable() { |
| 267 | updateDrawable(true /* with clear */); |
| 268 | } |
| 269 | |
| 270 | private boolean updateDrawable(boolean withClear) { |
Jorim Jaggi | 66ac133 | 2015-01-21 19:22:26 +0100 | [diff] [blame] | 271 | if (mIcon == null) { |
| 272 | return false; |
| 273 | } |
Adrian Roos | fb2d0cc | 2017-01-31 15:10:00 -0800 | [diff] [blame] | 274 | Drawable drawable; |
| 275 | try { |
| 276 | drawable = getIcon(mIcon); |
| 277 | } catch (OutOfMemoryError e) { |
| 278 | Log.w(TAG, "OOM while inflating " + mIcon.icon + " for slot " + mSlot); |
| 279 | return false; |
| 280 | } |
| 281 | |
Fabrice Di Meglio | 82fca5d | 2013-01-09 15:42:31 -0800 | [diff] [blame] | 282 | if (drawable == null) { |
Adrian Roos | fb2d0cc | 2017-01-31 15:10:00 -0800 | [diff] [blame] | 283 | Log.w(TAG, "No icon for slot " + mSlot + "; " + mIcon.icon); |
Fabrice Di Meglio | 82fca5d | 2013-01-09 15:42:31 -0800 | [diff] [blame] | 284 | return false; |
| 285 | } |
| 286 | if (withClear) { |
| 287 | setImageDrawable(null); |
| 288 | } |
| 289 | setImageDrawable(drawable); |
| 290 | return true; |
| 291 | } |
| 292 | |
Joe Onorato | f551054 | 2010-06-01 07:46:41 -0700 | [diff] [blame] | 293 | private Drawable getIcon(StatusBarIcon icon) { |
| 294 | return getIcon(getContext(), icon); |
| 295 | } |
| 296 | |
Joe Onorato | 0cbda99 | 2010-05-02 16:28:15 -0700 | [diff] [blame] | 297 | /** |
Dan Sandler | d63f932 | 2015-05-06 15:18:49 -0400 | [diff] [blame] | 298 | * Returns the right icon to use for this item |
John Spurlock | 209bede | 2013-07-17 12:23:27 -0400 | [diff] [blame] | 299 | * |
Dan Sandler | d63f932 | 2015-05-06 15:18:49 -0400 | [diff] [blame] | 300 | * @param context Context to use to get resources |
Joe Onorato | 0cbda99 | 2010-05-02 16:28:15 -0700 | [diff] [blame] | 301 | * @return Drawable for this item, or null if the package or item could not |
| 302 | * be found |
| 303 | */ |
Anthony Chen | 55e8e1e | 2016-01-08 10:31:46 -0800 | [diff] [blame] | 304 | public static Drawable getIcon(Context context, StatusBarIcon statusBarIcon) { |
| 305 | int userId = statusBarIcon.user.getIdentifier(); |
Dan Sandler | d63f932 | 2015-05-06 15:18:49 -0400 | [diff] [blame] | 306 | if (userId == UserHandle.USER_ALL) { |
Xiaohui Chen | 87d0e25 | 2015-07-30 15:38:16 -0700 | [diff] [blame] | 307 | userId = UserHandle.USER_SYSTEM; |
Joe Onorato | 0cbda99 | 2010-05-02 16:28:15 -0700 | [diff] [blame] | 308 | } |
Anthony Chen | 55e8e1e | 2016-01-08 10:31:46 -0800 | [diff] [blame] | 309 | |
| 310 | Drawable icon = statusBarIcon.icon.loadDrawableAsUser(context, userId); |
| 311 | |
| 312 | TypedValue typedValue = new TypedValue(); |
| 313 | context.getResources().getValue(R.dimen.status_bar_icon_scale_factor, typedValue, true); |
| 314 | float scaleFactor = typedValue.getFloat(); |
| 315 | |
| 316 | // No need to scale the icon, so return it as is. |
| 317 | if (scaleFactor == 1.f) { |
| 318 | return icon; |
| 319 | } |
| 320 | |
| 321 | return new ScalingDrawableWrapper(icon, scaleFactor); |
Joe Onorato | 0cbda99 | 2010-05-02 16:28:15 -0700 | [diff] [blame] | 322 | } |
Joe Onorato | b77f53b | 2010-05-28 19:59:51 -0400 | [diff] [blame] | 323 | |
| 324 | public StatusBarIcon getStatusBarIcon() { |
| 325 | return mIcon; |
| 326 | } |
Joe Onorato | f9ec03c | 2010-09-23 15:09:18 -0700 | [diff] [blame] | 327 | |
Svetoslav Ganov | 6179ea3 | 2011-06-28 01:12:41 -0700 | [diff] [blame] | 328 | @Override |
| 329 | public void onInitializeAccessibilityEvent(AccessibilityEvent event) { |
| 330 | super.onInitializeAccessibilityEvent(event); |
| 331 | if (mNotification != null) { |
| 332 | event.setParcelableData(mNotification); |
| 333 | } |
| 334 | } |
| 335 | |
| 336 | @Override |
Joe Onorato | 6c01a11 | 2010-10-04 17:38:47 -0400 | [diff] [blame] | 337 | protected void onSizeChanged(int w, int h, int oldw, int oldh) { |
| 338 | super.onSizeChanged(w, h, oldw, oldh); |
| 339 | if (mNumberBackground != null) { |
| 340 | placeNumber(); |
| 341 | } |
| 342 | } |
| 343 | |
Svetoslav Ganov | 6179ea3 | 2011-06-28 01:12:41 -0700 | [diff] [blame] | 344 | @Override |
Jorim Jaggi | 66ac133 | 2015-01-21 19:22:26 +0100 | [diff] [blame] | 345 | public void onRtlPropertiesChanged(int layoutDirection) { |
| 346 | super.onRtlPropertiesChanged(layoutDirection); |
| 347 | updateDrawable(); |
| 348 | } |
| 349 | |
| 350 | @Override |
Joe Onorato | 6c01a11 | 2010-10-04 17:38:47 -0400 | [diff] [blame] | 351 | protected void onDraw(Canvas canvas) { |
Selim Cinek | 49014f8 | 2016-11-04 14:55:30 -0700 | [diff] [blame] | 352 | if (mIconAppearAmount > 0.0f) { |
| 353 | canvas.save(); |
| 354 | canvas.scale(mIconScale * mIconAppearAmount, mIconScale * mIconAppearAmount, |
| 355 | getWidth() / 2, getHeight() / 2); |
| 356 | super.onDraw(canvas); |
| 357 | canvas.restore(); |
| 358 | } |
Joe Onorato | 6c01a11 | 2010-10-04 17:38:47 -0400 | [diff] [blame] | 359 | |
| 360 | if (mNumberBackground != null) { |
| 361 | mNumberBackground.draw(canvas); |
| 362 | canvas.drawText(mNumberText, mNumberX, mNumberY, mNumberPain); |
| 363 | } |
Selim Cinek | 49014f8 | 2016-11-04 14:55:30 -0700 | [diff] [blame] | 364 | if (mDotAppearAmount != 0.0f) { |
| 365 | float radius; |
| 366 | float alpha; |
| 367 | if (mDotAppearAmount <= 1.0f) { |
| 368 | radius = mDotRadius * mDotAppearAmount; |
| 369 | alpha = 1.0f; |
| 370 | } else { |
| 371 | float fadeOutAmount = mDotAppearAmount - 1.0f; |
| 372 | alpha = 1.0f - fadeOutAmount; |
| 373 | radius = NotificationUtils.interpolate(mDotRadius, getWidth() / 4, fadeOutAmount); |
| 374 | } |
| 375 | mDotPaint.setAlpha((int) (alpha * 255)); |
| 376 | canvas.drawCircle(getWidth() / 2, getHeight() / 2, radius, mDotPaint); |
| 377 | } |
Joe Onorato | 6c01a11 | 2010-10-04 17:38:47 -0400 | [diff] [blame] | 378 | } |
| 379 | |
Svetoslav Ganov | 6179ea3 | 2011-06-28 01:12:41 -0700 | [diff] [blame] | 380 | @Override |
Joe Onorato | f9ec03c | 2010-09-23 15:09:18 -0700 | [diff] [blame] | 381 | protected void debug(int depth) { |
| 382 | super.debug(depth); |
| 383 | Log.d("View", debugIndent(depth) + "slot=" + mSlot); |
| 384 | Log.d("View", debugIndent(depth) + "icon=" + mIcon); |
| 385 | } |
Joe Onorato | 6c01a11 | 2010-10-04 17:38:47 -0400 | [diff] [blame] | 386 | |
| 387 | void placeNumber() { |
Daniel Sandler | ebce011 | 2011-06-16 16:44:51 -0400 | [diff] [blame] | 388 | final String str; |
John Spurlock | 0153478 | 2014-01-13 11:59:22 -0500 | [diff] [blame] | 389 | final int tooBig = getContext().getResources().getInteger( |
Daniel Sandler | ebce011 | 2011-06-16 16:44:51 -0400 | [diff] [blame] | 390 | android.R.integer.status_bar_notification_info_maxnum); |
| 391 | if (mIcon.number > tooBig) { |
John Spurlock | 0153478 | 2014-01-13 11:59:22 -0500 | [diff] [blame] | 392 | str = getContext().getResources().getString( |
Daniel Sandler | ebce011 | 2011-06-16 16:44:51 -0400 | [diff] [blame] | 393 | android.R.string.status_bar_notification_info_overflow); |
| 394 | } else { |
| 395 | NumberFormat f = NumberFormat.getIntegerInstance(); |
| 396 | str = f.format(mIcon.number); |
| 397 | } |
| 398 | mNumberText = str; |
| 399 | |
Joe Onorato | 6c01a11 | 2010-10-04 17:38:47 -0400 | [diff] [blame] | 400 | final int w = getWidth(); |
| 401 | final int h = getHeight(); |
| 402 | final Rect r = new Rect(); |
| 403 | mNumberPain.getTextBounds(str, 0, str.length(), r); |
| 404 | final int tw = r.right - r.left; |
| 405 | final int th = r.bottom - r.top; |
| 406 | mNumberBackground.getPadding(r); |
| 407 | int dw = r.left + tw + r.right; |
| 408 | if (dw < mNumberBackground.getMinimumWidth()) { |
| 409 | dw = mNumberBackground.getMinimumWidth(); |
| 410 | } |
| 411 | mNumberX = w-r.right-((dw-r.right-r.left)/2); |
| 412 | int dh = r.top + th + r.bottom; |
| 413 | if (dh < mNumberBackground.getMinimumWidth()) { |
| 414 | dh = mNumberBackground.getMinimumWidth(); |
| 415 | } |
| 416 | mNumberY = h-r.bottom-((dh-r.top-th-r.bottom)/2); |
| 417 | mNumberBackground.setBounds(w-dw, h-dh, w, h); |
| 418 | } |
Svetoslav Ganov | 6179ea3 | 2011-06-28 01:12:41 -0700 | [diff] [blame] | 419 | |
| 420 | private void setContentDescription(Notification notification) { |
| 421 | if (notification != null) { |
Adrian Roos | eba0582 | 2016-04-22 17:09:27 -0700 | [diff] [blame] | 422 | String d = contentDescForNotification(mContext, notification); |
| 423 | if (!TextUtils.isEmpty(d)) { |
| 424 | setContentDescription(d); |
Svetoslav Ganov | 6179ea3 | 2011-06-28 01:12:41 -0700 | [diff] [blame] | 425 | } |
| 426 | } |
| 427 | } |
Daniel Sandler | 7579bca | 2011-08-18 15:47:26 -0400 | [diff] [blame] | 428 | |
| 429 | public String toString() { |
John Spurlock | 209bede | 2013-07-17 12:23:27 -0400 | [diff] [blame] | 430 | return "StatusBarIconView(slot=" + mSlot + " icon=" + mIcon |
Daniel Sandler | 7579bca | 2011-08-18 15:47:26 -0400 | [diff] [blame] | 431 | + " notification=" + mNotification + ")"; |
| 432 | } |
Jason Monk | 3b23007 | 2015-05-29 11:11:29 -0400 | [diff] [blame] | 433 | |
| 434 | public String getSlot() { |
| 435 | return mSlot; |
| 436 | } |
Adrian Roos | eba0582 | 2016-04-22 17:09:27 -0700 | [diff] [blame] | 437 | |
| 438 | |
| 439 | public static String contentDescForNotification(Context c, Notification n) { |
Adrian Roos | a8e18ef | 2016-05-26 17:17:02 -0700 | [diff] [blame] | 440 | String appName = ""; |
| 441 | try { |
| 442 | Notification.Builder builder = Notification.Builder.recoverBuilder(c, n); |
| 443 | appName = builder.loadHeaderAppName(); |
| 444 | } catch (RuntimeException e) { |
| 445 | Log.e(TAG, "Unable to recover builder", e); |
| 446 | // Trying to get the app name from the app info instead. |
| 447 | Parcelable appInfo = n.extras.getParcelable( |
| 448 | Notification.EXTRA_BUILDER_APPLICATION_INFO); |
| 449 | if (appInfo instanceof ApplicationInfo) { |
| 450 | appName = String.valueOf(((ApplicationInfo) appInfo).loadLabel( |
| 451 | c.getPackageManager())); |
| 452 | } |
| 453 | } |
Adrian Roos | 51548e6 | 2016-04-28 11:20:26 -0700 | [diff] [blame] | 454 | |
Julia Reynolds | 7f9ce78 | 2016-05-17 15:34:34 -0400 | [diff] [blame] | 455 | CharSequence title = n.extras.getCharSequence(Notification.EXTRA_TITLE); |
Adrian Roos | eba0582 | 2016-04-22 17:09:27 -0700 | [diff] [blame] | 456 | CharSequence ticker = n.tickerText; |
Adrian Roos | 51548e6 | 2016-04-28 11:20:26 -0700 | [diff] [blame] | 457 | |
| 458 | CharSequence desc = !TextUtils.isEmpty(ticker) ? ticker |
| 459 | : !TextUtils.isEmpty(title) ? title : ""; |
| 460 | |
| 461 | return c.getString(R.string.accessibility_desc_notification_icon, appName, desc); |
Adrian Roos | eba0582 | 2016-04-22 17:09:27 -0700 | [diff] [blame] | 462 | } |
Adrian Roos | 51548e6 | 2016-04-28 11:20:26 -0700 | [diff] [blame] | 463 | |
Selim Cinek | 875ba9b | 2017-02-13 16:20:17 -0800 | [diff] [blame] | 464 | /** |
| 465 | * Set the color that is used to draw decoration like the overflow dot. This will not be applied |
| 466 | * to the drawable. |
| 467 | */ |
| 468 | public void setDecorColor(int iconTint) { |
Selim Cinek | 30d9bad7 | 2017-03-20 17:43:50 +0000 | [diff] [blame^] | 469 | mDotPaint.setColor(iconTint); |
Selim Cinek | 49014f8 | 2016-11-04 14:55:30 -0700 | [diff] [blame] | 470 | } |
| 471 | |
Selim Cinek | 875ba9b | 2017-02-13 16:20:17 -0800 | [diff] [blame] | 472 | /** |
| 473 | * Set the static color that should be used for the drawable of this icon if it's not |
| 474 | * transitioning this also immediately sets the color. |
| 475 | */ |
| 476 | public void setStaticDrawableColor(int color) { |
| 477 | mDrawableColor = color; |
| 478 | setColorInternal(color); |
| 479 | mIconColor = color; |
| 480 | } |
| 481 | |
| 482 | private void setColorInternal(int color) { |
| 483 | if (color != NO_COLOR) { |
| 484 | setImageTintList(ColorStateList.valueOf(color)); |
| 485 | } else { |
| 486 | setImageTintList(null); |
| 487 | } |
| 488 | mCurrentSetColor = color; |
| 489 | } |
| 490 | |
| 491 | public void setIconColor(int iconColor, boolean animate) { |
| 492 | if (mIconColor != iconColor) { |
| 493 | mIconColor = iconColor; |
| 494 | if (mColorAnimator != null) { |
| 495 | mColorAnimator.cancel(); |
| 496 | } |
| 497 | if (mCurrentSetColor == iconColor) { |
| 498 | return; |
| 499 | } |
| 500 | if (animate && mCurrentSetColor != NO_COLOR) { |
| 501 | mAnimationStartColor = mCurrentSetColor; |
| 502 | mColorAnimator = ValueAnimator.ofFloat(0.0f, 1.0f); |
| 503 | mColorAnimator.setInterpolator(Interpolators.FAST_OUT_SLOW_IN); |
| 504 | mColorAnimator.setDuration(ANIMATION_DURATION_FAST); |
| 505 | mColorAnimator.addUpdateListener(mColorUpdater); |
| 506 | mColorAnimator.addListener(new AnimatorListenerAdapter() { |
| 507 | @Override |
| 508 | public void onAnimationEnd(Animator animation) { |
| 509 | mColorAnimator = null; |
| 510 | mAnimationStartColor = NO_COLOR; |
| 511 | } |
| 512 | }); |
| 513 | mColorAnimator.start(); |
| 514 | } else { |
| 515 | setColorInternal(iconColor); |
| 516 | } |
| 517 | } |
| 518 | } |
| 519 | |
| 520 | public int getStaticDrawableColor() { |
| 521 | return mDrawableColor; |
| 522 | } |
| 523 | |
Selim Cinek | 5b5beb01 | 2016-11-08 18:11:58 -0800 | [diff] [blame] | 524 | public void setVisibleState(int state) { |
| 525 | setVisibleState(state, true /* animate */, null /* endRunnable */); |
| 526 | } |
| 527 | |
| 528 | public void setVisibleState(int state, boolean animate) { |
| 529 | setVisibleState(state, animate, null); |
| 530 | } |
| 531 | |
| 532 | @Override |
| 533 | public boolean hasOverlappingRendering() { |
| 534 | return false; |
| 535 | } |
| 536 | |
| 537 | public void setVisibleState(int visibleState, boolean animate, Runnable endRunnable) { |
Selim Cinek | 65d418e | 2016-11-29 15:42:34 -0800 | [diff] [blame] | 538 | boolean runnableAdded = false; |
Selim Cinek | 49014f8 | 2016-11-04 14:55:30 -0700 | [diff] [blame] | 539 | if (visibleState != mVisibleState) { |
| 540 | mVisibleState = visibleState; |
Selim Cinek | 5ece5a7 | 2017-02-15 11:47:42 -0800 | [diff] [blame] | 541 | if (mIconAppearAnimator != null) { |
| 542 | mIconAppearAnimator.cancel(); |
| 543 | } |
| 544 | if (mDotAnimator != null) { |
| 545 | mDotAnimator.cancel(); |
| 546 | } |
Selim Cinek | 5b5beb01 | 2016-11-08 18:11:58 -0800 | [diff] [blame] | 547 | if (animate) { |
Selim Cinek | 5b5beb01 | 2016-11-08 18:11:58 -0800 | [diff] [blame] | 548 | float targetAmount = 0.0f; |
| 549 | Interpolator interpolator = Interpolators.FAST_OUT_LINEAR_IN; |
| 550 | if (visibleState == STATE_ICON) { |
| 551 | targetAmount = 1.0f; |
| 552 | interpolator = Interpolators.LINEAR_OUT_SLOW_IN; |
| 553 | } |
Selim Cinek | 65d418e | 2016-11-29 15:42:34 -0800 | [diff] [blame] | 554 | float currentAmount = getIconAppearAmount(); |
| 555 | if (targetAmount != currentAmount) { |
| 556 | mIconAppearAnimator = ObjectAnimator.ofFloat(this, ICON_APPEAR_AMOUNT, |
| 557 | currentAmount, targetAmount); |
| 558 | mIconAppearAnimator.setInterpolator(interpolator); |
Selim Cinek | 875ba9b | 2017-02-13 16:20:17 -0800 | [diff] [blame] | 559 | mIconAppearAnimator.setDuration(ANIMATION_DURATION_FAST); |
Selim Cinek | 65d418e | 2016-11-29 15:42:34 -0800 | [diff] [blame] | 560 | mIconAppearAnimator.addListener(new AnimatorListenerAdapter() { |
| 561 | @Override |
| 562 | public void onAnimationEnd(Animator animation) { |
| 563 | mIconAppearAnimator = null; |
| 564 | runRunnable(endRunnable); |
Selim Cinek | 5b5beb01 | 2016-11-08 18:11:58 -0800 | [diff] [blame] | 565 | } |
Selim Cinek | 65d418e | 2016-11-29 15:42:34 -0800 | [diff] [blame] | 566 | }); |
| 567 | mIconAppearAnimator.start(); |
| 568 | runnableAdded = true; |
| 569 | } |
Selim Cinek | 49014f8 | 2016-11-04 14:55:30 -0700 | [diff] [blame] | 570 | |
Selim Cinek | 5b5beb01 | 2016-11-08 18:11:58 -0800 | [diff] [blame] | 571 | targetAmount = visibleState == STATE_ICON ? 2.0f : 0.0f; |
| 572 | interpolator = Interpolators.FAST_OUT_LINEAR_IN; |
| 573 | if (visibleState == STATE_DOT) { |
| 574 | targetAmount = 1.0f; |
| 575 | interpolator = Interpolators.LINEAR_OUT_SLOW_IN; |
| 576 | } |
Selim Cinek | 65d418e | 2016-11-29 15:42:34 -0800 | [diff] [blame] | 577 | currentAmount = getDotAppearAmount(); |
| 578 | if (targetAmount != currentAmount) { |
| 579 | mDotAnimator = ObjectAnimator.ofFloat(this, DOT_APPEAR_AMOUNT, |
| 580 | currentAmount, targetAmount); |
| 581 | mDotAnimator.setInterpolator(interpolator); |
Selim Cinek | 875ba9b | 2017-02-13 16:20:17 -0800 | [diff] [blame] | 582 | mDotAnimator.setDuration(ANIMATION_DURATION_FAST); |
Selim Cinek | 65d418e | 2016-11-29 15:42:34 -0800 | [diff] [blame] | 583 | final boolean runRunnable = !runnableAdded; |
| 584 | mDotAnimator.addListener(new AnimatorListenerAdapter() { |
| 585 | @Override |
| 586 | public void onAnimationEnd(Animator animation) { |
| 587 | mDotAnimator = null; |
| 588 | if (runRunnable) { |
| 589 | runRunnable(endRunnable); |
| 590 | } |
| 591 | } |
| 592 | }); |
| 593 | mDotAnimator.start(); |
| 594 | runnableAdded = true; |
| 595 | } |
Selim Cinek | 5b5beb01 | 2016-11-08 18:11:58 -0800 | [diff] [blame] | 596 | } else { |
| 597 | setIconAppearAmount(visibleState == STATE_ICON ? 1.0f : 0.0f); |
Selim Cinek | 01a73f9 | 2016-12-06 16:13:42 -0800 | [diff] [blame] | 598 | setDotAppearAmount(visibleState == STATE_DOT ? 1.0f |
| 599 | : visibleState == STATE_ICON ? 2.0f |
| 600 | : 0.0f); |
Selim Cinek | 5b5beb01 | 2016-11-08 18:11:58 -0800 | [diff] [blame] | 601 | } |
Selim Cinek | 49014f8 | 2016-11-04 14:55:30 -0700 | [diff] [blame] | 602 | } |
Selim Cinek | 65d418e | 2016-11-29 15:42:34 -0800 | [diff] [blame] | 603 | if (!runnableAdded) { |
| 604 | runRunnable(endRunnable); |
| 605 | } |
| 606 | } |
| 607 | |
| 608 | private void runRunnable(Runnable runnable) { |
| 609 | if (runnable != null) { |
| 610 | runnable.run(); |
| 611 | } |
Selim Cinek | 49014f8 | 2016-11-04 14:55:30 -0700 | [diff] [blame] | 612 | } |
| 613 | |
Selim Cinek | 5b5beb01 | 2016-11-08 18:11:58 -0800 | [diff] [blame] | 614 | public void setIconAppearAmount(float iconAppearAmount) { |
Selim Cinek | 9ef119c | 2017-03-01 15:13:36 -0800 | [diff] [blame] | 615 | if (mIconAppearAmount != iconAppearAmount) { |
| 616 | mIconAppearAmount = iconAppearAmount; |
| 617 | invalidate(); |
| 618 | } |
Selim Cinek | 49014f8 | 2016-11-04 14:55:30 -0700 | [diff] [blame] | 619 | } |
| 620 | |
| 621 | public float getIconAppearAmount() { |
| 622 | return mIconAppearAmount; |
| 623 | } |
| 624 | |
| 625 | public int getVisibleState() { |
| 626 | return mVisibleState; |
| 627 | } |
| 628 | |
| 629 | public void setDotAppearAmount(float dotAppearAmount) { |
Selim Cinek | 9ef119c | 2017-03-01 15:13:36 -0800 | [diff] [blame] | 630 | if (mDotAppearAmount != dotAppearAmount) { |
| 631 | mDotAppearAmount = dotAppearAmount; |
| 632 | invalidate(); |
| 633 | } |
Selim Cinek | 49014f8 | 2016-11-04 14:55:30 -0700 | [diff] [blame] | 634 | } |
| 635 | |
Selim Cinek | 2b549f4 | 2016-11-22 16:38:51 -0800 | [diff] [blame] | 636 | @Override |
| 637 | public void setVisibility(int visibility) { |
| 638 | super.setVisibility(visibility); |
| 639 | if (mOnVisibilityChangedListener != null) { |
| 640 | mOnVisibilityChangedListener.onVisibilityChanged(visibility); |
| 641 | } |
| 642 | } |
| 643 | |
Selim Cinek | 49014f8 | 2016-11-04 14:55:30 -0700 | [diff] [blame] | 644 | public float getDotAppearAmount() { |
| 645 | return mDotAppearAmount; |
| 646 | } |
Selim Cinek | 2b549f4 | 2016-11-22 16:38:51 -0800 | [diff] [blame] | 647 | |
| 648 | public void setOnVisibilityChangedListener(OnVisibilityChangedListener listener) { |
| 649 | mOnVisibilityChangedListener = listener; |
| 650 | } |
| 651 | |
| 652 | public interface OnVisibilityChangedListener { |
| 653 | void onVisibilityChanged(int newVisibility); |
| 654 | } |
Joe Onorato | 0cbda99 | 2010-05-02 16:28:15 -0700 | [diff] [blame] | 655 | } |