blob: 827718b9eebc5cd00a8cfaac22ce85c9e5c50c77 [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
Daniel Sandler325dc232013-06-05 22:57:57 -040017package com.android.launcher3;
Joe Onorato0589f0f2010-02-08 13:44:00 -080018
Chris Wren6d0dde02014-02-10 12:16:54 -050019import com.android.launcher3.backup.BackupProtos;
20
Winson Chungd83f5f42012-02-13 14:27:42 -080021import android.app.ActivityManager;
Joe Onorato0589f0f2010-02-08 13:44:00 -080022import android.content.ComponentName;
Winson Chungd83f5f42012-02-13 14:27:42 -080023import android.content.Context;
Joe Onorato0589f0f2010-02-08 13:44:00 -080024import android.content.Intent;
Michael Jurkadac85912012-05-18 15:04:49 -070025import android.content.pm.ActivityInfo;
Joe Onorato0589f0f2010-02-08 13:44:00 -080026import android.content.pm.PackageManager;
27import android.content.pm.ResolveInfo;
Michael Jurkac9a96192010-11-01 11:52:08 -070028import android.content.res.Resources;
Joe Onorato0589f0f2010-02-08 13:44:00 -080029import android.graphics.Bitmap;
Chris Wren6d0dde02014-02-10 12:16:54 -050030import android.graphics.BitmapFactory;
Romain Guya28fd3f2010-03-15 14:44:42 -070031import android.graphics.Canvas;
Chris Wren6d0dde02014-02-10 12:16:54 -050032import android.graphics.Paint;
Joe Onorato0589f0f2010-02-08 13:44:00 -080033import android.graphics.drawable.Drawable;
Chris Wren6d0dde02014-02-10 12:16:54 -050034import android.util.Log;
Joe Onorato0589f0f2010-02-08 13:44:00 -080035
Chris Wren6d0dde02014-02-10 12:16:54 -050036import java.io.ByteArrayOutputStream;
37import java.io.File;
38import java.io.FileInputStream;
39import java.io.FileNotFoundException;
40import java.io.FileOutputStream;
41import java.io.IOException;
Joe Onorato0589f0f2010-02-08 13:44:00 -080042import java.util.HashMap;
Chris Wren6d0dde02014-02-10 12:16:54 -050043import java.util.HashSet;
Adam Cohenb6d33df2013-10-15 10:18:02 -070044import java.util.Iterator;
45import java.util.Map.Entry;
Joe Onorato0589f0f2010-02-08 13:44:00 -080046
47/**
48 * Cache of application icons. Icons can be made from any thread.
49 */
50public class IconCache {
Michael Jurka3a9fced2012-04-13 14:44:29 -070051 @SuppressWarnings("unused")
Joe Onorato0589f0f2010-02-08 13:44:00 -080052 private static final String TAG = "Launcher.IconCache";
53
54 private static final int INITIAL_ICON_CACHE_CAPACITY = 50;
Chris Wren6d0dde02014-02-10 12:16:54 -050055 private static final String RESOURCE_FILE_PREFIX = "icon_";
56
57 private static final boolean DEBUG = true;
Joe Onorato0589f0f2010-02-08 13:44:00 -080058
59 private static class CacheEntry {
60 public Bitmap icon;
61 public String title;
Joe Onorato0589f0f2010-02-08 13:44:00 -080062 }
63
Romain Guya28fd3f2010-03-15 14:44:42 -070064 private final Bitmap mDefaultIcon;
Daniel Sandlercc8befa2013-06-11 14:45:48 -040065 private final Context mContext;
Romain Guya28fd3f2010-03-15 14:44:42 -070066 private final PackageManager mPackageManager;
Joe Onorato0589f0f2010-02-08 13:44:00 -080067 private final HashMap<ComponentName, CacheEntry> mCache =
68 new HashMap<ComponentName, CacheEntry>(INITIAL_ICON_CACHE_CAPACITY);
Michael Jurkac9a96192010-11-01 11:52:08 -070069 private int mIconDpi;
Joe Onorato0589f0f2010-02-08 13:44:00 -080070
Daniel Sandlercc8befa2013-06-11 14:45:48 -040071 public IconCache(Context context) {
Winson Chungd83f5f42012-02-13 14:27:42 -080072 ActivityManager activityManager =
73 (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
74
Joe Onorato0589f0f2010-02-08 13:44:00 -080075 mContext = context;
76 mPackageManager = context.getPackageManager();
Winson Chungd83f5f42012-02-13 14:27:42 -080077 mIconDpi = activityManager.getLauncherLargeIconDensity();
78
Michael Jurkac9a96192010-11-01 11:52:08 -070079 // need to set mIconDpi before getting default icon
Romain Guya28fd3f2010-03-15 14:44:42 -070080 mDefaultIcon = makeDefaultIcon();
81 }
82
Michael Jurkac9a96192010-11-01 11:52:08 -070083 public Drawable getFullResDefaultActivityIcon() {
84 return getFullResIcon(Resources.getSystem(),
Michael Jurka8b805b12012-04-18 14:23:14 -070085 android.R.mipmap.sym_def_app_icon);
Michael Jurkac9a96192010-11-01 11:52:08 -070086 }
87
Michael Jurka4842ed02011-07-07 15:33:20 -070088 public Drawable getFullResIcon(Resources resources, int iconId) {
Michael Jurka721d9722011-08-03 11:49:59 -070089 Drawable d;
Michael Jurka4842ed02011-07-07 15:33:20 -070090 try {
Michael Jurka721d9722011-08-03 11:49:59 -070091 d = resources.getDrawableForDensity(iconId, mIconDpi);
Michael Jurka4842ed02011-07-07 15:33:20 -070092 } catch (Resources.NotFoundException e) {
Michael Jurka721d9722011-08-03 11:49:59 -070093 d = null;
Michael Jurka4842ed02011-07-07 15:33:20 -070094 }
Michael Jurka721d9722011-08-03 11:49:59 -070095
96 return (d != null) ? d : getFullResDefaultActivityIcon();
Michael Jurkac9a96192010-11-01 11:52:08 -070097 }
98
Winson Chung0b9fcf52011-10-31 13:05:15 -070099 public Drawable getFullResIcon(String packageName, int iconId) {
Michael Jurkac9a96192010-11-01 11:52:08 -0700100 Resources resources;
101 try {
Winson Chung0b9fcf52011-10-31 13:05:15 -0700102 resources = mPackageManager.getResourcesForApplication(packageName);
103 } catch (PackageManager.NameNotFoundException e) {
104 resources = null;
105 }
106 if (resources != null) {
107 if (iconId != 0) {
108 return getFullResIcon(resources, iconId);
109 }
110 }
111 return getFullResDefaultActivityIcon();
112 }
113
114 public Drawable getFullResIcon(ResolveInfo info) {
Michael Jurkadac85912012-05-18 15:04:49 -0700115 return getFullResIcon(info.activityInfo);
116 }
117
118 public Drawable getFullResIcon(ActivityInfo info) {
119
Winson Chung0b9fcf52011-10-31 13:05:15 -0700120 Resources resources;
121 try {
122 resources = mPackageManager.getResourcesForApplication(
Michael Jurkadac85912012-05-18 15:04:49 -0700123 info.applicationInfo);
Michael Jurkac9a96192010-11-01 11:52:08 -0700124 } catch (PackageManager.NameNotFoundException e) {
125 resources = null;
126 }
127 if (resources != null) {
Michael Jurkadac85912012-05-18 15:04:49 -0700128 int iconId = info.getIconResource();
Michael Jurkac9a96192010-11-01 11:52:08 -0700129 if (iconId != 0) {
130 return getFullResIcon(resources, iconId);
131 }
132 }
Chris Wren6d0dde02014-02-10 12:16:54 -0500133
Michael Jurkac9a96192010-11-01 11:52:08 -0700134 return getFullResDefaultActivityIcon();
135 }
136
Romain Guya28fd3f2010-03-15 14:44:42 -0700137 private Bitmap makeDefaultIcon() {
Michael Jurkac9a96192010-11-01 11:52:08 -0700138 Drawable d = getFullResDefaultActivityIcon();
Romain Guya28fd3f2010-03-15 14:44:42 -0700139 Bitmap b = Bitmap.createBitmap(Math.max(d.getIntrinsicWidth(), 1),
140 Math.max(d.getIntrinsicHeight(), 1),
141 Bitmap.Config.ARGB_8888);
142 Canvas c = new Canvas(b);
143 d.setBounds(0, 0, b.getWidth(), b.getHeight());
144 d.draw(c);
Adam Cohenaaf473c2011-08-03 12:02:47 -0700145 c.setBitmap(null);
Romain Guya28fd3f2010-03-15 14:44:42 -0700146 return b;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800147 }
148
149 /**
150 * Remove any records for the supplied ComponentName.
151 */
152 public void remove(ComponentName componentName) {
153 synchronized (mCache) {
154 mCache.remove(componentName);
155 }
156 }
157
158 /**
Chris Wren6d0dde02014-02-10 12:16:54 -0500159 * Remove any records for the supplied package name.
160 */
161 public void remove(String packageName) {
162 HashSet<ComponentName> forDeletion = new HashSet<ComponentName>();
163 for (ComponentName componentName: mCache.keySet()) {
164 if (componentName.getPackageName().equals(packageName)) {
165 forDeletion.add(componentName);
166 }
167 }
168 for (ComponentName condemned: forDeletion) {
169 remove(condemned);
170 }
171 }
172
173 /**
Joe Onorato0589f0f2010-02-08 13:44:00 -0800174 * Empty out the cache.
175 */
176 public void flush() {
177 synchronized (mCache) {
178 mCache.clear();
179 }
180 }
181
182 /**
Winson Chunge5467dc2013-10-14 17:03:04 -0700183 * Empty out the cache that aren't of the correct grid size
184 */
185 public void flushInvalidIcons(DeviceProfile grid) {
186 synchronized (mCache) {
Adam Cohenb6d33df2013-10-15 10:18:02 -0700187 Iterator<Entry<ComponentName, CacheEntry>> it = mCache.entrySet().iterator();
188 while (it.hasNext()) {
189 final CacheEntry e = it.next().getValue();
Winson Chung6e1c0d32013-10-25 15:24:24 -0700190 if (e.icon.getWidth() < grid.iconSizePx || e.icon.getHeight() < grid.iconSizePx) {
Adam Cohenb6d33df2013-10-15 10:18:02 -0700191 it.remove();
Winson Chunge5467dc2013-10-14 17:03:04 -0700192 }
193 }
194 }
195 }
196
197 /**
Joe Onorato0589f0f2010-02-08 13:44:00 -0800198 * Fill in "application" with the icon and label for "info."
199 */
Michael Jurkaeadbfc52013-09-04 00:45:37 +0200200 public void getTitleAndIcon(AppInfo application, ResolveInfo info,
Winson Chungc3eecff2011-07-11 17:44:15 -0700201 HashMap<Object, CharSequence> labelCache) {
Joe Onorato0589f0f2010-02-08 13:44:00 -0800202 synchronized (mCache) {
Winson Chungc3eecff2011-07-11 17:44:15 -0700203 CacheEntry entry = cacheLocked(application.componentName, info, labelCache);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800204
205 application.title = entry.title;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800206 application.iconBitmap = entry.icon;
207 }
208 }
209
210 public Bitmap getIcon(Intent intent) {
Chris Wren6d0dde02014-02-10 12:16:54 -0500211 return getIcon(intent, null);
212 }
213
214 public Bitmap getIcon(Intent intent, String title) {
Joe Onoratofad1fb52010-05-04 12:12:41 -0700215 synchronized (mCache) {
216 final ResolveInfo resolveInfo = mPackageManager.resolveActivity(intent, 0);
217 ComponentName component = intent.getComponent();
Joe Onorato0589f0f2010-02-08 13:44:00 -0800218
Chris Wren6d0dde02014-02-10 12:16:54 -0500219 if (component == null) {
Joe Onoratofad1fb52010-05-04 12:12:41 -0700220 return mDefaultIcon;
221 }
222
Winson Chungc3eecff2011-07-11 17:44:15 -0700223 CacheEntry entry = cacheLocked(component, resolveInfo, null);
Chris Wren6d0dde02014-02-10 12:16:54 -0500224 if (title != null) {
225 entry.title = title;
226 }
Joe Onoratofad1fb52010-05-04 12:12:41 -0700227 return entry.icon;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800228 }
Joe Onorato0589f0f2010-02-08 13:44:00 -0800229 }
230
Winson Chungaac01e12011-08-17 10:37:13 -0700231 public Bitmap getIcon(ComponentName component, ResolveInfo resolveInfo,
232 HashMap<Object, CharSequence> labelCache) {
Joe Onoratofad1fb52010-05-04 12:12:41 -0700233 synchronized (mCache) {
234 if (resolveInfo == null || component == null) {
235 return null;
236 }
Joe Onorato0589f0f2010-02-08 13:44:00 -0800237
Winson Chungaac01e12011-08-17 10:37:13 -0700238 CacheEntry entry = cacheLocked(component, resolveInfo, labelCache);
Joe Onoratofad1fb52010-05-04 12:12:41 -0700239 return entry.icon;
240 }
Joe Onorato0589f0f2010-02-08 13:44:00 -0800241 }
242
Joe Onoratoddc9c1f2010-08-30 18:30:15 -0700243 public boolean isDefaultIcon(Bitmap icon) {
244 return mDefaultIcon == icon;
245 }
246
Winson Chungc3eecff2011-07-11 17:44:15 -0700247 private CacheEntry cacheLocked(ComponentName componentName, ResolveInfo info,
248 HashMap<Object, CharSequence> labelCache) {
Joe Onorato0589f0f2010-02-08 13:44:00 -0800249 CacheEntry entry = mCache.get(componentName);
250 if (entry == null) {
251 entry = new CacheEntry();
252
Joe Onorato84f6a8d2010-02-12 17:53:35 -0500253 mCache.put(componentName, entry);
254
Chris Wren6d0dde02014-02-10 12:16:54 -0500255 if (info != null) {
256 ComponentName key = LauncherModel.getComponentNameFromResolveInfo(info);
257 if (labelCache != null && labelCache.containsKey(key)) {
258 entry.title = labelCache.get(key).toString();
259 } else {
260 entry.title = info.loadLabel(mPackageManager).toString();
261 if (labelCache != null) {
262 labelCache.put(key, entry.title);
263 }
264 }
265 if (entry.title == null) {
266 entry.title = info.activityInfo.name;
267 }
268
269 entry.icon = Utilities.createIconBitmap(
270 getFullResIcon(info), mContext);
Winson Chungc3eecff2011-07-11 17:44:15 -0700271 } else {
Chris Wren6d0dde02014-02-10 12:16:54 -0500272 entry.title = "";
273 Bitmap preloaded = getPreloadedIcon(componentName);
274 if (preloaded != null) {
275 if (DEBUG) Log.d(TAG, "using preloaded icon for " +
276 componentName.toShortString());
277 entry.icon = preloaded;
278 } else {
279 if (DEBUG) Log.d(TAG, "using default icon for " +
280 componentName.toShortString());
281 entry.icon = mDefaultIcon;
Winson Chungc3eecff2011-07-11 17:44:15 -0700282 }
283 }
Joe Onorato0589f0f2010-02-08 13:44:00 -0800284 }
285 return entry;
286 }
Daniel Sandler4e1cd232011-05-12 00:06:32 -0400287
288 public HashMap<ComponentName,Bitmap> getAllIcons() {
289 synchronized (mCache) {
290 HashMap<ComponentName,Bitmap> set = new HashMap<ComponentName,Bitmap>();
Daniel Sandler4e1cd232011-05-12 00:06:32 -0400291 for (ComponentName cn : mCache.keySet()) {
292 final CacheEntry e = mCache.get(cn);
293 set.put(cn, e.icon);
294 }
295 return set;
296 }
297 }
Chris Wren6d0dde02014-02-10 12:16:54 -0500298
299 /**
300 * Pre-load an icon into the persistent cache.
301 *
302 * <P>Queries for a component that does not exist in the package manager
303 * will be answered by the persistent cache.
304 *
305 * @param context application context
306 * @param componentName the icon should be returned for this component
307 * @param icon the icon to be persisted
308 * @param dpi the native density of the icon
309 */
310 public static void preloadIcon(Context context, ComponentName componentName, Bitmap icon,
311 int dpi) {
312 // TODO rescale to the correct native DPI
313 try {
314 PackageManager packageManager = context.getPackageManager();
315 packageManager.getActivityIcon(componentName);
316 // component is present on the system already, do nothing
317 return;
318 } catch (PackageManager.NameNotFoundException e) {
319 // pass
320 }
321
322 final String key = componentName.flattenToString();
323 FileOutputStream resourceFile = null;
324 try {
325 resourceFile = context.openFileOutput(getResourceFilename(componentName),
326 Context.MODE_PRIVATE);
327 ByteArrayOutputStream os = new ByteArrayOutputStream();
328 if (icon.compress(android.graphics.Bitmap.CompressFormat.PNG, 75, os)) {
329 byte[] buffer = os.toByteArray();
330 resourceFile.write(buffer, 0, buffer.length);
331 } else {
332 Log.w(TAG, "failed to encode cache for " + key);
333 return;
334 }
335 } catch (FileNotFoundException e) {
336 Log.w(TAG, "failed to pre-load cache for " + key, e);
337 } catch (IOException e) {
338 Log.w(TAG, "failed to pre-load cache for " + key, e);
339 } finally {
340 if (resourceFile != null) {
341 try {
342 resourceFile.close();
343 } catch (IOException e) {
344 Log.d(TAG, "failed to save restored icon for: " + key, e);
345 }
346 }
347 }
348 }
349
350 /**
351 * Read a pre-loaded icon from the persistent icon cache.
352 *
353 * @param componentName the component that should own the icon
354 * @returns a bitmap if one is cached, or null.
355 */
356 private Bitmap getPreloadedIcon(ComponentName componentName) {
357 final String key = componentName.flattenToShortString();
358
359 if (DEBUG) Log.v(TAG, "looking for pre-load icon for " + key);
360 Bitmap icon = null;
361 FileInputStream resourceFile = null;
362 try {
363 resourceFile = mContext.openFileInput(getResourceFilename(componentName));
364 byte[] buffer = new byte[1024];
365 ByteArrayOutputStream bytes = new ByteArrayOutputStream();
366 int bytesRead = 0;
367 while(bytesRead >= 0) {
368 bytes.write(buffer, 0, bytesRead);
369 bytesRead = resourceFile.read(buffer, 0, buffer.length);
370 }
371 if (DEBUG) Log.d(TAG, "read " + bytes.size());
372 icon = BitmapFactory.decodeByteArray(bytes.toByteArray(), 0, bytes.size());
373 if (icon == null) {
374 Log.w(TAG, "failed to decode pre-load icon for " + key);
375 }
376 } catch (FileNotFoundException e) {
377 if (DEBUG) Log.d(TAG, "there is no restored icon for: " + key, e);
378 } catch (IOException e) {
379 Log.w(TAG, "failed to read pre-load icon for: " + key, e);
380 } finally {
381 if(resourceFile != null) {
382 try {
383 resourceFile.close();
384 } catch (IOException e) {
385 Log.d(TAG, "failed to manage pre-load icon file: " + key, e);
386 }
387 }
388 }
389
390 if (icon != null) {
391 // TODO: handle alpha mask in the view layer
392 Bitmap b = Bitmap.createBitmap(Math.max(icon.getWidth(), 1),
393 Math.max(icon.getHeight(), 1),
394 Bitmap.Config.ARGB_8888);
395 Canvas c = new Canvas(b);
396 Paint paint = new Paint();
397 paint.setAlpha(127);
398 c.drawBitmap(icon, 0, 0, paint);
399 c.setBitmap(null);
400 icon.recycle();
401 icon = b;
402 }
403
404 return icon;
405 }
406
407 /**
408 * Remove a pre-loaded icon from the persistent icon cache.
409 *
410 * @param componentName the component that should own the icon
411 * @returns true on success
412 */
413 public boolean deletePreloadedIcon(ComponentName componentName) {
414 if (componentName == null) {
415 return false;
416 }
417 if (mCache.remove(componentName) != null) {
418 if (DEBUG) Log.d(TAG, "removed pre-loaded icon from the in-memory cache");
419 }
420 boolean success = mContext.deleteFile(getResourceFilename(componentName));
421 if (DEBUG && success) Log.d(TAG, "removed pre-loaded icon from persistent cache");
422
423 return success;
424 }
425
426 private static String getResourceFilename(ComponentName component) {
427 String resourceName = component.flattenToShortString();
428 String filename = resourceName.replace(File.separatorChar, '_');
429 return RESOURCE_FILE_PREFIX + filename;
430 }
Joe Onorato0589f0f2010-02-08 13:44:00 -0800431}