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) { |
| 121 | Drawable drawable = getIcon(icon); |
| 122 | if (drawable == null) { |
Daniel Sandler | c6d29fc | 2012-02-25 00:33:12 -0500 | [diff] [blame] | 123 | Slog.w(TAG, "No icon for slot " + mSlot); |
Joe Onorato | 005847b | 2010-06-04 16:08:02 -0400 | [diff] [blame] | 124 | return false; |
Joe Onorato | 871bdb9 | 2010-05-24 18:36:53 -0400 | [diff] [blame] | 125 | } |
Joe Onorato | 005847b | 2010-06-04 16:08:02 -0400 | [diff] [blame] | 126 | setImageDrawable(drawable); |
Joe Onorato | 0cbda99 | 2010-05-02 16:28:15 -0700 | [diff] [blame] | 127 | } |
Joe Onorato | 005847b | 2010-06-04 16:08:02 -0400 | [diff] [blame] | 128 | if (!levelEquals) { |
| 129 | setImageLevel(icon.iconLevel); |
Joe Onorato | 0cbda99 | 2010-05-02 16:28:15 -0700 | [diff] [blame] | 130 | } |
Daniel Sandler | 26c84b1 | 2011-07-27 00:09:40 -0400 | [diff] [blame] | 131 | |
Joe Onorato | 6c01a11 | 2010-10-04 17:38:47 -0400 | [diff] [blame] | 132 | if (!numberEquals) { |
Joe Onorato | 8595a3d | 2010-11-19 18:12:07 -0800 | [diff] [blame] | 133 | if (icon.number > 0 && mContext.getResources().getBoolean( |
| 134 | R.bool.config_statusBarShowNumber)) { |
Joe Onorato | 6c01a11 | 2010-10-04 17:38:47 -0400 | [diff] [blame] | 135 | if (mNumberBackground == null) { |
| 136 | mNumberBackground = getContext().getResources().getDrawable( |
| 137 | R.drawable.ic_notification_overlay); |
| 138 | } |
| 139 | placeNumber(); |
| 140 | } else { |
| 141 | mNumberBackground = null; |
| 142 | mNumberText = null; |
| 143 | } |
| 144 | invalidate(); |
| 145 | } |
Joe Onorato | 005847b | 2010-06-04 16:08:02 -0400 | [diff] [blame] | 146 | if (!visibilityEquals) { |
| 147 | setVisibility(icon.visible ? VISIBLE : GONE); |
| 148 | } |
Joe Onorato | 005847b | 2010-06-04 16:08:02 -0400 | [diff] [blame] | 149 | return true; |
Joe Onorato | 0cbda99 | 2010-05-02 16:28:15 -0700 | [diff] [blame] | 150 | } |
| 151 | |
Joe Onorato | f551054 | 2010-06-01 07:46:41 -0700 | [diff] [blame] | 152 | private Drawable getIcon(StatusBarIcon icon) { |
| 153 | return getIcon(getContext(), icon); |
| 154 | } |
| 155 | |
Joe Onorato | 0cbda99 | 2010-05-02 16:28:15 -0700 | [diff] [blame] | 156 | /** |
| 157 | * Returns the right icon to use for this item, respecting the iconId and |
| 158 | * iconPackage (if set) |
| 159 | * |
| 160 | * @param context Context to use to get resources if iconPackage is not set |
| 161 | * @return Drawable for this item, or null if the package or item could not |
| 162 | * be found |
| 163 | */ |
Joe Onorato | f551054 | 2010-06-01 07:46:41 -0700 | [diff] [blame] | 164 | public static Drawable getIcon(Context context, StatusBarIcon icon) { |
Joe Onorato | 0cbda99 | 2010-05-02 16:28:15 -0700 | [diff] [blame] | 165 | Resources r = null; |
| 166 | |
| 167 | if (icon.iconPackage != null) { |
| 168 | try { |
Jeff Sharkey | ded653b | 2012-09-27 19:09:24 -0700 | [diff] [blame^] | 169 | int userId = icon.user.getIdentifier(); |
| 170 | if (userId == UserHandle.USER_ALL) { |
| 171 | userId = UserHandle.USER_OWNER; |
| 172 | } |
| 173 | r = context.getPackageManager() |
| 174 | .getResourcesForApplicationAsUser(icon.iconPackage, userId); |
Joe Onorato | 0cbda99 | 2010-05-02 16:28:15 -0700 | [diff] [blame] | 175 | } catch (PackageManager.NameNotFoundException ex) { |
Daniel Sandler | c6d29fc | 2012-02-25 00:33:12 -0500 | [diff] [blame] | 176 | Slog.e(TAG, "Icon package not found: " + icon.iconPackage); |
Joe Onorato | 0cbda99 | 2010-05-02 16:28:15 -0700 | [diff] [blame] | 177 | return null; |
| 178 | } |
| 179 | } else { |
| 180 | r = context.getResources(); |
| 181 | } |
| 182 | |
| 183 | if (icon.iconId == 0) { |
Joe Onorato | 0cbda99 | 2010-05-02 16:28:15 -0700 | [diff] [blame] | 184 | return null; |
| 185 | } |
| 186 | |
| 187 | try { |
| 188 | return r.getDrawable(icon.iconId); |
| 189 | } catch (RuntimeException e) { |
Daniel Sandler | c6d29fc | 2012-02-25 00:33:12 -0500 | [diff] [blame] | 190 | Slog.w(TAG, "Icon not found in " |
Joe Onorato | 0cbda99 | 2010-05-02 16:28:15 -0700 | [diff] [blame] | 191 | + (icon.iconPackage != null ? icon.iconId : "<system>") |
| 192 | + ": " + Integer.toHexString(icon.iconId)); |
| 193 | } |
| 194 | |
| 195 | return null; |
| 196 | } |
Joe Onorato | b77f53b | 2010-05-28 19:59:51 -0400 | [diff] [blame] | 197 | |
| 198 | public StatusBarIcon getStatusBarIcon() { |
| 199 | return mIcon; |
| 200 | } |
Joe Onorato | f9ec03c | 2010-09-23 15:09:18 -0700 | [diff] [blame] | 201 | |
Svetoslav Ganov | 6179ea3 | 2011-06-28 01:12:41 -0700 | [diff] [blame] | 202 | @Override |
| 203 | public void onInitializeAccessibilityEvent(AccessibilityEvent event) { |
| 204 | super.onInitializeAccessibilityEvent(event); |
| 205 | if (mNotification != null) { |
| 206 | event.setParcelableData(mNotification); |
| 207 | } |
| 208 | } |
| 209 | |
| 210 | @Override |
Joe Onorato | 6c01a11 | 2010-10-04 17:38:47 -0400 | [diff] [blame] | 211 | protected void onSizeChanged(int w, int h, int oldw, int oldh) { |
| 212 | super.onSizeChanged(w, h, oldw, oldh); |
| 213 | if (mNumberBackground != null) { |
| 214 | placeNumber(); |
| 215 | } |
| 216 | } |
| 217 | |
Svetoslav Ganov | 6179ea3 | 2011-06-28 01:12:41 -0700 | [diff] [blame] | 218 | @Override |
Joe Onorato | 6c01a11 | 2010-10-04 17:38:47 -0400 | [diff] [blame] | 219 | protected void onDraw(Canvas canvas) { |
| 220 | super.onDraw(canvas); |
| 221 | |
| 222 | if (mNumberBackground != null) { |
| 223 | mNumberBackground.draw(canvas); |
| 224 | canvas.drawText(mNumberText, mNumberX, mNumberY, mNumberPain); |
| 225 | } |
| 226 | } |
| 227 | |
Svetoslav Ganov | 6179ea3 | 2011-06-28 01:12:41 -0700 | [diff] [blame] | 228 | @Override |
Joe Onorato | f9ec03c | 2010-09-23 15:09:18 -0700 | [diff] [blame] | 229 | protected void debug(int depth) { |
| 230 | super.debug(depth); |
| 231 | Log.d("View", debugIndent(depth) + "slot=" + mSlot); |
| 232 | Log.d("View", debugIndent(depth) + "icon=" + mIcon); |
| 233 | } |
Joe Onorato | 6c01a11 | 2010-10-04 17:38:47 -0400 | [diff] [blame] | 234 | |
| 235 | void placeNumber() { |
Daniel Sandler | ebce011 | 2011-06-16 16:44:51 -0400 | [diff] [blame] | 236 | final String str; |
| 237 | final int tooBig = mContext.getResources().getInteger( |
| 238 | android.R.integer.status_bar_notification_info_maxnum); |
| 239 | if (mIcon.number > tooBig) { |
| 240 | str = mContext.getResources().getString( |
| 241 | android.R.string.status_bar_notification_info_overflow); |
| 242 | } else { |
| 243 | NumberFormat f = NumberFormat.getIntegerInstance(); |
| 244 | str = f.format(mIcon.number); |
| 245 | } |
| 246 | mNumberText = str; |
| 247 | |
Joe Onorato | 6c01a11 | 2010-10-04 17:38:47 -0400 | [diff] [blame] | 248 | final int w = getWidth(); |
| 249 | final int h = getHeight(); |
| 250 | final Rect r = new Rect(); |
| 251 | mNumberPain.getTextBounds(str, 0, str.length(), r); |
| 252 | final int tw = r.right - r.left; |
| 253 | final int th = r.bottom - r.top; |
| 254 | mNumberBackground.getPadding(r); |
| 255 | int dw = r.left + tw + r.right; |
| 256 | if (dw < mNumberBackground.getMinimumWidth()) { |
| 257 | dw = mNumberBackground.getMinimumWidth(); |
| 258 | } |
| 259 | mNumberX = w-r.right-((dw-r.right-r.left)/2); |
| 260 | int dh = r.top + th + r.bottom; |
| 261 | if (dh < mNumberBackground.getMinimumWidth()) { |
| 262 | dh = mNumberBackground.getMinimumWidth(); |
| 263 | } |
| 264 | mNumberY = h-r.bottom-((dh-r.top-th-r.bottom)/2); |
| 265 | mNumberBackground.setBounds(w-dw, h-dh, w, h); |
| 266 | } |
Svetoslav Ganov | 6179ea3 | 2011-06-28 01:12:41 -0700 | [diff] [blame] | 267 | |
| 268 | private void setContentDescription(Notification notification) { |
| 269 | if (notification != null) { |
| 270 | CharSequence tickerText = notification.tickerText; |
| 271 | if (!TextUtils.isEmpty(tickerText)) { |
| 272 | setContentDescription(tickerText); |
| 273 | } |
| 274 | } |
| 275 | } |
Daniel Sandler | 7579bca | 2011-08-18 15:47:26 -0400 | [diff] [blame] | 276 | |
| 277 | public String toString() { |
| 278 | return "StatusBarIconView(slot=" + mSlot + " icon=" + mIcon |
| 279 | + " notification=" + mNotification + ")"; |
| 280 | } |
Joe Onorato | 0cbda99 | 2010-05-02 16:28:15 -0700 | [diff] [blame] | 281 | } |