<commit_before>package com.lorentzos.flingswipe;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.LinearInterpolator;
import android.view.animation.OvershootInterpolator;

/**
 * Created by dionysis_lorentzos on 5/8/14
 * for package com.lorentzos.swipecards
 * and project Swipe cards.
 * Use with caution dinausaurs might appear!
 */
public class FlingCardListener implements View.OnTouchListener {

    private final float objectX;
    private final float objectY;
    private final int objectH;
    private final int objectW;
    private final int parentWidth;
    private final FlingListener mFlingListener;
    private final Object dataObject;
    private final float halfWidth;
    private float BASE_ROTATION_DEGREES;

    private float aPosX;
    private float aPosY;
    private float aDownTouchX;
    private float aDownTouchY;
    private static final int INVALID_POINTER_ID = -1;

    // The active pointer is the one currently moving our object.
    private int mActivePointerId = INVALID_POINTER_ID;
    private View frame = null;

    private final int TOUCH_ABOVE = 0;
    private final int TOUCH_BELOW = 1;
    private int touchPosition;
    // private final Object obj = new Object();
    private boolean isAnimationRunning = false;
    private float MAX_COS = (float) Math.cos(Math.toRadians(45));
    // 支持左右滑
    private boolean isNeedSwipe = true;

    private float aTouchUpX;

    private int animDuration = 300;
    private float scale;

    public FlingCardListener(View frame, Object itemAtPosition, float rotation_degrees, FlingListener flingListener) {
        super();
        this.frame = frame;
        this.objectX = frame.getX();
        this.objectY = frame.getY();
        this.objectH = frame.getHeight();
        this.objectW = frame.getWidth();
        this.halfWidth = objectW/2f;
        this.dataObject = itemAtPosition;
        this.parentWidth = ((ViewGroup) frame.getParent()).getWidth();
        this.BASE_ROTATION_DEGREES = rotation_degrees;
        this.mFlingListener = flingListener;
    }

    public void setIsNeedSwipe(boolean isNeedSwipe) {
        this.isNeedSwipe = isNeedSwipe;
    }

    public boolean onTouch(View view, MotionEvent event) {
    	
    	try {
	        switch (event.getAction() & MotionEvent.ACTION_MASK) {
	            case MotionEvent.ACTION_DOWN:

	                // Save the ID of this pointer
	                mActivePointerId = event.getPointerId(0);
	                final float x = event.getX(mActivePointerId);
	                final float y = event.getY(mActivePointerId);					
	
	                // Remember where we started
	                aDownTouchX = x;
	                aDownTouchY = y;
	                // to prevent an initial jump of the magnifier, aposX and aPosY must
	                // have the values from the magnifier frame
	                if (aPosX == 0) {
	                    aPosX = frame.getX();
	                }
	                if (aPosY == 0) {
	                    aPosY = frame.getY();
	                }
	
	                if (y < objectH/2) {
	                    touchPosition = TOUCH_ABOVE;
	                } else {
	                    touchPosition = TOUCH_BELOW;
	                }
	                break;

	            case MotionEvent.ACTION_POINTER_DOWN:
	                break;
	
	            case MotionEvent.ACTION_POINTER_UP:
	                // Extract the index of the pointer that left the touch sensor
	                final int pointerIndex = (event.getAction() &
	                        MotionEvent.ACTION_POINTER_INDEX_MASK) >> MotionEvent.ACTION_POINTER_INDEX_SHIFT;
	                final int pointerId = event.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 = event.getPointerId(newPointerIndex);
	                }
	                break;
	            case MotionEvent.ACTION_MOVE:
	
	                // Find the index of the active pointer and fetch its position
	                final int pointerIndexMove = event.findPointerIndex(mActivePointerId);
	                final float xMove = event.getX(pointerIndexMove);
	                final float yMove = event.getY(pointerIndexMove);
	                
	                // from http://android-developers.blogspot.com/2010/06/making-sense-of-multitouch.html
	                // Calculate the distance moved
	                final float dx = xMove - aDownTouchX;
	                final float dy = yMove - aDownTouchY;
	
	                // Move the frame
	                aPosX += dx;
	                aPosY += dy;
	
	                // calculate the rotation degrees
	                float distObjectX = aPosX - objectX;
	                float rotation = BASE_ROTATION_DEGREES * 2f * distObjectX / parentWidth;
	                if (touchPosition == TOUCH_BELOW) {
	                    rotation = -rotation;
	                }
	
	                // in this area would be code for doing something with the view as the frame moves.
                    if (isNeedSwipe) {
                        frame.setX(aPosX);
                        frame.setY(aPosY);
                        frame.setRotation(rotation);
                        mFlingListener.onScroll(getScrollProgress(), getScrollXProgressPercent());
                    }
	                break;

                case MotionEvent.ACTION_UP:
                case MotionEvent.ACTION_CANCEL:
                    //mActivePointerId = INVALID_POINTER_ID;
                    aTouchUpX = event.getX(mActivePointerId);
                    mActivePointerId = INVALID_POINTER_ID;
                    resetCardViewOnStack(event);
	                break;

	        }
    	} catch (Exception e) {
			e.printStackTrace();
		}

        return true;
    }<commit_msg>修复 event.getX(pointerId) 多点触摸时java.lang.IllegalArgumentException:pointerIndex out of range异常<commit_after>package com.lorentzos.flingswipe;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.LinearInterpolator;
import android.view.animation.OvershootInterpolator;

/**
 * Created by dionysis_lorentzos on 5/8/14
 * for package com.lorentzos.swipecards
 * and project Swipe cards.
 * Use with caution dinausaurs might appear!
 */
public class FlingCardListener implements View.OnTouchListener {

    private final float objectX;
    private final float objectY;
    private final int objectH;
    private final int objectW;
    private final int parentWidth;
    private final FlingListener mFlingListener;
    private final Object dataObject;
    private final float halfWidth;
    private float BASE_ROTATION_DEGREES;

    private float aPosX;
    private float aPosY;
    private float aDownTouchX;
    private float aDownTouchY;
    private static final int INVALID_POINTER_ID = -1;

    // The active pointer is the one currently moving our object.
    private int mActivePointerId = INVALID_POINTER_ID;
    private View frame = null;

    private final int TOUCH_ABOVE = 0;
    private final int TOUCH_BELOW = 1;
    private int touchPosition;
    // private final Object obj = new Object();
    private boolean isAnimationRunning = false;
    private float MAX_COS = (float) Math.cos(Math.toRadians(45));
    // 支持左右滑
    private boolean isNeedSwipe = true;

    private float aTouchUpX;

    private int animDuration = 300;
    private float scale;

    public FlingCardListener(View frame, Object itemAtPosition, float rotation_degrees, FlingListener flingListener) {
        super();
        this.frame = frame;
        this.objectX = frame.getX();
        this.objectY = frame.getY();
        this.objectH = frame.getHeight();
        this.objectW = frame.getWidth();
        this.halfWidth = objectW/2f;
        this.dataObject = itemAtPosition;
        this.parentWidth = ((ViewGroup) frame.getParent()).getWidth();
        this.BASE_ROTATION_DEGREES = rotation_degrees;
        this.mFlingListener = flingListener;
    }

    public void setIsNeedSwipe(boolean isNeedSwipe) {
        this.isNeedSwipe = isNeedSwipe;
    }

    public boolean onTouch(View view, MotionEvent event) {
    	
    	try {
	        switch (event.getAction() & MotionEvent.ACTION_MASK) {
	            case MotionEvent.ACTION_DOWN:

	                // Save the ID of this pointer
	                mActivePointerId = event.getPointerId(0);
	                final float x = event.getX(mActivePointerId);
	                final float y = event.getY(mActivePointerId);					
	
	                // Remember where we started
	                aDownTouchX = x;
	                aDownTouchY = y;
	                // to prevent an initial jump of the magnifier, aposX and aPosY must
	                // have the values from the magnifier frame
	                if (aPosX == 0) {
	                    aPosX = frame.getX();
	                }
	                if (aPosY == 0) {
	                    aPosY = frame.getY();
	                }
	
	                if (y < objectH/2) {
	                    touchPosition = TOUCH_ABOVE;
	                } else {
	                    touchPosition = TOUCH_BELOW;
	                }
	                break;

	            case MotionEvent.ACTION_POINTER_DOWN:
	                break;
	
	            case MotionEvent.ACTION_POINTER_UP:
	                // Extract the index of the pointer that left the touch sensor
	                final int pointerIndex = (event.getAction() &
	                        MotionEvent.ACTION_POINTER_INDEX_MASK) >> MotionEvent.ACTION_POINTER_INDEX_SHIFT;
	                final int pointerId = event.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 = event.getPointerId(newPointerIndex);
	                }
	                break;
	            case MotionEvent.ACTION_MOVE:
	
	                // Find the index of the active pointer and fetch its position
	                final int pointerIndexMove = event.findPointerIndex(mActivePointerId);
	                final float xMove = event.getX(pointerIndexMove);
	                final float yMove = event.getY(pointerIndexMove);
	                
	                // from http://android-developers.blogspot.com/2010/06/making-sense-of-multitouch.html
	                // Calculate the distance moved
	                final float dx = xMove - aDownTouchX;
	                final float dy = yMove - aDownTouchY;
	
	                // Move the frame
	                aPosX += dx;
	                aPosY += dy;
	
	                // calculate the rotation degrees
	                float distObjectX = aPosX - objectX;
	                float rotation = BASE_ROTATION_DEGREES * 2f * distObjectX / parentWidth;
	                if (touchPosition == TOUCH_BELOW) {
	                    rotation = -rotation;
	                }
	
	                // in this area would be code for doing something with the view as the frame moves.
                    if (isNeedSwipe) {
                        frame.setX(aPosX);
                        frame.setY(aPosY);
                        frame.setRotation(rotation);
                        mFlingListener.onScroll(getScrollProgress(), getScrollXProgressPercent());
                    }
	                break;

                case MotionEvent.ACTION_UP:
                case MotionEvent.ACTION_CANCEL:
                    //mActivePointerId = INVALID_POINTER_ID;
                    int pointerCount = event.getPointerCount();
                    int activePointerId = Math.min(mActivePointerId,pointerCount - 1);
                    aTouchUpX = event.getX(activePointerId);
                    mActivePointerId = INVALID_POINTER_ID;
                    resetCardViewOnStack(event);
	                break;

	        }
    	} catch (Exception e) {
			e.printStackTrace();
		}

        return true;
    }
{'fraction_non_alphanumeric': 0.06933461139068615, 'fraction_numerical': 0.004393322150331496, 'mean_word_length': 1.743207712532866, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7659895', 'n_tokens_mistral': 3559, 'n_tokens_neox': 3185, 'n_words': 866}