Adding animations when dropping on delete / uninstall drop target

-> issue 5043661

Change-Id: I4e4830acc15e006e637b35c3d0dcc72c23414b95
diff --git a/src/com/android/launcher2/AppsCustomizePagedView.java b/src/com/android/launcher2/AppsCustomizePagedView.java
index f059469..c127ecd 100644
--- a/src/com/android/launcher2/AppsCustomizePagedView.java
+++ b/src/com/android/launcher2/AppsCustomizePagedView.java
@@ -449,7 +449,8 @@
     }
     private void endDragging(View target, boolean success) {
         mLauncher.getWorkspace().onDragStopped(success);
-        if (!success || target != mLauncher.getWorkspace()) {
+        if (!success || (target != mLauncher.getWorkspace() &&
+                !(target instanceof DeleteDropTarget))) {
             // Exit spring loaded mode if we have not successfully dropped or have not handled the
             // drop in Workspace
             mLauncher.exitSpringLoadedDragMode();
diff --git a/src/com/android/launcher2/ButtonDropTarget.java b/src/com/android/launcher2/ButtonDropTarget.java
index edc5acf..138770a 100644
--- a/src/com/android/launcher2/ButtonDropTarget.java
+++ b/src/com/android/launcher2/ButtonDropTarget.java
@@ -21,6 +21,7 @@
 import android.graphics.Paint;
 import android.util.AttributeSet;
 import android.widget.FrameLayout;
+import android.widget.TextView;
 
 import com.android.launcher.R;
 
@@ -34,6 +35,8 @@
 
     protected Launcher mLauncher;
     private int mBottomDragPadding;
+    protected TextView mText;
+    protected SearchDropTargetBar mSearchDropTargetBar;
 
     /** Whether this drop target is active for the current drag */
     protected boolean mActive;
@@ -61,8 +64,11 @@
         return false;
     }
 
+    public void setSearchDropTargetBar(SearchDropTargetBar searchDropTargetBar) {
+        mSearchDropTargetBar = searchDropTargetBar;
+    }
+
     public void onDrop(DragObject d) {
-        // Do nothing
     }
 
     public void onDragEnter(DragObject d) {
diff --git a/src/com/android/launcher2/DeleteDropTarget.java b/src/com/android/launcher2/DeleteDropTarget.java
index ffe4533..2c84c78 100644
--- a/src/com/android/launcher2/DeleteDropTarget.java
+++ b/src/com/android/launcher2/DeleteDropTarget.java
@@ -22,16 +22,19 @@
 import android.content.res.Resources;
 import android.graphics.PorterDuff;
 import android.graphics.PorterDuffColorFilter;
+import android.graphics.Rect;
 import android.graphics.drawable.TransitionDrawable;
 import android.util.AttributeSet;
 import android.view.View;
+import android.view.animation.AccelerateInterpolator;
+import android.view.animation.DecelerateInterpolator;
 import android.widget.TextView;
 
 import com.android.launcher.R;
 
 public class DeleteDropTarget extends ButtonDropTarget {
 
-    private TextView mText;
+    private static int DELETE_ANIMATION_DURATION = 220;
     private ColorStateList mOriginalTextColor;
     private TransitionDrawable mDrawable;
     private int mHoverColor = 0xFFFF0000;
@@ -147,7 +150,37 @@
         }
     }
 
-    public void onDrop(DragObject d) {
+    private void animateToTrashAndCompleteDrop(final DragObject d) {
+        DragLayer dragLayer = mLauncher.getDragLayer();
+        Rect from = new Rect();
+        Rect to = new Rect();
+        dragLayer.getViewRectRelativeToSelf(d.dragView, from);
+        dragLayer.getViewRectRelativeToSelf(mText, to);
+
+        int width = mDrawable.getIntrinsicWidth();
+        int height = mDrawable.getIntrinsicHeight();
+        to.set(to.left, to.top, to.left + width, to.bottom);
+
+        // Center the destination rect about the trash icon
+        int xOffset = (int) -(d.dragView.getMeasuredWidth() - width) / 2;
+        int yOffset = (int) -(d.dragView.getMeasuredHeight() - height) / 2;
+        to.offset(xOffset, yOffset);
+
+        mSearchDropTargetBar.deferOnDragEnd();
+        Runnable onAnimationEndRunnable = new Runnable() {
+            @Override
+            public void run() {
+                mSearchDropTargetBar.onDragEnd();
+                mLauncher.exitSpringLoadedDragMode();
+                completeDrop(d);
+            }
+        };
+        dragLayer.animateView(d.dragView, from, to, 0f, 0.1f,
+                DELETE_ANIMATION_DURATION, new DecelerateInterpolator(2),
+                new AccelerateInterpolator(2), onAnimationEndRunnable, false);
+    }
+
+    private void completeDrop(DragObject d) {
         ItemInfo item = (ItemInfo) d.dragInfo;
 
         if (isAllAppsApplication(d.dragSource, item)) {
@@ -178,4 +211,8 @@
             }
         }
     }
+
+    public void onDrop(DragObject d) {
+        animateToTrashAndCompleteDrop(d);
+    }
 }
diff --git a/src/com/android/launcher2/InfoDropTarget.java b/src/com/android/launcher2/InfoDropTarget.java
index 6ad7630..3507181 100644
--- a/src/com/android/launcher2/InfoDropTarget.java
+++ b/src/com/android/launcher2/InfoDropTarget.java
@@ -32,7 +32,6 @@
 
 public class InfoDropTarget extends ButtonDropTarget {
 
-    private TextView mText;
     private ColorStateList mOriginalTextColor;
     private TransitionDrawable mDrawable;
     private int mHoverColor = 0xFF0000FF;
diff --git a/src/com/android/launcher2/SearchDropTargetBar.java b/src/com/android/launcher2/SearchDropTargetBar.java
index 201daab..ee3ab18 100644
--- a/src/com/android/launcher2/SearchDropTargetBar.java
+++ b/src/com/android/launcher2/SearchDropTargetBar.java
@@ -50,6 +50,7 @@
     private ButtonDropTarget mInfoDropTarget;
     private ButtonDropTarget mDeleteDropTarget;
     private int mBarHeight;
+    private boolean mDeferOnDragEnd = false;
 
     public SearchDropTargetBar(Context context, AttributeSet attrs) {
         this(context, attrs, 0);
@@ -80,6 +81,9 @@
         mDeleteDropTarget = (ButtonDropTarget) mDropTargetBar.findViewById(R.id.delete_target);
         mBarHeight = getResources().getDimensionPixelSize(R.dimen.qsb_bar_height);
 
+        mInfoDropTarget.setSearchDropTargetBar(this);
+        mDeleteDropTarget.setSearchDropTargetBar(this);
+
         boolean enableDropDownDropTargets =
             getResources().getBoolean(R.bool.config_useDropTargetDownTransition);
 
@@ -191,14 +195,22 @@
         }
     }
 
+    public void deferOnDragEnd() {
+        mDeferOnDragEnd = true;
+    }
+
     @Override
     public void onDragEnd() {
-        // Restore the QSB search bar, and animate out the drop target bar
-        mDropTargetBarFadeInAnim.cancel();
-        mDropTargetBarFadeOutAnim.start();
-        if (!mIsSearchBarHidden) {
-            mQSBSearchBarFadeOutAnim.cancel();
-            mQSBSearchBarFadeInAnim.start();
+        if (!mDeferOnDragEnd) {
+            // Restore the QSB search bar, and animate out the drop target bar
+            mDropTargetBarFadeInAnim.cancel();
+            mDropTargetBarFadeOutAnim.start();
+            if (!mIsSearchBarHidden) {
+                mQSBSearchBarFadeOutAnim.cancel();
+                mQSBSearchBarFadeInAnim.start();
+            }
+        } else {
+            mDeferOnDragEnd = false;
         }
     }
 }