blob: e6847d8ce5e2d67990d81bac5f8b32075c8e806e [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);
Alan Viverette4a357cd2015-03-18 18:37:18 -070058 mNumberPain.setColor(context.getColor(R.drawable.notification_number_text_color));
Joe Onorato6c01a112010-10-04 17:38:47 -040059 mNumberPain.setAntiAlias(true);
Christoph Studera0506e72014-07-31 20:27:39 +020060 setNotification(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);
Daniel Sandler7579bca2011-08-18 15:47:26 -040070 }
Daniel Sandlerabff0322011-09-27 11:19:34 -040071
72 setScaleType(ImageView.ScaleType.CENTER);
Joe Onorato0cbda992010-05-02 16:28:15 -070073 }
74
Christoph Studera0506e72014-07-31 20:27:39 +020075 public void setNotification(Notification notification) {
76 mNotification = notification;
77 setContentDescription(notification);
78 }
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);
Daniel Sandler05e24142011-11-10 11:56:49 -050088 }
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) {
John Spurlock01534782014-01-13 11:59:22 -0500126 if (icon.number > 0 && getContext().getResources().getBoolean(
Joe Onorato8595a3d2010-11-19 18:12:07 -0800127 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) {
Jorim Jaggi66ac1332015-01-21 19:22:26 +0100150 if (mIcon == null) {
151 return false;
152 }
Fabrice Di Meglio82fca5d2013-01-09 15:42:31 -0800153 Drawable drawable = getIcon(mIcon);
154 if (drawable == null) {
John Spurlockcd686b52013-06-05 10:13:46 -0400155 Log.w(TAG, "No icon for slot " + mSlot);
Fabrice Di Meglio82fca5d2013-01-09 15:42:31 -0800156 return false;
157 }
158 if (withClear) {
159 setImageDrawable(null);
160 }
161 setImageDrawable(drawable);
162 return true;
163 }
164
Joe Onoratof5510542010-06-01 07:46:41 -0700165 private Drawable getIcon(StatusBarIcon icon) {
166 return getIcon(getContext(), icon);
167 }
168
Joe Onorato0cbda992010-05-02 16:28:15 -0700169 /**
170 * Returns the right icon to use for this item, respecting the iconId and
171 * iconPackage (if set)
John Spurlock209bede2013-07-17 12:23:27 -0400172 *
Joe Onorato0cbda992010-05-02 16:28:15 -0700173 * @param context Context to use to get resources if iconPackage is not set
174 * @return Drawable for this item, or null if the package or item could not
175 * be found
176 */
Joe Onoratof5510542010-06-01 07:46:41 -0700177 public static Drawable getIcon(Context context, StatusBarIcon icon) {
Joe Onorato0cbda992010-05-02 16:28:15 -0700178 Resources r = null;
179
180 if (icon.iconPackage != null) {
181 try {
Jeff Sharkeyded653b2012-09-27 19:09:24 -0700182 int userId = icon.user.getIdentifier();
183 if (userId == UserHandle.USER_ALL) {
184 userId = UserHandle.USER_OWNER;
185 }
186 r = context.getPackageManager()
187 .getResourcesForApplicationAsUser(icon.iconPackage, userId);
Joe Onorato0cbda992010-05-02 16:28:15 -0700188 } catch (PackageManager.NameNotFoundException ex) {
John Spurlockcd686b52013-06-05 10:13:46 -0400189 Log.e(TAG, "Icon package not found: " + icon.iconPackage);
Joe Onorato0cbda992010-05-02 16:28:15 -0700190 return null;
191 }
192 } else {
193 r = context.getResources();
194 }
195
196 if (icon.iconId == 0) {
Joe Onorato0cbda992010-05-02 16:28:15 -0700197 return null;
198 }
John Spurlock209bede2013-07-17 12:23:27 -0400199
Joe Onorato0cbda992010-05-02 16:28:15 -0700200 try {
201 return r.getDrawable(icon.iconId);
202 } catch (RuntimeException e) {
John Spurlockcd686b52013-06-05 10:13:46 -0400203 Log.w(TAG, "Icon not found in "
Joe Onorato0cbda992010-05-02 16:28:15 -0700204 + (icon.iconPackage != null ? icon.iconId : "<system>")
205 + ": " + Integer.toHexString(icon.iconId));
206 }
207
208 return null;
209 }
Joe Onoratob77f53b2010-05-28 19:59:51 -0400210
211 public StatusBarIcon getStatusBarIcon() {
212 return mIcon;
213 }
Joe Onoratof9ec03c2010-09-23 15:09:18 -0700214
Svetoslav Ganov6179ea32011-06-28 01:12:41 -0700215 @Override
216 public void onInitializeAccessibilityEvent(AccessibilityEvent event) {
217 super.onInitializeAccessibilityEvent(event);
218 if (mNotification != null) {
219 event.setParcelableData(mNotification);
220 }
221 }
222
223 @Override
Joe Onorato6c01a112010-10-04 17:38:47 -0400224 protected void onSizeChanged(int w, int h, int oldw, int oldh) {
225 super.onSizeChanged(w, h, oldw, oldh);
226 if (mNumberBackground != null) {
227 placeNumber();
228 }
229 }
230
Svetoslav Ganov6179ea32011-06-28 01:12:41 -0700231 @Override
Jorim Jaggi66ac1332015-01-21 19:22:26 +0100232 public void onRtlPropertiesChanged(int layoutDirection) {
233 super.onRtlPropertiesChanged(layoutDirection);
234 updateDrawable();
235 }
236
237 @Override
Joe Onorato6c01a112010-10-04 17:38:47 -0400238 protected void onDraw(Canvas canvas) {
239 super.onDraw(canvas);
240
241 if (mNumberBackground != null) {
242 mNumberBackground.draw(canvas);
243 canvas.drawText(mNumberText, mNumberX, mNumberY, mNumberPain);
244 }
245 }
246
Svetoslav Ganov6179ea32011-06-28 01:12:41 -0700247 @Override
Joe Onoratof9ec03c2010-09-23 15:09:18 -0700248 protected void debug(int depth) {
249 super.debug(depth);
250 Log.d("View", debugIndent(depth) + "slot=" + mSlot);
251 Log.d("View", debugIndent(depth) + "icon=" + mIcon);
252 }
Joe Onorato6c01a112010-10-04 17:38:47 -0400253
254 void placeNumber() {
Daniel Sandlerebce0112011-06-16 16:44:51 -0400255 final String str;
John Spurlock01534782014-01-13 11:59:22 -0500256 final int tooBig = getContext().getResources().getInteger(
Daniel Sandlerebce0112011-06-16 16:44:51 -0400257 android.R.integer.status_bar_notification_info_maxnum);
258 if (mIcon.number > tooBig) {
John Spurlock01534782014-01-13 11:59:22 -0500259 str = getContext().getResources().getString(
Daniel Sandlerebce0112011-06-16 16:44:51 -0400260 android.R.string.status_bar_notification_info_overflow);
261 } else {
262 NumberFormat f = NumberFormat.getIntegerInstance();
263 str = f.format(mIcon.number);
264 }
265 mNumberText = str;
266
Joe Onorato6c01a112010-10-04 17:38:47 -0400267 final int w = getWidth();
268 final int h = getHeight();
269 final Rect r = new Rect();
270 mNumberPain.getTextBounds(str, 0, str.length(), r);
271 final int tw = r.right - r.left;
272 final int th = r.bottom - r.top;
273 mNumberBackground.getPadding(r);
274 int dw = r.left + tw + r.right;
275 if (dw < mNumberBackground.getMinimumWidth()) {
276 dw = mNumberBackground.getMinimumWidth();
277 }
278 mNumberX = w-r.right-((dw-r.right-r.left)/2);
279 int dh = r.top + th + r.bottom;
280 if (dh < mNumberBackground.getMinimumWidth()) {
281 dh = mNumberBackground.getMinimumWidth();
282 }
283 mNumberY = h-r.bottom-((dh-r.top-th-r.bottom)/2);
284 mNumberBackground.setBounds(w-dw, h-dh, w, h);
285 }
Svetoslav Ganov6179ea32011-06-28 01:12:41 -0700286
287 private void setContentDescription(Notification notification) {
288 if (notification != null) {
289 CharSequence tickerText = notification.tickerText;
290 if (!TextUtils.isEmpty(tickerText)) {
291 setContentDescription(tickerText);
292 }
293 }
294 }
Daniel Sandler7579bca2011-08-18 15:47:26 -0400295
296 public String toString() {
John Spurlock209bede2013-07-17 12:23:27 -0400297 return "StatusBarIconView(slot=" + mSlot + " icon=" + mIcon
Daniel Sandler7579bca2011-08-18 15:47:26 -0400298 + " notification=" + mNotification + ")";
299 }
Joe Onorato0cbda992010-05-02 16:28:15 -0700300}