blob: bc1e798e9d508b2fd4cea1b281cd5f80cddda63b [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;
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 Onorato66d7d012010-05-14 10:05:10 -070042 if (a == b) {
43 return true;
44 }
Joe Onorato0cbda992010-05-02 16:28:15 -070045 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 Onorato005847b2010-06-04 16:08:02 -040054 /**
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 Onorato871bdb92010-05-24 18:36:53 -040070 }
Joe Onorato005847b2010-06-04 16:08:02 -040071 setImageDrawable(drawable);
Joe Onorato0cbda992010-05-02 16:28:15 -070072 }
Joe Onorato005847b2010-06-04 16:08:02 -040073 if (!levelEquals) {
74 setImageLevel(icon.iconLevel);
Joe Onorato0cbda992010-05-02 16:28:15 -070075 }
Joe Onorato005847b2010-06-04 16:08:02 -040076 if (!visibilityEquals) {
77 setVisibility(icon.visible ? VISIBLE : GONE);
78 }
79 mIcon = icon.clone();
80 return true;
Joe Onorato0cbda992010-05-02 16:28:15 -070081 }
82
Joe Onoratof5510542010-06-01 07:46:41 -070083 private Drawable getIcon(StatusBarIcon icon) {
84 return getIcon(getContext(), icon);
85 }
86
Joe Onorato0cbda992010-05-02 16:28:15 -070087 /**
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 Onoratof5510542010-06-01 07:46:41 -070095 public static Drawable getIcon(Context context, StatusBarIcon icon) {
Joe Onorato0cbda992010-05-02 16:28:15 -070096 Resources r = null;
97
98 if (icon.iconPackage != null) {
99 try {
100 r = context.getPackageManager().getResourcesForApplication(icon.iconPackage);
101 } catch (PackageManager.NameNotFoundException ex) {
Joe Onorato005847b2010-06-04 16:08:02 -0400102 Slog.e(PhoneStatusBarService.TAG, "Icon package not found: " + icon.iconPackage);
Joe Onorato0cbda992010-05-02 16:28:15 -0700103 return null;
104 }
105 } else {
106 r = context.getResources();
107 }
108
109 if (icon.iconId == 0) {
Joe Onorato0cbda992010-05-02 16:28:15 -0700110 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 Onoratob77f53b2010-05-28 19:59:51 -0400123
124 public StatusBarIcon getStatusBarIcon() {
125 return mIcon;
126 }
Joe Onorato0cbda992010-05-02 16:28:15 -0700127}