mirror of
https://github.com/libre-tube/LibreTube.git
synced 2025-04-29 00:10:32 +05:30
Merge pull request #2071 from Kruna1Pate1/fix/brightness-consistency
Fix brightness inconsistency
This commit is contained in:
commit
fd9ab4365a
@ -84,6 +84,7 @@ object PreferenceKeys {
|
||||
const val USE_HLS_OVER_DASH = "use_hls"
|
||||
const val QUEUE_AUTO_INSERT_RELATED = "queue_insert_related_videos"
|
||||
const val PLAYER_SWIPE_CONTROLS = "player_swipe_controls"
|
||||
const val PLAYER_SCREEN_BRIGHTNESS = "player_screen_brightness"
|
||||
const val SHOW_OPEN_WITH = "show_open_with"
|
||||
|
||||
/**
|
||||
|
@ -9,7 +9,6 @@ import android.os.Looper
|
||||
import android.util.AttributeSet
|
||||
import android.view.MotionEvent
|
||||
import android.view.View
|
||||
import android.view.WindowManager
|
||||
import com.github.libretube.R
|
||||
import com.github.libretube.databinding.DoubleTapOverlayBinding
|
||||
import com.github.libretube.databinding.ExoStyledPlayerControlViewBinding
|
||||
@ -320,15 +319,12 @@ internal class CustomExoPlayerView(
|
||||
}
|
||||
|
||||
private fun initializeGestureProgress() {
|
||||
val brightnessBar = gestureViewBinding.brightnessProgressBar
|
||||
val volumeBar = gestureViewBinding.volumeProgressBar
|
||||
|
||||
brightnessBar.progress = if (brightnessHelper.brightness == WindowManager.LayoutParams.BRIGHTNESS_OVERRIDE_NONE) {
|
||||
25.normalize(0, 100, 0, volumeBar.max)
|
||||
} else {
|
||||
brightnessHelper.getBrightnessWithScale(brightnessBar.max.toFloat()).toInt()
|
||||
gestureViewBinding.brightnessProgressBar.let { bar ->
|
||||
bar.progress = brightnessHelper.getBrightnessWithScale(bar.max.toFloat(), saved = true).toInt()
|
||||
}
|
||||
gestureViewBinding.volumeProgressBar.let { bar ->
|
||||
bar.progress = audioHelper.getVolumeWithScale(bar.max)
|
||||
}
|
||||
volumeBar.progress = audioHelper.getVolumeWithScale(volumeBar.max)
|
||||
}
|
||||
|
||||
private fun updateBrightness(distance: Float) {
|
||||
@ -445,6 +441,13 @@ internal class CustomExoPlayerView(
|
||||
params.bottomMargin = offset.toInt()
|
||||
it.layoutParams = params
|
||||
}
|
||||
|
||||
if (PlayerHelper.swipeGestureEnabled) {
|
||||
when (newConfig?.orientation) {
|
||||
Configuration.ORIENTATION_LANDSCAPE -> brightnessHelper.restoreSavedBrightness()
|
||||
else -> brightnessHelper.resetToSystemBrightness(false)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
override fun onSingleTap() {
|
||||
|
@ -1,10 +1,12 @@
|
||||
package com.github.libretube.util
|
||||
|
||||
import android.app.Activity
|
||||
import android.os.Build
|
||||
import android.view.WindowManager
|
||||
import com.github.libretube.constants.PreferenceKeys
|
||||
import com.github.libretube.extensions.normalize
|
||||
|
||||
class BrightnessHelper(activity: Activity) {
|
||||
class BrightnessHelper(private val activity: Activity) {
|
||||
|
||||
private val window = activity.window
|
||||
private val minBrightness = 0.0f
|
||||
@ -13,26 +15,61 @@ class BrightnessHelper(activity: Activity) {
|
||||
/**
|
||||
* Wrapper for the current screen brightness
|
||||
*/
|
||||
var brightness: Float
|
||||
private var brightness: Float
|
||||
get() = window.attributes.screenBrightness
|
||||
private set(value) {
|
||||
set(value) {
|
||||
val lp = window.attributes
|
||||
lp.screenBrightness = value
|
||||
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.
|
||||
* if [forced] is false then value will be stored only if it's not
|
||||
* [WindowManager.LayoutParams.BRIGHTNESS_OVERRIDE_NONE] value.
|
||||
*/
|
||||
fun resetToSystemBrightness(forced: Boolean = true) {
|
||||
if (forced || brightness != WindowManager.LayoutParams.BRIGHTNESS_OVERRIDE_NONE) {
|
||||
savedBrightness = brightness
|
||||
}
|
||||
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() {
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N && activity.isInPictureInPictureMode) {
|
||||
return
|
||||
}
|
||||
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()
|
||||
}
|
||||
|
||||
fun putFloat(key: String, value: Float) {
|
||||
editor.putFloat(key, value).commit()
|
||||
}
|
||||
|
||||
fun getString(key: String?, defValue: String): String {
|
||||
return settings.getString(key, defValue) ?: defValue
|
||||
}
|
||||
@ -53,6 +57,10 @@ object PreferenceHelper {
|
||||
return settings.getInt(key, defValue)
|
||||
}
|
||||
|
||||
fun getFloat(key: String?, defValue: Float): Float {
|
||||
return settings.getFloat(key, defValue)
|
||||
}
|
||||
|
||||
fun clearPreferences() {
|
||||
editor.clear().apply()
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user