/**
   * Determine drag.
   *
   * @param ev the ev
   */
  private void determineDrag(MotionEvent ev) {
    final int activePointerId = mActivePointerId;
    final int pointerIndex = getPointerIndex(ev, activePointerId);
    if (activePointerId == INVALID_POINTER) {
      return;
    }

    final float x = MotionEventCompat.getX(ev, pointerIndex);
    final float dx = x - mLastMotionX;
    final float xDiff = Math.abs(dx);
    final float y = MotionEventCompat.getY(ev, pointerIndex);
    final float dy = y - mLastMotionY;
    final float yDiff = Math.abs(dy);
    if (xDiff > (isMenuOpen() ? mTouchSlop / 2 : mTouchSlop)
        && xDiff > yDiff
        && thisSlideAllowed(dx)) {
      startDrag();
      mLastMotionX = x;
      mLastMotionY = y;
      setScrollingCacheEnabled(true);
      // TODO add back in touch slop check
    } else if (xDiff > mTouchSlop) {
      mIsUnableToDrag = true;
    }
  }
 private void onPointerUp(MotionEvent e) {
   final int actionIndex = MotionEventCompat.getActionIndex(e);
   if (MotionEventCompat.getPointerId(e, actionIndex) == mScrollPointerId) {
     // Pick a new pointer to pick up the slack.
     final int newIndex = actionIndex == 0 ? 1 : 0;
     mScrollPointerId = MotionEventCompat.getPointerId(e, newIndex);
     mInitialTouchX = mLastTouchX = (int) (MotionEventCompat.getX(e, newIndex) + 0.5f);
     mInitialTouchY = mLastTouchY = (int) (MotionEventCompat.getY(e, newIndex) + 0.5f);
   }
 }
  @Override
  public boolean onInterceptTouchEvent(MotionEvent ev) {

    if (!mEnabled) {
      return false;
    }

    final int action = ev.getAction() & MotionEventCompat.ACTION_MASK;

    if (action == MotionEvent.ACTION_CANCEL
        || action == MotionEvent.ACTION_UP
        || (action != MotionEvent.ACTION_DOWN && mIsUnableToDrag)) {
      endDrag();
      return false;
    }

    switch (action) {
      case MotionEvent.ACTION_MOVE:
        determineDrag(ev);
        break;
      case MotionEvent.ACTION_DOWN:
        int index = MotionEventCompat.getActionIndex(ev);
        mActivePointerId = MotionEventCompat.getPointerId(ev, index);
        if (mActivePointerId == INVALID_POINTER) {
          break;
        }

        mLastMotionX = mInitialMotionX = MotionEventCompat.getX(ev, index);
        mLastMotionY = MotionEventCompat.getY(ev, index);
        if (thisTouchAllowed(ev)) {
          mIsBeingDragged = false;
          mIsUnableToDrag = false;
          if (isMenuOpen()
              && mViewBehind.menuTouchInQuickReturn(mContent, mCurItem, ev.getX() + mScrollX)) {
            mQuickReturn = true;
          }
        } else {
          mIsUnableToDrag = true;
        }
        break;
      case MotionEventCompat.ACTION_POINTER_UP:
        onSecondaryPointerUp(ev);
        break;
    }

    if (!mIsBeingDragged) {
      if (mVelocityTracker == null) {
        mVelocityTracker = VelocityTracker.obtain();
      }
      mVelocityTracker.addMovement(ev);
    }
    return mIsBeingDragged || mQuickReturn;
  }
 /**
  * On secondary pointer up.
  *
  * @param ev the ev
  */
 private void onSecondaryPointerUp(MotionEvent ev) {
   final int pointerIndex = MotionEventCompat.getActionIndex(ev);
   final int pointerId = MotionEventCompat.getPointerId(ev, pointerIndex);
   if (pointerId == mActivePointerId) {
     // This was our active pointer going up. Choose a new
     // active pointer and adjust accordingly.
     final int newPointerIndex = pointerIndex == 0 ? 1 : 0;
     mLastMotionX = MotionEventCompat.getX(ev, newPointerIndex);
     mActivePointerId = MotionEventCompat.getPointerId(ev, newPointerIndex);
     if (mVelocityTracker != null) {
       mVelocityTracker.clear();
     }
   }
 }
  @Override
  public boolean onTouchEvent(MotionEvent ev) {

    if (!mEnabled) return false;

    if (!mIsBeingDragged && !thisTouchAllowed(ev)) return false;

    //		if (!mIsBeingDragged && !mQuickReturn)
    //			return false;

    final int action = ev.getAction();

    if (mVelocityTracker == null) {
      mVelocityTracker = VelocityTracker.obtain();
    }
    mVelocityTracker.addMovement(ev);

    switch (action & MotionEventCompat.ACTION_MASK) {
      case MotionEvent.ACTION_DOWN:
        /*
         * If being flinged and user touches, stop the fling. isFinished
         * will be false if being flinged.
         */
        completeScroll();

        // Remember where the motion event started
        int index = MotionEventCompat.getActionIndex(ev);
        mActivePointerId = MotionEventCompat.getPointerId(ev, index);
        mLastMotionX = mInitialMotionX = ev.getX();
        break;
      case MotionEvent.ACTION_MOVE:
        if (!mIsBeingDragged) {
          determineDrag(ev);
          if (mIsUnableToDrag) return false;
        }
        if (mIsBeingDragged) {
          // Scroll to follow the motion event
          final int activePointerIndex = getPointerIndex(ev, mActivePointerId);
          if (mActivePointerId == INVALID_POINTER) break;
          final float x = MotionEventCompat.getX(ev, activePointerIndex);
          final float deltaX = mLastMotionX - x;
          mLastMotionX = x;
          float oldScrollX = getScrollX();
          float scrollX = oldScrollX + deltaX;
          final float leftBound = getLeftBound();
          final float rightBound = getRightBound();
          if (scrollX < leftBound) {
            scrollX = leftBound;
          } else if (scrollX > rightBound) {
            scrollX = rightBound;
          }
          // Don't lose the rounded component
          mLastMotionX += scrollX - (int) scrollX;
          scrollTo((int) scrollX, getScrollY());
          pageScrolled((int) scrollX);
        }
        break;
      case MotionEvent.ACTION_UP:
        if (mIsBeingDragged) {
          final VelocityTracker velocityTracker = mVelocityTracker;
          velocityTracker.computeCurrentVelocity(1000, mMaximumVelocity);
          int initialVelocity =
              (int) VelocityTrackerCompat.getXVelocity(velocityTracker, mActivePointerId);
          final int scrollX = getScrollX();
          final float pageOffset = (float) (scrollX - getDestScrollX(mCurItem)) / getBehindWidth();
          final int activePointerIndex = getPointerIndex(ev, mActivePointerId);
          if (mActivePointerId != INVALID_POINTER) {
            final float x = MotionEventCompat.getX(ev, activePointerIndex);
            final int totalDelta = (int) (x - mInitialMotionX);
            int nextPage = determineTargetPage(pageOffset, initialVelocity, totalDelta);
            setCurrentItemInternal(nextPage, true, true, initialVelocity);
          } else {
            setCurrentItemInternal(mCurItem, true, true, initialVelocity);
          }
          mActivePointerId = INVALID_POINTER;
          endDrag();
        } else if (mQuickReturn
            && mViewBehind.menuTouchInQuickReturn(mContent, mCurItem, ev.getX() + mScrollX)) {
          // close the menu
          setCurrentItem(1);
          endDrag();
        }
        break;
      case MotionEvent.ACTION_CANCEL:
        if (mIsBeingDragged) {
          setCurrentItemInternal(mCurItem, true, true);
          mActivePointerId = INVALID_POINTER;
          endDrag();
        }
        break;
      case MotionEventCompat.ACTION_POINTER_DOWN:
        {
          final int indexx = MotionEventCompat.getActionIndex(ev);
          mLastMotionX = MotionEventCompat.getX(ev, indexx);
          mActivePointerId = MotionEventCompat.getPointerId(ev, indexx);
          break;
        }
      case MotionEventCompat.ACTION_POINTER_UP:
        onSecondaryPointerUp(ev);
        int pointerIndex = getPointerIndex(ev, mActivePointerId);
        if (mActivePointerId == INVALID_POINTER) break;
        mLastMotionX = MotionEventCompat.getX(ev, pointerIndex);
        break;
    }
    return true;
  }
    @Override
    public boolean onTouchEvent(MotionEvent ev) {

      // Check if pause button is hit
      if (ev.getAction() == MotionEvent.ACTION_DOWN) {
        float touchX = ev.getX();
        float touchY = ev.getY();

        if (pauseBt.getRect().contains((int) touchX, (int) touchY)) {
          if (ev.getAction() == MotionEvent.ACTION_DOWN) {
            // LeaveMemoryGame();
            Intent itn;
            itn = new Intent(getApplicationContext(), PauseMenuActivity.class);
            startActivity(itn);
          }
        }

        final int pointerIndex = MotionEventCompat.getActionIndex(ev);
        final float x = MotionEventCompat.getX(ev, pointerIndex);
        final float y = MotionEventCompat.getY(ev, pointerIndex);

        PlayerTouchX = (int) x;
        PlayerTouchY = (int) y;

        if (!showCardsState) {
          if (currentSelectedRectIndex1 == -1
              && currentSelectedRectIndex2 == -1
              && (currentSelectedRectIndex1 = findSelectedRect())
                  != -1) // nothing is selected: read first
          {
            Log.w(
                "Warn",
                "Selected 111: "
                    + currentSelectedRectIndex1
                    + " 2 is: "
                    + currentSelectedRectIndex2);
          } else if (currentSelectedRectIndex1 != -1
              && currentSelectedRectIndex2 == -1
              && (currentSelectedRectIndex2 = findSelectedRect()) != -1) // selected 1: read second
          {
            // Log.w("Warn", "Selected 222: " + currentSelectedRectIndex2 + "  1 is " +
            // currentSelectedRectIndex1);

            // search for pairs
            // if (KeysArray[currentSelectedRectIndex1] == currentSelectedRectIndex2 &&
            // KeysArray[currentSelectedRectIndex2] == currentSelectedRectIndex1) {
            if (cardList.get(currentSelectedRectIndex1).code
                == cardList.get(currentSelectedRectIndex2).code) {

              // Log.w("Warn", "Pair found");
              SoundHandler.PlaySound(SoundHandler.correct_sound_id3);
              pairs++;
              if (pairs == (M * N) / 2) LeaveMemoryGame();

              cardList.get(currentSelectedRectIndex1).turned = true; // reveal 'cards'
              cardList.get(currentSelectedRectIndex2).turned = true;

              currentSelectedRectIndex1 = -1;
              currentSelectedRectIndex2 = -1;
            } else // if no pairs found activate 'no select timer'
            {
              canSelectState = false;
              noSelectTimer = System.currentTimeMillis(); // start counting time..
            }
          } else if (currentSelectedRectIndex1 != -1 && currentSelectedRectIndex2 != -1) {
            Log.w("Warn", "Cant Select, waiting timer..");
          }
        }
      }
      return true;
    }
    public boolean onTouchEvent(MotionEvent motionevent)
    {
        float f;
        float f1;
        int i2;
        int j2 = motionevent.getAction();
        if (mVelocityTracker == null)
        {
            mVelocityTracker = VelocityTracker.obtain();
        }
        mVelocityTracker.addMovement(motionevent);
        boolean flag;
        int l;
        int k1;
        if ((j2 & 0xff) == 6)
        {
            flag = true;
        } else
        {
            flag = false;
        }
        if (flag)
        {
            l = MotionEventCompat.getActionIndex(motionevent);
        } else
        {
            l = -1;
        }
        i2 = MotionEventCompat.getPointerCount(motionevent);
        k1 = 0;
        f = 0.0F;
        float f2;
        for (f1 = 0.0F; k1 < i2; f1 = f2)
        {
            float f5 = f;
            f2 = f1;
            if (l != k1)
            {
                f2 = f1 + MotionEventCompat.getX(motionevent, k1);
                f5 = f + MotionEventCompat.getY(motionevent, k1);
            }
            k1++;
            f = f5;
        }

        int i;
        if (flag)
        {
            i = i2 - 1;
        } else
        {
            i = i2;
        }
        f1 /= i;
        f /= i;
        j2 & 0xff;
        JVM INSTR tableswitch 0 6: default 204
    //                   0 383
    //                   1 862
    //                   2 643
    //                   3 1136
    //                   4 204
    //                   5 213
    //                   6 239;
           goto _L1 _L2 _L3 _L4 _L5 _L1 _L6 _L7
_L1:
        return false;
_L6:
        mLastFocusX = f1;
        mDownFocusX = f1;
        mLastFocusY = f;
        mDownFocusY = f;
        cancelTaps();
        return false;
_L7:
        mLastFocusX = f1;
        mDownFocusX = f1;
        mLastFocusY = f;
        mDownFocusY = f;
        mVelocityTracker.computeCurrentVelocity(1000, mMaximumFlingVelocity);
        int i1 = MotionEventCompat.getActionIndex(motionevent);
        int j = MotionEventCompat.getPointerId(motionevent, i1);
        f = VelocityTrackerCompat.getXVelocity(mVelocityTracker, j);
        f1 = VelocityTrackerCompat.getYVelocity(mVelocityTracker, j);
        j = 0;
        while (j < i2) 
        {
            if (j != i1)
            {
                int l1 = MotionEventCompat.getPointerId(motionevent, j);
                float f3 = VelocityTrackerCompat.getXVelocity(mVelocityTracker, l1);
                if (VelocityTrackerCompat.getYVelocity(mVelocityTracker, l1) * f1 + f3 * f < 0.0F)
                {
                    mVelocityTracker.clear();
                    return false;
                }
            }
            j++;
        }
          goto _L8
Exemple #8
0
  @Override
  public boolean onTouchEvent(MotionEvent ev) {
    // return or not?
    mScaleDetector.onTouchEvent(ev);
    mGestureDetector.onTouchEvent(ev);

    final int action = MotionEventCompat.getActionMasked(ev);
    switch (action) {
      case MotionEvent.ACTION_DOWN:
        {
          final int pointerIndex = MotionEventCompat.getActionIndex(ev);
          final float x = MotionEventCompat.getX(ev, pointerIndex);
          final float y = MotionEventCompat.getY(ev, pointerIndex);

          int index = getActiveIndexByXY(x, y);
          if (index >= 0) {
            mActiveIndex = index;
            mLastTouchX = x;
            mLastTouchY = y;
            mActivePointerId = MotionEventCompat.getPointerId(ev, 0);
          }
          break;
        }

      case MotionEvent.ACTION_MOVE:
        if (mActivePointerId != INVALID_POINTER_ID) {
          final int pointerIndex = MotionEventCompat.findPointerIndex(ev, mActivePointerId);
          final float x = MotionEventCompat.getX(ev, pointerIndex);
          final float y = MotionEventCompat.getY(ev, pointerIndex);
          final float dx = x - mLastTouchX;
          final float dy = y - mLastTouchY;

          Figure figure = mFigures.get(mActiveIndex);
          figure.x += dx;
          figure.y += dy;

          mLastTouchX = x;
          mLastTouchY = y;

          invalidate();
        }
        break;

      case MotionEvent.ACTION_UP:
        {
          if (mActiveIndex >= 0) {
            if (mUpdateFigureListener != null) {
              mUpdateFigureListener.onUpdate(mFigures.get(mActiveIndex));
            }
            mActiveIndex = -1;
          }
          mActivePointerId = INVALID_POINTER_ID;
          break;
        }

      case MotionEvent.ACTION_CANCEL:
        {
          mActivePointerId = INVALID_POINTER_ID;
          mActiveIndex = -1;
          break;
        }

      case MotionEvent.ACTION_POINTER_UP:
        final int pointerIndex = MotionEventCompat.getActionIndex(ev);
        final int pointerId = MotionEventCompat.getPointerId(ev, pointerIndex);

        if (pointerId == mActivePointerId) {
          final int newPointerIndex = pointerIndex == 0 ? 1 : 0;
          mLastTouchX = MotionEventCompat.getX(ev, newPointerIndex);
          mLastTouchY = MotionEventCompat.getY(ev, newPointerIndex);
          mActivePointerId = MotionEventCompat.getPointerId(ev, newPointerIndex);
        }
        break;
    }
    return true;
  }
  @Override
  public boolean onTouchEvent(MotionEvent ev) {

    if (!mEnabled) return false;

    //		if (!mIsBeingDragged && !thisTouchAllowed(ev))
    //			return false;

    if (!mIsBeingDragged && !mQuickReturn) return false;

    final int action = ev.getAction();

    if (mVelocityTracker == null) {
      mVelocityTracker = VelocityTracker.obtain();
    }
    mVelocityTracker.addMovement(ev);

    switch (action & MotionEventCompat.ACTION_MASK) {
      case MotionEvent.ACTION_DOWN:
        /*
         * If being flinged and user touches, stop the fling. isFinished
         * will be false if being flinged.
         */
        completeScroll();

        // Remember where the motion event started
        mLastMotionX = mInitialMotionX = ev.getX();
        mActivePointerId = MotionEventCompat.getPointerId(ev, 0);
        break;
      case MotionEvent.ACTION_MOVE:
        if (!mIsBeingDragged) {
          if (mActivePointerId == INVALID_POINTER) break;
          final int pointerIndex = getPointerIndex(ev, mActivePointerId);
          final float x = MotionEventCompat.getX(ev, pointerIndex);
          final float dx = x - mLastMotionX;
          final float xDiff = Math.abs(dx);
          final float y = MotionEventCompat.getY(ev, pointerIndex);
          final float yDiff = Math.abs(y - mLastMotionY);
          if (DEBUG)
            Log.v(
                TAG,
                "onTouch moved to:("
                    + x
                    + ", "
                    + y
                    + "), diff:("
                    + xDiff
                    + ", "
                    + yDiff
                    + ")\nmIsBeingDragged:"
                    + mIsBeingDragged
                    + ", mLastMotionX:"
                    + mLastMotionX);
          if ((xDiff > mTouchSlop || (mQuickReturn && xDiff > mTouchSlop / 4))
              && xDiff > yDiff
              && thisSlideAllowed(dx)) {
            if (DEBUG) Log.v(TAG, "Starting drag! from onTouch");
            startDrag();
            mLastMotionX = x;
            setScrollingCacheEnabled(true);
          } else {
            if (DEBUG) Log.v(TAG, "onTouch returning false");
            return false;
          }
        }
        if (mIsBeingDragged) {
          // Scroll to follow the motion event
          final int activePointerIndex = getPointerIndex(ev, mActivePointerId);
          if (mActivePointerId == INVALID_POINTER) {
            break;
          }
          final float x = MotionEventCompat.getX(ev, activePointerIndex);
          final float deltaX = mLastMotionX - x;
          mLastMotionX = x;
          float oldScrollX = getScrollX();
          float scrollX = oldScrollX + deltaX;
          final float leftBound = getLeftBound();
          final float rightBound = getRightBound();
          if (scrollX < leftBound) {
            scrollX = leftBound;
          } else if (scrollX > rightBound) {
            scrollX = rightBound;
          }
          // Don't lose the rounded component
          mLastMotionX += scrollX - (int) scrollX;
          scrollTo((int) scrollX, getScrollY());
          pageScrolled((int) scrollX);
        }
        break;
      case MotionEvent.ACTION_UP:
        if (mIsBeingDragged) {
          final VelocityTracker velocityTracker = mVelocityTracker;
          velocityTracker.computeCurrentVelocity(1000, mMaximumVelocity);
          int initialVelocity =
              (int) VelocityTrackerCompat.getXVelocity(velocityTracker, mActivePointerId);
          final int scrollX = getScrollX();
          //				final int widthWithMargin = getWidth();
          //				final float pageOffset = (float) (scrollX % widthWithMargin) / widthWithMargin;
          // TODO test this. should get better flinging behavior
          final float pageOffset = (float) (scrollX - getDestScrollX(mCurItem)) / getBehindWidth();
          final int activePointerIndex = getPointerIndex(ev, mActivePointerId);
          if (mActivePointerId != INVALID_POINTER) {
            final float x = MotionEventCompat.getX(ev, activePointerIndex);
            final int totalDelta = (int) (x - mInitialMotionX);
            int nextPage = determineTargetPage(pageOffset, initialVelocity, totalDelta);
            setCurrentItemInternal(nextPage, true, true, initialVelocity);
          } else {
            setCurrentItemInternal(mCurItem, true, true, initialVelocity);
          }
          mActivePointerId = INVALID_POINTER;
          endDrag();
        } else if (mQuickReturn
            && mViewBehind.menuTouchInQuickReturn(mContent, mCurItem, ev.getX() + mScrollX)) {
          // close the menu
          setCurrentItem(1);
          endDrag();
        }
        break;
      case MotionEvent.ACTION_CANCEL:
        if (mIsBeingDragged) {
          setCurrentItemInternal(mCurItem, true, true);
          mActivePointerId = INVALID_POINTER;
          endDrag();
        }
        break;
      case MotionEventCompat.ACTION_POINTER_DOWN:
        {
          final int index = MotionEventCompat.getActionIndex(ev);
          final float x = MotionEventCompat.getX(ev, index);
          mLastMotionX = x;
          mActivePointerId = MotionEventCompat.getPointerId(ev, index);
          break;
        }
      case MotionEventCompat.ACTION_POINTER_UP:
        onSecondaryPointerUp(ev);
        int pointerIndex = this.getPointerIndex(ev, mActivePointerId);
        if (mActivePointerId == INVALID_POINTER) break;
        mLastMotionX = MotionEventCompat.getX(ev, pointerIndex);
        break;
    }
    return true;
  }
  @Override
  public boolean onTouchEvent(android.view.MotionEvent ev) {
    if (super.onTouchEvent(ev)) return true;
    if ((mViewPager == null) || (mViewPager.getAdapter().getCount() == 0)) return false;

    final int action = ev.getAction() & MotionEventCompat.ACTION_MASK;
    switch (action) {
      case MotionEvent.ACTION_DOWN:
        mActivePointerId = MotionEventCompat.getPointerId(ev, 0);
        mLastMotionX = ev.getX();
        break;

      case MotionEvent.ACTION_MOVE:
        {
          final int activePointerIndex = MotionEventCompat.findPointerIndex(ev, mActivePointerId);
          final float x = MotionEventCompat.getX(ev, activePointerIndex);
          final float deltaX = x - mLastMotionX;

          if (!mIsDragging) {
            if (Math.abs(deltaX) > mTouchSlop) {
              mIsDragging = true;
            }
          }

          if (mIsDragging) {
            mLastMotionX = x;
            if (mViewPager.isFakeDragging() || mViewPager.beginFakeDrag()) {
              mViewPager.fakeDragBy(deltaX);
            }
          }

          break;
        }

      case MotionEvent.ACTION_CANCEL:
      case MotionEvent.ACTION_UP:
        if (!mIsDragging) {
          final int count = mViewPager.getAdapter().getCount();
          final int width = getWidth();
          final float halfWidth = width / 2f;
          final float sixthWidth = width / 6f;
          final float leftThird = halfWidth - sixthWidth;
          final float rightThird = halfWidth + sixthWidth;
          final float eventX = ev.getX();

          if (eventX < leftThird) {
            if (mCurrentPage > 0) {
              if (action != MotionEvent.ACTION_CANCEL) {
                mViewPager.setCurrentItem(mCurrentPage - 1);
              }
              return true;
            }
          } else if (eventX > rightThird) {
            if (mCurrentPage < count - 1) {
              if (action != MotionEvent.ACTION_CANCEL) {
                mViewPager.setCurrentItem(mCurrentPage + 1);
              }
              return true;
            }
          } else {
            // Middle third
            if (mCenterItemClickListener != null && action != MotionEvent.ACTION_CANCEL) {
              mCenterItemClickListener.onCenterItemClick(mCurrentPage);
            }
          }
        }

        mIsDragging = false;
        mActivePointerId = INVALID_POINTER;
        if (mViewPager.isFakeDragging()) {
          mViewPager.endFakeDrag();
        }
        break;

      case MotionEventCompat.ACTION_POINTER_DOWN:
        {
          final int index = MotionEventCompat.getActionIndex(ev);
          mLastMotionX = MotionEventCompat.getX(ev, index);
          mActivePointerId = MotionEventCompat.getPointerId(ev, index);
          break;
        }

      case MotionEventCompat.ACTION_POINTER_UP:
        final int pointerIndex = MotionEventCompat.getActionIndex(ev);
        final int pointerId = MotionEventCompat.getPointerId(ev, pointerIndex);
        if (pointerId == mActivePointerId) {
          final int newPointerIndex = pointerIndex == 0 ? 1 : 0;
          mActivePointerId = MotionEventCompat.getPointerId(ev, newPointerIndex);
        }
        mLastMotionX =
            MotionEventCompat.getX(ev, MotionEventCompat.findPointerIndex(ev, mActivePointerId));
        break;
    }

    return true;
  }
Exemple #11
0
  @Override
  public boolean onTouchEvent(MotionEvent ev) {

    if (!mIsFlippingEnabled) {
      return false;
    }

    if (mPageCount < 1) {
      return false;
    }

    if (!mIsFlipping && !mLastTouchAllowed) {
      return false;
    }

    final int action = ev.getAction();

    if (action == MotionEvent.ACTION_UP
        || action == MotionEvent.ACTION_CANCEL
        || action == MotionEvent.ACTION_OUTSIDE) {
      mLastTouchAllowed = false;
    } else {
      mLastTouchAllowed = true;
    }

    trackVelocity(ev);

    switch (action & MotionEvent.ACTION_MASK) {
      case MotionEvent.ACTION_DOWN:

        // start flipping immediately if interrupting some sort of animation
        if (endScroll() || endPeak()) {
          mIsFlipping = true;
        }

        // Remember where the motion event started
        mLastX = ev.getX();
        mLastY = ev.getY();
        mActivePointerId = MotionEventCompat.getPointerId(ev, 0);
        break;
      case MotionEvent.ACTION_MOVE:
        if (!mIsFlipping) {
          final int pointerIndex = MotionEventCompat.findPointerIndex(ev, mActivePointerId);
          if (pointerIndex == -1) {
            mActivePointerId = INVALID_POINTER;
            break;
          }
          final float x = MotionEventCompat.getX(ev, pointerIndex);
          final float xDiff = Math.abs(x - mLastX);
          final float y = MotionEventCompat.getY(ev, pointerIndex);
          final float yDiff = Math.abs(y - mLastY);
          if ((mIsFlippingVertically && yDiff > mTouchSlop && yDiff > xDiff)
              || (!mIsFlippingVertically && xDiff > mTouchSlop && xDiff > yDiff)) {
            mIsFlipping = true;
            mLastX = x;
            mLastY = y;
          }
        }
        if (mIsFlipping) {
          // Scroll to follow the motion event
          final int activePointerIndex = MotionEventCompat.findPointerIndex(ev, mActivePointerId);
          if (activePointerIndex == -1) {
            mActivePointerId = INVALID_POINTER;
            break;
          }
          final float x = MotionEventCompat.getX(ev, activePointerIndex);
          final float deltaX = mLastX - x;
          final float y = MotionEventCompat.getY(ev, activePointerIndex);
          final float deltaY = mLastY - y;
          mLastX = x;
          mLastY = y;

          float deltaFlipDistance = 0;
          if (mIsFlippingVertically) {
            deltaFlipDistance = deltaY;
          } else {
            deltaFlipDistance = deltaX;
          }

          deltaFlipDistance /=
              ((isFlippingVertically() ? getHeight() : getWidth()) / FLIP_DISTANCE_PER_PAGE);
          setFlipDistance(mFlipDistance + deltaFlipDistance);

          final int minFlipDistance = 0;
          final int maxFlipDistance = (mPageCount - 1) * FLIP_DISTANCE_PER_PAGE;
          final boolean isOverFlipping =
              mFlipDistance < minFlipDistance || mFlipDistance > maxFlipDistance;
          if (isOverFlipping) {
            mIsOverFlipping = true;
            setFlipDistance(
                mOverFlipper.calculate(mFlipDistance, minFlipDistance, maxFlipDistance));
            if (mOnOverFlipListener != null) {
              float overFlip = mOverFlipper.getTotalOverFlip();
              mOnOverFlipListener.onOverFlip(
                  this, mOverFlipMode, overFlip < 0, Math.abs(overFlip), FLIP_DISTANCE_PER_PAGE);
            }
          } else if (mIsOverFlipping) {
            mIsOverFlipping = false;
            if (mOnOverFlipListener != null) {
              // TODO in the future should only notify flip distance 0
              // on the correct edge (previous/next)
              mOnOverFlipListener.onOverFlip(this, mOverFlipMode, false, 0, FLIP_DISTANCE_PER_PAGE);
              mOnOverFlipListener.onOverFlip(this, mOverFlipMode, true, 0, FLIP_DISTANCE_PER_PAGE);
            }
          }
        }
        break;
      case MotionEvent.ACTION_UP:
      case MotionEvent.ACTION_CANCEL:
        if (mIsFlipping) {
          final VelocityTracker velocityTracker = mVelocityTracker;
          velocityTracker.computeCurrentVelocity(1000, mMaximumVelocity);

          int velocity = 0;
          if (isFlippingVertically()) {
            velocity = (int) VelocityTrackerCompat.getYVelocity(velocityTracker, mActivePointerId);
          } else {
            velocity = (int) VelocityTrackerCompat.getXVelocity(velocityTracker, mActivePointerId);
          }
          smoothFlipTo(getNextPage(velocity));

          mActivePointerId = INVALID_POINTER;
          endFlip();

          mOverFlipper.overFlipEnded();
        }
        break;
      case MotionEventCompat.ACTION_POINTER_DOWN:
        {
          final int index = MotionEventCompat.getActionIndex(ev);
          final float x = MotionEventCompat.getX(ev, index);
          final float y = MotionEventCompat.getY(ev, index);
          mLastX = x;
          mLastY = y;
          mActivePointerId = MotionEventCompat.getPointerId(ev, index);
          break;
        }
      case MotionEventCompat.ACTION_POINTER_UP:
        onSecondaryPointerUp(ev);
        final int index = MotionEventCompat.findPointerIndex(ev, mActivePointerId);
        final float x = MotionEventCompat.getX(ev, index);
        final float y = MotionEventCompat.getY(ev, index);
        mLastX = x;
        mLastY = y;
        break;
    }
    if (mActivePointerId == INVALID_POINTER) {
      mLastTouchAllowed = false;
    }
    return true;
  }
Exemple #12
0
  @Override
  public boolean onInterceptTouchEvent(MotionEvent ev) {

    if (!mIsFlippingEnabled) {
      return false;
    }

    if (mPageCount < 1) {
      return false;
    }

    final int action = ev.getAction() & MotionEvent.ACTION_MASK;

    if (action == MotionEvent.ACTION_CANCEL || action == MotionEvent.ACTION_UP) {
      mIsFlipping = false;
      mIsUnableToFlip = false;
      mActivePointerId = INVALID_POINTER;
      if (mVelocityTracker != null) {
        mVelocityTracker.recycle();
        mVelocityTracker = null;
      }
      return false;
    }

    if (action != MotionEvent.ACTION_DOWN) {
      if (mIsFlipping) {
        return true;
      } else if (mIsUnableToFlip) {
        return false;
      }
    }

    switch (action) {
      case MotionEvent.ACTION_MOVE:
        final int activePointerId = mActivePointerId;
        if (activePointerId == INVALID_POINTER) {
          break;
        }

        final int pointerIndex = MotionEventCompat.findPointerIndex(ev, activePointerId);
        if (pointerIndex == -1) {
          mActivePointerId = INVALID_POINTER;
          break;
        }

        final float x = MotionEventCompat.getX(ev, pointerIndex);
        final float dx = x - mLastX;
        final float xDiff = Math.abs(dx);
        final float y = MotionEventCompat.getY(ev, pointerIndex);
        final float dy = y - mLastY;
        final float yDiff = Math.abs(dy);

        if ((mIsFlippingVertically && yDiff > mTouchSlop && yDiff > xDiff)
            || (!mIsFlippingVertically && xDiff > mTouchSlop && xDiff > yDiff)) {
          mIsFlipping = true;
          mLastX = x;
          mLastY = y;
        } else if ((mIsFlippingVertically && xDiff > mTouchSlop)
            || (!mIsFlippingVertically && yDiff > mTouchSlop)) {
          mIsUnableToFlip = true;
        }
        break;

      case MotionEvent.ACTION_DOWN:
        mActivePointerId = ev.getAction() & MotionEvent.ACTION_POINTER_INDEX_MASK;
        mLastX = MotionEventCompat.getX(ev, mActivePointerId);
        mLastY = MotionEventCompat.getY(ev, mActivePointerId);

        mIsFlipping = !mScroller.isFinished() | mPeakAnim != null;
        mIsUnableToFlip = false;
        mLastTouchAllowed = true;

        break;
      case MotionEventCompat.ACTION_POINTER_UP:
        onSecondaryPointerUp(ev);
        break;
    }

    if (!mIsFlipping) {
      trackVelocity(ev);
    }

    return mIsFlipping;
  }
 public boolean onTouchEvent(MotionEvent paramMotionEvent)
 {
   if (super.onTouchEvent(paramMotionEvent)) {}
   label322:
   do
   {
     int i1;
     do
     {
       int i5;
       float f1;
       float f2;
       do
       {
         float f4;
         do
         {
           float f3;
           do
           {
             return true;
             if ((this.e == null) || (this.e.getAdapter().getCount() == 0)) {
               return false;
             }
             i1 = 0xFF & paramMotionEvent.getAction();
             switch (i1)
             {
             case 4: 
             default: 
               return true;
             case 0: 
               this.p = MotionEventCompat.getPointerId(paramMotionEvent, 0);
               this.o = paramMotionEvent.getX();
               return true;
             case 2: 
               f3 = MotionEventCompat.getX(paramMotionEvent, MotionEventCompat.findPointerIndex(paramMotionEvent, this.p));
               f4 = f3 - this.o;
               if ((!this.q) && (Math.abs(f4) > this.n)) {
                 this.q = true;
               }
               break;
             }
           } while (!this.q);
           this.o = f3;
         } while ((!this.e.isFakeDragging()) && (!this.e.beginFakeDrag()));
         this.e.fakeDragBy(f4);
         return true;
         if (this.q) {
           break label322;
         }
         i5 = this.e.getAdapter().getCount();
         int i6 = getWidth();
         f1 = i6 / 2.0F;
         f2 = i6 / 6.0F;
         if ((this.g <= 0) || (paramMotionEvent.getX() >= f1 - f2)) {
           break;
         }
       } while (i1 == 3);
       this.e.setCurrentItem(-1 + this.g);
       return true;
       if ((this.g >= i5 - 1) || (paramMotionEvent.getX() <= f2 + f1)) {
         break;
       }
     } while (i1 == 3);
     this.e.setCurrentItem(1 + this.g);
     return true;
     this.q = false;
     this.p = -1;
   } while (!this.e.isFakeDragging());
   this.e.endFakeDrag();
   return true;
   int i4 = MotionEventCompat.getActionIndex(paramMotionEvent);
   this.o = MotionEventCompat.getX(paramMotionEvent, i4);
   this.p = MotionEventCompat.getPointerId(paramMotionEvent, i4);
   return true;
   int i2 = MotionEventCompat.getActionIndex(paramMotionEvent);
   if (MotionEventCompat.getPointerId(paramMotionEvent, i2) == this.p)
   {
     int i3 = 0;
     if (i2 == 0) {
       i3 = 1;
     }
     this.p = MotionEventCompat.getPointerId(paramMotionEvent, i3);
   }
   this.o = MotionEventCompat.getX(paramMotionEvent, MotionEventCompat.findPointerIndex(paramMotionEvent, this.p));
   return true;
 }
  @Override
  public boolean onTouchEvent(MotionEvent e) {
    /*
    if (mLayoutFrozen || mIgnoreMotionEventTillDown) {
        return false;
    }
    if (dispatchOnItemTouch(e)) {
        cancelTouch();
        return true;
    }

    if (mLayout == null) {
        return false;
    }
    */

    final boolean canScrollHorizontally = false; // mLayout.canScrollHorizontally();
    final boolean canScrollVertically = true; // mLayout.canScrollVertically();

    if (mVelocityTracker == null) {
      mVelocityTracker = VelocityTracker.obtain();
    }
    boolean eventAddedToVelocityTracker = false;

    final MotionEvent vtev = MotionEvent.obtain(e);
    final int action = MotionEventCompat.getActionMasked(e);
    final int actionIndex = MotionEventCompat.getActionIndex(e);

    if (action == MotionEvent.ACTION_DOWN) {
      mNestedOffsets[0] = mNestedOffsets[1] = 0;
    }
    vtev.offsetLocation(mNestedOffsets[0], mNestedOffsets[1]);

    switch (action) {
      case MotionEvent.ACTION_DOWN:
        {
          mScrollPointerId = MotionEventCompat.getPointerId(e, 0);
          mInitialTouchX = mLastTouchX = (int) (e.getX() + 0.5f);
          mInitialTouchY = mLastTouchY = (int) (e.getY() + 0.5f);

          int nestedScrollAxis = ViewCompat.SCROLL_AXIS_NONE;
          if (canScrollHorizontally) {
            nestedScrollAxis |= ViewCompat.SCROLL_AXIS_HORIZONTAL;
          }
          if (canScrollVertically) {
            nestedScrollAxis |= ViewCompat.SCROLL_AXIS_VERTICAL;
          }
          startNestedScroll(nestedScrollAxis);
        }
        break;

      case MotionEventCompat.ACTION_POINTER_DOWN:
        {
          mScrollPointerId = MotionEventCompat.getPointerId(e, actionIndex);
          mInitialTouchX = mLastTouchX = (int) (MotionEventCompat.getX(e, actionIndex) + 0.5f);
          mInitialTouchY = mLastTouchY = (int) (MotionEventCompat.getY(e, actionIndex) + 0.5f);
        }
        break;

      case MotionEvent.ACTION_MOVE:
        {
          final int index = MotionEventCompat.findPointerIndex(e, mScrollPointerId);
          if (index < 0) {
            Log.e(
                TAG,
                "Error processing scroll; pointer index for id "
                    + mScrollPointerId
                    + " not found. Did any MotionEvents get skipped?");
            return false;
          }

          final int x = (int) (MotionEventCompat.getX(e, index) + 0.5f);
          final int y = (int) (MotionEventCompat.getY(e, index) + 0.5f);
          int dx = mLastTouchX - x;
          int dy = mLastTouchY - y;

          if (dispatchNestedPreScroll(dx, dy, mScrollConsumed, mScrollOffset)) {
            dx -= mScrollConsumed[0];
            dy -= mScrollConsumed[1];
            vtev.offsetLocation(mScrollOffset[0], mScrollOffset[1]);
            // Updated the nested offsets
            mNestedOffsets[0] += mScrollOffset[0];
            mNestedOffsets[1] += mScrollOffset[1];
          }

          if (mScrollState != SCROLL_STATE_DRAGGING) {
            boolean startScroll = false;
            if (canScrollHorizontally && Math.abs(dx) > mTouchSlop) {
              if (dx > 0) {
                dx -= mTouchSlop;
              } else {
                dx += mTouchSlop;
              }
              startScroll = true;
            }
            if (canScrollVertically && Math.abs(dy) > mTouchSlop) {
              if (dy > 0) {
                dy -= mTouchSlop;
              } else {
                dy += mTouchSlop;
              }
              startScroll = true;
            }
            if (startScroll) {
              setScrollState(SCROLL_STATE_DRAGGING);
            }
          }

          if (mScrollState == SCROLL_STATE_DRAGGING) {
            mLastTouchX = x - mScrollOffset[0];
            mLastTouchY = y - mScrollOffset[1];

            if (scrollByInternal(
                canScrollHorizontally ? dx : 0, canScrollVertically ? dy : 0, vtev)) {
              getParent().requestDisallowInterceptTouchEvent(true);
            }
          }
        }
        break;

      case MotionEventCompat.ACTION_POINTER_UP:
        {
          onPointerUp(e);
        }
        break;

      case MotionEvent.ACTION_UP:
        {
          mVelocityTracker.addMovement(vtev);
          eventAddedToVelocityTracker = true;
          mVelocityTracker.computeCurrentVelocity(1000, mMaxFlingVelocity);
          final float xvel =
              canScrollHorizontally
                  ? -VelocityTrackerCompat.getXVelocity(mVelocityTracker, mScrollPointerId)
                  : 0;
          final float yvel =
              canScrollVertically
                  ? -VelocityTrackerCompat.getYVelocity(mVelocityTracker, mScrollPointerId)
                  : 0;
          if (!((xvel != 0 || yvel != 0) && fling((int) xvel, (int) yvel))) {
            setScrollState(SCROLL_STATE_IDLE);
          }
          resetTouch();
        }
        break;

      case MotionEvent.ACTION_CANCEL:
        {
          cancelTouch();
        }
        break;
    }

    if (!eventAddedToVelocityTracker) {
      mVelocityTracker.addMovement(vtev);
    }
    vtev.recycle();

    return true;
  }
  @Override
  public boolean onInterceptTouchEvent(MotionEvent e) {

    final boolean canScrollHorizontally = false; // mLayout.canScrollHorizontally();
    final boolean canScrollVertically = true; // mLayout.canScrollVertically();

    if (mVelocityTracker == null) {
      mVelocityTracker = VelocityTracker.obtain();
    }
    mVelocityTracker.addMovement(e);

    final int action = MotionEventCompat.getActionMasked(e);
    final int actionIndex = MotionEventCompat.getActionIndex(e);

    switch (action) {
      case MotionEvent.ACTION_DOWN:
        if (mIgnoreMotionEventTillDown) {
          mIgnoreMotionEventTillDown = false;
        }
        mScrollPointerId = MotionEventCompat.getPointerId(e, 0);
        mInitialTouchX = mLastTouchX = (int) (e.getX() + 0.5f);
        mInitialTouchY = mLastTouchY = (int) (e.getY() + 0.5f);

        if (mScrollState == SCROLL_STATE_SETTLING) {
          getParent().requestDisallowInterceptTouchEvent(true);
          setScrollState(SCROLL_STATE_DRAGGING);
        }

        // Clear the nested offsets
        mNestedOffsets[0] = mNestedOffsets[1] = 0;

        int nestedScrollAxis = ViewCompat.SCROLL_AXIS_NONE;
        if (canScrollHorizontally) {
          nestedScrollAxis |= ViewCompat.SCROLL_AXIS_HORIZONTAL;
        }
        if (canScrollVertically) {
          nestedScrollAxis |= ViewCompat.SCROLL_AXIS_VERTICAL;
        }
        startNestedScroll(nestedScrollAxis);
        break;

      case MotionEventCompat.ACTION_POINTER_DOWN:
        mScrollPointerId = MotionEventCompat.getPointerId(e, actionIndex);
        mInitialTouchX = mLastTouchX = (int) (MotionEventCompat.getX(e, actionIndex) + 0.5f);
        mInitialTouchY = mLastTouchY = (int) (MotionEventCompat.getY(e, actionIndex) + 0.5f);
        break;

      case MotionEvent.ACTION_MOVE:
        {
          final int index = MotionEventCompat.findPointerIndex(e, mScrollPointerId);
          if (index < 0) {
            Log.e(
                TAG,
                "Error processing scroll; pointer index for id "
                    + mScrollPointerId
                    + " not found. Did any MotionEvents get skipped?");
            return false;
          }

          final int x = (int) (MotionEventCompat.getX(e, index) + 0.5f);
          final int y = (int) (MotionEventCompat.getY(e, index) + 0.5f);
          if (mScrollState != SCROLL_STATE_DRAGGING) {
            final int dx = x - mInitialTouchX;
            final int dy = y - mInitialTouchY;
            boolean startScroll = false;
            if (canScrollHorizontally && Math.abs(dx) > mTouchSlop) {
              mLastTouchX = mInitialTouchX + mTouchSlop * (dx < 0 ? -1 : 1);
              startScroll = true;
            }
            if (canScrollVertically && Math.abs(dy) > mTouchSlop) {
              mLastTouchY = mInitialTouchY + mTouchSlop * (dy < 0 ? -1 : 1);
              startScroll = true;
            }
            if (startScroll) {
              setScrollState(SCROLL_STATE_DRAGGING);
            }
          }
        }
        break;

      case MotionEventCompat.ACTION_POINTER_UP:
        {
          onPointerUp(e);
        }
        break;

      case MotionEvent.ACTION_UP:
        {
          mVelocityTracker.clear();
          stopNestedScroll();
        }
        break;

      case MotionEvent.ACTION_CANCEL:
        {
          cancelTouch();
        }
    }
    return mScrollState == SCROLL_STATE_DRAGGING;
  }
  @Override
  public boolean onInterceptTouchEvent(MotionEvent ev) {
    boolean handle = false;
    if (!mEnabled) {
      return false;
    }

    final int action = ev.getAction() & MotionEventCompat.ACTION_MASK;

    if (action == MotionEvent.ACTION_CANCEL || action == MotionEvent.ACTION_UP) {
      endDrag();
      return false;
    }

    if (action != MotionEvent.ACTION_DOWN) {
      if (mIsDragging) {
        return true;
      } else if (mIsUnableToDrag) {
        return false;
      }
    }

    switch (action) {
      case MotionEvent.ACTION_MOVE:
        final int activePointerId = mActivePointerId;
        if (activePointerId == INVALID_POINTER) {
          break;
        }

        final int pointerIndex = MotionEventCompat.findPointerIndex(ev, activePointerId);
        if (pointerIndex == -1) {
          mActivePointerId = INVALID_POINTER;
          break;
        }

        final float x = MotionEventCompat.getX(ev, pointerIndex);
        final float dx = x - mLastX;
        final float xDiff = Math.abs(dx);
        final float y = MotionEventCompat.getY(ev, pointerIndex);
        final float dy = y - mLastY;
        final float yDiff = Math.abs(y - mLastY);

        if (xDiff > mTouchSlop && xDiff > yDiff && allowDragingX(dx, mInitialX)) {
          handle = true;
          mLastX = x;
        } else if (yDiff > mTouchSlop && yDiff > xDiff && allowDragingY(dy, mInitialY)) {
          handle = true;
          mLastY = y;
        }
        break;

      case MotionEvent.ACTION_DOWN:
        mActivePointerId =
            ev.getAction()
                & (Build.VERSION.SDK_INT >= 8
                    ? MotionEvent.ACTION_POINTER_INDEX_MASK
                    : MotionEventCompat.ACTION_POINTER_INDEX_MASK);
        mLastX = mInitialX = MotionEventCompat.getX(ev, mActivePointerId);
        mLastY = mInitialY = MotionEventCompat.getY(ev, mActivePointerId);
        if (allowSlidingFromHereX(ev, mInitialX)) {
          handle = false;
          // If nobody else got the focus we use it to close the layer
          return super.onInterceptTouchEvent(ev);
        } else if (allowSlidingFromHereY(ev, mInitialY)) {
          handle = false;
          // If nobody else got the focus we use it to close the layer
          return super.onInterceptTouchEvent(ev);
        } else {
          mIsUnableToDrag = true;
        }
        break;
      case MotionEventCompat.ACTION_POINTER_UP:
        onSecondaryPointerUp(ev);
        break;
    }

    if (!mIsDragging) {
      if (mVelocityTracker == null) {
        mVelocityTracker = VelocityTracker.obtain();
      }
      mVelocityTracker.addMovement(ev);
    }

    return handle;
  }
  @Override
  public boolean onTouchEvent(MotionEvent ev) {
    if (!mEnabled
        || !mIsDragging
            && !mLastTouchAllowed
            && !allowSlidingFromHereX(ev, mInitialX)
            && !allowSlidingFromHereY(ev, mInitialY)) {
      return false;
    }

    final int action = ev.getAction();

    if (action == MotionEvent.ACTION_UP
        || action == MotionEvent.ACTION_CANCEL
        || action == MotionEvent.ACTION_OUTSIDE) {
      mLastTouchAllowed = false;
    } else {
      mLastTouchAllowed = true;
    }

    if (mVelocityTracker == null) {
      mVelocityTracker = VelocityTracker.obtain();
    }
    mVelocityTracker.addMovement(ev);

    switch (action & MotionEventCompat.ACTION_MASK) {
      case MotionEvent.ACTION_DOWN:
        completeScroll();

        // Remember where the motion event started
        mLastX = mInitialX = ev.getX();
        mLastY = mInitialY = ev.getY();
        mActivePointerId = MotionEventCompat.getPointerId(ev, 0);
        break;
      case MotionEvent.ACTION_MOVE:
        if (!mIsDragging) {
          final int pointerIndex = MotionEventCompat.findPointerIndex(ev, mActivePointerId);
          if (pointerIndex == -1) {
            mActivePointerId = INVALID_POINTER;
            break;
          }
          final float x = MotionEventCompat.getX(ev, pointerIndex);
          final float xDiff = Math.abs(x - mLastX);
          final float y = MotionEventCompat.getY(ev, pointerIndex);
          final float yDiff = Math.abs(y - mLastY);
          if (xDiff > mTouchSlop && xDiff > yDiff) {
            mIsDragging = true;
            mLastX = x;
            setDrawingCacheEnabled(true);
          } else if (yDiff > mTouchSlop && yDiff > xDiff) {
            mIsDragging = true;
            mLastY = y;
            setDrawingCacheEnabled(true);
          }
        }
        if (mIsDragging) {
          // Scroll to follow the motion event
          final int activePointerIndex = MotionEventCompat.findPointerIndex(ev, mActivePointerId);
          if (activePointerIndex == -1) {
            mActivePointerId = INVALID_POINTER;
            break;
          }
          final float x = MotionEventCompat.getX(ev, activePointerIndex);
          final float y = MotionEventCompat.getY(ev, activePointerIndex);
          final float deltaX = mLastX - x;
          final float deltaY = mLastY - y;
          mLastX = x;
          mLastY = y;
          final float oldScrollX = getScrollX();
          final float oldScrollY = getScrollY();
          float scrollX = oldScrollX + deltaX;
          float scrollY = oldScrollY + deltaY;

          // Log.d("Layer", String.format("Layer scrollX[%f],scrollY[%f]", scrollX, scrollY));
          final float leftBound, rightBound;
          final float bottomBound, topBound;
          switch (mScreenSide) {
            case STICK_TO_LEFT:
              topBound = bottomBound = rightBound = 0;
              leftBound = getWidth(); // How far left we can scroll
              break;
            case STICK_TO_MIDDLE:
              topBound = getHeight();
              bottomBound = -getHeight();
              leftBound = getWidth();
              rightBound = -getWidth();
              break;
            case STICK_TO_RIGHT:
              rightBound = -getWidth();
              topBound = bottomBound = leftBound = 0;
              break;
            case STICK_TO_TOP:
              topBound = getHeight();
              bottomBound = rightBound = leftBound = 0;
              break;
            case STICK_TO_BOTTOM:
              topBound = rightBound = leftBound = 0;
              bottomBound = -getHeight();
              break;
            default:
              topBound = bottomBound = rightBound = leftBound = 0;
              break;
          }
          if (scrollX > leftBound) {
            scrollX = leftBound;
          } else if (scrollX < rightBound) {
            scrollX = rightBound;
          }
          if (scrollY > topBound) {
            scrollY = topBound;
          } else if (scrollY < bottomBound) {
            scrollY = bottomBound;
          }

          // Keep the precision
          mLastX += scrollX - (int) scrollX;
          mLastY += scrollY - (int) scrollY;
          scrollTo((int) scrollX, (int) scrollY);
        }
        break;
      case MotionEvent.ACTION_UP:
        if (mIsDragging) {
          final VelocityTracker velocityTracker = mVelocityTracker;
          velocityTracker.computeCurrentVelocity(1000, mMaximumVelocity);
          final int initialVelocityX =
              (int) VelocityTrackerCompat.getXVelocity(velocityTracker, mActivePointerId);
          final int initialVelocityY =
              (int) VelocityTrackerCompat.getYVelocity(velocityTracker, mActivePointerId);
          final int scrollX = getScrollX();
          final int scrollY = getScrollY();
          final int activePointerIndex = MotionEventCompat.findPointerIndex(ev, mActivePointerId);
          final float x = MotionEventCompat.getX(ev, activePointerIndex);
          final float y = MotionEventCompat.getY(ev, activePointerIndex);
          final int totalDeltaX = (int) (x - mInitialX);
          final int totalDeltaY = (int) (y - mInitialY);

          boolean nextStateOpened =
              determineNextStateOpened(
                  mIsOpen,
                  scrollX,
                  scrollY,
                  initialVelocityX,
                  initialVelocityY,
                  totalDeltaX,
                  totalDeltaY);
          switchLayer(nextStateOpened, true, true, initialVelocityX, initialVelocityY);

          mActivePointerId = INVALID_POINTER;
          endDrag();
        } else if (mIsOpen && closeOnTapEnabled) {
          closeLayer(true);
        } else if (!mIsOpen && openOnTapEnabled) {
          openLayer(true);
        }
        break;
      case MotionEvent.ACTION_CANCEL:
        if (mIsDragging) {
          switchLayer(mIsOpen, true, true);
          mActivePointerId = INVALID_POINTER;
          endDrag();
        }
        break;
      case MotionEventCompat.ACTION_POINTER_DOWN:
        {
          final int index = MotionEventCompat.getActionIndex(ev);
          mLastX = MotionEventCompat.getX(ev, index);
          mLastY = MotionEventCompat.getY(ev, index);
          mActivePointerId = MotionEventCompat.getPointerId(ev, index);
          break;
        }
      case MotionEventCompat.ACTION_POINTER_UP:
        onSecondaryPointerUp(ev);
        mLastX =
            MotionEventCompat.getX(ev, MotionEventCompat.findPointerIndex(ev, mActivePointerId));
        mLastY =
            MotionEventCompat.getY(ev, MotionEventCompat.findPointerIndex(ev, mActivePointerId));
        break;
    }
    if (mActivePointerId == INVALID_POINTER) {
      mLastTouchAllowed = false;
    }
    return true;
  }
Exemple #18
0
  @Override
  public boolean onInterceptTouchEvent(MotionEvent ev) {

    if (!mEnabled) return false;

    final int action = ev.getAction() & MotionEventCompat.ACTION_MASK;

    if (action == MotionEvent.ACTION_DOWN && DEBUG) Log.v(TAG, "Received ACTION_DOWN");

    if (action == MotionEvent.ACTION_CANCEL
        || action == MotionEvent.ACTION_UP
        || (action != MotionEvent.ACTION_DOWN && mIsUnableToDrag)) {
      endDrag();
      return false;
    }

    switch (action) {
      case MotionEvent.ACTION_MOVE:
        try {
          final int activePointerId = mActivePointerId;
          if (activePointerId == INVALID_POINTER) break;
          final int pointerIndex = this.getPointerIndex(ev, activePointerId);
          final float x = MotionEventCompat.getX(ev, pointerIndex);
          final float dx = x - mLastMotionX;
          final float xDiff = Math.abs(dx);
          final float y = MotionEventCompat.getY(ev, pointerIndex);
          final float yDiff = Math.abs(y - mLastMotionY);
          if (DEBUG)
            Log.v(
                TAG,
                "onInterceptTouch moved to:("
                    + x
                    + ", "
                    + y
                    + "), diff:("
                    + xDiff
                    + ", "
                    + yDiff
                    + "), mLastMotionX:"
                    + mLastMotionX);
          if (xDiff > mTouchSlop && xDiff > yDiff && thisSlideAllowed(dx)) {
            if (DEBUG) Log.v(TAG, "Starting drag! from onInterceptTouch");
            startDrag();
            mLastMotionX = x;
            setScrollingCacheEnabled(true);
          } else if (yDiff > mTouchSlop) {
            mIsUnableToDrag = true;
          }
        } catch (IllegalArgumentException e) {
          e.printStackTrace();
        }
        break;

      case MotionEvent.ACTION_DOWN:
        mActivePointerId =
            ev.getAction()
                & ((Build.VERSION.SDK_INT >= 8)
                    ? MotionEvent.ACTION_POINTER_INDEX_MASK
                    : MotionEvent.ACTION_POINTER_INDEX_MASK);
        mLastMotionX = mInitialMotionX = MotionEventCompat.getX(ev, mActivePointerId);
        mLastMotionY = MotionEventCompat.getY(ev, mActivePointerId);
        if (thisTouchAllowed(ev)) {
          mIsBeingDragged = false;
          mIsUnableToDrag = false;
          if (isMenuOpen()
              && mViewBehind.menuTouchInQuickReturn(mContent, mCurItem, ev.getX() + mScrollX)) {
            mQuickReturn = true;
          }
        } else {
          mIsUnableToDrag = true;
        }
        break;
      case MotionEventCompat.ACTION_POINTER_UP:
        onSecondaryPointerUp(ev);
        break;
    }

    if (!mIsBeingDragged) {
      if (mVelocityTracker == null) {
        mVelocityTracker = VelocityTracker.obtain();
      }
      mVelocityTracker.addMovement(ev);
    }
    return mIsBeingDragged || mQuickReturn;
  }
 public boolean onTouchEvent(MotionEvent paramMotionEvent) {
   int i1 = 0;
   if (super.onTouchEvent(paramMotionEvent)) {}
   label317:
   do {
     do {
       float f1;
       float f2;
       do {
         do {
           do {
             return true;
             if ((f == null) || (f.getAdapter().getCount() == 0)) {
               return false;
             }
             i2 = paramMotionEvent.getAction() & 0xFF;
             switch (i2) {
               case 4:
               default:
                 return true;
               case 0:
                 a = MotionEventCompat.getPointerId(paramMotionEvent, 0);
                 p = paramMotionEvent.getX();
                 return true;
               case 2:
                 f1 =
                     MotionEventCompat.getX(
                         paramMotionEvent,
                         MotionEventCompat.findPointerIndex(paramMotionEvent, a));
                 f2 = f1 - p;
                 if ((!q) && (Math.abs(f2) > o)) {
                   q = true;
                 }
                 break;
             }
           } while (!q);
           p = f1;
         } while ((!f.isFakeDragging()) && (!f.beginFakeDrag()));
         f.fakeDragBy(f2);
         return true;
         if (q) {
           break label317;
         }
         i1 = f.getAdapter().getCount();
         int i3 = getWidth();
         f1 = i3 / 2.0F;
         f2 = i3 / 6.0F;
         if ((h <= 0) || (paramMotionEvent.getX() >= f1 - f2)) {
           break;
         }
       } while (i2 == 3);
       f.setCurrentItem(h - 1);
       return true;
       if ((h >= i1 - 1) || (paramMotionEvent.getX() <= f2 + f1)) {
         break;
       }
     } while (i2 == 3);
     f.setCurrentItem(h + 1);
     return true;
     q = false;
     a = -1;
   } while (!f.isFakeDragging());
   f.endFakeDrag();
   return true;
   i1 = MotionEventCompat.getActionIndex(paramMotionEvent);
   p = MotionEventCompat.getX(paramMotionEvent, i1);
   a = MotionEventCompat.getPointerId(paramMotionEvent, i1);
   return true;
   int i2 = MotionEventCompat.getActionIndex(paramMotionEvent);
   if (MotionEventCompat.getPointerId(paramMotionEvent, i2) == a) {
     if (i2 == 0) {
       i1 = 1;
     }
     a = MotionEventCompat.getPointerId(paramMotionEvent, i1);
   }
   p =
       MotionEventCompat.getX(
           paramMotionEvent, MotionEventCompat.findPointerIndex(paramMotionEvent, a));
   return true;
 }
  public boolean onTouchEvent(android.view.MotionEvent ev) {
    if ((mViewPager == null) || (mViewPager.getAdapter().getCount() == 0)) {
      return false;
    }

    final int action = ev.getAction();

    switch (action & MotionEventCompat.ACTION_MASK) {
      case MotionEvent.ACTION_DOWN:
        mActivePointerId = MotionEventCompat.getPointerId(ev, 0);
        mLastMotionX = ev.getX();
        break;

      case MotionEvent.ACTION_MOVE:
        {
          final int activePointerIndex = MotionEventCompat.findPointerIndex(ev, mActivePointerId);
          final float x = MotionEventCompat.getX(ev, activePointerIndex);
          final float deltaX = x - mLastMotionX;

          if (!mIsDragging) {
            if (Math.abs(deltaX) > mTouchSlop) {
              mIsDragging = true;
            }
          }

          if (mIsDragging) {
            if (!mViewPager.isFakeDragging()) {
              mViewPager.beginFakeDrag();
            }

            mLastMotionX = x;

            mViewPager.fakeDragBy(deltaX);
          }

          break;
        }

      case MotionEvent.ACTION_CANCEL:
      case MotionEvent.ACTION_UP:
        if (!mIsDragging) {
          final int count = mViewPager.getAdapter().getCount();
          final int width = getWidth();
          final float halfWidth = width / 2f;
          final float sixthWidth = width / 6f;

          if ((mCurrentPage > 0) && (ev.getX() < halfWidth - sixthWidth)) {
            mViewPager.setCurrentItem(mCurrentPage - 1);
            return true;
          } else if ((mCurrentPage < count - 1) && (ev.getX() > halfWidth + sixthWidth)) {
            mViewPager.setCurrentItem(mCurrentPage + 1);
            return true;
          }
        }

        mIsDragging = false;
        mActivePointerId = INVALID_POINTER;
        if (mViewPager.isFakeDragging()) mViewPager.endFakeDrag();
        break;

      case MotionEventCompat.ACTION_POINTER_DOWN:
        {
          final int index = MotionEventCompat.getActionIndex(ev);
          final float x = MotionEventCompat.getX(ev, index);
          mLastMotionX = x;
          mActivePointerId = MotionEventCompat.getPointerId(ev, index);
          break;
        }

      case MotionEventCompat.ACTION_POINTER_UP:
        final int pointerIndex = MotionEventCompat.getActionIndex(ev);
        final int pointerId = MotionEventCompat.getPointerId(ev, pointerIndex);
        if (pointerId == mActivePointerId) {
          final int newPointerIndex = pointerIndex == 0 ? 1 : 0;
          mActivePointerId = MotionEventCompat.getPointerId(ev, newPointerIndex);
        }
        mLastMotionX =
            MotionEventCompat.getX(ev, MotionEventCompat.findPointerIndex(ev, mActivePointerId));
        break;
    }

    return true;
  };
Exemple #21
0
  public boolean onTouchEvent(MotionEvent var1) {
    this.initVelocityTrackerIfNotExists();
    MotionEvent var9 = MotionEvent.obtain(var1);
    int var2 = MotionEventCompat.getActionMasked(var1);
    if (var2 == 0) {
      this.mNestedYOffset = 0;
    }

    var9.offsetLocation(0.0F, (float) this.mNestedYOffset);
    ViewParent var10;
    switch (var2) {
      case 0:
        if (this.getChildCount() == 0) {
          return false;
        }

        boolean var8;
        if (!this.mScroller.isFinished()) {
          var8 = true;
        } else {
          var8 = false;
        }

        this.mIsBeingDragged = var8;
        if (var8) {
          var10 = this.getParent();
          if (var10 != null) {
            var10.requestDisallowInterceptTouchEvent(true);
          }
        }

        if (!this.mScroller.isFinished()) {
          this.mScroller.abortAnimation();
        }

        this.mLastMotionY = (int) var1.getY();
        this.mActivePointerId = MotionEventCompat.getPointerId(var1, 0);
        this.startNestedScroll(2);
        break;
      case 1:
        if (this.mIsBeingDragged) {
          VelocityTracker var11 = this.mVelocityTracker;
          var11.computeCurrentVelocity(1000, (float) this.mMaximumVelocity);
          var2 = (int) VelocityTrackerCompat.getYVelocity(var11, this.mActivePointerId);
          if (Math.abs(var2) > this.mMinimumVelocity) {
            this.flingWithNestedDispatch(-var2);
          }

          this.mActivePointerId = -1;
          this.endDrag();
        }
        break;
      case 2:
        int var4 = MotionEventCompat.findPointerIndex(var1, this.mActivePointerId);
        if (var4 == -1) {
          Log.e(
              "NestedScrollView",
              "Invalid pointerId=" + this.mActivePointerId + " in onTouchEvent");
        } else {
          int var5 = (int) MotionEventCompat.getY(var1, var4);
          var2 = this.mLastMotionY - var5;
          int var3 = var2;
          if (this.dispatchNestedPreScroll(0, var2, this.mScrollConsumed, this.mScrollOffset)) {
            var3 = var2 - this.mScrollConsumed[1];
            var9.offsetLocation(0.0F, (float) this.mScrollOffset[1]);
            this.mNestedYOffset += this.mScrollOffset[1];
          }

          var2 = var3;
          if (!this.mIsBeingDragged) {
            var2 = var3;
            if (Math.abs(var3) > this.mTouchSlop) {
              var10 = this.getParent();
              if (var10 != null) {
                var10.requestDisallowInterceptTouchEvent(true);
              }

              this.mIsBeingDragged = true;
              if (var3 > 0) {
                var2 = var3 - this.mTouchSlop;
              } else {
                var2 = var3 + this.mTouchSlop;
              }
            }
          }

          if (this.mIsBeingDragged) {
            this.mLastMotionY = var5 - this.mScrollOffset[1];
            int var6 = this.getScrollY();
            var5 = this.getScrollRange();
            var3 = ViewCompat.getOverScrollMode(this);
            boolean var12;
            if (var3 != 0 && (var3 != 1 || var5 <= 0)) {
              var12 = false;
            } else {
              var12 = true;
            }

            if (this.overScrollByCompat(0, var2, 0, this.getScrollY(), 0, var5, 0, 0, true)
                && !this.hasNestedScrollingParent()) {
              this.mVelocityTracker.clear();
            }

            int var7 = this.getScrollY() - var6;
            if (this.dispatchNestedScroll(0, var7, 0, var2 - var7, this.mScrollOffset)) {
              this.mLastMotionY -= this.mScrollOffset[1];
              var9.offsetLocation(0.0F, (float) this.mScrollOffset[1]);
              this.mNestedYOffset += this.mScrollOffset[1];
            } else if (var12) {
              this.ensureGlows();
              var3 = var6 + var2;
              if (var3 < 0) {
                this.mEdgeGlowTop.onPull(
                    (float) var2 / (float) this.getHeight(),
                    MotionEventCompat.getX(var1, var4) / (float) this.getWidth());
                if (!this.mEdgeGlowBottom.isFinished()) {
                  this.mEdgeGlowBottom.onRelease();
                }
              } else if (var3 > var5) {
                this.mEdgeGlowBottom.onPull(
                    (float) var2 / (float) this.getHeight(),
                    1.0F - MotionEventCompat.getX(var1, var4) / (float) this.getWidth());
                if (!this.mEdgeGlowTop.isFinished()) {
                  this.mEdgeGlowTop.onRelease();
                }
              }

              if (this.mEdgeGlowTop != null
                  && (!this.mEdgeGlowTop.isFinished() || !this.mEdgeGlowBottom.isFinished())) {
                ViewCompat.postInvalidateOnAnimation(this);
              }
            }
          }
        }
        break;
      case 3:
        if (this.mIsBeingDragged && this.getChildCount() > 0) {
          this.mActivePointerId = -1;
          this.endDrag();
        }
      case 4:
      default:
        break;
      case 5:
        var2 = MotionEventCompat.getActionIndex(var1);
        this.mLastMotionY = (int) MotionEventCompat.getY(var1, var2);
        this.mActivePointerId = MotionEventCompat.getPointerId(var1, var2);
        break;
      case 6:
        this.onSecondaryPointerUp(var1);
        this.mLastMotionY =
            (int)
                MotionEventCompat.getY(
                    var1, MotionEventCompat.findPointerIndex(var1, this.mActivePointerId));
    }

    if (this.mVelocityTracker != null) {
      this.mVelocityTracker.addMovement(var9);
    }

    var9.recycle();
    return true;
  }