Exclude shortcuts in popup from IconCache.

We want to load from/save icons to icon cache for pinned shortcuts only
to reduce memory footprint.

Bug: 140242324
Change-Id: I25c7d59e29c6e27752b36c2c3c226849d4e177af
This commit is contained in:
Pinyao Ting
2019-09-17 23:34:50 -07:00
parent ca939f073c
commit ddd0ff44ba
6 changed files with 26 additions and 15 deletions

View File

@@ -273,7 +273,7 @@ public abstract class BaseIconCache {
if (entry.icon == null) return;
entry.title = cachingLogic.getLabel(object);
entry.contentDescription = mPackageManager.getUserBadgedLabel(entry.title, user);
mCache.put(key, entry);
if (cachingLogic.addToMemCache()) mCache.put(key, entry);
ContentValues values = newContentValues(entry, entry.title.toString(),
componentName.getPackageName(), cachingLogic.getKeywords(object, mLocaleList));
@@ -312,20 +312,12 @@ public abstract class BaseIconCache {
@NonNull ComponentName componentName, @NonNull UserHandle user,
@NonNull Supplier<T> infoProvider, @NonNull CachingLogic<T> cachingLogic,
boolean usePackageIcon, boolean useLowResIcon) {
return cacheLocked(componentName, user, infoProvider, cachingLogic, usePackageIcon,
useLowResIcon, true);
}
protected <T> CacheEntry cacheLocked(
@NonNull ComponentName componentName, @NonNull UserHandle user,
@NonNull Supplier<T> infoProvider, @NonNull CachingLogic<T> cachingLogic,
boolean usePackageIcon, boolean useLowResIcon, boolean addToMemCache) {
assertWorkerThread();
ComponentKey cacheKey = new ComponentKey(componentName, user);
CacheEntry entry = mCache.get(cacheKey);
if (entry == null || (entry.isLowRes() && !useLowResIcon)) {
entry = new CacheEntry();
if (addToMemCache) {
if (cachingLogic.addToMemCache()) {
mCache.put(cacheKey, entry);
}

View File

@@ -49,4 +49,11 @@ public interface CachingLogic<T> {
default long getLastUpdatedTime(T object, PackageInfo info) {
return info.lastUpdateTime;
}
/**
* Returns true the object should be added to mem cache; otherwise returns false.
*/
default boolean addToMemCache() {
return true;
}
}

View File

@@ -34,9 +34,11 @@ public interface ComponentWithLabel {
class ComponentCachingLogic implements CachingLogic<ComponentWithLabel> {
private final PackageManager mPackageManager;
private final boolean mAddToMemCache;
public ComponentCachingLogic(Context context) {
public ComponentCachingLogic(Context context, boolean addToMemCache) {
mPackageManager = context.getPackageManager();
mAddToMemCache = addToMemCache;
}
@Override
@@ -60,5 +62,10 @@ public interface ComponentWithLabel {
// Do not load icon.
target.icon = BitmapInfo.LOW_RES_ICON;
}
@Override
public boolean addToMemCache() {
return mAddToMemCache;
}
}
}

View File

@@ -77,7 +77,7 @@ public class IconCache extends BaseIconCache {
public IconCache(Context context, InvariantDeviceProfile inv) {
super(context, LauncherFiles.APP_ICONS_DB, MODEL_EXECUTOR.getLooper(),
inv.fillResIconDpi, inv.iconBitmapSize, true /* inMemoryCache */);
mComponentWithLabelCachingLogic = new ComponentCachingLogic(context);
mComponentWithLabelCachingLogic = new ComponentCachingLogic(context, false);
mLauncherActivityInfoCachingLogic = LauncherActivityCachingLogic.newInstance(context);
mShortcutCachingLogic = new ShortcutCachingLogic();
mLauncherApps = LauncherAppsCompat.getInstance(mContext);
@@ -206,7 +206,7 @@ public class IconCache extends BaseIconCache {
public synchronized String getTitleNoCache(ComponentWithLabel info) {
CacheEntry entry = cacheLocked(info.getComponent(), info.getUser(), () -> info,
mComponentWithLabelCachingLogic, false /* usePackageIcon */,
true /* useLowResIcon */, false /* addToMemCache */);
true /* useLowResIcon */);
return Utilities.trim(entry.title);
}

View File

@@ -64,4 +64,9 @@ public class ShortcutCachingLogic implements CachingLogic<ShortcutInfo> {
if (shortcutInfo == null) return info.lastUpdateTime;
return Math.max(shortcutInfo.getLastChangedTimestamp(), info.lastUpdateTime);
}
@Override
public boolean addToMemCache() {
return false;
}
}

View File

@@ -242,8 +242,8 @@ public class LoaderTask implements Runnable {
verifyNotStopped();
TraceHelper.partitionSection(TAG, "step 4.3: save widgets in icon cache");
updateHandler.updateIcons(allWidgetsList, new ComponentCachingLogic(mApp.getContext()),
mApp.getModel()::onWidgetLabelsUpdated);
updateHandler.updateIcons(allWidgetsList, new ComponentCachingLogic(
mApp.getContext(), true), mApp.getModel()::onWidgetLabelsUpdated);
verifyNotStopped();
TraceHelper.partitionSection(TAG, "step 5: Finish icon cache update");