Joe Onorato | 0589f0f | 2010-02-08 13:44:00 -0800 | [diff] [blame] | 1 | /* |
| 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 Sandler | 325dc23 | 2013-06-05 22:57:57 -0400 | [diff] [blame^] | 17 | package com.android.launcher3; |
Joe Onorato | 0589f0f | 2010-02-08 13:44:00 -0800 | [diff] [blame] | 18 | |
Winson Chung | d83f5f4 | 2012-02-13 14:27:42 -0800 | [diff] [blame] | 19 | import android.app.ActivityManager; |
Joe Onorato | 0589f0f | 2010-02-08 13:44:00 -0800 | [diff] [blame] | 20 | import android.content.ComponentName; |
Winson Chung | d83f5f4 | 2012-02-13 14:27:42 -0800 | [diff] [blame] | 21 | import android.content.Context; |
Joe Onorato | 0589f0f | 2010-02-08 13:44:00 -0800 | [diff] [blame] | 22 | import android.content.Intent; |
Michael Jurka | dac8591 | 2012-05-18 15:04:49 -0700 | [diff] [blame] | 23 | import android.content.pm.ActivityInfo; |
Joe Onorato | 0589f0f | 2010-02-08 13:44:00 -0800 | [diff] [blame] | 24 | import android.content.pm.PackageManager; |
| 25 | import android.content.pm.ResolveInfo; |
Michael Jurka | c9a9619 | 2010-11-01 11:52:08 -0700 | [diff] [blame] | 26 | import android.content.res.Resources; |
Joe Onorato | 0589f0f | 2010-02-08 13:44:00 -0800 | [diff] [blame] | 27 | import android.graphics.Bitmap; |
Romain Guy | a28fd3f | 2010-03-15 14:44:42 -0700 | [diff] [blame] | 28 | import android.graphics.Canvas; |
Joe Onorato | 0589f0f | 2010-02-08 13:44:00 -0800 | [diff] [blame] | 29 | import android.graphics.drawable.Drawable; |
Joe Onorato | 0589f0f | 2010-02-08 13:44:00 -0800 | [diff] [blame] | 30 | |
Joe Onorato | 0589f0f | 2010-02-08 13:44:00 -0800 | [diff] [blame] | 31 | import java.util.HashMap; |
Joe Onorato | 0589f0f | 2010-02-08 13:44:00 -0800 | [diff] [blame] | 32 | |
| 33 | /** |
| 34 | * Cache of application icons. Icons can be made from any thread. |
| 35 | */ |
| 36 | public class IconCache { |
Michael Jurka | 3a9fced | 2012-04-13 14:44:29 -0700 | [diff] [blame] | 37 | @SuppressWarnings("unused") |
Joe Onorato | 0589f0f | 2010-02-08 13:44:00 -0800 | [diff] [blame] | 38 | 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 Onorato | 0589f0f | 2010-02-08 13:44:00 -0800 | [diff] [blame] | 45 | } |
| 46 | |
Romain Guy | a28fd3f | 2010-03-15 14:44:42 -0700 | [diff] [blame] | 47 | private final Bitmap mDefaultIcon; |
| 48 | private final LauncherApplication mContext; |
| 49 | private final PackageManager mPackageManager; |
Joe Onorato | 0589f0f | 2010-02-08 13:44:00 -0800 | [diff] [blame] | 50 | private final HashMap<ComponentName, CacheEntry> mCache = |
| 51 | new HashMap<ComponentName, CacheEntry>(INITIAL_ICON_CACHE_CAPACITY); |
Michael Jurka | c9a9619 | 2010-11-01 11:52:08 -0700 | [diff] [blame] | 52 | private int mIconDpi; |
Joe Onorato | 0589f0f | 2010-02-08 13:44:00 -0800 | [diff] [blame] | 53 | |
| 54 | public IconCache(LauncherApplication context) { |
Winson Chung | d83f5f4 | 2012-02-13 14:27:42 -0800 | [diff] [blame] | 55 | ActivityManager activityManager = |
| 56 | (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE); |
| 57 | |
Joe Onorato | 0589f0f | 2010-02-08 13:44:00 -0800 | [diff] [blame] | 58 | mContext = context; |
| 59 | mPackageManager = context.getPackageManager(); |
Winson Chung | d83f5f4 | 2012-02-13 14:27:42 -0800 | [diff] [blame] | 60 | mIconDpi = activityManager.getLauncherLargeIconDensity(); |
| 61 | |
Michael Jurka | c9a9619 | 2010-11-01 11:52:08 -0700 | [diff] [blame] | 62 | // need to set mIconDpi before getting default icon |
Romain Guy | a28fd3f | 2010-03-15 14:44:42 -0700 | [diff] [blame] | 63 | mDefaultIcon = makeDefaultIcon(); |
| 64 | } |
| 65 | |
Michael Jurka | c9a9619 | 2010-11-01 11:52:08 -0700 | [diff] [blame] | 66 | public Drawable getFullResDefaultActivityIcon() { |
| 67 | return getFullResIcon(Resources.getSystem(), |
Michael Jurka | 8b805b1 | 2012-04-18 14:23:14 -0700 | [diff] [blame] | 68 | android.R.mipmap.sym_def_app_icon); |
Michael Jurka | c9a9619 | 2010-11-01 11:52:08 -0700 | [diff] [blame] | 69 | } |
| 70 | |
Michael Jurka | 4842ed0 | 2011-07-07 15:33:20 -0700 | [diff] [blame] | 71 | public Drawable getFullResIcon(Resources resources, int iconId) { |
Michael Jurka | 721d972 | 2011-08-03 11:49:59 -0700 | [diff] [blame] | 72 | Drawable d; |
Michael Jurka | 4842ed0 | 2011-07-07 15:33:20 -0700 | [diff] [blame] | 73 | try { |
Michael Jurka | 721d972 | 2011-08-03 11:49:59 -0700 | [diff] [blame] | 74 | d = resources.getDrawableForDensity(iconId, mIconDpi); |
Michael Jurka | 4842ed0 | 2011-07-07 15:33:20 -0700 | [diff] [blame] | 75 | } catch (Resources.NotFoundException e) { |
Michael Jurka | 721d972 | 2011-08-03 11:49:59 -0700 | [diff] [blame] | 76 | d = null; |
Michael Jurka | 4842ed0 | 2011-07-07 15:33:20 -0700 | [diff] [blame] | 77 | } |
Michael Jurka | 721d972 | 2011-08-03 11:49:59 -0700 | [diff] [blame] | 78 | |
| 79 | return (d != null) ? d : getFullResDefaultActivityIcon(); |
Michael Jurka | c9a9619 | 2010-11-01 11:52:08 -0700 | [diff] [blame] | 80 | } |
| 81 | |
Winson Chung | 0b9fcf5 | 2011-10-31 13:05:15 -0700 | [diff] [blame] | 82 | public Drawable getFullResIcon(String packageName, int iconId) { |
Michael Jurka | c9a9619 | 2010-11-01 11:52:08 -0700 | [diff] [blame] | 83 | Resources resources; |
| 84 | try { |
Winson Chung | 0b9fcf5 | 2011-10-31 13:05:15 -0700 | [diff] [blame] | 85 | 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 Jurka | dac8591 | 2012-05-18 15:04:49 -0700 | [diff] [blame] | 98 | return getFullResIcon(info.activityInfo); |
| 99 | } |
| 100 | |
| 101 | public Drawable getFullResIcon(ActivityInfo info) { |
| 102 | |
Winson Chung | 0b9fcf5 | 2011-10-31 13:05:15 -0700 | [diff] [blame] | 103 | Resources resources; |
| 104 | try { |
| 105 | resources = mPackageManager.getResourcesForApplication( |
Michael Jurka | dac8591 | 2012-05-18 15:04:49 -0700 | [diff] [blame] | 106 | info.applicationInfo); |
Michael Jurka | c9a9619 | 2010-11-01 11:52:08 -0700 | [diff] [blame] | 107 | } catch (PackageManager.NameNotFoundException e) { |
| 108 | resources = null; |
| 109 | } |
| 110 | if (resources != null) { |
Michael Jurka | dac8591 | 2012-05-18 15:04:49 -0700 | [diff] [blame] | 111 | int iconId = info.getIconResource(); |
Michael Jurka | c9a9619 | 2010-11-01 11:52:08 -0700 | [diff] [blame] | 112 | if (iconId != 0) { |
| 113 | return getFullResIcon(resources, iconId); |
| 114 | } |
| 115 | } |
| 116 | return getFullResDefaultActivityIcon(); |
| 117 | } |
| 118 | |
Romain Guy | a28fd3f | 2010-03-15 14:44:42 -0700 | [diff] [blame] | 119 | private Bitmap makeDefaultIcon() { |
Michael Jurka | c9a9619 | 2010-11-01 11:52:08 -0700 | [diff] [blame] | 120 | Drawable d = getFullResDefaultActivityIcon(); |
Romain Guy | a28fd3f | 2010-03-15 14:44:42 -0700 | [diff] [blame] | 121 | 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 Cohen | aaf473c | 2011-08-03 12:02:47 -0700 | [diff] [blame] | 127 | c.setBitmap(null); |
Romain Guy | a28fd3f | 2010-03-15 14:44:42 -0700 | [diff] [blame] | 128 | return b; |
Joe Onorato | 0589f0f | 2010-02-08 13:44:00 -0800 | [diff] [blame] | 129 | } |
| 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 | /** |
| 150 | * Fill in "application" with the icon and label for "info." |
| 151 | */ |
Winson Chung | c3eecff | 2011-07-11 17:44:15 -0700 | [diff] [blame] | 152 | public void getTitleAndIcon(ApplicationInfo application, ResolveInfo info, |
| 153 | HashMap<Object, CharSequence> labelCache) { |
Joe Onorato | 0589f0f | 2010-02-08 13:44:00 -0800 | [diff] [blame] | 154 | synchronized (mCache) { |
Winson Chung | c3eecff | 2011-07-11 17:44:15 -0700 | [diff] [blame] | 155 | CacheEntry entry = cacheLocked(application.componentName, info, labelCache); |
Joe Onorato | 0589f0f | 2010-02-08 13:44:00 -0800 | [diff] [blame] | 156 | |
| 157 | application.title = entry.title; |
Joe Onorato | 0589f0f | 2010-02-08 13:44:00 -0800 | [diff] [blame] | 158 | application.iconBitmap = entry.icon; |
| 159 | } |
| 160 | } |
| 161 | |
| 162 | public Bitmap getIcon(Intent intent) { |
Joe Onorato | fad1fb5 | 2010-05-04 12:12:41 -0700 | [diff] [blame] | 163 | synchronized (mCache) { |
| 164 | final ResolveInfo resolveInfo = mPackageManager.resolveActivity(intent, 0); |
| 165 | ComponentName component = intent.getComponent(); |
Joe Onorato | 0589f0f | 2010-02-08 13:44:00 -0800 | [diff] [blame] | 166 | |
Joe Onorato | fad1fb5 | 2010-05-04 12:12:41 -0700 | [diff] [blame] | 167 | if (resolveInfo == null || component == null) { |
| 168 | return mDefaultIcon; |
| 169 | } |
| 170 | |
Winson Chung | c3eecff | 2011-07-11 17:44:15 -0700 | [diff] [blame] | 171 | CacheEntry entry = cacheLocked(component, resolveInfo, null); |
Joe Onorato | fad1fb5 | 2010-05-04 12:12:41 -0700 | [diff] [blame] | 172 | return entry.icon; |
Joe Onorato | 0589f0f | 2010-02-08 13:44:00 -0800 | [diff] [blame] | 173 | } |
Joe Onorato | 0589f0f | 2010-02-08 13:44:00 -0800 | [diff] [blame] | 174 | } |
| 175 | |
Winson Chung | aac01e1 | 2011-08-17 10:37:13 -0700 | [diff] [blame] | 176 | public Bitmap getIcon(ComponentName component, ResolveInfo resolveInfo, |
| 177 | HashMap<Object, CharSequence> labelCache) { |
Joe Onorato | fad1fb5 | 2010-05-04 12:12:41 -0700 | [diff] [blame] | 178 | synchronized (mCache) { |
| 179 | if (resolveInfo == null || component == null) { |
| 180 | return null; |
| 181 | } |
Joe Onorato | 0589f0f | 2010-02-08 13:44:00 -0800 | [diff] [blame] | 182 | |
Winson Chung | aac01e1 | 2011-08-17 10:37:13 -0700 | [diff] [blame] | 183 | CacheEntry entry = cacheLocked(component, resolveInfo, labelCache); |
Joe Onorato | fad1fb5 | 2010-05-04 12:12:41 -0700 | [diff] [blame] | 184 | return entry.icon; |
| 185 | } |
Joe Onorato | 0589f0f | 2010-02-08 13:44:00 -0800 | [diff] [blame] | 186 | } |
| 187 | |
Joe Onorato | ddc9c1f | 2010-08-30 18:30:15 -0700 | [diff] [blame] | 188 | public boolean isDefaultIcon(Bitmap icon) { |
| 189 | return mDefaultIcon == icon; |
| 190 | } |
| 191 | |
Winson Chung | c3eecff | 2011-07-11 17:44:15 -0700 | [diff] [blame] | 192 | private CacheEntry cacheLocked(ComponentName componentName, ResolveInfo info, |
| 193 | HashMap<Object, CharSequence> labelCache) { |
Joe Onorato | 0589f0f | 2010-02-08 13:44:00 -0800 | [diff] [blame] | 194 | CacheEntry entry = mCache.get(componentName); |
| 195 | if (entry == null) { |
| 196 | entry = new CacheEntry(); |
| 197 | |
Joe Onorato | 84f6a8d | 2010-02-12 17:53:35 -0500 | [diff] [blame] | 198 | mCache.put(componentName, entry); |
| 199 | |
Winson Chung | 5308f24 | 2011-08-18 12:12:41 -0700 | [diff] [blame] | 200 | ComponentName key = LauncherModel.getComponentNameFromResolveInfo(info); |
| 201 | if (labelCache != null && labelCache.containsKey(key)) { |
| 202 | entry.title = labelCache.get(key).toString(); |
Winson Chung | c3eecff | 2011-07-11 17:44:15 -0700 | [diff] [blame] | 203 | } else { |
| 204 | entry.title = info.loadLabel(mPackageManager).toString(); |
| 205 | if (labelCache != null) { |
Winson Chung | 5308f24 | 2011-08-18 12:12:41 -0700 | [diff] [blame] | 206 | labelCache.put(key, entry.title); |
Winson Chung | c3eecff | 2011-07-11 17:44:15 -0700 | [diff] [blame] | 207 | } |
| 208 | } |
Joe Onorato | 0589f0f | 2010-02-08 13:44:00 -0800 | [diff] [blame] | 209 | if (entry.title == null) { |
| 210 | entry.title = info.activityInfo.name; |
| 211 | } |
Kenny Root | 20b0a5f | 2011-03-10 10:53:06 -0800 | [diff] [blame] | 212 | |
Michael Jurka | 4842ed0 | 2011-07-07 15:33:20 -0700 | [diff] [blame] | 213 | entry.icon = Utilities.createIconBitmap( |
Winson Chung | 0b9fcf5 | 2011-10-31 13:05:15 -0700 | [diff] [blame] | 214 | getFullResIcon(info), mContext); |
Joe Onorato | 0589f0f | 2010-02-08 13:44:00 -0800 | [diff] [blame] | 215 | } |
| 216 | return entry; |
| 217 | } |
Daniel Sandler | 4e1cd23 | 2011-05-12 00:06:32 -0400 | [diff] [blame] | 218 | |
| 219 | public HashMap<ComponentName,Bitmap> getAllIcons() { |
| 220 | synchronized (mCache) { |
| 221 | HashMap<ComponentName,Bitmap> set = new HashMap<ComponentName,Bitmap>(); |
Daniel Sandler | 4e1cd23 | 2011-05-12 00:06:32 -0400 | [diff] [blame] | 222 | for (ComponentName cn : mCache.keySet()) { |
| 223 | final CacheEntry e = mCache.get(cn); |
| 224 | set.put(cn, e.icon); |
| 225 | } |
| 226 | return set; |
| 227 | } |
| 228 | } |
Joe Onorato | 0589f0f | 2010-02-08 13:44:00 -0800 | [diff] [blame] | 229 | } |