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