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 | |
Svetoslav Ganov | 6179ea3 | 2011-06-28 01:12:41 -0700 | [diff] [blame] | 19 | import android.app.Notification; |
Joe Onorato | 0cbda99 | 2010-05-02 16:28:15 -0700 | [diff] [blame] | 20 | import android.content.Context; |
Joe Onorato | 0cbda99 | 2010-05-02 16:28:15 -0700 | [diff] [blame] | 21 | import android.content.res.Resources; |
Joe Onorato | 0cbda99 | 2010-05-02 16:28:15 -0700 | [diff] [blame] | 22 | import android.graphics.Canvas; |
Joe Onorato | 6c01a11 | 2010-10-04 17:38:47 -0400 | [diff] [blame] | 23 | import android.graphics.Paint; |
| 24 | import android.graphics.Rect; |
John Spurlock | de84f0e | 2013-06-12 12:41:00 -0400 | [diff] [blame] | 25 | import android.graphics.drawable.Drawable; |
Dan Sandler | d63f932 | 2015-05-06 15:18:49 -0400 | [diff] [blame] | 26 | import android.graphics.drawable.Icon; |
Jeff Sharkey | ded653b | 2012-09-27 19:09:24 -0700 | [diff] [blame] | 27 | import android.os.UserHandle; |
Svetoslav Ganov | 6179ea3 | 2011-06-28 01:12:41 -0700 | [diff] [blame] | 28 | import android.text.TextUtils; |
Daniel Sandler | 05e2414 | 2011-11-10 11:56:49 -0500 | [diff] [blame] | 29 | import android.util.AttributeSet; |
Joe Onorato | f9ec03c | 2010-09-23 15:09:18 -0700 | [diff] [blame] | 30 | import android.util.Log; |
Anthony Chen | 55e8e1e | 2016-01-08 10:31:46 -0800 | [diff] [blame] | 31 | import android.util.TypedValue; |
Joe Onorato | 0cbda99 | 2010-05-02 16:28:15 -0700 | [diff] [blame] | 32 | import android.view.ViewDebug; |
Svetoslav Ganov | 6179ea3 | 2011-06-28 01:12:41 -0700 | [diff] [blame] | 33 | import android.view.accessibility.AccessibilityEvent; |
Winson | c0d7058 | 2016-01-29 10:24:39 -0800 | [diff] [blame^] | 34 | |
Joe Onorato | 0cbda99 | 2010-05-02 16:28:15 -0700 | [diff] [blame] | 35 | import com.android.internal.statusbar.StatusBarIcon; |
Joe Onorato | 6c01a11 | 2010-10-04 17:38:47 -0400 | [diff] [blame] | 36 | import com.android.systemui.R; |
| 37 | |
John Spurlock | de84f0e | 2013-06-12 12:41:00 -0400 | [diff] [blame] | 38 | import java.text.NumberFormat; |
| 39 | |
Joe Onorato | 0cbda99 | 2010-05-02 16:28:15 -0700 | [diff] [blame] | 40 | public class StatusBarIconView extends AnimatedImageView { |
| 41 | private static final String TAG = "StatusBarIconView"; |
| 42 | |
| 43 | private StatusBarIcon mIcon; |
| 44 | @ViewDebug.ExportedProperty private String mSlot; |
Joe Onorato | 6c01a11 | 2010-10-04 17:38:47 -0400 | [diff] [blame] | 45 | private Drawable mNumberBackground; |
| 46 | private Paint mNumberPain; |
| 47 | private int mNumberX; |
| 48 | private int mNumberY; |
| 49 | private String mNumberText; |
Svetoslav Ganov | 6179ea3 | 2011-06-28 01:12:41 -0700 | [diff] [blame] | 50 | private Notification mNotification; |
Jason Monk | 3b23007 | 2015-05-29 11:11:29 -0400 | [diff] [blame] | 51 | private final boolean mBlocked; |
Joe Onorato | 0cbda99 | 2010-05-02 16:28:15 -0700 | [diff] [blame] | 52 | |
John Spurlock | e6f0a71 | 2013-09-03 16:23:49 -0400 | [diff] [blame] | 53 | public StatusBarIconView(Context context, String slot, Notification notification) { |
Jason Monk | 3b23007 | 2015-05-29 11:11:29 -0400 | [diff] [blame] | 54 | this(context, slot, notification, false); |
| 55 | } |
| 56 | |
| 57 | public StatusBarIconView(Context context, String slot, Notification notification, |
| 58 | boolean blocked) { |
Joe Onorato | 0cbda99 | 2010-05-02 16:28:15 -0700 | [diff] [blame] | 59 | super(context); |
Joe Onorato | 6c01a11 | 2010-10-04 17:38:47 -0400 | [diff] [blame] | 60 | final Resources res = context.getResources(); |
Jason Monk | 3b23007 | 2015-05-29 11:11:29 -0400 | [diff] [blame] | 61 | mBlocked = blocked; |
Joe Onorato | 0cbda99 | 2010-05-02 16:28:15 -0700 | [diff] [blame] | 62 | mSlot = slot; |
Joe Onorato | 6c01a11 | 2010-10-04 17:38:47 -0400 | [diff] [blame] | 63 | mNumberPain = new Paint(); |
| 64 | mNumberPain.setTextAlign(Paint.Align.CENTER); |
Alan Viverette | 4a357cd | 2015-03-18 18:37:18 -0700 | [diff] [blame] | 65 | mNumberPain.setColor(context.getColor(R.drawable.notification_number_text_color)); |
Joe Onorato | 6c01a11 | 2010-10-04 17:38:47 -0400 | [diff] [blame] | 66 | mNumberPain.setAntiAlias(true); |
Christoph Studer | a0506e7 | 2014-07-31 20:27:39 +0200 | [diff] [blame] | 67 | setNotification(notification); |
Daniel Sandler | 26c84b1 | 2011-07-27 00:09:40 -0400 | [diff] [blame] | 68 | |
Daniel Sandler | 7579bca | 2011-08-18 15:47:26 -0400 | [diff] [blame] | 69 | // We do not resize and scale system icons (on the right), only notification icons (on the |
| 70 | // left). |
| 71 | if (notification != null) { |
| 72 | final int outerBounds = res.getDimensionPixelSize(R.dimen.status_bar_icon_size); |
| 73 | final int imageBounds = res.getDimensionPixelSize(R.dimen.status_bar_icon_drawing_size); |
| 74 | final float scale = (float)imageBounds / (float)outerBounds; |
| 75 | setScaleX(scale); |
| 76 | setScaleY(scale); |
Daniel Sandler | 7579bca | 2011-08-18 15:47:26 -0400 | [diff] [blame] | 77 | } |
Daniel Sandler | abff032 | 2011-09-27 11:19:34 -0400 | [diff] [blame] | 78 | |
Jason Monk | 66c89c1 | 2016-01-06 08:51:26 -0500 | [diff] [blame] | 79 | setScaleType(ScaleType.CENTER); |
Joe Onorato | 0cbda99 | 2010-05-02 16:28:15 -0700 | [diff] [blame] | 80 | } |
| 81 | |
Christoph Studer | a0506e7 | 2014-07-31 20:27:39 +0200 | [diff] [blame] | 82 | public void setNotification(Notification notification) { |
| 83 | mNotification = notification; |
| 84 | setContentDescription(notification); |
| 85 | } |
| 86 | |
Daniel Sandler | 05e2414 | 2011-11-10 11:56:49 -0500 | [diff] [blame] | 87 | public StatusBarIconView(Context context, AttributeSet attrs) { |
| 88 | super(context, attrs); |
Jason Monk | 3b23007 | 2015-05-29 11:11:29 -0400 | [diff] [blame] | 89 | mBlocked = false; |
Daniel Sandler | 05e2414 | 2011-11-10 11:56:49 -0500 | [diff] [blame] | 90 | final Resources res = context.getResources(); |
| 91 | final int outerBounds = res.getDimensionPixelSize(R.dimen.status_bar_icon_size); |
| 92 | final int imageBounds = res.getDimensionPixelSize(R.dimen.status_bar_icon_drawing_size); |
| 93 | final float scale = (float)imageBounds / (float)outerBounds; |
| 94 | setScaleX(scale); |
| 95 | setScaleY(scale); |
Daniel Sandler | 05e2414 | 2011-11-10 11:56:49 -0500 | [diff] [blame] | 96 | } |
| 97 | |
Joe Onorato | 0cbda99 | 2010-05-02 16:28:15 -0700 | [diff] [blame] | 98 | private static boolean streq(String a, String b) { |
Joe Onorato | 66d7d01 | 2010-05-14 10:05:10 -0700 | [diff] [blame] | 99 | if (a == b) { |
| 100 | return true; |
| 101 | } |
Joe Onorato | 0cbda99 | 2010-05-02 16:28:15 -0700 | [diff] [blame] | 102 | if (a == null && b != null) { |
| 103 | return false; |
| 104 | } |
| 105 | if (a != null && b == null) { |
| 106 | return false; |
| 107 | } |
| 108 | return a.equals(b); |
| 109 | } |
| 110 | |
Dan Sandler | d63f932 | 2015-05-06 15:18:49 -0400 | [diff] [blame] | 111 | public boolean equalIcons(Icon a, Icon b) { |
| 112 | if (a == b) return true; |
| 113 | if (a.getType() != b.getType()) return false; |
| 114 | switch (a.getType()) { |
| 115 | case Icon.TYPE_RESOURCE: |
| 116 | return a.getResPackage().equals(b.getResPackage()) && a.getResId() == b.getResId(); |
| 117 | case Icon.TYPE_URI: |
| 118 | return a.getUriString().equals(b.getUriString()); |
| 119 | default: |
| 120 | return false; |
| 121 | } |
| 122 | } |
Joe Onorato | 005847b | 2010-06-04 16:08:02 -0400 | [diff] [blame] | 123 | /** |
| 124 | * Returns whether the set succeeded. |
| 125 | */ |
| 126 | public boolean set(StatusBarIcon icon) { |
Dan Sandler | d63f932 | 2015-05-06 15:18:49 -0400 | [diff] [blame] | 127 | final boolean iconEquals = mIcon != null && equalIcons(mIcon.icon, icon.icon); |
Joe Onorato | 005847b | 2010-06-04 16:08:02 -0400 | [diff] [blame] | 128 | final boolean levelEquals = iconEquals |
| 129 | && mIcon.iconLevel == icon.iconLevel; |
| 130 | final boolean visibilityEquals = mIcon != null |
| 131 | && mIcon.visible == icon.visible; |
Joe Onorato | 6c01a11 | 2010-10-04 17:38:47 -0400 | [diff] [blame] | 132 | final boolean numberEquals = mIcon != null |
| 133 | && mIcon.number == icon.number; |
| 134 | mIcon = icon.clone(); |
Svetoslav Ganov | 6179ea3 | 2011-06-28 01:12:41 -0700 | [diff] [blame] | 135 | setContentDescription(icon.contentDescription); |
Joe Onorato | 005847b | 2010-06-04 16:08:02 -0400 | [diff] [blame] | 136 | if (!iconEquals) { |
Fabrice Di Meglio | 82fca5d | 2013-01-09 15:42:31 -0800 | [diff] [blame] | 137 | if (!updateDrawable(false /* no clear */)) return false; |
Joe Onorato | 0cbda99 | 2010-05-02 16:28:15 -0700 | [diff] [blame] | 138 | } |
Joe Onorato | 005847b | 2010-06-04 16:08:02 -0400 | [diff] [blame] | 139 | if (!levelEquals) { |
| 140 | setImageLevel(icon.iconLevel); |
Joe Onorato | 0cbda99 | 2010-05-02 16:28:15 -0700 | [diff] [blame] | 141 | } |
Daniel Sandler | 26c84b1 | 2011-07-27 00:09:40 -0400 | [diff] [blame] | 142 | |
Joe Onorato | 6c01a11 | 2010-10-04 17:38:47 -0400 | [diff] [blame] | 143 | if (!numberEquals) { |
John Spurlock | 0153478 | 2014-01-13 11:59:22 -0500 | [diff] [blame] | 144 | if (icon.number > 0 && getContext().getResources().getBoolean( |
Joe Onorato | 8595a3d | 2010-11-19 18:12:07 -0800 | [diff] [blame] | 145 | R.bool.config_statusBarShowNumber)) { |
Joe Onorato | 6c01a11 | 2010-10-04 17:38:47 -0400 | [diff] [blame] | 146 | if (mNumberBackground == null) { |
| 147 | mNumberBackground = getContext().getResources().getDrawable( |
| 148 | R.drawable.ic_notification_overlay); |
| 149 | } |
| 150 | placeNumber(); |
| 151 | } else { |
| 152 | mNumberBackground = null; |
| 153 | mNumberText = null; |
| 154 | } |
| 155 | invalidate(); |
| 156 | } |
Joe Onorato | 005847b | 2010-06-04 16:08:02 -0400 | [diff] [blame] | 157 | if (!visibilityEquals) { |
Jason Monk | 3b23007 | 2015-05-29 11:11:29 -0400 | [diff] [blame] | 158 | setVisibility(icon.visible && !mBlocked ? VISIBLE : GONE); |
Joe Onorato | 005847b | 2010-06-04 16:08:02 -0400 | [diff] [blame] | 159 | } |
Joe Onorato | 005847b | 2010-06-04 16:08:02 -0400 | [diff] [blame] | 160 | return true; |
Joe Onorato | 0cbda99 | 2010-05-02 16:28:15 -0700 | [diff] [blame] | 161 | } |
| 162 | |
Fabrice Di Meglio | 82fca5d | 2013-01-09 15:42:31 -0800 | [diff] [blame] | 163 | public void updateDrawable() { |
| 164 | updateDrawable(true /* with clear */); |
| 165 | } |
| 166 | |
| 167 | private boolean updateDrawable(boolean withClear) { |
Jorim Jaggi | 66ac133 | 2015-01-21 19:22:26 +0100 | [diff] [blame] | 168 | if (mIcon == null) { |
| 169 | return false; |
| 170 | } |
Fabrice Di Meglio | 82fca5d | 2013-01-09 15:42:31 -0800 | [diff] [blame] | 171 | Drawable drawable = getIcon(mIcon); |
| 172 | if (drawable == null) { |
John Spurlock | cd686b5 | 2013-06-05 10:13:46 -0400 | [diff] [blame] | 173 | Log.w(TAG, "No icon for slot " + mSlot); |
Fabrice Di Meglio | 82fca5d | 2013-01-09 15:42:31 -0800 | [diff] [blame] | 174 | return false; |
| 175 | } |
| 176 | if (withClear) { |
| 177 | setImageDrawable(null); |
| 178 | } |
| 179 | setImageDrawable(drawable); |
| 180 | return true; |
| 181 | } |
| 182 | |
Joe Onorato | f551054 | 2010-06-01 07:46:41 -0700 | [diff] [blame] | 183 | private Drawable getIcon(StatusBarIcon icon) { |
| 184 | return getIcon(getContext(), icon); |
| 185 | } |
| 186 | |
Joe Onorato | 0cbda99 | 2010-05-02 16:28:15 -0700 | [diff] [blame] | 187 | /** |
Dan Sandler | d63f932 | 2015-05-06 15:18:49 -0400 | [diff] [blame] | 188 | * Returns the right icon to use for this item |
John Spurlock | 209bede | 2013-07-17 12:23:27 -0400 | [diff] [blame] | 189 | * |
Dan Sandler | d63f932 | 2015-05-06 15:18:49 -0400 | [diff] [blame] | 190 | * @param context Context to use to get resources |
Joe Onorato | 0cbda99 | 2010-05-02 16:28:15 -0700 | [diff] [blame] | 191 | * @return Drawable for this item, or null if the package or item could not |
| 192 | * be found |
| 193 | */ |
Anthony Chen | 55e8e1e | 2016-01-08 10:31:46 -0800 | [diff] [blame] | 194 | public static Drawable getIcon(Context context, StatusBarIcon statusBarIcon) { |
| 195 | int userId = statusBarIcon.user.getIdentifier(); |
Dan Sandler | d63f932 | 2015-05-06 15:18:49 -0400 | [diff] [blame] | 196 | if (userId == UserHandle.USER_ALL) { |
Xiaohui Chen | 87d0e25 | 2015-07-30 15:38:16 -0700 | [diff] [blame] | 197 | userId = UserHandle.USER_SYSTEM; |
Joe Onorato | 0cbda99 | 2010-05-02 16:28:15 -0700 | [diff] [blame] | 198 | } |
Anthony Chen | 55e8e1e | 2016-01-08 10:31:46 -0800 | [diff] [blame] | 199 | |
| 200 | Drawable icon = statusBarIcon.icon.loadDrawableAsUser(context, userId); |
| 201 | |
| 202 | TypedValue typedValue = new TypedValue(); |
| 203 | context.getResources().getValue(R.dimen.status_bar_icon_scale_factor, typedValue, true); |
| 204 | float scaleFactor = typedValue.getFloat(); |
| 205 | |
| 206 | // No need to scale the icon, so return it as is. |
| 207 | if (scaleFactor == 1.f) { |
| 208 | return icon; |
| 209 | } |
| 210 | |
| 211 | return new ScalingDrawableWrapper(icon, scaleFactor); |
Joe Onorato | 0cbda99 | 2010-05-02 16:28:15 -0700 | [diff] [blame] | 212 | } |
Joe Onorato | b77f53b | 2010-05-28 19:59:51 -0400 | [diff] [blame] | 213 | |
| 214 | public StatusBarIcon getStatusBarIcon() { |
| 215 | return mIcon; |
| 216 | } |
Joe Onorato | f9ec03c | 2010-09-23 15:09:18 -0700 | [diff] [blame] | 217 | |
Svetoslav Ganov | 6179ea3 | 2011-06-28 01:12:41 -0700 | [diff] [blame] | 218 | @Override |
| 219 | public void onInitializeAccessibilityEvent(AccessibilityEvent event) { |
| 220 | super.onInitializeAccessibilityEvent(event); |
| 221 | if (mNotification != null) { |
| 222 | event.setParcelableData(mNotification); |
| 223 | } |
| 224 | } |
| 225 | |
| 226 | @Override |
Joe Onorato | 6c01a11 | 2010-10-04 17:38:47 -0400 | [diff] [blame] | 227 | protected void onSizeChanged(int w, int h, int oldw, int oldh) { |
| 228 | super.onSizeChanged(w, h, oldw, oldh); |
| 229 | if (mNumberBackground != null) { |
| 230 | placeNumber(); |
| 231 | } |
| 232 | } |
| 233 | |
Svetoslav Ganov | 6179ea3 | 2011-06-28 01:12:41 -0700 | [diff] [blame] | 234 | @Override |
Jorim Jaggi | 66ac133 | 2015-01-21 19:22:26 +0100 | [diff] [blame] | 235 | public void onRtlPropertiesChanged(int layoutDirection) { |
| 236 | super.onRtlPropertiesChanged(layoutDirection); |
| 237 | updateDrawable(); |
| 238 | } |
| 239 | |
| 240 | @Override |
Joe Onorato | 6c01a11 | 2010-10-04 17:38:47 -0400 | [diff] [blame] | 241 | protected void onDraw(Canvas canvas) { |
| 242 | super.onDraw(canvas); |
| 243 | |
| 244 | if (mNumberBackground != null) { |
| 245 | mNumberBackground.draw(canvas); |
| 246 | canvas.drawText(mNumberText, mNumberX, mNumberY, mNumberPain); |
| 247 | } |
| 248 | } |
| 249 | |
Svetoslav Ganov | 6179ea3 | 2011-06-28 01:12:41 -0700 | [diff] [blame] | 250 | @Override |
Joe Onorato | f9ec03c | 2010-09-23 15:09:18 -0700 | [diff] [blame] | 251 | protected void debug(int depth) { |
| 252 | super.debug(depth); |
| 253 | Log.d("View", debugIndent(depth) + "slot=" + mSlot); |
| 254 | Log.d("View", debugIndent(depth) + "icon=" + mIcon); |
| 255 | } |
Joe Onorato | 6c01a11 | 2010-10-04 17:38:47 -0400 | [diff] [blame] | 256 | |
| 257 | void placeNumber() { |
Daniel Sandler | ebce011 | 2011-06-16 16:44:51 -0400 | [diff] [blame] | 258 | final String str; |
John Spurlock | 0153478 | 2014-01-13 11:59:22 -0500 | [diff] [blame] | 259 | final int tooBig = getContext().getResources().getInteger( |
Daniel Sandler | ebce011 | 2011-06-16 16:44:51 -0400 | [diff] [blame] | 260 | android.R.integer.status_bar_notification_info_maxnum); |
| 261 | if (mIcon.number > tooBig) { |
John Spurlock | 0153478 | 2014-01-13 11:59:22 -0500 | [diff] [blame] | 262 | str = getContext().getResources().getString( |
Daniel Sandler | ebce011 | 2011-06-16 16:44:51 -0400 | [diff] [blame] | 263 | android.R.string.status_bar_notification_info_overflow); |
| 264 | } else { |
| 265 | NumberFormat f = NumberFormat.getIntegerInstance(); |
| 266 | str = f.format(mIcon.number); |
| 267 | } |
| 268 | mNumberText = str; |
| 269 | |
Joe Onorato | 6c01a11 | 2010-10-04 17:38:47 -0400 | [diff] [blame] | 270 | final int w = getWidth(); |
| 271 | final int h = getHeight(); |
| 272 | final Rect r = new Rect(); |
| 273 | mNumberPain.getTextBounds(str, 0, str.length(), r); |
| 274 | final int tw = r.right - r.left; |
| 275 | final int th = r.bottom - r.top; |
| 276 | mNumberBackground.getPadding(r); |
| 277 | int dw = r.left + tw + r.right; |
| 278 | if (dw < mNumberBackground.getMinimumWidth()) { |
| 279 | dw = mNumberBackground.getMinimumWidth(); |
| 280 | } |
| 281 | mNumberX = w-r.right-((dw-r.right-r.left)/2); |
| 282 | int dh = r.top + th + r.bottom; |
| 283 | if (dh < mNumberBackground.getMinimumWidth()) { |
| 284 | dh = mNumberBackground.getMinimumWidth(); |
| 285 | } |
| 286 | mNumberY = h-r.bottom-((dh-r.top-th-r.bottom)/2); |
| 287 | mNumberBackground.setBounds(w-dw, h-dh, w, h); |
| 288 | } |
Svetoslav Ganov | 6179ea3 | 2011-06-28 01:12:41 -0700 | [diff] [blame] | 289 | |
| 290 | private void setContentDescription(Notification notification) { |
| 291 | if (notification != null) { |
| 292 | CharSequence tickerText = notification.tickerText; |
| 293 | if (!TextUtils.isEmpty(tickerText)) { |
| 294 | setContentDescription(tickerText); |
| 295 | } |
| 296 | } |
| 297 | } |
Daniel Sandler | 7579bca | 2011-08-18 15:47:26 -0400 | [diff] [blame] | 298 | |
| 299 | public String toString() { |
John Spurlock | 209bede | 2013-07-17 12:23:27 -0400 | [diff] [blame] | 300 | return "StatusBarIconView(slot=" + mSlot + " icon=" + mIcon |
Daniel Sandler | 7579bca | 2011-08-18 15:47:26 -0400 | [diff] [blame] | 301 | + " notification=" + mNotification + ")"; |
| 302 | } |
Jason Monk | 3b23007 | 2015-05-29 11:11:29 -0400 | [diff] [blame] | 303 | |
| 304 | public String getSlot() { |
| 305 | return mSlot; |
| 306 | } |
Joe Onorato | 0cbda99 | 2010-05-02 16:28:15 -0700 | [diff] [blame] | 307 | } |