Clamping touch positions to drag layer rect to prevent dragging outside of bounds.

- Fixing small issue with default widget preview aspect ratios

Change-Id: I2dca0524e8aa7c48345b424bad889736fa345386
diff --git a/src/com/android/launcher2/DragController.java b/src/com/android/launcher2/DragController.java
index f6058a0..5aecede 100644
--- a/src/com/android/launcher2/DragController.java
+++ b/src/com/android/launcher2/DragController.java
@@ -110,6 +110,9 @@
     private int mLastTouch[] = new int[2];
     private int mDistanceSinceScroll = 0;
 
+    private int mTmpPoint[] = new int[2];
+    private Rect mDragLayerRect = new Rect();
+
     /**
      * Interface to receive notifications when a drag starts or stops
      */
@@ -385,6 +388,16 @@
     }
 
     /**
+     * Clamps the position to the drag layer bounds.
+     */
+    private int[] getClampedDragLayerPos(float x, float y) {
+        mLauncher.getDragLayer().getLocalVisibleRect(mDragLayerRect);
+        mTmpPoint[0] = (int) Math.max(mDragLayerRect.left, Math.min(x, mDragLayerRect.right - 1));
+        mTmpPoint[1] = (int) Math.max(mDragLayerRect.top, Math.min(y, mDragLayerRect.bottom - 1));
+        return mTmpPoint;
+    }
+
+    /**
      * Call this from a drag source view.
      */
     public boolean onInterceptTouchEvent(MotionEvent ev) {
@@ -394,8 +407,9 @@
         }
         final int action = ev.getAction();
 
-        final int dragLayerX = (int) ev.getX();
-        final int dragLayerY = (int) ev.getY();
+        final int[] dragLayerPos = getClampedDragLayerPos(ev.getX(), ev.getY());
+        final int dragLayerX = dragLayerPos[0];
+        final int dragLayerY = dragLayerPos[1];
 
         switch (action) {
             case MotionEvent.ACTION_MOVE:
@@ -506,8 +520,9 @@
         }
 
         final int action = ev.getAction();
-        final int dragLayerX = (int) ev.getX();
-        final int dragLayerY = (int) ev.getY();
+        final int[] dragLayerPos = getClampedDragLayerPos(ev.getX(), ev.getY());
+        final int dragLayerX = dragLayerPos[0];
+        final int dragLayerY = dragLayerPos[1];
 
         switch (action) {
         case MotionEvent.ACTION_DOWN: