blob: 543b8ee2dc2c071b2da2fb341ec13a6daa110a27 [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
Daniel Sandler325dc232013-06-05 22:57:57 -040017package com.android.launcher3;
Joe Onorato0589f0f2010-02-08 13:44:00 -080018
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;
Michael Jurkadac85912012-05-18 15:04:49 -070023import android.content.pm.ActivityInfo;
Joe Onorato0589f0f2010-02-08 13:44:00 -080024import android.content.pm.PackageManager;
25import android.content.pm.ResolveInfo;
Michael Jurkac9a96192010-11-01 11:52:08 -070026import android.content.res.Resources;
Joe Onorato0589f0f2010-02-08 13:44:00 -080027import android.graphics.Bitmap;
Romain Guya28fd3f2010-03-15 14:44:42 -070028import android.graphics.Canvas;
Joe Onorato0589f0f2010-02-08 13:44:00 -080029import android.graphics.drawable.Drawable;
Joe Onorato0589f0f2010-02-08 13:44:00 -080030
Joe Onorato0589f0f2010-02-08 13:44:00 -080031import java.util.HashMap;
Adam Cohenb6d33df2013-10-15 10:18:02 -070032import java.util.Iterator;
33import java.util.Map.Entry;
Joe Onorato0589f0f2010-02-08 13:44:00 -080034
35/**
36 * Cache of application icons. Icons can be made from any thread.
37 */
38public class IconCache {
Michael Jurka3a9fced2012-04-13 14:44:29 -070039 @SuppressWarnings("unused")
Joe Onorato0589f0f2010-02-08 13:44:00 -080040 private static final String TAG = "Launcher.IconCache";
41
42 private static final int INITIAL_ICON_CACHE_CAPACITY = 50;
43
44 private static class CacheEntry {
45 public Bitmap icon;
46 public String title;
Joe Onorato0589f0f2010-02-08 13:44:00 -080047 }
48
Romain Guya28fd3f2010-03-15 14:44:42 -070049 private final Bitmap mDefaultIcon;
Daniel Sandlercc8befa2013-06-11 14:45:48 -040050 private final Context mContext;
Romain Guya28fd3f2010-03-15 14:44:42 -070051 private final PackageManager mPackageManager;
Joe Onorato0589f0f2010-02-08 13:44:00 -080052 private final HashMap<ComponentName, CacheEntry> mCache =
53 new HashMap<ComponentName, CacheEntry>(INITIAL_ICON_CACHE_CAPACITY);
Michael Jurkac9a96192010-11-01 11:52:08 -070054 private int mIconDpi;
Joe Onorato0589f0f2010-02-08 13:44:00 -080055
Daniel Sandlercc8befa2013-06-11 14:45:48 -040056 public IconCache(Context context) {
Winson Chungd83f5f42012-02-13 14:27:42 -080057 ActivityManager activityManager =
58 (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
59
Joe Onorato0589f0f2010-02-08 13:44:00 -080060 mContext = context;
61 mPackageManager = context.getPackageManager();
Winson Chungd83f5f42012-02-13 14:27:42 -080062 mIconDpi = activityManager.getLauncherLargeIconDensity();
63
Michael Jurkac9a96192010-11-01 11:52:08 -070064 // need to set mIconDpi before getting default icon
Romain Guya28fd3f2010-03-15 14:44:42 -070065 mDefaultIcon = makeDefaultIcon();
66 }
67
Michael Jurkac9a96192010-11-01 11:52:08 -070068 public Drawable getFullResDefaultActivityIcon() {
69 return getFullResIcon(Resources.getSystem(),
Michael Jurka8b805b12012-04-18 14:23:14 -070070 android.R.mipmap.sym_def_app_icon);
Michael Jurkac9a96192010-11-01 11:52:08 -070071 }
72
Michael Jurka4842ed02011-07-07 15:33:20 -070073 public Drawable getFullResIcon(Resources resources, int iconId) {
Michael Jurka721d9722011-08-03 11:49:59 -070074 Drawable d;
Michael Jurka4842ed02011-07-07 15:33:20 -070075 try {
Michael Jurka721d9722011-08-03 11:49:59 -070076 d = resources.getDrawableForDensity(iconId, mIconDpi);
Michael Jurka4842ed02011-07-07 15:33:20 -070077 } catch (Resources.NotFoundException e) {
Michael Jurka721d9722011-08-03 11:49:59 -070078 d = null;
Michael Jurka4842ed02011-07-07 15:33:20 -070079 }
Michael Jurka721d9722011-08-03 11:49:59 -070080
81 return (d != null) ? d : getFullResDefaultActivityIcon();
Michael Jurkac9a96192010-11-01 11:52:08 -070082 }
83
Winson Chung0b9fcf52011-10-31 13:05:15 -070084 public Drawable getFullResIcon(String packageName, int iconId) {
Michael Jurkac9a96192010-11-01 11:52:08 -070085 Resources resources;
86 try {
Winson Chung0b9fcf52011-10-31 13:05:15 -070087 resources = mPackageManager.getResourcesForApplication(packageName);
88 } catch (PackageManager.NameNotFoundException e) {
89 resources = null;
90 }
91 if (resources != null) {
92 if (iconId != 0) {
93 return getFullResIcon(resources, iconId);
94 }
95 }
96 return getFullResDefaultActivityIcon();
97 }
98
99 public Drawable getFullResIcon(ResolveInfo info) {
Michael Jurkadac85912012-05-18 15:04:49 -0700100 return getFullResIcon(info.activityInfo);
101 }
102
103 public Drawable getFullResIcon(ActivityInfo info) {
104
Winson Chung0b9fcf52011-10-31 13:05:15 -0700105 Resources resources;
106 try {
107 resources = mPackageManager.getResourcesForApplication(
Michael Jurkadac85912012-05-18 15:04:49 -0700108 info.applicationInfo);
Michael Jurkac9a96192010-11-01 11:52:08 -0700109 } catch (PackageManager.NameNotFoundException e) {
110 resources = null;
111 }
112 if (resources != null) {
Michael Jurkadac85912012-05-18 15:04:49 -0700113 int iconId = info.getIconResource();
Michael Jurkac9a96192010-11-01 11:52:08 -0700114 if (iconId != 0) {
115 return getFullResIcon(resources, iconId);
116 }
117 }
118 return getFullResDefaultActivityIcon();
119 }
120
Romain Guya28fd3f2010-03-15 14:44:42 -0700121 private Bitmap makeDefaultIcon() {
Michael Jurkac9a96192010-11-01 11:52:08 -0700122 Drawable d = getFullResDefaultActivityIcon();
Romain Guya28fd3f2010-03-15 14:44:42 -0700123 Bitmap b = Bitmap.createBitmap(Math.max(d.getIntrinsicWidth(), 1),
124 Math.max(d.getIntrinsicHeight(), 1),
125 Bitmap.Config.ARGB_8888);
126 Canvas c = new Canvas(b);
127 d.setBounds(0, 0, b.getWidth(), b.getHeight());
128 d.draw(c);
Adam Cohenaaf473c2011-08-03 12:02:47 -0700129 c.setBitmap(null);
Romain Guya28fd3f2010-03-15 14:44:42 -0700130 return b;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800131 }
132
133 /**
134 * Remove any records for the supplied ComponentName.
135 */
136 public void remove(ComponentName componentName) {
137 synchronized (mCache) {
138 mCache.remove(componentName);
139 }
140 }
141
142 /**
143 * Empty out the cache.
144 */
145 public void flush() {
146 synchronized (mCache) {
147 mCache.clear();
148 }
149 }
150
151 /**
Winson Chunge5467dc2013-10-14 17:03:04 -0700152 * Empty out the cache that aren't of the correct grid size
153 */
154 public void flushInvalidIcons(DeviceProfile grid) {
155 synchronized (mCache) {
Adam Cohenb6d33df2013-10-15 10:18:02 -0700156 Iterator<Entry<ComponentName, CacheEntry>> it = mCache.entrySet().iterator();
157 while (it.hasNext()) {
158 final CacheEntry e = it.next().getValue();
Winson Chunge5467dc2013-10-14 17:03:04 -0700159 if (e.icon.getWidth() != grid.iconSizePx || e.icon.getHeight() != grid.iconSizePx) {
Adam Cohenb6d33df2013-10-15 10:18:02 -0700160 it.remove();
Winson Chunge5467dc2013-10-14 17:03:04 -0700161 }
162 }
163 }
164 }
165
166 /**
Joe Onorato0589f0f2010-02-08 13:44:00 -0800167 * Fill in "application" with the icon and label for "info."
168 */
Michael Jurkaeadbfc52013-09-04 00:45:37 +0200169 public void getTitleAndIcon(AppInfo application, ResolveInfo info,
Winson Chungc3eecff2011-07-11 17:44:15 -0700170 HashMap<Object, CharSequence> labelCache) {
Joe Onorato0589f0f2010-02-08 13:44:00 -0800171 synchronized (mCache) {
Winson Chungc3eecff2011-07-11 17:44:15 -0700172 CacheEntry entry = cacheLocked(application.componentName, info, labelCache);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800173
174 application.title = entry.title;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800175 application.iconBitmap = entry.icon;
176 }
177 }
178
179 public Bitmap getIcon(Intent intent) {
Joe Onoratofad1fb52010-05-04 12:12:41 -0700180 synchronized (mCache) {
181 final ResolveInfo resolveInfo = mPackageManager.resolveActivity(intent, 0);
182 ComponentName component = intent.getComponent();
Joe Onorato0589f0f2010-02-08 13:44:00 -0800183
Joe Onoratofad1fb52010-05-04 12:12:41 -0700184 if (resolveInfo == null || component == null) {
185 return mDefaultIcon;
186 }
187
Winson Chungc3eecff2011-07-11 17:44:15 -0700188 CacheEntry entry = cacheLocked(component, resolveInfo, null);
Joe Onoratofad1fb52010-05-04 12:12:41 -0700189 return entry.icon;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800190 }
Joe Onorato0589f0f2010-02-08 13:44:00 -0800191 }
192
Winson Chungaac01e12011-08-17 10:37:13 -0700193 public Bitmap getIcon(ComponentName component, ResolveInfo resolveInfo,
194 HashMap<Object, CharSequence> labelCache) {
Joe Onoratofad1fb52010-05-04 12:12:41 -0700195 synchronized (mCache) {
196 if (resolveInfo == null || component == null) {
197 return null;
198 }
Joe Onorato0589f0f2010-02-08 13:44:00 -0800199
Winson Chungaac01e12011-08-17 10:37:13 -0700200 CacheEntry entry = cacheLocked(component, resolveInfo, labelCache);
Joe Onoratofad1fb52010-05-04 12:12:41 -0700201 return entry.icon;
202 }
Joe Onorato0589f0f2010-02-08 13:44:00 -0800203 }
204
Joe Onoratoddc9c1f2010-08-30 18:30:15 -0700205 public boolean isDefaultIcon(Bitmap icon) {
206 return mDefaultIcon == icon;
207 }
208
Winson Chungc3eecff2011-07-11 17:44:15 -0700209 private CacheEntry cacheLocked(ComponentName componentName, ResolveInfo info,
210 HashMap<Object, CharSequence> labelCache) {
Joe Onorato0589f0f2010-02-08 13:44:00 -0800211 CacheEntry entry = mCache.get(componentName);
212 if (entry == null) {
213 entry = new CacheEntry();
214
Joe Onorato84f6a8d2010-02-12 17:53:35 -0500215 mCache.put(componentName, entry);
216
Winson Chung5308f242011-08-18 12:12:41 -0700217 ComponentName key = LauncherModel.getComponentNameFromResolveInfo(info);
218 if (labelCache != null && labelCache.containsKey(key)) {
219 entry.title = labelCache.get(key).toString();
Winson Chungc3eecff2011-07-11 17:44:15 -0700220 } else {
221 entry.title = info.loadLabel(mPackageManager).toString();
222 if (labelCache != null) {
Winson Chung5308f242011-08-18 12:12:41 -0700223 labelCache.put(key, entry.title);
Winson Chungc3eecff2011-07-11 17:44:15 -0700224 }
225 }
Joe Onorato0589f0f2010-02-08 13:44:00 -0800226 if (entry.title == null) {
227 entry.title = info.activityInfo.name;
228 }
Kenny Root20b0a5f2011-03-10 10:53:06 -0800229
Michael Jurka4842ed02011-07-07 15:33:20 -0700230 entry.icon = Utilities.createIconBitmap(
Winson Chung0b9fcf52011-10-31 13:05:15 -0700231 getFullResIcon(info), mContext);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800232 }
233 return entry;
234 }
Daniel Sandler4e1cd232011-05-12 00:06:32 -0400235
236 public HashMap<ComponentName,Bitmap> getAllIcons() {
237 synchronized (mCache) {
238 HashMap<ComponentName,Bitmap> set = new HashMap<ComponentName,Bitmap>();
Daniel Sandler4e1cd232011-05-12 00:06:32 -0400239 for (ComponentName cn : mCache.keySet()) {
240 final CacheEntry e = mCache.get(cn);
241 set.put(cn, e.icon);
242 }
243 return set;
244 }
245 }
Joe Onorato0589f0f2010-02-08 13:44:00 -0800246}