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 | |
| 17 | package com.android.launcher2; |
| 18 | |
| 19 | import android.content.ComponentName; |
Joe Onorato | 0589f0f | 2010-02-08 13:44:00 -0800 | [diff] [blame] | 20 | import android.content.Intent; |
Joe Onorato | 0589f0f | 2010-02-08 13:44:00 -0800 | [diff] [blame] | 21 | import android.content.pm.PackageManager; |
| 22 | import android.content.pm.ResolveInfo; |
Michael Jurka | c9a9619 | 2010-11-01 11:52:08 -0700 | [diff] [blame] | 23 | import android.content.res.Resources; |
Joe Onorato | 0589f0f | 2010-02-08 13:44:00 -0800 | [diff] [blame] | 24 | import android.graphics.Bitmap; |
Romain Guy | a28fd3f | 2010-03-15 14:44:42 -0700 | [diff] [blame] | 25 | import android.graphics.Canvas; |
Joe Onorato | 0589f0f | 2010-02-08 13:44:00 -0800 | [diff] [blame] | 26 | import android.graphics.drawable.Drawable; |
Michael Jurka | c9a9619 | 2010-11-01 11:52:08 -0700 | [diff] [blame] | 27 | import android.util.DisplayMetrics; |
Joe Onorato | 0589f0f | 2010-02-08 13:44:00 -0800 | [diff] [blame] | 28 | |
Joe Onorato | 0589f0f | 2010-02-08 13:44:00 -0800 | [diff] [blame] | 29 | import java.util.HashMap; |
Joe Onorato | 0589f0f | 2010-02-08 13:44:00 -0800 | [diff] [blame] | 30 | |
| 31 | /** |
| 32 | * Cache of application icons. Icons can be made from any thread. |
| 33 | */ |
| 34 | public 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 Onorato | 0589f0f | 2010-02-08 13:44:00 -0800 | [diff] [blame] | 42 | } |
| 43 | |
Romain Guy | a28fd3f | 2010-03-15 14:44:42 -0700 | [diff] [blame] | 44 | private final Bitmap mDefaultIcon; |
| 45 | private final LauncherApplication mContext; |
| 46 | private final PackageManager mPackageManager; |
| 47 | private final Utilities.BubbleText mBubble; |
Joe Onorato | 0589f0f | 2010-02-08 13:44:00 -0800 | [diff] [blame] | 48 | private final HashMap<ComponentName, CacheEntry> mCache = |
| 49 | new HashMap<ComponentName, CacheEntry>(INITIAL_ICON_CACHE_CAPACITY); |
Michael Jurka | c9a9619 | 2010-11-01 11:52:08 -0700 | [diff] [blame] | 50 | private int mIconDpi; |
Joe Onorato | 0589f0f | 2010-02-08 13:44:00 -0800 | [diff] [blame] | 51 | |
| 52 | public IconCache(LauncherApplication context) { |
| 53 | mContext = context; |
| 54 | mPackageManager = context.getPackageManager(); |
| 55 | mBubble = new Utilities.BubbleText(context); |
Michael Jurka | ef7b5da | 2011-08-05 19:12:37 -0700 | [diff] [blame^] | 56 | int density = context.getResources().getDisplayMetrics().densityDpi; |
Michael Jurka | a2eb170 | 2011-05-12 14:57:05 -0700 | [diff] [blame] | 57 | if (LauncherApplication.isScreenLarge()) { |
Michael Jurka | ef7b5da | 2011-08-05 19:12:37 -0700 | [diff] [blame^] | 58 | if (density == DisplayMetrics.DENSITY_LOW) { |
| 59 | mIconDpi = DisplayMetrics.DENSITY_MEDIUM; |
| 60 | } else if (density == DisplayMetrics.DENSITY_MEDIUM) { |
| 61 | mIconDpi = DisplayMetrics.DENSITY_HIGH; |
| 62 | } else if (density == DisplayMetrics.DENSITY_HIGH) { |
| 63 | mIconDpi = DisplayMetrics.DENSITY_XHIGH; |
| 64 | } else if (density == DisplayMetrics.DENSITY_XHIGH) { |
| 65 | // We'll need to use a denser icon, or some sort of a mipmap |
| 66 | mIconDpi = DisplayMetrics.DENSITY_XHIGH; |
| 67 | } |
Michael Jurka | c9a9619 | 2010-11-01 11:52:08 -0700 | [diff] [blame] | 68 | } else { |
| 69 | mIconDpi = context.getResources().getDisplayMetrics().densityDpi; |
| 70 | } |
| 71 | // need to set mIconDpi before getting default icon |
Romain Guy | a28fd3f | 2010-03-15 14:44:42 -0700 | [diff] [blame] | 72 | mDefaultIcon = makeDefaultIcon(); |
| 73 | } |
| 74 | |
Michael Jurka | c9a9619 | 2010-11-01 11:52:08 -0700 | [diff] [blame] | 75 | public Drawable getFullResDefaultActivityIcon() { |
| 76 | return getFullResIcon(Resources.getSystem(), |
Kenny Root | f5675de | 2011-01-12 10:15:46 -0800 | [diff] [blame] | 77 | com.android.internal.R.mipmap.sym_def_app_icon); |
Michael Jurka | c9a9619 | 2010-11-01 11:52:08 -0700 | [diff] [blame] | 78 | } |
| 79 | |
Michael Jurka | 4842ed0 | 2011-07-07 15:33:20 -0700 | [diff] [blame] | 80 | public Drawable getFullResIcon(Resources resources, int iconId) { |
Michael Jurka | 721d972 | 2011-08-03 11:49:59 -0700 | [diff] [blame] | 81 | Drawable d; |
Michael Jurka | 4842ed0 | 2011-07-07 15:33:20 -0700 | [diff] [blame] | 82 | try { |
Michael Jurka | 721d972 | 2011-08-03 11:49:59 -0700 | [diff] [blame] | 83 | d = resources.getDrawableForDensity(iconId, mIconDpi); |
Michael Jurka | 4842ed0 | 2011-07-07 15:33:20 -0700 | [diff] [blame] | 84 | } catch (Resources.NotFoundException e) { |
Michael Jurka | 721d972 | 2011-08-03 11:49:59 -0700 | [diff] [blame] | 85 | d = null; |
Michael Jurka | 4842ed0 | 2011-07-07 15:33:20 -0700 | [diff] [blame] | 86 | } |
Michael Jurka | 721d972 | 2011-08-03 11:49:59 -0700 | [diff] [blame] | 87 | |
| 88 | return (d != null) ? d : getFullResDefaultActivityIcon(); |
Michael Jurka | c9a9619 | 2010-11-01 11:52:08 -0700 | [diff] [blame] | 89 | } |
| 90 | |
Michael Jurka | 4842ed0 | 2011-07-07 15:33:20 -0700 | [diff] [blame] | 91 | public Drawable getFullResIcon(ResolveInfo info, PackageManager packageManager) { |
Michael Jurka | c9a9619 | 2010-11-01 11:52:08 -0700 | [diff] [blame] | 92 | Resources resources; |
| 93 | try { |
| 94 | resources = packageManager.getResourcesForApplication( |
| 95 | info.activityInfo.applicationInfo); |
| 96 | } catch (PackageManager.NameNotFoundException e) { |
| 97 | resources = null; |
| 98 | } |
| 99 | if (resources != null) { |
| 100 | int iconId = info.activityInfo.getIconResource(); |
| 101 | if (iconId != 0) { |
| 102 | return getFullResIcon(resources, iconId); |
| 103 | } |
| 104 | } |
| 105 | return getFullResDefaultActivityIcon(); |
| 106 | } |
| 107 | |
Romain Guy | a28fd3f | 2010-03-15 14:44:42 -0700 | [diff] [blame] | 108 | private Bitmap makeDefaultIcon() { |
Michael Jurka | c9a9619 | 2010-11-01 11:52:08 -0700 | [diff] [blame] | 109 | Drawable d = getFullResDefaultActivityIcon(); |
Romain Guy | a28fd3f | 2010-03-15 14:44:42 -0700 | [diff] [blame] | 110 | Bitmap b = Bitmap.createBitmap(Math.max(d.getIntrinsicWidth(), 1), |
| 111 | Math.max(d.getIntrinsicHeight(), 1), |
| 112 | Bitmap.Config.ARGB_8888); |
| 113 | Canvas c = new Canvas(b); |
| 114 | d.setBounds(0, 0, b.getWidth(), b.getHeight()); |
| 115 | d.draw(c); |
Adam Cohen | aaf473c | 2011-08-03 12:02:47 -0700 | [diff] [blame] | 116 | c.setBitmap(null); |
Romain Guy | a28fd3f | 2010-03-15 14:44:42 -0700 | [diff] [blame] | 117 | return b; |
Joe Onorato | 0589f0f | 2010-02-08 13:44:00 -0800 | [diff] [blame] | 118 | } |
| 119 | |
| 120 | /** |
| 121 | * Remove any records for the supplied ComponentName. |
| 122 | */ |
| 123 | public void remove(ComponentName componentName) { |
| 124 | synchronized (mCache) { |
| 125 | mCache.remove(componentName); |
| 126 | } |
| 127 | } |
| 128 | |
| 129 | /** |
| 130 | * Empty out the cache. |
| 131 | */ |
| 132 | public void flush() { |
| 133 | synchronized (mCache) { |
| 134 | mCache.clear(); |
| 135 | } |
| 136 | } |
| 137 | |
| 138 | /** |
| 139 | * Fill in "application" with the icon and label for "info." |
| 140 | */ |
Winson Chung | c3eecff | 2011-07-11 17:44:15 -0700 | [diff] [blame] | 141 | public void getTitleAndIcon(ApplicationInfo application, ResolveInfo info, |
| 142 | HashMap<Object, CharSequence> labelCache) { |
Joe Onorato | 0589f0f | 2010-02-08 13:44:00 -0800 | [diff] [blame] | 143 | synchronized (mCache) { |
Winson Chung | c3eecff | 2011-07-11 17:44:15 -0700 | [diff] [blame] | 144 | CacheEntry entry = cacheLocked(application.componentName, info, labelCache); |
Joe Onorato | 0589f0f | 2010-02-08 13:44:00 -0800 | [diff] [blame] | 145 | |
| 146 | application.title = entry.title; |
Joe Onorato | 0589f0f | 2010-02-08 13:44:00 -0800 | [diff] [blame] | 147 | application.iconBitmap = entry.icon; |
| 148 | } |
| 149 | } |
| 150 | |
| 151 | public Bitmap getIcon(Intent intent) { |
Joe Onorato | fad1fb5 | 2010-05-04 12:12:41 -0700 | [diff] [blame] | 152 | synchronized (mCache) { |
| 153 | final ResolveInfo resolveInfo = mPackageManager.resolveActivity(intent, 0); |
| 154 | ComponentName component = intent.getComponent(); |
Joe Onorato | 0589f0f | 2010-02-08 13:44:00 -0800 | [diff] [blame] | 155 | |
Joe Onorato | fad1fb5 | 2010-05-04 12:12:41 -0700 | [diff] [blame] | 156 | if (resolveInfo == null || component == null) { |
| 157 | return mDefaultIcon; |
| 158 | } |
| 159 | |
Winson Chung | c3eecff | 2011-07-11 17:44:15 -0700 | [diff] [blame] | 160 | CacheEntry entry = cacheLocked(component, resolveInfo, null); |
Joe Onorato | fad1fb5 | 2010-05-04 12:12:41 -0700 | [diff] [blame] | 161 | return entry.icon; |
Joe Onorato | 0589f0f | 2010-02-08 13:44:00 -0800 | [diff] [blame] | 162 | } |
Joe Onorato | 0589f0f | 2010-02-08 13:44:00 -0800 | [diff] [blame] | 163 | } |
| 164 | |
| 165 | public Bitmap getIcon(ComponentName component, ResolveInfo resolveInfo) { |
Joe Onorato | fad1fb5 | 2010-05-04 12:12:41 -0700 | [diff] [blame] | 166 | synchronized (mCache) { |
| 167 | if (resolveInfo == null || component == null) { |
| 168 | return null; |
| 169 | } |
Joe Onorato | 0589f0f | 2010-02-08 13:44:00 -0800 | [diff] [blame] | 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; |
| 173 | } |
Joe Onorato | 0589f0f | 2010-02-08 13:44:00 -0800 | [diff] [blame] | 174 | } |
| 175 | |
Joe Onorato | ddc9c1f | 2010-08-30 18:30:15 -0700 | [diff] [blame] | 176 | public boolean isDefaultIcon(Bitmap icon) { |
| 177 | return mDefaultIcon == icon; |
| 178 | } |
| 179 | |
Winson Chung | c3eecff | 2011-07-11 17:44:15 -0700 | [diff] [blame] | 180 | private CacheEntry cacheLocked(ComponentName componentName, ResolveInfo info, |
| 181 | HashMap<Object, CharSequence> labelCache) { |
Joe Onorato | 0589f0f | 2010-02-08 13:44:00 -0800 | [diff] [blame] | 182 | CacheEntry entry = mCache.get(componentName); |
| 183 | if (entry == null) { |
| 184 | entry = new CacheEntry(); |
| 185 | |
Joe Onorato | 84f6a8d | 2010-02-12 17:53:35 -0500 | [diff] [blame] | 186 | mCache.put(componentName, entry); |
| 187 | |
Winson Chung | c3eecff | 2011-07-11 17:44:15 -0700 | [diff] [blame] | 188 | if (labelCache != null && labelCache.containsKey(info)) { |
| 189 | entry.title = labelCache.get(info).toString(); |
| 190 | } else { |
| 191 | entry.title = info.loadLabel(mPackageManager).toString(); |
| 192 | if (labelCache != null) { |
| 193 | labelCache.put(info, entry.title); |
| 194 | } |
| 195 | } |
Joe Onorato | 0589f0f | 2010-02-08 13:44:00 -0800 | [diff] [blame] | 196 | if (entry.title == null) { |
| 197 | entry.title = info.activityInfo.name; |
| 198 | } |
Kenny Root | 20b0a5f | 2011-03-10 10:53:06 -0800 | [diff] [blame] | 199 | |
Michael Jurka | 4842ed0 | 2011-07-07 15:33:20 -0700 | [diff] [blame] | 200 | entry.icon = Utilities.createIconBitmap( |
| 201 | getFullResIcon(info, mPackageManager), mContext); |
Joe Onorato | 0589f0f | 2010-02-08 13:44:00 -0800 | [diff] [blame] | 202 | } |
| 203 | return entry; |
| 204 | } |
Daniel Sandler | 4e1cd23 | 2011-05-12 00:06:32 -0400 | [diff] [blame] | 205 | |
| 206 | public HashMap<ComponentName,Bitmap> getAllIcons() { |
| 207 | synchronized (mCache) { |
| 208 | HashMap<ComponentName,Bitmap> set = new HashMap<ComponentName,Bitmap>(); |
| 209 | int i = 0; |
| 210 | for (ComponentName cn : mCache.keySet()) { |
| 211 | final CacheEntry e = mCache.get(cn); |
| 212 | set.put(cn, e.icon); |
| 213 | } |
| 214 | return set; |
| 215 | } |
| 216 | } |
Joe Onorato | 0589f0f | 2010-02-08 13:44:00 -0800 | [diff] [blame] | 217 | } |