blob: 6f839bdb0b390eb759832121258831047bf22c4f [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
John Spurlocke6f0a712013-09-03 16:23:49 -040052 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);
Daniel Sandler7579bca2011-08-18 15:47:26 -040071 }
Daniel Sandlerabff0322011-09-27 11:19:34 -040072
73 setScaleType(ImageView.ScaleType.CENTER);
Joe Onorato0cbda992010-05-02 16:28:15 -070074 }
75
Daniel Sandler05e24142011-11-10 11:56:49 -050076 public StatusBarIconView(Context context, AttributeSet attrs) {
77 super(context, attrs);
78 final Resources res = context.getResources();
79 final int outerBounds = res.getDimensionPixelSize(R.dimen.status_bar_icon_size);
80 final int imageBounds = res.getDimensionPixelSize(R.dimen.status_bar_icon_drawing_size);
81 final float scale = (float)imageBounds / (float)outerBounds;
82 setScaleX(scale);
83 setScaleY(scale);
Daniel Sandler05e24142011-11-10 11:56:49 -050084 }
85
Joe Onorato0cbda992010-05-02 16:28:15 -070086 private static boolean streq(String a, String b) {
Joe Onorato66d7d012010-05-14 10:05:10 -070087 if (a == b) {
88 return true;
89 }
Joe Onorato0cbda992010-05-02 16:28:15 -070090 if (a == null && b != null) {
91 return false;
92 }
93 if (a != null && b == null) {
94 return false;
95 }
96 return a.equals(b);
97 }
98
Joe Onorato005847b2010-06-04 16:08:02 -040099 /**
100 * Returns whether the set succeeded.
101 */
102 public boolean set(StatusBarIcon icon) {
103 final boolean iconEquals = mIcon != null
104 && streq(mIcon.iconPackage, icon.iconPackage)
105 && mIcon.iconId == icon.iconId;
106 final boolean levelEquals = iconEquals
107 && mIcon.iconLevel == icon.iconLevel;
108 final boolean visibilityEquals = mIcon != null
109 && mIcon.visible == icon.visible;
Joe Onorato6c01a112010-10-04 17:38:47 -0400110 final boolean numberEquals = mIcon != null
111 && mIcon.number == icon.number;
112 mIcon = icon.clone();
Svetoslav Ganov6179ea32011-06-28 01:12:41 -0700113 setContentDescription(icon.contentDescription);
Joe Onorato005847b2010-06-04 16:08:02 -0400114 if (!iconEquals) {
Fabrice Di Meglio82fca5d2013-01-09 15:42:31 -0800115 if (!updateDrawable(false /* no clear */)) return false;
Joe Onorato0cbda992010-05-02 16:28:15 -0700116 }
Joe Onorato005847b2010-06-04 16:08:02 -0400117 if (!levelEquals) {
118 setImageLevel(icon.iconLevel);
Joe Onorato0cbda992010-05-02 16:28:15 -0700119 }
Daniel Sandler26c84b12011-07-27 00:09:40 -0400120
Joe Onorato6c01a112010-10-04 17:38:47 -0400121 if (!numberEquals) {
John Spurlock01534782014-01-13 11:59:22 -0500122 if (icon.number > 0 && getContext().getResources().getBoolean(
Joe Onorato8595a3d2010-11-19 18:12:07 -0800123 R.bool.config_statusBarShowNumber)) {
Joe Onorato6c01a112010-10-04 17:38:47 -0400124 if (mNumberBackground == null) {
125 mNumberBackground = getContext().getResources().getDrawable(
126 R.drawable.ic_notification_overlay);
127 }
128 placeNumber();
129 } else {
130 mNumberBackground = null;
131 mNumberText = null;
132 }
133 invalidate();
134 }
Joe Onorato005847b2010-06-04 16:08:02 -0400135 if (!visibilityEquals) {
136 setVisibility(icon.visible ? VISIBLE : GONE);
137 }
Joe Onorato005847b2010-06-04 16:08:02 -0400138 return true;
Joe Onorato0cbda992010-05-02 16:28:15 -0700139 }
140
Fabrice Di Meglio82fca5d2013-01-09 15:42:31 -0800141 public void updateDrawable() {
142 updateDrawable(true /* with clear */);
143 }
144
145 private boolean updateDrawable(boolean withClear) {
146 Drawable drawable = getIcon(mIcon);
147 if (drawable == null) {
John Spurlockcd686b52013-06-05 10:13:46 -0400148 Log.w(TAG, "No icon for slot " + mSlot);
Fabrice Di Meglio82fca5d2013-01-09 15:42:31 -0800149 return false;
150 }
151 if (withClear) {
152 setImageDrawable(null);
153 }
154 setImageDrawable(drawable);
155 return true;
156 }
157
Joe Onoratof5510542010-06-01 07:46:41 -0700158 private Drawable getIcon(StatusBarIcon icon) {
159 return getIcon(getContext(), icon);
160 }
161
Joe Onorato0cbda992010-05-02 16:28:15 -0700162 /**
163 * Returns the right icon to use for this item, respecting the iconId and
164 * iconPackage (if set)
John Spurlock209bede2013-07-17 12:23:27 -0400165 *
Joe Onorato0cbda992010-05-02 16:28:15 -0700166 * @param context Context to use to get resources if iconPackage is not set
167 * @return Drawable for this item, or null if the package or item could not
168 * be found
169 */
Joe Onoratof5510542010-06-01 07:46:41 -0700170 public static Drawable getIcon(Context context, StatusBarIcon icon) {
Joe Onorato0cbda992010-05-02 16:28:15 -0700171 Resources r = null;
172
173 if (icon.iconPackage != null) {
174 try {
Jeff Sharkeyded653b2012-09-27 19:09:24 -0700175 int userId = icon.user.getIdentifier();
176 if (userId == UserHandle.USER_ALL) {
177 userId = UserHandle.USER_OWNER;
178 }
179 r = context.getPackageManager()
180 .getResourcesForApplicationAsUser(icon.iconPackage, userId);
Joe Onorato0cbda992010-05-02 16:28:15 -0700181 } catch (PackageManager.NameNotFoundException ex) {
John Spurlockcd686b52013-06-05 10:13:46 -0400182 Log.e(TAG, "Icon package not found: " + icon.iconPackage);
Joe Onorato0cbda992010-05-02 16:28:15 -0700183 return null;
184 }
185 } else {
186 r = context.getResources();
187 }
188
189 if (icon.iconId == 0) {
Joe Onorato0cbda992010-05-02 16:28:15 -0700190 return null;
191 }
John Spurlock209bede2013-07-17 12:23:27 -0400192
Joe Onorato0cbda992010-05-02 16:28:15 -0700193 try {
194 return r.getDrawable(icon.iconId);
195 } catch (RuntimeException e) {
John Spurlockcd686b52013-06-05 10:13:46 -0400196 Log.w(TAG, "Icon not found in "
Joe Onorato0cbda992010-05-02 16:28:15 -0700197 + (icon.iconPackage != null ? icon.iconId : "<system>")
198 + ": " + Integer.toHexString(icon.iconId));
199 }
200
201 return null;
202 }
Joe Onoratob77f53b2010-05-28 19:59:51 -0400203
204 public StatusBarIcon getStatusBarIcon() {
205 return mIcon;
206 }
Joe Onoratof9ec03c2010-09-23 15:09:18 -0700207
Svetoslav Ganov6179ea32011-06-28 01:12:41 -0700208 @Override
209 public void onInitializeAccessibilityEvent(AccessibilityEvent event) {
210 super.onInitializeAccessibilityEvent(event);
211 if (mNotification != null) {
212 event.setParcelableData(mNotification);
213 }
214 }
215
216 @Override
Joe Onorato6c01a112010-10-04 17:38:47 -0400217 protected void onSizeChanged(int w, int h, int oldw, int oldh) {
218 super.onSizeChanged(w, h, oldw, oldh);
219 if (mNumberBackground != null) {
220 placeNumber();
221 }
222 }
223
Svetoslav Ganov6179ea32011-06-28 01:12:41 -0700224 @Override
Joe Onorato6c01a112010-10-04 17:38:47 -0400225 protected void onDraw(Canvas canvas) {
226 super.onDraw(canvas);
227
228 if (mNumberBackground != null) {
229 mNumberBackground.draw(canvas);
230 canvas.drawText(mNumberText, mNumberX, mNumberY, mNumberPain);
231 }
232 }
233
Svetoslav Ganov6179ea32011-06-28 01:12:41 -0700234 @Override
Joe Onoratof9ec03c2010-09-23 15:09:18 -0700235 protected void debug(int depth) {
236 super.debug(depth);
237 Log.d("View", debugIndent(depth) + "slot=" + mSlot);
238 Log.d("View", debugIndent(depth) + "icon=" + mIcon);
239 }
Joe Onorato6c01a112010-10-04 17:38:47 -0400240
241 void placeNumber() {
Daniel Sandlerebce0112011-06-16 16:44:51 -0400242 final String str;
John Spurlock01534782014-01-13 11:59:22 -0500243 final int tooBig = getContext().getResources().getInteger(
Daniel Sandlerebce0112011-06-16 16:44:51 -0400244 android.R.integer.status_bar_notification_info_maxnum);
245 if (mIcon.number > tooBig) {
John Spurlock01534782014-01-13 11:59:22 -0500246 str = getContext().getResources().getString(
Daniel Sandlerebce0112011-06-16 16:44:51 -0400247 android.R.string.status_bar_notification_info_overflow);
248 } else {
249 NumberFormat f = NumberFormat.getIntegerInstance();
250 str = f.format(mIcon.number);
251 }
252 mNumberText = str;
253
Joe Onorato6c01a112010-10-04 17:38:47 -0400254 final int w = getWidth();
255 final int h = getHeight();
256 final Rect r = new Rect();
257 mNumberPain.getTextBounds(str, 0, str.length(), r);
258 final int tw = r.right - r.left;
259 final int th = r.bottom - r.top;
260 mNumberBackground.getPadding(r);
261 int dw = r.left + tw + r.right;
262 if (dw < mNumberBackground.getMinimumWidth()) {
263 dw = mNumberBackground.getMinimumWidth();
264 }
265 mNumberX = w-r.right-((dw-r.right-r.left)/2);
266 int dh = r.top + th + r.bottom;
267 if (dh < mNumberBackground.getMinimumWidth()) {
268 dh = mNumberBackground.getMinimumWidth();
269 }
270 mNumberY = h-r.bottom-((dh-r.top-th-r.bottom)/2);
271 mNumberBackground.setBounds(w-dw, h-dh, w, h);
272 }
Svetoslav Ganov6179ea32011-06-28 01:12:41 -0700273
274 private void setContentDescription(Notification notification) {
275 if (notification != null) {
276 CharSequence tickerText = notification.tickerText;
277 if (!TextUtils.isEmpty(tickerText)) {
278 setContentDescription(tickerText);
279 }
280 }
281 }
Daniel Sandler7579bca2011-08-18 15:47:26 -0400282
283 public String toString() {
John Spurlock209bede2013-07-17 12:23:27 -0400284 return "StatusBarIconView(slot=" + mSlot + " icon=" + mIcon
Daniel Sandler7579bca2011-08-18 15:47:26 -0400285 + " notification=" + mNotification + ")";
286 }
Joe Onorato0cbda992010-05-02 16:28:15 -0700287}