blob: 5a7cf86be8e555772167361f30611a10e7c6ba7f [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;
Anthony Chen55e8e1e2016-01-08 10:31:46 -080027import android.graphics.drawable.ScaleDrawable;
Jeff Sharkeyded653b2012-09-27 19:09:24 -070028import android.os.UserHandle;
Svetoslav Ganov6179ea32011-06-28 01:12:41 -070029import android.text.TextUtils;
Daniel Sandler05e24142011-11-10 11:56:49 -050030import android.util.AttributeSet;
Joe Onoratof9ec03c2010-09-23 15:09:18 -070031import android.util.Log;
Anthony Chen55e8e1e2016-01-08 10:31:46 -080032import android.util.TypedValue;
33import android.view.Gravity;
Joe Onorato0cbda992010-05-02 16:28:15 -070034import android.view.ViewDebug;
Svetoslav Ganov6179ea32011-06-28 01:12:41 -070035import android.view.accessibility.AccessibilityEvent;
Joe Onorato0cbda992010-05-02 16:28:15 -070036import com.android.internal.statusbar.StatusBarIcon;
Joe Onorato6c01a112010-10-04 17:38:47 -040037import com.android.systemui.R;
38
John Spurlockde84f0e2013-06-12 12:41:00 -040039import java.text.NumberFormat;
40
Joe Onorato0cbda992010-05-02 16:28:15 -070041public class StatusBarIconView extends AnimatedImageView {
42 private static final String TAG = "StatusBarIconView";
43
44 private StatusBarIcon mIcon;
45 @ViewDebug.ExportedProperty private String mSlot;
Joe Onorato6c01a112010-10-04 17:38:47 -040046 private Drawable mNumberBackground;
47 private Paint mNumberPain;
48 private int mNumberX;
49 private int mNumberY;
50 private String mNumberText;
Svetoslav Ganov6179ea32011-06-28 01:12:41 -070051 private Notification mNotification;
Jason Monk3b230072015-05-29 11:11:29 -040052 private final boolean mBlocked;
Joe Onorato0cbda992010-05-02 16:28:15 -070053
John Spurlocke6f0a712013-09-03 16:23:49 -040054 public StatusBarIconView(Context context, String slot, Notification notification) {
Jason Monk3b230072015-05-29 11:11:29 -040055 this(context, slot, notification, false);
56 }
57
58 public StatusBarIconView(Context context, String slot, Notification notification,
59 boolean blocked) {
Joe Onorato0cbda992010-05-02 16:28:15 -070060 super(context);
Joe Onorato6c01a112010-10-04 17:38:47 -040061 final Resources res = context.getResources();
Jason Monk3b230072015-05-29 11:11:29 -040062 mBlocked = blocked;
Joe Onorato0cbda992010-05-02 16:28:15 -070063 mSlot = slot;
Joe Onorato6c01a112010-10-04 17:38:47 -040064 mNumberPain = new Paint();
65 mNumberPain.setTextAlign(Paint.Align.CENTER);
Alan Viverette4a357cd2015-03-18 18:37:18 -070066 mNumberPain.setColor(context.getColor(R.drawable.notification_number_text_color));
Joe Onorato6c01a112010-10-04 17:38:47 -040067 mNumberPain.setAntiAlias(true);
Christoph Studera0506e72014-07-31 20:27:39 +020068 setNotification(notification);
Daniel Sandler26c84b12011-07-27 00:09:40 -040069
Daniel Sandler7579bca2011-08-18 15:47:26 -040070 // We do not resize and scale system icons (on the right), only notification icons (on the
71 // left).
72 if (notification != null) {
73 final int outerBounds = res.getDimensionPixelSize(R.dimen.status_bar_icon_size);
74 final int imageBounds = res.getDimensionPixelSize(R.dimen.status_bar_icon_drawing_size);
75 final float scale = (float)imageBounds / (float)outerBounds;
76 setScaleX(scale);
77 setScaleY(scale);
Daniel Sandler7579bca2011-08-18 15:47:26 -040078 }
Daniel Sandlerabff0322011-09-27 11:19:34 -040079
Jason Monk66c89c12016-01-06 08:51:26 -050080 setScaleType(ScaleType.CENTER);
Joe Onorato0cbda992010-05-02 16:28:15 -070081 }
82
Christoph Studera0506e72014-07-31 20:27:39 +020083 public void setNotification(Notification notification) {
84 mNotification = notification;
85 setContentDescription(notification);
86 }
87
Daniel Sandler05e24142011-11-10 11:56:49 -050088 public StatusBarIconView(Context context, AttributeSet attrs) {
89 super(context, attrs);
Jason Monk3b230072015-05-29 11:11:29 -040090 mBlocked = false;
Daniel Sandler05e24142011-11-10 11:56:49 -050091 final Resources res = context.getResources();
92 final int outerBounds = res.getDimensionPixelSize(R.dimen.status_bar_icon_size);
93 final int imageBounds = res.getDimensionPixelSize(R.dimen.status_bar_icon_drawing_size);
94 final float scale = (float)imageBounds / (float)outerBounds;
95 setScaleX(scale);
96 setScaleY(scale);
Daniel Sandler05e24142011-11-10 11:56:49 -050097 }
98
Joe Onorato0cbda992010-05-02 16:28:15 -070099 private static boolean streq(String a, String b) {
Joe Onorato66d7d012010-05-14 10:05:10 -0700100 if (a == b) {
101 return true;
102 }
Joe Onorato0cbda992010-05-02 16:28:15 -0700103 if (a == null && b != null) {
104 return false;
105 }
106 if (a != null && b == null) {
107 return false;
108 }
109 return a.equals(b);
110 }
111
Dan Sandlerd63f9322015-05-06 15:18:49 -0400112 public boolean equalIcons(Icon a, Icon b) {
113 if (a == b) return true;
114 if (a.getType() != b.getType()) return false;
115 switch (a.getType()) {
116 case Icon.TYPE_RESOURCE:
117 return a.getResPackage().equals(b.getResPackage()) && a.getResId() == b.getResId();
118 case Icon.TYPE_URI:
119 return a.getUriString().equals(b.getUriString());
120 default:
121 return false;
122 }
123 }
Joe Onorato005847b2010-06-04 16:08:02 -0400124 /**
125 * Returns whether the set succeeded.
126 */
127 public boolean set(StatusBarIcon icon) {
Dan Sandlerd63f9322015-05-06 15:18:49 -0400128 final boolean iconEquals = mIcon != null && equalIcons(mIcon.icon, icon.icon);
Joe Onorato005847b2010-06-04 16:08:02 -0400129 final boolean levelEquals = iconEquals
130 && mIcon.iconLevel == icon.iconLevel;
131 final boolean visibilityEquals = mIcon != null
132 && mIcon.visible == icon.visible;
Joe Onorato6c01a112010-10-04 17:38:47 -0400133 final boolean numberEquals = mIcon != null
134 && mIcon.number == icon.number;
135 mIcon = icon.clone();
Svetoslav Ganov6179ea32011-06-28 01:12:41 -0700136 setContentDescription(icon.contentDescription);
Joe Onorato005847b2010-06-04 16:08:02 -0400137 if (!iconEquals) {
Fabrice Di Meglio82fca5d2013-01-09 15:42:31 -0800138 if (!updateDrawable(false /* no clear */)) return false;
Joe Onorato0cbda992010-05-02 16:28:15 -0700139 }
Joe Onorato005847b2010-06-04 16:08:02 -0400140 if (!levelEquals) {
141 setImageLevel(icon.iconLevel);
Joe Onorato0cbda992010-05-02 16:28:15 -0700142 }
Daniel Sandler26c84b12011-07-27 00:09:40 -0400143
Joe Onorato6c01a112010-10-04 17:38:47 -0400144 if (!numberEquals) {
John Spurlock01534782014-01-13 11:59:22 -0500145 if (icon.number > 0 && getContext().getResources().getBoolean(
Joe Onorato8595a3d2010-11-19 18:12:07 -0800146 R.bool.config_statusBarShowNumber)) {
Joe Onorato6c01a112010-10-04 17:38:47 -0400147 if (mNumberBackground == null) {
148 mNumberBackground = getContext().getResources().getDrawable(
149 R.drawable.ic_notification_overlay);
150 }
151 placeNumber();
152 } else {
153 mNumberBackground = null;
154 mNumberText = null;
155 }
156 invalidate();
157 }
Joe Onorato005847b2010-06-04 16:08:02 -0400158 if (!visibilityEquals) {
Jason Monk3b230072015-05-29 11:11:29 -0400159 setVisibility(icon.visible && !mBlocked ? VISIBLE : GONE);
Joe Onorato005847b2010-06-04 16:08:02 -0400160 }
Joe Onorato005847b2010-06-04 16:08:02 -0400161 return true;
Joe Onorato0cbda992010-05-02 16:28:15 -0700162 }
163
Fabrice Di Meglio82fca5d2013-01-09 15:42:31 -0800164 public void updateDrawable() {
165 updateDrawable(true /* with clear */);
166 }
167
168 private boolean updateDrawable(boolean withClear) {
Jorim Jaggi66ac1332015-01-21 19:22:26 +0100169 if (mIcon == null) {
170 return false;
171 }
Fabrice Di Meglio82fca5d2013-01-09 15:42:31 -0800172 Drawable drawable = getIcon(mIcon);
173 if (drawable == null) {
John Spurlockcd686b52013-06-05 10:13:46 -0400174 Log.w(TAG, "No icon for slot " + mSlot);
Fabrice Di Meglio82fca5d2013-01-09 15:42:31 -0800175 return false;
176 }
177 if (withClear) {
178 setImageDrawable(null);
179 }
180 setImageDrawable(drawable);
181 return true;
182 }
183
Joe Onoratof5510542010-06-01 07:46:41 -0700184 private Drawable getIcon(StatusBarIcon icon) {
185 return getIcon(getContext(), icon);
186 }
187
Joe Onorato0cbda992010-05-02 16:28:15 -0700188 /**
Dan Sandlerd63f9322015-05-06 15:18:49 -0400189 * Returns the right icon to use for this item
John Spurlock209bede2013-07-17 12:23:27 -0400190 *
Dan Sandlerd63f9322015-05-06 15:18:49 -0400191 * @param context Context to use to get resources
Joe Onorato0cbda992010-05-02 16:28:15 -0700192 * @return Drawable for this item, or null if the package or item could not
193 * be found
194 */
Anthony Chen55e8e1e2016-01-08 10:31:46 -0800195 public static Drawable getIcon(Context context, StatusBarIcon statusBarIcon) {
196 int userId = statusBarIcon.user.getIdentifier();
Dan Sandlerd63f9322015-05-06 15:18:49 -0400197 if (userId == UserHandle.USER_ALL) {
Xiaohui Chen87d0e252015-07-30 15:38:16 -0700198 userId = UserHandle.USER_SYSTEM;
Joe Onorato0cbda992010-05-02 16:28:15 -0700199 }
Anthony Chen55e8e1e2016-01-08 10:31:46 -0800200
201 Drawable icon = statusBarIcon.icon.loadDrawableAsUser(context, userId);
202
203 TypedValue typedValue = new TypedValue();
204 context.getResources().getValue(R.dimen.status_bar_icon_scale_factor, typedValue, true);
205 float scaleFactor = typedValue.getFloat();
206
207 // No need to scale the icon, so return it as is.
208 if (scaleFactor == 1.f) {
209 return icon;
210 }
211
212 return new ScalingDrawableWrapper(icon, scaleFactor);
Joe Onorato0cbda992010-05-02 16:28:15 -0700213 }
Joe Onoratob77f53b2010-05-28 19:59:51 -0400214
215 public StatusBarIcon getStatusBarIcon() {
216 return mIcon;
217 }
Joe Onoratof9ec03c2010-09-23 15:09:18 -0700218
Svetoslav Ganov6179ea32011-06-28 01:12:41 -0700219 @Override
220 public void onInitializeAccessibilityEvent(AccessibilityEvent event) {
221 super.onInitializeAccessibilityEvent(event);
222 if (mNotification != null) {
223 event.setParcelableData(mNotification);
224 }
225 }
226
227 @Override
Joe Onorato6c01a112010-10-04 17:38:47 -0400228 protected void onSizeChanged(int w, int h, int oldw, int oldh) {
229 super.onSizeChanged(w, h, oldw, oldh);
230 if (mNumberBackground != null) {
231 placeNumber();
232 }
233 }
234
Svetoslav Ganov6179ea32011-06-28 01:12:41 -0700235 @Override
Jorim Jaggi66ac1332015-01-21 19:22:26 +0100236 public void onRtlPropertiesChanged(int layoutDirection) {
237 super.onRtlPropertiesChanged(layoutDirection);
238 updateDrawable();
239 }
240
241 @Override
Joe Onorato6c01a112010-10-04 17:38:47 -0400242 protected void onDraw(Canvas canvas) {
243 super.onDraw(canvas);
244
245 if (mNumberBackground != null) {
246 mNumberBackground.draw(canvas);
247 canvas.drawText(mNumberText, mNumberX, mNumberY, mNumberPain);
248 }
249 }
250
Svetoslav Ganov6179ea32011-06-28 01:12:41 -0700251 @Override
Joe Onoratof9ec03c2010-09-23 15:09:18 -0700252 protected void debug(int depth) {
253 super.debug(depth);
254 Log.d("View", debugIndent(depth) + "slot=" + mSlot);
255 Log.d("View", debugIndent(depth) + "icon=" + mIcon);
256 }
Joe Onorato6c01a112010-10-04 17:38:47 -0400257
258 void placeNumber() {
Daniel Sandlerebce0112011-06-16 16:44:51 -0400259 final String str;
John Spurlock01534782014-01-13 11:59:22 -0500260 final int tooBig = getContext().getResources().getInteger(
Daniel Sandlerebce0112011-06-16 16:44:51 -0400261 android.R.integer.status_bar_notification_info_maxnum);
262 if (mIcon.number > tooBig) {
John Spurlock01534782014-01-13 11:59:22 -0500263 str = getContext().getResources().getString(
Daniel Sandlerebce0112011-06-16 16:44:51 -0400264 android.R.string.status_bar_notification_info_overflow);
265 } else {
266 NumberFormat f = NumberFormat.getIntegerInstance();
267 str = f.format(mIcon.number);
268 }
269 mNumberText = str;
270
Joe Onorato6c01a112010-10-04 17:38:47 -0400271 final int w = getWidth();
272 final int h = getHeight();
273 final Rect r = new Rect();
274 mNumberPain.getTextBounds(str, 0, str.length(), r);
275 final int tw = r.right - r.left;
276 final int th = r.bottom - r.top;
277 mNumberBackground.getPadding(r);
278 int dw = r.left + tw + r.right;
279 if (dw < mNumberBackground.getMinimumWidth()) {
280 dw = mNumberBackground.getMinimumWidth();
281 }
282 mNumberX = w-r.right-((dw-r.right-r.left)/2);
283 int dh = r.top + th + r.bottom;
284 if (dh < mNumberBackground.getMinimumWidth()) {
285 dh = mNumberBackground.getMinimumWidth();
286 }
287 mNumberY = h-r.bottom-((dh-r.top-th-r.bottom)/2);
288 mNumberBackground.setBounds(w-dw, h-dh, w, h);
289 }
Svetoslav Ganov6179ea32011-06-28 01:12:41 -0700290
291 private void setContentDescription(Notification notification) {
292 if (notification != null) {
293 CharSequence tickerText = notification.tickerText;
294 if (!TextUtils.isEmpty(tickerText)) {
295 setContentDescription(tickerText);
296 }
297 }
298 }
Daniel Sandler7579bca2011-08-18 15:47:26 -0400299
300 public String toString() {
John Spurlock209bede2013-07-17 12:23:27 -0400301 return "StatusBarIconView(slot=" + mSlot + " icon=" + mIcon
Daniel Sandler7579bca2011-08-18 15:47:26 -0400302 + " notification=" + mNotification + ")";
303 }
Jason Monk3b230072015-05-29 11:11:29 -0400304
305 public String getSlot() {
306 return mSlot;
307 }
Joe Onorato0cbda992010-05-02 16:28:15 -0700308}