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 | |
Michael Jurka | c9a9619 | 2010-11-01 11:52:08 -0700 | [diff] [blame] | 19 | import com.android.launcher.R; |
| 20 | |
Joe Onorato | 0589f0f | 2010-02-08 13:44:00 -0800 | [diff] [blame] | 21 | import android.content.ComponentName; |
Joe Onorato | 0589f0f | 2010-02-08 13:44:00 -0800 | [diff] [blame] | 22 | import android.content.Intent; |
Joe Onorato | 0589f0f | 2010-02-08 13:44:00 -0800 | [diff] [blame] | 23 | import android.content.pm.PackageManager; |
| 24 | import android.content.pm.ResolveInfo; |
Michael Jurka | c9a9619 | 2010-11-01 11:52:08 -0700 | [diff] [blame] | 25 | import android.content.res.Resources; |
Joe Onorato | 0589f0f | 2010-02-08 13:44:00 -0800 | [diff] [blame] | 26 | import android.graphics.Bitmap; |
Romain Guy | a28fd3f | 2010-03-15 14:44:42 -0700 | [diff] [blame] | 27 | import android.graphics.Canvas; |
Joe Onorato | 0589f0f | 2010-02-08 13:44:00 -0800 | [diff] [blame] | 28 | import android.graphics.drawable.Drawable; |
Michael Jurka | c9a9619 | 2010-11-01 11:52:08 -0700 | [diff] [blame] | 29 | import android.util.DisplayMetrics; |
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 { |
| 37 | private static final String TAG = "Launcher.IconCache"; |
| 38 | |
| 39 | private static final int INITIAL_ICON_CACHE_CAPACITY = 50; |
| 40 | |
| 41 | private static class CacheEntry { |
| 42 | public Bitmap icon; |
| 43 | public String title; |
| 44 | public Bitmap titleBitmap; |
| 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; |
| 50 | private final Utilities.BubbleText mBubble; |
Joe Onorato | 0589f0f | 2010-02-08 13:44:00 -0800 | [diff] [blame] | 51 | private final HashMap<ComponentName, CacheEntry> mCache = |
| 52 | new HashMap<ComponentName, CacheEntry>(INITIAL_ICON_CACHE_CAPACITY); |
Michael Jurka | c9a9619 | 2010-11-01 11:52:08 -0700 | [diff] [blame] | 53 | private int mIconDpi; |
Joe Onorato | 0589f0f | 2010-02-08 13:44:00 -0800 | [diff] [blame] | 54 | |
| 55 | public IconCache(LauncherApplication context) { |
| 56 | mContext = context; |
| 57 | mPackageManager = context.getPackageManager(); |
| 58 | mBubble = new Utilities.BubbleText(context); |
Michael Jurka | c9a9619 | 2010-11-01 11:52:08 -0700 | [diff] [blame] | 59 | if (LauncherApplication.isScreenXLarge()) { |
| 60 | mIconDpi = DisplayMetrics.DENSITY_HIGH; |
| 61 | } else { |
| 62 | mIconDpi = context.getResources().getDisplayMetrics().densityDpi; |
| 63 | } |
| 64 | // need to set mIconDpi before getting default icon |
Romain Guy | a28fd3f | 2010-03-15 14:44:42 -0700 | [diff] [blame] | 65 | mDefaultIcon = makeDefaultIcon(); |
| 66 | } |
| 67 | |
Michael Jurka | c9a9619 | 2010-11-01 11:52:08 -0700 | [diff] [blame] | 68 | public Drawable getFullResDefaultActivityIcon() { |
| 69 | return getFullResIcon(Resources.getSystem(), |
| 70 | com.android.internal.R.drawable.sym_def_app_icon); |
| 71 | } |
| 72 | |
| 73 | public Drawable getFullResIcon(Resources resources, int iconId) { |
| 74 | return resources.getDrawableForDensity(iconId, mIconDpi); |
| 75 | } |
| 76 | |
| 77 | public Drawable getFullResIcon(ResolveInfo info, PackageManager packageManager) { |
| 78 | Resources resources; |
| 79 | try { |
| 80 | resources = packageManager.getResourcesForApplication( |
| 81 | info.activityInfo.applicationInfo); |
| 82 | } catch (PackageManager.NameNotFoundException e) { |
| 83 | resources = null; |
| 84 | } |
| 85 | if (resources != null) { |
| 86 | int iconId = info.activityInfo.getIconResource(); |
| 87 | if (iconId != 0) { |
| 88 | return getFullResIcon(resources, iconId); |
| 89 | } |
| 90 | } |
| 91 | return getFullResDefaultActivityIcon(); |
| 92 | } |
| 93 | |
Romain Guy | a28fd3f | 2010-03-15 14:44:42 -0700 | [diff] [blame] | 94 | private Bitmap makeDefaultIcon() { |
Michael Jurka | c9a9619 | 2010-11-01 11:52:08 -0700 | [diff] [blame] | 95 | Drawable d = getFullResDefaultActivityIcon(); |
Romain Guy | a28fd3f | 2010-03-15 14:44:42 -0700 | [diff] [blame] | 96 | Bitmap b = Bitmap.createBitmap(Math.max(d.getIntrinsicWidth(), 1), |
| 97 | Math.max(d.getIntrinsicHeight(), 1), |
| 98 | Bitmap.Config.ARGB_8888); |
| 99 | Canvas c = new Canvas(b); |
| 100 | d.setBounds(0, 0, b.getWidth(), b.getHeight()); |
| 101 | d.draw(c); |
| 102 | return b; |
Joe Onorato | 0589f0f | 2010-02-08 13:44:00 -0800 | [diff] [blame] | 103 | } |
| 104 | |
| 105 | /** |
| 106 | * Remove any records for the supplied ComponentName. |
| 107 | */ |
| 108 | public void remove(ComponentName componentName) { |
| 109 | synchronized (mCache) { |
| 110 | mCache.remove(componentName); |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | /** |
| 115 | * Empty out the cache. |
| 116 | */ |
| 117 | public void flush() { |
| 118 | synchronized (mCache) { |
| 119 | mCache.clear(); |
| 120 | } |
| 121 | } |
| 122 | |
| 123 | /** |
| 124 | * Fill in "application" with the icon and label for "info." |
| 125 | */ |
| 126 | public void getTitleAndIcon(ApplicationInfo application, ResolveInfo info) { |
| 127 | synchronized (mCache) { |
| 128 | CacheEntry entry = cacheLocked(application.componentName, info); |
Joe Onorato | 84f6a8d | 2010-02-12 17:53:35 -0500 | [diff] [blame] | 129 | if (entry.titleBitmap == null) { |
Romain Guy | a28fd3f | 2010-03-15 14:44:42 -0700 | [diff] [blame] | 130 | entry.titleBitmap = mBubble.createTextBitmap(entry.title); |
Joe Onorato | 84f6a8d | 2010-02-12 17:53:35 -0500 | [diff] [blame] | 131 | } |
Joe Onorato | 0589f0f | 2010-02-08 13:44:00 -0800 | [diff] [blame] | 132 | |
| 133 | application.title = entry.title; |
| 134 | application.titleBitmap = entry.titleBitmap; |
| 135 | application.iconBitmap = entry.icon; |
| 136 | } |
| 137 | } |
| 138 | |
| 139 | public Bitmap getIcon(Intent intent) { |
Joe Onorato | fad1fb5 | 2010-05-04 12:12:41 -0700 | [diff] [blame] | 140 | synchronized (mCache) { |
| 141 | final ResolveInfo resolveInfo = mPackageManager.resolveActivity(intent, 0); |
| 142 | ComponentName component = intent.getComponent(); |
Joe Onorato | 0589f0f | 2010-02-08 13:44:00 -0800 | [diff] [blame] | 143 | |
Joe Onorato | fad1fb5 | 2010-05-04 12:12:41 -0700 | [diff] [blame] | 144 | if (resolveInfo == null || component == null) { |
| 145 | return mDefaultIcon; |
| 146 | } |
| 147 | |
| 148 | CacheEntry entry = cacheLocked(component, resolveInfo); |
| 149 | return entry.icon; |
Joe Onorato | 0589f0f | 2010-02-08 13:44:00 -0800 | [diff] [blame] | 150 | } |
Joe Onorato | 0589f0f | 2010-02-08 13:44:00 -0800 | [diff] [blame] | 151 | } |
| 152 | |
| 153 | public Bitmap getIcon(ComponentName component, ResolveInfo resolveInfo) { |
Joe Onorato | fad1fb5 | 2010-05-04 12:12:41 -0700 | [diff] [blame] | 154 | synchronized (mCache) { |
| 155 | if (resolveInfo == null || component == null) { |
| 156 | return null; |
| 157 | } |
Joe Onorato | 0589f0f | 2010-02-08 13:44:00 -0800 | [diff] [blame] | 158 | |
Joe Onorato | fad1fb5 | 2010-05-04 12:12:41 -0700 | [diff] [blame] | 159 | CacheEntry entry = cacheLocked(component, resolveInfo); |
| 160 | return entry.icon; |
| 161 | } |
Joe Onorato | 0589f0f | 2010-02-08 13:44:00 -0800 | [diff] [blame] | 162 | } |
| 163 | |
Joe Onorato | ddc9c1f | 2010-08-30 18:30:15 -0700 | [diff] [blame] | 164 | public boolean isDefaultIcon(Bitmap icon) { |
| 165 | return mDefaultIcon == icon; |
| 166 | } |
| 167 | |
Joe Onorato | 0589f0f | 2010-02-08 13:44:00 -0800 | [diff] [blame] | 168 | private CacheEntry cacheLocked(ComponentName componentName, ResolveInfo info) { |
| 169 | CacheEntry entry = mCache.get(componentName); |
| 170 | if (entry == null) { |
| 171 | entry = new CacheEntry(); |
| 172 | |
Joe Onorato | 84f6a8d | 2010-02-12 17:53:35 -0500 | [diff] [blame] | 173 | mCache.put(componentName, entry); |
| 174 | |
Joe Onorato | 0589f0f | 2010-02-08 13:44:00 -0800 | [diff] [blame] | 175 | entry.title = info.loadLabel(mPackageManager).toString(); |
| 176 | if (entry.title == null) { |
| 177 | entry.title = info.activityInfo.name; |
| 178 | } |
Joe Onorato | 0589f0f | 2010-02-08 13:44:00 -0800 | [diff] [blame] | 179 | entry.icon = Utilities.createIconBitmap( |
Michael Jurka | c9a9619 | 2010-11-01 11:52:08 -0700 | [diff] [blame] | 180 | getFullResIcon(info, mPackageManager), mContext); |
Joe Onorato | 0589f0f | 2010-02-08 13:44:00 -0800 | [diff] [blame] | 181 | } |
| 182 | return entry; |
| 183 | } |
| 184 | } |