From b0562e7ed10a1a2d420ac74bedfda068fd508c86 Mon Sep 17 00:00:00 2001 From: tibbi Date: Mon, 7 Aug 2017 21:56:25 +0200 Subject: [PATCH] allow controlling the brightness with sliding too --- .../gallery/fragments/VideoFragment.kt | 70 +++++++++++++++++++ app/src/main/res/layout/pager_video_item.xml | 5 ++ 2 files changed, 75 insertions(+) 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 5914da11f..f93bc617e 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/fragments/VideoFragment.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/fragments/VideoFragment.kt @@ -7,6 +7,7 @@ import android.net.Uri import android.os.Build import android.os.Bundle import android.os.Handler +import android.provider.Settings import android.util.DisplayMetrics import android.util.Log import android.view.* @@ -51,6 +52,8 @@ class VideoFragment : ViewPagerFragment(), SurfaceHolder.Callback, SeekBar.OnSee 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 = "" @@ -100,6 +103,11 @@ class VideoFragment : ViewPagerFragment(), SurfaceHolder.Callback, SeekBar.OnSee true } + mView.video_brightness_controller.setOnTouchListener { v, event -> + handleBrightnessTouched(event) + true + } + initTimeHolder() } @@ -164,8 +172,48 @@ class VideoFragment : ViewPagerFragment(), SurfaceHolder.Callback, SeekBar.OnSee } } + 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) > 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 -> { + if (System.currentTimeMillis() - mTouchDownTime < CLICK_MAX_DURATION) { + 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) @@ -186,6 +234,28 @@ class VideoFragment : ViewPagerFragment(), SurfaceHolder.Callback, SeekBar.OnSee }, 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() { mTimeHolder = mView.video_time_holder val res = resources diff --git a/app/src/main/res/layout/pager_video_item.xml b/app/src/main/res/layout/pager_video_item.xml index 47de68c41..3d88f52f7 100644 --- a/app/src/main/res/layout/pager_video_item.xml +++ b/app/src/main/res/layout/pager_video_item.xml @@ -19,6 +19,11 @@ android:layout_alignParentEnd="true" android:layout_alignParentRight="true"/> + +