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 17package com.android.systemui.statusbar; 18 19import android.app.Notification; 20import android.content.Context; 21import android.content.pm.PackageManager; 22import android.content.res.Resources; 23import android.graphics.drawable.Drawable; 24import android.graphics.Canvas; 25import android.graphics.Paint; 26import android.graphics.Rect; 27import android.os.UserHandle; 28import android.text.TextUtils; 29import android.util.AttributeSet; 30import android.util.Slog; 31import android.util.Log; 32import android.view.ViewDebug; 33import android.view.accessibility.AccessibilityEvent; 34import android.widget.ImageView; 35 36import java.text.NumberFormat; 37 38import com.android.internal.statusbar.StatusBarIcon; 39 40import com.android.systemui.R; 41 42public class StatusBarIconView extends AnimatedImageView { 43 private static final String TAG = "StatusBarIconView"; 44 45 private StatusBarIcon mIcon; 46 @ViewDebug.ExportedProperty private String mSlot; 47 private Drawable mNumberBackground; 48 private Paint mNumberPain; 49 private int mNumberX; 50 private int mNumberY; 51 private String mNumberText; 52 private Notification mNotification; 53 54 public StatusBarIconView(Context context, String slot, Notification notification) { 55 super(context); 56 final Resources res = context.getResources(); 57 mSlot = slot; 58 mNumberPain = new Paint(); 59 mNumberPain.setTextAlign(Paint.Align.CENTER); 60 mNumberPain.setColor(res.getColor(R.drawable.notification_number_text_color)); 61 mNumberPain.setAntiAlias(true); 62 mNotification = notification; 63 setContentDescription(notification); 64 65 // We do not resize and scale system icons (on the right), only notification icons (on the 66 // left). 67 if (notification != null) { 68 final int outerBounds = res.getDimensionPixelSize(R.dimen.status_bar_icon_size); 69 final int imageBounds = res.getDimensionPixelSize(R.dimen.status_bar_icon_drawing_size); 70 final float scale = (float)imageBounds / (float)outerBounds; 71 setScaleX(scale); 72 setScaleY(scale); 73 final float alpha = res.getFraction(R.dimen.status_bar_icon_drawing_alpha, 1, 1); 74 setAlpha(alpha); 75 } 76 77 setScaleType(ImageView.ScaleType.CENTER); 78 } 79 80 public StatusBarIconView(Context context, AttributeSet attrs) { 81 super(context, attrs); 82 final Resources res = context.getResources(); 83 final int outerBounds = res.getDimensionPixelSize(R.dimen.status_bar_icon_size); 84 final int imageBounds = res.getDimensionPixelSize(R.dimen.status_bar_icon_drawing_size); 85 final float scale = (float)imageBounds / (float)outerBounds; 86 setScaleX(scale); 87 setScaleY(scale); 88 final float alpha = res.getFraction(R.dimen.status_bar_icon_drawing_alpha, 1, 1); 89 setAlpha(alpha); 90 } 91 92 private static boolean streq(String a, String b) { 93 if (a == b) { 94 return true; 95 } 96 if (a == null && b != null) { 97 return false; 98 } 99 if (a != null && b == null) { 100 return false; 101 } 102 return a.equals(b); 103 } 104 105 /** 106 * Returns whether the set succeeded. 107 */ 108 public boolean set(StatusBarIcon icon) { 109 final boolean iconEquals = mIcon != null 110 && streq(mIcon.iconPackage, icon.iconPackage) 111 && mIcon.iconId == icon.iconId; 112 final boolean levelEquals = iconEquals 113 && mIcon.iconLevel == icon.iconLevel; 114 final boolean visibilityEquals = mIcon != null 115 && mIcon.visible == icon.visible; 116 final boolean numberEquals = mIcon != null 117 && mIcon.number == icon.number; 118 mIcon = icon.clone(); 119 setContentDescription(icon.contentDescription); 120 if (!iconEquals) { 121 Drawable drawable = getIcon(icon); 122 if (drawable == null) { 123 Slog.w(TAG, "No icon for slot " + mSlot); 124 return false; 125 } 126 setImageDrawable(drawable); 127 } 128 if (!levelEquals) { 129 setImageLevel(icon.iconLevel); 130 } 131 132 if (!numberEquals) { 133 if (icon.number > 0 && mContext.getResources().getBoolean( 134 R.bool.config_statusBarShowNumber)) { 135 if (mNumberBackground == null) { 136 mNumberBackground = getContext().getResources().getDrawable( 137 R.drawable.ic_notification_overlay); 138 } 139 placeNumber(); 140 } else { 141 mNumberBackground = null; 142 mNumberText = null; 143 } 144 invalidate(); 145 } 146 if (!visibilityEquals) { 147 setVisibility(icon.visible ? VISIBLE : GONE); 148 } 149 return true; 150 } 151 152 private Drawable getIcon(StatusBarIcon icon) { 153 return getIcon(getContext(), icon); 154 } 155 156 /** 157 * Returns the right icon to use for this item, respecting the iconId and 158 * iconPackage (if set) 159 * 160 * @param context Context to use to get resources if iconPackage is not set 161 * @return Drawable for this item, or null if the package or item could not 162 * be found 163 */ 164 public static Drawable getIcon(Context context, StatusBarIcon icon) { 165 Resources r = null; 166 167 if (icon.iconPackage != null) { 168 try { 169 int userId = icon.user.getIdentifier(); 170 if (userId == UserHandle.USER_ALL) { 171 userId = UserHandle.USER_OWNER; 172 } 173 r = context.getPackageManager() 174 .getResourcesForApplicationAsUser(icon.iconPackage, userId); 175 } catch (PackageManager.NameNotFoundException ex) { 176 Slog.e(TAG, "Icon package not found: " + icon.iconPackage); 177 return null; 178 } 179 } else { 180 r = context.getResources(); 181 } 182 183 if (icon.iconId == 0) { 184 return null; 185 } 186 187 try { 188 return r.getDrawable(icon.iconId); 189 } catch (RuntimeException e) { 190 Slog.w(TAG, "Icon not found in " 191 + (icon.iconPackage != null ? icon.iconId : "<system>") 192 + ": " + Integer.toHexString(icon.iconId)); 193 } 194 195 return null; 196 } 197 198 public StatusBarIcon getStatusBarIcon() { 199 return mIcon; 200 } 201 202 @Override 203 public void onInitializeAccessibilityEvent(AccessibilityEvent event) { 204 super.onInitializeAccessibilityEvent(event); 205 if (mNotification != null) { 206 event.setParcelableData(mNotification); 207 } 208 } 209 210 @Override 211 protected void onSizeChanged(int w, int h, int oldw, int oldh) { 212 super.onSizeChanged(w, h, oldw, oldh); 213 if (mNumberBackground != null) { 214 placeNumber(); 215 } 216 } 217 218 @Override 219 protected void onDraw(Canvas canvas) { 220 super.onDraw(canvas); 221 222 if (mNumberBackground != null) { 223 mNumberBackground.draw(canvas); 224 canvas.drawText(mNumberText, mNumberX, mNumberY, mNumberPain); 225 } 226 } 227 228 @Override 229 protected void debug(int depth) { 230 super.debug(depth); 231 Log.d("View", debugIndent(depth) + "slot=" + mSlot); 232 Log.d("View", debugIndent(depth) + "icon=" + mIcon); 233 } 234 235 void placeNumber() { 236 final String str; 237 final int tooBig = mContext.getResources().getInteger( 238 android.R.integer.status_bar_notification_info_maxnum); 239 if (mIcon.number > tooBig) { 240 str = mContext.getResources().getString( 241 android.R.string.status_bar_notification_info_overflow); 242 } else { 243 NumberFormat f = NumberFormat.getIntegerInstance(); 244 str = f.format(mIcon.number); 245 } 246 mNumberText = str; 247 248 final int w = getWidth(); 249 final int h = getHeight(); 250 final Rect r = new Rect(); 251 mNumberPain.getTextBounds(str, 0, str.length(), r); 252 final int tw = r.right - r.left; 253 final int th = r.bottom - r.top; 254 mNumberBackground.getPadding(r); 255 int dw = r.left + tw + r.right; 256 if (dw < mNumberBackground.getMinimumWidth()) { 257 dw = mNumberBackground.getMinimumWidth(); 258 } 259 mNumberX = w-r.right-((dw-r.right-r.left)/2); 260 int dh = r.top + th + r.bottom; 261 if (dh < mNumberBackground.getMinimumWidth()) { 262 dh = mNumberBackground.getMinimumWidth(); 263 } 264 mNumberY = h-r.bottom-((dh-r.top-th-r.bottom)/2); 265 mNumberBackground.setBounds(w-dw, h-dh, w, h); 266 } 267 268 private void setContentDescription(Notification notification) { 269 if (notification != null) { 270 CharSequence tickerText = notification.tickerText; 271 if (!TextUtils.isEmpty(tickerText)) { 272 setContentDescription(tickerText); 273 } 274 } 275 } 276 277 public String toString() { 278 return "StatusBarIconView(slot=" + mSlot + " icon=" + mIcon 279 + " notification=" + mNotification + ")"; 280 } 281} 282