blob: 4caada3e668bc1a20c24323d3c8297b3526b80be [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
19import android.content.ComponentName;
Joe Onorato0589f0f2010-02-08 13:44:00 -080020import android.content.Intent;
Joe Onorato0589f0f2010-02-08 13:44:00 -080021import android.content.pm.PackageManager;
22import android.content.pm.ResolveInfo;
Michael Jurkac9a96192010-11-01 11:52:08 -070023import android.content.res.Resources;
Joe Onorato0589f0f2010-02-08 13:44:00 -080024import android.graphics.Bitmap;
Romain Guya28fd3f2010-03-15 14:44:42 -070025import android.graphics.Canvas;
Joe Onorato0589f0f2010-02-08 13:44:00 -080026import android.graphics.drawable.Drawable;
Ramanan Rajeswarand1dbfe32012-02-13 13:54:42 -080027import android.util.DisplayMetrics;
Joe Onorato0589f0f2010-02-08 13:44:00 -080028
Joe Onorato0589f0f2010-02-08 13:44:00 -080029import java.util.HashMap;
Joe Onorato0589f0f2010-02-08 13:44:00 -080030
31/**
32 * Cache of application icons. Icons can be made from any thread.
33 */
34public class IconCache {
35 private static final String TAG = "Launcher.IconCache";
36
37 private static final int INITIAL_ICON_CACHE_CAPACITY = 50;
38
39 private static class CacheEntry {
40 public Bitmap icon;
41 public String title;
Joe Onorato0589f0f2010-02-08 13:44:00 -080042 }
43
Romain Guya28fd3f2010-03-15 14:44:42 -070044 private final Bitmap mDefaultIcon;
45 private final LauncherApplication mContext;
46 private final PackageManager mPackageManager;
Joe Onorato0589f0f2010-02-08 13:44:00 -080047 private final HashMap<ComponentName, CacheEntry> mCache =
48 new HashMap<ComponentName, CacheEntry>(INITIAL_ICON_CACHE_CAPACITY);
Michael Jurkac9a96192010-11-01 11:52:08 -070049 private int mIconDpi;
Joe Onorato0589f0f2010-02-08 13:44:00 -080050
51 public IconCache(LauncherApplication context) {
52 mContext = context;
53 mPackageManager = context.getPackageManager();
Michael Jurkaa2eb1702011-05-12 14:57:05 -070054 if (LauncherApplication.isScreenLarge()) {
Ramanan Rajeswarand1dbfe32012-02-13 13:54:42 -080055 if (density == DisplayMetrics.DENSITY_LOW) {
56 mIconDpi = DisplayMetrics.DENSITY_MEDIUM;
57 } else if (density == DisplayMetrics.DENSITY_MEDIUM) {
58 mIconDpi = DisplayMetrics.DENSITY_HIGH;
59 } else if (density == DisplayMetrics.DENSITY_HIGH) {
60 mIconDpi = DisplayMetrics.DENSITY_XHIGH;
61 } else if (density == DisplayMetrics.DENSITY_XHIGH) {
62 // We'll need to use a denser icon, or some sort of a mipmap
63 mIconDpi = DisplayMetrics.DENSITY_XHIGH;
64 }
Michael Jurkac9a96192010-11-01 11:52:08 -070065 } else {
66 mIconDpi = context.getResources().getDisplayMetrics().densityDpi;
67 }
68 // need to set mIconDpi before getting default icon
Romain Guya28fd3f2010-03-15 14:44:42 -070069 mDefaultIcon = makeDefaultIcon();
70 }
71
Michael Jurkac9a96192010-11-01 11:52:08 -070072 public Drawable getFullResDefaultActivityIcon() {
73 return getFullResIcon(Resources.getSystem(),
Kenny Rootf5675de2011-01-12 10:15:46 -080074 com.android.internal.R.mipmap.sym_def_app_icon);
Michael Jurkac9a96192010-11-01 11:52:08 -070075 }
76
Michael Jurka4842ed02011-07-07 15:33:20 -070077 public Drawable getFullResIcon(Resources resources, int iconId) {
Michael Jurka721d9722011-08-03 11:49:59 -070078 Drawable d;
Michael Jurka4842ed02011-07-07 15:33:20 -070079 try {
Michael Jurka721d9722011-08-03 11:49:59 -070080 d = resources.getDrawableForDensity(iconId, mIconDpi);
Michael Jurka4842ed02011-07-07 15:33:20 -070081 } catch (Resources.NotFoundException e) {
Michael Jurka721d9722011-08-03 11:49:59 -070082 d = null;
Michael Jurka4842ed02011-07-07 15:33:20 -070083 }
Michael Jurka721d9722011-08-03 11:49:59 -070084
85 return (d != null) ? d : getFullResDefaultActivityIcon();
Michael Jurkac9a96192010-11-01 11:52:08 -070086 }
87
Winson Chung0b9fcf52011-10-31 13:05:15 -070088 public Drawable getFullResIcon(String packageName, int iconId) {
Michael Jurkac9a96192010-11-01 11:52:08 -070089 Resources resources;
90 try {
Winson Chung0b9fcf52011-10-31 13:05:15 -070091 resources = mPackageManager.getResourcesForApplication(packageName);
92 } catch (PackageManager.NameNotFoundException e) {
93 resources = null;
94 }
95 if (resources != null) {
96 if (iconId != 0) {
97 return getFullResIcon(resources, iconId);
98 }
99 }
100 return getFullResDefaultActivityIcon();
101 }
102
103 public Drawable getFullResIcon(ResolveInfo info) {
104 Resources resources;
105 try {
106 resources = mPackageManager.getResourcesForApplication(
Michael Jurkac9a96192010-11-01 11:52:08 -0700107 info.activityInfo.applicationInfo);
108 } catch (PackageManager.NameNotFoundException e) {
109 resources = null;
110 }
111 if (resources != null) {
112 int iconId = info.activityInfo.getIconResource();
113 if (iconId != 0) {
114 return getFullResIcon(resources, iconId);
115 }
116 }
117 return getFullResDefaultActivityIcon();
118 }
119
Romain Guya28fd3f2010-03-15 14:44:42 -0700120 private Bitmap makeDefaultIcon() {
Michael Jurkac9a96192010-11-01 11:52:08 -0700121 Drawable d = getFullResDefaultActivityIcon();
Romain Guya28fd3f2010-03-15 14:44:42 -0700122 Bitmap b = Bitmap.createBitmap(Math.max(d.getIntrinsicWidth(), 1),
123 Math.max(d.getIntrinsicHeight(), 1),
124 Bitmap.Config.ARGB_8888);
125 Canvas c = new Canvas(b);
126 d.setBounds(0, 0, b.getWidth(), b.getHeight());
127 d.draw(c);
Adam Cohenaaf473c2011-08-03 12:02:47 -0700128 c.setBitmap(null);
Romain Guya28fd3f2010-03-15 14:44:42 -0700129 return b;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800130 }
131
132 /**
133 * Remove any records for the supplied ComponentName.
134 */
135 public void remove(ComponentName componentName) {
136 synchronized (mCache) {
137 mCache.remove(componentName);
138 }
139 }
140
141 /**
142 * Empty out the cache.
143 */
144 public void flush() {
145 synchronized (mCache) {
146 mCache.clear();
147 }
148 }
149
150 /**
151 * Fill in "application" with the icon and label for "info."
152 */
Winson Chungc3eecff2011-07-11 17:44:15 -0700153 public void getTitleAndIcon(ApplicationInfo application, ResolveInfo info,
154 HashMap<Object, CharSequence> labelCache) {
Joe Onorato0589f0f2010-02-08 13:44:00 -0800155 synchronized (mCache) {
Winson Chungc3eecff2011-07-11 17:44:15 -0700156 CacheEntry entry = cacheLocked(application.componentName, info, labelCache);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800157
158 application.title = entry.title;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800159 application.iconBitmap = entry.icon;
160 }
161 }
162
163 public Bitmap getIcon(Intent intent) {
Joe Onoratofad1fb52010-05-04 12:12:41 -0700164 synchronized (mCache) {
165 final ResolveInfo resolveInfo = mPackageManager.resolveActivity(intent, 0);
166 ComponentName component = intent.getComponent();
Joe Onorato0589f0f2010-02-08 13:44:00 -0800167
Joe Onoratofad1fb52010-05-04 12:12:41 -0700168 if (resolveInfo == null || component == null) {
169 return mDefaultIcon;
170 }
171
Winson Chungc3eecff2011-07-11 17:44:15 -0700172 CacheEntry entry = cacheLocked(component, resolveInfo, null);
Joe Onoratofad1fb52010-05-04 12:12:41 -0700173 return entry.icon;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800174 }
Joe Onorato0589f0f2010-02-08 13:44:00 -0800175 }
176
Winson Chungaac01e12011-08-17 10:37:13 -0700177 public Bitmap getIcon(ComponentName component, ResolveInfo resolveInfo,
178 HashMap<Object, CharSequence> labelCache) {
Joe Onoratofad1fb52010-05-04 12:12:41 -0700179 synchronized (mCache) {
180 if (resolveInfo == null || component == null) {
181 return null;
182 }
Joe Onorato0589f0f2010-02-08 13:44:00 -0800183
Winson Chungaac01e12011-08-17 10:37:13 -0700184 CacheEntry entry = cacheLocked(component, resolveInfo, labelCache);
Joe Onoratofad1fb52010-05-04 12:12:41 -0700185 return entry.icon;
186 }
Joe Onorato0589f0f2010-02-08 13:44:00 -0800187 }
188
Joe Onoratoddc9c1f2010-08-30 18:30:15 -0700189 public boolean isDefaultIcon(Bitmap icon) {
190 return mDefaultIcon == icon;
191 }
192
Winson Chungc3eecff2011-07-11 17:44:15 -0700193 private CacheEntry cacheLocked(ComponentName componentName, ResolveInfo info,
194 HashMap<Object, CharSequence> labelCache) {
Joe Onorato0589f0f2010-02-08 13:44:00 -0800195 CacheEntry entry = mCache.get(componentName);
196 if (entry == null) {
197 entry = new CacheEntry();
198
Joe Onorato84f6a8d2010-02-12 17:53:35 -0500199 mCache.put(componentName, entry);
200
Winson Chung5308f242011-08-18 12:12:41 -0700201 ComponentName key = LauncherModel.getComponentNameFromResolveInfo(info);
202 if (labelCache != null && labelCache.containsKey(key)) {
203 entry.title = labelCache.get(key).toString();
Winson Chungc3eecff2011-07-11 17:44:15 -0700204 } else {
205 entry.title = info.loadLabel(mPackageManager).toString();
206 if (labelCache != null) {
Winson Chung5308f242011-08-18 12:12:41 -0700207 labelCache.put(key, entry.title);
Winson Chungc3eecff2011-07-11 17:44:15 -0700208 }
209 }
Joe Onorato0589f0f2010-02-08 13:44:00 -0800210 if (entry.title == null) {
211 entry.title = info.activityInfo.name;
212 }
Kenny Root20b0a5f2011-03-10 10:53:06 -0800213
Michael Jurka4842ed02011-07-07 15:33:20 -0700214 entry.icon = Utilities.createIconBitmap(
Winson Chung0b9fcf52011-10-31 13:05:15 -0700215 getFullResIcon(info), mContext);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800216 }
217 return entry;
218 }
Daniel Sandler4e1cd232011-05-12 00:06:32 -0400219
220 public HashMap<ComponentName,Bitmap> getAllIcons() {
221 synchronized (mCache) {
222 HashMap<ComponentName,Bitmap> set = new HashMap<ComponentName,Bitmap>();
Daniel Sandler4e1cd232011-05-12 00:06:32 -0400223 for (ComponentName cn : mCache.keySet()) {
224 final CacheEntry e = mCache.get(cn);
225 set.put(cn, e.icon);
226 }
227 return set;
228 }
229 }
Joe Onorato0589f0f2010-02-08 13:44:00 -0800230}