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