improving some widgetview gesture related handling

This commit is contained in:
tibbi 2022-10-02 22:22:24 +02:00
parent 6d6bc948f2
commit ae66ce83f2
4 changed files with 50 additions and 7 deletions

View file

@ -19,3 +19,4 @@ const val ITEM_TYPE_WIDGET = 1
const val ITEM_TYPE_SHORTCUT = 2 const val ITEM_TYPE_SHORTCUT = 2
const val WIDGET_HOST_ID = 12345 const val WIDGET_HOST_ID = 12345
const val MAX_ALLOWED_MOVE_PX = 10

View file

@ -166,6 +166,7 @@ class HomeScreenGrid(context: Context, attrs: AttributeSet, defStyle: Int) : Rel
} }
} }
@SuppressLint("ClickableViewAccessibility")
fun widgetLongPressed(item: HomeScreenGridItem) { fun widgetLongPressed(item: HomeScreenGridItem) {
resizedWidget = item resizedWidget = item
redrawGrid() redrawGrid()
@ -178,7 +179,15 @@ class HomeScreenGrid(context: Context, attrs: AttributeSet, defStyle: Int) : Rel
val frameRect = Rect(viewX, viewY, viewX + widgetView.width, viewY + widgetView.height) val frameRect = Rect(viewX, viewY, viewX + widgetView.width, viewY + widgetView.height)
resize_frame.updateFrameCoords(frameRect) resize_frame.updateFrameCoords(frameRect)
resize_frame.beVisible() resize_frame.beVisible()
resize_frame.onClickListener = {
hideResizeLines()
}
widgetView.ignoreTouches = true widgetView.ignoreTouches = true
widgetView.setOnTouchListener { v, event ->
resize_frame.onTouchEvent(event)
return@setOnTouchListener true
}
} }
} }
@ -188,7 +197,10 @@ class HomeScreenGrid(context: Context, attrs: AttributeSet, defStyle: Int) : Rel
} }
resize_frame.beGone() resize_frame.beGone()
widgetViews.firstOrNull { it.tag == resizedWidget!!.widgetId }?.ignoreTouches = false widgetViews.firstOrNull { it.tag == resizedWidget!!.widgetId }?.apply {
ignoreTouches = false
setOnTouchListener(null)
}
resizedWidget = null resizedWidget = null
} }

View file

@ -7,12 +7,14 @@ import android.os.Handler
import android.view.MotionEvent import android.view.MotionEvent
import android.view.ViewConfiguration import android.view.ViewConfiguration
import com.simplemobiletools.commons.extensions.performHapticFeedback import com.simplemobiletools.commons.extensions.performHapticFeedback
import com.simplemobiletools.launcher.helpers.MAX_ALLOWED_MOVE_PX
class MyAppWidgetHostView(context: Context) : AppWidgetHostView(context) { class MyAppWidgetHostView(context: Context) : AppWidgetHostView(context) {
private var MAX_ALLOWED_MOVE = 10 private var MAX_CLICK_DURATION = 150
private var longPressHandler = Handler() private var longPressHandler = Handler()
private var actionDownCoords = PointF() private var actionDownCoords = PointF()
private var currentCoords = PointF() private var currentCoords = PointF()
private var actionDownMS = 0L
var hasLongPressed = false var hasLongPressed = false
var ignoreTouches = false var ignoreTouches = false
var longPressListener: ((x: Float, y: Float) -> Unit)? = null var longPressListener: ((x: Float, y: Float) -> Unit)? = null
@ -44,6 +46,7 @@ class MyAppWidgetHostView(context: Context) : AppWidgetHostView(context) {
actionDownCoords.y = event.rawY actionDownCoords.y = event.rawY
currentCoords.x = event.rawX currentCoords.x = event.rawX
currentCoords.y = event.rawY currentCoords.y = event.rawY
actionDownMS = System.currentTimeMillis()
} }
MotionEvent.ACTION_MOVE -> { MotionEvent.ACTION_MOVE -> {
currentCoords.x = event.rawX currentCoords.x = event.rawX
@ -58,7 +61,7 @@ class MyAppWidgetHostView(context: Context) : AppWidgetHostView(context) {
} }
private val longPressRunnable = Runnable { private val longPressRunnable = Runnable {
if (Math.abs(actionDownCoords.x - currentCoords.x) < MAX_ALLOWED_MOVE && Math.abs(actionDownCoords.y - currentCoords.y) < MAX_ALLOWED_MOVE) { if (Math.abs(actionDownCoords.x - currentCoords.x) < MAX_ALLOWED_MOVE_PX && Math.abs(actionDownCoords.y - currentCoords.y) < MAX_ALLOWED_MOVE_PX) {
longPressHandler.removeCallbacksAndMessages(null) longPressHandler.removeCallbacksAndMessages(null)
hasLongPressed = true hasLongPressed = true
longPressListener?.invoke(actionDownCoords.x, actionDownCoords.y) longPressListener?.invoke(actionDownCoords.x, actionDownCoords.y)

View file

@ -2,20 +2,23 @@ package com.simplemobiletools.launcher.views
import android.annotation.SuppressLint import android.annotation.SuppressLint
import android.content.Context import android.content.Context
import android.graphics.Canvas import android.graphics.*
import android.graphics.Color
import android.graphics.Paint
import android.graphics.Rect
import android.graphics.drawable.ColorDrawable import android.graphics.drawable.ColorDrawable
import android.util.AttributeSet import android.util.AttributeSet
import android.view.MotionEvent
import android.widget.RelativeLayout import android.widget.RelativeLayout
import com.simplemobiletools.launcher.R import com.simplemobiletools.launcher.R
import com.simplemobiletools.launcher.helpers.MAX_ALLOWED_MOVE_PX
@SuppressLint("ViewConstructor") @SuppressLint("ViewConstructor")
class MyAppWidgetResizeFrame(context: Context, attrs: AttributeSet, defStyle: Int) : RelativeLayout(context, attrs, defStyle) { class MyAppWidgetResizeFrame(context: Context, attrs: AttributeSet, defStyle: Int) : RelativeLayout(context, attrs, defStyle) {
constructor(context: Context, attrs: AttributeSet) : this(context, attrs, 0) constructor(context: Context, attrs: AttributeSet) : this(context, attrs, 0)
private var resizeWidgetLinePaint: Paint private var resizeWidgetLinePaint: Paint
private var actionDownCoords = PointF()
private var actionDownMS = 0L
private var MAX_CLICK_DURATION = 150
var onClickListener: (() -> Unit)? = null
init { init {
background = ColorDrawable(Color.TRANSPARENT) background = ColorDrawable(Color.TRANSPARENT)
@ -35,6 +38,30 @@ class MyAppWidgetResizeFrame(context: Context, attrs: AttributeSet, defStyle: In
requestLayout() requestLayout()
} }
override fun onTouchEvent(event: MotionEvent?): Boolean {
if (event == null) {
return false
}
when (event.actionMasked) {
MotionEvent.ACTION_DOWN -> {
actionDownCoords.x = event.rawX
actionDownCoords.y = event.rawY
actionDownMS = System.currentTimeMillis()
}
MotionEvent.ACTION_UP, MotionEvent.ACTION_CANCEL -> {
if (System.currentTimeMillis() - actionDownMS < MAX_CLICK_DURATION &&
Math.abs(actionDownCoords.x - event.rawX) < MAX_ALLOWED_MOVE_PX &&
Math.abs(actionDownCoords.y - event.rawY) < MAX_ALLOWED_MOVE_PX
) {
onClickListener?.invoke()
}
}
}
return super.onTouchEvent(event)
}
override fun onDraw(canvas: Canvas) { override fun onDraw(canvas: Canvas) {
super.onDraw(canvas) super.onDraw(canvas)
if (x != 0f || y != 0f) { if (x != 0f || y != 0f) {