Example #1
0
  public boolean onTouchEvent(MotionEvent event) {
    switch (event.getActionMasked()) {
      case MotionEvent.ACTION_DOWN:
        sX = event.getX();
        sY = event.getY();
        ptrID1 = event.getPointerId(0);
        mAngle = 0;
        firstTouch = true;
        break;
      case MotionEvent.ACTION_POINTER_DOWN:
        fX = event.getX();
        fY = event.getY();
        focalX = getMidpoint(fX, sX);
        focalY = getMidpoint(fY, sY);
        ptrID2 = event.getPointerId(event.getActionIndex());
        mAngle = 0;
        firstTouch = true;
        break;
      case MotionEvent.ACTION_MOVE:
        if (ptrID1 != INVALID_POINTER_ID && ptrID2 != INVALID_POINTER_ID) {
          float nfX, nfY, nsX, nsY;
          nsX = event.getX(event.findPointerIndex(ptrID1));
          nsY = event.getY(event.findPointerIndex(ptrID1));
          nfX = event.getX(event.findPointerIndex(ptrID2));
          nfY = event.getY(event.findPointerIndex(ptrID2));
          if (firstTouch) {
            mAngle = 0;
            firstTouch = false;
          } else {
            mAngle = angleBetweenLines(fX, fY, sX, sY, nfX, nfY, nsX, nsY);
          }

          if (mListener != null) {
            mListener.OnRotation(this);
          }
          fX = nfX;
          fY = nfY;
          sX = nsX;
          sY = nsY;
        }
        break;
      case MotionEvent.ACTION_UP:
        ptrID1 = INVALID_POINTER_ID;
        break;
      case MotionEvent.ACTION_POINTER_UP:
        ptrID2 = INVALID_POINTER_ID;
        break;
    }
    return true;
  }
    @Override
    public boolean onTouchEvent(MotionEvent ev) {
      final int action = ev.getAction();
      switch (action & MotionEvent.ACTION_MASK) {
        case MotionEvent.ACTION_DOWN:
          mActivePointerId = ev.getPointerId(0);
          break;
        case MotionEvent.ACTION_CANCEL:
        case MotionEvent.ACTION_UP:
          mActivePointerId = INVALID_POINTER_ID;
          break;
        case MotionEvent.ACTION_POINTER_UP:
          final int pointerIndex =
              (ev.getAction() & MotionEvent.ACTION_POINTER_INDEX_MASK)
                  >> MotionEvent.ACTION_POINTER_INDEX_SHIFT;
          final int pointerId = ev.getPointerId(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;
            mActivePointerId = ev.getPointerId(newPointerIndex);
            mLastTouchX = ev.getX(newPointerIndex);
            mLastTouchY = ev.getY(newPointerIndex);
          }
          break;
      }

      mActivePointerIndex =
          ev.findPointerIndex(mActivePointerId != INVALID_POINTER_ID ? mActivePointerId : 0);
      return super.onTouchEvent(ev);
    }
  /*
   * Determines if we should change the touch state to start dragging after the
   * user moves their touch point far enough.
   */
  protected void determineDraggingStart(MotionEvent ev) {
    /*
     * Locally do absolute value. mLastMotionX is set to the y value
     * of the down event.
     */
    final int pointerIndex = ev.findPointerIndex(mActivePointerId);
    final float x = ev.getX(pointerIndex);
    final float y = ev.getY(pointerIndex);
    final int xDiff = (int) Math.abs(x - mLastMotionX);
    final int yDiff = (int) Math.abs(y - mLastMotionY);

    final int touchSlop = mTouchSlop;
    boolean yMoved = yDiff > touchSlop;
    boolean isUpwardMotion = (yDiff / (float) xDiff) > mDragSlopeThreshold;

    if (isUpwardMotion && yMoved && mLastTouchedItem != null) {
      // Drag if the user moved far enough along the Y axis
      beginDragging(mLastTouchedItem);

      // Cancel any pending long press
      if (mAllowLongPress) {
        mAllowLongPress = false;
        // Try canceling the long press. It could also have been scheduled
        // by a distant descendant, so use the mAllowLongPress flag to block
        // everything
        final View currentPage = getPageAt(mCurrentPage);
        if (currentPage != null) {
          currentPage.cancelLongPress();
        }
      }
    }
  }
  @Override
  public boolean onTouchEvent(MotionEvent ev) {
    final int action = ev.getAction();
    switch (action & MotionEvent.ACTION_MASK) {
      case MotionEvent.ACTION_DOWN:
        mActivePointerId = ev.getPointerId(0);
        break;
      case MotionEvent.ACTION_CANCEL:
      case MotionEvent.ACTION_UP:
        mActivePointerId = INVALID_POINTER_ID;
        break;
      case MotionEvent.ACTION_POINTER_UP:
        // Ignore deprecation, ACTION_POINTER_ID_MASK and
        // ACTION_POINTER_ID_SHIFT has same value and are deprecated
        // You can have either deprecation or lint target api warning
        final int pointerIndex = Compat.getPointerIndex(ev.getAction());
        final int pointerId = ev.getPointerId(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;
          mActivePointerId = ev.getPointerId(newPointerIndex);
          mLastTouchX = ev.getX(newPointerIndex);
          mLastTouchY = ev.getY(newPointerIndex);
        }
        break;
    }

    mActivePointerIndex =
        ev.findPointerIndex(mActivePointerId != INVALID_POINTER_ID ? mActivePointerId : 0);
    return super.onTouchEvent(ev);
  }
  private boolean processMoveEvent(MotionEvent ev) {
    if (pointerId != INVALID_POINTER_ID) {
      final int pointerIndex = ev.findPointerIndex(pointerId);

      float x = ev.getX(pointerIndex);
      float y = ev.getY(pointerIndex);

      if (x < this.centerX - this.joystickRadius) {
        this.xPosition = (int) (this.centerX - this.joystickRadius);
      } else if (x > this.centerX + this.joystickRadius) {
        this.xPosition = (int) (this.centerX + this.joystickRadius);
      } else {
        this.xPosition = (int) x;
      }

      if (y < this.centerY - this.joystickRadius) {
        this.yPosition = (int) (this.centerY - this.joystickRadius);
      } else if (y > this.centerY + this.joystickRadius) {
        this.yPosition = (int) (this.centerY + this.joystickRadius);
      } else {
        this.yPosition = (int) y;
      }

      if (Math.abs(this.xPosition - this.centerX) > 5) {
        reportOnMoved();
      }
      invalidate();
      return true;
    }
    return false;
  }
  private boolean handleRowTouch(View view, MotionEvent event) {
    int action = event.getAction();
    switch (action & MotionEvent.ACTION_MASK) {
      case MotionEvent.ACTION_DOWN:
        mActivePointerId = event.getPointerId(0);
        mLastDownXlat = view.getTranslationX();
        mLastDraggingView = view;
        mLastDownX = event.getRawX();

        mVelocityTracker = VelocityTracker.obtain();
        mVelocityTracker.addMovement(event);

        int idx = mViews.indexOf(mLastDraggingView);
        mSpringChain.setControlSpringIndex(idx);

        mSpringChain.getControlSpring().setCurrentValue(mLastDownXlat);
        break;
      case MotionEvent.ACTION_MOVE:
        {
          final int pointerIndex = event.findPointerIndex(mActivePointerId);
          if (pointerIndex != -1) {
            final int location[] = {0, 0};
            view.getLocationOnScreen(location);
            float x = event.getX(pointerIndex) + location[0];
            float offset = x - mLastDownX + mLastDownXlat;
            mSpringChain.getControlSpring().setCurrentValue(offset);
            mVelocityTracker.addMovement(event);
          }
          break;
        }
      case MotionEvent.ACTION_CANCEL:
      case MotionEvent.ACTION_UP:
        final int pointerIndex = event.findPointerIndex(mActivePointerId);
        if (pointerIndex != -1) {
          mVelocityTracker.addMovement(event);
          mVelocityTracker.computeCurrentVelocity(1000);
          mSpringChain
              .getControlSpring()
              .setVelocity(mVelocityTracker.getXVelocity())
              .setEndValue(0);
        }
        break;
    }
    return true;
  }
Example #7
0
  /**
   * Handles forwarded events.
   *
   * @param activePointerId id of the pointer that activated forwarding
   * @return whether the event was handled
   */
  public boolean onForwardedEvent(MotionEvent event, int activePointerId) {
    boolean handledEvent = true;
    View vibrateView = null;

    final int actionMasked = event.getActionMasked();
    switch (actionMasked) {
      case MotionEvent.ACTION_CANCEL:
        handledEvent = false;
        break;
      case MotionEvent.ACTION_UP:
        handledEvent = false;
        // $FALL-THROUGH$
      case MotionEvent.ACTION_DOWN:
      case MotionEvent.ACTION_MOVE:
        final int activeIndex = event.findPointerIndex(activePointerId);
        if (activeIndex < 0) {
          handledEvent = false;
          break;
        }

        final int x = (int) event.getX(activeIndex);
        final int y = (int) event.getY(activeIndex);

        View pressedView = findViewByCoordinate(this, x, y, 0);
        if (mPressedChild != pressedView) {
          vibrateView = pressedView != null ? pressedView : mPressedChild;
          if (pressedView != null) pressedView.setPressed(true);
          if (mPressedChild != null) mPressedChild.setPressed(false);
          mPressedChild = pressedView;

          if (mOnForwardedEventListener != null) {
            mOnForwardedEventListener.onPressedView(event, activePointerId, mPressedChild);
          }
        }

        if (actionMasked == MotionEvent.ACTION_UP) {
          vibrateView = mPressedChild;
          clickPressedItem();
        }
        break;
    }

    // Failure to handle the event cancels forwarding.
    if (!handledEvent) {
      clearPressedItem();
    }

    if (vibrateView != null && mVibrateOnForwarded) {
      vibrateView.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
    }

    if (mOnForwardedEventListener != null) {
      mOnForwardedEventListener.onForwardedEvent(event, activePointerId);
    }

    return handledEvent;
  }
Example #8
0
 public boolean onTouchEvent(final MotionEvent event) {
   switch (event.getActionMasked()) {
     case MotionEvent.ACTION_DOWN:
       {
         final int index = event.getActionIndex();
         isInTouchSequence = true;
         mainPointerId = event.getPointerId(event.getActionIndex());
         nativeTouchDown(event.getX(index), event.getY(index));
         break;
       }
     case MotionEvent.ACTION_MOVE:
       {
         if (!isInTouchSequence) {
           return false;
         }
         final int index = event.findPointerIndex(mainPointerId);
         if (index != -1) {
           nativeTouchMove(event.getX(index), event.getY(index));
         }
         break;
       }
     case MotionEvent.ACTION_UP:
       // Fall through
     case MotionEvent.ACTION_CANCEL:
       {
         final int index = event.findPointerIndex(mainPointerId);
         nativeTouchUp(event.getX(index), event.getY(index));
         isInTouchSequence = false;
         mainPointerId = -1;
         break;
       }
     case MotionEvent.ACTION_POINTER_DOWN:
       {
         return false;
       }
     case MotionEvent.ACTION_POINTER_UP:
       {
         return false;
       }
     default:
   }
   return true;
 }
  private void handleCancel(MotionEvent event) {
    if (DEBUG && mDragging) Log.v(TAG, "** Handle CANCEL");

    // Drop the active target if canceled.
    mActiveTarget = -1;

    int actionIndex = event.findPointerIndex(mPointerId);
    actionIndex = actionIndex == -1 ? 0 : actionIndex;
    switchToState(STATE_FINISH, event.getX(actionIndex), event.getY(actionIndex));
  }
  private void trackTouchEvent(MotionEvent event) {
    final int pointerIndex = event.findPointerIndex(mActivePointerId);
    final float x = event.getX(pointerIndex);

    if (Thumb.MIN.equals(pressedThumb) && !mSingleThumb) {
      setNormalizedMinValue(screenToNormalized(x));
    } else if (Thumb.MAX.equals(pressedThumb)) {
      setNormalizedMaxValue(screenToNormalized(x));
    }
  }
  /* #Public Methods */
  public boolean onTouchEvent(MotionEvent event) {
    switch (event.getActionMasked()) {
      case MotionEvent.ACTION_DOWN:
        ptrID1 = event.getPointerId(event.getActionIndex());
        break;
      case MotionEvent.ACTION_POINTER_DOWN:
        ptrID2 = event.getPointerId(event.getActionIndex());
        sX = event.getX(event.findPointerIndex(ptrID1));
        sY = event.getY(event.findPointerIndex(ptrID1));
        fX = event.getX(event.findPointerIndex(ptrID2));
        fY = event.getY(event.findPointerIndex(ptrID2));
        break;
      case MotionEvent.ACTION_MOVE:
        if (ptrID1 != INVALID_POINTER_ID && ptrID2 != INVALID_POINTER_ID) {
          float nfX, nfY, nsX, nsY;
          nsX = event.getX(event.findPointerIndex(ptrID1));
          nsY = event.getY(event.findPointerIndex(ptrID1));
          nfX = event.getX(event.findPointerIndex(ptrID2));
          nfY = event.getY(event.findPointerIndex(ptrID2));

          mAngle = angleBetweenLines(fX, fY, sX, sY, nfX, nfY, nsX, nsY);

          if (mListener != null) {
            mListener.OnRotation(this);
          }
        }
        break;
      case MotionEvent.ACTION_UP:
        ptrID1 = INVALID_POINTER_ID;
        break;
      case MotionEvent.ACTION_POINTER_UP:
        ptrID2 = INVALID_POINTER_ID;
        break;
      case MotionEvent.ACTION_CANCEL:
        ptrID1 = INVALID_POINTER_ID;
        ptrID2 = INVALID_POINTER_ID;
        break;
    }
    return true;
  }
Example #12
0
  private void handleCancel(MotionEvent event) {
    if (DEBUG && mDragging) Log.v(TAG, "** Handle CANCEL");

    // We should drop the active target here but it interferes with
    // moving off the screen in the direction of the navigation bar. At some point we may
    // want to revisit how we handle this. For now we'll allow a canceled event to
    // activate the current target.

    // mActiveTarget = -1; // Drop the active target if canceled.

    int actionIndex = event.findPointerIndex(mPointerId);
    actionIndex = actionIndex == -1 ? 0 : actionIndex;
    switchToState(STATE_FINISH, event.getX(actionIndex), event.getY(actionIndex));
  }
 @Override
 public void onPointerEvent(MotionEvent motionEvent) {
   final int action = motionEvent.getAction();
   switch (action & MotionEvent.ACTION_MASK) {
     case MotionEvent.ACTION_DOWN:
       mPointerId = motionEvent.getPointerId(0);
       mDownX = motionEvent.getX();
       mDownY = motionEvent.getY();
       break;
     case MotionEvent.ACTION_MOVE:
       if (mPointerId >= 0) {
         int index = motionEvent.findPointerIndex(mPointerId);
         /// M : ALPS01259500, JE at motionEvent.getX
         /// The value of index sometimes was less than 0
         /// Before using it, need to check the value @{
         if (index < 0) {
           mPointerId = -1;
           break;
         }
         /// @}
         if ((motionEvent.getEventTime() - motionEvent.getDownTime()) > TAP_TIMEOUT_MSEC
             || (motionEvent.getX(index) - mDownX) > mMotionSlop
             || (motionEvent.getY(index) - mDownY) > mMotionSlop) {
           mPointerId = -1;
         }
       }
       break;
     case MotionEvent.ACTION_UP:
     case MotionEvent.ACTION_POINTER_UP:
       {
         int index =
             (action & MotionEvent.ACTION_POINTER_INDEX_MASK)
                 >> MotionEvent.ACTION_POINTER_INDEX_SHIFT;
         // Extract the index of the pointer that left the touch sensor
         if (mPointerId == motionEvent.getPointerId(index)) {
           final int x = (int) motionEvent.getX(index);
           final int y = (int) motionEvent.getY(index);
           if ((motionEvent.getEventTime() - motionEvent.getDownTime()) < TAP_TIMEOUT_MSEC
               && (x - mDownX) < mMotionSlop
               && (y - mDownY) < mMotionSlop
               && !mTouchExcludeRegion.contains(x, y)) {
             mService.mH.obtainMessage(H.TAP_OUTSIDE_STACK, x, y, mDisplayContent).sendToTarget();
           }
           mPointerId = -1;
         }
         break;
       }
   }
 }
  @Override
  public boolean onInterceptTouchEvent(MotionEvent event) {
    if (mTopCard == null) {
      return false;
    }
    if (mGestureDetector.onTouchEvent(event)) {
      return true;
    }
    final int pointerIndex;
    final float x, y;
    final float dx, dy;
    switch (event.getActionMasked()) {
      case MotionEvent.ACTION_DOWN:
        mTopCard.getHitRect(childRect);

        CardModel cardModel = (CardModel) getAdapter().getItem(getChildCount() - 1);

        if (cardModel.getOnClickListener() != null) {
          cardModel.getOnClickListener().OnClickListener();
        }
        pointerIndex = event.getActionIndex();
        x = event.getX(pointerIndex);
        y = event.getY(pointerIndex);

        if (!childRect.contains((int) x, (int) y)) {
          return false;
        }

        mLastTouchX = x;
        mLastTouchY = y;
        mActivePointerId = event.getPointerId(pointerIndex);
        break;
      case MotionEvent.ACTION_MOVE:
        pointerIndex = event.findPointerIndex(mActivePointerId);
        x = event.getX(pointerIndex);
        y = event.getY(pointerIndex);
        if (Math.abs(x - mLastTouchX) > mTouchSlop || Math.abs(y - mLastTouchY) > mTouchSlop) {
          float[] points = new float[] {x - mTopCard.getLeft(), y - mTopCard.getTop()};
          mTopCard.getMatrix().invert(mMatrix);
          mMatrix.mapPoints(points);
          mTopCard.setPivotX(points[0]);
          mTopCard.setPivotY(points[1]);
          return true;
        }
    }

    return false;
  }
Example #15
0
  private boolean processMoveEvent(MotionEvent ev) {
    if (pointerId != INVALID_POINTER_ID) {
      final int pointerIndex = ev.findPointerIndex(pointerId);

      // Translate touch position to center of view
      float x = ev.getX(pointerIndex);
      touchX = x - cX - offsetX;
      float y = ev.getY(pointerIndex);
      touchY = y - cY - offsetY;

      //        	Log.d(TAG, String.format("ACTION_MOVE: (%03.0f, %03.0f) => (%03.0f, %03.0f)", x, y,
      // touchX, touchY));

      reportOnMoved();
      invalidate();

      touchPressure = ev.getPressure(pointerIndex);
      reportOnPressure();

      return true;
    }
    return false;
  }
  @Override
  public boolean onInterceptTouchEvent(MotionEvent ev) {
    /*
     * This method JUST determines whether we want to intercept the motion.
     * If we return true, onMotionEvent will be called and we do the actual
     * scrolling there.
     */

    /*
     * Shortcut the most recurring case: the user is in the dragging state
     * and he is moving his finger. We want to intercept this motion.
     */
    final int action = ev.getAction();
    if ((action == MotionEvent.ACTION_MOVE) && (mIsBeingDragged)) {
      return true;
    }

    switch (action & MotionEvent.ACTION_MASK) {
      case MotionEvent.ACTION_MOVE:
        {
          /*
           * mIsBeingDragged == false, otherwise the shortcut would have
           * caught it. Check whether the user has moved far enough from his
           * original down touch.
           */

          /*
           * Locally do absolute value. mLastMotionY is set to the y value of
           * the down event.
           */
          final int activePointerId = mActivePointerId;
          if (activePointerId == INVALID_POINTER) {
            // If we don't have a valid id, the touch down wasn't on
            // content.
            break;
          }

          final int pointerIndex = ev.findPointerIndex(activePointerId);
          final float y = ev.getY(pointerIndex);
          final int yDiff = (int) Math.abs(y - mLastMotionY);
          if (yDiff > mTouchSlop) {
            mIsBeingDragged = true;
            mLastMotionY = y;
          }
          break;
        }

      case MotionEvent.ACTION_DOWN:
        {
          final float y = ev.getY();
          if (!inChild((int) ev.getX(), (int) y)) {
            mIsBeingDragged = false;
            break;
          }

          /*
           * Remember location of down touch. ACTION_DOWN always refers to
           * pointer index 0.
           */
          mLastMotionY = y;
          mActivePointerId = ev.getPointerId(0);

          /*
           * If being flinged and user touches the screen, initiate drag;
           * otherwise don't. mScroller.isFinished should be false when being
           * flinged.
           */
          mIsBeingDragged = !mScroller.isFinished();
          break;
        }

      case MotionEvent.ACTION_CANCEL:
      case MotionEvent.ACTION_UP:
        /* Release the drag */
        mIsBeingDragged = false;
        mActivePointerId = INVALID_POINTER;
        break;
      case MotionEvent.ACTION_POINTER_UP:
        onSecondaryPointerUp(ev);
        break;
    }

    /*
     * The only time we want to intercept motion events is if we are in the
     * drag mode.
     */
    return mIsBeingDragged;
  }
  @Override
  public boolean onTouchEvent(MotionEvent event) {
    if (mTopCard == null) {
      return false;
    }
    if (mGestureDetector.onTouchEvent(event)) {
      return true;
    }
    // Log.d("Touch Event", MotionEvent.actionToString(event.getActionMasked()) + " ");
    final int pointerIndex;
    final float x, y;
    final float dx, dy;
    switch (event.getActionMasked()) {
      case MotionEvent.ACTION_DOWN:
        mTopCard.getHitRect(childRect);

        pointerIndex = event.getActionIndex();
        x = event.getX(pointerIndex);
        y = event.getY(pointerIndex);

        if (!childRect.contains((int) x, (int) y)) {
          return false;
        }
        mLastTouchX = x;
        mLastTouchY = y;
        mActivePointerId = event.getPointerId(pointerIndex);

        float[] points = new float[] {x - mTopCard.getLeft(), y - mTopCard.getTop()};
        mTopCard.getMatrix().invert(mMatrix);
        mMatrix.mapPoints(points);
        mTopCard.setPivotX(points[0]);
        mTopCard.setPivotY(points[1]);

        break;
      case MotionEvent.ACTION_MOVE:
        pointerIndex = event.findPointerIndex(mActivePointerId);
        x = event.getX(pointerIndex);
        y = event.getY(pointerIndex);

        dx = x - mLastTouchX;
        dy = y - mLastTouchY;

        if (Math.abs(dx) > mTouchSlop || Math.abs(dy) > mTouchSlop) {
          mDragging = true;
        }

        if (!mDragging) {
          return true;
        }

        mTopCard.setTranslationX(mTopCard.getTranslationX() + dx);
        mTopCard.setTranslationY(mTopCard.getTranslationY() + dy);

        mTopCard.setRotation(40 * mTopCard.getTranslationX() / (getWidth() / 2.f));

        mLastTouchX = x;
        mLastTouchY = y;
        break;
      case MotionEvent.ACTION_UP:
      case MotionEvent.ACTION_CANCEL:
        if (!mDragging) {
          return true;
        }
        mDragging = false;
        mActivePointerId = INVALID_POINTER_ID;
        ValueAnimator animator =
            ObjectAnimator.ofPropertyValuesHolder(
                    mTopCard,
                    PropertyValuesHolder.ofFloat("translationX", 0),
                    PropertyValuesHolder.ofFloat("translationY", 0),
                    PropertyValuesHolder.ofFloat(
                        "rotation",
                        (float)
                            Math.toDegrees(
                                mRandom.nextGaussian() * DISORDERED_MAX_ROTATION_RADIANS)),
                    PropertyValuesHolder.ofFloat("pivotX", mTopCard.getWidth() / 2.f),
                    PropertyValuesHolder.ofFloat("pivotY", mTopCard.getHeight() / 2.f))
                .setDuration(250);
        animator.setInterpolator(new AccelerateInterpolator());
        animator.start();
        break;
      case MotionEvent.ACTION_POINTER_UP:
        pointerIndex = event.getActionIndex();
        final int pointerId = event.getPointerId(pointerIndex);

        if (pointerId == mActivePointerId) {
          final int newPointerIndex = pointerIndex == 0 ? 1 : 0;
          mLastTouchX = event.getX(newPointerIndex);
          mLastTouchY = event.getY(newPointerIndex);

          mActivePointerId = event.getPointerId(newPointerIndex);
        }
        break;
    }

    return true;
  }
  @Override
  public boolean onTouchEvent(MotionEvent event) {
    if (mSkipCallingOnTouchListener) {
      return super.onTouchEvent(event);
    }

    if (mOnTouchListener instanceof SwipeOnTouchListener) {
      if (((SwipeOnTouchListener) mOnTouchListener).isSwiping()) {
        mSkipCallingOnTouchListener = true;
        boolean retVal = mOnTouchListener.onTouch(this, event);
        mSkipCallingOnTouchListener = false;
        return retVal || super.onTouchEvent(event);
      }
    }

    switch (event.getAction() & MotionEvent.ACTION_MASK) {
      case MotionEvent.ACTION_DOWN:
        mDownX = (int) event.getX();
        mDownY = (int) event.getY();
        mActivePointerId = event.getPointerId(0);

        mDynamicTouchChildTouched = false;
        if (mResIdOfDynamicTouchChild != 0) {
          mIsParentHorizontalScrollContainer = false;

          int position = pointToPosition(mDownX, mDownY);
          int childNum = (position != INVALID_POSITION) ? position - getFirstVisiblePosition() : -1;
          View itemView = (childNum >= 0) ? getChildAt(childNum) : null;
          View childView =
              (itemView != null) ? itemView.findViewById(mResIdOfDynamicTouchChild) : null;
          if (childView != null) {
            final Rect childRect = getChildViewRect(this, childView);
            if (childRect.contains(mDownX, mDownY)) {
              mDynamicTouchChildTouched = true;
              getParent().requestDisallowInterceptTouchEvent(true);
            }
          }
        }

        if (mIsParentHorizontalScrollContainer) {
          // Do it now and don't wait until the user moves more than the
          // slop factor.
          getParent().requestDisallowInterceptTouchEvent(true);
        }
        break;
      case MotionEvent.ACTION_MOVE:
        if (mActivePointerId == INVALID_POINTER_ID) {
          break;
        }

        int pointerIndex = event.findPointerIndex(mActivePointerId);

        mLastEventY = (int) event.getY(pointerIndex);
        mLastEventX = (int) event.getX(pointerIndex);
        int deltaY = mLastEventY - mDownY;
        int deltaX = mLastEventX - mDownX;

        if (!mCellIsMobile && mDynamicTouchChildTouched) {
          if (Math.abs(deltaY) > mSlop && Math.abs(deltaY) > Math.abs(deltaX)) {
            makeCellMobile();

            // Cancel ListView's touch (un-highlighting the item)
            MotionEvent cancelEvent = MotionEvent.obtain(event);
            cancelEvent.setAction(
                MotionEvent.ACTION_CANCEL
                    | (event.getActionIndex() << MotionEvent.ACTION_POINTER_INDEX_SHIFT));
            super.onTouchEvent(cancelEvent);
            cancelEvent.recycle();
          }
        }

        if (mCellIsMobile) {
          mHoverCellCurrentBounds.offsetTo(
              mHoverCellOriginalBounds.left, mHoverCellOriginalBounds.top + deltaY + mTotalOffset);
          mHoverCell.setBounds(mHoverCellCurrentBounds);
          invalidate();

          handleCellSwitch();

          mIsMobileScrolling = false;
          handleMobileCellScroll();
        }
        break;
      case MotionEvent.ACTION_UP:
        mDynamicTouchChildTouched = false;
        touchEventsEnded();
        break;
      case MotionEvent.ACTION_CANCEL:
        mDynamicTouchChildTouched = false;
        touchEventsCancelled();
        break;
      case MotionEvent.ACTION_POINTER_UP:
        /*
         * If a multitouch event took place and the original touch dictating
         * the movement of the hover cell has ended, then the dragging event
         * ends and the hover cell is animated to its corresponding position
         * in the listview.
         */
        pointerIndex =
            (event.getAction() & MotionEvent.ACTION_POINTER_INDEX_MASK)
                >> MotionEvent.ACTION_POINTER_INDEX_SHIFT;
        final int pointerId = event.getPointerId(pointerIndex);
        if (pointerId == mActivePointerId) {
          mDynamicTouchChildTouched = false;
          touchEventsEnded();
        }
        break;
      default:
        break;
    }

    if (mCellIsMobile) {
      return false;
    } else if (mOnTouchListener != null) {
      mSkipCallingOnTouchListener = true;
      boolean retVal = mOnTouchListener.onTouch(this, event);
      mSkipCallingOnTouchListener = false;
      if (retVal) {
        return true;
      }
    }
    return super.onTouchEvent(event);
  }
  @Override
  public boolean onTouchEvent(MotionEvent ev) {

    final int action = ev.getAction();

    if (!isEnabled()) {
      if (!mScroller.isFinished()) {
        mScroller.abortAnimation();
      }
      snapToScreen(mCurrentScreen);
      return false; // We don't want the events. Let them fall through to the all apps view.
    }

    acquireVelocityTrackerAndAddMovement(ev);

    switch (action & MotionEvent.ACTION_MASK) {
      case MotionEvent.ACTION_DOWN:
        if (!mScroller.isFinished()) {
          mScroller.abortAnimation();
        }

        // Remember where the motion event started
        mLastMotionX = ev.getX();
        mActivePointerId = ev.getPointerId(0);
        if (mTouchState == TOUCH_STATE_SCROLLING) {
          enableChildrenCache(mCurrentScreen - 1, mCurrentScreen + 1);
        }
        break;
      case MotionEvent.ACTION_MOVE:
        if (mTouchState == TOUCH_STATE_SCROLLING) {
          final int pointerIndex = ev.findPointerIndex(mActivePointerId);
          final float x = ev.getX(pointerIndex);
          final float deltaX = mLastMotionX - x;
          final int mode = mOverScrollMode;

          // if ( !( Math.abs( deltaX ) > mTouchSlop ) ) {
          //	break;
          // }

          mLastMotionX = x;

          if (deltaX < 0) {
            mTouchX += deltaX;
            mSmoothingTime = System.nanoTime() / NANOTIME_DIV;

            if (mTouchX < 0 && mode != OVER_SCROLL_NEVER) {
              mTouchX = 0;
              mLastMotionX = mTouchX;

              if (mEdgeGlowLeft != null) {
                float overscroll = ((float) -deltaX * 2) / getWidth();
                mEdgeGlowLeft.onPull(overscroll);
                if (!mEdgeGlowRight.isFinished()) {
                  mEdgeGlowRight.onRelease();
                }
              }
            }

            invalidate();

          } else if (deltaX > 0) {
            final int totalWidth = getScreenScrollPositionX(mItemCount - 1);
            final float availableToScroll = getScreenScrollPositionX(mItemCount) - mTouchX;
            mSmoothingTime = System.nanoTime() / NANOTIME_DIV;

            mTouchX += Math.min(availableToScroll, deltaX);

            if (availableToScroll <= getWidth() && mode != OVER_SCROLL_NEVER) {
              mTouchX = totalWidth;
              mLastMotionX = totalWidth;

              if (mEdgeGlowLeft != null) {
                float overscroll = ((float) deltaX * 2) / getWidth();
                mEdgeGlowRight.onPull(overscroll);
                if (!mEdgeGlowLeft.isFinished()) {
                  mEdgeGlowLeft.onRelease();
                }
              }
            }
            invalidate();

          } else {
            awakenScrollBars();
          }
        }
        break;
      case MotionEvent.ACTION_UP:
        if (mTouchState == TOUCH_STATE_SCROLLING) {
          final VelocityTracker velocityTracker = mVelocityTracker;
          velocityTracker.computeCurrentVelocity(1000, mMaximumVelocity);
          final int velocityX = (int) velocityTracker.getXVelocity(mActivePointerId);

          final int screenWidth = getWidth();
          final int whichScreen = (getScrollX() + (screenWidth / 2)) / screenWidth;
          final float scrolledPos = (float) getScrollX() / screenWidth;

          if (velocityX > SNAP_VELOCITY && mCurrentScreen > 0) {
            // Fling hard enough to move left.
            // Don't fling across more than one screen at a time.
            final int bound = scrolledPos < whichScreen ? mCurrentScreen - 1 : mCurrentScreen;
            snapToScreen(Math.min(whichScreen, bound), velocityX, true);
          } else if (velocityX < -SNAP_VELOCITY && mCurrentScreen < mItemCount - 1) {
            // Fling hard enough to move right
            // Don't fling across more than one screen at a time.
            final int bound = scrolledPos > whichScreen ? mCurrentScreen + 1 : mCurrentScreen;
            snapToScreen(Math.max(whichScreen, bound), velocityX, true);
          } else {
            snapToScreen(whichScreen, 0, true);
          }

          if (mEdgeGlowLeft != null) {
            mEdgeGlowLeft.onRelease();
            mEdgeGlowRight.onRelease();
          }
        }
        mTouchState = TOUCH_STATE_REST;
        mActivePointerId = INVALID_POINTER;
        releaseVelocityTracker();
        break;
      case MotionEvent.ACTION_CANCEL:
        if (mTouchState == TOUCH_STATE_SCROLLING) {
          final int screenWidth = getWidth();
          final int whichScreen = (getScrollX() + (screenWidth / 2)) / screenWidth;
          snapToScreen(whichScreen, 0, true);
        }
        mTouchState = TOUCH_STATE_REST;
        mActivePointerId = INVALID_POINTER;
        releaseVelocityTracker();

        if (mEdgeGlowLeft != null) {
          mEdgeGlowLeft.onRelease();
          mEdgeGlowRight.onRelease();
        }

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

    return true;
  }
  private boolean interceptTouchEvent(MotionEvent ev) {
    /*
     * This method JUST determines whether we want to intercept the motion. If we
     * return true, onMotionEvent will be called and we do the actual scrolling there.
     */

    /*
     * Shortcut the most recurring case: the user is in the dragging state and he is
     * moving his finger. We want to intercept this motion.
     */
    final int action = ev.getAction();
    if ((action == MotionEvent.ACTION_MOVE) && (mIsBeingDragged)) {
      return true;
    }

    switch (action & MotionEvent.ACTION_MASK) {
      case MotionEvent.ACTION_MOVE:
        {
          /*
           * mIsBeingDragged == false, otherwise the shortcut would have caught it.
           * Check whether the user has moved far enough from his original down touch.
           */

          /*
           * Locally do absolute value. mLastMotionX is set to the x value of the down
           * event.
           */
          final int activePointerId = mActivePointerId;
          if (activePointerId == INVALID_POINTER) {
            // If we don't have a valid id, the touch down wasn't on content.
            break;
          }

          final int pointerIndex = ev.findPointerIndex(activePointerId);
          if (pointerIndex == -1) {
            Log.e(TAG, "Invalid pointerId=" + activePointerId + " in onInterceptTouchEvent");
            break;
          }

          final int x = (int) ev.getX(pointerIndex);
          final int xDiff = (int) Math.abs(x - mLastMotionX);
          if (xDiff > mTouchSlop) {
            setHorizontalScrollBarEnabled(true);
            mIsBeingDragged = true;
            mLastMotionX = x;
            initVelocityTrackerIfNotExists();
            mVelocityTracker.addMovement(ev);
            if (getParent() != null) getParent().requestDisallowInterceptTouchEvent(true);
          }
          break;
        }

      case MotionEvent.ACTION_DOWN:
        {
          final int x = (int) ev.getX();
          if (!inChild((int) x, (int) ev.getY())) {
            mIsBeingDragged = false;
            recycleVelocityTracker();
            break;
          }

          /*
           * Remember location of down touch. ACTION_DOWN always refers to pointer index
           * 0.
           */
          mLastMotionX = x;
          mActivePointerId = ev.getPointerId(0);

          initOrResetVelocityTracker();
          mVelocityTracker.addMovement(ev);

          /*
           * If being flinged and user touches the screen, initiate drag; otherwise
           * don't. mScroller.isFinished should be false when being flinged.
           */
          mIsBeingDragged = false;
          break;
        }

      case MotionEvent.ACTION_CANCEL:
      case MotionEvent.ACTION_UP:
        /* Release the drag */
        mIsBeingDragged = false;
        mActivePointerId = INVALID_POINTER;
        if (mScroller.springBack(getScrollX(), getScrollY(), 0, getScrollRange(), 0, 0)) {
          postInvalidateOnAnimation();
        }
        break;
      case MotionEvent.ACTION_POINTER_DOWN:
        {
          final int index = ev.getActionIndex();
          mLastMotionX = (int) ev.getX(index);
          mActivePointerId = ev.getPointerId(index);
          break;
        }
      case MotionEvent.ACTION_POINTER_UP:
        onSecondaryPointerUp(ev);
        mLastMotionX = (int) ev.getX(ev.findPointerIndex(mActivePointerId));
        break;
    }

    /*
     * The only time we want to intercept motion events is if we are in the drag mode.
     */
    return mIsBeingDragged;
  }
Example #21
0
  @Override
  public boolean onTouchEvent(MotionEvent ev) {
    // Let the ScaleGestureDetector inspect all events.
    mScaleDetector.onTouchEvent(ev);

    final int action = ev.getAction();
    switch (action & MotionEvent.ACTION_MASK) {
      case MotionEvent.ACTION_DOWN:
        {
          final float x = ev.getX();
          final float y = ev.getY();

          mLastTouchX = x;
          mLastTouchY = y;
          mActivePointerId = ev.getPointerId(0);
          break;
        }

      case MotionEvent.ACTION_MOVE:
        {
          final int pointerIndex = ev.findPointerIndex(mActivePointerId);
          final float x = ev.getX(pointerIndex);
          final float y = ev.getY(pointerIndex);

          // Only move if the ScaleGestureDetector isn't processing a gesture.
          if (!mScaleDetector.isInProgress()) {
            final float dx = x - mLastTouchX;
            final float dy = y - mLastTouchY;

            mPosX += dx;
            mPosY += dy;

            invalidate();
          }

          mLastTouchX = x;
          mLastTouchY = y;

          break;
        }

      case MotionEvent.ACTION_UP:
        {
          mActivePointerId = INVALID_POINTER_ID;
          break;
        }

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

      case MotionEvent.ACTION_POINTER_UP:
        {
          final int pointerIndex =
              (ev.getAction() & MotionEvent.ACTION_POINTER_INDEX_MASK)
                  >> MotionEvent.ACTION_POINTER_INDEX_SHIFT;
          final int pointerId = ev.getPointerId(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;
            mLastTouchX = ev.getX(newPointerIndex);
            mLastTouchY = ev.getY(newPointerIndex);
            mActivePointerId = ev.getPointerId(newPointerIndex);
          }
          break;
        }
    }

    return true;
  }
  /** Handles thumb selection and movement. Notifies listener callback on certain events. */
  @Override
  public boolean onTouchEvent(@NonNull MotionEvent event) {

    if (!isEnabled()) {
      return false;
    }

    int pointerIndex;

    final int action = event.getAction();
    switch (action & MotionEvent.ACTION_MASK) {
      case MotionEvent.ACTION_DOWN:
        // Remember where the motion event started
        mActivePointerId = event.getPointerId(event.getPointerCount() - 1);
        pointerIndex = event.findPointerIndex(mActivePointerId);
        mDownMotionX = event.getX(pointerIndex);
        final float downMotionY = event.getY(pointerIndex);

        pressedThumb = evalPressedThumb(mDownMotionX, downMotionY);

        // Only handle thumb presses.
        if (pressedThumb == null) {
          return super.onTouchEvent(event);
        }

        setPressed(true);
        invalidate();
        onStartTrackingTouch();
        trackTouchEvent(event);
        attemptClaimDrag();
        updateTouchPosition(pressedThumb.equals(Thumb.MIN));
        listener.onTouchStart(
            touchPositionX,
            touchPositionY,
            getSelectedMinValue(),
            getSelectedMaxValue(),
            pressedThumb.equals(Thumb.MIN));

        break;
      case MotionEvent.ACTION_MOVE:
        if (pressedThumb != null) {

          if (mIsDragging) {
            trackTouchEvent(event);
          } else {
            // Scroll to follow the motion event
            pointerIndex = event.findPointerIndex(mActivePointerId);
            final float x = event.getX(pointerIndex);

            if (Math.abs(x - mDownMotionX) > mScaledTouchSlop) {
              setPressed(true);
              invalidate();
              onStartTrackingTouch();
              trackTouchEvent(event);
              attemptClaimDrag();
            }
          }

          if (notifyWhileDragging && listener != null) {
            updateTouchPosition(pressedThumb.equals(Thumb.MIN));
            listener.onRangeSeekBarValuesChanged(
                this,
                getSelectedMinValue(),
                getSelectedMaxValue(),
                touchPositionX,
                touchPositionY,
                pressedThumb.equals(Thumb.MIN));
          }
        }
        break;
      case MotionEvent.ACTION_UP:
        if (mIsDragging) {
          trackTouchEvent(event);
          onStopTrackingTouch();
          setPressed(false);
        } else {
          // Touch up when we never crossed the touch slop threshold
          // should be interpreted as a tap-seek to that location.
          onStartTrackingTouch();
          trackTouchEvent(event);
          onStopTrackingTouch();
        }

        pressedThumb = null;
        invalidate();
        if (listener != null) {
          listener.onTouchLeave();
        }
        break;
      case MotionEvent.ACTION_POINTER_DOWN:
        {
          final int index = event.getPointerCount() - 1;
          // final int index = ev.getActionIndex();
          mDownMotionX = event.getX(index);
          mActivePointerId = event.getPointerId(index);
          invalidate();
          break;
        }
      case MotionEvent.ACTION_POINTER_UP:
        onSecondaryPointerUp(event);
        invalidate();
        break;
      case MotionEvent.ACTION_CANCEL:
        if (mIsDragging) {
          onStopTrackingTouch();
          setPressed(false);
        }
        invalidate(); // see above explanation
        break;
    }
    return true;
  }
Example #23
0
  @Override
  public boolean onTouchEvent(MotionEvent paramMotionEvent) {
    int action = 0xFF & paramMotionEvent.getAction();
    switch (action) {
      case MotionEvent.ACTION_OUTSIDE:
      case MotionEvent.ACTION_DOWN:
        this.mLastMotionY = paramMotionEvent.getY();
        this.mActivePointerId = paramMotionEvent.getPointerId(0);
        this.mLastHeight = this.mHeaderContainer.getBottom();
        Logger.v(TAG, "ACTION_DOWN");
        break;
      case MotionEvent.ACTION_UP:
        reset();

        Logger.v(TAG, "ACTION_UP");
        break;
      case MotionEvent.ACTION_MOVE:
        int firstItme = getFirstVisiblePosition();
        if (firstItme != 0) {
          break;
        }
        Logger.d("mmm", "mActivePointerId" + mActivePointerId);
        int j = paramMotionEvent.findPointerIndex(this.mActivePointerId);
        if (j == -1) {
          Logger.e(
              "PullToZoomListView2",
              "Invalid pointerId=" + this.mActivePointerId + " in onTouchEvent");
        } else {
          if (mLastMotionY == -1.0f) this.mLastMotionY = paramMotionEvent.getY(j);

          if (this.mHeaderContainer.getBottom() >= 0) {
            ViewGroup.LayoutParams localLayoutParams = this.mHeaderImage.getLayoutParams();
            float f = (paramMotionEvent.getY(j) - mLastMotionY);
            Logger.v(
                TAG + "::paramMotionEvent:",
                paramMotionEvent.getY(j) + "--mLastMotionY:" + mLastMotionY);
            localLayoutParams.height += (int) f;
            if (localLayoutParams.height <= 0) {
              localLayoutParams.height = 0;
              return super.onTouchEvent(paramMotionEvent);
            }
            if (localLayoutParams.height >= mHeaderMaxHeight) {
              localLayoutParams.height = mHeaderMaxHeight;
              return super.onTouchEvent(paramMotionEvent);
            }
            this.mHeaderImage.setLayoutParams(localLayoutParams);
          }
          this.mLastMotionY = paramMotionEvent.getY(j);
          return true;
        }
        Logger.v(TAG, "ACTION_MOVE");
        break;
      case MotionEvent.ACTION_POINTER_DOWN:
        try {
          this.mLastMotionY =
              paramMotionEvent.getY(paramMotionEvent.findPointerIndex(this.mActivePointerId));
          Logger.v(TAG, "ACTION_POINTER_DOWN");
        } catch (IllegalArgumentException e) {
          Logger.e(TAG, e.getMessage());
        }
        break;
      case MotionEvent.ACTION_CANCEL:
        int i = paramMotionEvent.getActionIndex();
        this.mLastMotionY = paramMotionEvent.getY(i);
        this.mActivePointerId = paramMotionEvent.getPointerId(i);
        break;
    }

    return super.onTouchEvent(paramMotionEvent);
  }
  @Override
  public boolean handleTouchEvent(MotionEvent ev, float scaleFactor, int xPos, int yPos) {

    mScaleDetector.onTouchEvent(ev);
    mGestureDetector.onTouchEvent(ev);

    final int action = ev.getAction();
    switch (action) {
      case MotionEvent.ACTION_DOWN:
        {
          final float x = ev.getX();
          final float y = ev.getY();

          // Remember where we started
          mLastTouchX = x;
          mLastTouchY = y;
          mActivePointerId = ev.getPointerId(0);

          break;
        }
      case MotionEvent.ACTION_MOVE:
        {
          final int pointerIndex = ev.findPointerIndex(mActivePointerId);

          if (pointerIndex < 0) return false;

          final float x = ev.getX(pointerIndex);
          final float y = ev.getY(pointerIndex);

          if (!mScaleDetector.isInProgress()) {
            // Calculate the distance moved
            final float dx = x - mLastTouchX;
            final float dy = y - mLastTouchY;

            // Move the object
            pan(dx, dy, scaleFactor, xPos, yPos);

            // Remember this touch position for the next move event
            mLastTouchX = x;
            mLastTouchY = y;
          }
          break;
        }
      case MotionEvent.ACTION_UP:
        {
          mActivePointerId = INVALID_POINTER_ID;
          break;
        }
      case MotionEvent.ACTION_CANCEL:
        {
          mActivePointerId = INVALID_POINTER_ID;
          break;
        }
      case MotionEvent.ACTION_POINTER_UP:
        {
          // Extract the index of the pointer that left the touch sensor
          final int pointerIndex =
              (action & MotionEvent.ACTION_POINTER_INDEX_MASK)
                  >> MotionEvent.ACTION_POINTER_INDEX_SHIFT;
          final int pointerId = ev.getPointerId(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;
            mLastTouchX = ev.getX(newPointerIndex);
            mLastTouchY = ev.getY(newPointerIndex);
            mActivePointerId = ev.getPointerId(newPointerIndex);
          }
          break;
        }
    } // and switch statement
    return true;
  }
Example #25
0
  private void handleMove(MotionEvent event) {
    int activeTarget = -1;
    final int historySize = event.getHistorySize();
    ArrayList<TargetDrawable> targets = mTargetDrawables;
    int ntargets = targets.size();
    float x = 0.0f;
    float y = 0.0f;
    int actionIndex = event.findPointerIndex(mPointerId);

    if (actionIndex == -1) {
      return; // no data for this pointer
    }

    for (int k = 0; k < historySize + 1; k++) {
      float eventX =
          k < historySize ? event.getHistoricalX(actionIndex, k) : event.getX(actionIndex);
      float eventY =
          k < historySize ? event.getHistoricalY(actionIndex, k) : event.getY(actionIndex);
      // tx and ty are relative to wave center
      float tx = eventX - mWaveCenterX;
      float ty = eventY - mWaveCenterY;
      float touchRadius = (float) Math.sqrt(dist2(tx, ty));
      final float scale = touchRadius > mOuterRadius ? mOuterRadius / touchRadius : 1.0f;
      float limitX = tx * scale;
      float limitY = ty * scale;
      double angleRad = Math.atan2(-ty, tx);

      if (!mDragging) {
        trySwitchToFirstTouchState(eventX, eventY);
      }

      if (mDragging) {
        // For multiple targets, snap to the one that matches
        final float snapRadius = mRingScaleFactor * mOuterRadius - mSnapMargin;
        final float snapDistance2 = snapRadius * snapRadius;
        // Find first target in range
        for (int i = 0; i < ntargets; i++) {
          TargetDrawable target = targets.get(i);

          double targetMinRad = (i - 0.5) * 2 * Math.PI / ntargets;
          double targetMaxRad = (i + 0.5) * 2 * Math.PI / ntargets;
          if (target.isEnabled()) {
            boolean angleMatches =
                (angleRad > targetMinRad && angleRad <= targetMaxRad)
                    || (angleRad + 2 * Math.PI > targetMinRad
                        && angleRad + 2 * Math.PI <= targetMaxRad);
            if (angleMatches && (dist2(tx, ty) > snapDistance2)) {
              activeTarget = i;
            }
          }
        }
      }
      x = limitX;
      y = limitY;
    }

    if (!mDragging) {
      return;
    }

    if (activeTarget != -1) {
      switchToState(STATE_SNAP, x, y);
      updateGlowPosition(x, y);
    } else {
      switchToState(STATE_TRACKING, x, y);
      updateGlowPosition(x, y);
    }

    if (mActiveTarget != activeTarget) {
      // Defocus the old target
      if (mActiveTarget != -1) {
        TargetDrawable target = targets.get(mActiveTarget);
        target.setState(TargetDrawable.STATE_INACTIVE);
      }
      // Focus the new target
      if (activeTarget != -1) {
        TargetDrawable target = targets.get(activeTarget);
        target.setState(TargetDrawable.STATE_FOCUSED);
        final AccessibilityManager accessibilityManager =
            (AccessibilityManager) getContext().getSystemService(Context.ACCESSIBILITY_SERVICE);
        if (accessibilityManager.isEnabled()) {
          String targetContentDescription = getTargetDescription(activeTarget);
          announceForAccessibility(targetContentDescription);
        }
      }
    }
    mActiveTarget = activeTarget;
  }
Example #26
0
 @Override
 public boolean onTouchEvent(MotionEvent paramMotionEvent) {
   Log.d("mmm", "" + (0xFF & paramMotionEvent.getAction()));
   switch (0xFF & paramMotionEvent.getAction()) {
     case 4:
     case 0:
       if (!this.mScalingRunnalable.mIsFinished) {
         this.mScalingRunnalable.abortAnimation();
       }
       this.mLastMotionY = paramMotionEvent.getY();
       this.mActivePointerId = paramMotionEvent.getPointerId(0);
       this.mMaxScale = (this.mScreenHeight / this.mHeaderHeight);
       this.mLastScale = (this.mHeaderContainer.getBottom() / this.mHeaderHeight);
       break;
     case 2:
       Log.d("mmm", "mActivePointerId" + mActivePointerId);
       int j = paramMotionEvent.findPointerIndex(this.mActivePointerId);
       if (j == -1) {
         Log.e(
             "PullToZoomListView",
             "Invalid pointerId=" + this.mActivePointerId + " in onTouchEvent");
       } else {
         if (this.mLastMotionY == -1.0F) this.mLastMotionY = paramMotionEvent.getY(j);
         if (this.mHeaderContainer.getBottom() >= this.mHeaderHeight) {
           ViewGroup.LayoutParams localLayoutParams = this.mHeaderContainer.getLayoutParams();
           float f =
               ((paramMotionEvent.getY(j) - this.mLastMotionY + this.mHeaderContainer.getBottom())
                               / this.mHeaderHeight
                           - this.mLastScale)
                       / 2.0F
                   + this.mLastScale;
           if ((this.mLastScale <= 1.0D) && (f < this.mLastScale)) {
             localLayoutParams.height = this.mHeaderHeight;
             this.mHeaderContainer.setLayoutParams(localLayoutParams);
             return super.onTouchEvent(paramMotionEvent);
           }
           this.mLastScale = Math.min(Math.max(f, 1.0F), this.mMaxScale);
           localLayoutParams.height = ((int) (this.mHeaderHeight * this.mLastScale));
           if (localLayoutParams.height < this.mScreenHeight)
             this.mHeaderContainer.setLayoutParams(localLayoutParams);
           this.mLastMotionY = paramMotionEvent.getY(j);
           return true;
         }
         this.mLastMotionY = paramMotionEvent.getY(j);
       }
       break;
     case 1:
       reset();
       endScraling();
       break;
     case 3:
       int i = paramMotionEvent.getActionIndex();
       this.mLastMotionY = paramMotionEvent.getY(i);
       this.mActivePointerId = paramMotionEvent.getPointerId(i);
       break;
     case 5:
       onSecondaryPointerUp(paramMotionEvent);
       this.mLastMotionY =
           paramMotionEvent.getY(paramMotionEvent.findPointerIndex(this.mActivePointerId));
       break;
     case 6:
   }
   return super.onTouchEvent(paramMotionEvent);
 }
  @Override
  public boolean onTouchEvent(MotionEvent ev) {

    if (ev.getAction() == MotionEvent.ACTION_DOWN && ev.getEdgeFlags() != 0) {
      // Don't handle edge touches immediately -- they may actually belong
      // to one of our
      // descendants.
      return false;
    }

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

    final int action = ev.getAction();

    switch (action & MotionEvent.ACTION_MASK) {
      case MotionEvent.ACTION_DOWN:
        {
          final float y = ev.getY();
          if (!(mIsBeingDragged = inChild((int) ev.getX(), (int) y))) {
            return false;
          }

          /*
           * If being flinged and user touches, stop the fling. isFinished
           * will be false if being flinged.
           */
          if (!mScroller.isFinished()) {
            mScroller.abortAnimation();
          }

          // Remember where the motion event started
          mLastMotionY = y;
          mActivePointerId = ev.getPointerId(0);
          break;
        }
      case MotionEvent.ACTION_MOVE:
        if (mIsBeingDragged) {
          // Scroll to follow the motion event
          final int activePointerIndex = ev.findPointerIndex(mActivePointerId);
          final float y = ev.getY(activePointerIndex);
          final int deltaY = (int) (mLastMotionY - y);
          mLastMotionY = y;

          if (isOverScrolled()) {
            // when overscrolling, move the scroller just half of the
            // finger movement, to make it feel like a spring...
            scrollBy(0, deltaY / 2);
          } else {
            scrollBy(0, deltaY);
          }
        }
        break;
      case MotionEvent.ACTION_UP:
        if (mIsBeingDragged) {
          final VelocityTracker velocityTracker = mVelocityTracker;
          velocityTracker.computeCurrentVelocity(1000, mMaximumVelocity);
          int initialVelocity = (int) velocityTracker.getYVelocity(mActivePointerId);

          if (getChildCount() > 0 && Math.abs(initialVelocity) > mMinimumVelocity) {
            fling(-initialVelocity);
          }

          mActivePointerId = INVALID_POINTER;
          mIsBeingDragged = false;

          if (mVelocityTracker != null) {
            mVelocityTracker.recycle();
            mVelocityTracker = null;
          }
        }
        break;
      case MotionEvent.ACTION_CANCEL:
        if (mIsBeingDragged && getChildCount() > 0) {
          mActivePointerId = INVALID_POINTER;
          mIsBeingDragged = false;
          if (mVelocityTracker != null) {
            mVelocityTracker.recycle();
            mVelocityTracker = null;
          }
        }
        break;
      case MotionEvent.ACTION_POINTER_UP:
        onSecondaryPointerUp(ev);
        break;
    }
    return true;
  }
  @Override
  public boolean onInterceptTouchEvent(MotionEvent ev) {

    final int action = ev.getAction();
    if (!isEnabled()) {
      return false; // We don't want the events. Let them fall through to the all apps view.
    }

    if ((action == MotionEvent.ACTION_MOVE) && (mTouchState != TOUCH_STATE_REST)) {
      return true;
    }

    acquireVelocityTrackerAndAddMovement(ev);

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

          /*
           * Locally do absolute value. mLastMotionX is set to the y value of the down event.
           */
          final int pointerIndex = ev.findPointerIndex(mActivePointerId);
          final float x = ev.getX(pointerIndex);
          final float y = ev.getY(pointerIndex);
          final int xDiff = (int) Math.abs(x - mLastMotionX);
          final int yDiff = (int) Math.abs(y - mLastMotionY);

          final int touchSlop = mTouchSlop;
          boolean xMoved = xDiff > touchSlop;
          boolean yMoved = yDiff > touchSlop;

          if (xMoved || yMoved) {

            if (xMoved) {
              // Scroll if the user moved far enough along the X axis
              mTouchState = TOUCH_STATE_SCROLLING;
              mLastMotionX = x;
              mTouchX = getScrollX();
              mSmoothingTime = System.nanoTime() / NANOTIME_DIV;
              enableChildrenCache(mCurrentScreen - 1, mCurrentScreen + 1);
            }
          }
          break;
        }

      case MotionEvent.ACTION_DOWN:
        {
          final float x = ev.getX();
          final float y = ev.getY();
          // Remember location of down touch
          mLastMotionX = x;
          mLastMotionY = y;
          mActivePointerId = ev.getPointerId(0);
          mAllowLongPress = true;

          mTouchState = mScroller.isFinished() ? TOUCH_STATE_REST : TOUCH_STATE_SCROLLING;
          break;
        }

      case MotionEvent.ACTION_CANCEL:
      case MotionEvent.ACTION_UP:
        // Release the drag
        clearChildrenCache();
        mTouchState = TOUCH_STATE_REST;
        mActivePointerId = INVALID_POINTER;
        mAllowLongPress = false;
        releaseVelocityTracker();
        break;

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

    /*
     * The only time we want to intercept motion events is if we are in the drag mode.
     */
    return mTouchState != TOUCH_STATE_REST;
  }
  private boolean touchEvent(MotionEvent ev) {
    initVelocityTrackerIfNotExists();
    mVelocityTracker.addMovement(ev);

    final int action = ev.getAction();

    switch (action & MotionEvent.ACTION_MASK) {
      case MotionEvent.ACTION_DOWN:
        {
          if (getChildCount() == 0 || !inScrollArea(ev)) {
            return false;
          }
          if ((mIsBeingDragged = !mScroller.isFinished())) {
            final ViewParent parent = getParent();
            if (parent != null) {
              parent.requestDisallowInterceptTouchEvent(true);
            }
          }

          /*
           * If being flinged and user touches, stop the fling. isFinished will be false
           * if being flinged.
           */
          if (!mScroller.isFinished()) {
            mScroller.abortAnimation();
          }

          // Remember where the motion event started
          mLastMotionX = (int) ev.getX();
          mActivePointerId = ev.getPointerId(0);
          break;
        }
      case MotionEvent.ACTION_MOVE:
        final int activePointerIndex = ev.findPointerIndex(mActivePointerId);
        if (activePointerIndex == -1) {
          Log.e(TAG, "Invalid pointerId=" + mActivePointerId + " in onTouchEvent");
          break;
        }

        final int x = (int) ev.getX(activePointerIndex);
        int deltaX = mLastMotionX - x;
        if (!mIsBeingDragged && Math.abs(deltaX) > mTouchSlop) {
          final ViewParent parent = getParent();
          if (parent != null) {
            parent.requestDisallowInterceptTouchEvent(true);
          }
          mIsBeingDragged = true;
          if (deltaX > 0) {
            deltaX -= mTouchSlop;
          } else {
            deltaX += mTouchSlop;
          }
        }
        if (mIsBeingDragged) {
          // Scroll to follow the motion event
          mLastMotionX = x;

          final int oldX = getScrollX();
          final int oldY = getScrollY();
          final int range = getScrollRange();
          final int overscrollMode = getOverScrollMode();
          final boolean canOverscroll =
              overscrollMode == OVER_SCROLL_ALWAYS
                  || (overscrollMode == OVER_SCROLL_IF_CONTENT_SCROLLS && range > 0);

          if (overScrollBy(deltaX, 0, getScrollX(), 0, range, 0, mOverscrollDistance, 0, true)) {
            // Break our velocity if we hit a scroll barrier.
            mVelocityTracker.clear();
          }
          onScrollChanged(getScrollX(), getScrollY(), oldX, oldY);
        }
        break;
      case MotionEvent.ACTION_UP:
        if (mIsBeingDragged) {
          final VelocityTracker velocityTracker = mVelocityTracker;
          velocityTracker.computeCurrentVelocity(1000, mMaximumVelocity);
          int initialVelocity = (int) velocityTracker.getXVelocity(mActivePointerId);

          if (getChildCount() > 0) {
            if ((Math.abs(initialVelocity) > mMinimumVelocity)) {
              fling(-initialVelocity);
            } else {
              int finalX = getScrollX();
              finalX = getScrollXFromFinalX(finalX);
              if (mScroller.springBack(getScrollX(), getScrollY(), finalX, finalX, 0, 0)) {
                postInvalidateOnAnimation();
              }
            }
          }

          mActivePointerId = INVALID_POINTER;
          mIsBeingDragged = false;
          recycleVelocityTracker();
        }
        break;
      case MotionEvent.ACTION_CANCEL:
        if (mIsBeingDragged && getChildCount() > 0) {
          if (mScroller.springBack(getScrollX(), getScrollY(), 0, getScrollRange(), 0, 0)) {
            postInvalidateOnAnimation();
          }
          mActivePointerId = INVALID_POINTER;
          mIsBeingDragged = false;
          recycleVelocityTracker();
        }
        break;
      case MotionEvent.ACTION_POINTER_UP:
        onSecondaryPointerUp(ev);
        break;
    }
    return true;
  }
  @Override
  public boolean onTouchEvent(MotionEvent ev) {
    if (!mMenuVisible && !mIsDragging && mTouchMode == TOUCH_MODE_NONE) {
      return false;
    }
    final int action = ev.getAction() & MotionEvent.ACTION_MASK;

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

    switch (action) {
      case MotionEvent.ACTION_DOWN:
        {
          mLastMotionX = mInitialMotionX = ev.getX();
          mLastMotionY = mInitialMotionY = ev.getY();
          final boolean allowDrag = onDownAllowDrag((int) mLastMotionX, (int) mLastMotionY);

          mActivePointerId = ev.getPointerId(0);

          if (allowDrag) {
            stopAnimation();
            endPeek();
            startLayerTranslation();
          }
          break;
        }

      case MotionEvent.ACTION_MOVE:
        {
          final int pointerIndex = ev.findPointerIndex(mActivePointerId);
          if (pointerIndex == -1) {
            mIsDragging = false;
            mActivePointerId = INVALID_POINTER;
            endDrag();
            closeMenu(true);
            return false;
          }

          if (!mIsDragging) {
            final float x = ev.getX(pointerIndex);
            final float dx = x - mLastMotionX;
            final float y = ev.getY(pointerIndex);
            final float dy = y - mLastMotionY;

            if (checkTouchSlop(dx, dy)) {
              final boolean allowDrag = onMoveAllowDrag((int) x, (int) y, dx, dy);

              if (allowDrag) {
                setDrawerState(STATE_DRAGGING);
                mIsDragging = true;
                mLastMotionX = x;
                mLastMotionY = y;
              } else {
                mInitialMotionX = x;
                mInitialMotionY = y;
              }
            }
          }

          if (mIsDragging) {
            startLayerTranslation();

            final float x = ev.getX(pointerIndex);
            final float dx = x - mLastMotionX;
            final float y = ev.getY(pointerIndex);
            final float dy = y - mLastMotionY;

            mLastMotionX = x;
            mLastMotionY = y;
            onMoveEvent(dx, dy);
          }
          break;
        }

      case MotionEvent.ACTION_CANCEL:
      case MotionEvent.ACTION_UP:
        {
          int index = ev.findPointerIndex(mActivePointerId);
          index = index == -1 ? 0 : index;
          final int x = (int) ev.getX(index);
          final int y = (int) ev.getY(index);
          onUpEvent(x, y);
          mActivePointerId = INVALID_POINTER;
          mIsDragging = false;
          break;
        }

      case MotionEvent.ACTION_POINTER_DOWN:
        final int index =
            (ev.getAction() & MotionEvent.ACTION_POINTER_INDEX_MASK)
                >> MotionEvent.ACTION_POINTER_INDEX_SHIFT;
        mLastMotionX = ev.getX(index);
        mLastMotionY = ev.getY(index);
        mActivePointerId = ev.getPointerId(index);
        break;

      case MotionEvent.ACTION_POINTER_UP:
        onPointerUp(ev);
        mLastMotionX = ev.getX(ev.findPointerIndex(mActivePointerId));
        mLastMotionY = ev.getY(ev.findPointerIndex(mActivePointerId));
        break;
    }

    return true;
  }