allow closing VideoPlayerActivity with Down gestures

This commit is contained in:
tibbi 2019-01-02 16:34:03 +01:00
parent 5b14e0dd08
commit 3fb5ebeee4

View file

@ -7,10 +7,7 @@ import android.graphics.drawable.ColorDrawable
import android.net.Uri
import android.os.Bundle
import android.util.DisplayMetrics
import android.view.Menu
import android.view.MenuItem
import android.view.View
import android.view.WindowManager
import android.view.*
import android.widget.SeekBar
import com.simplemobiletools.commons.extensions.beInvisibleIf
import com.simplemobiletools.commons.extensions.getFilenameFromUri
@ -27,6 +24,11 @@ open class VideoPlayerActivity : SimpleActivity(), SeekBar.OnSeekBarChangeListen
private var mIsFullscreen = false
private var mUri: Uri? = null
private var mTouchDownX = 0f
private var mTouchDownY = 0f
private var mCloseDownThreshold = 100f
private var mIgnoreCloseDown = false
public override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_video_player)
@ -75,6 +77,13 @@ open class VideoPlayerActivity : SimpleActivity(), SeekBar.OnSeekBarChangeListen
video_player_holder.setOnClickListener {
fullscreenToggled(!mIsFullscreen)
}
if (config.allowDownGesture) {
video_player_holder.setOnTouchListener { v, event ->
handleEvent(event)
false
}
}
}
override fun onCreateOptionsMenu(menu: Menu): Boolean {
@ -151,6 +160,25 @@ open class VideoPlayerActivity : SimpleActivity(), SeekBar.OnSeekBarChangeListen
}
private fun handleEvent(event: MotionEvent) {
when (event.actionMasked) {
MotionEvent.ACTION_DOWN -> {
mTouchDownX = event.x
mTouchDownY = event.y
}
MotionEvent.ACTION_POINTER_DOWN -> mIgnoreCloseDown = true
MotionEvent.ACTION_UP -> {
val diffX = mTouchDownX - event.x
val diffY = mTouchDownY - event.y
if (!mIgnoreCloseDown && Math.abs(diffY) > Math.abs(diffX) && diffY < -mCloseDownThreshold) {
supportFinishAfterTransition()
}
mIgnoreCloseDown = false
}
}
}
override fun onProgressChanged(seekBar: SeekBar?, progress: Int, fromUser: Boolean) {
}