From b4cd42a1df69ec887d0c66e8fe6fe1f27ebbf9bc Mon Sep 17 00:00:00 2001 From: Sunny Goyal Date: Mon, 16 Mar 2015 14:10:24 -0700 Subject: [PATCH] Extracting out CacheKey to a separate class Change-Id: Ifdd7cc79668b34298e3f788ee684080cecb86c3e --- src/com/android/launcher3/IconCache.java | 42 ++++----------- .../android/launcher3/util/ComponentKey.java | 51 +++++++++++++++++++ 2 files changed, 62 insertions(+), 31 deletions(-) create mode 100644 src/com/android/launcher3/util/ComponentKey.java diff --git a/src/com/android/launcher3/IconCache.java b/src/com/android/launcher3/IconCache.java index 91d4aaf218..43f838e7f6 100644 --- a/src/com/android/launcher3/IconCache.java +++ b/src/com/android/launcher3/IconCache.java @@ -40,6 +40,7 @@ import com.android.launcher3.compat.LauncherActivityInfoCompat; import com.android.launcher3.compat.LauncherAppsCompat; import com.android.launcher3.compat.UserHandleCompat; import com.android.launcher3.compat.UserManagerCompat; +import com.android.launcher3.util.ComponentKey; import java.util.HashMap; import java.util.HashSet; @@ -67,35 +68,14 @@ public class IconCache { public CharSequence contentDescription; } - private static class CacheKey { - public ComponentName componentName; - public UserHandleCompat user; - - CacheKey(ComponentName componentName, UserHandleCompat 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); - } - } - private final HashMap mDefaultIcons = new HashMap(); private final Context mContext; private final PackageManager mPackageManager; private final UserManagerCompat mUserManager; private final LauncherAppsCompat mLauncherApps; - private final HashMap mCache = - new HashMap(INITIAL_ICON_CACHE_CAPACITY); + private final HashMap mCache = + new HashMap(INITIAL_ICON_CACHE_CAPACITY); private final int mIconDpi; private final IconDB mIconDb; @@ -180,21 +160,21 @@ public class IconCache { * Remove any records for the supplied ComponentName. */ public synchronized void remove(ComponentName componentName, UserHandleCompat user) { - mCache.remove(new CacheKey(componentName, user)); + mCache.remove(new ComponentKey(componentName, user)); } /** * Remove any records for the supplied package name from memory. */ private void removeFromMemCacheLocked(String packageName, UserHandleCompat user) { - HashSet forDeletion = new HashSet(); - for (CacheKey key: mCache.keySet()) { + HashSet forDeletion = new HashSet(); + for (ComponentKey key: mCache.keySet()) { if (key.componentName.getPackageName().equals(packageName) && key.user.equals(user)) { forDeletion.add(key); } } - for (CacheKey condemned: forDeletion) { + for (ComponentKey condemned: forDeletion) { mCache.remove(condemned); } } @@ -324,7 +304,7 @@ public class IconCache { entry.icon = Utilities.createIconBitmap(app.getBadgedIcon(mIconDpi), mContext); entry.title = app.getLabel(); entry.contentDescription = mUserManager.getBadgedLabelForUser(entry.title, app.getUser()); - mCache.put(new CacheKey(app.getComponentName(), app.getUser()), entry); + mCache.put(new ComponentKey(app.getComponentName(), app.getUser()), entry); ContentValues values = new ContentValues(); values.put(IconDB.COLUMN_ICON, ItemInfo.flattenBitmap(entry.icon)); @@ -344,7 +324,7 @@ public class IconCache { * Empty out the cache that aren't of the correct grid size */ public synchronized void flushInvalidIcons(DeviceProfile grid) { - Iterator> it = mCache.entrySet().iterator(); + Iterator> it = mCache.entrySet().iterator(); while (it.hasNext()) { final CacheEntry e = it.next().getValue(); if ((e.icon != null) && (e.icon.getWidth() < grid.iconSizePx @@ -426,7 +406,7 @@ public class IconCache { */ private CacheEntry cacheLocked(ComponentName componentName, LauncherActivityInfoCompat info, UserHandleCompat user, boolean usePackageIcon) { - CacheKey cacheKey = new CacheKey(componentName, user); + ComponentKey cacheKey = new ComponentKey(componentName, user); CacheEntry entry = mCache.get(cacheKey); if (entry == null) { entry = new CacheEntry(); @@ -487,7 +467,7 @@ public class IconCache { */ private CacheEntry getEntryForPackage(String packageName, UserHandleCompat user) { ComponentName cn = new ComponentName(packageName, EMPTY_CLASS_NAME);; - CacheKey cacheKey = new CacheKey(cn, user); + ComponentKey cacheKey = new ComponentKey(cn, user); CacheEntry entry = mCache.get(cacheKey); if (entry == null) { entry = new CacheEntry(); diff --git a/src/com/android/launcher3/util/ComponentKey.java b/src/com/android/launcher3/util/ComponentKey.java new file mode 100644 index 0000000000..0f17f009e2 --- /dev/null +++ b/src/com/android/launcher3/util/ComponentKey.java @@ -0,0 +1,51 @@ +package com.android.launcher3.util; + +/** + * Copyright (C) 2015 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. + */ + +import android.content.ComponentName; + +import com.android.launcher3.compat.UserHandleCompat; + +import java.util.Arrays; + +public class ComponentKey { + + public final ComponentName componentName; + public final UserHandleCompat user; + + private final int mHashCode; + + public ComponentKey(ComponentName componentName, UserHandleCompat user) { + assert (componentName != null); + assert (user != null); + this.componentName = componentName; + this.user = user; + mHashCode = Arrays.hashCode(new Object[] {componentName, user}); + + } + + @Override + public int hashCode() { + return mHashCode; + } + + @Override + public boolean equals(Object o) { + ComponentKey other = (ComponentKey) o; + return other.componentName.equals(componentName) && other.user.equals(user); + } +} \ No newline at end of file