blob: cdfdad46406504b51345bc8e83c7b7e87cb6585d [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;
Adrian Roosa8e18ef2016-05-26 17:17:02 -070021import android.content.pm.ApplicationInfo;
Selim Cinek3e7592d2016-04-11 09:35:54 +080022import android.content.res.Configuration;
Joe Onorato0cbda992010-05-02 16:28:15 -070023import android.content.res.Resources;
Joe Onorato0cbda992010-05-02 16:28:15 -070024import android.graphics.Canvas;
Joe Onorato6c01a112010-10-04 17:38:47 -040025import android.graphics.Paint;
26import android.graphics.Rect;
John Spurlockde84f0e2013-06-12 12:41:00 -040027import android.graphics.drawable.Drawable;
Dan Sandlerd63f9322015-05-06 15:18:49 -040028import android.graphics.drawable.Icon;
Adrian Roosa8e18ef2016-05-26 17:17:02 -070029import android.os.Parcelable;
Jeff Sharkeyded653b2012-09-27 19:09:24 -070030import android.os.UserHandle;
Svetoslav Ganov6179ea32011-06-28 01:12:41 -070031import android.text.TextUtils;
Daniel Sandler05e24142011-11-10 11:56:49 -050032import android.util.AttributeSet;
Joe Onoratof9ec03c2010-09-23 15:09:18 -070033import android.util.Log;
Anthony Chen55e8e1e2016-01-08 10:31:46 -080034import android.util.TypedValue;
Joe Onorato0cbda992010-05-02 16:28:15 -070035import android.view.ViewDebug;
Svetoslav Ganov6179ea32011-06-28 01:12:41 -070036import android.view.accessibility.AccessibilityEvent;
Winsonc0d70582016-01-29 10:24:39 -080037
Joe Onorato0cbda992010-05-02 16:28:15 -070038import com.android.internal.statusbar.StatusBarIcon;
Joe Onorato6c01a112010-10-04 17:38:47 -040039import com.android.systemui.R;
40
John Spurlockde84f0e2013-06-12 12:41:00 -040041import java.text.NumberFormat;
42
Joe Onorato0cbda992010-05-02 16:28:15 -070043public class StatusBarIconView extends AnimatedImageView {
44 private static final String TAG = "StatusBarIconView";
Selim Cinek3e7592d2016-04-11 09:35:54 +080045 private boolean mAlwaysScaleIcon;
Joe Onorato0cbda992010-05-02 16:28:15 -070046
47 private StatusBarIcon mIcon;
48 @ViewDebug.ExportedProperty private String mSlot;
Joe Onorato6c01a112010-10-04 17:38:47 -040049 private Drawable mNumberBackground;
50 private Paint mNumberPain;
51 private int mNumberX;
52 private int mNumberY;
53 private String mNumberText;
Svetoslav Ganov6179ea32011-06-28 01:12:41 -070054 private Notification mNotification;
Jason Monk3b230072015-05-29 11:11:29 -040055 private final boolean mBlocked;
Selim Cinek3e7592d2016-04-11 09:35:54 +080056 private int mDensity;
Joe Onorato0cbda992010-05-02 16:28:15 -070057
John Spurlocke6f0a712013-09-03 16:23:49 -040058 public StatusBarIconView(Context context, String slot, Notification notification) {
Jason Monk3b230072015-05-29 11:11:29 -040059 this(context, slot, notification, false);
60 }
61
62 public StatusBarIconView(Context context, String slot, Notification notification,
63 boolean blocked) {
Joe Onorato0cbda992010-05-02 16:28:15 -070064 super(context);
Jason Monk3b230072015-05-29 11:11:29 -040065 mBlocked = blocked;
Joe Onorato0cbda992010-05-02 16:28:15 -070066 mSlot = slot;
Joe Onorato6c01a112010-10-04 17:38:47 -040067 mNumberPain = new Paint();
68 mNumberPain.setTextAlign(Paint.Align.CENTER);
Alan Viverette4a357cd2015-03-18 18:37:18 -070069 mNumberPain.setColor(context.getColor(R.drawable.notification_number_text_color));
Joe Onorato6c01a112010-10-04 17:38:47 -040070 mNumberPain.setAntiAlias(true);
Christoph Studera0506e72014-07-31 20:27:39 +020071 setNotification(notification);
Selim Cinek3e7592d2016-04-11 09:35:54 +080072 maybeUpdateIconScale();
73 setScaleType(ScaleType.CENTER);
74 mDensity = context.getResources().getDisplayMetrics().densityDpi;
75 }
Daniel Sandler26c84b12011-07-27 00:09:40 -040076
Selim Cinek3e7592d2016-04-11 09:35:54 +080077 private void maybeUpdateIconScale() {
Daniel Sandler7579bca2011-08-18 15:47:26 -040078 // We do not resize and scale system icons (on the right), only notification icons (on the
79 // left).
Selim Cinek3e7592d2016-04-11 09:35:54 +080080 if (mNotification != null || mAlwaysScaleIcon) {
81 updateIconScale();
Daniel Sandler7579bca2011-08-18 15:47:26 -040082 }
Selim Cinek3e7592d2016-04-11 09:35:54 +080083 }
Daniel Sandlerabff0322011-09-27 11:19:34 -040084
Selim Cinek3e7592d2016-04-11 09:35:54 +080085 private void updateIconScale() {
86 Resources res = mContext.getResources();
87 final int outerBounds = res.getDimensionPixelSize(R.dimen.status_bar_icon_size);
88 final int imageBounds = res.getDimensionPixelSize(R.dimen.status_bar_icon_drawing_size);
89 final float scale = (float)imageBounds / (float)outerBounds;
90 setScaleX(scale);
91 setScaleY(scale);
92 }
93
94 @Override
95 protected void onConfigurationChanged(Configuration newConfig) {
96 super.onConfigurationChanged(newConfig);
97 int density = newConfig.densityDpi;
98 if (density != mDensity) {
99 mDensity = density;
100 maybeUpdateIconScale();
101 updateDrawable();
102 }
Joe Onorato0cbda992010-05-02 16:28:15 -0700103 }
104
Christoph Studera0506e72014-07-31 20:27:39 +0200105 public void setNotification(Notification notification) {
106 mNotification = notification;
107 setContentDescription(notification);
108 }
109
Daniel Sandler05e24142011-11-10 11:56:49 -0500110 public StatusBarIconView(Context context, AttributeSet attrs) {
111 super(context, attrs);
Jason Monk3b230072015-05-29 11:11:29 -0400112 mBlocked = false;
Selim Cinek3e7592d2016-04-11 09:35:54 +0800113 mAlwaysScaleIcon = true;
114 updateIconScale();
115 mDensity = context.getResources().getDisplayMetrics().densityDpi;
Daniel Sandler05e24142011-11-10 11:56:49 -0500116 }
117
Joe Onorato0cbda992010-05-02 16:28:15 -0700118 private static boolean streq(String a, String b) {
Joe Onorato66d7d012010-05-14 10:05:10 -0700119 if (a == b) {
120 return true;
121 }
Joe Onorato0cbda992010-05-02 16:28:15 -0700122 if (a == null && b != null) {
123 return false;
124 }
125 if (a != null && b == null) {
126 return false;
127 }
128 return a.equals(b);
129 }
130
Dan Sandlerd63f9322015-05-06 15:18:49 -0400131 public boolean equalIcons(Icon a, Icon b) {
132 if (a == b) return true;
133 if (a.getType() != b.getType()) return false;
134 switch (a.getType()) {
135 case Icon.TYPE_RESOURCE:
136 return a.getResPackage().equals(b.getResPackage()) && a.getResId() == b.getResId();
137 case Icon.TYPE_URI:
138 return a.getUriString().equals(b.getUriString());
139 default:
140 return false;
141 }
142 }
Joe Onorato005847b2010-06-04 16:08:02 -0400143 /**
144 * Returns whether the set succeeded.
145 */
146 public boolean set(StatusBarIcon icon) {
Dan Sandlerd63f9322015-05-06 15:18:49 -0400147 final boolean iconEquals = mIcon != null && equalIcons(mIcon.icon, icon.icon);
Joe Onorato005847b2010-06-04 16:08:02 -0400148 final boolean levelEquals = iconEquals
149 && mIcon.iconLevel == icon.iconLevel;
150 final boolean visibilityEquals = mIcon != null
151 && mIcon.visible == icon.visible;
Joe Onorato6c01a112010-10-04 17:38:47 -0400152 final boolean numberEquals = mIcon != null
153 && mIcon.number == icon.number;
154 mIcon = icon.clone();
Svetoslav Ganov6179ea32011-06-28 01:12:41 -0700155 setContentDescription(icon.contentDescription);
Joe Onorato005847b2010-06-04 16:08:02 -0400156 if (!iconEquals) {
Fabrice Di Meglio82fca5d2013-01-09 15:42:31 -0800157 if (!updateDrawable(false /* no clear */)) return false;
Joe Onorato0cbda992010-05-02 16:28:15 -0700158 }
Joe Onorato005847b2010-06-04 16:08:02 -0400159 if (!levelEquals) {
160 setImageLevel(icon.iconLevel);
Joe Onorato0cbda992010-05-02 16:28:15 -0700161 }
Daniel Sandler26c84b12011-07-27 00:09:40 -0400162
Joe Onorato6c01a112010-10-04 17:38:47 -0400163 if (!numberEquals) {
John Spurlock01534782014-01-13 11:59:22 -0500164 if (icon.number > 0 && getContext().getResources().getBoolean(
Joe Onorato8595a3d2010-11-19 18:12:07 -0800165 R.bool.config_statusBarShowNumber)) {
Joe Onorato6c01a112010-10-04 17:38:47 -0400166 if (mNumberBackground == null) {
167 mNumberBackground = getContext().getResources().getDrawable(
168 R.drawable.ic_notification_overlay);
169 }
170 placeNumber();
171 } else {
172 mNumberBackground = null;
173 mNumberText = null;
174 }
175 invalidate();
176 }
Joe Onorato005847b2010-06-04 16:08:02 -0400177 if (!visibilityEquals) {
Jason Monk3b230072015-05-29 11:11:29 -0400178 setVisibility(icon.visible && !mBlocked ? VISIBLE : GONE);
Joe Onorato005847b2010-06-04 16:08:02 -0400179 }
Joe Onorato005847b2010-06-04 16:08:02 -0400180 return true;
Joe Onorato0cbda992010-05-02 16:28:15 -0700181 }
182
Fabrice Di Meglio82fca5d2013-01-09 15:42:31 -0800183 public void updateDrawable() {
184 updateDrawable(true /* with clear */);
185 }
186
187 private boolean updateDrawable(boolean withClear) {
Jorim Jaggi66ac1332015-01-21 19:22:26 +0100188 if (mIcon == null) {
189 return false;
190 }
Fabrice Di Meglio82fca5d2013-01-09 15:42:31 -0800191 Drawable drawable = getIcon(mIcon);
192 if (drawable == null) {
John Spurlockcd686b52013-06-05 10:13:46 -0400193 Log.w(TAG, "No icon for slot " + mSlot);
Fabrice Di Meglio82fca5d2013-01-09 15:42:31 -0800194 return false;
195 }
196 if (withClear) {
197 setImageDrawable(null);
198 }
199 setImageDrawable(drawable);
200 return true;
201 }
202
Joe Onoratof5510542010-06-01 07:46:41 -0700203 private Drawable getIcon(StatusBarIcon icon) {
204 return getIcon(getContext(), icon);
205 }
206
Joe Onorato0cbda992010-05-02 16:28:15 -0700207 /**
Dan Sandlerd63f9322015-05-06 15:18:49 -0400208 * Returns the right icon to use for this item
John Spurlock209bede2013-07-17 12:23:27 -0400209 *
Dan Sandlerd63f9322015-05-06 15:18:49 -0400210 * @param context Context to use to get resources
Joe Onorato0cbda992010-05-02 16:28:15 -0700211 * @return Drawable for this item, or null if the package or item could not
212 * be found
213 */
Anthony Chen55e8e1e2016-01-08 10:31:46 -0800214 public static Drawable getIcon(Context context, StatusBarIcon statusBarIcon) {
215 int userId = statusBarIcon.user.getIdentifier();
Dan Sandlerd63f9322015-05-06 15:18:49 -0400216 if (userId == UserHandle.USER_ALL) {
Xiaohui Chen87d0e252015-07-30 15:38:16 -0700217 userId = UserHandle.USER_SYSTEM;
Joe Onorato0cbda992010-05-02 16:28:15 -0700218 }
Anthony Chen55e8e1e2016-01-08 10:31:46 -0800219
220 Drawable icon = statusBarIcon.icon.loadDrawableAsUser(context, userId);
221
222 TypedValue typedValue = new TypedValue();
223 context.getResources().getValue(R.dimen.status_bar_icon_scale_factor, typedValue, true);
224 float scaleFactor = typedValue.getFloat();
225
226 // No need to scale the icon, so return it as is.
227 if (scaleFactor == 1.f) {
228 return icon;
229 }
230
231 return new ScalingDrawableWrapper(icon, scaleFactor);
Joe Onorato0cbda992010-05-02 16:28:15 -0700232 }
Joe Onoratob77f53b2010-05-28 19:59:51 -0400233
234 public StatusBarIcon getStatusBarIcon() {
235 return mIcon;
236 }
Joe Onoratof9ec03c2010-09-23 15:09:18 -0700237
Svetoslav Ganov6179ea32011-06-28 01:12:41 -0700238 @Override
239 public void onInitializeAccessibilityEvent(AccessibilityEvent event) {
240 super.onInitializeAccessibilityEvent(event);
241 if (mNotification != null) {
242 event.setParcelableData(mNotification);
243 }
244 }
245
246 @Override
Joe Onorato6c01a112010-10-04 17:38:47 -0400247 protected void onSizeChanged(int w, int h, int oldw, int oldh) {
248 super.onSizeChanged(w, h, oldw, oldh);
249 if (mNumberBackground != null) {
250 placeNumber();
251 }
252 }
253
Svetoslav Ganov6179ea32011-06-28 01:12:41 -0700254 @Override
Jorim Jaggi66ac1332015-01-21 19:22:26 +0100255 public void onRtlPropertiesChanged(int layoutDirection) {
256 super.onRtlPropertiesChanged(layoutDirection);
257 updateDrawable();
258 }
259
260 @Override
Joe Onorato6c01a112010-10-04 17:38:47 -0400261 protected void onDraw(Canvas canvas) {
262 super.onDraw(canvas);
263
264 if (mNumberBackground != null) {
265 mNumberBackground.draw(canvas);
266 canvas.drawText(mNumberText, mNumberX, mNumberY, mNumberPain);
267 }
268 }
269
Svetoslav Ganov6179ea32011-06-28 01:12:41 -0700270 @Override
Joe Onoratof9ec03c2010-09-23 15:09:18 -0700271 protected void debug(int depth) {
272 super.debug(depth);
273 Log.d("View", debugIndent(depth) + "slot=" + mSlot);
274 Log.d("View", debugIndent(depth) + "icon=" + mIcon);
275 }
Joe Onorato6c01a112010-10-04 17:38:47 -0400276
277 void placeNumber() {
Daniel Sandlerebce0112011-06-16 16:44:51 -0400278 final String str;
John Spurlock01534782014-01-13 11:59:22 -0500279 final int tooBig = getContext().getResources().getInteger(
Daniel Sandlerebce0112011-06-16 16:44:51 -0400280 android.R.integer.status_bar_notification_info_maxnum);
281 if (mIcon.number > tooBig) {
John Spurlock01534782014-01-13 11:59:22 -0500282 str = getContext().getResources().getString(
Daniel Sandlerebce0112011-06-16 16:44:51 -0400283 android.R.string.status_bar_notification_info_overflow);
284 } else {
285 NumberFormat f = NumberFormat.getIntegerInstance();
286 str = f.format(mIcon.number);
287 }
288 mNumberText = str;
289
Joe Onorato6c01a112010-10-04 17:38:47 -0400290 final int w = getWidth();
291 final int h = getHeight();
292 final Rect r = new Rect();
293 mNumberPain.getTextBounds(str, 0, str.length(), r);
294 final int tw = r.right - r.left;
295 final int th = r.bottom - r.top;
296 mNumberBackground.getPadding(r);
297 int dw = r.left + tw + r.right;
298 if (dw < mNumberBackground.getMinimumWidth()) {
299 dw = mNumberBackground.getMinimumWidth();
300 }
301 mNumberX = w-r.right-((dw-r.right-r.left)/2);
302 int dh = r.top + th + r.bottom;
303 if (dh < mNumberBackground.getMinimumWidth()) {
304 dh = mNumberBackground.getMinimumWidth();
305 }
306 mNumberY = h-r.bottom-((dh-r.top-th-r.bottom)/2);
307 mNumberBackground.setBounds(w-dw, h-dh, w, h);
308 }
Svetoslav Ganov6179ea32011-06-28 01:12:41 -0700309
310 private void setContentDescription(Notification notification) {
311 if (notification != null) {
Adrian Rooseba05822016-04-22 17:09:27 -0700312 String d = contentDescForNotification(mContext, notification);
313 if (!TextUtils.isEmpty(d)) {
314 setContentDescription(d);
Svetoslav Ganov6179ea32011-06-28 01:12:41 -0700315 }
316 }
317 }
Daniel Sandler7579bca2011-08-18 15:47:26 -0400318
319 public String toString() {
John Spurlock209bede2013-07-17 12:23:27 -0400320 return "StatusBarIconView(slot=" + mSlot + " icon=" + mIcon
Daniel Sandler7579bca2011-08-18 15:47:26 -0400321 + " notification=" + mNotification + ")";
322 }
Jason Monk3b230072015-05-29 11:11:29 -0400323
324 public String getSlot() {
325 return mSlot;
326 }
Adrian Rooseba05822016-04-22 17:09:27 -0700327
328
329 public static String contentDescForNotification(Context c, Notification n) {
Adrian Roosa8e18ef2016-05-26 17:17:02 -0700330 String appName = "";
331 try {
332 Notification.Builder builder = Notification.Builder.recoverBuilder(c, n);
333 appName = builder.loadHeaderAppName();
334 } catch (RuntimeException e) {
335 Log.e(TAG, "Unable to recover builder", e);
336 // Trying to get the app name from the app info instead.
337 Parcelable appInfo = n.extras.getParcelable(
338 Notification.EXTRA_BUILDER_APPLICATION_INFO);
339 if (appInfo instanceof ApplicationInfo) {
340 appName = String.valueOf(((ApplicationInfo) appInfo).loadLabel(
341 c.getPackageManager()));
342 }
343 }
Adrian Roos51548e62016-04-28 11:20:26 -0700344
Julia Reynolds7f9ce782016-05-17 15:34:34 -0400345 CharSequence title = n.extras.getCharSequence(Notification.EXTRA_TITLE);
Adrian Rooseba05822016-04-22 17:09:27 -0700346 CharSequence ticker = n.tickerText;
Adrian Roos51548e62016-04-28 11:20:26 -0700347
348 CharSequence desc = !TextUtils.isEmpty(ticker) ? ticker
349 : !TextUtils.isEmpty(title) ? title : "";
350
351 return c.getString(R.string.accessibility_desc_notification_icon, appName, desc);
Adrian Rooseba05822016-04-22 17:09:27 -0700352 }
Adrian Roos51548e62016-04-28 11:20:26 -0700353
Joe Onorato0cbda992010-05-02 16:28:15 -0700354}