blob: 2aab68beeda216eaf7caffdcd737fc24ffd53334 [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;
Joe Onorato0589f0f2010-02-08 13:44:00 -080032
33/**
34 * Cache of application icons. Icons can be made from any thread.
35 */
36public class IconCache {
Michael Jurka3a9fced2012-04-13 14:44:29 -070037 @SuppressWarnings("unused")
Joe Onorato0589f0f2010-02-08 13:44:00 -080038 private static final String TAG = "Launcher.IconCache";
39
40 private static final int INITIAL_ICON_CACHE_CAPACITY = 50;
41
42 private static class CacheEntry {
43 public Bitmap icon;
44 public String title;
Joe Onorato0589f0f2010-02-08 13:44:00 -080045 }
46
Romain Guya28fd3f2010-03-15 14:44:42 -070047 private final Bitmap mDefaultIcon;
Daniel Sandlercc8befa2013-06-11 14:45:48 -040048 private final Context mContext;
Romain Guya28fd3f2010-03-15 14:44:42 -070049 private final PackageManager mPackageManager;
Joe Onorato0589f0f2010-02-08 13:44:00 -080050 private final HashMap<ComponentName, CacheEntry> mCache =
51 new HashMap<ComponentName, CacheEntry>(INITIAL_ICON_CACHE_CAPACITY);
Michael Jurkac9a96192010-11-01 11:52:08 -070052 private int mIconDpi;
Joe Onorato0589f0f2010-02-08 13:44:00 -080053
Daniel Sandlercc8befa2013-06-11 14:45:48 -040054 public IconCache(Context context) {
Winson Chungd83f5f42012-02-13 14:27:42 -080055 ActivityManager activityManager =
56 (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
57
Joe Onorato0589f0f2010-02-08 13:44:00 -080058 mContext = context;
59 mPackageManager = context.getPackageManager();
Winson Chungd83f5f42012-02-13 14:27:42 -080060 mIconDpi = activityManager.getLauncherLargeIconDensity();
61
Michael Jurkac9a96192010-11-01 11:52:08 -070062 // need to set mIconDpi before getting default icon
Romain Guya28fd3f2010-03-15 14:44:42 -070063 mDefaultIcon = makeDefaultIcon();
64 }
65
Michael Jurkac9a96192010-11-01 11:52:08 -070066 public Drawable getFullResDefaultActivityIcon() {
67 return getFullResIcon(Resources.getSystem(),
Michael Jurka8b805b12012-04-18 14:23:14 -070068 android.R.mipmap.sym_def_app_icon);
Michael Jurkac9a96192010-11-01 11:52:08 -070069 }
70
Michael Jurka4842ed02011-07-07 15:33:20 -070071 public Drawable getFullResIcon(Resources resources, int iconId) {
Michael Jurka721d9722011-08-03 11:49:59 -070072 Drawable d;
Michael Jurka4842ed02011-07-07 15:33:20 -070073 try {
Michael Jurka721d9722011-08-03 11:49:59 -070074 d = resources.getDrawableForDensity(iconId, mIconDpi);
Michael Jurka4842ed02011-07-07 15:33:20 -070075 } catch (Resources.NotFoundException e) {
Michael Jurka721d9722011-08-03 11:49:59 -070076 d = null;
Michael Jurka4842ed02011-07-07 15:33:20 -070077 }
Michael Jurka721d9722011-08-03 11:49:59 -070078
79 return (d != null) ? d : getFullResDefaultActivityIcon();
Michael Jurkac9a96192010-11-01 11:52:08 -070080 }
81
Winson Chung0b9fcf52011-10-31 13:05:15 -070082 public Drawable getFullResIcon(String packageName, int iconId) {
Michael Jurkac9a96192010-11-01 11:52:08 -070083 Resources resources;
84 try {
Winson Chung0b9fcf52011-10-31 13:05:15 -070085 resources = mPackageManager.getResourcesForApplication(packageName);
86 } catch (PackageManager.NameNotFoundException e) {
87 resources = null;
88 }
89 if (resources != null) {
90 if (iconId != 0) {
91 return getFullResIcon(resources, iconId);
92 }
93 }
94 return getFullResDefaultActivityIcon();
95 }
96
97 public Drawable getFullResIcon(ResolveInfo info) {
Michael Jurkadac85912012-05-18 15:04:49 -070098 return getFullResIcon(info.activityInfo);
99 }
100
101 public Drawable getFullResIcon(ActivityInfo info) {
102
Winson Chung0b9fcf52011-10-31 13:05:15 -0700103 Resources resources;
104 try {
105 resources = mPackageManager.getResourcesForApplication(
Michael Jurkadac85912012-05-18 15:04:49 -0700106 info.applicationInfo);
Michael Jurkac9a96192010-11-01 11:52:08 -0700107 } catch (PackageManager.NameNotFoundException e) {
108 resources = null;
109 }
110 if (resources != null) {
Michael Jurkadac85912012-05-18 15:04:49 -0700111 int iconId = info.getIconResource();
Michael Jurkac9a96192010-11-01 11:52:08 -0700112 if (iconId != 0) {
113 return getFullResIcon(resources, iconId);
114 }
115 }
116 return getFullResDefaultActivityIcon();
117 }
118
Romain Guya28fd3f2010-03-15 14:44:42 -0700119 private Bitmap makeDefaultIcon() {
Michael Jurkac9a96192010-11-01 11:52:08 -0700120 Drawable d = getFullResDefaultActivityIcon();
Romain Guya28fd3f2010-03-15 14:44:42 -0700121 Bitmap b = Bitmap.createBitmap(Math.max(d.getIntrinsicWidth(), 1),
122 Math.max(d.getIntrinsicHeight(), 1),
123 Bitmap.Config.ARGB_8888);
124 Canvas c = new Canvas(b);
125 d.setBounds(0, 0, b.getWidth(), b.getHeight());
126 d.draw(c);
Adam Cohenaaf473c2011-08-03 12:02:47 -0700127 c.setBitmap(null);
Romain Guya28fd3f2010-03-15 14:44:42 -0700128 return b;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800129 }
130
131 /**
132 * Remove any records for the supplied ComponentName.
133 */
134 public void remove(ComponentName componentName) {
135 synchronized (mCache) {
136 mCache.remove(componentName);
137 }
138 }
139
140 /**
141 * Empty out the cache.
142 */
143 public void flush() {
144 synchronized (mCache) {
145 mCache.clear();
146 }
147 }
148
149 /**
Winson Chunge5467dc2013-10-14 17:03:04 -0700150 * Empty out the cache that aren't of the correct grid size
151 */
152 public void flushInvalidIcons(DeviceProfile grid) {
153 synchronized (mCache) {
154 for (ComponentName cn : mCache.keySet()) {
155 final CacheEntry e = mCache.get(cn);
156 if (e.icon.getWidth() != grid.iconSizePx || e.icon.getHeight() != grid.iconSizePx) {
157 mCache.remove(cn);
158 }
159 }
160 }
161 }
162
163 /**
Joe Onorato0589f0f2010-02-08 13:44:00 -0800164 * Fill in "application" with the icon and label for "info."
165 */
Michael Jurkaeadbfc52013-09-04 00:45:37 +0200166 public void getTitleAndIcon(AppInfo application, ResolveInfo info,
Winson Chungc3eecff2011-07-11 17:44:15 -0700167 HashMap<Object, CharSequence> labelCache) {
Joe Onorato0589f0f2010-02-08 13:44:00 -0800168 synchronized (mCache) {
Winson Chungc3eecff2011-07-11 17:44:15 -0700169 CacheEntry entry = cacheLocked(application.componentName, info, labelCache);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800170
171 application.title = entry.title;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800172 application.iconBitmap = entry.icon;
173 }
174 }
175
176 public Bitmap getIcon(Intent intent) {
Joe Onoratofad1fb52010-05-04 12:12:41 -0700177 synchronized (mCache) {
178 final ResolveInfo resolveInfo = mPackageManager.resolveActivity(intent, 0);
179 ComponentName component = intent.getComponent();
Joe Onorato0589f0f2010-02-08 13:44:00 -0800180
Joe Onoratofad1fb52010-05-04 12:12:41 -0700181 if (resolveInfo == null || component == null) {
182 return mDefaultIcon;
183 }
184
Winson Chungc3eecff2011-07-11 17:44:15 -0700185 CacheEntry entry = cacheLocked(component, resolveInfo, null);
Joe Onoratofad1fb52010-05-04 12:12:41 -0700186 return entry.icon;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800187 }
Joe Onorato0589f0f2010-02-08 13:44:00 -0800188 }
189
Winson Chungaac01e12011-08-17 10:37:13 -0700190 public Bitmap getIcon(ComponentName component, ResolveInfo resolveInfo,
191 HashMap<Object, CharSequence> labelCache) {
Joe Onoratofad1fb52010-05-04 12:12:41 -0700192 synchronized (mCache) {
193 if (resolveInfo == null || component == null) {
194 return null;
195 }
Joe Onorato0589f0f2010-02-08 13:44:00 -0800196
Winson Chungaac01e12011-08-17 10:37:13 -0700197 CacheEntry entry = cacheLocked(component, resolveInfo, labelCache);
Joe Onoratofad1fb52010-05-04 12:12:41 -0700198 return entry.icon;
199 }
Joe Onorato0589f0f2010-02-08 13:44:00 -0800200 }
201
Joe Onoratoddc9c1f2010-08-30 18:30:15 -0700202 public boolean isDefaultIcon(Bitmap icon) {
203 return mDefaultIcon == icon;
204 }
205
Winson Chungc3eecff2011-07-11 17:44:15 -0700206 private CacheEntry cacheLocked(ComponentName componentName, ResolveInfo info,
207 HashMap<Object, CharSequence> labelCache) {
Joe Onorato0589f0f2010-02-08 13:44:00 -0800208 CacheEntry entry = mCache.get(componentName);
209 if (entry == null) {
210 entry = new CacheEntry();
211
Joe Onorato84f6a8d2010-02-12 17:53:35 -0500212 mCache.put(componentName, entry);
213
Winson Chung5308f242011-08-18 12:12:41 -0700214 ComponentName key = LauncherModel.getComponentNameFromResolveInfo(info);
215 if (labelCache != null && labelCache.containsKey(key)) {
216 entry.title = labelCache.get(key).toString();
Winson Chungc3eecff2011-07-11 17:44:15 -0700217 } else {
218 entry.title = info.loadLabel(mPackageManager).toString();
219 if (labelCache != null) {
Winson Chung5308f242011-08-18 12:12:41 -0700220 labelCache.put(key, entry.title);
Winson Chungc3eecff2011-07-11 17:44:15 -0700221 }
222 }
Joe Onorato0589f0f2010-02-08 13:44:00 -0800223 if (entry.title == null) {
224 entry.title = info.activityInfo.name;
225 }
Kenny Root20b0a5f2011-03-10 10:53:06 -0800226
Michael Jurka4842ed02011-07-07 15:33:20 -0700227 entry.icon = Utilities.createIconBitmap(
Winson Chung0b9fcf52011-10-31 13:05:15 -0700228 getFullResIcon(info), mContext);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800229 }
230 return entry;
231 }
Daniel Sandler4e1cd232011-05-12 00:06:32 -0400232
233 public HashMap<ComponentName,Bitmap> getAllIcons() {
234 synchronized (mCache) {
235 HashMap<ComponentName,Bitmap> set = new HashMap<ComponentName,Bitmap>();
Daniel Sandler4e1cd232011-05-12 00:06:32 -0400236 for (ComponentName cn : mCache.keySet()) {
237 final CacheEntry e = mCache.get(cn);
238 set.put(cn, e.icon);
239 }
240 return set;
241 }
242 }
Joe Onorato0589f0f2010-02-08 13:44:00 -0800243}