Fix widget longpress issue where longpress misfires (issue 10988288)

Change-Id: Ibe4aa4d92a1b419b63ff3cf61164c637fb579221
diff --git a/src/com/android/launcher3/DragLayer.java b/src/com/android/launcher3/DragLayer.java
index 3c955cb..89f8275 100644
--- a/src/com/android/launcher3/DragLayer.java
+++ b/src/com/android/launcher3/DragLayer.java
@@ -69,6 +69,8 @@
     public static final int ANIMATION_END_FADE_OUT = 1;
     public static final int ANIMATION_END_REMAIN_VISIBLE = 2;
 
+    private TouchCompleteListener mTouchCompleteListener;
+
     private final Rect mInsets = new Rect();
 
     /**
@@ -173,10 +175,17 @@
 
     @Override
     public boolean onInterceptTouchEvent(MotionEvent ev) {
-        if (ev.getAction() == MotionEvent.ACTION_DOWN) {
+        int action = ev.getAction();
+
+        if (action == MotionEvent.ACTION_DOWN) {
             if (handleTouchDown(ev, true)) {
                 return true;
             }
+        } else if (action == MotionEvent.ACTION_UP || action == MotionEvent.ACTION_CANCEL) {
+            if (mTouchCompleteListener != null) {
+                mTouchCompleteListener.onTouchComplete();
+            }
+            mTouchCompleteListener = null;
         }
         clearAllResizeFrames();
         return mDragController.onInterceptTouchEvent(ev);
@@ -278,12 +287,15 @@
         int x = (int) ev.getX();
         int y = (int) ev.getY();
 
-        if (ev.getAction() == MotionEvent.ACTION_DOWN) {
-            if (ev.getAction() == MotionEvent.ACTION_DOWN) {
-                if (handleTouchDown(ev, false)) {
-                    return true;
-                }
+        if (action == MotionEvent.ACTION_DOWN) {
+            if (handleTouchDown(ev, false)) {
+                return true;
             }
+        } else if (action == MotionEvent.ACTION_UP || action == MotionEvent.ACTION_CANCEL) {
+            if (mTouchCompleteListener != null) {
+                mTouchCompleteListener.onTouchComplete();
+            }
+            mTouchCompleteListener = null;
         }
 
         if (mCurrentResizeFrame != null) {
@@ -818,4 +830,12 @@
             }
         }
     }
+
+    public void setTouchCompleteListener(TouchCompleteListener listener) {
+        mTouchCompleteListener = listener;
+    }
+
+    public interface TouchCompleteListener {
+        public void onTouchComplete();
+    }
 }
diff --git a/src/com/android/launcher3/LauncherAppWidgetHostView.java b/src/com/android/launcher3/LauncherAppWidgetHostView.java
index 59bd307..83aef1a 100644
--- a/src/com/android/launcher3/LauncherAppWidgetHostView.java
+++ b/src/com/android/launcher3/LauncherAppWidgetHostView.java
@@ -24,20 +24,24 @@
 import android.view.ViewGroup;
 import android.widget.RemoteViews;
 
+import com.android.launcher3.DragLayer.TouchCompleteListener;
+
 /**
  * {@inheritDoc}
  */
-public class LauncherAppWidgetHostView extends AppWidgetHostView {
+public class LauncherAppWidgetHostView extends AppWidgetHostView implements TouchCompleteListener {
     private CheckLongPressHelper mLongPressHelper;
     private LayoutInflater mInflater;
     private Context mContext;
     private int mPreviousOrientation;
+    private DragLayer mDragLayer;
 
     public LauncherAppWidgetHostView(Context context) {
         super(context);
         mContext = context;
         mLongPressHelper = new CheckLongPressHelper(this);
         mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
+        mDragLayer = ((Launcher) context).getDragLayer();
     }
 
     @Override
@@ -72,6 +76,7 @@
         switch (ev.getAction()) {
             case MotionEvent.ACTION_DOWN: {
                 mLongPressHelper.postCheckForLongPress();
+                mDragLayer.setTouchCompleteListener(this);
                 break;
             }
 
@@ -100,7 +105,11 @@
     @Override
     public void cancelLongPress() {
         super.cancelLongPress();
+        mLongPressHelper.cancelLongPress();
+    }
 
+    @Override
+    public void onTouchComplete() {
         mLongPressHelper.cancelLongPress();
     }
 
@@ -108,4 +117,6 @@
     public int getDescendantFocusability() {
         return ViewGroup.FOCUS_BLOCK_DESCENDANTS;
     }
+
+
 }