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; |
Joe Onorato | 0589f0f | 2010-02-08 13:44:00 -0800 | [diff] [blame] | 23 | import android.graphics.Bitmap; |
Romain Guy | a28fd3f | 2010-03-15 14:44:42 -0700 | [diff] [blame] | 24 | import android.graphics.Canvas; |
Joe Onorato | 0589f0f | 2010-02-08 13:44:00 -0800 | [diff] [blame] | 25 | import android.graphics.drawable.Drawable; |
Joe Onorato | 0589f0f | 2010-02-08 13:44:00 -0800 | [diff] [blame] | 26 | |
Joe Onorato | 0589f0f | 2010-02-08 13:44:00 -0800 | [diff] [blame] | 27 | import java.util.HashMap; |
Joe Onorato | 0589f0f | 2010-02-08 13:44:00 -0800 | [diff] [blame] | 28 | |
| 29 | /** |
| 30 | * Cache of application icons. Icons can be made from any thread. |
| 31 | */ |
| 32 | public class IconCache { |
| 33 | private static final String TAG = "Launcher.IconCache"; |
| 34 | |
| 35 | private static final int INITIAL_ICON_CACHE_CAPACITY = 50; |
| 36 | |
| 37 | private static class CacheEntry { |
| 38 | public Bitmap icon; |
| 39 | public String title; |
| 40 | public Bitmap titleBitmap; |
| 41 | } |
| 42 | |
Romain Guy | a28fd3f | 2010-03-15 14:44:42 -0700 | [diff] [blame] | 43 | private final Bitmap mDefaultIcon; |
| 44 | private final LauncherApplication mContext; |
| 45 | private final PackageManager mPackageManager; |
| 46 | private final Utilities.BubbleText mBubble; |
Joe Onorato | 0589f0f | 2010-02-08 13:44:00 -0800 | [diff] [blame] | 47 | private final HashMap<ComponentName, CacheEntry> mCache = |
| 48 | new HashMap<ComponentName, CacheEntry>(INITIAL_ICON_CACHE_CAPACITY); |
| 49 | |
| 50 | public IconCache(LauncherApplication context) { |
| 51 | mContext = context; |
| 52 | mPackageManager = context.getPackageManager(); |
| 53 | mBubble = new Utilities.BubbleText(context); |
Romain Guy | a28fd3f | 2010-03-15 14:44:42 -0700 | [diff] [blame] | 54 | mDefaultIcon = makeDefaultIcon(); |
| 55 | } |
| 56 | |
| 57 | private Bitmap makeDefaultIcon() { |
| 58 | Drawable d = mPackageManager.getDefaultActivityIcon(); |
| 59 | Bitmap b = Bitmap.createBitmap(Math.max(d.getIntrinsicWidth(), 1), |
| 60 | Math.max(d.getIntrinsicHeight(), 1), |
| 61 | Bitmap.Config.ARGB_8888); |
| 62 | Canvas c = new Canvas(b); |
| 63 | d.setBounds(0, 0, b.getWidth(), b.getHeight()); |
| 64 | d.draw(c); |
| 65 | return b; |
Joe Onorato | 0589f0f | 2010-02-08 13:44:00 -0800 | [diff] [blame] | 66 | } |
| 67 | |
| 68 | /** |
| 69 | * Remove any records for the supplied ComponentName. |
| 70 | */ |
| 71 | public void remove(ComponentName componentName) { |
| 72 | synchronized (mCache) { |
| 73 | mCache.remove(componentName); |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | /** |
| 78 | * Empty out the cache. |
| 79 | */ |
| 80 | public void flush() { |
| 81 | synchronized (mCache) { |
| 82 | mCache.clear(); |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | /** |
| 87 | * Fill in "application" with the icon and label for "info." |
| 88 | */ |
| 89 | public void getTitleAndIcon(ApplicationInfo application, ResolveInfo info) { |
| 90 | synchronized (mCache) { |
| 91 | CacheEntry entry = cacheLocked(application.componentName, info); |
Joe Onorato | 84f6a8d | 2010-02-12 17:53:35 -0500 | [diff] [blame] | 92 | if (entry.titleBitmap == null) { |
Romain Guy | a28fd3f | 2010-03-15 14:44:42 -0700 | [diff] [blame] | 93 | entry.titleBitmap = mBubble.createTextBitmap(entry.title); |
Joe Onorato | 84f6a8d | 2010-02-12 17:53:35 -0500 | [diff] [blame] | 94 | } |
Joe Onorato | 0589f0f | 2010-02-08 13:44:00 -0800 | [diff] [blame] | 95 | |
| 96 | application.title = entry.title; |
| 97 | application.titleBitmap = entry.titleBitmap; |
| 98 | application.iconBitmap = entry.icon; |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | public Bitmap getIcon(Intent intent) { |
Joe Onorato | fad1fb5 | 2010-05-04 12:12:41 -0700 | [diff] [blame] | 103 | synchronized (mCache) { |
| 104 | final ResolveInfo resolveInfo = mPackageManager.resolveActivity(intent, 0); |
| 105 | ComponentName component = intent.getComponent(); |
Joe Onorato | 0589f0f | 2010-02-08 13:44:00 -0800 | [diff] [blame] | 106 | |
Joe Onorato | fad1fb5 | 2010-05-04 12:12:41 -0700 | [diff] [blame] | 107 | if (resolveInfo == null || component == null) { |
| 108 | return mDefaultIcon; |
| 109 | } |
| 110 | |
| 111 | CacheEntry entry = cacheLocked(component, resolveInfo); |
| 112 | return entry.icon; |
Joe Onorato | 0589f0f | 2010-02-08 13:44:00 -0800 | [diff] [blame] | 113 | } |
Joe Onorato | 0589f0f | 2010-02-08 13:44:00 -0800 | [diff] [blame] | 114 | } |
| 115 | |
| 116 | public Bitmap getIcon(ComponentName component, ResolveInfo resolveInfo) { |
Joe Onorato | fad1fb5 | 2010-05-04 12:12:41 -0700 | [diff] [blame] | 117 | synchronized (mCache) { |
| 118 | if (resolveInfo == null || component == null) { |
| 119 | return null; |
| 120 | } |
Joe Onorato | 0589f0f | 2010-02-08 13:44:00 -0800 | [diff] [blame] | 121 | |
Joe Onorato | fad1fb5 | 2010-05-04 12:12:41 -0700 | [diff] [blame] | 122 | CacheEntry entry = cacheLocked(component, resolveInfo); |
| 123 | return entry.icon; |
| 124 | } |
Joe Onorato | 0589f0f | 2010-02-08 13:44:00 -0800 | [diff] [blame] | 125 | } |
| 126 | |
Joe Onorato | ddc9c1f | 2010-08-30 18:30:15 -0700 | [diff] [blame^] | 127 | public boolean isDefaultIcon(Bitmap icon) { |
| 128 | return mDefaultIcon == icon; |
| 129 | } |
| 130 | |
Joe Onorato | 0589f0f | 2010-02-08 13:44:00 -0800 | [diff] [blame] | 131 | private CacheEntry cacheLocked(ComponentName componentName, ResolveInfo info) { |
| 132 | CacheEntry entry = mCache.get(componentName); |
| 133 | if (entry == null) { |
| 134 | entry = new CacheEntry(); |
| 135 | |
Joe Onorato | 84f6a8d | 2010-02-12 17:53:35 -0500 | [diff] [blame] | 136 | mCache.put(componentName, entry); |
| 137 | |
Joe Onorato | 0589f0f | 2010-02-08 13:44:00 -0800 | [diff] [blame] | 138 | entry.title = info.loadLabel(mPackageManager).toString(); |
| 139 | if (entry.title == null) { |
| 140 | entry.title = info.activityInfo.name; |
| 141 | } |
Joe Onorato | 0589f0f | 2010-02-08 13:44:00 -0800 | [diff] [blame] | 142 | entry.icon = Utilities.createIconBitmap( |
| 143 | info.activityInfo.loadIcon(mPackageManager), mContext); |
Joe Onorato | 0589f0f | 2010-02-08 13:44:00 -0800 | [diff] [blame] | 144 | } |
| 145 | return entry; |
| 146 | } |
| 147 | } |