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; |
| 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 | 3e7592d | 2016-04-11 09:35:54 +0800 | [diff] [blame] | 26 | import android.content.res.Configuration; |
Joe Onorato | 0cbda99 | 2010-05-02 16:28:15 -0700 | [diff] [blame] | 27 | import android.content.res.Resources; |
Joe Onorato | 0cbda99 | 2010-05-02 16:28:15 -0700 | [diff] [blame] | 28 | import android.graphics.Canvas; |
Joe Onorato | 6c01a11 | 2010-10-04 17:38:47 -0400 | [diff] [blame] | 29 | import android.graphics.Paint; |
| 30 | import android.graphics.Rect; |
John Spurlock | de84f0e | 2013-06-12 12:41:00 -0400 | [diff] [blame] | 31 | import android.graphics.drawable.Drawable; |
Dan Sandler | d63f932 | 2015-05-06 15:18:49 -0400 | [diff] [blame] | 32 | import android.graphics.drawable.Icon; |
Adrian Roos | a8e18ef | 2016-05-26 17:17:02 -0700 | [diff] [blame] | 33 | import android.os.Parcelable; |
Jeff Sharkey | ded653b | 2012-09-27 19:09:24 -0700 | [diff] [blame] | 34 | import android.os.UserHandle; |
Svetoslav Ganov | 6179ea3 | 2011-06-28 01:12:41 -0700 | [diff] [blame] | 35 | import android.text.TextUtils; |
Daniel Sandler | 05e2414 | 2011-11-10 11:56:49 -0500 | [diff] [blame] | 36 | import android.util.AttributeSet; |
Selim Cinek | 49014f8 | 2016-11-04 14:55:30 -0700 | [diff] [blame^] | 37 | import android.util.FloatProperty; |
Joe Onorato | f9ec03c | 2010-09-23 15:09:18 -0700 | [diff] [blame] | 38 | import android.util.Log; |
Selim Cinek | 49014f8 | 2016-11-04 14:55:30 -0700 | [diff] [blame^] | 39 | import android.util.Property; |
Anthony Chen | 55e8e1e | 2016-01-08 10:31:46 -0800 | [diff] [blame] | 40 | import android.util.TypedValue; |
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 | }; |
| 71 | private static final Property<StatusBarIconView, Float> DOT_APPEAR_AMOUNG |
| 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; |
Joe Onorato | 0cbda99 | 2010-05-02 16:28:15 -0700 | [diff] [blame] | 106 | |
John Spurlock | e6f0a71 | 2013-09-03 16:23:49 -0400 | [diff] [blame] | 107 | public StatusBarIconView(Context context, String slot, Notification notification) { |
Jason Monk | 3b23007 | 2015-05-29 11:11:29 -0400 | [diff] [blame] | 108 | this(context, slot, notification, false); |
| 109 | } |
| 110 | |
| 111 | public StatusBarIconView(Context context, String slot, Notification notification, |
| 112 | boolean blocked) { |
Joe Onorato | 0cbda99 | 2010-05-02 16:28:15 -0700 | [diff] [blame] | 113 | super(context); |
Jason Monk | 3b23007 | 2015-05-29 11:11:29 -0400 | [diff] [blame] | 114 | mBlocked = blocked; |
Joe Onorato | 0cbda99 | 2010-05-02 16:28:15 -0700 | [diff] [blame] | 115 | mSlot = slot; |
Joe Onorato | 6c01a11 | 2010-10-04 17:38:47 -0400 | [diff] [blame] | 116 | mNumberPain = new Paint(); |
| 117 | mNumberPain.setTextAlign(Paint.Align.CENTER); |
Alan Viverette | 4a357cd | 2015-03-18 18:37:18 -0700 | [diff] [blame] | 118 | mNumberPain.setColor(context.getColor(R.drawable.notification_number_text_color)); |
Joe Onorato | 6c01a11 | 2010-10-04 17:38:47 -0400 | [diff] [blame] | 119 | mNumberPain.setAntiAlias(true); |
Christoph Studer | a0506e7 | 2014-07-31 20:27:39 +0200 | [diff] [blame] | 120 | setNotification(notification); |
Selim Cinek | 3e7592d | 2016-04-11 09:35:54 +0800 | [diff] [blame] | 121 | maybeUpdateIconScale(); |
| 122 | setScaleType(ScaleType.CENTER); |
| 123 | mDensity = context.getResources().getDisplayMetrics().densityDpi; |
Selim Cinek | 49014f8 | 2016-11-04 14:55:30 -0700 | [diff] [blame^] | 124 | if (mNotification != null) { |
| 125 | setIconTint(getContext().getColor( |
| 126 | com.android.internal.R.color.notification_icon_default_color)); |
| 127 | } |
| 128 | reloadDimens(); |
Selim Cinek | 3e7592d | 2016-04-11 09:35:54 +0800 | [diff] [blame] | 129 | } |
Daniel Sandler | 26c84b1 | 2011-07-27 00:09:40 -0400 | [diff] [blame] | 130 | |
Selim Cinek | 3e7592d | 2016-04-11 09:35:54 +0800 | [diff] [blame] | 131 | private void maybeUpdateIconScale() { |
Daniel Sandler | 7579bca | 2011-08-18 15:47:26 -0400 | [diff] [blame] | 132 | // We do not resize and scale system icons (on the right), only notification icons (on the |
| 133 | // left). |
Selim Cinek | 3e7592d | 2016-04-11 09:35:54 +0800 | [diff] [blame] | 134 | if (mNotification != null || mAlwaysScaleIcon) { |
| 135 | updateIconScale(); |
Daniel Sandler | 7579bca | 2011-08-18 15:47:26 -0400 | [diff] [blame] | 136 | } |
Selim Cinek | 3e7592d | 2016-04-11 09:35:54 +0800 | [diff] [blame] | 137 | } |
Daniel Sandler | abff032 | 2011-09-27 11:19:34 -0400 | [diff] [blame] | 138 | |
Selim Cinek | 3e7592d | 2016-04-11 09:35:54 +0800 | [diff] [blame] | 139 | private void updateIconScale() { |
| 140 | Resources res = mContext.getResources(); |
| 141 | final int outerBounds = res.getDimensionPixelSize(R.dimen.status_bar_icon_size); |
| 142 | final int imageBounds = res.getDimensionPixelSize(R.dimen.status_bar_icon_drawing_size); |
Selim Cinek | 281c202 | 2016-10-13 19:14:43 -0700 | [diff] [blame] | 143 | mIconScale = (float)imageBounds / (float)outerBounds; |
Selim Cinek | 281c202 | 2016-10-13 19:14:43 -0700 | [diff] [blame] | 144 | } |
| 145 | |
| 146 | public float getIconScale() { |
| 147 | return mIconScale; |
Selim Cinek | 3e7592d | 2016-04-11 09:35:54 +0800 | [diff] [blame] | 148 | } |
| 149 | |
| 150 | @Override |
| 151 | protected void onConfigurationChanged(Configuration newConfig) { |
| 152 | super.onConfigurationChanged(newConfig); |
| 153 | int density = newConfig.densityDpi; |
| 154 | if (density != mDensity) { |
| 155 | mDensity = density; |
| 156 | maybeUpdateIconScale(); |
| 157 | updateDrawable(); |
Selim Cinek | 49014f8 | 2016-11-04 14:55:30 -0700 | [diff] [blame^] | 158 | reloadDimens(); |
| 159 | } |
| 160 | } |
| 161 | |
| 162 | private void reloadDimens() { |
| 163 | boolean applyRadius = mDotRadius == mStaticDotRadius; |
| 164 | mStaticDotRadius = getResources().getDimensionPixelSize(R.dimen.overflow_dot_radius); |
| 165 | if (applyRadius) { |
| 166 | mDotRadius = mStaticDotRadius; |
Selim Cinek | 3e7592d | 2016-04-11 09:35:54 +0800 | [diff] [blame] | 167 | } |
Joe Onorato | 0cbda99 | 2010-05-02 16:28:15 -0700 | [diff] [blame] | 168 | } |
| 169 | |
Christoph Studer | a0506e7 | 2014-07-31 20:27:39 +0200 | [diff] [blame] | 170 | public void setNotification(Notification notification) { |
| 171 | mNotification = notification; |
| 172 | setContentDescription(notification); |
| 173 | } |
| 174 | |
Daniel Sandler | 05e2414 | 2011-11-10 11:56:49 -0500 | [diff] [blame] | 175 | public StatusBarIconView(Context context, AttributeSet attrs) { |
| 176 | super(context, attrs); |
Jason Monk | 3b23007 | 2015-05-29 11:11:29 -0400 | [diff] [blame] | 177 | mBlocked = false; |
Selim Cinek | 3e7592d | 2016-04-11 09:35:54 +0800 | [diff] [blame] | 178 | mAlwaysScaleIcon = true; |
| 179 | updateIconScale(); |
| 180 | mDensity = context.getResources().getDisplayMetrics().densityDpi; |
Daniel Sandler | 05e2414 | 2011-11-10 11:56:49 -0500 | [diff] [blame] | 181 | } |
| 182 | |
Joe Onorato | 0cbda99 | 2010-05-02 16:28:15 -0700 | [diff] [blame] | 183 | private static boolean streq(String a, String b) { |
Joe Onorato | 66d7d01 | 2010-05-14 10:05:10 -0700 | [diff] [blame] | 184 | if (a == b) { |
| 185 | return true; |
| 186 | } |
Joe Onorato | 0cbda99 | 2010-05-02 16:28:15 -0700 | [diff] [blame] | 187 | if (a == null && b != null) { |
| 188 | return false; |
| 189 | } |
| 190 | if (a != null && b == null) { |
| 191 | return false; |
| 192 | } |
| 193 | return a.equals(b); |
| 194 | } |
| 195 | |
Dan Sandler | d63f932 | 2015-05-06 15:18:49 -0400 | [diff] [blame] | 196 | public boolean equalIcons(Icon a, Icon b) { |
| 197 | if (a == b) return true; |
| 198 | if (a.getType() != b.getType()) return false; |
| 199 | switch (a.getType()) { |
| 200 | case Icon.TYPE_RESOURCE: |
| 201 | return a.getResPackage().equals(b.getResPackage()) && a.getResId() == b.getResId(); |
| 202 | case Icon.TYPE_URI: |
| 203 | return a.getUriString().equals(b.getUriString()); |
| 204 | default: |
| 205 | return false; |
| 206 | } |
| 207 | } |
Joe Onorato | 005847b | 2010-06-04 16:08:02 -0400 | [diff] [blame] | 208 | /** |
| 209 | * Returns whether the set succeeded. |
| 210 | */ |
| 211 | public boolean set(StatusBarIcon icon) { |
Dan Sandler | d63f932 | 2015-05-06 15:18:49 -0400 | [diff] [blame] | 212 | final boolean iconEquals = mIcon != null && equalIcons(mIcon.icon, icon.icon); |
Joe Onorato | 005847b | 2010-06-04 16:08:02 -0400 | [diff] [blame] | 213 | final boolean levelEquals = iconEquals |
| 214 | && mIcon.iconLevel == icon.iconLevel; |
| 215 | final boolean visibilityEquals = mIcon != null |
| 216 | && mIcon.visible == icon.visible; |
Joe Onorato | 6c01a11 | 2010-10-04 17:38:47 -0400 | [diff] [blame] | 217 | final boolean numberEquals = mIcon != null |
| 218 | && mIcon.number == icon.number; |
| 219 | mIcon = icon.clone(); |
Svetoslav Ganov | 6179ea3 | 2011-06-28 01:12:41 -0700 | [diff] [blame] | 220 | setContentDescription(icon.contentDescription); |
Joe Onorato | 005847b | 2010-06-04 16:08:02 -0400 | [diff] [blame] | 221 | if (!iconEquals) { |
Fabrice Di Meglio | 82fca5d | 2013-01-09 15:42:31 -0800 | [diff] [blame] | 222 | if (!updateDrawable(false /* no clear */)) return false; |
Joe Onorato | 0cbda99 | 2010-05-02 16:28:15 -0700 | [diff] [blame] | 223 | } |
Joe Onorato | 005847b | 2010-06-04 16:08:02 -0400 | [diff] [blame] | 224 | if (!levelEquals) { |
| 225 | setImageLevel(icon.iconLevel); |
Joe Onorato | 0cbda99 | 2010-05-02 16:28:15 -0700 | [diff] [blame] | 226 | } |
Daniel Sandler | 26c84b1 | 2011-07-27 00:09:40 -0400 | [diff] [blame] | 227 | |
Joe Onorato | 6c01a11 | 2010-10-04 17:38:47 -0400 | [diff] [blame] | 228 | if (!numberEquals) { |
John Spurlock | 0153478 | 2014-01-13 11:59:22 -0500 | [diff] [blame] | 229 | if (icon.number > 0 && getContext().getResources().getBoolean( |
Joe Onorato | 8595a3d | 2010-11-19 18:12:07 -0800 | [diff] [blame] | 230 | R.bool.config_statusBarShowNumber)) { |
Joe Onorato | 6c01a11 | 2010-10-04 17:38:47 -0400 | [diff] [blame] | 231 | if (mNumberBackground == null) { |
| 232 | mNumberBackground = getContext().getResources().getDrawable( |
| 233 | R.drawable.ic_notification_overlay); |
| 234 | } |
| 235 | placeNumber(); |
| 236 | } else { |
| 237 | mNumberBackground = null; |
| 238 | mNumberText = null; |
| 239 | } |
| 240 | invalidate(); |
| 241 | } |
Joe Onorato | 005847b | 2010-06-04 16:08:02 -0400 | [diff] [blame] | 242 | if (!visibilityEquals) { |
Jason Monk | 3b23007 | 2015-05-29 11:11:29 -0400 | [diff] [blame] | 243 | setVisibility(icon.visible && !mBlocked ? VISIBLE : GONE); |
Joe Onorato | 005847b | 2010-06-04 16:08:02 -0400 | [diff] [blame] | 244 | } |
Joe Onorato | 005847b | 2010-06-04 16:08:02 -0400 | [diff] [blame] | 245 | return true; |
Joe Onorato | 0cbda99 | 2010-05-02 16:28:15 -0700 | [diff] [blame] | 246 | } |
| 247 | |
Fabrice Di Meglio | 82fca5d | 2013-01-09 15:42:31 -0800 | [diff] [blame] | 248 | public void updateDrawable() { |
| 249 | updateDrawable(true /* with clear */); |
| 250 | } |
| 251 | |
| 252 | private boolean updateDrawable(boolean withClear) { |
Jorim Jaggi | 66ac133 | 2015-01-21 19:22:26 +0100 | [diff] [blame] | 253 | if (mIcon == null) { |
| 254 | return false; |
| 255 | } |
Fabrice Di Meglio | 82fca5d | 2013-01-09 15:42:31 -0800 | [diff] [blame] | 256 | Drawable drawable = getIcon(mIcon); |
| 257 | if (drawable == null) { |
John Spurlock | cd686b5 | 2013-06-05 10:13:46 -0400 | [diff] [blame] | 258 | Log.w(TAG, "No icon for slot " + mSlot); |
Fabrice Di Meglio | 82fca5d | 2013-01-09 15:42:31 -0800 | [diff] [blame] | 259 | return false; |
| 260 | } |
| 261 | if (withClear) { |
| 262 | setImageDrawable(null); |
| 263 | } |
| 264 | setImageDrawable(drawable); |
| 265 | return true; |
| 266 | } |
| 267 | |
Joe Onorato | f551054 | 2010-06-01 07:46:41 -0700 | [diff] [blame] | 268 | private Drawable getIcon(StatusBarIcon icon) { |
| 269 | return getIcon(getContext(), icon); |
| 270 | } |
| 271 | |
Joe Onorato | 0cbda99 | 2010-05-02 16:28:15 -0700 | [diff] [blame] | 272 | /** |
Dan Sandler | d63f932 | 2015-05-06 15:18:49 -0400 | [diff] [blame] | 273 | * Returns the right icon to use for this item |
John Spurlock | 209bede | 2013-07-17 12:23:27 -0400 | [diff] [blame] | 274 | * |
Dan Sandler | d63f932 | 2015-05-06 15:18:49 -0400 | [diff] [blame] | 275 | * @param context Context to use to get resources |
Joe Onorato | 0cbda99 | 2010-05-02 16:28:15 -0700 | [diff] [blame] | 276 | * @return Drawable for this item, or null if the package or item could not |
| 277 | * be found |
| 278 | */ |
Anthony Chen | 55e8e1e | 2016-01-08 10:31:46 -0800 | [diff] [blame] | 279 | public static Drawable getIcon(Context context, StatusBarIcon statusBarIcon) { |
| 280 | int userId = statusBarIcon.user.getIdentifier(); |
Dan Sandler | d63f932 | 2015-05-06 15:18:49 -0400 | [diff] [blame] | 281 | if (userId == UserHandle.USER_ALL) { |
Xiaohui Chen | 87d0e25 | 2015-07-30 15:38:16 -0700 | [diff] [blame] | 282 | userId = UserHandle.USER_SYSTEM; |
Joe Onorato | 0cbda99 | 2010-05-02 16:28:15 -0700 | [diff] [blame] | 283 | } |
Anthony Chen | 55e8e1e | 2016-01-08 10:31:46 -0800 | [diff] [blame] | 284 | |
| 285 | Drawable icon = statusBarIcon.icon.loadDrawableAsUser(context, userId); |
| 286 | |
| 287 | TypedValue typedValue = new TypedValue(); |
| 288 | context.getResources().getValue(R.dimen.status_bar_icon_scale_factor, typedValue, true); |
| 289 | float scaleFactor = typedValue.getFloat(); |
| 290 | |
| 291 | // No need to scale the icon, so return it as is. |
| 292 | if (scaleFactor == 1.f) { |
| 293 | return icon; |
| 294 | } |
| 295 | |
| 296 | return new ScalingDrawableWrapper(icon, scaleFactor); |
Joe Onorato | 0cbda99 | 2010-05-02 16:28:15 -0700 | [diff] [blame] | 297 | } |
Joe Onorato | b77f53b | 2010-05-28 19:59:51 -0400 | [diff] [blame] | 298 | |
| 299 | public StatusBarIcon getStatusBarIcon() { |
| 300 | return mIcon; |
| 301 | } |
Joe Onorato | f9ec03c | 2010-09-23 15:09:18 -0700 | [diff] [blame] | 302 | |
Svetoslav Ganov | 6179ea3 | 2011-06-28 01:12:41 -0700 | [diff] [blame] | 303 | @Override |
| 304 | public void onInitializeAccessibilityEvent(AccessibilityEvent event) { |
| 305 | super.onInitializeAccessibilityEvent(event); |
| 306 | if (mNotification != null) { |
| 307 | event.setParcelableData(mNotification); |
| 308 | } |
| 309 | } |
| 310 | |
| 311 | @Override |
Joe Onorato | 6c01a11 | 2010-10-04 17:38:47 -0400 | [diff] [blame] | 312 | protected void onSizeChanged(int w, int h, int oldw, int oldh) { |
| 313 | super.onSizeChanged(w, h, oldw, oldh); |
| 314 | if (mNumberBackground != null) { |
| 315 | placeNumber(); |
| 316 | } |
| 317 | } |
| 318 | |
Svetoslav Ganov | 6179ea3 | 2011-06-28 01:12:41 -0700 | [diff] [blame] | 319 | @Override |
Jorim Jaggi | 66ac133 | 2015-01-21 19:22:26 +0100 | [diff] [blame] | 320 | public void onRtlPropertiesChanged(int layoutDirection) { |
| 321 | super.onRtlPropertiesChanged(layoutDirection); |
| 322 | updateDrawable(); |
| 323 | } |
| 324 | |
| 325 | @Override |
Joe Onorato | 6c01a11 | 2010-10-04 17:38:47 -0400 | [diff] [blame] | 326 | protected void onDraw(Canvas canvas) { |
Selim Cinek | 49014f8 | 2016-11-04 14:55:30 -0700 | [diff] [blame^] | 327 | if (mIconAppearAmount > 0.0f) { |
| 328 | canvas.save(); |
| 329 | canvas.scale(mIconScale * mIconAppearAmount, mIconScale * mIconAppearAmount, |
| 330 | getWidth() / 2, getHeight() / 2); |
| 331 | super.onDraw(canvas); |
| 332 | canvas.restore(); |
| 333 | } |
Joe Onorato | 6c01a11 | 2010-10-04 17:38:47 -0400 | [diff] [blame] | 334 | |
| 335 | if (mNumberBackground != null) { |
| 336 | mNumberBackground.draw(canvas); |
| 337 | canvas.drawText(mNumberText, mNumberX, mNumberY, mNumberPain); |
| 338 | } |
Selim Cinek | 49014f8 | 2016-11-04 14:55:30 -0700 | [diff] [blame^] | 339 | if (mDotAppearAmount != 0.0f) { |
| 340 | float radius; |
| 341 | float alpha; |
| 342 | if (mDotAppearAmount <= 1.0f) { |
| 343 | radius = mDotRadius * mDotAppearAmount; |
| 344 | alpha = 1.0f; |
| 345 | } else { |
| 346 | float fadeOutAmount = mDotAppearAmount - 1.0f; |
| 347 | alpha = 1.0f - fadeOutAmount; |
| 348 | radius = NotificationUtils.interpolate(mDotRadius, getWidth() / 4, fadeOutAmount); |
| 349 | } |
| 350 | mDotPaint.setAlpha((int) (alpha * 255)); |
| 351 | canvas.drawCircle(getWidth() / 2, getHeight() / 2, radius, mDotPaint); |
| 352 | } |
Joe Onorato | 6c01a11 | 2010-10-04 17:38:47 -0400 | [diff] [blame] | 353 | } |
| 354 | |
Svetoslav Ganov | 6179ea3 | 2011-06-28 01:12:41 -0700 | [diff] [blame] | 355 | @Override |
Joe Onorato | f9ec03c | 2010-09-23 15:09:18 -0700 | [diff] [blame] | 356 | protected void debug(int depth) { |
| 357 | super.debug(depth); |
| 358 | Log.d("View", debugIndent(depth) + "slot=" + mSlot); |
| 359 | Log.d("View", debugIndent(depth) + "icon=" + mIcon); |
| 360 | } |
Joe Onorato | 6c01a11 | 2010-10-04 17:38:47 -0400 | [diff] [blame] | 361 | |
| 362 | void placeNumber() { |
Daniel Sandler | ebce011 | 2011-06-16 16:44:51 -0400 | [diff] [blame] | 363 | final String str; |
John Spurlock | 0153478 | 2014-01-13 11:59:22 -0500 | [diff] [blame] | 364 | final int tooBig = getContext().getResources().getInteger( |
Daniel Sandler | ebce011 | 2011-06-16 16:44:51 -0400 | [diff] [blame] | 365 | android.R.integer.status_bar_notification_info_maxnum); |
| 366 | if (mIcon.number > tooBig) { |
John Spurlock | 0153478 | 2014-01-13 11:59:22 -0500 | [diff] [blame] | 367 | str = getContext().getResources().getString( |
Daniel Sandler | ebce011 | 2011-06-16 16:44:51 -0400 | [diff] [blame] | 368 | android.R.string.status_bar_notification_info_overflow); |
| 369 | } else { |
| 370 | NumberFormat f = NumberFormat.getIntegerInstance(); |
| 371 | str = f.format(mIcon.number); |
| 372 | } |
| 373 | mNumberText = str; |
| 374 | |
Joe Onorato | 6c01a11 | 2010-10-04 17:38:47 -0400 | [diff] [blame] | 375 | final int w = getWidth(); |
| 376 | final int h = getHeight(); |
| 377 | final Rect r = new Rect(); |
| 378 | mNumberPain.getTextBounds(str, 0, str.length(), r); |
| 379 | final int tw = r.right - r.left; |
| 380 | final int th = r.bottom - r.top; |
| 381 | mNumberBackground.getPadding(r); |
| 382 | int dw = r.left + tw + r.right; |
| 383 | if (dw < mNumberBackground.getMinimumWidth()) { |
| 384 | dw = mNumberBackground.getMinimumWidth(); |
| 385 | } |
| 386 | mNumberX = w-r.right-((dw-r.right-r.left)/2); |
| 387 | int dh = r.top + th + r.bottom; |
| 388 | if (dh < mNumberBackground.getMinimumWidth()) { |
| 389 | dh = mNumberBackground.getMinimumWidth(); |
| 390 | } |
| 391 | mNumberY = h-r.bottom-((dh-r.top-th-r.bottom)/2); |
| 392 | mNumberBackground.setBounds(w-dw, h-dh, w, h); |
| 393 | } |
Svetoslav Ganov | 6179ea3 | 2011-06-28 01:12:41 -0700 | [diff] [blame] | 394 | |
| 395 | private void setContentDescription(Notification notification) { |
| 396 | if (notification != null) { |
Adrian Roos | eba0582 | 2016-04-22 17:09:27 -0700 | [diff] [blame] | 397 | String d = contentDescForNotification(mContext, notification); |
| 398 | if (!TextUtils.isEmpty(d)) { |
| 399 | setContentDescription(d); |
Svetoslav Ganov | 6179ea3 | 2011-06-28 01:12:41 -0700 | [diff] [blame] | 400 | } |
| 401 | } |
| 402 | } |
Daniel Sandler | 7579bca | 2011-08-18 15:47:26 -0400 | [diff] [blame] | 403 | |
| 404 | public String toString() { |
John Spurlock | 209bede | 2013-07-17 12:23:27 -0400 | [diff] [blame] | 405 | return "StatusBarIconView(slot=" + mSlot + " icon=" + mIcon |
Daniel Sandler | 7579bca | 2011-08-18 15:47:26 -0400 | [diff] [blame] | 406 | + " notification=" + mNotification + ")"; |
| 407 | } |
Jason Monk | 3b23007 | 2015-05-29 11:11:29 -0400 | [diff] [blame] | 408 | |
| 409 | public String getSlot() { |
| 410 | return mSlot; |
| 411 | } |
Adrian Roos | eba0582 | 2016-04-22 17:09:27 -0700 | [diff] [blame] | 412 | |
| 413 | |
| 414 | public static String contentDescForNotification(Context c, Notification n) { |
Adrian Roos | a8e18ef | 2016-05-26 17:17:02 -0700 | [diff] [blame] | 415 | String appName = ""; |
| 416 | try { |
| 417 | Notification.Builder builder = Notification.Builder.recoverBuilder(c, n); |
| 418 | appName = builder.loadHeaderAppName(); |
| 419 | } catch (RuntimeException e) { |
| 420 | Log.e(TAG, "Unable to recover builder", e); |
| 421 | // Trying to get the app name from the app info instead. |
| 422 | Parcelable appInfo = n.extras.getParcelable( |
| 423 | Notification.EXTRA_BUILDER_APPLICATION_INFO); |
| 424 | if (appInfo instanceof ApplicationInfo) { |
| 425 | appName = String.valueOf(((ApplicationInfo) appInfo).loadLabel( |
| 426 | c.getPackageManager())); |
| 427 | } |
| 428 | } |
Adrian Roos | 51548e6 | 2016-04-28 11:20:26 -0700 | [diff] [blame] | 429 | |
Julia Reynolds | 7f9ce78 | 2016-05-17 15:34:34 -0400 | [diff] [blame] | 430 | CharSequence title = n.extras.getCharSequence(Notification.EXTRA_TITLE); |
Adrian Roos | eba0582 | 2016-04-22 17:09:27 -0700 | [diff] [blame] | 431 | CharSequence ticker = n.tickerText; |
Adrian Roos | 51548e6 | 2016-04-28 11:20:26 -0700 | [diff] [blame] | 432 | |
| 433 | CharSequence desc = !TextUtils.isEmpty(ticker) ? ticker |
| 434 | : !TextUtils.isEmpty(title) ? title : ""; |
| 435 | |
| 436 | return c.getString(R.string.accessibility_desc_notification_icon, appName, desc); |
Adrian Roos | eba0582 | 2016-04-22 17:09:27 -0700 | [diff] [blame] | 437 | } |
Adrian Roos | 51548e6 | 2016-04-28 11:20:26 -0700 | [diff] [blame] | 438 | |
Selim Cinek | 49014f8 | 2016-11-04 14:55:30 -0700 | [diff] [blame^] | 439 | public void setIconTint(int iconTint) { |
| 440 | mDotPaint.setColor(iconTint); |
| 441 | } |
| 442 | |
| 443 | public void setVisibleState(int visibleState) { |
| 444 | if (visibleState != mVisibleState) { |
| 445 | mVisibleState = visibleState; |
| 446 | if (mIconAppearAnimator != null) { |
| 447 | mIconAppearAnimator.cancel(); |
| 448 | } |
| 449 | float targetAmount = 0.0f; |
| 450 | Interpolator interpolator = Interpolators.FAST_OUT_LINEAR_IN; |
| 451 | if (visibleState == STATE_ICON) { |
| 452 | targetAmount = 1.0f; |
| 453 | interpolator = Interpolators.LINEAR_OUT_SLOW_IN; |
| 454 | } |
| 455 | mIconAppearAnimator = ObjectAnimator.ofFloat(this, ICON_APPEAR_AMOUNT, |
| 456 | targetAmount); |
| 457 | mIconAppearAnimator.setInterpolator(interpolator); |
| 458 | mIconAppearAnimator.setDuration(100); |
| 459 | mIconAppearAnimator.addListener(new AnimatorListenerAdapter() { |
| 460 | @Override |
| 461 | public void onAnimationEnd(Animator animation) { |
| 462 | mIconAppearAnimator = null; |
| 463 | } |
| 464 | }); |
| 465 | mIconAppearAnimator.start(); |
| 466 | |
| 467 | if (mDotAnimator != null) { |
| 468 | mDotAnimator.cancel(); |
| 469 | } |
| 470 | targetAmount = visibleState == STATE_ICON ? 2.0f : 0.0f; |
| 471 | interpolator = Interpolators.FAST_OUT_LINEAR_IN; |
| 472 | if (visibleState == STATE_DOT) { |
| 473 | targetAmount = 1.0f; |
| 474 | interpolator = Interpolators.LINEAR_OUT_SLOW_IN; |
| 475 | } |
| 476 | mDotAnimator = ObjectAnimator.ofFloat(this, DOT_APPEAR_AMOUNG, |
| 477 | targetAmount); |
| 478 | mDotAnimator.setInterpolator(interpolator); |
| 479 | mDotAnimator.setDuration(100); |
| 480 | mDotAnimator.addListener(new AnimatorListenerAdapter() { |
| 481 | @Override |
| 482 | public void onAnimationEnd(Animator animation) { |
| 483 | mDotAnimator = null; |
| 484 | } |
| 485 | }); |
| 486 | mDotAnimator.start(); |
| 487 | } |
| 488 | } |
| 489 | |
| 490 | public void setIconAppearAmount(Float iconAppearAmount) { |
| 491 | mIconAppearAmount = iconAppearAmount; |
| 492 | invalidate(); |
| 493 | } |
| 494 | |
| 495 | public float getIconAppearAmount() { |
| 496 | return mIconAppearAmount; |
| 497 | } |
| 498 | |
| 499 | public int getVisibleState() { |
| 500 | return mVisibleState; |
| 501 | } |
| 502 | |
| 503 | public void setDotAppearAmount(float dotAppearAmount) { |
| 504 | mDotAppearAmount = dotAppearAmount; |
| 505 | invalidate(); |
| 506 | } |
| 507 | |
| 508 | public float getDotAppearAmount() { |
| 509 | return mDotAppearAmount; |
| 510 | } |
Joe Onorato | 0cbda99 | 2010-05-02 16:28:15 -0700 | [diff] [blame] | 511 | } |