From 3ee3887754309579f789c6f1ef355bc2757ed435 Mon Sep 17 00:00:00 2001
From: tibbi <tibor@kaputa.sk>
Date: Sun, 21 Oct 2018 21:01:03 +0200
Subject: [PATCH] add skip forward/backward at panorama videos

---
 .../activities/PanoramaVideoActivity.kt       | 22 ++++++++++++++++++-
 .../gallery/fragments/VideoFragment.kt        |  1 -
 .../gallery/helpers/Constants.kt              |  1 +
 3 files changed, 22 insertions(+), 2 deletions(-)

diff --git a/app/src/main/kotlin/com/simplemobiletools/gallery/activities/PanoramaVideoActivity.kt b/app/src/main/kotlin/com/simplemobiletools/gallery/activities/PanoramaVideoActivity.kt
index 6187d08ed..194554028 100644
--- a/app/src/main/kotlin/com/simplemobiletools/gallery/activities/PanoramaVideoActivity.kt
+++ b/app/src/main/kotlin/com/simplemobiletools/gallery/activities/PanoramaVideoActivity.kt
@@ -25,6 +25,7 @@ import com.simplemobiletools.gallery.extensions.hideSystemUI
 import com.simplemobiletools.gallery.extensions.navigationBarHeight
 import com.simplemobiletools.gallery.extensions.showSystemUI
 import com.simplemobiletools.gallery.helpers.HIDE_PLAY_PAUSE_DELAY
+import com.simplemobiletools.gallery.helpers.MIN_SKIP_LENGTH
 import com.simplemobiletools.gallery.helpers.PATH
 import com.simplemobiletools.gallery.helpers.PLAY_PAUSE_VISIBLE_ALPHA
 import kotlinx.android.synthetic.main.activity_panorama_video.*
@@ -113,6 +114,9 @@ open class PanoramaVideoActivity : SimpleActivity(), SeekBar.OnSeekBarChangeList
 
         intent.removeExtra(PATH)
 
+        video_curr_time.setOnClickListener { skip(false) }
+        video_duration.setOnClickListener { skip(true) }
+
         try {
             val options = VrVideoView.Options()
             options.inputType = VrVideoView.Options.TYPE_MONO
@@ -209,6 +213,7 @@ open class PanoramaVideoActivity : SimpleActivity(), SeekBar.OnSeekBarChangeList
     }
 
     private fun playVideo() {
+        video_play_outline.setImageResource(R.drawable.ic_pause)
         if (mCurrTime == mDuration) {
             setVideoProgress(0)
             mPlayOnReady = true
@@ -216,7 +221,6 @@ open class PanoramaVideoActivity : SimpleActivity(), SeekBar.OnSeekBarChangeList
         }
 
         vr_video_view.playVideo()
-        video_play_outline.setImageResource(R.drawable.ic_pause)
         window.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON)
     }
 
@@ -286,6 +290,22 @@ open class PanoramaVideoActivity : SimpleActivity(), SeekBar.OnSeekBarChangeList
         }
     }
 
+    private fun skip(forward: Boolean) {
+        if (forward && mCurrTime == mDuration) {
+            return
+        }
+
+        val curr = vr_video_view.currentPosition
+        val twoPercents = Math.max((vr_video_view.duration / 50).toInt(), MIN_SKIP_LENGTH)
+        val newProgress = if (forward) curr + twoPercents else curr - twoPercents
+        val roundProgress = Math.round(newProgress / 1000f)
+        val limitedProgress = Math.max(Math.min(vr_video_view.duration.toInt(), roundProgress), 0)
+        setVideoProgress(limitedProgress)
+        if (!mIsPlaying) {
+            togglePlayPause()
+        }
+    }
+
     override fun onProgressChanged(seekBar: SeekBar?, progress: Int, fromUser: Boolean) {
         if (fromUser) {
             setVideoProgress(progress)
diff --git a/app/src/main/kotlin/com/simplemobiletools/gallery/fragments/VideoFragment.kt b/app/src/main/kotlin/com/simplemobiletools/gallery/fragments/VideoFragment.kt
index 6d797c2ca..af176e24a 100644
--- a/app/src/main/kotlin/com/simplemobiletools/gallery/fragments/VideoFragment.kt
+++ b/app/src/main/kotlin/com/simplemobiletools/gallery/fragments/VideoFragment.kt
@@ -38,7 +38,6 @@ import java.io.File
 
 class VideoFragment : ViewPagerFragment(), TextureView.SurfaceTextureListener, SeekBar.OnSeekBarChangeListener {
     private val PROGRESS = "progress"
-    private val MIN_SKIP_LENGTH = 2000
 
     private var mTextureView: TextureView? = null
     private var mCurrTimeView: TextView? = null
diff --git a/app/src/main/kotlin/com/simplemobiletools/gallery/helpers/Constants.kt b/app/src/main/kotlin/com/simplemobiletools/gallery/helpers/Constants.kt
index 79a0b37f7..1208b47a8 100644
--- a/app/src/main/kotlin/com/simplemobiletools/gallery/helpers/Constants.kt
+++ b/app/src/main/kotlin/com/simplemobiletools/gallery/helpers/Constants.kt
@@ -88,6 +88,7 @@ const val DRAG_THRESHOLD = 8
 const val MONTH_MILLISECONDS = MONTH_SECONDS * 1000L
 const val HIDE_PLAY_PAUSE_DELAY = 500L
 const val PLAY_PAUSE_VISIBLE_ALPHA = 0.8f
+const val MIN_SKIP_LENGTH = 2000
 
 const val DIRECTORY = "directory"
 const val MEDIUM = "medium"