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