blob: d9d9c06061a301a5d5b134607d871d63c50afbb5 [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
19import android.content.Context;
20import android.content.pm.PackageManager;
21import android.content.res.Resources;
22import android.graphics.drawable.Drawable;
23import android.graphics.Canvas;
Joe Onorato6c01a112010-10-04 17:38:47 -040024import android.graphics.Paint;
25import android.graphics.Rect;
Joe Onorato0cbda992010-05-02 16:28:15 -070026import android.util.Slog;
Joe Onoratof9ec03c2010-09-23 15:09:18 -070027import android.util.Log;
Joe Onorato6c01a112010-10-04 17:38:47 -040028import android.view.View;
Joe Onorato0cbda992010-05-02 16:28:15 -070029import android.view.ViewDebug;
30import android.widget.FrameLayout;
31
Daniel Sandlerebce0112011-06-16 16:44:51 -040032import java.text.NumberFormat;
33
Joe Onorato0cbda992010-05-02 16:28:15 -070034import com.android.internal.statusbar.StatusBarIcon;
35
Joe Onorato6c01a112010-10-04 17:38:47 -040036import com.android.systemui.R;
37
Joe Onorato0cbda992010-05-02 16:28:15 -070038public class StatusBarIconView extends AnimatedImageView {
39 private static final String TAG = "StatusBarIconView";
40
41 private StatusBarIcon mIcon;
42 @ViewDebug.ExportedProperty private String mSlot;
Joe Onorato6c01a112010-10-04 17:38:47 -040043 private Drawable mNumberBackground;
44 private Paint mNumberPain;
45 private int mNumberX;
46 private int mNumberY;
47 private String mNumberText;
Joe Onorato0cbda992010-05-02 16:28:15 -070048
49 public StatusBarIconView(Context context, String slot) {
50 super(context);
Joe Onorato6c01a112010-10-04 17:38:47 -040051 final Resources res = context.getResources();
Joe Onorato0cbda992010-05-02 16:28:15 -070052 mSlot = slot;
Joe Onorato6c01a112010-10-04 17:38:47 -040053 mNumberPain = new Paint();
54 mNumberPain.setTextAlign(Paint.Align.CENTER);
55 mNumberPain.setColor(res.getColor(R.drawable.notification_number_text_color));
56 mNumberPain.setAntiAlias(true);
Joe Onorato0cbda992010-05-02 16:28:15 -070057 }
58
59 private static boolean streq(String a, String b) {
Joe Onorato66d7d012010-05-14 10:05:10 -070060 if (a == b) {
61 return true;
62 }
Joe Onorato0cbda992010-05-02 16:28:15 -070063 if (a == null && b != null) {
64 return false;
65 }
66 if (a != null && b == null) {
67 return false;
68 }
69 return a.equals(b);
70 }
71
Joe Onorato005847b2010-06-04 16:08:02 -040072 /**
73 * Returns whether the set succeeded.
74 */
75 public boolean set(StatusBarIcon icon) {
76 final boolean iconEquals = mIcon != null
77 && streq(mIcon.iconPackage, icon.iconPackage)
78 && mIcon.iconId == icon.iconId;
79 final boolean levelEquals = iconEquals
80 && mIcon.iconLevel == icon.iconLevel;
81 final boolean visibilityEquals = mIcon != null
82 && mIcon.visible == icon.visible;
Joe Onorato6c01a112010-10-04 17:38:47 -040083 final boolean numberEquals = mIcon != null
84 && mIcon.number == icon.number;
85 mIcon = icon.clone();
Joe Onorato005847b2010-06-04 16:08:02 -040086 if (!iconEquals) {
87 Drawable drawable = getIcon(icon);
88 if (drawable == null) {
Joe Onoratofd52b182010-11-10 18:00:52 -080089 Slog.w(StatusBar.TAG, "No icon for slot " + mSlot);
Joe Onorato005847b2010-06-04 16:08:02 -040090 return false;
Joe Onorato871bdb92010-05-24 18:36:53 -040091 }
Joe Onorato005847b2010-06-04 16:08:02 -040092 setImageDrawable(drawable);
Joe Onorato0cbda992010-05-02 16:28:15 -070093 }
Joe Onorato005847b2010-06-04 16:08:02 -040094 if (!levelEquals) {
95 setImageLevel(icon.iconLevel);
Joe Onorato0cbda992010-05-02 16:28:15 -070096 }
Joe Onorato6c01a112010-10-04 17:38:47 -040097 if (!numberEquals) {
Joe Onorato8595a3d2010-11-19 18:12:07 -080098 if (icon.number > 0 && mContext.getResources().getBoolean(
99 R.bool.config_statusBarShowNumber)) {
Joe Onorato6c01a112010-10-04 17:38:47 -0400100 if (mNumberBackground == null) {
101 mNumberBackground = getContext().getResources().getDrawable(
102 R.drawable.ic_notification_overlay);
103 }
104 placeNumber();
105 } else {
106 mNumberBackground = null;
107 mNumberText = null;
108 }
109 invalidate();
110 }
Joe Onorato005847b2010-06-04 16:08:02 -0400111 if (!visibilityEquals) {
112 setVisibility(icon.visible ? VISIBLE : GONE);
113 }
Joe Onorato005847b2010-06-04 16:08:02 -0400114 return true;
Joe Onorato0cbda992010-05-02 16:28:15 -0700115 }
116
Joe Onoratof5510542010-06-01 07:46:41 -0700117 private Drawable getIcon(StatusBarIcon icon) {
118 return getIcon(getContext(), icon);
119 }
120
Joe Onorato0cbda992010-05-02 16:28:15 -0700121 /**
122 * Returns the right icon to use for this item, respecting the iconId and
123 * iconPackage (if set)
124 *
125 * @param context Context to use to get resources if iconPackage is not set
126 * @return Drawable for this item, or null if the package or item could not
127 * be found
128 */
Joe Onoratof5510542010-06-01 07:46:41 -0700129 public static Drawable getIcon(Context context, StatusBarIcon icon) {
Joe Onorato0cbda992010-05-02 16:28:15 -0700130 Resources r = null;
131
132 if (icon.iconPackage != null) {
133 try {
134 r = context.getPackageManager().getResourcesForApplication(icon.iconPackage);
135 } catch (PackageManager.NameNotFoundException ex) {
Joe Onoratofd52b182010-11-10 18:00:52 -0800136 Slog.e(StatusBar.TAG, "Icon package not found: " + icon.iconPackage);
Joe Onorato0cbda992010-05-02 16:28:15 -0700137 return null;
138 }
139 } else {
140 r = context.getResources();
141 }
142
143 if (icon.iconId == 0) {
Joe Onorato0cbda992010-05-02 16:28:15 -0700144 return null;
145 }
146
147 try {
148 return r.getDrawable(icon.iconId);
149 } catch (RuntimeException e) {
Joe Onoratofd52b182010-11-10 18:00:52 -0800150 Slog.w(StatusBar.TAG, "Icon not found in "
Joe Onorato0cbda992010-05-02 16:28:15 -0700151 + (icon.iconPackage != null ? icon.iconId : "<system>")
152 + ": " + Integer.toHexString(icon.iconId));
153 }
154
155 return null;
156 }
Joe Onoratob77f53b2010-05-28 19:59:51 -0400157
158 public StatusBarIcon getStatusBarIcon() {
159 return mIcon;
160 }
Joe Onoratof9ec03c2010-09-23 15:09:18 -0700161
Joe Onorato6c01a112010-10-04 17:38:47 -0400162 protected void onSizeChanged(int w, int h, int oldw, int oldh) {
163 super.onSizeChanged(w, h, oldw, oldh);
164 if (mNumberBackground != null) {
165 placeNumber();
166 }
167 }
168
169 protected void onDraw(Canvas canvas) {
170 super.onDraw(canvas);
171
172 if (mNumberBackground != null) {
173 mNumberBackground.draw(canvas);
174 canvas.drawText(mNumberText, mNumberX, mNumberY, mNumberPain);
175 }
176 }
177
Joe Onoratof9ec03c2010-09-23 15:09:18 -0700178 protected void debug(int depth) {
179 super.debug(depth);
180 Log.d("View", debugIndent(depth) + "slot=" + mSlot);
181 Log.d("View", debugIndent(depth) + "icon=" + mIcon);
182 }
Joe Onorato6c01a112010-10-04 17:38:47 -0400183
184 void placeNumber() {
Daniel Sandlerebce0112011-06-16 16:44:51 -0400185 final String str;
186 final int tooBig = mContext.getResources().getInteger(
187 android.R.integer.status_bar_notification_info_maxnum);
188 if (mIcon.number > tooBig) {
189 str = mContext.getResources().getString(
190 android.R.string.status_bar_notification_info_overflow);
191 } else {
192 NumberFormat f = NumberFormat.getIntegerInstance();
193 str = f.format(mIcon.number);
194 }
195 mNumberText = str;
196
Joe Onorato6c01a112010-10-04 17:38:47 -0400197 final int w = getWidth();
198 final int h = getHeight();
199 final Rect r = new Rect();
200 mNumberPain.getTextBounds(str, 0, str.length(), r);
201 final int tw = r.right - r.left;
202 final int th = r.bottom - r.top;
203 mNumberBackground.getPadding(r);
204 int dw = r.left + tw + r.right;
205 if (dw < mNumberBackground.getMinimumWidth()) {
206 dw = mNumberBackground.getMinimumWidth();
207 }
208 mNumberX = w-r.right-((dw-r.right-r.left)/2);
209 int dh = r.top + th + r.bottom;
210 if (dh < mNumberBackground.getMinimumWidth()) {
211 dh = mNumberBackground.getMinimumWidth();
212 }
213 mNumberY = h-r.bottom-((dh-r.top-th-r.bottom)/2);
214 mNumberBackground.setBounds(w-dw, h-dh, w, h);
215 }
Joe Onorato0cbda992010-05-02 16:28:15 -0700216}