IconCache.java revision 4e012846cd09caa85b018c36810e9a64adb87640
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
17package com.android.launcher2;
18
19import android.content.ComponentName;
20import android.content.Intent;
21import android.content.pm.PackageManager;
22import android.content.pm.ResolveInfo;
23import android.content.res.Resources;
24import android.graphics.Bitmap;
25import android.graphics.Canvas;
26import android.graphics.drawable.Drawable;
27import android.util.Pair;
28import android.util.DisplayMetrics;
29
30import java.util.HashMap;
31
32/**
33 * Cache of application icons.  Icons can be made from any thread.
34 */
35public class IconCache {
36    private static final String TAG = "Launcher.IconCache";
37
38    private static final int INITIAL_ICON_CACHE_CAPACITY = 50;
39
40    private static class CacheEntry {
41        public Bitmap icon;
42        public String title;
43    }
44
45    private final Bitmap mDefaultIcon;
46    private final LauncherApplication mContext;
47    private final PackageManager mPackageManager;
48    private final Utilities.BubbleText mBubble;
49    private final HashMap<ComponentName, CacheEntry> mCache =
50            new HashMap<ComponentName, CacheEntry>(INITIAL_ICON_CACHE_CAPACITY);
51    private int mIconDpi;
52
53    public IconCache(LauncherApplication context) {
54        mContext = context;
55        mPackageManager = context.getPackageManager();
56        mBubble = new Utilities.BubbleText(context);
57        if (LauncherApplication.isScreenLarge()) {
58            mIconDpi = DisplayMetrics.DENSITY_HIGH;
59        } else {
60            mIconDpi = context.getResources().getDisplayMetrics().densityDpi;
61        }
62        // need to set mIconDpi before getting default icon
63        mDefaultIcon = makeDefaultIcon();
64    }
65
66    public Drawable getFullResDefaultActivityIcon() {
67        return getFullResIcon(Resources.getSystem(),
68                com.android.internal.R.mipmap.sym_def_app_icon);
69    }
70
71    public Drawable getFullResIcon(Resources resources, int iconId) {
72        try {
73            return resources.getDrawableForDensity(iconId, mIconDpi);
74        } catch (Resources.NotFoundException e) {
75            return getFullResDefaultActivityIcon();
76        }
77    }
78
79    public Drawable getFullResIcon(ResolveInfo info, PackageManager packageManager) {
80        Resources resources;
81        try {
82            resources = packageManager.getResourcesForApplication(
83                    info.activityInfo.applicationInfo);
84        } catch (PackageManager.NameNotFoundException e) {
85            resources = null;
86        }
87        if (resources != null) {
88            int iconId = info.activityInfo.getIconResource();
89            if (iconId != 0) {
90                return getFullResIcon(resources, iconId);
91            }
92        }
93        return getFullResDefaultActivityIcon();
94    }
95
96    private Bitmap makeDefaultIcon() {
97        Drawable d = getFullResDefaultActivityIcon();
98        Bitmap b = Bitmap.createBitmap(Math.max(d.getIntrinsicWidth(), 1),
99                Math.max(d.getIntrinsicHeight(), 1),
100                Bitmap.Config.ARGB_8888);
101        Canvas c = new Canvas(b);
102        d.setBounds(0, 0, b.getWidth(), b.getHeight());
103        d.draw(c);
104        return b;
105    }
106
107    /**
108     * Remove any records for the supplied ComponentName.
109     */
110    public void remove(ComponentName componentName) {
111        synchronized (mCache) {
112            mCache.remove(componentName);
113        }
114    }
115
116    /**
117     * Empty out the cache.
118     */
119    public void flush() {
120        synchronized (mCache) {
121            mCache.clear();
122        }
123    }
124
125    /**
126     * Fill in "application" with the icon and label for "info."
127     */
128    public void getTitleAndIcon(ApplicationInfo application, ResolveInfo info,
129            HashMap<Object, CharSequence> labelCache) {
130        synchronized (mCache) {
131            CacheEntry entry = cacheLocked(application.componentName, info, labelCache);
132
133            application.title = entry.title;
134            application.iconBitmap = entry.icon;
135        }
136    }
137
138    public Bitmap getIcon(Intent intent) {
139        synchronized (mCache) {
140            final ResolveInfo resolveInfo = mPackageManager.resolveActivity(intent, 0);
141            ComponentName component = intent.getComponent();
142
143            if (resolveInfo == null || component == null) {
144                return mDefaultIcon;
145            }
146
147            CacheEntry entry = cacheLocked(component, resolveInfo, null);
148            return entry.icon;
149        }
150    }
151
152    public Bitmap getIcon(ComponentName component, ResolveInfo resolveInfo) {
153        synchronized (mCache) {
154            if (resolveInfo == null || component == null) {
155                return null;
156            }
157
158            CacheEntry entry = cacheLocked(component, resolveInfo, null);
159            return entry.icon;
160        }
161    }
162
163    public boolean isDefaultIcon(Bitmap icon) {
164        return mDefaultIcon == icon;
165    }
166
167    private CacheEntry cacheLocked(ComponentName componentName, ResolveInfo info,
168            HashMap<Object, CharSequence> labelCache) {
169        CacheEntry entry = mCache.get(componentName);
170        if (entry == null) {
171            entry = new CacheEntry();
172
173            mCache.put(componentName, entry);
174
175            if (labelCache != null && labelCache.containsKey(info)) {
176                entry.title = labelCache.get(info).toString();
177            } else {
178                entry.title = info.loadLabel(mPackageManager).toString();
179                if (labelCache != null) {
180                    labelCache.put(info, entry.title);
181                }
182            }
183            if (entry.title == null) {
184                entry.title = info.activityInfo.name;
185            }
186
187            entry.icon = Utilities.createIconBitmap(
188                    getFullResIcon(info, mPackageManager), mContext);
189        }
190        return entry;
191    }
192
193    public HashMap<ComponentName,Bitmap> getAllIcons() {
194        synchronized (mCache) {
195            HashMap<ComponentName,Bitmap> set = new HashMap<ComponentName,Bitmap>();
196            int i = 0;
197            for (ComponentName cn : mCache.keySet()) {
198                final CacheEntry e = mCache.get(cn);
199                set.put(cn, e.icon);
200            }
201            return set;
202        }
203    }
204}
205