properly handle launching videos in slideshows

This commit is contained in:
tibbi 2017-08-05 15:53:02 +02:00
parent 74e6c69473
commit 3f1cfab633
4 changed files with 70 additions and 33 deletions

View file

@ -162,4 +162,6 @@ open class PhotoVideoActivity : SimpleActivity(), ViewPagerFragment.FragmentList
showSystemUI() showSystemUI()
} }
} }
override fun videoEnded() = false
} }

View file

@ -32,6 +32,7 @@ import com.simplemobiletools.gallery.dialogs.SaveAsDialog
import com.simplemobiletools.gallery.dialogs.SlideshowDialog import com.simplemobiletools.gallery.dialogs.SlideshowDialog
import com.simplemobiletools.gallery.extensions.* import com.simplemobiletools.gallery.extensions.*
import com.simplemobiletools.gallery.fragments.PhotoFragment import com.simplemobiletools.gallery.fragments.PhotoFragment
import com.simplemobiletools.gallery.fragments.VideoFragment
import com.simplemobiletools.gallery.fragments.ViewPagerFragment import com.simplemobiletools.gallery.fragments.ViewPagerFragment
import com.simplemobiletools.gallery.helpers.* import com.simplemobiletools.gallery.helpers.*
import com.simplemobiletools.gallery.models.Medium import com.simplemobiletools.gallery.models.Medium
@ -56,6 +57,8 @@ class ViewPagerActivity : SimpleActivity(), ViewPager.OnPageChangeListener, View
private var mSlideshowHandler = Handler() private var mSlideshowHandler = Handler()
private var mSlideshowInterval = SLIDESHOW_DEFAULT_INTERVAL private var mSlideshowInterval = SLIDESHOW_DEFAULT_INTERVAL
private var mSlideshowMoveBackwards = false private var mSlideshowMoveBackwards = false
private var mSlideshowMedia = mutableListOf<Medium>()
private var mAreSlideShowMediaVisible = false
companion object { companion object {
var screenWidth = 0 var screenWidth = 0
@ -262,12 +265,20 @@ class ViewPagerActivity : SimpleActivity(), ViewPager.OnPageChangeListener, View
private fun startSlideshow() { private fun startSlideshow() {
if (getMediaForSlideshow()) { if (getMediaForSlideshow()) {
view_pager.viewTreeObserver.addOnGlobalLayoutListener(object : ViewTreeObserver.OnGlobalLayoutListener {
override fun onGlobalLayout() {
view_pager.viewTreeObserver.removeOnGlobalLayoutListener(this)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1 && isDestroyed)
return
hideSystemUI() hideSystemUI()
mSlideshowInterval = config.slideshowInterval mSlideshowInterval = config.slideshowInterval
mSlideshowMoveBackwards = config.slideshowMoveBackwards mSlideshowMoveBackwards = config.slideshowMoveBackwards
mIsSlideshowActive = true mIsSlideshowActive = true
scheduleSwipe()
window.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON) window.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON)
scheduleSwipe()
}
})
} }
} }
@ -283,8 +294,19 @@ class ViewPagerActivity : SimpleActivity(), ViewPager.OnPageChangeListener, View
private fun scheduleSwipe() { private fun scheduleSwipe() {
mSlideshowHandler.removeCallbacksAndMessages(null) mSlideshowHandler.removeCallbacksAndMessages(null)
if (mIsSlideshowActive) { if (mIsSlideshowActive) {
if (getCurrentMedium()!!.isImage() || getCurrentMedium()!!.isGif()) {
mSlideshowHandler.postDelayed({ mSlideshowHandler.postDelayed({
if (mIsSlideshowActive && Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1 && !isDestroyed) { if (mIsSlideshowActive && Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1 && !isDestroyed) {
swipeToNextMedium()
}
}, mSlideshowInterval * 1000L)
} else {
(getCurrentFragment() as? VideoFragment)!!.playVideo()
}
}
}
private fun swipeToNextMedium() {
val before = view_pager.currentItem val before = view_pager.currentItem
view_pager.currentItem = if (mSlideshowMoveBackwards) --view_pager.currentItem else ++view_pager.currentItem view_pager.currentItem = if (mSlideshowMoveBackwards) --view_pager.currentItem else ++view_pager.currentItem
if (before == view_pager.currentItem) { if (before == view_pager.currentItem) {
@ -292,29 +314,27 @@ class ViewPagerActivity : SimpleActivity(), ViewPager.OnPageChangeListener, View
toast(R.string.slideshow_ended) toast(R.string.slideshow_ended)
} }
} }
}, mSlideshowInterval * 1000L)
}
}
private fun getMediaForSlideshow(): Boolean { private fun getMediaForSlideshow(): Boolean {
var slideshowMedia = mMedia.toMutableList() mSlideshowMedia = mMedia.toMutableList()
if (!config.slideshowIncludeVideos) { if (!config.slideshowIncludeVideos) {
slideshowMedia = slideshowMedia.filter { it.isImage() || it.isGif() } as MutableList mSlideshowMedia = mSlideshowMedia.filter { it.isImage() || it.isGif() } as MutableList
} }
if (config.slideshowRandomOrder) { if (config.slideshowRandomOrder) {
Collections.shuffle(slideshowMedia) Collections.shuffle(mSlideshowMedia)
mPos = 0 mPos = 0
} else { } else {
mPath = getCurrentPath() mPath = getCurrentPath()
mPos = getPositionInList(slideshowMedia) mPos = getPositionInList(mSlideshowMedia)
} }
return if (slideshowMedia.isEmpty()) { return if (mSlideshowMedia.isEmpty()) {
toast(R.string.no_media_for_slideshow) toast(R.string.no_media_for_slideshow)
false false
} else { } else {
updatePagerItems(slideshowMedia) updatePagerItems(mSlideshowMedia)
mAreSlideShowMediaVisible = true
true true
} }
} }
@ -336,7 +356,7 @@ class ViewPagerActivity : SimpleActivity(), ViewPager.OnPageChangeListener, View
getCurrentMedium()!!.apply { getCurrentMedium()!!.apply {
name = newFileName name = newFileName
path = it.absolutePath path = it.absolutePath
mMedia[mPos] = this getCurrentMedia()[mPos] = this
} }
invalidateOptionsMenu() invalidateOptionsMenu()
} }
@ -498,7 +518,7 @@ class ViewPagerActivity : SimpleActivity(), ViewPager.OnPageChangeListener, View
private fun askConfirmDelete() { private fun askConfirmDelete() {
ConfirmationDialog(this) { ConfirmationDialog(this) {
deleteFileBg(File(mMedia[mPos].path)) { deleteFileBg(File(getCurrentMedia()[mPos].path)) {
reloadViewPager() reloadViewPager()
} }
} }
@ -515,7 +535,7 @@ class ViewPagerActivity : SimpleActivity(), ViewPager.OnPageChangeListener, View
private fun renameFile() { private fun renameFile() {
RenameItemDialog(this, getCurrentPath()) { RenameItemDialog(this, getCurrentPath()) {
mMedia[mPos].path = it getCurrentMedia()[mPos].path = it
updateActionbarTitle() updateActionbarTitle()
} }
} }
@ -600,6 +620,12 @@ class ViewPagerActivity : SimpleActivity(), ViewPager.OnPageChangeListener, View
checkSystemUI() checkSystemUI()
} }
override fun videoEnded(): Boolean {
if (mIsSlideshowActive)
swipeToNextMedium()
return mIsSlideshowActive
}
private fun checkSystemUI() { private fun checkSystemUI() {
if (mIsFullScreen) { if (mIsFullScreen) {
hideSystemUI() hideSystemUI()
@ -611,19 +637,21 @@ class ViewPagerActivity : SimpleActivity(), ViewPager.OnPageChangeListener, View
private fun updateActionbarTitle() { private fun updateActionbarTitle() {
runOnUiThread { runOnUiThread {
if (mPos < mMedia.size) { if (mPos < getCurrentMedia().size) {
title = mMedia[mPos].path.getFilenameFromPath() title = getCurrentMedia()[mPos].path.getFilenameFromPath()
} }
} }
} }
private fun getCurrentMedium(): Medium? { private fun getCurrentMedium(): Medium? {
return if (mMedia.isEmpty() || mPos == -1) return if (getCurrentMedia().isEmpty() || mPos == -1)
null null
else else
mMedia[Math.min(mPos, mMedia.size - 1)] getCurrentMedia()[Math.min(mPos, getCurrentMedia().size - 1)]
} }
private fun getCurrentMedia() = if (mAreSlideShowMediaVisible) mSlideshowMedia else mMedia
private fun getCurrentPath() = getCurrentMedium()!!.path private fun getCurrentPath() = getCurrentMedium()!!.path
private fun getCurrentFile() = File(getCurrentPath()) private fun getCurrentFile() = File(getCurrentPath())

View file

@ -39,6 +39,7 @@ class VideoFragment : ViewPagerFragment(), SurfaceHolder.Callback, SeekBar.OnSee
private var mIsDragged = false private var mIsDragged = false
private var mIsFullscreen = false private var mIsFullscreen = false
private var mIsFragmentVisible = false private var mIsFragmentVisible = false
private var mPlayOnPrepare = false
private var mCurrTime = 0 private var mCurrTime = 0
private var mDuration = 0 private var mDuration = 0
@ -194,9 +195,13 @@ class VideoFragment : ViewPagerFragment(), SurfaceHolder.Callback, SeekBar.OnSee
} }
} }
private fun playVideo() { fun playVideo() {
if (mMediaPlayer != null) {
mIsPlaying = true mIsPlaying = true
mMediaPlayer?.start() mMediaPlayer?.start()
} else {
mPlayOnPrepare = true
}
mView.video_play_outline.setImageDrawable(null) mView.video_play_outline.setImageDrawable(null)
activity.window.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON) activity.window.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON)
} }
@ -271,12 +276,12 @@ class VideoFragment : ViewPagerFragment(), SurfaceHolder.Callback, SeekBar.OnSee
setupTimeHolder() setupTimeHolder()
setProgress(mCurrTime) setProgress(mCurrTime)
if (mIsFragmentVisible && context.config.autoplayVideos) if (mIsFragmentVisible && (context.config.autoplayVideos || mPlayOnPrepare))
playVideo() playVideo()
} }
private fun videoCompleted() { private fun videoCompleted() {
if (context.config.loopVideos) { if (listener?.videoEnded() == false && context.config.loopVideos) {
playVideo() playVideo()
} else { } else {
mSeekBar!!.progress = mSeekBar!!.max mSeekBar!!.progress = mSeekBar!!.max

View file

@ -9,5 +9,7 @@ abstract class ViewPagerFragment : Fragment() {
interface FragmentListener { interface FragmentListener {
fun fragmentClicked() fun fragmentClicked()
fun videoEnded(): Boolean
} }
} }