blob: 5c07cfb42ff70d452c58b0952b01cb238f35e64f [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;
43 public Bitmap titleBitmap;
44 }
45
Romain Guya28fd3f2010-03-15 14:44:42 -070046 private final Bitmap mDefaultIcon;
47 private final LauncherApplication mContext;
48 private final PackageManager mPackageManager;
49 private final Utilities.BubbleText mBubble;
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
54 public IconCache(LauncherApplication context) {
55 mContext = context;
56 mPackageManager = context.getPackageManager();
57 mBubble = new Utilities.BubbleText(context);
Michael Jurkaa2eb1702011-05-12 14:57:05 -070058 if (LauncherApplication.isScreenLarge()) {
Michael Jurkac9a96192010-11-01 11:52:08 -070059 mIconDpi = DisplayMetrics.DENSITY_HIGH;
60 } 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
Kenny Root20b0a5f2011-03-10 10:53:06 -080072 public Drawable getFullResIcon(Resources resources, int iconId)
73 throws Resources.NotFoundException {
Michael Jurkac9a96192010-11-01 11:52:08 -070074 return resources.getDrawableForDensity(iconId, mIconDpi);
75 }
76
Kenny Root20b0a5f2011-03-10 10:53:06 -080077 public Drawable getFullResIcon(ResolveInfo info, PackageManager packageManager)
78 throws Resources.NotFoundException {
Michael Jurkac9a96192010-11-01 11:52:08 -070079 Resources resources;
80 try {
81 resources = packageManager.getResourcesForApplication(
82 info.activityInfo.applicationInfo);
83 } catch (PackageManager.NameNotFoundException e) {
84 resources = null;
85 }
86 if (resources != null) {
87 int iconId = info.activityInfo.getIconResource();
88 if (iconId != 0) {
89 return getFullResIcon(resources, iconId);
90 }
91 }
92 return getFullResDefaultActivityIcon();
93 }
94
Romain Guya28fd3f2010-03-15 14:44:42 -070095 private Bitmap makeDefaultIcon() {
Michael Jurkac9a96192010-11-01 11:52:08 -070096 Drawable d = getFullResDefaultActivityIcon();
Romain Guya28fd3f2010-03-15 14:44:42 -070097 Bitmap b = Bitmap.createBitmap(Math.max(d.getIntrinsicWidth(), 1),
98 Math.max(d.getIntrinsicHeight(), 1),
99 Bitmap.Config.ARGB_8888);
100 Canvas c = new Canvas(b);
101 d.setBounds(0, 0, b.getWidth(), b.getHeight());
102 d.draw(c);
103 return b;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800104 }
105
106 /**
107 * Remove any records for the supplied ComponentName.
108 */
109 public void remove(ComponentName componentName) {
110 synchronized (mCache) {
111 mCache.remove(componentName);
112 }
113 }
114
115 /**
116 * Empty out the cache.
117 */
118 public void flush() {
119 synchronized (mCache) {
120 mCache.clear();
121 }
122 }
123
124 /**
125 * Fill in "application" with the icon and label for "info."
126 */
127 public void getTitleAndIcon(ApplicationInfo application, ResolveInfo info) {
128 synchronized (mCache) {
129 CacheEntry entry = cacheLocked(application.componentName, info);
Joe Onorato84f6a8d2010-02-12 17:53:35 -0500130 if (entry.titleBitmap == null) {
Romain Guya28fd3f2010-03-15 14:44:42 -0700131 entry.titleBitmap = mBubble.createTextBitmap(entry.title);
Joe Onorato84f6a8d2010-02-12 17:53:35 -0500132 }
Joe Onorato0589f0f2010-02-08 13:44:00 -0800133
134 application.title = entry.title;
135 application.titleBitmap = entry.titleBitmap;
136 application.iconBitmap = entry.icon;
137 }
138 }
139
140 public Bitmap getIcon(Intent intent) {
Joe Onoratofad1fb52010-05-04 12:12:41 -0700141 synchronized (mCache) {
142 final ResolveInfo resolveInfo = mPackageManager.resolveActivity(intent, 0);
143 ComponentName component = intent.getComponent();
Joe Onorato0589f0f2010-02-08 13:44:00 -0800144
Joe Onoratofad1fb52010-05-04 12:12:41 -0700145 if (resolveInfo == null || component == null) {
146 return mDefaultIcon;
147 }
148
149 CacheEntry entry = cacheLocked(component, resolveInfo);
150 return entry.icon;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800151 }
Joe Onorato0589f0f2010-02-08 13:44:00 -0800152 }
153
154 public Bitmap getIcon(ComponentName component, ResolveInfo resolveInfo) {
Joe Onoratofad1fb52010-05-04 12:12:41 -0700155 synchronized (mCache) {
156 if (resolveInfo == null || component == null) {
157 return null;
158 }
Joe Onorato0589f0f2010-02-08 13:44:00 -0800159
Joe Onoratofad1fb52010-05-04 12:12:41 -0700160 CacheEntry entry = cacheLocked(component, resolveInfo);
161 return entry.icon;
162 }
Joe Onorato0589f0f2010-02-08 13:44:00 -0800163 }
164
Joe Onoratoddc9c1f2010-08-30 18:30:15 -0700165 public boolean isDefaultIcon(Bitmap icon) {
166 return mDefaultIcon == icon;
167 }
168
Joe Onorato0589f0f2010-02-08 13:44:00 -0800169 private CacheEntry cacheLocked(ComponentName componentName, ResolveInfo info) {
170 CacheEntry entry = mCache.get(componentName);
171 if (entry == null) {
172 entry = new CacheEntry();
173
Joe Onorato84f6a8d2010-02-12 17:53:35 -0500174 mCache.put(componentName, entry);
175
Joe Onorato0589f0f2010-02-08 13:44:00 -0800176 entry.title = info.loadLabel(mPackageManager).toString();
177 if (entry.title == null) {
178 entry.title = info.activityInfo.name;
179 }
Kenny Root20b0a5f2011-03-10 10:53:06 -0800180
181 Drawable icon;
182 try {
183 icon = getFullResIcon(info, mPackageManager);
184 } catch (Resources.NotFoundException e) {
185 icon = getFullResDefaultActivityIcon();
186 }
187 entry.icon = Utilities.createIconBitmap(icon, mContext);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800188 }
189 return entry;
190 }
Daniel Sandler4e1cd232011-05-12 00:06:32 -0400191
192 public HashMap<ComponentName,Bitmap> getAllIcons() {
193 synchronized (mCache) {
194 HashMap<ComponentName,Bitmap> set = new HashMap<ComponentName,Bitmap>();
195 int i = 0;
196 for (ComponentName cn : mCache.keySet()) {
197 final CacheEntry e = mCache.get(cn);
198 set.put(cn, e.icon);
199 }
200 return set;
201 }
202 }
Joe Onorato0589f0f2010-02-08 13:44:00 -0800203}