Fix potential inconsistent DragLayout state

Prior to this change is was possible for DragLayout to end up in an open
state without a visible HistoryFragment. With this change we only enter
an open state after a successful view capture.

Fixes: 34157451
Test: manually verified on API 21, 23 & 25 device
Change-Id: Ia3631692ccc4281d06fd53f9b926e3043a9827f1
diff --git a/src/com/android/calculator2/DragLayout.java b/src/com/android/calculator2/DragLayout.java
index 1848e38..e8f02e5 100644
--- a/src/com/android/calculator2/DragLayout.java
+++ b/src/com/android/calculator2/DragLayout.java
@@ -87,8 +87,12 @@
 
             int top = 0;
             if (child == mHistoryFrame) {
-                top = mDragHelper.getViewDragState() != ViewDragHelper.STATE_IDLE
-                        ? child.getTop() : (mIsOpen ? 0 : -mVerticalRange);
+                if (mDragHelper.getCapturedView() == mHistoryFrame
+                        && mDragHelper.getViewDragState() != ViewDragHelper.STATE_IDLE) {
+                    top = child.getTop();
+                } else {
+                    top = mIsOpen ? 0 : -mVerticalRange;
+                }
             }
             child.layout(0, top, child.getMeasuredWidth(), top + child.getMeasuredHeight());
         }
@@ -195,18 +199,9 @@
         return mIsOpen;
     }
 
-    public void setOpen() {
-        if (!mIsOpen) {
-            mIsOpen = true;
-            mDragHelper.smoothSlideViewTo(mHistoryFrame, 0, 0);
-            mHistoryFrame.setVisibility(VISIBLE);
-        }
-    }
-
-    public void setClosed() {
+    private void setClosed() {
         if (mIsOpen) {
             mIsOpen = false;
-            mDragHelper.smoothSlideViewTo(mHistoryFrame, 0, -mVerticalRange);
             mHistoryFrame.setVisibility(View.INVISIBLE);
 
             if (mCloseCallback != null) {
@@ -215,7 +210,12 @@
         }
     }
 
-    public Animator createAnimator(final boolean toOpen) {
+    public Animator createAnimator(boolean toOpen) {
+        if (mIsOpen == toOpen) {
+            return null;
+        }
+
+        mIsOpen = true;
         mHistoryFrame.setVisibility(VISIBLE);
 
         final ValueAnimator animator = ValueAnimator.ofInt(mHistoryFrame.getTop(),
@@ -232,16 +232,14 @@
                 }
             }
         });
-        animator.addListener(new AnimatorListenerAdapter() {
-            @Override
-            public void onAnimationEnd(Animator animator) {
-                if (toOpen) {
-                    setOpen();
-                } else {
+        if (!toOpen) {
+            animator.addListener(new AnimatorListenerAdapter() {
+                @Override
+                public void onAnimationEnd(Animator animator) {
                     setClosed();
                 }
-            }
-        });
+            });
+        }
 
         return animator;
     }
@@ -292,12 +290,9 @@
         @Override
         public void onViewDragStateChanged(int state) {
             // The view stopped moving.
-            if (state == ViewDragHelper.STATE_IDLE) {
-                if (mDragHelper.getCapturedView().getTop() < -(mVerticalRange / 2)) {
-                    setClosed();
-                } else {
-                    setOpen();
-                }
+            if (state == ViewDragHelper.STATE_IDLE
+                    && mDragHelper.getCapturedView().getTop() < -(mVerticalRange / 2)) {
+                setClosed();
             }
         }