blob: 2e47adc9e88b3fa7ad6159395112d6330bc33b8b [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;
Michael Jurkac9a96192010-11-01 11:52:08 -070023import android.content.res.Resources;
Joe Onorato0589f0f2010-02-08 13:44:00 -080024import android.graphics.Bitmap;
Romain Guya28fd3f2010-03-15 14:44:42 -070025import android.graphics.Canvas;
Joe Onorato0589f0f2010-02-08 13:44:00 -080026import android.graphics.drawable.Drawable;
Michael Jurkac9a96192010-11-01 11:52:08 -070027import android.util.DisplayMetrics;
Joe Onorato0589f0f2010-02-08 13:44:00 -080028
Joe Onorato0589f0f2010-02-08 13:44:00 -080029import java.util.HashMap;
Joe Onorato0589f0f2010-02-08 13:44:00 -080030
31/**
32 * Cache of application icons. Icons can be made from any thread.
33 */
34public class IconCache {
35 private static final String TAG = "Launcher.IconCache";
36
37 private static final int INITIAL_ICON_CACHE_CAPACITY = 50;
38
39 private static class CacheEntry {
40 public Bitmap icon;
41 public String title;
42 public Bitmap titleBitmap;
43 }
44
Romain Guya28fd3f2010-03-15 14:44:42 -070045 private final Bitmap mDefaultIcon;
46 private final LauncherApplication mContext;
47 private final PackageManager mPackageManager;
48 private final Utilities.BubbleText mBubble;
Joe Onorato0589f0f2010-02-08 13:44:00 -080049 private final HashMap<ComponentName, CacheEntry> mCache =
50 new HashMap<ComponentName, CacheEntry>(INITIAL_ICON_CACHE_CAPACITY);
Michael Jurkac9a96192010-11-01 11:52:08 -070051 private int mIconDpi;
Joe Onorato0589f0f2010-02-08 13:44:00 -080052
53 public IconCache(LauncherApplication context) {
54 mContext = context;
55 mPackageManager = context.getPackageManager();
56 mBubble = new Utilities.BubbleText(context);
Michael Jurkac9a96192010-11-01 11:52:08 -070057 if (LauncherApplication.isScreenXLarge()) {
58 mIconDpi = DisplayMetrics.DENSITY_HIGH;
59 } else {
60 mIconDpi = context.getResources().getDisplayMetrics().densityDpi;
61 }
62 // need to set mIconDpi before getting default icon
Romain Guya28fd3f2010-03-15 14:44:42 -070063 mDefaultIcon = makeDefaultIcon();
64 }
65
Michael Jurkac9a96192010-11-01 11:52:08 -070066 public Drawable getFullResDefaultActivityIcon() {
67 return getFullResIcon(Resources.getSystem(),
Kenny Rootf5675de2011-01-12 10:15:46 -080068 com.android.internal.R.mipmap.sym_def_app_icon);
Michael Jurkac9a96192010-11-01 11:52:08 -070069 }
70
71 public Drawable getFullResIcon(Resources resources, int iconId) {
72 return resources.getDrawableForDensity(iconId, mIconDpi);
73 }
74
75 public Drawable getFullResIcon(ResolveInfo info, PackageManager packageManager) {
76 Resources resources;
77 try {
78 resources = packageManager.getResourcesForApplication(
79 info.activityInfo.applicationInfo);
80 } catch (PackageManager.NameNotFoundException e) {
81 resources = null;
82 }
83 if (resources != null) {
84 int iconId = info.activityInfo.getIconResource();
85 if (iconId != 0) {
86 return getFullResIcon(resources, iconId);
87 }
88 }
89 return getFullResDefaultActivityIcon();
90 }
91
Romain Guya28fd3f2010-03-15 14:44:42 -070092 private Bitmap makeDefaultIcon() {
Michael Jurkac9a96192010-11-01 11:52:08 -070093 Drawable d = getFullResDefaultActivityIcon();
Romain Guya28fd3f2010-03-15 14:44:42 -070094 Bitmap b = Bitmap.createBitmap(Math.max(d.getIntrinsicWidth(), 1),
95 Math.max(d.getIntrinsicHeight(), 1),
96 Bitmap.Config.ARGB_8888);
97 Canvas c = new Canvas(b);
98 d.setBounds(0, 0, b.getWidth(), b.getHeight());
99 d.draw(c);
100 return b;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800101 }
102
103 /**
104 * Remove any records for the supplied ComponentName.
105 */
106 public void remove(ComponentName componentName) {
107 synchronized (mCache) {
108 mCache.remove(componentName);
109 }
110 }
111
112 /**
113 * Empty out the cache.
114 */
115 public void flush() {
116 synchronized (mCache) {
117 mCache.clear();
118 }
119 }
120
121 /**
122 * Fill in "application" with the icon and label for "info."
123 */
124 public void getTitleAndIcon(ApplicationInfo application, ResolveInfo info) {
125 synchronized (mCache) {
126 CacheEntry entry = cacheLocked(application.componentName, info);
Joe Onorato84f6a8d2010-02-12 17:53:35 -0500127 if (entry.titleBitmap == null) {
Romain Guya28fd3f2010-03-15 14:44:42 -0700128 entry.titleBitmap = mBubble.createTextBitmap(entry.title);
Joe Onorato84f6a8d2010-02-12 17:53:35 -0500129 }
Joe Onorato0589f0f2010-02-08 13:44:00 -0800130
131 application.title = entry.title;
132 application.titleBitmap = entry.titleBitmap;
133 application.iconBitmap = entry.icon;
134 }
135 }
136
137 public Bitmap getIcon(Intent intent) {
Joe Onoratofad1fb52010-05-04 12:12:41 -0700138 synchronized (mCache) {
139 final ResolveInfo resolveInfo = mPackageManager.resolveActivity(intent, 0);
140 ComponentName component = intent.getComponent();
Joe Onorato0589f0f2010-02-08 13:44:00 -0800141
Joe Onoratofad1fb52010-05-04 12:12:41 -0700142 if (resolveInfo == null || component == null) {
143 return mDefaultIcon;
144 }
145
146 CacheEntry entry = cacheLocked(component, resolveInfo);
147 return entry.icon;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800148 }
Joe Onorato0589f0f2010-02-08 13:44:00 -0800149 }
150
151 public Bitmap getIcon(ComponentName component, ResolveInfo resolveInfo) {
Joe Onoratofad1fb52010-05-04 12:12:41 -0700152 synchronized (mCache) {
153 if (resolveInfo == null || component == null) {
154 return null;
155 }
Joe Onorato0589f0f2010-02-08 13:44:00 -0800156
Joe Onoratofad1fb52010-05-04 12:12:41 -0700157 CacheEntry entry = cacheLocked(component, resolveInfo);
158 return entry.icon;
159 }
Joe Onorato0589f0f2010-02-08 13:44:00 -0800160 }
161
Joe Onoratoddc9c1f2010-08-30 18:30:15 -0700162 public boolean isDefaultIcon(Bitmap icon) {
163 return mDefaultIcon == icon;
164 }
165
Joe Onorato0589f0f2010-02-08 13:44:00 -0800166 private CacheEntry cacheLocked(ComponentName componentName, ResolveInfo info) {
167 CacheEntry entry = mCache.get(componentName);
168 if (entry == null) {
169 entry = new CacheEntry();
170
Joe Onorato84f6a8d2010-02-12 17:53:35 -0500171 mCache.put(componentName, entry);
172
Joe Onorato0589f0f2010-02-08 13:44:00 -0800173 entry.title = info.loadLabel(mPackageManager).toString();
174 if (entry.title == null) {
175 entry.title = info.activityInfo.name;
176 }
Joe Onorato0589f0f2010-02-08 13:44:00 -0800177 entry.icon = Utilities.createIconBitmap(
Michael Jurkac9a96192010-11-01 11:52:08 -0700178 getFullResIcon(info, mPackageManager), mContext);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800179 }
180 return entry;
181 }
182}