blob: 81a786ca74f57f3f00d6cd0f8d524c8c1fdf8669 [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) {
Joe Onoratofad1fb52010-05-04 12:12:41 -0700103 synchronized (mCache) {
104 final ResolveInfo resolveInfo = mPackageManager.resolveActivity(intent, 0);
105 ComponentName component = intent.getComponent();
Joe Onorato0589f0f2010-02-08 13:44:00 -0800106
Joe Onoratofad1fb52010-05-04 12:12:41 -0700107 if (resolveInfo == null || component == null) {
108 return mDefaultIcon;
109 }
110
111 CacheEntry entry = cacheLocked(component, resolveInfo);
112 return entry.icon;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800113 }
Joe Onorato0589f0f2010-02-08 13:44:00 -0800114 }
115
116 public Bitmap getIcon(ComponentName component, ResolveInfo resolveInfo) {
Joe Onoratofad1fb52010-05-04 12:12:41 -0700117 synchronized (mCache) {
118 if (resolveInfo == null || component == null) {
119 return null;
120 }
Joe Onorato0589f0f2010-02-08 13:44:00 -0800121
Joe Onoratofad1fb52010-05-04 12:12:41 -0700122 CacheEntry entry = cacheLocked(component, resolveInfo);
123 return entry.icon;
124 }
Joe Onorato0589f0f2010-02-08 13:44:00 -0800125 }
126
Joe Onoratoddc9c1f2010-08-30 18:30:15 -0700127 public boolean isDefaultIcon(Bitmap icon) {
128 return mDefaultIcon == icon;
129 }
130
Joe Onorato0589f0f2010-02-08 13:44:00 -0800131 private CacheEntry cacheLocked(ComponentName componentName, ResolveInfo info) {
132 CacheEntry entry = mCache.get(componentName);
133 if (entry == null) {
134 entry = new CacheEntry();
135
Joe Onorato84f6a8d2010-02-12 17:53:35 -0500136 mCache.put(componentName, entry);
137
Joe Onorato0589f0f2010-02-08 13:44:00 -0800138 entry.title = info.loadLabel(mPackageManager).toString();
139 if (entry.title == null) {
140 entry.title = info.activityInfo.name;
141 }
Joe Onorato0589f0f2010-02-08 13:44:00 -0800142 entry.icon = Utilities.createIconBitmap(
143 info.activityInfo.loadIcon(mPackageManager), mContext);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800144 }
145 return entry;
146 }
147}