blob: 04762e9441719f71140b2a16cfbf0dbc2692a149 [file] [log] [blame]
Joe Onorato0589f0f2010-02-08 13:44:00 -08001/*
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
17package com.android.launcher2;
18
19import android.content.ComponentName;
Joe Onorato0589f0f2010-02-08 13:44:00 -080020import android.content.Intent;
Joe Onorato0589f0f2010-02-08 13:44:00 -080021import android.content.pm.PackageManager;
22import android.content.pm.ResolveInfo;
Joe Onorato0589f0f2010-02-08 13:44:00 -080023import android.graphics.Bitmap;
Romain Guya28fd3f2010-03-15 14:44:42 -070024import android.graphics.Canvas;
Joe Onorato0589f0f2010-02-08 13:44:00 -080025import android.graphics.drawable.Drawable;
Joe Onorato0589f0f2010-02-08 13:44:00 -080026
Joe Onorato0589f0f2010-02-08 13:44:00 -080027import java.util.HashMap;
Joe Onorato0589f0f2010-02-08 13:44:00 -080028
29/**
30 * Cache of application icons. Icons can be made from any thread.
31 */
32public 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 Guya28fd3f2010-03-15 14:44:42 -070043 private final Bitmap mDefaultIcon;
44 private final LauncherApplication mContext;
45 private final PackageManager mPackageManager;
46 private final Utilities.BubbleText mBubble;
Joe Onorato0589f0f2010-02-08 13:44:00 -080047 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 Guya28fd3f2010-03-15 14:44:42 -070054 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 Onorato0589f0f2010-02-08 13:44:00 -080066 }
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 Onorato84f6a8d2010-02-12 17:53:35 -050092 if (entry.titleBitmap == null) {
Romain Guya28fd3f2010-03-15 14:44:42 -070093 entry.titleBitmap = mBubble.createTextBitmap(entry.title);
Joe Onorato84f6a8d2010-02-12 17:53:35 -050094 }
Joe Onorato0589f0f2010-02-08 13:44:00 -080095
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 final ResolveInfo resolveInfo = mPackageManager.resolveActivity(intent, 0);
104 ComponentName component = intent.getComponent();
105
106 if (resolveInfo == null || component == null) {
Romain Guya28fd3f2010-03-15 14:44:42 -0700107 return mDefaultIcon;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800108 }
109
110 CacheEntry entry = cacheLocked(component, resolveInfo);
111 return entry.icon;
112 }
113
114 public Bitmap getIcon(ComponentName component, ResolveInfo resolveInfo) {
115 if (resolveInfo == null || component == null) {
116 return null;
117 }
118
119 CacheEntry entry = cacheLocked(component, resolveInfo);
120 return entry.icon;
121 }
122
123 private CacheEntry cacheLocked(ComponentName componentName, ResolveInfo info) {
124 CacheEntry entry = mCache.get(componentName);
125 if (entry == null) {
126 entry = new CacheEntry();
127
Joe Onorato84f6a8d2010-02-12 17:53:35 -0500128 mCache.put(componentName, entry);
129
Joe Onorato0589f0f2010-02-08 13:44:00 -0800130 entry.title = info.loadLabel(mPackageManager).toString();
131 if (entry.title == null) {
132 entry.title = info.activityInfo.name;
133 }
Joe Onorato0589f0f2010-02-08 13:44:00 -0800134 entry.icon = Utilities.createIconBitmap(
135 info.activityInfo.loadIcon(mPackageManager), mContext);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800136 }
137 return entry;
138 }
139}