blob: 39d56a42869ae9830ee7297d24e8f26bfa750aea [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;
Jeff Sharkeyded653b2012-09-27 19:09:24 -070027import android.os.UserHandle;
Svetoslav Ganov6179ea32011-06-28 01:12:41 -070028import android.text.TextUtils;
Daniel Sandler05e24142011-11-10 11:56:49 -050029import android.util.AttributeSet;
Joe Onorato0cbda992010-05-02 16:28:15 -070030import android.util.Slog;
Joe Onoratof9ec03c2010-09-23 15:09:18 -070031import android.util.Log;
Joe Onorato0cbda992010-05-02 16:28:15 -070032import android.view.ViewDebug;
Svetoslav Ganov6179ea32011-06-28 01:12:41 -070033import android.view.accessibility.AccessibilityEvent;
Daniel Sandlerabff0322011-09-27 11:19:34 -040034import android.widget.ImageView;
Joe Onorato0cbda992010-05-02 16:28:15 -070035
Daniel Sandlerebce0112011-06-16 16:44:51 -040036import java.text.NumberFormat;
37
Joe Onorato0cbda992010-05-02 16:28:15 -070038import com.android.internal.statusbar.StatusBarIcon;
39
Joe Onorato6c01a112010-10-04 17:38:47 -040040import com.android.systemui.R;
41
Joe Onorato0cbda992010-05-02 16:28:15 -070042public class StatusBarIconView extends AnimatedImageView {
43 private static final String TAG = "StatusBarIconView";
44
45 private StatusBarIcon mIcon;
46 @ViewDebug.ExportedProperty private String mSlot;
Joe Onorato6c01a112010-10-04 17:38:47 -040047 private Drawable mNumberBackground;
48 private Paint mNumberPain;
49 private int mNumberX;
50 private int mNumberY;
51 private String mNumberText;
Svetoslav Ganov6179ea32011-06-28 01:12:41 -070052 private Notification mNotification;
Joe Onorato0cbda992010-05-02 16:28:15 -070053
Svetoslav Ganov6179ea32011-06-28 01:12:41 -070054 public StatusBarIconView(Context context, String slot, Notification notification) {
Joe Onorato0cbda992010-05-02 16:28:15 -070055 super(context);
Joe Onorato6c01a112010-10-04 17:38:47 -040056 final Resources res = context.getResources();
Joe Onorato0cbda992010-05-02 16:28:15 -070057 mSlot = slot;
Joe Onorato6c01a112010-10-04 17:38:47 -040058 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 Ganov6179ea32011-06-28 01:12:41 -070062 mNotification = notification;
63 setContentDescription(notification);
Daniel Sandler26c84b12011-07-27 00:09:40 -040064
Daniel Sandler7579bca2011-08-18 15:47:26 -040065 // 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 Sandlerabff0322011-09-27 11:19:34 -040076
77 setScaleType(ImageView.ScaleType.CENTER);
Joe Onorato0cbda992010-05-02 16:28:15 -070078 }
79
Daniel Sandler05e24142011-11-10 11:56:49 -050080 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 Onorato0cbda992010-05-02 16:28:15 -070092 private static boolean streq(String a, String b) {
Joe Onorato66d7d012010-05-14 10:05:10 -070093 if (a == b) {
94 return true;
95 }
Joe Onorato0cbda992010-05-02 16:28:15 -070096 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 Onorato005847b2010-06-04 16:08:02 -0400105 /**
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 Onorato6c01a112010-10-04 17:38:47 -0400116 final boolean numberEquals = mIcon != null
117 && mIcon.number == icon.number;
118 mIcon = icon.clone();
Svetoslav Ganov6179ea32011-06-28 01:12:41 -0700119 setContentDescription(icon.contentDescription);
Joe Onorato005847b2010-06-04 16:08:02 -0400120 if (!iconEquals) {
Fabrice Di Meglio82fca5d2013-01-09 15:42:31 -0800121 if (!updateDrawable(false /* no clear */)) return false;
Joe Onorato0cbda992010-05-02 16:28:15 -0700122 }
Joe Onorato005847b2010-06-04 16:08:02 -0400123 if (!levelEquals) {
124 setImageLevel(icon.iconLevel);
Joe Onorato0cbda992010-05-02 16:28:15 -0700125 }
Daniel Sandler26c84b12011-07-27 00:09:40 -0400126
Joe Onorato6c01a112010-10-04 17:38:47 -0400127 if (!numberEquals) {
Joe Onorato8595a3d2010-11-19 18:12:07 -0800128 if (icon.number > 0 && mContext.getResources().getBoolean(
129 R.bool.config_statusBarShowNumber)) {
Joe Onorato6c01a112010-10-04 17:38:47 -0400130 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 Onorato005847b2010-06-04 16:08:02 -0400141 if (!visibilityEquals) {
142 setVisibility(icon.visible ? VISIBLE : GONE);
143 }
Joe Onorato005847b2010-06-04 16:08:02 -0400144 return true;
Joe Onorato0cbda992010-05-02 16:28:15 -0700145 }
146
Fabrice Di Meglio82fca5d2013-01-09 15:42:31 -0800147 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 Onoratof5510542010-06-01 07:46:41 -0700164 private Drawable getIcon(StatusBarIcon icon) {
165 return getIcon(getContext(), icon);
166 }
167
Joe Onorato0cbda992010-05-02 16:28:15 -0700168 /**
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 Onoratof5510542010-06-01 07:46:41 -0700176 public static Drawable getIcon(Context context, StatusBarIcon icon) {
Joe Onorato0cbda992010-05-02 16:28:15 -0700177 Resources r = null;
178
179 if (icon.iconPackage != null) {
180 try {
Jeff Sharkeyded653b2012-09-27 19:09:24 -0700181 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 Onorato0cbda992010-05-02 16:28:15 -0700187 } catch (PackageManager.NameNotFoundException ex) {
Daniel Sandlerc6d29fc2012-02-25 00:33:12 -0500188 Slog.e(TAG, "Icon package not found: " + icon.iconPackage);
Joe Onorato0cbda992010-05-02 16:28:15 -0700189 return null;
190 }
191 } else {
192 r = context.getResources();
193 }
194
195 if (icon.iconId == 0) {
Joe Onorato0cbda992010-05-02 16:28:15 -0700196 return null;
197 }
198
199 try {
200 return r.getDrawable(icon.iconId);
201 } catch (RuntimeException e) {
Daniel Sandlerc6d29fc2012-02-25 00:33:12 -0500202 Slog.w(TAG, "Icon not found in "
Joe Onorato0cbda992010-05-02 16:28:15 -0700203 + (icon.iconPackage != null ? icon.iconId : "<system>")
204 + ": " + Integer.toHexString(icon.iconId));
205 }
206
207 return null;
208 }
Joe Onoratob77f53b2010-05-28 19:59:51 -0400209
210 public StatusBarIcon getStatusBarIcon() {
211 return mIcon;
212 }
Joe Onoratof9ec03c2010-09-23 15:09:18 -0700213
Svetoslav Ganov6179ea32011-06-28 01:12:41 -0700214 @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 Onorato6c01a112010-10-04 17:38:47 -0400223 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 Ganov6179ea32011-06-28 01:12:41 -0700230 @Override
Joe Onorato6c01a112010-10-04 17:38:47 -0400231 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 Ganov6179ea32011-06-28 01:12:41 -0700240 @Override
Joe Onoratof9ec03c2010-09-23 15:09:18 -0700241 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 Onorato6c01a112010-10-04 17:38:47 -0400246
247 void placeNumber() {
Daniel Sandlerebce0112011-06-16 16:44:51 -0400248 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 Onorato6c01a112010-10-04 17:38:47 -0400260 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 Ganov6179ea32011-06-28 01:12:41 -0700279
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 Sandler7579bca2011-08-18 15:47:26 -0400288
289 public String toString() {
290 return "StatusBarIconView(slot=" + mSlot + " icon=" + mIcon
291 + " notification=" + mNotification + ")";
292 }
Joe Onorato0cbda992010-05-02 16:28:15 -0700293}