blob: efa9fac6db2950b6ab7a52780729fc237bd96637 [file] [log] [blame]
Joe Onorato0589f0f2010-02-08 13:44:00 -08001/*
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
Winson Chungd83f5f42012-02-13 14:27:42 -080019import android.app.ActivityManager;
Joe Onorato0589f0f2010-02-08 13:44:00 -080020import android.content.ComponentName;
Winson Chungd83f5f42012-02-13 14:27:42 -080021import android.content.Context;
Joe Onorato0589f0f2010-02-08 13:44:00 -080022import android.content.Intent;
Joe Onorato0589f0f2010-02-08 13:44:00 -080023import android.content.pm.PackageManager;
24import android.content.pm.ResolveInfo;
Michael Jurkac9a96192010-11-01 11:52:08 -070025import android.content.res.Resources;
Joe Onorato0589f0f2010-02-08 13:44:00 -080026import android.graphics.Bitmap;
Romain Guya28fd3f2010-03-15 14:44:42 -070027import android.graphics.Canvas;
Joe Onorato0589f0f2010-02-08 13:44:00 -080028import android.graphics.drawable.Drawable;
Joe Onorato0589f0f2010-02-08 13:44:00 -080029
Joe Onorato0589f0f2010-02-08 13:44:00 -080030import java.util.HashMap;
Joe Onorato0589f0f2010-02-08 13:44:00 -080031
32/**
33 * Cache of application icons. Icons can be made from any thread.
34 */
35public class IconCache {
Michael Jurka3a9fced2012-04-13 14:44:29 -070036 @SuppressWarnings("unused")
Joe Onorato0589f0f2010-02-08 13:44:00 -080037 private static final String TAG = "Launcher.IconCache";
38
39 private static final int INITIAL_ICON_CACHE_CAPACITY = 50;
40
41 private static class CacheEntry {
42 public Bitmap icon;
43 public String title;
Joe Onorato0589f0f2010-02-08 13:44:00 -080044 }
45
Romain Guya28fd3f2010-03-15 14:44:42 -070046 private final Bitmap mDefaultIcon;
47 private final LauncherApplication mContext;
48 private final PackageManager mPackageManager;
Joe Onorato0589f0f2010-02-08 13:44:00 -080049 private final HashMap<ComponentName, CacheEntry> mCache =
50 new HashMap<ComponentName, CacheEntry>(INITIAL_ICON_CACHE_CAPACITY);
Michael Jurkac9a96192010-11-01 11:52:08 -070051 private int mIconDpi;
Joe Onorato0589f0f2010-02-08 13:44:00 -080052
53 public IconCache(LauncherApplication context) {
Winson Chungd83f5f42012-02-13 14:27:42 -080054 ActivityManager activityManager =
55 (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
56
Joe Onorato0589f0f2010-02-08 13:44:00 -080057 mContext = context;
58 mPackageManager = context.getPackageManager();
Winson Chungd83f5f42012-02-13 14:27:42 -080059 mIconDpi = activityManager.getLauncherLargeIconDensity();
60
Michael Jurkac9a96192010-11-01 11:52:08 -070061 // need to set mIconDpi before getting default icon
Romain Guya28fd3f2010-03-15 14:44:42 -070062 mDefaultIcon = makeDefaultIcon();
63 }
64
Michael Jurkac9a96192010-11-01 11:52:08 -070065 public Drawable getFullResDefaultActivityIcon() {
66 return getFullResIcon(Resources.getSystem(),
Kenny Rootf5675de2011-01-12 10:15:46 -080067 com.android.internal.R.mipmap.sym_def_app_icon);
Michael Jurkac9a96192010-11-01 11:52:08 -070068 }
69
Michael Jurka4842ed02011-07-07 15:33:20 -070070 public Drawable getFullResIcon(Resources resources, int iconId) {
Michael Jurka721d9722011-08-03 11:49:59 -070071 Drawable d;
Michael Jurka4842ed02011-07-07 15:33:20 -070072 try {
Michael Jurka721d9722011-08-03 11:49:59 -070073 d = resources.getDrawableForDensity(iconId, mIconDpi);
Michael Jurka4842ed02011-07-07 15:33:20 -070074 } catch (Resources.NotFoundException e) {
Michael Jurka721d9722011-08-03 11:49:59 -070075 d = null;
Michael Jurka4842ed02011-07-07 15:33:20 -070076 }
Michael Jurka721d9722011-08-03 11:49:59 -070077
78 return (d != null) ? d : getFullResDefaultActivityIcon();
Michael Jurkac9a96192010-11-01 11:52:08 -070079 }
80
Winson Chung0b9fcf52011-10-31 13:05:15 -070081 public Drawable getFullResIcon(String packageName, int iconId) {
Michael Jurkac9a96192010-11-01 11:52:08 -070082 Resources resources;
83 try {
Winson Chung0b9fcf52011-10-31 13:05:15 -070084 resources = mPackageManager.getResourcesForApplication(packageName);
85 } catch (PackageManager.NameNotFoundException e) {
86 resources = null;
87 }
88 if (resources != null) {
89 if (iconId != 0) {
90 return getFullResIcon(resources, iconId);
91 }
92 }
93 return getFullResDefaultActivityIcon();
94 }
95
96 public Drawable getFullResIcon(ResolveInfo info) {
97 Resources resources;
98 try {
99 resources = mPackageManager.getResourcesForApplication(
Michael Jurkac9a96192010-11-01 11:52:08 -0700100 info.activityInfo.applicationInfo);
101 } catch (PackageManager.NameNotFoundException e) {
102 resources = null;
103 }
104 if (resources != null) {
105 int iconId = info.activityInfo.getIconResource();
106 if (iconId != 0) {
107 return getFullResIcon(resources, iconId);
108 }
109 }
110 return getFullResDefaultActivityIcon();
111 }
112
Romain Guya28fd3f2010-03-15 14:44:42 -0700113 private Bitmap makeDefaultIcon() {
Michael Jurkac9a96192010-11-01 11:52:08 -0700114 Drawable d = getFullResDefaultActivityIcon();
Romain Guya28fd3f2010-03-15 14:44:42 -0700115 Bitmap b = Bitmap.createBitmap(Math.max(d.getIntrinsicWidth(), 1),
116 Math.max(d.getIntrinsicHeight(), 1),
117 Bitmap.Config.ARGB_8888);
118 Canvas c = new Canvas(b);
119 d.setBounds(0, 0, b.getWidth(), b.getHeight());
120 d.draw(c);
Adam Cohenaaf473c2011-08-03 12:02:47 -0700121 c.setBitmap(null);
Romain Guya28fd3f2010-03-15 14:44:42 -0700122 return b;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800123 }
124
125 /**
126 * Remove any records for the supplied ComponentName.
127 */
128 public void remove(ComponentName componentName) {
129 synchronized (mCache) {
130 mCache.remove(componentName);
131 }
132 }
133
134 /**
135 * Empty out the cache.
136 */
137 public void flush() {
138 synchronized (mCache) {
139 mCache.clear();
140 }
141 }
142
143 /**
144 * Fill in "application" with the icon and label for "info."
145 */
Winson Chungc3eecff2011-07-11 17:44:15 -0700146 public void getTitleAndIcon(ApplicationInfo application, ResolveInfo info,
147 HashMap<Object, CharSequence> labelCache) {
Joe Onorato0589f0f2010-02-08 13:44:00 -0800148 synchronized (mCache) {
Winson Chungc3eecff2011-07-11 17:44:15 -0700149 CacheEntry entry = cacheLocked(application.componentName, info, labelCache);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800150
151 application.title = entry.title;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800152 application.iconBitmap = entry.icon;
153 }
154 }
155
156 public Bitmap getIcon(Intent intent) {
Joe Onoratofad1fb52010-05-04 12:12:41 -0700157 synchronized (mCache) {
158 final ResolveInfo resolveInfo = mPackageManager.resolveActivity(intent, 0);
159 ComponentName component = intent.getComponent();
Joe Onorato0589f0f2010-02-08 13:44:00 -0800160
Joe Onoratofad1fb52010-05-04 12:12:41 -0700161 if (resolveInfo == null || component == null) {
162 return mDefaultIcon;
163 }
164
Winson Chungc3eecff2011-07-11 17:44:15 -0700165 CacheEntry entry = cacheLocked(component, resolveInfo, null);
Joe Onoratofad1fb52010-05-04 12:12:41 -0700166 return entry.icon;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800167 }
Joe Onorato0589f0f2010-02-08 13:44:00 -0800168 }
169
Winson Chungaac01e12011-08-17 10:37:13 -0700170 public Bitmap getIcon(ComponentName component, ResolveInfo resolveInfo,
171 HashMap<Object, CharSequence> labelCache) {
Joe Onoratofad1fb52010-05-04 12:12:41 -0700172 synchronized (mCache) {
173 if (resolveInfo == null || component == null) {
174 return null;
175 }
Joe Onorato0589f0f2010-02-08 13:44:00 -0800176
Winson Chungaac01e12011-08-17 10:37:13 -0700177 CacheEntry entry = cacheLocked(component, resolveInfo, labelCache);
Joe Onoratofad1fb52010-05-04 12:12:41 -0700178 return entry.icon;
179 }
Joe Onorato0589f0f2010-02-08 13:44:00 -0800180 }
181
Joe Onoratoddc9c1f2010-08-30 18:30:15 -0700182 public boolean isDefaultIcon(Bitmap icon) {
183 return mDefaultIcon == icon;
184 }
185
Winson Chungc3eecff2011-07-11 17:44:15 -0700186 private CacheEntry cacheLocked(ComponentName componentName, ResolveInfo info,
187 HashMap<Object, CharSequence> labelCache) {
Joe Onorato0589f0f2010-02-08 13:44:00 -0800188 CacheEntry entry = mCache.get(componentName);
189 if (entry == null) {
190 entry = new CacheEntry();
191
Joe Onorato84f6a8d2010-02-12 17:53:35 -0500192 mCache.put(componentName, entry);
193
Winson Chung5308f242011-08-18 12:12:41 -0700194 ComponentName key = LauncherModel.getComponentNameFromResolveInfo(info);
195 if (labelCache != null && labelCache.containsKey(key)) {
196 entry.title = labelCache.get(key).toString();
Winson Chungc3eecff2011-07-11 17:44:15 -0700197 } else {
198 entry.title = info.loadLabel(mPackageManager).toString();
199 if (labelCache != null) {
Winson Chung5308f242011-08-18 12:12:41 -0700200 labelCache.put(key, entry.title);
Winson Chungc3eecff2011-07-11 17:44:15 -0700201 }
202 }
Joe Onorato0589f0f2010-02-08 13:44:00 -0800203 if (entry.title == null) {
204 entry.title = info.activityInfo.name;
205 }
Kenny Root20b0a5f2011-03-10 10:53:06 -0800206
Michael Jurka4842ed02011-07-07 15:33:20 -0700207 entry.icon = Utilities.createIconBitmap(
Winson Chung0b9fcf52011-10-31 13:05:15 -0700208 getFullResIcon(info), mContext);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800209 }
210 return entry;
211 }
Daniel Sandler4e1cd232011-05-12 00:06:32 -0400212
213 public HashMap<ComponentName,Bitmap> getAllIcons() {
214 synchronized (mCache) {
215 HashMap<ComponentName,Bitmap> set = new HashMap<ComponentName,Bitmap>();
Daniel Sandler4e1cd232011-05-12 00:06:32 -0400216 for (ComponentName cn : mCache.keySet()) {
217 final CacheEntry e = mCache.get(cn);
218 set.put(cn, e.icon);
219 }
220 return set;
221 }
222 }
Joe Onorato0589f0f2010-02-08 13:44:00 -0800223}