From ae66ce83f22f8427cc438f06ef4101ffb5126883 Mon Sep 17 00:00:00 2001 From: tibbi Date: Sun, 2 Oct 2022 22:22:24 +0200 Subject: [PATCH] improving some widgetview gesture related handling --- .../launcher/helpers/Constants.kt | 1 + .../launcher/views/HomeScreenGrid.kt | 14 +++++++- .../launcher/views/MyAppWidgetHostView.kt | 7 ++-- .../launcher/views/MyAppWidgetResizeFrame.kt | 35 ++++++++++++++++--- 4 files changed, 50 insertions(+), 7 deletions(-) diff --git a/app/src/main/kotlin/com/simplemobiletools/launcher/helpers/Constants.kt b/app/src/main/kotlin/com/simplemobiletools/launcher/helpers/Constants.kt index 36d5ddc..57a2c12 100644 --- a/app/src/main/kotlin/com/simplemobiletools/launcher/helpers/Constants.kt +++ b/app/src/main/kotlin/com/simplemobiletools/launcher/helpers/Constants.kt @@ -19,3 +19,4 @@ const val ITEM_TYPE_WIDGET = 1 const val ITEM_TYPE_SHORTCUT = 2 const val WIDGET_HOST_ID = 12345 +const val MAX_ALLOWED_MOVE_PX = 10 diff --git a/app/src/main/kotlin/com/simplemobiletools/launcher/views/HomeScreenGrid.kt b/app/src/main/kotlin/com/simplemobiletools/launcher/views/HomeScreenGrid.kt index 713bc76..fc98d5c 100644 --- a/app/src/main/kotlin/com/simplemobiletools/launcher/views/HomeScreenGrid.kt +++ b/app/src/main/kotlin/com/simplemobiletools/launcher/views/HomeScreenGrid.kt @@ -166,6 +166,7 @@ class HomeScreenGrid(context: Context, attrs: AttributeSet, defStyle: Int) : Rel } } + @SuppressLint("ClickableViewAccessibility") fun widgetLongPressed(item: HomeScreenGridItem) { resizedWidget = item 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) resize_frame.updateFrameCoords(frameRect) resize_frame.beVisible() + resize_frame.onClickListener = { + hideResizeLines() + } + 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() - widgetViews.firstOrNull { it.tag == resizedWidget!!.widgetId }?.ignoreTouches = false + widgetViews.firstOrNull { it.tag == resizedWidget!!.widgetId }?.apply { + ignoreTouches = false + setOnTouchListener(null) + } resizedWidget = null } diff --git a/app/src/main/kotlin/com/simplemobiletools/launcher/views/MyAppWidgetHostView.kt b/app/src/main/kotlin/com/simplemobiletools/launcher/views/MyAppWidgetHostView.kt index d78b0be..94069ff 100644 --- a/app/src/main/kotlin/com/simplemobiletools/launcher/views/MyAppWidgetHostView.kt +++ b/app/src/main/kotlin/com/simplemobiletools/launcher/views/MyAppWidgetHostView.kt @@ -7,12 +7,14 @@ import android.os.Handler import android.view.MotionEvent import android.view.ViewConfiguration import com.simplemobiletools.commons.extensions.performHapticFeedback +import com.simplemobiletools.launcher.helpers.MAX_ALLOWED_MOVE_PX class MyAppWidgetHostView(context: Context) : AppWidgetHostView(context) { - private var MAX_ALLOWED_MOVE = 10 + private var MAX_CLICK_DURATION = 150 private var longPressHandler = Handler() private var actionDownCoords = PointF() private var currentCoords = PointF() + private var actionDownMS = 0L var hasLongPressed = false var ignoreTouches = false var longPressListener: ((x: Float, y: Float) -> Unit)? = null @@ -44,6 +46,7 @@ class MyAppWidgetHostView(context: Context) : AppWidgetHostView(context) { actionDownCoords.y = event.rawY currentCoords.x = event.rawX currentCoords.y = event.rawY + actionDownMS = System.currentTimeMillis() } MotionEvent.ACTION_MOVE -> { currentCoords.x = event.rawX @@ -58,7 +61,7 @@ class MyAppWidgetHostView(context: Context) : AppWidgetHostView(context) { } 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) hasLongPressed = true longPressListener?.invoke(actionDownCoords.x, actionDownCoords.y) diff --git a/app/src/main/kotlin/com/simplemobiletools/launcher/views/MyAppWidgetResizeFrame.kt b/app/src/main/kotlin/com/simplemobiletools/launcher/views/MyAppWidgetResizeFrame.kt index 18cd559..887b939 100644 --- a/app/src/main/kotlin/com/simplemobiletools/launcher/views/MyAppWidgetResizeFrame.kt +++ b/app/src/main/kotlin/com/simplemobiletools/launcher/views/MyAppWidgetResizeFrame.kt @@ -2,20 +2,23 @@ package com.simplemobiletools.launcher.views import android.annotation.SuppressLint import android.content.Context -import android.graphics.Canvas -import android.graphics.Color -import android.graphics.Paint -import android.graphics.Rect +import android.graphics.* import android.graphics.drawable.ColorDrawable import android.util.AttributeSet +import android.view.MotionEvent import android.widget.RelativeLayout import com.simplemobiletools.launcher.R +import com.simplemobiletools.launcher.helpers.MAX_ALLOWED_MOVE_PX @SuppressLint("ViewConstructor") class MyAppWidgetResizeFrame(context: Context, attrs: AttributeSet, defStyle: Int) : RelativeLayout(context, attrs, defStyle) { constructor(context: Context, attrs: AttributeSet) : this(context, attrs, 0) private var resizeWidgetLinePaint: Paint + private var actionDownCoords = PointF() + private var actionDownMS = 0L + private var MAX_CLICK_DURATION = 150 + var onClickListener: (() -> Unit)? = null init { background = ColorDrawable(Color.TRANSPARENT) @@ -35,6 +38,30 @@ class MyAppWidgetResizeFrame(context: Context, attrs: AttributeSet, defStyle: In 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) { super.onDraw(canvas) if (x != 0f || y != 0f) {