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 | |
| 32 | import com.android.internal.statusbar.StatusBarIcon; |
| 33 | |
Joe Onorato | 6c01a11 | 2010-10-04 17:38:47 -0400 | [diff] [blame^] | 34 | import com.android.systemui.R; |
| 35 | |
Joe Onorato | 0cbda99 | 2010-05-02 16:28:15 -0700 | [diff] [blame] | 36 | public class StatusBarIconView extends AnimatedImageView { |
| 37 | private static final String TAG = "StatusBarIconView"; |
| 38 | |
| 39 | private StatusBarIcon mIcon; |
| 40 | @ViewDebug.ExportedProperty private String mSlot; |
Joe Onorato | 6c01a11 | 2010-10-04 17:38:47 -0400 | [diff] [blame^] | 41 | private Drawable mNumberBackground; |
| 42 | private Paint mNumberPain; |
| 43 | private int mNumberX; |
| 44 | private int mNumberY; |
| 45 | private String mNumberText; |
Joe Onorato | 0cbda99 | 2010-05-02 16:28:15 -0700 | [diff] [blame] | 46 | |
| 47 | public StatusBarIconView(Context context, String slot) { |
| 48 | super(context); |
Joe Onorato | 6c01a11 | 2010-10-04 17:38:47 -0400 | [diff] [blame^] | 49 | final Resources res = context.getResources(); |
Joe Onorato | 0cbda99 | 2010-05-02 16:28:15 -0700 | [diff] [blame] | 50 | mSlot = slot; |
Joe Onorato | 6c01a11 | 2010-10-04 17:38:47 -0400 | [diff] [blame^] | 51 | 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 Onorato | 0cbda99 | 2010-05-02 16:28:15 -0700 | [diff] [blame] | 55 | } |
| 56 | |
| 57 | private static boolean streq(String a, String b) { |
Joe Onorato | 66d7d01 | 2010-05-14 10:05:10 -0700 | [diff] [blame] | 58 | if (a == b) { |
| 59 | return true; |
| 60 | } |
Joe Onorato | 0cbda99 | 2010-05-02 16:28:15 -0700 | [diff] [blame] | 61 | 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 Onorato | 005847b | 2010-06-04 16:08:02 -0400 | [diff] [blame] | 70 | /** |
| 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 Onorato | 6c01a11 | 2010-10-04 17:38:47 -0400 | [diff] [blame^] | 81 | final boolean numberEquals = mIcon != null |
| 82 | && mIcon.number == icon.number; |
| 83 | mIcon = icon.clone(); |
Joe Onorato | 005847b | 2010-06-04 16:08:02 -0400 | [diff] [blame] | 84 | if (!iconEquals) { |
| 85 | Drawable drawable = getIcon(icon); |
| 86 | if (drawable == null) { |
Joe Onorato | 1c95ecb | 2010-06-28 17:19:12 -0400 | [diff] [blame] | 87 | Slog.w(StatusBarService.TAG, "No icon for slot " + mSlot); |
Joe Onorato | 005847b | 2010-06-04 16:08:02 -0400 | [diff] [blame] | 88 | return false; |
Joe Onorato | 871bdb9 | 2010-05-24 18:36:53 -0400 | [diff] [blame] | 89 | } |
Joe Onorato | 005847b | 2010-06-04 16:08:02 -0400 | [diff] [blame] | 90 | setImageDrawable(drawable); |
Joe Onorato | 0cbda99 | 2010-05-02 16:28:15 -0700 | [diff] [blame] | 91 | } |
Joe Onorato | 005847b | 2010-06-04 16:08:02 -0400 | [diff] [blame] | 92 | if (!levelEquals) { |
| 93 | setImageLevel(icon.iconLevel); |
Joe Onorato | 0cbda99 | 2010-05-02 16:28:15 -0700 | [diff] [blame] | 94 | } |
Joe Onorato | 6c01a11 | 2010-10-04 17:38:47 -0400 | [diff] [blame^] | 95 | 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 Onorato | 005847b | 2010-06-04 16:08:02 -0400 | [diff] [blame] | 108 | if (!visibilityEquals) { |
| 109 | setVisibility(icon.visible ? VISIBLE : GONE); |
| 110 | } |
Joe Onorato | 005847b | 2010-06-04 16:08:02 -0400 | [diff] [blame] | 111 | return true; |
Joe Onorato | 0cbda99 | 2010-05-02 16:28:15 -0700 | [diff] [blame] | 112 | } |
| 113 | |
Joe Onorato | f551054 | 2010-06-01 07:46:41 -0700 | [diff] [blame] | 114 | private Drawable getIcon(StatusBarIcon icon) { |
| 115 | return getIcon(getContext(), icon); |
| 116 | } |
| 117 | |
Joe Onorato | 0cbda99 | 2010-05-02 16:28:15 -0700 | [diff] [blame] | 118 | /** |
| 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 Onorato | f551054 | 2010-06-01 07:46:41 -0700 | [diff] [blame] | 126 | public static Drawable getIcon(Context context, StatusBarIcon icon) { |
Joe Onorato | 0cbda99 | 2010-05-02 16:28:15 -0700 | [diff] [blame] | 127 | Resources r = null; |
| 128 | |
| 129 | if (icon.iconPackage != null) { |
| 130 | try { |
| 131 | r = context.getPackageManager().getResourcesForApplication(icon.iconPackage); |
| 132 | } catch (PackageManager.NameNotFoundException ex) { |
Joe Onorato | 1c95ecb | 2010-06-28 17:19:12 -0400 | [diff] [blame] | 133 | Slog.e(StatusBarService.TAG, "Icon package not found: " + icon.iconPackage); |
Joe Onorato | 0cbda99 | 2010-05-02 16:28:15 -0700 | [diff] [blame] | 134 | return null; |
| 135 | } |
| 136 | } else { |
| 137 | r = context.getResources(); |
| 138 | } |
| 139 | |
| 140 | if (icon.iconId == 0) { |
Joe Onorato | 0cbda99 | 2010-05-02 16:28:15 -0700 | [diff] [blame] | 141 | return null; |
| 142 | } |
| 143 | |
| 144 | try { |
| 145 | return r.getDrawable(icon.iconId); |
| 146 | } catch (RuntimeException e) { |
Joe Onorato | 1c95ecb | 2010-06-28 17:19:12 -0400 | [diff] [blame] | 147 | Slog.w(StatusBarService.TAG, "Icon not found in " |
Joe Onorato | 0cbda99 | 2010-05-02 16:28:15 -0700 | [diff] [blame] | 148 | + (icon.iconPackage != null ? icon.iconId : "<system>") |
| 149 | + ": " + Integer.toHexString(icon.iconId)); |
| 150 | } |
| 151 | |
| 152 | return null; |
| 153 | } |
Joe Onorato | b77f53b | 2010-05-28 19:59:51 -0400 | [diff] [blame] | 154 | |
| 155 | public StatusBarIcon getStatusBarIcon() { |
| 156 | return mIcon; |
| 157 | } |
Joe Onorato | f9ec03c | 2010-09-23 15:09:18 -0700 | [diff] [blame] | 158 | |
Joe Onorato | 6c01a11 | 2010-10-04 17:38:47 -0400 | [diff] [blame^] | 159 | 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 Onorato | f9ec03c | 2010-09-23 15:09:18 -0700 | [diff] [blame] | 175 | 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 Onorato | 6c01a11 | 2010-10-04 17:38:47 -0400 | [diff] [blame^] | 180 | |
| 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 Onorato | 0cbda99 | 2010-05-02 16:28:15 -0700 | [diff] [blame] | 202 | } |