Disabling history

- Fixing regression in scrolling from the back of the stack to the front
  where bindVisibleTaskViews() would be called early causing jank and
  task views to be returned to the view pool before the animation was
  complete.

Change-Id: Ib68495a2e3b34f92a4971dd6b32b7bc6c616ac23
diff --git a/packages/SystemUI/src/com/android/systemui/recents/RecentsActivity.java b/packages/SystemUI/src/com/android/systemui/recents/RecentsActivity.java
index 9d4f425..c2a6108 100644
--- a/packages/SystemUI/src/com/android/systemui/recents/RecentsActivity.java
+++ b/packages/SystemUI/src/com/android/systemui/recents/RecentsActivity.java
@@ -230,7 +230,7 @@
      * Dismisses the history view back into the stack view.
      */
     boolean dismissHistory() {
-        if (mRecentsView.isHistoryVisible()) {
+        if (RecentsDebugFlags.Static.EnableHistory && mRecentsView.isHistoryVisible()) {
             EventBus.getDefault().send(new HideHistoryEvent(true /* animate */));
             return true;
         }
@@ -447,7 +447,7 @@
 
         // Reset some states
         mIgnoreAltTabRelease = false;
-        if (mRecentsView.isHistoryVisible()) {
+        if (RecentsDebugFlags.Static.EnableHistory && mRecentsView.isHistoryVisible()) {
             EventBus.getDefault().send(new HideHistoryEvent(false /* animate */));
         }
 
@@ -503,13 +503,16 @@
     @Override
     protected void onSaveInstanceState(Bundle outState) {
         super.onSaveInstanceState(outState);
-        outState.putBoolean(KEY_SAVED_STATE_HISTORY_VISIBLE, mRecentsView.isHistoryVisible());
+        if (RecentsDebugFlags.Static.EnableHistory) {
+            outState.putBoolean(KEY_SAVED_STATE_HISTORY_VISIBLE, mRecentsView.isHistoryVisible());
+        }
     }
 
     @Override
     protected void onRestoreInstanceState(Bundle savedInstanceState) {
         super.onRestoreInstanceState(savedInstanceState);
-        if (savedInstanceState.getBoolean(KEY_SAVED_STATE_HISTORY_VISIBLE, false)) {
+        if (RecentsDebugFlags.Static.EnableHistory &&
+                savedInstanceState.getBoolean(KEY_SAVED_STATE_HISTORY_VISIBLE, false)) {
             EventBus.getDefault().send(new ShowHistoryEvent());
         }
     }
@@ -603,7 +606,7 @@
     /**** EventBus events ****/
 
     public final void onBusEvent(ToggleRecentsEvent event) {
-        if (!dismissHistory()) {
+        if (!RecentsDebugFlags.Static.EnableHistory || !dismissHistory()) {
             RecentsActivityLaunchState launchState = Recents.getConfiguration().getLaunchState();
             if (launchState.launchedFromHome) {
                 dismissRecentsToHome(true /* animateTaskViews */);
@@ -614,7 +617,7 @@
     }
 
     public final void onBusEvent(IterateRecentsEvent event) {
-        if (!dismissHistory()) {
+        if (!RecentsDebugFlags.Static.EnableHistory || !dismissHistory()) {
             final RecentsDebugFlags debugFlags = Recents.getDebugFlags();
 
             // Start dozing after the recents button is clicked
@@ -651,7 +654,7 @@
             }
         } else if (event.triggeredFromHomeKey) {
             // Otherwise, dismiss Recents to Home
-            if (mRecentsView.isHistoryVisible()) {
+            if (RecentsDebugFlags.Static.EnableHistory && mRecentsView.isHistoryVisible()) {
                 // If the history view is visible, then just cross-fade home
                 ActivityOptions opts = ActivityOptions.makeCustomAnimation(RecentsActivity.this,
                                 R.anim.recents_to_launcher_enter,
diff --git a/packages/SystemUI/src/com/android/systemui/recents/RecentsDebugFlags.java b/packages/SystemUI/src/com/android/systemui/recents/RecentsDebugFlags.java
index 711d834..cd64323 100644
--- a/packages/SystemUI/src/com/android/systemui/recents/RecentsDebugFlags.java
+++ b/packages/SystemUI/src/com/android/systemui/recents/RecentsDebugFlags.java
@@ -37,6 +37,8 @@
         public static final boolean DisableBackgroundCache = false;
         // Enables the task affiliations
         public static final boolean EnableAffiliatedTaskGroups = true;
+        // Enables the history
+        public static final boolean EnableHistory = false;
         // Overrides the Tuner flags and enables the fast toggle and timeout
         public static final boolean EnableFastToggleTimeoutOverride = true;
 
diff --git a/packages/SystemUI/src/com/android/systemui/recents/views/RecentsView.java b/packages/SystemUI/src/com/android/systemui/recents/views/RecentsView.java
index c4db485..2e45627 100644
--- a/packages/SystemUI/src/com/android/systemui/recents/views/RecentsView.java
+++ b/packages/SystemUI/src/com/android/systemui/recents/views/RecentsView.java
@@ -143,21 +143,24 @@
         final float cornerRadius = context.getResources().getDimensionPixelSize(
                 R.dimen.recents_task_view_rounded_corners_radius);
         LayoutInflater inflater = LayoutInflater.from(context);
-        mHistoryButton = (TextView) inflater.inflate(R.layout.recents_history_button, this, false);
-        mHistoryButton.setOnClickListener(new View.OnClickListener() {
-            @Override
-            public void onClick(View v) {
-                EventBus.getDefault().send(new ToggleHistoryEvent());
-            }
-        });
-        addView(mHistoryButton);
-        mHistoryButton.setClipToOutline(true);
-        mHistoryButton.setOutlineProvider(new ViewOutlineProvider() {
-            @Override
-            public void getOutline(View view, Outline outline) {
-                outline.setRoundRect(0, 0, view.getWidth(), view.getHeight(), cornerRadius);
-            }
-        });
+        if (RecentsDebugFlags.Static.EnableHistory) {
+            mHistoryButton = (TextView) inflater.inflate(R.layout.recents_history_button, this,
+                    false);
+            mHistoryButton.setOnClickListener(new View.OnClickListener() {
+                @Override
+                public void onClick(View v) {
+                    EventBus.getDefault().send(new ToggleHistoryEvent());
+                }
+            });
+            addView(mHistoryButton);
+            mHistoryButton.setClipToOutline(true);
+            mHistoryButton.setOutlineProvider(new ViewOutlineProvider() {
+                @Override
+                public void getOutline(View view, Outline outline) {
+                    outline.setRoundRect(0, 0, view.getWidth(), view.getHeight(), cornerRadius);
+                }
+            });
+        }
         mEmptyView = inflater.inflate(R.layout.recents_empty, this, false);
         addView(mEmptyView);
 
@@ -331,7 +334,9 @@
         mTaskStackView.setVisibility(View.INVISIBLE);
         mEmptyView.setVisibility(View.VISIBLE);
         mEmptyView.bringToFront();
-        mHistoryButton.bringToFront();
+        if (RecentsDebugFlags.Static.EnableHistory) {
+            mHistoryButton.bringToFront();
+        }
     }
 
     /**
@@ -347,7 +352,9 @@
         if (mSearchBar != null) {
             mSearchBar.bringToFront();
         }
-        mHistoryButton.bringToFront();
+        if (RecentsDebugFlags.Static.EnableHistory) {
+            mHistoryButton.bringToFront();
+        }
     }
 
     @Override
@@ -397,21 +404,23 @@
                     MeasureSpec.makeMeasureSpec(height, MeasureSpec.EXACTLY));
         }
 
-        // Measure the history view
-        if (mHistoryView != null && mHistoryView.getVisibility() != GONE) {
-            measureChild(mHistoryView, MeasureSpec.makeMeasureSpec(width, MeasureSpec.EXACTLY),
-                    MeasureSpec.makeMeasureSpec(height, MeasureSpec.EXACTLY));
-        }
+        if (RecentsDebugFlags.Static.EnableHistory) {
+            // Measure the history view
+            if (mHistoryView != null && mHistoryView.getVisibility() != GONE) {
+                measureChild(mHistoryView, MeasureSpec.makeMeasureSpec(width, MeasureSpec.EXACTLY),
+                        MeasureSpec.makeMeasureSpec(height, MeasureSpec.EXACTLY));
+            }
 
-        // Measure the history button within the constraints of the space above the stack
-        Rect historyButtonRect = mTaskStackView.mLayoutAlgorithm.mHistoryButtonRect;
-        measureChild(mHistoryButton,
-                MeasureSpec.makeMeasureSpec(historyButtonRect.width(), MeasureSpec.AT_MOST),
-                MeasureSpec.makeMeasureSpec(historyButtonRect.height(), MeasureSpec.AT_MOST));
-        if (mHistoryClearAllButton != null && mHistoryClearAllButton.getVisibility() != GONE) {
-            measureChild(mHistoryClearAllButton,
+            // Measure the history button within the constraints of the space above the stack
+            Rect historyButtonRect = mTaskStackView.mLayoutAlgorithm.mHistoryButtonRect;
+            measureChild(mHistoryButton,
                     MeasureSpec.makeMeasureSpec(historyButtonRect.width(), MeasureSpec.AT_MOST),
                     MeasureSpec.makeMeasureSpec(historyButtonRect.height(), MeasureSpec.AT_MOST));
+            if (mHistoryClearAllButton != null && mHistoryClearAllButton.getVisibility() != GONE) {
+                measureChild(mHistoryClearAllButton,
+                    MeasureSpec.makeMeasureSpec(historyButtonRect.width(), MeasureSpec.AT_MOST),
+                    MeasureSpec.makeMeasureSpec(historyButtonRect.height(), MeasureSpec.AT_MOST));
+            }
         }
 
         setMeasuredDimension(width, height);
@@ -443,36 +452,39 @@
             mEmptyView.layout(left, top, right, bottom);
         }
 
-        // Layout the history view
-        if (mHistoryView != null && mHistoryView.getVisibility() != GONE) {
-            mHistoryView.layout(left, top, right, bottom);
-        }
+        if (RecentsDebugFlags.Static.EnableHistory) {
+            // Layout the history view
+            if (mHistoryView != null && mHistoryView.getVisibility() != GONE) {
+                mHistoryView.layout(left, top, right, bottom);
+            }
 
-        // Layout the history button such that its drawable is start-aligned with the stack,
-        // vertically centered in the available space above the stack
-        Rect historyButtonRect = mTaskStackView.mLayoutAlgorithm.mHistoryButtonRect;
-        int historyLeft = isLayoutRtl()
-                ? historyButtonRect.right + mHistoryButton.getPaddingStart()
-                        - mHistoryButton.getMeasuredWidth()
-                : historyButtonRect.left - mHistoryButton.getPaddingStart();
-        int historyTop = historyButtonRect.top +
-                (historyButtonRect.height() - mHistoryButton.getMeasuredHeight()) / 2;
-        mHistoryButton.layout(historyLeft, historyTop,
-                historyLeft + mHistoryButton.getMeasuredWidth(),
-                historyTop + mHistoryButton.getMeasuredHeight());
+            // Layout the history button such that its drawable is start-aligned with the stack,
+            // vertically centered in the available space above the stack
+            Rect historyButtonRect = mTaskStackView.mLayoutAlgorithm.mHistoryButtonRect;
+            int historyLeft = isLayoutRtl()
+                    ? historyButtonRect.right + mHistoryButton.getPaddingStart()
+                    - mHistoryButton.getMeasuredWidth()
+                    : historyButtonRect.left - mHistoryButton.getPaddingStart();
+            int historyTop = historyButtonRect.top +
+                    (historyButtonRect.height() - mHistoryButton.getMeasuredHeight()) / 2;
+            mHistoryButton.layout(historyLeft, historyTop,
+                    historyLeft + mHistoryButton.getMeasuredWidth(),
+                    historyTop + mHistoryButton.getMeasuredHeight());
 
-        // Layout the history clear all button such that it is end-aligned with the stack,
-        // vertically centered in the available space above the stack
-        if (mHistoryClearAllButton != null && mHistoryClearAllButton.getVisibility() != GONE) {
-            int clearAllLeft = isLayoutRtl()
-                    ? historyButtonRect.left - mHistoryClearAllButton.getPaddingStart()
-                    : historyButtonRect.right + mHistoryClearAllButton.getPaddingStart()
-                            - mHistoryClearAllButton.getMeasuredWidth();
-            int clearAllTop = historyButtonRect.top +
-                    (historyButtonRect.height() - mHistoryClearAllButton.getMeasuredHeight()) / 2;
-            mHistoryClearAllButton.layout(clearAllLeft, clearAllTop,
-                    clearAllLeft + mHistoryClearAllButton.getMeasuredWidth(),
-                    clearAllTop + mHistoryClearAllButton.getMeasuredHeight());
+            // Layout the history clear all button such that it is end-aligned with the stack,
+            // vertically centered in the available space above the stack
+            if (mHistoryClearAllButton != null && mHistoryClearAllButton.getVisibility() != GONE) {
+                int clearAllLeft = isLayoutRtl()
+                        ? historyButtonRect.left - mHistoryClearAllButton.getPaddingStart()
+                        : historyButtonRect.right + mHistoryClearAllButton.getPaddingStart()
+                        - mHistoryClearAllButton.getMeasuredWidth();
+                int clearAllTop = historyButtonRect.top +
+                        (historyButtonRect.height() - mHistoryClearAllButton.getMeasuredHeight()) /
+                                2;
+                mHistoryClearAllButton.layout(clearAllLeft, clearAllTop,
+                        clearAllLeft + mHistoryClearAllButton.getMeasuredWidth(),
+                        clearAllTop + mHistoryClearAllButton.getMeasuredHeight());
+            }
         }
 
         if (mAwaitingFirstLayout) {
@@ -540,9 +552,11 @@
     }
 
     public final void onBusEvent(DismissRecentsToHomeAnimationStarted event) {
-        // Hide the history button
         int taskViewExitToHomeDuration = TaskStackAnimationHelper.EXIT_TO_HOME_TRANSLATION_DURATION;
-        hideHistoryButton(taskViewExitToHomeDuration, false /* translate */);
+        if (RecentsDebugFlags.Static.EnableHistory) {
+            // Hide the history button
+            hideHistoryButton(taskViewExitToHomeDuration, false /* translate */);
+        }
         animateBackgroundScrim(0f, taskViewExitToHomeDuration);
     }
 
@@ -675,11 +689,17 @@
             // Reset the view state
             mAwaitingFirstLayout = true;
             mLastTaskLaunchedWasFreeform = false;
-            hideHistoryButton(0, false /* translate */);
+            if (RecentsDebugFlags.Static.EnableHistory) {
+                hideHistoryButton(0, false /* translate */);
+            }
         }
     }
 
     public final void onBusEvent(ToggleHistoryEvent event) {
+        if (!RecentsDebugFlags.Static.EnableHistory) {
+            return;
+        }
+
         if (mHistoryView != null && mHistoryView.isVisible()) {
             EventBus.getDefault().send(new HideHistoryEvent(true /* animate */));
         } else {
@@ -688,6 +708,10 @@
     }
 
     public final void onBusEvent(ShowHistoryEvent event) {
+        if (!RecentsDebugFlags.Static.EnableHistory) {
+            return;
+        }
+
         if (mHistoryView == null) {
             LayoutInflater inflater = LayoutInflater.from(getContext());
             mHistoryView = (RecentsHistoryView) inflater.inflate(R.layout.recents_history, this,
@@ -746,6 +770,10 @@
     }
 
     public final void onBusEvent(HideHistoryEvent event) {
+        if (!RecentsDebugFlags.Static.EnableHistory) {
+            return;
+        }
+
         // Animate the empty view in parallel with the history view (the task view animations are
         // handled in TaskStackView)
         Rect stackRect = mTaskStackView.mLayoutAlgorithm.mStackRect;
@@ -765,10 +793,18 @@
     }
 
     public final void onBusEvent(ShowHistoryButtonEvent event) {
+        if (!RecentsDebugFlags.Static.EnableHistory) {
+            return;
+        }
+
         showHistoryButton(150, event.translate);
     }
 
     public final void onBusEvent(HideHistoryButtonEvent event) {
+        if (!RecentsDebugFlags.Static.EnableHistory) {
+            return;
+        }
+
         hideHistoryButton(100, true /* translate */);
     }
 
@@ -776,6 +812,10 @@
      * Shows the history button.
      */
     private void showHistoryButton(final int duration, final boolean translate) {
+        if (!RecentsDebugFlags.Static.EnableHistory) {
+            return;
+        }
+
         final ReferenceCountedTrigger postAnimationTrigger = new ReferenceCountedTrigger();
         if (mHistoryButton.getVisibility() == View.INVISIBLE) {
             mHistoryButton.setVisibility(View.VISIBLE);
@@ -808,6 +848,10 @@
      * Hides the history button.
      */
     private void hideHistoryButton(int duration, boolean translate) {
+        if (!RecentsDebugFlags.Static.EnableHistory) {
+            return;
+        }
+
         final ReferenceCountedTrigger postAnimationTrigger = new ReferenceCountedTrigger();
         hideHistoryButton(duration, translate, postAnimationTrigger);
         postAnimationTrigger.flushLastDecrementRunnables();
@@ -818,6 +862,10 @@
      */
     private void hideHistoryButton(int duration, boolean translate,
             final ReferenceCountedTrigger postAnimationTrigger) {
+        if (!RecentsDebugFlags.Static.EnableHistory) {
+            return;
+        }
+
         if (mHistoryButton.getVisibility() == View.VISIBLE) {
             if (translate) {
                 mHistoryButton.animate()
diff --git a/packages/SystemUI/src/com/android/systemui/recents/views/TaskStackAnimationHelper.java b/packages/SystemUI/src/com/android/systemui/recents/views/TaskStackAnimationHelper.java
index 890b445..2cd0c19 100644
--- a/packages/SystemUI/src/com/android/systemui/recents/views/TaskStackAnimationHelper.java
+++ b/packages/SystemUI/src/com/android/systemui/recents/views/TaskStackAnimationHelper.java
@@ -467,12 +467,13 @@
 
         // Setup the end listener to return all the hidden views to the view pool after the
         // focus animation
-        AnimatorListenerAdapter endListener = new AnimatorListenerAdapter() {
+        ReferenceCountedTrigger postAnimTrigger = new ReferenceCountedTrigger();
+        postAnimTrigger.addLastDecrementRunnable(new Runnable() {
             @Override
-            public void onAnimationEnd(Animator animation) {
+            public void run() {
                 mStackView.bindVisibleTaskViews(newScroll);
             }
-        };
+        });
 
         List<TaskView> taskViews = mStackView.getTaskViews();
         int taskViewCount = taskViews.size();
@@ -513,7 +514,8 @@
             AnimationProps anim = new AnimationProps()
                     .setDuration(AnimationProps.BOUNDS, duration)
                     .setInterpolator(AnimationProps.BOUNDS, interpolator)
-                    .setListener(endListener);
+                    .setListener(postAnimTrigger.decrementOnAnimationEnd());
+            postAnimTrigger.increment();
             mStackView.updateTaskViewToTransform(tv, toTransform, anim);
         }
         return willScroll;