Fix bug 3259354 - Handle ActionBar backgrounds better for interacting
with action modes.

Tweak ActionBar/mode transition animation to look better with a
variety of action bar styles.

Fix bug 3273773 - ActionBar disappearing while displaying
popupwindow. Some SurfaceFlinger optimizations require a relayout to
recalculate the bounds of overlaid views.

Fix bug 3266010 - Cancel animations properly when switching between
modes.

Change-Id: Ic431176b11115a2211bd0a46d09c8998aefe58d6
diff --git a/core/java/com/android/internal/widget/ActionBarContextView.java b/core/java/com/android/internal/widget/ActionBarContextView.java
index 6d4c6bd..60c3487 100644
--- a/core/java/com/android/internal/widget/ActionBarContextView.java
+++ b/core/java/com/android/internal/widget/ActionBarContextView.java
@@ -159,15 +159,13 @@
     }
 
     public void initForMode(final ActionMode mode) {
-        if (mCurrentAnimation != null && mCurrentAnimation.isRunning()) {
-            mCurrentAnimation.end();
-            killMode();
-        }
+        finishAnimation();
+
         if (mClose == null) {
             LayoutInflater inflater = LayoutInflater.from(mContext);
             mClose = inflater.inflate(R.layout.action_mode_close_item, this, false);
             addView(mClose);
-        } else {
+        } else if (mClose.getParent() == null) {
             addView(mClose);
         }
 
@@ -188,6 +186,10 @@
     }
 
     public void closeMode() {
+        if (mAnimationMode == ANIMATE_OUT) {
+            // Called again during close; just finish what we were doing.
+            return;
+        }
         if (mClose == null) {
             killMode();
             return;
@@ -200,8 +202,10 @@
     }
 
     private void finishAnimation() {
-        if (mCurrentAnimation != null && mCurrentAnimation.isRunning()) {
-            mCurrentAnimation.end();
+        final Animator a = mCurrentAnimation;
+        if (a != null && a.isRunning()) {
+            mCurrentAnimation = null;
+            a.end();
         }
     }