Enabled swipe left/right to toggle between accounts
This commit is contained in:
@ -456,6 +456,40 @@ class MainActivity : AppCompatActivity() {
|
||||
}
|
||||
}
|
||||
|
||||
swipeRefresh.setOnTouchListener(object : OnSwipeTouchListener(this@MainActivity) {
|
||||
|
||||
override fun onSwipeLeft() {
|
||||
super.onSwipeLeft()
|
||||
if (UserAgent.uas().size > 0) {
|
||||
val curPos = aorSpinner.selectedItemPosition
|
||||
val newPos = if (curPos == -1)
|
||||
0
|
||||
else
|
||||
(curPos + 1) % UserAgent.uas().size
|
||||
if (curPos != newPos) {
|
||||
aorSpinner.setSelection(newPos)
|
||||
showCall(UserAgent.uas()[newPos])
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
override fun onSwipeRight() {
|
||||
super.onSwipeRight()
|
||||
if (UserAgent.uas().size > 0) {
|
||||
val curPos = aorSpinner.selectedItemPosition
|
||||
val newPos = when (curPos) {
|
||||
-1 -> 0
|
||||
0 -> UserAgent.uas().size - 1
|
||||
else -> curPos - 1
|
||||
}
|
||||
if (curPos != newPos) {
|
||||
aorSpinner.setSelection(newPos)
|
||||
showCall(UserAgent.uas()[newPos])
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
swipeRefresh.setOnRefreshListener {
|
||||
if (UserAgent.uas().size > 0) {
|
||||
if (aorSpinner.selectedItemPosition == -1)
|
||||
|
||||
@ -0,0 +1,70 @@
|
||||
package com.tutpro.baresip
|
||||
|
||||
import android.content.Context
|
||||
import android.view.GestureDetector
|
||||
import android.view.MotionEvent
|
||||
import android.view.View
|
||||
|
||||
open class OnSwipeTouchListener(ctx: Context) : View.OnTouchListener {
|
||||
|
||||
private val gestureDetector: GestureDetector
|
||||
|
||||
companion object {
|
||||
private val SWIPE_THRESHOLD = 100
|
||||
private val SWIPE_VELOCITY_THRESHOLD = 100
|
||||
}
|
||||
|
||||
init {
|
||||
gestureDetector = GestureDetector(ctx, GestureListener())
|
||||
}
|
||||
|
||||
override fun onTouch(v: View, event: MotionEvent): Boolean {
|
||||
return gestureDetector.onTouchEvent(event)
|
||||
}
|
||||
|
||||
private inner class GestureListener : GestureDetector.SimpleOnGestureListener() {
|
||||
|
||||
override fun onDown(e: MotionEvent): Boolean {
|
||||
return true
|
||||
}
|
||||
|
||||
override fun onFling(e1: MotionEvent, e2: MotionEvent, velocityX: Float, velocityY: Float): Boolean {
|
||||
var result = false
|
||||
try {
|
||||
val diffY = e2.y - e1.y
|
||||
val diffX = e2.x - e1.x
|
||||
if (Math.abs(diffX) > Math.abs(diffY)) {
|
||||
if (Math.abs(diffX) > SWIPE_THRESHOLD && Math.abs(velocityX) > SWIPE_VELOCITY_THRESHOLD) {
|
||||
if (diffX > 0) {
|
||||
onSwipeRight()
|
||||
} else {
|
||||
onSwipeLeft()
|
||||
}
|
||||
result = true
|
||||
}
|
||||
} else if (Math.abs(diffY) > SWIPE_THRESHOLD && Math.abs(velocityY) > SWIPE_VELOCITY_THRESHOLD) {
|
||||
if (diffY > 0) {
|
||||
onSwipeBottom()
|
||||
result = false
|
||||
} else {
|
||||
onSwipeTop()
|
||||
result = true
|
||||
}
|
||||
}
|
||||
} catch (exception: Exception) {
|
||||
exception.printStackTrace()
|
||||
}
|
||||
|
||||
return result
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
open fun onSwipeRight() {}
|
||||
|
||||
open fun onSwipeLeft() {}
|
||||
|
||||
open fun onSwipeTop() {}
|
||||
|
||||
open fun onSwipeBottom() {}
|
||||
}
|
||||
Reference in New Issue
Block a user