From 4449b96d6ee2832df3c50500ce272563d3ecae6e Mon Sep 17 00:00:00 2001 From: tibbi Date: Fri, 9 Feb 2018 17:51:42 +0100 Subject: [PATCH] handle touch events in MediaSideScroll itself --- .../gallery/fragments/PhotoFragment.kt | 28 ++--- .../gallery/fragments/VideoFragment.kt | 38 +++---- .../gallery/helpers/MediaSideScroll.kt | 104 ++++++++---------- app/src/main/res/layout/pager_video_item.xml | 4 +- 4 files changed, 67 insertions(+), 107 deletions(-) diff --git a/app/src/main/kotlin/com/simplemobiletools/gallery/fragments/PhotoFragment.kt b/app/src/main/kotlin/com/simplemobiletools/gallery/fragments/PhotoFragment.kt index 2d881aac5..6c7ac8d6a 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/fragments/PhotoFragment.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/fragments/PhotoFragment.kt @@ -30,7 +30,6 @@ import com.simplemobiletools.gallery.activities.ViewPagerActivity import com.simplemobiletools.gallery.extensions.* import com.simplemobiletools.gallery.helpers.GlideRotateTransformation import com.simplemobiletools.gallery.helpers.MEDIUM -import com.simplemobiletools.gallery.helpers.MediaSideScroll import com.simplemobiletools.gallery.models.Medium import it.sephiroth.android.library.exif2.ExifInterface import kotlinx.android.synthetic.main.pager_photo_item.view.* @@ -50,8 +49,6 @@ class PhotoFragment : ViewPagerFragment() { private var storedHideExtendedDetails = false private var storedExtendedDetails = 0 - private lateinit var brightnessSideScroll: MediaSideScroll - lateinit var view: ViewGroup lateinit var medium: Medium @@ -65,9 +62,14 @@ class PhotoFragment : ViewPagerFragment() { instant_prev_item.parentView = container instant_next_item.parentView = container - photo_brightness_controller.setOnTouchListener { v, event -> - brightnessSideScroll.handleBrightnessTouched(event) - true + photo_brightness_controller.initialize(activity!!, slide_info, true, container) { + view.apply { + if (subsampling_view.isVisible()) { + subsampling_view.performClick() + } else { + gif_view.performClick() + } + } } } @@ -116,20 +118,6 @@ class PhotoFragment : ViewPagerFragment() { 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() { super.onPause() storeStateVariables() 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 3af74be26..dfd152524 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/fragments/VideoFragment.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/fragments/VideoFragment.kt @@ -83,22 +83,21 @@ class VideoFragment : ViewPagerFragment(), SurfaceHolder.Callback, SeekBar.OnSee checkFullscreen() 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 } - 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() { super.onResume() activity!!.updateTextColors(mView!!.video_holder) @@ -151,16 +150,6 @@ class VideoFragment : ViewPagerFragment(), SurfaceHolder.Callback, SeekBar.OnSee mSurfaceView!!.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() checkExtendedDetails() initMediaPlayer() @@ -269,8 +258,9 @@ class VideoFragment : ViewPagerFragment(), SurfaceHolder.Callback, SeekBar.OnSee } private fun checkFullscreen() { - if (activity == null) + if (activity == null) { return + } var anim = android.R.anim.fade_in if (mIsFullscreen) { diff --git a/app/src/main/kotlin/com/simplemobiletools/gallery/helpers/MediaSideScroll.kt b/app/src/main/kotlin/com/simplemobiletools/gallery/helpers/MediaSideScroll.kt index f6f964461..43e832084 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/helpers/MediaSideScroll.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/helpers/MediaSideScroll.kt @@ -7,6 +7,7 @@ import android.os.Handler import android.provider.Settings import android.util.AttributeSet import android.view.MotionEvent +import android.view.ViewGroup import android.widget.RelativeLayout import android.widget.TextView import com.simplemobiletools.gallery.R @@ -18,33 +19,42 @@ class MediaSideScroll(context: Context, attrs: AttributeSet) : RelativeLayout(co private var mTouchDownX = 0f private var mTouchDownY = 0f private var mTouchDownTime = 0L - private var mTouchDownVolume = 0 - private var mTouchDownBrightness = -1 + private var mTouchDownValue = -1 private var mTempBrightness = 0 private var mLastTouchY = 0f + private var mIsBrightnessScroll = false private var mSlideInfoText = "" private var mSlideInfoFadeHandler = Handler() + private var mParentView: ViewGroup? = null private lateinit var activity: Activity private lateinit var slideInfoView: TextView 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.slideInfoView = slideInfoView 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) { MotionEvent.ACTION_DOWN -> { mTouchDownX = event.x mTouchDownY = event.y mLastTouchY = event.y mTouchDownTime = System.currentTimeMillis() - mTouchDownVolume = getCurrentVolume() - mSlideInfoText = "${activity.getString(R.string.volume)}:\n" + if (mIsBrightnessScroll) { + if (mTouchDownValue == -1) { + mTouchDownValue = getCurrentBrightness() + } + } else { + mTouchDownValue = getCurrentVolume() + } } MotionEvent.ACTION_MOVE -> { 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)) { mTouchDownY = event.y - mTouchDownVolume = getCurrentVolume() + mTouchDownValue = if (mIsBrightnessScroll) mTempBrightness else getCurrentVolume() } - volumePercentChanged(percent) + percentChanged(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) { + if (System.currentTimeMillis() - mTouchDownTime < CLICK_MAX_DURATION) { callback() } + + if (mIsBrightnessScroll) { + mTouchDownValue = mTempBrightness + } } } - } - - 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 - } - } + return true } 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) { val stream = AudioManager.STREAM_MUSIC val maxVolume = activity.audioManager.getStreamMaxVolume(stream) val percentPerPoint = 100 / maxVolume 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) val absolutePercent = ((newVolume / maxVolume.toFloat()) * 100).toInt() - slideInfoView.apply { - text = "$mSlideInfoText$absolutePercent%" - alpha = 1f - } + showValue(absolutePercent) mSlideInfoFadeHandler.removeCallbacksAndMessages(null) mSlideInfoFadeHandler.postDelayed({ @@ -145,15 +123,12 @@ class MediaSideScroll(context: Context, attrs: AttributeSet) : RelativeLayout(co private fun brightnessPercentChanged(percent: Int) { 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)) mTempBrightness = newBrightness.toInt() val absolutePercent = ((newBrightness / maxBrightness) * 100).toInt() - slideInfoView.apply { - text = "$mSlideInfoText$absolutePercent%" - alpha = 1f - } + showValue(absolutePercent) val attributes = activity.window.attributes attributes.screenBrightness = absolutePercent / 100f @@ -164,4 +139,11 @@ class MediaSideScroll(context: Context, attrs: AttributeSet) : RelativeLayout(co slideInfoView.animate().alpha(0f) }, SLIDE_INFO_FADE_DELAY) } + + private fun showValue(percent: Int) { + slideInfoView.apply { + text = "$mSlideInfoText:\n$percent%" + alpha = 1f + } + } } diff --git a/app/src/main/res/layout/pager_video_item.xml b/app/src/main/res/layout/pager_video_item.xml index 6d0ea0763..8b3291419 100644 --- a/app/src/main/res/layout/pager_video_item.xml +++ b/app/src/main/res/layout/pager_video_item.xml @@ -25,12 +25,12 @@ android:layout_width="@dimen/media_side_slider_width" android:layout_height="match_parent"/> - -