From 2a359916ca8eec1fe460c1ed5e861268ba94f793 Mon Sep 17 00:00:00 2001 From: Sunny Goyal Date: Mon, 16 Sep 2024 14:58:08 -0700 Subject: [PATCH] Moving some utility class to IconCache to make it easier to use outside Launcher Bug: 366237794 Flag: EXEMPT refactor Test: Presubmit Change-Id: I7583ae162b652e73a33f1aee76714e292b707e39 --- res/values/config.xml | 1 - .../launcher3/icons/ComponentWithLabel.java | 75 ----------------- .../icons/ComponentWithLabelAndIcon.java | 56 ------------- .../android/launcher3/icons/IconCache.java | 21 +++-- .../icons/LauncherActivityCachingLogic.java | 81 ------------------- src/com/android/launcher3/icons/Legacy.kt | 31 +++++++ .../launcher3/icons/ShortcutCachingLogic.java | 4 +- .../android/launcher3/model/LoaderTask.java | 8 +- .../launcher3/pm/PinRequestHelper.java | 5 +- 9 files changed, 50 insertions(+), 232 deletions(-) delete mode 100644 src/com/android/launcher3/icons/ComponentWithLabel.java delete mode 100644 src/com/android/launcher3/icons/ComponentWithLabelAndIcon.java delete mode 100644 src/com/android/launcher3/icons/LauncherActivityCachingLogic.java create mode 100644 src/com/android/launcher3/icons/Legacy.kt diff --git a/res/values/config.xml b/res/values/config.xml index 507ce9a39e..701e64a4b4 100644 --- a/res/values/config.xml +++ b/res/values/config.xml @@ -67,7 +67,6 @@ - diff --git a/src/com/android/launcher3/icons/ComponentWithLabel.java b/src/com/android/launcher3/icons/ComponentWithLabel.java deleted file mode 100644 index 30575fcbe5..0000000000 --- a/src/com/android/launcher3/icons/ComponentWithLabel.java +++ /dev/null @@ -1,75 +0,0 @@ -/* - * 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.PackageManager; -import android.os.UserHandle; - -import androidx.annotation.NonNull; - -import com.android.launcher3.icons.cache.CachingLogic; - -public interface ComponentWithLabel { - - ComponentName getComponent(); - - UserHandle getUser(); - - CharSequence getLabel(PackageManager pm); - - - class ComponentCachingLogic implements CachingLogic { - - private final PackageManager mPackageManager; - private final boolean mAddToMemCache; - - public ComponentCachingLogic(Context context, boolean addToMemCache) { - mPackageManager = context.getPackageManager(); - mAddToMemCache = addToMemCache; - } - - @Override - @NonNull - public ComponentName getComponent(@NonNull T object) { - return object.getComponent(); - } - - @NonNull - @Override - public UserHandle getUser(@NonNull T object) { - return object.getUser(); - } - - @NonNull - @Override - public CharSequence getLabel(@NonNull T object) { - return object.getLabel(mPackageManager); - } - - @NonNull - @Override - public BitmapInfo loadIcon(@NonNull Context context, @NonNull T object) { - return BitmapInfo.LOW_RES_INFO; - } - - @Override - public boolean addToMemCache() { - return mAddToMemCache; - } - } -} diff --git a/src/com/android/launcher3/icons/ComponentWithLabelAndIcon.java b/src/com/android/launcher3/icons/ComponentWithLabelAndIcon.java deleted file mode 100644 index 0a52dd7191..0000000000 --- a/src/com/android/launcher3/icons/ComponentWithLabelAndIcon.java +++ /dev/null @@ -1,56 +0,0 @@ -/* - * Copyright (C) 2020 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.Context; -import android.graphics.drawable.Drawable; - -import androidx.annotation.NonNull; - -import com.android.launcher3.LauncherAppState; -import com.android.launcher3.icons.BaseIconFactory.IconOptions; - -/** - * Extension of ComponentWithLabel to also support loading icons - */ -public interface ComponentWithLabelAndIcon extends ComponentWithLabel { - - /** - * Provide an icon for this object - */ - Drawable getFullResIcon(IconCache cache); - - class ComponentWithIconCachingLogic extends ComponentCachingLogic { - - public ComponentWithIconCachingLogic(Context context, boolean addToMemCache) { - super(context, addToMemCache); - } - - @NonNull - @Override - public BitmapInfo loadIcon(@NonNull Context context, - @NonNull ComponentWithLabelAndIcon object) { - Drawable d = object.getFullResIcon(LauncherAppState.getInstance(context) - .getIconCache()); - if (d == null) { - return super.loadIcon(context, object); - } - try (LauncherIcons li = LauncherIcons.obtain(context)) { - return li.createBadgedIconBitmap(d, new IconOptions().setUser(object.getUser())); - } - } - } -} diff --git a/src/com/android/launcher3/icons/IconCache.java b/src/com/android/launcher3/icons/IconCache.java index 44e448eea1..587dc2731a 100644 --- a/src/com/android/launcher3/icons/IconCache.java +++ b/src/com/android/launcher3/icons/IconCache.java @@ -54,9 +54,10 @@ import androidx.core.util.Pair; import com.android.launcher3.Flags; import com.android.launcher3.InvariantDeviceProfile; import com.android.launcher3.Utilities; -import com.android.launcher3.icons.ComponentWithLabel.ComponentCachingLogic; import com.android.launcher3.icons.cache.BaseIconCache; +import com.android.launcher3.icons.cache.CachedObjectCachingLogic; import com.android.launcher3.icons.cache.CachingLogic; +import com.android.launcher3.icons.cache.LauncherActivityCachingLogic; import com.android.launcher3.logging.FileLog; import com.android.launcher3.model.data.AppInfo; import com.android.launcher3.model.data.IconRequestInfo; @@ -102,7 +103,6 @@ public class IconCache extends BaseIconCache { private final LauncherApps mLauncherApps; private final UserCache mUserManager; private final InstantAppResolver mInstantAppResolver; - private final IconProvider mIconProvider; private final CancellableTask mCancelledTask; private final SparseArray mWidgetCategoryBitmapInfos; @@ -112,14 +112,14 @@ public class IconCache extends BaseIconCache { public IconCache(Context context, InvariantDeviceProfile idp, String dbFileName, IconProvider iconProvider) { super(context, dbFileName, MODEL_EXECUTOR.getLooper(), - idp.fillResIconDpi, idp.iconBitmapSize, true /* inMemoryCache */); - mComponentWithLabelCachingLogic = new ComponentCachingLogic(context, false); - mLauncherActivityInfoCachingLogic = LauncherActivityCachingLogic.newInstance(context); + idp.fillResIconDpi, idp.iconBitmapSize, true /* inMemoryCache */, iconProvider); + mComponentWithLabelCachingLogic = new CachedObjectCachingLogic( + context, false /* loadIcons */, false /* addToMemCache */); + mLauncherActivityInfoCachingLogic = LauncherActivityCachingLogic.INSTANCE; mShortcutCachingLogic = new ShortcutCachingLogic(); mLauncherApps = mContext.getSystemService(LauncherApps.class); mUserManager = UserCache.INSTANCE.get(mContext); mInstantAppResolver = InstantAppResolver.newInstance(mContext); - mIconProvider = iconProvider; mWidgetCategoryBitmapInfos = new SparseArray<>(); mCancelledTask = new CancellableTask(() -> null, MAIN_EXECUTOR, c -> { }); @@ -337,6 +337,9 @@ public class IconCache extends BaseIconCache { } } + /** + * Loads and returns the icon for the provided object without adding it to memCache + */ public synchronized String getTitleNoCache(ComponentWithLabel info) { CacheEntry entry = cacheLocked(info.getComponent(), info.getUser(), () -> info, mComponentWithLabelCachingLogic, false /* usePackageIcon */, @@ -629,12 +632,6 @@ public class IconCache extends BaseIconCache { info.getAppLabel()); } - @Override - @NonNull - protected String getIconSystemState(String packageName) { - return mIconProvider.getSystemStateForPackage(mSystemState, packageName); - } - /** * Interface for receiving itemInfo with high-res icon. */ diff --git a/src/com/android/launcher3/icons/LauncherActivityCachingLogic.java b/src/com/android/launcher3/icons/LauncherActivityCachingLogic.java deleted file mode 100644 index de2269c43d..0000000000 --- a/src/com/android/launcher3/icons/LauncherActivityCachingLogic.java +++ /dev/null @@ -1,81 +0,0 @@ -/* - * 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.Build; -import android.os.UserHandle; - -import androidx.annotation.NonNull; - -import com.android.launcher3.Flags; -import com.android.launcher3.LauncherAppState; -import com.android.launcher3.R; -import com.android.launcher3.icons.BaseIconFactory.IconOptions; -import com.android.launcher3.icons.cache.CachingLogic; -import com.android.launcher3.util.ResourceBasedOverride; - -/** - * Caching logic for LauncherActivityInfo. - */ -public class LauncherActivityCachingLogic - implements CachingLogic, ResourceBasedOverride { - - /** - * Creates and returns a new instance - */ - public static LauncherActivityCachingLogic newInstance(Context context) { - return Overrides.getObject(LauncherActivityCachingLogic.class, context, - R.string.launcher_activity_logic_class); - } - - @NonNull - @Override - public ComponentName getComponent(@NonNull LauncherActivityInfo object) { - return object.getComponentName(); - } - - @NonNull - @Override - public UserHandle getUser(@NonNull LauncherActivityInfo object) { - return object.getUser(); - } - - @NonNull - @Override - public CharSequence getLabel(@NonNull LauncherActivityInfo object) { - return object.getLabel(); - } - - @NonNull - @Override - public BitmapInfo loadIcon(@NonNull Context context, @NonNull LauncherActivityInfo object) { - try (LauncherIcons li = LauncherIcons.obtain(context)) { - IconOptions iconOptions = new IconOptions().setUser(object.getUser()); - iconOptions.mIsArchived = Flags.useNewIconForArchivedApps() - && Build.VERSION.SDK_INT >= 35 - && object.getActivityInfo().isArchived; - return li.createBadgedIconBitmap( - LauncherAppState.getInstance(context) - .getIconProvider() - .getIcon(object, li.mFillResIconDpi), - iconOptions - ); - } - } -} diff --git a/src/com/android/launcher3/icons/Legacy.kt b/src/com/android/launcher3/icons/Legacy.kt new file mode 100644 index 0000000000..3bf3bb2539 --- /dev/null +++ b/src/com/android/launcher3/icons/Legacy.kt @@ -0,0 +1,31 @@ +/* + * Copyright (C) 2024 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 com.android.launcher3.icons.cache.CachedObject + +/** + * This files contains some definitions used during refactoring to avoid breaking changes. + * + * TODO(b/366237794) remove this file once refactoring is complete + */ + +/** Temporary interface to allow easier refactoring */ +interface ComponentWithLabel : CachedObject + +/** Temporary interface to allow easier refactoring */ +interface ComponentWithLabelAndIcon : ComponentWithLabel diff --git a/src/com/android/launcher3/icons/ShortcutCachingLogic.java b/src/com/android/launcher3/icons/ShortcutCachingLogic.java index f40eda6c2d..7bb39e1230 100644 --- a/src/com/android/launcher3/icons/ShortcutCachingLogic.java +++ b/src/com/android/launcher3/icons/ShortcutCachingLogic.java @@ -33,6 +33,7 @@ import androidx.annotation.Nullable; import com.android.launcher3.LauncherAppState; import com.android.launcher3.icons.BaseIconFactory.IconOptions; +import com.android.launcher3.icons.cache.BaseIconCache; import com.android.launcher3.icons.cache.CachingLogic; import com.android.launcher3.shortcuts.ShortcutKey; import com.android.launcher3.util.Themes; @@ -72,7 +73,8 @@ public class ShortcutCachingLogic implements CachingLogic { @NonNull @Override - public BitmapInfo loadIcon(@NonNull Context context, @NonNull ShortcutInfo info) { + public BitmapInfo loadIcon(@NonNull Context context, @NonNull BaseIconCache cache, + @NonNull ShortcutInfo info) { try (LauncherIcons li = LauncherIcons.obtain(context)) { Drawable unbadgedDrawable = ShortcutCachingLogic.getIcon( context, info, LauncherAppState.getIDP(context).fillResIconDpi); diff --git a/src/com/android/launcher3/model/LoaderTask.java b/src/com/android/launcher3/model/LoaderTask.java index 605accf39c..609846f7fc 100644 --- a/src/com/android/launcher3/model/LoaderTask.java +++ b/src/com/android/launcher3/model/LoaderTask.java @@ -70,11 +70,11 @@ import com.android.launcher3.folder.FolderGridOrganizer; import com.android.launcher3.folder.FolderNameInfos; import com.android.launcher3.folder.FolderNameProvider; import com.android.launcher3.icons.ComponentWithLabelAndIcon; -import com.android.launcher3.icons.ComponentWithLabelAndIcon.ComponentWithIconCachingLogic; import com.android.launcher3.icons.IconCache; -import com.android.launcher3.icons.LauncherActivityCachingLogic; import com.android.launcher3.icons.ShortcutCachingLogic; +import com.android.launcher3.icons.cache.CachedObjectCachingLogic; import com.android.launcher3.icons.cache.IconCacheUpdateHandler; +import com.android.launcher3.icons.cache.LauncherActivityCachingLogic; import com.android.launcher3.logging.FileLog; import com.android.launcher3.model.data.AppInfo; import com.android.launcher3.model.data.AppPairInfo; @@ -298,7 +298,7 @@ public class LoaderTask implements Runnable { IconCacheUpdateHandler updateHandler = mIconCache.getUpdateHandler(); setIgnorePackages(updateHandler); updateHandler.updateIcons(allActivityList, - LauncherActivityCachingLogic.newInstance(mApp.getContext()), + LauncherActivityCachingLogic.INSTANCE, mApp.getModel()::onPackageIconsUpdated); logASplit("update icon cache"); @@ -360,7 +360,7 @@ public class LoaderTask implements Runnable { } updateHandler.updateIcons(allWidgetsList, - new ComponentWithIconCachingLogic(mApp.getContext(), true), + new CachedObjectCachingLogic(mApp.getContext()), mApp.getModel()::onWidgetLabelsUpdated); logASplit("save widgets in icon cache"); diff --git a/src/com/android/launcher3/pm/PinRequestHelper.java b/src/com/android/launcher3/pm/PinRequestHelper.java index 667136ae00..47afeef703 100644 --- a/src/com/android/launcher3/pm/PinRequestHelper.java +++ b/src/com/android/launcher3/pm/PinRequestHelper.java @@ -77,8 +77,9 @@ public class PinRequestHelper { WorkspaceItemInfo info = new WorkspaceItemInfo(si, context); // Apply the unbadged icon synchronously using the caching logic directly and // fetch the actual icon asynchronously. - info.bitmap = new ShortcutCachingLogic().loadIcon(context, si); - LauncherAppState.getInstance(context).getModel().updateAndBindWorkspaceItem(info, si); + LauncherAppState app = LauncherAppState.getInstance(context); + info.bitmap = new ShortcutCachingLogic().loadIcon(context, app.getIconCache(), si); + app.getModel().updateAndBindWorkspaceItem(info, si); return info; } else { return null;