blob: 6d76763edc84f0da1727436cf23c95513a31731f [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;
Selim Cinek3e7592d2016-04-11 09:35:54 +080021import android.content.res.Configuration;
Joe Onorato0cbda992010-05-02 16:28:15 -070022import 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;
Dan Sandlerd63f9322015-05-06 15:18:49 -040027import android.graphics.drawable.Icon;
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;
Joe Onorato0cbda992010-05-02 16:28:15 -070033import android.view.ViewDebug;
Svetoslav Ganov6179ea32011-06-28 01:12:41 -070034import android.view.accessibility.AccessibilityEvent;
Winsonc0d70582016-01-29 10:24:39 -080035
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";
Selim Cinek3e7592d2016-04-11 09:35:54 +080043 private boolean mAlwaysScaleIcon;
Joe Onorato0cbda992010-05-02 16:28:15 -070044
45 private StatusBarIcon mIcon;
46 @ViewDebug.ExportedProperty private String mSlot;
Joe Onorato6c01a112010-10-04 17:38:47 -040047 private Drawable mNumberBackground;
48 private Paint mNumberPain;
49 private int mNumberX;
50 private int mNumberY;
51 private String mNumberText;
Svetoslav Ganov6179ea32011-06-28 01:12:41 -070052 private Notification mNotification;
Jason Monk3b230072015-05-29 11:11:29 -040053 private final boolean mBlocked;
Selim Cinek3e7592d2016-04-11 09:35:54 +080054 private int mDensity;
Joe Onorato0cbda992010-05-02 16:28:15 -070055
John Spurlocke6f0a712013-09-03 16:23:49 -040056 public StatusBarIconView(Context context, String slot, Notification notification) {
Jason Monk3b230072015-05-29 11:11:29 -040057 this(context, slot, notification, false);
58 }
59
60 public StatusBarIconView(Context context, String slot, Notification notification,
61 boolean blocked) {
Joe Onorato0cbda992010-05-02 16:28:15 -070062 super(context);
Jason Monk3b230072015-05-29 11:11:29 -040063 mBlocked = blocked;
Joe Onorato0cbda992010-05-02 16:28:15 -070064 mSlot = slot;
Joe Onorato6c01a112010-10-04 17:38:47 -040065 mNumberPain = new Paint();
66 mNumberPain.setTextAlign(Paint.Align.CENTER);
Alan Viverette4a357cd2015-03-18 18:37:18 -070067 mNumberPain.setColor(context.getColor(R.drawable.notification_number_text_color));
Joe Onorato6c01a112010-10-04 17:38:47 -040068 mNumberPain.setAntiAlias(true);
Christoph Studera0506e72014-07-31 20:27:39 +020069 setNotification(notification);
Selim Cinek3e7592d2016-04-11 09:35:54 +080070 maybeUpdateIconScale();
71 setScaleType(ScaleType.CENTER);
72 mDensity = context.getResources().getDisplayMetrics().densityDpi;
73 }
Daniel Sandler26c84b12011-07-27 00:09:40 -040074
Selim Cinek3e7592d2016-04-11 09:35:54 +080075 private void maybeUpdateIconScale() {
Daniel Sandler7579bca2011-08-18 15:47:26 -040076 // We do not resize and scale system icons (on the right), only notification icons (on the
77 // left).
Selim Cinek3e7592d2016-04-11 09:35:54 +080078 if (mNotification != null || mAlwaysScaleIcon) {
79 updateIconScale();
Daniel Sandler7579bca2011-08-18 15:47:26 -040080 }
Selim Cinek3e7592d2016-04-11 09:35:54 +080081 }
Daniel Sandlerabff0322011-09-27 11:19:34 -040082
Selim Cinek3e7592d2016-04-11 09:35:54 +080083 private void updateIconScale() {
84 Resources res = mContext.getResources();
85 final int outerBounds = res.getDimensionPixelSize(R.dimen.status_bar_icon_size);
86 final int imageBounds = res.getDimensionPixelSize(R.dimen.status_bar_icon_drawing_size);
87 final float scale = (float)imageBounds / (float)outerBounds;
88 setScaleX(scale);
89 setScaleY(scale);
90 }
91
92 @Override
93 protected void onConfigurationChanged(Configuration newConfig) {
94 super.onConfigurationChanged(newConfig);
95 int density = newConfig.densityDpi;
96 if (density != mDensity) {
97 mDensity = density;
98 maybeUpdateIconScale();
99 updateDrawable();
100 }
Joe Onorato0cbda992010-05-02 16:28:15 -0700101 }
102
Christoph Studera0506e72014-07-31 20:27:39 +0200103 public void setNotification(Notification notification) {
104 mNotification = notification;
105 setContentDescription(notification);
106 }
107
Daniel Sandler05e24142011-11-10 11:56:49 -0500108 public StatusBarIconView(Context context, AttributeSet attrs) {
109 super(context, attrs);
Jason Monk3b230072015-05-29 11:11:29 -0400110 mBlocked = false;
Selim Cinek3e7592d2016-04-11 09:35:54 +0800111 mAlwaysScaleIcon = true;
112 updateIconScale();
113 mDensity = context.getResources().getDisplayMetrics().densityDpi;
Daniel Sandler05e24142011-11-10 11:56:49 -0500114 }
115
Joe Onorato0cbda992010-05-02 16:28:15 -0700116 private static boolean streq(String a, String b) {
Joe Onorato66d7d012010-05-14 10:05:10 -0700117 if (a == b) {
118 return true;
119 }
Joe Onorato0cbda992010-05-02 16:28:15 -0700120 if (a == null && b != null) {
121 return false;
122 }
123 if (a != null && b == null) {
124 return false;
125 }
126 return a.equals(b);
127 }
128
Dan Sandlerd63f9322015-05-06 15:18:49 -0400129 public boolean equalIcons(Icon a, Icon b) {
130 if (a == b) return true;
131 if (a.getType() != b.getType()) return false;
132 switch (a.getType()) {
133 case Icon.TYPE_RESOURCE:
134 return a.getResPackage().equals(b.getResPackage()) && a.getResId() == b.getResId();
135 case Icon.TYPE_URI:
136 return a.getUriString().equals(b.getUriString());
137 default:
138 return false;
139 }
140 }
Joe Onorato005847b2010-06-04 16:08:02 -0400141 /**
142 * Returns whether the set succeeded.
143 */
144 public boolean set(StatusBarIcon icon) {
Dan Sandlerd63f9322015-05-06 15:18:49 -0400145 final boolean iconEquals = mIcon != null && equalIcons(mIcon.icon, icon.icon);
Joe Onorato005847b2010-06-04 16:08:02 -0400146 final boolean levelEquals = iconEquals
147 && mIcon.iconLevel == icon.iconLevel;
148 final boolean visibilityEquals = mIcon != null
149 && mIcon.visible == icon.visible;
Joe Onorato6c01a112010-10-04 17:38:47 -0400150 final boolean numberEquals = mIcon != null
151 && mIcon.number == icon.number;
152 mIcon = icon.clone();
Svetoslav Ganov6179ea32011-06-28 01:12:41 -0700153 setContentDescription(icon.contentDescription);
Joe Onorato005847b2010-06-04 16:08:02 -0400154 if (!iconEquals) {
Fabrice Di Meglio82fca5d2013-01-09 15:42:31 -0800155 if (!updateDrawable(false /* no clear */)) return false;
Joe Onorato0cbda992010-05-02 16:28:15 -0700156 }
Joe Onorato005847b2010-06-04 16:08:02 -0400157 if (!levelEquals) {
158 setImageLevel(icon.iconLevel);
Joe Onorato0cbda992010-05-02 16:28:15 -0700159 }
Daniel Sandler26c84b12011-07-27 00:09:40 -0400160
Joe Onorato6c01a112010-10-04 17:38:47 -0400161 if (!numberEquals) {
John Spurlock01534782014-01-13 11:59:22 -0500162 if (icon.number > 0 && getContext().getResources().getBoolean(
Joe Onorato8595a3d2010-11-19 18:12:07 -0800163 R.bool.config_statusBarShowNumber)) {
Joe Onorato6c01a112010-10-04 17:38:47 -0400164 if (mNumberBackground == null) {
165 mNumberBackground = getContext().getResources().getDrawable(
166 R.drawable.ic_notification_overlay);
167 }
168 placeNumber();
169 } else {
170 mNumberBackground = null;
171 mNumberText = null;
172 }
173 invalidate();
174 }
Joe Onorato005847b2010-06-04 16:08:02 -0400175 if (!visibilityEquals) {
Jason Monk3b230072015-05-29 11:11:29 -0400176 setVisibility(icon.visible && !mBlocked ? VISIBLE : GONE);
Joe Onorato005847b2010-06-04 16:08:02 -0400177 }
Joe Onorato005847b2010-06-04 16:08:02 -0400178 return true;
Joe Onorato0cbda992010-05-02 16:28:15 -0700179 }
180
Fabrice Di Meglio82fca5d2013-01-09 15:42:31 -0800181 public void updateDrawable() {
182 updateDrawable(true /* with clear */);
183 }
184
185 private boolean updateDrawable(boolean withClear) {
Jorim Jaggi66ac1332015-01-21 19:22:26 +0100186 if (mIcon == null) {
187 return false;
188 }
Fabrice Di Meglio82fca5d2013-01-09 15:42:31 -0800189 Drawable drawable = getIcon(mIcon);
190 if (drawable == null) {
John Spurlockcd686b52013-06-05 10:13:46 -0400191 Log.w(TAG, "No icon for slot " + mSlot);
Fabrice Di Meglio82fca5d2013-01-09 15:42:31 -0800192 return false;
193 }
194 if (withClear) {
195 setImageDrawable(null);
196 }
197 setImageDrawable(drawable);
198 return true;
199 }
200
Joe Onoratof5510542010-06-01 07:46:41 -0700201 private Drawable getIcon(StatusBarIcon icon) {
202 return getIcon(getContext(), icon);
203 }
204
Joe Onorato0cbda992010-05-02 16:28:15 -0700205 /**
Dan Sandlerd63f9322015-05-06 15:18:49 -0400206 * Returns the right icon to use for this item
John Spurlock209bede2013-07-17 12:23:27 -0400207 *
Dan Sandlerd63f9322015-05-06 15:18:49 -0400208 * @param context Context to use to get resources
Joe Onorato0cbda992010-05-02 16:28:15 -0700209 * @return Drawable for this item, or null if the package or item could not
210 * be found
211 */
Anthony Chen55e8e1e2016-01-08 10:31:46 -0800212 public static Drawable getIcon(Context context, StatusBarIcon statusBarIcon) {
213 int userId = statusBarIcon.user.getIdentifier();
Dan Sandlerd63f9322015-05-06 15:18:49 -0400214 if (userId == UserHandle.USER_ALL) {
Xiaohui Chen87d0e252015-07-30 15:38:16 -0700215 userId = UserHandle.USER_SYSTEM;
Joe Onorato0cbda992010-05-02 16:28:15 -0700216 }
Anthony Chen55e8e1e2016-01-08 10:31:46 -0800217
218 Drawable icon = statusBarIcon.icon.loadDrawableAsUser(context, userId);
219
220 TypedValue typedValue = new TypedValue();
221 context.getResources().getValue(R.dimen.status_bar_icon_scale_factor, typedValue, true);
222 float scaleFactor = typedValue.getFloat();
223
224 // No need to scale the icon, so return it as is.
225 if (scaleFactor == 1.f) {
226 return icon;
227 }
228
229 return new ScalingDrawableWrapper(icon, scaleFactor);
Joe Onorato0cbda992010-05-02 16:28:15 -0700230 }
Joe Onoratob77f53b2010-05-28 19:59:51 -0400231
232 public StatusBarIcon getStatusBarIcon() {
233 return mIcon;
234 }
Joe Onoratof9ec03c2010-09-23 15:09:18 -0700235
Svetoslav Ganov6179ea32011-06-28 01:12:41 -0700236 @Override
237 public void onInitializeAccessibilityEvent(AccessibilityEvent event) {
238 super.onInitializeAccessibilityEvent(event);
239 if (mNotification != null) {
240 event.setParcelableData(mNotification);
241 }
242 }
243
244 @Override
Joe Onorato6c01a112010-10-04 17:38:47 -0400245 protected void onSizeChanged(int w, int h, int oldw, int oldh) {
246 super.onSizeChanged(w, h, oldw, oldh);
247 if (mNumberBackground != null) {
248 placeNumber();
249 }
250 }
251
Svetoslav Ganov6179ea32011-06-28 01:12:41 -0700252 @Override
Jorim Jaggi66ac1332015-01-21 19:22:26 +0100253 public void onRtlPropertiesChanged(int layoutDirection) {
254 super.onRtlPropertiesChanged(layoutDirection);
255 updateDrawable();
256 }
257
258 @Override
Joe Onorato6c01a112010-10-04 17:38:47 -0400259 protected void onDraw(Canvas canvas) {
260 super.onDraw(canvas);
261
262 if (mNumberBackground != null) {
263 mNumberBackground.draw(canvas);
264 canvas.drawText(mNumberText, mNumberX, mNumberY, mNumberPain);
265 }
266 }
267
Svetoslav Ganov6179ea32011-06-28 01:12:41 -0700268 @Override
Joe Onoratof9ec03c2010-09-23 15:09:18 -0700269 protected void debug(int depth) {
270 super.debug(depth);
271 Log.d("View", debugIndent(depth) + "slot=" + mSlot);
272 Log.d("View", debugIndent(depth) + "icon=" + mIcon);
273 }
Joe Onorato6c01a112010-10-04 17:38:47 -0400274
275 void placeNumber() {
Daniel Sandlerebce0112011-06-16 16:44:51 -0400276 final String str;
John Spurlock01534782014-01-13 11:59:22 -0500277 final int tooBig = getContext().getResources().getInteger(
Daniel Sandlerebce0112011-06-16 16:44:51 -0400278 android.R.integer.status_bar_notification_info_maxnum);
279 if (mIcon.number > tooBig) {
John Spurlock01534782014-01-13 11:59:22 -0500280 str = getContext().getResources().getString(
Daniel Sandlerebce0112011-06-16 16:44:51 -0400281 android.R.string.status_bar_notification_info_overflow);
282 } else {
283 NumberFormat f = NumberFormat.getIntegerInstance();
284 str = f.format(mIcon.number);
285 }
286 mNumberText = str;
287
Joe Onorato6c01a112010-10-04 17:38:47 -0400288 final int w = getWidth();
289 final int h = getHeight();
290 final Rect r = new Rect();
291 mNumberPain.getTextBounds(str, 0, str.length(), r);
292 final int tw = r.right - r.left;
293 final int th = r.bottom - r.top;
294 mNumberBackground.getPadding(r);
295 int dw = r.left + tw + r.right;
296 if (dw < mNumberBackground.getMinimumWidth()) {
297 dw = mNumberBackground.getMinimumWidth();
298 }
299 mNumberX = w-r.right-((dw-r.right-r.left)/2);
300 int dh = r.top + th + r.bottom;
301 if (dh < mNumberBackground.getMinimumWidth()) {
302 dh = mNumberBackground.getMinimumWidth();
303 }
304 mNumberY = h-r.bottom-((dh-r.top-th-r.bottom)/2);
305 mNumberBackground.setBounds(w-dw, h-dh, w, h);
306 }
Svetoslav Ganov6179ea32011-06-28 01:12:41 -0700307
308 private void setContentDescription(Notification notification) {
309 if (notification != null) {
Adrian Rooseba05822016-04-22 17:09:27 -0700310 String d = contentDescForNotification(mContext, notification);
311 if (!TextUtils.isEmpty(d)) {
312 setContentDescription(d);
Svetoslav Ganov6179ea32011-06-28 01:12:41 -0700313 }
314 }
315 }
Daniel Sandler7579bca2011-08-18 15:47:26 -0400316
317 public String toString() {
John Spurlock209bede2013-07-17 12:23:27 -0400318 return "StatusBarIconView(slot=" + mSlot + " icon=" + mIcon
Daniel Sandler7579bca2011-08-18 15:47:26 -0400319 + " notification=" + mNotification + ")";
320 }
Jason Monk3b230072015-05-29 11:11:29 -0400321
322 public String getSlot() {
323 return mSlot;
324 }
Adrian Rooseba05822016-04-22 17:09:27 -0700325
326
327 public static String contentDescForNotification(Context c, Notification n) {
328 Notification.Builder builder = Notification.Builder.recoverBuilder(c, n);
329 String appName = builder.loadHeaderAppName();
330 CharSequence ticker = n.tickerText;
331 return c.getString(R.string.accessibility_desc_notification_icon, appName, ticker);
332 }
Joe Onorato0cbda992010-05-02 16:28:15 -0700333}