Move side scroll functionality to MediaSideScroll.kt

This commit is contained in:
Nikola Trubitsyn 2018-02-02 16:28:13 +03:00
parent fe7b425d79
commit ff66f36cda
2 changed files with 168 additions and 141 deletions

View file

@ -7,7 +7,6 @@ import android.net.Uri
import android.os.Build
import android.os.Bundle
import android.os.Handler
import android.provider.Settings
import android.support.annotation.RequiresApi
import android.util.DisplayMetrics
import android.view.*
@ -17,16 +16,17 @@ import android.widget.TextView
import com.simplemobiletools.commons.extensions.*
import com.simplemobiletools.gallery.R
import com.simplemobiletools.gallery.activities.VideoActivity
import com.simplemobiletools.gallery.activities.ViewPagerActivity
import com.simplemobiletools.gallery.extensions.*
import com.simplemobiletools.gallery.extensions.config
import com.simplemobiletools.gallery.extensions.navigationBarHeight
import com.simplemobiletools.gallery.extensions.usableScreenSize
import com.simplemobiletools.gallery.extensions.windowManager
import com.simplemobiletools.gallery.helpers.MEDIUM
import com.simplemobiletools.gallery.helpers.MediaSideScroll
import com.simplemobiletools.gallery.models.Medium
import kotlinx.android.synthetic.main.pager_video_item.view.*
import java.io.IOException
class VideoFragment : ViewPagerFragment(), SurfaceHolder.Callback, SeekBar.OnSeekBarChangeListener {
private val CLICK_MAX_DURATION = 150
private val SLIDE_INFO_FADE_DELAY = 1000L
private val PROGRESS = "progress"
private var mMediaPlayer: MediaPlayer? = null
@ -50,21 +50,12 @@ class VideoFragment : ViewPagerFragment(), SurfaceHolder.Callback, SeekBar.OnSee
private var mDuration = 0
private var mEncodedPath = ""
private var mTouchDownX = 0f
private var mTouchDownY = 0f
private var mTouchDownTime = 0L
private var mTouchDownVolume = 0
private var mTouchDownBrightness = -1
private var mTempBrightness = 0
private var mLastTouchY = 0f
private var mSlideInfoText = ""
private var mSlideInfoFadeHandler = Handler()
private var mStoredShowExtendedDetails = false
private var mStoredHideExtendedDetails = false
private var mStoredExtendedDetails = 0
private lateinit var mediaSideScroll: MediaSideScroll
lateinit var medium: Medium
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
@ -94,6 +85,13 @@ class VideoFragment : ViewPagerFragment(), SurfaceHolder.Callback, SeekBar.OnSee
return mView
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
mediaSideScroll = MediaSideScroll(context!!, activity!!, view.slide_info, {
view.video_holder.performClick()
})
}
override fun onResume() {
super.onResume()
activity!!.updateTextColors(mView!!.video_holder)
@ -145,13 +143,15 @@ class VideoFragment : ViewPagerFragment(), SurfaceHolder.Callback, SeekBar.OnSee
mSurfaceHolder!!.addCallback(this)
mSurfaceView!!.setOnClickListener { toggleFullscreen() }
mView!!.video_holder.setOnClickListener { toggleFullscreen() }
mView!!.video_volume_controller.setOnTouchListener { v, event ->
handleVolumeTouched(event)
mediaSideScroll.handleVolumeTouched(event)
true
true
}
mView!!.video_brightness_controller.setOnTouchListener { v, event ->
handleBrightnessTouched(event)
mediaSideScroll.handleBrightnessTouched(event)
true
}
@ -186,129 +186,6 @@ class VideoFragment : ViewPagerFragment(), SurfaceHolder.Callback, SeekBar.OnSee
listener?.fragmentClicked()
}
private fun handleVolumeTouched(event: MotionEvent) {
when (event.action) {
MotionEvent.ACTION_DOWN -> {
mTouchDownX = event.x
mTouchDownY = event.y
mLastTouchY = event.y
mTouchDownTime = System.currentTimeMillis()
mTouchDownVolume = getCurrentVolume()
mSlideInfoText = "${getString(R.string.volume)}:\n"
}
MotionEvent.ACTION_MOVE -> {
val diffX = mTouchDownX - event.x
val diffY = mTouchDownY - event.y
if (Math.abs(diffY) > 20 && Math.abs(diffY) > Math.abs(diffX)) {
var percent = ((diffY / ViewPagerActivity.screenHeight) * 100).toInt() * 3
percent = Math.min(100, Math.max(-100, percent))
if ((percent == 100 && event.y > mLastTouchY) || (percent == -100 && event.y < mLastTouchY)) {
mTouchDownY = event.y
mTouchDownVolume = getCurrentVolume()
}
volumePercentChanged(percent)
}
mLastTouchY = event.y
}
MotionEvent.ACTION_UP, MotionEvent.ACTION_CANCEL -> {
val diffX = Math.abs(event.x - mTouchDownX)
val diffY = Math.abs(event.y - mTouchDownY)
if (System.currentTimeMillis() - mTouchDownTime < CLICK_MAX_DURATION && diffX < 20 && diffY < 20) {
mView!!.video_holder.performClick()
}
}
}
}
private fun handleBrightnessTouched(event: MotionEvent) {
when (event.action) {
MotionEvent.ACTION_DOWN -> {
mTouchDownX = event.x
mTouchDownY = event.y
mLastTouchY = event.y
mTouchDownTime = System.currentTimeMillis()
mSlideInfoText = "${getString(R.string.brightness)}:\n"
if (mTouchDownBrightness == -1)
mTouchDownBrightness = getCurrentBrightness()
}
MotionEvent.ACTION_MOVE -> {
val diffX = mTouchDownX - event.x
val diffY = mTouchDownY - event.y
if (Math.abs(diffY) > 20 && Math.abs(diffY) > Math.abs(diffX)) {
var percent = ((diffY / ViewPagerActivity.screenHeight) * 100).toInt() * 3
percent = Math.min(100, Math.max(-100, percent))
if ((percent == 100 && event.y > mLastTouchY) || (percent == -100 && event.y < mLastTouchY)) {
mTouchDownY = event.y
mTouchDownBrightness = mTempBrightness
}
brightnessPercentChanged(percent)
}
mLastTouchY = event.y
}
MotionEvent.ACTION_UP, MotionEvent.ACTION_CANCEL -> {
val diffX = Math.abs(event.x - mTouchDownX)
val diffY = Math.abs(event.y - mTouchDownY)
if (System.currentTimeMillis() - mTouchDownTime < CLICK_MAX_DURATION && diffX < 20 && diffY < 20) {
mView!!.video_holder.performClick()
}
mTouchDownBrightness = mTempBrightness
}
}
mView!!.video_holder
}
private fun getCurrentVolume() = context!!.audioManager.getStreamVolume(AudioManager.STREAM_MUSIC)
private fun getCurrentBrightness() = Settings.System.getInt(activity!!.contentResolver, Settings.System.SCREEN_BRIGHTNESS)
private fun volumePercentChanged(percent: Int) {
val stream = AudioManager.STREAM_MUSIC
val maxVolume = context!!.audioManager.getStreamMaxVolume(stream)
val percentPerPoint = 100 / maxVolume
val addPoints = percent / percentPerPoint
val newVolume = Math.min(maxVolume, Math.max(0, mTouchDownVolume + addPoints))
context!!.audioManager.setStreamVolume(stream, newVolume, 0)
val absolutePercent = ((newVolume / maxVolume.toFloat()) * 100).toInt()
mView!!.slide_info.apply {
text = "$mSlideInfoText$absolutePercent%"
alpha = 1f
}
mSlideInfoFadeHandler.removeCallbacksAndMessages(null)
mSlideInfoFadeHandler.postDelayed({
mView!!.slide_info.animate().alpha(0f)
}, SLIDE_INFO_FADE_DELAY)
}
private fun brightnessPercentChanged(percent: Int) {
val maxBrightness = 255f
var newBrightness = (mTouchDownBrightness + 2.55 * percent).toFloat()
newBrightness = Math.min(maxBrightness, Math.max(0f, newBrightness))
mTempBrightness = newBrightness.toInt()
val absolutePercent = ((newBrightness / maxBrightness) * 100).toInt()
mView!!.slide_info.apply {
text = "$mSlideInfoText$absolutePercent%"
alpha = 1f
}
val attributes = activity!!.window.attributes
attributes.screenBrightness = absolutePercent / 100f
activity!!.window.attributes = attributes
mSlideInfoFadeHandler.removeCallbacksAndMessages(null)
mSlideInfoFadeHandler.postDelayed({
mView!!.slide_info.animate().alpha(0f)
}, SLIDE_INFO_FADE_DELAY)
}
private fun initTimeHolder() {
val res = resources
val height = context!!.navigationBarHeight

View file

@ -0,0 +1,150 @@
package com.simplemobiletools.gallery.helpers
import android.app.Activity
import android.content.Context
import android.media.AudioManager
import android.os.Handler
import android.provider.Settings
import android.view.MotionEvent
import android.widget.TextView
import com.simplemobiletools.gallery.R
import com.simplemobiletools.gallery.activities.ViewPagerActivity
import com.simplemobiletools.gallery.extensions.audioManager
class MediaSideScroll(val context: Context, val activity: Activity, val slideInfoView: TextView, val onSpecialEvent: () -> Unit) {
private val CLICK_MAX_DURATION = 150
private val SLIDE_INFO_FADE_DELAY = 1000L
private var mTouchDownX = 0f
private var mTouchDownY = 0f
private var mTouchDownTime = 0L
private var mTouchDownVolume = 0
private var mTouchDownBrightness = -1
private var mTempBrightness = 0
private var mLastTouchY = 0f
private var mSlideInfoText = ""
private var mSlideInfoFadeHandler = Handler()
fun handleVolumeTouched(event: MotionEvent) {
when (event.action) {
MotionEvent.ACTION_DOWN -> {
mTouchDownX = event.x
mTouchDownY = event.y
mLastTouchY = event.y
mTouchDownTime = System.currentTimeMillis()
mTouchDownVolume = getCurrentVolume()
mSlideInfoText = "${context.getString(R.string.volume)}:\n"
}
MotionEvent.ACTION_MOVE -> {
val diffX = mTouchDownX - event.x
val diffY = mTouchDownY - event.y
if (Math.abs(diffY) > 20 && Math.abs(diffY) > Math.abs(diffX)) {
var percent = ((diffY / ViewPagerActivity.screenHeight) * 100).toInt() * 3
percent = Math.min(100, Math.max(-100, percent))
if ((percent == 100 && event.y > mLastTouchY) || (percent == -100 && event.y < mLastTouchY)) {
mTouchDownY = event.y
mTouchDownVolume = getCurrentVolume()
}
volumePercentChanged(percent)
}
mLastTouchY = event.y
}
MotionEvent.ACTION_UP, MotionEvent.ACTION_CANCEL -> {
val diffX = Math.abs(event.x - mTouchDownX)
val diffY = Math.abs(event.y - mTouchDownY)
if (System.currentTimeMillis() - mTouchDownTime < CLICK_MAX_DURATION && diffX < 20 && diffY < 20) {
onSpecialEvent()
}
}
}
}
fun handleBrightnessTouched(event: MotionEvent) {
when (event.action) {
MotionEvent.ACTION_DOWN -> {
mTouchDownX = event.x
mTouchDownY = event.y
mLastTouchY = event.y
mTouchDownTime = System.currentTimeMillis()
mSlideInfoText = "${context.getString(R.string.brightness)}:\n"
if (mTouchDownBrightness == -1)
mTouchDownBrightness = getCurrentBrightness()
}
MotionEvent.ACTION_MOVE -> {
val diffX = mTouchDownX - event.x
val diffY = mTouchDownY - event.y
if (Math.abs(diffY) > 20 && Math.abs(diffY) > Math.abs(diffX)) {
var percent = ((diffY / ViewPagerActivity.screenHeight) * 100).toInt() * 3
percent = Math.min(100, Math.max(-100, percent))
if ((percent == 100 && event.y > mLastTouchY) || (percent == -100 && event.y < mLastTouchY)) {
mTouchDownY = event.y
mTouchDownBrightness = mTempBrightness
}
brightnessPercentChanged(percent)
}
mLastTouchY = event.y
}
MotionEvent.ACTION_UP, MotionEvent.ACTION_CANCEL -> {
val diffX = Math.abs(event.x - mTouchDownX)
val diffY = Math.abs(event.y - mTouchDownY)
if (System.currentTimeMillis() - mTouchDownTime < CLICK_MAX_DURATION && diffX < 20 && diffY < 20) {
onSpecialEvent()
}
mTouchDownBrightness = mTempBrightness
}
}
//mView.video_holder
}
private fun getCurrentVolume() = context.audioManager.getStreamVolume(AudioManager.STREAM_MUSIC)
private fun getCurrentBrightness() = Settings.System.getInt(activity.contentResolver, Settings.System.SCREEN_BRIGHTNESS)
private fun volumePercentChanged(percent: Int) {
val stream = AudioManager.STREAM_MUSIC
val maxVolume = context.audioManager.getStreamMaxVolume(stream)
val percentPerPoint = 100 / maxVolume
val addPoints = percent / percentPerPoint
val newVolume = Math.min(maxVolume, Math.max(0, mTouchDownVolume + addPoints))
context.audioManager.setStreamVolume(stream, newVolume, 0)
val absolutePercent = ((newVolume / maxVolume.toFloat()) * 100).toInt()
slideInfoView.apply {
text = "$mSlideInfoText$absolutePercent%"
alpha = 1f
}
mSlideInfoFadeHandler.removeCallbacksAndMessages(null)
mSlideInfoFadeHandler.postDelayed({
slideInfoView.animate().alpha(0f)
}, SLIDE_INFO_FADE_DELAY)
}
private fun brightnessPercentChanged(percent: Int) {
val maxBrightness = 255f
var newBrightness = (mTouchDownBrightness + 2.55 * percent).toFloat()
newBrightness = Math.min(maxBrightness, Math.max(0f, newBrightness))
mTempBrightness = newBrightness.toInt()
val absolutePercent = ((newBrightness / maxBrightness) * 100).toInt()
slideInfoView.apply {
text = "$mSlideInfoText$absolutePercent%"
alpha = 1f
}
val attributes = activity.window.attributes
attributes.screenBrightness = absolutePercent / 100f
activity.window.attributes = attributes
mSlideInfoFadeHandler.removeCallbacksAndMessages(null)
mSlideInfoFadeHandler.postDelayed({
slideInfoView.animate().alpha(0f)
}, SLIDE_INFO_FADE_DELAY)
}
}