1919import android .content .Context ;
2020import android .os .SystemClock ;
2121import android .util .FloatMath ;
22+ import android .util .Log ;
2223
2324import java .util .Arrays ;
2425
@@ -223,10 +224,14 @@ private void addTouchHistory(MotionEvent ev) {
223224 * @param id pointer id to clear
224225 * @see #addTouchHistory(MotionEvent)
225226 */
226- private void removeTouchHistoryForId (int id ) {
227+ private boolean removeTouchHistoryForId (int id ) {
228+ if (id >= mTouchHistoryLastAccepted .length ) {
229+ return false ;
230+ }
227231 mTouchHistoryLastAccepted [id ] = Float .NaN ;
228232 mTouchHistoryDirection [id ] = 0 ;
229233 mTouchHistoryLastAcceptedTime [id ] = 0 ;
234+ return true ;
230235 }
231236
232237 /**
@@ -236,6 +241,11 @@ private void removeTouchHistoryForId(int id) {
236241 * @see #addTouchHistory(MotionEvent)
237242 */
238243 private float getAdjustedTouchHistory (int id ) {
244+ if (id >= mTouchHistoryLastAccepted .length ) {
245+ Log .e (TAG , "Error retrieving adjusted touch history for id=" + id +
246+ " - incomplete event stream?" );
247+ return 0 ;
248+ }
239249 return mTouchHistoryLastAccepted [id ];
240250 }
241251
@@ -244,6 +254,10 @@ private float getAdjustedTouchHistory(int id) {
244254 * @see #addTouchHistory(MotionEvent)
245255 */
246256 private void clearTouchHistory () {
257+ if (mTouchHistoryLastAccepted == null ) {
258+ // All three arrays will be null if this is the case; nothing to do.
259+ return ;
260+ }
247261 Arrays .fill (mTouchHistoryLastAccepted , Float .NaN );
248262 Arrays .fill (mTouchHistoryDirection , 0 );
249263 Arrays .fill (mTouchHistoryLastAcceptedTime , 0 );
@@ -333,7 +347,11 @@ public boolean onTouchEvent(MotionEvent event) {
333347 final float focusY = sumY / div ;
334348
335349 if (pointerUp ) {
336- removeTouchHistoryForId (event .getPointerId (event .getActionIndex ()));
350+ final int id = event .getPointerId (event .getActionIndex ());
351+ if (!removeTouchHistoryForId (id )) {
352+ Log .e (TAG , "Got ACTION_POINTER_UP for previously unknown id=" + id +
353+ " - incomplete event stream?" );
354+ }
337355 } else {
338356 addTouchHistory (event );
339357 }
0 commit comments