blob: 5eb0d683271b1b63b824ec69a27cfccb9273bd76 [file] [log] [blame]
Joe Onorato0cbda992010-05-02 16:28:15 -07001/*
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 Onorato79de0c52010-05-26 17:03:26 -040017package com.android.systemui.statusbar;
Joe Onorato0cbda992010-05-02 16:28:15 -070018
19import android.content.Context;
20import android.content.pm.PackageManager;
21import android.content.res.Resources;
22import android.graphics.drawable.Drawable;
23import android.graphics.Canvas;
24import android.util.Slog;
25import android.view.ViewDebug;
26import android.widget.FrameLayout;
27
28import com.android.internal.statusbar.StatusBarIcon;
29
30public class StatusBarIconView extends AnimatedImageView {
31 private static final String TAG = "StatusBarIconView";
32
33 private StatusBarIcon mIcon;
34 @ViewDebug.ExportedProperty private String mSlot;
Joe Onorato871bdb92010-05-24 18:36:53 -040035 @ViewDebug.ExportedProperty private boolean mError;
Joe Onorato0cbda992010-05-02 16:28:15 -070036
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 Onorato66d7d012010-05-14 10:05:10 -070043 if (a == b) {
44 return true;
45 }
Joe Onorato0cbda992010-05-02 16:28:15 -070046 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 Onorato871bdb92010-05-24 18:36:53 -040056 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;
Joe Onoratof5510542010-06-01 07:46:41 -070072 Slog.w(PhoneStatusBarService.TAG, "No icon ID for slot " + mSlot);
Joe Onorato871bdb92010-05-24 18:36:53 -040073 break error;
74 }
75 setImageDrawable(drawable);
76 }
77 if (!levelEquals) {
78 setImageLevel(icon.iconLevel);
79 }
80 if (!visibilityEquals) {
81 setVisibility(icon.visible ? VISIBLE : GONE);
82 }
83 mIcon = icon.clone();
Joe Onorato0cbda992010-05-02 16:28:15 -070084 }
Joe Onorato871bdb92010-05-24 18:36:53 -040085 if (mError) {
86 setVisibility(GONE);
Joe Onorato0cbda992010-05-02 16:28:15 -070087 }
Joe Onorato0cbda992010-05-02 16:28:15 -070088 }
89
Joe Onoratof5510542010-06-01 07:46:41 -070090 private Drawable getIcon(StatusBarIcon icon) {
91 return getIcon(getContext(), icon);
92 }
93
Joe Onorato0cbda992010-05-02 16:28:15 -070094 /**
95 * Returns the right icon to use for this item, respecting the iconId and
96 * iconPackage (if set)
97 *
98 * @param context Context to use to get resources if iconPackage is not set
99 * @return Drawable for this item, or null if the package or item could not
100 * be found
101 */
Joe Onoratof5510542010-06-01 07:46:41 -0700102 public static Drawable getIcon(Context context, StatusBarIcon icon) {
Joe Onorato0cbda992010-05-02 16:28:15 -0700103 Resources r = null;
104
105 if (icon.iconPackage != null) {
106 try {
107 r = context.getPackageManager().getResourcesForApplication(icon.iconPackage);
108 } catch (PackageManager.NameNotFoundException ex) {
109 Slog.e(PhoneStatusBarService.TAG, "Icon package not found: "+icon.iconPackage, ex);
110 return null;
111 }
112 } else {
113 r = context.getResources();
114 }
115
116 if (icon.iconId == 0) {
Joe Onorato0cbda992010-05-02 16:28:15 -0700117 return null;
118 }
119
120 try {
121 return r.getDrawable(icon.iconId);
122 } catch (RuntimeException e) {
123 Slog.w(PhoneStatusBarService.TAG, "Icon not found in "
124 + (icon.iconPackage != null ? icon.iconId : "<system>")
125 + ": " + Integer.toHexString(icon.iconId));
126 }
127
128 return null;
129 }
Joe Onoratob77f53b2010-05-28 19:59:51 -0400130
131 public StatusBarIcon getStatusBarIcon() {
132 return mIcon;
133 }
Joe Onorato0cbda992010-05-02 16:28:15 -0700134}