blob: 247e164eca5b2d57648f87b84fb60b7452c86c72 [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;
Michael Jurkac9a96192010-11-01 11:52:08 -070027import 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 Jurkaef7b5da2011-08-05 19:12:37 -070054 int density = context.getResources().getDisplayMetrics().densityDpi;
Michael Jurkaa2eb1702011-05-12 14:57:05 -070055 if (LauncherApplication.isScreenLarge()) {
Michael Jurkaef7b5da2011-08-05 19:12:37 -070056 if (density == DisplayMetrics.DENSITY_LOW) {
57 mIconDpi = DisplayMetrics.DENSITY_MEDIUM;
58 } else if (density == DisplayMetrics.DENSITY_MEDIUM) {
59 mIconDpi = DisplayMetrics.DENSITY_HIGH;
60 } else if (density == DisplayMetrics.DENSITY_HIGH) {
61 mIconDpi = DisplayMetrics.DENSITY_XHIGH;
62 } else if (density == DisplayMetrics.DENSITY_XHIGH) {
63 // We'll need to use a denser icon, or some sort of a mipmap
64 mIconDpi = DisplayMetrics.DENSITY_XHIGH;
65 }
Michael Jurkac9a96192010-11-01 11:52:08 -070066 } else {
67 mIconDpi = context.getResources().getDisplayMetrics().densityDpi;
68 }
69 // need to set mIconDpi before getting default icon
Romain Guya28fd3f2010-03-15 14:44:42 -070070 mDefaultIcon = makeDefaultIcon();
71 }
72
Michael Jurkac9a96192010-11-01 11:52:08 -070073 public Drawable getFullResDefaultActivityIcon() {
74 return getFullResIcon(Resources.getSystem(),
Kenny Rootf5675de2011-01-12 10:15:46 -080075 com.android.internal.R.mipmap.sym_def_app_icon);
Michael Jurkac9a96192010-11-01 11:52:08 -070076 }
77
Michael Jurka4842ed02011-07-07 15:33:20 -070078 public Drawable getFullResIcon(Resources resources, int iconId) {
Michael Jurka721d9722011-08-03 11:49:59 -070079 Drawable d;
Michael Jurka4842ed02011-07-07 15:33:20 -070080 try {
Michael Jurka721d9722011-08-03 11:49:59 -070081 d = resources.getDrawableForDensity(iconId, mIconDpi);
Michael Jurka4842ed02011-07-07 15:33:20 -070082 } catch (Resources.NotFoundException e) {
Michael Jurka721d9722011-08-03 11:49:59 -070083 d = null;
Michael Jurka4842ed02011-07-07 15:33:20 -070084 }
Michael Jurka721d9722011-08-03 11:49:59 -070085
86 return (d != null) ? d : getFullResDefaultActivityIcon();
Michael Jurkac9a96192010-11-01 11:52:08 -070087 }
88
Michael Jurka4842ed02011-07-07 15:33:20 -070089 public Drawable getFullResIcon(ResolveInfo info, PackageManager packageManager) {
Michael Jurkac9a96192010-11-01 11:52:08 -070090 Resources resources;
91 try {
92 resources = packageManager.getResourcesForApplication(
93 info.activityInfo.applicationInfo);
94 } catch (PackageManager.NameNotFoundException e) {
95 resources = null;
96 }
97 if (resources != null) {
98 int iconId = info.activityInfo.getIconResource();
99 if (iconId != 0) {
100 return getFullResIcon(resources, iconId);
101 }
102 }
103 return getFullResDefaultActivityIcon();
104 }
105
Romain Guya28fd3f2010-03-15 14:44:42 -0700106 private Bitmap makeDefaultIcon() {
Michael Jurkac9a96192010-11-01 11:52:08 -0700107 Drawable d = getFullResDefaultActivityIcon();
Romain Guya28fd3f2010-03-15 14:44:42 -0700108 Bitmap b = Bitmap.createBitmap(Math.max(d.getIntrinsicWidth(), 1),
109 Math.max(d.getIntrinsicHeight(), 1),
110 Bitmap.Config.ARGB_8888);
111 Canvas c = new Canvas(b);
112 d.setBounds(0, 0, b.getWidth(), b.getHeight());
113 d.draw(c);
Adam Cohenaaf473c2011-08-03 12:02:47 -0700114 c.setBitmap(null);
Romain Guya28fd3f2010-03-15 14:44:42 -0700115 return b;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800116 }
117
118 /**
119 * Remove any records for the supplied ComponentName.
120 */
121 public void remove(ComponentName componentName) {
122 synchronized (mCache) {
123 mCache.remove(componentName);
124 }
125 }
126
127 /**
128 * Empty out the cache.
129 */
130 public void flush() {
131 synchronized (mCache) {
132 mCache.clear();
133 }
134 }
135
136 /**
137 * Fill in "application" with the icon and label for "info."
138 */
Winson Chungc3eecff2011-07-11 17:44:15 -0700139 public void getTitleAndIcon(ApplicationInfo application, ResolveInfo info,
140 HashMap<Object, CharSequence> labelCache) {
Joe Onorato0589f0f2010-02-08 13:44:00 -0800141 synchronized (mCache) {
Winson Chungc3eecff2011-07-11 17:44:15 -0700142 CacheEntry entry = cacheLocked(application.componentName, info, labelCache);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800143
144 application.title = entry.title;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800145 application.iconBitmap = entry.icon;
146 }
147 }
148
149 public Bitmap getIcon(Intent intent) {
Joe Onoratofad1fb52010-05-04 12:12:41 -0700150 synchronized (mCache) {
151 final ResolveInfo resolveInfo = mPackageManager.resolveActivity(intent, 0);
152 ComponentName component = intent.getComponent();
Joe Onorato0589f0f2010-02-08 13:44:00 -0800153
Joe Onoratofad1fb52010-05-04 12:12:41 -0700154 if (resolveInfo == null || component == null) {
155 return mDefaultIcon;
156 }
157
Winson Chungc3eecff2011-07-11 17:44:15 -0700158 CacheEntry entry = cacheLocked(component, resolveInfo, null);
Joe Onoratofad1fb52010-05-04 12:12:41 -0700159 return entry.icon;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800160 }
Joe Onorato0589f0f2010-02-08 13:44:00 -0800161 }
162
163 public Bitmap getIcon(ComponentName component, ResolveInfo resolveInfo) {
Joe Onoratofad1fb52010-05-04 12:12:41 -0700164 synchronized (mCache) {
165 if (resolveInfo == null || component == null) {
166 return null;
167 }
Joe Onorato0589f0f2010-02-08 13:44:00 -0800168
Winson Chungc3eecff2011-07-11 17:44:15 -0700169 CacheEntry entry = cacheLocked(component, resolveInfo, null);
Joe Onoratofad1fb52010-05-04 12:12:41 -0700170 return entry.icon;
171 }
Joe Onorato0589f0f2010-02-08 13:44:00 -0800172 }
173
Joe Onoratoddc9c1f2010-08-30 18:30:15 -0700174 public boolean isDefaultIcon(Bitmap icon) {
175 return mDefaultIcon == icon;
176 }
177
Winson Chungc3eecff2011-07-11 17:44:15 -0700178 private CacheEntry cacheLocked(ComponentName componentName, ResolveInfo info,
179 HashMap<Object, CharSequence> labelCache) {
Joe Onorato0589f0f2010-02-08 13:44:00 -0800180 CacheEntry entry = mCache.get(componentName);
181 if (entry == null) {
182 entry = new CacheEntry();
183
Joe Onorato84f6a8d2010-02-12 17:53:35 -0500184 mCache.put(componentName, entry);
185
Winson Chung5308f242011-08-18 12:12:41 -0700186 ComponentName key = LauncherModel.getComponentNameFromResolveInfo(info);
187 if (labelCache != null && labelCache.containsKey(key)) {
188 entry.title = labelCache.get(key).toString();
Winson Chungc3eecff2011-07-11 17:44:15 -0700189 } else {
190 entry.title = info.loadLabel(mPackageManager).toString();
191 if (labelCache != null) {
Winson Chung5308f242011-08-18 12:12:41 -0700192 labelCache.put(key, entry.title);
Winson Chungc3eecff2011-07-11 17:44:15 -0700193 }
194 }
Joe Onorato0589f0f2010-02-08 13:44:00 -0800195 if (entry.title == null) {
196 entry.title = info.activityInfo.name;
197 }
Kenny Root20b0a5f2011-03-10 10:53:06 -0800198
Michael Jurka4842ed02011-07-07 15:33:20 -0700199 entry.icon = Utilities.createIconBitmap(
200 getFullResIcon(info, mPackageManager), mContext);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800201 }
202 return entry;
203 }
Daniel Sandler4e1cd232011-05-12 00:06:32 -0400204
205 public HashMap<ComponentName,Bitmap> getAllIcons() {
206 synchronized (mCache) {
207 HashMap<ComponentName,Bitmap> set = new HashMap<ComponentName,Bitmap>();
208 int i = 0;
209 for (ComponentName cn : mCache.keySet()) {
210 final CacheEntry e = mCache.get(cn);
211 set.put(cn, e.icon);
212 }
213 return set;
214 }
215 }
Joe Onorato0589f0f2010-02-08 13:44:00 -0800216}