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