blob: 05be2141e815725f5a2e68f452d4750ec237e9fc [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;
Joe Onorato0589f0f2010-02-08 13:44:00 -080068 }
69
Kenny Guyed131872014-04-30 03:02:21 +010070 private static class CacheKey {
71 public ComponentName componentName;
72 public UserHandleCompat user;
73
74 CacheKey(ComponentName componentName, UserHandleCompat user) {
75 this.componentName = componentName;
76 this.user = user;
77 }
78
79 @Override
80 public int hashCode() {
81 return componentName.hashCode() + user.hashCode();
82 }
83
84 @Override
85 public boolean equals(Object o) {
86 CacheKey other = (CacheKey) o;
87 return other.componentName.equals(componentName) && other.user.equals(user);
88 }
89 }
90
91 private final HashMap<UserHandleCompat, Bitmap> mDefaultIcons =
92 new HashMap<UserHandleCompat, Bitmap>();
Daniel Sandlercc8befa2013-06-11 14:45:48 -040093 private final Context mContext;
Romain Guya28fd3f2010-03-15 14:44:42 -070094 private final PackageManager mPackageManager;
Kenny Guyed131872014-04-30 03:02:21 +010095 private final UserManagerCompat mUserManager;
96 private final LauncherAppsCompat mLauncherApps;
97 private final HashMap<CacheKey, CacheEntry> mCache =
98 new HashMap<CacheKey, CacheEntry>(INITIAL_ICON_CACHE_CAPACITY);
Michael Jurkac9a96192010-11-01 11:52:08 -070099 private int mIconDpi;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800100
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400101 public IconCache(Context context) {
Winson Chungd83f5f42012-02-13 14:27:42 -0800102 ActivityManager activityManager =
103 (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
104
Joe Onorato0589f0f2010-02-08 13:44:00 -0800105 mContext = context;
106 mPackageManager = context.getPackageManager();
Kenny Guyed131872014-04-30 03:02:21 +0100107 mUserManager = UserManagerCompat.getInstance(mContext);
108 mLauncherApps = LauncherAppsCompat.getInstance(mContext);
Winson Chungd83f5f42012-02-13 14:27:42 -0800109 mIconDpi = activityManager.getLauncherLargeIconDensity();
110
Michael Jurkac9a96192010-11-01 11:52:08 -0700111 // need to set mIconDpi before getting default icon
Kenny Guyed131872014-04-30 03:02:21 +0100112 UserHandleCompat myUser = UserHandleCompat.myUserHandle();
113 mDefaultIcons.put(myUser, makeDefaultIcon(myUser));
Romain Guya28fd3f2010-03-15 14:44:42 -0700114 }
115
Michael Jurkac9a96192010-11-01 11:52:08 -0700116 public Drawable getFullResDefaultActivityIcon() {
117 return getFullResIcon(Resources.getSystem(),
Michael Jurka8b805b12012-04-18 14:23:14 -0700118 android.R.mipmap.sym_def_app_icon);
Michael Jurkac9a96192010-11-01 11:52:08 -0700119 }
120
Michael Jurka4842ed02011-07-07 15:33:20 -0700121 public Drawable getFullResIcon(Resources resources, int iconId) {
Michael Jurka721d9722011-08-03 11:49:59 -0700122 Drawable d;
Michael Jurka4842ed02011-07-07 15:33:20 -0700123 try {
Michael Jurka721d9722011-08-03 11:49:59 -0700124 d = resources.getDrawableForDensity(iconId, mIconDpi);
Michael Jurka4842ed02011-07-07 15:33:20 -0700125 } catch (Resources.NotFoundException e) {
Michael Jurka721d9722011-08-03 11:49:59 -0700126 d = null;
Michael Jurka4842ed02011-07-07 15:33:20 -0700127 }
Michael Jurka721d9722011-08-03 11:49:59 -0700128
129 return (d != null) ? d : getFullResDefaultActivityIcon();
Michael Jurkac9a96192010-11-01 11:52:08 -0700130 }
131
Winson Chung0b9fcf52011-10-31 13:05:15 -0700132 public Drawable getFullResIcon(String packageName, int iconId) {
Michael Jurkac9a96192010-11-01 11:52:08 -0700133 Resources resources;
134 try {
Winson Chung0b9fcf52011-10-31 13:05:15 -0700135 resources = mPackageManager.getResourcesForApplication(packageName);
136 } catch (PackageManager.NameNotFoundException e) {
137 resources = null;
138 }
139 if (resources != null) {
140 if (iconId != 0) {
141 return getFullResIcon(resources, iconId);
142 }
143 }
144 return getFullResDefaultActivityIcon();
145 }
146
147 public Drawable getFullResIcon(ResolveInfo info) {
Michael Jurkadac85912012-05-18 15:04:49 -0700148 return getFullResIcon(info.activityInfo);
149 }
150
151 public Drawable getFullResIcon(ActivityInfo info) {
152
Winson Chung0b9fcf52011-10-31 13:05:15 -0700153 Resources resources;
154 try {
155 resources = mPackageManager.getResourcesForApplication(
Michael Jurkadac85912012-05-18 15:04:49 -0700156 info.applicationInfo);
Michael Jurkac9a96192010-11-01 11:52:08 -0700157 } catch (PackageManager.NameNotFoundException e) {
158 resources = null;
159 }
160 if (resources != null) {
Michael Jurkadac85912012-05-18 15:04:49 -0700161 int iconId = info.getIconResource();
Michael Jurkac9a96192010-11-01 11:52:08 -0700162 if (iconId != 0) {
163 return getFullResIcon(resources, iconId);
164 }
165 }
Chris Wren6d0dde02014-02-10 12:16:54 -0500166
Michael Jurkac9a96192010-11-01 11:52:08 -0700167 return getFullResDefaultActivityIcon();
168 }
169
Kenny Guyed131872014-04-30 03:02:21 +0100170 private Bitmap makeDefaultIcon(UserHandleCompat user) {
171 Drawable unbadged = getFullResDefaultActivityIcon();
172 Drawable d = mUserManager.getBadgedDrawableForUser(unbadged, user);
Romain Guya28fd3f2010-03-15 14:44:42 -0700173 Bitmap b = Bitmap.createBitmap(Math.max(d.getIntrinsicWidth(), 1),
174 Math.max(d.getIntrinsicHeight(), 1),
175 Bitmap.Config.ARGB_8888);
176 Canvas c = new Canvas(b);
177 d.setBounds(0, 0, b.getWidth(), b.getHeight());
178 d.draw(c);
Adam Cohenaaf473c2011-08-03 12:02:47 -0700179 c.setBitmap(null);
Romain Guya28fd3f2010-03-15 14:44:42 -0700180 return b;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800181 }
182
183 /**
184 * Remove any records for the supplied ComponentName.
185 */
Kenny Guyed131872014-04-30 03:02:21 +0100186 public void remove(ComponentName componentName, UserHandleCompat user) {
Joe Onorato0589f0f2010-02-08 13:44:00 -0800187 synchronized (mCache) {
Kenny Guyed131872014-04-30 03:02:21 +0100188 mCache.remove(new CacheKey(componentName, user));
Joe Onorato0589f0f2010-02-08 13:44:00 -0800189 }
190 }
191
192 /**
Chris Wren6d0dde02014-02-10 12:16:54 -0500193 * Remove any records for the supplied package name.
194 */
Kenny Guyed131872014-04-30 03:02:21 +0100195 public void remove(String packageName, UserHandleCompat user) {
196 HashSet<CacheKey> forDeletion = new HashSet<CacheKey>();
197 for (CacheKey key: mCache.keySet()) {
198 if (key.componentName.getPackageName().equals(packageName)
199 && key.user.equals(user)) {
200 forDeletion.add(key);
Chris Wren6d0dde02014-02-10 12:16:54 -0500201 }
202 }
Kenny Guyed131872014-04-30 03:02:21 +0100203 for (CacheKey condemned: forDeletion) {
204 mCache.remove(condemned);
Chris Wren6d0dde02014-02-10 12:16:54 -0500205 }
206 }
207
208 /**
Joe Onorato0589f0f2010-02-08 13:44:00 -0800209 * Empty out the cache.
210 */
211 public void flush() {
212 synchronized (mCache) {
213 mCache.clear();
214 }
215 }
216
217 /**
Winson Chunge5467dc2013-10-14 17:03:04 -0700218 * Empty out the cache that aren't of the correct grid size
219 */
220 public void flushInvalidIcons(DeviceProfile grid) {
221 synchronized (mCache) {
Kenny Guyed131872014-04-30 03:02:21 +0100222 Iterator<Entry<CacheKey, CacheEntry>> it = mCache.entrySet().iterator();
Adam Cohenb6d33df2013-10-15 10:18:02 -0700223 while (it.hasNext()) {
224 final CacheEntry e = it.next().getValue();
Winson Chung6e1c0d32013-10-25 15:24:24 -0700225 if (e.icon.getWidth() < grid.iconSizePx || e.icon.getHeight() < grid.iconSizePx) {
Adam Cohenb6d33df2013-10-15 10:18:02 -0700226 it.remove();
Winson Chunge5467dc2013-10-14 17:03:04 -0700227 }
228 }
229 }
230 }
231
232 /**
Joe Onorato0589f0f2010-02-08 13:44:00 -0800233 * Fill in "application" with the icon and label for "info."
234 */
Kenny Guyed131872014-04-30 03:02:21 +0100235 public void getTitleAndIcon(AppInfo application, LauncherActivityInfoCompat info,
Winson Chungc3eecff2011-07-11 17:44:15 -0700236 HashMap<Object, CharSequence> labelCache) {
Joe Onorato0589f0f2010-02-08 13:44:00 -0800237 synchronized (mCache) {
Kenny Guyed131872014-04-30 03:02:21 +0100238 CacheEntry entry = cacheLocked(application.componentName, info, labelCache,
239 info.getUser());
Joe Onorato0589f0f2010-02-08 13:44:00 -0800240
241 application.title = entry.title;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800242 application.iconBitmap = entry.icon;
243 }
244 }
245
Kenny Guyed131872014-04-30 03:02:21 +0100246 public Bitmap getIcon(Intent intent, UserHandleCompat user) {
247 return getIcon(intent, null, user);
Chris Wren6d0dde02014-02-10 12:16:54 -0500248 }
249
Kenny Guyed131872014-04-30 03:02:21 +0100250 public Bitmap getIcon(Intent intent, String title, UserHandleCompat user) {
Joe Onoratofad1fb52010-05-04 12:12:41 -0700251 synchronized (mCache) {
Kenny Guyed131872014-04-30 03:02:21 +0100252 final LauncherActivityInfoCompat launcherActInfo =
253 mLauncherApps.resolveActivity(intent, user);
Joe Onoratofad1fb52010-05-04 12:12:41 -0700254 ComponentName component = intent.getComponent();
Joe Onorato0589f0f2010-02-08 13:44:00 -0800255
Kenny Guyed131872014-04-30 03:02:21 +0100256 if (launcherActInfo == null || component == null) {
257 return getDefaultIcon(user);
Joe Onoratofad1fb52010-05-04 12:12:41 -0700258 }
259
Kenny Guyed131872014-04-30 03:02:21 +0100260 CacheEntry entry = cacheLocked(component, launcherActInfo, null, user);
Chris Wren6d0dde02014-02-10 12:16:54 -0500261 if (title != null) {
262 entry.title = title;
263 }
Joe Onoratofad1fb52010-05-04 12:12:41 -0700264 return entry.icon;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800265 }
Joe Onorato0589f0f2010-02-08 13:44:00 -0800266 }
267
Kenny Guyed131872014-04-30 03:02:21 +0100268 public Bitmap getDefaultIcon(UserHandleCompat user) {
269 if (!mDefaultIcons.containsKey(user)) {
270 mDefaultIcons.put(user, makeDefaultIcon(user));
271 }
272 return mDefaultIcons.get(user);
273 }
274
275 public Bitmap getIcon(ComponentName component, LauncherActivityInfoCompat info,
Winson Chungaac01e12011-08-17 10:37:13 -0700276 HashMap<Object, CharSequence> labelCache) {
Joe Onoratofad1fb52010-05-04 12:12:41 -0700277 synchronized (mCache) {
Kenny Guyed131872014-04-30 03:02:21 +0100278 if (info == null || component == null) {
Joe Onoratofad1fb52010-05-04 12:12:41 -0700279 return null;
280 }
Joe Onorato0589f0f2010-02-08 13:44:00 -0800281
Kenny Guyed131872014-04-30 03:02:21 +0100282 CacheEntry entry = cacheLocked(component, info, labelCache, info.getUser());
Joe Onoratofad1fb52010-05-04 12:12:41 -0700283 return entry.icon;
284 }
Joe Onorato0589f0f2010-02-08 13:44:00 -0800285 }
286
Kenny Guyed131872014-04-30 03:02:21 +0100287 public boolean isDefaultIcon(Bitmap icon, UserHandleCompat user) {
288 return mDefaultIcons.get(user) == icon;
Joe Onoratoddc9c1f2010-08-30 18:30:15 -0700289 }
290
Kenny Guyed131872014-04-30 03:02:21 +0100291 private CacheEntry cacheLocked(ComponentName componentName, LauncherActivityInfoCompat info,
292 HashMap<Object, CharSequence> labelCache, UserHandleCompat user) {
293 CacheKey cacheKey = new CacheKey(componentName, user);
294 CacheEntry entry = mCache.get(cacheKey);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800295 if (entry == null) {
296 entry = new CacheEntry();
297
Kenny Guyed131872014-04-30 03:02:21 +0100298 mCache.put(cacheKey, entry);
Joe Onorato84f6a8d2010-02-12 17:53:35 -0500299
Chris Wren6d0dde02014-02-10 12:16:54 -0500300 if (info != null) {
Kenny Guyed131872014-04-30 03:02:21 +0100301 ComponentName labelKey = info.getComponentName();
302 if (labelCache != null && labelCache.containsKey(labelKey)) {
303 entry.title = labelCache.get(labelKey).toString();
Chris Wren6d0dde02014-02-10 12:16:54 -0500304 } else {
Kenny Guyed131872014-04-30 03:02:21 +0100305 entry.title = info.getLabel().toString();
Chris Wren6d0dde02014-02-10 12:16:54 -0500306 if (labelCache != null) {
Kenny Guyed131872014-04-30 03:02:21 +0100307 labelCache.put(labelKey, entry.title);
Chris Wren6d0dde02014-02-10 12:16:54 -0500308 }
309 }
Chris Wren6d0dde02014-02-10 12:16:54 -0500310
311 entry.icon = Utilities.createIconBitmap(
Kenny Guyed131872014-04-30 03:02:21 +0100312 info.getBadgedIcon(mIconDpi), mContext);
Winson Chungc3eecff2011-07-11 17:44:15 -0700313 } else {
Chris Wren6d0dde02014-02-10 12:16:54 -0500314 entry.title = "";
Kenny Guyed131872014-04-30 03:02:21 +0100315 Bitmap preloaded = getPreloadedIcon(componentName, user);
Chris Wren6d0dde02014-02-10 12:16:54 -0500316 if (preloaded != null) {
317 if (DEBUG) Log.d(TAG, "using preloaded icon for " +
318 componentName.toShortString());
319 entry.icon = preloaded;
320 } else {
321 if (DEBUG) Log.d(TAG, "using default icon for " +
322 componentName.toShortString());
Kenny Guyed131872014-04-30 03:02:21 +0100323 entry.icon = getDefaultIcon(user);
Winson Chungc3eecff2011-07-11 17:44:15 -0700324 }
325 }
Joe Onorato0589f0f2010-02-08 13:44:00 -0800326 }
327 return entry;
328 }
Daniel Sandler4e1cd232011-05-12 00:06:32 -0400329
330 public HashMap<ComponentName,Bitmap> getAllIcons() {
331 synchronized (mCache) {
332 HashMap<ComponentName,Bitmap> set = new HashMap<ComponentName,Bitmap>();
Kenny Guyed131872014-04-30 03:02:21 +0100333 for (CacheKey ck : mCache.keySet()) {
334 final CacheEntry e = mCache.get(ck);
335 set.put(ck.componentName, e.icon);
Daniel Sandler4e1cd232011-05-12 00:06:32 -0400336 }
337 return set;
338 }
339 }
Chris Wren6d0dde02014-02-10 12:16:54 -0500340
341 /**
342 * Pre-load an icon into the persistent cache.
343 *
344 * <P>Queries for a component that does not exist in the package manager
345 * will be answered by the persistent cache.
346 *
347 * @param context application context
348 * @param componentName the icon should be returned for this component
349 * @param icon the icon to be persisted
350 * @param dpi the native density of the icon
351 */
352 public static void preloadIcon(Context context, ComponentName componentName, Bitmap icon,
353 int dpi) {
354 // TODO rescale to the correct native DPI
355 try {
356 PackageManager packageManager = context.getPackageManager();
357 packageManager.getActivityIcon(componentName);
358 // component is present on the system already, do nothing
359 return;
360 } catch (PackageManager.NameNotFoundException e) {
361 // pass
362 }
363
364 final String key = componentName.flattenToString();
365 FileOutputStream resourceFile = null;
366 try {
367 resourceFile = context.openFileOutput(getResourceFilename(componentName),
368 Context.MODE_PRIVATE);
369 ByteArrayOutputStream os = new ByteArrayOutputStream();
370 if (icon.compress(android.graphics.Bitmap.CompressFormat.PNG, 75, os)) {
371 byte[] buffer = os.toByteArray();
372 resourceFile.write(buffer, 0, buffer.length);
373 } else {
374 Log.w(TAG, "failed to encode cache for " + key);
375 return;
376 }
377 } catch (FileNotFoundException e) {
378 Log.w(TAG, "failed to pre-load cache for " + key, e);
379 } catch (IOException e) {
380 Log.w(TAG, "failed to pre-load cache for " + key, e);
381 } finally {
382 if (resourceFile != null) {
383 try {
384 resourceFile.close();
385 } catch (IOException e) {
386 Log.d(TAG, "failed to save restored icon for: " + key, e);
387 }
388 }
389 }
390 }
391
392 /**
393 * Read a pre-loaded icon from the persistent icon cache.
394 *
395 * @param componentName the component that should own the icon
396 * @returns a bitmap if one is cached, or null.
397 */
Kenny Guyed131872014-04-30 03:02:21 +0100398 private Bitmap getPreloadedIcon(ComponentName componentName, UserHandleCompat user) {
Chris Wren6d0dde02014-02-10 12:16:54 -0500399 final String key = componentName.flattenToShortString();
400
Kenny Guyed131872014-04-30 03:02:21 +0100401 // We don't keep icons for other profiles in persistent cache.
402 if (!user.equals(UserHandleCompat.myUserHandle())) {
403 return null;
404 }
405
Chris Wren6d0dde02014-02-10 12:16:54 -0500406 if (DEBUG) Log.v(TAG, "looking for pre-load icon for " + key);
407 Bitmap icon = null;
408 FileInputStream resourceFile = null;
409 try {
410 resourceFile = mContext.openFileInput(getResourceFilename(componentName));
411 byte[] buffer = new byte[1024];
412 ByteArrayOutputStream bytes = new ByteArrayOutputStream();
413 int bytesRead = 0;
414 while(bytesRead >= 0) {
415 bytes.write(buffer, 0, bytesRead);
416 bytesRead = resourceFile.read(buffer, 0, buffer.length);
417 }
418 if (DEBUG) Log.d(TAG, "read " + bytes.size());
419 icon = BitmapFactory.decodeByteArray(bytes.toByteArray(), 0, bytes.size());
420 if (icon == null) {
421 Log.w(TAG, "failed to decode pre-load icon for " + key);
422 }
423 } catch (FileNotFoundException e) {
424 if (DEBUG) Log.d(TAG, "there is no restored icon for: " + key, e);
425 } catch (IOException e) {
426 Log.w(TAG, "failed to read pre-load icon for: " + key, e);
427 } finally {
428 if(resourceFile != null) {
429 try {
430 resourceFile.close();
431 } catch (IOException e) {
432 Log.d(TAG, "failed to manage pre-load icon file: " + key, e);
433 }
434 }
435 }
436
Chris Wren6d0dde02014-02-10 12:16:54 -0500437 return icon;
438 }
439
440 /**
441 * Remove a pre-loaded icon from the persistent icon cache.
442 *
443 * @param componentName the component that should own the icon
444 * @returns true on success
445 */
Kenny Guyed131872014-04-30 03:02:21 +0100446 public boolean deletePreloadedIcon(ComponentName componentName, UserHandleCompat user) {
447 // We don't keep icons for other profiles in persistent cache.
448 if (!user.equals(UserHandleCompat.myUserHandle())) {
449 return false;
450 }
Chris Wren6d0dde02014-02-10 12:16:54 -0500451 if (componentName == null) {
452 return false;
453 }
454 if (mCache.remove(componentName) != null) {
455 if (DEBUG) Log.d(TAG, "removed pre-loaded icon from the in-memory cache");
456 }
457 boolean success = mContext.deleteFile(getResourceFilename(componentName));
458 if (DEBUG && success) Log.d(TAG, "removed pre-loaded icon from persistent cache");
459
460 return success;
461 }
462
463 private static String getResourceFilename(ComponentName component) {
464 String resourceName = component.flattenToShortString();
465 String filename = resourceName.replace(File.separatorChar, '_');
466 return RESOURCE_FILE_PREFIX + filename;
467 }
Joe Onorato0589f0f2010-02-08 13:44:00 -0800468}