From 678f47c0c962ab66503c21628301b0b93b6fe889 Mon Sep 17 00:00:00 2001 From: Sunny Goyal Date: Mon, 5 Nov 2018 17:05:35 -0800 Subject: [PATCH] Removing some launcher cross dependency in BaseIconCache so that it can eventually be moved to icon library Change-Id: I65e3ad34322a10695c1ba114f5e60c4418747f05 --- .../launcher3/icons/BaseIconCache.java | 45 +++----------- .../android/launcher3/icons/CachingLogic.java | 60 +------------------ .../launcher3/icons/ComponentWithLabel.java | 33 ++++++++++ .../android/launcher3/icons/IconCache.java | 54 ++++++++++++++--- .../icons/LauncherActivtiyCachingLogic.java | 54 +++++++++++++++++ .../android/launcher3/model/LoaderTask.java | 9 +-- 6 files changed, 146 insertions(+), 109 deletions(-) create mode 100644 src/com/android/launcher3/icons/LauncherActivtiyCachingLogic.java diff --git a/src/com/android/launcher3/icons/BaseIconCache.java b/src/com/android/launcher3/icons/BaseIconCache.java index 2afe7139fd..52233ef0da 100644 --- a/src/com/android/launcher3/icons/BaseIconCache.java +++ b/src/com/android/launcher3/icons/BaseIconCache.java @@ -22,7 +22,6 @@ import android.content.ContentValues; import android.content.Context; import android.content.pm.ActivityInfo; import android.content.pm.ApplicationInfo; -import android.content.pm.LauncherActivityInfo; import android.content.pm.PackageInfo; import android.content.pm.PackageManager; import android.content.pm.PackageManager.NameNotFoundException; @@ -33,6 +32,7 @@ import android.database.sqlite.SQLiteException; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.graphics.drawable.Drawable; +import android.os.Build; import android.os.Build.VERSION; import android.os.Handler; import android.os.Process; @@ -41,15 +41,11 @@ import android.text.TextUtils; import android.util.Log; import com.android.launcher3.IconProvider; -import com.android.launcher3.ItemInfoWithIcon; import com.android.launcher3.LauncherFiles; import com.android.launcher3.LauncherModel; -import com.android.launcher3.MainThreadExecutor; import com.android.launcher3.Utilities; -import com.android.launcher3.compat.LauncherAppsCompat; import com.android.launcher3.compat.UserManagerCompat; import com.android.launcher3.graphics.BitmapRenderer; -import com.android.launcher3.model.PackageItemInfo; import com.android.launcher3.util.ComponentKey; import com.android.launcher3.util.InstantAppResolver; import com.android.launcher3.util.Preconditions; @@ -80,12 +76,10 @@ public class BaseIconCache { private final HashMap mDefaultIcons = new HashMap<>(); - final MainThreadExecutor mMainThreadExecutor = new MainThreadExecutor(); final Context mContext; final PackageManager mPackageManager; final IconProvider mIconProvider; final UserManagerCompat mUserManager; - final LauncherAppsCompat mLauncherApps; private final HashMap mCache = new HashMap<>(INITIAL_ICON_CACHE_CAPACITY); @@ -101,13 +95,12 @@ public class BaseIconCache { mContext = context; mPackageManager = context.getPackageManager(); mUserManager = UserManagerCompat.getInstance(mContext); - mLauncherApps = LauncherAppsCompat.getInstance(mContext); mInstantAppResolver = InstantAppResolver.newInstance(mContext); mIconProvider = IconProvider.newInstance(context); mWorkerHandler = new Handler(LauncherModel.getWorkerLooper()); - if (BitmapRenderer.USE_HARDWARE_BITMAP) { + if (BitmapRenderer.USE_HARDWARE_BITMAP && Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { mDecodeOptions = new BitmapFactory.Options(); mDecodeOptions.inPreferredConfig = Bitmap.Config.HARDWARE; } else { @@ -161,14 +154,6 @@ public class BaseIconCache { return getFullResDefaultActivityIcon(); } - public Drawable getFullResIcon(LauncherActivityInfo info) { - return getFullResIcon(info, true); - } - - public Drawable getFullResIcon(LauncherActivityInfo info, boolean flattenDrawable) { - return mIconProvider.getIcon(info, mIconDpi, flattenDrawable); - } - protected BitmapInfo makeDefaultIcon(UserHandle user) { try (LauncherIcons li = LauncherIcons.obtain(mContext)) { return li.createBadgedIconBitmap( @@ -238,9 +223,9 @@ public class BaseIconCache { } if (entry == null) { entry = new CacheEntry(); - cachingLogic.loadIcon(mContext, this, object, entry); + cachingLogic.loadIcon(mContext, object, entry); } - entry.title = cachingLogic.getLabel(object, mPackageManager); + entry.title = cachingLogic.getLabel(object); entry.contentDescription = mUserManager.getBadgedLabelForUser(entry.title, user); mCache.put(key, entry); @@ -262,22 +247,6 @@ public class BaseIconCache { mIconDb.insertOrReplace(values); } - /** - * Fill in {@param infoInOut} with the corresponding icon and label. - */ - public synchronized void getTitleAndIconForApp( - PackageItemInfo infoInOut, boolean useLowResIcon) { - CacheEntry entry = getEntryForPackageLocked( - infoInOut.packageName, infoInOut.user, useLowResIcon); - applyCacheEntry(entry, infoInOut); - } - - protected void applyCacheEntry(CacheEntry entry, ItemInfoWithIcon info) { - info.title = Utilities.trim(entry.title); - info.contentDescription = entry.contentDescription; - info.applyFrom((entry.icon == null) ? getDefaultIcon(info.user) : entry); - } - public synchronized BitmapInfo getDefaultIcon(UserHandle user) { if (!mDefaultIcons.containsKey(user)) { mDefaultIcons.put(user, makeDefaultIcon(user)); @@ -323,7 +292,7 @@ public class BaseIconCache { providerFetchedOnce = true; if (object != null) { - cachingLogic.loadIcon(mContext, this, object, entry); + cachingLogic.loadIcon(mContext, object, entry); } else { if (usePackageIcon) { CacheEntry packageEntry = getEntryForPackageLocked( @@ -350,7 +319,7 @@ public class BaseIconCache { providerFetchedOnce = true; } if (object != null) { - entry.title = cachingLogic.getLabel(object, mPackageManager); + entry.title = cachingLogic.getLabel(object); entry.contentDescription = mUserManager.getBadgedLabelForUser(entry.title, user); } } @@ -400,7 +369,7 @@ public class BaseIconCache { * 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, UserHandle user, + protected CacheEntry getEntryForPackageLocked(String packageName, UserHandle user, boolean useLowResIcon) { Preconditions.assertWorkerThread(); ComponentKey cacheKey = getPackageKey(packageName, user); diff --git a/src/com/android/launcher3/icons/CachingLogic.java b/src/com/android/launcher3/icons/CachingLogic.java index 24186ef990..0a3da04d78 100644 --- a/src/com/android/launcher3/icons/CachingLogic.java +++ b/src/com/android/launcher3/icons/CachingLogic.java @@ -17,8 +17,6 @@ package com.android.launcher3.icons; import android.content.ComponentName; import android.content.Context; -import android.content.pm.LauncherActivityInfo; -import android.content.pm.PackageManager; import android.os.UserHandle; public interface CachingLogic { @@ -27,61 +25,7 @@ public interface CachingLogic { UserHandle getUser(T object); - CharSequence getLabel(T object, PackageManager pm); + CharSequence getLabel(T object); - void loadIcon(Context context, BaseIconCache cache, T object, BitmapInfo target); - - CachingLogic LAUNCHER_ACTIVITY_INFO = - new CachingLogic() { - - @Override - public ComponentName getComponent(LauncherActivityInfo object) { - return object.getComponentName(); - } - - @Override - public UserHandle getUser(LauncherActivityInfo object) { - return object.getUser(); - } - - @Override - public CharSequence getLabel(LauncherActivityInfo object, PackageManager pm) { - return object.getLabel(); - } - - @Override - public void loadIcon(Context context, BaseIconCache cache, LauncherActivityInfo object, - BitmapInfo target) { - LauncherIcons li = LauncherIcons.obtain(context); - li.createBadgedIconBitmap(cache.getFullResIcon(object), object.getUser(), - object.getApplicationInfo().targetSdkVersion).applyTo(target); - li.recycle(); - } - }; - - CachingLogic COMPONENT_WITH_LABEL = - new CachingLogic() { - - @Override - public ComponentName getComponent(ComponentWithLabel object) { - return object.getComponent(); - } - - @Override - public UserHandle getUser(ComponentWithLabel object) { - return object.getUser(); - } - - @Override - public CharSequence getLabel(ComponentWithLabel object, PackageManager pm) { - return object.getLabel(pm); - } - - @Override - public void loadIcon(Context context, BaseIconCache cache, - ComponentWithLabel object, BitmapInfo target) { - // Do not load icon. - target.icon = BitmapInfo.LOW_RES_ICON; - } - }; + void loadIcon(Context context, T object, BitmapInfo target); } diff --git a/src/com/android/launcher3/icons/ComponentWithLabel.java b/src/com/android/launcher3/icons/ComponentWithLabel.java index 2badb4c373..7bb8832eed 100644 --- a/src/com/android/launcher3/icons/ComponentWithLabel.java +++ b/src/com/android/launcher3/icons/ComponentWithLabel.java @@ -16,6 +16,7 @@ package com.android.launcher3.icons; import android.content.ComponentName; +import android.content.Context; import android.content.pm.PackageManager; import android.os.UserHandle; @@ -26,4 +27,36 @@ public interface ComponentWithLabel { UserHandle getUser(); CharSequence getLabel(PackageManager pm); + + + class ComponentCachingLogic implements CachingLogic { + + private final PackageManager mPackageManager; + + public ComponentCachingLogic(Context context) { + mPackageManager = context.getPackageManager(); + } + + @Override + public ComponentName getComponent(ComponentWithLabel object) { + return object.getComponent(); + } + + @Override + public UserHandle getUser(ComponentWithLabel object) { + return object.getUser(); + } + + @Override + public CharSequence getLabel(ComponentWithLabel object) { + return object.getLabel(mPackageManager); + } + + @Override + public void loadIcon(Context context, + ComponentWithLabel object, BitmapInfo target) { + // Do not load icon. + target.icon = BitmapInfo.LOW_RES_ICON; + } + } } diff --git a/src/com/android/launcher3/icons/IconCache.java b/src/com/android/launcher3/icons/IconCache.java index 6e2ca28ec6..e10ff5b1b8 100644 --- a/src/com/android/launcher3/icons/IconCache.java +++ b/src/com/android/launcher3/icons/IconCache.java @@ -16,15 +16,13 @@ package com.android.launcher3.icons; -import static com.android.launcher3.icons.CachingLogic.COMPONENT_WITH_LABEL; -import static com.android.launcher3.icons.CachingLogic.LAUNCHER_ACTIVITY_INFO; - import android.content.Context; import android.content.Intent; import android.content.pm.LauncherActivityInfo; import android.content.pm.PackageInfo; import android.content.pm.PackageManager; import android.content.pm.PackageManager.NameNotFoundException; +import android.graphics.drawable.Drawable; import android.os.Handler; import android.os.Process; import android.os.UserHandle; @@ -34,8 +32,11 @@ import com.android.launcher3.AppInfo; import com.android.launcher3.InvariantDeviceProfile; import com.android.launcher3.ItemInfoWithIcon; import com.android.launcher3.LauncherModel; +import com.android.launcher3.MainThreadExecutor; import com.android.launcher3.ShortcutInfo; import com.android.launcher3.Utilities; +import com.android.launcher3.compat.LauncherAppsCompat; +import com.android.launcher3.icons.ComponentWithLabel.ComponentCachingLogic; import com.android.launcher3.model.PackageItemInfo; import com.android.launcher3.util.Preconditions; import com.android.launcher3.util.Provider; @@ -49,10 +50,21 @@ public class IconCache extends BaseIconCache { private static final String TAG = "Launcher.IconCache"; + private final MainThreadExecutor mMainThreadExecutor = new MainThreadExecutor(); + + private final CachingLogic mComponentWithLabelCachingLogic; + private final CachingLogic mLauncherActivityInfoCachingLogic; + + private final LauncherAppsCompat mLauncherApps; + private int mPendingIconRequestCount = 0; public IconCache(Context context, InvariantDeviceProfile inv) { super(context, inv.fillResIconDpi, inv.iconBitmapSize); + mComponentWithLabelCachingLogic = new ComponentCachingLogic(context); + mLauncherActivityInfoCachingLogic = new LauncherActivtiyCachingLogic(this); + mLauncherApps = LauncherAppsCompat.getInstance(mContext); + } /** @@ -65,7 +77,7 @@ public class IconCache extends BaseIconCache { PackageManager.GET_UNINSTALLED_PACKAGES); long userSerial = mUserManager.getSerialNumberForUser(user); for (LauncherActivityInfo app : mLauncherApps.getActivityList(packageName, user)) { - addIconToDBAndMemCache(app, LAUNCHER_ACTIVITY_INFO, info, userSerial, + addIconToDBAndMemCache(app, mLauncherActivityInfoCachingLogic, info, userSerial, false /*replace existing*/); } } catch (NameNotFoundException e) { @@ -115,7 +127,7 @@ public class IconCache extends BaseIconCache { */ public synchronized void updateTitleAndIcon(AppInfo application) { CacheEntry entry = cacheLocked(application.componentName, - application.user, Provider.of(null), LAUNCHER_ACTIVITY_INFO, + application.user, Provider.of(null), mLauncherActivityInfoCachingLogic, false, application.usingLowResIcon()); if (entry.icon != null && !isDefaultIcon(entry.icon, application.user)) { applyCacheEntry(entry, application); @@ -151,8 +163,8 @@ public class IconCache extends BaseIconCache { public synchronized String getTitleNoCache(ComponentWithLabel info) { CacheEntry entry = cacheLocked(info.getComponent(), info.getUser(), Provider.of(info), - COMPONENT_WITH_LABEL, false /* usePackageIcon */, true /* useLowResIcon */, - false /* addToMemCache */); + mComponentWithLabelCachingLogic, false /* usePackageIcon */, + true /* useLowResIcon */, false /* addToMemCache */); return Utilities.trim(entry.title); } @@ -164,11 +176,35 @@ public class IconCache extends BaseIconCache { @NonNull Provider activityInfoProvider, boolean usePkgIcon, boolean useLowResIcon) { CacheEntry entry = cacheLocked(infoInOut.getTargetComponent(), infoInOut.user, - activityInfoProvider, - LAUNCHER_ACTIVITY_INFO, usePkgIcon, useLowResIcon); + activityInfoProvider, mLauncherActivityInfoCachingLogic, usePkgIcon, useLowResIcon); applyCacheEntry(entry, infoInOut); } + + /** + * Fill in {@param infoInOut} with the corresponding icon and label. + */ + public synchronized void getTitleAndIconForApp( + PackageItemInfo infoInOut, boolean useLowResIcon) { + CacheEntry entry = getEntryForPackageLocked( + infoInOut.packageName, infoInOut.user, useLowResIcon); + applyCacheEntry(entry, infoInOut); + } + + protected void applyCacheEntry(CacheEntry entry, ItemInfoWithIcon info) { + info.title = Utilities.trim(entry.title); + info.contentDescription = entry.contentDescription; + info.applyFrom((entry.icon == null) ? getDefaultIcon(info.user) : entry); + } + + public Drawable getFullResIcon(LauncherActivityInfo info) { + return getFullResIcon(info, true); + } + + public Drawable getFullResIcon(LauncherActivityInfo info, boolean flattenDrawable) { + return mIconProvider.getIcon(info, mIconDpi, flattenDrawable); + } + public static abstract class IconLoadRequest extends HandlerRunnable { IconLoadRequest(Handler handler, Runnable endRunnable) { super(handler, endRunnable); diff --git a/src/com/android/launcher3/icons/LauncherActivtiyCachingLogic.java b/src/com/android/launcher3/icons/LauncherActivtiyCachingLogic.java new file mode 100644 index 0000000000..8c85b1c799 --- /dev/null +++ b/src/com/android/launcher3/icons/LauncherActivtiyCachingLogic.java @@ -0,0 +1,54 @@ +/* + * Copyright (C) 2018 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.android.launcher3.icons; + +import android.content.ComponentName; +import android.content.Context; +import android.content.pm.LauncherActivityInfo; +import android.os.UserHandle; + +public class LauncherActivtiyCachingLogic implements CachingLogic { + + private final IconCache mCache; + + public LauncherActivtiyCachingLogic(IconCache cache) { + mCache = cache; + } + + @Override + public ComponentName getComponent(LauncherActivityInfo object) { + return object.getComponentName(); + } + + @Override + public UserHandle getUser(LauncherActivityInfo object) { + return object.getUser(); + } + + @Override + public CharSequence getLabel(LauncherActivityInfo object) { + return object.getLabel(); + } + + @Override + public void loadIcon(Context context, LauncherActivityInfo object, + BitmapInfo target) { + LauncherIcons li = LauncherIcons.obtain(context); + li.createBadgedIconBitmap(mCache.getFullResIcon(object), + object.getUser(), object.getApplicationInfo().targetSdkVersion).applyTo(target); + li.recycle(); + } +} \ No newline at end of file diff --git a/src/com/android/launcher3/model/LoaderTask.java b/src/com/android/launcher3/model/LoaderTask.java index 8b3e2c9a86..8551e6fbc0 100644 --- a/src/com/android/launcher3/model/LoaderTask.java +++ b/src/com/android/launcher3/model/LoaderTask.java @@ -20,8 +20,6 @@ import static com.android.launcher3.ItemInfoWithIcon.FLAG_DISABLED_LOCKED_USER; import static com.android.launcher3.ItemInfoWithIcon.FLAG_DISABLED_SAFEMODE; import static com.android.launcher3.ItemInfoWithIcon.FLAG_DISABLED_SUSPENDED; import static com.android.launcher3.folder.ClippedFolderIconLayoutRule.MAX_NUM_ITEMS_IN_PREVIEW; -import static com.android.launcher3.icons.CachingLogic.COMPONENT_WITH_LABEL; -import static com.android.launcher3.icons.CachingLogic.LAUNCHER_ACTIVITY_INFO; import static com.android.launcher3.model.LoaderResults.filterCurrentWorkspaceItems; import android.appwidget.AppWidgetProviderInfo; @@ -46,6 +44,7 @@ import com.android.launcher3.AllAppsList; import com.android.launcher3.AppInfo; import com.android.launcher3.FolderInfo; import com.android.launcher3.icons.ComponentWithLabel; +import com.android.launcher3.icons.ComponentWithLabel.ComponentCachingLogic; import com.android.launcher3.icons.IconCacheUpdateHandler; import com.android.launcher3.icons.IconCache; import com.android.launcher3.InstallShortcutReceiver; @@ -63,6 +62,7 @@ import com.android.launcher3.compat.UserManagerCompat; import com.android.launcher3.config.FeatureFlags; import com.android.launcher3.folder.Folder; import com.android.launcher3.folder.FolderIconPreviewVerifier; +import com.android.launcher3.icons.LauncherActivtiyCachingLogic; import com.android.launcher3.icons.LauncherIcons; import com.android.launcher3.logging.FileLog; import com.android.launcher3.provider.ImportDataTask; @@ -202,7 +202,8 @@ public class LoaderTask implements Runnable { TraceHelper.partitionSection(TAG, "step 2.3: Update icon cache"); IconCacheUpdateHandler updateHandler = mIconCache.getUpdateHandler(); setIgnorePackages(updateHandler); - updateHandler.updateIcons(allActivityList, LAUNCHER_ACTIVITY_INFO, + updateHandler.updateIcons(allActivityList, + new LauncherActivtiyCachingLogic(mApp.getIconCache()), mApp.getModel()::onPackageIconsUpdated); // Take a break @@ -233,7 +234,7 @@ public class LoaderTask implements Runnable { verifyNotStopped(); TraceHelper.partitionSection(TAG, "step 4.3: Update icon cache"); - updateHandler.updateIcons(allWidgetsList, COMPONENT_WITH_LABEL, + updateHandler.updateIcons(allWidgetsList, new ComponentCachingLogic(mApp.getContext()), mApp.getModel()::onWidgetLabelsUpdated); verifyNotStopped();