revanced-integrations/app/src/main/java/app/revanced/integrations/youtube/swipecontrols/controller/ScreenBrightnessController.kt
MarcaDian 7c0b506b90
refactor
fixed brightness reset after soft close
comments
2024-04-20 12:21:16 +03:00

75 lines
2.4 KiB
Kotlin

package app.revanced.integrations.youtube.swipecontrols.controller
import android.app.Activity
import android.view.WindowManager
import app.revanced.integrations.youtube.swipecontrols.SwipeControlsConfigurationProvider
import app.revanced.integrations.youtube.swipecontrols.misc.clamp
/**
* controller to adjust the screen brightness level
*
* @param host the host activity of which the brightness is adjusted
*/
class ScreenBrightnessController(
private val host: Activity,
val config: SwipeControlsConfigurationProvider = SwipeControlsConfigurationProvider(host),
) {
/**
* the current screen brightness in percent, ranging from 0.0 to 100.0
*/
var screenBrightness: Double
get() = rawScreenBrightness * 100.0
set(value) {
rawScreenBrightness = (value.toFloat() / 100f).clamp(0f, 1f)
}
/**
* 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 (isBrightnessRestored) {
// Saves the current screen brightness value into settings
config.savedScreenBrightnessValue = rawScreenBrightness
// Reset the flag
isBrightnessRestored = false
}
}
/**
* restore the screen brightness from settings saved using [save]
*/
fun restore() {
// Restores the screen brightness value from the saved settings
rawScreenBrightness = config.savedScreenBrightnessValue
// Mark that brightness has been restored
isBrightnessRestored = true
}
/**
* wrapper for the raw screen brightness in [WindowManager.LayoutParams.screenBrightness]
*/
var rawScreenBrightness: Float
get() = host.window.attributes.screenBrightness
private set(value) {
val attr = host.window.attributes
attr.screenBrightness = value
host.window.attributes = attr
}
}