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; |
| 24 | import android.util.Slog; |
| 25 | import android.view.ViewDebug; |
| 26 | import android.widget.FrameLayout; |
| 27 | |
| 28 | import com.android.internal.statusbar.StatusBarIcon; |
| 29 | |
| 30 | public class StatusBarIconView extends AnimatedImageView { |
| 31 | private static final String TAG = "StatusBarIconView"; |
| 32 | |
| 33 | private StatusBarIcon mIcon; |
| 34 | @ViewDebug.ExportedProperty private String mSlot; |
Joe Onorato | 871bdb9 | 2010-05-24 18:36:53 -0400 | [diff] [blame] | 35 | @ViewDebug.ExportedProperty private boolean mError; |
Joe Onorato | 0cbda99 | 2010-05-02 16:28:15 -0700 | [diff] [blame] | 36 | |
| 37 | public StatusBarIconView(Context context, String slot) { |
| 38 | super(context); |
| 39 | mSlot = slot; |
| 40 | } |
| 41 | |
| 42 | private static boolean streq(String a, String b) { |
Joe Onorato | 66d7d01 | 2010-05-14 10:05:10 -0700 | [diff] [blame] | 43 | if (a == b) { |
| 44 | return true; |
| 45 | } |
Joe Onorato | 0cbda99 | 2010-05-02 16:28:15 -0700 | [diff] [blame] | 46 | if (a == null && b != null) { |
| 47 | return false; |
| 48 | } |
| 49 | if (a != null && b == null) { |
| 50 | return false; |
| 51 | } |
| 52 | return a.equals(b); |
| 53 | } |
| 54 | |
| 55 | public void set(StatusBarIcon icon) { |
Joe Onorato | 871bdb9 | 2010-05-24 18:36:53 -0400 | [diff] [blame] | 56 | error: { |
| 57 | final boolean iconEquals = !mError |
| 58 | && mIcon != null |
| 59 | && streq(mIcon.iconPackage, icon.iconPackage) |
| 60 | && mIcon.iconId == icon.iconId; |
| 61 | final boolean levelEquals = !mError |
| 62 | && iconEquals |
| 63 | && mIcon.iconLevel == icon.iconLevel; |
| 64 | final boolean visibilityEquals = !mError |
| 65 | && mIcon != null |
| 66 | && mIcon.visible == icon.visible; |
| 67 | mError = false; |
| 68 | if (!iconEquals) { |
| 69 | Drawable drawable = getIcon(icon); |
| 70 | if (drawable == null) { |
| 71 | mError = true; |
| 72 | break error; |
| 73 | } |
| 74 | setImageDrawable(drawable); |
| 75 | } |
| 76 | if (!levelEquals) { |
| 77 | setImageLevel(icon.iconLevel); |
| 78 | } |
| 79 | if (!visibilityEquals) { |
| 80 | setVisibility(icon.visible ? VISIBLE : GONE); |
| 81 | } |
| 82 | mIcon = icon.clone(); |
Joe Onorato | 0cbda99 | 2010-05-02 16:28:15 -0700 | [diff] [blame] | 83 | } |
Joe Onorato | 871bdb9 | 2010-05-24 18:36:53 -0400 | [diff] [blame] | 84 | if (mError) { |
| 85 | setVisibility(GONE); |
Joe Onorato | 0cbda99 | 2010-05-02 16:28:15 -0700 | [diff] [blame] | 86 | } |
Joe Onorato | 0cbda99 | 2010-05-02 16:28:15 -0700 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | /** |
| 90 | * Returns the right icon to use for this item, respecting the iconId and |
| 91 | * iconPackage (if set) |
| 92 | * |
| 93 | * @param context Context to use to get resources if iconPackage is not set |
| 94 | * @return Drawable for this item, or null if the package or item could not |
| 95 | * be found |
| 96 | */ |
| 97 | private Drawable getIcon(StatusBarIcon icon) { |
| 98 | Context context = getContext(); |
| 99 | Resources r = null; |
| 100 | |
| 101 | if (icon.iconPackage != null) { |
| 102 | try { |
| 103 | r = context.getPackageManager().getResourcesForApplication(icon.iconPackage); |
| 104 | } catch (PackageManager.NameNotFoundException ex) { |
| 105 | Slog.e(PhoneStatusBarService.TAG, "Icon package not found: "+icon.iconPackage, ex); |
| 106 | return null; |
| 107 | } |
| 108 | } else { |
| 109 | r = context.getResources(); |
| 110 | } |
| 111 | |
| 112 | if (icon.iconId == 0) { |
| 113 | Slog.w(PhoneStatusBarService.TAG, "No icon ID for slot " + mSlot); |
| 114 | return null; |
| 115 | } |
| 116 | |
| 117 | try { |
| 118 | return r.getDrawable(icon.iconId); |
| 119 | } catch (RuntimeException e) { |
| 120 | Slog.w(PhoneStatusBarService.TAG, "Icon not found in " |
| 121 | + (icon.iconPackage != null ? icon.iconId : "<system>") |
| 122 | + ": " + Integer.toHexString(icon.iconId)); |
| 123 | } |
| 124 | |
| 125 | return null; |
| 126 | } |
| 127 | } |