本文写的IconCache.java
做为应用的图标与名字缓存的工具类。
初始化:
均只列出相关代码
LauncherApplication.java 中
public class LauncherApplication extends Application {
private IconCache mIconCache;
@Override
public void onCreate() {
省略部分代码
super.onCreate();
mIconCache = new IconCache(this);
}
}
使用到的地方有
ApplicationInfo.java
class ApplicationInfo extends ItemInfo {
public ApplicationInfo(LauncherActivityInfo info, UserHandle user, IconCache iconCache,
HashMap
AllAppsList.java
/**
* Add and remove icons for this package which has been updated.
*/
public void updatePackage(Context context, String packageName, UserHandle user) {
省略部分代码
mIconCache.remove(component, user);
省略部分代码
mIconCache.remove(applicationInfo.componentName, user);
mIconCache.getTitleAndIcon(applicationInfo, info, null);
省略部分代码
mIconCache.remove(component, user);
}
关键的地方是IconCache.java
IconCache.java 中实现缓存是
/**
* Cache of application icons. Icons can be made from any thread.
*/
public class IconCache {
private final HashMap mCache =
new HashMap(INITIAL_ICON_CACHE_CAPACITY);
private static final int INITIAL_ICON_CACHE_CAPACITY = 50;
//静态内部类,CacheKey
//重写hashCode和equals 方法,当ComponentName与UserHandle相同
//我们认为CachKey相等
private static class CacheKey {
public ComponentName componentName;
public UserHandle user;
CacheKey(ComponentName componentName, UserHandle user) {
this.componentName = componentName;
this.user = user;
}
@Override
public int hashCode() {
return componentName.hashCode() + user.hashCode();
}
@Override
public boolean equals(Object o) {
CacheKey other = (CacheKey) o;
return other.componentName.equals(componentName) && other.user.equals(user);
}
}
//要保存的CacheEntry
//Btimap与title
private static class CacheEntry {
public Bitmap icon;
public String title;
public CharSequence contentDescription;
}
/**
* Remove any records for the supplied ComponentName.
*/
public void remove(ComponentName componentName, UserHandle user) {
synchronized (mCache) {
mCache.remove(new CacheKey(componentName, user));
}
}
/**
* Empty out the cache.
*/
public void flush() {
synchronized (mCache) {
mCache.clear();
}
}
/**
* Fill in "application" with the icon and label for "info."
*/
public void getTitleAndIcon(ApplicationInfo application, LauncherActivityInfo info,
HashMap