Joe Onorato | 0cbda99 | 2010-05-02 16:28:15 -0700 | [diff] [blame] | 1 | /* |
| 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 Onorato | 79de0c5 | 2010-05-26 17:03:26 -0400 | [diff] [blame] | 17 | package com.android.systemui.statusbar; |
Joe Onorato | 0cbda99 | 2010-05-02 16:28:15 -0700 | [diff] [blame] | 18 | |
| 19 | import android.content.Context; |
| 20 | import android.content.pm.PackageManager; |
| 21 | import android.content.res.Resources; |
| 22 | import android.graphics.drawable.Drawable; |
| 23 | import android.graphics.Canvas; |
Joe Onorato | 6c01a11 | 2010-10-04 17:38:47 -0400 | [diff] [blame] | 24 | import android.graphics.Paint; |
| 25 | import android.graphics.Rect; |
Joe Onorato | 0cbda99 | 2010-05-02 16:28:15 -0700 | [diff] [blame] | 26 | import android.util.Slog; |
Joe Onorato | f9ec03c | 2010-09-23 15:09:18 -0700 | [diff] [blame] | 27 | import android.util.Log; |
Joe Onorato | 6c01a11 | 2010-10-04 17:38:47 -0400 | [diff] [blame] | 28 | import android.view.View; |
Joe Onorato | 0cbda99 | 2010-05-02 16:28:15 -0700 | [diff] [blame] | 29 | import android.view.ViewDebug; |
| 30 | import android.widget.FrameLayout; |
| 31 | |
Daniel Sandler | ebce011 | 2011-06-16 16:44:51 -0400 | [diff] [blame^] | 32 | import java.text.NumberFormat; |
| 33 | |
Joe Onorato | 0cbda99 | 2010-05-02 16:28:15 -0700 | [diff] [blame] | 34 | import com.android.internal.statusbar.StatusBarIcon; |
| 35 | |
Joe Onorato | 6c01a11 | 2010-10-04 17:38:47 -0400 | [diff] [blame] | 36 | import com.android.systemui.R; |
| 37 | |
Joe Onorato | 0cbda99 | 2010-05-02 16:28:15 -0700 | [diff] [blame] | 38 | public class StatusBarIconView extends AnimatedImageView { |
| 39 | private static final String TAG = "StatusBarIconView"; |
| 40 | |
| 41 | private StatusBarIcon mIcon; |
| 42 | @ViewDebug.ExportedProperty private String mSlot; |
Joe Onorato | 6c01a11 | 2010-10-04 17:38:47 -0400 | [diff] [blame] | 43 | private Drawable mNumberBackground; |
| 44 | private Paint mNumberPain; |
| 45 | private int mNumberX; |
| 46 | private int mNumberY; |
| 47 | private String mNumberText; |
Joe Onorato | 0cbda99 | 2010-05-02 16:28:15 -0700 | [diff] [blame] | 48 | |
| 49 | public StatusBarIconView(Context context, String slot) { |
| 50 | super(context); |
Joe Onorato | 6c01a11 | 2010-10-04 17:38:47 -0400 | [diff] [blame] | 51 | final Resources res = context.getResources(); |
Joe Onorato | 0cbda99 | 2010-05-02 16:28:15 -0700 | [diff] [blame] | 52 | mSlot = slot; |
Joe Onorato | 6c01a11 | 2010-10-04 17:38:47 -0400 | [diff] [blame] | 53 | 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 Onorato | 0cbda99 | 2010-05-02 16:28:15 -0700 | [diff] [blame] | 57 | } |
| 58 | |
| 59 | private static boolean streq(String a, String b) { |
Joe Onorato | 66d7d01 | 2010-05-14 10:05:10 -0700 | [diff] [blame] | 60 | if (a == b) { |
| 61 | return true; |
| 62 | } |
Joe Onorato | 0cbda99 | 2010-05-02 16:28:15 -0700 | [diff] [blame] | 63 | 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 Onorato | 005847b | 2010-06-04 16:08:02 -0400 | [diff] [blame] | 72 | /** |
| 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 Onorato | 6c01a11 | 2010-10-04 17:38:47 -0400 | [diff] [blame] | 83 | final boolean numberEquals = mIcon != null |
| 84 | && mIcon.number == icon.number; |
| 85 | mIcon = icon.clone(); |
Joe Onorato | 005847b | 2010-06-04 16:08:02 -0400 | [diff] [blame] | 86 | if (!iconEquals) { |
| 87 | Drawable drawable = getIcon(icon); |
| 88 | if (drawable == null) { |
Joe Onorato | fd52b18 | 2010-11-10 18:00:52 -0800 | [diff] [blame] | 89 | Slog.w(StatusBar.TAG, "No icon for slot " + mSlot); |
Joe Onorato | 005847b | 2010-06-04 16:08:02 -0400 | [diff] [blame] | 90 | return false; |
Joe Onorato | 871bdb9 | 2010-05-24 18:36:53 -0400 | [diff] [blame] | 91 | } |
Joe Onorato | 005847b | 2010-06-04 16:08:02 -0400 | [diff] [blame] | 92 | setImageDrawable(drawable); |
Joe Onorato | 0cbda99 | 2010-05-02 16:28:15 -0700 | [diff] [blame] | 93 | } |
Joe Onorato | 005847b | 2010-06-04 16:08:02 -0400 | [diff] [blame] | 94 | if (!levelEquals) { |
| 95 | setImageLevel(icon.iconLevel); |
Joe Onorato | 0cbda99 | 2010-05-02 16:28:15 -0700 | [diff] [blame] | 96 | } |
Joe Onorato | 6c01a11 | 2010-10-04 17:38:47 -0400 | [diff] [blame] | 97 | if (!numberEquals) { |
Joe Onorato | 8595a3d | 2010-11-19 18:12:07 -0800 | [diff] [blame] | 98 | if (icon.number > 0 && mContext.getResources().getBoolean( |
| 99 | R.bool.config_statusBarShowNumber)) { |
Joe Onorato | 6c01a11 | 2010-10-04 17:38:47 -0400 | [diff] [blame] | 100 | 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 Onorato | 005847b | 2010-06-04 16:08:02 -0400 | [diff] [blame] | 111 | if (!visibilityEquals) { |
| 112 | setVisibility(icon.visible ? VISIBLE : GONE); |
| 113 | } |
Joe Onorato | 005847b | 2010-06-04 16:08:02 -0400 | [diff] [blame] | 114 | return true; |
Joe Onorato | 0cbda99 | 2010-05-02 16:28:15 -0700 | [diff] [blame] | 115 | } |
| 116 | |
Joe Onorato | f551054 | 2010-06-01 07:46:41 -0700 | [diff] [blame] | 117 | private Drawable getIcon(StatusBarIcon icon) { |
| 118 | return getIcon(getContext(), icon); |
| 119 | } |
| 120 | |
Joe Onorato | 0cbda99 | 2010-05-02 16:28:15 -0700 | [diff] [blame] | 121 | /** |
| 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 Onorato | f551054 | 2010-06-01 07:46:41 -0700 | [diff] [blame] | 129 | public static Drawable getIcon(Context context, StatusBarIcon icon) { |
Joe Onorato | 0cbda99 | 2010-05-02 16:28:15 -0700 | [diff] [blame] | 130 | Resources r = null; |
| 131 | |
| 132 | if (icon.iconPackage != null) { |
| 133 | try { |
| 134 | r = context.getPackageManager().getResourcesForApplication(icon.iconPackage); |
| 135 | } catch (PackageManager.NameNotFoundException ex) { |
Joe Onorato | fd52b18 | 2010-11-10 18:00:52 -0800 | [diff] [blame] | 136 | Slog.e(StatusBar.TAG, "Icon package not found: " + icon.iconPackage); |
Joe Onorato | 0cbda99 | 2010-05-02 16:28:15 -0700 | [diff] [blame] | 137 | return null; |
| 138 | } |
| 139 | } else { |
| 140 | r = context.getResources(); |
| 141 | } |
| 142 | |
| 143 | if (icon.iconId == 0) { |
Joe Onorato | 0cbda99 | 2010-05-02 16:28:15 -0700 | [diff] [blame] | 144 | return null; |
| 145 | } |
| 146 | |
| 147 | try { |
| 148 | return r.getDrawable(icon.iconId); |
| 149 | } catch (RuntimeException e) { |
Joe Onorato | fd52b18 | 2010-11-10 18:00:52 -0800 | [diff] [blame] | 150 | Slog.w(StatusBar.TAG, "Icon not found in " |
Joe Onorato | 0cbda99 | 2010-05-02 16:28:15 -0700 | [diff] [blame] | 151 | + (icon.iconPackage != null ? icon.iconId : "<system>") |
| 152 | + ": " + Integer.toHexString(icon.iconId)); |
| 153 | } |
| 154 | |
| 155 | return null; |
| 156 | } |
Joe Onorato | b77f53b | 2010-05-28 19:59:51 -0400 | [diff] [blame] | 157 | |
| 158 | public StatusBarIcon getStatusBarIcon() { |
| 159 | return mIcon; |
| 160 | } |
Joe Onorato | f9ec03c | 2010-09-23 15:09:18 -0700 | [diff] [blame] | 161 | |
Joe Onorato | 6c01a11 | 2010-10-04 17:38:47 -0400 | [diff] [blame] | 162 | 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 Onorato | f9ec03c | 2010-09-23 15:09:18 -0700 | [diff] [blame] | 178 | 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 Onorato | 6c01a11 | 2010-10-04 17:38:47 -0400 | [diff] [blame] | 183 | |
| 184 | void placeNumber() { |
Daniel Sandler | ebce011 | 2011-06-16 16:44:51 -0400 | [diff] [blame^] | 185 | 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 Onorato | 6c01a11 | 2010-10-04 17:38:47 -0400 | [diff] [blame] | 197 | 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 Onorato | 0cbda99 | 2010-05-02 16:28:15 -0700 | [diff] [blame] | 216 | } |