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.Intent; 21 import android.content.pm.PackageManager; 22 import android.content.pm.ResolveInfo; 23 import android.graphics.Bitmap; 24 import android.graphics.Canvas; 25 import android.graphics.drawable.Drawable; 26 27 import java.util.HashMap; 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 43 private final Bitmap mDefaultIcon; 44 private final LauncherApplication mContext; 45 private final PackageManager mPackageManager; 46 private final Utilities.BubbleText mBubble; 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); 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; 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 if (entry.titleBitmap == null) { 93 entry.titleBitmap = mBubble.createTextBitmap(entry.title); 94 } 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) { 103 synchronized (mCache) { 104 final ResolveInfo resolveInfo = mPackageManager.resolveActivity(intent, 0); 105 ComponentName component = intent.getComponent(); 106 107 if (resolveInfo == null || component == null) { 108 return mDefaultIcon; 109 } 110 111 CacheEntry entry = cacheLocked(component, resolveInfo); 112 return entry.icon; 113 } 114 } 115 116 public Bitmap getIcon(ComponentName component, ResolveInfo resolveInfo) { 117 synchronized (mCache) { 118 if (resolveInfo == null || component == null) { 119 return null; 120 } 121 122 CacheEntry entry = cacheLocked(component, resolveInfo); 123 return entry.icon; 124 } 125 } 126 127 public boolean isDefaultIcon(Bitmap icon) { 128 return mDefaultIcon == icon; 129 } 130 131 private CacheEntry cacheLocked(ComponentName componentName, ResolveInfo info) { 132 CacheEntry entry = mCache.get(componentName); 133 if (entry == null) { 134 entry = new CacheEntry(); 135 136 mCache.put(componentName, entry); 137 138 entry.title = info.loadLabel(mPackageManager).toString(); 139 if (entry.title == null) { 140 entry.title = info.activityInfo.name; 141 } 142 entry.icon = Utilities.createIconBitmap( 143 info.activityInfo.loadIcon(mPackageManager), mContext); 144 } 145 return entry; 146 } 147 } 148