android Launcher app 的边角料IconCache

本文写的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 labelCache) {
        省略部分代码
        iconCache.getTitleAndIcon(this, info, labelCache);
    }
}

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 labelCache) {
        synchronized (mCache) {
            //Cache的关键方法,都是调用cacheLocked方法
            CacheEntry entry = cacheLocked(application.componentName, info, labelCache,
                    info.getUser());

            application.title = entry.title;
            application.iconBitmap = entry.icon;
            application.contentDescription = entry.contentDescription;
        }
    }

    public Bitmap getIcon(Intent intent, UserHandle user) {
        synchronized (mCache) {
            LauncherApps launcherApps = (LauncherApps)
                    mContext.getSystemService(Context.LAUNCHER_APPS_SERVICE);
            final LauncherActivityInfo launcherActInfo =
                    launcherApps.resolveActivity(intent, user);
            ComponentName component = intent.getComponent();

            if (launcherActInfo == null || component == null) {
                return mDefaultIcon;
            }
             //Cache的关键方法,都是调用cacheLocked方法
            CacheEntry entry = cacheLocked(component, launcherActInfo, null, user);
            return entry.icon;
        }
    }

   //Cache的关键方法
   private CacheEntry cacheLocked(ComponentName componentName, LauncherActivityInfo info,
            HashMap labelCache, UserHandle user) {
        //在mCache中查找CacheEntry
        CacheKey cacheKey = new CacheKey(componentName, user);
        CacheEntry entry = mCache.get(cacheKey);
        if (entry == null) {
             /*未找到CacheEntry 对象新建 并压入 mCache*/
            entry = new CacheEntry();

            mCache.put(cacheKey, entry);

            ComponentName key = info.getComponentName();
            if (labelCache != null && labelCache.containsKey(key)) {
                entry.title = labelCache.get(key).toString();
            } else {
                entry.title = info.getLabel().toString();
                if (labelCache != null) {
                    labelCache.put(key, entry.title);
                }
            }
            if (entry.title == null) {
                entry.title = info.getComponentName().getShortClassName();
            }
            entry.contentDescription = mPackageManager.getUserBadgedLabel(entry.title, user);
            entry.icon = Utilities.createIconBitmap(info.getBadgedIcon(mIconDpi), mContext);
           /*未找到CacheEntry 对象新建 并压入 mCache*/
        }
        //将CacheEntry返回
        return entry;
    }
}

你可能感兴趣的:(android Launcher app 的边角料IconCache)