fixed brightness reset after soft close
comments
This commit is contained in:
MarcaDian 2024-04-20 12:21:16 +03:00
parent 1fbbfd90f3
commit 7c0b506b90
No known key found for this signature in database
GPG Key ID: 904EF10755E7C016
3 changed files with 37 additions and 22 deletions

View File

@ -111,7 +111,7 @@ class SwipeControlsConfigurationProvider(
get() = Settings.SWIPE_ENABLE_LOWEST_VALUE_AUTO_BRIGHTNESS.get()
/**
* variable that stores the brightness gesture value
* variable that stores the brightness gesture value in the settings
*/
var savedScreenBrightnessValue: Float
get() = Settings.SWIPE_BRIGHTNESS_VALUE.get()

View File

@ -166,22 +166,31 @@ class SwipeControlsHostActivity : Activity() {
contentRoot.addView(overlay)
}
// Flag that indicates whether the brightness has been saved and restored default brightness
private var isBrightnessSaved = false
/**
* called when the player type changes
*
* @param type the new player type
*/
private fun onPlayerTypeChanged(type: PlayerType) {
if (config.shouldSaveAndRestoreBrightness) {
when (type) {
PlayerType.WATCH_WHILE_FULLSCREEN -> screen?.restore()
else -> {
screen?.save()
screen?.restoreDefaultBrightness()
}
when {
// If saving and restoring brightness is enabled, and the player type is WATCH_WHILE_FULLSCREEN,
// and brightness has already been saved, then restore the screen brightness
config.shouldSaveAndRestoreBrightness && type == PlayerType.WATCH_WHILE_FULLSCREEN && isBrightnessSaved -> {
screen?.restore()
isBrightnessSaved = false
}
} else {
screen?.restoreDefaultBrightness() // here was lost the function of restore the default brightness
// If saving and restoring brightness is enabled, and brightness has not been saved,
// then save the current screen state, restore default brightness, and mark brightness as saved
config.shouldSaveAndRestoreBrightness && !isBrightnessSaved -> {
screen?.save()
screen?.restoreDefaultBrightness()
isBrightnessSaved = true
}
// If saving and restoring brightness is disabled, simply keep the default brightness
else -> screen?.restoreDefaultBrightness()
}
}

View File

@ -12,11 +12,9 @@ import app.revanced.integrations.youtube.swipecontrols.misc.clamp
*/
class ScreenBrightnessController(
private val host: Activity,
val config: SwipeControlsConfigurationProvider,
val config: SwipeControlsConfigurationProvider = SwipeControlsConfigurationProvider(host),
) {
constructor(context: Activity) : this(context, SwipeControlsConfigurationProvider(context))
/**
* the current screen brightness in percent, ranging from 0.0 to 100.0
*/
@ -26,25 +24,31 @@ class ScreenBrightnessController(
rawScreenBrightness = (value.toFloat() / 100f).clamp(0f, 1f)
}
/**
* restore the screen brightness to the default device brightness
*/
fun restoreDefaultBrightness() {
rawScreenBrightness = WindowManager.LayoutParams.BRIGHTNESS_OVERRIDE_NONE
}
/**
* is the screen brightness set to device- default?
*/
val isDefaultBrightness
get() = (rawScreenBrightness == WindowManager.LayoutParams.BRIGHTNESS_OVERRIDE_NONE)
/**
* restore the screen brightness to the default device brightness
*/
fun restoreDefaultBrightness() {
rawScreenBrightness = WindowManager.LayoutParams.BRIGHTNESS_OVERRIDE_NONE
}
// Flag that indicates whether the brightness has been restored
private var isBrightnessRestored = false
/**
* save the current screen brightness into settings, to be brought back using [restore]
*/
fun save() {
if (config.savedScreenBrightnessValue == -1f) {
if (isBrightnessRestored) {
// Saves the current screen brightness value into settings
config.savedScreenBrightnessValue = rawScreenBrightness
// Reset the flag
isBrightnessRestored = false
}
}
@ -52,8 +56,10 @@ class ScreenBrightnessController(
* restore the screen brightness from settings saved using [save]
*/
fun restore() {
// Restores the screen brightness value from the saved settings
rawScreenBrightness = config.savedScreenBrightnessValue
config.savedScreenBrightnessValue = -1f
// Mark that brightness has been restored
isBrightnessRestored = true
}
/**