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