Skip to content

Commit

Permalink
Merge pull request #3 from nos-digital/bugfix/motionevent-nullpointer
Browse files Browse the repository at this point in the history
Fix nullpointer issues with null motionevents
  • Loading branch information
vuongp committed Jun 16, 2020
2 parents 0ac0311 + fd925e7 commit 50559ff
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,11 @@ class DoubleTapToZoomTouchHandler(
}
)

override fun onTouch(v: View?, event: MotionEvent?) = gestureDetector.onTouchEvent(event)
override fun onTouch(v: View?, event: MotionEvent?): Boolean {
if (event == null) return false

return gestureDetector.onTouchEvent(event)
}

companion object {
var isAnimating = false
Expand Down
6 changes: 5 additions & 1 deletion library/src/main/java/nl/nos/imagin/Imagin.kt
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,11 @@ class Imagin private constructor(
)
)
touchListeners.add { _, event ->
scaleDetector.onTouchEvent(event)
if (event != null) {
scaleDetector.onTouchEvent(event)
} else {
false
}
}
return this
}
Expand Down
13 changes: 7 additions & 6 deletions library/src/main/java/nl/nos/imagin/ScrollHandler.kt
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,14 @@ class ScrollHandler(
object : GestureDetector.SimpleOnGestureListener() {

override fun onScroll(
firstMotionEvent: MotionEvent,
moveMotionEvent: MotionEvent,
firstMotionEvent: MotionEvent?,
moveMotionEvent: MotionEvent?,
distanceX: Float,
distanceY: Float
): Boolean {
if (imageView.rightEdgeIsVisible() && distanceX > 0 && moveMotionEvent.pointerCount == 1) {
if (imageView.rightEdgeIsVisible() && distanceX > 0 && moveMotionEvent?.pointerCount == 1) {
imageView.parent?.requestDisallowInterceptTouchEvent(false)
} else if (imageView.leftEdgeIsVisible() && distanceX < 0 && moveMotionEvent.pointerCount == 1) {
} else if (imageView.leftEdgeIsVisible() && distanceX < 0 && moveMotionEvent?.pointerCount == 1) {
imageView.parent?.requestDisallowInterceptTouchEvent(false)
}

Expand Down Expand Up @@ -80,12 +80,13 @@ class ScrollHandler(
}

override fun onTouch(v: View?, event: MotionEvent?): Boolean {
if (event == null) return false

imageView.parent?.requestDisallowInterceptTouchEvent(true)

val consumed = gestureDetector.onTouchEvent(event)


if (event?.action == MotionEvent.ACTION_UP || event?.action == MotionEvent.ACTION_CANCEL) {
if (event.action == MotionEvent.ACTION_UP || event.action == MotionEvent.ACTION_CANCEL) {
val imageSize = calculator.calculateImageSize(imageView) ?: return consumed

if (allowScrollOutOfBoundsHorizontally && shouldTriggerOutOfBoundListener(
Expand Down
6 changes: 5 additions & 1 deletion library/src/main/java/nl/nos/imagin/SingleTapHandler.kt
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,11 @@ class SingleTapHandler(
gestureDetector.setOnDoubleTapListener(tapListener)
}

override fun onTouch(v: View?, event: MotionEvent?) = gestureDetector.onTouchEvent(event)
override fun onTouch(v: View?, event: MotionEvent?): Boolean {
if (event == null) return false

return gestureDetector.onTouchEvent(event)
}

/**
* Simple listener for single view taps.
Expand Down

0 comments on commit 50559ff

Please sign in to comment.