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; |
Svetoslav Ganov | 6179ea3 | 2011-06-28 01:12:41 -0700 | [diff] [blame] | 22 | import android.app.Notification; |
Joe Onorato | 0cbda99 | 2010-05-02 16:28:15 -0700 | [diff] [blame] | 23 | import android.content.Context; |
Adrian Roos | a8e18ef | 2016-05-26 17:17:02 -0700 | [diff] [blame] | 24 | import android.content.pm.ApplicationInfo; |
Selim Cinek | 3e7592d | 2016-04-11 09:35:54 +0800 | [diff] [blame] | 25 | import android.content.res.Configuration; |
Joe Onorato | 0cbda99 | 2010-05-02 16:28:15 -0700 | [diff] [blame] | 26 | import android.content.res.Resources; |
Joe Onorato | 0cbda99 | 2010-05-02 16:28:15 -0700 | [diff] [blame] | 27 | import android.graphics.Canvas; |
Joe Onorato | 6c01a11 | 2010-10-04 17:38:47 -0400 | [diff] [blame] | 28 | import android.graphics.Paint; |
| 29 | import android.graphics.Rect; |
John Spurlock | de84f0e | 2013-06-12 12:41:00 -0400 | [diff] [blame] | 30 | import android.graphics.drawable.Drawable; |
Dan Sandler | d63f932 | 2015-05-06 15:18:49 -0400 | [diff] [blame] | 31 | import android.graphics.drawable.Icon; |
Adrian Roos | a8e18ef | 2016-05-26 17:17:02 -0700 | [diff] [blame] | 32 | import android.os.Parcelable; |
Jeff Sharkey | ded653b | 2012-09-27 19:09:24 -0700 | [diff] [blame] | 33 | import android.os.UserHandle; |
Svetoslav Ganov | 6179ea3 | 2011-06-28 01:12:41 -0700 | [diff] [blame] | 34 | import android.text.TextUtils; |
Daniel Sandler | 05e2414 | 2011-11-10 11:56:49 -0500 | [diff] [blame] | 35 | import android.util.AttributeSet; |
Selim Cinek | 49014f8 | 2016-11-04 14:55:30 -0700 | [diff] [blame] | 36 | import android.util.FloatProperty; |
Joe Onorato | f9ec03c | 2010-09-23 15:09:18 -0700 | [diff] [blame] | 37 | import android.util.Log; |
Selim Cinek | 49014f8 | 2016-11-04 14:55:30 -0700 | [diff] [blame] | 38 | import android.util.Property; |
Anthony Chen | 55e8e1e | 2016-01-08 10:31:46 -0800 | [diff] [blame] | 39 | import android.util.TypedValue; |
Selim Cinek | 2b549f4 | 2016-11-22 16:38:51 -0800 | [diff] [blame] | 40 | import android.view.View; |
Joe Onorato | 0cbda99 | 2010-05-02 16:28:15 -0700 | [diff] [blame] | 41 | import android.view.ViewDebug; |
Svetoslav Ganov | 6179ea3 | 2011-06-28 01:12:41 -0700 | [diff] [blame] | 42 | import android.view.accessibility.AccessibilityEvent; |
Selim Cinek | 49014f8 | 2016-11-04 14:55:30 -0700 | [diff] [blame] | 43 | import android.view.animation.Interpolator; |
Winson | c0d7058 | 2016-01-29 10:24:39 -0800 | [diff] [blame] | 44 | |
Joe Onorato | 0cbda99 | 2010-05-02 16:28:15 -0700 | [diff] [blame] | 45 | import com.android.internal.statusbar.StatusBarIcon; |
Selim Cinek | 49014f8 | 2016-11-04 14:55:30 -0700 | [diff] [blame] | 46 | import com.android.systemui.Interpolators; |
Joe Onorato | 6c01a11 | 2010-10-04 17:38:47 -0400 | [diff] [blame] | 47 | import com.android.systemui.R; |
Selim Cinek | 49014f8 | 2016-11-04 14:55:30 -0700 | [diff] [blame] | 48 | import com.android.systemui.statusbar.notification.NotificationUtils; |
Joe Onorato | 6c01a11 | 2010-10-04 17:38:47 -0400 | [diff] [blame] | 49 | |
John Spurlock | de84f0e | 2013-06-12 12:41:00 -0400 | [diff] [blame] | 50 | import java.text.NumberFormat; |
| 51 | |
Joe Onorato | 0cbda99 | 2010-05-02 16:28:15 -0700 | [diff] [blame] | 52 | public class StatusBarIconView extends AnimatedImageView { |
Selim Cinek | 49014f8 | 2016-11-04 14:55:30 -0700 | [diff] [blame] | 53 | public static final int STATE_ICON = 0; |
| 54 | public static final int STATE_DOT = 1; |
| 55 | public static final int STATE_HIDDEN = 2; |
Joe Onorato | 0cbda99 | 2010-05-02 16:28:15 -0700 | [diff] [blame] | 56 | |
Selim Cinek | 49014f8 | 2016-11-04 14:55:30 -0700 | [diff] [blame] | 57 | 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 Cinek | 5b5beb01 | 2016-11-08 18:11:58 -0800 | [diff] [blame] | 71 | private static final Property<StatusBarIconView, Float> DOT_APPEAR_AMOUNT |
Selim Cinek | 49014f8 | 2016-11-04 14:55:30 -0700 | [diff] [blame] | 72 | = 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 Onorato | 0cbda99 | 2010-05-02 16:28:15 -0700 | [diff] [blame] | 86 | private StatusBarIcon mIcon; |
| 87 | @ViewDebug.ExportedProperty private String mSlot; |
Joe Onorato | 6c01a11 | 2010-10-04 17:38:47 -0400 | [diff] [blame] | 88 | private Drawable mNumberBackground; |
| 89 | private Paint mNumberPain; |
| 90 | private int mNumberX; |
| 91 | private int mNumberY; |
| 92 | private String mNumberText; |
Svetoslav Ganov | 6179ea3 | 2011-06-28 01:12:41 -0700 | [diff] [blame] | 93 | private Notification mNotification; |
Jason Monk | 3b23007 | 2015-05-29 11:11:29 -0400 | [diff] [blame] | 94 | private final boolean mBlocked; |
Selim Cinek | 3e7592d | 2016-04-11 09:35:54 +0800 | [diff] [blame] | 95 | private int mDensity; |
Selim Cinek | 281c202 | 2016-10-13 19:14:43 -0700 | [diff] [blame] | 96 | private float mIconScale = 1.0f; |
Selim Cinek | 49014f8 | 2016-11-04 14:55:30 -0700 | [diff] [blame] | 97 | 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 Cinek | 2b549f4 | 2016-11-22 16:38:51 -0800 | [diff] [blame] | 106 | private OnVisibilityChangedListener mOnVisibilityChangedListener; |
Joe Onorato | 0cbda99 | 2010-05-02 16:28:15 -0700 | [diff] [blame] | 107 | |
John Spurlock | e6f0a71 | 2013-09-03 16:23:49 -0400 | [diff] [blame] | 108 | public StatusBarIconView(Context context, String slot, Notification notification) { |
Jason Monk | 3b23007 | 2015-05-29 11:11:29 -0400 | [diff] [blame] | 109 | this(context, slot, notification, false); |
| 110 | } |
| 111 | |
| 112 | public StatusBarIconView(Context context, String slot, Notification notification, |
| 113 | boolean blocked) { |
Joe Onorato | 0cbda99 | 2010-05-02 16:28:15 -0700 | [diff] [blame] | 114 | super(context); |
Jason Monk | 3b23007 | 2015-05-29 11:11:29 -0400 | [diff] [blame] | 115 | mBlocked = blocked; |
Joe Onorato | 0cbda99 | 2010-05-02 16:28:15 -0700 | [diff] [blame] | 116 | mSlot = slot; |
Joe Onorato | 6c01a11 | 2010-10-04 17:38:47 -0400 | [diff] [blame] | 117 | mNumberPain = new Paint(); |
| 118 | mNumberPain.setTextAlign(Paint.Align.CENTER); |
Alan Viverette | 4a357cd | 2015-03-18 18:37:18 -0700 | [diff] [blame] | 119 | mNumberPain.setColor(context.getColor(R.drawable.notification_number_text_color)); |
Joe Onorato | 6c01a11 | 2010-10-04 17:38:47 -0400 | [diff] [blame] | 120 | mNumberPain.setAntiAlias(true); |
Christoph Studer | a0506e7 | 2014-07-31 20:27:39 +0200 | [diff] [blame] | 121 | setNotification(notification); |
Selim Cinek | 3e7592d | 2016-04-11 09:35:54 +0800 | [diff] [blame] | 122 | maybeUpdateIconScale(); |
| 123 | setScaleType(ScaleType.CENTER); |
| 124 | mDensity = context.getResources().getDisplayMetrics().densityDpi; |
Selim Cinek | 49014f8 | 2016-11-04 14:55:30 -0700 | [diff] [blame] | 125 | if (mNotification != null) { |
| 126 | setIconTint(getContext().getColor( |
| 127 | com.android.internal.R.color.notification_icon_default_color)); |
| 128 | } |
| 129 | reloadDimens(); |
Selim Cinek | 3e7592d | 2016-04-11 09:35:54 +0800 | [diff] [blame] | 130 | } |
Daniel Sandler | 26c84b1 | 2011-07-27 00:09:40 -0400 | [diff] [blame] | 131 | |
Selim Cinek | 3e7592d | 2016-04-11 09:35:54 +0800 | [diff] [blame] | 132 | private void maybeUpdateIconScale() { |
Daniel Sandler | 7579bca | 2011-08-18 15:47:26 -0400 | [diff] [blame] | 133 | // We do not resize and scale system icons (on the right), only notification icons (on the |
| 134 | // left). |
Selim Cinek | 3e7592d | 2016-04-11 09:35:54 +0800 | [diff] [blame] | 135 | if (mNotification != null || mAlwaysScaleIcon) { |
| 136 | updateIconScale(); |
Daniel Sandler | 7579bca | 2011-08-18 15:47:26 -0400 | [diff] [blame] | 137 | } |
Selim Cinek | 3e7592d | 2016-04-11 09:35:54 +0800 | [diff] [blame] | 138 | } |
Daniel Sandler | abff032 | 2011-09-27 11:19:34 -0400 | [diff] [blame] | 139 | |
Selim Cinek | 3e7592d | 2016-04-11 09:35:54 +0800 | [diff] [blame] | 140 | 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 Cinek | 281c202 | 2016-10-13 19:14:43 -0700 | [diff] [blame] | 144 | mIconScale = (float)imageBounds / (float)outerBounds; |
Selim Cinek | 281c202 | 2016-10-13 19:14:43 -0700 | [diff] [blame] | 145 | } |
| 146 | |
| 147 | public float getIconScale() { |
| 148 | return mIconScale; |
Selim Cinek | 3e7592d | 2016-04-11 09:35:54 +0800 | [diff] [blame] | 149 | } |
| 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 Cinek | 49014f8 | 2016-11-04 14:55:30 -0700 | [diff] [blame] | 159 | 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 Cinek | 3e7592d | 2016-04-11 09:35:54 +0800 | [diff] [blame] | 168 | } |
Joe Onorato | 0cbda99 | 2010-05-02 16:28:15 -0700 | [diff] [blame] | 169 | } |
| 170 | |
Christoph Studer | a0506e7 | 2014-07-31 20:27:39 +0200 | [diff] [blame] | 171 | public void setNotification(Notification notification) { |
| 172 | mNotification = notification; |
| 173 | setContentDescription(notification); |
| 174 | } |
| 175 | |
Daniel Sandler | 05e2414 | 2011-11-10 11:56:49 -0500 | [diff] [blame] | 176 | public StatusBarIconView(Context context, AttributeSet attrs) { |
| 177 | super(context, attrs); |
Jason Monk | 3b23007 | 2015-05-29 11:11:29 -0400 | [diff] [blame] | 178 | mBlocked = false; |
Selim Cinek | 3e7592d | 2016-04-11 09:35:54 +0800 | [diff] [blame] | 179 | mAlwaysScaleIcon = true; |
| 180 | updateIconScale(); |
| 181 | mDensity = context.getResources().getDisplayMetrics().densityDpi; |
Daniel Sandler | 05e2414 | 2011-11-10 11:56:49 -0500 | [diff] [blame] | 182 | } |
| 183 | |
Joe Onorato | 0cbda99 | 2010-05-02 16:28:15 -0700 | [diff] [blame] | 184 | private static boolean streq(String a, String b) { |
Joe Onorato | 66d7d01 | 2010-05-14 10:05:10 -0700 | [diff] [blame] | 185 | if (a == b) { |
| 186 | return true; |
| 187 | } |
Joe Onorato | 0cbda99 | 2010-05-02 16:28:15 -0700 | [diff] [blame] | 188 | 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 Sandler | d63f932 | 2015-05-06 15:18:49 -0400 | [diff] [blame] | 197 | 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 Onorato | 005847b | 2010-06-04 16:08:02 -0400 | [diff] [blame] | 209 | /** |
| 210 | * Returns whether the set succeeded. |
| 211 | */ |
| 212 | public boolean set(StatusBarIcon icon) { |
Dan Sandler | d63f932 | 2015-05-06 15:18:49 -0400 | [diff] [blame] | 213 | final boolean iconEquals = mIcon != null && equalIcons(mIcon.icon, icon.icon); |
Joe Onorato | 005847b | 2010-06-04 16:08:02 -0400 | [diff] [blame] | 214 | final boolean levelEquals = iconEquals |
| 215 | && mIcon.iconLevel == icon.iconLevel; |
| 216 | final boolean visibilityEquals = mIcon != null |
| 217 | && mIcon.visible == icon.visible; |
Joe Onorato | 6c01a11 | 2010-10-04 17:38:47 -0400 | [diff] [blame] | 218 | final boolean numberEquals = mIcon != null |
| 219 | && mIcon.number == icon.number; |
| 220 | mIcon = icon.clone(); |
Svetoslav Ganov | 6179ea3 | 2011-06-28 01:12:41 -0700 | [diff] [blame] | 221 | setContentDescription(icon.contentDescription); |
Joe Onorato | 005847b | 2010-06-04 16:08:02 -0400 | [diff] [blame] | 222 | if (!iconEquals) { |
Fabrice Di Meglio | 82fca5d | 2013-01-09 15:42:31 -0800 | [diff] [blame] | 223 | if (!updateDrawable(false /* no clear */)) return false; |
Selim Cinek | 2b6eb803 | 2016-12-29 14:22:21 +0100 | [diff] [blame^] | 224 | // we have to clear the grayscale tag since it may have changed |
| 225 | setTag(R.id.icon_is_grayscale, null); |
Joe Onorato | 0cbda99 | 2010-05-02 16:28:15 -0700 | [diff] [blame] | 226 | } |
Joe Onorato | 005847b | 2010-06-04 16:08:02 -0400 | [diff] [blame] | 227 | if (!levelEquals) { |
| 228 | setImageLevel(icon.iconLevel); |
Joe Onorato | 0cbda99 | 2010-05-02 16:28:15 -0700 | [diff] [blame] | 229 | } |
Daniel Sandler | 26c84b1 | 2011-07-27 00:09:40 -0400 | [diff] [blame] | 230 | |
Joe Onorato | 6c01a11 | 2010-10-04 17:38:47 -0400 | [diff] [blame] | 231 | if (!numberEquals) { |
John Spurlock | 0153478 | 2014-01-13 11:59:22 -0500 | [diff] [blame] | 232 | if (icon.number > 0 && getContext().getResources().getBoolean( |
Joe Onorato | 8595a3d | 2010-11-19 18:12:07 -0800 | [diff] [blame] | 233 | R.bool.config_statusBarShowNumber)) { |
Joe Onorato | 6c01a11 | 2010-10-04 17:38:47 -0400 | [diff] [blame] | 234 | 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 Onorato | 005847b | 2010-06-04 16:08:02 -0400 | [diff] [blame] | 245 | if (!visibilityEquals) { |
Jason Monk | 3b23007 | 2015-05-29 11:11:29 -0400 | [diff] [blame] | 246 | setVisibility(icon.visible && !mBlocked ? VISIBLE : GONE); |
Joe Onorato | 005847b | 2010-06-04 16:08:02 -0400 | [diff] [blame] | 247 | } |
Joe Onorato | 005847b | 2010-06-04 16:08:02 -0400 | [diff] [blame] | 248 | return true; |
Joe Onorato | 0cbda99 | 2010-05-02 16:28:15 -0700 | [diff] [blame] | 249 | } |
| 250 | |
Fabrice Di Meglio | 82fca5d | 2013-01-09 15:42:31 -0800 | [diff] [blame] | 251 | public void updateDrawable() { |
| 252 | updateDrawable(true /* with clear */); |
| 253 | } |
| 254 | |
| 255 | private boolean updateDrawable(boolean withClear) { |
Jorim Jaggi | 66ac133 | 2015-01-21 19:22:26 +0100 | [diff] [blame] | 256 | if (mIcon == null) { |
| 257 | return false; |
| 258 | } |
Fabrice Di Meglio | 82fca5d | 2013-01-09 15:42:31 -0800 | [diff] [blame] | 259 | Drawable drawable = getIcon(mIcon); |
| 260 | if (drawable == null) { |
John Spurlock | cd686b5 | 2013-06-05 10:13:46 -0400 | [diff] [blame] | 261 | Log.w(TAG, "No icon for slot " + mSlot); |
Fabrice Di Meglio | 82fca5d | 2013-01-09 15:42:31 -0800 | [diff] [blame] | 262 | return false; |
| 263 | } |
| 264 | if (withClear) { |
| 265 | setImageDrawable(null); |
| 266 | } |
| 267 | setImageDrawable(drawable); |
| 268 | return true; |
| 269 | } |
| 270 | |
Joe Onorato | f551054 | 2010-06-01 07:46:41 -0700 | [diff] [blame] | 271 | private Drawable getIcon(StatusBarIcon icon) { |
| 272 | return getIcon(getContext(), icon); |
| 273 | } |
| 274 | |
Joe Onorato | 0cbda99 | 2010-05-02 16:28:15 -0700 | [diff] [blame] | 275 | /** |
Dan Sandler | d63f932 | 2015-05-06 15:18:49 -0400 | [diff] [blame] | 276 | * Returns the right icon to use for this item |
John Spurlock | 209bede | 2013-07-17 12:23:27 -0400 | [diff] [blame] | 277 | * |
Dan Sandler | d63f932 | 2015-05-06 15:18:49 -0400 | [diff] [blame] | 278 | * @param context Context to use to get resources |
Joe Onorato | 0cbda99 | 2010-05-02 16:28:15 -0700 | [diff] [blame] | 279 | * @return Drawable for this item, or null if the package or item could not |
| 280 | * be found |
| 281 | */ |
Anthony Chen | 55e8e1e | 2016-01-08 10:31:46 -0800 | [diff] [blame] | 282 | public static Drawable getIcon(Context context, StatusBarIcon statusBarIcon) { |
| 283 | int userId = statusBarIcon.user.getIdentifier(); |
Dan Sandler | d63f932 | 2015-05-06 15:18:49 -0400 | [diff] [blame] | 284 | if (userId == UserHandle.USER_ALL) { |
Xiaohui Chen | 87d0e25 | 2015-07-30 15:38:16 -0700 | [diff] [blame] | 285 | userId = UserHandle.USER_SYSTEM; |
Joe Onorato | 0cbda99 | 2010-05-02 16:28:15 -0700 | [diff] [blame] | 286 | } |
Anthony Chen | 55e8e1e | 2016-01-08 10:31:46 -0800 | [diff] [blame] | 287 | |
| 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 Onorato | 0cbda99 | 2010-05-02 16:28:15 -0700 | [diff] [blame] | 300 | } |
Joe Onorato | b77f53b | 2010-05-28 19:59:51 -0400 | [diff] [blame] | 301 | |
| 302 | public StatusBarIcon getStatusBarIcon() { |
| 303 | return mIcon; |
| 304 | } |
Joe Onorato | f9ec03c | 2010-09-23 15:09:18 -0700 | [diff] [blame] | 305 | |
Svetoslav Ganov | 6179ea3 | 2011-06-28 01:12:41 -0700 | [diff] [blame] | 306 | @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 Onorato | 6c01a11 | 2010-10-04 17:38:47 -0400 | [diff] [blame] | 315 | 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 Ganov | 6179ea3 | 2011-06-28 01:12:41 -0700 | [diff] [blame] | 322 | @Override |
Jorim Jaggi | 66ac133 | 2015-01-21 19:22:26 +0100 | [diff] [blame] | 323 | public void onRtlPropertiesChanged(int layoutDirection) { |
| 324 | super.onRtlPropertiesChanged(layoutDirection); |
| 325 | updateDrawable(); |
| 326 | } |
| 327 | |
| 328 | @Override |
Joe Onorato | 6c01a11 | 2010-10-04 17:38:47 -0400 | [diff] [blame] | 329 | protected void onDraw(Canvas canvas) { |
Selim Cinek | 49014f8 | 2016-11-04 14:55:30 -0700 | [diff] [blame] | 330 | 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 Onorato | 6c01a11 | 2010-10-04 17:38:47 -0400 | [diff] [blame] | 337 | |
| 338 | if (mNumberBackground != null) { |
| 339 | mNumberBackground.draw(canvas); |
| 340 | canvas.drawText(mNumberText, mNumberX, mNumberY, mNumberPain); |
| 341 | } |
Selim Cinek | 49014f8 | 2016-11-04 14:55:30 -0700 | [diff] [blame] | 342 | 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 Onorato | 6c01a11 | 2010-10-04 17:38:47 -0400 | [diff] [blame] | 356 | } |
| 357 | |
Svetoslav Ganov | 6179ea3 | 2011-06-28 01:12:41 -0700 | [diff] [blame] | 358 | @Override |
Joe Onorato | f9ec03c | 2010-09-23 15:09:18 -0700 | [diff] [blame] | 359 | 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 Onorato | 6c01a11 | 2010-10-04 17:38:47 -0400 | [diff] [blame] | 364 | |
| 365 | void placeNumber() { |
Daniel Sandler | ebce011 | 2011-06-16 16:44:51 -0400 | [diff] [blame] | 366 | final String str; |
John Spurlock | 0153478 | 2014-01-13 11:59:22 -0500 | [diff] [blame] | 367 | final int tooBig = getContext().getResources().getInteger( |
Daniel Sandler | ebce011 | 2011-06-16 16:44:51 -0400 | [diff] [blame] | 368 | android.R.integer.status_bar_notification_info_maxnum); |
| 369 | if (mIcon.number > tooBig) { |
John Spurlock | 0153478 | 2014-01-13 11:59:22 -0500 | [diff] [blame] | 370 | str = getContext().getResources().getString( |
Daniel Sandler | ebce011 | 2011-06-16 16:44:51 -0400 | [diff] [blame] | 371 | 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 Onorato | 6c01a11 | 2010-10-04 17:38:47 -0400 | [diff] [blame] | 378 | 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 Ganov | 6179ea3 | 2011-06-28 01:12:41 -0700 | [diff] [blame] | 397 | |
| 398 | private void setContentDescription(Notification notification) { |
| 399 | if (notification != null) { |
Adrian Roos | eba0582 | 2016-04-22 17:09:27 -0700 | [diff] [blame] | 400 | String d = contentDescForNotification(mContext, notification); |
| 401 | if (!TextUtils.isEmpty(d)) { |
| 402 | setContentDescription(d); |
Svetoslav Ganov | 6179ea3 | 2011-06-28 01:12:41 -0700 | [diff] [blame] | 403 | } |
| 404 | } |
| 405 | } |
Daniel Sandler | 7579bca | 2011-08-18 15:47:26 -0400 | [diff] [blame] | 406 | |
| 407 | public String toString() { |
John Spurlock | 209bede | 2013-07-17 12:23:27 -0400 | [diff] [blame] | 408 | return "StatusBarIconView(slot=" + mSlot + " icon=" + mIcon |
Daniel Sandler | 7579bca | 2011-08-18 15:47:26 -0400 | [diff] [blame] | 409 | + " notification=" + mNotification + ")"; |
| 410 | } |
Jason Monk | 3b23007 | 2015-05-29 11:11:29 -0400 | [diff] [blame] | 411 | |
| 412 | public String getSlot() { |
| 413 | return mSlot; |
| 414 | } |
Adrian Roos | eba0582 | 2016-04-22 17:09:27 -0700 | [diff] [blame] | 415 | |
| 416 | |
| 417 | public static String contentDescForNotification(Context c, Notification n) { |
Adrian Roos | a8e18ef | 2016-05-26 17:17:02 -0700 | [diff] [blame] | 418 | 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 Roos | 51548e6 | 2016-04-28 11:20:26 -0700 | [diff] [blame] | 432 | |
Julia Reynolds | 7f9ce78 | 2016-05-17 15:34:34 -0400 | [diff] [blame] | 433 | CharSequence title = n.extras.getCharSequence(Notification.EXTRA_TITLE); |
Adrian Roos | eba0582 | 2016-04-22 17:09:27 -0700 | [diff] [blame] | 434 | CharSequence ticker = n.tickerText; |
Adrian Roos | 51548e6 | 2016-04-28 11:20:26 -0700 | [diff] [blame] | 435 | |
| 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 Roos | eba0582 | 2016-04-22 17:09:27 -0700 | [diff] [blame] | 440 | } |
Adrian Roos | 51548e6 | 2016-04-28 11:20:26 -0700 | [diff] [blame] | 441 | |
Selim Cinek | 49014f8 | 2016-11-04 14:55:30 -0700 | [diff] [blame] | 442 | public void setIconTint(int iconTint) { |
| 443 | mDotPaint.setColor(iconTint); |
| 444 | } |
| 445 | |
Selim Cinek | 5b5beb01 | 2016-11-08 18:11:58 -0800 | [diff] [blame] | 446 | 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 Cinek | 65d418e | 2016-11-29 15:42:34 -0800 | [diff] [blame] | 460 | boolean runnableAdded = false; |
Selim Cinek | 49014f8 | 2016-11-04 14:55:30 -0700 | [diff] [blame] | 461 | if (visibleState != mVisibleState) { |
| 462 | mVisibleState = visibleState; |
Selim Cinek | 5b5beb01 | 2016-11-08 18:11:58 -0800 | [diff] [blame] | 463 | if (animate) { |
| 464 | if (mIconAppearAnimator != null) { |
| 465 | mIconAppearAnimator.cancel(); |
Selim Cinek | 49014f8 | 2016-11-04 14:55:30 -0700 | [diff] [blame] | 466 | } |
Selim Cinek | 5b5beb01 | 2016-11-08 18:11:58 -0800 | [diff] [blame] | 467 | 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 Cinek | 65d418e | 2016-11-29 15:42:34 -0800 | [diff] [blame] | 473 | 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 Cinek | 5b5beb01 | 2016-11-08 18:11:58 -0800 | [diff] [blame] | 484 | } |
Selim Cinek | 65d418e | 2016-11-29 15:42:34 -0800 | [diff] [blame] | 485 | }); |
| 486 | mIconAppearAnimator.start(); |
| 487 | runnableAdded = true; |
| 488 | } |
Selim Cinek | 49014f8 | 2016-11-04 14:55:30 -0700 | [diff] [blame] | 489 | |
Selim Cinek | 5b5beb01 | 2016-11-08 18:11:58 -0800 | [diff] [blame] | 490 | if (mDotAnimator != null) { |
| 491 | mDotAnimator.cancel(); |
Selim Cinek | 49014f8 | 2016-11-04 14:55:30 -0700 | [diff] [blame] | 492 | } |
Selim Cinek | 5b5beb01 | 2016-11-08 18:11:58 -0800 | [diff] [blame] | 493 | 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 Cinek | 65d418e | 2016-11-29 15:42:34 -0800 | [diff] [blame] | 499 | 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 Cinek | 5b5beb01 | 2016-11-08 18:11:58 -0800 | [diff] [blame] | 518 | } else { |
| 519 | setIconAppearAmount(visibleState == STATE_ICON ? 1.0f : 0.0f); |
Selim Cinek | 01a73f9 | 2016-12-06 16:13:42 -0800 | [diff] [blame] | 520 | setDotAppearAmount(visibleState == STATE_DOT ? 1.0f |
| 521 | : visibleState == STATE_ICON ? 2.0f |
| 522 | : 0.0f); |
Selim Cinek | 5b5beb01 | 2016-11-08 18:11:58 -0800 | [diff] [blame] | 523 | } |
Selim Cinek | 49014f8 | 2016-11-04 14:55:30 -0700 | [diff] [blame] | 524 | } |
Selim Cinek | 65d418e | 2016-11-29 15:42:34 -0800 | [diff] [blame] | 525 | if (!runnableAdded) { |
| 526 | runRunnable(endRunnable); |
| 527 | } |
| 528 | } |
| 529 | |
| 530 | private void runRunnable(Runnable runnable) { |
| 531 | if (runnable != null) { |
| 532 | runnable.run(); |
| 533 | } |
Selim Cinek | 49014f8 | 2016-11-04 14:55:30 -0700 | [diff] [blame] | 534 | } |
| 535 | |
Selim Cinek | 5b5beb01 | 2016-11-08 18:11:58 -0800 | [diff] [blame] | 536 | public void setIconAppearAmount(float iconAppearAmount) { |
Selim Cinek | 49014f8 | 2016-11-04 14:55:30 -0700 | [diff] [blame] | 537 | 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 Cinek | 2b549f4 | 2016-11-22 16:38:51 -0800 | [diff] [blame] | 554 | @Override |
| 555 | public void setVisibility(int visibility) { |
| 556 | super.setVisibility(visibility); |
| 557 | if (mOnVisibilityChangedListener != null) { |
| 558 | mOnVisibilityChangedListener.onVisibilityChanged(visibility); |
| 559 | } |
| 560 | } |
| 561 | |
Selim Cinek | 49014f8 | 2016-11-04 14:55:30 -0700 | [diff] [blame] | 562 | public float getDotAppearAmount() { |
| 563 | return mDotAppearAmount; |
| 564 | } |
Selim Cinek | 2b549f4 | 2016-11-22 16:38:51 -0800 | [diff] [blame] | 565 | |
| 566 | public void setOnVisibilityChangedListener(OnVisibilityChangedListener listener) { |
| 567 | mOnVisibilityChangedListener = listener; |
| 568 | } |
| 569 | |
| 570 | public interface OnVisibilityChangedListener { |
| 571 | void onVisibilityChanged(int newVisibility); |
| 572 | } |
Joe Onorato | 0cbda99 | 2010-05-02 16:28:15 -0700 | [diff] [blame] | 573 | } |