Further improving performance of All Apps transition

Change-Id: Ie8a2af004acba4c13066dd636842a71793064688
diff --git a/src/com/android/launcher2/AllAppsBackground.java b/src/com/android/launcher2/AllAppsBackground.java
new file mode 100644
index 0000000..5292d0a
--- /dev/null
+++ b/src/com/android/launcher2/AllAppsBackground.java
@@ -0,0 +1,53 @@
+/*
+ * Copyright (C) 2010 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 com.android.launcher.R;
+
+import android.content.Context;
+import android.graphics.Canvas;
+import android.graphics.drawable.Drawable;
+import android.util.AttributeSet;
+import android.view.View;
+
+/**
+ * An implementation of PagedView that populates the pages of the workspace
+ * with all of the user's applications.
+ */
+public class AllAppsBackground extends View {
+    private Drawable mBackground;
+
+    public AllAppsBackground(Context context) {
+        this(context, null);
+    }
+
+    public AllAppsBackground(Context context, AttributeSet attrs) {
+        this(context, attrs, 0);
+    }
+
+    public AllAppsBackground(Context context, AttributeSet attrs, int defStyle) {
+        super(context, attrs, defStyle);
+        mBackground = getResources().getDrawable(R.drawable.all_apps_bg_gradient);
+    }
+
+    @Override
+    public void onDraw(Canvas canvas) {
+        mBackground.setBounds(mScrollX, 0, mScrollX + getMeasuredWidth(),
+                getMeasuredHeight());
+        mBackground.draw(canvas);
+    }
+}
diff --git a/src/com/android/launcher2/AllAppsTabbed.java b/src/com/android/launcher2/AllAppsTabbed.java
index 36594dd..6619150 100644
--- a/src/com/android/launcher2/AllAppsTabbed.java
+++ b/src/com/android/launcher2/AllAppsTabbed.java
@@ -49,7 +49,8 @@
     private static final String TAG_DOWNLOADED = "DOWNLOADED";
 
     private AllAppsPagedView mAllApps;
-    private View mTabBar;
+    private AllAppsBackground mBackground;
+    private Launcher mLauncher;
     private Context mContext;
     private final LayoutInflater mInflater;
     private boolean mFirstLayout = true;
@@ -68,8 +69,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();
+            mBackground = (AllAppsBackground) findViewById(R.id.all_apps_background);
+            if (mBackground == null) throw new Resources.NotFoundException();
         } catch (Resources.NotFoundException e) {
             Log.e(TAG, "Can't find necessary layout elements for AllAppsTabbed");
         }
@@ -126,6 +127,7 @@
     @Override
     public void setLauncher(Launcher launcher) {
         mAllApps.setLauncher(launcher);
+        mLauncher = launcher;
     }
 
     @Override
@@ -170,25 +172,21 @@
     }
 
     @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);
+        // Turn on hardware layers for performance
+        setLayerType(LAYER_TYPE_HARDWARE, null);
+        // Re-enable the rendering of the dimmed background in All Apps for performance reasons
+        mLauncher.getWorkspace().disableBackground();
+        mBackground.setVisibility(VISIBLE);
     }
 
     @Override
     public void onLauncherAnimationEnd() {
-        mTabBar.setLayerType(LAYER_TYPE_NONE, null);
-        mAllApps.setLayerType(LAYER_TYPE_NONE, null);
+        setLayerType(LAYER_TYPE_NONE, null);
+        // Move the rendering of the dimmed background to workspace after the all apps animation
+        // is done, so that the background is not rendered *above* the mini workspace screens
+        mLauncher.getWorkspace().enableBackground();
+        mBackground.setVisibility(GONE);
     }
 
     @Override
diff --git a/src/com/android/launcher2/Launcher.java b/src/com/android/launcher2/Launcher.java
index 63982ba..e2bddde 100644
--- a/src/com/android/launcher2/Launcher.java
+++ b/src/com/android/launcher2/Launcher.java
@@ -330,7 +330,7 @@
 
             // share the same customization workspace across all the tabs
             mCustomizePagedView = (CustomizePagedView) mInflater.inflate(
-                    R.layout.customization_drawer, mHomeCustomizationDrawer, false);
+                    R.layout.customization_drawer_tab_contents, mHomeCustomizationDrawer, false);
             TabContentFactory contentFactory = new TabContentFactory() {
                 public View createTabContent(String tag) {
                     return mCustomizePagedView;
@@ -2738,6 +2738,9 @@
         final int duration = toAllApps ?
                 res.getInteger(R.integer.config_allAppsZoomInTime) :
                 res.getInteger(R.integer.config_customizeZoomInTime);
+        final int fadeDuration = toAllApps ?
+                res.getInteger(R.integer.config_allAppsFadeInTime) :
+                res.getInteger(R.integer.config_customizeFadeInTime);
 
         final float scale = toAllApps ?
                 (float) res.getInteger(R.integer.config_allAppsZoomScaleFactor) :
@@ -2768,7 +2771,7 @@
 
             if (toAllApps) {
                 toView.setFastAlpha(0f);
-                ValueAnimator alphaAnim = ValueAnimator.ofFloat(0f, 1f).setDuration(duration);
+                ValueAnimator alphaAnim = ValueAnimator.ofFloat(0f, 1f).setDuration(fadeDuration);
                 alphaAnim.setInterpolator(new DecelerateInterpolator(1.5f));
                 alphaAnim.addUpdateListener(new AnimatorUpdateListener() {
                     public void onAnimationUpdate(ValueAnimator animation) {
diff --git a/src/com/android/launcher2/Workspace.java b/src/com/android/launcher2/Workspace.java
index b1dd088..263aaad 100644
--- a/src/com/android/launcher2/Workspace.java
+++ b/src/com/android/launcher2/Workspace.java
@@ -114,6 +114,7 @@
     private ValueAnimator mBackgroundFadeOutAnimation;
     private Drawable mBackground;
     private Drawable mCustomizeTrayBackground;
+    boolean mDrawBackground;
     private boolean mDrawCustomizeTrayBackground;
     private float mBackgroundAlpha = 0;
     private float mOverScrollMaxBackgroundAlpha = 0.0f;
@@ -125,6 +126,7 @@
     private float[] mCustomizationDrawerTransformedPos = new float[2];
 
     private final WallpaperManager mWallpaperManager;
+    private IBinder mWindowToken;
 
     private int mDefaultPage;
 
@@ -785,9 +787,8 @@
             updateNow = keepUpdating = mWallpaperOffset.computeScrollOffset();
         }
         if (updateNow) {
-            IBinder token = getWindowToken();
-            if (token != null) {
-                mWallpaperManager.setWallpaperOffsets(getWindowToken(),
+            if (mWindowToken != null) {
+                mWallpaperManager.setWallpaperOffsets(mWindowToken,
                         mWallpaperOffset.getCurrX(), mWallpaperOffset.getCurrY());
             }
         }
@@ -954,6 +955,13 @@
         return mChildrenOutlineAlpha;
     }
 
+    void disableBackground() {
+        mDrawBackground = false;
+    }
+    void enableBackground() {
+        mDrawBackground = true;
+    }
+
     private void showBackgroundGradientForAllApps() {
         showBackgroundGradient();
         mDrawCustomizeTrayBackground = false;
@@ -995,8 +1003,10 @@
     }
 
     public void setBackgroundAlpha(float alpha) {
-        mBackgroundAlpha = alpha;
-        invalidate();
+        if (alpha != mBackgroundAlpha) {
+            mBackgroundAlpha = alpha;
+            invalidate();
+        }
     }
 
     public float getBackgroundAlpha() {
@@ -1090,8 +1100,13 @@
 
     protected void onAttachedToWindow() {
         super.onAttachedToWindow();
+        mWindowToken = getWindowToken();
         computeScroll();
-        mDragController.setWindowToken(getWindowToken());
+        mDragController.setWindowToken(mWindowToken);
+    }
+
+    protected void onDetachedFromWindow() {
+        mWindowToken = null;
     }
 
     @Override
@@ -1126,7 +1141,7 @@
         updateWallpaperOffsets();
 
         // Draw the background gradient if necessary
-        if (mBackground != null && mBackgroundAlpha > 0.0f) {
+        if (mBackground != null && mBackgroundAlpha > 0.0f && mDrawBackground) {
             int alpha = (int) (mBackgroundAlpha * 255);
             if (mDrawCustomizeTrayBackground) {
                 // Find out where to offset the gradient for the customization tray content