From 241c3b451d7841ba08247beea784953eca4e8582 Mon Sep 17 00:00:00 2001 From: Winson Chung Date: Wed, 25 Aug 2010 16:53:03 -0700 Subject: [PATCH] Adding outline cache for PagedViewIcons. Change-Id: I258740a0323660edd73b5f40d61d509455ae195b --- .../android/launcher2/AllAppsPagedView.java | 18 +++---- .../android/launcher2/CustomizePagedView.java | 17 ++++--- src/com/android/launcher2/PagedView.java | 27 +++++++++++ src/com/android/launcher2/PagedViewIcon.java | 48 +++++++++++++------ 4 files changed, 77 insertions(+), 33 deletions(-) diff --git a/src/com/android/launcher2/AllAppsPagedView.java b/src/com/android/launcher2/AllAppsPagedView.java index 378c2488c2..a673304eee 100644 --- a/src/com/android/launcher2/AllAppsPagedView.java +++ b/src/com/android/launcher2/AllAppsPagedView.java @@ -211,6 +211,7 @@ public class AllAppsPagedView extends PagedView mApps = list; Collections.sort(mApps, LauncherModel.APP_NAME_COMPARATOR); mFilteredApps = rebuildFilteredApps(mApps); + mPageViewIconCache.clear(); invalidatePageData(); } @@ -236,9 +237,11 @@ public class AllAppsPagedView extends PagedView // loop through all the apps and remove apps that have the same component final int length = list.size(); for (int i = 0; i < length; ++i) { - int removeIndex = findAppByComponent(mApps, list.get(i)); + final ApplicationInfo info = list.get(i); + int removeIndex = findAppByComponent(mApps, info); if (removeIndex > -1) { mApps.remove(removeIndex); + mPageViewIconCache.removeOutline(info); } } mFilteredApps = rebuildFilteredApps(mApps); @@ -327,16 +330,13 @@ public class AllAppsPagedView extends PagedView // actually reapply to the existing text views for (int i = startIndex; i < endIndex; ++i) { - int index = i - startIndex; - ApplicationInfo info = mFilteredApps.get(i); - TextView text = (TextView) layout.getChildAt(index); - text.setCompoundDrawablesWithIntrinsicBounds(null, - new FastBitmapDrawable(info.iconBitmap), null, null); - text.setText(info.title); - text.setTag(info); + final int index = i - startIndex; + final ApplicationInfo info = mFilteredApps.get(i); + PagedViewIcon icon = (PagedViewIcon) layout.getChildAt(index); + icon.applyFromApplicationInfo(info, mPageViewIconCache); PagedViewCellLayout.LayoutParams params = - (PagedViewCellLayout.LayoutParams) text.getLayoutParams(); + (PagedViewCellLayout.LayoutParams) icon.getLayoutParams(); params.cellX = index % mCellCountX; params.cellY = index / mCellCountX; } diff --git a/src/com/android/launcher2/CustomizePagedView.java b/src/com/android/launcher2/CustomizePagedView.java index 4488497282..786300bf79 100644 --- a/src/com/android/launcher2/CustomizePagedView.java +++ b/src/com/android/launcher2/CustomizePagedView.java @@ -136,6 +136,9 @@ public class CustomizePagedView extends PagedView mShortcutList = mPackageManager.queryIntentActivities(shortcutsIntent, 0); Collections.sort(mShortcutList, resolveInfoComparator); + // reset the icon cache + mPageViewIconCache.clear(); + invalidatePageData(); } @@ -443,19 +446,15 @@ public class CustomizePagedView extends PagedView layout.removeAllViews(); for (int i = startIndex; i < endIndex; ++i) { ResolveInfo info = list.get(i); - Drawable image = info.loadIcon(mPackageManager); - TextView text = (TextView) mInflater.inflate(R.layout.customize_paged_view_item, - layout, false); - image.setBounds(0, 0, image.getIntrinsicWidth(), image.getIntrinsicHeight()); - text.setCompoundDrawablesWithIntrinsicBounds(null, image, null, null); - text.setText(info.loadLabel(mPackageManager)); - text.setTag(info); - text.setOnLongClickListener(this); + PagedViewIcon icon = (PagedViewIcon) mInflater.inflate( + R.layout.customize_paged_view_item, layout, false); + icon.applyFromResolveInfo(info, mPackageManager, mPageViewIconCache); + icon.setOnLongClickListener(this); final int index = i - startIndex; final int x = index % mCellCountX; final int y = index / mCellCountX; - layout.addViewToCellLayout(text, -1, i, new PagedViewCellLayout.LayoutParams(x,y, 1,1)); + layout.addViewToCellLayout(icon, -1, i, new PagedViewCellLayout.LayoutParams(x,y, 1,1)); } } diff --git a/src/com/android/launcher2/PagedView.java b/src/com/android/launcher2/PagedView.java index d56e7acdf7..6154947972 100644 --- a/src/com/android/launcher2/PagedView.java +++ b/src/com/android/launcher2/PagedView.java @@ -18,8 +18,10 @@ package com.android.launcher2; import java.util.ArrayList; import java.util.Arrays; +import java.util.HashMap; import android.content.Context; +import android.graphics.Bitmap; import android.graphics.Canvas; import android.graphics.Rect; import android.os.Parcel; @@ -87,6 +89,30 @@ public abstract class PagedView extends ViewGroup { private ArrayList mDirtyPageContent; private boolean mDirtyPageAlpha; + protected PagedViewIconCache mPageViewIconCache; + + /** + * Simple cache mechanism for PagedViewIcon outlines. + */ + class PagedViewIconCache { + private final HashMap iconOutlineCache = new HashMap(); + + public void clear() { + iconOutlineCache.clear(); + } + public void addOutline(Object key, Bitmap b) { + iconOutlineCache.put(key, b); + } + public void removeOutline(Object key) { + if (iconOutlineCache.containsKey(key)) { + iconOutlineCache.remove(key); + } + } + public Bitmap getOutline(Object key) { + return iconOutlineCache.get(key); + } + } + public interface PageSwitchListener { void onPageSwitch(View newPage, int newPageIndex); } @@ -112,6 +138,7 @@ public abstract class PagedView extends ViewGroup { private void initWorkspace() { mDirtyPageContent = new ArrayList(); mDirtyPageContent.ensureCapacity(32); + mPageViewIconCache = new PagedViewIconCache(); mScroller = new Scroller(getContext()); mCurrentPage = 0; diff --git a/src/com/android/launcher2/PagedViewIcon.java b/src/com/android/launcher2/PagedViewIcon.java index 598761eef6..e227569d75 100644 --- a/src/com/android/launcher2/PagedViewIcon.java +++ b/src/com/android/launcher2/PagedViewIcon.java @@ -17,6 +17,8 @@ package com.android.launcher2; import android.content.Context; +import android.content.pm.PackageManager; +import android.content.pm.ResolveInfo; import android.content.res.Resources; import android.graphics.Bitmap; import android.graphics.BlurMaskFilter; @@ -31,9 +33,12 @@ import android.graphics.Region.Op; import android.graphics.drawable.Drawable; import android.util.AttributeSet; import android.util.DisplayMetrics; +import android.util.Log; import android.widget.Checkable; import android.widget.TextView; +import com.android.launcher2.PagedView.PagedViewIconCache; + class HolographicOutlineHelper { private final Paint mHolographicPaint = new Paint(); private final Paint mBlurPaint = new Paint(); @@ -138,6 +143,9 @@ public class PagedViewIcon extends TextView implements Checkable { private boolean mIsHolographicUpdatePass; private Rect mDrawableClipRect; + private Object mIconCacheKey; + private PagedViewIconCache mIconCache; + private int mAlpha; private int mHolographicAlpha; @@ -166,6 +174,30 @@ public class PagedViewIcon extends TextView implements Checkable { setBackgroundDrawable(null); } + public void applyFromApplicationInfo(ApplicationInfo info, PagedViewIconCache cache) { + mIconCache = cache; + mIconCacheKey = info; + mHolographicOutline = mIconCache.getOutline(mIconCacheKey); + + setCompoundDrawablesWithIntrinsicBounds(null, + new FastBitmapDrawable(info.iconBitmap), null, null); + setText(info.title); + setTag(info); + } + + public void applyFromResolveInfo(ResolveInfo info, PackageManager packageManager, + PagedViewIconCache cache) { + mIconCache = cache; + mIconCacheKey = info; + mHolographicOutline = mIconCache.getOutline(mIconCacheKey); + + Drawable image = info.loadIcon(packageManager); + image.setBounds(0, 0, image.getIntrinsicWidth(), image.getIntrinsicHeight()); + setCompoundDrawablesWithIntrinsicBounds(null, image, null, null); + setText(info.loadLabel(packageManager)); + setTag(info); + } + @Override public void setAlpha(float alpha) { final float viewAlpha = sHolographicOutlineHelper.viewAlphaInterpolator(alpha); @@ -175,21 +207,6 @@ public class PagedViewIcon extends TextView implements Checkable { super.setAlpha(viewAlpha); } - @Override - public void setCompoundDrawablesWithIntrinsicBounds(Drawable left, Drawable top, - Drawable right, Drawable bottom) { - invalidateHolographicImage(); - super.setCompoundDrawablesWithIntrinsicBounds(left, top, right, bottom); - } - - public void invalidateHolographicImage() { - if (mHolographicOutline != null) { - mHolographicOutline.recycle(); - mHolographicOutline = null; - mHolographicOutlineCanvas = null; - } - } - @Override protected void onLayout(boolean changed, int left, int top, int right, int bottom) { super.onLayout(changed, left, top, right, bottom); @@ -215,6 +232,7 @@ public class PagedViewIcon extends TextView implements Checkable { offset); sHolographicOutlineHelper.applyBlur(mHolographicOutline, mHolographicOutlineCanvas); mIsHolographicUpdatePass = false; + mIconCache.addOutline(mIconCacheKey, mHolographicOutline); } }