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