Esempio n. 1
0
 private void clearScrollRunnable() {
   mHandler.removeCallbacks(mScrollRunnable);
   if (mScrollState == SCROLL_WAITING_IN_ZONE) {
     mScrollState = SCROLL_OUTSIDE_ZONE;
     mScrollRunnable.setDirection(SCROLL_RIGHT);
     mDragScroller.onExitScrollArea();
     mLauncher.getDragLayer().onExitScrollArea();
   }
 }
Esempio n. 2
0
  @SuppressLint("NewApi")
  private void checkScrollState(int x, int y) {
    final int slop = ViewConfiguration.get(mLauncher).getScaledWindowTouchSlop();
    final int delay = mDistanceSinceScroll < slop ? RESCROLL_DELAY : SCROLL_DELAY;
    final DragLayer dragLayer = mLauncher.getDragLayer();
    boolean isRtl = false;
    if (Build.VERSION.SDK_INT >= 17) { // Modified by cooee Hugo.ye for API
      // level
      isRtl = (dragLayer.getLayoutDirection() == View.LAYOUT_DIRECTION_RTL);
    }

    final int forwardDirection = isRtl ? SCROLL_RIGHT : SCROLL_LEFT;
    final int backwardsDirection = isRtl ? SCROLL_LEFT : SCROLL_RIGHT;

    if (x < mScrollZone) {
      if (mScrollState == SCROLL_OUTSIDE_ZONE) {
        mScrollState = SCROLL_WAITING_IN_ZONE;
        if (mDragScroller.onEnterScrollArea(x, y, forwardDirection)) {
          dragLayer.onEnterScrollArea(forwardDirection);
          mScrollRunnable.setDirection(forwardDirection);
          mHandler.postDelayed(mScrollRunnable, delay);
        }
      }
    } else if (x > mScrollView.getWidth() - mScrollZone) {
      if (mScrollState == SCROLL_OUTSIDE_ZONE) {
        mScrollState = SCROLL_WAITING_IN_ZONE;
        if (mDragScroller.onEnterScrollArea(x, y, backwardsDirection)) {
          dragLayer.onEnterScrollArea(backwardsDirection);
          mScrollRunnable.setDirection(backwardsDirection);
          mHandler.postDelayed(mScrollRunnable, delay);
        }
      }
    } else {
      clearScrollRunnable();
    }
  }
Esempio n. 3
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;
  }
  @Override
  public boolean onTouchEvent(MotionEvent ev) {
    if (!mDragging) {
      return false;
    }

    final int action = ev.getAction();
    final float x = ev.getX();
    final float y = ev.getY();

    switch (action) {
      case MotionEvent.ACTION_DOWN:

        // Remember where the motion event started
        mLastMotionX = x;
        mLastMotionY = y;

        if ((x < SCROLL_ZONE) || (x > getWidth() - SCROLL_ZONE)) {
          mScrollState = SCROLL_WAITING_IN_ZONE;
          postDelayed(mScrollRunnable, SCROLL_DELAY);
        } else {
          mScrollState = SCROLL_OUTSIDE_ZONE;
        }

        break;
      case MotionEvent.ACTION_MOVE:
        final int scrollX = mScrollX;
        final int scrollY = mScrollY;

        final float touchX = mTouchOffsetX;
        final float touchY = mTouchOffsetY;

        final int offsetX = mBitmapOffsetX;
        final int offsetY = mBitmapOffsetY;

        int left = (int) (scrollX + mLastMotionX - touchX - offsetX);
        int top = (int) (scrollY + mLastMotionY - touchY - offsetY);

        int width;
        int height;
        if (mDrawModeBitmap && mDragBitmap != null) {
          final Bitmap dragBitmap = mDragBitmap;
          width = dragBitmap.getWidth();
          height = dragBitmap.getHeight();
        } else {
          width = mDrawWidth;
          height = mDrawHeight;
        }
        final Rect rect = mRect;
        rect.set(left - 1, top - 1, left + width + 1, top + height + 1);

        mLastMotionX = x;
        mLastMotionY = y;

        left = (int) (scrollX + x - touchX - offsetX);
        top = (int) (scrollY + y - touchY - offsetY);

        // Invalidate current icon position
        rect.union(left - 1, top - 1, left + width + 1, top + height + 1);

        final int[] coordinates = mDropCoordinates;
        DropTarget dropTarget = findDropTarget((int) x, (int) y, coordinates);
        if (dropTarget != null) {
          if (mLastDropTarget == dropTarget) {
            dropTarget.onDragOver(
                mDragSource,
                coordinates[0],
                coordinates[1],
                (int) mTouchOffsetX,
                (int) mTouchOffsetY,
                mDragInfo);
          } else {
            if (mLastDropTarget != null) {
              mLastDropTarget.onDragExit(
                  mDragSource,
                  coordinates[0],
                  coordinates[1],
                  (int) mTouchOffsetX,
                  (int) mTouchOffsetY,
                  mDragInfo);
            }
            dropTarget.onDragEnter(
                mDragSource,
                coordinates[0],
                coordinates[1],
                (int) mTouchOffsetX,
                (int) mTouchOffsetY,
                mDragInfo);
          }
        } else {
          if (mLastDropTarget != null) {
            mLastDropTarget.onDragExit(
                mDragSource,
                coordinates[0],
                coordinates[1],
                (int) mTouchOffsetX,
                (int) mTouchOffsetY,
                mDragInfo);
          }
        }

        invalidate(rect);

        mLastDropTarget = dropTarget;
        if (mTagPopup != null) {
          if (Math.abs(mOriginalX - mLastMotionX) > SCROLL_ZONE
              || Math.abs(mOriginalY - mLastMotionY) > SCROLL_ZONE) {
            final QuickActionWindow qa = (QuickActionWindow) mTagPopup;
            qa.dismiss();
            mTagPopup = null;
          }
        }
        boolean inDragRegion = false;
        if (mDragRegion != null) {
          final RectF region = mDragRegion;
          final boolean inRegion = region.contains(ev.getRawX(), ev.getRawY());
          if (!mEnteredRegion && inRegion) {
            mDragPaint = mTrashPaint;
            mRectPaint.setColor(COLOR_TRASH);
            mEnteredRegion = true;
            inDragRegion = true;
          } else if (mEnteredRegion && !inRegion) {
            mDragPaint = null;
            mRectPaint.setColor(COLOR_NORMAL);
            mEnteredRegion = false;
          }
        }

        if (!inDragRegion && x < SCROLL_ZONE) {
          if (mScrollState == SCROLL_OUTSIDE_ZONE) {
            mScrollState = SCROLL_WAITING_IN_ZONE;
            mScrollRunnable.setDirection(SCROLL_LEFT);
            postDelayed(mScrollRunnable, SCROLL_DELAY);
          }
        } else if (!inDragRegion && x > getWidth() - SCROLL_ZONE) {
          if (mScrollState == SCROLL_OUTSIDE_ZONE) {
            mScrollState = SCROLL_WAITING_IN_ZONE;
            mScrollRunnable.setDirection(SCROLL_RIGHT);
            postDelayed(mScrollRunnable, SCROLL_DELAY);
          }
        } else {
          if (mScrollState == SCROLL_WAITING_IN_ZONE) {
            mScrollState = SCROLL_OUTSIDE_ZONE;
            mScrollRunnable.setDirection(SCROLL_RIGHT);
            removeCallbacks(mScrollRunnable);
          }
        }

        break;
      case MotionEvent.ACTION_UP:
        removeCallbacks(mScrollRunnable);
        if (mShouldDrop) {
          drop(x, y);
          mShouldDrop = false;
        }
        endDrag();

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

    return true;
  }