From 5d66a4929f61b37a63dfb3ebdadde02e622afb22 Mon Sep 17 00:00:00 2001 From: Nishant Srivastava Date: Sat, 18 Aug 2018 19:19:12 +0200 Subject: [PATCH] =?UTF-8?q?=F0=9F=9A=A7=20handle=20cases=20where=20a=20tou?= =?UTF-8?q?ch=20event=20can=20be=20null=20and=20result=20in=20future=20NPE?= =?UTF-8?q?=20issues?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Fixes Issue #21 --- .../nisrulz/sensey/TouchTypeDetector.java | 47 ++++++++++++------- 1 file changed, 30 insertions(+), 17 deletions(-) diff --git a/sensey/src/main/java/com/github/nisrulz/sensey/TouchTypeDetector.java b/sensey/src/main/java/com/github/nisrulz/sensey/TouchTypeDetector.java index e55d8fb..7f2ecdb 100644 --- a/sensey/src/main/java/com/github/nisrulz/sensey/TouchTypeDetector.java +++ b/sensey/src/main/java/com/github/nisrulz/sensey/TouchTypeDetector.java @@ -31,13 +31,17 @@ class GestureListener extends GestureDetector.SimpleOnGestureListener { @Override public boolean onDoubleTap(MotionEvent e) { - touchTypListener.onDoubleTap(); - return super.onDoubleTap(e); + if (e != null) { + touchTypListener.onDoubleTap(); + return super.onDoubleTap(e); + } else { + return false; + } } @Override public boolean onFling(MotionEvent startevent, MotionEvent finishevent, float velocityX, - float velocityY) { + float velocityY) { final float deltaX = finishevent.getX() - startevent.getX(); final float deltaY = finishevent.getY() - startevent.getY(); @@ -67,13 +71,15 @@ public boolean onFling(MotionEvent startevent, MotionEvent finishevent, float ve @Override public void onLongPress(MotionEvent e) { - touchTypListener.onLongPress(); - super.onLongPress(e); + if (e != null) { + touchTypListener.onLongPress(); + super.onLongPress(e); + } } @Override public boolean onScroll(MotionEvent startevent, MotionEvent finishevent, float distanceX, - float distanceY) { + float distanceY) { float deltaX = finishevent.getX() - startevent.getX(); float deltaY = finishevent.getY() - startevent.getY(); @@ -103,8 +109,12 @@ public boolean onScroll(MotionEvent startevent, MotionEvent finishevent, float d @Override public boolean onSingleTapConfirmed(MotionEvent e) { - touchTypListener.onSingleTap(); - return super.onSingleTapConfirmed(e); + if (e != null) { + touchTypListener.onSingleTap(); + return super.onSingleTapConfirmed(e); + } else { + return false; + } } } @@ -155,15 +165,18 @@ public TouchTypeDetector(Context context, TouchTypListener touchTypListener) { } boolean onTouchEvent(MotionEvent event) { - - switch (event.getActionMasked()) { - case MotionEvent.ACTION_POINTER_DOWN: - if (event.getPointerCount() == 3) { - touchTypListener.onThreeFingerSingleTap(); - } else if (event.getPointerCount() == 2) { - touchTypListener.onTwoFingerSingleTap(); - } + if (event != null) { + switch (event.getActionMasked()) { + case MotionEvent.ACTION_POINTER_DOWN: + if (event.getPointerCount() == 3) { + touchTypListener.onThreeFingerSingleTap(); + } else if (event.getPointerCount() == 2) { + touchTypListener.onTwoFingerSingleTap(); + } + } + return gDetect.onTouchEvent(event); + } else { + return false; } - return gDetect.onTouchEvent(event); } }