[Android] Progressive touch/key repeat listener

Nicolas Pomepuy git at videolan.org
Fri Dec 4 08:36:29 CET 2020


vlc-android | branch: master | Nicolas Pomepuy <nicolas at videolabs.io> | Tue Dec  1 09:53:54 2020 +0100| [0f75a7c69b6fbde79a7891679a4f4cf15bd8d9e7] | committer: Nicolas Pomepuy

Progressive touch/key repeat listener

> https://code.videolan.org/videolan/vlc-android/commit/0f75a7c69b6fbde79a7891679a4f4cf15bd8d9e7
---

 .../src/org/videolan/vlc/gui/helpers/OnRepeatListener.kt      | 11 +++++++----
 .../src/org/videolan/vlc/gui/helpers/OnRepeatListenerKey.kt   |  4 ++--
 .../src/org/videolan/vlc/gui/helpers/OnRepeatListenerTouch.kt |  4 ++--
 3 files changed, 11 insertions(+), 8 deletions(-)

diff --git a/application/vlc-android/src/org/videolan/vlc/gui/helpers/OnRepeatListener.kt b/application/vlc-android/src/org/videolan/vlc/gui/helpers/OnRepeatListener.kt
index 23956642e..7117b240a 100644
--- a/application/vlc-android/src/org/videolan/vlc/gui/helpers/OnRepeatListener.kt
+++ b/application/vlc-android/src/org/videolan/vlc/gui/helpers/OnRepeatListener.kt
@@ -25,7 +25,6 @@ package org.videolan.vlc.gui.helpers
 
 import android.os.Message
 import android.util.Log
-import android.view.MotionEvent
 import android.view.View
 
 import org.videolan.tools.WeakHandler
@@ -37,9 +36,9 @@ import org.videolan.vlc.BuildConfig
  * @param normalInterval Normal interval in millis
  * @param clickListener The OnClickListener to trigger
  */
-open class OnRepeatListener(private val initialInterval: Int, private val normalInterval: Int, private val clickListener: View.OnClickListener) {
+open class OnRepeatListener(private val initialInterval: Int, private val normalInterval: Int, private val speedUpDelay: Int, private val clickListener: View.OnClickListener) {
     var downView: View? = null
-
+    var initialTime: Long = -1L
     private val handler = OnRepeatHandler(this)
 
     init {
@@ -53,6 +52,7 @@ open class OnRepeatListener(private val initialInterval: Int, private val normal
         handler.removeMessages(ACTION_ONCLICK)
         handler.sendEmptyMessageDelayed(ACTION_ONCLICK, initialInterval.toLong())
         downView = view
+        initialTime = System.currentTimeMillis()
         clickListener.onClick(view)
         view.isPressed = true
         if (BuildConfig.DEBUG) Log.d("Delay", "onTouch: ACTION_DOWN")
@@ -61,6 +61,7 @@ open class OnRepeatListener(private val initialInterval: Int, private val normal
     fun stopRepeating(view: View) {
         handler.removeMessages(ACTION_ONCLICK)
         downView = null
+        initialTime = -1L
         view.isPressed = false
         if (BuildConfig.DEBUG) Log.d("Delay", "onTouch: ACTION_UP")
     }
@@ -70,7 +71,8 @@ open class OnRepeatListener(private val initialInterval: Int, private val normal
         override fun handleMessage(msg: Message) {
             when (msg.what) {
                 ACTION_ONCLICK -> {
-                    sendEmptyMessageDelayed(ACTION_ONCLICK, owner!!.normalInterval.toLong())
+                    val interval = if (owner!!.initialTime > -1L && System.currentTimeMillis() - owner!!.initialTime > owner!!.speedUpDelay) owner!!.normalInterval.toLong() / 3 else owner!!.normalInterval.toLong()
+                    sendEmptyMessageDelayed(ACTION_ONCLICK, interval)
                     owner!!.clickListener.onClick(owner!!.downView)
                 }
             }
@@ -84,5 +86,6 @@ open class OnRepeatListener(private val initialInterval: Int, private val normal
         //Default values in milliseconds
         const val DEFAULT_INITIAL_DELAY = 500
         const val DEFAULT_NORMAL_DELAY = 150
+        const val DEFAULT_SPEEDUP_DELAY = 2000
     }
 }
diff --git a/application/vlc-android/src/org/videolan/vlc/gui/helpers/OnRepeatListenerKey.kt b/application/vlc-android/src/org/videolan/vlc/gui/helpers/OnRepeatListenerKey.kt
index a8c27c600..2809fa3ec 100644
--- a/application/vlc-android/src/org/videolan/vlc/gui/helpers/OnRepeatListenerKey.kt
+++ b/application/vlc-android/src/org/videolan/vlc/gui/helpers/OnRepeatListenerKey.kt
@@ -39,13 +39,13 @@ import org.videolan.vlc.BuildConfig
  * @param normalInterval Normal interval in millis
  * @param clickListener The OnClickListener to trigger
  */
-class OnRepeatListenerKey(private val initialInterval: Int, private val normalInterval: Int, private val clickListener: View.OnClickListener) : View.OnKeyListener, OnRepeatListener(initialInterval, normalInterval, clickListener) {
+class OnRepeatListenerKey(private val initialInterval: Int, private val normalInterval: Int, private val speedUpDelay: Int, private val clickListener: View.OnClickListener) : View.OnKeyListener, OnRepeatListener(initialInterval, normalInterval, speedUpDelay, clickListener) {
 
     /**
      *
      * @param clickListener The OnClickListener to trigger
      */
-    constructor(clickListener: View.OnClickListener) : this(DEFAULT_INITIAL_DELAY, DEFAULT_NORMAL_DELAY, clickListener)
+    constructor(clickListener: View.OnClickListener) : this(DEFAULT_INITIAL_DELAY, DEFAULT_NORMAL_DELAY, DEFAULT_SPEEDUP_DELAY, clickListener)
 
     override fun onKey(view: View?, keyCode: Int, event: KeyEvent?): Boolean {
         if (view == null || event == null) return false
diff --git a/application/vlc-android/src/org/videolan/vlc/gui/helpers/OnRepeatListenerTouch.kt b/application/vlc-android/src/org/videolan/vlc/gui/helpers/OnRepeatListenerTouch.kt
index 827d48354..a3f4abed1 100644
--- a/application/vlc-android/src/org/videolan/vlc/gui/helpers/OnRepeatListenerTouch.kt
+++ b/application/vlc-android/src/org/videolan/vlc/gui/helpers/OnRepeatListenerTouch.kt
@@ -38,12 +38,12 @@ import org.videolan.vlc.BuildConfig
  * @param normalInterval Normal interval in millis
  * @param clickListener The OnClickListener to trigger
  */
-class OnRepeatListenerTouch(private val initialInterval: Int, private val normalInterval: Int, private val clickListener: View.OnClickListener) : View.OnTouchListener, OnRepeatListener(initialInterval, normalInterval, clickListener) {
+class OnRepeatListenerTouch(private val initialInterval: Int, private val normalInterval: Int, private val speedUpDelay: Int, private val clickListener: View.OnClickListener) : View.OnTouchListener, OnRepeatListener(initialInterval, normalInterval, speedUpDelay, clickListener) {
     /**
      *
      * @param clickListener The OnClickListener to trigger
      */
-    constructor(clickListener: View.OnClickListener) : this(DEFAULT_INITIAL_DELAY, DEFAULT_NORMAL_DELAY, clickListener)
+    constructor(clickListener: View.OnClickListener) : this(DEFAULT_INITIAL_DELAY, DEFAULT_NORMAL_DELAY, DEFAULT_SPEEDUP_DELAY, clickListener)
 
     override fun onTouch(view: View, motionEvent: MotionEvent): Boolean {
         when (motionEvent.action) {



More information about the Android mailing list