blob: 7d8628d9deb1283c0810d1adc2d2cd759d8aef12 [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;
Kenny Guyed131872014-04-30 03:02:21 +010034import android.os.Build;
Chris Wren6d0dde02014-02-10 12:16:54 -050035import android.util.Log;
Joe Onorato0589f0f2010-02-08 13:44:00 -080036
Kenny Guyed131872014-04-30 03:02:21 +010037import com.android.launcher3.compat.LauncherActivityInfoCompat;
38import com.android.launcher3.compat.LauncherAppsCompat;
39import com.android.launcher3.compat.UserHandleCompat;
40import com.android.launcher3.compat.UserManagerCompat;
41
Chris Wren6d0dde02014-02-10 12:16:54 -050042import java.io.ByteArrayOutputStream;
43import java.io.File;
44import java.io.FileInputStream;
45import java.io.FileNotFoundException;
46import java.io.FileOutputStream;
47import java.io.IOException;
Joe Onorato0589f0f2010-02-08 13:44:00 -080048import java.util.HashMap;
Chris Wren6d0dde02014-02-10 12:16:54 -050049import java.util.HashSet;
Adam Cohenb6d33df2013-10-15 10:18:02 -070050import java.util.Iterator;
51import java.util.Map.Entry;
Joe Onorato0589f0f2010-02-08 13:44:00 -080052
53/**
54 * Cache of application icons. Icons can be made from any thread.
55 */
56public class IconCache {
Michael Jurka3a9fced2012-04-13 14:44:29 -070057 @SuppressWarnings("unused")
Joe Onorato0589f0f2010-02-08 13:44:00 -080058 private static final String TAG = "Launcher.IconCache";
59
60 private static final int INITIAL_ICON_CACHE_CAPACITY = 50;
Chris Wren6d0dde02014-02-10 12:16:54 -050061 private static final String RESOURCE_FILE_PREFIX = "icon_";
62
63 private static final boolean DEBUG = true;
Joe Onorato0589f0f2010-02-08 13:44:00 -080064
65 private static class CacheEntry {
66 public Bitmap icon;
67 public String title;
Kenny Guyc2bd8102014-06-30 12:30:31 +010068 public String contentDescription;
Joe Onorato0589f0f2010-02-08 13:44:00 -080069 }
70
Kenny Guyed131872014-04-30 03:02:21 +010071 private static class CacheKey {
72 public ComponentName componentName;
73 public UserHandleCompat user;
74
75 CacheKey(ComponentName componentName, UserHandleCompat user) {
76 this.componentName = componentName;
77 this.user = user;
78 }
79
80 @Override
81 public int hashCode() {
82 return componentName.hashCode() + user.hashCode();
83 }
84
85 @Override
86 public boolean equals(Object o) {
87 CacheKey other = (CacheKey) o;
88 return other.componentName.equals(componentName) && other.user.equals(user);
89 }
90 }
91
92 private final HashMap<UserHandleCompat, Bitmap> mDefaultIcons =
93 new HashMap<UserHandleCompat, Bitmap>();
Daniel Sandlercc8befa2013-06-11 14:45:48 -040094 private final Context mContext;
Romain Guya28fd3f2010-03-15 14:44:42 -070095 private final PackageManager mPackageManager;
Kenny Guyed131872014-04-30 03:02:21 +010096 private final UserManagerCompat mUserManager;
97 private final LauncherAppsCompat mLauncherApps;
98 private final HashMap<CacheKey, CacheEntry> mCache =
99 new HashMap<CacheKey, CacheEntry>(INITIAL_ICON_CACHE_CAPACITY);
Michael Jurkac9a96192010-11-01 11:52:08 -0700100 private int mIconDpi;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800101
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400102 public IconCache(Context context) {
Winson Chungd83f5f42012-02-13 14:27:42 -0800103 ActivityManager activityManager =
104 (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
105
Joe Onorato0589f0f2010-02-08 13:44:00 -0800106 mContext = context;
107 mPackageManager = context.getPackageManager();
Kenny Guyed131872014-04-30 03:02:21 +0100108 mUserManager = UserManagerCompat.getInstance(mContext);
109 mLauncherApps = LauncherAppsCompat.getInstance(mContext);
Winson Chungd83f5f42012-02-13 14:27:42 -0800110 mIconDpi = activityManager.getLauncherLargeIconDensity();
111
Michael Jurkac9a96192010-11-01 11:52:08 -0700112 // need to set mIconDpi before getting default icon
Kenny Guyed131872014-04-30 03:02:21 +0100113 UserHandleCompat myUser = UserHandleCompat.myUserHandle();
114 mDefaultIcons.put(myUser, makeDefaultIcon(myUser));
Romain Guya28fd3f2010-03-15 14:44:42 -0700115 }
116
Michael Jurkac9a96192010-11-01 11:52:08 -0700117 public Drawable getFullResDefaultActivityIcon() {
118 return getFullResIcon(Resources.getSystem(),
Michael Jurka8b805b12012-04-18 14:23:14 -0700119 android.R.mipmap.sym_def_app_icon);
Michael Jurkac9a96192010-11-01 11:52:08 -0700120 }
121
Michael Jurka4842ed02011-07-07 15:33:20 -0700122 public Drawable getFullResIcon(Resources resources, int iconId) {
Michael Jurka721d9722011-08-03 11:49:59 -0700123 Drawable d;
Michael Jurka4842ed02011-07-07 15:33:20 -0700124 try {
Michael Jurka721d9722011-08-03 11:49:59 -0700125 d = resources.getDrawableForDensity(iconId, mIconDpi);
Michael Jurka4842ed02011-07-07 15:33:20 -0700126 } catch (Resources.NotFoundException e) {
Michael Jurka721d9722011-08-03 11:49:59 -0700127 d = null;
Michael Jurka4842ed02011-07-07 15:33:20 -0700128 }
Michael Jurka721d9722011-08-03 11:49:59 -0700129
130 return (d != null) ? d : getFullResDefaultActivityIcon();
Michael Jurkac9a96192010-11-01 11:52:08 -0700131 }
132
Winson Chung0b9fcf52011-10-31 13:05:15 -0700133 public Drawable getFullResIcon(String packageName, int iconId) {
Michael Jurkac9a96192010-11-01 11:52:08 -0700134 Resources resources;
135 try {
Winson Chung0b9fcf52011-10-31 13:05:15 -0700136 resources = mPackageManager.getResourcesForApplication(packageName);
137 } catch (PackageManager.NameNotFoundException e) {
138 resources = null;
139 }
140 if (resources != null) {
141 if (iconId != 0) {
142 return getFullResIcon(resources, iconId);
143 }
144 }
145 return getFullResDefaultActivityIcon();
146 }
147
148 public Drawable getFullResIcon(ResolveInfo info) {
Michael Jurkadac85912012-05-18 15:04:49 -0700149 return getFullResIcon(info.activityInfo);
150 }
151
152 public Drawable getFullResIcon(ActivityInfo info) {
153
Winson Chung0b9fcf52011-10-31 13:05:15 -0700154 Resources resources;
155 try {
156 resources = mPackageManager.getResourcesForApplication(
Michael Jurkadac85912012-05-18 15:04:49 -0700157 info.applicationInfo);
Michael Jurkac9a96192010-11-01 11:52:08 -0700158 } catch (PackageManager.NameNotFoundException e) {
159 resources = null;
160 }
161 if (resources != null) {
Michael Jurkadac85912012-05-18 15:04:49 -0700162 int iconId = info.getIconResource();
Michael Jurkac9a96192010-11-01 11:52:08 -0700163 if (iconId != 0) {
164 return getFullResIcon(resources, iconId);
165 }
166 }
Chris Wren6d0dde02014-02-10 12:16:54 -0500167
Michael Jurkac9a96192010-11-01 11:52:08 -0700168 return getFullResDefaultActivityIcon();
169 }
170
Kenny Guyed131872014-04-30 03:02:21 +0100171 private Bitmap makeDefaultIcon(UserHandleCompat user) {
172 Drawable unbadged = getFullResDefaultActivityIcon();
173 Drawable d = mUserManager.getBadgedDrawableForUser(unbadged, user);
Romain Guya28fd3f2010-03-15 14:44:42 -0700174 Bitmap b = Bitmap.createBitmap(Math.max(d.getIntrinsicWidth(), 1),
175 Math.max(d.getIntrinsicHeight(), 1),
176 Bitmap.Config.ARGB_8888);
177 Canvas c = new Canvas(b);
178 d.setBounds(0, 0, b.getWidth(), b.getHeight());
179 d.draw(c);
Adam Cohenaaf473c2011-08-03 12:02:47 -0700180 c.setBitmap(null);
Romain Guya28fd3f2010-03-15 14:44:42 -0700181 return b;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800182 }
183
184 /**
185 * Remove any records for the supplied ComponentName.
186 */
Kenny Guyed131872014-04-30 03:02:21 +0100187 public void remove(ComponentName componentName, UserHandleCompat user) {
Joe Onorato0589f0f2010-02-08 13:44:00 -0800188 synchronized (mCache) {
Kenny Guyed131872014-04-30 03:02:21 +0100189 mCache.remove(new CacheKey(componentName, user));
Joe Onorato0589f0f2010-02-08 13:44:00 -0800190 }
191 }
192
193 /**
Chris Wren6d0dde02014-02-10 12:16:54 -0500194 * Remove any records for the supplied package name.
195 */
Kenny Guyed131872014-04-30 03:02:21 +0100196 public void remove(String packageName, UserHandleCompat user) {
197 HashSet<CacheKey> forDeletion = new HashSet<CacheKey>();
198 for (CacheKey key: mCache.keySet()) {
199 if (key.componentName.getPackageName().equals(packageName)
200 && key.user.equals(user)) {
201 forDeletion.add(key);
Chris Wren6d0dde02014-02-10 12:16:54 -0500202 }
203 }
Kenny Guyed131872014-04-30 03:02:21 +0100204 for (CacheKey condemned: forDeletion) {
205 mCache.remove(condemned);
Chris Wren6d0dde02014-02-10 12:16:54 -0500206 }
207 }
208
209 /**
Joe Onorato0589f0f2010-02-08 13:44:00 -0800210 * Empty out the cache.
211 */
212 public void flush() {
213 synchronized (mCache) {
214 mCache.clear();
215 }
216 }
217
218 /**
Winson Chunge5467dc2013-10-14 17:03:04 -0700219 * Empty out the cache that aren't of the correct grid size
220 */
221 public void flushInvalidIcons(DeviceProfile grid) {
222 synchronized (mCache) {
Kenny Guyed131872014-04-30 03:02:21 +0100223 Iterator<Entry<CacheKey, CacheEntry>> it = mCache.entrySet().iterator();
Adam Cohenb6d33df2013-10-15 10:18:02 -0700224 while (it.hasNext()) {
225 final CacheEntry e = it.next().getValue();
Winson Chung6e1c0d32013-10-25 15:24:24 -0700226 if (e.icon.getWidth() < grid.iconSizePx || e.icon.getHeight() < grid.iconSizePx) {
Adam Cohenb6d33df2013-10-15 10:18:02 -0700227 it.remove();
Winson Chunge5467dc2013-10-14 17:03:04 -0700228 }
229 }
230 }
231 }
232
233 /**
Joe Onorato0589f0f2010-02-08 13:44:00 -0800234 * Fill in "application" with the icon and label for "info."
235 */
Kenny Guyed131872014-04-30 03:02:21 +0100236 public void getTitleAndIcon(AppInfo application, LauncherActivityInfoCompat info,
Winson Chungc3eecff2011-07-11 17:44:15 -0700237 HashMap<Object, CharSequence> labelCache) {
Joe Onorato0589f0f2010-02-08 13:44:00 -0800238 synchronized (mCache) {
Kenny Guyed131872014-04-30 03:02:21 +0100239 CacheEntry entry = cacheLocked(application.componentName, info, labelCache,
240 info.getUser());
Joe Onorato0589f0f2010-02-08 13:44:00 -0800241
242 application.title = entry.title;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800243 application.iconBitmap = entry.icon;
Kenny Guyc2bd8102014-06-30 12:30:31 +0100244 application.contentDescription = entry.contentDescription;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800245 }
246 }
247
Kenny Guyed131872014-04-30 03:02:21 +0100248 public Bitmap getIcon(Intent intent, UserHandleCompat user) {
249 return getIcon(intent, null, user);
Chris Wren6d0dde02014-02-10 12:16:54 -0500250 }
251
Kenny Guyed131872014-04-30 03:02:21 +0100252 public Bitmap getIcon(Intent intent, String title, UserHandleCompat user) {
Joe Onoratofad1fb52010-05-04 12:12:41 -0700253 synchronized (mCache) {
Chris Wren075f9f52014-05-13 16:18:21 -0400254 LauncherActivityInfoCompat launcherActInfo =
Kenny Guyed131872014-04-30 03:02:21 +0100255 mLauncherApps.resolveActivity(intent, user);
Joe Onoratofad1fb52010-05-04 12:12:41 -0700256 ComponentName component = intent.getComponent();
Joe Onorato0589f0f2010-02-08 13:44:00 -0800257
Chris Wren075f9f52014-05-13 16:18:21 -0400258 // null info means not installed, but if we have a component from the intent then
259 // we should still look in the cache for restored app icons.
260 if (launcherActInfo == null && component == null) {
Kenny Guyed131872014-04-30 03:02:21 +0100261 return getDefaultIcon(user);
Joe Onoratofad1fb52010-05-04 12:12:41 -0700262 }
263
Kenny Guyed131872014-04-30 03:02:21 +0100264 CacheEntry entry = cacheLocked(component, launcherActInfo, null, user);
Chris Wren6d0dde02014-02-10 12:16:54 -0500265 if (title != null) {
266 entry.title = title;
Kenny Guyc2bd8102014-06-30 12:30:31 +0100267 entry.contentDescription = mUserManager.getBadgedLabelForUser(title, user);
Chris Wren6d0dde02014-02-10 12:16:54 -0500268 }
Joe Onoratofad1fb52010-05-04 12:12:41 -0700269 return entry.icon;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800270 }
Joe Onorato0589f0f2010-02-08 13:44:00 -0800271 }
272
Kenny Guyed131872014-04-30 03:02:21 +0100273 public Bitmap getDefaultIcon(UserHandleCompat user) {
274 if (!mDefaultIcons.containsKey(user)) {
275 mDefaultIcons.put(user, makeDefaultIcon(user));
276 }
277 return mDefaultIcons.get(user);
278 }
279
280 public Bitmap getIcon(ComponentName component, LauncherActivityInfoCompat info,
Winson Chungaac01e12011-08-17 10:37:13 -0700281 HashMap<Object, CharSequence> labelCache) {
Joe Onoratofad1fb52010-05-04 12:12:41 -0700282 synchronized (mCache) {
Kenny Guyed131872014-04-30 03:02:21 +0100283 if (info == null || component == null) {
Joe Onoratofad1fb52010-05-04 12:12:41 -0700284 return null;
285 }
Joe Onorato0589f0f2010-02-08 13:44:00 -0800286
Kenny Guyed131872014-04-30 03:02:21 +0100287 CacheEntry entry = cacheLocked(component, info, labelCache, info.getUser());
Joe Onoratofad1fb52010-05-04 12:12:41 -0700288 return entry.icon;
289 }
Joe Onorato0589f0f2010-02-08 13:44:00 -0800290 }
291
Kenny Guyed131872014-04-30 03:02:21 +0100292 public boolean isDefaultIcon(Bitmap icon, UserHandleCompat user) {
293 return mDefaultIcons.get(user) == icon;
Joe Onoratoddc9c1f2010-08-30 18:30:15 -0700294 }
295
Kenny Guyed131872014-04-30 03:02:21 +0100296 private CacheEntry cacheLocked(ComponentName componentName, LauncherActivityInfoCompat info,
297 HashMap<Object, CharSequence> labelCache, UserHandleCompat user) {
298 CacheKey cacheKey = new CacheKey(componentName, user);
299 CacheEntry entry = mCache.get(cacheKey);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800300 if (entry == null) {
301 entry = new CacheEntry();
302
Kenny Guyed131872014-04-30 03:02:21 +0100303 mCache.put(cacheKey, entry);
Joe Onorato84f6a8d2010-02-12 17:53:35 -0500304
Chris Wren6d0dde02014-02-10 12:16:54 -0500305 if (info != null) {
Kenny Guyed131872014-04-30 03:02:21 +0100306 ComponentName labelKey = info.getComponentName();
307 if (labelCache != null && labelCache.containsKey(labelKey)) {
308 entry.title = labelCache.get(labelKey).toString();
Chris Wren6d0dde02014-02-10 12:16:54 -0500309 } else {
Kenny Guyed131872014-04-30 03:02:21 +0100310 entry.title = info.getLabel().toString();
Chris Wren6d0dde02014-02-10 12:16:54 -0500311 if (labelCache != null) {
Kenny Guyed131872014-04-30 03:02:21 +0100312 labelCache.put(labelKey, entry.title);
Chris Wren6d0dde02014-02-10 12:16:54 -0500313 }
314 }
Chris Wren6d0dde02014-02-10 12:16:54 -0500315
Kenny Guyc2bd8102014-06-30 12:30:31 +0100316 entry.contentDescription = mUserManager.getBadgedLabelForUser(entry.title, user);
Chris Wren6d0dde02014-02-10 12:16:54 -0500317 entry.icon = Utilities.createIconBitmap(
Kenny Guyed131872014-04-30 03:02:21 +0100318 info.getBadgedIcon(mIconDpi), mContext);
Winson Chungc3eecff2011-07-11 17:44:15 -0700319 } else {
Chris Wren6d0dde02014-02-10 12:16:54 -0500320 entry.title = "";
Kenny Guyed131872014-04-30 03:02:21 +0100321 Bitmap preloaded = getPreloadedIcon(componentName, user);
Chris Wren6d0dde02014-02-10 12:16:54 -0500322 if (preloaded != null) {
323 if (DEBUG) Log.d(TAG, "using preloaded icon for " +
324 componentName.toShortString());
325 entry.icon = preloaded;
326 } else {
327 if (DEBUG) Log.d(TAG, "using default icon for " +
328 componentName.toShortString());
Kenny Guyed131872014-04-30 03:02:21 +0100329 entry.icon = getDefaultIcon(user);
Winson Chungc3eecff2011-07-11 17:44:15 -0700330 }
331 }
Joe Onorato0589f0f2010-02-08 13:44:00 -0800332 }
333 return entry;
334 }
Daniel Sandler4e1cd232011-05-12 00:06:32 -0400335
336 public HashMap<ComponentName,Bitmap> getAllIcons() {
337 synchronized (mCache) {
338 HashMap<ComponentName,Bitmap> set = new HashMap<ComponentName,Bitmap>();
Kenny Guyed131872014-04-30 03:02:21 +0100339 for (CacheKey ck : mCache.keySet()) {
340 final CacheEntry e = mCache.get(ck);
341 set.put(ck.componentName, e.icon);
Daniel Sandler4e1cd232011-05-12 00:06:32 -0400342 }
343 return set;
344 }
345 }
Chris Wren6d0dde02014-02-10 12:16:54 -0500346
347 /**
348 * Pre-load an icon into the persistent cache.
349 *
350 * <P>Queries for a component that does not exist in the package manager
351 * will be answered by the persistent cache.
352 *
353 * @param context application context
354 * @param componentName the icon should be returned for this component
355 * @param icon the icon to be persisted
356 * @param dpi the native density of the icon
357 */
358 public static void preloadIcon(Context context, ComponentName componentName, Bitmap icon,
359 int dpi) {
360 // TODO rescale to the correct native DPI
361 try {
362 PackageManager packageManager = context.getPackageManager();
363 packageManager.getActivityIcon(componentName);
364 // component is present on the system already, do nothing
365 return;
366 } catch (PackageManager.NameNotFoundException e) {
367 // pass
368 }
369
370 final String key = componentName.flattenToString();
371 FileOutputStream resourceFile = null;
372 try {
373 resourceFile = context.openFileOutput(getResourceFilename(componentName),
374 Context.MODE_PRIVATE);
375 ByteArrayOutputStream os = new ByteArrayOutputStream();
376 if (icon.compress(android.graphics.Bitmap.CompressFormat.PNG, 75, os)) {
377 byte[] buffer = os.toByteArray();
378 resourceFile.write(buffer, 0, buffer.length);
379 } else {
380 Log.w(TAG, "failed to encode cache for " + key);
381 return;
382 }
383 } catch (FileNotFoundException e) {
384 Log.w(TAG, "failed to pre-load cache for " + key, e);
385 } catch (IOException e) {
386 Log.w(TAG, "failed to pre-load cache for " + key, e);
387 } finally {
388 if (resourceFile != null) {
389 try {
390 resourceFile.close();
391 } catch (IOException e) {
392 Log.d(TAG, "failed to save restored icon for: " + key, e);
393 }
394 }
395 }
396 }
397
398 /**
399 * Read a pre-loaded icon from the persistent icon cache.
400 *
401 * @param componentName the component that should own the icon
402 * @returns a bitmap if one is cached, or null.
403 */
Kenny Guyed131872014-04-30 03:02:21 +0100404 private Bitmap getPreloadedIcon(ComponentName componentName, UserHandleCompat user) {
Chris Wren6d0dde02014-02-10 12:16:54 -0500405 final String key = componentName.flattenToShortString();
406
Kenny Guyed131872014-04-30 03:02:21 +0100407 // We don't keep icons for other profiles in persistent cache.
408 if (!user.equals(UserHandleCompat.myUserHandle())) {
409 return null;
410 }
411
Chris Wren6d0dde02014-02-10 12:16:54 -0500412 if (DEBUG) Log.v(TAG, "looking for pre-load icon for " + key);
413 Bitmap icon = null;
414 FileInputStream resourceFile = null;
415 try {
416 resourceFile = mContext.openFileInput(getResourceFilename(componentName));
417 byte[] buffer = new byte[1024];
418 ByteArrayOutputStream bytes = new ByteArrayOutputStream();
419 int bytesRead = 0;
420 while(bytesRead >= 0) {
421 bytes.write(buffer, 0, bytesRead);
422 bytesRead = resourceFile.read(buffer, 0, buffer.length);
423 }
424 if (DEBUG) Log.d(TAG, "read " + bytes.size());
425 icon = BitmapFactory.decodeByteArray(bytes.toByteArray(), 0, bytes.size());
426 if (icon == null) {
427 Log.w(TAG, "failed to decode pre-load icon for " + key);
428 }
429 } catch (FileNotFoundException e) {
430 if (DEBUG) Log.d(TAG, "there is no restored icon for: " + key, e);
431 } catch (IOException e) {
432 Log.w(TAG, "failed to read pre-load icon for: " + key, e);
433 } finally {
434 if(resourceFile != null) {
435 try {
436 resourceFile.close();
437 } catch (IOException e) {
438 Log.d(TAG, "failed to manage pre-load icon file: " + key, e);
439 }
440 }
441 }
442
Chris Wren6d0dde02014-02-10 12:16:54 -0500443 return icon;
444 }
445
446 /**
447 * Remove a pre-loaded icon from the persistent icon cache.
448 *
449 * @param componentName the component that should own the icon
450 * @returns true on success
451 */
Kenny Guyed131872014-04-30 03:02:21 +0100452 public boolean deletePreloadedIcon(ComponentName componentName, UserHandleCompat user) {
453 // We don't keep icons for other profiles in persistent cache.
454 if (!user.equals(UserHandleCompat.myUserHandle())) {
455 return false;
456 }
Chris Wren6d0dde02014-02-10 12:16:54 -0500457 if (componentName == null) {
458 return false;
459 }
460 if (mCache.remove(componentName) != null) {
461 if (DEBUG) Log.d(TAG, "removed pre-loaded icon from the in-memory cache");
462 }
463 boolean success = mContext.deleteFile(getResourceFilename(componentName));
464 if (DEBUG && success) Log.d(TAG, "removed pre-loaded icon from persistent cache");
465
466 return success;
467 }
468
469 private static String getResourceFilename(ComponentName component) {
470 String resourceName = component.flattenToShortString();
471 String filename = resourceName.replace(File.separatorChar, '_');
472 return RESOURCE_FILE_PREFIX + filename;
473 }
Joe Onorato0589f0f2010-02-08 13:44:00 -0800474}