blob: 4d38cc9a3fd54b0bdd0478885ffed2dd5bca1c26 [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 Chungaf235002012-01-24 14:46:24 -080019import android.app.ActivityManager;
Joe Onorato0589f0f2010-02-08 13:44:00 -080020import android.content.ComponentName;
Winson Chungaf235002012-01-24 14:46:24 -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 {
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;
Joe Onorato0589f0f2010-02-08 13:44:00 -080043 }
44
Romain Guya28fd3f2010-03-15 14:44:42 -070045 private final Bitmap mDefaultIcon;
46 private final LauncherApplication mContext;
47 private final PackageManager mPackageManager;
Joe Onorato0589f0f2010-02-08 13:44:00 -080048 private final HashMap<ComponentName, CacheEntry> mCache =
49 new HashMap<ComponentName, CacheEntry>(INITIAL_ICON_CACHE_CAPACITY);
Michael Jurkac9a96192010-11-01 11:52:08 -070050 private int mIconDpi;
Joe Onorato0589f0f2010-02-08 13:44:00 -080051
52 public IconCache(LauncherApplication context) {
53 mContext = context;
54 mPackageManager = context.getPackageManager();
Michael Jurkaef7b5da2011-08-05 19:12:37 -070055 int density = context.getResources().getDisplayMetrics().densityDpi;
Michael Jurkaa2eb1702011-05-12 14:57:05 -070056 if (LauncherApplication.isScreenLarge()) {
Winson Chungaf235002012-01-24 14:46:24 -080057 ActivityManager activityManager =
58 (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
59 mIconDpi = activityManager.getLauncherLargeIconDensity();
Michael Jurkac9a96192010-11-01 11:52:08 -070060 } else {
61 mIconDpi = context.getResources().getDisplayMetrics().densityDpi;
62 }
63 // need to set mIconDpi before getting default icon
Romain Guya28fd3f2010-03-15 14:44:42 -070064 mDefaultIcon = makeDefaultIcon();
65 }
66
Michael Jurkac9a96192010-11-01 11:52:08 -070067 public Drawable getFullResDefaultActivityIcon() {
68 return getFullResIcon(Resources.getSystem(),
Kenny Rootf5675de2011-01-12 10:15:46 -080069 com.android.internal.R.mipmap.sym_def_app_icon);
Michael Jurkac9a96192010-11-01 11:52:08 -070070 }
71
Michael Jurka4842ed02011-07-07 15:33:20 -070072 public Drawable getFullResIcon(Resources resources, int iconId) {
Michael Jurka721d9722011-08-03 11:49:59 -070073 Drawable d;
Michael Jurka4842ed02011-07-07 15:33:20 -070074 try {
Michael Jurka721d9722011-08-03 11:49:59 -070075 d = resources.getDrawableForDensity(iconId, mIconDpi);
Michael Jurka4842ed02011-07-07 15:33:20 -070076 } catch (Resources.NotFoundException e) {
Michael Jurka721d9722011-08-03 11:49:59 -070077 d = null;
Michael Jurka4842ed02011-07-07 15:33:20 -070078 }
Michael Jurka721d9722011-08-03 11:49:59 -070079
80 return (d != null) ? d : getFullResDefaultActivityIcon();
Michael Jurkac9a96192010-11-01 11:52:08 -070081 }
82
Winson Chung0b9fcf52011-10-31 13:05:15 -070083 public Drawable getFullResIcon(String packageName, int iconId) {
Michael Jurkac9a96192010-11-01 11:52:08 -070084 Resources resources;
85 try {
Winson Chung0b9fcf52011-10-31 13:05:15 -070086 resources = mPackageManager.getResourcesForApplication(packageName);
87 } catch (PackageManager.NameNotFoundException e) {
88 resources = null;
89 }
90 if (resources != null) {
91 if (iconId != 0) {
92 return getFullResIcon(resources, iconId);
93 }
94 }
95 return getFullResDefaultActivityIcon();
96 }
97
98 public Drawable getFullResIcon(ResolveInfo info) {
99 Resources resources;
100 try {
101 resources = mPackageManager.getResourcesForApplication(
Michael Jurkac9a96192010-11-01 11:52:08 -0700102 info.activityInfo.applicationInfo);
103 } catch (PackageManager.NameNotFoundException e) {
104 resources = null;
105 }
106 if (resources != null) {
107 int iconId = info.activityInfo.getIconResource();
108 if (iconId != 0) {
109 return getFullResIcon(resources, iconId);
110 }
111 }
112 return getFullResDefaultActivityIcon();
113 }
114
Romain Guya28fd3f2010-03-15 14:44:42 -0700115 private Bitmap makeDefaultIcon() {
Michael Jurkac9a96192010-11-01 11:52:08 -0700116 Drawable d = getFullResDefaultActivityIcon();
Romain Guya28fd3f2010-03-15 14:44:42 -0700117 Bitmap b = Bitmap.createBitmap(Math.max(d.getIntrinsicWidth(), 1),
118 Math.max(d.getIntrinsicHeight(), 1),
119 Bitmap.Config.ARGB_8888);
120 Canvas c = new Canvas(b);
121 d.setBounds(0, 0, b.getWidth(), b.getHeight());
122 d.draw(c);
Adam Cohenaaf473c2011-08-03 12:02:47 -0700123 c.setBitmap(null);
Romain Guya28fd3f2010-03-15 14:44:42 -0700124 return b;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800125 }
126
127 /**
128 * Remove any records for the supplied ComponentName.
129 */
130 public void remove(ComponentName componentName) {
131 synchronized (mCache) {
132 mCache.remove(componentName);
133 }
134 }
135
136 /**
137 * Empty out the cache.
138 */
139 public void flush() {
140 synchronized (mCache) {
141 mCache.clear();
142 }
143 }
144
145 /**
146 * Fill in "application" with the icon and label for "info."
147 */
Winson Chungc3eecff2011-07-11 17:44:15 -0700148 public void getTitleAndIcon(ApplicationInfo application, ResolveInfo info,
149 HashMap<Object, CharSequence> labelCache) {
Joe Onorato0589f0f2010-02-08 13:44:00 -0800150 synchronized (mCache) {
Winson Chungc3eecff2011-07-11 17:44:15 -0700151 CacheEntry entry = cacheLocked(application.componentName, info, labelCache);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800152
153 application.title = entry.title;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800154 application.iconBitmap = entry.icon;
155 }
156 }
157
158 public Bitmap getIcon(Intent intent) {
Joe Onoratofad1fb52010-05-04 12:12:41 -0700159 synchronized (mCache) {
160 final ResolveInfo resolveInfo = mPackageManager.resolveActivity(intent, 0);
161 ComponentName component = intent.getComponent();
Joe Onorato0589f0f2010-02-08 13:44:00 -0800162
Joe Onoratofad1fb52010-05-04 12:12:41 -0700163 if (resolveInfo == null || component == null) {
164 return mDefaultIcon;
165 }
166
Winson Chungc3eecff2011-07-11 17:44:15 -0700167 CacheEntry entry = cacheLocked(component, resolveInfo, null);
Joe Onoratofad1fb52010-05-04 12:12:41 -0700168 return entry.icon;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800169 }
Joe Onorato0589f0f2010-02-08 13:44:00 -0800170 }
171
Winson Chungaac01e12011-08-17 10:37:13 -0700172 public Bitmap getIcon(ComponentName component, ResolveInfo resolveInfo,
173 HashMap<Object, CharSequence> labelCache) {
Joe Onoratofad1fb52010-05-04 12:12:41 -0700174 synchronized (mCache) {
175 if (resolveInfo == null || component == null) {
176 return null;
177 }
Joe Onorato0589f0f2010-02-08 13:44:00 -0800178
Winson Chungaac01e12011-08-17 10:37:13 -0700179 CacheEntry entry = cacheLocked(component, resolveInfo, labelCache);
Joe Onoratofad1fb52010-05-04 12:12:41 -0700180 return entry.icon;
181 }
Joe Onorato0589f0f2010-02-08 13:44:00 -0800182 }
183
Joe Onoratoddc9c1f2010-08-30 18:30:15 -0700184 public boolean isDefaultIcon(Bitmap icon) {
185 return mDefaultIcon == icon;
186 }
187
Winson Chungc3eecff2011-07-11 17:44:15 -0700188 private CacheEntry cacheLocked(ComponentName componentName, ResolveInfo info,
189 HashMap<Object, CharSequence> labelCache) {
Joe Onorato0589f0f2010-02-08 13:44:00 -0800190 CacheEntry entry = mCache.get(componentName);
191 if (entry == null) {
192 entry = new CacheEntry();
193
Joe Onorato84f6a8d2010-02-12 17:53:35 -0500194 mCache.put(componentName, entry);
195
Winson Chung5308f242011-08-18 12:12:41 -0700196 ComponentName key = LauncherModel.getComponentNameFromResolveInfo(info);
197 if (labelCache != null && labelCache.containsKey(key)) {
198 entry.title = labelCache.get(key).toString();
Winson Chungc3eecff2011-07-11 17:44:15 -0700199 } else {
200 entry.title = info.loadLabel(mPackageManager).toString();
201 if (labelCache != null) {
Winson Chung5308f242011-08-18 12:12:41 -0700202 labelCache.put(key, entry.title);
Winson Chungc3eecff2011-07-11 17:44:15 -0700203 }
204 }
Joe Onorato0589f0f2010-02-08 13:44:00 -0800205 if (entry.title == null) {
206 entry.title = info.activityInfo.name;
207 }
Kenny Root20b0a5f2011-03-10 10:53:06 -0800208
Michael Jurka4842ed02011-07-07 15:33:20 -0700209 entry.icon = Utilities.createIconBitmap(
Winson Chung0b9fcf52011-10-31 13:05:15 -0700210 getFullResIcon(info), mContext);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800211 }
212 return entry;
213 }
Daniel Sandler4e1cd232011-05-12 00:06:32 -0400214
215 public HashMap<ComponentName,Bitmap> getAllIcons() {
216 synchronized (mCache) {
217 HashMap<ComponentName,Bitmap> set = new HashMap<ComponentName,Bitmap>();
Daniel Sandler4e1cd232011-05-12 00:06:32 -0400218 for (ComponentName cn : mCache.keySet()) {
219 final CacheEntry e = mCache.get(cn);
220 set.put(cn, e.icon);
221 }
222 return set;
223 }
224 }
Joe Onorato0589f0f2010-02-08 13:44:00 -0800225}