Ejemplo n.º 1
0
  /** Call this from a drag source view. */
  public boolean onInterceptTouchEvent(MotionEvent ev) {
    if (DEBUG) {
      Log.d(Launcher.TAG, "DragController.onInterceptTouchEvent " + ev + " mDragging=" + mDragging);
    }
    final int action = ev.getAction();

    if (action == MotionEvent.ACTION_DOWN) {
      recordScreenSize();
    }

    final int screenX = clamp((int) ev.getRawX(), 0, mDisplayMetrics.widthPixels);
    final int screenY = clamp((int) ev.getRawY(), 0, mDisplayMetrics.heightPixels);

    switch (action) {
      case MotionEvent.ACTION_MOVE:
        break;

      case MotionEvent.ACTION_DOWN:
        // Remember location of down touch
        mMotionDownX = screenX;
        mMotionDownY = screenY;
        mLastDropTarget = null;
        break;

      case MotionEvent.ACTION_CANCEL:
      case MotionEvent.ACTION_UP:
        if (mDragging) {
          drop(screenX, screenY);
        }
        endDrag();
        break;
    }

    return mDragging;
  }
Ejemplo n.º 2
0
  /** Call this from a drag source view. */
  public boolean onTouchEvent(MotionEvent ev) {
    View scrollView = mScrollView;

    if (!mDragging) {
      return false;
    }

    final int action = ev.getAction();
    final int screenX = clamp((int) ev.getRawX(), 0, mDisplayMetrics.widthPixels);
    final int screenY = clamp((int) ev.getRawY(), 0, mDisplayMetrics.heightPixels);

    switch (action) {
      case MotionEvent.ACTION_DOWN:
        // Remember where the motion event started
        mMotionDownX = screenX;
        mMotionDownY = screenY;

        if ((screenX < SCROLL_ZONE)
            || (screenY < Launcher.mScreenHeight - Workspace.workspaceBottom)
            || (screenX > scrollView.getWidth() - SCROLL_ZONE)) {
          mScrollState = SCROLL_WAITING_IN_ZONE;
          mHandler.postDelayed(mScrollRunnable, SCROLL_DELAY);
        } else {
          mScrollState = SCROLL_OUTSIDE_ZONE;
        }

        break;
      case MotionEvent.ACTION_MOVE:
        // Update the drag view. Don't use the clamped pos here so the
        // dragging looks
        // like it goes off screen a little, intead of bumping up against
        // the edge.
        mDragView.move((int) ev.getRawX(), (int) ev.getRawY());

        // Drop on someone?
        final int[] coordinates = mCoordinatesTemp;
        DropTarget dropTarget = findDropTarget(screenX, screenY, coordinates);
        if (dropTarget != null) {
          if (mLastDropTarget == dropTarget) {
            dropTarget.onDragOver(
                mDragSource,
                coordinates[0],
                coordinates[1],
                (int) mTouchOffsetX,
                (int) mTouchOffsetY,
                mDragView,
                mDragInfo);
          } else {
            if (mLastDropTarget != null) {
              mLastDropTarget.onDragExit(
                  mDragSource,
                  coordinates[0],
                  coordinates[1],
                  (int) mTouchOffsetX,
                  (int) mTouchOffsetY,
                  mDragView,
                  mDragInfo);
            }
            dropTarget.onDragEnter(
                mDragSource,
                coordinates[0],
                coordinates[1],
                (int) mTouchOffsetX,
                (int) mTouchOffsetY,
                mDragView,
                mDragInfo);
          }
        } else {
          if (mLastDropTarget != null) {
            mLastDropTarget.onDragExit(
                mDragSource,
                coordinates[0],
                coordinates[1],
                (int) mTouchOffsetX,
                (int) mTouchOffsetY,
                mDragView,
                mDragInfo);
          }
        }
        mLastDropTarget = dropTarget;

        // Scroll, maybe, but not if we're in the delete region.
        boolean inDeleteRegion = false;
        if (mDeleteRegion != null) {
          inDeleteRegion = mDeleteRegion.contains(screenX, screenY);
        }
        if (!inDeleteRegion
            && screenX < SCROLL_ZONE
            && (screenY < Launcher.mScreenHeight - Workspace.workspaceBottom)) {
          if (mScrollState == SCROLL_OUTSIDE_ZONE) {
            mScrollState = SCROLL_WAITING_IN_ZONE;
            mScrollRunnable.setDirection(SCROLL_LEFT);
            mHandler.postDelayed(mScrollRunnable, SCROLL_DELAY);
          }
        } else if (!inDeleteRegion
            && screenX > scrollView.getWidth() - SCROLL_ZONE
            && (screenY < Launcher.mScreenHeight - Workspace.workspaceBottom)) {
          if (mScrollState == SCROLL_OUTSIDE_ZONE) {
            mScrollState = SCROLL_WAITING_IN_ZONE;
            mScrollRunnable.setDirection(SCROLL_RIGHT);
            mHandler.postDelayed(mScrollRunnable, SCROLL_DELAY);
          }
        } else {
          if (mScrollState == SCROLL_WAITING_IN_ZONE) {
            mScrollState = SCROLL_OUTSIDE_ZONE;
            mScrollRunnable.setDirection(SCROLL_RIGHT);
            mHandler.removeCallbacks(mScrollRunnable);
          }
        }

        break;
      case MotionEvent.ACTION_UP:
        mHandler.removeCallbacks(mScrollRunnable);
        if (mDragging) {
          drop(screenX, screenY);
        }
        endDrag();

        break;
      case MotionEvent.ACTION_CANCEL:
        cancelDrag();
    }

    return true;
  }