blob: 04f97f3990053c275abf04f6959078733979e637 [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;
Daniel Sandler4e1cd232011-05-12 00:06:32 -040027import android.util.Pair;
Michael Jurkac9a96192010-11-01 11:52:08 -070028import android.util.DisplayMetrics;
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;
48 private final Utilities.BubbleText mBubble;
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) {
54 mContext = context;
55 mPackageManager = context.getPackageManager();
56 mBubble = new Utilities.BubbleText(context);
Michael Jurkaa2eb1702011-05-12 14:57:05 -070057 if (LauncherApplication.isScreenLarge()) {
Michael Jurkac9a96192010-11-01 11:52:08 -070058 mIconDpi = DisplayMetrics.DENSITY_HIGH;
59 } else {
60 mIconDpi = context.getResources().getDisplayMetrics().densityDpi;
61 }
62 // 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(),
Kenny Rootf5675de2011-01-12 10:15:46 -080068 com.android.internal.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
Michael Jurka4842ed02011-07-07 15:33:20 -070082 public Drawable getFullResIcon(ResolveInfo info, PackageManager packageManager) {
Michael Jurkac9a96192010-11-01 11:52:08 -070083 Resources resources;
84 try {
85 resources = packageManager.getResourcesForApplication(
86 info.activityInfo.applicationInfo);
87 } catch (PackageManager.NameNotFoundException e) {
88 resources = null;
89 }
90 if (resources != null) {
91 int iconId = info.activityInfo.getIconResource();
92 if (iconId != 0) {
93 return getFullResIcon(resources, iconId);
94 }
95 }
96 return getFullResDefaultActivityIcon();
97 }
98
Romain Guya28fd3f2010-03-15 14:44:42 -070099 private Bitmap makeDefaultIcon() {
Michael Jurkac9a96192010-11-01 11:52:08 -0700100 Drawable d = getFullResDefaultActivityIcon();
Romain Guya28fd3f2010-03-15 14:44:42 -0700101 Bitmap b = Bitmap.createBitmap(Math.max(d.getIntrinsicWidth(), 1),
102 Math.max(d.getIntrinsicHeight(), 1),
103 Bitmap.Config.ARGB_8888);
104 Canvas c = new Canvas(b);
105 d.setBounds(0, 0, b.getWidth(), b.getHeight());
106 d.draw(c);
Adam Cohenaaf473c2011-08-03 12:02:47 -0700107 c.setBitmap(null);
Romain Guya28fd3f2010-03-15 14:44:42 -0700108 return b;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800109 }
110
111 /**
112 * Remove any records for the supplied ComponentName.
113 */
114 public void remove(ComponentName componentName) {
115 synchronized (mCache) {
116 mCache.remove(componentName);
117 }
118 }
119
120 /**
121 * Empty out the cache.
122 */
123 public void flush() {
124 synchronized (mCache) {
125 mCache.clear();
126 }
127 }
128
129 /**
130 * Fill in "application" with the icon and label for "info."
131 */
Winson Chungc3eecff2011-07-11 17:44:15 -0700132 public void getTitleAndIcon(ApplicationInfo application, ResolveInfo info,
133 HashMap<Object, CharSequence> labelCache) {
Joe Onorato0589f0f2010-02-08 13:44:00 -0800134 synchronized (mCache) {
Winson Chungc3eecff2011-07-11 17:44:15 -0700135 CacheEntry entry = cacheLocked(application.componentName, info, labelCache);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800136
137 application.title = entry.title;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800138 application.iconBitmap = entry.icon;
139 }
140 }
141
142 public Bitmap getIcon(Intent intent) {
Joe Onoratofad1fb52010-05-04 12:12:41 -0700143 synchronized (mCache) {
144 final ResolveInfo resolveInfo = mPackageManager.resolveActivity(intent, 0);
145 ComponentName component = intent.getComponent();
Joe Onorato0589f0f2010-02-08 13:44:00 -0800146
Joe Onoratofad1fb52010-05-04 12:12:41 -0700147 if (resolveInfo == null || component == null) {
148 return mDefaultIcon;
149 }
150
Winson Chungc3eecff2011-07-11 17:44:15 -0700151 CacheEntry entry = cacheLocked(component, resolveInfo, null);
Joe Onoratofad1fb52010-05-04 12:12:41 -0700152 return entry.icon;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800153 }
Joe Onorato0589f0f2010-02-08 13:44:00 -0800154 }
155
156 public Bitmap getIcon(ComponentName component, ResolveInfo resolveInfo) {
Joe Onoratofad1fb52010-05-04 12:12:41 -0700157 synchronized (mCache) {
158 if (resolveInfo == null || component == null) {
159 return null;
160 }
Joe Onorato0589f0f2010-02-08 13:44:00 -0800161
Winson Chungc3eecff2011-07-11 17:44:15 -0700162 CacheEntry entry = cacheLocked(component, resolveInfo, null);
Joe Onoratofad1fb52010-05-04 12:12:41 -0700163 return entry.icon;
164 }
Joe Onorato0589f0f2010-02-08 13:44:00 -0800165 }
166
Joe Onoratoddc9c1f2010-08-30 18:30:15 -0700167 public boolean isDefaultIcon(Bitmap icon) {
168 return mDefaultIcon == icon;
169 }
170
Winson Chungc3eecff2011-07-11 17:44:15 -0700171 private CacheEntry cacheLocked(ComponentName componentName, ResolveInfo info,
172 HashMap<Object, CharSequence> labelCache) {
Joe Onorato0589f0f2010-02-08 13:44:00 -0800173 CacheEntry entry = mCache.get(componentName);
174 if (entry == null) {
175 entry = new CacheEntry();
176
Joe Onorato84f6a8d2010-02-12 17:53:35 -0500177 mCache.put(componentName, entry);
178
Winson Chungc3eecff2011-07-11 17:44:15 -0700179 if (labelCache != null && labelCache.containsKey(info)) {
180 entry.title = labelCache.get(info).toString();
181 } else {
182 entry.title = info.loadLabel(mPackageManager).toString();
183 if (labelCache != null) {
184 labelCache.put(info, entry.title);
185 }
186 }
Joe Onorato0589f0f2010-02-08 13:44:00 -0800187 if (entry.title == null) {
188 entry.title = info.activityInfo.name;
189 }
Kenny Root20b0a5f2011-03-10 10:53:06 -0800190
Michael Jurka4842ed02011-07-07 15:33:20 -0700191 entry.icon = Utilities.createIconBitmap(
192 getFullResIcon(info, mPackageManager), mContext);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800193 }
194 return entry;
195 }
Daniel Sandler4e1cd232011-05-12 00:06:32 -0400196
197 public HashMap<ComponentName,Bitmap> getAllIcons() {
198 synchronized (mCache) {
199 HashMap<ComponentName,Bitmap> set = new HashMap<ComponentName,Bitmap>();
200 int i = 0;
201 for (ComponentName cn : mCache.keySet()) {
202 final CacheEntry e = mCache.get(cn);
203 set.put(cn, e.icon);
204 }
205 return set;
206 }
207 }
Joe Onorato0589f0f2010-02-08 13:44:00 -0800208}