improving performance of first AllApps/Customize animation

Change-Id: I71e5cc634f7e1346617d101efd6945c00484cab3
diff --git a/src/com/android/launcher2/AllAppsPagedView.java b/src/com/android/launcher2/AllAppsPagedView.java
index 4158b4a..b9b38c3 100644
--- a/src/com/android/launcher2/AllAppsPagedView.java
+++ b/src/com/android/launcher2/AllAppsPagedView.java
@@ -63,6 +63,7 @@
     private int mAppFilter = ALL_APPS_FLAG;
 
     private final LayoutInflater mInflater;
+    private boolean mAllowHardwareLayerCreation;
 
 
     public AllAppsPagedView(Context context) {
@@ -95,6 +96,22 @@
         mCenterPagesVertically = false;
     }
 
+    void allowHardwareLayerCreation() {
+        // This is called after the first time we launch into All Apps. Before that point,
+        // there's no need for hardware layers here since there's a hardware layer set on the
+        // parent, AllAppsTabbed, during the AllApps transition -- creating hardware layers here
+        // before the animation is done slows down the animation
+        if (mAllowHardwareLayerCreation) {
+            return;
+        }
+        mAllowHardwareLayerCreation = true;
+        int childCount = getChildCount();
+        for (int i = 0; i < childCount; i++) {
+            PagedViewCellLayout page = (PagedViewCellLayout) getChildAt(i);
+            page.allowHardwareLayerCreation();
+        }
+    }
+
     @Override
     public void setLauncher(Launcher launcher) {
         mLauncher = launcher;
@@ -121,8 +138,6 @@
         cancelLongPress();
 
         if (isVisible()) {
-            getParent().bringChildToFront(this);
-            setVisibility(View.VISIBLE);
             if (animate) {
                 startAnimation(AnimationUtils.loadAnimation(getContext(),
                         R.anim.all_apps_2d_fade_in));
@@ -141,7 +156,6 @@
 
     protected void onAnimationEnd() {
         if (!isVisible()) {
-            setVisibility(View.GONE);
             mZoom = 0.0f;
 
             endChoiceMode();
@@ -388,6 +402,7 @@
         }
         mFilteredApps = rebuildFilteredApps(mApps);
     }
+
     @Override
     public void removeApps(ArrayList<ApplicationInfo> list) {
         removeAppsWithoutInvalidate(list);
@@ -434,12 +449,15 @@
         // remove any extra pages after the "last" page
         int extraPageDiff = curNumPages - numPages;
         for (int i = 0; i < extraPageDiff; ++i) {
+            PagedViewCellLayout page = (PagedViewCellLayout) getChildAt(numPages);
             removeViewAt(numPages);
         }
         // add any necessary pages
         for (int i = curNumPages; i < numPages; ++i) {
             PagedViewCellLayout layout = new PagedViewCellLayout(getContext());
-            layout.enableHardwareLayers();
+            if (mAllowHardwareLayerCreation) {
+                layout.allowHardwareLayerCreation();
+            }
             layout.setCellCount(mCellCountX, mCellCountY);
             layout.setPadding(mPageLayoutPaddingLeft, mPageLayoutPaddingTop,
                     mPageLayoutPaddingRight, mPageLayoutPaddingBottom);