From f3a2bf8edd2e070a24f0d7c105d78b38576e14ac Mon Sep 17 00:00:00 2001 From: Adam Powell Date: Fri, 28 Sep 2012 12:05:10 -0700 Subject: [PATCH] ScaleGestureDetector does the safety dance. Warn in the event of possibly bogus event streams and don't try to clear empty history. Bug 7241640 Bug 7243006 Change-Id: I037cf1334cab790ef5998ca5f8f6b323ed5f4459 --- .../android/view/ScaleGestureDetector.java | 22 +++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/core/java/android/view/ScaleGestureDetector.java b/core/java/android/view/ScaleGestureDetector.java index b0a271198ca21..4873860d59341 100644 --- a/core/java/android/view/ScaleGestureDetector.java +++ b/core/java/android/view/ScaleGestureDetector.java @@ -19,6 +19,7 @@ package android.view; import android.content.Context; import android.os.SystemClock; import android.util.FloatMath; +import android.util.Log; import java.util.Arrays; @@ -223,10 +224,14 @@ public class ScaleGestureDetector { * @param id pointer id to clear * @see #addTouchHistory(MotionEvent) */ - private void removeTouchHistoryForId(int id) { + private boolean removeTouchHistoryForId(int id) { + if (id >= mTouchHistoryLastAccepted.length) { + return false; + } mTouchHistoryLastAccepted[id] = Float.NaN; mTouchHistoryDirection[id] = 0; mTouchHistoryLastAcceptedTime[id] = 0; + return true; } /** @@ -236,6 +241,11 @@ public class ScaleGestureDetector { * @see #addTouchHistory(MotionEvent) */ private float getAdjustedTouchHistory(int id) { + if (id >= mTouchHistoryLastAccepted.length) { + Log.e(TAG, "Error retrieving adjusted touch history for id=" + id + + " - incomplete event stream?"); + return 0; + } return mTouchHistoryLastAccepted[id]; } @@ -244,6 +254,10 @@ public class ScaleGestureDetector { * @see #addTouchHistory(MotionEvent) */ private void clearTouchHistory() { + if (mTouchHistoryLastAccepted == null) { + // All three arrays will be null if this is the case; nothing to do. + return; + } Arrays.fill(mTouchHistoryLastAccepted, Float.NaN); Arrays.fill(mTouchHistoryDirection, 0); Arrays.fill(mTouchHistoryLastAcceptedTime, 0); @@ -333,7 +347,11 @@ public class ScaleGestureDetector { final float focusY = sumY / div; if (pointerUp) { - removeTouchHistoryForId(event.getPointerId(event.getActionIndex())); + final int id = event.getPointerId(event.getActionIndex()); + if (!removeTouchHistoryForId(id)) { + Log.e(TAG, "Got ACTION_POINTER_UP for previously unknown id=" + id + + " - incomplete event stream?"); + } } else { addTouchHistory(event); }