avoid VideoFragment implementing so many interfaces
This commit is contained in:
parent
f2bcac6298
commit
0f0b3cfc30
1 changed files with 30 additions and 44 deletions
|
@ -3,7 +3,6 @@ package com.simplemobiletools.gallery.fragments
|
||||||
import android.content.res.Configuration
|
import android.content.res.Configuration
|
||||||
import android.media.AudioManager
|
import android.media.AudioManager
|
||||||
import android.media.MediaPlayer
|
import android.media.MediaPlayer
|
||||||
import android.media.MediaPlayer.OnPreparedListener
|
|
||||||
import android.net.Uri
|
import android.net.Uri
|
||||||
import android.os.Build
|
import android.os.Build
|
||||||
import android.os.Bundle
|
import android.os.Bundle
|
||||||
|
@ -24,8 +23,7 @@ import kotlinx.android.synthetic.main.pager_video_item.view.*
|
||||||
import java.io.IOException
|
import java.io.IOException
|
||||||
import java.util.*
|
import java.util.*
|
||||||
|
|
||||||
class VideoFragment : ViewPagerFragment(), View.OnClickListener, SurfaceHolder.Callback, MediaPlayer.OnCompletionListener,
|
class VideoFragment : ViewPagerFragment(), SurfaceHolder.Callback, SeekBar.OnSeekBarChangeListener {
|
||||||
MediaPlayer.OnVideoSizeChangedListener, SeekBar.OnSeekBarChangeListener, OnPreparedListener {
|
|
||||||
|
|
||||||
private var mMediaPlayer: MediaPlayer? = null
|
private var mMediaPlayer: MediaPlayer? = null
|
||||||
private var mSurfaceView: SurfaceView? = null
|
private var mSurfaceView: SurfaceView? = null
|
||||||
|
@ -60,7 +58,6 @@ class VideoFragment : ViewPagerFragment(), View.OnClickListener, SurfaceHolder.C
|
||||||
|
|
||||||
mIsFullscreen = activity.window.decorView.systemUiVisibility and View.SYSTEM_UI_FLAG_FULLSCREEN == View.SYSTEM_UI_FLAG_FULLSCREEN
|
mIsFullscreen = activity.window.decorView.systemUiVisibility and View.SYSTEM_UI_FLAG_FULLSCREEN == View.SYSTEM_UI_FLAG_FULLSCREEN
|
||||||
setupPlayer()
|
setupPlayer()
|
||||||
mView.setOnClickListener(this)
|
|
||||||
|
|
||||||
activity.window.decorView.setOnSystemUiVisibilityChangeListener { visibility ->
|
activity.window.decorView.setOnSystemUiVisibilityChangeListener { visibility ->
|
||||||
val fullscreen = visibility and View.SYSTEM_UI_FLAG_FULLSCREEN != 0
|
val fullscreen = visibility and View.SYSTEM_UI_FLAG_FULLSCREEN != 0
|
||||||
|
@ -76,12 +73,16 @@ class VideoFragment : ViewPagerFragment(), View.OnClickListener, SurfaceHolder.C
|
||||||
if (activity == null)
|
if (activity == null)
|
||||||
return
|
return
|
||||||
|
|
||||||
mView.video_play_outline.setOnClickListener(this)
|
mView.video_play_outline.setOnClickListener { togglePlayPause() }
|
||||||
|
|
||||||
mSurfaceView = mView.video_surface
|
mSurfaceView = mView.video_surface
|
||||||
mSurfaceView!!.setOnClickListener(this)
|
|
||||||
mSurfaceHolder = mSurfaceView!!.holder
|
mSurfaceHolder = mSurfaceView!!.holder
|
||||||
mSurfaceHolder!!.addCallback(this)
|
mSurfaceHolder!!.addCallback(this)
|
||||||
|
mSurfaceView!!.setOnClickListener({
|
||||||
|
mIsFullscreen = !mIsFullscreen
|
||||||
|
checkFullscreen()
|
||||||
|
listener?.fragmentClicked()
|
||||||
|
})
|
||||||
|
|
||||||
initTimeHolder()
|
initTimeHolder()
|
||||||
}
|
}
|
||||||
|
@ -157,17 +158,6 @@ class VideoFragment : ViewPagerFragment(), View.OnClickListener, SurfaceHolder.C
|
||||||
outState.putInt(PROGRESS, mCurrTime)
|
outState.putInt(PROGRESS, mCurrTime)
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun onClick(v: View) {
|
|
||||||
when (v.id) {
|
|
||||||
R.id.video_play_outline -> togglePlayPause()
|
|
||||||
else -> {
|
|
||||||
mIsFullscreen = !mIsFullscreen
|
|
||||||
checkFullscreen()
|
|
||||||
listener?.fragmentClicked()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private fun checkFullscreen() {
|
private fun checkFullscreen() {
|
||||||
var anim = android.R.anim.fade_in
|
var anim = android.R.anim.fade_in
|
||||||
if (mIsFullscreen) {
|
if (mIsFullscreen) {
|
||||||
|
@ -218,9 +208,9 @@ class VideoFragment : ViewPagerFragment(), View.OnClickListener, SurfaceHolder.C
|
||||||
mMediaPlayer = MediaPlayer().apply {
|
mMediaPlayer = MediaPlayer().apply {
|
||||||
setDataSource(context, Uri.parse(mMedium.path))
|
setDataSource(context, Uri.parse(mMedium.path))
|
||||||
setDisplay(mSurfaceHolder)
|
setDisplay(mSurfaceHolder)
|
||||||
setOnCompletionListener(this@VideoFragment)
|
setOnCompletionListener { videoCompleted() }
|
||||||
setOnVideoSizeChangedListener(this@VideoFragment)
|
setOnVideoSizeChangedListener({ mediaPlayer, width, height -> setVideoSize() })
|
||||||
setOnPreparedListener(this@VideoFragment)
|
setOnPreparedListener { videoPrepared(it) }
|
||||||
setAudioStreamType(AudioManager.STREAM_MUSIC)
|
setAudioStreamType(AudioManager.STREAM_MUSIC)
|
||||||
prepareAsync()
|
prepareAsync()
|
||||||
}
|
}
|
||||||
|
@ -262,6 +252,26 @@ class VideoFragment : ViewPagerFragment(), View.OnClickListener, SurfaceHolder.C
|
||||||
mTimerHandler?.removeCallbacksAndMessages(null)
|
mTimerHandler?.removeCallbacksAndMessages(null)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private fun videoPrepared(mediaPlayer: MediaPlayer) {
|
||||||
|
mDuration = mediaPlayer.duration / 1000
|
||||||
|
addPreviewImage()
|
||||||
|
setupTimeHolder()
|
||||||
|
setProgress(mCurrTime)
|
||||||
|
|
||||||
|
if (mIsFragmentVisible && context.config.autoplayVideos)
|
||||||
|
playVideo()
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun videoCompleted() {
|
||||||
|
if (context.config.loopVideos) {
|
||||||
|
playVideo()
|
||||||
|
} else {
|
||||||
|
mSeekBar!!.progress = mSeekBar!!.max
|
||||||
|
mCurrTimeView!!.text = getTimeString(mDuration)
|
||||||
|
pauseVideo()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
override fun surfaceCreated(holder: SurfaceHolder) {
|
override fun surfaceCreated(holder: SurfaceHolder) {
|
||||||
initMediaPlayer()
|
initMediaPlayer()
|
||||||
}
|
}
|
||||||
|
@ -275,20 +285,6 @@ class VideoFragment : ViewPagerFragment(), View.OnClickListener, SurfaceHolder.C
|
||||||
mMediaPlayer = null
|
mMediaPlayer = null
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun onCompletion(mp: MediaPlayer) {
|
|
||||||
if (context.config.loopVideos) {
|
|
||||||
playVideo()
|
|
||||||
} else {
|
|
||||||
mSeekBar!!.progress = mSeekBar!!.max
|
|
||||||
mCurrTimeView!!.text = getTimeString(mDuration)
|
|
||||||
pauseVideo()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
override fun onVideoSizeChanged(mp: MediaPlayer, width: Int, height: Int) {
|
|
||||||
setVideoSize()
|
|
||||||
}
|
|
||||||
|
|
||||||
private fun setVideoSize() {
|
private fun setVideoSize() {
|
||||||
if (activity == null)
|
if (activity == null)
|
||||||
return
|
return
|
||||||
|
@ -360,14 +356,4 @@ class VideoFragment : ViewPagerFragment(), View.OnClickListener, SurfaceHolder.C
|
||||||
|
|
||||||
mIsDragged = false
|
mIsDragged = false
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun onPrepared(mp: MediaPlayer) {
|
|
||||||
mDuration = mp.duration / 1000
|
|
||||||
addPreviewImage()
|
|
||||||
setupTimeHolder()
|
|
||||||
setProgress(mCurrTime)
|
|
||||||
|
|
||||||
if (mIsFragmentVisible && context.config.autoplayVideos)
|
|
||||||
playVideo()
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue