handle touch events in MediaSideScroll itself

This commit is contained in:
tibbi 2018-02-09 17:51:42 +01:00
parent 16e30e5969
commit 4449b96d6e
4 changed files with 67 additions and 107 deletions

View file

@ -30,7 +30,6 @@ import com.simplemobiletools.gallery.activities.ViewPagerActivity
import com.simplemobiletools.gallery.extensions.* import com.simplemobiletools.gallery.extensions.*
import com.simplemobiletools.gallery.helpers.GlideRotateTransformation import com.simplemobiletools.gallery.helpers.GlideRotateTransformation
import com.simplemobiletools.gallery.helpers.MEDIUM import com.simplemobiletools.gallery.helpers.MEDIUM
import com.simplemobiletools.gallery.helpers.MediaSideScroll
import com.simplemobiletools.gallery.models.Medium import com.simplemobiletools.gallery.models.Medium
import it.sephiroth.android.library.exif2.ExifInterface import it.sephiroth.android.library.exif2.ExifInterface
import kotlinx.android.synthetic.main.pager_photo_item.view.* import kotlinx.android.synthetic.main.pager_photo_item.view.*
@ -50,8 +49,6 @@ class PhotoFragment : ViewPagerFragment() {
private var storedHideExtendedDetails = false private var storedHideExtendedDetails = false
private var storedExtendedDetails = 0 private var storedExtendedDetails = 0
private lateinit var brightnessSideScroll: MediaSideScroll
lateinit var view: ViewGroup lateinit var view: ViewGroup
lateinit var medium: Medium lateinit var medium: Medium
@ -65,9 +62,14 @@ class PhotoFragment : ViewPagerFragment() {
instant_prev_item.parentView = container instant_prev_item.parentView = container
instant_next_item.parentView = container instant_next_item.parentView = container
photo_brightness_controller.setOnTouchListener { v, event -> photo_brightness_controller.initialize(activity!!, slide_info, true, container) {
brightnessSideScroll.handleBrightnessTouched(event) view.apply {
true if (subsampling_view.isVisible()) {
subsampling_view.performClick()
} else {
gif_view.performClick()
}
}
} }
} }
@ -116,20 +118,6 @@ class PhotoFragment : ViewPagerFragment() {
return view return view
} }
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
brightnessSideScroll = view.photo_brightness_controller
brightnessSideScroll.initialize(activity!!, view.slide_info) {
view.apply {
if (subsampling_view.isVisible()) {
subsampling_view.performClick()
} else {
gif_view.performClick()
}
}
}
}
override fun onPause() { override fun onPause() {
super.onPause() super.onPause()
storeStateVariables() storeStateVariables()

View file

@ -83,22 +83,21 @@ class VideoFragment : ViewPagerFragment(), SurfaceHolder.Callback, SeekBar.OnSee
checkFullscreen() checkFullscreen()
wasInit = true wasInit = true
mView!!.apply {
brightnessSideScroll = video_brightness_controller
brightnessSideScroll.initialize(activity!!, slide_info, true, container) {
video_holder.performClick()
}
volumeSideScroll = video_volume_controller
volumeSideScroll.initialize(activity!!, slide_info, false, container) {
video_holder.performClick()
}
}
return mView return mView
} }
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
brightnessSideScroll = view.video_brightness_controller
brightnessSideScroll.initialize(activity!!, view.slide_info) {
view.video_holder.performClick()
}
volumeSideScroll = view.video_volume_controller
volumeSideScroll.initialize(activity!!, view.slide_info) {
view.video_holder.performClick()
}
}
override fun onResume() { override fun onResume() {
super.onResume() super.onResume()
activity!!.updateTextColors(mView!!.video_holder) activity!!.updateTextColors(mView!!.video_holder)
@ -151,16 +150,6 @@ class VideoFragment : ViewPagerFragment(), SurfaceHolder.Callback, SeekBar.OnSee
mSurfaceView!!.setOnClickListener { toggleFullscreen() } mSurfaceView!!.setOnClickListener { toggleFullscreen() }
mView!!.video_holder.setOnClickListener { toggleFullscreen() } mView!!.video_holder.setOnClickListener { toggleFullscreen() }
mView!!.video_volume_controller.setOnTouchListener { v, event ->
volumeSideScroll.handleVolumeTouched(event)
true
}
mView!!.video_brightness_controller.setOnTouchListener { v, event ->
volumeSideScroll.handleBrightnessTouched(event)
true
}
initTimeHolder() initTimeHolder()
checkExtendedDetails() checkExtendedDetails()
initMediaPlayer() initMediaPlayer()
@ -269,8 +258,9 @@ class VideoFragment : ViewPagerFragment(), SurfaceHolder.Callback, SeekBar.OnSee
} }
private fun checkFullscreen() { private fun checkFullscreen() {
if (activity == null) if (activity == null) {
return return
}
var anim = android.R.anim.fade_in var anim = android.R.anim.fade_in
if (mIsFullscreen) { if (mIsFullscreen) {

View file

@ -7,6 +7,7 @@ import android.os.Handler
import android.provider.Settings import android.provider.Settings
import android.util.AttributeSet import android.util.AttributeSet
import android.view.MotionEvent import android.view.MotionEvent
import android.view.ViewGroup
import android.widget.RelativeLayout import android.widget.RelativeLayout
import android.widget.TextView import android.widget.TextView
import com.simplemobiletools.gallery.R import com.simplemobiletools.gallery.R
@ -18,33 +19,42 @@ class MediaSideScroll(context: Context, attrs: AttributeSet) : RelativeLayout(co
private var mTouchDownX = 0f private var mTouchDownX = 0f
private var mTouchDownY = 0f private var mTouchDownY = 0f
private var mTouchDownTime = 0L private var mTouchDownTime = 0L
private var mTouchDownVolume = 0 private var mTouchDownValue = -1
private var mTouchDownBrightness = -1
private var mTempBrightness = 0 private var mTempBrightness = 0
private var mLastTouchY = 0f private var mLastTouchY = 0f
private var mIsBrightnessScroll = false
private var mSlideInfoText = "" private var mSlideInfoText = ""
private var mSlideInfoFadeHandler = Handler() private var mSlideInfoFadeHandler = Handler()
private var mParentView: ViewGroup? = null
private lateinit var activity: Activity private lateinit var activity: Activity
private lateinit var slideInfoView: TextView private lateinit var slideInfoView: TextView
private lateinit var callback: () -> Unit private lateinit var callback: () -> Unit
fun initialize(activity: Activity, slideInfoView: TextView, callback: () -> Unit) { fun initialize(activity: Activity, slideInfoView: TextView, isBrightness: Boolean, parentView: ViewGroup?, callback: () -> Unit) {
this.activity = activity this.activity = activity
this.slideInfoView = slideInfoView this.slideInfoView = slideInfoView
this.callback = callback this.callback = callback
mParentView = parentView
mIsBrightnessScroll = isBrightness
mSlideInfoText = activity.getString(if (isBrightness) R.string.brightness else R.string.volume)
} }
fun handleVolumeTouched(event: MotionEvent) { override fun onTouchEvent(event: MotionEvent): Boolean {
when (event.action) { when (event.action) {
MotionEvent.ACTION_DOWN -> { MotionEvent.ACTION_DOWN -> {
mTouchDownX = event.x mTouchDownX = event.x
mTouchDownY = event.y mTouchDownY = event.y
mLastTouchY = event.y mLastTouchY = event.y
mTouchDownTime = System.currentTimeMillis() mTouchDownTime = System.currentTimeMillis()
mTouchDownVolume = getCurrentVolume() if (mIsBrightnessScroll) {
mSlideInfoText = "${activity.getString(R.string.volume)}:\n" if (mTouchDownValue == -1) {
mTouchDownValue = getCurrentBrightness()
}
} else {
mTouchDownValue = getCurrentVolume()
}
} }
MotionEvent.ACTION_MOVE -> { MotionEvent.ACTION_MOVE -> {
val diffX = mTouchDownX - event.x val diffX = mTouchDownX - event.x
@ -56,61 +66,24 @@ class MediaSideScroll(context: Context, attrs: AttributeSet) : RelativeLayout(co
if ((percent == 100 && event.y > mLastTouchY) || (percent == -100 && event.y < mLastTouchY)) { if ((percent == 100 && event.y > mLastTouchY) || (percent == -100 && event.y < mLastTouchY)) {
mTouchDownY = event.y mTouchDownY = event.y
mTouchDownVolume = getCurrentVolume() mTouchDownValue = if (mIsBrightnessScroll) mTempBrightness else getCurrentVolume()
} }
volumePercentChanged(percent) percentChanged(percent)
} }
mLastTouchY = event.y mLastTouchY = event.y
} }
MotionEvent.ACTION_UP, MotionEvent.ACTION_CANCEL -> { MotionEvent.ACTION_UP, MotionEvent.ACTION_CANCEL -> {
val diffX = Math.abs(event.x - mTouchDownX) if (System.currentTimeMillis() - mTouchDownTime < CLICK_MAX_DURATION) {
val diffY = Math.abs(event.y - mTouchDownY)
if (System.currentTimeMillis() - mTouchDownTime < CLICK_MAX_DURATION && diffX < 20 && diffY < 20) {
callback() callback()
} }
if (mIsBrightnessScroll) {
mTouchDownValue = mTempBrightness
}
} }
} }
} return true
fun handleBrightnessTouched(event: MotionEvent) {
when (event.action) {
MotionEvent.ACTION_DOWN -> {
mTouchDownX = event.x
mTouchDownY = event.y
mLastTouchY = event.y
mTouchDownTime = System.currentTimeMillis()
mSlideInfoText = "${activity.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) {
callback()
}
mTouchDownBrightness = mTempBrightness
}
}
} }
private fun getCurrentVolume() = activity.audioManager.getStreamVolume(AudioManager.STREAM_MUSIC) private fun getCurrentVolume() = activity.audioManager.getStreamVolume(AudioManager.STREAM_MUSIC)
@ -123,19 +96,24 @@ class MediaSideScroll(context: Context, attrs: AttributeSet) : RelativeLayout(co
} }
} }
private fun percentChanged(percent: Int) {
if (mIsBrightnessScroll) {
brightnessPercentChanged(percent)
} else {
volumePercentChanged(percent)
}
}
private fun volumePercentChanged(percent: Int) { private fun volumePercentChanged(percent: Int) {
val stream = AudioManager.STREAM_MUSIC val stream = AudioManager.STREAM_MUSIC
val maxVolume = activity.audioManager.getStreamMaxVolume(stream) val maxVolume = activity.audioManager.getStreamMaxVolume(stream)
val percentPerPoint = 100 / maxVolume val percentPerPoint = 100 / maxVolume
val addPoints = percent / percentPerPoint val addPoints = percent / percentPerPoint
val newVolume = Math.min(maxVolume, Math.max(0, mTouchDownVolume + addPoints)) val newVolume = Math.min(maxVolume, Math.max(0, mTouchDownValue + addPoints))
activity.audioManager.setStreamVolume(stream, newVolume, 0) activity.audioManager.setStreamVolume(stream, newVolume, 0)
val absolutePercent = ((newVolume / maxVolume.toFloat()) * 100).toInt() val absolutePercent = ((newVolume / maxVolume.toFloat()) * 100).toInt()
slideInfoView.apply { showValue(absolutePercent)
text = "$mSlideInfoText$absolutePercent%"
alpha = 1f
}
mSlideInfoFadeHandler.removeCallbacksAndMessages(null) mSlideInfoFadeHandler.removeCallbacksAndMessages(null)
mSlideInfoFadeHandler.postDelayed({ mSlideInfoFadeHandler.postDelayed({
@ -145,15 +123,12 @@ class MediaSideScroll(context: Context, attrs: AttributeSet) : RelativeLayout(co
private fun brightnessPercentChanged(percent: Int) { private fun brightnessPercentChanged(percent: Int) {
val maxBrightness = 255f val maxBrightness = 255f
var newBrightness = (mTouchDownBrightness + 2.55 * percent).toFloat() var newBrightness = (mTouchDownValue + 2.55 * percent).toFloat()
newBrightness = Math.min(maxBrightness, Math.max(0f, newBrightness)) newBrightness = Math.min(maxBrightness, Math.max(0f, newBrightness))
mTempBrightness = newBrightness.toInt() mTempBrightness = newBrightness.toInt()
val absolutePercent = ((newBrightness / maxBrightness) * 100).toInt() val absolutePercent = ((newBrightness / maxBrightness) * 100).toInt()
slideInfoView.apply { showValue(absolutePercent)
text = "$mSlideInfoText$absolutePercent%"
alpha = 1f
}
val attributes = activity.window.attributes val attributes = activity.window.attributes
attributes.screenBrightness = absolutePercent / 100f attributes.screenBrightness = absolutePercent / 100f
@ -164,4 +139,11 @@ class MediaSideScroll(context: Context, attrs: AttributeSet) : RelativeLayout(co
slideInfoView.animate().alpha(0f) slideInfoView.animate().alpha(0f)
}, SLIDE_INFO_FADE_DELAY) }, SLIDE_INFO_FADE_DELAY)
} }
private fun showValue(percent: Int) {
slideInfoView.apply {
text = "$mSlideInfoText:\n$percent%"
alpha = 1f
}
}
} }

View file

@ -25,12 +25,12 @@
android:layout_width="@dimen/media_side_slider_width" android:layout_width="@dimen/media_side_slider_width"
android:layout_height="match_parent"/> android:layout_height="match_parent"/>
<RelativeLayout <com.simplemobiletools.gallery.views.InstantItemSwitch
android:id="@+id/instant_prev_item" android:id="@+id/instant_prev_item"
android:layout_width="@dimen/instant_change_bar_width" android:layout_width="@dimen/instant_change_bar_width"
android:layout_height="match_parent"/> android:layout_height="match_parent"/>
<RelativeLayout <com.simplemobiletools.gallery.views.InstantItemSwitch
android:id="@+id/instant_next_item" android:id="@+id/instant_next_item"
android:layout_width="@dimen/instant_change_bar_width" android:layout_width="@dimen/instant_change_bar_width"
android:layout_height="match_parent" android:layout_height="match_parent"