blob: 8419e565eb73cdf65bc8f3d8b287cb40d419c275 [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
32import com.android.internal.statusbar.StatusBarIcon;
33
Joe Onorato6c01a112010-10-04 17:38:47 -040034import com.android.systemui.R;
35
Joe Onorato0cbda992010-05-02 16:28:15 -070036public class StatusBarIconView extends AnimatedImageView {
37 private static final String TAG = "StatusBarIconView";
38
39 private StatusBarIcon mIcon;
40 @ViewDebug.ExportedProperty private String mSlot;
Joe Onorato6c01a112010-10-04 17:38:47 -040041 private Drawable mNumberBackground;
42 private Paint mNumberPain;
43 private int mNumberX;
44 private int mNumberY;
45 private String mNumberText;
Joe Onorato0cbda992010-05-02 16:28:15 -070046
47 public StatusBarIconView(Context context, String slot) {
48 super(context);
Joe Onorato6c01a112010-10-04 17:38:47 -040049 final Resources res = context.getResources();
Joe Onorato0cbda992010-05-02 16:28:15 -070050 mSlot = slot;
Joe Onorato6c01a112010-10-04 17:38:47 -040051 mNumberPain = new Paint();
52 mNumberPain.setTextAlign(Paint.Align.CENTER);
53 mNumberPain.setColor(res.getColor(R.drawable.notification_number_text_color));
54 mNumberPain.setAntiAlias(true);
Joe Onorato0cbda992010-05-02 16:28:15 -070055 }
56
57 private static boolean streq(String a, String b) {
Joe Onorato66d7d012010-05-14 10:05:10 -070058 if (a == b) {
59 return true;
60 }
Joe Onorato0cbda992010-05-02 16:28:15 -070061 if (a == null && b != null) {
62 return false;
63 }
64 if (a != null && b == null) {
65 return false;
66 }
67 return a.equals(b);
68 }
69
Joe Onorato005847b2010-06-04 16:08:02 -040070 /**
71 * Returns whether the set succeeded.
72 */
73 public boolean set(StatusBarIcon icon) {
74 final boolean iconEquals = mIcon != null
75 && streq(mIcon.iconPackage, icon.iconPackage)
76 && mIcon.iconId == icon.iconId;
77 final boolean levelEquals = iconEquals
78 && mIcon.iconLevel == icon.iconLevel;
79 final boolean visibilityEquals = mIcon != null
80 && mIcon.visible == icon.visible;
Joe Onorato6c01a112010-10-04 17:38:47 -040081 final boolean numberEquals = mIcon != null
82 && mIcon.number == icon.number;
83 mIcon = icon.clone();
Joe Onorato005847b2010-06-04 16:08:02 -040084 if (!iconEquals) {
85 Drawable drawable = getIcon(icon);
86 if (drawable == null) {
Joe Onorato1c95ecb2010-06-28 17:19:12 -040087 Slog.w(StatusBarService.TAG, "No icon for slot " + mSlot);
Joe Onorato005847b2010-06-04 16:08:02 -040088 return false;
Joe Onorato871bdb92010-05-24 18:36:53 -040089 }
Joe Onorato005847b2010-06-04 16:08:02 -040090 setImageDrawable(drawable);
Joe Onorato0cbda992010-05-02 16:28:15 -070091 }
Joe Onorato005847b2010-06-04 16:08:02 -040092 if (!levelEquals) {
93 setImageLevel(icon.iconLevel);
Joe Onorato0cbda992010-05-02 16:28:15 -070094 }
Joe Onorato6c01a112010-10-04 17:38:47 -040095 if (!numberEquals) {
96 if (icon.number > 0) {
97 if (mNumberBackground == null) {
98 mNumberBackground = getContext().getResources().getDrawable(
99 R.drawable.ic_notification_overlay);
100 }
101 placeNumber();
102 } else {
103 mNumberBackground = null;
104 mNumberText = null;
105 }
106 invalidate();
107 }
Joe Onorato005847b2010-06-04 16:08:02 -0400108 if (!visibilityEquals) {
109 setVisibility(icon.visible ? VISIBLE : GONE);
110 }
Joe Onorato005847b2010-06-04 16:08:02 -0400111 return true;
Joe Onorato0cbda992010-05-02 16:28:15 -0700112 }
113
Joe Onoratof5510542010-06-01 07:46:41 -0700114 private Drawable getIcon(StatusBarIcon icon) {
115 return getIcon(getContext(), icon);
116 }
117
Joe Onorato0cbda992010-05-02 16:28:15 -0700118 /**
119 * Returns the right icon to use for this item, respecting the iconId and
120 * iconPackage (if set)
121 *
122 * @param context Context to use to get resources if iconPackage is not set
123 * @return Drawable for this item, or null if the package or item could not
124 * be found
125 */
Joe Onoratof5510542010-06-01 07:46:41 -0700126 public static Drawable getIcon(Context context, StatusBarIcon icon) {
Joe Onorato0cbda992010-05-02 16:28:15 -0700127 Resources r = null;
128
129 if (icon.iconPackage != null) {
130 try {
131 r = context.getPackageManager().getResourcesForApplication(icon.iconPackage);
132 } catch (PackageManager.NameNotFoundException ex) {
Joe Onorato1c95ecb2010-06-28 17:19:12 -0400133 Slog.e(StatusBarService.TAG, "Icon package not found: " + icon.iconPackage);
Joe Onorato0cbda992010-05-02 16:28:15 -0700134 return null;
135 }
136 } else {
137 r = context.getResources();
138 }
139
140 if (icon.iconId == 0) {
Joe Onorato0cbda992010-05-02 16:28:15 -0700141 return null;
142 }
143
144 try {
145 return r.getDrawable(icon.iconId);
146 } catch (RuntimeException e) {
Joe Onorato1c95ecb2010-06-28 17:19:12 -0400147 Slog.w(StatusBarService.TAG, "Icon not found in "
Joe Onorato0cbda992010-05-02 16:28:15 -0700148 + (icon.iconPackage != null ? icon.iconId : "<system>")
149 + ": " + Integer.toHexString(icon.iconId));
150 }
151
152 return null;
153 }
Joe Onoratob77f53b2010-05-28 19:59:51 -0400154
155 public StatusBarIcon getStatusBarIcon() {
156 return mIcon;
157 }
Joe Onoratof9ec03c2010-09-23 15:09:18 -0700158
Joe Onorato6c01a112010-10-04 17:38:47 -0400159 protected void onSizeChanged(int w, int h, int oldw, int oldh) {
160 super.onSizeChanged(w, h, oldw, oldh);
161 if (mNumberBackground != null) {
162 placeNumber();
163 }
164 }
165
166 protected void onDraw(Canvas canvas) {
167 super.onDraw(canvas);
168
169 if (mNumberBackground != null) {
170 mNumberBackground.draw(canvas);
171 canvas.drawText(mNumberText, mNumberX, mNumberY, mNumberPain);
172 }
173 }
174
Joe Onoratof9ec03c2010-09-23 15:09:18 -0700175 protected void debug(int depth) {
176 super.debug(depth);
177 Log.d("View", debugIndent(depth) + "slot=" + mSlot);
178 Log.d("View", debugIndent(depth) + "icon=" + mIcon);
179 }
Joe Onorato6c01a112010-10-04 17:38:47 -0400180
181 void placeNumber() {
182 final String str = mNumberText = Integer.toString(mIcon.number);
183 final int w = getWidth();
184 final int h = getHeight();
185 final Rect r = new Rect();
186 mNumberPain.getTextBounds(str, 0, str.length(), r);
187 final int tw = r.right - r.left;
188 final int th = r.bottom - r.top;
189 mNumberBackground.getPadding(r);
190 int dw = r.left + tw + r.right;
191 if (dw < mNumberBackground.getMinimumWidth()) {
192 dw = mNumberBackground.getMinimumWidth();
193 }
194 mNumberX = w-r.right-((dw-r.right-r.left)/2);
195 int dh = r.top + th + r.bottom;
196 if (dh < mNumberBackground.getMinimumWidth()) {
197 dh = mNumberBackground.getMinimumWidth();
198 }
199 mNumberY = h-r.bottom-((dh-r.top-th-r.bottom)/2);
200 mNumberBackground.setBounds(w-dw, h-dh, w, h);
201 }
Joe Onorato0cbda992010-05-02 16:28:15 -0700202}