Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit d8a3663a authored by Keiji Ariyama's avatar Keiji Ariyama
Browse files

Fixes an issue that occured unexpected exception "pointerIndex out of range".

The findNewActiveIndex method may return -1.
So, the code should check case of -1 before event.getPointerId
and if index0 is -1, gesture should be ended immediately.

Change-Id: I4aae5c84e3db61d10b0bfcfa7bfa6b9115231a52
parent 67cf093d
Loading
Loading
Loading
Loading
+29 −9
Original line number Diff line number Diff line
@@ -220,8 +220,10 @@ public class ScaleGestureDetector {
                mActiveId1 = event.getPointerId(index1);
                if (index0 < 0 || index0 == index1) {
                    // Probably someone sending us a broken event stream.
                    index0 = findNewActiveIndex(event, index0 == index1 ? -1 : mActiveId1, index0);
                    mActiveId0 = event.getPointerId(index0);
                    boolean valid = handleBrokenEventStream(event);
                    if (!valid) {
                        return false;
                    }
                }
                mActive0MostRecent = false;

@@ -377,13 +379,10 @@ public class ScaleGestureDetector {
                    int index0 = event.findPointerIndex(mActiveId0);
                    if (index0 < 0 || mActiveId0 == mActiveId1) {
                        // Probably someone sending us a broken event stream.
                        Log.e(TAG, "Got " + MotionEvent.actionToString(action) +
                                " with bad state while a gesture was in progress. " +
                                "Did you forget to pass an event to " +
                                "ScaleGestureDetector#onTouchEvent?");
                        index0 = findNewActiveIndex(event,
                                mActiveId0 == mActiveId1 ? -1 : mActiveId1, index0);
                        mActiveId0 = event.getPointerId(index0);
                        boolean valid = handleBrokenEventStream(event);
                        if (!valid) {
                            return false;
                        }
                    }

                    setContext(event);
@@ -483,6 +482,27 @@ public class ScaleGestureDetector {
        return handled;
    }

    private boolean handleBrokenEventStream(MotionEvent event) {
        Log.e(TAG, "Got " + MotionEvent.actionToString(event.getActionMasked()) +
                " with bad state while a gesture was in progress. " +
                "Did you forget to pass an event to " +
                "ScaleGestureDetector#onTouchEvent?");
        int index0 = findNewActiveIndex(event,
                mActiveId0 == mActiveId1 ? -1 : mActiveId1,
                event.findPointerIndex(mActiveId0));
        if (index0 >= 0) {
            mActiveId0 = event.getPointerId(index0);
            return true;
        } else {
            mInvalidGesture = true;
            Log.e(TAG, "Invalid MotionEvent stream detected.", new Throwable());
            if (mGestureInProgress) {
                mListener.onScaleEnd(this);
            }
            return false;
        }
    }

    private int findNewActiveIndex(MotionEvent ev, int otherActiveId, int oldIndex) {
        final int pointerCount = ev.getPointerCount();