blob: 959ac7cc3a212f40e512b92db6381975db436c71 [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;
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 Onorato0cbda992010-05-02 16:28:15 -070083 }
Joe Onorato871bdb92010-05-24 18:36:53 -040084 if (mError) {
85 setVisibility(GONE);
Joe Onorato0cbda992010-05-02 16:28:15 -070086 }
Joe Onorato0cbda992010-05-02 16:28:15 -070087 }
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}