Fixed MusicSeekSkipTouchListener
This commit is contained in:
parent
5d0b5b91b0
commit
b17013f5bb
1 changed files with 53 additions and 37 deletions
|
@ -1,13 +1,14 @@
|
||||||
package code.name.monkey.retromusic.fragments
|
package code.name.monkey.retromusic.fragments
|
||||||
|
|
||||||
import android.annotation.SuppressLint
|
import android.annotation.SuppressLint
|
||||||
import android.view.GestureDetector
|
|
||||||
import android.view.MotionEvent
|
import android.view.MotionEvent
|
||||||
import android.view.View
|
import android.view.View
|
||||||
|
import android.view.ViewConfiguration
|
||||||
import androidx.fragment.app.FragmentActivity
|
import androidx.fragment.app.FragmentActivity
|
||||||
import androidx.lifecycle.lifecycleScope
|
import androidx.lifecycle.lifecycleScope
|
||||||
import code.name.monkey.retromusic.helper.MusicPlayerRemote
|
import code.name.monkey.retromusic.helper.MusicPlayerRemote
|
||||||
import kotlinx.coroutines.*
|
import kotlinx.coroutines.*
|
||||||
|
import kotlin.math.abs
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param activity, Activity
|
* @param activity, Activity
|
||||||
|
@ -16,48 +17,63 @@ import kotlinx.coroutines.*
|
||||||
class MusicSeekSkipTouchListener(val activity: FragmentActivity, val next: Boolean) :
|
class MusicSeekSkipTouchListener(val activity: FragmentActivity, val next: Boolean) :
|
||||||
View.OnTouchListener {
|
View.OnTouchListener {
|
||||||
|
|
||||||
var job: Job? = null
|
private var job: Job? = null
|
||||||
var counter = 0
|
private var counter = 0
|
||||||
var wasSeeking = false
|
private var wasSeeking = false
|
||||||
|
|
||||||
private val gestureDetector = GestureDetector(activity, object :
|
private var startX = 0f
|
||||||
GestureDetector.SimpleOnGestureListener() {
|
private var startY = 0f
|
||||||
override fun onDown(e: MotionEvent?): Boolean {
|
|
||||||
job = activity.lifecycleScope.launch(Dispatchers.Default) {
|
private val scaledTouchSlop = ViewConfiguration.get(activity).scaledTouchSlop
|
||||||
counter = 0
|
|
||||||
while (isActive) {
|
|
||||||
delay(500)
|
|
||||||
wasSeeking = true
|
|
||||||
var seekingDuration = MusicPlayerRemote.songProgressMillis
|
|
||||||
if (next) {
|
|
||||||
seekingDuration += 5000 * (counter.floorDiv(2) + 1)
|
|
||||||
} else {
|
|
||||||
seekingDuration -= 5000 * (counter.floorDiv(2) + 1)
|
|
||||||
}
|
|
||||||
MusicPlayerRemote.seekTo(seekingDuration)
|
|
||||||
counter += 1
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return super.onDown(e)
|
|
||||||
}
|
|
||||||
})
|
|
||||||
|
|
||||||
@SuppressLint("ClickableViewAccessibility")
|
@SuppressLint("ClickableViewAccessibility")
|
||||||
override fun onTouch(v: View?, event: MotionEvent?): Boolean {
|
override fun onTouch(v: View?, event: MotionEvent?): Boolean {
|
||||||
val action = event?.actionMasked
|
when (event?.actionMasked) {
|
||||||
if (action == MotionEvent.ACTION_UP) {
|
MotionEvent.ACTION_DOWN -> {
|
||||||
job?.cancel()
|
startX = event.x
|
||||||
if (v?.isPressed == true && !wasSeeking) {
|
startY = event.y
|
||||||
if (next) {
|
job = activity.lifecycleScope.launch(Dispatchers.Default) {
|
||||||
MusicPlayerRemote.playNextSong()
|
counter = 0
|
||||||
} else {
|
while (isActive) {
|
||||||
MusicPlayerRemote.back()
|
delay(500)
|
||||||
|
wasSeeking = true
|
||||||
|
var seekingDuration = MusicPlayerRemote.songProgressMillis
|
||||||
|
if (next) {
|
||||||
|
seekingDuration += 5000 * (counter.floorDiv(2) + 1)
|
||||||
|
} else {
|
||||||
|
seekingDuration -= 5000 * (counter.floorDiv(2) + 1)
|
||||||
|
}
|
||||||
|
withContext(Dispatchers.Main) {
|
||||||
|
MusicPlayerRemote.seekTo(seekingDuration)
|
||||||
|
}
|
||||||
|
counter += 1
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
wasSeeking = false
|
MotionEvent.ACTION_UP -> {
|
||||||
} else if (action == MotionEvent.ACTION_CANCEL) {
|
job?.cancel()
|
||||||
job?.cancel()
|
val endX = event.x
|
||||||
|
val endY = event.y
|
||||||
|
if (!wasSeeking && isAClick(startX, endX, startY, endY)) {
|
||||||
|
if (next) {
|
||||||
|
MusicPlayerRemote.playNextSong()
|
||||||
|
} else {
|
||||||
|
MusicPlayerRemote.back()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
wasSeeking = false
|
||||||
|
}
|
||||||
|
MotionEvent.ACTION_CANCEL -> {
|
||||||
|
job?.cancel()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return gestureDetector.onTouchEvent(event)
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun isAClick(startX: Float, endX: Float, startY: Float, endY: Float): Boolean {
|
||||||
|
val differenceX = abs(startX - endX)
|
||||||
|
val differenceY = abs(startY - endY)
|
||||||
|
return !(differenceX > scaledTouchSlop || differenceY > scaledTouchSlop)
|
||||||
}
|
}
|
||||||
}
|
}
|
Loading…
Add table
Add a link
Reference in a new issue