blob: db2415a2d41edf7346e883a2a85b5c3cfbbcdf25 [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;
Joe Onorato0cbda992010-05-02 16:28:15 -070021import android.content.res.Resources;
Joe Onorato0cbda992010-05-02 16:28:15 -070022import android.graphics.Canvas;
Joe Onorato6c01a112010-10-04 17:38:47 -040023import android.graphics.Paint;
24import android.graphics.Rect;
John Spurlockde84f0e2013-06-12 12:41:00 -040025import android.graphics.drawable.Drawable;
Dan Sandlerd63f9322015-05-06 15:18:49 -040026import android.graphics.drawable.Icon;
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;
Jason Monk3b230072015-05-29 11:11:29 -040051 private final boolean mBlocked;
Joe Onorato0cbda992010-05-02 16:28:15 -070052
John Spurlocke6f0a712013-09-03 16:23:49 -040053 public StatusBarIconView(Context context, String slot, Notification notification) {
Jason Monk3b230072015-05-29 11:11:29 -040054 this(context, slot, notification, false);
55 }
56
57 public StatusBarIconView(Context context, String slot, Notification notification,
58 boolean blocked) {
Joe Onorato0cbda992010-05-02 16:28:15 -070059 super(context);
Joe Onorato6c01a112010-10-04 17:38:47 -040060 final Resources res = context.getResources();
Jason Monk3b230072015-05-29 11:11:29 -040061 mBlocked = blocked;
Joe Onorato0cbda992010-05-02 16:28:15 -070062 mSlot = slot;
Joe Onorato6c01a112010-10-04 17:38:47 -040063 mNumberPain = new Paint();
64 mNumberPain.setTextAlign(Paint.Align.CENTER);
Alan Viverette4a357cd2015-03-18 18:37:18 -070065 mNumberPain.setColor(context.getColor(R.drawable.notification_number_text_color));
Joe Onorato6c01a112010-10-04 17:38:47 -040066 mNumberPain.setAntiAlias(true);
Christoph Studera0506e72014-07-31 20:27:39 +020067 setNotification(notification);
Daniel Sandler26c84b12011-07-27 00:09:40 -040068
Daniel Sandler7579bca2011-08-18 15:47:26 -040069 // We do not resize and scale system icons (on the right), only notification icons (on the
70 // left).
71 if (notification != null) {
72 final int outerBounds = res.getDimensionPixelSize(R.dimen.status_bar_icon_size);
73 final int imageBounds = res.getDimensionPixelSize(R.dimen.status_bar_icon_drawing_size);
74 final float scale = (float)imageBounds / (float)outerBounds;
75 setScaleX(scale);
76 setScaleY(scale);
Daniel Sandler7579bca2011-08-18 15:47:26 -040077 }
Daniel Sandlerabff0322011-09-27 11:19:34 -040078
79 setScaleType(ImageView.ScaleType.CENTER);
Joe Onorato0cbda992010-05-02 16:28:15 -070080 }
81
Christoph Studera0506e72014-07-31 20:27:39 +020082 public void setNotification(Notification notification) {
83 mNotification = notification;
84 setContentDescription(notification);
85 }
86
Daniel Sandler05e24142011-11-10 11:56:49 -050087 public StatusBarIconView(Context context, AttributeSet attrs) {
88 super(context, attrs);
Jason Monk3b230072015-05-29 11:11:29 -040089 mBlocked = false;
Daniel Sandler05e24142011-11-10 11:56:49 -050090 final Resources res = context.getResources();
91 final int outerBounds = res.getDimensionPixelSize(R.dimen.status_bar_icon_size);
92 final int imageBounds = res.getDimensionPixelSize(R.dimen.status_bar_icon_drawing_size);
93 final float scale = (float)imageBounds / (float)outerBounds;
94 setScaleX(scale);
95 setScaleY(scale);
Daniel Sandler05e24142011-11-10 11:56:49 -050096 }
97
Joe Onorato0cbda992010-05-02 16:28:15 -070098 private static boolean streq(String a, String b) {
Joe Onorato66d7d012010-05-14 10:05:10 -070099 if (a == b) {
100 return true;
101 }
Joe Onorato0cbda992010-05-02 16:28:15 -0700102 if (a == null && b != null) {
103 return false;
104 }
105 if (a != null && b == null) {
106 return false;
107 }
108 return a.equals(b);
109 }
110
Dan Sandlerd63f9322015-05-06 15:18:49 -0400111 public boolean equalIcons(Icon a, Icon b) {
112 if (a == b) return true;
113 if (a.getType() != b.getType()) return false;
114 switch (a.getType()) {
115 case Icon.TYPE_RESOURCE:
116 return a.getResPackage().equals(b.getResPackage()) && a.getResId() == b.getResId();
117 case Icon.TYPE_URI:
118 return a.getUriString().equals(b.getUriString());
119 default:
120 return false;
121 }
122 }
Joe Onorato005847b2010-06-04 16:08:02 -0400123 /**
124 * Returns whether the set succeeded.
125 */
126 public boolean set(StatusBarIcon icon) {
Dan Sandlerd63f9322015-05-06 15:18:49 -0400127 final boolean iconEquals = mIcon != null && equalIcons(mIcon.icon, icon.icon);
Joe Onorato005847b2010-06-04 16:08:02 -0400128 final boolean levelEquals = iconEquals
129 && mIcon.iconLevel == icon.iconLevel;
130 final boolean visibilityEquals = mIcon != null
131 && mIcon.visible == icon.visible;
Joe Onorato6c01a112010-10-04 17:38:47 -0400132 final boolean numberEquals = mIcon != null
133 && mIcon.number == icon.number;
134 mIcon = icon.clone();
Svetoslav Ganov6179ea32011-06-28 01:12:41 -0700135 setContentDescription(icon.contentDescription);
Joe Onorato005847b2010-06-04 16:08:02 -0400136 if (!iconEquals) {
Fabrice Di Meglio82fca5d2013-01-09 15:42:31 -0800137 if (!updateDrawable(false /* no clear */)) return false;
Joe Onorato0cbda992010-05-02 16:28:15 -0700138 }
Joe Onorato005847b2010-06-04 16:08:02 -0400139 if (!levelEquals) {
140 setImageLevel(icon.iconLevel);
Joe Onorato0cbda992010-05-02 16:28:15 -0700141 }
Daniel Sandler26c84b12011-07-27 00:09:40 -0400142
Joe Onorato6c01a112010-10-04 17:38:47 -0400143 if (!numberEquals) {
John Spurlock01534782014-01-13 11:59:22 -0500144 if (icon.number > 0 && getContext().getResources().getBoolean(
Joe Onorato8595a3d2010-11-19 18:12:07 -0800145 R.bool.config_statusBarShowNumber)) {
Joe Onorato6c01a112010-10-04 17:38:47 -0400146 if (mNumberBackground == null) {
147 mNumberBackground = getContext().getResources().getDrawable(
148 R.drawable.ic_notification_overlay);
149 }
150 placeNumber();
151 } else {
152 mNumberBackground = null;
153 mNumberText = null;
154 }
155 invalidate();
156 }
Joe Onorato005847b2010-06-04 16:08:02 -0400157 if (!visibilityEquals) {
Jason Monk3b230072015-05-29 11:11:29 -0400158 setVisibility(icon.visible && !mBlocked ? VISIBLE : GONE);
Joe Onorato005847b2010-06-04 16:08:02 -0400159 }
Joe Onorato005847b2010-06-04 16:08:02 -0400160 return true;
Joe Onorato0cbda992010-05-02 16:28:15 -0700161 }
162
Fabrice Di Meglio82fca5d2013-01-09 15:42:31 -0800163 public void updateDrawable() {
164 updateDrawable(true /* with clear */);
165 }
166
167 private boolean updateDrawable(boolean withClear) {
Jorim Jaggi66ac1332015-01-21 19:22:26 +0100168 if (mIcon == null) {
169 return false;
170 }
Fabrice Di Meglio82fca5d2013-01-09 15:42:31 -0800171 Drawable drawable = getIcon(mIcon);
172 if (drawable == null) {
John Spurlockcd686b52013-06-05 10:13:46 -0400173 Log.w(TAG, "No icon for slot " + mSlot);
Fabrice Di Meglio82fca5d2013-01-09 15:42:31 -0800174 return false;
175 }
176 if (withClear) {
177 setImageDrawable(null);
178 }
179 setImageDrawable(drawable);
180 return true;
181 }
182
Joe Onoratof5510542010-06-01 07:46:41 -0700183 private Drawable getIcon(StatusBarIcon icon) {
184 return getIcon(getContext(), icon);
185 }
186
Joe Onorato0cbda992010-05-02 16:28:15 -0700187 /**
Dan Sandlerd63f9322015-05-06 15:18:49 -0400188 * Returns the right icon to use for this item
John Spurlock209bede2013-07-17 12:23:27 -0400189 *
Dan Sandlerd63f9322015-05-06 15:18:49 -0400190 * @param context Context to use to get resources
Joe Onorato0cbda992010-05-02 16:28:15 -0700191 * @return Drawable for this item, or null if the package or item could not
192 * be found
193 */
Joe Onoratof5510542010-06-01 07:46:41 -0700194 public static Drawable getIcon(Context context, StatusBarIcon icon) {
Dan Sandlerd63f9322015-05-06 15:18:49 -0400195 int userId = icon.user.getIdentifier();
196 if (userId == UserHandle.USER_ALL) {
Xiaohui Chen87d0e252015-07-30 15:38:16 -0700197 userId = UserHandle.USER_SYSTEM;
Joe Onorato0cbda992010-05-02 16:28:15 -0700198 }
Dan Sandlerd63f9322015-05-06 15:18:49 -0400199 return icon.icon.loadDrawableAsUser(context, userId);
Joe Onorato0cbda992010-05-02 16:28:15 -0700200 }
Joe Onoratob77f53b2010-05-28 19:59:51 -0400201
202 public StatusBarIcon getStatusBarIcon() {
203 return mIcon;
204 }
Joe Onoratof9ec03c2010-09-23 15:09:18 -0700205
Svetoslav Ganov6179ea32011-06-28 01:12:41 -0700206 @Override
207 public void onInitializeAccessibilityEvent(AccessibilityEvent event) {
208 super.onInitializeAccessibilityEvent(event);
209 if (mNotification != null) {
210 event.setParcelableData(mNotification);
211 }
212 }
213
214 @Override
Joe Onorato6c01a112010-10-04 17:38:47 -0400215 protected void onSizeChanged(int w, int h, int oldw, int oldh) {
216 super.onSizeChanged(w, h, oldw, oldh);
217 if (mNumberBackground != null) {
218 placeNumber();
219 }
220 }
221
Svetoslav Ganov6179ea32011-06-28 01:12:41 -0700222 @Override
Jorim Jaggi66ac1332015-01-21 19:22:26 +0100223 public void onRtlPropertiesChanged(int layoutDirection) {
224 super.onRtlPropertiesChanged(layoutDirection);
225 updateDrawable();
226 }
227
228 @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;
John Spurlock01534782014-01-13 11:59:22 -0500247 final int tooBig = getContext().getResources().getInteger(
Daniel Sandlerebce0112011-06-16 16:44:51 -0400248 android.R.integer.status_bar_notification_info_maxnum);
249 if (mIcon.number > tooBig) {
John Spurlock01534782014-01-13 11:59:22 -0500250 str = getContext().getResources().getString(
Daniel Sandlerebce0112011-06-16 16:44:51 -0400251 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 }
Jason Monk3b230072015-05-29 11:11:29 -0400291
292 public String getSlot() {
293 return mSlot;
294 }
Joe Onorato0cbda992010-05-02 16:28:15 -0700295}