add some distance checking at long pressing widgets

This commit is contained in:
tibbi 2022-10-01 23:24:36 +02:00
parent db02a2ce40
commit f4ea085118
1 changed files with 14 additions and 4 deletions

View File

@ -9,8 +9,10 @@ import android.view.ViewConfiguration
import com.simplemobiletools.commons.extensions.performHapticFeedback
class MyAppWidgetHostView(context: Context) : AppWidgetHostView(context) {
private var MAX_ALLOWED_MOVE = 10
private var longPressHandler = Handler()
private var actionDownCoords = PointF()
private var currentCoords = PointF()
var hasLongPressed = false
var longPressListener: ((x: Float, y: Float) -> Unit)? = null
@ -25,6 +27,12 @@ class MyAppWidgetHostView(context: Context) : AppWidgetHostView(context) {
longPressHandler.postDelayed(longPressRunnable, ViewConfiguration.getLongPressTimeout().toLong())
actionDownCoords.x = event.rawX
actionDownCoords.y = event.rawY
currentCoords.x = event.rawX
currentCoords.y = event.rawY
}
MotionEvent.ACTION_MOVE -> {
currentCoords.x = event.rawX
currentCoords.y = event.rawY
}
MotionEvent.ACTION_UP, MotionEvent.ACTION_CANCEL -> longPressHandler.removeCallbacksAndMessages(null)
}
@ -33,9 +41,11 @@ class MyAppWidgetHostView(context: Context) : AppWidgetHostView(context) {
}
private val longPressRunnable = Runnable {
longPressHandler.removeCallbacksAndMessages(null)
hasLongPressed = true
longPressListener?.invoke(actionDownCoords.x, actionDownCoords.y)
performHapticFeedback()
if (Math.abs(actionDownCoords.x - currentCoords.x) < MAX_ALLOWED_MOVE && Math.abs(actionDownCoords.y - currentCoords.y) < MAX_ALLOWED_MOVE) {
longPressHandler.removeCallbacksAndMessages(null)
hasLongPressed = true
longPressListener?.invoke(actionDownCoords.x, actionDownCoords.y)
performHapticFeedback()
}
}
}