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; |
| 20 | import android.content.ContentResolver; |
| 21 | import android.content.ContentValues; |
| 22 | import android.content.Intent; |
| 23 | import android.content.Context; |
| 24 | import android.content.pm.ActivityInfo; |
| 25 | import android.content.pm.PackageManager; |
| 26 | import android.content.pm.ResolveInfo; |
| 27 | import android.content.res.Resources; |
| 28 | import android.database.Cursor; |
| 29 | import android.graphics.Bitmap; |
| 30 | import android.graphics.BitmapFactory; |
| 31 | import android.graphics.drawable.Drawable; |
| 32 | import android.graphics.drawable.BitmapDrawable; |
| 33 | import android.net.Uri; |
| 34 | import android.util.Log; |
| 35 | import android.os.Process; |
| 36 | |
| 37 | import java.lang.ref.WeakReference; |
| 38 | import java.util.ArrayList; |
| 39 | import java.util.HashMap; |
| 40 | import java.util.List; |
| 41 | |
| 42 | /** |
| 43 | * Cache of application icons. Icons can be made from any thread. |
| 44 | */ |
| 45 | public class IconCache { |
| 46 | private static final String TAG = "Launcher.IconCache"; |
| 47 | |
| 48 | private static final int INITIAL_ICON_CACHE_CAPACITY = 50; |
| 49 | |
| 50 | private static class CacheEntry { |
| 51 | public Bitmap icon; |
| 52 | public String title; |
| 53 | public Bitmap titleBitmap; |
| 54 | } |
| 55 | |
| 56 | private LauncherApplication mContext; |
| 57 | private PackageManager mPackageManager; |
| 58 | private Utilities.BubbleText mBubble; |
| 59 | private final HashMap<ComponentName, CacheEntry> mCache = |
| 60 | new HashMap<ComponentName, CacheEntry>(INITIAL_ICON_CACHE_CAPACITY); |
| 61 | |
| 62 | public IconCache(LauncherApplication context) { |
| 63 | mContext = context; |
| 64 | mPackageManager = context.getPackageManager(); |
| 65 | mBubble = new Utilities.BubbleText(context); |
| 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); |
| 92 | |
| 93 | application.title = entry.title; |
| 94 | application.titleBitmap = entry.titleBitmap; |
| 95 | application.iconBitmap = entry.icon; |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | public Bitmap getIcon(Intent intent) { |
| 100 | final ResolveInfo resolveInfo = mPackageManager.resolveActivity(intent, 0); |
| 101 | ComponentName component = intent.getComponent(); |
| 102 | |
| 103 | if (resolveInfo == null || component == null) { |
| 104 | return null; |
| 105 | } |
| 106 | |
| 107 | CacheEntry entry = cacheLocked(component, resolveInfo); |
| 108 | return entry.icon; |
| 109 | } |
| 110 | |
| 111 | public Bitmap getIcon(ComponentName component, ResolveInfo resolveInfo) { |
| 112 | if (resolveInfo == null || component == null) { |
| 113 | return null; |
| 114 | } |
| 115 | |
| 116 | CacheEntry entry = cacheLocked(component, resolveInfo); |
| 117 | return entry.icon; |
| 118 | } |
| 119 | |
| 120 | private CacheEntry cacheLocked(ComponentName componentName, ResolveInfo info) { |
| 121 | CacheEntry entry = mCache.get(componentName); |
| 122 | if (entry == null) { |
| 123 | entry = new CacheEntry(); |
| 124 | |
| 125 | entry.title = info.loadLabel(mPackageManager).toString(); |
| 126 | if (entry.title == null) { |
| 127 | entry.title = info.activityInfo.name; |
| 128 | } |
| 129 | entry.titleBitmap = mBubble.createTextBitmap(entry.title.toString()); |
| 130 | entry.icon = Utilities.createIconBitmap( |
| 131 | info.activityInfo.loadIcon(mPackageManager), mContext); |
| 132 | |
| 133 | mCache.put(componentName, entry); |
| 134 | } |
| 135 | return entry; |
| 136 | } |
| 137 | } |
| 138 | |