DoubleTapDetector.kt raw

   1  package io.github.domi04151309.alwayson.custom
   2  
   3  import android.view.MotionEvent
   4  import android.view.ViewConfiguration
   5  
   6  class DoubleTapDetector(
   7      private val listener: () -> Unit,
   8      private val timeout: Int = ViewConfiguration.getDoubleTapTimeout(),
   9  ) {
  10      private var lastTap = 0L
  11  
  12      fun onTouchEvent(ev: MotionEvent) {
  13          if (ev.action and MotionEvent.ACTION_MASK == MotionEvent.ACTION_DOWN) {
  14              lastTap =
  15                  if (System.currentTimeMillis() - lastTap < timeout) {
  16                      listener()
  17                      0
  18                  } else {
  19                      System.currentTimeMillis()
  20                  }
  21          }
  22      }
  23  }
  24