Improving performance of All Apps transition

- increased perf ~8fps using hardware layers

Change-Id: Ic6a2c23e9182fdb1ee362cbd72f7813f4e9accf0
diff --git a/src/com/android/launcher2/AllAppsTabbed.java b/src/com/android/launcher2/AllAppsTabbed.java
index 47a5bf7..36594dd 100644
--- a/src/com/android/launcher2/AllAppsTabbed.java
+++ b/src/com/android/launcher2/AllAppsTabbed.java
@@ -29,16 +29,19 @@
 import android.view.LayoutInflater;
 import android.view.MotionEvent;
 import android.view.View;
+import android.view.ViewGroup;
 import android.widget.TabHost;
 import android.widget.TabWidget;
 import android.widget.TextView;
+import android.widget.TabHost.OnTabChangeListener;
+import android.widget.TabHost.TabContentFactory;
 
 import java.util.ArrayList;
 
 /**
  * Implements a tabbed version of AllApps2D.
  */
-public class AllAppsTabbed extends TabHost implements AllAppsView {
+public class AllAppsTabbed extends TabHost implements AllAppsView, LauncherAnimatable {
 
     private static final String TAG = "Launcher.AllAppsTabbed";
 
@@ -46,6 +49,7 @@
     private static final String TAG_DOWNLOADED = "DOWNLOADED";
 
     private AllAppsPagedView mAllApps;
+    private View mTabBar;
     private Context mContext;
     private final LayoutInflater mInflater;
     private boolean mFirstLayout = true;
@@ -64,6 +68,8 @@
         try {
             mAllApps = (AllAppsPagedView) findViewById(R.id.all_apps_paged_view);
             if (mAllApps == null) throw new Resources.NotFoundException();
+            mTabBar = findViewById(R.id.all_apps_tab_bar);
+            if (mTabBar == null) throw new Resources.NotFoundException();
         } catch (Resources.NotFoundException e) {
             Log.e(TAG, "Can't find necessary layout elements for AllAppsTabbed");
         }
@@ -164,6 +170,28 @@
     }
 
     @Override
+    public void setFastAlpha(float alpha) {
+        final ViewGroup allAppsParent = (ViewGroup) mAllApps.getParent();
+        final ViewGroup tabBarParent = (ViewGroup) mAllApps.getParent();
+        mAllApps.setFastAlpha(alpha);
+        allAppsParent.fastInvalidate();
+        mTabBar.setFastAlpha(alpha);
+        tabBarParent.fastInvalidate();
+    }
+
+    @Override
+    public void onLauncherAnimationStart() {
+        mTabBar.setLayerType(LAYER_TYPE_HARDWARE, null);
+        mAllApps.setLayerType(LAYER_TYPE_HARDWARE, null);
+    }
+
+    @Override
+    public void onLauncherAnimationEnd() {
+        mTabBar.setLayerType(LAYER_TYPE_NONE, null);
+        mAllApps.setLayerType(LAYER_TYPE_NONE, null);
+    }
+
+    @Override
     public void setApps(ArrayList<ApplicationInfo> list) {
         mAllApps.setApps(list);
     }
diff --git a/src/com/android/launcher2/CustomizeTrayTabHost.java b/src/com/android/launcher2/CustomizeTrayTabHost.java
new file mode 100644
index 0000000..5029172
--- /dev/null
+++ b/src/com/android/launcher2/CustomizeTrayTabHost.java
@@ -0,0 +1,41 @@
+/*
+ * Copyright (C) 2011 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.launcher2;
+
+import android.content.Context;
+import android.util.AttributeSet;
+import android.widget.TabHost;
+
+public class CustomizeTrayTabHost extends TabHost implements LauncherAnimatable {
+    public CustomizeTrayTabHost(Context context) {
+        super(context);
+    }
+
+    public CustomizeTrayTabHost(Context context, AttributeSet attrs) {
+        super(context, attrs);
+    }
+
+    @Override
+    public void onLauncherAnimationStart() {
+        setLayerType(LAYER_TYPE_HARDWARE, null);
+    }
+
+    @Override
+    public void onLauncherAnimationEnd() {
+        setLayerType(LAYER_TYPE_NONE, null);
+    }
+}
diff --git a/src/com/android/launcher2/Launcher.java b/src/com/android/launcher2/Launcher.java
index 039e76b..63982ba 100644
--- a/src/com/android/launcher2/Launcher.java
+++ b/src/com/android/launcher2/Launcher.java
@@ -2767,7 +2767,7 @@
             });
 
             if (toAllApps) {
-                toView.setAlpha(0f);
+                toView.setFastAlpha(0f);
                 ValueAnimator alphaAnim = ValueAnimator.ofFloat(0f, 1f).setDuration(duration);
                 alphaAnim.setInterpolator(new DecelerateInterpolator(1.5f));
                 alphaAnim.addUpdateListener(new AnimatorUpdateListener() {
@@ -2781,9 +2781,8 @@
                 alphaAnim.start();
             }
 
-            // Only use hardware layers in portrait mode, they don't give any gains in landscape
-            if (mWorkspace.getWidth() < mWorkspace.getHeight()) {
-                toView.setLayerType(View.LAYER_TYPE_HARDWARE, null);
+            if (toView instanceof LauncherAnimatable) {
+                ((LauncherAnimatable) toView).onLauncherAnimationStart();
             }
             scaleAnim.addListener(new AnimatorListenerAdapter() {
                 @Override
@@ -2793,7 +2792,7 @@
                     toView.setTranslationY(0.0f);
                     toView.setVisibility(View.VISIBLE);
                     if (!toAllApps) {
-                        toView.setAlpha(1.0f);
+                        toView.setFastAlpha(1.0f);
                     }
                 }
                 @Override
@@ -2801,9 +2800,11 @@
                     // If we don't set the final scale values here, if this animation is cancelled
                     // it will have the wrong scale value and subsequent cameraPan animations will
                     // not fix that
-                    toView.setLayerType(View.LAYER_TYPE_NONE, null);
                     toView.setScaleX(1.0f);
                     toView.setScaleY(1.0f);
+                    if (toView instanceof LauncherAnimatable) {
+                        ((LauncherAnimatable) toView).onLauncherAnimationEnd();
+                    }
                 }
             });
 
@@ -2895,13 +2896,16 @@
                     fromView.setFastAlpha(a * 1f + b * 0f);
                 }
             });
-
-            fromView.setLayerType(View.LAYER_TYPE_HARDWARE, null);
+            if (fromView instanceof LauncherAnimatable) {
+                ((LauncherAnimatable) fromView).onLauncherAnimationStart();
+            }
             alphaAnim.addListener(new AnimatorListenerAdapter() {
                 @Override
                 public void onAnimationEnd(Animator animation) {
                     fromView.setVisibility(View.GONE);
-                    fromView.setLayerType(View.LAYER_TYPE_NONE, null);
+                    if (fromView instanceof LauncherAnimatable) {
+                        ((LauncherAnimatable) fromView).onLauncherAnimationEnd();
+                    }
                 }
             });
 
@@ -3714,3 +3718,8 @@
         Log.d(TAG, "END launcher2 dump state");
     }
 }
+
+interface LauncherAnimatable {
+    void onLauncherAnimationStart();
+    void onLauncherAnimationEnd();
+}
diff --git a/src/com/android/launcher2/Workspace.java b/src/com/android/launcher2/Workspace.java
index 40dc98d..b1dd088 100644
--- a/src/com/android/launcher2/Workspace.java
+++ b/src/com/android/launcher2/Workspace.java
@@ -792,7 +792,7 @@
             }
         }
         if (keepUpdating) {
-            invalidate();
+            fastInvalidate();
         }
     }
 
@@ -1167,8 +1167,9 @@
             final long drawingTime = getDrawingTime();
             for (int i = 0; i < pageCount; i++) {
                 final View page = (View) getChildAt(i);
-
-                drawChild(canvas, page, drawingTime);
+                if (page.getVisibility() == VISIBLE && page.getAlpha() != 0f) {
+                    drawChild(canvas, page, drawingTime);
+                }
             }
         } else {
             super.dispatchDraw(canvas);