mirror of
https://github.com/libre-tube/LibreTube.git
synced 2025-04-28 16:00:31 +05:30
Fix brightness inconsistency
Brightness changes affect only when player is in full-screen (landscape) mode. Store brightness value to shared preferences which can be restored for later use.
This commit is contained in:
parent
8de48a0d86
commit
92cc7a9cf3
@ -84,6 +84,7 @@ object PreferenceKeys {
|
|||||||
const val USE_HLS_OVER_DASH = "use_hls"
|
const val USE_HLS_OVER_DASH = "use_hls"
|
||||||
const val QUEUE_AUTO_INSERT_RELATED = "queue_insert_related_videos"
|
const val QUEUE_AUTO_INSERT_RELATED = "queue_insert_related_videos"
|
||||||
const val PLAYER_SWIPE_CONTROLS = "player_swipe_controls"
|
const val PLAYER_SWIPE_CONTROLS = "player_swipe_controls"
|
||||||
|
const val PLAYER_SCREEN_BRIGHTNESS = "player_screen_brightness"
|
||||||
const val SHOW_OPEN_WITH = "show_open_with"
|
const val SHOW_OPEN_WITH = "show_open_with"
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -9,7 +9,6 @@ import android.os.Looper
|
|||||||
import android.util.AttributeSet
|
import android.util.AttributeSet
|
||||||
import android.view.MotionEvent
|
import android.view.MotionEvent
|
||||||
import android.view.View
|
import android.view.View
|
||||||
import android.view.WindowManager
|
|
||||||
import com.github.libretube.R
|
import com.github.libretube.R
|
||||||
import com.github.libretube.databinding.DoubleTapOverlayBinding
|
import com.github.libretube.databinding.DoubleTapOverlayBinding
|
||||||
import com.github.libretube.databinding.ExoStyledPlayerControlViewBinding
|
import com.github.libretube.databinding.ExoStyledPlayerControlViewBinding
|
||||||
@ -320,15 +319,12 @@ internal class CustomExoPlayerView(
|
|||||||
}
|
}
|
||||||
|
|
||||||
private fun initializeGestureProgress() {
|
private fun initializeGestureProgress() {
|
||||||
val brightnessBar = gestureViewBinding.brightnessProgressBar
|
gestureViewBinding.brightnessProgressBar.let { bar ->
|
||||||
val volumeBar = gestureViewBinding.volumeProgressBar
|
bar.progress = brightnessHelper.getBrightnessWithScale(bar.max.toFloat(), saved = true).toInt()
|
||||||
|
}
|
||||||
brightnessBar.progress = if (brightnessHelper.brightness == WindowManager.LayoutParams.BRIGHTNESS_OVERRIDE_NONE) {
|
gestureViewBinding.volumeProgressBar.let { bar ->
|
||||||
25.normalize(0, 100, 0, volumeBar.max)
|
bar.progress = audioHelper.getVolumeWithScale(bar.max)
|
||||||
} else {
|
|
||||||
brightnessHelper.getBrightnessWithScale(brightnessBar.max.toFloat()).toInt()
|
|
||||||
}
|
}
|
||||||
volumeBar.progress = audioHelper.getVolumeWithScale(volumeBar.max)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun updateBrightness(distance: Float) {
|
private fun updateBrightness(distance: Float) {
|
||||||
@ -445,6 +441,13 @@ internal class CustomExoPlayerView(
|
|||||||
params.bottomMargin = offset.toInt()
|
params.bottomMargin = offset.toInt()
|
||||||
it.layoutParams = params
|
it.layoutParams = params
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (PlayerHelper.swipeGestureEnabled) {
|
||||||
|
when (newConfig?.orientation) {
|
||||||
|
Configuration.ORIENTATION_LANDSCAPE -> brightnessHelper.restoreSavedBrightness()
|
||||||
|
else -> brightnessHelper.resetToSystemBrightness(true)
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun onSingleTap() {
|
override fun onSingleTap() {
|
||||||
|
@ -2,6 +2,7 @@ package com.github.libretube.util
|
|||||||
|
|
||||||
import android.app.Activity
|
import android.app.Activity
|
||||||
import android.view.WindowManager
|
import android.view.WindowManager
|
||||||
|
import com.github.libretube.constants.PreferenceKeys
|
||||||
import com.github.libretube.extensions.normalize
|
import com.github.libretube.extensions.normalize
|
||||||
|
|
||||||
class BrightnessHelper(activity: Activity) {
|
class BrightnessHelper(activity: Activity) {
|
||||||
@ -13,26 +14,60 @@ class BrightnessHelper(activity: Activity) {
|
|||||||
/**
|
/**
|
||||||
* Wrapper for the current screen brightness
|
* Wrapper for the current screen brightness
|
||||||
*/
|
*/
|
||||||
var brightness: Float
|
private var brightness: Float
|
||||||
get() = window.attributes.screenBrightness
|
get() = window.attributes.screenBrightness
|
||||||
private set(value) {
|
set(value) {
|
||||||
val lp = window.attributes
|
val lp = window.attributes
|
||||||
lp.screenBrightness = value
|
lp.screenBrightness = value
|
||||||
window.attributes = lp
|
window.attributes = lp
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Restore screen brightness to device system brightness.
|
* Wrapper for the brightness persisted in the shared preferences.
|
||||||
*/
|
*/
|
||||||
fun resetToSystemBrightness() {
|
private var savedBrightness: Float
|
||||||
|
get() = PreferenceHelper.getFloat(PreferenceKeys.PLAYER_SCREEN_BRIGHTNESS, brightness)
|
||||||
|
set(value) = PreferenceHelper.putFloat(PreferenceKeys.PLAYER_SCREEN_BRIGHTNESS, value)
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Restore screen brightness to device system brightness.
|
||||||
|
* [saveOldValue] determines whether the old value should be persisted or not.
|
||||||
|
*/
|
||||||
|
fun resetToSystemBrightness(saveOldValue: Boolean = false) {
|
||||||
|
savedBrightness = if (saveOldValue) {
|
||||||
|
brightness
|
||||||
|
} else {
|
||||||
|
WindowManager.LayoutParams.BRIGHTNESS_OVERRIDE_NONE
|
||||||
|
}
|
||||||
|
|
||||||
brightness = WindowManager.LayoutParams.BRIGHTNESS_OVERRIDE_NONE
|
brightness = WindowManager.LayoutParams.BRIGHTNESS_OVERRIDE_NONE
|
||||||
}
|
}
|
||||||
|
|
||||||
fun setBrightnessWithScale(value: Float, maxValue: Float, minValue: Float = 0.0f) {
|
/**
|
||||||
brightness = value.normalize(minValue, maxValue, minBrightness, maxBrightness)
|
* Set current screen brightness to saved brightness value.
|
||||||
|
*/
|
||||||
|
fun restoreSavedBrightness() {
|
||||||
|
brightness = savedBrightness
|
||||||
}
|
}
|
||||||
|
|
||||||
fun getBrightnessWithScale(maxValue: Float, minValue: Float = 0.0f): Float {
|
/**
|
||||||
return brightness.normalize(minBrightness, maxBrightness, minValue, maxValue)
|
* Set current brightness value with scaling to given range.
|
||||||
|
* [shouldSave] determines whether the value should be persisted.
|
||||||
|
*/
|
||||||
|
fun setBrightnessWithScale(value: Float, maxValue: Float, minValue: Float = 0.0f, shouldSave: Boolean = false) {
|
||||||
|
brightness = value.normalize(minValue, maxValue, minBrightness, maxBrightness)
|
||||||
|
if (shouldSave) savedBrightness = brightness
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get scaled brightness with given range. if [saved] is
|
||||||
|
* ture value will be retrived from shared preferences.
|
||||||
|
*/
|
||||||
|
fun getBrightnessWithScale(maxValue: Float, minValue: Float = 0.0f, saved: Boolean = false): Float {
|
||||||
|
return if (saved) {
|
||||||
|
savedBrightness.normalize(minBrightness, maxBrightness, minValue, maxValue)
|
||||||
|
} else {
|
||||||
|
brightness.normalize(minBrightness, maxBrightness, minValue, maxValue)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -41,6 +41,10 @@ object PreferenceHelper {
|
|||||||
editor.putInt(key, value).commit()
|
editor.putInt(key, value).commit()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fun putFloat(key: String, value: Float) {
|
||||||
|
editor.putFloat(key, value).commit()
|
||||||
|
}
|
||||||
|
|
||||||
fun getString(key: String?, defValue: String): String {
|
fun getString(key: String?, defValue: String): String {
|
||||||
return settings.getString(key, defValue) ?: defValue
|
return settings.getString(key, defValue) ?: defValue
|
||||||
}
|
}
|
||||||
@ -53,6 +57,10 @@ object PreferenceHelper {
|
|||||||
return settings.getInt(key, defValue)
|
return settings.getInt(key, defValue)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fun getFloat(key: String?, defValue: Float): Float {
|
||||||
|
return settings.getFloat(key, defValue)
|
||||||
|
}
|
||||||
|
|
||||||
fun clearPreferences() {
|
fun clearPreferences() {
|
||||||
editor.clear().apply()
|
editor.clear().apply()
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user