blob: a90eb3fa9217552c5d453f17f65ad50b3ee867a5 [file] [log] [blame]
Joe Onorato0cbda992010-05-02 16:28:15 -07001/*
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 Onorato79de0c52010-05-26 17:03:26 -040017package com.android.systemui.statusbar;
Joe Onorato0cbda992010-05-02 16:28:15 -070018
Svetoslav Ganov6179ea32011-06-28 01:12:41 -070019import android.app.Notification;
Joe Onorato0cbda992010-05-02 16:28:15 -070020import android.content.Context;
21import android.content.pm.PackageManager;
22import android.content.res.Resources;
23import android.graphics.drawable.Drawable;
24import android.graphics.Canvas;
Joe Onorato6c01a112010-10-04 17:38:47 -040025import android.graphics.Paint;
26import android.graphics.Rect;
Svetoslav Ganov6179ea32011-06-28 01:12:41 -070027import android.text.TextUtils;
Joe Onorato0cbda992010-05-02 16:28:15 -070028import android.util.Slog;
Joe Onoratof9ec03c2010-09-23 15:09:18 -070029import android.util.Log;
Joe Onorato0cbda992010-05-02 16:28:15 -070030import android.view.ViewDebug;
Svetoslav Ganov6179ea32011-06-28 01:12:41 -070031import android.view.accessibility.AccessibilityEvent;
Joe Onorato0cbda992010-05-02 16:28:15 -070032
Daniel Sandlerebce0112011-06-16 16:44:51 -040033import java.text.NumberFormat;
34
Joe Onorato0cbda992010-05-02 16:28:15 -070035import com.android.internal.statusbar.StatusBarIcon;
36
Joe Onorato6c01a112010-10-04 17:38:47 -040037import com.android.systemui.R;
38
Joe Onorato0cbda992010-05-02 16:28:15 -070039public class StatusBarIconView extends AnimatedImageView {
40 private static final String TAG = "StatusBarIconView";
41
42 private StatusBarIcon mIcon;
43 @ViewDebug.ExportedProperty private String mSlot;
Joe Onorato6c01a112010-10-04 17:38:47 -040044 private Drawable mNumberBackground;
45 private Paint mNumberPain;
46 private int mNumberX;
47 private int mNumberY;
48 private String mNumberText;
Svetoslav Ganov6179ea32011-06-28 01:12:41 -070049 private Notification mNotification;
Joe Onorato0cbda992010-05-02 16:28:15 -070050
Svetoslav Ganov6179ea32011-06-28 01:12:41 -070051 public StatusBarIconView(Context context, String slot, Notification notification) {
Joe Onorato0cbda992010-05-02 16:28:15 -070052 super(context);
Joe Onorato6c01a112010-10-04 17:38:47 -040053 final Resources res = context.getResources();
Joe Onorato0cbda992010-05-02 16:28:15 -070054 mSlot = slot;
Joe Onorato6c01a112010-10-04 17:38:47 -040055 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 Ganov6179ea32011-06-28 01:12:41 -070059 mNotification = notification;
60 setContentDescription(notification);
Daniel Sandler26c84b12011-07-27 00:09:40 -040061
Daniel Sandler7579bca2011-08-18 15:47:26 -040062 // 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 Onorato0cbda992010-05-02 16:28:15 -070073 }
74
75 private static boolean streq(String a, String b) {
Joe Onorato66d7d012010-05-14 10:05:10 -070076 if (a == b) {
77 return true;
78 }
Joe Onorato0cbda992010-05-02 16:28:15 -070079 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 Onorato005847b2010-06-04 16:08:02 -040088 /**
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 Onorato6c01a112010-10-04 17:38:47 -040099 final boolean numberEquals = mIcon != null
100 && mIcon.number == icon.number;
101 mIcon = icon.clone();
Svetoslav Ganov6179ea32011-06-28 01:12:41 -0700102 setContentDescription(icon.contentDescription);
Joe Onorato005847b2010-06-04 16:08:02 -0400103 if (!iconEquals) {
104 Drawable drawable = getIcon(icon);
105 if (drawable == null) {
Joe Onoratofd52b182010-11-10 18:00:52 -0800106 Slog.w(StatusBar.TAG, "No icon for slot " + mSlot);
Joe Onorato005847b2010-06-04 16:08:02 -0400107 return false;
Joe Onorato871bdb92010-05-24 18:36:53 -0400108 }
Joe Onorato005847b2010-06-04 16:08:02 -0400109 setImageDrawable(drawable);
Joe Onorato0cbda992010-05-02 16:28:15 -0700110 }
Joe Onorato005847b2010-06-04 16:08:02 -0400111 if (!levelEquals) {
112 setImageLevel(icon.iconLevel);
Joe Onorato0cbda992010-05-02 16:28:15 -0700113 }
Daniel Sandler26c84b12011-07-27 00:09:40 -0400114
Joe Onorato6c01a112010-10-04 17:38:47 -0400115 if (!numberEquals) {
Joe Onorato8595a3d2010-11-19 18:12:07 -0800116 if (icon.number > 0 && mContext.getResources().getBoolean(
117 R.bool.config_statusBarShowNumber)) {
Joe Onorato6c01a112010-10-04 17:38:47 -0400118 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 Onorato005847b2010-06-04 16:08:02 -0400129 if (!visibilityEquals) {
130 setVisibility(icon.visible ? VISIBLE : GONE);
131 }
Joe Onorato005847b2010-06-04 16:08:02 -0400132 return true;
Joe Onorato0cbda992010-05-02 16:28:15 -0700133 }
134
Joe Onoratof5510542010-06-01 07:46:41 -0700135 private Drawable getIcon(StatusBarIcon icon) {
136 return getIcon(getContext(), icon);
137 }
138
Joe Onorato0cbda992010-05-02 16:28:15 -0700139 /**
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 Onoratof5510542010-06-01 07:46:41 -0700147 public static Drawable getIcon(Context context, StatusBarIcon icon) {
Joe Onorato0cbda992010-05-02 16:28:15 -0700148 Resources r = null;
149
150 if (icon.iconPackage != null) {
151 try {
152 r = context.getPackageManager().getResourcesForApplication(icon.iconPackage);
153 } catch (PackageManager.NameNotFoundException ex) {
Joe Onoratofd52b182010-11-10 18:00:52 -0800154 Slog.e(StatusBar.TAG, "Icon package not found: " + icon.iconPackage);
Joe Onorato0cbda992010-05-02 16:28:15 -0700155 return null;
156 }
157 } else {
158 r = context.getResources();
159 }
160
161 if (icon.iconId == 0) {
Joe Onorato0cbda992010-05-02 16:28:15 -0700162 return null;
163 }
164
165 try {
166 return r.getDrawable(icon.iconId);
167 } catch (RuntimeException e) {
Joe Onoratofd52b182010-11-10 18:00:52 -0800168 Slog.w(StatusBar.TAG, "Icon not found in "
Joe Onorato0cbda992010-05-02 16:28:15 -0700169 + (icon.iconPackage != null ? icon.iconId : "<system>")
170 + ": " + Integer.toHexString(icon.iconId));
171 }
172
173 return null;
174 }
Joe Onoratob77f53b2010-05-28 19:59:51 -0400175
176 public StatusBarIcon getStatusBarIcon() {
177 return mIcon;
178 }
Joe Onoratof9ec03c2010-09-23 15:09:18 -0700179
Svetoslav Ganov6179ea32011-06-28 01:12:41 -0700180 @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 Onorato6c01a112010-10-04 17:38:47 -0400189 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 Ganov6179ea32011-06-28 01:12:41 -0700196 @Override
Joe Onorato6c01a112010-10-04 17:38:47 -0400197 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 Ganov6179ea32011-06-28 01:12:41 -0700206 @Override
Joe Onoratof9ec03c2010-09-23 15:09:18 -0700207 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 Onorato6c01a112010-10-04 17:38:47 -0400212
213 void placeNumber() {
Daniel Sandlerebce0112011-06-16 16:44:51 -0400214 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 Onorato6c01a112010-10-04 17:38:47 -0400226 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 Ganov6179ea32011-06-28 01:12:41 -0700245
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 Sandler7579bca2011-08-18 15:47:26 -0400254
255 public String toString() {
256 return "StatusBarIconView(slot=" + mSlot + " icon=" + mIcon
257 + " notification=" + mNotification + ")";
258 }
Joe Onorato0cbda992010-05-02 16:28:15 -0700259}