private boolean getEntryFromDB(ComponentKey cacheKey, CacheEntry entry, boolean lowRes) { Cursor c = mIconDb .getReadableDatabase() .query( IconDB.TABLE_NAME, new String[] { lowRes ? IconDB.COLUMN_ICON_LOW_RES : IconDB.COLUMN_ICON, IconDB.COLUMN_LABEL }, IconDB.COLUMN_COMPONENT + " = ? AND " + IconDB.COLUMN_USER + " = ?", new String[] { cacheKey.componentName.flattenToString(), Long.toString(mUserManager.getSerialNumberForUser(cacheKey.user)) }, null, null, null); try { if (c.moveToNext()) { entry.icon = loadIconNoResize(c, 0, lowRes ? mLowResOptions : null); entry.isLowResIcon = lowRes; entry.title = c.getString(1); if (entry.title == null) { entry.title = ""; entry.contentDescription = ""; } else { entry.contentDescription = mUserManager.getBadgedLabelForUser(entry.title, cacheKey.user); } return true; } } finally { c.close(); } return false; }
/** * 创建一个图标缓存 * * @param componentName * @param info * @param labelCache * @return */ private CacheEntry cacheLocked( ComponentName componentName, ResolveInfo info, HashMap<Object, CharSequence> labelCache) { // 通过componentName在集合中获取图标缓存 CacheEntry entry = mCache.get(componentName); // 如果缓存为空,则创建缓存 if (entry == null) { entry = new CacheEntry(); mCache.put(componentName, entry); // 获得ComponentName 用它作为Key ComponentName key = getComponentNameFromResolveInfo(info); // 如果标签缓存不为空,并且集合中有这个键。则从集合中获得应用的标题 if (labelCache != null && labelCache.containsKey(key)) { entry.title = labelCache.get(key).toString(); } else { // 否则 通过通过ResolveInfo获得程序名,作为标题 entry.title = info.loadLabel(mPackageManager).toString(); // 如果标签集合不为空则将新的标签放入集合 if (labelCache != null) { labelCache.put(key, entry.title); } } // 如果标题为空 if (entry.title == null) { // 则从info中获得标题 entry.title = info.activityInfo.name; } // 为entry获得图标 entry.icon = Utilities.createIconBitmap(getFullResIcon(info), mContext); } return entry; }
/** * Retrieves the entry from the cache. If the entry is not present, it creates a new entry. This * method is not thread safe, it must be called from a synchronized method. */ private CacheEntry cacheLocked( ComponentName componentName, LauncherActivityInfoCompat info, UserHandleCompat user, boolean usePackageIcon, boolean useLowResIcon) { ComponentKey cacheKey = new ComponentKey(componentName, user); CacheEntry entry = mCache.get(cacheKey); if (entry == null || (entry.isLowResIcon && !useLowResIcon)) { entry = new CacheEntry(); mCache.put(cacheKey, entry); // Check the DB first. if (!getEntryFromDB(cacheKey, entry, useLowResIcon)) { if (info != null) { entry.icon = Utilities.createIconBitmap(info.getBadgedIcon(mIconDpi), mContext); } else { if (usePackageIcon) { CacheEntry packageEntry = getEntryForPackageLocked(componentName.getPackageName(), user, false); if (packageEntry != null) { if (DEBUG) Log.d(TAG, "using package default icon for " + componentName.toShortString()); entry.icon = packageEntry.icon; entry.title = packageEntry.title; entry.contentDescription = packageEntry.contentDescription; } } if (entry.icon == null) { if (DEBUG) Log.d(TAG, "using default icon for " + componentName.toShortString()); entry.icon = getDefaultIcon(user); } } } if (TextUtils.isEmpty(entry.title) && info != null) { entry.title = info.getLabel(); entry.contentDescription = mUserManager.getBadgedLabelForUser(entry.title, user); } } return entry; }
/** * Gets an entry for the package, which can be used as a fallback entry for various components. * This method is not thread safe, it must be called from a synchronized method. */ private CacheEntry getEntryForPackageLocked( String packageName, UserHandleCompat user, boolean useLowResIcon) { ComponentKey cacheKey = getPackageKey(packageName, user); CacheEntry entry = mCache.get(cacheKey); if (entry == null || (entry.isLowResIcon && !useLowResIcon)) { entry = new CacheEntry(); boolean entryUpdated = true; // Check the DB first. if (!getEntryFromDB(cacheKey, entry, useLowResIcon)) { try { int flags = UserHandleCompat.myUserHandle().equals(user) ? 0 : PackageManager.GET_UNINSTALLED_PACKAGES; PackageInfo info = mPackageManager.getPackageInfo(packageName, flags); ApplicationInfo appInfo = info.applicationInfo; if (appInfo == null) { throw new NameNotFoundException("ApplicationInfo is null"); } Drawable drawable = mUserManager.getBadgedDrawableForUser(appInfo.loadIcon(mPackageManager), user); entry.icon = Utilities.createIconBitmap(drawable, mContext); entry.title = appInfo.loadLabel(mPackageManager); entry.contentDescription = mUserManager.getBadgedLabelForUser(entry.title, user); entry.isLowResIcon = false; // Add the icon in the DB here, since these do not get written during // package updates. ContentValues values = newContentValues(entry.icon, entry.title.toString(), mPackageBgColor); addIconToDB( values, cacheKey.componentName, info, mUserManager.getSerialNumberForUser(user)); } catch (NameNotFoundException e) { if (DEBUG) Log.d(TAG, "Application not installed " + packageName); entryUpdated = false; } } // Only add a filled-out entry to the cache if (entryUpdated) { mCache.put(cacheKey, entry); } } return entry; }
public void unbindDrawables() { if (Launcher.LOGD) Log.d(TAG, "unbindDrawables"); synchronized (mCache) { for (CacheEntry ce : mCache.values()) { if (null != ce.icon) { ce.icon.recycle(); ce.icon = null; } if (null != ce.titleBitmap) { ce.titleBitmap.recycle(); ce.titleBitmap = null; } } } }
private CacheEntry cacheLocked(ComponentName componentName, ResolveInfo info) { CacheEntry entry = mCache.get(componentName); if (entry == null) { entry = new CacheEntry(); mCache.put(componentName, entry); entry.title = info.loadLabel(mPackageManager).toString(); if (entry.title == null) { entry.title = info.activityInfo.name; } entry.icon = Utilities.createIconBitmap(info.activityInfo.loadIcon(mPackageManager), mContext); } return entry; }
/** * Adds a default package entry in the cache. This entry is not persisted and will be removed when * the cache is flushed. */ public synchronized void cachePackageInstallInfo( String packageName, UserHandleCompat user, Bitmap icon, CharSequence title) { removeFromMemCacheLocked(packageName, user); ComponentKey cacheKey = getPackageKey(packageName, user); CacheEntry entry = mCache.get(cacheKey); // For icon caching, do not go through DB. Just update the in-memory entry. if (entry == null) { entry = new CacheEntry(); mCache.put(cacheKey, entry); } if (!TextUtils.isEmpty(title)) { entry.title = title; } if (icon != null) { entry.icon = Utilities.createIconBitmap(icon, mContext); } }
@Thunk ContentValues updateCacheAndGetContentValues( LauncherActivityInfoCompat app, boolean replaceExisting) { final ComponentKey key = new ComponentKey(app.getComponentName(), app.getUser()); CacheEntry entry = null; if (!replaceExisting) { entry = mCache.get(key); // We can't reuse the entry if the high-res icon is not present. if (entry == null || entry.isLowResIcon || entry.icon == null) { entry = null; } } if (entry == null) { entry = new CacheEntry(); entry.icon = Utilities.createIconBitmap(app.getBadgedIcon(mIconDpi), mContext); } entry.title = app.getLabel(); entry.contentDescription = mUserManager.getBadgedLabelForUser(entry.title, app.getUser()); mCache.put(new ComponentKey(app.getComponentName(), app.getUser()), entry); return newContentValues(entry.icon, entry.title.toString(), mActivityBgColor); }