blob: 5bda813fdaa2373b3e68b96e71ea21f48c805aea [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;
Joe Onorato0cbda992010-05-02 16:28:15 -070023import android.graphics.Canvas;
Joe Onorato6c01a112010-10-04 17:38:47 -040024import android.graphics.Paint;
25import android.graphics.Rect;
John Spurlockde84f0e2013-06-12 12:41:00 -040026import android.graphics.drawable.Drawable;
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 Onoratof9ec03c2010-09-23 15:09:18 -070030import android.util.Log;
Joe Onorato0cbda992010-05-02 16:28:15 -070031import android.view.ViewDebug;
Svetoslav Ganov6179ea32011-06-28 01:12:41 -070032import android.view.accessibility.AccessibilityEvent;
Daniel Sandlerabff0322011-09-27 11:19:34 -040033import android.widget.ImageView;
Joe Onorato0cbda992010-05-02 16:28:15 -070034
35import com.android.internal.statusbar.StatusBarIcon;
Joe Onorato6c01a112010-10-04 17:38:47 -040036import com.android.systemui.R;
37
John Spurlockde84f0e2013-06-12 12:41:00 -040038import java.text.NumberFormat;
39
Joe Onorato0cbda992010-05-02 16:28:15 -070040public class StatusBarIconView extends AnimatedImageView {
41 private static final String TAG = "StatusBarIconView";
42
43 private StatusBarIcon mIcon;
44 @ViewDebug.ExportedProperty private String mSlot;
Joe Onorato6c01a112010-10-04 17:38:47 -040045 private Drawable mNumberBackground;
46 private Paint mNumberPain;
47 private int mNumberX;
48 private int mNumberY;
49 private String mNumberText;
Svetoslav Ganov6179ea32011-06-28 01:12:41 -070050 private Notification mNotification;
Joe Onorato0cbda992010-05-02 16:28:15 -070051
Svetoslav Ganov6179ea32011-06-28 01:12:41 -070052 public StatusBarIconView(Context context, String slot, Notification notification) {
Joe Onorato0cbda992010-05-02 16:28:15 -070053 super(context);
Joe Onorato6c01a112010-10-04 17:38:47 -040054 final Resources res = context.getResources();
Joe Onorato0cbda992010-05-02 16:28:15 -070055 mSlot = slot;
Joe Onorato6c01a112010-10-04 17:38:47 -040056 mNumberPain = new Paint();
57 mNumberPain.setTextAlign(Paint.Align.CENTER);
58 mNumberPain.setColor(res.getColor(R.drawable.notification_number_text_color));
59 mNumberPain.setAntiAlias(true);
Svetoslav Ganov6179ea32011-06-28 01:12:41 -070060 mNotification = notification;
61 setContentDescription(notification);
Daniel Sandler26c84b12011-07-27 00:09:40 -040062
Daniel Sandler7579bca2011-08-18 15:47:26 -040063 // We do not resize and scale system icons (on the right), only notification icons (on the
64 // left).
65 if (notification != null) {
66 final int outerBounds = res.getDimensionPixelSize(R.dimen.status_bar_icon_size);
67 final int imageBounds = res.getDimensionPixelSize(R.dimen.status_bar_icon_drawing_size);
68 final float scale = (float)imageBounds / (float)outerBounds;
69 setScaleX(scale);
70 setScaleY(scale);
71 final float alpha = res.getFraction(R.dimen.status_bar_icon_drawing_alpha, 1, 1);
72 setAlpha(alpha);
73 }
Daniel Sandlerabff0322011-09-27 11:19:34 -040074
75 setScaleType(ImageView.ScaleType.CENTER);
Joe Onorato0cbda992010-05-02 16:28:15 -070076 }
77
Daniel Sandler05e24142011-11-10 11:56:49 -050078 public StatusBarIconView(Context context, AttributeSet attrs) {
79 super(context, attrs);
80 final Resources res = context.getResources();
81 final int outerBounds = res.getDimensionPixelSize(R.dimen.status_bar_icon_size);
82 final int imageBounds = res.getDimensionPixelSize(R.dimen.status_bar_icon_drawing_size);
83 final float scale = (float)imageBounds / (float)outerBounds;
84 setScaleX(scale);
85 setScaleY(scale);
86 final float alpha = res.getFraction(R.dimen.status_bar_icon_drawing_alpha, 1, 1);
87 setAlpha(alpha);
88 }
89
Joe Onorato0cbda992010-05-02 16:28:15 -070090 private static boolean streq(String a, String b) {
Joe Onorato66d7d012010-05-14 10:05:10 -070091 if (a == b) {
92 return true;
93 }
Joe Onorato0cbda992010-05-02 16:28:15 -070094 if (a == null && b != null) {
95 return false;
96 }
97 if (a != null && b == null) {
98 return false;
99 }
100 return a.equals(b);
101 }
102
Joe Onorato005847b2010-06-04 16:08:02 -0400103 /**
104 * Returns whether the set succeeded.
105 */
106 public boolean set(StatusBarIcon icon) {
107 final boolean iconEquals = mIcon != null
108 && streq(mIcon.iconPackage, icon.iconPackage)
109 && mIcon.iconId == icon.iconId;
110 final boolean levelEquals = iconEquals
111 && mIcon.iconLevel == icon.iconLevel;
112 final boolean visibilityEquals = mIcon != null
113 && mIcon.visible == icon.visible;
Joe Onorato6c01a112010-10-04 17:38:47 -0400114 final boolean numberEquals = mIcon != null
115 && mIcon.number == icon.number;
116 mIcon = icon.clone();
Svetoslav Ganov6179ea32011-06-28 01:12:41 -0700117 setContentDescription(icon.contentDescription);
Joe Onorato005847b2010-06-04 16:08:02 -0400118 if (!iconEquals) {
Fabrice Di Meglio82fca5d2013-01-09 15:42:31 -0800119 if (!updateDrawable(false /* no clear */)) return false;
Joe Onorato0cbda992010-05-02 16:28:15 -0700120 }
Joe Onorato005847b2010-06-04 16:08:02 -0400121 if (!levelEquals) {
122 setImageLevel(icon.iconLevel);
Joe Onorato0cbda992010-05-02 16:28:15 -0700123 }
Daniel Sandler26c84b12011-07-27 00:09:40 -0400124
Joe Onorato6c01a112010-10-04 17:38:47 -0400125 if (!numberEquals) {
Joe Onorato8595a3d2010-11-19 18:12:07 -0800126 if (icon.number > 0 && mContext.getResources().getBoolean(
127 R.bool.config_statusBarShowNumber)) {
Joe Onorato6c01a112010-10-04 17:38:47 -0400128 if (mNumberBackground == null) {
129 mNumberBackground = getContext().getResources().getDrawable(
130 R.drawable.ic_notification_overlay);
131 }
132 placeNumber();
133 } else {
134 mNumberBackground = null;
135 mNumberText = null;
136 }
137 invalidate();
138 }
Joe Onorato005847b2010-06-04 16:08:02 -0400139 if (!visibilityEquals) {
140 setVisibility(icon.visible ? VISIBLE : GONE);
141 }
Joe Onorato005847b2010-06-04 16:08:02 -0400142 return true;
Joe Onorato0cbda992010-05-02 16:28:15 -0700143 }
144
Fabrice Di Meglio82fca5d2013-01-09 15:42:31 -0800145 public void updateDrawable() {
146 updateDrawable(true /* with clear */);
147 }
148
149 private boolean updateDrawable(boolean withClear) {
150 Drawable drawable = getIcon(mIcon);
151 if (drawable == null) {
John Spurlockcd686b52013-06-05 10:13:46 -0400152 Log.w(TAG, "No icon for slot " + mSlot);
Fabrice Di Meglio82fca5d2013-01-09 15:42:31 -0800153 return false;
154 }
155 if (withClear) {
156 setImageDrawable(null);
157 }
158 setImageDrawable(drawable);
159 return true;
160 }
161
Joe Onoratof5510542010-06-01 07:46:41 -0700162 private Drawable getIcon(StatusBarIcon icon) {
163 return getIcon(getContext(), icon);
164 }
165
Joe Onorato0cbda992010-05-02 16:28:15 -0700166 /**
167 * Returns the right icon to use for this item, respecting the iconId and
168 * iconPackage (if set)
John Spurlock209bede2013-07-17 12:23:27 -0400169 *
Joe Onorato0cbda992010-05-02 16:28:15 -0700170 * @param context Context to use to get resources if iconPackage is not set
171 * @return Drawable for this item, or null if the package or item could not
172 * be found
173 */
Joe Onoratof5510542010-06-01 07:46:41 -0700174 public static Drawable getIcon(Context context, StatusBarIcon icon) {
Joe Onorato0cbda992010-05-02 16:28:15 -0700175 Resources r = null;
176
177 if (icon.iconPackage != null) {
178 try {
Jeff Sharkeyded653b2012-09-27 19:09:24 -0700179 int userId = icon.user.getIdentifier();
180 if (userId == UserHandle.USER_ALL) {
181 userId = UserHandle.USER_OWNER;
182 }
183 r = context.getPackageManager()
184 .getResourcesForApplicationAsUser(icon.iconPackage, userId);
Joe Onorato0cbda992010-05-02 16:28:15 -0700185 } catch (PackageManager.NameNotFoundException ex) {
John Spurlockcd686b52013-06-05 10:13:46 -0400186 Log.e(TAG, "Icon package not found: " + icon.iconPackage);
Joe Onorato0cbda992010-05-02 16:28:15 -0700187 return null;
188 }
189 } else {
190 r = context.getResources();
191 }
192
193 if (icon.iconId == 0) {
Joe Onorato0cbda992010-05-02 16:28:15 -0700194 return null;
195 }
John Spurlock209bede2013-07-17 12:23:27 -0400196
Joe Onorato0cbda992010-05-02 16:28:15 -0700197 try {
198 return r.getDrawable(icon.iconId);
199 } catch (RuntimeException e) {
John Spurlockcd686b52013-06-05 10:13:46 -0400200 Log.w(TAG, "Icon not found in "
Joe Onorato0cbda992010-05-02 16:28:15 -0700201 + (icon.iconPackage != null ? icon.iconId : "<system>")
202 + ": " + Integer.toHexString(icon.iconId));
203 }
204
205 return null;
206 }
Joe Onoratob77f53b2010-05-28 19:59:51 -0400207
208 public StatusBarIcon getStatusBarIcon() {
209 return mIcon;
210 }
Joe Onoratof9ec03c2010-09-23 15:09:18 -0700211
Svetoslav Ganov6179ea32011-06-28 01:12:41 -0700212 @Override
213 public void onInitializeAccessibilityEvent(AccessibilityEvent event) {
214 super.onInitializeAccessibilityEvent(event);
215 if (mNotification != null) {
216 event.setParcelableData(mNotification);
217 }
218 }
219
220 @Override
Joe Onorato6c01a112010-10-04 17:38:47 -0400221 protected void onSizeChanged(int w, int h, int oldw, int oldh) {
222 super.onSizeChanged(w, h, oldw, oldh);
223 if (mNumberBackground != null) {
224 placeNumber();
225 }
226 }
227
Svetoslav Ganov6179ea32011-06-28 01:12:41 -0700228 @Override
Joe Onorato6c01a112010-10-04 17:38:47 -0400229 protected void onDraw(Canvas canvas) {
230 super.onDraw(canvas);
231
232 if (mNumberBackground != null) {
233 mNumberBackground.draw(canvas);
234 canvas.drawText(mNumberText, mNumberX, mNumberY, mNumberPain);
235 }
236 }
237
Svetoslav Ganov6179ea32011-06-28 01:12:41 -0700238 @Override
Joe Onoratof9ec03c2010-09-23 15:09:18 -0700239 protected void debug(int depth) {
240 super.debug(depth);
241 Log.d("View", debugIndent(depth) + "slot=" + mSlot);
242 Log.d("View", debugIndent(depth) + "icon=" + mIcon);
243 }
Joe Onorato6c01a112010-10-04 17:38:47 -0400244
245 void placeNumber() {
Daniel Sandlerebce0112011-06-16 16:44:51 -0400246 final String str;
247 final int tooBig = mContext.getResources().getInteger(
248 android.R.integer.status_bar_notification_info_maxnum);
249 if (mIcon.number > tooBig) {
250 str = mContext.getResources().getString(
251 android.R.string.status_bar_notification_info_overflow);
252 } else {
253 NumberFormat f = NumberFormat.getIntegerInstance();
254 str = f.format(mIcon.number);
255 }
256 mNumberText = str;
257
Joe Onorato6c01a112010-10-04 17:38:47 -0400258 final int w = getWidth();
259 final int h = getHeight();
260 final Rect r = new Rect();
261 mNumberPain.getTextBounds(str, 0, str.length(), r);
262 final int tw = r.right - r.left;
263 final int th = r.bottom - r.top;
264 mNumberBackground.getPadding(r);
265 int dw = r.left + tw + r.right;
266 if (dw < mNumberBackground.getMinimumWidth()) {
267 dw = mNumberBackground.getMinimumWidth();
268 }
269 mNumberX = w-r.right-((dw-r.right-r.left)/2);
270 int dh = r.top + th + r.bottom;
271 if (dh < mNumberBackground.getMinimumWidth()) {
272 dh = mNumberBackground.getMinimumWidth();
273 }
274 mNumberY = h-r.bottom-((dh-r.top-th-r.bottom)/2);
275 mNumberBackground.setBounds(w-dw, h-dh, w, h);
276 }
Svetoslav Ganov6179ea32011-06-28 01:12:41 -0700277
278 private void setContentDescription(Notification notification) {
279 if (notification != null) {
280 CharSequence tickerText = notification.tickerText;
281 if (!TextUtils.isEmpty(tickerText)) {
282 setContentDescription(tickerText);
283 }
284 }
285 }
Daniel Sandler7579bca2011-08-18 15:47:26 -0400286
287 public String toString() {
John Spurlock209bede2013-07-17 12:23:27 -0400288 return "StatusBarIconView(slot=" + mSlot + " icon=" + mIcon
Daniel Sandler7579bca2011-08-18 15:47:26 -0400289 + " notification=" + mNotification + ")";
290 }
Joe Onorato0cbda992010-05-02 16:28:15 -0700291}